Merge from mainline (154736:156693)
[official-gcc/graphite-test-results.git] / gcc / c-typeck.c
blob567c2a512826cbf349b1973b139c83b06b719051
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
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 "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "c-lang.h"
37 #include "tm_p.h"
38 #include "flags.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "toplev.h"
42 #include "intl.h"
43 #include "ggc.h"
44 #include "target.h"
45 #include "tree-iterator.h"
46 #include "gimple.h"
47 #include "tree-flow.h"
49 /* Possible cases of implicit bad conversions. Used to select
50 diagnostic messages in convert_for_assignment. */
51 enum impl_conv {
52 ic_argpass,
53 ic_assign,
54 ic_init,
55 ic_return
58 /* Whether we are building a boolean conversion inside
59 convert_for_assignment, or some other late binary operation. If
60 build_binary_op is called (from code shared with C++) in this case,
61 then the operands have already been folded and the result will not
62 be folded again, so C_MAYBE_CONST_EXPR should not be generated. */
63 bool in_late_binary_op;
65 /* The level of nesting inside "__alignof__". */
66 int in_alignof;
68 /* The level of nesting inside "sizeof". */
69 int in_sizeof;
71 /* The level of nesting inside "typeof". */
72 int in_typeof;
74 /* Nonzero if we've already printed a "missing braces around initializer"
75 message within this initializer. */
76 static int missing_braces_mentioned;
78 static int require_constant_value;
79 static int require_constant_elements;
81 static bool null_pointer_constant_p (const_tree);
82 static tree qualify_type (tree, tree);
83 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *);
84 static int comp_target_types (location_t, tree, tree);
85 static int function_types_compatible_p (const_tree, const_tree, bool *);
86 static int type_lists_compatible_p (const_tree, const_tree, bool *);
87 static tree lookup_field (tree, tree);
88 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
89 tree);
90 static tree pointer_diff (location_t, tree, tree);
91 static tree convert_for_assignment (location_t, tree, tree, tree,
92 enum impl_conv, bool, tree, tree, int);
93 static tree valid_compound_expr_initializer (tree, tree);
94 static void push_string (const char *);
95 static void push_member_name (tree);
96 static int spelling_length (void);
97 static char *print_spelling (char *);
98 static void warning_init (int, const char *);
99 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
100 static void output_init_element (tree, tree, bool, tree, tree, int, bool);
101 static void output_pending_init_elements (int);
102 static int set_designator (int);
103 static void push_range_stack (tree);
104 static void add_pending_init (tree, tree, tree, bool);
105 static void set_nonincremental_init (void);
106 static void set_nonincremental_init_from_string (tree);
107 static tree find_init_member (tree);
108 static void readonly_error (tree, enum lvalue_use);
109 static void readonly_warning (tree, enum lvalue_use);
110 static int lvalue_or_else (const_tree, enum lvalue_use);
111 static void record_maybe_used_decl (tree);
112 static int comptypes_internal (const_tree, const_tree, bool *);
114 /* Return true if EXP is a null pointer constant, false otherwise. */
116 static bool
117 null_pointer_constant_p (const_tree expr)
119 /* This should really operate on c_expr structures, but they aren't
120 yet available everywhere required. */
121 tree type = TREE_TYPE (expr);
122 return (TREE_CODE (expr) == INTEGER_CST
123 && !TREE_OVERFLOW (expr)
124 && integer_zerop (expr)
125 && (INTEGRAL_TYPE_P (type)
126 || (TREE_CODE (type) == POINTER_TYPE
127 && VOID_TYPE_P (TREE_TYPE (type))
128 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
131 /* EXPR may appear in an unevaluated part of an integer constant
132 expression, but not in an evaluated part. Wrap it in a
133 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
134 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
136 static tree
137 note_integer_operands (tree expr)
139 tree ret;
140 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
142 ret = copy_node (expr);
143 TREE_OVERFLOW (ret) = 1;
145 else
147 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
148 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
150 return ret;
153 /* Having checked whether EXPR may appear in an unevaluated part of an
154 integer constant expression and found that it may, remove any
155 C_MAYBE_CONST_EXPR noting this fact and return the resulting
156 expression. */
158 static inline tree
159 remove_c_maybe_const_expr (tree expr)
161 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
162 return C_MAYBE_CONST_EXPR_EXPR (expr);
163 else
164 return expr;
167 \f/* This is a cache to hold if two types are compatible or not. */
169 struct tagged_tu_seen_cache {
170 const struct tagged_tu_seen_cache * next;
171 const_tree t1;
172 const_tree t2;
173 /* The return value of tagged_types_tu_compatible_p if we had seen
174 these two types already. */
175 int val;
178 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
179 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
181 /* Do `exp = require_complete_type (exp);' to make sure exp
182 does not have an incomplete type. (That includes void types.) */
184 tree
185 require_complete_type (tree value)
187 tree type = TREE_TYPE (value);
189 if (value == error_mark_node || type == error_mark_node)
190 return error_mark_node;
192 /* First, detect a valid value with a complete type. */
193 if (COMPLETE_TYPE_P (type))
194 return value;
196 c_incomplete_type_error (value, type);
197 return error_mark_node;
200 /* Print an error message for invalid use of an incomplete type.
201 VALUE is the expression that was used (or 0 if that isn't known)
202 and TYPE is the type that was invalid. */
204 void
205 c_incomplete_type_error (const_tree value, const_tree type)
207 const char *type_code_string;
209 /* Avoid duplicate error message. */
210 if (TREE_CODE (type) == ERROR_MARK)
211 return;
213 if (value != 0 && (TREE_CODE (value) == VAR_DECL
214 || TREE_CODE (value) == PARM_DECL))
215 error ("%qD has an incomplete type", value);
216 else
218 retry:
219 /* We must print an error message. Be clever about what it says. */
221 switch (TREE_CODE (type))
223 case RECORD_TYPE:
224 type_code_string = "struct";
225 break;
227 case UNION_TYPE:
228 type_code_string = "union";
229 break;
231 case ENUMERAL_TYPE:
232 type_code_string = "enum";
233 break;
235 case VOID_TYPE:
236 error ("invalid use of void expression");
237 return;
239 case ARRAY_TYPE:
240 if (TYPE_DOMAIN (type))
242 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
244 error ("invalid use of flexible array member");
245 return;
247 type = TREE_TYPE (type);
248 goto retry;
250 error ("invalid use of array with unspecified bounds");
251 return;
253 default:
254 gcc_unreachable ();
257 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
258 error ("invalid use of undefined type %<%s %E%>",
259 type_code_string, TYPE_NAME (type));
260 else
261 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
262 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
266 /* Given a type, apply default promotions wrt unnamed function
267 arguments and return the new type. */
269 tree
270 c_type_promotes_to (tree type)
272 if (TYPE_MAIN_VARIANT (type) == float_type_node)
273 return double_type_node;
275 if (c_promoting_integer_type_p (type))
277 /* Preserve unsignedness if not really getting any wider. */
278 if (TYPE_UNSIGNED (type)
279 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
280 return unsigned_type_node;
281 return integer_type_node;
284 return type;
287 /* Return true if between two named address spaces, whether there is a superset
288 named address space that encompasses both address spaces. If there is a
289 superset, return which address space is the superset. */
291 static bool
292 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
294 if (as1 == as2)
296 *common = as1;
297 return true;
299 else if (targetm.addr_space.subset_p (as1, as2))
301 *common = as2;
302 return true;
304 else if (targetm.addr_space.subset_p (as2, as1))
306 *common = as1;
307 return true;
309 else
310 return false;
313 /* Return a variant of TYPE which has all the type qualifiers of LIKE
314 as well as those of TYPE. */
316 static tree
317 qualify_type (tree type, tree like)
319 addr_space_t as_type = TYPE_ADDR_SPACE (type);
320 addr_space_t as_like = TYPE_ADDR_SPACE (like);
321 addr_space_t as_common;
323 /* If the two named address spaces are different, determine the common
324 superset address space. If there isn't one, raise an error. */
325 if (!addr_space_superset (as_type, as_like, &as_common))
327 as_common = as_type;
328 error ("%qT and %qT are in disjoint named address spaces",
329 type, like);
332 return c_build_qualified_type (type,
333 TYPE_QUALS_NO_ADDR_SPACE (type)
334 | TYPE_QUALS_NO_ADDR_SPACE (like)
335 | ENCODE_QUAL_ADDR_SPACE (as_common));
338 /* Return true iff the given tree T is a variable length array. */
340 bool
341 c_vla_type_p (const_tree t)
343 if (TREE_CODE (t) == ARRAY_TYPE
344 && C_TYPE_VARIABLE_SIZE (t))
345 return true;
346 return false;
349 /* Return the composite type of two compatible types.
351 We assume that comptypes has already been done and returned
352 nonzero; if that isn't so, this may crash. In particular, we
353 assume that qualifiers match. */
355 tree
356 composite_type (tree t1, tree t2)
358 enum tree_code code1;
359 enum tree_code code2;
360 tree attributes;
362 /* Save time if the two types are the same. */
364 if (t1 == t2) return t1;
366 /* If one type is nonsense, use the other. */
367 if (t1 == error_mark_node)
368 return t2;
369 if (t2 == error_mark_node)
370 return t1;
372 code1 = TREE_CODE (t1);
373 code2 = TREE_CODE (t2);
375 /* Merge the attributes. */
376 attributes = targetm.merge_type_attributes (t1, t2);
378 /* If one is an enumerated type and the other is the compatible
379 integer type, the composite type might be either of the two
380 (DR#013 question 3). For consistency, use the enumerated type as
381 the composite type. */
383 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
384 return t1;
385 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
386 return t2;
388 gcc_assert (code1 == code2);
390 switch (code1)
392 case POINTER_TYPE:
393 /* For two pointers, do this recursively on the target type. */
395 tree pointed_to_1 = TREE_TYPE (t1);
396 tree pointed_to_2 = TREE_TYPE (t2);
397 tree target = composite_type (pointed_to_1, pointed_to_2);
398 t1 = build_pointer_type (target);
399 t1 = build_type_attribute_variant (t1, attributes);
400 return qualify_type (t1, t2);
403 case ARRAY_TYPE:
405 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
406 int quals;
407 tree unqual_elt;
408 tree d1 = TYPE_DOMAIN (t1);
409 tree d2 = TYPE_DOMAIN (t2);
410 bool d1_variable, d2_variable;
411 bool d1_zero, d2_zero;
412 bool t1_complete, t2_complete;
414 /* We should not have any type quals on arrays at all. */
415 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
416 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
418 t1_complete = COMPLETE_TYPE_P (t1);
419 t2_complete = COMPLETE_TYPE_P (t2);
421 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
422 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
424 d1_variable = (!d1_zero
425 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
426 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
427 d2_variable = (!d2_zero
428 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
429 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
430 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
431 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
433 /* Save space: see if the result is identical to one of the args. */
434 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
435 && (d2_variable || d2_zero || !d1_variable))
436 return build_type_attribute_variant (t1, attributes);
437 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
438 && (d1_variable || d1_zero || !d2_variable))
439 return build_type_attribute_variant (t2, attributes);
441 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
442 return build_type_attribute_variant (t1, attributes);
443 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
444 return build_type_attribute_variant (t2, attributes);
446 /* Merge the element types, and have a size if either arg has
447 one. We may have qualifiers on the element types. To set
448 up TYPE_MAIN_VARIANT correctly, we need to form the
449 composite of the unqualified types and add the qualifiers
450 back at the end. */
451 quals = TYPE_QUALS (strip_array_types (elt));
452 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
453 t1 = build_array_type (unqual_elt,
454 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
455 && (d2_variable
456 || d2_zero
457 || !d1_variable))
458 ? t1
459 : t2));
460 /* Ensure a composite type involving a zero-length array type
461 is a zero-length type not an incomplete type. */
462 if (d1_zero && d2_zero
463 && (t1_complete || t2_complete)
464 && !COMPLETE_TYPE_P (t1))
466 TYPE_SIZE (t1) = bitsize_zero_node;
467 TYPE_SIZE_UNIT (t1) = size_zero_node;
469 t1 = c_build_qualified_type (t1, quals);
470 return build_type_attribute_variant (t1, attributes);
473 case ENUMERAL_TYPE:
474 case RECORD_TYPE:
475 case UNION_TYPE:
476 if (attributes != NULL)
478 /* Try harder not to create a new aggregate type. */
479 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
480 return t1;
481 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
482 return t2;
484 return build_type_attribute_variant (t1, attributes);
486 case FUNCTION_TYPE:
487 /* Function types: prefer the one that specified arg types.
488 If both do, merge the arg types. Also merge the return types. */
490 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
491 tree p1 = TYPE_ARG_TYPES (t1);
492 tree p2 = TYPE_ARG_TYPES (t2);
493 int len;
494 tree newargs, n;
495 int i;
497 /* Save space: see if the result is identical to one of the args. */
498 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
499 return build_type_attribute_variant (t1, attributes);
500 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
501 return build_type_attribute_variant (t2, attributes);
503 /* Simple way if one arg fails to specify argument types. */
504 if (TYPE_ARG_TYPES (t1) == 0)
506 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
507 t1 = build_type_attribute_variant (t1, attributes);
508 return qualify_type (t1, t2);
510 if (TYPE_ARG_TYPES (t2) == 0)
512 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
513 t1 = build_type_attribute_variant (t1, attributes);
514 return qualify_type (t1, t2);
517 /* If both args specify argument types, we must merge the two
518 lists, argument by argument. */
519 /* Tell global_bindings_p to return false so that variable_size
520 doesn't die on VLAs in parameter types. */
521 c_override_global_bindings_to_false = true;
523 len = list_length (p1);
524 newargs = 0;
526 for (i = 0; i < len; i++)
527 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
529 n = newargs;
531 for (; p1;
532 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
534 /* A null type means arg type is not specified.
535 Take whatever the other function type has. */
536 if (TREE_VALUE (p1) == 0)
538 TREE_VALUE (n) = TREE_VALUE (p2);
539 goto parm_done;
541 if (TREE_VALUE (p2) == 0)
543 TREE_VALUE (n) = TREE_VALUE (p1);
544 goto parm_done;
547 /* Given wait (union {union wait *u; int *i} *)
548 and wait (union wait *),
549 prefer union wait * as type of parm. */
550 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
551 && TREE_VALUE (p1) != TREE_VALUE (p2))
553 tree memb;
554 tree mv2 = TREE_VALUE (p2);
555 if (mv2 && mv2 != error_mark_node
556 && TREE_CODE (mv2) != ARRAY_TYPE)
557 mv2 = TYPE_MAIN_VARIANT (mv2);
558 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
559 memb; memb = TREE_CHAIN (memb))
561 tree mv3 = TREE_TYPE (memb);
562 if (mv3 && mv3 != error_mark_node
563 && TREE_CODE (mv3) != ARRAY_TYPE)
564 mv3 = TYPE_MAIN_VARIANT (mv3);
565 if (comptypes (mv3, mv2))
567 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
568 TREE_VALUE (p2));
569 pedwarn (input_location, OPT_pedantic,
570 "function types not truly compatible in ISO C");
571 goto parm_done;
575 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
576 && TREE_VALUE (p2) != TREE_VALUE (p1))
578 tree memb;
579 tree mv1 = TREE_VALUE (p1);
580 if (mv1 && mv1 != error_mark_node
581 && TREE_CODE (mv1) != ARRAY_TYPE)
582 mv1 = TYPE_MAIN_VARIANT (mv1);
583 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
584 memb; memb = TREE_CHAIN (memb))
586 tree mv3 = TREE_TYPE (memb);
587 if (mv3 && mv3 != error_mark_node
588 && TREE_CODE (mv3) != ARRAY_TYPE)
589 mv3 = TYPE_MAIN_VARIANT (mv3);
590 if (comptypes (mv3, mv1))
592 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
593 TREE_VALUE (p1));
594 pedwarn (input_location, OPT_pedantic,
595 "function types not truly compatible in ISO C");
596 goto parm_done;
600 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
601 parm_done: ;
604 c_override_global_bindings_to_false = false;
605 t1 = build_function_type (valtype, newargs);
606 t1 = qualify_type (t1, t2);
607 /* ... falls through ... */
610 default:
611 return build_type_attribute_variant (t1, attributes);
616 /* Return the type of a conditional expression between pointers to
617 possibly differently qualified versions of compatible types.
619 We assume that comp_target_types has already been done and returned
620 nonzero; if that isn't so, this may crash. */
622 static tree
623 common_pointer_type (tree t1, tree t2)
625 tree attributes;
626 tree pointed_to_1, mv1;
627 tree pointed_to_2, mv2;
628 tree target;
629 unsigned target_quals;
630 addr_space_t as1, as2, as_common;
631 int quals1, quals2;
633 /* Save time if the two types are the same. */
635 if (t1 == t2) return t1;
637 /* If one type is nonsense, use the other. */
638 if (t1 == error_mark_node)
639 return t2;
640 if (t2 == error_mark_node)
641 return t1;
643 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
644 && TREE_CODE (t2) == POINTER_TYPE);
646 /* Merge the attributes. */
647 attributes = targetm.merge_type_attributes (t1, t2);
649 /* Find the composite type of the target types, and combine the
650 qualifiers of the two types' targets. Do not lose qualifiers on
651 array element types by taking the TYPE_MAIN_VARIANT. */
652 mv1 = pointed_to_1 = TREE_TYPE (t1);
653 mv2 = pointed_to_2 = TREE_TYPE (t2);
654 if (TREE_CODE (mv1) != ARRAY_TYPE)
655 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
656 if (TREE_CODE (mv2) != ARRAY_TYPE)
657 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
658 target = composite_type (mv1, mv2);
660 /* For function types do not merge const qualifiers, but drop them
661 if used inconsistently. The middle-end uses these to mark const
662 and noreturn functions. */
663 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
664 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
666 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
667 target_quals = (quals1 & quals2);
668 else
669 target_quals = (quals1 | quals2);
671 /* If the two named address spaces are different, determine the common
672 superset address space. This is guaranteed to exist due to the
673 assumption that comp_target_type returned non-zero. */
674 as1 = TYPE_ADDR_SPACE (pointed_to_1);
675 as2 = TYPE_ADDR_SPACE (pointed_to_2);
676 if (!addr_space_superset (as1, as2, &as_common))
677 gcc_unreachable ();
679 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
681 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
682 return build_type_attribute_variant (t1, attributes);
685 /* Return the common type for two arithmetic types under the usual
686 arithmetic conversions. The default conversions have already been
687 applied, and enumerated types converted to their compatible integer
688 types. The resulting type is unqualified and has no attributes.
690 This is the type for the result of most arithmetic operations
691 if the operands have the given two types. */
693 static tree
694 c_common_type (tree t1, tree t2)
696 enum tree_code code1;
697 enum tree_code code2;
699 /* If one type is nonsense, use the other. */
700 if (t1 == error_mark_node)
701 return t2;
702 if (t2 == error_mark_node)
703 return t1;
705 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
706 t1 = TYPE_MAIN_VARIANT (t1);
708 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
709 t2 = TYPE_MAIN_VARIANT (t2);
711 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
712 t1 = build_type_attribute_variant (t1, NULL_TREE);
714 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
715 t2 = build_type_attribute_variant (t2, NULL_TREE);
717 /* Save time if the two types are the same. */
719 if (t1 == t2) return t1;
721 code1 = TREE_CODE (t1);
722 code2 = TREE_CODE (t2);
724 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
725 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
726 || code1 == INTEGER_TYPE);
727 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
728 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
729 || code2 == INTEGER_TYPE);
731 /* When one operand is a decimal float type, the other operand cannot be
732 a generic float type or a complex type. We also disallow vector types
733 here. */
734 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
735 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
737 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
739 error ("can%'t mix operands of decimal float and vector types");
740 return error_mark_node;
742 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
744 error ("can%'t mix operands of decimal float and complex types");
745 return error_mark_node;
747 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
749 error ("can%'t mix operands of decimal float and other float types");
750 return error_mark_node;
754 /* If one type is a vector type, return that type. (How the usual
755 arithmetic conversions apply to the vector types extension is not
756 precisely specified.) */
757 if (code1 == VECTOR_TYPE)
758 return t1;
760 if (code2 == VECTOR_TYPE)
761 return t2;
763 /* If one type is complex, form the common type of the non-complex
764 components, then make that complex. Use T1 or T2 if it is the
765 required type. */
766 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
768 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
769 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
770 tree subtype = c_common_type (subtype1, subtype2);
772 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
773 return t1;
774 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
775 return t2;
776 else
777 return build_complex_type (subtype);
780 /* If only one is real, use it as the result. */
782 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
783 return t1;
785 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
786 return t2;
788 /* If both are real and either are decimal floating point types, use
789 the decimal floating point type with the greater precision. */
791 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
793 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
794 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
795 return dfloat128_type_node;
796 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
797 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
798 return dfloat64_type_node;
799 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
801 return dfloat32_type_node;
804 /* Deal with fixed-point types. */
805 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
807 unsigned int unsignedp = 0, satp = 0;
808 enum machine_mode m1, m2;
809 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
811 m1 = TYPE_MODE (t1);
812 m2 = TYPE_MODE (t2);
814 /* If one input type is saturating, the result type is saturating. */
815 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
816 satp = 1;
818 /* If both fixed-point types are unsigned, the result type is unsigned.
819 When mixing fixed-point and integer types, follow the sign of the
820 fixed-point type.
821 Otherwise, the result type is signed. */
822 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
823 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
824 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
825 && TYPE_UNSIGNED (t1))
826 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
827 && TYPE_UNSIGNED (t2)))
828 unsignedp = 1;
830 /* The result type is signed. */
831 if (unsignedp == 0)
833 /* If the input type is unsigned, we need to convert to the
834 signed type. */
835 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
837 enum mode_class mclass = (enum mode_class) 0;
838 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
839 mclass = MODE_FRACT;
840 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
841 mclass = MODE_ACCUM;
842 else
843 gcc_unreachable ();
844 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
846 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
848 enum mode_class mclass = (enum mode_class) 0;
849 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
850 mclass = MODE_FRACT;
851 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
852 mclass = MODE_ACCUM;
853 else
854 gcc_unreachable ();
855 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
859 if (code1 == FIXED_POINT_TYPE)
861 fbit1 = GET_MODE_FBIT (m1);
862 ibit1 = GET_MODE_IBIT (m1);
864 else
866 fbit1 = 0;
867 /* Signed integers need to subtract one sign bit. */
868 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
871 if (code2 == FIXED_POINT_TYPE)
873 fbit2 = GET_MODE_FBIT (m2);
874 ibit2 = GET_MODE_IBIT (m2);
876 else
878 fbit2 = 0;
879 /* Signed integers need to subtract one sign bit. */
880 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
883 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
884 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
885 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
886 satp);
889 /* Both real or both integers; use the one with greater precision. */
891 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
892 return t1;
893 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
894 return t2;
896 /* Same precision. Prefer long longs to longs to ints when the
897 same precision, following the C99 rules on integer type rank
898 (which are equivalent to the C90 rules for C90 types). */
900 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
901 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
902 return long_long_unsigned_type_node;
904 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
905 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
907 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
908 return long_long_unsigned_type_node;
909 else
910 return long_long_integer_type_node;
913 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
914 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
915 return long_unsigned_type_node;
917 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
918 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
920 /* But preserve unsignedness from the other type,
921 since long cannot hold all the values of an unsigned int. */
922 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
923 return long_unsigned_type_node;
924 else
925 return long_integer_type_node;
928 /* Likewise, prefer long double to double even if same size. */
929 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
930 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
931 return long_double_type_node;
933 /* Otherwise prefer the unsigned one. */
935 if (TYPE_UNSIGNED (t1))
936 return t1;
937 else
938 return t2;
941 /* Wrapper around c_common_type that is used by c-common.c and other
942 front end optimizations that remove promotions. ENUMERAL_TYPEs
943 are allowed here and are converted to their compatible integer types.
944 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
945 preferably a non-Boolean type as the common type. */
946 tree
947 common_type (tree t1, tree t2)
949 if (TREE_CODE (t1) == ENUMERAL_TYPE)
950 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
951 if (TREE_CODE (t2) == ENUMERAL_TYPE)
952 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
954 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
955 if (TREE_CODE (t1) == BOOLEAN_TYPE
956 && TREE_CODE (t2) == BOOLEAN_TYPE)
957 return boolean_type_node;
959 /* If either type is BOOLEAN_TYPE, then return the other. */
960 if (TREE_CODE (t1) == BOOLEAN_TYPE)
961 return t2;
962 if (TREE_CODE (t2) == BOOLEAN_TYPE)
963 return t1;
965 return c_common_type (t1, t2);
968 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
969 or various other operations. Return 2 if they are compatible
970 but a warning may be needed if you use them together. */
973 comptypes (tree type1, tree type2)
975 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
976 int val;
978 val = comptypes_internal (type1, type2, NULL);
979 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
981 return val;
984 /* Like comptypes, but if it returns non-zero because enum and int are
985 compatible, it sets *ENUM_AND_INT_P to true. */
987 static int
988 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
990 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
991 int val;
993 val = comptypes_internal (type1, type2, enum_and_int_p);
994 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
996 return val;
999 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1000 or various other operations. Return 2 if they are compatible
1001 but a warning may be needed if you use them together. If
1002 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1003 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1004 *ENUM_AND_INT_P is never set to false. This differs from
1005 comptypes, in that we don't free the seen types. */
1007 static int
1008 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p)
1010 const_tree t1 = type1;
1011 const_tree t2 = type2;
1012 int attrval, val;
1014 /* Suppress errors caused by previously reported errors. */
1016 if (t1 == t2 || !t1 || !t2
1017 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1018 return 1;
1020 /* If either type is the internal version of sizetype, return the
1021 language version. */
1022 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
1023 && TYPE_ORIG_SIZE_TYPE (t1))
1024 t1 = TYPE_ORIG_SIZE_TYPE (t1);
1026 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
1027 && TYPE_ORIG_SIZE_TYPE (t2))
1028 t2 = TYPE_ORIG_SIZE_TYPE (t2);
1031 /* Enumerated types are compatible with integer types, but this is
1032 not transitive: two enumerated types in the same translation unit
1033 are compatible with each other only if they are the same type. */
1035 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1037 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1038 if (enum_and_int_p != NULL && TREE_CODE (t2) != VOID_TYPE)
1039 *enum_and_int_p = true;
1041 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1043 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1044 if (enum_and_int_p != NULL && TREE_CODE (t1) != VOID_TYPE)
1045 *enum_and_int_p = true;
1048 if (t1 == t2)
1049 return 1;
1051 /* Different classes of types can't be compatible. */
1053 if (TREE_CODE (t1) != TREE_CODE (t2))
1054 return 0;
1056 /* Qualifiers must match. C99 6.7.3p9 */
1058 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1059 return 0;
1061 /* Allow for two different type nodes which have essentially the same
1062 definition. Note that we already checked for equality of the type
1063 qualifiers (just above). */
1065 if (TREE_CODE (t1) != ARRAY_TYPE
1066 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1067 return 1;
1069 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1070 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1071 return 0;
1073 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1074 val = 0;
1076 switch (TREE_CODE (t1))
1078 case POINTER_TYPE:
1079 /* Do not remove mode or aliasing information. */
1080 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1081 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1082 break;
1083 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1084 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1085 enum_and_int_p));
1086 break;
1088 case FUNCTION_TYPE:
1089 val = function_types_compatible_p (t1, t2, enum_and_int_p);
1090 break;
1092 case ARRAY_TYPE:
1094 tree d1 = TYPE_DOMAIN (t1);
1095 tree d2 = TYPE_DOMAIN (t2);
1096 bool d1_variable, d2_variable;
1097 bool d1_zero, d2_zero;
1098 val = 1;
1100 /* Target types must match incl. qualifiers. */
1101 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1102 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1103 enum_and_int_p)))
1104 return 0;
1106 /* Sizes must match unless one is missing or variable. */
1107 if (d1 == 0 || d2 == 0 || d1 == d2)
1108 break;
1110 d1_zero = !TYPE_MAX_VALUE (d1);
1111 d2_zero = !TYPE_MAX_VALUE (d2);
1113 d1_variable = (!d1_zero
1114 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1115 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1116 d2_variable = (!d2_zero
1117 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1118 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1119 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1120 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1122 if (d1_variable || d2_variable)
1123 break;
1124 if (d1_zero && d2_zero)
1125 break;
1126 if (d1_zero || d2_zero
1127 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1128 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1129 val = 0;
1131 break;
1134 case ENUMERAL_TYPE:
1135 case RECORD_TYPE:
1136 case UNION_TYPE:
1137 if (val != 1 && !same_translation_unit_p (t1, t2))
1139 tree a1 = TYPE_ATTRIBUTES (t1);
1140 tree a2 = TYPE_ATTRIBUTES (t2);
1142 if (! attribute_list_contained (a1, a2)
1143 && ! attribute_list_contained (a2, a1))
1144 break;
1146 if (attrval != 2)
1147 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p);
1148 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p);
1150 break;
1152 case VECTOR_TYPE:
1153 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1154 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1155 enum_and_int_p));
1156 break;
1158 default:
1159 break;
1161 return attrval == 2 && val == 1 ? 2 : val;
1164 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1165 their qualifiers, except for named address spaces. If the pointers point to
1166 different named addresses, then we must determine if one address space is a
1167 subset of the other. */
1169 static int
1170 comp_target_types (location_t location, tree ttl, tree ttr)
1172 int val;
1173 tree mvl = TREE_TYPE (ttl);
1174 tree mvr = TREE_TYPE (ttr);
1175 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1176 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1177 addr_space_t as_common;
1178 bool enum_and_int_p;
1180 /* Fail if pointers point to incompatible address spaces. */
1181 if (!addr_space_superset (asl, asr, &as_common))
1182 return 0;
1184 /* Do not lose qualifiers on element types of array types that are
1185 pointer targets by taking their TYPE_MAIN_VARIANT. */
1186 if (TREE_CODE (mvl) != ARRAY_TYPE)
1187 mvl = TYPE_MAIN_VARIANT (mvl);
1188 if (TREE_CODE (mvr) != ARRAY_TYPE)
1189 mvr = TYPE_MAIN_VARIANT (mvr);
1190 enum_and_int_p = false;
1191 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1193 if (val == 2)
1194 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1196 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1197 warning_at (location, OPT_Wc___compat,
1198 "pointer target types incompatible in C++");
1200 return val;
1203 /* Subroutines of `comptypes'. */
1205 /* Determine whether two trees derive from the same translation unit.
1206 If the CONTEXT chain ends in a null, that tree's context is still
1207 being parsed, so if two trees have context chains ending in null,
1208 they're in the same translation unit. */
1210 same_translation_unit_p (const_tree t1, const_tree t2)
1212 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1213 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1215 case tcc_declaration:
1216 t1 = DECL_CONTEXT (t1); break;
1217 case tcc_type:
1218 t1 = TYPE_CONTEXT (t1); break;
1219 case tcc_exceptional:
1220 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1221 default: gcc_unreachable ();
1224 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1225 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1227 case tcc_declaration:
1228 t2 = DECL_CONTEXT (t2); break;
1229 case tcc_type:
1230 t2 = TYPE_CONTEXT (t2); break;
1231 case tcc_exceptional:
1232 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1233 default: gcc_unreachable ();
1236 return t1 == t2;
1239 /* Allocate the seen two types, assuming that they are compatible. */
1241 static struct tagged_tu_seen_cache *
1242 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1244 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1245 tu->next = tagged_tu_seen_base;
1246 tu->t1 = t1;
1247 tu->t2 = t2;
1249 tagged_tu_seen_base = tu;
1251 /* The C standard says that two structures in different translation
1252 units are compatible with each other only if the types of their
1253 fields are compatible (among other things). We assume that they
1254 are compatible until proven otherwise when building the cache.
1255 An example where this can occur is:
1256 struct a
1258 struct a *next;
1260 If we are comparing this against a similar struct in another TU,
1261 and did not assume they were compatible, we end up with an infinite
1262 loop. */
1263 tu->val = 1;
1264 return tu;
1267 /* Free the seen types until we get to TU_TIL. */
1269 static void
1270 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1272 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1273 while (tu != tu_til)
1275 const struct tagged_tu_seen_cache *const tu1
1276 = (const struct tagged_tu_seen_cache *) tu;
1277 tu = tu1->next;
1278 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1280 tagged_tu_seen_base = tu_til;
1283 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1284 compatible. If the two types are not the same (which has been
1285 checked earlier), this can only happen when multiple translation
1286 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1287 rules. ENUM_AND_INT_P is as in comptypes_internal. */
1289 static int
1290 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1291 bool *enum_and_int_p)
1293 tree s1, s2;
1294 bool needs_warning = false;
1296 /* We have to verify that the tags of the types are the same. This
1297 is harder than it looks because this may be a typedef, so we have
1298 to go look at the original type. It may even be a typedef of a
1299 typedef...
1300 In the case of compiler-created builtin structs the TYPE_DECL
1301 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1302 while (TYPE_NAME (t1)
1303 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1304 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1305 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1307 while (TYPE_NAME (t2)
1308 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1309 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1310 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1312 /* C90 didn't have the requirement that the two tags be the same. */
1313 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1314 return 0;
1316 /* C90 didn't say what happened if one or both of the types were
1317 incomplete; we choose to follow C99 rules here, which is that they
1318 are compatible. */
1319 if (TYPE_SIZE (t1) == NULL
1320 || TYPE_SIZE (t2) == NULL)
1321 return 1;
1324 const struct tagged_tu_seen_cache * tts_i;
1325 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1326 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1327 return tts_i->val;
1330 switch (TREE_CODE (t1))
1332 case ENUMERAL_TYPE:
1334 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1335 /* Speed up the case where the type values are in the same order. */
1336 tree tv1 = TYPE_VALUES (t1);
1337 tree tv2 = TYPE_VALUES (t2);
1339 if (tv1 == tv2)
1341 return 1;
1344 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1346 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1347 break;
1348 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1350 tu->val = 0;
1351 return 0;
1355 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1357 return 1;
1359 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1361 tu->val = 0;
1362 return 0;
1365 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1367 tu->val = 0;
1368 return 0;
1371 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1373 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1374 if (s2 == NULL
1375 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1377 tu->val = 0;
1378 return 0;
1381 return 1;
1384 case UNION_TYPE:
1386 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1387 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1389 tu->val = 0;
1390 return 0;
1393 /* Speed up the common case where the fields are in the same order. */
1394 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1395 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1397 int result;
1399 if (DECL_NAME (s1) != DECL_NAME (s2))
1400 break;
1401 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1402 enum_and_int_p);
1404 if (result != 1 && !DECL_NAME (s1))
1405 break;
1406 if (result == 0)
1408 tu->val = 0;
1409 return 0;
1411 if (result == 2)
1412 needs_warning = true;
1414 if (TREE_CODE (s1) == FIELD_DECL
1415 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1416 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1418 tu->val = 0;
1419 return 0;
1422 if (!s1 && !s2)
1424 tu->val = needs_warning ? 2 : 1;
1425 return tu->val;
1428 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1430 bool ok = false;
1432 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1433 if (DECL_NAME (s1) == DECL_NAME (s2))
1435 int result;
1437 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1438 enum_and_int_p);
1440 if (result != 1 && !DECL_NAME (s1))
1441 continue;
1442 if (result == 0)
1444 tu->val = 0;
1445 return 0;
1447 if (result == 2)
1448 needs_warning = true;
1450 if (TREE_CODE (s1) == FIELD_DECL
1451 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1452 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1453 break;
1455 ok = true;
1456 break;
1458 if (!ok)
1460 tu->val = 0;
1461 return 0;
1464 tu->val = needs_warning ? 2 : 10;
1465 return tu->val;
1468 case RECORD_TYPE:
1470 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1472 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1473 s1 && s2;
1474 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1476 int result;
1477 if (TREE_CODE (s1) != TREE_CODE (s2)
1478 || DECL_NAME (s1) != DECL_NAME (s2))
1479 break;
1480 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1481 enum_and_int_p);
1482 if (result == 0)
1483 break;
1484 if (result == 2)
1485 needs_warning = true;
1487 if (TREE_CODE (s1) == FIELD_DECL
1488 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1489 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1490 break;
1492 if (s1 && s2)
1493 tu->val = 0;
1494 else
1495 tu->val = needs_warning ? 2 : 1;
1496 return tu->val;
1499 default:
1500 gcc_unreachable ();
1504 /* Return 1 if two function types F1 and F2 are compatible.
1505 If either type specifies no argument types,
1506 the other must specify a fixed number of self-promoting arg types.
1507 Otherwise, if one type specifies only the number of arguments,
1508 the other must specify that number of self-promoting arg types.
1509 Otherwise, the argument types must match.
1510 ENUM_AND_INT_P is as in comptypes_internal. */
1512 static int
1513 function_types_compatible_p (const_tree f1, const_tree f2,
1514 bool *enum_and_int_p)
1516 tree args1, args2;
1517 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1518 int val = 1;
1519 int val1;
1520 tree ret1, ret2;
1522 ret1 = TREE_TYPE (f1);
1523 ret2 = TREE_TYPE (f2);
1525 /* 'volatile' qualifiers on a function's return type used to mean
1526 the function is noreturn. */
1527 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1528 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1529 if (TYPE_VOLATILE (ret1))
1530 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1531 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1532 if (TYPE_VOLATILE (ret2))
1533 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1534 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1535 val = comptypes_internal (ret1, ret2, enum_and_int_p);
1536 if (val == 0)
1537 return 0;
1539 args1 = TYPE_ARG_TYPES (f1);
1540 args2 = TYPE_ARG_TYPES (f2);
1542 /* An unspecified parmlist matches any specified parmlist
1543 whose argument types don't need default promotions. */
1545 if (args1 == 0)
1547 if (!self_promoting_args_p (args2))
1548 return 0;
1549 /* If one of these types comes from a non-prototype fn definition,
1550 compare that with the other type's arglist.
1551 If they don't match, ask for a warning (but no error). */
1552 if (TYPE_ACTUAL_ARG_TYPES (f1)
1553 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1554 enum_and_int_p))
1555 val = 2;
1556 return val;
1558 if (args2 == 0)
1560 if (!self_promoting_args_p (args1))
1561 return 0;
1562 if (TYPE_ACTUAL_ARG_TYPES (f2)
1563 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1564 enum_and_int_p))
1565 val = 2;
1566 return val;
1569 /* Both types have argument lists: compare them and propagate results. */
1570 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p);
1571 return val1 != 1 ? val1 : val;
1574 /* Check two lists of types for compatibility, returning 0 for
1575 incompatible, 1 for compatible, or 2 for compatible with
1576 warning. ENUM_AND_INT_P is as in comptypes_internal. */
1578 static int
1579 type_lists_compatible_p (const_tree args1, const_tree args2,
1580 bool *enum_and_int_p)
1582 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1583 int val = 1;
1584 int newval = 0;
1586 while (1)
1588 tree a1, mv1, a2, mv2;
1589 if (args1 == 0 && args2 == 0)
1590 return val;
1591 /* If one list is shorter than the other,
1592 they fail to match. */
1593 if (args1 == 0 || args2 == 0)
1594 return 0;
1595 mv1 = a1 = TREE_VALUE (args1);
1596 mv2 = a2 = TREE_VALUE (args2);
1597 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1598 mv1 = TYPE_MAIN_VARIANT (mv1);
1599 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1600 mv2 = TYPE_MAIN_VARIANT (mv2);
1601 /* A null pointer instead of a type
1602 means there is supposed to be an argument
1603 but nothing is specified about what type it has.
1604 So match anything that self-promotes. */
1605 if (a1 == 0)
1607 if (c_type_promotes_to (a2) != a2)
1608 return 0;
1610 else if (a2 == 0)
1612 if (c_type_promotes_to (a1) != a1)
1613 return 0;
1615 /* If one of the lists has an error marker, ignore this arg. */
1616 else if (TREE_CODE (a1) == ERROR_MARK
1617 || TREE_CODE (a2) == ERROR_MARK)
1619 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p)))
1621 /* Allow wait (union {union wait *u; int *i} *)
1622 and wait (union wait *) to be compatible. */
1623 if (TREE_CODE (a1) == UNION_TYPE
1624 && (TYPE_NAME (a1) == 0
1625 || TYPE_TRANSPARENT_AGGR (a1))
1626 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1627 && tree_int_cst_equal (TYPE_SIZE (a1),
1628 TYPE_SIZE (a2)))
1630 tree memb;
1631 for (memb = TYPE_FIELDS (a1);
1632 memb; memb = TREE_CHAIN (memb))
1634 tree mv3 = TREE_TYPE (memb);
1635 if (mv3 && mv3 != error_mark_node
1636 && TREE_CODE (mv3) != ARRAY_TYPE)
1637 mv3 = TYPE_MAIN_VARIANT (mv3);
1638 if (comptypes_internal (mv3, mv2, enum_and_int_p))
1639 break;
1641 if (memb == 0)
1642 return 0;
1644 else if (TREE_CODE (a2) == UNION_TYPE
1645 && (TYPE_NAME (a2) == 0
1646 || TYPE_TRANSPARENT_AGGR (a2))
1647 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1648 && tree_int_cst_equal (TYPE_SIZE (a2),
1649 TYPE_SIZE (a1)))
1651 tree memb;
1652 for (memb = TYPE_FIELDS (a2);
1653 memb; memb = TREE_CHAIN (memb))
1655 tree mv3 = TREE_TYPE (memb);
1656 if (mv3 && mv3 != error_mark_node
1657 && TREE_CODE (mv3) != ARRAY_TYPE)
1658 mv3 = TYPE_MAIN_VARIANT (mv3);
1659 if (comptypes_internal (mv3, mv1, enum_and_int_p))
1660 break;
1662 if (memb == 0)
1663 return 0;
1665 else
1666 return 0;
1669 /* comptypes said ok, but record if it said to warn. */
1670 if (newval > val)
1671 val = newval;
1673 args1 = TREE_CHAIN (args1);
1674 args2 = TREE_CHAIN (args2);
1678 /* Compute the size to increment a pointer by. */
1680 static tree
1681 c_size_in_bytes (const_tree type)
1683 enum tree_code code = TREE_CODE (type);
1685 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1686 return size_one_node;
1688 if (!COMPLETE_OR_VOID_TYPE_P (type))
1690 error ("arithmetic on pointer to an incomplete type");
1691 return size_one_node;
1694 /* Convert in case a char is more than one unit. */
1695 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1696 size_int (TYPE_PRECISION (char_type_node)
1697 / BITS_PER_UNIT));
1700 /* Return either DECL or its known constant value (if it has one). */
1702 tree
1703 decl_constant_value (tree decl)
1705 if (/* Don't change a variable array bound or initial value to a constant
1706 in a place where a variable is invalid. Note that DECL_INITIAL
1707 isn't valid for a PARM_DECL. */
1708 current_function_decl != 0
1709 && TREE_CODE (decl) != PARM_DECL
1710 && !TREE_THIS_VOLATILE (decl)
1711 && TREE_READONLY (decl)
1712 && DECL_INITIAL (decl) != 0
1713 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1714 /* This is invalid if initial value is not constant.
1715 If it has either a function call, a memory reference,
1716 or a variable, then re-evaluating it could give different results. */
1717 && TREE_CONSTANT (DECL_INITIAL (decl))
1718 /* Check for cases where this is sub-optimal, even though valid. */
1719 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1720 return DECL_INITIAL (decl);
1721 return decl;
1724 /* Convert the array expression EXP to a pointer. */
1725 static tree
1726 array_to_pointer_conversion (location_t loc, tree exp)
1728 tree orig_exp = exp;
1729 tree type = TREE_TYPE (exp);
1730 tree adr;
1731 tree restype = TREE_TYPE (type);
1732 tree ptrtype;
1734 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1736 STRIP_TYPE_NOPS (exp);
1738 if (TREE_NO_WARNING (orig_exp))
1739 TREE_NO_WARNING (exp) = 1;
1741 ptrtype = build_pointer_type (restype);
1743 if (TREE_CODE (exp) == INDIRECT_REF)
1744 return convert (ptrtype, TREE_OPERAND (exp, 0));
1746 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1747 return convert (ptrtype, adr);
1750 /* Convert the function expression EXP to a pointer. */
1751 static tree
1752 function_to_pointer_conversion (location_t loc, tree exp)
1754 tree orig_exp = exp;
1756 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1758 STRIP_TYPE_NOPS (exp);
1760 if (TREE_NO_WARNING (orig_exp))
1761 TREE_NO_WARNING (exp) = 1;
1763 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1766 /* Perform the default conversion of arrays and functions to pointers.
1767 Return the result of converting EXP. For any other expression, just
1768 return EXP.
1770 LOC is the location of the expression. */
1772 struct c_expr
1773 default_function_array_conversion (location_t loc, struct c_expr exp)
1775 tree orig_exp = exp.value;
1776 tree type = TREE_TYPE (exp.value);
1777 enum tree_code code = TREE_CODE (type);
1779 switch (code)
1781 case ARRAY_TYPE:
1783 bool not_lvalue = false;
1784 bool lvalue_array_p;
1786 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1787 || CONVERT_EXPR_P (exp.value))
1788 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1790 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1791 not_lvalue = true;
1792 exp.value = TREE_OPERAND (exp.value, 0);
1795 if (TREE_NO_WARNING (orig_exp))
1796 TREE_NO_WARNING (exp.value) = 1;
1798 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1799 if (!flag_isoc99 && !lvalue_array_p)
1801 /* Before C99, non-lvalue arrays do not decay to pointers.
1802 Normally, using such an array would be invalid; but it can
1803 be used correctly inside sizeof or as a statement expression.
1804 Thus, do not give an error here; an error will result later. */
1805 return exp;
1808 exp.value = array_to_pointer_conversion (loc, exp.value);
1810 break;
1811 case FUNCTION_TYPE:
1812 exp.value = function_to_pointer_conversion (loc, exp.value);
1813 break;
1814 default:
1815 break;
1818 return exp;
1822 /* EXP is an expression of integer type. Apply the integer promotions
1823 to it and return the promoted value. */
1825 tree
1826 perform_integral_promotions (tree exp)
1828 tree type = TREE_TYPE (exp);
1829 enum tree_code code = TREE_CODE (type);
1831 gcc_assert (INTEGRAL_TYPE_P (type));
1833 /* Normally convert enums to int,
1834 but convert wide enums to something wider. */
1835 if (code == ENUMERAL_TYPE)
1837 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1838 TYPE_PRECISION (integer_type_node)),
1839 ((TYPE_PRECISION (type)
1840 >= TYPE_PRECISION (integer_type_node))
1841 && TYPE_UNSIGNED (type)));
1843 return convert (type, exp);
1846 /* ??? This should no longer be needed now bit-fields have their
1847 proper types. */
1848 if (TREE_CODE (exp) == COMPONENT_REF
1849 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1850 /* If it's thinner than an int, promote it like a
1851 c_promoting_integer_type_p, otherwise leave it alone. */
1852 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1853 TYPE_PRECISION (integer_type_node)))
1854 return convert (integer_type_node, exp);
1856 if (c_promoting_integer_type_p (type))
1858 /* Preserve unsignedness if not really getting any wider. */
1859 if (TYPE_UNSIGNED (type)
1860 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1861 return convert (unsigned_type_node, exp);
1863 return convert (integer_type_node, exp);
1866 return exp;
1870 /* Perform default promotions for C data used in expressions.
1871 Enumeral types or short or char are converted to int.
1872 In addition, manifest constants symbols are replaced by their values. */
1874 tree
1875 default_conversion (tree exp)
1877 tree orig_exp;
1878 tree type = TREE_TYPE (exp);
1879 enum tree_code code = TREE_CODE (type);
1880 tree promoted_type;
1882 /* Functions and arrays have been converted during parsing. */
1883 gcc_assert (code != FUNCTION_TYPE);
1884 if (code == ARRAY_TYPE)
1885 return exp;
1887 /* Constants can be used directly unless they're not loadable. */
1888 if (TREE_CODE (exp) == CONST_DECL)
1889 exp = DECL_INITIAL (exp);
1891 /* Strip no-op conversions. */
1892 orig_exp = exp;
1893 STRIP_TYPE_NOPS (exp);
1895 if (TREE_NO_WARNING (orig_exp))
1896 TREE_NO_WARNING (exp) = 1;
1898 if (code == VOID_TYPE)
1900 error ("void value not ignored as it ought to be");
1901 return error_mark_node;
1904 exp = require_complete_type (exp);
1905 if (exp == error_mark_node)
1906 return error_mark_node;
1908 promoted_type = targetm.promoted_type (type);
1909 if (promoted_type)
1910 return convert (promoted_type, exp);
1912 if (INTEGRAL_TYPE_P (type))
1913 return perform_integral_promotions (exp);
1915 return exp;
1918 /* Look up COMPONENT in a structure or union DECL.
1920 If the component name is not found, returns NULL_TREE. Otherwise,
1921 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1922 stepping down the chain to the component, which is in the last
1923 TREE_VALUE of the list. Normally the list is of length one, but if
1924 the component is embedded within (nested) anonymous structures or
1925 unions, the list steps down the chain to the component. */
1927 static tree
1928 lookup_field (tree decl, tree component)
1930 tree type = TREE_TYPE (decl);
1931 tree field;
1933 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1934 to the field elements. Use a binary search on this array to quickly
1935 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1936 will always be set for structures which have many elements. */
1938 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1940 int bot, top, half;
1941 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1943 field = TYPE_FIELDS (type);
1944 bot = 0;
1945 top = TYPE_LANG_SPECIFIC (type)->s->len;
1946 while (top - bot > 1)
1948 half = (top - bot + 1) >> 1;
1949 field = field_array[bot+half];
1951 if (DECL_NAME (field) == NULL_TREE)
1953 /* Step through all anon unions in linear fashion. */
1954 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1956 field = field_array[bot++];
1957 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1958 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1960 tree anon = lookup_field (field, component);
1962 if (anon)
1963 return tree_cons (NULL_TREE, field, anon);
1967 /* Entire record is only anon unions. */
1968 if (bot > top)
1969 return NULL_TREE;
1971 /* Restart the binary search, with new lower bound. */
1972 continue;
1975 if (DECL_NAME (field) == component)
1976 break;
1977 if (DECL_NAME (field) < component)
1978 bot += half;
1979 else
1980 top = bot + half;
1983 if (DECL_NAME (field_array[bot]) == component)
1984 field = field_array[bot];
1985 else if (DECL_NAME (field) != component)
1986 return NULL_TREE;
1988 else
1990 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1992 if (DECL_NAME (field) == NULL_TREE
1993 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1994 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1996 tree anon = lookup_field (field, component);
1998 if (anon)
1999 return tree_cons (NULL_TREE, field, anon);
2002 if (DECL_NAME (field) == component)
2003 break;
2006 if (field == NULL_TREE)
2007 return NULL_TREE;
2010 return tree_cons (NULL_TREE, field, NULL_TREE);
2013 /* Make an expression to refer to the COMPONENT field of structure or
2014 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2015 location of the COMPONENT_REF. */
2017 tree
2018 build_component_ref (location_t loc, tree datum, tree component)
2020 tree type = TREE_TYPE (datum);
2021 enum tree_code code = TREE_CODE (type);
2022 tree field = NULL;
2023 tree ref;
2024 bool datum_lvalue = lvalue_p (datum);
2026 if (!objc_is_public (datum, component))
2027 return error_mark_node;
2029 /* See if there is a field or component with name COMPONENT. */
2031 if (code == RECORD_TYPE || code == UNION_TYPE)
2033 if (!COMPLETE_TYPE_P (type))
2035 c_incomplete_type_error (NULL_TREE, type);
2036 return error_mark_node;
2039 field = lookup_field (datum, component);
2041 if (!field)
2043 error_at (loc, "%qT has no member named %qE", type, component);
2044 return error_mark_node;
2047 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2048 This might be better solved in future the way the C++ front
2049 end does it - by giving the anonymous entities each a
2050 separate name and type, and then have build_component_ref
2051 recursively call itself. We can't do that here. */
2054 tree subdatum = TREE_VALUE (field);
2055 int quals;
2056 tree subtype;
2057 bool use_datum_quals;
2059 if (TREE_TYPE (subdatum) == error_mark_node)
2060 return error_mark_node;
2062 /* If this is an rvalue, it does not have qualifiers in C
2063 standard terms and we must avoid propagating such
2064 qualifiers down to a non-lvalue array that is then
2065 converted to a pointer. */
2066 use_datum_quals = (datum_lvalue
2067 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2069 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2070 if (use_datum_quals)
2071 quals |= TYPE_QUALS (TREE_TYPE (datum));
2072 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2074 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2075 NULL_TREE);
2076 SET_EXPR_LOCATION (ref, loc);
2077 if (TREE_READONLY (subdatum)
2078 || (use_datum_quals && TREE_READONLY (datum)))
2079 TREE_READONLY (ref) = 1;
2080 if (TREE_THIS_VOLATILE (subdatum)
2081 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2082 TREE_THIS_VOLATILE (ref) = 1;
2084 if (TREE_DEPRECATED (subdatum))
2085 warn_deprecated_use (subdatum, NULL_TREE);
2087 datum = ref;
2089 field = TREE_CHAIN (field);
2091 while (field);
2093 return ref;
2095 else if (code != ERROR_MARK)
2096 error_at (loc,
2097 "request for member %qE in something not a structure or union",
2098 component);
2100 return error_mark_node;
2103 /* Given an expression PTR for a pointer, return an expression
2104 for the value pointed to.
2105 ERRORSTRING is the name of the operator to appear in error messages.
2107 LOC is the location to use for the generated tree. */
2109 tree
2110 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2112 tree pointer = default_conversion (ptr);
2113 tree type = TREE_TYPE (pointer);
2114 tree ref;
2116 if (TREE_CODE (type) == POINTER_TYPE)
2118 if (CONVERT_EXPR_P (pointer)
2119 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2121 /* If a warning is issued, mark it to avoid duplicates from
2122 the backend. This only needs to be done at
2123 warn_strict_aliasing > 2. */
2124 if (warn_strict_aliasing > 2)
2125 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2126 type, TREE_OPERAND (pointer, 0)))
2127 TREE_NO_WARNING (pointer) = 1;
2130 if (TREE_CODE (pointer) == ADDR_EXPR
2131 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2132 == TREE_TYPE (type)))
2134 ref = TREE_OPERAND (pointer, 0);
2135 protected_set_expr_location (ref, loc);
2136 return ref;
2138 else
2140 tree t = TREE_TYPE (type);
2142 ref = build1 (INDIRECT_REF, t, pointer);
2144 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2146 error_at (loc, "dereferencing pointer to incomplete type");
2147 return error_mark_node;
2149 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2150 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2152 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2153 so that we get the proper error message if the result is used
2154 to assign to. Also, &* is supposed to be a no-op.
2155 And ANSI C seems to specify that the type of the result
2156 should be the const type. */
2157 /* A de-reference of a pointer to const is not a const. It is valid
2158 to change it via some other pointer. */
2159 TREE_READONLY (ref) = TYPE_READONLY (t);
2160 TREE_SIDE_EFFECTS (ref)
2161 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2162 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2163 protected_set_expr_location (ref, loc);
2164 return ref;
2167 else if (TREE_CODE (pointer) != ERROR_MARK)
2168 switch (errstring)
2170 case RO_ARRAY_INDEXING:
2171 error_at (loc,
2172 "invalid type argument of array indexing (have %qT)",
2173 type);
2174 break;
2175 case RO_UNARY_STAR:
2176 error_at (loc,
2177 "invalid type argument of unary %<*%> (have %qT)",
2178 type);
2179 break;
2180 case RO_ARROW:
2181 error_at (loc,
2182 "invalid type argument of %<->%> (have %qT)",
2183 type);
2184 break;
2185 default:
2186 gcc_unreachable ();
2188 return error_mark_node;
2191 /* This handles expressions of the form "a[i]", which denotes
2192 an array reference.
2194 This is logically equivalent in C to *(a+i), but we may do it differently.
2195 If A is a variable or a member, we generate a primitive ARRAY_REF.
2196 This avoids forcing the array out of registers, and can work on
2197 arrays that are not lvalues (for example, members of structures returned
2198 by functions).
2200 LOC is the location to use for the returned expression. */
2202 tree
2203 build_array_ref (location_t loc, tree array, tree index)
2205 tree ret;
2206 bool swapped = false;
2207 if (TREE_TYPE (array) == error_mark_node
2208 || TREE_TYPE (index) == error_mark_node)
2209 return error_mark_node;
2211 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2212 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2214 tree temp;
2215 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2216 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2218 error_at (loc, "subscripted value is neither array nor pointer");
2219 return error_mark_node;
2221 temp = array;
2222 array = index;
2223 index = temp;
2224 swapped = true;
2227 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2229 error_at (loc, "array subscript is not an integer");
2230 return error_mark_node;
2233 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2235 error_at (loc, "subscripted value is pointer to function");
2236 return error_mark_node;
2239 /* ??? Existing practice has been to warn only when the char
2240 index is syntactically the index, not for char[array]. */
2241 if (!swapped)
2242 warn_array_subscript_with_type_char (index);
2244 /* Apply default promotions *after* noticing character types. */
2245 index = default_conversion (index);
2247 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2249 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2251 tree rval, type;
2253 /* An array that is indexed by a non-constant
2254 cannot be stored in a register; we must be able to do
2255 address arithmetic on its address.
2256 Likewise an array of elements of variable size. */
2257 if (TREE_CODE (index) != INTEGER_CST
2258 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2259 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2261 if (!c_mark_addressable (array))
2262 return error_mark_node;
2264 /* An array that is indexed by a constant value which is not within
2265 the array bounds cannot be stored in a register either; because we
2266 would get a crash in store_bit_field/extract_bit_field when trying
2267 to access a non-existent part of the register. */
2268 if (TREE_CODE (index) == INTEGER_CST
2269 && TYPE_DOMAIN (TREE_TYPE (array))
2270 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2272 if (!c_mark_addressable (array))
2273 return error_mark_node;
2276 if (pedantic)
2278 tree foo = array;
2279 while (TREE_CODE (foo) == COMPONENT_REF)
2280 foo = TREE_OPERAND (foo, 0);
2281 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2282 pedwarn (loc, OPT_pedantic,
2283 "ISO C forbids subscripting %<register%> array");
2284 else if (!flag_isoc99 && !lvalue_p (foo))
2285 pedwarn (loc, OPT_pedantic,
2286 "ISO C90 forbids subscripting non-lvalue array");
2289 type = TREE_TYPE (TREE_TYPE (array));
2290 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2291 /* Array ref is const/volatile if the array elements are
2292 or if the array is. */
2293 TREE_READONLY (rval)
2294 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2295 | TREE_READONLY (array));
2296 TREE_SIDE_EFFECTS (rval)
2297 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2298 | TREE_SIDE_EFFECTS (array));
2299 TREE_THIS_VOLATILE (rval)
2300 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2301 /* This was added by rms on 16 Nov 91.
2302 It fixes vol struct foo *a; a->elts[1]
2303 in an inline function.
2304 Hope it doesn't break something else. */
2305 | TREE_THIS_VOLATILE (array));
2306 ret = require_complete_type (rval);
2307 protected_set_expr_location (ret, loc);
2308 return ret;
2310 else
2312 tree ar = default_conversion (array);
2314 if (ar == error_mark_node)
2315 return ar;
2317 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2318 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2320 return build_indirect_ref
2321 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2322 RO_ARRAY_INDEXING);
2326 /* Build an external reference to identifier ID. FUN indicates
2327 whether this will be used for a function call. LOC is the source
2328 location of the identifier. This sets *TYPE to the type of the
2329 identifier, which is not the same as the type of the returned value
2330 for CONST_DECLs defined as enum constants. If the type of the
2331 identifier is not available, *TYPE is set to NULL. */
2332 tree
2333 build_external_ref (location_t loc, tree id, int fun, tree *type)
2335 tree ref;
2336 tree decl = lookup_name (id);
2338 /* In Objective-C, an instance variable (ivar) may be preferred to
2339 whatever lookup_name() found. */
2340 decl = objc_lookup_ivar (decl, id);
2342 *type = NULL;
2343 if (decl && decl != error_mark_node)
2345 ref = decl;
2346 *type = TREE_TYPE (ref);
2348 else if (fun)
2349 /* Implicit function declaration. */
2350 ref = implicitly_declare (loc, id);
2351 else if (decl == error_mark_node)
2352 /* Don't complain about something that's already been
2353 complained about. */
2354 return error_mark_node;
2355 else
2357 undeclared_variable (loc, id);
2358 return error_mark_node;
2361 if (TREE_TYPE (ref) == error_mark_node)
2362 return error_mark_node;
2364 if (TREE_DEPRECATED (ref))
2365 warn_deprecated_use (ref, NULL_TREE);
2367 /* Recursive call does not count as usage. */
2368 if (ref != current_function_decl)
2370 TREE_USED (ref) = 1;
2373 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2375 if (!in_sizeof && !in_typeof)
2376 C_DECL_USED (ref) = 1;
2377 else if (DECL_INITIAL (ref) == 0
2378 && DECL_EXTERNAL (ref)
2379 && !TREE_PUBLIC (ref))
2380 record_maybe_used_decl (ref);
2383 if (TREE_CODE (ref) == CONST_DECL)
2385 used_types_insert (TREE_TYPE (ref));
2387 if (warn_cxx_compat
2388 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2389 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2391 warning_at (loc, OPT_Wc___compat,
2392 ("enum constant defined in struct or union "
2393 "is not visible in C++"));
2394 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2397 ref = DECL_INITIAL (ref);
2398 TREE_CONSTANT (ref) = 1;
2400 else if (current_function_decl != 0
2401 && !DECL_FILE_SCOPE_P (current_function_decl)
2402 && (TREE_CODE (ref) == VAR_DECL
2403 || TREE_CODE (ref) == PARM_DECL
2404 || TREE_CODE (ref) == FUNCTION_DECL))
2406 tree context = decl_function_context (ref);
2408 if (context != 0 && context != current_function_decl)
2409 DECL_NONLOCAL (ref) = 1;
2411 /* C99 6.7.4p3: An inline definition of a function with external
2412 linkage ... shall not contain a reference to an identifier with
2413 internal linkage. */
2414 else if (current_function_decl != 0
2415 && DECL_DECLARED_INLINE_P (current_function_decl)
2416 && DECL_EXTERNAL (current_function_decl)
2417 && VAR_OR_FUNCTION_DECL_P (ref)
2418 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2419 && ! TREE_PUBLIC (ref)
2420 && DECL_CONTEXT (ref) != current_function_decl)
2421 record_inline_static (loc, current_function_decl, ref,
2422 csi_internal);
2424 return ref;
2427 /* Record details of decls possibly used inside sizeof or typeof. */
2428 struct maybe_used_decl
2430 /* The decl. */
2431 tree decl;
2432 /* The level seen at (in_sizeof + in_typeof). */
2433 int level;
2434 /* The next one at this level or above, or NULL. */
2435 struct maybe_used_decl *next;
2438 static struct maybe_used_decl *maybe_used_decls;
2440 /* Record that DECL, an undefined static function reference seen
2441 inside sizeof or typeof, might be used if the operand of sizeof is
2442 a VLA type or the operand of typeof is a variably modified
2443 type. */
2445 static void
2446 record_maybe_used_decl (tree decl)
2448 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2449 t->decl = decl;
2450 t->level = in_sizeof + in_typeof;
2451 t->next = maybe_used_decls;
2452 maybe_used_decls = t;
2455 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2456 USED is false, just discard them. If it is true, mark them used
2457 (if no longer inside sizeof or typeof) or move them to the next
2458 level up (if still inside sizeof or typeof). */
2460 void
2461 pop_maybe_used (bool used)
2463 struct maybe_used_decl *p = maybe_used_decls;
2464 int cur_level = in_sizeof + in_typeof;
2465 while (p && p->level > cur_level)
2467 if (used)
2469 if (cur_level == 0)
2470 C_DECL_USED (p->decl) = 1;
2471 else
2472 p->level = cur_level;
2474 p = p->next;
2476 if (!used || cur_level == 0)
2477 maybe_used_decls = p;
2480 /* Return the result of sizeof applied to EXPR. */
2482 struct c_expr
2483 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2485 struct c_expr ret;
2486 if (expr.value == error_mark_node)
2488 ret.value = error_mark_node;
2489 ret.original_code = ERROR_MARK;
2490 ret.original_type = NULL;
2491 pop_maybe_used (false);
2493 else
2495 bool expr_const_operands = true;
2496 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2497 &expr_const_operands);
2498 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2499 ret.original_code = ERROR_MARK;
2500 ret.original_type = NULL;
2501 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2503 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2504 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2505 folded_expr, ret.value);
2506 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2507 SET_EXPR_LOCATION (ret.value, loc);
2509 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2511 return ret;
2514 /* Return the result of sizeof applied to T, a structure for the type
2515 name passed to sizeof (rather than the type itself). LOC is the
2516 location of the original expression. */
2518 struct c_expr
2519 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2521 tree type;
2522 struct c_expr ret;
2523 tree type_expr = NULL_TREE;
2524 bool type_expr_const = true;
2525 type = groktypename (t, &type_expr, &type_expr_const);
2526 ret.value = c_sizeof (loc, type);
2527 ret.original_code = ERROR_MARK;
2528 ret.original_type = NULL;
2529 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2530 && c_vla_type_p (type))
2532 /* If the type is a [*] array, it is a VLA but is represented as
2533 having a size of zero. In such a case we must ensure that
2534 the result of sizeof does not get folded to a constant by
2535 c_fully_fold, because if the size is evaluated the result is
2536 not constant and so constraints on zero or negative size
2537 arrays must not be applied when this sizeof call is inside
2538 another array declarator. */
2539 if (!type_expr)
2540 type_expr = integer_zero_node;
2541 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2542 type_expr, ret.value);
2543 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2545 pop_maybe_used (type != error_mark_node
2546 ? C_TYPE_VARIABLE_SIZE (type) : false);
2547 return ret;
2550 /* Build a function call to function FUNCTION with parameters PARAMS.
2551 The function call is at LOC.
2552 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2553 TREE_VALUE of each node is a parameter-expression.
2554 FUNCTION's data type may be a function type or a pointer-to-function. */
2556 tree
2557 build_function_call (location_t loc, tree function, tree params)
2559 VEC(tree,gc) *vec;
2560 tree ret;
2562 vec = VEC_alloc (tree, gc, list_length (params));
2563 for (; params; params = TREE_CHAIN (params))
2564 VEC_quick_push (tree, vec, TREE_VALUE (params));
2565 ret = build_function_call_vec (loc, function, vec, NULL);
2566 VEC_free (tree, gc, vec);
2567 return ret;
2570 /* Build a function call to function FUNCTION with parameters PARAMS.
2571 ORIGTYPES, if not NULL, is a vector of types; each element is
2572 either NULL or the original type of the corresponding element in
2573 PARAMS. The original type may differ from TREE_TYPE of the
2574 parameter for enums. FUNCTION's data type may be a function type
2575 or pointer-to-function. This function changes the elements of
2576 PARAMS. */
2578 tree
2579 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2580 VEC(tree,gc) *origtypes)
2582 tree fntype, fundecl = 0;
2583 tree name = NULL_TREE, result;
2584 tree tem;
2585 int nargs;
2586 tree *argarray;
2589 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2590 STRIP_TYPE_NOPS (function);
2592 /* Convert anything with function type to a pointer-to-function. */
2593 if (TREE_CODE (function) == FUNCTION_DECL)
2595 /* Implement type-directed function overloading for builtins.
2596 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2597 handle all the type checking. The result is a complete expression
2598 that implements this function call. */
2599 tem = resolve_overloaded_builtin (loc, function, params);
2600 if (tem)
2601 return tem;
2603 name = DECL_NAME (function);
2604 fundecl = function;
2606 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2607 function = function_to_pointer_conversion (loc, function);
2609 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2610 expressions, like those used for ObjC messenger dispatches. */
2611 if (!VEC_empty (tree, params))
2612 function = objc_rewrite_function_call (function,
2613 VEC_index (tree, params, 0));
2615 function = c_fully_fold (function, false, NULL);
2617 fntype = TREE_TYPE (function);
2619 if (TREE_CODE (fntype) == ERROR_MARK)
2620 return error_mark_node;
2622 if (!(TREE_CODE (fntype) == POINTER_TYPE
2623 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2625 error_at (loc, "called object %qE is not a function", function);
2626 return error_mark_node;
2629 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2630 current_function_returns_abnormally = 1;
2632 /* fntype now gets the type of function pointed to. */
2633 fntype = TREE_TYPE (fntype);
2635 /* Convert the parameters to the types declared in the
2636 function prototype, or apply default promotions. */
2638 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2639 function, fundecl);
2640 if (nargs < 0)
2641 return error_mark_node;
2643 /* Check that the function is called through a compatible prototype.
2644 If it is not, replace the call by a trap, wrapped up in a compound
2645 expression if necessary. This has the nice side-effect to prevent
2646 the tree-inliner from generating invalid assignment trees which may
2647 blow up in the RTL expander later. */
2648 if (CONVERT_EXPR_P (function)
2649 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2650 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2651 && !comptypes (fntype, TREE_TYPE (tem)))
2653 tree return_type = TREE_TYPE (fntype);
2654 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2655 NULL_TREE);
2656 int i;
2658 /* This situation leads to run-time undefined behavior. We can't,
2659 therefore, simply error unless we can prove that all possible
2660 executions of the program must execute the code. */
2661 if (warning_at (loc, 0, "function called through a non-compatible type"))
2662 /* We can, however, treat "undefined" any way we please.
2663 Call abort to encourage the user to fix the program. */
2664 inform (loc, "if this code is reached, the program will abort");
2665 /* Before the abort, allow the function arguments to exit or
2666 call longjmp. */
2667 for (i = 0; i < nargs; i++)
2668 trap = build2 (COMPOUND_EXPR, void_type_node,
2669 VEC_index (tree, params, i), trap);
2671 if (VOID_TYPE_P (return_type))
2673 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2674 pedwarn (loc, 0,
2675 "function with qualified void return type called");
2676 return trap;
2678 else
2680 tree rhs;
2682 if (AGGREGATE_TYPE_P (return_type))
2683 rhs = build_compound_literal (loc, return_type,
2684 build_constructor (return_type, 0),
2685 false);
2686 else
2687 rhs = fold_convert_loc (loc, return_type, integer_zero_node);
2689 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2690 trap, rhs));
2694 argarray = VEC_address (tree, params);
2696 /* Check that arguments to builtin functions match the expectations. */
2697 if (fundecl
2698 && DECL_BUILT_IN (fundecl)
2699 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2700 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2701 return error_mark_node;
2703 /* Check that the arguments to the function are valid. */
2704 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2705 TYPE_ARG_TYPES (fntype));
2707 if (name != NULL_TREE
2708 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2710 if (require_constant_value)
2711 result =
2712 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2713 function, nargs, argarray);
2714 else
2715 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2716 function, nargs, argarray);
2717 if (TREE_CODE (result) == NOP_EXPR
2718 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2719 STRIP_TYPE_NOPS (result);
2721 else
2722 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2723 function, nargs, argarray);
2725 if (VOID_TYPE_P (TREE_TYPE (result)))
2727 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2728 pedwarn (loc, 0,
2729 "function with qualified void return type called");
2730 return result;
2732 return require_complete_type (result);
2735 /* Convert the argument expressions in the vector VALUES
2736 to the types in the list TYPELIST.
2738 If TYPELIST is exhausted, or when an element has NULL as its type,
2739 perform the default conversions.
2741 ORIGTYPES is the original types of the expressions in VALUES. This
2742 holds the type of enum values which have been converted to integral
2743 types. It may be NULL.
2745 FUNCTION is a tree for the called function. It is used only for
2746 error messages, where it is formatted with %qE.
2748 This is also where warnings about wrong number of args are generated.
2750 Returns the actual number of arguments processed (which may be less
2751 than the length of VALUES in some error situations), or -1 on
2752 failure. */
2754 static int
2755 convert_arguments (tree typelist, VEC(tree,gc) *values,
2756 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2758 tree typetail, val;
2759 unsigned int parmnum;
2760 bool error_args = false;
2761 const bool type_generic = fundecl
2762 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2763 bool type_generic_remove_excess_precision = false;
2764 tree selector;
2766 /* Change pointer to function to the function itself for
2767 diagnostics. */
2768 if (TREE_CODE (function) == ADDR_EXPR
2769 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2770 function = TREE_OPERAND (function, 0);
2772 /* Handle an ObjC selector specially for diagnostics. */
2773 selector = objc_message_selector ();
2775 /* For type-generic built-in functions, determine whether excess
2776 precision should be removed (classification) or not
2777 (comparison). */
2778 if (type_generic
2779 && DECL_BUILT_IN (fundecl)
2780 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2782 switch (DECL_FUNCTION_CODE (fundecl))
2784 case BUILT_IN_ISFINITE:
2785 case BUILT_IN_ISINF:
2786 case BUILT_IN_ISINF_SIGN:
2787 case BUILT_IN_ISNAN:
2788 case BUILT_IN_ISNORMAL:
2789 case BUILT_IN_FPCLASSIFY:
2790 type_generic_remove_excess_precision = true;
2791 break;
2793 default:
2794 type_generic_remove_excess_precision = false;
2795 break;
2799 /* Scan the given expressions and types, producing individual
2800 converted arguments. */
2802 for (typetail = typelist, parmnum = 0;
2803 VEC_iterate (tree, values, parmnum, val);
2804 ++parmnum)
2806 tree type = typetail ? TREE_VALUE (typetail) : 0;
2807 tree valtype = TREE_TYPE (val);
2808 tree rname = function;
2809 int argnum = parmnum + 1;
2810 const char *invalid_func_diag;
2811 bool excess_precision = false;
2812 bool npc;
2813 tree parmval;
2815 if (type == void_type_node)
2817 error ("too many arguments to function %qE", function);
2818 return parmnum;
2821 if (selector && argnum > 2)
2823 rname = selector;
2824 argnum -= 2;
2827 npc = null_pointer_constant_p (val);
2829 /* If there is excess precision and a prototype, convert once to
2830 the required type rather than converting via the semantic
2831 type. Likewise without a prototype a float value represented
2832 as long double should be converted once to double. But for
2833 type-generic classification functions excess precision must
2834 be removed here. */
2835 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2836 && (type || !type_generic || !type_generic_remove_excess_precision))
2838 val = TREE_OPERAND (val, 0);
2839 excess_precision = true;
2841 val = c_fully_fold (val, false, NULL);
2842 STRIP_TYPE_NOPS (val);
2844 val = require_complete_type (val);
2846 if (type != 0)
2848 /* Formal parm type is specified by a function prototype. */
2850 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2852 error ("type of formal parameter %d is incomplete", parmnum + 1);
2853 parmval = val;
2855 else
2857 tree origtype;
2859 /* Optionally warn about conversions that
2860 differ from the default conversions. */
2861 if (warn_traditional_conversion || warn_traditional)
2863 unsigned int formal_prec = TYPE_PRECISION (type);
2865 if (INTEGRAL_TYPE_P (type)
2866 && TREE_CODE (valtype) == REAL_TYPE)
2867 warning (0, "passing argument %d of %qE as integer "
2868 "rather than floating due to prototype",
2869 argnum, rname);
2870 if (INTEGRAL_TYPE_P (type)
2871 && TREE_CODE (valtype) == COMPLEX_TYPE)
2872 warning (0, "passing argument %d of %qE as integer "
2873 "rather than complex due to prototype",
2874 argnum, rname);
2875 else if (TREE_CODE (type) == COMPLEX_TYPE
2876 && TREE_CODE (valtype) == REAL_TYPE)
2877 warning (0, "passing argument %d of %qE as complex "
2878 "rather than floating due to prototype",
2879 argnum, rname);
2880 else if (TREE_CODE (type) == REAL_TYPE
2881 && INTEGRAL_TYPE_P (valtype))
2882 warning (0, "passing argument %d of %qE as floating "
2883 "rather than integer due to prototype",
2884 argnum, rname);
2885 else if (TREE_CODE (type) == COMPLEX_TYPE
2886 && INTEGRAL_TYPE_P (valtype))
2887 warning (0, "passing argument %d of %qE as complex "
2888 "rather than integer due to prototype",
2889 argnum, rname);
2890 else if (TREE_CODE (type) == REAL_TYPE
2891 && TREE_CODE (valtype) == COMPLEX_TYPE)
2892 warning (0, "passing argument %d of %qE as floating "
2893 "rather than complex due to prototype",
2894 argnum, rname);
2895 /* ??? At some point, messages should be written about
2896 conversions between complex types, but that's too messy
2897 to do now. */
2898 else if (TREE_CODE (type) == REAL_TYPE
2899 && TREE_CODE (valtype) == REAL_TYPE)
2901 /* Warn if any argument is passed as `float',
2902 since without a prototype it would be `double'. */
2903 if (formal_prec == TYPE_PRECISION (float_type_node)
2904 && type != dfloat32_type_node)
2905 warning (0, "passing argument %d of %qE as %<float%> "
2906 "rather than %<double%> due to prototype",
2907 argnum, rname);
2909 /* Warn if mismatch between argument and prototype
2910 for decimal float types. Warn of conversions with
2911 binary float types and of precision narrowing due to
2912 prototype. */
2913 else if (type != valtype
2914 && (type == dfloat32_type_node
2915 || type == dfloat64_type_node
2916 || type == dfloat128_type_node
2917 || valtype == dfloat32_type_node
2918 || valtype == dfloat64_type_node
2919 || valtype == dfloat128_type_node)
2920 && (formal_prec
2921 <= TYPE_PRECISION (valtype)
2922 || (type == dfloat128_type_node
2923 && (valtype
2924 != dfloat64_type_node
2925 && (valtype
2926 != dfloat32_type_node)))
2927 || (type == dfloat64_type_node
2928 && (valtype
2929 != dfloat32_type_node))))
2930 warning (0, "passing argument %d of %qE as %qT "
2931 "rather than %qT due to prototype",
2932 argnum, rname, type, valtype);
2935 /* Detect integer changing in width or signedness.
2936 These warnings are only activated with
2937 -Wtraditional-conversion, not with -Wtraditional. */
2938 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2939 && INTEGRAL_TYPE_P (valtype))
2941 tree would_have_been = default_conversion (val);
2942 tree type1 = TREE_TYPE (would_have_been);
2944 if (TREE_CODE (type) == ENUMERAL_TYPE
2945 && (TYPE_MAIN_VARIANT (type)
2946 == TYPE_MAIN_VARIANT (valtype)))
2947 /* No warning if function asks for enum
2948 and the actual arg is that enum type. */
2950 else if (formal_prec != TYPE_PRECISION (type1))
2951 warning (OPT_Wtraditional_conversion,
2952 "passing argument %d of %qE "
2953 "with different width due to prototype",
2954 argnum, rname);
2955 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2957 /* Don't complain if the formal parameter type
2958 is an enum, because we can't tell now whether
2959 the value was an enum--even the same enum. */
2960 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2962 else if (TREE_CODE (val) == INTEGER_CST
2963 && int_fits_type_p (val, type))
2964 /* Change in signedness doesn't matter
2965 if a constant value is unaffected. */
2967 /* If the value is extended from a narrower
2968 unsigned type, it doesn't matter whether we
2969 pass it as signed or unsigned; the value
2970 certainly is the same either way. */
2971 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
2972 && TYPE_UNSIGNED (valtype))
2974 else if (TYPE_UNSIGNED (type))
2975 warning (OPT_Wtraditional_conversion,
2976 "passing argument %d of %qE "
2977 "as unsigned due to prototype",
2978 argnum, rname);
2979 else
2980 warning (OPT_Wtraditional_conversion,
2981 "passing argument %d of %qE "
2982 "as signed due to prototype", argnum, rname);
2986 /* Possibly restore an EXCESS_PRECISION_EXPR for the
2987 sake of better warnings from convert_and_check. */
2988 if (excess_precision)
2989 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
2990 origtype = (origtypes == NULL
2991 ? NULL_TREE
2992 : VEC_index (tree, origtypes, parmnum));
2993 parmval = convert_for_assignment (input_location, type, val,
2994 origtype, ic_argpass, npc,
2995 fundecl, function,
2996 parmnum + 1);
2998 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2999 && INTEGRAL_TYPE_P (type)
3000 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3001 parmval = default_conversion (parmval);
3004 else if (TREE_CODE (valtype) == REAL_TYPE
3005 && (TYPE_PRECISION (valtype)
3006 < TYPE_PRECISION (double_type_node))
3007 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3009 if (type_generic)
3010 parmval = val;
3011 else
3012 /* Convert `float' to `double'. */
3013 parmval = convert (double_type_node, val);
3015 else if (excess_precision && !type_generic)
3016 /* A "double" argument with excess precision being passed
3017 without a prototype or in variable arguments. */
3018 parmval = convert (valtype, val);
3019 else if ((invalid_func_diag =
3020 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3022 error (invalid_func_diag);
3023 return -1;
3025 else
3026 /* Convert `short' and `char' to full-size `int'. */
3027 parmval = default_conversion (val);
3029 VEC_replace (tree, values, parmnum, parmval);
3030 if (parmval == error_mark_node)
3031 error_args = true;
3033 if (typetail)
3034 typetail = TREE_CHAIN (typetail);
3037 gcc_assert (parmnum == VEC_length (tree, values));
3039 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3041 error ("too few arguments to function %qE", function);
3042 return -1;
3045 return error_args ? -1 : (int) parmnum;
3048 /* This is the entry point used by the parser to build unary operators
3049 in the input. CODE, a tree_code, specifies the unary operator, and
3050 ARG is the operand. For unary plus, the C parser currently uses
3051 CONVERT_EXPR for code.
3053 LOC is the location to use for the tree generated.
3056 struct c_expr
3057 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3059 struct c_expr result;
3061 result.value = build_unary_op (loc, code, arg.value, 0);
3062 result.original_code = code;
3063 result.original_type = NULL;
3065 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3066 overflow_warning (loc, result.value);
3068 return result;
3071 /* This is the entry point used by the parser to build binary operators
3072 in the input. CODE, a tree_code, specifies the binary operator, and
3073 ARG1 and ARG2 are the operands. In addition to constructing the
3074 expression, we check for operands that were written with other binary
3075 operators in a way that is likely to confuse the user.
3077 LOCATION is the location of the binary operator. */
3079 struct c_expr
3080 parser_build_binary_op (location_t location, enum tree_code code,
3081 struct c_expr arg1, struct c_expr arg2)
3083 struct c_expr result;
3085 enum tree_code code1 = arg1.original_code;
3086 enum tree_code code2 = arg2.original_code;
3087 tree type1 = (arg1.original_type
3088 ? arg1.original_type
3089 : TREE_TYPE (arg1.value));
3090 tree type2 = (arg2.original_type
3091 ? arg2.original_type
3092 : TREE_TYPE (arg2.value));
3094 result.value = build_binary_op (location, code,
3095 arg1.value, arg2.value, 1);
3096 result.original_code = code;
3097 result.original_type = NULL;
3099 if (TREE_CODE (result.value) == ERROR_MARK)
3100 return result;
3102 if (location != UNKNOWN_LOCATION)
3103 protected_set_expr_location (result.value, location);
3105 /* Check for cases such as x+y<<z which users are likely
3106 to misinterpret. */
3107 if (warn_parentheses)
3108 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3110 if (warn_logical_op)
3111 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3112 code1, arg1.value, code2, arg2.value);
3114 /* Warn about comparisons against string literals, with the exception
3115 of testing for equality or inequality of a string literal with NULL. */
3116 if (code == EQ_EXPR || code == NE_EXPR)
3118 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3119 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3120 warning_at (location, OPT_Waddress,
3121 "comparison with string literal results in unspecified behavior");
3123 else if (TREE_CODE_CLASS (code) == tcc_comparison
3124 && (code1 == STRING_CST || code2 == STRING_CST))
3125 warning_at (location, OPT_Waddress,
3126 "comparison with string literal results in unspecified behavior");
3128 if (TREE_OVERFLOW_P (result.value)
3129 && !TREE_OVERFLOW_P (arg1.value)
3130 && !TREE_OVERFLOW_P (arg2.value))
3131 overflow_warning (location, result.value);
3133 /* Warn about comparisons of different enum types. */
3134 if (warn_enum_compare
3135 && TREE_CODE_CLASS (code) == tcc_comparison
3136 && TREE_CODE (type1) == ENUMERAL_TYPE
3137 && TREE_CODE (type2) == ENUMERAL_TYPE
3138 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3139 warning_at (location, OPT_Wenum_compare,
3140 "comparison between %qT and %qT",
3141 type1, type2);
3143 return result;
3146 /* Return a tree for the difference of pointers OP0 and OP1.
3147 The resulting tree has type int. */
3149 static tree
3150 pointer_diff (location_t loc, tree op0, tree op1)
3152 tree restype = ptrdiff_type_node;
3153 tree result, inttype;
3155 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3156 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3157 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3158 tree con0, con1, lit0, lit1;
3159 tree orig_op1 = op1;
3161 /* If the operands point into different address spaces, we need to
3162 explicitly convert them to pointers into the common address space
3163 before we can subtract the numerical address values. */
3164 if (as0 != as1)
3166 addr_space_t as_common;
3167 tree common_type;
3169 /* Determine the common superset address space. This is guaranteed
3170 to exist because the caller verified that comp_target_types
3171 returned non-zero. */
3172 if (!addr_space_superset (as0, as1, &as_common))
3173 gcc_unreachable ();
3175 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3176 op0 = convert (common_type, op0);
3177 op1 = convert (common_type, op1);
3180 /* Determine integer type to perform computations in. This will usually
3181 be the same as the result type (ptrdiff_t), but may need to be a wider
3182 type if pointers for the address space are wider than ptrdiff_t. */
3183 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3184 inttype = lang_hooks.types.type_for_size
3185 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3186 else
3187 inttype = restype;
3190 if (TREE_CODE (target_type) == VOID_TYPE)
3191 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3192 "pointer of type %<void *%> used in subtraction");
3193 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3194 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3195 "pointer to a function used in subtraction");
3197 /* If the conversion to ptrdiff_type does anything like widening or
3198 converting a partial to an integral mode, we get a convert_expression
3199 that is in the way to do any simplifications.
3200 (fold-const.c doesn't know that the extra bits won't be needed.
3201 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3202 different mode in place.)
3203 So first try to find a common term here 'by hand'; we want to cover
3204 at least the cases that occur in legal static initializers. */
3205 if (CONVERT_EXPR_P (op0)
3206 && (TYPE_PRECISION (TREE_TYPE (op0))
3207 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3208 con0 = TREE_OPERAND (op0, 0);
3209 else
3210 con0 = op0;
3211 if (CONVERT_EXPR_P (op1)
3212 && (TYPE_PRECISION (TREE_TYPE (op1))
3213 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3214 con1 = TREE_OPERAND (op1, 0);
3215 else
3216 con1 = op1;
3218 if (TREE_CODE (con0) == PLUS_EXPR)
3220 lit0 = TREE_OPERAND (con0, 1);
3221 con0 = TREE_OPERAND (con0, 0);
3223 else
3224 lit0 = integer_zero_node;
3226 if (TREE_CODE (con1) == PLUS_EXPR)
3228 lit1 = TREE_OPERAND (con1, 1);
3229 con1 = TREE_OPERAND (con1, 0);
3231 else
3232 lit1 = integer_zero_node;
3234 if (operand_equal_p (con0, con1, 0))
3236 op0 = lit0;
3237 op1 = lit1;
3241 /* First do the subtraction as integers;
3242 then drop through to build the divide operator.
3243 Do not do default conversions on the minus operator
3244 in case restype is a short type. */
3246 op0 = build_binary_op (loc,
3247 MINUS_EXPR, convert (inttype, op0),
3248 convert (inttype, op1), 0);
3249 /* This generates an error if op1 is pointer to incomplete type. */
3250 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3251 error_at (loc, "arithmetic on pointer to an incomplete type");
3253 /* This generates an error if op0 is pointer to incomplete type. */
3254 op1 = c_size_in_bytes (target_type);
3256 /* Divide by the size, in easiest possible way. */
3257 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3258 op0, convert (inttype, op1));
3260 /* Convert to final result type if necessary. */
3261 return convert (restype, result);
3264 /* Construct and perhaps optimize a tree representation
3265 for a unary operation. CODE, a tree_code, specifies the operation
3266 and XARG is the operand.
3267 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3268 the default promotions (such as from short to int).
3269 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3270 allows non-lvalues; this is only used to handle conversion of non-lvalue
3271 arrays to pointers in C99.
3273 LOCATION is the location of the operator. */
3275 tree
3276 build_unary_op (location_t location,
3277 enum tree_code code, tree xarg, int flag)
3279 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3280 tree arg = xarg;
3281 tree argtype = 0;
3282 enum tree_code typecode;
3283 tree val;
3284 tree ret = error_mark_node;
3285 tree eptype = NULL_TREE;
3286 int noconvert = flag;
3287 const char *invalid_op_diag;
3288 bool int_operands;
3290 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3291 if (int_operands)
3292 arg = remove_c_maybe_const_expr (arg);
3294 if (code != ADDR_EXPR)
3295 arg = require_complete_type (arg);
3297 typecode = TREE_CODE (TREE_TYPE (arg));
3298 if (typecode == ERROR_MARK)
3299 return error_mark_node;
3300 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3301 typecode = INTEGER_TYPE;
3303 if ((invalid_op_diag
3304 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3306 error_at (location, invalid_op_diag);
3307 return error_mark_node;
3310 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3312 eptype = TREE_TYPE (arg);
3313 arg = TREE_OPERAND (arg, 0);
3316 switch (code)
3318 case CONVERT_EXPR:
3319 /* This is used for unary plus, because a CONVERT_EXPR
3320 is enough to prevent anybody from looking inside for
3321 associativity, but won't generate any code. */
3322 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3323 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3324 || typecode == VECTOR_TYPE))
3326 error_at (location, "wrong type argument to unary plus");
3327 return error_mark_node;
3329 else if (!noconvert)
3330 arg = default_conversion (arg);
3331 arg = non_lvalue_loc (location, arg);
3332 break;
3334 case NEGATE_EXPR:
3335 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3336 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3337 || typecode == VECTOR_TYPE))
3339 error_at (location, "wrong type argument to unary minus");
3340 return error_mark_node;
3342 else if (!noconvert)
3343 arg = default_conversion (arg);
3344 break;
3346 case BIT_NOT_EXPR:
3347 /* ~ works on integer types and non float vectors. */
3348 if (typecode == INTEGER_TYPE
3349 || (typecode == VECTOR_TYPE
3350 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3352 if (!noconvert)
3353 arg = default_conversion (arg);
3355 else if (typecode == COMPLEX_TYPE)
3357 code = CONJ_EXPR;
3358 pedwarn (location, OPT_pedantic,
3359 "ISO C does not support %<~%> for complex conjugation");
3360 if (!noconvert)
3361 arg = default_conversion (arg);
3363 else
3365 error_at (location, "wrong type argument to bit-complement");
3366 return error_mark_node;
3368 break;
3370 case ABS_EXPR:
3371 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3373 error_at (location, "wrong type argument to abs");
3374 return error_mark_node;
3376 else if (!noconvert)
3377 arg = default_conversion (arg);
3378 break;
3380 case CONJ_EXPR:
3381 /* Conjugating a real value is a no-op, but allow it anyway. */
3382 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3383 || typecode == COMPLEX_TYPE))
3385 error_at (location, "wrong type argument to conjugation");
3386 return error_mark_node;
3388 else if (!noconvert)
3389 arg = default_conversion (arg);
3390 break;
3392 case TRUTH_NOT_EXPR:
3393 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3394 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3395 && typecode != COMPLEX_TYPE)
3397 error_at (location,
3398 "wrong type argument to unary exclamation mark");
3399 return error_mark_node;
3401 arg = c_objc_common_truthvalue_conversion (location, arg);
3402 ret = invert_truthvalue_loc (location, arg);
3403 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3404 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3405 location = EXPR_LOCATION (ret);
3406 goto return_build_unary_op;
3408 case REALPART_EXPR:
3409 if (TREE_CODE (arg) == COMPLEX_CST)
3410 ret = TREE_REALPART (arg);
3411 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3412 ret = fold_build1_loc (location,
3413 REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3414 else
3415 ret = arg;
3416 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3417 eptype = TREE_TYPE (eptype);
3418 goto return_build_unary_op;
3420 case IMAGPART_EXPR:
3421 if (TREE_CODE (arg) == COMPLEX_CST)
3422 ret = TREE_IMAGPART (arg);
3423 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3424 ret = fold_build1_loc (location,
3425 IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3426 else
3427 ret = omit_one_operand_loc (location, TREE_TYPE (arg),
3428 integer_zero_node, arg);
3429 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3430 eptype = TREE_TYPE (eptype);
3431 goto return_build_unary_op;
3433 case PREINCREMENT_EXPR:
3434 case POSTINCREMENT_EXPR:
3435 case PREDECREMENT_EXPR:
3436 case POSTDECREMENT_EXPR:
3438 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3440 tree inner = build_unary_op (location, code,
3441 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3442 if (inner == error_mark_node)
3443 return error_mark_node;
3444 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3445 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3446 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3447 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3448 goto return_build_unary_op;
3451 /* Complain about anything that is not a true lvalue. */
3452 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3453 || code == POSTINCREMENT_EXPR)
3454 ? lv_increment
3455 : lv_decrement)))
3456 return error_mark_node;
3458 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3460 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3461 warning_at (location, OPT_Wc___compat,
3462 "increment of enumeration value is invalid in C++");
3463 else
3464 warning_at (location, OPT_Wc___compat,
3465 "decrement of enumeration value is invalid in C++");
3468 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3469 arg = c_fully_fold (arg, false, NULL);
3471 /* Increment or decrement the real part of the value,
3472 and don't change the imaginary part. */
3473 if (typecode == COMPLEX_TYPE)
3475 tree real, imag;
3477 pedwarn (location, OPT_pedantic,
3478 "ISO C does not support %<++%> and %<--%> on complex types");
3480 arg = stabilize_reference (arg);
3481 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3482 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3483 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3484 if (real == error_mark_node || imag == error_mark_node)
3485 return error_mark_node;
3486 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3487 real, imag);
3488 goto return_build_unary_op;
3491 /* Report invalid types. */
3493 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3494 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3496 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3497 error_at (location, "wrong type argument to increment");
3498 else
3499 error_at (location, "wrong type argument to decrement");
3501 return error_mark_node;
3505 tree inc;
3507 argtype = TREE_TYPE (arg);
3509 /* Compute the increment. */
3511 if (typecode == POINTER_TYPE)
3513 /* If pointer target is an undefined struct,
3514 we just cannot know how to do the arithmetic. */
3515 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3517 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3518 error_at (location,
3519 "increment of pointer to unknown structure");
3520 else
3521 error_at (location,
3522 "decrement of pointer to unknown structure");
3524 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3525 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3527 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3528 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3529 "wrong type argument to increment");
3530 else
3531 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3532 "wrong type argument to decrement");
3535 inc = c_size_in_bytes (TREE_TYPE (argtype));
3536 inc = fold_convert_loc (location, sizetype, inc);
3538 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3540 /* For signed fract types, we invert ++ to -- or
3541 -- to ++, and change inc from 1 to -1, because
3542 it is not possible to represent 1 in signed fract constants.
3543 For unsigned fract types, the result always overflows and
3544 we get an undefined (original) or the maximum value. */
3545 if (code == PREINCREMENT_EXPR)
3546 code = PREDECREMENT_EXPR;
3547 else if (code == PREDECREMENT_EXPR)
3548 code = PREINCREMENT_EXPR;
3549 else if (code == POSTINCREMENT_EXPR)
3550 code = POSTDECREMENT_EXPR;
3551 else /* code == POSTDECREMENT_EXPR */
3552 code = POSTINCREMENT_EXPR;
3554 inc = integer_minus_one_node;
3555 inc = convert (argtype, inc);
3557 else
3559 inc = integer_one_node;
3560 inc = convert (argtype, inc);
3563 /* Report a read-only lvalue. */
3564 if (TYPE_READONLY (argtype))
3566 readonly_error (arg,
3567 ((code == PREINCREMENT_EXPR
3568 || code == POSTINCREMENT_EXPR)
3569 ? lv_increment : lv_decrement));
3570 return error_mark_node;
3572 else if (TREE_READONLY (arg))
3573 readonly_warning (arg,
3574 ((code == PREINCREMENT_EXPR
3575 || code == POSTINCREMENT_EXPR)
3576 ? lv_increment : lv_decrement));
3578 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3579 val = boolean_increment (code, arg);
3580 else
3581 val = build2 (code, TREE_TYPE (arg), arg, inc);
3582 TREE_SIDE_EFFECTS (val) = 1;
3583 if (TREE_CODE (val) != code)
3584 TREE_NO_WARNING (val) = 1;
3585 ret = val;
3586 goto return_build_unary_op;
3589 case ADDR_EXPR:
3590 /* Note that this operation never does default_conversion. */
3592 /* The operand of unary '&' must be an lvalue (which excludes
3593 expressions of type void), or, in C99, the result of a [] or
3594 unary '*' operator. */
3595 if (VOID_TYPE_P (TREE_TYPE (arg))
3596 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3597 && (TREE_CODE (arg) != INDIRECT_REF
3598 || !flag_isoc99))
3599 pedwarn (location, 0, "taking address of expression of type %<void%>");
3601 /* Let &* cancel out to simplify resulting code. */
3602 if (TREE_CODE (arg) == INDIRECT_REF)
3604 /* Don't let this be an lvalue. */
3605 if (lvalue_p (TREE_OPERAND (arg, 0)))
3606 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3607 ret = TREE_OPERAND (arg, 0);
3608 goto return_build_unary_op;
3611 /* For &x[y], return x+y */
3612 if (TREE_CODE (arg) == ARRAY_REF)
3614 tree op0 = TREE_OPERAND (arg, 0);
3615 if (!c_mark_addressable (op0))
3616 return error_mark_node;
3617 return build_binary_op (location, PLUS_EXPR,
3618 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3619 ? array_to_pointer_conversion (location,
3620 op0)
3621 : op0),
3622 TREE_OPERAND (arg, 1), 1);
3625 /* Anything not already handled and not a true memory reference
3626 or a non-lvalue array is an error. */
3627 else if (typecode != FUNCTION_TYPE && !flag
3628 && !lvalue_or_else (arg, lv_addressof))
3629 return error_mark_node;
3631 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3632 folding later. */
3633 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3635 tree inner = build_unary_op (location, code,
3636 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3637 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3638 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3639 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3640 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3641 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3642 goto return_build_unary_op;
3645 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3646 argtype = TREE_TYPE (arg);
3648 /* If the lvalue is const or volatile, merge that into the type
3649 to which the address will point. Note that you can't get a
3650 restricted pointer by taking the address of something, so we
3651 only have to deal with `const' and `volatile' here. */
3652 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3653 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3654 argtype = c_build_type_variant (argtype,
3655 TREE_READONLY (arg),
3656 TREE_THIS_VOLATILE (arg));
3658 if (!c_mark_addressable (arg))
3659 return error_mark_node;
3661 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3662 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3664 argtype = build_pointer_type (argtype);
3666 /* ??? Cope with user tricks that amount to offsetof. Delete this
3667 when we have proper support for integer constant expressions. */
3668 val = get_base_address (arg);
3669 if (val && TREE_CODE (val) == INDIRECT_REF
3670 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3672 tree op0 = fold_convert_loc (location, sizetype,
3673 fold_offsetof (arg, val)), op1;
3675 op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3676 ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3677 goto return_build_unary_op;
3680 val = build1 (ADDR_EXPR, argtype, arg);
3682 ret = val;
3683 goto return_build_unary_op;
3685 default:
3686 gcc_unreachable ();
3689 if (argtype == 0)
3690 argtype = TREE_TYPE (arg);
3691 if (TREE_CODE (arg) == INTEGER_CST)
3692 ret = (require_constant_value
3693 ? fold_build1_initializer_loc (location, code, argtype, arg)
3694 : fold_build1_loc (location, code, argtype, arg));
3695 else
3696 ret = build1 (code, argtype, arg);
3697 return_build_unary_op:
3698 gcc_assert (ret != error_mark_node);
3699 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3700 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3701 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3702 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3703 ret = note_integer_operands (ret);
3704 if (eptype)
3705 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3706 protected_set_expr_location (ret, location);
3707 return ret;
3710 /* Return nonzero if REF is an lvalue valid for this language.
3711 Lvalues can be assigned, unless their type has TYPE_READONLY.
3712 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3714 bool
3715 lvalue_p (const_tree ref)
3717 const enum tree_code code = TREE_CODE (ref);
3719 switch (code)
3721 case REALPART_EXPR:
3722 case IMAGPART_EXPR:
3723 case COMPONENT_REF:
3724 return lvalue_p (TREE_OPERAND (ref, 0));
3726 case C_MAYBE_CONST_EXPR:
3727 return lvalue_p (TREE_OPERAND (ref, 1));
3729 case COMPOUND_LITERAL_EXPR:
3730 case STRING_CST:
3731 return 1;
3733 case INDIRECT_REF:
3734 case ARRAY_REF:
3735 case VAR_DECL:
3736 case PARM_DECL:
3737 case RESULT_DECL:
3738 case ERROR_MARK:
3739 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3740 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3742 case BIND_EXPR:
3743 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3745 default:
3746 return 0;
3750 /* Give an error for storing in something that is 'const'. */
3752 static void
3753 readonly_error (tree arg, enum lvalue_use use)
3755 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3756 || use == lv_asm);
3757 /* Using this macro rather than (for example) arrays of messages
3758 ensures that all the format strings are checked at compile
3759 time. */
3760 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3761 : (use == lv_increment ? (I) \
3762 : (use == lv_decrement ? (D) : (AS))))
3763 if (TREE_CODE (arg) == COMPONENT_REF)
3765 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3766 readonly_error (TREE_OPERAND (arg, 0), use);
3767 else
3768 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3769 G_("increment of read-only member %qD"),
3770 G_("decrement of read-only member %qD"),
3771 G_("read-only member %qD used as %<asm%> output")),
3772 TREE_OPERAND (arg, 1));
3774 else if (TREE_CODE (arg) == VAR_DECL)
3775 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3776 G_("increment of read-only variable %qD"),
3777 G_("decrement of read-only variable %qD"),
3778 G_("read-only variable %qD used as %<asm%> output")),
3779 arg);
3780 else
3781 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3782 G_("increment of read-only location %qE"),
3783 G_("decrement of read-only location %qE"),
3784 G_("read-only location %qE used as %<asm%> output")),
3785 arg);
3788 /* Give a warning for storing in something that is read-only in GCC
3789 terms but not const in ISO C terms. */
3791 static void
3792 readonly_warning (tree arg, enum lvalue_use use)
3794 switch (use)
3796 case lv_assign:
3797 warning (0, "assignment of read-only location %qE", arg);
3798 break;
3799 case lv_increment:
3800 warning (0, "increment of read-only location %qE", arg);
3801 break;
3802 case lv_decrement:
3803 warning (0, "decrement of read-only location %qE", arg);
3804 break;
3805 default:
3806 gcc_unreachable ();
3808 return;
3812 /* Return nonzero if REF is an lvalue valid for this language;
3813 otherwise, print an error message and return zero. USE says
3814 how the lvalue is being used and so selects the error message. */
3816 static int
3817 lvalue_or_else (const_tree ref, enum lvalue_use use)
3819 int win = lvalue_p (ref);
3821 if (!win)
3822 lvalue_error (use);
3824 return win;
3827 /* Mark EXP saying that we need to be able to take the
3828 address of it; it should not be allocated in a register.
3829 Returns true if successful. */
3831 bool
3832 c_mark_addressable (tree exp)
3834 tree x = exp;
3836 while (1)
3837 switch (TREE_CODE (x))
3839 case COMPONENT_REF:
3840 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3842 error
3843 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3844 return false;
3847 /* ... fall through ... */
3849 case ADDR_EXPR:
3850 case ARRAY_REF:
3851 case REALPART_EXPR:
3852 case IMAGPART_EXPR:
3853 x = TREE_OPERAND (x, 0);
3854 break;
3856 case COMPOUND_LITERAL_EXPR:
3857 case CONSTRUCTOR:
3858 TREE_ADDRESSABLE (x) = 1;
3859 return true;
3861 case VAR_DECL:
3862 case CONST_DECL:
3863 case PARM_DECL:
3864 case RESULT_DECL:
3865 if (C_DECL_REGISTER (x)
3866 && DECL_NONLOCAL (x))
3868 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3870 error
3871 ("global register variable %qD used in nested function", x);
3872 return false;
3874 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3876 else if (C_DECL_REGISTER (x))
3878 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3879 error ("address of global register variable %qD requested", x);
3880 else
3881 error ("address of register variable %qD requested", x);
3882 return false;
3885 /* drops in */
3886 case FUNCTION_DECL:
3887 TREE_ADDRESSABLE (x) = 1;
3888 /* drops out */
3889 default:
3890 return true;
3894 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
3895 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
3896 if folded to an integer constant then the unselected half may
3897 contain arbitrary operations not normally permitted in constant
3898 expressions. Set the location of the expression to LOC. */
3900 tree
3901 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
3902 tree op1, tree op1_original_type, tree op2,
3903 tree op2_original_type)
3905 tree type1;
3906 tree type2;
3907 enum tree_code code1;
3908 enum tree_code code2;
3909 tree result_type = NULL;
3910 tree ep_result_type = NULL;
3911 tree orig_op1 = op1, orig_op2 = op2;
3912 bool int_const, op1_int_operands, op2_int_operands, int_operands;
3913 bool ifexp_int_operands;
3914 tree ret;
3915 bool objc_ok;
3917 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
3918 if (op1_int_operands)
3919 op1 = remove_c_maybe_const_expr (op1);
3920 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
3921 if (op2_int_operands)
3922 op2 = remove_c_maybe_const_expr (op2);
3923 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
3924 if (ifexp_int_operands)
3925 ifexp = remove_c_maybe_const_expr (ifexp);
3927 /* Promote both alternatives. */
3929 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3930 op1 = default_conversion (op1);
3931 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3932 op2 = default_conversion (op2);
3934 if (TREE_CODE (ifexp) == ERROR_MARK
3935 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3936 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3937 return error_mark_node;
3939 type1 = TREE_TYPE (op1);
3940 code1 = TREE_CODE (type1);
3941 type2 = TREE_TYPE (op2);
3942 code2 = TREE_CODE (type2);
3944 /* C90 does not permit non-lvalue arrays in conditional expressions.
3945 In C99 they will be pointers by now. */
3946 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3948 error_at (colon_loc, "non-lvalue array in conditional expression");
3949 return error_mark_node;
3952 objc_ok = objc_compare_types (type1, type2, -3, NULL_TREE);
3954 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
3955 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3956 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3957 || code1 == COMPLEX_TYPE)
3958 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3959 || code2 == COMPLEX_TYPE))
3961 ep_result_type = c_common_type (type1, type2);
3962 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
3964 op1 = TREE_OPERAND (op1, 0);
3965 type1 = TREE_TYPE (op1);
3966 gcc_assert (TREE_CODE (type1) == code1);
3968 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
3970 op2 = TREE_OPERAND (op2, 0);
3971 type2 = TREE_TYPE (op2);
3972 gcc_assert (TREE_CODE (type2) == code2);
3976 if (warn_cxx_compat)
3978 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
3979 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
3981 if (TREE_CODE (t1) == ENUMERAL_TYPE
3982 && TREE_CODE (t2) == ENUMERAL_TYPE
3983 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
3984 warning_at (colon_loc, OPT_Wc___compat,
3985 ("different enum types in conditional is "
3986 "invalid in C++: %qT vs %qT"),
3987 t1, t2);
3990 /* Quickly detect the usual case where op1 and op2 have the same type
3991 after promotion. */
3992 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3994 if (type1 == type2)
3995 result_type = type1;
3996 else
3997 result_type = TYPE_MAIN_VARIANT (type1);
3999 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4000 || code1 == COMPLEX_TYPE)
4001 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4002 || code2 == COMPLEX_TYPE))
4004 result_type = c_common_type (type1, type2);
4006 /* If -Wsign-compare, warn here if type1 and type2 have
4007 different signedness. We'll promote the signed to unsigned
4008 and later code won't know it used to be different.
4009 Do this check on the original types, so that explicit casts
4010 will be considered, but default promotions won't. */
4011 if (c_inhibit_evaluation_warnings == 0)
4013 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4014 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4016 if (unsigned_op1 ^ unsigned_op2)
4018 bool ovf;
4020 /* Do not warn if the result type is signed, since the
4021 signed type will only be chosen if it can represent
4022 all the values of the unsigned type. */
4023 if (!TYPE_UNSIGNED (result_type))
4024 /* OK */;
4025 else
4027 bool op1_maybe_const = true;
4028 bool op2_maybe_const = true;
4030 /* Do not warn if the signed quantity is an
4031 unsuffixed integer literal (or some static
4032 constant expression involving such literals) and
4033 it is non-negative. This warning requires the
4034 operands to be folded for best results, so do
4035 that folding in this case even without
4036 warn_sign_compare to avoid warning options
4037 possibly affecting code generation. */
4038 c_inhibit_evaluation_warnings
4039 += (ifexp == truthvalue_false_node);
4040 op1 = c_fully_fold (op1, require_constant_value,
4041 &op1_maybe_const);
4042 c_inhibit_evaluation_warnings
4043 -= (ifexp == truthvalue_false_node);
4045 c_inhibit_evaluation_warnings
4046 += (ifexp == truthvalue_true_node);
4047 op2 = c_fully_fold (op2, require_constant_value,
4048 &op2_maybe_const);
4049 c_inhibit_evaluation_warnings
4050 -= (ifexp == truthvalue_true_node);
4052 if (warn_sign_compare)
4054 if ((unsigned_op2
4055 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4056 || (unsigned_op1
4057 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4058 /* OK */;
4059 else
4060 warning_at (colon_loc, OPT_Wsign_compare,
4061 ("signed and unsigned type in "
4062 "conditional expression"));
4064 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4065 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4066 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4067 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4072 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4074 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4075 pedwarn (colon_loc, OPT_pedantic,
4076 "ISO C forbids conditional expr with only one void side");
4077 result_type = void_type_node;
4079 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4081 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4082 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4083 addr_space_t as_common;
4085 if (comp_target_types (colon_loc, type1, type2))
4086 result_type = common_pointer_type (type1, type2);
4087 else if (null_pointer_constant_p (orig_op1))
4088 result_type = type2;
4089 else if (null_pointer_constant_p (orig_op2))
4090 result_type = type1;
4091 else if (!addr_space_superset (as1, as2, &as_common))
4093 error_at (colon_loc, "pointers to disjoint address spaces "
4094 "used in conditional expression");
4095 return error_mark_node;
4097 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4099 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4100 pedwarn (colon_loc, OPT_pedantic,
4101 "ISO C forbids conditional expr between "
4102 "%<void *%> and function pointer");
4103 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4104 TREE_TYPE (type2)));
4106 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4108 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4109 pedwarn (colon_loc, OPT_pedantic,
4110 "ISO C forbids conditional expr between "
4111 "%<void *%> and function pointer");
4112 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4113 TREE_TYPE (type1)));
4115 else
4117 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4119 if (!objc_ok)
4120 pedwarn (colon_loc, 0,
4121 "pointer type mismatch in conditional expression");
4122 result_type = build_pointer_type
4123 (build_qualified_type (void_type_node, qual));
4126 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4128 if (!null_pointer_constant_p (orig_op2))
4129 pedwarn (colon_loc, 0,
4130 "pointer/integer type mismatch in conditional expression");
4131 else
4133 op2 = null_pointer_node;
4135 result_type = type1;
4137 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4139 if (!null_pointer_constant_p (orig_op1))
4140 pedwarn (colon_loc, 0,
4141 "pointer/integer type mismatch in conditional expression");
4142 else
4144 op1 = null_pointer_node;
4146 result_type = type2;
4149 if (!result_type)
4151 if (flag_cond_mismatch)
4152 result_type = void_type_node;
4153 else
4155 error_at (colon_loc, "type mismatch in conditional expression");
4156 return error_mark_node;
4160 /* Merge const and volatile flags of the incoming types. */
4161 result_type
4162 = build_type_variant (result_type,
4163 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4164 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4166 if (result_type != type1)
4167 op1 = convert_and_check (result_type, op1);
4168 if (result_type != type2)
4169 op2 = convert_and_check (result_type, op2);
4171 if (ifexp_bcp && ifexp == truthvalue_true_node)
4173 op2_int_operands = true;
4174 op1 = c_fully_fold (op1, require_constant_value, NULL);
4176 if (ifexp_bcp && ifexp == truthvalue_false_node)
4178 op1_int_operands = true;
4179 op2 = c_fully_fold (op2, require_constant_value, NULL);
4181 int_const = int_operands = (ifexp_int_operands
4182 && op1_int_operands
4183 && op2_int_operands);
4184 if (int_operands)
4186 int_const = ((ifexp == truthvalue_true_node
4187 && TREE_CODE (orig_op1) == INTEGER_CST
4188 && !TREE_OVERFLOW (orig_op1))
4189 || (ifexp == truthvalue_false_node
4190 && TREE_CODE (orig_op2) == INTEGER_CST
4191 && !TREE_OVERFLOW (orig_op2)));
4193 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4194 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4195 else
4197 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4198 if (int_operands)
4199 ret = note_integer_operands (ret);
4201 if (ep_result_type)
4202 ret = build1 (EXCESS_PRECISION_EXPR, ep_result_type, ret);
4204 protected_set_expr_location (ret, colon_loc);
4205 return ret;
4208 /* Return a compound expression that performs two expressions and
4209 returns the value of the second of them.
4211 LOC is the location of the COMPOUND_EXPR. */
4213 tree
4214 build_compound_expr (location_t loc, tree expr1, tree expr2)
4216 bool expr1_int_operands, expr2_int_operands;
4217 tree eptype = NULL_TREE;
4218 tree ret;
4220 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4221 if (expr1_int_operands)
4222 expr1 = remove_c_maybe_const_expr (expr1);
4223 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4224 if (expr2_int_operands)
4225 expr2 = remove_c_maybe_const_expr (expr2);
4227 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4228 expr1 = TREE_OPERAND (expr1, 0);
4229 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4231 eptype = TREE_TYPE (expr2);
4232 expr2 = TREE_OPERAND (expr2, 0);
4235 if (!TREE_SIDE_EFFECTS (expr1))
4237 /* The left-hand operand of a comma expression is like an expression
4238 statement: with -Wunused, we should warn if it doesn't have
4239 any side-effects, unless it was explicitly cast to (void). */
4240 if (warn_unused_value)
4242 if (VOID_TYPE_P (TREE_TYPE (expr1))
4243 && CONVERT_EXPR_P (expr1))
4244 ; /* (void) a, b */
4245 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4246 && TREE_CODE (expr1) == COMPOUND_EXPR
4247 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4248 ; /* (void) a, (void) b, c */
4249 else
4250 warning_at (loc, OPT_Wunused_value,
4251 "left-hand operand of comma expression has no effect");
4255 /* With -Wunused, we should also warn if the left-hand operand does have
4256 side-effects, but computes a value which is not used. For example, in
4257 `foo() + bar(), baz()' the result of the `+' operator is not used,
4258 so we should issue a warning. */
4259 else if (warn_unused_value)
4260 warn_if_unused_value (expr1, loc);
4262 if (expr2 == error_mark_node)
4263 return error_mark_node;
4265 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4267 if (flag_isoc99
4268 && expr1_int_operands
4269 && expr2_int_operands)
4270 ret = note_integer_operands (ret);
4272 if (eptype)
4273 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4275 protected_set_expr_location (ret, loc);
4276 return ret;
4279 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4280 which we are casting. OTYPE is the type of the expression being
4281 cast. Both TYPE and OTYPE are pointer types. -Wcast-qual appeared
4282 on the command line. Named address space qualifiers are not handled
4283 here, because they result in different warnings. */
4285 static void
4286 handle_warn_cast_qual (tree type, tree otype)
4288 tree in_type = type;
4289 tree in_otype = otype;
4290 int added = 0;
4291 int discarded = 0;
4292 bool is_const;
4294 /* Check that the qualifiers on IN_TYPE are a superset of the
4295 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4296 nodes is uninteresting and we stop as soon as we hit a
4297 non-POINTER_TYPE node on either type. */
4300 in_otype = TREE_TYPE (in_otype);
4301 in_type = TREE_TYPE (in_type);
4303 /* GNU C allows cv-qualified function types. 'const' means the
4304 function is very pure, 'volatile' means it can't return. We
4305 need to warn when such qualifiers are added, not when they're
4306 taken away. */
4307 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4308 && TREE_CODE (in_type) == FUNCTION_TYPE)
4309 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4310 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4311 else
4312 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4313 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4315 while (TREE_CODE (in_type) == POINTER_TYPE
4316 && TREE_CODE (in_otype) == POINTER_TYPE);
4318 if (added)
4319 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
4321 if (discarded)
4322 /* There are qualifiers present in IN_OTYPE that are not present
4323 in IN_TYPE. */
4324 warning (OPT_Wcast_qual,
4325 "cast discards qualifiers from pointer target type");
4327 if (added || discarded)
4328 return;
4330 /* A cast from **T to const **T is unsafe, because it can cause a
4331 const value to be changed with no additional warning. We only
4332 issue this warning if T is the same on both sides, and we only
4333 issue the warning if there are the same number of pointers on
4334 both sides, as otherwise the cast is clearly unsafe anyhow. A
4335 cast is unsafe when a qualifier is added at one level and const
4336 is not present at all outer levels.
4338 To issue this warning, we check at each level whether the cast
4339 adds new qualifiers not already seen. We don't need to special
4340 case function types, as they won't have the same
4341 TYPE_MAIN_VARIANT. */
4343 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4344 return;
4345 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4346 return;
4348 in_type = type;
4349 in_otype = otype;
4350 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4353 in_type = TREE_TYPE (in_type);
4354 in_otype = TREE_TYPE (in_otype);
4355 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4356 && !is_const)
4358 warning (OPT_Wcast_qual,
4359 ("new qualifiers in middle of multi-level non-const cast "
4360 "are unsafe"));
4361 break;
4363 if (is_const)
4364 is_const = TYPE_READONLY (in_type);
4366 while (TREE_CODE (in_type) == POINTER_TYPE);
4369 /* Build an expression representing a cast to type TYPE of expression EXPR.
4370 LOC is the location of the cast-- typically the open paren of the cast. */
4372 tree
4373 build_c_cast (location_t loc, tree type, tree expr)
4375 tree value;
4377 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4378 expr = TREE_OPERAND (expr, 0);
4380 value = expr;
4382 if (type == error_mark_node || expr == error_mark_node)
4383 return error_mark_node;
4385 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4386 only in <protocol> qualifications. But when constructing cast expressions,
4387 the protocols do matter and must be kept around. */
4388 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4389 return build1 (NOP_EXPR, type, expr);
4391 type = TYPE_MAIN_VARIANT (type);
4393 if (TREE_CODE (type) == ARRAY_TYPE)
4395 error_at (loc, "cast specifies array type");
4396 return error_mark_node;
4399 if (TREE_CODE (type) == FUNCTION_TYPE)
4401 error_at (loc, "cast specifies function type");
4402 return error_mark_node;
4405 if (!VOID_TYPE_P (type))
4407 value = require_complete_type (value);
4408 if (value == error_mark_node)
4409 return error_mark_node;
4412 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4414 if (TREE_CODE (type) == RECORD_TYPE
4415 || TREE_CODE (type) == UNION_TYPE)
4416 pedwarn (loc, OPT_pedantic,
4417 "ISO C forbids casting nonscalar to the same type");
4419 else if (TREE_CODE (type) == UNION_TYPE)
4421 tree field;
4423 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4424 if (TREE_TYPE (field) != error_mark_node
4425 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4426 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4427 break;
4429 if (field)
4431 tree t;
4432 bool maybe_const = true;
4434 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4435 t = c_fully_fold (value, false, &maybe_const);
4436 t = build_constructor_single (type, field, t);
4437 if (!maybe_const)
4438 t = c_wrap_maybe_const (t, true);
4439 t = digest_init (loc, type, t,
4440 NULL_TREE, false, true, 0);
4441 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4442 return t;
4444 error_at (loc, "cast to union type from type not present in union");
4445 return error_mark_node;
4447 else
4449 tree otype, ovalue;
4451 if (type == void_type_node)
4453 tree t = build1 (CONVERT_EXPR, type, value);
4454 SET_EXPR_LOCATION (t, loc);
4455 return t;
4458 otype = TREE_TYPE (value);
4460 /* Optionally warn about potentially worrisome casts. */
4461 if (warn_cast_qual
4462 && TREE_CODE (type) == POINTER_TYPE
4463 && TREE_CODE (otype) == POINTER_TYPE)
4464 handle_warn_cast_qual (type, otype);
4466 /* Warn about conversions between pointers to disjoint
4467 address spaces. */
4468 if (TREE_CODE (type) == POINTER_TYPE
4469 && TREE_CODE (otype) == POINTER_TYPE
4470 && !null_pointer_constant_p (value))
4472 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4473 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4474 addr_space_t as_common;
4476 if (!addr_space_superset (as_to, as_from, &as_common))
4478 if (ADDR_SPACE_GENERIC_P (as_from))
4479 warning_at (loc, 0, "cast to %s address space pointer "
4480 "from disjoint generic address space pointer",
4481 c_addr_space_name (as_to));
4483 else if (ADDR_SPACE_GENERIC_P (as_to))
4484 warning_at (loc, 0, "cast to generic address space pointer "
4485 "from disjoint %s address space pointer",
4486 c_addr_space_name (as_from));
4488 else
4489 warning_at (loc, 0, "cast to %s address space pointer "
4490 "from disjoint %s address space pointer",
4491 c_addr_space_name (as_to),
4492 c_addr_space_name (as_from));
4496 /* Warn about possible alignment problems. */
4497 if (STRICT_ALIGNMENT
4498 && TREE_CODE (type) == POINTER_TYPE
4499 && TREE_CODE (otype) == POINTER_TYPE
4500 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4501 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4502 /* Don't warn about opaque types, where the actual alignment
4503 restriction is unknown. */
4504 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4505 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4506 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4507 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4508 warning_at (loc, OPT_Wcast_align,
4509 "cast increases required alignment of target type");
4511 if (TREE_CODE (type) == INTEGER_TYPE
4512 && TREE_CODE (otype) == POINTER_TYPE
4513 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4514 /* Unlike conversion of integers to pointers, where the
4515 warning is disabled for converting constants because
4516 of cases such as SIG_*, warn about converting constant
4517 pointers to integers. In some cases it may cause unwanted
4518 sign extension, and a warning is appropriate. */
4519 warning_at (loc, OPT_Wpointer_to_int_cast,
4520 "cast from pointer to integer of different size");
4522 if (TREE_CODE (value) == CALL_EXPR
4523 && TREE_CODE (type) != TREE_CODE (otype))
4524 warning_at (loc, OPT_Wbad_function_cast,
4525 "cast from function call of type %qT "
4526 "to non-matching type %qT", otype, type);
4528 if (TREE_CODE (type) == POINTER_TYPE
4529 && TREE_CODE (otype) == INTEGER_TYPE
4530 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4531 /* Don't warn about converting any constant. */
4532 && !TREE_CONSTANT (value))
4533 warning_at (loc,
4534 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4535 "of different size");
4537 if (warn_strict_aliasing <= 2)
4538 strict_aliasing_warning (otype, type, expr);
4540 /* If pedantic, warn for conversions between function and object
4541 pointer types, except for converting a null pointer constant
4542 to function pointer type. */
4543 if (pedantic
4544 && TREE_CODE (type) == POINTER_TYPE
4545 && TREE_CODE (otype) == POINTER_TYPE
4546 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4547 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4548 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4549 "conversion of function pointer to object pointer type");
4551 if (pedantic
4552 && TREE_CODE (type) == POINTER_TYPE
4553 && TREE_CODE (otype) == POINTER_TYPE
4554 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4555 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4556 && !null_pointer_constant_p (value))
4557 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4558 "conversion of object pointer to function pointer type");
4560 ovalue = value;
4561 value = convert (type, value);
4563 /* Ignore any integer overflow caused by the cast. */
4564 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4566 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4568 if (!TREE_OVERFLOW (value))
4570 /* Avoid clobbering a shared constant. */
4571 value = copy_node (value);
4572 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4575 else if (TREE_OVERFLOW (value))
4576 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4577 value = build_int_cst_wide (TREE_TYPE (value),
4578 TREE_INT_CST_LOW (value),
4579 TREE_INT_CST_HIGH (value));
4583 /* Don't let a cast be an lvalue. */
4584 if (value == expr)
4585 value = non_lvalue_loc (loc, value);
4587 /* Don't allow the results of casting to floating-point or complex
4588 types be confused with actual constants, or casts involving
4589 integer and pointer types other than direct integer-to-integer
4590 and integer-to-pointer be confused with integer constant
4591 expressions and null pointer constants. */
4592 if (TREE_CODE (value) == REAL_CST
4593 || TREE_CODE (value) == COMPLEX_CST
4594 || (TREE_CODE (value) == INTEGER_CST
4595 && !((TREE_CODE (expr) == INTEGER_CST
4596 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4597 || TREE_CODE (expr) == REAL_CST
4598 || TREE_CODE (expr) == COMPLEX_CST)))
4599 value = build1 (NOP_EXPR, type, value);
4601 if (CAN_HAVE_LOCATION_P (value))
4602 SET_EXPR_LOCATION (value, loc);
4603 return value;
4606 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4607 location of the open paren of the cast, or the position of the cast
4608 expr. */
4609 tree
4610 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4612 tree type;
4613 tree type_expr = NULL_TREE;
4614 bool type_expr_const = true;
4615 tree ret;
4616 int saved_wsp = warn_strict_prototypes;
4618 /* This avoids warnings about unprototyped casts on
4619 integers. E.g. "#define SIG_DFL (void(*)())0". */
4620 if (TREE_CODE (expr) == INTEGER_CST)
4621 warn_strict_prototypes = 0;
4622 type = groktypename (type_name, &type_expr, &type_expr_const);
4623 warn_strict_prototypes = saved_wsp;
4625 ret = build_c_cast (loc, type, expr);
4626 if (type_expr)
4628 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4629 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4630 SET_EXPR_LOCATION (ret, loc);
4633 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4634 SET_EXPR_LOCATION (ret, loc);
4636 /* C++ does not permits types to be defined in a cast. */
4637 if (warn_cxx_compat && type_name->specs->tag_defined_p)
4638 warning_at (loc, OPT_Wc___compat,
4639 "defining a type in a cast is invalid in C++");
4641 return ret;
4644 /* Build an assignment expression of lvalue LHS from value RHS.
4645 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4646 may differ from TREE_TYPE (LHS) for an enum bitfield.
4647 MODIFYCODE is the code for a binary operator that we use
4648 to combine the old value of LHS with RHS to get the new value.
4649 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4650 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4651 which may differ from TREE_TYPE (RHS) for an enum value.
4653 LOCATION is the location of the MODIFYCODE operator.
4654 RHS_LOC is the location of the RHS. */
4656 tree
4657 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4658 enum tree_code modifycode,
4659 location_t rhs_loc, tree rhs, tree rhs_origtype)
4661 tree result;
4662 tree newrhs;
4663 tree rhs_semantic_type = NULL_TREE;
4664 tree lhstype = TREE_TYPE (lhs);
4665 tree olhstype = lhstype;
4666 bool npc;
4668 /* Types that aren't fully specified cannot be used in assignments. */
4669 lhs = require_complete_type (lhs);
4671 /* Avoid duplicate error messages from operands that had errors. */
4672 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4673 return error_mark_node;
4675 if (!lvalue_or_else (lhs, lv_assign))
4676 return error_mark_node;
4678 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4680 rhs_semantic_type = TREE_TYPE (rhs);
4681 rhs = TREE_OPERAND (rhs, 0);
4684 newrhs = rhs;
4686 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4688 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4689 lhs_origtype, modifycode, rhs_loc, rhs,
4690 rhs_origtype);
4691 if (inner == error_mark_node)
4692 return error_mark_node;
4693 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4694 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4695 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4696 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4697 protected_set_expr_location (result, location);
4698 return result;
4701 /* If a binary op has been requested, combine the old LHS value with the RHS
4702 producing the value we should actually store into the LHS. */
4704 if (modifycode != NOP_EXPR)
4706 lhs = c_fully_fold (lhs, false, NULL);
4707 lhs = stabilize_reference (lhs);
4708 newrhs = build_binary_op (location,
4709 modifycode, lhs, rhs, 1);
4711 /* The original type of the right hand side is no longer
4712 meaningful. */
4713 rhs_origtype = NULL_TREE;
4716 /* Give an error for storing in something that is 'const'. */
4718 if (TYPE_READONLY (lhstype)
4719 || ((TREE_CODE (lhstype) == RECORD_TYPE
4720 || TREE_CODE (lhstype) == UNION_TYPE)
4721 && C_TYPE_FIELDS_READONLY (lhstype)))
4723 readonly_error (lhs, lv_assign);
4724 return error_mark_node;
4726 else if (TREE_READONLY (lhs))
4727 readonly_warning (lhs, lv_assign);
4729 /* If storing into a structure or union member,
4730 it has probably been given type `int'.
4731 Compute the type that would go with
4732 the actual amount of storage the member occupies. */
4734 if (TREE_CODE (lhs) == COMPONENT_REF
4735 && (TREE_CODE (lhstype) == INTEGER_TYPE
4736 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4737 || TREE_CODE (lhstype) == REAL_TYPE
4738 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4739 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4741 /* If storing in a field that is in actuality a short or narrower than one,
4742 we must store in the field in its actual type. */
4744 if (lhstype != TREE_TYPE (lhs))
4746 lhs = copy_node (lhs);
4747 TREE_TYPE (lhs) = lhstype;
4750 /* Issue -Wc++-compat warnings about an assignment to an enum type
4751 when LHS does not have its original type. This happens for,
4752 e.g., an enum bitfield in a struct. */
4753 if (warn_cxx_compat
4754 && lhs_origtype != NULL_TREE
4755 && lhs_origtype != lhstype
4756 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4758 tree checktype = (rhs_origtype != NULL_TREE
4759 ? rhs_origtype
4760 : TREE_TYPE (rhs));
4761 if (checktype != error_mark_node
4762 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4763 warning_at (location, OPT_Wc___compat,
4764 "enum conversion in assignment is invalid in C++");
4767 /* Convert new value to destination type. Fold it first, then
4768 restore any excess precision information, for the sake of
4769 conversion warnings. */
4771 npc = null_pointer_constant_p (newrhs);
4772 newrhs = c_fully_fold (newrhs, false, NULL);
4773 if (rhs_semantic_type)
4774 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4775 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4776 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4777 if (TREE_CODE (newrhs) == ERROR_MARK)
4778 return error_mark_node;
4780 /* Emit ObjC write barrier, if necessary. */
4781 if (c_dialect_objc () && flag_objc_gc)
4783 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4784 if (result)
4786 protected_set_expr_location (result, location);
4787 return result;
4791 /* Scan operands. */
4793 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4794 TREE_SIDE_EFFECTS (result) = 1;
4795 protected_set_expr_location (result, location);
4797 /* If we got the LHS in a different type for storing in,
4798 convert the result back to the nominal type of LHS
4799 so that the value we return always has the same type
4800 as the LHS argument. */
4802 if (olhstype == TREE_TYPE (result))
4803 return result;
4805 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
4806 ic_assign, false, NULL_TREE, NULL_TREE, 0);
4807 protected_set_expr_location (result, location);
4808 return result;
4811 /* Convert value RHS to type TYPE as preparation for an assignment to
4812 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
4813 original type of RHS; this differs from TREE_TYPE (RHS) for enum
4814 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
4815 constant before any folding.
4816 The real work of conversion is done by `convert'.
4817 The purpose of this function is to generate error messages
4818 for assignments that are not allowed in C.
4819 ERRTYPE says whether it is argument passing, assignment,
4820 initialization or return.
4822 LOCATION is the location of the RHS.
4823 FUNCTION is a tree for the function being called.
4824 PARMNUM is the number of the argument, for printing in error messages. */
4826 static tree
4827 convert_for_assignment (location_t location, tree type, tree rhs,
4828 tree origtype, enum impl_conv errtype,
4829 bool null_pointer_constant, tree fundecl,
4830 tree function, int parmnum)
4832 enum tree_code codel = TREE_CODE (type);
4833 tree orig_rhs = rhs;
4834 tree rhstype;
4835 enum tree_code coder;
4836 tree rname = NULL_TREE;
4837 bool objc_ok = false;
4839 if (errtype == ic_argpass)
4841 tree selector;
4842 /* Change pointer to function to the function itself for
4843 diagnostics. */
4844 if (TREE_CODE (function) == ADDR_EXPR
4845 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4846 function = TREE_OPERAND (function, 0);
4848 /* Handle an ObjC selector specially for diagnostics. */
4849 selector = objc_message_selector ();
4850 rname = function;
4851 if (selector && parmnum > 2)
4853 rname = selector;
4854 parmnum -= 2;
4858 /* This macro is used to emit diagnostics to ensure that all format
4859 strings are complete sentences, visible to gettext and checked at
4860 compile time. */
4861 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4862 do { \
4863 switch (errtype) \
4865 case ic_argpass: \
4866 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4867 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4868 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4869 "expected %qT but argument is of type %qT", \
4870 type, rhstype); \
4871 break; \
4872 case ic_assign: \
4873 pedwarn (LOCATION, OPT, AS); \
4874 break; \
4875 case ic_init: \
4876 pedwarn (LOCATION, OPT, IN); \
4877 break; \
4878 case ic_return: \
4879 pedwarn (LOCATION, OPT, RE); \
4880 break; \
4881 default: \
4882 gcc_unreachable (); \
4884 } while (0)
4886 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4887 rhs = TREE_OPERAND (rhs, 0);
4889 rhstype = TREE_TYPE (rhs);
4890 coder = TREE_CODE (rhstype);
4892 if (coder == ERROR_MARK)
4893 return error_mark_node;
4895 if (c_dialect_objc ())
4897 int parmno;
4899 switch (errtype)
4901 case ic_return:
4902 parmno = 0;
4903 break;
4905 case ic_assign:
4906 parmno = -1;
4907 break;
4909 case ic_init:
4910 parmno = -2;
4911 break;
4913 default:
4914 parmno = parmnum;
4915 break;
4918 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
4921 if (warn_cxx_compat)
4923 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
4924 if (checktype != error_mark_node
4925 && TREE_CODE (type) == ENUMERAL_TYPE
4926 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
4928 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
4929 G_("enum conversion when passing argument "
4930 "%d of %qE is invalid in C++"),
4931 G_("enum conversion in assignment is "
4932 "invalid in C++"),
4933 G_("enum conversion in initialization is "
4934 "invalid in C++"),
4935 G_("enum conversion in return is "
4936 "invalid in C++"));
4940 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
4941 return rhs;
4943 if (coder == VOID_TYPE)
4945 /* Except for passing an argument to an unprototyped function,
4946 this is a constraint violation. When passing an argument to
4947 an unprototyped function, it is compile-time undefined;
4948 making it a constraint in that case was rejected in
4949 DR#252. */
4950 error_at (location, "void value not ignored as it ought to be");
4951 return error_mark_node;
4953 rhs = require_complete_type (rhs);
4954 if (rhs == error_mark_node)
4955 return error_mark_node;
4956 /* A type converts to a reference to it.
4957 This code doesn't fully support references, it's just for the
4958 special case of va_start and va_copy. */
4959 if (codel == REFERENCE_TYPE
4960 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
4962 if (!lvalue_p (rhs))
4964 error_at (location, "cannot pass rvalue to reference parameter");
4965 return error_mark_node;
4967 if (!c_mark_addressable (rhs))
4968 return error_mark_node;
4969 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4970 SET_EXPR_LOCATION (rhs, location);
4972 /* We already know that these two types are compatible, but they
4973 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4974 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4975 likely to be va_list, a typedef to __builtin_va_list, which
4976 is different enough that it will cause problems later. */
4977 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4979 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4980 SET_EXPR_LOCATION (rhs, location);
4983 rhs = build1 (NOP_EXPR, type, rhs);
4984 SET_EXPR_LOCATION (rhs, location);
4985 return rhs;
4987 /* Some types can interconvert without explicit casts. */
4988 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
4989 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
4990 return convert (type, rhs);
4991 /* Arithmetic types all interconvert, and enum is treated like int. */
4992 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
4993 || codel == FIXED_POINT_TYPE
4994 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4995 || codel == BOOLEAN_TYPE)
4996 && (coder == INTEGER_TYPE || coder == REAL_TYPE
4997 || coder == FIXED_POINT_TYPE
4998 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4999 || coder == BOOLEAN_TYPE))
5001 tree ret;
5002 bool save = in_late_binary_op;
5003 if (codel == BOOLEAN_TYPE)
5004 in_late_binary_op = true;
5005 ret = convert_and_check (type, orig_rhs);
5006 if (codel == BOOLEAN_TYPE)
5007 in_late_binary_op = save;
5008 return ret;
5011 /* Aggregates in different TUs might need conversion. */
5012 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5013 && codel == coder
5014 && comptypes (type, rhstype))
5015 return convert_and_check (type, rhs);
5017 /* Conversion to a transparent union or record from its member types.
5018 This applies only to function arguments. */
5019 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5020 && TYPE_TRANSPARENT_AGGR (type))
5021 && errtype == ic_argpass)
5023 tree memb, marginal_memb = NULL_TREE;
5025 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
5027 tree memb_type = TREE_TYPE (memb);
5029 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5030 TYPE_MAIN_VARIANT (rhstype)))
5031 break;
5033 if (TREE_CODE (memb_type) != POINTER_TYPE)
5034 continue;
5036 if (coder == POINTER_TYPE)
5038 tree ttl = TREE_TYPE (memb_type);
5039 tree ttr = TREE_TYPE (rhstype);
5041 /* Any non-function converts to a [const][volatile] void *
5042 and vice versa; otherwise, targets must be the same.
5043 Meanwhile, the lhs target must have all the qualifiers of
5044 the rhs. */
5045 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5046 || comp_target_types (location, memb_type, rhstype))
5048 /* If this type won't generate any warnings, use it. */
5049 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5050 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5051 && TREE_CODE (ttl) == FUNCTION_TYPE)
5052 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5053 == TYPE_QUALS (ttr))
5054 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5055 == TYPE_QUALS (ttl))))
5056 break;
5058 /* Keep looking for a better type, but remember this one. */
5059 if (!marginal_memb)
5060 marginal_memb = memb;
5064 /* Can convert integer zero to any pointer type. */
5065 if (null_pointer_constant)
5067 rhs = null_pointer_node;
5068 break;
5072 if (memb || marginal_memb)
5074 if (!memb)
5076 /* We have only a marginally acceptable member type;
5077 it needs a warning. */
5078 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5079 tree ttr = TREE_TYPE (rhstype);
5081 /* Const and volatile mean something different for function
5082 types, so the usual warnings are not appropriate. */
5083 if (TREE_CODE (ttr) == FUNCTION_TYPE
5084 && TREE_CODE (ttl) == FUNCTION_TYPE)
5086 /* Because const and volatile on functions are
5087 restrictions that say the function will not do
5088 certain things, it is okay to use a const or volatile
5089 function where an ordinary one is wanted, but not
5090 vice-versa. */
5091 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5092 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5093 WARN_FOR_ASSIGNMENT (location, 0,
5094 G_("passing argument %d of %qE "
5095 "makes qualified function "
5096 "pointer from unqualified"),
5097 G_("assignment makes qualified "
5098 "function pointer from "
5099 "unqualified"),
5100 G_("initialization makes qualified "
5101 "function pointer from "
5102 "unqualified"),
5103 G_("return makes qualified function "
5104 "pointer from unqualified"));
5106 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5107 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5108 WARN_FOR_ASSIGNMENT (location, 0,
5109 G_("passing argument %d of %qE discards "
5110 "qualifiers from pointer target type"),
5111 G_("assignment discards qualifiers "
5112 "from pointer target type"),
5113 G_("initialization discards qualifiers "
5114 "from pointer target type"),
5115 G_("return discards qualifiers from "
5116 "pointer target type"));
5118 memb = marginal_memb;
5121 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5122 pedwarn (location, OPT_pedantic,
5123 "ISO C prohibits argument conversion to union type");
5125 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5126 return build_constructor_single (type, memb, rhs);
5130 /* Conversions among pointers */
5131 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5132 && (coder == codel))
5134 tree ttl = TREE_TYPE (type);
5135 tree ttr = TREE_TYPE (rhstype);
5136 tree mvl = ttl;
5137 tree mvr = ttr;
5138 bool is_opaque_pointer;
5139 int target_cmp = 0; /* Cache comp_target_types () result. */
5140 addr_space_t asl;
5141 addr_space_t asr;
5143 if (TREE_CODE (mvl) != ARRAY_TYPE)
5144 mvl = TYPE_MAIN_VARIANT (mvl);
5145 if (TREE_CODE (mvr) != ARRAY_TYPE)
5146 mvr = TYPE_MAIN_VARIANT (mvr);
5147 /* Opaque pointers are treated like void pointers. */
5148 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5150 /* C++ does not allow the implicit conversion void* -> T*. However,
5151 for the purpose of reducing the number of false positives, we
5152 tolerate the special case of
5154 int *p = NULL;
5156 where NULL is typically defined in C to be '(void *) 0'. */
5157 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5158 warning_at (location, OPT_Wc___compat,
5159 "request for implicit conversion "
5160 "from %qT to %qT not permitted in C++", rhstype, type);
5162 /* See if the pointers point to incompatible address spaces. */
5163 asl = TYPE_ADDR_SPACE (ttl);
5164 asr = TYPE_ADDR_SPACE (ttr);
5165 if (!null_pointer_constant_p (rhs)
5166 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5168 switch (errtype)
5170 case ic_argpass:
5171 error_at (location, "passing argument %d of %qE from pointer to "
5172 "non-enclosed address space", parmnum, rname);
5173 break;
5174 case ic_assign:
5175 error_at (location, "assignment from pointer to "
5176 "non-enclosed address space");
5177 break;
5178 case ic_init:
5179 error_at (location, "initialization from pointer to "
5180 "non-enclosed address space");
5181 break;
5182 case ic_return:
5183 error_at (location, "return from pointer to "
5184 "non-enclosed address space");
5185 break;
5186 default:
5187 gcc_unreachable ();
5189 return error_mark_node;
5192 /* Check if the right-hand side has a format attribute but the
5193 left-hand side doesn't. */
5194 if (warn_missing_format_attribute
5195 && check_missing_format_attribute (type, rhstype))
5197 switch (errtype)
5199 case ic_argpass:
5200 warning_at (location, OPT_Wmissing_format_attribute,
5201 "argument %d of %qE might be "
5202 "a candidate for a format attribute",
5203 parmnum, rname);
5204 break;
5205 case ic_assign:
5206 warning_at (location, OPT_Wmissing_format_attribute,
5207 "assignment left-hand side might be "
5208 "a candidate for a format attribute");
5209 break;
5210 case ic_init:
5211 warning_at (location, OPT_Wmissing_format_attribute,
5212 "initialization left-hand side might be "
5213 "a candidate for a format attribute");
5214 break;
5215 case ic_return:
5216 warning_at (location, OPT_Wmissing_format_attribute,
5217 "return type might be "
5218 "a candidate for a format attribute");
5219 break;
5220 default:
5221 gcc_unreachable ();
5225 /* Any non-function converts to a [const][volatile] void *
5226 and vice versa; otherwise, targets must be the same.
5227 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5228 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5229 || (target_cmp = comp_target_types (location, type, rhstype))
5230 || is_opaque_pointer
5231 || (c_common_unsigned_type (mvl)
5232 == c_common_unsigned_type (mvr)))
5234 if (pedantic
5235 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5237 (VOID_TYPE_P (ttr)
5238 && !null_pointer_constant
5239 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5240 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5241 G_("ISO C forbids passing argument %d of "
5242 "%qE between function pointer "
5243 "and %<void *%>"),
5244 G_("ISO C forbids assignment between "
5245 "function pointer and %<void *%>"),
5246 G_("ISO C forbids initialization between "
5247 "function pointer and %<void *%>"),
5248 G_("ISO C forbids return between function "
5249 "pointer and %<void *%>"));
5250 /* Const and volatile mean something different for function types,
5251 so the usual warnings are not appropriate. */
5252 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5253 && TREE_CODE (ttl) != FUNCTION_TYPE)
5255 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5256 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5258 /* Types differing only by the presence of the 'volatile'
5259 qualifier are acceptable if the 'volatile' has been added
5260 in by the Objective-C EH machinery. */
5261 if (!objc_type_quals_match (ttl, ttr))
5262 WARN_FOR_ASSIGNMENT (location, 0,
5263 G_("passing argument %d of %qE discards "
5264 "qualifiers from pointer target type"),
5265 G_("assignment discards qualifiers "
5266 "from pointer target type"),
5267 G_("initialization discards qualifiers "
5268 "from pointer target type"),
5269 G_("return discards qualifiers from "
5270 "pointer target type"));
5272 /* If this is not a case of ignoring a mismatch in signedness,
5273 no warning. */
5274 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5275 || target_cmp)
5277 /* If there is a mismatch, do warn. */
5278 else if (warn_pointer_sign)
5279 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5280 G_("pointer targets in passing argument "
5281 "%d of %qE differ in signedness"),
5282 G_("pointer targets in assignment "
5283 "differ in signedness"),
5284 G_("pointer targets in initialization "
5285 "differ in signedness"),
5286 G_("pointer targets in return differ "
5287 "in signedness"));
5289 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5290 && TREE_CODE (ttr) == FUNCTION_TYPE)
5292 /* Because const and volatile on functions are restrictions
5293 that say the function will not do certain things,
5294 it is okay to use a const or volatile function
5295 where an ordinary one is wanted, but not vice-versa. */
5296 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5297 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5298 WARN_FOR_ASSIGNMENT (location, 0,
5299 G_("passing argument %d of %qE makes "
5300 "qualified function pointer "
5301 "from unqualified"),
5302 G_("assignment makes qualified function "
5303 "pointer from unqualified"),
5304 G_("initialization makes qualified "
5305 "function pointer from unqualified"),
5306 G_("return makes qualified function "
5307 "pointer from unqualified"));
5310 else
5311 /* Avoid warning about the volatile ObjC EH puts on decls. */
5312 if (!objc_ok)
5313 WARN_FOR_ASSIGNMENT (location, 0,
5314 G_("passing argument %d of %qE from "
5315 "incompatible pointer type"),
5316 G_("assignment from incompatible pointer type"),
5317 G_("initialization from incompatible "
5318 "pointer type"),
5319 G_("return from incompatible pointer type"));
5321 return convert (type, rhs);
5323 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5325 /* ??? This should not be an error when inlining calls to
5326 unprototyped functions. */
5327 error_at (location, "invalid use of non-lvalue array");
5328 return error_mark_node;
5330 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5332 /* An explicit constant 0 can convert to a pointer,
5333 or one that results from arithmetic, even including
5334 a cast to integer type. */
5335 if (!null_pointer_constant)
5336 WARN_FOR_ASSIGNMENT (location, 0,
5337 G_("passing argument %d of %qE makes "
5338 "pointer from integer without a cast"),
5339 G_("assignment makes pointer from integer "
5340 "without a cast"),
5341 G_("initialization makes pointer from "
5342 "integer without a cast"),
5343 G_("return makes pointer from integer "
5344 "without a cast"));
5346 return convert (type, rhs);
5348 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5350 WARN_FOR_ASSIGNMENT (location, 0,
5351 G_("passing argument %d of %qE makes integer "
5352 "from pointer without a cast"),
5353 G_("assignment makes integer from pointer "
5354 "without a cast"),
5355 G_("initialization makes integer from pointer "
5356 "without a cast"),
5357 G_("return makes integer from pointer "
5358 "without a cast"));
5359 return convert (type, rhs);
5361 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5363 tree ret;
5364 bool save = in_late_binary_op;
5365 in_late_binary_op = true;
5366 ret = convert (type, rhs);
5367 in_late_binary_op = save;
5368 return ret;
5371 switch (errtype)
5373 case ic_argpass:
5374 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5375 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5376 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5377 "expected %qT but argument is of type %qT", type, rhstype);
5378 break;
5379 case ic_assign:
5380 error_at (location, "incompatible types when assigning to type %qT from "
5381 "type %qT", type, rhstype);
5382 break;
5383 case ic_init:
5384 error_at (location,
5385 "incompatible types when initializing type %qT using type %qT",
5386 type, rhstype);
5387 break;
5388 case ic_return:
5389 error_at (location,
5390 "incompatible types when returning type %qT but %qT was "
5391 "expected", rhstype, type);
5392 break;
5393 default:
5394 gcc_unreachable ();
5397 return error_mark_node;
5400 /* If VALUE is a compound expr all of whose expressions are constant, then
5401 return its value. Otherwise, return error_mark_node.
5403 This is for handling COMPOUND_EXPRs as initializer elements
5404 which is allowed with a warning when -pedantic is specified. */
5406 static tree
5407 valid_compound_expr_initializer (tree value, tree endtype)
5409 if (TREE_CODE (value) == COMPOUND_EXPR)
5411 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5412 == error_mark_node)
5413 return error_mark_node;
5414 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5415 endtype);
5417 else if (!initializer_constant_valid_p (value, endtype))
5418 return error_mark_node;
5419 else
5420 return value;
5423 /* Perform appropriate conversions on the initial value of a variable,
5424 store it in the declaration DECL,
5425 and print any error messages that are appropriate.
5426 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5427 If the init is invalid, store an ERROR_MARK.
5429 INIT_LOC is the location of the initial value. */
5431 void
5432 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5434 tree value, type;
5435 bool npc = false;
5437 /* If variable's type was invalidly declared, just ignore it. */
5439 type = TREE_TYPE (decl);
5440 if (TREE_CODE (type) == ERROR_MARK)
5441 return;
5443 /* Digest the specified initializer into an expression. */
5445 if (init)
5446 npc = null_pointer_constant_p (init);
5447 value = digest_init (init_loc, type, init, origtype, npc,
5448 true, TREE_STATIC (decl));
5450 /* Store the expression if valid; else report error. */
5452 if (!in_system_header
5453 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5454 warning (OPT_Wtraditional, "traditional C rejects automatic "
5455 "aggregate initialization");
5457 DECL_INITIAL (decl) = value;
5459 /* ANSI wants warnings about out-of-range constant initializers. */
5460 STRIP_TYPE_NOPS (value);
5461 if (TREE_STATIC (decl))
5462 constant_expression_warning (value);
5464 /* Check if we need to set array size from compound literal size. */
5465 if (TREE_CODE (type) == ARRAY_TYPE
5466 && TYPE_DOMAIN (type) == 0
5467 && value != error_mark_node)
5469 tree inside_init = init;
5471 STRIP_TYPE_NOPS (inside_init);
5472 inside_init = fold (inside_init);
5474 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5476 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5478 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5480 /* For int foo[] = (int [3]){1}; we need to set array size
5481 now since later on array initializer will be just the
5482 brace enclosed list of the compound literal. */
5483 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5484 TREE_TYPE (decl) = type;
5485 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5486 layout_type (type);
5487 layout_decl (cldecl, 0);
5493 /* Methods for storing and printing names for error messages. */
5495 /* Implement a spelling stack that allows components of a name to be pushed
5496 and popped. Each element on the stack is this structure. */
5498 struct spelling
5500 int kind;
5501 union
5503 unsigned HOST_WIDE_INT i;
5504 const char *s;
5505 } u;
5508 #define SPELLING_STRING 1
5509 #define SPELLING_MEMBER 2
5510 #define SPELLING_BOUNDS 3
5512 static struct spelling *spelling; /* Next stack element (unused). */
5513 static struct spelling *spelling_base; /* Spelling stack base. */
5514 static int spelling_size; /* Size of the spelling stack. */
5516 /* Macros to save and restore the spelling stack around push_... functions.
5517 Alternative to SAVE_SPELLING_STACK. */
5519 #define SPELLING_DEPTH() (spelling - spelling_base)
5520 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5522 /* Push an element on the spelling stack with type KIND and assign VALUE
5523 to MEMBER. */
5525 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5527 int depth = SPELLING_DEPTH (); \
5529 if (depth >= spelling_size) \
5531 spelling_size += 10; \
5532 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5533 spelling_size); \
5534 RESTORE_SPELLING_DEPTH (depth); \
5537 spelling->kind = (KIND); \
5538 spelling->MEMBER = (VALUE); \
5539 spelling++; \
5542 /* Push STRING on the stack. Printed literally. */
5544 static void
5545 push_string (const char *string)
5547 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5550 /* Push a member name on the stack. Printed as '.' STRING. */
5552 static void
5553 push_member_name (tree decl)
5555 const char *const string
5556 = (DECL_NAME (decl)
5557 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5558 : _("<anonymous>"));
5559 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5562 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5564 static void
5565 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5567 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5570 /* Compute the maximum size in bytes of the printed spelling. */
5572 static int
5573 spelling_length (void)
5575 int size = 0;
5576 struct spelling *p;
5578 for (p = spelling_base; p < spelling; p++)
5580 if (p->kind == SPELLING_BOUNDS)
5581 size += 25;
5582 else
5583 size += strlen (p->u.s) + 1;
5586 return size;
5589 /* Print the spelling to BUFFER and return it. */
5591 static char *
5592 print_spelling (char *buffer)
5594 char *d = buffer;
5595 struct spelling *p;
5597 for (p = spelling_base; p < spelling; p++)
5598 if (p->kind == SPELLING_BOUNDS)
5600 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5601 d += strlen (d);
5603 else
5605 const char *s;
5606 if (p->kind == SPELLING_MEMBER)
5607 *d++ = '.';
5608 for (s = p->u.s; (*d = *s++); d++)
5611 *d++ = '\0';
5612 return buffer;
5615 /* Issue an error message for a bad initializer component.
5616 MSGID identifies the message.
5617 The component name is taken from the spelling stack. */
5619 void
5620 error_init (const char *msgid)
5622 char *ofwhat;
5624 error ("%s", _(msgid));
5625 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5626 if (*ofwhat)
5627 error ("(near initialization for %qs)", ofwhat);
5630 /* Issue a pedantic warning for a bad initializer component. OPT is
5631 the option OPT_* (from options.h) controlling this warning or 0 if
5632 it is unconditionally given. MSGID identifies the message. The
5633 component name is taken from the spelling stack. */
5635 void
5636 pedwarn_init (location_t location, int opt, const char *msgid)
5638 char *ofwhat;
5640 pedwarn (location, opt, "%s", _(msgid));
5641 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5642 if (*ofwhat)
5643 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5646 /* Issue a warning for a bad initializer component.
5648 OPT is the OPT_W* value corresponding to the warning option that
5649 controls this warning. MSGID identifies the message. The
5650 component name is taken from the spelling stack. */
5652 static void
5653 warning_init (int opt, const char *msgid)
5655 char *ofwhat;
5657 warning (opt, "%s", _(msgid));
5658 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5659 if (*ofwhat)
5660 warning (opt, "(near initialization for %qs)", ofwhat);
5663 /* If TYPE is an array type and EXPR is a parenthesized string
5664 constant, warn if pedantic that EXPR is being used to initialize an
5665 object of type TYPE. */
5667 void
5668 maybe_warn_string_init (tree type, struct c_expr expr)
5670 if (pedantic
5671 && TREE_CODE (type) == ARRAY_TYPE
5672 && TREE_CODE (expr.value) == STRING_CST
5673 && expr.original_code != STRING_CST)
5674 pedwarn_init (input_location, OPT_pedantic,
5675 "array initialized from parenthesized string constant");
5678 /* Digest the parser output INIT as an initializer for type TYPE.
5679 Return a C expression of type TYPE to represent the initial value.
5681 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5683 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5685 If INIT is a string constant, STRICT_STRING is true if it is
5686 unparenthesized or we should not warn here for it being parenthesized.
5687 For other types of INIT, STRICT_STRING is not used.
5689 INIT_LOC is the location of the INIT.
5691 REQUIRE_CONSTANT requests an error if non-constant initializers or
5692 elements are seen. */
5694 static tree
5695 digest_init (location_t init_loc, tree type, tree init, tree origtype,
5696 bool null_pointer_constant, bool strict_string,
5697 int require_constant)
5699 enum tree_code code = TREE_CODE (type);
5700 tree inside_init = init;
5701 tree semantic_type = NULL_TREE;
5702 bool maybe_const = true;
5704 if (type == error_mark_node
5705 || !init
5706 || init == error_mark_node
5707 || TREE_TYPE (init) == error_mark_node)
5708 return error_mark_node;
5710 STRIP_TYPE_NOPS (inside_init);
5712 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5714 semantic_type = TREE_TYPE (inside_init);
5715 inside_init = TREE_OPERAND (inside_init, 0);
5717 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
5718 inside_init = decl_constant_value_for_optimization (inside_init);
5720 /* Initialization of an array of chars from a string constant
5721 optionally enclosed in braces. */
5723 if (code == ARRAY_TYPE && inside_init
5724 && TREE_CODE (inside_init) == STRING_CST)
5726 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5727 /* Note that an array could be both an array of character type
5728 and an array of wchar_t if wchar_t is signed char or unsigned
5729 char. */
5730 bool char_array = (typ1 == char_type_node
5731 || typ1 == signed_char_type_node
5732 || typ1 == unsigned_char_type_node);
5733 bool wchar_array = !!comptypes (typ1, wchar_type_node);
5734 bool char16_array = !!comptypes (typ1, char16_type_node);
5735 bool char32_array = !!comptypes (typ1, char32_type_node);
5737 if (char_array || wchar_array || char16_array || char32_array)
5739 struct c_expr expr;
5740 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
5741 expr.value = inside_init;
5742 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
5743 expr.original_type = NULL;
5744 maybe_warn_string_init (type, expr);
5746 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5747 pedwarn_init (init_loc, OPT_pedantic,
5748 "initialization of a flexible array member");
5750 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5751 TYPE_MAIN_VARIANT (type)))
5752 return inside_init;
5754 if (char_array)
5756 if (typ2 != char_type_node)
5758 error_init ("char-array initialized from wide string");
5759 return error_mark_node;
5762 else
5764 if (typ2 == char_type_node)
5766 error_init ("wide character array initialized from non-wide "
5767 "string");
5768 return error_mark_node;
5770 else if (!comptypes(typ1, typ2))
5772 error_init ("wide character array initialized from "
5773 "incompatible wide string");
5774 return error_mark_node;
5778 TREE_TYPE (inside_init) = type;
5779 if (TYPE_DOMAIN (type) != 0
5780 && TYPE_SIZE (type) != 0
5781 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
5783 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
5785 /* Subtract the size of a single (possibly wide) character
5786 because it's ok to ignore the terminating null char
5787 that is counted in the length of the constant. */
5788 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
5789 (len
5790 - (TYPE_PRECISION (typ1)
5791 / BITS_PER_UNIT))))
5792 pedwarn_init (init_loc, 0,
5793 ("initializer-string for array of chars "
5794 "is too long"));
5795 else if (warn_cxx_compat
5796 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
5797 warning_at (init_loc, OPT_Wc___compat,
5798 ("initializer-string for array chars "
5799 "is too long for C++"));
5802 return inside_init;
5804 else if (INTEGRAL_TYPE_P (typ1))
5806 error_init ("array of inappropriate type initialized "
5807 "from string constant");
5808 return error_mark_node;
5812 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5813 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5814 below and handle as a constructor. */
5815 if (code == VECTOR_TYPE
5816 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
5817 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
5818 && TREE_CONSTANT (inside_init))
5820 if (TREE_CODE (inside_init) == VECTOR_CST
5821 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5822 TYPE_MAIN_VARIANT (type)))
5823 return inside_init;
5825 if (TREE_CODE (inside_init) == CONSTRUCTOR)
5827 unsigned HOST_WIDE_INT ix;
5828 tree value;
5829 bool constant_p = true;
5831 /* Iterate through elements and check if all constructor
5832 elements are *_CSTs. */
5833 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
5834 if (!CONSTANT_CLASS_P (value))
5836 constant_p = false;
5837 break;
5840 if (constant_p)
5841 return build_vector_from_ctor (type,
5842 CONSTRUCTOR_ELTS (inside_init));
5846 if (warn_sequence_point)
5847 verify_sequence_points (inside_init);
5849 /* Any type can be initialized
5850 from an expression of the same type, optionally with braces. */
5852 if (inside_init && TREE_TYPE (inside_init) != 0
5853 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5854 TYPE_MAIN_VARIANT (type))
5855 || (code == ARRAY_TYPE
5856 && comptypes (TREE_TYPE (inside_init), type))
5857 || (code == VECTOR_TYPE
5858 && comptypes (TREE_TYPE (inside_init), type))
5859 || (code == POINTER_TYPE
5860 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
5861 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
5862 TREE_TYPE (type)))))
5864 if (code == POINTER_TYPE)
5866 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
5868 if (TREE_CODE (inside_init) == STRING_CST
5869 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5870 inside_init = array_to_pointer_conversion
5871 (init_loc, inside_init);
5872 else
5874 error_init ("invalid use of non-lvalue array");
5875 return error_mark_node;
5880 if (code == VECTOR_TYPE)
5881 /* Although the types are compatible, we may require a
5882 conversion. */
5883 inside_init = convert (type, inside_init);
5885 if (require_constant
5886 && (code == VECTOR_TYPE || !flag_isoc99)
5887 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5889 /* As an extension, allow initializing objects with static storage
5890 duration with compound literals (which are then treated just as
5891 the brace enclosed list they contain). Also allow this for
5892 vectors, as we can only assign them with compound literals. */
5893 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5894 inside_init = DECL_INITIAL (decl);
5897 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
5898 && TREE_CODE (inside_init) != CONSTRUCTOR)
5900 error_init ("array initialized from non-constant array expression");
5901 return error_mark_node;
5904 /* Compound expressions can only occur here if -pedantic or
5905 -pedantic-errors is specified. In the later case, we always want
5906 an error. In the former case, we simply want a warning. */
5907 if (require_constant && pedantic
5908 && TREE_CODE (inside_init) == COMPOUND_EXPR)
5910 inside_init
5911 = valid_compound_expr_initializer (inside_init,
5912 TREE_TYPE (inside_init));
5913 if (inside_init == error_mark_node)
5914 error_init ("initializer element is not constant");
5915 else
5916 pedwarn_init (init_loc, OPT_pedantic,
5917 "initializer element is not constant");
5918 if (flag_pedantic_errors)
5919 inside_init = error_mark_node;
5921 else if (require_constant
5922 && !initializer_constant_valid_p (inside_init,
5923 TREE_TYPE (inside_init)))
5925 error_init ("initializer element is not constant");
5926 inside_init = error_mark_node;
5928 else if (require_constant && !maybe_const)
5929 pedwarn_init (init_loc, 0,
5930 "initializer element is not a constant expression");
5932 /* Added to enable additional -Wmissing-format-attribute warnings. */
5933 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
5934 inside_init = convert_for_assignment (init_loc, type, inside_init,
5935 origtype,
5936 ic_init, null_pointer_constant,
5937 NULL_TREE, NULL_TREE, 0);
5938 return inside_init;
5941 /* Handle scalar types, including conversions. */
5943 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
5944 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
5945 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
5947 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
5948 && (TREE_CODE (init) == STRING_CST
5949 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
5950 inside_init = init = array_to_pointer_conversion (init_loc, init);
5951 if (semantic_type)
5952 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
5953 inside_init);
5954 inside_init
5955 = convert_for_assignment (init_loc, type, inside_init, origtype,
5956 ic_init, null_pointer_constant,
5957 NULL_TREE, NULL_TREE, 0);
5959 /* Check to see if we have already given an error message. */
5960 if (inside_init == error_mark_node)
5962 else if (require_constant && !TREE_CONSTANT (inside_init))
5964 error_init ("initializer element is not constant");
5965 inside_init = error_mark_node;
5967 else if (require_constant
5968 && !initializer_constant_valid_p (inside_init,
5969 TREE_TYPE (inside_init)))
5971 error_init ("initializer element is not computable at load time");
5972 inside_init = error_mark_node;
5974 else if (require_constant && !maybe_const)
5975 pedwarn_init (init_loc, 0,
5976 "initializer element is not a constant expression");
5978 return inside_init;
5981 /* Come here only for records and arrays. */
5983 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
5985 error_init ("variable-sized object may not be initialized");
5986 return error_mark_node;
5989 error_init ("invalid initializer");
5990 return error_mark_node;
5993 /* Handle initializers that use braces. */
5995 /* Type of object we are accumulating a constructor for.
5996 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
5997 static tree constructor_type;
5999 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6000 left to fill. */
6001 static tree constructor_fields;
6003 /* For an ARRAY_TYPE, this is the specified index
6004 at which to store the next element we get. */
6005 static tree constructor_index;
6007 /* For an ARRAY_TYPE, this is the maximum index. */
6008 static tree constructor_max_index;
6010 /* For a RECORD_TYPE, this is the first field not yet written out. */
6011 static tree constructor_unfilled_fields;
6013 /* For an ARRAY_TYPE, this is the index of the first element
6014 not yet written out. */
6015 static tree constructor_unfilled_index;
6017 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6018 This is so we can generate gaps between fields, when appropriate. */
6019 static tree constructor_bit_index;
6021 /* If we are saving up the elements rather than allocating them,
6022 this is the list of elements so far (in reverse order,
6023 most recent first). */
6024 static VEC(constructor_elt,gc) *constructor_elements;
6026 /* 1 if constructor should be incrementally stored into a constructor chain,
6027 0 if all the elements should be kept in AVL tree. */
6028 static int constructor_incremental;
6030 /* 1 if so far this constructor's elements are all compile-time constants. */
6031 static int constructor_constant;
6033 /* 1 if so far this constructor's elements are all valid address constants. */
6034 static int constructor_simple;
6036 /* 1 if this constructor has an element that cannot be part of a
6037 constant expression. */
6038 static int constructor_nonconst;
6040 /* 1 if this constructor is erroneous so far. */
6041 static int constructor_erroneous;
6043 /* Structure for managing pending initializer elements, organized as an
6044 AVL tree. */
6046 struct init_node
6048 struct init_node *left, *right;
6049 struct init_node *parent;
6050 int balance;
6051 tree purpose;
6052 tree value;
6053 tree origtype;
6056 /* Tree of pending elements at this constructor level.
6057 These are elements encountered out of order
6058 which belong at places we haven't reached yet in actually
6059 writing the output.
6060 Will never hold tree nodes across GC runs. */
6061 static struct init_node *constructor_pending_elts;
6063 /* The SPELLING_DEPTH of this constructor. */
6064 static int constructor_depth;
6066 /* DECL node for which an initializer is being read.
6067 0 means we are reading a constructor expression
6068 such as (struct foo) {...}. */
6069 static tree constructor_decl;
6071 /* Nonzero if this is an initializer for a top-level decl. */
6072 static int constructor_top_level;
6074 /* Nonzero if there were any member designators in this initializer. */
6075 static int constructor_designated;
6077 /* Nesting depth of designator list. */
6078 static int designator_depth;
6080 /* Nonzero if there were diagnosed errors in this designator list. */
6081 static int designator_erroneous;
6084 /* This stack has a level for each implicit or explicit level of
6085 structuring in the initializer, including the outermost one. It
6086 saves the values of most of the variables above. */
6088 struct constructor_range_stack;
6090 struct constructor_stack
6092 struct constructor_stack *next;
6093 tree type;
6094 tree fields;
6095 tree index;
6096 tree max_index;
6097 tree unfilled_index;
6098 tree unfilled_fields;
6099 tree bit_index;
6100 VEC(constructor_elt,gc) *elements;
6101 struct init_node *pending_elts;
6102 int offset;
6103 int depth;
6104 /* If value nonzero, this value should replace the entire
6105 constructor at this level. */
6106 struct c_expr replacement_value;
6107 struct constructor_range_stack *range_stack;
6108 char constant;
6109 char simple;
6110 char nonconst;
6111 char implicit;
6112 char erroneous;
6113 char outer;
6114 char incremental;
6115 char designated;
6118 static struct constructor_stack *constructor_stack;
6120 /* This stack represents designators from some range designator up to
6121 the last designator in the list. */
6123 struct constructor_range_stack
6125 struct constructor_range_stack *next, *prev;
6126 struct constructor_stack *stack;
6127 tree range_start;
6128 tree index;
6129 tree range_end;
6130 tree fields;
6133 static struct constructor_range_stack *constructor_range_stack;
6135 /* This stack records separate initializers that are nested.
6136 Nested initializers can't happen in ANSI C, but GNU C allows them
6137 in cases like { ... (struct foo) { ... } ... }. */
6139 struct initializer_stack
6141 struct initializer_stack *next;
6142 tree decl;
6143 struct constructor_stack *constructor_stack;
6144 struct constructor_range_stack *constructor_range_stack;
6145 VEC(constructor_elt,gc) *elements;
6146 struct spelling *spelling;
6147 struct spelling *spelling_base;
6148 int spelling_size;
6149 char top_level;
6150 char require_constant_value;
6151 char require_constant_elements;
6154 static struct initializer_stack *initializer_stack;
6156 /* Prepare to parse and output the initializer for variable DECL. */
6158 void
6159 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6161 const char *locus;
6162 struct initializer_stack *p = XNEW (struct initializer_stack);
6164 p->decl = constructor_decl;
6165 p->require_constant_value = require_constant_value;
6166 p->require_constant_elements = require_constant_elements;
6167 p->constructor_stack = constructor_stack;
6168 p->constructor_range_stack = constructor_range_stack;
6169 p->elements = constructor_elements;
6170 p->spelling = spelling;
6171 p->spelling_base = spelling_base;
6172 p->spelling_size = spelling_size;
6173 p->top_level = constructor_top_level;
6174 p->next = initializer_stack;
6175 initializer_stack = p;
6177 constructor_decl = decl;
6178 constructor_designated = 0;
6179 constructor_top_level = top_level;
6181 if (decl != 0 && decl != error_mark_node)
6183 require_constant_value = TREE_STATIC (decl);
6184 require_constant_elements
6185 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6186 /* For a scalar, you can always use any value to initialize,
6187 even within braces. */
6188 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6189 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6190 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6191 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6192 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6194 else
6196 require_constant_value = 0;
6197 require_constant_elements = 0;
6198 locus = _("(anonymous)");
6201 constructor_stack = 0;
6202 constructor_range_stack = 0;
6204 missing_braces_mentioned = 0;
6206 spelling_base = 0;
6207 spelling_size = 0;
6208 RESTORE_SPELLING_DEPTH (0);
6210 if (locus)
6211 push_string (locus);
6214 void
6215 finish_init (void)
6217 struct initializer_stack *p = initializer_stack;
6219 /* Free the whole constructor stack of this initializer. */
6220 while (constructor_stack)
6222 struct constructor_stack *q = constructor_stack;
6223 constructor_stack = q->next;
6224 free (q);
6227 gcc_assert (!constructor_range_stack);
6229 /* Pop back to the data of the outer initializer (if any). */
6230 free (spelling_base);
6232 constructor_decl = p->decl;
6233 require_constant_value = p->require_constant_value;
6234 require_constant_elements = p->require_constant_elements;
6235 constructor_stack = p->constructor_stack;
6236 constructor_range_stack = p->constructor_range_stack;
6237 constructor_elements = p->elements;
6238 spelling = p->spelling;
6239 spelling_base = p->spelling_base;
6240 spelling_size = p->spelling_size;
6241 constructor_top_level = p->top_level;
6242 initializer_stack = p->next;
6243 free (p);
6246 /* Call here when we see the initializer is surrounded by braces.
6247 This is instead of a call to push_init_level;
6248 it is matched by a call to pop_init_level.
6250 TYPE is the type to initialize, for a constructor expression.
6251 For an initializer for a decl, TYPE is zero. */
6253 void
6254 really_start_incremental_init (tree type)
6256 struct constructor_stack *p = XNEW (struct constructor_stack);
6258 if (type == 0)
6259 type = TREE_TYPE (constructor_decl);
6261 if (TREE_CODE (type) == VECTOR_TYPE
6262 && TYPE_VECTOR_OPAQUE (type))
6263 error ("opaque vector types cannot be initialized");
6265 p->type = constructor_type;
6266 p->fields = constructor_fields;
6267 p->index = constructor_index;
6268 p->max_index = constructor_max_index;
6269 p->unfilled_index = constructor_unfilled_index;
6270 p->unfilled_fields = constructor_unfilled_fields;
6271 p->bit_index = constructor_bit_index;
6272 p->elements = constructor_elements;
6273 p->constant = constructor_constant;
6274 p->simple = constructor_simple;
6275 p->nonconst = constructor_nonconst;
6276 p->erroneous = constructor_erroneous;
6277 p->pending_elts = constructor_pending_elts;
6278 p->depth = constructor_depth;
6279 p->replacement_value.value = 0;
6280 p->replacement_value.original_code = ERROR_MARK;
6281 p->replacement_value.original_type = NULL;
6282 p->implicit = 0;
6283 p->range_stack = 0;
6284 p->outer = 0;
6285 p->incremental = constructor_incremental;
6286 p->designated = constructor_designated;
6287 p->next = 0;
6288 constructor_stack = p;
6290 constructor_constant = 1;
6291 constructor_simple = 1;
6292 constructor_nonconst = 0;
6293 constructor_depth = SPELLING_DEPTH ();
6294 constructor_elements = 0;
6295 constructor_pending_elts = 0;
6296 constructor_type = type;
6297 constructor_incremental = 1;
6298 constructor_designated = 0;
6299 designator_depth = 0;
6300 designator_erroneous = 0;
6302 if (TREE_CODE (constructor_type) == RECORD_TYPE
6303 || TREE_CODE (constructor_type) == UNION_TYPE)
6305 constructor_fields = TYPE_FIELDS (constructor_type);
6306 /* Skip any nameless bit fields at the beginning. */
6307 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6308 && DECL_NAME (constructor_fields) == 0)
6309 constructor_fields = TREE_CHAIN (constructor_fields);
6311 constructor_unfilled_fields = constructor_fields;
6312 constructor_bit_index = bitsize_zero_node;
6314 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6316 if (TYPE_DOMAIN (constructor_type))
6318 constructor_max_index
6319 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6321 /* Detect non-empty initializations of zero-length arrays. */
6322 if (constructor_max_index == NULL_TREE
6323 && TYPE_SIZE (constructor_type))
6324 constructor_max_index = build_int_cst (NULL_TREE, -1);
6326 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6327 to initialize VLAs will cause a proper error; avoid tree
6328 checking errors as well by setting a safe value. */
6329 if (constructor_max_index
6330 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6331 constructor_max_index = build_int_cst (NULL_TREE, -1);
6333 constructor_index
6334 = convert (bitsizetype,
6335 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6337 else
6339 constructor_index = bitsize_zero_node;
6340 constructor_max_index = NULL_TREE;
6343 constructor_unfilled_index = constructor_index;
6345 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6347 /* Vectors are like simple fixed-size arrays. */
6348 constructor_max_index =
6349 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6350 constructor_index = bitsize_zero_node;
6351 constructor_unfilled_index = constructor_index;
6353 else
6355 /* Handle the case of int x = {5}; */
6356 constructor_fields = constructor_type;
6357 constructor_unfilled_fields = constructor_type;
6361 /* Push down into a subobject, for initialization.
6362 If this is for an explicit set of braces, IMPLICIT is 0.
6363 If it is because the next element belongs at a lower level,
6364 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6366 void
6367 push_init_level (int implicit)
6369 struct constructor_stack *p;
6370 tree value = NULL_TREE;
6372 /* If we've exhausted any levels that didn't have braces,
6373 pop them now. If implicit == 1, this will have been done in
6374 process_init_element; do not repeat it here because in the case
6375 of excess initializers for an empty aggregate this leads to an
6376 infinite cycle of popping a level and immediately recreating
6377 it. */
6378 if (implicit != 1)
6380 while (constructor_stack->implicit)
6382 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6383 || TREE_CODE (constructor_type) == UNION_TYPE)
6384 && constructor_fields == 0)
6385 process_init_element (pop_init_level (1), true);
6386 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6387 && constructor_max_index
6388 && tree_int_cst_lt (constructor_max_index,
6389 constructor_index))
6390 process_init_element (pop_init_level (1), true);
6391 else
6392 break;
6396 /* Unless this is an explicit brace, we need to preserve previous
6397 content if any. */
6398 if (implicit)
6400 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6401 || TREE_CODE (constructor_type) == UNION_TYPE)
6402 && constructor_fields)
6403 value = find_init_member (constructor_fields);
6404 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6405 value = find_init_member (constructor_index);
6408 p = XNEW (struct constructor_stack);
6409 p->type = constructor_type;
6410 p->fields = constructor_fields;
6411 p->index = constructor_index;
6412 p->max_index = constructor_max_index;
6413 p->unfilled_index = constructor_unfilled_index;
6414 p->unfilled_fields = constructor_unfilled_fields;
6415 p->bit_index = constructor_bit_index;
6416 p->elements = constructor_elements;
6417 p->constant = constructor_constant;
6418 p->simple = constructor_simple;
6419 p->nonconst = constructor_nonconst;
6420 p->erroneous = constructor_erroneous;
6421 p->pending_elts = constructor_pending_elts;
6422 p->depth = constructor_depth;
6423 p->replacement_value.value = 0;
6424 p->replacement_value.original_code = ERROR_MARK;
6425 p->replacement_value.original_type = NULL;
6426 p->implicit = implicit;
6427 p->outer = 0;
6428 p->incremental = constructor_incremental;
6429 p->designated = constructor_designated;
6430 p->next = constructor_stack;
6431 p->range_stack = 0;
6432 constructor_stack = p;
6434 constructor_constant = 1;
6435 constructor_simple = 1;
6436 constructor_nonconst = 0;
6437 constructor_depth = SPELLING_DEPTH ();
6438 constructor_elements = 0;
6439 constructor_incremental = 1;
6440 constructor_designated = 0;
6441 constructor_pending_elts = 0;
6442 if (!implicit)
6444 p->range_stack = constructor_range_stack;
6445 constructor_range_stack = 0;
6446 designator_depth = 0;
6447 designator_erroneous = 0;
6450 /* Don't die if an entire brace-pair level is superfluous
6451 in the containing level. */
6452 if (constructor_type == 0)
6454 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6455 || TREE_CODE (constructor_type) == UNION_TYPE)
6457 /* Don't die if there are extra init elts at the end. */
6458 if (constructor_fields == 0)
6459 constructor_type = 0;
6460 else
6462 constructor_type = TREE_TYPE (constructor_fields);
6463 push_member_name (constructor_fields);
6464 constructor_depth++;
6467 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6469 constructor_type = TREE_TYPE (constructor_type);
6470 push_array_bounds (tree_low_cst (constructor_index, 1));
6471 constructor_depth++;
6474 if (constructor_type == 0)
6476 error_init ("extra brace group at end of initializer");
6477 constructor_fields = 0;
6478 constructor_unfilled_fields = 0;
6479 return;
6482 if (value && TREE_CODE (value) == CONSTRUCTOR)
6484 constructor_constant = TREE_CONSTANT (value);
6485 constructor_simple = TREE_STATIC (value);
6486 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6487 constructor_elements = CONSTRUCTOR_ELTS (value);
6488 if (!VEC_empty (constructor_elt, constructor_elements)
6489 && (TREE_CODE (constructor_type) == RECORD_TYPE
6490 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6491 set_nonincremental_init ();
6494 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6496 missing_braces_mentioned = 1;
6497 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6500 if (TREE_CODE (constructor_type) == RECORD_TYPE
6501 || TREE_CODE (constructor_type) == UNION_TYPE)
6503 constructor_fields = TYPE_FIELDS (constructor_type);
6504 /* Skip any nameless bit fields at the beginning. */
6505 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6506 && DECL_NAME (constructor_fields) == 0)
6507 constructor_fields = TREE_CHAIN (constructor_fields);
6509 constructor_unfilled_fields = constructor_fields;
6510 constructor_bit_index = bitsize_zero_node;
6512 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6514 /* Vectors are like simple fixed-size arrays. */
6515 constructor_max_index =
6516 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6517 constructor_index = convert (bitsizetype, integer_zero_node);
6518 constructor_unfilled_index = constructor_index;
6520 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6522 if (TYPE_DOMAIN (constructor_type))
6524 constructor_max_index
6525 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6527 /* Detect non-empty initializations of zero-length arrays. */
6528 if (constructor_max_index == NULL_TREE
6529 && TYPE_SIZE (constructor_type))
6530 constructor_max_index = build_int_cst (NULL_TREE, -1);
6532 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6533 to initialize VLAs will cause a proper error; avoid tree
6534 checking errors as well by setting a safe value. */
6535 if (constructor_max_index
6536 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6537 constructor_max_index = build_int_cst (NULL_TREE, -1);
6539 constructor_index
6540 = convert (bitsizetype,
6541 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6543 else
6544 constructor_index = bitsize_zero_node;
6546 constructor_unfilled_index = constructor_index;
6547 if (value && TREE_CODE (value) == STRING_CST)
6549 /* We need to split the char/wchar array into individual
6550 characters, so that we don't have to special case it
6551 everywhere. */
6552 set_nonincremental_init_from_string (value);
6555 else
6557 if (constructor_type != error_mark_node)
6558 warning_init (0, "braces around scalar initializer");
6559 constructor_fields = constructor_type;
6560 constructor_unfilled_fields = constructor_type;
6564 /* At the end of an implicit or explicit brace level,
6565 finish up that level of constructor. If a single expression
6566 with redundant braces initialized that level, return the
6567 c_expr structure for that expression. Otherwise, the original_code
6568 element is set to ERROR_MARK.
6569 If we were outputting the elements as they are read, return 0 as the value
6570 from inner levels (process_init_element ignores that),
6571 but return error_mark_node as the value from the outermost level
6572 (that's what we want to put in DECL_INITIAL).
6573 Otherwise, return a CONSTRUCTOR expression as the value. */
6575 struct c_expr
6576 pop_init_level (int implicit)
6578 struct constructor_stack *p;
6579 struct c_expr ret;
6580 ret.value = 0;
6581 ret.original_code = ERROR_MARK;
6582 ret.original_type = NULL;
6584 if (implicit == 0)
6586 /* When we come to an explicit close brace,
6587 pop any inner levels that didn't have explicit braces. */
6588 while (constructor_stack->implicit)
6589 process_init_element (pop_init_level (1), true);
6591 gcc_assert (!constructor_range_stack);
6594 /* Now output all pending elements. */
6595 constructor_incremental = 1;
6596 output_pending_init_elements (1);
6598 p = constructor_stack;
6600 /* Error for initializing a flexible array member, or a zero-length
6601 array member in an inappropriate context. */
6602 if (constructor_type && constructor_fields
6603 && TREE_CODE (constructor_type) == ARRAY_TYPE
6604 && TYPE_DOMAIN (constructor_type)
6605 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6607 /* Silently discard empty initializations. The parser will
6608 already have pedwarned for empty brackets. */
6609 if (integer_zerop (constructor_unfilled_index))
6610 constructor_type = NULL_TREE;
6611 else
6613 gcc_assert (!TYPE_SIZE (constructor_type));
6615 if (constructor_depth > 2)
6616 error_init ("initialization of flexible array member in a nested context");
6617 else
6618 pedwarn_init (input_location, OPT_pedantic,
6619 "initialization of a flexible array member");
6621 /* We have already issued an error message for the existence
6622 of a flexible array member not at the end of the structure.
6623 Discard the initializer so that we do not die later. */
6624 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
6625 constructor_type = NULL_TREE;
6629 /* Warn when some struct elements are implicitly initialized to zero. */
6630 if (warn_missing_field_initializers
6631 && constructor_type
6632 && TREE_CODE (constructor_type) == RECORD_TYPE
6633 && constructor_unfilled_fields)
6635 /* Do not warn for flexible array members or zero-length arrays. */
6636 while (constructor_unfilled_fields
6637 && (!DECL_SIZE (constructor_unfilled_fields)
6638 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6639 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6641 /* Do not warn if this level of the initializer uses member
6642 designators; it is likely to be deliberate. */
6643 if (constructor_unfilled_fields && !constructor_designated)
6645 push_member_name (constructor_unfilled_fields);
6646 warning_init (OPT_Wmissing_field_initializers,
6647 "missing initializer");
6648 RESTORE_SPELLING_DEPTH (constructor_depth);
6652 /* Pad out the end of the structure. */
6653 if (p->replacement_value.value)
6654 /* If this closes a superfluous brace pair,
6655 just pass out the element between them. */
6656 ret = p->replacement_value;
6657 else if (constructor_type == 0)
6659 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6660 && TREE_CODE (constructor_type) != UNION_TYPE
6661 && TREE_CODE (constructor_type) != ARRAY_TYPE
6662 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6664 /* A nonincremental scalar initializer--just return
6665 the element, after verifying there is just one. */
6666 if (VEC_empty (constructor_elt,constructor_elements))
6668 if (!constructor_erroneous)
6669 error_init ("empty scalar initializer");
6670 ret.value = error_mark_node;
6672 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6674 error_init ("extra elements in scalar initializer");
6675 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6677 else
6678 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6680 else
6682 if (constructor_erroneous)
6683 ret.value = error_mark_node;
6684 else
6686 ret.value = build_constructor (constructor_type,
6687 constructor_elements);
6688 if (constructor_constant)
6689 TREE_CONSTANT (ret.value) = 1;
6690 if (constructor_constant && constructor_simple)
6691 TREE_STATIC (ret.value) = 1;
6692 if (constructor_nonconst)
6693 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6697 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6699 if (constructor_nonconst)
6700 ret.original_code = C_MAYBE_CONST_EXPR;
6701 else if (ret.original_code == C_MAYBE_CONST_EXPR)
6702 ret.original_code = ERROR_MARK;
6705 constructor_type = p->type;
6706 constructor_fields = p->fields;
6707 constructor_index = p->index;
6708 constructor_max_index = p->max_index;
6709 constructor_unfilled_index = p->unfilled_index;
6710 constructor_unfilled_fields = p->unfilled_fields;
6711 constructor_bit_index = p->bit_index;
6712 constructor_elements = p->elements;
6713 constructor_constant = p->constant;
6714 constructor_simple = p->simple;
6715 constructor_nonconst = p->nonconst;
6716 constructor_erroneous = p->erroneous;
6717 constructor_incremental = p->incremental;
6718 constructor_designated = p->designated;
6719 constructor_pending_elts = p->pending_elts;
6720 constructor_depth = p->depth;
6721 if (!p->implicit)
6722 constructor_range_stack = p->range_stack;
6723 RESTORE_SPELLING_DEPTH (constructor_depth);
6725 constructor_stack = p->next;
6726 free (p);
6728 if (ret.value == 0 && constructor_stack == 0)
6729 ret.value = error_mark_node;
6730 return ret;
6733 /* Common handling for both array range and field name designators.
6734 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6736 static int
6737 set_designator (int array)
6739 tree subtype;
6740 enum tree_code subcode;
6742 /* Don't die if an entire brace-pair level is superfluous
6743 in the containing level. */
6744 if (constructor_type == 0)
6745 return 1;
6747 /* If there were errors in this designator list already, bail out
6748 silently. */
6749 if (designator_erroneous)
6750 return 1;
6752 if (!designator_depth)
6754 gcc_assert (!constructor_range_stack);
6756 /* Designator list starts at the level of closest explicit
6757 braces. */
6758 while (constructor_stack->implicit)
6759 process_init_element (pop_init_level (1), true);
6760 constructor_designated = 1;
6761 return 0;
6764 switch (TREE_CODE (constructor_type))
6766 case RECORD_TYPE:
6767 case UNION_TYPE:
6768 subtype = TREE_TYPE (constructor_fields);
6769 if (subtype != error_mark_node)
6770 subtype = TYPE_MAIN_VARIANT (subtype);
6771 break;
6772 case ARRAY_TYPE:
6773 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6774 break;
6775 default:
6776 gcc_unreachable ();
6779 subcode = TREE_CODE (subtype);
6780 if (array && subcode != ARRAY_TYPE)
6782 error_init ("array index in non-array initializer");
6783 return 1;
6785 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
6787 error_init ("field name not in record or union initializer");
6788 return 1;
6791 constructor_designated = 1;
6792 push_init_level (2);
6793 return 0;
6796 /* If there are range designators in designator list, push a new designator
6797 to constructor_range_stack. RANGE_END is end of such stack range or
6798 NULL_TREE if there is no range designator at this level. */
6800 static void
6801 push_range_stack (tree range_end)
6803 struct constructor_range_stack *p;
6805 p = GGC_NEW (struct constructor_range_stack);
6806 p->prev = constructor_range_stack;
6807 p->next = 0;
6808 p->fields = constructor_fields;
6809 p->range_start = constructor_index;
6810 p->index = constructor_index;
6811 p->stack = constructor_stack;
6812 p->range_end = range_end;
6813 if (constructor_range_stack)
6814 constructor_range_stack->next = p;
6815 constructor_range_stack = p;
6818 /* Within an array initializer, specify the next index to be initialized.
6819 FIRST is that index. If LAST is nonzero, then initialize a range
6820 of indices, running from FIRST through LAST. */
6822 void
6823 set_init_index (tree first, tree last)
6825 if (set_designator (1))
6826 return;
6828 designator_erroneous = 1;
6830 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
6831 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
6833 error_init ("array index in initializer not of integer type");
6834 return;
6837 if (TREE_CODE (first) != INTEGER_CST)
6839 first = c_fully_fold (first, false, NULL);
6840 if (TREE_CODE (first) == INTEGER_CST)
6841 pedwarn_init (input_location, OPT_pedantic,
6842 "array index in initializer is not "
6843 "an integer constant expression");
6846 if (last && TREE_CODE (last) != INTEGER_CST)
6848 last = c_fully_fold (last, false, NULL);
6849 if (TREE_CODE (last) == INTEGER_CST)
6850 pedwarn_init (input_location, OPT_pedantic,
6851 "array index in initializer is not "
6852 "an integer constant expression");
6855 if (TREE_CODE (first) != INTEGER_CST)
6856 error_init ("nonconstant array index in initializer");
6857 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
6858 error_init ("nonconstant array index in initializer");
6859 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6860 error_init ("array index in non-array initializer");
6861 else if (tree_int_cst_sgn (first) == -1)
6862 error_init ("array index in initializer exceeds array bounds");
6863 else if (constructor_max_index
6864 && tree_int_cst_lt (constructor_max_index, first))
6865 error_init ("array index in initializer exceeds array bounds");
6866 else
6868 constant_expression_warning (first);
6869 if (last)
6870 constant_expression_warning (last);
6871 constructor_index = convert (bitsizetype, first);
6873 if (last)
6875 if (tree_int_cst_equal (first, last))
6876 last = 0;
6877 else if (tree_int_cst_lt (last, first))
6879 error_init ("empty index range in initializer");
6880 last = 0;
6882 else
6884 last = convert (bitsizetype, last);
6885 if (constructor_max_index != 0
6886 && tree_int_cst_lt (constructor_max_index, last))
6888 error_init ("array index range in initializer exceeds array bounds");
6889 last = 0;
6894 designator_depth++;
6895 designator_erroneous = 0;
6896 if (constructor_range_stack || last)
6897 push_range_stack (last);
6901 /* Within a struct initializer, specify the next field to be initialized. */
6903 void
6904 set_init_label (tree fieldname)
6906 tree tail;
6908 if (set_designator (0))
6909 return;
6911 designator_erroneous = 1;
6913 if (TREE_CODE (constructor_type) != RECORD_TYPE
6914 && TREE_CODE (constructor_type) != UNION_TYPE)
6916 error_init ("field name not in record or union initializer");
6917 return;
6920 for (tail = TYPE_FIELDS (constructor_type); tail;
6921 tail = TREE_CHAIN (tail))
6923 if (DECL_NAME (tail) == fieldname)
6924 break;
6927 if (tail == 0)
6928 error ("unknown field %qE specified in initializer", fieldname);
6929 else
6931 constructor_fields = tail;
6932 designator_depth++;
6933 designator_erroneous = 0;
6934 if (constructor_range_stack)
6935 push_range_stack (NULL_TREE);
6939 /* Add a new initializer to the tree of pending initializers. PURPOSE
6940 identifies the initializer, either array index or field in a structure.
6941 VALUE is the value of that index or field. If ORIGTYPE is not
6942 NULL_TREE, it is the original type of VALUE.
6944 IMPLICIT is true if value comes from pop_init_level (1),
6945 the new initializer has been merged with the existing one
6946 and thus no warnings should be emitted about overriding an
6947 existing initializer. */
6949 static void
6950 add_pending_init (tree purpose, tree value, tree origtype, bool implicit)
6952 struct init_node *p, **q, *r;
6954 q = &constructor_pending_elts;
6955 p = 0;
6957 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6959 while (*q != 0)
6961 p = *q;
6962 if (tree_int_cst_lt (purpose, p->purpose))
6963 q = &p->left;
6964 else if (tree_int_cst_lt (p->purpose, purpose))
6965 q = &p->right;
6966 else
6968 if (!implicit)
6970 if (TREE_SIDE_EFFECTS (p->value))
6971 warning_init (0, "initialized field with side-effects overwritten");
6972 else if (warn_override_init)
6973 warning_init (OPT_Woverride_init, "initialized field overwritten");
6975 p->value = value;
6976 p->origtype = origtype;
6977 return;
6981 else
6983 tree bitpos;
6985 bitpos = bit_position (purpose);
6986 while (*q != NULL)
6988 p = *q;
6989 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
6990 q = &p->left;
6991 else if (p->purpose != purpose)
6992 q = &p->right;
6993 else
6995 if (!implicit)
6997 if (TREE_SIDE_EFFECTS (p->value))
6998 warning_init (0, "initialized field with side-effects overwritten");
6999 else if (warn_override_init)
7000 warning_init (OPT_Woverride_init, "initialized field overwritten");
7002 p->value = value;
7003 p->origtype = origtype;
7004 return;
7009 r = GGC_NEW (struct init_node);
7010 r->purpose = purpose;
7011 r->value = value;
7012 r->origtype = origtype;
7014 *q = r;
7015 r->parent = p;
7016 r->left = 0;
7017 r->right = 0;
7018 r->balance = 0;
7020 while (p)
7022 struct init_node *s;
7024 if (r == p->left)
7026 if (p->balance == 0)
7027 p->balance = -1;
7028 else if (p->balance < 0)
7030 if (r->balance < 0)
7032 /* L rotation. */
7033 p->left = r->right;
7034 if (p->left)
7035 p->left->parent = p;
7036 r->right = p;
7038 p->balance = 0;
7039 r->balance = 0;
7041 s = p->parent;
7042 p->parent = r;
7043 r->parent = s;
7044 if (s)
7046 if (s->left == p)
7047 s->left = r;
7048 else
7049 s->right = r;
7051 else
7052 constructor_pending_elts = r;
7054 else
7056 /* LR rotation. */
7057 struct init_node *t = r->right;
7059 r->right = t->left;
7060 if (r->right)
7061 r->right->parent = r;
7062 t->left = r;
7064 p->left = t->right;
7065 if (p->left)
7066 p->left->parent = p;
7067 t->right = p;
7069 p->balance = t->balance < 0;
7070 r->balance = -(t->balance > 0);
7071 t->balance = 0;
7073 s = p->parent;
7074 p->parent = t;
7075 r->parent = t;
7076 t->parent = s;
7077 if (s)
7079 if (s->left == p)
7080 s->left = t;
7081 else
7082 s->right = t;
7084 else
7085 constructor_pending_elts = t;
7087 break;
7089 else
7091 /* p->balance == +1; growth of left side balances the node. */
7092 p->balance = 0;
7093 break;
7096 else /* r == p->right */
7098 if (p->balance == 0)
7099 /* Growth propagation from right side. */
7100 p->balance++;
7101 else if (p->balance > 0)
7103 if (r->balance > 0)
7105 /* R rotation. */
7106 p->right = r->left;
7107 if (p->right)
7108 p->right->parent = p;
7109 r->left = p;
7111 p->balance = 0;
7112 r->balance = 0;
7114 s = p->parent;
7115 p->parent = r;
7116 r->parent = s;
7117 if (s)
7119 if (s->left == p)
7120 s->left = r;
7121 else
7122 s->right = r;
7124 else
7125 constructor_pending_elts = r;
7127 else /* r->balance == -1 */
7129 /* RL rotation */
7130 struct init_node *t = r->left;
7132 r->left = t->right;
7133 if (r->left)
7134 r->left->parent = r;
7135 t->right = r;
7137 p->right = t->left;
7138 if (p->right)
7139 p->right->parent = p;
7140 t->left = p;
7142 r->balance = (t->balance < 0);
7143 p->balance = -(t->balance > 0);
7144 t->balance = 0;
7146 s = p->parent;
7147 p->parent = t;
7148 r->parent = t;
7149 t->parent = s;
7150 if (s)
7152 if (s->left == p)
7153 s->left = t;
7154 else
7155 s->right = t;
7157 else
7158 constructor_pending_elts = t;
7160 break;
7162 else
7164 /* p->balance == -1; growth of right side balances the node. */
7165 p->balance = 0;
7166 break;
7170 r = p;
7171 p = p->parent;
7175 /* Build AVL tree from a sorted chain. */
7177 static void
7178 set_nonincremental_init (void)
7180 unsigned HOST_WIDE_INT ix;
7181 tree index, value;
7183 if (TREE_CODE (constructor_type) != RECORD_TYPE
7184 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7185 return;
7187 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7188 add_pending_init (index, value, NULL_TREE, false);
7189 constructor_elements = 0;
7190 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7192 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7193 /* Skip any nameless bit fields at the beginning. */
7194 while (constructor_unfilled_fields != 0
7195 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7196 && DECL_NAME (constructor_unfilled_fields) == 0)
7197 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7200 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7202 if (TYPE_DOMAIN (constructor_type))
7203 constructor_unfilled_index
7204 = convert (bitsizetype,
7205 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7206 else
7207 constructor_unfilled_index = bitsize_zero_node;
7209 constructor_incremental = 0;
7212 /* Build AVL tree from a string constant. */
7214 static void
7215 set_nonincremental_init_from_string (tree str)
7217 tree value, purpose, type;
7218 HOST_WIDE_INT val[2];
7219 const char *p, *end;
7220 int byte, wchar_bytes, charwidth, bitpos;
7222 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7224 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7225 charwidth = TYPE_PRECISION (char_type_node);
7226 type = TREE_TYPE (constructor_type);
7227 p = TREE_STRING_POINTER (str);
7228 end = p + TREE_STRING_LENGTH (str);
7230 for (purpose = bitsize_zero_node;
7231 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7232 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7234 if (wchar_bytes == 1)
7236 val[1] = (unsigned char) *p++;
7237 val[0] = 0;
7239 else
7241 val[0] = 0;
7242 val[1] = 0;
7243 for (byte = 0; byte < wchar_bytes; byte++)
7245 if (BYTES_BIG_ENDIAN)
7246 bitpos = (wchar_bytes - byte - 1) * charwidth;
7247 else
7248 bitpos = byte * charwidth;
7249 val[bitpos < HOST_BITS_PER_WIDE_INT]
7250 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7251 << (bitpos % HOST_BITS_PER_WIDE_INT);
7255 if (!TYPE_UNSIGNED (type))
7257 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7258 if (bitpos < HOST_BITS_PER_WIDE_INT)
7260 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7262 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7263 val[0] = -1;
7266 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7268 if (val[1] < 0)
7269 val[0] = -1;
7271 else if (val[0] & (((HOST_WIDE_INT) 1)
7272 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7273 val[0] |= ((HOST_WIDE_INT) -1)
7274 << (bitpos - HOST_BITS_PER_WIDE_INT);
7277 value = build_int_cst_wide (type, val[1], val[0]);
7278 add_pending_init (purpose, value, NULL_TREE, false);
7281 constructor_incremental = 0;
7284 /* Return value of FIELD in pending initializer or zero if the field was
7285 not initialized yet. */
7287 static tree
7288 find_init_member (tree field)
7290 struct init_node *p;
7292 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7294 if (constructor_incremental
7295 && tree_int_cst_lt (field, constructor_unfilled_index))
7296 set_nonincremental_init ();
7298 p = constructor_pending_elts;
7299 while (p)
7301 if (tree_int_cst_lt (field, p->purpose))
7302 p = p->left;
7303 else if (tree_int_cst_lt (p->purpose, field))
7304 p = p->right;
7305 else
7306 return p->value;
7309 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7311 tree bitpos = bit_position (field);
7313 if (constructor_incremental
7314 && (!constructor_unfilled_fields
7315 || tree_int_cst_lt (bitpos,
7316 bit_position (constructor_unfilled_fields))))
7317 set_nonincremental_init ();
7319 p = constructor_pending_elts;
7320 while (p)
7322 if (field == p->purpose)
7323 return p->value;
7324 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7325 p = p->left;
7326 else
7327 p = p->right;
7330 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7332 if (!VEC_empty (constructor_elt, constructor_elements)
7333 && (VEC_last (constructor_elt, constructor_elements)->index
7334 == field))
7335 return VEC_last (constructor_elt, constructor_elements)->value;
7337 return 0;
7340 /* "Output" the next constructor element.
7341 At top level, really output it to assembler code now.
7342 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7343 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7344 TYPE is the data type that the containing data type wants here.
7345 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7346 If VALUE is a string constant, STRICT_STRING is true if it is
7347 unparenthesized or we should not warn here for it being parenthesized.
7348 For other types of VALUE, STRICT_STRING is not used.
7350 PENDING if non-nil means output pending elements that belong
7351 right after this element. (PENDING is normally 1;
7352 it is 0 while outputting pending elements, to avoid recursion.)
7354 IMPLICIT is true if value comes from pop_init_level (1),
7355 the new initializer has been merged with the existing one
7356 and thus no warnings should be emitted about overriding an
7357 existing initializer. */
7359 static void
7360 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7361 tree field, int pending, bool implicit)
7363 tree semantic_type = NULL_TREE;
7364 constructor_elt *celt;
7365 bool maybe_const = true;
7366 bool npc;
7368 if (type == error_mark_node || value == error_mark_node)
7370 constructor_erroneous = 1;
7371 return;
7373 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7374 && (TREE_CODE (value) == STRING_CST
7375 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7376 && !(TREE_CODE (value) == STRING_CST
7377 && TREE_CODE (type) == ARRAY_TYPE
7378 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7379 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7380 TYPE_MAIN_VARIANT (type)))
7381 value = array_to_pointer_conversion (input_location, value);
7383 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7384 && require_constant_value && !flag_isoc99 && pending)
7386 /* As an extension, allow initializing objects with static storage
7387 duration with compound literals (which are then treated just as
7388 the brace enclosed list they contain). */
7389 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7390 value = DECL_INITIAL (decl);
7393 npc = null_pointer_constant_p (value);
7394 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7396 semantic_type = TREE_TYPE (value);
7397 value = TREE_OPERAND (value, 0);
7399 value = c_fully_fold (value, require_constant_value, &maybe_const);
7401 if (value == error_mark_node)
7402 constructor_erroneous = 1;
7403 else if (!TREE_CONSTANT (value))
7404 constructor_constant = 0;
7405 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7406 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7407 || TREE_CODE (constructor_type) == UNION_TYPE)
7408 && DECL_C_BIT_FIELD (field)
7409 && TREE_CODE (value) != INTEGER_CST))
7410 constructor_simple = 0;
7411 if (!maybe_const)
7412 constructor_nonconst = 1;
7414 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7416 if (require_constant_value)
7418 error_init ("initializer element is not constant");
7419 value = error_mark_node;
7421 else if (require_constant_elements)
7422 pedwarn (input_location, 0,
7423 "initializer element is not computable at load time");
7425 else if (!maybe_const
7426 && (require_constant_value || require_constant_elements))
7427 pedwarn_init (input_location, 0,
7428 "initializer element is not a constant expression");
7430 /* Issue -Wc++-compat warnings about initializing a bitfield with
7431 enum type. */
7432 if (warn_cxx_compat
7433 && field != NULL_TREE
7434 && TREE_CODE (field) == FIELD_DECL
7435 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7436 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7437 != TYPE_MAIN_VARIANT (type))
7438 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7440 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7441 if (checktype != error_mark_node
7442 && (TYPE_MAIN_VARIANT (checktype)
7443 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7444 warning_init (OPT_Wc___compat,
7445 "enum conversion in initialization is invalid in C++");
7448 /* If this field is empty (and not at the end of structure),
7449 don't do anything other than checking the initializer. */
7450 if (field
7451 && (TREE_TYPE (field) == error_mark_node
7452 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7453 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7454 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7455 || TREE_CHAIN (field)))))
7456 return;
7458 if (semantic_type)
7459 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7460 value = digest_init (input_location, type, value, origtype, npc,
7461 strict_string, require_constant_value);
7462 if (value == error_mark_node)
7464 constructor_erroneous = 1;
7465 return;
7467 if (require_constant_value || require_constant_elements)
7468 constant_expression_warning (value);
7470 /* If this element doesn't come next in sequence,
7471 put it on constructor_pending_elts. */
7472 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7473 && (!constructor_incremental
7474 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7476 if (constructor_incremental
7477 && tree_int_cst_lt (field, constructor_unfilled_index))
7478 set_nonincremental_init ();
7480 add_pending_init (field, value, origtype, implicit);
7481 return;
7483 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7484 && (!constructor_incremental
7485 || field != constructor_unfilled_fields))
7487 /* We do this for records but not for unions. In a union,
7488 no matter which field is specified, it can be initialized
7489 right away since it starts at the beginning of the union. */
7490 if (constructor_incremental)
7492 if (!constructor_unfilled_fields)
7493 set_nonincremental_init ();
7494 else
7496 tree bitpos, unfillpos;
7498 bitpos = bit_position (field);
7499 unfillpos = bit_position (constructor_unfilled_fields);
7501 if (tree_int_cst_lt (bitpos, unfillpos))
7502 set_nonincremental_init ();
7506 add_pending_init (field, value, origtype, implicit);
7507 return;
7509 else if (TREE_CODE (constructor_type) == UNION_TYPE
7510 && !VEC_empty (constructor_elt, constructor_elements))
7512 if (!implicit)
7514 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7515 constructor_elements)->value))
7516 warning_init (0,
7517 "initialized field with side-effects overwritten");
7518 else if (warn_override_init)
7519 warning_init (OPT_Woverride_init, "initialized field overwritten");
7522 /* We can have just one union field set. */
7523 constructor_elements = 0;
7526 /* Otherwise, output this element either to
7527 constructor_elements or to the assembler file. */
7529 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7530 celt->index = field;
7531 celt->value = value;
7533 /* Advance the variable that indicates sequential elements output. */
7534 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7535 constructor_unfilled_index
7536 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7537 bitsize_one_node);
7538 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7540 constructor_unfilled_fields
7541 = TREE_CHAIN (constructor_unfilled_fields);
7543 /* Skip any nameless bit fields. */
7544 while (constructor_unfilled_fields != 0
7545 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7546 && DECL_NAME (constructor_unfilled_fields) == 0)
7547 constructor_unfilled_fields =
7548 TREE_CHAIN (constructor_unfilled_fields);
7550 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7551 constructor_unfilled_fields = 0;
7553 /* Now output any pending elements which have become next. */
7554 if (pending)
7555 output_pending_init_elements (0);
7558 /* Output any pending elements which have become next.
7559 As we output elements, constructor_unfilled_{fields,index}
7560 advances, which may cause other elements to become next;
7561 if so, they too are output.
7563 If ALL is 0, we return when there are
7564 no more pending elements to output now.
7566 If ALL is 1, we output space as necessary so that
7567 we can output all the pending elements. */
7569 static void
7570 output_pending_init_elements (int all)
7572 struct init_node *elt = constructor_pending_elts;
7573 tree next;
7575 retry:
7577 /* Look through the whole pending tree.
7578 If we find an element that should be output now,
7579 output it. Otherwise, set NEXT to the element
7580 that comes first among those still pending. */
7582 next = 0;
7583 while (elt)
7585 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7587 if (tree_int_cst_equal (elt->purpose,
7588 constructor_unfilled_index))
7589 output_init_element (elt->value, elt->origtype, true,
7590 TREE_TYPE (constructor_type),
7591 constructor_unfilled_index, 0, false);
7592 else if (tree_int_cst_lt (constructor_unfilled_index,
7593 elt->purpose))
7595 /* Advance to the next smaller node. */
7596 if (elt->left)
7597 elt = elt->left;
7598 else
7600 /* We have reached the smallest node bigger than the
7601 current unfilled index. Fill the space first. */
7602 next = elt->purpose;
7603 break;
7606 else
7608 /* Advance to the next bigger node. */
7609 if (elt->right)
7610 elt = elt->right;
7611 else
7613 /* We have reached the biggest node in a subtree. Find
7614 the parent of it, which is the next bigger node. */
7615 while (elt->parent && elt->parent->right == elt)
7616 elt = elt->parent;
7617 elt = elt->parent;
7618 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7619 elt->purpose))
7621 next = elt->purpose;
7622 break;
7627 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7628 || TREE_CODE (constructor_type) == UNION_TYPE)
7630 tree ctor_unfilled_bitpos, elt_bitpos;
7632 /* If the current record is complete we are done. */
7633 if (constructor_unfilled_fields == 0)
7634 break;
7636 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7637 elt_bitpos = bit_position (elt->purpose);
7638 /* We can't compare fields here because there might be empty
7639 fields in between. */
7640 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7642 constructor_unfilled_fields = elt->purpose;
7643 output_init_element (elt->value, elt->origtype, true,
7644 TREE_TYPE (elt->purpose),
7645 elt->purpose, 0, false);
7647 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7649 /* Advance to the next smaller node. */
7650 if (elt->left)
7651 elt = elt->left;
7652 else
7654 /* We have reached the smallest node bigger than the
7655 current unfilled field. Fill the space first. */
7656 next = elt->purpose;
7657 break;
7660 else
7662 /* Advance to the next bigger node. */
7663 if (elt->right)
7664 elt = elt->right;
7665 else
7667 /* We have reached the biggest node in a subtree. Find
7668 the parent of it, which is the next bigger node. */
7669 while (elt->parent && elt->parent->right == elt)
7670 elt = elt->parent;
7671 elt = elt->parent;
7672 if (elt
7673 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7674 bit_position (elt->purpose))))
7676 next = elt->purpose;
7677 break;
7684 /* Ordinarily return, but not if we want to output all
7685 and there are elements left. */
7686 if (!(all && next != 0))
7687 return;
7689 /* If it's not incremental, just skip over the gap, so that after
7690 jumping to retry we will output the next successive element. */
7691 if (TREE_CODE (constructor_type) == RECORD_TYPE
7692 || TREE_CODE (constructor_type) == UNION_TYPE)
7693 constructor_unfilled_fields = next;
7694 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7695 constructor_unfilled_index = next;
7697 /* ELT now points to the node in the pending tree with the next
7698 initializer to output. */
7699 goto retry;
7702 /* Add one non-braced element to the current constructor level.
7703 This adjusts the current position within the constructor's type.
7704 This may also start or terminate implicit levels
7705 to handle a partly-braced initializer.
7707 Once this has found the correct level for the new element,
7708 it calls output_init_element.
7710 IMPLICIT is true if value comes from pop_init_level (1),
7711 the new initializer has been merged with the existing one
7712 and thus no warnings should be emitted about overriding an
7713 existing initializer. */
7715 void
7716 process_init_element (struct c_expr value, bool implicit)
7718 tree orig_value = value.value;
7719 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
7720 bool strict_string = value.original_code == STRING_CST;
7722 designator_depth = 0;
7723 designator_erroneous = 0;
7725 /* Handle superfluous braces around string cst as in
7726 char x[] = {"foo"}; */
7727 if (string_flag
7728 && constructor_type
7729 && TREE_CODE (constructor_type) == ARRAY_TYPE
7730 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
7731 && integer_zerop (constructor_unfilled_index))
7733 if (constructor_stack->replacement_value.value)
7734 error_init ("excess elements in char array initializer");
7735 constructor_stack->replacement_value = value;
7736 return;
7739 if (constructor_stack->replacement_value.value != 0)
7741 error_init ("excess elements in struct initializer");
7742 return;
7745 /* Ignore elements of a brace group if it is entirely superfluous
7746 and has already been diagnosed. */
7747 if (constructor_type == 0)
7748 return;
7750 /* If we've exhausted any levels that didn't have braces,
7751 pop them now. */
7752 while (constructor_stack->implicit)
7754 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7755 || TREE_CODE (constructor_type) == UNION_TYPE)
7756 && constructor_fields == 0)
7757 process_init_element (pop_init_level (1), true);
7758 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
7759 || TREE_CODE (constructor_type) == VECTOR_TYPE)
7760 && (constructor_max_index == 0
7761 || tree_int_cst_lt (constructor_max_index,
7762 constructor_index)))
7763 process_init_element (pop_init_level (1), true);
7764 else
7765 break;
7768 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7769 if (constructor_range_stack)
7771 /* If value is a compound literal and we'll be just using its
7772 content, don't put it into a SAVE_EXPR. */
7773 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
7774 || !require_constant_value
7775 || flag_isoc99)
7777 tree semantic_type = NULL_TREE;
7778 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
7780 semantic_type = TREE_TYPE (value.value);
7781 value.value = TREE_OPERAND (value.value, 0);
7783 value.value = c_save_expr (value.value);
7784 if (semantic_type)
7785 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7786 value.value);
7790 while (1)
7792 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7794 tree fieldtype;
7795 enum tree_code fieldcode;
7797 if (constructor_fields == 0)
7799 pedwarn_init (input_location, 0,
7800 "excess elements in struct initializer");
7801 break;
7804 fieldtype = TREE_TYPE (constructor_fields);
7805 if (fieldtype != error_mark_node)
7806 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7807 fieldcode = TREE_CODE (fieldtype);
7809 /* Error for non-static initialization of a flexible array member. */
7810 if (fieldcode == ARRAY_TYPE
7811 && !require_constant_value
7812 && TYPE_SIZE (fieldtype) == NULL_TREE
7813 && TREE_CHAIN (constructor_fields) == NULL_TREE)
7815 error_init ("non-static initialization of a flexible array member");
7816 break;
7819 /* Accept a string constant to initialize a subarray. */
7820 if (value.value != 0
7821 && fieldcode == ARRAY_TYPE
7822 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7823 && string_flag)
7824 value.value = orig_value;
7825 /* Otherwise, if we have come to a subaggregate,
7826 and we don't have an element of its type, push into it. */
7827 else if (value.value != 0
7828 && value.value != error_mark_node
7829 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7830 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7831 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
7833 push_init_level (1);
7834 continue;
7837 if (value.value)
7839 push_member_name (constructor_fields);
7840 output_init_element (value.value, value.original_type,
7841 strict_string, fieldtype,
7842 constructor_fields, 1, implicit);
7843 RESTORE_SPELLING_DEPTH (constructor_depth);
7845 else
7846 /* Do the bookkeeping for an element that was
7847 directly output as a constructor. */
7849 /* For a record, keep track of end position of last field. */
7850 if (DECL_SIZE (constructor_fields))
7851 constructor_bit_index
7852 = size_binop_loc (input_location, PLUS_EXPR,
7853 bit_position (constructor_fields),
7854 DECL_SIZE (constructor_fields));
7856 /* If the current field was the first one not yet written out,
7857 it isn't now, so update. */
7858 if (constructor_unfilled_fields == constructor_fields)
7860 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7861 /* Skip any nameless bit fields. */
7862 while (constructor_unfilled_fields != 0
7863 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7864 && DECL_NAME (constructor_unfilled_fields) == 0)
7865 constructor_unfilled_fields =
7866 TREE_CHAIN (constructor_unfilled_fields);
7870 constructor_fields = TREE_CHAIN (constructor_fields);
7871 /* Skip any nameless bit fields at the beginning. */
7872 while (constructor_fields != 0
7873 && DECL_C_BIT_FIELD (constructor_fields)
7874 && DECL_NAME (constructor_fields) == 0)
7875 constructor_fields = TREE_CHAIN (constructor_fields);
7877 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7879 tree fieldtype;
7880 enum tree_code fieldcode;
7882 if (constructor_fields == 0)
7884 pedwarn_init (input_location, 0,
7885 "excess elements in union initializer");
7886 break;
7889 fieldtype = TREE_TYPE (constructor_fields);
7890 if (fieldtype != error_mark_node)
7891 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7892 fieldcode = TREE_CODE (fieldtype);
7894 /* Warn that traditional C rejects initialization of unions.
7895 We skip the warning if the value is zero. This is done
7896 under the assumption that the zero initializer in user
7897 code appears conditioned on e.g. __STDC__ to avoid
7898 "missing initializer" warnings and relies on default
7899 initialization to zero in the traditional C case.
7900 We also skip the warning if the initializer is designated,
7901 again on the assumption that this must be conditional on
7902 __STDC__ anyway (and we've already complained about the
7903 member-designator already). */
7904 if (!in_system_header && !constructor_designated
7905 && !(value.value && (integer_zerop (value.value)
7906 || real_zerop (value.value))))
7907 warning (OPT_Wtraditional, "traditional C rejects initialization "
7908 "of unions");
7910 /* Accept a string constant to initialize a subarray. */
7911 if (value.value != 0
7912 && fieldcode == ARRAY_TYPE
7913 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7914 && string_flag)
7915 value.value = orig_value;
7916 /* Otherwise, if we have come to a subaggregate,
7917 and we don't have an element of its type, push into it. */
7918 else if (value.value != 0
7919 && value.value != error_mark_node
7920 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7921 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7922 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
7924 push_init_level (1);
7925 continue;
7928 if (value.value)
7930 push_member_name (constructor_fields);
7931 output_init_element (value.value, value.original_type,
7932 strict_string, fieldtype,
7933 constructor_fields, 1, implicit);
7934 RESTORE_SPELLING_DEPTH (constructor_depth);
7936 else
7937 /* Do the bookkeeping for an element that was
7938 directly output as a constructor. */
7940 constructor_bit_index = DECL_SIZE (constructor_fields);
7941 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7944 constructor_fields = 0;
7946 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7948 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7949 enum tree_code eltcode = TREE_CODE (elttype);
7951 /* Accept a string constant to initialize a subarray. */
7952 if (value.value != 0
7953 && eltcode == ARRAY_TYPE
7954 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
7955 && string_flag)
7956 value.value = orig_value;
7957 /* Otherwise, if we have come to a subaggregate,
7958 and we don't have an element of its type, push into it. */
7959 else if (value.value != 0
7960 && value.value != error_mark_node
7961 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
7962 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
7963 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
7965 push_init_level (1);
7966 continue;
7969 if (constructor_max_index != 0
7970 && (tree_int_cst_lt (constructor_max_index, constructor_index)
7971 || integer_all_onesp (constructor_max_index)))
7973 pedwarn_init (input_location, 0,
7974 "excess elements in array initializer");
7975 break;
7978 /* Now output the actual element. */
7979 if (value.value)
7981 push_array_bounds (tree_low_cst (constructor_index, 1));
7982 output_init_element (value.value, value.original_type,
7983 strict_string, elttype,
7984 constructor_index, 1, implicit);
7985 RESTORE_SPELLING_DEPTH (constructor_depth);
7988 constructor_index
7989 = size_binop_loc (input_location, PLUS_EXPR,
7990 constructor_index, bitsize_one_node);
7992 if (!value.value)
7993 /* If we are doing the bookkeeping for an element that was
7994 directly output as a constructor, we must update
7995 constructor_unfilled_index. */
7996 constructor_unfilled_index = constructor_index;
7998 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8000 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8002 /* Do a basic check of initializer size. Note that vectors
8003 always have a fixed size derived from their type. */
8004 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8006 pedwarn_init (input_location, 0,
8007 "excess elements in vector initializer");
8008 break;
8011 /* Now output the actual element. */
8012 if (value.value)
8014 if (TREE_CODE (value.value) == VECTOR_CST)
8015 elttype = TYPE_MAIN_VARIANT (constructor_type);
8016 output_init_element (value.value, value.original_type,
8017 strict_string, elttype,
8018 constructor_index, 1, implicit);
8021 constructor_index
8022 = size_binop_loc (input_location,
8023 PLUS_EXPR, constructor_index, bitsize_one_node);
8025 if (!value.value)
8026 /* If we are doing the bookkeeping for an element that was
8027 directly output as a constructor, we must update
8028 constructor_unfilled_index. */
8029 constructor_unfilled_index = constructor_index;
8032 /* Handle the sole element allowed in a braced initializer
8033 for a scalar variable. */
8034 else if (constructor_type != error_mark_node
8035 && constructor_fields == 0)
8037 pedwarn_init (input_location, 0,
8038 "excess elements in scalar initializer");
8039 break;
8041 else
8043 if (value.value)
8044 output_init_element (value.value, value.original_type,
8045 strict_string, constructor_type,
8046 NULL_TREE, 1, implicit);
8047 constructor_fields = 0;
8050 /* Handle range initializers either at this level or anywhere higher
8051 in the designator stack. */
8052 if (constructor_range_stack)
8054 struct constructor_range_stack *p, *range_stack;
8055 int finish = 0;
8057 range_stack = constructor_range_stack;
8058 constructor_range_stack = 0;
8059 while (constructor_stack != range_stack->stack)
8061 gcc_assert (constructor_stack->implicit);
8062 process_init_element (pop_init_level (1), true);
8064 for (p = range_stack;
8065 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8066 p = p->prev)
8068 gcc_assert (constructor_stack->implicit);
8069 process_init_element (pop_init_level (1), true);
8072 p->index = size_binop_loc (input_location,
8073 PLUS_EXPR, p->index, bitsize_one_node);
8074 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8075 finish = 1;
8077 while (1)
8079 constructor_index = p->index;
8080 constructor_fields = p->fields;
8081 if (finish && p->range_end && p->index == p->range_start)
8083 finish = 0;
8084 p->prev = 0;
8086 p = p->next;
8087 if (!p)
8088 break;
8089 push_init_level (2);
8090 p->stack = constructor_stack;
8091 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8092 p->index = p->range_start;
8095 if (!finish)
8096 constructor_range_stack = range_stack;
8097 continue;
8100 break;
8103 constructor_range_stack = 0;
8106 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8107 (guaranteed to be 'volatile' or null) and ARGS (represented using
8108 an ASM_EXPR node). */
8109 tree
8110 build_asm_stmt (tree cv_qualifier, tree args)
8112 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8113 ASM_VOLATILE_P (args) = 1;
8114 return add_stmt (args);
8117 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8118 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8119 SIMPLE indicates whether there was anything at all after the
8120 string in the asm expression -- asm("blah") and asm("blah" : )
8121 are subtly different. We use a ASM_EXPR node to represent this. */
8122 tree
8123 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8124 tree clobbers, tree labels, bool simple)
8126 tree tail;
8127 tree args;
8128 int i;
8129 const char *constraint;
8130 const char **oconstraints;
8131 bool allows_mem, allows_reg, is_inout;
8132 int ninputs, noutputs;
8134 ninputs = list_length (inputs);
8135 noutputs = list_length (outputs);
8136 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8138 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8140 /* Remove output conversions that change the type but not the mode. */
8141 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8143 tree output = TREE_VALUE (tail);
8145 /* ??? Really, this should not be here. Users should be using a
8146 proper lvalue, dammit. But there's a long history of using casts
8147 in the output operands. In cases like longlong.h, this becomes a
8148 primitive form of typechecking -- if the cast can be removed, then
8149 the output operand had a type of the proper width; otherwise we'll
8150 get an error. Gross, but ... */
8151 STRIP_NOPS (output);
8153 if (!lvalue_or_else (output, lv_asm))
8154 output = error_mark_node;
8156 if (output != error_mark_node
8157 && (TREE_READONLY (output)
8158 || TYPE_READONLY (TREE_TYPE (output))
8159 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8160 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8161 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8162 readonly_error (output, lv_asm);
8164 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8165 oconstraints[i] = constraint;
8167 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8168 &allows_mem, &allows_reg, &is_inout))
8170 /* If the operand is going to end up in memory,
8171 mark it addressable. */
8172 if (!allows_reg && !c_mark_addressable (output))
8173 output = error_mark_node;
8175 else
8176 output = error_mark_node;
8178 TREE_VALUE (tail) = output;
8181 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8183 tree input;
8185 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8186 input = TREE_VALUE (tail);
8188 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8189 oconstraints, &allows_mem, &allows_reg))
8191 /* If the operand is going to end up in memory,
8192 mark it addressable. */
8193 if (!allows_reg && allows_mem)
8195 /* Strip the nops as we allow this case. FIXME, this really
8196 should be rejected or made deprecated. */
8197 STRIP_NOPS (input);
8198 if (!c_mark_addressable (input))
8199 input = error_mark_node;
8202 else
8203 input = error_mark_node;
8205 TREE_VALUE (tail) = input;
8208 /* ASMs with labels cannot have outputs. This should have been
8209 enforced by the parser. */
8210 gcc_assert (outputs == NULL || labels == NULL);
8212 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8214 /* asm statements without outputs, including simple ones, are treated
8215 as volatile. */
8216 ASM_INPUT_P (args) = simple;
8217 ASM_VOLATILE_P (args) = (noutputs == 0);
8219 return args;
8222 /* Generate a goto statement to LABEL. LOC is the location of the
8223 GOTO. */
8225 tree
8226 c_finish_goto_label (location_t loc, tree label)
8228 tree decl = lookup_label_for_goto (loc, label);
8229 if (!decl)
8230 return NULL_TREE;
8231 TREE_USED (decl) = 1;
8233 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8234 SET_EXPR_LOCATION (t, loc);
8235 return add_stmt (t);
8239 /* Generate a computed goto statement to EXPR. LOC is the location of
8240 the GOTO. */
8242 tree
8243 c_finish_goto_ptr (location_t loc, tree expr)
8245 tree t;
8246 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8247 expr = c_fully_fold (expr, false, NULL);
8248 expr = convert (ptr_type_node, expr);
8249 t = build1 (GOTO_EXPR, void_type_node, expr);
8250 SET_EXPR_LOCATION (t, loc);
8251 return add_stmt (t);
8254 /* Generate a C `return' statement. RETVAL is the expression for what
8255 to return, or a null pointer for `return;' with no value. LOC is
8256 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8257 is the original type of RETVAL. */
8259 tree
8260 c_finish_return (location_t loc, tree retval, tree origtype)
8262 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8263 bool no_warning = false;
8264 bool npc = false;
8266 if (TREE_THIS_VOLATILE (current_function_decl))
8267 warning_at (loc, 0,
8268 "function declared %<noreturn%> has a %<return%> statement");
8270 if (retval)
8272 tree semantic_type = NULL_TREE;
8273 npc = null_pointer_constant_p (retval);
8274 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8276 semantic_type = TREE_TYPE (retval);
8277 retval = TREE_OPERAND (retval, 0);
8279 retval = c_fully_fold (retval, false, NULL);
8280 if (semantic_type)
8281 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8284 if (!retval)
8286 current_function_returns_null = 1;
8287 if ((warn_return_type || flag_isoc99)
8288 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8290 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8291 "%<return%> with no value, in "
8292 "function returning non-void");
8293 no_warning = true;
8296 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8298 current_function_returns_null = 1;
8299 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8300 pedwarn (loc, 0,
8301 "%<return%> with a value, in function returning void");
8302 else
8303 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8304 "%<return%> with expression, in function returning void");
8306 else
8308 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8309 ic_return,
8310 npc, NULL_TREE, NULL_TREE, 0);
8311 tree res = DECL_RESULT (current_function_decl);
8312 tree inner;
8314 current_function_returns_value = 1;
8315 if (t == error_mark_node)
8316 return NULL_TREE;
8318 inner = t = convert (TREE_TYPE (res), t);
8320 /* Strip any conversions, additions, and subtractions, and see if
8321 we are returning the address of a local variable. Warn if so. */
8322 while (1)
8324 switch (TREE_CODE (inner))
8326 CASE_CONVERT:
8327 case NON_LVALUE_EXPR:
8328 case PLUS_EXPR:
8329 case POINTER_PLUS_EXPR:
8330 inner = TREE_OPERAND (inner, 0);
8331 continue;
8333 case MINUS_EXPR:
8334 /* If the second operand of the MINUS_EXPR has a pointer
8335 type (or is converted from it), this may be valid, so
8336 don't give a warning. */
8338 tree op1 = TREE_OPERAND (inner, 1);
8340 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8341 && (CONVERT_EXPR_P (op1)
8342 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8343 op1 = TREE_OPERAND (op1, 0);
8345 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8346 break;
8348 inner = TREE_OPERAND (inner, 0);
8349 continue;
8352 case ADDR_EXPR:
8353 inner = TREE_OPERAND (inner, 0);
8355 while (REFERENCE_CLASS_P (inner)
8356 && TREE_CODE (inner) != INDIRECT_REF)
8357 inner = TREE_OPERAND (inner, 0);
8359 if (DECL_P (inner)
8360 && !DECL_EXTERNAL (inner)
8361 && !TREE_STATIC (inner)
8362 && DECL_CONTEXT (inner) == current_function_decl)
8363 warning_at (loc,
8364 0, "function returns address of local variable");
8365 break;
8367 default:
8368 break;
8371 break;
8374 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8375 SET_EXPR_LOCATION (retval, loc);
8377 if (warn_sequence_point)
8378 verify_sequence_points (retval);
8381 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8382 TREE_NO_WARNING (ret_stmt) |= no_warning;
8383 return add_stmt (ret_stmt);
8386 struct c_switch {
8387 /* The SWITCH_EXPR being built. */
8388 tree switch_expr;
8390 /* The original type of the testing expression, i.e. before the
8391 default conversion is applied. */
8392 tree orig_type;
8394 /* A splay-tree mapping the low element of a case range to the high
8395 element, or NULL_TREE if there is no high element. Used to
8396 determine whether or not a new case label duplicates an old case
8397 label. We need a tree, rather than simply a hash table, because
8398 of the GNU case range extension. */
8399 splay_tree cases;
8401 /* The bindings at the point of the switch. This is used for
8402 warnings crossing decls when branching to a case label. */
8403 struct c_spot_bindings *bindings;
8405 /* The next node on the stack. */
8406 struct c_switch *next;
8409 /* A stack of the currently active switch statements. The innermost
8410 switch statement is on the top of the stack. There is no need to
8411 mark the stack for garbage collection because it is only active
8412 during the processing of the body of a function, and we never
8413 collect at that point. */
8415 struct c_switch *c_switch_stack;
8417 /* Start a C switch statement, testing expression EXP. Return the new
8418 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8419 SWITCH_COND_LOC is the location of the switch's condition. */
8421 tree
8422 c_start_case (location_t switch_loc,
8423 location_t switch_cond_loc,
8424 tree exp)
8426 tree orig_type = error_mark_node;
8427 struct c_switch *cs;
8429 if (exp != error_mark_node)
8431 orig_type = TREE_TYPE (exp);
8433 if (!INTEGRAL_TYPE_P (orig_type))
8435 if (orig_type != error_mark_node)
8437 error_at (switch_cond_loc, "switch quantity not an integer");
8438 orig_type = error_mark_node;
8440 exp = integer_zero_node;
8442 else
8444 tree type = TYPE_MAIN_VARIANT (orig_type);
8446 if (!in_system_header
8447 && (type == long_integer_type_node
8448 || type == long_unsigned_type_node))
8449 warning_at (switch_cond_loc,
8450 OPT_Wtraditional, "%<long%> switch expression not "
8451 "converted to %<int%> in ISO C");
8453 exp = c_fully_fold (exp, false, NULL);
8454 exp = default_conversion (exp);
8456 if (warn_sequence_point)
8457 verify_sequence_points (exp);
8461 /* Add this new SWITCH_EXPR to the stack. */
8462 cs = XNEW (struct c_switch);
8463 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8464 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8465 cs->orig_type = orig_type;
8466 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8467 cs->bindings = c_get_switch_bindings ();
8468 cs->next = c_switch_stack;
8469 c_switch_stack = cs;
8471 return add_stmt (cs->switch_expr);
8474 /* Process a case label at location LOC. */
8476 tree
8477 do_case (location_t loc, tree low_value, tree high_value)
8479 tree label = NULL_TREE;
8481 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8483 low_value = c_fully_fold (low_value, false, NULL);
8484 if (TREE_CODE (low_value) == INTEGER_CST)
8485 pedwarn (input_location, OPT_pedantic,
8486 "case label is not an integer constant expression");
8489 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8491 high_value = c_fully_fold (high_value, false, NULL);
8492 if (TREE_CODE (high_value) == INTEGER_CST)
8493 pedwarn (input_location, OPT_pedantic,
8494 "case label is not an integer constant expression");
8497 if (c_switch_stack == NULL)
8499 if (low_value)
8500 error_at (loc, "case label not within a switch statement");
8501 else
8502 error_at (loc, "%<default%> label not within a switch statement");
8503 return NULL_TREE;
8506 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8507 EXPR_LOCATION (c_switch_stack->switch_expr),
8508 loc))
8509 return NULL_TREE;
8511 label = c_add_case_label (loc, c_switch_stack->cases,
8512 SWITCH_COND (c_switch_stack->switch_expr),
8513 c_switch_stack->orig_type,
8514 low_value, high_value);
8515 if (label == error_mark_node)
8516 label = NULL_TREE;
8517 return label;
8520 /* Finish the switch statement. */
8522 void
8523 c_finish_case (tree body)
8525 struct c_switch *cs = c_switch_stack;
8526 location_t switch_location;
8528 SWITCH_BODY (cs->switch_expr) = body;
8530 /* Emit warnings as needed. */
8531 switch_location = EXPR_LOCATION (cs->switch_expr);
8532 c_do_switch_warnings (cs->cases, switch_location,
8533 TREE_TYPE (cs->switch_expr),
8534 SWITCH_COND (cs->switch_expr));
8536 /* Pop the stack. */
8537 c_switch_stack = cs->next;
8538 splay_tree_delete (cs->cases);
8539 c_release_switch_bindings (cs->bindings);
8540 XDELETE (cs);
8543 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8544 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8545 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8546 statement, and was not surrounded with parenthesis. */
8548 void
8549 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8550 tree else_block, bool nested_if)
8552 tree stmt;
8554 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8555 if (warn_parentheses && nested_if && else_block == NULL)
8557 tree inner_if = then_block;
8559 /* We know from the grammar productions that there is an IF nested
8560 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8561 it might not be exactly THEN_BLOCK, but should be the last
8562 non-container statement within. */
8563 while (1)
8564 switch (TREE_CODE (inner_if))
8566 case COND_EXPR:
8567 goto found;
8568 case BIND_EXPR:
8569 inner_if = BIND_EXPR_BODY (inner_if);
8570 break;
8571 case STATEMENT_LIST:
8572 inner_if = expr_last (then_block);
8573 break;
8574 case TRY_FINALLY_EXPR:
8575 case TRY_CATCH_EXPR:
8576 inner_if = TREE_OPERAND (inner_if, 0);
8577 break;
8578 default:
8579 gcc_unreachable ();
8581 found:
8583 if (COND_EXPR_ELSE (inner_if))
8584 warning_at (if_locus, OPT_Wparentheses,
8585 "suggest explicit braces to avoid ambiguous %<else%>");
8588 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8589 SET_EXPR_LOCATION (stmt, if_locus);
8590 add_stmt (stmt);
8593 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8594 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8595 is false for DO loops. INCR is the FOR increment expression. BODY is
8596 the statement controlled by the loop. BLAB is the break label. CLAB is
8597 the continue label. Everything is allowed to be NULL. */
8599 void
8600 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8601 tree blab, tree clab, bool cond_is_first)
8603 tree entry = NULL, exit = NULL, t;
8605 /* If the condition is zero don't generate a loop construct. */
8606 if (cond && integer_zerop (cond))
8608 if (cond_is_first)
8610 t = build_and_jump (&blab);
8611 SET_EXPR_LOCATION (t, start_locus);
8612 add_stmt (t);
8615 else
8617 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8619 /* If we have an exit condition, then we build an IF with gotos either
8620 out of the loop, or to the top of it. If there's no exit condition,
8621 then we just build a jump back to the top. */
8622 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8624 if (cond && !integer_nonzerop (cond))
8626 /* Canonicalize the loop condition to the end. This means
8627 generating a branch to the loop condition. Reuse the
8628 continue label, if possible. */
8629 if (cond_is_first)
8631 if (incr || !clab)
8633 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8634 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8636 else
8637 t = build1 (GOTO_EXPR, void_type_node, clab);
8638 SET_EXPR_LOCATION (t, start_locus);
8639 add_stmt (t);
8642 t = build_and_jump (&blab);
8643 if (cond_is_first)
8644 exit = fold_build3_loc (start_locus,
8645 COND_EXPR, void_type_node, cond, exit, t);
8646 else
8647 exit = fold_build3_loc (input_location,
8648 COND_EXPR, void_type_node, cond, exit, t);
8651 add_stmt (top);
8654 if (body)
8655 add_stmt (body);
8656 if (clab)
8657 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8658 if (incr)
8659 add_stmt (incr);
8660 if (entry)
8661 add_stmt (entry);
8662 if (exit)
8663 add_stmt (exit);
8664 if (blab)
8665 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8668 tree
8669 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
8671 bool skip;
8672 tree label = *label_p;
8674 /* In switch statements break is sometimes stylistically used after
8675 a return statement. This can lead to spurious warnings about
8676 control reaching the end of a non-void function when it is
8677 inlined. Note that we are calling block_may_fallthru with
8678 language specific tree nodes; this works because
8679 block_may_fallthru returns true when given something it does not
8680 understand. */
8681 skip = !block_may_fallthru (cur_stmt_list);
8683 if (!label)
8685 if (!skip)
8686 *label_p = label = create_artificial_label (loc);
8688 else if (TREE_CODE (label) == LABEL_DECL)
8690 else switch (TREE_INT_CST_LOW (label))
8692 case 0:
8693 if (is_break)
8694 error_at (loc, "break statement not within loop or switch");
8695 else
8696 error_at (loc, "continue statement not within a loop");
8697 return NULL_TREE;
8699 case 1:
8700 gcc_assert (is_break);
8701 error_at (loc, "break statement used with OpenMP for loop");
8702 return NULL_TREE;
8704 default:
8705 gcc_unreachable ();
8708 if (skip)
8709 return NULL_TREE;
8711 if (!is_break)
8712 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
8714 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
8717 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8719 static void
8720 emit_side_effect_warnings (location_t loc, tree expr)
8722 if (expr == error_mark_node)
8724 else if (!TREE_SIDE_EFFECTS (expr))
8726 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
8727 warning_at (loc, OPT_Wunused_value, "statement with no effect");
8729 else
8730 warn_if_unused_value (expr, loc);
8733 /* Process an expression as if it were a complete statement. Emit
8734 diagnostics, but do not call ADD_STMT. LOC is the location of the
8735 statement. */
8737 tree
8738 c_process_expr_stmt (location_t loc, tree expr)
8740 if (!expr)
8741 return NULL_TREE;
8743 expr = c_fully_fold (expr, false, NULL);
8745 if (warn_sequence_point)
8746 verify_sequence_points (expr);
8748 if (TREE_TYPE (expr) != error_mark_node
8749 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
8750 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
8751 error_at (loc, "expression statement has incomplete type");
8753 /* If we're not processing a statement expression, warn about unused values.
8754 Warnings for statement expressions will be emitted later, once we figure
8755 out which is the result. */
8756 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8757 && warn_unused_value)
8758 emit_side_effect_warnings (loc, expr);
8760 /* If the expression is not of a type to which we cannot assign a line
8761 number, wrap the thing in a no-op NOP_EXPR. */
8762 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
8764 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
8765 SET_EXPR_LOCATION (expr, loc);
8768 return expr;
8771 /* Emit an expression as a statement. LOC is the location of the
8772 expression. */
8774 tree
8775 c_finish_expr_stmt (location_t loc, tree expr)
8777 if (expr)
8778 return add_stmt (c_process_expr_stmt (loc, expr));
8779 else
8780 return NULL;
8783 /* Do the opposite and emit a statement as an expression. To begin,
8784 create a new binding level and return it. */
8786 tree
8787 c_begin_stmt_expr (void)
8789 tree ret;
8791 /* We must force a BLOCK for this level so that, if it is not expanded
8792 later, there is a way to turn off the entire subtree of blocks that
8793 are contained in it. */
8794 keep_next_level ();
8795 ret = c_begin_compound_stmt (true);
8797 c_bindings_start_stmt_expr (c_switch_stack == NULL
8798 ? NULL
8799 : c_switch_stack->bindings);
8801 /* Mark the current statement list as belonging to a statement list. */
8802 STATEMENT_LIST_STMT_EXPR (ret) = 1;
8804 return ret;
8807 /* LOC is the location of the compound statement to which this body
8808 belongs. */
8810 tree
8811 c_finish_stmt_expr (location_t loc, tree body)
8813 tree last, type, tmp, val;
8814 tree *last_p;
8816 body = c_end_compound_stmt (loc, body, true);
8818 c_bindings_end_stmt_expr (c_switch_stack == NULL
8819 ? NULL
8820 : c_switch_stack->bindings);
8822 /* Locate the last statement in BODY. See c_end_compound_stmt
8823 about always returning a BIND_EXPR. */
8824 last_p = &BIND_EXPR_BODY (body);
8825 last = BIND_EXPR_BODY (body);
8827 continue_searching:
8828 if (TREE_CODE (last) == STATEMENT_LIST)
8830 tree_stmt_iterator i;
8832 /* This can happen with degenerate cases like ({ }). No value. */
8833 if (!TREE_SIDE_EFFECTS (last))
8834 return body;
8836 /* If we're supposed to generate side effects warnings, process
8837 all of the statements except the last. */
8838 if (warn_unused_value)
8840 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
8842 location_t tloc;
8843 tree t = tsi_stmt (i);
8845 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
8846 emit_side_effect_warnings (tloc, t);
8849 else
8850 i = tsi_last (last);
8851 last_p = tsi_stmt_ptr (i);
8852 last = *last_p;
8855 /* If the end of the list is exception related, then the list was split
8856 by a call to push_cleanup. Continue searching. */
8857 if (TREE_CODE (last) == TRY_FINALLY_EXPR
8858 || TREE_CODE (last) == TRY_CATCH_EXPR)
8860 last_p = &TREE_OPERAND (last, 0);
8861 last = *last_p;
8862 goto continue_searching;
8865 if (last == error_mark_node)
8866 return last;
8868 /* In the case that the BIND_EXPR is not necessary, return the
8869 expression out from inside it. */
8870 if (last == BIND_EXPR_BODY (body)
8871 && BIND_EXPR_VARS (body) == NULL)
8873 /* Even if this looks constant, do not allow it in a constant
8874 expression. */
8875 last = c_wrap_maybe_const (last, true);
8876 /* Do not warn if the return value of a statement expression is
8877 unused. */
8878 TREE_NO_WARNING (last) = 1;
8879 return last;
8882 /* Extract the type of said expression. */
8883 type = TREE_TYPE (last);
8885 /* If we're not returning a value at all, then the BIND_EXPR that
8886 we already have is a fine expression to return. */
8887 if (!type || VOID_TYPE_P (type))
8888 return body;
8890 /* Now that we've located the expression containing the value, it seems
8891 silly to make voidify_wrapper_expr repeat the process. Create a
8892 temporary of the appropriate type and stick it in a TARGET_EXPR. */
8893 tmp = create_tmp_var_raw (type, NULL);
8895 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
8896 tree_expr_nonnegative_p giving up immediately. */
8897 val = last;
8898 if (TREE_CODE (val) == NOP_EXPR
8899 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
8900 val = TREE_OPERAND (val, 0);
8902 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
8903 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
8906 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
8907 SET_EXPR_LOCATION (t, loc);
8908 return t;
8912 /* Begin and end compound statements. This is as simple as pushing
8913 and popping new statement lists from the tree. */
8915 tree
8916 c_begin_compound_stmt (bool do_scope)
8918 tree stmt = push_stmt_list ();
8919 if (do_scope)
8920 push_scope ();
8921 return stmt;
8924 /* End a compound statement. STMT is the statement. LOC is the
8925 location of the compound statement-- this is usually the location
8926 of the opening brace. */
8928 tree
8929 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
8931 tree block = NULL;
8933 if (do_scope)
8935 if (c_dialect_objc ())
8936 objc_clear_super_receiver ();
8937 block = pop_scope ();
8940 stmt = pop_stmt_list (stmt);
8941 stmt = c_build_bind_expr (loc, block, stmt);
8943 /* If this compound statement is nested immediately inside a statement
8944 expression, then force a BIND_EXPR to be created. Otherwise we'll
8945 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
8946 STATEMENT_LISTs merge, and thus we can lose track of what statement
8947 was really last. */
8948 if (cur_stmt_list
8949 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8950 && TREE_CODE (stmt) != BIND_EXPR)
8952 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
8953 TREE_SIDE_EFFECTS (stmt) = 1;
8954 SET_EXPR_LOCATION (stmt, loc);
8957 return stmt;
8960 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
8961 when the current scope is exited. EH_ONLY is true when this is not
8962 meant to apply to normal control flow transfer. */
8964 void
8965 push_cleanup (tree decl, tree cleanup, bool eh_only)
8967 enum tree_code code;
8968 tree stmt, list;
8969 bool stmt_expr;
8971 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
8972 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
8973 add_stmt (stmt);
8974 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
8975 list = push_stmt_list ();
8976 TREE_OPERAND (stmt, 0) = list;
8977 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
8980 /* Build a binary-operation expression without default conversions.
8981 CODE is the kind of expression to build.
8982 LOCATION is the operator's location.
8983 This function differs from `build' in several ways:
8984 the data type of the result is computed and recorded in it,
8985 warnings are generated if arg data types are invalid,
8986 special handling for addition and subtraction of pointers is known,
8987 and some optimization is done (operations on narrow ints
8988 are done in the narrower type when that gives the same result).
8989 Constant folding is also done before the result is returned.
8991 Note that the operands will never have enumeral types, or function
8992 or array types, because either they will have the default conversions
8993 performed or they have both just been converted to some other type in which
8994 the arithmetic is to be done. */
8996 tree
8997 build_binary_op (location_t location, enum tree_code code,
8998 tree orig_op0, tree orig_op1, int convert_p)
9000 tree type0, type1, orig_type0, orig_type1;
9001 tree eptype;
9002 enum tree_code code0, code1;
9003 tree op0, op1;
9004 tree ret = error_mark_node;
9005 const char *invalid_op_diag;
9006 bool op0_int_operands, op1_int_operands;
9007 bool int_const, int_const_or_overflow, int_operands;
9009 /* Expression code to give to the expression when it is built.
9010 Normally this is CODE, which is what the caller asked for,
9011 but in some special cases we change it. */
9012 enum tree_code resultcode = code;
9014 /* Data type in which the computation is to be performed.
9015 In the simplest cases this is the common type of the arguments. */
9016 tree result_type = NULL;
9018 /* When the computation is in excess precision, the type of the
9019 final EXCESS_PRECISION_EXPR. */
9020 tree real_result_type = NULL;
9022 /* Nonzero means operands have already been type-converted
9023 in whatever way is necessary.
9024 Zero means they need to be converted to RESULT_TYPE. */
9025 int converted = 0;
9027 /* Nonzero means create the expression with this type, rather than
9028 RESULT_TYPE. */
9029 tree build_type = 0;
9031 /* Nonzero means after finally constructing the expression
9032 convert it to this type. */
9033 tree final_type = 0;
9035 /* Nonzero if this is an operation like MIN or MAX which can
9036 safely be computed in short if both args are promoted shorts.
9037 Also implies COMMON.
9038 -1 indicates a bitwise operation; this makes a difference
9039 in the exact conditions for when it is safe to do the operation
9040 in a narrower mode. */
9041 int shorten = 0;
9043 /* Nonzero if this is a comparison operation;
9044 if both args are promoted shorts, compare the original shorts.
9045 Also implies COMMON. */
9046 int short_compare = 0;
9048 /* Nonzero if this is a right-shift operation, which can be computed on the
9049 original short and then promoted if the operand is a promoted short. */
9050 int short_shift = 0;
9052 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9053 int common = 0;
9055 /* True means types are compatible as far as ObjC is concerned. */
9056 bool objc_ok;
9058 /* True means this is an arithmetic operation that may need excess
9059 precision. */
9060 bool may_need_excess_precision;
9062 if (location == UNKNOWN_LOCATION)
9063 location = input_location;
9065 op0 = orig_op0;
9066 op1 = orig_op1;
9068 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9069 if (op0_int_operands)
9070 op0 = remove_c_maybe_const_expr (op0);
9071 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9072 if (op1_int_operands)
9073 op1 = remove_c_maybe_const_expr (op1);
9074 int_operands = (op0_int_operands && op1_int_operands);
9075 if (int_operands)
9077 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9078 && TREE_CODE (orig_op1) == INTEGER_CST);
9079 int_const = (int_const_or_overflow
9080 && !TREE_OVERFLOW (orig_op0)
9081 && !TREE_OVERFLOW (orig_op1));
9083 else
9084 int_const = int_const_or_overflow = false;
9086 if (convert_p)
9088 op0 = default_conversion (op0);
9089 op1 = default_conversion (op1);
9092 orig_type0 = type0 = TREE_TYPE (op0);
9093 orig_type1 = type1 = TREE_TYPE (op1);
9095 /* The expression codes of the data types of the arguments tell us
9096 whether the arguments are integers, floating, pointers, etc. */
9097 code0 = TREE_CODE (type0);
9098 code1 = TREE_CODE (type1);
9100 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9101 STRIP_TYPE_NOPS (op0);
9102 STRIP_TYPE_NOPS (op1);
9104 /* If an error was already reported for one of the arguments,
9105 avoid reporting another error. */
9107 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9108 return error_mark_node;
9110 if ((invalid_op_diag
9111 = targetm.invalid_binary_op (code, type0, type1)))
9113 error_at (location, invalid_op_diag);
9114 return error_mark_node;
9117 switch (code)
9119 case PLUS_EXPR:
9120 case MINUS_EXPR:
9121 case MULT_EXPR:
9122 case TRUNC_DIV_EXPR:
9123 case CEIL_DIV_EXPR:
9124 case FLOOR_DIV_EXPR:
9125 case ROUND_DIV_EXPR:
9126 case EXACT_DIV_EXPR:
9127 may_need_excess_precision = true;
9128 break;
9129 default:
9130 may_need_excess_precision = false;
9131 break;
9133 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9135 op0 = TREE_OPERAND (op0, 0);
9136 type0 = TREE_TYPE (op0);
9138 else if (may_need_excess_precision
9139 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9141 type0 = eptype;
9142 op0 = convert (eptype, op0);
9144 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9146 op1 = TREE_OPERAND (op1, 0);
9147 type1 = TREE_TYPE (op1);
9149 else if (may_need_excess_precision
9150 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9152 type1 = eptype;
9153 op1 = convert (eptype, op1);
9156 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9158 switch (code)
9160 case PLUS_EXPR:
9161 /* Handle the pointer + int case. */
9162 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9164 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9165 goto return_build_binary_op;
9167 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9169 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9170 goto return_build_binary_op;
9172 else
9173 common = 1;
9174 break;
9176 case MINUS_EXPR:
9177 /* Subtraction of two similar pointers.
9178 We must subtract them as integers, then divide by object size. */
9179 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9180 && comp_target_types (location, type0, type1))
9182 ret = pointer_diff (location, op0, op1);
9183 goto return_build_binary_op;
9185 /* Handle pointer minus int. Just like pointer plus int. */
9186 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9188 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9189 goto return_build_binary_op;
9191 else
9192 common = 1;
9193 break;
9195 case MULT_EXPR:
9196 common = 1;
9197 break;
9199 case TRUNC_DIV_EXPR:
9200 case CEIL_DIV_EXPR:
9201 case FLOOR_DIV_EXPR:
9202 case ROUND_DIV_EXPR:
9203 case EXACT_DIV_EXPR:
9204 warn_for_div_by_zero (location, op1);
9206 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9207 || code0 == FIXED_POINT_TYPE
9208 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9209 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9210 || code1 == FIXED_POINT_TYPE
9211 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9213 enum tree_code tcode0 = code0, tcode1 = code1;
9215 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9216 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9217 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9218 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9220 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9221 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9222 resultcode = RDIV_EXPR;
9223 else
9224 /* Although it would be tempting to shorten always here, that
9225 loses on some targets, since the modulo instruction is
9226 undefined if the quotient can't be represented in the
9227 computation mode. We shorten only if unsigned or if
9228 dividing by something we know != -1. */
9229 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9230 || (TREE_CODE (op1) == INTEGER_CST
9231 && !integer_all_onesp (op1)));
9232 common = 1;
9234 break;
9236 case BIT_AND_EXPR:
9237 case BIT_IOR_EXPR:
9238 case BIT_XOR_EXPR:
9239 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9240 shorten = -1;
9241 /* Allow vector types which are not floating point types. */
9242 else if (code0 == VECTOR_TYPE
9243 && code1 == VECTOR_TYPE
9244 && !VECTOR_FLOAT_TYPE_P (type0)
9245 && !VECTOR_FLOAT_TYPE_P (type1))
9246 common = 1;
9247 break;
9249 case TRUNC_MOD_EXPR:
9250 case FLOOR_MOD_EXPR:
9251 warn_for_div_by_zero (location, op1);
9253 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9254 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9255 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9256 common = 1;
9257 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9259 /* Although it would be tempting to shorten always here, that loses
9260 on some targets, since the modulo instruction is undefined if the
9261 quotient can't be represented in the computation mode. We shorten
9262 only if unsigned or if dividing by something we know != -1. */
9263 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9264 || (TREE_CODE (op1) == INTEGER_CST
9265 && !integer_all_onesp (op1)));
9266 common = 1;
9268 break;
9270 case TRUTH_ANDIF_EXPR:
9271 case TRUTH_ORIF_EXPR:
9272 case TRUTH_AND_EXPR:
9273 case TRUTH_OR_EXPR:
9274 case TRUTH_XOR_EXPR:
9275 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9276 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9277 || code0 == FIXED_POINT_TYPE)
9278 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9279 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9280 || code1 == FIXED_POINT_TYPE))
9282 /* Result of these operations is always an int,
9283 but that does not mean the operands should be
9284 converted to ints! */
9285 result_type = integer_type_node;
9286 op0 = c_common_truthvalue_conversion (location, op0);
9287 op1 = c_common_truthvalue_conversion (location, op1);
9288 converted = 1;
9290 if (code == TRUTH_ANDIF_EXPR)
9292 int_const_or_overflow = (int_operands
9293 && TREE_CODE (orig_op0) == INTEGER_CST
9294 && (op0 == truthvalue_false_node
9295 || TREE_CODE (orig_op1) == INTEGER_CST));
9296 int_const = (int_const_or_overflow
9297 && !TREE_OVERFLOW (orig_op0)
9298 && (op0 == truthvalue_false_node
9299 || !TREE_OVERFLOW (orig_op1)));
9301 else if (code == TRUTH_ORIF_EXPR)
9303 int_const_or_overflow = (int_operands
9304 && TREE_CODE (orig_op0) == INTEGER_CST
9305 && (op0 == truthvalue_true_node
9306 || TREE_CODE (orig_op1) == INTEGER_CST));
9307 int_const = (int_const_or_overflow
9308 && !TREE_OVERFLOW (orig_op0)
9309 && (op0 == truthvalue_true_node
9310 || !TREE_OVERFLOW (orig_op1)));
9312 break;
9314 /* Shift operations: result has same type as first operand;
9315 always convert second operand to int.
9316 Also set SHORT_SHIFT if shifting rightward. */
9318 case RSHIFT_EXPR:
9319 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9320 && code1 == INTEGER_TYPE)
9322 if (TREE_CODE (op1) == INTEGER_CST)
9324 if (tree_int_cst_sgn (op1) < 0)
9326 int_const = false;
9327 if (c_inhibit_evaluation_warnings == 0)
9328 warning (0, "right shift count is negative");
9330 else
9332 if (!integer_zerop (op1))
9333 short_shift = 1;
9335 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9337 int_const = false;
9338 if (c_inhibit_evaluation_warnings == 0)
9339 warning (0, "right shift count >= width of type");
9344 /* Use the type of the value to be shifted. */
9345 result_type = type0;
9346 /* Convert the shift-count to an integer, regardless of size
9347 of value being shifted. */
9348 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9349 op1 = convert (integer_type_node, op1);
9350 /* Avoid converting op1 to result_type later. */
9351 converted = 1;
9353 break;
9355 case LSHIFT_EXPR:
9356 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9357 && code1 == INTEGER_TYPE)
9359 if (TREE_CODE (op1) == INTEGER_CST)
9361 if (tree_int_cst_sgn (op1) < 0)
9363 int_const = false;
9364 if (c_inhibit_evaluation_warnings == 0)
9365 warning (0, "left shift count is negative");
9368 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9370 int_const = false;
9371 if (c_inhibit_evaluation_warnings == 0)
9372 warning (0, "left shift count >= width of type");
9376 /* Use the type of the value to be shifted. */
9377 result_type = type0;
9378 /* Convert the shift-count to an integer, regardless of size
9379 of value being shifted. */
9380 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9381 op1 = convert (integer_type_node, op1);
9382 /* Avoid converting op1 to result_type later. */
9383 converted = 1;
9385 break;
9387 case EQ_EXPR:
9388 case NE_EXPR:
9389 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9390 warning_at (location,
9391 OPT_Wfloat_equal,
9392 "comparing floating point with == or != is unsafe");
9393 /* Result of comparison is always int,
9394 but don't convert the args to int! */
9395 build_type = integer_type_node;
9396 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9397 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9398 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9399 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9400 short_compare = 1;
9401 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9403 tree tt0 = TREE_TYPE (type0);
9404 tree tt1 = TREE_TYPE (type1);
9405 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
9406 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
9407 addr_space_t as_common = ADDR_SPACE_GENERIC;
9409 /* Anything compares with void *. void * compares with anything.
9410 Otherwise, the targets must be compatible
9411 and both must be object or both incomplete. */
9412 if (comp_target_types (location, type0, type1))
9413 result_type = common_pointer_type (type0, type1);
9414 else if (null_pointer_constant_p (orig_op0))
9415 result_type = type1;
9416 else if (null_pointer_constant_p (orig_op1))
9417 result_type = type0;
9418 else if (!addr_space_superset (as0, as1, &as_common))
9420 error_at (location, "comparison of pointers to "
9421 "disjoint address spaces");
9422 return error_mark_node;
9424 else if (VOID_TYPE_P (tt0))
9426 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
9427 pedwarn (location, OPT_pedantic, "ISO C forbids "
9428 "comparison of %<void *%> with function pointer");
9430 else if (VOID_TYPE_P (tt1))
9432 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
9433 pedwarn (location, OPT_pedantic, "ISO C forbids "
9434 "comparison of %<void *%> with function pointer");
9436 else
9437 /* Avoid warning about the volatile ObjC EH puts on decls. */
9438 if (!objc_ok)
9439 pedwarn (location, 0,
9440 "comparison of distinct pointer types lacks a cast");
9442 if (result_type == NULL_TREE)
9444 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9445 result_type = build_pointer_type
9446 (build_qualified_type (void_type_node, qual));
9449 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9451 if (TREE_CODE (op0) == ADDR_EXPR
9452 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9453 warning_at (location,
9454 OPT_Waddress, "the address of %qD will never be NULL",
9455 TREE_OPERAND (op0, 0));
9456 result_type = type0;
9458 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9460 if (TREE_CODE (op1) == ADDR_EXPR
9461 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9462 warning_at (location,
9463 OPT_Waddress, "the address of %qD will never be NULL",
9464 TREE_OPERAND (op1, 0));
9465 result_type = type1;
9467 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9469 result_type = type0;
9470 pedwarn (location, 0, "comparison between pointer and integer");
9472 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9474 result_type = type1;
9475 pedwarn (location, 0, "comparison between pointer and integer");
9477 break;
9479 case LE_EXPR:
9480 case GE_EXPR:
9481 case LT_EXPR:
9482 case GT_EXPR:
9483 build_type = integer_type_node;
9484 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9485 || code0 == FIXED_POINT_TYPE)
9486 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9487 || code1 == FIXED_POINT_TYPE))
9488 short_compare = 1;
9489 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9491 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
9492 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
9493 addr_space_t as_common;
9495 if (comp_target_types (location, type0, type1))
9497 result_type = common_pointer_type (type0, type1);
9498 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9499 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9500 pedwarn (location, 0,
9501 "comparison of complete and incomplete pointers");
9502 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9503 pedwarn (location, OPT_pedantic, "ISO C forbids "
9504 "ordered comparisons of pointers to functions");
9506 else if (!addr_space_superset (as0, as1, &as_common))
9508 error_at (location, "comparison of pointers to "
9509 "disjoint address spaces");
9510 return error_mark_node;
9512 else
9514 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9515 result_type = build_pointer_type
9516 (build_qualified_type (void_type_node, qual));
9517 pedwarn (location, 0,
9518 "comparison of distinct pointer types lacks a cast");
9521 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9523 result_type = type0;
9524 if (pedantic)
9525 pedwarn (location, OPT_pedantic,
9526 "ordered comparison of pointer with integer zero");
9527 else if (extra_warnings)
9528 warning_at (location, OPT_Wextra,
9529 "ordered comparison of pointer with integer zero");
9531 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9533 result_type = type1;
9534 pedwarn (location, OPT_pedantic,
9535 "ordered comparison of pointer with integer zero");
9537 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9539 result_type = type0;
9540 pedwarn (location, 0, "comparison between pointer and integer");
9542 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9544 result_type = type1;
9545 pedwarn (location, 0, "comparison between pointer and integer");
9547 break;
9549 default:
9550 gcc_unreachable ();
9553 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9554 return error_mark_node;
9556 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9557 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9558 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9559 TREE_TYPE (type1))))
9561 binary_op_error (location, code, type0, type1);
9562 return error_mark_node;
9565 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9566 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9568 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9569 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9571 bool first_complex = (code0 == COMPLEX_TYPE);
9572 bool second_complex = (code1 == COMPLEX_TYPE);
9573 int none_complex = (!first_complex && !second_complex);
9575 if (shorten || common || short_compare)
9577 result_type = c_common_type (type0, type1);
9578 if (result_type == error_mark_node)
9579 return error_mark_node;
9582 if (first_complex != second_complex
9583 && (code == PLUS_EXPR
9584 || code == MINUS_EXPR
9585 || code == MULT_EXPR
9586 || (code == TRUNC_DIV_EXPR && first_complex))
9587 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
9588 && flag_signed_zeros)
9590 /* An operation on mixed real/complex operands must be
9591 handled specially, but the language-independent code can
9592 more easily optimize the plain complex arithmetic if
9593 -fno-signed-zeros. */
9594 tree real_type = TREE_TYPE (result_type);
9595 tree real, imag;
9596 if (type0 != orig_type0 || type1 != orig_type1)
9598 gcc_assert (may_need_excess_precision && common);
9599 real_result_type = c_common_type (orig_type0, orig_type1);
9601 if (first_complex)
9603 if (TREE_TYPE (op0) != result_type)
9604 op0 = convert_and_check (result_type, op0);
9605 if (TREE_TYPE (op1) != real_type)
9606 op1 = convert_and_check (real_type, op1);
9608 else
9610 if (TREE_TYPE (op0) != real_type)
9611 op0 = convert_and_check (real_type, op0);
9612 if (TREE_TYPE (op1) != result_type)
9613 op1 = convert_and_check (result_type, op1);
9615 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9616 return error_mark_node;
9617 if (first_complex)
9619 op0 = c_save_expr (op0);
9620 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
9621 op0, 1);
9622 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
9623 op0, 1);
9624 switch (code)
9626 case MULT_EXPR:
9627 case TRUNC_DIV_EXPR:
9628 imag = build2 (resultcode, real_type, imag, op1);
9629 /* Fall through. */
9630 case PLUS_EXPR:
9631 case MINUS_EXPR:
9632 real = build2 (resultcode, real_type, real, op1);
9633 break;
9634 default:
9635 gcc_unreachable();
9638 else
9640 op1 = c_save_expr (op1);
9641 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
9642 op1, 1);
9643 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
9644 op1, 1);
9645 switch (code)
9647 case MULT_EXPR:
9648 imag = build2 (resultcode, real_type, op0, imag);
9649 /* Fall through. */
9650 case PLUS_EXPR:
9651 real = build2 (resultcode, real_type, op0, real);
9652 break;
9653 case MINUS_EXPR:
9654 real = build2 (resultcode, real_type, op0, real);
9655 imag = build1 (NEGATE_EXPR, real_type, imag);
9656 break;
9657 default:
9658 gcc_unreachable();
9661 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
9662 goto return_build_binary_op;
9665 /* For certain operations (which identify themselves by shorten != 0)
9666 if both args were extended from the same smaller type,
9667 do the arithmetic in that type and then extend.
9669 shorten !=0 and !=1 indicates a bitwise operation.
9670 For them, this optimization is safe only if
9671 both args are zero-extended or both are sign-extended.
9672 Otherwise, we might change the result.
9673 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9674 but calculated in (unsigned short) it would be (unsigned short)-1. */
9676 if (shorten && none_complex)
9678 final_type = result_type;
9679 result_type = shorten_binary_op (result_type, op0, op1,
9680 shorten == -1);
9683 /* Shifts can be shortened if shifting right. */
9685 if (short_shift)
9687 int unsigned_arg;
9688 tree arg0 = get_narrower (op0, &unsigned_arg);
9690 final_type = result_type;
9692 if (arg0 == op0 && final_type == TREE_TYPE (op0))
9693 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
9695 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9696 && tree_int_cst_sgn (op1) > 0
9697 /* We can shorten only if the shift count is less than the
9698 number of bits in the smaller type size. */
9699 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
9700 /* We cannot drop an unsigned shift after sign-extension. */
9701 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
9703 /* Do an unsigned shift if the operand was zero-extended. */
9704 result_type
9705 = c_common_signed_or_unsigned_type (unsigned_arg,
9706 TREE_TYPE (arg0));
9707 /* Convert value-to-be-shifted to that type. */
9708 if (TREE_TYPE (op0) != result_type)
9709 op0 = convert (result_type, op0);
9710 converted = 1;
9714 /* Comparison operations are shortened too but differently.
9715 They identify themselves by setting short_compare = 1. */
9717 if (short_compare)
9719 /* Don't write &op0, etc., because that would prevent op0
9720 from being kept in a register.
9721 Instead, make copies of the our local variables and
9722 pass the copies by reference, then copy them back afterward. */
9723 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
9724 enum tree_code xresultcode = resultcode;
9725 tree val
9726 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
9728 if (val != 0)
9730 ret = val;
9731 goto return_build_binary_op;
9734 op0 = xop0, op1 = xop1;
9735 converted = 1;
9736 resultcode = xresultcode;
9738 if (c_inhibit_evaluation_warnings == 0)
9740 bool op0_maybe_const = true;
9741 bool op1_maybe_const = true;
9742 tree orig_op0_folded, orig_op1_folded;
9744 if (in_late_binary_op)
9746 orig_op0_folded = orig_op0;
9747 orig_op1_folded = orig_op1;
9749 else
9751 /* Fold for the sake of possible warnings, as in
9752 build_conditional_expr. This requires the
9753 "original" values to be folded, not just op0 and
9754 op1. */
9755 c_inhibit_evaluation_warnings++;
9756 op0 = c_fully_fold (op0, require_constant_value,
9757 &op0_maybe_const);
9758 op1 = c_fully_fold (op1, require_constant_value,
9759 &op1_maybe_const);
9760 c_inhibit_evaluation_warnings--;
9761 orig_op0_folded = c_fully_fold (orig_op0,
9762 require_constant_value,
9763 NULL);
9764 orig_op1_folded = c_fully_fold (orig_op1,
9765 require_constant_value,
9766 NULL);
9769 if (warn_sign_compare)
9770 warn_for_sign_compare (location, orig_op0_folded,
9771 orig_op1_folded, op0, op1,
9772 result_type, resultcode);
9773 if (!in_late_binary_op)
9775 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
9776 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
9777 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
9778 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
9784 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9785 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9786 Then the expression will be built.
9787 It will be given type FINAL_TYPE if that is nonzero;
9788 otherwise, it will be given type RESULT_TYPE. */
9790 if (!result_type)
9792 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
9793 return error_mark_node;
9796 if (!converted)
9798 if (TREE_TYPE (op0) != result_type)
9799 op0 = convert_and_check (result_type, op0);
9800 if (TREE_TYPE (op1) != result_type)
9801 op1 = convert_and_check (result_type, op1);
9803 /* This can happen if one operand has a vector type, and the other
9804 has a different type. */
9805 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9806 return error_mark_node;
9809 if (build_type == NULL_TREE)
9811 build_type = result_type;
9812 if (type0 != orig_type0 || type1 != orig_type1)
9814 gcc_assert (may_need_excess_precision && common);
9815 real_result_type = c_common_type (orig_type0, orig_type1);
9819 /* Treat expressions in initializers specially as they can't trap. */
9820 if (int_const_or_overflow)
9821 ret = (require_constant_value
9822 ? fold_build2_initializer_loc (location, resultcode, build_type,
9823 op0, op1)
9824 : fold_build2_loc (location, resultcode, build_type, op0, op1));
9825 else
9826 ret = build2 (resultcode, build_type, op0, op1);
9827 if (final_type != 0)
9828 ret = convert (final_type, ret);
9830 return_build_binary_op:
9831 gcc_assert (ret != error_mark_node);
9832 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
9833 ret = (int_operands
9834 ? note_integer_operands (ret)
9835 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
9836 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
9837 && !in_late_binary_op)
9838 ret = note_integer_operands (ret);
9839 if (real_result_type)
9840 ret = build1 (EXCESS_PRECISION_EXPR, real_result_type, ret);
9841 protected_set_expr_location (ret, location);
9842 return ret;
9846 /* Convert EXPR to be a truth-value, validating its type for this
9847 purpose. LOCATION is the source location for the expression. */
9849 tree
9850 c_objc_common_truthvalue_conversion (location_t location, tree expr)
9852 bool int_const, int_operands;
9854 switch (TREE_CODE (TREE_TYPE (expr)))
9856 case ARRAY_TYPE:
9857 error_at (location, "used array that cannot be converted to pointer where scalar is required");
9858 return error_mark_node;
9860 case RECORD_TYPE:
9861 error_at (location, "used struct type value where scalar is required");
9862 return error_mark_node;
9864 case UNION_TYPE:
9865 error_at (location, "used union type value where scalar is required");
9866 return error_mark_node;
9868 case FUNCTION_TYPE:
9869 gcc_unreachable ();
9871 default:
9872 break;
9875 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
9876 int_operands = EXPR_INT_CONST_OPERANDS (expr);
9877 if (int_operands)
9878 expr = remove_c_maybe_const_expr (expr);
9880 /* ??? Should we also give an error for void and vectors rather than
9881 leaving those to give errors later? */
9882 expr = c_common_truthvalue_conversion (location, expr);
9884 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
9886 if (TREE_OVERFLOW (expr))
9887 return expr;
9888 else
9889 return note_integer_operands (expr);
9891 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
9892 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9893 return expr;
9897 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
9898 required. */
9900 tree
9901 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
9903 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
9905 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
9906 /* Executing a compound literal inside a function reinitializes
9907 it. */
9908 if (!TREE_STATIC (decl))
9909 *se = true;
9910 return decl;
9912 else
9913 return expr;
9916 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9918 tree
9919 c_begin_omp_parallel (void)
9921 tree block;
9923 keep_next_level ();
9924 block = c_begin_compound_stmt (true);
9926 return block;
9929 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
9930 statement. LOC is the location of the OMP_PARALLEL. */
9932 tree
9933 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
9935 tree stmt;
9937 block = c_end_compound_stmt (loc, block, true);
9939 stmt = make_node (OMP_PARALLEL);
9940 TREE_TYPE (stmt) = void_type_node;
9941 OMP_PARALLEL_CLAUSES (stmt) = clauses;
9942 OMP_PARALLEL_BODY (stmt) = block;
9943 SET_EXPR_LOCATION (stmt, loc);
9945 return add_stmt (stmt);
9948 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
9950 tree
9951 c_begin_omp_task (void)
9953 tree block;
9955 keep_next_level ();
9956 block = c_begin_compound_stmt (true);
9958 return block;
9961 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
9962 statement. LOC is the location of the #pragma. */
9964 tree
9965 c_finish_omp_task (location_t loc, tree clauses, tree block)
9967 tree stmt;
9969 block = c_end_compound_stmt (loc, block, true);
9971 stmt = make_node (OMP_TASK);
9972 TREE_TYPE (stmt) = void_type_node;
9973 OMP_TASK_CLAUSES (stmt) = clauses;
9974 OMP_TASK_BODY (stmt) = block;
9975 SET_EXPR_LOCATION (stmt, loc);
9977 return add_stmt (stmt);
9980 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
9981 Remove any elements from the list that are invalid. */
9983 tree
9984 c_finish_omp_clauses (tree clauses)
9986 bitmap_head generic_head, firstprivate_head, lastprivate_head;
9987 tree c, t, *pc = &clauses;
9988 const char *name;
9990 bitmap_obstack_initialize (NULL);
9991 bitmap_initialize (&generic_head, &bitmap_default_obstack);
9992 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
9993 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
9995 for (pc = &clauses, c = clauses; c ; c = *pc)
9997 bool remove = false;
9998 bool need_complete = false;
9999 bool need_implicitly_determined = false;
10001 switch (OMP_CLAUSE_CODE (c))
10003 case OMP_CLAUSE_SHARED:
10004 name = "shared";
10005 need_implicitly_determined = true;
10006 goto check_dup_generic;
10008 case OMP_CLAUSE_PRIVATE:
10009 name = "private";
10010 need_complete = true;
10011 need_implicitly_determined = true;
10012 goto check_dup_generic;
10014 case OMP_CLAUSE_REDUCTION:
10015 name = "reduction";
10016 need_implicitly_determined = true;
10017 t = OMP_CLAUSE_DECL (c);
10018 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10019 || POINTER_TYPE_P (TREE_TYPE (t)))
10021 error_at (OMP_CLAUSE_LOCATION (c),
10022 "%qE has invalid type for %<reduction%>", t);
10023 remove = true;
10025 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10027 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10028 const char *r_name = NULL;
10030 switch (r_code)
10032 case PLUS_EXPR:
10033 case MULT_EXPR:
10034 case MINUS_EXPR:
10035 break;
10036 case BIT_AND_EXPR:
10037 r_name = "&";
10038 break;
10039 case BIT_XOR_EXPR:
10040 r_name = "^";
10041 break;
10042 case BIT_IOR_EXPR:
10043 r_name = "|";
10044 break;
10045 case TRUTH_ANDIF_EXPR:
10046 r_name = "&&";
10047 break;
10048 case TRUTH_ORIF_EXPR:
10049 r_name = "||";
10050 break;
10051 default:
10052 gcc_unreachable ();
10054 if (r_name)
10056 error_at (OMP_CLAUSE_LOCATION (c),
10057 "%qE has invalid type for %<reduction(%s)%>",
10058 t, r_name);
10059 remove = true;
10062 goto check_dup_generic;
10064 case OMP_CLAUSE_COPYPRIVATE:
10065 name = "copyprivate";
10066 goto check_dup_generic;
10068 case OMP_CLAUSE_COPYIN:
10069 name = "copyin";
10070 t = OMP_CLAUSE_DECL (c);
10071 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10073 error_at (OMP_CLAUSE_LOCATION (c),
10074 "%qE must be %<threadprivate%> for %<copyin%>", t);
10075 remove = true;
10077 goto check_dup_generic;
10079 check_dup_generic:
10080 t = OMP_CLAUSE_DECL (c);
10081 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10083 error_at (OMP_CLAUSE_LOCATION (c),
10084 "%qE is not a variable in clause %qs", t, name);
10085 remove = true;
10087 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10088 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10089 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10091 error_at (OMP_CLAUSE_LOCATION (c),
10092 "%qE appears more than once in data clauses", t);
10093 remove = true;
10095 else
10096 bitmap_set_bit (&generic_head, DECL_UID (t));
10097 break;
10099 case OMP_CLAUSE_FIRSTPRIVATE:
10100 name = "firstprivate";
10101 t = OMP_CLAUSE_DECL (c);
10102 need_complete = true;
10103 need_implicitly_determined = true;
10104 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10106 error_at (OMP_CLAUSE_LOCATION (c),
10107 "%qE is not a variable in clause %<firstprivate%>", t);
10108 remove = true;
10110 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10111 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10113 error_at (OMP_CLAUSE_LOCATION (c),
10114 "%qE appears more than once in data clauses", t);
10115 remove = true;
10117 else
10118 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10119 break;
10121 case OMP_CLAUSE_LASTPRIVATE:
10122 name = "lastprivate";
10123 t = OMP_CLAUSE_DECL (c);
10124 need_complete = true;
10125 need_implicitly_determined = true;
10126 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10128 error_at (OMP_CLAUSE_LOCATION (c),
10129 "%qE is not a variable in clause %<lastprivate%>", t);
10130 remove = true;
10132 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10133 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10135 error_at (OMP_CLAUSE_LOCATION (c),
10136 "%qE appears more than once in data clauses", t);
10137 remove = true;
10139 else
10140 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10141 break;
10143 case OMP_CLAUSE_IF:
10144 case OMP_CLAUSE_NUM_THREADS:
10145 case OMP_CLAUSE_SCHEDULE:
10146 case OMP_CLAUSE_NOWAIT:
10147 case OMP_CLAUSE_ORDERED:
10148 case OMP_CLAUSE_DEFAULT:
10149 case OMP_CLAUSE_UNTIED:
10150 case OMP_CLAUSE_COLLAPSE:
10151 pc = &OMP_CLAUSE_CHAIN (c);
10152 continue;
10154 default:
10155 gcc_unreachable ();
10158 if (!remove)
10160 t = OMP_CLAUSE_DECL (c);
10162 if (need_complete)
10164 t = require_complete_type (t);
10165 if (t == error_mark_node)
10166 remove = true;
10169 if (need_implicitly_determined)
10171 const char *share_name = NULL;
10173 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10174 share_name = "threadprivate";
10175 else switch (c_omp_predetermined_sharing (t))
10177 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10178 break;
10179 case OMP_CLAUSE_DEFAULT_SHARED:
10180 share_name = "shared";
10181 break;
10182 case OMP_CLAUSE_DEFAULT_PRIVATE:
10183 share_name = "private";
10184 break;
10185 default:
10186 gcc_unreachable ();
10188 if (share_name)
10190 error_at (OMP_CLAUSE_LOCATION (c),
10191 "%qE is predetermined %qs for %qs",
10192 t, share_name, name);
10193 remove = true;
10198 if (remove)
10199 *pc = OMP_CLAUSE_CHAIN (c);
10200 else
10201 pc = &OMP_CLAUSE_CHAIN (c);
10204 bitmap_obstack_release (NULL);
10205 return clauses;
10208 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10209 down to the element type of an array. */
10211 tree
10212 c_build_qualified_type (tree type, int type_quals)
10214 if (type == error_mark_node)
10215 return type;
10217 if (TREE_CODE (type) == ARRAY_TYPE)
10219 tree t;
10220 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10221 type_quals);
10223 /* See if we already have an identically qualified type. */
10224 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10226 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10227 && TYPE_NAME (t) == TYPE_NAME (type)
10228 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10229 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10230 TYPE_ATTRIBUTES (type)))
10231 break;
10233 if (!t)
10235 tree domain = TYPE_DOMAIN (type);
10237 t = build_variant_type_copy (type);
10238 TREE_TYPE (t) = element_type;
10240 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10241 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10242 SET_TYPE_STRUCTURAL_EQUALITY (t);
10243 else if (TYPE_CANONICAL (element_type) != element_type
10244 || (domain && TYPE_CANONICAL (domain) != domain))
10246 tree unqualified_canon
10247 = build_array_type (TYPE_CANONICAL (element_type),
10248 domain? TYPE_CANONICAL (domain)
10249 : NULL_TREE);
10250 TYPE_CANONICAL (t)
10251 = c_build_qualified_type (unqualified_canon, type_quals);
10253 else
10254 TYPE_CANONICAL (t) = t;
10256 return t;
10259 /* A restrict-qualified pointer type must be a pointer to object or
10260 incomplete type. Note that the use of POINTER_TYPE_P also allows
10261 REFERENCE_TYPEs, which is appropriate for C++. */
10262 if ((type_quals & TYPE_QUAL_RESTRICT)
10263 && (!POINTER_TYPE_P (type)
10264 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10266 error ("invalid use of %<restrict%>");
10267 type_quals &= ~TYPE_QUAL_RESTRICT;
10270 return build_qualified_type (type, type_quals);
10273 /* Build a VA_ARG_EXPR for the C parser. */
10275 tree
10276 c_build_va_arg (location_t loc, tree expr, tree type)
10278 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10279 warning_at (loc, OPT_Wc___compat,
10280 "C++ requires promoted type, not enum type, in %<va_arg%>");
10281 return build_va_arg (loc, expr, type);