* configure.tgt: Add sh* case.
[official-gcc.git] / gcc / c-typeck.c
blob4a134b0e52412ed206170d6288482ee3fe40a209
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "langhooks.h"
34 #include "c-tree.h"
35 #include "c-lang.h"
36 #include "flags.h"
37 #include "output.h"
38 #include "intl.h"
39 #include "target.h"
40 #include "tree-iterator.h"
41 #include "bitmap.h"
42 #include "gimple.h"
43 #include "c-family/c-objc.h"
45 /* Possible cases of implicit bad conversions. Used to select
46 diagnostic messages in convert_for_assignment. */
47 enum impl_conv {
48 ic_argpass,
49 ic_assign,
50 ic_init,
51 ic_return
54 /* Possibe cases of scalar_to_vector conversion. */
55 enum stv_conv {
56 stv_error, /* Error occured. */
57 stv_nothing, /* Nothing happened. */
58 stv_firstarg, /* First argument must be expanded. */
59 stv_secondarg /* Second argument must be expanded. */
62 /* The level of nesting inside "__alignof__". */
63 int in_alignof;
65 /* The level of nesting inside "sizeof". */
66 int in_sizeof;
68 /* The level of nesting inside "typeof". */
69 int in_typeof;
71 /* Nonzero if we've already printed a "missing braces around initializer"
72 message within this initializer. */
73 static int missing_braces_mentioned;
75 static int require_constant_value;
76 static int require_constant_elements;
78 static bool null_pointer_constant_p (const_tree);
79 static tree qualify_type (tree, tree);
80 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
81 bool *);
82 static int comp_target_types (location_t, tree, tree);
83 static int function_types_compatible_p (const_tree, const_tree, bool *,
84 bool *);
85 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
86 static tree lookup_field (tree, tree);
87 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
88 tree);
89 static tree pointer_diff (location_t, tree, tree);
90 static tree convert_for_assignment (location_t, tree, tree, tree,
91 enum impl_conv, bool, tree, tree, int);
92 static tree valid_compound_expr_initializer (tree, tree);
93 static void push_string (const char *);
94 static void push_member_name (tree);
95 static int spelling_length (void);
96 static char *print_spelling (char *);
97 static void warning_init (int, const char *);
98 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
99 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
100 struct obstack *);
101 static void output_pending_init_elements (int, struct obstack *);
102 static int set_designator (int, struct obstack *);
103 static void push_range_stack (tree, struct obstack *);
104 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
105 static void set_nonincremental_init (struct obstack *);
106 static void set_nonincremental_init_from_string (tree, struct obstack *);
107 static tree find_init_member (tree, struct obstack *);
108 static void readonly_warning (tree, enum lvalue_use);
109 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
110 static void record_maybe_used_decl (tree);
111 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
113 /* Return true if EXP is a null pointer constant, false otherwise. */
115 static bool
116 null_pointer_constant_p (const_tree expr)
118 /* This should really operate on c_expr structures, but they aren't
119 yet available everywhere required. */
120 tree type = TREE_TYPE (expr);
121 return (TREE_CODE (expr) == INTEGER_CST
122 && !TREE_OVERFLOW (expr)
123 && integer_zerop (expr)
124 && (INTEGRAL_TYPE_P (type)
125 || (TREE_CODE (type) == POINTER_TYPE
126 && VOID_TYPE_P (TREE_TYPE (type))
127 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
130 /* EXPR may appear in an unevaluated part of an integer constant
131 expression, but not in an evaluated part. Wrap it in a
132 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
133 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
135 static tree
136 note_integer_operands (tree expr)
138 tree ret;
139 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
141 ret = copy_node (expr);
142 TREE_OVERFLOW (ret) = 1;
144 else
146 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
147 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
149 return ret;
152 /* Having checked whether EXPR may appear in an unevaluated part of an
153 integer constant expression and found that it may, remove any
154 C_MAYBE_CONST_EXPR noting this fact and return the resulting
155 expression. */
157 static inline tree
158 remove_c_maybe_const_expr (tree expr)
160 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
161 return C_MAYBE_CONST_EXPR_EXPR (expr);
162 else
163 return expr;
166 \f/* This is a cache to hold if two types are compatible or not. */
168 struct tagged_tu_seen_cache {
169 const struct tagged_tu_seen_cache * next;
170 const_tree t1;
171 const_tree t2;
172 /* The return value of tagged_types_tu_compatible_p if we had seen
173 these two types already. */
174 int val;
177 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
178 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
180 /* Do `exp = require_complete_type (exp);' to make sure exp
181 does not have an incomplete type. (That includes void types.) */
183 tree
184 require_complete_type (tree value)
186 tree type = TREE_TYPE (value);
188 if (value == error_mark_node || type == error_mark_node)
189 return error_mark_node;
191 /* First, detect a valid value with a complete type. */
192 if (COMPLETE_TYPE_P (type))
193 return value;
195 c_incomplete_type_error (value, type);
196 return error_mark_node;
199 /* Print an error message for invalid use of an incomplete type.
200 VALUE is the expression that was used (or 0 if that isn't known)
201 and TYPE is the type that was invalid. */
203 void
204 c_incomplete_type_error (const_tree value, const_tree type)
206 const char *type_code_string;
208 /* Avoid duplicate error message. */
209 if (TREE_CODE (type) == ERROR_MARK)
210 return;
212 if (value != 0 && (TREE_CODE (value) == VAR_DECL
213 || TREE_CODE (value) == PARM_DECL))
214 error ("%qD has an incomplete type", value);
215 else
217 retry:
218 /* We must print an error message. Be clever about what it says. */
220 switch (TREE_CODE (type))
222 case RECORD_TYPE:
223 type_code_string = "struct";
224 break;
226 case UNION_TYPE:
227 type_code_string = "union";
228 break;
230 case ENUMERAL_TYPE:
231 type_code_string = "enum";
232 break;
234 case VOID_TYPE:
235 error ("invalid use of void expression");
236 return;
238 case ARRAY_TYPE:
239 if (TYPE_DOMAIN (type))
241 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
243 error ("invalid use of flexible array member");
244 return;
246 type = TREE_TYPE (type);
247 goto retry;
249 error ("invalid use of array with unspecified bounds");
250 return;
252 default:
253 gcc_unreachable ();
256 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
257 error ("invalid use of undefined type %<%s %E%>",
258 type_code_string, TYPE_NAME (type));
259 else
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
261 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
265 /* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
268 tree
269 c_type_promotes_to (tree type)
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 return double_type_node;
274 if (c_promoting_integer_type_p (type))
276 /* Preserve unsignedness if not really getting any wider. */
277 if (TYPE_UNSIGNED (type)
278 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
279 return unsigned_type_node;
280 return integer_type_node;
283 return type;
286 /* Return true if between two named address spaces, whether there is a superset
287 named address space that encompasses both address spaces. If there is a
288 superset, return which address space is the superset. */
290 static bool
291 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
293 if (as1 == as2)
295 *common = as1;
296 return true;
298 else if (targetm.addr_space.subset_p (as1, as2))
300 *common = as2;
301 return true;
303 else if (targetm.addr_space.subset_p (as2, as1))
305 *common = as1;
306 return true;
308 else
309 return false;
312 /* Return a variant of TYPE which has all the type qualifiers of LIKE
313 as well as those of TYPE. */
315 static tree
316 qualify_type (tree type, tree like)
318 addr_space_t as_type = TYPE_ADDR_SPACE (type);
319 addr_space_t as_like = TYPE_ADDR_SPACE (like);
320 addr_space_t as_common;
322 /* If the two named address spaces are different, determine the common
323 superset address space. If there isn't one, raise an error. */
324 if (!addr_space_superset (as_type, as_like, &as_common))
326 as_common = as_type;
327 error ("%qT and %qT are in disjoint named address spaces",
328 type, like);
331 return c_build_qualified_type (type,
332 TYPE_QUALS_NO_ADDR_SPACE (type)
333 | TYPE_QUALS_NO_ADDR_SPACE (like)
334 | ENCODE_QUAL_ADDR_SPACE (as_common));
337 /* Return true iff the given tree T is a variable length array. */
339 bool
340 c_vla_type_p (const_tree t)
342 if (TREE_CODE (t) == ARRAY_TYPE
343 && C_TYPE_VARIABLE_SIZE (t))
344 return true;
345 return false;
348 /* Return the composite type of two compatible types.
350 We assume that comptypes has already been done and returned
351 nonzero; if that isn't so, this may crash. In particular, we
352 assume that qualifiers match. */
354 tree
355 composite_type (tree t1, tree t2)
357 enum tree_code code1;
358 enum tree_code code2;
359 tree attributes;
361 /* Save time if the two types are the same. */
363 if (t1 == t2) return t1;
365 /* If one type is nonsense, use the other. */
366 if (t1 == error_mark_node)
367 return t2;
368 if (t2 == error_mark_node)
369 return t1;
371 code1 = TREE_CODE (t1);
372 code2 = TREE_CODE (t2);
374 /* Merge the attributes. */
375 attributes = targetm.merge_type_attributes (t1, t2);
377 /* If one is an enumerated type and the other is the compatible
378 integer type, the composite type might be either of the two
379 (DR#013 question 3). For consistency, use the enumerated type as
380 the composite type. */
382 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
383 return t1;
384 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
385 return t2;
387 gcc_assert (code1 == code2);
389 switch (code1)
391 case POINTER_TYPE:
392 /* For two pointers, do this recursively on the target type. */
394 tree pointed_to_1 = TREE_TYPE (t1);
395 tree pointed_to_2 = TREE_TYPE (t2);
396 tree target = composite_type (pointed_to_1, pointed_to_2);
397 t1 = build_pointer_type (target);
398 t1 = build_type_attribute_variant (t1, attributes);
399 return qualify_type (t1, t2);
402 case ARRAY_TYPE:
404 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
405 int quals;
406 tree unqual_elt;
407 tree d1 = TYPE_DOMAIN (t1);
408 tree d2 = TYPE_DOMAIN (t2);
409 bool d1_variable, d2_variable;
410 bool d1_zero, d2_zero;
411 bool t1_complete, t2_complete;
413 /* We should not have any type quals on arrays at all. */
414 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
415 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
417 t1_complete = COMPLETE_TYPE_P (t1);
418 t2_complete = COMPLETE_TYPE_P (t2);
420 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
421 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
423 d1_variable = (!d1_zero
424 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
425 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
426 d2_variable = (!d2_zero
427 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
428 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
429 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
430 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
432 /* Save space: see if the result is identical to one of the args. */
433 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
434 && (d2_variable || d2_zero || !d1_variable))
435 return build_type_attribute_variant (t1, attributes);
436 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
437 && (d1_variable || d1_zero || !d2_variable))
438 return build_type_attribute_variant (t2, attributes);
440 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
441 return build_type_attribute_variant (t1, attributes);
442 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t2, attributes);
445 /* Merge the element types, and have a size if either arg has
446 one. We may have qualifiers on the element types. To set
447 up TYPE_MAIN_VARIANT correctly, we need to form the
448 composite of the unqualified types and add the qualifiers
449 back at the end. */
450 quals = TYPE_QUALS (strip_array_types (elt));
451 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
452 t1 = build_array_type (unqual_elt,
453 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
454 && (d2_variable
455 || d2_zero
456 || !d1_variable))
457 ? t1
458 : t2));
459 /* Ensure a composite type involving a zero-length array type
460 is a zero-length type not an incomplete type. */
461 if (d1_zero && d2_zero
462 && (t1_complete || t2_complete)
463 && !COMPLETE_TYPE_P (t1))
465 TYPE_SIZE (t1) = bitsize_zero_node;
466 TYPE_SIZE_UNIT (t1) = size_zero_node;
468 t1 = c_build_qualified_type (t1, quals);
469 return build_type_attribute_variant (t1, attributes);
472 case ENUMERAL_TYPE:
473 case RECORD_TYPE:
474 case UNION_TYPE:
475 if (attributes != NULL)
477 /* Try harder not to create a new aggregate type. */
478 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
479 return t1;
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
481 return t2;
483 return build_type_attribute_variant (t1, attributes);
485 case FUNCTION_TYPE:
486 /* Function types: prefer the one that specified arg types.
487 If both do, merge the arg types. Also merge the return types. */
489 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
490 tree p1 = TYPE_ARG_TYPES (t1);
491 tree p2 = TYPE_ARG_TYPES (t2);
492 int len;
493 tree newargs, n;
494 int i;
496 /* Save space: see if the result is identical to one of the args. */
497 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
498 return build_type_attribute_variant (t1, attributes);
499 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
500 return build_type_attribute_variant (t2, attributes);
502 /* Simple way if one arg fails to specify argument types. */
503 if (TYPE_ARG_TYPES (t1) == 0)
505 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
506 t1 = build_type_attribute_variant (t1, attributes);
507 return qualify_type (t1, t2);
509 if (TYPE_ARG_TYPES (t2) == 0)
511 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
512 t1 = build_type_attribute_variant (t1, attributes);
513 return qualify_type (t1, t2);
516 /* If both args specify argument types, we must merge the two
517 lists, argument by argument. */
519 len = list_length (p1);
520 newargs = 0;
522 for (i = 0; i < len; i++)
523 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
525 n = newargs;
527 for (; p1;
528 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
530 /* A null type means arg type is not specified.
531 Take whatever the other function type has. */
532 if (TREE_VALUE (p1) == 0)
534 TREE_VALUE (n) = TREE_VALUE (p2);
535 goto parm_done;
537 if (TREE_VALUE (p2) == 0)
539 TREE_VALUE (n) = TREE_VALUE (p1);
540 goto parm_done;
543 /* Given wait (union {union wait *u; int *i} *)
544 and wait (union wait *),
545 prefer union wait * as type of parm. */
546 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
547 && TREE_VALUE (p1) != TREE_VALUE (p2))
549 tree memb;
550 tree mv2 = TREE_VALUE (p2);
551 if (mv2 && mv2 != error_mark_node
552 && TREE_CODE (mv2) != ARRAY_TYPE)
553 mv2 = TYPE_MAIN_VARIANT (mv2);
554 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
555 memb; memb = DECL_CHAIN (memb))
557 tree mv3 = TREE_TYPE (memb);
558 if (mv3 && mv3 != error_mark_node
559 && TREE_CODE (mv3) != ARRAY_TYPE)
560 mv3 = TYPE_MAIN_VARIANT (mv3);
561 if (comptypes (mv3, mv2))
563 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
564 TREE_VALUE (p2));
565 pedwarn (input_location, OPT_pedantic,
566 "function types not truly compatible in ISO C");
567 goto parm_done;
571 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
572 && TREE_VALUE (p2) != TREE_VALUE (p1))
574 tree memb;
575 tree mv1 = TREE_VALUE (p1);
576 if (mv1 && mv1 != error_mark_node
577 && TREE_CODE (mv1) != ARRAY_TYPE)
578 mv1 = TYPE_MAIN_VARIANT (mv1);
579 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
580 memb; memb = DECL_CHAIN (memb))
582 tree mv3 = TREE_TYPE (memb);
583 if (mv3 && mv3 != error_mark_node
584 && TREE_CODE (mv3) != ARRAY_TYPE)
585 mv3 = TYPE_MAIN_VARIANT (mv3);
586 if (comptypes (mv3, mv1))
588 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
589 TREE_VALUE (p1));
590 pedwarn (input_location, OPT_pedantic,
591 "function types not truly compatible in ISO C");
592 goto parm_done;
596 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
597 parm_done: ;
600 t1 = build_function_type (valtype, newargs);
601 t1 = qualify_type (t1, t2);
602 /* ... falls through ... */
605 default:
606 return build_type_attribute_variant (t1, attributes);
611 /* Return the type of a conditional expression between pointers to
612 possibly differently qualified versions of compatible types.
614 We assume that comp_target_types has already been done and returned
615 nonzero; if that isn't so, this may crash. */
617 static tree
618 common_pointer_type (tree t1, tree t2)
620 tree attributes;
621 tree pointed_to_1, mv1;
622 tree pointed_to_2, mv2;
623 tree target;
624 unsigned target_quals;
625 addr_space_t as1, as2, as_common;
626 int quals1, quals2;
628 /* Save time if the two types are the same. */
630 if (t1 == t2) return t1;
632 /* If one type is nonsense, use the other. */
633 if (t1 == error_mark_node)
634 return t2;
635 if (t2 == error_mark_node)
636 return t1;
638 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
639 && TREE_CODE (t2) == POINTER_TYPE);
641 /* Merge the attributes. */
642 attributes = targetm.merge_type_attributes (t1, t2);
644 /* Find the composite type of the target types, and combine the
645 qualifiers of the two types' targets. Do not lose qualifiers on
646 array element types by taking the TYPE_MAIN_VARIANT. */
647 mv1 = pointed_to_1 = TREE_TYPE (t1);
648 mv2 = pointed_to_2 = TREE_TYPE (t2);
649 if (TREE_CODE (mv1) != ARRAY_TYPE)
650 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
651 if (TREE_CODE (mv2) != ARRAY_TYPE)
652 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
653 target = composite_type (mv1, mv2);
655 /* For function types do not merge const qualifiers, but drop them
656 if used inconsistently. The middle-end uses these to mark const
657 and noreturn functions. */
658 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
659 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
661 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
662 target_quals = (quals1 & quals2);
663 else
664 target_quals = (quals1 | quals2);
666 /* If the two named address spaces are different, determine the common
667 superset address space. This is guaranteed to exist due to the
668 assumption that comp_target_type returned non-zero. */
669 as1 = TYPE_ADDR_SPACE (pointed_to_1);
670 as2 = TYPE_ADDR_SPACE (pointed_to_2);
671 if (!addr_space_superset (as1, as2, &as_common))
672 gcc_unreachable ();
674 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
676 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
677 return build_type_attribute_variant (t1, attributes);
680 /* Return the common type for two arithmetic types under the usual
681 arithmetic conversions. The default conversions have already been
682 applied, and enumerated types converted to their compatible integer
683 types. The resulting type is unqualified and has no attributes.
685 This is the type for the result of most arithmetic operations
686 if the operands have the given two types. */
688 static tree
689 c_common_type (tree t1, tree t2)
691 enum tree_code code1;
692 enum tree_code code2;
694 /* If one type is nonsense, use the other. */
695 if (t1 == error_mark_node)
696 return t2;
697 if (t2 == error_mark_node)
698 return t1;
700 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
701 t1 = TYPE_MAIN_VARIANT (t1);
703 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
704 t2 = TYPE_MAIN_VARIANT (t2);
706 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
707 t1 = build_type_attribute_variant (t1, NULL_TREE);
709 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
710 t2 = build_type_attribute_variant (t2, NULL_TREE);
712 /* Save time if the two types are the same. */
714 if (t1 == t2) return t1;
716 code1 = TREE_CODE (t1);
717 code2 = TREE_CODE (t2);
719 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
720 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
721 || code1 == INTEGER_TYPE);
722 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
723 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
724 || code2 == INTEGER_TYPE);
726 /* When one operand is a decimal float type, the other operand cannot be
727 a generic float type or a complex type. We also disallow vector types
728 here. */
729 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
730 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
732 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
734 error ("can%'t mix operands of decimal float and vector types");
735 return error_mark_node;
737 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
739 error ("can%'t mix operands of decimal float and complex types");
740 return error_mark_node;
742 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
744 error ("can%'t mix operands of decimal float and other float types");
745 return error_mark_node;
749 /* If one type is a vector type, return that type. (How the usual
750 arithmetic conversions apply to the vector types extension is not
751 precisely specified.) */
752 if (code1 == VECTOR_TYPE)
753 return t1;
755 if (code2 == VECTOR_TYPE)
756 return t2;
758 /* If one type is complex, form the common type of the non-complex
759 components, then make that complex. Use T1 or T2 if it is the
760 required type. */
761 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
763 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
764 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
765 tree subtype = c_common_type (subtype1, subtype2);
767 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
768 return t1;
769 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
770 return t2;
771 else
772 return build_complex_type (subtype);
775 /* If only one is real, use it as the result. */
777 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
778 return t1;
780 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
781 return t2;
783 /* If both are real and either are decimal floating point types, use
784 the decimal floating point type with the greater precision. */
786 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
788 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
789 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
790 return dfloat128_type_node;
791 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
792 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
793 return dfloat64_type_node;
794 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
795 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
796 return dfloat32_type_node;
799 /* Deal with fixed-point types. */
800 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
802 unsigned int unsignedp = 0, satp = 0;
803 enum machine_mode m1, m2;
804 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
806 m1 = TYPE_MODE (t1);
807 m2 = TYPE_MODE (t2);
809 /* If one input type is saturating, the result type is saturating. */
810 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
811 satp = 1;
813 /* If both fixed-point types are unsigned, the result type is unsigned.
814 When mixing fixed-point and integer types, follow the sign of the
815 fixed-point type.
816 Otherwise, the result type is signed. */
817 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
818 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
819 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
820 && TYPE_UNSIGNED (t1))
821 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
822 && TYPE_UNSIGNED (t2)))
823 unsignedp = 1;
825 /* The result type is signed. */
826 if (unsignedp == 0)
828 /* If the input type is unsigned, we need to convert to the
829 signed type. */
830 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
832 enum mode_class mclass = (enum mode_class) 0;
833 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
834 mclass = MODE_FRACT;
835 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
836 mclass = MODE_ACCUM;
837 else
838 gcc_unreachable ();
839 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
841 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
843 enum mode_class mclass = (enum mode_class) 0;
844 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
845 mclass = MODE_FRACT;
846 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
847 mclass = MODE_ACCUM;
848 else
849 gcc_unreachable ();
850 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
854 if (code1 == FIXED_POINT_TYPE)
856 fbit1 = GET_MODE_FBIT (m1);
857 ibit1 = GET_MODE_IBIT (m1);
859 else
861 fbit1 = 0;
862 /* Signed integers need to subtract one sign bit. */
863 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
866 if (code2 == FIXED_POINT_TYPE)
868 fbit2 = GET_MODE_FBIT (m2);
869 ibit2 = GET_MODE_IBIT (m2);
871 else
873 fbit2 = 0;
874 /* Signed integers need to subtract one sign bit. */
875 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
878 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
879 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
880 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
881 satp);
884 /* Both real or both integers; use the one with greater precision. */
886 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
887 return t1;
888 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
889 return t2;
891 /* Same precision. Prefer long longs to longs to ints when the
892 same precision, following the C99 rules on integer type rank
893 (which are equivalent to the C90 rules for C90 types). */
895 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
896 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
897 return long_long_unsigned_type_node;
899 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
900 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
902 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
903 return long_long_unsigned_type_node;
904 else
905 return long_long_integer_type_node;
908 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
910 return long_unsigned_type_node;
912 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
915 /* But preserve unsignedness from the other type,
916 since long cannot hold all the values of an unsigned int. */
917 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
918 return long_unsigned_type_node;
919 else
920 return long_integer_type_node;
923 /* Likewise, prefer long double to double even if same size. */
924 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
925 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
926 return long_double_type_node;
928 /* Otherwise prefer the unsigned one. */
930 if (TYPE_UNSIGNED (t1))
931 return t1;
932 else
933 return t2;
936 /* Wrapper around c_common_type that is used by c-common.c and other
937 front end optimizations that remove promotions. ENUMERAL_TYPEs
938 are allowed here and are converted to their compatible integer types.
939 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
940 preferably a non-Boolean type as the common type. */
941 tree
942 common_type (tree t1, tree t2)
944 if (TREE_CODE (t1) == ENUMERAL_TYPE)
945 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
946 if (TREE_CODE (t2) == ENUMERAL_TYPE)
947 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
949 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
950 if (TREE_CODE (t1) == BOOLEAN_TYPE
951 && TREE_CODE (t2) == BOOLEAN_TYPE)
952 return boolean_type_node;
954 /* If either type is BOOLEAN_TYPE, then return the other. */
955 if (TREE_CODE (t1) == BOOLEAN_TYPE)
956 return t2;
957 if (TREE_CODE (t2) == BOOLEAN_TYPE)
958 return t1;
960 return c_common_type (t1, t2);
963 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
964 or various other operations. Return 2 if they are compatible
965 but a warning may be needed if you use them together. */
968 comptypes (tree type1, tree type2)
970 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
971 int val;
973 val = comptypes_internal (type1, type2, NULL, NULL);
974 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
976 return val;
979 /* Like comptypes, but if it returns non-zero because enum and int are
980 compatible, it sets *ENUM_AND_INT_P to true. */
982 static int
983 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
985 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
986 int val;
988 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
989 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
991 return val;
994 /* Like comptypes, but if it returns nonzero for different types, it
995 sets *DIFFERENT_TYPES_P to true. */
998 comptypes_check_different_types (tree type1, tree type2,
999 bool *different_types_p)
1001 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1002 int val;
1004 val = comptypes_internal (type1, type2, NULL, different_types_p);
1005 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1007 return val;
1010 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1011 or various other operations. Return 2 if they are compatible
1012 but a warning may be needed if you use them together. If
1013 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1014 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1015 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1016 NULL, and the types are compatible but different enough not to be
1017 permitted in C1X typedef redeclarations, then this sets
1018 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1019 false, but may or may not be set if the types are incompatible.
1020 This differs from comptypes, in that we don't free the seen
1021 types. */
1023 static int
1024 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1025 bool *different_types_p)
1027 const_tree t1 = type1;
1028 const_tree t2 = type2;
1029 int attrval, val;
1031 /* Suppress errors caused by previously reported errors. */
1033 if (t1 == t2 || !t1 || !t2
1034 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1035 return 1;
1037 /* Enumerated types are compatible with integer types, but this is
1038 not transitive: two enumerated types in the same translation unit
1039 are compatible with each other only if they are the same type. */
1041 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1043 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1044 if (TREE_CODE (t2) != VOID_TYPE)
1046 if (enum_and_int_p != NULL)
1047 *enum_and_int_p = true;
1048 if (different_types_p != NULL)
1049 *different_types_p = true;
1052 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1054 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1055 if (TREE_CODE (t1) != VOID_TYPE)
1057 if (enum_and_int_p != NULL)
1058 *enum_and_int_p = true;
1059 if (different_types_p != NULL)
1060 *different_types_p = true;
1064 if (t1 == t2)
1065 return 1;
1067 /* Different classes of types can't be compatible. */
1069 if (TREE_CODE (t1) != TREE_CODE (t2))
1070 return 0;
1072 /* Qualifiers must match. C99 6.7.3p9 */
1074 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1075 return 0;
1077 /* Allow for two different type nodes which have essentially the same
1078 definition. Note that we already checked for equality of the type
1079 qualifiers (just above). */
1081 if (TREE_CODE (t1) != ARRAY_TYPE
1082 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1083 return 1;
1085 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1086 if (!(attrval = comp_type_attributes (t1, t2)))
1087 return 0;
1089 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1090 val = 0;
1092 switch (TREE_CODE (t1))
1094 case POINTER_TYPE:
1095 /* Do not remove mode or aliasing information. */
1096 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1097 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1098 break;
1099 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1100 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1101 enum_and_int_p, different_types_p));
1102 break;
1104 case FUNCTION_TYPE:
1105 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1106 different_types_p);
1107 break;
1109 case ARRAY_TYPE:
1111 tree d1 = TYPE_DOMAIN (t1);
1112 tree d2 = TYPE_DOMAIN (t2);
1113 bool d1_variable, d2_variable;
1114 bool d1_zero, d2_zero;
1115 val = 1;
1117 /* Target types must match incl. qualifiers. */
1118 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1119 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1120 enum_and_int_p,
1121 different_types_p)))
1122 return 0;
1124 if (different_types_p != NULL
1125 && (d1 == 0) != (d2 == 0))
1126 *different_types_p = true;
1127 /* Sizes must match unless one is missing or variable. */
1128 if (d1 == 0 || d2 == 0 || d1 == d2)
1129 break;
1131 d1_zero = !TYPE_MAX_VALUE (d1);
1132 d2_zero = !TYPE_MAX_VALUE (d2);
1134 d1_variable = (!d1_zero
1135 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1136 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1137 d2_variable = (!d2_zero
1138 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1139 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1140 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1141 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1143 if (different_types_p != NULL
1144 && d1_variable != d2_variable)
1145 *different_types_p = true;
1146 if (d1_variable || d2_variable)
1147 break;
1148 if (d1_zero && d2_zero)
1149 break;
1150 if (d1_zero || d2_zero
1151 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1152 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1153 val = 0;
1155 break;
1158 case ENUMERAL_TYPE:
1159 case RECORD_TYPE:
1160 case UNION_TYPE:
1161 if (val != 1 && !same_translation_unit_p (t1, t2))
1163 tree a1 = TYPE_ATTRIBUTES (t1);
1164 tree a2 = TYPE_ATTRIBUTES (t2);
1166 if (! attribute_list_contained (a1, a2)
1167 && ! attribute_list_contained (a2, a1))
1168 break;
1170 if (attrval != 2)
1171 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1172 different_types_p);
1173 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1174 different_types_p);
1176 break;
1178 case VECTOR_TYPE:
1179 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1180 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1181 enum_and_int_p, different_types_p));
1182 break;
1184 default:
1185 break;
1187 return attrval == 2 && val == 1 ? 2 : val;
1190 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1191 their qualifiers, except for named address spaces. If the pointers point to
1192 different named addresses, then we must determine if one address space is a
1193 subset of the other. */
1195 static int
1196 comp_target_types (location_t location, tree ttl, tree ttr)
1198 int val;
1199 tree mvl = TREE_TYPE (ttl);
1200 tree mvr = TREE_TYPE (ttr);
1201 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1202 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1203 addr_space_t as_common;
1204 bool enum_and_int_p;
1206 /* Fail if pointers point to incompatible address spaces. */
1207 if (!addr_space_superset (asl, asr, &as_common))
1208 return 0;
1210 /* Do not lose qualifiers on element types of array types that are
1211 pointer targets by taking their TYPE_MAIN_VARIANT. */
1212 if (TREE_CODE (mvl) != ARRAY_TYPE)
1213 mvl = TYPE_MAIN_VARIANT (mvl);
1214 if (TREE_CODE (mvr) != ARRAY_TYPE)
1215 mvr = TYPE_MAIN_VARIANT (mvr);
1216 enum_and_int_p = false;
1217 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1219 if (val == 2)
1220 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1222 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1223 warning_at (location, OPT_Wc___compat,
1224 "pointer target types incompatible in C++");
1226 return val;
1229 /* Subroutines of `comptypes'. */
1231 /* Determine whether two trees derive from the same translation unit.
1232 If the CONTEXT chain ends in a null, that tree's context is still
1233 being parsed, so if two trees have context chains ending in null,
1234 they're in the same translation unit. */
1236 same_translation_unit_p (const_tree t1, const_tree t2)
1238 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1239 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1241 case tcc_declaration:
1242 t1 = DECL_CONTEXT (t1); break;
1243 case tcc_type:
1244 t1 = TYPE_CONTEXT (t1); break;
1245 case tcc_exceptional:
1246 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1247 default: gcc_unreachable ();
1250 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1251 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1253 case tcc_declaration:
1254 t2 = DECL_CONTEXT (t2); break;
1255 case tcc_type:
1256 t2 = TYPE_CONTEXT (t2); break;
1257 case tcc_exceptional:
1258 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1259 default: gcc_unreachable ();
1262 return t1 == t2;
1265 /* Allocate the seen two types, assuming that they are compatible. */
1267 static struct tagged_tu_seen_cache *
1268 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1270 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1271 tu->next = tagged_tu_seen_base;
1272 tu->t1 = t1;
1273 tu->t2 = t2;
1275 tagged_tu_seen_base = tu;
1277 /* The C standard says that two structures in different translation
1278 units are compatible with each other only if the types of their
1279 fields are compatible (among other things). We assume that they
1280 are compatible until proven otherwise when building the cache.
1281 An example where this can occur is:
1282 struct a
1284 struct a *next;
1286 If we are comparing this against a similar struct in another TU,
1287 and did not assume they were compatible, we end up with an infinite
1288 loop. */
1289 tu->val = 1;
1290 return tu;
1293 /* Free the seen types until we get to TU_TIL. */
1295 static void
1296 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1298 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1299 while (tu != tu_til)
1301 const struct tagged_tu_seen_cache *const tu1
1302 = (const struct tagged_tu_seen_cache *) tu;
1303 tu = tu1->next;
1304 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1306 tagged_tu_seen_base = tu_til;
1309 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1310 compatible. If the two types are not the same (which has been
1311 checked earlier), this can only happen when multiple translation
1312 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1313 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1314 comptypes_internal. */
1316 static int
1317 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1318 bool *enum_and_int_p, bool *different_types_p)
1320 tree s1, s2;
1321 bool needs_warning = false;
1323 /* We have to verify that the tags of the types are the same. This
1324 is harder than it looks because this may be a typedef, so we have
1325 to go look at the original type. It may even be a typedef of a
1326 typedef...
1327 In the case of compiler-created builtin structs the TYPE_DECL
1328 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1329 while (TYPE_NAME (t1)
1330 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1331 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1332 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1334 while (TYPE_NAME (t2)
1335 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1336 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1337 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1339 /* C90 didn't have the requirement that the two tags be the same. */
1340 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1341 return 0;
1343 /* C90 didn't say what happened if one or both of the types were
1344 incomplete; we choose to follow C99 rules here, which is that they
1345 are compatible. */
1346 if (TYPE_SIZE (t1) == NULL
1347 || TYPE_SIZE (t2) == NULL)
1348 return 1;
1351 const struct tagged_tu_seen_cache * tts_i;
1352 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1353 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1354 return tts_i->val;
1357 switch (TREE_CODE (t1))
1359 case ENUMERAL_TYPE:
1361 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1362 /* Speed up the case where the type values are in the same order. */
1363 tree tv1 = TYPE_VALUES (t1);
1364 tree tv2 = TYPE_VALUES (t2);
1366 if (tv1 == tv2)
1368 return 1;
1371 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1373 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1374 break;
1375 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1377 tu->val = 0;
1378 return 0;
1382 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1384 return 1;
1386 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1388 tu->val = 0;
1389 return 0;
1392 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1394 tu->val = 0;
1395 return 0;
1398 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1400 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1401 if (s2 == NULL
1402 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1404 tu->val = 0;
1405 return 0;
1408 return 1;
1411 case UNION_TYPE:
1413 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1414 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1416 tu->val = 0;
1417 return 0;
1420 /* Speed up the common case where the fields are in the same order. */
1421 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1422 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1424 int result;
1426 if (DECL_NAME (s1) != DECL_NAME (s2))
1427 break;
1428 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1429 enum_and_int_p, different_types_p);
1431 if (result != 1 && !DECL_NAME (s1))
1432 break;
1433 if (result == 0)
1435 tu->val = 0;
1436 return 0;
1438 if (result == 2)
1439 needs_warning = true;
1441 if (TREE_CODE (s1) == FIELD_DECL
1442 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1443 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1445 tu->val = 0;
1446 return 0;
1449 if (!s1 && !s2)
1451 tu->val = needs_warning ? 2 : 1;
1452 return tu->val;
1455 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1457 bool ok = false;
1459 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1460 if (DECL_NAME (s1) == DECL_NAME (s2))
1462 int result;
1464 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1465 enum_and_int_p,
1466 different_types_p);
1468 if (result != 1 && !DECL_NAME (s1))
1469 continue;
1470 if (result == 0)
1472 tu->val = 0;
1473 return 0;
1475 if (result == 2)
1476 needs_warning = true;
1478 if (TREE_CODE (s1) == FIELD_DECL
1479 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1480 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1481 break;
1483 ok = true;
1484 break;
1486 if (!ok)
1488 tu->val = 0;
1489 return 0;
1492 tu->val = needs_warning ? 2 : 10;
1493 return tu->val;
1496 case RECORD_TYPE:
1498 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1500 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1501 s1 && s2;
1502 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1504 int result;
1505 if (TREE_CODE (s1) != TREE_CODE (s2)
1506 || DECL_NAME (s1) != DECL_NAME (s2))
1507 break;
1508 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1509 enum_and_int_p, different_types_p);
1510 if (result == 0)
1511 break;
1512 if (result == 2)
1513 needs_warning = true;
1515 if (TREE_CODE (s1) == FIELD_DECL
1516 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1517 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1518 break;
1520 if (s1 && s2)
1521 tu->val = 0;
1522 else
1523 tu->val = needs_warning ? 2 : 1;
1524 return tu->val;
1527 default:
1528 gcc_unreachable ();
1532 /* Return 1 if two function types F1 and F2 are compatible.
1533 If either type specifies no argument types,
1534 the other must specify a fixed number of self-promoting arg types.
1535 Otherwise, if one type specifies only the number of arguments,
1536 the other must specify that number of self-promoting arg types.
1537 Otherwise, the argument types must match.
1538 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1540 static int
1541 function_types_compatible_p (const_tree f1, const_tree f2,
1542 bool *enum_and_int_p, bool *different_types_p)
1544 tree args1, args2;
1545 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1546 int val = 1;
1547 int val1;
1548 tree ret1, ret2;
1550 ret1 = TREE_TYPE (f1);
1551 ret2 = TREE_TYPE (f2);
1553 /* 'volatile' qualifiers on a function's return type used to mean
1554 the function is noreturn. */
1555 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1556 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1557 if (TYPE_VOLATILE (ret1))
1558 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1559 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1560 if (TYPE_VOLATILE (ret2))
1561 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1562 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1563 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1564 if (val == 0)
1565 return 0;
1567 args1 = TYPE_ARG_TYPES (f1);
1568 args2 = TYPE_ARG_TYPES (f2);
1570 if (different_types_p != NULL
1571 && (args1 == 0) != (args2 == 0))
1572 *different_types_p = true;
1574 /* An unspecified parmlist matches any specified parmlist
1575 whose argument types don't need default promotions. */
1577 if (args1 == 0)
1579 if (!self_promoting_args_p (args2))
1580 return 0;
1581 /* If one of these types comes from a non-prototype fn definition,
1582 compare that with the other type's arglist.
1583 If they don't match, ask for a warning (but no error). */
1584 if (TYPE_ACTUAL_ARG_TYPES (f1)
1585 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1586 enum_and_int_p, different_types_p))
1587 val = 2;
1588 return val;
1590 if (args2 == 0)
1592 if (!self_promoting_args_p (args1))
1593 return 0;
1594 if (TYPE_ACTUAL_ARG_TYPES (f2)
1595 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1596 enum_and_int_p, different_types_p))
1597 val = 2;
1598 return val;
1601 /* Both types have argument lists: compare them and propagate results. */
1602 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1603 different_types_p);
1604 return val1 != 1 ? val1 : val;
1607 /* Check two lists of types for compatibility, returning 0 for
1608 incompatible, 1 for compatible, or 2 for compatible with
1609 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1610 comptypes_internal. */
1612 static int
1613 type_lists_compatible_p (const_tree args1, const_tree args2,
1614 bool *enum_and_int_p, bool *different_types_p)
1616 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1617 int val = 1;
1618 int newval = 0;
1620 while (1)
1622 tree a1, mv1, a2, mv2;
1623 if (args1 == 0 && args2 == 0)
1624 return val;
1625 /* If one list is shorter than the other,
1626 they fail to match. */
1627 if (args1 == 0 || args2 == 0)
1628 return 0;
1629 mv1 = a1 = TREE_VALUE (args1);
1630 mv2 = a2 = TREE_VALUE (args2);
1631 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1632 mv1 = TYPE_MAIN_VARIANT (mv1);
1633 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1634 mv2 = TYPE_MAIN_VARIANT (mv2);
1635 /* A null pointer instead of a type
1636 means there is supposed to be an argument
1637 but nothing is specified about what type it has.
1638 So match anything that self-promotes. */
1639 if (different_types_p != NULL
1640 && (a1 == 0) != (a2 == 0))
1641 *different_types_p = true;
1642 if (a1 == 0)
1644 if (c_type_promotes_to (a2) != a2)
1645 return 0;
1647 else if (a2 == 0)
1649 if (c_type_promotes_to (a1) != a1)
1650 return 0;
1652 /* If one of the lists has an error marker, ignore this arg. */
1653 else if (TREE_CODE (a1) == ERROR_MARK
1654 || TREE_CODE (a2) == ERROR_MARK)
1656 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1657 different_types_p)))
1659 if (different_types_p != NULL)
1660 *different_types_p = true;
1661 /* Allow wait (union {union wait *u; int *i} *)
1662 and wait (union wait *) to be compatible. */
1663 if (TREE_CODE (a1) == UNION_TYPE
1664 && (TYPE_NAME (a1) == 0
1665 || TYPE_TRANSPARENT_AGGR (a1))
1666 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1667 && tree_int_cst_equal (TYPE_SIZE (a1),
1668 TYPE_SIZE (a2)))
1670 tree memb;
1671 for (memb = TYPE_FIELDS (a1);
1672 memb; memb = DECL_CHAIN (memb))
1674 tree mv3 = TREE_TYPE (memb);
1675 if (mv3 && mv3 != error_mark_node
1676 && TREE_CODE (mv3) != ARRAY_TYPE)
1677 mv3 = TYPE_MAIN_VARIANT (mv3);
1678 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1679 different_types_p))
1680 break;
1682 if (memb == 0)
1683 return 0;
1685 else if (TREE_CODE (a2) == UNION_TYPE
1686 && (TYPE_NAME (a2) == 0
1687 || TYPE_TRANSPARENT_AGGR (a2))
1688 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1689 && tree_int_cst_equal (TYPE_SIZE (a2),
1690 TYPE_SIZE (a1)))
1692 tree memb;
1693 for (memb = TYPE_FIELDS (a2);
1694 memb; memb = DECL_CHAIN (memb))
1696 tree mv3 = TREE_TYPE (memb);
1697 if (mv3 && mv3 != error_mark_node
1698 && TREE_CODE (mv3) != ARRAY_TYPE)
1699 mv3 = TYPE_MAIN_VARIANT (mv3);
1700 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1701 different_types_p))
1702 break;
1704 if (memb == 0)
1705 return 0;
1707 else
1708 return 0;
1711 /* comptypes said ok, but record if it said to warn. */
1712 if (newval > val)
1713 val = newval;
1715 args1 = TREE_CHAIN (args1);
1716 args2 = TREE_CHAIN (args2);
1720 /* Compute the size to increment a pointer by. */
1722 static tree
1723 c_size_in_bytes (const_tree type)
1725 enum tree_code code = TREE_CODE (type);
1727 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1728 return size_one_node;
1730 if (!COMPLETE_OR_VOID_TYPE_P (type))
1732 error ("arithmetic on pointer to an incomplete type");
1733 return size_one_node;
1736 /* Convert in case a char is more than one unit. */
1737 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1738 size_int (TYPE_PRECISION (char_type_node)
1739 / BITS_PER_UNIT));
1742 /* Return either DECL or its known constant value (if it has one). */
1744 tree
1745 decl_constant_value (tree decl)
1747 if (/* Don't change a variable array bound or initial value to a constant
1748 in a place where a variable is invalid. Note that DECL_INITIAL
1749 isn't valid for a PARM_DECL. */
1750 current_function_decl != 0
1751 && TREE_CODE (decl) != PARM_DECL
1752 && !TREE_THIS_VOLATILE (decl)
1753 && TREE_READONLY (decl)
1754 && DECL_INITIAL (decl) != 0
1755 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1756 /* This is invalid if initial value is not constant.
1757 If it has either a function call, a memory reference,
1758 or a variable, then re-evaluating it could give different results. */
1759 && TREE_CONSTANT (DECL_INITIAL (decl))
1760 /* Check for cases where this is sub-optimal, even though valid. */
1761 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1762 return DECL_INITIAL (decl);
1763 return decl;
1766 /* Convert the array expression EXP to a pointer. */
1767 static tree
1768 array_to_pointer_conversion (location_t loc, tree exp)
1770 tree orig_exp = exp;
1771 tree type = TREE_TYPE (exp);
1772 tree adr;
1773 tree restype = TREE_TYPE (type);
1774 tree ptrtype;
1776 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1778 STRIP_TYPE_NOPS (exp);
1780 if (TREE_NO_WARNING (orig_exp))
1781 TREE_NO_WARNING (exp) = 1;
1783 ptrtype = build_pointer_type (restype);
1785 if (TREE_CODE (exp) == INDIRECT_REF)
1786 return convert (ptrtype, TREE_OPERAND (exp, 0));
1788 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1789 return convert (ptrtype, adr);
1792 /* Convert the function expression EXP to a pointer. */
1793 static tree
1794 function_to_pointer_conversion (location_t loc, tree exp)
1796 tree orig_exp = exp;
1798 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1800 STRIP_TYPE_NOPS (exp);
1802 if (TREE_NO_WARNING (orig_exp))
1803 TREE_NO_WARNING (exp) = 1;
1805 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1808 /* Mark EXP as read, not just set, for set but not used -Wunused
1809 warning purposes. */
1811 void
1812 mark_exp_read (tree exp)
1814 switch (TREE_CODE (exp))
1816 case VAR_DECL:
1817 case PARM_DECL:
1818 DECL_READ_P (exp) = 1;
1819 break;
1820 case ARRAY_REF:
1821 case COMPONENT_REF:
1822 case MODIFY_EXPR:
1823 case REALPART_EXPR:
1824 case IMAGPART_EXPR:
1825 CASE_CONVERT:
1826 case ADDR_EXPR:
1827 mark_exp_read (TREE_OPERAND (exp, 0));
1828 break;
1829 case COMPOUND_EXPR:
1830 case C_MAYBE_CONST_EXPR:
1831 mark_exp_read (TREE_OPERAND (exp, 1));
1832 break;
1833 default:
1834 break;
1838 /* Perform the default conversion of arrays and functions to pointers.
1839 Return the result of converting EXP. For any other expression, just
1840 return EXP.
1842 LOC is the location of the expression. */
1844 struct c_expr
1845 default_function_array_conversion (location_t loc, struct c_expr exp)
1847 tree orig_exp = exp.value;
1848 tree type = TREE_TYPE (exp.value);
1849 enum tree_code code = TREE_CODE (type);
1851 switch (code)
1853 case ARRAY_TYPE:
1855 bool not_lvalue = false;
1856 bool lvalue_array_p;
1858 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1859 || CONVERT_EXPR_P (exp.value))
1860 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1862 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1863 not_lvalue = true;
1864 exp.value = TREE_OPERAND (exp.value, 0);
1867 if (TREE_NO_WARNING (orig_exp))
1868 TREE_NO_WARNING (exp.value) = 1;
1870 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1871 if (!flag_isoc99 && !lvalue_array_p)
1873 /* Before C99, non-lvalue arrays do not decay to pointers.
1874 Normally, using such an array would be invalid; but it can
1875 be used correctly inside sizeof or as a statement expression.
1876 Thus, do not give an error here; an error will result later. */
1877 return exp;
1880 exp.value = array_to_pointer_conversion (loc, exp.value);
1882 break;
1883 case FUNCTION_TYPE:
1884 exp.value = function_to_pointer_conversion (loc, exp.value);
1885 break;
1886 default:
1887 break;
1890 return exp;
1893 struct c_expr
1894 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1896 mark_exp_read (exp.value);
1897 return default_function_array_conversion (loc, exp);
1900 /* EXP is an expression of integer type. Apply the integer promotions
1901 to it and return the promoted value. */
1903 tree
1904 perform_integral_promotions (tree exp)
1906 tree type = TREE_TYPE (exp);
1907 enum tree_code code = TREE_CODE (type);
1909 gcc_assert (INTEGRAL_TYPE_P (type));
1911 /* Normally convert enums to int,
1912 but convert wide enums to something wider. */
1913 if (code == ENUMERAL_TYPE)
1915 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1916 TYPE_PRECISION (integer_type_node)),
1917 ((TYPE_PRECISION (type)
1918 >= TYPE_PRECISION (integer_type_node))
1919 && TYPE_UNSIGNED (type)));
1921 return convert (type, exp);
1924 /* ??? This should no longer be needed now bit-fields have their
1925 proper types. */
1926 if (TREE_CODE (exp) == COMPONENT_REF
1927 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1928 /* If it's thinner than an int, promote it like a
1929 c_promoting_integer_type_p, otherwise leave it alone. */
1930 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1931 TYPE_PRECISION (integer_type_node)))
1932 return convert (integer_type_node, exp);
1934 if (c_promoting_integer_type_p (type))
1936 /* Preserve unsignedness if not really getting any wider. */
1937 if (TYPE_UNSIGNED (type)
1938 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1939 return convert (unsigned_type_node, exp);
1941 return convert (integer_type_node, exp);
1944 return exp;
1948 /* Perform default promotions for C data used in expressions.
1949 Enumeral types or short or char are converted to int.
1950 In addition, manifest constants symbols are replaced by their values. */
1952 tree
1953 default_conversion (tree exp)
1955 tree orig_exp;
1956 tree type = TREE_TYPE (exp);
1957 enum tree_code code = TREE_CODE (type);
1958 tree promoted_type;
1960 mark_exp_read (exp);
1962 /* Functions and arrays have been converted during parsing. */
1963 gcc_assert (code != FUNCTION_TYPE);
1964 if (code == ARRAY_TYPE)
1965 return exp;
1967 /* Constants can be used directly unless they're not loadable. */
1968 if (TREE_CODE (exp) == CONST_DECL)
1969 exp = DECL_INITIAL (exp);
1971 /* Strip no-op conversions. */
1972 orig_exp = exp;
1973 STRIP_TYPE_NOPS (exp);
1975 if (TREE_NO_WARNING (orig_exp))
1976 TREE_NO_WARNING (exp) = 1;
1978 if (code == VOID_TYPE)
1980 error ("void value not ignored as it ought to be");
1981 return error_mark_node;
1984 exp = require_complete_type (exp);
1985 if (exp == error_mark_node)
1986 return error_mark_node;
1988 promoted_type = targetm.promoted_type (type);
1989 if (promoted_type)
1990 return convert (promoted_type, exp);
1992 if (INTEGRAL_TYPE_P (type))
1993 return perform_integral_promotions (exp);
1995 return exp;
1998 /* Look up COMPONENT in a structure or union TYPE.
2000 If the component name is not found, returns NULL_TREE. Otherwise,
2001 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2002 stepping down the chain to the component, which is in the last
2003 TREE_VALUE of the list. Normally the list is of length one, but if
2004 the component is embedded within (nested) anonymous structures or
2005 unions, the list steps down the chain to the component. */
2007 static tree
2008 lookup_field (tree type, tree component)
2010 tree field;
2012 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2013 to the field elements. Use a binary search on this array to quickly
2014 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2015 will always be set for structures which have many elements. */
2017 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2019 int bot, top, half;
2020 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2022 field = TYPE_FIELDS (type);
2023 bot = 0;
2024 top = TYPE_LANG_SPECIFIC (type)->s->len;
2025 while (top - bot > 1)
2027 half = (top - bot + 1) >> 1;
2028 field = field_array[bot+half];
2030 if (DECL_NAME (field) == NULL_TREE)
2032 /* Step through all anon unions in linear fashion. */
2033 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2035 field = field_array[bot++];
2036 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2037 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2039 tree anon = lookup_field (TREE_TYPE (field), component);
2041 if (anon)
2042 return tree_cons (NULL_TREE, field, anon);
2044 /* The Plan 9 compiler permits referring
2045 directly to an anonymous struct/union field
2046 using a typedef name. */
2047 if (flag_plan9_extensions
2048 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2049 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2050 == TYPE_DECL)
2051 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2052 == component))
2053 break;
2057 /* Entire record is only anon unions. */
2058 if (bot > top)
2059 return NULL_TREE;
2061 /* Restart the binary search, with new lower bound. */
2062 continue;
2065 if (DECL_NAME (field) == component)
2066 break;
2067 if (DECL_NAME (field) < component)
2068 bot += half;
2069 else
2070 top = bot + half;
2073 if (DECL_NAME (field_array[bot]) == component)
2074 field = field_array[bot];
2075 else if (DECL_NAME (field) != component)
2076 return NULL_TREE;
2078 else
2080 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2082 if (DECL_NAME (field) == NULL_TREE
2083 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2084 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2086 tree anon = lookup_field (TREE_TYPE (field), component);
2088 if (anon)
2089 return tree_cons (NULL_TREE, field, anon);
2091 /* The Plan 9 compiler permits referring directly to an
2092 anonymous struct/union field using a typedef
2093 name. */
2094 if (flag_plan9_extensions
2095 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2096 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2097 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2098 == component))
2099 break;
2102 if (DECL_NAME (field) == component)
2103 break;
2106 if (field == NULL_TREE)
2107 return NULL_TREE;
2110 return tree_cons (NULL_TREE, field, NULL_TREE);
2113 /* Make an expression to refer to the COMPONENT field of structure or
2114 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2115 location of the COMPONENT_REF. */
2117 tree
2118 build_component_ref (location_t loc, tree datum, tree component)
2120 tree type = TREE_TYPE (datum);
2121 enum tree_code code = TREE_CODE (type);
2122 tree field = NULL;
2123 tree ref;
2124 bool datum_lvalue = lvalue_p (datum);
2126 if (!objc_is_public (datum, component))
2127 return error_mark_node;
2129 /* Detect Objective-C property syntax object.property. */
2130 if (c_dialect_objc ()
2131 && (ref = objc_maybe_build_component_ref (datum, component)))
2132 return ref;
2134 /* See if there is a field or component with name COMPONENT. */
2136 if (code == RECORD_TYPE || code == UNION_TYPE)
2138 if (!COMPLETE_TYPE_P (type))
2140 c_incomplete_type_error (NULL_TREE, type);
2141 return error_mark_node;
2144 field = lookup_field (type, component);
2146 if (!field)
2148 error_at (loc, "%qT has no member named %qE", type, component);
2149 return error_mark_node;
2152 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2153 This might be better solved in future the way the C++ front
2154 end does it - by giving the anonymous entities each a
2155 separate name and type, and then have build_component_ref
2156 recursively call itself. We can't do that here. */
2159 tree subdatum = TREE_VALUE (field);
2160 int quals;
2161 tree subtype;
2162 bool use_datum_quals;
2164 if (TREE_TYPE (subdatum) == error_mark_node)
2165 return error_mark_node;
2167 /* If this is an rvalue, it does not have qualifiers in C
2168 standard terms and we must avoid propagating such
2169 qualifiers down to a non-lvalue array that is then
2170 converted to a pointer. */
2171 use_datum_quals = (datum_lvalue
2172 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2174 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2175 if (use_datum_quals)
2176 quals |= TYPE_QUALS (TREE_TYPE (datum));
2177 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2179 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2180 NULL_TREE);
2181 SET_EXPR_LOCATION (ref, loc);
2182 if (TREE_READONLY (subdatum)
2183 || (use_datum_quals && TREE_READONLY (datum)))
2184 TREE_READONLY (ref) = 1;
2185 if (TREE_THIS_VOLATILE (subdatum)
2186 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2187 TREE_THIS_VOLATILE (ref) = 1;
2189 if (TREE_DEPRECATED (subdatum))
2190 warn_deprecated_use (subdatum, NULL_TREE);
2192 datum = ref;
2194 field = TREE_CHAIN (field);
2196 while (field);
2198 return ref;
2200 else if (code != ERROR_MARK)
2201 error_at (loc,
2202 "request for member %qE in something not a structure or union",
2203 component);
2205 return error_mark_node;
2208 /* Given an expression PTR for a pointer, return an expression
2209 for the value pointed to.
2210 ERRORSTRING is the name of the operator to appear in error messages.
2212 LOC is the location to use for the generated tree. */
2214 tree
2215 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2217 tree pointer = default_conversion (ptr);
2218 tree type = TREE_TYPE (pointer);
2219 tree ref;
2221 if (TREE_CODE (type) == POINTER_TYPE)
2223 if (CONVERT_EXPR_P (pointer)
2224 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2226 /* If a warning is issued, mark it to avoid duplicates from
2227 the backend. This only needs to be done at
2228 warn_strict_aliasing > 2. */
2229 if (warn_strict_aliasing > 2)
2230 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2231 type, TREE_OPERAND (pointer, 0)))
2232 TREE_NO_WARNING (pointer) = 1;
2235 if (TREE_CODE (pointer) == ADDR_EXPR
2236 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2237 == TREE_TYPE (type)))
2239 ref = TREE_OPERAND (pointer, 0);
2240 protected_set_expr_location (ref, loc);
2241 return ref;
2243 else
2245 tree t = TREE_TYPE (type);
2247 ref = build1 (INDIRECT_REF, t, pointer);
2249 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2251 error_at (loc, "dereferencing pointer to incomplete type");
2252 return error_mark_node;
2254 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2255 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2257 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2258 so that we get the proper error message if the result is used
2259 to assign to. Also, &* is supposed to be a no-op.
2260 And ANSI C seems to specify that the type of the result
2261 should be the const type. */
2262 /* A de-reference of a pointer to const is not a const. It is valid
2263 to change it via some other pointer. */
2264 TREE_READONLY (ref) = TYPE_READONLY (t);
2265 TREE_SIDE_EFFECTS (ref)
2266 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2267 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2268 protected_set_expr_location (ref, loc);
2269 return ref;
2272 else if (TREE_CODE (pointer) != ERROR_MARK)
2273 invalid_indirection_error (loc, type, errstring);
2275 return error_mark_node;
2278 /* This handles expressions of the form "a[i]", which denotes
2279 an array reference.
2281 This is logically equivalent in C to *(a+i), but we may do it differently.
2282 If A is a variable or a member, we generate a primitive ARRAY_REF.
2283 This avoids forcing the array out of registers, and can work on
2284 arrays that are not lvalues (for example, members of structures returned
2285 by functions).
2287 For vector types, allow vector[i] but not i[vector], and create
2288 *(((type*)&vectortype) + i) for the expression.
2290 LOC is the location to use for the returned expression. */
2292 tree
2293 build_array_ref (location_t loc, tree array, tree index)
2295 tree ret;
2296 bool swapped = false;
2297 if (TREE_TYPE (array) == error_mark_node
2298 || TREE_TYPE (index) == error_mark_node)
2299 return error_mark_node;
2301 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2302 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2303 /* Allow vector[index] but not index[vector]. */
2304 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2306 tree temp;
2307 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2308 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2310 error_at (loc,
2311 "subscripted value is neither array nor pointer nor vector");
2313 return error_mark_node;
2315 temp = array;
2316 array = index;
2317 index = temp;
2318 swapped = true;
2321 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2323 error_at (loc, "array subscript is not an integer");
2324 return error_mark_node;
2327 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2329 error_at (loc, "subscripted value is pointer to function");
2330 return error_mark_node;
2333 /* ??? Existing practice has been to warn only when the char
2334 index is syntactically the index, not for char[array]. */
2335 if (!swapped)
2336 warn_array_subscript_with_type_char (index);
2338 /* Apply default promotions *after* noticing character types. */
2339 index = default_conversion (index);
2341 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2343 /* For vector[index], convert the vector to a
2344 pointer of the underlying type. */
2345 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2347 tree type = TREE_TYPE (array);
2348 tree type1;
2350 if (TREE_CODE (index) == INTEGER_CST)
2351 if (!host_integerp (index, 1)
2352 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2353 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2354 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2356 c_common_mark_addressable_vec (array);
2357 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2358 type = build_pointer_type (type);
2359 type1 = build_pointer_type (TREE_TYPE (array));
2360 array = build1 (ADDR_EXPR, type1, array);
2361 array = convert (type, array);
2364 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2366 tree rval, type;
2368 /* An array that is indexed by a non-constant
2369 cannot be stored in a register; we must be able to do
2370 address arithmetic on its address.
2371 Likewise an array of elements of variable size. */
2372 if (TREE_CODE (index) != INTEGER_CST
2373 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2374 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2376 if (!c_mark_addressable (array))
2377 return error_mark_node;
2379 /* An array that is indexed by a constant value which is not within
2380 the array bounds cannot be stored in a register either; because we
2381 would get a crash in store_bit_field/extract_bit_field when trying
2382 to access a non-existent part of the register. */
2383 if (TREE_CODE (index) == INTEGER_CST
2384 && TYPE_DOMAIN (TREE_TYPE (array))
2385 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2387 if (!c_mark_addressable (array))
2388 return error_mark_node;
2391 if (pedantic)
2393 tree foo = array;
2394 while (TREE_CODE (foo) == COMPONENT_REF)
2395 foo = TREE_OPERAND (foo, 0);
2396 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2397 pedwarn (loc, OPT_pedantic,
2398 "ISO C forbids subscripting %<register%> array");
2399 else if (!flag_isoc99 && !lvalue_p (foo))
2400 pedwarn (loc, OPT_pedantic,
2401 "ISO C90 forbids subscripting non-lvalue array");
2404 type = TREE_TYPE (TREE_TYPE (array));
2405 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2406 /* Array ref is const/volatile if the array elements are
2407 or if the array is. */
2408 TREE_READONLY (rval)
2409 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2410 | TREE_READONLY (array));
2411 TREE_SIDE_EFFECTS (rval)
2412 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2413 | TREE_SIDE_EFFECTS (array));
2414 TREE_THIS_VOLATILE (rval)
2415 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2416 /* This was added by rms on 16 Nov 91.
2417 It fixes vol struct foo *a; a->elts[1]
2418 in an inline function.
2419 Hope it doesn't break something else. */
2420 | TREE_THIS_VOLATILE (array));
2421 ret = require_complete_type (rval);
2422 protected_set_expr_location (ret, loc);
2423 return ret;
2425 else
2427 tree ar = default_conversion (array);
2429 if (ar == error_mark_node)
2430 return ar;
2432 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2433 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2435 return build_indirect_ref
2436 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2437 RO_ARRAY_INDEXING);
2441 /* Build an external reference to identifier ID. FUN indicates
2442 whether this will be used for a function call. LOC is the source
2443 location of the identifier. This sets *TYPE to the type of the
2444 identifier, which is not the same as the type of the returned value
2445 for CONST_DECLs defined as enum constants. If the type of the
2446 identifier is not available, *TYPE is set to NULL. */
2447 tree
2448 build_external_ref (location_t loc, tree id, int fun, tree *type)
2450 tree ref;
2451 tree decl = lookup_name (id);
2453 /* In Objective-C, an instance variable (ivar) may be preferred to
2454 whatever lookup_name() found. */
2455 decl = objc_lookup_ivar (decl, id);
2457 *type = NULL;
2458 if (decl && decl != error_mark_node)
2460 ref = decl;
2461 *type = TREE_TYPE (ref);
2463 else if (fun)
2464 /* Implicit function declaration. */
2465 ref = implicitly_declare (loc, id);
2466 else if (decl == error_mark_node)
2467 /* Don't complain about something that's already been
2468 complained about. */
2469 return error_mark_node;
2470 else
2472 undeclared_variable (loc, id);
2473 return error_mark_node;
2476 if (TREE_TYPE (ref) == error_mark_node)
2477 return error_mark_node;
2479 if (TREE_DEPRECATED (ref))
2480 warn_deprecated_use (ref, NULL_TREE);
2482 /* Recursive call does not count as usage. */
2483 if (ref != current_function_decl)
2485 TREE_USED (ref) = 1;
2488 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2490 if (!in_sizeof && !in_typeof)
2491 C_DECL_USED (ref) = 1;
2492 else if (DECL_INITIAL (ref) == 0
2493 && DECL_EXTERNAL (ref)
2494 && !TREE_PUBLIC (ref))
2495 record_maybe_used_decl (ref);
2498 if (TREE_CODE (ref) == CONST_DECL)
2500 used_types_insert (TREE_TYPE (ref));
2502 if (warn_cxx_compat
2503 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2504 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2506 warning_at (loc, OPT_Wc___compat,
2507 ("enum constant defined in struct or union "
2508 "is not visible in C++"));
2509 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2512 ref = DECL_INITIAL (ref);
2513 TREE_CONSTANT (ref) = 1;
2515 else if (current_function_decl != 0
2516 && !DECL_FILE_SCOPE_P (current_function_decl)
2517 && (TREE_CODE (ref) == VAR_DECL
2518 || TREE_CODE (ref) == PARM_DECL
2519 || TREE_CODE (ref) == FUNCTION_DECL))
2521 tree context = decl_function_context (ref);
2523 if (context != 0 && context != current_function_decl)
2524 DECL_NONLOCAL (ref) = 1;
2526 /* C99 6.7.4p3: An inline definition of a function with external
2527 linkage ... shall not contain a reference to an identifier with
2528 internal linkage. */
2529 else if (current_function_decl != 0
2530 && DECL_DECLARED_INLINE_P (current_function_decl)
2531 && DECL_EXTERNAL (current_function_decl)
2532 && VAR_OR_FUNCTION_DECL_P (ref)
2533 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2534 && ! TREE_PUBLIC (ref)
2535 && DECL_CONTEXT (ref) != current_function_decl)
2536 record_inline_static (loc, current_function_decl, ref,
2537 csi_internal);
2539 return ref;
2542 /* Record details of decls possibly used inside sizeof or typeof. */
2543 struct maybe_used_decl
2545 /* The decl. */
2546 tree decl;
2547 /* The level seen at (in_sizeof + in_typeof). */
2548 int level;
2549 /* The next one at this level or above, or NULL. */
2550 struct maybe_used_decl *next;
2553 static struct maybe_used_decl *maybe_used_decls;
2555 /* Record that DECL, an undefined static function reference seen
2556 inside sizeof or typeof, might be used if the operand of sizeof is
2557 a VLA type or the operand of typeof is a variably modified
2558 type. */
2560 static void
2561 record_maybe_used_decl (tree decl)
2563 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2564 t->decl = decl;
2565 t->level = in_sizeof + in_typeof;
2566 t->next = maybe_used_decls;
2567 maybe_used_decls = t;
2570 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2571 USED is false, just discard them. If it is true, mark them used
2572 (if no longer inside sizeof or typeof) or move them to the next
2573 level up (if still inside sizeof or typeof). */
2575 void
2576 pop_maybe_used (bool used)
2578 struct maybe_used_decl *p = maybe_used_decls;
2579 int cur_level = in_sizeof + in_typeof;
2580 while (p && p->level > cur_level)
2582 if (used)
2584 if (cur_level == 0)
2585 C_DECL_USED (p->decl) = 1;
2586 else
2587 p->level = cur_level;
2589 p = p->next;
2591 if (!used || cur_level == 0)
2592 maybe_used_decls = p;
2595 /* Return the result of sizeof applied to EXPR. */
2597 struct c_expr
2598 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2600 struct c_expr ret;
2601 if (expr.value == error_mark_node)
2603 ret.value = error_mark_node;
2604 ret.original_code = ERROR_MARK;
2605 ret.original_type = NULL;
2606 pop_maybe_used (false);
2608 else
2610 bool expr_const_operands = true;
2611 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2612 &expr_const_operands);
2613 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2614 ret.original_code = ERROR_MARK;
2615 ret.original_type = NULL;
2616 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2618 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2619 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2620 folded_expr, ret.value);
2621 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2622 SET_EXPR_LOCATION (ret.value, loc);
2624 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2626 return ret;
2629 /* Return the result of sizeof applied to T, a structure for the type
2630 name passed to sizeof (rather than the type itself). LOC is the
2631 location of the original expression. */
2633 struct c_expr
2634 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2636 tree type;
2637 struct c_expr ret;
2638 tree type_expr = NULL_TREE;
2639 bool type_expr_const = true;
2640 type = groktypename (t, &type_expr, &type_expr_const);
2641 ret.value = c_sizeof (loc, type);
2642 ret.original_code = ERROR_MARK;
2643 ret.original_type = NULL;
2644 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2645 && c_vla_type_p (type))
2647 /* If the type is a [*] array, it is a VLA but is represented as
2648 having a size of zero. In such a case we must ensure that
2649 the result of sizeof does not get folded to a constant by
2650 c_fully_fold, because if the size is evaluated the result is
2651 not constant and so constraints on zero or negative size
2652 arrays must not be applied when this sizeof call is inside
2653 another array declarator. */
2654 if (!type_expr)
2655 type_expr = integer_zero_node;
2656 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2657 type_expr, ret.value);
2658 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2660 pop_maybe_used (type != error_mark_node
2661 ? C_TYPE_VARIABLE_SIZE (type) : false);
2662 return ret;
2665 /* Build a function call to function FUNCTION with parameters PARAMS.
2666 The function call is at LOC.
2667 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2668 TREE_VALUE of each node is a parameter-expression.
2669 FUNCTION's data type may be a function type or a pointer-to-function. */
2671 tree
2672 build_function_call (location_t loc, tree function, tree params)
2674 VEC(tree,gc) *vec;
2675 tree ret;
2677 vec = VEC_alloc (tree, gc, list_length (params));
2678 for (; params; params = TREE_CHAIN (params))
2679 VEC_quick_push (tree, vec, TREE_VALUE (params));
2680 ret = build_function_call_vec (loc, function, vec, NULL);
2681 VEC_free (tree, gc, vec);
2682 return ret;
2685 /* Build a function call to function FUNCTION with parameters PARAMS.
2686 ORIGTYPES, if not NULL, is a vector of types; each element is
2687 either NULL or the original type of the corresponding element in
2688 PARAMS. The original type may differ from TREE_TYPE of the
2689 parameter for enums. FUNCTION's data type may be a function type
2690 or pointer-to-function. This function changes the elements of
2691 PARAMS. */
2693 tree
2694 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2695 VEC(tree,gc) *origtypes)
2697 tree fntype, fundecl = 0;
2698 tree name = NULL_TREE, result;
2699 tree tem;
2700 int nargs;
2701 tree *argarray;
2704 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2705 STRIP_TYPE_NOPS (function);
2707 /* Convert anything with function type to a pointer-to-function. */
2708 if (TREE_CODE (function) == FUNCTION_DECL)
2710 /* Implement type-directed function overloading for builtins.
2711 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2712 handle all the type checking. The result is a complete expression
2713 that implements this function call. */
2714 tem = resolve_overloaded_builtin (loc, function, params);
2715 if (tem)
2716 return tem;
2718 name = DECL_NAME (function);
2720 if (flag_tm)
2721 tm_malloc_replacement (function);
2722 fundecl = function;
2723 /* Atomic functions have type checking/casting already done. They are
2724 often rewritten and don't match the original parameter list. */
2725 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2726 origtypes = NULL;
2728 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2729 function = function_to_pointer_conversion (loc, function);
2731 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2732 expressions, like those used for ObjC messenger dispatches. */
2733 if (!VEC_empty (tree, params))
2734 function = objc_rewrite_function_call (function,
2735 VEC_index (tree, params, 0));
2737 function = c_fully_fold (function, false, NULL);
2739 fntype = TREE_TYPE (function);
2741 if (TREE_CODE (fntype) == ERROR_MARK)
2742 return error_mark_node;
2744 if (!(TREE_CODE (fntype) == POINTER_TYPE
2745 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2747 error_at (loc, "called object %qE is not a function", function);
2748 return error_mark_node;
2751 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2752 current_function_returns_abnormally = 1;
2754 /* fntype now gets the type of function pointed to. */
2755 fntype = TREE_TYPE (fntype);
2757 /* Convert the parameters to the types declared in the
2758 function prototype, or apply default promotions. */
2760 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2761 function, fundecl);
2762 if (nargs < 0)
2763 return error_mark_node;
2765 /* Check that the function is called through a compatible prototype.
2766 If it is not, replace the call by a trap, wrapped up in a compound
2767 expression if necessary. This has the nice side-effect to prevent
2768 the tree-inliner from generating invalid assignment trees which may
2769 blow up in the RTL expander later. */
2770 if (CONVERT_EXPR_P (function)
2771 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2772 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2773 && !comptypes (fntype, TREE_TYPE (tem)))
2775 tree return_type = TREE_TYPE (fntype);
2776 tree trap = build_function_call (loc,
2777 builtin_decl_explicit (BUILT_IN_TRAP),
2778 NULL_TREE);
2779 int i;
2781 /* This situation leads to run-time undefined behavior. We can't,
2782 therefore, simply error unless we can prove that all possible
2783 executions of the program must execute the code. */
2784 if (warning_at (loc, 0, "function called through a non-compatible type"))
2785 /* We can, however, treat "undefined" any way we please.
2786 Call abort to encourage the user to fix the program. */
2787 inform (loc, "if this code is reached, the program will abort");
2788 /* Before the abort, allow the function arguments to exit or
2789 call longjmp. */
2790 for (i = 0; i < nargs; i++)
2791 trap = build2 (COMPOUND_EXPR, void_type_node,
2792 VEC_index (tree, params, i), trap);
2794 if (VOID_TYPE_P (return_type))
2796 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2797 pedwarn (loc, 0,
2798 "function with qualified void return type called");
2799 return trap;
2801 else
2803 tree rhs;
2805 if (AGGREGATE_TYPE_P (return_type))
2806 rhs = build_compound_literal (loc, return_type,
2807 build_constructor (return_type, 0),
2808 false);
2809 else
2810 rhs = build_zero_cst (return_type);
2812 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2813 trap, rhs));
2817 argarray = VEC_address (tree, params);
2819 /* Check that arguments to builtin functions match the expectations. */
2820 if (fundecl
2821 && DECL_BUILT_IN (fundecl)
2822 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2823 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2824 return error_mark_node;
2826 /* Check that the arguments to the function are valid. */
2827 check_function_arguments (fntype, nargs, argarray);
2829 if (name != NULL_TREE
2830 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2832 if (require_constant_value)
2833 result =
2834 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2835 function, nargs, argarray);
2836 else
2837 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2838 function, nargs, argarray);
2839 if (TREE_CODE (result) == NOP_EXPR
2840 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2841 STRIP_TYPE_NOPS (result);
2843 else
2844 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2845 function, nargs, argarray);
2847 if (VOID_TYPE_P (TREE_TYPE (result)))
2849 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2850 pedwarn (loc, 0,
2851 "function with qualified void return type called");
2852 return result;
2854 return require_complete_type (result);
2857 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2858 and have vector types, V0 has the same type as V1, and the number of
2859 elements of V0, V1, MASK is the same.
2861 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2862 called with two arguments. In this case implementation passes the
2863 first argument twice in order to share the same tree code. This fact
2864 could enable the mask-values being twice the vector length. This is
2865 an implementation accident and this semantics is not guaranteed to
2866 the user. */
2867 tree
2868 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2870 tree ret;
2871 bool wrap = true;
2872 bool maybe_const = false;
2873 bool two_arguments = false;
2875 if (v1 == NULL_TREE)
2877 two_arguments = true;
2878 v1 = v0;
2881 if (v0 == error_mark_node || v1 == error_mark_node
2882 || mask == error_mark_node)
2883 return error_mark_node;
2885 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2886 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2888 error_at (loc, "__builtin_shuffle last argument must "
2889 "be an integer vector");
2890 return error_mark_node;
2893 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2894 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2896 error_at (loc, "__builtin_shuffle arguments must be vectors");
2897 return error_mark_node;
2900 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2902 error_at (loc, "__builtin_shuffle argument vectors must be of "
2903 "the same type");
2904 return error_mark_node;
2907 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2908 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2909 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2910 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2912 error_at (loc, "__builtin_shuffle number of elements of the "
2913 "argument vector(s) and the mask vector should "
2914 "be the same");
2915 return error_mark_node;
2918 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2919 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2921 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2922 "must have the same size as inner type of the mask");
2923 return error_mark_node;
2926 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2927 v0 = c_fully_fold (v0, false, &maybe_const);
2928 wrap &= maybe_const;
2930 if (two_arguments)
2931 v1 = v0 = save_expr (v0);
2932 else
2934 v1 = c_fully_fold (v1, false, &maybe_const);
2935 wrap &= maybe_const;
2938 mask = c_fully_fold (mask, false, &maybe_const);
2939 wrap &= maybe_const;
2941 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2943 if (!wrap)
2944 ret = c_wrap_maybe_const (ret, true);
2946 return ret;
2949 /* Convert the argument expressions in the vector VALUES
2950 to the types in the list TYPELIST.
2952 If TYPELIST is exhausted, or when an element has NULL as its type,
2953 perform the default conversions.
2955 ORIGTYPES is the original types of the expressions in VALUES. This
2956 holds the type of enum values which have been converted to integral
2957 types. It may be NULL.
2959 FUNCTION is a tree for the called function. It is used only for
2960 error messages, where it is formatted with %qE.
2962 This is also where warnings about wrong number of args are generated.
2964 Returns the actual number of arguments processed (which may be less
2965 than the length of VALUES in some error situations), or -1 on
2966 failure. */
2968 static int
2969 convert_arguments (tree typelist, VEC(tree,gc) *values,
2970 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2972 tree typetail, val;
2973 unsigned int parmnum;
2974 bool error_args = false;
2975 const bool type_generic = fundecl
2976 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2977 bool type_generic_remove_excess_precision = false;
2978 tree selector;
2980 /* Change pointer to function to the function itself for
2981 diagnostics. */
2982 if (TREE_CODE (function) == ADDR_EXPR
2983 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2984 function = TREE_OPERAND (function, 0);
2986 /* Handle an ObjC selector specially for diagnostics. */
2987 selector = objc_message_selector ();
2989 /* For type-generic built-in functions, determine whether excess
2990 precision should be removed (classification) or not
2991 (comparison). */
2992 if (type_generic
2993 && DECL_BUILT_IN (fundecl)
2994 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2996 switch (DECL_FUNCTION_CODE (fundecl))
2998 case BUILT_IN_ISFINITE:
2999 case BUILT_IN_ISINF:
3000 case BUILT_IN_ISINF_SIGN:
3001 case BUILT_IN_ISNAN:
3002 case BUILT_IN_ISNORMAL:
3003 case BUILT_IN_FPCLASSIFY:
3004 type_generic_remove_excess_precision = true;
3005 break;
3007 default:
3008 type_generic_remove_excess_precision = false;
3009 break;
3013 /* Scan the given expressions and types, producing individual
3014 converted arguments. */
3016 for (typetail = typelist, parmnum = 0;
3017 VEC_iterate (tree, values, parmnum, val);
3018 ++parmnum)
3020 tree type = typetail ? TREE_VALUE (typetail) : 0;
3021 tree valtype = TREE_TYPE (val);
3022 tree rname = function;
3023 int argnum = parmnum + 1;
3024 const char *invalid_func_diag;
3025 bool excess_precision = false;
3026 bool npc;
3027 tree parmval;
3029 if (type == void_type_node)
3031 if (selector)
3032 error_at (input_location,
3033 "too many arguments to method %qE", selector);
3034 else
3035 error_at (input_location,
3036 "too many arguments to function %qE", function);
3038 if (fundecl && !DECL_BUILT_IN (fundecl))
3039 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3040 return parmnum;
3043 if (selector && argnum > 2)
3045 rname = selector;
3046 argnum -= 2;
3049 npc = null_pointer_constant_p (val);
3051 /* If there is excess precision and a prototype, convert once to
3052 the required type rather than converting via the semantic
3053 type. Likewise without a prototype a float value represented
3054 as long double should be converted once to double. But for
3055 type-generic classification functions excess precision must
3056 be removed here. */
3057 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3058 && (type || !type_generic || !type_generic_remove_excess_precision))
3060 val = TREE_OPERAND (val, 0);
3061 excess_precision = true;
3063 val = c_fully_fold (val, false, NULL);
3064 STRIP_TYPE_NOPS (val);
3066 val = require_complete_type (val);
3068 if (type != 0)
3070 /* Formal parm type is specified by a function prototype. */
3072 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3074 error ("type of formal parameter %d is incomplete", parmnum + 1);
3075 parmval = val;
3077 else
3079 tree origtype;
3081 /* Optionally warn about conversions that
3082 differ from the default conversions. */
3083 if (warn_traditional_conversion || warn_traditional)
3085 unsigned int formal_prec = TYPE_PRECISION (type);
3087 if (INTEGRAL_TYPE_P (type)
3088 && TREE_CODE (valtype) == REAL_TYPE)
3089 warning (0, "passing argument %d of %qE as integer "
3090 "rather than floating due to prototype",
3091 argnum, rname);
3092 if (INTEGRAL_TYPE_P (type)
3093 && TREE_CODE (valtype) == COMPLEX_TYPE)
3094 warning (0, "passing argument %d of %qE as integer "
3095 "rather than complex due to prototype",
3096 argnum, rname);
3097 else if (TREE_CODE (type) == COMPLEX_TYPE
3098 && TREE_CODE (valtype) == REAL_TYPE)
3099 warning (0, "passing argument %d of %qE as complex "
3100 "rather than floating due to prototype",
3101 argnum, rname);
3102 else if (TREE_CODE (type) == REAL_TYPE
3103 && INTEGRAL_TYPE_P (valtype))
3104 warning (0, "passing argument %d of %qE as floating "
3105 "rather than integer due to prototype",
3106 argnum, rname);
3107 else if (TREE_CODE (type) == COMPLEX_TYPE
3108 && INTEGRAL_TYPE_P (valtype))
3109 warning (0, "passing argument %d of %qE as complex "
3110 "rather than integer due to prototype",
3111 argnum, rname);
3112 else if (TREE_CODE (type) == REAL_TYPE
3113 && TREE_CODE (valtype) == COMPLEX_TYPE)
3114 warning (0, "passing argument %d of %qE as floating "
3115 "rather than complex due to prototype",
3116 argnum, rname);
3117 /* ??? At some point, messages should be written about
3118 conversions between complex types, but that's too messy
3119 to do now. */
3120 else if (TREE_CODE (type) == REAL_TYPE
3121 && TREE_CODE (valtype) == REAL_TYPE)
3123 /* Warn if any argument is passed as `float',
3124 since without a prototype it would be `double'. */
3125 if (formal_prec == TYPE_PRECISION (float_type_node)
3126 && type != dfloat32_type_node)
3127 warning (0, "passing argument %d of %qE as %<float%> "
3128 "rather than %<double%> due to prototype",
3129 argnum, rname);
3131 /* Warn if mismatch between argument and prototype
3132 for decimal float types. Warn of conversions with
3133 binary float types and of precision narrowing due to
3134 prototype. */
3135 else if (type != valtype
3136 && (type == dfloat32_type_node
3137 || type == dfloat64_type_node
3138 || type == dfloat128_type_node
3139 || valtype == dfloat32_type_node
3140 || valtype == dfloat64_type_node
3141 || valtype == dfloat128_type_node)
3142 && (formal_prec
3143 <= TYPE_PRECISION (valtype)
3144 || (type == dfloat128_type_node
3145 && (valtype
3146 != dfloat64_type_node
3147 && (valtype
3148 != dfloat32_type_node)))
3149 || (type == dfloat64_type_node
3150 && (valtype
3151 != dfloat32_type_node))))
3152 warning (0, "passing argument %d of %qE as %qT "
3153 "rather than %qT due to prototype",
3154 argnum, rname, type, valtype);
3157 /* Detect integer changing in width or signedness.
3158 These warnings are only activated with
3159 -Wtraditional-conversion, not with -Wtraditional. */
3160 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3161 && INTEGRAL_TYPE_P (valtype))
3163 tree would_have_been = default_conversion (val);
3164 tree type1 = TREE_TYPE (would_have_been);
3166 if (TREE_CODE (type) == ENUMERAL_TYPE
3167 && (TYPE_MAIN_VARIANT (type)
3168 == TYPE_MAIN_VARIANT (valtype)))
3169 /* No warning if function asks for enum
3170 and the actual arg is that enum type. */
3172 else if (formal_prec != TYPE_PRECISION (type1))
3173 warning (OPT_Wtraditional_conversion,
3174 "passing argument %d of %qE "
3175 "with different width due to prototype",
3176 argnum, rname);
3177 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3179 /* Don't complain if the formal parameter type
3180 is an enum, because we can't tell now whether
3181 the value was an enum--even the same enum. */
3182 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3184 else if (TREE_CODE (val) == INTEGER_CST
3185 && int_fits_type_p (val, type))
3186 /* Change in signedness doesn't matter
3187 if a constant value is unaffected. */
3189 /* If the value is extended from a narrower
3190 unsigned type, it doesn't matter whether we
3191 pass it as signed or unsigned; the value
3192 certainly is the same either way. */
3193 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3194 && TYPE_UNSIGNED (valtype))
3196 else if (TYPE_UNSIGNED (type))
3197 warning (OPT_Wtraditional_conversion,
3198 "passing argument %d of %qE "
3199 "as unsigned due to prototype",
3200 argnum, rname);
3201 else
3202 warning (OPT_Wtraditional_conversion,
3203 "passing argument %d of %qE "
3204 "as signed due to prototype", argnum, rname);
3208 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3209 sake of better warnings from convert_and_check. */
3210 if (excess_precision)
3211 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3212 origtype = (origtypes == NULL
3213 ? NULL_TREE
3214 : VEC_index (tree, origtypes, parmnum));
3215 parmval = convert_for_assignment (input_location, type, val,
3216 origtype, ic_argpass, npc,
3217 fundecl, function,
3218 parmnum + 1);
3220 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3221 && INTEGRAL_TYPE_P (type)
3222 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3223 parmval = default_conversion (parmval);
3226 else if (TREE_CODE (valtype) == REAL_TYPE
3227 && (TYPE_PRECISION (valtype)
3228 < TYPE_PRECISION (double_type_node))
3229 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3231 if (type_generic)
3232 parmval = val;
3233 else
3235 /* Convert `float' to `double'. */
3236 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3237 warning (OPT_Wdouble_promotion,
3238 "implicit conversion from %qT to %qT when passing "
3239 "argument to function",
3240 valtype, double_type_node);
3241 parmval = convert (double_type_node, val);
3244 else if (excess_precision && !type_generic)
3245 /* A "double" argument with excess precision being passed
3246 without a prototype or in variable arguments. */
3247 parmval = convert (valtype, val);
3248 else if ((invalid_func_diag =
3249 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3251 error (invalid_func_diag);
3252 return -1;
3254 else
3255 /* Convert `short' and `char' to full-size `int'. */
3256 parmval = default_conversion (val);
3258 VEC_replace (tree, values, parmnum, parmval);
3259 if (parmval == error_mark_node)
3260 error_args = true;
3262 if (typetail)
3263 typetail = TREE_CHAIN (typetail);
3266 gcc_assert (parmnum == VEC_length (tree, values));
3268 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3270 error_at (input_location,
3271 "too few arguments to function %qE", function);
3272 if (fundecl && !DECL_BUILT_IN (fundecl))
3273 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3274 return -1;
3277 return error_args ? -1 : (int) parmnum;
3280 /* This is the entry point used by the parser to build unary operators
3281 in the input. CODE, a tree_code, specifies the unary operator, and
3282 ARG is the operand. For unary plus, the C parser currently uses
3283 CONVERT_EXPR for code.
3285 LOC is the location to use for the tree generated.
3288 struct c_expr
3289 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3291 struct c_expr result;
3293 result.value = build_unary_op (loc, code, arg.value, 0);
3294 result.original_code = code;
3295 result.original_type = NULL;
3297 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3298 overflow_warning (loc, result.value);
3300 return result;
3303 /* This is the entry point used by the parser to build binary operators
3304 in the input. CODE, a tree_code, specifies the binary operator, and
3305 ARG1 and ARG2 are the operands. In addition to constructing the
3306 expression, we check for operands that were written with other binary
3307 operators in a way that is likely to confuse the user.
3309 LOCATION is the location of the binary operator. */
3311 struct c_expr
3312 parser_build_binary_op (location_t location, enum tree_code code,
3313 struct c_expr arg1, struct c_expr arg2)
3315 struct c_expr result;
3317 enum tree_code code1 = arg1.original_code;
3318 enum tree_code code2 = arg2.original_code;
3319 tree type1 = (arg1.original_type
3320 ? arg1.original_type
3321 : TREE_TYPE (arg1.value));
3322 tree type2 = (arg2.original_type
3323 ? arg2.original_type
3324 : TREE_TYPE (arg2.value));
3326 result.value = build_binary_op (location, code,
3327 arg1.value, arg2.value, 1);
3328 result.original_code = code;
3329 result.original_type = NULL;
3331 if (TREE_CODE (result.value) == ERROR_MARK)
3332 return result;
3334 if (location != UNKNOWN_LOCATION)
3335 protected_set_expr_location (result.value, location);
3337 /* Check for cases such as x+y<<z which users are likely
3338 to misinterpret. */
3339 if (warn_parentheses)
3340 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3342 if (warn_logical_op)
3343 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3344 code1, arg1.value, code2, arg2.value);
3346 /* Warn about comparisons against string literals, with the exception
3347 of testing for equality or inequality of a string literal with NULL. */
3348 if (code == EQ_EXPR || code == NE_EXPR)
3350 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3351 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3352 warning_at (location, OPT_Waddress,
3353 "comparison with string literal results in unspecified behavior");
3355 else if (TREE_CODE_CLASS (code) == tcc_comparison
3356 && (code1 == STRING_CST || code2 == STRING_CST))
3357 warning_at (location, OPT_Waddress,
3358 "comparison with string literal results in unspecified behavior");
3360 if (TREE_OVERFLOW_P (result.value)
3361 && !TREE_OVERFLOW_P (arg1.value)
3362 && !TREE_OVERFLOW_P (arg2.value))
3363 overflow_warning (location, result.value);
3365 /* Warn about comparisons of different enum types. */
3366 if (warn_enum_compare
3367 && TREE_CODE_CLASS (code) == tcc_comparison
3368 && TREE_CODE (type1) == ENUMERAL_TYPE
3369 && TREE_CODE (type2) == ENUMERAL_TYPE
3370 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3371 warning_at (location, OPT_Wenum_compare,
3372 "comparison between %qT and %qT",
3373 type1, type2);
3375 return result;
3378 /* Return a tree for the difference of pointers OP0 and OP1.
3379 The resulting tree has type int. */
3381 static tree
3382 pointer_diff (location_t loc, tree op0, tree op1)
3384 tree restype = ptrdiff_type_node;
3385 tree result, inttype;
3387 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3388 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3389 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3390 tree con0, con1, lit0, lit1;
3391 tree orig_op1 = op1;
3393 /* If the operands point into different address spaces, we need to
3394 explicitly convert them to pointers into the common address space
3395 before we can subtract the numerical address values. */
3396 if (as0 != as1)
3398 addr_space_t as_common;
3399 tree common_type;
3401 /* Determine the common superset address space. This is guaranteed
3402 to exist because the caller verified that comp_target_types
3403 returned non-zero. */
3404 if (!addr_space_superset (as0, as1, &as_common))
3405 gcc_unreachable ();
3407 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3408 op0 = convert (common_type, op0);
3409 op1 = convert (common_type, op1);
3412 /* Determine integer type to perform computations in. This will usually
3413 be the same as the result type (ptrdiff_t), but may need to be a wider
3414 type if pointers for the address space are wider than ptrdiff_t. */
3415 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3416 inttype = lang_hooks.types.type_for_size
3417 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3418 else
3419 inttype = restype;
3422 if (TREE_CODE (target_type) == VOID_TYPE)
3423 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3424 "pointer of type %<void *%> used in subtraction");
3425 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3426 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3427 "pointer to a function used in subtraction");
3429 /* If the conversion to ptrdiff_type does anything like widening or
3430 converting a partial to an integral mode, we get a convert_expression
3431 that is in the way to do any simplifications.
3432 (fold-const.c doesn't know that the extra bits won't be needed.
3433 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3434 different mode in place.)
3435 So first try to find a common term here 'by hand'; we want to cover
3436 at least the cases that occur in legal static initializers. */
3437 if (CONVERT_EXPR_P (op0)
3438 && (TYPE_PRECISION (TREE_TYPE (op0))
3439 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3440 con0 = TREE_OPERAND (op0, 0);
3441 else
3442 con0 = op0;
3443 if (CONVERT_EXPR_P (op1)
3444 && (TYPE_PRECISION (TREE_TYPE (op1))
3445 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3446 con1 = TREE_OPERAND (op1, 0);
3447 else
3448 con1 = op1;
3450 if (TREE_CODE (con0) == PLUS_EXPR)
3452 lit0 = TREE_OPERAND (con0, 1);
3453 con0 = TREE_OPERAND (con0, 0);
3455 else
3456 lit0 = integer_zero_node;
3458 if (TREE_CODE (con1) == PLUS_EXPR)
3460 lit1 = TREE_OPERAND (con1, 1);
3461 con1 = TREE_OPERAND (con1, 0);
3463 else
3464 lit1 = integer_zero_node;
3466 if (operand_equal_p (con0, con1, 0))
3468 op0 = lit0;
3469 op1 = lit1;
3473 /* First do the subtraction as integers;
3474 then drop through to build the divide operator.
3475 Do not do default conversions on the minus operator
3476 in case restype is a short type. */
3478 op0 = build_binary_op (loc,
3479 MINUS_EXPR, convert (inttype, op0),
3480 convert (inttype, op1), 0);
3481 /* This generates an error if op1 is pointer to incomplete type. */
3482 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3483 error_at (loc, "arithmetic on pointer to an incomplete type");
3485 /* This generates an error if op0 is pointer to incomplete type. */
3486 op1 = c_size_in_bytes (target_type);
3488 /* Divide by the size, in easiest possible way. */
3489 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3490 op0, convert (inttype, op1));
3492 /* Convert to final result type if necessary. */
3493 return convert (restype, result);
3496 /* Construct and perhaps optimize a tree representation
3497 for a unary operation. CODE, a tree_code, specifies the operation
3498 and XARG is the operand.
3499 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3500 the default promotions (such as from short to int).
3501 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3502 allows non-lvalues; this is only used to handle conversion of non-lvalue
3503 arrays to pointers in C99.
3505 LOCATION is the location of the operator. */
3507 tree
3508 build_unary_op (location_t location,
3509 enum tree_code code, tree xarg, int flag)
3511 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3512 tree arg = xarg;
3513 tree argtype = 0;
3514 enum tree_code typecode;
3515 tree val;
3516 tree ret = error_mark_node;
3517 tree eptype = NULL_TREE;
3518 int noconvert = flag;
3519 const char *invalid_op_diag;
3520 bool int_operands;
3522 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3523 if (int_operands)
3524 arg = remove_c_maybe_const_expr (arg);
3526 if (code != ADDR_EXPR)
3527 arg = require_complete_type (arg);
3529 typecode = TREE_CODE (TREE_TYPE (arg));
3530 if (typecode == ERROR_MARK)
3531 return error_mark_node;
3532 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3533 typecode = INTEGER_TYPE;
3535 if ((invalid_op_diag
3536 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3538 error_at (location, invalid_op_diag);
3539 return error_mark_node;
3542 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3544 eptype = TREE_TYPE (arg);
3545 arg = TREE_OPERAND (arg, 0);
3548 switch (code)
3550 case CONVERT_EXPR:
3551 /* This is used for unary plus, because a CONVERT_EXPR
3552 is enough to prevent anybody from looking inside for
3553 associativity, but won't generate any code. */
3554 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3555 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3556 || typecode == VECTOR_TYPE))
3558 error_at (location, "wrong type argument to unary plus");
3559 return error_mark_node;
3561 else if (!noconvert)
3562 arg = default_conversion (arg);
3563 arg = non_lvalue_loc (location, arg);
3564 break;
3566 case NEGATE_EXPR:
3567 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3568 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3569 || typecode == VECTOR_TYPE))
3571 error_at (location, "wrong type argument to unary minus");
3572 return error_mark_node;
3574 else if (!noconvert)
3575 arg = default_conversion (arg);
3576 break;
3578 case BIT_NOT_EXPR:
3579 /* ~ works on integer types and non float vectors. */
3580 if (typecode == INTEGER_TYPE
3581 || (typecode == VECTOR_TYPE
3582 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3584 if (!noconvert)
3585 arg = default_conversion (arg);
3587 else if (typecode == COMPLEX_TYPE)
3589 code = CONJ_EXPR;
3590 pedwarn (location, OPT_pedantic,
3591 "ISO C does not support %<~%> for complex conjugation");
3592 if (!noconvert)
3593 arg = default_conversion (arg);
3595 else
3597 error_at (location, "wrong type argument to bit-complement");
3598 return error_mark_node;
3600 break;
3602 case ABS_EXPR:
3603 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3605 error_at (location, "wrong type argument to abs");
3606 return error_mark_node;
3608 else if (!noconvert)
3609 arg = default_conversion (arg);
3610 break;
3612 case CONJ_EXPR:
3613 /* Conjugating a real value is a no-op, but allow it anyway. */
3614 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3615 || typecode == COMPLEX_TYPE))
3617 error_at (location, "wrong type argument to conjugation");
3618 return error_mark_node;
3620 else if (!noconvert)
3621 arg = default_conversion (arg);
3622 break;
3624 case TRUTH_NOT_EXPR:
3625 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3626 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3627 && typecode != COMPLEX_TYPE)
3629 error_at (location,
3630 "wrong type argument to unary exclamation mark");
3631 return error_mark_node;
3633 arg = c_objc_common_truthvalue_conversion (location, arg);
3634 ret = invert_truthvalue_loc (location, arg);
3635 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3636 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3637 location = EXPR_LOCATION (ret);
3638 goto return_build_unary_op;
3640 case REALPART_EXPR:
3641 case IMAGPART_EXPR:
3642 ret = build_real_imag_expr (location, code, arg);
3643 if (ret == error_mark_node)
3644 return error_mark_node;
3645 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3646 eptype = TREE_TYPE (eptype);
3647 goto return_build_unary_op;
3649 case PREINCREMENT_EXPR:
3650 case POSTINCREMENT_EXPR:
3651 case PREDECREMENT_EXPR:
3652 case POSTDECREMENT_EXPR:
3654 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3656 tree inner = build_unary_op (location, code,
3657 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3658 if (inner == error_mark_node)
3659 return error_mark_node;
3660 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3661 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3662 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3663 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3664 goto return_build_unary_op;
3667 /* Complain about anything that is not a true lvalue. In
3668 Objective-C, skip this check for property_refs. */
3669 if (!objc_is_property_ref (arg)
3670 && !lvalue_or_else (location,
3671 arg, ((code == PREINCREMENT_EXPR
3672 || code == POSTINCREMENT_EXPR)
3673 ? lv_increment
3674 : lv_decrement)))
3675 return error_mark_node;
3677 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3679 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3680 warning_at (location, OPT_Wc___compat,
3681 "increment of enumeration value is invalid in C++");
3682 else
3683 warning_at (location, OPT_Wc___compat,
3684 "decrement of enumeration value is invalid in C++");
3687 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3688 arg = c_fully_fold (arg, false, NULL);
3690 /* Increment or decrement the real part of the value,
3691 and don't change the imaginary part. */
3692 if (typecode == COMPLEX_TYPE)
3694 tree real, imag;
3696 pedwarn (location, OPT_pedantic,
3697 "ISO C does not support %<++%> and %<--%> on complex types");
3699 arg = stabilize_reference (arg);
3700 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3701 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3702 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3703 if (real == error_mark_node || imag == error_mark_node)
3704 return error_mark_node;
3705 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3706 real, imag);
3707 goto return_build_unary_op;
3710 /* Report invalid types. */
3712 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3713 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3715 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3716 error_at (location, "wrong type argument to increment");
3717 else
3718 error_at (location, "wrong type argument to decrement");
3720 return error_mark_node;
3724 tree inc;
3726 argtype = TREE_TYPE (arg);
3728 /* Compute the increment. */
3730 if (typecode == POINTER_TYPE)
3732 /* If pointer target is an undefined struct,
3733 we just cannot know how to do the arithmetic. */
3734 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3736 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3737 error_at (location,
3738 "increment of pointer to unknown structure");
3739 else
3740 error_at (location,
3741 "decrement of pointer to unknown structure");
3743 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3744 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3746 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3747 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3748 "wrong type argument to increment");
3749 else
3750 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3751 "wrong type argument to decrement");
3754 inc = c_size_in_bytes (TREE_TYPE (argtype));
3755 inc = convert_to_ptrofftype_loc (location, inc);
3757 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3759 /* For signed fract types, we invert ++ to -- or
3760 -- to ++, and change inc from 1 to -1, because
3761 it is not possible to represent 1 in signed fract constants.
3762 For unsigned fract types, the result always overflows and
3763 we get an undefined (original) or the maximum value. */
3764 if (code == PREINCREMENT_EXPR)
3765 code = PREDECREMENT_EXPR;
3766 else if (code == PREDECREMENT_EXPR)
3767 code = PREINCREMENT_EXPR;
3768 else if (code == POSTINCREMENT_EXPR)
3769 code = POSTDECREMENT_EXPR;
3770 else /* code == POSTDECREMENT_EXPR */
3771 code = POSTINCREMENT_EXPR;
3773 inc = integer_minus_one_node;
3774 inc = convert (argtype, inc);
3776 else
3778 inc = integer_one_node;
3779 inc = convert (argtype, inc);
3782 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3783 need to ask Objective-C to build the increment or decrement
3784 expression for it. */
3785 if (objc_is_property_ref (arg))
3786 return objc_build_incr_expr_for_property_ref (location, code,
3787 arg, inc);
3789 /* Report a read-only lvalue. */
3790 if (TYPE_READONLY (argtype))
3792 readonly_error (arg,
3793 ((code == PREINCREMENT_EXPR
3794 || code == POSTINCREMENT_EXPR)
3795 ? lv_increment : lv_decrement));
3796 return error_mark_node;
3798 else if (TREE_READONLY (arg))
3799 readonly_warning (arg,
3800 ((code == PREINCREMENT_EXPR
3801 || code == POSTINCREMENT_EXPR)
3802 ? lv_increment : lv_decrement));
3804 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3805 val = boolean_increment (code, arg);
3806 else
3807 val = build2 (code, TREE_TYPE (arg), arg, inc);
3808 TREE_SIDE_EFFECTS (val) = 1;
3809 if (TREE_CODE (val) != code)
3810 TREE_NO_WARNING (val) = 1;
3811 ret = val;
3812 goto return_build_unary_op;
3815 case ADDR_EXPR:
3816 /* Note that this operation never does default_conversion. */
3818 /* The operand of unary '&' must be an lvalue (which excludes
3819 expressions of type void), or, in C99, the result of a [] or
3820 unary '*' operator. */
3821 if (VOID_TYPE_P (TREE_TYPE (arg))
3822 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3823 && (TREE_CODE (arg) != INDIRECT_REF
3824 || !flag_isoc99))
3825 pedwarn (location, 0, "taking address of expression of type %<void%>");
3827 /* Let &* cancel out to simplify resulting code. */
3828 if (TREE_CODE (arg) == INDIRECT_REF)
3830 /* Don't let this be an lvalue. */
3831 if (lvalue_p (TREE_OPERAND (arg, 0)))
3832 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3833 ret = TREE_OPERAND (arg, 0);
3834 goto return_build_unary_op;
3837 /* For &x[y], return x+y */
3838 if (TREE_CODE (arg) == ARRAY_REF)
3840 tree op0 = TREE_OPERAND (arg, 0);
3841 if (!c_mark_addressable (op0))
3842 return error_mark_node;
3845 /* Anything not already handled and not a true memory reference
3846 or a non-lvalue array is an error. */
3847 else if (typecode != FUNCTION_TYPE && !flag
3848 && !lvalue_or_else (location, arg, lv_addressof))
3849 return error_mark_node;
3851 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3852 folding later. */
3853 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3855 tree inner = build_unary_op (location, code,
3856 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3857 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3858 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3859 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3860 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3861 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3862 goto return_build_unary_op;
3865 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3866 argtype = TREE_TYPE (arg);
3868 /* If the lvalue is const or volatile, merge that into the type
3869 to which the address will point. This is only needed
3870 for function types. */
3871 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3872 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3873 && TREE_CODE (argtype) == FUNCTION_TYPE)
3875 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3876 int quals = orig_quals;
3878 if (TREE_READONLY (arg))
3879 quals |= TYPE_QUAL_CONST;
3880 if (TREE_THIS_VOLATILE (arg))
3881 quals |= TYPE_QUAL_VOLATILE;
3883 argtype = c_build_qualified_type (argtype, quals);
3886 if (!c_mark_addressable (arg))
3887 return error_mark_node;
3889 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3890 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3892 argtype = build_pointer_type (argtype);
3894 /* ??? Cope with user tricks that amount to offsetof. Delete this
3895 when we have proper support for integer constant expressions. */
3896 val = get_base_address (arg);
3897 if (val && TREE_CODE (val) == INDIRECT_REF
3898 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3900 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
3901 goto return_build_unary_op;
3904 val = build1 (ADDR_EXPR, argtype, arg);
3906 ret = val;
3907 goto return_build_unary_op;
3909 default:
3910 gcc_unreachable ();
3913 if (argtype == 0)
3914 argtype = TREE_TYPE (arg);
3915 if (TREE_CODE (arg) == INTEGER_CST)
3916 ret = (require_constant_value
3917 ? fold_build1_initializer_loc (location, code, argtype, arg)
3918 : fold_build1_loc (location, code, argtype, arg));
3919 else
3920 ret = build1 (code, argtype, arg);
3921 return_build_unary_op:
3922 gcc_assert (ret != error_mark_node);
3923 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3924 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3925 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3926 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3927 ret = note_integer_operands (ret);
3928 if (eptype)
3929 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3930 protected_set_expr_location (ret, location);
3931 return ret;
3934 /* Return nonzero if REF is an lvalue valid for this language.
3935 Lvalues can be assigned, unless their type has TYPE_READONLY.
3936 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3938 bool
3939 lvalue_p (const_tree ref)
3941 const enum tree_code code = TREE_CODE (ref);
3943 switch (code)
3945 case REALPART_EXPR:
3946 case IMAGPART_EXPR:
3947 case COMPONENT_REF:
3948 return lvalue_p (TREE_OPERAND (ref, 0));
3950 case C_MAYBE_CONST_EXPR:
3951 return lvalue_p (TREE_OPERAND (ref, 1));
3953 case COMPOUND_LITERAL_EXPR:
3954 case STRING_CST:
3955 return 1;
3957 case INDIRECT_REF:
3958 case ARRAY_REF:
3959 case VAR_DECL:
3960 case PARM_DECL:
3961 case RESULT_DECL:
3962 case ERROR_MARK:
3963 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3964 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3966 case BIND_EXPR:
3967 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3969 default:
3970 return 0;
3974 /* Give a warning for storing in something that is read-only in GCC
3975 terms but not const in ISO C terms. */
3977 static void
3978 readonly_warning (tree arg, enum lvalue_use use)
3980 switch (use)
3982 case lv_assign:
3983 warning (0, "assignment of read-only location %qE", arg);
3984 break;
3985 case lv_increment:
3986 warning (0, "increment of read-only location %qE", arg);
3987 break;
3988 case lv_decrement:
3989 warning (0, "decrement of read-only location %qE", arg);
3990 break;
3991 default:
3992 gcc_unreachable ();
3994 return;
3998 /* Return nonzero if REF is an lvalue valid for this language;
3999 otherwise, print an error message and return zero. USE says
4000 how the lvalue is being used and so selects the error message.
4001 LOCATION is the location at which any error should be reported. */
4003 static int
4004 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4006 int win = lvalue_p (ref);
4008 if (!win)
4009 lvalue_error (loc, use);
4011 return win;
4014 /* Mark EXP saying that we need to be able to take the
4015 address of it; it should not be allocated in a register.
4016 Returns true if successful. */
4018 bool
4019 c_mark_addressable (tree exp)
4021 tree x = exp;
4023 while (1)
4024 switch (TREE_CODE (x))
4026 case COMPONENT_REF:
4027 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4029 error
4030 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4031 return false;
4034 /* ... fall through ... */
4036 case ADDR_EXPR:
4037 case ARRAY_REF:
4038 case REALPART_EXPR:
4039 case IMAGPART_EXPR:
4040 x = TREE_OPERAND (x, 0);
4041 break;
4043 case COMPOUND_LITERAL_EXPR:
4044 case CONSTRUCTOR:
4045 TREE_ADDRESSABLE (x) = 1;
4046 return true;
4048 case VAR_DECL:
4049 case CONST_DECL:
4050 case PARM_DECL:
4051 case RESULT_DECL:
4052 if (C_DECL_REGISTER (x)
4053 && DECL_NONLOCAL (x))
4055 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4057 error
4058 ("global register variable %qD used in nested function", x);
4059 return false;
4061 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4063 else if (C_DECL_REGISTER (x))
4065 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4066 error ("address of global register variable %qD requested", x);
4067 else
4068 error ("address of register variable %qD requested", x);
4069 return false;
4072 /* drops in */
4073 case FUNCTION_DECL:
4074 TREE_ADDRESSABLE (x) = 1;
4075 /* drops out */
4076 default:
4077 return true;
4081 /* Convert EXPR to TYPE, warning about conversion problems with
4082 constants. SEMANTIC_TYPE is the type this conversion would use
4083 without excess precision. If SEMANTIC_TYPE is NULL, this function
4084 is equivalent to convert_and_check. This function is a wrapper that
4085 handles conversions that may be different than
4086 the usual ones because of excess precision. */
4088 static tree
4089 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4091 if (TREE_TYPE (expr) == type)
4092 return expr;
4094 if (!semantic_type)
4095 return convert_and_check (type, expr);
4097 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4098 && TREE_TYPE (expr) != semantic_type)
4100 /* For integers, we need to check the real conversion, not
4101 the conversion to the excess precision type. */
4102 expr = convert_and_check (semantic_type, expr);
4104 /* Result type is the excess precision type, which should be
4105 large enough, so do not check. */
4106 return convert (type, expr);
4109 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4110 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4111 if folded to an integer constant then the unselected half may
4112 contain arbitrary operations not normally permitted in constant
4113 expressions. Set the location of the expression to LOC. */
4115 tree
4116 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4117 tree op1, tree op1_original_type, tree op2,
4118 tree op2_original_type)
4120 tree type1;
4121 tree type2;
4122 enum tree_code code1;
4123 enum tree_code code2;
4124 tree result_type = NULL;
4125 tree semantic_result_type = NULL;
4126 tree orig_op1 = op1, orig_op2 = op2;
4127 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4128 bool ifexp_int_operands;
4129 tree ret;
4131 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4132 if (op1_int_operands)
4133 op1 = remove_c_maybe_const_expr (op1);
4134 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4135 if (op2_int_operands)
4136 op2 = remove_c_maybe_const_expr (op2);
4137 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4138 if (ifexp_int_operands)
4139 ifexp = remove_c_maybe_const_expr (ifexp);
4141 /* Promote both alternatives. */
4143 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4144 op1 = default_conversion (op1);
4145 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4146 op2 = default_conversion (op2);
4148 if (TREE_CODE (ifexp) == ERROR_MARK
4149 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4150 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4151 return error_mark_node;
4153 type1 = TREE_TYPE (op1);
4154 code1 = TREE_CODE (type1);
4155 type2 = TREE_TYPE (op2);
4156 code2 = TREE_CODE (type2);
4158 /* C90 does not permit non-lvalue arrays in conditional expressions.
4159 In C99 they will be pointers by now. */
4160 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4162 error_at (colon_loc, "non-lvalue array in conditional expression");
4163 return error_mark_node;
4166 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4167 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4168 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4169 || code1 == COMPLEX_TYPE)
4170 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4171 || code2 == COMPLEX_TYPE))
4173 semantic_result_type = c_common_type (type1, type2);
4174 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4176 op1 = TREE_OPERAND (op1, 0);
4177 type1 = TREE_TYPE (op1);
4178 gcc_assert (TREE_CODE (type1) == code1);
4180 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4182 op2 = TREE_OPERAND (op2, 0);
4183 type2 = TREE_TYPE (op2);
4184 gcc_assert (TREE_CODE (type2) == code2);
4188 if (warn_cxx_compat)
4190 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4191 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4193 if (TREE_CODE (t1) == ENUMERAL_TYPE
4194 && TREE_CODE (t2) == ENUMERAL_TYPE
4195 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4196 warning_at (colon_loc, OPT_Wc___compat,
4197 ("different enum types in conditional is "
4198 "invalid in C++: %qT vs %qT"),
4199 t1, t2);
4202 /* Quickly detect the usual case where op1 and op2 have the same type
4203 after promotion. */
4204 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4206 if (type1 == type2)
4207 result_type = type1;
4208 else
4209 result_type = TYPE_MAIN_VARIANT (type1);
4211 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4212 || code1 == COMPLEX_TYPE)
4213 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4214 || code2 == COMPLEX_TYPE))
4216 result_type = c_common_type (type1, type2);
4217 do_warn_double_promotion (result_type, type1, type2,
4218 "implicit conversion from %qT to %qT to "
4219 "match other result of conditional",
4220 colon_loc);
4222 /* If -Wsign-compare, warn here if type1 and type2 have
4223 different signedness. We'll promote the signed to unsigned
4224 and later code won't know it used to be different.
4225 Do this check on the original types, so that explicit casts
4226 will be considered, but default promotions won't. */
4227 if (c_inhibit_evaluation_warnings == 0)
4229 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4230 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4232 if (unsigned_op1 ^ unsigned_op2)
4234 bool ovf;
4236 /* Do not warn if the result type is signed, since the
4237 signed type will only be chosen if it can represent
4238 all the values of the unsigned type. */
4239 if (!TYPE_UNSIGNED (result_type))
4240 /* OK */;
4241 else
4243 bool op1_maybe_const = true;
4244 bool op2_maybe_const = true;
4246 /* Do not warn if the signed quantity is an
4247 unsuffixed integer literal (or some static
4248 constant expression involving such literals) and
4249 it is non-negative. This warning requires the
4250 operands to be folded for best results, so do
4251 that folding in this case even without
4252 warn_sign_compare to avoid warning options
4253 possibly affecting code generation. */
4254 c_inhibit_evaluation_warnings
4255 += (ifexp == truthvalue_false_node);
4256 op1 = c_fully_fold (op1, require_constant_value,
4257 &op1_maybe_const);
4258 c_inhibit_evaluation_warnings
4259 -= (ifexp == truthvalue_false_node);
4261 c_inhibit_evaluation_warnings
4262 += (ifexp == truthvalue_true_node);
4263 op2 = c_fully_fold (op2, require_constant_value,
4264 &op2_maybe_const);
4265 c_inhibit_evaluation_warnings
4266 -= (ifexp == truthvalue_true_node);
4268 if (warn_sign_compare)
4270 if ((unsigned_op2
4271 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4272 || (unsigned_op1
4273 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4274 /* OK */;
4275 else
4276 warning_at (colon_loc, OPT_Wsign_compare,
4277 ("signed and unsigned type in "
4278 "conditional expression"));
4280 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4281 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4282 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4283 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4288 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4290 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4291 pedwarn (colon_loc, OPT_pedantic,
4292 "ISO C forbids conditional expr with only one void side");
4293 result_type = void_type_node;
4295 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4297 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4298 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4299 addr_space_t as_common;
4301 if (comp_target_types (colon_loc, type1, type2))
4302 result_type = common_pointer_type (type1, type2);
4303 else if (null_pointer_constant_p (orig_op1))
4304 result_type = type2;
4305 else if (null_pointer_constant_p (orig_op2))
4306 result_type = type1;
4307 else if (!addr_space_superset (as1, as2, &as_common))
4309 error_at (colon_loc, "pointers to disjoint address spaces "
4310 "used in conditional expression");
4311 return error_mark_node;
4313 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4315 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4316 pedwarn (colon_loc, OPT_pedantic,
4317 "ISO C forbids conditional expr between "
4318 "%<void *%> and function pointer");
4319 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4320 TREE_TYPE (type2)));
4322 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4324 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4325 pedwarn (colon_loc, OPT_pedantic,
4326 "ISO C forbids conditional expr between "
4327 "%<void *%> and function pointer");
4328 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4329 TREE_TYPE (type1)));
4331 /* Objective-C pointer comparisons are a bit more lenient. */
4332 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4333 result_type = objc_common_type (type1, type2);
4334 else
4336 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4338 pedwarn (colon_loc, 0,
4339 "pointer type mismatch in conditional expression");
4340 result_type = build_pointer_type
4341 (build_qualified_type (void_type_node, qual));
4344 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4346 if (!null_pointer_constant_p (orig_op2))
4347 pedwarn (colon_loc, 0,
4348 "pointer/integer type mismatch in conditional expression");
4349 else
4351 op2 = null_pointer_node;
4353 result_type = type1;
4355 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4357 if (!null_pointer_constant_p (orig_op1))
4358 pedwarn (colon_loc, 0,
4359 "pointer/integer type mismatch in conditional expression");
4360 else
4362 op1 = null_pointer_node;
4364 result_type = type2;
4367 if (!result_type)
4369 if (flag_cond_mismatch)
4370 result_type = void_type_node;
4371 else
4373 error_at (colon_loc, "type mismatch in conditional expression");
4374 return error_mark_node;
4378 /* Merge const and volatile flags of the incoming types. */
4379 result_type
4380 = build_type_variant (result_type,
4381 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4382 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4384 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4385 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4387 if (ifexp_bcp && ifexp == truthvalue_true_node)
4389 op2_int_operands = true;
4390 op1 = c_fully_fold (op1, require_constant_value, NULL);
4392 if (ifexp_bcp && ifexp == truthvalue_false_node)
4394 op1_int_operands = true;
4395 op2 = c_fully_fold (op2, require_constant_value, NULL);
4397 int_const = int_operands = (ifexp_int_operands
4398 && op1_int_operands
4399 && op2_int_operands);
4400 if (int_operands)
4402 int_const = ((ifexp == truthvalue_true_node
4403 && TREE_CODE (orig_op1) == INTEGER_CST
4404 && !TREE_OVERFLOW (orig_op1))
4405 || (ifexp == truthvalue_false_node
4406 && TREE_CODE (orig_op2) == INTEGER_CST
4407 && !TREE_OVERFLOW (orig_op2)));
4409 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4410 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4411 else
4413 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4414 if (int_operands)
4415 ret = note_integer_operands (ret);
4417 if (semantic_result_type)
4418 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4420 protected_set_expr_location (ret, colon_loc);
4421 return ret;
4424 /* Return a compound expression that performs two expressions and
4425 returns the value of the second of them.
4427 LOC is the location of the COMPOUND_EXPR. */
4429 tree
4430 build_compound_expr (location_t loc, tree expr1, tree expr2)
4432 bool expr1_int_operands, expr2_int_operands;
4433 tree eptype = NULL_TREE;
4434 tree ret;
4436 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4437 if (expr1_int_operands)
4438 expr1 = remove_c_maybe_const_expr (expr1);
4439 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4440 if (expr2_int_operands)
4441 expr2 = remove_c_maybe_const_expr (expr2);
4443 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4444 expr1 = TREE_OPERAND (expr1, 0);
4445 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4447 eptype = TREE_TYPE (expr2);
4448 expr2 = TREE_OPERAND (expr2, 0);
4451 if (!TREE_SIDE_EFFECTS (expr1))
4453 /* The left-hand operand of a comma expression is like an expression
4454 statement: with -Wunused, we should warn if it doesn't have
4455 any side-effects, unless it was explicitly cast to (void). */
4456 if (warn_unused_value)
4458 if (VOID_TYPE_P (TREE_TYPE (expr1))
4459 && CONVERT_EXPR_P (expr1))
4460 ; /* (void) a, b */
4461 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4462 && TREE_CODE (expr1) == COMPOUND_EXPR
4463 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4464 ; /* (void) a, (void) b, c */
4465 else
4466 warning_at (loc, OPT_Wunused_value,
4467 "left-hand operand of comma expression has no effect");
4471 /* With -Wunused, we should also warn if the left-hand operand does have
4472 side-effects, but computes a value which is not used. For example, in
4473 `foo() + bar(), baz()' the result of the `+' operator is not used,
4474 so we should issue a warning. */
4475 else if (warn_unused_value)
4476 warn_if_unused_value (expr1, loc);
4478 if (expr2 == error_mark_node)
4479 return error_mark_node;
4481 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4483 if (flag_isoc99
4484 && expr1_int_operands
4485 && expr2_int_operands)
4486 ret = note_integer_operands (ret);
4488 if (eptype)
4489 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4491 protected_set_expr_location (ret, loc);
4492 return ret;
4495 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4496 which we are casting. OTYPE is the type of the expression being
4497 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4498 of the cast. -Wcast-qual appeared on the command line. Named
4499 address space qualifiers are not handled here, because they result
4500 in different warnings. */
4502 static void
4503 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4505 tree in_type = type;
4506 tree in_otype = otype;
4507 int added = 0;
4508 int discarded = 0;
4509 bool is_const;
4511 /* Check that the qualifiers on IN_TYPE are a superset of the
4512 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4513 nodes is uninteresting and we stop as soon as we hit a
4514 non-POINTER_TYPE node on either type. */
4517 in_otype = TREE_TYPE (in_otype);
4518 in_type = TREE_TYPE (in_type);
4520 /* GNU C allows cv-qualified function types. 'const' means the
4521 function is very pure, 'volatile' means it can't return. We
4522 need to warn when such qualifiers are added, not when they're
4523 taken away. */
4524 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4525 && TREE_CODE (in_type) == FUNCTION_TYPE)
4526 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4527 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4528 else
4529 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4530 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4532 while (TREE_CODE (in_type) == POINTER_TYPE
4533 && TREE_CODE (in_otype) == POINTER_TYPE);
4535 if (added)
4536 warning_at (loc, OPT_Wcast_qual,
4537 "cast adds %q#v qualifier to function type", added);
4539 if (discarded)
4540 /* There are qualifiers present in IN_OTYPE that are not present
4541 in IN_TYPE. */
4542 warning_at (loc, OPT_Wcast_qual,
4543 "cast discards %q#v qualifier from pointer target type",
4544 discarded);
4546 if (added || discarded)
4547 return;
4549 /* A cast from **T to const **T is unsafe, because it can cause a
4550 const value to be changed with no additional warning. We only
4551 issue this warning if T is the same on both sides, and we only
4552 issue the warning if there are the same number of pointers on
4553 both sides, as otherwise the cast is clearly unsafe anyhow. A
4554 cast is unsafe when a qualifier is added at one level and const
4555 is not present at all outer levels.
4557 To issue this warning, we check at each level whether the cast
4558 adds new qualifiers not already seen. We don't need to special
4559 case function types, as they won't have the same
4560 TYPE_MAIN_VARIANT. */
4562 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4563 return;
4564 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4565 return;
4567 in_type = type;
4568 in_otype = otype;
4569 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4572 in_type = TREE_TYPE (in_type);
4573 in_otype = TREE_TYPE (in_otype);
4574 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4575 && !is_const)
4577 warning_at (loc, OPT_Wcast_qual,
4578 "to be safe all intermediate pointers in cast from "
4579 "%qT to %qT must be %<const%> qualified",
4580 otype, type);
4581 break;
4583 if (is_const)
4584 is_const = TYPE_READONLY (in_type);
4586 while (TREE_CODE (in_type) == POINTER_TYPE);
4589 /* Build an expression representing a cast to type TYPE of expression EXPR.
4590 LOC is the location of the cast-- typically the open paren of the cast. */
4592 tree
4593 build_c_cast (location_t loc, tree type, tree expr)
4595 tree value;
4597 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4598 expr = TREE_OPERAND (expr, 0);
4600 value = expr;
4602 if (type == error_mark_node || expr == error_mark_node)
4603 return error_mark_node;
4605 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4606 only in <protocol> qualifications. But when constructing cast expressions,
4607 the protocols do matter and must be kept around. */
4608 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4609 return build1 (NOP_EXPR, type, expr);
4611 type = TYPE_MAIN_VARIANT (type);
4613 if (TREE_CODE (type) == ARRAY_TYPE)
4615 error_at (loc, "cast specifies array type");
4616 return error_mark_node;
4619 if (TREE_CODE (type) == FUNCTION_TYPE)
4621 error_at (loc, "cast specifies function type");
4622 return error_mark_node;
4625 if (!VOID_TYPE_P (type))
4627 value = require_complete_type (value);
4628 if (value == error_mark_node)
4629 return error_mark_node;
4632 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4634 if (TREE_CODE (type) == RECORD_TYPE
4635 || TREE_CODE (type) == UNION_TYPE)
4636 pedwarn (loc, OPT_pedantic,
4637 "ISO C forbids casting nonscalar to the same type");
4639 else if (TREE_CODE (type) == UNION_TYPE)
4641 tree field;
4643 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4644 if (TREE_TYPE (field) != error_mark_node
4645 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4646 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4647 break;
4649 if (field)
4651 tree t;
4652 bool maybe_const = true;
4654 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4655 t = c_fully_fold (value, false, &maybe_const);
4656 t = build_constructor_single (type, field, t);
4657 if (!maybe_const)
4658 t = c_wrap_maybe_const (t, true);
4659 t = digest_init (loc, type, t,
4660 NULL_TREE, false, true, 0);
4661 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4662 return t;
4664 error_at (loc, "cast to union type from type not present in union");
4665 return error_mark_node;
4667 else
4669 tree otype, ovalue;
4671 if (type == void_type_node)
4673 tree t = build1 (CONVERT_EXPR, type, value);
4674 SET_EXPR_LOCATION (t, loc);
4675 return t;
4678 otype = TREE_TYPE (value);
4680 /* Optionally warn about potentially worrisome casts. */
4681 if (warn_cast_qual
4682 && TREE_CODE (type) == POINTER_TYPE
4683 && TREE_CODE (otype) == POINTER_TYPE)
4684 handle_warn_cast_qual (loc, type, otype);
4686 /* Warn about conversions between pointers to disjoint
4687 address spaces. */
4688 if (TREE_CODE (type) == POINTER_TYPE
4689 && TREE_CODE (otype) == POINTER_TYPE
4690 && !null_pointer_constant_p (value))
4692 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4693 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4694 addr_space_t as_common;
4696 if (!addr_space_superset (as_to, as_from, &as_common))
4698 if (ADDR_SPACE_GENERIC_P (as_from))
4699 warning_at (loc, 0, "cast to %s address space pointer "
4700 "from disjoint generic address space pointer",
4701 c_addr_space_name (as_to));
4703 else if (ADDR_SPACE_GENERIC_P (as_to))
4704 warning_at (loc, 0, "cast to generic address space pointer "
4705 "from disjoint %s address space pointer",
4706 c_addr_space_name (as_from));
4708 else
4709 warning_at (loc, 0, "cast to %s address space pointer "
4710 "from disjoint %s address space pointer",
4711 c_addr_space_name (as_to),
4712 c_addr_space_name (as_from));
4716 /* Warn about possible alignment problems. */
4717 if (STRICT_ALIGNMENT
4718 && TREE_CODE (type) == POINTER_TYPE
4719 && TREE_CODE (otype) == POINTER_TYPE
4720 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4721 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4722 /* Don't warn about opaque types, where the actual alignment
4723 restriction is unknown. */
4724 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4725 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4726 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4727 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4728 warning_at (loc, OPT_Wcast_align,
4729 "cast increases required alignment of target type");
4731 if (TREE_CODE (type) == INTEGER_TYPE
4732 && TREE_CODE (otype) == POINTER_TYPE
4733 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4734 /* Unlike conversion of integers to pointers, where the
4735 warning is disabled for converting constants because
4736 of cases such as SIG_*, warn about converting constant
4737 pointers to integers. In some cases it may cause unwanted
4738 sign extension, and a warning is appropriate. */
4739 warning_at (loc, OPT_Wpointer_to_int_cast,
4740 "cast from pointer to integer of different size");
4742 if (TREE_CODE (value) == CALL_EXPR
4743 && TREE_CODE (type) != TREE_CODE (otype))
4744 warning_at (loc, OPT_Wbad_function_cast,
4745 "cast from function call of type %qT "
4746 "to non-matching type %qT", otype, type);
4748 if (TREE_CODE (type) == POINTER_TYPE
4749 && TREE_CODE (otype) == INTEGER_TYPE
4750 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4751 /* Don't warn about converting any constant. */
4752 && !TREE_CONSTANT (value))
4753 warning_at (loc,
4754 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4755 "of different size");
4757 if (warn_strict_aliasing <= 2)
4758 strict_aliasing_warning (otype, type, expr);
4760 /* If pedantic, warn for conversions between function and object
4761 pointer types, except for converting a null pointer constant
4762 to function pointer type. */
4763 if (pedantic
4764 && TREE_CODE (type) == POINTER_TYPE
4765 && TREE_CODE (otype) == POINTER_TYPE
4766 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4767 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4768 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4769 "conversion of function pointer to object pointer type");
4771 if (pedantic
4772 && TREE_CODE (type) == POINTER_TYPE
4773 && TREE_CODE (otype) == POINTER_TYPE
4774 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4775 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4776 && !null_pointer_constant_p (value))
4777 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4778 "conversion of object pointer to function pointer type");
4780 ovalue = value;
4781 value = convert (type, value);
4783 /* Ignore any integer overflow caused by the cast. */
4784 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4786 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4788 if (!TREE_OVERFLOW (value))
4790 /* Avoid clobbering a shared constant. */
4791 value = copy_node (value);
4792 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4795 else if (TREE_OVERFLOW (value))
4796 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4797 value = build_int_cst_wide (TREE_TYPE (value),
4798 TREE_INT_CST_LOW (value),
4799 TREE_INT_CST_HIGH (value));
4803 /* Don't let a cast be an lvalue. */
4804 if (value == expr)
4805 value = non_lvalue_loc (loc, value);
4807 /* Don't allow the results of casting to floating-point or complex
4808 types be confused with actual constants, or casts involving
4809 integer and pointer types other than direct integer-to-integer
4810 and integer-to-pointer be confused with integer constant
4811 expressions and null pointer constants. */
4812 if (TREE_CODE (value) == REAL_CST
4813 || TREE_CODE (value) == COMPLEX_CST
4814 || (TREE_CODE (value) == INTEGER_CST
4815 && !((TREE_CODE (expr) == INTEGER_CST
4816 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4817 || TREE_CODE (expr) == REAL_CST
4818 || TREE_CODE (expr) == COMPLEX_CST)))
4819 value = build1 (NOP_EXPR, type, value);
4821 if (CAN_HAVE_LOCATION_P (value))
4822 SET_EXPR_LOCATION (value, loc);
4823 return value;
4826 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4827 location of the open paren of the cast, or the position of the cast
4828 expr. */
4829 tree
4830 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4832 tree type;
4833 tree type_expr = NULL_TREE;
4834 bool type_expr_const = true;
4835 tree ret;
4836 int saved_wsp = warn_strict_prototypes;
4838 /* This avoids warnings about unprototyped casts on
4839 integers. E.g. "#define SIG_DFL (void(*)())0". */
4840 if (TREE_CODE (expr) == INTEGER_CST)
4841 warn_strict_prototypes = 0;
4842 type = groktypename (type_name, &type_expr, &type_expr_const);
4843 warn_strict_prototypes = saved_wsp;
4845 ret = build_c_cast (loc, type, expr);
4846 if (type_expr)
4848 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4849 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4850 SET_EXPR_LOCATION (ret, loc);
4853 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4854 SET_EXPR_LOCATION (ret, loc);
4856 /* C++ does not permits types to be defined in a cast, but it
4857 allows references to incomplete types. */
4858 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4859 warning_at (loc, OPT_Wc___compat,
4860 "defining a type in a cast is invalid in C++");
4862 return ret;
4865 /* Build an assignment expression of lvalue LHS from value RHS.
4866 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4867 may differ from TREE_TYPE (LHS) for an enum bitfield.
4868 MODIFYCODE is the code for a binary operator that we use
4869 to combine the old value of LHS with RHS to get the new value.
4870 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4871 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4872 which may differ from TREE_TYPE (RHS) for an enum value.
4874 LOCATION is the location of the MODIFYCODE operator.
4875 RHS_LOC is the location of the RHS. */
4877 tree
4878 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4879 enum tree_code modifycode,
4880 location_t rhs_loc, tree rhs, tree rhs_origtype)
4882 tree result;
4883 tree newrhs;
4884 tree rhs_semantic_type = NULL_TREE;
4885 tree lhstype = TREE_TYPE (lhs);
4886 tree olhstype = lhstype;
4887 bool npc;
4889 /* Types that aren't fully specified cannot be used in assignments. */
4890 lhs = require_complete_type (lhs);
4892 /* Avoid duplicate error messages from operands that had errors. */
4893 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4894 return error_mark_node;
4896 /* For ObjC properties, defer this check. */
4897 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4898 return error_mark_node;
4900 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4902 rhs_semantic_type = TREE_TYPE (rhs);
4903 rhs = TREE_OPERAND (rhs, 0);
4906 newrhs = rhs;
4908 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4910 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4911 lhs_origtype, modifycode, rhs_loc, rhs,
4912 rhs_origtype);
4913 if (inner == error_mark_node)
4914 return error_mark_node;
4915 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4916 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4917 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4918 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4919 protected_set_expr_location (result, location);
4920 return result;
4923 /* If a binary op has been requested, combine the old LHS value with the RHS
4924 producing the value we should actually store into the LHS. */
4926 if (modifycode != NOP_EXPR)
4928 lhs = c_fully_fold (lhs, false, NULL);
4929 lhs = stabilize_reference (lhs);
4930 newrhs = build_binary_op (location,
4931 modifycode, lhs, rhs, 1);
4933 /* The original type of the right hand side is no longer
4934 meaningful. */
4935 rhs_origtype = NULL_TREE;
4938 if (c_dialect_objc ())
4940 /* Check if we are modifying an Objective-C property reference;
4941 if so, we need to generate setter calls. */
4942 result = objc_maybe_build_modify_expr (lhs, newrhs);
4943 if (result)
4944 return result;
4946 /* Else, do the check that we postponed for Objective-C. */
4947 if (!lvalue_or_else (location, lhs, lv_assign))
4948 return error_mark_node;
4951 /* Give an error for storing in something that is 'const'. */
4953 if (TYPE_READONLY (lhstype)
4954 || ((TREE_CODE (lhstype) == RECORD_TYPE
4955 || TREE_CODE (lhstype) == UNION_TYPE)
4956 && C_TYPE_FIELDS_READONLY (lhstype)))
4958 readonly_error (lhs, lv_assign);
4959 return error_mark_node;
4961 else if (TREE_READONLY (lhs))
4962 readonly_warning (lhs, lv_assign);
4964 /* If storing into a structure or union member,
4965 it has probably been given type `int'.
4966 Compute the type that would go with
4967 the actual amount of storage the member occupies. */
4969 if (TREE_CODE (lhs) == COMPONENT_REF
4970 && (TREE_CODE (lhstype) == INTEGER_TYPE
4971 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4972 || TREE_CODE (lhstype) == REAL_TYPE
4973 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4974 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4976 /* If storing in a field that is in actuality a short or narrower than one,
4977 we must store in the field in its actual type. */
4979 if (lhstype != TREE_TYPE (lhs))
4981 lhs = copy_node (lhs);
4982 TREE_TYPE (lhs) = lhstype;
4985 /* Issue -Wc++-compat warnings about an assignment to an enum type
4986 when LHS does not have its original type. This happens for,
4987 e.g., an enum bitfield in a struct. */
4988 if (warn_cxx_compat
4989 && lhs_origtype != NULL_TREE
4990 && lhs_origtype != lhstype
4991 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4993 tree checktype = (rhs_origtype != NULL_TREE
4994 ? rhs_origtype
4995 : TREE_TYPE (rhs));
4996 if (checktype != error_mark_node
4997 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4998 warning_at (location, OPT_Wc___compat,
4999 "enum conversion in assignment is invalid in C++");
5002 /* Convert new value to destination type. Fold it first, then
5003 restore any excess precision information, for the sake of
5004 conversion warnings. */
5006 npc = null_pointer_constant_p (newrhs);
5007 newrhs = c_fully_fold (newrhs, false, NULL);
5008 if (rhs_semantic_type)
5009 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5010 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
5011 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
5012 if (TREE_CODE (newrhs) == ERROR_MARK)
5013 return error_mark_node;
5015 /* Emit ObjC write barrier, if necessary. */
5016 if (c_dialect_objc () && flag_objc_gc)
5018 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5019 if (result)
5021 protected_set_expr_location (result, location);
5022 return result;
5026 /* Scan operands. */
5028 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5029 TREE_SIDE_EFFECTS (result) = 1;
5030 protected_set_expr_location (result, location);
5032 /* If we got the LHS in a different type for storing in,
5033 convert the result back to the nominal type of LHS
5034 so that the value we return always has the same type
5035 as the LHS argument. */
5037 if (olhstype == TREE_TYPE (result))
5038 return result;
5040 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5041 ic_assign, false, NULL_TREE, NULL_TREE, 0);
5042 protected_set_expr_location (result, location);
5043 return result;
5046 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5047 This is used to implement -fplan9-extensions. */
5049 static bool
5050 find_anonymous_field_with_type (tree struct_type, tree type)
5052 tree field;
5053 bool found;
5055 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5056 || TREE_CODE (struct_type) == UNION_TYPE);
5057 found = false;
5058 for (field = TYPE_FIELDS (struct_type);
5059 field != NULL_TREE;
5060 field = TREE_CHAIN (field))
5062 if (DECL_NAME (field) == NULL
5063 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5065 if (found)
5066 return false;
5067 found = true;
5069 else if (DECL_NAME (field) == NULL
5070 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5071 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5072 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5074 if (found)
5075 return false;
5076 found = true;
5079 return found;
5082 /* RHS is an expression whose type is pointer to struct. If there is
5083 an anonymous field in RHS with type TYPE, then return a pointer to
5084 that field in RHS. This is used with -fplan9-extensions. This
5085 returns NULL if no conversion could be found. */
5087 static tree
5088 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5090 tree rhs_struct_type, lhs_main_type;
5091 tree field, found_field;
5092 bool found_sub_field;
5093 tree ret;
5095 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5096 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5097 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5098 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5100 gcc_assert (POINTER_TYPE_P (type));
5101 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5103 found_field = NULL_TREE;
5104 found_sub_field = false;
5105 for (field = TYPE_FIELDS (rhs_struct_type);
5106 field != NULL_TREE;
5107 field = TREE_CHAIN (field))
5109 if (DECL_NAME (field) != NULL_TREE
5110 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5111 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5112 continue;
5113 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5115 if (found_field != NULL_TREE)
5116 return NULL_TREE;
5117 found_field = field;
5119 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5120 lhs_main_type))
5122 if (found_field != NULL_TREE)
5123 return NULL_TREE;
5124 found_field = field;
5125 found_sub_field = true;
5129 if (found_field == NULL_TREE)
5130 return NULL_TREE;
5132 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5133 build_fold_indirect_ref (rhs), found_field,
5134 NULL_TREE);
5135 ret = build_fold_addr_expr_loc (location, ret);
5137 if (found_sub_field)
5139 ret = convert_to_anonymous_field (location, type, ret);
5140 gcc_assert (ret != NULL_TREE);
5143 return ret;
5146 /* Convert value RHS to type TYPE as preparation for an assignment to
5147 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5148 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5149 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5150 constant before any folding.
5151 The real work of conversion is done by `convert'.
5152 The purpose of this function is to generate error messages
5153 for assignments that are not allowed in C.
5154 ERRTYPE says whether it is argument passing, assignment,
5155 initialization or return.
5157 LOCATION is the location of the RHS.
5158 FUNCTION is a tree for the function being called.
5159 PARMNUM is the number of the argument, for printing in error messages. */
5161 static tree
5162 convert_for_assignment (location_t location, tree type, tree rhs,
5163 tree origtype, enum impl_conv errtype,
5164 bool null_pointer_constant, tree fundecl,
5165 tree function, int parmnum)
5167 enum tree_code codel = TREE_CODE (type);
5168 tree orig_rhs = rhs;
5169 tree rhstype;
5170 enum tree_code coder;
5171 tree rname = NULL_TREE;
5172 bool objc_ok = false;
5174 if (errtype == ic_argpass)
5176 tree selector;
5177 /* Change pointer to function to the function itself for
5178 diagnostics. */
5179 if (TREE_CODE (function) == ADDR_EXPR
5180 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5181 function = TREE_OPERAND (function, 0);
5183 /* Handle an ObjC selector specially for diagnostics. */
5184 selector = objc_message_selector ();
5185 rname = function;
5186 if (selector && parmnum > 2)
5188 rname = selector;
5189 parmnum -= 2;
5193 /* This macro is used to emit diagnostics to ensure that all format
5194 strings are complete sentences, visible to gettext and checked at
5195 compile time. */
5196 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5197 do { \
5198 switch (errtype) \
5200 case ic_argpass: \
5201 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5202 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5203 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5204 "expected %qT but argument is of type %qT", \
5205 type, rhstype); \
5206 break; \
5207 case ic_assign: \
5208 pedwarn (LOCATION, OPT, AS); \
5209 break; \
5210 case ic_init: \
5211 pedwarn_init (LOCATION, OPT, IN); \
5212 break; \
5213 case ic_return: \
5214 pedwarn (LOCATION, OPT, RE); \
5215 break; \
5216 default: \
5217 gcc_unreachable (); \
5219 } while (0)
5221 /* This macro is used to emit diagnostics to ensure that all format
5222 strings are complete sentences, visible to gettext and checked at
5223 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5224 extra parameter to enumerate qualifiers. */
5226 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5227 do { \
5228 switch (errtype) \
5230 case ic_argpass: \
5231 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5232 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5233 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5234 "expected %qT but argument is of type %qT", \
5235 type, rhstype); \
5236 break; \
5237 case ic_assign: \
5238 pedwarn (LOCATION, OPT, AS, QUALS); \
5239 break; \
5240 case ic_init: \
5241 pedwarn (LOCATION, OPT, IN, QUALS); \
5242 break; \
5243 case ic_return: \
5244 pedwarn (LOCATION, OPT, RE, QUALS); \
5245 break; \
5246 default: \
5247 gcc_unreachable (); \
5249 } while (0)
5251 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5252 rhs = TREE_OPERAND (rhs, 0);
5254 rhstype = TREE_TYPE (rhs);
5255 coder = TREE_CODE (rhstype);
5257 if (coder == ERROR_MARK)
5258 return error_mark_node;
5260 if (c_dialect_objc ())
5262 int parmno;
5264 switch (errtype)
5266 case ic_return:
5267 parmno = 0;
5268 break;
5270 case ic_assign:
5271 parmno = -1;
5272 break;
5274 case ic_init:
5275 parmno = -2;
5276 break;
5278 default:
5279 parmno = parmnum;
5280 break;
5283 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5286 if (warn_cxx_compat)
5288 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5289 if (checktype != error_mark_node
5290 && TREE_CODE (type) == ENUMERAL_TYPE
5291 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5293 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5294 G_("enum conversion when passing argument "
5295 "%d of %qE is invalid in C++"),
5296 G_("enum conversion in assignment is "
5297 "invalid in C++"),
5298 G_("enum conversion in initialization is "
5299 "invalid in C++"),
5300 G_("enum conversion in return is "
5301 "invalid in C++"));
5305 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5306 return rhs;
5308 if (coder == VOID_TYPE)
5310 /* Except for passing an argument to an unprototyped function,
5311 this is a constraint violation. When passing an argument to
5312 an unprototyped function, it is compile-time undefined;
5313 making it a constraint in that case was rejected in
5314 DR#252. */
5315 error_at (location, "void value not ignored as it ought to be");
5316 return error_mark_node;
5318 rhs = require_complete_type (rhs);
5319 if (rhs == error_mark_node)
5320 return error_mark_node;
5321 /* A type converts to a reference to it.
5322 This code doesn't fully support references, it's just for the
5323 special case of va_start and va_copy. */
5324 if (codel == REFERENCE_TYPE
5325 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5327 if (!lvalue_p (rhs))
5329 error_at (location, "cannot pass rvalue to reference parameter");
5330 return error_mark_node;
5332 if (!c_mark_addressable (rhs))
5333 return error_mark_node;
5334 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5335 SET_EXPR_LOCATION (rhs, location);
5337 /* We already know that these two types are compatible, but they
5338 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5339 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5340 likely to be va_list, a typedef to __builtin_va_list, which
5341 is different enough that it will cause problems later. */
5342 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5344 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5345 SET_EXPR_LOCATION (rhs, location);
5348 rhs = build1 (NOP_EXPR, type, rhs);
5349 SET_EXPR_LOCATION (rhs, location);
5350 return rhs;
5352 /* Some types can interconvert without explicit casts. */
5353 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5354 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5355 return convert (type, rhs);
5356 /* Arithmetic types all interconvert, and enum is treated like int. */
5357 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5358 || codel == FIXED_POINT_TYPE
5359 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5360 || codel == BOOLEAN_TYPE)
5361 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5362 || coder == FIXED_POINT_TYPE
5363 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5364 || coder == BOOLEAN_TYPE))
5366 tree ret;
5367 bool save = in_late_binary_op;
5368 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5369 in_late_binary_op = true;
5370 ret = convert_and_check (type, orig_rhs);
5371 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5372 in_late_binary_op = save;
5373 return ret;
5376 /* Aggregates in different TUs might need conversion. */
5377 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5378 && codel == coder
5379 && comptypes (type, rhstype))
5380 return convert_and_check (type, rhs);
5382 /* Conversion to a transparent union or record from its member types.
5383 This applies only to function arguments. */
5384 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5385 && TYPE_TRANSPARENT_AGGR (type))
5386 && errtype == ic_argpass)
5388 tree memb, marginal_memb = NULL_TREE;
5390 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5392 tree memb_type = TREE_TYPE (memb);
5394 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5395 TYPE_MAIN_VARIANT (rhstype)))
5396 break;
5398 if (TREE_CODE (memb_type) != POINTER_TYPE)
5399 continue;
5401 if (coder == POINTER_TYPE)
5403 tree ttl = TREE_TYPE (memb_type);
5404 tree ttr = TREE_TYPE (rhstype);
5406 /* Any non-function converts to a [const][volatile] void *
5407 and vice versa; otherwise, targets must be the same.
5408 Meanwhile, the lhs target must have all the qualifiers of
5409 the rhs. */
5410 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5411 || comp_target_types (location, memb_type, rhstype))
5413 /* If this type won't generate any warnings, use it. */
5414 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5415 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5416 && TREE_CODE (ttl) == FUNCTION_TYPE)
5417 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5418 == TYPE_QUALS (ttr))
5419 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5420 == TYPE_QUALS (ttl))))
5421 break;
5423 /* Keep looking for a better type, but remember this one. */
5424 if (!marginal_memb)
5425 marginal_memb = memb;
5429 /* Can convert integer zero to any pointer type. */
5430 if (null_pointer_constant)
5432 rhs = null_pointer_node;
5433 break;
5437 if (memb || marginal_memb)
5439 if (!memb)
5441 /* We have only a marginally acceptable member type;
5442 it needs a warning. */
5443 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5444 tree ttr = TREE_TYPE (rhstype);
5446 /* Const and volatile mean something different for function
5447 types, so the usual warnings are not appropriate. */
5448 if (TREE_CODE (ttr) == FUNCTION_TYPE
5449 && TREE_CODE (ttl) == FUNCTION_TYPE)
5451 /* Because const and volatile on functions are
5452 restrictions that say the function will not do
5453 certain things, it is okay to use a const or volatile
5454 function where an ordinary one is wanted, but not
5455 vice-versa. */
5456 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5457 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5458 WARN_FOR_QUALIFIERS (location, 0,
5459 G_("passing argument %d of %qE "
5460 "makes %q#v qualified function "
5461 "pointer from unqualified"),
5462 G_("assignment makes %q#v qualified "
5463 "function pointer from "
5464 "unqualified"),
5465 G_("initialization makes %q#v qualified "
5466 "function pointer from "
5467 "unqualified"),
5468 G_("return makes %q#v qualified function "
5469 "pointer from unqualified"),
5470 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5472 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5473 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5474 WARN_FOR_QUALIFIERS (location, 0,
5475 G_("passing argument %d of %qE discards "
5476 "%qv qualifier from pointer target type"),
5477 G_("assignment discards %qv qualifier "
5478 "from pointer target type"),
5479 G_("initialization discards %qv qualifier "
5480 "from pointer target type"),
5481 G_("return discards %qv qualifier from "
5482 "pointer target type"),
5483 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5485 memb = marginal_memb;
5488 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5489 pedwarn (location, OPT_pedantic,
5490 "ISO C prohibits argument conversion to union type");
5492 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5493 return build_constructor_single (type, memb, rhs);
5497 /* Conversions among pointers */
5498 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5499 && (coder == codel))
5501 tree ttl = TREE_TYPE (type);
5502 tree ttr = TREE_TYPE (rhstype);
5503 tree mvl = ttl;
5504 tree mvr = ttr;
5505 bool is_opaque_pointer;
5506 int target_cmp = 0; /* Cache comp_target_types () result. */
5507 addr_space_t asl;
5508 addr_space_t asr;
5510 if (TREE_CODE (mvl) != ARRAY_TYPE)
5511 mvl = TYPE_MAIN_VARIANT (mvl);
5512 if (TREE_CODE (mvr) != ARRAY_TYPE)
5513 mvr = TYPE_MAIN_VARIANT (mvr);
5514 /* Opaque pointers are treated like void pointers. */
5515 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5517 /* The Plan 9 compiler permits a pointer to a struct to be
5518 automatically converted into a pointer to an anonymous field
5519 within the struct. */
5520 if (flag_plan9_extensions
5521 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5522 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5523 && mvl != mvr)
5525 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5526 if (new_rhs != NULL_TREE)
5528 rhs = new_rhs;
5529 rhstype = TREE_TYPE (rhs);
5530 coder = TREE_CODE (rhstype);
5531 ttr = TREE_TYPE (rhstype);
5532 mvr = TYPE_MAIN_VARIANT (ttr);
5536 /* C++ does not allow the implicit conversion void* -> T*. However,
5537 for the purpose of reducing the number of false positives, we
5538 tolerate the special case of
5540 int *p = NULL;
5542 where NULL is typically defined in C to be '(void *) 0'. */
5543 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5544 warning_at (location, OPT_Wc___compat,
5545 "request for implicit conversion "
5546 "from %qT to %qT not permitted in C++", rhstype, type);
5548 /* See if the pointers point to incompatible address spaces. */
5549 asl = TYPE_ADDR_SPACE (ttl);
5550 asr = TYPE_ADDR_SPACE (ttr);
5551 if (!null_pointer_constant_p (rhs)
5552 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5554 switch (errtype)
5556 case ic_argpass:
5557 error_at (location, "passing argument %d of %qE from pointer to "
5558 "non-enclosed address space", parmnum, rname);
5559 break;
5560 case ic_assign:
5561 error_at (location, "assignment from pointer to "
5562 "non-enclosed address space");
5563 break;
5564 case ic_init:
5565 error_at (location, "initialization from pointer to "
5566 "non-enclosed address space");
5567 break;
5568 case ic_return:
5569 error_at (location, "return from pointer to "
5570 "non-enclosed address space");
5571 break;
5572 default:
5573 gcc_unreachable ();
5575 return error_mark_node;
5578 /* Check if the right-hand side has a format attribute but the
5579 left-hand side doesn't. */
5580 if (warn_missing_format_attribute
5581 && check_missing_format_attribute (type, rhstype))
5583 switch (errtype)
5585 case ic_argpass:
5586 warning_at (location, OPT_Wmissing_format_attribute,
5587 "argument %d of %qE might be "
5588 "a candidate for a format attribute",
5589 parmnum, rname);
5590 break;
5591 case ic_assign:
5592 warning_at (location, OPT_Wmissing_format_attribute,
5593 "assignment left-hand side might be "
5594 "a candidate for a format attribute");
5595 break;
5596 case ic_init:
5597 warning_at (location, OPT_Wmissing_format_attribute,
5598 "initialization left-hand side might be "
5599 "a candidate for a format attribute");
5600 break;
5601 case ic_return:
5602 warning_at (location, OPT_Wmissing_format_attribute,
5603 "return type might be "
5604 "a candidate for a format attribute");
5605 break;
5606 default:
5607 gcc_unreachable ();
5611 /* Any non-function converts to a [const][volatile] void *
5612 and vice versa; otherwise, targets must be the same.
5613 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5614 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5615 || (target_cmp = comp_target_types (location, type, rhstype))
5616 || is_opaque_pointer
5617 || (c_common_unsigned_type (mvl)
5618 == c_common_unsigned_type (mvr)))
5620 if (pedantic
5621 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5623 (VOID_TYPE_P (ttr)
5624 && !null_pointer_constant
5625 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5626 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5627 G_("ISO C forbids passing argument %d of "
5628 "%qE between function pointer "
5629 "and %<void *%>"),
5630 G_("ISO C forbids assignment between "
5631 "function pointer and %<void *%>"),
5632 G_("ISO C forbids initialization between "
5633 "function pointer and %<void *%>"),
5634 G_("ISO C forbids return between function "
5635 "pointer and %<void *%>"));
5636 /* Const and volatile mean something different for function types,
5637 so the usual warnings are not appropriate. */
5638 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5639 && TREE_CODE (ttl) != FUNCTION_TYPE)
5641 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5642 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5644 WARN_FOR_QUALIFIERS (location, 0,
5645 G_("passing argument %d of %qE discards "
5646 "%qv qualifier from pointer target type"),
5647 G_("assignment discards %qv qualifier "
5648 "from pointer target type"),
5649 G_("initialization discards %qv qualifier "
5650 "from pointer target type"),
5651 G_("return discards %qv qualifier from "
5652 "pointer target type"),
5653 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5655 /* If this is not a case of ignoring a mismatch in signedness,
5656 no warning. */
5657 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5658 || target_cmp)
5660 /* If there is a mismatch, do warn. */
5661 else if (warn_pointer_sign)
5662 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5663 G_("pointer targets in passing argument "
5664 "%d of %qE differ in signedness"),
5665 G_("pointer targets in assignment "
5666 "differ in signedness"),
5667 G_("pointer targets in initialization "
5668 "differ in signedness"),
5669 G_("pointer targets in return differ "
5670 "in signedness"));
5672 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5673 && TREE_CODE (ttr) == FUNCTION_TYPE)
5675 /* Because const and volatile on functions are restrictions
5676 that say the function will not do certain things,
5677 it is okay to use a const or volatile function
5678 where an ordinary one is wanted, but not vice-versa. */
5679 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5680 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5681 WARN_FOR_QUALIFIERS (location, 0,
5682 G_("passing argument %d of %qE makes "
5683 "%q#v qualified function pointer "
5684 "from unqualified"),
5685 G_("assignment makes %q#v qualified function "
5686 "pointer from unqualified"),
5687 G_("initialization makes %q#v qualified "
5688 "function pointer from unqualified"),
5689 G_("return makes %q#v qualified function "
5690 "pointer from unqualified"),
5691 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5694 else
5695 /* Avoid warning about the volatile ObjC EH puts on decls. */
5696 if (!objc_ok)
5697 WARN_FOR_ASSIGNMENT (location, 0,
5698 G_("passing argument %d of %qE from "
5699 "incompatible pointer type"),
5700 G_("assignment from incompatible pointer type"),
5701 G_("initialization from incompatible "
5702 "pointer type"),
5703 G_("return from incompatible pointer type"));
5705 return convert (type, rhs);
5707 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5709 /* ??? This should not be an error when inlining calls to
5710 unprototyped functions. */
5711 error_at (location, "invalid use of non-lvalue array");
5712 return error_mark_node;
5714 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5716 /* An explicit constant 0 can convert to a pointer,
5717 or one that results from arithmetic, even including
5718 a cast to integer type. */
5719 if (!null_pointer_constant)
5720 WARN_FOR_ASSIGNMENT (location, 0,
5721 G_("passing argument %d of %qE makes "
5722 "pointer from integer without a cast"),
5723 G_("assignment makes pointer from integer "
5724 "without a cast"),
5725 G_("initialization makes pointer from "
5726 "integer without a cast"),
5727 G_("return makes pointer from integer "
5728 "without a cast"));
5730 return convert (type, rhs);
5732 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5734 WARN_FOR_ASSIGNMENT (location, 0,
5735 G_("passing argument %d of %qE makes integer "
5736 "from pointer without a cast"),
5737 G_("assignment makes integer from pointer "
5738 "without a cast"),
5739 G_("initialization makes integer from pointer "
5740 "without a cast"),
5741 G_("return makes integer from pointer "
5742 "without a cast"));
5743 return convert (type, rhs);
5745 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5747 tree ret;
5748 bool save = in_late_binary_op;
5749 in_late_binary_op = true;
5750 ret = convert (type, rhs);
5751 in_late_binary_op = save;
5752 return ret;
5755 switch (errtype)
5757 case ic_argpass:
5758 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5759 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5760 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5761 "expected %qT but argument is of type %qT", type, rhstype);
5762 break;
5763 case ic_assign:
5764 error_at (location, "incompatible types when assigning to type %qT from "
5765 "type %qT", type, rhstype);
5766 break;
5767 case ic_init:
5768 error_at (location,
5769 "incompatible types when initializing type %qT using type %qT",
5770 type, rhstype);
5771 break;
5772 case ic_return:
5773 error_at (location,
5774 "incompatible types when returning type %qT but %qT was "
5775 "expected", rhstype, type);
5776 break;
5777 default:
5778 gcc_unreachable ();
5781 return error_mark_node;
5784 /* If VALUE is a compound expr all of whose expressions are constant, then
5785 return its value. Otherwise, return error_mark_node.
5787 This is for handling COMPOUND_EXPRs as initializer elements
5788 which is allowed with a warning when -pedantic is specified. */
5790 static tree
5791 valid_compound_expr_initializer (tree value, tree endtype)
5793 if (TREE_CODE (value) == COMPOUND_EXPR)
5795 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5796 == error_mark_node)
5797 return error_mark_node;
5798 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5799 endtype);
5801 else if (!initializer_constant_valid_p (value, endtype))
5802 return error_mark_node;
5803 else
5804 return value;
5807 /* Perform appropriate conversions on the initial value of a variable,
5808 store it in the declaration DECL,
5809 and print any error messages that are appropriate.
5810 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5811 If the init is invalid, store an ERROR_MARK.
5813 INIT_LOC is the location of the initial value. */
5815 void
5816 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5818 tree value, type;
5819 bool npc = false;
5821 /* If variable's type was invalidly declared, just ignore it. */
5823 type = TREE_TYPE (decl);
5824 if (TREE_CODE (type) == ERROR_MARK)
5825 return;
5827 /* Digest the specified initializer into an expression. */
5829 if (init)
5830 npc = null_pointer_constant_p (init);
5831 value = digest_init (init_loc, type, init, origtype, npc,
5832 true, TREE_STATIC (decl));
5834 /* Store the expression if valid; else report error. */
5836 if (!in_system_header
5837 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5838 warning (OPT_Wtraditional, "traditional C rejects automatic "
5839 "aggregate initialization");
5841 DECL_INITIAL (decl) = value;
5843 /* ANSI wants warnings about out-of-range constant initializers. */
5844 STRIP_TYPE_NOPS (value);
5845 if (TREE_STATIC (decl))
5846 constant_expression_warning (value);
5848 /* Check if we need to set array size from compound literal size. */
5849 if (TREE_CODE (type) == ARRAY_TYPE
5850 && TYPE_DOMAIN (type) == 0
5851 && value != error_mark_node)
5853 tree inside_init = init;
5855 STRIP_TYPE_NOPS (inside_init);
5856 inside_init = fold (inside_init);
5858 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5860 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5862 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5864 /* For int foo[] = (int [3]){1}; we need to set array size
5865 now since later on array initializer will be just the
5866 brace enclosed list of the compound literal. */
5867 tree etype = strip_array_types (TREE_TYPE (decl));
5868 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5869 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5870 layout_type (type);
5871 layout_decl (cldecl, 0);
5872 TREE_TYPE (decl)
5873 = c_build_qualified_type (type, TYPE_QUALS (etype));
5879 /* Methods for storing and printing names for error messages. */
5881 /* Implement a spelling stack that allows components of a name to be pushed
5882 and popped. Each element on the stack is this structure. */
5884 struct spelling
5886 int kind;
5887 union
5889 unsigned HOST_WIDE_INT i;
5890 const char *s;
5891 } u;
5894 #define SPELLING_STRING 1
5895 #define SPELLING_MEMBER 2
5896 #define SPELLING_BOUNDS 3
5898 static struct spelling *spelling; /* Next stack element (unused). */
5899 static struct spelling *spelling_base; /* Spelling stack base. */
5900 static int spelling_size; /* Size of the spelling stack. */
5902 /* Macros to save and restore the spelling stack around push_... functions.
5903 Alternative to SAVE_SPELLING_STACK. */
5905 #define SPELLING_DEPTH() (spelling - spelling_base)
5906 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5908 /* Push an element on the spelling stack with type KIND and assign VALUE
5909 to MEMBER. */
5911 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5913 int depth = SPELLING_DEPTH (); \
5915 if (depth >= spelling_size) \
5917 spelling_size += 10; \
5918 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5919 spelling_size); \
5920 RESTORE_SPELLING_DEPTH (depth); \
5923 spelling->kind = (KIND); \
5924 spelling->MEMBER = (VALUE); \
5925 spelling++; \
5928 /* Push STRING on the stack. Printed literally. */
5930 static void
5931 push_string (const char *string)
5933 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5936 /* Push a member name on the stack. Printed as '.' STRING. */
5938 static void
5939 push_member_name (tree decl)
5941 const char *const string
5942 = (DECL_NAME (decl)
5943 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5944 : _("<anonymous>"));
5945 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5948 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5950 static void
5951 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5953 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5956 /* Compute the maximum size in bytes of the printed spelling. */
5958 static int
5959 spelling_length (void)
5961 int size = 0;
5962 struct spelling *p;
5964 for (p = spelling_base; p < spelling; p++)
5966 if (p->kind == SPELLING_BOUNDS)
5967 size += 25;
5968 else
5969 size += strlen (p->u.s) + 1;
5972 return size;
5975 /* Print the spelling to BUFFER and return it. */
5977 static char *
5978 print_spelling (char *buffer)
5980 char *d = buffer;
5981 struct spelling *p;
5983 for (p = spelling_base; p < spelling; p++)
5984 if (p->kind == SPELLING_BOUNDS)
5986 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5987 d += strlen (d);
5989 else
5991 const char *s;
5992 if (p->kind == SPELLING_MEMBER)
5993 *d++ = '.';
5994 for (s = p->u.s; (*d = *s++); d++)
5997 *d++ = '\0';
5998 return buffer;
6001 /* Issue an error message for a bad initializer component.
6002 GMSGID identifies the message.
6003 The component name is taken from the spelling stack. */
6005 void
6006 error_init (const char *gmsgid)
6008 char *ofwhat;
6010 /* The gmsgid may be a format string with %< and %>. */
6011 error (gmsgid);
6012 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6013 if (*ofwhat)
6014 error ("(near initialization for %qs)", ofwhat);
6017 /* Issue a pedantic warning for a bad initializer component. OPT is
6018 the option OPT_* (from options.h) controlling this warning or 0 if
6019 it is unconditionally given. GMSGID identifies the message. The
6020 component name is taken from the spelling stack. */
6022 void
6023 pedwarn_init (location_t location, int opt, const char *gmsgid)
6025 char *ofwhat;
6027 /* The gmsgid may be a format string with %< and %>. */
6028 pedwarn (location, opt, gmsgid);
6029 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6030 if (*ofwhat)
6031 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6034 /* Issue a warning for a bad initializer component.
6036 OPT is the OPT_W* value corresponding to the warning option that
6037 controls this warning. GMSGID identifies the message. The
6038 component name is taken from the spelling stack. */
6040 static void
6041 warning_init (int opt, const char *gmsgid)
6043 char *ofwhat;
6045 /* The gmsgid may be a format string with %< and %>. */
6046 warning (opt, gmsgid);
6047 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6048 if (*ofwhat)
6049 warning (opt, "(near initialization for %qs)", ofwhat);
6052 /* If TYPE is an array type and EXPR is a parenthesized string
6053 constant, warn if pedantic that EXPR is being used to initialize an
6054 object of type TYPE. */
6056 void
6057 maybe_warn_string_init (tree type, struct c_expr expr)
6059 if (pedantic
6060 && TREE_CODE (type) == ARRAY_TYPE
6061 && TREE_CODE (expr.value) == STRING_CST
6062 && expr.original_code != STRING_CST)
6063 pedwarn_init (input_location, OPT_pedantic,
6064 "array initialized from parenthesized string constant");
6067 /* Digest the parser output INIT as an initializer for type TYPE.
6068 Return a C expression of type TYPE to represent the initial value.
6070 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6072 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6074 If INIT is a string constant, STRICT_STRING is true if it is
6075 unparenthesized or we should not warn here for it being parenthesized.
6076 For other types of INIT, STRICT_STRING is not used.
6078 INIT_LOC is the location of the INIT.
6080 REQUIRE_CONSTANT requests an error if non-constant initializers or
6081 elements are seen. */
6083 static tree
6084 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6085 bool null_pointer_constant, bool strict_string,
6086 int require_constant)
6088 enum tree_code code = TREE_CODE (type);
6089 tree inside_init = init;
6090 tree semantic_type = NULL_TREE;
6091 bool maybe_const = true;
6093 if (type == error_mark_node
6094 || !init
6095 || init == error_mark_node
6096 || TREE_TYPE (init) == error_mark_node)
6097 return error_mark_node;
6099 STRIP_TYPE_NOPS (inside_init);
6101 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6103 semantic_type = TREE_TYPE (inside_init);
6104 inside_init = TREE_OPERAND (inside_init, 0);
6106 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6107 inside_init = decl_constant_value_for_optimization (inside_init);
6109 /* Initialization of an array of chars from a string constant
6110 optionally enclosed in braces. */
6112 if (code == ARRAY_TYPE && inside_init
6113 && TREE_CODE (inside_init) == STRING_CST)
6115 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6116 /* Note that an array could be both an array of character type
6117 and an array of wchar_t if wchar_t is signed char or unsigned
6118 char. */
6119 bool char_array = (typ1 == char_type_node
6120 || typ1 == signed_char_type_node
6121 || typ1 == unsigned_char_type_node);
6122 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6123 bool char16_array = !!comptypes (typ1, char16_type_node);
6124 bool char32_array = !!comptypes (typ1, char32_type_node);
6126 if (char_array || wchar_array || char16_array || char32_array)
6128 struct c_expr expr;
6129 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6130 expr.value = inside_init;
6131 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6132 expr.original_type = NULL;
6133 maybe_warn_string_init (type, expr);
6135 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6136 pedwarn_init (init_loc, OPT_pedantic,
6137 "initialization of a flexible array member");
6139 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6140 TYPE_MAIN_VARIANT (type)))
6141 return inside_init;
6143 if (char_array)
6145 if (typ2 != char_type_node)
6147 error_init ("char-array initialized from wide string");
6148 return error_mark_node;
6151 else
6153 if (typ2 == char_type_node)
6155 error_init ("wide character array initialized from non-wide "
6156 "string");
6157 return error_mark_node;
6159 else if (!comptypes(typ1, typ2))
6161 error_init ("wide character array initialized from "
6162 "incompatible wide string");
6163 return error_mark_node;
6167 TREE_TYPE (inside_init) = type;
6168 if (TYPE_DOMAIN (type) != 0
6169 && TYPE_SIZE (type) != 0
6170 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6172 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6174 /* Subtract the size of a single (possibly wide) character
6175 because it's ok to ignore the terminating null char
6176 that is counted in the length of the constant. */
6177 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6178 (len
6179 - (TYPE_PRECISION (typ1)
6180 / BITS_PER_UNIT))))
6181 pedwarn_init (init_loc, 0,
6182 ("initializer-string for array of chars "
6183 "is too long"));
6184 else if (warn_cxx_compat
6185 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6186 warning_at (init_loc, OPT_Wc___compat,
6187 ("initializer-string for array chars "
6188 "is too long for C++"));
6191 return inside_init;
6193 else if (INTEGRAL_TYPE_P (typ1))
6195 error_init ("array of inappropriate type initialized "
6196 "from string constant");
6197 return error_mark_node;
6201 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6202 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6203 below and handle as a constructor. */
6204 if (code == VECTOR_TYPE
6205 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6206 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6207 && TREE_CONSTANT (inside_init))
6209 if (TREE_CODE (inside_init) == VECTOR_CST
6210 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6211 TYPE_MAIN_VARIANT (type)))
6212 return inside_init;
6214 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6216 unsigned HOST_WIDE_INT ix;
6217 tree value;
6218 bool constant_p = true;
6220 /* Iterate through elements and check if all constructor
6221 elements are *_CSTs. */
6222 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6223 if (!CONSTANT_CLASS_P (value))
6225 constant_p = false;
6226 break;
6229 if (constant_p)
6230 return build_vector_from_ctor (type,
6231 CONSTRUCTOR_ELTS (inside_init));
6235 if (warn_sequence_point)
6236 verify_sequence_points (inside_init);
6238 /* Any type can be initialized
6239 from an expression of the same type, optionally with braces. */
6241 if (inside_init && TREE_TYPE (inside_init) != 0
6242 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6243 TYPE_MAIN_VARIANT (type))
6244 || (code == ARRAY_TYPE
6245 && comptypes (TREE_TYPE (inside_init), type))
6246 || (code == VECTOR_TYPE
6247 && comptypes (TREE_TYPE (inside_init), type))
6248 || (code == POINTER_TYPE
6249 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6250 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6251 TREE_TYPE (type)))))
6253 if (code == POINTER_TYPE)
6255 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6257 if (TREE_CODE (inside_init) == STRING_CST
6258 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6259 inside_init = array_to_pointer_conversion
6260 (init_loc, inside_init);
6261 else
6263 error_init ("invalid use of non-lvalue array");
6264 return error_mark_node;
6269 if (code == VECTOR_TYPE)
6270 /* Although the types are compatible, we may require a
6271 conversion. */
6272 inside_init = convert (type, inside_init);
6274 if (require_constant
6275 && (code == VECTOR_TYPE || !flag_isoc99)
6276 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6278 /* As an extension, allow initializing objects with static storage
6279 duration with compound literals (which are then treated just as
6280 the brace enclosed list they contain). Also allow this for
6281 vectors, as we can only assign them with compound literals. */
6282 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6283 inside_init = DECL_INITIAL (decl);
6286 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6287 && TREE_CODE (inside_init) != CONSTRUCTOR)
6289 error_init ("array initialized from non-constant array expression");
6290 return error_mark_node;
6293 /* Compound expressions can only occur here if -pedantic or
6294 -pedantic-errors is specified. In the later case, we always want
6295 an error. In the former case, we simply want a warning. */
6296 if (require_constant && pedantic
6297 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6299 inside_init
6300 = valid_compound_expr_initializer (inside_init,
6301 TREE_TYPE (inside_init));
6302 if (inside_init == error_mark_node)
6303 error_init ("initializer element is not constant");
6304 else
6305 pedwarn_init (init_loc, OPT_pedantic,
6306 "initializer element is not constant");
6307 if (flag_pedantic_errors)
6308 inside_init = error_mark_node;
6310 else if (require_constant
6311 && !initializer_constant_valid_p (inside_init,
6312 TREE_TYPE (inside_init)))
6314 error_init ("initializer element is not constant");
6315 inside_init = error_mark_node;
6317 else if (require_constant && !maybe_const)
6318 pedwarn_init (init_loc, 0,
6319 "initializer element is not a constant expression");
6321 /* Added to enable additional -Wmissing-format-attribute warnings. */
6322 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6323 inside_init = convert_for_assignment (init_loc, type, inside_init,
6324 origtype,
6325 ic_init, null_pointer_constant,
6326 NULL_TREE, NULL_TREE, 0);
6327 return inside_init;
6330 /* Handle scalar types, including conversions. */
6332 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6333 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6334 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6336 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6337 && (TREE_CODE (init) == STRING_CST
6338 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6339 inside_init = init = array_to_pointer_conversion (init_loc, init);
6340 if (semantic_type)
6341 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6342 inside_init);
6343 inside_init
6344 = convert_for_assignment (init_loc, type, inside_init, origtype,
6345 ic_init, null_pointer_constant,
6346 NULL_TREE, NULL_TREE, 0);
6348 /* Check to see if we have already given an error message. */
6349 if (inside_init == error_mark_node)
6351 else if (require_constant && !TREE_CONSTANT (inside_init))
6353 error_init ("initializer element is not constant");
6354 inside_init = error_mark_node;
6356 else if (require_constant
6357 && !initializer_constant_valid_p (inside_init,
6358 TREE_TYPE (inside_init)))
6360 error_init ("initializer element is not computable at load time");
6361 inside_init = error_mark_node;
6363 else if (require_constant && !maybe_const)
6364 pedwarn_init (init_loc, 0,
6365 "initializer element is not a constant expression");
6367 return inside_init;
6370 /* Come here only for records and arrays. */
6372 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6374 error_init ("variable-sized object may not be initialized");
6375 return error_mark_node;
6378 error_init ("invalid initializer");
6379 return error_mark_node;
6382 /* Handle initializers that use braces. */
6384 /* Type of object we are accumulating a constructor for.
6385 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6386 static tree constructor_type;
6388 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6389 left to fill. */
6390 static tree constructor_fields;
6392 /* For an ARRAY_TYPE, this is the specified index
6393 at which to store the next element we get. */
6394 static tree constructor_index;
6396 /* For an ARRAY_TYPE, this is the maximum index. */
6397 static tree constructor_max_index;
6399 /* For a RECORD_TYPE, this is the first field not yet written out. */
6400 static tree constructor_unfilled_fields;
6402 /* For an ARRAY_TYPE, this is the index of the first element
6403 not yet written out. */
6404 static tree constructor_unfilled_index;
6406 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6407 This is so we can generate gaps between fields, when appropriate. */
6408 static tree constructor_bit_index;
6410 /* If we are saving up the elements rather than allocating them,
6411 this is the list of elements so far (in reverse order,
6412 most recent first). */
6413 static VEC(constructor_elt,gc) *constructor_elements;
6415 /* 1 if constructor should be incrementally stored into a constructor chain,
6416 0 if all the elements should be kept in AVL tree. */
6417 static int constructor_incremental;
6419 /* 1 if so far this constructor's elements are all compile-time constants. */
6420 static int constructor_constant;
6422 /* 1 if so far this constructor's elements are all valid address constants. */
6423 static int constructor_simple;
6425 /* 1 if this constructor has an element that cannot be part of a
6426 constant expression. */
6427 static int constructor_nonconst;
6429 /* 1 if this constructor is erroneous so far. */
6430 static int constructor_erroneous;
6432 /* Structure for managing pending initializer elements, organized as an
6433 AVL tree. */
6435 struct init_node
6437 struct init_node *left, *right;
6438 struct init_node *parent;
6439 int balance;
6440 tree purpose;
6441 tree value;
6442 tree origtype;
6445 /* Tree of pending elements at this constructor level.
6446 These are elements encountered out of order
6447 which belong at places we haven't reached yet in actually
6448 writing the output.
6449 Will never hold tree nodes across GC runs. */
6450 static struct init_node *constructor_pending_elts;
6452 /* The SPELLING_DEPTH of this constructor. */
6453 static int constructor_depth;
6455 /* DECL node for which an initializer is being read.
6456 0 means we are reading a constructor expression
6457 such as (struct foo) {...}. */
6458 static tree constructor_decl;
6460 /* Nonzero if this is an initializer for a top-level decl. */
6461 static int constructor_top_level;
6463 /* Nonzero if there were any member designators in this initializer. */
6464 static int constructor_designated;
6466 /* Nesting depth of designator list. */
6467 static int designator_depth;
6469 /* Nonzero if there were diagnosed errors in this designator list. */
6470 static int designator_erroneous;
6473 /* This stack has a level for each implicit or explicit level of
6474 structuring in the initializer, including the outermost one. It
6475 saves the values of most of the variables above. */
6477 struct constructor_range_stack;
6479 struct constructor_stack
6481 struct constructor_stack *next;
6482 tree type;
6483 tree fields;
6484 tree index;
6485 tree max_index;
6486 tree unfilled_index;
6487 tree unfilled_fields;
6488 tree bit_index;
6489 VEC(constructor_elt,gc) *elements;
6490 struct init_node *pending_elts;
6491 int offset;
6492 int depth;
6493 /* If value nonzero, this value should replace the entire
6494 constructor at this level. */
6495 struct c_expr replacement_value;
6496 struct constructor_range_stack *range_stack;
6497 char constant;
6498 char simple;
6499 char nonconst;
6500 char implicit;
6501 char erroneous;
6502 char outer;
6503 char incremental;
6504 char designated;
6507 static struct constructor_stack *constructor_stack;
6509 /* This stack represents designators from some range designator up to
6510 the last designator in the list. */
6512 struct constructor_range_stack
6514 struct constructor_range_stack *next, *prev;
6515 struct constructor_stack *stack;
6516 tree range_start;
6517 tree index;
6518 tree range_end;
6519 tree fields;
6522 static struct constructor_range_stack *constructor_range_stack;
6524 /* This stack records separate initializers that are nested.
6525 Nested initializers can't happen in ANSI C, but GNU C allows them
6526 in cases like { ... (struct foo) { ... } ... }. */
6528 struct initializer_stack
6530 struct initializer_stack *next;
6531 tree decl;
6532 struct constructor_stack *constructor_stack;
6533 struct constructor_range_stack *constructor_range_stack;
6534 VEC(constructor_elt,gc) *elements;
6535 struct spelling *spelling;
6536 struct spelling *spelling_base;
6537 int spelling_size;
6538 char top_level;
6539 char require_constant_value;
6540 char require_constant_elements;
6543 static struct initializer_stack *initializer_stack;
6545 /* Prepare to parse and output the initializer for variable DECL. */
6547 void
6548 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6550 const char *locus;
6551 struct initializer_stack *p = XNEW (struct initializer_stack);
6553 p->decl = constructor_decl;
6554 p->require_constant_value = require_constant_value;
6555 p->require_constant_elements = require_constant_elements;
6556 p->constructor_stack = constructor_stack;
6557 p->constructor_range_stack = constructor_range_stack;
6558 p->elements = constructor_elements;
6559 p->spelling = spelling;
6560 p->spelling_base = spelling_base;
6561 p->spelling_size = spelling_size;
6562 p->top_level = constructor_top_level;
6563 p->next = initializer_stack;
6564 initializer_stack = p;
6566 constructor_decl = decl;
6567 constructor_designated = 0;
6568 constructor_top_level = top_level;
6570 if (decl != 0 && decl != error_mark_node)
6572 require_constant_value = TREE_STATIC (decl);
6573 require_constant_elements
6574 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6575 /* For a scalar, you can always use any value to initialize,
6576 even within braces. */
6577 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6578 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6579 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6580 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6581 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6583 else
6585 require_constant_value = 0;
6586 require_constant_elements = 0;
6587 locus = _("(anonymous)");
6590 constructor_stack = 0;
6591 constructor_range_stack = 0;
6593 missing_braces_mentioned = 0;
6595 spelling_base = 0;
6596 spelling_size = 0;
6597 RESTORE_SPELLING_DEPTH (0);
6599 if (locus)
6600 push_string (locus);
6603 void
6604 finish_init (void)
6606 struct initializer_stack *p = initializer_stack;
6608 /* Free the whole constructor stack of this initializer. */
6609 while (constructor_stack)
6611 struct constructor_stack *q = constructor_stack;
6612 constructor_stack = q->next;
6613 free (q);
6616 gcc_assert (!constructor_range_stack);
6618 /* Pop back to the data of the outer initializer (if any). */
6619 free (spelling_base);
6621 constructor_decl = p->decl;
6622 require_constant_value = p->require_constant_value;
6623 require_constant_elements = p->require_constant_elements;
6624 constructor_stack = p->constructor_stack;
6625 constructor_range_stack = p->constructor_range_stack;
6626 constructor_elements = p->elements;
6627 spelling = p->spelling;
6628 spelling_base = p->spelling_base;
6629 spelling_size = p->spelling_size;
6630 constructor_top_level = p->top_level;
6631 initializer_stack = p->next;
6632 free (p);
6635 /* Call here when we see the initializer is surrounded by braces.
6636 This is instead of a call to push_init_level;
6637 it is matched by a call to pop_init_level.
6639 TYPE is the type to initialize, for a constructor expression.
6640 For an initializer for a decl, TYPE is zero. */
6642 void
6643 really_start_incremental_init (tree type)
6645 struct constructor_stack *p = XNEW (struct constructor_stack);
6647 if (type == 0)
6648 type = TREE_TYPE (constructor_decl);
6650 if (TREE_CODE (type) == VECTOR_TYPE
6651 && TYPE_VECTOR_OPAQUE (type))
6652 error ("opaque vector types cannot be initialized");
6654 p->type = constructor_type;
6655 p->fields = constructor_fields;
6656 p->index = constructor_index;
6657 p->max_index = constructor_max_index;
6658 p->unfilled_index = constructor_unfilled_index;
6659 p->unfilled_fields = constructor_unfilled_fields;
6660 p->bit_index = constructor_bit_index;
6661 p->elements = constructor_elements;
6662 p->constant = constructor_constant;
6663 p->simple = constructor_simple;
6664 p->nonconst = constructor_nonconst;
6665 p->erroneous = constructor_erroneous;
6666 p->pending_elts = constructor_pending_elts;
6667 p->depth = constructor_depth;
6668 p->replacement_value.value = 0;
6669 p->replacement_value.original_code = ERROR_MARK;
6670 p->replacement_value.original_type = NULL;
6671 p->implicit = 0;
6672 p->range_stack = 0;
6673 p->outer = 0;
6674 p->incremental = constructor_incremental;
6675 p->designated = constructor_designated;
6676 p->next = 0;
6677 constructor_stack = p;
6679 constructor_constant = 1;
6680 constructor_simple = 1;
6681 constructor_nonconst = 0;
6682 constructor_depth = SPELLING_DEPTH ();
6683 constructor_elements = 0;
6684 constructor_pending_elts = 0;
6685 constructor_type = type;
6686 constructor_incremental = 1;
6687 constructor_designated = 0;
6688 designator_depth = 0;
6689 designator_erroneous = 0;
6691 if (TREE_CODE (constructor_type) == RECORD_TYPE
6692 || TREE_CODE (constructor_type) == UNION_TYPE)
6694 constructor_fields = TYPE_FIELDS (constructor_type);
6695 /* Skip any nameless bit fields at the beginning. */
6696 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6697 && DECL_NAME (constructor_fields) == 0)
6698 constructor_fields = DECL_CHAIN (constructor_fields);
6700 constructor_unfilled_fields = constructor_fields;
6701 constructor_bit_index = bitsize_zero_node;
6703 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6705 if (TYPE_DOMAIN (constructor_type))
6707 constructor_max_index
6708 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6710 /* Detect non-empty initializations of zero-length arrays. */
6711 if (constructor_max_index == NULL_TREE
6712 && TYPE_SIZE (constructor_type))
6713 constructor_max_index = integer_minus_one_node;
6715 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6716 to initialize VLAs will cause a proper error; avoid tree
6717 checking errors as well by setting a safe value. */
6718 if (constructor_max_index
6719 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6720 constructor_max_index = integer_minus_one_node;
6722 constructor_index
6723 = convert (bitsizetype,
6724 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6726 else
6728 constructor_index = bitsize_zero_node;
6729 constructor_max_index = NULL_TREE;
6732 constructor_unfilled_index = constructor_index;
6734 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6736 /* Vectors are like simple fixed-size arrays. */
6737 constructor_max_index =
6738 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6739 constructor_index = bitsize_zero_node;
6740 constructor_unfilled_index = constructor_index;
6742 else
6744 /* Handle the case of int x = {5}; */
6745 constructor_fields = constructor_type;
6746 constructor_unfilled_fields = constructor_type;
6750 /* Push down into a subobject, for initialization.
6751 If this is for an explicit set of braces, IMPLICIT is 0.
6752 If it is because the next element belongs at a lower level,
6753 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6755 void
6756 push_init_level (int implicit, struct obstack * braced_init_obstack)
6758 struct constructor_stack *p;
6759 tree value = NULL_TREE;
6761 /* If we've exhausted any levels that didn't have braces,
6762 pop them now. If implicit == 1, this will have been done in
6763 process_init_element; do not repeat it here because in the case
6764 of excess initializers for an empty aggregate this leads to an
6765 infinite cycle of popping a level and immediately recreating
6766 it. */
6767 if (implicit != 1)
6769 while (constructor_stack->implicit)
6771 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6772 || TREE_CODE (constructor_type) == UNION_TYPE)
6773 && constructor_fields == 0)
6774 process_init_element (pop_init_level (1, braced_init_obstack),
6775 true, braced_init_obstack);
6776 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6777 && constructor_max_index
6778 && tree_int_cst_lt (constructor_max_index,
6779 constructor_index))
6780 process_init_element (pop_init_level (1, braced_init_obstack),
6781 true, braced_init_obstack);
6782 else
6783 break;
6787 /* Unless this is an explicit brace, we need to preserve previous
6788 content if any. */
6789 if (implicit)
6791 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6792 || TREE_CODE (constructor_type) == UNION_TYPE)
6793 && constructor_fields)
6794 value = find_init_member (constructor_fields, braced_init_obstack);
6795 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6796 value = find_init_member (constructor_index, braced_init_obstack);
6799 p = XNEW (struct constructor_stack);
6800 p->type = constructor_type;
6801 p->fields = constructor_fields;
6802 p->index = constructor_index;
6803 p->max_index = constructor_max_index;
6804 p->unfilled_index = constructor_unfilled_index;
6805 p->unfilled_fields = constructor_unfilled_fields;
6806 p->bit_index = constructor_bit_index;
6807 p->elements = constructor_elements;
6808 p->constant = constructor_constant;
6809 p->simple = constructor_simple;
6810 p->nonconst = constructor_nonconst;
6811 p->erroneous = constructor_erroneous;
6812 p->pending_elts = constructor_pending_elts;
6813 p->depth = constructor_depth;
6814 p->replacement_value.value = 0;
6815 p->replacement_value.original_code = ERROR_MARK;
6816 p->replacement_value.original_type = NULL;
6817 p->implicit = implicit;
6818 p->outer = 0;
6819 p->incremental = constructor_incremental;
6820 p->designated = constructor_designated;
6821 p->next = constructor_stack;
6822 p->range_stack = 0;
6823 constructor_stack = p;
6825 constructor_constant = 1;
6826 constructor_simple = 1;
6827 constructor_nonconst = 0;
6828 constructor_depth = SPELLING_DEPTH ();
6829 constructor_elements = 0;
6830 constructor_incremental = 1;
6831 constructor_designated = 0;
6832 constructor_pending_elts = 0;
6833 if (!implicit)
6835 p->range_stack = constructor_range_stack;
6836 constructor_range_stack = 0;
6837 designator_depth = 0;
6838 designator_erroneous = 0;
6841 /* Don't die if an entire brace-pair level is superfluous
6842 in the containing level. */
6843 if (constructor_type == 0)
6845 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6846 || TREE_CODE (constructor_type) == UNION_TYPE)
6848 /* Don't die if there are extra init elts at the end. */
6849 if (constructor_fields == 0)
6850 constructor_type = 0;
6851 else
6853 constructor_type = TREE_TYPE (constructor_fields);
6854 push_member_name (constructor_fields);
6855 constructor_depth++;
6858 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6860 constructor_type = TREE_TYPE (constructor_type);
6861 push_array_bounds (tree_low_cst (constructor_index, 1));
6862 constructor_depth++;
6865 if (constructor_type == 0)
6867 error_init ("extra brace group at end of initializer");
6868 constructor_fields = 0;
6869 constructor_unfilled_fields = 0;
6870 return;
6873 if (value && TREE_CODE (value) == CONSTRUCTOR)
6875 constructor_constant = TREE_CONSTANT (value);
6876 constructor_simple = TREE_STATIC (value);
6877 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6878 constructor_elements = CONSTRUCTOR_ELTS (value);
6879 if (!VEC_empty (constructor_elt, constructor_elements)
6880 && (TREE_CODE (constructor_type) == RECORD_TYPE
6881 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6882 set_nonincremental_init (braced_init_obstack);
6885 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6887 missing_braces_mentioned = 1;
6888 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6891 if (TREE_CODE (constructor_type) == RECORD_TYPE
6892 || TREE_CODE (constructor_type) == UNION_TYPE)
6894 constructor_fields = TYPE_FIELDS (constructor_type);
6895 /* Skip any nameless bit fields at the beginning. */
6896 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6897 && DECL_NAME (constructor_fields) == 0)
6898 constructor_fields = DECL_CHAIN (constructor_fields);
6900 constructor_unfilled_fields = constructor_fields;
6901 constructor_bit_index = bitsize_zero_node;
6903 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6905 /* Vectors are like simple fixed-size arrays. */
6906 constructor_max_index =
6907 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6908 constructor_index = bitsize_int (0);
6909 constructor_unfilled_index = constructor_index;
6911 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6913 if (TYPE_DOMAIN (constructor_type))
6915 constructor_max_index
6916 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6918 /* Detect non-empty initializations of zero-length arrays. */
6919 if (constructor_max_index == NULL_TREE
6920 && TYPE_SIZE (constructor_type))
6921 constructor_max_index = integer_minus_one_node;
6923 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6924 to initialize VLAs will cause a proper error; avoid tree
6925 checking errors as well by setting a safe value. */
6926 if (constructor_max_index
6927 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6928 constructor_max_index = integer_minus_one_node;
6930 constructor_index
6931 = convert (bitsizetype,
6932 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6934 else
6935 constructor_index = bitsize_zero_node;
6937 constructor_unfilled_index = constructor_index;
6938 if (value && TREE_CODE (value) == STRING_CST)
6940 /* We need to split the char/wchar array into individual
6941 characters, so that we don't have to special case it
6942 everywhere. */
6943 set_nonincremental_init_from_string (value, braced_init_obstack);
6946 else
6948 if (constructor_type != error_mark_node)
6949 warning_init (0, "braces around scalar initializer");
6950 constructor_fields = constructor_type;
6951 constructor_unfilled_fields = constructor_type;
6955 /* At the end of an implicit or explicit brace level,
6956 finish up that level of constructor. If a single expression
6957 with redundant braces initialized that level, return the
6958 c_expr structure for that expression. Otherwise, the original_code
6959 element is set to ERROR_MARK.
6960 If we were outputting the elements as they are read, return 0 as the value
6961 from inner levels (process_init_element ignores that),
6962 but return error_mark_node as the value from the outermost level
6963 (that's what we want to put in DECL_INITIAL).
6964 Otherwise, return a CONSTRUCTOR expression as the value. */
6966 struct c_expr
6967 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6969 struct constructor_stack *p;
6970 struct c_expr ret;
6971 ret.value = 0;
6972 ret.original_code = ERROR_MARK;
6973 ret.original_type = NULL;
6975 if (implicit == 0)
6977 /* When we come to an explicit close brace,
6978 pop any inner levels that didn't have explicit braces. */
6979 while (constructor_stack->implicit)
6981 process_init_element (pop_init_level (1, braced_init_obstack),
6982 true, braced_init_obstack);
6984 gcc_assert (!constructor_range_stack);
6987 /* Now output all pending elements. */
6988 constructor_incremental = 1;
6989 output_pending_init_elements (1, braced_init_obstack);
6991 p = constructor_stack;
6993 /* Error for initializing a flexible array member, or a zero-length
6994 array member in an inappropriate context. */
6995 if (constructor_type && constructor_fields
6996 && TREE_CODE (constructor_type) == ARRAY_TYPE
6997 && TYPE_DOMAIN (constructor_type)
6998 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7000 /* Silently discard empty initializations. The parser will
7001 already have pedwarned for empty brackets. */
7002 if (integer_zerop (constructor_unfilled_index))
7003 constructor_type = NULL_TREE;
7004 else
7006 gcc_assert (!TYPE_SIZE (constructor_type));
7008 if (constructor_depth > 2)
7009 error_init ("initialization of flexible array member in a nested context");
7010 else
7011 pedwarn_init (input_location, OPT_pedantic,
7012 "initialization of a flexible array member");
7014 /* We have already issued an error message for the existence
7015 of a flexible array member not at the end of the structure.
7016 Discard the initializer so that we do not die later. */
7017 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7018 constructor_type = NULL_TREE;
7022 /* Warn when some struct elements are implicitly initialized to zero. */
7023 if (warn_missing_field_initializers
7024 && constructor_type
7025 && TREE_CODE (constructor_type) == RECORD_TYPE
7026 && constructor_unfilled_fields)
7028 bool constructor_zeroinit =
7029 (VEC_length (constructor_elt, constructor_elements) == 1
7030 && integer_zerop
7031 (VEC_index (constructor_elt, constructor_elements, 0)->value));
7033 /* Do not warn for flexible array members or zero-length arrays. */
7034 while (constructor_unfilled_fields
7035 && (!DECL_SIZE (constructor_unfilled_fields)
7036 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7037 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7039 if (constructor_unfilled_fields
7040 /* Do not warn if this level of the initializer uses member
7041 designators; it is likely to be deliberate. */
7042 && !constructor_designated
7043 /* Do not warn about initializing with ` = {0}'. */
7044 && !constructor_zeroinit)
7046 push_member_name (constructor_unfilled_fields);
7047 warning_init (OPT_Wmissing_field_initializers,
7048 "missing initializer");
7049 RESTORE_SPELLING_DEPTH (constructor_depth);
7053 /* Pad out the end of the structure. */
7054 if (p->replacement_value.value)
7055 /* If this closes a superfluous brace pair,
7056 just pass out the element between them. */
7057 ret = p->replacement_value;
7058 else if (constructor_type == 0)
7060 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7061 && TREE_CODE (constructor_type) != UNION_TYPE
7062 && TREE_CODE (constructor_type) != ARRAY_TYPE
7063 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7065 /* A nonincremental scalar initializer--just return
7066 the element, after verifying there is just one. */
7067 if (VEC_empty (constructor_elt,constructor_elements))
7069 if (!constructor_erroneous)
7070 error_init ("empty scalar initializer");
7071 ret.value = error_mark_node;
7073 else if (VEC_length (constructor_elt,constructor_elements) != 1)
7075 error_init ("extra elements in scalar initializer");
7076 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7078 else
7079 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7081 else
7083 if (constructor_erroneous)
7084 ret.value = error_mark_node;
7085 else
7087 ret.value = build_constructor (constructor_type,
7088 constructor_elements);
7089 if (constructor_constant)
7090 TREE_CONSTANT (ret.value) = 1;
7091 if (constructor_constant && constructor_simple)
7092 TREE_STATIC (ret.value) = 1;
7093 if (constructor_nonconst)
7094 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7098 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7100 if (constructor_nonconst)
7101 ret.original_code = C_MAYBE_CONST_EXPR;
7102 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7103 ret.original_code = ERROR_MARK;
7106 constructor_type = p->type;
7107 constructor_fields = p->fields;
7108 constructor_index = p->index;
7109 constructor_max_index = p->max_index;
7110 constructor_unfilled_index = p->unfilled_index;
7111 constructor_unfilled_fields = p->unfilled_fields;
7112 constructor_bit_index = p->bit_index;
7113 constructor_elements = p->elements;
7114 constructor_constant = p->constant;
7115 constructor_simple = p->simple;
7116 constructor_nonconst = p->nonconst;
7117 constructor_erroneous = p->erroneous;
7118 constructor_incremental = p->incremental;
7119 constructor_designated = p->designated;
7120 constructor_pending_elts = p->pending_elts;
7121 constructor_depth = p->depth;
7122 if (!p->implicit)
7123 constructor_range_stack = p->range_stack;
7124 RESTORE_SPELLING_DEPTH (constructor_depth);
7126 constructor_stack = p->next;
7127 free (p);
7129 if (ret.value == 0 && constructor_stack == 0)
7130 ret.value = error_mark_node;
7131 return ret;
7134 /* Common handling for both array range and field name designators.
7135 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7137 static int
7138 set_designator (int array, struct obstack * braced_init_obstack)
7140 tree subtype;
7141 enum tree_code subcode;
7143 /* Don't die if an entire brace-pair level is superfluous
7144 in the containing level. */
7145 if (constructor_type == 0)
7146 return 1;
7148 /* If there were errors in this designator list already, bail out
7149 silently. */
7150 if (designator_erroneous)
7151 return 1;
7153 if (!designator_depth)
7155 gcc_assert (!constructor_range_stack);
7157 /* Designator list starts at the level of closest explicit
7158 braces. */
7159 while (constructor_stack->implicit)
7161 process_init_element (pop_init_level (1, braced_init_obstack),
7162 true, braced_init_obstack);
7164 constructor_designated = 1;
7165 return 0;
7168 switch (TREE_CODE (constructor_type))
7170 case RECORD_TYPE:
7171 case UNION_TYPE:
7172 subtype = TREE_TYPE (constructor_fields);
7173 if (subtype != error_mark_node)
7174 subtype = TYPE_MAIN_VARIANT (subtype);
7175 break;
7176 case ARRAY_TYPE:
7177 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7178 break;
7179 default:
7180 gcc_unreachable ();
7183 subcode = TREE_CODE (subtype);
7184 if (array && subcode != ARRAY_TYPE)
7186 error_init ("array index in non-array initializer");
7187 return 1;
7189 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7191 error_init ("field name not in record or union initializer");
7192 return 1;
7195 constructor_designated = 1;
7196 push_init_level (2, braced_init_obstack);
7197 return 0;
7200 /* If there are range designators in designator list, push a new designator
7201 to constructor_range_stack. RANGE_END is end of such stack range or
7202 NULL_TREE if there is no range designator at this level. */
7204 static void
7205 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7207 struct constructor_range_stack *p;
7209 p = (struct constructor_range_stack *)
7210 obstack_alloc (braced_init_obstack,
7211 sizeof (struct constructor_range_stack));
7212 p->prev = constructor_range_stack;
7213 p->next = 0;
7214 p->fields = constructor_fields;
7215 p->range_start = constructor_index;
7216 p->index = constructor_index;
7217 p->stack = constructor_stack;
7218 p->range_end = range_end;
7219 if (constructor_range_stack)
7220 constructor_range_stack->next = p;
7221 constructor_range_stack = p;
7224 /* Within an array initializer, specify the next index to be initialized.
7225 FIRST is that index. If LAST is nonzero, then initialize a range
7226 of indices, running from FIRST through LAST. */
7228 void
7229 set_init_index (tree first, tree last,
7230 struct obstack * braced_init_obstack)
7232 if (set_designator (1, braced_init_obstack))
7233 return;
7235 designator_erroneous = 1;
7237 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7238 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7240 error_init ("array index in initializer not of integer type");
7241 return;
7244 if (TREE_CODE (first) != INTEGER_CST)
7246 first = c_fully_fold (first, false, NULL);
7247 if (TREE_CODE (first) == INTEGER_CST)
7248 pedwarn_init (input_location, OPT_pedantic,
7249 "array index in initializer is not "
7250 "an integer constant expression");
7253 if (last && TREE_CODE (last) != INTEGER_CST)
7255 last = c_fully_fold (last, false, NULL);
7256 if (TREE_CODE (last) == INTEGER_CST)
7257 pedwarn_init (input_location, OPT_pedantic,
7258 "array index in initializer is not "
7259 "an integer constant expression");
7262 if (TREE_CODE (first) != INTEGER_CST)
7263 error_init ("nonconstant array index in initializer");
7264 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7265 error_init ("nonconstant array index in initializer");
7266 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7267 error_init ("array index in non-array initializer");
7268 else if (tree_int_cst_sgn (first) == -1)
7269 error_init ("array index in initializer exceeds array bounds");
7270 else if (constructor_max_index
7271 && tree_int_cst_lt (constructor_max_index, first))
7272 error_init ("array index in initializer exceeds array bounds");
7273 else
7275 constant_expression_warning (first);
7276 if (last)
7277 constant_expression_warning (last);
7278 constructor_index = convert (bitsizetype, first);
7280 if (last)
7282 if (tree_int_cst_equal (first, last))
7283 last = 0;
7284 else if (tree_int_cst_lt (last, first))
7286 error_init ("empty index range in initializer");
7287 last = 0;
7289 else
7291 last = convert (bitsizetype, last);
7292 if (constructor_max_index != 0
7293 && tree_int_cst_lt (constructor_max_index, last))
7295 error_init ("array index range in initializer exceeds array bounds");
7296 last = 0;
7301 designator_depth++;
7302 designator_erroneous = 0;
7303 if (constructor_range_stack || last)
7304 push_range_stack (last, braced_init_obstack);
7308 /* Within a struct initializer, specify the next field to be initialized. */
7310 void
7311 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7313 tree field;
7315 if (set_designator (0, braced_init_obstack))
7316 return;
7318 designator_erroneous = 1;
7320 if (TREE_CODE (constructor_type) != RECORD_TYPE
7321 && TREE_CODE (constructor_type) != UNION_TYPE)
7323 error_init ("field name not in record or union initializer");
7324 return;
7327 field = lookup_field (constructor_type, fieldname);
7329 if (field == 0)
7330 error ("unknown field %qE specified in initializer", fieldname);
7331 else
7334 constructor_fields = TREE_VALUE (field);
7335 designator_depth++;
7336 designator_erroneous = 0;
7337 if (constructor_range_stack)
7338 push_range_stack (NULL_TREE, braced_init_obstack);
7339 field = TREE_CHAIN (field);
7340 if (field)
7342 if (set_designator (0, braced_init_obstack))
7343 return;
7346 while (field != NULL_TREE);
7349 /* Add a new initializer to the tree of pending initializers. PURPOSE
7350 identifies the initializer, either array index or field in a structure.
7351 VALUE is the value of that index or field. If ORIGTYPE is not
7352 NULL_TREE, it is the original type of VALUE.
7354 IMPLICIT is true if value comes from pop_init_level (1),
7355 the new initializer has been merged with the existing one
7356 and thus no warnings should be emitted about overriding an
7357 existing initializer. */
7359 static void
7360 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7361 struct obstack * braced_init_obstack)
7363 struct init_node *p, **q, *r;
7365 q = &constructor_pending_elts;
7366 p = 0;
7368 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7370 while (*q != 0)
7372 p = *q;
7373 if (tree_int_cst_lt (purpose, p->purpose))
7374 q = &p->left;
7375 else if (tree_int_cst_lt (p->purpose, purpose))
7376 q = &p->right;
7377 else
7379 if (!implicit)
7381 if (TREE_SIDE_EFFECTS (p->value))
7382 warning_init (0, "initialized field with side-effects overwritten");
7383 else if (warn_override_init)
7384 warning_init (OPT_Woverride_init, "initialized field overwritten");
7386 p->value = value;
7387 p->origtype = origtype;
7388 return;
7392 else
7394 tree bitpos;
7396 bitpos = bit_position (purpose);
7397 while (*q != NULL)
7399 p = *q;
7400 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7401 q = &p->left;
7402 else if (p->purpose != purpose)
7403 q = &p->right;
7404 else
7406 if (!implicit)
7408 if (TREE_SIDE_EFFECTS (p->value))
7409 warning_init (0, "initialized field with side-effects overwritten");
7410 else if (warn_override_init)
7411 warning_init (OPT_Woverride_init, "initialized field overwritten");
7413 p->value = value;
7414 p->origtype = origtype;
7415 return;
7420 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7421 sizeof (struct init_node));
7422 r->purpose = purpose;
7423 r->value = value;
7424 r->origtype = origtype;
7426 *q = r;
7427 r->parent = p;
7428 r->left = 0;
7429 r->right = 0;
7430 r->balance = 0;
7432 while (p)
7434 struct init_node *s;
7436 if (r == p->left)
7438 if (p->balance == 0)
7439 p->balance = -1;
7440 else if (p->balance < 0)
7442 if (r->balance < 0)
7444 /* L rotation. */
7445 p->left = r->right;
7446 if (p->left)
7447 p->left->parent = p;
7448 r->right = p;
7450 p->balance = 0;
7451 r->balance = 0;
7453 s = p->parent;
7454 p->parent = r;
7455 r->parent = s;
7456 if (s)
7458 if (s->left == p)
7459 s->left = r;
7460 else
7461 s->right = r;
7463 else
7464 constructor_pending_elts = r;
7466 else
7468 /* LR rotation. */
7469 struct init_node *t = r->right;
7471 r->right = t->left;
7472 if (r->right)
7473 r->right->parent = r;
7474 t->left = r;
7476 p->left = t->right;
7477 if (p->left)
7478 p->left->parent = p;
7479 t->right = p;
7481 p->balance = t->balance < 0;
7482 r->balance = -(t->balance > 0);
7483 t->balance = 0;
7485 s = p->parent;
7486 p->parent = t;
7487 r->parent = t;
7488 t->parent = s;
7489 if (s)
7491 if (s->left == p)
7492 s->left = t;
7493 else
7494 s->right = t;
7496 else
7497 constructor_pending_elts = t;
7499 break;
7501 else
7503 /* p->balance == +1; growth of left side balances the node. */
7504 p->balance = 0;
7505 break;
7508 else /* r == p->right */
7510 if (p->balance == 0)
7511 /* Growth propagation from right side. */
7512 p->balance++;
7513 else if (p->balance > 0)
7515 if (r->balance > 0)
7517 /* R rotation. */
7518 p->right = r->left;
7519 if (p->right)
7520 p->right->parent = p;
7521 r->left = p;
7523 p->balance = 0;
7524 r->balance = 0;
7526 s = p->parent;
7527 p->parent = r;
7528 r->parent = s;
7529 if (s)
7531 if (s->left == p)
7532 s->left = r;
7533 else
7534 s->right = r;
7536 else
7537 constructor_pending_elts = r;
7539 else /* r->balance == -1 */
7541 /* RL rotation */
7542 struct init_node *t = r->left;
7544 r->left = t->right;
7545 if (r->left)
7546 r->left->parent = r;
7547 t->right = r;
7549 p->right = t->left;
7550 if (p->right)
7551 p->right->parent = p;
7552 t->left = p;
7554 r->balance = (t->balance < 0);
7555 p->balance = -(t->balance > 0);
7556 t->balance = 0;
7558 s = p->parent;
7559 p->parent = t;
7560 r->parent = t;
7561 t->parent = s;
7562 if (s)
7564 if (s->left == p)
7565 s->left = t;
7566 else
7567 s->right = t;
7569 else
7570 constructor_pending_elts = t;
7572 break;
7574 else
7576 /* p->balance == -1; growth of right side balances the node. */
7577 p->balance = 0;
7578 break;
7582 r = p;
7583 p = p->parent;
7587 /* Build AVL tree from a sorted chain. */
7589 static void
7590 set_nonincremental_init (struct obstack * braced_init_obstack)
7592 unsigned HOST_WIDE_INT ix;
7593 tree index, value;
7595 if (TREE_CODE (constructor_type) != RECORD_TYPE
7596 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7597 return;
7599 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7601 add_pending_init (index, value, NULL_TREE, false,
7602 braced_init_obstack);
7604 constructor_elements = 0;
7605 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7607 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7608 /* Skip any nameless bit fields at the beginning. */
7609 while (constructor_unfilled_fields != 0
7610 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7611 && DECL_NAME (constructor_unfilled_fields) == 0)
7612 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7615 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7617 if (TYPE_DOMAIN (constructor_type))
7618 constructor_unfilled_index
7619 = convert (bitsizetype,
7620 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7621 else
7622 constructor_unfilled_index = bitsize_zero_node;
7624 constructor_incremental = 0;
7627 /* Build AVL tree from a string constant. */
7629 static void
7630 set_nonincremental_init_from_string (tree str,
7631 struct obstack * braced_init_obstack)
7633 tree value, purpose, type;
7634 HOST_WIDE_INT val[2];
7635 const char *p, *end;
7636 int byte, wchar_bytes, charwidth, bitpos;
7638 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7640 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7641 charwidth = TYPE_PRECISION (char_type_node);
7642 type = TREE_TYPE (constructor_type);
7643 p = TREE_STRING_POINTER (str);
7644 end = p + TREE_STRING_LENGTH (str);
7646 for (purpose = bitsize_zero_node;
7647 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7648 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7650 if (wchar_bytes == 1)
7652 val[1] = (unsigned char) *p++;
7653 val[0] = 0;
7655 else
7657 val[0] = 0;
7658 val[1] = 0;
7659 for (byte = 0; byte < wchar_bytes; byte++)
7661 if (BYTES_BIG_ENDIAN)
7662 bitpos = (wchar_bytes - byte - 1) * charwidth;
7663 else
7664 bitpos = byte * charwidth;
7665 val[bitpos < HOST_BITS_PER_WIDE_INT]
7666 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7667 << (bitpos % HOST_BITS_PER_WIDE_INT);
7671 if (!TYPE_UNSIGNED (type))
7673 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7674 if (bitpos < HOST_BITS_PER_WIDE_INT)
7676 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7678 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7679 val[0] = -1;
7682 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7684 if (val[1] < 0)
7685 val[0] = -1;
7687 else if (val[0] & (((HOST_WIDE_INT) 1)
7688 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7689 val[0] |= ((HOST_WIDE_INT) -1)
7690 << (bitpos - HOST_BITS_PER_WIDE_INT);
7693 value = build_int_cst_wide (type, val[1], val[0]);
7694 add_pending_init (purpose, value, NULL_TREE, false,
7695 braced_init_obstack);
7698 constructor_incremental = 0;
7701 /* Return value of FIELD in pending initializer or zero if the field was
7702 not initialized yet. */
7704 static tree
7705 find_init_member (tree field, struct obstack * braced_init_obstack)
7707 struct init_node *p;
7709 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7711 if (constructor_incremental
7712 && tree_int_cst_lt (field, constructor_unfilled_index))
7713 set_nonincremental_init (braced_init_obstack);
7715 p = constructor_pending_elts;
7716 while (p)
7718 if (tree_int_cst_lt (field, p->purpose))
7719 p = p->left;
7720 else if (tree_int_cst_lt (p->purpose, field))
7721 p = p->right;
7722 else
7723 return p->value;
7726 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7728 tree bitpos = bit_position (field);
7730 if (constructor_incremental
7731 && (!constructor_unfilled_fields
7732 || tree_int_cst_lt (bitpos,
7733 bit_position (constructor_unfilled_fields))))
7734 set_nonincremental_init (braced_init_obstack);
7736 p = constructor_pending_elts;
7737 while (p)
7739 if (field == p->purpose)
7740 return p->value;
7741 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7742 p = p->left;
7743 else
7744 p = p->right;
7747 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7749 if (!VEC_empty (constructor_elt, constructor_elements)
7750 && (VEC_last (constructor_elt, constructor_elements)->index
7751 == field))
7752 return VEC_last (constructor_elt, constructor_elements)->value;
7754 return 0;
7757 /* "Output" the next constructor element.
7758 At top level, really output it to assembler code now.
7759 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7760 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7761 TYPE is the data type that the containing data type wants here.
7762 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7763 If VALUE is a string constant, STRICT_STRING is true if it is
7764 unparenthesized or we should not warn here for it being parenthesized.
7765 For other types of VALUE, STRICT_STRING is not used.
7767 PENDING if non-nil means output pending elements that belong
7768 right after this element. (PENDING is normally 1;
7769 it is 0 while outputting pending elements, to avoid recursion.)
7771 IMPLICIT is true if value comes from pop_init_level (1),
7772 the new initializer has been merged with the existing one
7773 and thus no warnings should be emitted about overriding an
7774 existing initializer. */
7776 static void
7777 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7778 tree field, int pending, bool implicit,
7779 struct obstack * braced_init_obstack)
7781 tree semantic_type = NULL_TREE;
7782 constructor_elt *celt;
7783 bool maybe_const = true;
7784 bool npc;
7786 if (type == error_mark_node || value == error_mark_node)
7788 constructor_erroneous = 1;
7789 return;
7791 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7792 && (TREE_CODE (value) == STRING_CST
7793 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7794 && !(TREE_CODE (value) == STRING_CST
7795 && TREE_CODE (type) == ARRAY_TYPE
7796 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7797 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7798 TYPE_MAIN_VARIANT (type)))
7799 value = array_to_pointer_conversion (input_location, value);
7801 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7802 && require_constant_value && !flag_isoc99 && pending)
7804 /* As an extension, allow initializing objects with static storage
7805 duration with compound literals (which are then treated just as
7806 the brace enclosed list they contain). */
7807 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7808 value = DECL_INITIAL (decl);
7811 npc = null_pointer_constant_p (value);
7812 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7814 semantic_type = TREE_TYPE (value);
7815 value = TREE_OPERAND (value, 0);
7817 value = c_fully_fold (value, require_constant_value, &maybe_const);
7819 if (value == error_mark_node)
7820 constructor_erroneous = 1;
7821 else if (!TREE_CONSTANT (value))
7822 constructor_constant = 0;
7823 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7824 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7825 || TREE_CODE (constructor_type) == UNION_TYPE)
7826 && DECL_C_BIT_FIELD (field)
7827 && TREE_CODE (value) != INTEGER_CST))
7828 constructor_simple = 0;
7829 if (!maybe_const)
7830 constructor_nonconst = 1;
7832 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7834 if (require_constant_value)
7836 error_init ("initializer element is not constant");
7837 value = error_mark_node;
7839 else if (require_constant_elements)
7840 pedwarn (input_location, 0,
7841 "initializer element is not computable at load time");
7843 else if (!maybe_const
7844 && (require_constant_value || require_constant_elements))
7845 pedwarn_init (input_location, 0,
7846 "initializer element is not a constant expression");
7848 /* Issue -Wc++-compat warnings about initializing a bitfield with
7849 enum type. */
7850 if (warn_cxx_compat
7851 && field != NULL_TREE
7852 && TREE_CODE (field) == FIELD_DECL
7853 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7854 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7855 != TYPE_MAIN_VARIANT (type))
7856 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7858 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7859 if (checktype != error_mark_node
7860 && (TYPE_MAIN_VARIANT (checktype)
7861 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7862 warning_init (OPT_Wc___compat,
7863 "enum conversion in initialization is invalid in C++");
7866 /* If this field is empty (and not at the end of structure),
7867 don't do anything other than checking the initializer. */
7868 if (field
7869 && (TREE_TYPE (field) == error_mark_node
7870 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7871 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7872 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7873 || DECL_CHAIN (field)))))
7874 return;
7876 if (semantic_type)
7877 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7878 value = digest_init (input_location, type, value, origtype, npc,
7879 strict_string, require_constant_value);
7880 if (value == error_mark_node)
7882 constructor_erroneous = 1;
7883 return;
7885 if (require_constant_value || require_constant_elements)
7886 constant_expression_warning (value);
7888 /* If this element doesn't come next in sequence,
7889 put it on constructor_pending_elts. */
7890 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7891 && (!constructor_incremental
7892 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7894 if (constructor_incremental
7895 && tree_int_cst_lt (field, constructor_unfilled_index))
7896 set_nonincremental_init (braced_init_obstack);
7898 add_pending_init (field, value, origtype, implicit,
7899 braced_init_obstack);
7900 return;
7902 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7903 && (!constructor_incremental
7904 || field != constructor_unfilled_fields))
7906 /* We do this for records but not for unions. In a union,
7907 no matter which field is specified, it can be initialized
7908 right away since it starts at the beginning of the union. */
7909 if (constructor_incremental)
7911 if (!constructor_unfilled_fields)
7912 set_nonincremental_init (braced_init_obstack);
7913 else
7915 tree bitpos, unfillpos;
7917 bitpos = bit_position (field);
7918 unfillpos = bit_position (constructor_unfilled_fields);
7920 if (tree_int_cst_lt (bitpos, unfillpos))
7921 set_nonincremental_init (braced_init_obstack);
7925 add_pending_init (field, value, origtype, implicit,
7926 braced_init_obstack);
7927 return;
7929 else if (TREE_CODE (constructor_type) == UNION_TYPE
7930 && !VEC_empty (constructor_elt, constructor_elements))
7932 if (!implicit)
7934 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7935 constructor_elements)->value))
7936 warning_init (0,
7937 "initialized field with side-effects overwritten");
7938 else if (warn_override_init)
7939 warning_init (OPT_Woverride_init, "initialized field overwritten");
7942 /* We can have just one union field set. */
7943 constructor_elements = 0;
7946 /* Otherwise, output this element either to
7947 constructor_elements or to the assembler file. */
7949 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7950 celt->index = field;
7951 celt->value = value;
7953 /* Advance the variable that indicates sequential elements output. */
7954 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7955 constructor_unfilled_index
7956 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7957 bitsize_one_node);
7958 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7960 constructor_unfilled_fields
7961 = DECL_CHAIN (constructor_unfilled_fields);
7963 /* Skip any nameless bit fields. */
7964 while (constructor_unfilled_fields != 0
7965 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7966 && DECL_NAME (constructor_unfilled_fields) == 0)
7967 constructor_unfilled_fields =
7968 DECL_CHAIN (constructor_unfilled_fields);
7970 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7971 constructor_unfilled_fields = 0;
7973 /* Now output any pending elements which have become next. */
7974 if (pending)
7975 output_pending_init_elements (0, braced_init_obstack);
7978 /* Output any pending elements which have become next.
7979 As we output elements, constructor_unfilled_{fields,index}
7980 advances, which may cause other elements to become next;
7981 if so, they too are output.
7983 If ALL is 0, we return when there are
7984 no more pending elements to output now.
7986 If ALL is 1, we output space as necessary so that
7987 we can output all the pending elements. */
7988 static void
7989 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7991 struct init_node *elt = constructor_pending_elts;
7992 tree next;
7994 retry:
7996 /* Look through the whole pending tree.
7997 If we find an element that should be output now,
7998 output it. Otherwise, set NEXT to the element
7999 that comes first among those still pending. */
8001 next = 0;
8002 while (elt)
8004 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8006 if (tree_int_cst_equal (elt->purpose,
8007 constructor_unfilled_index))
8008 output_init_element (elt->value, elt->origtype, true,
8009 TREE_TYPE (constructor_type),
8010 constructor_unfilled_index, 0, false,
8011 braced_init_obstack);
8012 else if (tree_int_cst_lt (constructor_unfilled_index,
8013 elt->purpose))
8015 /* Advance to the next smaller node. */
8016 if (elt->left)
8017 elt = elt->left;
8018 else
8020 /* We have reached the smallest node bigger than the
8021 current unfilled index. Fill the space first. */
8022 next = elt->purpose;
8023 break;
8026 else
8028 /* Advance to the next bigger node. */
8029 if (elt->right)
8030 elt = elt->right;
8031 else
8033 /* We have reached the biggest node in a subtree. Find
8034 the parent of it, which is the next bigger node. */
8035 while (elt->parent && elt->parent->right == elt)
8036 elt = elt->parent;
8037 elt = elt->parent;
8038 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8039 elt->purpose))
8041 next = elt->purpose;
8042 break;
8047 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8048 || TREE_CODE (constructor_type) == UNION_TYPE)
8050 tree ctor_unfilled_bitpos, elt_bitpos;
8052 /* If the current record is complete we are done. */
8053 if (constructor_unfilled_fields == 0)
8054 break;
8056 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8057 elt_bitpos = bit_position (elt->purpose);
8058 /* We can't compare fields here because there might be empty
8059 fields in between. */
8060 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8062 constructor_unfilled_fields = elt->purpose;
8063 output_init_element (elt->value, elt->origtype, true,
8064 TREE_TYPE (elt->purpose),
8065 elt->purpose, 0, false,
8066 braced_init_obstack);
8068 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8070 /* Advance to the next smaller node. */
8071 if (elt->left)
8072 elt = elt->left;
8073 else
8075 /* We have reached the smallest node bigger than the
8076 current unfilled field. Fill the space first. */
8077 next = elt->purpose;
8078 break;
8081 else
8083 /* Advance to the next bigger node. */
8084 if (elt->right)
8085 elt = elt->right;
8086 else
8088 /* We have reached the biggest node in a subtree. Find
8089 the parent of it, which is the next bigger node. */
8090 while (elt->parent && elt->parent->right == elt)
8091 elt = elt->parent;
8092 elt = elt->parent;
8093 if (elt
8094 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8095 bit_position (elt->purpose))))
8097 next = elt->purpose;
8098 break;
8105 /* Ordinarily return, but not if we want to output all
8106 and there are elements left. */
8107 if (!(all && next != 0))
8108 return;
8110 /* If it's not incremental, just skip over the gap, so that after
8111 jumping to retry we will output the next successive element. */
8112 if (TREE_CODE (constructor_type) == RECORD_TYPE
8113 || TREE_CODE (constructor_type) == UNION_TYPE)
8114 constructor_unfilled_fields = next;
8115 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8116 constructor_unfilled_index = next;
8118 /* ELT now points to the node in the pending tree with the next
8119 initializer to output. */
8120 goto retry;
8123 /* Add one non-braced element to the current constructor level.
8124 This adjusts the current position within the constructor's type.
8125 This may also start or terminate implicit levels
8126 to handle a partly-braced initializer.
8128 Once this has found the correct level for the new element,
8129 it calls output_init_element.
8131 IMPLICIT is true if value comes from pop_init_level (1),
8132 the new initializer has been merged with the existing one
8133 and thus no warnings should be emitted about overriding an
8134 existing initializer. */
8136 void
8137 process_init_element (struct c_expr value, bool implicit,
8138 struct obstack * braced_init_obstack)
8140 tree orig_value = value.value;
8141 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8142 bool strict_string = value.original_code == STRING_CST;
8144 designator_depth = 0;
8145 designator_erroneous = 0;
8147 /* Handle superfluous braces around string cst as in
8148 char x[] = {"foo"}; */
8149 if (string_flag
8150 && constructor_type
8151 && TREE_CODE (constructor_type) == ARRAY_TYPE
8152 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8153 && integer_zerop (constructor_unfilled_index))
8155 if (constructor_stack->replacement_value.value)
8156 error_init ("excess elements in char array initializer");
8157 constructor_stack->replacement_value = value;
8158 return;
8161 if (constructor_stack->replacement_value.value != 0)
8163 error_init ("excess elements in struct initializer");
8164 return;
8167 /* Ignore elements of a brace group if it is entirely superfluous
8168 and has already been diagnosed. */
8169 if (constructor_type == 0)
8170 return;
8172 /* If we've exhausted any levels that didn't have braces,
8173 pop them now. */
8174 while (constructor_stack->implicit)
8176 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8177 || TREE_CODE (constructor_type) == UNION_TYPE)
8178 && constructor_fields == 0)
8179 process_init_element (pop_init_level (1, braced_init_obstack),
8180 true, braced_init_obstack);
8181 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8182 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8183 && (constructor_max_index == 0
8184 || tree_int_cst_lt (constructor_max_index,
8185 constructor_index)))
8186 process_init_element (pop_init_level (1, braced_init_obstack),
8187 true, braced_init_obstack);
8188 else
8189 break;
8192 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8193 if (constructor_range_stack)
8195 /* If value is a compound literal and we'll be just using its
8196 content, don't put it into a SAVE_EXPR. */
8197 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8198 || !require_constant_value
8199 || flag_isoc99)
8201 tree semantic_type = NULL_TREE;
8202 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8204 semantic_type = TREE_TYPE (value.value);
8205 value.value = TREE_OPERAND (value.value, 0);
8207 value.value = c_save_expr (value.value);
8208 if (semantic_type)
8209 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8210 value.value);
8214 while (1)
8216 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8218 tree fieldtype;
8219 enum tree_code fieldcode;
8221 if (constructor_fields == 0)
8223 pedwarn_init (input_location, 0,
8224 "excess elements in struct initializer");
8225 break;
8228 fieldtype = TREE_TYPE (constructor_fields);
8229 if (fieldtype != error_mark_node)
8230 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8231 fieldcode = TREE_CODE (fieldtype);
8233 /* Error for non-static initialization of a flexible array member. */
8234 if (fieldcode == ARRAY_TYPE
8235 && !require_constant_value
8236 && TYPE_SIZE (fieldtype) == NULL_TREE
8237 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8239 error_init ("non-static initialization of a flexible array member");
8240 break;
8243 /* Accept a string constant to initialize a subarray. */
8244 if (value.value != 0
8245 && fieldcode == ARRAY_TYPE
8246 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8247 && string_flag)
8248 value.value = orig_value;
8249 /* Otherwise, if we have come to a subaggregate,
8250 and we don't have an element of its type, push into it. */
8251 else if (value.value != 0
8252 && value.value != error_mark_node
8253 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8254 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8255 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8257 push_init_level (1, braced_init_obstack);
8258 continue;
8261 if (value.value)
8263 push_member_name (constructor_fields);
8264 output_init_element (value.value, value.original_type,
8265 strict_string, fieldtype,
8266 constructor_fields, 1, implicit,
8267 braced_init_obstack);
8268 RESTORE_SPELLING_DEPTH (constructor_depth);
8270 else
8271 /* Do the bookkeeping for an element that was
8272 directly output as a constructor. */
8274 /* For a record, keep track of end position of last field. */
8275 if (DECL_SIZE (constructor_fields))
8276 constructor_bit_index
8277 = size_binop_loc (input_location, PLUS_EXPR,
8278 bit_position (constructor_fields),
8279 DECL_SIZE (constructor_fields));
8281 /* If the current field was the first one not yet written out,
8282 it isn't now, so update. */
8283 if (constructor_unfilled_fields == constructor_fields)
8285 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8286 /* Skip any nameless bit fields. */
8287 while (constructor_unfilled_fields != 0
8288 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8289 && DECL_NAME (constructor_unfilled_fields) == 0)
8290 constructor_unfilled_fields =
8291 DECL_CHAIN (constructor_unfilled_fields);
8295 constructor_fields = DECL_CHAIN (constructor_fields);
8296 /* Skip any nameless bit fields at the beginning. */
8297 while (constructor_fields != 0
8298 && DECL_C_BIT_FIELD (constructor_fields)
8299 && DECL_NAME (constructor_fields) == 0)
8300 constructor_fields = DECL_CHAIN (constructor_fields);
8302 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8304 tree fieldtype;
8305 enum tree_code fieldcode;
8307 if (constructor_fields == 0)
8309 pedwarn_init (input_location, 0,
8310 "excess elements in union initializer");
8311 break;
8314 fieldtype = TREE_TYPE (constructor_fields);
8315 if (fieldtype != error_mark_node)
8316 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8317 fieldcode = TREE_CODE (fieldtype);
8319 /* Warn that traditional C rejects initialization of unions.
8320 We skip the warning if the value is zero. This is done
8321 under the assumption that the zero initializer in user
8322 code appears conditioned on e.g. __STDC__ to avoid
8323 "missing initializer" warnings and relies on default
8324 initialization to zero in the traditional C case.
8325 We also skip the warning if the initializer is designated,
8326 again on the assumption that this must be conditional on
8327 __STDC__ anyway (and we've already complained about the
8328 member-designator already). */
8329 if (!in_system_header && !constructor_designated
8330 && !(value.value && (integer_zerop (value.value)
8331 || real_zerop (value.value))))
8332 warning (OPT_Wtraditional, "traditional C rejects initialization "
8333 "of unions");
8335 /* Accept a string constant to initialize a subarray. */
8336 if (value.value != 0
8337 && fieldcode == ARRAY_TYPE
8338 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8339 && string_flag)
8340 value.value = orig_value;
8341 /* Otherwise, if we have come to a subaggregate,
8342 and we don't have an element of its type, push into it. */
8343 else if (value.value != 0
8344 && value.value != error_mark_node
8345 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8346 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8347 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8349 push_init_level (1, braced_init_obstack);
8350 continue;
8353 if (value.value)
8355 push_member_name (constructor_fields);
8356 output_init_element (value.value, value.original_type,
8357 strict_string, fieldtype,
8358 constructor_fields, 1, implicit,
8359 braced_init_obstack);
8360 RESTORE_SPELLING_DEPTH (constructor_depth);
8362 else
8363 /* Do the bookkeeping for an element that was
8364 directly output as a constructor. */
8366 constructor_bit_index = DECL_SIZE (constructor_fields);
8367 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8370 constructor_fields = 0;
8372 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8374 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8375 enum tree_code eltcode = TREE_CODE (elttype);
8377 /* Accept a string constant to initialize a subarray. */
8378 if (value.value != 0
8379 && eltcode == ARRAY_TYPE
8380 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8381 && string_flag)
8382 value.value = orig_value;
8383 /* Otherwise, if we have come to a subaggregate,
8384 and we don't have an element of its type, push into it. */
8385 else if (value.value != 0
8386 && value.value != error_mark_node
8387 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8388 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8389 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8391 push_init_level (1, braced_init_obstack);
8392 continue;
8395 if (constructor_max_index != 0
8396 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8397 || integer_all_onesp (constructor_max_index)))
8399 pedwarn_init (input_location, 0,
8400 "excess elements in array initializer");
8401 break;
8404 /* Now output the actual element. */
8405 if (value.value)
8407 push_array_bounds (tree_low_cst (constructor_index, 1));
8408 output_init_element (value.value, value.original_type,
8409 strict_string, elttype,
8410 constructor_index, 1, implicit,
8411 braced_init_obstack);
8412 RESTORE_SPELLING_DEPTH (constructor_depth);
8415 constructor_index
8416 = size_binop_loc (input_location, PLUS_EXPR,
8417 constructor_index, bitsize_one_node);
8419 if (!value.value)
8420 /* If we are doing the bookkeeping for an element that was
8421 directly output as a constructor, we must update
8422 constructor_unfilled_index. */
8423 constructor_unfilled_index = constructor_index;
8425 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8427 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8429 /* Do a basic check of initializer size. Note that vectors
8430 always have a fixed size derived from their type. */
8431 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8433 pedwarn_init (input_location, 0,
8434 "excess elements in vector initializer");
8435 break;
8438 /* Now output the actual element. */
8439 if (value.value)
8441 if (TREE_CODE (value.value) == VECTOR_CST)
8442 elttype = TYPE_MAIN_VARIANT (constructor_type);
8443 output_init_element (value.value, value.original_type,
8444 strict_string, elttype,
8445 constructor_index, 1, implicit,
8446 braced_init_obstack);
8449 constructor_index
8450 = size_binop_loc (input_location,
8451 PLUS_EXPR, constructor_index, bitsize_one_node);
8453 if (!value.value)
8454 /* If we are doing the bookkeeping for an element that was
8455 directly output as a constructor, we must update
8456 constructor_unfilled_index. */
8457 constructor_unfilled_index = constructor_index;
8460 /* Handle the sole element allowed in a braced initializer
8461 for a scalar variable. */
8462 else if (constructor_type != error_mark_node
8463 && constructor_fields == 0)
8465 pedwarn_init (input_location, 0,
8466 "excess elements in scalar initializer");
8467 break;
8469 else
8471 if (value.value)
8472 output_init_element (value.value, value.original_type,
8473 strict_string, constructor_type,
8474 NULL_TREE, 1, implicit,
8475 braced_init_obstack);
8476 constructor_fields = 0;
8479 /* Handle range initializers either at this level or anywhere higher
8480 in the designator stack. */
8481 if (constructor_range_stack)
8483 struct constructor_range_stack *p, *range_stack;
8484 int finish = 0;
8486 range_stack = constructor_range_stack;
8487 constructor_range_stack = 0;
8488 while (constructor_stack != range_stack->stack)
8490 gcc_assert (constructor_stack->implicit);
8491 process_init_element (pop_init_level (1,
8492 braced_init_obstack),
8493 true, braced_init_obstack);
8495 for (p = range_stack;
8496 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8497 p = p->prev)
8499 gcc_assert (constructor_stack->implicit);
8500 process_init_element (pop_init_level (1, braced_init_obstack),
8501 true, braced_init_obstack);
8504 p->index = size_binop_loc (input_location,
8505 PLUS_EXPR, p->index, bitsize_one_node);
8506 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8507 finish = 1;
8509 while (1)
8511 constructor_index = p->index;
8512 constructor_fields = p->fields;
8513 if (finish && p->range_end && p->index == p->range_start)
8515 finish = 0;
8516 p->prev = 0;
8518 p = p->next;
8519 if (!p)
8520 break;
8521 push_init_level (2, braced_init_obstack);
8522 p->stack = constructor_stack;
8523 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8524 p->index = p->range_start;
8527 if (!finish)
8528 constructor_range_stack = range_stack;
8529 continue;
8532 break;
8535 constructor_range_stack = 0;
8538 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8539 (guaranteed to be 'volatile' or null) and ARGS (represented using
8540 an ASM_EXPR node). */
8541 tree
8542 build_asm_stmt (tree cv_qualifier, tree args)
8544 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8545 ASM_VOLATILE_P (args) = 1;
8546 return add_stmt (args);
8549 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8550 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8551 SIMPLE indicates whether there was anything at all after the
8552 string in the asm expression -- asm("blah") and asm("blah" : )
8553 are subtly different. We use a ASM_EXPR node to represent this. */
8554 tree
8555 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8556 tree clobbers, tree labels, bool simple)
8558 tree tail;
8559 tree args;
8560 int i;
8561 const char *constraint;
8562 const char **oconstraints;
8563 bool allows_mem, allows_reg, is_inout;
8564 int ninputs, noutputs;
8566 ninputs = list_length (inputs);
8567 noutputs = list_length (outputs);
8568 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8570 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8572 /* Remove output conversions that change the type but not the mode. */
8573 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8575 tree output = TREE_VALUE (tail);
8577 /* ??? Really, this should not be here. Users should be using a
8578 proper lvalue, dammit. But there's a long history of using casts
8579 in the output operands. In cases like longlong.h, this becomes a
8580 primitive form of typechecking -- if the cast can be removed, then
8581 the output operand had a type of the proper width; otherwise we'll
8582 get an error. Gross, but ... */
8583 STRIP_NOPS (output);
8585 if (!lvalue_or_else (loc, output, lv_asm))
8586 output = error_mark_node;
8588 if (output != error_mark_node
8589 && (TREE_READONLY (output)
8590 || TYPE_READONLY (TREE_TYPE (output))
8591 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8592 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8593 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8594 readonly_error (output, lv_asm);
8596 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8597 oconstraints[i] = constraint;
8599 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8600 &allows_mem, &allows_reg, &is_inout))
8602 /* If the operand is going to end up in memory,
8603 mark it addressable. */
8604 if (!allows_reg && !c_mark_addressable (output))
8605 output = error_mark_node;
8606 if (!(!allows_reg && allows_mem)
8607 && output != error_mark_node
8608 && VOID_TYPE_P (TREE_TYPE (output)))
8610 error_at (loc, "invalid use of void expression");
8611 output = error_mark_node;
8614 else
8615 output = error_mark_node;
8617 TREE_VALUE (tail) = output;
8620 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8622 tree input;
8624 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8625 input = TREE_VALUE (tail);
8627 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8628 oconstraints, &allows_mem, &allows_reg))
8630 /* If the operand is going to end up in memory,
8631 mark it addressable. */
8632 if (!allows_reg && allows_mem)
8634 /* Strip the nops as we allow this case. FIXME, this really
8635 should be rejected or made deprecated. */
8636 STRIP_NOPS (input);
8637 if (!c_mark_addressable (input))
8638 input = error_mark_node;
8640 else if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8642 error_at (loc, "invalid use of void expression");
8643 input = error_mark_node;
8646 else
8647 input = error_mark_node;
8649 TREE_VALUE (tail) = input;
8652 /* ASMs with labels cannot have outputs. This should have been
8653 enforced by the parser. */
8654 gcc_assert (outputs == NULL || labels == NULL);
8656 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8658 /* asm statements without outputs, including simple ones, are treated
8659 as volatile. */
8660 ASM_INPUT_P (args) = simple;
8661 ASM_VOLATILE_P (args) = (noutputs == 0);
8663 return args;
8666 /* Generate a goto statement to LABEL. LOC is the location of the
8667 GOTO. */
8669 tree
8670 c_finish_goto_label (location_t loc, tree label)
8672 tree decl = lookup_label_for_goto (loc, label);
8673 if (!decl)
8674 return NULL_TREE;
8675 TREE_USED (decl) = 1;
8677 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8678 SET_EXPR_LOCATION (t, loc);
8679 return add_stmt (t);
8683 /* Generate a computed goto statement to EXPR. LOC is the location of
8684 the GOTO. */
8686 tree
8687 c_finish_goto_ptr (location_t loc, tree expr)
8689 tree t;
8690 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8691 expr = c_fully_fold (expr, false, NULL);
8692 expr = convert (ptr_type_node, expr);
8693 t = build1 (GOTO_EXPR, void_type_node, expr);
8694 SET_EXPR_LOCATION (t, loc);
8695 return add_stmt (t);
8698 /* Generate a C `return' statement. RETVAL is the expression for what
8699 to return, or a null pointer for `return;' with no value. LOC is
8700 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8701 is the original type of RETVAL. */
8703 tree
8704 c_finish_return (location_t loc, tree retval, tree origtype)
8706 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8707 bool no_warning = false;
8708 bool npc = false;
8710 if (TREE_THIS_VOLATILE (current_function_decl))
8711 warning_at (loc, 0,
8712 "function declared %<noreturn%> has a %<return%> statement");
8714 if (retval)
8716 tree semantic_type = NULL_TREE;
8717 npc = null_pointer_constant_p (retval);
8718 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8720 semantic_type = TREE_TYPE (retval);
8721 retval = TREE_OPERAND (retval, 0);
8723 retval = c_fully_fold (retval, false, NULL);
8724 if (semantic_type)
8725 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8728 if (!retval)
8730 current_function_returns_null = 1;
8731 if ((warn_return_type || flag_isoc99)
8732 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8734 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8735 "%<return%> with no value, in "
8736 "function returning non-void");
8737 no_warning = true;
8740 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8742 current_function_returns_null = 1;
8743 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8744 pedwarn (loc, 0,
8745 "%<return%> with a value, in function returning void");
8746 else
8747 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8748 "%<return%> with expression, in function returning void");
8750 else
8752 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8753 ic_return,
8754 npc, NULL_TREE, NULL_TREE, 0);
8755 tree res = DECL_RESULT (current_function_decl);
8756 tree inner;
8758 current_function_returns_value = 1;
8759 if (t == error_mark_node)
8760 return NULL_TREE;
8762 inner = t = convert (TREE_TYPE (res), t);
8764 /* Strip any conversions, additions, and subtractions, and see if
8765 we are returning the address of a local variable. Warn if so. */
8766 while (1)
8768 switch (TREE_CODE (inner))
8770 CASE_CONVERT:
8771 case NON_LVALUE_EXPR:
8772 case PLUS_EXPR:
8773 case POINTER_PLUS_EXPR:
8774 inner = TREE_OPERAND (inner, 0);
8775 continue;
8777 case MINUS_EXPR:
8778 /* If the second operand of the MINUS_EXPR has a pointer
8779 type (or is converted from it), this may be valid, so
8780 don't give a warning. */
8782 tree op1 = TREE_OPERAND (inner, 1);
8784 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8785 && (CONVERT_EXPR_P (op1)
8786 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8787 op1 = TREE_OPERAND (op1, 0);
8789 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8790 break;
8792 inner = TREE_OPERAND (inner, 0);
8793 continue;
8796 case ADDR_EXPR:
8797 inner = TREE_OPERAND (inner, 0);
8799 while (REFERENCE_CLASS_P (inner)
8800 && TREE_CODE (inner) != INDIRECT_REF)
8801 inner = TREE_OPERAND (inner, 0);
8803 if (DECL_P (inner)
8804 && !DECL_EXTERNAL (inner)
8805 && !TREE_STATIC (inner)
8806 && DECL_CONTEXT (inner) == current_function_decl)
8807 warning_at (loc,
8808 0, "function returns address of local variable");
8809 break;
8811 default:
8812 break;
8815 break;
8818 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8819 SET_EXPR_LOCATION (retval, loc);
8821 if (warn_sequence_point)
8822 verify_sequence_points (retval);
8825 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8826 TREE_NO_WARNING (ret_stmt) |= no_warning;
8827 return add_stmt (ret_stmt);
8830 struct c_switch {
8831 /* The SWITCH_EXPR being built. */
8832 tree switch_expr;
8834 /* The original type of the testing expression, i.e. before the
8835 default conversion is applied. */
8836 tree orig_type;
8838 /* A splay-tree mapping the low element of a case range to the high
8839 element, or NULL_TREE if there is no high element. Used to
8840 determine whether or not a new case label duplicates an old case
8841 label. We need a tree, rather than simply a hash table, because
8842 of the GNU case range extension. */
8843 splay_tree cases;
8845 /* The bindings at the point of the switch. This is used for
8846 warnings crossing decls when branching to a case label. */
8847 struct c_spot_bindings *bindings;
8849 /* The next node on the stack. */
8850 struct c_switch *next;
8853 /* A stack of the currently active switch statements. The innermost
8854 switch statement is on the top of the stack. There is no need to
8855 mark the stack for garbage collection because it is only active
8856 during the processing of the body of a function, and we never
8857 collect at that point. */
8859 struct c_switch *c_switch_stack;
8861 /* Start a C switch statement, testing expression EXP. Return the new
8862 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8863 SWITCH_COND_LOC is the location of the switch's condition. */
8865 tree
8866 c_start_case (location_t switch_loc,
8867 location_t switch_cond_loc,
8868 tree exp)
8870 tree orig_type = error_mark_node;
8871 struct c_switch *cs;
8873 if (exp != error_mark_node)
8875 orig_type = TREE_TYPE (exp);
8877 if (!INTEGRAL_TYPE_P (orig_type))
8879 if (orig_type != error_mark_node)
8881 error_at (switch_cond_loc, "switch quantity not an integer");
8882 orig_type = error_mark_node;
8884 exp = integer_zero_node;
8886 else
8888 tree type = TYPE_MAIN_VARIANT (orig_type);
8890 if (!in_system_header
8891 && (type == long_integer_type_node
8892 || type == long_unsigned_type_node))
8893 warning_at (switch_cond_loc,
8894 OPT_Wtraditional, "%<long%> switch expression not "
8895 "converted to %<int%> in ISO C");
8897 exp = c_fully_fold (exp, false, NULL);
8898 exp = default_conversion (exp);
8900 if (warn_sequence_point)
8901 verify_sequence_points (exp);
8905 /* Add this new SWITCH_EXPR to the stack. */
8906 cs = XNEW (struct c_switch);
8907 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8908 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8909 cs->orig_type = orig_type;
8910 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8911 cs->bindings = c_get_switch_bindings ();
8912 cs->next = c_switch_stack;
8913 c_switch_stack = cs;
8915 return add_stmt (cs->switch_expr);
8918 /* Process a case label at location LOC. */
8920 tree
8921 do_case (location_t loc, tree low_value, tree high_value)
8923 tree label = NULL_TREE;
8925 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8927 low_value = c_fully_fold (low_value, false, NULL);
8928 if (TREE_CODE (low_value) == INTEGER_CST)
8929 pedwarn (input_location, OPT_pedantic,
8930 "case label is not an integer constant expression");
8933 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8935 high_value = c_fully_fold (high_value, false, NULL);
8936 if (TREE_CODE (high_value) == INTEGER_CST)
8937 pedwarn (input_location, OPT_pedantic,
8938 "case label is not an integer constant expression");
8941 if (c_switch_stack == NULL)
8943 if (low_value)
8944 error_at (loc, "case label not within a switch statement");
8945 else
8946 error_at (loc, "%<default%> label not within a switch statement");
8947 return NULL_TREE;
8950 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8951 EXPR_LOCATION (c_switch_stack->switch_expr),
8952 loc))
8953 return NULL_TREE;
8955 label = c_add_case_label (loc, c_switch_stack->cases,
8956 SWITCH_COND (c_switch_stack->switch_expr),
8957 c_switch_stack->orig_type,
8958 low_value, high_value);
8959 if (label == error_mark_node)
8960 label = NULL_TREE;
8961 return label;
8964 /* Finish the switch statement. */
8966 void
8967 c_finish_case (tree body)
8969 struct c_switch *cs = c_switch_stack;
8970 location_t switch_location;
8972 SWITCH_BODY (cs->switch_expr) = body;
8974 /* Emit warnings as needed. */
8975 switch_location = EXPR_LOCATION (cs->switch_expr);
8976 c_do_switch_warnings (cs->cases, switch_location,
8977 TREE_TYPE (cs->switch_expr),
8978 SWITCH_COND (cs->switch_expr));
8980 /* Pop the stack. */
8981 c_switch_stack = cs->next;
8982 splay_tree_delete (cs->cases);
8983 c_release_switch_bindings (cs->bindings);
8984 XDELETE (cs);
8987 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8988 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8989 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8990 statement, and was not surrounded with parenthesis. */
8992 void
8993 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8994 tree else_block, bool nested_if)
8996 tree stmt;
8998 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8999 if (warn_parentheses && nested_if && else_block == NULL)
9001 tree inner_if = then_block;
9003 /* We know from the grammar productions that there is an IF nested
9004 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9005 it might not be exactly THEN_BLOCK, but should be the last
9006 non-container statement within. */
9007 while (1)
9008 switch (TREE_CODE (inner_if))
9010 case COND_EXPR:
9011 goto found;
9012 case BIND_EXPR:
9013 inner_if = BIND_EXPR_BODY (inner_if);
9014 break;
9015 case STATEMENT_LIST:
9016 inner_if = expr_last (then_block);
9017 break;
9018 case TRY_FINALLY_EXPR:
9019 case TRY_CATCH_EXPR:
9020 inner_if = TREE_OPERAND (inner_if, 0);
9021 break;
9022 default:
9023 gcc_unreachable ();
9025 found:
9027 if (COND_EXPR_ELSE (inner_if))
9028 warning_at (if_locus, OPT_Wparentheses,
9029 "suggest explicit braces to avoid ambiguous %<else%>");
9032 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9033 SET_EXPR_LOCATION (stmt, if_locus);
9034 add_stmt (stmt);
9037 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9038 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9039 is false for DO loops. INCR is the FOR increment expression. BODY is
9040 the statement controlled by the loop. BLAB is the break label. CLAB is
9041 the continue label. Everything is allowed to be NULL. */
9043 void
9044 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9045 tree blab, tree clab, bool cond_is_first)
9047 tree entry = NULL, exit = NULL, t;
9049 /* If the condition is zero don't generate a loop construct. */
9050 if (cond && integer_zerop (cond))
9052 if (cond_is_first)
9054 t = build_and_jump (&blab);
9055 SET_EXPR_LOCATION (t, start_locus);
9056 add_stmt (t);
9059 else
9061 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9063 /* If we have an exit condition, then we build an IF with gotos either
9064 out of the loop, or to the top of it. If there's no exit condition,
9065 then we just build a jump back to the top. */
9066 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9068 if (cond && !integer_nonzerop (cond))
9070 /* Canonicalize the loop condition to the end. This means
9071 generating a branch to the loop condition. Reuse the
9072 continue label, if possible. */
9073 if (cond_is_first)
9075 if (incr || !clab)
9077 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9078 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9080 else
9081 t = build1 (GOTO_EXPR, void_type_node, clab);
9082 SET_EXPR_LOCATION (t, start_locus);
9083 add_stmt (t);
9086 t = build_and_jump (&blab);
9087 if (cond_is_first)
9088 exit = fold_build3_loc (start_locus,
9089 COND_EXPR, void_type_node, cond, exit, t);
9090 else
9091 exit = fold_build3_loc (input_location,
9092 COND_EXPR, void_type_node, cond, exit, t);
9095 add_stmt (top);
9098 if (body)
9099 add_stmt (body);
9100 if (clab)
9101 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9102 if (incr)
9103 add_stmt (incr);
9104 if (entry)
9105 add_stmt (entry);
9106 if (exit)
9107 add_stmt (exit);
9108 if (blab)
9109 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9112 tree
9113 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9115 bool skip;
9116 tree label = *label_p;
9118 /* In switch statements break is sometimes stylistically used after
9119 a return statement. This can lead to spurious warnings about
9120 control reaching the end of a non-void function when it is
9121 inlined. Note that we are calling block_may_fallthru with
9122 language specific tree nodes; this works because
9123 block_may_fallthru returns true when given something it does not
9124 understand. */
9125 skip = !block_may_fallthru (cur_stmt_list);
9127 if (!label)
9129 if (!skip)
9130 *label_p = label = create_artificial_label (loc);
9132 else if (TREE_CODE (label) == LABEL_DECL)
9134 else switch (TREE_INT_CST_LOW (label))
9136 case 0:
9137 if (is_break)
9138 error_at (loc, "break statement not within loop or switch");
9139 else
9140 error_at (loc, "continue statement not within a loop");
9141 return NULL_TREE;
9143 case 1:
9144 gcc_assert (is_break);
9145 error_at (loc, "break statement used with OpenMP for loop");
9146 return NULL_TREE;
9148 default:
9149 gcc_unreachable ();
9152 if (skip)
9153 return NULL_TREE;
9155 if (!is_break)
9156 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9158 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9161 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9163 static void
9164 emit_side_effect_warnings (location_t loc, tree expr)
9166 if (expr == error_mark_node)
9168 else if (!TREE_SIDE_EFFECTS (expr))
9170 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9171 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9173 else
9174 warn_if_unused_value (expr, loc);
9177 /* Process an expression as if it were a complete statement. Emit
9178 diagnostics, but do not call ADD_STMT. LOC is the location of the
9179 statement. */
9181 tree
9182 c_process_expr_stmt (location_t loc, tree expr)
9184 tree exprv;
9186 if (!expr)
9187 return NULL_TREE;
9189 expr = c_fully_fold (expr, false, NULL);
9191 if (warn_sequence_point)
9192 verify_sequence_points (expr);
9194 if (TREE_TYPE (expr) != error_mark_node
9195 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9196 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9197 error_at (loc, "expression statement has incomplete type");
9199 /* If we're not processing a statement expression, warn about unused values.
9200 Warnings for statement expressions will be emitted later, once we figure
9201 out which is the result. */
9202 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9203 && warn_unused_value)
9204 emit_side_effect_warnings (loc, expr);
9206 exprv = expr;
9207 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9208 exprv = TREE_OPERAND (exprv, 1);
9209 while (CONVERT_EXPR_P (exprv))
9210 exprv = TREE_OPERAND (exprv, 0);
9211 if (DECL_P (exprv)
9212 || handled_component_p (exprv)
9213 || TREE_CODE (exprv) == ADDR_EXPR)
9214 mark_exp_read (exprv);
9216 /* If the expression is not of a type to which we cannot assign a line
9217 number, wrap the thing in a no-op NOP_EXPR. */
9218 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9220 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9221 SET_EXPR_LOCATION (expr, loc);
9224 return expr;
9227 /* Emit an expression as a statement. LOC is the location of the
9228 expression. */
9230 tree
9231 c_finish_expr_stmt (location_t loc, tree expr)
9233 if (expr)
9234 return add_stmt (c_process_expr_stmt (loc, expr));
9235 else
9236 return NULL;
9239 /* Do the opposite and emit a statement as an expression. To begin,
9240 create a new binding level and return it. */
9242 tree
9243 c_begin_stmt_expr (void)
9245 tree ret;
9247 /* We must force a BLOCK for this level so that, if it is not expanded
9248 later, there is a way to turn off the entire subtree of blocks that
9249 are contained in it. */
9250 keep_next_level ();
9251 ret = c_begin_compound_stmt (true);
9253 c_bindings_start_stmt_expr (c_switch_stack == NULL
9254 ? NULL
9255 : c_switch_stack->bindings);
9257 /* Mark the current statement list as belonging to a statement list. */
9258 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9260 return ret;
9263 /* LOC is the location of the compound statement to which this body
9264 belongs. */
9266 tree
9267 c_finish_stmt_expr (location_t loc, tree body)
9269 tree last, type, tmp, val;
9270 tree *last_p;
9272 body = c_end_compound_stmt (loc, body, true);
9274 c_bindings_end_stmt_expr (c_switch_stack == NULL
9275 ? NULL
9276 : c_switch_stack->bindings);
9278 /* Locate the last statement in BODY. See c_end_compound_stmt
9279 about always returning a BIND_EXPR. */
9280 last_p = &BIND_EXPR_BODY (body);
9281 last = BIND_EXPR_BODY (body);
9283 continue_searching:
9284 if (TREE_CODE (last) == STATEMENT_LIST)
9286 tree_stmt_iterator i;
9288 /* This can happen with degenerate cases like ({ }). No value. */
9289 if (!TREE_SIDE_EFFECTS (last))
9290 return body;
9292 /* If we're supposed to generate side effects warnings, process
9293 all of the statements except the last. */
9294 if (warn_unused_value)
9296 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9298 location_t tloc;
9299 tree t = tsi_stmt (i);
9301 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9302 emit_side_effect_warnings (tloc, t);
9305 else
9306 i = tsi_last (last);
9307 last_p = tsi_stmt_ptr (i);
9308 last = *last_p;
9311 /* If the end of the list is exception related, then the list was split
9312 by a call to push_cleanup. Continue searching. */
9313 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9314 || TREE_CODE (last) == TRY_CATCH_EXPR)
9316 last_p = &TREE_OPERAND (last, 0);
9317 last = *last_p;
9318 goto continue_searching;
9321 if (last == error_mark_node)
9322 return last;
9324 /* In the case that the BIND_EXPR is not necessary, return the
9325 expression out from inside it. */
9326 if (last == BIND_EXPR_BODY (body)
9327 && BIND_EXPR_VARS (body) == NULL)
9329 /* Even if this looks constant, do not allow it in a constant
9330 expression. */
9331 last = c_wrap_maybe_const (last, true);
9332 /* Do not warn if the return value of a statement expression is
9333 unused. */
9334 TREE_NO_WARNING (last) = 1;
9335 return last;
9338 /* Extract the type of said expression. */
9339 type = TREE_TYPE (last);
9341 /* If we're not returning a value at all, then the BIND_EXPR that
9342 we already have is a fine expression to return. */
9343 if (!type || VOID_TYPE_P (type))
9344 return body;
9346 /* Now that we've located the expression containing the value, it seems
9347 silly to make voidify_wrapper_expr repeat the process. Create a
9348 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9349 tmp = create_tmp_var_raw (type, NULL);
9351 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9352 tree_expr_nonnegative_p giving up immediately. */
9353 val = last;
9354 if (TREE_CODE (val) == NOP_EXPR
9355 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9356 val = TREE_OPERAND (val, 0);
9358 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9359 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9362 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9363 SET_EXPR_LOCATION (t, loc);
9364 return t;
9368 /* Begin and end compound statements. This is as simple as pushing
9369 and popping new statement lists from the tree. */
9371 tree
9372 c_begin_compound_stmt (bool do_scope)
9374 tree stmt = push_stmt_list ();
9375 if (do_scope)
9376 push_scope ();
9377 return stmt;
9380 /* End a compound statement. STMT is the statement. LOC is the
9381 location of the compound statement-- this is usually the location
9382 of the opening brace. */
9384 tree
9385 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9387 tree block = NULL;
9389 if (do_scope)
9391 if (c_dialect_objc ())
9392 objc_clear_super_receiver ();
9393 block = pop_scope ();
9396 stmt = pop_stmt_list (stmt);
9397 stmt = c_build_bind_expr (loc, block, stmt);
9399 /* If this compound statement is nested immediately inside a statement
9400 expression, then force a BIND_EXPR to be created. Otherwise we'll
9401 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9402 STATEMENT_LISTs merge, and thus we can lose track of what statement
9403 was really last. */
9404 if (building_stmt_list_p ()
9405 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9406 && TREE_CODE (stmt) != BIND_EXPR)
9408 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9409 TREE_SIDE_EFFECTS (stmt) = 1;
9410 SET_EXPR_LOCATION (stmt, loc);
9413 return stmt;
9416 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9417 when the current scope is exited. EH_ONLY is true when this is not
9418 meant to apply to normal control flow transfer. */
9420 void
9421 push_cleanup (tree decl, tree cleanup, bool eh_only)
9423 enum tree_code code;
9424 tree stmt, list;
9425 bool stmt_expr;
9427 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9428 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9429 add_stmt (stmt);
9430 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9431 list = push_stmt_list ();
9432 TREE_OPERAND (stmt, 0) = list;
9433 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9436 /* Convert scalar to vector for the range of operations. */
9437 static enum stv_conv
9438 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1)
9440 tree type0 = TREE_TYPE (op0);
9441 tree type1 = TREE_TYPE (op1);
9442 bool integer_only_op = false;
9443 enum stv_conv ret = stv_firstarg;
9445 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
9446 || TREE_CODE (type1) == VECTOR_TYPE);
9447 switch (code)
9449 case RSHIFT_EXPR:
9450 case LSHIFT_EXPR:
9451 if (TREE_CODE (type0) == INTEGER_TYPE
9452 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9454 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9456 error_at (loc, "conversion of scalar to vector "
9457 "involves truncation");
9458 return stv_error;
9460 else
9461 return stv_firstarg;
9463 break;
9465 case BIT_IOR_EXPR:
9466 case BIT_XOR_EXPR:
9467 case BIT_AND_EXPR:
9468 integer_only_op = true;
9469 /* ... fall through ... */
9471 case PLUS_EXPR:
9472 case MINUS_EXPR:
9473 case MULT_EXPR:
9474 case TRUNC_DIV_EXPR:
9475 case TRUNC_MOD_EXPR:
9476 case RDIV_EXPR:
9477 if (TREE_CODE (type0) == VECTOR_TYPE)
9479 tree tmp;
9480 ret = stv_secondarg;
9481 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
9482 tmp = type0; type0 = type1; type1 = tmp;
9483 tmp = op0; op0 = op1; op1 = tmp;
9486 if (TREE_CODE (type0) == INTEGER_TYPE
9487 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9489 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9491 error_at (loc, "conversion of scalar to vector "
9492 "involves truncation");
9493 return stv_error;
9495 return ret;
9497 else if (!integer_only_op
9498 /* Allow integer --> real conversion if safe. */
9499 && (TREE_CODE (type0) == REAL_TYPE
9500 || TREE_CODE (type0) == INTEGER_TYPE)
9501 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
9503 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9505 error_at (loc, "conversion of scalar to vector "
9506 "involves truncation");
9507 return stv_error;
9509 return ret;
9511 default:
9512 break;
9515 return stv_nothing;
9518 /* Build a binary-operation expression without default conversions.
9519 CODE is the kind of expression to build.
9520 LOCATION is the operator's location.
9521 This function differs from `build' in several ways:
9522 the data type of the result is computed and recorded in it,
9523 warnings are generated if arg data types are invalid,
9524 special handling for addition and subtraction of pointers is known,
9525 and some optimization is done (operations on narrow ints
9526 are done in the narrower type when that gives the same result).
9527 Constant folding is also done before the result is returned.
9529 Note that the operands will never have enumeral types, or function
9530 or array types, because either they will have the default conversions
9531 performed or they have both just been converted to some other type in which
9532 the arithmetic is to be done. */
9534 tree
9535 build_binary_op (location_t location, enum tree_code code,
9536 tree orig_op0, tree orig_op1, int convert_p)
9538 tree type0, type1, orig_type0, orig_type1;
9539 tree eptype;
9540 enum tree_code code0, code1;
9541 tree op0, op1;
9542 tree ret = error_mark_node;
9543 const char *invalid_op_diag;
9544 bool op0_int_operands, op1_int_operands;
9545 bool int_const, int_const_or_overflow, int_operands;
9547 /* Expression code to give to the expression when it is built.
9548 Normally this is CODE, which is what the caller asked for,
9549 but in some special cases we change it. */
9550 enum tree_code resultcode = code;
9552 /* Data type in which the computation is to be performed.
9553 In the simplest cases this is the common type of the arguments. */
9554 tree result_type = NULL;
9556 /* When the computation is in excess precision, the type of the
9557 final EXCESS_PRECISION_EXPR. */
9558 tree semantic_result_type = NULL;
9560 /* Nonzero means operands have already been type-converted
9561 in whatever way is necessary.
9562 Zero means they need to be converted to RESULT_TYPE. */
9563 int converted = 0;
9565 /* Nonzero means create the expression with this type, rather than
9566 RESULT_TYPE. */
9567 tree build_type = 0;
9569 /* Nonzero means after finally constructing the expression
9570 convert it to this type. */
9571 tree final_type = 0;
9573 /* Nonzero if this is an operation like MIN or MAX which can
9574 safely be computed in short if both args are promoted shorts.
9575 Also implies COMMON.
9576 -1 indicates a bitwise operation; this makes a difference
9577 in the exact conditions for when it is safe to do the operation
9578 in a narrower mode. */
9579 int shorten = 0;
9581 /* Nonzero if this is a comparison operation;
9582 if both args are promoted shorts, compare the original shorts.
9583 Also implies COMMON. */
9584 int short_compare = 0;
9586 /* Nonzero if this is a right-shift operation, which can be computed on the
9587 original short and then promoted if the operand is a promoted short. */
9588 int short_shift = 0;
9590 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9591 int common = 0;
9593 /* True means types are compatible as far as ObjC is concerned. */
9594 bool objc_ok;
9596 /* True means this is an arithmetic operation that may need excess
9597 precision. */
9598 bool may_need_excess_precision;
9600 /* True means this is a boolean operation that converts both its
9601 operands to truth-values. */
9602 bool boolean_op = false;
9604 if (location == UNKNOWN_LOCATION)
9605 location = input_location;
9607 op0 = orig_op0;
9608 op1 = orig_op1;
9610 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9611 if (op0_int_operands)
9612 op0 = remove_c_maybe_const_expr (op0);
9613 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9614 if (op1_int_operands)
9615 op1 = remove_c_maybe_const_expr (op1);
9616 int_operands = (op0_int_operands && op1_int_operands);
9617 if (int_operands)
9619 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9620 && TREE_CODE (orig_op1) == INTEGER_CST);
9621 int_const = (int_const_or_overflow
9622 && !TREE_OVERFLOW (orig_op0)
9623 && !TREE_OVERFLOW (orig_op1));
9625 else
9626 int_const = int_const_or_overflow = false;
9628 /* Do not apply default conversion in mixed vector/scalar expression. */
9629 if (convert_p
9630 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
9631 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
9633 op0 = default_conversion (op0);
9634 op1 = default_conversion (op1);
9637 orig_type0 = type0 = TREE_TYPE (op0);
9638 orig_type1 = type1 = TREE_TYPE (op1);
9640 /* The expression codes of the data types of the arguments tell us
9641 whether the arguments are integers, floating, pointers, etc. */
9642 code0 = TREE_CODE (type0);
9643 code1 = TREE_CODE (type1);
9645 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9646 STRIP_TYPE_NOPS (op0);
9647 STRIP_TYPE_NOPS (op1);
9649 /* If an error was already reported for one of the arguments,
9650 avoid reporting another error. */
9652 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9653 return error_mark_node;
9655 if ((invalid_op_diag
9656 = targetm.invalid_binary_op (code, type0, type1)))
9658 error_at (location, invalid_op_diag);
9659 return error_mark_node;
9662 switch (code)
9664 case PLUS_EXPR:
9665 case MINUS_EXPR:
9666 case MULT_EXPR:
9667 case TRUNC_DIV_EXPR:
9668 case CEIL_DIV_EXPR:
9669 case FLOOR_DIV_EXPR:
9670 case ROUND_DIV_EXPR:
9671 case EXACT_DIV_EXPR:
9672 may_need_excess_precision = true;
9673 break;
9674 default:
9675 may_need_excess_precision = false;
9676 break;
9678 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9680 op0 = TREE_OPERAND (op0, 0);
9681 type0 = TREE_TYPE (op0);
9683 else if (may_need_excess_precision
9684 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9686 type0 = eptype;
9687 op0 = convert (eptype, op0);
9689 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9691 op1 = TREE_OPERAND (op1, 0);
9692 type1 = TREE_TYPE (op1);
9694 else if (may_need_excess_precision
9695 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9697 type1 = eptype;
9698 op1 = convert (eptype, op1);
9701 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9703 /* In case when one of the operands of the binary operation is
9704 a vector and another is a scalar -- convert scalar to vector. */
9705 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
9707 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1);
9709 switch (convert_flag)
9711 case stv_error:
9712 return error_mark_node;
9713 case stv_firstarg:
9715 bool maybe_const = true;
9716 tree sc;
9717 sc = c_fully_fold (op0, false, &maybe_const);
9718 sc = save_expr (sc);
9719 sc = convert (TREE_TYPE (type1), sc);
9720 op0 = build_vector_from_val (type1, sc);
9721 if (!maybe_const)
9722 op0 = c_wrap_maybe_const (op0, true);
9723 orig_type0 = type0 = TREE_TYPE (op0);
9724 code0 = TREE_CODE (type0);
9725 converted = 1;
9726 break;
9728 case stv_secondarg:
9730 bool maybe_const = true;
9731 tree sc;
9732 sc = c_fully_fold (op1, false, &maybe_const);
9733 sc = save_expr (sc);
9734 sc = convert (TREE_TYPE (type0), sc);
9735 op1 = build_vector_from_val (type0, sc);
9736 if (!maybe_const)
9737 op0 = c_wrap_maybe_const (op1, true);
9738 orig_type1 = type1 = TREE_TYPE (op1);
9739 code1 = TREE_CODE (type1);
9740 converted = 1;
9741 break;
9743 default:
9744 break;
9748 switch (code)
9750 case PLUS_EXPR:
9751 /* Handle the pointer + int case. */
9752 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9754 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9755 goto return_build_binary_op;
9757 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9759 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9760 goto return_build_binary_op;
9762 else
9763 common = 1;
9764 break;
9766 case MINUS_EXPR:
9767 /* Subtraction of two similar pointers.
9768 We must subtract them as integers, then divide by object size. */
9769 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9770 && comp_target_types (location, type0, type1))
9772 ret = pointer_diff (location, op0, op1);
9773 goto return_build_binary_op;
9775 /* Handle pointer minus int. Just like pointer plus int. */
9776 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9778 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9779 goto return_build_binary_op;
9781 else
9782 common = 1;
9783 break;
9785 case MULT_EXPR:
9786 common = 1;
9787 break;
9789 case TRUNC_DIV_EXPR:
9790 case CEIL_DIV_EXPR:
9791 case FLOOR_DIV_EXPR:
9792 case ROUND_DIV_EXPR:
9793 case EXACT_DIV_EXPR:
9794 warn_for_div_by_zero (location, op1);
9796 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9797 || code0 == FIXED_POINT_TYPE
9798 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9799 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9800 || code1 == FIXED_POINT_TYPE
9801 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9803 enum tree_code tcode0 = code0, tcode1 = code1;
9805 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9806 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9807 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9808 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9810 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9811 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9812 resultcode = RDIV_EXPR;
9813 else
9814 /* Although it would be tempting to shorten always here, that
9815 loses on some targets, since the modulo instruction is
9816 undefined if the quotient can't be represented in the
9817 computation mode. We shorten only if unsigned or if
9818 dividing by something we know != -1. */
9819 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9820 || (TREE_CODE (op1) == INTEGER_CST
9821 && !integer_all_onesp (op1)));
9822 common = 1;
9824 break;
9826 case BIT_AND_EXPR:
9827 case BIT_IOR_EXPR:
9828 case BIT_XOR_EXPR:
9829 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9830 shorten = -1;
9831 /* Allow vector types which are not floating point types. */
9832 else if (code0 == VECTOR_TYPE
9833 && code1 == VECTOR_TYPE
9834 && !VECTOR_FLOAT_TYPE_P (type0)
9835 && !VECTOR_FLOAT_TYPE_P (type1))
9836 common = 1;
9837 break;
9839 case TRUNC_MOD_EXPR:
9840 case FLOOR_MOD_EXPR:
9841 warn_for_div_by_zero (location, op1);
9843 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9844 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9845 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9846 common = 1;
9847 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9849 /* Although it would be tempting to shorten always here, that loses
9850 on some targets, since the modulo instruction is undefined if the
9851 quotient can't be represented in the computation mode. We shorten
9852 only if unsigned or if dividing by something we know != -1. */
9853 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9854 || (TREE_CODE (op1) == INTEGER_CST
9855 && !integer_all_onesp (op1)));
9856 common = 1;
9858 break;
9860 case TRUTH_ANDIF_EXPR:
9861 case TRUTH_ORIF_EXPR:
9862 case TRUTH_AND_EXPR:
9863 case TRUTH_OR_EXPR:
9864 case TRUTH_XOR_EXPR:
9865 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9866 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9867 || code0 == FIXED_POINT_TYPE)
9868 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9869 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9870 || code1 == FIXED_POINT_TYPE))
9872 /* Result of these operations is always an int,
9873 but that does not mean the operands should be
9874 converted to ints! */
9875 result_type = integer_type_node;
9876 op0 = c_common_truthvalue_conversion (location, op0);
9877 op1 = c_common_truthvalue_conversion (location, op1);
9878 converted = 1;
9879 boolean_op = true;
9881 if (code == TRUTH_ANDIF_EXPR)
9883 int_const_or_overflow = (int_operands
9884 && TREE_CODE (orig_op0) == INTEGER_CST
9885 && (op0 == truthvalue_false_node
9886 || TREE_CODE (orig_op1) == INTEGER_CST));
9887 int_const = (int_const_or_overflow
9888 && !TREE_OVERFLOW (orig_op0)
9889 && (op0 == truthvalue_false_node
9890 || !TREE_OVERFLOW (orig_op1)));
9892 else if (code == TRUTH_ORIF_EXPR)
9894 int_const_or_overflow = (int_operands
9895 && TREE_CODE (orig_op0) == INTEGER_CST
9896 && (op0 == truthvalue_true_node
9897 || TREE_CODE (orig_op1) == INTEGER_CST));
9898 int_const = (int_const_or_overflow
9899 && !TREE_OVERFLOW (orig_op0)
9900 && (op0 == truthvalue_true_node
9901 || !TREE_OVERFLOW (orig_op1)));
9903 break;
9905 /* Shift operations: result has same type as first operand;
9906 always convert second operand to int.
9907 Also set SHORT_SHIFT if shifting rightward. */
9909 case RSHIFT_EXPR:
9910 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9911 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9913 result_type = type0;
9914 converted = 1;
9916 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9917 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9918 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9919 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9921 result_type = type0;
9922 converted = 1;
9924 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9925 && code1 == INTEGER_TYPE)
9927 if (TREE_CODE (op1) == INTEGER_CST)
9929 if (tree_int_cst_sgn (op1) < 0)
9931 int_const = false;
9932 if (c_inhibit_evaluation_warnings == 0)
9933 warning (0, "right shift count is negative");
9935 else
9937 if (!integer_zerop (op1))
9938 short_shift = 1;
9940 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9942 int_const = false;
9943 if (c_inhibit_evaluation_warnings == 0)
9944 warning (0, "right shift count >= width of type");
9949 /* Use the type of the value to be shifted. */
9950 result_type = type0;
9951 /* Convert the non vector shift-count to an integer, regardless
9952 of size of value being shifted. */
9953 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9954 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9955 op1 = convert (integer_type_node, op1);
9956 /* Avoid converting op1 to result_type later. */
9957 converted = 1;
9959 break;
9961 case LSHIFT_EXPR:
9962 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9963 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9965 result_type = type0;
9966 converted = 1;
9968 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9969 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9970 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9971 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9973 result_type = type0;
9974 converted = 1;
9976 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9977 && code1 == INTEGER_TYPE)
9979 if (TREE_CODE (op1) == INTEGER_CST)
9981 if (tree_int_cst_sgn (op1) < 0)
9983 int_const = false;
9984 if (c_inhibit_evaluation_warnings == 0)
9985 warning (0, "left shift count is negative");
9988 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9990 int_const = false;
9991 if (c_inhibit_evaluation_warnings == 0)
9992 warning (0, "left shift count >= width of type");
9996 /* Use the type of the value to be shifted. */
9997 result_type = type0;
9998 /* Convert the non vector shift-count to an integer, regardless
9999 of size of value being shifted. */
10000 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10001 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10002 op1 = convert (integer_type_node, op1);
10003 /* Avoid converting op1 to result_type later. */
10004 converted = 1;
10006 break;
10008 case EQ_EXPR:
10009 case NE_EXPR:
10010 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10012 tree intt;
10013 if (TREE_TYPE (type0) != TREE_TYPE (type1))
10015 error_at (location, "comparing vectors with different "
10016 "element types");
10017 return error_mark_node;
10020 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10022 error_at (location, "comparing vectors with different "
10023 "number of elements");
10024 return error_mark_node;
10027 /* Always construct signed integer vector type. */
10028 intt = c_common_type_for_size (GET_MODE_BITSIZE
10029 (TYPE_MODE (TREE_TYPE (type0))), 0);
10030 result_type = build_opaque_vector_type (intt,
10031 TYPE_VECTOR_SUBPARTS (type0));
10032 converted = 1;
10033 break;
10035 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10036 warning_at (location,
10037 OPT_Wfloat_equal,
10038 "comparing floating point with == or != is unsafe");
10039 /* Result of comparison is always int,
10040 but don't convert the args to int! */
10041 build_type = integer_type_node;
10042 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10043 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10044 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10045 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10046 short_compare = 1;
10047 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10049 if (TREE_CODE (op0) == ADDR_EXPR
10050 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10052 if (code == EQ_EXPR)
10053 warning_at (location,
10054 OPT_Waddress,
10055 "the comparison will always evaluate as %<false%> "
10056 "for the address of %qD will never be NULL",
10057 TREE_OPERAND (op0, 0));
10058 else
10059 warning_at (location,
10060 OPT_Waddress,
10061 "the comparison will always evaluate as %<true%> "
10062 "for the address of %qD will never be NULL",
10063 TREE_OPERAND (op0, 0));
10065 result_type = type0;
10067 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10069 if (TREE_CODE (op1) == ADDR_EXPR
10070 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10072 if (code == EQ_EXPR)
10073 warning_at (location,
10074 OPT_Waddress,
10075 "the comparison will always evaluate as %<false%> "
10076 "for the address of %qD will never be NULL",
10077 TREE_OPERAND (op1, 0));
10078 else
10079 warning_at (location,
10080 OPT_Waddress,
10081 "the comparison will always evaluate as %<true%> "
10082 "for the address of %qD will never be NULL",
10083 TREE_OPERAND (op1, 0));
10085 result_type = type1;
10087 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10089 tree tt0 = TREE_TYPE (type0);
10090 tree tt1 = TREE_TYPE (type1);
10091 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10092 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10093 addr_space_t as_common = ADDR_SPACE_GENERIC;
10095 /* Anything compares with void *. void * compares with anything.
10096 Otherwise, the targets must be compatible
10097 and both must be object or both incomplete. */
10098 if (comp_target_types (location, type0, type1))
10099 result_type = common_pointer_type (type0, type1);
10100 else if (!addr_space_superset (as0, as1, &as_common))
10102 error_at (location, "comparison of pointers to "
10103 "disjoint address spaces");
10104 return error_mark_node;
10106 else if (VOID_TYPE_P (tt0))
10108 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10109 pedwarn (location, OPT_pedantic, "ISO C forbids "
10110 "comparison of %<void *%> with function pointer");
10112 else if (VOID_TYPE_P (tt1))
10114 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10115 pedwarn (location, OPT_pedantic, "ISO C forbids "
10116 "comparison of %<void *%> with function pointer");
10118 else
10119 /* Avoid warning about the volatile ObjC EH puts on decls. */
10120 if (!objc_ok)
10121 pedwarn (location, 0,
10122 "comparison of distinct pointer types lacks a cast");
10124 if (result_type == NULL_TREE)
10126 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10127 result_type = build_pointer_type
10128 (build_qualified_type (void_type_node, qual));
10131 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10133 result_type = type0;
10134 pedwarn (location, 0, "comparison between pointer and integer");
10136 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10138 result_type = type1;
10139 pedwarn (location, 0, "comparison between pointer and integer");
10141 break;
10143 case LE_EXPR:
10144 case GE_EXPR:
10145 case LT_EXPR:
10146 case GT_EXPR:
10147 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10149 tree intt;
10150 if (TREE_TYPE (type0) != TREE_TYPE (type1))
10152 error_at (location, "comparing vectors with different "
10153 "element types");
10154 return error_mark_node;
10157 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10159 error_at (location, "comparing vectors with different "
10160 "number of elements");
10161 return error_mark_node;
10164 /* Always construct signed integer vector type. */
10165 intt = c_common_type_for_size (GET_MODE_BITSIZE
10166 (TYPE_MODE (TREE_TYPE (type0))), 0);
10167 result_type = build_opaque_vector_type (intt,
10168 TYPE_VECTOR_SUBPARTS (type0));
10169 converted = 1;
10170 break;
10172 build_type = integer_type_node;
10173 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10174 || code0 == FIXED_POINT_TYPE)
10175 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10176 || code1 == FIXED_POINT_TYPE))
10177 short_compare = 1;
10178 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10180 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10181 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10182 addr_space_t as_common;
10184 if (comp_target_types (location, type0, type1))
10186 result_type = common_pointer_type (type0, type1);
10187 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10188 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10189 pedwarn (location, 0,
10190 "comparison of complete and incomplete pointers");
10191 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10192 pedwarn (location, OPT_pedantic, "ISO C forbids "
10193 "ordered comparisons of pointers to functions");
10194 else if (null_pointer_constant_p (orig_op0)
10195 || null_pointer_constant_p (orig_op1))
10196 warning_at (location, OPT_Wextra,
10197 "ordered comparison of pointer with null pointer");
10200 else if (!addr_space_superset (as0, as1, &as_common))
10202 error_at (location, "comparison of pointers to "
10203 "disjoint address spaces");
10204 return error_mark_node;
10206 else
10208 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10209 result_type = build_pointer_type
10210 (build_qualified_type (void_type_node, qual));
10211 pedwarn (location, 0,
10212 "comparison of distinct pointer types lacks a cast");
10215 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10217 result_type = type0;
10218 if (pedantic)
10219 pedwarn (location, OPT_pedantic,
10220 "ordered comparison of pointer with integer zero");
10221 else if (extra_warnings)
10222 warning_at (location, OPT_Wextra,
10223 "ordered comparison of pointer with integer zero");
10225 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10227 result_type = type1;
10228 if (pedantic)
10229 pedwarn (location, OPT_pedantic,
10230 "ordered comparison of pointer with integer zero");
10231 else if (extra_warnings)
10232 warning_at (location, OPT_Wextra,
10233 "ordered comparison of pointer with integer zero");
10235 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10237 result_type = type0;
10238 pedwarn (location, 0, "comparison between pointer and integer");
10240 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10242 result_type = type1;
10243 pedwarn (location, 0, "comparison between pointer and integer");
10245 break;
10247 default:
10248 gcc_unreachable ();
10251 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10252 return error_mark_node;
10254 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10255 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10256 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10257 TREE_TYPE (type1))))
10259 binary_op_error (location, code, type0, type1);
10260 return error_mark_node;
10263 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10264 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10266 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10267 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10269 bool first_complex = (code0 == COMPLEX_TYPE);
10270 bool second_complex = (code1 == COMPLEX_TYPE);
10271 int none_complex = (!first_complex && !second_complex);
10273 if (shorten || common || short_compare)
10275 result_type = c_common_type (type0, type1);
10276 do_warn_double_promotion (result_type, type0, type1,
10277 "implicit conversion from %qT to %qT "
10278 "to match other operand of binary "
10279 "expression",
10280 location);
10281 if (result_type == error_mark_node)
10282 return error_mark_node;
10285 if (first_complex != second_complex
10286 && (code == PLUS_EXPR
10287 || code == MINUS_EXPR
10288 || code == MULT_EXPR
10289 || (code == TRUNC_DIV_EXPR && first_complex))
10290 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10291 && flag_signed_zeros)
10293 /* An operation on mixed real/complex operands must be
10294 handled specially, but the language-independent code can
10295 more easily optimize the plain complex arithmetic if
10296 -fno-signed-zeros. */
10297 tree real_type = TREE_TYPE (result_type);
10298 tree real, imag;
10299 if (type0 != orig_type0 || type1 != orig_type1)
10301 gcc_assert (may_need_excess_precision && common);
10302 semantic_result_type = c_common_type (orig_type0, orig_type1);
10304 if (first_complex)
10306 if (TREE_TYPE (op0) != result_type)
10307 op0 = convert_and_check (result_type, op0);
10308 if (TREE_TYPE (op1) != real_type)
10309 op1 = convert_and_check (real_type, op1);
10311 else
10313 if (TREE_TYPE (op0) != real_type)
10314 op0 = convert_and_check (real_type, op0);
10315 if (TREE_TYPE (op1) != result_type)
10316 op1 = convert_and_check (result_type, op1);
10318 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10319 return error_mark_node;
10320 if (first_complex)
10322 op0 = c_save_expr (op0);
10323 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10324 op0, 1);
10325 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10326 op0, 1);
10327 switch (code)
10329 case MULT_EXPR:
10330 case TRUNC_DIV_EXPR:
10331 op1 = c_save_expr (op1);
10332 imag = build2 (resultcode, real_type, imag, op1);
10333 /* Fall through. */
10334 case PLUS_EXPR:
10335 case MINUS_EXPR:
10336 real = build2 (resultcode, real_type, real, op1);
10337 break;
10338 default:
10339 gcc_unreachable();
10342 else
10344 op1 = c_save_expr (op1);
10345 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10346 op1, 1);
10347 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10348 op1, 1);
10349 switch (code)
10351 case MULT_EXPR:
10352 op0 = c_save_expr (op0);
10353 imag = build2 (resultcode, real_type, op0, imag);
10354 /* Fall through. */
10355 case PLUS_EXPR:
10356 real = build2 (resultcode, real_type, op0, real);
10357 break;
10358 case MINUS_EXPR:
10359 real = build2 (resultcode, real_type, op0, real);
10360 imag = build1 (NEGATE_EXPR, real_type, imag);
10361 break;
10362 default:
10363 gcc_unreachable();
10366 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10367 goto return_build_binary_op;
10370 /* For certain operations (which identify themselves by shorten != 0)
10371 if both args were extended from the same smaller type,
10372 do the arithmetic in that type and then extend.
10374 shorten !=0 and !=1 indicates a bitwise operation.
10375 For them, this optimization is safe only if
10376 both args are zero-extended or both are sign-extended.
10377 Otherwise, we might change the result.
10378 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10379 but calculated in (unsigned short) it would be (unsigned short)-1. */
10381 if (shorten && none_complex)
10383 final_type = result_type;
10384 result_type = shorten_binary_op (result_type, op0, op1,
10385 shorten == -1);
10388 /* Shifts can be shortened if shifting right. */
10390 if (short_shift)
10392 int unsigned_arg;
10393 tree arg0 = get_narrower (op0, &unsigned_arg);
10395 final_type = result_type;
10397 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10398 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10400 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10401 && tree_int_cst_sgn (op1) > 0
10402 /* We can shorten only if the shift count is less than the
10403 number of bits in the smaller type size. */
10404 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10405 /* We cannot drop an unsigned shift after sign-extension. */
10406 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10408 /* Do an unsigned shift if the operand was zero-extended. */
10409 result_type
10410 = c_common_signed_or_unsigned_type (unsigned_arg,
10411 TREE_TYPE (arg0));
10412 /* Convert value-to-be-shifted to that type. */
10413 if (TREE_TYPE (op0) != result_type)
10414 op0 = convert (result_type, op0);
10415 converted = 1;
10419 /* Comparison operations are shortened too but differently.
10420 They identify themselves by setting short_compare = 1. */
10422 if (short_compare)
10424 /* Don't write &op0, etc., because that would prevent op0
10425 from being kept in a register.
10426 Instead, make copies of the our local variables and
10427 pass the copies by reference, then copy them back afterward. */
10428 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10429 enum tree_code xresultcode = resultcode;
10430 tree val
10431 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10433 if (val != 0)
10435 ret = val;
10436 goto return_build_binary_op;
10439 op0 = xop0, op1 = xop1;
10440 converted = 1;
10441 resultcode = xresultcode;
10443 if (c_inhibit_evaluation_warnings == 0)
10445 bool op0_maybe_const = true;
10446 bool op1_maybe_const = true;
10447 tree orig_op0_folded, orig_op1_folded;
10449 if (in_late_binary_op)
10451 orig_op0_folded = orig_op0;
10452 orig_op1_folded = orig_op1;
10454 else
10456 /* Fold for the sake of possible warnings, as in
10457 build_conditional_expr. This requires the
10458 "original" values to be folded, not just op0 and
10459 op1. */
10460 c_inhibit_evaluation_warnings++;
10461 op0 = c_fully_fold (op0, require_constant_value,
10462 &op0_maybe_const);
10463 op1 = c_fully_fold (op1, require_constant_value,
10464 &op1_maybe_const);
10465 c_inhibit_evaluation_warnings--;
10466 orig_op0_folded = c_fully_fold (orig_op0,
10467 require_constant_value,
10468 NULL);
10469 orig_op1_folded = c_fully_fold (orig_op1,
10470 require_constant_value,
10471 NULL);
10474 if (warn_sign_compare)
10475 warn_for_sign_compare (location, orig_op0_folded,
10476 orig_op1_folded, op0, op1,
10477 result_type, resultcode);
10478 if (!in_late_binary_op && !int_operands)
10480 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10481 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10482 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10483 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10489 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10490 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10491 Then the expression will be built.
10492 It will be given type FINAL_TYPE if that is nonzero;
10493 otherwise, it will be given type RESULT_TYPE. */
10495 if (!result_type)
10497 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10498 return error_mark_node;
10501 if (build_type == NULL_TREE)
10503 build_type = result_type;
10504 if ((type0 != orig_type0 || type1 != orig_type1)
10505 && !boolean_op)
10507 gcc_assert (may_need_excess_precision && common);
10508 semantic_result_type = c_common_type (orig_type0, orig_type1);
10512 if (!converted)
10514 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10515 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10517 /* This can happen if one operand has a vector type, and the other
10518 has a different type. */
10519 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10520 return error_mark_node;
10523 /* Treat expressions in initializers specially as they can't trap. */
10524 if (int_const_or_overflow)
10525 ret = (require_constant_value
10526 ? fold_build2_initializer_loc (location, resultcode, build_type,
10527 op0, op1)
10528 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10529 else
10530 ret = build2 (resultcode, build_type, op0, op1);
10531 if (final_type != 0)
10532 ret = convert (final_type, ret);
10534 return_build_binary_op:
10535 gcc_assert (ret != error_mark_node);
10536 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10537 ret = (int_operands
10538 ? note_integer_operands (ret)
10539 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10540 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10541 && !in_late_binary_op)
10542 ret = note_integer_operands (ret);
10543 if (semantic_result_type)
10544 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10545 protected_set_expr_location (ret, location);
10546 return ret;
10550 /* Convert EXPR to be a truth-value, validating its type for this
10551 purpose. LOCATION is the source location for the expression. */
10553 tree
10554 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10556 bool int_const, int_operands;
10558 switch (TREE_CODE (TREE_TYPE (expr)))
10560 case ARRAY_TYPE:
10561 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10562 return error_mark_node;
10564 case RECORD_TYPE:
10565 error_at (location, "used struct type value where scalar is required");
10566 return error_mark_node;
10568 case UNION_TYPE:
10569 error_at (location, "used union type value where scalar is required");
10570 return error_mark_node;
10572 case VOID_TYPE:
10573 error_at (location, "void value not ignored as it ought to be");
10574 return error_mark_node;
10576 case FUNCTION_TYPE:
10577 gcc_unreachable ();
10579 case VECTOR_TYPE:
10580 error_at (location, "used vector type where scalar is required");
10581 return error_mark_node;
10583 default:
10584 break;
10587 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10588 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10589 if (int_operands)
10590 expr = remove_c_maybe_const_expr (expr);
10592 /* ??? Should we also give an error for vectors rather than leaving
10593 those to give errors later? */
10594 expr = c_common_truthvalue_conversion (location, expr);
10596 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10598 if (TREE_OVERFLOW (expr))
10599 return expr;
10600 else
10601 return note_integer_operands (expr);
10603 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10604 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10605 return expr;
10609 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10610 required. */
10612 tree
10613 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10615 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10617 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10618 /* Executing a compound literal inside a function reinitializes
10619 it. */
10620 if (!TREE_STATIC (decl))
10621 *se = true;
10622 return decl;
10624 else
10625 return expr;
10628 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10630 tree
10631 c_begin_omp_parallel (void)
10633 tree block;
10635 keep_next_level ();
10636 block = c_begin_compound_stmt (true);
10638 return block;
10641 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10642 statement. LOC is the location of the OMP_PARALLEL. */
10644 tree
10645 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10647 tree stmt;
10649 block = c_end_compound_stmt (loc, block, true);
10651 stmt = make_node (OMP_PARALLEL);
10652 TREE_TYPE (stmt) = void_type_node;
10653 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10654 OMP_PARALLEL_BODY (stmt) = block;
10655 SET_EXPR_LOCATION (stmt, loc);
10657 return add_stmt (stmt);
10660 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10662 tree
10663 c_begin_omp_task (void)
10665 tree block;
10667 keep_next_level ();
10668 block = c_begin_compound_stmt (true);
10670 return block;
10673 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10674 statement. LOC is the location of the #pragma. */
10676 tree
10677 c_finish_omp_task (location_t loc, tree clauses, tree block)
10679 tree stmt;
10681 block = c_end_compound_stmt (loc, block, true);
10683 stmt = make_node (OMP_TASK);
10684 TREE_TYPE (stmt) = void_type_node;
10685 OMP_TASK_CLAUSES (stmt) = clauses;
10686 OMP_TASK_BODY (stmt) = block;
10687 SET_EXPR_LOCATION (stmt, loc);
10689 return add_stmt (stmt);
10692 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10693 Remove any elements from the list that are invalid. */
10695 tree
10696 c_finish_omp_clauses (tree clauses)
10698 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10699 tree c, t, *pc = &clauses;
10700 const char *name;
10702 bitmap_obstack_initialize (NULL);
10703 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10704 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10705 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10707 for (pc = &clauses, c = clauses; c ; c = *pc)
10709 bool remove = false;
10710 bool need_complete = false;
10711 bool need_implicitly_determined = false;
10713 switch (OMP_CLAUSE_CODE (c))
10715 case OMP_CLAUSE_SHARED:
10716 name = "shared";
10717 need_implicitly_determined = true;
10718 goto check_dup_generic;
10720 case OMP_CLAUSE_PRIVATE:
10721 name = "private";
10722 need_complete = true;
10723 need_implicitly_determined = true;
10724 goto check_dup_generic;
10726 case OMP_CLAUSE_REDUCTION:
10727 name = "reduction";
10728 need_implicitly_determined = true;
10729 t = OMP_CLAUSE_DECL (c);
10730 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10731 || POINTER_TYPE_P (TREE_TYPE (t)))
10733 error_at (OMP_CLAUSE_LOCATION (c),
10734 "%qE has invalid type for %<reduction%>", t);
10735 remove = true;
10737 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10739 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10740 const char *r_name = NULL;
10742 switch (r_code)
10744 case PLUS_EXPR:
10745 case MULT_EXPR:
10746 case MINUS_EXPR:
10747 case MIN_EXPR:
10748 case MAX_EXPR:
10749 break;
10750 case BIT_AND_EXPR:
10751 r_name = "&";
10752 break;
10753 case BIT_XOR_EXPR:
10754 r_name = "^";
10755 break;
10756 case BIT_IOR_EXPR:
10757 r_name = "|";
10758 break;
10759 case TRUTH_ANDIF_EXPR:
10760 r_name = "&&";
10761 break;
10762 case TRUTH_ORIF_EXPR:
10763 r_name = "||";
10764 break;
10765 default:
10766 gcc_unreachable ();
10768 if (r_name)
10770 error_at (OMP_CLAUSE_LOCATION (c),
10771 "%qE has invalid type for %<reduction(%s)%>",
10772 t, r_name);
10773 remove = true;
10776 goto check_dup_generic;
10778 case OMP_CLAUSE_COPYPRIVATE:
10779 name = "copyprivate";
10780 goto check_dup_generic;
10782 case OMP_CLAUSE_COPYIN:
10783 name = "copyin";
10784 t = OMP_CLAUSE_DECL (c);
10785 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10787 error_at (OMP_CLAUSE_LOCATION (c),
10788 "%qE must be %<threadprivate%> for %<copyin%>", t);
10789 remove = true;
10791 goto check_dup_generic;
10793 check_dup_generic:
10794 t = OMP_CLAUSE_DECL (c);
10795 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10797 error_at (OMP_CLAUSE_LOCATION (c),
10798 "%qE is not a variable in clause %qs", t, name);
10799 remove = true;
10801 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10802 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10803 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10805 error_at (OMP_CLAUSE_LOCATION (c),
10806 "%qE appears more than once in data clauses", t);
10807 remove = true;
10809 else
10810 bitmap_set_bit (&generic_head, DECL_UID (t));
10811 break;
10813 case OMP_CLAUSE_FIRSTPRIVATE:
10814 name = "firstprivate";
10815 t = OMP_CLAUSE_DECL (c);
10816 need_complete = true;
10817 need_implicitly_determined = true;
10818 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10820 error_at (OMP_CLAUSE_LOCATION (c),
10821 "%qE is not a variable in clause %<firstprivate%>", t);
10822 remove = true;
10824 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10825 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10827 error_at (OMP_CLAUSE_LOCATION (c),
10828 "%qE appears more than once in data clauses", t);
10829 remove = true;
10831 else
10832 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10833 break;
10835 case OMP_CLAUSE_LASTPRIVATE:
10836 name = "lastprivate";
10837 t = OMP_CLAUSE_DECL (c);
10838 need_complete = true;
10839 need_implicitly_determined = true;
10840 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10842 error_at (OMP_CLAUSE_LOCATION (c),
10843 "%qE is not a variable in clause %<lastprivate%>", t);
10844 remove = true;
10846 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10847 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10849 error_at (OMP_CLAUSE_LOCATION (c),
10850 "%qE appears more than once in data clauses", t);
10851 remove = true;
10853 else
10854 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10855 break;
10857 case OMP_CLAUSE_IF:
10858 case OMP_CLAUSE_NUM_THREADS:
10859 case OMP_CLAUSE_SCHEDULE:
10860 case OMP_CLAUSE_NOWAIT:
10861 case OMP_CLAUSE_ORDERED:
10862 case OMP_CLAUSE_DEFAULT:
10863 case OMP_CLAUSE_UNTIED:
10864 case OMP_CLAUSE_COLLAPSE:
10865 case OMP_CLAUSE_FINAL:
10866 case OMP_CLAUSE_MERGEABLE:
10867 pc = &OMP_CLAUSE_CHAIN (c);
10868 continue;
10870 default:
10871 gcc_unreachable ();
10874 if (!remove)
10876 t = OMP_CLAUSE_DECL (c);
10878 if (need_complete)
10880 t = require_complete_type (t);
10881 if (t == error_mark_node)
10882 remove = true;
10885 if (need_implicitly_determined)
10887 const char *share_name = NULL;
10889 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10890 share_name = "threadprivate";
10891 else switch (c_omp_predetermined_sharing (t))
10893 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10894 break;
10895 case OMP_CLAUSE_DEFAULT_SHARED:
10896 /* const vars may be specified in firstprivate clause. */
10897 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10898 && TREE_READONLY (t))
10899 break;
10900 share_name = "shared";
10901 break;
10902 case OMP_CLAUSE_DEFAULT_PRIVATE:
10903 share_name = "private";
10904 break;
10905 default:
10906 gcc_unreachable ();
10908 if (share_name)
10910 error_at (OMP_CLAUSE_LOCATION (c),
10911 "%qE is predetermined %qs for %qs",
10912 t, share_name, name);
10913 remove = true;
10918 if (remove)
10919 *pc = OMP_CLAUSE_CHAIN (c);
10920 else
10921 pc = &OMP_CLAUSE_CHAIN (c);
10924 bitmap_obstack_release (NULL);
10925 return clauses;
10928 /* Create a transaction node. */
10930 tree
10931 c_finish_transaction (location_t loc, tree block, int flags)
10933 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
10934 if (flags & TM_STMT_ATTR_OUTER)
10935 TRANSACTION_EXPR_OUTER (stmt) = 1;
10936 if (flags & TM_STMT_ATTR_RELAXED)
10937 TRANSACTION_EXPR_RELAXED (stmt) = 1;
10938 return add_stmt (stmt);
10941 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10942 down to the element type of an array. */
10944 tree
10945 c_build_qualified_type (tree type, int type_quals)
10947 if (type == error_mark_node)
10948 return type;
10950 if (TREE_CODE (type) == ARRAY_TYPE)
10952 tree t;
10953 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10954 type_quals);
10956 /* See if we already have an identically qualified type. */
10957 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10959 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10960 && TYPE_NAME (t) == TYPE_NAME (type)
10961 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10962 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10963 TYPE_ATTRIBUTES (type)))
10964 break;
10966 if (!t)
10968 tree domain = TYPE_DOMAIN (type);
10970 t = build_variant_type_copy (type);
10971 TREE_TYPE (t) = element_type;
10973 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10974 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10975 SET_TYPE_STRUCTURAL_EQUALITY (t);
10976 else if (TYPE_CANONICAL (element_type) != element_type
10977 || (domain && TYPE_CANONICAL (domain) != domain))
10979 tree unqualified_canon
10980 = build_array_type (TYPE_CANONICAL (element_type),
10981 domain? TYPE_CANONICAL (domain)
10982 : NULL_TREE);
10983 TYPE_CANONICAL (t)
10984 = c_build_qualified_type (unqualified_canon, type_quals);
10986 else
10987 TYPE_CANONICAL (t) = t;
10989 return t;
10992 /* A restrict-qualified pointer type must be a pointer to object or
10993 incomplete type. Note that the use of POINTER_TYPE_P also allows
10994 REFERENCE_TYPEs, which is appropriate for C++. */
10995 if ((type_quals & TYPE_QUAL_RESTRICT)
10996 && (!POINTER_TYPE_P (type)
10997 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10999 error ("invalid use of %<restrict%>");
11000 type_quals &= ~TYPE_QUAL_RESTRICT;
11003 return build_qualified_type (type, type_quals);
11006 /* Build a VA_ARG_EXPR for the C parser. */
11008 tree
11009 c_build_va_arg (location_t loc, tree expr, tree type)
11011 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
11012 warning_at (loc, OPT_Wc___compat,
11013 "C++ requires promoted type, not enum type, in %<va_arg%>");
11014 return build_va_arg (loc, expr, type);