2012-09-16 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / c / c-typeck.c
blob5b4ad28587448a2b0a857b3715d39d0e15305e8a
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,
4 2011, 2012 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 "intl.h"
38 #include "target.h"
39 #include "tree-iterator.h"
40 #include "bitmap.h"
41 #include "gimple.h"
42 #include "c-family/c-objc.h"
44 /* Possible cases of implicit bad conversions. Used to select
45 diagnostic messages in convert_for_assignment. */
46 enum impl_conv {
47 ic_argpass,
48 ic_assign,
49 ic_init,
50 ic_return
53 /* Possibe cases of scalar_to_vector conversion. */
54 enum stv_conv {
55 stv_error, /* Error occured. */
56 stv_nothing, /* Nothing happened. */
57 stv_firstarg, /* First argument must be expanded. */
58 stv_secondarg /* Second argument must be expanded. */
61 /* The level of nesting inside "__alignof__". */
62 int in_alignof;
64 /* The level of nesting inside "sizeof". */
65 int in_sizeof;
67 /* The level of nesting inside "typeof". */
68 int in_typeof;
70 /* The argument of last parsed sizeof expression, only to be tested
71 if expr.original_code == SIZEOF_EXPR. */
72 tree c_last_sizeof_arg;
74 /* Nonzero if we've already printed a "missing braces around initializer"
75 message within this initializer. */
76 static int missing_braces_mentioned;
78 static int require_constant_value;
79 static int require_constant_elements;
81 static bool null_pointer_constant_p (const_tree);
82 static tree qualify_type (tree, tree);
83 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
84 bool *);
85 static int comp_target_types (location_t, tree, tree);
86 static int function_types_compatible_p (const_tree, const_tree, bool *,
87 bool *);
88 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
89 static tree lookup_field (tree, tree);
90 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
91 tree);
92 static tree pointer_diff (location_t, tree, tree);
93 static tree convert_for_assignment (location_t, tree, tree, tree,
94 enum impl_conv, bool, tree, tree, int);
95 static tree valid_compound_expr_initializer (tree, tree);
96 static void push_string (const char *);
97 static void push_member_name (tree);
98 static int spelling_length (void);
99 static char *print_spelling (char *);
100 static void warning_init (int, const char *);
101 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
102 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
103 struct obstack *);
104 static void output_pending_init_elements (int, struct obstack *);
105 static int set_designator (int, struct obstack *);
106 static void push_range_stack (tree, struct obstack *);
107 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
108 static void set_nonincremental_init (struct obstack *);
109 static void set_nonincremental_init_from_string (tree, struct obstack *);
110 static tree find_init_member (tree, struct obstack *);
111 static void readonly_warning (tree, enum lvalue_use);
112 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
113 static void record_maybe_used_decl (tree);
114 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
116 /* Return true if EXP is a null pointer constant, false otherwise. */
118 static bool
119 null_pointer_constant_p (const_tree expr)
121 /* This should really operate on c_expr structures, but they aren't
122 yet available everywhere required. */
123 tree type = TREE_TYPE (expr);
124 return (TREE_CODE (expr) == INTEGER_CST
125 && !TREE_OVERFLOW (expr)
126 && integer_zerop (expr)
127 && (INTEGRAL_TYPE_P (type)
128 || (TREE_CODE (type) == POINTER_TYPE
129 && VOID_TYPE_P (TREE_TYPE (type))
130 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
133 /* EXPR may appear in an unevaluated part of an integer constant
134 expression, but not in an evaluated part. Wrap it in a
135 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
136 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
138 static tree
139 note_integer_operands (tree expr)
141 tree ret;
142 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
144 ret = copy_node (expr);
145 TREE_OVERFLOW (ret) = 1;
147 else
149 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
150 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
152 return ret;
155 /* Having checked whether EXPR may appear in an unevaluated part of an
156 integer constant expression and found that it may, remove any
157 C_MAYBE_CONST_EXPR noting this fact and return the resulting
158 expression. */
160 static inline tree
161 remove_c_maybe_const_expr (tree expr)
163 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
164 return C_MAYBE_CONST_EXPR_EXPR (expr);
165 else
166 return expr;
169 \f/* This is a cache to hold if two types are compatible or not. */
171 struct tagged_tu_seen_cache {
172 const struct tagged_tu_seen_cache * next;
173 const_tree t1;
174 const_tree t2;
175 /* The return value of tagged_types_tu_compatible_p if we had seen
176 these two types already. */
177 int val;
180 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
181 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
183 /* Do `exp = require_complete_type (exp);' to make sure exp
184 does not have an incomplete type. (That includes void types.) */
186 tree
187 require_complete_type (tree value)
189 tree type = TREE_TYPE (value);
191 if (value == error_mark_node || type == error_mark_node)
192 return error_mark_node;
194 /* First, detect a valid value with a complete type. */
195 if (COMPLETE_TYPE_P (type))
196 return value;
198 c_incomplete_type_error (value, type);
199 return error_mark_node;
202 /* Print an error message for invalid use of an incomplete type.
203 VALUE is the expression that was used (or 0 if that isn't known)
204 and TYPE is the type that was invalid. */
206 void
207 c_incomplete_type_error (const_tree value, const_tree type)
209 const char *type_code_string;
211 /* Avoid duplicate error message. */
212 if (TREE_CODE (type) == ERROR_MARK)
213 return;
215 if (value != 0 && (TREE_CODE (value) == VAR_DECL
216 || TREE_CODE (value) == PARM_DECL))
217 error ("%qD has an incomplete type", value);
218 else
220 retry:
221 /* We must print an error message. Be clever about what it says. */
223 switch (TREE_CODE (type))
225 case RECORD_TYPE:
226 type_code_string = "struct";
227 break;
229 case UNION_TYPE:
230 type_code_string = "union";
231 break;
233 case ENUMERAL_TYPE:
234 type_code_string = "enum";
235 break;
237 case VOID_TYPE:
238 error ("invalid use of void expression");
239 return;
241 case ARRAY_TYPE:
242 if (TYPE_DOMAIN (type))
244 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
246 error ("invalid use of flexible array member");
247 return;
249 type = TREE_TYPE (type);
250 goto retry;
252 error ("invalid use of array with unspecified bounds");
253 return;
255 default:
256 gcc_unreachable ();
259 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
260 error ("invalid use of undefined type %<%s %E%>",
261 type_code_string, TYPE_NAME (type));
262 else
263 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
264 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
268 /* Given a type, apply default promotions wrt unnamed function
269 arguments and return the new type. */
271 tree
272 c_type_promotes_to (tree type)
274 if (TYPE_MAIN_VARIANT (type) == float_type_node)
275 return double_type_node;
277 if (c_promoting_integer_type_p (type))
279 /* Preserve unsignedness if not really getting any wider. */
280 if (TYPE_UNSIGNED (type)
281 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
282 return unsigned_type_node;
283 return integer_type_node;
286 return type;
289 /* Return true if between two named address spaces, whether there is a superset
290 named address space that encompasses both address spaces. If there is a
291 superset, return which address space is the superset. */
293 static bool
294 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
296 if (as1 == as2)
298 *common = as1;
299 return true;
301 else if (targetm.addr_space.subset_p (as1, as2))
303 *common = as2;
304 return true;
306 else if (targetm.addr_space.subset_p (as2, as1))
308 *common = as1;
309 return true;
311 else
312 return false;
315 /* Return a variant of TYPE which has all the type qualifiers of LIKE
316 as well as those of TYPE. */
318 static tree
319 qualify_type (tree type, tree like)
321 addr_space_t as_type = TYPE_ADDR_SPACE (type);
322 addr_space_t as_like = TYPE_ADDR_SPACE (like);
323 addr_space_t as_common;
325 /* If the two named address spaces are different, determine the common
326 superset address space. If there isn't one, raise an error. */
327 if (!addr_space_superset (as_type, as_like, &as_common))
329 as_common = as_type;
330 error ("%qT and %qT are in disjoint named address spaces",
331 type, like);
334 return c_build_qualified_type (type,
335 TYPE_QUALS_NO_ADDR_SPACE (type)
336 | TYPE_QUALS_NO_ADDR_SPACE (like)
337 | ENCODE_QUAL_ADDR_SPACE (as_common));
340 /* Return true iff the given tree T is a variable length array. */
342 bool
343 c_vla_type_p (const_tree t)
345 if (TREE_CODE (t) == ARRAY_TYPE
346 && C_TYPE_VARIABLE_SIZE (t))
347 return true;
348 return false;
351 /* Return the composite type of two compatible types.
353 We assume that comptypes has already been done and returned
354 nonzero; if that isn't so, this may crash. In particular, we
355 assume that qualifiers match. */
357 tree
358 composite_type (tree t1, tree t2)
360 enum tree_code code1;
361 enum tree_code code2;
362 tree attributes;
364 /* Save time if the two types are the same. */
366 if (t1 == t2) return t1;
368 /* If one type is nonsense, use the other. */
369 if (t1 == error_mark_node)
370 return t2;
371 if (t2 == error_mark_node)
372 return t1;
374 code1 = TREE_CODE (t1);
375 code2 = TREE_CODE (t2);
377 /* Merge the attributes. */
378 attributes = targetm.merge_type_attributes (t1, t2);
380 /* If one is an enumerated type and the other is the compatible
381 integer type, the composite type might be either of the two
382 (DR#013 question 3). For consistency, use the enumerated type as
383 the composite type. */
385 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
386 return t1;
387 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
388 return t2;
390 gcc_assert (code1 == code2);
392 switch (code1)
394 case POINTER_TYPE:
395 /* For two pointers, do this recursively on the target type. */
397 tree pointed_to_1 = TREE_TYPE (t1);
398 tree pointed_to_2 = TREE_TYPE (t2);
399 tree target = composite_type (pointed_to_1, pointed_to_2);
400 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
401 t1 = build_type_attribute_variant (t1, attributes);
402 return qualify_type (t1, t2);
405 case ARRAY_TYPE:
407 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
408 int quals;
409 tree unqual_elt;
410 tree d1 = TYPE_DOMAIN (t1);
411 tree d2 = TYPE_DOMAIN (t2);
412 bool d1_variable, d2_variable;
413 bool d1_zero, d2_zero;
414 bool t1_complete, t2_complete;
416 /* We should not have any type quals on arrays at all. */
417 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
418 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
420 t1_complete = COMPLETE_TYPE_P (t1);
421 t2_complete = COMPLETE_TYPE_P (t2);
423 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
424 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
426 d1_variable = (!d1_zero
427 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
428 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
429 d2_variable = (!d2_zero
430 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
431 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
432 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
433 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
435 /* Save space: see if the result is identical to one of the args. */
436 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
437 && (d2_variable || d2_zero || !d1_variable))
438 return build_type_attribute_variant (t1, attributes);
439 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
440 && (d1_variable || d1_zero || !d2_variable))
441 return build_type_attribute_variant (t2, attributes);
443 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
444 return build_type_attribute_variant (t1, attributes);
445 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
446 return build_type_attribute_variant (t2, attributes);
448 /* Merge the element types, and have a size if either arg has
449 one. We may have qualifiers on the element types. To set
450 up TYPE_MAIN_VARIANT correctly, we need to form the
451 composite of the unqualified types and add the qualifiers
452 back at the end. */
453 quals = TYPE_QUALS (strip_array_types (elt));
454 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
455 t1 = build_array_type (unqual_elt,
456 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
457 && (d2_variable
458 || d2_zero
459 || !d1_variable))
460 ? t1
461 : t2));
462 /* Ensure a composite type involving a zero-length array type
463 is a zero-length type not an incomplete type. */
464 if (d1_zero && d2_zero
465 && (t1_complete || t2_complete)
466 && !COMPLETE_TYPE_P (t1))
468 TYPE_SIZE (t1) = bitsize_zero_node;
469 TYPE_SIZE_UNIT (t1) = size_zero_node;
471 t1 = c_build_qualified_type (t1, quals);
472 return build_type_attribute_variant (t1, attributes);
475 case ENUMERAL_TYPE:
476 case RECORD_TYPE:
477 case UNION_TYPE:
478 if (attributes != NULL)
480 /* Try harder not to create a new aggregate type. */
481 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
482 return t1;
483 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
484 return t2;
486 return build_type_attribute_variant (t1, attributes);
488 case FUNCTION_TYPE:
489 /* Function types: prefer the one that specified arg types.
490 If both do, merge the arg types. Also merge the return types. */
492 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
493 tree p1 = TYPE_ARG_TYPES (t1);
494 tree p2 = TYPE_ARG_TYPES (t2);
495 int len;
496 tree newargs, n;
497 int i;
499 /* Save space: see if the result is identical to one of the args. */
500 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
501 return build_type_attribute_variant (t1, attributes);
502 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
503 return build_type_attribute_variant (t2, attributes);
505 /* Simple way if one arg fails to specify argument types. */
506 if (TYPE_ARG_TYPES (t1) == 0)
508 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
509 t1 = build_type_attribute_variant (t1, attributes);
510 return qualify_type (t1, t2);
512 if (TYPE_ARG_TYPES (t2) == 0)
514 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
515 t1 = build_type_attribute_variant (t1, attributes);
516 return qualify_type (t1, t2);
519 /* If both args specify argument types, we must merge the two
520 lists, argument by argument. */
522 len = list_length (p1);
523 newargs = 0;
525 for (i = 0; i < len; i++)
526 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
528 n = newargs;
530 for (; p1;
531 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
533 /* A null type means arg type is not specified.
534 Take whatever the other function type has. */
535 if (TREE_VALUE (p1) == 0)
537 TREE_VALUE (n) = TREE_VALUE (p2);
538 goto parm_done;
540 if (TREE_VALUE (p2) == 0)
542 TREE_VALUE (n) = TREE_VALUE (p1);
543 goto parm_done;
546 /* Given wait (union {union wait *u; int *i} *)
547 and wait (union wait *),
548 prefer union wait * as type of parm. */
549 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
550 && TREE_VALUE (p1) != TREE_VALUE (p2))
552 tree memb;
553 tree mv2 = TREE_VALUE (p2);
554 if (mv2 && mv2 != error_mark_node
555 && TREE_CODE (mv2) != ARRAY_TYPE)
556 mv2 = TYPE_MAIN_VARIANT (mv2);
557 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
558 memb; memb = DECL_CHAIN (memb))
560 tree mv3 = TREE_TYPE (memb);
561 if (mv3 && mv3 != error_mark_node
562 && TREE_CODE (mv3) != ARRAY_TYPE)
563 mv3 = TYPE_MAIN_VARIANT (mv3);
564 if (comptypes (mv3, mv2))
566 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
567 TREE_VALUE (p2));
568 pedwarn (input_location, OPT_Wpedantic,
569 "function types not truly compatible in ISO C");
570 goto parm_done;
574 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
575 && TREE_VALUE (p2) != TREE_VALUE (p1))
577 tree memb;
578 tree mv1 = TREE_VALUE (p1);
579 if (mv1 && mv1 != error_mark_node
580 && TREE_CODE (mv1) != ARRAY_TYPE)
581 mv1 = TYPE_MAIN_VARIANT (mv1);
582 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
583 memb; memb = DECL_CHAIN (memb))
585 tree mv3 = TREE_TYPE (memb);
586 if (mv3 && mv3 != error_mark_node
587 && TREE_CODE (mv3) != ARRAY_TYPE)
588 mv3 = TYPE_MAIN_VARIANT (mv3);
589 if (comptypes (mv3, mv1))
591 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
592 TREE_VALUE (p1));
593 pedwarn (input_location, OPT_Wpedantic,
594 "function types not truly compatible in ISO C");
595 goto parm_done;
599 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
600 parm_done: ;
603 t1 = build_function_type (valtype, newargs);
604 t1 = qualify_type (t1, t2);
605 /* ... falls through ... */
608 default:
609 return build_type_attribute_variant (t1, attributes);
614 /* Return the type of a conditional expression between pointers to
615 possibly differently qualified versions of compatible types.
617 We assume that comp_target_types has already been done and returned
618 nonzero; if that isn't so, this may crash. */
620 static tree
621 common_pointer_type (tree t1, tree t2)
623 tree attributes;
624 tree pointed_to_1, mv1;
625 tree pointed_to_2, mv2;
626 tree target;
627 unsigned target_quals;
628 addr_space_t as1, as2, as_common;
629 int quals1, quals2;
631 /* Save time if the two types are the same. */
633 if (t1 == t2) return t1;
635 /* If one type is nonsense, use the other. */
636 if (t1 == error_mark_node)
637 return t2;
638 if (t2 == error_mark_node)
639 return t1;
641 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
642 && TREE_CODE (t2) == POINTER_TYPE);
644 /* Merge the attributes. */
645 attributes = targetm.merge_type_attributes (t1, t2);
647 /* Find the composite type of the target types, and combine the
648 qualifiers of the two types' targets. Do not lose qualifiers on
649 array element types by taking the TYPE_MAIN_VARIANT. */
650 mv1 = pointed_to_1 = TREE_TYPE (t1);
651 mv2 = pointed_to_2 = TREE_TYPE (t2);
652 if (TREE_CODE (mv1) != ARRAY_TYPE)
653 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
654 if (TREE_CODE (mv2) != ARRAY_TYPE)
655 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
656 target = composite_type (mv1, mv2);
658 /* For function types do not merge const qualifiers, but drop them
659 if used inconsistently. The middle-end uses these to mark const
660 and noreturn functions. */
661 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
662 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
664 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
665 target_quals = (quals1 & quals2);
666 else
667 target_quals = (quals1 | quals2);
669 /* If the two named address spaces are different, determine the common
670 superset address space. This is guaranteed to exist due to the
671 assumption that comp_target_type returned non-zero. */
672 as1 = TYPE_ADDR_SPACE (pointed_to_1);
673 as2 = TYPE_ADDR_SPACE (pointed_to_2);
674 if (!addr_space_superset (as1, as2, &as_common))
675 gcc_unreachable ();
677 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
679 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
680 return build_type_attribute_variant (t1, attributes);
683 /* Return the common type for two arithmetic types under the usual
684 arithmetic conversions. The default conversions have already been
685 applied, and enumerated types converted to their compatible integer
686 types. The resulting type is unqualified and has no attributes.
688 This is the type for the result of most arithmetic operations
689 if the operands have the given two types. */
691 static tree
692 c_common_type (tree t1, tree t2)
694 enum tree_code code1;
695 enum tree_code code2;
697 /* If one type is nonsense, use the other. */
698 if (t1 == error_mark_node)
699 return t2;
700 if (t2 == error_mark_node)
701 return t1;
703 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
704 t1 = TYPE_MAIN_VARIANT (t1);
706 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
707 t2 = TYPE_MAIN_VARIANT (t2);
709 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
710 t1 = build_type_attribute_variant (t1, NULL_TREE);
712 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
713 t2 = build_type_attribute_variant (t2, NULL_TREE);
715 /* Save time if the two types are the same. */
717 if (t1 == t2) return t1;
719 code1 = TREE_CODE (t1);
720 code2 = TREE_CODE (t2);
722 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
723 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
724 || code1 == INTEGER_TYPE);
725 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
726 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
727 || code2 == INTEGER_TYPE);
729 /* When one operand is a decimal float type, the other operand cannot be
730 a generic float type or a complex type. We also disallow vector types
731 here. */
732 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
733 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
735 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
737 error ("can%'t mix operands of decimal float and vector types");
738 return error_mark_node;
740 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
742 error ("can%'t mix operands of decimal float and complex types");
743 return error_mark_node;
745 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
747 error ("can%'t mix operands of decimal float and other float types");
748 return error_mark_node;
752 /* If one type is a vector type, return that type. (How the usual
753 arithmetic conversions apply to the vector types extension is not
754 precisely specified.) */
755 if (code1 == VECTOR_TYPE)
756 return t1;
758 if (code2 == VECTOR_TYPE)
759 return t2;
761 /* If one type is complex, form the common type of the non-complex
762 components, then make that complex. Use T1 or T2 if it is the
763 required type. */
764 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
766 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
767 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
768 tree subtype = c_common_type (subtype1, subtype2);
770 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
771 return t1;
772 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
773 return t2;
774 else
775 return build_complex_type (subtype);
778 /* If only one is real, use it as the result. */
780 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
781 return t1;
783 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
784 return t2;
786 /* If both are real and either are decimal floating point types, use
787 the decimal floating point type with the greater precision. */
789 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
791 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
792 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
793 return dfloat128_type_node;
794 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
795 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
796 return dfloat64_type_node;
797 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
798 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
799 return dfloat32_type_node;
802 /* Deal with fixed-point types. */
803 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
805 unsigned int unsignedp = 0, satp = 0;
806 enum machine_mode m1, m2;
807 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
809 m1 = TYPE_MODE (t1);
810 m2 = TYPE_MODE (t2);
812 /* If one input type is saturating, the result type is saturating. */
813 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
814 satp = 1;
816 /* If both fixed-point types are unsigned, the result type is unsigned.
817 When mixing fixed-point and integer types, follow the sign of the
818 fixed-point type.
819 Otherwise, the result type is signed. */
820 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
821 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
822 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
823 && TYPE_UNSIGNED (t1))
824 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
825 && TYPE_UNSIGNED (t2)))
826 unsignedp = 1;
828 /* The result type is signed. */
829 if (unsignedp == 0)
831 /* If the input type is unsigned, we need to convert to the
832 signed type. */
833 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
835 enum mode_class mclass = (enum mode_class) 0;
836 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
837 mclass = MODE_FRACT;
838 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
839 mclass = MODE_ACCUM;
840 else
841 gcc_unreachable ();
842 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
844 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
846 enum mode_class mclass = (enum mode_class) 0;
847 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
848 mclass = MODE_FRACT;
849 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
850 mclass = MODE_ACCUM;
851 else
852 gcc_unreachable ();
853 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
857 if (code1 == FIXED_POINT_TYPE)
859 fbit1 = GET_MODE_FBIT (m1);
860 ibit1 = GET_MODE_IBIT (m1);
862 else
864 fbit1 = 0;
865 /* Signed integers need to subtract one sign bit. */
866 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
869 if (code2 == FIXED_POINT_TYPE)
871 fbit2 = GET_MODE_FBIT (m2);
872 ibit2 = GET_MODE_IBIT (m2);
874 else
876 fbit2 = 0;
877 /* Signed integers need to subtract one sign bit. */
878 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
881 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
882 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
883 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
884 satp);
887 /* Both real or both integers; use the one with greater precision. */
889 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
890 return t1;
891 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
892 return t2;
894 /* Same precision. Prefer long longs to longs to ints when the
895 same precision, following the C99 rules on integer type rank
896 (which are equivalent to the C90 rules for C90 types). */
898 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
899 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
900 return long_long_unsigned_type_node;
902 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
903 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
905 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
906 return long_long_unsigned_type_node;
907 else
908 return long_long_integer_type_node;
911 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
912 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
913 return long_unsigned_type_node;
915 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
916 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
918 /* But preserve unsignedness from the other type,
919 since long cannot hold all the values of an unsigned int. */
920 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
921 return long_unsigned_type_node;
922 else
923 return long_integer_type_node;
926 /* Likewise, prefer long double to double even if same size. */
927 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
928 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
929 return long_double_type_node;
931 /* Otherwise prefer the unsigned one. */
933 if (TYPE_UNSIGNED (t1))
934 return t1;
935 else
936 return t2;
939 /* Wrapper around c_common_type that is used by c-common.c and other
940 front end optimizations that remove promotions. ENUMERAL_TYPEs
941 are allowed here and are converted to their compatible integer types.
942 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
943 preferably a non-Boolean type as the common type. */
944 tree
945 common_type (tree t1, tree t2)
947 if (TREE_CODE (t1) == ENUMERAL_TYPE)
948 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
949 if (TREE_CODE (t2) == ENUMERAL_TYPE)
950 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
952 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
953 if (TREE_CODE (t1) == BOOLEAN_TYPE
954 && TREE_CODE (t2) == BOOLEAN_TYPE)
955 return boolean_type_node;
957 /* If either type is BOOLEAN_TYPE, then return the other. */
958 if (TREE_CODE (t1) == BOOLEAN_TYPE)
959 return t2;
960 if (TREE_CODE (t2) == BOOLEAN_TYPE)
961 return t1;
963 return c_common_type (t1, t2);
966 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
967 or various other operations. Return 2 if they are compatible
968 but a warning may be needed if you use them together. */
971 comptypes (tree type1, tree type2)
973 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
974 int val;
976 val = comptypes_internal (type1, type2, NULL, NULL);
977 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
979 return val;
982 /* Like comptypes, but if it returns non-zero because enum and int are
983 compatible, it sets *ENUM_AND_INT_P to true. */
985 static int
986 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
988 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
989 int val;
991 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
992 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
994 return val;
997 /* Like comptypes, but if it returns nonzero for different types, it
998 sets *DIFFERENT_TYPES_P to true. */
1001 comptypes_check_different_types (tree type1, tree type2,
1002 bool *different_types_p)
1004 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1005 int val;
1007 val = comptypes_internal (type1, type2, NULL, different_types_p);
1008 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1010 return val;
1013 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1014 or various other operations. Return 2 if they are compatible
1015 but a warning may be needed if you use them together. If
1016 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1017 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1018 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1019 NULL, and the types are compatible but different enough not to be
1020 permitted in C11 typedef redeclarations, then this sets
1021 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1022 false, but may or may not be set if the types are incompatible.
1023 This differs from comptypes, in that we don't free the seen
1024 types. */
1026 static int
1027 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1028 bool *different_types_p)
1030 const_tree t1 = type1;
1031 const_tree t2 = type2;
1032 int attrval, val;
1034 /* Suppress errors caused by previously reported errors. */
1036 if (t1 == t2 || !t1 || !t2
1037 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1038 return 1;
1040 /* Enumerated types are compatible with integer types, but this is
1041 not transitive: two enumerated types in the same translation unit
1042 are compatible with each other only if they are the same type. */
1044 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1046 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1047 if (TREE_CODE (t2) != VOID_TYPE)
1049 if (enum_and_int_p != NULL)
1050 *enum_and_int_p = true;
1051 if (different_types_p != NULL)
1052 *different_types_p = true;
1055 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1057 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1058 if (TREE_CODE (t1) != VOID_TYPE)
1060 if (enum_and_int_p != NULL)
1061 *enum_and_int_p = true;
1062 if (different_types_p != NULL)
1063 *different_types_p = true;
1067 if (t1 == t2)
1068 return 1;
1070 /* Different classes of types can't be compatible. */
1072 if (TREE_CODE (t1) != TREE_CODE (t2))
1073 return 0;
1075 /* Qualifiers must match. C99 6.7.3p9 */
1077 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1078 return 0;
1080 /* Allow for two different type nodes which have essentially the same
1081 definition. Note that we already checked for equality of the type
1082 qualifiers (just above). */
1084 if (TREE_CODE (t1) != ARRAY_TYPE
1085 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1086 return 1;
1088 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1089 if (!(attrval = comp_type_attributes (t1, t2)))
1090 return 0;
1092 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1093 val = 0;
1095 switch (TREE_CODE (t1))
1097 case POINTER_TYPE:
1098 /* Do not remove mode or aliasing information. */
1099 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1100 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1101 break;
1102 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1103 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1104 enum_and_int_p, different_types_p));
1105 break;
1107 case FUNCTION_TYPE:
1108 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1109 different_types_p);
1110 break;
1112 case ARRAY_TYPE:
1114 tree d1 = TYPE_DOMAIN (t1);
1115 tree d2 = TYPE_DOMAIN (t2);
1116 bool d1_variable, d2_variable;
1117 bool d1_zero, d2_zero;
1118 val = 1;
1120 /* Target types must match incl. qualifiers. */
1121 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1122 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1123 enum_and_int_p,
1124 different_types_p)))
1125 return 0;
1127 if (different_types_p != NULL
1128 && (d1 == 0) != (d2 == 0))
1129 *different_types_p = true;
1130 /* Sizes must match unless one is missing or variable. */
1131 if (d1 == 0 || d2 == 0 || d1 == d2)
1132 break;
1134 d1_zero = !TYPE_MAX_VALUE (d1);
1135 d2_zero = !TYPE_MAX_VALUE (d2);
1137 d1_variable = (!d1_zero
1138 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1139 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1140 d2_variable = (!d2_zero
1141 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1142 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1143 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1144 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1146 if (different_types_p != NULL
1147 && d1_variable != d2_variable)
1148 *different_types_p = true;
1149 if (d1_variable || d2_variable)
1150 break;
1151 if (d1_zero && d2_zero)
1152 break;
1153 if (d1_zero || d2_zero
1154 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1155 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1156 val = 0;
1158 break;
1161 case ENUMERAL_TYPE:
1162 case RECORD_TYPE:
1163 case UNION_TYPE:
1164 if (val != 1 && !same_translation_unit_p (t1, t2))
1166 tree a1 = TYPE_ATTRIBUTES (t1);
1167 tree a2 = TYPE_ATTRIBUTES (t2);
1169 if (! attribute_list_contained (a1, a2)
1170 && ! attribute_list_contained (a2, a1))
1171 break;
1173 if (attrval != 2)
1174 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1175 different_types_p);
1176 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1177 different_types_p);
1179 break;
1181 case VECTOR_TYPE:
1182 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1183 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1184 enum_and_int_p, different_types_p));
1185 break;
1187 default:
1188 break;
1190 return attrval == 2 && val == 1 ? 2 : val;
1193 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1194 their qualifiers, except for named address spaces. If the pointers point to
1195 different named addresses, then we must determine if one address space is a
1196 subset of the other. */
1198 static int
1199 comp_target_types (location_t location, tree ttl, tree ttr)
1201 int val;
1202 tree mvl = TREE_TYPE (ttl);
1203 tree mvr = TREE_TYPE (ttr);
1204 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1205 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1206 addr_space_t as_common;
1207 bool enum_and_int_p;
1209 /* Fail if pointers point to incompatible address spaces. */
1210 if (!addr_space_superset (asl, asr, &as_common))
1211 return 0;
1213 /* Do not lose qualifiers on element types of array types that are
1214 pointer targets by taking their TYPE_MAIN_VARIANT. */
1215 if (TREE_CODE (mvl) != ARRAY_TYPE)
1216 mvl = TYPE_MAIN_VARIANT (mvl);
1217 if (TREE_CODE (mvr) != ARRAY_TYPE)
1218 mvr = TYPE_MAIN_VARIANT (mvr);
1219 enum_and_int_p = false;
1220 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1222 if (val == 2)
1223 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1225 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1226 warning_at (location, OPT_Wc___compat,
1227 "pointer target types incompatible in C++");
1229 return val;
1232 /* Subroutines of `comptypes'. */
1234 /* Determine whether two trees derive from the same translation unit.
1235 If the CONTEXT chain ends in a null, that tree's context is still
1236 being parsed, so if two trees have context chains ending in null,
1237 they're in the same translation unit. */
1239 same_translation_unit_p (const_tree t1, const_tree t2)
1241 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1242 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1244 case tcc_declaration:
1245 t1 = DECL_CONTEXT (t1); break;
1246 case tcc_type:
1247 t1 = TYPE_CONTEXT (t1); break;
1248 case tcc_exceptional:
1249 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1250 default: gcc_unreachable ();
1253 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1254 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1256 case tcc_declaration:
1257 t2 = DECL_CONTEXT (t2); break;
1258 case tcc_type:
1259 t2 = TYPE_CONTEXT (t2); break;
1260 case tcc_exceptional:
1261 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1262 default: gcc_unreachable ();
1265 return t1 == t2;
1268 /* Allocate the seen two types, assuming that they are compatible. */
1270 static struct tagged_tu_seen_cache *
1271 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1273 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1274 tu->next = tagged_tu_seen_base;
1275 tu->t1 = t1;
1276 tu->t2 = t2;
1278 tagged_tu_seen_base = tu;
1280 /* The C standard says that two structures in different translation
1281 units are compatible with each other only if the types of their
1282 fields are compatible (among other things). We assume that they
1283 are compatible until proven otherwise when building the cache.
1284 An example where this can occur is:
1285 struct a
1287 struct a *next;
1289 If we are comparing this against a similar struct in another TU,
1290 and did not assume they were compatible, we end up with an infinite
1291 loop. */
1292 tu->val = 1;
1293 return tu;
1296 /* Free the seen types until we get to TU_TIL. */
1298 static void
1299 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1301 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1302 while (tu != tu_til)
1304 const struct tagged_tu_seen_cache *const tu1
1305 = (const struct tagged_tu_seen_cache *) tu;
1306 tu = tu1->next;
1307 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1309 tagged_tu_seen_base = tu_til;
1312 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1313 compatible. If the two types are not the same (which has been
1314 checked earlier), this can only happen when multiple translation
1315 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1316 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1317 comptypes_internal. */
1319 static int
1320 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1321 bool *enum_and_int_p, bool *different_types_p)
1323 tree s1, s2;
1324 bool needs_warning = false;
1326 /* We have to verify that the tags of the types are the same. This
1327 is harder than it looks because this may be a typedef, so we have
1328 to go look at the original type. It may even be a typedef of a
1329 typedef...
1330 In the case of compiler-created builtin structs the TYPE_DECL
1331 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1332 while (TYPE_NAME (t1)
1333 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1334 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1335 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1337 while (TYPE_NAME (t2)
1338 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1339 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1340 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1342 /* C90 didn't have the requirement that the two tags be the same. */
1343 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1344 return 0;
1346 /* C90 didn't say what happened if one or both of the types were
1347 incomplete; we choose to follow C99 rules here, which is that they
1348 are compatible. */
1349 if (TYPE_SIZE (t1) == NULL
1350 || TYPE_SIZE (t2) == NULL)
1351 return 1;
1354 const struct tagged_tu_seen_cache * tts_i;
1355 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1356 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1357 return tts_i->val;
1360 switch (TREE_CODE (t1))
1362 case ENUMERAL_TYPE:
1364 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1365 /* Speed up the case where the type values are in the same order. */
1366 tree tv1 = TYPE_VALUES (t1);
1367 tree tv2 = TYPE_VALUES (t2);
1369 if (tv1 == tv2)
1371 return 1;
1374 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1376 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1377 break;
1378 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1380 tu->val = 0;
1381 return 0;
1385 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1387 return 1;
1389 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1391 tu->val = 0;
1392 return 0;
1395 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1397 tu->val = 0;
1398 return 0;
1401 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1403 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1404 if (s2 == NULL
1405 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1407 tu->val = 0;
1408 return 0;
1411 return 1;
1414 case UNION_TYPE:
1416 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1417 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1419 tu->val = 0;
1420 return 0;
1423 /* Speed up the common case where the fields are in the same order. */
1424 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1425 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1427 int result;
1429 if (DECL_NAME (s1) != DECL_NAME (s2))
1430 break;
1431 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1432 enum_and_int_p, different_types_p);
1434 if (result != 1 && !DECL_NAME (s1))
1435 break;
1436 if (result == 0)
1438 tu->val = 0;
1439 return 0;
1441 if (result == 2)
1442 needs_warning = true;
1444 if (TREE_CODE (s1) == FIELD_DECL
1445 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1446 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1448 tu->val = 0;
1449 return 0;
1452 if (!s1 && !s2)
1454 tu->val = needs_warning ? 2 : 1;
1455 return tu->val;
1458 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1460 bool ok = false;
1462 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1463 if (DECL_NAME (s1) == DECL_NAME (s2))
1465 int result;
1467 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1468 enum_and_int_p,
1469 different_types_p);
1471 if (result != 1 && !DECL_NAME (s1))
1472 continue;
1473 if (result == 0)
1475 tu->val = 0;
1476 return 0;
1478 if (result == 2)
1479 needs_warning = true;
1481 if (TREE_CODE (s1) == FIELD_DECL
1482 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1483 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1484 break;
1486 ok = true;
1487 break;
1489 if (!ok)
1491 tu->val = 0;
1492 return 0;
1495 tu->val = needs_warning ? 2 : 10;
1496 return tu->val;
1499 case RECORD_TYPE:
1501 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1503 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1504 s1 && s2;
1505 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1507 int result;
1508 if (TREE_CODE (s1) != TREE_CODE (s2)
1509 || DECL_NAME (s1) != DECL_NAME (s2))
1510 break;
1511 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1512 enum_and_int_p, different_types_p);
1513 if (result == 0)
1514 break;
1515 if (result == 2)
1516 needs_warning = true;
1518 if (TREE_CODE (s1) == FIELD_DECL
1519 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1520 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1521 break;
1523 if (s1 && s2)
1524 tu->val = 0;
1525 else
1526 tu->val = needs_warning ? 2 : 1;
1527 return tu->val;
1530 default:
1531 gcc_unreachable ();
1535 /* Return 1 if two function types F1 and F2 are compatible.
1536 If either type specifies no argument types,
1537 the other must specify a fixed number of self-promoting arg types.
1538 Otherwise, if one type specifies only the number of arguments,
1539 the other must specify that number of self-promoting arg types.
1540 Otherwise, the argument types must match.
1541 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1543 static int
1544 function_types_compatible_p (const_tree f1, const_tree f2,
1545 bool *enum_and_int_p, bool *different_types_p)
1547 tree args1, args2;
1548 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1549 int val = 1;
1550 int val1;
1551 tree ret1, ret2;
1553 ret1 = TREE_TYPE (f1);
1554 ret2 = TREE_TYPE (f2);
1556 /* 'volatile' qualifiers on a function's return type used to mean
1557 the function is noreturn. */
1558 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1559 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1560 if (TYPE_VOLATILE (ret1))
1561 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1562 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1563 if (TYPE_VOLATILE (ret2))
1564 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1565 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1566 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1567 if (val == 0)
1568 return 0;
1570 args1 = TYPE_ARG_TYPES (f1);
1571 args2 = TYPE_ARG_TYPES (f2);
1573 if (different_types_p != NULL
1574 && (args1 == 0) != (args2 == 0))
1575 *different_types_p = true;
1577 /* An unspecified parmlist matches any specified parmlist
1578 whose argument types don't need default promotions. */
1580 if (args1 == 0)
1582 if (!self_promoting_args_p (args2))
1583 return 0;
1584 /* If one of these types comes from a non-prototype fn definition,
1585 compare that with the other type's arglist.
1586 If they don't match, ask for a warning (but no error). */
1587 if (TYPE_ACTUAL_ARG_TYPES (f1)
1588 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1589 enum_and_int_p, different_types_p))
1590 val = 2;
1591 return val;
1593 if (args2 == 0)
1595 if (!self_promoting_args_p (args1))
1596 return 0;
1597 if (TYPE_ACTUAL_ARG_TYPES (f2)
1598 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1599 enum_and_int_p, different_types_p))
1600 val = 2;
1601 return val;
1604 /* Both types have argument lists: compare them and propagate results. */
1605 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1606 different_types_p);
1607 return val1 != 1 ? val1 : val;
1610 /* Check two lists of types for compatibility, returning 0 for
1611 incompatible, 1 for compatible, or 2 for compatible with
1612 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1613 comptypes_internal. */
1615 static int
1616 type_lists_compatible_p (const_tree args1, const_tree args2,
1617 bool *enum_and_int_p, bool *different_types_p)
1619 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1620 int val = 1;
1621 int newval = 0;
1623 while (1)
1625 tree a1, mv1, a2, mv2;
1626 if (args1 == 0 && args2 == 0)
1627 return val;
1628 /* If one list is shorter than the other,
1629 they fail to match. */
1630 if (args1 == 0 || args2 == 0)
1631 return 0;
1632 mv1 = a1 = TREE_VALUE (args1);
1633 mv2 = a2 = TREE_VALUE (args2);
1634 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1635 mv1 = TYPE_MAIN_VARIANT (mv1);
1636 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1637 mv2 = TYPE_MAIN_VARIANT (mv2);
1638 /* A null pointer instead of a type
1639 means there is supposed to be an argument
1640 but nothing is specified about what type it has.
1641 So match anything that self-promotes. */
1642 if (different_types_p != NULL
1643 && (a1 == 0) != (a2 == 0))
1644 *different_types_p = true;
1645 if (a1 == 0)
1647 if (c_type_promotes_to (a2) != a2)
1648 return 0;
1650 else if (a2 == 0)
1652 if (c_type_promotes_to (a1) != a1)
1653 return 0;
1655 /* If one of the lists has an error marker, ignore this arg. */
1656 else if (TREE_CODE (a1) == ERROR_MARK
1657 || TREE_CODE (a2) == ERROR_MARK)
1659 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1660 different_types_p)))
1662 if (different_types_p != NULL)
1663 *different_types_p = true;
1664 /* Allow wait (union {union wait *u; int *i} *)
1665 and wait (union wait *) to be compatible. */
1666 if (TREE_CODE (a1) == UNION_TYPE
1667 && (TYPE_NAME (a1) == 0
1668 || TYPE_TRANSPARENT_AGGR (a1))
1669 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1670 && tree_int_cst_equal (TYPE_SIZE (a1),
1671 TYPE_SIZE (a2)))
1673 tree memb;
1674 for (memb = TYPE_FIELDS (a1);
1675 memb; memb = DECL_CHAIN (memb))
1677 tree mv3 = TREE_TYPE (memb);
1678 if (mv3 && mv3 != error_mark_node
1679 && TREE_CODE (mv3) != ARRAY_TYPE)
1680 mv3 = TYPE_MAIN_VARIANT (mv3);
1681 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1682 different_types_p))
1683 break;
1685 if (memb == 0)
1686 return 0;
1688 else if (TREE_CODE (a2) == UNION_TYPE
1689 && (TYPE_NAME (a2) == 0
1690 || TYPE_TRANSPARENT_AGGR (a2))
1691 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1692 && tree_int_cst_equal (TYPE_SIZE (a2),
1693 TYPE_SIZE (a1)))
1695 tree memb;
1696 for (memb = TYPE_FIELDS (a2);
1697 memb; memb = DECL_CHAIN (memb))
1699 tree mv3 = TREE_TYPE (memb);
1700 if (mv3 && mv3 != error_mark_node
1701 && TREE_CODE (mv3) != ARRAY_TYPE)
1702 mv3 = TYPE_MAIN_VARIANT (mv3);
1703 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1704 different_types_p))
1705 break;
1707 if (memb == 0)
1708 return 0;
1710 else
1711 return 0;
1714 /* comptypes said ok, but record if it said to warn. */
1715 if (newval > val)
1716 val = newval;
1718 args1 = TREE_CHAIN (args1);
1719 args2 = TREE_CHAIN (args2);
1723 /* Compute the size to increment a pointer by. */
1725 static tree
1726 c_size_in_bytes (const_tree type)
1728 enum tree_code code = TREE_CODE (type);
1730 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1731 return size_one_node;
1733 if (!COMPLETE_OR_VOID_TYPE_P (type))
1735 error ("arithmetic on pointer to an incomplete type");
1736 return size_one_node;
1739 /* Convert in case a char is more than one unit. */
1740 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1741 size_int (TYPE_PRECISION (char_type_node)
1742 / BITS_PER_UNIT));
1745 /* Return either DECL or its known constant value (if it has one). */
1747 tree
1748 decl_constant_value (tree decl)
1750 if (/* Don't change a variable array bound or initial value to a constant
1751 in a place where a variable is invalid. Note that DECL_INITIAL
1752 isn't valid for a PARM_DECL. */
1753 current_function_decl != 0
1754 && TREE_CODE (decl) != PARM_DECL
1755 && !TREE_THIS_VOLATILE (decl)
1756 && TREE_READONLY (decl)
1757 && DECL_INITIAL (decl) != 0
1758 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1759 /* This is invalid if initial value is not constant.
1760 If it has either a function call, a memory reference,
1761 or a variable, then re-evaluating it could give different results. */
1762 && TREE_CONSTANT (DECL_INITIAL (decl))
1763 /* Check for cases where this is sub-optimal, even though valid. */
1764 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1765 return DECL_INITIAL (decl);
1766 return decl;
1769 /* Convert the array expression EXP to a pointer. */
1770 static tree
1771 array_to_pointer_conversion (location_t loc, tree exp)
1773 tree orig_exp = exp;
1774 tree type = TREE_TYPE (exp);
1775 tree adr;
1776 tree restype = TREE_TYPE (type);
1777 tree ptrtype;
1779 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1781 STRIP_TYPE_NOPS (exp);
1783 if (TREE_NO_WARNING (orig_exp))
1784 TREE_NO_WARNING (exp) = 1;
1786 ptrtype = build_pointer_type (restype);
1788 if (TREE_CODE (exp) == INDIRECT_REF)
1789 return convert (ptrtype, TREE_OPERAND (exp, 0));
1791 /* In C++ array compound literals are temporary objects unless they are
1792 const or appear in namespace scope, so they are destroyed too soon
1793 to use them for much of anything (c++/53220). */
1794 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1796 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1797 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1798 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1799 "converting an array compound literal to a pointer "
1800 "is ill-formed in C++");
1803 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1804 return convert (ptrtype, adr);
1807 /* Convert the function expression EXP to a pointer. */
1808 static tree
1809 function_to_pointer_conversion (location_t loc, tree exp)
1811 tree orig_exp = exp;
1813 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1815 STRIP_TYPE_NOPS (exp);
1817 if (TREE_NO_WARNING (orig_exp))
1818 TREE_NO_WARNING (exp) = 1;
1820 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1823 /* Mark EXP as read, not just set, for set but not used -Wunused
1824 warning purposes. */
1826 void
1827 mark_exp_read (tree exp)
1829 switch (TREE_CODE (exp))
1831 case VAR_DECL:
1832 case PARM_DECL:
1833 DECL_READ_P (exp) = 1;
1834 break;
1835 case ARRAY_REF:
1836 case COMPONENT_REF:
1837 case MODIFY_EXPR:
1838 case REALPART_EXPR:
1839 case IMAGPART_EXPR:
1840 CASE_CONVERT:
1841 case ADDR_EXPR:
1842 mark_exp_read (TREE_OPERAND (exp, 0));
1843 break;
1844 case COMPOUND_EXPR:
1845 case C_MAYBE_CONST_EXPR:
1846 mark_exp_read (TREE_OPERAND (exp, 1));
1847 break;
1848 default:
1849 break;
1853 /* Perform the default conversion of arrays and functions to pointers.
1854 Return the result of converting EXP. For any other expression, just
1855 return EXP.
1857 LOC is the location of the expression. */
1859 struct c_expr
1860 default_function_array_conversion (location_t loc, struct c_expr exp)
1862 tree orig_exp = exp.value;
1863 tree type = TREE_TYPE (exp.value);
1864 enum tree_code code = TREE_CODE (type);
1866 switch (code)
1868 case ARRAY_TYPE:
1870 bool not_lvalue = false;
1871 bool lvalue_array_p;
1873 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1874 || CONVERT_EXPR_P (exp.value))
1875 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1877 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1878 not_lvalue = true;
1879 exp.value = TREE_OPERAND (exp.value, 0);
1882 if (TREE_NO_WARNING (orig_exp))
1883 TREE_NO_WARNING (exp.value) = 1;
1885 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1886 if (!flag_isoc99 && !lvalue_array_p)
1888 /* Before C99, non-lvalue arrays do not decay to pointers.
1889 Normally, using such an array would be invalid; but it can
1890 be used correctly inside sizeof or as a statement expression.
1891 Thus, do not give an error here; an error will result later. */
1892 return exp;
1895 exp.value = array_to_pointer_conversion (loc, exp.value);
1897 break;
1898 case FUNCTION_TYPE:
1899 exp.value = function_to_pointer_conversion (loc, exp.value);
1900 break;
1901 default:
1902 break;
1905 return exp;
1908 struct c_expr
1909 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1911 mark_exp_read (exp.value);
1912 return default_function_array_conversion (loc, exp);
1915 /* EXP is an expression of integer type. Apply the integer promotions
1916 to it and return the promoted value. */
1918 tree
1919 perform_integral_promotions (tree exp)
1921 tree type = TREE_TYPE (exp);
1922 enum tree_code code = TREE_CODE (type);
1924 gcc_assert (INTEGRAL_TYPE_P (type));
1926 /* Normally convert enums to int,
1927 but convert wide enums to something wider. */
1928 if (code == ENUMERAL_TYPE)
1930 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1931 TYPE_PRECISION (integer_type_node)),
1932 ((TYPE_PRECISION (type)
1933 >= TYPE_PRECISION (integer_type_node))
1934 && TYPE_UNSIGNED (type)));
1936 return convert (type, exp);
1939 /* ??? This should no longer be needed now bit-fields have their
1940 proper types. */
1941 if (TREE_CODE (exp) == COMPONENT_REF
1942 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1943 /* If it's thinner than an int, promote it like a
1944 c_promoting_integer_type_p, otherwise leave it alone. */
1945 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1946 TYPE_PRECISION (integer_type_node)))
1947 return convert (integer_type_node, exp);
1949 if (c_promoting_integer_type_p (type))
1951 /* Preserve unsignedness if not really getting any wider. */
1952 if (TYPE_UNSIGNED (type)
1953 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1954 return convert (unsigned_type_node, exp);
1956 return convert (integer_type_node, exp);
1959 return exp;
1963 /* Perform default promotions for C data used in expressions.
1964 Enumeral types or short or char are converted to int.
1965 In addition, manifest constants symbols are replaced by their values. */
1967 tree
1968 default_conversion (tree exp)
1970 tree orig_exp;
1971 tree type = TREE_TYPE (exp);
1972 enum tree_code code = TREE_CODE (type);
1973 tree promoted_type;
1975 mark_exp_read (exp);
1977 /* Functions and arrays have been converted during parsing. */
1978 gcc_assert (code != FUNCTION_TYPE);
1979 if (code == ARRAY_TYPE)
1980 return exp;
1982 /* Constants can be used directly unless they're not loadable. */
1983 if (TREE_CODE (exp) == CONST_DECL)
1984 exp = DECL_INITIAL (exp);
1986 /* Strip no-op conversions. */
1987 orig_exp = exp;
1988 STRIP_TYPE_NOPS (exp);
1990 if (TREE_NO_WARNING (orig_exp))
1991 TREE_NO_WARNING (exp) = 1;
1993 if (code == VOID_TYPE)
1995 error ("void value not ignored as it ought to be");
1996 return error_mark_node;
1999 exp = require_complete_type (exp);
2000 if (exp == error_mark_node)
2001 return error_mark_node;
2003 promoted_type = targetm.promoted_type (type);
2004 if (promoted_type)
2005 return convert (promoted_type, exp);
2007 if (INTEGRAL_TYPE_P (type))
2008 return perform_integral_promotions (exp);
2010 return exp;
2013 /* Look up COMPONENT in a structure or union TYPE.
2015 If the component name is not found, returns NULL_TREE. Otherwise,
2016 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2017 stepping down the chain to the component, which is in the last
2018 TREE_VALUE of the list. Normally the list is of length one, but if
2019 the component is embedded within (nested) anonymous structures or
2020 unions, the list steps down the chain to the component. */
2022 static tree
2023 lookup_field (tree type, tree component)
2025 tree field;
2027 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2028 to the field elements. Use a binary search on this array to quickly
2029 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2030 will always be set for structures which have many elements. */
2032 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2034 int bot, top, half;
2035 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2037 field = TYPE_FIELDS (type);
2038 bot = 0;
2039 top = TYPE_LANG_SPECIFIC (type)->s->len;
2040 while (top - bot > 1)
2042 half = (top - bot + 1) >> 1;
2043 field = field_array[bot+half];
2045 if (DECL_NAME (field) == NULL_TREE)
2047 /* Step through all anon unions in linear fashion. */
2048 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2050 field = field_array[bot++];
2051 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2052 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2054 tree anon = lookup_field (TREE_TYPE (field), component);
2056 if (anon)
2057 return tree_cons (NULL_TREE, field, anon);
2059 /* The Plan 9 compiler permits referring
2060 directly to an anonymous struct/union field
2061 using a typedef name. */
2062 if (flag_plan9_extensions
2063 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2064 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2065 == TYPE_DECL)
2066 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2067 == component))
2068 break;
2072 /* Entire record is only anon unions. */
2073 if (bot > top)
2074 return NULL_TREE;
2076 /* Restart the binary search, with new lower bound. */
2077 continue;
2080 if (DECL_NAME (field) == component)
2081 break;
2082 if (DECL_NAME (field) < component)
2083 bot += half;
2084 else
2085 top = bot + half;
2088 if (DECL_NAME (field_array[bot]) == component)
2089 field = field_array[bot];
2090 else if (DECL_NAME (field) != component)
2091 return NULL_TREE;
2093 else
2095 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2097 if (DECL_NAME (field) == NULL_TREE
2098 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2099 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2101 tree anon = lookup_field (TREE_TYPE (field), component);
2103 if (anon)
2104 return tree_cons (NULL_TREE, field, anon);
2106 /* The Plan 9 compiler permits referring directly to an
2107 anonymous struct/union field using a typedef
2108 name. */
2109 if (flag_plan9_extensions
2110 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2111 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2112 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2113 == component))
2114 break;
2117 if (DECL_NAME (field) == component)
2118 break;
2121 if (field == NULL_TREE)
2122 return NULL_TREE;
2125 return tree_cons (NULL_TREE, field, NULL_TREE);
2128 /* Make an expression to refer to the COMPONENT field of structure or
2129 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2130 location of the COMPONENT_REF. */
2132 tree
2133 build_component_ref (location_t loc, tree datum, tree component)
2135 tree type = TREE_TYPE (datum);
2136 enum tree_code code = TREE_CODE (type);
2137 tree field = NULL;
2138 tree ref;
2139 bool datum_lvalue = lvalue_p (datum);
2141 if (!objc_is_public (datum, component))
2142 return error_mark_node;
2144 /* Detect Objective-C property syntax object.property. */
2145 if (c_dialect_objc ()
2146 && (ref = objc_maybe_build_component_ref (datum, component)))
2147 return ref;
2149 /* See if there is a field or component with name COMPONENT. */
2151 if (code == RECORD_TYPE || code == UNION_TYPE)
2153 if (!COMPLETE_TYPE_P (type))
2155 c_incomplete_type_error (NULL_TREE, type);
2156 return error_mark_node;
2159 field = lookup_field (type, component);
2161 if (!field)
2163 error_at (loc, "%qT has no member named %qE", type, component);
2164 return error_mark_node;
2167 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2168 This might be better solved in future the way the C++ front
2169 end does it - by giving the anonymous entities each a
2170 separate name and type, and then have build_component_ref
2171 recursively call itself. We can't do that here. */
2174 tree subdatum = TREE_VALUE (field);
2175 int quals;
2176 tree subtype;
2177 bool use_datum_quals;
2179 if (TREE_TYPE (subdatum) == error_mark_node)
2180 return error_mark_node;
2182 /* If this is an rvalue, it does not have qualifiers in C
2183 standard terms and we must avoid propagating such
2184 qualifiers down to a non-lvalue array that is then
2185 converted to a pointer. */
2186 use_datum_quals = (datum_lvalue
2187 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2189 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2190 if (use_datum_quals)
2191 quals |= TYPE_QUALS (TREE_TYPE (datum));
2192 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2194 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2195 NULL_TREE);
2196 SET_EXPR_LOCATION (ref, loc);
2197 if (TREE_READONLY (subdatum)
2198 || (use_datum_quals && TREE_READONLY (datum)))
2199 TREE_READONLY (ref) = 1;
2200 if (TREE_THIS_VOLATILE (subdatum)
2201 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2202 TREE_THIS_VOLATILE (ref) = 1;
2204 if (TREE_DEPRECATED (subdatum))
2205 warn_deprecated_use (subdatum, NULL_TREE);
2207 datum = ref;
2209 field = TREE_CHAIN (field);
2211 while (field);
2213 return ref;
2215 else if (code != ERROR_MARK)
2216 error_at (loc,
2217 "request for member %qE in something not a structure or union",
2218 component);
2220 return error_mark_node;
2223 /* Given an expression PTR for a pointer, return an expression
2224 for the value pointed to.
2225 ERRORSTRING is the name of the operator to appear in error messages.
2227 LOC is the location to use for the generated tree. */
2229 tree
2230 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2232 tree pointer = default_conversion (ptr);
2233 tree type = TREE_TYPE (pointer);
2234 tree ref;
2236 if (TREE_CODE (type) == POINTER_TYPE)
2238 if (CONVERT_EXPR_P (pointer)
2239 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2241 /* If a warning is issued, mark it to avoid duplicates from
2242 the backend. This only needs to be done at
2243 warn_strict_aliasing > 2. */
2244 if (warn_strict_aliasing > 2)
2245 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2246 type, TREE_OPERAND (pointer, 0)))
2247 TREE_NO_WARNING (pointer) = 1;
2250 if (TREE_CODE (pointer) == ADDR_EXPR
2251 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2252 == TREE_TYPE (type)))
2254 ref = TREE_OPERAND (pointer, 0);
2255 protected_set_expr_location (ref, loc);
2256 return ref;
2258 else
2260 tree t = TREE_TYPE (type);
2262 ref = build1 (INDIRECT_REF, t, pointer);
2264 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2266 error_at (loc, "dereferencing pointer to incomplete type");
2267 return error_mark_node;
2269 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2270 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2272 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2273 so that we get the proper error message if the result is used
2274 to assign to. Also, &* is supposed to be a no-op.
2275 And ANSI C seems to specify that the type of the result
2276 should be the const type. */
2277 /* A de-reference of a pointer to const is not a const. It is valid
2278 to change it via some other pointer. */
2279 TREE_READONLY (ref) = TYPE_READONLY (t);
2280 TREE_SIDE_EFFECTS (ref)
2281 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2282 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2283 protected_set_expr_location (ref, loc);
2284 return ref;
2287 else if (TREE_CODE (pointer) != ERROR_MARK)
2288 invalid_indirection_error (loc, type, errstring);
2290 return error_mark_node;
2293 /* This handles expressions of the form "a[i]", which denotes
2294 an array reference.
2296 This is logically equivalent in C to *(a+i), but we may do it differently.
2297 If A is a variable or a member, we generate a primitive ARRAY_REF.
2298 This avoids forcing the array out of registers, and can work on
2299 arrays that are not lvalues (for example, members of structures returned
2300 by functions).
2302 For vector types, allow vector[i] but not i[vector], and create
2303 *(((type*)&vectortype) + i) for the expression.
2305 LOC is the location to use for the returned expression. */
2307 tree
2308 build_array_ref (location_t loc, tree array, tree index)
2310 tree ret;
2311 bool swapped = false;
2312 if (TREE_TYPE (array) == error_mark_node
2313 || TREE_TYPE (index) == error_mark_node)
2314 return error_mark_node;
2316 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2317 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2318 /* Allow vector[index] but not index[vector]. */
2319 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2321 tree temp;
2322 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2323 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2325 error_at (loc,
2326 "subscripted value is neither array nor pointer nor vector");
2328 return error_mark_node;
2330 temp = array;
2331 array = index;
2332 index = temp;
2333 swapped = true;
2336 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2338 error_at (loc, "array subscript is not an integer");
2339 return error_mark_node;
2342 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2344 error_at (loc, "subscripted value is pointer to function");
2345 return error_mark_node;
2348 /* ??? Existing practice has been to warn only when the char
2349 index is syntactically the index, not for char[array]. */
2350 if (!swapped)
2351 warn_array_subscript_with_type_char (index);
2353 /* Apply default promotions *after* noticing character types. */
2354 index = default_conversion (index);
2356 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2358 convert_vector_to_pointer_for_subscript (loc, &array, index);
2360 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2362 tree rval, type;
2364 /* An array that is indexed by a non-constant
2365 cannot be stored in a register; we must be able to do
2366 address arithmetic on its address.
2367 Likewise an array of elements of variable size. */
2368 if (TREE_CODE (index) != INTEGER_CST
2369 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2370 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2372 if (!c_mark_addressable (array))
2373 return error_mark_node;
2375 /* An array that is indexed by a constant value which is not within
2376 the array bounds cannot be stored in a register either; because we
2377 would get a crash in store_bit_field/extract_bit_field when trying
2378 to access a non-existent part of the register. */
2379 if (TREE_CODE (index) == INTEGER_CST
2380 && TYPE_DOMAIN (TREE_TYPE (array))
2381 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2383 if (!c_mark_addressable (array))
2384 return error_mark_node;
2387 if (pedantic)
2389 tree foo = array;
2390 while (TREE_CODE (foo) == COMPONENT_REF)
2391 foo = TREE_OPERAND (foo, 0);
2392 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2393 pedwarn (loc, OPT_Wpedantic,
2394 "ISO C forbids subscripting %<register%> array");
2395 else if (!flag_isoc99 && !lvalue_p (foo))
2396 pedwarn (loc, OPT_Wpedantic,
2397 "ISO C90 forbids subscripting non-lvalue array");
2400 type = TREE_TYPE (TREE_TYPE (array));
2401 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2402 /* Array ref is const/volatile if the array elements are
2403 or if the array is. */
2404 TREE_READONLY (rval)
2405 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2406 | TREE_READONLY (array));
2407 TREE_SIDE_EFFECTS (rval)
2408 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2409 | TREE_SIDE_EFFECTS (array));
2410 TREE_THIS_VOLATILE (rval)
2411 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2412 /* This was added by rms on 16 Nov 91.
2413 It fixes vol struct foo *a; a->elts[1]
2414 in an inline function.
2415 Hope it doesn't break something else. */
2416 | TREE_THIS_VOLATILE (array));
2417 ret = require_complete_type (rval);
2418 protected_set_expr_location (ret, loc);
2419 return ret;
2421 else
2423 tree ar = default_conversion (array);
2425 if (ar == error_mark_node)
2426 return ar;
2428 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2429 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2431 return build_indirect_ref
2432 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2433 RO_ARRAY_INDEXING);
2437 /* Build an external reference to identifier ID. FUN indicates
2438 whether this will be used for a function call. LOC is the source
2439 location of the identifier. This sets *TYPE to the type of the
2440 identifier, which is not the same as the type of the returned value
2441 for CONST_DECLs defined as enum constants. If the type of the
2442 identifier is not available, *TYPE is set to NULL. */
2443 tree
2444 build_external_ref (location_t loc, tree id, int fun, tree *type)
2446 tree ref;
2447 tree decl = lookup_name (id);
2449 /* In Objective-C, an instance variable (ivar) may be preferred to
2450 whatever lookup_name() found. */
2451 decl = objc_lookup_ivar (decl, id);
2453 *type = NULL;
2454 if (decl && decl != error_mark_node)
2456 ref = decl;
2457 *type = TREE_TYPE (ref);
2459 else if (fun)
2460 /* Implicit function declaration. */
2461 ref = implicitly_declare (loc, id);
2462 else if (decl == error_mark_node)
2463 /* Don't complain about something that's already been
2464 complained about. */
2465 return error_mark_node;
2466 else
2468 undeclared_variable (loc, id);
2469 return error_mark_node;
2472 if (TREE_TYPE (ref) == error_mark_node)
2473 return error_mark_node;
2475 if (TREE_DEPRECATED (ref))
2476 warn_deprecated_use (ref, NULL_TREE);
2478 /* Recursive call does not count as usage. */
2479 if (ref != current_function_decl)
2481 TREE_USED (ref) = 1;
2484 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2486 if (!in_sizeof && !in_typeof)
2487 C_DECL_USED (ref) = 1;
2488 else if (DECL_INITIAL (ref) == 0
2489 && DECL_EXTERNAL (ref)
2490 && !TREE_PUBLIC (ref))
2491 record_maybe_used_decl (ref);
2494 if (TREE_CODE (ref) == CONST_DECL)
2496 used_types_insert (TREE_TYPE (ref));
2498 if (warn_cxx_compat
2499 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2500 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2502 warning_at (loc, OPT_Wc___compat,
2503 ("enum constant defined in struct or union "
2504 "is not visible in C++"));
2505 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2508 ref = DECL_INITIAL (ref);
2509 TREE_CONSTANT (ref) = 1;
2511 else if (current_function_decl != 0
2512 && !DECL_FILE_SCOPE_P (current_function_decl)
2513 && (TREE_CODE (ref) == VAR_DECL
2514 || TREE_CODE (ref) == PARM_DECL
2515 || TREE_CODE (ref) == FUNCTION_DECL))
2517 tree context = decl_function_context (ref);
2519 if (context != 0 && context != current_function_decl)
2520 DECL_NONLOCAL (ref) = 1;
2522 /* C99 6.7.4p3: An inline definition of a function with external
2523 linkage ... shall not contain a reference to an identifier with
2524 internal linkage. */
2525 else if (current_function_decl != 0
2526 && DECL_DECLARED_INLINE_P (current_function_decl)
2527 && DECL_EXTERNAL (current_function_decl)
2528 && VAR_OR_FUNCTION_DECL_P (ref)
2529 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2530 && ! TREE_PUBLIC (ref)
2531 && DECL_CONTEXT (ref) != current_function_decl)
2532 record_inline_static (loc, current_function_decl, ref,
2533 csi_internal);
2535 return ref;
2538 /* Record details of decls possibly used inside sizeof or typeof. */
2539 struct maybe_used_decl
2541 /* The decl. */
2542 tree decl;
2543 /* The level seen at (in_sizeof + in_typeof). */
2544 int level;
2545 /* The next one at this level or above, or NULL. */
2546 struct maybe_used_decl *next;
2549 static struct maybe_used_decl *maybe_used_decls;
2551 /* Record that DECL, an undefined static function reference seen
2552 inside sizeof or typeof, might be used if the operand of sizeof is
2553 a VLA type or the operand of typeof is a variably modified
2554 type. */
2556 static void
2557 record_maybe_used_decl (tree decl)
2559 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2560 t->decl = decl;
2561 t->level = in_sizeof + in_typeof;
2562 t->next = maybe_used_decls;
2563 maybe_used_decls = t;
2566 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2567 USED is false, just discard them. If it is true, mark them used
2568 (if no longer inside sizeof or typeof) or move them to the next
2569 level up (if still inside sizeof or typeof). */
2571 void
2572 pop_maybe_used (bool used)
2574 struct maybe_used_decl *p = maybe_used_decls;
2575 int cur_level = in_sizeof + in_typeof;
2576 while (p && p->level > cur_level)
2578 if (used)
2580 if (cur_level == 0)
2581 C_DECL_USED (p->decl) = 1;
2582 else
2583 p->level = cur_level;
2585 p = p->next;
2587 if (!used || cur_level == 0)
2588 maybe_used_decls = p;
2591 /* Return the result of sizeof applied to EXPR. */
2593 struct c_expr
2594 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2596 struct c_expr ret;
2597 if (expr.value == error_mark_node)
2599 ret.value = error_mark_node;
2600 ret.original_code = ERROR_MARK;
2601 ret.original_type = NULL;
2602 pop_maybe_used (false);
2604 else
2606 bool expr_const_operands = true;
2607 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2608 &expr_const_operands);
2609 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2610 c_last_sizeof_arg = expr.value;
2611 ret.original_code = SIZEOF_EXPR;
2612 ret.original_type = NULL;
2613 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2615 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2616 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2617 folded_expr, ret.value);
2618 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2619 SET_EXPR_LOCATION (ret.value, loc);
2621 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2623 return ret;
2626 /* Return the result of sizeof applied to T, a structure for the type
2627 name passed to sizeof (rather than the type itself). LOC is the
2628 location of the original expression. */
2630 struct c_expr
2631 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2633 tree type;
2634 struct c_expr ret;
2635 tree type_expr = NULL_TREE;
2636 bool type_expr_const = true;
2637 type = groktypename (t, &type_expr, &type_expr_const);
2638 ret.value = c_sizeof (loc, type);
2639 c_last_sizeof_arg = type;
2640 ret.original_code = SIZEOF_EXPR;
2641 ret.original_type = NULL;
2642 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2643 && c_vla_type_p (type))
2645 /* If the type is a [*] array, it is a VLA but is represented as
2646 having a size of zero. In such a case we must ensure that
2647 the result of sizeof does not get folded to a constant by
2648 c_fully_fold, because if the size is evaluated the result is
2649 not constant and so constraints on zero or negative size
2650 arrays must not be applied when this sizeof call is inside
2651 another array declarator. */
2652 if (!type_expr)
2653 type_expr = integer_zero_node;
2654 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2655 type_expr, ret.value);
2656 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2658 pop_maybe_used (type != error_mark_node
2659 ? C_TYPE_VARIABLE_SIZE (type) : false);
2660 return ret;
2663 /* Build a function call to function FUNCTION with parameters PARAMS.
2664 The function call is at LOC.
2665 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2666 TREE_VALUE of each node is a parameter-expression.
2667 FUNCTION's data type may be a function type or a pointer-to-function. */
2669 tree
2670 build_function_call (location_t loc, tree function, tree params)
2672 VEC(tree,gc) *vec;
2673 tree ret;
2675 vec = VEC_alloc (tree, gc, list_length (params));
2676 for (; params; params = TREE_CHAIN (params))
2677 VEC_quick_push (tree, vec, TREE_VALUE (params));
2678 ret = build_function_call_vec (loc, function, vec, NULL);
2679 VEC_free (tree, gc, vec);
2680 return ret;
2683 /* Give a note about the location of the declaration of DECL. */
2685 static void inform_declaration (tree decl)
2687 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2688 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2691 /* Build a function call to function FUNCTION with parameters PARAMS.
2692 ORIGTYPES, if not NULL, is a vector of types; each element is
2693 either NULL or the original type of the corresponding element in
2694 PARAMS. The original type may differ from TREE_TYPE of the
2695 parameter for enums. FUNCTION's data type may be a function type
2696 or pointer-to-function. This function changes the elements of
2697 PARAMS. */
2699 tree
2700 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2701 VEC(tree,gc) *origtypes)
2703 tree fntype, fundecl = 0;
2704 tree name = NULL_TREE, result;
2705 tree tem;
2706 int nargs;
2707 tree *argarray;
2710 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2711 STRIP_TYPE_NOPS (function);
2713 /* Convert anything with function type to a pointer-to-function. */
2714 if (TREE_CODE (function) == FUNCTION_DECL)
2716 /* Implement type-directed function overloading for builtins.
2717 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2718 handle all the type checking. The result is a complete expression
2719 that implements this function call. */
2720 tem = resolve_overloaded_builtin (loc, function, params);
2721 if (tem)
2722 return tem;
2724 name = DECL_NAME (function);
2726 if (flag_tm)
2727 tm_malloc_replacement (function);
2728 fundecl = function;
2729 /* Atomic functions have type checking/casting already done. They are
2730 often rewritten and don't match the original parameter list. */
2731 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2732 origtypes = NULL;
2734 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2735 function = function_to_pointer_conversion (loc, function);
2737 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2738 expressions, like those used for ObjC messenger dispatches. */
2739 if (!VEC_empty (tree, params))
2740 function = objc_rewrite_function_call (function,
2741 VEC_index (tree, params, 0));
2743 function = c_fully_fold (function, false, NULL);
2745 fntype = TREE_TYPE (function);
2747 if (TREE_CODE (fntype) == ERROR_MARK)
2748 return error_mark_node;
2750 if (!(TREE_CODE (fntype) == POINTER_TYPE
2751 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2753 if (!flag_diagnostics_show_caret)
2754 error_at (loc,
2755 "called object %qE is not a function or function pointer",
2756 function);
2757 else if (DECL_P (function))
2759 error_at (loc,
2760 "called object %qD is not a function or function pointer",
2761 function);
2762 inform_declaration (function);
2764 else
2765 error_at (loc,
2766 "called object is not a function or function pointer");
2767 return error_mark_node;
2770 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2771 current_function_returns_abnormally = 1;
2773 /* fntype now gets the type of function pointed to. */
2774 fntype = TREE_TYPE (fntype);
2776 /* Convert the parameters to the types declared in the
2777 function prototype, or apply default promotions. */
2779 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2780 function, fundecl);
2781 if (nargs < 0)
2782 return error_mark_node;
2784 /* Check that the function is called through a compatible prototype.
2785 If it is not, replace the call by a trap, wrapped up in a compound
2786 expression if necessary. This has the nice side-effect to prevent
2787 the tree-inliner from generating invalid assignment trees which may
2788 blow up in the RTL expander later. */
2789 if (CONVERT_EXPR_P (function)
2790 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2791 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2792 && !comptypes (fntype, TREE_TYPE (tem)))
2794 tree return_type = TREE_TYPE (fntype);
2795 tree trap = build_function_call (loc,
2796 builtin_decl_explicit (BUILT_IN_TRAP),
2797 NULL_TREE);
2798 int i;
2800 /* This situation leads to run-time undefined behavior. We can't,
2801 therefore, simply error unless we can prove that all possible
2802 executions of the program must execute the code. */
2803 if (warning_at (loc, 0, "function called through a non-compatible type"))
2804 /* We can, however, treat "undefined" any way we please.
2805 Call abort to encourage the user to fix the program. */
2806 inform (loc, "if this code is reached, the program will abort");
2807 /* Before the abort, allow the function arguments to exit or
2808 call longjmp. */
2809 for (i = 0; i < nargs; i++)
2810 trap = build2 (COMPOUND_EXPR, void_type_node,
2811 VEC_index (tree, params, i), trap);
2813 if (VOID_TYPE_P (return_type))
2815 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2816 pedwarn (loc, 0,
2817 "function with qualified void return type called");
2818 return trap;
2820 else
2822 tree rhs;
2824 if (AGGREGATE_TYPE_P (return_type))
2825 rhs = build_compound_literal (loc, return_type,
2826 build_constructor (return_type, 0),
2827 false);
2828 else
2829 rhs = build_zero_cst (return_type);
2831 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2832 trap, rhs));
2836 argarray = VEC_address (tree, params);
2838 /* Check that arguments to builtin functions match the expectations. */
2839 if (fundecl
2840 && DECL_BUILT_IN (fundecl)
2841 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2842 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2843 return error_mark_node;
2845 /* Check that the arguments to the function are valid. */
2846 check_function_arguments (fntype, nargs, argarray);
2848 if (name != NULL_TREE
2849 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2851 if (require_constant_value)
2852 result =
2853 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2854 function, nargs, argarray);
2855 else
2856 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2857 function, nargs, argarray);
2858 if (TREE_CODE (result) == NOP_EXPR
2859 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2860 STRIP_TYPE_NOPS (result);
2862 else
2863 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2864 function, nargs, argarray);
2866 if (VOID_TYPE_P (TREE_TYPE (result)))
2868 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2869 pedwarn (loc, 0,
2870 "function with qualified void return type called");
2871 return result;
2873 return require_complete_type (result);
2876 /* Convert the argument expressions in the vector VALUES
2877 to the types in the list TYPELIST.
2879 If TYPELIST is exhausted, or when an element has NULL as its type,
2880 perform the default conversions.
2882 ORIGTYPES is the original types of the expressions in VALUES. This
2883 holds the type of enum values which have been converted to integral
2884 types. It may be NULL.
2886 FUNCTION is a tree for the called function. It is used only for
2887 error messages, where it is formatted with %qE.
2889 This is also where warnings about wrong number of args are generated.
2891 Returns the actual number of arguments processed (which may be less
2892 than the length of VALUES in some error situations), or -1 on
2893 failure. */
2895 static int
2896 convert_arguments (tree typelist, VEC(tree,gc) *values,
2897 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2899 tree typetail, val;
2900 unsigned int parmnum;
2901 bool error_args = false;
2902 const bool type_generic = fundecl
2903 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2904 bool type_generic_remove_excess_precision = false;
2905 tree selector;
2907 /* Change pointer to function to the function itself for
2908 diagnostics. */
2909 if (TREE_CODE (function) == ADDR_EXPR
2910 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2911 function = TREE_OPERAND (function, 0);
2913 /* Handle an ObjC selector specially for diagnostics. */
2914 selector = objc_message_selector ();
2916 /* For type-generic built-in functions, determine whether excess
2917 precision should be removed (classification) or not
2918 (comparison). */
2919 if (type_generic
2920 && DECL_BUILT_IN (fundecl)
2921 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2923 switch (DECL_FUNCTION_CODE (fundecl))
2925 case BUILT_IN_ISFINITE:
2926 case BUILT_IN_ISINF:
2927 case BUILT_IN_ISINF_SIGN:
2928 case BUILT_IN_ISNAN:
2929 case BUILT_IN_ISNORMAL:
2930 case BUILT_IN_FPCLASSIFY:
2931 type_generic_remove_excess_precision = true;
2932 break;
2934 default:
2935 type_generic_remove_excess_precision = false;
2936 break;
2940 /* Scan the given expressions and types, producing individual
2941 converted arguments. */
2943 for (typetail = typelist, parmnum = 0;
2944 VEC_iterate (tree, values, parmnum, val);
2945 ++parmnum)
2947 tree type = typetail ? TREE_VALUE (typetail) : 0;
2948 tree valtype = TREE_TYPE (val);
2949 tree rname = function;
2950 int argnum = parmnum + 1;
2951 const char *invalid_func_diag;
2952 bool excess_precision = false;
2953 bool npc;
2954 tree parmval;
2956 if (type == void_type_node)
2958 if (selector)
2959 error_at (input_location,
2960 "too many arguments to method %qE", selector);
2961 else
2962 error_at (input_location,
2963 "too many arguments to function %qE", function);
2964 inform_declaration (fundecl);
2965 return parmnum;
2968 if (selector && argnum > 2)
2970 rname = selector;
2971 argnum -= 2;
2974 npc = null_pointer_constant_p (val);
2976 /* If there is excess precision and a prototype, convert once to
2977 the required type rather than converting via the semantic
2978 type. Likewise without a prototype a float value represented
2979 as long double should be converted once to double. But for
2980 type-generic classification functions excess precision must
2981 be removed here. */
2982 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2983 && (type || !type_generic || !type_generic_remove_excess_precision))
2985 val = TREE_OPERAND (val, 0);
2986 excess_precision = true;
2988 val = c_fully_fold (val, false, NULL);
2989 STRIP_TYPE_NOPS (val);
2991 val = require_complete_type (val);
2993 if (type != 0)
2995 /* Formal parm type is specified by a function prototype. */
2997 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2999 error ("type of formal parameter %d is incomplete", parmnum + 1);
3000 parmval = val;
3002 else
3004 tree origtype;
3006 /* Optionally warn about conversions that
3007 differ from the default conversions. */
3008 if (warn_traditional_conversion || warn_traditional)
3010 unsigned int formal_prec = TYPE_PRECISION (type);
3012 if (INTEGRAL_TYPE_P (type)
3013 && TREE_CODE (valtype) == REAL_TYPE)
3014 warning (0, "passing argument %d of %qE as integer "
3015 "rather than floating due to prototype",
3016 argnum, rname);
3017 if (INTEGRAL_TYPE_P (type)
3018 && TREE_CODE (valtype) == COMPLEX_TYPE)
3019 warning (0, "passing argument %d of %qE as integer "
3020 "rather than complex due to prototype",
3021 argnum, rname);
3022 else if (TREE_CODE (type) == COMPLEX_TYPE
3023 && TREE_CODE (valtype) == REAL_TYPE)
3024 warning (0, "passing argument %d of %qE as complex "
3025 "rather than floating due to prototype",
3026 argnum, rname);
3027 else if (TREE_CODE (type) == REAL_TYPE
3028 && INTEGRAL_TYPE_P (valtype))
3029 warning (0, "passing argument %d of %qE as floating "
3030 "rather than integer due to prototype",
3031 argnum, rname);
3032 else if (TREE_CODE (type) == COMPLEX_TYPE
3033 && INTEGRAL_TYPE_P (valtype))
3034 warning (0, "passing argument %d of %qE as complex "
3035 "rather than integer due to prototype",
3036 argnum, rname);
3037 else if (TREE_CODE (type) == REAL_TYPE
3038 && TREE_CODE (valtype) == COMPLEX_TYPE)
3039 warning (0, "passing argument %d of %qE as floating "
3040 "rather than complex due to prototype",
3041 argnum, rname);
3042 /* ??? At some point, messages should be written about
3043 conversions between complex types, but that's too messy
3044 to do now. */
3045 else if (TREE_CODE (type) == REAL_TYPE
3046 && TREE_CODE (valtype) == REAL_TYPE)
3048 /* Warn if any argument is passed as `float',
3049 since without a prototype it would be `double'. */
3050 if (formal_prec == TYPE_PRECISION (float_type_node)
3051 && type != dfloat32_type_node)
3052 warning (0, "passing argument %d of %qE as %<float%> "
3053 "rather than %<double%> due to prototype",
3054 argnum, rname);
3056 /* Warn if mismatch between argument and prototype
3057 for decimal float types. Warn of conversions with
3058 binary float types and of precision narrowing due to
3059 prototype. */
3060 else if (type != valtype
3061 && (type == dfloat32_type_node
3062 || type == dfloat64_type_node
3063 || type == dfloat128_type_node
3064 || valtype == dfloat32_type_node
3065 || valtype == dfloat64_type_node
3066 || valtype == dfloat128_type_node)
3067 && (formal_prec
3068 <= TYPE_PRECISION (valtype)
3069 || (type == dfloat128_type_node
3070 && (valtype
3071 != dfloat64_type_node
3072 && (valtype
3073 != dfloat32_type_node)))
3074 || (type == dfloat64_type_node
3075 && (valtype
3076 != dfloat32_type_node))))
3077 warning (0, "passing argument %d of %qE as %qT "
3078 "rather than %qT due to prototype",
3079 argnum, rname, type, valtype);
3082 /* Detect integer changing in width or signedness.
3083 These warnings are only activated with
3084 -Wtraditional-conversion, not with -Wtraditional. */
3085 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3086 && INTEGRAL_TYPE_P (valtype))
3088 tree would_have_been = default_conversion (val);
3089 tree type1 = TREE_TYPE (would_have_been);
3091 if (TREE_CODE (type) == ENUMERAL_TYPE
3092 && (TYPE_MAIN_VARIANT (type)
3093 == TYPE_MAIN_VARIANT (valtype)))
3094 /* No warning if function asks for enum
3095 and the actual arg is that enum type. */
3097 else if (formal_prec != TYPE_PRECISION (type1))
3098 warning (OPT_Wtraditional_conversion,
3099 "passing argument %d of %qE "
3100 "with different width due to prototype",
3101 argnum, rname);
3102 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3104 /* Don't complain if the formal parameter type
3105 is an enum, because we can't tell now whether
3106 the value was an enum--even the same enum. */
3107 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3109 else if (TREE_CODE (val) == INTEGER_CST
3110 && int_fits_type_p (val, type))
3111 /* Change in signedness doesn't matter
3112 if a constant value is unaffected. */
3114 /* If the value is extended from a narrower
3115 unsigned type, it doesn't matter whether we
3116 pass it as signed or unsigned; the value
3117 certainly is the same either way. */
3118 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3119 && TYPE_UNSIGNED (valtype))
3121 else if (TYPE_UNSIGNED (type))
3122 warning (OPT_Wtraditional_conversion,
3123 "passing argument %d of %qE "
3124 "as unsigned due to prototype",
3125 argnum, rname);
3126 else
3127 warning (OPT_Wtraditional_conversion,
3128 "passing argument %d of %qE "
3129 "as signed due to prototype", argnum, rname);
3133 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3134 sake of better warnings from convert_and_check. */
3135 if (excess_precision)
3136 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3137 origtype = (origtypes == NULL
3138 ? NULL_TREE
3139 : VEC_index (tree, origtypes, parmnum));
3140 parmval = convert_for_assignment (input_location, type, val,
3141 origtype, ic_argpass, npc,
3142 fundecl, function,
3143 parmnum + 1);
3145 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3146 && INTEGRAL_TYPE_P (type)
3147 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3148 parmval = default_conversion (parmval);
3151 else if (TREE_CODE (valtype) == REAL_TYPE
3152 && (TYPE_PRECISION (valtype)
3153 < TYPE_PRECISION (double_type_node))
3154 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3156 if (type_generic)
3157 parmval = val;
3158 else
3160 /* Convert `float' to `double'. */
3161 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3162 warning (OPT_Wdouble_promotion,
3163 "implicit conversion from %qT to %qT when passing "
3164 "argument to function",
3165 valtype, double_type_node);
3166 parmval = convert (double_type_node, val);
3169 else if (excess_precision && !type_generic)
3170 /* A "double" argument with excess precision being passed
3171 without a prototype or in variable arguments. */
3172 parmval = convert (valtype, val);
3173 else if ((invalid_func_diag =
3174 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3176 error (invalid_func_diag);
3177 return -1;
3179 else
3180 /* Convert `short' and `char' to full-size `int'. */
3181 parmval = default_conversion (val);
3183 VEC_replace (tree, values, parmnum, parmval);
3184 if (parmval == error_mark_node)
3185 error_args = true;
3187 if (typetail)
3188 typetail = TREE_CHAIN (typetail);
3191 gcc_assert (parmnum == VEC_length (tree, values));
3193 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3195 error_at (input_location,
3196 "too few arguments to function %qE", function);
3197 inform_declaration (fundecl);
3198 return -1;
3201 return error_args ? -1 : (int) parmnum;
3204 /* This is the entry point used by the parser to build unary operators
3205 in the input. CODE, a tree_code, specifies the unary operator, and
3206 ARG is the operand. For unary plus, the C parser currently uses
3207 CONVERT_EXPR for code.
3209 LOC is the location to use for the tree generated.
3212 struct c_expr
3213 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3215 struct c_expr result;
3217 result.value = build_unary_op (loc, code, arg.value, 0);
3218 result.original_code = code;
3219 result.original_type = NULL;
3221 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3222 overflow_warning (loc, result.value);
3224 return result;
3227 /* This is the entry point used by the parser to build binary operators
3228 in the input. CODE, a tree_code, specifies the binary operator, and
3229 ARG1 and ARG2 are the operands. In addition to constructing the
3230 expression, we check for operands that were written with other binary
3231 operators in a way that is likely to confuse the user.
3233 LOCATION is the location of the binary operator. */
3235 struct c_expr
3236 parser_build_binary_op (location_t location, enum tree_code code,
3237 struct c_expr arg1, struct c_expr arg2)
3239 struct c_expr result;
3241 enum tree_code code1 = arg1.original_code;
3242 enum tree_code code2 = arg2.original_code;
3243 tree type1 = (arg1.original_type
3244 ? arg1.original_type
3245 : TREE_TYPE (arg1.value));
3246 tree type2 = (arg2.original_type
3247 ? arg2.original_type
3248 : TREE_TYPE (arg2.value));
3250 result.value = build_binary_op (location, code,
3251 arg1.value, arg2.value, 1);
3252 result.original_code = code;
3253 result.original_type = NULL;
3255 if (TREE_CODE (result.value) == ERROR_MARK)
3256 return result;
3258 if (location != UNKNOWN_LOCATION)
3259 protected_set_expr_location (result.value, location);
3261 /* Check for cases such as x+y<<z which users are likely
3262 to misinterpret. */
3263 if (warn_parentheses)
3264 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3266 if (warn_logical_op)
3267 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3268 code1, arg1.value, code2, arg2.value);
3270 /* Warn about comparisons against string literals, with the exception
3271 of testing for equality or inequality of a string literal with NULL. */
3272 if (code == EQ_EXPR || code == NE_EXPR)
3274 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3275 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3276 warning_at (location, OPT_Waddress,
3277 "comparison with string literal results in unspecified behavior");
3279 else if (TREE_CODE_CLASS (code) == tcc_comparison
3280 && (code1 == STRING_CST || code2 == STRING_CST))
3281 warning_at (location, OPT_Waddress,
3282 "comparison with string literal results in unspecified behavior");
3284 if (TREE_OVERFLOW_P (result.value)
3285 && !TREE_OVERFLOW_P (arg1.value)
3286 && !TREE_OVERFLOW_P (arg2.value))
3287 overflow_warning (location, result.value);
3289 /* Warn about comparisons of different enum types. */
3290 if (warn_enum_compare
3291 && TREE_CODE_CLASS (code) == tcc_comparison
3292 && TREE_CODE (type1) == ENUMERAL_TYPE
3293 && TREE_CODE (type2) == ENUMERAL_TYPE
3294 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3295 warning_at (location, OPT_Wenum_compare,
3296 "comparison between %qT and %qT",
3297 type1, type2);
3299 return result;
3302 /* Return a tree for the difference of pointers OP0 and OP1.
3303 The resulting tree has type int. */
3305 static tree
3306 pointer_diff (location_t loc, tree op0, tree op1)
3308 tree restype = ptrdiff_type_node;
3309 tree result, inttype;
3311 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3312 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3313 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3314 tree con0, con1, lit0, lit1;
3315 tree orig_op1 = op1;
3317 /* If the operands point into different address spaces, we need to
3318 explicitly convert them to pointers into the common address space
3319 before we can subtract the numerical address values. */
3320 if (as0 != as1)
3322 addr_space_t as_common;
3323 tree common_type;
3325 /* Determine the common superset address space. This is guaranteed
3326 to exist because the caller verified that comp_target_types
3327 returned non-zero. */
3328 if (!addr_space_superset (as0, as1, &as_common))
3329 gcc_unreachable ();
3331 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3332 op0 = convert (common_type, op0);
3333 op1 = convert (common_type, op1);
3336 /* Determine integer type to perform computations in. This will usually
3337 be the same as the result type (ptrdiff_t), but may need to be a wider
3338 type if pointers for the address space are wider than ptrdiff_t. */
3339 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3340 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3341 else
3342 inttype = restype;
3345 if (TREE_CODE (target_type) == VOID_TYPE)
3346 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3347 "pointer of type %<void *%> used in subtraction");
3348 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3349 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3350 "pointer to a function used in subtraction");
3352 /* If the conversion to ptrdiff_type does anything like widening or
3353 converting a partial to an integral mode, we get a convert_expression
3354 that is in the way to do any simplifications.
3355 (fold-const.c doesn't know that the extra bits won't be needed.
3356 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3357 different mode in place.)
3358 So first try to find a common term here 'by hand'; we want to cover
3359 at least the cases that occur in legal static initializers. */
3360 if (CONVERT_EXPR_P (op0)
3361 && (TYPE_PRECISION (TREE_TYPE (op0))
3362 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3363 con0 = TREE_OPERAND (op0, 0);
3364 else
3365 con0 = op0;
3366 if (CONVERT_EXPR_P (op1)
3367 && (TYPE_PRECISION (TREE_TYPE (op1))
3368 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3369 con1 = TREE_OPERAND (op1, 0);
3370 else
3371 con1 = op1;
3373 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3375 lit0 = TREE_OPERAND (con0, 1);
3376 con0 = TREE_OPERAND (con0, 0);
3378 else
3379 lit0 = integer_zero_node;
3381 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
3383 lit1 = TREE_OPERAND (con1, 1);
3384 con1 = TREE_OPERAND (con1, 0);
3386 else
3387 lit1 = integer_zero_node;
3389 if (operand_equal_p (con0, con1, 0))
3391 op0 = lit0;
3392 op1 = lit1;
3396 /* First do the subtraction as integers;
3397 then drop through to build the divide operator.
3398 Do not do default conversions on the minus operator
3399 in case restype is a short type. */
3401 op0 = build_binary_op (loc,
3402 MINUS_EXPR, convert (inttype, op0),
3403 convert (inttype, op1), 0);
3404 /* This generates an error if op1 is pointer to incomplete type. */
3405 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3406 error_at (loc, "arithmetic on pointer to an incomplete type");
3408 /* This generates an error if op0 is pointer to incomplete type. */
3409 op1 = c_size_in_bytes (target_type);
3411 /* Divide by the size, in easiest possible way. */
3412 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3413 op0, convert (inttype, op1));
3415 /* Convert to final result type if necessary. */
3416 return convert (restype, result);
3419 /* Construct and perhaps optimize a tree representation
3420 for a unary operation. CODE, a tree_code, specifies the operation
3421 and XARG is the operand.
3422 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3423 the default promotions (such as from short to int).
3424 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3425 allows non-lvalues; this is only used to handle conversion of non-lvalue
3426 arrays to pointers in C99.
3428 LOCATION is the location of the operator. */
3430 tree
3431 build_unary_op (location_t location,
3432 enum tree_code code, tree xarg, int flag)
3434 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3435 tree arg = xarg;
3436 tree argtype = 0;
3437 enum tree_code typecode;
3438 tree val;
3439 tree ret = error_mark_node;
3440 tree eptype = NULL_TREE;
3441 int noconvert = flag;
3442 const char *invalid_op_diag;
3443 bool int_operands;
3445 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3446 if (int_operands)
3447 arg = remove_c_maybe_const_expr (arg);
3449 if (code != ADDR_EXPR)
3450 arg = require_complete_type (arg);
3452 typecode = TREE_CODE (TREE_TYPE (arg));
3453 if (typecode == ERROR_MARK)
3454 return error_mark_node;
3455 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3456 typecode = INTEGER_TYPE;
3458 if ((invalid_op_diag
3459 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3461 error_at (location, invalid_op_diag);
3462 return error_mark_node;
3465 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3467 eptype = TREE_TYPE (arg);
3468 arg = TREE_OPERAND (arg, 0);
3471 switch (code)
3473 case CONVERT_EXPR:
3474 /* This is used for unary plus, because a CONVERT_EXPR
3475 is enough to prevent anybody from looking inside for
3476 associativity, but won't generate any code. */
3477 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3478 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3479 || typecode == VECTOR_TYPE))
3481 error_at (location, "wrong type argument to unary plus");
3482 return error_mark_node;
3484 else if (!noconvert)
3485 arg = default_conversion (arg);
3486 arg = non_lvalue_loc (location, arg);
3487 break;
3489 case NEGATE_EXPR:
3490 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3491 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3492 || typecode == VECTOR_TYPE))
3494 error_at (location, "wrong type argument to unary minus");
3495 return error_mark_node;
3497 else if (!noconvert)
3498 arg = default_conversion (arg);
3499 break;
3501 case BIT_NOT_EXPR:
3502 /* ~ works on integer types and non float vectors. */
3503 if (typecode == INTEGER_TYPE
3504 || (typecode == VECTOR_TYPE
3505 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3507 if (!noconvert)
3508 arg = default_conversion (arg);
3510 else if (typecode == COMPLEX_TYPE)
3512 code = CONJ_EXPR;
3513 pedwarn (location, OPT_Wpedantic,
3514 "ISO C does not support %<~%> for complex conjugation");
3515 if (!noconvert)
3516 arg = default_conversion (arg);
3518 else
3520 error_at (location, "wrong type argument to bit-complement");
3521 return error_mark_node;
3523 break;
3525 case ABS_EXPR:
3526 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3528 error_at (location, "wrong type argument to abs");
3529 return error_mark_node;
3531 else if (!noconvert)
3532 arg = default_conversion (arg);
3533 break;
3535 case CONJ_EXPR:
3536 /* Conjugating a real value is a no-op, but allow it anyway. */
3537 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3538 || typecode == COMPLEX_TYPE))
3540 error_at (location, "wrong type argument to conjugation");
3541 return error_mark_node;
3543 else if (!noconvert)
3544 arg = default_conversion (arg);
3545 break;
3547 case TRUTH_NOT_EXPR:
3548 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3549 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3550 && typecode != COMPLEX_TYPE)
3552 error_at (location,
3553 "wrong type argument to unary exclamation mark");
3554 return error_mark_node;
3556 if (int_operands)
3558 arg = c_objc_common_truthvalue_conversion (location, xarg);
3559 arg = remove_c_maybe_const_expr (arg);
3561 else
3562 arg = c_objc_common_truthvalue_conversion (location, arg);
3563 ret = invert_truthvalue_loc (location, arg);
3564 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3565 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3566 location = EXPR_LOCATION (ret);
3567 goto return_build_unary_op;
3569 case REALPART_EXPR:
3570 case IMAGPART_EXPR:
3571 ret = build_real_imag_expr (location, code, arg);
3572 if (ret == error_mark_node)
3573 return error_mark_node;
3574 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3575 eptype = TREE_TYPE (eptype);
3576 goto return_build_unary_op;
3578 case PREINCREMENT_EXPR:
3579 case POSTINCREMENT_EXPR:
3580 case PREDECREMENT_EXPR:
3581 case POSTDECREMENT_EXPR:
3583 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3585 tree inner = build_unary_op (location, code,
3586 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3587 if (inner == error_mark_node)
3588 return error_mark_node;
3589 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3590 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3591 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3592 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3593 goto return_build_unary_op;
3596 /* Complain about anything that is not a true lvalue. In
3597 Objective-C, skip this check for property_refs. */
3598 if (!objc_is_property_ref (arg)
3599 && !lvalue_or_else (location,
3600 arg, ((code == PREINCREMENT_EXPR
3601 || code == POSTINCREMENT_EXPR)
3602 ? lv_increment
3603 : lv_decrement)))
3604 return error_mark_node;
3606 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3608 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3609 warning_at (location, OPT_Wc___compat,
3610 "increment of enumeration value is invalid in C++");
3611 else
3612 warning_at (location, OPT_Wc___compat,
3613 "decrement of enumeration value is invalid in C++");
3616 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3617 arg = c_fully_fold (arg, false, NULL);
3619 /* Increment or decrement the real part of the value,
3620 and don't change the imaginary part. */
3621 if (typecode == COMPLEX_TYPE)
3623 tree real, imag;
3625 pedwarn (location, OPT_Wpedantic,
3626 "ISO C does not support %<++%> and %<--%> on complex types");
3628 arg = stabilize_reference (arg);
3629 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3630 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3631 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3632 if (real == error_mark_node || imag == error_mark_node)
3633 return error_mark_node;
3634 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3635 real, imag);
3636 goto return_build_unary_op;
3639 /* Report invalid types. */
3641 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3642 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3644 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3645 error_at (location, "wrong type argument to increment");
3646 else
3647 error_at (location, "wrong type argument to decrement");
3649 return error_mark_node;
3653 tree inc;
3655 argtype = TREE_TYPE (arg);
3657 /* Compute the increment. */
3659 if (typecode == POINTER_TYPE)
3661 /* If pointer target is an undefined struct,
3662 we just cannot know how to do the arithmetic. */
3663 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3665 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3666 error_at (location,
3667 "increment of pointer to unknown structure");
3668 else
3669 error_at (location,
3670 "decrement of pointer to unknown structure");
3672 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3673 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3675 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3676 pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3677 "wrong type argument to increment");
3678 else
3679 pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3680 "wrong type argument to decrement");
3683 inc = c_size_in_bytes (TREE_TYPE (argtype));
3684 inc = convert_to_ptrofftype_loc (location, inc);
3686 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3688 /* For signed fract types, we invert ++ to -- or
3689 -- to ++, and change inc from 1 to -1, because
3690 it is not possible to represent 1 in signed fract constants.
3691 For unsigned fract types, the result always overflows and
3692 we get an undefined (original) or the maximum value. */
3693 if (code == PREINCREMENT_EXPR)
3694 code = PREDECREMENT_EXPR;
3695 else if (code == PREDECREMENT_EXPR)
3696 code = PREINCREMENT_EXPR;
3697 else if (code == POSTINCREMENT_EXPR)
3698 code = POSTDECREMENT_EXPR;
3699 else /* code == POSTDECREMENT_EXPR */
3700 code = POSTINCREMENT_EXPR;
3702 inc = integer_minus_one_node;
3703 inc = convert (argtype, inc);
3705 else
3707 inc = integer_one_node;
3708 inc = convert (argtype, inc);
3711 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3712 need to ask Objective-C to build the increment or decrement
3713 expression for it. */
3714 if (objc_is_property_ref (arg))
3715 return objc_build_incr_expr_for_property_ref (location, code,
3716 arg, inc);
3718 /* Report a read-only lvalue. */
3719 if (TYPE_READONLY (argtype))
3721 readonly_error (arg,
3722 ((code == PREINCREMENT_EXPR
3723 || code == POSTINCREMENT_EXPR)
3724 ? lv_increment : lv_decrement));
3725 return error_mark_node;
3727 else if (TREE_READONLY (arg))
3728 readonly_warning (arg,
3729 ((code == PREINCREMENT_EXPR
3730 || code == POSTINCREMENT_EXPR)
3731 ? lv_increment : lv_decrement));
3733 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3734 val = boolean_increment (code, arg);
3735 else
3736 val = build2 (code, TREE_TYPE (arg), arg, inc);
3737 TREE_SIDE_EFFECTS (val) = 1;
3738 if (TREE_CODE (val) != code)
3739 TREE_NO_WARNING (val) = 1;
3740 ret = val;
3741 goto return_build_unary_op;
3744 case ADDR_EXPR:
3745 /* Note that this operation never does default_conversion. */
3747 /* The operand of unary '&' must be an lvalue (which excludes
3748 expressions of type void), or, in C99, the result of a [] or
3749 unary '*' operator. */
3750 if (VOID_TYPE_P (TREE_TYPE (arg))
3751 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3752 && (TREE_CODE (arg) != INDIRECT_REF
3753 || !flag_isoc99))
3754 pedwarn (location, 0, "taking address of expression of type %<void%>");
3756 /* Let &* cancel out to simplify resulting code. */
3757 if (TREE_CODE (arg) == INDIRECT_REF)
3759 /* Don't let this be an lvalue. */
3760 if (lvalue_p (TREE_OPERAND (arg, 0)))
3761 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3762 ret = TREE_OPERAND (arg, 0);
3763 goto return_build_unary_op;
3766 /* For &x[y], return x+y */
3767 if (TREE_CODE (arg) == ARRAY_REF)
3769 tree op0 = TREE_OPERAND (arg, 0);
3770 if (!c_mark_addressable (op0))
3771 return error_mark_node;
3774 /* Anything not already handled and not a true memory reference
3775 or a non-lvalue array is an error. */
3776 else if (typecode != FUNCTION_TYPE && !flag
3777 && !lvalue_or_else (location, arg, lv_addressof))
3778 return error_mark_node;
3780 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3781 folding later. */
3782 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3784 tree inner = build_unary_op (location, code,
3785 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3786 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3787 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3788 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3789 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3790 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3791 goto return_build_unary_op;
3794 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3795 argtype = TREE_TYPE (arg);
3797 /* If the lvalue is const or volatile, merge that into the type
3798 to which the address will point. This is only needed
3799 for function types. */
3800 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3801 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3802 && TREE_CODE (argtype) == FUNCTION_TYPE)
3804 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3805 int quals = orig_quals;
3807 if (TREE_READONLY (arg))
3808 quals |= TYPE_QUAL_CONST;
3809 if (TREE_THIS_VOLATILE (arg))
3810 quals |= TYPE_QUAL_VOLATILE;
3812 argtype = c_build_qualified_type (argtype, quals);
3815 if (!c_mark_addressable (arg))
3816 return error_mark_node;
3818 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3819 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3821 argtype = build_pointer_type (argtype);
3823 /* ??? Cope with user tricks that amount to offsetof. Delete this
3824 when we have proper support for integer constant expressions. */
3825 val = get_base_address (arg);
3826 if (val && TREE_CODE (val) == INDIRECT_REF
3827 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3829 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
3830 goto return_build_unary_op;
3833 val = build1 (ADDR_EXPR, argtype, arg);
3835 ret = val;
3836 goto return_build_unary_op;
3838 default:
3839 gcc_unreachable ();
3842 if (argtype == 0)
3843 argtype = TREE_TYPE (arg);
3844 if (TREE_CODE (arg) == INTEGER_CST)
3845 ret = (require_constant_value
3846 ? fold_build1_initializer_loc (location, code, argtype, arg)
3847 : fold_build1_loc (location, code, argtype, arg));
3848 else
3849 ret = build1 (code, argtype, arg);
3850 return_build_unary_op:
3851 gcc_assert (ret != error_mark_node);
3852 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3853 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3854 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3855 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3856 ret = note_integer_operands (ret);
3857 if (eptype)
3858 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3859 protected_set_expr_location (ret, location);
3860 return ret;
3863 /* Return nonzero if REF is an lvalue valid for this language.
3864 Lvalues can be assigned, unless their type has TYPE_READONLY.
3865 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3867 bool
3868 lvalue_p (const_tree ref)
3870 const enum tree_code code = TREE_CODE (ref);
3872 switch (code)
3874 case REALPART_EXPR:
3875 case IMAGPART_EXPR:
3876 case COMPONENT_REF:
3877 return lvalue_p (TREE_OPERAND (ref, 0));
3879 case C_MAYBE_CONST_EXPR:
3880 return lvalue_p (TREE_OPERAND (ref, 1));
3882 case COMPOUND_LITERAL_EXPR:
3883 case STRING_CST:
3884 return 1;
3886 case INDIRECT_REF:
3887 case ARRAY_REF:
3888 case VAR_DECL:
3889 case PARM_DECL:
3890 case RESULT_DECL:
3891 case ERROR_MARK:
3892 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3893 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3895 case BIND_EXPR:
3896 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3898 default:
3899 return 0;
3903 /* Give a warning for storing in something that is read-only in GCC
3904 terms but not const in ISO C terms. */
3906 static void
3907 readonly_warning (tree arg, enum lvalue_use use)
3909 switch (use)
3911 case lv_assign:
3912 warning (0, "assignment of read-only location %qE", arg);
3913 break;
3914 case lv_increment:
3915 warning (0, "increment of read-only location %qE", arg);
3916 break;
3917 case lv_decrement:
3918 warning (0, "decrement of read-only location %qE", arg);
3919 break;
3920 default:
3921 gcc_unreachable ();
3923 return;
3927 /* Return nonzero if REF is an lvalue valid for this language;
3928 otherwise, print an error message and return zero. USE says
3929 how the lvalue is being used and so selects the error message.
3930 LOCATION is the location at which any error should be reported. */
3932 static int
3933 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
3935 int win = lvalue_p (ref);
3937 if (!win)
3938 lvalue_error (loc, use);
3940 return win;
3943 /* Mark EXP saying that we need to be able to take the
3944 address of it; it should not be allocated in a register.
3945 Returns true if successful. */
3947 bool
3948 c_mark_addressable (tree exp)
3950 tree x = exp;
3952 while (1)
3953 switch (TREE_CODE (x))
3955 case COMPONENT_REF:
3956 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3958 error
3959 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3960 return false;
3963 /* ... fall through ... */
3965 case ADDR_EXPR:
3966 case ARRAY_REF:
3967 case REALPART_EXPR:
3968 case IMAGPART_EXPR:
3969 x = TREE_OPERAND (x, 0);
3970 break;
3972 case COMPOUND_LITERAL_EXPR:
3973 case CONSTRUCTOR:
3974 TREE_ADDRESSABLE (x) = 1;
3975 return true;
3977 case VAR_DECL:
3978 case CONST_DECL:
3979 case PARM_DECL:
3980 case RESULT_DECL:
3981 if (C_DECL_REGISTER (x)
3982 && DECL_NONLOCAL (x))
3984 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3986 error
3987 ("global register variable %qD used in nested function", x);
3988 return false;
3990 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3992 else if (C_DECL_REGISTER (x))
3994 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3995 error ("address of global register variable %qD requested", x);
3996 else
3997 error ("address of register variable %qD requested", x);
3998 return false;
4001 /* drops in */
4002 case FUNCTION_DECL:
4003 TREE_ADDRESSABLE (x) = 1;
4004 /* drops out */
4005 default:
4006 return true;
4010 /* Convert EXPR to TYPE, warning about conversion problems with
4011 constants. SEMANTIC_TYPE is the type this conversion would use
4012 without excess precision. If SEMANTIC_TYPE is NULL, this function
4013 is equivalent to convert_and_check. This function is a wrapper that
4014 handles conversions that may be different than
4015 the usual ones because of excess precision. */
4017 static tree
4018 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4020 if (TREE_TYPE (expr) == type)
4021 return expr;
4023 if (!semantic_type)
4024 return convert_and_check (type, expr);
4026 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4027 && TREE_TYPE (expr) != semantic_type)
4029 /* For integers, we need to check the real conversion, not
4030 the conversion to the excess precision type. */
4031 expr = convert_and_check (semantic_type, expr);
4033 /* Result type is the excess precision type, which should be
4034 large enough, so do not check. */
4035 return convert (type, expr);
4038 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4039 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4040 if folded to an integer constant then the unselected half may
4041 contain arbitrary operations not normally permitted in constant
4042 expressions. Set the location of the expression to LOC. */
4044 tree
4045 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4046 tree op1, tree op1_original_type, tree op2,
4047 tree op2_original_type)
4049 tree type1;
4050 tree type2;
4051 enum tree_code code1;
4052 enum tree_code code2;
4053 tree result_type = NULL;
4054 tree semantic_result_type = NULL;
4055 tree orig_op1 = op1, orig_op2 = op2;
4056 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4057 bool ifexp_int_operands;
4058 tree ret;
4060 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4061 if (op1_int_operands)
4062 op1 = remove_c_maybe_const_expr (op1);
4063 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4064 if (op2_int_operands)
4065 op2 = remove_c_maybe_const_expr (op2);
4066 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4067 if (ifexp_int_operands)
4068 ifexp = remove_c_maybe_const_expr (ifexp);
4070 /* Promote both alternatives. */
4072 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4073 op1 = default_conversion (op1);
4074 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4075 op2 = default_conversion (op2);
4077 if (TREE_CODE (ifexp) == ERROR_MARK
4078 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4079 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4080 return error_mark_node;
4082 type1 = TREE_TYPE (op1);
4083 code1 = TREE_CODE (type1);
4084 type2 = TREE_TYPE (op2);
4085 code2 = TREE_CODE (type2);
4087 /* C90 does not permit non-lvalue arrays in conditional expressions.
4088 In C99 they will be pointers by now. */
4089 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4091 error_at (colon_loc, "non-lvalue array in conditional expression");
4092 return error_mark_node;
4095 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4096 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4097 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4098 || code1 == COMPLEX_TYPE)
4099 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4100 || code2 == COMPLEX_TYPE))
4102 semantic_result_type = c_common_type (type1, type2);
4103 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4105 op1 = TREE_OPERAND (op1, 0);
4106 type1 = TREE_TYPE (op1);
4107 gcc_assert (TREE_CODE (type1) == code1);
4109 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4111 op2 = TREE_OPERAND (op2, 0);
4112 type2 = TREE_TYPE (op2);
4113 gcc_assert (TREE_CODE (type2) == code2);
4117 if (warn_cxx_compat)
4119 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4120 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4122 if (TREE_CODE (t1) == ENUMERAL_TYPE
4123 && TREE_CODE (t2) == ENUMERAL_TYPE
4124 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4125 warning_at (colon_loc, OPT_Wc___compat,
4126 ("different enum types in conditional is "
4127 "invalid in C++: %qT vs %qT"),
4128 t1, t2);
4131 /* Quickly detect the usual case where op1 and op2 have the same type
4132 after promotion. */
4133 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4135 if (type1 == type2)
4136 result_type = type1;
4137 else
4138 result_type = TYPE_MAIN_VARIANT (type1);
4140 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4141 || code1 == COMPLEX_TYPE)
4142 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4143 || code2 == COMPLEX_TYPE))
4145 result_type = c_common_type (type1, type2);
4146 do_warn_double_promotion (result_type, type1, type2,
4147 "implicit conversion from %qT to %qT to "
4148 "match other result of conditional",
4149 colon_loc);
4151 /* If -Wsign-compare, warn here if type1 and type2 have
4152 different signedness. We'll promote the signed to unsigned
4153 and later code won't know it used to be different.
4154 Do this check on the original types, so that explicit casts
4155 will be considered, but default promotions won't. */
4156 if (c_inhibit_evaluation_warnings == 0)
4158 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4159 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4161 if (unsigned_op1 ^ unsigned_op2)
4163 bool ovf;
4165 /* Do not warn if the result type is signed, since the
4166 signed type will only be chosen if it can represent
4167 all the values of the unsigned type. */
4168 if (!TYPE_UNSIGNED (result_type))
4169 /* OK */;
4170 else
4172 bool op1_maybe_const = true;
4173 bool op2_maybe_const = true;
4175 /* Do not warn if the signed quantity is an
4176 unsuffixed integer literal (or some static
4177 constant expression involving such literals) and
4178 it is non-negative. This warning requires the
4179 operands to be folded for best results, so do
4180 that folding in this case even without
4181 warn_sign_compare to avoid warning options
4182 possibly affecting code generation. */
4183 c_inhibit_evaluation_warnings
4184 += (ifexp == truthvalue_false_node);
4185 op1 = c_fully_fold (op1, require_constant_value,
4186 &op1_maybe_const);
4187 c_inhibit_evaluation_warnings
4188 -= (ifexp == truthvalue_false_node);
4190 c_inhibit_evaluation_warnings
4191 += (ifexp == truthvalue_true_node);
4192 op2 = c_fully_fold (op2, require_constant_value,
4193 &op2_maybe_const);
4194 c_inhibit_evaluation_warnings
4195 -= (ifexp == truthvalue_true_node);
4197 if (warn_sign_compare)
4199 if ((unsigned_op2
4200 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4201 || (unsigned_op1
4202 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4203 /* OK */;
4204 else
4205 warning_at (colon_loc, OPT_Wsign_compare,
4206 ("signed and unsigned type in "
4207 "conditional expression"));
4209 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4210 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4211 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4212 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4217 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4219 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4220 pedwarn (colon_loc, OPT_Wpedantic,
4221 "ISO C forbids conditional expr with only one void side");
4222 result_type = void_type_node;
4224 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4226 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4227 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4228 addr_space_t as_common;
4230 if (comp_target_types (colon_loc, type1, type2))
4231 result_type = common_pointer_type (type1, type2);
4232 else if (null_pointer_constant_p (orig_op1))
4233 result_type = type2;
4234 else if (null_pointer_constant_p (orig_op2))
4235 result_type = type1;
4236 else if (!addr_space_superset (as1, as2, &as_common))
4238 error_at (colon_loc, "pointers to disjoint address spaces "
4239 "used in conditional expression");
4240 return error_mark_node;
4242 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4244 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4245 pedwarn (colon_loc, OPT_Wpedantic,
4246 "ISO C forbids conditional expr between "
4247 "%<void *%> and function pointer");
4248 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4249 TREE_TYPE (type2)));
4251 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4253 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4254 pedwarn (colon_loc, OPT_Wpedantic,
4255 "ISO C forbids conditional expr between "
4256 "%<void *%> and function pointer");
4257 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4258 TREE_TYPE (type1)));
4260 /* Objective-C pointer comparisons are a bit more lenient. */
4261 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4262 result_type = objc_common_type (type1, type2);
4263 else
4265 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4267 pedwarn (colon_loc, 0,
4268 "pointer type mismatch in conditional expression");
4269 result_type = build_pointer_type
4270 (build_qualified_type (void_type_node, qual));
4273 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4275 if (!null_pointer_constant_p (orig_op2))
4276 pedwarn (colon_loc, 0,
4277 "pointer/integer type mismatch in conditional expression");
4278 else
4280 op2 = null_pointer_node;
4282 result_type = type1;
4284 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4286 if (!null_pointer_constant_p (orig_op1))
4287 pedwarn (colon_loc, 0,
4288 "pointer/integer type mismatch in conditional expression");
4289 else
4291 op1 = null_pointer_node;
4293 result_type = type2;
4296 if (!result_type)
4298 if (flag_cond_mismatch)
4299 result_type = void_type_node;
4300 else
4302 error_at (colon_loc, "type mismatch in conditional expression");
4303 return error_mark_node;
4307 /* Merge const and volatile flags of the incoming types. */
4308 result_type
4309 = build_type_variant (result_type,
4310 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4311 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4313 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4314 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4316 if (ifexp_bcp && ifexp == truthvalue_true_node)
4318 op2_int_operands = true;
4319 op1 = c_fully_fold (op1, require_constant_value, NULL);
4321 if (ifexp_bcp && ifexp == truthvalue_false_node)
4323 op1_int_operands = true;
4324 op2 = c_fully_fold (op2, require_constant_value, NULL);
4326 int_const = int_operands = (ifexp_int_operands
4327 && op1_int_operands
4328 && op2_int_operands);
4329 if (int_operands)
4331 int_const = ((ifexp == truthvalue_true_node
4332 && TREE_CODE (orig_op1) == INTEGER_CST
4333 && !TREE_OVERFLOW (orig_op1))
4334 || (ifexp == truthvalue_false_node
4335 && TREE_CODE (orig_op2) == INTEGER_CST
4336 && !TREE_OVERFLOW (orig_op2)));
4338 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4339 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4340 else
4342 if (int_operands)
4344 op1 = remove_c_maybe_const_expr (op1);
4345 op2 = remove_c_maybe_const_expr (op2);
4347 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4348 if (int_operands)
4349 ret = note_integer_operands (ret);
4351 if (semantic_result_type)
4352 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4354 protected_set_expr_location (ret, colon_loc);
4355 return ret;
4358 /* Return a compound expression that performs two expressions and
4359 returns the value of the second of them.
4361 LOC is the location of the COMPOUND_EXPR. */
4363 tree
4364 build_compound_expr (location_t loc, tree expr1, tree expr2)
4366 bool expr1_int_operands, expr2_int_operands;
4367 tree eptype = NULL_TREE;
4368 tree ret;
4370 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4371 if (expr1_int_operands)
4372 expr1 = remove_c_maybe_const_expr (expr1);
4373 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4374 if (expr2_int_operands)
4375 expr2 = remove_c_maybe_const_expr (expr2);
4377 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4378 expr1 = TREE_OPERAND (expr1, 0);
4379 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4381 eptype = TREE_TYPE (expr2);
4382 expr2 = TREE_OPERAND (expr2, 0);
4385 if (!TREE_SIDE_EFFECTS (expr1))
4387 /* The left-hand operand of a comma expression is like an expression
4388 statement: with -Wunused, we should warn if it doesn't have
4389 any side-effects, unless it was explicitly cast to (void). */
4390 if (warn_unused_value)
4392 if (VOID_TYPE_P (TREE_TYPE (expr1))
4393 && CONVERT_EXPR_P (expr1))
4394 ; /* (void) a, b */
4395 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4396 && TREE_CODE (expr1) == COMPOUND_EXPR
4397 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4398 ; /* (void) a, (void) b, c */
4399 else
4400 warning_at (loc, OPT_Wunused_value,
4401 "left-hand operand of comma expression has no effect");
4405 /* With -Wunused, we should also warn if the left-hand operand does have
4406 side-effects, but computes a value which is not used. For example, in
4407 `foo() + bar(), baz()' the result of the `+' operator is not used,
4408 so we should issue a warning. */
4409 else if (warn_unused_value)
4410 warn_if_unused_value (expr1, loc);
4412 if (expr2 == error_mark_node)
4413 return error_mark_node;
4415 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4417 if (flag_isoc99
4418 && expr1_int_operands
4419 && expr2_int_operands)
4420 ret = note_integer_operands (ret);
4422 if (eptype)
4423 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4425 protected_set_expr_location (ret, loc);
4426 return ret;
4429 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4430 which we are casting. OTYPE is the type of the expression being
4431 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4432 of the cast. -Wcast-qual appeared on the command line. Named
4433 address space qualifiers are not handled here, because they result
4434 in different warnings. */
4436 static void
4437 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4439 tree in_type = type;
4440 tree in_otype = otype;
4441 int added = 0;
4442 int discarded = 0;
4443 bool is_const;
4445 /* Check that the qualifiers on IN_TYPE are a superset of the
4446 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4447 nodes is uninteresting and we stop as soon as we hit a
4448 non-POINTER_TYPE node on either type. */
4451 in_otype = TREE_TYPE (in_otype);
4452 in_type = TREE_TYPE (in_type);
4454 /* GNU C allows cv-qualified function types. 'const' means the
4455 function is very pure, 'volatile' means it can't return. We
4456 need to warn when such qualifiers are added, not when they're
4457 taken away. */
4458 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4459 && TREE_CODE (in_type) == FUNCTION_TYPE)
4460 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4461 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4462 else
4463 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4464 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4466 while (TREE_CODE (in_type) == POINTER_TYPE
4467 && TREE_CODE (in_otype) == POINTER_TYPE);
4469 if (added)
4470 warning_at (loc, OPT_Wcast_qual,
4471 "cast adds %q#v qualifier to function type", added);
4473 if (discarded)
4474 /* There are qualifiers present in IN_OTYPE that are not present
4475 in IN_TYPE. */
4476 warning_at (loc, OPT_Wcast_qual,
4477 "cast discards %q#v qualifier from pointer target type",
4478 discarded);
4480 if (added || discarded)
4481 return;
4483 /* A cast from **T to const **T is unsafe, because it can cause a
4484 const value to be changed with no additional warning. We only
4485 issue this warning if T is the same on both sides, and we only
4486 issue the warning if there are the same number of pointers on
4487 both sides, as otherwise the cast is clearly unsafe anyhow. A
4488 cast is unsafe when a qualifier is added at one level and const
4489 is not present at all outer levels.
4491 To issue this warning, we check at each level whether the cast
4492 adds new qualifiers not already seen. We don't need to special
4493 case function types, as they won't have the same
4494 TYPE_MAIN_VARIANT. */
4496 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4497 return;
4498 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4499 return;
4501 in_type = type;
4502 in_otype = otype;
4503 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4506 in_type = TREE_TYPE (in_type);
4507 in_otype = TREE_TYPE (in_otype);
4508 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4509 && !is_const)
4511 warning_at (loc, OPT_Wcast_qual,
4512 "to be safe all intermediate pointers in cast from "
4513 "%qT to %qT must be %<const%> qualified",
4514 otype, type);
4515 break;
4517 if (is_const)
4518 is_const = TYPE_READONLY (in_type);
4520 while (TREE_CODE (in_type) == POINTER_TYPE);
4523 /* Build an expression representing a cast to type TYPE of expression EXPR.
4524 LOC is the location of the cast-- typically the open paren of the cast. */
4526 tree
4527 build_c_cast (location_t loc, tree type, tree expr)
4529 tree value;
4531 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4532 expr = TREE_OPERAND (expr, 0);
4534 value = expr;
4536 if (type == error_mark_node || expr == error_mark_node)
4537 return error_mark_node;
4539 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4540 only in <protocol> qualifications. But when constructing cast expressions,
4541 the protocols do matter and must be kept around. */
4542 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4543 return build1 (NOP_EXPR, type, expr);
4545 type = TYPE_MAIN_VARIANT (type);
4547 if (TREE_CODE (type) == ARRAY_TYPE)
4549 error_at (loc, "cast specifies array type");
4550 return error_mark_node;
4553 if (TREE_CODE (type) == FUNCTION_TYPE)
4555 error_at (loc, "cast specifies function type");
4556 return error_mark_node;
4559 if (!VOID_TYPE_P (type))
4561 value = require_complete_type (value);
4562 if (value == error_mark_node)
4563 return error_mark_node;
4566 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4568 if (TREE_CODE (type) == RECORD_TYPE
4569 || TREE_CODE (type) == UNION_TYPE)
4570 pedwarn (loc, OPT_Wpedantic,
4571 "ISO C forbids casting nonscalar to the same type");
4573 else if (TREE_CODE (type) == UNION_TYPE)
4575 tree field;
4577 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4578 if (TREE_TYPE (field) != error_mark_node
4579 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4580 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4581 break;
4583 if (field)
4585 tree t;
4586 bool maybe_const = true;
4588 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
4589 t = c_fully_fold (value, false, &maybe_const);
4590 t = build_constructor_single (type, field, t);
4591 if (!maybe_const)
4592 t = c_wrap_maybe_const (t, true);
4593 t = digest_init (loc, type, t,
4594 NULL_TREE, false, true, 0);
4595 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4596 return t;
4598 error_at (loc, "cast to union type from type not present in union");
4599 return error_mark_node;
4601 else
4603 tree otype, ovalue;
4605 if (type == void_type_node)
4607 tree t = build1 (CONVERT_EXPR, type, value);
4608 SET_EXPR_LOCATION (t, loc);
4609 return t;
4612 otype = TREE_TYPE (value);
4614 /* Optionally warn about potentially worrisome casts. */
4615 if (warn_cast_qual
4616 && TREE_CODE (type) == POINTER_TYPE
4617 && TREE_CODE (otype) == POINTER_TYPE)
4618 handle_warn_cast_qual (loc, type, otype);
4620 /* Warn about conversions between pointers to disjoint
4621 address spaces. */
4622 if (TREE_CODE (type) == POINTER_TYPE
4623 && TREE_CODE (otype) == POINTER_TYPE
4624 && !null_pointer_constant_p (value))
4626 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4627 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4628 addr_space_t as_common;
4630 if (!addr_space_superset (as_to, as_from, &as_common))
4632 if (ADDR_SPACE_GENERIC_P (as_from))
4633 warning_at (loc, 0, "cast to %s address space pointer "
4634 "from disjoint generic address space pointer",
4635 c_addr_space_name (as_to));
4637 else if (ADDR_SPACE_GENERIC_P (as_to))
4638 warning_at (loc, 0, "cast to generic address space pointer "
4639 "from disjoint %s address space pointer",
4640 c_addr_space_name (as_from));
4642 else
4643 warning_at (loc, 0, "cast to %s address space pointer "
4644 "from disjoint %s address space pointer",
4645 c_addr_space_name (as_to),
4646 c_addr_space_name (as_from));
4650 /* Warn about possible alignment problems. */
4651 if (STRICT_ALIGNMENT
4652 && TREE_CODE (type) == POINTER_TYPE
4653 && TREE_CODE (otype) == POINTER_TYPE
4654 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4655 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4656 /* Don't warn about opaque types, where the actual alignment
4657 restriction is unknown. */
4658 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4659 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4660 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4661 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4662 warning_at (loc, OPT_Wcast_align,
4663 "cast increases required alignment of target type");
4665 if (TREE_CODE (type) == INTEGER_TYPE
4666 && TREE_CODE (otype) == POINTER_TYPE
4667 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4668 /* Unlike conversion of integers to pointers, where the
4669 warning is disabled for converting constants because
4670 of cases such as SIG_*, warn about converting constant
4671 pointers to integers. In some cases it may cause unwanted
4672 sign extension, and a warning is appropriate. */
4673 warning_at (loc, OPT_Wpointer_to_int_cast,
4674 "cast from pointer to integer of different size");
4676 if (TREE_CODE (value) == CALL_EXPR
4677 && TREE_CODE (type) != TREE_CODE (otype))
4678 warning_at (loc, OPT_Wbad_function_cast,
4679 "cast from function call of type %qT "
4680 "to non-matching type %qT", otype, type);
4682 if (TREE_CODE (type) == POINTER_TYPE
4683 && TREE_CODE (otype) == INTEGER_TYPE
4684 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4685 /* Don't warn about converting any constant. */
4686 && !TREE_CONSTANT (value))
4687 warning_at (loc,
4688 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4689 "of different size");
4691 if (warn_strict_aliasing <= 2)
4692 strict_aliasing_warning (otype, type, expr);
4694 /* If pedantic, warn for conversions between function and object
4695 pointer types, except for converting a null pointer constant
4696 to function pointer type. */
4697 if (pedantic
4698 && TREE_CODE (type) == POINTER_TYPE
4699 && TREE_CODE (otype) == POINTER_TYPE
4700 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4701 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4702 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
4703 "conversion of function pointer to object pointer type");
4705 if (pedantic
4706 && TREE_CODE (type) == POINTER_TYPE
4707 && TREE_CODE (otype) == POINTER_TYPE
4708 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4709 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4710 && !null_pointer_constant_p (value))
4711 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
4712 "conversion of object pointer to function pointer type");
4714 ovalue = value;
4715 value = convert (type, value);
4717 /* Ignore any integer overflow caused by the cast. */
4718 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4720 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4722 if (!TREE_OVERFLOW (value))
4724 /* Avoid clobbering a shared constant. */
4725 value = copy_node (value);
4726 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4729 else if (TREE_OVERFLOW (value))
4730 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4731 value = build_int_cst_wide (TREE_TYPE (value),
4732 TREE_INT_CST_LOW (value),
4733 TREE_INT_CST_HIGH (value));
4737 /* Don't let a cast be an lvalue. */
4738 if (value == expr)
4739 value = non_lvalue_loc (loc, value);
4741 /* Don't allow the results of casting to floating-point or complex
4742 types be confused with actual constants, or casts involving
4743 integer and pointer types other than direct integer-to-integer
4744 and integer-to-pointer be confused with integer constant
4745 expressions and null pointer constants. */
4746 if (TREE_CODE (value) == REAL_CST
4747 || TREE_CODE (value) == COMPLEX_CST
4748 || (TREE_CODE (value) == INTEGER_CST
4749 && !((TREE_CODE (expr) == INTEGER_CST
4750 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4751 || TREE_CODE (expr) == REAL_CST
4752 || TREE_CODE (expr) == COMPLEX_CST)))
4753 value = build1 (NOP_EXPR, type, value);
4755 if (CAN_HAVE_LOCATION_P (value))
4756 SET_EXPR_LOCATION (value, loc);
4757 return value;
4760 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4761 location of the open paren of the cast, or the position of the cast
4762 expr. */
4763 tree
4764 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4766 tree type;
4767 tree type_expr = NULL_TREE;
4768 bool type_expr_const = true;
4769 tree ret;
4770 int saved_wsp = warn_strict_prototypes;
4772 /* This avoids warnings about unprototyped casts on
4773 integers. E.g. "#define SIG_DFL (void(*)())0". */
4774 if (TREE_CODE (expr) == INTEGER_CST)
4775 warn_strict_prototypes = 0;
4776 type = groktypename (type_name, &type_expr, &type_expr_const);
4777 warn_strict_prototypes = saved_wsp;
4779 ret = build_c_cast (loc, type, expr);
4780 if (type_expr)
4782 bool inner_expr_const = true;
4783 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
4784 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4785 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
4786 && inner_expr_const);
4787 SET_EXPR_LOCATION (ret, loc);
4790 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4791 SET_EXPR_LOCATION (ret, loc);
4793 /* C++ does not permits types to be defined in a cast, but it
4794 allows references to incomplete types. */
4795 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4796 warning_at (loc, OPT_Wc___compat,
4797 "defining a type in a cast is invalid in C++");
4799 return ret;
4802 /* Build an assignment expression of lvalue LHS from value RHS.
4803 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4804 may differ from TREE_TYPE (LHS) for an enum bitfield.
4805 MODIFYCODE is the code for a binary operator that we use
4806 to combine the old value of LHS with RHS to get the new value.
4807 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4808 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4809 which may differ from TREE_TYPE (RHS) for an enum value.
4811 LOCATION is the location of the MODIFYCODE operator.
4812 RHS_LOC is the location of the RHS. */
4814 tree
4815 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4816 enum tree_code modifycode,
4817 location_t rhs_loc, tree rhs, tree rhs_origtype)
4819 tree result;
4820 tree newrhs;
4821 tree rhs_semantic_type = NULL_TREE;
4822 tree lhstype = TREE_TYPE (lhs);
4823 tree olhstype = lhstype;
4824 bool npc;
4826 /* Types that aren't fully specified cannot be used in assignments. */
4827 lhs = require_complete_type (lhs);
4829 /* Avoid duplicate error messages from operands that had errors. */
4830 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4831 return error_mark_node;
4833 /* For ObjC properties, defer this check. */
4834 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4835 return error_mark_node;
4837 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4839 rhs_semantic_type = TREE_TYPE (rhs);
4840 rhs = TREE_OPERAND (rhs, 0);
4843 newrhs = rhs;
4845 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4847 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4848 lhs_origtype, modifycode, rhs_loc, rhs,
4849 rhs_origtype);
4850 if (inner == error_mark_node)
4851 return error_mark_node;
4852 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4853 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4854 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4855 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4856 protected_set_expr_location (result, location);
4857 return result;
4860 /* If a binary op has been requested, combine the old LHS value with the RHS
4861 producing the value we should actually store into the LHS. */
4863 if (modifycode != NOP_EXPR)
4865 lhs = c_fully_fold (lhs, false, NULL);
4866 lhs = stabilize_reference (lhs);
4867 newrhs = build_binary_op (location,
4868 modifycode, lhs, rhs, 1);
4870 /* The original type of the right hand side is no longer
4871 meaningful. */
4872 rhs_origtype = NULL_TREE;
4875 if (c_dialect_objc ())
4877 /* Check if we are modifying an Objective-C property reference;
4878 if so, we need to generate setter calls. */
4879 result = objc_maybe_build_modify_expr (lhs, newrhs);
4880 if (result)
4881 return result;
4883 /* Else, do the check that we postponed for Objective-C. */
4884 if (!lvalue_or_else (location, lhs, lv_assign))
4885 return error_mark_node;
4888 /* Give an error for storing in something that is 'const'. */
4890 if (TYPE_READONLY (lhstype)
4891 || ((TREE_CODE (lhstype) == RECORD_TYPE
4892 || TREE_CODE (lhstype) == UNION_TYPE)
4893 && C_TYPE_FIELDS_READONLY (lhstype)))
4895 readonly_error (lhs, lv_assign);
4896 return error_mark_node;
4898 else if (TREE_READONLY (lhs))
4899 readonly_warning (lhs, lv_assign);
4901 /* If storing into a structure or union member,
4902 it has probably been given type `int'.
4903 Compute the type that would go with
4904 the actual amount of storage the member occupies. */
4906 if (TREE_CODE (lhs) == COMPONENT_REF
4907 && (TREE_CODE (lhstype) == INTEGER_TYPE
4908 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4909 || TREE_CODE (lhstype) == REAL_TYPE
4910 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4911 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4913 /* If storing in a field that is in actuality a short or narrower than one,
4914 we must store in the field in its actual type. */
4916 if (lhstype != TREE_TYPE (lhs))
4918 lhs = copy_node (lhs);
4919 TREE_TYPE (lhs) = lhstype;
4922 /* Issue -Wc++-compat warnings about an assignment to an enum type
4923 when LHS does not have its original type. This happens for,
4924 e.g., an enum bitfield in a struct. */
4925 if (warn_cxx_compat
4926 && lhs_origtype != NULL_TREE
4927 && lhs_origtype != lhstype
4928 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4930 tree checktype = (rhs_origtype != NULL_TREE
4931 ? rhs_origtype
4932 : TREE_TYPE (rhs));
4933 if (checktype != error_mark_node
4934 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4935 warning_at (location, OPT_Wc___compat,
4936 "enum conversion in assignment is invalid in C++");
4939 /* Convert new value to destination type. Fold it first, then
4940 restore any excess precision information, for the sake of
4941 conversion warnings. */
4943 npc = null_pointer_constant_p (newrhs);
4944 newrhs = c_fully_fold (newrhs, false, NULL);
4945 if (rhs_semantic_type)
4946 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4947 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4948 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4949 if (TREE_CODE (newrhs) == ERROR_MARK)
4950 return error_mark_node;
4952 /* Emit ObjC write barrier, if necessary. */
4953 if (c_dialect_objc () && flag_objc_gc)
4955 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4956 if (result)
4958 protected_set_expr_location (result, location);
4959 return result;
4963 /* Scan operands. */
4965 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4966 TREE_SIDE_EFFECTS (result) = 1;
4967 protected_set_expr_location (result, location);
4969 /* If we got the LHS in a different type for storing in,
4970 convert the result back to the nominal type of LHS
4971 so that the value we return always has the same type
4972 as the LHS argument. */
4974 if (olhstype == TREE_TYPE (result))
4975 return result;
4977 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
4978 ic_assign, false, NULL_TREE, NULL_TREE, 0);
4979 protected_set_expr_location (result, location);
4980 return result;
4983 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
4984 This is used to implement -fplan9-extensions. */
4986 static bool
4987 find_anonymous_field_with_type (tree struct_type, tree type)
4989 tree field;
4990 bool found;
4992 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
4993 || TREE_CODE (struct_type) == UNION_TYPE);
4994 found = false;
4995 for (field = TYPE_FIELDS (struct_type);
4996 field != NULL_TREE;
4997 field = TREE_CHAIN (field))
4999 if (DECL_NAME (field) == NULL
5000 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5002 if (found)
5003 return false;
5004 found = true;
5006 else if (DECL_NAME (field) == NULL
5007 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5008 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5009 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5011 if (found)
5012 return false;
5013 found = true;
5016 return found;
5019 /* RHS is an expression whose type is pointer to struct. If there is
5020 an anonymous field in RHS with type TYPE, then return a pointer to
5021 that field in RHS. This is used with -fplan9-extensions. This
5022 returns NULL if no conversion could be found. */
5024 static tree
5025 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5027 tree rhs_struct_type, lhs_main_type;
5028 tree field, found_field;
5029 bool found_sub_field;
5030 tree ret;
5032 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5033 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5034 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5035 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5037 gcc_assert (POINTER_TYPE_P (type));
5038 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5040 found_field = NULL_TREE;
5041 found_sub_field = false;
5042 for (field = TYPE_FIELDS (rhs_struct_type);
5043 field != NULL_TREE;
5044 field = TREE_CHAIN (field))
5046 if (DECL_NAME (field) != NULL_TREE
5047 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5048 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5049 continue;
5050 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5052 if (found_field != NULL_TREE)
5053 return NULL_TREE;
5054 found_field = field;
5056 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5057 lhs_main_type))
5059 if (found_field != NULL_TREE)
5060 return NULL_TREE;
5061 found_field = field;
5062 found_sub_field = true;
5066 if (found_field == NULL_TREE)
5067 return NULL_TREE;
5069 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5070 build_fold_indirect_ref (rhs), found_field,
5071 NULL_TREE);
5072 ret = build_fold_addr_expr_loc (location, ret);
5074 if (found_sub_field)
5076 ret = convert_to_anonymous_field (location, type, ret);
5077 gcc_assert (ret != NULL_TREE);
5080 return ret;
5083 /* Convert value RHS to type TYPE as preparation for an assignment to
5084 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5085 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5086 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5087 constant before any folding.
5088 The real work of conversion is done by `convert'.
5089 The purpose of this function is to generate error messages
5090 for assignments that are not allowed in C.
5091 ERRTYPE says whether it is argument passing, assignment,
5092 initialization or return.
5094 LOCATION is the location of the RHS.
5095 FUNCTION is a tree for the function being called.
5096 PARMNUM is the number of the argument, for printing in error messages. */
5098 static tree
5099 convert_for_assignment (location_t location, tree type, tree rhs,
5100 tree origtype, enum impl_conv errtype,
5101 bool null_pointer_constant, tree fundecl,
5102 tree function, int parmnum)
5104 enum tree_code codel = TREE_CODE (type);
5105 tree orig_rhs = rhs;
5106 tree rhstype;
5107 enum tree_code coder;
5108 tree rname = NULL_TREE;
5109 bool objc_ok = false;
5111 if (errtype == ic_argpass)
5113 tree selector;
5114 /* Change pointer to function to the function itself for
5115 diagnostics. */
5116 if (TREE_CODE (function) == ADDR_EXPR
5117 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5118 function = TREE_OPERAND (function, 0);
5120 /* Handle an ObjC selector specially for diagnostics. */
5121 selector = objc_message_selector ();
5122 rname = function;
5123 if (selector && parmnum > 2)
5125 rname = selector;
5126 parmnum -= 2;
5130 /* This macro is used to emit diagnostics to ensure that all format
5131 strings are complete sentences, visible to gettext and checked at
5132 compile time. */
5133 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5134 do { \
5135 switch (errtype) \
5137 case ic_argpass: \
5138 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5139 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5140 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5141 "expected %qT but argument is of type %qT", \
5142 type, rhstype); \
5143 break; \
5144 case ic_assign: \
5145 pedwarn (LOCATION, OPT, AS); \
5146 break; \
5147 case ic_init: \
5148 pedwarn_init (LOCATION, OPT, IN); \
5149 break; \
5150 case ic_return: \
5151 pedwarn (LOCATION, OPT, RE); \
5152 break; \
5153 default: \
5154 gcc_unreachable (); \
5156 } while (0)
5158 /* This macro is used to emit diagnostics to ensure that all format
5159 strings are complete sentences, visible to gettext and checked at
5160 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5161 extra parameter to enumerate qualifiers. */
5163 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5164 do { \
5165 switch (errtype) \
5167 case ic_argpass: \
5168 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5169 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5170 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5171 "expected %qT but argument is of type %qT", \
5172 type, rhstype); \
5173 break; \
5174 case ic_assign: \
5175 pedwarn (LOCATION, OPT, AS, QUALS); \
5176 break; \
5177 case ic_init: \
5178 pedwarn (LOCATION, OPT, IN, QUALS); \
5179 break; \
5180 case ic_return: \
5181 pedwarn (LOCATION, OPT, RE, QUALS); \
5182 break; \
5183 default: \
5184 gcc_unreachable (); \
5186 } while (0)
5188 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5189 rhs = TREE_OPERAND (rhs, 0);
5191 rhstype = TREE_TYPE (rhs);
5192 coder = TREE_CODE (rhstype);
5194 if (coder == ERROR_MARK)
5195 return error_mark_node;
5197 if (c_dialect_objc ())
5199 int parmno;
5201 switch (errtype)
5203 case ic_return:
5204 parmno = 0;
5205 break;
5207 case ic_assign:
5208 parmno = -1;
5209 break;
5211 case ic_init:
5212 parmno = -2;
5213 break;
5215 default:
5216 parmno = parmnum;
5217 break;
5220 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5223 if (warn_cxx_compat)
5225 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5226 if (checktype != error_mark_node
5227 && TREE_CODE (type) == ENUMERAL_TYPE
5228 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5230 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5231 G_("enum conversion when passing argument "
5232 "%d of %qE is invalid in C++"),
5233 G_("enum conversion in assignment is "
5234 "invalid in C++"),
5235 G_("enum conversion in initialization is "
5236 "invalid in C++"),
5237 G_("enum conversion in return is "
5238 "invalid in C++"));
5242 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5243 return rhs;
5245 if (coder == VOID_TYPE)
5247 /* Except for passing an argument to an unprototyped function,
5248 this is a constraint violation. When passing an argument to
5249 an unprototyped function, it is compile-time undefined;
5250 making it a constraint in that case was rejected in
5251 DR#252. */
5252 error_at (location, "void value not ignored as it ought to be");
5253 return error_mark_node;
5255 rhs = require_complete_type (rhs);
5256 if (rhs == error_mark_node)
5257 return error_mark_node;
5258 /* A type converts to a reference to it.
5259 This code doesn't fully support references, it's just for the
5260 special case of va_start and va_copy. */
5261 if (codel == REFERENCE_TYPE
5262 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5264 if (!lvalue_p (rhs))
5266 error_at (location, "cannot pass rvalue to reference parameter");
5267 return error_mark_node;
5269 if (!c_mark_addressable (rhs))
5270 return error_mark_node;
5271 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5272 SET_EXPR_LOCATION (rhs, location);
5274 /* We already know that these two types are compatible, but they
5275 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5276 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5277 likely to be va_list, a typedef to __builtin_va_list, which
5278 is different enough that it will cause problems later. */
5279 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5281 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5282 SET_EXPR_LOCATION (rhs, location);
5285 rhs = build1 (NOP_EXPR, type, rhs);
5286 SET_EXPR_LOCATION (rhs, location);
5287 return rhs;
5289 /* Some types can interconvert without explicit casts. */
5290 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5291 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5292 return convert (type, rhs);
5293 /* Arithmetic types all interconvert, and enum is treated like int. */
5294 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5295 || codel == FIXED_POINT_TYPE
5296 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5297 || codel == BOOLEAN_TYPE)
5298 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5299 || coder == FIXED_POINT_TYPE
5300 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5301 || coder == BOOLEAN_TYPE))
5303 tree ret;
5304 bool save = in_late_binary_op;
5305 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5306 in_late_binary_op = true;
5307 ret = convert_and_check (type, orig_rhs);
5308 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5309 in_late_binary_op = save;
5310 return ret;
5313 /* Aggregates in different TUs might need conversion. */
5314 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5315 && codel == coder
5316 && comptypes (type, rhstype))
5317 return convert_and_check (type, rhs);
5319 /* Conversion to a transparent union or record from its member types.
5320 This applies only to function arguments. */
5321 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5322 && TYPE_TRANSPARENT_AGGR (type))
5323 && errtype == ic_argpass)
5325 tree memb, marginal_memb = NULL_TREE;
5327 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5329 tree memb_type = TREE_TYPE (memb);
5331 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5332 TYPE_MAIN_VARIANT (rhstype)))
5333 break;
5335 if (TREE_CODE (memb_type) != POINTER_TYPE)
5336 continue;
5338 if (coder == POINTER_TYPE)
5340 tree ttl = TREE_TYPE (memb_type);
5341 tree ttr = TREE_TYPE (rhstype);
5343 /* Any non-function converts to a [const][volatile] void *
5344 and vice versa; otherwise, targets must be the same.
5345 Meanwhile, the lhs target must have all the qualifiers of
5346 the rhs. */
5347 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5348 || comp_target_types (location, memb_type, rhstype))
5350 /* If this type won't generate any warnings, use it. */
5351 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5352 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5353 && TREE_CODE (ttl) == FUNCTION_TYPE)
5354 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5355 == TYPE_QUALS (ttr))
5356 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5357 == TYPE_QUALS (ttl))))
5358 break;
5360 /* Keep looking for a better type, but remember this one. */
5361 if (!marginal_memb)
5362 marginal_memb = memb;
5366 /* Can convert integer zero to any pointer type. */
5367 if (null_pointer_constant)
5369 rhs = null_pointer_node;
5370 break;
5374 if (memb || marginal_memb)
5376 if (!memb)
5378 /* We have only a marginally acceptable member type;
5379 it needs a warning. */
5380 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5381 tree ttr = TREE_TYPE (rhstype);
5383 /* Const and volatile mean something different for function
5384 types, so the usual warnings are not appropriate. */
5385 if (TREE_CODE (ttr) == FUNCTION_TYPE
5386 && TREE_CODE (ttl) == FUNCTION_TYPE)
5388 /* Because const and volatile on functions are
5389 restrictions that say the function will not do
5390 certain things, it is okay to use a const or volatile
5391 function where an ordinary one is wanted, but not
5392 vice-versa. */
5393 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5394 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5395 WARN_FOR_QUALIFIERS (location, 0,
5396 G_("passing argument %d of %qE "
5397 "makes %q#v qualified function "
5398 "pointer from unqualified"),
5399 G_("assignment makes %q#v qualified "
5400 "function pointer from "
5401 "unqualified"),
5402 G_("initialization makes %q#v qualified "
5403 "function pointer from "
5404 "unqualified"),
5405 G_("return makes %q#v qualified function "
5406 "pointer from unqualified"),
5407 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5409 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5410 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5411 WARN_FOR_QUALIFIERS (location, 0,
5412 G_("passing argument %d of %qE discards "
5413 "%qv qualifier from pointer target type"),
5414 G_("assignment discards %qv qualifier "
5415 "from pointer target type"),
5416 G_("initialization discards %qv qualifier "
5417 "from pointer target type"),
5418 G_("return discards %qv qualifier from "
5419 "pointer target type"),
5420 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5422 memb = marginal_memb;
5425 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5426 pedwarn (location, OPT_Wpedantic,
5427 "ISO C prohibits argument conversion to union type");
5429 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5430 return build_constructor_single (type, memb, rhs);
5434 /* Conversions among pointers */
5435 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5436 && (coder == codel))
5438 tree ttl = TREE_TYPE (type);
5439 tree ttr = TREE_TYPE (rhstype);
5440 tree mvl = ttl;
5441 tree mvr = ttr;
5442 bool is_opaque_pointer;
5443 int target_cmp = 0; /* Cache comp_target_types () result. */
5444 addr_space_t asl;
5445 addr_space_t asr;
5447 if (TREE_CODE (mvl) != ARRAY_TYPE)
5448 mvl = TYPE_MAIN_VARIANT (mvl);
5449 if (TREE_CODE (mvr) != ARRAY_TYPE)
5450 mvr = TYPE_MAIN_VARIANT (mvr);
5451 /* Opaque pointers are treated like void pointers. */
5452 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5454 /* The Plan 9 compiler permits a pointer to a struct to be
5455 automatically converted into a pointer to an anonymous field
5456 within the struct. */
5457 if (flag_plan9_extensions
5458 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5459 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5460 && mvl != mvr)
5462 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5463 if (new_rhs != NULL_TREE)
5465 rhs = new_rhs;
5466 rhstype = TREE_TYPE (rhs);
5467 coder = TREE_CODE (rhstype);
5468 ttr = TREE_TYPE (rhstype);
5469 mvr = TYPE_MAIN_VARIANT (ttr);
5473 /* C++ does not allow the implicit conversion void* -> T*. However,
5474 for the purpose of reducing the number of false positives, we
5475 tolerate the special case of
5477 int *p = NULL;
5479 where NULL is typically defined in C to be '(void *) 0'. */
5480 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5481 warning_at (location, OPT_Wc___compat,
5482 "request for implicit conversion "
5483 "from %qT to %qT not permitted in C++", rhstype, type);
5485 /* See if the pointers point to incompatible address spaces. */
5486 asl = TYPE_ADDR_SPACE (ttl);
5487 asr = TYPE_ADDR_SPACE (ttr);
5488 if (!null_pointer_constant_p (rhs)
5489 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5491 switch (errtype)
5493 case ic_argpass:
5494 error_at (location, "passing argument %d of %qE from pointer to "
5495 "non-enclosed address space", parmnum, rname);
5496 break;
5497 case ic_assign:
5498 error_at (location, "assignment from pointer to "
5499 "non-enclosed address space");
5500 break;
5501 case ic_init:
5502 error_at (location, "initialization from pointer to "
5503 "non-enclosed address space");
5504 break;
5505 case ic_return:
5506 error_at (location, "return from pointer to "
5507 "non-enclosed address space");
5508 break;
5509 default:
5510 gcc_unreachable ();
5512 return error_mark_node;
5515 /* Check if the right-hand side has a format attribute but the
5516 left-hand side doesn't. */
5517 if (warn_suggest_attribute_format
5518 && check_missing_format_attribute (type, rhstype))
5520 switch (errtype)
5522 case ic_argpass:
5523 warning_at (location, OPT_Wsuggest_attribute_format,
5524 "argument %d of %qE might be "
5525 "a candidate for a format attribute",
5526 parmnum, rname);
5527 break;
5528 case ic_assign:
5529 warning_at (location, OPT_Wsuggest_attribute_format,
5530 "assignment left-hand side might be "
5531 "a candidate for a format attribute");
5532 break;
5533 case ic_init:
5534 warning_at (location, OPT_Wsuggest_attribute_format,
5535 "initialization left-hand side might be "
5536 "a candidate for a format attribute");
5537 break;
5538 case ic_return:
5539 warning_at (location, OPT_Wsuggest_attribute_format,
5540 "return type might be "
5541 "a candidate for a format attribute");
5542 break;
5543 default:
5544 gcc_unreachable ();
5548 /* Any non-function converts to a [const][volatile] void *
5549 and vice versa; otherwise, targets must be the same.
5550 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5551 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5552 || (target_cmp = comp_target_types (location, type, rhstype))
5553 || is_opaque_pointer
5554 || (c_common_unsigned_type (mvl)
5555 == c_common_unsigned_type (mvr)))
5557 if (pedantic
5558 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5560 (VOID_TYPE_P (ttr)
5561 && !null_pointer_constant
5562 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5563 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
5564 G_("ISO C forbids passing argument %d of "
5565 "%qE between function pointer "
5566 "and %<void *%>"),
5567 G_("ISO C forbids assignment between "
5568 "function pointer and %<void *%>"),
5569 G_("ISO C forbids initialization between "
5570 "function pointer and %<void *%>"),
5571 G_("ISO C forbids return between function "
5572 "pointer and %<void *%>"));
5573 /* Const and volatile mean something different for function types,
5574 so the usual warnings are not appropriate. */
5575 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5576 && TREE_CODE (ttl) != FUNCTION_TYPE)
5578 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5579 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5581 WARN_FOR_QUALIFIERS (location, 0,
5582 G_("passing argument %d of %qE discards "
5583 "%qv qualifier from pointer target type"),
5584 G_("assignment discards %qv qualifier "
5585 "from pointer target type"),
5586 G_("initialization discards %qv qualifier "
5587 "from pointer target type"),
5588 G_("return discards %qv qualifier from "
5589 "pointer target type"),
5590 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5592 /* If this is not a case of ignoring a mismatch in signedness,
5593 no warning. */
5594 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5595 || target_cmp)
5597 /* If there is a mismatch, do warn. */
5598 else if (warn_pointer_sign)
5599 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5600 G_("pointer targets in passing argument "
5601 "%d of %qE differ in signedness"),
5602 G_("pointer targets in assignment "
5603 "differ in signedness"),
5604 G_("pointer targets in initialization "
5605 "differ in signedness"),
5606 G_("pointer targets in return differ "
5607 "in signedness"));
5609 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5610 && TREE_CODE (ttr) == FUNCTION_TYPE)
5612 /* Because const and volatile on functions are restrictions
5613 that say the function will not do certain things,
5614 it is okay to use a const or volatile function
5615 where an ordinary one is wanted, but not vice-versa. */
5616 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5617 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5618 WARN_FOR_QUALIFIERS (location, 0,
5619 G_("passing argument %d of %qE makes "
5620 "%q#v qualified function pointer "
5621 "from unqualified"),
5622 G_("assignment makes %q#v qualified function "
5623 "pointer from unqualified"),
5624 G_("initialization makes %q#v qualified "
5625 "function pointer from unqualified"),
5626 G_("return makes %q#v qualified function "
5627 "pointer from unqualified"),
5628 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5631 else
5632 /* Avoid warning about the volatile ObjC EH puts on decls. */
5633 if (!objc_ok)
5634 WARN_FOR_ASSIGNMENT (location, 0,
5635 G_("passing argument %d of %qE from "
5636 "incompatible pointer type"),
5637 G_("assignment from incompatible pointer type"),
5638 G_("initialization from incompatible "
5639 "pointer type"),
5640 G_("return from incompatible pointer type"));
5642 return convert (type, rhs);
5644 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5646 /* ??? This should not be an error when inlining calls to
5647 unprototyped functions. */
5648 error_at (location, "invalid use of non-lvalue array");
5649 return error_mark_node;
5651 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5653 /* An explicit constant 0 can convert to a pointer,
5654 or one that results from arithmetic, even including
5655 a cast to integer type. */
5656 if (!null_pointer_constant)
5657 WARN_FOR_ASSIGNMENT (location, 0,
5658 G_("passing argument %d of %qE makes "
5659 "pointer from integer without a cast"),
5660 G_("assignment makes pointer from integer "
5661 "without a cast"),
5662 G_("initialization makes pointer from "
5663 "integer without a cast"),
5664 G_("return makes pointer from integer "
5665 "without a cast"));
5667 return convert (type, rhs);
5669 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5671 WARN_FOR_ASSIGNMENT (location, 0,
5672 G_("passing argument %d of %qE makes integer "
5673 "from pointer without a cast"),
5674 G_("assignment makes integer from pointer "
5675 "without a cast"),
5676 G_("initialization makes integer from pointer "
5677 "without a cast"),
5678 G_("return makes integer from pointer "
5679 "without a cast"));
5680 return convert (type, rhs);
5682 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5684 tree ret;
5685 bool save = in_late_binary_op;
5686 in_late_binary_op = true;
5687 ret = convert (type, rhs);
5688 in_late_binary_op = save;
5689 return ret;
5692 switch (errtype)
5694 case ic_argpass:
5695 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5696 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5697 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5698 "expected %qT but argument is of type %qT", type, rhstype);
5699 break;
5700 case ic_assign:
5701 error_at (location, "incompatible types when assigning to type %qT from "
5702 "type %qT", type, rhstype);
5703 break;
5704 case ic_init:
5705 error_at (location,
5706 "incompatible types when initializing type %qT using type %qT",
5707 type, rhstype);
5708 break;
5709 case ic_return:
5710 error_at (location,
5711 "incompatible types when returning type %qT but %qT was "
5712 "expected", rhstype, type);
5713 break;
5714 default:
5715 gcc_unreachable ();
5718 return error_mark_node;
5721 /* If VALUE is a compound expr all of whose expressions are constant, then
5722 return its value. Otherwise, return error_mark_node.
5724 This is for handling COMPOUND_EXPRs as initializer elements
5725 which is allowed with a warning when -pedantic is specified. */
5727 static tree
5728 valid_compound_expr_initializer (tree value, tree endtype)
5730 if (TREE_CODE (value) == COMPOUND_EXPR)
5732 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5733 == error_mark_node)
5734 return error_mark_node;
5735 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5736 endtype);
5738 else if (!initializer_constant_valid_p (value, endtype))
5739 return error_mark_node;
5740 else
5741 return value;
5744 /* Perform appropriate conversions on the initial value of a variable,
5745 store it in the declaration DECL,
5746 and print any error messages that are appropriate.
5747 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5748 If the init is invalid, store an ERROR_MARK.
5750 INIT_LOC is the location of the initial value. */
5752 void
5753 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5755 tree value, type;
5756 bool npc = false;
5758 /* If variable's type was invalidly declared, just ignore it. */
5760 type = TREE_TYPE (decl);
5761 if (TREE_CODE (type) == ERROR_MARK)
5762 return;
5764 /* Digest the specified initializer into an expression. */
5766 if (init)
5767 npc = null_pointer_constant_p (init);
5768 value = digest_init (init_loc, type, init, origtype, npc,
5769 true, TREE_STATIC (decl));
5771 /* Store the expression if valid; else report error. */
5773 if (!in_system_header
5774 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5775 warning (OPT_Wtraditional, "traditional C rejects automatic "
5776 "aggregate initialization");
5778 DECL_INITIAL (decl) = value;
5780 /* ANSI wants warnings about out-of-range constant initializers. */
5781 STRIP_TYPE_NOPS (value);
5782 if (TREE_STATIC (decl))
5783 constant_expression_warning (value);
5785 /* Check if we need to set array size from compound literal size. */
5786 if (TREE_CODE (type) == ARRAY_TYPE
5787 && TYPE_DOMAIN (type) == 0
5788 && value != error_mark_node)
5790 tree inside_init = init;
5792 STRIP_TYPE_NOPS (inside_init);
5793 inside_init = fold (inside_init);
5795 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5797 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5799 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5801 /* For int foo[] = (int [3]){1}; we need to set array size
5802 now since later on array initializer will be just the
5803 brace enclosed list of the compound literal. */
5804 tree etype = strip_array_types (TREE_TYPE (decl));
5805 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5806 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5807 layout_type (type);
5808 layout_decl (cldecl, 0);
5809 TREE_TYPE (decl)
5810 = c_build_qualified_type (type, TYPE_QUALS (etype));
5816 /* Methods for storing and printing names for error messages. */
5818 /* Implement a spelling stack that allows components of a name to be pushed
5819 and popped. Each element on the stack is this structure. */
5821 struct spelling
5823 int kind;
5824 union
5826 unsigned HOST_WIDE_INT i;
5827 const char *s;
5828 } u;
5831 #define SPELLING_STRING 1
5832 #define SPELLING_MEMBER 2
5833 #define SPELLING_BOUNDS 3
5835 static struct spelling *spelling; /* Next stack element (unused). */
5836 static struct spelling *spelling_base; /* Spelling stack base. */
5837 static int spelling_size; /* Size of the spelling stack. */
5839 /* Macros to save and restore the spelling stack around push_... functions.
5840 Alternative to SAVE_SPELLING_STACK. */
5842 #define SPELLING_DEPTH() (spelling - spelling_base)
5843 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5845 /* Push an element on the spelling stack with type KIND and assign VALUE
5846 to MEMBER. */
5848 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5850 int depth = SPELLING_DEPTH (); \
5852 if (depth >= spelling_size) \
5854 spelling_size += 10; \
5855 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5856 spelling_size); \
5857 RESTORE_SPELLING_DEPTH (depth); \
5860 spelling->kind = (KIND); \
5861 spelling->MEMBER = (VALUE); \
5862 spelling++; \
5865 /* Push STRING on the stack. Printed literally. */
5867 static void
5868 push_string (const char *string)
5870 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5873 /* Push a member name on the stack. Printed as '.' STRING. */
5875 static void
5876 push_member_name (tree decl)
5878 const char *const string
5879 = (DECL_NAME (decl)
5880 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5881 : _("<anonymous>"));
5882 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5885 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5887 static void
5888 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5890 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5893 /* Compute the maximum size in bytes of the printed spelling. */
5895 static int
5896 spelling_length (void)
5898 int size = 0;
5899 struct spelling *p;
5901 for (p = spelling_base; p < spelling; p++)
5903 if (p->kind == SPELLING_BOUNDS)
5904 size += 25;
5905 else
5906 size += strlen (p->u.s) + 1;
5909 return size;
5912 /* Print the spelling to BUFFER and return it. */
5914 static char *
5915 print_spelling (char *buffer)
5917 char *d = buffer;
5918 struct spelling *p;
5920 for (p = spelling_base; p < spelling; p++)
5921 if (p->kind == SPELLING_BOUNDS)
5923 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5924 d += strlen (d);
5926 else
5928 const char *s;
5929 if (p->kind == SPELLING_MEMBER)
5930 *d++ = '.';
5931 for (s = p->u.s; (*d = *s++); d++)
5934 *d++ = '\0';
5935 return buffer;
5938 /* Issue an error message for a bad initializer component.
5939 GMSGID identifies the message.
5940 The component name is taken from the spelling stack. */
5942 void
5943 error_init (const char *gmsgid)
5945 char *ofwhat;
5947 /* The gmsgid may be a format string with %< and %>. */
5948 error (gmsgid);
5949 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5950 if (*ofwhat)
5951 error ("(near initialization for %qs)", ofwhat);
5954 /* Issue a pedantic warning for a bad initializer component. OPT is
5955 the option OPT_* (from options.h) controlling this warning or 0 if
5956 it is unconditionally given. GMSGID identifies the message. The
5957 component name is taken from the spelling stack. */
5959 void
5960 pedwarn_init (location_t location, int opt, const char *gmsgid)
5962 char *ofwhat;
5964 /* The gmsgid may be a format string with %< and %>. */
5965 pedwarn (location, opt, gmsgid);
5966 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5967 if (*ofwhat)
5968 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5971 /* Issue a warning for a bad initializer component.
5973 OPT is the OPT_W* value corresponding to the warning option that
5974 controls this warning. GMSGID identifies the message. The
5975 component name is taken from the spelling stack. */
5977 static void
5978 warning_init (int opt, const char *gmsgid)
5980 char *ofwhat;
5982 /* The gmsgid may be a format string with %< and %>. */
5983 warning (opt, gmsgid);
5984 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5985 if (*ofwhat)
5986 warning (opt, "(near initialization for %qs)", ofwhat);
5989 /* If TYPE is an array type and EXPR is a parenthesized string
5990 constant, warn if pedantic that EXPR is being used to initialize an
5991 object of type TYPE. */
5993 void
5994 maybe_warn_string_init (tree type, struct c_expr expr)
5996 if (pedantic
5997 && TREE_CODE (type) == ARRAY_TYPE
5998 && TREE_CODE (expr.value) == STRING_CST
5999 && expr.original_code != STRING_CST)
6000 pedwarn_init (input_location, OPT_Wpedantic,
6001 "array initialized from parenthesized string constant");
6004 /* Digest the parser output INIT as an initializer for type TYPE.
6005 Return a C expression of type TYPE to represent the initial value.
6007 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6009 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6011 If INIT is a string constant, STRICT_STRING is true if it is
6012 unparenthesized or we should not warn here for it being parenthesized.
6013 For other types of INIT, STRICT_STRING is not used.
6015 INIT_LOC is the location of the INIT.
6017 REQUIRE_CONSTANT requests an error if non-constant initializers or
6018 elements are seen. */
6020 static tree
6021 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6022 bool null_pointer_constant, bool strict_string,
6023 int require_constant)
6025 enum tree_code code = TREE_CODE (type);
6026 tree inside_init = init;
6027 tree semantic_type = NULL_TREE;
6028 bool maybe_const = true;
6030 if (type == error_mark_node
6031 || !init
6032 || init == error_mark_node
6033 || TREE_TYPE (init) == error_mark_node)
6034 return error_mark_node;
6036 STRIP_TYPE_NOPS (inside_init);
6038 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6040 semantic_type = TREE_TYPE (inside_init);
6041 inside_init = TREE_OPERAND (inside_init, 0);
6043 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6044 inside_init = decl_constant_value_for_optimization (inside_init);
6046 /* Initialization of an array of chars from a string constant
6047 optionally enclosed in braces. */
6049 if (code == ARRAY_TYPE && inside_init
6050 && TREE_CODE (inside_init) == STRING_CST)
6052 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6053 /* Note that an array could be both an array of character type
6054 and an array of wchar_t if wchar_t is signed char or unsigned
6055 char. */
6056 bool char_array = (typ1 == char_type_node
6057 || typ1 == signed_char_type_node
6058 || typ1 == unsigned_char_type_node);
6059 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6060 bool char16_array = !!comptypes (typ1, char16_type_node);
6061 bool char32_array = !!comptypes (typ1, char32_type_node);
6063 if (char_array || wchar_array || char16_array || char32_array)
6065 struct c_expr expr;
6066 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6067 expr.value = inside_init;
6068 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6069 expr.original_type = NULL;
6070 maybe_warn_string_init (type, expr);
6072 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6073 pedwarn_init (init_loc, OPT_Wpedantic,
6074 "initialization of a flexible array member");
6076 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6077 TYPE_MAIN_VARIANT (type)))
6078 return inside_init;
6080 if (char_array)
6082 if (typ2 != char_type_node)
6084 error_init ("char-array initialized from wide string");
6085 return error_mark_node;
6088 else
6090 if (typ2 == char_type_node)
6092 error_init ("wide character array initialized from non-wide "
6093 "string");
6094 return error_mark_node;
6096 else if (!comptypes(typ1, typ2))
6098 error_init ("wide character array initialized from "
6099 "incompatible wide string");
6100 return error_mark_node;
6104 TREE_TYPE (inside_init) = type;
6105 if (TYPE_DOMAIN (type) != 0
6106 && TYPE_SIZE (type) != 0
6107 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6109 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6111 /* Subtract the size of a single (possibly wide) character
6112 because it's ok to ignore the terminating null char
6113 that is counted in the length of the constant. */
6114 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6115 (len
6116 - (TYPE_PRECISION (typ1)
6117 / BITS_PER_UNIT))))
6118 pedwarn_init (init_loc, 0,
6119 ("initializer-string for array of chars "
6120 "is too long"));
6121 else if (warn_cxx_compat
6122 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6123 warning_at (init_loc, OPT_Wc___compat,
6124 ("initializer-string for array chars "
6125 "is too long for C++"));
6128 return inside_init;
6130 else if (INTEGRAL_TYPE_P (typ1))
6132 error_init ("array of inappropriate type initialized "
6133 "from string constant");
6134 return error_mark_node;
6138 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6139 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6140 below and handle as a constructor. */
6141 if (code == VECTOR_TYPE
6142 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6143 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6144 && TREE_CONSTANT (inside_init))
6146 if (TREE_CODE (inside_init) == VECTOR_CST
6147 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6148 TYPE_MAIN_VARIANT (type)))
6149 return inside_init;
6151 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6153 unsigned HOST_WIDE_INT ix;
6154 tree value;
6155 bool constant_p = true;
6157 /* Iterate through elements and check if all constructor
6158 elements are *_CSTs. */
6159 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6160 if (!CONSTANT_CLASS_P (value))
6162 constant_p = false;
6163 break;
6166 if (constant_p)
6167 return build_vector_from_ctor (type,
6168 CONSTRUCTOR_ELTS (inside_init));
6172 if (warn_sequence_point)
6173 verify_sequence_points (inside_init);
6175 /* Any type can be initialized
6176 from an expression of the same type, optionally with braces. */
6178 if (inside_init && TREE_TYPE (inside_init) != 0
6179 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6180 TYPE_MAIN_VARIANT (type))
6181 || (code == ARRAY_TYPE
6182 && comptypes (TREE_TYPE (inside_init), type))
6183 || (code == VECTOR_TYPE
6184 && comptypes (TREE_TYPE (inside_init), type))
6185 || (code == POINTER_TYPE
6186 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6187 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6188 TREE_TYPE (type)))))
6190 if (code == POINTER_TYPE)
6192 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6194 if (TREE_CODE (inside_init) == STRING_CST
6195 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6196 inside_init = array_to_pointer_conversion
6197 (init_loc, inside_init);
6198 else
6200 error_init ("invalid use of non-lvalue array");
6201 return error_mark_node;
6206 if (code == VECTOR_TYPE)
6207 /* Although the types are compatible, we may require a
6208 conversion. */
6209 inside_init = convert (type, inside_init);
6211 if (require_constant
6212 && (code == VECTOR_TYPE || !flag_isoc99)
6213 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6215 /* As an extension, allow initializing objects with static storage
6216 duration with compound literals (which are then treated just as
6217 the brace enclosed list they contain). Also allow this for
6218 vectors, as we can only assign them with compound literals. */
6219 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6220 inside_init = DECL_INITIAL (decl);
6223 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6224 && TREE_CODE (inside_init) != CONSTRUCTOR)
6226 error_init ("array initialized from non-constant array expression");
6227 return error_mark_node;
6230 /* Compound expressions can only occur here if -Wpedantic or
6231 -pedantic-errors is specified. In the later case, we always want
6232 an error. In the former case, we simply want a warning. */
6233 if (require_constant && pedantic
6234 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6236 inside_init
6237 = valid_compound_expr_initializer (inside_init,
6238 TREE_TYPE (inside_init));
6239 if (inside_init == error_mark_node)
6240 error_init ("initializer element is not constant");
6241 else
6242 pedwarn_init (init_loc, OPT_Wpedantic,
6243 "initializer element is not constant");
6244 if (flag_pedantic_errors)
6245 inside_init = error_mark_node;
6247 else if (require_constant
6248 && !initializer_constant_valid_p (inside_init,
6249 TREE_TYPE (inside_init)))
6251 error_init ("initializer element is not constant");
6252 inside_init = error_mark_node;
6254 else if (require_constant && !maybe_const)
6255 pedwarn_init (init_loc, 0,
6256 "initializer element is not a constant expression");
6258 /* Added to enable additional -Wsuggest-attribute=format warnings. */
6259 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6260 inside_init = convert_for_assignment (init_loc, type, inside_init,
6261 origtype,
6262 ic_init, null_pointer_constant,
6263 NULL_TREE, NULL_TREE, 0);
6264 return inside_init;
6267 /* Handle scalar types, including conversions. */
6269 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6270 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6271 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6273 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6274 && (TREE_CODE (init) == STRING_CST
6275 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6276 inside_init = init = array_to_pointer_conversion (init_loc, init);
6277 if (semantic_type)
6278 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6279 inside_init);
6280 inside_init
6281 = convert_for_assignment (init_loc, type, inside_init, origtype,
6282 ic_init, null_pointer_constant,
6283 NULL_TREE, NULL_TREE, 0);
6285 /* Check to see if we have already given an error message. */
6286 if (inside_init == error_mark_node)
6288 else if (require_constant && !TREE_CONSTANT (inside_init))
6290 error_init ("initializer element is not constant");
6291 inside_init = error_mark_node;
6293 else if (require_constant
6294 && !initializer_constant_valid_p (inside_init,
6295 TREE_TYPE (inside_init)))
6297 error_init ("initializer element is not computable at load time");
6298 inside_init = error_mark_node;
6300 else if (require_constant && !maybe_const)
6301 pedwarn_init (init_loc, 0,
6302 "initializer element is not a constant expression");
6304 return inside_init;
6307 /* Come here only for records and arrays. */
6309 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6311 error_init ("variable-sized object may not be initialized");
6312 return error_mark_node;
6315 error_init ("invalid initializer");
6316 return error_mark_node;
6319 /* Handle initializers that use braces. */
6321 /* Type of object we are accumulating a constructor for.
6322 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6323 static tree constructor_type;
6325 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6326 left to fill. */
6327 static tree constructor_fields;
6329 /* For an ARRAY_TYPE, this is the specified index
6330 at which to store the next element we get. */
6331 static tree constructor_index;
6333 /* For an ARRAY_TYPE, this is the maximum index. */
6334 static tree constructor_max_index;
6336 /* For a RECORD_TYPE, this is the first field not yet written out. */
6337 static tree constructor_unfilled_fields;
6339 /* For an ARRAY_TYPE, this is the index of the first element
6340 not yet written out. */
6341 static tree constructor_unfilled_index;
6343 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6344 This is so we can generate gaps between fields, when appropriate. */
6345 static tree constructor_bit_index;
6347 /* If we are saving up the elements rather than allocating them,
6348 this is the list of elements so far (in reverse order,
6349 most recent first). */
6350 static VEC(constructor_elt,gc) *constructor_elements;
6352 /* 1 if constructor should be incrementally stored into a constructor chain,
6353 0 if all the elements should be kept in AVL tree. */
6354 static int constructor_incremental;
6356 /* 1 if so far this constructor's elements are all compile-time constants. */
6357 static int constructor_constant;
6359 /* 1 if so far this constructor's elements are all valid address constants. */
6360 static int constructor_simple;
6362 /* 1 if this constructor has an element that cannot be part of a
6363 constant expression. */
6364 static int constructor_nonconst;
6366 /* 1 if this constructor is erroneous so far. */
6367 static int constructor_erroneous;
6369 /* Structure for managing pending initializer elements, organized as an
6370 AVL tree. */
6372 struct init_node
6374 struct init_node *left, *right;
6375 struct init_node *parent;
6376 int balance;
6377 tree purpose;
6378 tree value;
6379 tree origtype;
6382 /* Tree of pending elements at this constructor level.
6383 These are elements encountered out of order
6384 which belong at places we haven't reached yet in actually
6385 writing the output.
6386 Will never hold tree nodes across GC runs. */
6387 static struct init_node *constructor_pending_elts;
6389 /* The SPELLING_DEPTH of this constructor. */
6390 static int constructor_depth;
6392 /* DECL node for which an initializer is being read.
6393 0 means we are reading a constructor expression
6394 such as (struct foo) {...}. */
6395 static tree constructor_decl;
6397 /* Nonzero if this is an initializer for a top-level decl. */
6398 static int constructor_top_level;
6400 /* Nonzero if there were any member designators in this initializer. */
6401 static int constructor_designated;
6403 /* Nesting depth of designator list. */
6404 static int designator_depth;
6406 /* Nonzero if there were diagnosed errors in this designator list. */
6407 static int designator_erroneous;
6410 /* This stack has a level for each implicit or explicit level of
6411 structuring in the initializer, including the outermost one. It
6412 saves the values of most of the variables above. */
6414 struct constructor_range_stack;
6416 struct constructor_stack
6418 struct constructor_stack *next;
6419 tree type;
6420 tree fields;
6421 tree index;
6422 tree max_index;
6423 tree unfilled_index;
6424 tree unfilled_fields;
6425 tree bit_index;
6426 VEC(constructor_elt,gc) *elements;
6427 struct init_node *pending_elts;
6428 int offset;
6429 int depth;
6430 /* If value nonzero, this value should replace the entire
6431 constructor at this level. */
6432 struct c_expr replacement_value;
6433 struct constructor_range_stack *range_stack;
6434 char constant;
6435 char simple;
6436 char nonconst;
6437 char implicit;
6438 char erroneous;
6439 char outer;
6440 char incremental;
6441 char designated;
6444 static struct constructor_stack *constructor_stack;
6446 /* This stack represents designators from some range designator up to
6447 the last designator in the list. */
6449 struct constructor_range_stack
6451 struct constructor_range_stack *next, *prev;
6452 struct constructor_stack *stack;
6453 tree range_start;
6454 tree index;
6455 tree range_end;
6456 tree fields;
6459 static struct constructor_range_stack *constructor_range_stack;
6461 /* This stack records separate initializers that are nested.
6462 Nested initializers can't happen in ANSI C, but GNU C allows them
6463 in cases like { ... (struct foo) { ... } ... }. */
6465 struct initializer_stack
6467 struct initializer_stack *next;
6468 tree decl;
6469 struct constructor_stack *constructor_stack;
6470 struct constructor_range_stack *constructor_range_stack;
6471 VEC(constructor_elt,gc) *elements;
6472 struct spelling *spelling;
6473 struct spelling *spelling_base;
6474 int spelling_size;
6475 char top_level;
6476 char require_constant_value;
6477 char require_constant_elements;
6480 static struct initializer_stack *initializer_stack;
6482 /* Prepare to parse and output the initializer for variable DECL. */
6484 void
6485 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6487 const char *locus;
6488 struct initializer_stack *p = XNEW (struct initializer_stack);
6490 p->decl = constructor_decl;
6491 p->require_constant_value = require_constant_value;
6492 p->require_constant_elements = require_constant_elements;
6493 p->constructor_stack = constructor_stack;
6494 p->constructor_range_stack = constructor_range_stack;
6495 p->elements = constructor_elements;
6496 p->spelling = spelling;
6497 p->spelling_base = spelling_base;
6498 p->spelling_size = spelling_size;
6499 p->top_level = constructor_top_level;
6500 p->next = initializer_stack;
6501 initializer_stack = p;
6503 constructor_decl = decl;
6504 constructor_designated = 0;
6505 constructor_top_level = top_level;
6507 if (decl != 0 && decl != error_mark_node)
6509 require_constant_value = TREE_STATIC (decl);
6510 require_constant_elements
6511 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6512 /* For a scalar, you can always use any value to initialize,
6513 even within braces. */
6514 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6515 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6516 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6517 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6518 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6520 else
6522 require_constant_value = 0;
6523 require_constant_elements = 0;
6524 locus = _("(anonymous)");
6527 constructor_stack = 0;
6528 constructor_range_stack = 0;
6530 missing_braces_mentioned = 0;
6532 spelling_base = 0;
6533 spelling_size = 0;
6534 RESTORE_SPELLING_DEPTH (0);
6536 if (locus)
6537 push_string (locus);
6540 void
6541 finish_init (void)
6543 struct initializer_stack *p = initializer_stack;
6545 /* Free the whole constructor stack of this initializer. */
6546 while (constructor_stack)
6548 struct constructor_stack *q = constructor_stack;
6549 constructor_stack = q->next;
6550 free (q);
6553 gcc_assert (!constructor_range_stack);
6555 /* Pop back to the data of the outer initializer (if any). */
6556 free (spelling_base);
6558 constructor_decl = p->decl;
6559 require_constant_value = p->require_constant_value;
6560 require_constant_elements = p->require_constant_elements;
6561 constructor_stack = p->constructor_stack;
6562 constructor_range_stack = p->constructor_range_stack;
6563 constructor_elements = p->elements;
6564 spelling = p->spelling;
6565 spelling_base = p->spelling_base;
6566 spelling_size = p->spelling_size;
6567 constructor_top_level = p->top_level;
6568 initializer_stack = p->next;
6569 free (p);
6572 /* Call here when we see the initializer is surrounded by braces.
6573 This is instead of a call to push_init_level;
6574 it is matched by a call to pop_init_level.
6576 TYPE is the type to initialize, for a constructor expression.
6577 For an initializer for a decl, TYPE is zero. */
6579 void
6580 really_start_incremental_init (tree type)
6582 struct constructor_stack *p = XNEW (struct constructor_stack);
6584 if (type == 0)
6585 type = TREE_TYPE (constructor_decl);
6587 if (TREE_CODE (type) == VECTOR_TYPE
6588 && TYPE_VECTOR_OPAQUE (type))
6589 error ("opaque vector types cannot be initialized");
6591 p->type = constructor_type;
6592 p->fields = constructor_fields;
6593 p->index = constructor_index;
6594 p->max_index = constructor_max_index;
6595 p->unfilled_index = constructor_unfilled_index;
6596 p->unfilled_fields = constructor_unfilled_fields;
6597 p->bit_index = constructor_bit_index;
6598 p->elements = constructor_elements;
6599 p->constant = constructor_constant;
6600 p->simple = constructor_simple;
6601 p->nonconst = constructor_nonconst;
6602 p->erroneous = constructor_erroneous;
6603 p->pending_elts = constructor_pending_elts;
6604 p->depth = constructor_depth;
6605 p->replacement_value.value = 0;
6606 p->replacement_value.original_code = ERROR_MARK;
6607 p->replacement_value.original_type = NULL;
6608 p->implicit = 0;
6609 p->range_stack = 0;
6610 p->outer = 0;
6611 p->incremental = constructor_incremental;
6612 p->designated = constructor_designated;
6613 p->next = 0;
6614 constructor_stack = p;
6616 constructor_constant = 1;
6617 constructor_simple = 1;
6618 constructor_nonconst = 0;
6619 constructor_depth = SPELLING_DEPTH ();
6620 constructor_elements = 0;
6621 constructor_pending_elts = 0;
6622 constructor_type = type;
6623 constructor_incremental = 1;
6624 constructor_designated = 0;
6625 designator_depth = 0;
6626 designator_erroneous = 0;
6628 if (TREE_CODE (constructor_type) == RECORD_TYPE
6629 || TREE_CODE (constructor_type) == UNION_TYPE)
6631 constructor_fields = TYPE_FIELDS (constructor_type);
6632 /* Skip any nameless bit fields at the beginning. */
6633 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6634 && DECL_NAME (constructor_fields) == 0)
6635 constructor_fields = DECL_CHAIN (constructor_fields);
6637 constructor_unfilled_fields = constructor_fields;
6638 constructor_bit_index = bitsize_zero_node;
6640 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6642 if (TYPE_DOMAIN (constructor_type))
6644 constructor_max_index
6645 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6647 /* Detect non-empty initializations of zero-length arrays. */
6648 if (constructor_max_index == NULL_TREE
6649 && TYPE_SIZE (constructor_type))
6650 constructor_max_index = integer_minus_one_node;
6652 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6653 to initialize VLAs will cause a proper error; avoid tree
6654 checking errors as well by setting a safe value. */
6655 if (constructor_max_index
6656 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6657 constructor_max_index = integer_minus_one_node;
6659 constructor_index
6660 = convert (bitsizetype,
6661 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6663 else
6665 constructor_index = bitsize_zero_node;
6666 constructor_max_index = NULL_TREE;
6669 constructor_unfilled_index = constructor_index;
6671 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6673 /* Vectors are like simple fixed-size arrays. */
6674 constructor_max_index =
6675 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6676 constructor_index = bitsize_zero_node;
6677 constructor_unfilled_index = constructor_index;
6679 else
6681 /* Handle the case of int x = {5}; */
6682 constructor_fields = constructor_type;
6683 constructor_unfilled_fields = constructor_type;
6687 /* Push down into a subobject, for initialization.
6688 If this is for an explicit set of braces, IMPLICIT is 0.
6689 If it is because the next element belongs at a lower level,
6690 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6692 void
6693 push_init_level (int implicit, struct obstack * braced_init_obstack)
6695 struct constructor_stack *p;
6696 tree value = NULL_TREE;
6698 /* If we've exhausted any levels that didn't have braces,
6699 pop them now. If implicit == 1, this will have been done in
6700 process_init_element; do not repeat it here because in the case
6701 of excess initializers for an empty aggregate this leads to an
6702 infinite cycle of popping a level and immediately recreating
6703 it. */
6704 if (implicit != 1)
6706 while (constructor_stack->implicit)
6708 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6709 || TREE_CODE (constructor_type) == UNION_TYPE)
6710 && constructor_fields == 0)
6711 process_init_element (pop_init_level (1, braced_init_obstack),
6712 true, braced_init_obstack);
6713 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6714 && constructor_max_index
6715 && tree_int_cst_lt (constructor_max_index,
6716 constructor_index))
6717 process_init_element (pop_init_level (1, braced_init_obstack),
6718 true, braced_init_obstack);
6719 else
6720 break;
6724 /* Unless this is an explicit brace, we need to preserve previous
6725 content if any. */
6726 if (implicit)
6728 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6729 || TREE_CODE (constructor_type) == UNION_TYPE)
6730 && constructor_fields)
6731 value = find_init_member (constructor_fields, braced_init_obstack);
6732 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6733 value = find_init_member (constructor_index, braced_init_obstack);
6736 p = XNEW (struct constructor_stack);
6737 p->type = constructor_type;
6738 p->fields = constructor_fields;
6739 p->index = constructor_index;
6740 p->max_index = constructor_max_index;
6741 p->unfilled_index = constructor_unfilled_index;
6742 p->unfilled_fields = constructor_unfilled_fields;
6743 p->bit_index = constructor_bit_index;
6744 p->elements = constructor_elements;
6745 p->constant = constructor_constant;
6746 p->simple = constructor_simple;
6747 p->nonconst = constructor_nonconst;
6748 p->erroneous = constructor_erroneous;
6749 p->pending_elts = constructor_pending_elts;
6750 p->depth = constructor_depth;
6751 p->replacement_value.value = 0;
6752 p->replacement_value.original_code = ERROR_MARK;
6753 p->replacement_value.original_type = NULL;
6754 p->implicit = implicit;
6755 p->outer = 0;
6756 p->incremental = constructor_incremental;
6757 p->designated = constructor_designated;
6758 p->next = constructor_stack;
6759 p->range_stack = 0;
6760 constructor_stack = p;
6762 constructor_constant = 1;
6763 constructor_simple = 1;
6764 constructor_nonconst = 0;
6765 constructor_depth = SPELLING_DEPTH ();
6766 constructor_elements = 0;
6767 constructor_incremental = 1;
6768 constructor_designated = 0;
6769 constructor_pending_elts = 0;
6770 if (!implicit)
6772 p->range_stack = constructor_range_stack;
6773 constructor_range_stack = 0;
6774 designator_depth = 0;
6775 designator_erroneous = 0;
6778 /* Don't die if an entire brace-pair level is superfluous
6779 in the containing level. */
6780 if (constructor_type == 0)
6782 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6783 || TREE_CODE (constructor_type) == UNION_TYPE)
6785 /* Don't die if there are extra init elts at the end. */
6786 if (constructor_fields == 0)
6787 constructor_type = 0;
6788 else
6790 constructor_type = TREE_TYPE (constructor_fields);
6791 push_member_name (constructor_fields);
6792 constructor_depth++;
6795 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6797 constructor_type = TREE_TYPE (constructor_type);
6798 push_array_bounds (tree_low_cst (constructor_index, 1));
6799 constructor_depth++;
6802 if (constructor_type == 0)
6804 error_init ("extra brace group at end of initializer");
6805 constructor_fields = 0;
6806 constructor_unfilled_fields = 0;
6807 return;
6810 if (value && TREE_CODE (value) == CONSTRUCTOR)
6812 constructor_constant = TREE_CONSTANT (value);
6813 constructor_simple = TREE_STATIC (value);
6814 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6815 constructor_elements = CONSTRUCTOR_ELTS (value);
6816 if (!VEC_empty (constructor_elt, constructor_elements)
6817 && (TREE_CODE (constructor_type) == RECORD_TYPE
6818 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6819 set_nonincremental_init (braced_init_obstack);
6822 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6824 missing_braces_mentioned = 1;
6825 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6828 if (TREE_CODE (constructor_type) == RECORD_TYPE
6829 || TREE_CODE (constructor_type) == UNION_TYPE)
6831 constructor_fields = TYPE_FIELDS (constructor_type);
6832 /* Skip any nameless bit fields at the beginning. */
6833 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6834 && DECL_NAME (constructor_fields) == 0)
6835 constructor_fields = DECL_CHAIN (constructor_fields);
6837 constructor_unfilled_fields = constructor_fields;
6838 constructor_bit_index = bitsize_zero_node;
6840 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6842 /* Vectors are like simple fixed-size arrays. */
6843 constructor_max_index =
6844 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6845 constructor_index = bitsize_int (0);
6846 constructor_unfilled_index = constructor_index;
6848 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6850 if (TYPE_DOMAIN (constructor_type))
6852 constructor_max_index
6853 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6855 /* Detect non-empty initializations of zero-length arrays. */
6856 if (constructor_max_index == NULL_TREE
6857 && TYPE_SIZE (constructor_type))
6858 constructor_max_index = integer_minus_one_node;
6860 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6861 to initialize VLAs will cause a proper error; avoid tree
6862 checking errors as well by setting a safe value. */
6863 if (constructor_max_index
6864 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6865 constructor_max_index = integer_minus_one_node;
6867 constructor_index
6868 = convert (bitsizetype,
6869 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6871 else
6872 constructor_index = bitsize_zero_node;
6874 constructor_unfilled_index = constructor_index;
6875 if (value && TREE_CODE (value) == STRING_CST)
6877 /* We need to split the char/wchar array into individual
6878 characters, so that we don't have to special case it
6879 everywhere. */
6880 set_nonincremental_init_from_string (value, braced_init_obstack);
6883 else
6885 if (constructor_type != error_mark_node)
6886 warning_init (0, "braces around scalar initializer");
6887 constructor_fields = constructor_type;
6888 constructor_unfilled_fields = constructor_type;
6892 /* At the end of an implicit or explicit brace level,
6893 finish up that level of constructor. If a single expression
6894 with redundant braces initialized that level, return the
6895 c_expr structure for that expression. Otherwise, the original_code
6896 element is set to ERROR_MARK.
6897 If we were outputting the elements as they are read, return 0 as the value
6898 from inner levels (process_init_element ignores that),
6899 but return error_mark_node as the value from the outermost level
6900 (that's what we want to put in DECL_INITIAL).
6901 Otherwise, return a CONSTRUCTOR expression as the value. */
6903 struct c_expr
6904 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6906 struct constructor_stack *p;
6907 struct c_expr ret;
6908 ret.value = 0;
6909 ret.original_code = ERROR_MARK;
6910 ret.original_type = NULL;
6912 if (implicit == 0)
6914 /* When we come to an explicit close brace,
6915 pop any inner levels that didn't have explicit braces. */
6916 while (constructor_stack->implicit)
6918 process_init_element (pop_init_level (1, braced_init_obstack),
6919 true, braced_init_obstack);
6921 gcc_assert (!constructor_range_stack);
6924 /* Now output all pending elements. */
6925 constructor_incremental = 1;
6926 output_pending_init_elements (1, braced_init_obstack);
6928 p = constructor_stack;
6930 /* Error for initializing a flexible array member, or a zero-length
6931 array member in an inappropriate context. */
6932 if (constructor_type && constructor_fields
6933 && TREE_CODE (constructor_type) == ARRAY_TYPE
6934 && TYPE_DOMAIN (constructor_type)
6935 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6937 /* Silently discard empty initializations. The parser will
6938 already have pedwarned for empty brackets. */
6939 if (integer_zerop (constructor_unfilled_index))
6940 constructor_type = NULL_TREE;
6941 else
6943 gcc_assert (!TYPE_SIZE (constructor_type));
6945 if (constructor_depth > 2)
6946 error_init ("initialization of flexible array member in a nested context");
6947 else
6948 pedwarn_init (input_location, OPT_Wpedantic,
6949 "initialization of a flexible array member");
6951 /* We have already issued an error message for the existence
6952 of a flexible array member not at the end of the structure.
6953 Discard the initializer so that we do not die later. */
6954 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6955 constructor_type = NULL_TREE;
6959 /* Warn when some struct elements are implicitly initialized to zero. */
6960 if (warn_missing_field_initializers
6961 && constructor_type
6962 && TREE_CODE (constructor_type) == RECORD_TYPE
6963 && constructor_unfilled_fields)
6965 bool constructor_zeroinit =
6966 (VEC_length (constructor_elt, constructor_elements) == 1
6967 && integer_zerop
6968 (VEC_index (constructor_elt, constructor_elements, 0).value));
6970 /* Do not warn for flexible array members or zero-length arrays. */
6971 while (constructor_unfilled_fields
6972 && (!DECL_SIZE (constructor_unfilled_fields)
6973 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6974 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
6976 if (constructor_unfilled_fields
6977 /* Do not warn if this level of the initializer uses member
6978 designators; it is likely to be deliberate. */
6979 && !constructor_designated
6980 /* Do not warn about initializing with ` = {0}'. */
6981 && !constructor_zeroinit)
6983 if (warning_at (input_location, OPT_Wmissing_field_initializers,
6984 "missing initializer for field %qD of %qT",
6985 constructor_unfilled_fields,
6986 constructor_type))
6987 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
6988 "%qD declared here", constructor_unfilled_fields);
6992 /* Pad out the end of the structure. */
6993 if (p->replacement_value.value)
6994 /* If this closes a superfluous brace pair,
6995 just pass out the element between them. */
6996 ret = p->replacement_value;
6997 else if (constructor_type == 0)
6999 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7000 && TREE_CODE (constructor_type) != UNION_TYPE
7001 && TREE_CODE (constructor_type) != ARRAY_TYPE
7002 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7004 /* A nonincremental scalar initializer--just return
7005 the element, after verifying there is just one. */
7006 if (VEC_empty (constructor_elt,constructor_elements))
7008 if (!constructor_erroneous)
7009 error_init ("empty scalar initializer");
7010 ret.value = error_mark_node;
7012 else if (VEC_length (constructor_elt,constructor_elements) != 1)
7014 error_init ("extra elements in scalar initializer");
7015 ret.value = VEC_index (constructor_elt,constructor_elements,0).value;
7017 else
7018 ret.value = VEC_index (constructor_elt,constructor_elements,0).value;
7020 else
7022 if (constructor_erroneous)
7023 ret.value = error_mark_node;
7024 else
7026 ret.value = build_constructor (constructor_type,
7027 constructor_elements);
7028 if (constructor_constant)
7029 TREE_CONSTANT (ret.value) = 1;
7030 if (constructor_constant && constructor_simple)
7031 TREE_STATIC (ret.value) = 1;
7032 if (constructor_nonconst)
7033 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7037 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7039 if (constructor_nonconst)
7040 ret.original_code = C_MAYBE_CONST_EXPR;
7041 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7042 ret.original_code = ERROR_MARK;
7045 constructor_type = p->type;
7046 constructor_fields = p->fields;
7047 constructor_index = p->index;
7048 constructor_max_index = p->max_index;
7049 constructor_unfilled_index = p->unfilled_index;
7050 constructor_unfilled_fields = p->unfilled_fields;
7051 constructor_bit_index = p->bit_index;
7052 constructor_elements = p->elements;
7053 constructor_constant = p->constant;
7054 constructor_simple = p->simple;
7055 constructor_nonconst = p->nonconst;
7056 constructor_erroneous = p->erroneous;
7057 constructor_incremental = p->incremental;
7058 constructor_designated = p->designated;
7059 constructor_pending_elts = p->pending_elts;
7060 constructor_depth = p->depth;
7061 if (!p->implicit)
7062 constructor_range_stack = p->range_stack;
7063 RESTORE_SPELLING_DEPTH (constructor_depth);
7065 constructor_stack = p->next;
7066 free (p);
7068 if (ret.value == 0 && constructor_stack == 0)
7069 ret.value = error_mark_node;
7070 return ret;
7073 /* Common handling for both array range and field name designators.
7074 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7076 static int
7077 set_designator (int array, struct obstack * braced_init_obstack)
7079 tree subtype;
7080 enum tree_code subcode;
7082 /* Don't die if an entire brace-pair level is superfluous
7083 in the containing level. */
7084 if (constructor_type == 0)
7085 return 1;
7087 /* If there were errors in this designator list already, bail out
7088 silently. */
7089 if (designator_erroneous)
7090 return 1;
7092 if (!designator_depth)
7094 gcc_assert (!constructor_range_stack);
7096 /* Designator list starts at the level of closest explicit
7097 braces. */
7098 while (constructor_stack->implicit)
7100 process_init_element (pop_init_level (1, braced_init_obstack),
7101 true, braced_init_obstack);
7103 constructor_designated = 1;
7104 return 0;
7107 switch (TREE_CODE (constructor_type))
7109 case RECORD_TYPE:
7110 case UNION_TYPE:
7111 subtype = TREE_TYPE (constructor_fields);
7112 if (subtype != error_mark_node)
7113 subtype = TYPE_MAIN_VARIANT (subtype);
7114 break;
7115 case ARRAY_TYPE:
7116 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7117 break;
7118 default:
7119 gcc_unreachable ();
7122 subcode = TREE_CODE (subtype);
7123 if (array && subcode != ARRAY_TYPE)
7125 error_init ("array index in non-array initializer");
7126 return 1;
7128 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7130 error_init ("field name not in record or union initializer");
7131 return 1;
7134 constructor_designated = 1;
7135 push_init_level (2, braced_init_obstack);
7136 return 0;
7139 /* If there are range designators in designator list, push a new designator
7140 to constructor_range_stack. RANGE_END is end of such stack range or
7141 NULL_TREE if there is no range designator at this level. */
7143 static void
7144 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7146 struct constructor_range_stack *p;
7148 p = (struct constructor_range_stack *)
7149 obstack_alloc (braced_init_obstack,
7150 sizeof (struct constructor_range_stack));
7151 p->prev = constructor_range_stack;
7152 p->next = 0;
7153 p->fields = constructor_fields;
7154 p->range_start = constructor_index;
7155 p->index = constructor_index;
7156 p->stack = constructor_stack;
7157 p->range_end = range_end;
7158 if (constructor_range_stack)
7159 constructor_range_stack->next = p;
7160 constructor_range_stack = p;
7163 /* Within an array initializer, specify the next index to be initialized.
7164 FIRST is that index. If LAST is nonzero, then initialize a range
7165 of indices, running from FIRST through LAST. */
7167 void
7168 set_init_index (tree first, tree last,
7169 struct obstack * braced_init_obstack)
7171 if (set_designator (1, braced_init_obstack))
7172 return;
7174 designator_erroneous = 1;
7176 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7177 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7179 error_init ("array index in initializer not of integer type");
7180 return;
7183 if (TREE_CODE (first) != INTEGER_CST)
7185 first = c_fully_fold (first, false, NULL);
7186 if (TREE_CODE (first) == INTEGER_CST)
7187 pedwarn_init (input_location, OPT_Wpedantic,
7188 "array index in initializer is not "
7189 "an integer constant expression");
7192 if (last && TREE_CODE (last) != INTEGER_CST)
7194 last = c_fully_fold (last, false, NULL);
7195 if (TREE_CODE (last) == INTEGER_CST)
7196 pedwarn_init (input_location, OPT_Wpedantic,
7197 "array index in initializer is not "
7198 "an integer constant expression");
7201 if (TREE_CODE (first) != INTEGER_CST)
7202 error_init ("nonconstant array index in initializer");
7203 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7204 error_init ("nonconstant array index in initializer");
7205 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7206 error_init ("array index in non-array initializer");
7207 else if (tree_int_cst_sgn (first) == -1)
7208 error_init ("array index in initializer exceeds array bounds");
7209 else if (constructor_max_index
7210 && tree_int_cst_lt (constructor_max_index, first))
7211 error_init ("array index in initializer exceeds array bounds");
7212 else
7214 constant_expression_warning (first);
7215 if (last)
7216 constant_expression_warning (last);
7217 constructor_index = convert (bitsizetype, first);
7219 if (last)
7221 if (tree_int_cst_equal (first, last))
7222 last = 0;
7223 else if (tree_int_cst_lt (last, first))
7225 error_init ("empty index range in initializer");
7226 last = 0;
7228 else
7230 last = convert (bitsizetype, last);
7231 if (constructor_max_index != 0
7232 && tree_int_cst_lt (constructor_max_index, last))
7234 error_init ("array index range in initializer exceeds array bounds");
7235 last = 0;
7240 designator_depth++;
7241 designator_erroneous = 0;
7242 if (constructor_range_stack || last)
7243 push_range_stack (last, braced_init_obstack);
7247 /* Within a struct initializer, specify the next field to be initialized. */
7249 void
7250 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7252 tree field;
7254 if (set_designator (0, braced_init_obstack))
7255 return;
7257 designator_erroneous = 1;
7259 if (TREE_CODE (constructor_type) != RECORD_TYPE
7260 && TREE_CODE (constructor_type) != UNION_TYPE)
7262 error_init ("field name not in record or union initializer");
7263 return;
7266 field = lookup_field (constructor_type, fieldname);
7268 if (field == 0)
7269 error ("unknown field %qE specified in initializer", fieldname);
7270 else
7273 constructor_fields = TREE_VALUE (field);
7274 designator_depth++;
7275 designator_erroneous = 0;
7276 if (constructor_range_stack)
7277 push_range_stack (NULL_TREE, braced_init_obstack);
7278 field = TREE_CHAIN (field);
7279 if (field)
7281 if (set_designator (0, braced_init_obstack))
7282 return;
7285 while (field != NULL_TREE);
7288 /* Add a new initializer to the tree of pending initializers. PURPOSE
7289 identifies the initializer, either array index or field in a structure.
7290 VALUE is the value of that index or field. If ORIGTYPE is not
7291 NULL_TREE, it is the original type of VALUE.
7293 IMPLICIT is true if value comes from pop_init_level (1),
7294 the new initializer has been merged with the existing one
7295 and thus no warnings should be emitted about overriding an
7296 existing initializer. */
7298 static void
7299 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7300 struct obstack * braced_init_obstack)
7302 struct init_node *p, **q, *r;
7304 q = &constructor_pending_elts;
7305 p = 0;
7307 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7309 while (*q != 0)
7311 p = *q;
7312 if (tree_int_cst_lt (purpose, p->purpose))
7313 q = &p->left;
7314 else if (tree_int_cst_lt (p->purpose, purpose))
7315 q = &p->right;
7316 else
7318 if (!implicit)
7320 if (TREE_SIDE_EFFECTS (p->value))
7321 warning_init (0, "initialized field with side-effects overwritten");
7322 else if (warn_override_init)
7323 warning_init (OPT_Woverride_init, "initialized field overwritten");
7325 p->value = value;
7326 p->origtype = origtype;
7327 return;
7331 else
7333 tree bitpos;
7335 bitpos = bit_position (purpose);
7336 while (*q != NULL)
7338 p = *q;
7339 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7340 q = &p->left;
7341 else if (p->purpose != purpose)
7342 q = &p->right;
7343 else
7345 if (!implicit)
7347 if (TREE_SIDE_EFFECTS (p->value))
7348 warning_init (0, "initialized field with side-effects overwritten");
7349 else if (warn_override_init)
7350 warning_init (OPT_Woverride_init, "initialized field overwritten");
7352 p->value = value;
7353 p->origtype = origtype;
7354 return;
7359 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7360 sizeof (struct init_node));
7361 r->purpose = purpose;
7362 r->value = value;
7363 r->origtype = origtype;
7365 *q = r;
7366 r->parent = p;
7367 r->left = 0;
7368 r->right = 0;
7369 r->balance = 0;
7371 while (p)
7373 struct init_node *s;
7375 if (r == p->left)
7377 if (p->balance == 0)
7378 p->balance = -1;
7379 else if (p->balance < 0)
7381 if (r->balance < 0)
7383 /* L rotation. */
7384 p->left = r->right;
7385 if (p->left)
7386 p->left->parent = p;
7387 r->right = p;
7389 p->balance = 0;
7390 r->balance = 0;
7392 s = p->parent;
7393 p->parent = r;
7394 r->parent = s;
7395 if (s)
7397 if (s->left == p)
7398 s->left = r;
7399 else
7400 s->right = r;
7402 else
7403 constructor_pending_elts = r;
7405 else
7407 /* LR rotation. */
7408 struct init_node *t = r->right;
7410 r->right = t->left;
7411 if (r->right)
7412 r->right->parent = r;
7413 t->left = r;
7415 p->left = t->right;
7416 if (p->left)
7417 p->left->parent = p;
7418 t->right = p;
7420 p->balance = t->balance < 0;
7421 r->balance = -(t->balance > 0);
7422 t->balance = 0;
7424 s = p->parent;
7425 p->parent = t;
7426 r->parent = t;
7427 t->parent = s;
7428 if (s)
7430 if (s->left == p)
7431 s->left = t;
7432 else
7433 s->right = t;
7435 else
7436 constructor_pending_elts = t;
7438 break;
7440 else
7442 /* p->balance == +1; growth of left side balances the node. */
7443 p->balance = 0;
7444 break;
7447 else /* r == p->right */
7449 if (p->balance == 0)
7450 /* Growth propagation from right side. */
7451 p->balance++;
7452 else if (p->balance > 0)
7454 if (r->balance > 0)
7456 /* R rotation. */
7457 p->right = r->left;
7458 if (p->right)
7459 p->right->parent = p;
7460 r->left = p;
7462 p->balance = 0;
7463 r->balance = 0;
7465 s = p->parent;
7466 p->parent = r;
7467 r->parent = s;
7468 if (s)
7470 if (s->left == p)
7471 s->left = r;
7472 else
7473 s->right = r;
7475 else
7476 constructor_pending_elts = r;
7478 else /* r->balance == -1 */
7480 /* RL rotation */
7481 struct init_node *t = r->left;
7483 r->left = t->right;
7484 if (r->left)
7485 r->left->parent = r;
7486 t->right = r;
7488 p->right = t->left;
7489 if (p->right)
7490 p->right->parent = p;
7491 t->left = p;
7493 r->balance = (t->balance < 0);
7494 p->balance = -(t->balance > 0);
7495 t->balance = 0;
7497 s = p->parent;
7498 p->parent = t;
7499 r->parent = t;
7500 t->parent = s;
7501 if (s)
7503 if (s->left == p)
7504 s->left = t;
7505 else
7506 s->right = t;
7508 else
7509 constructor_pending_elts = t;
7511 break;
7513 else
7515 /* p->balance == -1; growth of right side balances the node. */
7516 p->balance = 0;
7517 break;
7521 r = p;
7522 p = p->parent;
7526 /* Build AVL tree from a sorted chain. */
7528 static void
7529 set_nonincremental_init (struct obstack * braced_init_obstack)
7531 unsigned HOST_WIDE_INT ix;
7532 tree index, value;
7534 if (TREE_CODE (constructor_type) != RECORD_TYPE
7535 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7536 return;
7538 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7540 add_pending_init (index, value, NULL_TREE, true,
7541 braced_init_obstack);
7543 constructor_elements = 0;
7544 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7546 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7547 /* Skip any nameless bit fields at the beginning. */
7548 while (constructor_unfilled_fields != 0
7549 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7550 && DECL_NAME (constructor_unfilled_fields) == 0)
7551 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7554 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7556 if (TYPE_DOMAIN (constructor_type))
7557 constructor_unfilled_index
7558 = convert (bitsizetype,
7559 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7560 else
7561 constructor_unfilled_index = bitsize_zero_node;
7563 constructor_incremental = 0;
7566 /* Build AVL tree from a string constant. */
7568 static void
7569 set_nonincremental_init_from_string (tree str,
7570 struct obstack * braced_init_obstack)
7572 tree value, purpose, type;
7573 HOST_WIDE_INT val[2];
7574 const char *p, *end;
7575 int byte, wchar_bytes, charwidth, bitpos;
7577 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7579 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7580 charwidth = TYPE_PRECISION (char_type_node);
7581 type = TREE_TYPE (constructor_type);
7582 p = TREE_STRING_POINTER (str);
7583 end = p + TREE_STRING_LENGTH (str);
7585 for (purpose = bitsize_zero_node;
7586 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7587 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7589 if (wchar_bytes == 1)
7591 val[1] = (unsigned char) *p++;
7592 val[0] = 0;
7594 else
7596 val[0] = 0;
7597 val[1] = 0;
7598 for (byte = 0; byte < wchar_bytes; byte++)
7600 if (BYTES_BIG_ENDIAN)
7601 bitpos = (wchar_bytes - byte - 1) * charwidth;
7602 else
7603 bitpos = byte * charwidth;
7604 val[bitpos < HOST_BITS_PER_WIDE_INT]
7605 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7606 << (bitpos % HOST_BITS_PER_WIDE_INT);
7610 if (!TYPE_UNSIGNED (type))
7612 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7613 if (bitpos < HOST_BITS_PER_WIDE_INT)
7615 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7617 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7618 val[0] = -1;
7621 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7623 if (val[1] < 0)
7624 val[0] = -1;
7626 else if (val[0] & (((HOST_WIDE_INT) 1)
7627 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7628 val[0] |= ((HOST_WIDE_INT) -1)
7629 << (bitpos - HOST_BITS_PER_WIDE_INT);
7632 value = build_int_cst_wide (type, val[1], val[0]);
7633 add_pending_init (purpose, value, NULL_TREE, true,
7634 braced_init_obstack);
7637 constructor_incremental = 0;
7640 /* Return value of FIELD in pending initializer or zero if the field was
7641 not initialized yet. */
7643 static tree
7644 find_init_member (tree field, struct obstack * braced_init_obstack)
7646 struct init_node *p;
7648 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7650 if (constructor_incremental
7651 && tree_int_cst_lt (field, constructor_unfilled_index))
7652 set_nonincremental_init (braced_init_obstack);
7654 p = constructor_pending_elts;
7655 while (p)
7657 if (tree_int_cst_lt (field, p->purpose))
7658 p = p->left;
7659 else if (tree_int_cst_lt (p->purpose, field))
7660 p = p->right;
7661 else
7662 return p->value;
7665 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7667 tree bitpos = bit_position (field);
7669 if (constructor_incremental
7670 && (!constructor_unfilled_fields
7671 || tree_int_cst_lt (bitpos,
7672 bit_position (constructor_unfilled_fields))))
7673 set_nonincremental_init (braced_init_obstack);
7675 p = constructor_pending_elts;
7676 while (p)
7678 if (field == p->purpose)
7679 return p->value;
7680 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7681 p = p->left;
7682 else
7683 p = p->right;
7686 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7688 if (!VEC_empty (constructor_elt, constructor_elements)
7689 && (VEC_last (constructor_elt, constructor_elements).index
7690 == field))
7691 return VEC_last (constructor_elt, constructor_elements).value;
7693 return 0;
7696 /* "Output" the next constructor element.
7697 At top level, really output it to assembler code now.
7698 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7699 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7700 TYPE is the data type that the containing data type wants here.
7701 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7702 If VALUE is a string constant, STRICT_STRING is true if it is
7703 unparenthesized or we should not warn here for it being parenthesized.
7704 For other types of VALUE, STRICT_STRING is not used.
7706 PENDING if non-nil means output pending elements that belong
7707 right after this element. (PENDING is normally 1;
7708 it is 0 while outputting pending elements, to avoid recursion.)
7710 IMPLICIT is true if value comes from pop_init_level (1),
7711 the new initializer has been merged with the existing one
7712 and thus no warnings should be emitted about overriding an
7713 existing initializer. */
7715 static void
7716 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7717 tree field, int pending, bool implicit,
7718 struct obstack * braced_init_obstack)
7720 tree semantic_type = NULL_TREE;
7721 bool maybe_const = true;
7722 bool npc;
7724 if (type == error_mark_node || value == error_mark_node)
7726 constructor_erroneous = 1;
7727 return;
7729 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7730 && (TREE_CODE (value) == STRING_CST
7731 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7732 && !(TREE_CODE (value) == STRING_CST
7733 && TREE_CODE (type) == ARRAY_TYPE
7734 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7735 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7736 TYPE_MAIN_VARIANT (type)))
7737 value = array_to_pointer_conversion (input_location, value);
7739 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7740 && require_constant_value && !flag_isoc99 && pending)
7742 /* As an extension, allow initializing objects with static storage
7743 duration with compound literals (which are then treated just as
7744 the brace enclosed list they contain). */
7745 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7746 value = DECL_INITIAL (decl);
7749 npc = null_pointer_constant_p (value);
7750 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7752 semantic_type = TREE_TYPE (value);
7753 value = TREE_OPERAND (value, 0);
7755 value = c_fully_fold (value, require_constant_value, &maybe_const);
7757 if (value == error_mark_node)
7758 constructor_erroneous = 1;
7759 else if (!TREE_CONSTANT (value))
7760 constructor_constant = 0;
7761 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7762 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7763 || TREE_CODE (constructor_type) == UNION_TYPE)
7764 && DECL_C_BIT_FIELD (field)
7765 && TREE_CODE (value) != INTEGER_CST))
7766 constructor_simple = 0;
7767 if (!maybe_const)
7768 constructor_nonconst = 1;
7770 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7772 if (require_constant_value)
7774 error_init ("initializer element is not constant");
7775 value = error_mark_node;
7777 else if (require_constant_elements)
7778 pedwarn (input_location, 0,
7779 "initializer element is not computable at load time");
7781 else if (!maybe_const
7782 && (require_constant_value || require_constant_elements))
7783 pedwarn_init (input_location, 0,
7784 "initializer element is not a constant expression");
7786 /* Issue -Wc++-compat warnings about initializing a bitfield with
7787 enum type. */
7788 if (warn_cxx_compat
7789 && field != NULL_TREE
7790 && TREE_CODE (field) == FIELD_DECL
7791 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7792 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7793 != TYPE_MAIN_VARIANT (type))
7794 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7796 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7797 if (checktype != error_mark_node
7798 && (TYPE_MAIN_VARIANT (checktype)
7799 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7800 warning_init (OPT_Wc___compat,
7801 "enum conversion in initialization is invalid in C++");
7804 /* If this field is empty (and not at the end of structure),
7805 don't do anything other than checking the initializer. */
7806 if (field
7807 && (TREE_TYPE (field) == error_mark_node
7808 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7809 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7810 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7811 || DECL_CHAIN (field)))))
7812 return;
7814 if (semantic_type)
7815 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7816 value = digest_init (input_location, type, value, origtype, npc,
7817 strict_string, require_constant_value);
7818 if (value == error_mark_node)
7820 constructor_erroneous = 1;
7821 return;
7823 if (require_constant_value || require_constant_elements)
7824 constant_expression_warning (value);
7826 /* If this element doesn't come next in sequence,
7827 put it on constructor_pending_elts. */
7828 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7829 && (!constructor_incremental
7830 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7832 if (constructor_incremental
7833 && tree_int_cst_lt (field, constructor_unfilled_index))
7834 set_nonincremental_init (braced_init_obstack);
7836 add_pending_init (field, value, origtype, implicit,
7837 braced_init_obstack);
7838 return;
7840 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7841 && (!constructor_incremental
7842 || field != constructor_unfilled_fields))
7844 /* We do this for records but not for unions. In a union,
7845 no matter which field is specified, it can be initialized
7846 right away since it starts at the beginning of the union. */
7847 if (constructor_incremental)
7849 if (!constructor_unfilled_fields)
7850 set_nonincremental_init (braced_init_obstack);
7851 else
7853 tree bitpos, unfillpos;
7855 bitpos = bit_position (field);
7856 unfillpos = bit_position (constructor_unfilled_fields);
7858 if (tree_int_cst_lt (bitpos, unfillpos))
7859 set_nonincremental_init (braced_init_obstack);
7863 add_pending_init (field, value, origtype, implicit,
7864 braced_init_obstack);
7865 return;
7867 else if (TREE_CODE (constructor_type) == UNION_TYPE
7868 && !VEC_empty (constructor_elt, constructor_elements))
7870 if (!implicit)
7872 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7873 constructor_elements).value))
7874 warning_init (0,
7875 "initialized field with side-effects overwritten");
7876 else if (warn_override_init)
7877 warning_init (OPT_Woverride_init, "initialized field overwritten");
7880 /* We can have just one union field set. */
7881 constructor_elements = 0;
7884 /* Otherwise, output this element either to
7885 constructor_elements or to the assembler file. */
7887 constructor_elt celt = {field, value};
7888 VEC_safe_push (constructor_elt, gc, constructor_elements, celt);
7890 /* Advance the variable that indicates sequential elements output. */
7891 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7892 constructor_unfilled_index
7893 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7894 bitsize_one_node);
7895 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7897 constructor_unfilled_fields
7898 = DECL_CHAIN (constructor_unfilled_fields);
7900 /* Skip any nameless bit fields. */
7901 while (constructor_unfilled_fields != 0
7902 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7903 && DECL_NAME (constructor_unfilled_fields) == 0)
7904 constructor_unfilled_fields =
7905 DECL_CHAIN (constructor_unfilled_fields);
7907 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7908 constructor_unfilled_fields = 0;
7910 /* Now output any pending elements which have become next. */
7911 if (pending)
7912 output_pending_init_elements (0, braced_init_obstack);
7915 /* Output any pending elements which have become next.
7916 As we output elements, constructor_unfilled_{fields,index}
7917 advances, which may cause other elements to become next;
7918 if so, they too are output.
7920 If ALL is 0, we return when there are
7921 no more pending elements to output now.
7923 If ALL is 1, we output space as necessary so that
7924 we can output all the pending elements. */
7925 static void
7926 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7928 struct init_node *elt = constructor_pending_elts;
7929 tree next;
7931 retry:
7933 /* Look through the whole pending tree.
7934 If we find an element that should be output now,
7935 output it. Otherwise, set NEXT to the element
7936 that comes first among those still pending. */
7938 next = 0;
7939 while (elt)
7941 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7943 if (tree_int_cst_equal (elt->purpose,
7944 constructor_unfilled_index))
7945 output_init_element (elt->value, elt->origtype, true,
7946 TREE_TYPE (constructor_type),
7947 constructor_unfilled_index, 0, false,
7948 braced_init_obstack);
7949 else if (tree_int_cst_lt (constructor_unfilled_index,
7950 elt->purpose))
7952 /* Advance to the next smaller node. */
7953 if (elt->left)
7954 elt = elt->left;
7955 else
7957 /* We have reached the smallest node bigger than the
7958 current unfilled index. Fill the space first. */
7959 next = elt->purpose;
7960 break;
7963 else
7965 /* Advance to the next bigger node. */
7966 if (elt->right)
7967 elt = elt->right;
7968 else
7970 /* We have reached the biggest node in a subtree. Find
7971 the parent of it, which is the next bigger node. */
7972 while (elt->parent && elt->parent->right == elt)
7973 elt = elt->parent;
7974 elt = elt->parent;
7975 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7976 elt->purpose))
7978 next = elt->purpose;
7979 break;
7984 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7985 || TREE_CODE (constructor_type) == UNION_TYPE)
7987 tree ctor_unfilled_bitpos, elt_bitpos;
7989 /* If the current record is complete we are done. */
7990 if (constructor_unfilled_fields == 0)
7991 break;
7993 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7994 elt_bitpos = bit_position (elt->purpose);
7995 /* We can't compare fields here because there might be empty
7996 fields in between. */
7997 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7999 constructor_unfilled_fields = elt->purpose;
8000 output_init_element (elt->value, elt->origtype, true,
8001 TREE_TYPE (elt->purpose),
8002 elt->purpose, 0, false,
8003 braced_init_obstack);
8005 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8007 /* Advance to the next smaller node. */
8008 if (elt->left)
8009 elt = elt->left;
8010 else
8012 /* We have reached the smallest node bigger than the
8013 current unfilled field. Fill the space first. */
8014 next = elt->purpose;
8015 break;
8018 else
8020 /* Advance to the next bigger node. */
8021 if (elt->right)
8022 elt = elt->right;
8023 else
8025 /* We have reached the biggest node in a subtree. Find
8026 the parent of it, which is the next bigger node. */
8027 while (elt->parent && elt->parent->right == elt)
8028 elt = elt->parent;
8029 elt = elt->parent;
8030 if (elt
8031 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8032 bit_position (elt->purpose))))
8034 next = elt->purpose;
8035 break;
8042 /* Ordinarily return, but not if we want to output all
8043 and there are elements left. */
8044 if (!(all && next != 0))
8045 return;
8047 /* If it's not incremental, just skip over the gap, so that after
8048 jumping to retry we will output the next successive element. */
8049 if (TREE_CODE (constructor_type) == RECORD_TYPE
8050 || TREE_CODE (constructor_type) == UNION_TYPE)
8051 constructor_unfilled_fields = next;
8052 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8053 constructor_unfilled_index = next;
8055 /* ELT now points to the node in the pending tree with the next
8056 initializer to output. */
8057 goto retry;
8060 /* Add one non-braced element to the current constructor level.
8061 This adjusts the current position within the constructor's type.
8062 This may also start or terminate implicit levels
8063 to handle a partly-braced initializer.
8065 Once this has found the correct level for the new element,
8066 it calls output_init_element.
8068 IMPLICIT is true if value comes from pop_init_level (1),
8069 the new initializer has been merged with the existing one
8070 and thus no warnings should be emitted about overriding an
8071 existing initializer. */
8073 void
8074 process_init_element (struct c_expr value, bool implicit,
8075 struct obstack * braced_init_obstack)
8077 tree orig_value = value.value;
8078 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8079 bool strict_string = value.original_code == STRING_CST;
8081 designator_depth = 0;
8082 designator_erroneous = 0;
8084 /* Handle superfluous braces around string cst as in
8085 char x[] = {"foo"}; */
8086 if (string_flag
8087 && constructor_type
8088 && TREE_CODE (constructor_type) == ARRAY_TYPE
8089 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8090 && integer_zerop (constructor_unfilled_index))
8092 if (constructor_stack->replacement_value.value)
8093 error_init ("excess elements in char array initializer");
8094 constructor_stack->replacement_value = value;
8095 return;
8098 if (constructor_stack->replacement_value.value != 0)
8100 error_init ("excess elements in struct initializer");
8101 return;
8104 /* Ignore elements of a brace group if it is entirely superfluous
8105 and has already been diagnosed. */
8106 if (constructor_type == 0)
8107 return;
8109 /* If we've exhausted any levels that didn't have braces,
8110 pop them now. */
8111 while (constructor_stack->implicit)
8113 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8114 || TREE_CODE (constructor_type) == UNION_TYPE)
8115 && constructor_fields == 0)
8116 process_init_element (pop_init_level (1, braced_init_obstack),
8117 true, braced_init_obstack);
8118 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8119 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8120 && (constructor_max_index == 0
8121 || tree_int_cst_lt (constructor_max_index,
8122 constructor_index)))
8123 process_init_element (pop_init_level (1, braced_init_obstack),
8124 true, braced_init_obstack);
8125 else
8126 break;
8129 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8130 if (constructor_range_stack)
8132 /* If value is a compound literal and we'll be just using its
8133 content, don't put it into a SAVE_EXPR. */
8134 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8135 || !require_constant_value
8136 || flag_isoc99)
8138 tree semantic_type = NULL_TREE;
8139 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8141 semantic_type = TREE_TYPE (value.value);
8142 value.value = TREE_OPERAND (value.value, 0);
8144 value.value = c_save_expr (value.value);
8145 if (semantic_type)
8146 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8147 value.value);
8151 while (1)
8153 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8155 tree fieldtype;
8156 enum tree_code fieldcode;
8158 if (constructor_fields == 0)
8160 pedwarn_init (input_location, 0,
8161 "excess elements in struct initializer");
8162 break;
8165 fieldtype = TREE_TYPE (constructor_fields);
8166 if (fieldtype != error_mark_node)
8167 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8168 fieldcode = TREE_CODE (fieldtype);
8170 /* Error for non-static initialization of a flexible array member. */
8171 if (fieldcode == ARRAY_TYPE
8172 && !require_constant_value
8173 && TYPE_SIZE (fieldtype) == NULL_TREE
8174 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8176 error_init ("non-static initialization of a flexible array member");
8177 break;
8180 /* Accept a string constant to initialize a subarray. */
8181 if (value.value != 0
8182 && fieldcode == ARRAY_TYPE
8183 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8184 && string_flag)
8185 value.value = orig_value;
8186 /* Otherwise, if we have come to a subaggregate,
8187 and we don't have an element of its type, push into it. */
8188 else if (value.value != 0
8189 && value.value != error_mark_node
8190 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8191 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8192 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8194 push_init_level (1, braced_init_obstack);
8195 continue;
8198 if (value.value)
8200 push_member_name (constructor_fields);
8201 output_init_element (value.value, value.original_type,
8202 strict_string, fieldtype,
8203 constructor_fields, 1, implicit,
8204 braced_init_obstack);
8205 RESTORE_SPELLING_DEPTH (constructor_depth);
8207 else
8208 /* Do the bookkeeping for an element that was
8209 directly output as a constructor. */
8211 /* For a record, keep track of end position of last field. */
8212 if (DECL_SIZE (constructor_fields))
8213 constructor_bit_index
8214 = size_binop_loc (input_location, PLUS_EXPR,
8215 bit_position (constructor_fields),
8216 DECL_SIZE (constructor_fields));
8218 /* If the current field was the first one not yet written out,
8219 it isn't now, so update. */
8220 if (constructor_unfilled_fields == constructor_fields)
8222 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8223 /* Skip any nameless bit fields. */
8224 while (constructor_unfilled_fields != 0
8225 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8226 && DECL_NAME (constructor_unfilled_fields) == 0)
8227 constructor_unfilled_fields =
8228 DECL_CHAIN (constructor_unfilled_fields);
8232 constructor_fields = DECL_CHAIN (constructor_fields);
8233 /* Skip any nameless bit fields at the beginning. */
8234 while (constructor_fields != 0
8235 && DECL_C_BIT_FIELD (constructor_fields)
8236 && DECL_NAME (constructor_fields) == 0)
8237 constructor_fields = DECL_CHAIN (constructor_fields);
8239 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8241 tree fieldtype;
8242 enum tree_code fieldcode;
8244 if (constructor_fields == 0)
8246 pedwarn_init (input_location, 0,
8247 "excess elements in union initializer");
8248 break;
8251 fieldtype = TREE_TYPE (constructor_fields);
8252 if (fieldtype != error_mark_node)
8253 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8254 fieldcode = TREE_CODE (fieldtype);
8256 /* Warn that traditional C rejects initialization of unions.
8257 We skip the warning if the value is zero. This is done
8258 under the assumption that the zero initializer in user
8259 code appears conditioned on e.g. __STDC__ to avoid
8260 "missing initializer" warnings and relies on default
8261 initialization to zero in the traditional C case.
8262 We also skip the warning if the initializer is designated,
8263 again on the assumption that this must be conditional on
8264 __STDC__ anyway (and we've already complained about the
8265 member-designator already). */
8266 if (!in_system_header && !constructor_designated
8267 && !(value.value && (integer_zerop (value.value)
8268 || real_zerop (value.value))))
8269 warning (OPT_Wtraditional, "traditional C rejects initialization "
8270 "of unions");
8272 /* Accept a string constant to initialize a subarray. */
8273 if (value.value != 0
8274 && fieldcode == ARRAY_TYPE
8275 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8276 && string_flag)
8277 value.value = orig_value;
8278 /* Otherwise, if we have come to a subaggregate,
8279 and we don't have an element of its type, push into it. */
8280 else if (value.value != 0
8281 && value.value != error_mark_node
8282 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8283 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8284 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8286 push_init_level (1, braced_init_obstack);
8287 continue;
8290 if (value.value)
8292 push_member_name (constructor_fields);
8293 output_init_element (value.value, value.original_type,
8294 strict_string, fieldtype,
8295 constructor_fields, 1, implicit,
8296 braced_init_obstack);
8297 RESTORE_SPELLING_DEPTH (constructor_depth);
8299 else
8300 /* Do the bookkeeping for an element that was
8301 directly output as a constructor. */
8303 constructor_bit_index = DECL_SIZE (constructor_fields);
8304 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8307 constructor_fields = 0;
8309 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8311 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8312 enum tree_code eltcode = TREE_CODE (elttype);
8314 /* Accept a string constant to initialize a subarray. */
8315 if (value.value != 0
8316 && eltcode == ARRAY_TYPE
8317 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8318 && string_flag)
8319 value.value = orig_value;
8320 /* Otherwise, if we have come to a subaggregate,
8321 and we don't have an element of its type, push into it. */
8322 else if (value.value != 0
8323 && value.value != error_mark_node
8324 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8325 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8326 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8328 push_init_level (1, braced_init_obstack);
8329 continue;
8332 if (constructor_max_index != 0
8333 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8334 || integer_all_onesp (constructor_max_index)))
8336 pedwarn_init (input_location, 0,
8337 "excess elements in array initializer");
8338 break;
8341 /* Now output the actual element. */
8342 if (value.value)
8344 push_array_bounds (tree_low_cst (constructor_index, 1));
8345 output_init_element (value.value, value.original_type,
8346 strict_string, elttype,
8347 constructor_index, 1, implicit,
8348 braced_init_obstack);
8349 RESTORE_SPELLING_DEPTH (constructor_depth);
8352 constructor_index
8353 = size_binop_loc (input_location, PLUS_EXPR,
8354 constructor_index, bitsize_one_node);
8356 if (!value.value)
8357 /* If we are doing the bookkeeping for an element that was
8358 directly output as a constructor, we must update
8359 constructor_unfilled_index. */
8360 constructor_unfilled_index = constructor_index;
8362 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8364 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8366 /* Do a basic check of initializer size. Note that vectors
8367 always have a fixed size derived from their type. */
8368 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8370 pedwarn_init (input_location, 0,
8371 "excess elements in vector initializer");
8372 break;
8375 /* Now output the actual element. */
8376 if (value.value)
8378 if (TREE_CODE (value.value) == VECTOR_CST)
8379 elttype = TYPE_MAIN_VARIANT (constructor_type);
8380 output_init_element (value.value, value.original_type,
8381 strict_string, elttype,
8382 constructor_index, 1, implicit,
8383 braced_init_obstack);
8386 constructor_index
8387 = size_binop_loc (input_location,
8388 PLUS_EXPR, constructor_index, bitsize_one_node);
8390 if (!value.value)
8391 /* If we are doing the bookkeeping for an element that was
8392 directly output as a constructor, we must update
8393 constructor_unfilled_index. */
8394 constructor_unfilled_index = constructor_index;
8397 /* Handle the sole element allowed in a braced initializer
8398 for a scalar variable. */
8399 else if (constructor_type != error_mark_node
8400 && constructor_fields == 0)
8402 pedwarn_init (input_location, 0,
8403 "excess elements in scalar initializer");
8404 break;
8406 else
8408 if (value.value)
8409 output_init_element (value.value, value.original_type,
8410 strict_string, constructor_type,
8411 NULL_TREE, 1, implicit,
8412 braced_init_obstack);
8413 constructor_fields = 0;
8416 /* Handle range initializers either at this level or anywhere higher
8417 in the designator stack. */
8418 if (constructor_range_stack)
8420 struct constructor_range_stack *p, *range_stack;
8421 int finish = 0;
8423 range_stack = constructor_range_stack;
8424 constructor_range_stack = 0;
8425 while (constructor_stack != range_stack->stack)
8427 gcc_assert (constructor_stack->implicit);
8428 process_init_element (pop_init_level (1,
8429 braced_init_obstack),
8430 true, braced_init_obstack);
8432 for (p = range_stack;
8433 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8434 p = p->prev)
8436 gcc_assert (constructor_stack->implicit);
8437 process_init_element (pop_init_level (1, braced_init_obstack),
8438 true, braced_init_obstack);
8441 p->index = size_binop_loc (input_location,
8442 PLUS_EXPR, p->index, bitsize_one_node);
8443 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8444 finish = 1;
8446 while (1)
8448 constructor_index = p->index;
8449 constructor_fields = p->fields;
8450 if (finish && p->range_end && p->index == p->range_start)
8452 finish = 0;
8453 p->prev = 0;
8455 p = p->next;
8456 if (!p)
8457 break;
8458 push_init_level (2, braced_init_obstack);
8459 p->stack = constructor_stack;
8460 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8461 p->index = p->range_start;
8464 if (!finish)
8465 constructor_range_stack = range_stack;
8466 continue;
8469 break;
8472 constructor_range_stack = 0;
8475 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8476 (guaranteed to be 'volatile' or null) and ARGS (represented using
8477 an ASM_EXPR node). */
8478 tree
8479 build_asm_stmt (tree cv_qualifier, tree args)
8481 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8482 ASM_VOLATILE_P (args) = 1;
8483 return add_stmt (args);
8486 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8487 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8488 SIMPLE indicates whether there was anything at all after the
8489 string in the asm expression -- asm("blah") and asm("blah" : )
8490 are subtly different. We use a ASM_EXPR node to represent this. */
8491 tree
8492 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8493 tree clobbers, tree labels, bool simple)
8495 tree tail;
8496 tree args;
8497 int i;
8498 const char *constraint;
8499 const char **oconstraints;
8500 bool allows_mem, allows_reg, is_inout;
8501 int ninputs, noutputs;
8503 ninputs = list_length (inputs);
8504 noutputs = list_length (outputs);
8505 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8507 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8509 /* Remove output conversions that change the type but not the mode. */
8510 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8512 tree output = TREE_VALUE (tail);
8514 /* ??? Really, this should not be here. Users should be using a
8515 proper lvalue, dammit. But there's a long history of using casts
8516 in the output operands. In cases like longlong.h, this becomes a
8517 primitive form of typechecking -- if the cast can be removed, then
8518 the output operand had a type of the proper width; otherwise we'll
8519 get an error. Gross, but ... */
8520 STRIP_NOPS (output);
8522 if (!lvalue_or_else (loc, output, lv_asm))
8523 output = error_mark_node;
8525 if (output != error_mark_node
8526 && (TREE_READONLY (output)
8527 || TYPE_READONLY (TREE_TYPE (output))
8528 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8529 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8530 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8531 readonly_error (output, lv_asm);
8533 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8534 oconstraints[i] = constraint;
8536 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8537 &allows_mem, &allows_reg, &is_inout))
8539 /* If the operand is going to end up in memory,
8540 mark it addressable. */
8541 if (!allows_reg && !c_mark_addressable (output))
8542 output = error_mark_node;
8543 if (!(!allows_reg && allows_mem)
8544 && output != error_mark_node
8545 && VOID_TYPE_P (TREE_TYPE (output)))
8547 error_at (loc, "invalid use of void expression");
8548 output = error_mark_node;
8551 else
8552 output = error_mark_node;
8554 TREE_VALUE (tail) = output;
8557 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8559 tree input;
8561 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8562 input = TREE_VALUE (tail);
8564 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8565 oconstraints, &allows_mem, &allows_reg))
8567 /* If the operand is going to end up in memory,
8568 mark it addressable. */
8569 if (!allows_reg && allows_mem)
8571 /* Strip the nops as we allow this case. FIXME, this really
8572 should be rejected or made deprecated. */
8573 STRIP_NOPS (input);
8574 if (!c_mark_addressable (input))
8575 input = error_mark_node;
8577 else if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8579 error_at (loc, "invalid use of void expression");
8580 input = error_mark_node;
8583 else
8584 input = error_mark_node;
8586 TREE_VALUE (tail) = input;
8589 /* ASMs with labels cannot have outputs. This should have been
8590 enforced by the parser. */
8591 gcc_assert (outputs == NULL || labels == NULL);
8593 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8595 /* asm statements without outputs, including simple ones, are treated
8596 as volatile. */
8597 ASM_INPUT_P (args) = simple;
8598 ASM_VOLATILE_P (args) = (noutputs == 0);
8600 return args;
8603 /* Generate a goto statement to LABEL. LOC is the location of the
8604 GOTO. */
8606 tree
8607 c_finish_goto_label (location_t loc, tree label)
8609 tree decl = lookup_label_for_goto (loc, label);
8610 if (!decl)
8611 return NULL_TREE;
8612 TREE_USED (decl) = 1;
8614 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8615 SET_EXPR_LOCATION (t, loc);
8616 return add_stmt (t);
8620 /* Generate a computed goto statement to EXPR. LOC is the location of
8621 the GOTO. */
8623 tree
8624 c_finish_goto_ptr (location_t loc, tree expr)
8626 tree t;
8627 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
8628 expr = c_fully_fold (expr, false, NULL);
8629 expr = convert (ptr_type_node, expr);
8630 t = build1 (GOTO_EXPR, void_type_node, expr);
8631 SET_EXPR_LOCATION (t, loc);
8632 return add_stmt (t);
8635 /* Generate a C `return' statement. RETVAL is the expression for what
8636 to return, or a null pointer for `return;' with no value. LOC is
8637 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8638 is the original type of RETVAL. */
8640 tree
8641 c_finish_return (location_t loc, tree retval, tree origtype)
8643 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8644 bool no_warning = false;
8645 bool npc = false;
8647 if (TREE_THIS_VOLATILE (current_function_decl))
8648 warning_at (loc, 0,
8649 "function declared %<noreturn%> has a %<return%> statement");
8651 if (retval)
8653 tree semantic_type = NULL_TREE;
8654 npc = null_pointer_constant_p (retval);
8655 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8657 semantic_type = TREE_TYPE (retval);
8658 retval = TREE_OPERAND (retval, 0);
8660 retval = c_fully_fold (retval, false, NULL);
8661 if (semantic_type)
8662 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8665 if (!retval)
8667 current_function_returns_null = 1;
8668 if ((warn_return_type || flag_isoc99)
8669 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8671 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8672 "%<return%> with no value, in "
8673 "function returning non-void");
8674 no_warning = true;
8677 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8679 current_function_returns_null = 1;
8680 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8681 pedwarn (loc, 0,
8682 "%<return%> with a value, in function returning void");
8683 else
8684 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
8685 "%<return%> with expression, in function returning void");
8687 else
8689 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8690 ic_return,
8691 npc, NULL_TREE, NULL_TREE, 0);
8692 tree res = DECL_RESULT (current_function_decl);
8693 tree inner;
8694 bool save;
8696 current_function_returns_value = 1;
8697 if (t == error_mark_node)
8698 return NULL_TREE;
8700 save = in_late_binary_op;
8701 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
8702 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
8703 in_late_binary_op = true;
8704 inner = t = convert (TREE_TYPE (res), t);
8705 in_late_binary_op = save;
8707 /* Strip any conversions, additions, and subtractions, and see if
8708 we are returning the address of a local variable. Warn if so. */
8709 while (1)
8711 switch (TREE_CODE (inner))
8713 CASE_CONVERT:
8714 case NON_LVALUE_EXPR:
8715 case PLUS_EXPR:
8716 case POINTER_PLUS_EXPR:
8717 inner = TREE_OPERAND (inner, 0);
8718 continue;
8720 case MINUS_EXPR:
8721 /* If the second operand of the MINUS_EXPR has a pointer
8722 type (or is converted from it), this may be valid, so
8723 don't give a warning. */
8725 tree op1 = TREE_OPERAND (inner, 1);
8727 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8728 && (CONVERT_EXPR_P (op1)
8729 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8730 op1 = TREE_OPERAND (op1, 0);
8732 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8733 break;
8735 inner = TREE_OPERAND (inner, 0);
8736 continue;
8739 case ADDR_EXPR:
8740 inner = TREE_OPERAND (inner, 0);
8742 while (REFERENCE_CLASS_P (inner)
8743 && TREE_CODE (inner) != INDIRECT_REF)
8744 inner = TREE_OPERAND (inner, 0);
8746 if (DECL_P (inner)
8747 && !DECL_EXTERNAL (inner)
8748 && !TREE_STATIC (inner)
8749 && DECL_CONTEXT (inner) == current_function_decl)
8750 warning_at (loc,
8751 0, "function returns address of local variable");
8752 break;
8754 default:
8755 break;
8758 break;
8761 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8762 SET_EXPR_LOCATION (retval, loc);
8764 if (warn_sequence_point)
8765 verify_sequence_points (retval);
8768 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8769 TREE_NO_WARNING (ret_stmt) |= no_warning;
8770 return add_stmt (ret_stmt);
8773 struct c_switch {
8774 /* The SWITCH_EXPR being built. */
8775 tree switch_expr;
8777 /* The original type of the testing expression, i.e. before the
8778 default conversion is applied. */
8779 tree orig_type;
8781 /* A splay-tree mapping the low element of a case range to the high
8782 element, or NULL_TREE if there is no high element. Used to
8783 determine whether or not a new case label duplicates an old case
8784 label. We need a tree, rather than simply a hash table, because
8785 of the GNU case range extension. */
8786 splay_tree cases;
8788 /* The bindings at the point of the switch. This is used for
8789 warnings crossing decls when branching to a case label. */
8790 struct c_spot_bindings *bindings;
8792 /* The next node on the stack. */
8793 struct c_switch *next;
8796 /* A stack of the currently active switch statements. The innermost
8797 switch statement is on the top of the stack. There is no need to
8798 mark the stack for garbage collection because it is only active
8799 during the processing of the body of a function, and we never
8800 collect at that point. */
8802 struct c_switch *c_switch_stack;
8804 /* Start a C switch statement, testing expression EXP. Return the new
8805 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8806 SWITCH_COND_LOC is the location of the switch's condition. */
8808 tree
8809 c_start_case (location_t switch_loc,
8810 location_t switch_cond_loc,
8811 tree exp)
8813 tree orig_type = error_mark_node;
8814 struct c_switch *cs;
8816 if (exp != error_mark_node)
8818 orig_type = TREE_TYPE (exp);
8820 if (!INTEGRAL_TYPE_P (orig_type))
8822 if (orig_type != error_mark_node)
8824 error_at (switch_cond_loc, "switch quantity not an integer");
8825 orig_type = error_mark_node;
8827 exp = integer_zero_node;
8829 else
8831 tree type = TYPE_MAIN_VARIANT (orig_type);
8833 if (!in_system_header
8834 && (type == long_integer_type_node
8835 || type == long_unsigned_type_node))
8836 warning_at (switch_cond_loc,
8837 OPT_Wtraditional, "%<long%> switch expression not "
8838 "converted to %<int%> in ISO C");
8840 exp = c_fully_fold (exp, false, NULL);
8841 exp = default_conversion (exp);
8843 if (warn_sequence_point)
8844 verify_sequence_points (exp);
8848 /* Add this new SWITCH_EXPR to the stack. */
8849 cs = XNEW (struct c_switch);
8850 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8851 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8852 cs->orig_type = orig_type;
8853 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8854 cs->bindings = c_get_switch_bindings ();
8855 cs->next = c_switch_stack;
8856 c_switch_stack = cs;
8858 return add_stmt (cs->switch_expr);
8861 /* Process a case label at location LOC. */
8863 tree
8864 do_case (location_t loc, tree low_value, tree high_value)
8866 tree label = NULL_TREE;
8868 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8870 low_value = c_fully_fold (low_value, false, NULL);
8871 if (TREE_CODE (low_value) == INTEGER_CST)
8872 pedwarn (input_location, OPT_Wpedantic,
8873 "case label is not an integer constant expression");
8876 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8878 high_value = c_fully_fold (high_value, false, NULL);
8879 if (TREE_CODE (high_value) == INTEGER_CST)
8880 pedwarn (input_location, OPT_Wpedantic,
8881 "case label is not an integer constant expression");
8884 if (c_switch_stack == NULL)
8886 if (low_value)
8887 error_at (loc, "case label not within a switch statement");
8888 else
8889 error_at (loc, "%<default%> label not within a switch statement");
8890 return NULL_TREE;
8893 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8894 EXPR_LOCATION (c_switch_stack->switch_expr),
8895 loc))
8896 return NULL_TREE;
8898 label = c_add_case_label (loc, c_switch_stack->cases,
8899 SWITCH_COND (c_switch_stack->switch_expr),
8900 c_switch_stack->orig_type,
8901 low_value, high_value);
8902 if (label == error_mark_node)
8903 label = NULL_TREE;
8904 return label;
8907 /* Finish the switch statement. */
8909 void
8910 c_finish_case (tree body)
8912 struct c_switch *cs = c_switch_stack;
8913 location_t switch_location;
8915 SWITCH_BODY (cs->switch_expr) = body;
8917 /* Emit warnings as needed. */
8918 switch_location = EXPR_LOCATION (cs->switch_expr);
8919 c_do_switch_warnings (cs->cases, switch_location,
8920 TREE_TYPE (cs->switch_expr),
8921 SWITCH_COND (cs->switch_expr));
8923 /* Pop the stack. */
8924 c_switch_stack = cs->next;
8925 splay_tree_delete (cs->cases);
8926 c_release_switch_bindings (cs->bindings);
8927 XDELETE (cs);
8930 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8931 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8932 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8933 statement, and was not surrounded with parenthesis. */
8935 void
8936 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8937 tree else_block, bool nested_if)
8939 tree stmt;
8941 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8942 if (warn_parentheses && nested_if && else_block == NULL)
8944 tree inner_if = then_block;
8946 /* We know from the grammar productions that there is an IF nested
8947 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8948 it might not be exactly THEN_BLOCK, but should be the last
8949 non-container statement within. */
8950 while (1)
8951 switch (TREE_CODE (inner_if))
8953 case COND_EXPR:
8954 goto found;
8955 case BIND_EXPR:
8956 inner_if = BIND_EXPR_BODY (inner_if);
8957 break;
8958 case STATEMENT_LIST:
8959 inner_if = expr_last (then_block);
8960 break;
8961 case TRY_FINALLY_EXPR:
8962 case TRY_CATCH_EXPR:
8963 inner_if = TREE_OPERAND (inner_if, 0);
8964 break;
8965 default:
8966 gcc_unreachable ();
8968 found:
8970 if (COND_EXPR_ELSE (inner_if))
8971 warning_at (if_locus, OPT_Wparentheses,
8972 "suggest explicit braces to avoid ambiguous %<else%>");
8975 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8976 SET_EXPR_LOCATION (stmt, if_locus);
8977 add_stmt (stmt);
8980 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8981 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8982 is false for DO loops. INCR is the FOR increment expression. BODY is
8983 the statement controlled by the loop. BLAB is the break label. CLAB is
8984 the continue label. Everything is allowed to be NULL. */
8986 void
8987 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8988 tree blab, tree clab, bool cond_is_first)
8990 tree entry = NULL, exit = NULL, t;
8992 /* If the condition is zero don't generate a loop construct. */
8993 if (cond && integer_zerop (cond))
8995 if (cond_is_first)
8997 t = build_and_jump (&blab);
8998 SET_EXPR_LOCATION (t, start_locus);
8999 add_stmt (t);
9002 else
9004 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9006 /* If we have an exit condition, then we build an IF with gotos either
9007 out of the loop, or to the top of it. If there's no exit condition,
9008 then we just build a jump back to the top. */
9009 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9011 if (cond && !integer_nonzerop (cond))
9013 /* Canonicalize the loop condition to the end. This means
9014 generating a branch to the loop condition. Reuse the
9015 continue label, if possible. */
9016 if (cond_is_first)
9018 if (incr || !clab)
9020 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9021 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9023 else
9024 t = build1 (GOTO_EXPR, void_type_node, clab);
9025 SET_EXPR_LOCATION (t, start_locus);
9026 add_stmt (t);
9029 t = build_and_jump (&blab);
9030 if (cond_is_first)
9031 exit = fold_build3_loc (start_locus,
9032 COND_EXPR, void_type_node, cond, exit, t);
9033 else
9034 exit = fold_build3_loc (input_location,
9035 COND_EXPR, void_type_node, cond, exit, t);
9038 add_stmt (top);
9041 if (body)
9042 add_stmt (body);
9043 if (clab)
9044 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9045 if (incr)
9046 add_stmt (incr);
9047 if (entry)
9048 add_stmt (entry);
9049 if (exit)
9050 add_stmt (exit);
9051 if (blab)
9052 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9055 tree
9056 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9058 bool skip;
9059 tree label = *label_p;
9061 /* In switch statements break is sometimes stylistically used after
9062 a return statement. This can lead to spurious warnings about
9063 control reaching the end of a non-void function when it is
9064 inlined. Note that we are calling block_may_fallthru with
9065 language specific tree nodes; this works because
9066 block_may_fallthru returns true when given something it does not
9067 understand. */
9068 skip = !block_may_fallthru (cur_stmt_list);
9070 if (!label)
9072 if (!skip)
9073 *label_p = label = create_artificial_label (loc);
9075 else if (TREE_CODE (label) == LABEL_DECL)
9077 else switch (TREE_INT_CST_LOW (label))
9079 case 0:
9080 if (is_break)
9081 error_at (loc, "break statement not within loop or switch");
9082 else
9083 error_at (loc, "continue statement not within a loop");
9084 return NULL_TREE;
9086 case 1:
9087 gcc_assert (is_break);
9088 error_at (loc, "break statement used with OpenMP for loop");
9089 return NULL_TREE;
9091 default:
9092 gcc_unreachable ();
9095 if (skip)
9096 return NULL_TREE;
9098 if (!is_break)
9099 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9101 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9104 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9106 static void
9107 emit_side_effect_warnings (location_t loc, tree expr)
9109 if (expr == error_mark_node)
9111 else if (!TREE_SIDE_EFFECTS (expr))
9113 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9114 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9116 else
9117 warn_if_unused_value (expr, loc);
9120 /* Process an expression as if it were a complete statement. Emit
9121 diagnostics, but do not call ADD_STMT. LOC is the location of the
9122 statement. */
9124 tree
9125 c_process_expr_stmt (location_t loc, tree expr)
9127 tree exprv;
9129 if (!expr)
9130 return NULL_TREE;
9132 expr = c_fully_fold (expr, false, NULL);
9134 if (warn_sequence_point)
9135 verify_sequence_points (expr);
9137 if (TREE_TYPE (expr) != error_mark_node
9138 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9139 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9140 error_at (loc, "expression statement has incomplete type");
9142 /* If we're not processing a statement expression, warn about unused values.
9143 Warnings for statement expressions will be emitted later, once we figure
9144 out which is the result. */
9145 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9146 && warn_unused_value)
9147 emit_side_effect_warnings (loc, expr);
9149 exprv = expr;
9150 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9151 exprv = TREE_OPERAND (exprv, 1);
9152 while (CONVERT_EXPR_P (exprv))
9153 exprv = TREE_OPERAND (exprv, 0);
9154 if (DECL_P (exprv)
9155 || handled_component_p (exprv)
9156 || TREE_CODE (exprv) == ADDR_EXPR)
9157 mark_exp_read (exprv);
9159 /* If the expression is not of a type to which we cannot assign a line
9160 number, wrap the thing in a no-op NOP_EXPR. */
9161 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9163 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9164 SET_EXPR_LOCATION (expr, loc);
9167 return expr;
9170 /* Emit an expression as a statement. LOC is the location of the
9171 expression. */
9173 tree
9174 c_finish_expr_stmt (location_t loc, tree expr)
9176 if (expr)
9177 return add_stmt (c_process_expr_stmt (loc, expr));
9178 else
9179 return NULL;
9182 /* Do the opposite and emit a statement as an expression. To begin,
9183 create a new binding level and return it. */
9185 tree
9186 c_begin_stmt_expr (void)
9188 tree ret;
9190 /* We must force a BLOCK for this level so that, if it is not expanded
9191 later, there is a way to turn off the entire subtree of blocks that
9192 are contained in it. */
9193 keep_next_level ();
9194 ret = c_begin_compound_stmt (true);
9196 c_bindings_start_stmt_expr (c_switch_stack == NULL
9197 ? NULL
9198 : c_switch_stack->bindings);
9200 /* Mark the current statement list as belonging to a statement list. */
9201 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9203 return ret;
9206 /* LOC is the location of the compound statement to which this body
9207 belongs. */
9209 tree
9210 c_finish_stmt_expr (location_t loc, tree body)
9212 tree last, type, tmp, val;
9213 tree *last_p;
9215 body = c_end_compound_stmt (loc, body, true);
9217 c_bindings_end_stmt_expr (c_switch_stack == NULL
9218 ? NULL
9219 : c_switch_stack->bindings);
9221 /* Locate the last statement in BODY. See c_end_compound_stmt
9222 about always returning a BIND_EXPR. */
9223 last_p = &BIND_EXPR_BODY (body);
9224 last = BIND_EXPR_BODY (body);
9226 continue_searching:
9227 if (TREE_CODE (last) == STATEMENT_LIST)
9229 tree_stmt_iterator i;
9231 /* This can happen with degenerate cases like ({ }). No value. */
9232 if (!TREE_SIDE_EFFECTS (last))
9233 return body;
9235 /* If we're supposed to generate side effects warnings, process
9236 all of the statements except the last. */
9237 if (warn_unused_value)
9239 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9241 location_t tloc;
9242 tree t = tsi_stmt (i);
9244 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9245 emit_side_effect_warnings (tloc, t);
9248 else
9249 i = tsi_last (last);
9250 last_p = tsi_stmt_ptr (i);
9251 last = *last_p;
9254 /* If the end of the list is exception related, then the list was split
9255 by a call to push_cleanup. Continue searching. */
9256 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9257 || TREE_CODE (last) == TRY_CATCH_EXPR)
9259 last_p = &TREE_OPERAND (last, 0);
9260 last = *last_p;
9261 goto continue_searching;
9264 if (last == error_mark_node)
9265 return last;
9267 /* In the case that the BIND_EXPR is not necessary, return the
9268 expression out from inside it. */
9269 if (last == BIND_EXPR_BODY (body)
9270 && BIND_EXPR_VARS (body) == NULL)
9272 /* Even if this looks constant, do not allow it in a constant
9273 expression. */
9274 last = c_wrap_maybe_const (last, true);
9275 /* Do not warn if the return value of a statement expression is
9276 unused. */
9277 TREE_NO_WARNING (last) = 1;
9278 return last;
9281 /* Extract the type of said expression. */
9282 type = TREE_TYPE (last);
9284 /* If we're not returning a value at all, then the BIND_EXPR that
9285 we already have is a fine expression to return. */
9286 if (!type || VOID_TYPE_P (type))
9287 return body;
9289 /* Now that we've located the expression containing the value, it seems
9290 silly to make voidify_wrapper_expr repeat the process. Create a
9291 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9292 tmp = create_tmp_var_raw (type, NULL);
9294 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9295 tree_expr_nonnegative_p giving up immediately. */
9296 val = last;
9297 if (TREE_CODE (val) == NOP_EXPR
9298 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9299 val = TREE_OPERAND (val, 0);
9301 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9302 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9305 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9306 SET_EXPR_LOCATION (t, loc);
9307 return t;
9311 /* Begin and end compound statements. This is as simple as pushing
9312 and popping new statement lists from the tree. */
9314 tree
9315 c_begin_compound_stmt (bool do_scope)
9317 tree stmt = push_stmt_list ();
9318 if (do_scope)
9319 push_scope ();
9320 return stmt;
9323 /* End a compound statement. STMT is the statement. LOC is the
9324 location of the compound statement-- this is usually the location
9325 of the opening brace. */
9327 tree
9328 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9330 tree block = NULL;
9332 if (do_scope)
9334 if (c_dialect_objc ())
9335 objc_clear_super_receiver ();
9336 block = pop_scope ();
9339 stmt = pop_stmt_list (stmt);
9340 stmt = c_build_bind_expr (loc, block, stmt);
9342 /* If this compound statement is nested immediately inside a statement
9343 expression, then force a BIND_EXPR to be created. Otherwise we'll
9344 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9345 STATEMENT_LISTs merge, and thus we can lose track of what statement
9346 was really last. */
9347 if (building_stmt_list_p ()
9348 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9349 && TREE_CODE (stmt) != BIND_EXPR)
9351 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9352 TREE_SIDE_EFFECTS (stmt) = 1;
9353 SET_EXPR_LOCATION (stmt, loc);
9356 return stmt;
9359 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9360 when the current scope is exited. EH_ONLY is true when this is not
9361 meant to apply to normal control flow transfer. */
9363 void
9364 push_cleanup (tree decl, tree cleanup, bool eh_only)
9366 enum tree_code code;
9367 tree stmt, list;
9368 bool stmt_expr;
9370 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9371 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9372 add_stmt (stmt);
9373 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9374 list = push_stmt_list ();
9375 TREE_OPERAND (stmt, 0) = list;
9376 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9379 /* Convert scalar to vector for the range of operations. */
9380 static enum stv_conv
9381 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1)
9383 tree type0 = TREE_TYPE (op0);
9384 tree type1 = TREE_TYPE (op1);
9385 bool integer_only_op = false;
9386 enum stv_conv ret = stv_firstarg;
9388 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
9389 || TREE_CODE (type1) == VECTOR_TYPE);
9390 switch (code)
9392 case RSHIFT_EXPR:
9393 case LSHIFT_EXPR:
9394 if (TREE_CODE (type0) == INTEGER_TYPE
9395 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9397 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9399 error_at (loc, "conversion of scalar to vector "
9400 "involves truncation");
9401 return stv_error;
9403 else
9404 return stv_firstarg;
9406 break;
9408 case BIT_IOR_EXPR:
9409 case BIT_XOR_EXPR:
9410 case BIT_AND_EXPR:
9411 integer_only_op = true;
9412 /* ... fall through ... */
9414 case PLUS_EXPR:
9415 case MINUS_EXPR:
9416 case MULT_EXPR:
9417 case TRUNC_DIV_EXPR:
9418 case TRUNC_MOD_EXPR:
9419 case RDIV_EXPR:
9420 if (TREE_CODE (type0) == VECTOR_TYPE)
9422 tree tmp;
9423 ret = stv_secondarg;
9424 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
9425 tmp = type0; type0 = type1; type1 = tmp;
9426 tmp = op0; op0 = op1; op1 = tmp;
9429 if (TREE_CODE (type0) == INTEGER_TYPE
9430 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9432 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9434 error_at (loc, "conversion of scalar to vector "
9435 "involves truncation");
9436 return stv_error;
9438 return ret;
9440 else if (!integer_only_op
9441 /* Allow integer --> real conversion if safe. */
9442 && (TREE_CODE (type0) == REAL_TYPE
9443 || TREE_CODE (type0) == INTEGER_TYPE)
9444 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
9446 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9448 error_at (loc, "conversion of scalar to vector "
9449 "involves truncation");
9450 return stv_error;
9452 return ret;
9454 default:
9455 break;
9458 return stv_nothing;
9461 /* Build a binary-operation expression without default conversions.
9462 CODE is the kind of expression to build.
9463 LOCATION is the operator's location.
9464 This function differs from `build' in several ways:
9465 the data type of the result is computed and recorded in it,
9466 warnings are generated if arg data types are invalid,
9467 special handling for addition and subtraction of pointers is known,
9468 and some optimization is done (operations on narrow ints
9469 are done in the narrower type when that gives the same result).
9470 Constant folding is also done before the result is returned.
9472 Note that the operands will never have enumeral types, or function
9473 or array types, because either they will have the default conversions
9474 performed or they have both just been converted to some other type in which
9475 the arithmetic is to be done. */
9477 tree
9478 build_binary_op (location_t location, enum tree_code code,
9479 tree orig_op0, tree orig_op1, int convert_p)
9481 tree type0, type1, orig_type0, orig_type1;
9482 tree eptype;
9483 enum tree_code code0, code1;
9484 tree op0, op1;
9485 tree ret = error_mark_node;
9486 const char *invalid_op_diag;
9487 bool op0_int_operands, op1_int_operands;
9488 bool int_const, int_const_or_overflow, int_operands;
9490 /* Expression code to give to the expression when it is built.
9491 Normally this is CODE, which is what the caller asked for,
9492 but in some special cases we change it. */
9493 enum tree_code resultcode = code;
9495 /* Data type in which the computation is to be performed.
9496 In the simplest cases this is the common type of the arguments. */
9497 tree result_type = NULL;
9499 /* When the computation is in excess precision, the type of the
9500 final EXCESS_PRECISION_EXPR. */
9501 tree semantic_result_type = NULL;
9503 /* Nonzero means operands have already been type-converted
9504 in whatever way is necessary.
9505 Zero means they need to be converted to RESULT_TYPE. */
9506 int converted = 0;
9508 /* Nonzero means create the expression with this type, rather than
9509 RESULT_TYPE. */
9510 tree build_type = 0;
9512 /* Nonzero means after finally constructing the expression
9513 convert it to this type. */
9514 tree final_type = 0;
9516 /* Nonzero if this is an operation like MIN or MAX which can
9517 safely be computed in short if both args are promoted shorts.
9518 Also implies COMMON.
9519 -1 indicates a bitwise operation; this makes a difference
9520 in the exact conditions for when it is safe to do the operation
9521 in a narrower mode. */
9522 int shorten = 0;
9524 /* Nonzero if this is a comparison operation;
9525 if both args are promoted shorts, compare the original shorts.
9526 Also implies COMMON. */
9527 int short_compare = 0;
9529 /* Nonzero if this is a right-shift operation, which can be computed on the
9530 original short and then promoted if the operand is a promoted short. */
9531 int short_shift = 0;
9533 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9534 int common = 0;
9536 /* True means types are compatible as far as ObjC is concerned. */
9537 bool objc_ok;
9539 /* True means this is an arithmetic operation that may need excess
9540 precision. */
9541 bool may_need_excess_precision;
9543 /* True means this is a boolean operation that converts both its
9544 operands to truth-values. */
9545 bool boolean_op = false;
9547 if (location == UNKNOWN_LOCATION)
9548 location = input_location;
9550 op0 = orig_op0;
9551 op1 = orig_op1;
9553 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9554 if (op0_int_operands)
9555 op0 = remove_c_maybe_const_expr (op0);
9556 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9557 if (op1_int_operands)
9558 op1 = remove_c_maybe_const_expr (op1);
9559 int_operands = (op0_int_operands && op1_int_operands);
9560 if (int_operands)
9562 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9563 && TREE_CODE (orig_op1) == INTEGER_CST);
9564 int_const = (int_const_or_overflow
9565 && !TREE_OVERFLOW (orig_op0)
9566 && !TREE_OVERFLOW (orig_op1));
9568 else
9569 int_const = int_const_or_overflow = false;
9571 /* Do not apply default conversion in mixed vector/scalar expression. */
9572 if (convert_p
9573 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
9574 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
9576 op0 = default_conversion (op0);
9577 op1 = default_conversion (op1);
9580 orig_type0 = type0 = TREE_TYPE (op0);
9581 orig_type1 = type1 = TREE_TYPE (op1);
9583 /* The expression codes of the data types of the arguments tell us
9584 whether the arguments are integers, floating, pointers, etc. */
9585 code0 = TREE_CODE (type0);
9586 code1 = TREE_CODE (type1);
9588 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9589 STRIP_TYPE_NOPS (op0);
9590 STRIP_TYPE_NOPS (op1);
9592 /* If an error was already reported for one of the arguments,
9593 avoid reporting another error. */
9595 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9596 return error_mark_node;
9598 if ((invalid_op_diag
9599 = targetm.invalid_binary_op (code, type0, type1)))
9601 error_at (location, invalid_op_diag);
9602 return error_mark_node;
9605 switch (code)
9607 case PLUS_EXPR:
9608 case MINUS_EXPR:
9609 case MULT_EXPR:
9610 case TRUNC_DIV_EXPR:
9611 case CEIL_DIV_EXPR:
9612 case FLOOR_DIV_EXPR:
9613 case ROUND_DIV_EXPR:
9614 case EXACT_DIV_EXPR:
9615 may_need_excess_precision = true;
9616 break;
9617 default:
9618 may_need_excess_precision = false;
9619 break;
9621 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9623 op0 = TREE_OPERAND (op0, 0);
9624 type0 = TREE_TYPE (op0);
9626 else if (may_need_excess_precision
9627 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9629 type0 = eptype;
9630 op0 = convert (eptype, op0);
9632 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9634 op1 = TREE_OPERAND (op1, 0);
9635 type1 = TREE_TYPE (op1);
9637 else if (may_need_excess_precision
9638 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9640 type1 = eptype;
9641 op1 = convert (eptype, op1);
9644 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9646 /* In case when one of the operands of the binary operation is
9647 a vector and another is a scalar -- convert scalar to vector. */
9648 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
9650 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1);
9652 switch (convert_flag)
9654 case stv_error:
9655 return error_mark_node;
9656 case stv_firstarg:
9658 bool maybe_const = true;
9659 tree sc;
9660 sc = c_fully_fold (op0, false, &maybe_const);
9661 sc = save_expr (sc);
9662 sc = convert (TREE_TYPE (type1), sc);
9663 op0 = build_vector_from_val (type1, sc);
9664 if (!maybe_const)
9665 op0 = c_wrap_maybe_const (op0, true);
9666 orig_type0 = type0 = TREE_TYPE (op0);
9667 code0 = TREE_CODE (type0);
9668 converted = 1;
9669 break;
9671 case stv_secondarg:
9673 bool maybe_const = true;
9674 tree sc;
9675 sc = c_fully_fold (op1, false, &maybe_const);
9676 sc = save_expr (sc);
9677 sc = convert (TREE_TYPE (type0), sc);
9678 op1 = build_vector_from_val (type0, sc);
9679 if (!maybe_const)
9680 op1 = c_wrap_maybe_const (op1, true);
9681 orig_type1 = type1 = TREE_TYPE (op1);
9682 code1 = TREE_CODE (type1);
9683 converted = 1;
9684 break;
9686 default:
9687 break;
9691 switch (code)
9693 case PLUS_EXPR:
9694 /* Handle the pointer + int case. */
9695 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9697 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9698 goto return_build_binary_op;
9700 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9702 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9703 goto return_build_binary_op;
9705 else
9706 common = 1;
9707 break;
9709 case MINUS_EXPR:
9710 /* Subtraction of two similar pointers.
9711 We must subtract them as integers, then divide by object size. */
9712 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9713 && comp_target_types (location, type0, type1))
9715 ret = pointer_diff (location, op0, op1);
9716 goto return_build_binary_op;
9718 /* Handle pointer minus int. Just like pointer plus int. */
9719 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9721 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9722 goto return_build_binary_op;
9724 else
9725 common = 1;
9726 break;
9728 case MULT_EXPR:
9729 common = 1;
9730 break;
9732 case TRUNC_DIV_EXPR:
9733 case CEIL_DIV_EXPR:
9734 case FLOOR_DIV_EXPR:
9735 case ROUND_DIV_EXPR:
9736 case EXACT_DIV_EXPR:
9737 warn_for_div_by_zero (location, op1);
9739 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9740 || code0 == FIXED_POINT_TYPE
9741 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9742 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9743 || code1 == FIXED_POINT_TYPE
9744 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9746 enum tree_code tcode0 = code0, tcode1 = code1;
9748 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9749 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9750 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9751 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9753 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9754 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9755 resultcode = RDIV_EXPR;
9756 else
9757 /* Although it would be tempting to shorten always here, that
9758 loses on some targets, since the modulo instruction is
9759 undefined if the quotient can't be represented in the
9760 computation mode. We shorten only if unsigned or if
9761 dividing by something we know != -1. */
9762 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9763 || (TREE_CODE (op1) == INTEGER_CST
9764 && !integer_all_onesp (op1)));
9765 common = 1;
9767 break;
9769 case BIT_AND_EXPR:
9770 case BIT_IOR_EXPR:
9771 case BIT_XOR_EXPR:
9772 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9773 shorten = -1;
9774 /* Allow vector types which are not floating point types. */
9775 else if (code0 == VECTOR_TYPE
9776 && code1 == VECTOR_TYPE
9777 && !VECTOR_FLOAT_TYPE_P (type0)
9778 && !VECTOR_FLOAT_TYPE_P (type1))
9779 common = 1;
9780 break;
9782 case TRUNC_MOD_EXPR:
9783 case FLOOR_MOD_EXPR:
9784 warn_for_div_by_zero (location, op1);
9786 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9787 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9788 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9789 common = 1;
9790 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9792 /* Although it would be tempting to shorten always here, that loses
9793 on some targets, since the modulo instruction is undefined if the
9794 quotient can't be represented in the computation mode. We shorten
9795 only if unsigned or if dividing by something we know != -1. */
9796 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9797 || (TREE_CODE (op1) == INTEGER_CST
9798 && !integer_all_onesp (op1)));
9799 common = 1;
9801 break;
9803 case TRUTH_ANDIF_EXPR:
9804 case TRUTH_ORIF_EXPR:
9805 case TRUTH_AND_EXPR:
9806 case TRUTH_OR_EXPR:
9807 case TRUTH_XOR_EXPR:
9808 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9809 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9810 || code0 == FIXED_POINT_TYPE)
9811 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9812 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9813 || code1 == FIXED_POINT_TYPE))
9815 /* Result of these operations is always an int,
9816 but that does not mean the operands should be
9817 converted to ints! */
9818 result_type = integer_type_node;
9819 if (op0_int_operands)
9821 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
9822 op0 = remove_c_maybe_const_expr (op0);
9824 else
9825 op0 = c_objc_common_truthvalue_conversion (location, op0);
9826 if (op1_int_operands)
9828 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
9829 op1 = remove_c_maybe_const_expr (op1);
9831 else
9832 op1 = c_objc_common_truthvalue_conversion (location, op1);
9833 converted = 1;
9834 boolean_op = true;
9836 if (code == TRUTH_ANDIF_EXPR)
9838 int_const_or_overflow = (int_operands
9839 && TREE_CODE (orig_op0) == INTEGER_CST
9840 && (op0 == truthvalue_false_node
9841 || TREE_CODE (orig_op1) == INTEGER_CST));
9842 int_const = (int_const_or_overflow
9843 && !TREE_OVERFLOW (orig_op0)
9844 && (op0 == truthvalue_false_node
9845 || !TREE_OVERFLOW (orig_op1)));
9847 else if (code == TRUTH_ORIF_EXPR)
9849 int_const_or_overflow = (int_operands
9850 && TREE_CODE (orig_op0) == INTEGER_CST
9851 && (op0 == truthvalue_true_node
9852 || TREE_CODE (orig_op1) == INTEGER_CST));
9853 int_const = (int_const_or_overflow
9854 && !TREE_OVERFLOW (orig_op0)
9855 && (op0 == truthvalue_true_node
9856 || !TREE_OVERFLOW (orig_op1)));
9858 break;
9860 /* Shift operations: result has same type as first operand;
9861 always convert second operand to int.
9862 Also set SHORT_SHIFT if shifting rightward. */
9864 case RSHIFT_EXPR:
9865 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9866 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9868 result_type = type0;
9869 converted = 1;
9871 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9872 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9873 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9874 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9876 result_type = type0;
9877 converted = 1;
9879 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9880 && code1 == INTEGER_TYPE)
9882 if (TREE_CODE (op1) == INTEGER_CST)
9884 if (tree_int_cst_sgn (op1) < 0)
9886 int_const = false;
9887 if (c_inhibit_evaluation_warnings == 0)
9888 warning (0, "right shift count is negative");
9890 else
9892 if (!integer_zerop (op1))
9893 short_shift = 1;
9895 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9897 int_const = false;
9898 if (c_inhibit_evaluation_warnings == 0)
9899 warning (0, "right shift count >= width of type");
9904 /* Use the type of the value to be shifted. */
9905 result_type = type0;
9906 /* Convert the non vector shift-count to an integer, regardless
9907 of size of value being shifted. */
9908 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9909 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9910 op1 = convert (integer_type_node, op1);
9911 /* Avoid converting op1 to result_type later. */
9912 converted = 1;
9914 break;
9916 case LSHIFT_EXPR:
9917 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9918 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9920 result_type = type0;
9921 converted = 1;
9923 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9924 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9925 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9926 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9928 result_type = type0;
9929 converted = 1;
9931 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9932 && code1 == INTEGER_TYPE)
9934 if (TREE_CODE (op1) == INTEGER_CST)
9936 if (tree_int_cst_sgn (op1) < 0)
9938 int_const = false;
9939 if (c_inhibit_evaluation_warnings == 0)
9940 warning (0, "left shift count is negative");
9943 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9945 int_const = false;
9946 if (c_inhibit_evaluation_warnings == 0)
9947 warning (0, "left shift count >= width of type");
9951 /* Use the type of the value to be shifted. */
9952 result_type = type0;
9953 /* Convert the non vector shift-count to an integer, regardless
9954 of size of value being shifted. */
9955 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9956 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9957 op1 = convert (integer_type_node, op1);
9958 /* Avoid converting op1 to result_type later. */
9959 converted = 1;
9961 break;
9963 case EQ_EXPR:
9964 case NE_EXPR:
9965 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
9967 tree intt;
9968 if (TREE_TYPE (type0) != TREE_TYPE (type1))
9970 error_at (location, "comparing vectors with different "
9971 "element types");
9972 return error_mark_node;
9975 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
9977 error_at (location, "comparing vectors with different "
9978 "number of elements");
9979 return error_mark_node;
9982 /* Always construct signed integer vector type. */
9983 intt = c_common_type_for_size (GET_MODE_BITSIZE
9984 (TYPE_MODE (TREE_TYPE (type0))), 0);
9985 result_type = build_opaque_vector_type (intt,
9986 TYPE_VECTOR_SUBPARTS (type0));
9987 converted = 1;
9988 break;
9990 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9991 warning_at (location,
9992 OPT_Wfloat_equal,
9993 "comparing floating point with == or != is unsafe");
9994 /* Result of comparison is always int,
9995 but don't convert the args to int! */
9996 build_type = integer_type_node;
9997 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9998 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9999 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10000 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10001 short_compare = 1;
10002 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10004 if (TREE_CODE (op0) == ADDR_EXPR
10005 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10007 if (code == EQ_EXPR)
10008 warning_at (location,
10009 OPT_Waddress,
10010 "the comparison will always evaluate as %<false%> "
10011 "for the address of %qD will never be NULL",
10012 TREE_OPERAND (op0, 0));
10013 else
10014 warning_at (location,
10015 OPT_Waddress,
10016 "the comparison will always evaluate as %<true%> "
10017 "for the address of %qD will never be NULL",
10018 TREE_OPERAND (op0, 0));
10020 result_type = type0;
10022 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10024 if (TREE_CODE (op1) == ADDR_EXPR
10025 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10027 if (code == EQ_EXPR)
10028 warning_at (location,
10029 OPT_Waddress,
10030 "the comparison will always evaluate as %<false%> "
10031 "for the address of %qD will never be NULL",
10032 TREE_OPERAND (op1, 0));
10033 else
10034 warning_at (location,
10035 OPT_Waddress,
10036 "the comparison will always evaluate as %<true%> "
10037 "for the address of %qD will never be NULL",
10038 TREE_OPERAND (op1, 0));
10040 result_type = type1;
10042 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10044 tree tt0 = TREE_TYPE (type0);
10045 tree tt1 = TREE_TYPE (type1);
10046 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10047 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10048 addr_space_t as_common = ADDR_SPACE_GENERIC;
10050 /* Anything compares with void *. void * compares with anything.
10051 Otherwise, the targets must be compatible
10052 and both must be object or both incomplete. */
10053 if (comp_target_types (location, type0, type1))
10054 result_type = common_pointer_type (type0, type1);
10055 else if (!addr_space_superset (as0, as1, &as_common))
10057 error_at (location, "comparison of pointers to "
10058 "disjoint address spaces");
10059 return error_mark_node;
10061 else if (VOID_TYPE_P (tt0))
10063 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10064 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10065 "comparison of %<void *%> with function pointer");
10067 else if (VOID_TYPE_P (tt1))
10069 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10070 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10071 "comparison of %<void *%> with function pointer");
10073 else
10074 /* Avoid warning about the volatile ObjC EH puts on decls. */
10075 if (!objc_ok)
10076 pedwarn (location, 0,
10077 "comparison of distinct pointer types lacks a cast");
10079 if (result_type == NULL_TREE)
10081 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10082 result_type = build_pointer_type
10083 (build_qualified_type (void_type_node, qual));
10086 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10088 result_type = type0;
10089 pedwarn (location, 0, "comparison between pointer and integer");
10091 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10093 result_type = type1;
10094 pedwarn (location, 0, "comparison between pointer and integer");
10096 break;
10098 case LE_EXPR:
10099 case GE_EXPR:
10100 case LT_EXPR:
10101 case GT_EXPR:
10102 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10104 tree intt;
10105 if (TREE_TYPE (type0) != TREE_TYPE (type1))
10107 error_at (location, "comparing vectors with different "
10108 "element types");
10109 return error_mark_node;
10112 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10114 error_at (location, "comparing vectors with different "
10115 "number of elements");
10116 return error_mark_node;
10119 /* Always construct signed integer vector type. */
10120 intt = c_common_type_for_size (GET_MODE_BITSIZE
10121 (TYPE_MODE (TREE_TYPE (type0))), 0);
10122 result_type = build_opaque_vector_type (intt,
10123 TYPE_VECTOR_SUBPARTS (type0));
10124 converted = 1;
10125 break;
10127 build_type = integer_type_node;
10128 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10129 || code0 == FIXED_POINT_TYPE)
10130 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10131 || code1 == FIXED_POINT_TYPE))
10132 short_compare = 1;
10133 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10135 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10136 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10137 addr_space_t as_common;
10139 if (comp_target_types (location, type0, type1))
10141 result_type = common_pointer_type (type0, type1);
10142 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10143 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10144 pedwarn (location, 0,
10145 "comparison of complete and incomplete pointers");
10146 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10147 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10148 "ordered comparisons of pointers to functions");
10149 else if (null_pointer_constant_p (orig_op0)
10150 || null_pointer_constant_p (orig_op1))
10151 warning_at (location, OPT_Wextra,
10152 "ordered comparison of pointer with null pointer");
10155 else if (!addr_space_superset (as0, as1, &as_common))
10157 error_at (location, "comparison of pointers to "
10158 "disjoint address spaces");
10159 return error_mark_node;
10161 else
10163 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10164 result_type = build_pointer_type
10165 (build_qualified_type (void_type_node, qual));
10166 pedwarn (location, 0,
10167 "comparison of distinct pointer types lacks a cast");
10170 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10172 result_type = type0;
10173 if (pedantic)
10174 pedwarn (location, OPT_Wpedantic,
10175 "ordered comparison of pointer with integer zero");
10176 else if (extra_warnings)
10177 warning_at (location, OPT_Wextra,
10178 "ordered comparison of pointer with integer zero");
10180 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10182 result_type = type1;
10183 if (pedantic)
10184 pedwarn (location, OPT_Wpedantic,
10185 "ordered comparison of pointer with integer zero");
10186 else if (extra_warnings)
10187 warning_at (location, OPT_Wextra,
10188 "ordered comparison of pointer with integer zero");
10190 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10192 result_type = type0;
10193 pedwarn (location, 0, "comparison between pointer and integer");
10195 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10197 result_type = type1;
10198 pedwarn (location, 0, "comparison between pointer and integer");
10200 break;
10202 default:
10203 gcc_unreachable ();
10206 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10207 return error_mark_node;
10209 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10210 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10211 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10212 TREE_TYPE (type1))))
10214 binary_op_error (location, code, type0, type1);
10215 return error_mark_node;
10218 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10219 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10221 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10222 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10224 bool first_complex = (code0 == COMPLEX_TYPE);
10225 bool second_complex = (code1 == COMPLEX_TYPE);
10226 int none_complex = (!first_complex && !second_complex);
10228 if (shorten || common || short_compare)
10230 result_type = c_common_type (type0, type1);
10231 do_warn_double_promotion (result_type, type0, type1,
10232 "implicit conversion from %qT to %qT "
10233 "to match other operand of binary "
10234 "expression",
10235 location);
10236 if (result_type == error_mark_node)
10237 return error_mark_node;
10240 if (first_complex != second_complex
10241 && (code == PLUS_EXPR
10242 || code == MINUS_EXPR
10243 || code == MULT_EXPR
10244 || (code == TRUNC_DIV_EXPR && first_complex))
10245 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10246 && flag_signed_zeros)
10248 /* An operation on mixed real/complex operands must be
10249 handled specially, but the language-independent code can
10250 more easily optimize the plain complex arithmetic if
10251 -fno-signed-zeros. */
10252 tree real_type = TREE_TYPE (result_type);
10253 tree real, imag;
10254 if (type0 != orig_type0 || type1 != orig_type1)
10256 gcc_assert (may_need_excess_precision && common);
10257 semantic_result_type = c_common_type (orig_type0, orig_type1);
10259 if (first_complex)
10261 if (TREE_TYPE (op0) != result_type)
10262 op0 = convert_and_check (result_type, op0);
10263 if (TREE_TYPE (op1) != real_type)
10264 op1 = convert_and_check (real_type, op1);
10266 else
10268 if (TREE_TYPE (op0) != real_type)
10269 op0 = convert_and_check (real_type, op0);
10270 if (TREE_TYPE (op1) != result_type)
10271 op1 = convert_and_check (result_type, op1);
10273 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10274 return error_mark_node;
10275 if (first_complex)
10277 op0 = c_save_expr (op0);
10278 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10279 op0, 1);
10280 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10281 op0, 1);
10282 switch (code)
10284 case MULT_EXPR:
10285 case TRUNC_DIV_EXPR:
10286 op1 = c_save_expr (op1);
10287 imag = build2 (resultcode, real_type, imag, op1);
10288 /* Fall through. */
10289 case PLUS_EXPR:
10290 case MINUS_EXPR:
10291 real = build2 (resultcode, real_type, real, op1);
10292 break;
10293 default:
10294 gcc_unreachable();
10297 else
10299 op1 = c_save_expr (op1);
10300 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10301 op1, 1);
10302 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10303 op1, 1);
10304 switch (code)
10306 case MULT_EXPR:
10307 op0 = c_save_expr (op0);
10308 imag = build2 (resultcode, real_type, op0, imag);
10309 /* Fall through. */
10310 case PLUS_EXPR:
10311 real = build2 (resultcode, real_type, op0, real);
10312 break;
10313 case MINUS_EXPR:
10314 real = build2 (resultcode, real_type, op0, real);
10315 imag = build1 (NEGATE_EXPR, real_type, imag);
10316 break;
10317 default:
10318 gcc_unreachable();
10321 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10322 goto return_build_binary_op;
10325 /* For certain operations (which identify themselves by shorten != 0)
10326 if both args were extended from the same smaller type,
10327 do the arithmetic in that type and then extend.
10329 shorten !=0 and !=1 indicates a bitwise operation.
10330 For them, this optimization is safe only if
10331 both args are zero-extended or both are sign-extended.
10332 Otherwise, we might change the result.
10333 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10334 but calculated in (unsigned short) it would be (unsigned short)-1. */
10336 if (shorten && none_complex)
10338 final_type = result_type;
10339 result_type = shorten_binary_op (result_type, op0, op1,
10340 shorten == -1);
10343 /* Shifts can be shortened if shifting right. */
10345 if (short_shift)
10347 int unsigned_arg;
10348 tree arg0 = get_narrower (op0, &unsigned_arg);
10350 final_type = result_type;
10352 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10353 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10355 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10356 && tree_int_cst_sgn (op1) > 0
10357 /* We can shorten only if the shift count is less than the
10358 number of bits in the smaller type size. */
10359 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10360 /* We cannot drop an unsigned shift after sign-extension. */
10361 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10363 /* Do an unsigned shift if the operand was zero-extended. */
10364 result_type
10365 = c_common_signed_or_unsigned_type (unsigned_arg,
10366 TREE_TYPE (arg0));
10367 /* Convert value-to-be-shifted to that type. */
10368 if (TREE_TYPE (op0) != result_type)
10369 op0 = convert (result_type, op0);
10370 converted = 1;
10374 /* Comparison operations are shortened too but differently.
10375 They identify themselves by setting short_compare = 1. */
10377 if (short_compare)
10379 /* Don't write &op0, etc., because that would prevent op0
10380 from being kept in a register.
10381 Instead, make copies of the our local variables and
10382 pass the copies by reference, then copy them back afterward. */
10383 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10384 enum tree_code xresultcode = resultcode;
10385 tree val
10386 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10388 if (val != 0)
10390 ret = val;
10391 goto return_build_binary_op;
10394 op0 = xop0, op1 = xop1;
10395 converted = 1;
10396 resultcode = xresultcode;
10398 if (c_inhibit_evaluation_warnings == 0)
10400 bool op0_maybe_const = true;
10401 bool op1_maybe_const = true;
10402 tree orig_op0_folded, orig_op1_folded;
10404 if (in_late_binary_op)
10406 orig_op0_folded = orig_op0;
10407 orig_op1_folded = orig_op1;
10409 else
10411 /* Fold for the sake of possible warnings, as in
10412 build_conditional_expr. This requires the
10413 "original" values to be folded, not just op0 and
10414 op1. */
10415 c_inhibit_evaluation_warnings++;
10416 op0 = c_fully_fold (op0, require_constant_value,
10417 &op0_maybe_const);
10418 op1 = c_fully_fold (op1, require_constant_value,
10419 &op1_maybe_const);
10420 c_inhibit_evaluation_warnings--;
10421 orig_op0_folded = c_fully_fold (orig_op0,
10422 require_constant_value,
10423 NULL);
10424 orig_op1_folded = c_fully_fold (orig_op1,
10425 require_constant_value,
10426 NULL);
10429 if (warn_sign_compare)
10430 warn_for_sign_compare (location, orig_op0_folded,
10431 orig_op1_folded, op0, op1,
10432 result_type, resultcode);
10433 if (!in_late_binary_op && !int_operands)
10435 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10436 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10437 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10438 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10444 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10445 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10446 Then the expression will be built.
10447 It will be given type FINAL_TYPE if that is nonzero;
10448 otherwise, it will be given type RESULT_TYPE. */
10450 if (!result_type)
10452 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10453 return error_mark_node;
10456 if (build_type == NULL_TREE)
10458 build_type = result_type;
10459 if ((type0 != orig_type0 || type1 != orig_type1)
10460 && !boolean_op)
10462 gcc_assert (may_need_excess_precision && common);
10463 semantic_result_type = c_common_type (orig_type0, orig_type1);
10467 if (!converted)
10469 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10470 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10472 /* This can happen if one operand has a vector type, and the other
10473 has a different type. */
10474 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10475 return error_mark_node;
10478 /* Treat expressions in initializers specially as they can't trap. */
10479 if (int_const_or_overflow)
10480 ret = (require_constant_value
10481 ? fold_build2_initializer_loc (location, resultcode, build_type,
10482 op0, op1)
10483 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10484 else
10485 ret = build2 (resultcode, build_type, op0, op1);
10486 if (final_type != 0)
10487 ret = convert (final_type, ret);
10489 return_build_binary_op:
10490 gcc_assert (ret != error_mark_node);
10491 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10492 ret = (int_operands
10493 ? note_integer_operands (ret)
10494 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10495 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10496 && !in_late_binary_op)
10497 ret = note_integer_operands (ret);
10498 if (semantic_result_type)
10499 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10500 protected_set_expr_location (ret, location);
10501 return ret;
10505 /* Convert EXPR to be a truth-value, validating its type for this
10506 purpose. LOCATION is the source location for the expression. */
10508 tree
10509 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10511 bool int_const, int_operands;
10513 switch (TREE_CODE (TREE_TYPE (expr)))
10515 case ARRAY_TYPE:
10516 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10517 return error_mark_node;
10519 case RECORD_TYPE:
10520 error_at (location, "used struct type value where scalar is required");
10521 return error_mark_node;
10523 case UNION_TYPE:
10524 error_at (location, "used union type value where scalar is required");
10525 return error_mark_node;
10527 case VOID_TYPE:
10528 error_at (location, "void value not ignored as it ought to be");
10529 return error_mark_node;
10531 case FUNCTION_TYPE:
10532 gcc_unreachable ();
10534 case VECTOR_TYPE:
10535 error_at (location, "used vector type where scalar is required");
10536 return error_mark_node;
10538 default:
10539 break;
10542 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10543 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10544 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
10546 expr = remove_c_maybe_const_expr (expr);
10547 expr = build2 (NE_EXPR, integer_type_node, expr,
10548 convert (TREE_TYPE (expr), integer_zero_node));
10549 expr = note_integer_operands (expr);
10551 else
10552 /* ??? Should we also give an error for vectors rather than leaving
10553 those to give errors later? */
10554 expr = c_common_truthvalue_conversion (location, expr);
10556 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10558 if (TREE_OVERFLOW (expr))
10559 return expr;
10560 else
10561 return note_integer_operands (expr);
10563 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10564 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10565 return expr;
10569 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10570 required. */
10572 tree
10573 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10575 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10577 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10578 /* Executing a compound literal inside a function reinitializes
10579 it. */
10580 if (!TREE_STATIC (decl))
10581 *se = true;
10582 return decl;
10584 else
10585 return expr;
10588 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10590 tree
10591 c_begin_omp_parallel (void)
10593 tree block;
10595 keep_next_level ();
10596 block = c_begin_compound_stmt (true);
10598 return block;
10601 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10602 statement. LOC is the location of the OMP_PARALLEL. */
10604 tree
10605 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10607 tree stmt;
10609 block = c_end_compound_stmt (loc, block, true);
10611 stmt = make_node (OMP_PARALLEL);
10612 TREE_TYPE (stmt) = void_type_node;
10613 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10614 OMP_PARALLEL_BODY (stmt) = block;
10615 SET_EXPR_LOCATION (stmt, loc);
10617 return add_stmt (stmt);
10620 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10622 tree
10623 c_begin_omp_task (void)
10625 tree block;
10627 keep_next_level ();
10628 block = c_begin_compound_stmt (true);
10630 return block;
10633 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10634 statement. LOC is the location of the #pragma. */
10636 tree
10637 c_finish_omp_task (location_t loc, tree clauses, tree block)
10639 tree stmt;
10641 block = c_end_compound_stmt (loc, block, true);
10643 stmt = make_node (OMP_TASK);
10644 TREE_TYPE (stmt) = void_type_node;
10645 OMP_TASK_CLAUSES (stmt) = clauses;
10646 OMP_TASK_BODY (stmt) = block;
10647 SET_EXPR_LOCATION (stmt, loc);
10649 return add_stmt (stmt);
10652 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10653 Remove any elements from the list that are invalid. */
10655 tree
10656 c_finish_omp_clauses (tree clauses)
10658 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10659 tree c, t, *pc = &clauses;
10660 const char *name;
10662 bitmap_obstack_initialize (NULL);
10663 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10664 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10665 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10667 for (pc = &clauses, c = clauses; c ; c = *pc)
10669 bool remove = false;
10670 bool need_complete = false;
10671 bool need_implicitly_determined = false;
10673 switch (OMP_CLAUSE_CODE (c))
10675 case OMP_CLAUSE_SHARED:
10676 name = "shared";
10677 need_implicitly_determined = true;
10678 goto check_dup_generic;
10680 case OMP_CLAUSE_PRIVATE:
10681 name = "private";
10682 need_complete = true;
10683 need_implicitly_determined = true;
10684 goto check_dup_generic;
10686 case OMP_CLAUSE_REDUCTION:
10687 name = "reduction";
10688 need_implicitly_determined = true;
10689 t = OMP_CLAUSE_DECL (c);
10690 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10691 || POINTER_TYPE_P (TREE_TYPE (t)))
10693 error_at (OMP_CLAUSE_LOCATION (c),
10694 "%qE has invalid type for %<reduction%>", t);
10695 remove = true;
10697 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10699 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10700 const char *r_name = NULL;
10702 switch (r_code)
10704 case PLUS_EXPR:
10705 case MULT_EXPR:
10706 case MINUS_EXPR:
10707 case MIN_EXPR:
10708 case MAX_EXPR:
10709 break;
10710 case BIT_AND_EXPR:
10711 r_name = "&";
10712 break;
10713 case BIT_XOR_EXPR:
10714 r_name = "^";
10715 break;
10716 case BIT_IOR_EXPR:
10717 r_name = "|";
10718 break;
10719 case TRUTH_ANDIF_EXPR:
10720 r_name = "&&";
10721 break;
10722 case TRUTH_ORIF_EXPR:
10723 r_name = "||";
10724 break;
10725 default:
10726 gcc_unreachable ();
10728 if (r_name)
10730 error_at (OMP_CLAUSE_LOCATION (c),
10731 "%qE has invalid type for %<reduction(%s)%>",
10732 t, r_name);
10733 remove = true;
10736 goto check_dup_generic;
10738 case OMP_CLAUSE_COPYPRIVATE:
10739 name = "copyprivate";
10740 goto check_dup_generic;
10742 case OMP_CLAUSE_COPYIN:
10743 name = "copyin";
10744 t = OMP_CLAUSE_DECL (c);
10745 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10747 error_at (OMP_CLAUSE_LOCATION (c),
10748 "%qE must be %<threadprivate%> for %<copyin%>", t);
10749 remove = true;
10751 goto check_dup_generic;
10753 check_dup_generic:
10754 t = OMP_CLAUSE_DECL (c);
10755 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10757 error_at (OMP_CLAUSE_LOCATION (c),
10758 "%qE is not a variable in clause %qs", t, name);
10759 remove = true;
10761 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10762 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10763 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10765 error_at (OMP_CLAUSE_LOCATION (c),
10766 "%qE appears more than once in data clauses", t);
10767 remove = true;
10769 else
10770 bitmap_set_bit (&generic_head, DECL_UID (t));
10771 break;
10773 case OMP_CLAUSE_FIRSTPRIVATE:
10774 name = "firstprivate";
10775 t = OMP_CLAUSE_DECL (c);
10776 need_complete = true;
10777 need_implicitly_determined = true;
10778 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10780 error_at (OMP_CLAUSE_LOCATION (c),
10781 "%qE is not a variable in clause %<firstprivate%>", t);
10782 remove = true;
10784 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10785 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10787 error_at (OMP_CLAUSE_LOCATION (c),
10788 "%qE appears more than once in data clauses", t);
10789 remove = true;
10791 else
10792 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10793 break;
10795 case OMP_CLAUSE_LASTPRIVATE:
10796 name = "lastprivate";
10797 t = OMP_CLAUSE_DECL (c);
10798 need_complete = true;
10799 need_implicitly_determined = true;
10800 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10802 error_at (OMP_CLAUSE_LOCATION (c),
10803 "%qE is not a variable in clause %<lastprivate%>", t);
10804 remove = true;
10806 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10807 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10809 error_at (OMP_CLAUSE_LOCATION (c),
10810 "%qE appears more than once in data clauses", t);
10811 remove = true;
10813 else
10814 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10815 break;
10817 case OMP_CLAUSE_IF:
10818 case OMP_CLAUSE_NUM_THREADS:
10819 case OMP_CLAUSE_SCHEDULE:
10820 case OMP_CLAUSE_NOWAIT:
10821 case OMP_CLAUSE_ORDERED:
10822 case OMP_CLAUSE_DEFAULT:
10823 case OMP_CLAUSE_UNTIED:
10824 case OMP_CLAUSE_COLLAPSE:
10825 case OMP_CLAUSE_FINAL:
10826 case OMP_CLAUSE_MERGEABLE:
10827 pc = &OMP_CLAUSE_CHAIN (c);
10828 continue;
10830 default:
10831 gcc_unreachable ();
10834 if (!remove)
10836 t = OMP_CLAUSE_DECL (c);
10838 if (need_complete)
10840 t = require_complete_type (t);
10841 if (t == error_mark_node)
10842 remove = true;
10845 if (need_implicitly_determined)
10847 const char *share_name = NULL;
10849 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10850 share_name = "threadprivate";
10851 else switch (c_omp_predetermined_sharing (t))
10853 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10854 break;
10855 case OMP_CLAUSE_DEFAULT_SHARED:
10856 /* const vars may be specified in firstprivate clause. */
10857 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10858 && TREE_READONLY (t))
10859 break;
10860 share_name = "shared";
10861 break;
10862 case OMP_CLAUSE_DEFAULT_PRIVATE:
10863 share_name = "private";
10864 break;
10865 default:
10866 gcc_unreachable ();
10868 if (share_name)
10870 error_at (OMP_CLAUSE_LOCATION (c),
10871 "%qE is predetermined %qs for %qs",
10872 t, share_name, name);
10873 remove = true;
10878 if (remove)
10879 *pc = OMP_CLAUSE_CHAIN (c);
10880 else
10881 pc = &OMP_CLAUSE_CHAIN (c);
10884 bitmap_obstack_release (NULL);
10885 return clauses;
10888 /* Create a transaction node. */
10890 tree
10891 c_finish_transaction (location_t loc, tree block, int flags)
10893 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
10894 if (flags & TM_STMT_ATTR_OUTER)
10895 TRANSACTION_EXPR_OUTER (stmt) = 1;
10896 if (flags & TM_STMT_ATTR_RELAXED)
10897 TRANSACTION_EXPR_RELAXED (stmt) = 1;
10898 return add_stmt (stmt);
10901 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10902 down to the element type of an array. */
10904 tree
10905 c_build_qualified_type (tree type, int type_quals)
10907 if (type == error_mark_node)
10908 return type;
10910 if (TREE_CODE (type) == ARRAY_TYPE)
10912 tree t;
10913 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10914 type_quals);
10916 /* See if we already have an identically qualified type. */
10917 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10919 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10920 && TYPE_NAME (t) == TYPE_NAME (type)
10921 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10922 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10923 TYPE_ATTRIBUTES (type)))
10924 break;
10926 if (!t)
10928 tree domain = TYPE_DOMAIN (type);
10930 t = build_variant_type_copy (type);
10931 TREE_TYPE (t) = element_type;
10933 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10934 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10935 SET_TYPE_STRUCTURAL_EQUALITY (t);
10936 else if (TYPE_CANONICAL (element_type) != element_type
10937 || (domain && TYPE_CANONICAL (domain) != domain))
10939 tree unqualified_canon
10940 = build_array_type (TYPE_CANONICAL (element_type),
10941 domain? TYPE_CANONICAL (domain)
10942 : NULL_TREE);
10943 TYPE_CANONICAL (t)
10944 = c_build_qualified_type (unqualified_canon, type_quals);
10946 else
10947 TYPE_CANONICAL (t) = t;
10949 return t;
10952 /* A restrict-qualified pointer type must be a pointer to object or
10953 incomplete type. Note that the use of POINTER_TYPE_P also allows
10954 REFERENCE_TYPEs, which is appropriate for C++. */
10955 if ((type_quals & TYPE_QUAL_RESTRICT)
10956 && (!POINTER_TYPE_P (type)
10957 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10959 error ("invalid use of %<restrict%>");
10960 type_quals &= ~TYPE_QUAL_RESTRICT;
10963 return build_qualified_type (type, type_quals);
10966 /* Build a VA_ARG_EXPR for the C parser. */
10968 tree
10969 c_build_va_arg (location_t loc, tree expr, tree type)
10971 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10972 warning_at (loc, OPT_Wc___compat,
10973 "C++ requires promoted type, not enum type, in %<va_arg%>");
10974 return build_va_arg (loc, expr, type);