In gcc/objc/: 2010-11-08 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / gcc / c-typeck.c
blob7f448ddf2ba8f9fffa709356a17ee5415d8ad346
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 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "langhooks.h"
34 #include "c-tree.h"
35 #include "c-lang.h"
36 #include "flags.h"
37 #include "output.h"
38 #include "toplev.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "tree-iterator.h"
42 #include "bitmap.h"
43 #include "gimple.h"
45 /* Possible cases of implicit bad conversions. Used to select
46 diagnostic messages in convert_for_assignment. */
47 enum impl_conv {
48 ic_argpass,
49 ic_assign,
50 ic_init,
51 ic_return
54 /* Whether we are building a boolean conversion inside
55 convert_for_assignment, or some other late binary operation. If
56 build_binary_op is called (from code shared with C++) in this case,
57 then the operands have already been folded and the result will not
58 be folded again, so C_MAYBE_CONST_EXPR should not be generated. */
59 bool in_late_binary_op;
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 /* Nonzero if we've already printed a "missing braces around initializer"
71 message within this initializer. */
72 static int missing_braces_mentioned;
74 static int require_constant_value;
75 static int require_constant_elements;
77 static bool null_pointer_constant_p (const_tree);
78 static tree qualify_type (tree, tree);
79 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
80 bool *);
81 static int comp_target_types (location_t, tree, tree);
82 static int function_types_compatible_p (const_tree, const_tree, bool *,
83 bool *);
84 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
85 static tree lookup_field (tree, tree);
86 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
87 tree);
88 static tree pointer_diff (location_t, tree, tree);
89 static tree convert_for_assignment (location_t, tree, tree, tree,
90 enum impl_conv, bool, tree, tree, int);
91 static tree valid_compound_expr_initializer (tree, tree);
92 static void push_string (const char *);
93 static void push_member_name (tree);
94 static int spelling_length (void);
95 static char *print_spelling (char *);
96 static void warning_init (int, const char *);
97 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
98 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
99 struct obstack *);
100 static void output_pending_init_elements (int, struct obstack *);
101 static int set_designator (int, struct obstack *);
102 static void push_range_stack (tree, struct obstack *);
103 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
104 static void set_nonincremental_init (struct obstack *);
105 static void set_nonincremental_init_from_string (tree, struct obstack *);
106 static tree find_init_member (tree, struct obstack *);
107 static void readonly_error (tree, enum lvalue_use);
108 static void readonly_warning (tree, enum lvalue_use);
109 static int lvalue_or_else (const_tree, enum lvalue_use);
110 static void record_maybe_used_decl (tree);
111 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
113 /* Return true if EXP is a null pointer constant, false otherwise. */
115 static bool
116 null_pointer_constant_p (const_tree expr)
118 /* This should really operate on c_expr structures, but they aren't
119 yet available everywhere required. */
120 tree type = TREE_TYPE (expr);
121 return (TREE_CODE (expr) == INTEGER_CST
122 && !TREE_OVERFLOW (expr)
123 && integer_zerop (expr)
124 && (INTEGRAL_TYPE_P (type)
125 || (TREE_CODE (type) == POINTER_TYPE
126 && VOID_TYPE_P (TREE_TYPE (type))
127 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
130 /* EXPR may appear in an unevaluated part of an integer constant
131 expression, but not in an evaluated part. Wrap it in a
132 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
133 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
135 static tree
136 note_integer_operands (tree expr)
138 tree ret;
139 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
141 ret = copy_node (expr);
142 TREE_OVERFLOW (ret) = 1;
144 else
146 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
147 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
149 return ret;
152 /* Having checked whether EXPR may appear in an unevaluated part of an
153 integer constant expression and found that it may, remove any
154 C_MAYBE_CONST_EXPR noting this fact and return the resulting
155 expression. */
157 static inline tree
158 remove_c_maybe_const_expr (tree expr)
160 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
161 return C_MAYBE_CONST_EXPR_EXPR (expr);
162 else
163 return expr;
166 \f/* This is a cache to hold if two types are compatible or not. */
168 struct tagged_tu_seen_cache {
169 const struct tagged_tu_seen_cache * next;
170 const_tree t1;
171 const_tree t2;
172 /* The return value of tagged_types_tu_compatible_p if we had seen
173 these two types already. */
174 int val;
177 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
178 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
180 /* Do `exp = require_complete_type (exp);' to make sure exp
181 does not have an incomplete type. (That includes void types.) */
183 tree
184 require_complete_type (tree value)
186 tree type = TREE_TYPE (value);
188 if (value == error_mark_node || type == error_mark_node)
189 return error_mark_node;
191 /* First, detect a valid value with a complete type. */
192 if (COMPLETE_TYPE_P (type))
193 return value;
195 c_incomplete_type_error (value, type);
196 return error_mark_node;
199 /* Print an error message for invalid use of an incomplete type.
200 VALUE is the expression that was used (or 0 if that isn't known)
201 and TYPE is the type that was invalid. */
203 void
204 c_incomplete_type_error (const_tree value, const_tree type)
206 const char *type_code_string;
208 /* Avoid duplicate error message. */
209 if (TREE_CODE (type) == ERROR_MARK)
210 return;
212 if (value != 0 && (TREE_CODE (value) == VAR_DECL
213 || TREE_CODE (value) == PARM_DECL))
214 error ("%qD has an incomplete type", value);
215 else
217 retry:
218 /* We must print an error message. Be clever about what it says. */
220 switch (TREE_CODE (type))
222 case RECORD_TYPE:
223 type_code_string = "struct";
224 break;
226 case UNION_TYPE:
227 type_code_string = "union";
228 break;
230 case ENUMERAL_TYPE:
231 type_code_string = "enum";
232 break;
234 case VOID_TYPE:
235 error ("invalid use of void expression");
236 return;
238 case ARRAY_TYPE:
239 if (TYPE_DOMAIN (type))
241 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
243 error ("invalid use of flexible array member");
244 return;
246 type = TREE_TYPE (type);
247 goto retry;
249 error ("invalid use of array with unspecified bounds");
250 return;
252 default:
253 gcc_unreachable ();
256 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
257 error ("invalid use of undefined type %<%s %E%>",
258 type_code_string, TYPE_NAME (type));
259 else
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
261 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
265 /* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
268 tree
269 c_type_promotes_to (tree type)
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 return double_type_node;
274 if (c_promoting_integer_type_p (type))
276 /* Preserve unsignedness if not really getting any wider. */
277 if (TYPE_UNSIGNED (type)
278 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
279 return unsigned_type_node;
280 return integer_type_node;
283 return type;
286 /* Return true if between two named address spaces, whether there is a superset
287 named address space that encompasses both address spaces. If there is a
288 superset, return which address space is the superset. */
290 static bool
291 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
293 if (as1 == as2)
295 *common = as1;
296 return true;
298 else if (targetm.addr_space.subset_p (as1, as2))
300 *common = as2;
301 return true;
303 else if (targetm.addr_space.subset_p (as2, as1))
305 *common = as1;
306 return true;
308 else
309 return false;
312 /* Return a variant of TYPE which has all the type qualifiers of LIKE
313 as well as those of TYPE. */
315 static tree
316 qualify_type (tree type, tree like)
318 addr_space_t as_type = TYPE_ADDR_SPACE (type);
319 addr_space_t as_like = TYPE_ADDR_SPACE (like);
320 addr_space_t as_common;
322 /* If the two named address spaces are different, determine the common
323 superset address space. If there isn't one, raise an error. */
324 if (!addr_space_superset (as_type, as_like, &as_common))
326 as_common = as_type;
327 error ("%qT and %qT are in disjoint named address spaces",
328 type, like);
331 return c_build_qualified_type (type,
332 TYPE_QUALS_NO_ADDR_SPACE (type)
333 | TYPE_QUALS_NO_ADDR_SPACE (like)
334 | ENCODE_QUAL_ADDR_SPACE (as_common));
337 /* Return true iff the given tree T is a variable length array. */
339 bool
340 c_vla_type_p (const_tree t)
342 if (TREE_CODE (t) == ARRAY_TYPE
343 && C_TYPE_VARIABLE_SIZE (t))
344 return true;
345 return false;
348 /* Return the composite type of two compatible types.
350 We assume that comptypes has already been done and returned
351 nonzero; if that isn't so, this may crash. In particular, we
352 assume that qualifiers match. */
354 tree
355 composite_type (tree t1, tree t2)
357 enum tree_code code1;
358 enum tree_code code2;
359 tree attributes;
361 /* Save time if the two types are the same. */
363 if (t1 == t2) return t1;
365 /* If one type is nonsense, use the other. */
366 if (t1 == error_mark_node)
367 return t2;
368 if (t2 == error_mark_node)
369 return t1;
371 code1 = TREE_CODE (t1);
372 code2 = TREE_CODE (t2);
374 /* Merge the attributes. */
375 attributes = targetm.merge_type_attributes (t1, t2);
377 /* If one is an enumerated type and the other is the compatible
378 integer type, the composite type might be either of the two
379 (DR#013 question 3). For consistency, use the enumerated type as
380 the composite type. */
382 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
383 return t1;
384 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
385 return t2;
387 gcc_assert (code1 == code2);
389 switch (code1)
391 case POINTER_TYPE:
392 /* For two pointers, do this recursively on the target type. */
394 tree pointed_to_1 = TREE_TYPE (t1);
395 tree pointed_to_2 = TREE_TYPE (t2);
396 tree target = composite_type (pointed_to_1, pointed_to_2);
397 t1 = build_pointer_type (target);
398 t1 = build_type_attribute_variant (t1, attributes);
399 return qualify_type (t1, t2);
402 case ARRAY_TYPE:
404 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
405 int quals;
406 tree unqual_elt;
407 tree d1 = TYPE_DOMAIN (t1);
408 tree d2 = TYPE_DOMAIN (t2);
409 bool d1_variable, d2_variable;
410 bool d1_zero, d2_zero;
411 bool t1_complete, t2_complete;
413 /* We should not have any type quals on arrays at all. */
414 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
415 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
417 t1_complete = COMPLETE_TYPE_P (t1);
418 t2_complete = COMPLETE_TYPE_P (t2);
420 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
421 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
423 d1_variable = (!d1_zero
424 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
425 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
426 d2_variable = (!d2_zero
427 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
428 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
429 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
430 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
432 /* Save space: see if the result is identical to one of the args. */
433 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
434 && (d2_variable || d2_zero || !d1_variable))
435 return build_type_attribute_variant (t1, attributes);
436 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
437 && (d1_variable || d1_zero || !d2_variable))
438 return build_type_attribute_variant (t2, attributes);
440 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
441 return build_type_attribute_variant (t1, attributes);
442 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t2, attributes);
445 /* Merge the element types, and have a size if either arg has
446 one. We may have qualifiers on the element types. To set
447 up TYPE_MAIN_VARIANT correctly, we need to form the
448 composite of the unqualified types and add the qualifiers
449 back at the end. */
450 quals = TYPE_QUALS (strip_array_types (elt));
451 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
452 t1 = build_array_type (unqual_elt,
453 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
454 && (d2_variable
455 || d2_zero
456 || !d1_variable))
457 ? t1
458 : t2));
459 /* Ensure a composite type involving a zero-length array type
460 is a zero-length type not an incomplete type. */
461 if (d1_zero && d2_zero
462 && (t1_complete || t2_complete)
463 && !COMPLETE_TYPE_P (t1))
465 TYPE_SIZE (t1) = bitsize_zero_node;
466 TYPE_SIZE_UNIT (t1) = size_zero_node;
468 t1 = c_build_qualified_type (t1, quals);
469 return build_type_attribute_variant (t1, attributes);
472 case ENUMERAL_TYPE:
473 case RECORD_TYPE:
474 case UNION_TYPE:
475 if (attributes != NULL)
477 /* Try harder not to create a new aggregate type. */
478 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
479 return t1;
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
481 return t2;
483 return build_type_attribute_variant (t1, attributes);
485 case FUNCTION_TYPE:
486 /* Function types: prefer the one that specified arg types.
487 If both do, merge the arg types. Also merge the return types. */
489 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
490 tree p1 = TYPE_ARG_TYPES (t1);
491 tree p2 = TYPE_ARG_TYPES (t2);
492 int len;
493 tree newargs, n;
494 int i;
496 /* Save space: see if the result is identical to one of the args. */
497 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
498 return build_type_attribute_variant (t1, attributes);
499 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
500 return build_type_attribute_variant (t2, attributes);
502 /* Simple way if one arg fails to specify argument types. */
503 if (TYPE_ARG_TYPES (t1) == 0)
505 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
506 t1 = build_type_attribute_variant (t1, attributes);
507 return qualify_type (t1, t2);
509 if (TYPE_ARG_TYPES (t2) == 0)
511 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
512 t1 = build_type_attribute_variant (t1, attributes);
513 return qualify_type (t1, t2);
516 /* If both args specify argument types, we must merge the two
517 lists, argument by argument. */
518 /* Tell global_bindings_p to return false so that variable_size
519 doesn't die on VLAs in parameter types. */
520 c_override_global_bindings_to_false = true;
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_pedantic,
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_pedantic,
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 c_override_global_bindings_to_false = false;
604 t1 = build_function_type (valtype, newargs);
605 t1 = qualify_type (t1, t2);
606 /* ... falls through ... */
609 default:
610 return build_type_attribute_variant (t1, attributes);
615 /* Return the type of a conditional expression between pointers to
616 possibly differently qualified versions of compatible types.
618 We assume that comp_target_types has already been done and returned
619 nonzero; if that isn't so, this may crash. */
621 static tree
622 common_pointer_type (tree t1, tree t2)
624 tree attributes;
625 tree pointed_to_1, mv1;
626 tree pointed_to_2, mv2;
627 tree target;
628 unsigned target_quals;
629 addr_space_t as1, as2, as_common;
630 int quals1, quals2;
632 /* Save time if the two types are the same. */
634 if (t1 == t2) return t1;
636 /* If one type is nonsense, use the other. */
637 if (t1 == error_mark_node)
638 return t2;
639 if (t2 == error_mark_node)
640 return t1;
642 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
643 && TREE_CODE (t2) == POINTER_TYPE);
645 /* Merge the attributes. */
646 attributes = targetm.merge_type_attributes (t1, t2);
648 /* Find the composite type of the target types, and combine the
649 qualifiers of the two types' targets. Do not lose qualifiers on
650 array element types by taking the TYPE_MAIN_VARIANT. */
651 mv1 = pointed_to_1 = TREE_TYPE (t1);
652 mv2 = pointed_to_2 = TREE_TYPE (t2);
653 if (TREE_CODE (mv1) != ARRAY_TYPE)
654 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
655 if (TREE_CODE (mv2) != ARRAY_TYPE)
656 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
657 target = composite_type (mv1, mv2);
659 /* For function types do not merge const qualifiers, but drop them
660 if used inconsistently. The middle-end uses these to mark const
661 and noreturn functions. */
662 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
663 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
665 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
666 target_quals = (quals1 & quals2);
667 else
668 target_quals = (quals1 | quals2);
670 /* If the two named address spaces are different, determine the common
671 superset address space. This is guaranteed to exist due to the
672 assumption that comp_target_type returned non-zero. */
673 as1 = TYPE_ADDR_SPACE (pointed_to_1);
674 as2 = TYPE_ADDR_SPACE (pointed_to_2);
675 if (!addr_space_superset (as1, as2, &as_common))
676 gcc_unreachable ();
678 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
680 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
681 return build_type_attribute_variant (t1, attributes);
684 /* Return the common type for two arithmetic types under the usual
685 arithmetic conversions. The default conversions have already been
686 applied, and enumerated types converted to their compatible integer
687 types. The resulting type is unqualified and has no attributes.
689 This is the type for the result of most arithmetic operations
690 if the operands have the given two types. */
692 static tree
693 c_common_type (tree t1, tree t2)
695 enum tree_code code1;
696 enum tree_code code2;
698 /* If one type is nonsense, use the other. */
699 if (t1 == error_mark_node)
700 return t2;
701 if (t2 == error_mark_node)
702 return t1;
704 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
705 t1 = TYPE_MAIN_VARIANT (t1);
707 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
708 t2 = TYPE_MAIN_VARIANT (t2);
710 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
711 t1 = build_type_attribute_variant (t1, NULL_TREE);
713 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
714 t2 = build_type_attribute_variant (t2, NULL_TREE);
716 /* Save time if the two types are the same. */
718 if (t1 == t2) return t1;
720 code1 = TREE_CODE (t1);
721 code2 = TREE_CODE (t2);
723 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
724 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
725 || code1 == INTEGER_TYPE);
726 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
727 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
728 || code2 == INTEGER_TYPE);
730 /* When one operand is a decimal float type, the other operand cannot be
731 a generic float type or a complex type. We also disallow vector types
732 here. */
733 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
734 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
736 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
738 error ("can%'t mix operands of decimal float and vector types");
739 return error_mark_node;
741 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
743 error ("can%'t mix operands of decimal float and complex types");
744 return error_mark_node;
746 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
748 error ("can%'t mix operands of decimal float and other float types");
749 return error_mark_node;
753 /* If one type is a vector type, return that type. (How the usual
754 arithmetic conversions apply to the vector types extension is not
755 precisely specified.) */
756 if (code1 == VECTOR_TYPE)
757 return t1;
759 if (code2 == VECTOR_TYPE)
760 return t2;
762 /* If one type is complex, form the common type of the non-complex
763 components, then make that complex. Use T1 or T2 if it is the
764 required type. */
765 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
767 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
768 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
769 tree subtype = c_common_type (subtype1, subtype2);
771 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
772 return t1;
773 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
774 return t2;
775 else
776 return build_complex_type (subtype);
779 /* If only one is real, use it as the result. */
781 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
782 return t1;
784 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
785 return t2;
787 /* If both are real and either are decimal floating point types, use
788 the decimal floating point type with the greater precision. */
790 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
792 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
793 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
794 return dfloat128_type_node;
795 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
796 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
797 return dfloat64_type_node;
798 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
799 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
800 return dfloat32_type_node;
803 /* Deal with fixed-point types. */
804 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
806 unsigned int unsignedp = 0, satp = 0;
807 enum machine_mode m1, m2;
808 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
810 m1 = TYPE_MODE (t1);
811 m2 = TYPE_MODE (t2);
813 /* If one input type is saturating, the result type is saturating. */
814 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
815 satp = 1;
817 /* If both fixed-point types are unsigned, the result type is unsigned.
818 When mixing fixed-point and integer types, follow the sign of the
819 fixed-point type.
820 Otherwise, the result type is signed. */
821 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
822 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
823 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
824 && TYPE_UNSIGNED (t1))
825 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
826 && TYPE_UNSIGNED (t2)))
827 unsignedp = 1;
829 /* The result type is signed. */
830 if (unsignedp == 0)
832 /* If the input type is unsigned, we need to convert to the
833 signed type. */
834 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
836 enum mode_class mclass = (enum mode_class) 0;
837 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
838 mclass = MODE_FRACT;
839 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
840 mclass = MODE_ACCUM;
841 else
842 gcc_unreachable ();
843 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
845 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
847 enum mode_class mclass = (enum mode_class) 0;
848 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
849 mclass = MODE_FRACT;
850 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
851 mclass = MODE_ACCUM;
852 else
853 gcc_unreachable ();
854 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
858 if (code1 == FIXED_POINT_TYPE)
860 fbit1 = GET_MODE_FBIT (m1);
861 ibit1 = GET_MODE_IBIT (m1);
863 else
865 fbit1 = 0;
866 /* Signed integers need to subtract one sign bit. */
867 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
870 if (code2 == FIXED_POINT_TYPE)
872 fbit2 = GET_MODE_FBIT (m2);
873 ibit2 = GET_MODE_IBIT (m2);
875 else
877 fbit2 = 0;
878 /* Signed integers need to subtract one sign bit. */
879 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
882 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
883 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
884 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
885 satp);
888 /* Both real or both integers; use the one with greater precision. */
890 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
891 return t1;
892 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
893 return t2;
895 /* Same precision. Prefer long longs to longs to ints when the
896 same precision, following the C99 rules on integer type rank
897 (which are equivalent to the C90 rules for C90 types). */
899 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
900 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
901 return long_long_unsigned_type_node;
903 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
906 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
907 return long_long_unsigned_type_node;
908 else
909 return long_long_integer_type_node;
912 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
914 return long_unsigned_type_node;
916 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
919 /* But preserve unsignedness from the other type,
920 since long cannot hold all the values of an unsigned int. */
921 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
922 return long_unsigned_type_node;
923 else
924 return long_integer_type_node;
927 /* Likewise, prefer long double to double even if same size. */
928 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
929 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
930 return long_double_type_node;
932 /* Otherwise prefer the unsigned one. */
934 if (TYPE_UNSIGNED (t1))
935 return t1;
936 else
937 return t2;
940 /* Wrapper around c_common_type that is used by c-common.c and other
941 front end optimizations that remove promotions. ENUMERAL_TYPEs
942 are allowed here and are converted to their compatible integer types.
943 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
944 preferably a non-Boolean type as the common type. */
945 tree
946 common_type (tree t1, tree t2)
948 if (TREE_CODE (t1) == ENUMERAL_TYPE)
949 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
950 if (TREE_CODE (t2) == ENUMERAL_TYPE)
951 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
953 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
954 if (TREE_CODE (t1) == BOOLEAN_TYPE
955 && TREE_CODE (t2) == BOOLEAN_TYPE)
956 return boolean_type_node;
958 /* If either type is BOOLEAN_TYPE, then return the other. */
959 if (TREE_CODE (t1) == BOOLEAN_TYPE)
960 return t2;
961 if (TREE_CODE (t2) == BOOLEAN_TYPE)
962 return t1;
964 return c_common_type (t1, t2);
967 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
968 or various other operations. Return 2 if they are compatible
969 but a warning may be needed if you use them together. */
972 comptypes (tree type1, tree type2)
974 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
975 int val;
977 val = comptypes_internal (type1, type2, NULL, NULL);
978 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
980 return val;
983 /* Like comptypes, but if it returns non-zero because enum and int are
984 compatible, it sets *ENUM_AND_INT_P to true. */
986 static int
987 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
989 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
990 int val;
992 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
993 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
995 return val;
998 /* Like comptypes, but if it returns nonzero for different types, it
999 sets *DIFFERENT_TYPES_P to true. */
1002 comptypes_check_different_types (tree type1, tree type2,
1003 bool *different_types_p)
1005 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1006 int val;
1008 val = comptypes_internal (type1, type2, NULL, different_types_p);
1009 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1011 return val;
1014 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1015 or various other operations. Return 2 if they are compatible
1016 but a warning may be needed if you use them together. If
1017 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1018 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1019 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1020 NULL, and the types are compatible but different enough not to be
1021 permitted in C1X typedef redeclarations, then this sets
1022 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1023 false, but may or may not be set if the types are incompatible.
1024 This differs from comptypes, in that we don't free the seen
1025 types. */
1027 static int
1028 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1029 bool *different_types_p)
1031 const_tree t1 = type1;
1032 const_tree t2 = type2;
1033 int attrval, val;
1035 /* Suppress errors caused by previously reported errors. */
1037 if (t1 == t2 || !t1 || !t2
1038 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1039 return 1;
1041 /* Enumerated types are compatible with integer types, but this is
1042 not transitive: two enumerated types in the same translation unit
1043 are compatible with each other only if they are the same type. */
1045 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1047 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1048 if (TREE_CODE (t2) != VOID_TYPE)
1050 if (enum_and_int_p != NULL)
1051 *enum_and_int_p = true;
1052 if (different_types_p != NULL)
1053 *different_types_p = true;
1056 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1058 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1059 if (TREE_CODE (t1) != VOID_TYPE)
1061 if (enum_and_int_p != NULL)
1062 *enum_and_int_p = true;
1063 if (different_types_p != NULL)
1064 *different_types_p = true;
1068 if (t1 == t2)
1069 return 1;
1071 /* Different classes of types can't be compatible. */
1073 if (TREE_CODE (t1) != TREE_CODE (t2))
1074 return 0;
1076 /* Qualifiers must match. C99 6.7.3p9 */
1078 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1079 return 0;
1081 /* Allow for two different type nodes which have essentially the same
1082 definition. Note that we already checked for equality of the type
1083 qualifiers (just above). */
1085 if (TREE_CODE (t1) != ARRAY_TYPE
1086 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1087 return 1;
1089 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1090 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1091 return 0;
1093 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1094 val = 0;
1096 switch (TREE_CODE (t1))
1098 case POINTER_TYPE:
1099 /* Do not remove mode or aliasing information. */
1100 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1101 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1102 break;
1103 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1104 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1105 enum_and_int_p, different_types_p));
1106 break;
1108 case FUNCTION_TYPE:
1109 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1110 different_types_p);
1111 break;
1113 case ARRAY_TYPE:
1115 tree d1 = TYPE_DOMAIN (t1);
1116 tree d2 = TYPE_DOMAIN (t2);
1117 bool d1_variable, d2_variable;
1118 bool d1_zero, d2_zero;
1119 val = 1;
1121 /* Target types must match incl. qualifiers. */
1122 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1123 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1124 enum_and_int_p,
1125 different_types_p)))
1126 return 0;
1128 if (different_types_p != NULL
1129 && (d1 == 0) != (d2 == 0))
1130 *different_types_p = true;
1131 /* Sizes must match unless one is missing or variable. */
1132 if (d1 == 0 || d2 == 0 || d1 == d2)
1133 break;
1135 d1_zero = !TYPE_MAX_VALUE (d1);
1136 d2_zero = !TYPE_MAX_VALUE (d2);
1138 d1_variable = (!d1_zero
1139 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1140 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1141 d2_variable = (!d2_zero
1142 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1143 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1144 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1145 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1147 if (different_types_p != NULL
1148 && d1_variable != d2_variable)
1149 *different_types_p = true;
1150 if (d1_variable || d2_variable)
1151 break;
1152 if (d1_zero && d2_zero)
1153 break;
1154 if (d1_zero || d2_zero
1155 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1156 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1157 val = 0;
1159 break;
1162 case ENUMERAL_TYPE:
1163 case RECORD_TYPE:
1164 case UNION_TYPE:
1165 if (val != 1 && !same_translation_unit_p (t1, t2))
1167 tree a1 = TYPE_ATTRIBUTES (t1);
1168 tree a2 = TYPE_ATTRIBUTES (t2);
1170 if (! attribute_list_contained (a1, a2)
1171 && ! attribute_list_contained (a2, a1))
1172 break;
1174 if (attrval != 2)
1175 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1176 different_types_p);
1177 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1178 different_types_p);
1180 break;
1182 case VECTOR_TYPE:
1183 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1184 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1185 enum_and_int_p, different_types_p));
1186 break;
1188 default:
1189 break;
1191 return attrval == 2 && val == 1 ? 2 : val;
1194 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1195 their qualifiers, except for named address spaces. If the pointers point to
1196 different named addresses, then we must determine if one address space is a
1197 subset of the other. */
1199 static int
1200 comp_target_types (location_t location, tree ttl, tree ttr)
1202 int val;
1203 tree mvl = TREE_TYPE (ttl);
1204 tree mvr = TREE_TYPE (ttr);
1205 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1206 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1207 addr_space_t as_common;
1208 bool enum_and_int_p;
1210 /* Fail if pointers point to incompatible address spaces. */
1211 if (!addr_space_superset (asl, asr, &as_common))
1212 return 0;
1214 /* Do not lose qualifiers on element types of array types that are
1215 pointer targets by taking their TYPE_MAIN_VARIANT. */
1216 if (TREE_CODE (mvl) != ARRAY_TYPE)
1217 mvl = TYPE_MAIN_VARIANT (mvl);
1218 if (TREE_CODE (mvr) != ARRAY_TYPE)
1219 mvr = TYPE_MAIN_VARIANT (mvr);
1220 enum_and_int_p = false;
1221 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1223 if (val == 2)
1224 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1226 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1227 warning_at (location, OPT_Wc___compat,
1228 "pointer target types incompatible in C++");
1230 return val;
1233 /* Subroutines of `comptypes'. */
1235 /* Determine whether two trees derive from the same translation unit.
1236 If the CONTEXT chain ends in a null, that tree's context is still
1237 being parsed, so if two trees have context chains ending in null,
1238 they're in the same translation unit. */
1240 same_translation_unit_p (const_tree t1, const_tree t2)
1242 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1243 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1245 case tcc_declaration:
1246 t1 = DECL_CONTEXT (t1); break;
1247 case tcc_type:
1248 t1 = TYPE_CONTEXT (t1); break;
1249 case tcc_exceptional:
1250 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1251 default: gcc_unreachable ();
1254 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1255 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1257 case tcc_declaration:
1258 t2 = DECL_CONTEXT (t2); break;
1259 case tcc_type:
1260 t2 = TYPE_CONTEXT (t2); break;
1261 case tcc_exceptional:
1262 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1263 default: gcc_unreachable ();
1266 return t1 == t2;
1269 /* Allocate the seen two types, assuming that they are compatible. */
1271 static struct tagged_tu_seen_cache *
1272 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1274 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1275 tu->next = tagged_tu_seen_base;
1276 tu->t1 = t1;
1277 tu->t2 = t2;
1279 tagged_tu_seen_base = tu;
1281 /* The C standard says that two structures in different translation
1282 units are compatible with each other only if the types of their
1283 fields are compatible (among other things). We assume that they
1284 are compatible until proven otherwise when building the cache.
1285 An example where this can occur is:
1286 struct a
1288 struct a *next;
1290 If we are comparing this against a similar struct in another TU,
1291 and did not assume they were compatible, we end up with an infinite
1292 loop. */
1293 tu->val = 1;
1294 return tu;
1297 /* Free the seen types until we get to TU_TIL. */
1299 static void
1300 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1302 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1303 while (tu != tu_til)
1305 const struct tagged_tu_seen_cache *const tu1
1306 = (const struct tagged_tu_seen_cache *) tu;
1307 tu = tu1->next;
1308 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1310 tagged_tu_seen_base = tu_til;
1313 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1314 compatible. If the two types are not the same (which has been
1315 checked earlier), this can only happen when multiple translation
1316 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1317 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1318 comptypes_internal. */
1320 static int
1321 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1322 bool *enum_and_int_p, bool *different_types_p)
1324 tree s1, s2;
1325 bool needs_warning = false;
1327 /* We have to verify that the tags of the types are the same. This
1328 is harder than it looks because this may be a typedef, so we have
1329 to go look at the original type. It may even be a typedef of a
1330 typedef...
1331 In the case of compiler-created builtin structs the TYPE_DECL
1332 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1333 while (TYPE_NAME (t1)
1334 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1335 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1336 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1338 while (TYPE_NAME (t2)
1339 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1340 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1341 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1343 /* C90 didn't have the requirement that the two tags be the same. */
1344 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1345 return 0;
1347 /* C90 didn't say what happened if one or both of the types were
1348 incomplete; we choose to follow C99 rules here, which is that they
1349 are compatible. */
1350 if (TYPE_SIZE (t1) == NULL
1351 || TYPE_SIZE (t2) == NULL)
1352 return 1;
1355 const struct tagged_tu_seen_cache * tts_i;
1356 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1357 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1358 return tts_i->val;
1361 switch (TREE_CODE (t1))
1363 case ENUMERAL_TYPE:
1365 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1366 /* Speed up the case where the type values are in the same order. */
1367 tree tv1 = TYPE_VALUES (t1);
1368 tree tv2 = TYPE_VALUES (t2);
1370 if (tv1 == tv2)
1372 return 1;
1375 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1377 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1378 break;
1379 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1381 tu->val = 0;
1382 return 0;
1386 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1388 return 1;
1390 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1392 tu->val = 0;
1393 return 0;
1396 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1398 tu->val = 0;
1399 return 0;
1402 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1404 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1405 if (s2 == NULL
1406 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1408 tu->val = 0;
1409 return 0;
1412 return 1;
1415 case UNION_TYPE:
1417 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1418 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1420 tu->val = 0;
1421 return 0;
1424 /* Speed up the common case where the fields are in the same order. */
1425 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1426 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1428 int result;
1430 if (DECL_NAME (s1) != DECL_NAME (s2))
1431 break;
1432 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1433 enum_and_int_p, different_types_p);
1435 if (result != 1 && !DECL_NAME (s1))
1436 break;
1437 if (result == 0)
1439 tu->val = 0;
1440 return 0;
1442 if (result == 2)
1443 needs_warning = true;
1445 if (TREE_CODE (s1) == FIELD_DECL
1446 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1447 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1449 tu->val = 0;
1450 return 0;
1453 if (!s1 && !s2)
1455 tu->val = needs_warning ? 2 : 1;
1456 return tu->val;
1459 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1461 bool ok = false;
1463 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1464 if (DECL_NAME (s1) == DECL_NAME (s2))
1466 int result;
1468 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1469 enum_and_int_p,
1470 different_types_p);
1472 if (result != 1 && !DECL_NAME (s1))
1473 continue;
1474 if (result == 0)
1476 tu->val = 0;
1477 return 0;
1479 if (result == 2)
1480 needs_warning = true;
1482 if (TREE_CODE (s1) == FIELD_DECL
1483 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1484 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1485 break;
1487 ok = true;
1488 break;
1490 if (!ok)
1492 tu->val = 0;
1493 return 0;
1496 tu->val = needs_warning ? 2 : 10;
1497 return tu->val;
1500 case RECORD_TYPE:
1502 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1504 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1505 s1 && s2;
1506 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1508 int result;
1509 if (TREE_CODE (s1) != TREE_CODE (s2)
1510 || DECL_NAME (s1) != DECL_NAME (s2))
1511 break;
1512 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1513 enum_and_int_p, different_types_p);
1514 if (result == 0)
1515 break;
1516 if (result == 2)
1517 needs_warning = true;
1519 if (TREE_CODE (s1) == FIELD_DECL
1520 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1521 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1522 break;
1524 if (s1 && s2)
1525 tu->val = 0;
1526 else
1527 tu->val = needs_warning ? 2 : 1;
1528 return tu->val;
1531 default:
1532 gcc_unreachable ();
1536 /* Return 1 if two function types F1 and F2 are compatible.
1537 If either type specifies no argument types,
1538 the other must specify a fixed number of self-promoting arg types.
1539 Otherwise, if one type specifies only the number of arguments,
1540 the other must specify that number of self-promoting arg types.
1541 Otherwise, the argument types must match.
1542 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1544 static int
1545 function_types_compatible_p (const_tree f1, const_tree f2,
1546 bool *enum_and_int_p, bool *different_types_p)
1548 tree args1, args2;
1549 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1550 int val = 1;
1551 int val1;
1552 tree ret1, ret2;
1554 ret1 = TREE_TYPE (f1);
1555 ret2 = TREE_TYPE (f2);
1557 /* 'volatile' qualifiers on a function's return type used to mean
1558 the function is noreturn. */
1559 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1560 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1561 if (TYPE_VOLATILE (ret1))
1562 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1563 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1564 if (TYPE_VOLATILE (ret2))
1565 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1566 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1567 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1568 if (val == 0)
1569 return 0;
1571 args1 = TYPE_ARG_TYPES (f1);
1572 args2 = TYPE_ARG_TYPES (f2);
1574 if (different_types_p != NULL
1575 && (args1 == 0) != (args2 == 0))
1576 *different_types_p = true;
1578 /* An unspecified parmlist matches any specified parmlist
1579 whose argument types don't need default promotions. */
1581 if (args1 == 0)
1583 if (!self_promoting_args_p (args2))
1584 return 0;
1585 /* If one of these types comes from a non-prototype fn definition,
1586 compare that with the other type's arglist.
1587 If they don't match, ask for a warning (but no error). */
1588 if (TYPE_ACTUAL_ARG_TYPES (f1)
1589 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1590 enum_and_int_p, different_types_p))
1591 val = 2;
1592 return val;
1594 if (args2 == 0)
1596 if (!self_promoting_args_p (args1))
1597 return 0;
1598 if (TYPE_ACTUAL_ARG_TYPES (f2)
1599 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1600 enum_and_int_p, different_types_p))
1601 val = 2;
1602 return val;
1605 /* Both types have argument lists: compare them and propagate results. */
1606 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1607 different_types_p);
1608 return val1 != 1 ? val1 : val;
1611 /* Check two lists of types for compatibility, returning 0 for
1612 incompatible, 1 for compatible, or 2 for compatible with
1613 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1614 comptypes_internal. */
1616 static int
1617 type_lists_compatible_p (const_tree args1, const_tree args2,
1618 bool *enum_and_int_p, bool *different_types_p)
1620 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1621 int val = 1;
1622 int newval = 0;
1624 while (1)
1626 tree a1, mv1, a2, mv2;
1627 if (args1 == 0 && args2 == 0)
1628 return val;
1629 /* If one list is shorter than the other,
1630 they fail to match. */
1631 if (args1 == 0 || args2 == 0)
1632 return 0;
1633 mv1 = a1 = TREE_VALUE (args1);
1634 mv2 = a2 = TREE_VALUE (args2);
1635 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1636 mv1 = TYPE_MAIN_VARIANT (mv1);
1637 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1638 mv2 = TYPE_MAIN_VARIANT (mv2);
1639 /* A null pointer instead of a type
1640 means there is supposed to be an argument
1641 but nothing is specified about what type it has.
1642 So match anything that self-promotes. */
1643 if (different_types_p != NULL
1644 && (a1 == 0) != (a2 == 0))
1645 *different_types_p = true;
1646 if (a1 == 0)
1648 if (c_type_promotes_to (a2) != a2)
1649 return 0;
1651 else if (a2 == 0)
1653 if (c_type_promotes_to (a1) != a1)
1654 return 0;
1656 /* If one of the lists has an error marker, ignore this arg. */
1657 else if (TREE_CODE (a1) == ERROR_MARK
1658 || TREE_CODE (a2) == ERROR_MARK)
1660 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1661 different_types_p)))
1663 if (different_types_p != NULL)
1664 *different_types_p = true;
1665 /* Allow wait (union {union wait *u; int *i} *)
1666 and wait (union wait *) to be compatible. */
1667 if (TREE_CODE (a1) == UNION_TYPE
1668 && (TYPE_NAME (a1) == 0
1669 || TYPE_TRANSPARENT_AGGR (a1))
1670 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1671 && tree_int_cst_equal (TYPE_SIZE (a1),
1672 TYPE_SIZE (a2)))
1674 tree memb;
1675 for (memb = TYPE_FIELDS (a1);
1676 memb; memb = DECL_CHAIN (memb))
1678 tree mv3 = TREE_TYPE (memb);
1679 if (mv3 && mv3 != error_mark_node
1680 && TREE_CODE (mv3) != ARRAY_TYPE)
1681 mv3 = TYPE_MAIN_VARIANT (mv3);
1682 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1683 different_types_p))
1684 break;
1686 if (memb == 0)
1687 return 0;
1689 else if (TREE_CODE (a2) == UNION_TYPE
1690 && (TYPE_NAME (a2) == 0
1691 || TYPE_TRANSPARENT_AGGR (a2))
1692 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1693 && tree_int_cst_equal (TYPE_SIZE (a2),
1694 TYPE_SIZE (a1)))
1696 tree memb;
1697 for (memb = TYPE_FIELDS (a2);
1698 memb; memb = DECL_CHAIN (memb))
1700 tree mv3 = TREE_TYPE (memb);
1701 if (mv3 && mv3 != error_mark_node
1702 && TREE_CODE (mv3) != ARRAY_TYPE)
1703 mv3 = TYPE_MAIN_VARIANT (mv3);
1704 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1705 different_types_p))
1706 break;
1708 if (memb == 0)
1709 return 0;
1711 else
1712 return 0;
1715 /* comptypes said ok, but record if it said to warn. */
1716 if (newval > val)
1717 val = newval;
1719 args1 = TREE_CHAIN (args1);
1720 args2 = TREE_CHAIN (args2);
1724 /* Compute the size to increment a pointer by. */
1726 static tree
1727 c_size_in_bytes (const_tree type)
1729 enum tree_code code = TREE_CODE (type);
1731 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1732 return size_one_node;
1734 if (!COMPLETE_OR_VOID_TYPE_P (type))
1736 error ("arithmetic on pointer to an incomplete type");
1737 return size_one_node;
1740 /* Convert in case a char is more than one unit. */
1741 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1742 size_int (TYPE_PRECISION (char_type_node)
1743 / BITS_PER_UNIT));
1746 /* Return either DECL or its known constant value (if it has one). */
1748 tree
1749 decl_constant_value (tree decl)
1751 if (/* Don't change a variable array bound or initial value to a constant
1752 in a place where a variable is invalid. Note that DECL_INITIAL
1753 isn't valid for a PARM_DECL. */
1754 current_function_decl != 0
1755 && TREE_CODE (decl) != PARM_DECL
1756 && !TREE_THIS_VOLATILE (decl)
1757 && TREE_READONLY (decl)
1758 && DECL_INITIAL (decl) != 0
1759 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1760 /* This is invalid if initial value is not constant.
1761 If it has either a function call, a memory reference,
1762 or a variable, then re-evaluating it could give different results. */
1763 && TREE_CONSTANT (DECL_INITIAL (decl))
1764 /* Check for cases where this is sub-optimal, even though valid. */
1765 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1766 return DECL_INITIAL (decl);
1767 return decl;
1770 /* Convert the array expression EXP to a pointer. */
1771 static tree
1772 array_to_pointer_conversion (location_t loc, tree exp)
1774 tree orig_exp = exp;
1775 tree type = TREE_TYPE (exp);
1776 tree adr;
1777 tree restype = TREE_TYPE (type);
1778 tree ptrtype;
1780 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1782 STRIP_TYPE_NOPS (exp);
1784 if (TREE_NO_WARNING (orig_exp))
1785 TREE_NO_WARNING (exp) = 1;
1787 ptrtype = build_pointer_type (restype);
1789 if (TREE_CODE (exp) == INDIRECT_REF)
1790 return convert (ptrtype, TREE_OPERAND (exp, 0));
1792 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1793 return convert (ptrtype, adr);
1796 /* Convert the function expression EXP to a pointer. */
1797 static tree
1798 function_to_pointer_conversion (location_t loc, tree exp)
1800 tree orig_exp = exp;
1802 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1804 STRIP_TYPE_NOPS (exp);
1806 if (TREE_NO_WARNING (orig_exp))
1807 TREE_NO_WARNING (exp) = 1;
1809 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1812 /* Mark EXP as read, not just set, for set but not used -Wunused
1813 warning purposes. */
1815 void
1816 mark_exp_read (tree exp)
1818 switch (TREE_CODE (exp))
1820 case VAR_DECL:
1821 case PARM_DECL:
1822 DECL_READ_P (exp) = 1;
1823 break;
1824 case ARRAY_REF:
1825 case COMPONENT_REF:
1826 case MODIFY_EXPR:
1827 case REALPART_EXPR:
1828 case IMAGPART_EXPR:
1829 CASE_CONVERT:
1830 case ADDR_EXPR:
1831 mark_exp_read (TREE_OPERAND (exp, 0));
1832 break;
1833 case COMPOUND_EXPR:
1834 case C_MAYBE_CONST_EXPR:
1835 mark_exp_read (TREE_OPERAND (exp, 1));
1836 break;
1837 default:
1838 break;
1842 /* Perform the default conversion of arrays and functions to pointers.
1843 Return the result of converting EXP. For any other expression, just
1844 return EXP.
1846 LOC is the location of the expression. */
1848 struct c_expr
1849 default_function_array_conversion (location_t loc, struct c_expr exp)
1851 tree orig_exp = exp.value;
1852 tree type = TREE_TYPE (exp.value);
1853 enum tree_code code = TREE_CODE (type);
1855 switch (code)
1857 case ARRAY_TYPE:
1859 bool not_lvalue = false;
1860 bool lvalue_array_p;
1862 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1863 || CONVERT_EXPR_P (exp.value))
1864 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1866 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1867 not_lvalue = true;
1868 exp.value = TREE_OPERAND (exp.value, 0);
1871 if (TREE_NO_WARNING (orig_exp))
1872 TREE_NO_WARNING (exp.value) = 1;
1874 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1875 if (!flag_isoc99 && !lvalue_array_p)
1877 /* Before C99, non-lvalue arrays do not decay to pointers.
1878 Normally, using such an array would be invalid; but it can
1879 be used correctly inside sizeof or as a statement expression.
1880 Thus, do not give an error here; an error will result later. */
1881 return exp;
1884 exp.value = array_to_pointer_conversion (loc, exp.value);
1886 break;
1887 case FUNCTION_TYPE:
1888 exp.value = function_to_pointer_conversion (loc, exp.value);
1889 break;
1890 default:
1891 break;
1894 return exp;
1897 struct c_expr
1898 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1900 mark_exp_read (exp.value);
1901 return default_function_array_conversion (loc, exp);
1904 /* EXP is an expression of integer type. Apply the integer promotions
1905 to it and return the promoted value. */
1907 tree
1908 perform_integral_promotions (tree exp)
1910 tree type = TREE_TYPE (exp);
1911 enum tree_code code = TREE_CODE (type);
1913 gcc_assert (INTEGRAL_TYPE_P (type));
1915 /* Normally convert enums to int,
1916 but convert wide enums to something wider. */
1917 if (code == ENUMERAL_TYPE)
1919 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1920 TYPE_PRECISION (integer_type_node)),
1921 ((TYPE_PRECISION (type)
1922 >= TYPE_PRECISION (integer_type_node))
1923 && TYPE_UNSIGNED (type)));
1925 return convert (type, exp);
1928 /* ??? This should no longer be needed now bit-fields have their
1929 proper types. */
1930 if (TREE_CODE (exp) == COMPONENT_REF
1931 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1932 /* If it's thinner than an int, promote it like a
1933 c_promoting_integer_type_p, otherwise leave it alone. */
1934 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1935 TYPE_PRECISION (integer_type_node)))
1936 return convert (integer_type_node, exp);
1938 if (c_promoting_integer_type_p (type))
1940 /* Preserve unsignedness if not really getting any wider. */
1941 if (TYPE_UNSIGNED (type)
1942 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1943 return convert (unsigned_type_node, exp);
1945 return convert (integer_type_node, exp);
1948 return exp;
1952 /* Perform default promotions for C data used in expressions.
1953 Enumeral types or short or char are converted to int.
1954 In addition, manifest constants symbols are replaced by their values. */
1956 tree
1957 default_conversion (tree exp)
1959 tree orig_exp;
1960 tree type = TREE_TYPE (exp);
1961 enum tree_code code = TREE_CODE (type);
1962 tree promoted_type;
1964 mark_exp_read (exp);
1966 /* Functions and arrays have been converted during parsing. */
1967 gcc_assert (code != FUNCTION_TYPE);
1968 if (code == ARRAY_TYPE)
1969 return exp;
1971 /* Constants can be used directly unless they're not loadable. */
1972 if (TREE_CODE (exp) == CONST_DECL)
1973 exp = DECL_INITIAL (exp);
1975 /* Strip no-op conversions. */
1976 orig_exp = exp;
1977 STRIP_TYPE_NOPS (exp);
1979 if (TREE_NO_WARNING (orig_exp))
1980 TREE_NO_WARNING (exp) = 1;
1982 if (code == VOID_TYPE)
1984 error ("void value not ignored as it ought to be");
1985 return error_mark_node;
1988 exp = require_complete_type (exp);
1989 if (exp == error_mark_node)
1990 return error_mark_node;
1992 promoted_type = targetm.promoted_type (type);
1993 if (promoted_type)
1994 return convert (promoted_type, exp);
1996 if (INTEGRAL_TYPE_P (type))
1997 return perform_integral_promotions (exp);
1999 return exp;
2002 /* Look up COMPONENT in a structure or union TYPE.
2004 If the component name is not found, returns NULL_TREE. Otherwise,
2005 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2006 stepping down the chain to the component, which is in the last
2007 TREE_VALUE of the list. Normally the list is of length one, but if
2008 the component is embedded within (nested) anonymous structures or
2009 unions, the list steps down the chain to the component. */
2011 static tree
2012 lookup_field (tree type, tree component)
2014 tree field;
2016 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2017 to the field elements. Use a binary search on this array to quickly
2018 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2019 will always be set for structures which have many elements. */
2021 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2023 int bot, top, half;
2024 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2026 field = TYPE_FIELDS (type);
2027 bot = 0;
2028 top = TYPE_LANG_SPECIFIC (type)->s->len;
2029 while (top - bot > 1)
2031 half = (top - bot + 1) >> 1;
2032 field = field_array[bot+half];
2034 if (DECL_NAME (field) == NULL_TREE)
2036 /* Step through all anon unions in linear fashion. */
2037 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2039 field = field_array[bot++];
2040 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2041 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2043 tree anon = lookup_field (TREE_TYPE (field), component);
2045 if (anon)
2046 return tree_cons (NULL_TREE, field, anon);
2048 /* The Plan 9 compiler permits referring
2049 directly to an anonymous struct/union field
2050 using a typedef name. */
2051 if (flag_plan9_extensions
2052 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2053 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2054 == TYPE_DECL)
2055 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2056 == component))
2057 break;
2061 /* Entire record is only anon unions. */
2062 if (bot > top)
2063 return NULL_TREE;
2065 /* Restart the binary search, with new lower bound. */
2066 continue;
2069 if (DECL_NAME (field) == component)
2070 break;
2071 if (DECL_NAME (field) < component)
2072 bot += half;
2073 else
2074 top = bot + half;
2077 if (DECL_NAME (field_array[bot]) == component)
2078 field = field_array[bot];
2079 else if (DECL_NAME (field) != component)
2080 return NULL_TREE;
2082 else
2084 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2086 if (DECL_NAME (field) == NULL_TREE
2087 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2088 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2090 tree anon = lookup_field (TREE_TYPE (field), component);
2092 if (anon)
2093 return tree_cons (NULL_TREE, field, anon);
2095 /* The Plan 9 compiler permits referring directly to an
2096 anonymous struct/union field using a typedef
2097 name. */
2098 if (flag_plan9_extensions
2099 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2100 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2101 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2102 == component))
2103 break;
2106 if (DECL_NAME (field) == component)
2107 break;
2110 if (field == NULL_TREE)
2111 return NULL_TREE;
2114 return tree_cons (NULL_TREE, field, NULL_TREE);
2117 /* Make an expression to refer to the COMPONENT field of structure or
2118 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2119 location of the COMPONENT_REF. */
2121 tree
2122 build_component_ref (location_t loc, tree datum, tree component)
2124 tree type = TREE_TYPE (datum);
2125 enum tree_code code = TREE_CODE (type);
2126 tree field = NULL;
2127 tree ref;
2128 bool datum_lvalue = lvalue_p (datum);
2130 if (!objc_is_public (datum, component))
2131 return error_mark_node;
2133 /* Detect Objective-C property syntax object.property. */
2134 if (c_dialect_objc ()
2135 && (ref = objc_maybe_build_component_ref (datum, component)))
2136 return ref;
2138 /* See if there is a field or component with name COMPONENT. */
2140 if (code == RECORD_TYPE || code == UNION_TYPE)
2142 if (!COMPLETE_TYPE_P (type))
2144 c_incomplete_type_error (NULL_TREE, type);
2145 return error_mark_node;
2148 field = lookup_field (type, component);
2150 if (!field)
2152 error_at (loc, "%qT has no member named %qE", type, component);
2153 return error_mark_node;
2156 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2157 This might be better solved in future the way the C++ front
2158 end does it - by giving the anonymous entities each a
2159 separate name and type, and then have build_component_ref
2160 recursively call itself. We can't do that here. */
2163 tree subdatum = TREE_VALUE (field);
2164 int quals;
2165 tree subtype;
2166 bool use_datum_quals;
2168 if (TREE_TYPE (subdatum) == error_mark_node)
2169 return error_mark_node;
2171 /* If this is an rvalue, it does not have qualifiers in C
2172 standard terms and we must avoid propagating such
2173 qualifiers down to a non-lvalue array that is then
2174 converted to a pointer. */
2175 use_datum_quals = (datum_lvalue
2176 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2178 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2179 if (use_datum_quals)
2180 quals |= TYPE_QUALS (TREE_TYPE (datum));
2181 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2183 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2184 NULL_TREE);
2185 SET_EXPR_LOCATION (ref, loc);
2186 if (TREE_READONLY (subdatum)
2187 || (use_datum_quals && TREE_READONLY (datum)))
2188 TREE_READONLY (ref) = 1;
2189 if (TREE_THIS_VOLATILE (subdatum)
2190 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2191 TREE_THIS_VOLATILE (ref) = 1;
2193 if (TREE_DEPRECATED (subdatum))
2194 warn_deprecated_use (subdatum, NULL_TREE);
2196 datum = ref;
2198 field = TREE_CHAIN (field);
2200 while (field);
2202 return ref;
2204 else if (code != ERROR_MARK)
2205 error_at (loc,
2206 "request for member %qE in something not a structure or union",
2207 component);
2209 return error_mark_node;
2212 /* Given an expression PTR for a pointer, return an expression
2213 for the value pointed to.
2214 ERRORSTRING is the name of the operator to appear in error messages.
2216 LOC is the location to use for the generated tree. */
2218 tree
2219 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2221 tree pointer = default_conversion (ptr);
2222 tree type = TREE_TYPE (pointer);
2223 tree ref;
2225 if (TREE_CODE (type) == POINTER_TYPE)
2227 if (CONVERT_EXPR_P (pointer)
2228 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2230 /* If a warning is issued, mark it to avoid duplicates from
2231 the backend. This only needs to be done at
2232 warn_strict_aliasing > 2. */
2233 if (warn_strict_aliasing > 2)
2234 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2235 type, TREE_OPERAND (pointer, 0)))
2236 TREE_NO_WARNING (pointer) = 1;
2239 if (TREE_CODE (pointer) == ADDR_EXPR
2240 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2241 == TREE_TYPE (type)))
2243 ref = TREE_OPERAND (pointer, 0);
2244 protected_set_expr_location (ref, loc);
2245 return ref;
2247 else
2249 tree t = TREE_TYPE (type);
2251 ref = build1 (INDIRECT_REF, t, pointer);
2253 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2255 error_at (loc, "dereferencing pointer to incomplete type");
2256 return error_mark_node;
2258 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2259 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2261 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2262 so that we get the proper error message if the result is used
2263 to assign to. Also, &* is supposed to be a no-op.
2264 And ANSI C seems to specify that the type of the result
2265 should be the const type. */
2266 /* A de-reference of a pointer to const is not a const. It is valid
2267 to change it via some other pointer. */
2268 TREE_READONLY (ref) = TYPE_READONLY (t);
2269 TREE_SIDE_EFFECTS (ref)
2270 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2271 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2272 protected_set_expr_location (ref, loc);
2273 return ref;
2276 else if (TREE_CODE (pointer) != ERROR_MARK)
2277 switch (errstring)
2279 case RO_ARRAY_INDEXING:
2280 error_at (loc,
2281 "invalid type argument of array indexing (have %qT)",
2282 type);
2283 break;
2284 case RO_UNARY_STAR:
2285 error_at (loc,
2286 "invalid type argument of unary %<*%> (have %qT)",
2287 type);
2288 break;
2289 case RO_ARROW:
2290 error_at (loc,
2291 "invalid type argument of %<->%> (have %qT)",
2292 type);
2293 break;
2294 default:
2295 gcc_unreachable ();
2297 return error_mark_node;
2300 /* This handles expressions of the form "a[i]", which denotes
2301 an array reference.
2303 This is logically equivalent in C to *(a+i), but we may do it differently.
2304 If A is a variable or a member, we generate a primitive ARRAY_REF.
2305 This avoids forcing the array out of registers, and can work on
2306 arrays that are not lvalues (for example, members of structures returned
2307 by functions).
2309 For vector types, allow vector[i] but not i[vector], and create
2310 *(((type*)&vectortype) + i) for the expression.
2312 LOC is the location to use for the returned expression. */
2314 tree
2315 build_array_ref (location_t loc, tree array, tree index)
2317 tree ret;
2318 bool swapped = false;
2319 if (TREE_TYPE (array) == error_mark_node
2320 || TREE_TYPE (index) == error_mark_node)
2321 return error_mark_node;
2323 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2324 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2325 /* Allow vector[index] but not index[vector]. */
2326 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2328 tree temp;
2329 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2330 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2332 error_at (loc,
2333 "subscripted value is neither array nor pointer nor vector");
2335 return error_mark_node;
2337 temp = array;
2338 array = index;
2339 index = temp;
2340 swapped = true;
2343 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2345 error_at (loc, "array subscript is not an integer");
2346 return error_mark_node;
2349 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2351 error_at (loc, "subscripted value is pointer to function");
2352 return error_mark_node;
2355 /* ??? Existing practice has been to warn only when the char
2356 index is syntactically the index, not for char[array]. */
2357 if (!swapped)
2358 warn_array_subscript_with_type_char (index);
2360 /* Apply default promotions *after* noticing character types. */
2361 index = default_conversion (index);
2363 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2365 /* For vector[index], convert the vector to a
2366 pointer of the underlying type. */
2367 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2369 tree type = TREE_TYPE (array);
2370 tree type1;
2372 if (TREE_CODE (index) == INTEGER_CST)
2373 if (!host_integerp (index, 1)
2374 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2375 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2376 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2378 c_common_mark_addressable_vec (array);
2379 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2380 type = build_pointer_type (type);
2381 type1 = build_pointer_type (TREE_TYPE (array));
2382 array = build1 (ADDR_EXPR, type1, array);
2383 array = convert (type, array);
2386 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2388 tree rval, type;
2390 /* An array that is indexed by a non-constant
2391 cannot be stored in a register; we must be able to do
2392 address arithmetic on its address.
2393 Likewise an array of elements of variable size. */
2394 if (TREE_CODE (index) != INTEGER_CST
2395 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2396 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2398 if (!c_mark_addressable (array))
2399 return error_mark_node;
2401 /* An array that is indexed by a constant value which is not within
2402 the array bounds cannot be stored in a register either; because we
2403 would get a crash in store_bit_field/extract_bit_field when trying
2404 to access a non-existent part of the register. */
2405 if (TREE_CODE (index) == INTEGER_CST
2406 && TYPE_DOMAIN (TREE_TYPE (array))
2407 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2409 if (!c_mark_addressable (array))
2410 return error_mark_node;
2413 if (pedantic)
2415 tree foo = array;
2416 while (TREE_CODE (foo) == COMPONENT_REF)
2417 foo = TREE_OPERAND (foo, 0);
2418 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2419 pedwarn (loc, OPT_pedantic,
2420 "ISO C forbids subscripting %<register%> array");
2421 else if (!flag_isoc99 && !lvalue_p (foo))
2422 pedwarn (loc, OPT_pedantic,
2423 "ISO C90 forbids subscripting non-lvalue array");
2426 type = TREE_TYPE (TREE_TYPE (array));
2427 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2428 /* Array ref is const/volatile if the array elements are
2429 or if the array is. */
2430 TREE_READONLY (rval)
2431 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2432 | TREE_READONLY (array));
2433 TREE_SIDE_EFFECTS (rval)
2434 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2435 | TREE_SIDE_EFFECTS (array));
2436 TREE_THIS_VOLATILE (rval)
2437 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2438 /* This was added by rms on 16 Nov 91.
2439 It fixes vol struct foo *a; a->elts[1]
2440 in an inline function.
2441 Hope it doesn't break something else. */
2442 | TREE_THIS_VOLATILE (array));
2443 ret = require_complete_type (rval);
2444 protected_set_expr_location (ret, loc);
2445 return ret;
2447 else
2449 tree ar = default_conversion (array);
2451 if (ar == error_mark_node)
2452 return ar;
2454 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2455 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2457 return build_indirect_ref
2458 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2459 RO_ARRAY_INDEXING);
2463 /* Build an external reference to identifier ID. FUN indicates
2464 whether this will be used for a function call. LOC is the source
2465 location of the identifier. This sets *TYPE to the type of the
2466 identifier, which is not the same as the type of the returned value
2467 for CONST_DECLs defined as enum constants. If the type of the
2468 identifier is not available, *TYPE is set to NULL. */
2469 tree
2470 build_external_ref (location_t loc, tree id, int fun, tree *type)
2472 tree ref;
2473 tree decl = lookup_name (id);
2475 /* In Objective-C, an instance variable (ivar) may be preferred to
2476 whatever lookup_name() found. */
2477 decl = objc_lookup_ivar (decl, id);
2479 *type = NULL;
2480 if (decl && decl != error_mark_node)
2482 ref = decl;
2483 *type = TREE_TYPE (ref);
2485 else if (fun)
2486 /* Implicit function declaration. */
2487 ref = implicitly_declare (loc, id);
2488 else if (decl == error_mark_node)
2489 /* Don't complain about something that's already been
2490 complained about. */
2491 return error_mark_node;
2492 else
2494 undeclared_variable (loc, id);
2495 return error_mark_node;
2498 if (TREE_TYPE (ref) == error_mark_node)
2499 return error_mark_node;
2501 if (TREE_DEPRECATED (ref))
2502 warn_deprecated_use (ref, NULL_TREE);
2504 /* Recursive call does not count as usage. */
2505 if (ref != current_function_decl)
2507 TREE_USED (ref) = 1;
2510 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2512 if (!in_sizeof && !in_typeof)
2513 C_DECL_USED (ref) = 1;
2514 else if (DECL_INITIAL (ref) == 0
2515 && DECL_EXTERNAL (ref)
2516 && !TREE_PUBLIC (ref))
2517 record_maybe_used_decl (ref);
2520 if (TREE_CODE (ref) == CONST_DECL)
2522 used_types_insert (TREE_TYPE (ref));
2524 if (warn_cxx_compat
2525 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2526 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2528 warning_at (loc, OPT_Wc___compat,
2529 ("enum constant defined in struct or union "
2530 "is not visible in C++"));
2531 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2534 ref = DECL_INITIAL (ref);
2535 TREE_CONSTANT (ref) = 1;
2537 else if (current_function_decl != 0
2538 && !DECL_FILE_SCOPE_P (current_function_decl)
2539 && (TREE_CODE (ref) == VAR_DECL
2540 || TREE_CODE (ref) == PARM_DECL
2541 || TREE_CODE (ref) == FUNCTION_DECL))
2543 tree context = decl_function_context (ref);
2545 if (context != 0 && context != current_function_decl)
2546 DECL_NONLOCAL (ref) = 1;
2548 /* C99 6.7.4p3: An inline definition of a function with external
2549 linkage ... shall not contain a reference to an identifier with
2550 internal linkage. */
2551 else if (current_function_decl != 0
2552 && DECL_DECLARED_INLINE_P (current_function_decl)
2553 && DECL_EXTERNAL (current_function_decl)
2554 && VAR_OR_FUNCTION_DECL_P (ref)
2555 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2556 && ! TREE_PUBLIC (ref)
2557 && DECL_CONTEXT (ref) != current_function_decl)
2558 record_inline_static (loc, current_function_decl, ref,
2559 csi_internal);
2561 return ref;
2564 /* Record details of decls possibly used inside sizeof or typeof. */
2565 struct maybe_used_decl
2567 /* The decl. */
2568 tree decl;
2569 /* The level seen at (in_sizeof + in_typeof). */
2570 int level;
2571 /* The next one at this level or above, or NULL. */
2572 struct maybe_used_decl *next;
2575 static struct maybe_used_decl *maybe_used_decls;
2577 /* Record that DECL, an undefined static function reference seen
2578 inside sizeof or typeof, might be used if the operand of sizeof is
2579 a VLA type or the operand of typeof is a variably modified
2580 type. */
2582 static void
2583 record_maybe_used_decl (tree decl)
2585 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2586 t->decl = decl;
2587 t->level = in_sizeof + in_typeof;
2588 t->next = maybe_used_decls;
2589 maybe_used_decls = t;
2592 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2593 USED is false, just discard them. If it is true, mark them used
2594 (if no longer inside sizeof or typeof) or move them to the next
2595 level up (if still inside sizeof or typeof). */
2597 void
2598 pop_maybe_used (bool used)
2600 struct maybe_used_decl *p = maybe_used_decls;
2601 int cur_level = in_sizeof + in_typeof;
2602 while (p && p->level > cur_level)
2604 if (used)
2606 if (cur_level == 0)
2607 C_DECL_USED (p->decl) = 1;
2608 else
2609 p->level = cur_level;
2611 p = p->next;
2613 if (!used || cur_level == 0)
2614 maybe_used_decls = p;
2617 /* Return the result of sizeof applied to EXPR. */
2619 struct c_expr
2620 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2622 struct c_expr ret;
2623 if (expr.value == error_mark_node)
2625 ret.value = error_mark_node;
2626 ret.original_code = ERROR_MARK;
2627 ret.original_type = NULL;
2628 pop_maybe_used (false);
2630 else
2632 bool expr_const_operands = true;
2633 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2634 &expr_const_operands);
2635 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2636 ret.original_code = ERROR_MARK;
2637 ret.original_type = NULL;
2638 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2640 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2641 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2642 folded_expr, ret.value);
2643 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2644 SET_EXPR_LOCATION (ret.value, loc);
2646 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2648 return ret;
2651 /* Return the result of sizeof applied to T, a structure for the type
2652 name passed to sizeof (rather than the type itself). LOC is the
2653 location of the original expression. */
2655 struct c_expr
2656 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2658 tree type;
2659 struct c_expr ret;
2660 tree type_expr = NULL_TREE;
2661 bool type_expr_const = true;
2662 type = groktypename (t, &type_expr, &type_expr_const);
2663 ret.value = c_sizeof (loc, type);
2664 ret.original_code = ERROR_MARK;
2665 ret.original_type = NULL;
2666 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2667 && c_vla_type_p (type))
2669 /* If the type is a [*] array, it is a VLA but is represented as
2670 having a size of zero. In such a case we must ensure that
2671 the result of sizeof does not get folded to a constant by
2672 c_fully_fold, because if the size is evaluated the result is
2673 not constant and so constraints on zero or negative size
2674 arrays must not be applied when this sizeof call is inside
2675 another array declarator. */
2676 if (!type_expr)
2677 type_expr = integer_zero_node;
2678 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2679 type_expr, ret.value);
2680 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2682 pop_maybe_used (type != error_mark_node
2683 ? C_TYPE_VARIABLE_SIZE (type) : false);
2684 return ret;
2687 /* Build a function call to function FUNCTION with parameters PARAMS.
2688 The function call is at LOC.
2689 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2690 TREE_VALUE of each node is a parameter-expression.
2691 FUNCTION's data type may be a function type or a pointer-to-function. */
2693 tree
2694 build_function_call (location_t loc, tree function, tree params)
2696 VEC(tree,gc) *vec;
2697 tree ret;
2699 vec = VEC_alloc (tree, gc, list_length (params));
2700 for (; params; params = TREE_CHAIN (params))
2701 VEC_quick_push (tree, vec, TREE_VALUE (params));
2702 ret = build_function_call_vec (loc, function, vec, NULL);
2703 VEC_free (tree, gc, vec);
2704 return ret;
2707 /* Build a function call to function FUNCTION with parameters PARAMS.
2708 ORIGTYPES, if not NULL, is a vector of types; each element is
2709 either NULL or the original type of the corresponding element in
2710 PARAMS. The original type may differ from TREE_TYPE of the
2711 parameter for enums. FUNCTION's data type may be a function type
2712 or pointer-to-function. This function changes the elements of
2713 PARAMS. */
2715 tree
2716 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2717 VEC(tree,gc) *origtypes)
2719 tree fntype, fundecl = 0;
2720 tree name = NULL_TREE, result;
2721 tree tem;
2722 int nargs;
2723 tree *argarray;
2726 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2727 STRIP_TYPE_NOPS (function);
2729 /* Convert anything with function type to a pointer-to-function. */
2730 if (TREE_CODE (function) == FUNCTION_DECL)
2732 /* Implement type-directed function overloading for builtins.
2733 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2734 handle all the type checking. The result is a complete expression
2735 that implements this function call. */
2736 tem = resolve_overloaded_builtin (loc, function, params);
2737 if (tem)
2738 return tem;
2740 name = DECL_NAME (function);
2741 fundecl = function;
2743 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2744 function = function_to_pointer_conversion (loc, function);
2746 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2747 expressions, like those used for ObjC messenger dispatches. */
2748 if (!VEC_empty (tree, params))
2749 function = objc_rewrite_function_call (function,
2750 VEC_index (tree, params, 0));
2752 function = c_fully_fold (function, false, NULL);
2754 fntype = TREE_TYPE (function);
2756 if (TREE_CODE (fntype) == ERROR_MARK)
2757 return error_mark_node;
2759 if (!(TREE_CODE (fntype) == POINTER_TYPE
2760 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2762 error_at (loc, "called object %qE is not a function", function);
2763 return error_mark_node;
2766 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2767 current_function_returns_abnormally = 1;
2769 /* fntype now gets the type of function pointed to. */
2770 fntype = TREE_TYPE (fntype);
2772 /* Convert the parameters to the types declared in the
2773 function prototype, or apply default promotions. */
2775 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2776 function, fundecl);
2777 if (nargs < 0)
2778 return error_mark_node;
2780 /* Check that the function is called through a compatible prototype.
2781 If it is not, replace the call by a trap, wrapped up in a compound
2782 expression if necessary. This has the nice side-effect to prevent
2783 the tree-inliner from generating invalid assignment trees which may
2784 blow up in the RTL expander later. */
2785 if (CONVERT_EXPR_P (function)
2786 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2787 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2788 && !comptypes (fntype, TREE_TYPE (tem)))
2790 tree return_type = TREE_TYPE (fntype);
2791 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2792 NULL_TREE);
2793 int i;
2795 /* This situation leads to run-time undefined behavior. We can't,
2796 therefore, simply error unless we can prove that all possible
2797 executions of the program must execute the code. */
2798 if (warning_at (loc, 0, "function called through a non-compatible type"))
2799 /* We can, however, treat "undefined" any way we please.
2800 Call abort to encourage the user to fix the program. */
2801 inform (loc, "if this code is reached, the program will abort");
2802 /* Before the abort, allow the function arguments to exit or
2803 call longjmp. */
2804 for (i = 0; i < nargs; i++)
2805 trap = build2 (COMPOUND_EXPR, void_type_node,
2806 VEC_index (tree, params, i), trap);
2808 if (VOID_TYPE_P (return_type))
2810 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2811 pedwarn (loc, 0,
2812 "function with qualified void return type called");
2813 return trap;
2815 else
2817 tree rhs;
2819 if (AGGREGATE_TYPE_P (return_type))
2820 rhs = build_compound_literal (loc, return_type,
2821 build_constructor (return_type, 0),
2822 false);
2823 else
2824 rhs = build_zero_cst (return_type);
2826 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2827 trap, rhs));
2831 argarray = VEC_address (tree, params);
2833 /* Check that arguments to builtin functions match the expectations. */
2834 if (fundecl
2835 && DECL_BUILT_IN (fundecl)
2836 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2837 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2838 return error_mark_node;
2840 /* Check that the arguments to the function are valid. */
2841 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2842 TYPE_ARG_TYPES (fntype));
2844 if (name != NULL_TREE
2845 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2847 if (require_constant_value)
2848 result =
2849 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2850 function, nargs, argarray);
2851 else
2852 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2853 function, nargs, argarray);
2854 if (TREE_CODE (result) == NOP_EXPR
2855 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2856 STRIP_TYPE_NOPS (result);
2858 else
2859 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2860 function, nargs, argarray);
2862 if (VOID_TYPE_P (TREE_TYPE (result)))
2864 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2865 pedwarn (loc, 0,
2866 "function with qualified void return type called");
2867 return result;
2869 return require_complete_type (result);
2872 /* Convert the argument expressions in the vector VALUES
2873 to the types in the list TYPELIST.
2875 If TYPELIST is exhausted, or when an element has NULL as its type,
2876 perform the default conversions.
2878 ORIGTYPES is the original types of the expressions in VALUES. This
2879 holds the type of enum values which have been converted to integral
2880 types. It may be NULL.
2882 FUNCTION is a tree for the called function. It is used only for
2883 error messages, where it is formatted with %qE.
2885 This is also where warnings about wrong number of args are generated.
2887 Returns the actual number of arguments processed (which may be less
2888 than the length of VALUES in some error situations), or -1 on
2889 failure. */
2891 static int
2892 convert_arguments (tree typelist, VEC(tree,gc) *values,
2893 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2895 tree typetail, val;
2896 unsigned int parmnum;
2897 bool error_args = false;
2898 const bool type_generic = fundecl
2899 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2900 bool type_generic_remove_excess_precision = false;
2901 tree selector;
2903 /* Change pointer to function to the function itself for
2904 diagnostics. */
2905 if (TREE_CODE (function) == ADDR_EXPR
2906 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2907 function = TREE_OPERAND (function, 0);
2909 /* Handle an ObjC selector specially for diagnostics. */
2910 selector = objc_message_selector ();
2912 /* For type-generic built-in functions, determine whether excess
2913 precision should be removed (classification) or not
2914 (comparison). */
2915 if (type_generic
2916 && DECL_BUILT_IN (fundecl)
2917 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2919 switch (DECL_FUNCTION_CODE (fundecl))
2921 case BUILT_IN_ISFINITE:
2922 case BUILT_IN_ISINF:
2923 case BUILT_IN_ISINF_SIGN:
2924 case BUILT_IN_ISNAN:
2925 case BUILT_IN_ISNORMAL:
2926 case BUILT_IN_FPCLASSIFY:
2927 type_generic_remove_excess_precision = true;
2928 break;
2930 default:
2931 type_generic_remove_excess_precision = false;
2932 break;
2936 /* Scan the given expressions and types, producing individual
2937 converted arguments. */
2939 for (typetail = typelist, parmnum = 0;
2940 VEC_iterate (tree, values, parmnum, val);
2941 ++parmnum)
2943 tree type = typetail ? TREE_VALUE (typetail) : 0;
2944 tree valtype = TREE_TYPE (val);
2945 tree rname = function;
2946 int argnum = parmnum + 1;
2947 const char *invalid_func_diag;
2948 bool excess_precision = false;
2949 bool npc;
2950 tree parmval;
2952 if (type == void_type_node)
2954 if (selector)
2955 error_at (input_location,
2956 "too many arguments to method %qE", selector);
2957 else
2958 error_at (input_location,
2959 "too many arguments to function %qE", function);
2961 if (fundecl && !DECL_BUILT_IN (fundecl))
2962 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2963 return parmnum;
2966 if (selector && argnum > 2)
2968 rname = selector;
2969 argnum -= 2;
2972 npc = null_pointer_constant_p (val);
2974 /* If there is excess precision and a prototype, convert once to
2975 the required type rather than converting via the semantic
2976 type. Likewise without a prototype a float value represented
2977 as long double should be converted once to double. But for
2978 type-generic classification functions excess precision must
2979 be removed here. */
2980 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2981 && (type || !type_generic || !type_generic_remove_excess_precision))
2983 val = TREE_OPERAND (val, 0);
2984 excess_precision = true;
2986 val = c_fully_fold (val, false, NULL);
2987 STRIP_TYPE_NOPS (val);
2989 val = require_complete_type (val);
2991 if (type != 0)
2993 /* Formal parm type is specified by a function prototype. */
2995 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2997 error ("type of formal parameter %d is incomplete", parmnum + 1);
2998 parmval = val;
3000 else
3002 tree origtype;
3004 /* Optionally warn about conversions that
3005 differ from the default conversions. */
3006 if (warn_traditional_conversion || warn_traditional)
3008 unsigned int formal_prec = TYPE_PRECISION (type);
3010 if (INTEGRAL_TYPE_P (type)
3011 && TREE_CODE (valtype) == REAL_TYPE)
3012 warning (0, "passing argument %d of %qE as integer "
3013 "rather than floating due to prototype",
3014 argnum, rname);
3015 if (INTEGRAL_TYPE_P (type)
3016 && TREE_CODE (valtype) == COMPLEX_TYPE)
3017 warning (0, "passing argument %d of %qE as integer "
3018 "rather than complex due to prototype",
3019 argnum, rname);
3020 else if (TREE_CODE (type) == COMPLEX_TYPE
3021 && TREE_CODE (valtype) == REAL_TYPE)
3022 warning (0, "passing argument %d of %qE as complex "
3023 "rather than floating due to prototype",
3024 argnum, rname);
3025 else if (TREE_CODE (type) == REAL_TYPE
3026 && INTEGRAL_TYPE_P (valtype))
3027 warning (0, "passing argument %d of %qE as floating "
3028 "rather than integer due to prototype",
3029 argnum, rname);
3030 else if (TREE_CODE (type) == COMPLEX_TYPE
3031 && INTEGRAL_TYPE_P (valtype))
3032 warning (0, "passing argument %d of %qE as complex "
3033 "rather than integer due to prototype",
3034 argnum, rname);
3035 else if (TREE_CODE (type) == REAL_TYPE
3036 && TREE_CODE (valtype) == COMPLEX_TYPE)
3037 warning (0, "passing argument %d of %qE as floating "
3038 "rather than complex due to prototype",
3039 argnum, rname);
3040 /* ??? At some point, messages should be written about
3041 conversions between complex types, but that's too messy
3042 to do now. */
3043 else if (TREE_CODE (type) == REAL_TYPE
3044 && TREE_CODE (valtype) == REAL_TYPE)
3046 /* Warn if any argument is passed as `float',
3047 since without a prototype it would be `double'. */
3048 if (formal_prec == TYPE_PRECISION (float_type_node)
3049 && type != dfloat32_type_node)
3050 warning (0, "passing argument %d of %qE as %<float%> "
3051 "rather than %<double%> due to prototype",
3052 argnum, rname);
3054 /* Warn if mismatch between argument and prototype
3055 for decimal float types. Warn of conversions with
3056 binary float types and of precision narrowing due to
3057 prototype. */
3058 else if (type != valtype
3059 && (type == dfloat32_type_node
3060 || type == dfloat64_type_node
3061 || type == dfloat128_type_node
3062 || valtype == dfloat32_type_node
3063 || valtype == dfloat64_type_node
3064 || valtype == dfloat128_type_node)
3065 && (formal_prec
3066 <= TYPE_PRECISION (valtype)
3067 || (type == dfloat128_type_node
3068 && (valtype
3069 != dfloat64_type_node
3070 && (valtype
3071 != dfloat32_type_node)))
3072 || (type == dfloat64_type_node
3073 && (valtype
3074 != dfloat32_type_node))))
3075 warning (0, "passing argument %d of %qE as %qT "
3076 "rather than %qT due to prototype",
3077 argnum, rname, type, valtype);
3080 /* Detect integer changing in width or signedness.
3081 These warnings are only activated with
3082 -Wtraditional-conversion, not with -Wtraditional. */
3083 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3084 && INTEGRAL_TYPE_P (valtype))
3086 tree would_have_been = default_conversion (val);
3087 tree type1 = TREE_TYPE (would_have_been);
3089 if (TREE_CODE (type) == ENUMERAL_TYPE
3090 && (TYPE_MAIN_VARIANT (type)
3091 == TYPE_MAIN_VARIANT (valtype)))
3092 /* No warning if function asks for enum
3093 and the actual arg is that enum type. */
3095 else if (formal_prec != TYPE_PRECISION (type1))
3096 warning (OPT_Wtraditional_conversion,
3097 "passing argument %d of %qE "
3098 "with different width due to prototype",
3099 argnum, rname);
3100 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3102 /* Don't complain if the formal parameter type
3103 is an enum, because we can't tell now whether
3104 the value was an enum--even the same enum. */
3105 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3107 else if (TREE_CODE (val) == INTEGER_CST
3108 && int_fits_type_p (val, type))
3109 /* Change in signedness doesn't matter
3110 if a constant value is unaffected. */
3112 /* If the value is extended from a narrower
3113 unsigned type, it doesn't matter whether we
3114 pass it as signed or unsigned; the value
3115 certainly is the same either way. */
3116 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3117 && TYPE_UNSIGNED (valtype))
3119 else if (TYPE_UNSIGNED (type))
3120 warning (OPT_Wtraditional_conversion,
3121 "passing argument %d of %qE "
3122 "as unsigned due to prototype",
3123 argnum, rname);
3124 else
3125 warning (OPT_Wtraditional_conversion,
3126 "passing argument %d of %qE "
3127 "as signed due to prototype", argnum, rname);
3131 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3132 sake of better warnings from convert_and_check. */
3133 if (excess_precision)
3134 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3135 origtype = (origtypes == NULL
3136 ? NULL_TREE
3137 : VEC_index (tree, origtypes, parmnum));
3138 parmval = convert_for_assignment (input_location, type, val,
3139 origtype, ic_argpass, npc,
3140 fundecl, function,
3141 parmnum + 1);
3143 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3144 && INTEGRAL_TYPE_P (type)
3145 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3146 parmval = default_conversion (parmval);
3149 else if (TREE_CODE (valtype) == REAL_TYPE
3150 && (TYPE_PRECISION (valtype)
3151 < TYPE_PRECISION (double_type_node))
3152 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3154 if (type_generic)
3155 parmval = val;
3156 else
3158 /* Convert `float' to `double'. */
3159 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3160 warning (OPT_Wdouble_promotion,
3161 "implicit conversion from %qT to %qT when passing "
3162 "argument to function",
3163 valtype, double_type_node);
3164 parmval = convert (double_type_node, val);
3167 else if (excess_precision && !type_generic)
3168 /* A "double" argument with excess precision being passed
3169 without a prototype or in variable arguments. */
3170 parmval = convert (valtype, val);
3171 else if ((invalid_func_diag =
3172 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3174 error (invalid_func_diag);
3175 return -1;
3177 else
3178 /* Convert `short' and `char' to full-size `int'. */
3179 parmval = default_conversion (val);
3181 VEC_replace (tree, values, parmnum, parmval);
3182 if (parmval == error_mark_node)
3183 error_args = true;
3185 if (typetail)
3186 typetail = TREE_CHAIN (typetail);
3189 gcc_assert (parmnum == VEC_length (tree, values));
3191 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3193 error_at (input_location,
3194 "too few arguments to function %qE", function);
3195 if (fundecl && !DECL_BUILT_IN (fundecl))
3196 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3197 return -1;
3200 return error_args ? -1 : (int) parmnum;
3203 /* This is the entry point used by the parser to build unary operators
3204 in the input. CODE, a tree_code, specifies the unary operator, and
3205 ARG is the operand. For unary plus, the C parser currently uses
3206 CONVERT_EXPR for code.
3208 LOC is the location to use for the tree generated.
3211 struct c_expr
3212 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3214 struct c_expr result;
3216 result.value = build_unary_op (loc, code, arg.value, 0);
3217 result.original_code = code;
3218 result.original_type = NULL;
3220 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3221 overflow_warning (loc, result.value);
3223 return result;
3226 /* This is the entry point used by the parser to build binary operators
3227 in the input. CODE, a tree_code, specifies the binary operator, and
3228 ARG1 and ARG2 are the operands. In addition to constructing the
3229 expression, we check for operands that were written with other binary
3230 operators in a way that is likely to confuse the user.
3232 LOCATION is the location of the binary operator. */
3234 struct c_expr
3235 parser_build_binary_op (location_t location, enum tree_code code,
3236 struct c_expr arg1, struct c_expr arg2)
3238 struct c_expr result;
3240 enum tree_code code1 = arg1.original_code;
3241 enum tree_code code2 = arg2.original_code;
3242 tree type1 = (arg1.original_type
3243 ? arg1.original_type
3244 : TREE_TYPE (arg1.value));
3245 tree type2 = (arg2.original_type
3246 ? arg2.original_type
3247 : TREE_TYPE (arg2.value));
3249 result.value = build_binary_op (location, code,
3250 arg1.value, arg2.value, 1);
3251 result.original_code = code;
3252 result.original_type = NULL;
3254 if (TREE_CODE (result.value) == ERROR_MARK)
3255 return result;
3257 if (location != UNKNOWN_LOCATION)
3258 protected_set_expr_location (result.value, location);
3260 /* Check for cases such as x+y<<z which users are likely
3261 to misinterpret. */
3262 if (warn_parentheses)
3263 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3265 if (warn_logical_op)
3266 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3267 code1, arg1.value, code2, arg2.value);
3269 /* Warn about comparisons against string literals, with the exception
3270 of testing for equality or inequality of a string literal with NULL. */
3271 if (code == EQ_EXPR || code == NE_EXPR)
3273 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3274 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3275 warning_at (location, OPT_Waddress,
3276 "comparison with string literal results in unspecified behavior");
3278 else if (TREE_CODE_CLASS (code) == tcc_comparison
3279 && (code1 == STRING_CST || code2 == STRING_CST))
3280 warning_at (location, OPT_Waddress,
3281 "comparison with string literal results in unspecified behavior");
3283 if (TREE_OVERFLOW_P (result.value)
3284 && !TREE_OVERFLOW_P (arg1.value)
3285 && !TREE_OVERFLOW_P (arg2.value))
3286 overflow_warning (location, result.value);
3288 /* Warn about comparisons of different enum types. */
3289 if (warn_enum_compare
3290 && TREE_CODE_CLASS (code) == tcc_comparison
3291 && TREE_CODE (type1) == ENUMERAL_TYPE
3292 && TREE_CODE (type2) == ENUMERAL_TYPE
3293 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3294 warning_at (location, OPT_Wenum_compare,
3295 "comparison between %qT and %qT",
3296 type1, type2);
3298 return result;
3301 /* Return a tree for the difference of pointers OP0 and OP1.
3302 The resulting tree has type int. */
3304 static tree
3305 pointer_diff (location_t loc, tree op0, tree op1)
3307 tree restype = ptrdiff_type_node;
3308 tree result, inttype;
3310 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3311 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3312 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3313 tree con0, con1, lit0, lit1;
3314 tree orig_op1 = op1;
3316 /* If the operands point into different address spaces, we need to
3317 explicitly convert them to pointers into the common address space
3318 before we can subtract the numerical address values. */
3319 if (as0 != as1)
3321 addr_space_t as_common;
3322 tree common_type;
3324 /* Determine the common superset address space. This is guaranteed
3325 to exist because the caller verified that comp_target_types
3326 returned non-zero. */
3327 if (!addr_space_superset (as0, as1, &as_common))
3328 gcc_unreachable ();
3330 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3331 op0 = convert (common_type, op0);
3332 op1 = convert (common_type, op1);
3335 /* Determine integer type to perform computations in. This will usually
3336 be the same as the result type (ptrdiff_t), but may need to be a wider
3337 type if pointers for the address space are wider than ptrdiff_t. */
3338 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3339 inttype = lang_hooks.types.type_for_size
3340 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3341 else
3342 inttype = restype;
3345 if (TREE_CODE (target_type) == VOID_TYPE)
3346 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3347 "pointer of type %<void *%> used in subtraction");
3348 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3349 pedwarn (loc, pedantic ? OPT_pedantic : 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) == 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) == 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_pedantic,
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 arg = c_objc_common_truthvalue_conversion (location, arg);
3557 ret = invert_truthvalue_loc (location, arg);
3558 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3559 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3560 location = EXPR_LOCATION (ret);
3561 goto return_build_unary_op;
3563 case REALPART_EXPR:
3564 if (TREE_CODE (arg) == COMPLEX_CST)
3565 ret = TREE_REALPART (arg);
3566 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3567 ret = fold_build1_loc (location,
3568 REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3569 else
3570 ret = arg;
3571 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3572 eptype = TREE_TYPE (eptype);
3573 goto return_build_unary_op;
3575 case IMAGPART_EXPR:
3576 if (TREE_CODE (arg) == COMPLEX_CST)
3577 ret = TREE_IMAGPART (arg);
3578 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3579 ret = fold_build1_loc (location,
3580 IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3581 else
3582 ret = omit_one_operand_loc (location, TREE_TYPE (arg),
3583 integer_zero_node, arg);
3584 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3585 eptype = TREE_TYPE (eptype);
3586 goto return_build_unary_op;
3588 case PREINCREMENT_EXPR:
3589 case POSTINCREMENT_EXPR:
3590 case PREDECREMENT_EXPR:
3591 case POSTDECREMENT_EXPR:
3593 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3595 tree inner = build_unary_op (location, code,
3596 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3597 if (inner == error_mark_node)
3598 return error_mark_node;
3599 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3600 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3601 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3602 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3603 goto return_build_unary_op;
3606 /* Complain about anything that is not a true lvalue. */
3607 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3608 || code == POSTINCREMENT_EXPR)
3609 ? lv_increment
3610 : lv_decrement)))
3611 return error_mark_node;
3613 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3615 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3616 warning_at (location, OPT_Wc___compat,
3617 "increment of enumeration value is invalid in C++");
3618 else
3619 warning_at (location, OPT_Wc___compat,
3620 "decrement of enumeration value is invalid in C++");
3623 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3624 arg = c_fully_fold (arg, false, NULL);
3626 /* Increment or decrement the real part of the value,
3627 and don't change the imaginary part. */
3628 if (typecode == COMPLEX_TYPE)
3630 tree real, imag;
3632 pedwarn (location, OPT_pedantic,
3633 "ISO C does not support %<++%> and %<--%> on complex types");
3635 arg = stabilize_reference (arg);
3636 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3637 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3638 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3639 if (real == error_mark_node || imag == error_mark_node)
3640 return error_mark_node;
3641 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3642 real, imag);
3643 goto return_build_unary_op;
3646 /* Report invalid types. */
3648 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3649 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3651 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3652 error_at (location, "wrong type argument to increment");
3653 else
3654 error_at (location, "wrong type argument to decrement");
3656 return error_mark_node;
3660 tree inc;
3662 argtype = TREE_TYPE (arg);
3664 /* Compute the increment. */
3666 if (typecode == POINTER_TYPE)
3668 /* If pointer target is an undefined struct,
3669 we just cannot know how to do the arithmetic. */
3670 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3672 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3673 error_at (location,
3674 "increment of pointer to unknown structure");
3675 else
3676 error_at (location,
3677 "decrement of pointer to unknown structure");
3679 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3680 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3682 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3683 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3684 "wrong type argument to increment");
3685 else
3686 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3687 "wrong type argument to decrement");
3690 inc = c_size_in_bytes (TREE_TYPE (argtype));
3691 inc = fold_convert_loc (location, sizetype, inc);
3693 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3695 /* For signed fract types, we invert ++ to -- or
3696 -- to ++, and change inc from 1 to -1, because
3697 it is not possible to represent 1 in signed fract constants.
3698 For unsigned fract types, the result always overflows and
3699 we get an undefined (original) or the maximum value. */
3700 if (code == PREINCREMENT_EXPR)
3701 code = PREDECREMENT_EXPR;
3702 else if (code == PREDECREMENT_EXPR)
3703 code = PREINCREMENT_EXPR;
3704 else if (code == POSTINCREMENT_EXPR)
3705 code = POSTDECREMENT_EXPR;
3706 else /* code == POSTDECREMENT_EXPR */
3707 code = POSTINCREMENT_EXPR;
3709 inc = integer_minus_one_node;
3710 inc = convert (argtype, inc);
3712 else
3714 inc = integer_one_node;
3715 inc = convert (argtype, 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;
3772 return build_binary_op (location, PLUS_EXPR,
3773 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3774 ? array_to_pointer_conversion (location,
3775 op0)
3776 : op0),
3777 TREE_OPERAND (arg, 1), 1);
3780 /* Anything not already handled and not a true memory reference
3781 or a non-lvalue array is an error. */
3782 else if (typecode != FUNCTION_TYPE && !flag
3783 && !lvalue_or_else (arg, lv_addressof))
3784 return error_mark_node;
3786 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3787 folding later. */
3788 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3790 tree inner = build_unary_op (location, code,
3791 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3792 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3793 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3794 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3795 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3796 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3797 goto return_build_unary_op;
3800 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3801 argtype = TREE_TYPE (arg);
3803 /* If the lvalue is const or volatile, merge that into the type
3804 to which the address will point. This should only be needed
3805 for function types. */
3806 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3807 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3809 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3810 int quals = orig_quals;
3812 if (TREE_READONLY (arg))
3813 quals |= TYPE_QUAL_CONST;
3814 if (TREE_THIS_VOLATILE (arg))
3815 quals |= TYPE_QUAL_VOLATILE;
3817 gcc_assert (quals == orig_quals
3818 || TREE_CODE (argtype) == FUNCTION_TYPE);
3820 argtype = c_build_qualified_type (argtype, quals);
3823 if (!c_mark_addressable (arg))
3824 return error_mark_node;
3826 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3827 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3829 argtype = build_pointer_type (argtype);
3831 /* ??? Cope with user tricks that amount to offsetof. Delete this
3832 when we have proper support for integer constant expressions. */
3833 val = get_base_address (arg);
3834 if (val && TREE_CODE (val) == INDIRECT_REF
3835 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3837 tree op0 = fold_convert_loc (location, sizetype,
3838 fold_offsetof (arg, val)), op1;
3840 op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3841 ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3842 goto return_build_unary_op;
3845 val = build1 (ADDR_EXPR, argtype, arg);
3847 ret = val;
3848 goto return_build_unary_op;
3850 default:
3851 gcc_unreachable ();
3854 if (argtype == 0)
3855 argtype = TREE_TYPE (arg);
3856 if (TREE_CODE (arg) == INTEGER_CST)
3857 ret = (require_constant_value
3858 ? fold_build1_initializer_loc (location, code, argtype, arg)
3859 : fold_build1_loc (location, code, argtype, arg));
3860 else
3861 ret = build1 (code, argtype, arg);
3862 return_build_unary_op:
3863 gcc_assert (ret != error_mark_node);
3864 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3865 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3866 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3867 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3868 ret = note_integer_operands (ret);
3869 if (eptype)
3870 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3871 protected_set_expr_location (ret, location);
3872 return ret;
3875 /* Return nonzero if REF is an lvalue valid for this language.
3876 Lvalues can be assigned, unless their type has TYPE_READONLY.
3877 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3879 bool
3880 lvalue_p (const_tree ref)
3882 const enum tree_code code = TREE_CODE (ref);
3884 switch (code)
3886 case REALPART_EXPR:
3887 case IMAGPART_EXPR:
3888 case COMPONENT_REF:
3889 return lvalue_p (TREE_OPERAND (ref, 0));
3891 case C_MAYBE_CONST_EXPR:
3892 return lvalue_p (TREE_OPERAND (ref, 1));
3894 case COMPOUND_LITERAL_EXPR:
3895 case STRING_CST:
3896 return 1;
3898 case INDIRECT_REF:
3899 case ARRAY_REF:
3900 case VAR_DECL:
3901 case PARM_DECL:
3902 case RESULT_DECL:
3903 case ERROR_MARK:
3904 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3905 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3907 case BIND_EXPR:
3908 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3910 default:
3911 return 0;
3915 /* Give an error for storing in something that is 'const'. */
3917 static void
3918 readonly_error (tree arg, enum lvalue_use use)
3920 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3921 || use == lv_asm);
3922 /* Using this macro rather than (for example) arrays of messages
3923 ensures that all the format strings are checked at compile
3924 time. */
3925 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3926 : (use == lv_increment ? (I) \
3927 : (use == lv_decrement ? (D) : (AS))))
3928 if (TREE_CODE (arg) == COMPONENT_REF)
3930 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3931 readonly_error (TREE_OPERAND (arg, 0), use);
3932 else
3933 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3934 G_("increment of read-only member %qD"),
3935 G_("decrement of read-only member %qD"),
3936 G_("read-only member %qD used as %<asm%> output")),
3937 TREE_OPERAND (arg, 1));
3939 else if (TREE_CODE (arg) == VAR_DECL)
3940 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3941 G_("increment of read-only variable %qD"),
3942 G_("decrement of read-only variable %qD"),
3943 G_("read-only variable %qD used as %<asm%> output")),
3944 arg);
3945 else
3946 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3947 G_("increment of read-only location %qE"),
3948 G_("decrement of read-only location %qE"),
3949 G_("read-only location %qE used as %<asm%> output")),
3950 arg);
3953 /* Give a warning for storing in something that is read-only in GCC
3954 terms but not const in ISO C terms. */
3956 static void
3957 readonly_warning (tree arg, enum lvalue_use use)
3959 switch (use)
3961 case lv_assign:
3962 warning (0, "assignment of read-only location %qE", arg);
3963 break;
3964 case lv_increment:
3965 warning (0, "increment of read-only location %qE", arg);
3966 break;
3967 case lv_decrement:
3968 warning (0, "decrement of read-only location %qE", arg);
3969 break;
3970 default:
3971 gcc_unreachable ();
3973 return;
3977 /* Return nonzero if REF is an lvalue valid for this language;
3978 otherwise, print an error message and return zero. USE says
3979 how the lvalue is being used and so selects the error message. */
3981 static int
3982 lvalue_or_else (const_tree ref, enum lvalue_use use)
3984 int win = lvalue_p (ref);
3986 if (!win)
3987 lvalue_error (use);
3989 return win;
3992 /* Mark EXP saying that we need to be able to take the
3993 address of it; it should not be allocated in a register.
3994 Returns true if successful. */
3996 bool
3997 c_mark_addressable (tree exp)
3999 tree x = exp;
4001 while (1)
4002 switch (TREE_CODE (x))
4004 case COMPONENT_REF:
4005 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4007 error
4008 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4009 return false;
4012 /* ... fall through ... */
4014 case ADDR_EXPR:
4015 case ARRAY_REF:
4016 case REALPART_EXPR:
4017 case IMAGPART_EXPR:
4018 x = TREE_OPERAND (x, 0);
4019 break;
4021 case COMPOUND_LITERAL_EXPR:
4022 case CONSTRUCTOR:
4023 TREE_ADDRESSABLE (x) = 1;
4024 return true;
4026 case VAR_DECL:
4027 case CONST_DECL:
4028 case PARM_DECL:
4029 case RESULT_DECL:
4030 if (C_DECL_REGISTER (x)
4031 && DECL_NONLOCAL (x))
4033 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4035 error
4036 ("global register variable %qD used in nested function", x);
4037 return false;
4039 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4041 else if (C_DECL_REGISTER (x))
4043 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4044 error ("address of global register variable %qD requested", x);
4045 else
4046 error ("address of register variable %qD requested", x);
4047 return false;
4050 /* drops in */
4051 case FUNCTION_DECL:
4052 TREE_ADDRESSABLE (x) = 1;
4053 /* drops out */
4054 default:
4055 return true;
4059 /* Convert EXPR to TYPE, warning about conversion problems with
4060 constants. SEMANTIC_TYPE is the type this conversion would use
4061 without excess precision. If SEMANTIC_TYPE is NULL, this function
4062 is equivalent to convert_and_check. This function is a wrapper that
4063 handles conversions that may be different than
4064 the usual ones because of excess precision. */
4066 static tree
4067 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4069 if (TREE_TYPE (expr) == type)
4070 return expr;
4072 if (!semantic_type)
4073 return convert_and_check (type, expr);
4075 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4076 && TREE_TYPE (expr) != semantic_type)
4078 /* For integers, we need to check the real conversion, not
4079 the conversion to the excess precision type. */
4080 expr = convert_and_check (semantic_type, expr);
4082 /* Result type is the excess precision type, which should be
4083 large enough, so do not check. */
4084 return convert (type, expr);
4087 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4088 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4089 if folded to an integer constant then the unselected half may
4090 contain arbitrary operations not normally permitted in constant
4091 expressions. Set the location of the expression to LOC. */
4093 tree
4094 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4095 tree op1, tree op1_original_type, tree op2,
4096 tree op2_original_type)
4098 tree type1;
4099 tree type2;
4100 enum tree_code code1;
4101 enum tree_code code2;
4102 tree result_type = NULL;
4103 tree semantic_result_type = NULL;
4104 tree orig_op1 = op1, orig_op2 = op2;
4105 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4106 bool ifexp_int_operands;
4107 tree ret;
4109 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4110 if (op1_int_operands)
4111 op1 = remove_c_maybe_const_expr (op1);
4112 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4113 if (op2_int_operands)
4114 op2 = remove_c_maybe_const_expr (op2);
4115 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4116 if (ifexp_int_operands)
4117 ifexp = remove_c_maybe_const_expr (ifexp);
4119 /* Promote both alternatives. */
4121 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4122 op1 = default_conversion (op1);
4123 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4124 op2 = default_conversion (op2);
4126 if (TREE_CODE (ifexp) == ERROR_MARK
4127 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4128 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4129 return error_mark_node;
4131 type1 = TREE_TYPE (op1);
4132 code1 = TREE_CODE (type1);
4133 type2 = TREE_TYPE (op2);
4134 code2 = TREE_CODE (type2);
4136 /* C90 does not permit non-lvalue arrays in conditional expressions.
4137 In C99 they will be pointers by now. */
4138 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4140 error_at (colon_loc, "non-lvalue array in conditional expression");
4141 return error_mark_node;
4144 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4145 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4146 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4147 || code1 == COMPLEX_TYPE)
4148 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4149 || code2 == COMPLEX_TYPE))
4151 semantic_result_type = c_common_type (type1, type2);
4152 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4154 op1 = TREE_OPERAND (op1, 0);
4155 type1 = TREE_TYPE (op1);
4156 gcc_assert (TREE_CODE (type1) == code1);
4158 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4160 op2 = TREE_OPERAND (op2, 0);
4161 type2 = TREE_TYPE (op2);
4162 gcc_assert (TREE_CODE (type2) == code2);
4166 if (warn_cxx_compat)
4168 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4169 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4171 if (TREE_CODE (t1) == ENUMERAL_TYPE
4172 && TREE_CODE (t2) == ENUMERAL_TYPE
4173 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4174 warning_at (colon_loc, OPT_Wc___compat,
4175 ("different enum types in conditional is "
4176 "invalid in C++: %qT vs %qT"),
4177 t1, t2);
4180 /* Quickly detect the usual case where op1 and op2 have the same type
4181 after promotion. */
4182 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4184 if (type1 == type2)
4185 result_type = type1;
4186 else
4187 result_type = TYPE_MAIN_VARIANT (type1);
4189 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4190 || code1 == COMPLEX_TYPE)
4191 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4192 || code2 == COMPLEX_TYPE))
4194 result_type = c_common_type (type1, type2);
4195 do_warn_double_promotion (result_type, type1, type2,
4196 "implicit conversion from %qT to %qT to "
4197 "match other result of conditional",
4198 colon_loc);
4200 /* If -Wsign-compare, warn here if type1 and type2 have
4201 different signedness. We'll promote the signed to unsigned
4202 and later code won't know it used to be different.
4203 Do this check on the original types, so that explicit casts
4204 will be considered, but default promotions won't. */
4205 if (c_inhibit_evaluation_warnings == 0)
4207 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4208 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4210 if (unsigned_op1 ^ unsigned_op2)
4212 bool ovf;
4214 /* Do not warn if the result type is signed, since the
4215 signed type will only be chosen if it can represent
4216 all the values of the unsigned type. */
4217 if (!TYPE_UNSIGNED (result_type))
4218 /* OK */;
4219 else
4221 bool op1_maybe_const = true;
4222 bool op2_maybe_const = true;
4224 /* Do not warn if the signed quantity is an
4225 unsuffixed integer literal (or some static
4226 constant expression involving such literals) and
4227 it is non-negative. This warning requires the
4228 operands to be folded for best results, so do
4229 that folding in this case even without
4230 warn_sign_compare to avoid warning options
4231 possibly affecting code generation. */
4232 c_inhibit_evaluation_warnings
4233 += (ifexp == truthvalue_false_node);
4234 op1 = c_fully_fold (op1, require_constant_value,
4235 &op1_maybe_const);
4236 c_inhibit_evaluation_warnings
4237 -= (ifexp == truthvalue_false_node);
4239 c_inhibit_evaluation_warnings
4240 += (ifexp == truthvalue_true_node);
4241 op2 = c_fully_fold (op2, require_constant_value,
4242 &op2_maybe_const);
4243 c_inhibit_evaluation_warnings
4244 -= (ifexp == truthvalue_true_node);
4246 if (warn_sign_compare)
4248 if ((unsigned_op2
4249 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4250 || (unsigned_op1
4251 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4252 /* OK */;
4253 else
4254 warning_at (colon_loc, OPT_Wsign_compare,
4255 ("signed and unsigned type in "
4256 "conditional expression"));
4258 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4259 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4260 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4261 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4266 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4268 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4269 pedwarn (colon_loc, OPT_pedantic,
4270 "ISO C forbids conditional expr with only one void side");
4271 result_type = void_type_node;
4273 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4275 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4276 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4277 addr_space_t as_common;
4279 if (comp_target_types (colon_loc, type1, type2))
4280 result_type = common_pointer_type (type1, type2);
4281 else if (null_pointer_constant_p (orig_op1))
4282 result_type = type2;
4283 else if (null_pointer_constant_p (orig_op2))
4284 result_type = type1;
4285 else if (!addr_space_superset (as1, as2, &as_common))
4287 error_at (colon_loc, "pointers to disjoint address spaces "
4288 "used in conditional expression");
4289 return error_mark_node;
4291 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4293 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4294 pedwarn (colon_loc, OPT_pedantic,
4295 "ISO C forbids conditional expr between "
4296 "%<void *%> and function pointer");
4297 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4298 TREE_TYPE (type2)));
4300 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4302 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4303 pedwarn (colon_loc, OPT_pedantic,
4304 "ISO C forbids conditional expr between "
4305 "%<void *%> and function pointer");
4306 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4307 TREE_TYPE (type1)));
4309 /* Objective-C pointer comparisons are a bit more lenient. */
4310 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4311 result_type = objc_common_type (type1, type2);
4312 else
4314 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4316 pedwarn (colon_loc, 0,
4317 "pointer type mismatch in conditional expression");
4318 result_type = build_pointer_type
4319 (build_qualified_type (void_type_node, qual));
4322 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4324 if (!null_pointer_constant_p (orig_op2))
4325 pedwarn (colon_loc, 0,
4326 "pointer/integer type mismatch in conditional expression");
4327 else
4329 op2 = null_pointer_node;
4331 result_type = type1;
4333 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4335 if (!null_pointer_constant_p (orig_op1))
4336 pedwarn (colon_loc, 0,
4337 "pointer/integer type mismatch in conditional expression");
4338 else
4340 op1 = null_pointer_node;
4342 result_type = type2;
4345 if (!result_type)
4347 if (flag_cond_mismatch)
4348 result_type = void_type_node;
4349 else
4351 error_at (colon_loc, "type mismatch in conditional expression");
4352 return error_mark_node;
4356 /* Merge const and volatile flags of the incoming types. */
4357 result_type
4358 = build_type_variant (result_type,
4359 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4360 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4362 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4363 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4365 if (ifexp_bcp && ifexp == truthvalue_true_node)
4367 op2_int_operands = true;
4368 op1 = c_fully_fold (op1, require_constant_value, NULL);
4370 if (ifexp_bcp && ifexp == truthvalue_false_node)
4372 op1_int_operands = true;
4373 op2 = c_fully_fold (op2, require_constant_value, NULL);
4375 int_const = int_operands = (ifexp_int_operands
4376 && op1_int_operands
4377 && op2_int_operands);
4378 if (int_operands)
4380 int_const = ((ifexp == truthvalue_true_node
4381 && TREE_CODE (orig_op1) == INTEGER_CST
4382 && !TREE_OVERFLOW (orig_op1))
4383 || (ifexp == truthvalue_false_node
4384 && TREE_CODE (orig_op2) == INTEGER_CST
4385 && !TREE_OVERFLOW (orig_op2)));
4387 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4388 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4389 else
4391 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4392 if (int_operands)
4393 ret = note_integer_operands (ret);
4395 if (semantic_result_type)
4396 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4398 protected_set_expr_location (ret, colon_loc);
4399 return ret;
4402 /* Return a compound expression that performs two expressions and
4403 returns the value of the second of them.
4405 LOC is the location of the COMPOUND_EXPR. */
4407 tree
4408 build_compound_expr (location_t loc, tree expr1, tree expr2)
4410 bool expr1_int_operands, expr2_int_operands;
4411 tree eptype = NULL_TREE;
4412 tree ret;
4414 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4415 if (expr1_int_operands)
4416 expr1 = remove_c_maybe_const_expr (expr1);
4417 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4418 if (expr2_int_operands)
4419 expr2 = remove_c_maybe_const_expr (expr2);
4421 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4422 expr1 = TREE_OPERAND (expr1, 0);
4423 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4425 eptype = TREE_TYPE (expr2);
4426 expr2 = TREE_OPERAND (expr2, 0);
4429 if (!TREE_SIDE_EFFECTS (expr1))
4431 /* The left-hand operand of a comma expression is like an expression
4432 statement: with -Wunused, we should warn if it doesn't have
4433 any side-effects, unless it was explicitly cast to (void). */
4434 if (warn_unused_value)
4436 if (VOID_TYPE_P (TREE_TYPE (expr1))
4437 && CONVERT_EXPR_P (expr1))
4438 ; /* (void) a, b */
4439 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4440 && TREE_CODE (expr1) == COMPOUND_EXPR
4441 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4442 ; /* (void) a, (void) b, c */
4443 else
4444 warning_at (loc, OPT_Wunused_value,
4445 "left-hand operand of comma expression has no effect");
4449 /* With -Wunused, we should also warn if the left-hand operand does have
4450 side-effects, but computes a value which is not used. For example, in
4451 `foo() + bar(), baz()' the result of the `+' operator is not used,
4452 so we should issue a warning. */
4453 else if (warn_unused_value)
4454 warn_if_unused_value (expr1, loc);
4456 if (expr2 == error_mark_node)
4457 return error_mark_node;
4459 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4461 if (flag_isoc99
4462 && expr1_int_operands
4463 && expr2_int_operands)
4464 ret = note_integer_operands (ret);
4466 if (eptype)
4467 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4469 protected_set_expr_location (ret, loc);
4470 return ret;
4473 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4474 which we are casting. OTYPE is the type of the expression being
4475 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4476 of the cast. -Wcast-qual appeared on the command line. Named
4477 address space qualifiers are not handled here, because they result
4478 in different warnings. */
4480 static void
4481 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4483 tree in_type = type;
4484 tree in_otype = otype;
4485 int added = 0;
4486 int discarded = 0;
4487 bool is_const;
4489 /* Check that the qualifiers on IN_TYPE are a superset of the
4490 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4491 nodes is uninteresting and we stop as soon as we hit a
4492 non-POINTER_TYPE node on either type. */
4495 in_otype = TREE_TYPE (in_otype);
4496 in_type = TREE_TYPE (in_type);
4498 /* GNU C allows cv-qualified function types. 'const' means the
4499 function is very pure, 'volatile' means it can't return. We
4500 need to warn when such qualifiers are added, not when they're
4501 taken away. */
4502 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4503 && TREE_CODE (in_type) == FUNCTION_TYPE)
4504 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4505 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4506 else
4507 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4508 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4510 while (TREE_CODE (in_type) == POINTER_TYPE
4511 && TREE_CODE (in_otype) == POINTER_TYPE);
4513 if (added)
4514 warning_at (loc, OPT_Wcast_qual,
4515 "cast adds %q#v qualifier to function type", added);
4517 if (discarded)
4518 /* There are qualifiers present in IN_OTYPE that are not present
4519 in IN_TYPE. */
4520 warning_at (loc, OPT_Wcast_qual,
4521 "cast discards %q#v qualifier from pointer target type",
4522 discarded);
4524 if (added || discarded)
4525 return;
4527 /* A cast from **T to const **T is unsafe, because it can cause a
4528 const value to be changed with no additional warning. We only
4529 issue this warning if T is the same on both sides, and we only
4530 issue the warning if there are the same number of pointers on
4531 both sides, as otherwise the cast is clearly unsafe anyhow. A
4532 cast is unsafe when a qualifier is added at one level and const
4533 is not present at all outer levels.
4535 To issue this warning, we check at each level whether the cast
4536 adds new qualifiers not already seen. We don't need to special
4537 case function types, as they won't have the same
4538 TYPE_MAIN_VARIANT. */
4540 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4541 return;
4542 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4543 return;
4545 in_type = type;
4546 in_otype = otype;
4547 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4550 in_type = TREE_TYPE (in_type);
4551 in_otype = TREE_TYPE (in_otype);
4552 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4553 && !is_const)
4555 warning_at (loc, OPT_Wcast_qual,
4556 "to be safe all intermediate pointers in cast from "
4557 "%qT to %qT must be %<const%> qualified",
4558 otype, type);
4559 break;
4561 if (is_const)
4562 is_const = TYPE_READONLY (in_type);
4564 while (TREE_CODE (in_type) == POINTER_TYPE);
4567 /* Build an expression representing a cast to type TYPE of expression EXPR.
4568 LOC is the location of the cast-- typically the open paren of the cast. */
4570 tree
4571 build_c_cast (location_t loc, tree type, tree expr)
4573 tree value;
4575 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4576 expr = TREE_OPERAND (expr, 0);
4578 value = expr;
4580 if (type == error_mark_node || expr == error_mark_node)
4581 return error_mark_node;
4583 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4584 only in <protocol> qualifications. But when constructing cast expressions,
4585 the protocols do matter and must be kept around. */
4586 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4587 return build1 (NOP_EXPR, type, expr);
4589 type = TYPE_MAIN_VARIANT (type);
4591 if (TREE_CODE (type) == ARRAY_TYPE)
4593 error_at (loc, "cast specifies array type");
4594 return error_mark_node;
4597 if (TREE_CODE (type) == FUNCTION_TYPE)
4599 error_at (loc, "cast specifies function type");
4600 return error_mark_node;
4603 if (!VOID_TYPE_P (type))
4605 value = require_complete_type (value);
4606 if (value == error_mark_node)
4607 return error_mark_node;
4610 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4612 if (TREE_CODE (type) == RECORD_TYPE
4613 || TREE_CODE (type) == UNION_TYPE)
4614 pedwarn (loc, OPT_pedantic,
4615 "ISO C forbids casting nonscalar to the same type");
4617 else if (TREE_CODE (type) == UNION_TYPE)
4619 tree field;
4621 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4622 if (TREE_TYPE (field) != error_mark_node
4623 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4624 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4625 break;
4627 if (field)
4629 tree t;
4630 bool maybe_const = true;
4632 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4633 t = c_fully_fold (value, false, &maybe_const);
4634 t = build_constructor_single (type, field, t);
4635 if (!maybe_const)
4636 t = c_wrap_maybe_const (t, true);
4637 t = digest_init (loc, type, t,
4638 NULL_TREE, false, true, 0);
4639 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4640 return t;
4642 error_at (loc, "cast to union type from type not present in union");
4643 return error_mark_node;
4645 else
4647 tree otype, ovalue;
4649 if (type == void_type_node)
4651 tree t = build1 (CONVERT_EXPR, type, value);
4652 SET_EXPR_LOCATION (t, loc);
4653 return t;
4656 otype = TREE_TYPE (value);
4658 /* Optionally warn about potentially worrisome casts. */
4659 if (warn_cast_qual
4660 && TREE_CODE (type) == POINTER_TYPE
4661 && TREE_CODE (otype) == POINTER_TYPE)
4662 handle_warn_cast_qual (loc, type, otype);
4664 /* Warn about conversions between pointers to disjoint
4665 address spaces. */
4666 if (TREE_CODE (type) == POINTER_TYPE
4667 && TREE_CODE (otype) == POINTER_TYPE
4668 && !null_pointer_constant_p (value))
4670 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4671 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4672 addr_space_t as_common;
4674 if (!addr_space_superset (as_to, as_from, &as_common))
4676 if (ADDR_SPACE_GENERIC_P (as_from))
4677 warning_at (loc, 0, "cast to %s address space pointer "
4678 "from disjoint generic address space pointer",
4679 c_addr_space_name (as_to));
4681 else if (ADDR_SPACE_GENERIC_P (as_to))
4682 warning_at (loc, 0, "cast to generic address space pointer "
4683 "from disjoint %s address space pointer",
4684 c_addr_space_name (as_from));
4686 else
4687 warning_at (loc, 0, "cast to %s address space pointer "
4688 "from disjoint %s address space pointer",
4689 c_addr_space_name (as_to),
4690 c_addr_space_name (as_from));
4694 /* Warn about possible alignment problems. */
4695 if (STRICT_ALIGNMENT
4696 && TREE_CODE (type) == POINTER_TYPE
4697 && TREE_CODE (otype) == POINTER_TYPE
4698 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4699 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4700 /* Don't warn about opaque types, where the actual alignment
4701 restriction is unknown. */
4702 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4703 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4704 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4705 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4706 warning_at (loc, OPT_Wcast_align,
4707 "cast increases required alignment of target type");
4709 if (TREE_CODE (type) == INTEGER_TYPE
4710 && TREE_CODE (otype) == POINTER_TYPE
4711 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4712 /* Unlike conversion of integers to pointers, where the
4713 warning is disabled for converting constants because
4714 of cases such as SIG_*, warn about converting constant
4715 pointers to integers. In some cases it may cause unwanted
4716 sign extension, and a warning is appropriate. */
4717 warning_at (loc, OPT_Wpointer_to_int_cast,
4718 "cast from pointer to integer of different size");
4720 if (TREE_CODE (value) == CALL_EXPR
4721 && TREE_CODE (type) != TREE_CODE (otype))
4722 warning_at (loc, OPT_Wbad_function_cast,
4723 "cast from function call of type %qT "
4724 "to non-matching type %qT", otype, type);
4726 if (TREE_CODE (type) == POINTER_TYPE
4727 && TREE_CODE (otype) == INTEGER_TYPE
4728 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4729 /* Don't warn about converting any constant. */
4730 && !TREE_CONSTANT (value))
4731 warning_at (loc,
4732 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4733 "of different size");
4735 if (warn_strict_aliasing <= 2)
4736 strict_aliasing_warning (otype, type, expr);
4738 /* If pedantic, warn for conversions between function and object
4739 pointer types, except for converting a null pointer constant
4740 to function pointer type. */
4741 if (pedantic
4742 && TREE_CODE (type) == POINTER_TYPE
4743 && TREE_CODE (otype) == POINTER_TYPE
4744 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4745 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4746 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4747 "conversion of function pointer to object pointer type");
4749 if (pedantic
4750 && TREE_CODE (type) == POINTER_TYPE
4751 && TREE_CODE (otype) == POINTER_TYPE
4752 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4753 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4754 && !null_pointer_constant_p (value))
4755 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4756 "conversion of object pointer to function pointer type");
4758 ovalue = value;
4759 value = convert (type, value);
4761 /* Ignore any integer overflow caused by the cast. */
4762 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4764 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4766 if (!TREE_OVERFLOW (value))
4768 /* Avoid clobbering a shared constant. */
4769 value = copy_node (value);
4770 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4773 else if (TREE_OVERFLOW (value))
4774 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4775 value = build_int_cst_wide (TREE_TYPE (value),
4776 TREE_INT_CST_LOW (value),
4777 TREE_INT_CST_HIGH (value));
4781 /* Don't let a cast be an lvalue. */
4782 if (value == expr)
4783 value = non_lvalue_loc (loc, value);
4785 /* Don't allow the results of casting to floating-point or complex
4786 types be confused with actual constants, or casts involving
4787 integer and pointer types other than direct integer-to-integer
4788 and integer-to-pointer be confused with integer constant
4789 expressions and null pointer constants. */
4790 if (TREE_CODE (value) == REAL_CST
4791 || TREE_CODE (value) == COMPLEX_CST
4792 || (TREE_CODE (value) == INTEGER_CST
4793 && !((TREE_CODE (expr) == INTEGER_CST
4794 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4795 || TREE_CODE (expr) == REAL_CST
4796 || TREE_CODE (expr) == COMPLEX_CST)))
4797 value = build1 (NOP_EXPR, type, value);
4799 if (CAN_HAVE_LOCATION_P (value))
4800 SET_EXPR_LOCATION (value, loc);
4801 return value;
4804 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4805 location of the open paren of the cast, or the position of the cast
4806 expr. */
4807 tree
4808 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4810 tree type;
4811 tree type_expr = NULL_TREE;
4812 bool type_expr_const = true;
4813 tree ret;
4814 int saved_wsp = warn_strict_prototypes;
4816 /* This avoids warnings about unprototyped casts on
4817 integers. E.g. "#define SIG_DFL (void(*)())0". */
4818 if (TREE_CODE (expr) == INTEGER_CST)
4819 warn_strict_prototypes = 0;
4820 type = groktypename (type_name, &type_expr, &type_expr_const);
4821 warn_strict_prototypes = saved_wsp;
4823 ret = build_c_cast (loc, type, expr);
4824 if (type_expr)
4826 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4827 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4828 SET_EXPR_LOCATION (ret, loc);
4831 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4832 SET_EXPR_LOCATION (ret, loc);
4834 /* C++ does not permits types to be defined in a cast. */
4835 if (warn_cxx_compat && type_name->specs->tag_defined_p)
4836 warning_at (loc, OPT_Wc___compat,
4837 "defining a type in a cast is invalid in C++");
4839 return ret;
4842 /* Build an assignment expression of lvalue LHS from value RHS.
4843 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4844 may differ from TREE_TYPE (LHS) for an enum bitfield.
4845 MODIFYCODE is the code for a binary operator that we use
4846 to combine the old value of LHS with RHS to get the new value.
4847 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4848 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4849 which may differ from TREE_TYPE (RHS) for an enum value.
4851 LOCATION is the location of the MODIFYCODE operator.
4852 RHS_LOC is the location of the RHS. */
4854 tree
4855 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4856 enum tree_code modifycode,
4857 location_t rhs_loc, tree rhs, tree rhs_origtype)
4859 tree result;
4860 tree newrhs;
4861 tree rhs_semantic_type = NULL_TREE;
4862 tree lhstype = TREE_TYPE (lhs);
4863 tree olhstype = lhstype;
4864 bool npc;
4866 /* Types that aren't fully specified cannot be used in assignments. */
4867 lhs = require_complete_type (lhs);
4869 /* Avoid duplicate error messages from operands that had errors. */
4870 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4871 return error_mark_node;
4873 /* For ObjC properties, defer this check. */
4874 if (!objc_is_property_ref (lhs) && !lvalue_or_else (lhs, lv_assign))
4875 return error_mark_node;
4877 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4879 rhs_semantic_type = TREE_TYPE (rhs);
4880 rhs = TREE_OPERAND (rhs, 0);
4883 newrhs = rhs;
4885 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4887 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4888 lhs_origtype, modifycode, rhs_loc, rhs,
4889 rhs_origtype);
4890 if (inner == error_mark_node)
4891 return error_mark_node;
4892 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4893 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4894 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4895 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4896 protected_set_expr_location (result, location);
4897 return result;
4900 /* If a binary op has been requested, combine the old LHS value with the RHS
4901 producing the value we should actually store into the LHS. */
4903 if (modifycode != NOP_EXPR)
4905 lhs = c_fully_fold (lhs, false, NULL);
4906 lhs = stabilize_reference (lhs);
4907 newrhs = build_binary_op (location,
4908 modifycode, lhs, rhs, 1);
4910 /* The original type of the right hand side is no longer
4911 meaningful. */
4912 rhs_origtype = NULL_TREE;
4915 if (c_dialect_objc ())
4917 /* Check if we are modifying an Objective-C property reference;
4918 if so, we need to generate setter calls. */
4919 result = objc_maybe_build_modify_expr (lhs, newrhs);
4920 if (result)
4921 return result;
4923 /* Else, do the check that we postponed for Objective-C. */
4924 if (!lvalue_or_else (lhs, lv_assign))
4925 return error_mark_node;
4928 /* Give an error for storing in something that is 'const'. */
4930 if (TYPE_READONLY (lhstype)
4931 || ((TREE_CODE (lhstype) == RECORD_TYPE
4932 || TREE_CODE (lhstype) == UNION_TYPE)
4933 && C_TYPE_FIELDS_READONLY (lhstype)))
4935 readonly_error (lhs, lv_assign);
4936 return error_mark_node;
4938 else if (TREE_READONLY (lhs))
4939 readonly_warning (lhs, lv_assign);
4941 /* If storing into a structure or union member,
4942 it has probably been given type `int'.
4943 Compute the type that would go with
4944 the actual amount of storage the member occupies. */
4946 if (TREE_CODE (lhs) == COMPONENT_REF
4947 && (TREE_CODE (lhstype) == INTEGER_TYPE
4948 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4949 || TREE_CODE (lhstype) == REAL_TYPE
4950 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4951 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4953 /* If storing in a field that is in actuality a short or narrower than one,
4954 we must store in the field in its actual type. */
4956 if (lhstype != TREE_TYPE (lhs))
4958 lhs = copy_node (lhs);
4959 TREE_TYPE (lhs) = lhstype;
4962 /* Issue -Wc++-compat warnings about an assignment to an enum type
4963 when LHS does not have its original type. This happens for,
4964 e.g., an enum bitfield in a struct. */
4965 if (warn_cxx_compat
4966 && lhs_origtype != NULL_TREE
4967 && lhs_origtype != lhstype
4968 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4970 tree checktype = (rhs_origtype != NULL_TREE
4971 ? rhs_origtype
4972 : TREE_TYPE (rhs));
4973 if (checktype != error_mark_node
4974 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4975 warning_at (location, OPT_Wc___compat,
4976 "enum conversion in assignment is invalid in C++");
4979 /* Convert new value to destination type. Fold it first, then
4980 restore any excess precision information, for the sake of
4981 conversion warnings. */
4983 npc = null_pointer_constant_p (newrhs);
4984 newrhs = c_fully_fold (newrhs, false, NULL);
4985 if (rhs_semantic_type)
4986 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4987 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4988 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4989 if (TREE_CODE (newrhs) == ERROR_MARK)
4990 return error_mark_node;
4992 /* Emit ObjC write barrier, if necessary. */
4993 if (c_dialect_objc () && flag_objc_gc)
4995 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4996 if (result)
4998 protected_set_expr_location (result, location);
4999 return result;
5003 /* Scan operands. */
5005 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5006 TREE_SIDE_EFFECTS (result) = 1;
5007 protected_set_expr_location (result, location);
5009 /* If we got the LHS in a different type for storing in,
5010 convert the result back to the nominal type of LHS
5011 so that the value we return always has the same type
5012 as the LHS argument. */
5014 if (olhstype == TREE_TYPE (result))
5015 return result;
5017 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5018 ic_assign, false, NULL_TREE, NULL_TREE, 0);
5019 protected_set_expr_location (result, location);
5020 return result;
5023 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5024 This is used to implement -fplan9-extensions. */
5026 static bool
5027 find_anonymous_field_with_type (tree struct_type, tree type)
5029 tree field;
5030 bool found;
5032 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5033 || TREE_CODE (struct_type) == UNION_TYPE);
5034 found = false;
5035 for (field = TYPE_FIELDS (struct_type);
5036 field != NULL_TREE;
5037 field = TREE_CHAIN (field))
5039 if (DECL_NAME (field) == NULL
5040 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5042 if (found)
5043 return false;
5044 found = true;
5046 else if (DECL_NAME (field) == NULL
5047 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5048 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5049 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5051 if (found)
5052 return false;
5053 found = true;
5056 return found;
5059 /* RHS is an expression whose type is pointer to struct. If there is
5060 an anonymous field in RHS with type TYPE, then return a pointer to
5061 that field in RHS. This is used with -fplan9-extensions. This
5062 returns NULL if no conversion could be found. */
5064 static tree
5065 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5067 tree rhs_struct_type, lhs_main_type;
5068 tree field, found_field;
5069 bool found_sub_field;
5070 tree ret;
5072 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5073 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5074 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5075 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5077 gcc_assert (POINTER_TYPE_P (type));
5078 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5080 found_field = NULL_TREE;
5081 found_sub_field = false;
5082 for (field = TYPE_FIELDS (rhs_struct_type);
5083 field != NULL_TREE;
5084 field = TREE_CHAIN (field))
5086 if (DECL_NAME (field) != NULL_TREE
5087 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5088 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5089 continue;
5090 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5092 if (found_field != NULL_TREE)
5093 return NULL_TREE;
5094 found_field = field;
5096 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5097 lhs_main_type))
5099 if (found_field != NULL_TREE)
5100 return NULL_TREE;
5101 found_field = field;
5102 found_sub_field = true;
5106 if (found_field == NULL_TREE)
5107 return NULL_TREE;
5109 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5110 build_fold_indirect_ref (rhs), found_field,
5111 NULL_TREE);
5112 ret = build_fold_addr_expr_loc (location, ret);
5114 if (found_sub_field)
5116 ret = convert_to_anonymous_field (location, type, ret);
5117 gcc_assert (ret != NULL_TREE);
5120 return ret;
5123 /* Convert value RHS to type TYPE as preparation for an assignment to
5124 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5125 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5126 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5127 constant before any folding.
5128 The real work of conversion is done by `convert'.
5129 The purpose of this function is to generate error messages
5130 for assignments that are not allowed in C.
5131 ERRTYPE says whether it is argument passing, assignment,
5132 initialization or return.
5134 LOCATION is the location of the RHS.
5135 FUNCTION is a tree for the function being called.
5136 PARMNUM is the number of the argument, for printing in error messages. */
5138 static tree
5139 convert_for_assignment (location_t location, tree type, tree rhs,
5140 tree origtype, enum impl_conv errtype,
5141 bool null_pointer_constant, tree fundecl,
5142 tree function, int parmnum)
5144 enum tree_code codel = TREE_CODE (type);
5145 tree orig_rhs = rhs;
5146 tree rhstype;
5147 enum tree_code coder;
5148 tree rname = NULL_TREE;
5149 bool objc_ok = false;
5151 if (errtype == ic_argpass)
5153 tree selector;
5154 /* Change pointer to function to the function itself for
5155 diagnostics. */
5156 if (TREE_CODE (function) == ADDR_EXPR
5157 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5158 function = TREE_OPERAND (function, 0);
5160 /* Handle an ObjC selector specially for diagnostics. */
5161 selector = objc_message_selector ();
5162 rname = function;
5163 if (selector && parmnum > 2)
5165 rname = selector;
5166 parmnum -= 2;
5170 /* This macro is used to emit diagnostics to ensure that all format
5171 strings are complete sentences, visible to gettext and checked at
5172 compile time. */
5173 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5174 do { \
5175 switch (errtype) \
5177 case ic_argpass: \
5178 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5179 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5180 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5181 "expected %qT but argument is of type %qT", \
5182 type, rhstype); \
5183 break; \
5184 case ic_assign: \
5185 pedwarn (LOCATION, OPT, AS); \
5186 break; \
5187 case ic_init: \
5188 pedwarn_init (LOCATION, OPT, IN); \
5189 break; \
5190 case ic_return: \
5191 pedwarn (LOCATION, OPT, RE); \
5192 break; \
5193 default: \
5194 gcc_unreachable (); \
5196 } while (0)
5198 /* This macro is used to emit diagnostics to ensure that all format
5199 strings are complete sentences, visible to gettext and checked at
5200 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5201 extra parameter to enumerate qualifiers. */
5203 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5204 do { \
5205 switch (errtype) \
5207 case ic_argpass: \
5208 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5209 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5210 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5211 "expected %qT but argument is of type %qT", \
5212 type, rhstype); \
5213 break; \
5214 case ic_assign: \
5215 pedwarn (LOCATION, OPT, AS, QUALS); \
5216 break; \
5217 case ic_init: \
5218 pedwarn (LOCATION, OPT, IN, QUALS); \
5219 break; \
5220 case ic_return: \
5221 pedwarn (LOCATION, OPT, RE, QUALS); \
5222 break; \
5223 default: \
5224 gcc_unreachable (); \
5226 } while (0)
5228 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5229 rhs = TREE_OPERAND (rhs, 0);
5231 rhstype = TREE_TYPE (rhs);
5232 coder = TREE_CODE (rhstype);
5234 if (coder == ERROR_MARK)
5235 return error_mark_node;
5237 if (c_dialect_objc ())
5239 int parmno;
5241 switch (errtype)
5243 case ic_return:
5244 parmno = 0;
5245 break;
5247 case ic_assign:
5248 parmno = -1;
5249 break;
5251 case ic_init:
5252 parmno = -2;
5253 break;
5255 default:
5256 parmno = parmnum;
5257 break;
5260 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5263 if (warn_cxx_compat)
5265 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5266 if (checktype != error_mark_node
5267 && TREE_CODE (type) == ENUMERAL_TYPE
5268 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5270 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5271 G_("enum conversion when passing argument "
5272 "%d of %qE is invalid in C++"),
5273 G_("enum conversion in assignment is "
5274 "invalid in C++"),
5275 G_("enum conversion in initialization is "
5276 "invalid in C++"),
5277 G_("enum conversion in return is "
5278 "invalid in C++"));
5282 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5283 return rhs;
5285 if (coder == VOID_TYPE)
5287 /* Except for passing an argument to an unprototyped function,
5288 this is a constraint violation. When passing an argument to
5289 an unprototyped function, it is compile-time undefined;
5290 making it a constraint in that case was rejected in
5291 DR#252. */
5292 error_at (location, "void value not ignored as it ought to be");
5293 return error_mark_node;
5295 rhs = require_complete_type (rhs);
5296 if (rhs == error_mark_node)
5297 return error_mark_node;
5298 /* A type converts to a reference to it.
5299 This code doesn't fully support references, it's just for the
5300 special case of va_start and va_copy. */
5301 if (codel == REFERENCE_TYPE
5302 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5304 if (!lvalue_p (rhs))
5306 error_at (location, "cannot pass rvalue to reference parameter");
5307 return error_mark_node;
5309 if (!c_mark_addressable (rhs))
5310 return error_mark_node;
5311 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5312 SET_EXPR_LOCATION (rhs, location);
5314 /* We already know that these two types are compatible, but they
5315 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5316 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5317 likely to be va_list, a typedef to __builtin_va_list, which
5318 is different enough that it will cause problems later. */
5319 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5321 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5322 SET_EXPR_LOCATION (rhs, location);
5325 rhs = build1 (NOP_EXPR, type, rhs);
5326 SET_EXPR_LOCATION (rhs, location);
5327 return rhs;
5329 /* Some types can interconvert without explicit casts. */
5330 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5331 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5332 return convert (type, rhs);
5333 /* Arithmetic types all interconvert, and enum is treated like int. */
5334 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5335 || codel == FIXED_POINT_TYPE
5336 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5337 || codel == BOOLEAN_TYPE)
5338 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5339 || coder == FIXED_POINT_TYPE
5340 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5341 || coder == BOOLEAN_TYPE))
5343 tree ret;
5344 bool save = in_late_binary_op;
5345 if (codel == BOOLEAN_TYPE)
5346 in_late_binary_op = true;
5347 ret = convert_and_check (type, orig_rhs);
5348 if (codel == BOOLEAN_TYPE)
5349 in_late_binary_op = save;
5350 return ret;
5353 /* Aggregates in different TUs might need conversion. */
5354 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5355 && codel == coder
5356 && comptypes (type, rhstype))
5357 return convert_and_check (type, rhs);
5359 /* Conversion to a transparent union or record from its member types.
5360 This applies only to function arguments. */
5361 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5362 && TYPE_TRANSPARENT_AGGR (type))
5363 && errtype == ic_argpass)
5365 tree memb, marginal_memb = NULL_TREE;
5367 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5369 tree memb_type = TREE_TYPE (memb);
5371 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5372 TYPE_MAIN_VARIANT (rhstype)))
5373 break;
5375 if (TREE_CODE (memb_type) != POINTER_TYPE)
5376 continue;
5378 if (coder == POINTER_TYPE)
5380 tree ttl = TREE_TYPE (memb_type);
5381 tree ttr = TREE_TYPE (rhstype);
5383 /* Any non-function converts to a [const][volatile] void *
5384 and vice versa; otherwise, targets must be the same.
5385 Meanwhile, the lhs target must have all the qualifiers of
5386 the rhs. */
5387 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5388 || comp_target_types (location, memb_type, rhstype))
5390 /* If this type won't generate any warnings, use it. */
5391 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5392 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5393 && TREE_CODE (ttl) == FUNCTION_TYPE)
5394 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5395 == TYPE_QUALS (ttr))
5396 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5397 == TYPE_QUALS (ttl))))
5398 break;
5400 /* Keep looking for a better type, but remember this one. */
5401 if (!marginal_memb)
5402 marginal_memb = memb;
5406 /* Can convert integer zero to any pointer type. */
5407 if (null_pointer_constant)
5409 rhs = null_pointer_node;
5410 break;
5414 if (memb || marginal_memb)
5416 if (!memb)
5418 /* We have only a marginally acceptable member type;
5419 it needs a warning. */
5420 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5421 tree ttr = TREE_TYPE (rhstype);
5423 /* Const and volatile mean something different for function
5424 types, so the usual warnings are not appropriate. */
5425 if (TREE_CODE (ttr) == FUNCTION_TYPE
5426 && TREE_CODE (ttl) == FUNCTION_TYPE)
5428 /* Because const and volatile on functions are
5429 restrictions that say the function will not do
5430 certain things, it is okay to use a const or volatile
5431 function where an ordinary one is wanted, but not
5432 vice-versa. */
5433 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5434 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5435 WARN_FOR_QUALIFIERS (location, 0,
5436 G_("passing argument %d of %qE "
5437 "makes %q#v qualified function "
5438 "pointer from unqualified"),
5439 G_("assignment makes %q#v qualified "
5440 "function pointer from "
5441 "unqualified"),
5442 G_("initialization makes %q#v qualified "
5443 "function pointer from "
5444 "unqualified"),
5445 G_("return makes %q#v qualified function "
5446 "pointer from unqualified"),
5447 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5449 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5450 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5451 WARN_FOR_QUALIFIERS (location, 0,
5452 G_("passing argument %d of %qE discards "
5453 "%qv qualifier from pointer target type"),
5454 G_("assignment discards %qv qualifier "
5455 "from pointer target type"),
5456 G_("initialization discards %qv qualifier "
5457 "from pointer target type"),
5458 G_("return discards %qv qualifier from "
5459 "pointer target type"),
5460 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5462 memb = marginal_memb;
5465 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5466 pedwarn (location, OPT_pedantic,
5467 "ISO C prohibits argument conversion to union type");
5469 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5470 return build_constructor_single (type, memb, rhs);
5474 /* Conversions among pointers */
5475 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5476 && (coder == codel))
5478 tree ttl = TREE_TYPE (type);
5479 tree ttr = TREE_TYPE (rhstype);
5480 tree mvl = ttl;
5481 tree mvr = ttr;
5482 bool is_opaque_pointer;
5483 int target_cmp = 0; /* Cache comp_target_types () result. */
5484 addr_space_t asl;
5485 addr_space_t asr;
5487 if (TREE_CODE (mvl) != ARRAY_TYPE)
5488 mvl = TYPE_MAIN_VARIANT (mvl);
5489 if (TREE_CODE (mvr) != ARRAY_TYPE)
5490 mvr = TYPE_MAIN_VARIANT (mvr);
5491 /* Opaque pointers are treated like void pointers. */
5492 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5494 /* The Plan 9 compiler permits a pointer to a struct to be
5495 automatically converted into a pointer to an anonymous field
5496 within the struct. */
5497 if (flag_plan9_extensions
5498 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5499 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5500 && mvl != mvr)
5502 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5503 if (new_rhs != NULL_TREE)
5505 rhs = new_rhs;
5506 rhstype = TREE_TYPE (rhs);
5507 coder = TREE_CODE (rhstype);
5508 ttr = TREE_TYPE (rhstype);
5509 mvr = TYPE_MAIN_VARIANT (ttr);
5513 /* C++ does not allow the implicit conversion void* -> T*. However,
5514 for the purpose of reducing the number of false positives, we
5515 tolerate the special case of
5517 int *p = NULL;
5519 where NULL is typically defined in C to be '(void *) 0'. */
5520 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5521 warning_at (location, OPT_Wc___compat,
5522 "request for implicit conversion "
5523 "from %qT to %qT not permitted in C++", rhstype, type);
5525 /* See if the pointers point to incompatible address spaces. */
5526 asl = TYPE_ADDR_SPACE (ttl);
5527 asr = TYPE_ADDR_SPACE (ttr);
5528 if (!null_pointer_constant_p (rhs)
5529 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5531 switch (errtype)
5533 case ic_argpass:
5534 error_at (location, "passing argument %d of %qE from pointer to "
5535 "non-enclosed address space", parmnum, rname);
5536 break;
5537 case ic_assign:
5538 error_at (location, "assignment from pointer to "
5539 "non-enclosed address space");
5540 break;
5541 case ic_init:
5542 error_at (location, "initialization from pointer to "
5543 "non-enclosed address space");
5544 break;
5545 case ic_return:
5546 error_at (location, "return from pointer to "
5547 "non-enclosed address space");
5548 break;
5549 default:
5550 gcc_unreachable ();
5552 return error_mark_node;
5555 /* Check if the right-hand side has a format attribute but the
5556 left-hand side doesn't. */
5557 if (warn_missing_format_attribute
5558 && check_missing_format_attribute (type, rhstype))
5560 switch (errtype)
5562 case ic_argpass:
5563 warning_at (location, OPT_Wmissing_format_attribute,
5564 "argument %d of %qE might be "
5565 "a candidate for a format attribute",
5566 parmnum, rname);
5567 break;
5568 case ic_assign:
5569 warning_at (location, OPT_Wmissing_format_attribute,
5570 "assignment left-hand side might be "
5571 "a candidate for a format attribute");
5572 break;
5573 case ic_init:
5574 warning_at (location, OPT_Wmissing_format_attribute,
5575 "initialization left-hand side might be "
5576 "a candidate for a format attribute");
5577 break;
5578 case ic_return:
5579 warning_at (location, OPT_Wmissing_format_attribute,
5580 "return type might be "
5581 "a candidate for a format attribute");
5582 break;
5583 default:
5584 gcc_unreachable ();
5588 /* Any non-function converts to a [const][volatile] void *
5589 and vice versa; otherwise, targets must be the same.
5590 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5591 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5592 || (target_cmp = comp_target_types (location, type, rhstype))
5593 || is_opaque_pointer
5594 || (c_common_unsigned_type (mvl)
5595 == c_common_unsigned_type (mvr)))
5597 if (pedantic
5598 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5600 (VOID_TYPE_P (ttr)
5601 && !null_pointer_constant
5602 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5603 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5604 G_("ISO C forbids passing argument %d of "
5605 "%qE between function pointer "
5606 "and %<void *%>"),
5607 G_("ISO C forbids assignment between "
5608 "function pointer and %<void *%>"),
5609 G_("ISO C forbids initialization between "
5610 "function pointer and %<void *%>"),
5611 G_("ISO C forbids return between function "
5612 "pointer and %<void *%>"));
5613 /* Const and volatile mean something different for function types,
5614 so the usual warnings are not appropriate. */
5615 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5616 && TREE_CODE (ttl) != FUNCTION_TYPE)
5618 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5619 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5621 /* Types differing only by the presence of the 'volatile'
5622 qualifier are acceptable if the 'volatile' has been added
5623 in by the Objective-C EH machinery. */
5624 if (!objc_type_quals_match (ttl, ttr))
5625 WARN_FOR_QUALIFIERS (location, 0,
5626 G_("passing argument %d of %qE discards "
5627 "%qv qualifier from pointer target type"),
5628 G_("assignment discards %qv qualifier "
5629 "from pointer target type"),
5630 G_("initialization discards %qv qualifier "
5631 "from pointer target type"),
5632 G_("return discards %qv qualifier from "
5633 "pointer target type"),
5634 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5636 /* If this is not a case of ignoring a mismatch in signedness,
5637 no warning. */
5638 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5639 || target_cmp)
5641 /* If there is a mismatch, do warn. */
5642 else if (warn_pointer_sign)
5643 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5644 G_("pointer targets in passing argument "
5645 "%d of %qE differ in signedness"),
5646 G_("pointer targets in assignment "
5647 "differ in signedness"),
5648 G_("pointer targets in initialization "
5649 "differ in signedness"),
5650 G_("pointer targets in return differ "
5651 "in signedness"));
5653 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5654 && TREE_CODE (ttr) == FUNCTION_TYPE)
5656 /* Because const and volatile on functions are restrictions
5657 that say the function will not do certain things,
5658 it is okay to use a const or volatile function
5659 where an ordinary one is wanted, but not vice-versa. */
5660 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5661 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5662 WARN_FOR_QUALIFIERS (location, 0,
5663 G_("passing argument %d of %qE makes "
5664 "%q#v qualified function pointer "
5665 "from unqualified"),
5666 G_("assignment makes %q#v qualified function "
5667 "pointer from unqualified"),
5668 G_("initialization makes %q#v qualified "
5669 "function pointer from unqualified"),
5670 G_("return makes %q#v qualified function "
5671 "pointer from unqualified"),
5672 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5675 else
5676 /* Avoid warning about the volatile ObjC EH puts on decls. */
5677 if (!objc_ok)
5678 WARN_FOR_ASSIGNMENT (location, 0,
5679 G_("passing argument %d of %qE from "
5680 "incompatible pointer type"),
5681 G_("assignment from incompatible pointer type"),
5682 G_("initialization from incompatible "
5683 "pointer type"),
5684 G_("return from incompatible pointer type"));
5686 return convert (type, rhs);
5688 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5690 /* ??? This should not be an error when inlining calls to
5691 unprototyped functions. */
5692 error_at (location, "invalid use of non-lvalue array");
5693 return error_mark_node;
5695 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5697 /* An explicit constant 0 can convert to a pointer,
5698 or one that results from arithmetic, even including
5699 a cast to integer type. */
5700 if (!null_pointer_constant)
5701 WARN_FOR_ASSIGNMENT (location, 0,
5702 G_("passing argument %d of %qE makes "
5703 "pointer from integer without a cast"),
5704 G_("assignment makes pointer from integer "
5705 "without a cast"),
5706 G_("initialization makes pointer from "
5707 "integer without a cast"),
5708 G_("return makes pointer from integer "
5709 "without a cast"));
5711 return convert (type, rhs);
5713 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5715 WARN_FOR_ASSIGNMENT (location, 0,
5716 G_("passing argument %d of %qE makes integer "
5717 "from pointer without a cast"),
5718 G_("assignment makes integer from pointer "
5719 "without a cast"),
5720 G_("initialization makes integer from pointer "
5721 "without a cast"),
5722 G_("return makes integer from pointer "
5723 "without a cast"));
5724 return convert (type, rhs);
5726 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5728 tree ret;
5729 bool save = in_late_binary_op;
5730 in_late_binary_op = true;
5731 ret = convert (type, rhs);
5732 in_late_binary_op = save;
5733 return ret;
5736 switch (errtype)
5738 case ic_argpass:
5739 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5740 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5741 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5742 "expected %qT but argument is of type %qT", type, rhstype);
5743 break;
5744 case ic_assign:
5745 error_at (location, "incompatible types when assigning to type %qT from "
5746 "type %qT", type, rhstype);
5747 break;
5748 case ic_init:
5749 error_at (location,
5750 "incompatible types when initializing type %qT using type %qT",
5751 type, rhstype);
5752 break;
5753 case ic_return:
5754 error_at (location,
5755 "incompatible types when returning type %qT but %qT was "
5756 "expected", rhstype, type);
5757 break;
5758 default:
5759 gcc_unreachable ();
5762 return error_mark_node;
5765 /* If VALUE is a compound expr all of whose expressions are constant, then
5766 return its value. Otherwise, return error_mark_node.
5768 This is for handling COMPOUND_EXPRs as initializer elements
5769 which is allowed with a warning when -pedantic is specified. */
5771 static tree
5772 valid_compound_expr_initializer (tree value, tree endtype)
5774 if (TREE_CODE (value) == COMPOUND_EXPR)
5776 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5777 == error_mark_node)
5778 return error_mark_node;
5779 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5780 endtype);
5782 else if (!initializer_constant_valid_p (value, endtype))
5783 return error_mark_node;
5784 else
5785 return value;
5788 /* Perform appropriate conversions on the initial value of a variable,
5789 store it in the declaration DECL,
5790 and print any error messages that are appropriate.
5791 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5792 If the init is invalid, store an ERROR_MARK.
5794 INIT_LOC is the location of the initial value. */
5796 void
5797 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5799 tree value, type;
5800 bool npc = false;
5802 /* If variable's type was invalidly declared, just ignore it. */
5804 type = TREE_TYPE (decl);
5805 if (TREE_CODE (type) == ERROR_MARK)
5806 return;
5808 /* Digest the specified initializer into an expression. */
5810 if (init)
5811 npc = null_pointer_constant_p (init);
5812 value = digest_init (init_loc, type, init, origtype, npc,
5813 true, TREE_STATIC (decl));
5815 /* Store the expression if valid; else report error. */
5817 if (!in_system_header
5818 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5819 warning (OPT_Wtraditional, "traditional C rejects automatic "
5820 "aggregate initialization");
5822 DECL_INITIAL (decl) = value;
5824 /* ANSI wants warnings about out-of-range constant initializers. */
5825 STRIP_TYPE_NOPS (value);
5826 if (TREE_STATIC (decl))
5827 constant_expression_warning (value);
5829 /* Check if we need to set array size from compound literal size. */
5830 if (TREE_CODE (type) == ARRAY_TYPE
5831 && TYPE_DOMAIN (type) == 0
5832 && value != error_mark_node)
5834 tree inside_init = init;
5836 STRIP_TYPE_NOPS (inside_init);
5837 inside_init = fold (inside_init);
5839 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5841 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5843 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5845 /* For int foo[] = (int [3]){1}; we need to set array size
5846 now since later on array initializer will be just the
5847 brace enclosed list of the compound literal. */
5848 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5849 TREE_TYPE (decl) = type;
5850 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5851 layout_type (type);
5852 layout_decl (cldecl, 0);
5858 /* Methods for storing and printing names for error messages. */
5860 /* Implement a spelling stack that allows components of a name to be pushed
5861 and popped. Each element on the stack is this structure. */
5863 struct spelling
5865 int kind;
5866 union
5868 unsigned HOST_WIDE_INT i;
5869 const char *s;
5870 } u;
5873 #define SPELLING_STRING 1
5874 #define SPELLING_MEMBER 2
5875 #define SPELLING_BOUNDS 3
5877 static struct spelling *spelling; /* Next stack element (unused). */
5878 static struct spelling *spelling_base; /* Spelling stack base. */
5879 static int spelling_size; /* Size of the spelling stack. */
5881 /* Macros to save and restore the spelling stack around push_... functions.
5882 Alternative to SAVE_SPELLING_STACK. */
5884 #define SPELLING_DEPTH() (spelling - spelling_base)
5885 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5887 /* Push an element on the spelling stack with type KIND and assign VALUE
5888 to MEMBER. */
5890 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5892 int depth = SPELLING_DEPTH (); \
5894 if (depth >= spelling_size) \
5896 spelling_size += 10; \
5897 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5898 spelling_size); \
5899 RESTORE_SPELLING_DEPTH (depth); \
5902 spelling->kind = (KIND); \
5903 spelling->MEMBER = (VALUE); \
5904 spelling++; \
5907 /* Push STRING on the stack. Printed literally. */
5909 static void
5910 push_string (const char *string)
5912 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5915 /* Push a member name on the stack. Printed as '.' STRING. */
5917 static void
5918 push_member_name (tree decl)
5920 const char *const string
5921 = (DECL_NAME (decl)
5922 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5923 : _("<anonymous>"));
5924 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5927 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5929 static void
5930 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5932 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5935 /* Compute the maximum size in bytes of the printed spelling. */
5937 static int
5938 spelling_length (void)
5940 int size = 0;
5941 struct spelling *p;
5943 for (p = spelling_base; p < spelling; p++)
5945 if (p->kind == SPELLING_BOUNDS)
5946 size += 25;
5947 else
5948 size += strlen (p->u.s) + 1;
5951 return size;
5954 /* Print the spelling to BUFFER and return it. */
5956 static char *
5957 print_spelling (char *buffer)
5959 char *d = buffer;
5960 struct spelling *p;
5962 for (p = spelling_base; p < spelling; p++)
5963 if (p->kind == SPELLING_BOUNDS)
5965 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5966 d += strlen (d);
5968 else
5970 const char *s;
5971 if (p->kind == SPELLING_MEMBER)
5972 *d++ = '.';
5973 for (s = p->u.s; (*d = *s++); d++)
5976 *d++ = '\0';
5977 return buffer;
5980 /* Issue an error message for a bad initializer component.
5981 GMSGID identifies the message.
5982 The component name is taken from the spelling stack. */
5984 void
5985 error_init (const char *gmsgid)
5987 char *ofwhat;
5989 /* The gmsgid may be a format string with %< and %>. */
5990 error (gmsgid);
5991 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5992 if (*ofwhat)
5993 error ("(near initialization for %qs)", ofwhat);
5996 /* Issue a pedantic warning for a bad initializer component. OPT is
5997 the option OPT_* (from options.h) controlling this warning or 0 if
5998 it is unconditionally given. GMSGID identifies the message. The
5999 component name is taken from the spelling stack. */
6001 void
6002 pedwarn_init (location_t location, int opt, const char *gmsgid)
6004 char *ofwhat;
6006 /* The gmsgid may be a format string with %< and %>. */
6007 pedwarn (location, opt, gmsgid);
6008 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6009 if (*ofwhat)
6010 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6013 /* Issue a warning for a bad initializer component.
6015 OPT is the OPT_W* value corresponding to the warning option that
6016 controls this warning. GMSGID identifies the message. The
6017 component name is taken from the spelling stack. */
6019 static void
6020 warning_init (int opt, const char *gmsgid)
6022 char *ofwhat;
6024 /* The gmsgid may be a format string with %< and %>. */
6025 warning (opt, gmsgid);
6026 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6027 if (*ofwhat)
6028 warning (opt, "(near initialization for %qs)", ofwhat);
6031 /* If TYPE is an array type and EXPR is a parenthesized string
6032 constant, warn if pedantic that EXPR is being used to initialize an
6033 object of type TYPE. */
6035 void
6036 maybe_warn_string_init (tree type, struct c_expr expr)
6038 if (pedantic
6039 && TREE_CODE (type) == ARRAY_TYPE
6040 && TREE_CODE (expr.value) == STRING_CST
6041 && expr.original_code != STRING_CST)
6042 pedwarn_init (input_location, OPT_pedantic,
6043 "array initialized from parenthesized string constant");
6046 /* Digest the parser output INIT as an initializer for type TYPE.
6047 Return a C expression of type TYPE to represent the initial value.
6049 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6051 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6053 If INIT is a string constant, STRICT_STRING is true if it is
6054 unparenthesized or we should not warn here for it being parenthesized.
6055 For other types of INIT, STRICT_STRING is not used.
6057 INIT_LOC is the location of the INIT.
6059 REQUIRE_CONSTANT requests an error if non-constant initializers or
6060 elements are seen. */
6062 static tree
6063 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6064 bool null_pointer_constant, bool strict_string,
6065 int require_constant)
6067 enum tree_code code = TREE_CODE (type);
6068 tree inside_init = init;
6069 tree semantic_type = NULL_TREE;
6070 bool maybe_const = true;
6072 if (type == error_mark_node
6073 || !init
6074 || init == error_mark_node
6075 || TREE_TYPE (init) == error_mark_node)
6076 return error_mark_node;
6078 STRIP_TYPE_NOPS (inside_init);
6080 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6082 semantic_type = TREE_TYPE (inside_init);
6083 inside_init = TREE_OPERAND (inside_init, 0);
6085 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6086 inside_init = decl_constant_value_for_optimization (inside_init);
6088 /* Initialization of an array of chars from a string constant
6089 optionally enclosed in braces. */
6091 if (code == ARRAY_TYPE && inside_init
6092 && TREE_CODE (inside_init) == STRING_CST)
6094 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6095 /* Note that an array could be both an array of character type
6096 and an array of wchar_t if wchar_t is signed char or unsigned
6097 char. */
6098 bool char_array = (typ1 == char_type_node
6099 || typ1 == signed_char_type_node
6100 || typ1 == unsigned_char_type_node);
6101 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6102 bool char16_array = !!comptypes (typ1, char16_type_node);
6103 bool char32_array = !!comptypes (typ1, char32_type_node);
6105 if (char_array || wchar_array || char16_array || char32_array)
6107 struct c_expr expr;
6108 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6109 expr.value = inside_init;
6110 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6111 expr.original_type = NULL;
6112 maybe_warn_string_init (type, expr);
6114 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6115 pedwarn_init (init_loc, OPT_pedantic,
6116 "initialization of a flexible array member");
6118 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6119 TYPE_MAIN_VARIANT (type)))
6120 return inside_init;
6122 if (char_array)
6124 if (typ2 != char_type_node)
6126 error_init ("char-array initialized from wide string");
6127 return error_mark_node;
6130 else
6132 if (typ2 == char_type_node)
6134 error_init ("wide character array initialized from non-wide "
6135 "string");
6136 return error_mark_node;
6138 else if (!comptypes(typ1, typ2))
6140 error_init ("wide character array initialized from "
6141 "incompatible wide string");
6142 return error_mark_node;
6146 TREE_TYPE (inside_init) = type;
6147 if (TYPE_DOMAIN (type) != 0
6148 && TYPE_SIZE (type) != 0
6149 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6151 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6153 /* Subtract the size of a single (possibly wide) character
6154 because it's ok to ignore the terminating null char
6155 that is counted in the length of the constant. */
6156 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6157 (len
6158 - (TYPE_PRECISION (typ1)
6159 / BITS_PER_UNIT))))
6160 pedwarn_init (init_loc, 0,
6161 ("initializer-string for array of chars "
6162 "is too long"));
6163 else if (warn_cxx_compat
6164 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6165 warning_at (init_loc, OPT_Wc___compat,
6166 ("initializer-string for array chars "
6167 "is too long for C++"));
6170 return inside_init;
6172 else if (INTEGRAL_TYPE_P (typ1))
6174 error_init ("array of inappropriate type initialized "
6175 "from string constant");
6176 return error_mark_node;
6180 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6181 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6182 below and handle as a constructor. */
6183 if (code == VECTOR_TYPE
6184 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6185 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6186 && TREE_CONSTANT (inside_init))
6188 if (TREE_CODE (inside_init) == VECTOR_CST
6189 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6190 TYPE_MAIN_VARIANT (type)))
6191 return inside_init;
6193 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6195 unsigned HOST_WIDE_INT ix;
6196 tree value;
6197 bool constant_p = true;
6199 /* Iterate through elements and check if all constructor
6200 elements are *_CSTs. */
6201 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6202 if (!CONSTANT_CLASS_P (value))
6204 constant_p = false;
6205 break;
6208 if (constant_p)
6209 return build_vector_from_ctor (type,
6210 CONSTRUCTOR_ELTS (inside_init));
6214 if (warn_sequence_point)
6215 verify_sequence_points (inside_init);
6217 /* Any type can be initialized
6218 from an expression of the same type, optionally with braces. */
6220 if (inside_init && TREE_TYPE (inside_init) != 0
6221 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6222 TYPE_MAIN_VARIANT (type))
6223 || (code == ARRAY_TYPE
6224 && comptypes (TREE_TYPE (inside_init), type))
6225 || (code == VECTOR_TYPE
6226 && comptypes (TREE_TYPE (inside_init), type))
6227 || (code == POINTER_TYPE
6228 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6229 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6230 TREE_TYPE (type)))))
6232 if (code == POINTER_TYPE)
6234 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6236 if (TREE_CODE (inside_init) == STRING_CST
6237 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6238 inside_init = array_to_pointer_conversion
6239 (init_loc, inside_init);
6240 else
6242 error_init ("invalid use of non-lvalue array");
6243 return error_mark_node;
6248 if (code == VECTOR_TYPE)
6249 /* Although the types are compatible, we may require a
6250 conversion. */
6251 inside_init = convert (type, inside_init);
6253 if (require_constant
6254 && (code == VECTOR_TYPE || !flag_isoc99)
6255 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6257 /* As an extension, allow initializing objects with static storage
6258 duration with compound literals (which are then treated just as
6259 the brace enclosed list they contain). Also allow this for
6260 vectors, as we can only assign them with compound literals. */
6261 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6262 inside_init = DECL_INITIAL (decl);
6265 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6266 && TREE_CODE (inside_init) != CONSTRUCTOR)
6268 error_init ("array initialized from non-constant array expression");
6269 return error_mark_node;
6272 /* Compound expressions can only occur here if -pedantic or
6273 -pedantic-errors is specified. In the later case, we always want
6274 an error. In the former case, we simply want a warning. */
6275 if (require_constant && pedantic
6276 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6278 inside_init
6279 = valid_compound_expr_initializer (inside_init,
6280 TREE_TYPE (inside_init));
6281 if (inside_init == error_mark_node)
6282 error_init ("initializer element is not constant");
6283 else
6284 pedwarn_init (init_loc, OPT_pedantic,
6285 "initializer element is not constant");
6286 if (flag_pedantic_errors)
6287 inside_init = error_mark_node;
6289 else if (require_constant
6290 && !initializer_constant_valid_p (inside_init,
6291 TREE_TYPE (inside_init)))
6293 error_init ("initializer element is not constant");
6294 inside_init = error_mark_node;
6296 else if (require_constant && !maybe_const)
6297 pedwarn_init (init_loc, 0,
6298 "initializer element is not a constant expression");
6300 /* Added to enable additional -Wmissing-format-attribute warnings. */
6301 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6302 inside_init = convert_for_assignment (init_loc, type, inside_init,
6303 origtype,
6304 ic_init, null_pointer_constant,
6305 NULL_TREE, NULL_TREE, 0);
6306 return inside_init;
6309 /* Handle scalar types, including conversions. */
6311 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6312 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6313 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6315 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6316 && (TREE_CODE (init) == STRING_CST
6317 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6318 inside_init = init = array_to_pointer_conversion (init_loc, init);
6319 if (semantic_type)
6320 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6321 inside_init);
6322 inside_init
6323 = convert_for_assignment (init_loc, type, inside_init, origtype,
6324 ic_init, null_pointer_constant,
6325 NULL_TREE, NULL_TREE, 0);
6327 /* Check to see if we have already given an error message. */
6328 if (inside_init == error_mark_node)
6330 else if (require_constant && !TREE_CONSTANT (inside_init))
6332 error_init ("initializer element is not constant");
6333 inside_init = error_mark_node;
6335 else if (require_constant
6336 && !initializer_constant_valid_p (inside_init,
6337 TREE_TYPE (inside_init)))
6339 error_init ("initializer element is not computable at load time");
6340 inside_init = error_mark_node;
6342 else if (require_constant && !maybe_const)
6343 pedwarn_init (init_loc, 0,
6344 "initializer element is not a constant expression");
6346 return inside_init;
6349 /* Come here only for records and arrays. */
6351 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6353 error_init ("variable-sized object may not be initialized");
6354 return error_mark_node;
6357 error_init ("invalid initializer");
6358 return error_mark_node;
6361 /* Handle initializers that use braces. */
6363 /* Type of object we are accumulating a constructor for.
6364 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6365 static tree constructor_type;
6367 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6368 left to fill. */
6369 static tree constructor_fields;
6371 /* For an ARRAY_TYPE, this is the specified index
6372 at which to store the next element we get. */
6373 static tree constructor_index;
6375 /* For an ARRAY_TYPE, this is the maximum index. */
6376 static tree constructor_max_index;
6378 /* For a RECORD_TYPE, this is the first field not yet written out. */
6379 static tree constructor_unfilled_fields;
6381 /* For an ARRAY_TYPE, this is the index of the first element
6382 not yet written out. */
6383 static tree constructor_unfilled_index;
6385 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6386 This is so we can generate gaps between fields, when appropriate. */
6387 static tree constructor_bit_index;
6389 /* If we are saving up the elements rather than allocating them,
6390 this is the list of elements so far (in reverse order,
6391 most recent first). */
6392 static VEC(constructor_elt,gc) *constructor_elements;
6394 /* 1 if constructor should be incrementally stored into a constructor chain,
6395 0 if all the elements should be kept in AVL tree. */
6396 static int constructor_incremental;
6398 /* 1 if so far this constructor's elements are all compile-time constants. */
6399 static int constructor_constant;
6401 /* 1 if so far this constructor's elements are all valid address constants. */
6402 static int constructor_simple;
6404 /* 1 if this constructor has an element that cannot be part of a
6405 constant expression. */
6406 static int constructor_nonconst;
6408 /* 1 if this constructor is erroneous so far. */
6409 static int constructor_erroneous;
6411 /* Structure for managing pending initializer elements, organized as an
6412 AVL tree. */
6414 struct init_node
6416 struct init_node *left, *right;
6417 struct init_node *parent;
6418 int balance;
6419 tree purpose;
6420 tree value;
6421 tree origtype;
6424 /* Tree of pending elements at this constructor level.
6425 These are elements encountered out of order
6426 which belong at places we haven't reached yet in actually
6427 writing the output.
6428 Will never hold tree nodes across GC runs. */
6429 static struct init_node *constructor_pending_elts;
6431 /* The SPELLING_DEPTH of this constructor. */
6432 static int constructor_depth;
6434 /* DECL node for which an initializer is being read.
6435 0 means we are reading a constructor expression
6436 such as (struct foo) {...}. */
6437 static tree constructor_decl;
6439 /* Nonzero if this is an initializer for a top-level decl. */
6440 static int constructor_top_level;
6442 /* Nonzero if there were any member designators in this initializer. */
6443 static int constructor_designated;
6445 /* Nesting depth of designator list. */
6446 static int designator_depth;
6448 /* Nonzero if there were diagnosed errors in this designator list. */
6449 static int designator_erroneous;
6452 /* This stack has a level for each implicit or explicit level of
6453 structuring in the initializer, including the outermost one. It
6454 saves the values of most of the variables above. */
6456 struct constructor_range_stack;
6458 struct constructor_stack
6460 struct constructor_stack *next;
6461 tree type;
6462 tree fields;
6463 tree index;
6464 tree max_index;
6465 tree unfilled_index;
6466 tree unfilled_fields;
6467 tree bit_index;
6468 VEC(constructor_elt,gc) *elements;
6469 struct init_node *pending_elts;
6470 int offset;
6471 int depth;
6472 /* If value nonzero, this value should replace the entire
6473 constructor at this level. */
6474 struct c_expr replacement_value;
6475 struct constructor_range_stack *range_stack;
6476 char constant;
6477 char simple;
6478 char nonconst;
6479 char implicit;
6480 char erroneous;
6481 char outer;
6482 char incremental;
6483 char designated;
6486 static struct constructor_stack *constructor_stack;
6488 /* This stack represents designators from some range designator up to
6489 the last designator in the list. */
6491 struct constructor_range_stack
6493 struct constructor_range_stack *next, *prev;
6494 struct constructor_stack *stack;
6495 tree range_start;
6496 tree index;
6497 tree range_end;
6498 tree fields;
6501 static struct constructor_range_stack *constructor_range_stack;
6503 /* This stack records separate initializers that are nested.
6504 Nested initializers can't happen in ANSI C, but GNU C allows them
6505 in cases like { ... (struct foo) { ... } ... }. */
6507 struct initializer_stack
6509 struct initializer_stack *next;
6510 tree decl;
6511 struct constructor_stack *constructor_stack;
6512 struct constructor_range_stack *constructor_range_stack;
6513 VEC(constructor_elt,gc) *elements;
6514 struct spelling *spelling;
6515 struct spelling *spelling_base;
6516 int spelling_size;
6517 char top_level;
6518 char require_constant_value;
6519 char require_constant_elements;
6522 static struct initializer_stack *initializer_stack;
6524 /* Prepare to parse and output the initializer for variable DECL. */
6526 void
6527 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6529 const char *locus;
6530 struct initializer_stack *p = XNEW (struct initializer_stack);
6532 p->decl = constructor_decl;
6533 p->require_constant_value = require_constant_value;
6534 p->require_constant_elements = require_constant_elements;
6535 p->constructor_stack = constructor_stack;
6536 p->constructor_range_stack = constructor_range_stack;
6537 p->elements = constructor_elements;
6538 p->spelling = spelling;
6539 p->spelling_base = spelling_base;
6540 p->spelling_size = spelling_size;
6541 p->top_level = constructor_top_level;
6542 p->next = initializer_stack;
6543 initializer_stack = p;
6545 constructor_decl = decl;
6546 constructor_designated = 0;
6547 constructor_top_level = top_level;
6549 if (decl != 0 && decl != error_mark_node)
6551 require_constant_value = TREE_STATIC (decl);
6552 require_constant_elements
6553 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6554 /* For a scalar, you can always use any value to initialize,
6555 even within braces. */
6556 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6557 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6558 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6559 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6560 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6562 else
6564 require_constant_value = 0;
6565 require_constant_elements = 0;
6566 locus = _("(anonymous)");
6569 constructor_stack = 0;
6570 constructor_range_stack = 0;
6572 missing_braces_mentioned = 0;
6574 spelling_base = 0;
6575 spelling_size = 0;
6576 RESTORE_SPELLING_DEPTH (0);
6578 if (locus)
6579 push_string (locus);
6582 void
6583 finish_init (void)
6585 struct initializer_stack *p = initializer_stack;
6587 /* Free the whole constructor stack of this initializer. */
6588 while (constructor_stack)
6590 struct constructor_stack *q = constructor_stack;
6591 constructor_stack = q->next;
6592 free (q);
6595 gcc_assert (!constructor_range_stack);
6597 /* Pop back to the data of the outer initializer (if any). */
6598 free (spelling_base);
6600 constructor_decl = p->decl;
6601 require_constant_value = p->require_constant_value;
6602 require_constant_elements = p->require_constant_elements;
6603 constructor_stack = p->constructor_stack;
6604 constructor_range_stack = p->constructor_range_stack;
6605 constructor_elements = p->elements;
6606 spelling = p->spelling;
6607 spelling_base = p->spelling_base;
6608 spelling_size = p->spelling_size;
6609 constructor_top_level = p->top_level;
6610 initializer_stack = p->next;
6611 free (p);
6614 /* Call here when we see the initializer is surrounded by braces.
6615 This is instead of a call to push_init_level;
6616 it is matched by a call to pop_init_level.
6618 TYPE is the type to initialize, for a constructor expression.
6619 For an initializer for a decl, TYPE is zero. */
6621 void
6622 really_start_incremental_init (tree type)
6624 struct constructor_stack *p = XNEW (struct constructor_stack);
6626 if (type == 0)
6627 type = TREE_TYPE (constructor_decl);
6629 if (TREE_CODE (type) == VECTOR_TYPE
6630 && TYPE_VECTOR_OPAQUE (type))
6631 error ("opaque vector types cannot be initialized");
6633 p->type = constructor_type;
6634 p->fields = constructor_fields;
6635 p->index = constructor_index;
6636 p->max_index = constructor_max_index;
6637 p->unfilled_index = constructor_unfilled_index;
6638 p->unfilled_fields = constructor_unfilled_fields;
6639 p->bit_index = constructor_bit_index;
6640 p->elements = constructor_elements;
6641 p->constant = constructor_constant;
6642 p->simple = constructor_simple;
6643 p->nonconst = constructor_nonconst;
6644 p->erroneous = constructor_erroneous;
6645 p->pending_elts = constructor_pending_elts;
6646 p->depth = constructor_depth;
6647 p->replacement_value.value = 0;
6648 p->replacement_value.original_code = ERROR_MARK;
6649 p->replacement_value.original_type = NULL;
6650 p->implicit = 0;
6651 p->range_stack = 0;
6652 p->outer = 0;
6653 p->incremental = constructor_incremental;
6654 p->designated = constructor_designated;
6655 p->next = 0;
6656 constructor_stack = p;
6658 constructor_constant = 1;
6659 constructor_simple = 1;
6660 constructor_nonconst = 0;
6661 constructor_depth = SPELLING_DEPTH ();
6662 constructor_elements = 0;
6663 constructor_pending_elts = 0;
6664 constructor_type = type;
6665 constructor_incremental = 1;
6666 constructor_designated = 0;
6667 designator_depth = 0;
6668 designator_erroneous = 0;
6670 if (TREE_CODE (constructor_type) == RECORD_TYPE
6671 || TREE_CODE (constructor_type) == UNION_TYPE)
6673 constructor_fields = TYPE_FIELDS (constructor_type);
6674 /* Skip any nameless bit fields at the beginning. */
6675 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6676 && DECL_NAME (constructor_fields) == 0)
6677 constructor_fields = DECL_CHAIN (constructor_fields);
6679 constructor_unfilled_fields = constructor_fields;
6680 constructor_bit_index = bitsize_zero_node;
6682 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6684 if (TYPE_DOMAIN (constructor_type))
6686 constructor_max_index
6687 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6689 /* Detect non-empty initializations of zero-length arrays. */
6690 if (constructor_max_index == NULL_TREE
6691 && TYPE_SIZE (constructor_type))
6692 constructor_max_index = integer_minus_one_node;
6694 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6695 to initialize VLAs will cause a proper error; avoid tree
6696 checking errors as well by setting a safe value. */
6697 if (constructor_max_index
6698 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6699 constructor_max_index = integer_minus_one_node;
6701 constructor_index
6702 = convert (bitsizetype,
6703 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6705 else
6707 constructor_index = bitsize_zero_node;
6708 constructor_max_index = NULL_TREE;
6711 constructor_unfilled_index = constructor_index;
6713 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6715 /* Vectors are like simple fixed-size arrays. */
6716 constructor_max_index =
6717 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6718 constructor_index = bitsize_zero_node;
6719 constructor_unfilled_index = constructor_index;
6721 else
6723 /* Handle the case of int x = {5}; */
6724 constructor_fields = constructor_type;
6725 constructor_unfilled_fields = constructor_type;
6729 /* Push down into a subobject, for initialization.
6730 If this is for an explicit set of braces, IMPLICIT is 0.
6731 If it is because the next element belongs at a lower level,
6732 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6734 void
6735 push_init_level (int implicit, struct obstack * braced_init_obstack)
6737 struct constructor_stack *p;
6738 tree value = NULL_TREE;
6740 /* If we've exhausted any levels that didn't have braces,
6741 pop them now. If implicit == 1, this will have been done in
6742 process_init_element; do not repeat it here because in the case
6743 of excess initializers for an empty aggregate this leads to an
6744 infinite cycle of popping a level and immediately recreating
6745 it. */
6746 if (implicit != 1)
6748 while (constructor_stack->implicit)
6750 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6751 || TREE_CODE (constructor_type) == UNION_TYPE)
6752 && constructor_fields == 0)
6753 process_init_element (pop_init_level (1, braced_init_obstack),
6754 true, braced_init_obstack);
6755 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6756 && constructor_max_index
6757 && tree_int_cst_lt (constructor_max_index,
6758 constructor_index))
6759 process_init_element (pop_init_level (1, braced_init_obstack),
6760 true, braced_init_obstack);
6761 else
6762 break;
6766 /* Unless this is an explicit brace, we need to preserve previous
6767 content if any. */
6768 if (implicit)
6770 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6771 || TREE_CODE (constructor_type) == UNION_TYPE)
6772 && constructor_fields)
6773 value = find_init_member (constructor_fields, braced_init_obstack);
6774 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6775 value = find_init_member (constructor_index, braced_init_obstack);
6778 p = XNEW (struct constructor_stack);
6779 p->type = constructor_type;
6780 p->fields = constructor_fields;
6781 p->index = constructor_index;
6782 p->max_index = constructor_max_index;
6783 p->unfilled_index = constructor_unfilled_index;
6784 p->unfilled_fields = constructor_unfilled_fields;
6785 p->bit_index = constructor_bit_index;
6786 p->elements = constructor_elements;
6787 p->constant = constructor_constant;
6788 p->simple = constructor_simple;
6789 p->nonconst = constructor_nonconst;
6790 p->erroneous = constructor_erroneous;
6791 p->pending_elts = constructor_pending_elts;
6792 p->depth = constructor_depth;
6793 p->replacement_value.value = 0;
6794 p->replacement_value.original_code = ERROR_MARK;
6795 p->replacement_value.original_type = NULL;
6796 p->implicit = implicit;
6797 p->outer = 0;
6798 p->incremental = constructor_incremental;
6799 p->designated = constructor_designated;
6800 p->next = constructor_stack;
6801 p->range_stack = 0;
6802 constructor_stack = p;
6804 constructor_constant = 1;
6805 constructor_simple = 1;
6806 constructor_nonconst = 0;
6807 constructor_depth = SPELLING_DEPTH ();
6808 constructor_elements = 0;
6809 constructor_incremental = 1;
6810 constructor_designated = 0;
6811 constructor_pending_elts = 0;
6812 if (!implicit)
6814 p->range_stack = constructor_range_stack;
6815 constructor_range_stack = 0;
6816 designator_depth = 0;
6817 designator_erroneous = 0;
6820 /* Don't die if an entire brace-pair level is superfluous
6821 in the containing level. */
6822 if (constructor_type == 0)
6824 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6825 || TREE_CODE (constructor_type) == UNION_TYPE)
6827 /* Don't die if there are extra init elts at the end. */
6828 if (constructor_fields == 0)
6829 constructor_type = 0;
6830 else
6832 constructor_type = TREE_TYPE (constructor_fields);
6833 push_member_name (constructor_fields);
6834 constructor_depth++;
6837 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6839 constructor_type = TREE_TYPE (constructor_type);
6840 push_array_bounds (tree_low_cst (constructor_index, 1));
6841 constructor_depth++;
6844 if (constructor_type == 0)
6846 error_init ("extra brace group at end of initializer");
6847 constructor_fields = 0;
6848 constructor_unfilled_fields = 0;
6849 return;
6852 if (value && TREE_CODE (value) == CONSTRUCTOR)
6854 constructor_constant = TREE_CONSTANT (value);
6855 constructor_simple = TREE_STATIC (value);
6856 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6857 constructor_elements = CONSTRUCTOR_ELTS (value);
6858 if (!VEC_empty (constructor_elt, constructor_elements)
6859 && (TREE_CODE (constructor_type) == RECORD_TYPE
6860 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6861 set_nonincremental_init (braced_init_obstack);
6864 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6866 missing_braces_mentioned = 1;
6867 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6870 if (TREE_CODE (constructor_type) == RECORD_TYPE
6871 || TREE_CODE (constructor_type) == UNION_TYPE)
6873 constructor_fields = TYPE_FIELDS (constructor_type);
6874 /* Skip any nameless bit fields at the beginning. */
6875 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6876 && DECL_NAME (constructor_fields) == 0)
6877 constructor_fields = DECL_CHAIN (constructor_fields);
6879 constructor_unfilled_fields = constructor_fields;
6880 constructor_bit_index = bitsize_zero_node;
6882 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6884 /* Vectors are like simple fixed-size arrays. */
6885 constructor_max_index =
6886 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6887 constructor_index = convert (bitsizetype, integer_zero_node);
6888 constructor_unfilled_index = constructor_index;
6890 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6892 if (TYPE_DOMAIN (constructor_type))
6894 constructor_max_index
6895 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6897 /* Detect non-empty initializations of zero-length arrays. */
6898 if (constructor_max_index == NULL_TREE
6899 && TYPE_SIZE (constructor_type))
6900 constructor_max_index = integer_minus_one_node;
6902 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6903 to initialize VLAs will cause a proper error; avoid tree
6904 checking errors as well by setting a safe value. */
6905 if (constructor_max_index
6906 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6907 constructor_max_index = integer_minus_one_node;
6909 constructor_index
6910 = convert (bitsizetype,
6911 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6913 else
6914 constructor_index = bitsize_zero_node;
6916 constructor_unfilled_index = constructor_index;
6917 if (value && TREE_CODE (value) == STRING_CST)
6919 /* We need to split the char/wchar array into individual
6920 characters, so that we don't have to special case it
6921 everywhere. */
6922 set_nonincremental_init_from_string (value, braced_init_obstack);
6925 else
6927 if (constructor_type != error_mark_node)
6928 warning_init (0, "braces around scalar initializer");
6929 constructor_fields = constructor_type;
6930 constructor_unfilled_fields = constructor_type;
6934 /* At the end of an implicit or explicit brace level,
6935 finish up that level of constructor. If a single expression
6936 with redundant braces initialized that level, return the
6937 c_expr structure for that expression. Otherwise, the original_code
6938 element is set to ERROR_MARK.
6939 If we were outputting the elements as they are read, return 0 as the value
6940 from inner levels (process_init_element ignores that),
6941 but return error_mark_node as the value from the outermost level
6942 (that's what we want to put in DECL_INITIAL).
6943 Otherwise, return a CONSTRUCTOR expression as the value. */
6945 struct c_expr
6946 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6948 struct constructor_stack *p;
6949 struct c_expr ret;
6950 ret.value = 0;
6951 ret.original_code = ERROR_MARK;
6952 ret.original_type = NULL;
6954 if (implicit == 0)
6956 /* When we come to an explicit close brace,
6957 pop any inner levels that didn't have explicit braces. */
6958 while (constructor_stack->implicit)
6960 process_init_element (pop_init_level (1, braced_init_obstack),
6961 true, braced_init_obstack);
6963 gcc_assert (!constructor_range_stack);
6966 /* Now output all pending elements. */
6967 constructor_incremental = 1;
6968 output_pending_init_elements (1, braced_init_obstack);
6970 p = constructor_stack;
6972 /* Error for initializing a flexible array member, or a zero-length
6973 array member in an inappropriate context. */
6974 if (constructor_type && constructor_fields
6975 && TREE_CODE (constructor_type) == ARRAY_TYPE
6976 && TYPE_DOMAIN (constructor_type)
6977 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6979 /* Silently discard empty initializations. The parser will
6980 already have pedwarned for empty brackets. */
6981 if (integer_zerop (constructor_unfilled_index))
6982 constructor_type = NULL_TREE;
6983 else
6985 gcc_assert (!TYPE_SIZE (constructor_type));
6987 if (constructor_depth > 2)
6988 error_init ("initialization of flexible array member in a nested context");
6989 else
6990 pedwarn_init (input_location, OPT_pedantic,
6991 "initialization of a flexible array member");
6993 /* We have already issued an error message for the existence
6994 of a flexible array member not at the end of the structure.
6995 Discard the initializer so that we do not die later. */
6996 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6997 constructor_type = NULL_TREE;
7001 /* Warn when some struct elements are implicitly initialized to zero. */
7002 if (warn_missing_field_initializers
7003 && constructor_type
7004 && TREE_CODE (constructor_type) == RECORD_TYPE
7005 && constructor_unfilled_fields)
7007 /* Do not warn for flexible array members or zero-length arrays. */
7008 while (constructor_unfilled_fields
7009 && (!DECL_SIZE (constructor_unfilled_fields)
7010 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7011 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7013 /* Do not warn if this level of the initializer uses member
7014 designators; it is likely to be deliberate. */
7015 if (constructor_unfilled_fields && !constructor_designated)
7017 push_member_name (constructor_unfilled_fields);
7018 warning_init (OPT_Wmissing_field_initializers,
7019 "missing initializer");
7020 RESTORE_SPELLING_DEPTH (constructor_depth);
7024 /* Pad out the end of the structure. */
7025 if (p->replacement_value.value)
7026 /* If this closes a superfluous brace pair,
7027 just pass out the element between them. */
7028 ret = p->replacement_value;
7029 else if (constructor_type == 0)
7031 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7032 && TREE_CODE (constructor_type) != UNION_TYPE
7033 && TREE_CODE (constructor_type) != ARRAY_TYPE
7034 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7036 /* A nonincremental scalar initializer--just return
7037 the element, after verifying there is just one. */
7038 if (VEC_empty (constructor_elt,constructor_elements))
7040 if (!constructor_erroneous)
7041 error_init ("empty scalar initializer");
7042 ret.value = error_mark_node;
7044 else if (VEC_length (constructor_elt,constructor_elements) != 1)
7046 error_init ("extra elements in scalar initializer");
7047 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7049 else
7050 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7052 else
7054 if (constructor_erroneous)
7055 ret.value = error_mark_node;
7056 else
7058 ret.value = build_constructor (constructor_type,
7059 constructor_elements);
7060 if (constructor_constant)
7061 TREE_CONSTANT (ret.value) = 1;
7062 if (constructor_constant && constructor_simple)
7063 TREE_STATIC (ret.value) = 1;
7064 if (constructor_nonconst)
7065 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7069 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7071 if (constructor_nonconst)
7072 ret.original_code = C_MAYBE_CONST_EXPR;
7073 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7074 ret.original_code = ERROR_MARK;
7077 constructor_type = p->type;
7078 constructor_fields = p->fields;
7079 constructor_index = p->index;
7080 constructor_max_index = p->max_index;
7081 constructor_unfilled_index = p->unfilled_index;
7082 constructor_unfilled_fields = p->unfilled_fields;
7083 constructor_bit_index = p->bit_index;
7084 constructor_elements = p->elements;
7085 constructor_constant = p->constant;
7086 constructor_simple = p->simple;
7087 constructor_nonconst = p->nonconst;
7088 constructor_erroneous = p->erroneous;
7089 constructor_incremental = p->incremental;
7090 constructor_designated = p->designated;
7091 constructor_pending_elts = p->pending_elts;
7092 constructor_depth = p->depth;
7093 if (!p->implicit)
7094 constructor_range_stack = p->range_stack;
7095 RESTORE_SPELLING_DEPTH (constructor_depth);
7097 constructor_stack = p->next;
7098 free (p);
7100 if (ret.value == 0 && constructor_stack == 0)
7101 ret.value = error_mark_node;
7102 return ret;
7105 /* Common handling for both array range and field name designators.
7106 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7108 static int
7109 set_designator (int array, struct obstack * braced_init_obstack)
7111 tree subtype;
7112 enum tree_code subcode;
7114 /* Don't die if an entire brace-pair level is superfluous
7115 in the containing level. */
7116 if (constructor_type == 0)
7117 return 1;
7119 /* If there were errors in this designator list already, bail out
7120 silently. */
7121 if (designator_erroneous)
7122 return 1;
7124 if (!designator_depth)
7126 gcc_assert (!constructor_range_stack);
7128 /* Designator list starts at the level of closest explicit
7129 braces. */
7130 while (constructor_stack->implicit)
7132 process_init_element (pop_init_level (1, braced_init_obstack),
7133 true, braced_init_obstack);
7135 constructor_designated = 1;
7136 return 0;
7139 switch (TREE_CODE (constructor_type))
7141 case RECORD_TYPE:
7142 case UNION_TYPE:
7143 subtype = TREE_TYPE (constructor_fields);
7144 if (subtype != error_mark_node)
7145 subtype = TYPE_MAIN_VARIANT (subtype);
7146 break;
7147 case ARRAY_TYPE:
7148 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7149 break;
7150 default:
7151 gcc_unreachable ();
7154 subcode = TREE_CODE (subtype);
7155 if (array && subcode != ARRAY_TYPE)
7157 error_init ("array index in non-array initializer");
7158 return 1;
7160 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7162 error_init ("field name not in record or union initializer");
7163 return 1;
7166 constructor_designated = 1;
7167 push_init_level (2, braced_init_obstack);
7168 return 0;
7171 /* If there are range designators in designator list, push a new designator
7172 to constructor_range_stack. RANGE_END is end of such stack range or
7173 NULL_TREE if there is no range designator at this level. */
7175 static void
7176 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7178 struct constructor_range_stack *p;
7180 p = (struct constructor_range_stack *)
7181 obstack_alloc (braced_init_obstack,
7182 sizeof (struct constructor_range_stack));
7183 p->prev = constructor_range_stack;
7184 p->next = 0;
7185 p->fields = constructor_fields;
7186 p->range_start = constructor_index;
7187 p->index = constructor_index;
7188 p->stack = constructor_stack;
7189 p->range_end = range_end;
7190 if (constructor_range_stack)
7191 constructor_range_stack->next = p;
7192 constructor_range_stack = p;
7195 /* Within an array initializer, specify the next index to be initialized.
7196 FIRST is that index. If LAST is nonzero, then initialize a range
7197 of indices, running from FIRST through LAST. */
7199 void
7200 set_init_index (tree first, tree last,
7201 struct obstack * braced_init_obstack)
7203 if (set_designator (1, braced_init_obstack))
7204 return;
7206 designator_erroneous = 1;
7208 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7209 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7211 error_init ("array index in initializer not of integer type");
7212 return;
7215 if (TREE_CODE (first) != INTEGER_CST)
7217 first = c_fully_fold (first, false, NULL);
7218 if (TREE_CODE (first) == INTEGER_CST)
7219 pedwarn_init (input_location, OPT_pedantic,
7220 "array index in initializer is not "
7221 "an integer constant expression");
7224 if (last && TREE_CODE (last) != INTEGER_CST)
7226 last = c_fully_fold (last, false, NULL);
7227 if (TREE_CODE (last) == INTEGER_CST)
7228 pedwarn_init (input_location, OPT_pedantic,
7229 "array index in initializer is not "
7230 "an integer constant expression");
7233 if (TREE_CODE (first) != INTEGER_CST)
7234 error_init ("nonconstant array index in initializer");
7235 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7236 error_init ("nonconstant array index in initializer");
7237 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7238 error_init ("array index in non-array initializer");
7239 else if (tree_int_cst_sgn (first) == -1)
7240 error_init ("array index in initializer exceeds array bounds");
7241 else if (constructor_max_index
7242 && tree_int_cst_lt (constructor_max_index, first))
7243 error_init ("array index in initializer exceeds array bounds");
7244 else
7246 constant_expression_warning (first);
7247 if (last)
7248 constant_expression_warning (last);
7249 constructor_index = convert (bitsizetype, first);
7251 if (last)
7253 if (tree_int_cst_equal (first, last))
7254 last = 0;
7255 else if (tree_int_cst_lt (last, first))
7257 error_init ("empty index range in initializer");
7258 last = 0;
7260 else
7262 last = convert (bitsizetype, last);
7263 if (constructor_max_index != 0
7264 && tree_int_cst_lt (constructor_max_index, last))
7266 error_init ("array index range in initializer exceeds array bounds");
7267 last = 0;
7272 designator_depth++;
7273 designator_erroneous = 0;
7274 if (constructor_range_stack || last)
7275 push_range_stack (last, braced_init_obstack);
7279 /* Within a struct initializer, specify the next field to be initialized. */
7281 void
7282 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7284 tree field;
7286 if (set_designator (0, braced_init_obstack))
7287 return;
7289 designator_erroneous = 1;
7291 if (TREE_CODE (constructor_type) != RECORD_TYPE
7292 && TREE_CODE (constructor_type) != UNION_TYPE)
7294 error_init ("field name not in record or union initializer");
7295 return;
7298 field = lookup_field (constructor_type, fieldname);
7300 if (field == 0)
7301 error ("unknown field %qE specified in initializer", fieldname);
7302 else
7305 constructor_fields = TREE_VALUE (field);
7306 designator_depth++;
7307 designator_erroneous = 0;
7308 if (constructor_range_stack)
7309 push_range_stack (NULL_TREE, braced_init_obstack);
7310 field = TREE_CHAIN (field);
7311 if (field)
7313 if (set_designator (0, braced_init_obstack))
7314 return;
7317 while (field != NULL_TREE);
7320 /* Add a new initializer to the tree of pending initializers. PURPOSE
7321 identifies the initializer, either array index or field in a structure.
7322 VALUE is the value of that index or field. If ORIGTYPE is not
7323 NULL_TREE, it is the original type of VALUE.
7325 IMPLICIT is true if value comes from pop_init_level (1),
7326 the new initializer has been merged with the existing one
7327 and thus no warnings should be emitted about overriding an
7328 existing initializer. */
7330 static void
7331 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7332 struct obstack * braced_init_obstack)
7334 struct init_node *p, **q, *r;
7336 q = &constructor_pending_elts;
7337 p = 0;
7339 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7341 while (*q != 0)
7343 p = *q;
7344 if (tree_int_cst_lt (purpose, p->purpose))
7345 q = &p->left;
7346 else if (tree_int_cst_lt (p->purpose, purpose))
7347 q = &p->right;
7348 else
7350 if (!implicit)
7352 if (TREE_SIDE_EFFECTS (p->value))
7353 warning_init (0, "initialized field with side-effects overwritten");
7354 else if (warn_override_init)
7355 warning_init (OPT_Woverride_init, "initialized field overwritten");
7357 p->value = value;
7358 p->origtype = origtype;
7359 return;
7363 else
7365 tree bitpos;
7367 bitpos = bit_position (purpose);
7368 while (*q != NULL)
7370 p = *q;
7371 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7372 q = &p->left;
7373 else if (p->purpose != purpose)
7374 q = &p->right;
7375 else
7377 if (!implicit)
7379 if (TREE_SIDE_EFFECTS (p->value))
7380 warning_init (0, "initialized field with side-effects overwritten");
7381 else if (warn_override_init)
7382 warning_init (OPT_Woverride_init, "initialized field overwritten");
7384 p->value = value;
7385 p->origtype = origtype;
7386 return;
7391 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7392 sizeof (struct init_node));
7393 r->purpose = purpose;
7394 r->value = value;
7395 r->origtype = origtype;
7397 *q = r;
7398 r->parent = p;
7399 r->left = 0;
7400 r->right = 0;
7401 r->balance = 0;
7403 while (p)
7405 struct init_node *s;
7407 if (r == p->left)
7409 if (p->balance == 0)
7410 p->balance = -1;
7411 else if (p->balance < 0)
7413 if (r->balance < 0)
7415 /* L rotation. */
7416 p->left = r->right;
7417 if (p->left)
7418 p->left->parent = p;
7419 r->right = p;
7421 p->balance = 0;
7422 r->balance = 0;
7424 s = p->parent;
7425 p->parent = r;
7426 r->parent = s;
7427 if (s)
7429 if (s->left == p)
7430 s->left = r;
7431 else
7432 s->right = r;
7434 else
7435 constructor_pending_elts = r;
7437 else
7439 /* LR rotation. */
7440 struct init_node *t = r->right;
7442 r->right = t->left;
7443 if (r->right)
7444 r->right->parent = r;
7445 t->left = r;
7447 p->left = t->right;
7448 if (p->left)
7449 p->left->parent = p;
7450 t->right = p;
7452 p->balance = t->balance < 0;
7453 r->balance = -(t->balance > 0);
7454 t->balance = 0;
7456 s = p->parent;
7457 p->parent = t;
7458 r->parent = t;
7459 t->parent = s;
7460 if (s)
7462 if (s->left == p)
7463 s->left = t;
7464 else
7465 s->right = t;
7467 else
7468 constructor_pending_elts = t;
7470 break;
7472 else
7474 /* p->balance == +1; growth of left side balances the node. */
7475 p->balance = 0;
7476 break;
7479 else /* r == p->right */
7481 if (p->balance == 0)
7482 /* Growth propagation from right side. */
7483 p->balance++;
7484 else if (p->balance > 0)
7486 if (r->balance > 0)
7488 /* R rotation. */
7489 p->right = r->left;
7490 if (p->right)
7491 p->right->parent = p;
7492 r->left = p;
7494 p->balance = 0;
7495 r->balance = 0;
7497 s = p->parent;
7498 p->parent = r;
7499 r->parent = s;
7500 if (s)
7502 if (s->left == p)
7503 s->left = r;
7504 else
7505 s->right = r;
7507 else
7508 constructor_pending_elts = r;
7510 else /* r->balance == -1 */
7512 /* RL rotation */
7513 struct init_node *t = r->left;
7515 r->left = t->right;
7516 if (r->left)
7517 r->left->parent = r;
7518 t->right = r;
7520 p->right = t->left;
7521 if (p->right)
7522 p->right->parent = p;
7523 t->left = p;
7525 r->balance = (t->balance < 0);
7526 p->balance = -(t->balance > 0);
7527 t->balance = 0;
7529 s = p->parent;
7530 p->parent = t;
7531 r->parent = t;
7532 t->parent = s;
7533 if (s)
7535 if (s->left == p)
7536 s->left = t;
7537 else
7538 s->right = t;
7540 else
7541 constructor_pending_elts = t;
7543 break;
7545 else
7547 /* p->balance == -1; growth of right side balances the node. */
7548 p->balance = 0;
7549 break;
7553 r = p;
7554 p = p->parent;
7558 /* Build AVL tree from a sorted chain. */
7560 static void
7561 set_nonincremental_init (struct obstack * braced_init_obstack)
7563 unsigned HOST_WIDE_INT ix;
7564 tree index, value;
7566 if (TREE_CODE (constructor_type) != RECORD_TYPE
7567 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7568 return;
7570 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7572 add_pending_init (index, value, NULL_TREE, false,
7573 braced_init_obstack);
7575 constructor_elements = 0;
7576 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7578 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7579 /* Skip any nameless bit fields at the beginning. */
7580 while (constructor_unfilled_fields != 0
7581 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7582 && DECL_NAME (constructor_unfilled_fields) == 0)
7583 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7586 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7588 if (TYPE_DOMAIN (constructor_type))
7589 constructor_unfilled_index
7590 = convert (bitsizetype,
7591 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7592 else
7593 constructor_unfilled_index = bitsize_zero_node;
7595 constructor_incremental = 0;
7598 /* Build AVL tree from a string constant. */
7600 static void
7601 set_nonincremental_init_from_string (tree str,
7602 struct obstack * braced_init_obstack)
7604 tree value, purpose, type;
7605 HOST_WIDE_INT val[2];
7606 const char *p, *end;
7607 int byte, wchar_bytes, charwidth, bitpos;
7609 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7611 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7612 charwidth = TYPE_PRECISION (char_type_node);
7613 type = TREE_TYPE (constructor_type);
7614 p = TREE_STRING_POINTER (str);
7615 end = p + TREE_STRING_LENGTH (str);
7617 for (purpose = bitsize_zero_node;
7618 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7619 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7621 if (wchar_bytes == 1)
7623 val[1] = (unsigned char) *p++;
7624 val[0] = 0;
7626 else
7628 val[0] = 0;
7629 val[1] = 0;
7630 for (byte = 0; byte < wchar_bytes; byte++)
7632 if (BYTES_BIG_ENDIAN)
7633 bitpos = (wchar_bytes - byte - 1) * charwidth;
7634 else
7635 bitpos = byte * charwidth;
7636 val[bitpos < HOST_BITS_PER_WIDE_INT]
7637 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7638 << (bitpos % HOST_BITS_PER_WIDE_INT);
7642 if (!TYPE_UNSIGNED (type))
7644 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7645 if (bitpos < HOST_BITS_PER_WIDE_INT)
7647 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7649 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7650 val[0] = -1;
7653 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7655 if (val[1] < 0)
7656 val[0] = -1;
7658 else if (val[0] & (((HOST_WIDE_INT) 1)
7659 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7660 val[0] |= ((HOST_WIDE_INT) -1)
7661 << (bitpos - HOST_BITS_PER_WIDE_INT);
7664 value = build_int_cst_wide (type, val[1], val[0]);
7665 add_pending_init (purpose, value, NULL_TREE, false,
7666 braced_init_obstack);
7669 constructor_incremental = 0;
7672 /* Return value of FIELD in pending initializer or zero if the field was
7673 not initialized yet. */
7675 static tree
7676 find_init_member (tree field, struct obstack * braced_init_obstack)
7678 struct init_node *p;
7680 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7682 if (constructor_incremental
7683 && tree_int_cst_lt (field, constructor_unfilled_index))
7684 set_nonincremental_init (braced_init_obstack);
7686 p = constructor_pending_elts;
7687 while (p)
7689 if (tree_int_cst_lt (field, p->purpose))
7690 p = p->left;
7691 else if (tree_int_cst_lt (p->purpose, field))
7692 p = p->right;
7693 else
7694 return p->value;
7697 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7699 tree bitpos = bit_position (field);
7701 if (constructor_incremental
7702 && (!constructor_unfilled_fields
7703 || tree_int_cst_lt (bitpos,
7704 bit_position (constructor_unfilled_fields))))
7705 set_nonincremental_init (braced_init_obstack);
7707 p = constructor_pending_elts;
7708 while (p)
7710 if (field == p->purpose)
7711 return p->value;
7712 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7713 p = p->left;
7714 else
7715 p = p->right;
7718 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7720 if (!VEC_empty (constructor_elt, constructor_elements)
7721 && (VEC_last (constructor_elt, constructor_elements)->index
7722 == field))
7723 return VEC_last (constructor_elt, constructor_elements)->value;
7725 return 0;
7728 /* "Output" the next constructor element.
7729 At top level, really output it to assembler code now.
7730 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7731 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7732 TYPE is the data type that the containing data type wants here.
7733 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7734 If VALUE is a string constant, STRICT_STRING is true if it is
7735 unparenthesized or we should not warn here for it being parenthesized.
7736 For other types of VALUE, STRICT_STRING is not used.
7738 PENDING if non-nil means output pending elements that belong
7739 right after this element. (PENDING is normally 1;
7740 it is 0 while outputting pending elements, to avoid recursion.)
7742 IMPLICIT is true if value comes from pop_init_level (1),
7743 the new initializer has been merged with the existing one
7744 and thus no warnings should be emitted about overriding an
7745 existing initializer. */
7747 static void
7748 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7749 tree field, int pending, bool implicit,
7750 struct obstack * braced_init_obstack)
7752 tree semantic_type = NULL_TREE;
7753 constructor_elt *celt;
7754 bool maybe_const = true;
7755 bool npc;
7757 if (type == error_mark_node || value == error_mark_node)
7759 constructor_erroneous = 1;
7760 return;
7762 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7763 && (TREE_CODE (value) == STRING_CST
7764 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7765 && !(TREE_CODE (value) == STRING_CST
7766 && TREE_CODE (type) == ARRAY_TYPE
7767 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7768 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7769 TYPE_MAIN_VARIANT (type)))
7770 value = array_to_pointer_conversion (input_location, value);
7772 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7773 && require_constant_value && !flag_isoc99 && pending)
7775 /* As an extension, allow initializing objects with static storage
7776 duration with compound literals (which are then treated just as
7777 the brace enclosed list they contain). */
7778 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7779 value = DECL_INITIAL (decl);
7782 npc = null_pointer_constant_p (value);
7783 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7785 semantic_type = TREE_TYPE (value);
7786 value = TREE_OPERAND (value, 0);
7788 value = c_fully_fold (value, require_constant_value, &maybe_const);
7790 if (value == error_mark_node)
7791 constructor_erroneous = 1;
7792 else if (!TREE_CONSTANT (value))
7793 constructor_constant = 0;
7794 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7795 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7796 || TREE_CODE (constructor_type) == UNION_TYPE)
7797 && DECL_C_BIT_FIELD (field)
7798 && TREE_CODE (value) != INTEGER_CST))
7799 constructor_simple = 0;
7800 if (!maybe_const)
7801 constructor_nonconst = 1;
7803 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7805 if (require_constant_value)
7807 error_init ("initializer element is not constant");
7808 value = error_mark_node;
7810 else if (require_constant_elements)
7811 pedwarn (input_location, 0,
7812 "initializer element is not computable at load time");
7814 else if (!maybe_const
7815 && (require_constant_value || require_constant_elements))
7816 pedwarn_init (input_location, 0,
7817 "initializer element is not a constant expression");
7819 /* Issue -Wc++-compat warnings about initializing a bitfield with
7820 enum type. */
7821 if (warn_cxx_compat
7822 && field != NULL_TREE
7823 && TREE_CODE (field) == FIELD_DECL
7824 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7825 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7826 != TYPE_MAIN_VARIANT (type))
7827 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7829 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7830 if (checktype != error_mark_node
7831 && (TYPE_MAIN_VARIANT (checktype)
7832 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7833 warning_init (OPT_Wc___compat,
7834 "enum conversion in initialization is invalid in C++");
7837 /* If this field is empty (and not at the end of structure),
7838 don't do anything other than checking the initializer. */
7839 if (field
7840 && (TREE_TYPE (field) == error_mark_node
7841 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7842 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7843 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7844 || DECL_CHAIN (field)))))
7845 return;
7847 if (semantic_type)
7848 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7849 value = digest_init (input_location, type, value, origtype, npc,
7850 strict_string, require_constant_value);
7851 if (value == error_mark_node)
7853 constructor_erroneous = 1;
7854 return;
7856 if (require_constant_value || require_constant_elements)
7857 constant_expression_warning (value);
7859 /* If this element doesn't come next in sequence,
7860 put it on constructor_pending_elts. */
7861 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7862 && (!constructor_incremental
7863 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7865 if (constructor_incremental
7866 && tree_int_cst_lt (field, constructor_unfilled_index))
7867 set_nonincremental_init (braced_init_obstack);
7869 add_pending_init (field, value, origtype, implicit,
7870 braced_init_obstack);
7871 return;
7873 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7874 && (!constructor_incremental
7875 || field != constructor_unfilled_fields))
7877 /* We do this for records but not for unions. In a union,
7878 no matter which field is specified, it can be initialized
7879 right away since it starts at the beginning of the union. */
7880 if (constructor_incremental)
7882 if (!constructor_unfilled_fields)
7883 set_nonincremental_init (braced_init_obstack);
7884 else
7886 tree bitpos, unfillpos;
7888 bitpos = bit_position (field);
7889 unfillpos = bit_position (constructor_unfilled_fields);
7891 if (tree_int_cst_lt (bitpos, unfillpos))
7892 set_nonincremental_init (braced_init_obstack);
7896 add_pending_init (field, value, origtype, implicit,
7897 braced_init_obstack);
7898 return;
7900 else if (TREE_CODE (constructor_type) == UNION_TYPE
7901 && !VEC_empty (constructor_elt, constructor_elements))
7903 if (!implicit)
7905 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7906 constructor_elements)->value))
7907 warning_init (0,
7908 "initialized field with side-effects overwritten");
7909 else if (warn_override_init)
7910 warning_init (OPT_Woverride_init, "initialized field overwritten");
7913 /* We can have just one union field set. */
7914 constructor_elements = 0;
7917 /* Otherwise, output this element either to
7918 constructor_elements or to the assembler file. */
7920 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7921 celt->index = field;
7922 celt->value = value;
7924 /* Advance the variable that indicates sequential elements output. */
7925 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7926 constructor_unfilled_index
7927 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7928 bitsize_one_node);
7929 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7931 constructor_unfilled_fields
7932 = DECL_CHAIN (constructor_unfilled_fields);
7934 /* Skip any nameless bit fields. */
7935 while (constructor_unfilled_fields != 0
7936 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7937 && DECL_NAME (constructor_unfilled_fields) == 0)
7938 constructor_unfilled_fields =
7939 DECL_CHAIN (constructor_unfilled_fields);
7941 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7942 constructor_unfilled_fields = 0;
7944 /* Now output any pending elements which have become next. */
7945 if (pending)
7946 output_pending_init_elements (0, braced_init_obstack);
7949 /* Output any pending elements which have become next.
7950 As we output elements, constructor_unfilled_{fields,index}
7951 advances, which may cause other elements to become next;
7952 if so, they too are output.
7954 If ALL is 0, we return when there are
7955 no more pending elements to output now.
7957 If ALL is 1, we output space as necessary so that
7958 we can output all the pending elements. */
7959 static void
7960 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7962 struct init_node *elt = constructor_pending_elts;
7963 tree next;
7965 retry:
7967 /* Look through the whole pending tree.
7968 If we find an element that should be output now,
7969 output it. Otherwise, set NEXT to the element
7970 that comes first among those still pending. */
7972 next = 0;
7973 while (elt)
7975 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7977 if (tree_int_cst_equal (elt->purpose,
7978 constructor_unfilled_index))
7979 output_init_element (elt->value, elt->origtype, true,
7980 TREE_TYPE (constructor_type),
7981 constructor_unfilled_index, 0, false,
7982 braced_init_obstack);
7983 else if (tree_int_cst_lt (constructor_unfilled_index,
7984 elt->purpose))
7986 /* Advance to the next smaller node. */
7987 if (elt->left)
7988 elt = elt->left;
7989 else
7991 /* We have reached the smallest node bigger than the
7992 current unfilled index. Fill the space first. */
7993 next = elt->purpose;
7994 break;
7997 else
7999 /* Advance to the next bigger node. */
8000 if (elt->right)
8001 elt = elt->right;
8002 else
8004 /* We have reached the biggest node in a subtree. Find
8005 the parent of it, which is the next bigger node. */
8006 while (elt->parent && elt->parent->right == elt)
8007 elt = elt->parent;
8008 elt = elt->parent;
8009 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8010 elt->purpose))
8012 next = elt->purpose;
8013 break;
8018 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8019 || TREE_CODE (constructor_type) == UNION_TYPE)
8021 tree ctor_unfilled_bitpos, elt_bitpos;
8023 /* If the current record is complete we are done. */
8024 if (constructor_unfilled_fields == 0)
8025 break;
8027 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8028 elt_bitpos = bit_position (elt->purpose);
8029 /* We can't compare fields here because there might be empty
8030 fields in between. */
8031 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8033 constructor_unfilled_fields = elt->purpose;
8034 output_init_element (elt->value, elt->origtype, true,
8035 TREE_TYPE (elt->purpose),
8036 elt->purpose, 0, false,
8037 braced_init_obstack);
8039 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8041 /* Advance to the next smaller node. */
8042 if (elt->left)
8043 elt = elt->left;
8044 else
8046 /* We have reached the smallest node bigger than the
8047 current unfilled field. Fill the space first. */
8048 next = elt->purpose;
8049 break;
8052 else
8054 /* Advance to the next bigger node. */
8055 if (elt->right)
8056 elt = elt->right;
8057 else
8059 /* We have reached the biggest node in a subtree. Find
8060 the parent of it, which is the next bigger node. */
8061 while (elt->parent && elt->parent->right == elt)
8062 elt = elt->parent;
8063 elt = elt->parent;
8064 if (elt
8065 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8066 bit_position (elt->purpose))))
8068 next = elt->purpose;
8069 break;
8076 /* Ordinarily return, but not if we want to output all
8077 and there are elements left. */
8078 if (!(all && next != 0))
8079 return;
8081 /* If it's not incremental, just skip over the gap, so that after
8082 jumping to retry we will output the next successive element. */
8083 if (TREE_CODE (constructor_type) == RECORD_TYPE
8084 || TREE_CODE (constructor_type) == UNION_TYPE)
8085 constructor_unfilled_fields = next;
8086 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8087 constructor_unfilled_index = next;
8089 /* ELT now points to the node in the pending tree with the next
8090 initializer to output. */
8091 goto retry;
8094 /* Add one non-braced element to the current constructor level.
8095 This adjusts the current position within the constructor's type.
8096 This may also start or terminate implicit levels
8097 to handle a partly-braced initializer.
8099 Once this has found the correct level for the new element,
8100 it calls output_init_element.
8102 IMPLICIT is true if value comes from pop_init_level (1),
8103 the new initializer has been merged with the existing one
8104 and thus no warnings should be emitted about overriding an
8105 existing initializer. */
8107 void
8108 process_init_element (struct c_expr value, bool implicit,
8109 struct obstack * braced_init_obstack)
8111 tree orig_value = value.value;
8112 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8113 bool strict_string = value.original_code == STRING_CST;
8115 designator_depth = 0;
8116 designator_erroneous = 0;
8118 /* Handle superfluous braces around string cst as in
8119 char x[] = {"foo"}; */
8120 if (string_flag
8121 && constructor_type
8122 && TREE_CODE (constructor_type) == ARRAY_TYPE
8123 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8124 && integer_zerop (constructor_unfilled_index))
8126 if (constructor_stack->replacement_value.value)
8127 error_init ("excess elements in char array initializer");
8128 constructor_stack->replacement_value = value;
8129 return;
8132 if (constructor_stack->replacement_value.value != 0)
8134 error_init ("excess elements in struct initializer");
8135 return;
8138 /* Ignore elements of a brace group if it is entirely superfluous
8139 and has already been diagnosed. */
8140 if (constructor_type == 0)
8141 return;
8143 /* If we've exhausted any levels that didn't have braces,
8144 pop them now. */
8145 while (constructor_stack->implicit)
8147 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8148 || TREE_CODE (constructor_type) == UNION_TYPE)
8149 && constructor_fields == 0)
8150 process_init_element (pop_init_level (1, braced_init_obstack),
8151 true, braced_init_obstack);
8152 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8153 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8154 && (constructor_max_index == 0
8155 || tree_int_cst_lt (constructor_max_index,
8156 constructor_index)))
8157 process_init_element (pop_init_level (1, braced_init_obstack),
8158 true, braced_init_obstack);
8159 else
8160 break;
8163 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8164 if (constructor_range_stack)
8166 /* If value is a compound literal and we'll be just using its
8167 content, don't put it into a SAVE_EXPR. */
8168 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8169 || !require_constant_value
8170 || flag_isoc99)
8172 tree semantic_type = NULL_TREE;
8173 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8175 semantic_type = TREE_TYPE (value.value);
8176 value.value = TREE_OPERAND (value.value, 0);
8178 value.value = c_save_expr (value.value);
8179 if (semantic_type)
8180 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8181 value.value);
8185 while (1)
8187 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8189 tree fieldtype;
8190 enum tree_code fieldcode;
8192 if (constructor_fields == 0)
8194 pedwarn_init (input_location, 0,
8195 "excess elements in struct initializer");
8196 break;
8199 fieldtype = TREE_TYPE (constructor_fields);
8200 if (fieldtype != error_mark_node)
8201 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8202 fieldcode = TREE_CODE (fieldtype);
8204 /* Error for non-static initialization of a flexible array member. */
8205 if (fieldcode == ARRAY_TYPE
8206 && !require_constant_value
8207 && TYPE_SIZE (fieldtype) == NULL_TREE
8208 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8210 error_init ("non-static initialization of a flexible array member");
8211 break;
8214 /* Accept a string constant to initialize a subarray. */
8215 if (value.value != 0
8216 && fieldcode == ARRAY_TYPE
8217 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8218 && string_flag)
8219 value.value = orig_value;
8220 /* Otherwise, if we have come to a subaggregate,
8221 and we don't have an element of its type, push into it. */
8222 else if (value.value != 0
8223 && value.value != error_mark_node
8224 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8225 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8226 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8228 push_init_level (1, braced_init_obstack);
8229 continue;
8232 if (value.value)
8234 push_member_name (constructor_fields);
8235 output_init_element (value.value, value.original_type,
8236 strict_string, fieldtype,
8237 constructor_fields, 1, implicit,
8238 braced_init_obstack);
8239 RESTORE_SPELLING_DEPTH (constructor_depth);
8241 else
8242 /* Do the bookkeeping for an element that was
8243 directly output as a constructor. */
8245 /* For a record, keep track of end position of last field. */
8246 if (DECL_SIZE (constructor_fields))
8247 constructor_bit_index
8248 = size_binop_loc (input_location, PLUS_EXPR,
8249 bit_position (constructor_fields),
8250 DECL_SIZE (constructor_fields));
8252 /* If the current field was the first one not yet written out,
8253 it isn't now, so update. */
8254 if (constructor_unfilled_fields == constructor_fields)
8256 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8257 /* Skip any nameless bit fields. */
8258 while (constructor_unfilled_fields != 0
8259 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8260 && DECL_NAME (constructor_unfilled_fields) == 0)
8261 constructor_unfilled_fields =
8262 DECL_CHAIN (constructor_unfilled_fields);
8266 constructor_fields = DECL_CHAIN (constructor_fields);
8267 /* Skip any nameless bit fields at the beginning. */
8268 while (constructor_fields != 0
8269 && DECL_C_BIT_FIELD (constructor_fields)
8270 && DECL_NAME (constructor_fields) == 0)
8271 constructor_fields = DECL_CHAIN (constructor_fields);
8273 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8275 tree fieldtype;
8276 enum tree_code fieldcode;
8278 if (constructor_fields == 0)
8280 pedwarn_init (input_location, 0,
8281 "excess elements in union initializer");
8282 break;
8285 fieldtype = TREE_TYPE (constructor_fields);
8286 if (fieldtype != error_mark_node)
8287 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8288 fieldcode = TREE_CODE (fieldtype);
8290 /* Warn that traditional C rejects initialization of unions.
8291 We skip the warning if the value is zero. This is done
8292 under the assumption that the zero initializer in user
8293 code appears conditioned on e.g. __STDC__ to avoid
8294 "missing initializer" warnings and relies on default
8295 initialization to zero in the traditional C case.
8296 We also skip the warning if the initializer is designated,
8297 again on the assumption that this must be conditional on
8298 __STDC__ anyway (and we've already complained about the
8299 member-designator already). */
8300 if (!in_system_header && !constructor_designated
8301 && !(value.value && (integer_zerop (value.value)
8302 || real_zerop (value.value))))
8303 warning (OPT_Wtraditional, "traditional C rejects initialization "
8304 "of unions");
8306 /* Accept a string constant to initialize a subarray. */
8307 if (value.value != 0
8308 && fieldcode == ARRAY_TYPE
8309 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8310 && string_flag)
8311 value.value = orig_value;
8312 /* Otherwise, if we have come to a subaggregate,
8313 and we don't have an element of its type, push into it. */
8314 else if (value.value != 0
8315 && value.value != error_mark_node
8316 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8317 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8318 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8320 push_init_level (1, braced_init_obstack);
8321 continue;
8324 if (value.value)
8326 push_member_name (constructor_fields);
8327 output_init_element (value.value, value.original_type,
8328 strict_string, fieldtype,
8329 constructor_fields, 1, implicit,
8330 braced_init_obstack);
8331 RESTORE_SPELLING_DEPTH (constructor_depth);
8333 else
8334 /* Do the bookkeeping for an element that was
8335 directly output as a constructor. */
8337 constructor_bit_index = DECL_SIZE (constructor_fields);
8338 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8341 constructor_fields = 0;
8343 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8345 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8346 enum tree_code eltcode = TREE_CODE (elttype);
8348 /* Accept a string constant to initialize a subarray. */
8349 if (value.value != 0
8350 && eltcode == ARRAY_TYPE
8351 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8352 && string_flag)
8353 value.value = orig_value;
8354 /* Otherwise, if we have come to a subaggregate,
8355 and we don't have an element of its type, push into it. */
8356 else if (value.value != 0
8357 && value.value != error_mark_node
8358 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8359 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8360 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8362 push_init_level (1, braced_init_obstack);
8363 continue;
8366 if (constructor_max_index != 0
8367 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8368 || integer_all_onesp (constructor_max_index)))
8370 pedwarn_init (input_location, 0,
8371 "excess elements in array initializer");
8372 break;
8375 /* Now output the actual element. */
8376 if (value.value)
8378 push_array_bounds (tree_low_cst (constructor_index, 1));
8379 output_init_element (value.value, value.original_type,
8380 strict_string, elttype,
8381 constructor_index, 1, implicit,
8382 braced_init_obstack);
8383 RESTORE_SPELLING_DEPTH (constructor_depth);
8386 constructor_index
8387 = size_binop_loc (input_location, PLUS_EXPR,
8388 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;
8396 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8398 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8400 /* Do a basic check of initializer size. Note that vectors
8401 always have a fixed size derived from their type. */
8402 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8404 pedwarn_init (input_location, 0,
8405 "excess elements in vector initializer");
8406 break;
8409 /* Now output the actual element. */
8410 if (value.value)
8412 if (TREE_CODE (value.value) == VECTOR_CST)
8413 elttype = TYPE_MAIN_VARIANT (constructor_type);
8414 output_init_element (value.value, value.original_type,
8415 strict_string, elttype,
8416 constructor_index, 1, implicit,
8417 braced_init_obstack);
8420 constructor_index
8421 = size_binop_loc (input_location,
8422 PLUS_EXPR, constructor_index, bitsize_one_node);
8424 if (!value.value)
8425 /* If we are doing the bookkeeping for an element that was
8426 directly output as a constructor, we must update
8427 constructor_unfilled_index. */
8428 constructor_unfilled_index = constructor_index;
8431 /* Handle the sole element allowed in a braced initializer
8432 for a scalar variable. */
8433 else if (constructor_type != error_mark_node
8434 && constructor_fields == 0)
8436 pedwarn_init (input_location, 0,
8437 "excess elements in scalar initializer");
8438 break;
8440 else
8442 if (value.value)
8443 output_init_element (value.value, value.original_type,
8444 strict_string, constructor_type,
8445 NULL_TREE, 1, implicit,
8446 braced_init_obstack);
8447 constructor_fields = 0;
8450 /* Handle range initializers either at this level or anywhere higher
8451 in the designator stack. */
8452 if (constructor_range_stack)
8454 struct constructor_range_stack *p, *range_stack;
8455 int finish = 0;
8457 range_stack = constructor_range_stack;
8458 constructor_range_stack = 0;
8459 while (constructor_stack != range_stack->stack)
8461 gcc_assert (constructor_stack->implicit);
8462 process_init_element (pop_init_level (1,
8463 braced_init_obstack),
8464 true, braced_init_obstack);
8466 for (p = range_stack;
8467 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8468 p = p->prev)
8470 gcc_assert (constructor_stack->implicit);
8471 process_init_element (pop_init_level (1, braced_init_obstack),
8472 true, braced_init_obstack);
8475 p->index = size_binop_loc (input_location,
8476 PLUS_EXPR, p->index, bitsize_one_node);
8477 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8478 finish = 1;
8480 while (1)
8482 constructor_index = p->index;
8483 constructor_fields = p->fields;
8484 if (finish && p->range_end && p->index == p->range_start)
8486 finish = 0;
8487 p->prev = 0;
8489 p = p->next;
8490 if (!p)
8491 break;
8492 push_init_level (2, braced_init_obstack);
8493 p->stack = constructor_stack;
8494 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8495 p->index = p->range_start;
8498 if (!finish)
8499 constructor_range_stack = range_stack;
8500 continue;
8503 break;
8506 constructor_range_stack = 0;
8509 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8510 (guaranteed to be 'volatile' or null) and ARGS (represented using
8511 an ASM_EXPR node). */
8512 tree
8513 build_asm_stmt (tree cv_qualifier, tree args)
8515 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8516 ASM_VOLATILE_P (args) = 1;
8517 return add_stmt (args);
8520 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8521 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8522 SIMPLE indicates whether there was anything at all after the
8523 string in the asm expression -- asm("blah") and asm("blah" : )
8524 are subtly different. We use a ASM_EXPR node to represent this. */
8525 tree
8526 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8527 tree clobbers, tree labels, bool simple)
8529 tree tail;
8530 tree args;
8531 int i;
8532 const char *constraint;
8533 const char **oconstraints;
8534 bool allows_mem, allows_reg, is_inout;
8535 int ninputs, noutputs;
8537 ninputs = list_length (inputs);
8538 noutputs = list_length (outputs);
8539 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8541 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8543 /* Remove output conversions that change the type but not the mode. */
8544 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8546 tree output = TREE_VALUE (tail);
8548 /* ??? Really, this should not be here. Users should be using a
8549 proper lvalue, dammit. But there's a long history of using casts
8550 in the output operands. In cases like longlong.h, this becomes a
8551 primitive form of typechecking -- if the cast can be removed, then
8552 the output operand had a type of the proper width; otherwise we'll
8553 get an error. Gross, but ... */
8554 STRIP_NOPS (output);
8556 if (!lvalue_or_else (output, lv_asm))
8557 output = error_mark_node;
8559 if (output != error_mark_node
8560 && (TREE_READONLY (output)
8561 || TYPE_READONLY (TREE_TYPE (output))
8562 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8563 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8564 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8565 readonly_error (output, lv_asm);
8567 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8568 oconstraints[i] = constraint;
8570 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8571 &allows_mem, &allows_reg, &is_inout))
8573 /* If the operand is going to end up in memory,
8574 mark it addressable. */
8575 if (!allows_reg && !c_mark_addressable (output))
8576 output = error_mark_node;
8578 else
8579 output = error_mark_node;
8581 TREE_VALUE (tail) = output;
8584 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8586 tree input;
8588 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8589 input = TREE_VALUE (tail);
8591 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8592 oconstraints, &allows_mem, &allows_reg))
8594 /* If the operand is going to end up in memory,
8595 mark it addressable. */
8596 if (!allows_reg && allows_mem)
8598 /* Strip the nops as we allow this case. FIXME, this really
8599 should be rejected or made deprecated. */
8600 STRIP_NOPS (input);
8601 if (!c_mark_addressable (input))
8602 input = error_mark_node;
8605 else
8606 input = error_mark_node;
8608 TREE_VALUE (tail) = input;
8611 /* ASMs with labels cannot have outputs. This should have been
8612 enforced by the parser. */
8613 gcc_assert (outputs == NULL || labels == NULL);
8615 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8617 /* asm statements without outputs, including simple ones, are treated
8618 as volatile. */
8619 ASM_INPUT_P (args) = simple;
8620 ASM_VOLATILE_P (args) = (noutputs == 0);
8622 return args;
8625 /* Generate a goto statement to LABEL. LOC is the location of the
8626 GOTO. */
8628 tree
8629 c_finish_goto_label (location_t loc, tree label)
8631 tree decl = lookup_label_for_goto (loc, label);
8632 if (!decl)
8633 return NULL_TREE;
8634 TREE_USED (decl) = 1;
8636 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8637 SET_EXPR_LOCATION (t, loc);
8638 return add_stmt (t);
8642 /* Generate a computed goto statement to EXPR. LOC is the location of
8643 the GOTO. */
8645 tree
8646 c_finish_goto_ptr (location_t loc, tree expr)
8648 tree t;
8649 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8650 expr = c_fully_fold (expr, false, NULL);
8651 expr = convert (ptr_type_node, expr);
8652 t = build1 (GOTO_EXPR, void_type_node, expr);
8653 SET_EXPR_LOCATION (t, loc);
8654 return add_stmt (t);
8657 /* Generate a C `return' statement. RETVAL is the expression for what
8658 to return, or a null pointer for `return;' with no value. LOC is
8659 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8660 is the original type of RETVAL. */
8662 tree
8663 c_finish_return (location_t loc, tree retval, tree origtype)
8665 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8666 bool no_warning = false;
8667 bool npc = false;
8669 if (TREE_THIS_VOLATILE (current_function_decl))
8670 warning_at (loc, 0,
8671 "function declared %<noreturn%> has a %<return%> statement");
8673 if (retval)
8675 tree semantic_type = NULL_TREE;
8676 npc = null_pointer_constant_p (retval);
8677 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8679 semantic_type = TREE_TYPE (retval);
8680 retval = TREE_OPERAND (retval, 0);
8682 retval = c_fully_fold (retval, false, NULL);
8683 if (semantic_type)
8684 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8687 if (!retval)
8689 current_function_returns_null = 1;
8690 if ((warn_return_type || flag_isoc99)
8691 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8693 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8694 "%<return%> with no value, in "
8695 "function returning non-void");
8696 no_warning = true;
8699 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8701 current_function_returns_null = 1;
8702 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8703 pedwarn (loc, 0,
8704 "%<return%> with a value, in function returning void");
8705 else
8706 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8707 "%<return%> with expression, in function returning void");
8709 else
8711 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8712 ic_return,
8713 npc, NULL_TREE, NULL_TREE, 0);
8714 tree res = DECL_RESULT (current_function_decl);
8715 tree inner;
8717 current_function_returns_value = 1;
8718 if (t == error_mark_node)
8719 return NULL_TREE;
8721 inner = t = convert (TREE_TYPE (res), t);
8723 /* Strip any conversions, additions, and subtractions, and see if
8724 we are returning the address of a local variable. Warn if so. */
8725 while (1)
8727 switch (TREE_CODE (inner))
8729 CASE_CONVERT:
8730 case NON_LVALUE_EXPR:
8731 case PLUS_EXPR:
8732 case POINTER_PLUS_EXPR:
8733 inner = TREE_OPERAND (inner, 0);
8734 continue;
8736 case MINUS_EXPR:
8737 /* If the second operand of the MINUS_EXPR has a pointer
8738 type (or is converted from it), this may be valid, so
8739 don't give a warning. */
8741 tree op1 = TREE_OPERAND (inner, 1);
8743 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8744 && (CONVERT_EXPR_P (op1)
8745 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8746 op1 = TREE_OPERAND (op1, 0);
8748 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8749 break;
8751 inner = TREE_OPERAND (inner, 0);
8752 continue;
8755 case ADDR_EXPR:
8756 inner = TREE_OPERAND (inner, 0);
8758 while (REFERENCE_CLASS_P (inner)
8759 && TREE_CODE (inner) != INDIRECT_REF)
8760 inner = TREE_OPERAND (inner, 0);
8762 if (DECL_P (inner)
8763 && !DECL_EXTERNAL (inner)
8764 && !TREE_STATIC (inner)
8765 && DECL_CONTEXT (inner) == current_function_decl)
8766 warning_at (loc,
8767 0, "function returns address of local variable");
8768 break;
8770 default:
8771 break;
8774 break;
8777 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8778 SET_EXPR_LOCATION (retval, loc);
8780 if (warn_sequence_point)
8781 verify_sequence_points (retval);
8784 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8785 TREE_NO_WARNING (ret_stmt) |= no_warning;
8786 return add_stmt (ret_stmt);
8789 struct c_switch {
8790 /* The SWITCH_EXPR being built. */
8791 tree switch_expr;
8793 /* The original type of the testing expression, i.e. before the
8794 default conversion is applied. */
8795 tree orig_type;
8797 /* A splay-tree mapping the low element of a case range to the high
8798 element, or NULL_TREE if there is no high element. Used to
8799 determine whether or not a new case label duplicates an old case
8800 label. We need a tree, rather than simply a hash table, because
8801 of the GNU case range extension. */
8802 splay_tree cases;
8804 /* The bindings at the point of the switch. This is used for
8805 warnings crossing decls when branching to a case label. */
8806 struct c_spot_bindings *bindings;
8808 /* The next node on the stack. */
8809 struct c_switch *next;
8812 /* A stack of the currently active switch statements. The innermost
8813 switch statement is on the top of the stack. There is no need to
8814 mark the stack for garbage collection because it is only active
8815 during the processing of the body of a function, and we never
8816 collect at that point. */
8818 struct c_switch *c_switch_stack;
8820 /* Start a C switch statement, testing expression EXP. Return the new
8821 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8822 SWITCH_COND_LOC is the location of the switch's condition. */
8824 tree
8825 c_start_case (location_t switch_loc,
8826 location_t switch_cond_loc,
8827 tree exp)
8829 tree orig_type = error_mark_node;
8830 struct c_switch *cs;
8832 if (exp != error_mark_node)
8834 orig_type = TREE_TYPE (exp);
8836 if (!INTEGRAL_TYPE_P (orig_type))
8838 if (orig_type != error_mark_node)
8840 error_at (switch_cond_loc, "switch quantity not an integer");
8841 orig_type = error_mark_node;
8843 exp = integer_zero_node;
8845 else
8847 tree type = TYPE_MAIN_VARIANT (orig_type);
8849 if (!in_system_header
8850 && (type == long_integer_type_node
8851 || type == long_unsigned_type_node))
8852 warning_at (switch_cond_loc,
8853 OPT_Wtraditional, "%<long%> switch expression not "
8854 "converted to %<int%> in ISO C");
8856 exp = c_fully_fold (exp, false, NULL);
8857 exp = default_conversion (exp);
8859 if (warn_sequence_point)
8860 verify_sequence_points (exp);
8864 /* Add this new SWITCH_EXPR to the stack. */
8865 cs = XNEW (struct c_switch);
8866 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8867 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8868 cs->orig_type = orig_type;
8869 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8870 cs->bindings = c_get_switch_bindings ();
8871 cs->next = c_switch_stack;
8872 c_switch_stack = cs;
8874 return add_stmt (cs->switch_expr);
8877 /* Process a case label at location LOC. */
8879 tree
8880 do_case (location_t loc, tree low_value, tree high_value)
8882 tree label = NULL_TREE;
8884 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8886 low_value = c_fully_fold (low_value, false, NULL);
8887 if (TREE_CODE (low_value) == INTEGER_CST)
8888 pedwarn (input_location, OPT_pedantic,
8889 "case label is not an integer constant expression");
8892 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8894 high_value = c_fully_fold (high_value, false, NULL);
8895 if (TREE_CODE (high_value) == INTEGER_CST)
8896 pedwarn (input_location, OPT_pedantic,
8897 "case label is not an integer constant expression");
8900 if (c_switch_stack == NULL)
8902 if (low_value)
8903 error_at (loc, "case label not within a switch statement");
8904 else
8905 error_at (loc, "%<default%> label not within a switch statement");
8906 return NULL_TREE;
8909 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8910 EXPR_LOCATION (c_switch_stack->switch_expr),
8911 loc))
8912 return NULL_TREE;
8914 label = c_add_case_label (loc, c_switch_stack->cases,
8915 SWITCH_COND (c_switch_stack->switch_expr),
8916 c_switch_stack->orig_type,
8917 low_value, high_value);
8918 if (label == error_mark_node)
8919 label = NULL_TREE;
8920 return label;
8923 /* Finish the switch statement. */
8925 void
8926 c_finish_case (tree body)
8928 struct c_switch *cs = c_switch_stack;
8929 location_t switch_location;
8931 SWITCH_BODY (cs->switch_expr) = body;
8933 /* Emit warnings as needed. */
8934 switch_location = EXPR_LOCATION (cs->switch_expr);
8935 c_do_switch_warnings (cs->cases, switch_location,
8936 TREE_TYPE (cs->switch_expr),
8937 SWITCH_COND (cs->switch_expr));
8939 /* Pop the stack. */
8940 c_switch_stack = cs->next;
8941 splay_tree_delete (cs->cases);
8942 c_release_switch_bindings (cs->bindings);
8943 XDELETE (cs);
8946 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8947 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8948 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8949 statement, and was not surrounded with parenthesis. */
8951 void
8952 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8953 tree else_block, bool nested_if)
8955 tree stmt;
8957 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8958 if (warn_parentheses && nested_if && else_block == NULL)
8960 tree inner_if = then_block;
8962 /* We know from the grammar productions that there is an IF nested
8963 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8964 it might not be exactly THEN_BLOCK, but should be the last
8965 non-container statement within. */
8966 while (1)
8967 switch (TREE_CODE (inner_if))
8969 case COND_EXPR:
8970 goto found;
8971 case BIND_EXPR:
8972 inner_if = BIND_EXPR_BODY (inner_if);
8973 break;
8974 case STATEMENT_LIST:
8975 inner_if = expr_last (then_block);
8976 break;
8977 case TRY_FINALLY_EXPR:
8978 case TRY_CATCH_EXPR:
8979 inner_if = TREE_OPERAND (inner_if, 0);
8980 break;
8981 default:
8982 gcc_unreachable ();
8984 found:
8986 if (COND_EXPR_ELSE (inner_if))
8987 warning_at (if_locus, OPT_Wparentheses,
8988 "suggest explicit braces to avoid ambiguous %<else%>");
8991 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8992 SET_EXPR_LOCATION (stmt, if_locus);
8993 add_stmt (stmt);
8996 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8997 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8998 is false for DO loops. INCR is the FOR increment expression. BODY is
8999 the statement controlled by the loop. BLAB is the break label. CLAB is
9000 the continue label. Everything is allowed to be NULL. */
9002 void
9003 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9004 tree blab, tree clab, bool cond_is_first)
9006 tree entry = NULL, exit = NULL, t;
9008 /* If the condition is zero don't generate a loop construct. */
9009 if (cond && integer_zerop (cond))
9011 if (cond_is_first)
9013 t = build_and_jump (&blab);
9014 SET_EXPR_LOCATION (t, start_locus);
9015 add_stmt (t);
9018 else
9020 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9022 /* If we have an exit condition, then we build an IF with gotos either
9023 out of the loop, or to the top of it. If there's no exit condition,
9024 then we just build a jump back to the top. */
9025 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9027 if (cond && !integer_nonzerop (cond))
9029 /* Canonicalize the loop condition to the end. This means
9030 generating a branch to the loop condition. Reuse the
9031 continue label, if possible. */
9032 if (cond_is_first)
9034 if (incr || !clab)
9036 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9037 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9039 else
9040 t = build1 (GOTO_EXPR, void_type_node, clab);
9041 SET_EXPR_LOCATION (t, start_locus);
9042 add_stmt (t);
9045 t = build_and_jump (&blab);
9046 if (cond_is_first)
9047 exit = fold_build3_loc (start_locus,
9048 COND_EXPR, void_type_node, cond, exit, t);
9049 else
9050 exit = fold_build3_loc (input_location,
9051 COND_EXPR, void_type_node, cond, exit, t);
9054 add_stmt (top);
9057 if (body)
9058 add_stmt (body);
9059 if (clab)
9060 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9061 if (incr)
9062 add_stmt (incr);
9063 if (entry)
9064 add_stmt (entry);
9065 if (exit)
9066 add_stmt (exit);
9067 if (blab)
9068 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9071 tree
9072 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9074 bool skip;
9075 tree label = *label_p;
9077 /* In switch statements break is sometimes stylistically used after
9078 a return statement. This can lead to spurious warnings about
9079 control reaching the end of a non-void function when it is
9080 inlined. Note that we are calling block_may_fallthru with
9081 language specific tree nodes; this works because
9082 block_may_fallthru returns true when given something it does not
9083 understand. */
9084 skip = !block_may_fallthru (cur_stmt_list);
9086 if (!label)
9088 if (!skip)
9089 *label_p = label = create_artificial_label (loc);
9091 else if (TREE_CODE (label) == LABEL_DECL)
9093 else switch (TREE_INT_CST_LOW (label))
9095 case 0:
9096 if (is_break)
9097 error_at (loc, "break statement not within loop or switch");
9098 else
9099 error_at (loc, "continue statement not within a loop");
9100 return NULL_TREE;
9102 case 1:
9103 gcc_assert (is_break);
9104 error_at (loc, "break statement used with OpenMP for loop");
9105 return NULL_TREE;
9107 default:
9108 gcc_unreachable ();
9111 if (skip)
9112 return NULL_TREE;
9114 if (!is_break)
9115 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9117 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9120 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9122 static void
9123 emit_side_effect_warnings (location_t loc, tree expr)
9125 if (expr == error_mark_node)
9127 else if (!TREE_SIDE_EFFECTS (expr))
9129 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9130 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9132 else
9133 warn_if_unused_value (expr, loc);
9136 /* Process an expression as if it were a complete statement. Emit
9137 diagnostics, but do not call ADD_STMT. LOC is the location of the
9138 statement. */
9140 tree
9141 c_process_expr_stmt (location_t loc, tree expr)
9143 tree exprv;
9145 if (!expr)
9146 return NULL_TREE;
9148 expr = c_fully_fold (expr, false, NULL);
9150 if (warn_sequence_point)
9151 verify_sequence_points (expr);
9153 if (TREE_TYPE (expr) != error_mark_node
9154 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9155 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9156 error_at (loc, "expression statement has incomplete type");
9158 /* If we're not processing a statement expression, warn about unused values.
9159 Warnings for statement expressions will be emitted later, once we figure
9160 out which is the result. */
9161 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9162 && warn_unused_value)
9163 emit_side_effect_warnings (loc, expr);
9165 exprv = expr;
9166 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9167 exprv = TREE_OPERAND (exprv, 1);
9168 if (DECL_P (exprv) || handled_component_p (exprv))
9169 mark_exp_read (exprv);
9171 /* If the expression is not of a type to which we cannot assign a line
9172 number, wrap the thing in a no-op NOP_EXPR. */
9173 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9175 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9176 SET_EXPR_LOCATION (expr, loc);
9179 return expr;
9182 /* Emit an expression as a statement. LOC is the location of the
9183 expression. */
9185 tree
9186 c_finish_expr_stmt (location_t loc, tree expr)
9188 if (expr)
9189 return add_stmt (c_process_expr_stmt (loc, expr));
9190 else
9191 return NULL;
9194 /* Do the opposite and emit a statement as an expression. To begin,
9195 create a new binding level and return it. */
9197 tree
9198 c_begin_stmt_expr (void)
9200 tree ret;
9202 /* We must force a BLOCK for this level so that, if it is not expanded
9203 later, there is a way to turn off the entire subtree of blocks that
9204 are contained in it. */
9205 keep_next_level ();
9206 ret = c_begin_compound_stmt (true);
9208 c_bindings_start_stmt_expr (c_switch_stack == NULL
9209 ? NULL
9210 : c_switch_stack->bindings);
9212 /* Mark the current statement list as belonging to a statement list. */
9213 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9215 return ret;
9218 /* LOC is the location of the compound statement to which this body
9219 belongs. */
9221 tree
9222 c_finish_stmt_expr (location_t loc, tree body)
9224 tree last, type, tmp, val;
9225 tree *last_p;
9227 body = c_end_compound_stmt (loc, body, true);
9229 c_bindings_end_stmt_expr (c_switch_stack == NULL
9230 ? NULL
9231 : c_switch_stack->bindings);
9233 /* Locate the last statement in BODY. See c_end_compound_stmt
9234 about always returning a BIND_EXPR. */
9235 last_p = &BIND_EXPR_BODY (body);
9236 last = BIND_EXPR_BODY (body);
9238 continue_searching:
9239 if (TREE_CODE (last) == STATEMENT_LIST)
9241 tree_stmt_iterator i;
9243 /* This can happen with degenerate cases like ({ }). No value. */
9244 if (!TREE_SIDE_EFFECTS (last))
9245 return body;
9247 /* If we're supposed to generate side effects warnings, process
9248 all of the statements except the last. */
9249 if (warn_unused_value)
9251 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9253 location_t tloc;
9254 tree t = tsi_stmt (i);
9256 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9257 emit_side_effect_warnings (tloc, t);
9260 else
9261 i = tsi_last (last);
9262 last_p = tsi_stmt_ptr (i);
9263 last = *last_p;
9266 /* If the end of the list is exception related, then the list was split
9267 by a call to push_cleanup. Continue searching. */
9268 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9269 || TREE_CODE (last) == TRY_CATCH_EXPR)
9271 last_p = &TREE_OPERAND (last, 0);
9272 last = *last_p;
9273 goto continue_searching;
9276 if (last == error_mark_node)
9277 return last;
9279 /* In the case that the BIND_EXPR is not necessary, return the
9280 expression out from inside it. */
9281 if (last == BIND_EXPR_BODY (body)
9282 && BIND_EXPR_VARS (body) == NULL)
9284 /* Even if this looks constant, do not allow it in a constant
9285 expression. */
9286 last = c_wrap_maybe_const (last, true);
9287 /* Do not warn if the return value of a statement expression is
9288 unused. */
9289 TREE_NO_WARNING (last) = 1;
9290 return last;
9293 /* Extract the type of said expression. */
9294 type = TREE_TYPE (last);
9296 /* If we're not returning a value at all, then the BIND_EXPR that
9297 we already have is a fine expression to return. */
9298 if (!type || VOID_TYPE_P (type))
9299 return body;
9301 /* Now that we've located the expression containing the value, it seems
9302 silly to make voidify_wrapper_expr repeat the process. Create a
9303 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9304 tmp = create_tmp_var_raw (type, NULL);
9306 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9307 tree_expr_nonnegative_p giving up immediately. */
9308 val = last;
9309 if (TREE_CODE (val) == NOP_EXPR
9310 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9311 val = TREE_OPERAND (val, 0);
9313 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9314 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9317 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9318 SET_EXPR_LOCATION (t, loc);
9319 return t;
9323 /* Begin and end compound statements. This is as simple as pushing
9324 and popping new statement lists from the tree. */
9326 tree
9327 c_begin_compound_stmt (bool do_scope)
9329 tree stmt = push_stmt_list ();
9330 if (do_scope)
9331 push_scope ();
9332 return stmt;
9335 /* End a compound statement. STMT is the statement. LOC is the
9336 location of the compound statement-- this is usually the location
9337 of the opening brace. */
9339 tree
9340 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9342 tree block = NULL;
9344 if (do_scope)
9346 if (c_dialect_objc ())
9347 objc_clear_super_receiver ();
9348 block = pop_scope ();
9351 stmt = pop_stmt_list (stmt);
9352 stmt = c_build_bind_expr (loc, block, stmt);
9354 /* If this compound statement is nested immediately inside a statement
9355 expression, then force a BIND_EXPR to be created. Otherwise we'll
9356 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9357 STATEMENT_LISTs merge, and thus we can lose track of what statement
9358 was really last. */
9359 if (cur_stmt_list
9360 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9361 && TREE_CODE (stmt) != BIND_EXPR)
9363 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9364 TREE_SIDE_EFFECTS (stmt) = 1;
9365 SET_EXPR_LOCATION (stmt, loc);
9368 return stmt;
9371 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9372 when the current scope is exited. EH_ONLY is true when this is not
9373 meant to apply to normal control flow transfer. */
9375 void
9376 push_cleanup (tree decl, tree cleanup, bool eh_only)
9378 enum tree_code code;
9379 tree stmt, list;
9380 bool stmt_expr;
9382 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9383 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9384 add_stmt (stmt);
9385 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9386 list = push_stmt_list ();
9387 TREE_OPERAND (stmt, 0) = list;
9388 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9391 /* Build a binary-operation expression without default conversions.
9392 CODE is the kind of expression to build.
9393 LOCATION is the operator's location.
9394 This function differs from `build' in several ways:
9395 the data type of the result is computed and recorded in it,
9396 warnings are generated if arg data types are invalid,
9397 special handling for addition and subtraction of pointers is known,
9398 and some optimization is done (operations on narrow ints
9399 are done in the narrower type when that gives the same result).
9400 Constant folding is also done before the result is returned.
9402 Note that the operands will never have enumeral types, or function
9403 or array types, because either they will have the default conversions
9404 performed or they have both just been converted to some other type in which
9405 the arithmetic is to be done. */
9407 tree
9408 build_binary_op (location_t location, enum tree_code code,
9409 tree orig_op0, tree orig_op1, int convert_p)
9411 tree type0, type1, orig_type0, orig_type1;
9412 tree eptype;
9413 enum tree_code code0, code1;
9414 tree op0, op1;
9415 tree ret = error_mark_node;
9416 const char *invalid_op_diag;
9417 bool op0_int_operands, op1_int_operands;
9418 bool int_const, int_const_or_overflow, int_operands;
9420 /* Expression code to give to the expression when it is built.
9421 Normally this is CODE, which is what the caller asked for,
9422 but in some special cases we change it. */
9423 enum tree_code resultcode = code;
9425 /* Data type in which the computation is to be performed.
9426 In the simplest cases this is the common type of the arguments. */
9427 tree result_type = NULL;
9429 /* When the computation is in excess precision, the type of the
9430 final EXCESS_PRECISION_EXPR. */
9431 tree semantic_result_type = NULL;
9433 /* Nonzero means operands have already been type-converted
9434 in whatever way is necessary.
9435 Zero means they need to be converted to RESULT_TYPE. */
9436 int converted = 0;
9438 /* Nonzero means create the expression with this type, rather than
9439 RESULT_TYPE. */
9440 tree build_type = 0;
9442 /* Nonzero means after finally constructing the expression
9443 convert it to this type. */
9444 tree final_type = 0;
9446 /* Nonzero if this is an operation like MIN or MAX which can
9447 safely be computed in short if both args are promoted shorts.
9448 Also implies COMMON.
9449 -1 indicates a bitwise operation; this makes a difference
9450 in the exact conditions for when it is safe to do the operation
9451 in a narrower mode. */
9452 int shorten = 0;
9454 /* Nonzero if this is a comparison operation;
9455 if both args are promoted shorts, compare the original shorts.
9456 Also implies COMMON. */
9457 int short_compare = 0;
9459 /* Nonzero if this is a right-shift operation, which can be computed on the
9460 original short and then promoted if the operand is a promoted short. */
9461 int short_shift = 0;
9463 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9464 int common = 0;
9466 /* True means types are compatible as far as ObjC is concerned. */
9467 bool objc_ok;
9469 /* True means this is an arithmetic operation that may need excess
9470 precision. */
9471 bool may_need_excess_precision;
9473 /* True means this is a boolean operation that converts both its
9474 operands to truth-values. */
9475 bool boolean_op = false;
9477 if (location == UNKNOWN_LOCATION)
9478 location = input_location;
9480 op0 = orig_op0;
9481 op1 = orig_op1;
9483 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9484 if (op0_int_operands)
9485 op0 = remove_c_maybe_const_expr (op0);
9486 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9487 if (op1_int_operands)
9488 op1 = remove_c_maybe_const_expr (op1);
9489 int_operands = (op0_int_operands && op1_int_operands);
9490 if (int_operands)
9492 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9493 && TREE_CODE (orig_op1) == INTEGER_CST);
9494 int_const = (int_const_or_overflow
9495 && !TREE_OVERFLOW (orig_op0)
9496 && !TREE_OVERFLOW (orig_op1));
9498 else
9499 int_const = int_const_or_overflow = false;
9501 if (convert_p)
9503 op0 = default_conversion (op0);
9504 op1 = default_conversion (op1);
9507 orig_type0 = type0 = TREE_TYPE (op0);
9508 orig_type1 = type1 = TREE_TYPE (op1);
9510 /* The expression codes of the data types of the arguments tell us
9511 whether the arguments are integers, floating, pointers, etc. */
9512 code0 = TREE_CODE (type0);
9513 code1 = TREE_CODE (type1);
9515 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9516 STRIP_TYPE_NOPS (op0);
9517 STRIP_TYPE_NOPS (op1);
9519 /* If an error was already reported for one of the arguments,
9520 avoid reporting another error. */
9522 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9523 return error_mark_node;
9525 if ((invalid_op_diag
9526 = targetm.invalid_binary_op (code, type0, type1)))
9528 error_at (location, invalid_op_diag);
9529 return error_mark_node;
9532 switch (code)
9534 case PLUS_EXPR:
9535 case MINUS_EXPR:
9536 case MULT_EXPR:
9537 case TRUNC_DIV_EXPR:
9538 case CEIL_DIV_EXPR:
9539 case FLOOR_DIV_EXPR:
9540 case ROUND_DIV_EXPR:
9541 case EXACT_DIV_EXPR:
9542 may_need_excess_precision = true;
9543 break;
9544 default:
9545 may_need_excess_precision = false;
9546 break;
9548 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9550 op0 = TREE_OPERAND (op0, 0);
9551 type0 = TREE_TYPE (op0);
9553 else if (may_need_excess_precision
9554 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9556 type0 = eptype;
9557 op0 = convert (eptype, op0);
9559 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9561 op1 = TREE_OPERAND (op1, 0);
9562 type1 = TREE_TYPE (op1);
9564 else if (may_need_excess_precision
9565 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9567 type1 = eptype;
9568 op1 = convert (eptype, op1);
9571 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9573 switch (code)
9575 case PLUS_EXPR:
9576 /* Handle the pointer + int case. */
9577 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9579 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9580 goto return_build_binary_op;
9582 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9584 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9585 goto return_build_binary_op;
9587 else
9588 common = 1;
9589 break;
9591 case MINUS_EXPR:
9592 /* Subtraction of two similar pointers.
9593 We must subtract them as integers, then divide by object size. */
9594 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9595 && comp_target_types (location, type0, type1))
9597 ret = pointer_diff (location, op0, op1);
9598 goto return_build_binary_op;
9600 /* Handle pointer minus int. Just like pointer plus int. */
9601 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9603 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9604 goto return_build_binary_op;
9606 else
9607 common = 1;
9608 break;
9610 case MULT_EXPR:
9611 common = 1;
9612 break;
9614 case TRUNC_DIV_EXPR:
9615 case CEIL_DIV_EXPR:
9616 case FLOOR_DIV_EXPR:
9617 case ROUND_DIV_EXPR:
9618 case EXACT_DIV_EXPR:
9619 warn_for_div_by_zero (location, op1);
9621 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9622 || code0 == FIXED_POINT_TYPE
9623 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9624 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9625 || code1 == FIXED_POINT_TYPE
9626 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9628 enum tree_code tcode0 = code0, tcode1 = code1;
9630 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9631 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9632 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9633 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9635 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9636 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9637 resultcode = RDIV_EXPR;
9638 else
9639 /* Although it would be tempting to shorten always here, that
9640 loses on some targets, since the modulo instruction is
9641 undefined if the quotient can't be represented in the
9642 computation mode. We shorten only if unsigned or if
9643 dividing by something we know != -1. */
9644 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9645 || (TREE_CODE (op1) == INTEGER_CST
9646 && !integer_all_onesp (op1)));
9647 common = 1;
9649 break;
9651 case BIT_AND_EXPR:
9652 case BIT_IOR_EXPR:
9653 case BIT_XOR_EXPR:
9654 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9655 shorten = -1;
9656 /* Allow vector types which are not floating point types. */
9657 else if (code0 == VECTOR_TYPE
9658 && code1 == VECTOR_TYPE
9659 && !VECTOR_FLOAT_TYPE_P (type0)
9660 && !VECTOR_FLOAT_TYPE_P (type1))
9661 common = 1;
9662 break;
9664 case TRUNC_MOD_EXPR:
9665 case FLOOR_MOD_EXPR:
9666 warn_for_div_by_zero (location, op1);
9668 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9669 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9670 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9671 common = 1;
9672 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9674 /* Although it would be tempting to shorten always here, that loses
9675 on some targets, since the modulo instruction is undefined if the
9676 quotient can't be represented in the computation mode. We shorten
9677 only if unsigned or if dividing by something we know != -1. */
9678 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9679 || (TREE_CODE (op1) == INTEGER_CST
9680 && !integer_all_onesp (op1)));
9681 common = 1;
9683 break;
9685 case TRUTH_ANDIF_EXPR:
9686 case TRUTH_ORIF_EXPR:
9687 case TRUTH_AND_EXPR:
9688 case TRUTH_OR_EXPR:
9689 case TRUTH_XOR_EXPR:
9690 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9691 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9692 || code0 == FIXED_POINT_TYPE)
9693 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9694 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9695 || code1 == FIXED_POINT_TYPE))
9697 /* Result of these operations is always an int,
9698 but that does not mean the operands should be
9699 converted to ints! */
9700 result_type = integer_type_node;
9701 op0 = c_common_truthvalue_conversion (location, op0);
9702 op1 = c_common_truthvalue_conversion (location, op1);
9703 converted = 1;
9704 boolean_op = true;
9706 if (code == TRUTH_ANDIF_EXPR)
9708 int_const_or_overflow = (int_operands
9709 && TREE_CODE (orig_op0) == INTEGER_CST
9710 && (op0 == truthvalue_false_node
9711 || TREE_CODE (orig_op1) == INTEGER_CST));
9712 int_const = (int_const_or_overflow
9713 && !TREE_OVERFLOW (orig_op0)
9714 && (op0 == truthvalue_false_node
9715 || !TREE_OVERFLOW (orig_op1)));
9717 else if (code == TRUTH_ORIF_EXPR)
9719 int_const_or_overflow = (int_operands
9720 && TREE_CODE (orig_op0) == INTEGER_CST
9721 && (op0 == truthvalue_true_node
9722 || TREE_CODE (orig_op1) == INTEGER_CST));
9723 int_const = (int_const_or_overflow
9724 && !TREE_OVERFLOW (orig_op0)
9725 && (op0 == truthvalue_true_node
9726 || !TREE_OVERFLOW (orig_op1)));
9728 break;
9730 /* Shift operations: result has same type as first operand;
9731 always convert second operand to int.
9732 Also set SHORT_SHIFT if shifting rightward. */
9734 case RSHIFT_EXPR:
9735 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9736 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9738 result_type = type0;
9739 converted = 1;
9741 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9742 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9743 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9744 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9746 result_type = type0;
9747 converted = 1;
9749 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9750 && code1 == INTEGER_TYPE)
9752 if (TREE_CODE (op1) == INTEGER_CST)
9754 if (tree_int_cst_sgn (op1) < 0)
9756 int_const = false;
9757 if (c_inhibit_evaluation_warnings == 0)
9758 warning (0, "right shift count is negative");
9760 else
9762 if (!integer_zerop (op1))
9763 short_shift = 1;
9765 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9767 int_const = false;
9768 if (c_inhibit_evaluation_warnings == 0)
9769 warning (0, "right shift count >= width of type");
9774 /* Use the type of the value to be shifted. */
9775 result_type = type0;
9776 /* Convert the non vector shift-count to an integer, regardless
9777 of size of value being shifted. */
9778 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9779 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9780 op1 = convert (integer_type_node, op1);
9781 /* Avoid converting op1 to result_type later. */
9782 converted = 1;
9784 break;
9786 case LSHIFT_EXPR:
9787 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9788 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9790 result_type = type0;
9791 converted = 1;
9793 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9794 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9795 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9796 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9798 result_type = type0;
9799 converted = 1;
9801 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9802 && code1 == INTEGER_TYPE)
9804 if (TREE_CODE (op1) == INTEGER_CST)
9806 if (tree_int_cst_sgn (op1) < 0)
9808 int_const = false;
9809 if (c_inhibit_evaluation_warnings == 0)
9810 warning (0, "left shift count is negative");
9813 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9815 int_const = false;
9816 if (c_inhibit_evaluation_warnings == 0)
9817 warning (0, "left shift count >= width of type");
9821 /* Use the type of the value to be shifted. */
9822 result_type = type0;
9823 /* Convert the non vector shift-count to an integer, regardless
9824 of size of value being shifted. */
9825 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9826 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9827 op1 = convert (integer_type_node, op1);
9828 /* Avoid converting op1 to result_type later. */
9829 converted = 1;
9831 break;
9833 case EQ_EXPR:
9834 case NE_EXPR:
9835 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9836 warning_at (location,
9837 OPT_Wfloat_equal,
9838 "comparing floating point with == or != is unsafe");
9839 /* Result of comparison is always int,
9840 but don't convert the args to int! */
9841 build_type = integer_type_node;
9842 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9843 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9844 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9845 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9846 short_compare = 1;
9847 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9849 if (TREE_CODE (op0) == ADDR_EXPR
9850 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9852 if (code == EQ_EXPR)
9853 warning_at (location,
9854 OPT_Waddress,
9855 "the comparison will always evaluate as %<false%> "
9856 "for the address of %qD will never be NULL",
9857 TREE_OPERAND (op0, 0));
9858 else
9859 warning_at (location,
9860 OPT_Waddress,
9861 "the comparison will always evaluate as %<true%> "
9862 "for the address of %qD will never be NULL",
9863 TREE_OPERAND (op0, 0));
9865 result_type = type0;
9867 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9869 if (TREE_CODE (op1) == ADDR_EXPR
9870 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9872 if (code == EQ_EXPR)
9873 warning_at (location,
9874 OPT_Waddress,
9875 "the comparison will always evaluate as %<false%> "
9876 "for the address of %qD will never be NULL",
9877 TREE_OPERAND (op1, 0));
9878 else
9879 warning_at (location,
9880 OPT_Waddress,
9881 "the comparison will always evaluate as %<true%> "
9882 "for the address of %qD will never be NULL",
9883 TREE_OPERAND (op1, 0));
9885 result_type = type1;
9887 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9889 tree tt0 = TREE_TYPE (type0);
9890 tree tt1 = TREE_TYPE (type1);
9891 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
9892 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
9893 addr_space_t as_common = ADDR_SPACE_GENERIC;
9895 /* Anything compares with void *. void * compares with anything.
9896 Otherwise, the targets must be compatible
9897 and both must be object or both incomplete. */
9898 if (comp_target_types (location, type0, type1))
9899 result_type = common_pointer_type (type0, type1);
9900 else if (!addr_space_superset (as0, as1, &as_common))
9902 error_at (location, "comparison of pointers to "
9903 "disjoint address spaces");
9904 return error_mark_node;
9906 else if (VOID_TYPE_P (tt0))
9908 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
9909 pedwarn (location, OPT_pedantic, "ISO C forbids "
9910 "comparison of %<void *%> with function pointer");
9912 else if (VOID_TYPE_P (tt1))
9914 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
9915 pedwarn (location, OPT_pedantic, "ISO C forbids "
9916 "comparison of %<void *%> with function pointer");
9918 else
9919 /* Avoid warning about the volatile ObjC EH puts on decls. */
9920 if (!objc_ok)
9921 pedwarn (location, 0,
9922 "comparison of distinct pointer types lacks a cast");
9924 if (result_type == NULL_TREE)
9926 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9927 result_type = build_pointer_type
9928 (build_qualified_type (void_type_node, qual));
9931 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9933 result_type = type0;
9934 pedwarn (location, 0, "comparison between pointer and integer");
9936 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9938 result_type = type1;
9939 pedwarn (location, 0, "comparison between pointer and integer");
9941 break;
9943 case LE_EXPR:
9944 case GE_EXPR:
9945 case LT_EXPR:
9946 case GT_EXPR:
9947 build_type = integer_type_node;
9948 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9949 || code0 == FIXED_POINT_TYPE)
9950 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9951 || code1 == FIXED_POINT_TYPE))
9952 short_compare = 1;
9953 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9955 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
9956 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
9957 addr_space_t as_common;
9959 if (comp_target_types (location, type0, type1))
9961 result_type = common_pointer_type (type0, type1);
9962 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9963 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9964 pedwarn (location, 0,
9965 "comparison of complete and incomplete pointers");
9966 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9967 pedwarn (location, OPT_pedantic, "ISO C forbids "
9968 "ordered comparisons of pointers to functions");
9969 else if (null_pointer_constant_p (orig_op0)
9970 || null_pointer_constant_p (orig_op1))
9971 warning_at (location, OPT_Wextra,
9972 "ordered comparison of pointer with null pointer");
9975 else if (!addr_space_superset (as0, as1, &as_common))
9977 error_at (location, "comparison of pointers to "
9978 "disjoint address spaces");
9979 return error_mark_node;
9981 else
9983 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9984 result_type = build_pointer_type
9985 (build_qualified_type (void_type_node, qual));
9986 pedwarn (location, 0,
9987 "comparison of distinct pointer types lacks a cast");
9990 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9992 result_type = type0;
9993 if (pedantic)
9994 pedwarn (location, OPT_pedantic,
9995 "ordered comparison of pointer with integer zero");
9996 else if (extra_warnings)
9997 warning_at (location, OPT_Wextra,
9998 "ordered comparison of pointer with integer zero");
10000 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10002 result_type = type1;
10003 if (pedantic)
10004 pedwarn (location, OPT_pedantic,
10005 "ordered comparison of pointer with integer zero");
10006 else if (extra_warnings)
10007 warning_at (location, OPT_Wextra,
10008 "ordered comparison of pointer with integer zero");
10010 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10012 result_type = type0;
10013 pedwarn (location, 0, "comparison between pointer and integer");
10015 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10017 result_type = type1;
10018 pedwarn (location, 0, "comparison between pointer and integer");
10020 break;
10022 default:
10023 gcc_unreachable ();
10026 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10027 return error_mark_node;
10029 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10030 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10031 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10032 TREE_TYPE (type1))))
10034 binary_op_error (location, code, type0, type1);
10035 return error_mark_node;
10038 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10039 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10041 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10042 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10044 bool first_complex = (code0 == COMPLEX_TYPE);
10045 bool second_complex = (code1 == COMPLEX_TYPE);
10046 int none_complex = (!first_complex && !second_complex);
10048 if (shorten || common || short_compare)
10050 result_type = c_common_type (type0, type1);
10051 do_warn_double_promotion (result_type, type0, type1,
10052 "implicit conversion from %qT to %qT "
10053 "to match other operand of binary "
10054 "expression",
10055 location);
10056 if (result_type == error_mark_node)
10057 return error_mark_node;
10060 if (first_complex != second_complex
10061 && (code == PLUS_EXPR
10062 || code == MINUS_EXPR
10063 || code == MULT_EXPR
10064 || (code == TRUNC_DIV_EXPR && first_complex))
10065 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10066 && flag_signed_zeros)
10068 /* An operation on mixed real/complex operands must be
10069 handled specially, but the language-independent code can
10070 more easily optimize the plain complex arithmetic if
10071 -fno-signed-zeros. */
10072 tree real_type = TREE_TYPE (result_type);
10073 tree real, imag;
10074 if (type0 != orig_type0 || type1 != orig_type1)
10076 gcc_assert (may_need_excess_precision && common);
10077 semantic_result_type = c_common_type (orig_type0, orig_type1);
10079 if (first_complex)
10081 if (TREE_TYPE (op0) != result_type)
10082 op0 = convert_and_check (result_type, op0);
10083 if (TREE_TYPE (op1) != real_type)
10084 op1 = convert_and_check (real_type, op1);
10086 else
10088 if (TREE_TYPE (op0) != real_type)
10089 op0 = convert_and_check (real_type, op0);
10090 if (TREE_TYPE (op1) != result_type)
10091 op1 = convert_and_check (result_type, op1);
10093 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10094 return error_mark_node;
10095 if (first_complex)
10097 op0 = c_save_expr (op0);
10098 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10099 op0, 1);
10100 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10101 op0, 1);
10102 switch (code)
10104 case MULT_EXPR:
10105 case TRUNC_DIV_EXPR:
10106 imag = build2 (resultcode, real_type, imag, op1);
10107 /* Fall through. */
10108 case PLUS_EXPR:
10109 case MINUS_EXPR:
10110 real = build2 (resultcode, real_type, real, op1);
10111 break;
10112 default:
10113 gcc_unreachable();
10116 else
10118 op1 = c_save_expr (op1);
10119 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10120 op1, 1);
10121 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10122 op1, 1);
10123 switch (code)
10125 case MULT_EXPR:
10126 imag = build2 (resultcode, real_type, op0, imag);
10127 /* Fall through. */
10128 case PLUS_EXPR:
10129 real = build2 (resultcode, real_type, op0, real);
10130 break;
10131 case MINUS_EXPR:
10132 real = build2 (resultcode, real_type, op0, real);
10133 imag = build1 (NEGATE_EXPR, real_type, imag);
10134 break;
10135 default:
10136 gcc_unreachable();
10139 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10140 goto return_build_binary_op;
10143 /* For certain operations (which identify themselves by shorten != 0)
10144 if both args were extended from the same smaller type,
10145 do the arithmetic in that type and then extend.
10147 shorten !=0 and !=1 indicates a bitwise operation.
10148 For them, this optimization is safe only if
10149 both args are zero-extended or both are sign-extended.
10150 Otherwise, we might change the result.
10151 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10152 but calculated in (unsigned short) it would be (unsigned short)-1. */
10154 if (shorten && none_complex)
10156 final_type = result_type;
10157 result_type = shorten_binary_op (result_type, op0, op1,
10158 shorten == -1);
10161 /* Shifts can be shortened if shifting right. */
10163 if (short_shift)
10165 int unsigned_arg;
10166 tree arg0 = get_narrower (op0, &unsigned_arg);
10168 final_type = result_type;
10170 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10171 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10173 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10174 && tree_int_cst_sgn (op1) > 0
10175 /* We can shorten only if the shift count is less than the
10176 number of bits in the smaller type size. */
10177 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10178 /* We cannot drop an unsigned shift after sign-extension. */
10179 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10181 /* Do an unsigned shift if the operand was zero-extended. */
10182 result_type
10183 = c_common_signed_or_unsigned_type (unsigned_arg,
10184 TREE_TYPE (arg0));
10185 /* Convert value-to-be-shifted to that type. */
10186 if (TREE_TYPE (op0) != result_type)
10187 op0 = convert (result_type, op0);
10188 converted = 1;
10192 /* Comparison operations are shortened too but differently.
10193 They identify themselves by setting short_compare = 1. */
10195 if (short_compare)
10197 /* Don't write &op0, etc., because that would prevent op0
10198 from being kept in a register.
10199 Instead, make copies of the our local variables and
10200 pass the copies by reference, then copy them back afterward. */
10201 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10202 enum tree_code xresultcode = resultcode;
10203 tree val
10204 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10206 if (val != 0)
10208 ret = val;
10209 goto return_build_binary_op;
10212 op0 = xop0, op1 = xop1;
10213 converted = 1;
10214 resultcode = xresultcode;
10216 if (c_inhibit_evaluation_warnings == 0)
10218 bool op0_maybe_const = true;
10219 bool op1_maybe_const = true;
10220 tree orig_op0_folded, orig_op1_folded;
10222 if (in_late_binary_op)
10224 orig_op0_folded = orig_op0;
10225 orig_op1_folded = orig_op1;
10227 else
10229 /* Fold for the sake of possible warnings, as in
10230 build_conditional_expr. This requires the
10231 "original" values to be folded, not just op0 and
10232 op1. */
10233 c_inhibit_evaluation_warnings++;
10234 op0 = c_fully_fold (op0, require_constant_value,
10235 &op0_maybe_const);
10236 op1 = c_fully_fold (op1, require_constant_value,
10237 &op1_maybe_const);
10238 c_inhibit_evaluation_warnings--;
10239 orig_op0_folded = c_fully_fold (orig_op0,
10240 require_constant_value,
10241 NULL);
10242 orig_op1_folded = c_fully_fold (orig_op1,
10243 require_constant_value,
10244 NULL);
10247 if (warn_sign_compare)
10248 warn_for_sign_compare (location, orig_op0_folded,
10249 orig_op1_folded, op0, op1,
10250 result_type, resultcode);
10251 if (!in_late_binary_op)
10253 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10254 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10255 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10256 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10262 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10263 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10264 Then the expression will be built.
10265 It will be given type FINAL_TYPE if that is nonzero;
10266 otherwise, it will be given type RESULT_TYPE. */
10268 if (!result_type)
10270 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10271 return error_mark_node;
10274 if (build_type == NULL_TREE)
10276 build_type = result_type;
10277 if ((type0 != orig_type0 || type1 != orig_type1)
10278 && !boolean_op)
10280 gcc_assert (may_need_excess_precision && common);
10281 semantic_result_type = c_common_type (orig_type0, orig_type1);
10285 if (!converted)
10287 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10288 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10290 /* This can happen if one operand has a vector type, and the other
10291 has a different type. */
10292 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10293 return error_mark_node;
10296 /* Treat expressions in initializers specially as they can't trap. */
10297 if (int_const_or_overflow)
10298 ret = (require_constant_value
10299 ? fold_build2_initializer_loc (location, resultcode, build_type,
10300 op0, op1)
10301 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10302 else
10303 ret = build2 (resultcode, build_type, op0, op1);
10304 if (final_type != 0)
10305 ret = convert (final_type, ret);
10307 return_build_binary_op:
10308 gcc_assert (ret != error_mark_node);
10309 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10310 ret = (int_operands
10311 ? note_integer_operands (ret)
10312 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10313 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10314 && !in_late_binary_op)
10315 ret = note_integer_operands (ret);
10316 if (semantic_result_type)
10317 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10318 protected_set_expr_location (ret, location);
10319 return ret;
10323 /* Convert EXPR to be a truth-value, validating its type for this
10324 purpose. LOCATION is the source location for the expression. */
10326 tree
10327 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10329 bool int_const, int_operands;
10331 switch (TREE_CODE (TREE_TYPE (expr)))
10333 case ARRAY_TYPE:
10334 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10335 return error_mark_node;
10337 case RECORD_TYPE:
10338 error_at (location, "used struct type value where scalar is required");
10339 return error_mark_node;
10341 case UNION_TYPE:
10342 error_at (location, "used union type value where scalar is required");
10343 return error_mark_node;
10345 case FUNCTION_TYPE:
10346 gcc_unreachable ();
10348 default:
10349 break;
10352 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10353 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10354 if (int_operands)
10355 expr = remove_c_maybe_const_expr (expr);
10357 /* ??? Should we also give an error for void and vectors rather than
10358 leaving those to give errors later? */
10359 expr = c_common_truthvalue_conversion (location, expr);
10361 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10363 if (TREE_OVERFLOW (expr))
10364 return expr;
10365 else
10366 return note_integer_operands (expr);
10368 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10369 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10370 return expr;
10374 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10375 required. */
10377 tree
10378 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10380 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10382 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10383 /* Executing a compound literal inside a function reinitializes
10384 it. */
10385 if (!TREE_STATIC (decl))
10386 *se = true;
10387 return decl;
10389 else
10390 return expr;
10393 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10395 tree
10396 c_begin_omp_parallel (void)
10398 tree block;
10400 keep_next_level ();
10401 block = c_begin_compound_stmt (true);
10403 return block;
10406 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10407 statement. LOC is the location of the OMP_PARALLEL. */
10409 tree
10410 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10412 tree stmt;
10414 block = c_end_compound_stmt (loc, block, true);
10416 stmt = make_node (OMP_PARALLEL);
10417 TREE_TYPE (stmt) = void_type_node;
10418 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10419 OMP_PARALLEL_BODY (stmt) = block;
10420 SET_EXPR_LOCATION (stmt, loc);
10422 return add_stmt (stmt);
10425 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10427 tree
10428 c_begin_omp_task (void)
10430 tree block;
10432 keep_next_level ();
10433 block = c_begin_compound_stmt (true);
10435 return block;
10438 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10439 statement. LOC is the location of the #pragma. */
10441 tree
10442 c_finish_omp_task (location_t loc, tree clauses, tree block)
10444 tree stmt;
10446 block = c_end_compound_stmt (loc, block, true);
10448 stmt = make_node (OMP_TASK);
10449 TREE_TYPE (stmt) = void_type_node;
10450 OMP_TASK_CLAUSES (stmt) = clauses;
10451 OMP_TASK_BODY (stmt) = block;
10452 SET_EXPR_LOCATION (stmt, loc);
10454 return add_stmt (stmt);
10457 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10458 Remove any elements from the list that are invalid. */
10460 tree
10461 c_finish_omp_clauses (tree clauses)
10463 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10464 tree c, t, *pc = &clauses;
10465 const char *name;
10467 bitmap_obstack_initialize (NULL);
10468 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10469 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10470 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10472 for (pc = &clauses, c = clauses; c ; c = *pc)
10474 bool remove = false;
10475 bool need_complete = false;
10476 bool need_implicitly_determined = false;
10478 switch (OMP_CLAUSE_CODE (c))
10480 case OMP_CLAUSE_SHARED:
10481 name = "shared";
10482 need_implicitly_determined = true;
10483 goto check_dup_generic;
10485 case OMP_CLAUSE_PRIVATE:
10486 name = "private";
10487 need_complete = true;
10488 need_implicitly_determined = true;
10489 goto check_dup_generic;
10491 case OMP_CLAUSE_REDUCTION:
10492 name = "reduction";
10493 need_implicitly_determined = true;
10494 t = OMP_CLAUSE_DECL (c);
10495 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10496 || POINTER_TYPE_P (TREE_TYPE (t)))
10498 error_at (OMP_CLAUSE_LOCATION (c),
10499 "%qE has invalid type for %<reduction%>", t);
10500 remove = true;
10502 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10504 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10505 const char *r_name = NULL;
10507 switch (r_code)
10509 case PLUS_EXPR:
10510 case MULT_EXPR:
10511 case MINUS_EXPR:
10512 break;
10513 case BIT_AND_EXPR:
10514 r_name = "&";
10515 break;
10516 case BIT_XOR_EXPR:
10517 r_name = "^";
10518 break;
10519 case BIT_IOR_EXPR:
10520 r_name = "|";
10521 break;
10522 case TRUTH_ANDIF_EXPR:
10523 r_name = "&&";
10524 break;
10525 case TRUTH_ORIF_EXPR:
10526 r_name = "||";
10527 break;
10528 default:
10529 gcc_unreachable ();
10531 if (r_name)
10533 error_at (OMP_CLAUSE_LOCATION (c),
10534 "%qE has invalid type for %<reduction(%s)%>",
10535 t, r_name);
10536 remove = true;
10539 goto check_dup_generic;
10541 case OMP_CLAUSE_COPYPRIVATE:
10542 name = "copyprivate";
10543 goto check_dup_generic;
10545 case OMP_CLAUSE_COPYIN:
10546 name = "copyin";
10547 t = OMP_CLAUSE_DECL (c);
10548 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10550 error_at (OMP_CLAUSE_LOCATION (c),
10551 "%qE must be %<threadprivate%> for %<copyin%>", t);
10552 remove = true;
10554 goto check_dup_generic;
10556 check_dup_generic:
10557 t = OMP_CLAUSE_DECL (c);
10558 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10560 error_at (OMP_CLAUSE_LOCATION (c),
10561 "%qE is not a variable in clause %qs", t, name);
10562 remove = true;
10564 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10565 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10566 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10568 error_at (OMP_CLAUSE_LOCATION (c),
10569 "%qE appears more than once in data clauses", t);
10570 remove = true;
10572 else
10573 bitmap_set_bit (&generic_head, DECL_UID (t));
10574 break;
10576 case OMP_CLAUSE_FIRSTPRIVATE:
10577 name = "firstprivate";
10578 t = OMP_CLAUSE_DECL (c);
10579 need_complete = true;
10580 need_implicitly_determined = true;
10581 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10583 error_at (OMP_CLAUSE_LOCATION (c),
10584 "%qE is not a variable in clause %<firstprivate%>", t);
10585 remove = true;
10587 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10588 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10590 error_at (OMP_CLAUSE_LOCATION (c),
10591 "%qE appears more than once in data clauses", t);
10592 remove = true;
10594 else
10595 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10596 break;
10598 case OMP_CLAUSE_LASTPRIVATE:
10599 name = "lastprivate";
10600 t = OMP_CLAUSE_DECL (c);
10601 need_complete = true;
10602 need_implicitly_determined = true;
10603 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10605 error_at (OMP_CLAUSE_LOCATION (c),
10606 "%qE is not a variable in clause %<lastprivate%>", t);
10607 remove = true;
10609 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10610 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10612 error_at (OMP_CLAUSE_LOCATION (c),
10613 "%qE appears more than once in data clauses", t);
10614 remove = true;
10616 else
10617 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10618 break;
10620 case OMP_CLAUSE_IF:
10621 case OMP_CLAUSE_NUM_THREADS:
10622 case OMP_CLAUSE_SCHEDULE:
10623 case OMP_CLAUSE_NOWAIT:
10624 case OMP_CLAUSE_ORDERED:
10625 case OMP_CLAUSE_DEFAULT:
10626 case OMP_CLAUSE_UNTIED:
10627 case OMP_CLAUSE_COLLAPSE:
10628 pc = &OMP_CLAUSE_CHAIN (c);
10629 continue;
10631 default:
10632 gcc_unreachable ();
10635 if (!remove)
10637 t = OMP_CLAUSE_DECL (c);
10639 if (need_complete)
10641 t = require_complete_type (t);
10642 if (t == error_mark_node)
10643 remove = true;
10646 if (need_implicitly_determined)
10648 const char *share_name = NULL;
10650 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10651 share_name = "threadprivate";
10652 else switch (c_omp_predetermined_sharing (t))
10654 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10655 break;
10656 case OMP_CLAUSE_DEFAULT_SHARED:
10657 share_name = "shared";
10658 break;
10659 case OMP_CLAUSE_DEFAULT_PRIVATE:
10660 share_name = "private";
10661 break;
10662 default:
10663 gcc_unreachable ();
10665 if (share_name)
10667 error_at (OMP_CLAUSE_LOCATION (c),
10668 "%qE is predetermined %qs for %qs",
10669 t, share_name, name);
10670 remove = true;
10675 if (remove)
10676 *pc = OMP_CLAUSE_CHAIN (c);
10677 else
10678 pc = &OMP_CLAUSE_CHAIN (c);
10681 bitmap_obstack_release (NULL);
10682 return clauses;
10685 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10686 down to the element type of an array. */
10688 tree
10689 c_build_qualified_type (tree type, int type_quals)
10691 if (type == error_mark_node)
10692 return type;
10694 if (TREE_CODE (type) == ARRAY_TYPE)
10696 tree t;
10697 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10698 type_quals);
10700 /* See if we already have an identically qualified type. */
10701 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10703 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10704 && TYPE_NAME (t) == TYPE_NAME (type)
10705 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10706 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10707 TYPE_ATTRIBUTES (type)))
10708 break;
10710 if (!t)
10712 tree domain = TYPE_DOMAIN (type);
10714 t = build_variant_type_copy (type);
10715 TREE_TYPE (t) = element_type;
10717 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10718 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10719 SET_TYPE_STRUCTURAL_EQUALITY (t);
10720 else if (TYPE_CANONICAL (element_type) != element_type
10721 || (domain && TYPE_CANONICAL (domain) != domain))
10723 tree unqualified_canon
10724 = build_array_type (TYPE_CANONICAL (element_type),
10725 domain? TYPE_CANONICAL (domain)
10726 : NULL_TREE);
10727 TYPE_CANONICAL (t)
10728 = c_build_qualified_type (unqualified_canon, type_quals);
10730 else
10731 TYPE_CANONICAL (t) = t;
10733 return t;
10736 /* A restrict-qualified pointer type must be a pointer to object or
10737 incomplete type. Note that the use of POINTER_TYPE_P also allows
10738 REFERENCE_TYPEs, which is appropriate for C++. */
10739 if ((type_quals & TYPE_QUAL_RESTRICT)
10740 && (!POINTER_TYPE_P (type)
10741 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10743 error ("invalid use of %<restrict%>");
10744 type_quals &= ~TYPE_QUAL_RESTRICT;
10747 return build_qualified_type (type, type_quals);
10750 /* Build a VA_ARG_EXPR for the C parser. */
10752 tree
10753 c_build_va_arg (location_t loc, tree expr, tree type)
10755 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10756 warning_at (loc, OPT_Wc___compat,
10757 "C++ requires promoted type, not enum type, in %<va_arg%>");
10758 return build_va_arg (loc, expr, type);