OpenACC: Basic support for #pragma acc parallel.
[official-gcc.git] / gcc / c / c-typeck.c
blobe7096e6318813e33460327d15010ec759863c3f1
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
24 and some optimization. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "langhooks.h"
32 #include "c-tree.h"
33 #include "c-lang.h"
34 #include "flags.h"
35 #include "intl.h"
36 #include "target.h"
37 #include "tree-iterator.h"
38 #include "bitmap.h"
39 #include "gimple.h"
40 #include "tree-inline.h"
41 #include "omp-low.h"
42 #include "c-family/c-objc.h"
43 #include "c-family/c-common.h"
44 #include "c-family/c-ubsan.h"
46 /* Possible cases of implicit bad conversions. Used to select
47 diagnostic messages in convert_for_assignment. */
48 enum impl_conv {
49 ic_argpass,
50 ic_assign,
51 ic_init,
52 ic_return
55 /* The level of nesting inside "__alignof__". */
56 int in_alignof;
58 /* The level of nesting inside "sizeof". */
59 int in_sizeof;
61 /* The level of nesting inside "typeof". */
62 int in_typeof;
64 /* The argument of last parsed sizeof expression, only to be tested
65 if expr.original_code == SIZEOF_EXPR. */
66 tree c_last_sizeof_arg;
68 /* Nonzero if we've already printed a "missing braces around initializer"
69 message within this initializer. */
70 static int missing_braces_mentioned;
72 static int require_constant_value;
73 static int require_constant_elements;
75 static bool null_pointer_constant_p (const_tree);
76 static tree qualify_type (tree, tree);
77 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
78 bool *);
79 static int comp_target_types (location_t, tree, tree);
80 static int function_types_compatible_p (const_tree, const_tree, bool *,
81 bool *);
82 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
83 static tree lookup_field (tree, tree);
84 static int convert_arguments (tree, vec<tree, va_gc> *, vec<tree, va_gc> *,
85 tree, tree);
86 static tree pointer_diff (location_t, tree, tree);
87 static tree convert_for_assignment (location_t, tree, tree, tree,
88 enum impl_conv, bool, tree, tree, int);
89 static tree valid_compound_expr_initializer (tree, tree);
90 static void push_string (const char *);
91 static void push_member_name (tree);
92 static int spelling_length (void);
93 static char *print_spelling (char *);
94 static void warning_init (int, const char *);
95 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
96 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
97 struct obstack *);
98 static void output_pending_init_elements (int, struct obstack *);
99 static int set_designator (int, struct obstack *);
100 static void push_range_stack (tree, struct obstack *);
101 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
102 static void set_nonincremental_init (struct obstack *);
103 static void set_nonincremental_init_from_string (tree, struct obstack *);
104 static tree find_init_member (tree, struct obstack *);
105 static void readonly_warning (tree, enum lvalue_use);
106 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
107 static void record_maybe_used_decl (tree);
108 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
110 /* Return true if EXP is a null pointer constant, false otherwise. */
112 static bool
113 null_pointer_constant_p (const_tree expr)
115 /* This should really operate on c_expr structures, but they aren't
116 yet available everywhere required. */
117 tree type = TREE_TYPE (expr);
118 return (TREE_CODE (expr) == INTEGER_CST
119 && !TREE_OVERFLOW (expr)
120 && integer_zerop (expr)
121 && (INTEGRAL_TYPE_P (type)
122 || (TREE_CODE (type) == POINTER_TYPE
123 && VOID_TYPE_P (TREE_TYPE (type))
124 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
127 /* EXPR may appear in an unevaluated part of an integer constant
128 expression, but not in an evaluated part. Wrap it in a
129 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
130 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
132 static tree
133 note_integer_operands (tree expr)
135 tree ret;
136 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
138 ret = copy_node (expr);
139 TREE_OVERFLOW (ret) = 1;
141 else
143 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
144 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
146 return ret;
149 /* Having checked whether EXPR may appear in an unevaluated part of an
150 integer constant expression and found that it may, remove any
151 C_MAYBE_CONST_EXPR noting this fact and return the resulting
152 expression. */
154 static inline tree
155 remove_c_maybe_const_expr (tree expr)
157 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
158 return C_MAYBE_CONST_EXPR_EXPR (expr);
159 else
160 return expr;
163 \f/* This is a cache to hold if two types are compatible or not. */
165 struct tagged_tu_seen_cache {
166 const struct tagged_tu_seen_cache * next;
167 const_tree t1;
168 const_tree t2;
169 /* The return value of tagged_types_tu_compatible_p if we had seen
170 these two types already. */
171 int val;
174 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
175 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
177 /* Do `exp = require_complete_type (exp);' to make sure exp
178 does not have an incomplete type. (That includes void types.) */
180 tree
181 require_complete_type (tree value)
183 tree type = TREE_TYPE (value);
185 if (value == error_mark_node || type == error_mark_node)
186 return error_mark_node;
188 /* First, detect a valid value with a complete type. */
189 if (COMPLETE_TYPE_P (type))
190 return value;
192 c_incomplete_type_error (value, type);
193 return error_mark_node;
196 /* Print an error message for invalid use of an incomplete type.
197 VALUE is the expression that was used (or 0 if that isn't known)
198 and TYPE is the type that was invalid. */
200 void
201 c_incomplete_type_error (const_tree value, const_tree type)
203 const char *type_code_string;
205 /* Avoid duplicate error message. */
206 if (TREE_CODE (type) == ERROR_MARK)
207 return;
209 if (value != 0 && (TREE_CODE (value) == VAR_DECL
210 || TREE_CODE (value) == PARM_DECL))
211 error ("%qD has an incomplete type", value);
212 else
214 retry:
215 /* We must print an error message. Be clever about what it says. */
217 switch (TREE_CODE (type))
219 case RECORD_TYPE:
220 type_code_string = "struct";
221 break;
223 case UNION_TYPE:
224 type_code_string = "union";
225 break;
227 case ENUMERAL_TYPE:
228 type_code_string = "enum";
229 break;
231 case VOID_TYPE:
232 error ("invalid use of void expression");
233 return;
235 case ARRAY_TYPE:
236 if (TYPE_DOMAIN (type))
238 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
240 error ("invalid use of flexible array member");
241 return;
243 type = TREE_TYPE (type);
244 goto retry;
246 error ("invalid use of array with unspecified bounds");
247 return;
249 default:
250 gcc_unreachable ();
253 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
254 error ("invalid use of undefined type %<%s %E%>",
255 type_code_string, TYPE_NAME (type));
256 else
257 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
258 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
262 /* Given a type, apply default promotions wrt unnamed function
263 arguments and return the new type. */
265 tree
266 c_type_promotes_to (tree type)
268 if (TYPE_MAIN_VARIANT (type) == float_type_node)
269 return double_type_node;
271 if (c_promoting_integer_type_p (type))
273 /* Preserve unsignedness if not really getting any wider. */
274 if (TYPE_UNSIGNED (type)
275 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
276 return unsigned_type_node;
277 return integer_type_node;
280 return type;
283 /* Return true if between two named address spaces, whether there is a superset
284 named address space that encompasses both address spaces. If there is a
285 superset, return which address space is the superset. */
287 static bool
288 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
290 if (as1 == as2)
292 *common = as1;
293 return true;
295 else if (targetm.addr_space.subset_p (as1, as2))
297 *common = as2;
298 return true;
300 else if (targetm.addr_space.subset_p (as2, as1))
302 *common = as1;
303 return true;
305 else
306 return false;
309 /* Return a variant of TYPE which has all the type qualifiers of LIKE
310 as well as those of TYPE. */
312 static tree
313 qualify_type (tree type, tree like)
315 addr_space_t as_type = TYPE_ADDR_SPACE (type);
316 addr_space_t as_like = TYPE_ADDR_SPACE (like);
317 addr_space_t as_common;
319 /* If the two named address spaces are different, determine the common
320 superset address space. If there isn't one, raise an error. */
321 if (!addr_space_superset (as_type, as_like, &as_common))
323 as_common = as_type;
324 error ("%qT and %qT are in disjoint named address spaces",
325 type, like);
328 return c_build_qualified_type (type,
329 TYPE_QUALS_NO_ADDR_SPACE (type)
330 | TYPE_QUALS_NO_ADDR_SPACE (like)
331 | ENCODE_QUAL_ADDR_SPACE (as_common));
334 /* Return true iff the given tree T is a variable length array. */
336 bool
337 c_vla_type_p (const_tree t)
339 if (TREE_CODE (t) == ARRAY_TYPE
340 && C_TYPE_VARIABLE_SIZE (t))
341 return true;
342 return false;
345 /* Return the composite type of two compatible types.
347 We assume that comptypes has already been done and returned
348 nonzero; if that isn't so, this may crash. In particular, we
349 assume that qualifiers match. */
351 tree
352 composite_type (tree t1, tree t2)
354 enum tree_code code1;
355 enum tree_code code2;
356 tree attributes;
358 /* Save time if the two types are the same. */
360 if (t1 == t2) return t1;
362 /* If one type is nonsense, use the other. */
363 if (t1 == error_mark_node)
364 return t2;
365 if (t2 == error_mark_node)
366 return t1;
368 code1 = TREE_CODE (t1);
369 code2 = TREE_CODE (t2);
371 /* Merge the attributes. */
372 attributes = targetm.merge_type_attributes (t1, t2);
374 /* If one is an enumerated type and the other is the compatible
375 integer type, the composite type might be either of the two
376 (DR#013 question 3). For consistency, use the enumerated type as
377 the composite type. */
379 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
380 return t1;
381 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
382 return t2;
384 gcc_assert (code1 == code2);
386 switch (code1)
388 case POINTER_TYPE:
389 /* For two pointers, do this recursively on the target type. */
391 tree pointed_to_1 = TREE_TYPE (t1);
392 tree pointed_to_2 = TREE_TYPE (t2);
393 tree target = composite_type (pointed_to_1, pointed_to_2);
394 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
395 t1 = build_type_attribute_variant (t1, attributes);
396 return qualify_type (t1, t2);
399 case ARRAY_TYPE:
401 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
402 int quals;
403 tree unqual_elt;
404 tree d1 = TYPE_DOMAIN (t1);
405 tree d2 = TYPE_DOMAIN (t2);
406 bool d1_variable, d2_variable;
407 bool d1_zero, d2_zero;
408 bool t1_complete, t2_complete;
410 /* We should not have any type quals on arrays at all. */
411 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
412 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
414 t1_complete = COMPLETE_TYPE_P (t1);
415 t2_complete = COMPLETE_TYPE_P (t2);
417 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
418 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
420 d1_variable = (!d1_zero
421 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
422 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
423 d2_variable = (!d2_zero
424 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
425 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
426 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
427 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
429 /* Save space: see if the result is identical to one of the args. */
430 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
431 && (d2_variable || d2_zero || !d1_variable))
432 return build_type_attribute_variant (t1, attributes);
433 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
434 && (d1_variable || d1_zero || !d2_variable))
435 return build_type_attribute_variant (t2, attributes);
437 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
438 return build_type_attribute_variant (t1, attributes);
439 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
440 return build_type_attribute_variant (t2, attributes);
442 /* Merge the element types, and have a size if either arg has
443 one. We may have qualifiers on the element types. To set
444 up TYPE_MAIN_VARIANT correctly, we need to form the
445 composite of the unqualified types and add the qualifiers
446 back at the end. */
447 quals = TYPE_QUALS (strip_array_types (elt));
448 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
449 t1 = build_array_type (unqual_elt,
450 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
451 && (d2_variable
452 || d2_zero
453 || !d1_variable))
454 ? t1
455 : t2));
456 /* Ensure a composite type involving a zero-length array type
457 is a zero-length type not an incomplete type. */
458 if (d1_zero && d2_zero
459 && (t1_complete || t2_complete)
460 && !COMPLETE_TYPE_P (t1))
462 TYPE_SIZE (t1) = bitsize_zero_node;
463 TYPE_SIZE_UNIT (t1) = size_zero_node;
465 t1 = c_build_qualified_type (t1, quals);
466 return build_type_attribute_variant (t1, attributes);
469 case ENUMERAL_TYPE:
470 case RECORD_TYPE:
471 case UNION_TYPE:
472 if (attributes != NULL)
474 /* Try harder not to create a new aggregate type. */
475 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
476 return t1;
477 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
478 return t2;
480 return build_type_attribute_variant (t1, attributes);
482 case FUNCTION_TYPE:
483 /* Function types: prefer the one that specified arg types.
484 If both do, merge the arg types. Also merge the return types. */
486 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
487 tree p1 = TYPE_ARG_TYPES (t1);
488 tree p2 = TYPE_ARG_TYPES (t2);
489 int len;
490 tree newargs, n;
491 int i;
493 /* Save space: see if the result is identical to one of the args. */
494 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
495 return build_type_attribute_variant (t1, attributes);
496 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
497 return build_type_attribute_variant (t2, attributes);
499 /* Simple way if one arg fails to specify argument types. */
500 if (TYPE_ARG_TYPES (t1) == 0)
502 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
503 t1 = build_type_attribute_variant (t1, attributes);
504 return qualify_type (t1, t2);
506 if (TYPE_ARG_TYPES (t2) == 0)
508 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
509 t1 = build_type_attribute_variant (t1, attributes);
510 return qualify_type (t1, t2);
513 /* If both args specify argument types, we must merge the two
514 lists, argument by argument. */
516 len = list_length (p1);
517 newargs = 0;
519 for (i = 0; i < len; i++)
520 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
522 n = newargs;
524 for (; p1;
525 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
527 /* A null type means arg type is not specified.
528 Take whatever the other function type has. */
529 if (TREE_VALUE (p1) == 0)
531 TREE_VALUE (n) = TREE_VALUE (p2);
532 goto parm_done;
534 if (TREE_VALUE (p2) == 0)
536 TREE_VALUE (n) = TREE_VALUE (p1);
537 goto parm_done;
540 /* Given wait (union {union wait *u; int *i} *)
541 and wait (union wait *),
542 prefer union wait * as type of parm. */
543 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
544 && TREE_VALUE (p1) != TREE_VALUE (p2))
546 tree memb;
547 tree mv2 = TREE_VALUE (p2);
548 if (mv2 && mv2 != error_mark_node
549 && TREE_CODE (mv2) != ARRAY_TYPE)
550 mv2 = TYPE_MAIN_VARIANT (mv2);
551 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
552 memb; memb = DECL_CHAIN (memb))
554 tree mv3 = TREE_TYPE (memb);
555 if (mv3 && mv3 != error_mark_node
556 && TREE_CODE (mv3) != ARRAY_TYPE)
557 mv3 = TYPE_MAIN_VARIANT (mv3);
558 if (comptypes (mv3, mv2))
560 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
561 TREE_VALUE (p2));
562 pedwarn (input_location, OPT_Wpedantic,
563 "function types not truly compatible in ISO C");
564 goto parm_done;
568 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
569 && TREE_VALUE (p2) != TREE_VALUE (p1))
571 tree memb;
572 tree mv1 = TREE_VALUE (p1);
573 if (mv1 && mv1 != error_mark_node
574 && TREE_CODE (mv1) != ARRAY_TYPE)
575 mv1 = TYPE_MAIN_VARIANT (mv1);
576 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
577 memb; memb = DECL_CHAIN (memb))
579 tree mv3 = TREE_TYPE (memb);
580 if (mv3 && mv3 != error_mark_node
581 && TREE_CODE (mv3) != ARRAY_TYPE)
582 mv3 = TYPE_MAIN_VARIANT (mv3);
583 if (comptypes (mv3, mv1))
585 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
586 TREE_VALUE (p1));
587 pedwarn (input_location, OPT_Wpedantic,
588 "function types not truly compatible in ISO C");
589 goto parm_done;
593 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
594 parm_done: ;
597 t1 = build_function_type (valtype, newargs);
598 t1 = qualify_type (t1, t2);
599 /* ... falls through ... */
602 default:
603 return build_type_attribute_variant (t1, attributes);
608 /* Return the type of a conditional expression between pointers to
609 possibly differently qualified versions of compatible types.
611 We assume that comp_target_types has already been done and returned
612 nonzero; if that isn't so, this may crash. */
614 static tree
615 common_pointer_type (tree t1, tree t2)
617 tree attributes;
618 tree pointed_to_1, mv1;
619 tree pointed_to_2, mv2;
620 tree target;
621 unsigned target_quals;
622 addr_space_t as1, as2, as_common;
623 int quals1, quals2;
625 /* Save time if the two types are the same. */
627 if (t1 == t2) return t1;
629 /* If one type is nonsense, use the other. */
630 if (t1 == error_mark_node)
631 return t2;
632 if (t2 == error_mark_node)
633 return t1;
635 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
636 && TREE_CODE (t2) == POINTER_TYPE);
638 /* Merge the attributes. */
639 attributes = targetm.merge_type_attributes (t1, t2);
641 /* Find the composite type of the target types, and combine the
642 qualifiers of the two types' targets. Do not lose qualifiers on
643 array element types by taking the TYPE_MAIN_VARIANT. */
644 mv1 = pointed_to_1 = TREE_TYPE (t1);
645 mv2 = pointed_to_2 = TREE_TYPE (t2);
646 if (TREE_CODE (mv1) != ARRAY_TYPE)
647 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
648 if (TREE_CODE (mv2) != ARRAY_TYPE)
649 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
650 target = composite_type (mv1, mv2);
652 /* For function types do not merge const qualifiers, but drop them
653 if used inconsistently. The middle-end uses these to mark const
654 and noreturn functions. */
655 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
656 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
658 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
659 target_quals = (quals1 & quals2);
660 else
661 target_quals = (quals1 | quals2);
663 /* If the two named address spaces are different, determine the common
664 superset address space. This is guaranteed to exist due to the
665 assumption that comp_target_type returned non-zero. */
666 as1 = TYPE_ADDR_SPACE (pointed_to_1);
667 as2 = TYPE_ADDR_SPACE (pointed_to_2);
668 if (!addr_space_superset (as1, as2, &as_common))
669 gcc_unreachable ();
671 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
673 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
674 return build_type_attribute_variant (t1, attributes);
677 /* Return the common type for two arithmetic types under the usual
678 arithmetic conversions. The default conversions have already been
679 applied, and enumerated types converted to their compatible integer
680 types. The resulting type is unqualified and has no attributes.
682 This is the type for the result of most arithmetic operations
683 if the operands have the given two types. */
685 static tree
686 c_common_type (tree t1, tree t2)
688 enum tree_code code1;
689 enum tree_code code2;
691 /* If one type is nonsense, use the other. */
692 if (t1 == error_mark_node)
693 return t2;
694 if (t2 == error_mark_node)
695 return t1;
697 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
698 t1 = TYPE_MAIN_VARIANT (t1);
700 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
701 t2 = TYPE_MAIN_VARIANT (t2);
703 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
704 t1 = build_type_attribute_variant (t1, NULL_TREE);
706 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
707 t2 = build_type_attribute_variant (t2, NULL_TREE);
709 /* Save time if the two types are the same. */
711 if (t1 == t2) return t1;
713 code1 = TREE_CODE (t1);
714 code2 = TREE_CODE (t2);
716 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
717 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
718 || code1 == INTEGER_TYPE);
719 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
720 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
721 || code2 == INTEGER_TYPE);
723 /* When one operand is a decimal float type, the other operand cannot be
724 a generic float type or a complex type. We also disallow vector types
725 here. */
726 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
727 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
729 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
731 error ("can%'t mix operands of decimal float and vector types");
732 return error_mark_node;
734 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
736 error ("can%'t mix operands of decimal float and complex types");
737 return error_mark_node;
739 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
741 error ("can%'t mix operands of decimal float and other float types");
742 return error_mark_node;
746 /* If one type is a vector type, return that type. (How the usual
747 arithmetic conversions apply to the vector types extension is not
748 precisely specified.) */
749 if (code1 == VECTOR_TYPE)
750 return t1;
752 if (code2 == VECTOR_TYPE)
753 return t2;
755 /* If one type is complex, form the common type of the non-complex
756 components, then make that complex. Use T1 or T2 if it is the
757 required type. */
758 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
760 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
761 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
762 tree subtype = c_common_type (subtype1, subtype2);
764 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
765 return t1;
766 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
767 return t2;
768 else
769 return build_complex_type (subtype);
772 /* If only one is real, use it as the result. */
774 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
775 return t1;
777 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
778 return t2;
780 /* If both are real and either are decimal floating point types, use
781 the decimal floating point type with the greater precision. */
783 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
785 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
786 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
787 return dfloat128_type_node;
788 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
789 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
790 return dfloat64_type_node;
791 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
792 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
793 return dfloat32_type_node;
796 /* Deal with fixed-point types. */
797 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
799 unsigned int unsignedp = 0, satp = 0;
800 enum machine_mode m1, m2;
801 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
803 m1 = TYPE_MODE (t1);
804 m2 = TYPE_MODE (t2);
806 /* If one input type is saturating, the result type is saturating. */
807 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
808 satp = 1;
810 /* If both fixed-point types are unsigned, the result type is unsigned.
811 When mixing fixed-point and integer types, follow the sign of the
812 fixed-point type.
813 Otherwise, the result type is signed. */
814 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
815 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
816 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
817 && TYPE_UNSIGNED (t1))
818 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
819 && TYPE_UNSIGNED (t2)))
820 unsignedp = 1;
822 /* The result type is signed. */
823 if (unsignedp == 0)
825 /* If the input type is unsigned, we need to convert to the
826 signed type. */
827 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
829 enum mode_class mclass = (enum mode_class) 0;
830 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
831 mclass = MODE_FRACT;
832 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
833 mclass = MODE_ACCUM;
834 else
835 gcc_unreachable ();
836 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
838 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
840 enum mode_class mclass = (enum mode_class) 0;
841 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
842 mclass = MODE_FRACT;
843 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
844 mclass = MODE_ACCUM;
845 else
846 gcc_unreachable ();
847 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
851 if (code1 == FIXED_POINT_TYPE)
853 fbit1 = GET_MODE_FBIT (m1);
854 ibit1 = GET_MODE_IBIT (m1);
856 else
858 fbit1 = 0;
859 /* Signed integers need to subtract one sign bit. */
860 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
863 if (code2 == FIXED_POINT_TYPE)
865 fbit2 = GET_MODE_FBIT (m2);
866 ibit2 = GET_MODE_IBIT (m2);
868 else
870 fbit2 = 0;
871 /* Signed integers need to subtract one sign bit. */
872 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
875 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
876 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
877 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
878 satp);
881 /* Both real or both integers; use the one with greater precision. */
883 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
884 return t1;
885 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
886 return t2;
888 /* Same precision. Prefer long longs to longs to ints when the
889 same precision, following the C99 rules on integer type rank
890 (which are equivalent to the C90 rules for C90 types). */
892 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
893 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
894 return long_long_unsigned_type_node;
896 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
897 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
899 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
900 return long_long_unsigned_type_node;
901 else
902 return long_long_integer_type_node;
905 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
906 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
907 return long_unsigned_type_node;
909 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
912 /* But preserve unsignedness from the other type,
913 since long cannot hold all the values of an unsigned int. */
914 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
915 return long_unsigned_type_node;
916 else
917 return long_integer_type_node;
920 /* Likewise, prefer long double to double even if same size. */
921 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
923 return long_double_type_node;
925 /* Likewise, prefer double to float even if same size.
926 We got a couple of embedded targets with 32 bit doubles, and the
927 pdp11 might have 64 bit floats. */
928 if (TYPE_MAIN_VARIANT (t1) == double_type_node
929 || TYPE_MAIN_VARIANT (t2) == double_type_node)
930 return double_type_node;
932 /* Otherwise prefer the unsigned one. */
934 if (TYPE_UNSIGNED (t1))
935 return t1;
936 else
937 return t2;
940 /* Wrapper around c_common_type that is used by c-common.c and other
941 front end optimizations that remove promotions. ENUMERAL_TYPEs
942 are allowed here and are converted to their compatible integer types.
943 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
944 preferably a non-Boolean type as the common type. */
945 tree
946 common_type (tree t1, tree t2)
948 if (TREE_CODE (t1) == ENUMERAL_TYPE)
949 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
950 if (TREE_CODE (t2) == ENUMERAL_TYPE)
951 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
953 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
954 if (TREE_CODE (t1) == BOOLEAN_TYPE
955 && TREE_CODE (t2) == BOOLEAN_TYPE)
956 return boolean_type_node;
958 /* If either type is BOOLEAN_TYPE, then return the other. */
959 if (TREE_CODE (t1) == BOOLEAN_TYPE)
960 return t2;
961 if (TREE_CODE (t2) == BOOLEAN_TYPE)
962 return t1;
964 return c_common_type (t1, t2);
967 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
968 or various other operations. Return 2 if they are compatible
969 but a warning may be needed if you use them together. */
972 comptypes (tree type1, tree type2)
974 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
975 int val;
977 val = comptypes_internal (type1, type2, NULL, NULL);
978 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
980 return val;
983 /* Like comptypes, but if it returns non-zero because enum and int are
984 compatible, it sets *ENUM_AND_INT_P to true. */
986 static int
987 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
989 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
990 int val;
992 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
993 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
995 return val;
998 /* Like comptypes, but if it returns nonzero for different types, it
999 sets *DIFFERENT_TYPES_P to true. */
1002 comptypes_check_different_types (tree type1, tree type2,
1003 bool *different_types_p)
1005 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1006 int val;
1008 val = comptypes_internal (type1, type2, NULL, different_types_p);
1009 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1011 return val;
1014 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1015 or various other operations. Return 2 if they are compatible
1016 but a warning may be needed if you use them together. If
1017 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1018 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1019 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1020 NULL, and the types are compatible but different enough not to be
1021 permitted in C11 typedef redeclarations, then this sets
1022 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1023 false, but may or may not be set if the types are incompatible.
1024 This differs from comptypes, in that we don't free the seen
1025 types. */
1027 static int
1028 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1029 bool *different_types_p)
1031 const_tree t1 = type1;
1032 const_tree t2 = type2;
1033 int attrval, val;
1035 /* Suppress errors caused by previously reported errors. */
1037 if (t1 == t2 || !t1 || !t2
1038 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1039 return 1;
1041 /* Enumerated types are compatible with integer types, but this is
1042 not transitive: two enumerated types in the same translation unit
1043 are compatible with each other only if they are the same type. */
1045 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1047 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1048 if (TREE_CODE (t2) != VOID_TYPE)
1050 if (enum_and_int_p != NULL)
1051 *enum_and_int_p = true;
1052 if (different_types_p != NULL)
1053 *different_types_p = true;
1056 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1058 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1059 if (TREE_CODE (t1) != VOID_TYPE)
1061 if (enum_and_int_p != NULL)
1062 *enum_and_int_p = true;
1063 if (different_types_p != NULL)
1064 *different_types_p = true;
1068 if (t1 == t2)
1069 return 1;
1071 /* Different classes of types can't be compatible. */
1073 if (TREE_CODE (t1) != TREE_CODE (t2))
1074 return 0;
1076 /* Qualifiers must match. C99 6.7.3p9 */
1078 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1079 return 0;
1081 /* Allow for two different type nodes which have essentially the same
1082 definition. Note that we already checked for equality of the type
1083 qualifiers (just above). */
1085 if (TREE_CODE (t1) != ARRAY_TYPE
1086 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1087 return 1;
1089 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1090 if (!(attrval = comp_type_attributes (t1, t2)))
1091 return 0;
1093 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1094 val = 0;
1096 switch (TREE_CODE (t1))
1098 case POINTER_TYPE:
1099 /* Do not remove mode or aliasing information. */
1100 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1101 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1102 break;
1103 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1104 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1105 enum_and_int_p, different_types_p));
1106 break;
1108 case FUNCTION_TYPE:
1109 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1110 different_types_p);
1111 break;
1113 case ARRAY_TYPE:
1115 tree d1 = TYPE_DOMAIN (t1);
1116 tree d2 = TYPE_DOMAIN (t2);
1117 bool d1_variable, d2_variable;
1118 bool d1_zero, d2_zero;
1119 val = 1;
1121 /* Target types must match incl. qualifiers. */
1122 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1123 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1124 enum_and_int_p,
1125 different_types_p)))
1126 return 0;
1128 if (different_types_p != NULL
1129 && (d1 == 0) != (d2 == 0))
1130 *different_types_p = true;
1131 /* Sizes must match unless one is missing or variable. */
1132 if (d1 == 0 || d2 == 0 || d1 == d2)
1133 break;
1135 d1_zero = !TYPE_MAX_VALUE (d1);
1136 d2_zero = !TYPE_MAX_VALUE (d2);
1138 d1_variable = (!d1_zero
1139 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1140 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1141 d2_variable = (!d2_zero
1142 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1143 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1144 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1145 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1147 if (different_types_p != NULL
1148 && d1_variable != d2_variable)
1149 *different_types_p = true;
1150 if (d1_variable || d2_variable)
1151 break;
1152 if (d1_zero && d2_zero)
1153 break;
1154 if (d1_zero || d2_zero
1155 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1156 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1157 val = 0;
1159 break;
1162 case ENUMERAL_TYPE:
1163 case RECORD_TYPE:
1164 case UNION_TYPE:
1165 if (val != 1 && !same_translation_unit_p (t1, t2))
1167 tree a1 = TYPE_ATTRIBUTES (t1);
1168 tree a2 = TYPE_ATTRIBUTES (t2);
1170 if (! attribute_list_contained (a1, a2)
1171 && ! attribute_list_contained (a2, a1))
1172 break;
1174 if (attrval != 2)
1175 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1176 different_types_p);
1177 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1178 different_types_p);
1180 break;
1182 case VECTOR_TYPE:
1183 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1184 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1185 enum_and_int_p, different_types_p));
1186 break;
1188 default:
1189 break;
1191 return attrval == 2 && val == 1 ? 2 : val;
1194 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1195 their qualifiers, except for named address spaces. If the pointers point to
1196 different named addresses, then we must determine if one address space is a
1197 subset of the other. */
1199 static int
1200 comp_target_types (location_t location, tree ttl, tree ttr)
1202 int val;
1203 tree mvl = TREE_TYPE (ttl);
1204 tree mvr = TREE_TYPE (ttr);
1205 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1206 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1207 addr_space_t as_common;
1208 bool enum_and_int_p;
1210 /* Fail if pointers point to incompatible address spaces. */
1211 if (!addr_space_superset (asl, asr, &as_common))
1212 return 0;
1214 /* Do not lose qualifiers on element types of array types that are
1215 pointer targets by taking their TYPE_MAIN_VARIANT. */
1216 if (TREE_CODE (mvl) != ARRAY_TYPE)
1217 mvl = TYPE_MAIN_VARIANT (mvl);
1218 if (TREE_CODE (mvr) != ARRAY_TYPE)
1219 mvr = TYPE_MAIN_VARIANT (mvr);
1220 enum_and_int_p = false;
1221 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1223 if (val == 2)
1224 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1226 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1227 warning_at (location, OPT_Wc___compat,
1228 "pointer target types incompatible in C++");
1230 return val;
1233 /* Subroutines of `comptypes'. */
1235 /* Determine whether two trees derive from the same translation unit.
1236 If the CONTEXT chain ends in a null, that tree's context is still
1237 being parsed, so if two trees have context chains ending in null,
1238 they're in the same translation unit. */
1240 same_translation_unit_p (const_tree t1, const_tree t2)
1242 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1243 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1245 case tcc_declaration:
1246 t1 = DECL_CONTEXT (t1); break;
1247 case tcc_type:
1248 t1 = TYPE_CONTEXT (t1); break;
1249 case tcc_exceptional:
1250 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1251 default: gcc_unreachable ();
1254 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1255 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1257 case tcc_declaration:
1258 t2 = DECL_CONTEXT (t2); break;
1259 case tcc_type:
1260 t2 = TYPE_CONTEXT (t2); break;
1261 case tcc_exceptional:
1262 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1263 default: gcc_unreachable ();
1266 return t1 == t2;
1269 /* Allocate the seen two types, assuming that they are compatible. */
1271 static struct tagged_tu_seen_cache *
1272 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1274 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1275 tu->next = tagged_tu_seen_base;
1276 tu->t1 = t1;
1277 tu->t2 = t2;
1279 tagged_tu_seen_base = tu;
1281 /* The C standard says that two structures in different translation
1282 units are compatible with each other only if the types of their
1283 fields are compatible (among other things). We assume that they
1284 are compatible until proven otherwise when building the cache.
1285 An example where this can occur is:
1286 struct a
1288 struct a *next;
1290 If we are comparing this against a similar struct in another TU,
1291 and did not assume they were compatible, we end up with an infinite
1292 loop. */
1293 tu->val = 1;
1294 return tu;
1297 /* Free the seen types until we get to TU_TIL. */
1299 static void
1300 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1302 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1303 while (tu != tu_til)
1305 const struct tagged_tu_seen_cache *const tu1
1306 = (const struct tagged_tu_seen_cache *) tu;
1307 tu = tu1->next;
1308 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1310 tagged_tu_seen_base = tu_til;
1313 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1314 compatible. If the two types are not the same (which has been
1315 checked earlier), this can only happen when multiple translation
1316 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1317 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1318 comptypes_internal. */
1320 static int
1321 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1322 bool *enum_and_int_p, bool *different_types_p)
1324 tree s1, s2;
1325 bool needs_warning = false;
1327 /* We have to verify that the tags of the types are the same. This
1328 is harder than it looks because this may be a typedef, so we have
1329 to go look at the original type. It may even be a typedef of a
1330 typedef...
1331 In the case of compiler-created builtin structs the TYPE_DECL
1332 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1333 while (TYPE_NAME (t1)
1334 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1335 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1336 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1338 while (TYPE_NAME (t2)
1339 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1340 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1341 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1343 /* C90 didn't have the requirement that the two tags be the same. */
1344 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1345 return 0;
1347 /* C90 didn't say what happened if one or both of the types were
1348 incomplete; we choose to follow C99 rules here, which is that they
1349 are compatible. */
1350 if (TYPE_SIZE (t1) == NULL
1351 || TYPE_SIZE (t2) == NULL)
1352 return 1;
1355 const struct tagged_tu_seen_cache * tts_i;
1356 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1357 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1358 return tts_i->val;
1361 switch (TREE_CODE (t1))
1363 case ENUMERAL_TYPE:
1365 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1366 /* Speed up the case where the type values are in the same order. */
1367 tree tv1 = TYPE_VALUES (t1);
1368 tree tv2 = TYPE_VALUES (t2);
1370 if (tv1 == tv2)
1372 return 1;
1375 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1377 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1378 break;
1379 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1381 tu->val = 0;
1382 return 0;
1386 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1388 return 1;
1390 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1392 tu->val = 0;
1393 return 0;
1396 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1398 tu->val = 0;
1399 return 0;
1402 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1404 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1405 if (s2 == NULL
1406 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1408 tu->val = 0;
1409 return 0;
1412 return 1;
1415 case UNION_TYPE:
1417 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1418 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1420 tu->val = 0;
1421 return 0;
1424 /* Speed up the common case where the fields are in the same order. */
1425 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1426 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1428 int result;
1430 if (DECL_NAME (s1) != DECL_NAME (s2))
1431 break;
1432 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1433 enum_and_int_p, different_types_p);
1435 if (result != 1 && !DECL_NAME (s1))
1436 break;
1437 if (result == 0)
1439 tu->val = 0;
1440 return 0;
1442 if (result == 2)
1443 needs_warning = true;
1445 if (TREE_CODE (s1) == FIELD_DECL
1446 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1447 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1449 tu->val = 0;
1450 return 0;
1453 if (!s1 && !s2)
1455 tu->val = needs_warning ? 2 : 1;
1456 return tu->val;
1459 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1461 bool ok = false;
1463 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1464 if (DECL_NAME (s1) == DECL_NAME (s2))
1466 int result;
1468 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1469 enum_and_int_p,
1470 different_types_p);
1472 if (result != 1 && !DECL_NAME (s1))
1473 continue;
1474 if (result == 0)
1476 tu->val = 0;
1477 return 0;
1479 if (result == 2)
1480 needs_warning = true;
1482 if (TREE_CODE (s1) == FIELD_DECL
1483 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1484 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1485 break;
1487 ok = true;
1488 break;
1490 if (!ok)
1492 tu->val = 0;
1493 return 0;
1496 tu->val = needs_warning ? 2 : 10;
1497 return tu->val;
1500 case RECORD_TYPE:
1502 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1504 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1505 s1 && s2;
1506 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1508 int result;
1509 if (TREE_CODE (s1) != TREE_CODE (s2)
1510 || DECL_NAME (s1) != DECL_NAME (s2))
1511 break;
1512 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1513 enum_and_int_p, different_types_p);
1514 if (result == 0)
1515 break;
1516 if (result == 2)
1517 needs_warning = true;
1519 if (TREE_CODE (s1) == FIELD_DECL
1520 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1521 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1522 break;
1524 if (s1 && s2)
1525 tu->val = 0;
1526 else
1527 tu->val = needs_warning ? 2 : 1;
1528 return tu->val;
1531 default:
1532 gcc_unreachable ();
1536 /* Return 1 if two function types F1 and F2 are compatible.
1537 If either type specifies no argument types,
1538 the other must specify a fixed number of self-promoting arg types.
1539 Otherwise, if one type specifies only the number of arguments,
1540 the other must specify that number of self-promoting arg types.
1541 Otherwise, the argument types must match.
1542 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1544 static int
1545 function_types_compatible_p (const_tree f1, const_tree f2,
1546 bool *enum_and_int_p, bool *different_types_p)
1548 tree args1, args2;
1549 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1550 int val = 1;
1551 int val1;
1552 tree ret1, ret2;
1554 ret1 = TREE_TYPE (f1);
1555 ret2 = TREE_TYPE (f2);
1557 /* 'volatile' qualifiers on a function's return type used to mean
1558 the function is noreturn. */
1559 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1560 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1561 if (TYPE_VOLATILE (ret1))
1562 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1563 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1564 if (TYPE_VOLATILE (ret2))
1565 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1566 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1567 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1568 if (val == 0)
1569 return 0;
1571 args1 = TYPE_ARG_TYPES (f1);
1572 args2 = TYPE_ARG_TYPES (f2);
1574 if (different_types_p != NULL
1575 && (args1 == 0) != (args2 == 0))
1576 *different_types_p = true;
1578 /* An unspecified parmlist matches any specified parmlist
1579 whose argument types don't need default promotions. */
1581 if (args1 == 0)
1583 if (!self_promoting_args_p (args2))
1584 return 0;
1585 /* If one of these types comes from a non-prototype fn definition,
1586 compare that with the other type's arglist.
1587 If they don't match, ask for a warning (but no error). */
1588 if (TYPE_ACTUAL_ARG_TYPES (f1)
1589 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1590 enum_and_int_p, different_types_p))
1591 val = 2;
1592 return val;
1594 if (args2 == 0)
1596 if (!self_promoting_args_p (args1))
1597 return 0;
1598 if (TYPE_ACTUAL_ARG_TYPES (f2)
1599 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1600 enum_and_int_p, different_types_p))
1601 val = 2;
1602 return val;
1605 /* Both types have argument lists: compare them and propagate results. */
1606 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1607 different_types_p);
1608 return val1 != 1 ? val1 : val;
1611 /* Check two lists of types for compatibility, returning 0 for
1612 incompatible, 1 for compatible, or 2 for compatible with
1613 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1614 comptypes_internal. */
1616 static int
1617 type_lists_compatible_p (const_tree args1, const_tree args2,
1618 bool *enum_and_int_p, bool *different_types_p)
1620 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1621 int val = 1;
1622 int newval = 0;
1624 while (1)
1626 tree a1, mv1, a2, mv2;
1627 if (args1 == 0 && args2 == 0)
1628 return val;
1629 /* If one list is shorter than the other,
1630 they fail to match. */
1631 if (args1 == 0 || args2 == 0)
1632 return 0;
1633 mv1 = a1 = TREE_VALUE (args1);
1634 mv2 = a2 = TREE_VALUE (args2);
1635 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1636 mv1 = TYPE_MAIN_VARIANT (mv1);
1637 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1638 mv2 = TYPE_MAIN_VARIANT (mv2);
1639 /* A null pointer instead of a type
1640 means there is supposed to be an argument
1641 but nothing is specified about what type it has.
1642 So match anything that self-promotes. */
1643 if (different_types_p != NULL
1644 && (a1 == 0) != (a2 == 0))
1645 *different_types_p = true;
1646 if (a1 == 0)
1648 if (c_type_promotes_to (a2) != a2)
1649 return 0;
1651 else if (a2 == 0)
1653 if (c_type_promotes_to (a1) != a1)
1654 return 0;
1656 /* If one of the lists has an error marker, ignore this arg. */
1657 else if (TREE_CODE (a1) == ERROR_MARK
1658 || TREE_CODE (a2) == ERROR_MARK)
1660 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1661 different_types_p)))
1663 if (different_types_p != NULL)
1664 *different_types_p = true;
1665 /* Allow wait (union {union wait *u; int *i} *)
1666 and wait (union wait *) to be compatible. */
1667 if (TREE_CODE (a1) == UNION_TYPE
1668 && (TYPE_NAME (a1) == 0
1669 || TYPE_TRANSPARENT_AGGR (a1))
1670 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1671 && tree_int_cst_equal (TYPE_SIZE (a1),
1672 TYPE_SIZE (a2)))
1674 tree memb;
1675 for (memb = TYPE_FIELDS (a1);
1676 memb; memb = DECL_CHAIN (memb))
1678 tree mv3 = TREE_TYPE (memb);
1679 if (mv3 && mv3 != error_mark_node
1680 && TREE_CODE (mv3) != ARRAY_TYPE)
1681 mv3 = TYPE_MAIN_VARIANT (mv3);
1682 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1683 different_types_p))
1684 break;
1686 if (memb == 0)
1687 return 0;
1689 else if (TREE_CODE (a2) == UNION_TYPE
1690 && (TYPE_NAME (a2) == 0
1691 || TYPE_TRANSPARENT_AGGR (a2))
1692 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1693 && tree_int_cst_equal (TYPE_SIZE (a2),
1694 TYPE_SIZE (a1)))
1696 tree memb;
1697 for (memb = TYPE_FIELDS (a2);
1698 memb; memb = DECL_CHAIN (memb))
1700 tree mv3 = TREE_TYPE (memb);
1701 if (mv3 && mv3 != error_mark_node
1702 && TREE_CODE (mv3) != ARRAY_TYPE)
1703 mv3 = TYPE_MAIN_VARIANT (mv3);
1704 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1705 different_types_p))
1706 break;
1708 if (memb == 0)
1709 return 0;
1711 else
1712 return 0;
1715 /* comptypes said ok, but record if it said to warn. */
1716 if (newval > val)
1717 val = newval;
1719 args1 = TREE_CHAIN (args1);
1720 args2 = TREE_CHAIN (args2);
1724 /* Compute the size to increment a pointer by. */
1726 static tree
1727 c_size_in_bytes (const_tree type)
1729 enum tree_code code = TREE_CODE (type);
1731 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1732 return size_one_node;
1734 if (!COMPLETE_OR_VOID_TYPE_P (type))
1736 error ("arithmetic on pointer to an incomplete type");
1737 return size_one_node;
1740 /* Convert in case a char is more than one unit. */
1741 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1742 size_int (TYPE_PRECISION (char_type_node)
1743 / BITS_PER_UNIT));
1746 /* Return either DECL or its known constant value (if it has one). */
1748 tree
1749 decl_constant_value (tree decl)
1751 if (/* Don't change a variable array bound or initial value to a constant
1752 in a place where a variable is invalid. Note that DECL_INITIAL
1753 isn't valid for a PARM_DECL. */
1754 current_function_decl != 0
1755 && TREE_CODE (decl) != PARM_DECL
1756 && !TREE_THIS_VOLATILE (decl)
1757 && TREE_READONLY (decl)
1758 && DECL_INITIAL (decl) != 0
1759 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1760 /* This is invalid if initial value is not constant.
1761 If it has either a function call, a memory reference,
1762 or a variable, then re-evaluating it could give different results. */
1763 && TREE_CONSTANT (DECL_INITIAL (decl))
1764 /* Check for cases where this is sub-optimal, even though valid. */
1765 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1766 return DECL_INITIAL (decl);
1767 return decl;
1770 /* Convert the array expression EXP to a pointer. */
1771 static tree
1772 array_to_pointer_conversion (location_t loc, tree exp)
1774 tree orig_exp = exp;
1775 tree type = TREE_TYPE (exp);
1776 tree adr;
1777 tree restype = TREE_TYPE (type);
1778 tree ptrtype;
1780 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1782 STRIP_TYPE_NOPS (exp);
1784 if (TREE_NO_WARNING (orig_exp))
1785 TREE_NO_WARNING (exp) = 1;
1787 ptrtype = build_pointer_type (restype);
1789 if (TREE_CODE (exp) == INDIRECT_REF)
1790 return convert (ptrtype, TREE_OPERAND (exp, 0));
1792 /* In C++ array compound literals are temporary objects unless they are
1793 const or appear in namespace scope, so they are destroyed too soon
1794 to use them for much of anything (c++/53220). */
1795 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1797 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1798 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1799 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1800 "converting an array compound literal to a pointer "
1801 "is ill-formed in C++");
1804 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1805 return convert (ptrtype, adr);
1808 /* Convert the function expression EXP to a pointer. */
1809 static tree
1810 function_to_pointer_conversion (location_t loc, tree exp)
1812 tree orig_exp = exp;
1814 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1816 STRIP_TYPE_NOPS (exp);
1818 if (TREE_NO_WARNING (orig_exp))
1819 TREE_NO_WARNING (exp) = 1;
1821 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1824 /* Mark EXP as read, not just set, for set but not used -Wunused
1825 warning purposes. */
1827 void
1828 mark_exp_read (tree exp)
1830 switch (TREE_CODE (exp))
1832 case VAR_DECL:
1833 case PARM_DECL:
1834 DECL_READ_P (exp) = 1;
1835 break;
1836 case ARRAY_REF:
1837 case COMPONENT_REF:
1838 case MODIFY_EXPR:
1839 case REALPART_EXPR:
1840 case IMAGPART_EXPR:
1841 CASE_CONVERT:
1842 case ADDR_EXPR:
1843 mark_exp_read (TREE_OPERAND (exp, 0));
1844 break;
1845 case COMPOUND_EXPR:
1846 case C_MAYBE_CONST_EXPR:
1847 mark_exp_read (TREE_OPERAND (exp, 1));
1848 break;
1849 default:
1850 break;
1854 /* Perform the default conversion of arrays and functions to pointers.
1855 Return the result of converting EXP. For any other expression, just
1856 return EXP.
1858 LOC is the location of the expression. */
1860 struct c_expr
1861 default_function_array_conversion (location_t loc, struct c_expr exp)
1863 tree orig_exp = exp.value;
1864 tree type = TREE_TYPE (exp.value);
1865 enum tree_code code = TREE_CODE (type);
1867 switch (code)
1869 case ARRAY_TYPE:
1871 bool not_lvalue = false;
1872 bool lvalue_array_p;
1874 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1875 || CONVERT_EXPR_P (exp.value))
1876 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1878 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1879 not_lvalue = true;
1880 exp.value = TREE_OPERAND (exp.value, 0);
1883 if (TREE_NO_WARNING (orig_exp))
1884 TREE_NO_WARNING (exp.value) = 1;
1886 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1887 if (!flag_isoc99 && !lvalue_array_p)
1889 /* Before C99, non-lvalue arrays do not decay to pointers.
1890 Normally, using such an array would be invalid; but it can
1891 be used correctly inside sizeof or as a statement expression.
1892 Thus, do not give an error here; an error will result later. */
1893 return exp;
1896 exp.value = array_to_pointer_conversion (loc, exp.value);
1898 break;
1899 case FUNCTION_TYPE:
1900 exp.value = function_to_pointer_conversion (loc, exp.value);
1901 break;
1902 default:
1903 break;
1906 return exp;
1909 struct c_expr
1910 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1912 mark_exp_read (exp.value);
1913 return default_function_array_conversion (loc, exp);
1916 /* EXP is an expression of integer type. Apply the integer promotions
1917 to it and return the promoted value. */
1919 tree
1920 perform_integral_promotions (tree exp)
1922 tree type = TREE_TYPE (exp);
1923 enum tree_code code = TREE_CODE (type);
1925 gcc_assert (INTEGRAL_TYPE_P (type));
1927 /* Normally convert enums to int,
1928 but convert wide enums to something wider. */
1929 if (code == ENUMERAL_TYPE)
1931 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1932 TYPE_PRECISION (integer_type_node)),
1933 ((TYPE_PRECISION (type)
1934 >= TYPE_PRECISION (integer_type_node))
1935 && TYPE_UNSIGNED (type)));
1937 return convert (type, exp);
1940 /* ??? This should no longer be needed now bit-fields have their
1941 proper types. */
1942 if (TREE_CODE (exp) == COMPONENT_REF
1943 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1944 /* If it's thinner than an int, promote it like a
1945 c_promoting_integer_type_p, otherwise leave it alone. */
1946 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1947 TYPE_PRECISION (integer_type_node)))
1948 return convert (integer_type_node, exp);
1950 if (c_promoting_integer_type_p (type))
1952 /* Preserve unsignedness if not really getting any wider. */
1953 if (TYPE_UNSIGNED (type)
1954 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1955 return convert (unsigned_type_node, exp);
1957 return convert (integer_type_node, exp);
1960 return exp;
1964 /* Perform default promotions for C data used in expressions.
1965 Enumeral types or short or char are converted to int.
1966 In addition, manifest constants symbols are replaced by their values. */
1968 tree
1969 default_conversion (tree exp)
1971 tree orig_exp;
1972 tree type = TREE_TYPE (exp);
1973 enum tree_code code = TREE_CODE (type);
1974 tree promoted_type;
1976 mark_exp_read (exp);
1978 /* Functions and arrays have been converted during parsing. */
1979 gcc_assert (code != FUNCTION_TYPE);
1980 if (code == ARRAY_TYPE)
1981 return exp;
1983 /* Constants can be used directly unless they're not loadable. */
1984 if (TREE_CODE (exp) == CONST_DECL)
1985 exp = DECL_INITIAL (exp);
1987 /* Strip no-op conversions. */
1988 orig_exp = exp;
1989 STRIP_TYPE_NOPS (exp);
1991 if (TREE_NO_WARNING (orig_exp))
1992 TREE_NO_WARNING (exp) = 1;
1994 if (code == VOID_TYPE)
1996 error ("void value not ignored as it ought to be");
1997 return error_mark_node;
2000 exp = require_complete_type (exp);
2001 if (exp == error_mark_node)
2002 return error_mark_node;
2004 promoted_type = targetm.promoted_type (type);
2005 if (promoted_type)
2006 return convert (promoted_type, exp);
2008 if (INTEGRAL_TYPE_P (type))
2009 return perform_integral_promotions (exp);
2011 return exp;
2014 /* Look up COMPONENT in a structure or union TYPE.
2016 If the component name is not found, returns NULL_TREE. Otherwise,
2017 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2018 stepping down the chain to the component, which is in the last
2019 TREE_VALUE of the list. Normally the list is of length one, but if
2020 the component is embedded within (nested) anonymous structures or
2021 unions, the list steps down the chain to the component. */
2023 static tree
2024 lookup_field (tree type, tree component)
2026 tree field;
2028 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2029 to the field elements. Use a binary search on this array to quickly
2030 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2031 will always be set for structures which have many elements. */
2033 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2035 int bot, top, half;
2036 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2038 field = TYPE_FIELDS (type);
2039 bot = 0;
2040 top = TYPE_LANG_SPECIFIC (type)->s->len;
2041 while (top - bot > 1)
2043 half = (top - bot + 1) >> 1;
2044 field = field_array[bot+half];
2046 if (DECL_NAME (field) == NULL_TREE)
2048 /* Step through all anon unions in linear fashion. */
2049 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2051 field = field_array[bot++];
2052 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2053 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2055 tree anon = lookup_field (TREE_TYPE (field), component);
2057 if (anon)
2058 return tree_cons (NULL_TREE, field, anon);
2060 /* The Plan 9 compiler permits referring
2061 directly to an anonymous struct/union field
2062 using a typedef name. */
2063 if (flag_plan9_extensions
2064 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2065 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2066 == TYPE_DECL)
2067 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2068 == component))
2069 break;
2073 /* Entire record is only anon unions. */
2074 if (bot > top)
2075 return NULL_TREE;
2077 /* Restart the binary search, with new lower bound. */
2078 continue;
2081 if (DECL_NAME (field) == component)
2082 break;
2083 if (DECL_NAME (field) < component)
2084 bot += half;
2085 else
2086 top = bot + half;
2089 if (DECL_NAME (field_array[bot]) == component)
2090 field = field_array[bot];
2091 else if (DECL_NAME (field) != component)
2092 return NULL_TREE;
2094 else
2096 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2098 if (DECL_NAME (field) == NULL_TREE
2099 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2100 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2102 tree anon = lookup_field (TREE_TYPE (field), component);
2104 if (anon)
2105 return tree_cons (NULL_TREE, field, anon);
2107 /* The Plan 9 compiler permits referring directly to an
2108 anonymous struct/union field using a typedef
2109 name. */
2110 if (flag_plan9_extensions
2111 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2112 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2113 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2114 == component))
2115 break;
2118 if (DECL_NAME (field) == component)
2119 break;
2122 if (field == NULL_TREE)
2123 return NULL_TREE;
2126 return tree_cons (NULL_TREE, field, NULL_TREE);
2129 /* Make an expression to refer to the COMPONENT field of structure or
2130 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2131 location of the COMPONENT_REF. */
2133 tree
2134 build_component_ref (location_t loc, tree datum, tree component)
2136 tree type = TREE_TYPE (datum);
2137 enum tree_code code = TREE_CODE (type);
2138 tree field = NULL;
2139 tree ref;
2140 bool datum_lvalue = lvalue_p (datum);
2142 if (!objc_is_public (datum, component))
2143 return error_mark_node;
2145 /* Detect Objective-C property syntax object.property. */
2146 if (c_dialect_objc ()
2147 && (ref = objc_maybe_build_component_ref (datum, component)))
2148 return ref;
2150 /* See if there is a field or component with name COMPONENT. */
2152 if (code == RECORD_TYPE || code == UNION_TYPE)
2154 if (!COMPLETE_TYPE_P (type))
2156 c_incomplete_type_error (NULL_TREE, type);
2157 return error_mark_node;
2160 field = lookup_field (type, component);
2162 if (!field)
2164 error_at (loc, "%qT has no member named %qE", type, component);
2165 return error_mark_node;
2168 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2169 This might be better solved in future the way the C++ front
2170 end does it - by giving the anonymous entities each a
2171 separate name and type, and then have build_component_ref
2172 recursively call itself. We can't do that here. */
2175 tree subdatum = TREE_VALUE (field);
2176 int quals;
2177 tree subtype;
2178 bool use_datum_quals;
2180 if (TREE_TYPE (subdatum) == error_mark_node)
2181 return error_mark_node;
2183 /* If this is an rvalue, it does not have qualifiers in C
2184 standard terms and we must avoid propagating such
2185 qualifiers down to a non-lvalue array that is then
2186 converted to a pointer. */
2187 use_datum_quals = (datum_lvalue
2188 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2190 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2191 if (use_datum_quals)
2192 quals |= TYPE_QUALS (TREE_TYPE (datum));
2193 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2195 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2196 NULL_TREE);
2197 SET_EXPR_LOCATION (ref, loc);
2198 if (TREE_READONLY (subdatum)
2199 || (use_datum_quals && TREE_READONLY (datum)))
2200 TREE_READONLY (ref) = 1;
2201 if (TREE_THIS_VOLATILE (subdatum)
2202 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2203 TREE_THIS_VOLATILE (ref) = 1;
2205 if (TREE_DEPRECATED (subdatum))
2206 warn_deprecated_use (subdatum, NULL_TREE);
2208 datum = ref;
2210 field = TREE_CHAIN (field);
2212 while (field);
2214 return ref;
2216 else if (code != ERROR_MARK)
2217 error_at (loc,
2218 "request for member %qE in something not a structure or union",
2219 component);
2221 return error_mark_node;
2224 /* Given an expression PTR for a pointer, return an expression
2225 for the value pointed to.
2226 ERRORSTRING is the name of the operator to appear in error messages.
2228 LOC is the location to use for the generated tree. */
2230 tree
2231 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2233 tree pointer = default_conversion (ptr);
2234 tree type = TREE_TYPE (pointer);
2235 tree ref;
2237 if (TREE_CODE (type) == POINTER_TYPE)
2239 if (CONVERT_EXPR_P (pointer)
2240 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2242 /* If a warning is issued, mark it to avoid duplicates from
2243 the backend. This only needs to be done at
2244 warn_strict_aliasing > 2. */
2245 if (warn_strict_aliasing > 2)
2246 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2247 type, TREE_OPERAND (pointer, 0)))
2248 TREE_NO_WARNING (pointer) = 1;
2251 if (TREE_CODE (pointer) == ADDR_EXPR
2252 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2253 == TREE_TYPE (type)))
2255 ref = TREE_OPERAND (pointer, 0);
2256 protected_set_expr_location (ref, loc);
2257 return ref;
2259 else
2261 tree t = TREE_TYPE (type);
2263 ref = build1 (INDIRECT_REF, t, pointer);
2265 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2267 error_at (loc, "dereferencing pointer to incomplete type");
2268 return error_mark_node;
2270 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2271 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2273 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2274 so that we get the proper error message if the result is used
2275 to assign to. Also, &* is supposed to be a no-op.
2276 And ANSI C seems to specify that the type of the result
2277 should be the const type. */
2278 /* A de-reference of a pointer to const is not a const. It is valid
2279 to change it via some other pointer. */
2280 TREE_READONLY (ref) = TYPE_READONLY (t);
2281 TREE_SIDE_EFFECTS (ref)
2282 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2283 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2284 protected_set_expr_location (ref, loc);
2285 return ref;
2288 else if (TREE_CODE (pointer) != ERROR_MARK)
2289 invalid_indirection_error (loc, type, errstring);
2291 return error_mark_node;
2294 /* This handles expressions of the form "a[i]", which denotes
2295 an array reference.
2297 This is logically equivalent in C to *(a+i), but we may do it differently.
2298 If A is a variable or a member, we generate a primitive ARRAY_REF.
2299 This avoids forcing the array out of registers, and can work on
2300 arrays that are not lvalues (for example, members of structures returned
2301 by functions).
2303 For vector types, allow vector[i] but not i[vector], and create
2304 *(((type*)&vectortype) + i) for the expression.
2306 LOC is the location to use for the returned expression. */
2308 tree
2309 build_array_ref (location_t loc, tree array, tree index)
2311 tree ret;
2312 bool swapped = false;
2313 if (TREE_TYPE (array) == error_mark_node
2314 || TREE_TYPE (index) == error_mark_node)
2315 return error_mark_node;
2317 if (flag_enable_cilkplus && contains_array_notation_expr (index))
2319 size_t rank = 0;
2320 if (!find_rank (loc, index, index, true, &rank))
2321 return error_mark_node;
2322 if (rank > 1)
2324 error_at (loc, "rank of the array's index is greater than 1");
2325 return error_mark_node;
2328 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2329 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2330 /* Allow vector[index] but not index[vector]. */
2331 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2333 tree temp;
2334 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2335 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2337 error_at (loc,
2338 "subscripted value is neither array nor pointer nor vector");
2340 return error_mark_node;
2342 temp = array;
2343 array = index;
2344 index = temp;
2345 swapped = true;
2348 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2350 error_at (loc, "array subscript is not an integer");
2351 return error_mark_node;
2354 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2356 error_at (loc, "subscripted value is pointer to function");
2357 return error_mark_node;
2360 /* ??? Existing practice has been to warn only when the char
2361 index is syntactically the index, not for char[array]. */
2362 if (!swapped)
2363 warn_array_subscript_with_type_char (index);
2365 /* Apply default promotions *after* noticing character types. */
2366 index = default_conversion (index);
2368 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2370 convert_vector_to_pointer_for_subscript (loc, &array, index);
2372 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2374 tree rval, type;
2376 /* An array that is indexed by a non-constant
2377 cannot be stored in a register; we must be able to do
2378 address arithmetic on its address.
2379 Likewise an array of elements of variable size. */
2380 if (TREE_CODE (index) != INTEGER_CST
2381 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2382 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2384 if (!c_mark_addressable (array))
2385 return error_mark_node;
2387 /* An array that is indexed by a constant value which is not within
2388 the array bounds cannot be stored in a register either; because we
2389 would get a crash in store_bit_field/extract_bit_field when trying
2390 to access a non-existent part of the register. */
2391 if (TREE_CODE (index) == INTEGER_CST
2392 && TYPE_DOMAIN (TREE_TYPE (array))
2393 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2395 if (!c_mark_addressable (array))
2396 return error_mark_node;
2399 if (pedantic)
2401 tree foo = array;
2402 while (TREE_CODE (foo) == COMPONENT_REF)
2403 foo = TREE_OPERAND (foo, 0);
2404 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2405 pedwarn (loc, OPT_Wpedantic,
2406 "ISO C forbids subscripting %<register%> array");
2407 else if (!flag_isoc99 && !lvalue_p (foo))
2408 pedwarn (loc, OPT_Wpedantic,
2409 "ISO C90 forbids subscripting non-lvalue array");
2412 type = TREE_TYPE (TREE_TYPE (array));
2413 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2414 /* Array ref is const/volatile if the array elements are
2415 or if the array is. */
2416 TREE_READONLY (rval)
2417 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2418 | TREE_READONLY (array));
2419 TREE_SIDE_EFFECTS (rval)
2420 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2421 | TREE_SIDE_EFFECTS (array));
2422 TREE_THIS_VOLATILE (rval)
2423 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2424 /* This was added by rms on 16 Nov 91.
2425 It fixes vol struct foo *a; a->elts[1]
2426 in an inline function.
2427 Hope it doesn't break something else. */
2428 | TREE_THIS_VOLATILE (array));
2429 ret = require_complete_type (rval);
2430 protected_set_expr_location (ret, loc);
2431 return ret;
2433 else
2435 tree ar = default_conversion (array);
2437 if (ar == error_mark_node)
2438 return ar;
2440 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2441 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2443 return build_indirect_ref
2444 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2445 RO_ARRAY_INDEXING);
2449 /* Build an external reference to identifier ID. FUN indicates
2450 whether this will be used for a function call. LOC is the source
2451 location of the identifier. This sets *TYPE to the type of the
2452 identifier, which is not the same as the type of the returned value
2453 for CONST_DECLs defined as enum constants. If the type of the
2454 identifier is not available, *TYPE is set to NULL. */
2455 tree
2456 build_external_ref (location_t loc, tree id, int fun, tree *type)
2458 tree ref;
2459 tree decl = lookup_name (id);
2461 /* In Objective-C, an instance variable (ivar) may be preferred to
2462 whatever lookup_name() found. */
2463 decl = objc_lookup_ivar (decl, id);
2465 *type = NULL;
2466 if (decl && decl != error_mark_node)
2468 ref = decl;
2469 *type = TREE_TYPE (ref);
2471 else if (fun)
2472 /* Implicit function declaration. */
2473 ref = implicitly_declare (loc, id);
2474 else if (decl == error_mark_node)
2475 /* Don't complain about something that's already been
2476 complained about. */
2477 return error_mark_node;
2478 else
2480 undeclared_variable (loc, id);
2481 return error_mark_node;
2484 if (TREE_TYPE (ref) == error_mark_node)
2485 return error_mark_node;
2487 if (TREE_DEPRECATED (ref))
2488 warn_deprecated_use (ref, NULL_TREE);
2490 /* Recursive call does not count as usage. */
2491 if (ref != current_function_decl)
2493 TREE_USED (ref) = 1;
2496 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2498 if (!in_sizeof && !in_typeof)
2499 C_DECL_USED (ref) = 1;
2500 else if (DECL_INITIAL (ref) == 0
2501 && DECL_EXTERNAL (ref)
2502 && !TREE_PUBLIC (ref))
2503 record_maybe_used_decl (ref);
2506 if (TREE_CODE (ref) == CONST_DECL)
2508 used_types_insert (TREE_TYPE (ref));
2510 if (warn_cxx_compat
2511 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2512 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2514 warning_at (loc, OPT_Wc___compat,
2515 ("enum constant defined in struct or union "
2516 "is not visible in C++"));
2517 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2520 ref = DECL_INITIAL (ref);
2521 TREE_CONSTANT (ref) = 1;
2523 else if (current_function_decl != 0
2524 && !DECL_FILE_SCOPE_P (current_function_decl)
2525 && (TREE_CODE (ref) == VAR_DECL
2526 || TREE_CODE (ref) == PARM_DECL
2527 || TREE_CODE (ref) == FUNCTION_DECL))
2529 tree context = decl_function_context (ref);
2531 if (context != 0 && context != current_function_decl)
2532 DECL_NONLOCAL (ref) = 1;
2534 /* C99 6.7.4p3: An inline definition of a function with external
2535 linkage ... shall not contain a reference to an identifier with
2536 internal linkage. */
2537 else if (current_function_decl != 0
2538 && DECL_DECLARED_INLINE_P (current_function_decl)
2539 && DECL_EXTERNAL (current_function_decl)
2540 && VAR_OR_FUNCTION_DECL_P (ref)
2541 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2542 && ! TREE_PUBLIC (ref)
2543 && DECL_CONTEXT (ref) != current_function_decl)
2544 record_inline_static (loc, current_function_decl, ref,
2545 csi_internal);
2547 return ref;
2550 /* Record details of decls possibly used inside sizeof or typeof. */
2551 struct maybe_used_decl
2553 /* The decl. */
2554 tree decl;
2555 /* The level seen at (in_sizeof + in_typeof). */
2556 int level;
2557 /* The next one at this level or above, or NULL. */
2558 struct maybe_used_decl *next;
2561 static struct maybe_used_decl *maybe_used_decls;
2563 /* Record that DECL, an undefined static function reference seen
2564 inside sizeof or typeof, might be used if the operand of sizeof is
2565 a VLA type or the operand of typeof is a variably modified
2566 type. */
2568 static void
2569 record_maybe_used_decl (tree decl)
2571 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2572 t->decl = decl;
2573 t->level = in_sizeof + in_typeof;
2574 t->next = maybe_used_decls;
2575 maybe_used_decls = t;
2578 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2579 USED is false, just discard them. If it is true, mark them used
2580 (if no longer inside sizeof or typeof) or move them to the next
2581 level up (if still inside sizeof or typeof). */
2583 void
2584 pop_maybe_used (bool used)
2586 struct maybe_used_decl *p = maybe_used_decls;
2587 int cur_level = in_sizeof + in_typeof;
2588 while (p && p->level > cur_level)
2590 if (used)
2592 if (cur_level == 0)
2593 C_DECL_USED (p->decl) = 1;
2594 else
2595 p->level = cur_level;
2597 p = p->next;
2599 if (!used || cur_level == 0)
2600 maybe_used_decls = p;
2603 /* Return the result of sizeof applied to EXPR. */
2605 struct c_expr
2606 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2608 struct c_expr ret;
2609 if (expr.value == error_mark_node)
2611 ret.value = error_mark_node;
2612 ret.original_code = ERROR_MARK;
2613 ret.original_type = NULL;
2614 pop_maybe_used (false);
2616 else
2618 bool expr_const_operands = true;
2619 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2620 &expr_const_operands);
2621 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2622 c_last_sizeof_arg = expr.value;
2623 ret.original_code = SIZEOF_EXPR;
2624 ret.original_type = NULL;
2625 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2627 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2628 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2629 folded_expr, ret.value);
2630 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2631 SET_EXPR_LOCATION (ret.value, loc);
2633 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2635 return ret;
2638 /* Return the result of sizeof applied to T, a structure for the type
2639 name passed to sizeof (rather than the type itself). LOC is the
2640 location of the original expression. */
2642 struct c_expr
2643 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2645 tree type;
2646 struct c_expr ret;
2647 tree type_expr = NULL_TREE;
2648 bool type_expr_const = true;
2649 type = groktypename (t, &type_expr, &type_expr_const);
2650 ret.value = c_sizeof (loc, type);
2651 c_last_sizeof_arg = type;
2652 ret.original_code = SIZEOF_EXPR;
2653 ret.original_type = NULL;
2654 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2655 && c_vla_type_p (type))
2657 /* If the type is a [*] array, it is a VLA but is represented as
2658 having a size of zero. In such a case we must ensure that
2659 the result of sizeof does not get folded to a constant by
2660 c_fully_fold, because if the size is evaluated the result is
2661 not constant and so constraints on zero or negative size
2662 arrays must not be applied when this sizeof call is inside
2663 another array declarator. */
2664 if (!type_expr)
2665 type_expr = integer_zero_node;
2666 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2667 type_expr, ret.value);
2668 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2670 pop_maybe_used (type != error_mark_node
2671 ? C_TYPE_VARIABLE_SIZE (type) : false);
2672 return ret;
2675 /* Build a function call to function FUNCTION with parameters PARAMS.
2676 The function call is at LOC.
2677 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2678 TREE_VALUE of each node is a parameter-expression.
2679 FUNCTION's data type may be a function type or a pointer-to-function. */
2681 tree
2682 build_function_call (location_t loc, tree function, tree params)
2684 vec<tree, va_gc> *v;
2685 tree ret;
2687 vec_alloc (v, list_length (params));
2688 for (; params; params = TREE_CHAIN (params))
2689 v->quick_push (TREE_VALUE (params));
2690 ret = build_function_call_vec (loc, function, v, NULL);
2691 vec_free (v);
2692 return ret;
2695 /* Give a note about the location of the declaration of DECL. */
2697 static void inform_declaration (tree decl)
2699 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2700 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2703 /* Build a function call to function FUNCTION with parameters PARAMS.
2704 ORIGTYPES, if not NULL, is a vector of types; each element is
2705 either NULL or the original type of the corresponding element in
2706 PARAMS. The original type may differ from TREE_TYPE of the
2707 parameter for enums. FUNCTION's data type may be a function type
2708 or pointer-to-function. This function changes the elements of
2709 PARAMS. */
2711 tree
2712 build_function_call_vec (location_t loc, tree function,
2713 vec<tree, va_gc> *params,
2714 vec<tree, va_gc> *origtypes)
2716 tree fntype, fundecl = 0;
2717 tree name = NULL_TREE, result;
2718 tree tem;
2719 int nargs;
2720 tree *argarray;
2723 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2724 STRIP_TYPE_NOPS (function);
2726 /* Convert anything with function type to a pointer-to-function. */
2727 if (TREE_CODE (function) == FUNCTION_DECL)
2729 /* Implement type-directed function overloading for builtins.
2730 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2731 handle all the type checking. The result is a complete expression
2732 that implements this function call. */
2733 tem = resolve_overloaded_builtin (loc, function, params);
2734 if (tem)
2735 return tem;
2737 name = DECL_NAME (function);
2739 if (flag_tm)
2740 tm_malloc_replacement (function);
2741 fundecl = function;
2742 /* Atomic functions have type checking/casting already done. They are
2743 often rewritten and don't match the original parameter list. */
2744 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2745 origtypes = NULL;
2747 if (flag_enable_cilkplus
2748 && is_cilkplus_reduce_builtin (function))
2749 origtypes = NULL;
2751 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2752 function = function_to_pointer_conversion (loc, function);
2754 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2755 expressions, like those used for ObjC messenger dispatches. */
2756 if (params && !params->is_empty ())
2757 function = objc_rewrite_function_call (function, (*params)[0]);
2759 function = c_fully_fold (function, false, NULL);
2761 fntype = TREE_TYPE (function);
2763 if (TREE_CODE (fntype) == ERROR_MARK)
2764 return error_mark_node;
2766 if (!(TREE_CODE (fntype) == POINTER_TYPE
2767 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2769 if (!flag_diagnostics_show_caret)
2770 error_at (loc,
2771 "called object %qE is not a function or function pointer",
2772 function);
2773 else if (DECL_P (function))
2775 error_at (loc,
2776 "called object %qD is not a function or function pointer",
2777 function);
2778 inform_declaration (function);
2780 else
2781 error_at (loc,
2782 "called object is not a function or function pointer");
2783 return error_mark_node;
2786 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2787 current_function_returns_abnormally = 1;
2789 /* fntype now gets the type of function pointed to. */
2790 fntype = TREE_TYPE (fntype);
2792 /* Convert the parameters to the types declared in the
2793 function prototype, or apply default promotions. */
2795 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2796 function, fundecl);
2797 if (nargs < 0)
2798 return error_mark_node;
2800 /* Check that the function is called through a compatible prototype.
2801 If it is not, replace the call by a trap, wrapped up in a compound
2802 expression if necessary. This has the nice side-effect to prevent
2803 the tree-inliner from generating invalid assignment trees which may
2804 blow up in the RTL expander later. */
2805 if (CONVERT_EXPR_P (function)
2806 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2807 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2808 && !comptypes (fntype, TREE_TYPE (tem)))
2810 tree return_type = TREE_TYPE (fntype);
2811 tree trap = build_function_call (loc,
2812 builtin_decl_explicit (BUILT_IN_TRAP),
2813 NULL_TREE);
2814 int i;
2816 /* This situation leads to run-time undefined behavior. We can't,
2817 therefore, simply error unless we can prove that all possible
2818 executions of the program must execute the code. */
2819 if (warning_at (loc, 0, "function called through a non-compatible type"))
2820 /* We can, however, treat "undefined" any way we please.
2821 Call abort to encourage the user to fix the program. */
2822 inform (loc, "if this code is reached, the program will abort");
2823 /* Before the abort, allow the function arguments to exit or
2824 call longjmp. */
2825 for (i = 0; i < nargs; i++)
2826 trap = build2 (COMPOUND_EXPR, void_type_node, (*params)[i], trap);
2828 if (VOID_TYPE_P (return_type))
2830 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2831 pedwarn (loc, 0,
2832 "function with qualified void return type called");
2833 return trap;
2835 else
2837 tree rhs;
2839 if (AGGREGATE_TYPE_P (return_type))
2840 rhs = build_compound_literal (loc, return_type,
2841 build_constructor (return_type,
2842 NULL),
2843 false);
2844 else
2845 rhs = build_zero_cst (return_type);
2847 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2848 trap, rhs));
2852 argarray = vec_safe_address (params);
2854 /* Check that arguments to builtin functions match the expectations. */
2855 if (fundecl
2856 && DECL_BUILT_IN (fundecl)
2857 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2858 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2859 return error_mark_node;
2861 /* Check that the arguments to the function are valid. */
2862 check_function_arguments (fntype, nargs, argarray);
2864 if (name != NULL_TREE
2865 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2867 if (require_constant_value)
2868 result =
2869 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2870 function, nargs, argarray);
2871 else
2872 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2873 function, nargs, argarray);
2874 if (TREE_CODE (result) == NOP_EXPR
2875 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2876 STRIP_TYPE_NOPS (result);
2878 else
2879 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2880 function, nargs, argarray);
2882 if (VOID_TYPE_P (TREE_TYPE (result)))
2884 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2885 pedwarn (loc, 0,
2886 "function with qualified void return type called");
2887 return result;
2889 return require_complete_type (result);
2892 /* Convert the argument expressions in the vector VALUES
2893 to the types in the list TYPELIST.
2895 If TYPELIST is exhausted, or when an element has NULL as its type,
2896 perform the default conversions.
2898 ORIGTYPES is the original types of the expressions in VALUES. This
2899 holds the type of enum values which have been converted to integral
2900 types. It may be NULL.
2902 FUNCTION is a tree for the called function. It is used only for
2903 error messages, where it is formatted with %qE.
2905 This is also where warnings about wrong number of args are generated.
2907 Returns the actual number of arguments processed (which may be less
2908 than the length of VALUES in some error situations), or -1 on
2909 failure. */
2911 static int
2912 convert_arguments (tree typelist, vec<tree, va_gc> *values,
2913 vec<tree, va_gc> *origtypes, tree function, tree fundecl)
2915 tree typetail, val;
2916 unsigned int parmnum;
2917 bool error_args = false;
2918 const bool type_generic = fundecl
2919 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2920 bool type_generic_remove_excess_precision = false;
2921 tree selector;
2923 /* Change pointer to function to the function itself for
2924 diagnostics. */
2925 if (TREE_CODE (function) == ADDR_EXPR
2926 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2927 function = TREE_OPERAND (function, 0);
2929 /* Handle an ObjC selector specially for diagnostics. */
2930 selector = objc_message_selector ();
2932 /* For type-generic built-in functions, determine whether excess
2933 precision should be removed (classification) or not
2934 (comparison). */
2935 if (type_generic
2936 && DECL_BUILT_IN (fundecl)
2937 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2939 switch (DECL_FUNCTION_CODE (fundecl))
2941 case BUILT_IN_ISFINITE:
2942 case BUILT_IN_ISINF:
2943 case BUILT_IN_ISINF_SIGN:
2944 case BUILT_IN_ISNAN:
2945 case BUILT_IN_ISNORMAL:
2946 case BUILT_IN_FPCLASSIFY:
2947 type_generic_remove_excess_precision = true;
2948 break;
2950 default:
2951 type_generic_remove_excess_precision = false;
2952 break;
2955 if (flag_enable_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
2956 return vec_safe_length (values);
2958 /* Scan the given expressions and types, producing individual
2959 converted arguments. */
2961 for (typetail = typelist, parmnum = 0;
2962 values && values->iterate (parmnum, &val);
2963 ++parmnum)
2965 tree type = typetail ? TREE_VALUE (typetail) : 0;
2966 tree valtype = TREE_TYPE (val);
2967 tree rname = function;
2968 int argnum = parmnum + 1;
2969 const char *invalid_func_diag;
2970 bool excess_precision = false;
2971 bool npc;
2972 tree parmval;
2974 if (type == void_type_node)
2976 if (selector)
2977 error_at (input_location,
2978 "too many arguments to method %qE", selector);
2979 else
2980 error_at (input_location,
2981 "too many arguments to function %qE", function);
2982 inform_declaration (fundecl);
2983 return parmnum;
2986 if (selector && argnum > 2)
2988 rname = selector;
2989 argnum -= 2;
2992 npc = null_pointer_constant_p (val);
2994 /* If there is excess precision and a prototype, convert once to
2995 the required type rather than converting via the semantic
2996 type. Likewise without a prototype a float value represented
2997 as long double should be converted once to double. But for
2998 type-generic classification functions excess precision must
2999 be removed here. */
3000 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3001 && (type || !type_generic || !type_generic_remove_excess_precision))
3003 val = TREE_OPERAND (val, 0);
3004 excess_precision = true;
3006 val = c_fully_fold (val, false, NULL);
3007 STRIP_TYPE_NOPS (val);
3009 val = require_complete_type (val);
3011 if (type != 0)
3013 /* Formal parm type is specified by a function prototype. */
3015 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3017 error ("type of formal parameter %d is incomplete", parmnum + 1);
3018 parmval = val;
3020 else
3022 tree origtype;
3024 /* Optionally warn about conversions that
3025 differ from the default conversions. */
3026 if (warn_traditional_conversion || warn_traditional)
3028 unsigned int formal_prec = TYPE_PRECISION (type);
3030 if (INTEGRAL_TYPE_P (type)
3031 && TREE_CODE (valtype) == REAL_TYPE)
3032 warning (0, "passing argument %d of %qE as integer "
3033 "rather than floating due to prototype",
3034 argnum, rname);
3035 if (INTEGRAL_TYPE_P (type)
3036 && TREE_CODE (valtype) == COMPLEX_TYPE)
3037 warning (0, "passing argument %d of %qE as integer "
3038 "rather than complex due to prototype",
3039 argnum, rname);
3040 else if (TREE_CODE (type) == COMPLEX_TYPE
3041 && TREE_CODE (valtype) == REAL_TYPE)
3042 warning (0, "passing argument %d of %qE as complex "
3043 "rather than floating due to prototype",
3044 argnum, rname);
3045 else if (TREE_CODE (type) == REAL_TYPE
3046 && INTEGRAL_TYPE_P (valtype))
3047 warning (0, "passing argument %d of %qE as floating "
3048 "rather than integer due to prototype",
3049 argnum, rname);
3050 else if (TREE_CODE (type) == COMPLEX_TYPE
3051 && INTEGRAL_TYPE_P (valtype))
3052 warning (0, "passing argument %d of %qE as complex "
3053 "rather than integer due to prototype",
3054 argnum, rname);
3055 else if (TREE_CODE (type) == REAL_TYPE
3056 && TREE_CODE (valtype) == COMPLEX_TYPE)
3057 warning (0, "passing argument %d of %qE as floating "
3058 "rather than complex due to prototype",
3059 argnum, rname);
3060 /* ??? At some point, messages should be written about
3061 conversions between complex types, but that's too messy
3062 to do now. */
3063 else if (TREE_CODE (type) == REAL_TYPE
3064 && TREE_CODE (valtype) == REAL_TYPE)
3066 /* Warn if any argument is passed as `float',
3067 since without a prototype it would be `double'. */
3068 if (formal_prec == TYPE_PRECISION (float_type_node)
3069 && type != dfloat32_type_node)
3070 warning (0, "passing argument %d of %qE as %<float%> "
3071 "rather than %<double%> due to prototype",
3072 argnum, rname);
3074 /* Warn if mismatch between argument and prototype
3075 for decimal float types. Warn of conversions with
3076 binary float types and of precision narrowing due to
3077 prototype. */
3078 else if (type != valtype
3079 && (type == dfloat32_type_node
3080 || type == dfloat64_type_node
3081 || type == dfloat128_type_node
3082 || valtype == dfloat32_type_node
3083 || valtype == dfloat64_type_node
3084 || valtype == dfloat128_type_node)
3085 && (formal_prec
3086 <= TYPE_PRECISION (valtype)
3087 || (type == dfloat128_type_node
3088 && (valtype
3089 != dfloat64_type_node
3090 && (valtype
3091 != dfloat32_type_node)))
3092 || (type == dfloat64_type_node
3093 && (valtype
3094 != dfloat32_type_node))))
3095 warning (0, "passing argument %d of %qE as %qT "
3096 "rather than %qT due to prototype",
3097 argnum, rname, type, valtype);
3100 /* Detect integer changing in width or signedness.
3101 These warnings are only activated with
3102 -Wtraditional-conversion, not with -Wtraditional. */
3103 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3104 && INTEGRAL_TYPE_P (valtype))
3106 tree would_have_been = default_conversion (val);
3107 tree type1 = TREE_TYPE (would_have_been);
3109 if (TREE_CODE (type) == ENUMERAL_TYPE
3110 && (TYPE_MAIN_VARIANT (type)
3111 == TYPE_MAIN_VARIANT (valtype)))
3112 /* No warning if function asks for enum
3113 and the actual arg is that enum type. */
3115 else if (formal_prec != TYPE_PRECISION (type1))
3116 warning (OPT_Wtraditional_conversion,
3117 "passing argument %d of %qE "
3118 "with different width due to prototype",
3119 argnum, rname);
3120 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3122 /* Don't complain if the formal parameter type
3123 is an enum, because we can't tell now whether
3124 the value was an enum--even the same enum. */
3125 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3127 else if (TREE_CODE (val) == INTEGER_CST
3128 && int_fits_type_p (val, type))
3129 /* Change in signedness doesn't matter
3130 if a constant value is unaffected. */
3132 /* If the value is extended from a narrower
3133 unsigned type, it doesn't matter whether we
3134 pass it as signed or unsigned; the value
3135 certainly is the same either way. */
3136 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3137 && TYPE_UNSIGNED (valtype))
3139 else if (TYPE_UNSIGNED (type))
3140 warning (OPT_Wtraditional_conversion,
3141 "passing argument %d of %qE "
3142 "as unsigned due to prototype",
3143 argnum, rname);
3144 else
3145 warning (OPT_Wtraditional_conversion,
3146 "passing argument %d of %qE "
3147 "as signed due to prototype", argnum, rname);
3151 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3152 sake of better warnings from convert_and_check. */
3153 if (excess_precision)
3154 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3155 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3156 parmval = convert_for_assignment (input_location, type, val,
3157 origtype, ic_argpass, npc,
3158 fundecl, function,
3159 parmnum + 1);
3161 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3162 && INTEGRAL_TYPE_P (type)
3163 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3164 parmval = default_conversion (parmval);
3167 else if (TREE_CODE (valtype) == REAL_TYPE
3168 && (TYPE_PRECISION (valtype)
3169 <= TYPE_PRECISION (double_type_node))
3170 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3171 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3172 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3174 if (type_generic)
3175 parmval = val;
3176 else
3178 /* Convert `float' to `double'. */
3179 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3180 warning (OPT_Wdouble_promotion,
3181 "implicit conversion from %qT to %qT when passing "
3182 "argument to function",
3183 valtype, double_type_node);
3184 parmval = convert (double_type_node, val);
3187 else if (excess_precision && !type_generic)
3188 /* A "double" argument with excess precision being passed
3189 without a prototype or in variable arguments. */
3190 parmval = convert (valtype, val);
3191 else if ((invalid_func_diag =
3192 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3194 error (invalid_func_diag);
3195 return -1;
3197 else
3198 /* Convert `short' and `char' to full-size `int'. */
3199 parmval = default_conversion (val);
3201 (*values)[parmnum] = parmval;
3202 if (parmval == error_mark_node)
3203 error_args = true;
3205 if (typetail)
3206 typetail = TREE_CHAIN (typetail);
3209 gcc_assert (parmnum == vec_safe_length (values));
3211 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3213 error_at (input_location,
3214 "too few arguments to function %qE", function);
3215 inform_declaration (fundecl);
3216 return -1;
3219 return error_args ? -1 : (int) parmnum;
3222 /* This is the entry point used by the parser to build unary operators
3223 in the input. CODE, a tree_code, specifies the unary operator, and
3224 ARG is the operand. For unary plus, the C parser currently uses
3225 CONVERT_EXPR for code.
3227 LOC is the location to use for the tree generated.
3230 struct c_expr
3231 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3233 struct c_expr result;
3235 result.value = build_unary_op (loc, code, arg.value, 0);
3236 result.original_code = code;
3237 result.original_type = NULL;
3239 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3240 overflow_warning (loc, result.value);
3242 return result;
3245 /* This is the entry point used by the parser to build binary operators
3246 in the input. CODE, a tree_code, specifies the binary operator, and
3247 ARG1 and ARG2 are the operands. In addition to constructing the
3248 expression, we check for operands that were written with other binary
3249 operators in a way that is likely to confuse the user.
3251 LOCATION is the location of the binary operator. */
3253 struct c_expr
3254 parser_build_binary_op (location_t location, enum tree_code code,
3255 struct c_expr arg1, struct c_expr arg2)
3257 struct c_expr result;
3259 enum tree_code code1 = arg1.original_code;
3260 enum tree_code code2 = arg2.original_code;
3261 tree type1 = (arg1.original_type
3262 ? arg1.original_type
3263 : TREE_TYPE (arg1.value));
3264 tree type2 = (arg2.original_type
3265 ? arg2.original_type
3266 : TREE_TYPE (arg2.value));
3268 result.value = build_binary_op (location, code,
3269 arg1.value, arg2.value, 1);
3270 result.original_code = code;
3271 result.original_type = NULL;
3273 if (TREE_CODE (result.value) == ERROR_MARK)
3274 return result;
3276 if (location != UNKNOWN_LOCATION)
3277 protected_set_expr_location (result.value, location);
3279 /* Check for cases such as x+y<<z which users are likely
3280 to misinterpret. */
3281 if (warn_parentheses)
3282 warn_about_parentheses (input_location, code,
3283 code1, arg1.value, code2, arg2.value);
3285 if (warn_logical_op)
3286 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3287 code1, arg1.value, code2, arg2.value);
3289 /* Warn about comparisons against string literals, with the exception
3290 of testing for equality or inequality of a string literal with NULL. */
3291 if (code == EQ_EXPR || code == NE_EXPR)
3293 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3294 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3295 warning_at (location, OPT_Waddress,
3296 "comparison with string literal results in unspecified behavior");
3298 else if (TREE_CODE_CLASS (code) == tcc_comparison
3299 && (code1 == STRING_CST || code2 == STRING_CST))
3300 warning_at (location, OPT_Waddress,
3301 "comparison with string literal results in unspecified behavior");
3303 if (TREE_OVERFLOW_P (result.value)
3304 && !TREE_OVERFLOW_P (arg1.value)
3305 && !TREE_OVERFLOW_P (arg2.value))
3306 overflow_warning (location, result.value);
3308 /* Warn about comparisons of different enum types. */
3309 if (warn_enum_compare
3310 && TREE_CODE_CLASS (code) == tcc_comparison
3311 && TREE_CODE (type1) == ENUMERAL_TYPE
3312 && TREE_CODE (type2) == ENUMERAL_TYPE
3313 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3314 warning_at (location, OPT_Wenum_compare,
3315 "comparison between %qT and %qT",
3316 type1, type2);
3318 return result;
3321 /* Return a tree for the difference of pointers OP0 and OP1.
3322 The resulting tree has type int. */
3324 static tree
3325 pointer_diff (location_t loc, tree op0, tree op1)
3327 tree restype = ptrdiff_type_node;
3328 tree result, inttype;
3330 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3331 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3332 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3333 tree con0, con1, lit0, lit1;
3334 tree orig_op1 = op1;
3336 /* If the operands point into different address spaces, we need to
3337 explicitly convert them to pointers into the common address space
3338 before we can subtract the numerical address values. */
3339 if (as0 != as1)
3341 addr_space_t as_common;
3342 tree common_type;
3344 /* Determine the common superset address space. This is guaranteed
3345 to exist because the caller verified that comp_target_types
3346 returned non-zero. */
3347 if (!addr_space_superset (as0, as1, &as_common))
3348 gcc_unreachable ();
3350 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3351 op0 = convert (common_type, op0);
3352 op1 = convert (common_type, op1);
3355 /* Determine integer type to perform computations in. This will usually
3356 be the same as the result type (ptrdiff_t), but may need to be a wider
3357 type if pointers for the address space are wider than ptrdiff_t. */
3358 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3359 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3360 else
3361 inttype = restype;
3364 if (TREE_CODE (target_type) == VOID_TYPE)
3365 pedwarn (loc, OPT_Wpointer_arith,
3366 "pointer of type %<void *%> used in subtraction");
3367 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3368 pedwarn (loc, OPT_Wpointer_arith,
3369 "pointer to a function used in subtraction");
3371 /* If the conversion to ptrdiff_type does anything like widening or
3372 converting a partial to an integral mode, we get a convert_expression
3373 that is in the way to do any simplifications.
3374 (fold-const.c doesn't know that the extra bits won't be needed.
3375 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3376 different mode in place.)
3377 So first try to find a common term here 'by hand'; we want to cover
3378 at least the cases that occur in legal static initializers. */
3379 if (CONVERT_EXPR_P (op0)
3380 && (TYPE_PRECISION (TREE_TYPE (op0))
3381 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3382 con0 = TREE_OPERAND (op0, 0);
3383 else
3384 con0 = op0;
3385 if (CONVERT_EXPR_P (op1)
3386 && (TYPE_PRECISION (TREE_TYPE (op1))
3387 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3388 con1 = TREE_OPERAND (op1, 0);
3389 else
3390 con1 = op1;
3392 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3394 lit0 = TREE_OPERAND (con0, 1);
3395 con0 = TREE_OPERAND (con0, 0);
3397 else
3398 lit0 = integer_zero_node;
3400 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
3402 lit1 = TREE_OPERAND (con1, 1);
3403 con1 = TREE_OPERAND (con1, 0);
3405 else
3406 lit1 = integer_zero_node;
3408 if (operand_equal_p (con0, con1, 0))
3410 op0 = lit0;
3411 op1 = lit1;
3415 /* First do the subtraction as integers;
3416 then drop through to build the divide operator.
3417 Do not do default conversions on the minus operator
3418 in case restype is a short type. */
3420 op0 = build_binary_op (loc,
3421 MINUS_EXPR, convert (inttype, op0),
3422 convert (inttype, op1), 0);
3423 /* This generates an error if op1 is pointer to incomplete type. */
3424 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3425 error_at (loc, "arithmetic on pointer to an incomplete type");
3427 /* This generates an error if op0 is pointer to incomplete type. */
3428 op1 = c_size_in_bytes (target_type);
3430 /* Divide by the size, in easiest possible way. */
3431 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3432 op0, convert (inttype, op1));
3434 /* Convert to final result type if necessary. */
3435 return convert (restype, result);
3438 /* Construct and perhaps optimize a tree representation
3439 for a unary operation. CODE, a tree_code, specifies the operation
3440 and XARG is the operand.
3441 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3442 the default promotions (such as from short to int).
3443 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3444 allows non-lvalues; this is only used to handle conversion of non-lvalue
3445 arrays to pointers in C99.
3447 LOCATION is the location of the operator. */
3449 tree
3450 build_unary_op (location_t location,
3451 enum tree_code code, tree xarg, int flag)
3453 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3454 tree arg = xarg;
3455 tree argtype = 0;
3456 enum tree_code typecode;
3457 tree val;
3458 tree ret = error_mark_node;
3459 tree eptype = NULL_TREE;
3460 int noconvert = flag;
3461 const char *invalid_op_diag;
3462 bool int_operands;
3464 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3465 if (int_operands)
3466 arg = remove_c_maybe_const_expr (arg);
3468 if (code != ADDR_EXPR)
3469 arg = require_complete_type (arg);
3471 typecode = TREE_CODE (TREE_TYPE (arg));
3472 if (typecode == ERROR_MARK)
3473 return error_mark_node;
3474 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3475 typecode = INTEGER_TYPE;
3477 if ((invalid_op_diag
3478 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3480 error_at (location, invalid_op_diag);
3481 return error_mark_node;
3484 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3486 eptype = TREE_TYPE (arg);
3487 arg = TREE_OPERAND (arg, 0);
3490 switch (code)
3492 case CONVERT_EXPR:
3493 /* This is used for unary plus, because a CONVERT_EXPR
3494 is enough to prevent anybody from looking inside for
3495 associativity, but won't generate any code. */
3496 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3497 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3498 || typecode == VECTOR_TYPE))
3500 error_at (location, "wrong type argument to unary plus");
3501 return error_mark_node;
3503 else if (!noconvert)
3504 arg = default_conversion (arg);
3505 arg = non_lvalue_loc (location, arg);
3506 break;
3508 case NEGATE_EXPR:
3509 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3510 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3511 || typecode == VECTOR_TYPE))
3513 error_at (location, "wrong type argument to unary minus");
3514 return error_mark_node;
3516 else if (!noconvert)
3517 arg = default_conversion (arg);
3518 break;
3520 case BIT_NOT_EXPR:
3521 /* ~ works on integer types and non float vectors. */
3522 if (typecode == INTEGER_TYPE
3523 || (typecode == VECTOR_TYPE
3524 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3526 if (!noconvert)
3527 arg = default_conversion (arg);
3529 else if (typecode == COMPLEX_TYPE)
3531 code = CONJ_EXPR;
3532 pedwarn (location, OPT_Wpedantic,
3533 "ISO C does not support %<~%> for complex conjugation");
3534 if (!noconvert)
3535 arg = default_conversion (arg);
3537 else
3539 error_at (location, "wrong type argument to bit-complement");
3540 return error_mark_node;
3542 break;
3544 case ABS_EXPR:
3545 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3547 error_at (location, "wrong type argument to abs");
3548 return error_mark_node;
3550 else if (!noconvert)
3551 arg = default_conversion (arg);
3552 break;
3554 case CONJ_EXPR:
3555 /* Conjugating a real value is a no-op, but allow it anyway. */
3556 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3557 || typecode == COMPLEX_TYPE))
3559 error_at (location, "wrong type argument to conjugation");
3560 return error_mark_node;
3562 else if (!noconvert)
3563 arg = default_conversion (arg);
3564 break;
3566 case TRUTH_NOT_EXPR:
3567 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3568 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3569 && typecode != COMPLEX_TYPE)
3571 error_at (location,
3572 "wrong type argument to unary exclamation mark");
3573 return error_mark_node;
3575 if (int_operands)
3577 arg = c_objc_common_truthvalue_conversion (location, xarg);
3578 arg = remove_c_maybe_const_expr (arg);
3580 else
3581 arg = c_objc_common_truthvalue_conversion (location, arg);
3582 ret = invert_truthvalue_loc (location, arg);
3583 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3584 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3585 location = EXPR_LOCATION (ret);
3586 goto return_build_unary_op;
3588 case REALPART_EXPR:
3589 case IMAGPART_EXPR:
3590 ret = build_real_imag_expr (location, code, arg);
3591 if (ret == error_mark_node)
3592 return error_mark_node;
3593 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3594 eptype = TREE_TYPE (eptype);
3595 goto return_build_unary_op;
3597 case PREINCREMENT_EXPR:
3598 case POSTINCREMENT_EXPR:
3599 case PREDECREMENT_EXPR:
3600 case POSTDECREMENT_EXPR:
3602 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3604 tree inner = build_unary_op (location, code,
3605 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3606 if (inner == error_mark_node)
3607 return error_mark_node;
3608 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3609 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3610 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3611 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3612 goto return_build_unary_op;
3615 /* Complain about anything that is not a true lvalue. In
3616 Objective-C, skip this check for property_refs. */
3617 if (!objc_is_property_ref (arg)
3618 && !lvalue_or_else (location,
3619 arg, ((code == PREINCREMENT_EXPR
3620 || code == POSTINCREMENT_EXPR)
3621 ? lv_increment
3622 : lv_decrement)))
3623 return error_mark_node;
3625 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3627 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3628 warning_at (location, OPT_Wc___compat,
3629 "increment of enumeration value is invalid in C++");
3630 else
3631 warning_at (location, OPT_Wc___compat,
3632 "decrement of enumeration value is invalid in C++");
3635 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3636 arg = c_fully_fold (arg, false, NULL);
3638 /* Increment or decrement the real part of the value,
3639 and don't change the imaginary part. */
3640 if (typecode == COMPLEX_TYPE)
3642 tree real, imag;
3644 pedwarn (location, OPT_Wpedantic,
3645 "ISO C does not support %<++%> and %<--%> on complex types");
3647 arg = stabilize_reference (arg);
3648 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3649 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3650 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3651 if (real == error_mark_node || imag == error_mark_node)
3652 return error_mark_node;
3653 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3654 real, imag);
3655 goto return_build_unary_op;
3658 /* Report invalid types. */
3660 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3661 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3663 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3664 error_at (location, "wrong type argument to increment");
3665 else
3666 error_at (location, "wrong type argument to decrement");
3668 return error_mark_node;
3672 tree inc;
3674 argtype = TREE_TYPE (arg);
3676 /* Compute the increment. */
3678 if (typecode == POINTER_TYPE)
3680 /* If pointer target is an undefined struct,
3681 we just cannot know how to do the arithmetic. */
3682 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3684 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3685 error_at (location,
3686 "increment of pointer to unknown structure");
3687 else
3688 error_at (location,
3689 "decrement of pointer to unknown structure");
3691 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3692 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3694 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3695 pedwarn (location, OPT_Wpointer_arith,
3696 "wrong type argument to increment");
3697 else
3698 pedwarn (location, OPT_Wpointer_arith,
3699 "wrong type argument to decrement");
3702 inc = c_size_in_bytes (TREE_TYPE (argtype));
3703 inc = convert_to_ptrofftype_loc (location, inc);
3705 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3707 /* For signed fract types, we invert ++ to -- or
3708 -- to ++, and change inc from 1 to -1, because
3709 it is not possible to represent 1 in signed fract constants.
3710 For unsigned fract types, the result always overflows and
3711 we get an undefined (original) or the maximum value. */
3712 if (code == PREINCREMENT_EXPR)
3713 code = PREDECREMENT_EXPR;
3714 else if (code == PREDECREMENT_EXPR)
3715 code = PREINCREMENT_EXPR;
3716 else if (code == POSTINCREMENT_EXPR)
3717 code = POSTDECREMENT_EXPR;
3718 else /* code == POSTDECREMENT_EXPR */
3719 code = POSTINCREMENT_EXPR;
3721 inc = integer_minus_one_node;
3722 inc = convert (argtype, inc);
3724 else
3726 inc = integer_one_node;
3727 inc = convert (argtype, inc);
3730 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3731 need to ask Objective-C to build the increment or decrement
3732 expression for it. */
3733 if (objc_is_property_ref (arg))
3734 return objc_build_incr_expr_for_property_ref (location, code,
3735 arg, inc);
3737 /* Report a read-only lvalue. */
3738 if (TYPE_READONLY (argtype))
3740 readonly_error (arg,
3741 ((code == PREINCREMENT_EXPR
3742 || code == POSTINCREMENT_EXPR)
3743 ? lv_increment : lv_decrement));
3744 return error_mark_node;
3746 else if (TREE_READONLY (arg))
3747 readonly_warning (arg,
3748 ((code == PREINCREMENT_EXPR
3749 || code == POSTINCREMENT_EXPR)
3750 ? lv_increment : lv_decrement));
3752 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3753 val = boolean_increment (code, arg);
3754 else
3755 val = build2 (code, TREE_TYPE (arg), arg, inc);
3756 TREE_SIDE_EFFECTS (val) = 1;
3757 if (TREE_CODE (val) != code)
3758 TREE_NO_WARNING (val) = 1;
3759 ret = val;
3760 goto return_build_unary_op;
3763 case ADDR_EXPR:
3764 /* Note that this operation never does default_conversion. */
3766 /* The operand of unary '&' must be an lvalue (which excludes
3767 expressions of type void), or, in C99, the result of a [] or
3768 unary '*' operator. */
3769 if (VOID_TYPE_P (TREE_TYPE (arg))
3770 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3771 && (TREE_CODE (arg) != INDIRECT_REF
3772 || !flag_isoc99))
3773 pedwarn (location, 0, "taking address of expression of type %<void%>");
3775 /* Let &* cancel out to simplify resulting code. */
3776 if (TREE_CODE (arg) == INDIRECT_REF)
3778 /* Don't let this be an lvalue. */
3779 if (lvalue_p (TREE_OPERAND (arg, 0)))
3780 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3781 ret = TREE_OPERAND (arg, 0);
3782 goto return_build_unary_op;
3785 /* For &x[y], return x+y */
3786 if (TREE_CODE (arg) == ARRAY_REF)
3788 tree op0 = TREE_OPERAND (arg, 0);
3789 if (!c_mark_addressable (op0))
3790 return error_mark_node;
3793 /* Anything not already handled and not a true memory reference
3794 or a non-lvalue array is an error. */
3795 else if (typecode != FUNCTION_TYPE && !flag
3796 && !lvalue_or_else (location, arg, lv_addressof))
3797 return error_mark_node;
3799 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3800 folding later. */
3801 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3803 tree inner = build_unary_op (location, code,
3804 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3805 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3806 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3807 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3808 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3809 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3810 goto return_build_unary_op;
3813 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3814 argtype = TREE_TYPE (arg);
3816 /* If the lvalue is const or volatile, merge that into the type
3817 to which the address will point. This is only needed
3818 for function types. */
3819 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3820 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3821 && TREE_CODE (argtype) == FUNCTION_TYPE)
3823 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3824 int quals = orig_quals;
3826 if (TREE_READONLY (arg))
3827 quals |= TYPE_QUAL_CONST;
3828 if (TREE_THIS_VOLATILE (arg))
3829 quals |= TYPE_QUAL_VOLATILE;
3831 argtype = c_build_qualified_type (argtype, quals);
3834 if (!c_mark_addressable (arg))
3835 return error_mark_node;
3837 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3838 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3840 argtype = build_pointer_type (argtype);
3842 /* ??? Cope with user tricks that amount to offsetof. Delete this
3843 when we have proper support for integer constant expressions. */
3844 val = get_base_address (arg);
3845 if (val && TREE_CODE (val) == INDIRECT_REF
3846 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3848 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
3849 goto return_build_unary_op;
3852 val = build1 (ADDR_EXPR, argtype, arg);
3854 ret = val;
3855 goto return_build_unary_op;
3857 default:
3858 gcc_unreachable ();
3861 if (argtype == 0)
3862 argtype = TREE_TYPE (arg);
3863 if (TREE_CODE (arg) == INTEGER_CST)
3864 ret = (require_constant_value
3865 ? fold_build1_initializer_loc (location, code, argtype, arg)
3866 : fold_build1_loc (location, code, argtype, arg));
3867 else
3868 ret = build1 (code, argtype, arg);
3869 return_build_unary_op:
3870 gcc_assert (ret != error_mark_node);
3871 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3872 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3873 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3874 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3875 ret = note_integer_operands (ret);
3876 if (eptype)
3877 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3878 protected_set_expr_location (ret, location);
3879 return ret;
3882 /* Return nonzero if REF is an lvalue valid for this language.
3883 Lvalues can be assigned, unless their type has TYPE_READONLY.
3884 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3886 bool
3887 lvalue_p (const_tree ref)
3889 const enum tree_code code = TREE_CODE (ref);
3891 switch (code)
3893 case REALPART_EXPR:
3894 case IMAGPART_EXPR:
3895 case COMPONENT_REF:
3896 return lvalue_p (TREE_OPERAND (ref, 0));
3898 case C_MAYBE_CONST_EXPR:
3899 return lvalue_p (TREE_OPERAND (ref, 1));
3901 case COMPOUND_LITERAL_EXPR:
3902 case STRING_CST:
3903 return 1;
3905 case INDIRECT_REF:
3906 case ARRAY_REF:
3907 case ARRAY_NOTATION_REF:
3908 case VAR_DECL:
3909 case PARM_DECL:
3910 case RESULT_DECL:
3911 case ERROR_MARK:
3912 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3913 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3915 case BIND_EXPR:
3916 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3918 default:
3919 return 0;
3923 /* Give a warning for storing in something that is read-only in GCC
3924 terms but not const in ISO C terms. */
3926 static void
3927 readonly_warning (tree arg, enum lvalue_use use)
3929 switch (use)
3931 case lv_assign:
3932 warning (0, "assignment of read-only location %qE", arg);
3933 break;
3934 case lv_increment:
3935 warning (0, "increment of read-only location %qE", arg);
3936 break;
3937 case lv_decrement:
3938 warning (0, "decrement of read-only location %qE", arg);
3939 break;
3940 default:
3941 gcc_unreachable ();
3943 return;
3947 /* Return nonzero if REF is an lvalue valid for this language;
3948 otherwise, print an error message and return zero. USE says
3949 how the lvalue is being used and so selects the error message.
3950 LOCATION is the location at which any error should be reported. */
3952 static int
3953 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
3955 int win = lvalue_p (ref);
3957 if (!win)
3958 lvalue_error (loc, use);
3960 return win;
3963 /* Mark EXP saying that we need to be able to take the
3964 address of it; it should not be allocated in a register.
3965 Returns true if successful. */
3967 bool
3968 c_mark_addressable (tree exp)
3970 tree x = exp;
3972 while (1)
3973 switch (TREE_CODE (x))
3975 case COMPONENT_REF:
3976 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3978 error
3979 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3980 return false;
3983 /* ... fall through ... */
3985 case ADDR_EXPR:
3986 case ARRAY_REF:
3987 case REALPART_EXPR:
3988 case IMAGPART_EXPR:
3989 x = TREE_OPERAND (x, 0);
3990 break;
3992 case COMPOUND_LITERAL_EXPR:
3993 case CONSTRUCTOR:
3994 TREE_ADDRESSABLE (x) = 1;
3995 return true;
3997 case VAR_DECL:
3998 case CONST_DECL:
3999 case PARM_DECL:
4000 case RESULT_DECL:
4001 if (C_DECL_REGISTER (x)
4002 && DECL_NONLOCAL (x))
4004 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4006 error
4007 ("global register variable %qD used in nested function", x);
4008 return false;
4010 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4012 else if (C_DECL_REGISTER (x))
4014 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4015 error ("address of global register variable %qD requested", x);
4016 else
4017 error ("address of register variable %qD requested", x);
4018 return false;
4021 /* drops in */
4022 case FUNCTION_DECL:
4023 TREE_ADDRESSABLE (x) = 1;
4024 /* drops out */
4025 default:
4026 return true;
4030 /* Convert EXPR to TYPE, warning about conversion problems with
4031 constants. SEMANTIC_TYPE is the type this conversion would use
4032 without excess precision. If SEMANTIC_TYPE is NULL, this function
4033 is equivalent to convert_and_check. This function is a wrapper that
4034 handles conversions that may be different than
4035 the usual ones because of excess precision. */
4037 static tree
4038 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4040 if (TREE_TYPE (expr) == type)
4041 return expr;
4043 if (!semantic_type)
4044 return convert_and_check (type, expr);
4046 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4047 && TREE_TYPE (expr) != semantic_type)
4049 /* For integers, we need to check the real conversion, not
4050 the conversion to the excess precision type. */
4051 expr = convert_and_check (semantic_type, expr);
4053 /* Result type is the excess precision type, which should be
4054 large enough, so do not check. */
4055 return convert (type, expr);
4058 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4059 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4060 if folded to an integer constant then the unselected half may
4061 contain arbitrary operations not normally permitted in constant
4062 expressions. Set the location of the expression to LOC. */
4064 tree
4065 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4066 tree op1, tree op1_original_type, tree op2,
4067 tree op2_original_type)
4069 tree type1;
4070 tree type2;
4071 enum tree_code code1;
4072 enum tree_code code2;
4073 tree result_type = NULL;
4074 tree semantic_result_type = NULL;
4075 tree orig_op1 = op1, orig_op2 = op2;
4076 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4077 bool ifexp_int_operands;
4078 tree ret;
4080 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4081 if (op1_int_operands)
4082 op1 = remove_c_maybe_const_expr (op1);
4083 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4084 if (op2_int_operands)
4085 op2 = remove_c_maybe_const_expr (op2);
4086 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4087 if (ifexp_int_operands)
4088 ifexp = remove_c_maybe_const_expr (ifexp);
4090 /* Promote both alternatives. */
4092 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4093 op1 = default_conversion (op1);
4094 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4095 op2 = default_conversion (op2);
4097 if (TREE_CODE (ifexp) == ERROR_MARK
4098 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4099 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4100 return error_mark_node;
4102 type1 = TREE_TYPE (op1);
4103 code1 = TREE_CODE (type1);
4104 type2 = TREE_TYPE (op2);
4105 code2 = TREE_CODE (type2);
4107 /* C90 does not permit non-lvalue arrays in conditional expressions.
4108 In C99 they will be pointers by now. */
4109 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4111 error_at (colon_loc, "non-lvalue array in conditional expression");
4112 return error_mark_node;
4115 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4116 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4117 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4118 || code1 == COMPLEX_TYPE)
4119 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4120 || code2 == COMPLEX_TYPE))
4122 semantic_result_type = c_common_type (type1, type2);
4123 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4125 op1 = TREE_OPERAND (op1, 0);
4126 type1 = TREE_TYPE (op1);
4127 gcc_assert (TREE_CODE (type1) == code1);
4129 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4131 op2 = TREE_OPERAND (op2, 0);
4132 type2 = TREE_TYPE (op2);
4133 gcc_assert (TREE_CODE (type2) == code2);
4137 if (warn_cxx_compat)
4139 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4140 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4142 if (TREE_CODE (t1) == ENUMERAL_TYPE
4143 && TREE_CODE (t2) == ENUMERAL_TYPE
4144 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4145 warning_at (colon_loc, OPT_Wc___compat,
4146 ("different enum types in conditional is "
4147 "invalid in C++: %qT vs %qT"),
4148 t1, t2);
4151 /* Quickly detect the usual case where op1 and op2 have the same type
4152 after promotion. */
4153 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4155 if (type1 == type2)
4156 result_type = type1;
4157 else
4158 result_type = TYPE_MAIN_VARIANT (type1);
4160 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4161 || code1 == COMPLEX_TYPE)
4162 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4163 || code2 == COMPLEX_TYPE))
4165 result_type = c_common_type (type1, type2);
4166 do_warn_double_promotion (result_type, type1, type2,
4167 "implicit conversion from %qT to %qT to "
4168 "match other result of conditional",
4169 colon_loc);
4171 /* If -Wsign-compare, warn here if type1 and type2 have
4172 different signedness. We'll promote the signed to unsigned
4173 and later code won't know it used to be different.
4174 Do this check on the original types, so that explicit casts
4175 will be considered, but default promotions won't. */
4176 if (c_inhibit_evaluation_warnings == 0)
4178 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4179 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4181 if (unsigned_op1 ^ unsigned_op2)
4183 bool ovf;
4185 /* Do not warn if the result type is signed, since the
4186 signed type will only be chosen if it can represent
4187 all the values of the unsigned type. */
4188 if (!TYPE_UNSIGNED (result_type))
4189 /* OK */;
4190 else
4192 bool op1_maybe_const = true;
4193 bool op2_maybe_const = true;
4195 /* Do not warn if the signed quantity is an
4196 unsuffixed integer literal (or some static
4197 constant expression involving such literals) and
4198 it is non-negative. This warning requires the
4199 operands to be folded for best results, so do
4200 that folding in this case even without
4201 warn_sign_compare to avoid warning options
4202 possibly affecting code generation. */
4203 c_inhibit_evaluation_warnings
4204 += (ifexp == truthvalue_false_node);
4205 op1 = c_fully_fold (op1, require_constant_value,
4206 &op1_maybe_const);
4207 c_inhibit_evaluation_warnings
4208 -= (ifexp == truthvalue_false_node);
4210 c_inhibit_evaluation_warnings
4211 += (ifexp == truthvalue_true_node);
4212 op2 = c_fully_fold (op2, require_constant_value,
4213 &op2_maybe_const);
4214 c_inhibit_evaluation_warnings
4215 -= (ifexp == truthvalue_true_node);
4217 if (warn_sign_compare)
4219 if ((unsigned_op2
4220 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4221 || (unsigned_op1
4222 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4223 /* OK */;
4224 else
4225 warning_at (colon_loc, OPT_Wsign_compare,
4226 ("signed and unsigned type in "
4227 "conditional expression"));
4229 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4230 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4231 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4232 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4237 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4239 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4240 pedwarn (colon_loc, OPT_Wpedantic,
4241 "ISO C forbids conditional expr with only one void side");
4242 result_type = void_type_node;
4244 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4246 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4247 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4248 addr_space_t as_common;
4250 if (comp_target_types (colon_loc, type1, type2))
4251 result_type = common_pointer_type (type1, type2);
4252 else if (null_pointer_constant_p (orig_op1))
4253 result_type = type2;
4254 else if (null_pointer_constant_p (orig_op2))
4255 result_type = type1;
4256 else if (!addr_space_superset (as1, as2, &as_common))
4258 error_at (colon_loc, "pointers to disjoint address spaces "
4259 "used in conditional expression");
4260 return error_mark_node;
4262 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4264 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4265 pedwarn (colon_loc, OPT_Wpedantic,
4266 "ISO C forbids conditional expr between "
4267 "%<void *%> and function pointer");
4268 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4269 TREE_TYPE (type2)));
4271 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4273 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4274 pedwarn (colon_loc, OPT_Wpedantic,
4275 "ISO C forbids conditional expr between "
4276 "%<void *%> and function pointer");
4277 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4278 TREE_TYPE (type1)));
4280 /* Objective-C pointer comparisons are a bit more lenient. */
4281 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4282 result_type = objc_common_type (type1, type2);
4283 else
4285 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4287 pedwarn (colon_loc, 0,
4288 "pointer type mismatch in conditional expression");
4289 result_type = build_pointer_type
4290 (build_qualified_type (void_type_node, qual));
4293 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4295 if (!null_pointer_constant_p (orig_op2))
4296 pedwarn (colon_loc, 0,
4297 "pointer/integer type mismatch in conditional expression");
4298 else
4300 op2 = null_pointer_node;
4302 result_type = type1;
4304 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4306 if (!null_pointer_constant_p (orig_op1))
4307 pedwarn (colon_loc, 0,
4308 "pointer/integer type mismatch in conditional expression");
4309 else
4311 op1 = null_pointer_node;
4313 result_type = type2;
4316 if (!result_type)
4318 if (flag_cond_mismatch)
4319 result_type = void_type_node;
4320 else
4322 error_at (colon_loc, "type mismatch in conditional expression");
4323 return error_mark_node;
4327 /* Merge const and volatile flags of the incoming types. */
4328 result_type
4329 = build_type_variant (result_type,
4330 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4331 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4333 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4334 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4336 if (ifexp_bcp && ifexp == truthvalue_true_node)
4338 op2_int_operands = true;
4339 op1 = c_fully_fold (op1, require_constant_value, NULL);
4341 if (ifexp_bcp && ifexp == truthvalue_false_node)
4343 op1_int_operands = true;
4344 op2 = c_fully_fold (op2, require_constant_value, NULL);
4346 int_const = int_operands = (ifexp_int_operands
4347 && op1_int_operands
4348 && op2_int_operands);
4349 if (int_operands)
4351 int_const = ((ifexp == truthvalue_true_node
4352 && TREE_CODE (orig_op1) == INTEGER_CST
4353 && !TREE_OVERFLOW (orig_op1))
4354 || (ifexp == truthvalue_false_node
4355 && TREE_CODE (orig_op2) == INTEGER_CST
4356 && !TREE_OVERFLOW (orig_op2)));
4358 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4359 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4360 else
4362 if (int_operands)
4364 op1 = remove_c_maybe_const_expr (op1);
4365 op2 = remove_c_maybe_const_expr (op2);
4367 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4368 if (int_operands)
4369 ret = note_integer_operands (ret);
4371 if (semantic_result_type)
4372 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4374 protected_set_expr_location (ret, colon_loc);
4375 return ret;
4378 /* Return a compound expression that performs two expressions and
4379 returns the value of the second of them.
4381 LOC is the location of the COMPOUND_EXPR. */
4383 tree
4384 build_compound_expr (location_t loc, tree expr1, tree expr2)
4386 bool expr1_int_operands, expr2_int_operands;
4387 tree eptype = NULL_TREE;
4388 tree ret;
4390 if (flag_enable_cilkplus
4391 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4392 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4394 error_at (loc,
4395 "spawned function call cannot be part of a comma expression");
4396 return error_mark_node;
4398 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4399 if (expr1_int_operands)
4400 expr1 = remove_c_maybe_const_expr (expr1);
4401 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4402 if (expr2_int_operands)
4403 expr2 = remove_c_maybe_const_expr (expr2);
4405 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4406 expr1 = TREE_OPERAND (expr1, 0);
4407 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4409 eptype = TREE_TYPE (expr2);
4410 expr2 = TREE_OPERAND (expr2, 0);
4413 if (!TREE_SIDE_EFFECTS (expr1))
4415 /* The left-hand operand of a comma expression is like an expression
4416 statement: with -Wunused, we should warn if it doesn't have
4417 any side-effects, unless it was explicitly cast to (void). */
4418 if (warn_unused_value)
4420 if (VOID_TYPE_P (TREE_TYPE (expr1))
4421 && CONVERT_EXPR_P (expr1))
4422 ; /* (void) a, b */
4423 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4424 && TREE_CODE (expr1) == COMPOUND_EXPR
4425 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4426 ; /* (void) a, (void) b, c */
4427 else
4428 warning_at (loc, OPT_Wunused_value,
4429 "left-hand operand of comma expression has no effect");
4433 /* With -Wunused, we should also warn if the left-hand operand does have
4434 side-effects, but computes a value which is not used. For example, in
4435 `foo() + bar(), baz()' the result of the `+' operator is not used,
4436 so we should issue a warning. */
4437 else if (warn_unused_value)
4438 warn_if_unused_value (expr1, loc);
4440 if (expr2 == error_mark_node)
4441 return error_mark_node;
4443 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4445 if (flag_isoc99
4446 && expr1_int_operands
4447 && expr2_int_operands)
4448 ret = note_integer_operands (ret);
4450 if (eptype)
4451 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4453 protected_set_expr_location (ret, loc);
4454 return ret;
4457 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4458 which we are casting. OTYPE is the type of the expression being
4459 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4460 of the cast. -Wcast-qual appeared on the command line. Named
4461 address space qualifiers are not handled here, because they result
4462 in different warnings. */
4464 static void
4465 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4467 tree in_type = type;
4468 tree in_otype = otype;
4469 int added = 0;
4470 int discarded = 0;
4471 bool is_const;
4473 /* Check that the qualifiers on IN_TYPE are a superset of the
4474 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4475 nodes is uninteresting and we stop as soon as we hit a
4476 non-POINTER_TYPE node on either type. */
4479 in_otype = TREE_TYPE (in_otype);
4480 in_type = TREE_TYPE (in_type);
4482 /* GNU C allows cv-qualified function types. 'const' means the
4483 function is very pure, 'volatile' means it can't return. We
4484 need to warn when such qualifiers are added, not when they're
4485 taken away. */
4486 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4487 && TREE_CODE (in_type) == FUNCTION_TYPE)
4488 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4489 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4490 else
4491 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4492 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4494 while (TREE_CODE (in_type) == POINTER_TYPE
4495 && TREE_CODE (in_otype) == POINTER_TYPE);
4497 if (added)
4498 warning_at (loc, OPT_Wcast_qual,
4499 "cast adds %q#v qualifier to function type", added);
4501 if (discarded)
4502 /* There are qualifiers present in IN_OTYPE that are not present
4503 in IN_TYPE. */
4504 warning_at (loc, OPT_Wcast_qual,
4505 "cast discards %q#v qualifier from pointer target type",
4506 discarded);
4508 if (added || discarded)
4509 return;
4511 /* A cast from **T to const **T is unsafe, because it can cause a
4512 const value to be changed with no additional warning. We only
4513 issue this warning if T is the same on both sides, and we only
4514 issue the warning if there are the same number of pointers on
4515 both sides, as otherwise the cast is clearly unsafe anyhow. A
4516 cast is unsafe when a qualifier is added at one level and const
4517 is not present at all outer levels.
4519 To issue this warning, we check at each level whether the cast
4520 adds new qualifiers not already seen. We don't need to special
4521 case function types, as they won't have the same
4522 TYPE_MAIN_VARIANT. */
4524 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4525 return;
4526 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4527 return;
4529 in_type = type;
4530 in_otype = otype;
4531 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4534 in_type = TREE_TYPE (in_type);
4535 in_otype = TREE_TYPE (in_otype);
4536 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4537 && !is_const)
4539 warning_at (loc, OPT_Wcast_qual,
4540 "to be safe all intermediate pointers in cast from "
4541 "%qT to %qT must be %<const%> qualified",
4542 otype, type);
4543 break;
4545 if (is_const)
4546 is_const = TYPE_READONLY (in_type);
4548 while (TREE_CODE (in_type) == POINTER_TYPE);
4551 /* Build an expression representing a cast to type TYPE of expression EXPR.
4552 LOC is the location of the cast-- typically the open paren of the cast. */
4554 tree
4555 build_c_cast (location_t loc, tree type, tree expr)
4557 tree value;
4559 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4560 expr = TREE_OPERAND (expr, 0);
4562 value = expr;
4564 if (type == error_mark_node || expr == error_mark_node)
4565 return error_mark_node;
4567 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4568 only in <protocol> qualifications. But when constructing cast expressions,
4569 the protocols do matter and must be kept around. */
4570 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4571 return build1 (NOP_EXPR, type, expr);
4573 type = TYPE_MAIN_VARIANT (type);
4575 if (TREE_CODE (type) == ARRAY_TYPE)
4577 error_at (loc, "cast specifies array type");
4578 return error_mark_node;
4581 if (TREE_CODE (type) == FUNCTION_TYPE)
4583 error_at (loc, "cast specifies function type");
4584 return error_mark_node;
4587 if (!VOID_TYPE_P (type))
4589 value = require_complete_type (value);
4590 if (value == error_mark_node)
4591 return error_mark_node;
4594 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4596 if (TREE_CODE (type) == RECORD_TYPE
4597 || TREE_CODE (type) == UNION_TYPE)
4598 pedwarn (loc, OPT_Wpedantic,
4599 "ISO C forbids casting nonscalar to the same type");
4601 else if (TREE_CODE (type) == UNION_TYPE)
4603 tree field;
4605 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4606 if (TREE_TYPE (field) != error_mark_node
4607 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4608 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4609 break;
4611 if (field)
4613 tree t;
4614 bool maybe_const = true;
4616 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
4617 t = c_fully_fold (value, false, &maybe_const);
4618 t = build_constructor_single (type, field, t);
4619 if (!maybe_const)
4620 t = c_wrap_maybe_const (t, true);
4621 t = digest_init (loc, type, t,
4622 NULL_TREE, false, true, 0);
4623 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4624 return t;
4626 error_at (loc, "cast to union type from type not present in union");
4627 return error_mark_node;
4629 else
4631 tree otype, ovalue;
4633 if (type == void_type_node)
4635 tree t = build1 (CONVERT_EXPR, type, value);
4636 SET_EXPR_LOCATION (t, loc);
4637 return t;
4640 otype = TREE_TYPE (value);
4642 /* Optionally warn about potentially worrisome casts. */
4643 if (warn_cast_qual
4644 && TREE_CODE (type) == POINTER_TYPE
4645 && TREE_CODE (otype) == POINTER_TYPE)
4646 handle_warn_cast_qual (loc, type, otype);
4648 /* Warn about conversions between pointers to disjoint
4649 address spaces. */
4650 if (TREE_CODE (type) == POINTER_TYPE
4651 && TREE_CODE (otype) == POINTER_TYPE
4652 && !null_pointer_constant_p (value))
4654 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4655 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4656 addr_space_t as_common;
4658 if (!addr_space_superset (as_to, as_from, &as_common))
4660 if (ADDR_SPACE_GENERIC_P (as_from))
4661 warning_at (loc, 0, "cast to %s address space pointer "
4662 "from disjoint generic address space pointer",
4663 c_addr_space_name (as_to));
4665 else if (ADDR_SPACE_GENERIC_P (as_to))
4666 warning_at (loc, 0, "cast to generic address space pointer "
4667 "from disjoint %s address space pointer",
4668 c_addr_space_name (as_from));
4670 else
4671 warning_at (loc, 0, "cast to %s address space pointer "
4672 "from disjoint %s address space pointer",
4673 c_addr_space_name (as_to),
4674 c_addr_space_name (as_from));
4678 /* Warn about possible alignment problems. */
4679 if (STRICT_ALIGNMENT
4680 && TREE_CODE (type) == POINTER_TYPE
4681 && TREE_CODE (otype) == POINTER_TYPE
4682 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4683 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4684 /* Don't warn about opaque types, where the actual alignment
4685 restriction is unknown. */
4686 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4687 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4688 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4689 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4690 warning_at (loc, OPT_Wcast_align,
4691 "cast increases required alignment of target type");
4693 if (TREE_CODE (type) == INTEGER_TYPE
4694 && TREE_CODE (otype) == POINTER_TYPE
4695 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4696 /* Unlike conversion of integers to pointers, where the
4697 warning is disabled for converting constants because
4698 of cases such as SIG_*, warn about converting constant
4699 pointers to integers. In some cases it may cause unwanted
4700 sign extension, and a warning is appropriate. */
4701 warning_at (loc, OPT_Wpointer_to_int_cast,
4702 "cast from pointer to integer of different size");
4704 if (TREE_CODE (value) == CALL_EXPR
4705 && TREE_CODE (type) != TREE_CODE (otype))
4706 warning_at (loc, OPT_Wbad_function_cast,
4707 "cast from function call of type %qT "
4708 "to non-matching type %qT", otype, type);
4710 if (TREE_CODE (type) == POINTER_TYPE
4711 && TREE_CODE (otype) == INTEGER_TYPE
4712 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4713 /* Don't warn about converting any constant. */
4714 && !TREE_CONSTANT (value))
4715 warning_at (loc,
4716 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4717 "of different size");
4719 if (warn_strict_aliasing <= 2)
4720 strict_aliasing_warning (otype, type, expr);
4722 /* If pedantic, warn for conversions between function and object
4723 pointer types, except for converting a null pointer constant
4724 to function pointer type. */
4725 if (pedantic
4726 && TREE_CODE (type) == POINTER_TYPE
4727 && TREE_CODE (otype) == POINTER_TYPE
4728 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4729 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4730 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
4731 "conversion of function pointer to object pointer type");
4733 if (pedantic
4734 && TREE_CODE (type) == POINTER_TYPE
4735 && TREE_CODE (otype) == POINTER_TYPE
4736 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4737 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4738 && !null_pointer_constant_p (value))
4739 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
4740 "conversion of object pointer to function pointer type");
4742 ovalue = value;
4743 value = convert (type, value);
4745 /* Ignore any integer overflow caused by the cast. */
4746 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4748 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4750 if (!TREE_OVERFLOW (value))
4752 /* Avoid clobbering a shared constant. */
4753 value = copy_node (value);
4754 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4757 else if (TREE_OVERFLOW (value))
4758 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4759 value = build_int_cst_wide (TREE_TYPE (value),
4760 TREE_INT_CST_LOW (value),
4761 TREE_INT_CST_HIGH (value));
4765 /* Don't let a cast be an lvalue. */
4766 if (value == expr)
4767 value = non_lvalue_loc (loc, value);
4769 /* Don't allow the results of casting to floating-point or complex
4770 types be confused with actual constants, or casts involving
4771 integer and pointer types other than direct integer-to-integer
4772 and integer-to-pointer be confused with integer constant
4773 expressions and null pointer constants. */
4774 if (TREE_CODE (value) == REAL_CST
4775 || TREE_CODE (value) == COMPLEX_CST
4776 || (TREE_CODE (value) == INTEGER_CST
4777 && !((TREE_CODE (expr) == INTEGER_CST
4778 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4779 || TREE_CODE (expr) == REAL_CST
4780 || TREE_CODE (expr) == COMPLEX_CST)))
4781 value = build1 (NOP_EXPR, type, value);
4783 if (CAN_HAVE_LOCATION_P (value))
4784 SET_EXPR_LOCATION (value, loc);
4785 return value;
4788 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4789 location of the open paren of the cast, or the position of the cast
4790 expr. */
4791 tree
4792 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4794 tree type;
4795 tree type_expr = NULL_TREE;
4796 bool type_expr_const = true;
4797 tree ret;
4798 int saved_wsp = warn_strict_prototypes;
4800 /* This avoids warnings about unprototyped casts on
4801 integers. E.g. "#define SIG_DFL (void(*)())0". */
4802 if (TREE_CODE (expr) == INTEGER_CST)
4803 warn_strict_prototypes = 0;
4804 type = groktypename (type_name, &type_expr, &type_expr_const);
4805 warn_strict_prototypes = saved_wsp;
4807 ret = build_c_cast (loc, type, expr);
4808 if (type_expr)
4810 bool inner_expr_const = true;
4811 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
4812 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4813 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
4814 && inner_expr_const);
4815 SET_EXPR_LOCATION (ret, loc);
4818 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4819 SET_EXPR_LOCATION (ret, loc);
4821 /* C++ does not permits types to be defined in a cast, but it
4822 allows references to incomplete types. */
4823 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4824 warning_at (loc, OPT_Wc___compat,
4825 "defining a type in a cast is invalid in C++");
4827 return ret;
4830 /* Build an assignment expression of lvalue LHS from value RHS.
4831 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4832 may differ from TREE_TYPE (LHS) for an enum bitfield.
4833 MODIFYCODE is the code for a binary operator that we use
4834 to combine the old value of LHS with RHS to get the new value.
4835 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4836 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4837 which may differ from TREE_TYPE (RHS) for an enum value.
4839 LOCATION is the location of the MODIFYCODE operator.
4840 RHS_LOC is the location of the RHS. */
4842 tree
4843 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4844 enum tree_code modifycode,
4845 location_t rhs_loc, tree rhs, tree rhs_origtype)
4847 tree result;
4848 tree newrhs;
4849 tree rhs_semantic_type = NULL_TREE;
4850 tree lhstype = TREE_TYPE (lhs);
4851 tree olhstype = lhstype;
4852 bool npc;
4854 /* Types that aren't fully specified cannot be used in assignments. */
4855 lhs = require_complete_type (lhs);
4857 /* Avoid duplicate error messages from operands that had errors. */
4858 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4859 return error_mark_node;
4861 /* For ObjC properties, defer this check. */
4862 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4863 return error_mark_node;
4865 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4867 rhs_semantic_type = TREE_TYPE (rhs);
4868 rhs = TREE_OPERAND (rhs, 0);
4871 newrhs = rhs;
4873 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4875 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4876 lhs_origtype, modifycode, rhs_loc, rhs,
4877 rhs_origtype);
4878 if (inner == error_mark_node)
4879 return error_mark_node;
4880 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4881 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4882 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4883 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4884 protected_set_expr_location (result, location);
4885 return result;
4888 /* If a binary op has been requested, combine the old LHS value with the RHS
4889 producing the value we should actually store into the LHS. */
4891 if (modifycode != NOP_EXPR)
4893 lhs = c_fully_fold (lhs, false, NULL);
4894 lhs = stabilize_reference (lhs);
4895 newrhs = build_binary_op (location,
4896 modifycode, lhs, rhs, 1);
4898 /* The original type of the right hand side is no longer
4899 meaningful. */
4900 rhs_origtype = NULL_TREE;
4903 if (c_dialect_objc ())
4905 /* Check if we are modifying an Objective-C property reference;
4906 if so, we need to generate setter calls. */
4907 result = objc_maybe_build_modify_expr (lhs, newrhs);
4908 if (result)
4909 return result;
4911 /* Else, do the check that we postponed for Objective-C. */
4912 if (!lvalue_or_else (location, lhs, lv_assign))
4913 return error_mark_node;
4916 /* Give an error for storing in something that is 'const'. */
4918 if (TYPE_READONLY (lhstype)
4919 || ((TREE_CODE (lhstype) == RECORD_TYPE
4920 || TREE_CODE (lhstype) == UNION_TYPE)
4921 && C_TYPE_FIELDS_READONLY (lhstype)))
4923 readonly_error (lhs, lv_assign);
4924 return error_mark_node;
4926 else if (TREE_READONLY (lhs))
4927 readonly_warning (lhs, lv_assign);
4929 /* If storing into a structure or union member,
4930 it has probably been given type `int'.
4931 Compute the type that would go with
4932 the actual amount of storage the member occupies. */
4934 if (TREE_CODE (lhs) == COMPONENT_REF
4935 && (TREE_CODE (lhstype) == INTEGER_TYPE
4936 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4937 || TREE_CODE (lhstype) == REAL_TYPE
4938 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4939 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4941 /* If storing in a field that is in actuality a short or narrower than one,
4942 we must store in the field in its actual type. */
4944 if (lhstype != TREE_TYPE (lhs))
4946 lhs = copy_node (lhs);
4947 TREE_TYPE (lhs) = lhstype;
4950 /* Issue -Wc++-compat warnings about an assignment to an enum type
4951 when LHS does not have its original type. This happens for,
4952 e.g., an enum bitfield in a struct. */
4953 if (warn_cxx_compat
4954 && lhs_origtype != NULL_TREE
4955 && lhs_origtype != lhstype
4956 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4958 tree checktype = (rhs_origtype != NULL_TREE
4959 ? rhs_origtype
4960 : TREE_TYPE (rhs));
4961 if (checktype != error_mark_node
4962 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4963 warning_at (location, OPT_Wc___compat,
4964 "enum conversion in assignment is invalid in C++");
4967 /* Convert new value to destination type. Fold it first, then
4968 restore any excess precision information, for the sake of
4969 conversion warnings. */
4971 npc = null_pointer_constant_p (newrhs);
4972 newrhs = c_fully_fold (newrhs, false, NULL);
4973 if (rhs_semantic_type)
4974 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4975 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4976 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4977 if (TREE_CODE (newrhs) == ERROR_MARK)
4978 return error_mark_node;
4980 /* Emit ObjC write barrier, if necessary. */
4981 if (c_dialect_objc () && flag_objc_gc)
4983 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4984 if (result)
4986 protected_set_expr_location (result, location);
4987 return result;
4991 /* Scan operands. */
4993 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4994 TREE_SIDE_EFFECTS (result) = 1;
4995 protected_set_expr_location (result, location);
4997 /* If we got the LHS in a different type for storing in,
4998 convert the result back to the nominal type of LHS
4999 so that the value we return always has the same type
5000 as the LHS argument. */
5002 if (olhstype == TREE_TYPE (result))
5003 return result;
5005 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5006 ic_assign, false, NULL_TREE, NULL_TREE, 0);
5007 protected_set_expr_location (result, location);
5008 return result;
5011 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5012 This is used to implement -fplan9-extensions. */
5014 static bool
5015 find_anonymous_field_with_type (tree struct_type, tree type)
5017 tree field;
5018 bool found;
5020 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5021 || TREE_CODE (struct_type) == UNION_TYPE);
5022 found = false;
5023 for (field = TYPE_FIELDS (struct_type);
5024 field != NULL_TREE;
5025 field = TREE_CHAIN (field))
5027 if (DECL_NAME (field) == NULL
5028 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5030 if (found)
5031 return false;
5032 found = true;
5034 else if (DECL_NAME (field) == NULL
5035 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5036 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5037 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5039 if (found)
5040 return false;
5041 found = true;
5044 return found;
5047 /* RHS is an expression whose type is pointer to struct. If there is
5048 an anonymous field in RHS with type TYPE, then return a pointer to
5049 that field in RHS. This is used with -fplan9-extensions. This
5050 returns NULL if no conversion could be found. */
5052 static tree
5053 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5055 tree rhs_struct_type, lhs_main_type;
5056 tree field, found_field;
5057 bool found_sub_field;
5058 tree ret;
5060 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5061 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5062 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5063 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5065 gcc_assert (POINTER_TYPE_P (type));
5066 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5068 found_field = NULL_TREE;
5069 found_sub_field = false;
5070 for (field = TYPE_FIELDS (rhs_struct_type);
5071 field != NULL_TREE;
5072 field = TREE_CHAIN (field))
5074 if (DECL_NAME (field) != NULL_TREE
5075 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5076 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5077 continue;
5078 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5080 if (found_field != NULL_TREE)
5081 return NULL_TREE;
5082 found_field = field;
5084 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5085 lhs_main_type))
5087 if (found_field != NULL_TREE)
5088 return NULL_TREE;
5089 found_field = field;
5090 found_sub_field = true;
5094 if (found_field == NULL_TREE)
5095 return NULL_TREE;
5097 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5098 build_fold_indirect_ref (rhs), found_field,
5099 NULL_TREE);
5100 ret = build_fold_addr_expr_loc (location, ret);
5102 if (found_sub_field)
5104 ret = convert_to_anonymous_field (location, type, ret);
5105 gcc_assert (ret != NULL_TREE);
5108 return ret;
5111 /* Convert value RHS to type TYPE as preparation for an assignment to
5112 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5113 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5114 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5115 constant before any folding.
5116 The real work of conversion is done by `convert'.
5117 The purpose of this function is to generate error messages
5118 for assignments that are not allowed in C.
5119 ERRTYPE says whether it is argument passing, assignment,
5120 initialization or return.
5122 LOCATION is the location of the RHS.
5123 FUNCTION is a tree for the function being called.
5124 PARMNUM is the number of the argument, for printing in error messages. */
5126 static tree
5127 convert_for_assignment (location_t location, tree type, tree rhs,
5128 tree origtype, enum impl_conv errtype,
5129 bool null_pointer_constant, tree fundecl,
5130 tree function, int parmnum)
5132 enum tree_code codel = TREE_CODE (type);
5133 tree orig_rhs = rhs;
5134 tree rhstype;
5135 enum tree_code coder;
5136 tree rname = NULL_TREE;
5137 bool objc_ok = false;
5139 if (errtype == ic_argpass)
5141 tree selector;
5142 /* Change pointer to function to the function itself for
5143 diagnostics. */
5144 if (TREE_CODE (function) == ADDR_EXPR
5145 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5146 function = TREE_OPERAND (function, 0);
5148 /* Handle an ObjC selector specially for diagnostics. */
5149 selector = objc_message_selector ();
5150 rname = function;
5151 if (selector && parmnum > 2)
5153 rname = selector;
5154 parmnum -= 2;
5158 /* This macro is used to emit diagnostics to ensure that all format
5159 strings are complete sentences, visible to gettext and checked at
5160 compile time. */
5161 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5162 do { \
5163 switch (errtype) \
5165 case ic_argpass: \
5166 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5167 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5168 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5169 "expected %qT but argument is of type %qT", \
5170 type, rhstype); \
5171 break; \
5172 case ic_assign: \
5173 pedwarn (LOCATION, OPT, AS); \
5174 break; \
5175 case ic_init: \
5176 pedwarn_init (LOCATION, OPT, IN); \
5177 break; \
5178 case ic_return: \
5179 pedwarn (LOCATION, OPT, RE); \
5180 break; \
5181 default: \
5182 gcc_unreachable (); \
5184 } while (0)
5186 /* This macro is used to emit diagnostics to ensure that all format
5187 strings are complete sentences, visible to gettext and checked at
5188 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5189 extra parameter to enumerate qualifiers. */
5191 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5192 do { \
5193 switch (errtype) \
5195 case ic_argpass: \
5196 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5197 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5198 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5199 "expected %qT but argument is of type %qT", \
5200 type, rhstype); \
5201 break; \
5202 case ic_assign: \
5203 pedwarn (LOCATION, OPT, AS, QUALS); \
5204 break; \
5205 case ic_init: \
5206 pedwarn (LOCATION, OPT, IN, QUALS); \
5207 break; \
5208 case ic_return: \
5209 pedwarn (LOCATION, OPT, RE, QUALS); \
5210 break; \
5211 default: \
5212 gcc_unreachable (); \
5214 } while (0)
5216 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5217 rhs = TREE_OPERAND (rhs, 0);
5219 rhstype = TREE_TYPE (rhs);
5220 coder = TREE_CODE (rhstype);
5222 if (coder == ERROR_MARK)
5223 return error_mark_node;
5225 if (c_dialect_objc ())
5227 int parmno;
5229 switch (errtype)
5231 case ic_return:
5232 parmno = 0;
5233 break;
5235 case ic_assign:
5236 parmno = -1;
5237 break;
5239 case ic_init:
5240 parmno = -2;
5241 break;
5243 default:
5244 parmno = parmnum;
5245 break;
5248 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5251 if (warn_cxx_compat)
5253 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5254 if (checktype != error_mark_node
5255 && TREE_CODE (type) == ENUMERAL_TYPE
5256 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5258 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5259 G_("enum conversion when passing argument "
5260 "%d of %qE is invalid in C++"),
5261 G_("enum conversion in assignment is "
5262 "invalid in C++"),
5263 G_("enum conversion in initialization is "
5264 "invalid in C++"),
5265 G_("enum conversion in return is "
5266 "invalid in C++"));
5270 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5271 return rhs;
5273 if (coder == VOID_TYPE)
5275 /* Except for passing an argument to an unprototyped function,
5276 this is a constraint violation. When passing an argument to
5277 an unprototyped function, it is compile-time undefined;
5278 making it a constraint in that case was rejected in
5279 DR#252. */
5280 error_at (location, "void value not ignored as it ought to be");
5281 return error_mark_node;
5283 rhs = require_complete_type (rhs);
5284 if (rhs == error_mark_node)
5285 return error_mark_node;
5286 /* A non-reference type can convert to a reference. This handles
5287 va_start, va_copy and possibly port built-ins. */
5288 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
5290 if (!lvalue_p (rhs))
5292 error_at (location, "cannot pass rvalue to reference parameter");
5293 return error_mark_node;
5295 if (!c_mark_addressable (rhs))
5296 return error_mark_node;
5297 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5298 SET_EXPR_LOCATION (rhs, location);
5300 rhs = convert_for_assignment (location, build_pointer_type (TREE_TYPE (type)),
5301 rhs, origtype, errtype, null_pointer_constant,
5302 fundecl, function, parmnum);
5303 if (rhs == error_mark_node)
5304 return error_mark_node;
5306 rhs = build1 (NOP_EXPR, type, rhs);
5307 SET_EXPR_LOCATION (rhs, location);
5308 return rhs;
5310 /* Some types can interconvert without explicit casts. */
5311 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5312 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5313 return convert (type, rhs);
5314 /* Arithmetic types all interconvert, and enum is treated like int. */
5315 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5316 || codel == FIXED_POINT_TYPE
5317 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5318 || codel == BOOLEAN_TYPE)
5319 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5320 || coder == FIXED_POINT_TYPE
5321 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5322 || coder == BOOLEAN_TYPE))
5324 tree ret;
5325 bool save = in_late_binary_op;
5326 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5327 in_late_binary_op = true;
5328 ret = convert_and_check (type, orig_rhs);
5329 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5330 in_late_binary_op = save;
5331 return ret;
5334 /* Aggregates in different TUs might need conversion. */
5335 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5336 && codel == coder
5337 && comptypes (type, rhstype))
5338 return convert_and_check (type, rhs);
5340 /* Conversion to a transparent union or record from its member types.
5341 This applies only to function arguments. */
5342 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5343 && TYPE_TRANSPARENT_AGGR (type))
5344 && errtype == ic_argpass)
5346 tree memb, marginal_memb = NULL_TREE;
5348 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5350 tree memb_type = TREE_TYPE (memb);
5352 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5353 TYPE_MAIN_VARIANT (rhstype)))
5354 break;
5356 if (TREE_CODE (memb_type) != POINTER_TYPE)
5357 continue;
5359 if (coder == POINTER_TYPE)
5361 tree ttl = TREE_TYPE (memb_type);
5362 tree ttr = TREE_TYPE (rhstype);
5364 /* Any non-function converts to a [const][volatile] void *
5365 and vice versa; otherwise, targets must be the same.
5366 Meanwhile, the lhs target must have all the qualifiers of
5367 the rhs. */
5368 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5369 || comp_target_types (location, memb_type, rhstype))
5371 /* If this type won't generate any warnings, use it. */
5372 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5373 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5374 && TREE_CODE (ttl) == FUNCTION_TYPE)
5375 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5376 == TYPE_QUALS (ttr))
5377 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5378 == TYPE_QUALS (ttl))))
5379 break;
5381 /* Keep looking for a better type, but remember this one. */
5382 if (!marginal_memb)
5383 marginal_memb = memb;
5387 /* Can convert integer zero to any pointer type. */
5388 if (null_pointer_constant)
5390 rhs = null_pointer_node;
5391 break;
5395 if (memb || marginal_memb)
5397 if (!memb)
5399 /* We have only a marginally acceptable member type;
5400 it needs a warning. */
5401 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5402 tree ttr = TREE_TYPE (rhstype);
5404 /* Const and volatile mean something different for function
5405 types, so the usual warnings are not appropriate. */
5406 if (TREE_CODE (ttr) == FUNCTION_TYPE
5407 && TREE_CODE (ttl) == FUNCTION_TYPE)
5409 /* Because const and volatile on functions are
5410 restrictions that say the function will not do
5411 certain things, it is okay to use a const or volatile
5412 function where an ordinary one is wanted, but not
5413 vice-versa. */
5414 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5415 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5416 WARN_FOR_QUALIFIERS (location, 0,
5417 G_("passing argument %d of %qE "
5418 "makes %q#v qualified function "
5419 "pointer from unqualified"),
5420 G_("assignment makes %q#v qualified "
5421 "function pointer from "
5422 "unqualified"),
5423 G_("initialization makes %q#v qualified "
5424 "function pointer from "
5425 "unqualified"),
5426 G_("return makes %q#v qualified function "
5427 "pointer from unqualified"),
5428 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5430 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5431 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5432 WARN_FOR_QUALIFIERS (location, 0,
5433 G_("passing argument %d of %qE discards "
5434 "%qv qualifier from pointer target type"),
5435 G_("assignment discards %qv qualifier "
5436 "from pointer target type"),
5437 G_("initialization discards %qv qualifier "
5438 "from pointer target type"),
5439 G_("return discards %qv qualifier from "
5440 "pointer target type"),
5441 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5443 memb = marginal_memb;
5446 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5447 pedwarn (location, OPT_Wpedantic,
5448 "ISO C prohibits argument conversion to union type");
5450 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5451 return build_constructor_single (type, memb, rhs);
5455 /* Conversions among pointers */
5456 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5457 && (coder == codel))
5459 tree ttl = TREE_TYPE (type);
5460 tree ttr = TREE_TYPE (rhstype);
5461 tree mvl = ttl;
5462 tree mvr = ttr;
5463 bool is_opaque_pointer;
5464 int target_cmp = 0; /* Cache comp_target_types () result. */
5465 addr_space_t asl;
5466 addr_space_t asr;
5468 if (TREE_CODE (mvl) != ARRAY_TYPE)
5469 mvl = TYPE_MAIN_VARIANT (mvl);
5470 if (TREE_CODE (mvr) != ARRAY_TYPE)
5471 mvr = TYPE_MAIN_VARIANT (mvr);
5472 /* Opaque pointers are treated like void pointers. */
5473 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5475 /* The Plan 9 compiler permits a pointer to a struct to be
5476 automatically converted into a pointer to an anonymous field
5477 within the struct. */
5478 if (flag_plan9_extensions
5479 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5480 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5481 && mvl != mvr)
5483 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5484 if (new_rhs != NULL_TREE)
5486 rhs = new_rhs;
5487 rhstype = TREE_TYPE (rhs);
5488 coder = TREE_CODE (rhstype);
5489 ttr = TREE_TYPE (rhstype);
5490 mvr = TYPE_MAIN_VARIANT (ttr);
5494 /* C++ does not allow the implicit conversion void* -> T*. However,
5495 for the purpose of reducing the number of false positives, we
5496 tolerate the special case of
5498 int *p = NULL;
5500 where NULL is typically defined in C to be '(void *) 0'. */
5501 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5502 warning_at (location, OPT_Wc___compat,
5503 "request for implicit conversion "
5504 "from %qT to %qT not permitted in C++", rhstype, type);
5506 /* See if the pointers point to incompatible address spaces. */
5507 asl = TYPE_ADDR_SPACE (ttl);
5508 asr = TYPE_ADDR_SPACE (ttr);
5509 if (!null_pointer_constant_p (rhs)
5510 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5512 switch (errtype)
5514 case ic_argpass:
5515 error_at (location, "passing argument %d of %qE from pointer to "
5516 "non-enclosed address space", parmnum, rname);
5517 break;
5518 case ic_assign:
5519 error_at (location, "assignment from pointer to "
5520 "non-enclosed address space");
5521 break;
5522 case ic_init:
5523 error_at (location, "initialization from pointer to "
5524 "non-enclosed address space");
5525 break;
5526 case ic_return:
5527 error_at (location, "return from pointer to "
5528 "non-enclosed address space");
5529 break;
5530 default:
5531 gcc_unreachable ();
5533 return error_mark_node;
5536 /* Check if the right-hand side has a format attribute but the
5537 left-hand side doesn't. */
5538 if (warn_suggest_attribute_format
5539 && check_missing_format_attribute (type, rhstype))
5541 switch (errtype)
5543 case ic_argpass:
5544 warning_at (location, OPT_Wsuggest_attribute_format,
5545 "argument %d of %qE might be "
5546 "a candidate for a format attribute",
5547 parmnum, rname);
5548 break;
5549 case ic_assign:
5550 warning_at (location, OPT_Wsuggest_attribute_format,
5551 "assignment left-hand side might be "
5552 "a candidate for a format attribute");
5553 break;
5554 case ic_init:
5555 warning_at (location, OPT_Wsuggest_attribute_format,
5556 "initialization left-hand side might be "
5557 "a candidate for a format attribute");
5558 break;
5559 case ic_return:
5560 warning_at (location, OPT_Wsuggest_attribute_format,
5561 "return type might be "
5562 "a candidate for a format attribute");
5563 break;
5564 default:
5565 gcc_unreachable ();
5569 /* Any non-function converts to a [const][volatile] void *
5570 and vice versa; otherwise, targets must be the same.
5571 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5572 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5573 || (target_cmp = comp_target_types (location, type, rhstype))
5574 || is_opaque_pointer
5575 || ((c_common_unsigned_type (mvl)
5576 == c_common_unsigned_type (mvr))
5577 && c_common_signed_type (mvl)
5578 == c_common_signed_type (mvr)))
5580 if (pedantic
5581 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5583 (VOID_TYPE_P (ttr)
5584 && !null_pointer_constant
5585 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5586 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
5587 G_("ISO C forbids passing argument %d of "
5588 "%qE between function pointer "
5589 "and %<void *%>"),
5590 G_("ISO C forbids assignment between "
5591 "function pointer and %<void *%>"),
5592 G_("ISO C forbids initialization between "
5593 "function pointer and %<void *%>"),
5594 G_("ISO C forbids return between function "
5595 "pointer and %<void *%>"));
5596 /* Const and volatile mean something different for function types,
5597 so the usual warnings are not appropriate. */
5598 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5599 && TREE_CODE (ttl) != FUNCTION_TYPE)
5601 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5602 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5604 WARN_FOR_QUALIFIERS (location, 0,
5605 G_("passing argument %d of %qE discards "
5606 "%qv qualifier from pointer target type"),
5607 G_("assignment discards %qv qualifier "
5608 "from pointer target type"),
5609 G_("initialization discards %qv qualifier "
5610 "from pointer target type"),
5611 G_("return discards %qv qualifier from "
5612 "pointer target type"),
5613 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5615 /* If this is not a case of ignoring a mismatch in signedness,
5616 no warning. */
5617 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5618 || target_cmp)
5620 /* If there is a mismatch, do warn. */
5621 else if (warn_pointer_sign)
5622 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5623 G_("pointer targets in passing argument "
5624 "%d of %qE differ in signedness"),
5625 G_("pointer targets in assignment "
5626 "differ in signedness"),
5627 G_("pointer targets in initialization "
5628 "differ in signedness"),
5629 G_("pointer targets in return differ "
5630 "in signedness"));
5632 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5633 && TREE_CODE (ttr) == FUNCTION_TYPE)
5635 /* Because const and volatile on functions are restrictions
5636 that say the function will not do certain things,
5637 it is okay to use a const or volatile function
5638 where an ordinary one is wanted, but not vice-versa. */
5639 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5640 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5641 WARN_FOR_QUALIFIERS (location, 0,
5642 G_("passing argument %d of %qE makes "
5643 "%q#v qualified function pointer "
5644 "from unqualified"),
5645 G_("assignment makes %q#v qualified function "
5646 "pointer from unqualified"),
5647 G_("initialization makes %q#v qualified "
5648 "function pointer from unqualified"),
5649 G_("return makes %q#v qualified function "
5650 "pointer from unqualified"),
5651 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5654 else
5655 /* Avoid warning about the volatile ObjC EH puts on decls. */
5656 if (!objc_ok)
5657 WARN_FOR_ASSIGNMENT (location, 0,
5658 G_("passing argument %d of %qE from "
5659 "incompatible pointer type"),
5660 G_("assignment from incompatible pointer type"),
5661 G_("initialization from incompatible "
5662 "pointer type"),
5663 G_("return from incompatible pointer type"));
5665 return convert (type, rhs);
5667 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5669 /* ??? This should not be an error when inlining calls to
5670 unprototyped functions. */
5671 error_at (location, "invalid use of non-lvalue array");
5672 return error_mark_node;
5674 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5676 /* An explicit constant 0 can convert to a pointer,
5677 or one that results from arithmetic, even including
5678 a cast to integer type. */
5679 if (!null_pointer_constant)
5680 WARN_FOR_ASSIGNMENT (location, 0,
5681 G_("passing argument %d of %qE makes "
5682 "pointer from integer without a cast"),
5683 G_("assignment makes pointer from integer "
5684 "without a cast"),
5685 G_("initialization makes pointer from "
5686 "integer without a cast"),
5687 G_("return makes pointer from integer "
5688 "without a cast"));
5690 return convert (type, rhs);
5692 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5694 WARN_FOR_ASSIGNMENT (location, 0,
5695 G_("passing argument %d of %qE makes integer "
5696 "from pointer without a cast"),
5697 G_("assignment makes integer from pointer "
5698 "without a cast"),
5699 G_("initialization makes integer from pointer "
5700 "without a cast"),
5701 G_("return makes integer from pointer "
5702 "without a cast"));
5703 return convert (type, rhs);
5705 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5707 tree ret;
5708 bool save = in_late_binary_op;
5709 in_late_binary_op = true;
5710 ret = convert (type, rhs);
5711 in_late_binary_op = save;
5712 return ret;
5715 switch (errtype)
5717 case ic_argpass:
5718 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5719 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5720 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5721 "expected %qT but argument is of type %qT", type, rhstype);
5722 break;
5723 case ic_assign:
5724 error_at (location, "incompatible types when assigning to type %qT from "
5725 "type %qT", type, rhstype);
5726 break;
5727 case ic_init:
5728 error_at (location,
5729 "incompatible types when initializing type %qT using type %qT",
5730 type, rhstype);
5731 break;
5732 case ic_return:
5733 error_at (location,
5734 "incompatible types when returning type %qT but %qT was "
5735 "expected", rhstype, type);
5736 break;
5737 default:
5738 gcc_unreachable ();
5741 return error_mark_node;
5744 /* If VALUE is a compound expr all of whose expressions are constant, then
5745 return its value. Otherwise, return error_mark_node.
5747 This is for handling COMPOUND_EXPRs as initializer elements
5748 which is allowed with a warning when -pedantic is specified. */
5750 static tree
5751 valid_compound_expr_initializer (tree value, tree endtype)
5753 if (TREE_CODE (value) == COMPOUND_EXPR)
5755 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5756 == error_mark_node)
5757 return error_mark_node;
5758 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5759 endtype);
5761 else if (!initializer_constant_valid_p (value, endtype))
5762 return error_mark_node;
5763 else
5764 return value;
5767 /* Perform appropriate conversions on the initial value of a variable,
5768 store it in the declaration DECL,
5769 and print any error messages that are appropriate.
5770 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5771 If the init is invalid, store an ERROR_MARK.
5773 INIT_LOC is the location of the initial value. */
5775 void
5776 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5778 tree value, type;
5779 bool npc = false;
5781 /* If variable's type was invalidly declared, just ignore it. */
5783 type = TREE_TYPE (decl);
5784 if (TREE_CODE (type) == ERROR_MARK)
5785 return;
5787 /* Digest the specified initializer into an expression. */
5789 if (init)
5790 npc = null_pointer_constant_p (init);
5791 value = digest_init (init_loc, type, init, origtype, npc,
5792 true, TREE_STATIC (decl));
5794 /* Store the expression if valid; else report error. */
5796 if (!in_system_header
5797 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5798 warning (OPT_Wtraditional, "traditional C rejects automatic "
5799 "aggregate initialization");
5801 DECL_INITIAL (decl) = value;
5803 /* ANSI wants warnings about out-of-range constant initializers. */
5804 STRIP_TYPE_NOPS (value);
5805 if (TREE_STATIC (decl))
5806 constant_expression_warning (value);
5808 /* Check if we need to set array size from compound literal size. */
5809 if (TREE_CODE (type) == ARRAY_TYPE
5810 && TYPE_DOMAIN (type) == 0
5811 && value != error_mark_node)
5813 tree inside_init = init;
5815 STRIP_TYPE_NOPS (inside_init);
5816 inside_init = fold (inside_init);
5818 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5820 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5822 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5824 /* For int foo[] = (int [3]){1}; we need to set array size
5825 now since later on array initializer will be just the
5826 brace enclosed list of the compound literal. */
5827 tree etype = strip_array_types (TREE_TYPE (decl));
5828 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5829 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5830 layout_type (type);
5831 layout_decl (cldecl, 0);
5832 TREE_TYPE (decl)
5833 = c_build_qualified_type (type, TYPE_QUALS (etype));
5839 /* Methods for storing and printing names for error messages. */
5841 /* Implement a spelling stack that allows components of a name to be pushed
5842 and popped. Each element on the stack is this structure. */
5844 struct spelling
5846 int kind;
5847 union
5849 unsigned HOST_WIDE_INT i;
5850 const char *s;
5851 } u;
5854 #define SPELLING_STRING 1
5855 #define SPELLING_MEMBER 2
5856 #define SPELLING_BOUNDS 3
5858 static struct spelling *spelling; /* Next stack element (unused). */
5859 static struct spelling *spelling_base; /* Spelling stack base. */
5860 static int spelling_size; /* Size of the spelling stack. */
5862 /* Macros to save and restore the spelling stack around push_... functions.
5863 Alternative to SAVE_SPELLING_STACK. */
5865 #define SPELLING_DEPTH() (spelling - spelling_base)
5866 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5868 /* Push an element on the spelling stack with type KIND and assign VALUE
5869 to MEMBER. */
5871 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5873 int depth = SPELLING_DEPTH (); \
5875 if (depth >= spelling_size) \
5877 spelling_size += 10; \
5878 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5879 spelling_size); \
5880 RESTORE_SPELLING_DEPTH (depth); \
5883 spelling->kind = (KIND); \
5884 spelling->MEMBER = (VALUE); \
5885 spelling++; \
5888 /* Push STRING on the stack. Printed literally. */
5890 static void
5891 push_string (const char *string)
5893 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5896 /* Push a member name on the stack. Printed as '.' STRING. */
5898 static void
5899 push_member_name (tree decl)
5901 const char *const string
5902 = (DECL_NAME (decl)
5903 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5904 : _("<anonymous>"));
5905 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5908 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5910 static void
5911 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5913 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5916 /* Compute the maximum size in bytes of the printed spelling. */
5918 static int
5919 spelling_length (void)
5921 int size = 0;
5922 struct spelling *p;
5924 for (p = spelling_base; p < spelling; p++)
5926 if (p->kind == SPELLING_BOUNDS)
5927 size += 25;
5928 else
5929 size += strlen (p->u.s) + 1;
5932 return size;
5935 /* Print the spelling to BUFFER and return it. */
5937 static char *
5938 print_spelling (char *buffer)
5940 char *d = buffer;
5941 struct spelling *p;
5943 for (p = spelling_base; p < spelling; p++)
5944 if (p->kind == SPELLING_BOUNDS)
5946 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5947 d += strlen (d);
5949 else
5951 const char *s;
5952 if (p->kind == SPELLING_MEMBER)
5953 *d++ = '.';
5954 for (s = p->u.s; (*d = *s++); d++)
5957 *d++ = '\0';
5958 return buffer;
5961 /* Issue an error message for a bad initializer component.
5962 GMSGID identifies the message.
5963 The component name is taken from the spelling stack. */
5965 void
5966 error_init (const char *gmsgid)
5968 char *ofwhat;
5970 /* The gmsgid may be a format string with %< and %>. */
5971 error (gmsgid);
5972 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5973 if (*ofwhat)
5974 error ("(near initialization for %qs)", ofwhat);
5977 /* Issue a pedantic warning for a bad initializer component. OPT is
5978 the option OPT_* (from options.h) controlling this warning or 0 if
5979 it is unconditionally given. GMSGID identifies the message. The
5980 component name is taken from the spelling stack. */
5982 void
5983 pedwarn_init (location_t location, int opt, const char *gmsgid)
5985 char *ofwhat;
5987 /* The gmsgid may be a format string with %< and %>. */
5988 pedwarn (location, opt, gmsgid);
5989 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5990 if (*ofwhat)
5991 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5994 /* Issue a warning for a bad initializer component.
5996 OPT is the OPT_W* value corresponding to the warning option that
5997 controls this warning. GMSGID identifies the message. The
5998 component name is taken from the spelling stack. */
6000 static void
6001 warning_init (int opt, const char *gmsgid)
6003 char *ofwhat;
6005 /* The gmsgid may be a format string with %< and %>. */
6006 warning (opt, gmsgid);
6007 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6008 if (*ofwhat)
6009 warning (opt, "(near initialization for %qs)", ofwhat);
6012 /* If TYPE is an array type and EXPR is a parenthesized string
6013 constant, warn if pedantic that EXPR is being used to initialize an
6014 object of type TYPE. */
6016 void
6017 maybe_warn_string_init (tree type, struct c_expr expr)
6019 if (pedantic
6020 && TREE_CODE (type) == ARRAY_TYPE
6021 && TREE_CODE (expr.value) == STRING_CST
6022 && expr.original_code != STRING_CST)
6023 pedwarn_init (input_location, OPT_Wpedantic,
6024 "array initialized from parenthesized string constant");
6027 /* Digest the parser output INIT as an initializer for type TYPE.
6028 Return a C expression of type TYPE to represent the initial value.
6030 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6032 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6034 If INIT is a string constant, STRICT_STRING is true if it is
6035 unparenthesized or we should not warn here for it being parenthesized.
6036 For other types of INIT, STRICT_STRING is not used.
6038 INIT_LOC is the location of the INIT.
6040 REQUIRE_CONSTANT requests an error if non-constant initializers or
6041 elements are seen. */
6043 static tree
6044 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6045 bool null_pointer_constant, bool strict_string,
6046 int require_constant)
6048 enum tree_code code = TREE_CODE (type);
6049 tree inside_init = init;
6050 tree semantic_type = NULL_TREE;
6051 bool maybe_const = true;
6053 if (type == error_mark_node
6054 || !init
6055 || init == error_mark_node
6056 || TREE_TYPE (init) == error_mark_node)
6057 return error_mark_node;
6059 STRIP_TYPE_NOPS (inside_init);
6061 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6063 semantic_type = TREE_TYPE (inside_init);
6064 inside_init = TREE_OPERAND (inside_init, 0);
6066 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6067 inside_init = decl_constant_value_for_optimization (inside_init);
6069 /* Initialization of an array of chars from a string constant
6070 optionally enclosed in braces. */
6072 if (code == ARRAY_TYPE && inside_init
6073 && TREE_CODE (inside_init) == STRING_CST)
6075 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6076 /* Note that an array could be both an array of character type
6077 and an array of wchar_t if wchar_t is signed char or unsigned
6078 char. */
6079 bool char_array = (typ1 == char_type_node
6080 || typ1 == signed_char_type_node
6081 || typ1 == unsigned_char_type_node);
6082 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6083 bool char16_array = !!comptypes (typ1, char16_type_node);
6084 bool char32_array = !!comptypes (typ1, char32_type_node);
6086 if (char_array || wchar_array || char16_array || char32_array)
6088 struct c_expr expr;
6089 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6090 expr.value = inside_init;
6091 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6092 expr.original_type = NULL;
6093 maybe_warn_string_init (type, expr);
6095 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6096 pedwarn_init (init_loc, OPT_Wpedantic,
6097 "initialization of a flexible array member");
6099 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6100 TYPE_MAIN_VARIANT (type)))
6101 return inside_init;
6103 if (char_array)
6105 if (typ2 != char_type_node)
6107 error_init ("char-array initialized from wide string");
6108 return error_mark_node;
6111 else
6113 if (typ2 == char_type_node)
6115 error_init ("wide character array initialized from non-wide "
6116 "string");
6117 return error_mark_node;
6119 else if (!comptypes(typ1, typ2))
6121 error_init ("wide character array initialized from "
6122 "incompatible wide string");
6123 return error_mark_node;
6127 TREE_TYPE (inside_init) = type;
6128 if (TYPE_DOMAIN (type) != 0
6129 && TYPE_SIZE (type) != 0
6130 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6132 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6134 /* Subtract the size of a single (possibly wide) character
6135 because it's ok to ignore the terminating null char
6136 that is counted in the length of the constant. */
6137 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6138 (len
6139 - (TYPE_PRECISION (typ1)
6140 / BITS_PER_UNIT))))
6141 pedwarn_init (init_loc, 0,
6142 ("initializer-string for array of chars "
6143 "is too long"));
6144 else if (warn_cxx_compat
6145 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6146 warning_at (init_loc, OPT_Wc___compat,
6147 ("initializer-string for array chars "
6148 "is too long for C++"));
6151 return inside_init;
6153 else if (INTEGRAL_TYPE_P (typ1))
6155 error_init ("array of inappropriate type initialized "
6156 "from string constant");
6157 return error_mark_node;
6161 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6162 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6163 below and handle as a constructor. */
6164 if (code == VECTOR_TYPE
6165 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6166 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6167 && TREE_CONSTANT (inside_init))
6169 if (TREE_CODE (inside_init) == VECTOR_CST
6170 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6171 TYPE_MAIN_VARIANT (type)))
6172 return inside_init;
6174 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6176 unsigned HOST_WIDE_INT ix;
6177 tree value;
6178 bool constant_p = true;
6180 /* Iterate through elements and check if all constructor
6181 elements are *_CSTs. */
6182 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6183 if (!CONSTANT_CLASS_P (value))
6185 constant_p = false;
6186 break;
6189 if (constant_p)
6190 return build_vector_from_ctor (type,
6191 CONSTRUCTOR_ELTS (inside_init));
6195 if (warn_sequence_point)
6196 verify_sequence_points (inside_init);
6198 /* Any type can be initialized
6199 from an expression of the same type, optionally with braces. */
6201 if (inside_init && TREE_TYPE (inside_init) != 0
6202 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6203 TYPE_MAIN_VARIANT (type))
6204 || (code == ARRAY_TYPE
6205 && comptypes (TREE_TYPE (inside_init), type))
6206 || (code == VECTOR_TYPE
6207 && comptypes (TREE_TYPE (inside_init), type))
6208 || (code == POINTER_TYPE
6209 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6210 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6211 TREE_TYPE (type)))))
6213 if (code == POINTER_TYPE)
6215 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6217 if (TREE_CODE (inside_init) == STRING_CST
6218 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6219 inside_init = array_to_pointer_conversion
6220 (init_loc, inside_init);
6221 else
6223 error_init ("invalid use of non-lvalue array");
6224 return error_mark_node;
6229 if (code == VECTOR_TYPE)
6230 /* Although the types are compatible, we may require a
6231 conversion. */
6232 inside_init = convert (type, inside_init);
6234 if (require_constant
6235 && (code == VECTOR_TYPE || !flag_isoc99)
6236 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6238 /* As an extension, allow initializing objects with static storage
6239 duration with compound literals (which are then treated just as
6240 the brace enclosed list they contain). Also allow this for
6241 vectors, as we can only assign them with compound literals. */
6242 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6243 inside_init = DECL_INITIAL (decl);
6246 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6247 && TREE_CODE (inside_init) != CONSTRUCTOR)
6249 error_init ("array initialized from non-constant array expression");
6250 return error_mark_node;
6253 /* Compound expressions can only occur here if -Wpedantic or
6254 -pedantic-errors is specified. In the later case, we always want
6255 an error. In the former case, we simply want a warning. */
6256 if (require_constant && pedantic
6257 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6259 inside_init
6260 = valid_compound_expr_initializer (inside_init,
6261 TREE_TYPE (inside_init));
6262 if (inside_init == error_mark_node)
6263 error_init ("initializer element is not constant");
6264 else
6265 pedwarn_init (init_loc, OPT_Wpedantic,
6266 "initializer element is not constant");
6267 if (flag_pedantic_errors)
6268 inside_init = error_mark_node;
6270 else if (require_constant
6271 && !initializer_constant_valid_p (inside_init,
6272 TREE_TYPE (inside_init)))
6274 error_init ("initializer element is not constant");
6275 inside_init = error_mark_node;
6277 else if (require_constant && !maybe_const)
6278 pedwarn_init (init_loc, 0,
6279 "initializer element is not a constant expression");
6281 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6282 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6283 inside_init = convert_for_assignment (init_loc, type, inside_init,
6284 origtype,
6285 ic_init, null_pointer_constant,
6286 NULL_TREE, NULL_TREE, 0);
6287 return inside_init;
6290 /* Handle scalar types, including conversions. */
6292 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6293 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6294 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6296 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6297 && (TREE_CODE (init) == STRING_CST
6298 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6299 inside_init = init = array_to_pointer_conversion (init_loc, init);
6300 if (semantic_type)
6301 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6302 inside_init);
6303 inside_init
6304 = convert_for_assignment (init_loc, type, inside_init, origtype,
6305 ic_init, null_pointer_constant,
6306 NULL_TREE, NULL_TREE, 0);
6308 /* Check to see if we have already given an error message. */
6309 if (inside_init == error_mark_node)
6311 else if (require_constant && !TREE_CONSTANT (inside_init))
6313 error_init ("initializer element is not constant");
6314 inside_init = error_mark_node;
6316 else if (require_constant
6317 && !initializer_constant_valid_p (inside_init,
6318 TREE_TYPE (inside_init)))
6320 error_init ("initializer element is not computable at load time");
6321 inside_init = error_mark_node;
6323 else if (require_constant && !maybe_const)
6324 pedwarn_init (init_loc, 0,
6325 "initializer element is not a constant expression");
6327 return inside_init;
6330 /* Come here only for records and arrays. */
6332 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6334 error_init ("variable-sized object may not be initialized");
6335 return error_mark_node;
6338 error_init ("invalid initializer");
6339 return error_mark_node;
6342 /* Handle initializers that use braces. */
6344 /* Type of object we are accumulating a constructor for.
6345 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6346 static tree constructor_type;
6348 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6349 left to fill. */
6350 static tree constructor_fields;
6352 /* For an ARRAY_TYPE, this is the specified index
6353 at which to store the next element we get. */
6354 static tree constructor_index;
6356 /* For an ARRAY_TYPE, this is the maximum index. */
6357 static tree constructor_max_index;
6359 /* For a RECORD_TYPE, this is the first field not yet written out. */
6360 static tree constructor_unfilled_fields;
6362 /* For an ARRAY_TYPE, this is the index of the first element
6363 not yet written out. */
6364 static tree constructor_unfilled_index;
6366 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6367 This is so we can generate gaps between fields, when appropriate. */
6368 static tree constructor_bit_index;
6370 /* If we are saving up the elements rather than allocating them,
6371 this is the list of elements so far (in reverse order,
6372 most recent first). */
6373 static vec<constructor_elt, va_gc> *constructor_elements;
6375 /* 1 if constructor should be incrementally stored into a constructor chain,
6376 0 if all the elements should be kept in AVL tree. */
6377 static int constructor_incremental;
6379 /* 1 if so far this constructor's elements are all compile-time constants. */
6380 static int constructor_constant;
6382 /* 1 if so far this constructor's elements are all valid address constants. */
6383 static int constructor_simple;
6385 /* 1 if this constructor has an element that cannot be part of a
6386 constant expression. */
6387 static int constructor_nonconst;
6389 /* 1 if this constructor is erroneous so far. */
6390 static int constructor_erroneous;
6392 /* Structure for managing pending initializer elements, organized as an
6393 AVL tree. */
6395 struct init_node
6397 struct init_node *left, *right;
6398 struct init_node *parent;
6399 int balance;
6400 tree purpose;
6401 tree value;
6402 tree origtype;
6405 /* Tree of pending elements at this constructor level.
6406 These are elements encountered out of order
6407 which belong at places we haven't reached yet in actually
6408 writing the output.
6409 Will never hold tree nodes across GC runs. */
6410 static struct init_node *constructor_pending_elts;
6412 /* The SPELLING_DEPTH of this constructor. */
6413 static int constructor_depth;
6415 /* DECL node for which an initializer is being read.
6416 0 means we are reading a constructor expression
6417 such as (struct foo) {...}. */
6418 static tree constructor_decl;
6420 /* Nonzero if this is an initializer for a top-level decl. */
6421 static int constructor_top_level;
6423 /* Nonzero if there were any member designators in this initializer. */
6424 static int constructor_designated;
6426 /* Nesting depth of designator list. */
6427 static int designator_depth;
6429 /* Nonzero if there were diagnosed errors in this designator list. */
6430 static int designator_erroneous;
6433 /* This stack has a level for each implicit or explicit level of
6434 structuring in the initializer, including the outermost one. It
6435 saves the values of most of the variables above. */
6437 struct constructor_range_stack;
6439 struct constructor_stack
6441 struct constructor_stack *next;
6442 tree type;
6443 tree fields;
6444 tree index;
6445 tree max_index;
6446 tree unfilled_index;
6447 tree unfilled_fields;
6448 tree bit_index;
6449 vec<constructor_elt, va_gc> *elements;
6450 struct init_node *pending_elts;
6451 int offset;
6452 int depth;
6453 /* If value nonzero, this value should replace the entire
6454 constructor at this level. */
6455 struct c_expr replacement_value;
6456 struct constructor_range_stack *range_stack;
6457 char constant;
6458 char simple;
6459 char nonconst;
6460 char implicit;
6461 char erroneous;
6462 char outer;
6463 char incremental;
6464 char designated;
6467 static struct constructor_stack *constructor_stack;
6469 /* This stack represents designators from some range designator up to
6470 the last designator in the list. */
6472 struct constructor_range_stack
6474 struct constructor_range_stack *next, *prev;
6475 struct constructor_stack *stack;
6476 tree range_start;
6477 tree index;
6478 tree range_end;
6479 tree fields;
6482 static struct constructor_range_stack *constructor_range_stack;
6484 /* This stack records separate initializers that are nested.
6485 Nested initializers can't happen in ANSI C, but GNU C allows them
6486 in cases like { ... (struct foo) { ... } ... }. */
6488 struct initializer_stack
6490 struct initializer_stack *next;
6491 tree decl;
6492 struct constructor_stack *constructor_stack;
6493 struct constructor_range_stack *constructor_range_stack;
6494 vec<constructor_elt, va_gc> *elements;
6495 struct spelling *spelling;
6496 struct spelling *spelling_base;
6497 int spelling_size;
6498 char top_level;
6499 char require_constant_value;
6500 char require_constant_elements;
6503 static struct initializer_stack *initializer_stack;
6505 /* Prepare to parse and output the initializer for variable DECL. */
6507 void
6508 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6510 const char *locus;
6511 struct initializer_stack *p = XNEW (struct initializer_stack);
6513 p->decl = constructor_decl;
6514 p->require_constant_value = require_constant_value;
6515 p->require_constant_elements = require_constant_elements;
6516 p->constructor_stack = constructor_stack;
6517 p->constructor_range_stack = constructor_range_stack;
6518 p->elements = constructor_elements;
6519 p->spelling = spelling;
6520 p->spelling_base = spelling_base;
6521 p->spelling_size = spelling_size;
6522 p->top_level = constructor_top_level;
6523 p->next = initializer_stack;
6524 initializer_stack = p;
6526 constructor_decl = decl;
6527 constructor_designated = 0;
6528 constructor_top_level = top_level;
6530 if (decl != 0 && decl != error_mark_node)
6532 require_constant_value = TREE_STATIC (decl);
6533 require_constant_elements
6534 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6535 /* For a scalar, you can always use any value to initialize,
6536 even within braces. */
6537 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6538 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6539 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6540 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6541 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6543 else
6545 require_constant_value = 0;
6546 require_constant_elements = 0;
6547 locus = _("(anonymous)");
6550 constructor_stack = 0;
6551 constructor_range_stack = 0;
6553 missing_braces_mentioned = 0;
6555 spelling_base = 0;
6556 spelling_size = 0;
6557 RESTORE_SPELLING_DEPTH (0);
6559 if (locus)
6560 push_string (locus);
6563 void
6564 finish_init (void)
6566 struct initializer_stack *p = initializer_stack;
6568 /* Free the whole constructor stack of this initializer. */
6569 while (constructor_stack)
6571 struct constructor_stack *q = constructor_stack;
6572 constructor_stack = q->next;
6573 free (q);
6576 gcc_assert (!constructor_range_stack);
6578 /* Pop back to the data of the outer initializer (if any). */
6579 free (spelling_base);
6581 constructor_decl = p->decl;
6582 require_constant_value = p->require_constant_value;
6583 require_constant_elements = p->require_constant_elements;
6584 constructor_stack = p->constructor_stack;
6585 constructor_range_stack = p->constructor_range_stack;
6586 constructor_elements = p->elements;
6587 spelling = p->spelling;
6588 spelling_base = p->spelling_base;
6589 spelling_size = p->spelling_size;
6590 constructor_top_level = p->top_level;
6591 initializer_stack = p->next;
6592 free (p);
6595 /* Call here when we see the initializer is surrounded by braces.
6596 This is instead of a call to push_init_level;
6597 it is matched by a call to pop_init_level.
6599 TYPE is the type to initialize, for a constructor expression.
6600 For an initializer for a decl, TYPE is zero. */
6602 void
6603 really_start_incremental_init (tree type)
6605 struct constructor_stack *p = XNEW (struct constructor_stack);
6607 if (type == 0)
6608 type = TREE_TYPE (constructor_decl);
6610 if (TREE_CODE (type) == VECTOR_TYPE
6611 && TYPE_VECTOR_OPAQUE (type))
6612 error ("opaque vector types cannot be initialized");
6614 p->type = constructor_type;
6615 p->fields = constructor_fields;
6616 p->index = constructor_index;
6617 p->max_index = constructor_max_index;
6618 p->unfilled_index = constructor_unfilled_index;
6619 p->unfilled_fields = constructor_unfilled_fields;
6620 p->bit_index = constructor_bit_index;
6621 p->elements = constructor_elements;
6622 p->constant = constructor_constant;
6623 p->simple = constructor_simple;
6624 p->nonconst = constructor_nonconst;
6625 p->erroneous = constructor_erroneous;
6626 p->pending_elts = constructor_pending_elts;
6627 p->depth = constructor_depth;
6628 p->replacement_value.value = 0;
6629 p->replacement_value.original_code = ERROR_MARK;
6630 p->replacement_value.original_type = NULL;
6631 p->implicit = 0;
6632 p->range_stack = 0;
6633 p->outer = 0;
6634 p->incremental = constructor_incremental;
6635 p->designated = constructor_designated;
6636 p->next = 0;
6637 constructor_stack = p;
6639 constructor_constant = 1;
6640 constructor_simple = 1;
6641 constructor_nonconst = 0;
6642 constructor_depth = SPELLING_DEPTH ();
6643 constructor_elements = NULL;
6644 constructor_pending_elts = 0;
6645 constructor_type = type;
6646 constructor_incremental = 1;
6647 constructor_designated = 0;
6648 designator_depth = 0;
6649 designator_erroneous = 0;
6651 if (TREE_CODE (constructor_type) == RECORD_TYPE
6652 || TREE_CODE (constructor_type) == UNION_TYPE)
6654 constructor_fields = TYPE_FIELDS (constructor_type);
6655 /* Skip any nameless bit fields at the beginning. */
6656 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6657 && DECL_NAME (constructor_fields) == 0)
6658 constructor_fields = DECL_CHAIN (constructor_fields);
6660 constructor_unfilled_fields = constructor_fields;
6661 constructor_bit_index = bitsize_zero_node;
6663 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6665 if (TYPE_DOMAIN (constructor_type))
6667 constructor_max_index
6668 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6670 /* Detect non-empty initializations of zero-length arrays. */
6671 if (constructor_max_index == NULL_TREE
6672 && TYPE_SIZE (constructor_type))
6673 constructor_max_index = integer_minus_one_node;
6675 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6676 to initialize VLAs will cause a proper error; avoid tree
6677 checking errors as well by setting a safe value. */
6678 if (constructor_max_index
6679 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6680 constructor_max_index = integer_minus_one_node;
6682 constructor_index
6683 = convert (bitsizetype,
6684 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6686 else
6688 constructor_index = bitsize_zero_node;
6689 constructor_max_index = NULL_TREE;
6692 constructor_unfilled_index = constructor_index;
6694 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6696 /* Vectors are like simple fixed-size arrays. */
6697 constructor_max_index =
6698 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6699 constructor_index = bitsize_zero_node;
6700 constructor_unfilled_index = constructor_index;
6702 else
6704 /* Handle the case of int x = {5}; */
6705 constructor_fields = constructor_type;
6706 constructor_unfilled_fields = constructor_type;
6710 /* Push down into a subobject, for initialization.
6711 If this is for an explicit set of braces, IMPLICIT is 0.
6712 If it is because the next element belongs at a lower level,
6713 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6715 void
6716 push_init_level (int implicit, struct obstack * braced_init_obstack)
6718 struct constructor_stack *p;
6719 tree value = NULL_TREE;
6721 /* If we've exhausted any levels that didn't have braces,
6722 pop them now. If implicit == 1, this will have been done in
6723 process_init_element; do not repeat it here because in the case
6724 of excess initializers for an empty aggregate this leads to an
6725 infinite cycle of popping a level and immediately recreating
6726 it. */
6727 if (implicit != 1)
6729 while (constructor_stack->implicit)
6731 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6732 || TREE_CODE (constructor_type) == UNION_TYPE)
6733 && constructor_fields == 0)
6734 process_init_element (pop_init_level (1, braced_init_obstack),
6735 true, braced_init_obstack);
6736 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6737 && constructor_max_index
6738 && tree_int_cst_lt (constructor_max_index,
6739 constructor_index))
6740 process_init_element (pop_init_level (1, braced_init_obstack),
6741 true, braced_init_obstack);
6742 else
6743 break;
6747 /* Unless this is an explicit brace, we need to preserve previous
6748 content if any. */
6749 if (implicit)
6751 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6752 || TREE_CODE (constructor_type) == UNION_TYPE)
6753 && constructor_fields)
6754 value = find_init_member (constructor_fields, braced_init_obstack);
6755 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6756 value = find_init_member (constructor_index, braced_init_obstack);
6759 p = XNEW (struct constructor_stack);
6760 p->type = constructor_type;
6761 p->fields = constructor_fields;
6762 p->index = constructor_index;
6763 p->max_index = constructor_max_index;
6764 p->unfilled_index = constructor_unfilled_index;
6765 p->unfilled_fields = constructor_unfilled_fields;
6766 p->bit_index = constructor_bit_index;
6767 p->elements = constructor_elements;
6768 p->constant = constructor_constant;
6769 p->simple = constructor_simple;
6770 p->nonconst = constructor_nonconst;
6771 p->erroneous = constructor_erroneous;
6772 p->pending_elts = constructor_pending_elts;
6773 p->depth = constructor_depth;
6774 p->replacement_value.value = 0;
6775 p->replacement_value.original_code = ERROR_MARK;
6776 p->replacement_value.original_type = NULL;
6777 p->implicit = implicit;
6778 p->outer = 0;
6779 p->incremental = constructor_incremental;
6780 p->designated = constructor_designated;
6781 p->next = constructor_stack;
6782 p->range_stack = 0;
6783 constructor_stack = p;
6785 constructor_constant = 1;
6786 constructor_simple = 1;
6787 constructor_nonconst = 0;
6788 constructor_depth = SPELLING_DEPTH ();
6789 constructor_elements = NULL;
6790 constructor_incremental = 1;
6791 constructor_designated = 0;
6792 constructor_pending_elts = 0;
6793 if (!implicit)
6795 p->range_stack = constructor_range_stack;
6796 constructor_range_stack = 0;
6797 designator_depth = 0;
6798 designator_erroneous = 0;
6801 /* Don't die if an entire brace-pair level is superfluous
6802 in the containing level. */
6803 if (constructor_type == 0)
6805 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6806 || TREE_CODE (constructor_type) == UNION_TYPE)
6808 /* Don't die if there are extra init elts at the end. */
6809 if (constructor_fields == 0)
6810 constructor_type = 0;
6811 else
6813 constructor_type = TREE_TYPE (constructor_fields);
6814 push_member_name (constructor_fields);
6815 constructor_depth++;
6818 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6820 constructor_type = TREE_TYPE (constructor_type);
6821 push_array_bounds (tree_low_cst (constructor_index, 1));
6822 constructor_depth++;
6825 if (constructor_type == 0)
6827 error_init ("extra brace group at end of initializer");
6828 constructor_fields = 0;
6829 constructor_unfilled_fields = 0;
6830 return;
6833 if (value && TREE_CODE (value) == CONSTRUCTOR)
6835 constructor_constant = TREE_CONSTANT (value);
6836 constructor_simple = TREE_STATIC (value);
6837 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6838 constructor_elements = CONSTRUCTOR_ELTS (value);
6839 if (!vec_safe_is_empty (constructor_elements)
6840 && (TREE_CODE (constructor_type) == RECORD_TYPE
6841 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6842 set_nonincremental_init (braced_init_obstack);
6845 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6847 missing_braces_mentioned = 1;
6848 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6851 if (TREE_CODE (constructor_type) == RECORD_TYPE
6852 || TREE_CODE (constructor_type) == UNION_TYPE)
6854 constructor_fields = TYPE_FIELDS (constructor_type);
6855 /* Skip any nameless bit fields at the beginning. */
6856 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6857 && DECL_NAME (constructor_fields) == 0)
6858 constructor_fields = DECL_CHAIN (constructor_fields);
6860 constructor_unfilled_fields = constructor_fields;
6861 constructor_bit_index = bitsize_zero_node;
6863 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6865 /* Vectors are like simple fixed-size arrays. */
6866 constructor_max_index =
6867 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6868 constructor_index = bitsize_int (0);
6869 constructor_unfilled_index = constructor_index;
6871 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6873 if (TYPE_DOMAIN (constructor_type))
6875 constructor_max_index
6876 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6878 /* Detect non-empty initializations of zero-length arrays. */
6879 if (constructor_max_index == NULL_TREE
6880 && TYPE_SIZE (constructor_type))
6881 constructor_max_index = integer_minus_one_node;
6883 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6884 to initialize VLAs will cause a proper error; avoid tree
6885 checking errors as well by setting a safe value. */
6886 if (constructor_max_index
6887 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6888 constructor_max_index = integer_minus_one_node;
6890 constructor_index
6891 = convert (bitsizetype,
6892 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6894 else
6895 constructor_index = bitsize_zero_node;
6897 constructor_unfilled_index = constructor_index;
6898 if (value && TREE_CODE (value) == STRING_CST)
6900 /* We need to split the char/wchar array into individual
6901 characters, so that we don't have to special case it
6902 everywhere. */
6903 set_nonincremental_init_from_string (value, braced_init_obstack);
6906 else
6908 if (constructor_type != error_mark_node)
6909 warning_init (0, "braces around scalar initializer");
6910 constructor_fields = constructor_type;
6911 constructor_unfilled_fields = constructor_type;
6915 /* At the end of an implicit or explicit brace level,
6916 finish up that level of constructor. If a single expression
6917 with redundant braces initialized that level, return the
6918 c_expr structure for that expression. Otherwise, the original_code
6919 element is set to ERROR_MARK.
6920 If we were outputting the elements as they are read, return 0 as the value
6921 from inner levels (process_init_element ignores that),
6922 but return error_mark_node as the value from the outermost level
6923 (that's what we want to put in DECL_INITIAL).
6924 Otherwise, return a CONSTRUCTOR expression as the value. */
6926 struct c_expr
6927 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6929 struct constructor_stack *p;
6930 struct c_expr ret;
6931 ret.value = 0;
6932 ret.original_code = ERROR_MARK;
6933 ret.original_type = NULL;
6935 if (implicit == 0)
6937 /* When we come to an explicit close brace,
6938 pop any inner levels that didn't have explicit braces. */
6939 while (constructor_stack->implicit)
6941 process_init_element (pop_init_level (1, braced_init_obstack),
6942 true, braced_init_obstack);
6944 gcc_assert (!constructor_range_stack);
6947 /* Now output all pending elements. */
6948 constructor_incremental = 1;
6949 output_pending_init_elements (1, braced_init_obstack);
6951 p = constructor_stack;
6953 /* Error for initializing a flexible array member, or a zero-length
6954 array member in an inappropriate context. */
6955 if (constructor_type && constructor_fields
6956 && TREE_CODE (constructor_type) == ARRAY_TYPE
6957 && TYPE_DOMAIN (constructor_type)
6958 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6960 /* Silently discard empty initializations. The parser will
6961 already have pedwarned for empty brackets. */
6962 if (integer_zerop (constructor_unfilled_index))
6963 constructor_type = NULL_TREE;
6964 else
6966 gcc_assert (!TYPE_SIZE (constructor_type));
6968 if (constructor_depth > 2)
6969 error_init ("initialization of flexible array member in a nested context");
6970 else
6971 pedwarn_init (input_location, OPT_Wpedantic,
6972 "initialization of a flexible array member");
6974 /* We have already issued an error message for the existence
6975 of a flexible array member not at the end of the structure.
6976 Discard the initializer so that we do not die later. */
6977 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6978 constructor_type = NULL_TREE;
6982 /* Warn when some struct elements are implicitly initialized to zero. */
6983 if (warn_missing_field_initializers
6984 && constructor_type
6985 && TREE_CODE (constructor_type) == RECORD_TYPE
6986 && constructor_unfilled_fields)
6988 bool constructor_zeroinit =
6989 (vec_safe_length (constructor_elements) == 1
6990 && integer_zerop ((*constructor_elements)[0].value));
6992 /* Do not warn for flexible array members or zero-length arrays. */
6993 while (constructor_unfilled_fields
6994 && (!DECL_SIZE (constructor_unfilled_fields)
6995 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6996 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
6998 if (constructor_unfilled_fields
6999 /* Do not warn if this level of the initializer uses member
7000 designators; it is likely to be deliberate. */
7001 && !constructor_designated
7002 /* Do not warn about initializing with ` = {0}'. */
7003 && !constructor_zeroinit)
7005 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7006 "missing initializer for field %qD of %qT",
7007 constructor_unfilled_fields,
7008 constructor_type))
7009 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7010 "%qD declared here", constructor_unfilled_fields);
7014 /* Pad out the end of the structure. */
7015 if (p->replacement_value.value)
7016 /* If this closes a superfluous brace pair,
7017 just pass out the element between them. */
7018 ret = p->replacement_value;
7019 else if (constructor_type == 0)
7021 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7022 && TREE_CODE (constructor_type) != UNION_TYPE
7023 && TREE_CODE (constructor_type) != ARRAY_TYPE
7024 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7026 /* A nonincremental scalar initializer--just return
7027 the element, after verifying there is just one. */
7028 if (vec_safe_is_empty (constructor_elements))
7030 if (!constructor_erroneous)
7031 error_init ("empty scalar initializer");
7032 ret.value = error_mark_node;
7034 else if (vec_safe_length (constructor_elements) != 1)
7036 error_init ("extra elements in scalar initializer");
7037 ret.value = (*constructor_elements)[0].value;
7039 else
7040 ret.value = (*constructor_elements)[0].value;
7042 else
7044 if (constructor_erroneous)
7045 ret.value = error_mark_node;
7046 else
7048 ret.value = build_constructor (constructor_type,
7049 constructor_elements);
7050 if (constructor_constant)
7051 TREE_CONSTANT (ret.value) = 1;
7052 if (constructor_constant && constructor_simple)
7053 TREE_STATIC (ret.value) = 1;
7054 if (constructor_nonconst)
7055 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7059 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7061 if (constructor_nonconst)
7062 ret.original_code = C_MAYBE_CONST_EXPR;
7063 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7064 ret.original_code = ERROR_MARK;
7067 constructor_type = p->type;
7068 constructor_fields = p->fields;
7069 constructor_index = p->index;
7070 constructor_max_index = p->max_index;
7071 constructor_unfilled_index = p->unfilled_index;
7072 constructor_unfilled_fields = p->unfilled_fields;
7073 constructor_bit_index = p->bit_index;
7074 constructor_elements = p->elements;
7075 constructor_constant = p->constant;
7076 constructor_simple = p->simple;
7077 constructor_nonconst = p->nonconst;
7078 constructor_erroneous = p->erroneous;
7079 constructor_incremental = p->incremental;
7080 constructor_designated = p->designated;
7081 constructor_pending_elts = p->pending_elts;
7082 constructor_depth = p->depth;
7083 if (!p->implicit)
7084 constructor_range_stack = p->range_stack;
7085 RESTORE_SPELLING_DEPTH (constructor_depth);
7087 constructor_stack = p->next;
7088 free (p);
7090 if (ret.value == 0 && constructor_stack == 0)
7091 ret.value = error_mark_node;
7092 return ret;
7095 /* Common handling for both array range and field name designators.
7096 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7098 static int
7099 set_designator (int array, struct obstack * braced_init_obstack)
7101 tree subtype;
7102 enum tree_code subcode;
7104 /* Don't die if an entire brace-pair level is superfluous
7105 in the containing level. */
7106 if (constructor_type == 0)
7107 return 1;
7109 /* If there were errors in this designator list already, bail out
7110 silently. */
7111 if (designator_erroneous)
7112 return 1;
7114 if (!designator_depth)
7116 gcc_assert (!constructor_range_stack);
7118 /* Designator list starts at the level of closest explicit
7119 braces. */
7120 while (constructor_stack->implicit)
7122 process_init_element (pop_init_level (1, braced_init_obstack),
7123 true, braced_init_obstack);
7125 constructor_designated = 1;
7126 return 0;
7129 switch (TREE_CODE (constructor_type))
7131 case RECORD_TYPE:
7132 case UNION_TYPE:
7133 subtype = TREE_TYPE (constructor_fields);
7134 if (subtype != error_mark_node)
7135 subtype = TYPE_MAIN_VARIANT (subtype);
7136 break;
7137 case ARRAY_TYPE:
7138 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7139 break;
7140 default:
7141 gcc_unreachable ();
7144 subcode = TREE_CODE (subtype);
7145 if (array && subcode != ARRAY_TYPE)
7147 error_init ("array index in non-array initializer");
7148 return 1;
7150 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7152 error_init ("field name not in record or union initializer");
7153 return 1;
7156 constructor_designated = 1;
7157 push_init_level (2, braced_init_obstack);
7158 return 0;
7161 /* If there are range designators in designator list, push a new designator
7162 to constructor_range_stack. RANGE_END is end of such stack range or
7163 NULL_TREE if there is no range designator at this level. */
7165 static void
7166 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7168 struct constructor_range_stack *p;
7170 p = (struct constructor_range_stack *)
7171 obstack_alloc (braced_init_obstack,
7172 sizeof (struct constructor_range_stack));
7173 p->prev = constructor_range_stack;
7174 p->next = 0;
7175 p->fields = constructor_fields;
7176 p->range_start = constructor_index;
7177 p->index = constructor_index;
7178 p->stack = constructor_stack;
7179 p->range_end = range_end;
7180 if (constructor_range_stack)
7181 constructor_range_stack->next = p;
7182 constructor_range_stack = p;
7185 /* Within an array initializer, specify the next index to be initialized.
7186 FIRST is that index. If LAST is nonzero, then initialize a range
7187 of indices, running from FIRST through LAST. */
7189 void
7190 set_init_index (tree first, tree last,
7191 struct obstack * braced_init_obstack)
7193 if (set_designator (1, braced_init_obstack))
7194 return;
7196 designator_erroneous = 1;
7198 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7199 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7201 error_init ("array index in initializer not of integer type");
7202 return;
7205 if (TREE_CODE (first) != INTEGER_CST)
7207 first = c_fully_fold (first, false, NULL);
7208 if (TREE_CODE (first) == INTEGER_CST)
7209 pedwarn_init (input_location, OPT_Wpedantic,
7210 "array index in initializer is not "
7211 "an integer constant expression");
7214 if (last && TREE_CODE (last) != INTEGER_CST)
7216 last = c_fully_fold (last, false, NULL);
7217 if (TREE_CODE (last) == INTEGER_CST)
7218 pedwarn_init (input_location, OPT_Wpedantic,
7219 "array index in initializer is not "
7220 "an integer constant expression");
7223 if (TREE_CODE (first) != INTEGER_CST)
7224 error_init ("nonconstant array index in initializer");
7225 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7226 error_init ("nonconstant array index in initializer");
7227 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7228 error_init ("array index in non-array initializer");
7229 else if (tree_int_cst_sgn (first) == -1)
7230 error_init ("array index in initializer exceeds array bounds");
7231 else if (constructor_max_index
7232 && tree_int_cst_lt (constructor_max_index, first))
7233 error_init ("array index in initializer exceeds array bounds");
7234 else
7236 constant_expression_warning (first);
7237 if (last)
7238 constant_expression_warning (last);
7239 constructor_index = convert (bitsizetype, first);
7240 if (tree_int_cst_lt (constructor_index, first))
7242 constructor_index = copy_node (constructor_index);
7243 TREE_OVERFLOW (constructor_index) = 1;
7246 if (last)
7248 if (tree_int_cst_equal (first, last))
7249 last = 0;
7250 else if (tree_int_cst_lt (last, first))
7252 error_init ("empty index range in initializer");
7253 last = 0;
7255 else
7257 last = convert (bitsizetype, last);
7258 if (constructor_max_index != 0
7259 && tree_int_cst_lt (constructor_max_index, last))
7261 error_init ("array index range in initializer exceeds array bounds");
7262 last = 0;
7267 designator_depth++;
7268 designator_erroneous = 0;
7269 if (constructor_range_stack || last)
7270 push_range_stack (last, braced_init_obstack);
7274 /* Within a struct initializer, specify the next field to be initialized. */
7276 void
7277 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7279 tree field;
7281 if (set_designator (0, braced_init_obstack))
7282 return;
7284 designator_erroneous = 1;
7286 if (TREE_CODE (constructor_type) != RECORD_TYPE
7287 && TREE_CODE (constructor_type) != UNION_TYPE)
7289 error_init ("field name not in record or union initializer");
7290 return;
7293 field = lookup_field (constructor_type, fieldname);
7295 if (field == 0)
7296 error ("unknown field %qE specified in initializer", fieldname);
7297 else
7300 constructor_fields = TREE_VALUE (field);
7301 designator_depth++;
7302 designator_erroneous = 0;
7303 if (constructor_range_stack)
7304 push_range_stack (NULL_TREE, braced_init_obstack);
7305 field = TREE_CHAIN (field);
7306 if (field)
7308 if (set_designator (0, braced_init_obstack))
7309 return;
7312 while (field != NULL_TREE);
7315 /* Add a new initializer to the tree of pending initializers. PURPOSE
7316 identifies the initializer, either array index or field in a structure.
7317 VALUE is the value of that index or field. If ORIGTYPE is not
7318 NULL_TREE, it is the original type of VALUE.
7320 IMPLICIT is true if value comes from pop_init_level (1),
7321 the new initializer has been merged with the existing one
7322 and thus no warnings should be emitted about overriding an
7323 existing initializer. */
7325 static void
7326 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7327 struct obstack * braced_init_obstack)
7329 struct init_node *p, **q, *r;
7331 q = &constructor_pending_elts;
7332 p = 0;
7334 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7336 while (*q != 0)
7338 p = *q;
7339 if (tree_int_cst_lt (purpose, p->purpose))
7340 q = &p->left;
7341 else if (tree_int_cst_lt (p->purpose, purpose))
7342 q = &p->right;
7343 else
7345 if (!implicit)
7347 if (TREE_SIDE_EFFECTS (p->value))
7348 warning_init (0, "initialized field with side-effects overwritten");
7349 else if (warn_override_init)
7350 warning_init (OPT_Woverride_init, "initialized field overwritten");
7352 p->value = value;
7353 p->origtype = origtype;
7354 return;
7358 else
7360 tree bitpos;
7362 bitpos = bit_position (purpose);
7363 while (*q != NULL)
7365 p = *q;
7366 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7367 q = &p->left;
7368 else if (p->purpose != purpose)
7369 q = &p->right;
7370 else
7372 if (!implicit)
7374 if (TREE_SIDE_EFFECTS (p->value))
7375 warning_init (0, "initialized field with side-effects overwritten");
7376 else if (warn_override_init)
7377 warning_init (OPT_Woverride_init, "initialized field overwritten");
7379 p->value = value;
7380 p->origtype = origtype;
7381 return;
7386 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7387 sizeof (struct init_node));
7388 r->purpose = purpose;
7389 r->value = value;
7390 r->origtype = origtype;
7392 *q = r;
7393 r->parent = p;
7394 r->left = 0;
7395 r->right = 0;
7396 r->balance = 0;
7398 while (p)
7400 struct init_node *s;
7402 if (r == p->left)
7404 if (p->balance == 0)
7405 p->balance = -1;
7406 else if (p->balance < 0)
7408 if (r->balance < 0)
7410 /* L rotation. */
7411 p->left = r->right;
7412 if (p->left)
7413 p->left->parent = p;
7414 r->right = p;
7416 p->balance = 0;
7417 r->balance = 0;
7419 s = p->parent;
7420 p->parent = r;
7421 r->parent = s;
7422 if (s)
7424 if (s->left == p)
7425 s->left = r;
7426 else
7427 s->right = r;
7429 else
7430 constructor_pending_elts = r;
7432 else
7434 /* LR rotation. */
7435 struct init_node *t = r->right;
7437 r->right = t->left;
7438 if (r->right)
7439 r->right->parent = r;
7440 t->left = r;
7442 p->left = t->right;
7443 if (p->left)
7444 p->left->parent = p;
7445 t->right = p;
7447 p->balance = t->balance < 0;
7448 r->balance = -(t->balance > 0);
7449 t->balance = 0;
7451 s = p->parent;
7452 p->parent = t;
7453 r->parent = t;
7454 t->parent = s;
7455 if (s)
7457 if (s->left == p)
7458 s->left = t;
7459 else
7460 s->right = t;
7462 else
7463 constructor_pending_elts = t;
7465 break;
7467 else
7469 /* p->balance == +1; growth of left side balances the node. */
7470 p->balance = 0;
7471 break;
7474 else /* r == p->right */
7476 if (p->balance == 0)
7477 /* Growth propagation from right side. */
7478 p->balance++;
7479 else if (p->balance > 0)
7481 if (r->balance > 0)
7483 /* R rotation. */
7484 p->right = r->left;
7485 if (p->right)
7486 p->right->parent = p;
7487 r->left = p;
7489 p->balance = 0;
7490 r->balance = 0;
7492 s = p->parent;
7493 p->parent = r;
7494 r->parent = s;
7495 if (s)
7497 if (s->left == p)
7498 s->left = r;
7499 else
7500 s->right = r;
7502 else
7503 constructor_pending_elts = r;
7505 else /* r->balance == -1 */
7507 /* RL rotation */
7508 struct init_node *t = r->left;
7510 r->left = t->right;
7511 if (r->left)
7512 r->left->parent = r;
7513 t->right = r;
7515 p->right = t->left;
7516 if (p->right)
7517 p->right->parent = p;
7518 t->left = p;
7520 r->balance = (t->balance < 0);
7521 p->balance = -(t->balance > 0);
7522 t->balance = 0;
7524 s = p->parent;
7525 p->parent = t;
7526 r->parent = t;
7527 t->parent = s;
7528 if (s)
7530 if (s->left == p)
7531 s->left = t;
7532 else
7533 s->right = t;
7535 else
7536 constructor_pending_elts = t;
7538 break;
7540 else
7542 /* p->balance == -1; growth of right side balances the node. */
7543 p->balance = 0;
7544 break;
7548 r = p;
7549 p = p->parent;
7553 /* Build AVL tree from a sorted chain. */
7555 static void
7556 set_nonincremental_init (struct obstack * braced_init_obstack)
7558 unsigned HOST_WIDE_INT ix;
7559 tree index, value;
7561 if (TREE_CODE (constructor_type) != RECORD_TYPE
7562 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7563 return;
7565 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7567 add_pending_init (index, value, NULL_TREE, true,
7568 braced_init_obstack);
7570 constructor_elements = NULL;
7571 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7573 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7574 /* Skip any nameless bit fields at the beginning. */
7575 while (constructor_unfilled_fields != 0
7576 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7577 && DECL_NAME (constructor_unfilled_fields) == 0)
7578 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7581 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7583 if (TYPE_DOMAIN (constructor_type))
7584 constructor_unfilled_index
7585 = convert (bitsizetype,
7586 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7587 else
7588 constructor_unfilled_index = bitsize_zero_node;
7590 constructor_incremental = 0;
7593 /* Build AVL tree from a string constant. */
7595 static void
7596 set_nonincremental_init_from_string (tree str,
7597 struct obstack * braced_init_obstack)
7599 tree value, purpose, type;
7600 HOST_WIDE_INT val[2];
7601 const char *p, *end;
7602 int byte, wchar_bytes, charwidth, bitpos;
7604 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7606 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7607 charwidth = TYPE_PRECISION (char_type_node);
7608 type = TREE_TYPE (constructor_type);
7609 p = TREE_STRING_POINTER (str);
7610 end = p + TREE_STRING_LENGTH (str);
7612 for (purpose = bitsize_zero_node;
7613 p < end
7614 && !(constructor_max_index
7615 && tree_int_cst_lt (constructor_max_index, purpose));
7616 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7618 if (wchar_bytes == 1)
7620 val[1] = (unsigned char) *p++;
7621 val[0] = 0;
7623 else
7625 val[0] = 0;
7626 val[1] = 0;
7627 for (byte = 0; byte < wchar_bytes; byte++)
7629 if (BYTES_BIG_ENDIAN)
7630 bitpos = (wchar_bytes - byte - 1) * charwidth;
7631 else
7632 bitpos = byte * charwidth;
7633 val[bitpos < HOST_BITS_PER_WIDE_INT]
7634 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7635 << (bitpos % HOST_BITS_PER_WIDE_INT);
7639 if (!TYPE_UNSIGNED (type))
7641 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7642 if (bitpos < HOST_BITS_PER_WIDE_INT)
7644 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7646 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7647 val[0] = -1;
7650 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7652 if (val[1] < 0)
7653 val[0] = -1;
7655 else if (val[0] & (((HOST_WIDE_INT) 1)
7656 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7657 val[0] |= ((HOST_WIDE_INT) -1)
7658 << (bitpos - HOST_BITS_PER_WIDE_INT);
7661 value = build_int_cst_wide (type, val[1], val[0]);
7662 add_pending_init (purpose, value, NULL_TREE, true,
7663 braced_init_obstack);
7666 constructor_incremental = 0;
7669 /* Return value of FIELD in pending initializer or zero if the field was
7670 not initialized yet. */
7672 static tree
7673 find_init_member (tree field, struct obstack * braced_init_obstack)
7675 struct init_node *p;
7677 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7679 if (constructor_incremental
7680 && tree_int_cst_lt (field, constructor_unfilled_index))
7681 set_nonincremental_init (braced_init_obstack);
7683 p = constructor_pending_elts;
7684 while (p)
7686 if (tree_int_cst_lt (field, p->purpose))
7687 p = p->left;
7688 else if (tree_int_cst_lt (p->purpose, field))
7689 p = p->right;
7690 else
7691 return p->value;
7694 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7696 tree bitpos = bit_position (field);
7698 if (constructor_incremental
7699 && (!constructor_unfilled_fields
7700 || tree_int_cst_lt (bitpos,
7701 bit_position (constructor_unfilled_fields))))
7702 set_nonincremental_init (braced_init_obstack);
7704 p = constructor_pending_elts;
7705 while (p)
7707 if (field == p->purpose)
7708 return p->value;
7709 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7710 p = p->left;
7711 else
7712 p = p->right;
7715 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7717 if (!vec_safe_is_empty (constructor_elements)
7718 && (constructor_elements->last ().index == field))
7719 return constructor_elements->last ().value;
7721 return 0;
7724 /* "Output" the next constructor element.
7725 At top level, really output it to assembler code now.
7726 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7727 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7728 TYPE is the data type that the containing data type wants here.
7729 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7730 If VALUE is a string constant, STRICT_STRING is true if it is
7731 unparenthesized or we should not warn here for it being parenthesized.
7732 For other types of VALUE, STRICT_STRING is not used.
7734 PENDING if non-nil means output pending elements that belong
7735 right after this element. (PENDING is normally 1;
7736 it is 0 while outputting pending elements, to avoid recursion.)
7738 IMPLICIT is true if value comes from pop_init_level (1),
7739 the new initializer has been merged with the existing one
7740 and thus no warnings should be emitted about overriding an
7741 existing initializer. */
7743 static void
7744 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7745 tree field, int pending, bool implicit,
7746 struct obstack * braced_init_obstack)
7748 tree semantic_type = NULL_TREE;
7749 bool maybe_const = true;
7750 bool npc;
7752 if (type == error_mark_node || value == error_mark_node)
7754 constructor_erroneous = 1;
7755 return;
7757 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7758 && (TREE_CODE (value) == STRING_CST
7759 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7760 && !(TREE_CODE (value) == STRING_CST
7761 && TREE_CODE (type) == ARRAY_TYPE
7762 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7763 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7764 TYPE_MAIN_VARIANT (type)))
7765 value = array_to_pointer_conversion (input_location, value);
7767 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7768 && require_constant_value && !flag_isoc99 && pending)
7770 /* As an extension, allow initializing objects with static storage
7771 duration with compound literals (which are then treated just as
7772 the brace enclosed list they contain). */
7773 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7774 value = DECL_INITIAL (decl);
7777 npc = null_pointer_constant_p (value);
7778 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7780 semantic_type = TREE_TYPE (value);
7781 value = TREE_OPERAND (value, 0);
7783 value = c_fully_fold (value, require_constant_value, &maybe_const);
7785 if (value == error_mark_node)
7786 constructor_erroneous = 1;
7787 else if (!TREE_CONSTANT (value))
7788 constructor_constant = 0;
7789 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7790 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7791 || TREE_CODE (constructor_type) == UNION_TYPE)
7792 && DECL_C_BIT_FIELD (field)
7793 && TREE_CODE (value) != INTEGER_CST))
7794 constructor_simple = 0;
7795 if (!maybe_const)
7796 constructor_nonconst = 1;
7798 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7800 if (require_constant_value)
7802 error_init ("initializer element is not constant");
7803 value = error_mark_node;
7805 else if (require_constant_elements)
7806 pedwarn (input_location, 0,
7807 "initializer element is not computable at load time");
7809 else if (!maybe_const
7810 && (require_constant_value || require_constant_elements))
7811 pedwarn_init (input_location, 0,
7812 "initializer element is not a constant expression");
7814 /* Issue -Wc++-compat warnings about initializing a bitfield with
7815 enum type. */
7816 if (warn_cxx_compat
7817 && field != NULL_TREE
7818 && TREE_CODE (field) == FIELD_DECL
7819 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7820 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7821 != TYPE_MAIN_VARIANT (type))
7822 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7824 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7825 if (checktype != error_mark_node
7826 && (TYPE_MAIN_VARIANT (checktype)
7827 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7828 warning_init (OPT_Wc___compat,
7829 "enum conversion in initialization is invalid in C++");
7832 /* If this field is empty (and not at the end of structure),
7833 don't do anything other than checking the initializer. */
7834 if (field
7835 && (TREE_TYPE (field) == error_mark_node
7836 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7837 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7838 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7839 || DECL_CHAIN (field)))))
7840 return;
7842 if (semantic_type)
7843 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7844 value = digest_init (input_location, type, value, origtype, npc,
7845 strict_string, require_constant_value);
7846 if (value == error_mark_node)
7848 constructor_erroneous = 1;
7849 return;
7851 if (require_constant_value || require_constant_elements)
7852 constant_expression_warning (value);
7854 /* If this element doesn't come next in sequence,
7855 put it on constructor_pending_elts. */
7856 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7857 && (!constructor_incremental
7858 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7860 if (constructor_incremental
7861 && tree_int_cst_lt (field, constructor_unfilled_index))
7862 set_nonincremental_init (braced_init_obstack);
7864 add_pending_init (field, value, origtype, implicit,
7865 braced_init_obstack);
7866 return;
7868 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7869 && (!constructor_incremental
7870 || field != constructor_unfilled_fields))
7872 /* We do this for records but not for unions. In a union,
7873 no matter which field is specified, it can be initialized
7874 right away since it starts at the beginning of the union. */
7875 if (constructor_incremental)
7877 if (!constructor_unfilled_fields)
7878 set_nonincremental_init (braced_init_obstack);
7879 else
7881 tree bitpos, unfillpos;
7883 bitpos = bit_position (field);
7884 unfillpos = bit_position (constructor_unfilled_fields);
7886 if (tree_int_cst_lt (bitpos, unfillpos))
7887 set_nonincremental_init (braced_init_obstack);
7891 add_pending_init (field, value, origtype, implicit,
7892 braced_init_obstack);
7893 return;
7895 else if (TREE_CODE (constructor_type) == UNION_TYPE
7896 && !vec_safe_is_empty (constructor_elements))
7898 if (!implicit)
7900 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
7901 warning_init (0,
7902 "initialized field with side-effects overwritten");
7903 else if (warn_override_init)
7904 warning_init (OPT_Woverride_init, "initialized field overwritten");
7907 /* We can have just one union field set. */
7908 constructor_elements = NULL;
7911 /* Otherwise, output this element either to
7912 constructor_elements or to the assembler file. */
7914 constructor_elt celt = {field, value};
7915 vec_safe_push (constructor_elements, celt);
7917 /* Advance the variable that indicates sequential elements output. */
7918 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7919 constructor_unfilled_index
7920 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7921 bitsize_one_node);
7922 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7924 constructor_unfilled_fields
7925 = DECL_CHAIN (constructor_unfilled_fields);
7927 /* Skip any nameless bit fields. */
7928 while (constructor_unfilled_fields != 0
7929 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7930 && DECL_NAME (constructor_unfilled_fields) == 0)
7931 constructor_unfilled_fields =
7932 DECL_CHAIN (constructor_unfilled_fields);
7934 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7935 constructor_unfilled_fields = 0;
7937 /* Now output any pending elements which have become next. */
7938 if (pending)
7939 output_pending_init_elements (0, braced_init_obstack);
7942 /* Output any pending elements which have become next.
7943 As we output elements, constructor_unfilled_{fields,index}
7944 advances, which may cause other elements to become next;
7945 if so, they too are output.
7947 If ALL is 0, we return when there are
7948 no more pending elements to output now.
7950 If ALL is 1, we output space as necessary so that
7951 we can output all the pending elements. */
7952 static void
7953 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7955 struct init_node *elt = constructor_pending_elts;
7956 tree next;
7958 retry:
7960 /* Look through the whole pending tree.
7961 If we find an element that should be output now,
7962 output it. Otherwise, set NEXT to the element
7963 that comes first among those still pending. */
7965 next = 0;
7966 while (elt)
7968 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7970 if (tree_int_cst_equal (elt->purpose,
7971 constructor_unfilled_index))
7972 output_init_element (elt->value, elt->origtype, true,
7973 TREE_TYPE (constructor_type),
7974 constructor_unfilled_index, 0, false,
7975 braced_init_obstack);
7976 else if (tree_int_cst_lt (constructor_unfilled_index,
7977 elt->purpose))
7979 /* Advance to the next smaller node. */
7980 if (elt->left)
7981 elt = elt->left;
7982 else
7984 /* We have reached the smallest node bigger than the
7985 current unfilled index. Fill the space first. */
7986 next = elt->purpose;
7987 break;
7990 else
7992 /* Advance to the next bigger node. */
7993 if (elt->right)
7994 elt = elt->right;
7995 else
7997 /* We have reached the biggest node in a subtree. Find
7998 the parent of it, which is the next bigger node. */
7999 while (elt->parent && elt->parent->right == elt)
8000 elt = elt->parent;
8001 elt = elt->parent;
8002 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8003 elt->purpose))
8005 next = elt->purpose;
8006 break;
8011 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8012 || TREE_CODE (constructor_type) == UNION_TYPE)
8014 tree ctor_unfilled_bitpos, elt_bitpos;
8016 /* If the current record is complete we are done. */
8017 if (constructor_unfilled_fields == 0)
8018 break;
8020 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8021 elt_bitpos = bit_position (elt->purpose);
8022 /* We can't compare fields here because there might be empty
8023 fields in between. */
8024 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8026 constructor_unfilled_fields = elt->purpose;
8027 output_init_element (elt->value, elt->origtype, true,
8028 TREE_TYPE (elt->purpose),
8029 elt->purpose, 0, false,
8030 braced_init_obstack);
8032 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8034 /* Advance to the next smaller node. */
8035 if (elt->left)
8036 elt = elt->left;
8037 else
8039 /* We have reached the smallest node bigger than the
8040 current unfilled field. Fill the space first. */
8041 next = elt->purpose;
8042 break;
8045 else
8047 /* Advance to the next bigger node. */
8048 if (elt->right)
8049 elt = elt->right;
8050 else
8052 /* We have reached the biggest node in a subtree. Find
8053 the parent of it, which is the next bigger node. */
8054 while (elt->parent && elt->parent->right == elt)
8055 elt = elt->parent;
8056 elt = elt->parent;
8057 if (elt
8058 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8059 bit_position (elt->purpose))))
8061 next = elt->purpose;
8062 break;
8069 /* Ordinarily return, but not if we want to output all
8070 and there are elements left. */
8071 if (!(all && next != 0))
8072 return;
8074 /* If it's not incremental, just skip over the gap, so that after
8075 jumping to retry we will output the next successive element. */
8076 if (TREE_CODE (constructor_type) == RECORD_TYPE
8077 || TREE_CODE (constructor_type) == UNION_TYPE)
8078 constructor_unfilled_fields = next;
8079 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8080 constructor_unfilled_index = next;
8082 /* ELT now points to the node in the pending tree with the next
8083 initializer to output. */
8084 goto retry;
8087 /* Add one non-braced element to the current constructor level.
8088 This adjusts the current position within the constructor's type.
8089 This may also start or terminate implicit levels
8090 to handle a partly-braced initializer.
8092 Once this has found the correct level for the new element,
8093 it calls output_init_element.
8095 IMPLICIT is true if value comes from pop_init_level (1),
8096 the new initializer has been merged with the existing one
8097 and thus no warnings should be emitted about overriding an
8098 existing initializer. */
8100 void
8101 process_init_element (struct c_expr value, bool implicit,
8102 struct obstack * braced_init_obstack)
8104 tree orig_value = value.value;
8105 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8106 bool strict_string = value.original_code == STRING_CST;
8108 designator_depth = 0;
8109 designator_erroneous = 0;
8111 /* Handle superfluous braces around string cst as in
8112 char x[] = {"foo"}; */
8113 if (string_flag
8114 && constructor_type
8115 && TREE_CODE (constructor_type) == ARRAY_TYPE
8116 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8117 && integer_zerop (constructor_unfilled_index))
8119 if (constructor_stack->replacement_value.value)
8120 error_init ("excess elements in char array initializer");
8121 constructor_stack->replacement_value = value;
8122 return;
8125 if (constructor_stack->replacement_value.value != 0)
8127 error_init ("excess elements in struct initializer");
8128 return;
8131 /* Ignore elements of a brace group if it is entirely superfluous
8132 and has already been diagnosed. */
8133 if (constructor_type == 0)
8134 return;
8136 /* If we've exhausted any levels that didn't have braces,
8137 pop them now. */
8138 while (constructor_stack->implicit)
8140 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8141 || TREE_CODE (constructor_type) == UNION_TYPE)
8142 && constructor_fields == 0)
8143 process_init_element (pop_init_level (1, braced_init_obstack),
8144 true, braced_init_obstack);
8145 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8146 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8147 && constructor_max_index
8148 && tree_int_cst_lt (constructor_max_index,
8149 constructor_index))
8150 process_init_element (pop_init_level (1, braced_init_obstack),
8151 true, braced_init_obstack);
8152 else
8153 break;
8156 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8157 if (constructor_range_stack)
8159 /* If value is a compound literal and we'll be just using its
8160 content, don't put it into a SAVE_EXPR. */
8161 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8162 || !require_constant_value
8163 || flag_isoc99)
8165 tree semantic_type = NULL_TREE;
8166 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8168 semantic_type = TREE_TYPE (value.value);
8169 value.value = TREE_OPERAND (value.value, 0);
8171 value.value = c_save_expr (value.value);
8172 if (semantic_type)
8173 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8174 value.value);
8178 while (1)
8180 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8182 tree fieldtype;
8183 enum tree_code fieldcode;
8185 if (constructor_fields == 0)
8187 pedwarn_init (input_location, 0,
8188 "excess elements in struct initializer");
8189 break;
8192 fieldtype = TREE_TYPE (constructor_fields);
8193 if (fieldtype != error_mark_node)
8194 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8195 fieldcode = TREE_CODE (fieldtype);
8197 /* Error for non-static initialization of a flexible array member. */
8198 if (fieldcode == ARRAY_TYPE
8199 && !require_constant_value
8200 && TYPE_SIZE (fieldtype) == NULL_TREE
8201 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8203 error_init ("non-static initialization of a flexible array member");
8204 break;
8207 /* Accept a string constant to initialize a subarray. */
8208 if (value.value != 0
8209 && fieldcode == ARRAY_TYPE
8210 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8211 && string_flag)
8212 value.value = orig_value;
8213 /* Otherwise, if we have come to a subaggregate,
8214 and we don't have an element of its type, push into it. */
8215 else if (value.value != 0
8216 && value.value != error_mark_node
8217 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8218 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8219 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8221 push_init_level (1, braced_init_obstack);
8222 continue;
8225 if (value.value)
8227 push_member_name (constructor_fields);
8228 output_init_element (value.value, value.original_type,
8229 strict_string, fieldtype,
8230 constructor_fields, 1, implicit,
8231 braced_init_obstack);
8232 RESTORE_SPELLING_DEPTH (constructor_depth);
8234 else
8235 /* Do the bookkeeping for an element that was
8236 directly output as a constructor. */
8238 /* For a record, keep track of end position of last field. */
8239 if (DECL_SIZE (constructor_fields))
8240 constructor_bit_index
8241 = size_binop_loc (input_location, PLUS_EXPR,
8242 bit_position (constructor_fields),
8243 DECL_SIZE (constructor_fields));
8245 /* If the current field was the first one not yet written out,
8246 it isn't now, so update. */
8247 if (constructor_unfilled_fields == constructor_fields)
8249 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8250 /* Skip any nameless bit fields. */
8251 while (constructor_unfilled_fields != 0
8252 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8253 && DECL_NAME (constructor_unfilled_fields) == 0)
8254 constructor_unfilled_fields =
8255 DECL_CHAIN (constructor_unfilled_fields);
8259 constructor_fields = DECL_CHAIN (constructor_fields);
8260 /* Skip any nameless bit fields at the beginning. */
8261 while (constructor_fields != 0
8262 && DECL_C_BIT_FIELD (constructor_fields)
8263 && DECL_NAME (constructor_fields) == 0)
8264 constructor_fields = DECL_CHAIN (constructor_fields);
8266 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8268 tree fieldtype;
8269 enum tree_code fieldcode;
8271 if (constructor_fields == 0)
8273 pedwarn_init (input_location, 0,
8274 "excess elements in union initializer");
8275 break;
8278 fieldtype = TREE_TYPE (constructor_fields);
8279 if (fieldtype != error_mark_node)
8280 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8281 fieldcode = TREE_CODE (fieldtype);
8283 /* Warn that traditional C rejects initialization of unions.
8284 We skip the warning if the value is zero. This is done
8285 under the assumption that the zero initializer in user
8286 code appears conditioned on e.g. __STDC__ to avoid
8287 "missing initializer" warnings and relies on default
8288 initialization to zero in the traditional C case.
8289 We also skip the warning if the initializer is designated,
8290 again on the assumption that this must be conditional on
8291 __STDC__ anyway (and we've already complained about the
8292 member-designator already). */
8293 if (!in_system_header && !constructor_designated
8294 && !(value.value && (integer_zerop (value.value)
8295 || real_zerop (value.value))))
8296 warning (OPT_Wtraditional, "traditional C rejects initialization "
8297 "of unions");
8299 /* Accept a string constant to initialize a subarray. */
8300 if (value.value != 0
8301 && fieldcode == ARRAY_TYPE
8302 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8303 && string_flag)
8304 value.value = orig_value;
8305 /* Otherwise, if we have come to a subaggregate,
8306 and we don't have an element of its type, push into it. */
8307 else if (value.value != 0
8308 && value.value != error_mark_node
8309 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8310 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8311 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8313 push_init_level (1, braced_init_obstack);
8314 continue;
8317 if (value.value)
8319 push_member_name (constructor_fields);
8320 output_init_element (value.value, value.original_type,
8321 strict_string, fieldtype,
8322 constructor_fields, 1, implicit,
8323 braced_init_obstack);
8324 RESTORE_SPELLING_DEPTH (constructor_depth);
8326 else
8327 /* Do the bookkeeping for an element that was
8328 directly output as a constructor. */
8330 constructor_bit_index = DECL_SIZE (constructor_fields);
8331 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8334 constructor_fields = 0;
8336 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8338 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8339 enum tree_code eltcode = TREE_CODE (elttype);
8341 /* Accept a string constant to initialize a subarray. */
8342 if (value.value != 0
8343 && eltcode == ARRAY_TYPE
8344 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8345 && string_flag)
8346 value.value = orig_value;
8347 /* Otherwise, if we have come to a subaggregate,
8348 and we don't have an element of its type, push into it. */
8349 else if (value.value != 0
8350 && value.value != error_mark_node
8351 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8352 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8353 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8355 push_init_level (1, braced_init_obstack);
8356 continue;
8359 if (constructor_max_index != 0
8360 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8361 || integer_all_onesp (constructor_max_index)))
8363 pedwarn_init (input_location, 0,
8364 "excess elements in array initializer");
8365 break;
8368 /* Now output the actual element. */
8369 if (value.value)
8371 push_array_bounds (tree_low_cst (constructor_index, 1));
8372 output_init_element (value.value, value.original_type,
8373 strict_string, elttype,
8374 constructor_index, 1, implicit,
8375 braced_init_obstack);
8376 RESTORE_SPELLING_DEPTH (constructor_depth);
8379 constructor_index
8380 = size_binop_loc (input_location, PLUS_EXPR,
8381 constructor_index, bitsize_one_node);
8383 if (!value.value)
8384 /* If we are doing the bookkeeping for an element that was
8385 directly output as a constructor, we must update
8386 constructor_unfilled_index. */
8387 constructor_unfilled_index = constructor_index;
8389 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8391 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8393 /* Do a basic check of initializer size. Note that vectors
8394 always have a fixed size derived from their type. */
8395 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8397 pedwarn_init (input_location, 0,
8398 "excess elements in vector initializer");
8399 break;
8402 /* Now output the actual element. */
8403 if (value.value)
8405 if (TREE_CODE (value.value) == VECTOR_CST)
8406 elttype = TYPE_MAIN_VARIANT (constructor_type);
8407 output_init_element (value.value, value.original_type,
8408 strict_string, elttype,
8409 constructor_index, 1, implicit,
8410 braced_init_obstack);
8413 constructor_index
8414 = size_binop_loc (input_location,
8415 PLUS_EXPR, constructor_index, bitsize_one_node);
8417 if (!value.value)
8418 /* If we are doing the bookkeeping for an element that was
8419 directly output as a constructor, we must update
8420 constructor_unfilled_index. */
8421 constructor_unfilled_index = constructor_index;
8424 /* Handle the sole element allowed in a braced initializer
8425 for a scalar variable. */
8426 else if (constructor_type != error_mark_node
8427 && constructor_fields == 0)
8429 pedwarn_init (input_location, 0,
8430 "excess elements in scalar initializer");
8431 break;
8433 else
8435 if (value.value)
8436 output_init_element (value.value, value.original_type,
8437 strict_string, constructor_type,
8438 NULL_TREE, 1, implicit,
8439 braced_init_obstack);
8440 constructor_fields = 0;
8443 /* Handle range initializers either at this level or anywhere higher
8444 in the designator stack. */
8445 if (constructor_range_stack)
8447 struct constructor_range_stack *p, *range_stack;
8448 int finish = 0;
8450 range_stack = constructor_range_stack;
8451 constructor_range_stack = 0;
8452 while (constructor_stack != range_stack->stack)
8454 gcc_assert (constructor_stack->implicit);
8455 process_init_element (pop_init_level (1,
8456 braced_init_obstack),
8457 true, braced_init_obstack);
8459 for (p = range_stack;
8460 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8461 p = p->prev)
8463 gcc_assert (constructor_stack->implicit);
8464 process_init_element (pop_init_level (1, braced_init_obstack),
8465 true, braced_init_obstack);
8468 p->index = size_binop_loc (input_location,
8469 PLUS_EXPR, p->index, bitsize_one_node);
8470 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8471 finish = 1;
8473 while (1)
8475 constructor_index = p->index;
8476 constructor_fields = p->fields;
8477 if (finish && p->range_end && p->index == p->range_start)
8479 finish = 0;
8480 p->prev = 0;
8482 p = p->next;
8483 if (!p)
8484 break;
8485 push_init_level (2, braced_init_obstack);
8486 p->stack = constructor_stack;
8487 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8488 p->index = p->range_start;
8491 if (!finish)
8492 constructor_range_stack = range_stack;
8493 continue;
8496 break;
8499 constructor_range_stack = 0;
8502 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8503 (guaranteed to be 'volatile' or null) and ARGS (represented using
8504 an ASM_EXPR node). */
8505 tree
8506 build_asm_stmt (tree cv_qualifier, tree args)
8508 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8509 ASM_VOLATILE_P (args) = 1;
8510 return add_stmt (args);
8513 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8514 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8515 SIMPLE indicates whether there was anything at all after the
8516 string in the asm expression -- asm("blah") and asm("blah" : )
8517 are subtly different. We use a ASM_EXPR node to represent this. */
8518 tree
8519 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8520 tree clobbers, tree labels, bool simple)
8522 tree tail;
8523 tree args;
8524 int i;
8525 const char *constraint;
8526 const char **oconstraints;
8527 bool allows_mem, allows_reg, is_inout;
8528 int ninputs, noutputs;
8530 ninputs = list_length (inputs);
8531 noutputs = list_length (outputs);
8532 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8534 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8536 /* Remove output conversions that change the type but not the mode. */
8537 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8539 tree output = TREE_VALUE (tail);
8541 output = c_fully_fold (output, false, NULL);
8543 /* ??? Really, this should not be here. Users should be using a
8544 proper lvalue, dammit. But there's a long history of using casts
8545 in the output operands. In cases like longlong.h, this becomes a
8546 primitive form of typechecking -- if the cast can be removed, then
8547 the output operand had a type of the proper width; otherwise we'll
8548 get an error. Gross, but ... */
8549 STRIP_NOPS (output);
8551 if (!lvalue_or_else (loc, output, lv_asm))
8552 output = error_mark_node;
8554 if (output != error_mark_node
8555 && (TREE_READONLY (output)
8556 || TYPE_READONLY (TREE_TYPE (output))
8557 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8558 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8559 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8560 readonly_error (output, lv_asm);
8562 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8563 oconstraints[i] = constraint;
8565 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8566 &allows_mem, &allows_reg, &is_inout))
8568 /* If the operand is going to end up in memory,
8569 mark it addressable. */
8570 if (!allows_reg && !c_mark_addressable (output))
8571 output = error_mark_node;
8572 if (!(!allows_reg && allows_mem)
8573 && output != error_mark_node
8574 && VOID_TYPE_P (TREE_TYPE (output)))
8576 error_at (loc, "invalid use of void expression");
8577 output = error_mark_node;
8580 else
8581 output = error_mark_node;
8583 TREE_VALUE (tail) = output;
8586 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8588 tree input;
8590 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8591 input = TREE_VALUE (tail);
8593 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8594 oconstraints, &allows_mem, &allows_reg))
8596 /* If the operand is going to end up in memory,
8597 mark it addressable. */
8598 if (!allows_reg && allows_mem)
8600 input = c_fully_fold (input, false, NULL);
8602 /* Strip the nops as we allow this case. FIXME, this really
8603 should be rejected or made deprecated. */
8604 STRIP_NOPS (input);
8605 if (!c_mark_addressable (input))
8606 input = error_mark_node;
8608 else
8610 struct c_expr expr;
8611 memset (&expr, 0, sizeof (expr));
8612 expr.value = input;
8613 expr = default_function_array_conversion (loc, expr);
8614 input = c_fully_fold (expr.value, false, NULL);
8616 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8618 error_at (loc, "invalid use of void expression");
8619 input = error_mark_node;
8623 else
8624 input = error_mark_node;
8626 TREE_VALUE (tail) = input;
8629 /* ASMs with labels cannot have outputs. This should have been
8630 enforced by the parser. */
8631 gcc_assert (outputs == NULL || labels == NULL);
8633 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8635 /* asm statements without outputs, including simple ones, are treated
8636 as volatile. */
8637 ASM_INPUT_P (args) = simple;
8638 ASM_VOLATILE_P (args) = (noutputs == 0);
8640 return args;
8643 /* Generate a goto statement to LABEL. LOC is the location of the
8644 GOTO. */
8646 tree
8647 c_finish_goto_label (location_t loc, tree label)
8649 tree decl = lookup_label_for_goto (loc, label);
8650 if (!decl)
8651 return NULL_TREE;
8652 TREE_USED (decl) = 1;
8654 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8655 SET_EXPR_LOCATION (t, loc);
8656 return add_stmt (t);
8660 /* Generate a computed goto statement to EXPR. LOC is the location of
8661 the GOTO. */
8663 tree
8664 c_finish_goto_ptr (location_t loc, tree expr)
8666 tree t;
8667 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
8668 expr = c_fully_fold (expr, false, NULL);
8669 expr = convert (ptr_type_node, expr);
8670 t = build1 (GOTO_EXPR, void_type_node, expr);
8671 SET_EXPR_LOCATION (t, loc);
8672 return add_stmt (t);
8675 /* Generate a C `return' statement. RETVAL is the expression for what
8676 to return, or a null pointer for `return;' with no value. LOC is
8677 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8678 is the original type of RETVAL. */
8680 tree
8681 c_finish_return (location_t loc, tree retval, tree origtype)
8683 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8684 bool no_warning = false;
8685 bool npc = false;
8686 size_t rank = 0;
8688 if (TREE_THIS_VOLATILE (current_function_decl))
8689 warning_at (loc, 0,
8690 "function declared %<noreturn%> has a %<return%> statement");
8692 if (flag_enable_cilkplus && contains_array_notation_expr (retval))
8694 /* Array notations are allowed in a return statement if it is inside a
8695 built-in array notation reduction function. */
8696 if (!find_rank (loc, retval, retval, false, &rank))
8697 return error_mark_node;
8698 if (rank >= 1)
8700 error_at (loc, "array notation expression cannot be used as a "
8701 "return value");
8702 return error_mark_node;
8705 if (flag_enable_cilkplus && retval && TREE_CODE (retval) == CILK_SPAWN_STMT)
8707 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
8708 "allowed");
8709 return error_mark_node;
8711 if (retval)
8713 tree semantic_type = NULL_TREE;
8714 npc = null_pointer_constant_p (retval);
8715 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8717 semantic_type = TREE_TYPE (retval);
8718 retval = TREE_OPERAND (retval, 0);
8720 retval = c_fully_fold (retval, false, NULL);
8721 if (semantic_type)
8722 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8725 if (!retval)
8727 current_function_returns_null = 1;
8728 if ((warn_return_type || flag_isoc99)
8729 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8731 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8732 "%<return%> with no value, in "
8733 "function returning non-void");
8734 no_warning = true;
8737 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8739 current_function_returns_null = 1;
8740 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8741 pedwarn (loc, 0,
8742 "%<return%> with a value, in function returning void");
8743 else
8744 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
8745 "%<return%> with expression, in function returning void");
8747 else
8749 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8750 ic_return,
8751 npc, NULL_TREE, NULL_TREE, 0);
8752 tree res = DECL_RESULT (current_function_decl);
8753 tree inner;
8754 bool save;
8756 current_function_returns_value = 1;
8757 if (t == error_mark_node)
8758 return NULL_TREE;
8760 save = in_late_binary_op;
8761 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
8762 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
8763 in_late_binary_op = true;
8764 inner = t = convert (TREE_TYPE (res), t);
8765 in_late_binary_op = save;
8767 /* Strip any conversions, additions, and subtractions, and see if
8768 we are returning the address of a local variable. Warn if so. */
8769 while (1)
8771 switch (TREE_CODE (inner))
8773 CASE_CONVERT:
8774 case NON_LVALUE_EXPR:
8775 case PLUS_EXPR:
8776 case POINTER_PLUS_EXPR:
8777 inner = TREE_OPERAND (inner, 0);
8778 continue;
8780 case MINUS_EXPR:
8781 /* If the second operand of the MINUS_EXPR has a pointer
8782 type (or is converted from it), this may be valid, so
8783 don't give a warning. */
8785 tree op1 = TREE_OPERAND (inner, 1);
8787 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8788 && (CONVERT_EXPR_P (op1)
8789 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8790 op1 = TREE_OPERAND (op1, 0);
8792 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8793 break;
8795 inner = TREE_OPERAND (inner, 0);
8796 continue;
8799 case ADDR_EXPR:
8800 inner = TREE_OPERAND (inner, 0);
8802 while (REFERENCE_CLASS_P (inner)
8803 && TREE_CODE (inner) != INDIRECT_REF)
8804 inner = TREE_OPERAND (inner, 0);
8806 if (DECL_P (inner)
8807 && !DECL_EXTERNAL (inner)
8808 && !TREE_STATIC (inner)
8809 && DECL_CONTEXT (inner) == current_function_decl)
8810 warning_at (loc,
8811 OPT_Wreturn_local_addr, "function returns address "
8812 "of local variable");
8813 break;
8815 default:
8816 break;
8819 break;
8822 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8823 SET_EXPR_LOCATION (retval, loc);
8825 if (warn_sequence_point)
8826 verify_sequence_points (retval);
8829 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8830 TREE_NO_WARNING (ret_stmt) |= no_warning;
8831 return add_stmt (ret_stmt);
8834 struct c_switch {
8835 /* The SWITCH_EXPR being built. */
8836 tree switch_expr;
8838 /* The original type of the testing expression, i.e. before the
8839 default conversion is applied. */
8840 tree orig_type;
8842 /* A splay-tree mapping the low element of a case range to the high
8843 element, or NULL_TREE if there is no high element. Used to
8844 determine whether or not a new case label duplicates an old case
8845 label. We need a tree, rather than simply a hash table, because
8846 of the GNU case range extension. */
8847 splay_tree cases;
8849 /* The bindings at the point of the switch. This is used for
8850 warnings crossing decls when branching to a case label. */
8851 struct c_spot_bindings *bindings;
8853 /* The next node on the stack. */
8854 struct c_switch *next;
8857 /* A stack of the currently active switch statements. The innermost
8858 switch statement is on the top of the stack. There is no need to
8859 mark the stack for garbage collection because it is only active
8860 during the processing of the body of a function, and we never
8861 collect at that point. */
8863 struct c_switch *c_switch_stack;
8865 /* Start a C switch statement, testing expression EXP. Return the new
8866 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8867 SWITCH_COND_LOC is the location of the switch's condition. */
8869 tree
8870 c_start_case (location_t switch_loc,
8871 location_t switch_cond_loc,
8872 tree exp)
8874 tree orig_type = error_mark_node;
8875 struct c_switch *cs;
8877 if (exp != error_mark_node)
8879 orig_type = TREE_TYPE (exp);
8881 if (!INTEGRAL_TYPE_P (orig_type))
8883 if (orig_type != error_mark_node)
8885 error_at (switch_cond_loc, "switch quantity not an integer");
8886 orig_type = error_mark_node;
8888 exp = integer_zero_node;
8890 else
8892 tree type = TYPE_MAIN_VARIANT (orig_type);
8894 if (!in_system_header
8895 && (type == long_integer_type_node
8896 || type == long_unsigned_type_node))
8897 warning_at (switch_cond_loc,
8898 OPT_Wtraditional, "%<long%> switch expression not "
8899 "converted to %<int%> in ISO C");
8901 exp = c_fully_fold (exp, false, NULL);
8902 exp = default_conversion (exp);
8904 if (warn_sequence_point)
8905 verify_sequence_points (exp);
8909 /* Add this new SWITCH_EXPR to the stack. */
8910 cs = XNEW (struct c_switch);
8911 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8912 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8913 cs->orig_type = orig_type;
8914 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8915 cs->bindings = c_get_switch_bindings ();
8916 cs->next = c_switch_stack;
8917 c_switch_stack = cs;
8919 return add_stmt (cs->switch_expr);
8922 /* Process a case label at location LOC. */
8924 tree
8925 do_case (location_t loc, tree low_value, tree high_value)
8927 tree label = NULL_TREE;
8929 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8931 low_value = c_fully_fold (low_value, false, NULL);
8932 if (TREE_CODE (low_value) == INTEGER_CST)
8933 pedwarn (input_location, OPT_Wpedantic,
8934 "case label is not an integer constant expression");
8937 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8939 high_value = c_fully_fold (high_value, false, NULL);
8940 if (TREE_CODE (high_value) == INTEGER_CST)
8941 pedwarn (input_location, OPT_Wpedantic,
8942 "case label is not an integer constant expression");
8945 if (c_switch_stack == NULL)
8947 if (low_value)
8948 error_at (loc, "case label not within a switch statement");
8949 else
8950 error_at (loc, "%<default%> label not within a switch statement");
8951 return NULL_TREE;
8954 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8955 EXPR_LOCATION (c_switch_stack->switch_expr),
8956 loc))
8957 return NULL_TREE;
8959 label = c_add_case_label (loc, c_switch_stack->cases,
8960 SWITCH_COND (c_switch_stack->switch_expr),
8961 c_switch_stack->orig_type,
8962 low_value, high_value);
8963 if (label == error_mark_node)
8964 label = NULL_TREE;
8965 return label;
8968 /* Finish the switch statement. */
8970 void
8971 c_finish_case (tree body)
8973 struct c_switch *cs = c_switch_stack;
8974 location_t switch_location;
8976 SWITCH_BODY (cs->switch_expr) = body;
8978 /* Emit warnings as needed. */
8979 switch_location = EXPR_LOCATION (cs->switch_expr);
8980 c_do_switch_warnings (cs->cases, switch_location,
8981 TREE_TYPE (cs->switch_expr),
8982 SWITCH_COND (cs->switch_expr));
8984 /* Pop the stack. */
8985 c_switch_stack = cs->next;
8986 splay_tree_delete (cs->cases);
8987 c_release_switch_bindings (cs->bindings);
8988 XDELETE (cs);
8991 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8992 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8993 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8994 statement, and was not surrounded with parenthesis. */
8996 void
8997 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8998 tree else_block, bool nested_if)
9000 tree stmt;
9002 /* If the condition has array notations, then the rank of the then_block and
9003 else_block must be either 0 or be equal to the rank of the condition. If
9004 the condition does not have array notations then break them up as it is
9005 broken up in a normal expression. */
9006 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
9008 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9009 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9010 return;
9011 if (then_block
9012 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9013 return;
9014 if (else_block
9015 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9016 return;
9017 if (cond_rank != then_rank && then_rank != 0)
9019 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9020 " and the then-block");
9021 return;
9023 else if (cond_rank != else_rank && else_rank != 0)
9025 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9026 " and the else-block");
9027 return;
9030 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9031 if (warn_parentheses && nested_if && else_block == NULL)
9033 tree inner_if = then_block;
9035 /* We know from the grammar productions that there is an IF nested
9036 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9037 it might not be exactly THEN_BLOCK, but should be the last
9038 non-container statement within. */
9039 while (1)
9040 switch (TREE_CODE (inner_if))
9042 case COND_EXPR:
9043 goto found;
9044 case BIND_EXPR:
9045 inner_if = BIND_EXPR_BODY (inner_if);
9046 break;
9047 case STATEMENT_LIST:
9048 inner_if = expr_last (then_block);
9049 break;
9050 case TRY_FINALLY_EXPR:
9051 case TRY_CATCH_EXPR:
9052 inner_if = TREE_OPERAND (inner_if, 0);
9053 break;
9054 default:
9055 gcc_unreachable ();
9057 found:
9059 if (COND_EXPR_ELSE (inner_if))
9060 warning_at (if_locus, OPT_Wparentheses,
9061 "suggest explicit braces to avoid ambiguous %<else%>");
9064 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9065 SET_EXPR_LOCATION (stmt, if_locus);
9066 add_stmt (stmt);
9069 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9070 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9071 is false for DO loops. INCR is the FOR increment expression. BODY is
9072 the statement controlled by the loop. BLAB is the break label. CLAB is
9073 the continue label. Everything is allowed to be NULL. */
9075 void
9076 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9077 tree blab, tree clab, bool cond_is_first)
9079 tree entry = NULL, exit = NULL, t;
9081 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
9083 error_at (start_locus, "array notation expression cannot be used in a "
9084 "loop%'s condition");
9085 return;
9088 /* If the condition is zero don't generate a loop construct. */
9089 if (cond && integer_zerop (cond))
9091 if (cond_is_first)
9093 t = build_and_jump (&blab);
9094 SET_EXPR_LOCATION (t, start_locus);
9095 add_stmt (t);
9098 else
9100 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9102 /* If we have an exit condition, then we build an IF with gotos either
9103 out of the loop, or to the top of it. If there's no exit condition,
9104 then we just build a jump back to the top. */
9105 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9107 if (cond && !integer_nonzerop (cond))
9109 /* Canonicalize the loop condition to the end. This means
9110 generating a branch to the loop condition. Reuse the
9111 continue label, if possible. */
9112 if (cond_is_first)
9114 if (incr || !clab)
9116 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9117 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9119 else
9120 t = build1 (GOTO_EXPR, void_type_node, clab);
9121 SET_EXPR_LOCATION (t, start_locus);
9122 add_stmt (t);
9125 t = build_and_jump (&blab);
9126 if (cond_is_first)
9127 exit = fold_build3_loc (start_locus,
9128 COND_EXPR, void_type_node, cond, exit, t);
9129 else
9130 exit = fold_build3_loc (input_location,
9131 COND_EXPR, void_type_node, cond, exit, t);
9134 add_stmt (top);
9137 if (body)
9138 add_stmt (body);
9139 if (clab)
9140 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9141 if (incr)
9142 add_stmt (incr);
9143 if (entry)
9144 add_stmt (entry);
9145 if (exit)
9146 add_stmt (exit);
9147 if (blab)
9148 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9151 tree
9152 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9154 bool skip;
9155 tree label = *label_p;
9157 /* In switch statements break is sometimes stylistically used after
9158 a return statement. This can lead to spurious warnings about
9159 control reaching the end of a non-void function when it is
9160 inlined. Note that we are calling block_may_fallthru with
9161 language specific tree nodes; this works because
9162 block_may_fallthru returns true when given something it does not
9163 understand. */
9164 skip = !block_may_fallthru (cur_stmt_list);
9166 if (!label)
9168 if (!skip)
9169 *label_p = label = create_artificial_label (loc);
9171 else if (TREE_CODE (label) == LABEL_DECL)
9173 else switch (TREE_INT_CST_LOW (label))
9175 case 0:
9176 if (is_break)
9177 error_at (loc, "break statement not within loop or switch");
9178 else
9179 error_at (loc, "continue statement not within a loop");
9180 return NULL_TREE;
9182 case 1:
9183 gcc_assert (is_break);
9184 error_at (loc, "break statement used with OpenMP for loop");
9185 return NULL_TREE;
9187 default:
9188 gcc_unreachable ();
9191 if (skip)
9192 return NULL_TREE;
9194 if (!is_break)
9195 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9197 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9200 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9202 static void
9203 emit_side_effect_warnings (location_t loc, tree expr)
9205 if (expr == error_mark_node)
9207 else if (!TREE_SIDE_EFFECTS (expr))
9209 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9210 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9212 else
9213 warn_if_unused_value (expr, loc);
9216 /* Process an expression as if it were a complete statement. Emit
9217 diagnostics, but do not call ADD_STMT. LOC is the location of the
9218 statement. */
9220 tree
9221 c_process_expr_stmt (location_t loc, tree expr)
9223 tree exprv;
9225 if (!expr)
9226 return NULL_TREE;
9228 expr = c_fully_fold (expr, false, NULL);
9230 if (warn_sequence_point)
9231 verify_sequence_points (expr);
9233 if (TREE_TYPE (expr) != error_mark_node
9234 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9235 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9236 error_at (loc, "expression statement has incomplete type");
9238 /* If we're not processing a statement expression, warn about unused values.
9239 Warnings for statement expressions will be emitted later, once we figure
9240 out which is the result. */
9241 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9242 && warn_unused_value)
9243 emit_side_effect_warnings (loc, expr);
9245 exprv = expr;
9246 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9247 exprv = TREE_OPERAND (exprv, 1);
9248 while (CONVERT_EXPR_P (exprv))
9249 exprv = TREE_OPERAND (exprv, 0);
9250 if (DECL_P (exprv)
9251 || handled_component_p (exprv)
9252 || TREE_CODE (exprv) == ADDR_EXPR)
9253 mark_exp_read (exprv);
9255 /* If the expression is not of a type to which we cannot assign a line
9256 number, wrap the thing in a no-op NOP_EXPR. */
9257 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9259 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9260 SET_EXPR_LOCATION (expr, loc);
9263 return expr;
9266 /* Emit an expression as a statement. LOC is the location of the
9267 expression. */
9269 tree
9270 c_finish_expr_stmt (location_t loc, tree expr)
9272 if (expr)
9273 return add_stmt (c_process_expr_stmt (loc, expr));
9274 else
9275 return NULL;
9278 /* Do the opposite and emit a statement as an expression. To begin,
9279 create a new binding level and return it. */
9281 tree
9282 c_begin_stmt_expr (void)
9284 tree ret;
9286 /* We must force a BLOCK for this level so that, if it is not expanded
9287 later, there is a way to turn off the entire subtree of blocks that
9288 are contained in it. */
9289 keep_next_level ();
9290 ret = c_begin_compound_stmt (true);
9292 c_bindings_start_stmt_expr (c_switch_stack == NULL
9293 ? NULL
9294 : c_switch_stack->bindings);
9296 /* Mark the current statement list as belonging to a statement list. */
9297 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9299 return ret;
9302 /* LOC is the location of the compound statement to which this body
9303 belongs. */
9305 tree
9306 c_finish_stmt_expr (location_t loc, tree body)
9308 tree last, type, tmp, val;
9309 tree *last_p;
9311 body = c_end_compound_stmt (loc, body, true);
9313 c_bindings_end_stmt_expr (c_switch_stack == NULL
9314 ? NULL
9315 : c_switch_stack->bindings);
9317 /* Locate the last statement in BODY. See c_end_compound_stmt
9318 about always returning a BIND_EXPR. */
9319 last_p = &BIND_EXPR_BODY (body);
9320 last = BIND_EXPR_BODY (body);
9322 continue_searching:
9323 if (TREE_CODE (last) == STATEMENT_LIST)
9325 tree_stmt_iterator i;
9327 /* This can happen with degenerate cases like ({ }). No value. */
9328 if (!TREE_SIDE_EFFECTS (last))
9329 return body;
9331 /* If we're supposed to generate side effects warnings, process
9332 all of the statements except the last. */
9333 if (warn_unused_value)
9335 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9337 location_t tloc;
9338 tree t = tsi_stmt (i);
9340 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9341 emit_side_effect_warnings (tloc, t);
9344 else
9345 i = tsi_last (last);
9346 last_p = tsi_stmt_ptr (i);
9347 last = *last_p;
9350 /* If the end of the list is exception related, then the list was split
9351 by a call to push_cleanup. Continue searching. */
9352 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9353 || TREE_CODE (last) == TRY_CATCH_EXPR)
9355 last_p = &TREE_OPERAND (last, 0);
9356 last = *last_p;
9357 goto continue_searching;
9360 if (last == error_mark_node)
9361 return last;
9363 /* In the case that the BIND_EXPR is not necessary, return the
9364 expression out from inside it. */
9365 if (last == BIND_EXPR_BODY (body)
9366 && BIND_EXPR_VARS (body) == NULL)
9368 /* Even if this looks constant, do not allow it in a constant
9369 expression. */
9370 last = c_wrap_maybe_const (last, true);
9371 /* Do not warn if the return value of a statement expression is
9372 unused. */
9373 TREE_NO_WARNING (last) = 1;
9374 return last;
9377 /* Extract the type of said expression. */
9378 type = TREE_TYPE (last);
9380 /* If we're not returning a value at all, then the BIND_EXPR that
9381 we already have is a fine expression to return. */
9382 if (!type || VOID_TYPE_P (type))
9383 return body;
9385 /* Now that we've located the expression containing the value, it seems
9386 silly to make voidify_wrapper_expr repeat the process. Create a
9387 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9388 tmp = create_tmp_var_raw (type, NULL);
9390 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9391 tree_expr_nonnegative_p giving up immediately. */
9392 val = last;
9393 if (TREE_CODE (val) == NOP_EXPR
9394 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9395 val = TREE_OPERAND (val, 0);
9397 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9398 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9401 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9402 SET_EXPR_LOCATION (t, loc);
9403 return t;
9407 /* Begin and end compound statements. This is as simple as pushing
9408 and popping new statement lists from the tree. */
9410 tree
9411 c_begin_compound_stmt (bool do_scope)
9413 tree stmt = push_stmt_list ();
9414 if (do_scope)
9415 push_scope ();
9416 return stmt;
9419 /* End a compound statement. STMT is the statement. LOC is the
9420 location of the compound statement-- this is usually the location
9421 of the opening brace. */
9423 tree
9424 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9426 tree block = NULL;
9428 if (do_scope)
9430 if (c_dialect_objc ())
9431 objc_clear_super_receiver ();
9432 block = pop_scope ();
9435 stmt = pop_stmt_list (stmt);
9436 stmt = c_build_bind_expr (loc, block, stmt);
9438 /* If this compound statement is nested immediately inside a statement
9439 expression, then force a BIND_EXPR to be created. Otherwise we'll
9440 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9441 STATEMENT_LISTs merge, and thus we can lose track of what statement
9442 was really last. */
9443 if (building_stmt_list_p ()
9444 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9445 && TREE_CODE (stmt) != BIND_EXPR)
9447 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9448 TREE_SIDE_EFFECTS (stmt) = 1;
9449 SET_EXPR_LOCATION (stmt, loc);
9452 return stmt;
9455 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9456 when the current scope is exited. EH_ONLY is true when this is not
9457 meant to apply to normal control flow transfer. */
9459 void
9460 push_cleanup (tree decl, tree cleanup, bool eh_only)
9462 enum tree_code code;
9463 tree stmt, list;
9464 bool stmt_expr;
9466 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9467 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9468 add_stmt (stmt);
9469 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9470 list = push_stmt_list ();
9471 TREE_OPERAND (stmt, 0) = list;
9472 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9475 /* Build a binary-operation expression without default conversions.
9476 CODE is the kind of expression to build.
9477 LOCATION is the operator's location.
9478 This function differs from `build' in several ways:
9479 the data type of the result is computed and recorded in it,
9480 warnings are generated if arg data types are invalid,
9481 special handling for addition and subtraction of pointers is known,
9482 and some optimization is done (operations on narrow ints
9483 are done in the narrower type when that gives the same result).
9484 Constant folding is also done before the result is returned.
9486 Note that the operands will never have enumeral types, or function
9487 or array types, because either they will have the default conversions
9488 performed or they have both just been converted to some other type in which
9489 the arithmetic is to be done. */
9491 tree
9492 build_binary_op (location_t location, enum tree_code code,
9493 tree orig_op0, tree orig_op1, int convert_p)
9495 tree type0, type1, orig_type0, orig_type1;
9496 tree eptype;
9497 enum tree_code code0, code1;
9498 tree op0, op1;
9499 tree ret = error_mark_node;
9500 const char *invalid_op_diag;
9501 bool op0_int_operands, op1_int_operands;
9502 bool int_const, int_const_or_overflow, int_operands;
9504 /* Expression code to give to the expression when it is built.
9505 Normally this is CODE, which is what the caller asked for,
9506 but in some special cases we change it. */
9507 enum tree_code resultcode = code;
9509 /* Data type in which the computation is to be performed.
9510 In the simplest cases this is the common type of the arguments. */
9511 tree result_type = NULL;
9513 /* When the computation is in excess precision, the type of the
9514 final EXCESS_PRECISION_EXPR. */
9515 tree semantic_result_type = NULL;
9517 /* Nonzero means operands have already been type-converted
9518 in whatever way is necessary.
9519 Zero means they need to be converted to RESULT_TYPE. */
9520 int converted = 0;
9522 /* Nonzero means create the expression with this type, rather than
9523 RESULT_TYPE. */
9524 tree build_type = 0;
9526 /* Nonzero means after finally constructing the expression
9527 convert it to this type. */
9528 tree final_type = 0;
9530 /* Nonzero if this is an operation like MIN or MAX which can
9531 safely be computed in short if both args are promoted shorts.
9532 Also implies COMMON.
9533 -1 indicates a bitwise operation; this makes a difference
9534 in the exact conditions for when it is safe to do the operation
9535 in a narrower mode. */
9536 int shorten = 0;
9538 /* Nonzero if this is a comparison operation;
9539 if both args are promoted shorts, compare the original shorts.
9540 Also implies COMMON. */
9541 int short_compare = 0;
9543 /* Nonzero if this is a right-shift operation, which can be computed on the
9544 original short and then promoted if the operand is a promoted short. */
9545 int short_shift = 0;
9547 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9548 int common = 0;
9550 /* True means types are compatible as far as ObjC is concerned. */
9551 bool objc_ok;
9553 /* True means this is an arithmetic operation that may need excess
9554 precision. */
9555 bool may_need_excess_precision;
9557 /* True means this is a boolean operation that converts both its
9558 operands to truth-values. */
9559 bool boolean_op = false;
9561 /* Remember whether we're doing / or %. */
9562 bool doing_div_or_mod = false;
9564 /* Remember whether we're doing << or >>. */
9565 bool doing_shift = false;
9567 /* Tree holding instrumentation expression. */
9568 tree instrument_expr = NULL;
9570 if (location == UNKNOWN_LOCATION)
9571 location = input_location;
9573 op0 = orig_op0;
9574 op1 = orig_op1;
9576 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9577 if (op0_int_operands)
9578 op0 = remove_c_maybe_const_expr (op0);
9579 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9580 if (op1_int_operands)
9581 op1 = remove_c_maybe_const_expr (op1);
9582 int_operands = (op0_int_operands && op1_int_operands);
9583 if (int_operands)
9585 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9586 && TREE_CODE (orig_op1) == INTEGER_CST);
9587 int_const = (int_const_or_overflow
9588 && !TREE_OVERFLOW (orig_op0)
9589 && !TREE_OVERFLOW (orig_op1));
9591 else
9592 int_const = int_const_or_overflow = false;
9594 /* Do not apply default conversion in mixed vector/scalar expression. */
9595 if (convert_p
9596 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
9597 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
9599 op0 = default_conversion (op0);
9600 op1 = default_conversion (op1);
9603 /* When Cilk Plus is enabled and there are array notations inside op0, then
9604 we check to see if there are builtin array notation functions. If
9605 so, then we take on the type of the array notation inside it. */
9606 if (flag_enable_cilkplus && contains_array_notation_expr (op0))
9607 orig_type0 = type0 = find_correct_array_notation_type (op0);
9608 else
9609 orig_type0 = type0 = TREE_TYPE (op0);
9611 if (flag_enable_cilkplus && contains_array_notation_expr (op1))
9612 orig_type1 = type1 = find_correct_array_notation_type (op1);
9613 else
9614 orig_type1 = type1 = TREE_TYPE (op1);
9616 /* The expression codes of the data types of the arguments tell us
9617 whether the arguments are integers, floating, pointers, etc. */
9618 code0 = TREE_CODE (type0);
9619 code1 = TREE_CODE (type1);
9621 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9622 STRIP_TYPE_NOPS (op0);
9623 STRIP_TYPE_NOPS (op1);
9625 /* If an error was already reported for one of the arguments,
9626 avoid reporting another error. */
9628 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9629 return error_mark_node;
9631 if ((invalid_op_diag
9632 = targetm.invalid_binary_op (code, type0, type1)))
9634 error_at (location, invalid_op_diag);
9635 return error_mark_node;
9638 switch (code)
9640 case PLUS_EXPR:
9641 case MINUS_EXPR:
9642 case MULT_EXPR:
9643 case TRUNC_DIV_EXPR:
9644 case CEIL_DIV_EXPR:
9645 case FLOOR_DIV_EXPR:
9646 case ROUND_DIV_EXPR:
9647 case EXACT_DIV_EXPR:
9648 may_need_excess_precision = true;
9649 break;
9650 default:
9651 may_need_excess_precision = false;
9652 break;
9654 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9656 op0 = TREE_OPERAND (op0, 0);
9657 type0 = TREE_TYPE (op0);
9659 else if (may_need_excess_precision
9660 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9662 type0 = eptype;
9663 op0 = convert (eptype, op0);
9665 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9667 op1 = TREE_OPERAND (op1, 0);
9668 type1 = TREE_TYPE (op1);
9670 else if (may_need_excess_precision
9671 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9673 type1 = eptype;
9674 op1 = convert (eptype, op1);
9677 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9679 /* In case when one of the operands of the binary operation is
9680 a vector and another is a scalar -- convert scalar to vector. */
9681 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
9683 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
9684 true);
9686 switch (convert_flag)
9688 case stv_error:
9689 return error_mark_node;
9690 case stv_firstarg:
9692 bool maybe_const = true;
9693 tree sc;
9694 sc = c_fully_fold (op0, false, &maybe_const);
9695 sc = save_expr (sc);
9696 sc = convert (TREE_TYPE (type1), sc);
9697 op0 = build_vector_from_val (type1, sc);
9698 if (!maybe_const)
9699 op0 = c_wrap_maybe_const (op0, true);
9700 orig_type0 = type0 = TREE_TYPE (op0);
9701 code0 = TREE_CODE (type0);
9702 converted = 1;
9703 break;
9705 case stv_secondarg:
9707 bool maybe_const = true;
9708 tree sc;
9709 sc = c_fully_fold (op1, false, &maybe_const);
9710 sc = save_expr (sc);
9711 sc = convert (TREE_TYPE (type0), sc);
9712 op1 = build_vector_from_val (type0, sc);
9713 if (!maybe_const)
9714 op1 = c_wrap_maybe_const (op1, true);
9715 orig_type1 = type1 = TREE_TYPE (op1);
9716 code1 = TREE_CODE (type1);
9717 converted = 1;
9718 break;
9720 default:
9721 break;
9725 switch (code)
9727 case PLUS_EXPR:
9728 /* Handle the pointer + int case. */
9729 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9731 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9732 goto return_build_binary_op;
9734 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9736 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9737 goto return_build_binary_op;
9739 else
9740 common = 1;
9741 break;
9743 case MINUS_EXPR:
9744 /* Subtraction of two similar pointers.
9745 We must subtract them as integers, then divide by object size. */
9746 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9747 && comp_target_types (location, type0, type1))
9749 ret = pointer_diff (location, op0, op1);
9750 goto return_build_binary_op;
9752 /* Handle pointer minus int. Just like pointer plus int. */
9753 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9755 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9756 goto return_build_binary_op;
9758 else
9759 common = 1;
9760 break;
9762 case MULT_EXPR:
9763 common = 1;
9764 break;
9766 case TRUNC_DIV_EXPR:
9767 case CEIL_DIV_EXPR:
9768 case FLOOR_DIV_EXPR:
9769 case ROUND_DIV_EXPR:
9770 case EXACT_DIV_EXPR:
9771 doing_div_or_mod = true;
9772 warn_for_div_by_zero (location, op1);
9774 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9775 || code0 == FIXED_POINT_TYPE
9776 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9777 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9778 || code1 == FIXED_POINT_TYPE
9779 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9781 enum tree_code tcode0 = code0, tcode1 = code1;
9783 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9784 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9785 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9786 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9788 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9789 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9790 resultcode = RDIV_EXPR;
9791 else
9792 /* Although it would be tempting to shorten always here, that
9793 loses on some targets, since the modulo instruction is
9794 undefined if the quotient can't be represented in the
9795 computation mode. We shorten only if unsigned or if
9796 dividing by something we know != -1. */
9797 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9798 || (TREE_CODE (op1) == INTEGER_CST
9799 && !integer_all_onesp (op1)));
9800 common = 1;
9802 break;
9804 case BIT_AND_EXPR:
9805 case BIT_IOR_EXPR:
9806 case BIT_XOR_EXPR:
9807 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9808 shorten = -1;
9809 /* Allow vector types which are not floating point types. */
9810 else if (code0 == VECTOR_TYPE
9811 && code1 == VECTOR_TYPE
9812 && !VECTOR_FLOAT_TYPE_P (type0)
9813 && !VECTOR_FLOAT_TYPE_P (type1))
9814 common = 1;
9815 break;
9817 case TRUNC_MOD_EXPR:
9818 case FLOOR_MOD_EXPR:
9819 doing_div_or_mod = true;
9820 warn_for_div_by_zero (location, op1);
9822 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9823 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9824 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9825 common = 1;
9826 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9828 /* Although it would be tempting to shorten always here, that loses
9829 on some targets, since the modulo instruction is undefined if the
9830 quotient can't be represented in the computation mode. We shorten
9831 only if unsigned or if dividing by something we know != -1. */
9832 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9833 || (TREE_CODE (op1) == INTEGER_CST
9834 && !integer_all_onesp (op1)));
9835 common = 1;
9837 break;
9839 case TRUTH_ANDIF_EXPR:
9840 case TRUTH_ORIF_EXPR:
9841 case TRUTH_AND_EXPR:
9842 case TRUTH_OR_EXPR:
9843 case TRUTH_XOR_EXPR:
9844 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9845 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9846 || code0 == FIXED_POINT_TYPE)
9847 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9848 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9849 || code1 == FIXED_POINT_TYPE))
9851 /* Result of these operations is always an int,
9852 but that does not mean the operands should be
9853 converted to ints! */
9854 result_type = integer_type_node;
9855 if (op0_int_operands)
9857 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
9858 op0 = remove_c_maybe_const_expr (op0);
9860 else
9861 op0 = c_objc_common_truthvalue_conversion (location, op0);
9862 if (op1_int_operands)
9864 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
9865 op1 = remove_c_maybe_const_expr (op1);
9867 else
9868 op1 = c_objc_common_truthvalue_conversion (location, op1);
9869 converted = 1;
9870 boolean_op = true;
9872 if (code == TRUTH_ANDIF_EXPR)
9874 int_const_or_overflow = (int_operands
9875 && TREE_CODE (orig_op0) == INTEGER_CST
9876 && (op0 == truthvalue_false_node
9877 || TREE_CODE (orig_op1) == INTEGER_CST));
9878 int_const = (int_const_or_overflow
9879 && !TREE_OVERFLOW (orig_op0)
9880 && (op0 == truthvalue_false_node
9881 || !TREE_OVERFLOW (orig_op1)));
9883 else if (code == TRUTH_ORIF_EXPR)
9885 int_const_or_overflow = (int_operands
9886 && TREE_CODE (orig_op0) == INTEGER_CST
9887 && (op0 == truthvalue_true_node
9888 || TREE_CODE (orig_op1) == INTEGER_CST));
9889 int_const = (int_const_or_overflow
9890 && !TREE_OVERFLOW (orig_op0)
9891 && (op0 == truthvalue_true_node
9892 || !TREE_OVERFLOW (orig_op1)));
9894 break;
9896 /* Shift operations: result has same type as first operand;
9897 always convert second operand to int.
9898 Also set SHORT_SHIFT if shifting rightward. */
9900 case RSHIFT_EXPR:
9901 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9902 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9904 result_type = type0;
9905 converted = 1;
9907 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9908 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9909 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9910 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9912 result_type = type0;
9913 converted = 1;
9915 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9916 && code1 == INTEGER_TYPE)
9918 doing_shift = true;
9919 if (TREE_CODE (op1) == INTEGER_CST)
9921 if (tree_int_cst_sgn (op1) < 0)
9923 int_const = false;
9924 if (c_inhibit_evaluation_warnings == 0)
9925 warning (0, "right shift count is negative");
9927 else
9929 if (!integer_zerop (op1))
9930 short_shift = 1;
9932 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9934 int_const = false;
9935 if (c_inhibit_evaluation_warnings == 0)
9936 warning (0, "right shift count >= width of type");
9941 /* Use the type of the value to be shifted. */
9942 result_type = type0;
9943 /* Convert the non vector shift-count to an integer, regardless
9944 of size of value being shifted. */
9945 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9946 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9947 op1 = convert (integer_type_node, op1);
9948 /* Avoid converting op1 to result_type later. */
9949 converted = 1;
9951 break;
9953 case LSHIFT_EXPR:
9954 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9955 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9957 result_type = type0;
9958 converted = 1;
9960 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9961 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9962 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9963 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9965 result_type = type0;
9966 converted = 1;
9968 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9969 && code1 == INTEGER_TYPE)
9971 doing_shift = true;
9972 if (TREE_CODE (op1) == INTEGER_CST)
9974 if (tree_int_cst_sgn (op1) < 0)
9976 int_const = false;
9977 if (c_inhibit_evaluation_warnings == 0)
9978 warning (0, "left shift count is negative");
9981 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9983 int_const = false;
9984 if (c_inhibit_evaluation_warnings == 0)
9985 warning (0, "left shift count >= width of type");
9989 /* Use the type of the value to be shifted. */
9990 result_type = type0;
9991 /* Convert the non vector shift-count to an integer, regardless
9992 of size of value being shifted. */
9993 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9994 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9995 op1 = convert (integer_type_node, op1);
9996 /* Avoid converting op1 to result_type later. */
9997 converted = 1;
9999 break;
10001 case EQ_EXPR:
10002 case NE_EXPR:
10003 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10005 tree intt;
10006 if (!vector_types_compatible_elements_p (type0, type1))
10008 error_at (location, "comparing vectors with different "
10009 "element types");
10010 return error_mark_node;
10013 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10015 error_at (location, "comparing vectors with different "
10016 "number of elements");
10017 return error_mark_node;
10020 /* Always construct signed integer vector type. */
10021 intt = c_common_type_for_size (GET_MODE_BITSIZE
10022 (TYPE_MODE (TREE_TYPE (type0))), 0);
10023 result_type = build_opaque_vector_type (intt,
10024 TYPE_VECTOR_SUBPARTS (type0));
10025 converted = 1;
10026 break;
10028 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10029 warning_at (location,
10030 OPT_Wfloat_equal,
10031 "comparing floating point with == or != is unsafe");
10032 /* Result of comparison is always int,
10033 but don't convert the args to int! */
10034 build_type = integer_type_node;
10035 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10036 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10037 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10038 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10039 short_compare = 1;
10040 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10042 if (TREE_CODE (op0) == ADDR_EXPR
10043 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10045 if (code == EQ_EXPR)
10046 warning_at (location,
10047 OPT_Waddress,
10048 "the comparison will always evaluate as %<false%> "
10049 "for the address of %qD will never be NULL",
10050 TREE_OPERAND (op0, 0));
10051 else
10052 warning_at (location,
10053 OPT_Waddress,
10054 "the comparison will always evaluate as %<true%> "
10055 "for the address of %qD will never be NULL",
10056 TREE_OPERAND (op0, 0));
10058 result_type = type0;
10060 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10062 if (TREE_CODE (op1) == ADDR_EXPR
10063 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10065 if (code == EQ_EXPR)
10066 warning_at (location,
10067 OPT_Waddress,
10068 "the comparison will always evaluate as %<false%> "
10069 "for the address of %qD will never be NULL",
10070 TREE_OPERAND (op1, 0));
10071 else
10072 warning_at (location,
10073 OPT_Waddress,
10074 "the comparison will always evaluate as %<true%> "
10075 "for the address of %qD will never be NULL",
10076 TREE_OPERAND (op1, 0));
10078 result_type = type1;
10080 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10082 tree tt0 = TREE_TYPE (type0);
10083 tree tt1 = TREE_TYPE (type1);
10084 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10085 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10086 addr_space_t as_common = ADDR_SPACE_GENERIC;
10088 /* Anything compares with void *. void * compares with anything.
10089 Otherwise, the targets must be compatible
10090 and both must be object or both incomplete. */
10091 if (comp_target_types (location, type0, type1))
10092 result_type = common_pointer_type (type0, type1);
10093 else if (!addr_space_superset (as0, as1, &as_common))
10095 error_at (location, "comparison of pointers to "
10096 "disjoint address spaces");
10097 return error_mark_node;
10099 else if (VOID_TYPE_P (tt0))
10101 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10102 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10103 "comparison of %<void *%> with function pointer");
10105 else if (VOID_TYPE_P (tt1))
10107 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10108 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10109 "comparison of %<void *%> with function pointer");
10111 else
10112 /* Avoid warning about the volatile ObjC EH puts on decls. */
10113 if (!objc_ok)
10114 pedwarn (location, 0,
10115 "comparison of distinct pointer types lacks a cast");
10117 if (result_type == NULL_TREE)
10119 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10120 result_type = build_pointer_type
10121 (build_qualified_type (void_type_node, qual));
10124 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10126 result_type = type0;
10127 pedwarn (location, 0, "comparison between pointer and integer");
10129 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10131 result_type = type1;
10132 pedwarn (location, 0, "comparison between pointer and integer");
10134 break;
10136 case LE_EXPR:
10137 case GE_EXPR:
10138 case LT_EXPR:
10139 case GT_EXPR:
10140 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10142 tree intt;
10143 if (!vector_types_compatible_elements_p (type0, type1))
10145 error_at (location, "comparing vectors with different "
10146 "element types");
10147 return error_mark_node;
10150 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10152 error_at (location, "comparing vectors with different "
10153 "number of elements");
10154 return error_mark_node;
10157 /* Always construct signed integer vector type. */
10158 intt = c_common_type_for_size (GET_MODE_BITSIZE
10159 (TYPE_MODE (TREE_TYPE (type0))), 0);
10160 result_type = build_opaque_vector_type (intt,
10161 TYPE_VECTOR_SUBPARTS (type0));
10162 converted = 1;
10163 break;
10165 build_type = integer_type_node;
10166 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10167 || code0 == FIXED_POINT_TYPE)
10168 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10169 || code1 == FIXED_POINT_TYPE))
10170 short_compare = 1;
10171 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10173 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10174 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10175 addr_space_t as_common;
10177 if (comp_target_types (location, type0, type1))
10179 result_type = common_pointer_type (type0, type1);
10180 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10181 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10182 pedwarn (location, 0,
10183 "comparison of complete and incomplete pointers");
10184 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10185 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10186 "ordered comparisons of pointers to functions");
10187 else if (null_pointer_constant_p (orig_op0)
10188 || null_pointer_constant_p (orig_op1))
10189 warning_at (location, OPT_Wextra,
10190 "ordered comparison of pointer with null pointer");
10193 else if (!addr_space_superset (as0, as1, &as_common))
10195 error_at (location, "comparison of pointers to "
10196 "disjoint address spaces");
10197 return error_mark_node;
10199 else
10201 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10202 result_type = build_pointer_type
10203 (build_qualified_type (void_type_node, qual));
10204 pedwarn (location, 0,
10205 "comparison of distinct pointer types lacks a cast");
10208 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10210 result_type = type0;
10211 if (pedantic)
10212 pedwarn (location, OPT_Wpedantic,
10213 "ordered comparison of pointer with integer zero");
10214 else if (extra_warnings)
10215 warning_at (location, OPT_Wextra,
10216 "ordered comparison of pointer with integer zero");
10218 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10220 result_type = type1;
10221 if (pedantic)
10222 pedwarn (location, OPT_Wpedantic,
10223 "ordered comparison of pointer with integer zero");
10224 else if (extra_warnings)
10225 warning_at (location, OPT_Wextra,
10226 "ordered comparison of pointer with integer zero");
10228 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10230 result_type = type0;
10231 pedwarn (location, 0, "comparison between pointer and integer");
10233 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10235 result_type = type1;
10236 pedwarn (location, 0, "comparison between pointer and integer");
10238 break;
10240 default:
10241 gcc_unreachable ();
10244 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10245 return error_mark_node;
10247 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10248 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10249 || !vector_types_compatible_elements_p (type0, type1)))
10251 binary_op_error (location, code, type0, type1);
10252 return error_mark_node;
10255 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10256 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10258 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10259 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10261 bool first_complex = (code0 == COMPLEX_TYPE);
10262 bool second_complex = (code1 == COMPLEX_TYPE);
10263 int none_complex = (!first_complex && !second_complex);
10265 if (shorten || common || short_compare)
10267 result_type = c_common_type (type0, type1);
10268 do_warn_double_promotion (result_type, type0, type1,
10269 "implicit conversion from %qT to %qT "
10270 "to match other operand of binary "
10271 "expression",
10272 location);
10273 if (result_type == error_mark_node)
10274 return error_mark_node;
10277 if (first_complex != second_complex
10278 && (code == PLUS_EXPR
10279 || code == MINUS_EXPR
10280 || code == MULT_EXPR
10281 || (code == TRUNC_DIV_EXPR && first_complex))
10282 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10283 && flag_signed_zeros)
10285 /* An operation on mixed real/complex operands must be
10286 handled specially, but the language-independent code can
10287 more easily optimize the plain complex arithmetic if
10288 -fno-signed-zeros. */
10289 tree real_type = TREE_TYPE (result_type);
10290 tree real, imag;
10291 if (type0 != orig_type0 || type1 != orig_type1)
10293 gcc_assert (may_need_excess_precision && common);
10294 semantic_result_type = c_common_type (orig_type0, orig_type1);
10296 if (first_complex)
10298 if (TREE_TYPE (op0) != result_type)
10299 op0 = convert_and_check (result_type, op0);
10300 if (TREE_TYPE (op1) != real_type)
10301 op1 = convert_and_check (real_type, op1);
10303 else
10305 if (TREE_TYPE (op0) != real_type)
10306 op0 = convert_and_check (real_type, op0);
10307 if (TREE_TYPE (op1) != result_type)
10308 op1 = convert_and_check (result_type, op1);
10310 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10311 return error_mark_node;
10312 if (first_complex)
10314 op0 = c_save_expr (op0);
10315 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10316 op0, 1);
10317 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10318 op0, 1);
10319 switch (code)
10321 case MULT_EXPR:
10322 case TRUNC_DIV_EXPR:
10323 op1 = c_save_expr (op1);
10324 imag = build2 (resultcode, real_type, imag, op1);
10325 /* Fall through. */
10326 case PLUS_EXPR:
10327 case MINUS_EXPR:
10328 real = build2 (resultcode, real_type, real, op1);
10329 break;
10330 default:
10331 gcc_unreachable();
10334 else
10336 op1 = c_save_expr (op1);
10337 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10338 op1, 1);
10339 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10340 op1, 1);
10341 switch (code)
10343 case MULT_EXPR:
10344 op0 = c_save_expr (op0);
10345 imag = build2 (resultcode, real_type, op0, imag);
10346 /* Fall through. */
10347 case PLUS_EXPR:
10348 real = build2 (resultcode, real_type, op0, real);
10349 break;
10350 case MINUS_EXPR:
10351 real = build2 (resultcode, real_type, op0, real);
10352 imag = build1 (NEGATE_EXPR, real_type, imag);
10353 break;
10354 default:
10355 gcc_unreachable();
10358 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10359 goto return_build_binary_op;
10362 /* For certain operations (which identify themselves by shorten != 0)
10363 if both args were extended from the same smaller type,
10364 do the arithmetic in that type and then extend.
10366 shorten !=0 and !=1 indicates a bitwise operation.
10367 For them, this optimization is safe only if
10368 both args are zero-extended or both are sign-extended.
10369 Otherwise, we might change the result.
10370 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10371 but calculated in (unsigned short) it would be (unsigned short)-1. */
10373 if (shorten && none_complex)
10375 final_type = result_type;
10376 result_type = shorten_binary_op (result_type, op0, op1,
10377 shorten == -1);
10380 /* Shifts can be shortened if shifting right. */
10382 if (short_shift)
10384 int unsigned_arg;
10385 tree arg0 = get_narrower (op0, &unsigned_arg);
10387 final_type = result_type;
10389 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10390 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10392 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10393 && tree_int_cst_sgn (op1) > 0
10394 /* We can shorten only if the shift count is less than the
10395 number of bits in the smaller type size. */
10396 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10397 /* We cannot drop an unsigned shift after sign-extension. */
10398 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10400 /* Do an unsigned shift if the operand was zero-extended. */
10401 result_type
10402 = c_common_signed_or_unsigned_type (unsigned_arg,
10403 TREE_TYPE (arg0));
10404 /* Convert value-to-be-shifted to that type. */
10405 if (TREE_TYPE (op0) != result_type)
10406 op0 = convert (result_type, op0);
10407 converted = 1;
10411 /* Comparison operations are shortened too but differently.
10412 They identify themselves by setting short_compare = 1. */
10414 if (short_compare)
10416 /* Don't write &op0, etc., because that would prevent op0
10417 from being kept in a register.
10418 Instead, make copies of the our local variables and
10419 pass the copies by reference, then copy them back afterward. */
10420 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10421 enum tree_code xresultcode = resultcode;
10422 tree val
10423 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10425 if (val != 0)
10427 ret = val;
10428 goto return_build_binary_op;
10431 op0 = xop0, op1 = xop1;
10432 converted = 1;
10433 resultcode = xresultcode;
10435 if (c_inhibit_evaluation_warnings == 0)
10437 bool op0_maybe_const = true;
10438 bool op1_maybe_const = true;
10439 tree orig_op0_folded, orig_op1_folded;
10441 if (in_late_binary_op)
10443 orig_op0_folded = orig_op0;
10444 orig_op1_folded = orig_op1;
10446 else
10448 /* Fold for the sake of possible warnings, as in
10449 build_conditional_expr. This requires the
10450 "original" values to be folded, not just op0 and
10451 op1. */
10452 c_inhibit_evaluation_warnings++;
10453 op0 = c_fully_fold (op0, require_constant_value,
10454 &op0_maybe_const);
10455 op1 = c_fully_fold (op1, require_constant_value,
10456 &op1_maybe_const);
10457 c_inhibit_evaluation_warnings--;
10458 orig_op0_folded = c_fully_fold (orig_op0,
10459 require_constant_value,
10460 NULL);
10461 orig_op1_folded = c_fully_fold (orig_op1,
10462 require_constant_value,
10463 NULL);
10466 if (warn_sign_compare)
10467 warn_for_sign_compare (location, orig_op0_folded,
10468 orig_op1_folded, op0, op1,
10469 result_type, resultcode);
10470 if (!in_late_binary_op && !int_operands)
10472 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10473 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10474 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10475 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10481 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10482 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10483 Then the expression will be built.
10484 It will be given type FINAL_TYPE if that is nonzero;
10485 otherwise, it will be given type RESULT_TYPE. */
10487 if (!result_type)
10489 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10490 return error_mark_node;
10493 if (build_type == NULL_TREE)
10495 build_type = result_type;
10496 if ((type0 != orig_type0 || type1 != orig_type1)
10497 && !boolean_op)
10499 gcc_assert (may_need_excess_precision && common);
10500 semantic_result_type = c_common_type (orig_type0, orig_type1);
10504 if (!converted)
10506 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10507 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10509 /* This can happen if one operand has a vector type, and the other
10510 has a different type. */
10511 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10512 return error_mark_node;
10515 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
10516 && current_function_decl != 0
10517 && !lookup_attribute ("no_sanitize_undefined",
10518 DECL_ATTRIBUTES (current_function_decl))
10519 && (doing_div_or_mod || doing_shift))
10521 /* OP0 and/or OP1 might have side-effects. */
10522 op0 = c_save_expr (op0);
10523 op1 = c_save_expr (op1);
10524 op0 = c_fully_fold (op0, false, NULL);
10525 op1 = c_fully_fold (op1, false, NULL);
10526 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
10527 instrument_expr = ubsan_instrument_division (location, op0, op1);
10528 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
10529 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
10532 /* Treat expressions in initializers specially as they can't trap. */
10533 if (int_const_or_overflow)
10534 ret = (require_constant_value
10535 ? fold_build2_initializer_loc (location, resultcode, build_type,
10536 op0, op1)
10537 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10538 else
10539 ret = build2 (resultcode, build_type, op0, op1);
10540 if (final_type != 0)
10541 ret = convert (final_type, ret);
10543 return_build_binary_op:
10544 gcc_assert (ret != error_mark_node);
10545 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10546 ret = (int_operands
10547 ? note_integer_operands (ret)
10548 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10549 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10550 && !in_late_binary_op)
10551 ret = note_integer_operands (ret);
10552 if (semantic_result_type)
10553 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10554 protected_set_expr_location (ret, location);
10556 if (instrument_expr != NULL)
10557 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
10558 instrument_expr, ret);
10560 return ret;
10564 /* Convert EXPR to be a truth-value, validating its type for this
10565 purpose. LOCATION is the source location for the expression. */
10567 tree
10568 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10570 bool int_const, int_operands;
10572 switch (TREE_CODE (TREE_TYPE (expr)))
10574 case ARRAY_TYPE:
10575 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10576 return error_mark_node;
10578 case RECORD_TYPE:
10579 error_at (location, "used struct type value where scalar is required");
10580 return error_mark_node;
10582 case UNION_TYPE:
10583 error_at (location, "used union type value where scalar is required");
10584 return error_mark_node;
10586 case VOID_TYPE:
10587 error_at (location, "void value not ignored as it ought to be");
10588 return error_mark_node;
10590 case FUNCTION_TYPE:
10591 gcc_unreachable ();
10593 case VECTOR_TYPE:
10594 error_at (location, "used vector type where scalar is required");
10595 return error_mark_node;
10597 default:
10598 break;
10601 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10602 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10603 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
10605 expr = remove_c_maybe_const_expr (expr);
10606 expr = build2 (NE_EXPR, integer_type_node, expr,
10607 convert (TREE_TYPE (expr), integer_zero_node));
10608 expr = note_integer_operands (expr);
10610 else
10611 /* ??? Should we also give an error for vectors rather than leaving
10612 those to give errors later? */
10613 expr = c_common_truthvalue_conversion (location, expr);
10615 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10617 if (TREE_OVERFLOW (expr))
10618 return expr;
10619 else
10620 return note_integer_operands (expr);
10622 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10623 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10624 return expr;
10628 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10629 required. */
10631 tree
10632 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10634 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10636 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10637 /* Executing a compound literal inside a function reinitializes
10638 it. */
10639 if (!TREE_STATIC (decl))
10640 *se = true;
10641 return decl;
10643 else
10644 return expr;
10647 /* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
10648 statement. LOC is the location of the OACC_PARALLEL. */
10650 tree
10651 c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
10653 tree stmt;
10655 block = c_end_compound_stmt (loc, block, true);
10657 stmt = make_node (OACC_PARALLEL);
10658 TREE_TYPE (stmt) = void_type_node;
10659 OACC_PARALLEL_CLAUSES (stmt) = clauses;
10660 OACC_PARALLEL_BODY (stmt) = block;
10661 SET_EXPR_LOCATION (stmt, loc);
10663 return add_stmt (stmt);
10666 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10668 tree
10669 c_begin_omp_parallel (void)
10671 tree block;
10673 keep_next_level ();
10674 block = c_begin_compound_stmt (true);
10676 return block;
10679 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10680 statement. LOC is the location of the OMP_PARALLEL. */
10682 tree
10683 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10685 tree stmt;
10687 block = c_end_compound_stmt (loc, block, true);
10689 stmt = make_node (OMP_PARALLEL);
10690 TREE_TYPE (stmt) = void_type_node;
10691 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10692 OMP_PARALLEL_BODY (stmt) = block;
10693 SET_EXPR_LOCATION (stmt, loc);
10695 return add_stmt (stmt);
10698 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10700 tree
10701 c_begin_omp_task (void)
10703 tree block;
10705 keep_next_level ();
10706 block = c_begin_compound_stmt (true);
10708 return block;
10711 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10712 statement. LOC is the location of the #pragma. */
10714 tree
10715 c_finish_omp_task (location_t loc, tree clauses, tree block)
10717 tree stmt;
10719 block = c_end_compound_stmt (loc, block, true);
10721 stmt = make_node (OMP_TASK);
10722 TREE_TYPE (stmt) = void_type_node;
10723 OMP_TASK_CLAUSES (stmt) = clauses;
10724 OMP_TASK_BODY (stmt) = block;
10725 SET_EXPR_LOCATION (stmt, loc);
10727 return add_stmt (stmt);
10730 /* Generate GOMP_cancel call for #pragma omp cancel. */
10732 void
10733 c_finish_omp_cancel (location_t loc, tree clauses)
10735 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
10736 int mask = 0;
10737 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
10738 mask = 1;
10739 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
10740 mask = 2;
10741 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
10742 mask = 4;
10743 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
10744 mask = 8;
10745 else
10747 error_at (loc, "%<#pragma omp cancel must specify one of "
10748 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
10749 "clauses");
10750 return;
10752 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
10753 if (ifc != NULL_TREE)
10755 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
10756 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
10757 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
10758 build_zero_cst (type));
10760 else
10761 ifc = boolean_true_node;
10762 tree stmt = build_call_expr_loc (loc, fn, 2,
10763 build_int_cst (integer_type_node, mask),
10764 ifc);
10765 add_stmt (stmt);
10768 /* Generate GOMP_cancellation_point call for
10769 #pragma omp cancellation point. */
10771 void
10772 c_finish_omp_cancellation_point (location_t loc, tree clauses)
10774 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
10775 int mask = 0;
10776 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
10777 mask = 1;
10778 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
10779 mask = 2;
10780 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
10781 mask = 4;
10782 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
10783 mask = 8;
10784 else
10786 error_at (loc, "%<#pragma omp cancellation point must specify one of "
10787 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
10788 "clauses");
10789 return;
10791 tree stmt = build_call_expr_loc (loc, fn, 1,
10792 build_int_cst (integer_type_node, mask));
10793 add_stmt (stmt);
10796 /* Helper function for handle_omp_array_sections. Called recursively
10797 to handle multiple array-section-subscripts. C is the clause,
10798 T current expression (initially OMP_CLAUSE_DECL), which is either
10799 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
10800 expression if specified, TREE_VALUE length expression if specified,
10801 TREE_CHAIN is what it has been specified after, or some decl.
10802 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
10803 set to true if any of the array-section-subscript could have length
10804 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
10805 first array-section-subscript which is known not to have length
10806 of one. Given say:
10807 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
10808 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
10809 all are or may have length of 1, array-section-subscript [:2] is the
10810 first one knonwn not to have length 1. For array-section-subscript
10811 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
10812 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
10813 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
10814 case though, as some lengths could be zero. */
10816 static tree
10817 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
10818 bool &maybe_zero_len, unsigned int &first_non_one)
10820 tree ret, low_bound, length, type;
10821 if (TREE_CODE (t) != TREE_LIST)
10823 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
10824 return error_mark_node;
10825 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10827 if (DECL_P (t))
10828 error_at (OMP_CLAUSE_LOCATION (c),
10829 "%qD is not a variable in %qs clause", t,
10830 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10831 else
10832 error_at (OMP_CLAUSE_LOCATION (c),
10833 "%qE is not a variable in %qs clause", t,
10834 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10835 return error_mark_node;
10837 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
10838 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10840 error_at (OMP_CLAUSE_LOCATION (c),
10841 "%qD is threadprivate variable in %qs clause", t,
10842 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10843 return error_mark_node;
10845 return t;
10848 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
10849 maybe_zero_len, first_non_one);
10850 if (ret == error_mark_node || ret == NULL_TREE)
10851 return ret;
10853 type = TREE_TYPE (ret);
10854 low_bound = TREE_PURPOSE (t);
10855 length = TREE_VALUE (t);
10857 if (low_bound == error_mark_node || length == error_mark_node)
10858 return error_mark_node;
10860 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
10862 error_at (OMP_CLAUSE_LOCATION (c),
10863 "low bound %qE of array section does not have integral type",
10864 low_bound);
10865 return error_mark_node;
10867 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
10869 error_at (OMP_CLAUSE_LOCATION (c),
10870 "length %qE of array section does not have integral type",
10871 length);
10872 return error_mark_node;
10874 if (low_bound
10875 && TREE_CODE (low_bound) == INTEGER_CST
10876 && TYPE_PRECISION (TREE_TYPE (low_bound))
10877 > TYPE_PRECISION (sizetype))
10878 low_bound = fold_convert (sizetype, low_bound);
10879 if (length
10880 && TREE_CODE (length) == INTEGER_CST
10881 && TYPE_PRECISION (TREE_TYPE (length))
10882 > TYPE_PRECISION (sizetype))
10883 length = fold_convert (sizetype, length);
10884 if (low_bound == NULL_TREE)
10885 low_bound = integer_zero_node;
10887 if (length != NULL_TREE)
10889 if (!integer_nonzerop (length))
10890 maybe_zero_len = true;
10891 if (first_non_one == types.length ()
10892 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
10893 first_non_one++;
10895 if (TREE_CODE (type) == ARRAY_TYPE)
10897 if (length == NULL_TREE
10898 && (TYPE_DOMAIN (type) == NULL_TREE
10899 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
10901 error_at (OMP_CLAUSE_LOCATION (c),
10902 "for unknown bound array type length expression must "
10903 "be specified");
10904 return error_mark_node;
10906 if (TREE_CODE (low_bound) == INTEGER_CST
10907 && tree_int_cst_sgn (low_bound) == -1)
10909 error_at (OMP_CLAUSE_LOCATION (c),
10910 "negative low bound in array section in %qs clause",
10911 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10912 return error_mark_node;
10914 if (length != NULL_TREE
10915 && TREE_CODE (length) == INTEGER_CST
10916 && tree_int_cst_sgn (length) == -1)
10918 error_at (OMP_CLAUSE_LOCATION (c),
10919 "negative length in array section in %qs clause",
10920 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10921 return error_mark_node;
10923 if (TYPE_DOMAIN (type)
10924 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
10925 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
10926 == INTEGER_CST)
10928 tree size = size_binop (PLUS_EXPR,
10929 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
10930 size_one_node);
10931 if (TREE_CODE (low_bound) == INTEGER_CST)
10933 if (tree_int_cst_lt (size, low_bound))
10935 error_at (OMP_CLAUSE_LOCATION (c),
10936 "low bound %qE above array section size "
10937 "in %qs clause", low_bound,
10938 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10939 return error_mark_node;
10941 if (tree_int_cst_equal (size, low_bound))
10942 maybe_zero_len = true;
10943 else if (length == NULL_TREE
10944 && first_non_one == types.length ()
10945 && tree_int_cst_equal
10946 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
10947 low_bound))
10948 first_non_one++;
10950 else if (length == NULL_TREE)
10952 maybe_zero_len = true;
10953 if (first_non_one == types.length ())
10954 first_non_one++;
10956 if (length && TREE_CODE (length) == INTEGER_CST)
10958 if (tree_int_cst_lt (size, length))
10960 error_at (OMP_CLAUSE_LOCATION (c),
10961 "length %qE above array section size "
10962 "in %qs clause", length,
10963 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10964 return error_mark_node;
10966 if (TREE_CODE (low_bound) == INTEGER_CST)
10968 tree lbpluslen
10969 = size_binop (PLUS_EXPR,
10970 fold_convert (sizetype, low_bound),
10971 fold_convert (sizetype, length));
10972 if (TREE_CODE (lbpluslen) == INTEGER_CST
10973 && tree_int_cst_lt (size, lbpluslen))
10975 error_at (OMP_CLAUSE_LOCATION (c),
10976 "high bound %qE above array section size "
10977 "in %qs clause", lbpluslen,
10978 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
10979 return error_mark_node;
10984 else if (length == NULL_TREE)
10986 maybe_zero_len = true;
10987 if (first_non_one == types.length ())
10988 first_non_one++;
10991 /* For [lb:] we will need to evaluate lb more than once. */
10992 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
10994 tree lb = c_save_expr (low_bound);
10995 if (lb != low_bound)
10997 TREE_PURPOSE (t) = lb;
10998 low_bound = lb;
11002 else if (TREE_CODE (type) == POINTER_TYPE)
11004 if (length == NULL_TREE)
11006 error_at (OMP_CLAUSE_LOCATION (c),
11007 "for pointer type length expression must be specified");
11008 return error_mark_node;
11010 /* If there is a pointer type anywhere but in the very first
11011 array-section-subscript, the array section can't be contiguous. */
11012 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11013 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11015 error_at (OMP_CLAUSE_LOCATION (c),
11016 "array section is not contiguous in %qs clause",
11017 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11018 return error_mark_node;
11021 else
11023 error_at (OMP_CLAUSE_LOCATION (c),
11024 "%qE does not have pointer or array type", ret);
11025 return error_mark_node;
11027 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11028 types.safe_push (TREE_TYPE (ret));
11029 /* We will need to evaluate lb more than once. */
11030 tree lb = c_save_expr (low_bound);
11031 if (lb != low_bound)
11033 TREE_PURPOSE (t) = lb;
11034 low_bound = lb;
11036 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11037 return ret;
11040 /* Handle array sections for clause C. */
11042 static bool
11043 handle_omp_array_sections (tree c)
11045 bool maybe_zero_len = false;
11046 unsigned int first_non_one = 0;
11047 vec<tree> types = vNULL;
11048 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11049 maybe_zero_len, first_non_one);
11050 if (first == error_mark_node)
11052 types.release ();
11053 return true;
11055 if (first == NULL_TREE)
11057 types.release ();
11058 return false;
11060 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11062 tree t = OMP_CLAUSE_DECL (c);
11063 tree tem = NULL_TREE;
11064 types.release ();
11065 /* Need to evaluate side effects in the length expressions
11066 if any. */
11067 while (TREE_CODE (t) == TREE_LIST)
11069 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11071 if (tem == NULL_TREE)
11072 tem = TREE_VALUE (t);
11073 else
11074 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11075 TREE_VALUE (t), tem);
11077 t = TREE_CHAIN (t);
11079 if (tem)
11080 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11081 first = c_fully_fold (first, false, NULL);
11082 OMP_CLAUSE_DECL (c) = first;
11084 else
11086 unsigned int num = types.length (), i;
11087 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11088 tree condition = NULL_TREE;
11090 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11091 maybe_zero_len = true;
11093 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11094 t = TREE_CHAIN (t))
11096 tree low_bound = TREE_PURPOSE (t);
11097 tree length = TREE_VALUE (t);
11099 i--;
11100 if (low_bound
11101 && TREE_CODE (low_bound) == INTEGER_CST
11102 && TYPE_PRECISION (TREE_TYPE (low_bound))
11103 > TYPE_PRECISION (sizetype))
11104 low_bound = fold_convert (sizetype, low_bound);
11105 if (length
11106 && TREE_CODE (length) == INTEGER_CST
11107 && TYPE_PRECISION (TREE_TYPE (length))
11108 > TYPE_PRECISION (sizetype))
11109 length = fold_convert (sizetype, length);
11110 if (low_bound == NULL_TREE)
11111 low_bound = integer_zero_node;
11112 if (!maybe_zero_len && i > first_non_one)
11114 if (integer_nonzerop (low_bound))
11115 goto do_warn_noncontiguous;
11116 if (length != NULL_TREE
11117 && TREE_CODE (length) == INTEGER_CST
11118 && TYPE_DOMAIN (types[i])
11119 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11120 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11121 == INTEGER_CST)
11123 tree size;
11124 size = size_binop (PLUS_EXPR,
11125 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11126 size_one_node);
11127 if (!tree_int_cst_equal (length, size))
11129 do_warn_noncontiguous:
11130 error_at (OMP_CLAUSE_LOCATION (c),
11131 "array section is not contiguous in %qs "
11132 "clause",
11133 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11134 types.release ();
11135 return true;
11138 if (length != NULL_TREE
11139 && TREE_SIDE_EFFECTS (length))
11141 if (side_effects == NULL_TREE)
11142 side_effects = length;
11143 else
11144 side_effects = build2 (COMPOUND_EXPR,
11145 TREE_TYPE (side_effects),
11146 length, side_effects);
11149 else
11151 tree l;
11153 if (i > first_non_one && length && integer_nonzerop (length))
11154 continue;
11155 if (length)
11156 l = fold_convert (sizetype, length);
11157 else
11159 l = size_binop (PLUS_EXPR,
11160 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11161 size_one_node);
11162 l = size_binop (MINUS_EXPR, l,
11163 fold_convert (sizetype, low_bound));
11165 if (i > first_non_one)
11167 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11168 size_zero_node);
11169 if (condition == NULL_TREE)
11170 condition = l;
11171 else
11172 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11173 l, condition);
11175 else if (size == NULL_TREE)
11177 size = size_in_bytes (TREE_TYPE (types[i]));
11178 size = size_binop (MULT_EXPR, size, l);
11179 if (condition)
11180 size = fold_build3 (COND_EXPR, sizetype, condition,
11181 size, size_zero_node);
11183 else
11184 size = size_binop (MULT_EXPR, size, l);
11187 types.release ();
11188 if (side_effects)
11189 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11190 first = c_fully_fold (first, false, NULL);
11191 OMP_CLAUSE_DECL (c) = first;
11192 if (size)
11193 size = c_fully_fold (size, false, NULL);
11194 OMP_CLAUSE_SIZE (c) = size;
11195 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11196 return false;
11197 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11198 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11199 if (!c_mark_addressable (t))
11200 return false;
11201 OMP_CLAUSE_DECL (c2) = t;
11202 t = build_fold_addr_expr (first);
11203 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11204 tree ptr = OMP_CLAUSE_DECL (c2);
11205 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11206 ptr = build_fold_addr_expr (ptr);
11207 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11208 ptrdiff_type_node, t,
11209 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11210 ptrdiff_type_node, ptr));
11211 t = c_fully_fold (t, false, NULL);
11212 OMP_CLAUSE_SIZE (c2) = t;
11213 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11214 OMP_CLAUSE_CHAIN (c) = c2;
11216 return false;
11219 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
11220 an inline call. But, remap
11221 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11222 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11224 static tree
11225 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11226 tree decl, tree placeholder)
11228 copy_body_data id;
11229 struct pointer_map_t *decl_map = pointer_map_create ();
11231 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
11232 *pointer_map_insert (decl_map, omp_decl2) = decl;
11233 memset (&id, 0, sizeof (id));
11234 id.src_fn = DECL_CONTEXT (omp_decl1);
11235 id.dst_fn = current_function_decl;
11236 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11237 id.decl_map = decl_map;
11239 id.copy_decl = copy_decl_no_change;
11240 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11241 id.transform_new_cfg = true;
11242 id.transform_return_to_modify = false;
11243 id.transform_lang_insert_block = NULL;
11244 id.eh_lp_nr = 0;
11245 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11246 pointer_map_destroy (decl_map);
11247 return stmt;
11250 /* Helper function of c_finish_omp_clauses, called via walk_tree.
11251 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11253 static tree
11254 c_find_omp_placeholder_r (tree *tp, int *, void *data)
11256 if (*tp == (tree) data)
11257 return *tp;
11258 return NULL_TREE;
11261 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
11262 Remove any elements from the list that are invalid. */
11264 tree
11265 c_finish_omp_clauses (tree clauses)
11267 bitmap_head generic_head, firstprivate_head, lastprivate_head;
11268 bitmap_head aligned_head;
11269 tree c, t, *pc = &clauses;
11270 bool branch_seen = false;
11271 bool copyprivate_seen = false;
11272 tree *nowait_clause = NULL;
11274 bitmap_obstack_initialize (NULL);
11275 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11276 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11277 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
11278 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
11280 for (pc = &clauses, c = clauses; c ; c = *pc)
11282 bool remove = false;
11283 bool need_complete = false;
11284 bool need_implicitly_determined = false;
11286 switch (OMP_CLAUSE_CODE (c))
11288 case OMP_CLAUSE_SHARED:
11289 need_implicitly_determined = true;
11290 goto check_dup_generic;
11292 case OMP_CLAUSE_PRIVATE:
11293 need_complete = true;
11294 need_implicitly_determined = true;
11295 goto check_dup_generic;
11297 case OMP_CLAUSE_REDUCTION:
11298 need_implicitly_determined = true;
11299 t = OMP_CLAUSE_DECL (c);
11300 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
11301 && FLOAT_TYPE_P (TREE_TYPE (t)))
11303 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11304 const char *r_name = NULL;
11306 switch (r_code)
11308 case PLUS_EXPR:
11309 case MULT_EXPR:
11310 case MINUS_EXPR:
11311 case MIN_EXPR:
11312 case MAX_EXPR:
11313 break;
11314 case BIT_AND_EXPR:
11315 r_name = "&";
11316 break;
11317 case BIT_XOR_EXPR:
11318 r_name = "^";
11319 break;
11320 case BIT_IOR_EXPR:
11321 r_name = "|";
11322 break;
11323 case TRUTH_ANDIF_EXPR:
11324 r_name = "&&";
11325 break;
11326 case TRUTH_ORIF_EXPR:
11327 r_name = "||";
11328 break;
11329 default:
11330 gcc_unreachable ();
11332 if (r_name)
11334 error_at (OMP_CLAUSE_LOCATION (c),
11335 "%qE has invalid type for %<reduction(%s)%>",
11336 t, r_name);
11337 remove = true;
11338 break;
11341 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11343 error_at (OMP_CLAUSE_LOCATION (c),
11344 "user defined reduction not found for %qD", t);
11345 remove = true;
11346 break;
11348 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11350 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11351 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11352 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11353 VAR_DECL, NULL_TREE, type);
11354 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11355 DECL_ARTIFICIAL (placeholder) = 1;
11356 DECL_IGNORED_P (placeholder) = 1;
11357 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11358 c_mark_addressable (placeholder);
11359 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11360 c_mark_addressable (OMP_CLAUSE_DECL (c));
11361 OMP_CLAUSE_REDUCTION_MERGE (c)
11362 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11363 TREE_VEC_ELT (list, 0),
11364 TREE_VEC_ELT (list, 1),
11365 OMP_CLAUSE_DECL (c), placeholder);
11366 OMP_CLAUSE_REDUCTION_MERGE (c)
11367 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11368 void_type_node, NULL_TREE,
11369 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11370 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11371 if (TREE_VEC_LENGTH (list) == 6)
11373 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11374 c_mark_addressable (OMP_CLAUSE_DECL (c));
11375 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11376 c_mark_addressable (placeholder);
11377 tree init = TREE_VEC_ELT (list, 5);
11378 if (init == error_mark_node)
11379 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11380 OMP_CLAUSE_REDUCTION_INIT (c)
11381 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11382 TREE_VEC_ELT (list, 3),
11383 OMP_CLAUSE_DECL (c), placeholder);
11384 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11385 OMP_CLAUSE_REDUCTION_INIT (c)
11386 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11387 OMP_CLAUSE_REDUCTION_INIT (c));
11388 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11389 c_find_omp_placeholder_r,
11390 placeholder, NULL))
11391 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11393 else
11395 tree init;
11396 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11397 init = build_constructor (TREE_TYPE (t), NULL);
11398 else
11399 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11400 OMP_CLAUSE_REDUCTION_INIT (c)
11401 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11403 OMP_CLAUSE_REDUCTION_INIT (c)
11404 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11405 void_type_node, NULL_TREE,
11406 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11407 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11409 goto check_dup_generic;
11411 case OMP_CLAUSE_COPYPRIVATE:
11412 copyprivate_seen = true;
11413 if (nowait_clause)
11415 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11416 "%<nowait%> clause must not be used together "
11417 "with %<copyprivate%>");
11418 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11419 nowait_clause = NULL;
11421 goto check_dup_generic;
11423 case OMP_CLAUSE_COPYIN:
11424 t = OMP_CLAUSE_DECL (c);
11425 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11427 error_at (OMP_CLAUSE_LOCATION (c),
11428 "%qE must be %<threadprivate%> for %<copyin%>", t);
11429 remove = true;
11430 break;
11432 goto check_dup_generic;
11434 case OMP_CLAUSE_LINEAR:
11435 t = OMP_CLAUSE_DECL (c);
11436 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11437 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11439 error_at (OMP_CLAUSE_LOCATION (c),
11440 "linear clause applied to non-integral non-pointer "
11441 "variable with type %qT", TREE_TYPE (t));
11442 remove = true;
11443 break;
11445 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11447 tree s = OMP_CLAUSE_LINEAR_STEP (c);
11448 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
11449 OMP_CLAUSE_DECL (c), s);
11450 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11451 sizetype, s, OMP_CLAUSE_DECL (c));
11452 if (s == error_mark_node)
11453 s = size_one_node;
11454 OMP_CLAUSE_LINEAR_STEP (c) = s;
11456 goto check_dup_generic;
11458 check_dup_generic:
11459 t = OMP_CLAUSE_DECL (c);
11460 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11462 error_at (OMP_CLAUSE_LOCATION (c),
11463 "%qE is not a variable in clause %qs", t,
11464 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11465 remove = true;
11467 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11468 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
11469 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11471 error_at (OMP_CLAUSE_LOCATION (c),
11472 "%qE appears more than once in data clauses", t);
11473 remove = true;
11475 else
11476 bitmap_set_bit (&generic_head, DECL_UID (t));
11477 break;
11479 case OMP_CLAUSE_FIRSTPRIVATE:
11480 t = OMP_CLAUSE_DECL (c);
11481 need_complete = true;
11482 need_implicitly_determined = true;
11483 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11485 error_at (OMP_CLAUSE_LOCATION (c),
11486 "%qE is not a variable in clause %<firstprivate%>", t);
11487 remove = true;
11489 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11490 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
11492 error_at (OMP_CLAUSE_LOCATION (c),
11493 "%qE appears more than once in data clauses", t);
11494 remove = true;
11496 else
11497 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
11498 break;
11500 case OMP_CLAUSE_LASTPRIVATE:
11501 t = OMP_CLAUSE_DECL (c);
11502 need_complete = true;
11503 need_implicitly_determined = true;
11504 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11506 error_at (OMP_CLAUSE_LOCATION (c),
11507 "%qE is not a variable in clause %<lastprivate%>", t);
11508 remove = true;
11510 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11511 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11513 error_at (OMP_CLAUSE_LOCATION (c),
11514 "%qE appears more than once in data clauses", t);
11515 remove = true;
11517 else
11518 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
11519 break;
11521 case OMP_CLAUSE_ALIGNED:
11522 t = OMP_CLAUSE_DECL (c);
11523 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11525 error_at (OMP_CLAUSE_LOCATION (c),
11526 "%qE is not a variable in %<aligned%> clause", t);
11527 remove = true;
11529 else if (!POINTER_TYPE_P (TREE_TYPE (t))
11530 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
11532 error_at (OMP_CLAUSE_LOCATION (c),
11533 "%qE in %<aligned%> clause is neither a pointer nor "
11534 "an array", t);
11535 remove = true;
11537 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
11539 error_at (OMP_CLAUSE_LOCATION (c),
11540 "%qE appears more than once in %<aligned%> clauses",
11542 remove = true;
11544 else
11545 bitmap_set_bit (&aligned_head, DECL_UID (t));
11546 break;
11548 case OMP_CLAUSE_DEPEND:
11549 t = OMP_CLAUSE_DECL (c);
11550 if (TREE_CODE (t) == TREE_LIST)
11552 if (handle_omp_array_sections (c))
11553 remove = true;
11554 break;
11556 if (t == error_mark_node)
11557 remove = true;
11558 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11560 error_at (OMP_CLAUSE_LOCATION (c),
11561 "%qE is not a variable in %<depend%> clause", t);
11562 remove = true;
11564 else if (!c_mark_addressable (t))
11565 remove = true;
11566 break;
11568 case OMP_CLAUSE_MAP:
11569 case OMP_CLAUSE_TO:
11570 case OMP_CLAUSE_FROM:
11571 t = OMP_CLAUSE_DECL (c);
11572 if (TREE_CODE (t) == TREE_LIST)
11574 if (handle_omp_array_sections (c))
11575 remove = true;
11576 else
11578 t = OMP_CLAUSE_DECL (c);
11579 if (!COMPLETE_TYPE_P (TREE_TYPE (t)))
11581 error_at (OMP_CLAUSE_LOCATION (c),
11582 "array section does not have mappable type "
11583 "in %qs clause",
11584 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11585 remove = true;
11588 break;
11590 if (t == error_mark_node)
11591 remove = true;
11592 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11594 error_at (OMP_CLAUSE_LOCATION (c),
11595 "%qE is not a variable in %qs clause", t,
11596 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11597 remove = true;
11599 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11601 error_at (OMP_CLAUSE_LOCATION (c),
11602 "%qD is threadprivate variable in %qs clause", t,
11603 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11604 remove = true;
11606 else if (!c_mark_addressable (t))
11607 remove = true;
11608 else if (!COMPLETE_TYPE_P (TREE_TYPE (t))
11609 && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
11610 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER))
11612 error_at (OMP_CLAUSE_LOCATION (c),
11613 "%qD does not have a mappable type in %qs clause", t,
11614 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11615 remove = true;
11617 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
11619 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11620 error ("%qD appears more than once in motion clauses", t);
11621 else
11622 error ("%qD appears more than once in map clauses", t);
11623 remove = true;
11625 else
11626 bitmap_set_bit (&generic_head, DECL_UID (t));
11627 break;
11629 case OMP_CLAUSE_UNIFORM:
11630 t = OMP_CLAUSE_DECL (c);
11631 if (TREE_CODE (t) != PARM_DECL)
11633 if (DECL_P (t))
11634 error_at (OMP_CLAUSE_LOCATION (c),
11635 "%qD is not an argument in %<uniform%> clause", t);
11636 else
11637 error_at (OMP_CLAUSE_LOCATION (c),
11638 "%qE is not an argument in %<uniform%> clause", t);
11639 remove = true;
11640 break;
11642 goto check_dup_generic;
11644 case OMP_CLAUSE_NOWAIT:
11645 if (copyprivate_seen)
11647 error_at (OMP_CLAUSE_LOCATION (c),
11648 "%<nowait%> clause must not be used together "
11649 "with %<copyprivate%>");
11650 remove = true;
11651 break;
11653 nowait_clause = pc;
11654 pc = &OMP_CLAUSE_CHAIN (c);
11655 continue;
11657 case OMP_CLAUSE_IF:
11658 case OMP_CLAUSE_NUM_THREADS:
11659 case OMP_CLAUSE_NUM_TEAMS:
11660 case OMP_CLAUSE_THREAD_LIMIT:
11661 case OMP_CLAUSE_SCHEDULE:
11662 case OMP_CLAUSE_ORDERED:
11663 case OMP_CLAUSE_DEFAULT:
11664 case OMP_CLAUSE_UNTIED:
11665 case OMP_CLAUSE_COLLAPSE:
11666 case OMP_CLAUSE_FINAL:
11667 case OMP_CLAUSE_MERGEABLE:
11668 case OMP_CLAUSE_SAFELEN:
11669 case OMP_CLAUSE_SIMDLEN:
11670 case OMP_CLAUSE_DEVICE:
11671 case OMP_CLAUSE_DIST_SCHEDULE:
11672 case OMP_CLAUSE_PARALLEL:
11673 case OMP_CLAUSE_FOR:
11674 case OMP_CLAUSE_SECTIONS:
11675 case OMP_CLAUSE_TASKGROUP:
11676 case OMP_CLAUSE_PROC_BIND:
11677 pc = &OMP_CLAUSE_CHAIN (c);
11678 continue;
11680 case OMP_CLAUSE_INBRANCH:
11681 case OMP_CLAUSE_NOTINBRANCH:
11682 if (branch_seen)
11684 error_at (OMP_CLAUSE_LOCATION (c),
11685 "%<inbranch%> clause is incompatible with "
11686 "%<notinbranch%>");
11687 remove = true;
11688 break;
11690 branch_seen = true;
11691 pc = &OMP_CLAUSE_CHAIN (c);
11692 continue;
11694 default:
11695 gcc_unreachable ();
11698 if (!remove)
11700 t = OMP_CLAUSE_DECL (c);
11702 if (need_complete)
11704 t = require_complete_type (t);
11705 if (t == error_mark_node)
11706 remove = true;
11709 if (need_implicitly_determined)
11711 const char *share_name = NULL;
11713 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11714 share_name = "threadprivate";
11715 else switch (c_omp_predetermined_sharing (t))
11717 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
11718 break;
11719 case OMP_CLAUSE_DEFAULT_SHARED:
11720 /* const vars may be specified in firstprivate clause. */
11721 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11722 && TREE_READONLY (t))
11723 break;
11724 share_name = "shared";
11725 break;
11726 case OMP_CLAUSE_DEFAULT_PRIVATE:
11727 share_name = "private";
11728 break;
11729 default:
11730 gcc_unreachable ();
11732 if (share_name)
11734 error_at (OMP_CLAUSE_LOCATION (c),
11735 "%qE is predetermined %qs for %qs",
11736 t, share_name,
11737 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11738 remove = true;
11743 if (remove)
11744 *pc = OMP_CLAUSE_CHAIN (c);
11745 else
11746 pc = &OMP_CLAUSE_CHAIN (c);
11749 bitmap_obstack_release (NULL);
11750 return clauses;
11753 /* Create a transaction node. */
11755 tree
11756 c_finish_transaction (location_t loc, tree block, int flags)
11758 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
11759 if (flags & TM_STMT_ATTR_OUTER)
11760 TRANSACTION_EXPR_OUTER (stmt) = 1;
11761 if (flags & TM_STMT_ATTR_RELAXED)
11762 TRANSACTION_EXPR_RELAXED (stmt) = 1;
11763 return add_stmt (stmt);
11766 /* Make a variant type in the proper way for C/C++, propagating qualifiers
11767 down to the element type of an array. */
11769 tree
11770 c_build_qualified_type (tree type, int type_quals)
11772 if (type == error_mark_node)
11773 return type;
11775 if (TREE_CODE (type) == ARRAY_TYPE)
11777 tree t;
11778 tree element_type = c_build_qualified_type (TREE_TYPE (type),
11779 type_quals);
11781 /* See if we already have an identically qualified type. */
11782 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11784 if (TYPE_QUALS (strip_array_types (t)) == type_quals
11785 && TYPE_NAME (t) == TYPE_NAME (type)
11786 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
11787 && attribute_list_equal (TYPE_ATTRIBUTES (t),
11788 TYPE_ATTRIBUTES (type)))
11789 break;
11791 if (!t)
11793 tree domain = TYPE_DOMAIN (type);
11795 t = build_variant_type_copy (type);
11796 TREE_TYPE (t) = element_type;
11798 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
11799 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
11800 SET_TYPE_STRUCTURAL_EQUALITY (t);
11801 else if (TYPE_CANONICAL (element_type) != element_type
11802 || (domain && TYPE_CANONICAL (domain) != domain))
11804 tree unqualified_canon
11805 = build_array_type (TYPE_CANONICAL (element_type),
11806 domain? TYPE_CANONICAL (domain)
11807 : NULL_TREE);
11808 TYPE_CANONICAL (t)
11809 = c_build_qualified_type (unqualified_canon, type_quals);
11811 else
11812 TYPE_CANONICAL (t) = t;
11814 return t;
11817 /* A restrict-qualified pointer type must be a pointer to object or
11818 incomplete type. Note that the use of POINTER_TYPE_P also allows
11819 REFERENCE_TYPEs, which is appropriate for C++. */
11820 if ((type_quals & TYPE_QUAL_RESTRICT)
11821 && (!POINTER_TYPE_P (type)
11822 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
11824 error ("invalid use of %<restrict%>");
11825 type_quals &= ~TYPE_QUAL_RESTRICT;
11828 return build_qualified_type (type, type_quals);
11831 /* Build a VA_ARG_EXPR for the C parser. */
11833 tree
11834 c_build_va_arg (location_t loc, tree expr, tree type)
11836 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
11837 warning_at (loc, OPT_Wc___compat,
11838 "C++ requires promoted type, not enum type, in %<va_arg%>");
11839 return build_va_arg (loc, expr, type);
11842 /* Return truthvalue of whether T1 is the same tree structure as T2.
11843 Return 1 if they are the same. Return 0 if they are different. */
11845 bool
11846 c_tree_equal (tree t1, tree t2)
11848 enum tree_code code1, code2;
11850 if (t1 == t2)
11851 return true;
11852 if (!t1 || !t2)
11853 return false;
11855 for (code1 = TREE_CODE (t1);
11856 CONVERT_EXPR_CODE_P (code1)
11857 || code1 == NON_LVALUE_EXPR;
11858 code1 = TREE_CODE (t1))
11859 t1 = TREE_OPERAND (t1, 0);
11860 for (code2 = TREE_CODE (t2);
11861 CONVERT_EXPR_CODE_P (code2)
11862 || code2 == NON_LVALUE_EXPR;
11863 code2 = TREE_CODE (t2))
11864 t2 = TREE_OPERAND (t2, 0);
11866 /* They might have become equal now. */
11867 if (t1 == t2)
11868 return true;
11870 if (code1 != code2)
11871 return false;
11873 switch (code1)
11875 case INTEGER_CST:
11876 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
11877 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
11879 case REAL_CST:
11880 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
11882 case STRING_CST:
11883 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
11884 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
11885 TREE_STRING_LENGTH (t1));
11887 case FIXED_CST:
11888 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
11889 TREE_FIXED_CST (t2));
11891 case COMPLEX_CST:
11892 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
11893 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
11895 case VECTOR_CST:
11896 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
11898 case CONSTRUCTOR:
11899 /* We need to do this when determining whether or not two
11900 non-type pointer to member function template arguments
11901 are the same. */
11902 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
11903 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
11904 return false;
11906 tree field, value;
11907 unsigned int i;
11908 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
11910 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
11911 if (!c_tree_equal (field, elt2->index)
11912 || !c_tree_equal (value, elt2->value))
11913 return false;
11916 return true;
11918 case TREE_LIST:
11919 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
11920 return false;
11921 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
11922 return false;
11923 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
11925 case SAVE_EXPR:
11926 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
11928 case CALL_EXPR:
11930 tree arg1, arg2;
11931 call_expr_arg_iterator iter1, iter2;
11932 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
11933 return false;
11934 for (arg1 = first_call_expr_arg (t1, &iter1),
11935 arg2 = first_call_expr_arg (t2, &iter2);
11936 arg1 && arg2;
11937 arg1 = next_call_expr_arg (&iter1),
11938 arg2 = next_call_expr_arg (&iter2))
11939 if (!c_tree_equal (arg1, arg2))
11940 return false;
11941 if (arg1 || arg2)
11942 return false;
11943 return true;
11946 case TARGET_EXPR:
11948 tree o1 = TREE_OPERAND (t1, 0);
11949 tree o2 = TREE_OPERAND (t2, 0);
11951 /* Special case: if either target is an unallocated VAR_DECL,
11952 it means that it's going to be unified with whatever the
11953 TARGET_EXPR is really supposed to initialize, so treat it
11954 as being equivalent to anything. */
11955 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
11956 && !DECL_RTL_SET_P (o1))
11957 /*Nop*/;
11958 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
11959 && !DECL_RTL_SET_P (o2))
11960 /*Nop*/;
11961 else if (!c_tree_equal (o1, o2))
11962 return false;
11964 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
11967 case COMPONENT_REF:
11968 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
11969 return false;
11970 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
11972 case PARM_DECL:
11973 case VAR_DECL:
11974 case CONST_DECL:
11975 case FIELD_DECL:
11976 case FUNCTION_DECL:
11977 case IDENTIFIER_NODE:
11978 case SSA_NAME:
11979 return false;
11981 case TREE_VEC:
11983 unsigned ix;
11984 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
11985 return false;
11986 for (ix = TREE_VEC_LENGTH (t1); ix--;)
11987 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
11988 TREE_VEC_ELT (t2, ix)))
11989 return false;
11990 return true;
11993 default:
11994 break;
11997 switch (TREE_CODE_CLASS (code1))
11999 case tcc_unary:
12000 case tcc_binary:
12001 case tcc_comparison:
12002 case tcc_expression:
12003 case tcc_vl_exp:
12004 case tcc_reference:
12005 case tcc_statement:
12007 int i, n = TREE_OPERAND_LENGTH (t1);
12009 switch (code1)
12011 case PREINCREMENT_EXPR:
12012 case PREDECREMENT_EXPR:
12013 case POSTINCREMENT_EXPR:
12014 case POSTDECREMENT_EXPR:
12015 n = 1;
12016 break;
12017 case ARRAY_REF:
12018 n = 2;
12019 break;
12020 default:
12021 break;
12024 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12025 && n != TREE_OPERAND_LENGTH (t2))
12026 return false;
12028 for (i = 0; i < n; ++i)
12029 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12030 return false;
12032 return true;
12035 case tcc_type:
12036 return comptypes (t1, t2);
12037 default:
12038 gcc_unreachable ();
12040 /* We can get here with --disable-checking. */
12041 return false;