c-tree.h (enum c_typespec_kind): Add ctsk_none.
[official-gcc.git] / gcc / c-typeck.c
blob9018c4e933c4412af55c8729b3722a9e70988c18
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "langhooks.h"
34 #include "c-tree.h"
35 #include "c-lang.h"
36 #include "flags.h"
37 #include "output.h"
38 #include "toplev.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "tree-iterator.h"
42 #include "bitmap.h"
43 #include "gimple.h"
45 /* Possible cases of implicit bad conversions. Used to select
46 diagnostic messages in convert_for_assignment. */
47 enum impl_conv {
48 ic_argpass,
49 ic_assign,
50 ic_init,
51 ic_return
54 /* Whether we are building a boolean conversion inside
55 convert_for_assignment, or some other late binary operation. If
56 build_binary_op is called (from code shared with C++) in this case,
57 then the operands have already been folded and the result will not
58 be folded again, so C_MAYBE_CONST_EXPR should not be generated. */
59 bool in_late_binary_op;
61 /* The level of nesting inside "__alignof__". */
62 int in_alignof;
64 /* The level of nesting inside "sizeof". */
65 int in_sizeof;
67 /* The level of nesting inside "typeof". */
68 int in_typeof;
70 /* Nonzero if we've already printed a "missing braces around initializer"
71 message within this initializer. */
72 static int missing_braces_mentioned;
74 static int require_constant_value;
75 static int require_constant_elements;
77 static bool null_pointer_constant_p (const_tree);
78 static tree qualify_type (tree, tree);
79 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
80 bool *);
81 static int comp_target_types (location_t, tree, tree);
82 static int function_types_compatible_p (const_tree, const_tree, bool *,
83 bool *);
84 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
85 static tree lookup_field (tree, tree);
86 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
87 tree);
88 static tree pointer_diff (location_t, tree, tree);
89 static tree convert_for_assignment (location_t, tree, tree, tree,
90 enum impl_conv, bool, tree, tree, int);
91 static tree valid_compound_expr_initializer (tree, tree);
92 static void push_string (const char *);
93 static void push_member_name (tree);
94 static int spelling_length (void);
95 static char *print_spelling (char *);
96 static void warning_init (int, const char *);
97 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
98 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
99 struct obstack *);
100 static void output_pending_init_elements (int, struct obstack *);
101 static int set_designator (int, struct obstack *);
102 static void push_range_stack (tree, struct obstack *);
103 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
104 static void set_nonincremental_init (struct obstack *);
105 static void set_nonincremental_init_from_string (tree, struct obstack *);
106 static tree find_init_member (tree, struct obstack *);
107 static void readonly_error (tree, enum lvalue_use);
108 static void readonly_warning (tree, enum lvalue_use);
109 static int lvalue_or_else (const_tree, enum lvalue_use);
110 static void record_maybe_used_decl (tree);
111 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
113 /* Return true if EXP is a null pointer constant, false otherwise. */
115 static bool
116 null_pointer_constant_p (const_tree expr)
118 /* This should really operate on c_expr structures, but they aren't
119 yet available everywhere required. */
120 tree type = TREE_TYPE (expr);
121 return (TREE_CODE (expr) == INTEGER_CST
122 && !TREE_OVERFLOW (expr)
123 && integer_zerop (expr)
124 && (INTEGRAL_TYPE_P (type)
125 || (TREE_CODE (type) == POINTER_TYPE
126 && VOID_TYPE_P (TREE_TYPE (type))
127 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
130 /* EXPR may appear in an unevaluated part of an integer constant
131 expression, but not in an evaluated part. Wrap it in a
132 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
133 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
135 static tree
136 note_integer_operands (tree expr)
138 tree ret;
139 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
141 ret = copy_node (expr);
142 TREE_OVERFLOW (ret) = 1;
144 else
146 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
147 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
149 return ret;
152 /* Having checked whether EXPR may appear in an unevaluated part of an
153 integer constant expression and found that it may, remove any
154 C_MAYBE_CONST_EXPR noting this fact and return the resulting
155 expression. */
157 static inline tree
158 remove_c_maybe_const_expr (tree expr)
160 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
161 return C_MAYBE_CONST_EXPR_EXPR (expr);
162 else
163 return expr;
166 \f/* This is a cache to hold if two types are compatible or not. */
168 struct tagged_tu_seen_cache {
169 const struct tagged_tu_seen_cache * next;
170 const_tree t1;
171 const_tree t2;
172 /* The return value of tagged_types_tu_compatible_p if we had seen
173 these two types already. */
174 int val;
177 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
178 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
180 /* Do `exp = require_complete_type (exp);' to make sure exp
181 does not have an incomplete type. (That includes void types.) */
183 tree
184 require_complete_type (tree value)
186 tree type = TREE_TYPE (value);
188 if (value == error_mark_node || type == error_mark_node)
189 return error_mark_node;
191 /* First, detect a valid value with a complete type. */
192 if (COMPLETE_TYPE_P (type))
193 return value;
195 c_incomplete_type_error (value, type);
196 return error_mark_node;
199 /* Print an error message for invalid use of an incomplete type.
200 VALUE is the expression that was used (or 0 if that isn't known)
201 and TYPE is the type that was invalid. */
203 void
204 c_incomplete_type_error (const_tree value, const_tree type)
206 const char *type_code_string;
208 /* Avoid duplicate error message. */
209 if (TREE_CODE (type) == ERROR_MARK)
210 return;
212 if (value != 0 && (TREE_CODE (value) == VAR_DECL
213 || TREE_CODE (value) == PARM_DECL))
214 error ("%qD has an incomplete type", value);
215 else
217 retry:
218 /* We must print an error message. Be clever about what it says. */
220 switch (TREE_CODE (type))
222 case RECORD_TYPE:
223 type_code_string = "struct";
224 break;
226 case UNION_TYPE:
227 type_code_string = "union";
228 break;
230 case ENUMERAL_TYPE:
231 type_code_string = "enum";
232 break;
234 case VOID_TYPE:
235 error ("invalid use of void expression");
236 return;
238 case ARRAY_TYPE:
239 if (TYPE_DOMAIN (type))
241 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
243 error ("invalid use of flexible array member");
244 return;
246 type = TREE_TYPE (type);
247 goto retry;
249 error ("invalid use of array with unspecified bounds");
250 return;
252 default:
253 gcc_unreachable ();
256 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
257 error ("invalid use of undefined type %<%s %E%>",
258 type_code_string, TYPE_NAME (type));
259 else
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
261 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
265 /* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
268 tree
269 c_type_promotes_to (tree type)
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 return double_type_node;
274 if (c_promoting_integer_type_p (type))
276 /* Preserve unsignedness if not really getting any wider. */
277 if (TYPE_UNSIGNED (type)
278 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
279 return unsigned_type_node;
280 return integer_type_node;
283 return type;
286 /* Return true if between two named address spaces, whether there is a superset
287 named address space that encompasses both address spaces. If there is a
288 superset, return which address space is the superset. */
290 static bool
291 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
293 if (as1 == as2)
295 *common = as1;
296 return true;
298 else if (targetm.addr_space.subset_p (as1, as2))
300 *common = as2;
301 return true;
303 else if (targetm.addr_space.subset_p (as2, as1))
305 *common = as1;
306 return true;
308 else
309 return false;
312 /* Return a variant of TYPE which has all the type qualifiers of LIKE
313 as well as those of TYPE. */
315 static tree
316 qualify_type (tree type, tree like)
318 addr_space_t as_type = TYPE_ADDR_SPACE (type);
319 addr_space_t as_like = TYPE_ADDR_SPACE (like);
320 addr_space_t as_common;
322 /* If the two named address spaces are different, determine the common
323 superset address space. If there isn't one, raise an error. */
324 if (!addr_space_superset (as_type, as_like, &as_common))
326 as_common = as_type;
327 error ("%qT and %qT are in disjoint named address spaces",
328 type, like);
331 return c_build_qualified_type (type,
332 TYPE_QUALS_NO_ADDR_SPACE (type)
333 | TYPE_QUALS_NO_ADDR_SPACE (like)
334 | ENCODE_QUAL_ADDR_SPACE (as_common));
337 /* Return true iff the given tree T is a variable length array. */
339 bool
340 c_vla_type_p (const_tree t)
342 if (TREE_CODE (t) == ARRAY_TYPE
343 && C_TYPE_VARIABLE_SIZE (t))
344 return true;
345 return false;
348 /* Return the composite type of two compatible types.
350 We assume that comptypes has already been done and returned
351 nonzero; if that isn't so, this may crash. In particular, we
352 assume that qualifiers match. */
354 tree
355 composite_type (tree t1, tree t2)
357 enum tree_code code1;
358 enum tree_code code2;
359 tree attributes;
361 /* Save time if the two types are the same. */
363 if (t1 == t2) return t1;
365 /* If one type is nonsense, use the other. */
366 if (t1 == error_mark_node)
367 return t2;
368 if (t2 == error_mark_node)
369 return t1;
371 code1 = TREE_CODE (t1);
372 code2 = TREE_CODE (t2);
374 /* Merge the attributes. */
375 attributes = targetm.merge_type_attributes (t1, t2);
377 /* If one is an enumerated type and the other is the compatible
378 integer type, the composite type might be either of the two
379 (DR#013 question 3). For consistency, use the enumerated type as
380 the composite type. */
382 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
383 return t1;
384 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
385 return t2;
387 gcc_assert (code1 == code2);
389 switch (code1)
391 case POINTER_TYPE:
392 /* For two pointers, do this recursively on the target type. */
394 tree pointed_to_1 = TREE_TYPE (t1);
395 tree pointed_to_2 = TREE_TYPE (t2);
396 tree target = composite_type (pointed_to_1, pointed_to_2);
397 t1 = build_pointer_type (target);
398 t1 = build_type_attribute_variant (t1, attributes);
399 return qualify_type (t1, t2);
402 case ARRAY_TYPE:
404 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
405 int quals;
406 tree unqual_elt;
407 tree d1 = TYPE_DOMAIN (t1);
408 tree d2 = TYPE_DOMAIN (t2);
409 bool d1_variable, d2_variable;
410 bool d1_zero, d2_zero;
411 bool t1_complete, t2_complete;
413 /* We should not have any type quals on arrays at all. */
414 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
415 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
417 t1_complete = COMPLETE_TYPE_P (t1);
418 t2_complete = COMPLETE_TYPE_P (t2);
420 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
421 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
423 d1_variable = (!d1_zero
424 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
425 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
426 d2_variable = (!d2_zero
427 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
428 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
429 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
430 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
432 /* Save space: see if the result is identical to one of the args. */
433 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
434 && (d2_variable || d2_zero || !d1_variable))
435 return build_type_attribute_variant (t1, attributes);
436 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
437 && (d1_variable || d1_zero || !d2_variable))
438 return build_type_attribute_variant (t2, attributes);
440 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
441 return build_type_attribute_variant (t1, attributes);
442 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t2, attributes);
445 /* Merge the element types, and have a size if either arg has
446 one. We may have qualifiers on the element types. To set
447 up TYPE_MAIN_VARIANT correctly, we need to form the
448 composite of the unqualified types and add the qualifiers
449 back at the end. */
450 quals = TYPE_QUALS (strip_array_types (elt));
451 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
452 t1 = build_array_type (unqual_elt,
453 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
454 && (d2_variable
455 || d2_zero
456 || !d1_variable))
457 ? t1
458 : t2));
459 /* Ensure a composite type involving a zero-length array type
460 is a zero-length type not an incomplete type. */
461 if (d1_zero && d2_zero
462 && (t1_complete || t2_complete)
463 && !COMPLETE_TYPE_P (t1))
465 TYPE_SIZE (t1) = bitsize_zero_node;
466 TYPE_SIZE_UNIT (t1) = size_zero_node;
468 t1 = c_build_qualified_type (t1, quals);
469 return build_type_attribute_variant (t1, attributes);
472 case ENUMERAL_TYPE:
473 case RECORD_TYPE:
474 case UNION_TYPE:
475 if (attributes != NULL)
477 /* Try harder not to create a new aggregate type. */
478 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
479 return t1;
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
481 return t2;
483 return build_type_attribute_variant (t1, attributes);
485 case FUNCTION_TYPE:
486 /* Function types: prefer the one that specified arg types.
487 If both do, merge the arg types. Also merge the return types. */
489 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
490 tree p1 = TYPE_ARG_TYPES (t1);
491 tree p2 = TYPE_ARG_TYPES (t2);
492 int len;
493 tree newargs, n;
494 int i;
496 /* Save space: see if the result is identical to one of the args. */
497 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
498 return build_type_attribute_variant (t1, attributes);
499 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
500 return build_type_attribute_variant (t2, attributes);
502 /* Simple way if one arg fails to specify argument types. */
503 if (TYPE_ARG_TYPES (t1) == 0)
505 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
506 t1 = build_type_attribute_variant (t1, attributes);
507 return qualify_type (t1, t2);
509 if (TYPE_ARG_TYPES (t2) == 0)
511 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
512 t1 = build_type_attribute_variant (t1, attributes);
513 return qualify_type (t1, t2);
516 /* If both args specify argument types, we must merge the two
517 lists, argument by argument. */
518 /* Tell global_bindings_p to return false so that variable_size
519 doesn't die on VLAs in parameter types. */
520 c_override_global_bindings_to_false = true;
522 len = list_length (p1);
523 newargs = 0;
525 for (i = 0; i < len; i++)
526 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
528 n = newargs;
530 for (; p1;
531 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
533 /* A null type means arg type is not specified.
534 Take whatever the other function type has. */
535 if (TREE_VALUE (p1) == 0)
537 TREE_VALUE (n) = TREE_VALUE (p2);
538 goto parm_done;
540 if (TREE_VALUE (p2) == 0)
542 TREE_VALUE (n) = TREE_VALUE (p1);
543 goto parm_done;
546 /* Given wait (union {union wait *u; int *i} *)
547 and wait (union wait *),
548 prefer union wait * as type of parm. */
549 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
550 && TREE_VALUE (p1) != TREE_VALUE (p2))
552 tree memb;
553 tree mv2 = TREE_VALUE (p2);
554 if (mv2 && mv2 != error_mark_node
555 && TREE_CODE (mv2) != ARRAY_TYPE)
556 mv2 = TYPE_MAIN_VARIANT (mv2);
557 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
558 memb; memb = DECL_CHAIN (memb))
560 tree mv3 = TREE_TYPE (memb);
561 if (mv3 && mv3 != error_mark_node
562 && TREE_CODE (mv3) != ARRAY_TYPE)
563 mv3 = TYPE_MAIN_VARIANT (mv3);
564 if (comptypes (mv3, mv2))
566 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
567 TREE_VALUE (p2));
568 pedwarn (input_location, OPT_pedantic,
569 "function types not truly compatible in ISO C");
570 goto parm_done;
574 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
575 && TREE_VALUE (p2) != TREE_VALUE (p1))
577 tree memb;
578 tree mv1 = TREE_VALUE (p1);
579 if (mv1 && mv1 != error_mark_node
580 && TREE_CODE (mv1) != ARRAY_TYPE)
581 mv1 = TYPE_MAIN_VARIANT (mv1);
582 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
583 memb; memb = DECL_CHAIN (memb))
585 tree mv3 = TREE_TYPE (memb);
586 if (mv3 && mv3 != error_mark_node
587 && TREE_CODE (mv3) != ARRAY_TYPE)
588 mv3 = TYPE_MAIN_VARIANT (mv3);
589 if (comptypes (mv3, mv1))
591 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
592 TREE_VALUE (p1));
593 pedwarn (input_location, OPT_pedantic,
594 "function types not truly compatible in ISO C");
595 goto parm_done;
599 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
600 parm_done: ;
603 c_override_global_bindings_to_false = false;
604 t1 = build_function_type (valtype, newargs);
605 t1 = qualify_type (t1, t2);
606 /* ... falls through ... */
609 default:
610 return build_type_attribute_variant (t1, attributes);
615 /* Return the type of a conditional expression between pointers to
616 possibly differently qualified versions of compatible types.
618 We assume that comp_target_types has already been done and returned
619 nonzero; if that isn't so, this may crash. */
621 static tree
622 common_pointer_type (tree t1, tree t2)
624 tree attributes;
625 tree pointed_to_1, mv1;
626 tree pointed_to_2, mv2;
627 tree target;
628 unsigned target_quals;
629 addr_space_t as1, as2, as_common;
630 int quals1, quals2;
632 /* Save time if the two types are the same. */
634 if (t1 == t2) return t1;
636 /* If one type is nonsense, use the other. */
637 if (t1 == error_mark_node)
638 return t2;
639 if (t2 == error_mark_node)
640 return t1;
642 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
643 && TREE_CODE (t2) == POINTER_TYPE);
645 /* Merge the attributes. */
646 attributes = targetm.merge_type_attributes (t1, t2);
648 /* Find the composite type of the target types, and combine the
649 qualifiers of the two types' targets. Do not lose qualifiers on
650 array element types by taking the TYPE_MAIN_VARIANT. */
651 mv1 = pointed_to_1 = TREE_TYPE (t1);
652 mv2 = pointed_to_2 = TREE_TYPE (t2);
653 if (TREE_CODE (mv1) != ARRAY_TYPE)
654 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
655 if (TREE_CODE (mv2) != ARRAY_TYPE)
656 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
657 target = composite_type (mv1, mv2);
659 /* For function types do not merge const qualifiers, but drop them
660 if used inconsistently. The middle-end uses these to mark const
661 and noreturn functions. */
662 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
663 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
665 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
666 target_quals = (quals1 & quals2);
667 else
668 target_quals = (quals1 | quals2);
670 /* If the two named address spaces are different, determine the common
671 superset address space. This is guaranteed to exist due to the
672 assumption that comp_target_type returned non-zero. */
673 as1 = TYPE_ADDR_SPACE (pointed_to_1);
674 as2 = TYPE_ADDR_SPACE (pointed_to_2);
675 if (!addr_space_superset (as1, as2, &as_common))
676 gcc_unreachable ();
678 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
680 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
681 return build_type_attribute_variant (t1, attributes);
684 /* Return the common type for two arithmetic types under the usual
685 arithmetic conversions. The default conversions have already been
686 applied, and enumerated types converted to their compatible integer
687 types. The resulting type is unqualified and has no attributes.
689 This is the type for the result of most arithmetic operations
690 if the operands have the given two types. */
692 static tree
693 c_common_type (tree t1, tree t2)
695 enum tree_code code1;
696 enum tree_code code2;
698 /* If one type is nonsense, use the other. */
699 if (t1 == error_mark_node)
700 return t2;
701 if (t2 == error_mark_node)
702 return t1;
704 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
705 t1 = TYPE_MAIN_VARIANT (t1);
707 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
708 t2 = TYPE_MAIN_VARIANT (t2);
710 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
711 t1 = build_type_attribute_variant (t1, NULL_TREE);
713 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
714 t2 = build_type_attribute_variant (t2, NULL_TREE);
716 /* Save time if the two types are the same. */
718 if (t1 == t2) return t1;
720 code1 = TREE_CODE (t1);
721 code2 = TREE_CODE (t2);
723 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
724 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
725 || code1 == INTEGER_TYPE);
726 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
727 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
728 || code2 == INTEGER_TYPE);
730 /* When one operand is a decimal float type, the other operand cannot be
731 a generic float type or a complex type. We also disallow vector types
732 here. */
733 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
734 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
736 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
738 error ("can%'t mix operands of decimal float and vector types");
739 return error_mark_node;
741 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
743 error ("can%'t mix operands of decimal float and complex types");
744 return error_mark_node;
746 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
748 error ("can%'t mix operands of decimal float and other float types");
749 return error_mark_node;
753 /* If one type is a vector type, return that type. (How the usual
754 arithmetic conversions apply to the vector types extension is not
755 precisely specified.) */
756 if (code1 == VECTOR_TYPE)
757 return t1;
759 if (code2 == VECTOR_TYPE)
760 return t2;
762 /* If one type is complex, form the common type of the non-complex
763 components, then make that complex. Use T1 or T2 if it is the
764 required type. */
765 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
767 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
768 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
769 tree subtype = c_common_type (subtype1, subtype2);
771 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
772 return t1;
773 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
774 return t2;
775 else
776 return build_complex_type (subtype);
779 /* If only one is real, use it as the result. */
781 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
782 return t1;
784 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
785 return t2;
787 /* If both are real and either are decimal floating point types, use
788 the decimal floating point type with the greater precision. */
790 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
792 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
793 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
794 return dfloat128_type_node;
795 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
796 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
797 return dfloat64_type_node;
798 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
799 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
800 return dfloat32_type_node;
803 /* Deal with fixed-point types. */
804 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
806 unsigned int unsignedp = 0, satp = 0;
807 enum machine_mode m1, m2;
808 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
810 m1 = TYPE_MODE (t1);
811 m2 = TYPE_MODE (t2);
813 /* If one input type is saturating, the result type is saturating. */
814 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
815 satp = 1;
817 /* If both fixed-point types are unsigned, the result type is unsigned.
818 When mixing fixed-point and integer types, follow the sign of the
819 fixed-point type.
820 Otherwise, the result type is signed. */
821 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
822 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
823 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
824 && TYPE_UNSIGNED (t1))
825 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
826 && TYPE_UNSIGNED (t2)))
827 unsignedp = 1;
829 /* The result type is signed. */
830 if (unsignedp == 0)
832 /* If the input type is unsigned, we need to convert to the
833 signed type. */
834 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
836 enum mode_class mclass = (enum mode_class) 0;
837 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
838 mclass = MODE_FRACT;
839 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
840 mclass = MODE_ACCUM;
841 else
842 gcc_unreachable ();
843 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
845 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
847 enum mode_class mclass = (enum mode_class) 0;
848 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
849 mclass = MODE_FRACT;
850 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
851 mclass = MODE_ACCUM;
852 else
853 gcc_unreachable ();
854 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
858 if (code1 == FIXED_POINT_TYPE)
860 fbit1 = GET_MODE_FBIT (m1);
861 ibit1 = GET_MODE_IBIT (m1);
863 else
865 fbit1 = 0;
866 /* Signed integers need to subtract one sign bit. */
867 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
870 if (code2 == FIXED_POINT_TYPE)
872 fbit2 = GET_MODE_FBIT (m2);
873 ibit2 = GET_MODE_IBIT (m2);
875 else
877 fbit2 = 0;
878 /* Signed integers need to subtract one sign bit. */
879 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
882 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
883 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
884 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
885 satp);
888 /* Both real or both integers; use the one with greater precision. */
890 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
891 return t1;
892 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
893 return t2;
895 /* Same precision. Prefer long longs to longs to ints when the
896 same precision, following the C99 rules on integer type rank
897 (which are equivalent to the C90 rules for C90 types). */
899 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
900 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
901 return long_long_unsigned_type_node;
903 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
906 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
907 return long_long_unsigned_type_node;
908 else
909 return long_long_integer_type_node;
912 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
914 return long_unsigned_type_node;
916 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
919 /* But preserve unsignedness from the other type,
920 since long cannot hold all the values of an unsigned int. */
921 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
922 return long_unsigned_type_node;
923 else
924 return long_integer_type_node;
927 /* Likewise, prefer long double to double even if same size. */
928 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
929 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
930 return long_double_type_node;
932 /* Otherwise prefer the unsigned one. */
934 if (TYPE_UNSIGNED (t1))
935 return t1;
936 else
937 return t2;
940 /* Wrapper around c_common_type that is used by c-common.c and other
941 front end optimizations that remove promotions. ENUMERAL_TYPEs
942 are allowed here and are converted to their compatible integer types.
943 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
944 preferably a non-Boolean type as the common type. */
945 tree
946 common_type (tree t1, tree t2)
948 if (TREE_CODE (t1) == ENUMERAL_TYPE)
949 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
950 if (TREE_CODE (t2) == ENUMERAL_TYPE)
951 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
953 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
954 if (TREE_CODE (t1) == BOOLEAN_TYPE
955 && TREE_CODE (t2) == BOOLEAN_TYPE)
956 return boolean_type_node;
958 /* If either type is BOOLEAN_TYPE, then return the other. */
959 if (TREE_CODE (t1) == BOOLEAN_TYPE)
960 return t2;
961 if (TREE_CODE (t2) == BOOLEAN_TYPE)
962 return t1;
964 return c_common_type (t1, t2);
967 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
968 or various other operations. Return 2 if they are compatible
969 but a warning may be needed if you use them together. */
972 comptypes (tree type1, tree type2)
974 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
975 int val;
977 val = comptypes_internal (type1, type2, NULL, NULL);
978 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
980 return val;
983 /* Like comptypes, but if it returns non-zero because enum and int are
984 compatible, it sets *ENUM_AND_INT_P to true. */
986 static int
987 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
989 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
990 int val;
992 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
993 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
995 return val;
998 /* Like comptypes, but if it returns nonzero for different types, it
999 sets *DIFFERENT_TYPES_P to true. */
1002 comptypes_check_different_types (tree type1, tree type2,
1003 bool *different_types_p)
1005 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1006 int val;
1008 val = comptypes_internal (type1, type2, NULL, different_types_p);
1009 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1011 return val;
1014 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1015 or various other operations. Return 2 if they are compatible
1016 but a warning may be needed if you use them together. If
1017 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1018 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1019 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1020 NULL, and the types are compatible but different enough not to be
1021 permitted in C1X typedef redeclarations, then this sets
1022 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1023 false, but may or may not be set if the types are incompatible.
1024 This differs from comptypes, in that we don't free the seen
1025 types. */
1027 static int
1028 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1029 bool *different_types_p)
1031 const_tree t1 = type1;
1032 const_tree t2 = type2;
1033 int attrval, val;
1035 /* Suppress errors caused by previously reported errors. */
1037 if (t1 == t2 || !t1 || !t2
1038 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1039 return 1;
1041 /* Enumerated types are compatible with integer types, but this is
1042 not transitive: two enumerated types in the same translation unit
1043 are compatible with each other only if they are the same type. */
1045 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1047 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1048 if (TREE_CODE (t2) != VOID_TYPE)
1050 if (enum_and_int_p != NULL)
1051 *enum_and_int_p = true;
1052 if (different_types_p != NULL)
1053 *different_types_p = true;
1056 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1058 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1059 if (TREE_CODE (t1) != VOID_TYPE)
1061 if (enum_and_int_p != NULL)
1062 *enum_and_int_p = true;
1063 if (different_types_p != NULL)
1064 *different_types_p = true;
1068 if (t1 == t2)
1069 return 1;
1071 /* Different classes of types can't be compatible. */
1073 if (TREE_CODE (t1) != TREE_CODE (t2))
1074 return 0;
1076 /* Qualifiers must match. C99 6.7.3p9 */
1078 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1079 return 0;
1081 /* Allow for two different type nodes which have essentially the same
1082 definition. Note that we already checked for equality of the type
1083 qualifiers (just above). */
1085 if (TREE_CODE (t1) != ARRAY_TYPE
1086 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1087 return 1;
1089 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1090 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1091 return 0;
1093 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1094 val = 0;
1096 switch (TREE_CODE (t1))
1098 case POINTER_TYPE:
1099 /* Do not remove mode or aliasing information. */
1100 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1101 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1102 break;
1103 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1104 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1105 enum_and_int_p, different_types_p));
1106 break;
1108 case FUNCTION_TYPE:
1109 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1110 different_types_p);
1111 break;
1113 case ARRAY_TYPE:
1115 tree d1 = TYPE_DOMAIN (t1);
1116 tree d2 = TYPE_DOMAIN (t2);
1117 bool d1_variable, d2_variable;
1118 bool d1_zero, d2_zero;
1119 val = 1;
1121 /* Target types must match incl. qualifiers. */
1122 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1123 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1124 enum_and_int_p,
1125 different_types_p)))
1126 return 0;
1128 if (different_types_p != NULL
1129 && (d1 == 0) != (d2 == 0))
1130 *different_types_p = true;
1131 /* Sizes must match unless one is missing or variable. */
1132 if (d1 == 0 || d2 == 0 || d1 == d2)
1133 break;
1135 d1_zero = !TYPE_MAX_VALUE (d1);
1136 d2_zero = !TYPE_MAX_VALUE (d2);
1138 d1_variable = (!d1_zero
1139 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1140 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1141 d2_variable = (!d2_zero
1142 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1143 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1144 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1145 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1147 if (different_types_p != NULL
1148 && d1_variable != d2_variable)
1149 *different_types_p = true;
1150 if (d1_variable || d2_variable)
1151 break;
1152 if (d1_zero && d2_zero)
1153 break;
1154 if (d1_zero || d2_zero
1155 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1156 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1157 val = 0;
1159 break;
1162 case ENUMERAL_TYPE:
1163 case RECORD_TYPE:
1164 case UNION_TYPE:
1165 if (val != 1 && !same_translation_unit_p (t1, t2))
1167 tree a1 = TYPE_ATTRIBUTES (t1);
1168 tree a2 = TYPE_ATTRIBUTES (t2);
1170 if (! attribute_list_contained (a1, a2)
1171 && ! attribute_list_contained (a2, a1))
1172 break;
1174 if (attrval != 2)
1175 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1176 different_types_p);
1177 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1178 different_types_p);
1180 break;
1182 case VECTOR_TYPE:
1183 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1184 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1185 enum_and_int_p, different_types_p));
1186 break;
1188 default:
1189 break;
1191 return attrval == 2 && val == 1 ? 2 : val;
1194 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1195 their qualifiers, except for named address spaces. If the pointers point to
1196 different named addresses, then we must determine if one address space is a
1197 subset of the other. */
1199 static int
1200 comp_target_types (location_t location, tree ttl, tree ttr)
1202 int val;
1203 tree mvl = TREE_TYPE (ttl);
1204 tree mvr = TREE_TYPE (ttr);
1205 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1206 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1207 addr_space_t as_common;
1208 bool enum_and_int_p;
1210 /* Fail if pointers point to incompatible address spaces. */
1211 if (!addr_space_superset (asl, asr, &as_common))
1212 return 0;
1214 /* Do not lose qualifiers on element types of array types that are
1215 pointer targets by taking their TYPE_MAIN_VARIANT. */
1216 if (TREE_CODE (mvl) != ARRAY_TYPE)
1217 mvl = TYPE_MAIN_VARIANT (mvl);
1218 if (TREE_CODE (mvr) != ARRAY_TYPE)
1219 mvr = TYPE_MAIN_VARIANT (mvr);
1220 enum_and_int_p = false;
1221 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1223 if (val == 2)
1224 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1226 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1227 warning_at (location, OPT_Wc___compat,
1228 "pointer target types incompatible in C++");
1230 return val;
1233 /* Subroutines of `comptypes'. */
1235 /* Determine whether two trees derive from the same translation unit.
1236 If the CONTEXT chain ends in a null, that tree's context is still
1237 being parsed, so if two trees have context chains ending in null,
1238 they're in the same translation unit. */
1240 same_translation_unit_p (const_tree t1, const_tree t2)
1242 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1243 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1245 case tcc_declaration:
1246 t1 = DECL_CONTEXT (t1); break;
1247 case tcc_type:
1248 t1 = TYPE_CONTEXT (t1); break;
1249 case tcc_exceptional:
1250 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1251 default: gcc_unreachable ();
1254 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1255 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1257 case tcc_declaration:
1258 t2 = DECL_CONTEXT (t2); break;
1259 case tcc_type:
1260 t2 = TYPE_CONTEXT (t2); break;
1261 case tcc_exceptional:
1262 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1263 default: gcc_unreachable ();
1266 return t1 == t2;
1269 /* Allocate the seen two types, assuming that they are compatible. */
1271 static struct tagged_tu_seen_cache *
1272 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1274 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1275 tu->next = tagged_tu_seen_base;
1276 tu->t1 = t1;
1277 tu->t2 = t2;
1279 tagged_tu_seen_base = tu;
1281 /* The C standard says that two structures in different translation
1282 units are compatible with each other only if the types of their
1283 fields are compatible (among other things). We assume that they
1284 are compatible until proven otherwise when building the cache.
1285 An example where this can occur is:
1286 struct a
1288 struct a *next;
1290 If we are comparing this against a similar struct in another TU,
1291 and did not assume they were compatible, we end up with an infinite
1292 loop. */
1293 tu->val = 1;
1294 return tu;
1297 /* Free the seen types until we get to TU_TIL. */
1299 static void
1300 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1302 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1303 while (tu != tu_til)
1305 const struct tagged_tu_seen_cache *const tu1
1306 = (const struct tagged_tu_seen_cache *) tu;
1307 tu = tu1->next;
1308 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1310 tagged_tu_seen_base = tu_til;
1313 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1314 compatible. If the two types are not the same (which has been
1315 checked earlier), this can only happen when multiple translation
1316 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1317 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1318 comptypes_internal. */
1320 static int
1321 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1322 bool *enum_and_int_p, bool *different_types_p)
1324 tree s1, s2;
1325 bool needs_warning = false;
1327 /* We have to verify that the tags of the types are the same. This
1328 is harder than it looks because this may be a typedef, so we have
1329 to go look at the original type. It may even be a typedef of a
1330 typedef...
1331 In the case of compiler-created builtin structs the TYPE_DECL
1332 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1333 while (TYPE_NAME (t1)
1334 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1335 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1336 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1338 while (TYPE_NAME (t2)
1339 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1340 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1341 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1343 /* C90 didn't have the requirement that the two tags be the same. */
1344 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1345 return 0;
1347 /* C90 didn't say what happened if one or both of the types were
1348 incomplete; we choose to follow C99 rules here, which is that they
1349 are compatible. */
1350 if (TYPE_SIZE (t1) == NULL
1351 || TYPE_SIZE (t2) == NULL)
1352 return 1;
1355 const struct tagged_tu_seen_cache * tts_i;
1356 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1357 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1358 return tts_i->val;
1361 switch (TREE_CODE (t1))
1363 case ENUMERAL_TYPE:
1365 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1366 /* Speed up the case where the type values are in the same order. */
1367 tree tv1 = TYPE_VALUES (t1);
1368 tree tv2 = TYPE_VALUES (t2);
1370 if (tv1 == tv2)
1372 return 1;
1375 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1377 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1378 break;
1379 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1381 tu->val = 0;
1382 return 0;
1386 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1388 return 1;
1390 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1392 tu->val = 0;
1393 return 0;
1396 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1398 tu->val = 0;
1399 return 0;
1402 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1404 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1405 if (s2 == NULL
1406 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1408 tu->val = 0;
1409 return 0;
1412 return 1;
1415 case UNION_TYPE:
1417 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1418 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1420 tu->val = 0;
1421 return 0;
1424 /* Speed up the common case where the fields are in the same order. */
1425 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1426 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1428 int result;
1430 if (DECL_NAME (s1) != DECL_NAME (s2))
1431 break;
1432 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1433 enum_and_int_p, different_types_p);
1435 if (result != 1 && !DECL_NAME (s1))
1436 break;
1437 if (result == 0)
1439 tu->val = 0;
1440 return 0;
1442 if (result == 2)
1443 needs_warning = true;
1445 if (TREE_CODE (s1) == FIELD_DECL
1446 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1447 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1449 tu->val = 0;
1450 return 0;
1453 if (!s1 && !s2)
1455 tu->val = needs_warning ? 2 : 1;
1456 return tu->val;
1459 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1461 bool ok = false;
1463 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1464 if (DECL_NAME (s1) == DECL_NAME (s2))
1466 int result;
1468 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1469 enum_and_int_p,
1470 different_types_p);
1472 if (result != 1 && !DECL_NAME (s1))
1473 continue;
1474 if (result == 0)
1476 tu->val = 0;
1477 return 0;
1479 if (result == 2)
1480 needs_warning = true;
1482 if (TREE_CODE (s1) == FIELD_DECL
1483 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1484 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1485 break;
1487 ok = true;
1488 break;
1490 if (!ok)
1492 tu->val = 0;
1493 return 0;
1496 tu->val = needs_warning ? 2 : 10;
1497 return tu->val;
1500 case RECORD_TYPE:
1502 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1504 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1505 s1 && s2;
1506 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1508 int result;
1509 if (TREE_CODE (s1) != TREE_CODE (s2)
1510 || DECL_NAME (s1) != DECL_NAME (s2))
1511 break;
1512 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1513 enum_and_int_p, different_types_p);
1514 if (result == 0)
1515 break;
1516 if (result == 2)
1517 needs_warning = true;
1519 if (TREE_CODE (s1) == FIELD_DECL
1520 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1521 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1522 break;
1524 if (s1 && s2)
1525 tu->val = 0;
1526 else
1527 tu->val = needs_warning ? 2 : 1;
1528 return tu->val;
1531 default:
1532 gcc_unreachable ();
1536 /* Return 1 if two function types F1 and F2 are compatible.
1537 If either type specifies no argument types,
1538 the other must specify a fixed number of self-promoting arg types.
1539 Otherwise, if one type specifies only the number of arguments,
1540 the other must specify that number of self-promoting arg types.
1541 Otherwise, the argument types must match.
1542 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1544 static int
1545 function_types_compatible_p (const_tree f1, const_tree f2,
1546 bool *enum_and_int_p, bool *different_types_p)
1548 tree args1, args2;
1549 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1550 int val = 1;
1551 int val1;
1552 tree ret1, ret2;
1554 ret1 = TREE_TYPE (f1);
1555 ret2 = TREE_TYPE (f2);
1557 /* 'volatile' qualifiers on a function's return type used to mean
1558 the function is noreturn. */
1559 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1560 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1561 if (TYPE_VOLATILE (ret1))
1562 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1563 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1564 if (TYPE_VOLATILE (ret2))
1565 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1566 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1567 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1568 if (val == 0)
1569 return 0;
1571 args1 = TYPE_ARG_TYPES (f1);
1572 args2 = TYPE_ARG_TYPES (f2);
1574 if (different_types_p != NULL
1575 && (args1 == 0) != (args2 == 0))
1576 *different_types_p = true;
1578 /* An unspecified parmlist matches any specified parmlist
1579 whose argument types don't need default promotions. */
1581 if (args1 == 0)
1583 if (!self_promoting_args_p (args2))
1584 return 0;
1585 /* If one of these types comes from a non-prototype fn definition,
1586 compare that with the other type's arglist.
1587 If they don't match, ask for a warning (but no error). */
1588 if (TYPE_ACTUAL_ARG_TYPES (f1)
1589 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1590 enum_and_int_p, different_types_p))
1591 val = 2;
1592 return val;
1594 if (args2 == 0)
1596 if (!self_promoting_args_p (args1))
1597 return 0;
1598 if (TYPE_ACTUAL_ARG_TYPES (f2)
1599 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1600 enum_and_int_p, different_types_p))
1601 val = 2;
1602 return val;
1605 /* Both types have argument lists: compare them and propagate results. */
1606 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1607 different_types_p);
1608 return val1 != 1 ? val1 : val;
1611 /* Check two lists of types for compatibility, returning 0 for
1612 incompatible, 1 for compatible, or 2 for compatible with
1613 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1614 comptypes_internal. */
1616 static int
1617 type_lists_compatible_p (const_tree args1, const_tree args2,
1618 bool *enum_and_int_p, bool *different_types_p)
1620 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1621 int val = 1;
1622 int newval = 0;
1624 while (1)
1626 tree a1, mv1, a2, mv2;
1627 if (args1 == 0 && args2 == 0)
1628 return val;
1629 /* If one list is shorter than the other,
1630 they fail to match. */
1631 if (args1 == 0 || args2 == 0)
1632 return 0;
1633 mv1 = a1 = TREE_VALUE (args1);
1634 mv2 = a2 = TREE_VALUE (args2);
1635 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1636 mv1 = TYPE_MAIN_VARIANT (mv1);
1637 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1638 mv2 = TYPE_MAIN_VARIANT (mv2);
1639 /* A null pointer instead of a type
1640 means there is supposed to be an argument
1641 but nothing is specified about what type it has.
1642 So match anything that self-promotes. */
1643 if (different_types_p != NULL
1644 && (a1 == 0) != (a2 == 0))
1645 *different_types_p = true;
1646 if (a1 == 0)
1648 if (c_type_promotes_to (a2) != a2)
1649 return 0;
1651 else if (a2 == 0)
1653 if (c_type_promotes_to (a1) != a1)
1654 return 0;
1656 /* If one of the lists has an error marker, ignore this arg. */
1657 else if (TREE_CODE (a1) == ERROR_MARK
1658 || TREE_CODE (a2) == ERROR_MARK)
1660 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1661 different_types_p)))
1663 if (different_types_p != NULL)
1664 *different_types_p = true;
1665 /* Allow wait (union {union wait *u; int *i} *)
1666 and wait (union wait *) to be compatible. */
1667 if (TREE_CODE (a1) == UNION_TYPE
1668 && (TYPE_NAME (a1) == 0
1669 || TYPE_TRANSPARENT_AGGR (a1))
1670 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1671 && tree_int_cst_equal (TYPE_SIZE (a1),
1672 TYPE_SIZE (a2)))
1674 tree memb;
1675 for (memb = TYPE_FIELDS (a1);
1676 memb; memb = DECL_CHAIN (memb))
1678 tree mv3 = TREE_TYPE (memb);
1679 if (mv3 && mv3 != error_mark_node
1680 && TREE_CODE (mv3) != ARRAY_TYPE)
1681 mv3 = TYPE_MAIN_VARIANT (mv3);
1682 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1683 different_types_p))
1684 break;
1686 if (memb == 0)
1687 return 0;
1689 else if (TREE_CODE (a2) == UNION_TYPE
1690 && (TYPE_NAME (a2) == 0
1691 || TYPE_TRANSPARENT_AGGR (a2))
1692 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1693 && tree_int_cst_equal (TYPE_SIZE (a2),
1694 TYPE_SIZE (a1)))
1696 tree memb;
1697 for (memb = TYPE_FIELDS (a2);
1698 memb; memb = DECL_CHAIN (memb))
1700 tree mv3 = TREE_TYPE (memb);
1701 if (mv3 && mv3 != error_mark_node
1702 && TREE_CODE (mv3) != ARRAY_TYPE)
1703 mv3 = TYPE_MAIN_VARIANT (mv3);
1704 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1705 different_types_p))
1706 break;
1708 if (memb == 0)
1709 return 0;
1711 else
1712 return 0;
1715 /* comptypes said ok, but record if it said to warn. */
1716 if (newval > val)
1717 val = newval;
1719 args1 = TREE_CHAIN (args1);
1720 args2 = TREE_CHAIN (args2);
1724 /* Compute the size to increment a pointer by. */
1726 static tree
1727 c_size_in_bytes (const_tree type)
1729 enum tree_code code = TREE_CODE (type);
1731 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1732 return size_one_node;
1734 if (!COMPLETE_OR_VOID_TYPE_P (type))
1736 error ("arithmetic on pointer to an incomplete type");
1737 return size_one_node;
1740 /* Convert in case a char is more than one unit. */
1741 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1742 size_int (TYPE_PRECISION (char_type_node)
1743 / BITS_PER_UNIT));
1746 /* Return either DECL or its known constant value (if it has one). */
1748 tree
1749 decl_constant_value (tree decl)
1751 if (/* Don't change a variable array bound or initial value to a constant
1752 in a place where a variable is invalid. Note that DECL_INITIAL
1753 isn't valid for a PARM_DECL. */
1754 current_function_decl != 0
1755 && TREE_CODE (decl) != PARM_DECL
1756 && !TREE_THIS_VOLATILE (decl)
1757 && TREE_READONLY (decl)
1758 && DECL_INITIAL (decl) != 0
1759 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1760 /* This is invalid if initial value is not constant.
1761 If it has either a function call, a memory reference,
1762 or a variable, then re-evaluating it could give different results. */
1763 && TREE_CONSTANT (DECL_INITIAL (decl))
1764 /* Check for cases where this is sub-optimal, even though valid. */
1765 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1766 return DECL_INITIAL (decl);
1767 return decl;
1770 /* Convert the array expression EXP to a pointer. */
1771 static tree
1772 array_to_pointer_conversion (location_t loc, tree exp)
1774 tree orig_exp = exp;
1775 tree type = TREE_TYPE (exp);
1776 tree adr;
1777 tree restype = TREE_TYPE (type);
1778 tree ptrtype;
1780 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1782 STRIP_TYPE_NOPS (exp);
1784 if (TREE_NO_WARNING (orig_exp))
1785 TREE_NO_WARNING (exp) = 1;
1787 ptrtype = build_pointer_type (restype);
1789 if (TREE_CODE (exp) == INDIRECT_REF)
1790 return convert (ptrtype, TREE_OPERAND (exp, 0));
1792 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1793 return convert (ptrtype, adr);
1796 /* Convert the function expression EXP to a pointer. */
1797 static tree
1798 function_to_pointer_conversion (location_t loc, tree exp)
1800 tree orig_exp = exp;
1802 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1804 STRIP_TYPE_NOPS (exp);
1806 if (TREE_NO_WARNING (orig_exp))
1807 TREE_NO_WARNING (exp) = 1;
1809 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1812 /* Mark EXP as read, not just set, for set but not used -Wunused
1813 warning purposes. */
1815 void
1816 mark_exp_read (tree exp)
1818 switch (TREE_CODE (exp))
1820 case VAR_DECL:
1821 case PARM_DECL:
1822 DECL_READ_P (exp) = 1;
1823 break;
1824 case ARRAY_REF:
1825 case COMPONENT_REF:
1826 case MODIFY_EXPR:
1827 case REALPART_EXPR:
1828 case IMAGPART_EXPR:
1829 CASE_CONVERT:
1830 case ADDR_EXPR:
1831 mark_exp_read (TREE_OPERAND (exp, 0));
1832 break;
1833 case COMPOUND_EXPR:
1834 case C_MAYBE_CONST_EXPR:
1835 mark_exp_read (TREE_OPERAND (exp, 1));
1836 break;
1837 default:
1838 break;
1842 /* Perform the default conversion of arrays and functions to pointers.
1843 Return the result of converting EXP. For any other expression, just
1844 return EXP.
1846 LOC is the location of the expression. */
1848 struct c_expr
1849 default_function_array_conversion (location_t loc, struct c_expr exp)
1851 tree orig_exp = exp.value;
1852 tree type = TREE_TYPE (exp.value);
1853 enum tree_code code = TREE_CODE (type);
1855 switch (code)
1857 case ARRAY_TYPE:
1859 bool not_lvalue = false;
1860 bool lvalue_array_p;
1862 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1863 || CONVERT_EXPR_P (exp.value))
1864 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1866 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1867 not_lvalue = true;
1868 exp.value = TREE_OPERAND (exp.value, 0);
1871 if (TREE_NO_WARNING (orig_exp))
1872 TREE_NO_WARNING (exp.value) = 1;
1874 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1875 if (!flag_isoc99 && !lvalue_array_p)
1877 /* Before C99, non-lvalue arrays do not decay to pointers.
1878 Normally, using such an array would be invalid; but it can
1879 be used correctly inside sizeof or as a statement expression.
1880 Thus, do not give an error here; an error will result later. */
1881 return exp;
1884 exp.value = array_to_pointer_conversion (loc, exp.value);
1886 break;
1887 case FUNCTION_TYPE:
1888 exp.value = function_to_pointer_conversion (loc, exp.value);
1889 break;
1890 default:
1891 break;
1894 return exp;
1897 struct c_expr
1898 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1900 mark_exp_read (exp.value);
1901 return default_function_array_conversion (loc, exp);
1904 /* EXP is an expression of integer type. Apply the integer promotions
1905 to it and return the promoted value. */
1907 tree
1908 perform_integral_promotions (tree exp)
1910 tree type = TREE_TYPE (exp);
1911 enum tree_code code = TREE_CODE (type);
1913 gcc_assert (INTEGRAL_TYPE_P (type));
1915 /* Normally convert enums to int,
1916 but convert wide enums to something wider. */
1917 if (code == ENUMERAL_TYPE)
1919 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1920 TYPE_PRECISION (integer_type_node)),
1921 ((TYPE_PRECISION (type)
1922 >= TYPE_PRECISION (integer_type_node))
1923 && TYPE_UNSIGNED (type)));
1925 return convert (type, exp);
1928 /* ??? This should no longer be needed now bit-fields have their
1929 proper types. */
1930 if (TREE_CODE (exp) == COMPONENT_REF
1931 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1932 /* If it's thinner than an int, promote it like a
1933 c_promoting_integer_type_p, otherwise leave it alone. */
1934 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1935 TYPE_PRECISION (integer_type_node)))
1936 return convert (integer_type_node, exp);
1938 if (c_promoting_integer_type_p (type))
1940 /* Preserve unsignedness if not really getting any wider. */
1941 if (TYPE_UNSIGNED (type)
1942 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1943 return convert (unsigned_type_node, exp);
1945 return convert (integer_type_node, exp);
1948 return exp;
1952 /* Perform default promotions for C data used in expressions.
1953 Enumeral types or short or char are converted to int.
1954 In addition, manifest constants symbols are replaced by their values. */
1956 tree
1957 default_conversion (tree exp)
1959 tree orig_exp;
1960 tree type = TREE_TYPE (exp);
1961 enum tree_code code = TREE_CODE (type);
1962 tree promoted_type;
1964 mark_exp_read (exp);
1966 /* Functions and arrays have been converted during parsing. */
1967 gcc_assert (code != FUNCTION_TYPE);
1968 if (code == ARRAY_TYPE)
1969 return exp;
1971 /* Constants can be used directly unless they're not loadable. */
1972 if (TREE_CODE (exp) == CONST_DECL)
1973 exp = DECL_INITIAL (exp);
1975 /* Strip no-op conversions. */
1976 orig_exp = exp;
1977 STRIP_TYPE_NOPS (exp);
1979 if (TREE_NO_WARNING (orig_exp))
1980 TREE_NO_WARNING (exp) = 1;
1982 if (code == VOID_TYPE)
1984 error ("void value not ignored as it ought to be");
1985 return error_mark_node;
1988 exp = require_complete_type (exp);
1989 if (exp == error_mark_node)
1990 return error_mark_node;
1992 promoted_type = targetm.promoted_type (type);
1993 if (promoted_type)
1994 return convert (promoted_type, exp);
1996 if (INTEGRAL_TYPE_P (type))
1997 return perform_integral_promotions (exp);
1999 return exp;
2002 /* Look up COMPONENT in a structure or union TYPE.
2004 If the component name is not found, returns NULL_TREE. Otherwise,
2005 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2006 stepping down the chain to the component, which is in the last
2007 TREE_VALUE of the list. Normally the list is of length one, but if
2008 the component is embedded within (nested) anonymous structures or
2009 unions, the list steps down the chain to the component. */
2011 static tree
2012 lookup_field (tree type, tree component)
2014 tree field;
2016 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2017 to the field elements. Use a binary search on this array to quickly
2018 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2019 will always be set for structures which have many elements. */
2021 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2023 int bot, top, half;
2024 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2026 field = TYPE_FIELDS (type);
2027 bot = 0;
2028 top = TYPE_LANG_SPECIFIC (type)->s->len;
2029 while (top - bot > 1)
2031 half = (top - bot + 1) >> 1;
2032 field = field_array[bot+half];
2034 if (DECL_NAME (field) == NULL_TREE)
2036 /* Step through all anon unions in linear fashion. */
2037 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2039 field = field_array[bot++];
2040 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2041 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2043 tree anon = lookup_field (TREE_TYPE (field), component);
2045 if (anon)
2046 return tree_cons (NULL_TREE, field, anon);
2048 /* The Plan 9 compiler permits referring
2049 directly to an anonymous struct/union field
2050 using a typedef name. */
2051 if (flag_plan9_extensions
2052 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2053 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2054 == TYPE_DECL)
2055 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2056 == component))
2057 break;
2061 /* Entire record is only anon unions. */
2062 if (bot > top)
2063 return NULL_TREE;
2065 /* Restart the binary search, with new lower bound. */
2066 continue;
2069 if (DECL_NAME (field) == component)
2070 break;
2071 if (DECL_NAME (field) < component)
2072 bot += half;
2073 else
2074 top = bot + half;
2077 if (DECL_NAME (field_array[bot]) == component)
2078 field = field_array[bot];
2079 else if (DECL_NAME (field) != component)
2080 return NULL_TREE;
2082 else
2084 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2086 if (DECL_NAME (field) == NULL_TREE
2087 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2088 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2090 tree anon = lookup_field (TREE_TYPE (field), component);
2092 if (anon)
2093 return tree_cons (NULL_TREE, field, anon);
2095 /* The Plan 9 compiler permits referring directly to an
2096 anonymous struct/union field using a typedef
2097 name. */
2098 if (flag_plan9_extensions
2099 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2100 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2101 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2102 == component))
2103 break;
2106 if (DECL_NAME (field) == component)
2107 break;
2110 if (field == NULL_TREE)
2111 return NULL_TREE;
2114 return tree_cons (NULL_TREE, field, NULL_TREE);
2117 /* Make an expression to refer to the COMPONENT field of structure or
2118 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2119 location of the COMPONENT_REF. */
2121 tree
2122 build_component_ref (location_t loc, tree datum, tree component)
2124 tree type = TREE_TYPE (datum);
2125 enum tree_code code = TREE_CODE (type);
2126 tree field = NULL;
2127 tree ref;
2128 bool datum_lvalue = lvalue_p (datum);
2130 if (!objc_is_public (datum, component))
2131 return error_mark_node;
2133 /* Detect Objective-C property syntax object.property. */
2134 if (c_dialect_objc ()
2135 && (ref = objc_maybe_build_component_ref (datum, component)))
2136 return ref;
2138 /* See if there is a field or component with name COMPONENT. */
2140 if (code == RECORD_TYPE || code == UNION_TYPE)
2142 if (!COMPLETE_TYPE_P (type))
2144 c_incomplete_type_error (NULL_TREE, type);
2145 return error_mark_node;
2148 field = lookup_field (type, component);
2150 if (!field)
2152 error_at (loc, "%qT has no member named %qE", type, component);
2153 return error_mark_node;
2156 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2157 This might be better solved in future the way the C++ front
2158 end does it - by giving the anonymous entities each a
2159 separate name and type, and then have build_component_ref
2160 recursively call itself. We can't do that here. */
2163 tree subdatum = TREE_VALUE (field);
2164 int quals;
2165 tree subtype;
2166 bool use_datum_quals;
2168 if (TREE_TYPE (subdatum) == error_mark_node)
2169 return error_mark_node;
2171 /* If this is an rvalue, it does not have qualifiers in C
2172 standard terms and we must avoid propagating such
2173 qualifiers down to a non-lvalue array that is then
2174 converted to a pointer. */
2175 use_datum_quals = (datum_lvalue
2176 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2178 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2179 if (use_datum_quals)
2180 quals |= TYPE_QUALS (TREE_TYPE (datum));
2181 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2183 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2184 NULL_TREE);
2185 SET_EXPR_LOCATION (ref, loc);
2186 if (TREE_READONLY (subdatum)
2187 || (use_datum_quals && TREE_READONLY (datum)))
2188 TREE_READONLY (ref) = 1;
2189 if (TREE_THIS_VOLATILE (subdatum)
2190 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2191 TREE_THIS_VOLATILE (ref) = 1;
2193 if (TREE_DEPRECATED (subdatum))
2194 warn_deprecated_use (subdatum, NULL_TREE);
2196 datum = ref;
2198 field = TREE_CHAIN (field);
2200 while (field);
2202 return ref;
2204 else if (code != ERROR_MARK)
2205 error_at (loc,
2206 "request for member %qE in something not a structure or union",
2207 component);
2209 return error_mark_node;
2212 /* Given an expression PTR for a pointer, return an expression
2213 for the value pointed to.
2214 ERRORSTRING is the name of the operator to appear in error messages.
2216 LOC is the location to use for the generated tree. */
2218 tree
2219 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2221 tree pointer = default_conversion (ptr);
2222 tree type = TREE_TYPE (pointer);
2223 tree ref;
2225 if (TREE_CODE (type) == POINTER_TYPE)
2227 if (CONVERT_EXPR_P (pointer)
2228 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2230 /* If a warning is issued, mark it to avoid duplicates from
2231 the backend. This only needs to be done at
2232 warn_strict_aliasing > 2. */
2233 if (warn_strict_aliasing > 2)
2234 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2235 type, TREE_OPERAND (pointer, 0)))
2236 TREE_NO_WARNING (pointer) = 1;
2239 if (TREE_CODE (pointer) == ADDR_EXPR
2240 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2241 == TREE_TYPE (type)))
2243 ref = TREE_OPERAND (pointer, 0);
2244 protected_set_expr_location (ref, loc);
2245 return ref;
2247 else
2249 tree t = TREE_TYPE (type);
2251 ref = build1 (INDIRECT_REF, t, pointer);
2253 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2255 error_at (loc, "dereferencing pointer to incomplete type");
2256 return error_mark_node;
2258 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2259 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2261 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2262 so that we get the proper error message if the result is used
2263 to assign to. Also, &* is supposed to be a no-op.
2264 And ANSI C seems to specify that the type of the result
2265 should be the const type. */
2266 /* A de-reference of a pointer to const is not a const. It is valid
2267 to change it via some other pointer. */
2268 TREE_READONLY (ref) = TYPE_READONLY (t);
2269 TREE_SIDE_EFFECTS (ref)
2270 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2271 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2272 protected_set_expr_location (ref, loc);
2273 return ref;
2276 else if (TREE_CODE (pointer) != ERROR_MARK)
2277 switch (errstring)
2279 case RO_ARRAY_INDEXING:
2280 error_at (loc,
2281 "invalid type argument of array indexing (have %qT)",
2282 type);
2283 break;
2284 case RO_UNARY_STAR:
2285 error_at (loc,
2286 "invalid type argument of unary %<*%> (have %qT)",
2287 type);
2288 break;
2289 case RO_ARROW:
2290 error_at (loc,
2291 "invalid type argument of %<->%> (have %qT)",
2292 type);
2293 break;
2294 default:
2295 gcc_unreachable ();
2297 return error_mark_node;
2300 /* This handles expressions of the form "a[i]", which denotes
2301 an array reference.
2303 This is logically equivalent in C to *(a+i), but we may do it differently.
2304 If A is a variable or a member, we generate a primitive ARRAY_REF.
2305 This avoids forcing the array out of registers, and can work on
2306 arrays that are not lvalues (for example, members of structures returned
2307 by functions).
2309 For vector types, allow vector[i] but not i[vector], and create
2310 *(((type*)&vectortype) + i) for the expression.
2312 LOC is the location to use for the returned expression. */
2314 tree
2315 build_array_ref (location_t loc, tree array, tree index)
2317 tree ret;
2318 bool swapped = false;
2319 if (TREE_TYPE (array) == error_mark_node
2320 || TREE_TYPE (index) == error_mark_node)
2321 return error_mark_node;
2323 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2324 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2325 /* Allow vector[index] but not index[vector]. */
2326 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2328 tree temp;
2329 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2330 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2332 error_at (loc,
2333 "subscripted value is neither array nor pointer nor vector");
2335 return error_mark_node;
2337 temp = array;
2338 array = index;
2339 index = temp;
2340 swapped = true;
2343 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2345 error_at (loc, "array subscript is not an integer");
2346 return error_mark_node;
2349 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2351 error_at (loc, "subscripted value is pointer to function");
2352 return error_mark_node;
2355 /* ??? Existing practice has been to warn only when the char
2356 index is syntactically the index, not for char[array]. */
2357 if (!swapped)
2358 warn_array_subscript_with_type_char (index);
2360 /* Apply default promotions *after* noticing character types. */
2361 index = default_conversion (index);
2363 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2365 /* For vector[index], convert the vector to a
2366 pointer of the underlying type. */
2367 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2369 tree type = TREE_TYPE (array);
2370 tree type1;
2372 if (TREE_CODE (index) == INTEGER_CST)
2373 if (!host_integerp (index, 1)
2374 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2375 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2376 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2378 c_common_mark_addressable_vec (array);
2379 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2380 type = build_pointer_type (type);
2381 type1 = build_pointer_type (TREE_TYPE (array));
2382 array = build1 (ADDR_EXPR, type1, array);
2383 array = convert (type, array);
2386 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2388 tree rval, type;
2390 /* An array that is indexed by a non-constant
2391 cannot be stored in a register; we must be able to do
2392 address arithmetic on its address.
2393 Likewise an array of elements of variable size. */
2394 if (TREE_CODE (index) != INTEGER_CST
2395 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2396 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2398 if (!c_mark_addressable (array))
2399 return error_mark_node;
2401 /* An array that is indexed by a constant value which is not within
2402 the array bounds cannot be stored in a register either; because we
2403 would get a crash in store_bit_field/extract_bit_field when trying
2404 to access a non-existent part of the register. */
2405 if (TREE_CODE (index) == INTEGER_CST
2406 && TYPE_DOMAIN (TREE_TYPE (array))
2407 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2409 if (!c_mark_addressable (array))
2410 return error_mark_node;
2413 if (pedantic)
2415 tree foo = array;
2416 while (TREE_CODE (foo) == COMPONENT_REF)
2417 foo = TREE_OPERAND (foo, 0);
2418 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2419 pedwarn (loc, OPT_pedantic,
2420 "ISO C forbids subscripting %<register%> array");
2421 else if (!flag_isoc99 && !lvalue_p (foo))
2422 pedwarn (loc, OPT_pedantic,
2423 "ISO C90 forbids subscripting non-lvalue array");
2426 type = TREE_TYPE (TREE_TYPE (array));
2427 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2428 /* Array ref is const/volatile if the array elements are
2429 or if the array is. */
2430 TREE_READONLY (rval)
2431 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2432 | TREE_READONLY (array));
2433 TREE_SIDE_EFFECTS (rval)
2434 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2435 | TREE_SIDE_EFFECTS (array));
2436 TREE_THIS_VOLATILE (rval)
2437 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2438 /* This was added by rms on 16 Nov 91.
2439 It fixes vol struct foo *a; a->elts[1]
2440 in an inline function.
2441 Hope it doesn't break something else. */
2442 | TREE_THIS_VOLATILE (array));
2443 ret = require_complete_type (rval);
2444 protected_set_expr_location (ret, loc);
2445 return ret;
2447 else
2449 tree ar = default_conversion (array);
2451 if (ar == error_mark_node)
2452 return ar;
2454 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2455 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2457 return build_indirect_ref
2458 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2459 RO_ARRAY_INDEXING);
2463 /* Build an external reference to identifier ID. FUN indicates
2464 whether this will be used for a function call. LOC is the source
2465 location of the identifier. This sets *TYPE to the type of the
2466 identifier, which is not the same as the type of the returned value
2467 for CONST_DECLs defined as enum constants. If the type of the
2468 identifier is not available, *TYPE is set to NULL. */
2469 tree
2470 build_external_ref (location_t loc, tree id, int fun, tree *type)
2472 tree ref;
2473 tree decl = lookup_name (id);
2475 /* In Objective-C, an instance variable (ivar) may be preferred to
2476 whatever lookup_name() found. */
2477 decl = objc_lookup_ivar (decl, id);
2479 *type = NULL;
2480 if (decl && decl != error_mark_node)
2482 ref = decl;
2483 *type = TREE_TYPE (ref);
2485 else if (fun)
2486 /* Implicit function declaration. */
2487 ref = implicitly_declare (loc, id);
2488 else if (decl == error_mark_node)
2489 /* Don't complain about something that's already been
2490 complained about. */
2491 return error_mark_node;
2492 else
2494 undeclared_variable (loc, id);
2495 return error_mark_node;
2498 if (TREE_TYPE (ref) == error_mark_node)
2499 return error_mark_node;
2501 if (TREE_DEPRECATED (ref))
2502 warn_deprecated_use (ref, NULL_TREE);
2504 /* Recursive call does not count as usage. */
2505 if (ref != current_function_decl)
2507 TREE_USED (ref) = 1;
2510 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2512 if (!in_sizeof && !in_typeof)
2513 C_DECL_USED (ref) = 1;
2514 else if (DECL_INITIAL (ref) == 0
2515 && DECL_EXTERNAL (ref)
2516 && !TREE_PUBLIC (ref))
2517 record_maybe_used_decl (ref);
2520 if (TREE_CODE (ref) == CONST_DECL)
2522 used_types_insert (TREE_TYPE (ref));
2524 if (warn_cxx_compat
2525 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2526 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2528 warning_at (loc, OPT_Wc___compat,
2529 ("enum constant defined in struct or union "
2530 "is not visible in C++"));
2531 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2534 ref = DECL_INITIAL (ref);
2535 TREE_CONSTANT (ref) = 1;
2537 else if (current_function_decl != 0
2538 && !DECL_FILE_SCOPE_P (current_function_decl)
2539 && (TREE_CODE (ref) == VAR_DECL
2540 || TREE_CODE (ref) == PARM_DECL
2541 || TREE_CODE (ref) == FUNCTION_DECL))
2543 tree context = decl_function_context (ref);
2545 if (context != 0 && context != current_function_decl)
2546 DECL_NONLOCAL (ref) = 1;
2548 /* C99 6.7.4p3: An inline definition of a function with external
2549 linkage ... shall not contain a reference to an identifier with
2550 internal linkage. */
2551 else if (current_function_decl != 0
2552 && DECL_DECLARED_INLINE_P (current_function_decl)
2553 && DECL_EXTERNAL (current_function_decl)
2554 && VAR_OR_FUNCTION_DECL_P (ref)
2555 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2556 && ! TREE_PUBLIC (ref)
2557 && DECL_CONTEXT (ref) != current_function_decl)
2558 record_inline_static (loc, current_function_decl, ref,
2559 csi_internal);
2561 return ref;
2564 /* Record details of decls possibly used inside sizeof or typeof. */
2565 struct maybe_used_decl
2567 /* The decl. */
2568 tree decl;
2569 /* The level seen at (in_sizeof + in_typeof). */
2570 int level;
2571 /* The next one at this level or above, or NULL. */
2572 struct maybe_used_decl *next;
2575 static struct maybe_used_decl *maybe_used_decls;
2577 /* Record that DECL, an undefined static function reference seen
2578 inside sizeof or typeof, might be used if the operand of sizeof is
2579 a VLA type or the operand of typeof is a variably modified
2580 type. */
2582 static void
2583 record_maybe_used_decl (tree decl)
2585 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2586 t->decl = decl;
2587 t->level = in_sizeof + in_typeof;
2588 t->next = maybe_used_decls;
2589 maybe_used_decls = t;
2592 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2593 USED is false, just discard them. If it is true, mark them used
2594 (if no longer inside sizeof or typeof) or move them to the next
2595 level up (if still inside sizeof or typeof). */
2597 void
2598 pop_maybe_used (bool used)
2600 struct maybe_used_decl *p = maybe_used_decls;
2601 int cur_level = in_sizeof + in_typeof;
2602 while (p && p->level > cur_level)
2604 if (used)
2606 if (cur_level == 0)
2607 C_DECL_USED (p->decl) = 1;
2608 else
2609 p->level = cur_level;
2611 p = p->next;
2613 if (!used || cur_level == 0)
2614 maybe_used_decls = p;
2617 /* Return the result of sizeof applied to EXPR. */
2619 struct c_expr
2620 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2622 struct c_expr ret;
2623 if (expr.value == error_mark_node)
2625 ret.value = error_mark_node;
2626 ret.original_code = ERROR_MARK;
2627 ret.original_type = NULL;
2628 pop_maybe_used (false);
2630 else
2632 bool expr_const_operands = true;
2633 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2634 &expr_const_operands);
2635 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2636 ret.original_code = ERROR_MARK;
2637 ret.original_type = NULL;
2638 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2640 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2641 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2642 folded_expr, ret.value);
2643 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2644 SET_EXPR_LOCATION (ret.value, loc);
2646 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2648 return ret;
2651 /* Return the result of sizeof applied to T, a structure for the type
2652 name passed to sizeof (rather than the type itself). LOC is the
2653 location of the original expression. */
2655 struct c_expr
2656 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2658 tree type;
2659 struct c_expr ret;
2660 tree type_expr = NULL_TREE;
2661 bool type_expr_const = true;
2662 type = groktypename (t, &type_expr, &type_expr_const);
2663 ret.value = c_sizeof (loc, type);
2664 ret.original_code = ERROR_MARK;
2665 ret.original_type = NULL;
2666 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2667 && c_vla_type_p (type))
2669 /* If the type is a [*] array, it is a VLA but is represented as
2670 having a size of zero. In such a case we must ensure that
2671 the result of sizeof does not get folded to a constant by
2672 c_fully_fold, because if the size is evaluated the result is
2673 not constant and so constraints on zero or negative size
2674 arrays must not be applied when this sizeof call is inside
2675 another array declarator. */
2676 if (!type_expr)
2677 type_expr = integer_zero_node;
2678 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2679 type_expr, ret.value);
2680 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2682 pop_maybe_used (type != error_mark_node
2683 ? C_TYPE_VARIABLE_SIZE (type) : false);
2684 return ret;
2687 /* Build a function call to function FUNCTION with parameters PARAMS.
2688 The function call is at LOC.
2689 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2690 TREE_VALUE of each node is a parameter-expression.
2691 FUNCTION's data type may be a function type or a pointer-to-function. */
2693 tree
2694 build_function_call (location_t loc, tree function, tree params)
2696 VEC(tree,gc) *vec;
2697 tree ret;
2699 vec = VEC_alloc (tree, gc, list_length (params));
2700 for (; params; params = TREE_CHAIN (params))
2701 VEC_quick_push (tree, vec, TREE_VALUE (params));
2702 ret = build_function_call_vec (loc, function, vec, NULL);
2703 VEC_free (tree, gc, vec);
2704 return ret;
2707 /* Build a function call to function FUNCTION with parameters PARAMS.
2708 ORIGTYPES, if not NULL, is a vector of types; each element is
2709 either NULL or the original type of the corresponding element in
2710 PARAMS. The original type may differ from TREE_TYPE of the
2711 parameter for enums. FUNCTION's data type may be a function type
2712 or pointer-to-function. This function changes the elements of
2713 PARAMS. */
2715 tree
2716 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2717 VEC(tree,gc) *origtypes)
2719 tree fntype, fundecl = 0;
2720 tree name = NULL_TREE, result;
2721 tree tem;
2722 int nargs;
2723 tree *argarray;
2726 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2727 STRIP_TYPE_NOPS (function);
2729 /* Convert anything with function type to a pointer-to-function. */
2730 if (TREE_CODE (function) == FUNCTION_DECL)
2732 /* Implement type-directed function overloading for builtins.
2733 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2734 handle all the type checking. The result is a complete expression
2735 that implements this function call. */
2736 tem = resolve_overloaded_builtin (loc, function, params);
2737 if (tem)
2738 return tem;
2740 name = DECL_NAME (function);
2741 fundecl = function;
2743 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2744 function = function_to_pointer_conversion (loc, function);
2746 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2747 expressions, like those used for ObjC messenger dispatches. */
2748 if (!VEC_empty (tree, params))
2749 function = objc_rewrite_function_call (function,
2750 VEC_index (tree, params, 0));
2752 function = c_fully_fold (function, false, NULL);
2754 fntype = TREE_TYPE (function);
2756 if (TREE_CODE (fntype) == ERROR_MARK)
2757 return error_mark_node;
2759 if (!(TREE_CODE (fntype) == POINTER_TYPE
2760 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2762 error_at (loc, "called object %qE is not a function", function);
2763 return error_mark_node;
2766 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2767 current_function_returns_abnormally = 1;
2769 /* fntype now gets the type of function pointed to. */
2770 fntype = TREE_TYPE (fntype);
2772 /* Convert the parameters to the types declared in the
2773 function prototype, or apply default promotions. */
2775 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2776 function, fundecl);
2777 if (nargs < 0)
2778 return error_mark_node;
2780 /* Check that the function is called through a compatible prototype.
2781 If it is not, replace the call by a trap, wrapped up in a compound
2782 expression if necessary. This has the nice side-effect to prevent
2783 the tree-inliner from generating invalid assignment trees which may
2784 blow up in the RTL expander later. */
2785 if (CONVERT_EXPR_P (function)
2786 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2787 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2788 && !comptypes (fntype, TREE_TYPE (tem)))
2790 tree return_type = TREE_TYPE (fntype);
2791 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2792 NULL_TREE);
2793 int i;
2795 /* This situation leads to run-time undefined behavior. We can't,
2796 therefore, simply error unless we can prove that all possible
2797 executions of the program must execute the code. */
2798 if (warning_at (loc, 0, "function called through a non-compatible type"))
2799 /* We can, however, treat "undefined" any way we please.
2800 Call abort to encourage the user to fix the program. */
2801 inform (loc, "if this code is reached, the program will abort");
2802 /* Before the abort, allow the function arguments to exit or
2803 call longjmp. */
2804 for (i = 0; i < nargs; i++)
2805 trap = build2 (COMPOUND_EXPR, void_type_node,
2806 VEC_index (tree, params, i), trap);
2808 if (VOID_TYPE_P (return_type))
2810 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2811 pedwarn (loc, 0,
2812 "function with qualified void return type called");
2813 return trap;
2815 else
2817 tree rhs;
2819 if (AGGREGATE_TYPE_P (return_type))
2820 rhs = build_compound_literal (loc, return_type,
2821 build_constructor (return_type, 0),
2822 false);
2823 else
2824 rhs = build_zero_cst (return_type);
2826 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2827 trap, rhs));
2831 argarray = VEC_address (tree, params);
2833 /* Check that arguments to builtin functions match the expectations. */
2834 if (fundecl
2835 && DECL_BUILT_IN (fundecl)
2836 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2837 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2838 return error_mark_node;
2840 /* Check that the arguments to the function are valid. */
2841 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2842 TYPE_ARG_TYPES (fntype));
2844 if (name != NULL_TREE
2845 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2847 if (require_constant_value)
2848 result =
2849 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2850 function, nargs, argarray);
2851 else
2852 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2853 function, nargs, argarray);
2854 if (TREE_CODE (result) == NOP_EXPR
2855 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2856 STRIP_TYPE_NOPS (result);
2858 else
2859 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2860 function, nargs, argarray);
2862 if (VOID_TYPE_P (TREE_TYPE (result)))
2864 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2865 pedwarn (loc, 0,
2866 "function with qualified void return type called");
2867 return result;
2869 return require_complete_type (result);
2872 /* Convert the argument expressions in the vector VALUES
2873 to the types in the list TYPELIST.
2875 If TYPELIST is exhausted, or when an element has NULL as its type,
2876 perform the default conversions.
2878 ORIGTYPES is the original types of the expressions in VALUES. This
2879 holds the type of enum values which have been converted to integral
2880 types. It may be NULL.
2882 FUNCTION is a tree for the called function. It is used only for
2883 error messages, where it is formatted with %qE.
2885 This is also where warnings about wrong number of args are generated.
2887 Returns the actual number of arguments processed (which may be less
2888 than the length of VALUES in some error situations), or -1 on
2889 failure. */
2891 static int
2892 convert_arguments (tree typelist, VEC(tree,gc) *values,
2893 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2895 tree typetail, val;
2896 unsigned int parmnum;
2897 bool error_args = false;
2898 const bool type_generic = fundecl
2899 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2900 bool type_generic_remove_excess_precision = false;
2901 tree selector;
2903 /* Change pointer to function to the function itself for
2904 diagnostics. */
2905 if (TREE_CODE (function) == ADDR_EXPR
2906 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2907 function = TREE_OPERAND (function, 0);
2909 /* Handle an ObjC selector specially for diagnostics. */
2910 selector = objc_message_selector ();
2912 /* For type-generic built-in functions, determine whether excess
2913 precision should be removed (classification) or not
2914 (comparison). */
2915 if (type_generic
2916 && DECL_BUILT_IN (fundecl)
2917 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2919 switch (DECL_FUNCTION_CODE (fundecl))
2921 case BUILT_IN_ISFINITE:
2922 case BUILT_IN_ISINF:
2923 case BUILT_IN_ISINF_SIGN:
2924 case BUILT_IN_ISNAN:
2925 case BUILT_IN_ISNORMAL:
2926 case BUILT_IN_FPCLASSIFY:
2927 type_generic_remove_excess_precision = true;
2928 break;
2930 default:
2931 type_generic_remove_excess_precision = false;
2932 break;
2936 /* Scan the given expressions and types, producing individual
2937 converted arguments. */
2939 for (typetail = typelist, parmnum = 0;
2940 VEC_iterate (tree, values, parmnum, val);
2941 ++parmnum)
2943 tree type = typetail ? TREE_VALUE (typetail) : 0;
2944 tree valtype = TREE_TYPE (val);
2945 tree rname = function;
2946 int argnum = parmnum + 1;
2947 const char *invalid_func_diag;
2948 bool excess_precision = false;
2949 bool npc;
2950 tree parmval;
2952 if (type == void_type_node)
2954 if (selector)
2955 error_at (input_location,
2956 "too many arguments to method %qE", selector);
2957 else
2958 error_at (input_location,
2959 "too many arguments to function %qE", function);
2961 if (fundecl && !DECL_BUILT_IN (fundecl))
2962 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2963 return parmnum;
2966 if (selector && argnum > 2)
2968 rname = selector;
2969 argnum -= 2;
2972 npc = null_pointer_constant_p (val);
2974 /* If there is excess precision and a prototype, convert once to
2975 the required type rather than converting via the semantic
2976 type. Likewise without a prototype a float value represented
2977 as long double should be converted once to double. But for
2978 type-generic classification functions excess precision must
2979 be removed here. */
2980 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2981 && (type || !type_generic || !type_generic_remove_excess_precision))
2983 val = TREE_OPERAND (val, 0);
2984 excess_precision = true;
2986 val = c_fully_fold (val, false, NULL);
2987 STRIP_TYPE_NOPS (val);
2989 val = require_complete_type (val);
2991 if (type != 0)
2993 /* Formal parm type is specified by a function prototype. */
2995 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2997 error ("type of formal parameter %d is incomplete", parmnum + 1);
2998 parmval = val;
3000 else
3002 tree origtype;
3004 /* Optionally warn about conversions that
3005 differ from the default conversions. */
3006 if (warn_traditional_conversion || warn_traditional)
3008 unsigned int formal_prec = TYPE_PRECISION (type);
3010 if (INTEGRAL_TYPE_P (type)
3011 && TREE_CODE (valtype) == REAL_TYPE)
3012 warning (0, "passing argument %d of %qE as integer "
3013 "rather than floating due to prototype",
3014 argnum, rname);
3015 if (INTEGRAL_TYPE_P (type)
3016 && TREE_CODE (valtype) == COMPLEX_TYPE)
3017 warning (0, "passing argument %d of %qE as integer "
3018 "rather than complex due to prototype",
3019 argnum, rname);
3020 else if (TREE_CODE (type) == COMPLEX_TYPE
3021 && TREE_CODE (valtype) == REAL_TYPE)
3022 warning (0, "passing argument %d of %qE as complex "
3023 "rather than floating due to prototype",
3024 argnum, rname);
3025 else if (TREE_CODE (type) == REAL_TYPE
3026 && INTEGRAL_TYPE_P (valtype))
3027 warning (0, "passing argument %d of %qE as floating "
3028 "rather than integer due to prototype",
3029 argnum, rname);
3030 else if (TREE_CODE (type) == COMPLEX_TYPE
3031 && INTEGRAL_TYPE_P (valtype))
3032 warning (0, "passing argument %d of %qE as complex "
3033 "rather than integer due to prototype",
3034 argnum, rname);
3035 else if (TREE_CODE (type) == REAL_TYPE
3036 && TREE_CODE (valtype) == COMPLEX_TYPE)
3037 warning (0, "passing argument %d of %qE as floating "
3038 "rather than complex due to prototype",
3039 argnum, rname);
3040 /* ??? At some point, messages should be written about
3041 conversions between complex types, but that's too messy
3042 to do now. */
3043 else if (TREE_CODE (type) == REAL_TYPE
3044 && TREE_CODE (valtype) == REAL_TYPE)
3046 /* Warn if any argument is passed as `float',
3047 since without a prototype it would be `double'. */
3048 if (formal_prec == TYPE_PRECISION (float_type_node)
3049 && type != dfloat32_type_node)
3050 warning (0, "passing argument %d of %qE as %<float%> "
3051 "rather than %<double%> due to prototype",
3052 argnum, rname);
3054 /* Warn if mismatch between argument and prototype
3055 for decimal float types. Warn of conversions with
3056 binary float types and of precision narrowing due to
3057 prototype. */
3058 else if (type != valtype
3059 && (type == dfloat32_type_node
3060 || type == dfloat64_type_node
3061 || type == dfloat128_type_node
3062 || valtype == dfloat32_type_node
3063 || valtype == dfloat64_type_node
3064 || valtype == dfloat128_type_node)
3065 && (formal_prec
3066 <= TYPE_PRECISION (valtype)
3067 || (type == dfloat128_type_node
3068 && (valtype
3069 != dfloat64_type_node
3070 && (valtype
3071 != dfloat32_type_node)))
3072 || (type == dfloat64_type_node
3073 && (valtype
3074 != dfloat32_type_node))))
3075 warning (0, "passing argument %d of %qE as %qT "
3076 "rather than %qT due to prototype",
3077 argnum, rname, type, valtype);
3080 /* Detect integer changing in width or signedness.
3081 These warnings are only activated with
3082 -Wtraditional-conversion, not with -Wtraditional. */
3083 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3084 && INTEGRAL_TYPE_P (valtype))
3086 tree would_have_been = default_conversion (val);
3087 tree type1 = TREE_TYPE (would_have_been);
3089 if (TREE_CODE (type) == ENUMERAL_TYPE
3090 && (TYPE_MAIN_VARIANT (type)
3091 == TYPE_MAIN_VARIANT (valtype)))
3092 /* No warning if function asks for enum
3093 and the actual arg is that enum type. */
3095 else if (formal_prec != TYPE_PRECISION (type1))
3096 warning (OPT_Wtraditional_conversion,
3097 "passing argument %d of %qE "
3098 "with different width due to prototype",
3099 argnum, rname);
3100 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3102 /* Don't complain if the formal parameter type
3103 is an enum, because we can't tell now whether
3104 the value was an enum--even the same enum. */
3105 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3107 else if (TREE_CODE (val) == INTEGER_CST
3108 && int_fits_type_p (val, type))
3109 /* Change in signedness doesn't matter
3110 if a constant value is unaffected. */
3112 /* If the value is extended from a narrower
3113 unsigned type, it doesn't matter whether we
3114 pass it as signed or unsigned; the value
3115 certainly is the same either way. */
3116 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3117 && TYPE_UNSIGNED (valtype))
3119 else if (TYPE_UNSIGNED (type))
3120 warning (OPT_Wtraditional_conversion,
3121 "passing argument %d of %qE "
3122 "as unsigned due to prototype",
3123 argnum, rname);
3124 else
3125 warning (OPT_Wtraditional_conversion,
3126 "passing argument %d of %qE "
3127 "as signed due to prototype", argnum, rname);
3131 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3132 sake of better warnings from convert_and_check. */
3133 if (excess_precision)
3134 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3135 origtype = (origtypes == NULL
3136 ? NULL_TREE
3137 : VEC_index (tree, origtypes, parmnum));
3138 parmval = convert_for_assignment (input_location, type, val,
3139 origtype, ic_argpass, npc,
3140 fundecl, function,
3141 parmnum + 1);
3143 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3144 && INTEGRAL_TYPE_P (type)
3145 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3146 parmval = default_conversion (parmval);
3149 else if (TREE_CODE (valtype) == REAL_TYPE
3150 && (TYPE_PRECISION (valtype)
3151 < TYPE_PRECISION (double_type_node))
3152 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3154 if (type_generic)
3155 parmval = val;
3156 else
3158 /* Convert `float' to `double'. */
3159 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3160 warning (OPT_Wdouble_promotion,
3161 "implicit conversion from %qT to %qT when passing "
3162 "argument to function",
3163 valtype, double_type_node);
3164 parmval = convert (double_type_node, val);
3167 else if (excess_precision && !type_generic)
3168 /* A "double" argument with excess precision being passed
3169 without a prototype or in variable arguments. */
3170 parmval = convert (valtype, val);
3171 else if ((invalid_func_diag =
3172 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3174 error (invalid_func_diag);
3175 return -1;
3177 else
3178 /* Convert `short' and `char' to full-size `int'. */
3179 parmval = default_conversion (val);
3181 VEC_replace (tree, values, parmnum, parmval);
3182 if (parmval == error_mark_node)
3183 error_args = true;
3185 if (typetail)
3186 typetail = TREE_CHAIN (typetail);
3189 gcc_assert (parmnum == VEC_length (tree, values));
3191 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3193 error_at (input_location,
3194 "too few arguments to function %qE", function);
3195 if (fundecl && !DECL_BUILT_IN (fundecl))
3196 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3197 return -1;
3200 return error_args ? -1 : (int) parmnum;
3203 /* This is the entry point used by the parser to build unary operators
3204 in the input. CODE, a tree_code, specifies the unary operator, and
3205 ARG is the operand. For unary plus, the C parser currently uses
3206 CONVERT_EXPR for code.
3208 LOC is the location to use for the tree generated.
3211 struct c_expr
3212 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3214 struct c_expr result;
3216 result.value = build_unary_op (loc, code, arg.value, 0);
3217 result.original_code = code;
3218 result.original_type = NULL;
3220 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3221 overflow_warning (loc, result.value);
3223 return result;
3226 /* This is the entry point used by the parser to build binary operators
3227 in the input. CODE, a tree_code, specifies the binary operator, and
3228 ARG1 and ARG2 are the operands. In addition to constructing the
3229 expression, we check for operands that were written with other binary
3230 operators in a way that is likely to confuse the user.
3232 LOCATION is the location of the binary operator. */
3234 struct c_expr
3235 parser_build_binary_op (location_t location, enum tree_code code,
3236 struct c_expr arg1, struct c_expr arg2)
3238 struct c_expr result;
3240 enum tree_code code1 = arg1.original_code;
3241 enum tree_code code2 = arg2.original_code;
3242 tree type1 = (arg1.original_type
3243 ? arg1.original_type
3244 : TREE_TYPE (arg1.value));
3245 tree type2 = (arg2.original_type
3246 ? arg2.original_type
3247 : TREE_TYPE (arg2.value));
3249 result.value = build_binary_op (location, code,
3250 arg1.value, arg2.value, 1);
3251 result.original_code = code;
3252 result.original_type = NULL;
3254 if (TREE_CODE (result.value) == ERROR_MARK)
3255 return result;
3257 if (location != UNKNOWN_LOCATION)
3258 protected_set_expr_location (result.value, location);
3260 /* Check for cases such as x+y<<z which users are likely
3261 to misinterpret. */
3262 if (warn_parentheses)
3263 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3265 if (warn_logical_op)
3266 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3267 code1, arg1.value, code2, arg2.value);
3269 /* Warn about comparisons against string literals, with the exception
3270 of testing for equality or inequality of a string literal with NULL. */
3271 if (code == EQ_EXPR || code == NE_EXPR)
3273 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3274 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3275 warning_at (location, OPT_Waddress,
3276 "comparison with string literal results in unspecified behavior");
3278 else if (TREE_CODE_CLASS (code) == tcc_comparison
3279 && (code1 == STRING_CST || code2 == STRING_CST))
3280 warning_at (location, OPT_Waddress,
3281 "comparison with string literal results in unspecified behavior");
3283 if (TREE_OVERFLOW_P (result.value)
3284 && !TREE_OVERFLOW_P (arg1.value)
3285 && !TREE_OVERFLOW_P (arg2.value))
3286 overflow_warning (location, result.value);
3288 /* Warn about comparisons of different enum types. */
3289 if (warn_enum_compare
3290 && TREE_CODE_CLASS (code) == tcc_comparison
3291 && TREE_CODE (type1) == ENUMERAL_TYPE
3292 && TREE_CODE (type2) == ENUMERAL_TYPE
3293 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3294 warning_at (location, OPT_Wenum_compare,
3295 "comparison between %qT and %qT",
3296 type1, type2);
3298 return result;
3301 /* Return a tree for the difference of pointers OP0 and OP1.
3302 The resulting tree has type int. */
3304 static tree
3305 pointer_diff (location_t loc, tree op0, tree op1)
3307 tree restype = ptrdiff_type_node;
3308 tree result, inttype;
3310 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3311 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3312 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3313 tree con0, con1, lit0, lit1;
3314 tree orig_op1 = op1;
3316 /* If the operands point into different address spaces, we need to
3317 explicitly convert them to pointers into the common address space
3318 before we can subtract the numerical address values. */
3319 if (as0 != as1)
3321 addr_space_t as_common;
3322 tree common_type;
3324 /* Determine the common superset address space. This is guaranteed
3325 to exist because the caller verified that comp_target_types
3326 returned non-zero. */
3327 if (!addr_space_superset (as0, as1, &as_common))
3328 gcc_unreachable ();
3330 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3331 op0 = convert (common_type, op0);
3332 op1 = convert (common_type, op1);
3335 /* Determine integer type to perform computations in. This will usually
3336 be the same as the result type (ptrdiff_t), but may need to be a wider
3337 type if pointers for the address space are wider than ptrdiff_t. */
3338 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3339 inttype = lang_hooks.types.type_for_size
3340 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3341 else
3342 inttype = restype;
3345 if (TREE_CODE (target_type) == VOID_TYPE)
3346 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3347 "pointer of type %<void *%> used in subtraction");
3348 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3349 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3350 "pointer to a function used in subtraction");
3352 /* If the conversion to ptrdiff_type does anything like widening or
3353 converting a partial to an integral mode, we get a convert_expression
3354 that is in the way to do any simplifications.
3355 (fold-const.c doesn't know that the extra bits won't be needed.
3356 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3357 different mode in place.)
3358 So first try to find a common term here 'by hand'; we want to cover
3359 at least the cases that occur in legal static initializers. */
3360 if (CONVERT_EXPR_P (op0)
3361 && (TYPE_PRECISION (TREE_TYPE (op0))
3362 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3363 con0 = TREE_OPERAND (op0, 0);
3364 else
3365 con0 = op0;
3366 if (CONVERT_EXPR_P (op1)
3367 && (TYPE_PRECISION (TREE_TYPE (op1))
3368 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3369 con1 = TREE_OPERAND (op1, 0);
3370 else
3371 con1 = op1;
3373 if (TREE_CODE (con0) == PLUS_EXPR)
3375 lit0 = TREE_OPERAND (con0, 1);
3376 con0 = TREE_OPERAND (con0, 0);
3378 else
3379 lit0 = integer_zero_node;
3381 if (TREE_CODE (con1) == PLUS_EXPR)
3383 lit1 = TREE_OPERAND (con1, 1);
3384 con1 = TREE_OPERAND (con1, 0);
3386 else
3387 lit1 = integer_zero_node;
3389 if (operand_equal_p (con0, con1, 0))
3391 op0 = lit0;
3392 op1 = lit1;
3396 /* First do the subtraction as integers;
3397 then drop through to build the divide operator.
3398 Do not do default conversions on the minus operator
3399 in case restype is a short type. */
3401 op0 = build_binary_op (loc,
3402 MINUS_EXPR, convert (inttype, op0),
3403 convert (inttype, op1), 0);
3404 /* This generates an error if op1 is pointer to incomplete type. */
3405 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3406 error_at (loc, "arithmetic on pointer to an incomplete type");
3408 /* This generates an error if op0 is pointer to incomplete type. */
3409 op1 = c_size_in_bytes (target_type);
3411 /* Divide by the size, in easiest possible way. */
3412 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3413 op0, convert (inttype, op1));
3415 /* Convert to final result type if necessary. */
3416 return convert (restype, result);
3419 /* Construct and perhaps optimize a tree representation
3420 for a unary operation. CODE, a tree_code, specifies the operation
3421 and XARG is the operand.
3422 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3423 the default promotions (such as from short to int).
3424 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3425 allows non-lvalues; this is only used to handle conversion of non-lvalue
3426 arrays to pointers in C99.
3428 LOCATION is the location of the operator. */
3430 tree
3431 build_unary_op (location_t location,
3432 enum tree_code code, tree xarg, int flag)
3434 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3435 tree arg = xarg;
3436 tree argtype = 0;
3437 enum tree_code typecode;
3438 tree val;
3439 tree ret = error_mark_node;
3440 tree eptype = NULL_TREE;
3441 int noconvert = flag;
3442 const char *invalid_op_diag;
3443 bool int_operands;
3445 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3446 if (int_operands)
3447 arg = remove_c_maybe_const_expr (arg);
3449 if (code != ADDR_EXPR)
3450 arg = require_complete_type (arg);
3452 typecode = TREE_CODE (TREE_TYPE (arg));
3453 if (typecode == ERROR_MARK)
3454 return error_mark_node;
3455 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3456 typecode = INTEGER_TYPE;
3458 if ((invalid_op_diag
3459 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3461 error_at (location, invalid_op_diag);
3462 return error_mark_node;
3465 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3467 eptype = TREE_TYPE (arg);
3468 arg = TREE_OPERAND (arg, 0);
3471 switch (code)
3473 case CONVERT_EXPR:
3474 /* This is used for unary plus, because a CONVERT_EXPR
3475 is enough to prevent anybody from looking inside for
3476 associativity, but won't generate any code. */
3477 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3478 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3479 || typecode == VECTOR_TYPE))
3481 error_at (location, "wrong type argument to unary plus");
3482 return error_mark_node;
3484 else if (!noconvert)
3485 arg = default_conversion (arg);
3486 arg = non_lvalue_loc (location, arg);
3487 break;
3489 case NEGATE_EXPR:
3490 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3491 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3492 || typecode == VECTOR_TYPE))
3494 error_at (location, "wrong type argument to unary minus");
3495 return error_mark_node;
3497 else if (!noconvert)
3498 arg = default_conversion (arg);
3499 break;
3501 case BIT_NOT_EXPR:
3502 /* ~ works on integer types and non float vectors. */
3503 if (typecode == INTEGER_TYPE
3504 || (typecode == VECTOR_TYPE
3505 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3507 if (!noconvert)
3508 arg = default_conversion (arg);
3510 else if (typecode == COMPLEX_TYPE)
3512 code = CONJ_EXPR;
3513 pedwarn (location, OPT_pedantic,
3514 "ISO C does not support %<~%> for complex conjugation");
3515 if (!noconvert)
3516 arg = default_conversion (arg);
3518 else
3520 error_at (location, "wrong type argument to bit-complement");
3521 return error_mark_node;
3523 break;
3525 case ABS_EXPR:
3526 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3528 error_at (location, "wrong type argument to abs");
3529 return error_mark_node;
3531 else if (!noconvert)
3532 arg = default_conversion (arg);
3533 break;
3535 case CONJ_EXPR:
3536 /* Conjugating a real value is a no-op, but allow it anyway. */
3537 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3538 || typecode == COMPLEX_TYPE))
3540 error_at (location, "wrong type argument to conjugation");
3541 return error_mark_node;
3543 else if (!noconvert)
3544 arg = default_conversion (arg);
3545 break;
3547 case TRUTH_NOT_EXPR:
3548 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3549 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3550 && typecode != COMPLEX_TYPE)
3552 error_at (location,
3553 "wrong type argument to unary exclamation mark");
3554 return error_mark_node;
3556 arg = c_objc_common_truthvalue_conversion (location, arg);
3557 ret = invert_truthvalue_loc (location, arg);
3558 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3559 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3560 location = EXPR_LOCATION (ret);
3561 goto return_build_unary_op;
3563 case REALPART_EXPR:
3564 if (TREE_CODE (arg) == COMPLEX_CST)
3565 ret = TREE_REALPART (arg);
3566 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3567 ret = fold_build1_loc (location,
3568 REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3569 else
3570 ret = arg;
3571 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3572 eptype = TREE_TYPE (eptype);
3573 goto return_build_unary_op;
3575 case IMAGPART_EXPR:
3576 if (TREE_CODE (arg) == COMPLEX_CST)
3577 ret = TREE_IMAGPART (arg);
3578 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3579 ret = fold_build1_loc (location,
3580 IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3581 else
3582 ret = omit_one_operand_loc (location, TREE_TYPE (arg),
3583 integer_zero_node, arg);
3584 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3585 eptype = TREE_TYPE (eptype);
3586 goto return_build_unary_op;
3588 case PREINCREMENT_EXPR:
3589 case POSTINCREMENT_EXPR:
3590 case PREDECREMENT_EXPR:
3591 case POSTDECREMENT_EXPR:
3593 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3595 tree inner = build_unary_op (location, code,
3596 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3597 if (inner == error_mark_node)
3598 return error_mark_node;
3599 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3600 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3601 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3602 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3603 goto return_build_unary_op;
3606 /* Complain about anything that is not a true lvalue. */
3607 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3608 || code == POSTINCREMENT_EXPR)
3609 ? lv_increment
3610 : lv_decrement)))
3611 return error_mark_node;
3613 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3615 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3616 warning_at (location, OPT_Wc___compat,
3617 "increment of enumeration value is invalid in C++");
3618 else
3619 warning_at (location, OPT_Wc___compat,
3620 "decrement of enumeration value is invalid in C++");
3623 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3624 arg = c_fully_fold (arg, false, NULL);
3626 /* Increment or decrement the real part of the value,
3627 and don't change the imaginary part. */
3628 if (typecode == COMPLEX_TYPE)
3630 tree real, imag;
3632 pedwarn (location, OPT_pedantic,
3633 "ISO C does not support %<++%> and %<--%> on complex types");
3635 arg = stabilize_reference (arg);
3636 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3637 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3638 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3639 if (real == error_mark_node || imag == error_mark_node)
3640 return error_mark_node;
3641 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3642 real, imag);
3643 goto return_build_unary_op;
3646 /* Report invalid types. */
3648 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3649 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3651 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3652 error_at (location, "wrong type argument to increment");
3653 else
3654 error_at (location, "wrong type argument to decrement");
3656 return error_mark_node;
3660 tree inc;
3662 argtype = TREE_TYPE (arg);
3664 /* Compute the increment. */
3666 if (typecode == POINTER_TYPE)
3668 /* If pointer target is an undefined struct,
3669 we just cannot know how to do the arithmetic. */
3670 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3672 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3673 error_at (location,
3674 "increment of pointer to unknown structure");
3675 else
3676 error_at (location,
3677 "decrement of pointer to unknown structure");
3679 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3680 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3682 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3683 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3684 "wrong type argument to increment");
3685 else
3686 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3687 "wrong type argument to decrement");
3690 inc = c_size_in_bytes (TREE_TYPE (argtype));
3691 inc = fold_convert_loc (location, sizetype, inc);
3693 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3695 /* For signed fract types, we invert ++ to -- or
3696 -- to ++, and change inc from 1 to -1, because
3697 it is not possible to represent 1 in signed fract constants.
3698 For unsigned fract types, the result always overflows and
3699 we get an undefined (original) or the maximum value. */
3700 if (code == PREINCREMENT_EXPR)
3701 code = PREDECREMENT_EXPR;
3702 else if (code == PREDECREMENT_EXPR)
3703 code = PREINCREMENT_EXPR;
3704 else if (code == POSTINCREMENT_EXPR)
3705 code = POSTDECREMENT_EXPR;
3706 else /* code == POSTDECREMENT_EXPR */
3707 code = POSTINCREMENT_EXPR;
3709 inc = integer_minus_one_node;
3710 inc = convert (argtype, inc);
3712 else
3714 inc = integer_one_node;
3715 inc = convert (argtype, inc);
3718 /* Report a read-only lvalue. */
3719 if (TYPE_READONLY (argtype))
3721 readonly_error (arg,
3722 ((code == PREINCREMENT_EXPR
3723 || code == POSTINCREMENT_EXPR)
3724 ? lv_increment : lv_decrement));
3725 return error_mark_node;
3727 else if (TREE_READONLY (arg))
3728 readonly_warning (arg,
3729 ((code == PREINCREMENT_EXPR
3730 || code == POSTINCREMENT_EXPR)
3731 ? lv_increment : lv_decrement));
3733 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3734 val = boolean_increment (code, arg);
3735 else
3736 val = build2 (code, TREE_TYPE (arg), arg, inc);
3737 TREE_SIDE_EFFECTS (val) = 1;
3738 if (TREE_CODE (val) != code)
3739 TREE_NO_WARNING (val) = 1;
3740 ret = val;
3741 goto return_build_unary_op;
3744 case ADDR_EXPR:
3745 /* Note that this operation never does default_conversion. */
3747 /* The operand of unary '&' must be an lvalue (which excludes
3748 expressions of type void), or, in C99, the result of a [] or
3749 unary '*' operator. */
3750 if (VOID_TYPE_P (TREE_TYPE (arg))
3751 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3752 && (TREE_CODE (arg) != INDIRECT_REF
3753 || !flag_isoc99))
3754 pedwarn (location, 0, "taking address of expression of type %<void%>");
3756 /* Let &* cancel out to simplify resulting code. */
3757 if (TREE_CODE (arg) == INDIRECT_REF)
3759 /* Don't let this be an lvalue. */
3760 if (lvalue_p (TREE_OPERAND (arg, 0)))
3761 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3762 ret = TREE_OPERAND (arg, 0);
3763 goto return_build_unary_op;
3766 /* For &x[y], return x+y */
3767 if (TREE_CODE (arg) == ARRAY_REF)
3769 tree op0 = TREE_OPERAND (arg, 0);
3770 if (!c_mark_addressable (op0))
3771 return error_mark_node;
3772 return build_binary_op (location, PLUS_EXPR,
3773 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3774 ? array_to_pointer_conversion (location,
3775 op0)
3776 : op0),
3777 TREE_OPERAND (arg, 1), 1);
3780 /* Anything not already handled and not a true memory reference
3781 or a non-lvalue array is an error. */
3782 else if (typecode != FUNCTION_TYPE && !flag
3783 && !lvalue_or_else (arg, lv_addressof))
3784 return error_mark_node;
3786 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3787 folding later. */
3788 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3790 tree inner = build_unary_op (location, code,
3791 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3792 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3793 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3794 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3795 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3796 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3797 goto return_build_unary_op;
3800 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3801 argtype = TREE_TYPE (arg);
3803 /* If the lvalue is const or volatile, merge that into the type
3804 to which the address will point. This should only be needed
3805 for function types. */
3806 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3807 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3809 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3810 int quals = orig_quals;
3812 if (TREE_READONLY (arg))
3813 quals |= TYPE_QUAL_CONST;
3814 if (TREE_THIS_VOLATILE (arg))
3815 quals |= TYPE_QUAL_VOLATILE;
3817 gcc_assert (quals == orig_quals
3818 || TREE_CODE (argtype) == FUNCTION_TYPE);
3820 argtype = c_build_qualified_type (argtype, quals);
3823 if (!c_mark_addressable (arg))
3824 return error_mark_node;
3826 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3827 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3829 argtype = build_pointer_type (argtype);
3831 /* ??? Cope with user tricks that amount to offsetof. Delete this
3832 when we have proper support for integer constant expressions. */
3833 val = get_base_address (arg);
3834 if (val && TREE_CODE (val) == INDIRECT_REF
3835 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3837 tree op0 = fold_convert_loc (location, sizetype,
3838 fold_offsetof (arg, val)), op1;
3840 op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3841 ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3842 goto return_build_unary_op;
3845 val = build1 (ADDR_EXPR, argtype, arg);
3847 ret = val;
3848 goto return_build_unary_op;
3850 default:
3851 gcc_unreachable ();
3854 if (argtype == 0)
3855 argtype = TREE_TYPE (arg);
3856 if (TREE_CODE (arg) == INTEGER_CST)
3857 ret = (require_constant_value
3858 ? fold_build1_initializer_loc (location, code, argtype, arg)
3859 : fold_build1_loc (location, code, argtype, arg));
3860 else
3861 ret = build1 (code, argtype, arg);
3862 return_build_unary_op:
3863 gcc_assert (ret != error_mark_node);
3864 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3865 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3866 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3867 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3868 ret = note_integer_operands (ret);
3869 if (eptype)
3870 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3871 protected_set_expr_location (ret, location);
3872 return ret;
3875 /* Return nonzero if REF is an lvalue valid for this language.
3876 Lvalues can be assigned, unless their type has TYPE_READONLY.
3877 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3879 bool
3880 lvalue_p (const_tree ref)
3882 const enum tree_code code = TREE_CODE (ref);
3884 switch (code)
3886 case REALPART_EXPR:
3887 case IMAGPART_EXPR:
3888 case COMPONENT_REF:
3889 return lvalue_p (TREE_OPERAND (ref, 0));
3891 case C_MAYBE_CONST_EXPR:
3892 return lvalue_p (TREE_OPERAND (ref, 1));
3894 case COMPOUND_LITERAL_EXPR:
3895 case STRING_CST:
3896 return 1;
3898 case INDIRECT_REF:
3899 case ARRAY_REF:
3900 case VAR_DECL:
3901 case PARM_DECL:
3902 case RESULT_DECL:
3903 case ERROR_MARK:
3904 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3905 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3907 case BIND_EXPR:
3908 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3910 default:
3911 return 0;
3915 /* Give an error for storing in something that is 'const'. */
3917 static void
3918 readonly_error (tree arg, enum lvalue_use use)
3920 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3921 || use == lv_asm);
3922 /* Using this macro rather than (for example) arrays of messages
3923 ensures that all the format strings are checked at compile
3924 time. */
3925 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3926 : (use == lv_increment ? (I) \
3927 : (use == lv_decrement ? (D) : (AS))))
3928 if (TREE_CODE (arg) == COMPONENT_REF)
3930 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3931 readonly_error (TREE_OPERAND (arg, 0), use);
3932 else
3933 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3934 G_("increment of read-only member %qD"),
3935 G_("decrement of read-only member %qD"),
3936 G_("read-only member %qD used as %<asm%> output")),
3937 TREE_OPERAND (arg, 1));
3939 else if (TREE_CODE (arg) == VAR_DECL)
3940 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3941 G_("increment of read-only variable %qD"),
3942 G_("decrement of read-only variable %qD"),
3943 G_("read-only variable %qD used as %<asm%> output")),
3944 arg);
3945 else
3946 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3947 G_("increment of read-only location %qE"),
3948 G_("decrement of read-only location %qE"),
3949 G_("read-only location %qE used as %<asm%> output")),
3950 arg);
3953 /* Give a warning for storing in something that is read-only in GCC
3954 terms but not const in ISO C terms. */
3956 static void
3957 readonly_warning (tree arg, enum lvalue_use use)
3959 switch (use)
3961 case lv_assign:
3962 warning (0, "assignment of read-only location %qE", arg);
3963 break;
3964 case lv_increment:
3965 warning (0, "increment of read-only location %qE", arg);
3966 break;
3967 case lv_decrement:
3968 warning (0, "decrement of read-only location %qE", arg);
3969 break;
3970 default:
3971 gcc_unreachable ();
3973 return;
3977 /* Return nonzero if REF is an lvalue valid for this language;
3978 otherwise, print an error message and return zero. USE says
3979 how the lvalue is being used and so selects the error message. */
3981 static int
3982 lvalue_or_else (const_tree ref, enum lvalue_use use)
3984 int win = lvalue_p (ref);
3986 if (!win)
3987 lvalue_error (use);
3989 return win;
3992 /* Mark EXP saying that we need to be able to take the
3993 address of it; it should not be allocated in a register.
3994 Returns true if successful. */
3996 bool
3997 c_mark_addressable (tree exp)
3999 tree x = exp;
4001 while (1)
4002 switch (TREE_CODE (x))
4004 case COMPONENT_REF:
4005 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4007 error
4008 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4009 return false;
4012 /* ... fall through ... */
4014 case ADDR_EXPR:
4015 case ARRAY_REF:
4016 case REALPART_EXPR:
4017 case IMAGPART_EXPR:
4018 x = TREE_OPERAND (x, 0);
4019 break;
4021 case COMPOUND_LITERAL_EXPR:
4022 case CONSTRUCTOR:
4023 TREE_ADDRESSABLE (x) = 1;
4024 return true;
4026 case VAR_DECL:
4027 case CONST_DECL:
4028 case PARM_DECL:
4029 case RESULT_DECL:
4030 if (C_DECL_REGISTER (x)
4031 && DECL_NONLOCAL (x))
4033 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4035 error
4036 ("global register variable %qD used in nested function", x);
4037 return false;
4039 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4041 else if (C_DECL_REGISTER (x))
4043 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4044 error ("address of global register variable %qD requested", x);
4045 else
4046 error ("address of register variable %qD requested", x);
4047 return false;
4050 /* drops in */
4051 case FUNCTION_DECL:
4052 TREE_ADDRESSABLE (x) = 1;
4053 /* drops out */
4054 default:
4055 return true;
4059 /* Convert EXPR to TYPE, warning about conversion problems with
4060 constants. SEMANTIC_TYPE is the type this conversion would use
4061 without excess precision. If SEMANTIC_TYPE is NULL, this function
4062 is equivalent to convert_and_check. This function is a wrapper that
4063 handles conversions that may be different than
4064 the usual ones because of excess precision. */
4066 static tree
4067 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4069 if (TREE_TYPE (expr) == type)
4070 return expr;
4072 if (!semantic_type)
4073 return convert_and_check (type, expr);
4075 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4076 && TREE_TYPE (expr) != semantic_type)
4078 /* For integers, we need to check the real conversion, not
4079 the conversion to the excess precision type. */
4080 expr = convert_and_check (semantic_type, expr);
4082 /* Result type is the excess precision type, which should be
4083 large enough, so do not check. */
4084 return convert (type, expr);
4087 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4088 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4089 if folded to an integer constant then the unselected half may
4090 contain arbitrary operations not normally permitted in constant
4091 expressions. Set the location of the expression to LOC. */
4093 tree
4094 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4095 tree op1, tree op1_original_type, tree op2,
4096 tree op2_original_type)
4098 tree type1;
4099 tree type2;
4100 enum tree_code code1;
4101 enum tree_code code2;
4102 tree result_type = NULL;
4103 tree semantic_result_type = NULL;
4104 tree orig_op1 = op1, orig_op2 = op2;
4105 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4106 bool ifexp_int_operands;
4107 tree ret;
4109 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4110 if (op1_int_operands)
4111 op1 = remove_c_maybe_const_expr (op1);
4112 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4113 if (op2_int_operands)
4114 op2 = remove_c_maybe_const_expr (op2);
4115 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4116 if (ifexp_int_operands)
4117 ifexp = remove_c_maybe_const_expr (ifexp);
4119 /* Promote both alternatives. */
4121 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4122 op1 = default_conversion (op1);
4123 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4124 op2 = default_conversion (op2);
4126 if (TREE_CODE (ifexp) == ERROR_MARK
4127 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4128 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4129 return error_mark_node;
4131 type1 = TREE_TYPE (op1);
4132 code1 = TREE_CODE (type1);
4133 type2 = TREE_TYPE (op2);
4134 code2 = TREE_CODE (type2);
4136 /* C90 does not permit non-lvalue arrays in conditional expressions.
4137 In C99 they will be pointers by now. */
4138 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4140 error_at (colon_loc, "non-lvalue array in conditional expression");
4141 return error_mark_node;
4144 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4145 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4146 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4147 || code1 == COMPLEX_TYPE)
4148 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4149 || code2 == COMPLEX_TYPE))
4151 semantic_result_type = c_common_type (type1, type2);
4152 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4154 op1 = TREE_OPERAND (op1, 0);
4155 type1 = TREE_TYPE (op1);
4156 gcc_assert (TREE_CODE (type1) == code1);
4158 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4160 op2 = TREE_OPERAND (op2, 0);
4161 type2 = TREE_TYPE (op2);
4162 gcc_assert (TREE_CODE (type2) == code2);
4166 if (warn_cxx_compat)
4168 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4169 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4171 if (TREE_CODE (t1) == ENUMERAL_TYPE
4172 && TREE_CODE (t2) == ENUMERAL_TYPE
4173 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4174 warning_at (colon_loc, OPT_Wc___compat,
4175 ("different enum types in conditional is "
4176 "invalid in C++: %qT vs %qT"),
4177 t1, t2);
4180 /* Quickly detect the usual case where op1 and op2 have the same type
4181 after promotion. */
4182 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4184 if (type1 == type2)
4185 result_type = type1;
4186 else
4187 result_type = TYPE_MAIN_VARIANT (type1);
4189 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4190 || code1 == COMPLEX_TYPE)
4191 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4192 || code2 == COMPLEX_TYPE))
4194 result_type = c_common_type (type1, type2);
4195 do_warn_double_promotion (result_type, type1, type2,
4196 "implicit conversion from %qT to %qT to "
4197 "match other result of conditional",
4198 colon_loc);
4200 /* If -Wsign-compare, warn here if type1 and type2 have
4201 different signedness. We'll promote the signed to unsigned
4202 and later code won't know it used to be different.
4203 Do this check on the original types, so that explicit casts
4204 will be considered, but default promotions won't. */
4205 if (c_inhibit_evaluation_warnings == 0)
4207 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4208 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4210 if (unsigned_op1 ^ unsigned_op2)
4212 bool ovf;
4214 /* Do not warn if the result type is signed, since the
4215 signed type will only be chosen if it can represent
4216 all the values of the unsigned type. */
4217 if (!TYPE_UNSIGNED (result_type))
4218 /* OK */;
4219 else
4221 bool op1_maybe_const = true;
4222 bool op2_maybe_const = true;
4224 /* Do not warn if the signed quantity is an
4225 unsuffixed integer literal (or some static
4226 constant expression involving such literals) and
4227 it is non-negative. This warning requires the
4228 operands to be folded for best results, so do
4229 that folding in this case even without
4230 warn_sign_compare to avoid warning options
4231 possibly affecting code generation. */
4232 c_inhibit_evaluation_warnings
4233 += (ifexp == truthvalue_false_node);
4234 op1 = c_fully_fold (op1, require_constant_value,
4235 &op1_maybe_const);
4236 c_inhibit_evaluation_warnings
4237 -= (ifexp == truthvalue_false_node);
4239 c_inhibit_evaluation_warnings
4240 += (ifexp == truthvalue_true_node);
4241 op2 = c_fully_fold (op2, require_constant_value,
4242 &op2_maybe_const);
4243 c_inhibit_evaluation_warnings
4244 -= (ifexp == truthvalue_true_node);
4246 if (warn_sign_compare)
4248 if ((unsigned_op2
4249 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4250 || (unsigned_op1
4251 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4252 /* OK */;
4253 else
4254 warning_at (colon_loc, OPT_Wsign_compare,
4255 ("signed and unsigned type in "
4256 "conditional expression"));
4258 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4259 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4260 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4261 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4266 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4268 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4269 pedwarn (colon_loc, OPT_pedantic,
4270 "ISO C forbids conditional expr with only one void side");
4271 result_type = void_type_node;
4273 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4275 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4276 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4277 addr_space_t as_common;
4279 if (comp_target_types (colon_loc, type1, type2))
4280 result_type = common_pointer_type (type1, type2);
4281 else if (null_pointer_constant_p (orig_op1))
4282 result_type = type2;
4283 else if (null_pointer_constant_p (orig_op2))
4284 result_type = type1;
4285 else if (!addr_space_superset (as1, as2, &as_common))
4287 error_at (colon_loc, "pointers to disjoint address spaces "
4288 "used in conditional expression");
4289 return error_mark_node;
4291 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4293 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4294 pedwarn (colon_loc, OPT_pedantic,
4295 "ISO C forbids conditional expr between "
4296 "%<void *%> and function pointer");
4297 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4298 TREE_TYPE (type2)));
4300 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4302 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4303 pedwarn (colon_loc, OPT_pedantic,
4304 "ISO C forbids conditional expr between "
4305 "%<void *%> and function pointer");
4306 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4307 TREE_TYPE (type1)));
4309 /* Objective-C pointer comparisons are a bit more lenient. */
4310 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4311 result_type = objc_common_type (type1, type2);
4312 else
4314 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4316 pedwarn (colon_loc, 0,
4317 "pointer type mismatch in conditional expression");
4318 result_type = build_pointer_type
4319 (build_qualified_type (void_type_node, qual));
4322 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4324 if (!null_pointer_constant_p (orig_op2))
4325 pedwarn (colon_loc, 0,
4326 "pointer/integer type mismatch in conditional expression");
4327 else
4329 op2 = null_pointer_node;
4331 result_type = type1;
4333 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4335 if (!null_pointer_constant_p (orig_op1))
4336 pedwarn (colon_loc, 0,
4337 "pointer/integer type mismatch in conditional expression");
4338 else
4340 op1 = null_pointer_node;
4342 result_type = type2;
4345 if (!result_type)
4347 if (flag_cond_mismatch)
4348 result_type = void_type_node;
4349 else
4351 error_at (colon_loc, "type mismatch in conditional expression");
4352 return error_mark_node;
4356 /* Merge const and volatile flags of the incoming types. */
4357 result_type
4358 = build_type_variant (result_type,
4359 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4360 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4362 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4363 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4365 if (ifexp_bcp && ifexp == truthvalue_true_node)
4367 op2_int_operands = true;
4368 op1 = c_fully_fold (op1, require_constant_value, NULL);
4370 if (ifexp_bcp && ifexp == truthvalue_false_node)
4372 op1_int_operands = true;
4373 op2 = c_fully_fold (op2, require_constant_value, NULL);
4375 int_const = int_operands = (ifexp_int_operands
4376 && op1_int_operands
4377 && op2_int_operands);
4378 if (int_operands)
4380 int_const = ((ifexp == truthvalue_true_node
4381 && TREE_CODE (orig_op1) == INTEGER_CST
4382 && !TREE_OVERFLOW (orig_op1))
4383 || (ifexp == truthvalue_false_node
4384 && TREE_CODE (orig_op2) == INTEGER_CST
4385 && !TREE_OVERFLOW (orig_op2)));
4387 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4388 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4389 else
4391 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4392 if (int_operands)
4393 ret = note_integer_operands (ret);
4395 if (semantic_result_type)
4396 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4398 protected_set_expr_location (ret, colon_loc);
4399 return ret;
4402 /* Return a compound expression that performs two expressions and
4403 returns the value of the second of them.
4405 LOC is the location of the COMPOUND_EXPR. */
4407 tree
4408 build_compound_expr (location_t loc, tree expr1, tree expr2)
4410 bool expr1_int_operands, expr2_int_operands;
4411 tree eptype = NULL_TREE;
4412 tree ret;
4414 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4415 if (expr1_int_operands)
4416 expr1 = remove_c_maybe_const_expr (expr1);
4417 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4418 if (expr2_int_operands)
4419 expr2 = remove_c_maybe_const_expr (expr2);
4421 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4422 expr1 = TREE_OPERAND (expr1, 0);
4423 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4425 eptype = TREE_TYPE (expr2);
4426 expr2 = TREE_OPERAND (expr2, 0);
4429 if (!TREE_SIDE_EFFECTS (expr1))
4431 /* The left-hand operand of a comma expression is like an expression
4432 statement: with -Wunused, we should warn if it doesn't have
4433 any side-effects, unless it was explicitly cast to (void). */
4434 if (warn_unused_value)
4436 if (VOID_TYPE_P (TREE_TYPE (expr1))
4437 && CONVERT_EXPR_P (expr1))
4438 ; /* (void) a, b */
4439 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4440 && TREE_CODE (expr1) == COMPOUND_EXPR
4441 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4442 ; /* (void) a, (void) b, c */
4443 else
4444 warning_at (loc, OPT_Wunused_value,
4445 "left-hand operand of comma expression has no effect");
4449 /* With -Wunused, we should also warn if the left-hand operand does have
4450 side-effects, but computes a value which is not used. For example, in
4451 `foo() + bar(), baz()' the result of the `+' operator is not used,
4452 so we should issue a warning. */
4453 else if (warn_unused_value)
4454 warn_if_unused_value (expr1, loc);
4456 if (expr2 == error_mark_node)
4457 return error_mark_node;
4459 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4461 if (flag_isoc99
4462 && expr1_int_operands
4463 && expr2_int_operands)
4464 ret = note_integer_operands (ret);
4466 if (eptype)
4467 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4469 protected_set_expr_location (ret, loc);
4470 return ret;
4473 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4474 which we are casting. OTYPE is the type of the expression being
4475 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4476 of the cast. -Wcast-qual appeared on the command line. Named
4477 address space qualifiers are not handled here, because they result
4478 in different warnings. */
4480 static void
4481 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4483 tree in_type = type;
4484 tree in_otype = otype;
4485 int added = 0;
4486 int discarded = 0;
4487 bool is_const;
4489 /* Check that the qualifiers on IN_TYPE are a superset of the
4490 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4491 nodes is uninteresting and we stop as soon as we hit a
4492 non-POINTER_TYPE node on either type. */
4495 in_otype = TREE_TYPE (in_otype);
4496 in_type = TREE_TYPE (in_type);
4498 /* GNU C allows cv-qualified function types. 'const' means the
4499 function is very pure, 'volatile' means it can't return. We
4500 need to warn when such qualifiers are added, not when they're
4501 taken away. */
4502 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4503 && TREE_CODE (in_type) == FUNCTION_TYPE)
4504 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4505 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4506 else
4507 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4508 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4510 while (TREE_CODE (in_type) == POINTER_TYPE
4511 && TREE_CODE (in_otype) == POINTER_TYPE);
4513 if (added)
4514 warning_at (loc, OPT_Wcast_qual,
4515 "cast adds %q#v qualifier to function type", added);
4517 if (discarded)
4518 /* There are qualifiers present in IN_OTYPE that are not present
4519 in IN_TYPE. */
4520 warning_at (loc, OPT_Wcast_qual,
4521 "cast discards %q#v qualifier from pointer target type",
4522 discarded);
4524 if (added || discarded)
4525 return;
4527 /* A cast from **T to const **T is unsafe, because it can cause a
4528 const value to be changed with no additional warning. We only
4529 issue this warning if T is the same on both sides, and we only
4530 issue the warning if there are the same number of pointers on
4531 both sides, as otherwise the cast is clearly unsafe anyhow. A
4532 cast is unsafe when a qualifier is added at one level and const
4533 is not present at all outer levels.
4535 To issue this warning, we check at each level whether the cast
4536 adds new qualifiers not already seen. We don't need to special
4537 case function types, as they won't have the same
4538 TYPE_MAIN_VARIANT. */
4540 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4541 return;
4542 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4543 return;
4545 in_type = type;
4546 in_otype = otype;
4547 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4550 in_type = TREE_TYPE (in_type);
4551 in_otype = TREE_TYPE (in_otype);
4552 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4553 && !is_const)
4555 warning_at (loc, OPT_Wcast_qual,
4556 "to be safe all intermediate pointers in cast from "
4557 "%qT to %qT must be %<const%> qualified",
4558 otype, type);
4559 break;
4561 if (is_const)
4562 is_const = TYPE_READONLY (in_type);
4564 while (TREE_CODE (in_type) == POINTER_TYPE);
4567 /* Build an expression representing a cast to type TYPE of expression EXPR.
4568 LOC is the location of the cast-- typically the open paren of the cast. */
4570 tree
4571 build_c_cast (location_t loc, tree type, tree expr)
4573 tree value;
4575 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4576 expr = TREE_OPERAND (expr, 0);
4578 value = expr;
4580 if (type == error_mark_node || expr == error_mark_node)
4581 return error_mark_node;
4583 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4584 only in <protocol> qualifications. But when constructing cast expressions,
4585 the protocols do matter and must be kept around. */
4586 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4587 return build1 (NOP_EXPR, type, expr);
4589 type = TYPE_MAIN_VARIANT (type);
4591 if (TREE_CODE (type) == ARRAY_TYPE)
4593 error_at (loc, "cast specifies array type");
4594 return error_mark_node;
4597 if (TREE_CODE (type) == FUNCTION_TYPE)
4599 error_at (loc, "cast specifies function type");
4600 return error_mark_node;
4603 if (!VOID_TYPE_P (type))
4605 value = require_complete_type (value);
4606 if (value == error_mark_node)
4607 return error_mark_node;
4610 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4612 if (TREE_CODE (type) == RECORD_TYPE
4613 || TREE_CODE (type) == UNION_TYPE)
4614 pedwarn (loc, OPT_pedantic,
4615 "ISO C forbids casting nonscalar to the same type");
4617 else if (TREE_CODE (type) == UNION_TYPE)
4619 tree field;
4621 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4622 if (TREE_TYPE (field) != error_mark_node
4623 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4624 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4625 break;
4627 if (field)
4629 tree t;
4630 bool maybe_const = true;
4632 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4633 t = c_fully_fold (value, false, &maybe_const);
4634 t = build_constructor_single (type, field, t);
4635 if (!maybe_const)
4636 t = c_wrap_maybe_const (t, true);
4637 t = digest_init (loc, type, t,
4638 NULL_TREE, false, true, 0);
4639 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4640 return t;
4642 error_at (loc, "cast to union type from type not present in union");
4643 return error_mark_node;
4645 else
4647 tree otype, ovalue;
4649 if (type == void_type_node)
4651 tree t = build1 (CONVERT_EXPR, type, value);
4652 SET_EXPR_LOCATION (t, loc);
4653 return t;
4656 otype = TREE_TYPE (value);
4658 /* Optionally warn about potentially worrisome casts. */
4659 if (warn_cast_qual
4660 && TREE_CODE (type) == POINTER_TYPE
4661 && TREE_CODE (otype) == POINTER_TYPE)
4662 handle_warn_cast_qual (loc, type, otype);
4664 /* Warn about conversions between pointers to disjoint
4665 address spaces. */
4666 if (TREE_CODE (type) == POINTER_TYPE
4667 && TREE_CODE (otype) == POINTER_TYPE
4668 && !null_pointer_constant_p (value))
4670 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4671 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4672 addr_space_t as_common;
4674 if (!addr_space_superset (as_to, as_from, &as_common))
4676 if (ADDR_SPACE_GENERIC_P (as_from))
4677 warning_at (loc, 0, "cast to %s address space pointer "
4678 "from disjoint generic address space pointer",
4679 c_addr_space_name (as_to));
4681 else if (ADDR_SPACE_GENERIC_P (as_to))
4682 warning_at (loc, 0, "cast to generic address space pointer "
4683 "from disjoint %s address space pointer",
4684 c_addr_space_name (as_from));
4686 else
4687 warning_at (loc, 0, "cast to %s address space pointer "
4688 "from disjoint %s address space pointer",
4689 c_addr_space_name (as_to),
4690 c_addr_space_name (as_from));
4694 /* Warn about possible alignment problems. */
4695 if (STRICT_ALIGNMENT
4696 && TREE_CODE (type) == POINTER_TYPE
4697 && TREE_CODE (otype) == POINTER_TYPE
4698 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4699 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4700 /* Don't warn about opaque types, where the actual alignment
4701 restriction is unknown. */
4702 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4703 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4704 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4705 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4706 warning_at (loc, OPT_Wcast_align,
4707 "cast increases required alignment of target type");
4709 if (TREE_CODE (type) == INTEGER_TYPE
4710 && TREE_CODE (otype) == POINTER_TYPE
4711 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4712 /* Unlike conversion of integers to pointers, where the
4713 warning is disabled for converting constants because
4714 of cases such as SIG_*, warn about converting constant
4715 pointers to integers. In some cases it may cause unwanted
4716 sign extension, and a warning is appropriate. */
4717 warning_at (loc, OPT_Wpointer_to_int_cast,
4718 "cast from pointer to integer of different size");
4720 if (TREE_CODE (value) == CALL_EXPR
4721 && TREE_CODE (type) != TREE_CODE (otype))
4722 warning_at (loc, OPT_Wbad_function_cast,
4723 "cast from function call of type %qT "
4724 "to non-matching type %qT", otype, type);
4726 if (TREE_CODE (type) == POINTER_TYPE
4727 && TREE_CODE (otype) == INTEGER_TYPE
4728 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4729 /* Don't warn about converting any constant. */
4730 && !TREE_CONSTANT (value))
4731 warning_at (loc,
4732 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4733 "of different size");
4735 if (warn_strict_aliasing <= 2)
4736 strict_aliasing_warning (otype, type, expr);
4738 /* If pedantic, warn for conversions between function and object
4739 pointer types, except for converting a null pointer constant
4740 to function pointer type. */
4741 if (pedantic
4742 && TREE_CODE (type) == POINTER_TYPE
4743 && TREE_CODE (otype) == POINTER_TYPE
4744 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4745 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4746 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4747 "conversion of function pointer to object pointer type");
4749 if (pedantic
4750 && TREE_CODE (type) == POINTER_TYPE
4751 && TREE_CODE (otype) == POINTER_TYPE
4752 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4753 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4754 && !null_pointer_constant_p (value))
4755 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4756 "conversion of object pointer to function pointer type");
4758 ovalue = value;
4759 value = convert (type, value);
4761 /* Ignore any integer overflow caused by the cast. */
4762 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4764 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4766 if (!TREE_OVERFLOW (value))
4768 /* Avoid clobbering a shared constant. */
4769 value = copy_node (value);
4770 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4773 else if (TREE_OVERFLOW (value))
4774 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4775 value = build_int_cst_wide (TREE_TYPE (value),
4776 TREE_INT_CST_LOW (value),
4777 TREE_INT_CST_HIGH (value));
4781 /* Don't let a cast be an lvalue. */
4782 if (value == expr)
4783 value = non_lvalue_loc (loc, value);
4785 /* Don't allow the results of casting to floating-point or complex
4786 types be confused with actual constants, or casts involving
4787 integer and pointer types other than direct integer-to-integer
4788 and integer-to-pointer be confused with integer constant
4789 expressions and null pointer constants. */
4790 if (TREE_CODE (value) == REAL_CST
4791 || TREE_CODE (value) == COMPLEX_CST
4792 || (TREE_CODE (value) == INTEGER_CST
4793 && !((TREE_CODE (expr) == INTEGER_CST
4794 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4795 || TREE_CODE (expr) == REAL_CST
4796 || TREE_CODE (expr) == COMPLEX_CST)))
4797 value = build1 (NOP_EXPR, type, value);
4799 if (CAN_HAVE_LOCATION_P (value))
4800 SET_EXPR_LOCATION (value, loc);
4801 return value;
4804 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4805 location of the open paren of the cast, or the position of the cast
4806 expr. */
4807 tree
4808 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4810 tree type;
4811 tree type_expr = NULL_TREE;
4812 bool type_expr_const = true;
4813 tree ret;
4814 int saved_wsp = warn_strict_prototypes;
4816 /* This avoids warnings about unprototyped casts on
4817 integers. E.g. "#define SIG_DFL (void(*)())0". */
4818 if (TREE_CODE (expr) == INTEGER_CST)
4819 warn_strict_prototypes = 0;
4820 type = groktypename (type_name, &type_expr, &type_expr_const);
4821 warn_strict_prototypes = saved_wsp;
4823 ret = build_c_cast (loc, type, expr);
4824 if (type_expr)
4826 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4827 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4828 SET_EXPR_LOCATION (ret, loc);
4831 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4832 SET_EXPR_LOCATION (ret, loc);
4834 /* C++ does not permits types to be defined in a cast, but it
4835 allows references to incomplete types. */
4836 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4837 warning_at (loc, OPT_Wc___compat,
4838 "defining a type in a cast is invalid in C++");
4840 return ret;
4843 /* Build an assignment expression of lvalue LHS from value RHS.
4844 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4845 may differ from TREE_TYPE (LHS) for an enum bitfield.
4846 MODIFYCODE is the code for a binary operator that we use
4847 to combine the old value of LHS with RHS to get the new value.
4848 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4849 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4850 which may differ from TREE_TYPE (RHS) for an enum value.
4852 LOCATION is the location of the MODIFYCODE operator.
4853 RHS_LOC is the location of the RHS. */
4855 tree
4856 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4857 enum tree_code modifycode,
4858 location_t rhs_loc, tree rhs, tree rhs_origtype)
4860 tree result;
4861 tree newrhs;
4862 tree rhs_semantic_type = NULL_TREE;
4863 tree lhstype = TREE_TYPE (lhs);
4864 tree olhstype = lhstype;
4865 bool npc;
4867 /* Types that aren't fully specified cannot be used in assignments. */
4868 lhs = require_complete_type (lhs);
4870 /* Avoid duplicate error messages from operands that had errors. */
4871 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4872 return error_mark_node;
4874 /* For ObjC properties, defer this check. */
4875 if (!objc_is_property_ref (lhs) && !lvalue_or_else (lhs, lv_assign))
4876 return error_mark_node;
4878 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4880 rhs_semantic_type = TREE_TYPE (rhs);
4881 rhs = TREE_OPERAND (rhs, 0);
4884 newrhs = rhs;
4886 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4888 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4889 lhs_origtype, modifycode, rhs_loc, rhs,
4890 rhs_origtype);
4891 if (inner == error_mark_node)
4892 return error_mark_node;
4893 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4894 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4895 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4896 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4897 protected_set_expr_location (result, location);
4898 return result;
4901 /* If a binary op has been requested, combine the old LHS value with the RHS
4902 producing the value we should actually store into the LHS. */
4904 if (modifycode != NOP_EXPR)
4906 lhs = c_fully_fold (lhs, false, NULL);
4907 lhs = stabilize_reference (lhs);
4908 newrhs = build_binary_op (location,
4909 modifycode, lhs, rhs, 1);
4911 /* The original type of the right hand side is no longer
4912 meaningful. */
4913 rhs_origtype = NULL_TREE;
4916 if (c_dialect_objc ())
4918 /* Check if we are modifying an Objective-C property reference;
4919 if so, we need to generate setter calls. */
4920 result = objc_maybe_build_modify_expr (lhs, newrhs);
4921 if (result)
4922 return result;
4924 /* Else, do the check that we postponed for Objective-C. */
4925 if (!lvalue_or_else (lhs, lv_assign))
4926 return error_mark_node;
4929 /* Give an error for storing in something that is 'const'. */
4931 if (TYPE_READONLY (lhstype)
4932 || ((TREE_CODE (lhstype) == RECORD_TYPE
4933 || TREE_CODE (lhstype) == UNION_TYPE)
4934 && C_TYPE_FIELDS_READONLY (lhstype)))
4936 readonly_error (lhs, lv_assign);
4937 return error_mark_node;
4939 else if (TREE_READONLY (lhs))
4940 readonly_warning (lhs, lv_assign);
4942 /* If storing into a structure or union member,
4943 it has probably been given type `int'.
4944 Compute the type that would go with
4945 the actual amount of storage the member occupies. */
4947 if (TREE_CODE (lhs) == COMPONENT_REF
4948 && (TREE_CODE (lhstype) == INTEGER_TYPE
4949 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4950 || TREE_CODE (lhstype) == REAL_TYPE
4951 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4952 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4954 /* If storing in a field that is in actuality a short or narrower than one,
4955 we must store in the field in its actual type. */
4957 if (lhstype != TREE_TYPE (lhs))
4959 lhs = copy_node (lhs);
4960 TREE_TYPE (lhs) = lhstype;
4963 /* Issue -Wc++-compat warnings about an assignment to an enum type
4964 when LHS does not have its original type. This happens for,
4965 e.g., an enum bitfield in a struct. */
4966 if (warn_cxx_compat
4967 && lhs_origtype != NULL_TREE
4968 && lhs_origtype != lhstype
4969 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4971 tree checktype = (rhs_origtype != NULL_TREE
4972 ? rhs_origtype
4973 : TREE_TYPE (rhs));
4974 if (checktype != error_mark_node
4975 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4976 warning_at (location, OPT_Wc___compat,
4977 "enum conversion in assignment is invalid in C++");
4980 /* Convert new value to destination type. Fold it first, then
4981 restore any excess precision information, for the sake of
4982 conversion warnings. */
4984 npc = null_pointer_constant_p (newrhs);
4985 newrhs = c_fully_fold (newrhs, false, NULL);
4986 if (rhs_semantic_type)
4987 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4988 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4989 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4990 if (TREE_CODE (newrhs) == ERROR_MARK)
4991 return error_mark_node;
4993 /* Emit ObjC write barrier, if necessary. */
4994 if (c_dialect_objc () && flag_objc_gc)
4996 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4997 if (result)
4999 protected_set_expr_location (result, location);
5000 return result;
5004 /* Scan operands. */
5006 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5007 TREE_SIDE_EFFECTS (result) = 1;
5008 protected_set_expr_location (result, location);
5010 /* If we got the LHS in a different type for storing in,
5011 convert the result back to the nominal type of LHS
5012 so that the value we return always has the same type
5013 as the LHS argument. */
5015 if (olhstype == TREE_TYPE (result))
5016 return result;
5018 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5019 ic_assign, false, NULL_TREE, NULL_TREE, 0);
5020 protected_set_expr_location (result, location);
5021 return result;
5024 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5025 This is used to implement -fplan9-extensions. */
5027 static bool
5028 find_anonymous_field_with_type (tree struct_type, tree type)
5030 tree field;
5031 bool found;
5033 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5034 || TREE_CODE (struct_type) == UNION_TYPE);
5035 found = false;
5036 for (field = TYPE_FIELDS (struct_type);
5037 field != NULL_TREE;
5038 field = TREE_CHAIN (field))
5040 if (DECL_NAME (field) == NULL
5041 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5043 if (found)
5044 return false;
5045 found = true;
5047 else if (DECL_NAME (field) == NULL
5048 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5049 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5050 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5052 if (found)
5053 return false;
5054 found = true;
5057 return found;
5060 /* RHS is an expression whose type is pointer to struct. If there is
5061 an anonymous field in RHS with type TYPE, then return a pointer to
5062 that field in RHS. This is used with -fplan9-extensions. This
5063 returns NULL if no conversion could be found. */
5065 static tree
5066 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5068 tree rhs_struct_type, lhs_main_type;
5069 tree field, found_field;
5070 bool found_sub_field;
5071 tree ret;
5073 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5074 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5075 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5076 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5078 gcc_assert (POINTER_TYPE_P (type));
5079 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5081 found_field = NULL_TREE;
5082 found_sub_field = false;
5083 for (field = TYPE_FIELDS (rhs_struct_type);
5084 field != NULL_TREE;
5085 field = TREE_CHAIN (field))
5087 if (DECL_NAME (field) != NULL_TREE
5088 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5089 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5090 continue;
5091 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5093 if (found_field != NULL_TREE)
5094 return NULL_TREE;
5095 found_field = field;
5097 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5098 lhs_main_type))
5100 if (found_field != NULL_TREE)
5101 return NULL_TREE;
5102 found_field = field;
5103 found_sub_field = true;
5107 if (found_field == NULL_TREE)
5108 return NULL_TREE;
5110 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5111 build_fold_indirect_ref (rhs), found_field,
5112 NULL_TREE);
5113 ret = build_fold_addr_expr_loc (location, ret);
5115 if (found_sub_field)
5117 ret = convert_to_anonymous_field (location, type, ret);
5118 gcc_assert (ret != NULL_TREE);
5121 return ret;
5124 /* Convert value RHS to type TYPE as preparation for an assignment to
5125 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5126 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5127 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5128 constant before any folding.
5129 The real work of conversion is done by `convert'.
5130 The purpose of this function is to generate error messages
5131 for assignments that are not allowed in C.
5132 ERRTYPE says whether it is argument passing, assignment,
5133 initialization or return.
5135 LOCATION is the location of the RHS.
5136 FUNCTION is a tree for the function being called.
5137 PARMNUM is the number of the argument, for printing in error messages. */
5139 static tree
5140 convert_for_assignment (location_t location, tree type, tree rhs,
5141 tree origtype, enum impl_conv errtype,
5142 bool null_pointer_constant, tree fundecl,
5143 tree function, int parmnum)
5145 enum tree_code codel = TREE_CODE (type);
5146 tree orig_rhs = rhs;
5147 tree rhstype;
5148 enum tree_code coder;
5149 tree rname = NULL_TREE;
5150 bool objc_ok = false;
5152 if (errtype == ic_argpass)
5154 tree selector;
5155 /* Change pointer to function to the function itself for
5156 diagnostics. */
5157 if (TREE_CODE (function) == ADDR_EXPR
5158 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5159 function = TREE_OPERAND (function, 0);
5161 /* Handle an ObjC selector specially for diagnostics. */
5162 selector = objc_message_selector ();
5163 rname = function;
5164 if (selector && parmnum > 2)
5166 rname = selector;
5167 parmnum -= 2;
5171 /* This macro is used to emit diagnostics to ensure that all format
5172 strings are complete sentences, visible to gettext and checked at
5173 compile time. */
5174 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5175 do { \
5176 switch (errtype) \
5178 case ic_argpass: \
5179 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5180 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5181 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5182 "expected %qT but argument is of type %qT", \
5183 type, rhstype); \
5184 break; \
5185 case ic_assign: \
5186 pedwarn (LOCATION, OPT, AS); \
5187 break; \
5188 case ic_init: \
5189 pedwarn_init (LOCATION, OPT, IN); \
5190 break; \
5191 case ic_return: \
5192 pedwarn (LOCATION, OPT, RE); \
5193 break; \
5194 default: \
5195 gcc_unreachable (); \
5197 } while (0)
5199 /* This macro is used to emit diagnostics to ensure that all format
5200 strings are complete sentences, visible to gettext and checked at
5201 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5202 extra parameter to enumerate qualifiers. */
5204 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5205 do { \
5206 switch (errtype) \
5208 case ic_argpass: \
5209 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5210 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5211 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5212 "expected %qT but argument is of type %qT", \
5213 type, rhstype); \
5214 break; \
5215 case ic_assign: \
5216 pedwarn (LOCATION, OPT, AS, QUALS); \
5217 break; \
5218 case ic_init: \
5219 pedwarn (LOCATION, OPT, IN, QUALS); \
5220 break; \
5221 case ic_return: \
5222 pedwarn (LOCATION, OPT, RE, QUALS); \
5223 break; \
5224 default: \
5225 gcc_unreachable (); \
5227 } while (0)
5229 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5230 rhs = TREE_OPERAND (rhs, 0);
5232 rhstype = TREE_TYPE (rhs);
5233 coder = TREE_CODE (rhstype);
5235 if (coder == ERROR_MARK)
5236 return error_mark_node;
5238 if (c_dialect_objc ())
5240 int parmno;
5242 switch (errtype)
5244 case ic_return:
5245 parmno = 0;
5246 break;
5248 case ic_assign:
5249 parmno = -1;
5250 break;
5252 case ic_init:
5253 parmno = -2;
5254 break;
5256 default:
5257 parmno = parmnum;
5258 break;
5261 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5264 if (warn_cxx_compat)
5266 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5267 if (checktype != error_mark_node
5268 && TREE_CODE (type) == ENUMERAL_TYPE
5269 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5271 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5272 G_("enum conversion when passing argument "
5273 "%d of %qE is invalid in C++"),
5274 G_("enum conversion in assignment is "
5275 "invalid in C++"),
5276 G_("enum conversion in initialization is "
5277 "invalid in C++"),
5278 G_("enum conversion in return is "
5279 "invalid in C++"));
5283 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5284 return rhs;
5286 if (coder == VOID_TYPE)
5288 /* Except for passing an argument to an unprototyped function,
5289 this is a constraint violation. When passing an argument to
5290 an unprototyped function, it is compile-time undefined;
5291 making it a constraint in that case was rejected in
5292 DR#252. */
5293 error_at (location, "void value not ignored as it ought to be");
5294 return error_mark_node;
5296 rhs = require_complete_type (rhs);
5297 if (rhs == error_mark_node)
5298 return error_mark_node;
5299 /* A type converts to a reference to it.
5300 This code doesn't fully support references, it's just for the
5301 special case of va_start and va_copy. */
5302 if (codel == REFERENCE_TYPE
5303 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5305 if (!lvalue_p (rhs))
5307 error_at (location, "cannot pass rvalue to reference parameter");
5308 return error_mark_node;
5310 if (!c_mark_addressable (rhs))
5311 return error_mark_node;
5312 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5313 SET_EXPR_LOCATION (rhs, location);
5315 /* We already know that these two types are compatible, but they
5316 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5317 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5318 likely to be va_list, a typedef to __builtin_va_list, which
5319 is different enough that it will cause problems later. */
5320 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5322 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5323 SET_EXPR_LOCATION (rhs, location);
5326 rhs = build1 (NOP_EXPR, type, rhs);
5327 SET_EXPR_LOCATION (rhs, location);
5328 return rhs;
5330 /* Some types can interconvert without explicit casts. */
5331 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5332 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5333 return convert (type, rhs);
5334 /* Arithmetic types all interconvert, and enum is treated like int. */
5335 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5336 || codel == FIXED_POINT_TYPE
5337 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5338 || codel == BOOLEAN_TYPE)
5339 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5340 || coder == FIXED_POINT_TYPE
5341 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5342 || coder == BOOLEAN_TYPE))
5344 tree ret;
5345 bool save = in_late_binary_op;
5346 if (codel == BOOLEAN_TYPE)
5347 in_late_binary_op = true;
5348 ret = convert_and_check (type, orig_rhs);
5349 if (codel == BOOLEAN_TYPE)
5350 in_late_binary_op = save;
5351 return ret;
5354 /* Aggregates in different TUs might need conversion. */
5355 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5356 && codel == coder
5357 && comptypes (type, rhstype))
5358 return convert_and_check (type, rhs);
5360 /* Conversion to a transparent union or record from its member types.
5361 This applies only to function arguments. */
5362 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5363 && TYPE_TRANSPARENT_AGGR (type))
5364 && errtype == ic_argpass)
5366 tree memb, marginal_memb = NULL_TREE;
5368 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5370 tree memb_type = TREE_TYPE (memb);
5372 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5373 TYPE_MAIN_VARIANT (rhstype)))
5374 break;
5376 if (TREE_CODE (memb_type) != POINTER_TYPE)
5377 continue;
5379 if (coder == POINTER_TYPE)
5381 tree ttl = TREE_TYPE (memb_type);
5382 tree ttr = TREE_TYPE (rhstype);
5384 /* Any non-function converts to a [const][volatile] void *
5385 and vice versa; otherwise, targets must be the same.
5386 Meanwhile, the lhs target must have all the qualifiers of
5387 the rhs. */
5388 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5389 || comp_target_types (location, memb_type, rhstype))
5391 /* If this type won't generate any warnings, use it. */
5392 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5393 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5394 && TREE_CODE (ttl) == FUNCTION_TYPE)
5395 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5396 == TYPE_QUALS (ttr))
5397 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5398 == TYPE_QUALS (ttl))))
5399 break;
5401 /* Keep looking for a better type, but remember this one. */
5402 if (!marginal_memb)
5403 marginal_memb = memb;
5407 /* Can convert integer zero to any pointer type. */
5408 if (null_pointer_constant)
5410 rhs = null_pointer_node;
5411 break;
5415 if (memb || marginal_memb)
5417 if (!memb)
5419 /* We have only a marginally acceptable member type;
5420 it needs a warning. */
5421 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5422 tree ttr = TREE_TYPE (rhstype);
5424 /* Const and volatile mean something different for function
5425 types, so the usual warnings are not appropriate. */
5426 if (TREE_CODE (ttr) == FUNCTION_TYPE
5427 && TREE_CODE (ttl) == FUNCTION_TYPE)
5429 /* Because const and volatile on functions are
5430 restrictions that say the function will not do
5431 certain things, it is okay to use a const or volatile
5432 function where an ordinary one is wanted, but not
5433 vice-versa. */
5434 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5435 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5436 WARN_FOR_QUALIFIERS (location, 0,
5437 G_("passing argument %d of %qE "
5438 "makes %q#v qualified function "
5439 "pointer from unqualified"),
5440 G_("assignment makes %q#v qualified "
5441 "function pointer from "
5442 "unqualified"),
5443 G_("initialization makes %q#v qualified "
5444 "function pointer from "
5445 "unqualified"),
5446 G_("return makes %q#v qualified function "
5447 "pointer from unqualified"),
5448 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5450 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5451 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5452 WARN_FOR_QUALIFIERS (location, 0,
5453 G_("passing argument %d of %qE discards "
5454 "%qv qualifier from pointer target type"),
5455 G_("assignment discards %qv qualifier "
5456 "from pointer target type"),
5457 G_("initialization discards %qv qualifier "
5458 "from pointer target type"),
5459 G_("return discards %qv qualifier from "
5460 "pointer target type"),
5461 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5463 memb = marginal_memb;
5466 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5467 pedwarn (location, OPT_pedantic,
5468 "ISO C prohibits argument conversion to union type");
5470 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5471 return build_constructor_single (type, memb, rhs);
5475 /* Conversions among pointers */
5476 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5477 && (coder == codel))
5479 tree ttl = TREE_TYPE (type);
5480 tree ttr = TREE_TYPE (rhstype);
5481 tree mvl = ttl;
5482 tree mvr = ttr;
5483 bool is_opaque_pointer;
5484 int target_cmp = 0; /* Cache comp_target_types () result. */
5485 addr_space_t asl;
5486 addr_space_t asr;
5488 if (TREE_CODE (mvl) != ARRAY_TYPE)
5489 mvl = TYPE_MAIN_VARIANT (mvl);
5490 if (TREE_CODE (mvr) != ARRAY_TYPE)
5491 mvr = TYPE_MAIN_VARIANT (mvr);
5492 /* Opaque pointers are treated like void pointers. */
5493 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5495 /* The Plan 9 compiler permits a pointer to a struct to be
5496 automatically converted into a pointer to an anonymous field
5497 within the struct. */
5498 if (flag_plan9_extensions
5499 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5500 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5501 && mvl != mvr)
5503 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5504 if (new_rhs != NULL_TREE)
5506 rhs = new_rhs;
5507 rhstype = TREE_TYPE (rhs);
5508 coder = TREE_CODE (rhstype);
5509 ttr = TREE_TYPE (rhstype);
5510 mvr = TYPE_MAIN_VARIANT (ttr);
5514 /* C++ does not allow the implicit conversion void* -> T*. However,
5515 for the purpose of reducing the number of false positives, we
5516 tolerate the special case of
5518 int *p = NULL;
5520 where NULL is typically defined in C to be '(void *) 0'. */
5521 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5522 warning_at (location, OPT_Wc___compat,
5523 "request for implicit conversion "
5524 "from %qT to %qT not permitted in C++", rhstype, type);
5526 /* See if the pointers point to incompatible address spaces. */
5527 asl = TYPE_ADDR_SPACE (ttl);
5528 asr = TYPE_ADDR_SPACE (ttr);
5529 if (!null_pointer_constant_p (rhs)
5530 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5532 switch (errtype)
5534 case ic_argpass:
5535 error_at (location, "passing argument %d of %qE from pointer to "
5536 "non-enclosed address space", parmnum, rname);
5537 break;
5538 case ic_assign:
5539 error_at (location, "assignment from pointer to "
5540 "non-enclosed address space");
5541 break;
5542 case ic_init:
5543 error_at (location, "initialization from pointer to "
5544 "non-enclosed address space");
5545 break;
5546 case ic_return:
5547 error_at (location, "return from pointer to "
5548 "non-enclosed address space");
5549 break;
5550 default:
5551 gcc_unreachable ();
5553 return error_mark_node;
5556 /* Check if the right-hand side has a format attribute but the
5557 left-hand side doesn't. */
5558 if (warn_missing_format_attribute
5559 && check_missing_format_attribute (type, rhstype))
5561 switch (errtype)
5563 case ic_argpass:
5564 warning_at (location, OPT_Wmissing_format_attribute,
5565 "argument %d of %qE might be "
5566 "a candidate for a format attribute",
5567 parmnum, rname);
5568 break;
5569 case ic_assign:
5570 warning_at (location, OPT_Wmissing_format_attribute,
5571 "assignment left-hand side might be "
5572 "a candidate for a format attribute");
5573 break;
5574 case ic_init:
5575 warning_at (location, OPT_Wmissing_format_attribute,
5576 "initialization left-hand side might be "
5577 "a candidate for a format attribute");
5578 break;
5579 case ic_return:
5580 warning_at (location, OPT_Wmissing_format_attribute,
5581 "return type might be "
5582 "a candidate for a format attribute");
5583 break;
5584 default:
5585 gcc_unreachable ();
5589 /* Any non-function converts to a [const][volatile] void *
5590 and vice versa; otherwise, targets must be the same.
5591 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5592 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5593 || (target_cmp = comp_target_types (location, type, rhstype))
5594 || is_opaque_pointer
5595 || (c_common_unsigned_type (mvl)
5596 == c_common_unsigned_type (mvr)))
5598 if (pedantic
5599 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5601 (VOID_TYPE_P (ttr)
5602 && !null_pointer_constant
5603 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5604 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5605 G_("ISO C forbids passing argument %d of "
5606 "%qE between function pointer "
5607 "and %<void *%>"),
5608 G_("ISO C forbids assignment between "
5609 "function pointer and %<void *%>"),
5610 G_("ISO C forbids initialization between "
5611 "function pointer and %<void *%>"),
5612 G_("ISO C forbids return between function "
5613 "pointer and %<void *%>"));
5614 /* Const and volatile mean something different for function types,
5615 so the usual warnings are not appropriate. */
5616 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5617 && TREE_CODE (ttl) != FUNCTION_TYPE)
5619 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5620 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5622 /* Types differing only by the presence of the 'volatile'
5623 qualifier are acceptable if the 'volatile' has been added
5624 in by the Objective-C EH machinery. */
5625 if (!objc_type_quals_match (ttl, ttr))
5626 WARN_FOR_QUALIFIERS (location, 0,
5627 G_("passing argument %d of %qE discards "
5628 "%qv qualifier from pointer target type"),
5629 G_("assignment discards %qv qualifier "
5630 "from pointer target type"),
5631 G_("initialization discards %qv qualifier "
5632 "from pointer target type"),
5633 G_("return discards %qv qualifier from "
5634 "pointer target type"),
5635 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5637 /* If this is not a case of ignoring a mismatch in signedness,
5638 no warning. */
5639 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5640 || target_cmp)
5642 /* If there is a mismatch, do warn. */
5643 else if (warn_pointer_sign)
5644 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5645 G_("pointer targets in passing argument "
5646 "%d of %qE differ in signedness"),
5647 G_("pointer targets in assignment "
5648 "differ in signedness"),
5649 G_("pointer targets in initialization "
5650 "differ in signedness"),
5651 G_("pointer targets in return differ "
5652 "in signedness"));
5654 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5655 && TREE_CODE (ttr) == FUNCTION_TYPE)
5657 /* Because const and volatile on functions are restrictions
5658 that say the function will not do certain things,
5659 it is okay to use a const or volatile function
5660 where an ordinary one is wanted, but not vice-versa. */
5661 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5662 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5663 WARN_FOR_QUALIFIERS (location, 0,
5664 G_("passing argument %d of %qE makes "
5665 "%q#v qualified function pointer "
5666 "from unqualified"),
5667 G_("assignment makes %q#v qualified function "
5668 "pointer from unqualified"),
5669 G_("initialization makes %q#v qualified "
5670 "function pointer from unqualified"),
5671 G_("return makes %q#v qualified function "
5672 "pointer from unqualified"),
5673 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5676 else
5677 /* Avoid warning about the volatile ObjC EH puts on decls. */
5678 if (!objc_ok)
5679 WARN_FOR_ASSIGNMENT (location, 0,
5680 G_("passing argument %d of %qE from "
5681 "incompatible pointer type"),
5682 G_("assignment from incompatible pointer type"),
5683 G_("initialization from incompatible "
5684 "pointer type"),
5685 G_("return from incompatible pointer type"));
5687 return convert (type, rhs);
5689 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5691 /* ??? This should not be an error when inlining calls to
5692 unprototyped functions. */
5693 error_at (location, "invalid use of non-lvalue array");
5694 return error_mark_node;
5696 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5698 /* An explicit constant 0 can convert to a pointer,
5699 or one that results from arithmetic, even including
5700 a cast to integer type. */
5701 if (!null_pointer_constant)
5702 WARN_FOR_ASSIGNMENT (location, 0,
5703 G_("passing argument %d of %qE makes "
5704 "pointer from integer without a cast"),
5705 G_("assignment makes pointer from integer "
5706 "without a cast"),
5707 G_("initialization makes pointer from "
5708 "integer without a cast"),
5709 G_("return makes pointer from integer "
5710 "without a cast"));
5712 return convert (type, rhs);
5714 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5716 WARN_FOR_ASSIGNMENT (location, 0,
5717 G_("passing argument %d of %qE makes integer "
5718 "from pointer without a cast"),
5719 G_("assignment makes integer from pointer "
5720 "without a cast"),
5721 G_("initialization makes integer from pointer "
5722 "without a cast"),
5723 G_("return makes integer from pointer "
5724 "without a cast"));
5725 return convert (type, rhs);
5727 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5729 tree ret;
5730 bool save = in_late_binary_op;
5731 in_late_binary_op = true;
5732 ret = convert (type, rhs);
5733 in_late_binary_op = save;
5734 return ret;
5737 switch (errtype)
5739 case ic_argpass:
5740 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5741 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5742 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5743 "expected %qT but argument is of type %qT", type, rhstype);
5744 break;
5745 case ic_assign:
5746 error_at (location, "incompatible types when assigning to type %qT from "
5747 "type %qT", type, rhstype);
5748 break;
5749 case ic_init:
5750 error_at (location,
5751 "incompatible types when initializing type %qT using type %qT",
5752 type, rhstype);
5753 break;
5754 case ic_return:
5755 error_at (location,
5756 "incompatible types when returning type %qT but %qT was "
5757 "expected", rhstype, type);
5758 break;
5759 default:
5760 gcc_unreachable ();
5763 return error_mark_node;
5766 /* If VALUE is a compound expr all of whose expressions are constant, then
5767 return its value. Otherwise, return error_mark_node.
5769 This is for handling COMPOUND_EXPRs as initializer elements
5770 which is allowed with a warning when -pedantic is specified. */
5772 static tree
5773 valid_compound_expr_initializer (tree value, tree endtype)
5775 if (TREE_CODE (value) == COMPOUND_EXPR)
5777 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5778 == error_mark_node)
5779 return error_mark_node;
5780 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5781 endtype);
5783 else if (!initializer_constant_valid_p (value, endtype))
5784 return error_mark_node;
5785 else
5786 return value;
5789 /* Perform appropriate conversions on the initial value of a variable,
5790 store it in the declaration DECL,
5791 and print any error messages that are appropriate.
5792 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5793 If the init is invalid, store an ERROR_MARK.
5795 INIT_LOC is the location of the initial value. */
5797 void
5798 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5800 tree value, type;
5801 bool npc = false;
5803 /* If variable's type was invalidly declared, just ignore it. */
5805 type = TREE_TYPE (decl);
5806 if (TREE_CODE (type) == ERROR_MARK)
5807 return;
5809 /* Digest the specified initializer into an expression. */
5811 if (init)
5812 npc = null_pointer_constant_p (init);
5813 value = digest_init (init_loc, type, init, origtype, npc,
5814 true, TREE_STATIC (decl));
5816 /* Store the expression if valid; else report error. */
5818 if (!in_system_header
5819 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5820 warning (OPT_Wtraditional, "traditional C rejects automatic "
5821 "aggregate initialization");
5823 DECL_INITIAL (decl) = value;
5825 /* ANSI wants warnings about out-of-range constant initializers. */
5826 STRIP_TYPE_NOPS (value);
5827 if (TREE_STATIC (decl))
5828 constant_expression_warning (value);
5830 /* Check if we need to set array size from compound literal size. */
5831 if (TREE_CODE (type) == ARRAY_TYPE
5832 && TYPE_DOMAIN (type) == 0
5833 && value != error_mark_node)
5835 tree inside_init = init;
5837 STRIP_TYPE_NOPS (inside_init);
5838 inside_init = fold (inside_init);
5840 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5842 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5844 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5846 /* For int foo[] = (int [3]){1}; we need to set array size
5847 now since later on array initializer will be just the
5848 brace enclosed list of the compound literal. */
5849 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5850 TREE_TYPE (decl) = type;
5851 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5852 layout_type (type);
5853 layout_decl (cldecl, 0);
5859 /* Methods for storing and printing names for error messages. */
5861 /* Implement a spelling stack that allows components of a name to be pushed
5862 and popped. Each element on the stack is this structure. */
5864 struct spelling
5866 int kind;
5867 union
5869 unsigned HOST_WIDE_INT i;
5870 const char *s;
5871 } u;
5874 #define SPELLING_STRING 1
5875 #define SPELLING_MEMBER 2
5876 #define SPELLING_BOUNDS 3
5878 static struct spelling *spelling; /* Next stack element (unused). */
5879 static struct spelling *spelling_base; /* Spelling stack base. */
5880 static int spelling_size; /* Size of the spelling stack. */
5882 /* Macros to save and restore the spelling stack around push_... functions.
5883 Alternative to SAVE_SPELLING_STACK. */
5885 #define SPELLING_DEPTH() (spelling - spelling_base)
5886 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5888 /* Push an element on the spelling stack with type KIND and assign VALUE
5889 to MEMBER. */
5891 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5893 int depth = SPELLING_DEPTH (); \
5895 if (depth >= spelling_size) \
5897 spelling_size += 10; \
5898 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5899 spelling_size); \
5900 RESTORE_SPELLING_DEPTH (depth); \
5903 spelling->kind = (KIND); \
5904 spelling->MEMBER = (VALUE); \
5905 spelling++; \
5908 /* Push STRING on the stack. Printed literally. */
5910 static void
5911 push_string (const char *string)
5913 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5916 /* Push a member name on the stack. Printed as '.' STRING. */
5918 static void
5919 push_member_name (tree decl)
5921 const char *const string
5922 = (DECL_NAME (decl)
5923 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5924 : _("<anonymous>"));
5925 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5928 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5930 static void
5931 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5933 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5936 /* Compute the maximum size in bytes of the printed spelling. */
5938 static int
5939 spelling_length (void)
5941 int size = 0;
5942 struct spelling *p;
5944 for (p = spelling_base; p < spelling; p++)
5946 if (p->kind == SPELLING_BOUNDS)
5947 size += 25;
5948 else
5949 size += strlen (p->u.s) + 1;
5952 return size;
5955 /* Print the spelling to BUFFER and return it. */
5957 static char *
5958 print_spelling (char *buffer)
5960 char *d = buffer;
5961 struct spelling *p;
5963 for (p = spelling_base; p < spelling; p++)
5964 if (p->kind == SPELLING_BOUNDS)
5966 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5967 d += strlen (d);
5969 else
5971 const char *s;
5972 if (p->kind == SPELLING_MEMBER)
5973 *d++ = '.';
5974 for (s = p->u.s; (*d = *s++); d++)
5977 *d++ = '\0';
5978 return buffer;
5981 /* Issue an error message for a bad initializer component.
5982 GMSGID identifies the message.
5983 The component name is taken from the spelling stack. */
5985 void
5986 error_init (const char *gmsgid)
5988 char *ofwhat;
5990 /* The gmsgid may be a format string with %< and %>. */
5991 error (gmsgid);
5992 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5993 if (*ofwhat)
5994 error ("(near initialization for %qs)", ofwhat);
5997 /* Issue a pedantic warning for a bad initializer component. OPT is
5998 the option OPT_* (from options.h) controlling this warning or 0 if
5999 it is unconditionally given. GMSGID identifies the message. The
6000 component name is taken from the spelling stack. */
6002 void
6003 pedwarn_init (location_t location, int opt, const char *gmsgid)
6005 char *ofwhat;
6007 /* The gmsgid may be a format string with %< and %>. */
6008 pedwarn (location, opt, gmsgid);
6009 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6010 if (*ofwhat)
6011 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6014 /* Issue a warning for a bad initializer component.
6016 OPT is the OPT_W* value corresponding to the warning option that
6017 controls this warning. GMSGID identifies the message. The
6018 component name is taken from the spelling stack. */
6020 static void
6021 warning_init (int opt, const char *gmsgid)
6023 char *ofwhat;
6025 /* The gmsgid may be a format string with %< and %>. */
6026 warning (opt, gmsgid);
6027 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6028 if (*ofwhat)
6029 warning (opt, "(near initialization for %qs)", ofwhat);
6032 /* If TYPE is an array type and EXPR is a parenthesized string
6033 constant, warn if pedantic that EXPR is being used to initialize an
6034 object of type TYPE. */
6036 void
6037 maybe_warn_string_init (tree type, struct c_expr expr)
6039 if (pedantic
6040 && TREE_CODE (type) == ARRAY_TYPE
6041 && TREE_CODE (expr.value) == STRING_CST
6042 && expr.original_code != STRING_CST)
6043 pedwarn_init (input_location, OPT_pedantic,
6044 "array initialized from parenthesized string constant");
6047 /* Digest the parser output INIT as an initializer for type TYPE.
6048 Return a C expression of type TYPE to represent the initial value.
6050 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6052 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6054 If INIT is a string constant, STRICT_STRING is true if it is
6055 unparenthesized or we should not warn here for it being parenthesized.
6056 For other types of INIT, STRICT_STRING is not used.
6058 INIT_LOC is the location of the INIT.
6060 REQUIRE_CONSTANT requests an error if non-constant initializers or
6061 elements are seen. */
6063 static tree
6064 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6065 bool null_pointer_constant, bool strict_string,
6066 int require_constant)
6068 enum tree_code code = TREE_CODE (type);
6069 tree inside_init = init;
6070 tree semantic_type = NULL_TREE;
6071 bool maybe_const = true;
6073 if (type == error_mark_node
6074 || !init
6075 || init == error_mark_node
6076 || TREE_TYPE (init) == error_mark_node)
6077 return error_mark_node;
6079 STRIP_TYPE_NOPS (inside_init);
6081 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6083 semantic_type = TREE_TYPE (inside_init);
6084 inside_init = TREE_OPERAND (inside_init, 0);
6086 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6087 inside_init = decl_constant_value_for_optimization (inside_init);
6089 /* Initialization of an array of chars from a string constant
6090 optionally enclosed in braces. */
6092 if (code == ARRAY_TYPE && inside_init
6093 && TREE_CODE (inside_init) == STRING_CST)
6095 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6096 /* Note that an array could be both an array of character type
6097 and an array of wchar_t if wchar_t is signed char or unsigned
6098 char. */
6099 bool char_array = (typ1 == char_type_node
6100 || typ1 == signed_char_type_node
6101 || typ1 == unsigned_char_type_node);
6102 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6103 bool char16_array = !!comptypes (typ1, char16_type_node);
6104 bool char32_array = !!comptypes (typ1, char32_type_node);
6106 if (char_array || wchar_array || char16_array || char32_array)
6108 struct c_expr expr;
6109 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6110 expr.value = inside_init;
6111 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6112 expr.original_type = NULL;
6113 maybe_warn_string_init (type, expr);
6115 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6116 pedwarn_init (init_loc, OPT_pedantic,
6117 "initialization of a flexible array member");
6119 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6120 TYPE_MAIN_VARIANT (type)))
6121 return inside_init;
6123 if (char_array)
6125 if (typ2 != char_type_node)
6127 error_init ("char-array initialized from wide string");
6128 return error_mark_node;
6131 else
6133 if (typ2 == char_type_node)
6135 error_init ("wide character array initialized from non-wide "
6136 "string");
6137 return error_mark_node;
6139 else if (!comptypes(typ1, typ2))
6141 error_init ("wide character array initialized from "
6142 "incompatible wide string");
6143 return error_mark_node;
6147 TREE_TYPE (inside_init) = type;
6148 if (TYPE_DOMAIN (type) != 0
6149 && TYPE_SIZE (type) != 0
6150 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6152 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6154 /* Subtract the size of a single (possibly wide) character
6155 because it's ok to ignore the terminating null char
6156 that is counted in the length of the constant. */
6157 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6158 (len
6159 - (TYPE_PRECISION (typ1)
6160 / BITS_PER_UNIT))))
6161 pedwarn_init (init_loc, 0,
6162 ("initializer-string for array of chars "
6163 "is too long"));
6164 else if (warn_cxx_compat
6165 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6166 warning_at (init_loc, OPT_Wc___compat,
6167 ("initializer-string for array chars "
6168 "is too long for C++"));
6171 return inside_init;
6173 else if (INTEGRAL_TYPE_P (typ1))
6175 error_init ("array of inappropriate type initialized "
6176 "from string constant");
6177 return error_mark_node;
6181 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6182 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6183 below and handle as a constructor. */
6184 if (code == VECTOR_TYPE
6185 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6186 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6187 && TREE_CONSTANT (inside_init))
6189 if (TREE_CODE (inside_init) == VECTOR_CST
6190 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6191 TYPE_MAIN_VARIANT (type)))
6192 return inside_init;
6194 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6196 unsigned HOST_WIDE_INT ix;
6197 tree value;
6198 bool constant_p = true;
6200 /* Iterate through elements and check if all constructor
6201 elements are *_CSTs. */
6202 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6203 if (!CONSTANT_CLASS_P (value))
6205 constant_p = false;
6206 break;
6209 if (constant_p)
6210 return build_vector_from_ctor (type,
6211 CONSTRUCTOR_ELTS (inside_init));
6215 if (warn_sequence_point)
6216 verify_sequence_points (inside_init);
6218 /* Any type can be initialized
6219 from an expression of the same type, optionally with braces. */
6221 if (inside_init && TREE_TYPE (inside_init) != 0
6222 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6223 TYPE_MAIN_VARIANT (type))
6224 || (code == ARRAY_TYPE
6225 && comptypes (TREE_TYPE (inside_init), type))
6226 || (code == VECTOR_TYPE
6227 && comptypes (TREE_TYPE (inside_init), type))
6228 || (code == POINTER_TYPE
6229 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6230 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6231 TREE_TYPE (type)))))
6233 if (code == POINTER_TYPE)
6235 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6237 if (TREE_CODE (inside_init) == STRING_CST
6238 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6239 inside_init = array_to_pointer_conversion
6240 (init_loc, inside_init);
6241 else
6243 error_init ("invalid use of non-lvalue array");
6244 return error_mark_node;
6249 if (code == VECTOR_TYPE)
6250 /* Although the types are compatible, we may require a
6251 conversion. */
6252 inside_init = convert (type, inside_init);
6254 if (require_constant
6255 && (code == VECTOR_TYPE || !flag_isoc99)
6256 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6258 /* As an extension, allow initializing objects with static storage
6259 duration with compound literals (which are then treated just as
6260 the brace enclosed list they contain). Also allow this for
6261 vectors, as we can only assign them with compound literals. */
6262 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6263 inside_init = DECL_INITIAL (decl);
6266 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6267 && TREE_CODE (inside_init) != CONSTRUCTOR)
6269 error_init ("array initialized from non-constant array expression");
6270 return error_mark_node;
6273 /* Compound expressions can only occur here if -pedantic or
6274 -pedantic-errors is specified. In the later case, we always want
6275 an error. In the former case, we simply want a warning. */
6276 if (require_constant && pedantic
6277 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6279 inside_init
6280 = valid_compound_expr_initializer (inside_init,
6281 TREE_TYPE (inside_init));
6282 if (inside_init == error_mark_node)
6283 error_init ("initializer element is not constant");
6284 else
6285 pedwarn_init (init_loc, OPT_pedantic,
6286 "initializer element is not constant");
6287 if (flag_pedantic_errors)
6288 inside_init = error_mark_node;
6290 else if (require_constant
6291 && !initializer_constant_valid_p (inside_init,
6292 TREE_TYPE (inside_init)))
6294 error_init ("initializer element is not constant");
6295 inside_init = error_mark_node;
6297 else if (require_constant && !maybe_const)
6298 pedwarn_init (init_loc, 0,
6299 "initializer element is not a constant expression");
6301 /* Added to enable additional -Wmissing-format-attribute warnings. */
6302 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6303 inside_init = convert_for_assignment (init_loc, type, inside_init,
6304 origtype,
6305 ic_init, null_pointer_constant,
6306 NULL_TREE, NULL_TREE, 0);
6307 return inside_init;
6310 /* Handle scalar types, including conversions. */
6312 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6313 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6314 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6316 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6317 && (TREE_CODE (init) == STRING_CST
6318 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6319 inside_init = init = array_to_pointer_conversion (init_loc, init);
6320 if (semantic_type)
6321 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6322 inside_init);
6323 inside_init
6324 = convert_for_assignment (init_loc, type, inside_init, origtype,
6325 ic_init, null_pointer_constant,
6326 NULL_TREE, NULL_TREE, 0);
6328 /* Check to see if we have already given an error message. */
6329 if (inside_init == error_mark_node)
6331 else if (require_constant && !TREE_CONSTANT (inside_init))
6333 error_init ("initializer element is not constant");
6334 inside_init = error_mark_node;
6336 else if (require_constant
6337 && !initializer_constant_valid_p (inside_init,
6338 TREE_TYPE (inside_init)))
6340 error_init ("initializer element is not computable at load time");
6341 inside_init = error_mark_node;
6343 else if (require_constant && !maybe_const)
6344 pedwarn_init (init_loc, 0,
6345 "initializer element is not a constant expression");
6347 return inside_init;
6350 /* Come here only for records and arrays. */
6352 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6354 error_init ("variable-sized object may not be initialized");
6355 return error_mark_node;
6358 error_init ("invalid initializer");
6359 return error_mark_node;
6362 /* Handle initializers that use braces. */
6364 /* Type of object we are accumulating a constructor for.
6365 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6366 static tree constructor_type;
6368 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6369 left to fill. */
6370 static tree constructor_fields;
6372 /* For an ARRAY_TYPE, this is the specified index
6373 at which to store the next element we get. */
6374 static tree constructor_index;
6376 /* For an ARRAY_TYPE, this is the maximum index. */
6377 static tree constructor_max_index;
6379 /* For a RECORD_TYPE, this is the first field not yet written out. */
6380 static tree constructor_unfilled_fields;
6382 /* For an ARRAY_TYPE, this is the index of the first element
6383 not yet written out. */
6384 static tree constructor_unfilled_index;
6386 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6387 This is so we can generate gaps between fields, when appropriate. */
6388 static tree constructor_bit_index;
6390 /* If we are saving up the elements rather than allocating them,
6391 this is the list of elements so far (in reverse order,
6392 most recent first). */
6393 static VEC(constructor_elt,gc) *constructor_elements;
6395 /* 1 if constructor should be incrementally stored into a constructor chain,
6396 0 if all the elements should be kept in AVL tree. */
6397 static int constructor_incremental;
6399 /* 1 if so far this constructor's elements are all compile-time constants. */
6400 static int constructor_constant;
6402 /* 1 if so far this constructor's elements are all valid address constants. */
6403 static int constructor_simple;
6405 /* 1 if this constructor has an element that cannot be part of a
6406 constant expression. */
6407 static int constructor_nonconst;
6409 /* 1 if this constructor is erroneous so far. */
6410 static int constructor_erroneous;
6412 /* Structure for managing pending initializer elements, organized as an
6413 AVL tree. */
6415 struct init_node
6417 struct init_node *left, *right;
6418 struct init_node *parent;
6419 int balance;
6420 tree purpose;
6421 tree value;
6422 tree origtype;
6425 /* Tree of pending elements at this constructor level.
6426 These are elements encountered out of order
6427 which belong at places we haven't reached yet in actually
6428 writing the output.
6429 Will never hold tree nodes across GC runs. */
6430 static struct init_node *constructor_pending_elts;
6432 /* The SPELLING_DEPTH of this constructor. */
6433 static int constructor_depth;
6435 /* DECL node for which an initializer is being read.
6436 0 means we are reading a constructor expression
6437 such as (struct foo) {...}. */
6438 static tree constructor_decl;
6440 /* Nonzero if this is an initializer for a top-level decl. */
6441 static int constructor_top_level;
6443 /* Nonzero if there were any member designators in this initializer. */
6444 static int constructor_designated;
6446 /* Nesting depth of designator list. */
6447 static int designator_depth;
6449 /* Nonzero if there were diagnosed errors in this designator list. */
6450 static int designator_erroneous;
6453 /* This stack has a level for each implicit or explicit level of
6454 structuring in the initializer, including the outermost one. It
6455 saves the values of most of the variables above. */
6457 struct constructor_range_stack;
6459 struct constructor_stack
6461 struct constructor_stack *next;
6462 tree type;
6463 tree fields;
6464 tree index;
6465 tree max_index;
6466 tree unfilled_index;
6467 tree unfilled_fields;
6468 tree bit_index;
6469 VEC(constructor_elt,gc) *elements;
6470 struct init_node *pending_elts;
6471 int offset;
6472 int depth;
6473 /* If value nonzero, this value should replace the entire
6474 constructor at this level. */
6475 struct c_expr replacement_value;
6476 struct constructor_range_stack *range_stack;
6477 char constant;
6478 char simple;
6479 char nonconst;
6480 char implicit;
6481 char erroneous;
6482 char outer;
6483 char incremental;
6484 char designated;
6487 static struct constructor_stack *constructor_stack;
6489 /* This stack represents designators from some range designator up to
6490 the last designator in the list. */
6492 struct constructor_range_stack
6494 struct constructor_range_stack *next, *prev;
6495 struct constructor_stack *stack;
6496 tree range_start;
6497 tree index;
6498 tree range_end;
6499 tree fields;
6502 static struct constructor_range_stack *constructor_range_stack;
6504 /* This stack records separate initializers that are nested.
6505 Nested initializers can't happen in ANSI C, but GNU C allows them
6506 in cases like { ... (struct foo) { ... } ... }. */
6508 struct initializer_stack
6510 struct initializer_stack *next;
6511 tree decl;
6512 struct constructor_stack *constructor_stack;
6513 struct constructor_range_stack *constructor_range_stack;
6514 VEC(constructor_elt,gc) *elements;
6515 struct spelling *spelling;
6516 struct spelling *spelling_base;
6517 int spelling_size;
6518 char top_level;
6519 char require_constant_value;
6520 char require_constant_elements;
6523 static struct initializer_stack *initializer_stack;
6525 /* Prepare to parse and output the initializer for variable DECL. */
6527 void
6528 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6530 const char *locus;
6531 struct initializer_stack *p = XNEW (struct initializer_stack);
6533 p->decl = constructor_decl;
6534 p->require_constant_value = require_constant_value;
6535 p->require_constant_elements = require_constant_elements;
6536 p->constructor_stack = constructor_stack;
6537 p->constructor_range_stack = constructor_range_stack;
6538 p->elements = constructor_elements;
6539 p->spelling = spelling;
6540 p->spelling_base = spelling_base;
6541 p->spelling_size = spelling_size;
6542 p->top_level = constructor_top_level;
6543 p->next = initializer_stack;
6544 initializer_stack = p;
6546 constructor_decl = decl;
6547 constructor_designated = 0;
6548 constructor_top_level = top_level;
6550 if (decl != 0 && decl != error_mark_node)
6552 require_constant_value = TREE_STATIC (decl);
6553 require_constant_elements
6554 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6555 /* For a scalar, you can always use any value to initialize,
6556 even within braces. */
6557 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6558 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6559 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6560 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6561 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6563 else
6565 require_constant_value = 0;
6566 require_constant_elements = 0;
6567 locus = _("(anonymous)");
6570 constructor_stack = 0;
6571 constructor_range_stack = 0;
6573 missing_braces_mentioned = 0;
6575 spelling_base = 0;
6576 spelling_size = 0;
6577 RESTORE_SPELLING_DEPTH (0);
6579 if (locus)
6580 push_string (locus);
6583 void
6584 finish_init (void)
6586 struct initializer_stack *p = initializer_stack;
6588 /* Free the whole constructor stack of this initializer. */
6589 while (constructor_stack)
6591 struct constructor_stack *q = constructor_stack;
6592 constructor_stack = q->next;
6593 free (q);
6596 gcc_assert (!constructor_range_stack);
6598 /* Pop back to the data of the outer initializer (if any). */
6599 free (spelling_base);
6601 constructor_decl = p->decl;
6602 require_constant_value = p->require_constant_value;
6603 require_constant_elements = p->require_constant_elements;
6604 constructor_stack = p->constructor_stack;
6605 constructor_range_stack = p->constructor_range_stack;
6606 constructor_elements = p->elements;
6607 spelling = p->spelling;
6608 spelling_base = p->spelling_base;
6609 spelling_size = p->spelling_size;
6610 constructor_top_level = p->top_level;
6611 initializer_stack = p->next;
6612 free (p);
6615 /* Call here when we see the initializer is surrounded by braces.
6616 This is instead of a call to push_init_level;
6617 it is matched by a call to pop_init_level.
6619 TYPE is the type to initialize, for a constructor expression.
6620 For an initializer for a decl, TYPE is zero. */
6622 void
6623 really_start_incremental_init (tree type)
6625 struct constructor_stack *p = XNEW (struct constructor_stack);
6627 if (type == 0)
6628 type = TREE_TYPE (constructor_decl);
6630 if (TREE_CODE (type) == VECTOR_TYPE
6631 && TYPE_VECTOR_OPAQUE (type))
6632 error ("opaque vector types cannot be initialized");
6634 p->type = constructor_type;
6635 p->fields = constructor_fields;
6636 p->index = constructor_index;
6637 p->max_index = constructor_max_index;
6638 p->unfilled_index = constructor_unfilled_index;
6639 p->unfilled_fields = constructor_unfilled_fields;
6640 p->bit_index = constructor_bit_index;
6641 p->elements = constructor_elements;
6642 p->constant = constructor_constant;
6643 p->simple = constructor_simple;
6644 p->nonconst = constructor_nonconst;
6645 p->erroneous = constructor_erroneous;
6646 p->pending_elts = constructor_pending_elts;
6647 p->depth = constructor_depth;
6648 p->replacement_value.value = 0;
6649 p->replacement_value.original_code = ERROR_MARK;
6650 p->replacement_value.original_type = NULL;
6651 p->implicit = 0;
6652 p->range_stack = 0;
6653 p->outer = 0;
6654 p->incremental = constructor_incremental;
6655 p->designated = constructor_designated;
6656 p->next = 0;
6657 constructor_stack = p;
6659 constructor_constant = 1;
6660 constructor_simple = 1;
6661 constructor_nonconst = 0;
6662 constructor_depth = SPELLING_DEPTH ();
6663 constructor_elements = 0;
6664 constructor_pending_elts = 0;
6665 constructor_type = type;
6666 constructor_incremental = 1;
6667 constructor_designated = 0;
6668 designator_depth = 0;
6669 designator_erroneous = 0;
6671 if (TREE_CODE (constructor_type) == RECORD_TYPE
6672 || TREE_CODE (constructor_type) == UNION_TYPE)
6674 constructor_fields = TYPE_FIELDS (constructor_type);
6675 /* Skip any nameless bit fields at the beginning. */
6676 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6677 && DECL_NAME (constructor_fields) == 0)
6678 constructor_fields = DECL_CHAIN (constructor_fields);
6680 constructor_unfilled_fields = constructor_fields;
6681 constructor_bit_index = bitsize_zero_node;
6683 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6685 if (TYPE_DOMAIN (constructor_type))
6687 constructor_max_index
6688 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6690 /* Detect non-empty initializations of zero-length arrays. */
6691 if (constructor_max_index == NULL_TREE
6692 && TYPE_SIZE (constructor_type))
6693 constructor_max_index = integer_minus_one_node;
6695 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6696 to initialize VLAs will cause a proper error; avoid tree
6697 checking errors as well by setting a safe value. */
6698 if (constructor_max_index
6699 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6700 constructor_max_index = integer_minus_one_node;
6702 constructor_index
6703 = convert (bitsizetype,
6704 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6706 else
6708 constructor_index = bitsize_zero_node;
6709 constructor_max_index = NULL_TREE;
6712 constructor_unfilled_index = constructor_index;
6714 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6716 /* Vectors are like simple fixed-size arrays. */
6717 constructor_max_index =
6718 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6719 constructor_index = bitsize_zero_node;
6720 constructor_unfilled_index = constructor_index;
6722 else
6724 /* Handle the case of int x = {5}; */
6725 constructor_fields = constructor_type;
6726 constructor_unfilled_fields = constructor_type;
6730 /* Push down into a subobject, for initialization.
6731 If this is for an explicit set of braces, IMPLICIT is 0.
6732 If it is because the next element belongs at a lower level,
6733 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6735 void
6736 push_init_level (int implicit, struct obstack * braced_init_obstack)
6738 struct constructor_stack *p;
6739 tree value = NULL_TREE;
6741 /* If we've exhausted any levels that didn't have braces,
6742 pop them now. If implicit == 1, this will have been done in
6743 process_init_element; do not repeat it here because in the case
6744 of excess initializers for an empty aggregate this leads to an
6745 infinite cycle of popping a level and immediately recreating
6746 it. */
6747 if (implicit != 1)
6749 while (constructor_stack->implicit)
6751 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6752 || TREE_CODE (constructor_type) == UNION_TYPE)
6753 && constructor_fields == 0)
6754 process_init_element (pop_init_level (1, braced_init_obstack),
6755 true, braced_init_obstack);
6756 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6757 && constructor_max_index
6758 && tree_int_cst_lt (constructor_max_index,
6759 constructor_index))
6760 process_init_element (pop_init_level (1, braced_init_obstack),
6761 true, braced_init_obstack);
6762 else
6763 break;
6767 /* Unless this is an explicit brace, we need to preserve previous
6768 content if any. */
6769 if (implicit)
6771 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6772 || TREE_CODE (constructor_type) == UNION_TYPE)
6773 && constructor_fields)
6774 value = find_init_member (constructor_fields, braced_init_obstack);
6775 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6776 value = find_init_member (constructor_index, braced_init_obstack);
6779 p = XNEW (struct constructor_stack);
6780 p->type = constructor_type;
6781 p->fields = constructor_fields;
6782 p->index = constructor_index;
6783 p->max_index = constructor_max_index;
6784 p->unfilled_index = constructor_unfilled_index;
6785 p->unfilled_fields = constructor_unfilled_fields;
6786 p->bit_index = constructor_bit_index;
6787 p->elements = constructor_elements;
6788 p->constant = constructor_constant;
6789 p->simple = constructor_simple;
6790 p->nonconst = constructor_nonconst;
6791 p->erroneous = constructor_erroneous;
6792 p->pending_elts = constructor_pending_elts;
6793 p->depth = constructor_depth;
6794 p->replacement_value.value = 0;
6795 p->replacement_value.original_code = ERROR_MARK;
6796 p->replacement_value.original_type = NULL;
6797 p->implicit = implicit;
6798 p->outer = 0;
6799 p->incremental = constructor_incremental;
6800 p->designated = constructor_designated;
6801 p->next = constructor_stack;
6802 p->range_stack = 0;
6803 constructor_stack = p;
6805 constructor_constant = 1;
6806 constructor_simple = 1;
6807 constructor_nonconst = 0;
6808 constructor_depth = SPELLING_DEPTH ();
6809 constructor_elements = 0;
6810 constructor_incremental = 1;
6811 constructor_designated = 0;
6812 constructor_pending_elts = 0;
6813 if (!implicit)
6815 p->range_stack = constructor_range_stack;
6816 constructor_range_stack = 0;
6817 designator_depth = 0;
6818 designator_erroneous = 0;
6821 /* Don't die if an entire brace-pair level is superfluous
6822 in the containing level. */
6823 if (constructor_type == 0)
6825 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6826 || TREE_CODE (constructor_type) == UNION_TYPE)
6828 /* Don't die if there are extra init elts at the end. */
6829 if (constructor_fields == 0)
6830 constructor_type = 0;
6831 else
6833 constructor_type = TREE_TYPE (constructor_fields);
6834 push_member_name (constructor_fields);
6835 constructor_depth++;
6838 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6840 constructor_type = TREE_TYPE (constructor_type);
6841 push_array_bounds (tree_low_cst (constructor_index, 1));
6842 constructor_depth++;
6845 if (constructor_type == 0)
6847 error_init ("extra brace group at end of initializer");
6848 constructor_fields = 0;
6849 constructor_unfilled_fields = 0;
6850 return;
6853 if (value && TREE_CODE (value) == CONSTRUCTOR)
6855 constructor_constant = TREE_CONSTANT (value);
6856 constructor_simple = TREE_STATIC (value);
6857 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6858 constructor_elements = CONSTRUCTOR_ELTS (value);
6859 if (!VEC_empty (constructor_elt, constructor_elements)
6860 && (TREE_CODE (constructor_type) == RECORD_TYPE
6861 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6862 set_nonincremental_init (braced_init_obstack);
6865 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6867 missing_braces_mentioned = 1;
6868 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6871 if (TREE_CODE (constructor_type) == RECORD_TYPE
6872 || TREE_CODE (constructor_type) == UNION_TYPE)
6874 constructor_fields = TYPE_FIELDS (constructor_type);
6875 /* Skip any nameless bit fields at the beginning. */
6876 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6877 && DECL_NAME (constructor_fields) == 0)
6878 constructor_fields = DECL_CHAIN (constructor_fields);
6880 constructor_unfilled_fields = constructor_fields;
6881 constructor_bit_index = bitsize_zero_node;
6883 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6885 /* Vectors are like simple fixed-size arrays. */
6886 constructor_max_index =
6887 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6888 constructor_index = convert (bitsizetype, integer_zero_node);
6889 constructor_unfilled_index = constructor_index;
6891 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6893 if (TYPE_DOMAIN (constructor_type))
6895 constructor_max_index
6896 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6898 /* Detect non-empty initializations of zero-length arrays. */
6899 if (constructor_max_index == NULL_TREE
6900 && TYPE_SIZE (constructor_type))
6901 constructor_max_index = integer_minus_one_node;
6903 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6904 to initialize VLAs will cause a proper error; avoid tree
6905 checking errors as well by setting a safe value. */
6906 if (constructor_max_index
6907 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6908 constructor_max_index = integer_minus_one_node;
6910 constructor_index
6911 = convert (bitsizetype,
6912 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6914 else
6915 constructor_index = bitsize_zero_node;
6917 constructor_unfilled_index = constructor_index;
6918 if (value && TREE_CODE (value) == STRING_CST)
6920 /* We need to split the char/wchar array into individual
6921 characters, so that we don't have to special case it
6922 everywhere. */
6923 set_nonincremental_init_from_string (value, braced_init_obstack);
6926 else
6928 if (constructor_type != error_mark_node)
6929 warning_init (0, "braces around scalar initializer");
6930 constructor_fields = constructor_type;
6931 constructor_unfilled_fields = constructor_type;
6935 /* At the end of an implicit or explicit brace level,
6936 finish up that level of constructor. If a single expression
6937 with redundant braces initialized that level, return the
6938 c_expr structure for that expression. Otherwise, the original_code
6939 element is set to ERROR_MARK.
6940 If we were outputting the elements as they are read, return 0 as the value
6941 from inner levels (process_init_element ignores that),
6942 but return error_mark_node as the value from the outermost level
6943 (that's what we want to put in DECL_INITIAL).
6944 Otherwise, return a CONSTRUCTOR expression as the value. */
6946 struct c_expr
6947 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6949 struct constructor_stack *p;
6950 struct c_expr ret;
6951 ret.value = 0;
6952 ret.original_code = ERROR_MARK;
6953 ret.original_type = NULL;
6955 if (implicit == 0)
6957 /* When we come to an explicit close brace,
6958 pop any inner levels that didn't have explicit braces. */
6959 while (constructor_stack->implicit)
6961 process_init_element (pop_init_level (1, braced_init_obstack),
6962 true, braced_init_obstack);
6964 gcc_assert (!constructor_range_stack);
6967 /* Now output all pending elements. */
6968 constructor_incremental = 1;
6969 output_pending_init_elements (1, braced_init_obstack);
6971 p = constructor_stack;
6973 /* Error for initializing a flexible array member, or a zero-length
6974 array member in an inappropriate context. */
6975 if (constructor_type && constructor_fields
6976 && TREE_CODE (constructor_type) == ARRAY_TYPE
6977 && TYPE_DOMAIN (constructor_type)
6978 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6980 /* Silently discard empty initializations. The parser will
6981 already have pedwarned for empty brackets. */
6982 if (integer_zerop (constructor_unfilled_index))
6983 constructor_type = NULL_TREE;
6984 else
6986 gcc_assert (!TYPE_SIZE (constructor_type));
6988 if (constructor_depth > 2)
6989 error_init ("initialization of flexible array member in a nested context");
6990 else
6991 pedwarn_init (input_location, OPT_pedantic,
6992 "initialization of a flexible array member");
6994 /* We have already issued an error message for the existence
6995 of a flexible array member not at the end of the structure.
6996 Discard the initializer so that we do not die later. */
6997 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6998 constructor_type = NULL_TREE;
7002 /* Warn when some struct elements are implicitly initialized to zero. */
7003 if (warn_missing_field_initializers
7004 && constructor_type
7005 && TREE_CODE (constructor_type) == RECORD_TYPE
7006 && constructor_unfilled_fields)
7008 /* Do not warn for flexible array members or zero-length arrays. */
7009 while (constructor_unfilled_fields
7010 && (!DECL_SIZE (constructor_unfilled_fields)
7011 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7012 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7014 /* Do not warn if this level of the initializer uses member
7015 designators; it is likely to be deliberate. */
7016 if (constructor_unfilled_fields && !constructor_designated)
7018 push_member_name (constructor_unfilled_fields);
7019 warning_init (OPT_Wmissing_field_initializers,
7020 "missing initializer");
7021 RESTORE_SPELLING_DEPTH (constructor_depth);
7025 /* Pad out the end of the structure. */
7026 if (p->replacement_value.value)
7027 /* If this closes a superfluous brace pair,
7028 just pass out the element between them. */
7029 ret = p->replacement_value;
7030 else if (constructor_type == 0)
7032 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7033 && TREE_CODE (constructor_type) != UNION_TYPE
7034 && TREE_CODE (constructor_type) != ARRAY_TYPE
7035 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7037 /* A nonincremental scalar initializer--just return
7038 the element, after verifying there is just one. */
7039 if (VEC_empty (constructor_elt,constructor_elements))
7041 if (!constructor_erroneous)
7042 error_init ("empty scalar initializer");
7043 ret.value = error_mark_node;
7045 else if (VEC_length (constructor_elt,constructor_elements) != 1)
7047 error_init ("extra elements in scalar initializer");
7048 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7050 else
7051 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7053 else
7055 if (constructor_erroneous)
7056 ret.value = error_mark_node;
7057 else
7059 ret.value = build_constructor (constructor_type,
7060 constructor_elements);
7061 if (constructor_constant)
7062 TREE_CONSTANT (ret.value) = 1;
7063 if (constructor_constant && constructor_simple)
7064 TREE_STATIC (ret.value) = 1;
7065 if (constructor_nonconst)
7066 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7070 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7072 if (constructor_nonconst)
7073 ret.original_code = C_MAYBE_CONST_EXPR;
7074 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7075 ret.original_code = ERROR_MARK;
7078 constructor_type = p->type;
7079 constructor_fields = p->fields;
7080 constructor_index = p->index;
7081 constructor_max_index = p->max_index;
7082 constructor_unfilled_index = p->unfilled_index;
7083 constructor_unfilled_fields = p->unfilled_fields;
7084 constructor_bit_index = p->bit_index;
7085 constructor_elements = p->elements;
7086 constructor_constant = p->constant;
7087 constructor_simple = p->simple;
7088 constructor_nonconst = p->nonconst;
7089 constructor_erroneous = p->erroneous;
7090 constructor_incremental = p->incremental;
7091 constructor_designated = p->designated;
7092 constructor_pending_elts = p->pending_elts;
7093 constructor_depth = p->depth;
7094 if (!p->implicit)
7095 constructor_range_stack = p->range_stack;
7096 RESTORE_SPELLING_DEPTH (constructor_depth);
7098 constructor_stack = p->next;
7099 free (p);
7101 if (ret.value == 0 && constructor_stack == 0)
7102 ret.value = error_mark_node;
7103 return ret;
7106 /* Common handling for both array range and field name designators.
7107 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7109 static int
7110 set_designator (int array, struct obstack * braced_init_obstack)
7112 tree subtype;
7113 enum tree_code subcode;
7115 /* Don't die if an entire brace-pair level is superfluous
7116 in the containing level. */
7117 if (constructor_type == 0)
7118 return 1;
7120 /* If there were errors in this designator list already, bail out
7121 silently. */
7122 if (designator_erroneous)
7123 return 1;
7125 if (!designator_depth)
7127 gcc_assert (!constructor_range_stack);
7129 /* Designator list starts at the level of closest explicit
7130 braces. */
7131 while (constructor_stack->implicit)
7133 process_init_element (pop_init_level (1, braced_init_obstack),
7134 true, braced_init_obstack);
7136 constructor_designated = 1;
7137 return 0;
7140 switch (TREE_CODE (constructor_type))
7142 case RECORD_TYPE:
7143 case UNION_TYPE:
7144 subtype = TREE_TYPE (constructor_fields);
7145 if (subtype != error_mark_node)
7146 subtype = TYPE_MAIN_VARIANT (subtype);
7147 break;
7148 case ARRAY_TYPE:
7149 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7150 break;
7151 default:
7152 gcc_unreachable ();
7155 subcode = TREE_CODE (subtype);
7156 if (array && subcode != ARRAY_TYPE)
7158 error_init ("array index in non-array initializer");
7159 return 1;
7161 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7163 error_init ("field name not in record or union initializer");
7164 return 1;
7167 constructor_designated = 1;
7168 push_init_level (2, braced_init_obstack);
7169 return 0;
7172 /* If there are range designators in designator list, push a new designator
7173 to constructor_range_stack. RANGE_END is end of such stack range or
7174 NULL_TREE if there is no range designator at this level. */
7176 static void
7177 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7179 struct constructor_range_stack *p;
7181 p = (struct constructor_range_stack *)
7182 obstack_alloc (braced_init_obstack,
7183 sizeof (struct constructor_range_stack));
7184 p->prev = constructor_range_stack;
7185 p->next = 0;
7186 p->fields = constructor_fields;
7187 p->range_start = constructor_index;
7188 p->index = constructor_index;
7189 p->stack = constructor_stack;
7190 p->range_end = range_end;
7191 if (constructor_range_stack)
7192 constructor_range_stack->next = p;
7193 constructor_range_stack = p;
7196 /* Within an array initializer, specify the next index to be initialized.
7197 FIRST is that index. If LAST is nonzero, then initialize a range
7198 of indices, running from FIRST through LAST. */
7200 void
7201 set_init_index (tree first, tree last,
7202 struct obstack * braced_init_obstack)
7204 if (set_designator (1, braced_init_obstack))
7205 return;
7207 designator_erroneous = 1;
7209 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7210 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7212 error_init ("array index in initializer not of integer type");
7213 return;
7216 if (TREE_CODE (first) != INTEGER_CST)
7218 first = c_fully_fold (first, false, NULL);
7219 if (TREE_CODE (first) == INTEGER_CST)
7220 pedwarn_init (input_location, OPT_pedantic,
7221 "array index in initializer is not "
7222 "an integer constant expression");
7225 if (last && TREE_CODE (last) != INTEGER_CST)
7227 last = c_fully_fold (last, false, NULL);
7228 if (TREE_CODE (last) == INTEGER_CST)
7229 pedwarn_init (input_location, OPT_pedantic,
7230 "array index in initializer is not "
7231 "an integer constant expression");
7234 if (TREE_CODE (first) != INTEGER_CST)
7235 error_init ("nonconstant array index in initializer");
7236 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7237 error_init ("nonconstant array index in initializer");
7238 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7239 error_init ("array index in non-array initializer");
7240 else if (tree_int_cst_sgn (first) == -1)
7241 error_init ("array index in initializer exceeds array bounds");
7242 else if (constructor_max_index
7243 && tree_int_cst_lt (constructor_max_index, first))
7244 error_init ("array index in initializer exceeds array bounds");
7245 else
7247 constant_expression_warning (first);
7248 if (last)
7249 constant_expression_warning (last);
7250 constructor_index = convert (bitsizetype, first);
7252 if (last)
7254 if (tree_int_cst_equal (first, last))
7255 last = 0;
7256 else if (tree_int_cst_lt (last, first))
7258 error_init ("empty index range in initializer");
7259 last = 0;
7261 else
7263 last = convert (bitsizetype, last);
7264 if (constructor_max_index != 0
7265 && tree_int_cst_lt (constructor_max_index, last))
7267 error_init ("array index range in initializer exceeds array bounds");
7268 last = 0;
7273 designator_depth++;
7274 designator_erroneous = 0;
7275 if (constructor_range_stack || last)
7276 push_range_stack (last, braced_init_obstack);
7280 /* Within a struct initializer, specify the next field to be initialized. */
7282 void
7283 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7285 tree field;
7287 if (set_designator (0, braced_init_obstack))
7288 return;
7290 designator_erroneous = 1;
7292 if (TREE_CODE (constructor_type) != RECORD_TYPE
7293 && TREE_CODE (constructor_type) != UNION_TYPE)
7295 error_init ("field name not in record or union initializer");
7296 return;
7299 field = lookup_field (constructor_type, fieldname);
7301 if (field == 0)
7302 error ("unknown field %qE specified in initializer", fieldname);
7303 else
7306 constructor_fields = TREE_VALUE (field);
7307 designator_depth++;
7308 designator_erroneous = 0;
7309 if (constructor_range_stack)
7310 push_range_stack (NULL_TREE, braced_init_obstack);
7311 field = TREE_CHAIN (field);
7312 if (field)
7314 if (set_designator (0, braced_init_obstack))
7315 return;
7318 while (field != NULL_TREE);
7321 /* Add a new initializer to the tree of pending initializers. PURPOSE
7322 identifies the initializer, either array index or field in a structure.
7323 VALUE is the value of that index or field. If ORIGTYPE is not
7324 NULL_TREE, it is the original type of VALUE.
7326 IMPLICIT is true if value comes from pop_init_level (1),
7327 the new initializer has been merged with the existing one
7328 and thus no warnings should be emitted about overriding an
7329 existing initializer. */
7331 static void
7332 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7333 struct obstack * braced_init_obstack)
7335 struct init_node *p, **q, *r;
7337 q = &constructor_pending_elts;
7338 p = 0;
7340 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7342 while (*q != 0)
7344 p = *q;
7345 if (tree_int_cst_lt (purpose, p->purpose))
7346 q = &p->left;
7347 else if (tree_int_cst_lt (p->purpose, purpose))
7348 q = &p->right;
7349 else
7351 if (!implicit)
7353 if (TREE_SIDE_EFFECTS (p->value))
7354 warning_init (0, "initialized field with side-effects overwritten");
7355 else if (warn_override_init)
7356 warning_init (OPT_Woverride_init, "initialized field overwritten");
7358 p->value = value;
7359 p->origtype = origtype;
7360 return;
7364 else
7366 tree bitpos;
7368 bitpos = bit_position (purpose);
7369 while (*q != NULL)
7371 p = *q;
7372 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7373 q = &p->left;
7374 else if (p->purpose != purpose)
7375 q = &p->right;
7376 else
7378 if (!implicit)
7380 if (TREE_SIDE_EFFECTS (p->value))
7381 warning_init (0, "initialized field with side-effects overwritten");
7382 else if (warn_override_init)
7383 warning_init (OPT_Woverride_init, "initialized field overwritten");
7385 p->value = value;
7386 p->origtype = origtype;
7387 return;
7392 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7393 sizeof (struct init_node));
7394 r->purpose = purpose;
7395 r->value = value;
7396 r->origtype = origtype;
7398 *q = r;
7399 r->parent = p;
7400 r->left = 0;
7401 r->right = 0;
7402 r->balance = 0;
7404 while (p)
7406 struct init_node *s;
7408 if (r == p->left)
7410 if (p->balance == 0)
7411 p->balance = -1;
7412 else if (p->balance < 0)
7414 if (r->balance < 0)
7416 /* L rotation. */
7417 p->left = r->right;
7418 if (p->left)
7419 p->left->parent = p;
7420 r->right = p;
7422 p->balance = 0;
7423 r->balance = 0;
7425 s = p->parent;
7426 p->parent = r;
7427 r->parent = s;
7428 if (s)
7430 if (s->left == p)
7431 s->left = r;
7432 else
7433 s->right = r;
7435 else
7436 constructor_pending_elts = r;
7438 else
7440 /* LR rotation. */
7441 struct init_node *t = r->right;
7443 r->right = t->left;
7444 if (r->right)
7445 r->right->parent = r;
7446 t->left = r;
7448 p->left = t->right;
7449 if (p->left)
7450 p->left->parent = p;
7451 t->right = p;
7453 p->balance = t->balance < 0;
7454 r->balance = -(t->balance > 0);
7455 t->balance = 0;
7457 s = p->parent;
7458 p->parent = t;
7459 r->parent = t;
7460 t->parent = s;
7461 if (s)
7463 if (s->left == p)
7464 s->left = t;
7465 else
7466 s->right = t;
7468 else
7469 constructor_pending_elts = t;
7471 break;
7473 else
7475 /* p->balance == +1; growth of left side balances the node. */
7476 p->balance = 0;
7477 break;
7480 else /* r == p->right */
7482 if (p->balance == 0)
7483 /* Growth propagation from right side. */
7484 p->balance++;
7485 else if (p->balance > 0)
7487 if (r->balance > 0)
7489 /* R rotation. */
7490 p->right = r->left;
7491 if (p->right)
7492 p->right->parent = p;
7493 r->left = p;
7495 p->balance = 0;
7496 r->balance = 0;
7498 s = p->parent;
7499 p->parent = r;
7500 r->parent = s;
7501 if (s)
7503 if (s->left == p)
7504 s->left = r;
7505 else
7506 s->right = r;
7508 else
7509 constructor_pending_elts = r;
7511 else /* r->balance == -1 */
7513 /* RL rotation */
7514 struct init_node *t = r->left;
7516 r->left = t->right;
7517 if (r->left)
7518 r->left->parent = r;
7519 t->right = r;
7521 p->right = t->left;
7522 if (p->right)
7523 p->right->parent = p;
7524 t->left = p;
7526 r->balance = (t->balance < 0);
7527 p->balance = -(t->balance > 0);
7528 t->balance = 0;
7530 s = p->parent;
7531 p->parent = t;
7532 r->parent = t;
7533 t->parent = s;
7534 if (s)
7536 if (s->left == p)
7537 s->left = t;
7538 else
7539 s->right = t;
7541 else
7542 constructor_pending_elts = t;
7544 break;
7546 else
7548 /* p->balance == -1; growth of right side balances the node. */
7549 p->balance = 0;
7550 break;
7554 r = p;
7555 p = p->parent;
7559 /* Build AVL tree from a sorted chain. */
7561 static void
7562 set_nonincremental_init (struct obstack * braced_init_obstack)
7564 unsigned HOST_WIDE_INT ix;
7565 tree index, value;
7567 if (TREE_CODE (constructor_type) != RECORD_TYPE
7568 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7569 return;
7571 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7573 add_pending_init (index, value, NULL_TREE, false,
7574 braced_init_obstack);
7576 constructor_elements = 0;
7577 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7579 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7580 /* Skip any nameless bit fields at the beginning. */
7581 while (constructor_unfilled_fields != 0
7582 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7583 && DECL_NAME (constructor_unfilled_fields) == 0)
7584 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7587 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7589 if (TYPE_DOMAIN (constructor_type))
7590 constructor_unfilled_index
7591 = convert (bitsizetype,
7592 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7593 else
7594 constructor_unfilled_index = bitsize_zero_node;
7596 constructor_incremental = 0;
7599 /* Build AVL tree from a string constant. */
7601 static void
7602 set_nonincremental_init_from_string (tree str,
7603 struct obstack * braced_init_obstack)
7605 tree value, purpose, type;
7606 HOST_WIDE_INT val[2];
7607 const char *p, *end;
7608 int byte, wchar_bytes, charwidth, bitpos;
7610 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7612 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7613 charwidth = TYPE_PRECISION (char_type_node);
7614 type = TREE_TYPE (constructor_type);
7615 p = TREE_STRING_POINTER (str);
7616 end = p + TREE_STRING_LENGTH (str);
7618 for (purpose = bitsize_zero_node;
7619 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7620 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7622 if (wchar_bytes == 1)
7624 val[1] = (unsigned char) *p++;
7625 val[0] = 0;
7627 else
7629 val[0] = 0;
7630 val[1] = 0;
7631 for (byte = 0; byte < wchar_bytes; byte++)
7633 if (BYTES_BIG_ENDIAN)
7634 bitpos = (wchar_bytes - byte - 1) * charwidth;
7635 else
7636 bitpos = byte * charwidth;
7637 val[bitpos < HOST_BITS_PER_WIDE_INT]
7638 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7639 << (bitpos % HOST_BITS_PER_WIDE_INT);
7643 if (!TYPE_UNSIGNED (type))
7645 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7646 if (bitpos < HOST_BITS_PER_WIDE_INT)
7648 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7650 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7651 val[0] = -1;
7654 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7656 if (val[1] < 0)
7657 val[0] = -1;
7659 else if (val[0] & (((HOST_WIDE_INT) 1)
7660 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7661 val[0] |= ((HOST_WIDE_INT) -1)
7662 << (bitpos - HOST_BITS_PER_WIDE_INT);
7665 value = build_int_cst_wide (type, val[1], val[0]);
7666 add_pending_init (purpose, value, NULL_TREE, false,
7667 braced_init_obstack);
7670 constructor_incremental = 0;
7673 /* Return value of FIELD in pending initializer or zero if the field was
7674 not initialized yet. */
7676 static tree
7677 find_init_member (tree field, struct obstack * braced_init_obstack)
7679 struct init_node *p;
7681 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7683 if (constructor_incremental
7684 && tree_int_cst_lt (field, constructor_unfilled_index))
7685 set_nonincremental_init (braced_init_obstack);
7687 p = constructor_pending_elts;
7688 while (p)
7690 if (tree_int_cst_lt (field, p->purpose))
7691 p = p->left;
7692 else if (tree_int_cst_lt (p->purpose, field))
7693 p = p->right;
7694 else
7695 return p->value;
7698 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7700 tree bitpos = bit_position (field);
7702 if (constructor_incremental
7703 && (!constructor_unfilled_fields
7704 || tree_int_cst_lt (bitpos,
7705 bit_position (constructor_unfilled_fields))))
7706 set_nonincremental_init (braced_init_obstack);
7708 p = constructor_pending_elts;
7709 while (p)
7711 if (field == p->purpose)
7712 return p->value;
7713 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7714 p = p->left;
7715 else
7716 p = p->right;
7719 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7721 if (!VEC_empty (constructor_elt, constructor_elements)
7722 && (VEC_last (constructor_elt, constructor_elements)->index
7723 == field))
7724 return VEC_last (constructor_elt, constructor_elements)->value;
7726 return 0;
7729 /* "Output" the next constructor element.
7730 At top level, really output it to assembler code now.
7731 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7732 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7733 TYPE is the data type that the containing data type wants here.
7734 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7735 If VALUE is a string constant, STRICT_STRING is true if it is
7736 unparenthesized or we should not warn here for it being parenthesized.
7737 For other types of VALUE, STRICT_STRING is not used.
7739 PENDING if non-nil means output pending elements that belong
7740 right after this element. (PENDING is normally 1;
7741 it is 0 while outputting pending elements, to avoid recursion.)
7743 IMPLICIT is true if value comes from pop_init_level (1),
7744 the new initializer has been merged with the existing one
7745 and thus no warnings should be emitted about overriding an
7746 existing initializer. */
7748 static void
7749 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7750 tree field, int pending, bool implicit,
7751 struct obstack * braced_init_obstack)
7753 tree semantic_type = NULL_TREE;
7754 constructor_elt *celt;
7755 bool maybe_const = true;
7756 bool npc;
7758 if (type == error_mark_node || value == error_mark_node)
7760 constructor_erroneous = 1;
7761 return;
7763 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7764 && (TREE_CODE (value) == STRING_CST
7765 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7766 && !(TREE_CODE (value) == STRING_CST
7767 && TREE_CODE (type) == ARRAY_TYPE
7768 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7769 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7770 TYPE_MAIN_VARIANT (type)))
7771 value = array_to_pointer_conversion (input_location, value);
7773 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7774 && require_constant_value && !flag_isoc99 && pending)
7776 /* As an extension, allow initializing objects with static storage
7777 duration with compound literals (which are then treated just as
7778 the brace enclosed list they contain). */
7779 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7780 value = DECL_INITIAL (decl);
7783 npc = null_pointer_constant_p (value);
7784 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7786 semantic_type = TREE_TYPE (value);
7787 value = TREE_OPERAND (value, 0);
7789 value = c_fully_fold (value, require_constant_value, &maybe_const);
7791 if (value == error_mark_node)
7792 constructor_erroneous = 1;
7793 else if (!TREE_CONSTANT (value))
7794 constructor_constant = 0;
7795 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7796 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7797 || TREE_CODE (constructor_type) == UNION_TYPE)
7798 && DECL_C_BIT_FIELD (field)
7799 && TREE_CODE (value) != INTEGER_CST))
7800 constructor_simple = 0;
7801 if (!maybe_const)
7802 constructor_nonconst = 1;
7804 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7806 if (require_constant_value)
7808 error_init ("initializer element is not constant");
7809 value = error_mark_node;
7811 else if (require_constant_elements)
7812 pedwarn (input_location, 0,
7813 "initializer element is not computable at load time");
7815 else if (!maybe_const
7816 && (require_constant_value || require_constant_elements))
7817 pedwarn_init (input_location, 0,
7818 "initializer element is not a constant expression");
7820 /* Issue -Wc++-compat warnings about initializing a bitfield with
7821 enum type. */
7822 if (warn_cxx_compat
7823 && field != NULL_TREE
7824 && TREE_CODE (field) == FIELD_DECL
7825 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7826 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7827 != TYPE_MAIN_VARIANT (type))
7828 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7830 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7831 if (checktype != error_mark_node
7832 && (TYPE_MAIN_VARIANT (checktype)
7833 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7834 warning_init (OPT_Wc___compat,
7835 "enum conversion in initialization is invalid in C++");
7838 /* If this field is empty (and not at the end of structure),
7839 don't do anything other than checking the initializer. */
7840 if (field
7841 && (TREE_TYPE (field) == error_mark_node
7842 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7843 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7844 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7845 || DECL_CHAIN (field)))))
7846 return;
7848 if (semantic_type)
7849 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7850 value = digest_init (input_location, type, value, origtype, npc,
7851 strict_string, require_constant_value);
7852 if (value == error_mark_node)
7854 constructor_erroneous = 1;
7855 return;
7857 if (require_constant_value || require_constant_elements)
7858 constant_expression_warning (value);
7860 /* If this element doesn't come next in sequence,
7861 put it on constructor_pending_elts. */
7862 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7863 && (!constructor_incremental
7864 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7866 if (constructor_incremental
7867 && tree_int_cst_lt (field, constructor_unfilled_index))
7868 set_nonincremental_init (braced_init_obstack);
7870 add_pending_init (field, value, origtype, implicit,
7871 braced_init_obstack);
7872 return;
7874 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7875 && (!constructor_incremental
7876 || field != constructor_unfilled_fields))
7878 /* We do this for records but not for unions. In a union,
7879 no matter which field is specified, it can be initialized
7880 right away since it starts at the beginning of the union. */
7881 if (constructor_incremental)
7883 if (!constructor_unfilled_fields)
7884 set_nonincremental_init (braced_init_obstack);
7885 else
7887 tree bitpos, unfillpos;
7889 bitpos = bit_position (field);
7890 unfillpos = bit_position (constructor_unfilled_fields);
7892 if (tree_int_cst_lt (bitpos, unfillpos))
7893 set_nonincremental_init (braced_init_obstack);
7897 add_pending_init (field, value, origtype, implicit,
7898 braced_init_obstack);
7899 return;
7901 else if (TREE_CODE (constructor_type) == UNION_TYPE
7902 && !VEC_empty (constructor_elt, constructor_elements))
7904 if (!implicit)
7906 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7907 constructor_elements)->value))
7908 warning_init (0,
7909 "initialized field with side-effects overwritten");
7910 else if (warn_override_init)
7911 warning_init (OPT_Woverride_init, "initialized field overwritten");
7914 /* We can have just one union field set. */
7915 constructor_elements = 0;
7918 /* Otherwise, output this element either to
7919 constructor_elements or to the assembler file. */
7921 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7922 celt->index = field;
7923 celt->value = value;
7925 /* Advance the variable that indicates sequential elements output. */
7926 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7927 constructor_unfilled_index
7928 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7929 bitsize_one_node);
7930 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7932 constructor_unfilled_fields
7933 = DECL_CHAIN (constructor_unfilled_fields);
7935 /* Skip any nameless bit fields. */
7936 while (constructor_unfilled_fields != 0
7937 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7938 && DECL_NAME (constructor_unfilled_fields) == 0)
7939 constructor_unfilled_fields =
7940 DECL_CHAIN (constructor_unfilled_fields);
7942 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7943 constructor_unfilled_fields = 0;
7945 /* Now output any pending elements which have become next. */
7946 if (pending)
7947 output_pending_init_elements (0, braced_init_obstack);
7950 /* Output any pending elements which have become next.
7951 As we output elements, constructor_unfilled_{fields,index}
7952 advances, which may cause other elements to become next;
7953 if so, they too are output.
7955 If ALL is 0, we return when there are
7956 no more pending elements to output now.
7958 If ALL is 1, we output space as necessary so that
7959 we can output all the pending elements. */
7960 static void
7961 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7963 struct init_node *elt = constructor_pending_elts;
7964 tree next;
7966 retry:
7968 /* Look through the whole pending tree.
7969 If we find an element that should be output now,
7970 output it. Otherwise, set NEXT to the element
7971 that comes first among those still pending. */
7973 next = 0;
7974 while (elt)
7976 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7978 if (tree_int_cst_equal (elt->purpose,
7979 constructor_unfilled_index))
7980 output_init_element (elt->value, elt->origtype, true,
7981 TREE_TYPE (constructor_type),
7982 constructor_unfilled_index, 0, false,
7983 braced_init_obstack);
7984 else if (tree_int_cst_lt (constructor_unfilled_index,
7985 elt->purpose))
7987 /* Advance to the next smaller node. */
7988 if (elt->left)
7989 elt = elt->left;
7990 else
7992 /* We have reached the smallest node bigger than the
7993 current unfilled index. Fill the space first. */
7994 next = elt->purpose;
7995 break;
7998 else
8000 /* Advance to the next bigger node. */
8001 if (elt->right)
8002 elt = elt->right;
8003 else
8005 /* We have reached the biggest node in a subtree. Find
8006 the parent of it, which is the next bigger node. */
8007 while (elt->parent && elt->parent->right == elt)
8008 elt = elt->parent;
8009 elt = elt->parent;
8010 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8011 elt->purpose))
8013 next = elt->purpose;
8014 break;
8019 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8020 || TREE_CODE (constructor_type) == UNION_TYPE)
8022 tree ctor_unfilled_bitpos, elt_bitpos;
8024 /* If the current record is complete we are done. */
8025 if (constructor_unfilled_fields == 0)
8026 break;
8028 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8029 elt_bitpos = bit_position (elt->purpose);
8030 /* We can't compare fields here because there might be empty
8031 fields in between. */
8032 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8034 constructor_unfilled_fields = elt->purpose;
8035 output_init_element (elt->value, elt->origtype, true,
8036 TREE_TYPE (elt->purpose),
8037 elt->purpose, 0, false,
8038 braced_init_obstack);
8040 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8042 /* Advance to the next smaller node. */
8043 if (elt->left)
8044 elt = elt->left;
8045 else
8047 /* We have reached the smallest node bigger than the
8048 current unfilled field. Fill the space first. */
8049 next = elt->purpose;
8050 break;
8053 else
8055 /* Advance to the next bigger node. */
8056 if (elt->right)
8057 elt = elt->right;
8058 else
8060 /* We have reached the biggest node in a subtree. Find
8061 the parent of it, which is the next bigger node. */
8062 while (elt->parent && elt->parent->right == elt)
8063 elt = elt->parent;
8064 elt = elt->parent;
8065 if (elt
8066 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8067 bit_position (elt->purpose))))
8069 next = elt->purpose;
8070 break;
8077 /* Ordinarily return, but not if we want to output all
8078 and there are elements left. */
8079 if (!(all && next != 0))
8080 return;
8082 /* If it's not incremental, just skip over the gap, so that after
8083 jumping to retry we will output the next successive element. */
8084 if (TREE_CODE (constructor_type) == RECORD_TYPE
8085 || TREE_CODE (constructor_type) == UNION_TYPE)
8086 constructor_unfilled_fields = next;
8087 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8088 constructor_unfilled_index = next;
8090 /* ELT now points to the node in the pending tree with the next
8091 initializer to output. */
8092 goto retry;
8095 /* Add one non-braced element to the current constructor level.
8096 This adjusts the current position within the constructor's type.
8097 This may also start or terminate implicit levels
8098 to handle a partly-braced initializer.
8100 Once this has found the correct level for the new element,
8101 it calls output_init_element.
8103 IMPLICIT is true if value comes from pop_init_level (1),
8104 the new initializer has been merged with the existing one
8105 and thus no warnings should be emitted about overriding an
8106 existing initializer. */
8108 void
8109 process_init_element (struct c_expr value, bool implicit,
8110 struct obstack * braced_init_obstack)
8112 tree orig_value = value.value;
8113 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8114 bool strict_string = value.original_code == STRING_CST;
8116 designator_depth = 0;
8117 designator_erroneous = 0;
8119 /* Handle superfluous braces around string cst as in
8120 char x[] = {"foo"}; */
8121 if (string_flag
8122 && constructor_type
8123 && TREE_CODE (constructor_type) == ARRAY_TYPE
8124 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8125 && integer_zerop (constructor_unfilled_index))
8127 if (constructor_stack->replacement_value.value)
8128 error_init ("excess elements in char array initializer");
8129 constructor_stack->replacement_value = value;
8130 return;
8133 if (constructor_stack->replacement_value.value != 0)
8135 error_init ("excess elements in struct initializer");
8136 return;
8139 /* Ignore elements of a brace group if it is entirely superfluous
8140 and has already been diagnosed. */
8141 if (constructor_type == 0)
8142 return;
8144 /* If we've exhausted any levels that didn't have braces,
8145 pop them now. */
8146 while (constructor_stack->implicit)
8148 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8149 || TREE_CODE (constructor_type) == UNION_TYPE)
8150 && constructor_fields == 0)
8151 process_init_element (pop_init_level (1, braced_init_obstack),
8152 true, braced_init_obstack);
8153 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8154 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8155 && (constructor_max_index == 0
8156 || tree_int_cst_lt (constructor_max_index,
8157 constructor_index)))
8158 process_init_element (pop_init_level (1, braced_init_obstack),
8159 true, braced_init_obstack);
8160 else
8161 break;
8164 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8165 if (constructor_range_stack)
8167 /* If value is a compound literal and we'll be just using its
8168 content, don't put it into a SAVE_EXPR. */
8169 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8170 || !require_constant_value
8171 || flag_isoc99)
8173 tree semantic_type = NULL_TREE;
8174 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8176 semantic_type = TREE_TYPE (value.value);
8177 value.value = TREE_OPERAND (value.value, 0);
8179 value.value = c_save_expr (value.value);
8180 if (semantic_type)
8181 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8182 value.value);
8186 while (1)
8188 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8190 tree fieldtype;
8191 enum tree_code fieldcode;
8193 if (constructor_fields == 0)
8195 pedwarn_init (input_location, 0,
8196 "excess elements in struct initializer");
8197 break;
8200 fieldtype = TREE_TYPE (constructor_fields);
8201 if (fieldtype != error_mark_node)
8202 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8203 fieldcode = TREE_CODE (fieldtype);
8205 /* Error for non-static initialization of a flexible array member. */
8206 if (fieldcode == ARRAY_TYPE
8207 && !require_constant_value
8208 && TYPE_SIZE (fieldtype) == NULL_TREE
8209 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8211 error_init ("non-static initialization of a flexible array member");
8212 break;
8215 /* Accept a string constant to initialize a subarray. */
8216 if (value.value != 0
8217 && fieldcode == ARRAY_TYPE
8218 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8219 && string_flag)
8220 value.value = orig_value;
8221 /* Otherwise, if we have come to a subaggregate,
8222 and we don't have an element of its type, push into it. */
8223 else if (value.value != 0
8224 && value.value != error_mark_node
8225 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8226 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8227 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8229 push_init_level (1, braced_init_obstack);
8230 continue;
8233 if (value.value)
8235 push_member_name (constructor_fields);
8236 output_init_element (value.value, value.original_type,
8237 strict_string, fieldtype,
8238 constructor_fields, 1, implicit,
8239 braced_init_obstack);
8240 RESTORE_SPELLING_DEPTH (constructor_depth);
8242 else
8243 /* Do the bookkeeping for an element that was
8244 directly output as a constructor. */
8246 /* For a record, keep track of end position of last field. */
8247 if (DECL_SIZE (constructor_fields))
8248 constructor_bit_index
8249 = size_binop_loc (input_location, PLUS_EXPR,
8250 bit_position (constructor_fields),
8251 DECL_SIZE (constructor_fields));
8253 /* If the current field was the first one not yet written out,
8254 it isn't now, so update. */
8255 if (constructor_unfilled_fields == constructor_fields)
8257 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8258 /* Skip any nameless bit fields. */
8259 while (constructor_unfilled_fields != 0
8260 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8261 && DECL_NAME (constructor_unfilled_fields) == 0)
8262 constructor_unfilled_fields =
8263 DECL_CHAIN (constructor_unfilled_fields);
8267 constructor_fields = DECL_CHAIN (constructor_fields);
8268 /* Skip any nameless bit fields at the beginning. */
8269 while (constructor_fields != 0
8270 && DECL_C_BIT_FIELD (constructor_fields)
8271 && DECL_NAME (constructor_fields) == 0)
8272 constructor_fields = DECL_CHAIN (constructor_fields);
8274 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8276 tree fieldtype;
8277 enum tree_code fieldcode;
8279 if (constructor_fields == 0)
8281 pedwarn_init (input_location, 0,
8282 "excess elements in union initializer");
8283 break;
8286 fieldtype = TREE_TYPE (constructor_fields);
8287 if (fieldtype != error_mark_node)
8288 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8289 fieldcode = TREE_CODE (fieldtype);
8291 /* Warn that traditional C rejects initialization of unions.
8292 We skip the warning if the value is zero. This is done
8293 under the assumption that the zero initializer in user
8294 code appears conditioned on e.g. __STDC__ to avoid
8295 "missing initializer" warnings and relies on default
8296 initialization to zero in the traditional C case.
8297 We also skip the warning if the initializer is designated,
8298 again on the assumption that this must be conditional on
8299 __STDC__ anyway (and we've already complained about the
8300 member-designator already). */
8301 if (!in_system_header && !constructor_designated
8302 && !(value.value && (integer_zerop (value.value)
8303 || real_zerop (value.value))))
8304 warning (OPT_Wtraditional, "traditional C rejects initialization "
8305 "of unions");
8307 /* Accept a string constant to initialize a subarray. */
8308 if (value.value != 0
8309 && fieldcode == ARRAY_TYPE
8310 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8311 && string_flag)
8312 value.value = orig_value;
8313 /* Otherwise, if we have come to a subaggregate,
8314 and we don't have an element of its type, push into it. */
8315 else if (value.value != 0
8316 && value.value != error_mark_node
8317 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8318 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8319 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8321 push_init_level (1, braced_init_obstack);
8322 continue;
8325 if (value.value)
8327 push_member_name (constructor_fields);
8328 output_init_element (value.value, value.original_type,
8329 strict_string, fieldtype,
8330 constructor_fields, 1, implicit,
8331 braced_init_obstack);
8332 RESTORE_SPELLING_DEPTH (constructor_depth);
8334 else
8335 /* Do the bookkeeping for an element that was
8336 directly output as a constructor. */
8338 constructor_bit_index = DECL_SIZE (constructor_fields);
8339 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8342 constructor_fields = 0;
8344 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8346 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8347 enum tree_code eltcode = TREE_CODE (elttype);
8349 /* Accept a string constant to initialize a subarray. */
8350 if (value.value != 0
8351 && eltcode == ARRAY_TYPE
8352 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8353 && string_flag)
8354 value.value = orig_value;
8355 /* Otherwise, if we have come to a subaggregate,
8356 and we don't have an element of its type, push into it. */
8357 else if (value.value != 0
8358 && value.value != error_mark_node
8359 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8360 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8361 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8363 push_init_level (1, braced_init_obstack);
8364 continue;
8367 if (constructor_max_index != 0
8368 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8369 || integer_all_onesp (constructor_max_index)))
8371 pedwarn_init (input_location, 0,
8372 "excess elements in array initializer");
8373 break;
8376 /* Now output the actual element. */
8377 if (value.value)
8379 push_array_bounds (tree_low_cst (constructor_index, 1));
8380 output_init_element (value.value, value.original_type,
8381 strict_string, elttype,
8382 constructor_index, 1, implicit,
8383 braced_init_obstack);
8384 RESTORE_SPELLING_DEPTH (constructor_depth);
8387 constructor_index
8388 = size_binop_loc (input_location, PLUS_EXPR,
8389 constructor_index, bitsize_one_node);
8391 if (!value.value)
8392 /* If we are doing the bookkeeping for an element that was
8393 directly output as a constructor, we must update
8394 constructor_unfilled_index. */
8395 constructor_unfilled_index = constructor_index;
8397 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8399 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8401 /* Do a basic check of initializer size. Note that vectors
8402 always have a fixed size derived from their type. */
8403 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8405 pedwarn_init (input_location, 0,
8406 "excess elements in vector initializer");
8407 break;
8410 /* Now output the actual element. */
8411 if (value.value)
8413 if (TREE_CODE (value.value) == VECTOR_CST)
8414 elttype = TYPE_MAIN_VARIANT (constructor_type);
8415 output_init_element (value.value, value.original_type,
8416 strict_string, elttype,
8417 constructor_index, 1, implicit,
8418 braced_init_obstack);
8421 constructor_index
8422 = size_binop_loc (input_location,
8423 PLUS_EXPR, constructor_index, bitsize_one_node);
8425 if (!value.value)
8426 /* If we are doing the bookkeeping for an element that was
8427 directly output as a constructor, we must update
8428 constructor_unfilled_index. */
8429 constructor_unfilled_index = constructor_index;
8432 /* Handle the sole element allowed in a braced initializer
8433 for a scalar variable. */
8434 else if (constructor_type != error_mark_node
8435 && constructor_fields == 0)
8437 pedwarn_init (input_location, 0,
8438 "excess elements in scalar initializer");
8439 break;
8441 else
8443 if (value.value)
8444 output_init_element (value.value, value.original_type,
8445 strict_string, constructor_type,
8446 NULL_TREE, 1, implicit,
8447 braced_init_obstack);
8448 constructor_fields = 0;
8451 /* Handle range initializers either at this level or anywhere higher
8452 in the designator stack. */
8453 if (constructor_range_stack)
8455 struct constructor_range_stack *p, *range_stack;
8456 int finish = 0;
8458 range_stack = constructor_range_stack;
8459 constructor_range_stack = 0;
8460 while (constructor_stack != range_stack->stack)
8462 gcc_assert (constructor_stack->implicit);
8463 process_init_element (pop_init_level (1,
8464 braced_init_obstack),
8465 true, braced_init_obstack);
8467 for (p = range_stack;
8468 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8469 p = p->prev)
8471 gcc_assert (constructor_stack->implicit);
8472 process_init_element (pop_init_level (1, braced_init_obstack),
8473 true, braced_init_obstack);
8476 p->index = size_binop_loc (input_location,
8477 PLUS_EXPR, p->index, bitsize_one_node);
8478 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8479 finish = 1;
8481 while (1)
8483 constructor_index = p->index;
8484 constructor_fields = p->fields;
8485 if (finish && p->range_end && p->index == p->range_start)
8487 finish = 0;
8488 p->prev = 0;
8490 p = p->next;
8491 if (!p)
8492 break;
8493 push_init_level (2, braced_init_obstack);
8494 p->stack = constructor_stack;
8495 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8496 p->index = p->range_start;
8499 if (!finish)
8500 constructor_range_stack = range_stack;
8501 continue;
8504 break;
8507 constructor_range_stack = 0;
8510 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8511 (guaranteed to be 'volatile' or null) and ARGS (represented using
8512 an ASM_EXPR node). */
8513 tree
8514 build_asm_stmt (tree cv_qualifier, tree args)
8516 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8517 ASM_VOLATILE_P (args) = 1;
8518 return add_stmt (args);
8521 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8522 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8523 SIMPLE indicates whether there was anything at all after the
8524 string in the asm expression -- asm("blah") and asm("blah" : )
8525 are subtly different. We use a ASM_EXPR node to represent this. */
8526 tree
8527 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8528 tree clobbers, tree labels, bool simple)
8530 tree tail;
8531 tree args;
8532 int i;
8533 const char *constraint;
8534 const char **oconstraints;
8535 bool allows_mem, allows_reg, is_inout;
8536 int ninputs, noutputs;
8538 ninputs = list_length (inputs);
8539 noutputs = list_length (outputs);
8540 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8542 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8544 /* Remove output conversions that change the type but not the mode. */
8545 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8547 tree output = TREE_VALUE (tail);
8549 /* ??? Really, this should not be here. Users should be using a
8550 proper lvalue, dammit. But there's a long history of using casts
8551 in the output operands. In cases like longlong.h, this becomes a
8552 primitive form of typechecking -- if the cast can be removed, then
8553 the output operand had a type of the proper width; otherwise we'll
8554 get an error. Gross, but ... */
8555 STRIP_NOPS (output);
8557 if (!lvalue_or_else (output, lv_asm))
8558 output = error_mark_node;
8560 if (output != error_mark_node
8561 && (TREE_READONLY (output)
8562 || TYPE_READONLY (TREE_TYPE (output))
8563 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8564 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8565 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8566 readonly_error (output, lv_asm);
8568 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8569 oconstraints[i] = constraint;
8571 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8572 &allows_mem, &allows_reg, &is_inout))
8574 /* If the operand is going to end up in memory,
8575 mark it addressable. */
8576 if (!allows_reg && !c_mark_addressable (output))
8577 output = error_mark_node;
8579 else
8580 output = error_mark_node;
8582 TREE_VALUE (tail) = output;
8585 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8587 tree input;
8589 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8590 input = TREE_VALUE (tail);
8592 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8593 oconstraints, &allows_mem, &allows_reg))
8595 /* If the operand is going to end up in memory,
8596 mark it addressable. */
8597 if (!allows_reg && allows_mem)
8599 /* Strip the nops as we allow this case. FIXME, this really
8600 should be rejected or made deprecated. */
8601 STRIP_NOPS (input);
8602 if (!c_mark_addressable (input))
8603 input = error_mark_node;
8606 else
8607 input = error_mark_node;
8609 TREE_VALUE (tail) = input;
8612 /* ASMs with labels cannot have outputs. This should have been
8613 enforced by the parser. */
8614 gcc_assert (outputs == NULL || labels == NULL);
8616 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8618 /* asm statements without outputs, including simple ones, are treated
8619 as volatile. */
8620 ASM_INPUT_P (args) = simple;
8621 ASM_VOLATILE_P (args) = (noutputs == 0);
8623 return args;
8626 /* Generate a goto statement to LABEL. LOC is the location of the
8627 GOTO. */
8629 tree
8630 c_finish_goto_label (location_t loc, tree label)
8632 tree decl = lookup_label_for_goto (loc, label);
8633 if (!decl)
8634 return NULL_TREE;
8635 TREE_USED (decl) = 1;
8637 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8638 SET_EXPR_LOCATION (t, loc);
8639 return add_stmt (t);
8643 /* Generate a computed goto statement to EXPR. LOC is the location of
8644 the GOTO. */
8646 tree
8647 c_finish_goto_ptr (location_t loc, tree expr)
8649 tree t;
8650 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8651 expr = c_fully_fold (expr, false, NULL);
8652 expr = convert (ptr_type_node, expr);
8653 t = build1 (GOTO_EXPR, void_type_node, expr);
8654 SET_EXPR_LOCATION (t, loc);
8655 return add_stmt (t);
8658 /* Generate a C `return' statement. RETVAL is the expression for what
8659 to return, or a null pointer for `return;' with no value. LOC is
8660 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8661 is the original type of RETVAL. */
8663 tree
8664 c_finish_return (location_t loc, tree retval, tree origtype)
8666 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8667 bool no_warning = false;
8668 bool npc = false;
8670 if (TREE_THIS_VOLATILE (current_function_decl))
8671 warning_at (loc, 0,
8672 "function declared %<noreturn%> has a %<return%> statement");
8674 if (retval)
8676 tree semantic_type = NULL_TREE;
8677 npc = null_pointer_constant_p (retval);
8678 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8680 semantic_type = TREE_TYPE (retval);
8681 retval = TREE_OPERAND (retval, 0);
8683 retval = c_fully_fold (retval, false, NULL);
8684 if (semantic_type)
8685 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8688 if (!retval)
8690 current_function_returns_null = 1;
8691 if ((warn_return_type || flag_isoc99)
8692 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8694 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8695 "%<return%> with no value, in "
8696 "function returning non-void");
8697 no_warning = true;
8700 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8702 current_function_returns_null = 1;
8703 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8704 pedwarn (loc, 0,
8705 "%<return%> with a value, in function returning void");
8706 else
8707 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8708 "%<return%> with expression, in function returning void");
8710 else
8712 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8713 ic_return,
8714 npc, NULL_TREE, NULL_TREE, 0);
8715 tree res = DECL_RESULT (current_function_decl);
8716 tree inner;
8718 current_function_returns_value = 1;
8719 if (t == error_mark_node)
8720 return NULL_TREE;
8722 inner = t = convert (TREE_TYPE (res), t);
8724 /* Strip any conversions, additions, and subtractions, and see if
8725 we are returning the address of a local variable. Warn if so. */
8726 while (1)
8728 switch (TREE_CODE (inner))
8730 CASE_CONVERT:
8731 case NON_LVALUE_EXPR:
8732 case PLUS_EXPR:
8733 case POINTER_PLUS_EXPR:
8734 inner = TREE_OPERAND (inner, 0);
8735 continue;
8737 case MINUS_EXPR:
8738 /* If the second operand of the MINUS_EXPR has a pointer
8739 type (or is converted from it), this may be valid, so
8740 don't give a warning. */
8742 tree op1 = TREE_OPERAND (inner, 1);
8744 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8745 && (CONVERT_EXPR_P (op1)
8746 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8747 op1 = TREE_OPERAND (op1, 0);
8749 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8750 break;
8752 inner = TREE_OPERAND (inner, 0);
8753 continue;
8756 case ADDR_EXPR:
8757 inner = TREE_OPERAND (inner, 0);
8759 while (REFERENCE_CLASS_P (inner)
8760 && TREE_CODE (inner) != INDIRECT_REF)
8761 inner = TREE_OPERAND (inner, 0);
8763 if (DECL_P (inner)
8764 && !DECL_EXTERNAL (inner)
8765 && !TREE_STATIC (inner)
8766 && DECL_CONTEXT (inner) == current_function_decl)
8767 warning_at (loc,
8768 0, "function returns address of local variable");
8769 break;
8771 default:
8772 break;
8775 break;
8778 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8779 SET_EXPR_LOCATION (retval, loc);
8781 if (warn_sequence_point)
8782 verify_sequence_points (retval);
8785 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8786 TREE_NO_WARNING (ret_stmt) |= no_warning;
8787 return add_stmt (ret_stmt);
8790 struct c_switch {
8791 /* The SWITCH_EXPR being built. */
8792 tree switch_expr;
8794 /* The original type of the testing expression, i.e. before the
8795 default conversion is applied. */
8796 tree orig_type;
8798 /* A splay-tree mapping the low element of a case range to the high
8799 element, or NULL_TREE if there is no high element. Used to
8800 determine whether or not a new case label duplicates an old case
8801 label. We need a tree, rather than simply a hash table, because
8802 of the GNU case range extension. */
8803 splay_tree cases;
8805 /* The bindings at the point of the switch. This is used for
8806 warnings crossing decls when branching to a case label. */
8807 struct c_spot_bindings *bindings;
8809 /* The next node on the stack. */
8810 struct c_switch *next;
8813 /* A stack of the currently active switch statements. The innermost
8814 switch statement is on the top of the stack. There is no need to
8815 mark the stack for garbage collection because it is only active
8816 during the processing of the body of a function, and we never
8817 collect at that point. */
8819 struct c_switch *c_switch_stack;
8821 /* Start a C switch statement, testing expression EXP. Return the new
8822 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8823 SWITCH_COND_LOC is the location of the switch's condition. */
8825 tree
8826 c_start_case (location_t switch_loc,
8827 location_t switch_cond_loc,
8828 tree exp)
8830 tree orig_type = error_mark_node;
8831 struct c_switch *cs;
8833 if (exp != error_mark_node)
8835 orig_type = TREE_TYPE (exp);
8837 if (!INTEGRAL_TYPE_P (orig_type))
8839 if (orig_type != error_mark_node)
8841 error_at (switch_cond_loc, "switch quantity not an integer");
8842 orig_type = error_mark_node;
8844 exp = integer_zero_node;
8846 else
8848 tree type = TYPE_MAIN_VARIANT (orig_type);
8850 if (!in_system_header
8851 && (type == long_integer_type_node
8852 || type == long_unsigned_type_node))
8853 warning_at (switch_cond_loc,
8854 OPT_Wtraditional, "%<long%> switch expression not "
8855 "converted to %<int%> in ISO C");
8857 exp = c_fully_fold (exp, false, NULL);
8858 exp = default_conversion (exp);
8860 if (warn_sequence_point)
8861 verify_sequence_points (exp);
8865 /* Add this new SWITCH_EXPR to the stack. */
8866 cs = XNEW (struct c_switch);
8867 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8868 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8869 cs->orig_type = orig_type;
8870 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8871 cs->bindings = c_get_switch_bindings ();
8872 cs->next = c_switch_stack;
8873 c_switch_stack = cs;
8875 return add_stmt (cs->switch_expr);
8878 /* Process a case label at location LOC. */
8880 tree
8881 do_case (location_t loc, tree low_value, tree high_value)
8883 tree label = NULL_TREE;
8885 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8887 low_value = c_fully_fold (low_value, false, NULL);
8888 if (TREE_CODE (low_value) == INTEGER_CST)
8889 pedwarn (input_location, OPT_pedantic,
8890 "case label is not an integer constant expression");
8893 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8895 high_value = c_fully_fold (high_value, false, NULL);
8896 if (TREE_CODE (high_value) == INTEGER_CST)
8897 pedwarn (input_location, OPT_pedantic,
8898 "case label is not an integer constant expression");
8901 if (c_switch_stack == NULL)
8903 if (low_value)
8904 error_at (loc, "case label not within a switch statement");
8905 else
8906 error_at (loc, "%<default%> label not within a switch statement");
8907 return NULL_TREE;
8910 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8911 EXPR_LOCATION (c_switch_stack->switch_expr),
8912 loc))
8913 return NULL_TREE;
8915 label = c_add_case_label (loc, c_switch_stack->cases,
8916 SWITCH_COND (c_switch_stack->switch_expr),
8917 c_switch_stack->orig_type,
8918 low_value, high_value);
8919 if (label == error_mark_node)
8920 label = NULL_TREE;
8921 return label;
8924 /* Finish the switch statement. */
8926 void
8927 c_finish_case (tree body)
8929 struct c_switch *cs = c_switch_stack;
8930 location_t switch_location;
8932 SWITCH_BODY (cs->switch_expr) = body;
8934 /* Emit warnings as needed. */
8935 switch_location = EXPR_LOCATION (cs->switch_expr);
8936 c_do_switch_warnings (cs->cases, switch_location,
8937 TREE_TYPE (cs->switch_expr),
8938 SWITCH_COND (cs->switch_expr));
8940 /* Pop the stack. */
8941 c_switch_stack = cs->next;
8942 splay_tree_delete (cs->cases);
8943 c_release_switch_bindings (cs->bindings);
8944 XDELETE (cs);
8947 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8948 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8949 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8950 statement, and was not surrounded with parenthesis. */
8952 void
8953 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8954 tree else_block, bool nested_if)
8956 tree stmt;
8958 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8959 if (warn_parentheses && nested_if && else_block == NULL)
8961 tree inner_if = then_block;
8963 /* We know from the grammar productions that there is an IF nested
8964 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8965 it might not be exactly THEN_BLOCK, but should be the last
8966 non-container statement within. */
8967 while (1)
8968 switch (TREE_CODE (inner_if))
8970 case COND_EXPR:
8971 goto found;
8972 case BIND_EXPR:
8973 inner_if = BIND_EXPR_BODY (inner_if);
8974 break;
8975 case STATEMENT_LIST:
8976 inner_if = expr_last (then_block);
8977 break;
8978 case TRY_FINALLY_EXPR:
8979 case TRY_CATCH_EXPR:
8980 inner_if = TREE_OPERAND (inner_if, 0);
8981 break;
8982 default:
8983 gcc_unreachable ();
8985 found:
8987 if (COND_EXPR_ELSE (inner_if))
8988 warning_at (if_locus, OPT_Wparentheses,
8989 "suggest explicit braces to avoid ambiguous %<else%>");
8992 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8993 SET_EXPR_LOCATION (stmt, if_locus);
8994 add_stmt (stmt);
8997 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8998 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8999 is false for DO loops. INCR is the FOR increment expression. BODY is
9000 the statement controlled by the loop. BLAB is the break label. CLAB is
9001 the continue label. Everything is allowed to be NULL. */
9003 void
9004 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9005 tree blab, tree clab, bool cond_is_first)
9007 tree entry = NULL, exit = NULL, t;
9009 /* If the condition is zero don't generate a loop construct. */
9010 if (cond && integer_zerop (cond))
9012 if (cond_is_first)
9014 t = build_and_jump (&blab);
9015 SET_EXPR_LOCATION (t, start_locus);
9016 add_stmt (t);
9019 else
9021 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9023 /* If we have an exit condition, then we build an IF with gotos either
9024 out of the loop, or to the top of it. If there's no exit condition,
9025 then we just build a jump back to the top. */
9026 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9028 if (cond && !integer_nonzerop (cond))
9030 /* Canonicalize the loop condition to the end. This means
9031 generating a branch to the loop condition. Reuse the
9032 continue label, if possible. */
9033 if (cond_is_first)
9035 if (incr || !clab)
9037 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9038 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9040 else
9041 t = build1 (GOTO_EXPR, void_type_node, clab);
9042 SET_EXPR_LOCATION (t, start_locus);
9043 add_stmt (t);
9046 t = build_and_jump (&blab);
9047 if (cond_is_first)
9048 exit = fold_build3_loc (start_locus,
9049 COND_EXPR, void_type_node, cond, exit, t);
9050 else
9051 exit = fold_build3_loc (input_location,
9052 COND_EXPR, void_type_node, cond, exit, t);
9055 add_stmt (top);
9058 if (body)
9059 add_stmt (body);
9060 if (clab)
9061 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9062 if (incr)
9063 add_stmt (incr);
9064 if (entry)
9065 add_stmt (entry);
9066 if (exit)
9067 add_stmt (exit);
9068 if (blab)
9069 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9072 tree
9073 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9075 bool skip;
9076 tree label = *label_p;
9078 /* In switch statements break is sometimes stylistically used after
9079 a return statement. This can lead to spurious warnings about
9080 control reaching the end of a non-void function when it is
9081 inlined. Note that we are calling block_may_fallthru with
9082 language specific tree nodes; this works because
9083 block_may_fallthru returns true when given something it does not
9084 understand. */
9085 skip = !block_may_fallthru (cur_stmt_list);
9087 if (!label)
9089 if (!skip)
9090 *label_p = label = create_artificial_label (loc);
9092 else if (TREE_CODE (label) == LABEL_DECL)
9094 else switch (TREE_INT_CST_LOW (label))
9096 case 0:
9097 if (is_break)
9098 error_at (loc, "break statement not within loop or switch");
9099 else
9100 error_at (loc, "continue statement not within a loop");
9101 return NULL_TREE;
9103 case 1:
9104 gcc_assert (is_break);
9105 error_at (loc, "break statement used with OpenMP for loop");
9106 return NULL_TREE;
9108 default:
9109 gcc_unreachable ();
9112 if (skip)
9113 return NULL_TREE;
9115 if (!is_break)
9116 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9118 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9121 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9123 static void
9124 emit_side_effect_warnings (location_t loc, tree expr)
9126 if (expr == error_mark_node)
9128 else if (!TREE_SIDE_EFFECTS (expr))
9130 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9131 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9133 else
9134 warn_if_unused_value (expr, loc);
9137 /* Process an expression as if it were a complete statement. Emit
9138 diagnostics, but do not call ADD_STMT. LOC is the location of the
9139 statement. */
9141 tree
9142 c_process_expr_stmt (location_t loc, tree expr)
9144 tree exprv;
9146 if (!expr)
9147 return NULL_TREE;
9149 expr = c_fully_fold (expr, false, NULL);
9151 if (warn_sequence_point)
9152 verify_sequence_points (expr);
9154 if (TREE_TYPE (expr) != error_mark_node
9155 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9156 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9157 error_at (loc, "expression statement has incomplete type");
9159 /* If we're not processing a statement expression, warn about unused values.
9160 Warnings for statement expressions will be emitted later, once we figure
9161 out which is the result. */
9162 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9163 && warn_unused_value)
9164 emit_side_effect_warnings (loc, expr);
9166 exprv = expr;
9167 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9168 exprv = TREE_OPERAND (exprv, 1);
9169 if (DECL_P (exprv) || handled_component_p (exprv))
9170 mark_exp_read (exprv);
9172 /* If the expression is not of a type to which we cannot assign a line
9173 number, wrap the thing in a no-op NOP_EXPR. */
9174 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9176 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9177 SET_EXPR_LOCATION (expr, loc);
9180 return expr;
9183 /* Emit an expression as a statement. LOC is the location of the
9184 expression. */
9186 tree
9187 c_finish_expr_stmt (location_t loc, tree expr)
9189 if (expr)
9190 return add_stmt (c_process_expr_stmt (loc, expr));
9191 else
9192 return NULL;
9195 /* Do the opposite and emit a statement as an expression. To begin,
9196 create a new binding level and return it. */
9198 tree
9199 c_begin_stmt_expr (void)
9201 tree ret;
9203 /* We must force a BLOCK for this level so that, if it is not expanded
9204 later, there is a way to turn off the entire subtree of blocks that
9205 are contained in it. */
9206 keep_next_level ();
9207 ret = c_begin_compound_stmt (true);
9209 c_bindings_start_stmt_expr (c_switch_stack == NULL
9210 ? NULL
9211 : c_switch_stack->bindings);
9213 /* Mark the current statement list as belonging to a statement list. */
9214 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9216 return ret;
9219 /* LOC is the location of the compound statement to which this body
9220 belongs. */
9222 tree
9223 c_finish_stmt_expr (location_t loc, tree body)
9225 tree last, type, tmp, val;
9226 tree *last_p;
9228 body = c_end_compound_stmt (loc, body, true);
9230 c_bindings_end_stmt_expr (c_switch_stack == NULL
9231 ? NULL
9232 : c_switch_stack->bindings);
9234 /* Locate the last statement in BODY. See c_end_compound_stmt
9235 about always returning a BIND_EXPR. */
9236 last_p = &BIND_EXPR_BODY (body);
9237 last = BIND_EXPR_BODY (body);
9239 continue_searching:
9240 if (TREE_CODE (last) == STATEMENT_LIST)
9242 tree_stmt_iterator i;
9244 /* This can happen with degenerate cases like ({ }). No value. */
9245 if (!TREE_SIDE_EFFECTS (last))
9246 return body;
9248 /* If we're supposed to generate side effects warnings, process
9249 all of the statements except the last. */
9250 if (warn_unused_value)
9252 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9254 location_t tloc;
9255 tree t = tsi_stmt (i);
9257 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9258 emit_side_effect_warnings (tloc, t);
9261 else
9262 i = tsi_last (last);
9263 last_p = tsi_stmt_ptr (i);
9264 last = *last_p;
9267 /* If the end of the list is exception related, then the list was split
9268 by a call to push_cleanup. Continue searching. */
9269 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9270 || TREE_CODE (last) == TRY_CATCH_EXPR)
9272 last_p = &TREE_OPERAND (last, 0);
9273 last = *last_p;
9274 goto continue_searching;
9277 if (last == error_mark_node)
9278 return last;
9280 /* In the case that the BIND_EXPR is not necessary, return the
9281 expression out from inside it. */
9282 if (last == BIND_EXPR_BODY (body)
9283 && BIND_EXPR_VARS (body) == NULL)
9285 /* Even if this looks constant, do not allow it in a constant
9286 expression. */
9287 last = c_wrap_maybe_const (last, true);
9288 /* Do not warn if the return value of a statement expression is
9289 unused. */
9290 TREE_NO_WARNING (last) = 1;
9291 return last;
9294 /* Extract the type of said expression. */
9295 type = TREE_TYPE (last);
9297 /* If we're not returning a value at all, then the BIND_EXPR that
9298 we already have is a fine expression to return. */
9299 if (!type || VOID_TYPE_P (type))
9300 return body;
9302 /* Now that we've located the expression containing the value, it seems
9303 silly to make voidify_wrapper_expr repeat the process. Create a
9304 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9305 tmp = create_tmp_var_raw (type, NULL);
9307 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9308 tree_expr_nonnegative_p giving up immediately. */
9309 val = last;
9310 if (TREE_CODE (val) == NOP_EXPR
9311 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9312 val = TREE_OPERAND (val, 0);
9314 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9315 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9318 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9319 SET_EXPR_LOCATION (t, loc);
9320 return t;
9324 /* Begin and end compound statements. This is as simple as pushing
9325 and popping new statement lists from the tree. */
9327 tree
9328 c_begin_compound_stmt (bool do_scope)
9330 tree stmt = push_stmt_list ();
9331 if (do_scope)
9332 push_scope ();
9333 return stmt;
9336 /* End a compound statement. STMT is the statement. LOC is the
9337 location of the compound statement-- this is usually the location
9338 of the opening brace. */
9340 tree
9341 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9343 tree block = NULL;
9345 if (do_scope)
9347 if (c_dialect_objc ())
9348 objc_clear_super_receiver ();
9349 block = pop_scope ();
9352 stmt = pop_stmt_list (stmt);
9353 stmt = c_build_bind_expr (loc, block, stmt);
9355 /* If this compound statement is nested immediately inside a statement
9356 expression, then force a BIND_EXPR to be created. Otherwise we'll
9357 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9358 STATEMENT_LISTs merge, and thus we can lose track of what statement
9359 was really last. */
9360 if (cur_stmt_list
9361 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9362 && TREE_CODE (stmt) != BIND_EXPR)
9364 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9365 TREE_SIDE_EFFECTS (stmt) = 1;
9366 SET_EXPR_LOCATION (stmt, loc);
9369 return stmt;
9372 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9373 when the current scope is exited. EH_ONLY is true when this is not
9374 meant to apply to normal control flow transfer. */
9376 void
9377 push_cleanup (tree decl, tree cleanup, bool eh_only)
9379 enum tree_code code;
9380 tree stmt, list;
9381 bool stmt_expr;
9383 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9384 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9385 add_stmt (stmt);
9386 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9387 list = push_stmt_list ();
9388 TREE_OPERAND (stmt, 0) = list;
9389 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9392 /* Build a binary-operation expression without default conversions.
9393 CODE is the kind of expression to build.
9394 LOCATION is the operator's location.
9395 This function differs from `build' in several ways:
9396 the data type of the result is computed and recorded in it,
9397 warnings are generated if arg data types are invalid,
9398 special handling for addition and subtraction of pointers is known,
9399 and some optimization is done (operations on narrow ints
9400 are done in the narrower type when that gives the same result).
9401 Constant folding is also done before the result is returned.
9403 Note that the operands will never have enumeral types, or function
9404 or array types, because either they will have the default conversions
9405 performed or they have both just been converted to some other type in which
9406 the arithmetic is to be done. */
9408 tree
9409 build_binary_op (location_t location, enum tree_code code,
9410 tree orig_op0, tree orig_op1, int convert_p)
9412 tree type0, type1, orig_type0, orig_type1;
9413 tree eptype;
9414 enum tree_code code0, code1;
9415 tree op0, op1;
9416 tree ret = error_mark_node;
9417 const char *invalid_op_diag;
9418 bool op0_int_operands, op1_int_operands;
9419 bool int_const, int_const_or_overflow, int_operands;
9421 /* Expression code to give to the expression when it is built.
9422 Normally this is CODE, which is what the caller asked for,
9423 but in some special cases we change it. */
9424 enum tree_code resultcode = code;
9426 /* Data type in which the computation is to be performed.
9427 In the simplest cases this is the common type of the arguments. */
9428 tree result_type = NULL;
9430 /* When the computation is in excess precision, the type of the
9431 final EXCESS_PRECISION_EXPR. */
9432 tree semantic_result_type = NULL;
9434 /* Nonzero means operands have already been type-converted
9435 in whatever way is necessary.
9436 Zero means they need to be converted to RESULT_TYPE. */
9437 int converted = 0;
9439 /* Nonzero means create the expression with this type, rather than
9440 RESULT_TYPE. */
9441 tree build_type = 0;
9443 /* Nonzero means after finally constructing the expression
9444 convert it to this type. */
9445 tree final_type = 0;
9447 /* Nonzero if this is an operation like MIN or MAX which can
9448 safely be computed in short if both args are promoted shorts.
9449 Also implies COMMON.
9450 -1 indicates a bitwise operation; this makes a difference
9451 in the exact conditions for when it is safe to do the operation
9452 in a narrower mode. */
9453 int shorten = 0;
9455 /* Nonzero if this is a comparison operation;
9456 if both args are promoted shorts, compare the original shorts.
9457 Also implies COMMON. */
9458 int short_compare = 0;
9460 /* Nonzero if this is a right-shift operation, which can be computed on the
9461 original short and then promoted if the operand is a promoted short. */
9462 int short_shift = 0;
9464 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9465 int common = 0;
9467 /* True means types are compatible as far as ObjC is concerned. */
9468 bool objc_ok;
9470 /* True means this is an arithmetic operation that may need excess
9471 precision. */
9472 bool may_need_excess_precision;
9474 /* True means this is a boolean operation that converts both its
9475 operands to truth-values. */
9476 bool boolean_op = false;
9478 if (location == UNKNOWN_LOCATION)
9479 location = input_location;
9481 op0 = orig_op0;
9482 op1 = orig_op1;
9484 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9485 if (op0_int_operands)
9486 op0 = remove_c_maybe_const_expr (op0);
9487 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9488 if (op1_int_operands)
9489 op1 = remove_c_maybe_const_expr (op1);
9490 int_operands = (op0_int_operands && op1_int_operands);
9491 if (int_operands)
9493 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9494 && TREE_CODE (orig_op1) == INTEGER_CST);
9495 int_const = (int_const_or_overflow
9496 && !TREE_OVERFLOW (orig_op0)
9497 && !TREE_OVERFLOW (orig_op1));
9499 else
9500 int_const = int_const_or_overflow = false;
9502 if (convert_p)
9504 op0 = default_conversion (op0);
9505 op1 = default_conversion (op1);
9508 orig_type0 = type0 = TREE_TYPE (op0);
9509 orig_type1 = type1 = TREE_TYPE (op1);
9511 /* The expression codes of the data types of the arguments tell us
9512 whether the arguments are integers, floating, pointers, etc. */
9513 code0 = TREE_CODE (type0);
9514 code1 = TREE_CODE (type1);
9516 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9517 STRIP_TYPE_NOPS (op0);
9518 STRIP_TYPE_NOPS (op1);
9520 /* If an error was already reported for one of the arguments,
9521 avoid reporting another error. */
9523 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9524 return error_mark_node;
9526 if ((invalid_op_diag
9527 = targetm.invalid_binary_op (code, type0, type1)))
9529 error_at (location, invalid_op_diag);
9530 return error_mark_node;
9533 switch (code)
9535 case PLUS_EXPR:
9536 case MINUS_EXPR:
9537 case MULT_EXPR:
9538 case TRUNC_DIV_EXPR:
9539 case CEIL_DIV_EXPR:
9540 case FLOOR_DIV_EXPR:
9541 case ROUND_DIV_EXPR:
9542 case EXACT_DIV_EXPR:
9543 may_need_excess_precision = true;
9544 break;
9545 default:
9546 may_need_excess_precision = false;
9547 break;
9549 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9551 op0 = TREE_OPERAND (op0, 0);
9552 type0 = TREE_TYPE (op0);
9554 else if (may_need_excess_precision
9555 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9557 type0 = eptype;
9558 op0 = convert (eptype, op0);
9560 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9562 op1 = TREE_OPERAND (op1, 0);
9563 type1 = TREE_TYPE (op1);
9565 else if (may_need_excess_precision
9566 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9568 type1 = eptype;
9569 op1 = convert (eptype, op1);
9572 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9574 switch (code)
9576 case PLUS_EXPR:
9577 /* Handle the pointer + int case. */
9578 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9580 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9581 goto return_build_binary_op;
9583 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9585 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9586 goto return_build_binary_op;
9588 else
9589 common = 1;
9590 break;
9592 case MINUS_EXPR:
9593 /* Subtraction of two similar pointers.
9594 We must subtract them as integers, then divide by object size. */
9595 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9596 && comp_target_types (location, type0, type1))
9598 ret = pointer_diff (location, op0, op1);
9599 goto return_build_binary_op;
9601 /* Handle pointer minus int. Just like pointer plus int. */
9602 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9604 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9605 goto return_build_binary_op;
9607 else
9608 common = 1;
9609 break;
9611 case MULT_EXPR:
9612 common = 1;
9613 break;
9615 case TRUNC_DIV_EXPR:
9616 case CEIL_DIV_EXPR:
9617 case FLOOR_DIV_EXPR:
9618 case ROUND_DIV_EXPR:
9619 case EXACT_DIV_EXPR:
9620 warn_for_div_by_zero (location, op1);
9622 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9623 || code0 == FIXED_POINT_TYPE
9624 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9625 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9626 || code1 == FIXED_POINT_TYPE
9627 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9629 enum tree_code tcode0 = code0, tcode1 = code1;
9631 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9632 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9633 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9634 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9636 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9637 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9638 resultcode = RDIV_EXPR;
9639 else
9640 /* Although it would be tempting to shorten always here, that
9641 loses on some targets, since the modulo instruction is
9642 undefined if the quotient can't be represented in the
9643 computation mode. We shorten only if unsigned or if
9644 dividing by something we know != -1. */
9645 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9646 || (TREE_CODE (op1) == INTEGER_CST
9647 && !integer_all_onesp (op1)));
9648 common = 1;
9650 break;
9652 case BIT_AND_EXPR:
9653 case BIT_IOR_EXPR:
9654 case BIT_XOR_EXPR:
9655 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9656 shorten = -1;
9657 /* Allow vector types which are not floating point types. */
9658 else if (code0 == VECTOR_TYPE
9659 && code1 == VECTOR_TYPE
9660 && !VECTOR_FLOAT_TYPE_P (type0)
9661 && !VECTOR_FLOAT_TYPE_P (type1))
9662 common = 1;
9663 break;
9665 case TRUNC_MOD_EXPR:
9666 case FLOOR_MOD_EXPR:
9667 warn_for_div_by_zero (location, op1);
9669 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9670 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9671 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9672 common = 1;
9673 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9675 /* Although it would be tempting to shorten always here, that loses
9676 on some targets, since the modulo instruction is undefined if the
9677 quotient can't be represented in the computation mode. We shorten
9678 only if unsigned or if dividing by something we know != -1. */
9679 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9680 || (TREE_CODE (op1) == INTEGER_CST
9681 && !integer_all_onesp (op1)));
9682 common = 1;
9684 break;
9686 case TRUTH_ANDIF_EXPR:
9687 case TRUTH_ORIF_EXPR:
9688 case TRUTH_AND_EXPR:
9689 case TRUTH_OR_EXPR:
9690 case TRUTH_XOR_EXPR:
9691 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9692 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9693 || code0 == FIXED_POINT_TYPE)
9694 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9695 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9696 || code1 == FIXED_POINT_TYPE))
9698 /* Result of these operations is always an int,
9699 but that does not mean the operands should be
9700 converted to ints! */
9701 result_type = integer_type_node;
9702 op0 = c_common_truthvalue_conversion (location, op0);
9703 op1 = c_common_truthvalue_conversion (location, op1);
9704 converted = 1;
9705 boolean_op = true;
9707 if (code == TRUTH_ANDIF_EXPR)
9709 int_const_or_overflow = (int_operands
9710 && TREE_CODE (orig_op0) == INTEGER_CST
9711 && (op0 == truthvalue_false_node
9712 || TREE_CODE (orig_op1) == INTEGER_CST));
9713 int_const = (int_const_or_overflow
9714 && !TREE_OVERFLOW (orig_op0)
9715 && (op0 == truthvalue_false_node
9716 || !TREE_OVERFLOW (orig_op1)));
9718 else if (code == TRUTH_ORIF_EXPR)
9720 int_const_or_overflow = (int_operands
9721 && TREE_CODE (orig_op0) == INTEGER_CST
9722 && (op0 == truthvalue_true_node
9723 || TREE_CODE (orig_op1) == INTEGER_CST));
9724 int_const = (int_const_or_overflow
9725 && !TREE_OVERFLOW (orig_op0)
9726 && (op0 == truthvalue_true_node
9727 || !TREE_OVERFLOW (orig_op1)));
9729 break;
9731 /* Shift operations: result has same type as first operand;
9732 always convert second operand to int.
9733 Also set SHORT_SHIFT if shifting rightward. */
9735 case RSHIFT_EXPR:
9736 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9737 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9739 result_type = type0;
9740 converted = 1;
9742 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9743 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9744 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9745 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9747 result_type = type0;
9748 converted = 1;
9750 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9751 && code1 == INTEGER_TYPE)
9753 if (TREE_CODE (op1) == INTEGER_CST)
9755 if (tree_int_cst_sgn (op1) < 0)
9757 int_const = false;
9758 if (c_inhibit_evaluation_warnings == 0)
9759 warning (0, "right shift count is negative");
9761 else
9763 if (!integer_zerop (op1))
9764 short_shift = 1;
9766 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9768 int_const = false;
9769 if (c_inhibit_evaluation_warnings == 0)
9770 warning (0, "right shift count >= width of type");
9775 /* Use the type of the value to be shifted. */
9776 result_type = type0;
9777 /* Convert the non vector shift-count to an integer, regardless
9778 of size of value being shifted. */
9779 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9780 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9781 op1 = convert (integer_type_node, op1);
9782 /* Avoid converting op1 to result_type later. */
9783 converted = 1;
9785 break;
9787 case LSHIFT_EXPR:
9788 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9789 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9791 result_type = type0;
9792 converted = 1;
9794 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9795 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9796 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9797 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9799 result_type = type0;
9800 converted = 1;
9802 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9803 && code1 == INTEGER_TYPE)
9805 if (TREE_CODE (op1) == INTEGER_CST)
9807 if (tree_int_cst_sgn (op1) < 0)
9809 int_const = false;
9810 if (c_inhibit_evaluation_warnings == 0)
9811 warning (0, "left shift count is negative");
9814 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9816 int_const = false;
9817 if (c_inhibit_evaluation_warnings == 0)
9818 warning (0, "left shift count >= width of type");
9822 /* Use the type of the value to be shifted. */
9823 result_type = type0;
9824 /* Convert the non vector shift-count to an integer, regardless
9825 of size of value being shifted. */
9826 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9827 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9828 op1 = convert (integer_type_node, op1);
9829 /* Avoid converting op1 to result_type later. */
9830 converted = 1;
9832 break;
9834 case EQ_EXPR:
9835 case NE_EXPR:
9836 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9837 warning_at (location,
9838 OPT_Wfloat_equal,
9839 "comparing floating point with == or != is unsafe");
9840 /* Result of comparison is always int,
9841 but don't convert the args to int! */
9842 build_type = integer_type_node;
9843 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9844 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9845 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9846 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9847 short_compare = 1;
9848 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9850 if (TREE_CODE (op0) == ADDR_EXPR
9851 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9853 if (code == EQ_EXPR)
9854 warning_at (location,
9855 OPT_Waddress,
9856 "the comparison will always evaluate as %<false%> "
9857 "for the address of %qD will never be NULL",
9858 TREE_OPERAND (op0, 0));
9859 else
9860 warning_at (location,
9861 OPT_Waddress,
9862 "the comparison will always evaluate as %<true%> "
9863 "for the address of %qD will never be NULL",
9864 TREE_OPERAND (op0, 0));
9866 result_type = type0;
9868 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9870 if (TREE_CODE (op1) == ADDR_EXPR
9871 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9873 if (code == EQ_EXPR)
9874 warning_at (location,
9875 OPT_Waddress,
9876 "the comparison will always evaluate as %<false%> "
9877 "for the address of %qD will never be NULL",
9878 TREE_OPERAND (op1, 0));
9879 else
9880 warning_at (location,
9881 OPT_Waddress,
9882 "the comparison will always evaluate as %<true%> "
9883 "for the address of %qD will never be NULL",
9884 TREE_OPERAND (op1, 0));
9886 result_type = type1;
9888 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9890 tree tt0 = TREE_TYPE (type0);
9891 tree tt1 = TREE_TYPE (type1);
9892 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
9893 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
9894 addr_space_t as_common = ADDR_SPACE_GENERIC;
9896 /* Anything compares with void *. void * compares with anything.
9897 Otherwise, the targets must be compatible
9898 and both must be object or both incomplete. */
9899 if (comp_target_types (location, type0, type1))
9900 result_type = common_pointer_type (type0, type1);
9901 else if (!addr_space_superset (as0, as1, &as_common))
9903 error_at (location, "comparison of pointers to "
9904 "disjoint address spaces");
9905 return error_mark_node;
9907 else if (VOID_TYPE_P (tt0))
9909 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
9910 pedwarn (location, OPT_pedantic, "ISO C forbids "
9911 "comparison of %<void *%> with function pointer");
9913 else if (VOID_TYPE_P (tt1))
9915 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
9916 pedwarn (location, OPT_pedantic, "ISO C forbids "
9917 "comparison of %<void *%> with function pointer");
9919 else
9920 /* Avoid warning about the volatile ObjC EH puts on decls. */
9921 if (!objc_ok)
9922 pedwarn (location, 0,
9923 "comparison of distinct pointer types lacks a cast");
9925 if (result_type == NULL_TREE)
9927 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9928 result_type = build_pointer_type
9929 (build_qualified_type (void_type_node, qual));
9932 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9934 result_type = type0;
9935 pedwarn (location, 0, "comparison between pointer and integer");
9937 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9939 result_type = type1;
9940 pedwarn (location, 0, "comparison between pointer and integer");
9942 break;
9944 case LE_EXPR:
9945 case GE_EXPR:
9946 case LT_EXPR:
9947 case GT_EXPR:
9948 build_type = integer_type_node;
9949 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9950 || code0 == FIXED_POINT_TYPE)
9951 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9952 || code1 == FIXED_POINT_TYPE))
9953 short_compare = 1;
9954 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9956 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
9957 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
9958 addr_space_t as_common;
9960 if (comp_target_types (location, type0, type1))
9962 result_type = common_pointer_type (type0, type1);
9963 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9964 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9965 pedwarn (location, 0,
9966 "comparison of complete and incomplete pointers");
9967 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9968 pedwarn (location, OPT_pedantic, "ISO C forbids "
9969 "ordered comparisons of pointers to functions");
9970 else if (null_pointer_constant_p (orig_op0)
9971 || null_pointer_constant_p (orig_op1))
9972 warning_at (location, OPT_Wextra,
9973 "ordered comparison of pointer with null pointer");
9976 else if (!addr_space_superset (as0, as1, &as_common))
9978 error_at (location, "comparison of pointers to "
9979 "disjoint address spaces");
9980 return error_mark_node;
9982 else
9984 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9985 result_type = build_pointer_type
9986 (build_qualified_type (void_type_node, qual));
9987 pedwarn (location, 0,
9988 "comparison of distinct pointer types lacks a cast");
9991 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9993 result_type = type0;
9994 if (pedantic)
9995 pedwarn (location, OPT_pedantic,
9996 "ordered comparison of pointer with integer zero");
9997 else if (extra_warnings)
9998 warning_at (location, OPT_Wextra,
9999 "ordered comparison of pointer with integer zero");
10001 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10003 result_type = type1;
10004 if (pedantic)
10005 pedwarn (location, OPT_pedantic,
10006 "ordered comparison of pointer with integer zero");
10007 else if (extra_warnings)
10008 warning_at (location, OPT_Wextra,
10009 "ordered comparison of pointer with integer zero");
10011 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10013 result_type = type0;
10014 pedwarn (location, 0, "comparison between pointer and integer");
10016 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10018 result_type = type1;
10019 pedwarn (location, 0, "comparison between pointer and integer");
10021 break;
10023 default:
10024 gcc_unreachable ();
10027 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10028 return error_mark_node;
10030 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10031 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10032 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10033 TREE_TYPE (type1))))
10035 binary_op_error (location, code, type0, type1);
10036 return error_mark_node;
10039 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10040 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10042 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10043 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10045 bool first_complex = (code0 == COMPLEX_TYPE);
10046 bool second_complex = (code1 == COMPLEX_TYPE);
10047 int none_complex = (!first_complex && !second_complex);
10049 if (shorten || common || short_compare)
10051 result_type = c_common_type (type0, type1);
10052 do_warn_double_promotion (result_type, type0, type1,
10053 "implicit conversion from %qT to %qT "
10054 "to match other operand of binary "
10055 "expression",
10056 location);
10057 if (result_type == error_mark_node)
10058 return error_mark_node;
10061 if (first_complex != second_complex
10062 && (code == PLUS_EXPR
10063 || code == MINUS_EXPR
10064 || code == MULT_EXPR
10065 || (code == TRUNC_DIV_EXPR && first_complex))
10066 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10067 && flag_signed_zeros)
10069 /* An operation on mixed real/complex operands must be
10070 handled specially, but the language-independent code can
10071 more easily optimize the plain complex arithmetic if
10072 -fno-signed-zeros. */
10073 tree real_type = TREE_TYPE (result_type);
10074 tree real, imag;
10075 if (type0 != orig_type0 || type1 != orig_type1)
10077 gcc_assert (may_need_excess_precision && common);
10078 semantic_result_type = c_common_type (orig_type0, orig_type1);
10080 if (first_complex)
10082 if (TREE_TYPE (op0) != result_type)
10083 op0 = convert_and_check (result_type, op0);
10084 if (TREE_TYPE (op1) != real_type)
10085 op1 = convert_and_check (real_type, op1);
10087 else
10089 if (TREE_TYPE (op0) != real_type)
10090 op0 = convert_and_check (real_type, op0);
10091 if (TREE_TYPE (op1) != result_type)
10092 op1 = convert_and_check (result_type, op1);
10094 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10095 return error_mark_node;
10096 if (first_complex)
10098 op0 = c_save_expr (op0);
10099 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10100 op0, 1);
10101 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10102 op0, 1);
10103 switch (code)
10105 case MULT_EXPR:
10106 case TRUNC_DIV_EXPR:
10107 imag = build2 (resultcode, real_type, imag, op1);
10108 /* Fall through. */
10109 case PLUS_EXPR:
10110 case MINUS_EXPR:
10111 real = build2 (resultcode, real_type, real, op1);
10112 break;
10113 default:
10114 gcc_unreachable();
10117 else
10119 op1 = c_save_expr (op1);
10120 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10121 op1, 1);
10122 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10123 op1, 1);
10124 switch (code)
10126 case MULT_EXPR:
10127 imag = build2 (resultcode, real_type, op0, imag);
10128 /* Fall through. */
10129 case PLUS_EXPR:
10130 real = build2 (resultcode, real_type, op0, real);
10131 break;
10132 case MINUS_EXPR:
10133 real = build2 (resultcode, real_type, op0, real);
10134 imag = build1 (NEGATE_EXPR, real_type, imag);
10135 break;
10136 default:
10137 gcc_unreachable();
10140 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10141 goto return_build_binary_op;
10144 /* For certain operations (which identify themselves by shorten != 0)
10145 if both args were extended from the same smaller type,
10146 do the arithmetic in that type and then extend.
10148 shorten !=0 and !=1 indicates a bitwise operation.
10149 For them, this optimization is safe only if
10150 both args are zero-extended or both are sign-extended.
10151 Otherwise, we might change the result.
10152 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10153 but calculated in (unsigned short) it would be (unsigned short)-1. */
10155 if (shorten && none_complex)
10157 final_type = result_type;
10158 result_type = shorten_binary_op (result_type, op0, op1,
10159 shorten == -1);
10162 /* Shifts can be shortened if shifting right. */
10164 if (short_shift)
10166 int unsigned_arg;
10167 tree arg0 = get_narrower (op0, &unsigned_arg);
10169 final_type = result_type;
10171 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10172 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10174 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10175 && tree_int_cst_sgn (op1) > 0
10176 /* We can shorten only if the shift count is less than the
10177 number of bits in the smaller type size. */
10178 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10179 /* We cannot drop an unsigned shift after sign-extension. */
10180 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10182 /* Do an unsigned shift if the operand was zero-extended. */
10183 result_type
10184 = c_common_signed_or_unsigned_type (unsigned_arg,
10185 TREE_TYPE (arg0));
10186 /* Convert value-to-be-shifted to that type. */
10187 if (TREE_TYPE (op0) != result_type)
10188 op0 = convert (result_type, op0);
10189 converted = 1;
10193 /* Comparison operations are shortened too but differently.
10194 They identify themselves by setting short_compare = 1. */
10196 if (short_compare)
10198 /* Don't write &op0, etc., because that would prevent op0
10199 from being kept in a register.
10200 Instead, make copies of the our local variables and
10201 pass the copies by reference, then copy them back afterward. */
10202 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10203 enum tree_code xresultcode = resultcode;
10204 tree val
10205 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10207 if (val != 0)
10209 ret = val;
10210 goto return_build_binary_op;
10213 op0 = xop0, op1 = xop1;
10214 converted = 1;
10215 resultcode = xresultcode;
10217 if (c_inhibit_evaluation_warnings == 0)
10219 bool op0_maybe_const = true;
10220 bool op1_maybe_const = true;
10221 tree orig_op0_folded, orig_op1_folded;
10223 if (in_late_binary_op)
10225 orig_op0_folded = orig_op0;
10226 orig_op1_folded = orig_op1;
10228 else
10230 /* Fold for the sake of possible warnings, as in
10231 build_conditional_expr. This requires the
10232 "original" values to be folded, not just op0 and
10233 op1. */
10234 c_inhibit_evaluation_warnings++;
10235 op0 = c_fully_fold (op0, require_constant_value,
10236 &op0_maybe_const);
10237 op1 = c_fully_fold (op1, require_constant_value,
10238 &op1_maybe_const);
10239 c_inhibit_evaluation_warnings--;
10240 orig_op0_folded = c_fully_fold (orig_op0,
10241 require_constant_value,
10242 NULL);
10243 orig_op1_folded = c_fully_fold (orig_op1,
10244 require_constant_value,
10245 NULL);
10248 if (warn_sign_compare)
10249 warn_for_sign_compare (location, orig_op0_folded,
10250 orig_op1_folded, op0, op1,
10251 result_type, resultcode);
10252 if (!in_late_binary_op)
10254 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10255 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10256 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10257 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10263 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10264 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10265 Then the expression will be built.
10266 It will be given type FINAL_TYPE if that is nonzero;
10267 otherwise, it will be given type RESULT_TYPE. */
10269 if (!result_type)
10271 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10272 return error_mark_node;
10275 if (build_type == NULL_TREE)
10277 build_type = result_type;
10278 if ((type0 != orig_type0 || type1 != orig_type1)
10279 && !boolean_op)
10281 gcc_assert (may_need_excess_precision && common);
10282 semantic_result_type = c_common_type (orig_type0, orig_type1);
10286 if (!converted)
10288 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10289 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10291 /* This can happen if one operand has a vector type, and the other
10292 has a different type. */
10293 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10294 return error_mark_node;
10297 /* Treat expressions in initializers specially as they can't trap. */
10298 if (int_const_or_overflow)
10299 ret = (require_constant_value
10300 ? fold_build2_initializer_loc (location, resultcode, build_type,
10301 op0, op1)
10302 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10303 else
10304 ret = build2 (resultcode, build_type, op0, op1);
10305 if (final_type != 0)
10306 ret = convert (final_type, ret);
10308 return_build_binary_op:
10309 gcc_assert (ret != error_mark_node);
10310 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10311 ret = (int_operands
10312 ? note_integer_operands (ret)
10313 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10314 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10315 && !in_late_binary_op)
10316 ret = note_integer_operands (ret);
10317 if (semantic_result_type)
10318 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10319 protected_set_expr_location (ret, location);
10320 return ret;
10324 /* Convert EXPR to be a truth-value, validating its type for this
10325 purpose. LOCATION is the source location for the expression. */
10327 tree
10328 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10330 bool int_const, int_operands;
10332 switch (TREE_CODE (TREE_TYPE (expr)))
10334 case ARRAY_TYPE:
10335 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10336 return error_mark_node;
10338 case RECORD_TYPE:
10339 error_at (location, "used struct type value where scalar is required");
10340 return error_mark_node;
10342 case UNION_TYPE:
10343 error_at (location, "used union type value where scalar is required");
10344 return error_mark_node;
10346 case FUNCTION_TYPE:
10347 gcc_unreachable ();
10349 default:
10350 break;
10353 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10354 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10355 if (int_operands)
10356 expr = remove_c_maybe_const_expr (expr);
10358 /* ??? Should we also give an error for void and vectors rather than
10359 leaving those to give errors later? */
10360 expr = c_common_truthvalue_conversion (location, expr);
10362 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10364 if (TREE_OVERFLOW (expr))
10365 return expr;
10366 else
10367 return note_integer_operands (expr);
10369 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10370 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10371 return expr;
10375 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10376 required. */
10378 tree
10379 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10381 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10383 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10384 /* Executing a compound literal inside a function reinitializes
10385 it. */
10386 if (!TREE_STATIC (decl))
10387 *se = true;
10388 return decl;
10390 else
10391 return expr;
10394 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10396 tree
10397 c_begin_omp_parallel (void)
10399 tree block;
10401 keep_next_level ();
10402 block = c_begin_compound_stmt (true);
10404 return block;
10407 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10408 statement. LOC is the location of the OMP_PARALLEL. */
10410 tree
10411 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10413 tree stmt;
10415 block = c_end_compound_stmt (loc, block, true);
10417 stmt = make_node (OMP_PARALLEL);
10418 TREE_TYPE (stmt) = void_type_node;
10419 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10420 OMP_PARALLEL_BODY (stmt) = block;
10421 SET_EXPR_LOCATION (stmt, loc);
10423 return add_stmt (stmt);
10426 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10428 tree
10429 c_begin_omp_task (void)
10431 tree block;
10433 keep_next_level ();
10434 block = c_begin_compound_stmt (true);
10436 return block;
10439 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10440 statement. LOC is the location of the #pragma. */
10442 tree
10443 c_finish_omp_task (location_t loc, tree clauses, tree block)
10445 tree stmt;
10447 block = c_end_compound_stmt (loc, block, true);
10449 stmt = make_node (OMP_TASK);
10450 TREE_TYPE (stmt) = void_type_node;
10451 OMP_TASK_CLAUSES (stmt) = clauses;
10452 OMP_TASK_BODY (stmt) = block;
10453 SET_EXPR_LOCATION (stmt, loc);
10455 return add_stmt (stmt);
10458 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10459 Remove any elements from the list that are invalid. */
10461 tree
10462 c_finish_omp_clauses (tree clauses)
10464 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10465 tree c, t, *pc = &clauses;
10466 const char *name;
10468 bitmap_obstack_initialize (NULL);
10469 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10470 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10471 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10473 for (pc = &clauses, c = clauses; c ; c = *pc)
10475 bool remove = false;
10476 bool need_complete = false;
10477 bool need_implicitly_determined = false;
10479 switch (OMP_CLAUSE_CODE (c))
10481 case OMP_CLAUSE_SHARED:
10482 name = "shared";
10483 need_implicitly_determined = true;
10484 goto check_dup_generic;
10486 case OMP_CLAUSE_PRIVATE:
10487 name = "private";
10488 need_complete = true;
10489 need_implicitly_determined = true;
10490 goto check_dup_generic;
10492 case OMP_CLAUSE_REDUCTION:
10493 name = "reduction";
10494 need_implicitly_determined = true;
10495 t = OMP_CLAUSE_DECL (c);
10496 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10497 || POINTER_TYPE_P (TREE_TYPE (t)))
10499 error_at (OMP_CLAUSE_LOCATION (c),
10500 "%qE has invalid type for %<reduction%>", t);
10501 remove = true;
10503 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10505 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10506 const char *r_name = NULL;
10508 switch (r_code)
10510 case PLUS_EXPR:
10511 case MULT_EXPR:
10512 case MINUS_EXPR:
10513 break;
10514 case BIT_AND_EXPR:
10515 r_name = "&";
10516 break;
10517 case BIT_XOR_EXPR:
10518 r_name = "^";
10519 break;
10520 case BIT_IOR_EXPR:
10521 r_name = "|";
10522 break;
10523 case TRUTH_ANDIF_EXPR:
10524 r_name = "&&";
10525 break;
10526 case TRUTH_ORIF_EXPR:
10527 r_name = "||";
10528 break;
10529 default:
10530 gcc_unreachable ();
10532 if (r_name)
10534 error_at (OMP_CLAUSE_LOCATION (c),
10535 "%qE has invalid type for %<reduction(%s)%>",
10536 t, r_name);
10537 remove = true;
10540 goto check_dup_generic;
10542 case OMP_CLAUSE_COPYPRIVATE:
10543 name = "copyprivate";
10544 goto check_dup_generic;
10546 case OMP_CLAUSE_COPYIN:
10547 name = "copyin";
10548 t = OMP_CLAUSE_DECL (c);
10549 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10551 error_at (OMP_CLAUSE_LOCATION (c),
10552 "%qE must be %<threadprivate%> for %<copyin%>", t);
10553 remove = true;
10555 goto check_dup_generic;
10557 check_dup_generic:
10558 t = OMP_CLAUSE_DECL (c);
10559 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10561 error_at (OMP_CLAUSE_LOCATION (c),
10562 "%qE is not a variable in clause %qs", t, name);
10563 remove = true;
10565 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10566 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10567 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10569 error_at (OMP_CLAUSE_LOCATION (c),
10570 "%qE appears more than once in data clauses", t);
10571 remove = true;
10573 else
10574 bitmap_set_bit (&generic_head, DECL_UID (t));
10575 break;
10577 case OMP_CLAUSE_FIRSTPRIVATE:
10578 name = "firstprivate";
10579 t = OMP_CLAUSE_DECL (c);
10580 need_complete = true;
10581 need_implicitly_determined = true;
10582 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10584 error_at (OMP_CLAUSE_LOCATION (c),
10585 "%qE is not a variable in clause %<firstprivate%>", t);
10586 remove = true;
10588 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10589 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10591 error_at (OMP_CLAUSE_LOCATION (c),
10592 "%qE appears more than once in data clauses", t);
10593 remove = true;
10595 else
10596 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10597 break;
10599 case OMP_CLAUSE_LASTPRIVATE:
10600 name = "lastprivate";
10601 t = OMP_CLAUSE_DECL (c);
10602 need_complete = true;
10603 need_implicitly_determined = true;
10604 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10606 error_at (OMP_CLAUSE_LOCATION (c),
10607 "%qE is not a variable in clause %<lastprivate%>", t);
10608 remove = true;
10610 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10611 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10613 error_at (OMP_CLAUSE_LOCATION (c),
10614 "%qE appears more than once in data clauses", t);
10615 remove = true;
10617 else
10618 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10619 break;
10621 case OMP_CLAUSE_IF:
10622 case OMP_CLAUSE_NUM_THREADS:
10623 case OMP_CLAUSE_SCHEDULE:
10624 case OMP_CLAUSE_NOWAIT:
10625 case OMP_CLAUSE_ORDERED:
10626 case OMP_CLAUSE_DEFAULT:
10627 case OMP_CLAUSE_UNTIED:
10628 case OMP_CLAUSE_COLLAPSE:
10629 pc = &OMP_CLAUSE_CHAIN (c);
10630 continue;
10632 default:
10633 gcc_unreachable ();
10636 if (!remove)
10638 t = OMP_CLAUSE_DECL (c);
10640 if (need_complete)
10642 t = require_complete_type (t);
10643 if (t == error_mark_node)
10644 remove = true;
10647 if (need_implicitly_determined)
10649 const char *share_name = NULL;
10651 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10652 share_name = "threadprivate";
10653 else switch (c_omp_predetermined_sharing (t))
10655 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10656 break;
10657 case OMP_CLAUSE_DEFAULT_SHARED:
10658 share_name = "shared";
10659 break;
10660 case OMP_CLAUSE_DEFAULT_PRIVATE:
10661 share_name = "private";
10662 break;
10663 default:
10664 gcc_unreachable ();
10666 if (share_name)
10668 error_at (OMP_CLAUSE_LOCATION (c),
10669 "%qE is predetermined %qs for %qs",
10670 t, share_name, name);
10671 remove = true;
10676 if (remove)
10677 *pc = OMP_CLAUSE_CHAIN (c);
10678 else
10679 pc = &OMP_CLAUSE_CHAIN (c);
10682 bitmap_obstack_release (NULL);
10683 return clauses;
10686 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10687 down to the element type of an array. */
10689 tree
10690 c_build_qualified_type (tree type, int type_quals)
10692 if (type == error_mark_node)
10693 return type;
10695 if (TREE_CODE (type) == ARRAY_TYPE)
10697 tree t;
10698 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10699 type_quals);
10701 /* See if we already have an identically qualified type. */
10702 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10704 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10705 && TYPE_NAME (t) == TYPE_NAME (type)
10706 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10707 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10708 TYPE_ATTRIBUTES (type)))
10709 break;
10711 if (!t)
10713 tree domain = TYPE_DOMAIN (type);
10715 t = build_variant_type_copy (type);
10716 TREE_TYPE (t) = element_type;
10718 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10719 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10720 SET_TYPE_STRUCTURAL_EQUALITY (t);
10721 else if (TYPE_CANONICAL (element_type) != element_type
10722 || (domain && TYPE_CANONICAL (domain) != domain))
10724 tree unqualified_canon
10725 = build_array_type (TYPE_CANONICAL (element_type),
10726 domain? TYPE_CANONICAL (domain)
10727 : NULL_TREE);
10728 TYPE_CANONICAL (t)
10729 = c_build_qualified_type (unqualified_canon, type_quals);
10731 else
10732 TYPE_CANONICAL (t) = t;
10734 return t;
10737 /* A restrict-qualified pointer type must be a pointer to object or
10738 incomplete type. Note that the use of POINTER_TYPE_P also allows
10739 REFERENCE_TYPEs, which is appropriate for C++. */
10740 if ((type_quals & TYPE_QUAL_RESTRICT)
10741 && (!POINTER_TYPE_P (type)
10742 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10744 error ("invalid use of %<restrict%>");
10745 type_quals &= ~TYPE_QUAL_RESTRICT;
10748 return build_qualified_type (type, type_quals);
10751 /* Build a VA_ARG_EXPR for the C parser. */
10753 tree
10754 c_build_va_arg (location_t loc, tree expr, tree type)
10756 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10757 warning_at (loc, OPT_Wc___compat,
10758 "C++ requires promoted type, not enum type, in %<va_arg%>");
10759 return build_va_arg (loc, expr, type);