Fix compilation of build/*.o when using C++.
[official-gcc.git] / gcc / c-typeck.c
blobd3fa68e711738d2fa40e7227ba462f72b4a7b209
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_for_mode (target, TYPE_MODE (t1), false);
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_Wpedantic,
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_Wpedantic,
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 C11 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_Wpedantic, "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 /* In C++ array compound literals are temporary objects unless they are
1789 const or appear in namespace scope, so they are destroyed too soon
1790 to use them for much of anything (c++/53220). */
1791 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1793 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1794 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1795 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1796 "converting an array compound literal to a pointer "
1797 "is ill-formed in C++");
1800 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1801 return convert (ptrtype, adr);
1804 /* Convert the function expression EXP to a pointer. */
1805 static tree
1806 function_to_pointer_conversion (location_t loc, tree exp)
1808 tree orig_exp = exp;
1810 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1812 STRIP_TYPE_NOPS (exp);
1814 if (TREE_NO_WARNING (orig_exp))
1815 TREE_NO_WARNING (exp) = 1;
1817 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1820 /* Mark EXP as read, not just set, for set but not used -Wunused
1821 warning purposes. */
1823 void
1824 mark_exp_read (tree exp)
1826 switch (TREE_CODE (exp))
1828 case VAR_DECL:
1829 case PARM_DECL:
1830 DECL_READ_P (exp) = 1;
1831 break;
1832 case ARRAY_REF:
1833 case COMPONENT_REF:
1834 case MODIFY_EXPR:
1835 case REALPART_EXPR:
1836 case IMAGPART_EXPR:
1837 CASE_CONVERT:
1838 case ADDR_EXPR:
1839 mark_exp_read (TREE_OPERAND (exp, 0));
1840 break;
1841 case COMPOUND_EXPR:
1842 case C_MAYBE_CONST_EXPR:
1843 mark_exp_read (TREE_OPERAND (exp, 1));
1844 break;
1845 default:
1846 break;
1850 /* Perform the default conversion of arrays and functions to pointers.
1851 Return the result of converting EXP. For any other expression, just
1852 return EXP.
1854 LOC is the location of the expression. */
1856 struct c_expr
1857 default_function_array_conversion (location_t loc, struct c_expr exp)
1859 tree orig_exp = exp.value;
1860 tree type = TREE_TYPE (exp.value);
1861 enum tree_code code = TREE_CODE (type);
1863 switch (code)
1865 case ARRAY_TYPE:
1867 bool not_lvalue = false;
1868 bool lvalue_array_p;
1870 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1871 || CONVERT_EXPR_P (exp.value))
1872 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1874 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1875 not_lvalue = true;
1876 exp.value = TREE_OPERAND (exp.value, 0);
1879 if (TREE_NO_WARNING (orig_exp))
1880 TREE_NO_WARNING (exp.value) = 1;
1882 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1883 if (!flag_isoc99 && !lvalue_array_p)
1885 /* Before C99, non-lvalue arrays do not decay to pointers.
1886 Normally, using such an array would be invalid; but it can
1887 be used correctly inside sizeof or as a statement expression.
1888 Thus, do not give an error here; an error will result later. */
1889 return exp;
1892 exp.value = array_to_pointer_conversion (loc, exp.value);
1894 break;
1895 case FUNCTION_TYPE:
1896 exp.value = function_to_pointer_conversion (loc, exp.value);
1897 break;
1898 default:
1899 break;
1902 return exp;
1905 struct c_expr
1906 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1908 mark_exp_read (exp.value);
1909 return default_function_array_conversion (loc, exp);
1912 /* EXP is an expression of integer type. Apply the integer promotions
1913 to it and return the promoted value. */
1915 tree
1916 perform_integral_promotions (tree exp)
1918 tree type = TREE_TYPE (exp);
1919 enum tree_code code = TREE_CODE (type);
1921 gcc_assert (INTEGRAL_TYPE_P (type));
1923 /* Normally convert enums to int,
1924 but convert wide enums to something wider. */
1925 if (code == ENUMERAL_TYPE)
1927 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1928 TYPE_PRECISION (integer_type_node)),
1929 ((TYPE_PRECISION (type)
1930 >= TYPE_PRECISION (integer_type_node))
1931 && TYPE_UNSIGNED (type)));
1933 return convert (type, exp);
1936 /* ??? This should no longer be needed now bit-fields have their
1937 proper types. */
1938 if (TREE_CODE (exp) == COMPONENT_REF
1939 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1940 /* If it's thinner than an int, promote it like a
1941 c_promoting_integer_type_p, otherwise leave it alone. */
1942 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1943 TYPE_PRECISION (integer_type_node)))
1944 return convert (integer_type_node, exp);
1946 if (c_promoting_integer_type_p (type))
1948 /* Preserve unsignedness if not really getting any wider. */
1949 if (TYPE_UNSIGNED (type)
1950 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1951 return convert (unsigned_type_node, exp);
1953 return convert (integer_type_node, exp);
1956 return exp;
1960 /* Perform default promotions for C data used in expressions.
1961 Enumeral types or short or char are converted to int.
1962 In addition, manifest constants symbols are replaced by their values. */
1964 tree
1965 default_conversion (tree exp)
1967 tree orig_exp;
1968 tree type = TREE_TYPE (exp);
1969 enum tree_code code = TREE_CODE (type);
1970 tree promoted_type;
1972 mark_exp_read (exp);
1974 /* Functions and arrays have been converted during parsing. */
1975 gcc_assert (code != FUNCTION_TYPE);
1976 if (code == ARRAY_TYPE)
1977 return exp;
1979 /* Constants can be used directly unless they're not loadable. */
1980 if (TREE_CODE (exp) == CONST_DECL)
1981 exp = DECL_INITIAL (exp);
1983 /* Strip no-op conversions. */
1984 orig_exp = exp;
1985 STRIP_TYPE_NOPS (exp);
1987 if (TREE_NO_WARNING (orig_exp))
1988 TREE_NO_WARNING (exp) = 1;
1990 if (code == VOID_TYPE)
1992 error ("void value not ignored as it ought to be");
1993 return error_mark_node;
1996 exp = require_complete_type (exp);
1997 if (exp == error_mark_node)
1998 return error_mark_node;
2000 promoted_type = targetm.promoted_type (type);
2001 if (promoted_type)
2002 return convert (promoted_type, exp);
2004 if (INTEGRAL_TYPE_P (type))
2005 return perform_integral_promotions (exp);
2007 return exp;
2010 /* Look up COMPONENT in a structure or union TYPE.
2012 If the component name is not found, returns NULL_TREE. Otherwise,
2013 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2014 stepping down the chain to the component, which is in the last
2015 TREE_VALUE of the list. Normally the list is of length one, but if
2016 the component is embedded within (nested) anonymous structures or
2017 unions, the list steps down the chain to the component. */
2019 static tree
2020 lookup_field (tree type, tree component)
2022 tree field;
2024 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2025 to the field elements. Use a binary search on this array to quickly
2026 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2027 will always be set for structures which have many elements. */
2029 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2031 int bot, top, half;
2032 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2034 field = TYPE_FIELDS (type);
2035 bot = 0;
2036 top = TYPE_LANG_SPECIFIC (type)->s->len;
2037 while (top - bot > 1)
2039 half = (top - bot + 1) >> 1;
2040 field = field_array[bot+half];
2042 if (DECL_NAME (field) == NULL_TREE)
2044 /* Step through all anon unions in linear fashion. */
2045 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2047 field = field_array[bot++];
2048 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2049 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2051 tree anon = lookup_field (TREE_TYPE (field), component);
2053 if (anon)
2054 return tree_cons (NULL_TREE, field, anon);
2056 /* The Plan 9 compiler permits referring
2057 directly to an anonymous struct/union field
2058 using a typedef name. */
2059 if (flag_plan9_extensions
2060 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2061 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2062 == TYPE_DECL)
2063 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2064 == component))
2065 break;
2069 /* Entire record is only anon unions. */
2070 if (bot > top)
2071 return NULL_TREE;
2073 /* Restart the binary search, with new lower bound. */
2074 continue;
2077 if (DECL_NAME (field) == component)
2078 break;
2079 if (DECL_NAME (field) < component)
2080 bot += half;
2081 else
2082 top = bot + half;
2085 if (DECL_NAME (field_array[bot]) == component)
2086 field = field_array[bot];
2087 else if (DECL_NAME (field) != component)
2088 return NULL_TREE;
2090 else
2092 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2094 if (DECL_NAME (field) == NULL_TREE
2095 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2096 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2098 tree anon = lookup_field (TREE_TYPE (field), component);
2100 if (anon)
2101 return tree_cons (NULL_TREE, field, anon);
2103 /* The Plan 9 compiler permits referring directly to an
2104 anonymous struct/union field using a typedef
2105 name. */
2106 if (flag_plan9_extensions
2107 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2108 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2109 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2110 == component))
2111 break;
2114 if (DECL_NAME (field) == component)
2115 break;
2118 if (field == NULL_TREE)
2119 return NULL_TREE;
2122 return tree_cons (NULL_TREE, field, NULL_TREE);
2125 /* Make an expression to refer to the COMPONENT field of structure or
2126 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2127 location of the COMPONENT_REF. */
2129 tree
2130 build_component_ref (location_t loc, tree datum, tree component)
2132 tree type = TREE_TYPE (datum);
2133 enum tree_code code = TREE_CODE (type);
2134 tree field = NULL;
2135 tree ref;
2136 bool datum_lvalue = lvalue_p (datum);
2138 if (!objc_is_public (datum, component))
2139 return error_mark_node;
2141 /* Detect Objective-C property syntax object.property. */
2142 if (c_dialect_objc ()
2143 && (ref = objc_maybe_build_component_ref (datum, component)))
2144 return ref;
2146 /* See if there is a field or component with name COMPONENT. */
2148 if (code == RECORD_TYPE || code == UNION_TYPE)
2150 if (!COMPLETE_TYPE_P (type))
2152 c_incomplete_type_error (NULL_TREE, type);
2153 return error_mark_node;
2156 field = lookup_field (type, component);
2158 if (!field)
2160 error_at (loc, "%qT has no member named %qE", type, component);
2161 return error_mark_node;
2164 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2165 This might be better solved in future the way the C++ front
2166 end does it - by giving the anonymous entities each a
2167 separate name and type, and then have build_component_ref
2168 recursively call itself. We can't do that here. */
2171 tree subdatum = TREE_VALUE (field);
2172 int quals;
2173 tree subtype;
2174 bool use_datum_quals;
2176 if (TREE_TYPE (subdatum) == error_mark_node)
2177 return error_mark_node;
2179 /* If this is an rvalue, it does not have qualifiers in C
2180 standard terms and we must avoid propagating such
2181 qualifiers down to a non-lvalue array that is then
2182 converted to a pointer. */
2183 use_datum_quals = (datum_lvalue
2184 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2186 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2187 if (use_datum_quals)
2188 quals |= TYPE_QUALS (TREE_TYPE (datum));
2189 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2191 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2192 NULL_TREE);
2193 SET_EXPR_LOCATION (ref, loc);
2194 if (TREE_READONLY (subdatum)
2195 || (use_datum_quals && TREE_READONLY (datum)))
2196 TREE_READONLY (ref) = 1;
2197 if (TREE_THIS_VOLATILE (subdatum)
2198 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2199 TREE_THIS_VOLATILE (ref) = 1;
2201 if (TREE_DEPRECATED (subdatum))
2202 warn_deprecated_use (subdatum, NULL_TREE);
2204 datum = ref;
2206 field = TREE_CHAIN (field);
2208 while (field);
2210 return ref;
2212 else if (code != ERROR_MARK)
2213 error_at (loc,
2214 "request for member %qE in something not a structure or union",
2215 component);
2217 return error_mark_node;
2220 /* Given an expression PTR for a pointer, return an expression
2221 for the value pointed to.
2222 ERRORSTRING is the name of the operator to appear in error messages.
2224 LOC is the location to use for the generated tree. */
2226 tree
2227 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2229 tree pointer = default_conversion (ptr);
2230 tree type = TREE_TYPE (pointer);
2231 tree ref;
2233 if (TREE_CODE (type) == POINTER_TYPE)
2235 if (CONVERT_EXPR_P (pointer)
2236 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2238 /* If a warning is issued, mark it to avoid duplicates from
2239 the backend. This only needs to be done at
2240 warn_strict_aliasing > 2. */
2241 if (warn_strict_aliasing > 2)
2242 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2243 type, TREE_OPERAND (pointer, 0)))
2244 TREE_NO_WARNING (pointer) = 1;
2247 if (TREE_CODE (pointer) == ADDR_EXPR
2248 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2249 == TREE_TYPE (type)))
2251 ref = TREE_OPERAND (pointer, 0);
2252 protected_set_expr_location (ref, loc);
2253 return ref;
2255 else
2257 tree t = TREE_TYPE (type);
2259 ref = build1 (INDIRECT_REF, t, pointer);
2261 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2263 error_at (loc, "dereferencing pointer to incomplete type");
2264 return error_mark_node;
2266 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2267 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2269 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2270 so that we get the proper error message if the result is used
2271 to assign to. Also, &* is supposed to be a no-op.
2272 And ANSI C seems to specify that the type of the result
2273 should be the const type. */
2274 /* A de-reference of a pointer to const is not a const. It is valid
2275 to change it via some other pointer. */
2276 TREE_READONLY (ref) = TYPE_READONLY (t);
2277 TREE_SIDE_EFFECTS (ref)
2278 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2279 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2280 protected_set_expr_location (ref, loc);
2281 return ref;
2284 else if (TREE_CODE (pointer) != ERROR_MARK)
2285 invalid_indirection_error (loc, type, errstring);
2287 return error_mark_node;
2290 /* This handles expressions of the form "a[i]", which denotes
2291 an array reference.
2293 This is logically equivalent in C to *(a+i), but we may do it differently.
2294 If A is a variable or a member, we generate a primitive ARRAY_REF.
2295 This avoids forcing the array out of registers, and can work on
2296 arrays that are not lvalues (for example, members of structures returned
2297 by functions).
2299 For vector types, allow vector[i] but not i[vector], and create
2300 *(((type*)&vectortype) + i) for the expression.
2302 LOC is the location to use for the returned expression. */
2304 tree
2305 build_array_ref (location_t loc, tree array, tree index)
2307 tree ret;
2308 bool swapped = false;
2309 if (TREE_TYPE (array) == error_mark_node
2310 || TREE_TYPE (index) == error_mark_node)
2311 return error_mark_node;
2313 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2314 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2315 /* Allow vector[index] but not index[vector]. */
2316 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2318 tree temp;
2319 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2320 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2322 error_at (loc,
2323 "subscripted value is neither array nor pointer nor vector");
2325 return error_mark_node;
2327 temp = array;
2328 array = index;
2329 index = temp;
2330 swapped = true;
2333 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2335 error_at (loc, "array subscript is not an integer");
2336 return error_mark_node;
2339 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2341 error_at (loc, "subscripted value is pointer to function");
2342 return error_mark_node;
2345 /* ??? Existing practice has been to warn only when the char
2346 index is syntactically the index, not for char[array]. */
2347 if (!swapped)
2348 warn_array_subscript_with_type_char (index);
2350 /* Apply default promotions *after* noticing character types. */
2351 index = default_conversion (index);
2353 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2355 convert_vector_to_pointer_for_subscript (loc, &array, index);
2357 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2359 tree rval, type;
2361 /* An array that is indexed by a non-constant
2362 cannot be stored in a register; we must be able to do
2363 address arithmetic on its address.
2364 Likewise an array of elements of variable size. */
2365 if (TREE_CODE (index) != INTEGER_CST
2366 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2367 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2369 if (!c_mark_addressable (array))
2370 return error_mark_node;
2372 /* An array that is indexed by a constant value which is not within
2373 the array bounds cannot be stored in a register either; because we
2374 would get a crash in store_bit_field/extract_bit_field when trying
2375 to access a non-existent part of the register. */
2376 if (TREE_CODE (index) == INTEGER_CST
2377 && TYPE_DOMAIN (TREE_TYPE (array))
2378 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2380 if (!c_mark_addressable (array))
2381 return error_mark_node;
2384 if (pedantic)
2386 tree foo = array;
2387 while (TREE_CODE (foo) == COMPONENT_REF)
2388 foo = TREE_OPERAND (foo, 0);
2389 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2390 pedwarn (loc, OPT_Wpedantic,
2391 "ISO C forbids subscripting %<register%> array");
2392 else if (!flag_isoc99 && !lvalue_p (foo))
2393 pedwarn (loc, OPT_Wpedantic,
2394 "ISO C90 forbids subscripting non-lvalue array");
2397 type = TREE_TYPE (TREE_TYPE (array));
2398 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2399 /* Array ref is const/volatile if the array elements are
2400 or if the array is. */
2401 TREE_READONLY (rval)
2402 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2403 | TREE_READONLY (array));
2404 TREE_SIDE_EFFECTS (rval)
2405 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2406 | TREE_SIDE_EFFECTS (array));
2407 TREE_THIS_VOLATILE (rval)
2408 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2409 /* This was added by rms on 16 Nov 91.
2410 It fixes vol struct foo *a; a->elts[1]
2411 in an inline function.
2412 Hope it doesn't break something else. */
2413 | TREE_THIS_VOLATILE (array));
2414 ret = require_complete_type (rval);
2415 protected_set_expr_location (ret, loc);
2416 return ret;
2418 else
2420 tree ar = default_conversion (array);
2422 if (ar == error_mark_node)
2423 return ar;
2425 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2426 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2428 return build_indirect_ref
2429 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2430 RO_ARRAY_INDEXING);
2434 /* Build an external reference to identifier ID. FUN indicates
2435 whether this will be used for a function call. LOC is the source
2436 location of the identifier. This sets *TYPE to the type of the
2437 identifier, which is not the same as the type of the returned value
2438 for CONST_DECLs defined as enum constants. If the type of the
2439 identifier is not available, *TYPE is set to NULL. */
2440 tree
2441 build_external_ref (location_t loc, tree id, int fun, tree *type)
2443 tree ref;
2444 tree decl = lookup_name (id);
2446 /* In Objective-C, an instance variable (ivar) may be preferred to
2447 whatever lookup_name() found. */
2448 decl = objc_lookup_ivar (decl, id);
2450 *type = NULL;
2451 if (decl && decl != error_mark_node)
2453 ref = decl;
2454 *type = TREE_TYPE (ref);
2456 else if (fun)
2457 /* Implicit function declaration. */
2458 ref = implicitly_declare (loc, id);
2459 else if (decl == error_mark_node)
2460 /* Don't complain about something that's already been
2461 complained about. */
2462 return error_mark_node;
2463 else
2465 undeclared_variable (loc, id);
2466 return error_mark_node;
2469 if (TREE_TYPE (ref) == error_mark_node)
2470 return error_mark_node;
2472 if (TREE_DEPRECATED (ref))
2473 warn_deprecated_use (ref, NULL_TREE);
2475 /* Recursive call does not count as usage. */
2476 if (ref != current_function_decl)
2478 TREE_USED (ref) = 1;
2481 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2483 if (!in_sizeof && !in_typeof)
2484 C_DECL_USED (ref) = 1;
2485 else if (DECL_INITIAL (ref) == 0
2486 && DECL_EXTERNAL (ref)
2487 && !TREE_PUBLIC (ref))
2488 record_maybe_used_decl (ref);
2491 if (TREE_CODE (ref) == CONST_DECL)
2493 used_types_insert (TREE_TYPE (ref));
2495 if (warn_cxx_compat
2496 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2497 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2499 warning_at (loc, OPT_Wc___compat,
2500 ("enum constant defined in struct or union "
2501 "is not visible in C++"));
2502 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2505 ref = DECL_INITIAL (ref);
2506 TREE_CONSTANT (ref) = 1;
2508 else if (current_function_decl != 0
2509 && !DECL_FILE_SCOPE_P (current_function_decl)
2510 && (TREE_CODE (ref) == VAR_DECL
2511 || TREE_CODE (ref) == PARM_DECL
2512 || TREE_CODE (ref) == FUNCTION_DECL))
2514 tree context = decl_function_context (ref);
2516 if (context != 0 && context != current_function_decl)
2517 DECL_NONLOCAL (ref) = 1;
2519 /* C99 6.7.4p3: An inline definition of a function with external
2520 linkage ... shall not contain a reference to an identifier with
2521 internal linkage. */
2522 else if (current_function_decl != 0
2523 && DECL_DECLARED_INLINE_P (current_function_decl)
2524 && DECL_EXTERNAL (current_function_decl)
2525 && VAR_OR_FUNCTION_DECL_P (ref)
2526 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2527 && ! TREE_PUBLIC (ref)
2528 && DECL_CONTEXT (ref) != current_function_decl)
2529 record_inline_static (loc, current_function_decl, ref,
2530 csi_internal);
2532 return ref;
2535 /* Record details of decls possibly used inside sizeof or typeof. */
2536 struct maybe_used_decl
2538 /* The decl. */
2539 tree decl;
2540 /* The level seen at (in_sizeof + in_typeof). */
2541 int level;
2542 /* The next one at this level or above, or NULL. */
2543 struct maybe_used_decl *next;
2546 static struct maybe_used_decl *maybe_used_decls;
2548 /* Record that DECL, an undefined static function reference seen
2549 inside sizeof or typeof, might be used if the operand of sizeof is
2550 a VLA type or the operand of typeof is a variably modified
2551 type. */
2553 static void
2554 record_maybe_used_decl (tree decl)
2556 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2557 t->decl = decl;
2558 t->level = in_sizeof + in_typeof;
2559 t->next = maybe_used_decls;
2560 maybe_used_decls = t;
2563 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2564 USED is false, just discard them. If it is true, mark them used
2565 (if no longer inside sizeof or typeof) or move them to the next
2566 level up (if still inside sizeof or typeof). */
2568 void
2569 pop_maybe_used (bool used)
2571 struct maybe_used_decl *p = maybe_used_decls;
2572 int cur_level = in_sizeof + in_typeof;
2573 while (p && p->level > cur_level)
2575 if (used)
2577 if (cur_level == 0)
2578 C_DECL_USED (p->decl) = 1;
2579 else
2580 p->level = cur_level;
2582 p = p->next;
2584 if (!used || cur_level == 0)
2585 maybe_used_decls = p;
2588 /* Return the result of sizeof applied to EXPR. */
2590 struct c_expr
2591 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2593 struct c_expr ret;
2594 if (expr.value == error_mark_node)
2596 ret.value = error_mark_node;
2597 ret.original_code = ERROR_MARK;
2598 ret.original_type = NULL;
2599 pop_maybe_used (false);
2601 else
2603 bool expr_const_operands = true;
2604 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2605 &expr_const_operands);
2606 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2607 ret.original_code = ERROR_MARK;
2608 ret.original_type = NULL;
2609 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2611 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2612 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2613 folded_expr, ret.value);
2614 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2615 SET_EXPR_LOCATION (ret.value, loc);
2617 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2619 return ret;
2622 /* Return the result of sizeof applied to T, a structure for the type
2623 name passed to sizeof (rather than the type itself). LOC is the
2624 location of the original expression. */
2626 struct c_expr
2627 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2629 tree type;
2630 struct c_expr ret;
2631 tree type_expr = NULL_TREE;
2632 bool type_expr_const = true;
2633 type = groktypename (t, &type_expr, &type_expr_const);
2634 ret.value = c_sizeof (loc, type);
2635 ret.original_code = ERROR_MARK;
2636 ret.original_type = NULL;
2637 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2638 && c_vla_type_p (type))
2640 /* If the type is a [*] array, it is a VLA but is represented as
2641 having a size of zero. In such a case we must ensure that
2642 the result of sizeof does not get folded to a constant by
2643 c_fully_fold, because if the size is evaluated the result is
2644 not constant and so constraints on zero or negative size
2645 arrays must not be applied when this sizeof call is inside
2646 another array declarator. */
2647 if (!type_expr)
2648 type_expr = integer_zero_node;
2649 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2650 type_expr, ret.value);
2651 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2653 pop_maybe_used (type != error_mark_node
2654 ? C_TYPE_VARIABLE_SIZE (type) : false);
2655 return ret;
2658 /* Build a function call to function FUNCTION with parameters PARAMS.
2659 The function call is at LOC.
2660 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2661 TREE_VALUE of each node is a parameter-expression.
2662 FUNCTION's data type may be a function type or a pointer-to-function. */
2664 tree
2665 build_function_call (location_t loc, tree function, tree params)
2667 VEC(tree,gc) *vec;
2668 tree ret;
2670 vec = VEC_alloc (tree, gc, list_length (params));
2671 for (; params; params = TREE_CHAIN (params))
2672 VEC_quick_push (tree, vec, TREE_VALUE (params));
2673 ret = build_function_call_vec (loc, function, vec, NULL);
2674 VEC_free (tree, gc, vec);
2675 return ret;
2678 /* Give a note about the location of the declaration of DECL. */
2680 static void inform_declaration (tree decl)
2682 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2683 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2686 /* Build a function call to function FUNCTION with parameters PARAMS.
2687 ORIGTYPES, if not NULL, is a vector of types; each element is
2688 either NULL or the original type of the corresponding element in
2689 PARAMS. The original type may differ from TREE_TYPE of the
2690 parameter for enums. FUNCTION's data type may be a function type
2691 or pointer-to-function. This function changes the elements of
2692 PARAMS. */
2694 tree
2695 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2696 VEC(tree,gc) *origtypes)
2698 tree fntype, fundecl = 0;
2699 tree name = NULL_TREE, result;
2700 tree tem;
2701 int nargs;
2702 tree *argarray;
2705 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2706 STRIP_TYPE_NOPS (function);
2708 /* Convert anything with function type to a pointer-to-function. */
2709 if (TREE_CODE (function) == FUNCTION_DECL)
2711 /* Implement type-directed function overloading for builtins.
2712 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2713 handle all the type checking. The result is a complete expression
2714 that implements this function call. */
2715 tem = resolve_overloaded_builtin (loc, function, params);
2716 if (tem)
2717 return tem;
2719 name = DECL_NAME (function);
2721 if (flag_tm)
2722 tm_malloc_replacement (function);
2723 fundecl = function;
2724 /* Atomic functions have type checking/casting already done. They are
2725 often rewritten and don't match the original parameter list. */
2726 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2727 origtypes = NULL;
2729 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2730 function = function_to_pointer_conversion (loc, function);
2732 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2733 expressions, like those used for ObjC messenger dispatches. */
2734 if (!VEC_empty (tree, params))
2735 function = objc_rewrite_function_call (function,
2736 VEC_index (tree, params, 0));
2738 function = c_fully_fold (function, false, NULL);
2740 fntype = TREE_TYPE (function);
2742 if (TREE_CODE (fntype) == ERROR_MARK)
2743 return error_mark_node;
2745 if (!(TREE_CODE (fntype) == POINTER_TYPE
2746 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2748 if (!flag_diagnostics_show_caret)
2749 error_at (loc,
2750 "called object %qE is not a function or function pointer",
2751 function);
2752 else if (DECL_P (function))
2754 error_at (loc,
2755 "called object %qD is not a function or function pointer",
2756 function);
2757 inform_declaration (function);
2759 else
2760 error_at (loc,
2761 "called object is not a function or function pointer");
2762 return error_mark_node;
2765 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2766 current_function_returns_abnormally = 1;
2768 /* fntype now gets the type of function pointed to. */
2769 fntype = TREE_TYPE (fntype);
2771 /* Convert the parameters to the types declared in the
2772 function prototype, or apply default promotions. */
2774 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2775 function, fundecl);
2776 if (nargs < 0)
2777 return error_mark_node;
2779 /* Check that the function is called through a compatible prototype.
2780 If it is not, replace the call by a trap, wrapped up in a compound
2781 expression if necessary. This has the nice side-effect to prevent
2782 the tree-inliner from generating invalid assignment trees which may
2783 blow up in the RTL expander later. */
2784 if (CONVERT_EXPR_P (function)
2785 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2786 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2787 && !comptypes (fntype, TREE_TYPE (tem)))
2789 tree return_type = TREE_TYPE (fntype);
2790 tree trap = build_function_call (loc,
2791 builtin_decl_explicit (BUILT_IN_TRAP),
2792 NULL_TREE);
2793 int i;
2795 /* This situation leads to run-time undefined behavior. We can't,
2796 therefore, simply error unless we can prove that all possible
2797 executions of the program must execute the code. */
2798 if (warning_at (loc, 0, "function called through a non-compatible type"))
2799 /* We can, however, treat "undefined" any way we please.
2800 Call abort to encourage the user to fix the program. */
2801 inform (loc, "if this code is reached, the program will abort");
2802 /* Before the abort, allow the function arguments to exit or
2803 call longjmp. */
2804 for (i = 0; i < nargs; i++)
2805 trap = build2 (COMPOUND_EXPR, void_type_node,
2806 VEC_index (tree, params, i), trap);
2808 if (VOID_TYPE_P (return_type))
2810 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2811 pedwarn (loc, 0,
2812 "function with qualified void return type called");
2813 return trap;
2815 else
2817 tree rhs;
2819 if (AGGREGATE_TYPE_P (return_type))
2820 rhs = build_compound_literal (loc, return_type,
2821 build_constructor (return_type, 0),
2822 false);
2823 else
2824 rhs = build_zero_cst (return_type);
2826 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2827 trap, rhs));
2831 argarray = VEC_address (tree, params);
2833 /* Check that arguments to builtin functions match the expectations. */
2834 if (fundecl
2835 && DECL_BUILT_IN (fundecl)
2836 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2837 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2838 return error_mark_node;
2840 /* Check that the arguments to the function are valid. */
2841 check_function_arguments (fntype, nargs, argarray);
2843 if (name != NULL_TREE
2844 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2846 if (require_constant_value)
2847 result =
2848 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2849 function, nargs, argarray);
2850 else
2851 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2852 function, nargs, argarray);
2853 if (TREE_CODE (result) == NOP_EXPR
2854 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2855 STRIP_TYPE_NOPS (result);
2857 else
2858 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2859 function, nargs, argarray);
2861 if (VOID_TYPE_P (TREE_TYPE (result)))
2863 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2864 pedwarn (loc, 0,
2865 "function with qualified void return type called");
2866 return result;
2868 return require_complete_type (result);
2871 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2872 and have vector types, V0 has the same type as V1, and the number of
2873 elements of V0, V1, MASK is the same.
2875 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2876 called with two arguments. In this case implementation passes the
2877 first argument twice in order to share the same tree code. This fact
2878 could enable the mask-values being twice the vector length. This is
2879 an implementation accident and this semantics is not guaranteed to
2880 the user. */
2881 tree
2882 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2884 tree ret;
2885 bool wrap = true;
2886 bool maybe_const = false;
2887 bool two_arguments = false;
2889 if (v1 == NULL_TREE)
2891 two_arguments = true;
2892 v1 = v0;
2895 if (v0 == error_mark_node || v1 == error_mark_node
2896 || mask == error_mark_node)
2897 return error_mark_node;
2899 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2900 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2902 error_at (loc, "__builtin_shuffle last argument must "
2903 "be an integer vector");
2904 return error_mark_node;
2907 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2908 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2910 error_at (loc, "__builtin_shuffle arguments must be vectors");
2911 return error_mark_node;
2914 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2916 error_at (loc, "__builtin_shuffle argument vectors must be of "
2917 "the same type");
2918 return error_mark_node;
2921 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2922 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2923 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2924 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2926 error_at (loc, "__builtin_shuffle number of elements of the "
2927 "argument vector(s) and the mask vector should "
2928 "be the same");
2929 return error_mark_node;
2932 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2933 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2935 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2936 "must have the same size as inner type of the mask");
2937 return error_mark_node;
2940 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2941 v0 = c_fully_fold (v0, false, &maybe_const);
2942 wrap &= maybe_const;
2944 if (two_arguments)
2945 v1 = v0 = save_expr (v0);
2946 else
2948 v1 = c_fully_fold (v1, false, &maybe_const);
2949 wrap &= maybe_const;
2952 mask = c_fully_fold (mask, false, &maybe_const);
2953 wrap &= maybe_const;
2955 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2957 if (!wrap)
2958 ret = c_wrap_maybe_const (ret, true);
2960 return ret;
2963 /* Convert the argument expressions in the vector VALUES
2964 to the types in the list TYPELIST.
2966 If TYPELIST is exhausted, or when an element has NULL as its type,
2967 perform the default conversions.
2969 ORIGTYPES is the original types of the expressions in VALUES. This
2970 holds the type of enum values which have been converted to integral
2971 types. It may be NULL.
2973 FUNCTION is a tree for the called function. It is used only for
2974 error messages, where it is formatted with %qE.
2976 This is also where warnings about wrong number of args are generated.
2978 Returns the actual number of arguments processed (which may be less
2979 than the length of VALUES in some error situations), or -1 on
2980 failure. */
2982 static int
2983 convert_arguments (tree typelist, VEC(tree,gc) *values,
2984 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2986 tree typetail, val;
2987 unsigned int parmnum;
2988 bool error_args = false;
2989 const bool type_generic = fundecl
2990 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2991 bool type_generic_remove_excess_precision = false;
2992 tree selector;
2994 /* Change pointer to function to the function itself for
2995 diagnostics. */
2996 if (TREE_CODE (function) == ADDR_EXPR
2997 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2998 function = TREE_OPERAND (function, 0);
3000 /* Handle an ObjC selector specially for diagnostics. */
3001 selector = objc_message_selector ();
3003 /* For type-generic built-in functions, determine whether excess
3004 precision should be removed (classification) or not
3005 (comparison). */
3006 if (type_generic
3007 && DECL_BUILT_IN (fundecl)
3008 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3010 switch (DECL_FUNCTION_CODE (fundecl))
3012 case BUILT_IN_ISFINITE:
3013 case BUILT_IN_ISINF:
3014 case BUILT_IN_ISINF_SIGN:
3015 case BUILT_IN_ISNAN:
3016 case BUILT_IN_ISNORMAL:
3017 case BUILT_IN_FPCLASSIFY:
3018 type_generic_remove_excess_precision = true;
3019 break;
3021 default:
3022 type_generic_remove_excess_precision = false;
3023 break;
3027 /* Scan the given expressions and types, producing individual
3028 converted arguments. */
3030 for (typetail = typelist, parmnum = 0;
3031 VEC_iterate (tree, values, parmnum, val);
3032 ++parmnum)
3034 tree type = typetail ? TREE_VALUE (typetail) : 0;
3035 tree valtype = TREE_TYPE (val);
3036 tree rname = function;
3037 int argnum = parmnum + 1;
3038 const char *invalid_func_diag;
3039 bool excess_precision = false;
3040 bool npc;
3041 tree parmval;
3043 if (type == void_type_node)
3045 if (selector)
3046 error_at (input_location,
3047 "too many arguments to method %qE", selector);
3048 else
3049 error_at (input_location,
3050 "too many arguments to function %qE", function);
3051 inform_declaration (fundecl);
3052 return parmnum;
3055 if (selector && argnum > 2)
3057 rname = selector;
3058 argnum -= 2;
3061 npc = null_pointer_constant_p (val);
3063 /* If there is excess precision and a prototype, convert once to
3064 the required type rather than converting via the semantic
3065 type. Likewise without a prototype a float value represented
3066 as long double should be converted once to double. But for
3067 type-generic classification functions excess precision must
3068 be removed here. */
3069 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3070 && (type || !type_generic || !type_generic_remove_excess_precision))
3072 val = TREE_OPERAND (val, 0);
3073 excess_precision = true;
3075 val = c_fully_fold (val, false, NULL);
3076 STRIP_TYPE_NOPS (val);
3078 val = require_complete_type (val);
3080 if (type != 0)
3082 /* Formal parm type is specified by a function prototype. */
3084 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3086 error ("type of formal parameter %d is incomplete", parmnum + 1);
3087 parmval = val;
3089 else
3091 tree origtype;
3093 /* Optionally warn about conversions that
3094 differ from the default conversions. */
3095 if (warn_traditional_conversion || warn_traditional)
3097 unsigned int formal_prec = TYPE_PRECISION (type);
3099 if (INTEGRAL_TYPE_P (type)
3100 && TREE_CODE (valtype) == REAL_TYPE)
3101 warning (0, "passing argument %d of %qE as integer "
3102 "rather than floating due to prototype",
3103 argnum, rname);
3104 if (INTEGRAL_TYPE_P (type)
3105 && TREE_CODE (valtype) == COMPLEX_TYPE)
3106 warning (0, "passing argument %d of %qE as integer "
3107 "rather than complex due to prototype",
3108 argnum, rname);
3109 else if (TREE_CODE (type) == COMPLEX_TYPE
3110 && TREE_CODE (valtype) == REAL_TYPE)
3111 warning (0, "passing argument %d of %qE as complex "
3112 "rather than floating due to prototype",
3113 argnum, rname);
3114 else if (TREE_CODE (type) == REAL_TYPE
3115 && INTEGRAL_TYPE_P (valtype))
3116 warning (0, "passing argument %d of %qE as floating "
3117 "rather than integer due to prototype",
3118 argnum, rname);
3119 else if (TREE_CODE (type) == COMPLEX_TYPE
3120 && INTEGRAL_TYPE_P (valtype))
3121 warning (0, "passing argument %d of %qE as complex "
3122 "rather than integer due to prototype",
3123 argnum, rname);
3124 else if (TREE_CODE (type) == REAL_TYPE
3125 && TREE_CODE (valtype) == COMPLEX_TYPE)
3126 warning (0, "passing argument %d of %qE as floating "
3127 "rather than complex due to prototype",
3128 argnum, rname);
3129 /* ??? At some point, messages should be written about
3130 conversions between complex types, but that's too messy
3131 to do now. */
3132 else if (TREE_CODE (type) == REAL_TYPE
3133 && TREE_CODE (valtype) == REAL_TYPE)
3135 /* Warn if any argument is passed as `float',
3136 since without a prototype it would be `double'. */
3137 if (formal_prec == TYPE_PRECISION (float_type_node)
3138 && type != dfloat32_type_node)
3139 warning (0, "passing argument %d of %qE as %<float%> "
3140 "rather than %<double%> due to prototype",
3141 argnum, rname);
3143 /* Warn if mismatch between argument and prototype
3144 for decimal float types. Warn of conversions with
3145 binary float types and of precision narrowing due to
3146 prototype. */
3147 else if (type != valtype
3148 && (type == dfloat32_type_node
3149 || type == dfloat64_type_node
3150 || type == dfloat128_type_node
3151 || valtype == dfloat32_type_node
3152 || valtype == dfloat64_type_node
3153 || valtype == dfloat128_type_node)
3154 && (formal_prec
3155 <= TYPE_PRECISION (valtype)
3156 || (type == dfloat128_type_node
3157 && (valtype
3158 != dfloat64_type_node
3159 && (valtype
3160 != dfloat32_type_node)))
3161 || (type == dfloat64_type_node
3162 && (valtype
3163 != dfloat32_type_node))))
3164 warning (0, "passing argument %d of %qE as %qT "
3165 "rather than %qT due to prototype",
3166 argnum, rname, type, valtype);
3169 /* Detect integer changing in width or signedness.
3170 These warnings are only activated with
3171 -Wtraditional-conversion, not with -Wtraditional. */
3172 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3173 && INTEGRAL_TYPE_P (valtype))
3175 tree would_have_been = default_conversion (val);
3176 tree type1 = TREE_TYPE (would_have_been);
3178 if (TREE_CODE (type) == ENUMERAL_TYPE
3179 && (TYPE_MAIN_VARIANT (type)
3180 == TYPE_MAIN_VARIANT (valtype)))
3181 /* No warning if function asks for enum
3182 and the actual arg is that enum type. */
3184 else if (formal_prec != TYPE_PRECISION (type1))
3185 warning (OPT_Wtraditional_conversion,
3186 "passing argument %d of %qE "
3187 "with different width due to prototype",
3188 argnum, rname);
3189 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3191 /* Don't complain if the formal parameter type
3192 is an enum, because we can't tell now whether
3193 the value was an enum--even the same enum. */
3194 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3196 else if (TREE_CODE (val) == INTEGER_CST
3197 && int_fits_type_p (val, type))
3198 /* Change in signedness doesn't matter
3199 if a constant value is unaffected. */
3201 /* If the value is extended from a narrower
3202 unsigned type, it doesn't matter whether we
3203 pass it as signed or unsigned; the value
3204 certainly is the same either way. */
3205 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3206 && TYPE_UNSIGNED (valtype))
3208 else if (TYPE_UNSIGNED (type))
3209 warning (OPT_Wtraditional_conversion,
3210 "passing argument %d of %qE "
3211 "as unsigned due to prototype",
3212 argnum, rname);
3213 else
3214 warning (OPT_Wtraditional_conversion,
3215 "passing argument %d of %qE "
3216 "as signed due to prototype", argnum, rname);
3220 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3221 sake of better warnings from convert_and_check. */
3222 if (excess_precision)
3223 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3224 origtype = (origtypes == NULL
3225 ? NULL_TREE
3226 : VEC_index (tree, origtypes, parmnum));
3227 parmval = convert_for_assignment (input_location, type, val,
3228 origtype, ic_argpass, npc,
3229 fundecl, function,
3230 parmnum + 1);
3232 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3233 && INTEGRAL_TYPE_P (type)
3234 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3235 parmval = default_conversion (parmval);
3238 else if (TREE_CODE (valtype) == REAL_TYPE
3239 && (TYPE_PRECISION (valtype)
3240 < TYPE_PRECISION (double_type_node))
3241 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3243 if (type_generic)
3244 parmval = val;
3245 else
3247 /* Convert `float' to `double'. */
3248 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3249 warning (OPT_Wdouble_promotion,
3250 "implicit conversion from %qT to %qT when passing "
3251 "argument to function",
3252 valtype, double_type_node);
3253 parmval = convert (double_type_node, val);
3256 else if (excess_precision && !type_generic)
3257 /* A "double" argument with excess precision being passed
3258 without a prototype or in variable arguments. */
3259 parmval = convert (valtype, val);
3260 else if ((invalid_func_diag =
3261 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3263 error (invalid_func_diag);
3264 return -1;
3266 else
3267 /* Convert `short' and `char' to full-size `int'. */
3268 parmval = default_conversion (val);
3270 VEC_replace (tree, values, parmnum, parmval);
3271 if (parmval == error_mark_node)
3272 error_args = true;
3274 if (typetail)
3275 typetail = TREE_CHAIN (typetail);
3278 gcc_assert (parmnum == VEC_length (tree, values));
3280 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3282 error_at (input_location,
3283 "too few arguments to function %qE", function);
3284 inform_declaration (fundecl);
3285 return -1;
3288 return error_args ? -1 : (int) parmnum;
3291 /* This is the entry point used by the parser to build unary operators
3292 in the input. CODE, a tree_code, specifies the unary operator, and
3293 ARG is the operand. For unary plus, the C parser currently uses
3294 CONVERT_EXPR for code.
3296 LOC is the location to use for the tree generated.
3299 struct c_expr
3300 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3302 struct c_expr result;
3304 result.value = build_unary_op (loc, code, arg.value, 0);
3305 result.original_code = code;
3306 result.original_type = NULL;
3308 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3309 overflow_warning (loc, result.value);
3311 return result;
3314 /* This is the entry point used by the parser to build binary operators
3315 in the input. CODE, a tree_code, specifies the binary operator, and
3316 ARG1 and ARG2 are the operands. In addition to constructing the
3317 expression, we check for operands that were written with other binary
3318 operators in a way that is likely to confuse the user.
3320 LOCATION is the location of the binary operator. */
3322 struct c_expr
3323 parser_build_binary_op (location_t location, enum tree_code code,
3324 struct c_expr arg1, struct c_expr arg2)
3326 struct c_expr result;
3328 enum tree_code code1 = arg1.original_code;
3329 enum tree_code code2 = arg2.original_code;
3330 tree type1 = (arg1.original_type
3331 ? arg1.original_type
3332 : TREE_TYPE (arg1.value));
3333 tree type2 = (arg2.original_type
3334 ? arg2.original_type
3335 : TREE_TYPE (arg2.value));
3337 result.value = build_binary_op (location, code,
3338 arg1.value, arg2.value, 1);
3339 result.original_code = code;
3340 result.original_type = NULL;
3342 if (TREE_CODE (result.value) == ERROR_MARK)
3343 return result;
3345 if (location != UNKNOWN_LOCATION)
3346 protected_set_expr_location (result.value, location);
3348 /* Check for cases such as x+y<<z which users are likely
3349 to misinterpret. */
3350 if (warn_parentheses)
3351 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3353 if (warn_logical_op)
3354 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3355 code1, arg1.value, code2, arg2.value);
3357 /* Warn about comparisons against string literals, with the exception
3358 of testing for equality or inequality of a string literal with NULL. */
3359 if (code == EQ_EXPR || code == NE_EXPR)
3361 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3362 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3363 warning_at (location, OPT_Waddress,
3364 "comparison with string literal results in unspecified behavior");
3366 else if (TREE_CODE_CLASS (code) == tcc_comparison
3367 && (code1 == STRING_CST || code2 == STRING_CST))
3368 warning_at (location, OPT_Waddress,
3369 "comparison with string literal results in unspecified behavior");
3371 if (TREE_OVERFLOW_P (result.value)
3372 && !TREE_OVERFLOW_P (arg1.value)
3373 && !TREE_OVERFLOW_P (arg2.value))
3374 overflow_warning (location, result.value);
3376 /* Warn about comparisons of different enum types. */
3377 if (warn_enum_compare
3378 && TREE_CODE_CLASS (code) == tcc_comparison
3379 && TREE_CODE (type1) == ENUMERAL_TYPE
3380 && TREE_CODE (type2) == ENUMERAL_TYPE
3381 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3382 warning_at (location, OPT_Wenum_compare,
3383 "comparison between %qT and %qT",
3384 type1, type2);
3386 return result;
3389 /* Return a tree for the difference of pointers OP0 and OP1.
3390 The resulting tree has type int. */
3392 static tree
3393 pointer_diff (location_t loc, tree op0, tree op1)
3395 tree restype = ptrdiff_type_node;
3396 tree result, inttype;
3398 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3399 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3400 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3401 tree con0, con1, lit0, lit1;
3402 tree orig_op1 = op1;
3404 /* If the operands point into different address spaces, we need to
3405 explicitly convert them to pointers into the common address space
3406 before we can subtract the numerical address values. */
3407 if (as0 != as1)
3409 addr_space_t as_common;
3410 tree common_type;
3412 /* Determine the common superset address space. This is guaranteed
3413 to exist because the caller verified that comp_target_types
3414 returned non-zero. */
3415 if (!addr_space_superset (as0, as1, &as_common))
3416 gcc_unreachable ();
3418 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3419 op0 = convert (common_type, op0);
3420 op1 = convert (common_type, op1);
3423 /* Determine integer type to perform computations in. This will usually
3424 be the same as the result type (ptrdiff_t), but may need to be a wider
3425 type if pointers for the address space are wider than ptrdiff_t. */
3426 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3427 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3428 else
3429 inttype = restype;
3432 if (TREE_CODE (target_type) == VOID_TYPE)
3433 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3434 "pointer of type %<void *%> used in subtraction");
3435 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3436 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3437 "pointer to a function used in subtraction");
3439 /* If the conversion to ptrdiff_type does anything like widening or
3440 converting a partial to an integral mode, we get a convert_expression
3441 that is in the way to do any simplifications.
3442 (fold-const.c doesn't know that the extra bits won't be needed.
3443 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3444 different mode in place.)
3445 So first try to find a common term here 'by hand'; we want to cover
3446 at least the cases that occur in legal static initializers. */
3447 if (CONVERT_EXPR_P (op0)
3448 && (TYPE_PRECISION (TREE_TYPE (op0))
3449 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3450 con0 = TREE_OPERAND (op0, 0);
3451 else
3452 con0 = op0;
3453 if (CONVERT_EXPR_P (op1)
3454 && (TYPE_PRECISION (TREE_TYPE (op1))
3455 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3456 con1 = TREE_OPERAND (op1, 0);
3457 else
3458 con1 = op1;
3460 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3462 lit0 = TREE_OPERAND (con0, 1);
3463 con0 = TREE_OPERAND (con0, 0);
3465 else
3466 lit0 = integer_zero_node;
3468 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
3470 lit1 = TREE_OPERAND (con1, 1);
3471 con1 = TREE_OPERAND (con1, 0);
3473 else
3474 lit1 = integer_zero_node;
3476 if (operand_equal_p (con0, con1, 0))
3478 op0 = lit0;
3479 op1 = lit1;
3483 /* First do the subtraction as integers;
3484 then drop through to build the divide operator.
3485 Do not do default conversions on the minus operator
3486 in case restype is a short type. */
3488 op0 = build_binary_op (loc,
3489 MINUS_EXPR, convert (inttype, op0),
3490 convert (inttype, op1), 0);
3491 /* This generates an error if op1 is pointer to incomplete type. */
3492 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3493 error_at (loc, "arithmetic on pointer to an incomplete type");
3495 /* This generates an error if op0 is pointer to incomplete type. */
3496 op1 = c_size_in_bytes (target_type);
3498 /* Divide by the size, in easiest possible way. */
3499 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3500 op0, convert (inttype, op1));
3502 /* Convert to final result type if necessary. */
3503 return convert (restype, result);
3506 /* Construct and perhaps optimize a tree representation
3507 for a unary operation. CODE, a tree_code, specifies the operation
3508 and XARG is the operand.
3509 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3510 the default promotions (such as from short to int).
3511 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3512 allows non-lvalues; this is only used to handle conversion of non-lvalue
3513 arrays to pointers in C99.
3515 LOCATION is the location of the operator. */
3517 tree
3518 build_unary_op (location_t location,
3519 enum tree_code code, tree xarg, int flag)
3521 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3522 tree arg = xarg;
3523 tree argtype = 0;
3524 enum tree_code typecode;
3525 tree val;
3526 tree ret = error_mark_node;
3527 tree eptype = NULL_TREE;
3528 int noconvert = flag;
3529 const char *invalid_op_diag;
3530 bool int_operands;
3532 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3533 if (int_operands)
3534 arg = remove_c_maybe_const_expr (arg);
3536 if (code != ADDR_EXPR)
3537 arg = require_complete_type (arg);
3539 typecode = TREE_CODE (TREE_TYPE (arg));
3540 if (typecode == ERROR_MARK)
3541 return error_mark_node;
3542 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3543 typecode = INTEGER_TYPE;
3545 if ((invalid_op_diag
3546 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3548 error_at (location, invalid_op_diag);
3549 return error_mark_node;
3552 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3554 eptype = TREE_TYPE (arg);
3555 arg = TREE_OPERAND (arg, 0);
3558 switch (code)
3560 case CONVERT_EXPR:
3561 /* This is used for unary plus, because a CONVERT_EXPR
3562 is enough to prevent anybody from looking inside for
3563 associativity, but won't generate any code. */
3564 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3565 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3566 || typecode == VECTOR_TYPE))
3568 error_at (location, "wrong type argument to unary plus");
3569 return error_mark_node;
3571 else if (!noconvert)
3572 arg = default_conversion (arg);
3573 arg = non_lvalue_loc (location, arg);
3574 break;
3576 case NEGATE_EXPR:
3577 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3578 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3579 || typecode == VECTOR_TYPE))
3581 error_at (location, "wrong type argument to unary minus");
3582 return error_mark_node;
3584 else if (!noconvert)
3585 arg = default_conversion (arg);
3586 break;
3588 case BIT_NOT_EXPR:
3589 /* ~ works on integer types and non float vectors. */
3590 if (typecode == INTEGER_TYPE
3591 || (typecode == VECTOR_TYPE
3592 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3594 if (!noconvert)
3595 arg = default_conversion (arg);
3597 else if (typecode == COMPLEX_TYPE)
3599 code = CONJ_EXPR;
3600 pedwarn (location, OPT_Wpedantic,
3601 "ISO C does not support %<~%> for complex conjugation");
3602 if (!noconvert)
3603 arg = default_conversion (arg);
3605 else
3607 error_at (location, "wrong type argument to bit-complement");
3608 return error_mark_node;
3610 break;
3612 case ABS_EXPR:
3613 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3615 error_at (location, "wrong type argument to abs");
3616 return error_mark_node;
3618 else if (!noconvert)
3619 arg = default_conversion (arg);
3620 break;
3622 case CONJ_EXPR:
3623 /* Conjugating a real value is a no-op, but allow it anyway. */
3624 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3625 || typecode == COMPLEX_TYPE))
3627 error_at (location, "wrong type argument to conjugation");
3628 return error_mark_node;
3630 else if (!noconvert)
3631 arg = default_conversion (arg);
3632 break;
3634 case TRUTH_NOT_EXPR:
3635 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3636 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3637 && typecode != COMPLEX_TYPE)
3639 error_at (location,
3640 "wrong type argument to unary exclamation mark");
3641 return error_mark_node;
3643 arg = c_objc_common_truthvalue_conversion (location, arg);
3644 ret = invert_truthvalue_loc (location, arg);
3645 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3646 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3647 location = EXPR_LOCATION (ret);
3648 goto return_build_unary_op;
3650 case REALPART_EXPR:
3651 case IMAGPART_EXPR:
3652 ret = build_real_imag_expr (location, code, arg);
3653 if (ret == error_mark_node)
3654 return error_mark_node;
3655 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3656 eptype = TREE_TYPE (eptype);
3657 goto return_build_unary_op;
3659 case PREINCREMENT_EXPR:
3660 case POSTINCREMENT_EXPR:
3661 case PREDECREMENT_EXPR:
3662 case POSTDECREMENT_EXPR:
3664 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3666 tree inner = build_unary_op (location, code,
3667 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3668 if (inner == error_mark_node)
3669 return error_mark_node;
3670 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3671 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3672 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3673 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3674 goto return_build_unary_op;
3677 /* Complain about anything that is not a true lvalue. In
3678 Objective-C, skip this check for property_refs. */
3679 if (!objc_is_property_ref (arg)
3680 && !lvalue_or_else (location,
3681 arg, ((code == PREINCREMENT_EXPR
3682 || code == POSTINCREMENT_EXPR)
3683 ? lv_increment
3684 : lv_decrement)))
3685 return error_mark_node;
3687 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3689 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3690 warning_at (location, OPT_Wc___compat,
3691 "increment of enumeration value is invalid in C++");
3692 else
3693 warning_at (location, OPT_Wc___compat,
3694 "decrement of enumeration value is invalid in C++");
3697 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3698 arg = c_fully_fold (arg, false, NULL);
3700 /* Increment or decrement the real part of the value,
3701 and don't change the imaginary part. */
3702 if (typecode == COMPLEX_TYPE)
3704 tree real, imag;
3706 pedwarn (location, OPT_Wpedantic,
3707 "ISO C does not support %<++%> and %<--%> on complex types");
3709 arg = stabilize_reference (arg);
3710 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3711 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3712 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3713 if (real == error_mark_node || imag == error_mark_node)
3714 return error_mark_node;
3715 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3716 real, imag);
3717 goto return_build_unary_op;
3720 /* Report invalid types. */
3722 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3723 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3725 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3726 error_at (location, "wrong type argument to increment");
3727 else
3728 error_at (location, "wrong type argument to decrement");
3730 return error_mark_node;
3734 tree inc;
3736 argtype = TREE_TYPE (arg);
3738 /* Compute the increment. */
3740 if (typecode == POINTER_TYPE)
3742 /* If pointer target is an undefined struct,
3743 we just cannot know how to do the arithmetic. */
3744 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3746 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3747 error_at (location,
3748 "increment of pointer to unknown structure");
3749 else
3750 error_at (location,
3751 "decrement of pointer to unknown structure");
3753 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3754 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3756 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3757 pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3758 "wrong type argument to increment");
3759 else
3760 pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3761 "wrong type argument to decrement");
3764 inc = c_size_in_bytes (TREE_TYPE (argtype));
3765 inc = convert_to_ptrofftype_loc (location, inc);
3767 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3769 /* For signed fract types, we invert ++ to -- or
3770 -- to ++, and change inc from 1 to -1, because
3771 it is not possible to represent 1 in signed fract constants.
3772 For unsigned fract types, the result always overflows and
3773 we get an undefined (original) or the maximum value. */
3774 if (code == PREINCREMENT_EXPR)
3775 code = PREDECREMENT_EXPR;
3776 else if (code == PREDECREMENT_EXPR)
3777 code = PREINCREMENT_EXPR;
3778 else if (code == POSTINCREMENT_EXPR)
3779 code = POSTDECREMENT_EXPR;
3780 else /* code == POSTDECREMENT_EXPR */
3781 code = POSTINCREMENT_EXPR;
3783 inc = integer_minus_one_node;
3784 inc = convert (argtype, inc);
3786 else
3788 inc = integer_one_node;
3789 inc = convert (argtype, inc);
3792 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3793 need to ask Objective-C to build the increment or decrement
3794 expression for it. */
3795 if (objc_is_property_ref (arg))
3796 return objc_build_incr_expr_for_property_ref (location, code,
3797 arg, inc);
3799 /* Report a read-only lvalue. */
3800 if (TYPE_READONLY (argtype))
3802 readonly_error (arg,
3803 ((code == PREINCREMENT_EXPR
3804 || code == POSTINCREMENT_EXPR)
3805 ? lv_increment : lv_decrement));
3806 return error_mark_node;
3808 else if (TREE_READONLY (arg))
3809 readonly_warning (arg,
3810 ((code == PREINCREMENT_EXPR
3811 || code == POSTINCREMENT_EXPR)
3812 ? lv_increment : lv_decrement));
3814 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3815 val = boolean_increment (code, arg);
3816 else
3817 val = build2 (code, TREE_TYPE (arg), arg, inc);
3818 TREE_SIDE_EFFECTS (val) = 1;
3819 if (TREE_CODE (val) != code)
3820 TREE_NO_WARNING (val) = 1;
3821 ret = val;
3822 goto return_build_unary_op;
3825 case ADDR_EXPR:
3826 /* Note that this operation never does default_conversion. */
3828 /* The operand of unary '&' must be an lvalue (which excludes
3829 expressions of type void), or, in C99, the result of a [] or
3830 unary '*' operator. */
3831 if (VOID_TYPE_P (TREE_TYPE (arg))
3832 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3833 && (TREE_CODE (arg) != INDIRECT_REF
3834 || !flag_isoc99))
3835 pedwarn (location, 0, "taking address of expression of type %<void%>");
3837 /* Let &* cancel out to simplify resulting code. */
3838 if (TREE_CODE (arg) == INDIRECT_REF)
3840 /* Don't let this be an lvalue. */
3841 if (lvalue_p (TREE_OPERAND (arg, 0)))
3842 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3843 ret = TREE_OPERAND (arg, 0);
3844 goto return_build_unary_op;
3847 /* For &x[y], return x+y */
3848 if (TREE_CODE (arg) == ARRAY_REF)
3850 tree op0 = TREE_OPERAND (arg, 0);
3851 if (!c_mark_addressable (op0))
3852 return error_mark_node;
3855 /* Anything not already handled and not a true memory reference
3856 or a non-lvalue array is an error. */
3857 else if (typecode != FUNCTION_TYPE && !flag
3858 && !lvalue_or_else (location, arg, lv_addressof))
3859 return error_mark_node;
3861 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3862 folding later. */
3863 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3865 tree inner = build_unary_op (location, code,
3866 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3867 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3868 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3869 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3870 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3871 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3872 goto return_build_unary_op;
3875 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3876 argtype = TREE_TYPE (arg);
3878 /* If the lvalue is const or volatile, merge that into the type
3879 to which the address will point. This is only needed
3880 for function types. */
3881 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3882 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3883 && TREE_CODE (argtype) == FUNCTION_TYPE)
3885 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3886 int quals = orig_quals;
3888 if (TREE_READONLY (arg))
3889 quals |= TYPE_QUAL_CONST;
3890 if (TREE_THIS_VOLATILE (arg))
3891 quals |= TYPE_QUAL_VOLATILE;
3893 argtype = c_build_qualified_type (argtype, quals);
3896 if (!c_mark_addressable (arg))
3897 return error_mark_node;
3899 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3900 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3902 argtype = build_pointer_type (argtype);
3904 /* ??? Cope with user tricks that amount to offsetof. Delete this
3905 when we have proper support for integer constant expressions. */
3906 val = get_base_address (arg);
3907 if (val && TREE_CODE (val) == INDIRECT_REF
3908 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3910 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
3911 goto return_build_unary_op;
3914 val = build1 (ADDR_EXPR, argtype, arg);
3916 ret = val;
3917 goto return_build_unary_op;
3919 default:
3920 gcc_unreachable ();
3923 if (argtype == 0)
3924 argtype = TREE_TYPE (arg);
3925 if (TREE_CODE (arg) == INTEGER_CST)
3926 ret = (require_constant_value
3927 ? fold_build1_initializer_loc (location, code, argtype, arg)
3928 : fold_build1_loc (location, code, argtype, arg));
3929 else
3930 ret = build1 (code, argtype, arg);
3931 return_build_unary_op:
3932 gcc_assert (ret != error_mark_node);
3933 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3934 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3935 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3936 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3937 ret = note_integer_operands (ret);
3938 if (eptype)
3939 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3940 protected_set_expr_location (ret, location);
3941 return ret;
3944 /* Return nonzero if REF is an lvalue valid for this language.
3945 Lvalues can be assigned, unless their type has TYPE_READONLY.
3946 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3948 bool
3949 lvalue_p (const_tree ref)
3951 const enum tree_code code = TREE_CODE (ref);
3953 switch (code)
3955 case REALPART_EXPR:
3956 case IMAGPART_EXPR:
3957 case COMPONENT_REF:
3958 return lvalue_p (TREE_OPERAND (ref, 0));
3960 case C_MAYBE_CONST_EXPR:
3961 return lvalue_p (TREE_OPERAND (ref, 1));
3963 case COMPOUND_LITERAL_EXPR:
3964 case STRING_CST:
3965 return 1;
3967 case INDIRECT_REF:
3968 case ARRAY_REF:
3969 case VAR_DECL:
3970 case PARM_DECL:
3971 case RESULT_DECL:
3972 case ERROR_MARK:
3973 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3974 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3976 case BIND_EXPR:
3977 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3979 default:
3980 return 0;
3984 /* Give a warning for storing in something that is read-only in GCC
3985 terms but not const in ISO C terms. */
3987 static void
3988 readonly_warning (tree arg, enum lvalue_use use)
3990 switch (use)
3992 case lv_assign:
3993 warning (0, "assignment of read-only location %qE", arg);
3994 break;
3995 case lv_increment:
3996 warning (0, "increment of read-only location %qE", arg);
3997 break;
3998 case lv_decrement:
3999 warning (0, "decrement of read-only location %qE", arg);
4000 break;
4001 default:
4002 gcc_unreachable ();
4004 return;
4008 /* Return nonzero if REF is an lvalue valid for this language;
4009 otherwise, print an error message and return zero. USE says
4010 how the lvalue is being used and so selects the error message.
4011 LOCATION is the location at which any error should be reported. */
4013 static int
4014 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4016 int win = lvalue_p (ref);
4018 if (!win)
4019 lvalue_error (loc, use);
4021 return win;
4024 /* Mark EXP saying that we need to be able to take the
4025 address of it; it should not be allocated in a register.
4026 Returns true if successful. */
4028 bool
4029 c_mark_addressable (tree exp)
4031 tree x = exp;
4033 while (1)
4034 switch (TREE_CODE (x))
4036 case COMPONENT_REF:
4037 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4039 error
4040 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4041 return false;
4044 /* ... fall through ... */
4046 case ADDR_EXPR:
4047 case ARRAY_REF:
4048 case REALPART_EXPR:
4049 case IMAGPART_EXPR:
4050 x = TREE_OPERAND (x, 0);
4051 break;
4053 case COMPOUND_LITERAL_EXPR:
4054 case CONSTRUCTOR:
4055 TREE_ADDRESSABLE (x) = 1;
4056 return true;
4058 case VAR_DECL:
4059 case CONST_DECL:
4060 case PARM_DECL:
4061 case RESULT_DECL:
4062 if (C_DECL_REGISTER (x)
4063 && DECL_NONLOCAL (x))
4065 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4067 error
4068 ("global register variable %qD used in nested function", x);
4069 return false;
4071 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4073 else if (C_DECL_REGISTER (x))
4075 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4076 error ("address of global register variable %qD requested", x);
4077 else
4078 error ("address of register variable %qD requested", x);
4079 return false;
4082 /* drops in */
4083 case FUNCTION_DECL:
4084 TREE_ADDRESSABLE (x) = 1;
4085 /* drops out */
4086 default:
4087 return true;
4091 /* Convert EXPR to TYPE, warning about conversion problems with
4092 constants. SEMANTIC_TYPE is the type this conversion would use
4093 without excess precision. If SEMANTIC_TYPE is NULL, this function
4094 is equivalent to convert_and_check. This function is a wrapper that
4095 handles conversions that may be different than
4096 the usual ones because of excess precision. */
4098 static tree
4099 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4101 if (TREE_TYPE (expr) == type)
4102 return expr;
4104 if (!semantic_type)
4105 return convert_and_check (type, expr);
4107 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4108 && TREE_TYPE (expr) != semantic_type)
4110 /* For integers, we need to check the real conversion, not
4111 the conversion to the excess precision type. */
4112 expr = convert_and_check (semantic_type, expr);
4114 /* Result type is the excess precision type, which should be
4115 large enough, so do not check. */
4116 return convert (type, expr);
4119 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4120 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4121 if folded to an integer constant then the unselected half may
4122 contain arbitrary operations not normally permitted in constant
4123 expressions. Set the location of the expression to LOC. */
4125 tree
4126 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4127 tree op1, tree op1_original_type, tree op2,
4128 tree op2_original_type)
4130 tree type1;
4131 tree type2;
4132 enum tree_code code1;
4133 enum tree_code code2;
4134 tree result_type = NULL;
4135 tree semantic_result_type = NULL;
4136 tree orig_op1 = op1, orig_op2 = op2;
4137 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4138 bool ifexp_int_operands;
4139 tree ret;
4141 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4142 if (op1_int_operands)
4143 op1 = remove_c_maybe_const_expr (op1);
4144 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4145 if (op2_int_operands)
4146 op2 = remove_c_maybe_const_expr (op2);
4147 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4148 if (ifexp_int_operands)
4149 ifexp = remove_c_maybe_const_expr (ifexp);
4151 /* Promote both alternatives. */
4153 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4154 op1 = default_conversion (op1);
4155 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4156 op2 = default_conversion (op2);
4158 if (TREE_CODE (ifexp) == ERROR_MARK
4159 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4160 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4161 return error_mark_node;
4163 type1 = TREE_TYPE (op1);
4164 code1 = TREE_CODE (type1);
4165 type2 = TREE_TYPE (op2);
4166 code2 = TREE_CODE (type2);
4168 /* C90 does not permit non-lvalue arrays in conditional expressions.
4169 In C99 they will be pointers by now. */
4170 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4172 error_at (colon_loc, "non-lvalue array in conditional expression");
4173 return error_mark_node;
4176 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4177 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4178 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4179 || code1 == COMPLEX_TYPE)
4180 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4181 || code2 == COMPLEX_TYPE))
4183 semantic_result_type = c_common_type (type1, type2);
4184 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4186 op1 = TREE_OPERAND (op1, 0);
4187 type1 = TREE_TYPE (op1);
4188 gcc_assert (TREE_CODE (type1) == code1);
4190 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4192 op2 = TREE_OPERAND (op2, 0);
4193 type2 = TREE_TYPE (op2);
4194 gcc_assert (TREE_CODE (type2) == code2);
4198 if (warn_cxx_compat)
4200 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4201 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4203 if (TREE_CODE (t1) == ENUMERAL_TYPE
4204 && TREE_CODE (t2) == ENUMERAL_TYPE
4205 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4206 warning_at (colon_loc, OPT_Wc___compat,
4207 ("different enum types in conditional is "
4208 "invalid in C++: %qT vs %qT"),
4209 t1, t2);
4212 /* Quickly detect the usual case where op1 and op2 have the same type
4213 after promotion. */
4214 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4216 if (type1 == type2)
4217 result_type = type1;
4218 else
4219 result_type = TYPE_MAIN_VARIANT (type1);
4221 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4222 || code1 == COMPLEX_TYPE)
4223 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4224 || code2 == COMPLEX_TYPE))
4226 result_type = c_common_type (type1, type2);
4227 do_warn_double_promotion (result_type, type1, type2,
4228 "implicit conversion from %qT to %qT to "
4229 "match other result of conditional",
4230 colon_loc);
4232 /* If -Wsign-compare, warn here if type1 and type2 have
4233 different signedness. We'll promote the signed to unsigned
4234 and later code won't know it used to be different.
4235 Do this check on the original types, so that explicit casts
4236 will be considered, but default promotions won't. */
4237 if (c_inhibit_evaluation_warnings == 0)
4239 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4240 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4242 if (unsigned_op1 ^ unsigned_op2)
4244 bool ovf;
4246 /* Do not warn if the result type is signed, since the
4247 signed type will only be chosen if it can represent
4248 all the values of the unsigned type. */
4249 if (!TYPE_UNSIGNED (result_type))
4250 /* OK */;
4251 else
4253 bool op1_maybe_const = true;
4254 bool op2_maybe_const = true;
4256 /* Do not warn if the signed quantity is an
4257 unsuffixed integer literal (or some static
4258 constant expression involving such literals) and
4259 it is non-negative. This warning requires the
4260 operands to be folded for best results, so do
4261 that folding in this case even without
4262 warn_sign_compare to avoid warning options
4263 possibly affecting code generation. */
4264 c_inhibit_evaluation_warnings
4265 += (ifexp == truthvalue_false_node);
4266 op1 = c_fully_fold (op1, require_constant_value,
4267 &op1_maybe_const);
4268 c_inhibit_evaluation_warnings
4269 -= (ifexp == truthvalue_false_node);
4271 c_inhibit_evaluation_warnings
4272 += (ifexp == truthvalue_true_node);
4273 op2 = c_fully_fold (op2, require_constant_value,
4274 &op2_maybe_const);
4275 c_inhibit_evaluation_warnings
4276 -= (ifexp == truthvalue_true_node);
4278 if (warn_sign_compare)
4280 if ((unsigned_op2
4281 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4282 || (unsigned_op1
4283 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4284 /* OK */;
4285 else
4286 warning_at (colon_loc, OPT_Wsign_compare,
4287 ("signed and unsigned type in "
4288 "conditional expression"));
4290 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4291 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4292 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4293 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4298 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4300 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4301 pedwarn (colon_loc, OPT_Wpedantic,
4302 "ISO C forbids conditional expr with only one void side");
4303 result_type = void_type_node;
4305 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4307 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4308 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4309 addr_space_t as_common;
4311 if (comp_target_types (colon_loc, type1, type2))
4312 result_type = common_pointer_type (type1, type2);
4313 else if (null_pointer_constant_p (orig_op1))
4314 result_type = type2;
4315 else if (null_pointer_constant_p (orig_op2))
4316 result_type = type1;
4317 else if (!addr_space_superset (as1, as2, &as_common))
4319 error_at (colon_loc, "pointers to disjoint address spaces "
4320 "used in conditional expression");
4321 return error_mark_node;
4323 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4325 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4326 pedwarn (colon_loc, OPT_Wpedantic,
4327 "ISO C forbids conditional expr between "
4328 "%<void *%> and function pointer");
4329 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4330 TREE_TYPE (type2)));
4332 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4334 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4335 pedwarn (colon_loc, OPT_Wpedantic,
4336 "ISO C forbids conditional expr between "
4337 "%<void *%> and function pointer");
4338 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4339 TREE_TYPE (type1)));
4341 /* Objective-C pointer comparisons are a bit more lenient. */
4342 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4343 result_type = objc_common_type (type1, type2);
4344 else
4346 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4348 pedwarn (colon_loc, 0,
4349 "pointer type mismatch in conditional expression");
4350 result_type = build_pointer_type
4351 (build_qualified_type (void_type_node, qual));
4354 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4356 if (!null_pointer_constant_p (orig_op2))
4357 pedwarn (colon_loc, 0,
4358 "pointer/integer type mismatch in conditional expression");
4359 else
4361 op2 = null_pointer_node;
4363 result_type = type1;
4365 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4367 if (!null_pointer_constant_p (orig_op1))
4368 pedwarn (colon_loc, 0,
4369 "pointer/integer type mismatch in conditional expression");
4370 else
4372 op1 = null_pointer_node;
4374 result_type = type2;
4377 if (!result_type)
4379 if (flag_cond_mismatch)
4380 result_type = void_type_node;
4381 else
4383 error_at (colon_loc, "type mismatch in conditional expression");
4384 return error_mark_node;
4388 /* Merge const and volatile flags of the incoming types. */
4389 result_type
4390 = build_type_variant (result_type,
4391 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4392 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4394 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4395 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4397 if (ifexp_bcp && ifexp == truthvalue_true_node)
4399 op2_int_operands = true;
4400 op1 = c_fully_fold (op1, require_constant_value, NULL);
4402 if (ifexp_bcp && ifexp == truthvalue_false_node)
4404 op1_int_operands = true;
4405 op2 = c_fully_fold (op2, require_constant_value, NULL);
4407 int_const = int_operands = (ifexp_int_operands
4408 && op1_int_operands
4409 && op2_int_operands);
4410 if (int_operands)
4412 int_const = ((ifexp == truthvalue_true_node
4413 && TREE_CODE (orig_op1) == INTEGER_CST
4414 && !TREE_OVERFLOW (orig_op1))
4415 || (ifexp == truthvalue_false_node
4416 && TREE_CODE (orig_op2) == INTEGER_CST
4417 && !TREE_OVERFLOW (orig_op2)));
4419 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4420 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4421 else
4423 if (int_operands)
4425 op1 = remove_c_maybe_const_expr (op1);
4426 op2 = remove_c_maybe_const_expr (op2);
4428 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4429 if (int_operands)
4430 ret = note_integer_operands (ret);
4432 if (semantic_result_type)
4433 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4435 protected_set_expr_location (ret, colon_loc);
4436 return ret;
4439 /* Return a compound expression that performs two expressions and
4440 returns the value of the second of them.
4442 LOC is the location of the COMPOUND_EXPR. */
4444 tree
4445 build_compound_expr (location_t loc, tree expr1, tree expr2)
4447 bool expr1_int_operands, expr2_int_operands;
4448 tree eptype = NULL_TREE;
4449 tree ret;
4451 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4452 if (expr1_int_operands)
4453 expr1 = remove_c_maybe_const_expr (expr1);
4454 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4455 if (expr2_int_operands)
4456 expr2 = remove_c_maybe_const_expr (expr2);
4458 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4459 expr1 = TREE_OPERAND (expr1, 0);
4460 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4462 eptype = TREE_TYPE (expr2);
4463 expr2 = TREE_OPERAND (expr2, 0);
4466 if (!TREE_SIDE_EFFECTS (expr1))
4468 /* The left-hand operand of a comma expression is like an expression
4469 statement: with -Wunused, we should warn if it doesn't have
4470 any side-effects, unless it was explicitly cast to (void). */
4471 if (warn_unused_value)
4473 if (VOID_TYPE_P (TREE_TYPE (expr1))
4474 && CONVERT_EXPR_P (expr1))
4475 ; /* (void) a, b */
4476 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4477 && TREE_CODE (expr1) == COMPOUND_EXPR
4478 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4479 ; /* (void) a, (void) b, c */
4480 else
4481 warning_at (loc, OPT_Wunused_value,
4482 "left-hand operand of comma expression has no effect");
4486 /* With -Wunused, we should also warn if the left-hand operand does have
4487 side-effects, but computes a value which is not used. For example, in
4488 `foo() + bar(), baz()' the result of the `+' operator is not used,
4489 so we should issue a warning. */
4490 else if (warn_unused_value)
4491 warn_if_unused_value (expr1, loc);
4493 if (expr2 == error_mark_node)
4494 return error_mark_node;
4496 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4498 if (flag_isoc99
4499 && expr1_int_operands
4500 && expr2_int_operands)
4501 ret = note_integer_operands (ret);
4503 if (eptype)
4504 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4506 protected_set_expr_location (ret, loc);
4507 return ret;
4510 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4511 which we are casting. OTYPE is the type of the expression being
4512 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4513 of the cast. -Wcast-qual appeared on the command line. Named
4514 address space qualifiers are not handled here, because they result
4515 in different warnings. */
4517 static void
4518 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4520 tree in_type = type;
4521 tree in_otype = otype;
4522 int added = 0;
4523 int discarded = 0;
4524 bool is_const;
4526 /* Check that the qualifiers on IN_TYPE are a superset of the
4527 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4528 nodes is uninteresting and we stop as soon as we hit a
4529 non-POINTER_TYPE node on either type. */
4532 in_otype = TREE_TYPE (in_otype);
4533 in_type = TREE_TYPE (in_type);
4535 /* GNU C allows cv-qualified function types. 'const' means the
4536 function is very pure, 'volatile' means it can't return. We
4537 need to warn when such qualifiers are added, not when they're
4538 taken away. */
4539 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4540 && TREE_CODE (in_type) == FUNCTION_TYPE)
4541 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4542 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4543 else
4544 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4545 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4547 while (TREE_CODE (in_type) == POINTER_TYPE
4548 && TREE_CODE (in_otype) == POINTER_TYPE);
4550 if (added)
4551 warning_at (loc, OPT_Wcast_qual,
4552 "cast adds %q#v qualifier to function type", added);
4554 if (discarded)
4555 /* There are qualifiers present in IN_OTYPE that are not present
4556 in IN_TYPE. */
4557 warning_at (loc, OPT_Wcast_qual,
4558 "cast discards %q#v qualifier from pointer target type",
4559 discarded);
4561 if (added || discarded)
4562 return;
4564 /* A cast from **T to const **T is unsafe, because it can cause a
4565 const value to be changed with no additional warning. We only
4566 issue this warning if T is the same on both sides, and we only
4567 issue the warning if there are the same number of pointers on
4568 both sides, as otherwise the cast is clearly unsafe anyhow. A
4569 cast is unsafe when a qualifier is added at one level and const
4570 is not present at all outer levels.
4572 To issue this warning, we check at each level whether the cast
4573 adds new qualifiers not already seen. We don't need to special
4574 case function types, as they won't have the same
4575 TYPE_MAIN_VARIANT. */
4577 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4578 return;
4579 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4580 return;
4582 in_type = type;
4583 in_otype = otype;
4584 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4587 in_type = TREE_TYPE (in_type);
4588 in_otype = TREE_TYPE (in_otype);
4589 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4590 && !is_const)
4592 warning_at (loc, OPT_Wcast_qual,
4593 "to be safe all intermediate pointers in cast from "
4594 "%qT to %qT must be %<const%> qualified",
4595 otype, type);
4596 break;
4598 if (is_const)
4599 is_const = TYPE_READONLY (in_type);
4601 while (TREE_CODE (in_type) == POINTER_TYPE);
4604 /* Build an expression representing a cast to type TYPE of expression EXPR.
4605 LOC is the location of the cast-- typically the open paren of the cast. */
4607 tree
4608 build_c_cast (location_t loc, tree type, tree expr)
4610 tree value;
4612 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4613 expr = TREE_OPERAND (expr, 0);
4615 value = expr;
4617 if (type == error_mark_node || expr == error_mark_node)
4618 return error_mark_node;
4620 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4621 only in <protocol> qualifications. But when constructing cast expressions,
4622 the protocols do matter and must be kept around. */
4623 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4624 return build1 (NOP_EXPR, type, expr);
4626 type = TYPE_MAIN_VARIANT (type);
4628 if (TREE_CODE (type) == ARRAY_TYPE)
4630 error_at (loc, "cast specifies array type");
4631 return error_mark_node;
4634 if (TREE_CODE (type) == FUNCTION_TYPE)
4636 error_at (loc, "cast specifies function type");
4637 return error_mark_node;
4640 if (!VOID_TYPE_P (type))
4642 value = require_complete_type (value);
4643 if (value == error_mark_node)
4644 return error_mark_node;
4647 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4649 if (TREE_CODE (type) == RECORD_TYPE
4650 || TREE_CODE (type) == UNION_TYPE)
4651 pedwarn (loc, OPT_Wpedantic,
4652 "ISO C forbids casting nonscalar to the same type");
4654 else if (TREE_CODE (type) == UNION_TYPE)
4656 tree field;
4658 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4659 if (TREE_TYPE (field) != error_mark_node
4660 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4661 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4662 break;
4664 if (field)
4666 tree t;
4667 bool maybe_const = true;
4669 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
4670 t = c_fully_fold (value, false, &maybe_const);
4671 t = build_constructor_single (type, field, t);
4672 if (!maybe_const)
4673 t = c_wrap_maybe_const (t, true);
4674 t = digest_init (loc, type, t,
4675 NULL_TREE, false, true, 0);
4676 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4677 return t;
4679 error_at (loc, "cast to union type from type not present in union");
4680 return error_mark_node;
4682 else
4684 tree otype, ovalue;
4686 if (type == void_type_node)
4688 tree t = build1 (CONVERT_EXPR, type, value);
4689 SET_EXPR_LOCATION (t, loc);
4690 return t;
4693 otype = TREE_TYPE (value);
4695 /* Optionally warn about potentially worrisome casts. */
4696 if (warn_cast_qual
4697 && TREE_CODE (type) == POINTER_TYPE
4698 && TREE_CODE (otype) == POINTER_TYPE)
4699 handle_warn_cast_qual (loc, type, otype);
4701 /* Warn about conversions between pointers to disjoint
4702 address spaces. */
4703 if (TREE_CODE (type) == POINTER_TYPE
4704 && TREE_CODE (otype) == POINTER_TYPE
4705 && !null_pointer_constant_p (value))
4707 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4708 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4709 addr_space_t as_common;
4711 if (!addr_space_superset (as_to, as_from, &as_common))
4713 if (ADDR_SPACE_GENERIC_P (as_from))
4714 warning_at (loc, 0, "cast to %s address space pointer "
4715 "from disjoint generic address space pointer",
4716 c_addr_space_name (as_to));
4718 else if (ADDR_SPACE_GENERIC_P (as_to))
4719 warning_at (loc, 0, "cast to generic address space pointer "
4720 "from disjoint %s address space pointer",
4721 c_addr_space_name (as_from));
4723 else
4724 warning_at (loc, 0, "cast to %s address space pointer "
4725 "from disjoint %s address space pointer",
4726 c_addr_space_name (as_to),
4727 c_addr_space_name (as_from));
4731 /* Warn about possible alignment problems. */
4732 if (STRICT_ALIGNMENT
4733 && TREE_CODE (type) == POINTER_TYPE
4734 && TREE_CODE (otype) == POINTER_TYPE
4735 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4736 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4737 /* Don't warn about opaque types, where the actual alignment
4738 restriction is unknown. */
4739 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4740 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4741 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4742 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4743 warning_at (loc, OPT_Wcast_align,
4744 "cast increases required alignment of target type");
4746 if (TREE_CODE (type) == INTEGER_TYPE
4747 && TREE_CODE (otype) == POINTER_TYPE
4748 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4749 /* Unlike conversion of integers to pointers, where the
4750 warning is disabled for converting constants because
4751 of cases such as SIG_*, warn about converting constant
4752 pointers to integers. In some cases it may cause unwanted
4753 sign extension, and a warning is appropriate. */
4754 warning_at (loc, OPT_Wpointer_to_int_cast,
4755 "cast from pointer to integer of different size");
4757 if (TREE_CODE (value) == CALL_EXPR
4758 && TREE_CODE (type) != TREE_CODE (otype))
4759 warning_at (loc, OPT_Wbad_function_cast,
4760 "cast from function call of type %qT "
4761 "to non-matching type %qT", otype, type);
4763 if (TREE_CODE (type) == POINTER_TYPE
4764 && TREE_CODE (otype) == INTEGER_TYPE
4765 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4766 /* Don't warn about converting any constant. */
4767 && !TREE_CONSTANT (value))
4768 warning_at (loc,
4769 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4770 "of different size");
4772 if (warn_strict_aliasing <= 2)
4773 strict_aliasing_warning (otype, type, expr);
4775 /* If pedantic, warn for conversions between function and object
4776 pointer types, except for converting a null pointer constant
4777 to function pointer type. */
4778 if (pedantic
4779 && TREE_CODE (type) == POINTER_TYPE
4780 && TREE_CODE (otype) == POINTER_TYPE
4781 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4782 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4783 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
4784 "conversion of function pointer to object pointer type");
4786 if (pedantic
4787 && TREE_CODE (type) == POINTER_TYPE
4788 && TREE_CODE (otype) == POINTER_TYPE
4789 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4790 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4791 && !null_pointer_constant_p (value))
4792 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
4793 "conversion of object pointer to function pointer type");
4795 ovalue = value;
4796 value = convert (type, value);
4798 /* Ignore any integer overflow caused by the cast. */
4799 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4801 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4803 if (!TREE_OVERFLOW (value))
4805 /* Avoid clobbering a shared constant. */
4806 value = copy_node (value);
4807 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4810 else if (TREE_OVERFLOW (value))
4811 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4812 value = build_int_cst_wide (TREE_TYPE (value),
4813 TREE_INT_CST_LOW (value),
4814 TREE_INT_CST_HIGH (value));
4818 /* Don't let a cast be an lvalue. */
4819 if (value == expr)
4820 value = non_lvalue_loc (loc, value);
4822 /* Don't allow the results of casting to floating-point or complex
4823 types be confused with actual constants, or casts involving
4824 integer and pointer types other than direct integer-to-integer
4825 and integer-to-pointer be confused with integer constant
4826 expressions and null pointer constants. */
4827 if (TREE_CODE (value) == REAL_CST
4828 || TREE_CODE (value) == COMPLEX_CST
4829 || (TREE_CODE (value) == INTEGER_CST
4830 && !((TREE_CODE (expr) == INTEGER_CST
4831 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4832 || TREE_CODE (expr) == REAL_CST
4833 || TREE_CODE (expr) == COMPLEX_CST)))
4834 value = build1 (NOP_EXPR, type, value);
4836 if (CAN_HAVE_LOCATION_P (value))
4837 SET_EXPR_LOCATION (value, loc);
4838 return value;
4841 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4842 location of the open paren of the cast, or the position of the cast
4843 expr. */
4844 tree
4845 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4847 tree type;
4848 tree type_expr = NULL_TREE;
4849 bool type_expr_const = true;
4850 tree ret;
4851 int saved_wsp = warn_strict_prototypes;
4853 /* This avoids warnings about unprototyped casts on
4854 integers. E.g. "#define SIG_DFL (void(*)())0". */
4855 if (TREE_CODE (expr) == INTEGER_CST)
4856 warn_strict_prototypes = 0;
4857 type = groktypename (type_name, &type_expr, &type_expr_const);
4858 warn_strict_prototypes = saved_wsp;
4860 ret = build_c_cast (loc, type, expr);
4861 if (type_expr)
4863 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4864 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4865 SET_EXPR_LOCATION (ret, loc);
4868 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4869 SET_EXPR_LOCATION (ret, loc);
4871 /* C++ does not permits types to be defined in a cast, but it
4872 allows references to incomplete types. */
4873 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4874 warning_at (loc, OPT_Wc___compat,
4875 "defining a type in a cast is invalid in C++");
4877 return ret;
4880 /* Build an assignment expression of lvalue LHS from value RHS.
4881 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4882 may differ from TREE_TYPE (LHS) for an enum bitfield.
4883 MODIFYCODE is the code for a binary operator that we use
4884 to combine the old value of LHS with RHS to get the new value.
4885 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4886 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4887 which may differ from TREE_TYPE (RHS) for an enum value.
4889 LOCATION is the location of the MODIFYCODE operator.
4890 RHS_LOC is the location of the RHS. */
4892 tree
4893 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4894 enum tree_code modifycode,
4895 location_t rhs_loc, tree rhs, tree rhs_origtype)
4897 tree result;
4898 tree newrhs;
4899 tree rhs_semantic_type = NULL_TREE;
4900 tree lhstype = TREE_TYPE (lhs);
4901 tree olhstype = lhstype;
4902 bool npc;
4904 /* Types that aren't fully specified cannot be used in assignments. */
4905 lhs = require_complete_type (lhs);
4907 /* Avoid duplicate error messages from operands that had errors. */
4908 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4909 return error_mark_node;
4911 /* For ObjC properties, defer this check. */
4912 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4913 return error_mark_node;
4915 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4917 rhs_semantic_type = TREE_TYPE (rhs);
4918 rhs = TREE_OPERAND (rhs, 0);
4921 newrhs = rhs;
4923 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4925 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4926 lhs_origtype, modifycode, rhs_loc, rhs,
4927 rhs_origtype);
4928 if (inner == error_mark_node)
4929 return error_mark_node;
4930 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4931 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4932 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4933 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4934 protected_set_expr_location (result, location);
4935 return result;
4938 /* If a binary op has been requested, combine the old LHS value with the RHS
4939 producing the value we should actually store into the LHS. */
4941 if (modifycode != NOP_EXPR)
4943 lhs = c_fully_fold (lhs, false, NULL);
4944 lhs = stabilize_reference (lhs);
4945 newrhs = build_binary_op (location,
4946 modifycode, lhs, rhs, 1);
4948 /* The original type of the right hand side is no longer
4949 meaningful. */
4950 rhs_origtype = NULL_TREE;
4953 if (c_dialect_objc ())
4955 /* Check if we are modifying an Objective-C property reference;
4956 if so, we need to generate setter calls. */
4957 result = objc_maybe_build_modify_expr (lhs, newrhs);
4958 if (result)
4959 return result;
4961 /* Else, do the check that we postponed for Objective-C. */
4962 if (!lvalue_or_else (location, lhs, lv_assign))
4963 return error_mark_node;
4966 /* Give an error for storing in something that is 'const'. */
4968 if (TYPE_READONLY (lhstype)
4969 || ((TREE_CODE (lhstype) == RECORD_TYPE
4970 || TREE_CODE (lhstype) == UNION_TYPE)
4971 && C_TYPE_FIELDS_READONLY (lhstype)))
4973 readonly_error (lhs, lv_assign);
4974 return error_mark_node;
4976 else if (TREE_READONLY (lhs))
4977 readonly_warning (lhs, lv_assign);
4979 /* If storing into a structure or union member,
4980 it has probably been given type `int'.
4981 Compute the type that would go with
4982 the actual amount of storage the member occupies. */
4984 if (TREE_CODE (lhs) == COMPONENT_REF
4985 && (TREE_CODE (lhstype) == INTEGER_TYPE
4986 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4987 || TREE_CODE (lhstype) == REAL_TYPE
4988 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4989 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4991 /* If storing in a field that is in actuality a short or narrower than one,
4992 we must store in the field in its actual type. */
4994 if (lhstype != TREE_TYPE (lhs))
4996 lhs = copy_node (lhs);
4997 TREE_TYPE (lhs) = lhstype;
5000 /* Issue -Wc++-compat warnings about an assignment to an enum type
5001 when LHS does not have its original type. This happens for,
5002 e.g., an enum bitfield in a struct. */
5003 if (warn_cxx_compat
5004 && lhs_origtype != NULL_TREE
5005 && lhs_origtype != lhstype
5006 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5008 tree checktype = (rhs_origtype != NULL_TREE
5009 ? rhs_origtype
5010 : TREE_TYPE (rhs));
5011 if (checktype != error_mark_node
5012 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
5013 warning_at (location, OPT_Wc___compat,
5014 "enum conversion in assignment is invalid in C++");
5017 /* Convert new value to destination type. Fold it first, then
5018 restore any excess precision information, for the sake of
5019 conversion warnings. */
5021 npc = null_pointer_constant_p (newrhs);
5022 newrhs = c_fully_fold (newrhs, false, NULL);
5023 if (rhs_semantic_type)
5024 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5025 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
5026 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
5027 if (TREE_CODE (newrhs) == ERROR_MARK)
5028 return error_mark_node;
5030 /* Emit ObjC write barrier, if necessary. */
5031 if (c_dialect_objc () && flag_objc_gc)
5033 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5034 if (result)
5036 protected_set_expr_location (result, location);
5037 return result;
5041 /* Scan operands. */
5043 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5044 TREE_SIDE_EFFECTS (result) = 1;
5045 protected_set_expr_location (result, location);
5047 /* If we got the LHS in a different type for storing in,
5048 convert the result back to the nominal type of LHS
5049 so that the value we return always has the same type
5050 as the LHS argument. */
5052 if (olhstype == TREE_TYPE (result))
5053 return result;
5055 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5056 ic_assign, false, NULL_TREE, NULL_TREE, 0);
5057 protected_set_expr_location (result, location);
5058 return result;
5061 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5062 This is used to implement -fplan9-extensions. */
5064 static bool
5065 find_anonymous_field_with_type (tree struct_type, tree type)
5067 tree field;
5068 bool found;
5070 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5071 || TREE_CODE (struct_type) == UNION_TYPE);
5072 found = false;
5073 for (field = TYPE_FIELDS (struct_type);
5074 field != NULL_TREE;
5075 field = TREE_CHAIN (field))
5077 if (DECL_NAME (field) == NULL
5078 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5080 if (found)
5081 return false;
5082 found = true;
5084 else if (DECL_NAME (field) == NULL
5085 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5086 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5087 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5089 if (found)
5090 return false;
5091 found = true;
5094 return found;
5097 /* RHS is an expression whose type is pointer to struct. If there is
5098 an anonymous field in RHS with type TYPE, then return a pointer to
5099 that field in RHS. This is used with -fplan9-extensions. This
5100 returns NULL if no conversion could be found. */
5102 static tree
5103 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5105 tree rhs_struct_type, lhs_main_type;
5106 tree field, found_field;
5107 bool found_sub_field;
5108 tree ret;
5110 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5111 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5112 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5113 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5115 gcc_assert (POINTER_TYPE_P (type));
5116 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5118 found_field = NULL_TREE;
5119 found_sub_field = false;
5120 for (field = TYPE_FIELDS (rhs_struct_type);
5121 field != NULL_TREE;
5122 field = TREE_CHAIN (field))
5124 if (DECL_NAME (field) != NULL_TREE
5125 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5126 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5127 continue;
5128 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5130 if (found_field != NULL_TREE)
5131 return NULL_TREE;
5132 found_field = field;
5134 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5135 lhs_main_type))
5137 if (found_field != NULL_TREE)
5138 return NULL_TREE;
5139 found_field = field;
5140 found_sub_field = true;
5144 if (found_field == NULL_TREE)
5145 return NULL_TREE;
5147 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5148 build_fold_indirect_ref (rhs), found_field,
5149 NULL_TREE);
5150 ret = build_fold_addr_expr_loc (location, ret);
5152 if (found_sub_field)
5154 ret = convert_to_anonymous_field (location, type, ret);
5155 gcc_assert (ret != NULL_TREE);
5158 return ret;
5161 /* Convert value RHS to type TYPE as preparation for an assignment to
5162 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5163 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5164 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5165 constant before any folding.
5166 The real work of conversion is done by `convert'.
5167 The purpose of this function is to generate error messages
5168 for assignments that are not allowed in C.
5169 ERRTYPE says whether it is argument passing, assignment,
5170 initialization or return.
5172 LOCATION is the location of the RHS.
5173 FUNCTION is a tree for the function being called.
5174 PARMNUM is the number of the argument, for printing in error messages. */
5176 static tree
5177 convert_for_assignment (location_t location, tree type, tree rhs,
5178 tree origtype, enum impl_conv errtype,
5179 bool null_pointer_constant, tree fundecl,
5180 tree function, int parmnum)
5182 enum tree_code codel = TREE_CODE (type);
5183 tree orig_rhs = rhs;
5184 tree rhstype;
5185 enum tree_code coder;
5186 tree rname = NULL_TREE;
5187 bool objc_ok = false;
5189 if (errtype == ic_argpass)
5191 tree selector;
5192 /* Change pointer to function to the function itself for
5193 diagnostics. */
5194 if (TREE_CODE (function) == ADDR_EXPR
5195 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5196 function = TREE_OPERAND (function, 0);
5198 /* Handle an ObjC selector specially for diagnostics. */
5199 selector = objc_message_selector ();
5200 rname = function;
5201 if (selector && parmnum > 2)
5203 rname = selector;
5204 parmnum -= 2;
5208 /* This macro is used to emit diagnostics to ensure that all format
5209 strings are complete sentences, visible to gettext and checked at
5210 compile time. */
5211 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5212 do { \
5213 switch (errtype) \
5215 case ic_argpass: \
5216 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5217 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5218 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5219 "expected %qT but argument is of type %qT", \
5220 type, rhstype); \
5221 break; \
5222 case ic_assign: \
5223 pedwarn (LOCATION, OPT, AS); \
5224 break; \
5225 case ic_init: \
5226 pedwarn_init (LOCATION, OPT, IN); \
5227 break; \
5228 case ic_return: \
5229 pedwarn (LOCATION, OPT, RE); \
5230 break; \
5231 default: \
5232 gcc_unreachable (); \
5234 } while (0)
5236 /* This macro is used to emit diagnostics to ensure that all format
5237 strings are complete sentences, visible to gettext and checked at
5238 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5239 extra parameter to enumerate qualifiers. */
5241 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5242 do { \
5243 switch (errtype) \
5245 case ic_argpass: \
5246 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5247 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5248 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5249 "expected %qT but argument is of type %qT", \
5250 type, rhstype); \
5251 break; \
5252 case ic_assign: \
5253 pedwarn (LOCATION, OPT, AS, QUALS); \
5254 break; \
5255 case ic_init: \
5256 pedwarn (LOCATION, OPT, IN, QUALS); \
5257 break; \
5258 case ic_return: \
5259 pedwarn (LOCATION, OPT, RE, QUALS); \
5260 break; \
5261 default: \
5262 gcc_unreachable (); \
5264 } while (0)
5266 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5267 rhs = TREE_OPERAND (rhs, 0);
5269 rhstype = TREE_TYPE (rhs);
5270 coder = TREE_CODE (rhstype);
5272 if (coder == ERROR_MARK)
5273 return error_mark_node;
5275 if (c_dialect_objc ())
5277 int parmno;
5279 switch (errtype)
5281 case ic_return:
5282 parmno = 0;
5283 break;
5285 case ic_assign:
5286 parmno = -1;
5287 break;
5289 case ic_init:
5290 parmno = -2;
5291 break;
5293 default:
5294 parmno = parmnum;
5295 break;
5298 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5301 if (warn_cxx_compat)
5303 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5304 if (checktype != error_mark_node
5305 && TREE_CODE (type) == ENUMERAL_TYPE
5306 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5308 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5309 G_("enum conversion when passing argument "
5310 "%d of %qE is invalid in C++"),
5311 G_("enum conversion in assignment is "
5312 "invalid in C++"),
5313 G_("enum conversion in initialization is "
5314 "invalid in C++"),
5315 G_("enum conversion in return is "
5316 "invalid in C++"));
5320 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5321 return rhs;
5323 if (coder == VOID_TYPE)
5325 /* Except for passing an argument to an unprototyped function,
5326 this is a constraint violation. When passing an argument to
5327 an unprototyped function, it is compile-time undefined;
5328 making it a constraint in that case was rejected in
5329 DR#252. */
5330 error_at (location, "void value not ignored as it ought to be");
5331 return error_mark_node;
5333 rhs = require_complete_type (rhs);
5334 if (rhs == error_mark_node)
5335 return error_mark_node;
5336 /* A type converts to a reference to it.
5337 This code doesn't fully support references, it's just for the
5338 special case of va_start and va_copy. */
5339 if (codel == REFERENCE_TYPE
5340 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5342 if (!lvalue_p (rhs))
5344 error_at (location, "cannot pass rvalue to reference parameter");
5345 return error_mark_node;
5347 if (!c_mark_addressable (rhs))
5348 return error_mark_node;
5349 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5350 SET_EXPR_LOCATION (rhs, location);
5352 /* We already know that these two types are compatible, but they
5353 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5354 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5355 likely to be va_list, a typedef to __builtin_va_list, which
5356 is different enough that it will cause problems later. */
5357 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5359 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5360 SET_EXPR_LOCATION (rhs, location);
5363 rhs = build1 (NOP_EXPR, type, rhs);
5364 SET_EXPR_LOCATION (rhs, location);
5365 return rhs;
5367 /* Some types can interconvert without explicit casts. */
5368 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5369 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5370 return convert (type, rhs);
5371 /* Arithmetic types all interconvert, and enum is treated like int. */
5372 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5373 || codel == FIXED_POINT_TYPE
5374 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5375 || codel == BOOLEAN_TYPE)
5376 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5377 || coder == FIXED_POINT_TYPE
5378 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5379 || coder == BOOLEAN_TYPE))
5381 tree ret;
5382 bool save = in_late_binary_op;
5383 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5384 in_late_binary_op = true;
5385 ret = convert_and_check (type, orig_rhs);
5386 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5387 in_late_binary_op = save;
5388 return ret;
5391 /* Aggregates in different TUs might need conversion. */
5392 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5393 && codel == coder
5394 && comptypes (type, rhstype))
5395 return convert_and_check (type, rhs);
5397 /* Conversion to a transparent union or record from its member types.
5398 This applies only to function arguments. */
5399 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5400 && TYPE_TRANSPARENT_AGGR (type))
5401 && errtype == ic_argpass)
5403 tree memb, marginal_memb = NULL_TREE;
5405 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5407 tree memb_type = TREE_TYPE (memb);
5409 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5410 TYPE_MAIN_VARIANT (rhstype)))
5411 break;
5413 if (TREE_CODE (memb_type) != POINTER_TYPE)
5414 continue;
5416 if (coder == POINTER_TYPE)
5418 tree ttl = TREE_TYPE (memb_type);
5419 tree ttr = TREE_TYPE (rhstype);
5421 /* Any non-function converts to a [const][volatile] void *
5422 and vice versa; otherwise, targets must be the same.
5423 Meanwhile, the lhs target must have all the qualifiers of
5424 the rhs. */
5425 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5426 || comp_target_types (location, memb_type, rhstype))
5428 /* If this type won't generate any warnings, use it. */
5429 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5430 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5431 && TREE_CODE (ttl) == FUNCTION_TYPE)
5432 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5433 == TYPE_QUALS (ttr))
5434 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5435 == TYPE_QUALS (ttl))))
5436 break;
5438 /* Keep looking for a better type, but remember this one. */
5439 if (!marginal_memb)
5440 marginal_memb = memb;
5444 /* Can convert integer zero to any pointer type. */
5445 if (null_pointer_constant)
5447 rhs = null_pointer_node;
5448 break;
5452 if (memb || marginal_memb)
5454 if (!memb)
5456 /* We have only a marginally acceptable member type;
5457 it needs a warning. */
5458 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5459 tree ttr = TREE_TYPE (rhstype);
5461 /* Const and volatile mean something different for function
5462 types, so the usual warnings are not appropriate. */
5463 if (TREE_CODE (ttr) == FUNCTION_TYPE
5464 && TREE_CODE (ttl) == FUNCTION_TYPE)
5466 /* Because const and volatile on functions are
5467 restrictions that say the function will not do
5468 certain things, it is okay to use a const or volatile
5469 function where an ordinary one is wanted, but not
5470 vice-versa. */
5471 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5472 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5473 WARN_FOR_QUALIFIERS (location, 0,
5474 G_("passing argument %d of %qE "
5475 "makes %q#v qualified function "
5476 "pointer from unqualified"),
5477 G_("assignment makes %q#v qualified "
5478 "function pointer from "
5479 "unqualified"),
5480 G_("initialization makes %q#v qualified "
5481 "function pointer from "
5482 "unqualified"),
5483 G_("return makes %q#v qualified function "
5484 "pointer from unqualified"),
5485 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5487 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5488 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5489 WARN_FOR_QUALIFIERS (location, 0,
5490 G_("passing argument %d of %qE discards "
5491 "%qv qualifier from pointer target type"),
5492 G_("assignment discards %qv qualifier "
5493 "from pointer target type"),
5494 G_("initialization discards %qv qualifier "
5495 "from pointer target type"),
5496 G_("return discards %qv qualifier from "
5497 "pointer target type"),
5498 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5500 memb = marginal_memb;
5503 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5504 pedwarn (location, OPT_Wpedantic,
5505 "ISO C prohibits argument conversion to union type");
5507 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5508 return build_constructor_single (type, memb, rhs);
5512 /* Conversions among pointers */
5513 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5514 && (coder == codel))
5516 tree ttl = TREE_TYPE (type);
5517 tree ttr = TREE_TYPE (rhstype);
5518 tree mvl = ttl;
5519 tree mvr = ttr;
5520 bool is_opaque_pointer;
5521 int target_cmp = 0; /* Cache comp_target_types () result. */
5522 addr_space_t asl;
5523 addr_space_t asr;
5525 if (TREE_CODE (mvl) != ARRAY_TYPE)
5526 mvl = TYPE_MAIN_VARIANT (mvl);
5527 if (TREE_CODE (mvr) != ARRAY_TYPE)
5528 mvr = TYPE_MAIN_VARIANT (mvr);
5529 /* Opaque pointers are treated like void pointers. */
5530 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5532 /* The Plan 9 compiler permits a pointer to a struct to be
5533 automatically converted into a pointer to an anonymous field
5534 within the struct. */
5535 if (flag_plan9_extensions
5536 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5537 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5538 && mvl != mvr)
5540 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5541 if (new_rhs != NULL_TREE)
5543 rhs = new_rhs;
5544 rhstype = TREE_TYPE (rhs);
5545 coder = TREE_CODE (rhstype);
5546 ttr = TREE_TYPE (rhstype);
5547 mvr = TYPE_MAIN_VARIANT (ttr);
5551 /* C++ does not allow the implicit conversion void* -> T*. However,
5552 for the purpose of reducing the number of false positives, we
5553 tolerate the special case of
5555 int *p = NULL;
5557 where NULL is typically defined in C to be '(void *) 0'. */
5558 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5559 warning_at (location, OPT_Wc___compat,
5560 "request for implicit conversion "
5561 "from %qT to %qT not permitted in C++", rhstype, type);
5563 /* See if the pointers point to incompatible address spaces. */
5564 asl = TYPE_ADDR_SPACE (ttl);
5565 asr = TYPE_ADDR_SPACE (ttr);
5566 if (!null_pointer_constant_p (rhs)
5567 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5569 switch (errtype)
5571 case ic_argpass:
5572 error_at (location, "passing argument %d of %qE from pointer to "
5573 "non-enclosed address space", parmnum, rname);
5574 break;
5575 case ic_assign:
5576 error_at (location, "assignment from pointer to "
5577 "non-enclosed address space");
5578 break;
5579 case ic_init:
5580 error_at (location, "initialization from pointer to "
5581 "non-enclosed address space");
5582 break;
5583 case ic_return:
5584 error_at (location, "return from pointer to "
5585 "non-enclosed address space");
5586 break;
5587 default:
5588 gcc_unreachable ();
5590 return error_mark_node;
5593 /* Check if the right-hand side has a format attribute but the
5594 left-hand side doesn't. */
5595 if (warn_suggest_attribute_format
5596 && check_missing_format_attribute (type, rhstype))
5598 switch (errtype)
5600 case ic_argpass:
5601 warning_at (location, OPT_Wsuggest_attribute_format,
5602 "argument %d of %qE might be "
5603 "a candidate for a format attribute",
5604 parmnum, rname);
5605 break;
5606 case ic_assign:
5607 warning_at (location, OPT_Wsuggest_attribute_format,
5608 "assignment left-hand side might be "
5609 "a candidate for a format attribute");
5610 break;
5611 case ic_init:
5612 warning_at (location, OPT_Wsuggest_attribute_format,
5613 "initialization left-hand side might be "
5614 "a candidate for a format attribute");
5615 break;
5616 case ic_return:
5617 warning_at (location, OPT_Wsuggest_attribute_format,
5618 "return type might be "
5619 "a candidate for a format attribute");
5620 break;
5621 default:
5622 gcc_unreachable ();
5626 /* Any non-function converts to a [const][volatile] void *
5627 and vice versa; otherwise, targets must be the same.
5628 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5629 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5630 || (target_cmp = comp_target_types (location, type, rhstype))
5631 || is_opaque_pointer
5632 || (c_common_unsigned_type (mvl)
5633 == c_common_unsigned_type (mvr)))
5635 if (pedantic
5636 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5638 (VOID_TYPE_P (ttr)
5639 && !null_pointer_constant
5640 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5641 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
5642 G_("ISO C forbids passing argument %d of "
5643 "%qE between function pointer "
5644 "and %<void *%>"),
5645 G_("ISO C forbids assignment between "
5646 "function pointer and %<void *%>"),
5647 G_("ISO C forbids initialization between "
5648 "function pointer and %<void *%>"),
5649 G_("ISO C forbids return between function "
5650 "pointer and %<void *%>"));
5651 /* Const and volatile mean something different for function types,
5652 so the usual warnings are not appropriate. */
5653 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5654 && TREE_CODE (ttl) != FUNCTION_TYPE)
5656 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5657 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5659 WARN_FOR_QUALIFIERS (location, 0,
5660 G_("passing argument %d of %qE discards "
5661 "%qv qualifier from pointer target type"),
5662 G_("assignment discards %qv qualifier "
5663 "from pointer target type"),
5664 G_("initialization discards %qv qualifier "
5665 "from pointer target type"),
5666 G_("return discards %qv qualifier from "
5667 "pointer target type"),
5668 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5670 /* If this is not a case of ignoring a mismatch in signedness,
5671 no warning. */
5672 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5673 || target_cmp)
5675 /* If there is a mismatch, do warn. */
5676 else if (warn_pointer_sign)
5677 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5678 G_("pointer targets in passing argument "
5679 "%d of %qE differ in signedness"),
5680 G_("pointer targets in assignment "
5681 "differ in signedness"),
5682 G_("pointer targets in initialization "
5683 "differ in signedness"),
5684 G_("pointer targets in return differ "
5685 "in signedness"));
5687 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5688 && TREE_CODE (ttr) == FUNCTION_TYPE)
5690 /* Because const and volatile on functions are restrictions
5691 that say the function will not do certain things,
5692 it is okay to use a const or volatile function
5693 where an ordinary one is wanted, but not vice-versa. */
5694 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5695 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5696 WARN_FOR_QUALIFIERS (location, 0,
5697 G_("passing argument %d of %qE makes "
5698 "%q#v qualified function pointer "
5699 "from unqualified"),
5700 G_("assignment makes %q#v qualified function "
5701 "pointer from unqualified"),
5702 G_("initialization makes %q#v qualified "
5703 "function pointer from unqualified"),
5704 G_("return makes %q#v qualified function "
5705 "pointer from unqualified"),
5706 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5709 else
5710 /* Avoid warning about the volatile ObjC EH puts on decls. */
5711 if (!objc_ok)
5712 WARN_FOR_ASSIGNMENT (location, 0,
5713 G_("passing argument %d of %qE from "
5714 "incompatible pointer type"),
5715 G_("assignment from incompatible pointer type"),
5716 G_("initialization from incompatible "
5717 "pointer type"),
5718 G_("return from incompatible pointer type"));
5720 return convert (type, rhs);
5722 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5724 /* ??? This should not be an error when inlining calls to
5725 unprototyped functions. */
5726 error_at (location, "invalid use of non-lvalue array");
5727 return error_mark_node;
5729 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5731 /* An explicit constant 0 can convert to a pointer,
5732 or one that results from arithmetic, even including
5733 a cast to integer type. */
5734 if (!null_pointer_constant)
5735 WARN_FOR_ASSIGNMENT (location, 0,
5736 G_("passing argument %d of %qE makes "
5737 "pointer from integer without a cast"),
5738 G_("assignment makes pointer from integer "
5739 "without a cast"),
5740 G_("initialization makes pointer from "
5741 "integer without a cast"),
5742 G_("return makes pointer from integer "
5743 "without a cast"));
5745 return convert (type, rhs);
5747 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5749 WARN_FOR_ASSIGNMENT (location, 0,
5750 G_("passing argument %d of %qE makes integer "
5751 "from pointer without a cast"),
5752 G_("assignment makes integer from pointer "
5753 "without a cast"),
5754 G_("initialization makes integer from pointer "
5755 "without a cast"),
5756 G_("return makes integer from pointer "
5757 "without a cast"));
5758 return convert (type, rhs);
5760 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5762 tree ret;
5763 bool save = in_late_binary_op;
5764 in_late_binary_op = true;
5765 ret = convert (type, rhs);
5766 in_late_binary_op = save;
5767 return ret;
5770 switch (errtype)
5772 case ic_argpass:
5773 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5774 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5775 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5776 "expected %qT but argument is of type %qT", type, rhstype);
5777 break;
5778 case ic_assign:
5779 error_at (location, "incompatible types when assigning to type %qT from "
5780 "type %qT", type, rhstype);
5781 break;
5782 case ic_init:
5783 error_at (location,
5784 "incompatible types when initializing type %qT using type %qT",
5785 type, rhstype);
5786 break;
5787 case ic_return:
5788 error_at (location,
5789 "incompatible types when returning type %qT but %qT was "
5790 "expected", rhstype, type);
5791 break;
5792 default:
5793 gcc_unreachable ();
5796 return error_mark_node;
5799 /* If VALUE is a compound expr all of whose expressions are constant, then
5800 return its value. Otherwise, return error_mark_node.
5802 This is for handling COMPOUND_EXPRs as initializer elements
5803 which is allowed with a warning when -pedantic is specified. */
5805 static tree
5806 valid_compound_expr_initializer (tree value, tree endtype)
5808 if (TREE_CODE (value) == COMPOUND_EXPR)
5810 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5811 == error_mark_node)
5812 return error_mark_node;
5813 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5814 endtype);
5816 else if (!initializer_constant_valid_p (value, endtype))
5817 return error_mark_node;
5818 else
5819 return value;
5822 /* Perform appropriate conversions on the initial value of a variable,
5823 store it in the declaration DECL,
5824 and print any error messages that are appropriate.
5825 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5826 If the init is invalid, store an ERROR_MARK.
5828 INIT_LOC is the location of the initial value. */
5830 void
5831 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5833 tree value, type;
5834 bool npc = false;
5836 /* If variable's type was invalidly declared, just ignore it. */
5838 type = TREE_TYPE (decl);
5839 if (TREE_CODE (type) == ERROR_MARK)
5840 return;
5842 /* Digest the specified initializer into an expression. */
5844 if (init)
5845 npc = null_pointer_constant_p (init);
5846 value = digest_init (init_loc, type, init, origtype, npc,
5847 true, TREE_STATIC (decl));
5849 /* Store the expression if valid; else report error. */
5851 if (!in_system_header
5852 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5853 warning (OPT_Wtraditional, "traditional C rejects automatic "
5854 "aggregate initialization");
5856 DECL_INITIAL (decl) = value;
5858 /* ANSI wants warnings about out-of-range constant initializers. */
5859 STRIP_TYPE_NOPS (value);
5860 if (TREE_STATIC (decl))
5861 constant_expression_warning (value);
5863 /* Check if we need to set array size from compound literal size. */
5864 if (TREE_CODE (type) == ARRAY_TYPE
5865 && TYPE_DOMAIN (type) == 0
5866 && value != error_mark_node)
5868 tree inside_init = init;
5870 STRIP_TYPE_NOPS (inside_init);
5871 inside_init = fold (inside_init);
5873 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5875 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5877 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5879 /* For int foo[] = (int [3]){1}; we need to set array size
5880 now since later on array initializer will be just the
5881 brace enclosed list of the compound literal. */
5882 tree etype = strip_array_types (TREE_TYPE (decl));
5883 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5884 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5885 layout_type (type);
5886 layout_decl (cldecl, 0);
5887 TREE_TYPE (decl)
5888 = c_build_qualified_type (type, TYPE_QUALS (etype));
5894 /* Methods for storing and printing names for error messages. */
5896 /* Implement a spelling stack that allows components of a name to be pushed
5897 and popped. Each element on the stack is this structure. */
5899 struct spelling
5901 int kind;
5902 union
5904 unsigned HOST_WIDE_INT i;
5905 const char *s;
5906 } u;
5909 #define SPELLING_STRING 1
5910 #define SPELLING_MEMBER 2
5911 #define SPELLING_BOUNDS 3
5913 static struct spelling *spelling; /* Next stack element (unused). */
5914 static struct spelling *spelling_base; /* Spelling stack base. */
5915 static int spelling_size; /* Size of the spelling stack. */
5917 /* Macros to save and restore the spelling stack around push_... functions.
5918 Alternative to SAVE_SPELLING_STACK. */
5920 #define SPELLING_DEPTH() (spelling - spelling_base)
5921 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5923 /* Push an element on the spelling stack with type KIND and assign VALUE
5924 to MEMBER. */
5926 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5928 int depth = SPELLING_DEPTH (); \
5930 if (depth >= spelling_size) \
5932 spelling_size += 10; \
5933 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5934 spelling_size); \
5935 RESTORE_SPELLING_DEPTH (depth); \
5938 spelling->kind = (KIND); \
5939 spelling->MEMBER = (VALUE); \
5940 spelling++; \
5943 /* Push STRING on the stack. Printed literally. */
5945 static void
5946 push_string (const char *string)
5948 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5951 /* Push a member name on the stack. Printed as '.' STRING. */
5953 static void
5954 push_member_name (tree decl)
5956 const char *const string
5957 = (DECL_NAME (decl)
5958 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5959 : _("<anonymous>"));
5960 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5963 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5965 static void
5966 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5968 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5971 /* Compute the maximum size in bytes of the printed spelling. */
5973 static int
5974 spelling_length (void)
5976 int size = 0;
5977 struct spelling *p;
5979 for (p = spelling_base; p < spelling; p++)
5981 if (p->kind == SPELLING_BOUNDS)
5982 size += 25;
5983 else
5984 size += strlen (p->u.s) + 1;
5987 return size;
5990 /* Print the spelling to BUFFER and return it. */
5992 static char *
5993 print_spelling (char *buffer)
5995 char *d = buffer;
5996 struct spelling *p;
5998 for (p = spelling_base; p < spelling; p++)
5999 if (p->kind == SPELLING_BOUNDS)
6001 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6002 d += strlen (d);
6004 else
6006 const char *s;
6007 if (p->kind == SPELLING_MEMBER)
6008 *d++ = '.';
6009 for (s = p->u.s; (*d = *s++); d++)
6012 *d++ = '\0';
6013 return buffer;
6016 /* Issue an error message for a bad initializer component.
6017 GMSGID identifies the message.
6018 The component name is taken from the spelling stack. */
6020 void
6021 error_init (const char *gmsgid)
6023 char *ofwhat;
6025 /* The gmsgid may be a format string with %< and %>. */
6026 error (gmsgid);
6027 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6028 if (*ofwhat)
6029 error ("(near initialization for %qs)", ofwhat);
6032 /* Issue a pedantic warning for a bad initializer component. OPT is
6033 the option OPT_* (from options.h) controlling this warning or 0 if
6034 it is unconditionally given. GMSGID identifies the message. The
6035 component name is taken from the spelling stack. */
6037 void
6038 pedwarn_init (location_t location, int opt, const char *gmsgid)
6040 char *ofwhat;
6042 /* The gmsgid may be a format string with %< and %>. */
6043 pedwarn (location, opt, gmsgid);
6044 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6045 if (*ofwhat)
6046 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6049 /* Issue a warning for a bad initializer component.
6051 OPT is the OPT_W* value corresponding to the warning option that
6052 controls this warning. GMSGID identifies the message. The
6053 component name is taken from the spelling stack. */
6055 static void
6056 warning_init (int opt, const char *gmsgid)
6058 char *ofwhat;
6060 /* The gmsgid may be a format string with %< and %>. */
6061 warning (opt, gmsgid);
6062 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6063 if (*ofwhat)
6064 warning (opt, "(near initialization for %qs)", ofwhat);
6067 /* If TYPE is an array type and EXPR is a parenthesized string
6068 constant, warn if pedantic that EXPR is being used to initialize an
6069 object of type TYPE. */
6071 void
6072 maybe_warn_string_init (tree type, struct c_expr expr)
6074 if (pedantic
6075 && TREE_CODE (type) == ARRAY_TYPE
6076 && TREE_CODE (expr.value) == STRING_CST
6077 && expr.original_code != STRING_CST)
6078 pedwarn_init (input_location, OPT_Wpedantic,
6079 "array initialized from parenthesized string constant");
6082 /* Digest the parser output INIT as an initializer for type TYPE.
6083 Return a C expression of type TYPE to represent the initial value.
6085 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6087 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6089 If INIT is a string constant, STRICT_STRING is true if it is
6090 unparenthesized or we should not warn here for it being parenthesized.
6091 For other types of INIT, STRICT_STRING is not used.
6093 INIT_LOC is the location of the INIT.
6095 REQUIRE_CONSTANT requests an error if non-constant initializers or
6096 elements are seen. */
6098 static tree
6099 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6100 bool null_pointer_constant, bool strict_string,
6101 int require_constant)
6103 enum tree_code code = TREE_CODE (type);
6104 tree inside_init = init;
6105 tree semantic_type = NULL_TREE;
6106 bool maybe_const = true;
6108 if (type == error_mark_node
6109 || !init
6110 || init == error_mark_node
6111 || TREE_TYPE (init) == error_mark_node)
6112 return error_mark_node;
6114 STRIP_TYPE_NOPS (inside_init);
6116 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6118 semantic_type = TREE_TYPE (inside_init);
6119 inside_init = TREE_OPERAND (inside_init, 0);
6121 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6122 inside_init = decl_constant_value_for_optimization (inside_init);
6124 /* Initialization of an array of chars from a string constant
6125 optionally enclosed in braces. */
6127 if (code == ARRAY_TYPE && inside_init
6128 && TREE_CODE (inside_init) == STRING_CST)
6130 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6131 /* Note that an array could be both an array of character type
6132 and an array of wchar_t if wchar_t is signed char or unsigned
6133 char. */
6134 bool char_array = (typ1 == char_type_node
6135 || typ1 == signed_char_type_node
6136 || typ1 == unsigned_char_type_node);
6137 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6138 bool char16_array = !!comptypes (typ1, char16_type_node);
6139 bool char32_array = !!comptypes (typ1, char32_type_node);
6141 if (char_array || wchar_array || char16_array || char32_array)
6143 struct c_expr expr;
6144 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6145 expr.value = inside_init;
6146 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6147 expr.original_type = NULL;
6148 maybe_warn_string_init (type, expr);
6150 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6151 pedwarn_init (init_loc, OPT_Wpedantic,
6152 "initialization of a flexible array member");
6154 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6155 TYPE_MAIN_VARIANT (type)))
6156 return inside_init;
6158 if (char_array)
6160 if (typ2 != char_type_node)
6162 error_init ("char-array initialized from wide string");
6163 return error_mark_node;
6166 else
6168 if (typ2 == char_type_node)
6170 error_init ("wide character array initialized from non-wide "
6171 "string");
6172 return error_mark_node;
6174 else if (!comptypes(typ1, typ2))
6176 error_init ("wide character array initialized from "
6177 "incompatible wide string");
6178 return error_mark_node;
6182 TREE_TYPE (inside_init) = type;
6183 if (TYPE_DOMAIN (type) != 0
6184 && TYPE_SIZE (type) != 0
6185 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6187 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6189 /* Subtract the size of a single (possibly wide) character
6190 because it's ok to ignore the terminating null char
6191 that is counted in the length of the constant. */
6192 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6193 (len
6194 - (TYPE_PRECISION (typ1)
6195 / BITS_PER_UNIT))))
6196 pedwarn_init (init_loc, 0,
6197 ("initializer-string for array of chars "
6198 "is too long"));
6199 else if (warn_cxx_compat
6200 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6201 warning_at (init_loc, OPT_Wc___compat,
6202 ("initializer-string for array chars "
6203 "is too long for C++"));
6206 return inside_init;
6208 else if (INTEGRAL_TYPE_P (typ1))
6210 error_init ("array of inappropriate type initialized "
6211 "from string constant");
6212 return error_mark_node;
6216 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6217 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6218 below and handle as a constructor. */
6219 if (code == VECTOR_TYPE
6220 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6221 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6222 && TREE_CONSTANT (inside_init))
6224 if (TREE_CODE (inside_init) == VECTOR_CST
6225 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6226 TYPE_MAIN_VARIANT (type)))
6227 return inside_init;
6229 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6231 unsigned HOST_WIDE_INT ix;
6232 tree value;
6233 bool constant_p = true;
6235 /* Iterate through elements and check if all constructor
6236 elements are *_CSTs. */
6237 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6238 if (!CONSTANT_CLASS_P (value))
6240 constant_p = false;
6241 break;
6244 if (constant_p)
6245 return build_vector_from_ctor (type,
6246 CONSTRUCTOR_ELTS (inside_init));
6250 if (warn_sequence_point)
6251 verify_sequence_points (inside_init);
6253 /* Any type can be initialized
6254 from an expression of the same type, optionally with braces. */
6256 if (inside_init && TREE_TYPE (inside_init) != 0
6257 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6258 TYPE_MAIN_VARIANT (type))
6259 || (code == ARRAY_TYPE
6260 && comptypes (TREE_TYPE (inside_init), type))
6261 || (code == VECTOR_TYPE
6262 && comptypes (TREE_TYPE (inside_init), type))
6263 || (code == POINTER_TYPE
6264 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6265 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6266 TREE_TYPE (type)))))
6268 if (code == POINTER_TYPE)
6270 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6272 if (TREE_CODE (inside_init) == STRING_CST
6273 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6274 inside_init = array_to_pointer_conversion
6275 (init_loc, inside_init);
6276 else
6278 error_init ("invalid use of non-lvalue array");
6279 return error_mark_node;
6284 if (code == VECTOR_TYPE)
6285 /* Although the types are compatible, we may require a
6286 conversion. */
6287 inside_init = convert (type, inside_init);
6289 if (require_constant
6290 && (code == VECTOR_TYPE || !flag_isoc99)
6291 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6293 /* As an extension, allow initializing objects with static storage
6294 duration with compound literals (which are then treated just as
6295 the brace enclosed list they contain). Also allow this for
6296 vectors, as we can only assign them with compound literals. */
6297 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6298 inside_init = DECL_INITIAL (decl);
6301 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6302 && TREE_CODE (inside_init) != CONSTRUCTOR)
6304 error_init ("array initialized from non-constant array expression");
6305 return error_mark_node;
6308 /* Compound expressions can only occur here if -Wpedantic or
6309 -pedantic-errors is specified. In the later case, we always want
6310 an error. In the former case, we simply want a warning. */
6311 if (require_constant && pedantic
6312 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6314 inside_init
6315 = valid_compound_expr_initializer (inside_init,
6316 TREE_TYPE (inside_init));
6317 if (inside_init == error_mark_node)
6318 error_init ("initializer element is not constant");
6319 else
6320 pedwarn_init (init_loc, OPT_Wpedantic,
6321 "initializer element is not constant");
6322 if (flag_pedantic_errors)
6323 inside_init = error_mark_node;
6325 else if (require_constant
6326 && !initializer_constant_valid_p (inside_init,
6327 TREE_TYPE (inside_init)))
6329 error_init ("initializer element is not constant");
6330 inside_init = error_mark_node;
6332 else if (require_constant && !maybe_const)
6333 pedwarn_init (init_loc, 0,
6334 "initializer element is not a constant expression");
6336 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6337 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6338 inside_init = convert_for_assignment (init_loc, type, inside_init,
6339 origtype,
6340 ic_init, null_pointer_constant,
6341 NULL_TREE, NULL_TREE, 0);
6342 return inside_init;
6345 /* Handle scalar types, including conversions. */
6347 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6348 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6349 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6351 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6352 && (TREE_CODE (init) == STRING_CST
6353 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6354 inside_init = init = array_to_pointer_conversion (init_loc, init);
6355 if (semantic_type)
6356 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6357 inside_init);
6358 inside_init
6359 = convert_for_assignment (init_loc, type, inside_init, origtype,
6360 ic_init, null_pointer_constant,
6361 NULL_TREE, NULL_TREE, 0);
6363 /* Check to see if we have already given an error message. */
6364 if (inside_init == error_mark_node)
6366 else if (require_constant && !TREE_CONSTANT (inside_init))
6368 error_init ("initializer element is not constant");
6369 inside_init = error_mark_node;
6371 else if (require_constant
6372 && !initializer_constant_valid_p (inside_init,
6373 TREE_TYPE (inside_init)))
6375 error_init ("initializer element is not computable at load time");
6376 inside_init = error_mark_node;
6378 else if (require_constant && !maybe_const)
6379 pedwarn_init (init_loc, 0,
6380 "initializer element is not a constant expression");
6382 return inside_init;
6385 /* Come here only for records and arrays. */
6387 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6389 error_init ("variable-sized object may not be initialized");
6390 return error_mark_node;
6393 error_init ("invalid initializer");
6394 return error_mark_node;
6397 /* Handle initializers that use braces. */
6399 /* Type of object we are accumulating a constructor for.
6400 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6401 static tree constructor_type;
6403 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6404 left to fill. */
6405 static tree constructor_fields;
6407 /* For an ARRAY_TYPE, this is the specified index
6408 at which to store the next element we get. */
6409 static tree constructor_index;
6411 /* For an ARRAY_TYPE, this is the maximum index. */
6412 static tree constructor_max_index;
6414 /* For a RECORD_TYPE, this is the first field not yet written out. */
6415 static tree constructor_unfilled_fields;
6417 /* For an ARRAY_TYPE, this is the index of the first element
6418 not yet written out. */
6419 static tree constructor_unfilled_index;
6421 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6422 This is so we can generate gaps between fields, when appropriate. */
6423 static tree constructor_bit_index;
6425 /* If we are saving up the elements rather than allocating them,
6426 this is the list of elements so far (in reverse order,
6427 most recent first). */
6428 static VEC(constructor_elt,gc) *constructor_elements;
6430 /* 1 if constructor should be incrementally stored into a constructor chain,
6431 0 if all the elements should be kept in AVL tree. */
6432 static int constructor_incremental;
6434 /* 1 if so far this constructor's elements are all compile-time constants. */
6435 static int constructor_constant;
6437 /* 1 if so far this constructor's elements are all valid address constants. */
6438 static int constructor_simple;
6440 /* 1 if this constructor has an element that cannot be part of a
6441 constant expression. */
6442 static int constructor_nonconst;
6444 /* 1 if this constructor is erroneous so far. */
6445 static int constructor_erroneous;
6447 /* Structure for managing pending initializer elements, organized as an
6448 AVL tree. */
6450 struct init_node
6452 struct init_node *left, *right;
6453 struct init_node *parent;
6454 int balance;
6455 tree purpose;
6456 tree value;
6457 tree origtype;
6460 /* Tree of pending elements at this constructor level.
6461 These are elements encountered out of order
6462 which belong at places we haven't reached yet in actually
6463 writing the output.
6464 Will never hold tree nodes across GC runs. */
6465 static struct init_node *constructor_pending_elts;
6467 /* The SPELLING_DEPTH of this constructor. */
6468 static int constructor_depth;
6470 /* DECL node for which an initializer is being read.
6471 0 means we are reading a constructor expression
6472 such as (struct foo) {...}. */
6473 static tree constructor_decl;
6475 /* Nonzero if this is an initializer for a top-level decl. */
6476 static int constructor_top_level;
6478 /* Nonzero if there were any member designators in this initializer. */
6479 static int constructor_designated;
6481 /* Nesting depth of designator list. */
6482 static int designator_depth;
6484 /* Nonzero if there were diagnosed errors in this designator list. */
6485 static int designator_erroneous;
6488 /* This stack has a level for each implicit or explicit level of
6489 structuring in the initializer, including the outermost one. It
6490 saves the values of most of the variables above. */
6492 struct constructor_range_stack;
6494 struct constructor_stack
6496 struct constructor_stack *next;
6497 tree type;
6498 tree fields;
6499 tree index;
6500 tree max_index;
6501 tree unfilled_index;
6502 tree unfilled_fields;
6503 tree bit_index;
6504 VEC(constructor_elt,gc) *elements;
6505 struct init_node *pending_elts;
6506 int offset;
6507 int depth;
6508 /* If value nonzero, this value should replace the entire
6509 constructor at this level. */
6510 struct c_expr replacement_value;
6511 struct constructor_range_stack *range_stack;
6512 char constant;
6513 char simple;
6514 char nonconst;
6515 char implicit;
6516 char erroneous;
6517 char outer;
6518 char incremental;
6519 char designated;
6522 static struct constructor_stack *constructor_stack;
6524 /* This stack represents designators from some range designator up to
6525 the last designator in the list. */
6527 struct constructor_range_stack
6529 struct constructor_range_stack *next, *prev;
6530 struct constructor_stack *stack;
6531 tree range_start;
6532 tree index;
6533 tree range_end;
6534 tree fields;
6537 static struct constructor_range_stack *constructor_range_stack;
6539 /* This stack records separate initializers that are nested.
6540 Nested initializers can't happen in ANSI C, but GNU C allows them
6541 in cases like { ... (struct foo) { ... } ... }. */
6543 struct initializer_stack
6545 struct initializer_stack *next;
6546 tree decl;
6547 struct constructor_stack *constructor_stack;
6548 struct constructor_range_stack *constructor_range_stack;
6549 VEC(constructor_elt,gc) *elements;
6550 struct spelling *spelling;
6551 struct spelling *spelling_base;
6552 int spelling_size;
6553 char top_level;
6554 char require_constant_value;
6555 char require_constant_elements;
6558 static struct initializer_stack *initializer_stack;
6560 /* Prepare to parse and output the initializer for variable DECL. */
6562 void
6563 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6565 const char *locus;
6566 struct initializer_stack *p = XNEW (struct initializer_stack);
6568 p->decl = constructor_decl;
6569 p->require_constant_value = require_constant_value;
6570 p->require_constant_elements = require_constant_elements;
6571 p->constructor_stack = constructor_stack;
6572 p->constructor_range_stack = constructor_range_stack;
6573 p->elements = constructor_elements;
6574 p->spelling = spelling;
6575 p->spelling_base = spelling_base;
6576 p->spelling_size = spelling_size;
6577 p->top_level = constructor_top_level;
6578 p->next = initializer_stack;
6579 initializer_stack = p;
6581 constructor_decl = decl;
6582 constructor_designated = 0;
6583 constructor_top_level = top_level;
6585 if (decl != 0 && decl != error_mark_node)
6587 require_constant_value = TREE_STATIC (decl);
6588 require_constant_elements
6589 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6590 /* For a scalar, you can always use any value to initialize,
6591 even within braces. */
6592 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6593 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6594 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6595 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6596 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6598 else
6600 require_constant_value = 0;
6601 require_constant_elements = 0;
6602 locus = _("(anonymous)");
6605 constructor_stack = 0;
6606 constructor_range_stack = 0;
6608 missing_braces_mentioned = 0;
6610 spelling_base = 0;
6611 spelling_size = 0;
6612 RESTORE_SPELLING_DEPTH (0);
6614 if (locus)
6615 push_string (locus);
6618 void
6619 finish_init (void)
6621 struct initializer_stack *p = initializer_stack;
6623 /* Free the whole constructor stack of this initializer. */
6624 while (constructor_stack)
6626 struct constructor_stack *q = constructor_stack;
6627 constructor_stack = q->next;
6628 free (q);
6631 gcc_assert (!constructor_range_stack);
6633 /* Pop back to the data of the outer initializer (if any). */
6634 free (spelling_base);
6636 constructor_decl = p->decl;
6637 require_constant_value = p->require_constant_value;
6638 require_constant_elements = p->require_constant_elements;
6639 constructor_stack = p->constructor_stack;
6640 constructor_range_stack = p->constructor_range_stack;
6641 constructor_elements = p->elements;
6642 spelling = p->spelling;
6643 spelling_base = p->spelling_base;
6644 spelling_size = p->spelling_size;
6645 constructor_top_level = p->top_level;
6646 initializer_stack = p->next;
6647 free (p);
6650 /* Call here when we see the initializer is surrounded by braces.
6651 This is instead of a call to push_init_level;
6652 it is matched by a call to pop_init_level.
6654 TYPE is the type to initialize, for a constructor expression.
6655 For an initializer for a decl, TYPE is zero. */
6657 void
6658 really_start_incremental_init (tree type)
6660 struct constructor_stack *p = XNEW (struct constructor_stack);
6662 if (type == 0)
6663 type = TREE_TYPE (constructor_decl);
6665 if (TREE_CODE (type) == VECTOR_TYPE
6666 && TYPE_VECTOR_OPAQUE (type))
6667 error ("opaque vector types cannot be initialized");
6669 p->type = constructor_type;
6670 p->fields = constructor_fields;
6671 p->index = constructor_index;
6672 p->max_index = constructor_max_index;
6673 p->unfilled_index = constructor_unfilled_index;
6674 p->unfilled_fields = constructor_unfilled_fields;
6675 p->bit_index = constructor_bit_index;
6676 p->elements = constructor_elements;
6677 p->constant = constructor_constant;
6678 p->simple = constructor_simple;
6679 p->nonconst = constructor_nonconst;
6680 p->erroneous = constructor_erroneous;
6681 p->pending_elts = constructor_pending_elts;
6682 p->depth = constructor_depth;
6683 p->replacement_value.value = 0;
6684 p->replacement_value.original_code = ERROR_MARK;
6685 p->replacement_value.original_type = NULL;
6686 p->implicit = 0;
6687 p->range_stack = 0;
6688 p->outer = 0;
6689 p->incremental = constructor_incremental;
6690 p->designated = constructor_designated;
6691 p->next = 0;
6692 constructor_stack = p;
6694 constructor_constant = 1;
6695 constructor_simple = 1;
6696 constructor_nonconst = 0;
6697 constructor_depth = SPELLING_DEPTH ();
6698 constructor_elements = 0;
6699 constructor_pending_elts = 0;
6700 constructor_type = type;
6701 constructor_incremental = 1;
6702 constructor_designated = 0;
6703 designator_depth = 0;
6704 designator_erroneous = 0;
6706 if (TREE_CODE (constructor_type) == RECORD_TYPE
6707 || TREE_CODE (constructor_type) == UNION_TYPE)
6709 constructor_fields = TYPE_FIELDS (constructor_type);
6710 /* Skip any nameless bit fields at the beginning. */
6711 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6712 && DECL_NAME (constructor_fields) == 0)
6713 constructor_fields = DECL_CHAIN (constructor_fields);
6715 constructor_unfilled_fields = constructor_fields;
6716 constructor_bit_index = bitsize_zero_node;
6718 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6720 if (TYPE_DOMAIN (constructor_type))
6722 constructor_max_index
6723 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6725 /* Detect non-empty initializations of zero-length arrays. */
6726 if (constructor_max_index == NULL_TREE
6727 && TYPE_SIZE (constructor_type))
6728 constructor_max_index = integer_minus_one_node;
6730 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6731 to initialize VLAs will cause a proper error; avoid tree
6732 checking errors as well by setting a safe value. */
6733 if (constructor_max_index
6734 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6735 constructor_max_index = integer_minus_one_node;
6737 constructor_index
6738 = convert (bitsizetype,
6739 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6741 else
6743 constructor_index = bitsize_zero_node;
6744 constructor_max_index = NULL_TREE;
6747 constructor_unfilled_index = constructor_index;
6749 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6751 /* Vectors are like simple fixed-size arrays. */
6752 constructor_max_index =
6753 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6754 constructor_index = bitsize_zero_node;
6755 constructor_unfilled_index = constructor_index;
6757 else
6759 /* Handle the case of int x = {5}; */
6760 constructor_fields = constructor_type;
6761 constructor_unfilled_fields = constructor_type;
6765 /* Push down into a subobject, for initialization.
6766 If this is for an explicit set of braces, IMPLICIT is 0.
6767 If it is because the next element belongs at a lower level,
6768 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6770 void
6771 push_init_level (int implicit, struct obstack * braced_init_obstack)
6773 struct constructor_stack *p;
6774 tree value = NULL_TREE;
6776 /* If we've exhausted any levels that didn't have braces,
6777 pop them now. If implicit == 1, this will have been done in
6778 process_init_element; do not repeat it here because in the case
6779 of excess initializers for an empty aggregate this leads to an
6780 infinite cycle of popping a level and immediately recreating
6781 it. */
6782 if (implicit != 1)
6784 while (constructor_stack->implicit)
6786 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6787 || TREE_CODE (constructor_type) == UNION_TYPE)
6788 && constructor_fields == 0)
6789 process_init_element (pop_init_level (1, braced_init_obstack),
6790 true, braced_init_obstack);
6791 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6792 && constructor_max_index
6793 && tree_int_cst_lt (constructor_max_index,
6794 constructor_index))
6795 process_init_element (pop_init_level (1, braced_init_obstack),
6796 true, braced_init_obstack);
6797 else
6798 break;
6802 /* Unless this is an explicit brace, we need to preserve previous
6803 content if any. */
6804 if (implicit)
6806 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6807 || TREE_CODE (constructor_type) == UNION_TYPE)
6808 && constructor_fields)
6809 value = find_init_member (constructor_fields, braced_init_obstack);
6810 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6811 value = find_init_member (constructor_index, braced_init_obstack);
6814 p = XNEW (struct constructor_stack);
6815 p->type = constructor_type;
6816 p->fields = constructor_fields;
6817 p->index = constructor_index;
6818 p->max_index = constructor_max_index;
6819 p->unfilled_index = constructor_unfilled_index;
6820 p->unfilled_fields = constructor_unfilled_fields;
6821 p->bit_index = constructor_bit_index;
6822 p->elements = constructor_elements;
6823 p->constant = constructor_constant;
6824 p->simple = constructor_simple;
6825 p->nonconst = constructor_nonconst;
6826 p->erroneous = constructor_erroneous;
6827 p->pending_elts = constructor_pending_elts;
6828 p->depth = constructor_depth;
6829 p->replacement_value.value = 0;
6830 p->replacement_value.original_code = ERROR_MARK;
6831 p->replacement_value.original_type = NULL;
6832 p->implicit = implicit;
6833 p->outer = 0;
6834 p->incremental = constructor_incremental;
6835 p->designated = constructor_designated;
6836 p->next = constructor_stack;
6837 p->range_stack = 0;
6838 constructor_stack = p;
6840 constructor_constant = 1;
6841 constructor_simple = 1;
6842 constructor_nonconst = 0;
6843 constructor_depth = SPELLING_DEPTH ();
6844 constructor_elements = 0;
6845 constructor_incremental = 1;
6846 constructor_designated = 0;
6847 constructor_pending_elts = 0;
6848 if (!implicit)
6850 p->range_stack = constructor_range_stack;
6851 constructor_range_stack = 0;
6852 designator_depth = 0;
6853 designator_erroneous = 0;
6856 /* Don't die if an entire brace-pair level is superfluous
6857 in the containing level. */
6858 if (constructor_type == 0)
6860 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6861 || TREE_CODE (constructor_type) == UNION_TYPE)
6863 /* Don't die if there are extra init elts at the end. */
6864 if (constructor_fields == 0)
6865 constructor_type = 0;
6866 else
6868 constructor_type = TREE_TYPE (constructor_fields);
6869 push_member_name (constructor_fields);
6870 constructor_depth++;
6873 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6875 constructor_type = TREE_TYPE (constructor_type);
6876 push_array_bounds (tree_low_cst (constructor_index, 1));
6877 constructor_depth++;
6880 if (constructor_type == 0)
6882 error_init ("extra brace group at end of initializer");
6883 constructor_fields = 0;
6884 constructor_unfilled_fields = 0;
6885 return;
6888 if (value && TREE_CODE (value) == CONSTRUCTOR)
6890 constructor_constant = TREE_CONSTANT (value);
6891 constructor_simple = TREE_STATIC (value);
6892 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6893 constructor_elements = CONSTRUCTOR_ELTS (value);
6894 if (!VEC_empty (constructor_elt, constructor_elements)
6895 && (TREE_CODE (constructor_type) == RECORD_TYPE
6896 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6897 set_nonincremental_init (braced_init_obstack);
6900 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6902 missing_braces_mentioned = 1;
6903 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6906 if (TREE_CODE (constructor_type) == RECORD_TYPE
6907 || TREE_CODE (constructor_type) == UNION_TYPE)
6909 constructor_fields = TYPE_FIELDS (constructor_type);
6910 /* Skip any nameless bit fields at the beginning. */
6911 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6912 && DECL_NAME (constructor_fields) == 0)
6913 constructor_fields = DECL_CHAIN (constructor_fields);
6915 constructor_unfilled_fields = constructor_fields;
6916 constructor_bit_index = bitsize_zero_node;
6918 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6920 /* Vectors are like simple fixed-size arrays. */
6921 constructor_max_index =
6922 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6923 constructor_index = bitsize_int (0);
6924 constructor_unfilled_index = constructor_index;
6926 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6928 if (TYPE_DOMAIN (constructor_type))
6930 constructor_max_index
6931 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6933 /* Detect non-empty initializations of zero-length arrays. */
6934 if (constructor_max_index == NULL_TREE
6935 && TYPE_SIZE (constructor_type))
6936 constructor_max_index = integer_minus_one_node;
6938 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6939 to initialize VLAs will cause a proper error; avoid tree
6940 checking errors as well by setting a safe value. */
6941 if (constructor_max_index
6942 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6943 constructor_max_index = integer_minus_one_node;
6945 constructor_index
6946 = convert (bitsizetype,
6947 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6949 else
6950 constructor_index = bitsize_zero_node;
6952 constructor_unfilled_index = constructor_index;
6953 if (value && TREE_CODE (value) == STRING_CST)
6955 /* We need to split the char/wchar array into individual
6956 characters, so that we don't have to special case it
6957 everywhere. */
6958 set_nonincremental_init_from_string (value, braced_init_obstack);
6961 else
6963 if (constructor_type != error_mark_node)
6964 warning_init (0, "braces around scalar initializer");
6965 constructor_fields = constructor_type;
6966 constructor_unfilled_fields = constructor_type;
6970 /* At the end of an implicit or explicit brace level,
6971 finish up that level of constructor. If a single expression
6972 with redundant braces initialized that level, return the
6973 c_expr structure for that expression. Otherwise, the original_code
6974 element is set to ERROR_MARK.
6975 If we were outputting the elements as they are read, return 0 as the value
6976 from inner levels (process_init_element ignores that),
6977 but return error_mark_node as the value from the outermost level
6978 (that's what we want to put in DECL_INITIAL).
6979 Otherwise, return a CONSTRUCTOR expression as the value. */
6981 struct c_expr
6982 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6984 struct constructor_stack *p;
6985 struct c_expr ret;
6986 ret.value = 0;
6987 ret.original_code = ERROR_MARK;
6988 ret.original_type = NULL;
6990 if (implicit == 0)
6992 /* When we come to an explicit close brace,
6993 pop any inner levels that didn't have explicit braces. */
6994 while (constructor_stack->implicit)
6996 process_init_element (pop_init_level (1, braced_init_obstack),
6997 true, braced_init_obstack);
6999 gcc_assert (!constructor_range_stack);
7002 /* Now output all pending elements. */
7003 constructor_incremental = 1;
7004 output_pending_init_elements (1, braced_init_obstack);
7006 p = constructor_stack;
7008 /* Error for initializing a flexible array member, or a zero-length
7009 array member in an inappropriate context. */
7010 if (constructor_type && constructor_fields
7011 && TREE_CODE (constructor_type) == ARRAY_TYPE
7012 && TYPE_DOMAIN (constructor_type)
7013 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7015 /* Silently discard empty initializations. The parser will
7016 already have pedwarned for empty brackets. */
7017 if (integer_zerop (constructor_unfilled_index))
7018 constructor_type = NULL_TREE;
7019 else
7021 gcc_assert (!TYPE_SIZE (constructor_type));
7023 if (constructor_depth > 2)
7024 error_init ("initialization of flexible array member in a nested context");
7025 else
7026 pedwarn_init (input_location, OPT_Wpedantic,
7027 "initialization of a flexible array member");
7029 /* We have already issued an error message for the existence
7030 of a flexible array member not at the end of the structure.
7031 Discard the initializer so that we do not die later. */
7032 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7033 constructor_type = NULL_TREE;
7037 /* Warn when some struct elements are implicitly initialized to zero. */
7038 if (warn_missing_field_initializers
7039 && constructor_type
7040 && TREE_CODE (constructor_type) == RECORD_TYPE
7041 && constructor_unfilled_fields)
7043 bool constructor_zeroinit =
7044 (VEC_length (constructor_elt, constructor_elements) == 1
7045 && integer_zerop
7046 (VEC_index (constructor_elt, constructor_elements, 0)->value));
7048 /* Do not warn for flexible array members or zero-length arrays. */
7049 while (constructor_unfilled_fields
7050 && (!DECL_SIZE (constructor_unfilled_fields)
7051 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7052 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7054 if (constructor_unfilled_fields
7055 /* Do not warn if this level of the initializer uses member
7056 designators; it is likely to be deliberate. */
7057 && !constructor_designated
7058 /* Do not warn about initializing with ` = {0}'. */
7059 && !constructor_zeroinit)
7061 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7062 "missing initializer for field %qD of %qT",
7063 constructor_unfilled_fields,
7064 constructor_type))
7065 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7066 "%qD declared here", constructor_unfilled_fields);
7070 /* Pad out the end of the structure. */
7071 if (p->replacement_value.value)
7072 /* If this closes a superfluous brace pair,
7073 just pass out the element between them. */
7074 ret = p->replacement_value;
7075 else if (constructor_type == 0)
7077 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7078 && TREE_CODE (constructor_type) != UNION_TYPE
7079 && TREE_CODE (constructor_type) != ARRAY_TYPE
7080 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7082 /* A nonincremental scalar initializer--just return
7083 the element, after verifying there is just one. */
7084 if (VEC_empty (constructor_elt,constructor_elements))
7086 if (!constructor_erroneous)
7087 error_init ("empty scalar initializer");
7088 ret.value = error_mark_node;
7090 else if (VEC_length (constructor_elt,constructor_elements) != 1)
7092 error_init ("extra elements in scalar initializer");
7093 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7095 else
7096 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7098 else
7100 if (constructor_erroneous)
7101 ret.value = error_mark_node;
7102 else
7104 ret.value = build_constructor (constructor_type,
7105 constructor_elements);
7106 if (constructor_constant)
7107 TREE_CONSTANT (ret.value) = 1;
7108 if (constructor_constant && constructor_simple)
7109 TREE_STATIC (ret.value) = 1;
7110 if (constructor_nonconst)
7111 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7115 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7117 if (constructor_nonconst)
7118 ret.original_code = C_MAYBE_CONST_EXPR;
7119 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7120 ret.original_code = ERROR_MARK;
7123 constructor_type = p->type;
7124 constructor_fields = p->fields;
7125 constructor_index = p->index;
7126 constructor_max_index = p->max_index;
7127 constructor_unfilled_index = p->unfilled_index;
7128 constructor_unfilled_fields = p->unfilled_fields;
7129 constructor_bit_index = p->bit_index;
7130 constructor_elements = p->elements;
7131 constructor_constant = p->constant;
7132 constructor_simple = p->simple;
7133 constructor_nonconst = p->nonconst;
7134 constructor_erroneous = p->erroneous;
7135 constructor_incremental = p->incremental;
7136 constructor_designated = p->designated;
7137 constructor_pending_elts = p->pending_elts;
7138 constructor_depth = p->depth;
7139 if (!p->implicit)
7140 constructor_range_stack = p->range_stack;
7141 RESTORE_SPELLING_DEPTH (constructor_depth);
7143 constructor_stack = p->next;
7144 free (p);
7146 if (ret.value == 0 && constructor_stack == 0)
7147 ret.value = error_mark_node;
7148 return ret;
7151 /* Common handling for both array range and field name designators.
7152 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7154 static int
7155 set_designator (int array, struct obstack * braced_init_obstack)
7157 tree subtype;
7158 enum tree_code subcode;
7160 /* Don't die if an entire brace-pair level is superfluous
7161 in the containing level. */
7162 if (constructor_type == 0)
7163 return 1;
7165 /* If there were errors in this designator list already, bail out
7166 silently. */
7167 if (designator_erroneous)
7168 return 1;
7170 if (!designator_depth)
7172 gcc_assert (!constructor_range_stack);
7174 /* Designator list starts at the level of closest explicit
7175 braces. */
7176 while (constructor_stack->implicit)
7178 process_init_element (pop_init_level (1, braced_init_obstack),
7179 true, braced_init_obstack);
7181 constructor_designated = 1;
7182 return 0;
7185 switch (TREE_CODE (constructor_type))
7187 case RECORD_TYPE:
7188 case UNION_TYPE:
7189 subtype = TREE_TYPE (constructor_fields);
7190 if (subtype != error_mark_node)
7191 subtype = TYPE_MAIN_VARIANT (subtype);
7192 break;
7193 case ARRAY_TYPE:
7194 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7195 break;
7196 default:
7197 gcc_unreachable ();
7200 subcode = TREE_CODE (subtype);
7201 if (array && subcode != ARRAY_TYPE)
7203 error_init ("array index in non-array initializer");
7204 return 1;
7206 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7208 error_init ("field name not in record or union initializer");
7209 return 1;
7212 constructor_designated = 1;
7213 push_init_level (2, braced_init_obstack);
7214 return 0;
7217 /* If there are range designators in designator list, push a new designator
7218 to constructor_range_stack. RANGE_END is end of such stack range or
7219 NULL_TREE if there is no range designator at this level. */
7221 static void
7222 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7224 struct constructor_range_stack *p;
7226 p = (struct constructor_range_stack *)
7227 obstack_alloc (braced_init_obstack,
7228 sizeof (struct constructor_range_stack));
7229 p->prev = constructor_range_stack;
7230 p->next = 0;
7231 p->fields = constructor_fields;
7232 p->range_start = constructor_index;
7233 p->index = constructor_index;
7234 p->stack = constructor_stack;
7235 p->range_end = range_end;
7236 if (constructor_range_stack)
7237 constructor_range_stack->next = p;
7238 constructor_range_stack = p;
7241 /* Within an array initializer, specify the next index to be initialized.
7242 FIRST is that index. If LAST is nonzero, then initialize a range
7243 of indices, running from FIRST through LAST. */
7245 void
7246 set_init_index (tree first, tree last,
7247 struct obstack * braced_init_obstack)
7249 if (set_designator (1, braced_init_obstack))
7250 return;
7252 designator_erroneous = 1;
7254 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7255 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7257 error_init ("array index in initializer not of integer type");
7258 return;
7261 if (TREE_CODE (first) != INTEGER_CST)
7263 first = c_fully_fold (first, false, NULL);
7264 if (TREE_CODE (first) == INTEGER_CST)
7265 pedwarn_init (input_location, OPT_Wpedantic,
7266 "array index in initializer is not "
7267 "an integer constant expression");
7270 if (last && TREE_CODE (last) != INTEGER_CST)
7272 last = c_fully_fold (last, false, NULL);
7273 if (TREE_CODE (last) == INTEGER_CST)
7274 pedwarn_init (input_location, OPT_Wpedantic,
7275 "array index in initializer is not "
7276 "an integer constant expression");
7279 if (TREE_CODE (first) != INTEGER_CST)
7280 error_init ("nonconstant array index in initializer");
7281 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7282 error_init ("nonconstant array index in initializer");
7283 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7284 error_init ("array index in non-array initializer");
7285 else if (tree_int_cst_sgn (first) == -1)
7286 error_init ("array index in initializer exceeds array bounds");
7287 else if (constructor_max_index
7288 && tree_int_cst_lt (constructor_max_index, first))
7289 error_init ("array index in initializer exceeds array bounds");
7290 else
7292 constant_expression_warning (first);
7293 if (last)
7294 constant_expression_warning (last);
7295 constructor_index = convert (bitsizetype, first);
7297 if (last)
7299 if (tree_int_cst_equal (first, last))
7300 last = 0;
7301 else if (tree_int_cst_lt (last, first))
7303 error_init ("empty index range in initializer");
7304 last = 0;
7306 else
7308 last = convert (bitsizetype, last);
7309 if (constructor_max_index != 0
7310 && tree_int_cst_lt (constructor_max_index, last))
7312 error_init ("array index range in initializer exceeds array bounds");
7313 last = 0;
7318 designator_depth++;
7319 designator_erroneous = 0;
7320 if (constructor_range_stack || last)
7321 push_range_stack (last, braced_init_obstack);
7325 /* Within a struct initializer, specify the next field to be initialized. */
7327 void
7328 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7330 tree field;
7332 if (set_designator (0, braced_init_obstack))
7333 return;
7335 designator_erroneous = 1;
7337 if (TREE_CODE (constructor_type) != RECORD_TYPE
7338 && TREE_CODE (constructor_type) != UNION_TYPE)
7340 error_init ("field name not in record or union initializer");
7341 return;
7344 field = lookup_field (constructor_type, fieldname);
7346 if (field == 0)
7347 error ("unknown field %qE specified in initializer", fieldname);
7348 else
7351 constructor_fields = TREE_VALUE (field);
7352 designator_depth++;
7353 designator_erroneous = 0;
7354 if (constructor_range_stack)
7355 push_range_stack (NULL_TREE, braced_init_obstack);
7356 field = TREE_CHAIN (field);
7357 if (field)
7359 if (set_designator (0, braced_init_obstack))
7360 return;
7363 while (field != NULL_TREE);
7366 /* Add a new initializer to the tree of pending initializers. PURPOSE
7367 identifies the initializer, either array index or field in a structure.
7368 VALUE is the value of that index or field. If ORIGTYPE is not
7369 NULL_TREE, it is the original type of VALUE.
7371 IMPLICIT is true if value comes from pop_init_level (1),
7372 the new initializer has been merged with the existing one
7373 and thus no warnings should be emitted about overriding an
7374 existing initializer. */
7376 static void
7377 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7378 struct obstack * braced_init_obstack)
7380 struct init_node *p, **q, *r;
7382 q = &constructor_pending_elts;
7383 p = 0;
7385 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7387 while (*q != 0)
7389 p = *q;
7390 if (tree_int_cst_lt (purpose, p->purpose))
7391 q = &p->left;
7392 else if (tree_int_cst_lt (p->purpose, purpose))
7393 q = &p->right;
7394 else
7396 if (!implicit)
7398 if (TREE_SIDE_EFFECTS (p->value))
7399 warning_init (0, "initialized field with side-effects overwritten");
7400 else if (warn_override_init)
7401 warning_init (OPT_Woverride_init, "initialized field overwritten");
7403 p->value = value;
7404 p->origtype = origtype;
7405 return;
7409 else
7411 tree bitpos;
7413 bitpos = bit_position (purpose);
7414 while (*q != NULL)
7416 p = *q;
7417 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7418 q = &p->left;
7419 else if (p->purpose != purpose)
7420 q = &p->right;
7421 else
7423 if (!implicit)
7425 if (TREE_SIDE_EFFECTS (p->value))
7426 warning_init (0, "initialized field with side-effects overwritten");
7427 else if (warn_override_init)
7428 warning_init (OPT_Woverride_init, "initialized field overwritten");
7430 p->value = value;
7431 p->origtype = origtype;
7432 return;
7437 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7438 sizeof (struct init_node));
7439 r->purpose = purpose;
7440 r->value = value;
7441 r->origtype = origtype;
7443 *q = r;
7444 r->parent = p;
7445 r->left = 0;
7446 r->right = 0;
7447 r->balance = 0;
7449 while (p)
7451 struct init_node *s;
7453 if (r == p->left)
7455 if (p->balance == 0)
7456 p->balance = -1;
7457 else if (p->balance < 0)
7459 if (r->balance < 0)
7461 /* L rotation. */
7462 p->left = r->right;
7463 if (p->left)
7464 p->left->parent = p;
7465 r->right = p;
7467 p->balance = 0;
7468 r->balance = 0;
7470 s = p->parent;
7471 p->parent = r;
7472 r->parent = s;
7473 if (s)
7475 if (s->left == p)
7476 s->left = r;
7477 else
7478 s->right = r;
7480 else
7481 constructor_pending_elts = r;
7483 else
7485 /* LR rotation. */
7486 struct init_node *t = r->right;
7488 r->right = t->left;
7489 if (r->right)
7490 r->right->parent = r;
7491 t->left = r;
7493 p->left = t->right;
7494 if (p->left)
7495 p->left->parent = p;
7496 t->right = p;
7498 p->balance = t->balance < 0;
7499 r->balance = -(t->balance > 0);
7500 t->balance = 0;
7502 s = p->parent;
7503 p->parent = t;
7504 r->parent = t;
7505 t->parent = s;
7506 if (s)
7508 if (s->left == p)
7509 s->left = t;
7510 else
7511 s->right = t;
7513 else
7514 constructor_pending_elts = t;
7516 break;
7518 else
7520 /* p->balance == +1; growth of left side balances the node. */
7521 p->balance = 0;
7522 break;
7525 else /* r == p->right */
7527 if (p->balance == 0)
7528 /* Growth propagation from right side. */
7529 p->balance++;
7530 else if (p->balance > 0)
7532 if (r->balance > 0)
7534 /* R rotation. */
7535 p->right = r->left;
7536 if (p->right)
7537 p->right->parent = p;
7538 r->left = p;
7540 p->balance = 0;
7541 r->balance = 0;
7543 s = p->parent;
7544 p->parent = r;
7545 r->parent = s;
7546 if (s)
7548 if (s->left == p)
7549 s->left = r;
7550 else
7551 s->right = r;
7553 else
7554 constructor_pending_elts = r;
7556 else /* r->balance == -1 */
7558 /* RL rotation */
7559 struct init_node *t = r->left;
7561 r->left = t->right;
7562 if (r->left)
7563 r->left->parent = r;
7564 t->right = r;
7566 p->right = t->left;
7567 if (p->right)
7568 p->right->parent = p;
7569 t->left = p;
7571 r->balance = (t->balance < 0);
7572 p->balance = -(t->balance > 0);
7573 t->balance = 0;
7575 s = p->parent;
7576 p->parent = t;
7577 r->parent = t;
7578 t->parent = s;
7579 if (s)
7581 if (s->left == p)
7582 s->left = t;
7583 else
7584 s->right = t;
7586 else
7587 constructor_pending_elts = t;
7589 break;
7591 else
7593 /* p->balance == -1; growth of right side balances the node. */
7594 p->balance = 0;
7595 break;
7599 r = p;
7600 p = p->parent;
7604 /* Build AVL tree from a sorted chain. */
7606 static void
7607 set_nonincremental_init (struct obstack * braced_init_obstack)
7609 unsigned HOST_WIDE_INT ix;
7610 tree index, value;
7612 if (TREE_CODE (constructor_type) != RECORD_TYPE
7613 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7614 return;
7616 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7618 add_pending_init (index, value, NULL_TREE, true,
7619 braced_init_obstack);
7621 constructor_elements = 0;
7622 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7624 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7625 /* Skip any nameless bit fields at the beginning. */
7626 while (constructor_unfilled_fields != 0
7627 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7628 && DECL_NAME (constructor_unfilled_fields) == 0)
7629 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7632 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7634 if (TYPE_DOMAIN (constructor_type))
7635 constructor_unfilled_index
7636 = convert (bitsizetype,
7637 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7638 else
7639 constructor_unfilled_index = bitsize_zero_node;
7641 constructor_incremental = 0;
7644 /* Build AVL tree from a string constant. */
7646 static void
7647 set_nonincremental_init_from_string (tree str,
7648 struct obstack * braced_init_obstack)
7650 tree value, purpose, type;
7651 HOST_WIDE_INT val[2];
7652 const char *p, *end;
7653 int byte, wchar_bytes, charwidth, bitpos;
7655 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7657 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7658 charwidth = TYPE_PRECISION (char_type_node);
7659 type = TREE_TYPE (constructor_type);
7660 p = TREE_STRING_POINTER (str);
7661 end = p + TREE_STRING_LENGTH (str);
7663 for (purpose = bitsize_zero_node;
7664 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7665 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7667 if (wchar_bytes == 1)
7669 val[1] = (unsigned char) *p++;
7670 val[0] = 0;
7672 else
7674 val[0] = 0;
7675 val[1] = 0;
7676 for (byte = 0; byte < wchar_bytes; byte++)
7678 if (BYTES_BIG_ENDIAN)
7679 bitpos = (wchar_bytes - byte - 1) * charwidth;
7680 else
7681 bitpos = byte * charwidth;
7682 val[bitpos < HOST_BITS_PER_WIDE_INT]
7683 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7684 << (bitpos % HOST_BITS_PER_WIDE_INT);
7688 if (!TYPE_UNSIGNED (type))
7690 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7691 if (bitpos < HOST_BITS_PER_WIDE_INT)
7693 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7695 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7696 val[0] = -1;
7699 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7701 if (val[1] < 0)
7702 val[0] = -1;
7704 else if (val[0] & (((HOST_WIDE_INT) 1)
7705 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7706 val[0] |= ((HOST_WIDE_INT) -1)
7707 << (bitpos - HOST_BITS_PER_WIDE_INT);
7710 value = build_int_cst_wide (type, val[1], val[0]);
7711 add_pending_init (purpose, value, NULL_TREE, true,
7712 braced_init_obstack);
7715 constructor_incremental = 0;
7718 /* Return value of FIELD in pending initializer or zero if the field was
7719 not initialized yet. */
7721 static tree
7722 find_init_member (tree field, struct obstack * braced_init_obstack)
7724 struct init_node *p;
7726 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7728 if (constructor_incremental
7729 && tree_int_cst_lt (field, constructor_unfilled_index))
7730 set_nonincremental_init (braced_init_obstack);
7732 p = constructor_pending_elts;
7733 while (p)
7735 if (tree_int_cst_lt (field, p->purpose))
7736 p = p->left;
7737 else if (tree_int_cst_lt (p->purpose, field))
7738 p = p->right;
7739 else
7740 return p->value;
7743 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7745 tree bitpos = bit_position (field);
7747 if (constructor_incremental
7748 && (!constructor_unfilled_fields
7749 || tree_int_cst_lt (bitpos,
7750 bit_position (constructor_unfilled_fields))))
7751 set_nonincremental_init (braced_init_obstack);
7753 p = constructor_pending_elts;
7754 while (p)
7756 if (field == p->purpose)
7757 return p->value;
7758 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7759 p = p->left;
7760 else
7761 p = p->right;
7764 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7766 if (!VEC_empty (constructor_elt, constructor_elements)
7767 && (VEC_last (constructor_elt, constructor_elements)->index
7768 == field))
7769 return VEC_last (constructor_elt, constructor_elements)->value;
7771 return 0;
7774 /* "Output" the next constructor element.
7775 At top level, really output it to assembler code now.
7776 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7777 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7778 TYPE is the data type that the containing data type wants here.
7779 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7780 If VALUE is a string constant, STRICT_STRING is true if it is
7781 unparenthesized or we should not warn here for it being parenthesized.
7782 For other types of VALUE, STRICT_STRING is not used.
7784 PENDING if non-nil means output pending elements that belong
7785 right after this element. (PENDING is normally 1;
7786 it is 0 while outputting pending elements, to avoid recursion.)
7788 IMPLICIT is true if value comes from pop_init_level (1),
7789 the new initializer has been merged with the existing one
7790 and thus no warnings should be emitted about overriding an
7791 existing initializer. */
7793 static void
7794 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7795 tree field, int pending, bool implicit,
7796 struct obstack * braced_init_obstack)
7798 tree semantic_type = NULL_TREE;
7799 constructor_elt *celt;
7800 bool maybe_const = true;
7801 bool npc;
7803 if (type == error_mark_node || value == error_mark_node)
7805 constructor_erroneous = 1;
7806 return;
7808 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7809 && (TREE_CODE (value) == STRING_CST
7810 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7811 && !(TREE_CODE (value) == STRING_CST
7812 && TREE_CODE (type) == ARRAY_TYPE
7813 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7814 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7815 TYPE_MAIN_VARIANT (type)))
7816 value = array_to_pointer_conversion (input_location, value);
7818 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7819 && require_constant_value && !flag_isoc99 && pending)
7821 /* As an extension, allow initializing objects with static storage
7822 duration with compound literals (which are then treated just as
7823 the brace enclosed list they contain). */
7824 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7825 value = DECL_INITIAL (decl);
7828 npc = null_pointer_constant_p (value);
7829 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7831 semantic_type = TREE_TYPE (value);
7832 value = TREE_OPERAND (value, 0);
7834 value = c_fully_fold (value, require_constant_value, &maybe_const);
7836 if (value == error_mark_node)
7837 constructor_erroneous = 1;
7838 else if (!TREE_CONSTANT (value))
7839 constructor_constant = 0;
7840 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7841 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7842 || TREE_CODE (constructor_type) == UNION_TYPE)
7843 && DECL_C_BIT_FIELD (field)
7844 && TREE_CODE (value) != INTEGER_CST))
7845 constructor_simple = 0;
7846 if (!maybe_const)
7847 constructor_nonconst = 1;
7849 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7851 if (require_constant_value)
7853 error_init ("initializer element is not constant");
7854 value = error_mark_node;
7856 else if (require_constant_elements)
7857 pedwarn (input_location, 0,
7858 "initializer element is not computable at load time");
7860 else if (!maybe_const
7861 && (require_constant_value || require_constant_elements))
7862 pedwarn_init (input_location, 0,
7863 "initializer element is not a constant expression");
7865 /* Issue -Wc++-compat warnings about initializing a bitfield with
7866 enum type. */
7867 if (warn_cxx_compat
7868 && field != NULL_TREE
7869 && TREE_CODE (field) == FIELD_DECL
7870 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7871 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7872 != TYPE_MAIN_VARIANT (type))
7873 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7875 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7876 if (checktype != error_mark_node
7877 && (TYPE_MAIN_VARIANT (checktype)
7878 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7879 warning_init (OPT_Wc___compat,
7880 "enum conversion in initialization is invalid in C++");
7883 /* If this field is empty (and not at the end of structure),
7884 don't do anything other than checking the initializer. */
7885 if (field
7886 && (TREE_TYPE (field) == error_mark_node
7887 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7888 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7889 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7890 || DECL_CHAIN (field)))))
7891 return;
7893 if (semantic_type)
7894 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7895 value = digest_init (input_location, type, value, origtype, npc,
7896 strict_string, require_constant_value);
7897 if (value == error_mark_node)
7899 constructor_erroneous = 1;
7900 return;
7902 if (require_constant_value || require_constant_elements)
7903 constant_expression_warning (value);
7905 /* If this element doesn't come next in sequence,
7906 put it on constructor_pending_elts. */
7907 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7908 && (!constructor_incremental
7909 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7911 if (constructor_incremental
7912 && tree_int_cst_lt (field, constructor_unfilled_index))
7913 set_nonincremental_init (braced_init_obstack);
7915 add_pending_init (field, value, origtype, implicit,
7916 braced_init_obstack);
7917 return;
7919 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7920 && (!constructor_incremental
7921 || field != constructor_unfilled_fields))
7923 /* We do this for records but not for unions. In a union,
7924 no matter which field is specified, it can be initialized
7925 right away since it starts at the beginning of the union. */
7926 if (constructor_incremental)
7928 if (!constructor_unfilled_fields)
7929 set_nonincremental_init (braced_init_obstack);
7930 else
7932 tree bitpos, unfillpos;
7934 bitpos = bit_position (field);
7935 unfillpos = bit_position (constructor_unfilled_fields);
7937 if (tree_int_cst_lt (bitpos, unfillpos))
7938 set_nonincremental_init (braced_init_obstack);
7942 add_pending_init (field, value, origtype, implicit,
7943 braced_init_obstack);
7944 return;
7946 else if (TREE_CODE (constructor_type) == UNION_TYPE
7947 && !VEC_empty (constructor_elt, constructor_elements))
7949 if (!implicit)
7951 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7952 constructor_elements)->value))
7953 warning_init (0,
7954 "initialized field with side-effects overwritten");
7955 else if (warn_override_init)
7956 warning_init (OPT_Woverride_init, "initialized field overwritten");
7959 /* We can have just one union field set. */
7960 constructor_elements = 0;
7963 /* Otherwise, output this element either to
7964 constructor_elements or to the assembler file. */
7966 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7967 celt->index = field;
7968 celt->value = value;
7970 /* Advance the variable that indicates sequential elements output. */
7971 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7972 constructor_unfilled_index
7973 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7974 bitsize_one_node);
7975 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7977 constructor_unfilled_fields
7978 = DECL_CHAIN (constructor_unfilled_fields);
7980 /* Skip any nameless bit fields. */
7981 while (constructor_unfilled_fields != 0
7982 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7983 && DECL_NAME (constructor_unfilled_fields) == 0)
7984 constructor_unfilled_fields =
7985 DECL_CHAIN (constructor_unfilled_fields);
7987 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7988 constructor_unfilled_fields = 0;
7990 /* Now output any pending elements which have become next. */
7991 if (pending)
7992 output_pending_init_elements (0, braced_init_obstack);
7995 /* Output any pending elements which have become next.
7996 As we output elements, constructor_unfilled_{fields,index}
7997 advances, which may cause other elements to become next;
7998 if so, they too are output.
8000 If ALL is 0, we return when there are
8001 no more pending elements to output now.
8003 If ALL is 1, we output space as necessary so that
8004 we can output all the pending elements. */
8005 static void
8006 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8008 struct init_node *elt = constructor_pending_elts;
8009 tree next;
8011 retry:
8013 /* Look through the whole pending tree.
8014 If we find an element that should be output now,
8015 output it. Otherwise, set NEXT to the element
8016 that comes first among those still pending. */
8018 next = 0;
8019 while (elt)
8021 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8023 if (tree_int_cst_equal (elt->purpose,
8024 constructor_unfilled_index))
8025 output_init_element (elt->value, elt->origtype, true,
8026 TREE_TYPE (constructor_type),
8027 constructor_unfilled_index, 0, false,
8028 braced_init_obstack);
8029 else if (tree_int_cst_lt (constructor_unfilled_index,
8030 elt->purpose))
8032 /* Advance to the next smaller node. */
8033 if (elt->left)
8034 elt = elt->left;
8035 else
8037 /* We have reached the smallest node bigger than the
8038 current unfilled index. Fill the space first. */
8039 next = elt->purpose;
8040 break;
8043 else
8045 /* Advance to the next bigger node. */
8046 if (elt->right)
8047 elt = elt->right;
8048 else
8050 /* We have reached the biggest node in a subtree. Find
8051 the parent of it, which is the next bigger node. */
8052 while (elt->parent && elt->parent->right == elt)
8053 elt = elt->parent;
8054 elt = elt->parent;
8055 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8056 elt->purpose))
8058 next = elt->purpose;
8059 break;
8064 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8065 || TREE_CODE (constructor_type) == UNION_TYPE)
8067 tree ctor_unfilled_bitpos, elt_bitpos;
8069 /* If the current record is complete we are done. */
8070 if (constructor_unfilled_fields == 0)
8071 break;
8073 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8074 elt_bitpos = bit_position (elt->purpose);
8075 /* We can't compare fields here because there might be empty
8076 fields in between. */
8077 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8079 constructor_unfilled_fields = elt->purpose;
8080 output_init_element (elt->value, elt->origtype, true,
8081 TREE_TYPE (elt->purpose),
8082 elt->purpose, 0, false,
8083 braced_init_obstack);
8085 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8087 /* Advance to the next smaller node. */
8088 if (elt->left)
8089 elt = elt->left;
8090 else
8092 /* We have reached the smallest node bigger than the
8093 current unfilled field. Fill the space first. */
8094 next = elt->purpose;
8095 break;
8098 else
8100 /* Advance to the next bigger node. */
8101 if (elt->right)
8102 elt = elt->right;
8103 else
8105 /* We have reached the biggest node in a subtree. Find
8106 the parent of it, which is the next bigger node. */
8107 while (elt->parent && elt->parent->right == elt)
8108 elt = elt->parent;
8109 elt = elt->parent;
8110 if (elt
8111 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8112 bit_position (elt->purpose))))
8114 next = elt->purpose;
8115 break;
8122 /* Ordinarily return, but not if we want to output all
8123 and there are elements left. */
8124 if (!(all && next != 0))
8125 return;
8127 /* If it's not incremental, just skip over the gap, so that after
8128 jumping to retry we will output the next successive element. */
8129 if (TREE_CODE (constructor_type) == RECORD_TYPE
8130 || TREE_CODE (constructor_type) == UNION_TYPE)
8131 constructor_unfilled_fields = next;
8132 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8133 constructor_unfilled_index = next;
8135 /* ELT now points to the node in the pending tree with the next
8136 initializer to output. */
8137 goto retry;
8140 /* Add one non-braced element to the current constructor level.
8141 This adjusts the current position within the constructor's type.
8142 This may also start or terminate implicit levels
8143 to handle a partly-braced initializer.
8145 Once this has found the correct level for the new element,
8146 it calls output_init_element.
8148 IMPLICIT is true if value comes from pop_init_level (1),
8149 the new initializer has been merged with the existing one
8150 and thus no warnings should be emitted about overriding an
8151 existing initializer. */
8153 void
8154 process_init_element (struct c_expr value, bool implicit,
8155 struct obstack * braced_init_obstack)
8157 tree orig_value = value.value;
8158 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8159 bool strict_string = value.original_code == STRING_CST;
8161 designator_depth = 0;
8162 designator_erroneous = 0;
8164 /* Handle superfluous braces around string cst as in
8165 char x[] = {"foo"}; */
8166 if (string_flag
8167 && constructor_type
8168 && TREE_CODE (constructor_type) == ARRAY_TYPE
8169 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8170 && integer_zerop (constructor_unfilled_index))
8172 if (constructor_stack->replacement_value.value)
8173 error_init ("excess elements in char array initializer");
8174 constructor_stack->replacement_value = value;
8175 return;
8178 if (constructor_stack->replacement_value.value != 0)
8180 error_init ("excess elements in struct initializer");
8181 return;
8184 /* Ignore elements of a brace group if it is entirely superfluous
8185 and has already been diagnosed. */
8186 if (constructor_type == 0)
8187 return;
8189 /* If we've exhausted any levels that didn't have braces,
8190 pop them now. */
8191 while (constructor_stack->implicit)
8193 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8194 || TREE_CODE (constructor_type) == UNION_TYPE)
8195 && constructor_fields == 0)
8196 process_init_element (pop_init_level (1, braced_init_obstack),
8197 true, braced_init_obstack);
8198 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8199 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8200 && (constructor_max_index == 0
8201 || tree_int_cst_lt (constructor_max_index,
8202 constructor_index)))
8203 process_init_element (pop_init_level (1, braced_init_obstack),
8204 true, braced_init_obstack);
8205 else
8206 break;
8209 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8210 if (constructor_range_stack)
8212 /* If value is a compound literal and we'll be just using its
8213 content, don't put it into a SAVE_EXPR. */
8214 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8215 || !require_constant_value
8216 || flag_isoc99)
8218 tree semantic_type = NULL_TREE;
8219 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8221 semantic_type = TREE_TYPE (value.value);
8222 value.value = TREE_OPERAND (value.value, 0);
8224 value.value = c_save_expr (value.value);
8225 if (semantic_type)
8226 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8227 value.value);
8231 while (1)
8233 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8235 tree fieldtype;
8236 enum tree_code fieldcode;
8238 if (constructor_fields == 0)
8240 pedwarn_init (input_location, 0,
8241 "excess elements in struct initializer");
8242 break;
8245 fieldtype = TREE_TYPE (constructor_fields);
8246 if (fieldtype != error_mark_node)
8247 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8248 fieldcode = TREE_CODE (fieldtype);
8250 /* Error for non-static initialization of a flexible array member. */
8251 if (fieldcode == ARRAY_TYPE
8252 && !require_constant_value
8253 && TYPE_SIZE (fieldtype) == NULL_TREE
8254 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8256 error_init ("non-static initialization of a flexible array member");
8257 break;
8260 /* Accept a string constant to initialize a subarray. */
8261 if (value.value != 0
8262 && fieldcode == ARRAY_TYPE
8263 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8264 && string_flag)
8265 value.value = orig_value;
8266 /* Otherwise, if we have come to a subaggregate,
8267 and we don't have an element of its type, push into it. */
8268 else if (value.value != 0
8269 && value.value != error_mark_node
8270 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8271 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8272 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8274 push_init_level (1, braced_init_obstack);
8275 continue;
8278 if (value.value)
8280 push_member_name (constructor_fields);
8281 output_init_element (value.value, value.original_type,
8282 strict_string, fieldtype,
8283 constructor_fields, 1, implicit,
8284 braced_init_obstack);
8285 RESTORE_SPELLING_DEPTH (constructor_depth);
8287 else
8288 /* Do the bookkeeping for an element that was
8289 directly output as a constructor. */
8291 /* For a record, keep track of end position of last field. */
8292 if (DECL_SIZE (constructor_fields))
8293 constructor_bit_index
8294 = size_binop_loc (input_location, PLUS_EXPR,
8295 bit_position (constructor_fields),
8296 DECL_SIZE (constructor_fields));
8298 /* If the current field was the first one not yet written out,
8299 it isn't now, so update. */
8300 if (constructor_unfilled_fields == constructor_fields)
8302 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8303 /* Skip any nameless bit fields. */
8304 while (constructor_unfilled_fields != 0
8305 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8306 && DECL_NAME (constructor_unfilled_fields) == 0)
8307 constructor_unfilled_fields =
8308 DECL_CHAIN (constructor_unfilled_fields);
8312 constructor_fields = DECL_CHAIN (constructor_fields);
8313 /* Skip any nameless bit fields at the beginning. */
8314 while (constructor_fields != 0
8315 && DECL_C_BIT_FIELD (constructor_fields)
8316 && DECL_NAME (constructor_fields) == 0)
8317 constructor_fields = DECL_CHAIN (constructor_fields);
8319 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8321 tree fieldtype;
8322 enum tree_code fieldcode;
8324 if (constructor_fields == 0)
8326 pedwarn_init (input_location, 0,
8327 "excess elements in union initializer");
8328 break;
8331 fieldtype = TREE_TYPE (constructor_fields);
8332 if (fieldtype != error_mark_node)
8333 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8334 fieldcode = TREE_CODE (fieldtype);
8336 /* Warn that traditional C rejects initialization of unions.
8337 We skip the warning if the value is zero. This is done
8338 under the assumption that the zero initializer in user
8339 code appears conditioned on e.g. __STDC__ to avoid
8340 "missing initializer" warnings and relies on default
8341 initialization to zero in the traditional C case.
8342 We also skip the warning if the initializer is designated,
8343 again on the assumption that this must be conditional on
8344 __STDC__ anyway (and we've already complained about the
8345 member-designator already). */
8346 if (!in_system_header && !constructor_designated
8347 && !(value.value && (integer_zerop (value.value)
8348 || real_zerop (value.value))))
8349 warning (OPT_Wtraditional, "traditional C rejects initialization "
8350 "of unions");
8352 /* Accept a string constant to initialize a subarray. */
8353 if (value.value != 0
8354 && fieldcode == ARRAY_TYPE
8355 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8356 && string_flag)
8357 value.value = orig_value;
8358 /* Otherwise, if we have come to a subaggregate,
8359 and we don't have an element of its type, push into it. */
8360 else if (value.value != 0
8361 && value.value != error_mark_node
8362 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8363 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8364 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8366 push_init_level (1, braced_init_obstack);
8367 continue;
8370 if (value.value)
8372 push_member_name (constructor_fields);
8373 output_init_element (value.value, value.original_type,
8374 strict_string, fieldtype,
8375 constructor_fields, 1, implicit,
8376 braced_init_obstack);
8377 RESTORE_SPELLING_DEPTH (constructor_depth);
8379 else
8380 /* Do the bookkeeping for an element that was
8381 directly output as a constructor. */
8383 constructor_bit_index = DECL_SIZE (constructor_fields);
8384 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8387 constructor_fields = 0;
8389 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8391 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8392 enum tree_code eltcode = TREE_CODE (elttype);
8394 /* Accept a string constant to initialize a subarray. */
8395 if (value.value != 0
8396 && eltcode == ARRAY_TYPE
8397 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8398 && string_flag)
8399 value.value = orig_value;
8400 /* Otherwise, if we have come to a subaggregate,
8401 and we don't have an element of its type, push into it. */
8402 else if (value.value != 0
8403 && value.value != error_mark_node
8404 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8405 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8406 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8408 push_init_level (1, braced_init_obstack);
8409 continue;
8412 if (constructor_max_index != 0
8413 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8414 || integer_all_onesp (constructor_max_index)))
8416 pedwarn_init (input_location, 0,
8417 "excess elements in array initializer");
8418 break;
8421 /* Now output the actual element. */
8422 if (value.value)
8424 push_array_bounds (tree_low_cst (constructor_index, 1));
8425 output_init_element (value.value, value.original_type,
8426 strict_string, elttype,
8427 constructor_index, 1, implicit,
8428 braced_init_obstack);
8429 RESTORE_SPELLING_DEPTH (constructor_depth);
8432 constructor_index
8433 = size_binop_loc (input_location, PLUS_EXPR,
8434 constructor_index, bitsize_one_node);
8436 if (!value.value)
8437 /* If we are doing the bookkeeping for an element that was
8438 directly output as a constructor, we must update
8439 constructor_unfilled_index. */
8440 constructor_unfilled_index = constructor_index;
8442 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8444 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8446 /* Do a basic check of initializer size. Note that vectors
8447 always have a fixed size derived from their type. */
8448 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8450 pedwarn_init (input_location, 0,
8451 "excess elements in vector initializer");
8452 break;
8455 /* Now output the actual element. */
8456 if (value.value)
8458 if (TREE_CODE (value.value) == VECTOR_CST)
8459 elttype = TYPE_MAIN_VARIANT (constructor_type);
8460 output_init_element (value.value, value.original_type,
8461 strict_string, elttype,
8462 constructor_index, 1, implicit,
8463 braced_init_obstack);
8466 constructor_index
8467 = size_binop_loc (input_location,
8468 PLUS_EXPR, constructor_index, bitsize_one_node);
8470 if (!value.value)
8471 /* If we are doing the bookkeeping for an element that was
8472 directly output as a constructor, we must update
8473 constructor_unfilled_index. */
8474 constructor_unfilled_index = constructor_index;
8477 /* Handle the sole element allowed in a braced initializer
8478 for a scalar variable. */
8479 else if (constructor_type != error_mark_node
8480 && constructor_fields == 0)
8482 pedwarn_init (input_location, 0,
8483 "excess elements in scalar initializer");
8484 break;
8486 else
8488 if (value.value)
8489 output_init_element (value.value, value.original_type,
8490 strict_string, constructor_type,
8491 NULL_TREE, 1, implicit,
8492 braced_init_obstack);
8493 constructor_fields = 0;
8496 /* Handle range initializers either at this level or anywhere higher
8497 in the designator stack. */
8498 if (constructor_range_stack)
8500 struct constructor_range_stack *p, *range_stack;
8501 int finish = 0;
8503 range_stack = constructor_range_stack;
8504 constructor_range_stack = 0;
8505 while (constructor_stack != range_stack->stack)
8507 gcc_assert (constructor_stack->implicit);
8508 process_init_element (pop_init_level (1,
8509 braced_init_obstack),
8510 true, braced_init_obstack);
8512 for (p = range_stack;
8513 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8514 p = p->prev)
8516 gcc_assert (constructor_stack->implicit);
8517 process_init_element (pop_init_level (1, braced_init_obstack),
8518 true, braced_init_obstack);
8521 p->index = size_binop_loc (input_location,
8522 PLUS_EXPR, p->index, bitsize_one_node);
8523 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8524 finish = 1;
8526 while (1)
8528 constructor_index = p->index;
8529 constructor_fields = p->fields;
8530 if (finish && p->range_end && p->index == p->range_start)
8532 finish = 0;
8533 p->prev = 0;
8535 p = p->next;
8536 if (!p)
8537 break;
8538 push_init_level (2, braced_init_obstack);
8539 p->stack = constructor_stack;
8540 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8541 p->index = p->range_start;
8544 if (!finish)
8545 constructor_range_stack = range_stack;
8546 continue;
8549 break;
8552 constructor_range_stack = 0;
8555 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8556 (guaranteed to be 'volatile' or null) and ARGS (represented using
8557 an ASM_EXPR node). */
8558 tree
8559 build_asm_stmt (tree cv_qualifier, tree args)
8561 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8562 ASM_VOLATILE_P (args) = 1;
8563 return add_stmt (args);
8566 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8567 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8568 SIMPLE indicates whether there was anything at all after the
8569 string in the asm expression -- asm("blah") and asm("blah" : )
8570 are subtly different. We use a ASM_EXPR node to represent this. */
8571 tree
8572 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8573 tree clobbers, tree labels, bool simple)
8575 tree tail;
8576 tree args;
8577 int i;
8578 const char *constraint;
8579 const char **oconstraints;
8580 bool allows_mem, allows_reg, is_inout;
8581 int ninputs, noutputs;
8583 ninputs = list_length (inputs);
8584 noutputs = list_length (outputs);
8585 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8587 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8589 /* Remove output conversions that change the type but not the mode. */
8590 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8592 tree output = TREE_VALUE (tail);
8594 /* ??? Really, this should not be here. Users should be using a
8595 proper lvalue, dammit. But there's a long history of using casts
8596 in the output operands. In cases like longlong.h, this becomes a
8597 primitive form of typechecking -- if the cast can be removed, then
8598 the output operand had a type of the proper width; otherwise we'll
8599 get an error. Gross, but ... */
8600 STRIP_NOPS (output);
8602 if (!lvalue_or_else (loc, output, lv_asm))
8603 output = error_mark_node;
8605 if (output != error_mark_node
8606 && (TREE_READONLY (output)
8607 || TYPE_READONLY (TREE_TYPE (output))
8608 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8609 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8610 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8611 readonly_error (output, lv_asm);
8613 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8614 oconstraints[i] = constraint;
8616 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8617 &allows_mem, &allows_reg, &is_inout))
8619 /* If the operand is going to end up in memory,
8620 mark it addressable. */
8621 if (!allows_reg && !c_mark_addressable (output))
8622 output = error_mark_node;
8623 if (!(!allows_reg && allows_mem)
8624 && output != error_mark_node
8625 && VOID_TYPE_P (TREE_TYPE (output)))
8627 error_at (loc, "invalid use of void expression");
8628 output = error_mark_node;
8631 else
8632 output = error_mark_node;
8634 TREE_VALUE (tail) = output;
8637 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8639 tree input;
8641 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8642 input = TREE_VALUE (tail);
8644 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8645 oconstraints, &allows_mem, &allows_reg))
8647 /* If the operand is going to end up in memory,
8648 mark it addressable. */
8649 if (!allows_reg && allows_mem)
8651 /* Strip the nops as we allow this case. FIXME, this really
8652 should be rejected or made deprecated. */
8653 STRIP_NOPS (input);
8654 if (!c_mark_addressable (input))
8655 input = error_mark_node;
8657 else if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8659 error_at (loc, "invalid use of void expression");
8660 input = error_mark_node;
8663 else
8664 input = error_mark_node;
8666 TREE_VALUE (tail) = input;
8669 /* ASMs with labels cannot have outputs. This should have been
8670 enforced by the parser. */
8671 gcc_assert (outputs == NULL || labels == NULL);
8673 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8675 /* asm statements without outputs, including simple ones, are treated
8676 as volatile. */
8677 ASM_INPUT_P (args) = simple;
8678 ASM_VOLATILE_P (args) = (noutputs == 0);
8680 return args;
8683 /* Generate a goto statement to LABEL. LOC is the location of the
8684 GOTO. */
8686 tree
8687 c_finish_goto_label (location_t loc, tree label)
8689 tree decl = lookup_label_for_goto (loc, label);
8690 if (!decl)
8691 return NULL_TREE;
8692 TREE_USED (decl) = 1;
8694 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8695 SET_EXPR_LOCATION (t, loc);
8696 return add_stmt (t);
8700 /* Generate a computed goto statement to EXPR. LOC is the location of
8701 the GOTO. */
8703 tree
8704 c_finish_goto_ptr (location_t loc, tree expr)
8706 tree t;
8707 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
8708 expr = c_fully_fold (expr, false, NULL);
8709 expr = convert (ptr_type_node, expr);
8710 t = build1 (GOTO_EXPR, void_type_node, expr);
8711 SET_EXPR_LOCATION (t, loc);
8712 return add_stmt (t);
8715 /* Generate a C `return' statement. RETVAL is the expression for what
8716 to return, or a null pointer for `return;' with no value. LOC is
8717 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8718 is the original type of RETVAL. */
8720 tree
8721 c_finish_return (location_t loc, tree retval, tree origtype)
8723 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8724 bool no_warning = false;
8725 bool npc = false;
8727 if (TREE_THIS_VOLATILE (current_function_decl))
8728 warning_at (loc, 0,
8729 "function declared %<noreturn%> has a %<return%> statement");
8731 if (retval)
8733 tree semantic_type = NULL_TREE;
8734 npc = null_pointer_constant_p (retval);
8735 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8737 semantic_type = TREE_TYPE (retval);
8738 retval = TREE_OPERAND (retval, 0);
8740 retval = c_fully_fold (retval, false, NULL);
8741 if (semantic_type)
8742 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8745 if (!retval)
8747 current_function_returns_null = 1;
8748 if ((warn_return_type || flag_isoc99)
8749 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8751 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8752 "%<return%> with no value, in "
8753 "function returning non-void");
8754 no_warning = true;
8757 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8759 current_function_returns_null = 1;
8760 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8761 pedwarn (loc, 0,
8762 "%<return%> with a value, in function returning void");
8763 else
8764 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
8765 "%<return%> with expression, in function returning void");
8767 else
8769 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8770 ic_return,
8771 npc, NULL_TREE, NULL_TREE, 0);
8772 tree res = DECL_RESULT (current_function_decl);
8773 tree inner;
8775 current_function_returns_value = 1;
8776 if (t == error_mark_node)
8777 return NULL_TREE;
8779 inner = t = convert (TREE_TYPE (res), t);
8781 /* Strip any conversions, additions, and subtractions, and see if
8782 we are returning the address of a local variable. Warn if so. */
8783 while (1)
8785 switch (TREE_CODE (inner))
8787 CASE_CONVERT:
8788 case NON_LVALUE_EXPR:
8789 case PLUS_EXPR:
8790 case POINTER_PLUS_EXPR:
8791 inner = TREE_OPERAND (inner, 0);
8792 continue;
8794 case MINUS_EXPR:
8795 /* If the second operand of the MINUS_EXPR has a pointer
8796 type (or is converted from it), this may be valid, so
8797 don't give a warning. */
8799 tree op1 = TREE_OPERAND (inner, 1);
8801 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8802 && (CONVERT_EXPR_P (op1)
8803 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8804 op1 = TREE_OPERAND (op1, 0);
8806 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8807 break;
8809 inner = TREE_OPERAND (inner, 0);
8810 continue;
8813 case ADDR_EXPR:
8814 inner = TREE_OPERAND (inner, 0);
8816 while (REFERENCE_CLASS_P (inner)
8817 && TREE_CODE (inner) != INDIRECT_REF)
8818 inner = TREE_OPERAND (inner, 0);
8820 if (DECL_P (inner)
8821 && !DECL_EXTERNAL (inner)
8822 && !TREE_STATIC (inner)
8823 && DECL_CONTEXT (inner) == current_function_decl)
8824 warning_at (loc,
8825 0, "function returns address of local variable");
8826 break;
8828 default:
8829 break;
8832 break;
8835 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8836 SET_EXPR_LOCATION (retval, loc);
8838 if (warn_sequence_point)
8839 verify_sequence_points (retval);
8842 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8843 TREE_NO_WARNING (ret_stmt) |= no_warning;
8844 return add_stmt (ret_stmt);
8847 struct c_switch {
8848 /* The SWITCH_EXPR being built. */
8849 tree switch_expr;
8851 /* The original type of the testing expression, i.e. before the
8852 default conversion is applied. */
8853 tree orig_type;
8855 /* A splay-tree mapping the low element of a case range to the high
8856 element, or NULL_TREE if there is no high element. Used to
8857 determine whether or not a new case label duplicates an old case
8858 label. We need a tree, rather than simply a hash table, because
8859 of the GNU case range extension. */
8860 splay_tree cases;
8862 /* The bindings at the point of the switch. This is used for
8863 warnings crossing decls when branching to a case label. */
8864 struct c_spot_bindings *bindings;
8866 /* The next node on the stack. */
8867 struct c_switch *next;
8870 /* A stack of the currently active switch statements. The innermost
8871 switch statement is on the top of the stack. There is no need to
8872 mark the stack for garbage collection because it is only active
8873 during the processing of the body of a function, and we never
8874 collect at that point. */
8876 struct c_switch *c_switch_stack;
8878 /* Start a C switch statement, testing expression EXP. Return the new
8879 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8880 SWITCH_COND_LOC is the location of the switch's condition. */
8882 tree
8883 c_start_case (location_t switch_loc,
8884 location_t switch_cond_loc,
8885 tree exp)
8887 tree orig_type = error_mark_node;
8888 struct c_switch *cs;
8890 if (exp != error_mark_node)
8892 orig_type = TREE_TYPE (exp);
8894 if (!INTEGRAL_TYPE_P (orig_type))
8896 if (orig_type != error_mark_node)
8898 error_at (switch_cond_loc, "switch quantity not an integer");
8899 orig_type = error_mark_node;
8901 exp = integer_zero_node;
8903 else
8905 tree type = TYPE_MAIN_VARIANT (orig_type);
8907 if (!in_system_header
8908 && (type == long_integer_type_node
8909 || type == long_unsigned_type_node))
8910 warning_at (switch_cond_loc,
8911 OPT_Wtraditional, "%<long%> switch expression not "
8912 "converted to %<int%> in ISO C");
8914 exp = c_fully_fold (exp, false, NULL);
8915 exp = default_conversion (exp);
8917 if (warn_sequence_point)
8918 verify_sequence_points (exp);
8922 /* Add this new SWITCH_EXPR to the stack. */
8923 cs = XNEW (struct c_switch);
8924 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8925 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8926 cs->orig_type = orig_type;
8927 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8928 cs->bindings = c_get_switch_bindings ();
8929 cs->next = c_switch_stack;
8930 c_switch_stack = cs;
8932 return add_stmt (cs->switch_expr);
8935 /* Process a case label at location LOC. */
8937 tree
8938 do_case (location_t loc, tree low_value, tree high_value)
8940 tree label = NULL_TREE;
8942 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8944 low_value = c_fully_fold (low_value, false, NULL);
8945 if (TREE_CODE (low_value) == INTEGER_CST)
8946 pedwarn (input_location, OPT_Wpedantic,
8947 "case label is not an integer constant expression");
8950 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8952 high_value = c_fully_fold (high_value, false, NULL);
8953 if (TREE_CODE (high_value) == INTEGER_CST)
8954 pedwarn (input_location, OPT_Wpedantic,
8955 "case label is not an integer constant expression");
8958 if (c_switch_stack == NULL)
8960 if (low_value)
8961 error_at (loc, "case label not within a switch statement");
8962 else
8963 error_at (loc, "%<default%> label not within a switch statement");
8964 return NULL_TREE;
8967 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8968 EXPR_LOCATION (c_switch_stack->switch_expr),
8969 loc))
8970 return NULL_TREE;
8972 label = c_add_case_label (loc, c_switch_stack->cases,
8973 SWITCH_COND (c_switch_stack->switch_expr),
8974 c_switch_stack->orig_type,
8975 low_value, high_value);
8976 if (label == error_mark_node)
8977 label = NULL_TREE;
8978 return label;
8981 /* Finish the switch statement. */
8983 void
8984 c_finish_case (tree body)
8986 struct c_switch *cs = c_switch_stack;
8987 location_t switch_location;
8989 SWITCH_BODY (cs->switch_expr) = body;
8991 /* Emit warnings as needed. */
8992 switch_location = EXPR_LOCATION (cs->switch_expr);
8993 c_do_switch_warnings (cs->cases, switch_location,
8994 TREE_TYPE (cs->switch_expr),
8995 SWITCH_COND (cs->switch_expr));
8997 /* Pop the stack. */
8998 c_switch_stack = cs->next;
8999 splay_tree_delete (cs->cases);
9000 c_release_switch_bindings (cs->bindings);
9001 XDELETE (cs);
9004 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9005 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9006 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9007 statement, and was not surrounded with parenthesis. */
9009 void
9010 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9011 tree else_block, bool nested_if)
9013 tree stmt;
9015 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9016 if (warn_parentheses && nested_if && else_block == NULL)
9018 tree inner_if = then_block;
9020 /* We know from the grammar productions that there is an IF nested
9021 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9022 it might not be exactly THEN_BLOCK, but should be the last
9023 non-container statement within. */
9024 while (1)
9025 switch (TREE_CODE (inner_if))
9027 case COND_EXPR:
9028 goto found;
9029 case BIND_EXPR:
9030 inner_if = BIND_EXPR_BODY (inner_if);
9031 break;
9032 case STATEMENT_LIST:
9033 inner_if = expr_last (then_block);
9034 break;
9035 case TRY_FINALLY_EXPR:
9036 case TRY_CATCH_EXPR:
9037 inner_if = TREE_OPERAND (inner_if, 0);
9038 break;
9039 default:
9040 gcc_unreachable ();
9042 found:
9044 if (COND_EXPR_ELSE (inner_if))
9045 warning_at (if_locus, OPT_Wparentheses,
9046 "suggest explicit braces to avoid ambiguous %<else%>");
9049 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9050 SET_EXPR_LOCATION (stmt, if_locus);
9051 add_stmt (stmt);
9054 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9055 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9056 is false for DO loops. INCR is the FOR increment expression. BODY is
9057 the statement controlled by the loop. BLAB is the break label. CLAB is
9058 the continue label. Everything is allowed to be NULL. */
9060 void
9061 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9062 tree blab, tree clab, bool cond_is_first)
9064 tree entry = NULL, exit = NULL, t;
9066 /* If the condition is zero don't generate a loop construct. */
9067 if (cond && integer_zerop (cond))
9069 if (cond_is_first)
9071 t = build_and_jump (&blab);
9072 SET_EXPR_LOCATION (t, start_locus);
9073 add_stmt (t);
9076 else
9078 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9080 /* If we have an exit condition, then we build an IF with gotos either
9081 out of the loop, or to the top of it. If there's no exit condition,
9082 then we just build a jump back to the top. */
9083 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9085 if (cond && !integer_nonzerop (cond))
9087 /* Canonicalize the loop condition to the end. This means
9088 generating a branch to the loop condition. Reuse the
9089 continue label, if possible. */
9090 if (cond_is_first)
9092 if (incr || !clab)
9094 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9095 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9097 else
9098 t = build1 (GOTO_EXPR, void_type_node, clab);
9099 SET_EXPR_LOCATION (t, start_locus);
9100 add_stmt (t);
9103 t = build_and_jump (&blab);
9104 if (cond_is_first)
9105 exit = fold_build3_loc (start_locus,
9106 COND_EXPR, void_type_node, cond, exit, t);
9107 else
9108 exit = fold_build3_loc (input_location,
9109 COND_EXPR, void_type_node, cond, exit, t);
9112 add_stmt (top);
9115 if (body)
9116 add_stmt (body);
9117 if (clab)
9118 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9119 if (incr)
9120 add_stmt (incr);
9121 if (entry)
9122 add_stmt (entry);
9123 if (exit)
9124 add_stmt (exit);
9125 if (blab)
9126 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9129 tree
9130 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9132 bool skip;
9133 tree label = *label_p;
9135 /* In switch statements break is sometimes stylistically used after
9136 a return statement. This can lead to spurious warnings about
9137 control reaching the end of a non-void function when it is
9138 inlined. Note that we are calling block_may_fallthru with
9139 language specific tree nodes; this works because
9140 block_may_fallthru returns true when given something it does not
9141 understand. */
9142 skip = !block_may_fallthru (cur_stmt_list);
9144 if (!label)
9146 if (!skip)
9147 *label_p = label = create_artificial_label (loc);
9149 else if (TREE_CODE (label) == LABEL_DECL)
9151 else switch (TREE_INT_CST_LOW (label))
9153 case 0:
9154 if (is_break)
9155 error_at (loc, "break statement not within loop or switch");
9156 else
9157 error_at (loc, "continue statement not within a loop");
9158 return NULL_TREE;
9160 case 1:
9161 gcc_assert (is_break);
9162 error_at (loc, "break statement used with OpenMP for loop");
9163 return NULL_TREE;
9165 default:
9166 gcc_unreachable ();
9169 if (skip)
9170 return NULL_TREE;
9172 if (!is_break)
9173 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9175 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9178 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9180 static void
9181 emit_side_effect_warnings (location_t loc, tree expr)
9183 if (expr == error_mark_node)
9185 else if (!TREE_SIDE_EFFECTS (expr))
9187 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9188 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9190 else
9191 warn_if_unused_value (expr, loc);
9194 /* Process an expression as if it were a complete statement. Emit
9195 diagnostics, but do not call ADD_STMT. LOC is the location of the
9196 statement. */
9198 tree
9199 c_process_expr_stmt (location_t loc, tree expr)
9201 tree exprv;
9203 if (!expr)
9204 return NULL_TREE;
9206 expr = c_fully_fold (expr, false, NULL);
9208 if (warn_sequence_point)
9209 verify_sequence_points (expr);
9211 if (TREE_TYPE (expr) != error_mark_node
9212 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9213 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9214 error_at (loc, "expression statement has incomplete type");
9216 /* If we're not processing a statement expression, warn about unused values.
9217 Warnings for statement expressions will be emitted later, once we figure
9218 out which is the result. */
9219 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9220 && warn_unused_value)
9221 emit_side_effect_warnings (loc, expr);
9223 exprv = expr;
9224 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9225 exprv = TREE_OPERAND (exprv, 1);
9226 while (CONVERT_EXPR_P (exprv))
9227 exprv = TREE_OPERAND (exprv, 0);
9228 if (DECL_P (exprv)
9229 || handled_component_p (exprv)
9230 || TREE_CODE (exprv) == ADDR_EXPR)
9231 mark_exp_read (exprv);
9233 /* If the expression is not of a type to which we cannot assign a line
9234 number, wrap the thing in a no-op NOP_EXPR. */
9235 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9237 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9238 SET_EXPR_LOCATION (expr, loc);
9241 return expr;
9244 /* Emit an expression as a statement. LOC is the location of the
9245 expression. */
9247 tree
9248 c_finish_expr_stmt (location_t loc, tree expr)
9250 if (expr)
9251 return add_stmt (c_process_expr_stmt (loc, expr));
9252 else
9253 return NULL;
9256 /* Do the opposite and emit a statement as an expression. To begin,
9257 create a new binding level and return it. */
9259 tree
9260 c_begin_stmt_expr (void)
9262 tree ret;
9264 /* We must force a BLOCK for this level so that, if it is not expanded
9265 later, there is a way to turn off the entire subtree of blocks that
9266 are contained in it. */
9267 keep_next_level ();
9268 ret = c_begin_compound_stmt (true);
9270 c_bindings_start_stmt_expr (c_switch_stack == NULL
9271 ? NULL
9272 : c_switch_stack->bindings);
9274 /* Mark the current statement list as belonging to a statement list. */
9275 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9277 return ret;
9280 /* LOC is the location of the compound statement to which this body
9281 belongs. */
9283 tree
9284 c_finish_stmt_expr (location_t loc, tree body)
9286 tree last, type, tmp, val;
9287 tree *last_p;
9289 body = c_end_compound_stmt (loc, body, true);
9291 c_bindings_end_stmt_expr (c_switch_stack == NULL
9292 ? NULL
9293 : c_switch_stack->bindings);
9295 /* Locate the last statement in BODY. See c_end_compound_stmt
9296 about always returning a BIND_EXPR. */
9297 last_p = &BIND_EXPR_BODY (body);
9298 last = BIND_EXPR_BODY (body);
9300 continue_searching:
9301 if (TREE_CODE (last) == STATEMENT_LIST)
9303 tree_stmt_iterator i;
9305 /* This can happen with degenerate cases like ({ }). No value. */
9306 if (!TREE_SIDE_EFFECTS (last))
9307 return body;
9309 /* If we're supposed to generate side effects warnings, process
9310 all of the statements except the last. */
9311 if (warn_unused_value)
9313 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9315 location_t tloc;
9316 tree t = tsi_stmt (i);
9318 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9319 emit_side_effect_warnings (tloc, t);
9322 else
9323 i = tsi_last (last);
9324 last_p = tsi_stmt_ptr (i);
9325 last = *last_p;
9328 /* If the end of the list is exception related, then the list was split
9329 by a call to push_cleanup. Continue searching. */
9330 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9331 || TREE_CODE (last) == TRY_CATCH_EXPR)
9333 last_p = &TREE_OPERAND (last, 0);
9334 last = *last_p;
9335 goto continue_searching;
9338 if (last == error_mark_node)
9339 return last;
9341 /* In the case that the BIND_EXPR is not necessary, return the
9342 expression out from inside it. */
9343 if (last == BIND_EXPR_BODY (body)
9344 && BIND_EXPR_VARS (body) == NULL)
9346 /* Even if this looks constant, do not allow it in a constant
9347 expression. */
9348 last = c_wrap_maybe_const (last, true);
9349 /* Do not warn if the return value of a statement expression is
9350 unused. */
9351 TREE_NO_WARNING (last) = 1;
9352 return last;
9355 /* Extract the type of said expression. */
9356 type = TREE_TYPE (last);
9358 /* If we're not returning a value at all, then the BIND_EXPR that
9359 we already have is a fine expression to return. */
9360 if (!type || VOID_TYPE_P (type))
9361 return body;
9363 /* Now that we've located the expression containing the value, it seems
9364 silly to make voidify_wrapper_expr repeat the process. Create a
9365 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9366 tmp = create_tmp_var_raw (type, NULL);
9368 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9369 tree_expr_nonnegative_p giving up immediately. */
9370 val = last;
9371 if (TREE_CODE (val) == NOP_EXPR
9372 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9373 val = TREE_OPERAND (val, 0);
9375 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9376 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9379 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9380 SET_EXPR_LOCATION (t, loc);
9381 return t;
9385 /* Begin and end compound statements. This is as simple as pushing
9386 and popping new statement lists from the tree. */
9388 tree
9389 c_begin_compound_stmt (bool do_scope)
9391 tree stmt = push_stmt_list ();
9392 if (do_scope)
9393 push_scope ();
9394 return stmt;
9397 /* End a compound statement. STMT is the statement. LOC is the
9398 location of the compound statement-- this is usually the location
9399 of the opening brace. */
9401 tree
9402 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9404 tree block = NULL;
9406 if (do_scope)
9408 if (c_dialect_objc ())
9409 objc_clear_super_receiver ();
9410 block = pop_scope ();
9413 stmt = pop_stmt_list (stmt);
9414 stmt = c_build_bind_expr (loc, block, stmt);
9416 /* If this compound statement is nested immediately inside a statement
9417 expression, then force a BIND_EXPR to be created. Otherwise we'll
9418 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9419 STATEMENT_LISTs merge, and thus we can lose track of what statement
9420 was really last. */
9421 if (building_stmt_list_p ()
9422 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9423 && TREE_CODE (stmt) != BIND_EXPR)
9425 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9426 TREE_SIDE_EFFECTS (stmt) = 1;
9427 SET_EXPR_LOCATION (stmt, loc);
9430 return stmt;
9433 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9434 when the current scope is exited. EH_ONLY is true when this is not
9435 meant to apply to normal control flow transfer. */
9437 void
9438 push_cleanup (tree decl, tree cleanup, bool eh_only)
9440 enum tree_code code;
9441 tree stmt, list;
9442 bool stmt_expr;
9444 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9445 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9446 add_stmt (stmt);
9447 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9448 list = push_stmt_list ();
9449 TREE_OPERAND (stmt, 0) = list;
9450 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9453 /* Convert scalar to vector for the range of operations. */
9454 static enum stv_conv
9455 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1)
9457 tree type0 = TREE_TYPE (op0);
9458 tree type1 = TREE_TYPE (op1);
9459 bool integer_only_op = false;
9460 enum stv_conv ret = stv_firstarg;
9462 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
9463 || TREE_CODE (type1) == VECTOR_TYPE);
9464 switch (code)
9466 case RSHIFT_EXPR:
9467 case LSHIFT_EXPR:
9468 if (TREE_CODE (type0) == INTEGER_TYPE
9469 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9471 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9473 error_at (loc, "conversion of scalar to vector "
9474 "involves truncation");
9475 return stv_error;
9477 else
9478 return stv_firstarg;
9480 break;
9482 case BIT_IOR_EXPR:
9483 case BIT_XOR_EXPR:
9484 case BIT_AND_EXPR:
9485 integer_only_op = true;
9486 /* ... fall through ... */
9488 case PLUS_EXPR:
9489 case MINUS_EXPR:
9490 case MULT_EXPR:
9491 case TRUNC_DIV_EXPR:
9492 case TRUNC_MOD_EXPR:
9493 case RDIV_EXPR:
9494 if (TREE_CODE (type0) == VECTOR_TYPE)
9496 tree tmp;
9497 ret = stv_secondarg;
9498 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
9499 tmp = type0; type0 = type1; type1 = tmp;
9500 tmp = op0; op0 = op1; op1 = tmp;
9503 if (TREE_CODE (type0) == INTEGER_TYPE
9504 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9506 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9508 error_at (loc, "conversion of scalar to vector "
9509 "involves truncation");
9510 return stv_error;
9512 return ret;
9514 else if (!integer_only_op
9515 /* Allow integer --> real conversion if safe. */
9516 && (TREE_CODE (type0) == REAL_TYPE
9517 || TREE_CODE (type0) == INTEGER_TYPE)
9518 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
9520 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9522 error_at (loc, "conversion of scalar to vector "
9523 "involves truncation");
9524 return stv_error;
9526 return ret;
9528 default:
9529 break;
9532 return stv_nothing;
9535 /* Build a binary-operation expression without default conversions.
9536 CODE is the kind of expression to build.
9537 LOCATION is the operator's location.
9538 This function differs from `build' in several ways:
9539 the data type of the result is computed and recorded in it,
9540 warnings are generated if arg data types are invalid,
9541 special handling for addition and subtraction of pointers is known,
9542 and some optimization is done (operations on narrow ints
9543 are done in the narrower type when that gives the same result).
9544 Constant folding is also done before the result is returned.
9546 Note that the operands will never have enumeral types, or function
9547 or array types, because either they will have the default conversions
9548 performed or they have both just been converted to some other type in which
9549 the arithmetic is to be done. */
9551 tree
9552 build_binary_op (location_t location, enum tree_code code,
9553 tree orig_op0, tree orig_op1, int convert_p)
9555 tree type0, type1, orig_type0, orig_type1;
9556 tree eptype;
9557 enum tree_code code0, code1;
9558 tree op0, op1;
9559 tree ret = error_mark_node;
9560 const char *invalid_op_diag;
9561 bool op0_int_operands, op1_int_operands;
9562 bool int_const, int_const_or_overflow, int_operands;
9564 /* Expression code to give to the expression when it is built.
9565 Normally this is CODE, which is what the caller asked for,
9566 but in some special cases we change it. */
9567 enum tree_code resultcode = code;
9569 /* Data type in which the computation is to be performed.
9570 In the simplest cases this is the common type of the arguments. */
9571 tree result_type = NULL;
9573 /* When the computation is in excess precision, the type of the
9574 final EXCESS_PRECISION_EXPR. */
9575 tree semantic_result_type = NULL;
9577 /* Nonzero means operands have already been type-converted
9578 in whatever way is necessary.
9579 Zero means they need to be converted to RESULT_TYPE. */
9580 int converted = 0;
9582 /* Nonzero means create the expression with this type, rather than
9583 RESULT_TYPE. */
9584 tree build_type = 0;
9586 /* Nonzero means after finally constructing the expression
9587 convert it to this type. */
9588 tree final_type = 0;
9590 /* Nonzero if this is an operation like MIN or MAX which can
9591 safely be computed in short if both args are promoted shorts.
9592 Also implies COMMON.
9593 -1 indicates a bitwise operation; this makes a difference
9594 in the exact conditions for when it is safe to do the operation
9595 in a narrower mode. */
9596 int shorten = 0;
9598 /* Nonzero if this is a comparison operation;
9599 if both args are promoted shorts, compare the original shorts.
9600 Also implies COMMON. */
9601 int short_compare = 0;
9603 /* Nonzero if this is a right-shift operation, which can be computed on the
9604 original short and then promoted if the operand is a promoted short. */
9605 int short_shift = 0;
9607 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9608 int common = 0;
9610 /* True means types are compatible as far as ObjC is concerned. */
9611 bool objc_ok;
9613 /* True means this is an arithmetic operation that may need excess
9614 precision. */
9615 bool may_need_excess_precision;
9617 /* True means this is a boolean operation that converts both its
9618 operands to truth-values. */
9619 bool boolean_op = false;
9621 if (location == UNKNOWN_LOCATION)
9622 location = input_location;
9624 op0 = orig_op0;
9625 op1 = orig_op1;
9627 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9628 if (op0_int_operands)
9629 op0 = remove_c_maybe_const_expr (op0);
9630 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9631 if (op1_int_operands)
9632 op1 = remove_c_maybe_const_expr (op1);
9633 int_operands = (op0_int_operands && op1_int_operands);
9634 if (int_operands)
9636 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9637 && TREE_CODE (orig_op1) == INTEGER_CST);
9638 int_const = (int_const_or_overflow
9639 && !TREE_OVERFLOW (orig_op0)
9640 && !TREE_OVERFLOW (orig_op1));
9642 else
9643 int_const = int_const_or_overflow = false;
9645 /* Do not apply default conversion in mixed vector/scalar expression. */
9646 if (convert_p
9647 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
9648 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
9650 op0 = default_conversion (op0);
9651 op1 = default_conversion (op1);
9654 orig_type0 = type0 = TREE_TYPE (op0);
9655 orig_type1 = type1 = TREE_TYPE (op1);
9657 /* The expression codes of the data types of the arguments tell us
9658 whether the arguments are integers, floating, pointers, etc. */
9659 code0 = TREE_CODE (type0);
9660 code1 = TREE_CODE (type1);
9662 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9663 STRIP_TYPE_NOPS (op0);
9664 STRIP_TYPE_NOPS (op1);
9666 /* If an error was already reported for one of the arguments,
9667 avoid reporting another error. */
9669 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9670 return error_mark_node;
9672 if ((invalid_op_diag
9673 = targetm.invalid_binary_op (code, type0, type1)))
9675 error_at (location, invalid_op_diag);
9676 return error_mark_node;
9679 switch (code)
9681 case PLUS_EXPR:
9682 case MINUS_EXPR:
9683 case MULT_EXPR:
9684 case TRUNC_DIV_EXPR:
9685 case CEIL_DIV_EXPR:
9686 case FLOOR_DIV_EXPR:
9687 case ROUND_DIV_EXPR:
9688 case EXACT_DIV_EXPR:
9689 may_need_excess_precision = true;
9690 break;
9691 default:
9692 may_need_excess_precision = false;
9693 break;
9695 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9697 op0 = TREE_OPERAND (op0, 0);
9698 type0 = TREE_TYPE (op0);
9700 else if (may_need_excess_precision
9701 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9703 type0 = eptype;
9704 op0 = convert (eptype, op0);
9706 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9708 op1 = TREE_OPERAND (op1, 0);
9709 type1 = TREE_TYPE (op1);
9711 else if (may_need_excess_precision
9712 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9714 type1 = eptype;
9715 op1 = convert (eptype, op1);
9718 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9720 /* In case when one of the operands of the binary operation is
9721 a vector and another is a scalar -- convert scalar to vector. */
9722 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
9724 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1);
9726 switch (convert_flag)
9728 case stv_error:
9729 return error_mark_node;
9730 case stv_firstarg:
9732 bool maybe_const = true;
9733 tree sc;
9734 sc = c_fully_fold (op0, false, &maybe_const);
9735 sc = save_expr (sc);
9736 sc = convert (TREE_TYPE (type1), sc);
9737 op0 = build_vector_from_val (type1, sc);
9738 if (!maybe_const)
9739 op0 = c_wrap_maybe_const (op0, true);
9740 orig_type0 = type0 = TREE_TYPE (op0);
9741 code0 = TREE_CODE (type0);
9742 converted = 1;
9743 break;
9745 case stv_secondarg:
9747 bool maybe_const = true;
9748 tree sc;
9749 sc = c_fully_fold (op1, false, &maybe_const);
9750 sc = save_expr (sc);
9751 sc = convert (TREE_TYPE (type0), sc);
9752 op1 = build_vector_from_val (type0, sc);
9753 if (!maybe_const)
9754 op1 = c_wrap_maybe_const (op1, true);
9755 orig_type1 = type1 = TREE_TYPE (op1);
9756 code1 = TREE_CODE (type1);
9757 converted = 1;
9758 break;
9760 default:
9761 break;
9765 switch (code)
9767 case PLUS_EXPR:
9768 /* Handle the pointer + int case. */
9769 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9771 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9772 goto return_build_binary_op;
9774 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9776 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9777 goto return_build_binary_op;
9779 else
9780 common = 1;
9781 break;
9783 case MINUS_EXPR:
9784 /* Subtraction of two similar pointers.
9785 We must subtract them as integers, then divide by object size. */
9786 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9787 && comp_target_types (location, type0, type1))
9789 ret = pointer_diff (location, op0, op1);
9790 goto return_build_binary_op;
9792 /* Handle pointer minus int. Just like pointer plus int. */
9793 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9795 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9796 goto return_build_binary_op;
9798 else
9799 common = 1;
9800 break;
9802 case MULT_EXPR:
9803 common = 1;
9804 break;
9806 case TRUNC_DIV_EXPR:
9807 case CEIL_DIV_EXPR:
9808 case FLOOR_DIV_EXPR:
9809 case ROUND_DIV_EXPR:
9810 case EXACT_DIV_EXPR:
9811 warn_for_div_by_zero (location, op1);
9813 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9814 || code0 == FIXED_POINT_TYPE
9815 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9816 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9817 || code1 == FIXED_POINT_TYPE
9818 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9820 enum tree_code tcode0 = code0, tcode1 = code1;
9822 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9823 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9824 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9825 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9827 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9828 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9829 resultcode = RDIV_EXPR;
9830 else
9831 /* Although it would be tempting to shorten always here, that
9832 loses on some targets, since the modulo instruction is
9833 undefined if the quotient can't be represented in the
9834 computation mode. We shorten only if unsigned or if
9835 dividing by something we know != -1. */
9836 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9837 || (TREE_CODE (op1) == INTEGER_CST
9838 && !integer_all_onesp (op1)));
9839 common = 1;
9841 break;
9843 case BIT_AND_EXPR:
9844 case BIT_IOR_EXPR:
9845 case BIT_XOR_EXPR:
9846 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9847 shorten = -1;
9848 /* Allow vector types which are not floating point types. */
9849 else if (code0 == VECTOR_TYPE
9850 && code1 == VECTOR_TYPE
9851 && !VECTOR_FLOAT_TYPE_P (type0)
9852 && !VECTOR_FLOAT_TYPE_P (type1))
9853 common = 1;
9854 break;
9856 case TRUNC_MOD_EXPR:
9857 case FLOOR_MOD_EXPR:
9858 warn_for_div_by_zero (location, op1);
9860 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9861 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9862 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9863 common = 1;
9864 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9866 /* Although it would be tempting to shorten always here, that loses
9867 on some targets, since the modulo instruction is undefined if the
9868 quotient can't be represented in the computation mode. We shorten
9869 only if unsigned or if dividing by something we know != -1. */
9870 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9871 || (TREE_CODE (op1) == INTEGER_CST
9872 && !integer_all_onesp (op1)));
9873 common = 1;
9875 break;
9877 case TRUTH_ANDIF_EXPR:
9878 case TRUTH_ORIF_EXPR:
9879 case TRUTH_AND_EXPR:
9880 case TRUTH_OR_EXPR:
9881 case TRUTH_XOR_EXPR:
9882 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9883 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9884 || code0 == FIXED_POINT_TYPE)
9885 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9886 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9887 || code1 == FIXED_POINT_TYPE))
9889 /* Result of these operations is always an int,
9890 but that does not mean the operands should be
9891 converted to ints! */
9892 result_type = integer_type_node;
9893 op0 = c_common_truthvalue_conversion (location, op0);
9894 op1 = c_common_truthvalue_conversion (location, op1);
9895 converted = 1;
9896 boolean_op = true;
9898 if (code == TRUTH_ANDIF_EXPR)
9900 int_const_or_overflow = (int_operands
9901 && TREE_CODE (orig_op0) == INTEGER_CST
9902 && (op0 == truthvalue_false_node
9903 || TREE_CODE (orig_op1) == INTEGER_CST));
9904 int_const = (int_const_or_overflow
9905 && !TREE_OVERFLOW (orig_op0)
9906 && (op0 == truthvalue_false_node
9907 || !TREE_OVERFLOW (orig_op1)));
9909 else if (code == TRUTH_ORIF_EXPR)
9911 int_const_or_overflow = (int_operands
9912 && TREE_CODE (orig_op0) == INTEGER_CST
9913 && (op0 == truthvalue_true_node
9914 || TREE_CODE (orig_op1) == INTEGER_CST));
9915 int_const = (int_const_or_overflow
9916 && !TREE_OVERFLOW (orig_op0)
9917 && (op0 == truthvalue_true_node
9918 || !TREE_OVERFLOW (orig_op1)));
9920 break;
9922 /* Shift operations: result has same type as first operand;
9923 always convert second operand to int.
9924 Also set SHORT_SHIFT if shifting rightward. */
9926 case RSHIFT_EXPR:
9927 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9928 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9930 result_type = type0;
9931 converted = 1;
9933 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9934 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9935 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9936 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9938 result_type = type0;
9939 converted = 1;
9941 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9942 && code1 == INTEGER_TYPE)
9944 if (TREE_CODE (op1) == INTEGER_CST)
9946 if (tree_int_cst_sgn (op1) < 0)
9948 int_const = false;
9949 if (c_inhibit_evaluation_warnings == 0)
9950 warning (0, "right shift count is negative");
9952 else
9954 if (!integer_zerop (op1))
9955 short_shift = 1;
9957 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9959 int_const = false;
9960 if (c_inhibit_evaluation_warnings == 0)
9961 warning (0, "right shift count >= width of type");
9966 /* Use the type of the value to be shifted. */
9967 result_type = type0;
9968 /* Convert the non vector shift-count to an integer, regardless
9969 of size of value being shifted. */
9970 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9971 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9972 op1 = convert (integer_type_node, op1);
9973 /* Avoid converting op1 to result_type later. */
9974 converted = 1;
9976 break;
9978 case LSHIFT_EXPR:
9979 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9980 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9982 result_type = type0;
9983 converted = 1;
9985 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9986 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9987 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9988 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9990 result_type = type0;
9991 converted = 1;
9993 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9994 && code1 == INTEGER_TYPE)
9996 if (TREE_CODE (op1) == INTEGER_CST)
9998 if (tree_int_cst_sgn (op1) < 0)
10000 int_const = false;
10001 if (c_inhibit_evaluation_warnings == 0)
10002 warning (0, "left shift count is negative");
10005 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10007 int_const = false;
10008 if (c_inhibit_evaluation_warnings == 0)
10009 warning (0, "left shift count >= width of type");
10013 /* Use the type of the value to be shifted. */
10014 result_type = type0;
10015 /* Convert the non vector shift-count to an integer, regardless
10016 of size of value being shifted. */
10017 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10018 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10019 op1 = convert (integer_type_node, op1);
10020 /* Avoid converting op1 to result_type later. */
10021 converted = 1;
10023 break;
10025 case EQ_EXPR:
10026 case NE_EXPR:
10027 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10029 tree intt;
10030 if (TREE_TYPE (type0) != TREE_TYPE (type1))
10032 error_at (location, "comparing vectors with different "
10033 "element types");
10034 return error_mark_node;
10037 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10039 error_at (location, "comparing vectors with different "
10040 "number of elements");
10041 return error_mark_node;
10044 /* Always construct signed integer vector type. */
10045 intt = c_common_type_for_size (GET_MODE_BITSIZE
10046 (TYPE_MODE (TREE_TYPE (type0))), 0);
10047 result_type = build_opaque_vector_type (intt,
10048 TYPE_VECTOR_SUBPARTS (type0));
10049 converted = 1;
10050 break;
10052 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10053 warning_at (location,
10054 OPT_Wfloat_equal,
10055 "comparing floating point with == or != is unsafe");
10056 /* Result of comparison is always int,
10057 but don't convert the args to int! */
10058 build_type = integer_type_node;
10059 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10060 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10061 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10062 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10063 short_compare = 1;
10064 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10066 if (TREE_CODE (op0) == ADDR_EXPR
10067 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10069 if (code == EQ_EXPR)
10070 warning_at (location,
10071 OPT_Waddress,
10072 "the comparison will always evaluate as %<false%> "
10073 "for the address of %qD will never be NULL",
10074 TREE_OPERAND (op0, 0));
10075 else
10076 warning_at (location,
10077 OPT_Waddress,
10078 "the comparison will always evaluate as %<true%> "
10079 "for the address of %qD will never be NULL",
10080 TREE_OPERAND (op0, 0));
10082 result_type = type0;
10084 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10086 if (TREE_CODE (op1) == ADDR_EXPR
10087 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10089 if (code == EQ_EXPR)
10090 warning_at (location,
10091 OPT_Waddress,
10092 "the comparison will always evaluate as %<false%> "
10093 "for the address of %qD will never be NULL",
10094 TREE_OPERAND (op1, 0));
10095 else
10096 warning_at (location,
10097 OPT_Waddress,
10098 "the comparison will always evaluate as %<true%> "
10099 "for the address of %qD will never be NULL",
10100 TREE_OPERAND (op1, 0));
10102 result_type = type1;
10104 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10106 tree tt0 = TREE_TYPE (type0);
10107 tree tt1 = TREE_TYPE (type1);
10108 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10109 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10110 addr_space_t as_common = ADDR_SPACE_GENERIC;
10112 /* Anything compares with void *. void * compares with anything.
10113 Otherwise, the targets must be compatible
10114 and both must be object or both incomplete. */
10115 if (comp_target_types (location, type0, type1))
10116 result_type = common_pointer_type (type0, type1);
10117 else if (!addr_space_superset (as0, as1, &as_common))
10119 error_at (location, "comparison of pointers to "
10120 "disjoint address spaces");
10121 return error_mark_node;
10123 else if (VOID_TYPE_P (tt0))
10125 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10126 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10127 "comparison of %<void *%> with function pointer");
10129 else if (VOID_TYPE_P (tt1))
10131 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10132 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10133 "comparison of %<void *%> with function pointer");
10135 else
10136 /* Avoid warning about the volatile ObjC EH puts on decls. */
10137 if (!objc_ok)
10138 pedwarn (location, 0,
10139 "comparison of distinct pointer types lacks a cast");
10141 if (result_type == NULL_TREE)
10143 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10144 result_type = build_pointer_type
10145 (build_qualified_type (void_type_node, qual));
10148 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10150 result_type = type0;
10151 pedwarn (location, 0, "comparison between pointer and integer");
10153 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10155 result_type = type1;
10156 pedwarn (location, 0, "comparison between pointer and integer");
10158 break;
10160 case LE_EXPR:
10161 case GE_EXPR:
10162 case LT_EXPR:
10163 case GT_EXPR:
10164 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10166 tree intt;
10167 if (TREE_TYPE (type0) != TREE_TYPE (type1))
10169 error_at (location, "comparing vectors with different "
10170 "element types");
10171 return error_mark_node;
10174 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10176 error_at (location, "comparing vectors with different "
10177 "number of elements");
10178 return error_mark_node;
10181 /* Always construct signed integer vector type. */
10182 intt = c_common_type_for_size (GET_MODE_BITSIZE
10183 (TYPE_MODE (TREE_TYPE (type0))), 0);
10184 result_type = build_opaque_vector_type (intt,
10185 TYPE_VECTOR_SUBPARTS (type0));
10186 converted = 1;
10187 break;
10189 build_type = integer_type_node;
10190 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10191 || code0 == FIXED_POINT_TYPE)
10192 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10193 || code1 == FIXED_POINT_TYPE))
10194 short_compare = 1;
10195 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10197 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10198 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10199 addr_space_t as_common;
10201 if (comp_target_types (location, type0, type1))
10203 result_type = common_pointer_type (type0, type1);
10204 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10205 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10206 pedwarn (location, 0,
10207 "comparison of complete and incomplete pointers");
10208 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10209 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10210 "ordered comparisons of pointers to functions");
10211 else if (null_pointer_constant_p (orig_op0)
10212 || null_pointer_constant_p (orig_op1))
10213 warning_at (location, OPT_Wextra,
10214 "ordered comparison of pointer with null pointer");
10217 else if (!addr_space_superset (as0, as1, &as_common))
10219 error_at (location, "comparison of pointers to "
10220 "disjoint address spaces");
10221 return error_mark_node;
10223 else
10225 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10226 result_type = build_pointer_type
10227 (build_qualified_type (void_type_node, qual));
10228 pedwarn (location, 0,
10229 "comparison of distinct pointer types lacks a cast");
10232 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10234 result_type = type0;
10235 if (pedantic)
10236 pedwarn (location, OPT_Wpedantic,
10237 "ordered comparison of pointer with integer zero");
10238 else if (extra_warnings)
10239 warning_at (location, OPT_Wextra,
10240 "ordered comparison of pointer with integer zero");
10242 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10244 result_type = type1;
10245 if (pedantic)
10246 pedwarn (location, OPT_Wpedantic,
10247 "ordered comparison of pointer with integer zero");
10248 else if (extra_warnings)
10249 warning_at (location, OPT_Wextra,
10250 "ordered comparison of pointer with integer zero");
10252 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10254 result_type = type0;
10255 pedwarn (location, 0, "comparison between pointer and integer");
10257 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10259 result_type = type1;
10260 pedwarn (location, 0, "comparison between pointer and integer");
10262 break;
10264 default:
10265 gcc_unreachable ();
10268 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10269 return error_mark_node;
10271 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10272 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10273 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10274 TREE_TYPE (type1))))
10276 binary_op_error (location, code, type0, type1);
10277 return error_mark_node;
10280 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10281 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10283 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10284 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10286 bool first_complex = (code0 == COMPLEX_TYPE);
10287 bool second_complex = (code1 == COMPLEX_TYPE);
10288 int none_complex = (!first_complex && !second_complex);
10290 if (shorten || common || short_compare)
10292 result_type = c_common_type (type0, type1);
10293 do_warn_double_promotion (result_type, type0, type1,
10294 "implicit conversion from %qT to %qT "
10295 "to match other operand of binary "
10296 "expression",
10297 location);
10298 if (result_type == error_mark_node)
10299 return error_mark_node;
10302 if (first_complex != second_complex
10303 && (code == PLUS_EXPR
10304 || code == MINUS_EXPR
10305 || code == MULT_EXPR
10306 || (code == TRUNC_DIV_EXPR && first_complex))
10307 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10308 && flag_signed_zeros)
10310 /* An operation on mixed real/complex operands must be
10311 handled specially, but the language-independent code can
10312 more easily optimize the plain complex arithmetic if
10313 -fno-signed-zeros. */
10314 tree real_type = TREE_TYPE (result_type);
10315 tree real, imag;
10316 if (type0 != orig_type0 || type1 != orig_type1)
10318 gcc_assert (may_need_excess_precision && common);
10319 semantic_result_type = c_common_type (orig_type0, orig_type1);
10321 if (first_complex)
10323 if (TREE_TYPE (op0) != result_type)
10324 op0 = convert_and_check (result_type, op0);
10325 if (TREE_TYPE (op1) != real_type)
10326 op1 = convert_and_check (real_type, op1);
10328 else
10330 if (TREE_TYPE (op0) != real_type)
10331 op0 = convert_and_check (real_type, op0);
10332 if (TREE_TYPE (op1) != result_type)
10333 op1 = convert_and_check (result_type, op1);
10335 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10336 return error_mark_node;
10337 if (first_complex)
10339 op0 = c_save_expr (op0);
10340 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10341 op0, 1);
10342 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10343 op0, 1);
10344 switch (code)
10346 case MULT_EXPR:
10347 case TRUNC_DIV_EXPR:
10348 op1 = c_save_expr (op1);
10349 imag = build2 (resultcode, real_type, imag, op1);
10350 /* Fall through. */
10351 case PLUS_EXPR:
10352 case MINUS_EXPR:
10353 real = build2 (resultcode, real_type, real, op1);
10354 break;
10355 default:
10356 gcc_unreachable();
10359 else
10361 op1 = c_save_expr (op1);
10362 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10363 op1, 1);
10364 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10365 op1, 1);
10366 switch (code)
10368 case MULT_EXPR:
10369 op0 = c_save_expr (op0);
10370 imag = build2 (resultcode, real_type, op0, imag);
10371 /* Fall through. */
10372 case PLUS_EXPR:
10373 real = build2 (resultcode, real_type, op0, real);
10374 break;
10375 case MINUS_EXPR:
10376 real = build2 (resultcode, real_type, op0, real);
10377 imag = build1 (NEGATE_EXPR, real_type, imag);
10378 break;
10379 default:
10380 gcc_unreachable();
10383 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10384 goto return_build_binary_op;
10387 /* For certain operations (which identify themselves by shorten != 0)
10388 if both args were extended from the same smaller type,
10389 do the arithmetic in that type and then extend.
10391 shorten !=0 and !=1 indicates a bitwise operation.
10392 For them, this optimization is safe only if
10393 both args are zero-extended or both are sign-extended.
10394 Otherwise, we might change the result.
10395 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10396 but calculated in (unsigned short) it would be (unsigned short)-1. */
10398 if (shorten && none_complex)
10400 final_type = result_type;
10401 result_type = shorten_binary_op (result_type, op0, op1,
10402 shorten == -1);
10405 /* Shifts can be shortened if shifting right. */
10407 if (short_shift)
10409 int unsigned_arg;
10410 tree arg0 = get_narrower (op0, &unsigned_arg);
10412 final_type = result_type;
10414 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10415 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10417 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10418 && tree_int_cst_sgn (op1) > 0
10419 /* We can shorten only if the shift count is less than the
10420 number of bits in the smaller type size. */
10421 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10422 /* We cannot drop an unsigned shift after sign-extension. */
10423 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10425 /* Do an unsigned shift if the operand was zero-extended. */
10426 result_type
10427 = c_common_signed_or_unsigned_type (unsigned_arg,
10428 TREE_TYPE (arg0));
10429 /* Convert value-to-be-shifted to that type. */
10430 if (TREE_TYPE (op0) != result_type)
10431 op0 = convert (result_type, op0);
10432 converted = 1;
10436 /* Comparison operations are shortened too but differently.
10437 They identify themselves by setting short_compare = 1. */
10439 if (short_compare)
10441 /* Don't write &op0, etc., because that would prevent op0
10442 from being kept in a register.
10443 Instead, make copies of the our local variables and
10444 pass the copies by reference, then copy them back afterward. */
10445 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10446 enum tree_code xresultcode = resultcode;
10447 tree val
10448 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10450 if (val != 0)
10452 ret = val;
10453 goto return_build_binary_op;
10456 op0 = xop0, op1 = xop1;
10457 converted = 1;
10458 resultcode = xresultcode;
10460 if (c_inhibit_evaluation_warnings == 0)
10462 bool op0_maybe_const = true;
10463 bool op1_maybe_const = true;
10464 tree orig_op0_folded, orig_op1_folded;
10466 if (in_late_binary_op)
10468 orig_op0_folded = orig_op0;
10469 orig_op1_folded = orig_op1;
10471 else
10473 /* Fold for the sake of possible warnings, as in
10474 build_conditional_expr. This requires the
10475 "original" values to be folded, not just op0 and
10476 op1. */
10477 c_inhibit_evaluation_warnings++;
10478 op0 = c_fully_fold (op0, require_constant_value,
10479 &op0_maybe_const);
10480 op1 = c_fully_fold (op1, require_constant_value,
10481 &op1_maybe_const);
10482 c_inhibit_evaluation_warnings--;
10483 orig_op0_folded = c_fully_fold (orig_op0,
10484 require_constant_value,
10485 NULL);
10486 orig_op1_folded = c_fully_fold (orig_op1,
10487 require_constant_value,
10488 NULL);
10491 if (warn_sign_compare)
10492 warn_for_sign_compare (location, orig_op0_folded,
10493 orig_op1_folded, op0, op1,
10494 result_type, resultcode);
10495 if (!in_late_binary_op && !int_operands)
10497 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10498 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10499 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10500 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10506 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10507 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10508 Then the expression will be built.
10509 It will be given type FINAL_TYPE if that is nonzero;
10510 otherwise, it will be given type RESULT_TYPE. */
10512 if (!result_type)
10514 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10515 return error_mark_node;
10518 if (build_type == NULL_TREE)
10520 build_type = result_type;
10521 if ((type0 != orig_type0 || type1 != orig_type1)
10522 && !boolean_op)
10524 gcc_assert (may_need_excess_precision && common);
10525 semantic_result_type = c_common_type (orig_type0, orig_type1);
10529 if (!converted)
10531 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10532 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10534 /* This can happen if one operand has a vector type, and the other
10535 has a different type. */
10536 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10537 return error_mark_node;
10540 /* Treat expressions in initializers specially as they can't trap. */
10541 if (int_const_or_overflow)
10542 ret = (require_constant_value
10543 ? fold_build2_initializer_loc (location, resultcode, build_type,
10544 op0, op1)
10545 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10546 else
10547 ret = build2 (resultcode, build_type, op0, op1);
10548 if (final_type != 0)
10549 ret = convert (final_type, ret);
10551 return_build_binary_op:
10552 gcc_assert (ret != error_mark_node);
10553 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10554 ret = (int_operands
10555 ? note_integer_operands (ret)
10556 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10557 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10558 && !in_late_binary_op)
10559 ret = note_integer_operands (ret);
10560 if (semantic_result_type)
10561 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10562 protected_set_expr_location (ret, location);
10563 return ret;
10567 /* Convert EXPR to be a truth-value, validating its type for this
10568 purpose. LOCATION is the source location for the expression. */
10570 tree
10571 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10573 bool int_const, int_operands;
10575 switch (TREE_CODE (TREE_TYPE (expr)))
10577 case ARRAY_TYPE:
10578 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10579 return error_mark_node;
10581 case RECORD_TYPE:
10582 error_at (location, "used struct type value where scalar is required");
10583 return error_mark_node;
10585 case UNION_TYPE:
10586 error_at (location, "used union type value where scalar is required");
10587 return error_mark_node;
10589 case VOID_TYPE:
10590 error_at (location, "void value not ignored as it ought to be");
10591 return error_mark_node;
10593 case FUNCTION_TYPE:
10594 gcc_unreachable ();
10596 case VECTOR_TYPE:
10597 error_at (location, "used vector type where scalar is required");
10598 return error_mark_node;
10600 default:
10601 break;
10604 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10605 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10606 if (int_operands)
10607 expr = remove_c_maybe_const_expr (expr);
10609 /* ??? Should we also give an error for vectors rather than leaving
10610 those to give errors later? */
10611 expr = c_common_truthvalue_conversion (location, expr);
10613 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10615 if (TREE_OVERFLOW (expr))
10616 return expr;
10617 else
10618 return note_integer_operands (expr);
10620 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10621 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10622 return expr;
10626 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10627 required. */
10629 tree
10630 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10632 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10634 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10635 /* Executing a compound literal inside a function reinitializes
10636 it. */
10637 if (!TREE_STATIC (decl))
10638 *se = true;
10639 return decl;
10641 else
10642 return expr;
10645 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10647 tree
10648 c_begin_omp_parallel (void)
10650 tree block;
10652 keep_next_level ();
10653 block = c_begin_compound_stmt (true);
10655 return block;
10658 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10659 statement. LOC is the location of the OMP_PARALLEL. */
10661 tree
10662 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10664 tree stmt;
10666 block = c_end_compound_stmt (loc, block, true);
10668 stmt = make_node (OMP_PARALLEL);
10669 TREE_TYPE (stmt) = void_type_node;
10670 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10671 OMP_PARALLEL_BODY (stmt) = block;
10672 SET_EXPR_LOCATION (stmt, loc);
10674 return add_stmt (stmt);
10677 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10679 tree
10680 c_begin_omp_task (void)
10682 tree block;
10684 keep_next_level ();
10685 block = c_begin_compound_stmt (true);
10687 return block;
10690 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10691 statement. LOC is the location of the #pragma. */
10693 tree
10694 c_finish_omp_task (location_t loc, tree clauses, tree block)
10696 tree stmt;
10698 block = c_end_compound_stmt (loc, block, true);
10700 stmt = make_node (OMP_TASK);
10701 TREE_TYPE (stmt) = void_type_node;
10702 OMP_TASK_CLAUSES (stmt) = clauses;
10703 OMP_TASK_BODY (stmt) = block;
10704 SET_EXPR_LOCATION (stmt, loc);
10706 return add_stmt (stmt);
10709 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10710 Remove any elements from the list that are invalid. */
10712 tree
10713 c_finish_omp_clauses (tree clauses)
10715 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10716 tree c, t, *pc = &clauses;
10717 const char *name;
10719 bitmap_obstack_initialize (NULL);
10720 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10721 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10722 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10724 for (pc = &clauses, c = clauses; c ; c = *pc)
10726 bool remove = false;
10727 bool need_complete = false;
10728 bool need_implicitly_determined = false;
10730 switch (OMP_CLAUSE_CODE (c))
10732 case OMP_CLAUSE_SHARED:
10733 name = "shared";
10734 need_implicitly_determined = true;
10735 goto check_dup_generic;
10737 case OMP_CLAUSE_PRIVATE:
10738 name = "private";
10739 need_complete = true;
10740 need_implicitly_determined = true;
10741 goto check_dup_generic;
10743 case OMP_CLAUSE_REDUCTION:
10744 name = "reduction";
10745 need_implicitly_determined = true;
10746 t = OMP_CLAUSE_DECL (c);
10747 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10748 || POINTER_TYPE_P (TREE_TYPE (t)))
10750 error_at (OMP_CLAUSE_LOCATION (c),
10751 "%qE has invalid type for %<reduction%>", t);
10752 remove = true;
10754 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10756 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10757 const char *r_name = NULL;
10759 switch (r_code)
10761 case PLUS_EXPR:
10762 case MULT_EXPR:
10763 case MINUS_EXPR:
10764 case MIN_EXPR:
10765 case MAX_EXPR:
10766 break;
10767 case BIT_AND_EXPR:
10768 r_name = "&";
10769 break;
10770 case BIT_XOR_EXPR:
10771 r_name = "^";
10772 break;
10773 case BIT_IOR_EXPR:
10774 r_name = "|";
10775 break;
10776 case TRUTH_ANDIF_EXPR:
10777 r_name = "&&";
10778 break;
10779 case TRUTH_ORIF_EXPR:
10780 r_name = "||";
10781 break;
10782 default:
10783 gcc_unreachable ();
10785 if (r_name)
10787 error_at (OMP_CLAUSE_LOCATION (c),
10788 "%qE has invalid type for %<reduction(%s)%>",
10789 t, r_name);
10790 remove = true;
10793 goto check_dup_generic;
10795 case OMP_CLAUSE_COPYPRIVATE:
10796 name = "copyprivate";
10797 goto check_dup_generic;
10799 case OMP_CLAUSE_COPYIN:
10800 name = "copyin";
10801 t = OMP_CLAUSE_DECL (c);
10802 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10804 error_at (OMP_CLAUSE_LOCATION (c),
10805 "%qE must be %<threadprivate%> for %<copyin%>", t);
10806 remove = true;
10808 goto check_dup_generic;
10810 check_dup_generic:
10811 t = OMP_CLAUSE_DECL (c);
10812 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10814 error_at (OMP_CLAUSE_LOCATION (c),
10815 "%qE is not a variable in clause %qs", t, name);
10816 remove = true;
10818 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10819 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10820 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10822 error_at (OMP_CLAUSE_LOCATION (c),
10823 "%qE appears more than once in data clauses", t);
10824 remove = true;
10826 else
10827 bitmap_set_bit (&generic_head, DECL_UID (t));
10828 break;
10830 case OMP_CLAUSE_FIRSTPRIVATE:
10831 name = "firstprivate";
10832 t = OMP_CLAUSE_DECL (c);
10833 need_complete = true;
10834 need_implicitly_determined = true;
10835 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10837 error_at (OMP_CLAUSE_LOCATION (c),
10838 "%qE is not a variable in clause %<firstprivate%>", t);
10839 remove = true;
10841 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10842 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10844 error_at (OMP_CLAUSE_LOCATION (c),
10845 "%qE appears more than once in data clauses", t);
10846 remove = true;
10848 else
10849 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10850 break;
10852 case OMP_CLAUSE_LASTPRIVATE:
10853 name = "lastprivate";
10854 t = OMP_CLAUSE_DECL (c);
10855 need_complete = true;
10856 need_implicitly_determined = true;
10857 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10859 error_at (OMP_CLAUSE_LOCATION (c),
10860 "%qE is not a variable in clause %<lastprivate%>", t);
10861 remove = true;
10863 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10864 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10866 error_at (OMP_CLAUSE_LOCATION (c),
10867 "%qE appears more than once in data clauses", t);
10868 remove = true;
10870 else
10871 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10872 break;
10874 case OMP_CLAUSE_IF:
10875 case OMP_CLAUSE_NUM_THREADS:
10876 case OMP_CLAUSE_SCHEDULE:
10877 case OMP_CLAUSE_NOWAIT:
10878 case OMP_CLAUSE_ORDERED:
10879 case OMP_CLAUSE_DEFAULT:
10880 case OMP_CLAUSE_UNTIED:
10881 case OMP_CLAUSE_COLLAPSE:
10882 case OMP_CLAUSE_FINAL:
10883 case OMP_CLAUSE_MERGEABLE:
10884 pc = &OMP_CLAUSE_CHAIN (c);
10885 continue;
10887 default:
10888 gcc_unreachable ();
10891 if (!remove)
10893 t = OMP_CLAUSE_DECL (c);
10895 if (need_complete)
10897 t = require_complete_type (t);
10898 if (t == error_mark_node)
10899 remove = true;
10902 if (need_implicitly_determined)
10904 const char *share_name = NULL;
10906 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10907 share_name = "threadprivate";
10908 else switch (c_omp_predetermined_sharing (t))
10910 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10911 break;
10912 case OMP_CLAUSE_DEFAULT_SHARED:
10913 /* const vars may be specified in firstprivate clause. */
10914 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10915 && TREE_READONLY (t))
10916 break;
10917 share_name = "shared";
10918 break;
10919 case OMP_CLAUSE_DEFAULT_PRIVATE:
10920 share_name = "private";
10921 break;
10922 default:
10923 gcc_unreachable ();
10925 if (share_name)
10927 error_at (OMP_CLAUSE_LOCATION (c),
10928 "%qE is predetermined %qs for %qs",
10929 t, share_name, name);
10930 remove = true;
10935 if (remove)
10936 *pc = OMP_CLAUSE_CHAIN (c);
10937 else
10938 pc = &OMP_CLAUSE_CHAIN (c);
10941 bitmap_obstack_release (NULL);
10942 return clauses;
10945 /* Create a transaction node. */
10947 tree
10948 c_finish_transaction (location_t loc, tree block, int flags)
10950 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
10951 if (flags & TM_STMT_ATTR_OUTER)
10952 TRANSACTION_EXPR_OUTER (stmt) = 1;
10953 if (flags & TM_STMT_ATTR_RELAXED)
10954 TRANSACTION_EXPR_RELAXED (stmt) = 1;
10955 return add_stmt (stmt);
10958 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10959 down to the element type of an array. */
10961 tree
10962 c_build_qualified_type (tree type, int type_quals)
10964 if (type == error_mark_node)
10965 return type;
10967 if (TREE_CODE (type) == ARRAY_TYPE)
10969 tree t;
10970 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10971 type_quals);
10973 /* See if we already have an identically qualified type. */
10974 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10976 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10977 && TYPE_NAME (t) == TYPE_NAME (type)
10978 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10979 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10980 TYPE_ATTRIBUTES (type)))
10981 break;
10983 if (!t)
10985 tree domain = TYPE_DOMAIN (type);
10987 t = build_variant_type_copy (type);
10988 TREE_TYPE (t) = element_type;
10990 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10991 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10992 SET_TYPE_STRUCTURAL_EQUALITY (t);
10993 else if (TYPE_CANONICAL (element_type) != element_type
10994 || (domain && TYPE_CANONICAL (domain) != domain))
10996 tree unqualified_canon
10997 = build_array_type (TYPE_CANONICAL (element_type),
10998 domain? TYPE_CANONICAL (domain)
10999 : NULL_TREE);
11000 TYPE_CANONICAL (t)
11001 = c_build_qualified_type (unqualified_canon, type_quals);
11003 else
11004 TYPE_CANONICAL (t) = t;
11006 return t;
11009 /* A restrict-qualified pointer type must be a pointer to object or
11010 incomplete type. Note that the use of POINTER_TYPE_P also allows
11011 REFERENCE_TYPEs, which is appropriate for C++. */
11012 if ((type_quals & TYPE_QUAL_RESTRICT)
11013 && (!POINTER_TYPE_P (type)
11014 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
11016 error ("invalid use of %<restrict%>");
11017 type_quals &= ~TYPE_QUAL_RESTRICT;
11020 return build_qualified_type (type, type_quals);
11023 /* Build a VA_ARG_EXPR for the C parser. */
11025 tree
11026 c_build_va_arg (location_t loc, tree expr, tree type)
11028 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
11029 warning_at (loc, OPT_Wc___compat,
11030 "C++ requires promoted type, not enum type, in %<va_arg%>");
11031 return build_va_arg (loc, expr, type);