sh.c (sh_promote_function_mode): Use default_promote_function_mode if !sh_promote_pro...
[official-gcc.git] / gcc / c-typeck.c
blob1476b6d4ffc6d970c7a99a27eb49d8aa69fe22c6
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 if (c_dialect_objc ()
2134 && (ref = objc_build_getter_call (datum, component)))
2135 return ref;
2137 /* See if there is a field or component with name COMPONENT. */
2139 if (code == RECORD_TYPE || code == UNION_TYPE)
2141 if (!COMPLETE_TYPE_P (type))
2143 c_incomplete_type_error (NULL_TREE, type);
2144 return error_mark_node;
2147 field = lookup_field (type, component);
2149 if (!field)
2151 error_at (loc, "%qT has no member named %qE", type, component);
2152 return error_mark_node;
2155 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2156 This might be better solved in future the way the C++ front
2157 end does it - by giving the anonymous entities each a
2158 separate name and type, and then have build_component_ref
2159 recursively call itself. We can't do that here. */
2162 tree subdatum = TREE_VALUE (field);
2163 int quals;
2164 tree subtype;
2165 bool use_datum_quals;
2167 if (TREE_TYPE (subdatum) == error_mark_node)
2168 return error_mark_node;
2170 /* If this is an rvalue, it does not have qualifiers in C
2171 standard terms and we must avoid propagating such
2172 qualifiers down to a non-lvalue array that is then
2173 converted to a pointer. */
2174 use_datum_quals = (datum_lvalue
2175 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2177 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2178 if (use_datum_quals)
2179 quals |= TYPE_QUALS (TREE_TYPE (datum));
2180 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2182 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2183 NULL_TREE);
2184 SET_EXPR_LOCATION (ref, loc);
2185 if (TREE_READONLY (subdatum)
2186 || (use_datum_quals && TREE_READONLY (datum)))
2187 TREE_READONLY (ref) = 1;
2188 if (TREE_THIS_VOLATILE (subdatum)
2189 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2190 TREE_THIS_VOLATILE (ref) = 1;
2192 if (TREE_DEPRECATED (subdatum))
2193 warn_deprecated_use (subdatum, NULL_TREE);
2195 datum = ref;
2197 field = TREE_CHAIN (field);
2199 while (field);
2201 return ref;
2203 else if (code != ERROR_MARK)
2204 error_at (loc,
2205 "request for member %qE in something not a structure or union",
2206 component);
2208 return error_mark_node;
2211 /* Given an expression PTR for a pointer, return an expression
2212 for the value pointed to.
2213 ERRORSTRING is the name of the operator to appear in error messages.
2215 LOC is the location to use for the generated tree. */
2217 tree
2218 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2220 tree pointer = default_conversion (ptr);
2221 tree type = TREE_TYPE (pointer);
2222 tree ref;
2224 if (TREE_CODE (type) == POINTER_TYPE)
2226 if (CONVERT_EXPR_P (pointer)
2227 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2229 /* If a warning is issued, mark it to avoid duplicates from
2230 the backend. This only needs to be done at
2231 warn_strict_aliasing > 2. */
2232 if (warn_strict_aliasing > 2)
2233 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2234 type, TREE_OPERAND (pointer, 0)))
2235 TREE_NO_WARNING (pointer) = 1;
2238 if (TREE_CODE (pointer) == ADDR_EXPR
2239 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2240 == TREE_TYPE (type)))
2242 ref = TREE_OPERAND (pointer, 0);
2243 protected_set_expr_location (ref, loc);
2244 return ref;
2246 else
2248 tree t = TREE_TYPE (type);
2250 ref = build1 (INDIRECT_REF, t, pointer);
2252 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2254 error_at (loc, "dereferencing pointer to incomplete type");
2255 return error_mark_node;
2257 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2258 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2260 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2261 so that we get the proper error message if the result is used
2262 to assign to. Also, &* is supposed to be a no-op.
2263 And ANSI C seems to specify that the type of the result
2264 should be the const type. */
2265 /* A de-reference of a pointer to const is not a const. It is valid
2266 to change it via some other pointer. */
2267 TREE_READONLY (ref) = TYPE_READONLY (t);
2268 TREE_SIDE_EFFECTS (ref)
2269 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2270 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2271 protected_set_expr_location (ref, loc);
2272 return ref;
2275 else if (TREE_CODE (pointer) != ERROR_MARK)
2276 switch (errstring)
2278 case RO_ARRAY_INDEXING:
2279 error_at (loc,
2280 "invalid type argument of array indexing (have %qT)",
2281 type);
2282 break;
2283 case RO_UNARY_STAR:
2284 error_at (loc,
2285 "invalid type argument of unary %<*%> (have %qT)",
2286 type);
2287 break;
2288 case RO_ARROW:
2289 error_at (loc,
2290 "invalid type argument of %<->%> (have %qT)",
2291 type);
2292 break;
2293 default:
2294 gcc_unreachable ();
2296 return error_mark_node;
2299 /* This handles expressions of the form "a[i]", which denotes
2300 an array reference.
2302 This is logically equivalent in C to *(a+i), but we may do it differently.
2303 If A is a variable or a member, we generate a primitive ARRAY_REF.
2304 This avoids forcing the array out of registers, and can work on
2305 arrays that are not lvalues (for example, members of structures returned
2306 by functions).
2308 For vector types, allow vector[i] but not i[vector], and create
2309 *(((type*)&vectortype) + i) for the expression.
2311 LOC is the location to use for the returned expression. */
2313 tree
2314 build_array_ref (location_t loc, tree array, tree index)
2316 tree ret;
2317 bool swapped = false;
2318 if (TREE_TYPE (array) == error_mark_node
2319 || TREE_TYPE (index) == error_mark_node)
2320 return error_mark_node;
2322 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2323 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2324 /* Allow vector[index] but not index[vector]. */
2325 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2327 tree temp;
2328 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2329 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2331 error_at (loc,
2332 "subscripted value is neither array nor pointer nor vector");
2334 return error_mark_node;
2336 temp = array;
2337 array = index;
2338 index = temp;
2339 swapped = true;
2342 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2344 error_at (loc, "array subscript is not an integer");
2345 return error_mark_node;
2348 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2350 error_at (loc, "subscripted value is pointer to function");
2351 return error_mark_node;
2354 /* ??? Existing practice has been to warn only when the char
2355 index is syntactically the index, not for char[array]. */
2356 if (!swapped)
2357 warn_array_subscript_with_type_char (index);
2359 /* Apply default promotions *after* noticing character types. */
2360 index = default_conversion (index);
2362 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2364 /* For vector[index], convert the vector to a
2365 pointer of the underlying type. */
2366 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2368 tree type = TREE_TYPE (array);
2369 tree type1;
2371 if (TREE_CODE (index) == INTEGER_CST)
2372 if (!host_integerp (index, 1)
2373 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2374 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2375 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2377 c_common_mark_addressable_vec (array);
2378 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2379 type = build_pointer_type (type);
2380 type1 = build_pointer_type (TREE_TYPE (array));
2381 array = build1 (ADDR_EXPR, type1, array);
2382 array = convert (type, array);
2385 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2387 tree rval, type;
2389 /* An array that is indexed by a non-constant
2390 cannot be stored in a register; we must be able to do
2391 address arithmetic on its address.
2392 Likewise an array of elements of variable size. */
2393 if (TREE_CODE (index) != INTEGER_CST
2394 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2395 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2397 if (!c_mark_addressable (array))
2398 return error_mark_node;
2400 /* An array that is indexed by a constant value which is not within
2401 the array bounds cannot be stored in a register either; because we
2402 would get a crash in store_bit_field/extract_bit_field when trying
2403 to access a non-existent part of the register. */
2404 if (TREE_CODE (index) == INTEGER_CST
2405 && TYPE_DOMAIN (TREE_TYPE (array))
2406 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2408 if (!c_mark_addressable (array))
2409 return error_mark_node;
2412 if (pedantic)
2414 tree foo = array;
2415 while (TREE_CODE (foo) == COMPONENT_REF)
2416 foo = TREE_OPERAND (foo, 0);
2417 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2418 pedwarn (loc, OPT_pedantic,
2419 "ISO C forbids subscripting %<register%> array");
2420 else if (!flag_isoc99 && !lvalue_p (foo))
2421 pedwarn (loc, OPT_pedantic,
2422 "ISO C90 forbids subscripting non-lvalue array");
2425 type = TREE_TYPE (TREE_TYPE (array));
2426 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2427 /* Array ref is const/volatile if the array elements are
2428 or if the array is. */
2429 TREE_READONLY (rval)
2430 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2431 | TREE_READONLY (array));
2432 TREE_SIDE_EFFECTS (rval)
2433 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2434 | TREE_SIDE_EFFECTS (array));
2435 TREE_THIS_VOLATILE (rval)
2436 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2437 /* This was added by rms on 16 Nov 91.
2438 It fixes vol struct foo *a; a->elts[1]
2439 in an inline function.
2440 Hope it doesn't break something else. */
2441 | TREE_THIS_VOLATILE (array));
2442 ret = require_complete_type (rval);
2443 protected_set_expr_location (ret, loc);
2444 return ret;
2446 else
2448 tree ar = default_conversion (array);
2450 if (ar == error_mark_node)
2451 return ar;
2453 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2454 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2456 return build_indirect_ref
2457 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2458 RO_ARRAY_INDEXING);
2462 /* Build an external reference to identifier ID. FUN indicates
2463 whether this will be used for a function call. LOC is the source
2464 location of the identifier. This sets *TYPE to the type of the
2465 identifier, which is not the same as the type of the returned value
2466 for CONST_DECLs defined as enum constants. If the type of the
2467 identifier is not available, *TYPE is set to NULL. */
2468 tree
2469 build_external_ref (location_t loc, tree id, int fun, tree *type)
2471 tree ref;
2472 tree decl = lookup_name (id);
2474 /* In Objective-C, an instance variable (ivar) may be preferred to
2475 whatever lookup_name() found. */
2476 decl = objc_lookup_ivar (decl, id);
2478 *type = NULL;
2479 if (decl && decl != error_mark_node)
2481 ref = decl;
2482 *type = TREE_TYPE (ref);
2484 else if (fun)
2485 /* Implicit function declaration. */
2486 ref = implicitly_declare (loc, id);
2487 else if (decl == error_mark_node)
2488 /* Don't complain about something that's already been
2489 complained about. */
2490 return error_mark_node;
2491 else
2493 undeclared_variable (loc, id);
2494 return error_mark_node;
2497 if (TREE_TYPE (ref) == error_mark_node)
2498 return error_mark_node;
2500 if (TREE_DEPRECATED (ref))
2501 warn_deprecated_use (ref, NULL_TREE);
2503 /* Recursive call does not count as usage. */
2504 if (ref != current_function_decl)
2506 TREE_USED (ref) = 1;
2509 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2511 if (!in_sizeof && !in_typeof)
2512 C_DECL_USED (ref) = 1;
2513 else if (DECL_INITIAL (ref) == 0
2514 && DECL_EXTERNAL (ref)
2515 && !TREE_PUBLIC (ref))
2516 record_maybe_used_decl (ref);
2519 if (TREE_CODE (ref) == CONST_DECL)
2521 used_types_insert (TREE_TYPE (ref));
2523 if (warn_cxx_compat
2524 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2525 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2527 warning_at (loc, OPT_Wc___compat,
2528 ("enum constant defined in struct or union "
2529 "is not visible in C++"));
2530 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2533 ref = DECL_INITIAL (ref);
2534 TREE_CONSTANT (ref) = 1;
2536 else if (current_function_decl != 0
2537 && !DECL_FILE_SCOPE_P (current_function_decl)
2538 && (TREE_CODE (ref) == VAR_DECL
2539 || TREE_CODE (ref) == PARM_DECL
2540 || TREE_CODE (ref) == FUNCTION_DECL))
2542 tree context = decl_function_context (ref);
2544 if (context != 0 && context != current_function_decl)
2545 DECL_NONLOCAL (ref) = 1;
2547 /* C99 6.7.4p3: An inline definition of a function with external
2548 linkage ... shall not contain a reference to an identifier with
2549 internal linkage. */
2550 else if (current_function_decl != 0
2551 && DECL_DECLARED_INLINE_P (current_function_decl)
2552 && DECL_EXTERNAL (current_function_decl)
2553 && VAR_OR_FUNCTION_DECL_P (ref)
2554 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2555 && ! TREE_PUBLIC (ref)
2556 && DECL_CONTEXT (ref) != current_function_decl)
2557 record_inline_static (loc, current_function_decl, ref,
2558 csi_internal);
2560 return ref;
2563 /* Record details of decls possibly used inside sizeof or typeof. */
2564 struct maybe_used_decl
2566 /* The decl. */
2567 tree decl;
2568 /* The level seen at (in_sizeof + in_typeof). */
2569 int level;
2570 /* The next one at this level or above, or NULL. */
2571 struct maybe_used_decl *next;
2574 static struct maybe_used_decl *maybe_used_decls;
2576 /* Record that DECL, an undefined static function reference seen
2577 inside sizeof or typeof, might be used if the operand of sizeof is
2578 a VLA type or the operand of typeof is a variably modified
2579 type. */
2581 static void
2582 record_maybe_used_decl (tree decl)
2584 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2585 t->decl = decl;
2586 t->level = in_sizeof + in_typeof;
2587 t->next = maybe_used_decls;
2588 maybe_used_decls = t;
2591 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2592 USED is false, just discard them. If it is true, mark them used
2593 (if no longer inside sizeof or typeof) or move them to the next
2594 level up (if still inside sizeof or typeof). */
2596 void
2597 pop_maybe_used (bool used)
2599 struct maybe_used_decl *p = maybe_used_decls;
2600 int cur_level = in_sizeof + in_typeof;
2601 while (p && p->level > cur_level)
2603 if (used)
2605 if (cur_level == 0)
2606 C_DECL_USED (p->decl) = 1;
2607 else
2608 p->level = cur_level;
2610 p = p->next;
2612 if (!used || cur_level == 0)
2613 maybe_used_decls = p;
2616 /* Return the result of sizeof applied to EXPR. */
2618 struct c_expr
2619 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2621 struct c_expr ret;
2622 if (expr.value == error_mark_node)
2624 ret.value = error_mark_node;
2625 ret.original_code = ERROR_MARK;
2626 ret.original_type = NULL;
2627 pop_maybe_used (false);
2629 else
2631 bool expr_const_operands = true;
2632 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2633 &expr_const_operands);
2634 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2635 ret.original_code = ERROR_MARK;
2636 ret.original_type = NULL;
2637 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2639 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2640 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2641 folded_expr, ret.value);
2642 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2643 SET_EXPR_LOCATION (ret.value, loc);
2645 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2647 return ret;
2650 /* Return the result of sizeof applied to T, a structure for the type
2651 name passed to sizeof (rather than the type itself). LOC is the
2652 location of the original expression. */
2654 struct c_expr
2655 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2657 tree type;
2658 struct c_expr ret;
2659 tree type_expr = NULL_TREE;
2660 bool type_expr_const = true;
2661 type = groktypename (t, &type_expr, &type_expr_const);
2662 ret.value = c_sizeof (loc, type);
2663 ret.original_code = ERROR_MARK;
2664 ret.original_type = NULL;
2665 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2666 && c_vla_type_p (type))
2668 /* If the type is a [*] array, it is a VLA but is represented as
2669 having a size of zero. In such a case we must ensure that
2670 the result of sizeof does not get folded to a constant by
2671 c_fully_fold, because if the size is evaluated the result is
2672 not constant and so constraints on zero or negative size
2673 arrays must not be applied when this sizeof call is inside
2674 another array declarator. */
2675 if (!type_expr)
2676 type_expr = integer_zero_node;
2677 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2678 type_expr, ret.value);
2679 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2681 pop_maybe_used (type != error_mark_node
2682 ? C_TYPE_VARIABLE_SIZE (type) : false);
2683 return ret;
2686 /* Build a function call to function FUNCTION with parameters PARAMS.
2687 The function call is at LOC.
2688 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2689 TREE_VALUE of each node is a parameter-expression.
2690 FUNCTION's data type may be a function type or a pointer-to-function. */
2692 tree
2693 build_function_call (location_t loc, tree function, tree params)
2695 VEC(tree,gc) *vec;
2696 tree ret;
2698 vec = VEC_alloc (tree, gc, list_length (params));
2699 for (; params; params = TREE_CHAIN (params))
2700 VEC_quick_push (tree, vec, TREE_VALUE (params));
2701 ret = build_function_call_vec (loc, function, vec, NULL);
2702 VEC_free (tree, gc, vec);
2703 return ret;
2706 /* Build a function call to function FUNCTION with parameters PARAMS.
2707 ORIGTYPES, if not NULL, is a vector of types; each element is
2708 either NULL or the original type of the corresponding element in
2709 PARAMS. The original type may differ from TREE_TYPE of the
2710 parameter for enums. FUNCTION's data type may be a function type
2711 or pointer-to-function. This function changes the elements of
2712 PARAMS. */
2714 tree
2715 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2716 VEC(tree,gc) *origtypes)
2718 tree fntype, fundecl = 0;
2719 tree name = NULL_TREE, result;
2720 tree tem;
2721 int nargs;
2722 tree *argarray;
2725 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2726 STRIP_TYPE_NOPS (function);
2728 /* Convert anything with function type to a pointer-to-function. */
2729 if (TREE_CODE (function) == FUNCTION_DECL)
2731 /* Implement type-directed function overloading for builtins.
2732 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2733 handle all the type checking. The result is a complete expression
2734 that implements this function call. */
2735 tem = resolve_overloaded_builtin (loc, function, params);
2736 if (tem)
2737 return tem;
2739 name = DECL_NAME (function);
2740 fundecl = function;
2742 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2743 function = function_to_pointer_conversion (loc, function);
2745 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2746 expressions, like those used for ObjC messenger dispatches. */
2747 if (!VEC_empty (tree, params))
2748 function = objc_rewrite_function_call (function,
2749 VEC_index (tree, params, 0));
2751 function = c_fully_fold (function, false, NULL);
2753 fntype = TREE_TYPE (function);
2755 if (TREE_CODE (fntype) == ERROR_MARK)
2756 return error_mark_node;
2758 if (!(TREE_CODE (fntype) == POINTER_TYPE
2759 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2761 error_at (loc, "called object %qE is not a function", function);
2762 return error_mark_node;
2765 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2766 current_function_returns_abnormally = 1;
2768 /* fntype now gets the type of function pointed to. */
2769 fntype = TREE_TYPE (fntype);
2771 /* Convert the parameters to the types declared in the
2772 function prototype, or apply default promotions. */
2774 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2775 function, fundecl);
2776 if (nargs < 0)
2777 return error_mark_node;
2779 /* Check that the function is called through a compatible prototype.
2780 If it is not, replace the call by a trap, wrapped up in a compound
2781 expression if necessary. This has the nice side-effect to prevent
2782 the tree-inliner from generating invalid assignment trees which may
2783 blow up in the RTL expander later. */
2784 if (CONVERT_EXPR_P (function)
2785 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2786 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2787 && !comptypes (fntype, TREE_TYPE (tem)))
2789 tree return_type = TREE_TYPE (fntype);
2790 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2791 NULL_TREE);
2792 int i;
2794 /* This situation leads to run-time undefined behavior. We can't,
2795 therefore, simply error unless we can prove that all possible
2796 executions of the program must execute the code. */
2797 if (warning_at (loc, 0, "function called through a non-compatible type"))
2798 /* We can, however, treat "undefined" any way we please.
2799 Call abort to encourage the user to fix the program. */
2800 inform (loc, "if this code is reached, the program will abort");
2801 /* Before the abort, allow the function arguments to exit or
2802 call longjmp. */
2803 for (i = 0; i < nargs; i++)
2804 trap = build2 (COMPOUND_EXPR, void_type_node,
2805 VEC_index (tree, params, i), trap);
2807 if (VOID_TYPE_P (return_type))
2809 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2810 pedwarn (loc, 0,
2811 "function with qualified void return type called");
2812 return trap;
2814 else
2816 tree rhs;
2818 if (AGGREGATE_TYPE_P (return_type))
2819 rhs = build_compound_literal (loc, return_type,
2820 build_constructor (return_type, 0),
2821 false);
2822 else
2823 rhs = fold_convert_loc (loc, return_type, integer_zero_node);
2825 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2826 trap, rhs));
2830 argarray = VEC_address (tree, params);
2832 /* Check that arguments to builtin functions match the expectations. */
2833 if (fundecl
2834 && DECL_BUILT_IN (fundecl)
2835 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2836 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2837 return error_mark_node;
2839 /* Check that the arguments to the function are valid. */
2840 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2841 TYPE_ARG_TYPES (fntype));
2843 if (name != NULL_TREE
2844 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2846 if (require_constant_value)
2847 result =
2848 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2849 function, nargs, argarray);
2850 else
2851 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2852 function, nargs, argarray);
2853 if (TREE_CODE (result) == NOP_EXPR
2854 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2855 STRIP_TYPE_NOPS (result);
2857 else
2858 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2859 function, nargs, argarray);
2861 if (VOID_TYPE_P (TREE_TYPE (result)))
2863 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2864 pedwarn (loc, 0,
2865 "function with qualified void return type called");
2866 return result;
2868 return require_complete_type (result);
2871 /* Convert the argument expressions in the vector VALUES
2872 to the types in the list TYPELIST.
2874 If TYPELIST is exhausted, or when an element has NULL as its type,
2875 perform the default conversions.
2877 ORIGTYPES is the original types of the expressions in VALUES. This
2878 holds the type of enum values which have been converted to integral
2879 types. It may be NULL.
2881 FUNCTION is a tree for the called function. It is used only for
2882 error messages, where it is formatted with %qE.
2884 This is also where warnings about wrong number of args are generated.
2886 Returns the actual number of arguments processed (which may be less
2887 than the length of VALUES in some error situations), or -1 on
2888 failure. */
2890 static int
2891 convert_arguments (tree typelist, VEC(tree,gc) *values,
2892 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2894 tree typetail, val;
2895 unsigned int parmnum;
2896 bool error_args = false;
2897 const bool type_generic = fundecl
2898 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2899 bool type_generic_remove_excess_precision = false;
2900 tree selector;
2902 /* Change pointer to function to the function itself for
2903 diagnostics. */
2904 if (TREE_CODE (function) == ADDR_EXPR
2905 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2906 function = TREE_OPERAND (function, 0);
2908 /* Handle an ObjC selector specially for diagnostics. */
2909 selector = objc_message_selector ();
2911 /* For type-generic built-in functions, determine whether excess
2912 precision should be removed (classification) or not
2913 (comparison). */
2914 if (type_generic
2915 && DECL_BUILT_IN (fundecl)
2916 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2918 switch (DECL_FUNCTION_CODE (fundecl))
2920 case BUILT_IN_ISFINITE:
2921 case BUILT_IN_ISINF:
2922 case BUILT_IN_ISINF_SIGN:
2923 case BUILT_IN_ISNAN:
2924 case BUILT_IN_ISNORMAL:
2925 case BUILT_IN_FPCLASSIFY:
2926 type_generic_remove_excess_precision = true;
2927 break;
2929 default:
2930 type_generic_remove_excess_precision = false;
2931 break;
2935 /* Scan the given expressions and types, producing individual
2936 converted arguments. */
2938 for (typetail = typelist, parmnum = 0;
2939 VEC_iterate (tree, values, parmnum, val);
2940 ++parmnum)
2942 tree type = typetail ? TREE_VALUE (typetail) : 0;
2943 tree valtype = TREE_TYPE (val);
2944 tree rname = function;
2945 int argnum = parmnum + 1;
2946 const char *invalid_func_diag;
2947 bool excess_precision = false;
2948 bool npc;
2949 tree parmval;
2951 if (type == void_type_node)
2953 if (selector)
2954 error_at (input_location,
2955 "too many arguments to method %qE", selector);
2956 else
2957 error_at (input_location,
2958 "too many arguments to function %qE", function);
2960 if (fundecl && !DECL_BUILT_IN (fundecl))
2961 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2962 return parmnum;
2965 if (selector && argnum > 2)
2967 rname = selector;
2968 argnum -= 2;
2971 npc = null_pointer_constant_p (val);
2973 /* If there is excess precision and a prototype, convert once to
2974 the required type rather than converting via the semantic
2975 type. Likewise without a prototype a float value represented
2976 as long double should be converted once to double. But for
2977 type-generic classification functions excess precision must
2978 be removed here. */
2979 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2980 && (type || !type_generic || !type_generic_remove_excess_precision))
2982 val = TREE_OPERAND (val, 0);
2983 excess_precision = true;
2985 val = c_fully_fold (val, false, NULL);
2986 STRIP_TYPE_NOPS (val);
2988 val = require_complete_type (val);
2990 if (type != 0)
2992 /* Formal parm type is specified by a function prototype. */
2994 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2996 error ("type of formal parameter %d is incomplete", parmnum + 1);
2997 parmval = val;
2999 else
3001 tree origtype;
3003 /* Optionally warn about conversions that
3004 differ from the default conversions. */
3005 if (warn_traditional_conversion || warn_traditional)
3007 unsigned int formal_prec = TYPE_PRECISION (type);
3009 if (INTEGRAL_TYPE_P (type)
3010 && TREE_CODE (valtype) == REAL_TYPE)
3011 warning (0, "passing argument %d of %qE as integer "
3012 "rather than floating due to prototype",
3013 argnum, rname);
3014 if (INTEGRAL_TYPE_P (type)
3015 && TREE_CODE (valtype) == COMPLEX_TYPE)
3016 warning (0, "passing argument %d of %qE as integer "
3017 "rather than complex due to prototype",
3018 argnum, rname);
3019 else if (TREE_CODE (type) == COMPLEX_TYPE
3020 && TREE_CODE (valtype) == REAL_TYPE)
3021 warning (0, "passing argument %d of %qE as complex "
3022 "rather than floating due to prototype",
3023 argnum, rname);
3024 else if (TREE_CODE (type) == REAL_TYPE
3025 && INTEGRAL_TYPE_P (valtype))
3026 warning (0, "passing argument %d of %qE as floating "
3027 "rather than integer due to prototype",
3028 argnum, rname);
3029 else if (TREE_CODE (type) == COMPLEX_TYPE
3030 && INTEGRAL_TYPE_P (valtype))
3031 warning (0, "passing argument %d of %qE as complex "
3032 "rather than integer due to prototype",
3033 argnum, rname);
3034 else if (TREE_CODE (type) == REAL_TYPE
3035 && TREE_CODE (valtype) == COMPLEX_TYPE)
3036 warning (0, "passing argument %d of %qE as floating "
3037 "rather than complex due to prototype",
3038 argnum, rname);
3039 /* ??? At some point, messages should be written about
3040 conversions between complex types, but that's too messy
3041 to do now. */
3042 else if (TREE_CODE (type) == REAL_TYPE
3043 && TREE_CODE (valtype) == REAL_TYPE)
3045 /* Warn if any argument is passed as `float',
3046 since without a prototype it would be `double'. */
3047 if (formal_prec == TYPE_PRECISION (float_type_node)
3048 && type != dfloat32_type_node)
3049 warning (0, "passing argument %d of %qE as %<float%> "
3050 "rather than %<double%> due to prototype",
3051 argnum, rname);
3053 /* Warn if mismatch between argument and prototype
3054 for decimal float types. Warn of conversions with
3055 binary float types and of precision narrowing due to
3056 prototype. */
3057 else if (type != valtype
3058 && (type == dfloat32_type_node
3059 || type == dfloat64_type_node
3060 || type == dfloat128_type_node
3061 || valtype == dfloat32_type_node
3062 || valtype == dfloat64_type_node
3063 || valtype == dfloat128_type_node)
3064 && (formal_prec
3065 <= TYPE_PRECISION (valtype)
3066 || (type == dfloat128_type_node
3067 && (valtype
3068 != dfloat64_type_node
3069 && (valtype
3070 != dfloat32_type_node)))
3071 || (type == dfloat64_type_node
3072 && (valtype
3073 != dfloat32_type_node))))
3074 warning (0, "passing argument %d of %qE as %qT "
3075 "rather than %qT due to prototype",
3076 argnum, rname, type, valtype);
3079 /* Detect integer changing in width or signedness.
3080 These warnings are only activated with
3081 -Wtraditional-conversion, not with -Wtraditional. */
3082 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3083 && INTEGRAL_TYPE_P (valtype))
3085 tree would_have_been = default_conversion (val);
3086 tree type1 = TREE_TYPE (would_have_been);
3088 if (TREE_CODE (type) == ENUMERAL_TYPE
3089 && (TYPE_MAIN_VARIANT (type)
3090 == TYPE_MAIN_VARIANT (valtype)))
3091 /* No warning if function asks for enum
3092 and the actual arg is that enum type. */
3094 else if (formal_prec != TYPE_PRECISION (type1))
3095 warning (OPT_Wtraditional_conversion,
3096 "passing argument %d of %qE "
3097 "with different width due to prototype",
3098 argnum, rname);
3099 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3101 /* Don't complain if the formal parameter type
3102 is an enum, because we can't tell now whether
3103 the value was an enum--even the same enum. */
3104 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3106 else if (TREE_CODE (val) == INTEGER_CST
3107 && int_fits_type_p (val, type))
3108 /* Change in signedness doesn't matter
3109 if a constant value is unaffected. */
3111 /* If the value is extended from a narrower
3112 unsigned type, it doesn't matter whether we
3113 pass it as signed or unsigned; the value
3114 certainly is the same either way. */
3115 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3116 && TYPE_UNSIGNED (valtype))
3118 else if (TYPE_UNSIGNED (type))
3119 warning (OPT_Wtraditional_conversion,
3120 "passing argument %d of %qE "
3121 "as unsigned due to prototype",
3122 argnum, rname);
3123 else
3124 warning (OPT_Wtraditional_conversion,
3125 "passing argument %d of %qE "
3126 "as signed due to prototype", argnum, rname);
3130 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3131 sake of better warnings from convert_and_check. */
3132 if (excess_precision)
3133 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3134 origtype = (origtypes == NULL
3135 ? NULL_TREE
3136 : VEC_index (tree, origtypes, parmnum));
3137 parmval = convert_for_assignment (input_location, type, val,
3138 origtype, ic_argpass, npc,
3139 fundecl, function,
3140 parmnum + 1);
3142 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3143 && INTEGRAL_TYPE_P (type)
3144 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3145 parmval = default_conversion (parmval);
3148 else if (TREE_CODE (valtype) == REAL_TYPE
3149 && (TYPE_PRECISION (valtype)
3150 < TYPE_PRECISION (double_type_node))
3151 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3153 if (type_generic)
3154 parmval = val;
3155 else
3157 /* Convert `float' to `double'. */
3158 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3159 warning (OPT_Wdouble_promotion,
3160 "implicit conversion from %qT to %qT when passing "
3161 "argument to function",
3162 valtype, double_type_node);
3163 parmval = convert (double_type_node, val);
3166 else if (excess_precision && !type_generic)
3167 /* A "double" argument with excess precision being passed
3168 without a prototype or in variable arguments. */
3169 parmval = convert (valtype, val);
3170 else if ((invalid_func_diag =
3171 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3173 error (invalid_func_diag);
3174 return -1;
3176 else
3177 /* Convert `short' and `char' to full-size `int'. */
3178 parmval = default_conversion (val);
3180 VEC_replace (tree, values, parmnum, parmval);
3181 if (parmval == error_mark_node)
3182 error_args = true;
3184 if (typetail)
3185 typetail = TREE_CHAIN (typetail);
3188 gcc_assert (parmnum == VEC_length (tree, values));
3190 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3192 error_at (input_location,
3193 "too few arguments to function %qE", function);
3194 if (fundecl && !DECL_BUILT_IN (fundecl))
3195 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3196 return -1;
3199 return error_args ? -1 : (int) parmnum;
3202 /* This is the entry point used by the parser to build unary operators
3203 in the input. CODE, a tree_code, specifies the unary operator, and
3204 ARG is the operand. For unary plus, the C parser currently uses
3205 CONVERT_EXPR for code.
3207 LOC is the location to use for the tree generated.
3210 struct c_expr
3211 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3213 struct c_expr result;
3215 result.value = build_unary_op (loc, code, arg.value, 0);
3216 result.original_code = code;
3217 result.original_type = NULL;
3219 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3220 overflow_warning (loc, result.value);
3222 return result;
3225 /* This is the entry point used by the parser to build binary operators
3226 in the input. CODE, a tree_code, specifies the binary operator, and
3227 ARG1 and ARG2 are the operands. In addition to constructing the
3228 expression, we check for operands that were written with other binary
3229 operators in a way that is likely to confuse the user.
3231 LOCATION is the location of the binary operator. */
3233 struct c_expr
3234 parser_build_binary_op (location_t location, enum tree_code code,
3235 struct c_expr arg1, struct c_expr arg2)
3237 struct c_expr result;
3239 enum tree_code code1 = arg1.original_code;
3240 enum tree_code code2 = arg2.original_code;
3241 tree type1 = (arg1.original_type
3242 ? arg1.original_type
3243 : TREE_TYPE (arg1.value));
3244 tree type2 = (arg2.original_type
3245 ? arg2.original_type
3246 : TREE_TYPE (arg2.value));
3248 result.value = build_binary_op (location, code,
3249 arg1.value, arg2.value, 1);
3250 result.original_code = code;
3251 result.original_type = NULL;
3253 if (TREE_CODE (result.value) == ERROR_MARK)
3254 return result;
3256 if (location != UNKNOWN_LOCATION)
3257 protected_set_expr_location (result.value, location);
3259 /* Check for cases such as x+y<<z which users are likely
3260 to misinterpret. */
3261 if (warn_parentheses)
3262 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3264 if (warn_logical_op)
3265 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3266 code1, arg1.value, code2, arg2.value);
3268 /* Warn about comparisons against string literals, with the exception
3269 of testing for equality or inequality of a string literal with NULL. */
3270 if (code == EQ_EXPR || code == NE_EXPR)
3272 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3273 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3274 warning_at (location, OPT_Waddress,
3275 "comparison with string literal results in unspecified behavior");
3277 else if (TREE_CODE_CLASS (code) == tcc_comparison
3278 && (code1 == STRING_CST || code2 == STRING_CST))
3279 warning_at (location, OPT_Waddress,
3280 "comparison with string literal results in unspecified behavior");
3282 if (TREE_OVERFLOW_P (result.value)
3283 && !TREE_OVERFLOW_P (arg1.value)
3284 && !TREE_OVERFLOW_P (arg2.value))
3285 overflow_warning (location, result.value);
3287 /* Warn about comparisons of different enum types. */
3288 if (warn_enum_compare
3289 && TREE_CODE_CLASS (code) == tcc_comparison
3290 && TREE_CODE (type1) == ENUMERAL_TYPE
3291 && TREE_CODE (type2) == ENUMERAL_TYPE
3292 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3293 warning_at (location, OPT_Wenum_compare,
3294 "comparison between %qT and %qT",
3295 type1, type2);
3297 return result;
3300 /* Return a tree for the difference of pointers OP0 and OP1.
3301 The resulting tree has type int. */
3303 static tree
3304 pointer_diff (location_t loc, tree op0, tree op1)
3306 tree restype = ptrdiff_type_node;
3307 tree result, inttype;
3309 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3310 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3311 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3312 tree con0, con1, lit0, lit1;
3313 tree orig_op1 = op1;
3315 /* If the operands point into different address spaces, we need to
3316 explicitly convert them to pointers into the common address space
3317 before we can subtract the numerical address values. */
3318 if (as0 != as1)
3320 addr_space_t as_common;
3321 tree common_type;
3323 /* Determine the common superset address space. This is guaranteed
3324 to exist because the caller verified that comp_target_types
3325 returned non-zero. */
3326 if (!addr_space_superset (as0, as1, &as_common))
3327 gcc_unreachable ();
3329 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3330 op0 = convert (common_type, op0);
3331 op1 = convert (common_type, op1);
3334 /* Determine integer type to perform computations in. This will usually
3335 be the same as the result type (ptrdiff_t), but may need to be a wider
3336 type if pointers for the address space are wider than ptrdiff_t. */
3337 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3338 inttype = lang_hooks.types.type_for_size
3339 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3340 else
3341 inttype = restype;
3344 if (TREE_CODE (target_type) == VOID_TYPE)
3345 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3346 "pointer of type %<void *%> used in subtraction");
3347 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3348 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3349 "pointer to a function used in subtraction");
3351 /* If the conversion to ptrdiff_type does anything like widening or
3352 converting a partial to an integral mode, we get a convert_expression
3353 that is in the way to do any simplifications.
3354 (fold-const.c doesn't know that the extra bits won't be needed.
3355 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3356 different mode in place.)
3357 So first try to find a common term here 'by hand'; we want to cover
3358 at least the cases that occur in legal static initializers. */
3359 if (CONVERT_EXPR_P (op0)
3360 && (TYPE_PRECISION (TREE_TYPE (op0))
3361 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3362 con0 = TREE_OPERAND (op0, 0);
3363 else
3364 con0 = op0;
3365 if (CONVERT_EXPR_P (op1)
3366 && (TYPE_PRECISION (TREE_TYPE (op1))
3367 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3368 con1 = TREE_OPERAND (op1, 0);
3369 else
3370 con1 = op1;
3372 if (TREE_CODE (con0) == PLUS_EXPR)
3374 lit0 = TREE_OPERAND (con0, 1);
3375 con0 = TREE_OPERAND (con0, 0);
3377 else
3378 lit0 = integer_zero_node;
3380 if (TREE_CODE (con1) == PLUS_EXPR)
3382 lit1 = TREE_OPERAND (con1, 1);
3383 con1 = TREE_OPERAND (con1, 0);
3385 else
3386 lit1 = integer_zero_node;
3388 if (operand_equal_p (con0, con1, 0))
3390 op0 = lit0;
3391 op1 = lit1;
3395 /* First do the subtraction as integers;
3396 then drop through to build the divide operator.
3397 Do not do default conversions on the minus operator
3398 in case restype is a short type. */
3400 op0 = build_binary_op (loc,
3401 MINUS_EXPR, convert (inttype, op0),
3402 convert (inttype, op1), 0);
3403 /* This generates an error if op1 is pointer to incomplete type. */
3404 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3405 error_at (loc, "arithmetic on pointer to an incomplete type");
3407 /* This generates an error if op0 is pointer to incomplete type. */
3408 op1 = c_size_in_bytes (target_type);
3410 /* Divide by the size, in easiest possible way. */
3411 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3412 op0, convert (inttype, op1));
3414 /* Convert to final result type if necessary. */
3415 return convert (restype, result);
3418 /* Construct and perhaps optimize a tree representation
3419 for a unary operation. CODE, a tree_code, specifies the operation
3420 and XARG is the operand.
3421 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3422 the default promotions (such as from short to int).
3423 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3424 allows non-lvalues; this is only used to handle conversion of non-lvalue
3425 arrays to pointers in C99.
3427 LOCATION is the location of the operator. */
3429 tree
3430 build_unary_op (location_t location,
3431 enum tree_code code, tree xarg, int flag)
3433 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3434 tree arg = xarg;
3435 tree argtype = 0;
3436 enum tree_code typecode;
3437 tree val;
3438 tree ret = error_mark_node;
3439 tree eptype = NULL_TREE;
3440 int noconvert = flag;
3441 const char *invalid_op_diag;
3442 bool int_operands;
3444 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3445 if (int_operands)
3446 arg = remove_c_maybe_const_expr (arg);
3448 if (code != ADDR_EXPR)
3449 arg = require_complete_type (arg);
3451 typecode = TREE_CODE (TREE_TYPE (arg));
3452 if (typecode == ERROR_MARK)
3453 return error_mark_node;
3454 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3455 typecode = INTEGER_TYPE;
3457 if ((invalid_op_diag
3458 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3460 error_at (location, invalid_op_diag);
3461 return error_mark_node;
3464 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3466 eptype = TREE_TYPE (arg);
3467 arg = TREE_OPERAND (arg, 0);
3470 switch (code)
3472 case CONVERT_EXPR:
3473 /* This is used for unary plus, because a CONVERT_EXPR
3474 is enough to prevent anybody from looking inside for
3475 associativity, but won't generate any code. */
3476 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3477 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3478 || typecode == VECTOR_TYPE))
3480 error_at (location, "wrong type argument to unary plus");
3481 return error_mark_node;
3483 else if (!noconvert)
3484 arg = default_conversion (arg);
3485 arg = non_lvalue_loc (location, arg);
3486 break;
3488 case NEGATE_EXPR:
3489 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3490 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3491 || typecode == VECTOR_TYPE))
3493 error_at (location, "wrong type argument to unary minus");
3494 return error_mark_node;
3496 else if (!noconvert)
3497 arg = default_conversion (arg);
3498 break;
3500 case BIT_NOT_EXPR:
3501 /* ~ works on integer types and non float vectors. */
3502 if (typecode == INTEGER_TYPE
3503 || (typecode == VECTOR_TYPE
3504 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3506 if (!noconvert)
3507 arg = default_conversion (arg);
3509 else if (typecode == COMPLEX_TYPE)
3511 code = CONJ_EXPR;
3512 pedwarn (location, OPT_pedantic,
3513 "ISO C does not support %<~%> for complex conjugation");
3514 if (!noconvert)
3515 arg = default_conversion (arg);
3517 else
3519 error_at (location, "wrong type argument to bit-complement");
3520 return error_mark_node;
3522 break;
3524 case ABS_EXPR:
3525 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3527 error_at (location, "wrong type argument to abs");
3528 return error_mark_node;
3530 else if (!noconvert)
3531 arg = default_conversion (arg);
3532 break;
3534 case CONJ_EXPR:
3535 /* Conjugating a real value is a no-op, but allow it anyway. */
3536 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3537 || typecode == COMPLEX_TYPE))
3539 error_at (location, "wrong type argument to conjugation");
3540 return error_mark_node;
3542 else if (!noconvert)
3543 arg = default_conversion (arg);
3544 break;
3546 case TRUTH_NOT_EXPR:
3547 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3548 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3549 && typecode != COMPLEX_TYPE)
3551 error_at (location,
3552 "wrong type argument to unary exclamation mark");
3553 return error_mark_node;
3555 arg = c_objc_common_truthvalue_conversion (location, arg);
3556 ret = invert_truthvalue_loc (location, arg);
3557 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3558 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3559 location = EXPR_LOCATION (ret);
3560 goto return_build_unary_op;
3562 case REALPART_EXPR:
3563 if (TREE_CODE (arg) == COMPLEX_CST)
3564 ret = TREE_REALPART (arg);
3565 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3566 ret = fold_build1_loc (location,
3567 REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3568 else
3569 ret = arg;
3570 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3571 eptype = TREE_TYPE (eptype);
3572 goto return_build_unary_op;
3574 case IMAGPART_EXPR:
3575 if (TREE_CODE (arg) == COMPLEX_CST)
3576 ret = TREE_IMAGPART (arg);
3577 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3578 ret = fold_build1_loc (location,
3579 IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3580 else
3581 ret = omit_one_operand_loc (location, TREE_TYPE (arg),
3582 integer_zero_node, arg);
3583 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3584 eptype = TREE_TYPE (eptype);
3585 goto return_build_unary_op;
3587 case PREINCREMENT_EXPR:
3588 case POSTINCREMENT_EXPR:
3589 case PREDECREMENT_EXPR:
3590 case POSTDECREMENT_EXPR:
3592 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3594 tree inner = build_unary_op (location, code,
3595 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3596 if (inner == error_mark_node)
3597 return error_mark_node;
3598 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3599 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3600 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3601 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3602 goto return_build_unary_op;
3605 /* Complain about anything that is not a true lvalue. */
3606 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3607 || code == POSTINCREMENT_EXPR)
3608 ? lv_increment
3609 : lv_decrement)))
3610 return error_mark_node;
3612 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3614 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3615 warning_at (location, OPT_Wc___compat,
3616 "increment of enumeration value is invalid in C++");
3617 else
3618 warning_at (location, OPT_Wc___compat,
3619 "decrement of enumeration value is invalid in C++");
3622 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3623 arg = c_fully_fold (arg, false, NULL);
3625 /* Increment or decrement the real part of the value,
3626 and don't change the imaginary part. */
3627 if (typecode == COMPLEX_TYPE)
3629 tree real, imag;
3631 pedwarn (location, OPT_pedantic,
3632 "ISO C does not support %<++%> and %<--%> on complex types");
3634 arg = stabilize_reference (arg);
3635 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3636 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3637 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3638 if (real == error_mark_node || imag == error_mark_node)
3639 return error_mark_node;
3640 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3641 real, imag);
3642 goto return_build_unary_op;
3645 /* Report invalid types. */
3647 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3648 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3650 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3651 error_at (location, "wrong type argument to increment");
3652 else
3653 error_at (location, "wrong type argument to decrement");
3655 return error_mark_node;
3659 tree inc;
3661 argtype = TREE_TYPE (arg);
3663 /* Compute the increment. */
3665 if (typecode == POINTER_TYPE)
3667 /* If pointer target is an undefined struct,
3668 we just cannot know how to do the arithmetic. */
3669 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3671 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3672 error_at (location,
3673 "increment of pointer to unknown structure");
3674 else
3675 error_at (location,
3676 "decrement of pointer to unknown structure");
3678 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3679 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3681 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3682 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3683 "wrong type argument to increment");
3684 else
3685 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3686 "wrong type argument to decrement");
3689 inc = c_size_in_bytes (TREE_TYPE (argtype));
3690 inc = fold_convert_loc (location, sizetype, inc);
3692 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3694 /* For signed fract types, we invert ++ to -- or
3695 -- to ++, and change inc from 1 to -1, because
3696 it is not possible to represent 1 in signed fract constants.
3697 For unsigned fract types, the result always overflows and
3698 we get an undefined (original) or the maximum value. */
3699 if (code == PREINCREMENT_EXPR)
3700 code = PREDECREMENT_EXPR;
3701 else if (code == PREDECREMENT_EXPR)
3702 code = PREINCREMENT_EXPR;
3703 else if (code == POSTINCREMENT_EXPR)
3704 code = POSTDECREMENT_EXPR;
3705 else /* code == POSTDECREMENT_EXPR */
3706 code = POSTINCREMENT_EXPR;
3708 inc = integer_minus_one_node;
3709 inc = convert (argtype, inc);
3711 else
3713 inc = integer_one_node;
3714 inc = convert (argtype, inc);
3717 /* Report a read-only lvalue. */
3718 if (TYPE_READONLY (argtype))
3720 readonly_error (arg,
3721 ((code == PREINCREMENT_EXPR
3722 || code == POSTINCREMENT_EXPR)
3723 ? lv_increment : lv_decrement));
3724 return error_mark_node;
3726 else if (TREE_READONLY (arg))
3727 readonly_warning (arg,
3728 ((code == PREINCREMENT_EXPR
3729 || code == POSTINCREMENT_EXPR)
3730 ? lv_increment : lv_decrement));
3732 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3733 val = boolean_increment (code, arg);
3734 else
3735 val = build2 (code, TREE_TYPE (arg), arg, inc);
3736 TREE_SIDE_EFFECTS (val) = 1;
3737 if (TREE_CODE (val) != code)
3738 TREE_NO_WARNING (val) = 1;
3739 ret = val;
3740 goto return_build_unary_op;
3743 case ADDR_EXPR:
3744 /* Note that this operation never does default_conversion. */
3746 /* The operand of unary '&' must be an lvalue (which excludes
3747 expressions of type void), or, in C99, the result of a [] or
3748 unary '*' operator. */
3749 if (VOID_TYPE_P (TREE_TYPE (arg))
3750 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3751 && (TREE_CODE (arg) != INDIRECT_REF
3752 || !flag_isoc99))
3753 pedwarn (location, 0, "taking address of expression of type %<void%>");
3755 /* Let &* cancel out to simplify resulting code. */
3756 if (TREE_CODE (arg) == INDIRECT_REF)
3758 /* Don't let this be an lvalue. */
3759 if (lvalue_p (TREE_OPERAND (arg, 0)))
3760 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3761 ret = TREE_OPERAND (arg, 0);
3762 goto return_build_unary_op;
3765 /* For &x[y], return x+y */
3766 if (TREE_CODE (arg) == ARRAY_REF)
3768 tree op0 = TREE_OPERAND (arg, 0);
3769 if (!c_mark_addressable (op0))
3770 return error_mark_node;
3771 return build_binary_op (location, PLUS_EXPR,
3772 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3773 ? array_to_pointer_conversion (location,
3774 op0)
3775 : op0),
3776 TREE_OPERAND (arg, 1), 1);
3779 /* Anything not already handled and not a true memory reference
3780 or a non-lvalue array is an error. */
3781 else if (typecode != FUNCTION_TYPE && !flag
3782 && !lvalue_or_else (arg, lv_addressof))
3783 return error_mark_node;
3785 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3786 folding later. */
3787 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3789 tree inner = build_unary_op (location, code,
3790 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3791 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3792 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3793 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3794 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3795 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3796 goto return_build_unary_op;
3799 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3800 argtype = TREE_TYPE (arg);
3802 /* If the lvalue is const or volatile, merge that into the type
3803 to which the address will point. This should only be needed
3804 for function types. */
3805 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3806 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3808 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3809 int quals = orig_quals;
3811 if (TREE_READONLY (arg))
3812 quals |= TYPE_QUAL_CONST;
3813 if (TREE_THIS_VOLATILE (arg))
3814 quals |= TYPE_QUAL_VOLATILE;
3816 gcc_assert (quals == orig_quals
3817 || TREE_CODE (argtype) == FUNCTION_TYPE);
3819 argtype = c_build_qualified_type (argtype, quals);
3822 if (!c_mark_addressable (arg))
3823 return error_mark_node;
3825 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3826 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3828 argtype = build_pointer_type (argtype);
3830 /* ??? Cope with user tricks that amount to offsetof. Delete this
3831 when we have proper support for integer constant expressions. */
3832 val = get_base_address (arg);
3833 if (val && TREE_CODE (val) == INDIRECT_REF
3834 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3836 tree op0 = fold_convert_loc (location, sizetype,
3837 fold_offsetof (arg, val)), op1;
3839 op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3840 ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3841 goto return_build_unary_op;
3844 val = build1 (ADDR_EXPR, argtype, arg);
3846 ret = val;
3847 goto return_build_unary_op;
3849 default:
3850 gcc_unreachable ();
3853 if (argtype == 0)
3854 argtype = TREE_TYPE (arg);
3855 if (TREE_CODE (arg) == INTEGER_CST)
3856 ret = (require_constant_value
3857 ? fold_build1_initializer_loc (location, code, argtype, arg)
3858 : fold_build1_loc (location, code, argtype, arg));
3859 else
3860 ret = build1 (code, argtype, arg);
3861 return_build_unary_op:
3862 gcc_assert (ret != error_mark_node);
3863 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3864 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3865 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3866 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3867 ret = note_integer_operands (ret);
3868 if (eptype)
3869 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3870 protected_set_expr_location (ret, location);
3871 return ret;
3874 /* Return nonzero if REF is an lvalue valid for this language.
3875 Lvalues can be assigned, unless their type has TYPE_READONLY.
3876 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3878 bool
3879 lvalue_p (const_tree ref)
3881 const enum tree_code code = TREE_CODE (ref);
3883 switch (code)
3885 case REALPART_EXPR:
3886 case IMAGPART_EXPR:
3887 case COMPONENT_REF:
3888 return lvalue_p (TREE_OPERAND (ref, 0));
3890 case C_MAYBE_CONST_EXPR:
3891 return lvalue_p (TREE_OPERAND (ref, 1));
3893 case COMPOUND_LITERAL_EXPR:
3894 case STRING_CST:
3895 return 1;
3897 case INDIRECT_REF:
3898 case ARRAY_REF:
3899 case VAR_DECL:
3900 case PARM_DECL:
3901 case RESULT_DECL:
3902 case ERROR_MARK:
3903 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3904 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3906 case BIND_EXPR:
3907 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3909 default:
3910 return 0;
3914 /* Give an error for storing in something that is 'const'. */
3916 static void
3917 readonly_error (tree arg, enum lvalue_use use)
3919 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3920 || use == lv_asm);
3921 /* Using this macro rather than (for example) arrays of messages
3922 ensures that all the format strings are checked at compile
3923 time. */
3924 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3925 : (use == lv_increment ? (I) \
3926 : (use == lv_decrement ? (D) : (AS))))
3927 if (TREE_CODE (arg) == COMPONENT_REF)
3929 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3930 readonly_error (TREE_OPERAND (arg, 0), use);
3931 else
3932 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3933 G_("increment of read-only member %qD"),
3934 G_("decrement of read-only member %qD"),
3935 G_("read-only member %qD used as %<asm%> output")),
3936 TREE_OPERAND (arg, 1));
3938 else if (TREE_CODE (arg) == VAR_DECL)
3939 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3940 G_("increment of read-only variable %qD"),
3941 G_("decrement of read-only variable %qD"),
3942 G_("read-only variable %qD used as %<asm%> output")),
3943 arg);
3944 else
3945 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3946 G_("increment of read-only location %qE"),
3947 G_("decrement of read-only location %qE"),
3948 G_("read-only location %qE used as %<asm%> output")),
3949 arg);
3952 /* Give a warning for storing in something that is read-only in GCC
3953 terms but not const in ISO C terms. */
3955 static void
3956 readonly_warning (tree arg, enum lvalue_use use)
3958 switch (use)
3960 case lv_assign:
3961 warning (0, "assignment of read-only location %qE", arg);
3962 break;
3963 case lv_increment:
3964 warning (0, "increment of read-only location %qE", arg);
3965 break;
3966 case lv_decrement:
3967 warning (0, "decrement of read-only location %qE", arg);
3968 break;
3969 default:
3970 gcc_unreachable ();
3972 return;
3976 /* Return nonzero if REF is an lvalue valid for this language;
3977 otherwise, print an error message and return zero. USE says
3978 how the lvalue is being used and so selects the error message. */
3980 static int
3981 lvalue_or_else (const_tree ref, enum lvalue_use use)
3983 int win = lvalue_p (ref);
3985 if (!win)
3986 lvalue_error (use);
3988 return win;
3991 /* Mark EXP saying that we need to be able to take the
3992 address of it; it should not be allocated in a register.
3993 Returns true if successful. */
3995 bool
3996 c_mark_addressable (tree exp)
3998 tree x = exp;
4000 while (1)
4001 switch (TREE_CODE (x))
4003 case COMPONENT_REF:
4004 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4006 error
4007 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4008 return false;
4011 /* ... fall through ... */
4013 case ADDR_EXPR:
4014 case ARRAY_REF:
4015 case REALPART_EXPR:
4016 case IMAGPART_EXPR:
4017 x = TREE_OPERAND (x, 0);
4018 break;
4020 case COMPOUND_LITERAL_EXPR:
4021 case CONSTRUCTOR:
4022 TREE_ADDRESSABLE (x) = 1;
4023 return true;
4025 case VAR_DECL:
4026 case CONST_DECL:
4027 case PARM_DECL:
4028 case RESULT_DECL:
4029 if (C_DECL_REGISTER (x)
4030 && DECL_NONLOCAL (x))
4032 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4034 error
4035 ("global register variable %qD used in nested function", x);
4036 return false;
4038 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4040 else if (C_DECL_REGISTER (x))
4042 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4043 error ("address of global register variable %qD requested", x);
4044 else
4045 error ("address of register variable %qD requested", x);
4046 return false;
4049 /* drops in */
4050 case FUNCTION_DECL:
4051 TREE_ADDRESSABLE (x) = 1;
4052 /* drops out */
4053 default:
4054 return true;
4058 /* Convert EXPR to TYPE, warning about conversion problems with
4059 constants. SEMANTIC_TYPE is the type this conversion would use
4060 without excess precision. If SEMANTIC_TYPE is NULL, this function
4061 is equivalent to convert_and_check. This function is a wrapper that
4062 handles conversions that may be different than
4063 the usual ones because of excess precision. */
4065 static tree
4066 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4068 if (TREE_TYPE (expr) == type)
4069 return expr;
4071 if (!semantic_type)
4072 return convert_and_check (type, expr);
4074 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4075 && TREE_TYPE (expr) != semantic_type)
4077 /* For integers, we need to check the real conversion, not
4078 the conversion to the excess precision type. */
4079 expr = convert_and_check (semantic_type, expr);
4081 /* Result type is the excess precision type, which should be
4082 large enough, so do not check. */
4083 return convert (type, expr);
4086 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4087 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4088 if folded to an integer constant then the unselected half may
4089 contain arbitrary operations not normally permitted in constant
4090 expressions. Set the location of the expression to LOC. */
4092 tree
4093 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4094 tree op1, tree op1_original_type, tree op2,
4095 tree op2_original_type)
4097 tree type1;
4098 tree type2;
4099 enum tree_code code1;
4100 enum tree_code code2;
4101 tree result_type = NULL;
4102 tree semantic_result_type = NULL;
4103 tree orig_op1 = op1, orig_op2 = op2;
4104 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4105 bool ifexp_int_operands;
4106 tree ret;
4108 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4109 if (op1_int_operands)
4110 op1 = remove_c_maybe_const_expr (op1);
4111 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4112 if (op2_int_operands)
4113 op2 = remove_c_maybe_const_expr (op2);
4114 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4115 if (ifexp_int_operands)
4116 ifexp = remove_c_maybe_const_expr (ifexp);
4118 /* Promote both alternatives. */
4120 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4121 op1 = default_conversion (op1);
4122 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4123 op2 = default_conversion (op2);
4125 if (TREE_CODE (ifexp) == ERROR_MARK
4126 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4127 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4128 return error_mark_node;
4130 type1 = TREE_TYPE (op1);
4131 code1 = TREE_CODE (type1);
4132 type2 = TREE_TYPE (op2);
4133 code2 = TREE_CODE (type2);
4135 /* C90 does not permit non-lvalue arrays in conditional expressions.
4136 In C99 they will be pointers by now. */
4137 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4139 error_at (colon_loc, "non-lvalue array in conditional expression");
4140 return error_mark_node;
4143 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4144 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4145 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4146 || code1 == COMPLEX_TYPE)
4147 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4148 || code2 == COMPLEX_TYPE))
4150 semantic_result_type = c_common_type (type1, type2);
4151 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4153 op1 = TREE_OPERAND (op1, 0);
4154 type1 = TREE_TYPE (op1);
4155 gcc_assert (TREE_CODE (type1) == code1);
4157 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4159 op2 = TREE_OPERAND (op2, 0);
4160 type2 = TREE_TYPE (op2);
4161 gcc_assert (TREE_CODE (type2) == code2);
4165 if (warn_cxx_compat)
4167 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4168 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4170 if (TREE_CODE (t1) == ENUMERAL_TYPE
4171 && TREE_CODE (t2) == ENUMERAL_TYPE
4172 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4173 warning_at (colon_loc, OPT_Wc___compat,
4174 ("different enum types in conditional is "
4175 "invalid in C++: %qT vs %qT"),
4176 t1, t2);
4179 /* Quickly detect the usual case where op1 and op2 have the same type
4180 after promotion. */
4181 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4183 if (type1 == type2)
4184 result_type = type1;
4185 else
4186 result_type = TYPE_MAIN_VARIANT (type1);
4188 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4189 || code1 == COMPLEX_TYPE)
4190 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4191 || code2 == COMPLEX_TYPE))
4193 result_type = c_common_type (type1, type2);
4194 do_warn_double_promotion (result_type, type1, type2,
4195 "implicit conversion from %qT to %qT to "
4196 "match other result of conditional",
4197 colon_loc);
4199 /* If -Wsign-compare, warn here if type1 and type2 have
4200 different signedness. We'll promote the signed to unsigned
4201 and later code won't know it used to be different.
4202 Do this check on the original types, so that explicit casts
4203 will be considered, but default promotions won't. */
4204 if (c_inhibit_evaluation_warnings == 0)
4206 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4207 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4209 if (unsigned_op1 ^ unsigned_op2)
4211 bool ovf;
4213 /* Do not warn if the result type is signed, since the
4214 signed type will only be chosen if it can represent
4215 all the values of the unsigned type. */
4216 if (!TYPE_UNSIGNED (result_type))
4217 /* OK */;
4218 else
4220 bool op1_maybe_const = true;
4221 bool op2_maybe_const = true;
4223 /* Do not warn if the signed quantity is an
4224 unsuffixed integer literal (or some static
4225 constant expression involving such literals) and
4226 it is non-negative. This warning requires the
4227 operands to be folded for best results, so do
4228 that folding in this case even without
4229 warn_sign_compare to avoid warning options
4230 possibly affecting code generation. */
4231 c_inhibit_evaluation_warnings
4232 += (ifexp == truthvalue_false_node);
4233 op1 = c_fully_fold (op1, require_constant_value,
4234 &op1_maybe_const);
4235 c_inhibit_evaluation_warnings
4236 -= (ifexp == truthvalue_false_node);
4238 c_inhibit_evaluation_warnings
4239 += (ifexp == truthvalue_true_node);
4240 op2 = c_fully_fold (op2, require_constant_value,
4241 &op2_maybe_const);
4242 c_inhibit_evaluation_warnings
4243 -= (ifexp == truthvalue_true_node);
4245 if (warn_sign_compare)
4247 if ((unsigned_op2
4248 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4249 || (unsigned_op1
4250 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4251 /* OK */;
4252 else
4253 warning_at (colon_loc, OPT_Wsign_compare,
4254 ("signed and unsigned type in "
4255 "conditional expression"));
4257 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4258 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4259 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4260 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4265 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4267 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4268 pedwarn (colon_loc, OPT_pedantic,
4269 "ISO C forbids conditional expr with only one void side");
4270 result_type = void_type_node;
4272 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4274 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4275 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4276 addr_space_t as_common;
4278 if (comp_target_types (colon_loc, type1, type2))
4279 result_type = common_pointer_type (type1, type2);
4280 else if (null_pointer_constant_p (orig_op1))
4281 result_type = type2;
4282 else if (null_pointer_constant_p (orig_op2))
4283 result_type = type1;
4284 else if (!addr_space_superset (as1, as2, &as_common))
4286 error_at (colon_loc, "pointers to disjoint address spaces "
4287 "used in conditional expression");
4288 return error_mark_node;
4290 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4292 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4293 pedwarn (colon_loc, OPT_pedantic,
4294 "ISO C forbids conditional expr between "
4295 "%<void *%> and function pointer");
4296 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4297 TREE_TYPE (type2)));
4299 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4301 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4302 pedwarn (colon_loc, OPT_pedantic,
4303 "ISO C forbids conditional expr between "
4304 "%<void *%> and function pointer");
4305 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4306 TREE_TYPE (type1)));
4308 /* Objective-C pointer comparisons are a bit more lenient. */
4309 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4310 result_type = objc_common_type (type1, type2);
4311 else
4313 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4315 pedwarn (colon_loc, 0,
4316 "pointer type mismatch in conditional expression");
4317 result_type = build_pointer_type
4318 (build_qualified_type (void_type_node, qual));
4321 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4323 if (!null_pointer_constant_p (orig_op2))
4324 pedwarn (colon_loc, 0,
4325 "pointer/integer type mismatch in conditional expression");
4326 else
4328 op2 = null_pointer_node;
4330 result_type = type1;
4332 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4334 if (!null_pointer_constant_p (orig_op1))
4335 pedwarn (colon_loc, 0,
4336 "pointer/integer type mismatch in conditional expression");
4337 else
4339 op1 = null_pointer_node;
4341 result_type = type2;
4344 if (!result_type)
4346 if (flag_cond_mismatch)
4347 result_type = void_type_node;
4348 else
4350 error_at (colon_loc, "type mismatch in conditional expression");
4351 return error_mark_node;
4355 /* Merge const and volatile flags of the incoming types. */
4356 result_type
4357 = build_type_variant (result_type,
4358 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4359 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4361 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4362 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4364 if (ifexp_bcp && ifexp == truthvalue_true_node)
4366 op2_int_operands = true;
4367 op1 = c_fully_fold (op1, require_constant_value, NULL);
4369 if (ifexp_bcp && ifexp == truthvalue_false_node)
4371 op1_int_operands = true;
4372 op2 = c_fully_fold (op2, require_constant_value, NULL);
4374 int_const = int_operands = (ifexp_int_operands
4375 && op1_int_operands
4376 && op2_int_operands);
4377 if (int_operands)
4379 int_const = ((ifexp == truthvalue_true_node
4380 && TREE_CODE (orig_op1) == INTEGER_CST
4381 && !TREE_OVERFLOW (orig_op1))
4382 || (ifexp == truthvalue_false_node
4383 && TREE_CODE (orig_op2) == INTEGER_CST
4384 && !TREE_OVERFLOW (orig_op2)));
4386 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4387 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4388 else
4390 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4391 if (int_operands)
4392 ret = note_integer_operands (ret);
4394 if (semantic_result_type)
4395 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4397 protected_set_expr_location (ret, colon_loc);
4398 return ret;
4401 /* Return a compound expression that performs two expressions and
4402 returns the value of the second of them.
4404 LOC is the location of the COMPOUND_EXPR. */
4406 tree
4407 build_compound_expr (location_t loc, tree expr1, tree expr2)
4409 bool expr1_int_operands, expr2_int_operands;
4410 tree eptype = NULL_TREE;
4411 tree ret;
4413 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4414 if (expr1_int_operands)
4415 expr1 = remove_c_maybe_const_expr (expr1);
4416 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4417 if (expr2_int_operands)
4418 expr2 = remove_c_maybe_const_expr (expr2);
4420 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4421 expr1 = TREE_OPERAND (expr1, 0);
4422 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4424 eptype = TREE_TYPE (expr2);
4425 expr2 = TREE_OPERAND (expr2, 0);
4428 if (!TREE_SIDE_EFFECTS (expr1))
4430 /* The left-hand operand of a comma expression is like an expression
4431 statement: with -Wunused, we should warn if it doesn't have
4432 any side-effects, unless it was explicitly cast to (void). */
4433 if (warn_unused_value)
4435 if (VOID_TYPE_P (TREE_TYPE (expr1))
4436 && CONVERT_EXPR_P (expr1))
4437 ; /* (void) a, b */
4438 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4439 && TREE_CODE (expr1) == COMPOUND_EXPR
4440 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4441 ; /* (void) a, (void) b, c */
4442 else
4443 warning_at (loc, OPT_Wunused_value,
4444 "left-hand operand of comma expression has no effect");
4448 /* With -Wunused, we should also warn if the left-hand operand does have
4449 side-effects, but computes a value which is not used. For example, in
4450 `foo() + bar(), baz()' the result of the `+' operator is not used,
4451 so we should issue a warning. */
4452 else if (warn_unused_value)
4453 warn_if_unused_value (expr1, loc);
4455 if (expr2 == error_mark_node)
4456 return error_mark_node;
4458 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4460 if (flag_isoc99
4461 && expr1_int_operands
4462 && expr2_int_operands)
4463 ret = note_integer_operands (ret);
4465 if (eptype)
4466 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4468 protected_set_expr_location (ret, loc);
4469 return ret;
4472 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4473 which we are casting. OTYPE is the type of the expression being
4474 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4475 of the cast. -Wcast-qual appeared on the command line. Named
4476 address space qualifiers are not handled here, because they result
4477 in different warnings. */
4479 static void
4480 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4482 tree in_type = type;
4483 tree in_otype = otype;
4484 int added = 0;
4485 int discarded = 0;
4486 bool is_const;
4488 /* Check that the qualifiers on IN_TYPE are a superset of the
4489 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4490 nodes is uninteresting and we stop as soon as we hit a
4491 non-POINTER_TYPE node on either type. */
4494 in_otype = TREE_TYPE (in_otype);
4495 in_type = TREE_TYPE (in_type);
4497 /* GNU C allows cv-qualified function types. 'const' means the
4498 function is very pure, 'volatile' means it can't return. We
4499 need to warn when such qualifiers are added, not when they're
4500 taken away. */
4501 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4502 && TREE_CODE (in_type) == FUNCTION_TYPE)
4503 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4504 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4505 else
4506 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4507 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4509 while (TREE_CODE (in_type) == POINTER_TYPE
4510 && TREE_CODE (in_otype) == POINTER_TYPE);
4512 if (added)
4513 warning_at (loc, OPT_Wcast_qual,
4514 "cast adds %q#v qualifier to function type", added);
4516 if (discarded)
4517 /* There are qualifiers present in IN_OTYPE that are not present
4518 in IN_TYPE. */
4519 warning_at (loc, OPT_Wcast_qual,
4520 "cast discards %q#v qualifier from pointer target type",
4521 discarded);
4523 if (added || discarded)
4524 return;
4526 /* A cast from **T to const **T is unsafe, because it can cause a
4527 const value to be changed with no additional warning. We only
4528 issue this warning if T is the same on both sides, and we only
4529 issue the warning if there are the same number of pointers on
4530 both sides, as otherwise the cast is clearly unsafe anyhow. A
4531 cast is unsafe when a qualifier is added at one level and const
4532 is not present at all outer levels.
4534 To issue this warning, we check at each level whether the cast
4535 adds new qualifiers not already seen. We don't need to special
4536 case function types, as they won't have the same
4537 TYPE_MAIN_VARIANT. */
4539 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4540 return;
4541 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4542 return;
4544 in_type = type;
4545 in_otype = otype;
4546 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4549 in_type = TREE_TYPE (in_type);
4550 in_otype = TREE_TYPE (in_otype);
4551 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4552 && !is_const)
4554 warning_at (loc, OPT_Wcast_qual,
4555 "to be safe all intermediate pointers in cast from "
4556 "%qT to %qT must be %<const%> qualified",
4557 otype, type);
4558 break;
4560 if (is_const)
4561 is_const = TYPE_READONLY (in_type);
4563 while (TREE_CODE (in_type) == POINTER_TYPE);
4566 /* Build an expression representing a cast to type TYPE of expression EXPR.
4567 LOC is the location of the cast-- typically the open paren of the cast. */
4569 tree
4570 build_c_cast (location_t loc, tree type, tree expr)
4572 tree value;
4574 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4575 expr = TREE_OPERAND (expr, 0);
4577 value = expr;
4579 if (type == error_mark_node || expr == error_mark_node)
4580 return error_mark_node;
4582 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4583 only in <protocol> qualifications. But when constructing cast expressions,
4584 the protocols do matter and must be kept around. */
4585 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4586 return build1 (NOP_EXPR, type, expr);
4588 type = TYPE_MAIN_VARIANT (type);
4590 if (TREE_CODE (type) == ARRAY_TYPE)
4592 error_at (loc, "cast specifies array type");
4593 return error_mark_node;
4596 if (TREE_CODE (type) == FUNCTION_TYPE)
4598 error_at (loc, "cast specifies function type");
4599 return error_mark_node;
4602 if (!VOID_TYPE_P (type))
4604 value = require_complete_type (value);
4605 if (value == error_mark_node)
4606 return error_mark_node;
4609 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4611 if (TREE_CODE (type) == RECORD_TYPE
4612 || TREE_CODE (type) == UNION_TYPE)
4613 pedwarn (loc, OPT_pedantic,
4614 "ISO C forbids casting nonscalar to the same type");
4616 else if (TREE_CODE (type) == UNION_TYPE)
4618 tree field;
4620 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4621 if (TREE_TYPE (field) != error_mark_node
4622 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4623 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4624 break;
4626 if (field)
4628 tree t;
4629 bool maybe_const = true;
4631 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4632 t = c_fully_fold (value, false, &maybe_const);
4633 t = build_constructor_single (type, field, t);
4634 if (!maybe_const)
4635 t = c_wrap_maybe_const (t, true);
4636 t = digest_init (loc, type, t,
4637 NULL_TREE, false, true, 0);
4638 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4639 return t;
4641 error_at (loc, "cast to union type from type not present in union");
4642 return error_mark_node;
4644 else
4646 tree otype, ovalue;
4648 if (type == void_type_node)
4650 tree t = build1 (CONVERT_EXPR, type, value);
4651 SET_EXPR_LOCATION (t, loc);
4652 return t;
4655 otype = TREE_TYPE (value);
4657 /* Optionally warn about potentially worrisome casts. */
4658 if (warn_cast_qual
4659 && TREE_CODE (type) == POINTER_TYPE
4660 && TREE_CODE (otype) == POINTER_TYPE)
4661 handle_warn_cast_qual (loc, type, otype);
4663 /* Warn about conversions between pointers to disjoint
4664 address spaces. */
4665 if (TREE_CODE (type) == POINTER_TYPE
4666 && TREE_CODE (otype) == POINTER_TYPE
4667 && !null_pointer_constant_p (value))
4669 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4670 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4671 addr_space_t as_common;
4673 if (!addr_space_superset (as_to, as_from, &as_common))
4675 if (ADDR_SPACE_GENERIC_P (as_from))
4676 warning_at (loc, 0, "cast to %s address space pointer "
4677 "from disjoint generic address space pointer",
4678 c_addr_space_name (as_to));
4680 else if (ADDR_SPACE_GENERIC_P (as_to))
4681 warning_at (loc, 0, "cast to generic address space pointer "
4682 "from disjoint %s address space pointer",
4683 c_addr_space_name (as_from));
4685 else
4686 warning_at (loc, 0, "cast to %s address space pointer "
4687 "from disjoint %s address space pointer",
4688 c_addr_space_name (as_to),
4689 c_addr_space_name (as_from));
4693 /* Warn about possible alignment problems. */
4694 if (STRICT_ALIGNMENT
4695 && TREE_CODE (type) == POINTER_TYPE
4696 && TREE_CODE (otype) == POINTER_TYPE
4697 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4698 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4699 /* Don't warn about opaque types, where the actual alignment
4700 restriction is unknown. */
4701 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4702 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4703 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4704 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4705 warning_at (loc, OPT_Wcast_align,
4706 "cast increases required alignment of target type");
4708 if (TREE_CODE (type) == INTEGER_TYPE
4709 && TREE_CODE (otype) == POINTER_TYPE
4710 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4711 /* Unlike conversion of integers to pointers, where the
4712 warning is disabled for converting constants because
4713 of cases such as SIG_*, warn about converting constant
4714 pointers to integers. In some cases it may cause unwanted
4715 sign extension, and a warning is appropriate. */
4716 warning_at (loc, OPT_Wpointer_to_int_cast,
4717 "cast from pointer to integer of different size");
4719 if (TREE_CODE (value) == CALL_EXPR
4720 && TREE_CODE (type) != TREE_CODE (otype))
4721 warning_at (loc, OPT_Wbad_function_cast,
4722 "cast from function call of type %qT "
4723 "to non-matching type %qT", otype, type);
4725 if (TREE_CODE (type) == POINTER_TYPE
4726 && TREE_CODE (otype) == INTEGER_TYPE
4727 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4728 /* Don't warn about converting any constant. */
4729 && !TREE_CONSTANT (value))
4730 warning_at (loc,
4731 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4732 "of different size");
4734 if (warn_strict_aliasing <= 2)
4735 strict_aliasing_warning (otype, type, expr);
4737 /* If pedantic, warn for conversions between function and object
4738 pointer types, except for converting a null pointer constant
4739 to function pointer type. */
4740 if (pedantic
4741 && TREE_CODE (type) == POINTER_TYPE
4742 && TREE_CODE (otype) == POINTER_TYPE
4743 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4744 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4745 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4746 "conversion of function pointer to object pointer type");
4748 if (pedantic
4749 && TREE_CODE (type) == POINTER_TYPE
4750 && TREE_CODE (otype) == POINTER_TYPE
4751 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4752 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4753 && !null_pointer_constant_p (value))
4754 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4755 "conversion of object pointer to function pointer type");
4757 ovalue = value;
4758 value = convert (type, value);
4760 /* Ignore any integer overflow caused by the cast. */
4761 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4763 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4765 if (!TREE_OVERFLOW (value))
4767 /* Avoid clobbering a shared constant. */
4768 value = copy_node (value);
4769 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4772 else if (TREE_OVERFLOW (value))
4773 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4774 value = build_int_cst_wide (TREE_TYPE (value),
4775 TREE_INT_CST_LOW (value),
4776 TREE_INT_CST_HIGH (value));
4780 /* Don't let a cast be an lvalue. */
4781 if (value == expr)
4782 value = non_lvalue_loc (loc, value);
4784 /* Don't allow the results of casting to floating-point or complex
4785 types be confused with actual constants, or casts involving
4786 integer and pointer types other than direct integer-to-integer
4787 and integer-to-pointer be confused with integer constant
4788 expressions and null pointer constants. */
4789 if (TREE_CODE (value) == REAL_CST
4790 || TREE_CODE (value) == COMPLEX_CST
4791 || (TREE_CODE (value) == INTEGER_CST
4792 && !((TREE_CODE (expr) == INTEGER_CST
4793 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4794 || TREE_CODE (expr) == REAL_CST
4795 || TREE_CODE (expr) == COMPLEX_CST)))
4796 value = build1 (NOP_EXPR, type, value);
4798 if (CAN_HAVE_LOCATION_P (value))
4799 SET_EXPR_LOCATION (value, loc);
4800 return value;
4803 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4804 location of the open paren of the cast, or the position of the cast
4805 expr. */
4806 tree
4807 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4809 tree type;
4810 tree type_expr = NULL_TREE;
4811 bool type_expr_const = true;
4812 tree ret;
4813 int saved_wsp = warn_strict_prototypes;
4815 /* This avoids warnings about unprototyped casts on
4816 integers. E.g. "#define SIG_DFL (void(*)())0". */
4817 if (TREE_CODE (expr) == INTEGER_CST)
4818 warn_strict_prototypes = 0;
4819 type = groktypename (type_name, &type_expr, &type_expr_const);
4820 warn_strict_prototypes = saved_wsp;
4822 ret = build_c_cast (loc, type, expr);
4823 if (type_expr)
4825 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4826 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4827 SET_EXPR_LOCATION (ret, loc);
4830 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4831 SET_EXPR_LOCATION (ret, loc);
4833 /* C++ does not permits types to be defined in a cast. */
4834 if (warn_cxx_compat && type_name->specs->tag_defined_p)
4835 warning_at (loc, OPT_Wc___compat,
4836 "defining a type in a cast is invalid in C++");
4838 return ret;
4841 /* Build an assignment expression of lvalue LHS from value RHS.
4842 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4843 may differ from TREE_TYPE (LHS) for an enum bitfield.
4844 MODIFYCODE is the code for a binary operator that we use
4845 to combine the old value of LHS with RHS to get the new value.
4846 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4847 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4848 which may differ from TREE_TYPE (RHS) for an enum value.
4850 LOCATION is the location of the MODIFYCODE operator.
4851 RHS_LOC is the location of the RHS. */
4853 tree
4854 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4855 enum tree_code modifycode,
4856 location_t rhs_loc, tree rhs, tree rhs_origtype)
4858 tree result;
4859 tree newrhs;
4860 tree rhs_semantic_type = NULL_TREE;
4861 tree lhstype = TREE_TYPE (lhs);
4862 tree olhstype = lhstype;
4863 bool npc;
4865 /* Types that aren't fully specified cannot be used in assignments. */
4866 lhs = require_complete_type (lhs);
4868 /* Avoid duplicate error messages from operands that had errors. */
4869 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4870 return error_mark_node;
4872 /* For ObjC, defer this check until we have assessed CLASS.property. */
4873 if (!c_dialect_objc () && !lvalue_or_else (lhs, lv_assign))
4874 return error_mark_node;
4876 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4878 rhs_semantic_type = TREE_TYPE (rhs);
4879 rhs = TREE_OPERAND (rhs, 0);
4882 newrhs = rhs;
4884 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4886 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4887 lhs_origtype, modifycode, rhs_loc, rhs,
4888 rhs_origtype);
4889 if (inner == error_mark_node)
4890 return error_mark_node;
4891 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4892 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4893 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4894 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4895 protected_set_expr_location (result, location);
4896 return result;
4899 /* If a binary op has been requested, combine the old LHS value with the RHS
4900 producing the value we should actually store into the LHS. */
4902 if (modifycode != NOP_EXPR)
4904 lhs = c_fully_fold (lhs, false, NULL);
4905 lhs = stabilize_reference (lhs);
4906 newrhs = build_binary_op (location,
4907 modifycode, lhs, rhs, 1);
4909 /* The original type of the right hand side is no longer
4910 meaningful. */
4911 rhs_origtype = NULL_TREE;
4914 if (c_dialect_objc ())
4916 result = objc_build_setter_call (lhs, newrhs);
4917 if (result)
4918 return result;
4919 if (!lvalue_or_else (lhs, lv_assign))
4920 return error_mark_node;
4923 /* Give an error for storing in something that is 'const'. */
4925 if (TYPE_READONLY (lhstype)
4926 || ((TREE_CODE (lhstype) == RECORD_TYPE
4927 || TREE_CODE (lhstype) == UNION_TYPE)
4928 && C_TYPE_FIELDS_READONLY (lhstype)))
4930 readonly_error (lhs, lv_assign);
4931 return error_mark_node;
4933 else if (TREE_READONLY (lhs))
4934 readonly_warning (lhs, lv_assign);
4936 /* If storing into a structure or union member,
4937 it has probably been given type `int'.
4938 Compute the type that would go with
4939 the actual amount of storage the member occupies. */
4941 if (TREE_CODE (lhs) == COMPONENT_REF
4942 && (TREE_CODE (lhstype) == INTEGER_TYPE
4943 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4944 || TREE_CODE (lhstype) == REAL_TYPE
4945 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4946 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4948 /* If storing in a field that is in actuality a short or narrower than one,
4949 we must store in the field in its actual type. */
4951 if (lhstype != TREE_TYPE (lhs))
4953 lhs = copy_node (lhs);
4954 TREE_TYPE (lhs) = lhstype;
4957 /* Issue -Wc++-compat warnings about an assignment to an enum type
4958 when LHS does not have its original type. This happens for,
4959 e.g., an enum bitfield in a struct. */
4960 if (warn_cxx_compat
4961 && lhs_origtype != NULL_TREE
4962 && lhs_origtype != lhstype
4963 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4965 tree checktype = (rhs_origtype != NULL_TREE
4966 ? rhs_origtype
4967 : TREE_TYPE (rhs));
4968 if (checktype != error_mark_node
4969 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4970 warning_at (location, OPT_Wc___compat,
4971 "enum conversion in assignment is invalid in C++");
4974 /* Convert new value to destination type. Fold it first, then
4975 restore any excess precision information, for the sake of
4976 conversion warnings. */
4978 npc = null_pointer_constant_p (newrhs);
4979 newrhs = c_fully_fold (newrhs, false, NULL);
4980 if (rhs_semantic_type)
4981 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4982 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4983 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4984 if (TREE_CODE (newrhs) == ERROR_MARK)
4985 return error_mark_node;
4987 /* Emit ObjC write barrier, if necessary. */
4988 if (c_dialect_objc () && flag_objc_gc)
4990 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4991 if (result)
4993 protected_set_expr_location (result, location);
4994 return result;
4998 /* Scan operands. */
5000 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5001 TREE_SIDE_EFFECTS (result) = 1;
5002 protected_set_expr_location (result, location);
5004 /* If we got the LHS in a different type for storing in,
5005 convert the result back to the nominal type of LHS
5006 so that the value we return always has the same type
5007 as the LHS argument. */
5009 if (olhstype == TREE_TYPE (result))
5010 return result;
5012 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5013 ic_assign, false, NULL_TREE, NULL_TREE, 0);
5014 protected_set_expr_location (result, location);
5015 return result;
5018 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5019 This is used to implement -fplan9-extensions. */
5021 static bool
5022 find_anonymous_field_with_type (tree struct_type, tree type)
5024 tree field;
5025 bool found;
5027 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5028 || TREE_CODE (struct_type) == UNION_TYPE);
5029 found = false;
5030 for (field = TYPE_FIELDS (struct_type);
5031 field != NULL_TREE;
5032 field = TREE_CHAIN (field))
5034 if (DECL_NAME (field) == NULL
5035 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5037 if (found)
5038 return false;
5039 found = true;
5041 else if (DECL_NAME (field) == NULL
5042 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5043 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5044 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5046 if (found)
5047 return false;
5048 found = true;
5051 return found;
5054 /* RHS is an expression whose type is pointer to struct. If there is
5055 an anonymous field in RHS with type TYPE, then return a pointer to
5056 that field in RHS. This is used with -fplan9-extensions. This
5057 returns NULL if no conversion could be found. */
5059 static tree
5060 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5062 tree rhs_struct_type, lhs_main_type;
5063 tree field, found_field;
5064 bool found_sub_field;
5065 tree ret;
5067 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5068 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5069 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5070 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5072 gcc_assert (POINTER_TYPE_P (type));
5073 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5075 found_field = NULL_TREE;
5076 found_sub_field = false;
5077 for (field = TYPE_FIELDS (rhs_struct_type);
5078 field != NULL_TREE;
5079 field = TREE_CHAIN (field))
5081 if (DECL_NAME (field) != NULL_TREE
5082 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5083 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5084 continue;
5085 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5087 if (found_field != NULL_TREE)
5088 return NULL_TREE;
5089 found_field = field;
5091 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5092 lhs_main_type))
5094 if (found_field != NULL_TREE)
5095 return NULL_TREE;
5096 found_field = field;
5097 found_sub_field = true;
5101 if (found_field == NULL_TREE)
5102 return NULL_TREE;
5104 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5105 build_fold_indirect_ref (rhs), found_field,
5106 NULL_TREE);
5107 ret = build_fold_addr_expr_loc (location, ret);
5109 if (found_sub_field)
5111 ret = convert_to_anonymous_field (location, type, ret);
5112 gcc_assert (ret != NULL_TREE);
5115 return ret;
5118 /* Convert value RHS to type TYPE as preparation for an assignment to
5119 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5120 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5121 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5122 constant before any folding.
5123 The real work of conversion is done by `convert'.
5124 The purpose of this function is to generate error messages
5125 for assignments that are not allowed in C.
5126 ERRTYPE says whether it is argument passing, assignment,
5127 initialization or return.
5129 LOCATION is the location of the RHS.
5130 FUNCTION is a tree for the function being called.
5131 PARMNUM is the number of the argument, for printing in error messages. */
5133 static tree
5134 convert_for_assignment (location_t location, tree type, tree rhs,
5135 tree origtype, enum impl_conv errtype,
5136 bool null_pointer_constant, tree fundecl,
5137 tree function, int parmnum)
5139 enum tree_code codel = TREE_CODE (type);
5140 tree orig_rhs = rhs;
5141 tree rhstype;
5142 enum tree_code coder;
5143 tree rname = NULL_TREE;
5144 bool objc_ok = false;
5146 if (errtype == ic_argpass)
5148 tree selector;
5149 /* Change pointer to function to the function itself for
5150 diagnostics. */
5151 if (TREE_CODE (function) == ADDR_EXPR
5152 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5153 function = TREE_OPERAND (function, 0);
5155 /* Handle an ObjC selector specially for diagnostics. */
5156 selector = objc_message_selector ();
5157 rname = function;
5158 if (selector && parmnum > 2)
5160 rname = selector;
5161 parmnum -= 2;
5165 /* This macro is used to emit diagnostics to ensure that all format
5166 strings are complete sentences, visible to gettext and checked at
5167 compile time. */
5168 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5169 do { \
5170 switch (errtype) \
5172 case ic_argpass: \
5173 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5174 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5175 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5176 "expected %qT but argument is of type %qT", \
5177 type, rhstype); \
5178 break; \
5179 case ic_assign: \
5180 pedwarn (LOCATION, OPT, AS); \
5181 break; \
5182 case ic_init: \
5183 pedwarn_init (LOCATION, OPT, IN); \
5184 break; \
5185 case ic_return: \
5186 pedwarn (LOCATION, OPT, RE); \
5187 break; \
5188 default: \
5189 gcc_unreachable (); \
5191 } while (0)
5193 /* This macro is used to emit diagnostics to ensure that all format
5194 strings are complete sentences, visible to gettext and checked at
5195 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5196 extra parameter to enumerate qualifiers. */
5198 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5199 do { \
5200 switch (errtype) \
5202 case ic_argpass: \
5203 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5204 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5205 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5206 "expected %qT but argument is of type %qT", \
5207 type, rhstype); \
5208 break; \
5209 case ic_assign: \
5210 pedwarn (LOCATION, OPT, AS, QUALS); \
5211 break; \
5212 case ic_init: \
5213 pedwarn (LOCATION, OPT, IN, QUALS); \
5214 break; \
5215 case ic_return: \
5216 pedwarn (LOCATION, OPT, RE, QUALS); \
5217 break; \
5218 default: \
5219 gcc_unreachable (); \
5221 } while (0)
5223 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5224 rhs = TREE_OPERAND (rhs, 0);
5226 rhstype = TREE_TYPE (rhs);
5227 coder = TREE_CODE (rhstype);
5229 if (coder == ERROR_MARK)
5230 return error_mark_node;
5232 if (c_dialect_objc ())
5234 int parmno;
5236 switch (errtype)
5238 case ic_return:
5239 parmno = 0;
5240 break;
5242 case ic_assign:
5243 parmno = -1;
5244 break;
5246 case ic_init:
5247 parmno = -2;
5248 break;
5250 default:
5251 parmno = parmnum;
5252 break;
5255 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5258 if (warn_cxx_compat)
5260 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5261 if (checktype != error_mark_node
5262 && TREE_CODE (type) == ENUMERAL_TYPE
5263 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5265 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5266 G_("enum conversion when passing argument "
5267 "%d of %qE is invalid in C++"),
5268 G_("enum conversion in assignment is "
5269 "invalid in C++"),
5270 G_("enum conversion in initialization is "
5271 "invalid in C++"),
5272 G_("enum conversion in return is "
5273 "invalid in C++"));
5277 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5278 return rhs;
5280 if (coder == VOID_TYPE)
5282 /* Except for passing an argument to an unprototyped function,
5283 this is a constraint violation. When passing an argument to
5284 an unprototyped function, it is compile-time undefined;
5285 making it a constraint in that case was rejected in
5286 DR#252. */
5287 error_at (location, "void value not ignored as it ought to be");
5288 return error_mark_node;
5290 rhs = require_complete_type (rhs);
5291 if (rhs == error_mark_node)
5292 return error_mark_node;
5293 /* A type converts to a reference to it.
5294 This code doesn't fully support references, it's just for the
5295 special case of va_start and va_copy. */
5296 if (codel == REFERENCE_TYPE
5297 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5299 if (!lvalue_p (rhs))
5301 error_at (location, "cannot pass rvalue to reference parameter");
5302 return error_mark_node;
5304 if (!c_mark_addressable (rhs))
5305 return error_mark_node;
5306 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5307 SET_EXPR_LOCATION (rhs, location);
5309 /* We already know that these two types are compatible, but they
5310 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5311 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5312 likely to be va_list, a typedef to __builtin_va_list, which
5313 is different enough that it will cause problems later. */
5314 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5316 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5317 SET_EXPR_LOCATION (rhs, location);
5320 rhs = build1 (NOP_EXPR, type, rhs);
5321 SET_EXPR_LOCATION (rhs, location);
5322 return rhs;
5324 /* Some types can interconvert without explicit casts. */
5325 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5326 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5327 return convert (type, rhs);
5328 /* Arithmetic types all interconvert, and enum is treated like int. */
5329 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5330 || codel == FIXED_POINT_TYPE
5331 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5332 || codel == BOOLEAN_TYPE)
5333 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5334 || coder == FIXED_POINT_TYPE
5335 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5336 || coder == BOOLEAN_TYPE))
5338 tree ret;
5339 bool save = in_late_binary_op;
5340 if (codel == BOOLEAN_TYPE)
5341 in_late_binary_op = true;
5342 ret = convert_and_check (type, orig_rhs);
5343 if (codel == BOOLEAN_TYPE)
5344 in_late_binary_op = save;
5345 return ret;
5348 /* Aggregates in different TUs might need conversion. */
5349 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5350 && codel == coder
5351 && comptypes (type, rhstype))
5352 return convert_and_check (type, rhs);
5354 /* Conversion to a transparent union or record from its member types.
5355 This applies only to function arguments. */
5356 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5357 && TYPE_TRANSPARENT_AGGR (type))
5358 && errtype == ic_argpass)
5360 tree memb, marginal_memb = NULL_TREE;
5362 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5364 tree memb_type = TREE_TYPE (memb);
5366 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5367 TYPE_MAIN_VARIANT (rhstype)))
5368 break;
5370 if (TREE_CODE (memb_type) != POINTER_TYPE)
5371 continue;
5373 if (coder == POINTER_TYPE)
5375 tree ttl = TREE_TYPE (memb_type);
5376 tree ttr = TREE_TYPE (rhstype);
5378 /* Any non-function converts to a [const][volatile] void *
5379 and vice versa; otherwise, targets must be the same.
5380 Meanwhile, the lhs target must have all the qualifiers of
5381 the rhs. */
5382 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5383 || comp_target_types (location, memb_type, rhstype))
5385 /* If this type won't generate any warnings, use it. */
5386 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5387 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5388 && TREE_CODE (ttl) == FUNCTION_TYPE)
5389 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5390 == TYPE_QUALS (ttr))
5391 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5392 == TYPE_QUALS (ttl))))
5393 break;
5395 /* Keep looking for a better type, but remember this one. */
5396 if (!marginal_memb)
5397 marginal_memb = memb;
5401 /* Can convert integer zero to any pointer type. */
5402 if (null_pointer_constant)
5404 rhs = null_pointer_node;
5405 break;
5409 if (memb || marginal_memb)
5411 if (!memb)
5413 /* We have only a marginally acceptable member type;
5414 it needs a warning. */
5415 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5416 tree ttr = TREE_TYPE (rhstype);
5418 /* Const and volatile mean something different for function
5419 types, so the usual warnings are not appropriate. */
5420 if (TREE_CODE (ttr) == FUNCTION_TYPE
5421 && TREE_CODE (ttl) == FUNCTION_TYPE)
5423 /* Because const and volatile on functions are
5424 restrictions that say the function will not do
5425 certain things, it is okay to use a const or volatile
5426 function where an ordinary one is wanted, but not
5427 vice-versa. */
5428 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5429 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5430 WARN_FOR_QUALIFIERS (location, 0,
5431 G_("passing argument %d of %qE "
5432 "makes %q#v qualified function "
5433 "pointer from unqualified"),
5434 G_("assignment makes %q#v qualified "
5435 "function pointer from "
5436 "unqualified"),
5437 G_("initialization makes %q#v qualified "
5438 "function pointer from "
5439 "unqualified"),
5440 G_("return makes %q#v qualified function "
5441 "pointer from unqualified"),
5442 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5444 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5445 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5446 WARN_FOR_QUALIFIERS (location, 0,
5447 G_("passing argument %d of %qE discards "
5448 "%qv qualifier from pointer target type"),
5449 G_("assignment discards %qv qualifier "
5450 "from pointer target type"),
5451 G_("initialization discards %qv qualifier "
5452 "from pointer target type"),
5453 G_("return discards %qv qualifier from "
5454 "pointer target type"),
5455 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5457 memb = marginal_memb;
5460 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5461 pedwarn (location, OPT_pedantic,
5462 "ISO C prohibits argument conversion to union type");
5464 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5465 return build_constructor_single (type, memb, rhs);
5469 /* Conversions among pointers */
5470 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5471 && (coder == codel))
5473 tree ttl = TREE_TYPE (type);
5474 tree ttr = TREE_TYPE (rhstype);
5475 tree mvl = ttl;
5476 tree mvr = ttr;
5477 bool is_opaque_pointer;
5478 int target_cmp = 0; /* Cache comp_target_types () result. */
5479 addr_space_t asl;
5480 addr_space_t asr;
5482 if (TREE_CODE (mvl) != ARRAY_TYPE)
5483 mvl = TYPE_MAIN_VARIANT (mvl);
5484 if (TREE_CODE (mvr) != ARRAY_TYPE)
5485 mvr = TYPE_MAIN_VARIANT (mvr);
5486 /* Opaque pointers are treated like void pointers. */
5487 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5489 /* The Plan 9 compiler permits a pointer to a struct to be
5490 automatically converted into a pointer to an anonymous field
5491 within the struct. */
5492 if (flag_plan9_extensions
5493 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5494 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5495 && mvl != mvr)
5497 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5498 if (new_rhs != NULL_TREE)
5500 rhs = new_rhs;
5501 rhstype = TREE_TYPE (rhs);
5502 coder = TREE_CODE (rhstype);
5503 ttr = TREE_TYPE (rhstype);
5504 mvr = TYPE_MAIN_VARIANT (ttr);
5508 /* C++ does not allow the implicit conversion void* -> T*. However,
5509 for the purpose of reducing the number of false positives, we
5510 tolerate the special case of
5512 int *p = NULL;
5514 where NULL is typically defined in C to be '(void *) 0'. */
5515 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5516 warning_at (location, OPT_Wc___compat,
5517 "request for implicit conversion "
5518 "from %qT to %qT not permitted in C++", rhstype, type);
5520 /* See if the pointers point to incompatible address spaces. */
5521 asl = TYPE_ADDR_SPACE (ttl);
5522 asr = TYPE_ADDR_SPACE (ttr);
5523 if (!null_pointer_constant_p (rhs)
5524 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5526 switch (errtype)
5528 case ic_argpass:
5529 error_at (location, "passing argument %d of %qE from pointer to "
5530 "non-enclosed address space", parmnum, rname);
5531 break;
5532 case ic_assign:
5533 error_at (location, "assignment from pointer to "
5534 "non-enclosed address space");
5535 break;
5536 case ic_init:
5537 error_at (location, "initialization from pointer to "
5538 "non-enclosed address space");
5539 break;
5540 case ic_return:
5541 error_at (location, "return from pointer to "
5542 "non-enclosed address space");
5543 break;
5544 default:
5545 gcc_unreachable ();
5547 return error_mark_node;
5550 /* Check if the right-hand side has a format attribute but the
5551 left-hand side doesn't. */
5552 if (warn_missing_format_attribute
5553 && check_missing_format_attribute (type, rhstype))
5555 switch (errtype)
5557 case ic_argpass:
5558 warning_at (location, OPT_Wmissing_format_attribute,
5559 "argument %d of %qE might be "
5560 "a candidate for a format attribute",
5561 parmnum, rname);
5562 break;
5563 case ic_assign:
5564 warning_at (location, OPT_Wmissing_format_attribute,
5565 "assignment left-hand side might be "
5566 "a candidate for a format attribute");
5567 break;
5568 case ic_init:
5569 warning_at (location, OPT_Wmissing_format_attribute,
5570 "initialization left-hand side might be "
5571 "a candidate for a format attribute");
5572 break;
5573 case ic_return:
5574 warning_at (location, OPT_Wmissing_format_attribute,
5575 "return type might be "
5576 "a candidate for a format attribute");
5577 break;
5578 default:
5579 gcc_unreachable ();
5583 /* Any non-function converts to a [const][volatile] void *
5584 and vice versa; otherwise, targets must be the same.
5585 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5586 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5587 || (target_cmp = comp_target_types (location, type, rhstype))
5588 || is_opaque_pointer
5589 || (c_common_unsigned_type (mvl)
5590 == c_common_unsigned_type (mvr)))
5592 if (pedantic
5593 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5595 (VOID_TYPE_P (ttr)
5596 && !null_pointer_constant
5597 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5598 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5599 G_("ISO C forbids passing argument %d of "
5600 "%qE between function pointer "
5601 "and %<void *%>"),
5602 G_("ISO C forbids assignment between "
5603 "function pointer and %<void *%>"),
5604 G_("ISO C forbids initialization between "
5605 "function pointer and %<void *%>"),
5606 G_("ISO C forbids return between function "
5607 "pointer and %<void *%>"));
5608 /* Const and volatile mean something different for function types,
5609 so the usual warnings are not appropriate. */
5610 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5611 && TREE_CODE (ttl) != FUNCTION_TYPE)
5613 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5614 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5616 /* Types differing only by the presence of the 'volatile'
5617 qualifier are acceptable if the 'volatile' has been added
5618 in by the Objective-C EH machinery. */
5619 if (!objc_type_quals_match (ttl, ttr))
5620 WARN_FOR_QUALIFIERS (location, 0,
5621 G_("passing argument %d of %qE discards "
5622 "%qv qualifier from pointer target type"),
5623 G_("assignment discards %qv qualifier "
5624 "from pointer target type"),
5625 G_("initialization discards %qv qualifier "
5626 "from pointer target type"),
5627 G_("return discards %qv qualifier from "
5628 "pointer target type"),
5629 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5631 /* If this is not a case of ignoring a mismatch in signedness,
5632 no warning. */
5633 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5634 || target_cmp)
5636 /* If there is a mismatch, do warn. */
5637 else if (warn_pointer_sign)
5638 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5639 G_("pointer targets in passing argument "
5640 "%d of %qE differ in signedness"),
5641 G_("pointer targets in assignment "
5642 "differ in signedness"),
5643 G_("pointer targets in initialization "
5644 "differ in signedness"),
5645 G_("pointer targets in return differ "
5646 "in signedness"));
5648 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5649 && TREE_CODE (ttr) == FUNCTION_TYPE)
5651 /* Because const and volatile on functions are restrictions
5652 that say the function will not do certain things,
5653 it is okay to use a const or volatile function
5654 where an ordinary one is wanted, but not vice-versa. */
5655 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5656 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5657 WARN_FOR_QUALIFIERS (location, 0,
5658 G_("passing argument %d of %qE makes "
5659 "%q#v qualified function pointer "
5660 "from unqualified"),
5661 G_("assignment makes %q#v qualified function "
5662 "pointer from unqualified"),
5663 G_("initialization makes %q#v qualified "
5664 "function pointer from unqualified"),
5665 G_("return makes %q#v qualified function "
5666 "pointer from unqualified"),
5667 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5670 else
5671 /* Avoid warning about the volatile ObjC EH puts on decls. */
5672 if (!objc_ok)
5673 WARN_FOR_ASSIGNMENT (location, 0,
5674 G_("passing argument %d of %qE from "
5675 "incompatible pointer type"),
5676 G_("assignment from incompatible pointer type"),
5677 G_("initialization from incompatible "
5678 "pointer type"),
5679 G_("return from incompatible pointer type"));
5681 return convert (type, rhs);
5683 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5685 /* ??? This should not be an error when inlining calls to
5686 unprototyped functions. */
5687 error_at (location, "invalid use of non-lvalue array");
5688 return error_mark_node;
5690 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5692 /* An explicit constant 0 can convert to a pointer,
5693 or one that results from arithmetic, even including
5694 a cast to integer type. */
5695 if (!null_pointer_constant)
5696 WARN_FOR_ASSIGNMENT (location, 0,
5697 G_("passing argument %d of %qE makes "
5698 "pointer from integer without a cast"),
5699 G_("assignment makes pointer from integer "
5700 "without a cast"),
5701 G_("initialization makes pointer from "
5702 "integer without a cast"),
5703 G_("return makes pointer from integer "
5704 "without a cast"));
5706 return convert (type, rhs);
5708 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5710 WARN_FOR_ASSIGNMENT (location, 0,
5711 G_("passing argument %d of %qE makes integer "
5712 "from pointer without a cast"),
5713 G_("assignment makes integer from pointer "
5714 "without a cast"),
5715 G_("initialization makes integer from pointer "
5716 "without a cast"),
5717 G_("return makes integer from pointer "
5718 "without a cast"));
5719 return convert (type, rhs);
5721 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5723 tree ret;
5724 bool save = in_late_binary_op;
5725 in_late_binary_op = true;
5726 ret = convert (type, rhs);
5727 in_late_binary_op = save;
5728 return ret;
5731 switch (errtype)
5733 case ic_argpass:
5734 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5735 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5736 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5737 "expected %qT but argument is of type %qT", type, rhstype);
5738 break;
5739 case ic_assign:
5740 error_at (location, "incompatible types when assigning to type %qT from "
5741 "type %qT", type, rhstype);
5742 break;
5743 case ic_init:
5744 error_at (location,
5745 "incompatible types when initializing type %qT using type %qT",
5746 type, rhstype);
5747 break;
5748 case ic_return:
5749 error_at (location,
5750 "incompatible types when returning type %qT but %qT was "
5751 "expected", rhstype, type);
5752 break;
5753 default:
5754 gcc_unreachable ();
5757 return error_mark_node;
5760 /* If VALUE is a compound expr all of whose expressions are constant, then
5761 return its value. Otherwise, return error_mark_node.
5763 This is for handling COMPOUND_EXPRs as initializer elements
5764 which is allowed with a warning when -pedantic is specified. */
5766 static tree
5767 valid_compound_expr_initializer (tree value, tree endtype)
5769 if (TREE_CODE (value) == COMPOUND_EXPR)
5771 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5772 == error_mark_node)
5773 return error_mark_node;
5774 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5775 endtype);
5777 else if (!initializer_constant_valid_p (value, endtype))
5778 return error_mark_node;
5779 else
5780 return value;
5783 /* Perform appropriate conversions on the initial value of a variable,
5784 store it in the declaration DECL,
5785 and print any error messages that are appropriate.
5786 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5787 If the init is invalid, store an ERROR_MARK.
5789 INIT_LOC is the location of the initial value. */
5791 void
5792 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5794 tree value, type;
5795 bool npc = false;
5797 /* If variable's type was invalidly declared, just ignore it. */
5799 type = TREE_TYPE (decl);
5800 if (TREE_CODE (type) == ERROR_MARK)
5801 return;
5803 /* Digest the specified initializer into an expression. */
5805 if (init)
5806 npc = null_pointer_constant_p (init);
5807 value = digest_init (init_loc, type, init, origtype, npc,
5808 true, TREE_STATIC (decl));
5810 /* Store the expression if valid; else report error. */
5812 if (!in_system_header
5813 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5814 warning (OPT_Wtraditional, "traditional C rejects automatic "
5815 "aggregate initialization");
5817 DECL_INITIAL (decl) = value;
5819 /* ANSI wants warnings about out-of-range constant initializers. */
5820 STRIP_TYPE_NOPS (value);
5821 if (TREE_STATIC (decl))
5822 constant_expression_warning (value);
5824 /* Check if we need to set array size from compound literal size. */
5825 if (TREE_CODE (type) == ARRAY_TYPE
5826 && TYPE_DOMAIN (type) == 0
5827 && value != error_mark_node)
5829 tree inside_init = init;
5831 STRIP_TYPE_NOPS (inside_init);
5832 inside_init = fold (inside_init);
5834 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5836 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5838 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5840 /* For int foo[] = (int [3]){1}; we need to set array size
5841 now since later on array initializer will be just the
5842 brace enclosed list of the compound literal. */
5843 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5844 TREE_TYPE (decl) = type;
5845 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5846 layout_type (type);
5847 layout_decl (cldecl, 0);
5853 /* Methods for storing and printing names for error messages. */
5855 /* Implement a spelling stack that allows components of a name to be pushed
5856 and popped. Each element on the stack is this structure. */
5858 struct spelling
5860 int kind;
5861 union
5863 unsigned HOST_WIDE_INT i;
5864 const char *s;
5865 } u;
5868 #define SPELLING_STRING 1
5869 #define SPELLING_MEMBER 2
5870 #define SPELLING_BOUNDS 3
5872 static struct spelling *spelling; /* Next stack element (unused). */
5873 static struct spelling *spelling_base; /* Spelling stack base. */
5874 static int spelling_size; /* Size of the spelling stack. */
5876 /* Macros to save and restore the spelling stack around push_... functions.
5877 Alternative to SAVE_SPELLING_STACK. */
5879 #define SPELLING_DEPTH() (spelling - spelling_base)
5880 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5882 /* Push an element on the spelling stack with type KIND and assign VALUE
5883 to MEMBER. */
5885 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5887 int depth = SPELLING_DEPTH (); \
5889 if (depth >= spelling_size) \
5891 spelling_size += 10; \
5892 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5893 spelling_size); \
5894 RESTORE_SPELLING_DEPTH (depth); \
5897 spelling->kind = (KIND); \
5898 spelling->MEMBER = (VALUE); \
5899 spelling++; \
5902 /* Push STRING on the stack. Printed literally. */
5904 static void
5905 push_string (const char *string)
5907 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5910 /* Push a member name on the stack. Printed as '.' STRING. */
5912 static void
5913 push_member_name (tree decl)
5915 const char *const string
5916 = (DECL_NAME (decl)
5917 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5918 : _("<anonymous>"));
5919 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5922 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5924 static void
5925 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5927 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5930 /* Compute the maximum size in bytes of the printed spelling. */
5932 static int
5933 spelling_length (void)
5935 int size = 0;
5936 struct spelling *p;
5938 for (p = spelling_base; p < spelling; p++)
5940 if (p->kind == SPELLING_BOUNDS)
5941 size += 25;
5942 else
5943 size += strlen (p->u.s) + 1;
5946 return size;
5949 /* Print the spelling to BUFFER and return it. */
5951 static char *
5952 print_spelling (char *buffer)
5954 char *d = buffer;
5955 struct spelling *p;
5957 for (p = spelling_base; p < spelling; p++)
5958 if (p->kind == SPELLING_BOUNDS)
5960 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5961 d += strlen (d);
5963 else
5965 const char *s;
5966 if (p->kind == SPELLING_MEMBER)
5967 *d++ = '.';
5968 for (s = p->u.s; (*d = *s++); d++)
5971 *d++ = '\0';
5972 return buffer;
5975 /* Issue an error message for a bad initializer component.
5976 GMSGID identifies the message.
5977 The component name is taken from the spelling stack. */
5979 void
5980 error_init (const char *gmsgid)
5982 char *ofwhat;
5984 /* The gmsgid may be a format string with %< and %>. */
5985 error (gmsgid);
5986 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5987 if (*ofwhat)
5988 error ("(near initialization for %qs)", ofwhat);
5991 /* Issue a pedantic warning for a bad initializer component. OPT is
5992 the option OPT_* (from options.h) controlling this warning or 0 if
5993 it is unconditionally given. GMSGID identifies the message. The
5994 component name is taken from the spelling stack. */
5996 void
5997 pedwarn_init (location_t location, int opt, const char *gmsgid)
5999 char *ofwhat;
6001 /* The gmsgid may be a format string with %< and %>. */
6002 pedwarn (location, opt, gmsgid);
6003 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6004 if (*ofwhat)
6005 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6008 /* Issue a warning for a bad initializer component.
6010 OPT is the OPT_W* value corresponding to the warning option that
6011 controls this warning. GMSGID identifies the message. The
6012 component name is taken from the spelling stack. */
6014 static void
6015 warning_init (int opt, const char *gmsgid)
6017 char *ofwhat;
6019 /* The gmsgid may be a format string with %< and %>. */
6020 warning (opt, gmsgid);
6021 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6022 if (*ofwhat)
6023 warning (opt, "(near initialization for %qs)", ofwhat);
6026 /* If TYPE is an array type and EXPR is a parenthesized string
6027 constant, warn if pedantic that EXPR is being used to initialize an
6028 object of type TYPE. */
6030 void
6031 maybe_warn_string_init (tree type, struct c_expr expr)
6033 if (pedantic
6034 && TREE_CODE (type) == ARRAY_TYPE
6035 && TREE_CODE (expr.value) == STRING_CST
6036 && expr.original_code != STRING_CST)
6037 pedwarn_init (input_location, OPT_pedantic,
6038 "array initialized from parenthesized string constant");
6041 /* Digest the parser output INIT as an initializer for type TYPE.
6042 Return a C expression of type TYPE to represent the initial value.
6044 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6046 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6048 If INIT is a string constant, STRICT_STRING is true if it is
6049 unparenthesized or we should not warn here for it being parenthesized.
6050 For other types of INIT, STRICT_STRING is not used.
6052 INIT_LOC is the location of the INIT.
6054 REQUIRE_CONSTANT requests an error if non-constant initializers or
6055 elements are seen. */
6057 static tree
6058 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6059 bool null_pointer_constant, bool strict_string,
6060 int require_constant)
6062 enum tree_code code = TREE_CODE (type);
6063 tree inside_init = init;
6064 tree semantic_type = NULL_TREE;
6065 bool maybe_const = true;
6067 if (type == error_mark_node
6068 || !init
6069 || init == error_mark_node
6070 || TREE_TYPE (init) == error_mark_node)
6071 return error_mark_node;
6073 STRIP_TYPE_NOPS (inside_init);
6075 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6077 semantic_type = TREE_TYPE (inside_init);
6078 inside_init = TREE_OPERAND (inside_init, 0);
6080 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6081 inside_init = decl_constant_value_for_optimization (inside_init);
6083 /* Initialization of an array of chars from a string constant
6084 optionally enclosed in braces. */
6086 if (code == ARRAY_TYPE && inside_init
6087 && TREE_CODE (inside_init) == STRING_CST)
6089 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6090 /* Note that an array could be both an array of character type
6091 and an array of wchar_t if wchar_t is signed char or unsigned
6092 char. */
6093 bool char_array = (typ1 == char_type_node
6094 || typ1 == signed_char_type_node
6095 || typ1 == unsigned_char_type_node);
6096 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6097 bool char16_array = !!comptypes (typ1, char16_type_node);
6098 bool char32_array = !!comptypes (typ1, char32_type_node);
6100 if (char_array || wchar_array || char16_array || char32_array)
6102 struct c_expr expr;
6103 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6104 expr.value = inside_init;
6105 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6106 expr.original_type = NULL;
6107 maybe_warn_string_init (type, expr);
6109 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6110 pedwarn_init (init_loc, OPT_pedantic,
6111 "initialization of a flexible array member");
6113 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6114 TYPE_MAIN_VARIANT (type)))
6115 return inside_init;
6117 if (char_array)
6119 if (typ2 != char_type_node)
6121 error_init ("char-array initialized from wide string");
6122 return error_mark_node;
6125 else
6127 if (typ2 == char_type_node)
6129 error_init ("wide character array initialized from non-wide "
6130 "string");
6131 return error_mark_node;
6133 else if (!comptypes(typ1, typ2))
6135 error_init ("wide character array initialized from "
6136 "incompatible wide string");
6137 return error_mark_node;
6141 TREE_TYPE (inside_init) = type;
6142 if (TYPE_DOMAIN (type) != 0
6143 && TYPE_SIZE (type) != 0
6144 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6146 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6148 /* Subtract the size of a single (possibly wide) character
6149 because it's ok to ignore the terminating null char
6150 that is counted in the length of the constant. */
6151 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6152 (len
6153 - (TYPE_PRECISION (typ1)
6154 / BITS_PER_UNIT))))
6155 pedwarn_init (init_loc, 0,
6156 ("initializer-string for array of chars "
6157 "is too long"));
6158 else if (warn_cxx_compat
6159 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6160 warning_at (init_loc, OPT_Wc___compat,
6161 ("initializer-string for array chars "
6162 "is too long for C++"));
6165 return inside_init;
6167 else if (INTEGRAL_TYPE_P (typ1))
6169 error_init ("array of inappropriate type initialized "
6170 "from string constant");
6171 return error_mark_node;
6175 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6176 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6177 below and handle as a constructor. */
6178 if (code == VECTOR_TYPE
6179 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6180 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6181 && TREE_CONSTANT (inside_init))
6183 if (TREE_CODE (inside_init) == VECTOR_CST
6184 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6185 TYPE_MAIN_VARIANT (type)))
6186 return inside_init;
6188 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6190 unsigned HOST_WIDE_INT ix;
6191 tree value;
6192 bool constant_p = true;
6194 /* Iterate through elements and check if all constructor
6195 elements are *_CSTs. */
6196 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6197 if (!CONSTANT_CLASS_P (value))
6199 constant_p = false;
6200 break;
6203 if (constant_p)
6204 return build_vector_from_ctor (type,
6205 CONSTRUCTOR_ELTS (inside_init));
6209 if (warn_sequence_point)
6210 verify_sequence_points (inside_init);
6212 /* Any type can be initialized
6213 from an expression of the same type, optionally with braces. */
6215 if (inside_init && TREE_TYPE (inside_init) != 0
6216 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6217 TYPE_MAIN_VARIANT (type))
6218 || (code == ARRAY_TYPE
6219 && comptypes (TREE_TYPE (inside_init), type))
6220 || (code == VECTOR_TYPE
6221 && comptypes (TREE_TYPE (inside_init), type))
6222 || (code == POINTER_TYPE
6223 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6224 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6225 TREE_TYPE (type)))))
6227 if (code == POINTER_TYPE)
6229 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6231 if (TREE_CODE (inside_init) == STRING_CST
6232 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6233 inside_init = array_to_pointer_conversion
6234 (init_loc, inside_init);
6235 else
6237 error_init ("invalid use of non-lvalue array");
6238 return error_mark_node;
6243 if (code == VECTOR_TYPE)
6244 /* Although the types are compatible, we may require a
6245 conversion. */
6246 inside_init = convert (type, inside_init);
6248 if (require_constant
6249 && (code == VECTOR_TYPE || !flag_isoc99)
6250 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6252 /* As an extension, allow initializing objects with static storage
6253 duration with compound literals (which are then treated just as
6254 the brace enclosed list they contain). Also allow this for
6255 vectors, as we can only assign them with compound literals. */
6256 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6257 inside_init = DECL_INITIAL (decl);
6260 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6261 && TREE_CODE (inside_init) != CONSTRUCTOR)
6263 error_init ("array initialized from non-constant array expression");
6264 return error_mark_node;
6267 /* Compound expressions can only occur here if -pedantic or
6268 -pedantic-errors is specified. In the later case, we always want
6269 an error. In the former case, we simply want a warning. */
6270 if (require_constant && pedantic
6271 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6273 inside_init
6274 = valid_compound_expr_initializer (inside_init,
6275 TREE_TYPE (inside_init));
6276 if (inside_init == error_mark_node)
6277 error_init ("initializer element is not constant");
6278 else
6279 pedwarn_init (init_loc, OPT_pedantic,
6280 "initializer element is not constant");
6281 if (flag_pedantic_errors)
6282 inside_init = error_mark_node;
6284 else if (require_constant
6285 && !initializer_constant_valid_p (inside_init,
6286 TREE_TYPE (inside_init)))
6288 error_init ("initializer element is not constant");
6289 inside_init = error_mark_node;
6291 else if (require_constant && !maybe_const)
6292 pedwarn_init (init_loc, 0,
6293 "initializer element is not a constant expression");
6295 /* Added to enable additional -Wmissing-format-attribute warnings. */
6296 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6297 inside_init = convert_for_assignment (init_loc, type, inside_init,
6298 origtype,
6299 ic_init, null_pointer_constant,
6300 NULL_TREE, NULL_TREE, 0);
6301 return inside_init;
6304 /* Handle scalar types, including conversions. */
6306 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6307 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6308 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6310 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6311 && (TREE_CODE (init) == STRING_CST
6312 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6313 inside_init = init = array_to_pointer_conversion (init_loc, init);
6314 if (semantic_type)
6315 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6316 inside_init);
6317 inside_init
6318 = convert_for_assignment (init_loc, type, inside_init, origtype,
6319 ic_init, null_pointer_constant,
6320 NULL_TREE, NULL_TREE, 0);
6322 /* Check to see if we have already given an error message. */
6323 if (inside_init == error_mark_node)
6325 else if (require_constant && !TREE_CONSTANT (inside_init))
6327 error_init ("initializer element is not constant");
6328 inside_init = error_mark_node;
6330 else if (require_constant
6331 && !initializer_constant_valid_p (inside_init,
6332 TREE_TYPE (inside_init)))
6334 error_init ("initializer element is not computable at load time");
6335 inside_init = error_mark_node;
6337 else if (require_constant && !maybe_const)
6338 pedwarn_init (init_loc, 0,
6339 "initializer element is not a constant expression");
6341 return inside_init;
6344 /* Come here only for records and arrays. */
6346 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6348 error_init ("variable-sized object may not be initialized");
6349 return error_mark_node;
6352 error_init ("invalid initializer");
6353 return error_mark_node;
6356 /* Handle initializers that use braces. */
6358 /* Type of object we are accumulating a constructor for.
6359 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6360 static tree constructor_type;
6362 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6363 left to fill. */
6364 static tree constructor_fields;
6366 /* For an ARRAY_TYPE, this is the specified index
6367 at which to store the next element we get. */
6368 static tree constructor_index;
6370 /* For an ARRAY_TYPE, this is the maximum index. */
6371 static tree constructor_max_index;
6373 /* For a RECORD_TYPE, this is the first field not yet written out. */
6374 static tree constructor_unfilled_fields;
6376 /* For an ARRAY_TYPE, this is the index of the first element
6377 not yet written out. */
6378 static tree constructor_unfilled_index;
6380 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6381 This is so we can generate gaps between fields, when appropriate. */
6382 static tree constructor_bit_index;
6384 /* If we are saving up the elements rather than allocating them,
6385 this is the list of elements so far (in reverse order,
6386 most recent first). */
6387 static VEC(constructor_elt,gc) *constructor_elements;
6389 /* 1 if constructor should be incrementally stored into a constructor chain,
6390 0 if all the elements should be kept in AVL tree. */
6391 static int constructor_incremental;
6393 /* 1 if so far this constructor's elements are all compile-time constants. */
6394 static int constructor_constant;
6396 /* 1 if so far this constructor's elements are all valid address constants. */
6397 static int constructor_simple;
6399 /* 1 if this constructor has an element that cannot be part of a
6400 constant expression. */
6401 static int constructor_nonconst;
6403 /* 1 if this constructor is erroneous so far. */
6404 static int constructor_erroneous;
6406 /* Structure for managing pending initializer elements, organized as an
6407 AVL tree. */
6409 struct init_node
6411 struct init_node *left, *right;
6412 struct init_node *parent;
6413 int balance;
6414 tree purpose;
6415 tree value;
6416 tree origtype;
6419 /* Tree of pending elements at this constructor level.
6420 These are elements encountered out of order
6421 which belong at places we haven't reached yet in actually
6422 writing the output.
6423 Will never hold tree nodes across GC runs. */
6424 static struct init_node *constructor_pending_elts;
6426 /* The SPELLING_DEPTH of this constructor. */
6427 static int constructor_depth;
6429 /* DECL node for which an initializer is being read.
6430 0 means we are reading a constructor expression
6431 such as (struct foo) {...}. */
6432 static tree constructor_decl;
6434 /* Nonzero if this is an initializer for a top-level decl. */
6435 static int constructor_top_level;
6437 /* Nonzero if there were any member designators in this initializer. */
6438 static int constructor_designated;
6440 /* Nesting depth of designator list. */
6441 static int designator_depth;
6443 /* Nonzero if there were diagnosed errors in this designator list. */
6444 static int designator_erroneous;
6447 /* This stack has a level for each implicit or explicit level of
6448 structuring in the initializer, including the outermost one. It
6449 saves the values of most of the variables above. */
6451 struct constructor_range_stack;
6453 struct constructor_stack
6455 struct constructor_stack *next;
6456 tree type;
6457 tree fields;
6458 tree index;
6459 tree max_index;
6460 tree unfilled_index;
6461 tree unfilled_fields;
6462 tree bit_index;
6463 VEC(constructor_elt,gc) *elements;
6464 struct init_node *pending_elts;
6465 int offset;
6466 int depth;
6467 /* If value nonzero, this value should replace the entire
6468 constructor at this level. */
6469 struct c_expr replacement_value;
6470 struct constructor_range_stack *range_stack;
6471 char constant;
6472 char simple;
6473 char nonconst;
6474 char implicit;
6475 char erroneous;
6476 char outer;
6477 char incremental;
6478 char designated;
6481 static struct constructor_stack *constructor_stack;
6483 /* This stack represents designators from some range designator up to
6484 the last designator in the list. */
6486 struct constructor_range_stack
6488 struct constructor_range_stack *next, *prev;
6489 struct constructor_stack *stack;
6490 tree range_start;
6491 tree index;
6492 tree range_end;
6493 tree fields;
6496 static struct constructor_range_stack *constructor_range_stack;
6498 /* This stack records separate initializers that are nested.
6499 Nested initializers can't happen in ANSI C, but GNU C allows them
6500 in cases like { ... (struct foo) { ... } ... }. */
6502 struct initializer_stack
6504 struct initializer_stack *next;
6505 tree decl;
6506 struct constructor_stack *constructor_stack;
6507 struct constructor_range_stack *constructor_range_stack;
6508 VEC(constructor_elt,gc) *elements;
6509 struct spelling *spelling;
6510 struct spelling *spelling_base;
6511 int spelling_size;
6512 char top_level;
6513 char require_constant_value;
6514 char require_constant_elements;
6517 static struct initializer_stack *initializer_stack;
6519 /* Prepare to parse and output the initializer for variable DECL. */
6521 void
6522 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6524 const char *locus;
6525 struct initializer_stack *p = XNEW (struct initializer_stack);
6527 p->decl = constructor_decl;
6528 p->require_constant_value = require_constant_value;
6529 p->require_constant_elements = require_constant_elements;
6530 p->constructor_stack = constructor_stack;
6531 p->constructor_range_stack = constructor_range_stack;
6532 p->elements = constructor_elements;
6533 p->spelling = spelling;
6534 p->spelling_base = spelling_base;
6535 p->spelling_size = spelling_size;
6536 p->top_level = constructor_top_level;
6537 p->next = initializer_stack;
6538 initializer_stack = p;
6540 constructor_decl = decl;
6541 constructor_designated = 0;
6542 constructor_top_level = top_level;
6544 if (decl != 0 && decl != error_mark_node)
6546 require_constant_value = TREE_STATIC (decl);
6547 require_constant_elements
6548 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6549 /* For a scalar, you can always use any value to initialize,
6550 even within braces. */
6551 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6552 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6553 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6554 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6555 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6557 else
6559 require_constant_value = 0;
6560 require_constant_elements = 0;
6561 locus = _("(anonymous)");
6564 constructor_stack = 0;
6565 constructor_range_stack = 0;
6567 missing_braces_mentioned = 0;
6569 spelling_base = 0;
6570 spelling_size = 0;
6571 RESTORE_SPELLING_DEPTH (0);
6573 if (locus)
6574 push_string (locus);
6577 void
6578 finish_init (void)
6580 struct initializer_stack *p = initializer_stack;
6582 /* Free the whole constructor stack of this initializer. */
6583 while (constructor_stack)
6585 struct constructor_stack *q = constructor_stack;
6586 constructor_stack = q->next;
6587 free (q);
6590 gcc_assert (!constructor_range_stack);
6592 /* Pop back to the data of the outer initializer (if any). */
6593 free (spelling_base);
6595 constructor_decl = p->decl;
6596 require_constant_value = p->require_constant_value;
6597 require_constant_elements = p->require_constant_elements;
6598 constructor_stack = p->constructor_stack;
6599 constructor_range_stack = p->constructor_range_stack;
6600 constructor_elements = p->elements;
6601 spelling = p->spelling;
6602 spelling_base = p->spelling_base;
6603 spelling_size = p->spelling_size;
6604 constructor_top_level = p->top_level;
6605 initializer_stack = p->next;
6606 free (p);
6609 /* Call here when we see the initializer is surrounded by braces.
6610 This is instead of a call to push_init_level;
6611 it is matched by a call to pop_init_level.
6613 TYPE is the type to initialize, for a constructor expression.
6614 For an initializer for a decl, TYPE is zero. */
6616 void
6617 really_start_incremental_init (tree type)
6619 struct constructor_stack *p = XNEW (struct constructor_stack);
6621 if (type == 0)
6622 type = TREE_TYPE (constructor_decl);
6624 if (TREE_CODE (type) == VECTOR_TYPE
6625 && TYPE_VECTOR_OPAQUE (type))
6626 error ("opaque vector types cannot be initialized");
6628 p->type = constructor_type;
6629 p->fields = constructor_fields;
6630 p->index = constructor_index;
6631 p->max_index = constructor_max_index;
6632 p->unfilled_index = constructor_unfilled_index;
6633 p->unfilled_fields = constructor_unfilled_fields;
6634 p->bit_index = constructor_bit_index;
6635 p->elements = constructor_elements;
6636 p->constant = constructor_constant;
6637 p->simple = constructor_simple;
6638 p->nonconst = constructor_nonconst;
6639 p->erroneous = constructor_erroneous;
6640 p->pending_elts = constructor_pending_elts;
6641 p->depth = constructor_depth;
6642 p->replacement_value.value = 0;
6643 p->replacement_value.original_code = ERROR_MARK;
6644 p->replacement_value.original_type = NULL;
6645 p->implicit = 0;
6646 p->range_stack = 0;
6647 p->outer = 0;
6648 p->incremental = constructor_incremental;
6649 p->designated = constructor_designated;
6650 p->next = 0;
6651 constructor_stack = p;
6653 constructor_constant = 1;
6654 constructor_simple = 1;
6655 constructor_nonconst = 0;
6656 constructor_depth = SPELLING_DEPTH ();
6657 constructor_elements = 0;
6658 constructor_pending_elts = 0;
6659 constructor_type = type;
6660 constructor_incremental = 1;
6661 constructor_designated = 0;
6662 designator_depth = 0;
6663 designator_erroneous = 0;
6665 if (TREE_CODE (constructor_type) == RECORD_TYPE
6666 || TREE_CODE (constructor_type) == UNION_TYPE)
6668 constructor_fields = TYPE_FIELDS (constructor_type);
6669 /* Skip any nameless bit fields at the beginning. */
6670 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6671 && DECL_NAME (constructor_fields) == 0)
6672 constructor_fields = DECL_CHAIN (constructor_fields);
6674 constructor_unfilled_fields = constructor_fields;
6675 constructor_bit_index = bitsize_zero_node;
6677 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6679 if (TYPE_DOMAIN (constructor_type))
6681 constructor_max_index
6682 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6684 /* Detect non-empty initializations of zero-length arrays. */
6685 if (constructor_max_index == NULL_TREE
6686 && TYPE_SIZE (constructor_type))
6687 constructor_max_index = integer_minus_one_node;
6689 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6690 to initialize VLAs will cause a proper error; avoid tree
6691 checking errors as well by setting a safe value. */
6692 if (constructor_max_index
6693 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6694 constructor_max_index = integer_minus_one_node;
6696 constructor_index
6697 = convert (bitsizetype,
6698 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6700 else
6702 constructor_index = bitsize_zero_node;
6703 constructor_max_index = NULL_TREE;
6706 constructor_unfilled_index = constructor_index;
6708 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6710 /* Vectors are like simple fixed-size arrays. */
6711 constructor_max_index =
6712 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6713 constructor_index = bitsize_zero_node;
6714 constructor_unfilled_index = constructor_index;
6716 else
6718 /* Handle the case of int x = {5}; */
6719 constructor_fields = constructor_type;
6720 constructor_unfilled_fields = constructor_type;
6724 /* Push down into a subobject, for initialization.
6725 If this is for an explicit set of braces, IMPLICIT is 0.
6726 If it is because the next element belongs at a lower level,
6727 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6729 void
6730 push_init_level (int implicit, struct obstack * braced_init_obstack)
6732 struct constructor_stack *p;
6733 tree value = NULL_TREE;
6735 /* If we've exhausted any levels that didn't have braces,
6736 pop them now. If implicit == 1, this will have been done in
6737 process_init_element; do not repeat it here because in the case
6738 of excess initializers for an empty aggregate this leads to an
6739 infinite cycle of popping a level and immediately recreating
6740 it. */
6741 if (implicit != 1)
6743 while (constructor_stack->implicit)
6745 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6746 || TREE_CODE (constructor_type) == UNION_TYPE)
6747 && constructor_fields == 0)
6748 process_init_element (pop_init_level (1, braced_init_obstack),
6749 true, braced_init_obstack);
6750 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6751 && constructor_max_index
6752 && tree_int_cst_lt (constructor_max_index,
6753 constructor_index))
6754 process_init_element (pop_init_level (1, braced_init_obstack),
6755 true, braced_init_obstack);
6756 else
6757 break;
6761 /* Unless this is an explicit brace, we need to preserve previous
6762 content if any. */
6763 if (implicit)
6765 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6766 || TREE_CODE (constructor_type) == UNION_TYPE)
6767 && constructor_fields)
6768 value = find_init_member (constructor_fields, braced_init_obstack);
6769 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6770 value = find_init_member (constructor_index, braced_init_obstack);
6773 p = XNEW (struct constructor_stack);
6774 p->type = constructor_type;
6775 p->fields = constructor_fields;
6776 p->index = constructor_index;
6777 p->max_index = constructor_max_index;
6778 p->unfilled_index = constructor_unfilled_index;
6779 p->unfilled_fields = constructor_unfilled_fields;
6780 p->bit_index = constructor_bit_index;
6781 p->elements = constructor_elements;
6782 p->constant = constructor_constant;
6783 p->simple = constructor_simple;
6784 p->nonconst = constructor_nonconst;
6785 p->erroneous = constructor_erroneous;
6786 p->pending_elts = constructor_pending_elts;
6787 p->depth = constructor_depth;
6788 p->replacement_value.value = 0;
6789 p->replacement_value.original_code = ERROR_MARK;
6790 p->replacement_value.original_type = NULL;
6791 p->implicit = implicit;
6792 p->outer = 0;
6793 p->incremental = constructor_incremental;
6794 p->designated = constructor_designated;
6795 p->next = constructor_stack;
6796 p->range_stack = 0;
6797 constructor_stack = p;
6799 constructor_constant = 1;
6800 constructor_simple = 1;
6801 constructor_nonconst = 0;
6802 constructor_depth = SPELLING_DEPTH ();
6803 constructor_elements = 0;
6804 constructor_incremental = 1;
6805 constructor_designated = 0;
6806 constructor_pending_elts = 0;
6807 if (!implicit)
6809 p->range_stack = constructor_range_stack;
6810 constructor_range_stack = 0;
6811 designator_depth = 0;
6812 designator_erroneous = 0;
6815 /* Don't die if an entire brace-pair level is superfluous
6816 in the containing level. */
6817 if (constructor_type == 0)
6819 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6820 || TREE_CODE (constructor_type) == UNION_TYPE)
6822 /* Don't die if there are extra init elts at the end. */
6823 if (constructor_fields == 0)
6824 constructor_type = 0;
6825 else
6827 constructor_type = TREE_TYPE (constructor_fields);
6828 push_member_name (constructor_fields);
6829 constructor_depth++;
6832 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6834 constructor_type = TREE_TYPE (constructor_type);
6835 push_array_bounds (tree_low_cst (constructor_index, 1));
6836 constructor_depth++;
6839 if (constructor_type == 0)
6841 error_init ("extra brace group at end of initializer");
6842 constructor_fields = 0;
6843 constructor_unfilled_fields = 0;
6844 return;
6847 if (value && TREE_CODE (value) == CONSTRUCTOR)
6849 constructor_constant = TREE_CONSTANT (value);
6850 constructor_simple = TREE_STATIC (value);
6851 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6852 constructor_elements = CONSTRUCTOR_ELTS (value);
6853 if (!VEC_empty (constructor_elt, constructor_elements)
6854 && (TREE_CODE (constructor_type) == RECORD_TYPE
6855 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6856 set_nonincremental_init (braced_init_obstack);
6859 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6861 missing_braces_mentioned = 1;
6862 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6865 if (TREE_CODE (constructor_type) == RECORD_TYPE
6866 || TREE_CODE (constructor_type) == UNION_TYPE)
6868 constructor_fields = TYPE_FIELDS (constructor_type);
6869 /* Skip any nameless bit fields at the beginning. */
6870 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6871 && DECL_NAME (constructor_fields) == 0)
6872 constructor_fields = DECL_CHAIN (constructor_fields);
6874 constructor_unfilled_fields = constructor_fields;
6875 constructor_bit_index = bitsize_zero_node;
6877 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6879 /* Vectors are like simple fixed-size arrays. */
6880 constructor_max_index =
6881 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6882 constructor_index = convert (bitsizetype, integer_zero_node);
6883 constructor_unfilled_index = constructor_index;
6885 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6887 if (TYPE_DOMAIN (constructor_type))
6889 constructor_max_index
6890 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6892 /* Detect non-empty initializations of zero-length arrays. */
6893 if (constructor_max_index == NULL_TREE
6894 && TYPE_SIZE (constructor_type))
6895 constructor_max_index = integer_minus_one_node;
6897 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6898 to initialize VLAs will cause a proper error; avoid tree
6899 checking errors as well by setting a safe value. */
6900 if (constructor_max_index
6901 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6902 constructor_max_index = integer_minus_one_node;
6904 constructor_index
6905 = convert (bitsizetype,
6906 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6908 else
6909 constructor_index = bitsize_zero_node;
6911 constructor_unfilled_index = constructor_index;
6912 if (value && TREE_CODE (value) == STRING_CST)
6914 /* We need to split the char/wchar array into individual
6915 characters, so that we don't have to special case it
6916 everywhere. */
6917 set_nonincremental_init_from_string (value, braced_init_obstack);
6920 else
6922 if (constructor_type != error_mark_node)
6923 warning_init (0, "braces around scalar initializer");
6924 constructor_fields = constructor_type;
6925 constructor_unfilled_fields = constructor_type;
6929 /* At the end of an implicit or explicit brace level,
6930 finish up that level of constructor. If a single expression
6931 with redundant braces initialized that level, return the
6932 c_expr structure for that expression. Otherwise, the original_code
6933 element is set to ERROR_MARK.
6934 If we were outputting the elements as they are read, return 0 as the value
6935 from inner levels (process_init_element ignores that),
6936 but return error_mark_node as the value from the outermost level
6937 (that's what we want to put in DECL_INITIAL).
6938 Otherwise, return a CONSTRUCTOR expression as the value. */
6940 struct c_expr
6941 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6943 struct constructor_stack *p;
6944 struct c_expr ret;
6945 ret.value = 0;
6946 ret.original_code = ERROR_MARK;
6947 ret.original_type = NULL;
6949 if (implicit == 0)
6951 /* When we come to an explicit close brace,
6952 pop any inner levels that didn't have explicit braces. */
6953 while (constructor_stack->implicit)
6955 process_init_element (pop_init_level (1, braced_init_obstack),
6956 true, braced_init_obstack);
6958 gcc_assert (!constructor_range_stack);
6961 /* Now output all pending elements. */
6962 constructor_incremental = 1;
6963 output_pending_init_elements (1, braced_init_obstack);
6965 p = constructor_stack;
6967 /* Error for initializing a flexible array member, or a zero-length
6968 array member in an inappropriate context. */
6969 if (constructor_type && constructor_fields
6970 && TREE_CODE (constructor_type) == ARRAY_TYPE
6971 && TYPE_DOMAIN (constructor_type)
6972 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6974 /* Silently discard empty initializations. The parser will
6975 already have pedwarned for empty brackets. */
6976 if (integer_zerop (constructor_unfilled_index))
6977 constructor_type = NULL_TREE;
6978 else
6980 gcc_assert (!TYPE_SIZE (constructor_type));
6982 if (constructor_depth > 2)
6983 error_init ("initialization of flexible array member in a nested context");
6984 else
6985 pedwarn_init (input_location, OPT_pedantic,
6986 "initialization of a flexible array member");
6988 /* We have already issued an error message for the existence
6989 of a flexible array member not at the end of the structure.
6990 Discard the initializer so that we do not die later. */
6991 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6992 constructor_type = NULL_TREE;
6996 /* Warn when some struct elements are implicitly initialized to zero. */
6997 if (warn_missing_field_initializers
6998 && constructor_type
6999 && TREE_CODE (constructor_type) == RECORD_TYPE
7000 && constructor_unfilled_fields)
7002 /* Do not warn for flexible array members or zero-length arrays. */
7003 while (constructor_unfilled_fields
7004 && (!DECL_SIZE (constructor_unfilled_fields)
7005 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7006 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7008 /* Do not warn if this level of the initializer uses member
7009 designators; it is likely to be deliberate. */
7010 if (constructor_unfilled_fields && !constructor_designated)
7012 push_member_name (constructor_unfilled_fields);
7013 warning_init (OPT_Wmissing_field_initializers,
7014 "missing initializer");
7015 RESTORE_SPELLING_DEPTH (constructor_depth);
7019 /* Pad out the end of the structure. */
7020 if (p->replacement_value.value)
7021 /* If this closes a superfluous brace pair,
7022 just pass out the element between them. */
7023 ret = p->replacement_value;
7024 else if (constructor_type == 0)
7026 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7027 && TREE_CODE (constructor_type) != UNION_TYPE
7028 && TREE_CODE (constructor_type) != ARRAY_TYPE
7029 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7031 /* A nonincremental scalar initializer--just return
7032 the element, after verifying there is just one. */
7033 if (VEC_empty (constructor_elt,constructor_elements))
7035 if (!constructor_erroneous)
7036 error_init ("empty scalar initializer");
7037 ret.value = error_mark_node;
7039 else if (VEC_length (constructor_elt,constructor_elements) != 1)
7041 error_init ("extra elements in scalar initializer");
7042 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7044 else
7045 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7047 else
7049 if (constructor_erroneous)
7050 ret.value = error_mark_node;
7051 else
7053 ret.value = build_constructor (constructor_type,
7054 constructor_elements);
7055 if (constructor_constant)
7056 TREE_CONSTANT (ret.value) = 1;
7057 if (constructor_constant && constructor_simple)
7058 TREE_STATIC (ret.value) = 1;
7059 if (constructor_nonconst)
7060 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7064 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7066 if (constructor_nonconst)
7067 ret.original_code = C_MAYBE_CONST_EXPR;
7068 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7069 ret.original_code = ERROR_MARK;
7072 constructor_type = p->type;
7073 constructor_fields = p->fields;
7074 constructor_index = p->index;
7075 constructor_max_index = p->max_index;
7076 constructor_unfilled_index = p->unfilled_index;
7077 constructor_unfilled_fields = p->unfilled_fields;
7078 constructor_bit_index = p->bit_index;
7079 constructor_elements = p->elements;
7080 constructor_constant = p->constant;
7081 constructor_simple = p->simple;
7082 constructor_nonconst = p->nonconst;
7083 constructor_erroneous = p->erroneous;
7084 constructor_incremental = p->incremental;
7085 constructor_designated = p->designated;
7086 constructor_pending_elts = p->pending_elts;
7087 constructor_depth = p->depth;
7088 if (!p->implicit)
7089 constructor_range_stack = p->range_stack;
7090 RESTORE_SPELLING_DEPTH (constructor_depth);
7092 constructor_stack = p->next;
7093 free (p);
7095 if (ret.value == 0 && constructor_stack == 0)
7096 ret.value = error_mark_node;
7097 return ret;
7100 /* Common handling for both array range and field name designators.
7101 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7103 static int
7104 set_designator (int array, struct obstack * braced_init_obstack)
7106 tree subtype;
7107 enum tree_code subcode;
7109 /* Don't die if an entire brace-pair level is superfluous
7110 in the containing level. */
7111 if (constructor_type == 0)
7112 return 1;
7114 /* If there were errors in this designator list already, bail out
7115 silently. */
7116 if (designator_erroneous)
7117 return 1;
7119 if (!designator_depth)
7121 gcc_assert (!constructor_range_stack);
7123 /* Designator list starts at the level of closest explicit
7124 braces. */
7125 while (constructor_stack->implicit)
7127 process_init_element (pop_init_level (1, braced_init_obstack),
7128 true, braced_init_obstack);
7130 constructor_designated = 1;
7131 return 0;
7134 switch (TREE_CODE (constructor_type))
7136 case RECORD_TYPE:
7137 case UNION_TYPE:
7138 subtype = TREE_TYPE (constructor_fields);
7139 if (subtype != error_mark_node)
7140 subtype = TYPE_MAIN_VARIANT (subtype);
7141 break;
7142 case ARRAY_TYPE:
7143 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7144 break;
7145 default:
7146 gcc_unreachable ();
7149 subcode = TREE_CODE (subtype);
7150 if (array && subcode != ARRAY_TYPE)
7152 error_init ("array index in non-array initializer");
7153 return 1;
7155 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7157 error_init ("field name not in record or union initializer");
7158 return 1;
7161 constructor_designated = 1;
7162 push_init_level (2, braced_init_obstack);
7163 return 0;
7166 /* If there are range designators in designator list, push a new designator
7167 to constructor_range_stack. RANGE_END is end of such stack range or
7168 NULL_TREE if there is no range designator at this level. */
7170 static void
7171 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7173 struct constructor_range_stack *p;
7175 p = (struct constructor_range_stack *)
7176 obstack_alloc (braced_init_obstack,
7177 sizeof (struct constructor_range_stack));
7178 p->prev = constructor_range_stack;
7179 p->next = 0;
7180 p->fields = constructor_fields;
7181 p->range_start = constructor_index;
7182 p->index = constructor_index;
7183 p->stack = constructor_stack;
7184 p->range_end = range_end;
7185 if (constructor_range_stack)
7186 constructor_range_stack->next = p;
7187 constructor_range_stack = p;
7190 /* Within an array initializer, specify the next index to be initialized.
7191 FIRST is that index. If LAST is nonzero, then initialize a range
7192 of indices, running from FIRST through LAST. */
7194 void
7195 set_init_index (tree first, tree last,
7196 struct obstack * braced_init_obstack)
7198 if (set_designator (1, braced_init_obstack))
7199 return;
7201 designator_erroneous = 1;
7203 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7204 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7206 error_init ("array index in initializer not of integer type");
7207 return;
7210 if (TREE_CODE (first) != INTEGER_CST)
7212 first = c_fully_fold (first, false, NULL);
7213 if (TREE_CODE (first) == INTEGER_CST)
7214 pedwarn_init (input_location, OPT_pedantic,
7215 "array index in initializer is not "
7216 "an integer constant expression");
7219 if (last && TREE_CODE (last) != INTEGER_CST)
7221 last = c_fully_fold (last, false, NULL);
7222 if (TREE_CODE (last) == INTEGER_CST)
7223 pedwarn_init (input_location, OPT_pedantic,
7224 "array index in initializer is not "
7225 "an integer constant expression");
7228 if (TREE_CODE (first) != INTEGER_CST)
7229 error_init ("nonconstant array index in initializer");
7230 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7231 error_init ("nonconstant array index in initializer");
7232 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7233 error_init ("array index in non-array initializer");
7234 else if (tree_int_cst_sgn (first) == -1)
7235 error_init ("array index in initializer exceeds array bounds");
7236 else if (constructor_max_index
7237 && tree_int_cst_lt (constructor_max_index, first))
7238 error_init ("array index in initializer exceeds array bounds");
7239 else
7241 constant_expression_warning (first);
7242 if (last)
7243 constant_expression_warning (last);
7244 constructor_index = convert (bitsizetype, first);
7246 if (last)
7248 if (tree_int_cst_equal (first, last))
7249 last = 0;
7250 else if (tree_int_cst_lt (last, first))
7252 error_init ("empty index range in initializer");
7253 last = 0;
7255 else
7257 last = convert (bitsizetype, last);
7258 if (constructor_max_index != 0
7259 && tree_int_cst_lt (constructor_max_index, last))
7261 error_init ("array index range in initializer exceeds array bounds");
7262 last = 0;
7267 designator_depth++;
7268 designator_erroneous = 0;
7269 if (constructor_range_stack || last)
7270 push_range_stack (last, braced_init_obstack);
7274 /* Within a struct initializer, specify the next field to be initialized. */
7276 void
7277 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7279 tree field;
7281 if (set_designator (0, braced_init_obstack))
7282 return;
7284 designator_erroneous = 1;
7286 if (TREE_CODE (constructor_type) != RECORD_TYPE
7287 && TREE_CODE (constructor_type) != UNION_TYPE)
7289 error_init ("field name not in record or union initializer");
7290 return;
7293 field = lookup_field (constructor_type, fieldname);
7295 if (field == 0)
7296 error ("unknown field %qE specified in initializer", fieldname);
7297 else
7300 constructor_fields = TREE_VALUE (field);
7301 designator_depth++;
7302 designator_erroneous = 0;
7303 if (constructor_range_stack)
7304 push_range_stack (NULL_TREE, braced_init_obstack);
7305 field = TREE_CHAIN (field);
7306 if (field)
7308 if (set_designator (0, braced_init_obstack))
7309 return;
7312 while (field != NULL_TREE);
7315 /* Add a new initializer to the tree of pending initializers. PURPOSE
7316 identifies the initializer, either array index or field in a structure.
7317 VALUE is the value of that index or field. If ORIGTYPE is not
7318 NULL_TREE, it is the original type of VALUE.
7320 IMPLICIT is true if value comes from pop_init_level (1),
7321 the new initializer has been merged with the existing one
7322 and thus no warnings should be emitted about overriding an
7323 existing initializer. */
7325 static void
7326 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7327 struct obstack * braced_init_obstack)
7329 struct init_node *p, **q, *r;
7331 q = &constructor_pending_elts;
7332 p = 0;
7334 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7336 while (*q != 0)
7338 p = *q;
7339 if (tree_int_cst_lt (purpose, p->purpose))
7340 q = &p->left;
7341 else if (tree_int_cst_lt (p->purpose, purpose))
7342 q = &p->right;
7343 else
7345 if (!implicit)
7347 if (TREE_SIDE_EFFECTS (p->value))
7348 warning_init (0, "initialized field with side-effects overwritten");
7349 else if (warn_override_init)
7350 warning_init (OPT_Woverride_init, "initialized field overwritten");
7352 p->value = value;
7353 p->origtype = origtype;
7354 return;
7358 else
7360 tree bitpos;
7362 bitpos = bit_position (purpose);
7363 while (*q != NULL)
7365 p = *q;
7366 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7367 q = &p->left;
7368 else if (p->purpose != purpose)
7369 q = &p->right;
7370 else
7372 if (!implicit)
7374 if (TREE_SIDE_EFFECTS (p->value))
7375 warning_init (0, "initialized field with side-effects overwritten");
7376 else if (warn_override_init)
7377 warning_init (OPT_Woverride_init, "initialized field overwritten");
7379 p->value = value;
7380 p->origtype = origtype;
7381 return;
7386 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7387 sizeof (struct init_node));
7388 r->purpose = purpose;
7389 r->value = value;
7390 r->origtype = origtype;
7392 *q = r;
7393 r->parent = p;
7394 r->left = 0;
7395 r->right = 0;
7396 r->balance = 0;
7398 while (p)
7400 struct init_node *s;
7402 if (r == p->left)
7404 if (p->balance == 0)
7405 p->balance = -1;
7406 else if (p->balance < 0)
7408 if (r->balance < 0)
7410 /* L rotation. */
7411 p->left = r->right;
7412 if (p->left)
7413 p->left->parent = p;
7414 r->right = p;
7416 p->balance = 0;
7417 r->balance = 0;
7419 s = p->parent;
7420 p->parent = r;
7421 r->parent = s;
7422 if (s)
7424 if (s->left == p)
7425 s->left = r;
7426 else
7427 s->right = r;
7429 else
7430 constructor_pending_elts = r;
7432 else
7434 /* LR rotation. */
7435 struct init_node *t = r->right;
7437 r->right = t->left;
7438 if (r->right)
7439 r->right->parent = r;
7440 t->left = r;
7442 p->left = t->right;
7443 if (p->left)
7444 p->left->parent = p;
7445 t->right = p;
7447 p->balance = t->balance < 0;
7448 r->balance = -(t->balance > 0);
7449 t->balance = 0;
7451 s = p->parent;
7452 p->parent = t;
7453 r->parent = t;
7454 t->parent = s;
7455 if (s)
7457 if (s->left == p)
7458 s->left = t;
7459 else
7460 s->right = t;
7462 else
7463 constructor_pending_elts = t;
7465 break;
7467 else
7469 /* p->balance == +1; growth of left side balances the node. */
7470 p->balance = 0;
7471 break;
7474 else /* r == p->right */
7476 if (p->balance == 0)
7477 /* Growth propagation from right side. */
7478 p->balance++;
7479 else if (p->balance > 0)
7481 if (r->balance > 0)
7483 /* R rotation. */
7484 p->right = r->left;
7485 if (p->right)
7486 p->right->parent = p;
7487 r->left = p;
7489 p->balance = 0;
7490 r->balance = 0;
7492 s = p->parent;
7493 p->parent = r;
7494 r->parent = s;
7495 if (s)
7497 if (s->left == p)
7498 s->left = r;
7499 else
7500 s->right = r;
7502 else
7503 constructor_pending_elts = r;
7505 else /* r->balance == -1 */
7507 /* RL rotation */
7508 struct init_node *t = r->left;
7510 r->left = t->right;
7511 if (r->left)
7512 r->left->parent = r;
7513 t->right = r;
7515 p->right = t->left;
7516 if (p->right)
7517 p->right->parent = p;
7518 t->left = p;
7520 r->balance = (t->balance < 0);
7521 p->balance = -(t->balance > 0);
7522 t->balance = 0;
7524 s = p->parent;
7525 p->parent = t;
7526 r->parent = t;
7527 t->parent = s;
7528 if (s)
7530 if (s->left == p)
7531 s->left = t;
7532 else
7533 s->right = t;
7535 else
7536 constructor_pending_elts = t;
7538 break;
7540 else
7542 /* p->balance == -1; growth of right side balances the node. */
7543 p->balance = 0;
7544 break;
7548 r = p;
7549 p = p->parent;
7553 /* Build AVL tree from a sorted chain. */
7555 static void
7556 set_nonincremental_init (struct obstack * braced_init_obstack)
7558 unsigned HOST_WIDE_INT ix;
7559 tree index, value;
7561 if (TREE_CODE (constructor_type) != RECORD_TYPE
7562 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7563 return;
7565 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7567 add_pending_init (index, value, NULL_TREE, false,
7568 braced_init_obstack);
7570 constructor_elements = 0;
7571 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7573 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7574 /* Skip any nameless bit fields at the beginning. */
7575 while (constructor_unfilled_fields != 0
7576 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7577 && DECL_NAME (constructor_unfilled_fields) == 0)
7578 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7581 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7583 if (TYPE_DOMAIN (constructor_type))
7584 constructor_unfilled_index
7585 = convert (bitsizetype,
7586 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7587 else
7588 constructor_unfilled_index = bitsize_zero_node;
7590 constructor_incremental = 0;
7593 /* Build AVL tree from a string constant. */
7595 static void
7596 set_nonincremental_init_from_string (tree str,
7597 struct obstack * braced_init_obstack)
7599 tree value, purpose, type;
7600 HOST_WIDE_INT val[2];
7601 const char *p, *end;
7602 int byte, wchar_bytes, charwidth, bitpos;
7604 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7606 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7607 charwidth = TYPE_PRECISION (char_type_node);
7608 type = TREE_TYPE (constructor_type);
7609 p = TREE_STRING_POINTER (str);
7610 end = p + TREE_STRING_LENGTH (str);
7612 for (purpose = bitsize_zero_node;
7613 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7614 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7616 if (wchar_bytes == 1)
7618 val[1] = (unsigned char) *p++;
7619 val[0] = 0;
7621 else
7623 val[0] = 0;
7624 val[1] = 0;
7625 for (byte = 0; byte < wchar_bytes; byte++)
7627 if (BYTES_BIG_ENDIAN)
7628 bitpos = (wchar_bytes - byte - 1) * charwidth;
7629 else
7630 bitpos = byte * charwidth;
7631 val[bitpos < HOST_BITS_PER_WIDE_INT]
7632 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7633 << (bitpos % HOST_BITS_PER_WIDE_INT);
7637 if (!TYPE_UNSIGNED (type))
7639 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7640 if (bitpos < HOST_BITS_PER_WIDE_INT)
7642 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7644 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7645 val[0] = -1;
7648 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7650 if (val[1] < 0)
7651 val[0] = -1;
7653 else if (val[0] & (((HOST_WIDE_INT) 1)
7654 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7655 val[0] |= ((HOST_WIDE_INT) -1)
7656 << (bitpos - HOST_BITS_PER_WIDE_INT);
7659 value = build_int_cst_wide (type, val[1], val[0]);
7660 add_pending_init (purpose, value, NULL_TREE, false,
7661 braced_init_obstack);
7664 constructor_incremental = 0;
7667 /* Return value of FIELD in pending initializer or zero if the field was
7668 not initialized yet. */
7670 static tree
7671 find_init_member (tree field, struct obstack * braced_init_obstack)
7673 struct init_node *p;
7675 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7677 if (constructor_incremental
7678 && tree_int_cst_lt (field, constructor_unfilled_index))
7679 set_nonincremental_init (braced_init_obstack);
7681 p = constructor_pending_elts;
7682 while (p)
7684 if (tree_int_cst_lt (field, p->purpose))
7685 p = p->left;
7686 else if (tree_int_cst_lt (p->purpose, field))
7687 p = p->right;
7688 else
7689 return p->value;
7692 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7694 tree bitpos = bit_position (field);
7696 if (constructor_incremental
7697 && (!constructor_unfilled_fields
7698 || tree_int_cst_lt (bitpos,
7699 bit_position (constructor_unfilled_fields))))
7700 set_nonincremental_init (braced_init_obstack);
7702 p = constructor_pending_elts;
7703 while (p)
7705 if (field == p->purpose)
7706 return p->value;
7707 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7708 p = p->left;
7709 else
7710 p = p->right;
7713 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7715 if (!VEC_empty (constructor_elt, constructor_elements)
7716 && (VEC_last (constructor_elt, constructor_elements)->index
7717 == field))
7718 return VEC_last (constructor_elt, constructor_elements)->value;
7720 return 0;
7723 /* "Output" the next constructor element.
7724 At top level, really output it to assembler code now.
7725 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7726 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7727 TYPE is the data type that the containing data type wants here.
7728 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7729 If VALUE is a string constant, STRICT_STRING is true if it is
7730 unparenthesized or we should not warn here for it being parenthesized.
7731 For other types of VALUE, STRICT_STRING is not used.
7733 PENDING if non-nil means output pending elements that belong
7734 right after this element. (PENDING is normally 1;
7735 it is 0 while outputting pending elements, to avoid recursion.)
7737 IMPLICIT is true if value comes from pop_init_level (1),
7738 the new initializer has been merged with the existing one
7739 and thus no warnings should be emitted about overriding an
7740 existing initializer. */
7742 static void
7743 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7744 tree field, int pending, bool implicit,
7745 struct obstack * braced_init_obstack)
7747 tree semantic_type = NULL_TREE;
7748 constructor_elt *celt;
7749 bool maybe_const = true;
7750 bool npc;
7752 if (type == error_mark_node || value == error_mark_node)
7754 constructor_erroneous = 1;
7755 return;
7757 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7758 && (TREE_CODE (value) == STRING_CST
7759 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7760 && !(TREE_CODE (value) == STRING_CST
7761 && TREE_CODE (type) == ARRAY_TYPE
7762 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7763 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7764 TYPE_MAIN_VARIANT (type)))
7765 value = array_to_pointer_conversion (input_location, value);
7767 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7768 && require_constant_value && !flag_isoc99 && pending)
7770 /* As an extension, allow initializing objects with static storage
7771 duration with compound literals (which are then treated just as
7772 the brace enclosed list they contain). */
7773 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7774 value = DECL_INITIAL (decl);
7777 npc = null_pointer_constant_p (value);
7778 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7780 semantic_type = TREE_TYPE (value);
7781 value = TREE_OPERAND (value, 0);
7783 value = c_fully_fold (value, require_constant_value, &maybe_const);
7785 if (value == error_mark_node)
7786 constructor_erroneous = 1;
7787 else if (!TREE_CONSTANT (value))
7788 constructor_constant = 0;
7789 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7790 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7791 || TREE_CODE (constructor_type) == UNION_TYPE)
7792 && DECL_C_BIT_FIELD (field)
7793 && TREE_CODE (value) != INTEGER_CST))
7794 constructor_simple = 0;
7795 if (!maybe_const)
7796 constructor_nonconst = 1;
7798 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7800 if (require_constant_value)
7802 error_init ("initializer element is not constant");
7803 value = error_mark_node;
7805 else if (require_constant_elements)
7806 pedwarn (input_location, 0,
7807 "initializer element is not computable at load time");
7809 else if (!maybe_const
7810 && (require_constant_value || require_constant_elements))
7811 pedwarn_init (input_location, 0,
7812 "initializer element is not a constant expression");
7814 /* Issue -Wc++-compat warnings about initializing a bitfield with
7815 enum type. */
7816 if (warn_cxx_compat
7817 && field != NULL_TREE
7818 && TREE_CODE (field) == FIELD_DECL
7819 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7820 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7821 != TYPE_MAIN_VARIANT (type))
7822 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7824 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7825 if (checktype != error_mark_node
7826 && (TYPE_MAIN_VARIANT (checktype)
7827 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7828 warning_init (OPT_Wc___compat,
7829 "enum conversion in initialization is invalid in C++");
7832 /* If this field is empty (and not at the end of structure),
7833 don't do anything other than checking the initializer. */
7834 if (field
7835 && (TREE_TYPE (field) == error_mark_node
7836 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7837 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7838 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7839 || DECL_CHAIN (field)))))
7840 return;
7842 if (semantic_type)
7843 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7844 value = digest_init (input_location, type, value, origtype, npc,
7845 strict_string, require_constant_value);
7846 if (value == error_mark_node)
7848 constructor_erroneous = 1;
7849 return;
7851 if (require_constant_value || require_constant_elements)
7852 constant_expression_warning (value);
7854 /* If this element doesn't come next in sequence,
7855 put it on constructor_pending_elts. */
7856 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7857 && (!constructor_incremental
7858 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7860 if (constructor_incremental
7861 && tree_int_cst_lt (field, constructor_unfilled_index))
7862 set_nonincremental_init (braced_init_obstack);
7864 add_pending_init (field, value, origtype, implicit,
7865 braced_init_obstack);
7866 return;
7868 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7869 && (!constructor_incremental
7870 || field != constructor_unfilled_fields))
7872 /* We do this for records but not for unions. In a union,
7873 no matter which field is specified, it can be initialized
7874 right away since it starts at the beginning of the union. */
7875 if (constructor_incremental)
7877 if (!constructor_unfilled_fields)
7878 set_nonincremental_init (braced_init_obstack);
7879 else
7881 tree bitpos, unfillpos;
7883 bitpos = bit_position (field);
7884 unfillpos = bit_position (constructor_unfilled_fields);
7886 if (tree_int_cst_lt (bitpos, unfillpos))
7887 set_nonincremental_init (braced_init_obstack);
7891 add_pending_init (field, value, origtype, implicit,
7892 braced_init_obstack);
7893 return;
7895 else if (TREE_CODE (constructor_type) == UNION_TYPE
7896 && !VEC_empty (constructor_elt, constructor_elements))
7898 if (!implicit)
7900 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7901 constructor_elements)->value))
7902 warning_init (0,
7903 "initialized field with side-effects overwritten");
7904 else if (warn_override_init)
7905 warning_init (OPT_Woverride_init, "initialized field overwritten");
7908 /* We can have just one union field set. */
7909 constructor_elements = 0;
7912 /* Otherwise, output this element either to
7913 constructor_elements or to the assembler file. */
7915 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7916 celt->index = field;
7917 celt->value = value;
7919 /* Advance the variable that indicates sequential elements output. */
7920 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7921 constructor_unfilled_index
7922 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7923 bitsize_one_node);
7924 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7926 constructor_unfilled_fields
7927 = DECL_CHAIN (constructor_unfilled_fields);
7929 /* Skip any nameless bit fields. */
7930 while (constructor_unfilled_fields != 0
7931 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7932 && DECL_NAME (constructor_unfilled_fields) == 0)
7933 constructor_unfilled_fields =
7934 DECL_CHAIN (constructor_unfilled_fields);
7936 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7937 constructor_unfilled_fields = 0;
7939 /* Now output any pending elements which have become next. */
7940 if (pending)
7941 output_pending_init_elements (0, braced_init_obstack);
7944 /* Output any pending elements which have become next.
7945 As we output elements, constructor_unfilled_{fields,index}
7946 advances, which may cause other elements to become next;
7947 if so, they too are output.
7949 If ALL is 0, we return when there are
7950 no more pending elements to output now.
7952 If ALL is 1, we output space as necessary so that
7953 we can output all the pending elements. */
7954 static void
7955 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7957 struct init_node *elt = constructor_pending_elts;
7958 tree next;
7960 retry:
7962 /* Look through the whole pending tree.
7963 If we find an element that should be output now,
7964 output it. Otherwise, set NEXT to the element
7965 that comes first among those still pending. */
7967 next = 0;
7968 while (elt)
7970 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7972 if (tree_int_cst_equal (elt->purpose,
7973 constructor_unfilled_index))
7974 output_init_element (elt->value, elt->origtype, true,
7975 TREE_TYPE (constructor_type),
7976 constructor_unfilled_index, 0, false,
7977 braced_init_obstack);
7978 else if (tree_int_cst_lt (constructor_unfilled_index,
7979 elt->purpose))
7981 /* Advance to the next smaller node. */
7982 if (elt->left)
7983 elt = elt->left;
7984 else
7986 /* We have reached the smallest node bigger than the
7987 current unfilled index. Fill the space first. */
7988 next = elt->purpose;
7989 break;
7992 else
7994 /* Advance to the next bigger node. */
7995 if (elt->right)
7996 elt = elt->right;
7997 else
7999 /* We have reached the biggest node in a subtree. Find
8000 the parent of it, which is the next bigger node. */
8001 while (elt->parent && elt->parent->right == elt)
8002 elt = elt->parent;
8003 elt = elt->parent;
8004 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8005 elt->purpose))
8007 next = elt->purpose;
8008 break;
8013 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8014 || TREE_CODE (constructor_type) == UNION_TYPE)
8016 tree ctor_unfilled_bitpos, elt_bitpos;
8018 /* If the current record is complete we are done. */
8019 if (constructor_unfilled_fields == 0)
8020 break;
8022 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8023 elt_bitpos = bit_position (elt->purpose);
8024 /* We can't compare fields here because there might be empty
8025 fields in between. */
8026 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8028 constructor_unfilled_fields = elt->purpose;
8029 output_init_element (elt->value, elt->origtype, true,
8030 TREE_TYPE (elt->purpose),
8031 elt->purpose, 0, false,
8032 braced_init_obstack);
8034 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8036 /* Advance to the next smaller node. */
8037 if (elt->left)
8038 elt = elt->left;
8039 else
8041 /* We have reached the smallest node bigger than the
8042 current unfilled field. Fill the space first. */
8043 next = elt->purpose;
8044 break;
8047 else
8049 /* Advance to the next bigger node. */
8050 if (elt->right)
8051 elt = elt->right;
8052 else
8054 /* We have reached the biggest node in a subtree. Find
8055 the parent of it, which is the next bigger node. */
8056 while (elt->parent && elt->parent->right == elt)
8057 elt = elt->parent;
8058 elt = elt->parent;
8059 if (elt
8060 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8061 bit_position (elt->purpose))))
8063 next = elt->purpose;
8064 break;
8071 /* Ordinarily return, but not if we want to output all
8072 and there are elements left. */
8073 if (!(all && next != 0))
8074 return;
8076 /* If it's not incremental, just skip over the gap, so that after
8077 jumping to retry we will output the next successive element. */
8078 if (TREE_CODE (constructor_type) == RECORD_TYPE
8079 || TREE_CODE (constructor_type) == UNION_TYPE)
8080 constructor_unfilled_fields = next;
8081 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8082 constructor_unfilled_index = next;
8084 /* ELT now points to the node in the pending tree with the next
8085 initializer to output. */
8086 goto retry;
8089 /* Add one non-braced element to the current constructor level.
8090 This adjusts the current position within the constructor's type.
8091 This may also start or terminate implicit levels
8092 to handle a partly-braced initializer.
8094 Once this has found the correct level for the new element,
8095 it calls output_init_element.
8097 IMPLICIT is true if value comes from pop_init_level (1),
8098 the new initializer has been merged with the existing one
8099 and thus no warnings should be emitted about overriding an
8100 existing initializer. */
8102 void
8103 process_init_element (struct c_expr value, bool implicit,
8104 struct obstack * braced_init_obstack)
8106 tree orig_value = value.value;
8107 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8108 bool strict_string = value.original_code == STRING_CST;
8110 designator_depth = 0;
8111 designator_erroneous = 0;
8113 /* Handle superfluous braces around string cst as in
8114 char x[] = {"foo"}; */
8115 if (string_flag
8116 && constructor_type
8117 && TREE_CODE (constructor_type) == ARRAY_TYPE
8118 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8119 && integer_zerop (constructor_unfilled_index))
8121 if (constructor_stack->replacement_value.value)
8122 error_init ("excess elements in char array initializer");
8123 constructor_stack->replacement_value = value;
8124 return;
8127 if (constructor_stack->replacement_value.value != 0)
8129 error_init ("excess elements in struct initializer");
8130 return;
8133 /* Ignore elements of a brace group if it is entirely superfluous
8134 and has already been diagnosed. */
8135 if (constructor_type == 0)
8136 return;
8138 /* If we've exhausted any levels that didn't have braces,
8139 pop them now. */
8140 while (constructor_stack->implicit)
8142 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8143 || TREE_CODE (constructor_type) == UNION_TYPE)
8144 && constructor_fields == 0)
8145 process_init_element (pop_init_level (1, braced_init_obstack),
8146 true, braced_init_obstack);
8147 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8148 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8149 && (constructor_max_index == 0
8150 || tree_int_cst_lt (constructor_max_index,
8151 constructor_index)))
8152 process_init_element (pop_init_level (1, braced_init_obstack),
8153 true, braced_init_obstack);
8154 else
8155 break;
8158 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8159 if (constructor_range_stack)
8161 /* If value is a compound literal and we'll be just using its
8162 content, don't put it into a SAVE_EXPR. */
8163 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8164 || !require_constant_value
8165 || flag_isoc99)
8167 tree semantic_type = NULL_TREE;
8168 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8170 semantic_type = TREE_TYPE (value.value);
8171 value.value = TREE_OPERAND (value.value, 0);
8173 value.value = c_save_expr (value.value);
8174 if (semantic_type)
8175 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8176 value.value);
8180 while (1)
8182 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8184 tree fieldtype;
8185 enum tree_code fieldcode;
8187 if (constructor_fields == 0)
8189 pedwarn_init (input_location, 0,
8190 "excess elements in struct initializer");
8191 break;
8194 fieldtype = TREE_TYPE (constructor_fields);
8195 if (fieldtype != error_mark_node)
8196 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8197 fieldcode = TREE_CODE (fieldtype);
8199 /* Error for non-static initialization of a flexible array member. */
8200 if (fieldcode == ARRAY_TYPE
8201 && !require_constant_value
8202 && TYPE_SIZE (fieldtype) == NULL_TREE
8203 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8205 error_init ("non-static initialization of a flexible array member");
8206 break;
8209 /* Accept a string constant to initialize a subarray. */
8210 if (value.value != 0
8211 && fieldcode == ARRAY_TYPE
8212 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8213 && string_flag)
8214 value.value = orig_value;
8215 /* Otherwise, if we have come to a subaggregate,
8216 and we don't have an element of its type, push into it. */
8217 else if (value.value != 0
8218 && value.value != error_mark_node
8219 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8220 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8221 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8223 push_init_level (1, braced_init_obstack);
8224 continue;
8227 if (value.value)
8229 push_member_name (constructor_fields);
8230 output_init_element (value.value, value.original_type,
8231 strict_string, fieldtype,
8232 constructor_fields, 1, implicit,
8233 braced_init_obstack);
8234 RESTORE_SPELLING_DEPTH (constructor_depth);
8236 else
8237 /* Do the bookkeeping for an element that was
8238 directly output as a constructor. */
8240 /* For a record, keep track of end position of last field. */
8241 if (DECL_SIZE (constructor_fields))
8242 constructor_bit_index
8243 = size_binop_loc (input_location, PLUS_EXPR,
8244 bit_position (constructor_fields),
8245 DECL_SIZE (constructor_fields));
8247 /* If the current field was the first one not yet written out,
8248 it isn't now, so update. */
8249 if (constructor_unfilled_fields == constructor_fields)
8251 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8252 /* Skip any nameless bit fields. */
8253 while (constructor_unfilled_fields != 0
8254 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8255 && DECL_NAME (constructor_unfilled_fields) == 0)
8256 constructor_unfilled_fields =
8257 DECL_CHAIN (constructor_unfilled_fields);
8261 constructor_fields = DECL_CHAIN (constructor_fields);
8262 /* Skip any nameless bit fields at the beginning. */
8263 while (constructor_fields != 0
8264 && DECL_C_BIT_FIELD (constructor_fields)
8265 && DECL_NAME (constructor_fields) == 0)
8266 constructor_fields = DECL_CHAIN (constructor_fields);
8268 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8270 tree fieldtype;
8271 enum tree_code fieldcode;
8273 if (constructor_fields == 0)
8275 pedwarn_init (input_location, 0,
8276 "excess elements in union initializer");
8277 break;
8280 fieldtype = TREE_TYPE (constructor_fields);
8281 if (fieldtype != error_mark_node)
8282 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8283 fieldcode = TREE_CODE (fieldtype);
8285 /* Warn that traditional C rejects initialization of unions.
8286 We skip the warning if the value is zero. This is done
8287 under the assumption that the zero initializer in user
8288 code appears conditioned on e.g. __STDC__ to avoid
8289 "missing initializer" warnings and relies on default
8290 initialization to zero in the traditional C case.
8291 We also skip the warning if the initializer is designated,
8292 again on the assumption that this must be conditional on
8293 __STDC__ anyway (and we've already complained about the
8294 member-designator already). */
8295 if (!in_system_header && !constructor_designated
8296 && !(value.value && (integer_zerop (value.value)
8297 || real_zerop (value.value))))
8298 warning (OPT_Wtraditional, "traditional C rejects initialization "
8299 "of unions");
8301 /* Accept a string constant to initialize a subarray. */
8302 if (value.value != 0
8303 && fieldcode == ARRAY_TYPE
8304 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8305 && string_flag)
8306 value.value = orig_value;
8307 /* Otherwise, if we have come to a subaggregate,
8308 and we don't have an element of its type, push into it. */
8309 else if (value.value != 0
8310 && value.value != error_mark_node
8311 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8312 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8313 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8315 push_init_level (1, braced_init_obstack);
8316 continue;
8319 if (value.value)
8321 push_member_name (constructor_fields);
8322 output_init_element (value.value, value.original_type,
8323 strict_string, fieldtype,
8324 constructor_fields, 1, implicit,
8325 braced_init_obstack);
8326 RESTORE_SPELLING_DEPTH (constructor_depth);
8328 else
8329 /* Do the bookkeeping for an element that was
8330 directly output as a constructor. */
8332 constructor_bit_index = DECL_SIZE (constructor_fields);
8333 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8336 constructor_fields = 0;
8338 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8340 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8341 enum tree_code eltcode = TREE_CODE (elttype);
8343 /* Accept a string constant to initialize a subarray. */
8344 if (value.value != 0
8345 && eltcode == ARRAY_TYPE
8346 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8347 && string_flag)
8348 value.value = orig_value;
8349 /* Otherwise, if we have come to a subaggregate,
8350 and we don't have an element of its type, push into it. */
8351 else if (value.value != 0
8352 && value.value != error_mark_node
8353 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8354 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8355 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8357 push_init_level (1, braced_init_obstack);
8358 continue;
8361 if (constructor_max_index != 0
8362 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8363 || integer_all_onesp (constructor_max_index)))
8365 pedwarn_init (input_location, 0,
8366 "excess elements in array initializer");
8367 break;
8370 /* Now output the actual element. */
8371 if (value.value)
8373 push_array_bounds (tree_low_cst (constructor_index, 1));
8374 output_init_element (value.value, value.original_type,
8375 strict_string, elttype,
8376 constructor_index, 1, implicit,
8377 braced_init_obstack);
8378 RESTORE_SPELLING_DEPTH (constructor_depth);
8381 constructor_index
8382 = size_binop_loc (input_location, PLUS_EXPR,
8383 constructor_index, bitsize_one_node);
8385 if (!value.value)
8386 /* If we are doing the bookkeeping for an element that was
8387 directly output as a constructor, we must update
8388 constructor_unfilled_index. */
8389 constructor_unfilled_index = constructor_index;
8391 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8393 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8395 /* Do a basic check of initializer size. Note that vectors
8396 always have a fixed size derived from their type. */
8397 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8399 pedwarn_init (input_location, 0,
8400 "excess elements in vector initializer");
8401 break;
8404 /* Now output the actual element. */
8405 if (value.value)
8407 if (TREE_CODE (value.value) == VECTOR_CST)
8408 elttype = TYPE_MAIN_VARIANT (constructor_type);
8409 output_init_element (value.value, value.original_type,
8410 strict_string, elttype,
8411 constructor_index, 1, implicit,
8412 braced_init_obstack);
8415 constructor_index
8416 = size_binop_loc (input_location,
8417 PLUS_EXPR, constructor_index, bitsize_one_node);
8419 if (!value.value)
8420 /* If we are doing the bookkeeping for an element that was
8421 directly output as a constructor, we must update
8422 constructor_unfilled_index. */
8423 constructor_unfilled_index = constructor_index;
8426 /* Handle the sole element allowed in a braced initializer
8427 for a scalar variable. */
8428 else if (constructor_type != error_mark_node
8429 && constructor_fields == 0)
8431 pedwarn_init (input_location, 0,
8432 "excess elements in scalar initializer");
8433 break;
8435 else
8437 if (value.value)
8438 output_init_element (value.value, value.original_type,
8439 strict_string, constructor_type,
8440 NULL_TREE, 1, implicit,
8441 braced_init_obstack);
8442 constructor_fields = 0;
8445 /* Handle range initializers either at this level or anywhere higher
8446 in the designator stack. */
8447 if (constructor_range_stack)
8449 struct constructor_range_stack *p, *range_stack;
8450 int finish = 0;
8452 range_stack = constructor_range_stack;
8453 constructor_range_stack = 0;
8454 while (constructor_stack != range_stack->stack)
8456 gcc_assert (constructor_stack->implicit);
8457 process_init_element (pop_init_level (1,
8458 braced_init_obstack),
8459 true, braced_init_obstack);
8461 for (p = range_stack;
8462 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8463 p = p->prev)
8465 gcc_assert (constructor_stack->implicit);
8466 process_init_element (pop_init_level (1, braced_init_obstack),
8467 true, braced_init_obstack);
8470 p->index = size_binop_loc (input_location,
8471 PLUS_EXPR, p->index, bitsize_one_node);
8472 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8473 finish = 1;
8475 while (1)
8477 constructor_index = p->index;
8478 constructor_fields = p->fields;
8479 if (finish && p->range_end && p->index == p->range_start)
8481 finish = 0;
8482 p->prev = 0;
8484 p = p->next;
8485 if (!p)
8486 break;
8487 push_init_level (2, braced_init_obstack);
8488 p->stack = constructor_stack;
8489 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8490 p->index = p->range_start;
8493 if (!finish)
8494 constructor_range_stack = range_stack;
8495 continue;
8498 break;
8501 constructor_range_stack = 0;
8504 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8505 (guaranteed to be 'volatile' or null) and ARGS (represented using
8506 an ASM_EXPR node). */
8507 tree
8508 build_asm_stmt (tree cv_qualifier, tree args)
8510 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8511 ASM_VOLATILE_P (args) = 1;
8512 return add_stmt (args);
8515 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8516 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8517 SIMPLE indicates whether there was anything at all after the
8518 string in the asm expression -- asm("blah") and asm("blah" : )
8519 are subtly different. We use a ASM_EXPR node to represent this. */
8520 tree
8521 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8522 tree clobbers, tree labels, bool simple)
8524 tree tail;
8525 tree args;
8526 int i;
8527 const char *constraint;
8528 const char **oconstraints;
8529 bool allows_mem, allows_reg, is_inout;
8530 int ninputs, noutputs;
8532 ninputs = list_length (inputs);
8533 noutputs = list_length (outputs);
8534 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8536 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8538 /* Remove output conversions that change the type but not the mode. */
8539 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8541 tree output = TREE_VALUE (tail);
8543 /* ??? Really, this should not be here. Users should be using a
8544 proper lvalue, dammit. But there's a long history of using casts
8545 in the output operands. In cases like longlong.h, this becomes a
8546 primitive form of typechecking -- if the cast can be removed, then
8547 the output operand had a type of the proper width; otherwise we'll
8548 get an error. Gross, but ... */
8549 STRIP_NOPS (output);
8551 if (!lvalue_or_else (output, lv_asm))
8552 output = error_mark_node;
8554 if (output != error_mark_node
8555 && (TREE_READONLY (output)
8556 || TYPE_READONLY (TREE_TYPE (output))
8557 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8558 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8559 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8560 readonly_error (output, lv_asm);
8562 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8563 oconstraints[i] = constraint;
8565 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8566 &allows_mem, &allows_reg, &is_inout))
8568 /* If the operand is going to end up in memory,
8569 mark it addressable. */
8570 if (!allows_reg && !c_mark_addressable (output))
8571 output = error_mark_node;
8573 else
8574 output = error_mark_node;
8576 TREE_VALUE (tail) = output;
8579 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8581 tree input;
8583 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8584 input = TREE_VALUE (tail);
8586 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8587 oconstraints, &allows_mem, &allows_reg))
8589 /* If the operand is going to end up in memory,
8590 mark it addressable. */
8591 if (!allows_reg && allows_mem)
8593 /* Strip the nops as we allow this case. FIXME, this really
8594 should be rejected or made deprecated. */
8595 STRIP_NOPS (input);
8596 if (!c_mark_addressable (input))
8597 input = error_mark_node;
8600 else
8601 input = error_mark_node;
8603 TREE_VALUE (tail) = input;
8606 /* ASMs with labels cannot have outputs. This should have been
8607 enforced by the parser. */
8608 gcc_assert (outputs == NULL || labels == NULL);
8610 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8612 /* asm statements without outputs, including simple ones, are treated
8613 as volatile. */
8614 ASM_INPUT_P (args) = simple;
8615 ASM_VOLATILE_P (args) = (noutputs == 0);
8617 return args;
8620 /* Generate a goto statement to LABEL. LOC is the location of the
8621 GOTO. */
8623 tree
8624 c_finish_goto_label (location_t loc, tree label)
8626 tree decl = lookup_label_for_goto (loc, label);
8627 if (!decl)
8628 return NULL_TREE;
8629 TREE_USED (decl) = 1;
8631 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8632 SET_EXPR_LOCATION (t, loc);
8633 return add_stmt (t);
8637 /* Generate a computed goto statement to EXPR. LOC is the location of
8638 the GOTO. */
8640 tree
8641 c_finish_goto_ptr (location_t loc, tree expr)
8643 tree t;
8644 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8645 expr = c_fully_fold (expr, false, NULL);
8646 expr = convert (ptr_type_node, expr);
8647 t = build1 (GOTO_EXPR, void_type_node, expr);
8648 SET_EXPR_LOCATION (t, loc);
8649 return add_stmt (t);
8652 /* Generate a C `return' statement. RETVAL is the expression for what
8653 to return, or a null pointer for `return;' with no value. LOC is
8654 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8655 is the original type of RETVAL. */
8657 tree
8658 c_finish_return (location_t loc, tree retval, tree origtype)
8660 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8661 bool no_warning = false;
8662 bool npc = false;
8664 if (TREE_THIS_VOLATILE (current_function_decl))
8665 warning_at (loc, 0,
8666 "function declared %<noreturn%> has a %<return%> statement");
8668 if (retval)
8670 tree semantic_type = NULL_TREE;
8671 npc = null_pointer_constant_p (retval);
8672 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8674 semantic_type = TREE_TYPE (retval);
8675 retval = TREE_OPERAND (retval, 0);
8677 retval = c_fully_fold (retval, false, NULL);
8678 if (semantic_type)
8679 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8682 if (!retval)
8684 current_function_returns_null = 1;
8685 if ((warn_return_type || flag_isoc99)
8686 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8688 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8689 "%<return%> with no value, in "
8690 "function returning non-void");
8691 no_warning = true;
8694 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8696 current_function_returns_null = 1;
8697 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8698 pedwarn (loc, 0,
8699 "%<return%> with a value, in function returning void");
8700 else
8701 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8702 "%<return%> with expression, in function returning void");
8704 else
8706 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8707 ic_return,
8708 npc, NULL_TREE, NULL_TREE, 0);
8709 tree res = DECL_RESULT (current_function_decl);
8710 tree inner;
8712 current_function_returns_value = 1;
8713 if (t == error_mark_node)
8714 return NULL_TREE;
8716 inner = t = convert (TREE_TYPE (res), t);
8718 /* Strip any conversions, additions, and subtractions, and see if
8719 we are returning the address of a local variable. Warn if so. */
8720 while (1)
8722 switch (TREE_CODE (inner))
8724 CASE_CONVERT:
8725 case NON_LVALUE_EXPR:
8726 case PLUS_EXPR:
8727 case POINTER_PLUS_EXPR:
8728 inner = TREE_OPERAND (inner, 0);
8729 continue;
8731 case MINUS_EXPR:
8732 /* If the second operand of the MINUS_EXPR has a pointer
8733 type (or is converted from it), this may be valid, so
8734 don't give a warning. */
8736 tree op1 = TREE_OPERAND (inner, 1);
8738 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8739 && (CONVERT_EXPR_P (op1)
8740 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8741 op1 = TREE_OPERAND (op1, 0);
8743 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8744 break;
8746 inner = TREE_OPERAND (inner, 0);
8747 continue;
8750 case ADDR_EXPR:
8751 inner = TREE_OPERAND (inner, 0);
8753 while (REFERENCE_CLASS_P (inner)
8754 && TREE_CODE (inner) != INDIRECT_REF)
8755 inner = TREE_OPERAND (inner, 0);
8757 if (DECL_P (inner)
8758 && !DECL_EXTERNAL (inner)
8759 && !TREE_STATIC (inner)
8760 && DECL_CONTEXT (inner) == current_function_decl)
8761 warning_at (loc,
8762 0, "function returns address of local variable");
8763 break;
8765 default:
8766 break;
8769 break;
8772 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8773 SET_EXPR_LOCATION (retval, loc);
8775 if (warn_sequence_point)
8776 verify_sequence_points (retval);
8779 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8780 TREE_NO_WARNING (ret_stmt) |= no_warning;
8781 return add_stmt (ret_stmt);
8784 struct c_switch {
8785 /* The SWITCH_EXPR being built. */
8786 tree switch_expr;
8788 /* The original type of the testing expression, i.e. before the
8789 default conversion is applied. */
8790 tree orig_type;
8792 /* A splay-tree mapping the low element of a case range to the high
8793 element, or NULL_TREE if there is no high element. Used to
8794 determine whether or not a new case label duplicates an old case
8795 label. We need a tree, rather than simply a hash table, because
8796 of the GNU case range extension. */
8797 splay_tree cases;
8799 /* The bindings at the point of the switch. This is used for
8800 warnings crossing decls when branching to a case label. */
8801 struct c_spot_bindings *bindings;
8803 /* The next node on the stack. */
8804 struct c_switch *next;
8807 /* A stack of the currently active switch statements. The innermost
8808 switch statement is on the top of the stack. There is no need to
8809 mark the stack for garbage collection because it is only active
8810 during the processing of the body of a function, and we never
8811 collect at that point. */
8813 struct c_switch *c_switch_stack;
8815 /* Start a C switch statement, testing expression EXP. Return the new
8816 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8817 SWITCH_COND_LOC is the location of the switch's condition. */
8819 tree
8820 c_start_case (location_t switch_loc,
8821 location_t switch_cond_loc,
8822 tree exp)
8824 tree orig_type = error_mark_node;
8825 struct c_switch *cs;
8827 if (exp != error_mark_node)
8829 orig_type = TREE_TYPE (exp);
8831 if (!INTEGRAL_TYPE_P (orig_type))
8833 if (orig_type != error_mark_node)
8835 error_at (switch_cond_loc, "switch quantity not an integer");
8836 orig_type = error_mark_node;
8838 exp = integer_zero_node;
8840 else
8842 tree type = TYPE_MAIN_VARIANT (orig_type);
8844 if (!in_system_header
8845 && (type == long_integer_type_node
8846 || type == long_unsigned_type_node))
8847 warning_at (switch_cond_loc,
8848 OPT_Wtraditional, "%<long%> switch expression not "
8849 "converted to %<int%> in ISO C");
8851 exp = c_fully_fold (exp, false, NULL);
8852 exp = default_conversion (exp);
8854 if (warn_sequence_point)
8855 verify_sequence_points (exp);
8859 /* Add this new SWITCH_EXPR to the stack. */
8860 cs = XNEW (struct c_switch);
8861 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8862 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8863 cs->orig_type = orig_type;
8864 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8865 cs->bindings = c_get_switch_bindings ();
8866 cs->next = c_switch_stack;
8867 c_switch_stack = cs;
8869 return add_stmt (cs->switch_expr);
8872 /* Process a case label at location LOC. */
8874 tree
8875 do_case (location_t loc, tree low_value, tree high_value)
8877 tree label = NULL_TREE;
8879 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8881 low_value = c_fully_fold (low_value, false, NULL);
8882 if (TREE_CODE (low_value) == INTEGER_CST)
8883 pedwarn (input_location, OPT_pedantic,
8884 "case label is not an integer constant expression");
8887 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8889 high_value = c_fully_fold (high_value, false, NULL);
8890 if (TREE_CODE (high_value) == INTEGER_CST)
8891 pedwarn (input_location, OPT_pedantic,
8892 "case label is not an integer constant expression");
8895 if (c_switch_stack == NULL)
8897 if (low_value)
8898 error_at (loc, "case label not within a switch statement");
8899 else
8900 error_at (loc, "%<default%> label not within a switch statement");
8901 return NULL_TREE;
8904 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8905 EXPR_LOCATION (c_switch_stack->switch_expr),
8906 loc))
8907 return NULL_TREE;
8909 label = c_add_case_label (loc, c_switch_stack->cases,
8910 SWITCH_COND (c_switch_stack->switch_expr),
8911 c_switch_stack->orig_type,
8912 low_value, high_value);
8913 if (label == error_mark_node)
8914 label = NULL_TREE;
8915 return label;
8918 /* Finish the switch statement. */
8920 void
8921 c_finish_case (tree body)
8923 struct c_switch *cs = c_switch_stack;
8924 location_t switch_location;
8926 SWITCH_BODY (cs->switch_expr) = body;
8928 /* Emit warnings as needed. */
8929 switch_location = EXPR_LOCATION (cs->switch_expr);
8930 c_do_switch_warnings (cs->cases, switch_location,
8931 TREE_TYPE (cs->switch_expr),
8932 SWITCH_COND (cs->switch_expr));
8934 /* Pop the stack. */
8935 c_switch_stack = cs->next;
8936 splay_tree_delete (cs->cases);
8937 c_release_switch_bindings (cs->bindings);
8938 XDELETE (cs);
8941 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8942 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8943 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8944 statement, and was not surrounded with parenthesis. */
8946 void
8947 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8948 tree else_block, bool nested_if)
8950 tree stmt;
8952 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8953 if (warn_parentheses && nested_if && else_block == NULL)
8955 tree inner_if = then_block;
8957 /* We know from the grammar productions that there is an IF nested
8958 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8959 it might not be exactly THEN_BLOCK, but should be the last
8960 non-container statement within. */
8961 while (1)
8962 switch (TREE_CODE (inner_if))
8964 case COND_EXPR:
8965 goto found;
8966 case BIND_EXPR:
8967 inner_if = BIND_EXPR_BODY (inner_if);
8968 break;
8969 case STATEMENT_LIST:
8970 inner_if = expr_last (then_block);
8971 break;
8972 case TRY_FINALLY_EXPR:
8973 case TRY_CATCH_EXPR:
8974 inner_if = TREE_OPERAND (inner_if, 0);
8975 break;
8976 default:
8977 gcc_unreachable ();
8979 found:
8981 if (COND_EXPR_ELSE (inner_if))
8982 warning_at (if_locus, OPT_Wparentheses,
8983 "suggest explicit braces to avoid ambiguous %<else%>");
8986 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8987 SET_EXPR_LOCATION (stmt, if_locus);
8988 add_stmt (stmt);
8991 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8992 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8993 is false for DO loops. INCR is the FOR increment expression. BODY is
8994 the statement controlled by the loop. BLAB is the break label. CLAB is
8995 the continue label. Everything is allowed to be NULL. */
8997 void
8998 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8999 tree blab, tree clab, bool cond_is_first)
9001 tree entry = NULL, exit = NULL, t;
9003 /* If the condition is zero don't generate a loop construct. */
9004 if (cond && integer_zerop (cond))
9006 if (cond_is_first)
9008 t = build_and_jump (&blab);
9009 SET_EXPR_LOCATION (t, start_locus);
9010 add_stmt (t);
9013 else
9015 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9017 /* If we have an exit condition, then we build an IF with gotos either
9018 out of the loop, or to the top of it. If there's no exit condition,
9019 then we just build a jump back to the top. */
9020 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9022 if (cond && !integer_nonzerop (cond))
9024 /* Canonicalize the loop condition to the end. This means
9025 generating a branch to the loop condition. Reuse the
9026 continue label, if possible. */
9027 if (cond_is_first)
9029 if (incr || !clab)
9031 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9032 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9034 else
9035 t = build1 (GOTO_EXPR, void_type_node, clab);
9036 SET_EXPR_LOCATION (t, start_locus);
9037 add_stmt (t);
9040 t = build_and_jump (&blab);
9041 if (cond_is_first)
9042 exit = fold_build3_loc (start_locus,
9043 COND_EXPR, void_type_node, cond, exit, t);
9044 else
9045 exit = fold_build3_loc (input_location,
9046 COND_EXPR, void_type_node, cond, exit, t);
9049 add_stmt (top);
9052 if (body)
9053 add_stmt (body);
9054 if (clab)
9055 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9056 if (incr)
9057 add_stmt (incr);
9058 if (entry)
9059 add_stmt (entry);
9060 if (exit)
9061 add_stmt (exit);
9062 if (blab)
9063 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9066 tree
9067 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9069 bool skip;
9070 tree label = *label_p;
9072 /* In switch statements break is sometimes stylistically used after
9073 a return statement. This can lead to spurious warnings about
9074 control reaching the end of a non-void function when it is
9075 inlined. Note that we are calling block_may_fallthru with
9076 language specific tree nodes; this works because
9077 block_may_fallthru returns true when given something it does not
9078 understand. */
9079 skip = !block_may_fallthru (cur_stmt_list);
9081 if (!label)
9083 if (!skip)
9084 *label_p = label = create_artificial_label (loc);
9086 else if (TREE_CODE (label) == LABEL_DECL)
9088 else switch (TREE_INT_CST_LOW (label))
9090 case 0:
9091 if (is_break)
9092 error_at (loc, "break statement not within loop or switch");
9093 else
9094 error_at (loc, "continue statement not within a loop");
9095 return NULL_TREE;
9097 case 1:
9098 gcc_assert (is_break);
9099 error_at (loc, "break statement used with OpenMP for loop");
9100 return NULL_TREE;
9102 default:
9103 gcc_unreachable ();
9106 if (skip)
9107 return NULL_TREE;
9109 if (!is_break)
9110 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9112 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9115 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9117 static void
9118 emit_side_effect_warnings (location_t loc, tree expr)
9120 if (expr == error_mark_node)
9122 else if (!TREE_SIDE_EFFECTS (expr))
9124 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9125 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9127 else
9128 warn_if_unused_value (expr, loc);
9131 /* Process an expression as if it were a complete statement. Emit
9132 diagnostics, but do not call ADD_STMT. LOC is the location of the
9133 statement. */
9135 tree
9136 c_process_expr_stmt (location_t loc, tree expr)
9138 tree exprv;
9140 if (!expr)
9141 return NULL_TREE;
9143 expr = c_fully_fold (expr, false, NULL);
9145 if (warn_sequence_point)
9146 verify_sequence_points (expr);
9148 if (TREE_TYPE (expr) != error_mark_node
9149 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9150 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9151 error_at (loc, "expression statement has incomplete type");
9153 /* If we're not processing a statement expression, warn about unused values.
9154 Warnings for statement expressions will be emitted later, once we figure
9155 out which is the result. */
9156 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9157 && warn_unused_value)
9158 emit_side_effect_warnings (loc, expr);
9160 exprv = expr;
9161 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9162 exprv = TREE_OPERAND (exprv, 1);
9163 if (DECL_P (exprv) || handled_component_p (exprv))
9164 mark_exp_read (exprv);
9166 /* If the expression is not of a type to which we cannot assign a line
9167 number, wrap the thing in a no-op NOP_EXPR. */
9168 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9170 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9171 SET_EXPR_LOCATION (expr, loc);
9174 return expr;
9177 /* Emit an expression as a statement. LOC is the location of the
9178 expression. */
9180 tree
9181 c_finish_expr_stmt (location_t loc, tree expr)
9183 if (expr)
9184 return add_stmt (c_process_expr_stmt (loc, expr));
9185 else
9186 return NULL;
9189 /* Do the opposite and emit a statement as an expression. To begin,
9190 create a new binding level and return it. */
9192 tree
9193 c_begin_stmt_expr (void)
9195 tree ret;
9197 /* We must force a BLOCK for this level so that, if it is not expanded
9198 later, there is a way to turn off the entire subtree of blocks that
9199 are contained in it. */
9200 keep_next_level ();
9201 ret = c_begin_compound_stmt (true);
9203 c_bindings_start_stmt_expr (c_switch_stack == NULL
9204 ? NULL
9205 : c_switch_stack->bindings);
9207 /* Mark the current statement list as belonging to a statement list. */
9208 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9210 return ret;
9213 /* LOC is the location of the compound statement to which this body
9214 belongs. */
9216 tree
9217 c_finish_stmt_expr (location_t loc, tree body)
9219 tree last, type, tmp, val;
9220 tree *last_p;
9222 body = c_end_compound_stmt (loc, body, true);
9224 c_bindings_end_stmt_expr (c_switch_stack == NULL
9225 ? NULL
9226 : c_switch_stack->bindings);
9228 /* Locate the last statement in BODY. See c_end_compound_stmt
9229 about always returning a BIND_EXPR. */
9230 last_p = &BIND_EXPR_BODY (body);
9231 last = BIND_EXPR_BODY (body);
9233 continue_searching:
9234 if (TREE_CODE (last) == STATEMENT_LIST)
9236 tree_stmt_iterator i;
9238 /* This can happen with degenerate cases like ({ }). No value. */
9239 if (!TREE_SIDE_EFFECTS (last))
9240 return body;
9242 /* If we're supposed to generate side effects warnings, process
9243 all of the statements except the last. */
9244 if (warn_unused_value)
9246 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9248 location_t tloc;
9249 tree t = tsi_stmt (i);
9251 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9252 emit_side_effect_warnings (tloc, t);
9255 else
9256 i = tsi_last (last);
9257 last_p = tsi_stmt_ptr (i);
9258 last = *last_p;
9261 /* If the end of the list is exception related, then the list was split
9262 by a call to push_cleanup. Continue searching. */
9263 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9264 || TREE_CODE (last) == TRY_CATCH_EXPR)
9266 last_p = &TREE_OPERAND (last, 0);
9267 last = *last_p;
9268 goto continue_searching;
9271 if (last == error_mark_node)
9272 return last;
9274 /* In the case that the BIND_EXPR is not necessary, return the
9275 expression out from inside it. */
9276 if (last == BIND_EXPR_BODY (body)
9277 && BIND_EXPR_VARS (body) == NULL)
9279 /* Even if this looks constant, do not allow it in a constant
9280 expression. */
9281 last = c_wrap_maybe_const (last, true);
9282 /* Do not warn if the return value of a statement expression is
9283 unused. */
9284 TREE_NO_WARNING (last) = 1;
9285 return last;
9288 /* Extract the type of said expression. */
9289 type = TREE_TYPE (last);
9291 /* If we're not returning a value at all, then the BIND_EXPR that
9292 we already have is a fine expression to return. */
9293 if (!type || VOID_TYPE_P (type))
9294 return body;
9296 /* Now that we've located the expression containing the value, it seems
9297 silly to make voidify_wrapper_expr repeat the process. Create a
9298 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9299 tmp = create_tmp_var_raw (type, NULL);
9301 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9302 tree_expr_nonnegative_p giving up immediately. */
9303 val = last;
9304 if (TREE_CODE (val) == NOP_EXPR
9305 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9306 val = TREE_OPERAND (val, 0);
9308 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9309 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9312 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9313 SET_EXPR_LOCATION (t, loc);
9314 return t;
9318 /* Begin and end compound statements. This is as simple as pushing
9319 and popping new statement lists from the tree. */
9321 tree
9322 c_begin_compound_stmt (bool do_scope)
9324 tree stmt = push_stmt_list ();
9325 if (do_scope)
9326 push_scope ();
9327 return stmt;
9330 /* End a compound statement. STMT is the statement. LOC is the
9331 location of the compound statement-- this is usually the location
9332 of the opening brace. */
9334 tree
9335 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9337 tree block = NULL;
9339 if (do_scope)
9341 if (c_dialect_objc ())
9342 objc_clear_super_receiver ();
9343 block = pop_scope ();
9346 stmt = pop_stmt_list (stmt);
9347 stmt = c_build_bind_expr (loc, block, stmt);
9349 /* If this compound statement is nested immediately inside a statement
9350 expression, then force a BIND_EXPR to be created. Otherwise we'll
9351 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9352 STATEMENT_LISTs merge, and thus we can lose track of what statement
9353 was really last. */
9354 if (cur_stmt_list
9355 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9356 && TREE_CODE (stmt) != BIND_EXPR)
9358 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9359 TREE_SIDE_EFFECTS (stmt) = 1;
9360 SET_EXPR_LOCATION (stmt, loc);
9363 return stmt;
9366 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9367 when the current scope is exited. EH_ONLY is true when this is not
9368 meant to apply to normal control flow transfer. */
9370 void
9371 push_cleanup (tree decl, tree cleanup, bool eh_only)
9373 enum tree_code code;
9374 tree stmt, list;
9375 bool stmt_expr;
9377 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9378 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9379 add_stmt (stmt);
9380 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9381 list = push_stmt_list ();
9382 TREE_OPERAND (stmt, 0) = list;
9383 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9386 /* Build a binary-operation expression without default conversions.
9387 CODE is the kind of expression to build.
9388 LOCATION is the operator's location.
9389 This function differs from `build' in several ways:
9390 the data type of the result is computed and recorded in it,
9391 warnings are generated if arg data types are invalid,
9392 special handling for addition and subtraction of pointers is known,
9393 and some optimization is done (operations on narrow ints
9394 are done in the narrower type when that gives the same result).
9395 Constant folding is also done before the result is returned.
9397 Note that the operands will never have enumeral types, or function
9398 or array types, because either they will have the default conversions
9399 performed or they have both just been converted to some other type in which
9400 the arithmetic is to be done. */
9402 tree
9403 build_binary_op (location_t location, enum tree_code code,
9404 tree orig_op0, tree orig_op1, int convert_p)
9406 tree type0, type1, orig_type0, orig_type1;
9407 tree eptype;
9408 enum tree_code code0, code1;
9409 tree op0, op1;
9410 tree ret = error_mark_node;
9411 const char *invalid_op_diag;
9412 bool op0_int_operands, op1_int_operands;
9413 bool int_const, int_const_or_overflow, int_operands;
9415 /* Expression code to give to the expression when it is built.
9416 Normally this is CODE, which is what the caller asked for,
9417 but in some special cases we change it. */
9418 enum tree_code resultcode = code;
9420 /* Data type in which the computation is to be performed.
9421 In the simplest cases this is the common type of the arguments. */
9422 tree result_type = NULL;
9424 /* When the computation is in excess precision, the type of the
9425 final EXCESS_PRECISION_EXPR. */
9426 tree semantic_result_type = NULL;
9428 /* Nonzero means operands have already been type-converted
9429 in whatever way is necessary.
9430 Zero means they need to be converted to RESULT_TYPE. */
9431 int converted = 0;
9433 /* Nonzero means create the expression with this type, rather than
9434 RESULT_TYPE. */
9435 tree build_type = 0;
9437 /* Nonzero means after finally constructing the expression
9438 convert it to this type. */
9439 tree final_type = 0;
9441 /* Nonzero if this is an operation like MIN or MAX which can
9442 safely be computed in short if both args are promoted shorts.
9443 Also implies COMMON.
9444 -1 indicates a bitwise operation; this makes a difference
9445 in the exact conditions for when it is safe to do the operation
9446 in a narrower mode. */
9447 int shorten = 0;
9449 /* Nonzero if this is a comparison operation;
9450 if both args are promoted shorts, compare the original shorts.
9451 Also implies COMMON. */
9452 int short_compare = 0;
9454 /* Nonzero if this is a right-shift operation, which can be computed on the
9455 original short and then promoted if the operand is a promoted short. */
9456 int short_shift = 0;
9458 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9459 int common = 0;
9461 /* True means types are compatible as far as ObjC is concerned. */
9462 bool objc_ok;
9464 /* True means this is an arithmetic operation that may need excess
9465 precision. */
9466 bool may_need_excess_precision;
9468 /* True means this is a boolean operation that converts both its
9469 operands to truth-values. */
9470 bool boolean_op = false;
9472 if (location == UNKNOWN_LOCATION)
9473 location = input_location;
9475 op0 = orig_op0;
9476 op1 = orig_op1;
9478 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9479 if (op0_int_operands)
9480 op0 = remove_c_maybe_const_expr (op0);
9481 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9482 if (op1_int_operands)
9483 op1 = remove_c_maybe_const_expr (op1);
9484 int_operands = (op0_int_operands && op1_int_operands);
9485 if (int_operands)
9487 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9488 && TREE_CODE (orig_op1) == INTEGER_CST);
9489 int_const = (int_const_or_overflow
9490 && !TREE_OVERFLOW (orig_op0)
9491 && !TREE_OVERFLOW (orig_op1));
9493 else
9494 int_const = int_const_or_overflow = false;
9496 if (convert_p)
9498 op0 = default_conversion (op0);
9499 op1 = default_conversion (op1);
9502 orig_type0 = type0 = TREE_TYPE (op0);
9503 orig_type1 = type1 = TREE_TYPE (op1);
9505 /* The expression codes of the data types of the arguments tell us
9506 whether the arguments are integers, floating, pointers, etc. */
9507 code0 = TREE_CODE (type0);
9508 code1 = TREE_CODE (type1);
9510 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9511 STRIP_TYPE_NOPS (op0);
9512 STRIP_TYPE_NOPS (op1);
9514 /* If an error was already reported for one of the arguments,
9515 avoid reporting another error. */
9517 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9518 return error_mark_node;
9520 if ((invalid_op_diag
9521 = targetm.invalid_binary_op (code, type0, type1)))
9523 error_at (location, invalid_op_diag);
9524 return error_mark_node;
9527 switch (code)
9529 case PLUS_EXPR:
9530 case MINUS_EXPR:
9531 case MULT_EXPR:
9532 case TRUNC_DIV_EXPR:
9533 case CEIL_DIV_EXPR:
9534 case FLOOR_DIV_EXPR:
9535 case ROUND_DIV_EXPR:
9536 case EXACT_DIV_EXPR:
9537 may_need_excess_precision = true;
9538 break;
9539 default:
9540 may_need_excess_precision = false;
9541 break;
9543 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9545 op0 = TREE_OPERAND (op0, 0);
9546 type0 = TREE_TYPE (op0);
9548 else if (may_need_excess_precision
9549 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9551 type0 = eptype;
9552 op0 = convert (eptype, op0);
9554 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9556 op1 = TREE_OPERAND (op1, 0);
9557 type1 = TREE_TYPE (op1);
9559 else if (may_need_excess_precision
9560 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9562 type1 = eptype;
9563 op1 = convert (eptype, op1);
9566 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9568 switch (code)
9570 case PLUS_EXPR:
9571 /* Handle the pointer + int case. */
9572 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9574 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9575 goto return_build_binary_op;
9577 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9579 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9580 goto return_build_binary_op;
9582 else
9583 common = 1;
9584 break;
9586 case MINUS_EXPR:
9587 /* Subtraction of two similar pointers.
9588 We must subtract them as integers, then divide by object size. */
9589 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9590 && comp_target_types (location, type0, type1))
9592 ret = pointer_diff (location, op0, op1);
9593 goto return_build_binary_op;
9595 /* Handle pointer minus int. Just like pointer plus int. */
9596 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9598 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9599 goto return_build_binary_op;
9601 else
9602 common = 1;
9603 break;
9605 case MULT_EXPR:
9606 common = 1;
9607 break;
9609 case TRUNC_DIV_EXPR:
9610 case CEIL_DIV_EXPR:
9611 case FLOOR_DIV_EXPR:
9612 case ROUND_DIV_EXPR:
9613 case EXACT_DIV_EXPR:
9614 warn_for_div_by_zero (location, op1);
9616 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9617 || code0 == FIXED_POINT_TYPE
9618 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9619 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9620 || code1 == FIXED_POINT_TYPE
9621 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9623 enum tree_code tcode0 = code0, tcode1 = code1;
9625 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9626 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9627 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9628 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9630 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9631 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9632 resultcode = RDIV_EXPR;
9633 else
9634 /* Although it would be tempting to shorten always here, that
9635 loses on some targets, since the modulo instruction is
9636 undefined if the quotient can't be represented in the
9637 computation mode. We shorten only if unsigned or if
9638 dividing by something we know != -1. */
9639 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9640 || (TREE_CODE (op1) == INTEGER_CST
9641 && !integer_all_onesp (op1)));
9642 common = 1;
9644 break;
9646 case BIT_AND_EXPR:
9647 case BIT_IOR_EXPR:
9648 case BIT_XOR_EXPR:
9649 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9650 shorten = -1;
9651 /* Allow vector types which are not floating point types. */
9652 else if (code0 == VECTOR_TYPE
9653 && code1 == VECTOR_TYPE
9654 && !VECTOR_FLOAT_TYPE_P (type0)
9655 && !VECTOR_FLOAT_TYPE_P (type1))
9656 common = 1;
9657 break;
9659 case TRUNC_MOD_EXPR:
9660 case FLOOR_MOD_EXPR:
9661 warn_for_div_by_zero (location, op1);
9663 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9664 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9665 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9666 common = 1;
9667 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9669 /* Although it would be tempting to shorten always here, that loses
9670 on some targets, since the modulo instruction is undefined if the
9671 quotient can't be represented in the computation mode. We shorten
9672 only if unsigned or if dividing by something we know != -1. */
9673 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9674 || (TREE_CODE (op1) == INTEGER_CST
9675 && !integer_all_onesp (op1)));
9676 common = 1;
9678 break;
9680 case TRUTH_ANDIF_EXPR:
9681 case TRUTH_ORIF_EXPR:
9682 case TRUTH_AND_EXPR:
9683 case TRUTH_OR_EXPR:
9684 case TRUTH_XOR_EXPR:
9685 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9686 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9687 || code0 == FIXED_POINT_TYPE)
9688 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9689 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9690 || code1 == FIXED_POINT_TYPE))
9692 /* Result of these operations is always an int,
9693 but that does not mean the operands should be
9694 converted to ints! */
9695 result_type = integer_type_node;
9696 op0 = c_common_truthvalue_conversion (location, op0);
9697 op1 = c_common_truthvalue_conversion (location, op1);
9698 converted = 1;
9699 boolean_op = true;
9701 if (code == TRUTH_ANDIF_EXPR)
9703 int_const_or_overflow = (int_operands
9704 && TREE_CODE (orig_op0) == INTEGER_CST
9705 && (op0 == truthvalue_false_node
9706 || TREE_CODE (orig_op1) == INTEGER_CST));
9707 int_const = (int_const_or_overflow
9708 && !TREE_OVERFLOW (orig_op0)
9709 && (op0 == truthvalue_false_node
9710 || !TREE_OVERFLOW (orig_op1)));
9712 else if (code == TRUTH_ORIF_EXPR)
9714 int_const_or_overflow = (int_operands
9715 && TREE_CODE (orig_op0) == INTEGER_CST
9716 && (op0 == truthvalue_true_node
9717 || TREE_CODE (orig_op1) == INTEGER_CST));
9718 int_const = (int_const_or_overflow
9719 && !TREE_OVERFLOW (orig_op0)
9720 && (op0 == truthvalue_true_node
9721 || !TREE_OVERFLOW (orig_op1)));
9723 break;
9725 /* Shift operations: result has same type as first operand;
9726 always convert second operand to int.
9727 Also set SHORT_SHIFT if shifting rightward. */
9729 case RSHIFT_EXPR:
9730 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9731 && code1 == INTEGER_TYPE)
9733 if (TREE_CODE (op1) == INTEGER_CST)
9735 if (tree_int_cst_sgn (op1) < 0)
9737 int_const = false;
9738 if (c_inhibit_evaluation_warnings == 0)
9739 warning (0, "right shift count is negative");
9741 else
9743 if (!integer_zerop (op1))
9744 short_shift = 1;
9746 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9748 int_const = false;
9749 if (c_inhibit_evaluation_warnings == 0)
9750 warning (0, "right shift count >= width of type");
9755 /* Use the type of the value to be shifted. */
9756 result_type = type0;
9757 /* Convert the shift-count to an integer, regardless of size
9758 of value being shifted. */
9759 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9760 op1 = convert (integer_type_node, op1);
9761 /* Avoid converting op1 to result_type later. */
9762 converted = 1;
9764 break;
9766 case LSHIFT_EXPR:
9767 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9768 && code1 == INTEGER_TYPE)
9770 if (TREE_CODE (op1) == INTEGER_CST)
9772 if (tree_int_cst_sgn (op1) < 0)
9774 int_const = false;
9775 if (c_inhibit_evaluation_warnings == 0)
9776 warning (0, "left shift count is negative");
9779 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9781 int_const = false;
9782 if (c_inhibit_evaluation_warnings == 0)
9783 warning (0, "left shift count >= width of type");
9787 /* Use the type of the value to be shifted. */
9788 result_type = type0;
9789 /* Convert the shift-count to an integer, regardless of size
9790 of value being shifted. */
9791 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9792 op1 = convert (integer_type_node, op1);
9793 /* Avoid converting op1 to result_type later. */
9794 converted = 1;
9796 break;
9798 case EQ_EXPR:
9799 case NE_EXPR:
9800 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9801 warning_at (location,
9802 OPT_Wfloat_equal,
9803 "comparing floating point with == or != is unsafe");
9804 /* Result of comparison is always int,
9805 but don't convert the args to int! */
9806 build_type = integer_type_node;
9807 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9808 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9809 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9810 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9811 short_compare = 1;
9812 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9814 if (TREE_CODE (op0) == ADDR_EXPR
9815 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9817 if (code == EQ_EXPR)
9818 warning_at (location,
9819 OPT_Waddress,
9820 "the comparison will always evaluate as %<false%> "
9821 "for the address of %qD will never be NULL",
9822 TREE_OPERAND (op0, 0));
9823 else
9824 warning_at (location,
9825 OPT_Waddress,
9826 "the comparison will always evaluate as %<true%> "
9827 "for the address of %qD will never be NULL",
9828 TREE_OPERAND (op0, 0));
9830 result_type = type0;
9832 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9834 if (TREE_CODE (op1) == ADDR_EXPR
9835 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9837 if (code == EQ_EXPR)
9838 warning_at (location,
9839 OPT_Waddress,
9840 "the comparison will always evaluate as %<false%> "
9841 "for the address of %qD will never be NULL",
9842 TREE_OPERAND (op1, 0));
9843 else
9844 warning_at (location,
9845 OPT_Waddress,
9846 "the comparison will always evaluate as %<true%> "
9847 "for the address of %qD will never be NULL",
9848 TREE_OPERAND (op1, 0));
9850 result_type = type1;
9852 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9854 tree tt0 = TREE_TYPE (type0);
9855 tree tt1 = TREE_TYPE (type1);
9856 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
9857 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
9858 addr_space_t as_common = ADDR_SPACE_GENERIC;
9860 /* Anything compares with void *. void * compares with anything.
9861 Otherwise, the targets must be compatible
9862 and both must be object or both incomplete. */
9863 if (comp_target_types (location, type0, type1))
9864 result_type = common_pointer_type (type0, type1);
9865 else if (!addr_space_superset (as0, as1, &as_common))
9867 error_at (location, "comparison of pointers to "
9868 "disjoint address spaces");
9869 return error_mark_node;
9871 else if (VOID_TYPE_P (tt0))
9873 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
9874 pedwarn (location, OPT_pedantic, "ISO C forbids "
9875 "comparison of %<void *%> with function pointer");
9877 else if (VOID_TYPE_P (tt1))
9879 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
9880 pedwarn (location, OPT_pedantic, "ISO C forbids "
9881 "comparison of %<void *%> with function pointer");
9883 else
9884 /* Avoid warning about the volatile ObjC EH puts on decls. */
9885 if (!objc_ok)
9886 pedwarn (location, 0,
9887 "comparison of distinct pointer types lacks a cast");
9889 if (result_type == NULL_TREE)
9891 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9892 result_type = build_pointer_type
9893 (build_qualified_type (void_type_node, qual));
9896 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9898 result_type = type0;
9899 pedwarn (location, 0, "comparison between pointer and integer");
9901 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9903 result_type = type1;
9904 pedwarn (location, 0, "comparison between pointer and integer");
9906 break;
9908 case LE_EXPR:
9909 case GE_EXPR:
9910 case LT_EXPR:
9911 case GT_EXPR:
9912 build_type = integer_type_node;
9913 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9914 || code0 == FIXED_POINT_TYPE)
9915 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9916 || code1 == FIXED_POINT_TYPE))
9917 short_compare = 1;
9918 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9920 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
9921 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
9922 addr_space_t as_common;
9924 if (comp_target_types (location, type0, type1))
9926 result_type = common_pointer_type (type0, type1);
9927 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9928 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9929 pedwarn (location, 0,
9930 "comparison of complete and incomplete pointers");
9931 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9932 pedwarn (location, OPT_pedantic, "ISO C forbids "
9933 "ordered comparisons of pointers to functions");
9934 else if (null_pointer_constant_p (orig_op0)
9935 || null_pointer_constant_p (orig_op1))
9936 warning_at (location, OPT_Wextra,
9937 "ordered comparison of pointer with null pointer");
9940 else if (!addr_space_superset (as0, as1, &as_common))
9942 error_at (location, "comparison of pointers to "
9943 "disjoint address spaces");
9944 return error_mark_node;
9946 else
9948 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9949 result_type = build_pointer_type
9950 (build_qualified_type (void_type_node, qual));
9951 pedwarn (location, 0,
9952 "comparison of distinct pointer types lacks a cast");
9955 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9957 result_type = type0;
9958 if (pedantic)
9959 pedwarn (location, OPT_pedantic,
9960 "ordered comparison of pointer with integer zero");
9961 else if (extra_warnings)
9962 warning_at (location, OPT_Wextra,
9963 "ordered comparison of pointer with integer zero");
9965 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9967 result_type = type1;
9968 if (pedantic)
9969 pedwarn (location, OPT_pedantic,
9970 "ordered comparison of pointer with integer zero");
9971 else if (extra_warnings)
9972 warning_at (location, OPT_Wextra,
9973 "ordered comparison of pointer with integer zero");
9975 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9977 result_type = type0;
9978 pedwarn (location, 0, "comparison between pointer and integer");
9980 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9982 result_type = type1;
9983 pedwarn (location, 0, "comparison between pointer and integer");
9985 break;
9987 default:
9988 gcc_unreachable ();
9991 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9992 return error_mark_node;
9994 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9995 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9996 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9997 TREE_TYPE (type1))))
9999 binary_op_error (location, code, type0, type1);
10000 return error_mark_node;
10003 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10004 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10006 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10007 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10009 bool first_complex = (code0 == COMPLEX_TYPE);
10010 bool second_complex = (code1 == COMPLEX_TYPE);
10011 int none_complex = (!first_complex && !second_complex);
10013 if (shorten || common || short_compare)
10015 result_type = c_common_type (type0, type1);
10016 do_warn_double_promotion (result_type, type0, type1,
10017 "implicit conversion from %qT to %qT "
10018 "to match other operand of binary "
10019 "expression",
10020 location);
10021 if (result_type == error_mark_node)
10022 return error_mark_node;
10025 if (first_complex != second_complex
10026 && (code == PLUS_EXPR
10027 || code == MINUS_EXPR
10028 || code == MULT_EXPR
10029 || (code == TRUNC_DIV_EXPR && first_complex))
10030 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10031 && flag_signed_zeros)
10033 /* An operation on mixed real/complex operands must be
10034 handled specially, but the language-independent code can
10035 more easily optimize the plain complex arithmetic if
10036 -fno-signed-zeros. */
10037 tree real_type = TREE_TYPE (result_type);
10038 tree real, imag;
10039 if (type0 != orig_type0 || type1 != orig_type1)
10041 gcc_assert (may_need_excess_precision && common);
10042 semantic_result_type = c_common_type (orig_type0, orig_type1);
10044 if (first_complex)
10046 if (TREE_TYPE (op0) != result_type)
10047 op0 = convert_and_check (result_type, op0);
10048 if (TREE_TYPE (op1) != real_type)
10049 op1 = convert_and_check (real_type, op1);
10051 else
10053 if (TREE_TYPE (op0) != real_type)
10054 op0 = convert_and_check (real_type, op0);
10055 if (TREE_TYPE (op1) != result_type)
10056 op1 = convert_and_check (result_type, op1);
10058 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10059 return error_mark_node;
10060 if (first_complex)
10062 op0 = c_save_expr (op0);
10063 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10064 op0, 1);
10065 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10066 op0, 1);
10067 switch (code)
10069 case MULT_EXPR:
10070 case TRUNC_DIV_EXPR:
10071 imag = build2 (resultcode, real_type, imag, op1);
10072 /* Fall through. */
10073 case PLUS_EXPR:
10074 case MINUS_EXPR:
10075 real = build2 (resultcode, real_type, real, op1);
10076 break;
10077 default:
10078 gcc_unreachable();
10081 else
10083 op1 = c_save_expr (op1);
10084 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10085 op1, 1);
10086 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10087 op1, 1);
10088 switch (code)
10090 case MULT_EXPR:
10091 imag = build2 (resultcode, real_type, op0, imag);
10092 /* Fall through. */
10093 case PLUS_EXPR:
10094 real = build2 (resultcode, real_type, op0, real);
10095 break;
10096 case MINUS_EXPR:
10097 real = build2 (resultcode, real_type, op0, real);
10098 imag = build1 (NEGATE_EXPR, real_type, imag);
10099 break;
10100 default:
10101 gcc_unreachable();
10104 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10105 goto return_build_binary_op;
10108 /* For certain operations (which identify themselves by shorten != 0)
10109 if both args were extended from the same smaller type,
10110 do the arithmetic in that type and then extend.
10112 shorten !=0 and !=1 indicates a bitwise operation.
10113 For them, this optimization is safe only if
10114 both args are zero-extended or both are sign-extended.
10115 Otherwise, we might change the result.
10116 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10117 but calculated in (unsigned short) it would be (unsigned short)-1. */
10119 if (shorten && none_complex)
10121 final_type = result_type;
10122 result_type = shorten_binary_op (result_type, op0, op1,
10123 shorten == -1);
10126 /* Shifts can be shortened if shifting right. */
10128 if (short_shift)
10130 int unsigned_arg;
10131 tree arg0 = get_narrower (op0, &unsigned_arg);
10133 final_type = result_type;
10135 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10136 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10138 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10139 && tree_int_cst_sgn (op1) > 0
10140 /* We can shorten only if the shift count is less than the
10141 number of bits in the smaller type size. */
10142 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10143 /* We cannot drop an unsigned shift after sign-extension. */
10144 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10146 /* Do an unsigned shift if the operand was zero-extended. */
10147 result_type
10148 = c_common_signed_or_unsigned_type (unsigned_arg,
10149 TREE_TYPE (arg0));
10150 /* Convert value-to-be-shifted to that type. */
10151 if (TREE_TYPE (op0) != result_type)
10152 op0 = convert (result_type, op0);
10153 converted = 1;
10157 /* Comparison operations are shortened too but differently.
10158 They identify themselves by setting short_compare = 1. */
10160 if (short_compare)
10162 /* Don't write &op0, etc., because that would prevent op0
10163 from being kept in a register.
10164 Instead, make copies of the our local variables and
10165 pass the copies by reference, then copy them back afterward. */
10166 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10167 enum tree_code xresultcode = resultcode;
10168 tree val
10169 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10171 if (val != 0)
10173 ret = val;
10174 goto return_build_binary_op;
10177 op0 = xop0, op1 = xop1;
10178 converted = 1;
10179 resultcode = xresultcode;
10181 if (c_inhibit_evaluation_warnings == 0)
10183 bool op0_maybe_const = true;
10184 bool op1_maybe_const = true;
10185 tree orig_op0_folded, orig_op1_folded;
10187 if (in_late_binary_op)
10189 orig_op0_folded = orig_op0;
10190 orig_op1_folded = orig_op1;
10192 else
10194 /* Fold for the sake of possible warnings, as in
10195 build_conditional_expr. This requires the
10196 "original" values to be folded, not just op0 and
10197 op1. */
10198 c_inhibit_evaluation_warnings++;
10199 op0 = c_fully_fold (op0, require_constant_value,
10200 &op0_maybe_const);
10201 op1 = c_fully_fold (op1, require_constant_value,
10202 &op1_maybe_const);
10203 c_inhibit_evaluation_warnings--;
10204 orig_op0_folded = c_fully_fold (orig_op0,
10205 require_constant_value,
10206 NULL);
10207 orig_op1_folded = c_fully_fold (orig_op1,
10208 require_constant_value,
10209 NULL);
10212 if (warn_sign_compare)
10213 warn_for_sign_compare (location, orig_op0_folded,
10214 orig_op1_folded, op0, op1,
10215 result_type, resultcode);
10216 if (!in_late_binary_op)
10218 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10219 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10220 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10221 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10227 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10228 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10229 Then the expression will be built.
10230 It will be given type FINAL_TYPE if that is nonzero;
10231 otherwise, it will be given type RESULT_TYPE. */
10233 if (!result_type)
10235 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10236 return error_mark_node;
10239 if (build_type == NULL_TREE)
10241 build_type = result_type;
10242 if ((type0 != orig_type0 || type1 != orig_type1)
10243 && !boolean_op)
10245 gcc_assert (may_need_excess_precision && common);
10246 semantic_result_type = c_common_type (orig_type0, orig_type1);
10250 if (!converted)
10252 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10253 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10255 /* This can happen if one operand has a vector type, and the other
10256 has a different type. */
10257 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10258 return error_mark_node;
10261 /* Treat expressions in initializers specially as they can't trap. */
10262 if (int_const_or_overflow)
10263 ret = (require_constant_value
10264 ? fold_build2_initializer_loc (location, resultcode, build_type,
10265 op0, op1)
10266 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10267 else
10268 ret = build2 (resultcode, build_type, op0, op1);
10269 if (final_type != 0)
10270 ret = convert (final_type, ret);
10272 return_build_binary_op:
10273 gcc_assert (ret != error_mark_node);
10274 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10275 ret = (int_operands
10276 ? note_integer_operands (ret)
10277 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10278 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10279 && !in_late_binary_op)
10280 ret = note_integer_operands (ret);
10281 if (semantic_result_type)
10282 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10283 protected_set_expr_location (ret, location);
10284 return ret;
10288 /* Convert EXPR to be a truth-value, validating its type for this
10289 purpose. LOCATION is the source location for the expression. */
10291 tree
10292 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10294 bool int_const, int_operands;
10296 switch (TREE_CODE (TREE_TYPE (expr)))
10298 case ARRAY_TYPE:
10299 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10300 return error_mark_node;
10302 case RECORD_TYPE:
10303 error_at (location, "used struct type value where scalar is required");
10304 return error_mark_node;
10306 case UNION_TYPE:
10307 error_at (location, "used union type value where scalar is required");
10308 return error_mark_node;
10310 case FUNCTION_TYPE:
10311 gcc_unreachable ();
10313 default:
10314 break;
10317 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10318 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10319 if (int_operands)
10320 expr = remove_c_maybe_const_expr (expr);
10322 /* ??? Should we also give an error for void and vectors rather than
10323 leaving those to give errors later? */
10324 expr = c_common_truthvalue_conversion (location, expr);
10326 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10328 if (TREE_OVERFLOW (expr))
10329 return expr;
10330 else
10331 return note_integer_operands (expr);
10333 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10334 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10335 return expr;
10339 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10340 required. */
10342 tree
10343 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10345 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10347 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10348 /* Executing a compound literal inside a function reinitializes
10349 it. */
10350 if (!TREE_STATIC (decl))
10351 *se = true;
10352 return decl;
10354 else
10355 return expr;
10358 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10360 tree
10361 c_begin_omp_parallel (void)
10363 tree block;
10365 keep_next_level ();
10366 block = c_begin_compound_stmt (true);
10368 return block;
10371 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10372 statement. LOC is the location of the OMP_PARALLEL. */
10374 tree
10375 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10377 tree stmt;
10379 block = c_end_compound_stmt (loc, block, true);
10381 stmt = make_node (OMP_PARALLEL);
10382 TREE_TYPE (stmt) = void_type_node;
10383 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10384 OMP_PARALLEL_BODY (stmt) = block;
10385 SET_EXPR_LOCATION (stmt, loc);
10387 return add_stmt (stmt);
10390 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10392 tree
10393 c_begin_omp_task (void)
10395 tree block;
10397 keep_next_level ();
10398 block = c_begin_compound_stmt (true);
10400 return block;
10403 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10404 statement. LOC is the location of the #pragma. */
10406 tree
10407 c_finish_omp_task (location_t loc, tree clauses, tree block)
10409 tree stmt;
10411 block = c_end_compound_stmt (loc, block, true);
10413 stmt = make_node (OMP_TASK);
10414 TREE_TYPE (stmt) = void_type_node;
10415 OMP_TASK_CLAUSES (stmt) = clauses;
10416 OMP_TASK_BODY (stmt) = block;
10417 SET_EXPR_LOCATION (stmt, loc);
10419 return add_stmt (stmt);
10422 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10423 Remove any elements from the list that are invalid. */
10425 tree
10426 c_finish_omp_clauses (tree clauses)
10428 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10429 tree c, t, *pc = &clauses;
10430 const char *name;
10432 bitmap_obstack_initialize (NULL);
10433 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10434 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10435 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10437 for (pc = &clauses, c = clauses; c ; c = *pc)
10439 bool remove = false;
10440 bool need_complete = false;
10441 bool need_implicitly_determined = false;
10443 switch (OMP_CLAUSE_CODE (c))
10445 case OMP_CLAUSE_SHARED:
10446 name = "shared";
10447 need_implicitly_determined = true;
10448 goto check_dup_generic;
10450 case OMP_CLAUSE_PRIVATE:
10451 name = "private";
10452 need_complete = true;
10453 need_implicitly_determined = true;
10454 goto check_dup_generic;
10456 case OMP_CLAUSE_REDUCTION:
10457 name = "reduction";
10458 need_implicitly_determined = true;
10459 t = OMP_CLAUSE_DECL (c);
10460 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10461 || POINTER_TYPE_P (TREE_TYPE (t)))
10463 error_at (OMP_CLAUSE_LOCATION (c),
10464 "%qE has invalid type for %<reduction%>", t);
10465 remove = true;
10467 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10469 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10470 const char *r_name = NULL;
10472 switch (r_code)
10474 case PLUS_EXPR:
10475 case MULT_EXPR:
10476 case MINUS_EXPR:
10477 break;
10478 case BIT_AND_EXPR:
10479 r_name = "&";
10480 break;
10481 case BIT_XOR_EXPR:
10482 r_name = "^";
10483 break;
10484 case BIT_IOR_EXPR:
10485 r_name = "|";
10486 break;
10487 case TRUTH_ANDIF_EXPR:
10488 r_name = "&&";
10489 break;
10490 case TRUTH_ORIF_EXPR:
10491 r_name = "||";
10492 break;
10493 default:
10494 gcc_unreachable ();
10496 if (r_name)
10498 error_at (OMP_CLAUSE_LOCATION (c),
10499 "%qE has invalid type for %<reduction(%s)%>",
10500 t, r_name);
10501 remove = true;
10504 goto check_dup_generic;
10506 case OMP_CLAUSE_COPYPRIVATE:
10507 name = "copyprivate";
10508 goto check_dup_generic;
10510 case OMP_CLAUSE_COPYIN:
10511 name = "copyin";
10512 t = OMP_CLAUSE_DECL (c);
10513 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10515 error_at (OMP_CLAUSE_LOCATION (c),
10516 "%qE must be %<threadprivate%> for %<copyin%>", t);
10517 remove = true;
10519 goto check_dup_generic;
10521 check_dup_generic:
10522 t = OMP_CLAUSE_DECL (c);
10523 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10525 error_at (OMP_CLAUSE_LOCATION (c),
10526 "%qE is not a variable in clause %qs", t, name);
10527 remove = true;
10529 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10530 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10531 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10533 error_at (OMP_CLAUSE_LOCATION (c),
10534 "%qE appears more than once in data clauses", t);
10535 remove = true;
10537 else
10538 bitmap_set_bit (&generic_head, DECL_UID (t));
10539 break;
10541 case OMP_CLAUSE_FIRSTPRIVATE:
10542 name = "firstprivate";
10543 t = OMP_CLAUSE_DECL (c);
10544 need_complete = true;
10545 need_implicitly_determined = true;
10546 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10548 error_at (OMP_CLAUSE_LOCATION (c),
10549 "%qE is not a variable in clause %<firstprivate%>", t);
10550 remove = true;
10552 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10553 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10555 error_at (OMP_CLAUSE_LOCATION (c),
10556 "%qE appears more than once in data clauses", t);
10557 remove = true;
10559 else
10560 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10561 break;
10563 case OMP_CLAUSE_LASTPRIVATE:
10564 name = "lastprivate";
10565 t = OMP_CLAUSE_DECL (c);
10566 need_complete = true;
10567 need_implicitly_determined = true;
10568 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10570 error_at (OMP_CLAUSE_LOCATION (c),
10571 "%qE is not a variable in clause %<lastprivate%>", t);
10572 remove = true;
10574 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10575 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10577 error_at (OMP_CLAUSE_LOCATION (c),
10578 "%qE appears more than once in data clauses", t);
10579 remove = true;
10581 else
10582 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10583 break;
10585 case OMP_CLAUSE_IF:
10586 case OMP_CLAUSE_NUM_THREADS:
10587 case OMP_CLAUSE_SCHEDULE:
10588 case OMP_CLAUSE_NOWAIT:
10589 case OMP_CLAUSE_ORDERED:
10590 case OMP_CLAUSE_DEFAULT:
10591 case OMP_CLAUSE_UNTIED:
10592 case OMP_CLAUSE_COLLAPSE:
10593 pc = &OMP_CLAUSE_CHAIN (c);
10594 continue;
10596 default:
10597 gcc_unreachable ();
10600 if (!remove)
10602 t = OMP_CLAUSE_DECL (c);
10604 if (need_complete)
10606 t = require_complete_type (t);
10607 if (t == error_mark_node)
10608 remove = true;
10611 if (need_implicitly_determined)
10613 const char *share_name = NULL;
10615 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10616 share_name = "threadprivate";
10617 else switch (c_omp_predetermined_sharing (t))
10619 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10620 break;
10621 case OMP_CLAUSE_DEFAULT_SHARED:
10622 share_name = "shared";
10623 break;
10624 case OMP_CLAUSE_DEFAULT_PRIVATE:
10625 share_name = "private";
10626 break;
10627 default:
10628 gcc_unreachable ();
10630 if (share_name)
10632 error_at (OMP_CLAUSE_LOCATION (c),
10633 "%qE is predetermined %qs for %qs",
10634 t, share_name, name);
10635 remove = true;
10640 if (remove)
10641 *pc = OMP_CLAUSE_CHAIN (c);
10642 else
10643 pc = &OMP_CLAUSE_CHAIN (c);
10646 bitmap_obstack_release (NULL);
10647 return clauses;
10650 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10651 down to the element type of an array. */
10653 tree
10654 c_build_qualified_type (tree type, int type_quals)
10656 if (type == error_mark_node)
10657 return type;
10659 if (TREE_CODE (type) == ARRAY_TYPE)
10661 tree t;
10662 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10663 type_quals);
10665 /* See if we already have an identically qualified type. */
10666 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10668 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10669 && TYPE_NAME (t) == TYPE_NAME (type)
10670 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10671 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10672 TYPE_ATTRIBUTES (type)))
10673 break;
10675 if (!t)
10677 tree domain = TYPE_DOMAIN (type);
10679 t = build_variant_type_copy (type);
10680 TREE_TYPE (t) = element_type;
10682 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10683 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10684 SET_TYPE_STRUCTURAL_EQUALITY (t);
10685 else if (TYPE_CANONICAL (element_type) != element_type
10686 || (domain && TYPE_CANONICAL (domain) != domain))
10688 tree unqualified_canon
10689 = build_array_type (TYPE_CANONICAL (element_type),
10690 domain? TYPE_CANONICAL (domain)
10691 : NULL_TREE);
10692 TYPE_CANONICAL (t)
10693 = c_build_qualified_type (unqualified_canon, type_quals);
10695 else
10696 TYPE_CANONICAL (t) = t;
10698 return t;
10701 /* A restrict-qualified pointer type must be a pointer to object or
10702 incomplete type. Note that the use of POINTER_TYPE_P also allows
10703 REFERENCE_TYPEs, which is appropriate for C++. */
10704 if ((type_quals & TYPE_QUAL_RESTRICT)
10705 && (!POINTER_TYPE_P (type)
10706 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10708 error ("invalid use of %<restrict%>");
10709 type_quals &= ~TYPE_QUAL_RESTRICT;
10712 return build_qualified_type (type, type_quals);
10715 /* Build a VA_ARG_EXPR for the C parser. */
10717 tree
10718 c_build_va_arg (location_t loc, tree expr, tree type)
10720 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10721 warning_at (loc, OPT_Wc___compat,
10722 "C++ requires promoted type, not enum type, in %<va_arg%>");
10723 return build_va_arg (loc, expr, type);