Support AVX for cmpss/cmpsd.
[official-gcc.git] / gcc / c-typeck.c
blob083b5a15ac33ba9fa861ce64407a5a16dc81d2b2
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "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 struct obstack *);
102 static void output_pending_init_elements (int, struct obstack *);
103 static int set_designator (int, struct obstack *);
104 static void push_range_stack (tree, struct obstack *);
105 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
106 static void set_nonincremental_init (struct obstack *);
107 static void set_nonincremental_init_from_string (tree, struct obstack *);
108 static tree find_init_member (tree, struct obstack *);
109 static void readonly_error (tree, enum lvalue_use);
110 static void readonly_warning (tree, enum lvalue_use);
111 static int lvalue_or_else (const_tree, enum lvalue_use);
112 static void record_maybe_used_decl (tree);
113 static int comptypes_internal (const_tree, const_tree, bool *);
115 /* Return true if EXP is a null pointer constant, false otherwise. */
117 static bool
118 null_pointer_constant_p (const_tree expr)
120 /* This should really operate on c_expr structures, but they aren't
121 yet available everywhere required. */
122 tree type = TREE_TYPE (expr);
123 return (TREE_CODE (expr) == INTEGER_CST
124 && !TREE_OVERFLOW (expr)
125 && integer_zerop (expr)
126 && (INTEGRAL_TYPE_P (type)
127 || (TREE_CODE (type) == POINTER_TYPE
128 && VOID_TYPE_P (TREE_TYPE (type))
129 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
132 /* EXPR may appear in an unevaluated part of an integer constant
133 expression, but not in an evaluated part. Wrap it in a
134 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
135 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
137 static tree
138 note_integer_operands (tree expr)
140 tree ret;
141 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
143 ret = copy_node (expr);
144 TREE_OVERFLOW (ret) = 1;
146 else
148 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
149 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
151 return ret;
154 /* Having checked whether EXPR may appear in an unevaluated part of an
155 integer constant expression and found that it may, remove any
156 C_MAYBE_CONST_EXPR noting this fact and return the resulting
157 expression. */
159 static inline tree
160 remove_c_maybe_const_expr (tree expr)
162 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
163 return C_MAYBE_CONST_EXPR_EXPR (expr);
164 else
165 return expr;
168 \f/* This is a cache to hold if two types are compatible or not. */
170 struct tagged_tu_seen_cache {
171 const struct tagged_tu_seen_cache * next;
172 const_tree t1;
173 const_tree t2;
174 /* The return value of tagged_types_tu_compatible_p if we had seen
175 these two types already. */
176 int val;
179 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
180 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
182 /* Do `exp = require_complete_type (exp);' to make sure exp
183 does not have an incomplete type. (That includes void types.) */
185 tree
186 require_complete_type (tree value)
188 tree type = TREE_TYPE (value);
190 if (value == error_mark_node || type == error_mark_node)
191 return error_mark_node;
193 /* First, detect a valid value with a complete type. */
194 if (COMPLETE_TYPE_P (type))
195 return value;
197 c_incomplete_type_error (value, type);
198 return error_mark_node;
201 /* Print an error message for invalid use of an incomplete type.
202 VALUE is the expression that was used (or 0 if that isn't known)
203 and TYPE is the type that was invalid. */
205 void
206 c_incomplete_type_error (const_tree value, const_tree type)
208 const char *type_code_string;
210 /* Avoid duplicate error message. */
211 if (TREE_CODE (type) == ERROR_MARK)
212 return;
214 if (value != 0 && (TREE_CODE (value) == VAR_DECL
215 || TREE_CODE (value) == PARM_DECL))
216 error ("%qD has an incomplete type", value);
217 else
219 retry:
220 /* We must print an error message. Be clever about what it says. */
222 switch (TREE_CODE (type))
224 case RECORD_TYPE:
225 type_code_string = "struct";
226 break;
228 case UNION_TYPE:
229 type_code_string = "union";
230 break;
232 case ENUMERAL_TYPE:
233 type_code_string = "enum";
234 break;
236 case VOID_TYPE:
237 error ("invalid use of void expression");
238 return;
240 case ARRAY_TYPE:
241 if (TYPE_DOMAIN (type))
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
245 error ("invalid use of flexible array member");
246 return;
248 type = TREE_TYPE (type);
249 goto retry;
251 error ("invalid use of array with unspecified bounds");
252 return;
254 default:
255 gcc_unreachable ();
258 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
259 error ("invalid use of undefined type %<%s %E%>",
260 type_code_string, TYPE_NAME (type));
261 else
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
263 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
267 /* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
270 tree
271 c_type_promotes_to (tree type)
273 if (TYPE_MAIN_VARIANT (type) == float_type_node)
274 return double_type_node;
276 if (c_promoting_integer_type_p (type))
278 /* Preserve unsignedness if not really getting any wider. */
279 if (TYPE_UNSIGNED (type)
280 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
281 return unsigned_type_node;
282 return integer_type_node;
285 return type;
288 /* Return true if between two named address spaces, whether there is a superset
289 named address space that encompasses both address spaces. If there is a
290 superset, return which address space is the superset. */
292 static bool
293 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
295 if (as1 == as2)
297 *common = as1;
298 return true;
300 else if (targetm.addr_space.subset_p (as1, as2))
302 *common = as2;
303 return true;
305 else if (targetm.addr_space.subset_p (as2, as1))
307 *common = as1;
308 return true;
310 else
311 return false;
314 /* Return a variant of TYPE which has all the type qualifiers of LIKE
315 as well as those of TYPE. */
317 static tree
318 qualify_type (tree type, tree like)
320 addr_space_t as_type = TYPE_ADDR_SPACE (type);
321 addr_space_t as_like = TYPE_ADDR_SPACE (like);
322 addr_space_t as_common;
324 /* If the two named address spaces are different, determine the common
325 superset address space. If there isn't one, raise an error. */
326 if (!addr_space_superset (as_type, as_like, &as_common))
328 as_common = as_type;
329 error ("%qT and %qT are in disjoint named address spaces",
330 type, like);
333 return c_build_qualified_type (type,
334 TYPE_QUALS_NO_ADDR_SPACE (type)
335 | TYPE_QUALS_NO_ADDR_SPACE (like)
336 | ENCODE_QUAL_ADDR_SPACE (as_common));
339 /* Return true iff the given tree T is a variable length array. */
341 bool
342 c_vla_type_p (const_tree t)
344 if (TREE_CODE (t) == ARRAY_TYPE
345 && C_TYPE_VARIABLE_SIZE (t))
346 return true;
347 return false;
350 /* Return the composite type of two compatible types.
352 We assume that comptypes has already been done and returned
353 nonzero; if that isn't so, this may crash. In particular, we
354 assume that qualifiers match. */
356 tree
357 composite_type (tree t1, tree t2)
359 enum tree_code code1;
360 enum tree_code code2;
361 tree attributes;
363 /* Save time if the two types are the same. */
365 if (t1 == t2) return t1;
367 /* If one type is nonsense, use the other. */
368 if (t1 == error_mark_node)
369 return t2;
370 if (t2 == error_mark_node)
371 return t1;
373 code1 = TREE_CODE (t1);
374 code2 = TREE_CODE (t2);
376 /* Merge the attributes. */
377 attributes = targetm.merge_type_attributes (t1, t2);
379 /* If one is an enumerated type and the other is the compatible
380 integer type, the composite type might be either of the two
381 (DR#013 question 3). For consistency, use the enumerated type as
382 the composite type. */
384 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
385 return t1;
386 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
387 return t2;
389 gcc_assert (code1 == code2);
391 switch (code1)
393 case POINTER_TYPE:
394 /* For two pointers, do this recursively on the target type. */
396 tree pointed_to_1 = TREE_TYPE (t1);
397 tree pointed_to_2 = TREE_TYPE (t2);
398 tree target = composite_type (pointed_to_1, pointed_to_2);
399 t1 = build_pointer_type (target);
400 t1 = build_type_attribute_variant (t1, attributes);
401 return qualify_type (t1, t2);
404 case ARRAY_TYPE:
406 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
407 int quals;
408 tree unqual_elt;
409 tree d1 = TYPE_DOMAIN (t1);
410 tree d2 = TYPE_DOMAIN (t2);
411 bool d1_variable, d2_variable;
412 bool d1_zero, d2_zero;
413 bool t1_complete, t2_complete;
415 /* We should not have any type quals on arrays at all. */
416 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
417 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
419 t1_complete = COMPLETE_TYPE_P (t1);
420 t2_complete = COMPLETE_TYPE_P (t2);
422 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
423 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
425 d1_variable = (!d1_zero
426 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
427 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
428 d2_variable = (!d2_zero
429 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
430 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
431 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
432 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
434 /* Save space: see if the result is identical to one of the args. */
435 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
436 && (d2_variable || d2_zero || !d1_variable))
437 return build_type_attribute_variant (t1, attributes);
438 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
439 && (d1_variable || d1_zero || !d2_variable))
440 return build_type_attribute_variant (t2, attributes);
442 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t1, attributes);
444 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
445 return build_type_attribute_variant (t2, attributes);
447 /* Merge the element types, and have a size if either arg has
448 one. We may have qualifiers on the element types. To set
449 up TYPE_MAIN_VARIANT correctly, we need to form the
450 composite of the unqualified types and add the qualifiers
451 back at the end. */
452 quals = TYPE_QUALS (strip_array_types (elt));
453 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
454 t1 = build_array_type (unqual_elt,
455 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
456 && (d2_variable
457 || d2_zero
458 || !d1_variable))
459 ? t1
460 : t2));
461 /* Ensure a composite type involving a zero-length array type
462 is a zero-length type not an incomplete type. */
463 if (d1_zero && d2_zero
464 && (t1_complete || t2_complete)
465 && !COMPLETE_TYPE_P (t1))
467 TYPE_SIZE (t1) = bitsize_zero_node;
468 TYPE_SIZE_UNIT (t1) = size_zero_node;
470 t1 = c_build_qualified_type (t1, quals);
471 return build_type_attribute_variant (t1, attributes);
474 case ENUMERAL_TYPE:
475 case RECORD_TYPE:
476 case UNION_TYPE:
477 if (attributes != NULL)
479 /* Try harder not to create a new aggregate type. */
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
481 return t1;
482 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
483 return t2;
485 return build_type_attribute_variant (t1, attributes);
487 case FUNCTION_TYPE:
488 /* Function types: prefer the one that specified arg types.
489 If both do, merge the arg types. Also merge the return types. */
491 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
492 tree p1 = TYPE_ARG_TYPES (t1);
493 tree p2 = TYPE_ARG_TYPES (t2);
494 int len;
495 tree newargs, n;
496 int i;
498 /* Save space: see if the result is identical to one of the args. */
499 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
500 return build_type_attribute_variant (t1, attributes);
501 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
502 return build_type_attribute_variant (t2, attributes);
504 /* Simple way if one arg fails to specify argument types. */
505 if (TYPE_ARG_TYPES (t1) == 0)
507 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
508 t1 = build_type_attribute_variant (t1, attributes);
509 return qualify_type (t1, t2);
511 if (TYPE_ARG_TYPES (t2) == 0)
513 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
514 t1 = build_type_attribute_variant (t1, attributes);
515 return qualify_type (t1, t2);
518 /* If both args specify argument types, we must merge the two
519 lists, argument by argument. */
520 /* Tell global_bindings_p to return false so that variable_size
521 doesn't die on VLAs in parameter types. */
522 c_override_global_bindings_to_false = true;
524 len = list_length (p1);
525 newargs = 0;
527 for (i = 0; i < len; i++)
528 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
530 n = newargs;
532 for (; p1;
533 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
535 /* A null type means arg type is not specified.
536 Take whatever the other function type has. */
537 if (TREE_VALUE (p1) == 0)
539 TREE_VALUE (n) = TREE_VALUE (p2);
540 goto parm_done;
542 if (TREE_VALUE (p2) == 0)
544 TREE_VALUE (n) = TREE_VALUE (p1);
545 goto parm_done;
548 /* Given wait (union {union wait *u; int *i} *)
549 and wait (union wait *),
550 prefer union wait * as type of parm. */
551 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
552 && TREE_VALUE (p1) != TREE_VALUE (p2))
554 tree memb;
555 tree mv2 = TREE_VALUE (p2);
556 if (mv2 && mv2 != error_mark_node
557 && TREE_CODE (mv2) != ARRAY_TYPE)
558 mv2 = TYPE_MAIN_VARIANT (mv2);
559 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
560 memb; memb = TREE_CHAIN (memb))
562 tree mv3 = TREE_TYPE (memb);
563 if (mv3 && mv3 != error_mark_node
564 && TREE_CODE (mv3) != ARRAY_TYPE)
565 mv3 = TYPE_MAIN_VARIANT (mv3);
566 if (comptypes (mv3, mv2))
568 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
569 TREE_VALUE (p2));
570 pedwarn (input_location, OPT_pedantic,
571 "function types not truly compatible in ISO C");
572 goto parm_done;
576 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
577 && TREE_VALUE (p2) != TREE_VALUE (p1))
579 tree memb;
580 tree mv1 = TREE_VALUE (p1);
581 if (mv1 && mv1 != error_mark_node
582 && TREE_CODE (mv1) != ARRAY_TYPE)
583 mv1 = TYPE_MAIN_VARIANT (mv1);
584 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
585 memb; memb = TREE_CHAIN (memb))
587 tree mv3 = TREE_TYPE (memb);
588 if (mv3 && mv3 != error_mark_node
589 && TREE_CODE (mv3) != ARRAY_TYPE)
590 mv3 = TYPE_MAIN_VARIANT (mv3);
591 if (comptypes (mv3, mv1))
593 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
594 TREE_VALUE (p1));
595 pedwarn (input_location, OPT_pedantic,
596 "function types not truly compatible in ISO C");
597 goto parm_done;
601 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
602 parm_done: ;
605 c_override_global_bindings_to_false = false;
606 t1 = build_function_type (valtype, newargs);
607 t1 = qualify_type (t1, t2);
608 /* ... falls through ... */
611 default:
612 return build_type_attribute_variant (t1, attributes);
617 /* Return the type of a conditional expression between pointers to
618 possibly differently qualified versions of compatible types.
620 We assume that comp_target_types has already been done and returned
621 nonzero; if that isn't so, this may crash. */
623 static tree
624 common_pointer_type (tree t1, tree t2)
626 tree attributes;
627 tree pointed_to_1, mv1;
628 tree pointed_to_2, mv2;
629 tree target;
630 unsigned target_quals;
631 addr_space_t as1, as2, as_common;
632 int quals1, quals2;
634 /* Save time if the two types are the same. */
636 if (t1 == t2) return t1;
638 /* If one type is nonsense, use the other. */
639 if (t1 == error_mark_node)
640 return t2;
641 if (t2 == error_mark_node)
642 return t1;
644 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
645 && TREE_CODE (t2) == POINTER_TYPE);
647 /* Merge the attributes. */
648 attributes = targetm.merge_type_attributes (t1, t2);
650 /* Find the composite type of the target types, and combine the
651 qualifiers of the two types' targets. Do not lose qualifiers on
652 array element types by taking the TYPE_MAIN_VARIANT. */
653 mv1 = pointed_to_1 = TREE_TYPE (t1);
654 mv2 = pointed_to_2 = TREE_TYPE (t2);
655 if (TREE_CODE (mv1) != ARRAY_TYPE)
656 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
657 if (TREE_CODE (mv2) != ARRAY_TYPE)
658 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
659 target = composite_type (mv1, mv2);
661 /* For function types do not merge const qualifiers, but drop them
662 if used inconsistently. The middle-end uses these to mark const
663 and noreturn functions. */
664 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
665 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
667 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
668 target_quals = (quals1 & quals2);
669 else
670 target_quals = (quals1 | quals2);
672 /* If the two named address spaces are different, determine the common
673 superset address space. This is guaranteed to exist due to the
674 assumption that comp_target_type returned non-zero. */
675 as1 = TYPE_ADDR_SPACE (pointed_to_1);
676 as2 = TYPE_ADDR_SPACE (pointed_to_2);
677 if (!addr_space_superset (as1, as2, &as_common))
678 gcc_unreachable ();
680 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
682 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
683 return build_type_attribute_variant (t1, attributes);
686 /* Return the common type for two arithmetic types under the usual
687 arithmetic conversions. The default conversions have already been
688 applied, and enumerated types converted to their compatible integer
689 types. The resulting type is unqualified and has no attributes.
691 This is the type for the result of most arithmetic operations
692 if the operands have the given two types. */
694 static tree
695 c_common_type (tree t1, tree t2)
697 enum tree_code code1;
698 enum tree_code code2;
700 /* If one type is nonsense, use the other. */
701 if (t1 == error_mark_node)
702 return t2;
703 if (t2 == error_mark_node)
704 return t1;
706 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
707 t1 = TYPE_MAIN_VARIANT (t1);
709 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
710 t2 = TYPE_MAIN_VARIANT (t2);
712 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
713 t1 = build_type_attribute_variant (t1, NULL_TREE);
715 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
716 t2 = build_type_attribute_variant (t2, NULL_TREE);
718 /* Save time if the two types are the same. */
720 if (t1 == t2) return t1;
722 code1 = TREE_CODE (t1);
723 code2 = TREE_CODE (t2);
725 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
726 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
727 || code1 == INTEGER_TYPE);
728 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
729 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
730 || code2 == INTEGER_TYPE);
732 /* When one operand is a decimal float type, the other operand cannot be
733 a generic float type or a complex type. We also disallow vector types
734 here. */
735 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
736 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
738 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
740 error ("can%'t mix operands of decimal float and vector types");
741 return error_mark_node;
743 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
745 error ("can%'t mix operands of decimal float and complex types");
746 return error_mark_node;
748 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
750 error ("can%'t mix operands of decimal float and other float types");
751 return error_mark_node;
755 /* If one type is a vector type, return that type. (How the usual
756 arithmetic conversions apply to the vector types extension is not
757 precisely specified.) */
758 if (code1 == VECTOR_TYPE)
759 return t1;
761 if (code2 == VECTOR_TYPE)
762 return t2;
764 /* If one type is complex, form the common type of the non-complex
765 components, then make that complex. Use T1 or T2 if it is the
766 required type. */
767 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
769 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
770 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
771 tree subtype = c_common_type (subtype1, subtype2);
773 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
774 return t1;
775 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
776 return t2;
777 else
778 return build_complex_type (subtype);
781 /* If only one is real, use it as the result. */
783 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
784 return t1;
786 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
787 return t2;
789 /* If both are real and either are decimal floating point types, use
790 the decimal floating point type with the greater precision. */
792 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
794 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
795 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
796 return dfloat128_type_node;
797 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
798 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
799 return dfloat64_type_node;
800 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
801 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
802 return dfloat32_type_node;
805 /* Deal with fixed-point types. */
806 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
808 unsigned int unsignedp = 0, satp = 0;
809 enum machine_mode m1, m2;
810 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
812 m1 = TYPE_MODE (t1);
813 m2 = TYPE_MODE (t2);
815 /* If one input type is saturating, the result type is saturating. */
816 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
817 satp = 1;
819 /* If both fixed-point types are unsigned, the result type is unsigned.
820 When mixing fixed-point and integer types, follow the sign of the
821 fixed-point type.
822 Otherwise, the result type is signed. */
823 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
824 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
825 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
826 && TYPE_UNSIGNED (t1))
827 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
828 && TYPE_UNSIGNED (t2)))
829 unsignedp = 1;
831 /* The result type is signed. */
832 if (unsignedp == 0)
834 /* If the input type is unsigned, we need to convert to the
835 signed type. */
836 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
838 enum mode_class mclass = (enum mode_class) 0;
839 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
840 mclass = MODE_FRACT;
841 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
842 mclass = MODE_ACCUM;
843 else
844 gcc_unreachable ();
845 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
847 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
849 enum mode_class mclass = (enum mode_class) 0;
850 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
851 mclass = MODE_FRACT;
852 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
853 mclass = MODE_ACCUM;
854 else
855 gcc_unreachable ();
856 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
860 if (code1 == FIXED_POINT_TYPE)
862 fbit1 = GET_MODE_FBIT (m1);
863 ibit1 = GET_MODE_IBIT (m1);
865 else
867 fbit1 = 0;
868 /* Signed integers need to subtract one sign bit. */
869 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
872 if (code2 == FIXED_POINT_TYPE)
874 fbit2 = GET_MODE_FBIT (m2);
875 ibit2 = GET_MODE_IBIT (m2);
877 else
879 fbit2 = 0;
880 /* Signed integers need to subtract one sign bit. */
881 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
884 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
885 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
886 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
887 satp);
890 /* Both real or both integers; use the one with greater precision. */
892 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
893 return t1;
894 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
895 return t2;
897 /* Same precision. Prefer long longs to longs to ints when the
898 same precision, following the C99 rules on integer type rank
899 (which are equivalent to the C90 rules for C90 types). */
901 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
902 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
903 return long_long_unsigned_type_node;
905 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
906 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
908 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
909 return long_long_unsigned_type_node;
910 else
911 return long_long_integer_type_node;
914 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
915 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
916 return long_unsigned_type_node;
918 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
919 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
921 /* But preserve unsignedness from the other type,
922 since long cannot hold all the values of an unsigned int. */
923 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
924 return long_unsigned_type_node;
925 else
926 return long_integer_type_node;
929 /* Likewise, prefer long double to double even if same size. */
930 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
931 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
932 return long_double_type_node;
934 /* Otherwise prefer the unsigned one. */
936 if (TYPE_UNSIGNED (t1))
937 return t1;
938 else
939 return t2;
942 /* Wrapper around c_common_type that is used by c-common.c and other
943 front end optimizations that remove promotions. ENUMERAL_TYPEs
944 are allowed here and are converted to their compatible integer types.
945 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
946 preferably a non-Boolean type as the common type. */
947 tree
948 common_type (tree t1, tree t2)
950 if (TREE_CODE (t1) == ENUMERAL_TYPE)
951 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
952 if (TREE_CODE (t2) == ENUMERAL_TYPE)
953 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
955 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
956 if (TREE_CODE (t1) == BOOLEAN_TYPE
957 && TREE_CODE (t2) == BOOLEAN_TYPE)
958 return boolean_type_node;
960 /* If either type is BOOLEAN_TYPE, then return the other. */
961 if (TREE_CODE (t1) == BOOLEAN_TYPE)
962 return t2;
963 if (TREE_CODE (t2) == BOOLEAN_TYPE)
964 return t1;
966 return c_common_type (t1, t2);
969 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
970 or various other operations. Return 2 if they are compatible
971 but a warning may be needed if you use them together. */
974 comptypes (tree type1, tree type2)
976 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
977 int val;
979 val = comptypes_internal (type1, type2, NULL);
980 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
982 return val;
985 /* Like comptypes, but if it returns non-zero because enum and int are
986 compatible, it sets *ENUM_AND_INT_P to true. */
988 static int
989 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
991 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
992 int val;
994 val = comptypes_internal (type1, type2, enum_and_int_p);
995 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
997 return val;
1000 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1001 or various other operations. Return 2 if they are compatible
1002 but a warning may be needed if you use them together. If
1003 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1004 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1005 *ENUM_AND_INT_P is never set to false. This differs from
1006 comptypes, in that we don't free the seen types. */
1008 static int
1009 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p)
1011 const_tree t1 = type1;
1012 const_tree t2 = type2;
1013 int attrval, val;
1015 /* Suppress errors caused by previously reported errors. */
1017 if (t1 == t2 || !t1 || !t2
1018 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1019 return 1;
1021 /* If either type is the internal version of sizetype, return the
1022 language version. */
1023 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
1024 && TYPE_ORIG_SIZE_TYPE (t1))
1025 t1 = TYPE_ORIG_SIZE_TYPE (t1);
1027 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
1028 && TYPE_ORIG_SIZE_TYPE (t2))
1029 t2 = TYPE_ORIG_SIZE_TYPE (t2);
1032 /* Enumerated types are compatible with integer types, but this is
1033 not transitive: two enumerated types in the same translation unit
1034 are compatible with each other only if they are the same type. */
1036 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1038 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1039 if (enum_and_int_p != NULL && TREE_CODE (t2) != VOID_TYPE)
1040 *enum_and_int_p = true;
1042 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1044 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1045 if (enum_and_int_p != NULL && TREE_CODE (t1) != VOID_TYPE)
1046 *enum_and_int_p = true;
1049 if (t1 == t2)
1050 return 1;
1052 /* Different classes of types can't be compatible. */
1054 if (TREE_CODE (t1) != TREE_CODE (t2))
1055 return 0;
1057 /* Qualifiers must match. C99 6.7.3p9 */
1059 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1060 return 0;
1062 /* Allow for two different type nodes which have essentially the same
1063 definition. Note that we already checked for equality of the type
1064 qualifiers (just above). */
1066 if (TREE_CODE (t1) != ARRAY_TYPE
1067 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1068 return 1;
1070 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1071 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1072 return 0;
1074 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1075 val = 0;
1077 switch (TREE_CODE (t1))
1079 case POINTER_TYPE:
1080 /* Do not remove mode or aliasing information. */
1081 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1082 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1083 break;
1084 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1085 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1086 enum_and_int_p));
1087 break;
1089 case FUNCTION_TYPE:
1090 val = function_types_compatible_p (t1, t2, enum_and_int_p);
1091 break;
1093 case ARRAY_TYPE:
1095 tree d1 = TYPE_DOMAIN (t1);
1096 tree d2 = TYPE_DOMAIN (t2);
1097 bool d1_variable, d2_variable;
1098 bool d1_zero, d2_zero;
1099 val = 1;
1101 /* Target types must match incl. qualifiers. */
1102 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1103 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1104 enum_and_int_p)))
1105 return 0;
1107 /* Sizes must match unless one is missing or variable. */
1108 if (d1 == 0 || d2 == 0 || d1 == d2)
1109 break;
1111 d1_zero = !TYPE_MAX_VALUE (d1);
1112 d2_zero = !TYPE_MAX_VALUE (d2);
1114 d1_variable = (!d1_zero
1115 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1116 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1117 d2_variable = (!d2_zero
1118 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1119 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1120 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1121 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1123 if (d1_variable || d2_variable)
1124 break;
1125 if (d1_zero && d2_zero)
1126 break;
1127 if (d1_zero || d2_zero
1128 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1129 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1130 val = 0;
1132 break;
1135 case ENUMERAL_TYPE:
1136 case RECORD_TYPE:
1137 case UNION_TYPE:
1138 if (val != 1 && !same_translation_unit_p (t1, t2))
1140 tree a1 = TYPE_ATTRIBUTES (t1);
1141 tree a2 = TYPE_ATTRIBUTES (t2);
1143 if (! attribute_list_contained (a1, a2)
1144 && ! attribute_list_contained (a2, a1))
1145 break;
1147 if (attrval != 2)
1148 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p);
1149 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p);
1151 break;
1153 case VECTOR_TYPE:
1154 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1155 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1156 enum_and_int_p));
1157 break;
1159 default:
1160 break;
1162 return attrval == 2 && val == 1 ? 2 : val;
1165 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1166 their qualifiers, except for named address spaces. If the pointers point to
1167 different named addresses, then we must determine if one address space is a
1168 subset of the other. */
1170 static int
1171 comp_target_types (location_t location, tree ttl, tree ttr)
1173 int val;
1174 tree mvl = TREE_TYPE (ttl);
1175 tree mvr = TREE_TYPE (ttr);
1176 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1177 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1178 addr_space_t as_common;
1179 bool enum_and_int_p;
1181 /* Fail if pointers point to incompatible address spaces. */
1182 if (!addr_space_superset (asl, asr, &as_common))
1183 return 0;
1185 /* Do not lose qualifiers on element types of array types that are
1186 pointer targets by taking their TYPE_MAIN_VARIANT. */
1187 if (TREE_CODE (mvl) != ARRAY_TYPE)
1188 mvl = TYPE_MAIN_VARIANT (mvl);
1189 if (TREE_CODE (mvr) != ARRAY_TYPE)
1190 mvr = TYPE_MAIN_VARIANT (mvr);
1191 enum_and_int_p = false;
1192 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1194 if (val == 2)
1195 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1197 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1198 warning_at (location, OPT_Wc___compat,
1199 "pointer target types incompatible in C++");
1201 return val;
1204 /* Subroutines of `comptypes'. */
1206 /* Determine whether two trees derive from the same translation unit.
1207 If the CONTEXT chain ends in a null, that tree's context is still
1208 being parsed, so if two trees have context chains ending in null,
1209 they're in the same translation unit. */
1211 same_translation_unit_p (const_tree t1, const_tree t2)
1213 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1214 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1216 case tcc_declaration:
1217 t1 = DECL_CONTEXT (t1); break;
1218 case tcc_type:
1219 t1 = TYPE_CONTEXT (t1); break;
1220 case tcc_exceptional:
1221 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1222 default: gcc_unreachable ();
1225 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1226 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1228 case tcc_declaration:
1229 t2 = DECL_CONTEXT (t2); break;
1230 case tcc_type:
1231 t2 = TYPE_CONTEXT (t2); break;
1232 case tcc_exceptional:
1233 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1234 default: gcc_unreachable ();
1237 return t1 == t2;
1240 /* Allocate the seen two types, assuming that they are compatible. */
1242 static struct tagged_tu_seen_cache *
1243 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1245 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1246 tu->next = tagged_tu_seen_base;
1247 tu->t1 = t1;
1248 tu->t2 = t2;
1250 tagged_tu_seen_base = tu;
1252 /* The C standard says that two structures in different translation
1253 units are compatible with each other only if the types of their
1254 fields are compatible (among other things). We assume that they
1255 are compatible until proven otherwise when building the cache.
1256 An example where this can occur is:
1257 struct a
1259 struct a *next;
1261 If we are comparing this against a similar struct in another TU,
1262 and did not assume they were compatible, we end up with an infinite
1263 loop. */
1264 tu->val = 1;
1265 return tu;
1268 /* Free the seen types until we get to TU_TIL. */
1270 static void
1271 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1273 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1274 while (tu != tu_til)
1276 const struct tagged_tu_seen_cache *const tu1
1277 = (const struct tagged_tu_seen_cache *) tu;
1278 tu = tu1->next;
1279 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1281 tagged_tu_seen_base = tu_til;
1284 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1285 compatible. If the two types are not the same (which has been
1286 checked earlier), this can only happen when multiple translation
1287 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1288 rules. ENUM_AND_INT_P is as in comptypes_internal. */
1290 static int
1291 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1292 bool *enum_and_int_p)
1294 tree s1, s2;
1295 bool needs_warning = false;
1297 /* We have to verify that the tags of the types are the same. This
1298 is harder than it looks because this may be a typedef, so we have
1299 to go look at the original type. It may even be a typedef of a
1300 typedef...
1301 In the case of compiler-created builtin structs the TYPE_DECL
1302 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1303 while (TYPE_NAME (t1)
1304 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1305 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1306 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1308 while (TYPE_NAME (t2)
1309 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1310 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1311 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1313 /* C90 didn't have the requirement that the two tags be the same. */
1314 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1315 return 0;
1317 /* C90 didn't say what happened if one or both of the types were
1318 incomplete; we choose to follow C99 rules here, which is that they
1319 are compatible. */
1320 if (TYPE_SIZE (t1) == NULL
1321 || TYPE_SIZE (t2) == NULL)
1322 return 1;
1325 const struct tagged_tu_seen_cache * tts_i;
1326 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1327 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1328 return tts_i->val;
1331 switch (TREE_CODE (t1))
1333 case ENUMERAL_TYPE:
1335 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1336 /* Speed up the case where the type values are in the same order. */
1337 tree tv1 = TYPE_VALUES (t1);
1338 tree tv2 = TYPE_VALUES (t2);
1340 if (tv1 == tv2)
1342 return 1;
1345 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1347 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1348 break;
1349 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1351 tu->val = 0;
1352 return 0;
1356 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1358 return 1;
1360 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1362 tu->val = 0;
1363 return 0;
1366 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1368 tu->val = 0;
1369 return 0;
1372 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1374 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1375 if (s2 == NULL
1376 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1378 tu->val = 0;
1379 return 0;
1382 return 1;
1385 case UNION_TYPE:
1387 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1388 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1390 tu->val = 0;
1391 return 0;
1394 /* Speed up the common case where the fields are in the same order. */
1395 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1396 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1398 int result;
1400 if (DECL_NAME (s1) != DECL_NAME (s2))
1401 break;
1402 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1403 enum_and_int_p);
1405 if (result != 1 && !DECL_NAME (s1))
1406 break;
1407 if (result == 0)
1409 tu->val = 0;
1410 return 0;
1412 if (result == 2)
1413 needs_warning = true;
1415 if (TREE_CODE (s1) == FIELD_DECL
1416 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1417 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1419 tu->val = 0;
1420 return 0;
1423 if (!s1 && !s2)
1425 tu->val = needs_warning ? 2 : 1;
1426 return tu->val;
1429 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1431 bool ok = false;
1433 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1434 if (DECL_NAME (s1) == DECL_NAME (s2))
1436 int result;
1438 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1439 enum_and_int_p);
1441 if (result != 1 && !DECL_NAME (s1))
1442 continue;
1443 if (result == 0)
1445 tu->val = 0;
1446 return 0;
1448 if (result == 2)
1449 needs_warning = true;
1451 if (TREE_CODE (s1) == FIELD_DECL
1452 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1453 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1454 break;
1456 ok = true;
1457 break;
1459 if (!ok)
1461 tu->val = 0;
1462 return 0;
1465 tu->val = needs_warning ? 2 : 10;
1466 return tu->val;
1469 case RECORD_TYPE:
1471 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1473 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1474 s1 && s2;
1475 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1477 int result;
1478 if (TREE_CODE (s1) != TREE_CODE (s2)
1479 || DECL_NAME (s1) != DECL_NAME (s2))
1480 break;
1481 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1482 enum_and_int_p);
1483 if (result == 0)
1484 break;
1485 if (result == 2)
1486 needs_warning = true;
1488 if (TREE_CODE (s1) == FIELD_DECL
1489 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1490 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1491 break;
1493 if (s1 && s2)
1494 tu->val = 0;
1495 else
1496 tu->val = needs_warning ? 2 : 1;
1497 return tu->val;
1500 default:
1501 gcc_unreachable ();
1505 /* Return 1 if two function types F1 and F2 are compatible.
1506 If either type specifies no argument types,
1507 the other must specify a fixed number of self-promoting arg types.
1508 Otherwise, if one type specifies only the number of arguments,
1509 the other must specify that number of self-promoting arg types.
1510 Otherwise, the argument types must match.
1511 ENUM_AND_INT_P is as in comptypes_internal. */
1513 static int
1514 function_types_compatible_p (const_tree f1, const_tree f2,
1515 bool *enum_and_int_p)
1517 tree args1, args2;
1518 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1519 int val = 1;
1520 int val1;
1521 tree ret1, ret2;
1523 ret1 = TREE_TYPE (f1);
1524 ret2 = TREE_TYPE (f2);
1526 /* 'volatile' qualifiers on a function's return type used to mean
1527 the function is noreturn. */
1528 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1529 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1530 if (TYPE_VOLATILE (ret1))
1531 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1532 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1533 if (TYPE_VOLATILE (ret2))
1534 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1535 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1536 val = comptypes_internal (ret1, ret2, enum_and_int_p);
1537 if (val == 0)
1538 return 0;
1540 args1 = TYPE_ARG_TYPES (f1);
1541 args2 = TYPE_ARG_TYPES (f2);
1543 /* An unspecified parmlist matches any specified parmlist
1544 whose argument types don't need default promotions. */
1546 if (args1 == 0)
1548 if (!self_promoting_args_p (args2))
1549 return 0;
1550 /* If one of these types comes from a non-prototype fn definition,
1551 compare that with the other type's arglist.
1552 If they don't match, ask for a warning (but no error). */
1553 if (TYPE_ACTUAL_ARG_TYPES (f1)
1554 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1555 enum_and_int_p))
1556 val = 2;
1557 return val;
1559 if (args2 == 0)
1561 if (!self_promoting_args_p (args1))
1562 return 0;
1563 if (TYPE_ACTUAL_ARG_TYPES (f2)
1564 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1565 enum_and_int_p))
1566 val = 2;
1567 return val;
1570 /* Both types have argument lists: compare them and propagate results. */
1571 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p);
1572 return val1 != 1 ? val1 : val;
1575 /* Check two lists of types for compatibility, returning 0 for
1576 incompatible, 1 for compatible, or 2 for compatible with
1577 warning. ENUM_AND_INT_P is as in comptypes_internal. */
1579 static int
1580 type_lists_compatible_p (const_tree args1, const_tree args2,
1581 bool *enum_and_int_p)
1583 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1584 int val = 1;
1585 int newval = 0;
1587 while (1)
1589 tree a1, mv1, a2, mv2;
1590 if (args1 == 0 && args2 == 0)
1591 return val;
1592 /* If one list is shorter than the other,
1593 they fail to match. */
1594 if (args1 == 0 || args2 == 0)
1595 return 0;
1596 mv1 = a1 = TREE_VALUE (args1);
1597 mv2 = a2 = TREE_VALUE (args2);
1598 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1599 mv1 = TYPE_MAIN_VARIANT (mv1);
1600 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1601 mv2 = TYPE_MAIN_VARIANT (mv2);
1602 /* A null pointer instead of a type
1603 means there is supposed to be an argument
1604 but nothing is specified about what type it has.
1605 So match anything that self-promotes. */
1606 if (a1 == 0)
1608 if (c_type_promotes_to (a2) != a2)
1609 return 0;
1611 else if (a2 == 0)
1613 if (c_type_promotes_to (a1) != a1)
1614 return 0;
1616 /* If one of the lists has an error marker, ignore this arg. */
1617 else if (TREE_CODE (a1) == ERROR_MARK
1618 || TREE_CODE (a2) == ERROR_MARK)
1620 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p)))
1622 /* Allow wait (union {union wait *u; int *i} *)
1623 and wait (union wait *) to be compatible. */
1624 if (TREE_CODE (a1) == UNION_TYPE
1625 && (TYPE_NAME (a1) == 0
1626 || TYPE_TRANSPARENT_AGGR (a1))
1627 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1628 && tree_int_cst_equal (TYPE_SIZE (a1),
1629 TYPE_SIZE (a2)))
1631 tree memb;
1632 for (memb = TYPE_FIELDS (a1);
1633 memb; memb = TREE_CHAIN (memb))
1635 tree mv3 = TREE_TYPE (memb);
1636 if (mv3 && mv3 != error_mark_node
1637 && TREE_CODE (mv3) != ARRAY_TYPE)
1638 mv3 = TYPE_MAIN_VARIANT (mv3);
1639 if (comptypes_internal (mv3, mv2, enum_and_int_p))
1640 break;
1642 if (memb == 0)
1643 return 0;
1645 else if (TREE_CODE (a2) == UNION_TYPE
1646 && (TYPE_NAME (a2) == 0
1647 || TYPE_TRANSPARENT_AGGR (a2))
1648 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1649 && tree_int_cst_equal (TYPE_SIZE (a2),
1650 TYPE_SIZE (a1)))
1652 tree memb;
1653 for (memb = TYPE_FIELDS (a2);
1654 memb; memb = TREE_CHAIN (memb))
1656 tree mv3 = TREE_TYPE (memb);
1657 if (mv3 && mv3 != error_mark_node
1658 && TREE_CODE (mv3) != ARRAY_TYPE)
1659 mv3 = TYPE_MAIN_VARIANT (mv3);
1660 if (comptypes_internal (mv3, mv1, enum_and_int_p))
1661 break;
1663 if (memb == 0)
1664 return 0;
1666 else
1667 return 0;
1670 /* comptypes said ok, but record if it said to warn. */
1671 if (newval > val)
1672 val = newval;
1674 args1 = TREE_CHAIN (args1);
1675 args2 = TREE_CHAIN (args2);
1679 /* Compute the size to increment a pointer by. */
1681 static tree
1682 c_size_in_bytes (const_tree type)
1684 enum tree_code code = TREE_CODE (type);
1686 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1687 return size_one_node;
1689 if (!COMPLETE_OR_VOID_TYPE_P (type))
1691 error ("arithmetic on pointer to an incomplete type");
1692 return size_one_node;
1695 /* Convert in case a char is more than one unit. */
1696 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1697 size_int (TYPE_PRECISION (char_type_node)
1698 / BITS_PER_UNIT));
1701 /* Return either DECL or its known constant value (if it has one). */
1703 tree
1704 decl_constant_value (tree decl)
1706 if (/* Don't change a variable array bound or initial value to a constant
1707 in a place where a variable is invalid. Note that DECL_INITIAL
1708 isn't valid for a PARM_DECL. */
1709 current_function_decl != 0
1710 && TREE_CODE (decl) != PARM_DECL
1711 && !TREE_THIS_VOLATILE (decl)
1712 && TREE_READONLY (decl)
1713 && DECL_INITIAL (decl) != 0
1714 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1715 /* This is invalid if initial value is not constant.
1716 If it has either a function call, a memory reference,
1717 or a variable, then re-evaluating it could give different results. */
1718 && TREE_CONSTANT (DECL_INITIAL (decl))
1719 /* Check for cases where this is sub-optimal, even though valid. */
1720 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1721 return DECL_INITIAL (decl);
1722 return decl;
1725 /* Convert the array expression EXP to a pointer. */
1726 static tree
1727 array_to_pointer_conversion (location_t loc, tree exp)
1729 tree orig_exp = exp;
1730 tree type = TREE_TYPE (exp);
1731 tree adr;
1732 tree restype = TREE_TYPE (type);
1733 tree ptrtype;
1735 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1737 STRIP_TYPE_NOPS (exp);
1739 if (TREE_NO_WARNING (orig_exp))
1740 TREE_NO_WARNING (exp) = 1;
1742 ptrtype = build_pointer_type (restype);
1744 if (TREE_CODE (exp) == INDIRECT_REF)
1745 return convert (ptrtype, TREE_OPERAND (exp, 0));
1747 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1748 return convert (ptrtype, adr);
1751 /* Convert the function expression EXP to a pointer. */
1752 static tree
1753 function_to_pointer_conversion (location_t loc, tree exp)
1755 tree orig_exp = exp;
1757 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1759 STRIP_TYPE_NOPS (exp);
1761 if (TREE_NO_WARNING (orig_exp))
1762 TREE_NO_WARNING (exp) = 1;
1764 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1767 /* Mark EXP as read, not just set, for set but not used -Wunused
1768 warning purposes. */
1770 void
1771 mark_exp_read (tree exp)
1773 switch (TREE_CODE (exp))
1775 case VAR_DECL:
1776 case PARM_DECL:
1777 DECL_READ_P (exp) = 1;
1778 break;
1779 case ARRAY_REF:
1780 case COMPONENT_REF:
1781 case MODIFY_EXPR:
1782 case REALPART_EXPR:
1783 case IMAGPART_EXPR:
1784 CASE_CONVERT:
1785 case ADDR_EXPR:
1786 mark_exp_read (TREE_OPERAND (exp, 0));
1787 break;
1788 case COMPOUND_EXPR:
1789 mark_exp_read (TREE_OPERAND (exp, 1));
1790 break;
1791 default:
1792 break;
1796 /* Perform the default conversion of arrays and functions to pointers.
1797 Return the result of converting EXP. For any other expression, just
1798 return EXP.
1800 LOC is the location of the expression. */
1802 struct c_expr
1803 default_function_array_conversion (location_t loc, struct c_expr exp)
1805 tree orig_exp = exp.value;
1806 tree type = TREE_TYPE (exp.value);
1807 enum tree_code code = TREE_CODE (type);
1809 switch (code)
1811 case ARRAY_TYPE:
1813 bool not_lvalue = false;
1814 bool lvalue_array_p;
1816 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1817 || CONVERT_EXPR_P (exp.value))
1818 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1820 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1821 not_lvalue = true;
1822 exp.value = TREE_OPERAND (exp.value, 0);
1825 if (TREE_NO_WARNING (orig_exp))
1826 TREE_NO_WARNING (exp.value) = 1;
1828 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1829 if (!flag_isoc99 && !lvalue_array_p)
1831 /* Before C99, non-lvalue arrays do not decay to pointers.
1832 Normally, using such an array would be invalid; but it can
1833 be used correctly inside sizeof or as a statement expression.
1834 Thus, do not give an error here; an error will result later. */
1835 return exp;
1838 exp.value = array_to_pointer_conversion (loc, exp.value);
1840 break;
1841 case FUNCTION_TYPE:
1842 exp.value = function_to_pointer_conversion (loc, exp.value);
1843 break;
1844 default:
1845 break;
1848 return exp;
1851 struct c_expr
1852 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1854 mark_exp_read (exp.value);
1855 return default_function_array_conversion (loc, exp);
1858 /* EXP is an expression of integer type. Apply the integer promotions
1859 to it and return the promoted value. */
1861 tree
1862 perform_integral_promotions (tree exp)
1864 tree type = TREE_TYPE (exp);
1865 enum tree_code code = TREE_CODE (type);
1867 gcc_assert (INTEGRAL_TYPE_P (type));
1869 /* Normally convert enums to int,
1870 but convert wide enums to something wider. */
1871 if (code == ENUMERAL_TYPE)
1873 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1874 TYPE_PRECISION (integer_type_node)),
1875 ((TYPE_PRECISION (type)
1876 >= TYPE_PRECISION (integer_type_node))
1877 && TYPE_UNSIGNED (type)));
1879 return convert (type, exp);
1882 /* ??? This should no longer be needed now bit-fields have their
1883 proper types. */
1884 if (TREE_CODE (exp) == COMPONENT_REF
1885 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1886 /* If it's thinner than an int, promote it like a
1887 c_promoting_integer_type_p, otherwise leave it alone. */
1888 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1889 TYPE_PRECISION (integer_type_node)))
1890 return convert (integer_type_node, exp);
1892 if (c_promoting_integer_type_p (type))
1894 /* Preserve unsignedness if not really getting any wider. */
1895 if (TYPE_UNSIGNED (type)
1896 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1897 return convert (unsigned_type_node, exp);
1899 return convert (integer_type_node, exp);
1902 return exp;
1906 /* Perform default promotions for C data used in expressions.
1907 Enumeral types or short or char are converted to int.
1908 In addition, manifest constants symbols are replaced by their values. */
1910 tree
1911 default_conversion (tree exp)
1913 tree orig_exp;
1914 tree type = TREE_TYPE (exp);
1915 enum tree_code code = TREE_CODE (type);
1916 tree promoted_type;
1918 mark_exp_read (exp);
1920 /* Functions and arrays have been converted during parsing. */
1921 gcc_assert (code != FUNCTION_TYPE);
1922 if (code == ARRAY_TYPE)
1923 return exp;
1925 /* Constants can be used directly unless they're not loadable. */
1926 if (TREE_CODE (exp) == CONST_DECL)
1927 exp = DECL_INITIAL (exp);
1929 /* Strip no-op conversions. */
1930 orig_exp = exp;
1931 STRIP_TYPE_NOPS (exp);
1933 if (TREE_NO_WARNING (orig_exp))
1934 TREE_NO_WARNING (exp) = 1;
1936 if (code == VOID_TYPE)
1938 error ("void value not ignored as it ought to be");
1939 return error_mark_node;
1942 exp = require_complete_type (exp);
1943 if (exp == error_mark_node)
1944 return error_mark_node;
1946 promoted_type = targetm.promoted_type (type);
1947 if (promoted_type)
1948 return convert (promoted_type, exp);
1950 if (INTEGRAL_TYPE_P (type))
1951 return perform_integral_promotions (exp);
1953 return exp;
1956 /* Look up COMPONENT in a structure or union TYPE.
1958 If the component name is not found, returns NULL_TREE. Otherwise,
1959 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1960 stepping down the chain to the component, which is in the last
1961 TREE_VALUE of the list. Normally the list is of length one, but if
1962 the component is embedded within (nested) anonymous structures or
1963 unions, the list steps down the chain to the component. */
1965 static tree
1966 lookup_field (tree type, tree component)
1968 tree field;
1970 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1971 to the field elements. Use a binary search on this array to quickly
1972 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1973 will always be set for structures which have many elements. */
1975 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1977 int bot, top, half;
1978 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1980 field = TYPE_FIELDS (type);
1981 bot = 0;
1982 top = TYPE_LANG_SPECIFIC (type)->s->len;
1983 while (top - bot > 1)
1985 half = (top - bot + 1) >> 1;
1986 field = field_array[bot+half];
1988 if (DECL_NAME (field) == NULL_TREE)
1990 /* Step through all anon unions in linear fashion. */
1991 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1993 field = field_array[bot++];
1994 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1995 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1997 tree anon = lookup_field (TREE_TYPE (field), component);
1999 if (anon)
2000 return tree_cons (NULL_TREE, field, anon);
2004 /* Entire record is only anon unions. */
2005 if (bot > top)
2006 return NULL_TREE;
2008 /* Restart the binary search, with new lower bound. */
2009 continue;
2012 if (DECL_NAME (field) == component)
2013 break;
2014 if (DECL_NAME (field) < component)
2015 bot += half;
2016 else
2017 top = bot + half;
2020 if (DECL_NAME (field_array[bot]) == component)
2021 field = field_array[bot];
2022 else if (DECL_NAME (field) != component)
2023 return NULL_TREE;
2025 else
2027 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2029 if (DECL_NAME (field) == NULL_TREE
2030 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2031 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2033 tree anon = lookup_field (TREE_TYPE (field), component);
2035 if (anon)
2036 return tree_cons (NULL_TREE, field, anon);
2039 if (DECL_NAME (field) == component)
2040 break;
2043 if (field == NULL_TREE)
2044 return NULL_TREE;
2047 return tree_cons (NULL_TREE, field, NULL_TREE);
2050 /* Make an expression to refer to the COMPONENT field of structure or
2051 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2052 location of the COMPONENT_REF. */
2054 tree
2055 build_component_ref (location_t loc, tree datum, tree component)
2057 tree type = TREE_TYPE (datum);
2058 enum tree_code code = TREE_CODE (type);
2059 tree field = NULL;
2060 tree ref;
2061 bool datum_lvalue = lvalue_p (datum);
2063 if (!objc_is_public (datum, component))
2064 return error_mark_node;
2066 /* See if there is a field or component with name COMPONENT. */
2068 if (code == RECORD_TYPE || code == UNION_TYPE)
2070 if (!COMPLETE_TYPE_P (type))
2072 c_incomplete_type_error (NULL_TREE, type);
2073 return error_mark_node;
2076 field = lookup_field (type, component);
2078 if (!field)
2080 error_at (loc, "%qT has no member named %qE", type, component);
2081 return error_mark_node;
2084 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2085 This might be better solved in future the way the C++ front
2086 end does it - by giving the anonymous entities each a
2087 separate name and type, and then have build_component_ref
2088 recursively call itself. We can't do that here. */
2091 tree subdatum = TREE_VALUE (field);
2092 int quals;
2093 tree subtype;
2094 bool use_datum_quals;
2096 if (TREE_TYPE (subdatum) == error_mark_node)
2097 return error_mark_node;
2099 /* If this is an rvalue, it does not have qualifiers in C
2100 standard terms and we must avoid propagating such
2101 qualifiers down to a non-lvalue array that is then
2102 converted to a pointer. */
2103 use_datum_quals = (datum_lvalue
2104 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2106 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2107 if (use_datum_quals)
2108 quals |= TYPE_QUALS (TREE_TYPE (datum));
2109 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2111 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2112 NULL_TREE);
2113 SET_EXPR_LOCATION (ref, loc);
2114 if (TREE_READONLY (subdatum)
2115 || (use_datum_quals && TREE_READONLY (datum)))
2116 TREE_READONLY (ref) = 1;
2117 if (TREE_THIS_VOLATILE (subdatum)
2118 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2119 TREE_THIS_VOLATILE (ref) = 1;
2121 if (TREE_DEPRECATED (subdatum))
2122 warn_deprecated_use (subdatum, NULL_TREE);
2124 datum = ref;
2126 field = TREE_CHAIN (field);
2128 while (field);
2130 return ref;
2132 else if (code != ERROR_MARK)
2133 error_at (loc,
2134 "request for member %qE in something not a structure or union",
2135 component);
2137 return error_mark_node;
2140 /* Given an expression PTR for a pointer, return an expression
2141 for the value pointed to.
2142 ERRORSTRING is the name of the operator to appear in error messages.
2144 LOC is the location to use for the generated tree. */
2146 tree
2147 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2149 tree pointer = default_conversion (ptr);
2150 tree type = TREE_TYPE (pointer);
2151 tree ref;
2153 if (TREE_CODE (type) == POINTER_TYPE)
2155 if (CONVERT_EXPR_P (pointer)
2156 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2158 /* If a warning is issued, mark it to avoid duplicates from
2159 the backend. This only needs to be done at
2160 warn_strict_aliasing > 2. */
2161 if (warn_strict_aliasing > 2)
2162 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2163 type, TREE_OPERAND (pointer, 0)))
2164 TREE_NO_WARNING (pointer) = 1;
2167 if (TREE_CODE (pointer) == ADDR_EXPR
2168 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2169 == TREE_TYPE (type)))
2171 ref = TREE_OPERAND (pointer, 0);
2172 protected_set_expr_location (ref, loc);
2173 return ref;
2175 else
2177 tree t = TREE_TYPE (type);
2179 ref = build1 (INDIRECT_REF, t, pointer);
2181 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2183 error_at (loc, "dereferencing pointer to incomplete type");
2184 return error_mark_node;
2186 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2187 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2189 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2190 so that we get the proper error message if the result is used
2191 to assign to. Also, &* is supposed to be a no-op.
2192 And ANSI C seems to specify that the type of the result
2193 should be the const type. */
2194 /* A de-reference of a pointer to const is not a const. It is valid
2195 to change it via some other pointer. */
2196 TREE_READONLY (ref) = TYPE_READONLY (t);
2197 TREE_SIDE_EFFECTS (ref)
2198 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2199 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2200 protected_set_expr_location (ref, loc);
2201 return ref;
2204 else if (TREE_CODE (pointer) != ERROR_MARK)
2205 switch (errstring)
2207 case RO_ARRAY_INDEXING:
2208 error_at (loc,
2209 "invalid type argument of array indexing (have %qT)",
2210 type);
2211 break;
2212 case RO_UNARY_STAR:
2213 error_at (loc,
2214 "invalid type argument of unary %<*%> (have %qT)",
2215 type);
2216 break;
2217 case RO_ARROW:
2218 error_at (loc,
2219 "invalid type argument of %<->%> (have %qT)",
2220 type);
2221 break;
2222 default:
2223 gcc_unreachable ();
2225 return error_mark_node;
2228 /* This handles expressions of the form "a[i]", which denotes
2229 an array reference.
2231 This is logically equivalent in C to *(a+i), but we may do it differently.
2232 If A is a variable or a member, we generate a primitive ARRAY_REF.
2233 This avoids forcing the array out of registers, and can work on
2234 arrays that are not lvalues (for example, members of structures returned
2235 by functions).
2237 LOC is the location to use for the returned expression. */
2239 tree
2240 build_array_ref (location_t loc, tree array, tree index)
2242 tree ret;
2243 bool swapped = false;
2244 if (TREE_TYPE (array) == error_mark_node
2245 || TREE_TYPE (index) == error_mark_node)
2246 return error_mark_node;
2248 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2249 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2251 tree temp;
2252 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2253 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2255 error_at (loc, "subscripted value is neither array nor pointer");
2256 return error_mark_node;
2258 temp = array;
2259 array = index;
2260 index = temp;
2261 swapped = true;
2264 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2266 error_at (loc, "array subscript is not an integer");
2267 return error_mark_node;
2270 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2272 error_at (loc, "subscripted value is pointer to function");
2273 return error_mark_node;
2276 /* ??? Existing practice has been to warn only when the char
2277 index is syntactically the index, not for char[array]. */
2278 if (!swapped)
2279 warn_array_subscript_with_type_char (index);
2281 /* Apply default promotions *after* noticing character types. */
2282 index = default_conversion (index);
2284 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2286 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2288 tree rval, type;
2290 /* An array that is indexed by a non-constant
2291 cannot be stored in a register; we must be able to do
2292 address arithmetic on its address.
2293 Likewise an array of elements of variable size. */
2294 if (TREE_CODE (index) != INTEGER_CST
2295 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2296 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2298 if (!c_mark_addressable (array))
2299 return error_mark_node;
2301 /* An array that is indexed by a constant value which is not within
2302 the array bounds cannot be stored in a register either; because we
2303 would get a crash in store_bit_field/extract_bit_field when trying
2304 to access a non-existent part of the register. */
2305 if (TREE_CODE (index) == INTEGER_CST
2306 && TYPE_DOMAIN (TREE_TYPE (array))
2307 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2309 if (!c_mark_addressable (array))
2310 return error_mark_node;
2313 if (pedantic)
2315 tree foo = array;
2316 while (TREE_CODE (foo) == COMPONENT_REF)
2317 foo = TREE_OPERAND (foo, 0);
2318 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2319 pedwarn (loc, OPT_pedantic,
2320 "ISO C forbids subscripting %<register%> array");
2321 else if (!flag_isoc99 && !lvalue_p (foo))
2322 pedwarn (loc, OPT_pedantic,
2323 "ISO C90 forbids subscripting non-lvalue array");
2326 type = TREE_TYPE (TREE_TYPE (array));
2327 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2328 /* Array ref is const/volatile if the array elements are
2329 or if the array is. */
2330 TREE_READONLY (rval)
2331 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2332 | TREE_READONLY (array));
2333 TREE_SIDE_EFFECTS (rval)
2334 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2335 | TREE_SIDE_EFFECTS (array));
2336 TREE_THIS_VOLATILE (rval)
2337 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2338 /* This was added by rms on 16 Nov 91.
2339 It fixes vol struct foo *a; a->elts[1]
2340 in an inline function.
2341 Hope it doesn't break something else. */
2342 | TREE_THIS_VOLATILE (array));
2343 ret = require_complete_type (rval);
2344 protected_set_expr_location (ret, loc);
2345 return ret;
2347 else
2349 tree ar = default_conversion (array);
2351 if (ar == error_mark_node)
2352 return ar;
2354 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2355 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2357 return build_indirect_ref
2358 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2359 RO_ARRAY_INDEXING);
2363 /* Build an external reference to identifier ID. FUN indicates
2364 whether this will be used for a function call. LOC is the source
2365 location of the identifier. This sets *TYPE to the type of the
2366 identifier, which is not the same as the type of the returned value
2367 for CONST_DECLs defined as enum constants. If the type of the
2368 identifier is not available, *TYPE is set to NULL. */
2369 tree
2370 build_external_ref (location_t loc, tree id, int fun, tree *type)
2372 tree ref;
2373 tree decl = lookup_name (id);
2375 /* In Objective-C, an instance variable (ivar) may be preferred to
2376 whatever lookup_name() found. */
2377 decl = objc_lookup_ivar (decl, id);
2379 *type = NULL;
2380 if (decl && decl != error_mark_node)
2382 ref = decl;
2383 *type = TREE_TYPE (ref);
2385 else if (fun)
2386 /* Implicit function declaration. */
2387 ref = implicitly_declare (loc, id);
2388 else if (decl == error_mark_node)
2389 /* Don't complain about something that's already been
2390 complained about. */
2391 return error_mark_node;
2392 else
2394 undeclared_variable (loc, id);
2395 return error_mark_node;
2398 if (TREE_TYPE (ref) == error_mark_node)
2399 return error_mark_node;
2401 if (TREE_DEPRECATED (ref))
2402 warn_deprecated_use (ref, NULL_TREE);
2404 /* Recursive call does not count as usage. */
2405 if (ref != current_function_decl)
2407 TREE_USED (ref) = 1;
2410 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2412 if (!in_sizeof && !in_typeof)
2413 C_DECL_USED (ref) = 1;
2414 else if (DECL_INITIAL (ref) == 0
2415 && DECL_EXTERNAL (ref)
2416 && !TREE_PUBLIC (ref))
2417 record_maybe_used_decl (ref);
2420 if (TREE_CODE (ref) == CONST_DECL)
2422 used_types_insert (TREE_TYPE (ref));
2424 if (warn_cxx_compat
2425 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2426 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2428 warning_at (loc, OPT_Wc___compat,
2429 ("enum constant defined in struct or union "
2430 "is not visible in C++"));
2431 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2434 ref = DECL_INITIAL (ref);
2435 TREE_CONSTANT (ref) = 1;
2437 else if (current_function_decl != 0
2438 && !DECL_FILE_SCOPE_P (current_function_decl)
2439 && (TREE_CODE (ref) == VAR_DECL
2440 || TREE_CODE (ref) == PARM_DECL
2441 || TREE_CODE (ref) == FUNCTION_DECL))
2443 tree context = decl_function_context (ref);
2445 if (context != 0 && context != current_function_decl)
2446 DECL_NONLOCAL (ref) = 1;
2448 /* C99 6.7.4p3: An inline definition of a function with external
2449 linkage ... shall not contain a reference to an identifier with
2450 internal linkage. */
2451 else if (current_function_decl != 0
2452 && DECL_DECLARED_INLINE_P (current_function_decl)
2453 && DECL_EXTERNAL (current_function_decl)
2454 && VAR_OR_FUNCTION_DECL_P (ref)
2455 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2456 && ! TREE_PUBLIC (ref)
2457 && DECL_CONTEXT (ref) != current_function_decl)
2458 record_inline_static (loc, current_function_decl, ref,
2459 csi_internal);
2461 return ref;
2464 /* Record details of decls possibly used inside sizeof or typeof. */
2465 struct maybe_used_decl
2467 /* The decl. */
2468 tree decl;
2469 /* The level seen at (in_sizeof + in_typeof). */
2470 int level;
2471 /* The next one at this level or above, or NULL. */
2472 struct maybe_used_decl *next;
2475 static struct maybe_used_decl *maybe_used_decls;
2477 /* Record that DECL, an undefined static function reference seen
2478 inside sizeof or typeof, might be used if the operand of sizeof is
2479 a VLA type or the operand of typeof is a variably modified
2480 type. */
2482 static void
2483 record_maybe_used_decl (tree decl)
2485 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2486 t->decl = decl;
2487 t->level = in_sizeof + in_typeof;
2488 t->next = maybe_used_decls;
2489 maybe_used_decls = t;
2492 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2493 USED is false, just discard them. If it is true, mark them used
2494 (if no longer inside sizeof or typeof) or move them to the next
2495 level up (if still inside sizeof or typeof). */
2497 void
2498 pop_maybe_used (bool used)
2500 struct maybe_used_decl *p = maybe_used_decls;
2501 int cur_level = in_sizeof + in_typeof;
2502 while (p && p->level > cur_level)
2504 if (used)
2506 if (cur_level == 0)
2507 C_DECL_USED (p->decl) = 1;
2508 else
2509 p->level = cur_level;
2511 p = p->next;
2513 if (!used || cur_level == 0)
2514 maybe_used_decls = p;
2517 /* Return the result of sizeof applied to EXPR. */
2519 struct c_expr
2520 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2522 struct c_expr ret;
2523 if (expr.value == error_mark_node)
2525 ret.value = error_mark_node;
2526 ret.original_code = ERROR_MARK;
2527 ret.original_type = NULL;
2528 pop_maybe_used (false);
2530 else
2532 bool expr_const_operands = true;
2533 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2534 &expr_const_operands);
2535 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2536 ret.original_code = ERROR_MARK;
2537 ret.original_type = NULL;
2538 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2540 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2541 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2542 folded_expr, ret.value);
2543 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2544 SET_EXPR_LOCATION (ret.value, loc);
2546 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2548 return ret;
2551 /* Return the result of sizeof applied to T, a structure for the type
2552 name passed to sizeof (rather than the type itself). LOC is the
2553 location of the original expression. */
2555 struct c_expr
2556 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2558 tree type;
2559 struct c_expr ret;
2560 tree type_expr = NULL_TREE;
2561 bool type_expr_const = true;
2562 type = groktypename (t, &type_expr, &type_expr_const);
2563 ret.value = c_sizeof (loc, type);
2564 ret.original_code = ERROR_MARK;
2565 ret.original_type = NULL;
2566 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2567 && c_vla_type_p (type))
2569 /* If the type is a [*] array, it is a VLA but is represented as
2570 having a size of zero. In such a case we must ensure that
2571 the result of sizeof does not get folded to a constant by
2572 c_fully_fold, because if the size is evaluated the result is
2573 not constant and so constraints on zero or negative size
2574 arrays must not be applied when this sizeof call is inside
2575 another array declarator. */
2576 if (!type_expr)
2577 type_expr = integer_zero_node;
2578 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2579 type_expr, ret.value);
2580 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2582 pop_maybe_used (type != error_mark_node
2583 ? C_TYPE_VARIABLE_SIZE (type) : false);
2584 return ret;
2587 /* Build a function call to function FUNCTION with parameters PARAMS.
2588 The function call is at LOC.
2589 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2590 TREE_VALUE of each node is a parameter-expression.
2591 FUNCTION's data type may be a function type or a pointer-to-function. */
2593 tree
2594 build_function_call (location_t loc, tree function, tree params)
2596 VEC(tree,gc) *vec;
2597 tree ret;
2599 vec = VEC_alloc (tree, gc, list_length (params));
2600 for (; params; params = TREE_CHAIN (params))
2601 VEC_quick_push (tree, vec, TREE_VALUE (params));
2602 ret = build_function_call_vec (loc, function, vec, NULL);
2603 VEC_free (tree, gc, vec);
2604 return ret;
2607 /* Build a function call to function FUNCTION with parameters PARAMS.
2608 ORIGTYPES, if not NULL, is a vector of types; each element is
2609 either NULL or the original type of the corresponding element in
2610 PARAMS. The original type may differ from TREE_TYPE of the
2611 parameter for enums. FUNCTION's data type may be a function type
2612 or pointer-to-function. This function changes the elements of
2613 PARAMS. */
2615 tree
2616 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2617 VEC(tree,gc) *origtypes)
2619 tree fntype, fundecl = 0;
2620 tree name = NULL_TREE, result;
2621 tree tem;
2622 int nargs;
2623 tree *argarray;
2626 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2627 STRIP_TYPE_NOPS (function);
2629 /* Convert anything with function type to a pointer-to-function. */
2630 if (TREE_CODE (function) == FUNCTION_DECL)
2632 /* Implement type-directed function overloading for builtins.
2633 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2634 handle all the type checking. The result is a complete expression
2635 that implements this function call. */
2636 tem = resolve_overloaded_builtin (loc, function, params);
2637 if (tem)
2638 return tem;
2640 name = DECL_NAME (function);
2641 fundecl = function;
2643 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2644 function = function_to_pointer_conversion (loc, function);
2646 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2647 expressions, like those used for ObjC messenger dispatches. */
2648 if (!VEC_empty (tree, params))
2649 function = objc_rewrite_function_call (function,
2650 VEC_index (tree, params, 0));
2652 function = c_fully_fold (function, false, NULL);
2654 fntype = TREE_TYPE (function);
2656 if (TREE_CODE (fntype) == ERROR_MARK)
2657 return error_mark_node;
2659 if (!(TREE_CODE (fntype) == POINTER_TYPE
2660 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2662 error_at (loc, "called object %qE is not a function", function);
2663 return error_mark_node;
2666 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2667 current_function_returns_abnormally = 1;
2669 /* fntype now gets the type of function pointed to. */
2670 fntype = TREE_TYPE (fntype);
2672 /* Convert the parameters to the types declared in the
2673 function prototype, or apply default promotions. */
2675 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2676 function, fundecl);
2677 if (nargs < 0)
2678 return error_mark_node;
2680 /* Check that the function is called through a compatible prototype.
2681 If it is not, replace the call by a trap, wrapped up in a compound
2682 expression if necessary. This has the nice side-effect to prevent
2683 the tree-inliner from generating invalid assignment trees which may
2684 blow up in the RTL expander later. */
2685 if (CONVERT_EXPR_P (function)
2686 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2687 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2688 && !comptypes (fntype, TREE_TYPE (tem)))
2690 tree return_type = TREE_TYPE (fntype);
2691 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2692 NULL_TREE);
2693 int i;
2695 /* This situation leads to run-time undefined behavior. We can't,
2696 therefore, simply error unless we can prove that all possible
2697 executions of the program must execute the code. */
2698 if (warning_at (loc, 0, "function called through a non-compatible type"))
2699 /* We can, however, treat "undefined" any way we please.
2700 Call abort to encourage the user to fix the program. */
2701 inform (loc, "if this code is reached, the program will abort");
2702 /* Before the abort, allow the function arguments to exit or
2703 call longjmp. */
2704 for (i = 0; i < nargs; i++)
2705 trap = build2 (COMPOUND_EXPR, void_type_node,
2706 VEC_index (tree, params, i), trap);
2708 if (VOID_TYPE_P (return_type))
2710 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2711 pedwarn (loc, 0,
2712 "function with qualified void return type called");
2713 return trap;
2715 else
2717 tree rhs;
2719 if (AGGREGATE_TYPE_P (return_type))
2720 rhs = build_compound_literal (loc, return_type,
2721 build_constructor (return_type, 0),
2722 false);
2723 else
2724 rhs = fold_convert_loc (loc, return_type, integer_zero_node);
2726 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2727 trap, rhs));
2731 argarray = VEC_address (tree, params);
2733 /* Check that arguments to builtin functions match the expectations. */
2734 if (fundecl
2735 && DECL_BUILT_IN (fundecl)
2736 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2737 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2738 return error_mark_node;
2740 /* Check that the arguments to the function are valid. */
2741 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2742 TYPE_ARG_TYPES (fntype));
2744 if (name != NULL_TREE
2745 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2747 if (require_constant_value)
2748 result =
2749 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2750 function, nargs, argarray);
2751 else
2752 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2753 function, nargs, argarray);
2754 if (TREE_CODE (result) == NOP_EXPR
2755 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2756 STRIP_TYPE_NOPS (result);
2758 else
2759 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2760 function, nargs, argarray);
2762 if (VOID_TYPE_P (TREE_TYPE (result)))
2764 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2765 pedwarn (loc, 0,
2766 "function with qualified void return type called");
2767 return result;
2769 return require_complete_type (result);
2772 /* Convert the argument expressions in the vector VALUES
2773 to the types in the list TYPELIST.
2775 If TYPELIST is exhausted, or when an element has NULL as its type,
2776 perform the default conversions.
2778 ORIGTYPES is the original types of the expressions in VALUES. This
2779 holds the type of enum values which have been converted to integral
2780 types. It may be NULL.
2782 FUNCTION is a tree for the called function. It is used only for
2783 error messages, where it is formatted with %qE.
2785 This is also where warnings about wrong number of args are generated.
2787 Returns the actual number of arguments processed (which may be less
2788 than the length of VALUES in some error situations), or -1 on
2789 failure. */
2791 static int
2792 convert_arguments (tree typelist, VEC(tree,gc) *values,
2793 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2795 tree typetail, val;
2796 unsigned int parmnum;
2797 bool error_args = false;
2798 const bool type_generic = fundecl
2799 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2800 bool type_generic_remove_excess_precision = false;
2801 tree selector;
2803 /* Change pointer to function to the function itself for
2804 diagnostics. */
2805 if (TREE_CODE (function) == ADDR_EXPR
2806 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2807 function = TREE_OPERAND (function, 0);
2809 /* Handle an ObjC selector specially for diagnostics. */
2810 selector = objc_message_selector ();
2812 /* For type-generic built-in functions, determine whether excess
2813 precision should be removed (classification) or not
2814 (comparison). */
2815 if (type_generic
2816 && DECL_BUILT_IN (fundecl)
2817 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2819 switch (DECL_FUNCTION_CODE (fundecl))
2821 case BUILT_IN_ISFINITE:
2822 case BUILT_IN_ISINF:
2823 case BUILT_IN_ISINF_SIGN:
2824 case BUILT_IN_ISNAN:
2825 case BUILT_IN_ISNORMAL:
2826 case BUILT_IN_FPCLASSIFY:
2827 type_generic_remove_excess_precision = true;
2828 break;
2830 default:
2831 type_generic_remove_excess_precision = false;
2832 break;
2836 /* Scan the given expressions and types, producing individual
2837 converted arguments. */
2839 for (typetail = typelist, parmnum = 0;
2840 VEC_iterate (tree, values, parmnum, val);
2841 ++parmnum)
2843 tree type = typetail ? TREE_VALUE (typetail) : 0;
2844 tree valtype = TREE_TYPE (val);
2845 tree rname = function;
2846 int argnum = parmnum + 1;
2847 const char *invalid_func_diag;
2848 bool excess_precision = false;
2849 bool npc;
2850 tree parmval;
2852 if (type == void_type_node)
2854 error_at (input_location,
2855 "too many arguments to function %qE", function);
2856 if (fundecl && !DECL_BUILT_IN (fundecl))
2857 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2858 return parmnum;
2861 if (selector && argnum > 2)
2863 rname = selector;
2864 argnum -= 2;
2867 npc = null_pointer_constant_p (val);
2869 /* If there is excess precision and a prototype, convert once to
2870 the required type rather than converting via the semantic
2871 type. Likewise without a prototype a float value represented
2872 as long double should be converted once to double. But for
2873 type-generic classification functions excess precision must
2874 be removed here. */
2875 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2876 && (type || !type_generic || !type_generic_remove_excess_precision))
2878 val = TREE_OPERAND (val, 0);
2879 excess_precision = true;
2881 val = c_fully_fold (val, false, NULL);
2882 STRIP_TYPE_NOPS (val);
2884 val = require_complete_type (val);
2886 if (type != 0)
2888 /* Formal parm type is specified by a function prototype. */
2890 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2892 error ("type of formal parameter %d is incomplete", parmnum + 1);
2893 parmval = val;
2895 else
2897 tree origtype;
2899 /* Optionally warn about conversions that
2900 differ from the default conversions. */
2901 if (warn_traditional_conversion || warn_traditional)
2903 unsigned int formal_prec = TYPE_PRECISION (type);
2905 if (INTEGRAL_TYPE_P (type)
2906 && TREE_CODE (valtype) == REAL_TYPE)
2907 warning (0, "passing argument %d of %qE as integer "
2908 "rather than floating due to prototype",
2909 argnum, rname);
2910 if (INTEGRAL_TYPE_P (type)
2911 && TREE_CODE (valtype) == COMPLEX_TYPE)
2912 warning (0, "passing argument %d of %qE as integer "
2913 "rather than complex due to prototype",
2914 argnum, rname);
2915 else if (TREE_CODE (type) == COMPLEX_TYPE
2916 && TREE_CODE (valtype) == REAL_TYPE)
2917 warning (0, "passing argument %d of %qE as complex "
2918 "rather than floating due to prototype",
2919 argnum, rname);
2920 else if (TREE_CODE (type) == REAL_TYPE
2921 && INTEGRAL_TYPE_P (valtype))
2922 warning (0, "passing argument %d of %qE as floating "
2923 "rather than integer due to prototype",
2924 argnum, rname);
2925 else if (TREE_CODE (type) == COMPLEX_TYPE
2926 && INTEGRAL_TYPE_P (valtype))
2927 warning (0, "passing argument %d of %qE as complex "
2928 "rather than integer due to prototype",
2929 argnum, rname);
2930 else if (TREE_CODE (type) == REAL_TYPE
2931 && TREE_CODE (valtype) == COMPLEX_TYPE)
2932 warning (0, "passing argument %d of %qE as floating "
2933 "rather than complex due to prototype",
2934 argnum, rname);
2935 /* ??? At some point, messages should be written about
2936 conversions between complex types, but that's too messy
2937 to do now. */
2938 else if (TREE_CODE (type) == REAL_TYPE
2939 && TREE_CODE (valtype) == REAL_TYPE)
2941 /* Warn if any argument is passed as `float',
2942 since without a prototype it would be `double'. */
2943 if (formal_prec == TYPE_PRECISION (float_type_node)
2944 && type != dfloat32_type_node)
2945 warning (0, "passing argument %d of %qE as %<float%> "
2946 "rather than %<double%> due to prototype",
2947 argnum, rname);
2949 /* Warn if mismatch between argument and prototype
2950 for decimal float types. Warn of conversions with
2951 binary float types and of precision narrowing due to
2952 prototype. */
2953 else if (type != valtype
2954 && (type == dfloat32_type_node
2955 || type == dfloat64_type_node
2956 || type == dfloat128_type_node
2957 || valtype == dfloat32_type_node
2958 || valtype == dfloat64_type_node
2959 || valtype == dfloat128_type_node)
2960 && (formal_prec
2961 <= TYPE_PRECISION (valtype)
2962 || (type == dfloat128_type_node
2963 && (valtype
2964 != dfloat64_type_node
2965 && (valtype
2966 != dfloat32_type_node)))
2967 || (type == dfloat64_type_node
2968 && (valtype
2969 != dfloat32_type_node))))
2970 warning (0, "passing argument %d of %qE as %qT "
2971 "rather than %qT due to prototype",
2972 argnum, rname, type, valtype);
2975 /* Detect integer changing in width or signedness.
2976 These warnings are only activated with
2977 -Wtraditional-conversion, not with -Wtraditional. */
2978 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
2979 && INTEGRAL_TYPE_P (valtype))
2981 tree would_have_been = default_conversion (val);
2982 tree type1 = TREE_TYPE (would_have_been);
2984 if (TREE_CODE (type) == ENUMERAL_TYPE
2985 && (TYPE_MAIN_VARIANT (type)
2986 == TYPE_MAIN_VARIANT (valtype)))
2987 /* No warning if function asks for enum
2988 and the actual arg is that enum type. */
2990 else if (formal_prec != TYPE_PRECISION (type1))
2991 warning (OPT_Wtraditional_conversion,
2992 "passing argument %d of %qE "
2993 "with different width due to prototype",
2994 argnum, rname);
2995 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2997 /* Don't complain if the formal parameter type
2998 is an enum, because we can't tell now whether
2999 the value was an enum--even the same enum. */
3000 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3002 else if (TREE_CODE (val) == INTEGER_CST
3003 && int_fits_type_p (val, type))
3004 /* Change in signedness doesn't matter
3005 if a constant value is unaffected. */
3007 /* If the value is extended from a narrower
3008 unsigned type, it doesn't matter whether we
3009 pass it as signed or unsigned; the value
3010 certainly is the same either way. */
3011 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3012 && TYPE_UNSIGNED (valtype))
3014 else if (TYPE_UNSIGNED (type))
3015 warning (OPT_Wtraditional_conversion,
3016 "passing argument %d of %qE "
3017 "as unsigned due to prototype",
3018 argnum, rname);
3019 else
3020 warning (OPT_Wtraditional_conversion,
3021 "passing argument %d of %qE "
3022 "as signed due to prototype", argnum, rname);
3026 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3027 sake of better warnings from convert_and_check. */
3028 if (excess_precision)
3029 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3030 origtype = (origtypes == NULL
3031 ? NULL_TREE
3032 : VEC_index (tree, origtypes, parmnum));
3033 parmval = convert_for_assignment (input_location, type, val,
3034 origtype, ic_argpass, npc,
3035 fundecl, function,
3036 parmnum + 1);
3038 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3039 && INTEGRAL_TYPE_P (type)
3040 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3041 parmval = default_conversion (parmval);
3044 else if (TREE_CODE (valtype) == REAL_TYPE
3045 && (TYPE_PRECISION (valtype)
3046 < TYPE_PRECISION (double_type_node))
3047 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3049 if (type_generic)
3050 parmval = val;
3051 else
3052 /* Convert `float' to `double'. */
3053 parmval = convert (double_type_node, val);
3055 else if (excess_precision && !type_generic)
3056 /* A "double" argument with excess precision being passed
3057 without a prototype or in variable arguments. */
3058 parmval = convert (valtype, val);
3059 else if ((invalid_func_diag =
3060 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3062 error (invalid_func_diag);
3063 return -1;
3065 else
3066 /* Convert `short' and `char' to full-size `int'. */
3067 parmval = default_conversion (val);
3069 VEC_replace (tree, values, parmnum, parmval);
3070 if (parmval == error_mark_node)
3071 error_args = true;
3073 if (typetail)
3074 typetail = TREE_CHAIN (typetail);
3077 gcc_assert (parmnum == VEC_length (tree, values));
3079 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3081 error_at (input_location,
3082 "too few arguments to function %qE", function);
3083 if (fundecl && !DECL_BUILT_IN (fundecl))
3084 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3085 return -1;
3088 return error_args ? -1 : (int) parmnum;
3091 /* This is the entry point used by the parser to build unary operators
3092 in the input. CODE, a tree_code, specifies the unary operator, and
3093 ARG is the operand. For unary plus, the C parser currently uses
3094 CONVERT_EXPR for code.
3096 LOC is the location to use for the tree generated.
3099 struct c_expr
3100 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3102 struct c_expr result;
3104 result.value = build_unary_op (loc, code, arg.value, 0);
3105 result.original_code = code;
3106 result.original_type = NULL;
3108 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3109 overflow_warning (loc, result.value);
3111 return result;
3114 /* This is the entry point used by the parser to build binary operators
3115 in the input. CODE, a tree_code, specifies the binary operator, and
3116 ARG1 and ARG2 are the operands. In addition to constructing the
3117 expression, we check for operands that were written with other binary
3118 operators in a way that is likely to confuse the user.
3120 LOCATION is the location of the binary operator. */
3122 struct c_expr
3123 parser_build_binary_op (location_t location, enum tree_code code,
3124 struct c_expr arg1, struct c_expr arg2)
3126 struct c_expr result;
3128 enum tree_code code1 = arg1.original_code;
3129 enum tree_code code2 = arg2.original_code;
3130 tree type1 = (arg1.original_type
3131 ? arg1.original_type
3132 : TREE_TYPE (arg1.value));
3133 tree type2 = (arg2.original_type
3134 ? arg2.original_type
3135 : TREE_TYPE (arg2.value));
3137 result.value = build_binary_op (location, code,
3138 arg1.value, arg2.value, 1);
3139 result.original_code = code;
3140 result.original_type = NULL;
3142 if (TREE_CODE (result.value) == ERROR_MARK)
3143 return result;
3145 if (location != UNKNOWN_LOCATION)
3146 protected_set_expr_location (result.value, location);
3148 /* Check for cases such as x+y<<z which users are likely
3149 to misinterpret. */
3150 if (warn_parentheses)
3151 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3153 if (warn_logical_op)
3154 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3155 code1, arg1.value, code2, arg2.value);
3157 /* Warn about comparisons against string literals, with the exception
3158 of testing for equality or inequality of a string literal with NULL. */
3159 if (code == EQ_EXPR || code == NE_EXPR)
3161 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3162 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3163 warning_at (location, OPT_Waddress,
3164 "comparison with string literal results in unspecified behavior");
3166 else if (TREE_CODE_CLASS (code) == tcc_comparison
3167 && (code1 == STRING_CST || code2 == STRING_CST))
3168 warning_at (location, OPT_Waddress,
3169 "comparison with string literal results in unspecified behavior");
3171 if (TREE_OVERFLOW_P (result.value)
3172 && !TREE_OVERFLOW_P (arg1.value)
3173 && !TREE_OVERFLOW_P (arg2.value))
3174 overflow_warning (location, result.value);
3176 /* Warn about comparisons of different enum types. */
3177 if (warn_enum_compare
3178 && TREE_CODE_CLASS (code) == tcc_comparison
3179 && TREE_CODE (type1) == ENUMERAL_TYPE
3180 && TREE_CODE (type2) == ENUMERAL_TYPE
3181 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3182 warning_at (location, OPT_Wenum_compare,
3183 "comparison between %qT and %qT",
3184 type1, type2);
3186 return result;
3189 /* Return a tree for the difference of pointers OP0 and OP1.
3190 The resulting tree has type int. */
3192 static tree
3193 pointer_diff (location_t loc, tree op0, tree op1)
3195 tree restype = ptrdiff_type_node;
3196 tree result, inttype;
3198 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3199 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3200 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3201 tree con0, con1, lit0, lit1;
3202 tree orig_op1 = op1;
3204 /* If the operands point into different address spaces, we need to
3205 explicitly convert them to pointers into the common address space
3206 before we can subtract the numerical address values. */
3207 if (as0 != as1)
3209 addr_space_t as_common;
3210 tree common_type;
3212 /* Determine the common superset address space. This is guaranteed
3213 to exist because the caller verified that comp_target_types
3214 returned non-zero. */
3215 if (!addr_space_superset (as0, as1, &as_common))
3216 gcc_unreachable ();
3218 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3219 op0 = convert (common_type, op0);
3220 op1 = convert (common_type, op1);
3223 /* Determine integer type to perform computations in. This will usually
3224 be the same as the result type (ptrdiff_t), but may need to be a wider
3225 type if pointers for the address space are wider than ptrdiff_t. */
3226 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3227 inttype = lang_hooks.types.type_for_size
3228 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3229 else
3230 inttype = restype;
3233 if (TREE_CODE (target_type) == VOID_TYPE)
3234 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3235 "pointer of type %<void *%> used in subtraction");
3236 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3237 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3238 "pointer to a function used in subtraction");
3240 /* If the conversion to ptrdiff_type does anything like widening or
3241 converting a partial to an integral mode, we get a convert_expression
3242 that is in the way to do any simplifications.
3243 (fold-const.c doesn't know that the extra bits won't be needed.
3244 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3245 different mode in place.)
3246 So first try to find a common term here 'by hand'; we want to cover
3247 at least the cases that occur in legal static initializers. */
3248 if (CONVERT_EXPR_P (op0)
3249 && (TYPE_PRECISION (TREE_TYPE (op0))
3250 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3251 con0 = TREE_OPERAND (op0, 0);
3252 else
3253 con0 = op0;
3254 if (CONVERT_EXPR_P (op1)
3255 && (TYPE_PRECISION (TREE_TYPE (op1))
3256 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3257 con1 = TREE_OPERAND (op1, 0);
3258 else
3259 con1 = op1;
3261 if (TREE_CODE (con0) == PLUS_EXPR)
3263 lit0 = TREE_OPERAND (con0, 1);
3264 con0 = TREE_OPERAND (con0, 0);
3266 else
3267 lit0 = integer_zero_node;
3269 if (TREE_CODE (con1) == PLUS_EXPR)
3271 lit1 = TREE_OPERAND (con1, 1);
3272 con1 = TREE_OPERAND (con1, 0);
3274 else
3275 lit1 = integer_zero_node;
3277 if (operand_equal_p (con0, con1, 0))
3279 op0 = lit0;
3280 op1 = lit1;
3284 /* First do the subtraction as integers;
3285 then drop through to build the divide operator.
3286 Do not do default conversions on the minus operator
3287 in case restype is a short type. */
3289 op0 = build_binary_op (loc,
3290 MINUS_EXPR, convert (inttype, op0),
3291 convert (inttype, op1), 0);
3292 /* This generates an error if op1 is pointer to incomplete type. */
3293 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3294 error_at (loc, "arithmetic on pointer to an incomplete type");
3296 /* This generates an error if op0 is pointer to incomplete type. */
3297 op1 = c_size_in_bytes (target_type);
3299 /* Divide by the size, in easiest possible way. */
3300 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3301 op0, convert (inttype, op1));
3303 /* Convert to final result type if necessary. */
3304 return convert (restype, result);
3307 /* Construct and perhaps optimize a tree representation
3308 for a unary operation. CODE, a tree_code, specifies the operation
3309 and XARG is the operand.
3310 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3311 the default promotions (such as from short to int).
3312 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3313 allows non-lvalues; this is only used to handle conversion of non-lvalue
3314 arrays to pointers in C99.
3316 LOCATION is the location of the operator. */
3318 tree
3319 build_unary_op (location_t location,
3320 enum tree_code code, tree xarg, int flag)
3322 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3323 tree arg = xarg;
3324 tree argtype = 0;
3325 enum tree_code typecode;
3326 tree val;
3327 tree ret = error_mark_node;
3328 tree eptype = NULL_TREE;
3329 int noconvert = flag;
3330 const char *invalid_op_diag;
3331 bool int_operands;
3333 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3334 if (int_operands)
3335 arg = remove_c_maybe_const_expr (arg);
3337 if (code != ADDR_EXPR)
3338 arg = require_complete_type (arg);
3340 typecode = TREE_CODE (TREE_TYPE (arg));
3341 if (typecode == ERROR_MARK)
3342 return error_mark_node;
3343 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3344 typecode = INTEGER_TYPE;
3346 if ((invalid_op_diag
3347 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3349 error_at (location, invalid_op_diag);
3350 return error_mark_node;
3353 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3355 eptype = TREE_TYPE (arg);
3356 arg = TREE_OPERAND (arg, 0);
3359 switch (code)
3361 case CONVERT_EXPR:
3362 /* This is used for unary plus, because a CONVERT_EXPR
3363 is enough to prevent anybody from looking inside for
3364 associativity, but won't generate any code. */
3365 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3366 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3367 || typecode == VECTOR_TYPE))
3369 error_at (location, "wrong type argument to unary plus");
3370 return error_mark_node;
3372 else if (!noconvert)
3373 arg = default_conversion (arg);
3374 arg = non_lvalue_loc (location, arg);
3375 break;
3377 case NEGATE_EXPR:
3378 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3379 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3380 || typecode == VECTOR_TYPE))
3382 error_at (location, "wrong type argument to unary minus");
3383 return error_mark_node;
3385 else if (!noconvert)
3386 arg = default_conversion (arg);
3387 break;
3389 case BIT_NOT_EXPR:
3390 /* ~ works on integer types and non float vectors. */
3391 if (typecode == INTEGER_TYPE
3392 || (typecode == VECTOR_TYPE
3393 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3395 if (!noconvert)
3396 arg = default_conversion (arg);
3398 else if (typecode == COMPLEX_TYPE)
3400 code = CONJ_EXPR;
3401 pedwarn (location, OPT_pedantic,
3402 "ISO C does not support %<~%> for complex conjugation");
3403 if (!noconvert)
3404 arg = default_conversion (arg);
3406 else
3408 error_at (location, "wrong type argument to bit-complement");
3409 return error_mark_node;
3411 break;
3413 case ABS_EXPR:
3414 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3416 error_at (location, "wrong type argument to abs");
3417 return error_mark_node;
3419 else if (!noconvert)
3420 arg = default_conversion (arg);
3421 break;
3423 case CONJ_EXPR:
3424 /* Conjugating a real value is a no-op, but allow it anyway. */
3425 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3426 || typecode == COMPLEX_TYPE))
3428 error_at (location, "wrong type argument to conjugation");
3429 return error_mark_node;
3431 else if (!noconvert)
3432 arg = default_conversion (arg);
3433 break;
3435 case TRUTH_NOT_EXPR:
3436 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3437 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3438 && typecode != COMPLEX_TYPE)
3440 error_at (location,
3441 "wrong type argument to unary exclamation mark");
3442 return error_mark_node;
3444 arg = c_objc_common_truthvalue_conversion (location, arg);
3445 ret = invert_truthvalue_loc (location, arg);
3446 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3447 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3448 location = EXPR_LOCATION (ret);
3449 goto return_build_unary_op;
3451 case REALPART_EXPR:
3452 if (TREE_CODE (arg) == COMPLEX_CST)
3453 ret = TREE_REALPART (arg);
3454 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3455 ret = fold_build1_loc (location,
3456 REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3457 else
3458 ret = arg;
3459 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3460 eptype = TREE_TYPE (eptype);
3461 goto return_build_unary_op;
3463 case IMAGPART_EXPR:
3464 if (TREE_CODE (arg) == COMPLEX_CST)
3465 ret = TREE_IMAGPART (arg);
3466 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3467 ret = fold_build1_loc (location,
3468 IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3469 else
3470 ret = omit_one_operand_loc (location, TREE_TYPE (arg),
3471 integer_zero_node, arg);
3472 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3473 eptype = TREE_TYPE (eptype);
3474 goto return_build_unary_op;
3476 case PREINCREMENT_EXPR:
3477 case POSTINCREMENT_EXPR:
3478 case PREDECREMENT_EXPR:
3479 case POSTDECREMENT_EXPR:
3481 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3483 tree inner = build_unary_op (location, code,
3484 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3485 if (inner == error_mark_node)
3486 return error_mark_node;
3487 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3488 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3489 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3490 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3491 goto return_build_unary_op;
3494 /* Complain about anything that is not a true lvalue. */
3495 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3496 || code == POSTINCREMENT_EXPR)
3497 ? lv_increment
3498 : lv_decrement)))
3499 return error_mark_node;
3501 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3503 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3504 warning_at (location, OPT_Wc___compat,
3505 "increment of enumeration value is invalid in C++");
3506 else
3507 warning_at (location, OPT_Wc___compat,
3508 "decrement of enumeration value is invalid in C++");
3511 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3512 arg = c_fully_fold (arg, false, NULL);
3514 /* Increment or decrement the real part of the value,
3515 and don't change the imaginary part. */
3516 if (typecode == COMPLEX_TYPE)
3518 tree real, imag;
3520 pedwarn (location, OPT_pedantic,
3521 "ISO C does not support %<++%> and %<--%> on complex types");
3523 arg = stabilize_reference (arg);
3524 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3525 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3526 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3527 if (real == error_mark_node || imag == error_mark_node)
3528 return error_mark_node;
3529 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3530 real, imag);
3531 goto return_build_unary_op;
3534 /* Report invalid types. */
3536 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3537 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3539 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3540 error_at (location, "wrong type argument to increment");
3541 else
3542 error_at (location, "wrong type argument to decrement");
3544 return error_mark_node;
3548 tree inc;
3550 argtype = TREE_TYPE (arg);
3552 /* Compute the increment. */
3554 if (typecode == POINTER_TYPE)
3556 /* If pointer target is an undefined struct,
3557 we just cannot know how to do the arithmetic. */
3558 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3560 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3561 error_at (location,
3562 "increment of pointer to unknown structure");
3563 else
3564 error_at (location,
3565 "decrement of pointer to unknown structure");
3567 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3568 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3570 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3571 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3572 "wrong type argument to increment");
3573 else
3574 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3575 "wrong type argument to decrement");
3578 inc = c_size_in_bytes (TREE_TYPE (argtype));
3579 inc = fold_convert_loc (location, sizetype, inc);
3581 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3583 /* For signed fract types, we invert ++ to -- or
3584 -- to ++, and change inc from 1 to -1, because
3585 it is not possible to represent 1 in signed fract constants.
3586 For unsigned fract types, the result always overflows and
3587 we get an undefined (original) or the maximum value. */
3588 if (code == PREINCREMENT_EXPR)
3589 code = PREDECREMENT_EXPR;
3590 else if (code == PREDECREMENT_EXPR)
3591 code = PREINCREMENT_EXPR;
3592 else if (code == POSTINCREMENT_EXPR)
3593 code = POSTDECREMENT_EXPR;
3594 else /* code == POSTDECREMENT_EXPR */
3595 code = POSTINCREMENT_EXPR;
3597 inc = integer_minus_one_node;
3598 inc = convert (argtype, inc);
3600 else
3602 inc = integer_one_node;
3603 inc = convert (argtype, inc);
3606 /* Report a read-only lvalue. */
3607 if (TYPE_READONLY (argtype))
3609 readonly_error (arg,
3610 ((code == PREINCREMENT_EXPR
3611 || code == POSTINCREMENT_EXPR)
3612 ? lv_increment : lv_decrement));
3613 return error_mark_node;
3615 else if (TREE_READONLY (arg))
3616 readonly_warning (arg,
3617 ((code == PREINCREMENT_EXPR
3618 || code == POSTINCREMENT_EXPR)
3619 ? lv_increment : lv_decrement));
3621 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3622 val = boolean_increment (code, arg);
3623 else
3624 val = build2 (code, TREE_TYPE (arg), arg, inc);
3625 TREE_SIDE_EFFECTS (val) = 1;
3626 if (TREE_CODE (val) != code)
3627 TREE_NO_WARNING (val) = 1;
3628 ret = val;
3629 goto return_build_unary_op;
3632 case ADDR_EXPR:
3633 /* Note that this operation never does default_conversion. */
3635 /* The operand of unary '&' must be an lvalue (which excludes
3636 expressions of type void), or, in C99, the result of a [] or
3637 unary '*' operator. */
3638 if (VOID_TYPE_P (TREE_TYPE (arg))
3639 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3640 && (TREE_CODE (arg) != INDIRECT_REF
3641 || !flag_isoc99))
3642 pedwarn (location, 0, "taking address of expression of type %<void%>");
3644 /* Let &* cancel out to simplify resulting code. */
3645 if (TREE_CODE (arg) == INDIRECT_REF)
3647 /* Don't let this be an lvalue. */
3648 if (lvalue_p (TREE_OPERAND (arg, 0)))
3649 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3650 ret = TREE_OPERAND (arg, 0);
3651 goto return_build_unary_op;
3654 /* For &x[y], return x+y */
3655 if (TREE_CODE (arg) == ARRAY_REF)
3657 tree op0 = TREE_OPERAND (arg, 0);
3658 if (!c_mark_addressable (op0))
3659 return error_mark_node;
3660 return build_binary_op (location, PLUS_EXPR,
3661 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3662 ? array_to_pointer_conversion (location,
3663 op0)
3664 : op0),
3665 TREE_OPERAND (arg, 1), 1);
3668 /* Anything not already handled and not a true memory reference
3669 or a non-lvalue array is an error. */
3670 else if (typecode != FUNCTION_TYPE && !flag
3671 && !lvalue_or_else (arg, lv_addressof))
3672 return error_mark_node;
3674 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3675 folding later. */
3676 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3678 tree inner = build_unary_op (location, code,
3679 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3680 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3681 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3682 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3683 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3684 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3685 goto return_build_unary_op;
3688 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3689 argtype = TREE_TYPE (arg);
3691 /* If the lvalue is const or volatile, merge that into the type
3692 to which the address will point. Note that you can't get a
3693 restricted pointer by taking the address of something, so we
3694 only have to deal with `const' and `volatile' here. */
3695 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3696 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3697 argtype = c_build_type_variant (argtype,
3698 TREE_READONLY (arg),
3699 TREE_THIS_VOLATILE (arg));
3701 if (!c_mark_addressable (arg))
3702 return error_mark_node;
3704 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3705 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3707 argtype = build_pointer_type (argtype);
3709 /* ??? Cope with user tricks that amount to offsetof. Delete this
3710 when we have proper support for integer constant expressions. */
3711 val = get_base_address (arg);
3712 if (val && TREE_CODE (val) == INDIRECT_REF
3713 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3715 tree op0 = fold_convert_loc (location, sizetype,
3716 fold_offsetof (arg, val)), op1;
3718 op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3719 ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3720 goto return_build_unary_op;
3723 val = build1 (ADDR_EXPR, argtype, arg);
3725 ret = val;
3726 goto return_build_unary_op;
3728 default:
3729 gcc_unreachable ();
3732 if (argtype == 0)
3733 argtype = TREE_TYPE (arg);
3734 if (TREE_CODE (arg) == INTEGER_CST)
3735 ret = (require_constant_value
3736 ? fold_build1_initializer_loc (location, code, argtype, arg)
3737 : fold_build1_loc (location, code, argtype, arg));
3738 else
3739 ret = build1 (code, argtype, arg);
3740 return_build_unary_op:
3741 gcc_assert (ret != error_mark_node);
3742 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3743 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3744 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3745 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3746 ret = note_integer_operands (ret);
3747 if (eptype)
3748 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3749 protected_set_expr_location (ret, location);
3750 return ret;
3753 /* Return nonzero if REF is an lvalue valid for this language.
3754 Lvalues can be assigned, unless their type has TYPE_READONLY.
3755 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3757 bool
3758 lvalue_p (const_tree ref)
3760 const enum tree_code code = TREE_CODE (ref);
3762 switch (code)
3764 case REALPART_EXPR:
3765 case IMAGPART_EXPR:
3766 case COMPONENT_REF:
3767 return lvalue_p (TREE_OPERAND (ref, 0));
3769 case C_MAYBE_CONST_EXPR:
3770 return lvalue_p (TREE_OPERAND (ref, 1));
3772 case COMPOUND_LITERAL_EXPR:
3773 case STRING_CST:
3774 return 1;
3776 case INDIRECT_REF:
3777 case ARRAY_REF:
3778 case VAR_DECL:
3779 case PARM_DECL:
3780 case RESULT_DECL:
3781 case ERROR_MARK:
3782 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3783 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3785 case BIND_EXPR:
3786 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3788 default:
3789 return 0;
3793 /* Give an error for storing in something that is 'const'. */
3795 static void
3796 readonly_error (tree arg, enum lvalue_use use)
3798 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3799 || use == lv_asm);
3800 /* Using this macro rather than (for example) arrays of messages
3801 ensures that all the format strings are checked at compile
3802 time. */
3803 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3804 : (use == lv_increment ? (I) \
3805 : (use == lv_decrement ? (D) : (AS))))
3806 if (TREE_CODE (arg) == COMPONENT_REF)
3808 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3809 readonly_error (TREE_OPERAND (arg, 0), use);
3810 else
3811 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3812 G_("increment of read-only member %qD"),
3813 G_("decrement of read-only member %qD"),
3814 G_("read-only member %qD used as %<asm%> output")),
3815 TREE_OPERAND (arg, 1));
3817 else if (TREE_CODE (arg) == VAR_DECL)
3818 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3819 G_("increment of read-only variable %qD"),
3820 G_("decrement of read-only variable %qD"),
3821 G_("read-only variable %qD used as %<asm%> output")),
3822 arg);
3823 else
3824 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3825 G_("increment of read-only location %qE"),
3826 G_("decrement of read-only location %qE"),
3827 G_("read-only location %qE used as %<asm%> output")),
3828 arg);
3831 /* Give a warning for storing in something that is read-only in GCC
3832 terms but not const in ISO C terms. */
3834 static void
3835 readonly_warning (tree arg, enum lvalue_use use)
3837 switch (use)
3839 case lv_assign:
3840 warning (0, "assignment of read-only location %qE", arg);
3841 break;
3842 case lv_increment:
3843 warning (0, "increment of read-only location %qE", arg);
3844 break;
3845 case lv_decrement:
3846 warning (0, "decrement of read-only location %qE", arg);
3847 break;
3848 default:
3849 gcc_unreachable ();
3851 return;
3855 /* Return nonzero if REF is an lvalue valid for this language;
3856 otherwise, print an error message and return zero. USE says
3857 how the lvalue is being used and so selects the error message. */
3859 static int
3860 lvalue_or_else (const_tree ref, enum lvalue_use use)
3862 int win = lvalue_p (ref);
3864 if (!win)
3865 lvalue_error (use);
3867 return win;
3870 /* Mark EXP saying that we need to be able to take the
3871 address of it; it should not be allocated in a register.
3872 Returns true if successful. */
3874 bool
3875 c_mark_addressable (tree exp)
3877 tree x = exp;
3879 while (1)
3880 switch (TREE_CODE (x))
3882 case COMPONENT_REF:
3883 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3885 error
3886 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3887 return false;
3890 /* ... fall through ... */
3892 case ADDR_EXPR:
3893 case ARRAY_REF:
3894 case REALPART_EXPR:
3895 case IMAGPART_EXPR:
3896 x = TREE_OPERAND (x, 0);
3897 break;
3899 case COMPOUND_LITERAL_EXPR:
3900 case CONSTRUCTOR:
3901 TREE_ADDRESSABLE (x) = 1;
3902 return true;
3904 case VAR_DECL:
3905 case CONST_DECL:
3906 case PARM_DECL:
3907 case RESULT_DECL:
3908 if (C_DECL_REGISTER (x)
3909 && DECL_NONLOCAL (x))
3911 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3913 error
3914 ("global register variable %qD used in nested function", x);
3915 return false;
3917 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3919 else if (C_DECL_REGISTER (x))
3921 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3922 error ("address of global register variable %qD requested", x);
3923 else
3924 error ("address of register variable %qD requested", x);
3925 return false;
3928 /* drops in */
3929 case FUNCTION_DECL:
3930 TREE_ADDRESSABLE (x) = 1;
3931 /* drops out */
3932 default:
3933 return true;
3937 /* Convert EXPR to TYPE, warning about conversion problems with
3938 constants. SEMANTIC_TYPE is the type this conversion would use
3939 without excess precision. If SEMANTIC_TYPE is NULL, this function
3940 is equivalent to convert_and_check. This function is a wrapper that
3941 handles conversions that may be different than
3942 the usual ones because of excess precision. */
3944 static tree
3945 ep_convert_and_check (tree type, tree expr, tree semantic_type)
3947 if (TREE_TYPE (expr) == type)
3948 return expr;
3950 if (!semantic_type)
3951 return convert_and_check (type, expr);
3953 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3954 && TREE_TYPE (expr) != semantic_type)
3956 /* For integers, we need to check the real conversion, not
3957 the conversion to the excess precision type. */
3958 expr = convert_and_check (semantic_type, expr);
3960 /* Result type is the excess precision type, which should be
3961 large enough, so do not check. */
3962 return convert (type, expr);
3965 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
3966 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
3967 if folded to an integer constant then the unselected half may
3968 contain arbitrary operations not normally permitted in constant
3969 expressions. Set the location of the expression to LOC. */
3971 tree
3972 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
3973 tree op1, tree op1_original_type, tree op2,
3974 tree op2_original_type)
3976 tree type1;
3977 tree type2;
3978 enum tree_code code1;
3979 enum tree_code code2;
3980 tree result_type = NULL;
3981 tree semantic_result_type = NULL;
3982 tree orig_op1 = op1, orig_op2 = op2;
3983 bool int_const, op1_int_operands, op2_int_operands, int_operands;
3984 bool ifexp_int_operands;
3985 tree ret;
3986 bool objc_ok;
3988 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
3989 if (op1_int_operands)
3990 op1 = remove_c_maybe_const_expr (op1);
3991 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
3992 if (op2_int_operands)
3993 op2 = remove_c_maybe_const_expr (op2);
3994 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
3995 if (ifexp_int_operands)
3996 ifexp = remove_c_maybe_const_expr (ifexp);
3998 /* Promote both alternatives. */
4000 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4001 op1 = default_conversion (op1);
4002 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4003 op2 = default_conversion (op2);
4005 if (TREE_CODE (ifexp) == ERROR_MARK
4006 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4007 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4008 return error_mark_node;
4010 type1 = TREE_TYPE (op1);
4011 code1 = TREE_CODE (type1);
4012 type2 = TREE_TYPE (op2);
4013 code2 = TREE_CODE (type2);
4015 /* C90 does not permit non-lvalue arrays in conditional expressions.
4016 In C99 they will be pointers by now. */
4017 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4019 error_at (colon_loc, "non-lvalue array in conditional expression");
4020 return error_mark_node;
4023 objc_ok = objc_compare_types (type1, type2, -3, NULL_TREE);
4025 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4026 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4027 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4028 || code1 == COMPLEX_TYPE)
4029 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4030 || code2 == COMPLEX_TYPE))
4032 semantic_result_type = c_common_type (type1, type2);
4033 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4035 op1 = TREE_OPERAND (op1, 0);
4036 type1 = TREE_TYPE (op1);
4037 gcc_assert (TREE_CODE (type1) == code1);
4039 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4041 op2 = TREE_OPERAND (op2, 0);
4042 type2 = TREE_TYPE (op2);
4043 gcc_assert (TREE_CODE (type2) == code2);
4047 if (warn_cxx_compat)
4049 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4050 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4052 if (TREE_CODE (t1) == ENUMERAL_TYPE
4053 && TREE_CODE (t2) == ENUMERAL_TYPE
4054 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4055 warning_at (colon_loc, OPT_Wc___compat,
4056 ("different enum types in conditional is "
4057 "invalid in C++: %qT vs %qT"),
4058 t1, t2);
4061 /* Quickly detect the usual case where op1 and op2 have the same type
4062 after promotion. */
4063 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4065 if (type1 == type2)
4066 result_type = type1;
4067 else
4068 result_type = TYPE_MAIN_VARIANT (type1);
4070 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4071 || code1 == COMPLEX_TYPE)
4072 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4073 || code2 == COMPLEX_TYPE))
4075 result_type = c_common_type (type1, type2);
4077 /* If -Wsign-compare, warn here if type1 and type2 have
4078 different signedness. We'll promote the signed to unsigned
4079 and later code won't know it used to be different.
4080 Do this check on the original types, so that explicit casts
4081 will be considered, but default promotions won't. */
4082 if (c_inhibit_evaluation_warnings == 0)
4084 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4085 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4087 if (unsigned_op1 ^ unsigned_op2)
4089 bool ovf;
4091 /* Do not warn if the result type is signed, since the
4092 signed type will only be chosen if it can represent
4093 all the values of the unsigned type. */
4094 if (!TYPE_UNSIGNED (result_type))
4095 /* OK */;
4096 else
4098 bool op1_maybe_const = true;
4099 bool op2_maybe_const = true;
4101 /* Do not warn if the signed quantity is an
4102 unsuffixed integer literal (or some static
4103 constant expression involving such literals) and
4104 it is non-negative. This warning requires the
4105 operands to be folded for best results, so do
4106 that folding in this case even without
4107 warn_sign_compare to avoid warning options
4108 possibly affecting code generation. */
4109 c_inhibit_evaluation_warnings
4110 += (ifexp == truthvalue_false_node);
4111 op1 = c_fully_fold (op1, require_constant_value,
4112 &op1_maybe_const);
4113 c_inhibit_evaluation_warnings
4114 -= (ifexp == truthvalue_false_node);
4116 c_inhibit_evaluation_warnings
4117 += (ifexp == truthvalue_true_node);
4118 op2 = c_fully_fold (op2, require_constant_value,
4119 &op2_maybe_const);
4120 c_inhibit_evaluation_warnings
4121 -= (ifexp == truthvalue_true_node);
4123 if (warn_sign_compare)
4125 if ((unsigned_op2
4126 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4127 || (unsigned_op1
4128 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4129 /* OK */;
4130 else
4131 warning_at (colon_loc, OPT_Wsign_compare,
4132 ("signed and unsigned type in "
4133 "conditional expression"));
4135 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4136 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4137 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4138 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4143 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4145 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4146 pedwarn (colon_loc, OPT_pedantic,
4147 "ISO C forbids conditional expr with only one void side");
4148 result_type = void_type_node;
4150 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4152 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4153 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4154 addr_space_t as_common;
4156 if (comp_target_types (colon_loc, type1, type2))
4157 result_type = common_pointer_type (type1, type2);
4158 else if (null_pointer_constant_p (orig_op1))
4159 result_type = type2;
4160 else if (null_pointer_constant_p (orig_op2))
4161 result_type = type1;
4162 else if (!addr_space_superset (as1, as2, &as_common))
4164 error_at (colon_loc, "pointers to disjoint address spaces "
4165 "used in conditional expression");
4166 return error_mark_node;
4168 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4170 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4171 pedwarn (colon_loc, OPT_pedantic,
4172 "ISO C forbids conditional expr between "
4173 "%<void *%> and function pointer");
4174 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4175 TREE_TYPE (type2)));
4177 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4179 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4180 pedwarn (colon_loc, OPT_pedantic,
4181 "ISO C forbids conditional expr between "
4182 "%<void *%> and function pointer");
4183 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4184 TREE_TYPE (type1)));
4186 else
4188 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4190 if (!objc_ok)
4191 pedwarn (colon_loc, 0,
4192 "pointer type mismatch in conditional expression");
4193 result_type = build_pointer_type
4194 (build_qualified_type (void_type_node, qual));
4197 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4199 if (!null_pointer_constant_p (orig_op2))
4200 pedwarn (colon_loc, 0,
4201 "pointer/integer type mismatch in conditional expression");
4202 else
4204 op2 = null_pointer_node;
4206 result_type = type1;
4208 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4210 if (!null_pointer_constant_p (orig_op1))
4211 pedwarn (colon_loc, 0,
4212 "pointer/integer type mismatch in conditional expression");
4213 else
4215 op1 = null_pointer_node;
4217 result_type = type2;
4220 if (!result_type)
4222 if (flag_cond_mismatch)
4223 result_type = void_type_node;
4224 else
4226 error_at (colon_loc, "type mismatch in conditional expression");
4227 return error_mark_node;
4231 /* Merge const and volatile flags of the incoming types. */
4232 result_type
4233 = build_type_variant (result_type,
4234 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4235 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4237 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4238 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4240 if (ifexp_bcp && ifexp == truthvalue_true_node)
4242 op2_int_operands = true;
4243 op1 = c_fully_fold (op1, require_constant_value, NULL);
4245 if (ifexp_bcp && ifexp == truthvalue_false_node)
4247 op1_int_operands = true;
4248 op2 = c_fully_fold (op2, require_constant_value, NULL);
4250 int_const = int_operands = (ifexp_int_operands
4251 && op1_int_operands
4252 && op2_int_operands);
4253 if (int_operands)
4255 int_const = ((ifexp == truthvalue_true_node
4256 && TREE_CODE (orig_op1) == INTEGER_CST
4257 && !TREE_OVERFLOW (orig_op1))
4258 || (ifexp == truthvalue_false_node
4259 && TREE_CODE (orig_op2) == INTEGER_CST
4260 && !TREE_OVERFLOW (orig_op2)));
4262 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4263 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4264 else
4266 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4267 if (int_operands)
4268 ret = note_integer_operands (ret);
4270 if (semantic_result_type)
4271 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4273 protected_set_expr_location (ret, colon_loc);
4274 return ret;
4277 /* Return a compound expression that performs two expressions and
4278 returns the value of the second of them.
4280 LOC is the location of the COMPOUND_EXPR. */
4282 tree
4283 build_compound_expr (location_t loc, tree expr1, tree expr2)
4285 bool expr1_int_operands, expr2_int_operands;
4286 tree eptype = NULL_TREE;
4287 tree ret;
4289 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4290 if (expr1_int_operands)
4291 expr1 = remove_c_maybe_const_expr (expr1);
4292 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4293 if (expr2_int_operands)
4294 expr2 = remove_c_maybe_const_expr (expr2);
4296 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4297 expr1 = TREE_OPERAND (expr1, 0);
4298 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4300 eptype = TREE_TYPE (expr2);
4301 expr2 = TREE_OPERAND (expr2, 0);
4304 if (!TREE_SIDE_EFFECTS (expr1))
4306 /* The left-hand operand of a comma expression is like an expression
4307 statement: with -Wunused, we should warn if it doesn't have
4308 any side-effects, unless it was explicitly cast to (void). */
4309 if (warn_unused_value)
4311 if (VOID_TYPE_P (TREE_TYPE (expr1))
4312 && CONVERT_EXPR_P (expr1))
4313 ; /* (void) a, b */
4314 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4315 && TREE_CODE (expr1) == COMPOUND_EXPR
4316 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4317 ; /* (void) a, (void) b, c */
4318 else
4319 warning_at (loc, OPT_Wunused_value,
4320 "left-hand operand of comma expression has no effect");
4324 /* With -Wunused, we should also warn if the left-hand operand does have
4325 side-effects, but computes a value which is not used. For example, in
4326 `foo() + bar(), baz()' the result of the `+' operator is not used,
4327 so we should issue a warning. */
4328 else if (warn_unused_value)
4329 warn_if_unused_value (expr1, loc);
4331 if (expr2 == error_mark_node)
4332 return error_mark_node;
4334 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4336 if (flag_isoc99
4337 && expr1_int_operands
4338 && expr2_int_operands)
4339 ret = note_integer_operands (ret);
4341 if (eptype)
4342 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4344 protected_set_expr_location (ret, loc);
4345 return ret;
4348 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4349 which we are casting. OTYPE is the type of the expression being
4350 cast. Both TYPE and OTYPE are pointer types. -Wcast-qual appeared
4351 on the command line. Named address space qualifiers are not handled
4352 here, because they result in different warnings. */
4354 static void
4355 handle_warn_cast_qual (tree type, tree otype)
4357 tree in_type = type;
4358 tree in_otype = otype;
4359 int added = 0;
4360 int discarded = 0;
4361 bool is_const;
4363 /* Check that the qualifiers on IN_TYPE are a superset of the
4364 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4365 nodes is uninteresting and we stop as soon as we hit a
4366 non-POINTER_TYPE node on either type. */
4369 in_otype = TREE_TYPE (in_otype);
4370 in_type = TREE_TYPE (in_type);
4372 /* GNU C allows cv-qualified function types. 'const' means the
4373 function is very pure, 'volatile' means it can't return. We
4374 need to warn when such qualifiers are added, not when they're
4375 taken away. */
4376 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4377 && TREE_CODE (in_type) == FUNCTION_TYPE)
4378 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4379 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4380 else
4381 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4382 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4384 while (TREE_CODE (in_type) == POINTER_TYPE
4385 && TREE_CODE (in_otype) == POINTER_TYPE);
4387 if (added)
4388 warning (OPT_Wcast_qual, "cast adds new qualifiers to function type");
4390 if (discarded)
4391 /* There are qualifiers present in IN_OTYPE that are not present
4392 in IN_TYPE. */
4393 warning (OPT_Wcast_qual,
4394 "cast discards qualifiers from pointer target type");
4396 if (added || discarded)
4397 return;
4399 /* A cast from **T to const **T is unsafe, because it can cause a
4400 const value to be changed with no additional warning. We only
4401 issue this warning if T is the same on both sides, and we only
4402 issue the warning if there are the same number of pointers on
4403 both sides, as otherwise the cast is clearly unsafe anyhow. A
4404 cast is unsafe when a qualifier is added at one level and const
4405 is not present at all outer levels.
4407 To issue this warning, we check at each level whether the cast
4408 adds new qualifiers not already seen. We don't need to special
4409 case function types, as they won't have the same
4410 TYPE_MAIN_VARIANT. */
4412 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4413 return;
4414 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4415 return;
4417 in_type = type;
4418 in_otype = otype;
4419 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4422 in_type = TREE_TYPE (in_type);
4423 in_otype = TREE_TYPE (in_otype);
4424 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4425 && !is_const)
4427 warning (OPT_Wcast_qual,
4428 ("new qualifiers in middle of multi-level non-const cast "
4429 "are unsafe"));
4430 break;
4432 if (is_const)
4433 is_const = TYPE_READONLY (in_type);
4435 while (TREE_CODE (in_type) == POINTER_TYPE);
4438 /* Build an expression representing a cast to type TYPE of expression EXPR.
4439 LOC is the location of the cast-- typically the open paren of the cast. */
4441 tree
4442 build_c_cast (location_t loc, tree type, tree expr)
4444 tree value;
4446 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4447 expr = TREE_OPERAND (expr, 0);
4449 value = expr;
4451 if (type == error_mark_node || expr == error_mark_node)
4452 return error_mark_node;
4454 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4455 only in <protocol> qualifications. But when constructing cast expressions,
4456 the protocols do matter and must be kept around. */
4457 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4458 return build1 (NOP_EXPR, type, expr);
4460 type = TYPE_MAIN_VARIANT (type);
4462 if (TREE_CODE (type) == ARRAY_TYPE)
4464 error_at (loc, "cast specifies array type");
4465 return error_mark_node;
4468 if (TREE_CODE (type) == FUNCTION_TYPE)
4470 error_at (loc, "cast specifies function type");
4471 return error_mark_node;
4474 if (!VOID_TYPE_P (type))
4476 value = require_complete_type (value);
4477 if (value == error_mark_node)
4478 return error_mark_node;
4481 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4483 if (TREE_CODE (type) == RECORD_TYPE
4484 || TREE_CODE (type) == UNION_TYPE)
4485 pedwarn (loc, OPT_pedantic,
4486 "ISO C forbids casting nonscalar to the same type");
4488 else if (TREE_CODE (type) == UNION_TYPE)
4490 tree field;
4492 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4493 if (TREE_TYPE (field) != error_mark_node
4494 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4495 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4496 break;
4498 if (field)
4500 tree t;
4501 bool maybe_const = true;
4503 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4504 t = c_fully_fold (value, false, &maybe_const);
4505 t = build_constructor_single (type, field, t);
4506 if (!maybe_const)
4507 t = c_wrap_maybe_const (t, true);
4508 t = digest_init (loc, type, t,
4509 NULL_TREE, false, true, 0);
4510 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4511 return t;
4513 error_at (loc, "cast to union type from type not present in union");
4514 return error_mark_node;
4516 else
4518 tree otype, ovalue;
4520 if (type == void_type_node)
4522 tree t = build1 (CONVERT_EXPR, type, value);
4523 SET_EXPR_LOCATION (t, loc);
4524 return t;
4527 otype = TREE_TYPE (value);
4529 /* Optionally warn about potentially worrisome casts. */
4530 if (warn_cast_qual
4531 && TREE_CODE (type) == POINTER_TYPE
4532 && TREE_CODE (otype) == POINTER_TYPE)
4533 handle_warn_cast_qual (type, otype);
4535 /* Warn about conversions between pointers to disjoint
4536 address spaces. */
4537 if (TREE_CODE (type) == POINTER_TYPE
4538 && TREE_CODE (otype) == POINTER_TYPE
4539 && !null_pointer_constant_p (value))
4541 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4542 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4543 addr_space_t as_common;
4545 if (!addr_space_superset (as_to, as_from, &as_common))
4547 if (ADDR_SPACE_GENERIC_P (as_from))
4548 warning_at (loc, 0, "cast to %s address space pointer "
4549 "from disjoint generic address space pointer",
4550 c_addr_space_name (as_to));
4552 else if (ADDR_SPACE_GENERIC_P (as_to))
4553 warning_at (loc, 0, "cast to generic address space pointer "
4554 "from disjoint %s address space pointer",
4555 c_addr_space_name (as_from));
4557 else
4558 warning_at (loc, 0, "cast to %s address space pointer "
4559 "from disjoint %s address space pointer",
4560 c_addr_space_name (as_to),
4561 c_addr_space_name (as_from));
4565 /* Warn about possible alignment problems. */
4566 if (STRICT_ALIGNMENT
4567 && TREE_CODE (type) == POINTER_TYPE
4568 && TREE_CODE (otype) == POINTER_TYPE
4569 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4570 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4571 /* Don't warn about opaque types, where the actual alignment
4572 restriction is unknown. */
4573 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4574 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4575 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4576 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4577 warning_at (loc, OPT_Wcast_align,
4578 "cast increases required alignment of target type");
4580 if (TREE_CODE (type) == INTEGER_TYPE
4581 && TREE_CODE (otype) == POINTER_TYPE
4582 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4583 /* Unlike conversion of integers to pointers, where the
4584 warning is disabled for converting constants because
4585 of cases such as SIG_*, warn about converting constant
4586 pointers to integers. In some cases it may cause unwanted
4587 sign extension, and a warning is appropriate. */
4588 warning_at (loc, OPT_Wpointer_to_int_cast,
4589 "cast from pointer to integer of different size");
4591 if (TREE_CODE (value) == CALL_EXPR
4592 && TREE_CODE (type) != TREE_CODE (otype))
4593 warning_at (loc, OPT_Wbad_function_cast,
4594 "cast from function call of type %qT "
4595 "to non-matching type %qT", otype, type);
4597 if (TREE_CODE (type) == POINTER_TYPE
4598 && TREE_CODE (otype) == INTEGER_TYPE
4599 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4600 /* Don't warn about converting any constant. */
4601 && !TREE_CONSTANT (value))
4602 warning_at (loc,
4603 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4604 "of different size");
4606 if (warn_strict_aliasing <= 2)
4607 strict_aliasing_warning (otype, type, expr);
4609 /* If pedantic, warn for conversions between function and object
4610 pointer types, except for converting a null pointer constant
4611 to function pointer type. */
4612 if (pedantic
4613 && TREE_CODE (type) == POINTER_TYPE
4614 && TREE_CODE (otype) == POINTER_TYPE
4615 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4616 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4617 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4618 "conversion of function pointer to object pointer type");
4620 if (pedantic
4621 && TREE_CODE (type) == POINTER_TYPE
4622 && TREE_CODE (otype) == POINTER_TYPE
4623 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4624 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4625 && !null_pointer_constant_p (value))
4626 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4627 "conversion of object pointer to function pointer type");
4629 ovalue = value;
4630 value = convert (type, value);
4632 /* Ignore any integer overflow caused by the cast. */
4633 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4635 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4637 if (!TREE_OVERFLOW (value))
4639 /* Avoid clobbering a shared constant. */
4640 value = copy_node (value);
4641 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4644 else if (TREE_OVERFLOW (value))
4645 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4646 value = build_int_cst_wide (TREE_TYPE (value),
4647 TREE_INT_CST_LOW (value),
4648 TREE_INT_CST_HIGH (value));
4652 /* Don't let a cast be an lvalue. */
4653 if (value == expr)
4654 value = non_lvalue_loc (loc, value);
4656 /* Don't allow the results of casting to floating-point or complex
4657 types be confused with actual constants, or casts involving
4658 integer and pointer types other than direct integer-to-integer
4659 and integer-to-pointer be confused with integer constant
4660 expressions and null pointer constants. */
4661 if (TREE_CODE (value) == REAL_CST
4662 || TREE_CODE (value) == COMPLEX_CST
4663 || (TREE_CODE (value) == INTEGER_CST
4664 && !((TREE_CODE (expr) == INTEGER_CST
4665 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4666 || TREE_CODE (expr) == REAL_CST
4667 || TREE_CODE (expr) == COMPLEX_CST)))
4668 value = build1 (NOP_EXPR, type, value);
4670 if (CAN_HAVE_LOCATION_P (value))
4671 SET_EXPR_LOCATION (value, loc);
4672 return value;
4675 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4676 location of the open paren of the cast, or the position of the cast
4677 expr. */
4678 tree
4679 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4681 tree type;
4682 tree type_expr = NULL_TREE;
4683 bool type_expr_const = true;
4684 tree ret;
4685 int saved_wsp = warn_strict_prototypes;
4687 /* This avoids warnings about unprototyped casts on
4688 integers. E.g. "#define SIG_DFL (void(*)())0". */
4689 if (TREE_CODE (expr) == INTEGER_CST)
4690 warn_strict_prototypes = 0;
4691 type = groktypename (type_name, &type_expr, &type_expr_const);
4692 warn_strict_prototypes = saved_wsp;
4694 ret = build_c_cast (loc, type, expr);
4695 if (type_expr)
4697 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4698 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4699 SET_EXPR_LOCATION (ret, loc);
4702 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4703 SET_EXPR_LOCATION (ret, loc);
4705 /* C++ does not permits types to be defined in a cast. */
4706 if (warn_cxx_compat && type_name->specs->tag_defined_p)
4707 warning_at (loc, OPT_Wc___compat,
4708 "defining a type in a cast is invalid in C++");
4710 return ret;
4713 /* Build an assignment expression of lvalue LHS from value RHS.
4714 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4715 may differ from TREE_TYPE (LHS) for an enum bitfield.
4716 MODIFYCODE is the code for a binary operator that we use
4717 to combine the old value of LHS with RHS to get the new value.
4718 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4719 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4720 which may differ from TREE_TYPE (RHS) for an enum value.
4722 LOCATION is the location of the MODIFYCODE operator.
4723 RHS_LOC is the location of the RHS. */
4725 tree
4726 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4727 enum tree_code modifycode,
4728 location_t rhs_loc, tree rhs, tree rhs_origtype)
4730 tree result;
4731 tree newrhs;
4732 tree rhs_semantic_type = NULL_TREE;
4733 tree lhstype = TREE_TYPE (lhs);
4734 tree olhstype = lhstype;
4735 bool npc;
4737 /* Types that aren't fully specified cannot be used in assignments. */
4738 lhs = require_complete_type (lhs);
4740 /* Avoid duplicate error messages from operands that had errors. */
4741 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4742 return error_mark_node;
4744 if (!lvalue_or_else (lhs, lv_assign))
4745 return error_mark_node;
4747 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4749 rhs_semantic_type = TREE_TYPE (rhs);
4750 rhs = TREE_OPERAND (rhs, 0);
4753 newrhs = rhs;
4755 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4757 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4758 lhs_origtype, modifycode, rhs_loc, rhs,
4759 rhs_origtype);
4760 if (inner == error_mark_node)
4761 return error_mark_node;
4762 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4763 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4764 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4765 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4766 protected_set_expr_location (result, location);
4767 return result;
4770 /* If a binary op has been requested, combine the old LHS value with the RHS
4771 producing the value we should actually store into the LHS. */
4773 if (modifycode != NOP_EXPR)
4775 lhs = c_fully_fold (lhs, false, NULL);
4776 lhs = stabilize_reference (lhs);
4777 newrhs = build_binary_op (location,
4778 modifycode, lhs, rhs, 1);
4780 /* The original type of the right hand side is no longer
4781 meaningful. */
4782 rhs_origtype = NULL_TREE;
4785 /* Give an error for storing in something that is 'const'. */
4787 if (TYPE_READONLY (lhstype)
4788 || ((TREE_CODE (lhstype) == RECORD_TYPE
4789 || TREE_CODE (lhstype) == UNION_TYPE)
4790 && C_TYPE_FIELDS_READONLY (lhstype)))
4792 readonly_error (lhs, lv_assign);
4793 return error_mark_node;
4795 else if (TREE_READONLY (lhs))
4796 readonly_warning (lhs, lv_assign);
4798 /* If storing into a structure or union member,
4799 it has probably been given type `int'.
4800 Compute the type that would go with
4801 the actual amount of storage the member occupies. */
4803 if (TREE_CODE (lhs) == COMPONENT_REF
4804 && (TREE_CODE (lhstype) == INTEGER_TYPE
4805 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4806 || TREE_CODE (lhstype) == REAL_TYPE
4807 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4808 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4810 /* If storing in a field that is in actuality a short or narrower than one,
4811 we must store in the field in its actual type. */
4813 if (lhstype != TREE_TYPE (lhs))
4815 lhs = copy_node (lhs);
4816 TREE_TYPE (lhs) = lhstype;
4819 /* Issue -Wc++-compat warnings about an assignment to an enum type
4820 when LHS does not have its original type. This happens for,
4821 e.g., an enum bitfield in a struct. */
4822 if (warn_cxx_compat
4823 && lhs_origtype != NULL_TREE
4824 && lhs_origtype != lhstype
4825 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4827 tree checktype = (rhs_origtype != NULL_TREE
4828 ? rhs_origtype
4829 : TREE_TYPE (rhs));
4830 if (checktype != error_mark_node
4831 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4832 warning_at (location, OPT_Wc___compat,
4833 "enum conversion in assignment is invalid in C++");
4836 /* Convert new value to destination type. Fold it first, then
4837 restore any excess precision information, for the sake of
4838 conversion warnings. */
4840 npc = null_pointer_constant_p (newrhs);
4841 newrhs = c_fully_fold (newrhs, false, NULL);
4842 if (rhs_semantic_type)
4843 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4844 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4845 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4846 if (TREE_CODE (newrhs) == ERROR_MARK)
4847 return error_mark_node;
4849 /* Emit ObjC write barrier, if necessary. */
4850 if (c_dialect_objc () && flag_objc_gc)
4852 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4853 if (result)
4855 protected_set_expr_location (result, location);
4856 return result;
4860 /* Scan operands. */
4862 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4863 TREE_SIDE_EFFECTS (result) = 1;
4864 protected_set_expr_location (result, location);
4866 /* If we got the LHS in a different type for storing in,
4867 convert the result back to the nominal type of LHS
4868 so that the value we return always has the same type
4869 as the LHS argument. */
4871 if (olhstype == TREE_TYPE (result))
4872 return result;
4874 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
4875 ic_assign, false, NULL_TREE, NULL_TREE, 0);
4876 protected_set_expr_location (result, location);
4877 return result;
4880 /* Convert value RHS to type TYPE as preparation for an assignment to
4881 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
4882 original type of RHS; this differs from TREE_TYPE (RHS) for enum
4883 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
4884 constant before any folding.
4885 The real work of conversion is done by `convert'.
4886 The purpose of this function is to generate error messages
4887 for assignments that are not allowed in C.
4888 ERRTYPE says whether it is argument passing, assignment,
4889 initialization or return.
4891 LOCATION is the location of the RHS.
4892 FUNCTION is a tree for the function being called.
4893 PARMNUM is the number of the argument, for printing in error messages. */
4895 static tree
4896 convert_for_assignment (location_t location, tree type, tree rhs,
4897 tree origtype, enum impl_conv errtype,
4898 bool null_pointer_constant, tree fundecl,
4899 tree function, int parmnum)
4901 enum tree_code codel = TREE_CODE (type);
4902 tree orig_rhs = rhs;
4903 tree rhstype;
4904 enum tree_code coder;
4905 tree rname = NULL_TREE;
4906 bool objc_ok = false;
4908 if (errtype == ic_argpass)
4910 tree selector;
4911 /* Change pointer to function to the function itself for
4912 diagnostics. */
4913 if (TREE_CODE (function) == ADDR_EXPR
4914 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4915 function = TREE_OPERAND (function, 0);
4917 /* Handle an ObjC selector specially for diagnostics. */
4918 selector = objc_message_selector ();
4919 rname = function;
4920 if (selector && parmnum > 2)
4922 rname = selector;
4923 parmnum -= 2;
4927 /* This macro is used to emit diagnostics to ensure that all format
4928 strings are complete sentences, visible to gettext and checked at
4929 compile time. */
4930 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
4931 do { \
4932 switch (errtype) \
4934 case ic_argpass: \
4935 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
4936 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
4937 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
4938 "expected %qT but argument is of type %qT", \
4939 type, rhstype); \
4940 break; \
4941 case ic_assign: \
4942 pedwarn (LOCATION, OPT, AS); \
4943 break; \
4944 case ic_init: \
4945 pedwarn (LOCATION, OPT, IN); \
4946 break; \
4947 case ic_return: \
4948 pedwarn (LOCATION, OPT, RE); \
4949 break; \
4950 default: \
4951 gcc_unreachable (); \
4953 } while (0)
4955 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4956 rhs = TREE_OPERAND (rhs, 0);
4958 rhstype = TREE_TYPE (rhs);
4959 coder = TREE_CODE (rhstype);
4961 if (coder == ERROR_MARK)
4962 return error_mark_node;
4964 if (c_dialect_objc ())
4966 int parmno;
4968 switch (errtype)
4970 case ic_return:
4971 parmno = 0;
4972 break;
4974 case ic_assign:
4975 parmno = -1;
4976 break;
4978 case ic_init:
4979 parmno = -2;
4980 break;
4982 default:
4983 parmno = parmnum;
4984 break;
4987 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
4990 if (warn_cxx_compat)
4992 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
4993 if (checktype != error_mark_node
4994 && TREE_CODE (type) == ENUMERAL_TYPE
4995 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
4997 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
4998 G_("enum conversion when passing argument "
4999 "%d of %qE is invalid in C++"),
5000 G_("enum conversion in assignment is "
5001 "invalid in C++"),
5002 G_("enum conversion in initialization is "
5003 "invalid in C++"),
5004 G_("enum conversion in return is "
5005 "invalid in C++"));
5009 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5010 return rhs;
5012 if (coder == VOID_TYPE)
5014 /* Except for passing an argument to an unprototyped function,
5015 this is a constraint violation. When passing an argument to
5016 an unprototyped function, it is compile-time undefined;
5017 making it a constraint in that case was rejected in
5018 DR#252. */
5019 error_at (location, "void value not ignored as it ought to be");
5020 return error_mark_node;
5022 rhs = require_complete_type (rhs);
5023 if (rhs == error_mark_node)
5024 return error_mark_node;
5025 /* A type converts to a reference to it.
5026 This code doesn't fully support references, it's just for the
5027 special case of va_start and va_copy. */
5028 if (codel == REFERENCE_TYPE
5029 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5031 if (!lvalue_p (rhs))
5033 error_at (location, "cannot pass rvalue to reference parameter");
5034 return error_mark_node;
5036 if (!c_mark_addressable (rhs))
5037 return error_mark_node;
5038 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5039 SET_EXPR_LOCATION (rhs, location);
5041 /* We already know that these two types are compatible, but they
5042 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5043 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5044 likely to be va_list, a typedef to __builtin_va_list, which
5045 is different enough that it will cause problems later. */
5046 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5048 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5049 SET_EXPR_LOCATION (rhs, location);
5052 rhs = build1 (NOP_EXPR, type, rhs);
5053 SET_EXPR_LOCATION (rhs, location);
5054 return rhs;
5056 /* Some types can interconvert without explicit casts. */
5057 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5058 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5059 return convert (type, rhs);
5060 /* Arithmetic types all interconvert, and enum is treated like int. */
5061 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5062 || codel == FIXED_POINT_TYPE
5063 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5064 || codel == BOOLEAN_TYPE)
5065 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5066 || coder == FIXED_POINT_TYPE
5067 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5068 || coder == BOOLEAN_TYPE))
5070 tree ret;
5071 bool save = in_late_binary_op;
5072 if (codel == BOOLEAN_TYPE)
5073 in_late_binary_op = true;
5074 ret = convert_and_check (type, orig_rhs);
5075 if (codel == BOOLEAN_TYPE)
5076 in_late_binary_op = save;
5077 return ret;
5080 /* Aggregates in different TUs might need conversion. */
5081 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5082 && codel == coder
5083 && comptypes (type, rhstype))
5084 return convert_and_check (type, rhs);
5086 /* Conversion to a transparent union or record from its member types.
5087 This applies only to function arguments. */
5088 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5089 && TYPE_TRANSPARENT_AGGR (type))
5090 && errtype == ic_argpass)
5092 tree memb, marginal_memb = NULL_TREE;
5094 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
5096 tree memb_type = TREE_TYPE (memb);
5098 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5099 TYPE_MAIN_VARIANT (rhstype)))
5100 break;
5102 if (TREE_CODE (memb_type) != POINTER_TYPE)
5103 continue;
5105 if (coder == POINTER_TYPE)
5107 tree ttl = TREE_TYPE (memb_type);
5108 tree ttr = TREE_TYPE (rhstype);
5110 /* Any non-function converts to a [const][volatile] void *
5111 and vice versa; otherwise, targets must be the same.
5112 Meanwhile, the lhs target must have all the qualifiers of
5113 the rhs. */
5114 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5115 || comp_target_types (location, memb_type, rhstype))
5117 /* If this type won't generate any warnings, use it. */
5118 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5119 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5120 && TREE_CODE (ttl) == FUNCTION_TYPE)
5121 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5122 == TYPE_QUALS (ttr))
5123 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5124 == TYPE_QUALS (ttl))))
5125 break;
5127 /* Keep looking for a better type, but remember this one. */
5128 if (!marginal_memb)
5129 marginal_memb = memb;
5133 /* Can convert integer zero to any pointer type. */
5134 if (null_pointer_constant)
5136 rhs = null_pointer_node;
5137 break;
5141 if (memb || marginal_memb)
5143 if (!memb)
5145 /* We have only a marginally acceptable member type;
5146 it needs a warning. */
5147 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5148 tree ttr = TREE_TYPE (rhstype);
5150 /* Const and volatile mean something different for function
5151 types, so the usual warnings are not appropriate. */
5152 if (TREE_CODE (ttr) == FUNCTION_TYPE
5153 && TREE_CODE (ttl) == FUNCTION_TYPE)
5155 /* Because const and volatile on functions are
5156 restrictions that say the function will not do
5157 certain things, it is okay to use a const or volatile
5158 function where an ordinary one is wanted, but not
5159 vice-versa. */
5160 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5161 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5162 WARN_FOR_ASSIGNMENT (location, 0,
5163 G_("passing argument %d of %qE "
5164 "makes qualified function "
5165 "pointer from unqualified"),
5166 G_("assignment makes qualified "
5167 "function pointer from "
5168 "unqualified"),
5169 G_("initialization makes qualified "
5170 "function pointer from "
5171 "unqualified"),
5172 G_("return makes qualified function "
5173 "pointer from unqualified"));
5175 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5176 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5177 WARN_FOR_ASSIGNMENT (location, 0,
5178 G_("passing argument %d of %qE discards "
5179 "qualifiers from pointer target type"),
5180 G_("assignment discards qualifiers "
5181 "from pointer target type"),
5182 G_("initialization discards qualifiers "
5183 "from pointer target type"),
5184 G_("return discards qualifiers from "
5185 "pointer target type"));
5187 memb = marginal_memb;
5190 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5191 pedwarn (location, OPT_pedantic,
5192 "ISO C prohibits argument conversion to union type");
5194 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5195 return build_constructor_single (type, memb, rhs);
5199 /* Conversions among pointers */
5200 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5201 && (coder == codel))
5203 tree ttl = TREE_TYPE (type);
5204 tree ttr = TREE_TYPE (rhstype);
5205 tree mvl = ttl;
5206 tree mvr = ttr;
5207 bool is_opaque_pointer;
5208 int target_cmp = 0; /* Cache comp_target_types () result. */
5209 addr_space_t asl;
5210 addr_space_t asr;
5212 if (TREE_CODE (mvl) != ARRAY_TYPE)
5213 mvl = TYPE_MAIN_VARIANT (mvl);
5214 if (TREE_CODE (mvr) != ARRAY_TYPE)
5215 mvr = TYPE_MAIN_VARIANT (mvr);
5216 /* Opaque pointers are treated like void pointers. */
5217 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5219 /* C++ does not allow the implicit conversion void* -> T*. However,
5220 for the purpose of reducing the number of false positives, we
5221 tolerate the special case of
5223 int *p = NULL;
5225 where NULL is typically defined in C to be '(void *) 0'. */
5226 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5227 warning_at (location, OPT_Wc___compat,
5228 "request for implicit conversion "
5229 "from %qT to %qT not permitted in C++", rhstype, type);
5231 /* See if the pointers point to incompatible address spaces. */
5232 asl = TYPE_ADDR_SPACE (ttl);
5233 asr = TYPE_ADDR_SPACE (ttr);
5234 if (!null_pointer_constant_p (rhs)
5235 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5237 switch (errtype)
5239 case ic_argpass:
5240 error_at (location, "passing argument %d of %qE from pointer to "
5241 "non-enclosed address space", parmnum, rname);
5242 break;
5243 case ic_assign:
5244 error_at (location, "assignment from pointer to "
5245 "non-enclosed address space");
5246 break;
5247 case ic_init:
5248 error_at (location, "initialization from pointer to "
5249 "non-enclosed address space");
5250 break;
5251 case ic_return:
5252 error_at (location, "return from pointer to "
5253 "non-enclosed address space");
5254 break;
5255 default:
5256 gcc_unreachable ();
5258 return error_mark_node;
5261 /* Check if the right-hand side has a format attribute but the
5262 left-hand side doesn't. */
5263 if (warn_missing_format_attribute
5264 && check_missing_format_attribute (type, rhstype))
5266 switch (errtype)
5268 case ic_argpass:
5269 warning_at (location, OPT_Wmissing_format_attribute,
5270 "argument %d of %qE might be "
5271 "a candidate for a format attribute",
5272 parmnum, rname);
5273 break;
5274 case ic_assign:
5275 warning_at (location, OPT_Wmissing_format_attribute,
5276 "assignment left-hand side might be "
5277 "a candidate for a format attribute");
5278 break;
5279 case ic_init:
5280 warning_at (location, OPT_Wmissing_format_attribute,
5281 "initialization left-hand side might be "
5282 "a candidate for a format attribute");
5283 break;
5284 case ic_return:
5285 warning_at (location, OPT_Wmissing_format_attribute,
5286 "return type might be "
5287 "a candidate for a format attribute");
5288 break;
5289 default:
5290 gcc_unreachable ();
5294 /* Any non-function converts to a [const][volatile] void *
5295 and vice versa; otherwise, targets must be the same.
5296 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5297 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5298 || (target_cmp = comp_target_types (location, type, rhstype))
5299 || is_opaque_pointer
5300 || (c_common_unsigned_type (mvl)
5301 == c_common_unsigned_type (mvr)))
5303 if (pedantic
5304 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5306 (VOID_TYPE_P (ttr)
5307 && !null_pointer_constant
5308 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5309 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5310 G_("ISO C forbids passing argument %d of "
5311 "%qE between function pointer "
5312 "and %<void *%>"),
5313 G_("ISO C forbids assignment between "
5314 "function pointer and %<void *%>"),
5315 G_("ISO C forbids initialization between "
5316 "function pointer and %<void *%>"),
5317 G_("ISO C forbids return between function "
5318 "pointer and %<void *%>"));
5319 /* Const and volatile mean something different for function types,
5320 so the usual warnings are not appropriate. */
5321 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5322 && TREE_CODE (ttl) != FUNCTION_TYPE)
5324 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5325 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5327 /* Types differing only by the presence of the 'volatile'
5328 qualifier are acceptable if the 'volatile' has been added
5329 in by the Objective-C EH machinery. */
5330 if (!objc_type_quals_match (ttl, ttr))
5331 WARN_FOR_ASSIGNMENT (location, 0,
5332 G_("passing argument %d of %qE discards "
5333 "qualifiers from pointer target type"),
5334 G_("assignment discards qualifiers "
5335 "from pointer target type"),
5336 G_("initialization discards qualifiers "
5337 "from pointer target type"),
5338 G_("return discards qualifiers from "
5339 "pointer target type"));
5341 /* If this is not a case of ignoring a mismatch in signedness,
5342 no warning. */
5343 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5344 || target_cmp)
5346 /* If there is a mismatch, do warn. */
5347 else if (warn_pointer_sign)
5348 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5349 G_("pointer targets in passing argument "
5350 "%d of %qE differ in signedness"),
5351 G_("pointer targets in assignment "
5352 "differ in signedness"),
5353 G_("pointer targets in initialization "
5354 "differ in signedness"),
5355 G_("pointer targets in return differ "
5356 "in signedness"));
5358 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5359 && TREE_CODE (ttr) == FUNCTION_TYPE)
5361 /* Because const and volatile on functions are restrictions
5362 that say the function will not do certain things,
5363 it is okay to use a const or volatile function
5364 where an ordinary one is wanted, but not vice-versa. */
5365 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5366 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5367 WARN_FOR_ASSIGNMENT (location, 0,
5368 G_("passing argument %d of %qE makes "
5369 "qualified function pointer "
5370 "from unqualified"),
5371 G_("assignment makes qualified function "
5372 "pointer from unqualified"),
5373 G_("initialization makes qualified "
5374 "function pointer from unqualified"),
5375 G_("return makes qualified function "
5376 "pointer from unqualified"));
5379 else
5380 /* Avoid warning about the volatile ObjC EH puts on decls. */
5381 if (!objc_ok)
5382 WARN_FOR_ASSIGNMENT (location, 0,
5383 G_("passing argument %d of %qE from "
5384 "incompatible pointer type"),
5385 G_("assignment from incompatible pointer type"),
5386 G_("initialization from incompatible "
5387 "pointer type"),
5388 G_("return from incompatible pointer type"));
5390 return convert (type, rhs);
5392 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5394 /* ??? This should not be an error when inlining calls to
5395 unprototyped functions. */
5396 error_at (location, "invalid use of non-lvalue array");
5397 return error_mark_node;
5399 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5401 /* An explicit constant 0 can convert to a pointer,
5402 or one that results from arithmetic, even including
5403 a cast to integer type. */
5404 if (!null_pointer_constant)
5405 WARN_FOR_ASSIGNMENT (location, 0,
5406 G_("passing argument %d of %qE makes "
5407 "pointer from integer without a cast"),
5408 G_("assignment makes pointer from integer "
5409 "without a cast"),
5410 G_("initialization makes pointer from "
5411 "integer without a cast"),
5412 G_("return makes pointer from integer "
5413 "without a cast"));
5415 return convert (type, rhs);
5417 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5419 WARN_FOR_ASSIGNMENT (location, 0,
5420 G_("passing argument %d of %qE makes integer "
5421 "from pointer without a cast"),
5422 G_("assignment makes integer from pointer "
5423 "without a cast"),
5424 G_("initialization makes integer from pointer "
5425 "without a cast"),
5426 G_("return makes integer from pointer "
5427 "without a cast"));
5428 return convert (type, rhs);
5430 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5432 tree ret;
5433 bool save = in_late_binary_op;
5434 in_late_binary_op = true;
5435 ret = convert (type, rhs);
5436 in_late_binary_op = save;
5437 return ret;
5440 switch (errtype)
5442 case ic_argpass:
5443 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5444 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5445 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5446 "expected %qT but argument is of type %qT", type, rhstype);
5447 break;
5448 case ic_assign:
5449 error_at (location, "incompatible types when assigning to type %qT from "
5450 "type %qT", type, rhstype);
5451 break;
5452 case ic_init:
5453 error_at (location,
5454 "incompatible types when initializing type %qT using type %qT",
5455 type, rhstype);
5456 break;
5457 case ic_return:
5458 error_at (location,
5459 "incompatible types when returning type %qT but %qT was "
5460 "expected", rhstype, type);
5461 break;
5462 default:
5463 gcc_unreachable ();
5466 return error_mark_node;
5469 /* If VALUE is a compound expr all of whose expressions are constant, then
5470 return its value. Otherwise, return error_mark_node.
5472 This is for handling COMPOUND_EXPRs as initializer elements
5473 which is allowed with a warning when -pedantic is specified. */
5475 static tree
5476 valid_compound_expr_initializer (tree value, tree endtype)
5478 if (TREE_CODE (value) == COMPOUND_EXPR)
5480 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5481 == error_mark_node)
5482 return error_mark_node;
5483 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5484 endtype);
5486 else if (!initializer_constant_valid_p (value, endtype))
5487 return error_mark_node;
5488 else
5489 return value;
5492 /* Perform appropriate conversions on the initial value of a variable,
5493 store it in the declaration DECL,
5494 and print any error messages that are appropriate.
5495 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5496 If the init is invalid, store an ERROR_MARK.
5498 INIT_LOC is the location of the initial value. */
5500 void
5501 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5503 tree value, type;
5504 bool npc = false;
5506 /* If variable's type was invalidly declared, just ignore it. */
5508 type = TREE_TYPE (decl);
5509 if (TREE_CODE (type) == ERROR_MARK)
5510 return;
5512 /* Digest the specified initializer into an expression. */
5514 if (init)
5515 npc = null_pointer_constant_p (init);
5516 value = digest_init (init_loc, type, init, origtype, npc,
5517 true, TREE_STATIC (decl));
5519 /* Store the expression if valid; else report error. */
5521 if (!in_system_header
5522 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5523 warning (OPT_Wtraditional, "traditional C rejects automatic "
5524 "aggregate initialization");
5526 DECL_INITIAL (decl) = value;
5528 /* ANSI wants warnings about out-of-range constant initializers. */
5529 STRIP_TYPE_NOPS (value);
5530 if (TREE_STATIC (decl))
5531 constant_expression_warning (value);
5533 /* Check if we need to set array size from compound literal size. */
5534 if (TREE_CODE (type) == ARRAY_TYPE
5535 && TYPE_DOMAIN (type) == 0
5536 && value != error_mark_node)
5538 tree inside_init = init;
5540 STRIP_TYPE_NOPS (inside_init);
5541 inside_init = fold (inside_init);
5543 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5545 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5547 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5549 /* For int foo[] = (int [3]){1}; we need to set array size
5550 now since later on array initializer will be just the
5551 brace enclosed list of the compound literal. */
5552 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5553 TREE_TYPE (decl) = type;
5554 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5555 layout_type (type);
5556 layout_decl (cldecl, 0);
5562 /* Methods for storing and printing names for error messages. */
5564 /* Implement a spelling stack that allows components of a name to be pushed
5565 and popped. Each element on the stack is this structure. */
5567 struct spelling
5569 int kind;
5570 union
5572 unsigned HOST_WIDE_INT i;
5573 const char *s;
5574 } u;
5577 #define SPELLING_STRING 1
5578 #define SPELLING_MEMBER 2
5579 #define SPELLING_BOUNDS 3
5581 static struct spelling *spelling; /* Next stack element (unused). */
5582 static struct spelling *spelling_base; /* Spelling stack base. */
5583 static int spelling_size; /* Size of the spelling stack. */
5585 /* Macros to save and restore the spelling stack around push_... functions.
5586 Alternative to SAVE_SPELLING_STACK. */
5588 #define SPELLING_DEPTH() (spelling - spelling_base)
5589 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5591 /* Push an element on the spelling stack with type KIND and assign VALUE
5592 to MEMBER. */
5594 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5596 int depth = SPELLING_DEPTH (); \
5598 if (depth >= spelling_size) \
5600 spelling_size += 10; \
5601 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5602 spelling_size); \
5603 RESTORE_SPELLING_DEPTH (depth); \
5606 spelling->kind = (KIND); \
5607 spelling->MEMBER = (VALUE); \
5608 spelling++; \
5611 /* Push STRING on the stack. Printed literally. */
5613 static void
5614 push_string (const char *string)
5616 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5619 /* Push a member name on the stack. Printed as '.' STRING. */
5621 static void
5622 push_member_name (tree decl)
5624 const char *const string
5625 = (DECL_NAME (decl)
5626 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5627 : _("<anonymous>"));
5628 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5631 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5633 static void
5634 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5636 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5639 /* Compute the maximum size in bytes of the printed spelling. */
5641 static int
5642 spelling_length (void)
5644 int size = 0;
5645 struct spelling *p;
5647 for (p = spelling_base; p < spelling; p++)
5649 if (p->kind == SPELLING_BOUNDS)
5650 size += 25;
5651 else
5652 size += strlen (p->u.s) + 1;
5655 return size;
5658 /* Print the spelling to BUFFER and return it. */
5660 static char *
5661 print_spelling (char *buffer)
5663 char *d = buffer;
5664 struct spelling *p;
5666 for (p = spelling_base; p < spelling; p++)
5667 if (p->kind == SPELLING_BOUNDS)
5669 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5670 d += strlen (d);
5672 else
5674 const char *s;
5675 if (p->kind == SPELLING_MEMBER)
5676 *d++ = '.';
5677 for (s = p->u.s; (*d = *s++); d++)
5680 *d++ = '\0';
5681 return buffer;
5684 /* Issue an error message for a bad initializer component.
5685 MSGID identifies the message.
5686 The component name is taken from the spelling stack. */
5688 void
5689 error_init (const char *msgid)
5691 char *ofwhat;
5693 error ("%s", _(msgid));
5694 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5695 if (*ofwhat)
5696 error ("(near initialization for %qs)", ofwhat);
5699 /* Issue a pedantic warning for a bad initializer component. OPT is
5700 the option OPT_* (from options.h) controlling this warning or 0 if
5701 it is unconditionally given. MSGID identifies the message. The
5702 component name is taken from the spelling stack. */
5704 void
5705 pedwarn_init (location_t location, int opt, const char *msgid)
5707 char *ofwhat;
5709 pedwarn (location, opt, "%s", _(msgid));
5710 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5711 if (*ofwhat)
5712 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5715 /* Issue a warning for a bad initializer component.
5717 OPT is the OPT_W* value corresponding to the warning option that
5718 controls this warning. MSGID identifies the message. The
5719 component name is taken from the spelling stack. */
5721 static void
5722 warning_init (int opt, const char *msgid)
5724 char *ofwhat;
5726 warning (opt, "%s", _(msgid));
5727 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5728 if (*ofwhat)
5729 warning (opt, "(near initialization for %qs)", ofwhat);
5732 /* If TYPE is an array type and EXPR is a parenthesized string
5733 constant, warn if pedantic that EXPR is being used to initialize an
5734 object of type TYPE. */
5736 void
5737 maybe_warn_string_init (tree type, struct c_expr expr)
5739 if (pedantic
5740 && TREE_CODE (type) == ARRAY_TYPE
5741 && TREE_CODE (expr.value) == STRING_CST
5742 && expr.original_code != STRING_CST)
5743 pedwarn_init (input_location, OPT_pedantic,
5744 "array initialized from parenthesized string constant");
5747 /* Digest the parser output INIT as an initializer for type TYPE.
5748 Return a C expression of type TYPE to represent the initial value.
5750 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5752 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5754 If INIT is a string constant, STRICT_STRING is true if it is
5755 unparenthesized or we should not warn here for it being parenthesized.
5756 For other types of INIT, STRICT_STRING is not used.
5758 INIT_LOC is the location of the INIT.
5760 REQUIRE_CONSTANT requests an error if non-constant initializers or
5761 elements are seen. */
5763 static tree
5764 digest_init (location_t init_loc, tree type, tree init, tree origtype,
5765 bool null_pointer_constant, bool strict_string,
5766 int require_constant)
5768 enum tree_code code = TREE_CODE (type);
5769 tree inside_init = init;
5770 tree semantic_type = NULL_TREE;
5771 bool maybe_const = true;
5773 if (type == error_mark_node
5774 || !init
5775 || init == error_mark_node
5776 || TREE_TYPE (init) == error_mark_node)
5777 return error_mark_node;
5779 STRIP_TYPE_NOPS (inside_init);
5781 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5783 semantic_type = TREE_TYPE (inside_init);
5784 inside_init = TREE_OPERAND (inside_init, 0);
5786 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
5787 inside_init = decl_constant_value_for_optimization (inside_init);
5789 /* Initialization of an array of chars from a string constant
5790 optionally enclosed in braces. */
5792 if (code == ARRAY_TYPE && inside_init
5793 && TREE_CODE (inside_init) == STRING_CST)
5795 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5796 /* Note that an array could be both an array of character type
5797 and an array of wchar_t if wchar_t is signed char or unsigned
5798 char. */
5799 bool char_array = (typ1 == char_type_node
5800 || typ1 == signed_char_type_node
5801 || typ1 == unsigned_char_type_node);
5802 bool wchar_array = !!comptypes (typ1, wchar_type_node);
5803 bool char16_array = !!comptypes (typ1, char16_type_node);
5804 bool char32_array = !!comptypes (typ1, char32_type_node);
5806 if (char_array || wchar_array || char16_array || char32_array)
5808 struct c_expr expr;
5809 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
5810 expr.value = inside_init;
5811 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
5812 expr.original_type = NULL;
5813 maybe_warn_string_init (type, expr);
5815 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5816 pedwarn_init (init_loc, OPT_pedantic,
5817 "initialization of a flexible array member");
5819 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5820 TYPE_MAIN_VARIANT (type)))
5821 return inside_init;
5823 if (char_array)
5825 if (typ2 != char_type_node)
5827 error_init ("char-array initialized from wide string");
5828 return error_mark_node;
5831 else
5833 if (typ2 == char_type_node)
5835 error_init ("wide character array initialized from non-wide "
5836 "string");
5837 return error_mark_node;
5839 else if (!comptypes(typ1, typ2))
5841 error_init ("wide character array initialized from "
5842 "incompatible wide string");
5843 return error_mark_node;
5847 TREE_TYPE (inside_init) = type;
5848 if (TYPE_DOMAIN (type) != 0
5849 && TYPE_SIZE (type) != 0
5850 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
5852 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
5854 /* Subtract the size of a single (possibly wide) character
5855 because it's ok to ignore the terminating null char
5856 that is counted in the length of the constant. */
5857 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
5858 (len
5859 - (TYPE_PRECISION (typ1)
5860 / BITS_PER_UNIT))))
5861 pedwarn_init (init_loc, 0,
5862 ("initializer-string for array of chars "
5863 "is too long"));
5864 else if (warn_cxx_compat
5865 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
5866 warning_at (init_loc, OPT_Wc___compat,
5867 ("initializer-string for array chars "
5868 "is too long for C++"));
5871 return inside_init;
5873 else if (INTEGRAL_TYPE_P (typ1))
5875 error_init ("array of inappropriate type initialized "
5876 "from string constant");
5877 return error_mark_node;
5881 /* Build a VECTOR_CST from a *constant* vector constructor. If the
5882 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5883 below and handle as a constructor. */
5884 if (code == VECTOR_TYPE
5885 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
5886 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
5887 && TREE_CONSTANT (inside_init))
5889 if (TREE_CODE (inside_init) == VECTOR_CST
5890 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5891 TYPE_MAIN_VARIANT (type)))
5892 return inside_init;
5894 if (TREE_CODE (inside_init) == CONSTRUCTOR)
5896 unsigned HOST_WIDE_INT ix;
5897 tree value;
5898 bool constant_p = true;
5900 /* Iterate through elements and check if all constructor
5901 elements are *_CSTs. */
5902 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
5903 if (!CONSTANT_CLASS_P (value))
5905 constant_p = false;
5906 break;
5909 if (constant_p)
5910 return build_vector_from_ctor (type,
5911 CONSTRUCTOR_ELTS (inside_init));
5915 if (warn_sequence_point)
5916 verify_sequence_points (inside_init);
5918 /* Any type can be initialized
5919 from an expression of the same type, optionally with braces. */
5921 if (inside_init && TREE_TYPE (inside_init) != 0
5922 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5923 TYPE_MAIN_VARIANT (type))
5924 || (code == ARRAY_TYPE
5925 && comptypes (TREE_TYPE (inside_init), type))
5926 || (code == VECTOR_TYPE
5927 && comptypes (TREE_TYPE (inside_init), type))
5928 || (code == POINTER_TYPE
5929 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
5930 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
5931 TREE_TYPE (type)))))
5933 if (code == POINTER_TYPE)
5935 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
5937 if (TREE_CODE (inside_init) == STRING_CST
5938 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5939 inside_init = array_to_pointer_conversion
5940 (init_loc, inside_init);
5941 else
5943 error_init ("invalid use of non-lvalue array");
5944 return error_mark_node;
5949 if (code == VECTOR_TYPE)
5950 /* Although the types are compatible, we may require a
5951 conversion. */
5952 inside_init = convert (type, inside_init);
5954 if (require_constant
5955 && (code == VECTOR_TYPE || !flag_isoc99)
5956 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5958 /* As an extension, allow initializing objects with static storage
5959 duration with compound literals (which are then treated just as
5960 the brace enclosed list they contain). Also allow this for
5961 vectors, as we can only assign them with compound literals. */
5962 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5963 inside_init = DECL_INITIAL (decl);
5966 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
5967 && TREE_CODE (inside_init) != CONSTRUCTOR)
5969 error_init ("array initialized from non-constant array expression");
5970 return error_mark_node;
5973 /* Compound expressions can only occur here if -pedantic or
5974 -pedantic-errors is specified. In the later case, we always want
5975 an error. In the former case, we simply want a warning. */
5976 if (require_constant && pedantic
5977 && TREE_CODE (inside_init) == COMPOUND_EXPR)
5979 inside_init
5980 = valid_compound_expr_initializer (inside_init,
5981 TREE_TYPE (inside_init));
5982 if (inside_init == error_mark_node)
5983 error_init ("initializer element is not constant");
5984 else
5985 pedwarn_init (init_loc, OPT_pedantic,
5986 "initializer element is not constant");
5987 if (flag_pedantic_errors)
5988 inside_init = error_mark_node;
5990 else if (require_constant
5991 && !initializer_constant_valid_p (inside_init,
5992 TREE_TYPE (inside_init)))
5994 error_init ("initializer element is not constant");
5995 inside_init = error_mark_node;
5997 else if (require_constant && !maybe_const)
5998 pedwarn_init (init_loc, 0,
5999 "initializer element is not a constant expression");
6001 /* Added to enable additional -Wmissing-format-attribute warnings. */
6002 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6003 inside_init = convert_for_assignment (init_loc, type, inside_init,
6004 origtype,
6005 ic_init, null_pointer_constant,
6006 NULL_TREE, NULL_TREE, 0);
6007 return inside_init;
6010 /* Handle scalar types, including conversions. */
6012 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6013 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6014 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6016 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6017 && (TREE_CODE (init) == STRING_CST
6018 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6019 inside_init = init = array_to_pointer_conversion (init_loc, init);
6020 if (semantic_type)
6021 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6022 inside_init);
6023 inside_init
6024 = convert_for_assignment (init_loc, type, inside_init, origtype,
6025 ic_init, null_pointer_constant,
6026 NULL_TREE, NULL_TREE, 0);
6028 /* Check to see if we have already given an error message. */
6029 if (inside_init == error_mark_node)
6031 else if (require_constant && !TREE_CONSTANT (inside_init))
6033 error_init ("initializer element is not constant");
6034 inside_init = error_mark_node;
6036 else if (require_constant
6037 && !initializer_constant_valid_p (inside_init,
6038 TREE_TYPE (inside_init)))
6040 error_init ("initializer element is not computable at load time");
6041 inside_init = error_mark_node;
6043 else if (require_constant && !maybe_const)
6044 pedwarn_init (init_loc, 0,
6045 "initializer element is not a constant expression");
6047 return inside_init;
6050 /* Come here only for records and arrays. */
6052 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6054 error_init ("variable-sized object may not be initialized");
6055 return error_mark_node;
6058 error_init ("invalid initializer");
6059 return error_mark_node;
6062 /* Handle initializers that use braces. */
6064 /* Type of object we are accumulating a constructor for.
6065 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6066 static tree constructor_type;
6068 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6069 left to fill. */
6070 static tree constructor_fields;
6072 /* For an ARRAY_TYPE, this is the specified index
6073 at which to store the next element we get. */
6074 static tree constructor_index;
6076 /* For an ARRAY_TYPE, this is the maximum index. */
6077 static tree constructor_max_index;
6079 /* For a RECORD_TYPE, this is the first field not yet written out. */
6080 static tree constructor_unfilled_fields;
6082 /* For an ARRAY_TYPE, this is the index of the first element
6083 not yet written out. */
6084 static tree constructor_unfilled_index;
6086 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6087 This is so we can generate gaps between fields, when appropriate. */
6088 static tree constructor_bit_index;
6090 /* If we are saving up the elements rather than allocating them,
6091 this is the list of elements so far (in reverse order,
6092 most recent first). */
6093 static VEC(constructor_elt,gc) *constructor_elements;
6095 /* 1 if constructor should be incrementally stored into a constructor chain,
6096 0 if all the elements should be kept in AVL tree. */
6097 static int constructor_incremental;
6099 /* 1 if so far this constructor's elements are all compile-time constants. */
6100 static int constructor_constant;
6102 /* 1 if so far this constructor's elements are all valid address constants. */
6103 static int constructor_simple;
6105 /* 1 if this constructor has an element that cannot be part of a
6106 constant expression. */
6107 static int constructor_nonconst;
6109 /* 1 if this constructor is erroneous so far. */
6110 static int constructor_erroneous;
6112 /* Structure for managing pending initializer elements, organized as an
6113 AVL tree. */
6115 struct init_node
6117 struct init_node *left, *right;
6118 struct init_node *parent;
6119 int balance;
6120 tree purpose;
6121 tree value;
6122 tree origtype;
6125 /* Tree of pending elements at this constructor level.
6126 These are elements encountered out of order
6127 which belong at places we haven't reached yet in actually
6128 writing the output.
6129 Will never hold tree nodes across GC runs. */
6130 static struct init_node *constructor_pending_elts;
6132 /* The SPELLING_DEPTH of this constructor. */
6133 static int constructor_depth;
6135 /* DECL node for which an initializer is being read.
6136 0 means we are reading a constructor expression
6137 such as (struct foo) {...}. */
6138 static tree constructor_decl;
6140 /* Nonzero if this is an initializer for a top-level decl. */
6141 static int constructor_top_level;
6143 /* Nonzero if there were any member designators in this initializer. */
6144 static int constructor_designated;
6146 /* Nesting depth of designator list. */
6147 static int designator_depth;
6149 /* Nonzero if there were diagnosed errors in this designator list. */
6150 static int designator_erroneous;
6153 /* This stack has a level for each implicit or explicit level of
6154 structuring in the initializer, including the outermost one. It
6155 saves the values of most of the variables above. */
6157 struct constructor_range_stack;
6159 struct constructor_stack
6161 struct constructor_stack *next;
6162 tree type;
6163 tree fields;
6164 tree index;
6165 tree max_index;
6166 tree unfilled_index;
6167 tree unfilled_fields;
6168 tree bit_index;
6169 VEC(constructor_elt,gc) *elements;
6170 struct init_node *pending_elts;
6171 int offset;
6172 int depth;
6173 /* If value nonzero, this value should replace the entire
6174 constructor at this level. */
6175 struct c_expr replacement_value;
6176 struct constructor_range_stack *range_stack;
6177 char constant;
6178 char simple;
6179 char nonconst;
6180 char implicit;
6181 char erroneous;
6182 char outer;
6183 char incremental;
6184 char designated;
6187 static struct constructor_stack *constructor_stack;
6189 /* This stack represents designators from some range designator up to
6190 the last designator in the list. */
6192 struct constructor_range_stack
6194 struct constructor_range_stack *next, *prev;
6195 struct constructor_stack *stack;
6196 tree range_start;
6197 tree index;
6198 tree range_end;
6199 tree fields;
6202 static struct constructor_range_stack *constructor_range_stack;
6204 /* This stack records separate initializers that are nested.
6205 Nested initializers can't happen in ANSI C, but GNU C allows them
6206 in cases like { ... (struct foo) { ... } ... }. */
6208 struct initializer_stack
6210 struct initializer_stack *next;
6211 tree decl;
6212 struct constructor_stack *constructor_stack;
6213 struct constructor_range_stack *constructor_range_stack;
6214 VEC(constructor_elt,gc) *elements;
6215 struct spelling *spelling;
6216 struct spelling *spelling_base;
6217 int spelling_size;
6218 char top_level;
6219 char require_constant_value;
6220 char require_constant_elements;
6223 static struct initializer_stack *initializer_stack;
6225 /* Prepare to parse and output the initializer for variable DECL. */
6227 void
6228 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6230 const char *locus;
6231 struct initializer_stack *p = XNEW (struct initializer_stack);
6233 p->decl = constructor_decl;
6234 p->require_constant_value = require_constant_value;
6235 p->require_constant_elements = require_constant_elements;
6236 p->constructor_stack = constructor_stack;
6237 p->constructor_range_stack = constructor_range_stack;
6238 p->elements = constructor_elements;
6239 p->spelling = spelling;
6240 p->spelling_base = spelling_base;
6241 p->spelling_size = spelling_size;
6242 p->top_level = constructor_top_level;
6243 p->next = initializer_stack;
6244 initializer_stack = p;
6246 constructor_decl = decl;
6247 constructor_designated = 0;
6248 constructor_top_level = top_level;
6250 if (decl != 0 && decl != error_mark_node)
6252 require_constant_value = TREE_STATIC (decl);
6253 require_constant_elements
6254 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6255 /* For a scalar, you can always use any value to initialize,
6256 even within braces. */
6257 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6258 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6259 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6260 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6261 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6263 else
6265 require_constant_value = 0;
6266 require_constant_elements = 0;
6267 locus = _("(anonymous)");
6270 constructor_stack = 0;
6271 constructor_range_stack = 0;
6273 missing_braces_mentioned = 0;
6275 spelling_base = 0;
6276 spelling_size = 0;
6277 RESTORE_SPELLING_DEPTH (0);
6279 if (locus)
6280 push_string (locus);
6283 void
6284 finish_init (void)
6286 struct initializer_stack *p = initializer_stack;
6288 /* Free the whole constructor stack of this initializer. */
6289 while (constructor_stack)
6291 struct constructor_stack *q = constructor_stack;
6292 constructor_stack = q->next;
6293 free (q);
6296 gcc_assert (!constructor_range_stack);
6298 /* Pop back to the data of the outer initializer (if any). */
6299 free (spelling_base);
6301 constructor_decl = p->decl;
6302 require_constant_value = p->require_constant_value;
6303 require_constant_elements = p->require_constant_elements;
6304 constructor_stack = p->constructor_stack;
6305 constructor_range_stack = p->constructor_range_stack;
6306 constructor_elements = p->elements;
6307 spelling = p->spelling;
6308 spelling_base = p->spelling_base;
6309 spelling_size = p->spelling_size;
6310 constructor_top_level = p->top_level;
6311 initializer_stack = p->next;
6312 free (p);
6315 /* Call here when we see the initializer is surrounded by braces.
6316 This is instead of a call to push_init_level;
6317 it is matched by a call to pop_init_level.
6319 TYPE is the type to initialize, for a constructor expression.
6320 For an initializer for a decl, TYPE is zero. */
6322 void
6323 really_start_incremental_init (tree type)
6325 struct constructor_stack *p = XNEW (struct constructor_stack);
6327 if (type == 0)
6328 type = TREE_TYPE (constructor_decl);
6330 if (TREE_CODE (type) == VECTOR_TYPE
6331 && TYPE_VECTOR_OPAQUE (type))
6332 error ("opaque vector types cannot be initialized");
6334 p->type = constructor_type;
6335 p->fields = constructor_fields;
6336 p->index = constructor_index;
6337 p->max_index = constructor_max_index;
6338 p->unfilled_index = constructor_unfilled_index;
6339 p->unfilled_fields = constructor_unfilled_fields;
6340 p->bit_index = constructor_bit_index;
6341 p->elements = constructor_elements;
6342 p->constant = constructor_constant;
6343 p->simple = constructor_simple;
6344 p->nonconst = constructor_nonconst;
6345 p->erroneous = constructor_erroneous;
6346 p->pending_elts = constructor_pending_elts;
6347 p->depth = constructor_depth;
6348 p->replacement_value.value = 0;
6349 p->replacement_value.original_code = ERROR_MARK;
6350 p->replacement_value.original_type = NULL;
6351 p->implicit = 0;
6352 p->range_stack = 0;
6353 p->outer = 0;
6354 p->incremental = constructor_incremental;
6355 p->designated = constructor_designated;
6356 p->next = 0;
6357 constructor_stack = p;
6359 constructor_constant = 1;
6360 constructor_simple = 1;
6361 constructor_nonconst = 0;
6362 constructor_depth = SPELLING_DEPTH ();
6363 constructor_elements = 0;
6364 constructor_pending_elts = 0;
6365 constructor_type = type;
6366 constructor_incremental = 1;
6367 constructor_designated = 0;
6368 designator_depth = 0;
6369 designator_erroneous = 0;
6371 if (TREE_CODE (constructor_type) == RECORD_TYPE
6372 || TREE_CODE (constructor_type) == UNION_TYPE)
6374 constructor_fields = TYPE_FIELDS (constructor_type);
6375 /* Skip any nameless bit fields at the beginning. */
6376 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6377 && DECL_NAME (constructor_fields) == 0)
6378 constructor_fields = TREE_CHAIN (constructor_fields);
6380 constructor_unfilled_fields = constructor_fields;
6381 constructor_bit_index = bitsize_zero_node;
6383 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6385 if (TYPE_DOMAIN (constructor_type))
6387 constructor_max_index
6388 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6390 /* Detect non-empty initializations of zero-length arrays. */
6391 if (constructor_max_index == NULL_TREE
6392 && TYPE_SIZE (constructor_type))
6393 constructor_max_index = build_int_cst (NULL_TREE, -1);
6395 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6396 to initialize VLAs will cause a proper error; avoid tree
6397 checking errors as well by setting a safe value. */
6398 if (constructor_max_index
6399 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6400 constructor_max_index = build_int_cst (NULL_TREE, -1);
6402 constructor_index
6403 = convert (bitsizetype,
6404 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6406 else
6408 constructor_index = bitsize_zero_node;
6409 constructor_max_index = NULL_TREE;
6412 constructor_unfilled_index = constructor_index;
6414 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6416 /* Vectors are like simple fixed-size arrays. */
6417 constructor_max_index =
6418 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6419 constructor_index = bitsize_zero_node;
6420 constructor_unfilled_index = constructor_index;
6422 else
6424 /* Handle the case of int x = {5}; */
6425 constructor_fields = constructor_type;
6426 constructor_unfilled_fields = constructor_type;
6430 /* Push down into a subobject, for initialization.
6431 If this is for an explicit set of braces, IMPLICIT is 0.
6432 If it is because the next element belongs at a lower level,
6433 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6435 void
6436 push_init_level (int implicit, struct obstack * braced_init_obstack)
6438 struct constructor_stack *p;
6439 tree value = NULL_TREE;
6441 /* If we've exhausted any levels that didn't have braces,
6442 pop them now. If implicit == 1, this will have been done in
6443 process_init_element; do not repeat it here because in the case
6444 of excess initializers for an empty aggregate this leads to an
6445 infinite cycle of popping a level and immediately recreating
6446 it. */
6447 if (implicit != 1)
6449 while (constructor_stack->implicit)
6451 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6452 || TREE_CODE (constructor_type) == UNION_TYPE)
6453 && constructor_fields == 0)
6454 process_init_element (pop_init_level (1, braced_init_obstack),
6455 true, braced_init_obstack);
6456 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6457 && constructor_max_index
6458 && tree_int_cst_lt (constructor_max_index,
6459 constructor_index))
6460 process_init_element (pop_init_level (1, braced_init_obstack),
6461 true, braced_init_obstack);
6462 else
6463 break;
6467 /* Unless this is an explicit brace, we need to preserve previous
6468 content if any. */
6469 if (implicit)
6471 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6472 || TREE_CODE (constructor_type) == UNION_TYPE)
6473 && constructor_fields)
6474 value = find_init_member (constructor_fields, braced_init_obstack);
6475 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6476 value = find_init_member (constructor_index, braced_init_obstack);
6479 p = XNEW (struct constructor_stack);
6480 p->type = constructor_type;
6481 p->fields = constructor_fields;
6482 p->index = constructor_index;
6483 p->max_index = constructor_max_index;
6484 p->unfilled_index = constructor_unfilled_index;
6485 p->unfilled_fields = constructor_unfilled_fields;
6486 p->bit_index = constructor_bit_index;
6487 p->elements = constructor_elements;
6488 p->constant = constructor_constant;
6489 p->simple = constructor_simple;
6490 p->nonconst = constructor_nonconst;
6491 p->erroneous = constructor_erroneous;
6492 p->pending_elts = constructor_pending_elts;
6493 p->depth = constructor_depth;
6494 p->replacement_value.value = 0;
6495 p->replacement_value.original_code = ERROR_MARK;
6496 p->replacement_value.original_type = NULL;
6497 p->implicit = implicit;
6498 p->outer = 0;
6499 p->incremental = constructor_incremental;
6500 p->designated = constructor_designated;
6501 p->next = constructor_stack;
6502 p->range_stack = 0;
6503 constructor_stack = p;
6505 constructor_constant = 1;
6506 constructor_simple = 1;
6507 constructor_nonconst = 0;
6508 constructor_depth = SPELLING_DEPTH ();
6509 constructor_elements = 0;
6510 constructor_incremental = 1;
6511 constructor_designated = 0;
6512 constructor_pending_elts = 0;
6513 if (!implicit)
6515 p->range_stack = constructor_range_stack;
6516 constructor_range_stack = 0;
6517 designator_depth = 0;
6518 designator_erroneous = 0;
6521 /* Don't die if an entire brace-pair level is superfluous
6522 in the containing level. */
6523 if (constructor_type == 0)
6525 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6526 || TREE_CODE (constructor_type) == UNION_TYPE)
6528 /* Don't die if there are extra init elts at the end. */
6529 if (constructor_fields == 0)
6530 constructor_type = 0;
6531 else
6533 constructor_type = TREE_TYPE (constructor_fields);
6534 push_member_name (constructor_fields);
6535 constructor_depth++;
6538 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6540 constructor_type = TREE_TYPE (constructor_type);
6541 push_array_bounds (tree_low_cst (constructor_index, 1));
6542 constructor_depth++;
6545 if (constructor_type == 0)
6547 error_init ("extra brace group at end of initializer");
6548 constructor_fields = 0;
6549 constructor_unfilled_fields = 0;
6550 return;
6553 if (value && TREE_CODE (value) == CONSTRUCTOR)
6555 constructor_constant = TREE_CONSTANT (value);
6556 constructor_simple = TREE_STATIC (value);
6557 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6558 constructor_elements = CONSTRUCTOR_ELTS (value);
6559 if (!VEC_empty (constructor_elt, constructor_elements)
6560 && (TREE_CODE (constructor_type) == RECORD_TYPE
6561 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6562 set_nonincremental_init (braced_init_obstack);
6565 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6567 missing_braces_mentioned = 1;
6568 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6571 if (TREE_CODE (constructor_type) == RECORD_TYPE
6572 || TREE_CODE (constructor_type) == UNION_TYPE)
6574 constructor_fields = TYPE_FIELDS (constructor_type);
6575 /* Skip any nameless bit fields at the beginning. */
6576 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6577 && DECL_NAME (constructor_fields) == 0)
6578 constructor_fields = TREE_CHAIN (constructor_fields);
6580 constructor_unfilled_fields = constructor_fields;
6581 constructor_bit_index = bitsize_zero_node;
6583 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6585 /* Vectors are like simple fixed-size arrays. */
6586 constructor_max_index =
6587 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6588 constructor_index = convert (bitsizetype, integer_zero_node);
6589 constructor_unfilled_index = constructor_index;
6591 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6593 if (TYPE_DOMAIN (constructor_type))
6595 constructor_max_index
6596 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6598 /* Detect non-empty initializations of zero-length arrays. */
6599 if (constructor_max_index == NULL_TREE
6600 && TYPE_SIZE (constructor_type))
6601 constructor_max_index = build_int_cst (NULL_TREE, -1);
6603 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6604 to initialize VLAs will cause a proper error; avoid tree
6605 checking errors as well by setting a safe value. */
6606 if (constructor_max_index
6607 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6608 constructor_max_index = build_int_cst (NULL_TREE, -1);
6610 constructor_index
6611 = convert (bitsizetype,
6612 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6614 else
6615 constructor_index = bitsize_zero_node;
6617 constructor_unfilled_index = constructor_index;
6618 if (value && TREE_CODE (value) == STRING_CST)
6620 /* We need to split the char/wchar array into individual
6621 characters, so that we don't have to special case it
6622 everywhere. */
6623 set_nonincremental_init_from_string (value, braced_init_obstack);
6626 else
6628 if (constructor_type != error_mark_node)
6629 warning_init (0, "braces around scalar initializer");
6630 constructor_fields = constructor_type;
6631 constructor_unfilled_fields = constructor_type;
6635 /* At the end of an implicit or explicit brace level,
6636 finish up that level of constructor. If a single expression
6637 with redundant braces initialized that level, return the
6638 c_expr structure for that expression. Otherwise, the original_code
6639 element is set to ERROR_MARK.
6640 If we were outputting the elements as they are read, return 0 as the value
6641 from inner levels (process_init_element ignores that),
6642 but return error_mark_node as the value from the outermost level
6643 (that's what we want to put in DECL_INITIAL).
6644 Otherwise, return a CONSTRUCTOR expression as the value. */
6646 struct c_expr
6647 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6649 struct constructor_stack *p;
6650 struct c_expr ret;
6651 ret.value = 0;
6652 ret.original_code = ERROR_MARK;
6653 ret.original_type = NULL;
6655 if (implicit == 0)
6657 /* When we come to an explicit close brace,
6658 pop any inner levels that didn't have explicit braces. */
6659 while (constructor_stack->implicit)
6661 process_init_element (pop_init_level (1, braced_init_obstack),
6662 true, braced_init_obstack);
6664 gcc_assert (!constructor_range_stack);
6667 /* Now output all pending elements. */
6668 constructor_incremental = 1;
6669 output_pending_init_elements (1, braced_init_obstack);
6671 p = constructor_stack;
6673 /* Error for initializing a flexible array member, or a zero-length
6674 array member in an inappropriate context. */
6675 if (constructor_type && constructor_fields
6676 && TREE_CODE (constructor_type) == ARRAY_TYPE
6677 && TYPE_DOMAIN (constructor_type)
6678 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6680 /* Silently discard empty initializations. The parser will
6681 already have pedwarned for empty brackets. */
6682 if (integer_zerop (constructor_unfilled_index))
6683 constructor_type = NULL_TREE;
6684 else
6686 gcc_assert (!TYPE_SIZE (constructor_type));
6688 if (constructor_depth > 2)
6689 error_init ("initialization of flexible array member in a nested context");
6690 else
6691 pedwarn_init (input_location, OPT_pedantic,
6692 "initialization of a flexible array member");
6694 /* We have already issued an error message for the existence
6695 of a flexible array member not at the end of the structure.
6696 Discard the initializer so that we do not die later. */
6697 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
6698 constructor_type = NULL_TREE;
6702 /* Warn when some struct elements are implicitly initialized to zero. */
6703 if (warn_missing_field_initializers
6704 && constructor_type
6705 && TREE_CODE (constructor_type) == RECORD_TYPE
6706 && constructor_unfilled_fields)
6708 /* Do not warn for flexible array members or zero-length arrays. */
6709 while (constructor_unfilled_fields
6710 && (!DECL_SIZE (constructor_unfilled_fields)
6711 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6712 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
6714 /* Do not warn if this level of the initializer uses member
6715 designators; it is likely to be deliberate. */
6716 if (constructor_unfilled_fields && !constructor_designated)
6718 push_member_name (constructor_unfilled_fields);
6719 warning_init (OPT_Wmissing_field_initializers,
6720 "missing initializer");
6721 RESTORE_SPELLING_DEPTH (constructor_depth);
6725 /* Pad out the end of the structure. */
6726 if (p->replacement_value.value)
6727 /* If this closes a superfluous brace pair,
6728 just pass out the element between them. */
6729 ret = p->replacement_value;
6730 else if (constructor_type == 0)
6732 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6733 && TREE_CODE (constructor_type) != UNION_TYPE
6734 && TREE_CODE (constructor_type) != ARRAY_TYPE
6735 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6737 /* A nonincremental scalar initializer--just return
6738 the element, after verifying there is just one. */
6739 if (VEC_empty (constructor_elt,constructor_elements))
6741 if (!constructor_erroneous)
6742 error_init ("empty scalar initializer");
6743 ret.value = error_mark_node;
6745 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6747 error_init ("extra elements in scalar initializer");
6748 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6750 else
6751 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6753 else
6755 if (constructor_erroneous)
6756 ret.value = error_mark_node;
6757 else
6759 ret.value = build_constructor (constructor_type,
6760 constructor_elements);
6761 if (constructor_constant)
6762 TREE_CONSTANT (ret.value) = 1;
6763 if (constructor_constant && constructor_simple)
6764 TREE_STATIC (ret.value) = 1;
6765 if (constructor_nonconst)
6766 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6770 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6772 if (constructor_nonconst)
6773 ret.original_code = C_MAYBE_CONST_EXPR;
6774 else if (ret.original_code == C_MAYBE_CONST_EXPR)
6775 ret.original_code = ERROR_MARK;
6778 constructor_type = p->type;
6779 constructor_fields = p->fields;
6780 constructor_index = p->index;
6781 constructor_max_index = p->max_index;
6782 constructor_unfilled_index = p->unfilled_index;
6783 constructor_unfilled_fields = p->unfilled_fields;
6784 constructor_bit_index = p->bit_index;
6785 constructor_elements = p->elements;
6786 constructor_constant = p->constant;
6787 constructor_simple = p->simple;
6788 constructor_nonconst = p->nonconst;
6789 constructor_erroneous = p->erroneous;
6790 constructor_incremental = p->incremental;
6791 constructor_designated = p->designated;
6792 constructor_pending_elts = p->pending_elts;
6793 constructor_depth = p->depth;
6794 if (!p->implicit)
6795 constructor_range_stack = p->range_stack;
6796 RESTORE_SPELLING_DEPTH (constructor_depth);
6798 constructor_stack = p->next;
6799 free (p);
6801 if (ret.value == 0 && constructor_stack == 0)
6802 ret.value = error_mark_node;
6803 return ret;
6806 /* Common handling for both array range and field name designators.
6807 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6809 static int
6810 set_designator (int array, struct obstack * braced_init_obstack)
6812 tree subtype;
6813 enum tree_code subcode;
6815 /* Don't die if an entire brace-pair level is superfluous
6816 in the containing level. */
6817 if (constructor_type == 0)
6818 return 1;
6820 /* If there were errors in this designator list already, bail out
6821 silently. */
6822 if (designator_erroneous)
6823 return 1;
6825 if (!designator_depth)
6827 gcc_assert (!constructor_range_stack);
6829 /* Designator list starts at the level of closest explicit
6830 braces. */
6831 while (constructor_stack->implicit)
6833 process_init_element (pop_init_level (1, braced_init_obstack),
6834 true, braced_init_obstack);
6836 constructor_designated = 1;
6837 return 0;
6840 switch (TREE_CODE (constructor_type))
6842 case RECORD_TYPE:
6843 case UNION_TYPE:
6844 subtype = TREE_TYPE (constructor_fields);
6845 if (subtype != error_mark_node)
6846 subtype = TYPE_MAIN_VARIANT (subtype);
6847 break;
6848 case ARRAY_TYPE:
6849 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6850 break;
6851 default:
6852 gcc_unreachable ();
6855 subcode = TREE_CODE (subtype);
6856 if (array && subcode != ARRAY_TYPE)
6858 error_init ("array index in non-array initializer");
6859 return 1;
6861 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
6863 error_init ("field name not in record or union initializer");
6864 return 1;
6867 constructor_designated = 1;
6868 push_init_level (2, braced_init_obstack);
6869 return 0;
6872 /* If there are range designators in designator list, push a new designator
6873 to constructor_range_stack. RANGE_END is end of such stack range or
6874 NULL_TREE if there is no range designator at this level. */
6876 static void
6877 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
6879 struct constructor_range_stack *p;
6881 p = (struct constructor_range_stack *)
6882 obstack_alloc (braced_init_obstack,
6883 sizeof (struct constructor_range_stack));
6884 p->prev = constructor_range_stack;
6885 p->next = 0;
6886 p->fields = constructor_fields;
6887 p->range_start = constructor_index;
6888 p->index = constructor_index;
6889 p->stack = constructor_stack;
6890 p->range_end = range_end;
6891 if (constructor_range_stack)
6892 constructor_range_stack->next = p;
6893 constructor_range_stack = p;
6896 /* Within an array initializer, specify the next index to be initialized.
6897 FIRST is that index. If LAST is nonzero, then initialize a range
6898 of indices, running from FIRST through LAST. */
6900 void
6901 set_init_index (tree first, tree last,
6902 struct obstack * braced_init_obstack)
6904 if (set_designator (1, braced_init_obstack))
6905 return;
6907 designator_erroneous = 1;
6909 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
6910 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
6912 error_init ("array index in initializer not of integer type");
6913 return;
6916 if (TREE_CODE (first) != INTEGER_CST)
6918 first = c_fully_fold (first, false, NULL);
6919 if (TREE_CODE (first) == INTEGER_CST)
6920 pedwarn_init (input_location, OPT_pedantic,
6921 "array index in initializer is not "
6922 "an integer constant expression");
6925 if (last && TREE_CODE (last) != INTEGER_CST)
6927 last = c_fully_fold (last, false, NULL);
6928 if (TREE_CODE (last) == INTEGER_CST)
6929 pedwarn_init (input_location, OPT_pedantic,
6930 "array index in initializer is not "
6931 "an integer constant expression");
6934 if (TREE_CODE (first) != INTEGER_CST)
6935 error_init ("nonconstant array index in initializer");
6936 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
6937 error_init ("nonconstant array index in initializer");
6938 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6939 error_init ("array index in non-array initializer");
6940 else if (tree_int_cst_sgn (first) == -1)
6941 error_init ("array index in initializer exceeds array bounds");
6942 else if (constructor_max_index
6943 && tree_int_cst_lt (constructor_max_index, first))
6944 error_init ("array index in initializer exceeds array bounds");
6945 else
6947 constant_expression_warning (first);
6948 if (last)
6949 constant_expression_warning (last);
6950 constructor_index = convert (bitsizetype, first);
6952 if (last)
6954 if (tree_int_cst_equal (first, last))
6955 last = 0;
6956 else if (tree_int_cst_lt (last, first))
6958 error_init ("empty index range in initializer");
6959 last = 0;
6961 else
6963 last = convert (bitsizetype, last);
6964 if (constructor_max_index != 0
6965 && tree_int_cst_lt (constructor_max_index, last))
6967 error_init ("array index range in initializer exceeds array bounds");
6968 last = 0;
6973 designator_depth++;
6974 designator_erroneous = 0;
6975 if (constructor_range_stack || last)
6976 push_range_stack (last, braced_init_obstack);
6980 /* Within a struct initializer, specify the next field to be initialized. */
6982 void
6983 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
6985 tree field;
6987 if (set_designator (0, braced_init_obstack))
6988 return;
6990 designator_erroneous = 1;
6992 if (TREE_CODE (constructor_type) != RECORD_TYPE
6993 && TREE_CODE (constructor_type) != UNION_TYPE)
6995 error_init ("field name not in record or union initializer");
6996 return;
6999 field = lookup_field (constructor_type, fieldname);
7001 if (field == 0)
7002 error ("unknown field %qE specified in initializer", fieldname);
7003 else
7006 constructor_fields = TREE_VALUE (field);
7007 designator_depth++;
7008 designator_erroneous = 0;
7009 if (constructor_range_stack)
7010 push_range_stack (NULL_TREE, braced_init_obstack);
7011 field = TREE_CHAIN (field);
7012 if (field)
7014 if (set_designator (0, braced_init_obstack))
7015 return;
7018 while (field != NULL_TREE);
7021 /* Add a new initializer to the tree of pending initializers. PURPOSE
7022 identifies the initializer, either array index or field in a structure.
7023 VALUE is the value of that index or field. If ORIGTYPE is not
7024 NULL_TREE, it is the original type of VALUE.
7026 IMPLICIT is true if value comes from pop_init_level (1),
7027 the new initializer has been merged with the existing one
7028 and thus no warnings should be emitted about overriding an
7029 existing initializer. */
7031 static void
7032 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7033 struct obstack * braced_init_obstack)
7035 struct init_node *p, **q, *r;
7037 q = &constructor_pending_elts;
7038 p = 0;
7040 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7042 while (*q != 0)
7044 p = *q;
7045 if (tree_int_cst_lt (purpose, p->purpose))
7046 q = &p->left;
7047 else if (tree_int_cst_lt (p->purpose, purpose))
7048 q = &p->right;
7049 else
7051 if (!implicit)
7053 if (TREE_SIDE_EFFECTS (p->value))
7054 warning_init (0, "initialized field with side-effects overwritten");
7055 else if (warn_override_init)
7056 warning_init (OPT_Woverride_init, "initialized field overwritten");
7058 p->value = value;
7059 p->origtype = origtype;
7060 return;
7064 else
7066 tree bitpos;
7068 bitpos = bit_position (purpose);
7069 while (*q != NULL)
7071 p = *q;
7072 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7073 q = &p->left;
7074 else if (p->purpose != purpose)
7075 q = &p->right;
7076 else
7078 if (!implicit)
7080 if (TREE_SIDE_EFFECTS (p->value))
7081 warning_init (0, "initialized field with side-effects overwritten");
7082 else if (warn_override_init)
7083 warning_init (OPT_Woverride_init, "initialized field overwritten");
7085 p->value = value;
7086 p->origtype = origtype;
7087 return;
7092 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7093 sizeof (struct init_node));
7094 r->purpose = purpose;
7095 r->value = value;
7096 r->origtype = origtype;
7098 *q = r;
7099 r->parent = p;
7100 r->left = 0;
7101 r->right = 0;
7102 r->balance = 0;
7104 while (p)
7106 struct init_node *s;
7108 if (r == p->left)
7110 if (p->balance == 0)
7111 p->balance = -1;
7112 else if (p->balance < 0)
7114 if (r->balance < 0)
7116 /* L rotation. */
7117 p->left = r->right;
7118 if (p->left)
7119 p->left->parent = p;
7120 r->right = p;
7122 p->balance = 0;
7123 r->balance = 0;
7125 s = p->parent;
7126 p->parent = r;
7127 r->parent = s;
7128 if (s)
7130 if (s->left == p)
7131 s->left = r;
7132 else
7133 s->right = r;
7135 else
7136 constructor_pending_elts = r;
7138 else
7140 /* LR rotation. */
7141 struct init_node *t = r->right;
7143 r->right = t->left;
7144 if (r->right)
7145 r->right->parent = r;
7146 t->left = r;
7148 p->left = t->right;
7149 if (p->left)
7150 p->left->parent = p;
7151 t->right = p;
7153 p->balance = t->balance < 0;
7154 r->balance = -(t->balance > 0);
7155 t->balance = 0;
7157 s = p->parent;
7158 p->parent = t;
7159 r->parent = t;
7160 t->parent = s;
7161 if (s)
7163 if (s->left == p)
7164 s->left = t;
7165 else
7166 s->right = t;
7168 else
7169 constructor_pending_elts = t;
7171 break;
7173 else
7175 /* p->balance == +1; growth of left side balances the node. */
7176 p->balance = 0;
7177 break;
7180 else /* r == p->right */
7182 if (p->balance == 0)
7183 /* Growth propagation from right side. */
7184 p->balance++;
7185 else if (p->balance > 0)
7187 if (r->balance > 0)
7189 /* R rotation. */
7190 p->right = r->left;
7191 if (p->right)
7192 p->right->parent = p;
7193 r->left = p;
7195 p->balance = 0;
7196 r->balance = 0;
7198 s = p->parent;
7199 p->parent = r;
7200 r->parent = s;
7201 if (s)
7203 if (s->left == p)
7204 s->left = r;
7205 else
7206 s->right = r;
7208 else
7209 constructor_pending_elts = r;
7211 else /* r->balance == -1 */
7213 /* RL rotation */
7214 struct init_node *t = r->left;
7216 r->left = t->right;
7217 if (r->left)
7218 r->left->parent = r;
7219 t->right = r;
7221 p->right = t->left;
7222 if (p->right)
7223 p->right->parent = p;
7224 t->left = p;
7226 r->balance = (t->balance < 0);
7227 p->balance = -(t->balance > 0);
7228 t->balance = 0;
7230 s = p->parent;
7231 p->parent = t;
7232 r->parent = t;
7233 t->parent = s;
7234 if (s)
7236 if (s->left == p)
7237 s->left = t;
7238 else
7239 s->right = t;
7241 else
7242 constructor_pending_elts = t;
7244 break;
7246 else
7248 /* p->balance == -1; growth of right side balances the node. */
7249 p->balance = 0;
7250 break;
7254 r = p;
7255 p = p->parent;
7259 /* Build AVL tree from a sorted chain. */
7261 static void
7262 set_nonincremental_init (struct obstack * braced_init_obstack)
7264 unsigned HOST_WIDE_INT ix;
7265 tree index, value;
7267 if (TREE_CODE (constructor_type) != RECORD_TYPE
7268 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7269 return;
7271 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7273 add_pending_init (index, value, NULL_TREE, false,
7274 braced_init_obstack);
7276 constructor_elements = 0;
7277 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7279 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7280 /* Skip any nameless bit fields at the beginning. */
7281 while (constructor_unfilled_fields != 0
7282 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7283 && DECL_NAME (constructor_unfilled_fields) == 0)
7284 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7287 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7289 if (TYPE_DOMAIN (constructor_type))
7290 constructor_unfilled_index
7291 = convert (bitsizetype,
7292 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7293 else
7294 constructor_unfilled_index = bitsize_zero_node;
7296 constructor_incremental = 0;
7299 /* Build AVL tree from a string constant. */
7301 static void
7302 set_nonincremental_init_from_string (tree str,
7303 struct obstack * braced_init_obstack)
7305 tree value, purpose, type;
7306 HOST_WIDE_INT val[2];
7307 const char *p, *end;
7308 int byte, wchar_bytes, charwidth, bitpos;
7310 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7312 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7313 charwidth = TYPE_PRECISION (char_type_node);
7314 type = TREE_TYPE (constructor_type);
7315 p = TREE_STRING_POINTER (str);
7316 end = p + TREE_STRING_LENGTH (str);
7318 for (purpose = bitsize_zero_node;
7319 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7320 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7322 if (wchar_bytes == 1)
7324 val[1] = (unsigned char) *p++;
7325 val[0] = 0;
7327 else
7329 val[0] = 0;
7330 val[1] = 0;
7331 for (byte = 0; byte < wchar_bytes; byte++)
7333 if (BYTES_BIG_ENDIAN)
7334 bitpos = (wchar_bytes - byte - 1) * charwidth;
7335 else
7336 bitpos = byte * charwidth;
7337 val[bitpos < HOST_BITS_PER_WIDE_INT]
7338 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7339 << (bitpos % HOST_BITS_PER_WIDE_INT);
7343 if (!TYPE_UNSIGNED (type))
7345 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7346 if (bitpos < HOST_BITS_PER_WIDE_INT)
7348 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7350 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7351 val[0] = -1;
7354 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7356 if (val[1] < 0)
7357 val[0] = -1;
7359 else if (val[0] & (((HOST_WIDE_INT) 1)
7360 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7361 val[0] |= ((HOST_WIDE_INT) -1)
7362 << (bitpos - HOST_BITS_PER_WIDE_INT);
7365 value = build_int_cst_wide (type, val[1], val[0]);
7366 add_pending_init (purpose, value, NULL_TREE, false,
7367 braced_init_obstack);
7370 constructor_incremental = 0;
7373 /* Return value of FIELD in pending initializer or zero if the field was
7374 not initialized yet. */
7376 static tree
7377 find_init_member (tree field, struct obstack * braced_init_obstack)
7379 struct init_node *p;
7381 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7383 if (constructor_incremental
7384 && tree_int_cst_lt (field, constructor_unfilled_index))
7385 set_nonincremental_init (braced_init_obstack);
7387 p = constructor_pending_elts;
7388 while (p)
7390 if (tree_int_cst_lt (field, p->purpose))
7391 p = p->left;
7392 else if (tree_int_cst_lt (p->purpose, field))
7393 p = p->right;
7394 else
7395 return p->value;
7398 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7400 tree bitpos = bit_position (field);
7402 if (constructor_incremental
7403 && (!constructor_unfilled_fields
7404 || tree_int_cst_lt (bitpos,
7405 bit_position (constructor_unfilled_fields))))
7406 set_nonincremental_init (braced_init_obstack);
7408 p = constructor_pending_elts;
7409 while (p)
7411 if (field == p->purpose)
7412 return p->value;
7413 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7414 p = p->left;
7415 else
7416 p = p->right;
7419 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7421 if (!VEC_empty (constructor_elt, constructor_elements)
7422 && (VEC_last (constructor_elt, constructor_elements)->index
7423 == field))
7424 return VEC_last (constructor_elt, constructor_elements)->value;
7426 return 0;
7429 /* "Output" the next constructor element.
7430 At top level, really output it to assembler code now.
7431 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7432 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7433 TYPE is the data type that the containing data type wants here.
7434 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7435 If VALUE is a string constant, STRICT_STRING is true if it is
7436 unparenthesized or we should not warn here for it being parenthesized.
7437 For other types of VALUE, STRICT_STRING is not used.
7439 PENDING if non-nil means output pending elements that belong
7440 right after this element. (PENDING is normally 1;
7441 it is 0 while outputting pending elements, to avoid recursion.)
7443 IMPLICIT is true if value comes from pop_init_level (1),
7444 the new initializer has been merged with the existing one
7445 and thus no warnings should be emitted about overriding an
7446 existing initializer. */
7448 static void
7449 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7450 tree field, int pending, bool implicit,
7451 struct obstack * braced_init_obstack)
7453 tree semantic_type = NULL_TREE;
7454 constructor_elt *celt;
7455 bool maybe_const = true;
7456 bool npc;
7458 if (type == error_mark_node || value == error_mark_node)
7460 constructor_erroneous = 1;
7461 return;
7463 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7464 && (TREE_CODE (value) == STRING_CST
7465 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7466 && !(TREE_CODE (value) == STRING_CST
7467 && TREE_CODE (type) == ARRAY_TYPE
7468 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7469 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7470 TYPE_MAIN_VARIANT (type)))
7471 value = array_to_pointer_conversion (input_location, value);
7473 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7474 && require_constant_value && !flag_isoc99 && pending)
7476 /* As an extension, allow initializing objects with static storage
7477 duration with compound literals (which are then treated just as
7478 the brace enclosed list they contain). */
7479 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7480 value = DECL_INITIAL (decl);
7483 npc = null_pointer_constant_p (value);
7484 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7486 semantic_type = TREE_TYPE (value);
7487 value = TREE_OPERAND (value, 0);
7489 value = c_fully_fold (value, require_constant_value, &maybe_const);
7491 if (value == error_mark_node)
7492 constructor_erroneous = 1;
7493 else if (!TREE_CONSTANT (value))
7494 constructor_constant = 0;
7495 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7496 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7497 || TREE_CODE (constructor_type) == UNION_TYPE)
7498 && DECL_C_BIT_FIELD (field)
7499 && TREE_CODE (value) != INTEGER_CST))
7500 constructor_simple = 0;
7501 if (!maybe_const)
7502 constructor_nonconst = 1;
7504 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7506 if (require_constant_value)
7508 error_init ("initializer element is not constant");
7509 value = error_mark_node;
7511 else if (require_constant_elements)
7512 pedwarn (input_location, 0,
7513 "initializer element is not computable at load time");
7515 else if (!maybe_const
7516 && (require_constant_value || require_constant_elements))
7517 pedwarn_init (input_location, 0,
7518 "initializer element is not a constant expression");
7520 /* Issue -Wc++-compat warnings about initializing a bitfield with
7521 enum type. */
7522 if (warn_cxx_compat
7523 && field != NULL_TREE
7524 && TREE_CODE (field) == FIELD_DECL
7525 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7526 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7527 != TYPE_MAIN_VARIANT (type))
7528 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7530 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7531 if (checktype != error_mark_node
7532 && (TYPE_MAIN_VARIANT (checktype)
7533 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7534 warning_init (OPT_Wc___compat,
7535 "enum conversion in initialization is invalid in C++");
7538 /* If this field is empty (and not at the end of structure),
7539 don't do anything other than checking the initializer. */
7540 if (field
7541 && (TREE_TYPE (field) == error_mark_node
7542 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7543 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7544 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7545 || TREE_CHAIN (field)))))
7546 return;
7548 if (semantic_type)
7549 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7550 value = digest_init (input_location, type, value, origtype, npc,
7551 strict_string, require_constant_value);
7552 if (value == error_mark_node)
7554 constructor_erroneous = 1;
7555 return;
7557 if (require_constant_value || require_constant_elements)
7558 constant_expression_warning (value);
7560 /* If this element doesn't come next in sequence,
7561 put it on constructor_pending_elts. */
7562 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7563 && (!constructor_incremental
7564 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7566 if (constructor_incremental
7567 && tree_int_cst_lt (field, constructor_unfilled_index))
7568 set_nonincremental_init (braced_init_obstack);
7570 add_pending_init (field, value, origtype, implicit,
7571 braced_init_obstack);
7572 return;
7574 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7575 && (!constructor_incremental
7576 || field != constructor_unfilled_fields))
7578 /* We do this for records but not for unions. In a union,
7579 no matter which field is specified, it can be initialized
7580 right away since it starts at the beginning of the union. */
7581 if (constructor_incremental)
7583 if (!constructor_unfilled_fields)
7584 set_nonincremental_init (braced_init_obstack);
7585 else
7587 tree bitpos, unfillpos;
7589 bitpos = bit_position (field);
7590 unfillpos = bit_position (constructor_unfilled_fields);
7592 if (tree_int_cst_lt (bitpos, unfillpos))
7593 set_nonincremental_init (braced_init_obstack);
7597 add_pending_init (field, value, origtype, implicit,
7598 braced_init_obstack);
7599 return;
7601 else if (TREE_CODE (constructor_type) == UNION_TYPE
7602 && !VEC_empty (constructor_elt, constructor_elements))
7604 if (!implicit)
7606 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7607 constructor_elements)->value))
7608 warning_init (0,
7609 "initialized field with side-effects overwritten");
7610 else if (warn_override_init)
7611 warning_init (OPT_Woverride_init, "initialized field overwritten");
7614 /* We can have just one union field set. */
7615 constructor_elements = 0;
7618 /* Otherwise, output this element either to
7619 constructor_elements or to the assembler file. */
7621 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7622 celt->index = field;
7623 celt->value = value;
7625 /* Advance the variable that indicates sequential elements output. */
7626 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7627 constructor_unfilled_index
7628 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7629 bitsize_one_node);
7630 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7632 constructor_unfilled_fields
7633 = TREE_CHAIN (constructor_unfilled_fields);
7635 /* Skip any nameless bit fields. */
7636 while (constructor_unfilled_fields != 0
7637 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7638 && DECL_NAME (constructor_unfilled_fields) == 0)
7639 constructor_unfilled_fields =
7640 TREE_CHAIN (constructor_unfilled_fields);
7642 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7643 constructor_unfilled_fields = 0;
7645 /* Now output any pending elements which have become next. */
7646 if (pending)
7647 output_pending_init_elements (0, braced_init_obstack);
7650 /* Output any pending elements which have become next.
7651 As we output elements, constructor_unfilled_{fields,index}
7652 advances, which may cause other elements to become next;
7653 if so, they too are output.
7655 If ALL is 0, we return when there are
7656 no more pending elements to output now.
7658 If ALL is 1, we output space as necessary so that
7659 we can output all the pending elements. */
7660 static void
7661 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7663 struct init_node *elt = constructor_pending_elts;
7664 tree next;
7666 retry:
7668 /* Look through the whole pending tree.
7669 If we find an element that should be output now,
7670 output it. Otherwise, set NEXT to the element
7671 that comes first among those still pending. */
7673 next = 0;
7674 while (elt)
7676 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7678 if (tree_int_cst_equal (elt->purpose,
7679 constructor_unfilled_index))
7680 output_init_element (elt->value, elt->origtype, true,
7681 TREE_TYPE (constructor_type),
7682 constructor_unfilled_index, 0, false,
7683 braced_init_obstack);
7684 else if (tree_int_cst_lt (constructor_unfilled_index,
7685 elt->purpose))
7687 /* Advance to the next smaller node. */
7688 if (elt->left)
7689 elt = elt->left;
7690 else
7692 /* We have reached the smallest node bigger than the
7693 current unfilled index. Fill the space first. */
7694 next = elt->purpose;
7695 break;
7698 else
7700 /* Advance to the next bigger node. */
7701 if (elt->right)
7702 elt = elt->right;
7703 else
7705 /* We have reached the biggest node in a subtree. Find
7706 the parent of it, which is the next bigger node. */
7707 while (elt->parent && elt->parent->right == elt)
7708 elt = elt->parent;
7709 elt = elt->parent;
7710 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7711 elt->purpose))
7713 next = elt->purpose;
7714 break;
7719 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7720 || TREE_CODE (constructor_type) == UNION_TYPE)
7722 tree ctor_unfilled_bitpos, elt_bitpos;
7724 /* If the current record is complete we are done. */
7725 if (constructor_unfilled_fields == 0)
7726 break;
7728 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7729 elt_bitpos = bit_position (elt->purpose);
7730 /* We can't compare fields here because there might be empty
7731 fields in between. */
7732 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7734 constructor_unfilled_fields = elt->purpose;
7735 output_init_element (elt->value, elt->origtype, true,
7736 TREE_TYPE (elt->purpose),
7737 elt->purpose, 0, false,
7738 braced_init_obstack);
7740 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7742 /* Advance to the next smaller node. */
7743 if (elt->left)
7744 elt = elt->left;
7745 else
7747 /* We have reached the smallest node bigger than the
7748 current unfilled field. Fill the space first. */
7749 next = elt->purpose;
7750 break;
7753 else
7755 /* Advance to the next bigger node. */
7756 if (elt->right)
7757 elt = elt->right;
7758 else
7760 /* We have reached the biggest node in a subtree. Find
7761 the parent of it, which is the next bigger node. */
7762 while (elt->parent && elt->parent->right == elt)
7763 elt = elt->parent;
7764 elt = elt->parent;
7765 if (elt
7766 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7767 bit_position (elt->purpose))))
7769 next = elt->purpose;
7770 break;
7777 /* Ordinarily return, but not if we want to output all
7778 and there are elements left. */
7779 if (!(all && next != 0))
7780 return;
7782 /* If it's not incremental, just skip over the gap, so that after
7783 jumping to retry we will output the next successive element. */
7784 if (TREE_CODE (constructor_type) == RECORD_TYPE
7785 || TREE_CODE (constructor_type) == UNION_TYPE)
7786 constructor_unfilled_fields = next;
7787 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7788 constructor_unfilled_index = next;
7790 /* ELT now points to the node in the pending tree with the next
7791 initializer to output. */
7792 goto retry;
7795 /* Add one non-braced element to the current constructor level.
7796 This adjusts the current position within the constructor's type.
7797 This may also start or terminate implicit levels
7798 to handle a partly-braced initializer.
7800 Once this has found the correct level for the new element,
7801 it calls output_init_element.
7803 IMPLICIT is true if value comes from pop_init_level (1),
7804 the new initializer has been merged with the existing one
7805 and thus no warnings should be emitted about overriding an
7806 existing initializer. */
7808 void
7809 process_init_element (struct c_expr value, bool implicit,
7810 struct obstack * braced_init_obstack)
7812 tree orig_value = value.value;
7813 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
7814 bool strict_string = value.original_code == STRING_CST;
7816 designator_depth = 0;
7817 designator_erroneous = 0;
7819 /* Handle superfluous braces around string cst as in
7820 char x[] = {"foo"}; */
7821 if (string_flag
7822 && constructor_type
7823 && TREE_CODE (constructor_type) == ARRAY_TYPE
7824 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
7825 && integer_zerop (constructor_unfilled_index))
7827 if (constructor_stack->replacement_value.value)
7828 error_init ("excess elements in char array initializer");
7829 constructor_stack->replacement_value = value;
7830 return;
7833 if (constructor_stack->replacement_value.value != 0)
7835 error_init ("excess elements in struct initializer");
7836 return;
7839 /* Ignore elements of a brace group if it is entirely superfluous
7840 and has already been diagnosed. */
7841 if (constructor_type == 0)
7842 return;
7844 /* If we've exhausted any levels that didn't have braces,
7845 pop them now. */
7846 while (constructor_stack->implicit)
7848 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7849 || TREE_CODE (constructor_type) == UNION_TYPE)
7850 && constructor_fields == 0)
7851 process_init_element (pop_init_level (1, braced_init_obstack),
7852 true, braced_init_obstack);
7853 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
7854 || TREE_CODE (constructor_type) == VECTOR_TYPE)
7855 && (constructor_max_index == 0
7856 || tree_int_cst_lt (constructor_max_index,
7857 constructor_index)))
7858 process_init_element (pop_init_level (1, braced_init_obstack),
7859 true, braced_init_obstack);
7860 else
7861 break;
7864 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7865 if (constructor_range_stack)
7867 /* If value is a compound literal and we'll be just using its
7868 content, don't put it into a SAVE_EXPR. */
7869 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
7870 || !require_constant_value
7871 || flag_isoc99)
7873 tree semantic_type = NULL_TREE;
7874 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
7876 semantic_type = TREE_TYPE (value.value);
7877 value.value = TREE_OPERAND (value.value, 0);
7879 value.value = c_save_expr (value.value);
7880 if (semantic_type)
7881 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7882 value.value);
7886 while (1)
7888 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7890 tree fieldtype;
7891 enum tree_code fieldcode;
7893 if (constructor_fields == 0)
7895 pedwarn_init (input_location, 0,
7896 "excess elements in struct initializer");
7897 break;
7900 fieldtype = TREE_TYPE (constructor_fields);
7901 if (fieldtype != error_mark_node)
7902 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7903 fieldcode = TREE_CODE (fieldtype);
7905 /* Error for non-static initialization of a flexible array member. */
7906 if (fieldcode == ARRAY_TYPE
7907 && !require_constant_value
7908 && TYPE_SIZE (fieldtype) == NULL_TREE
7909 && TREE_CHAIN (constructor_fields) == NULL_TREE)
7911 error_init ("non-static initialization of a flexible array member");
7912 break;
7915 /* Accept a string constant to initialize a subarray. */
7916 if (value.value != 0
7917 && fieldcode == ARRAY_TYPE
7918 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
7919 && string_flag)
7920 value.value = orig_value;
7921 /* Otherwise, if we have come to a subaggregate,
7922 and we don't have an element of its type, push into it. */
7923 else if (value.value != 0
7924 && value.value != error_mark_node
7925 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
7926 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
7927 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
7929 push_init_level (1, braced_init_obstack);
7930 continue;
7933 if (value.value)
7935 push_member_name (constructor_fields);
7936 output_init_element (value.value, value.original_type,
7937 strict_string, fieldtype,
7938 constructor_fields, 1, implicit,
7939 braced_init_obstack);
7940 RESTORE_SPELLING_DEPTH (constructor_depth);
7942 else
7943 /* Do the bookkeeping for an element that was
7944 directly output as a constructor. */
7946 /* For a record, keep track of end position of last field. */
7947 if (DECL_SIZE (constructor_fields))
7948 constructor_bit_index
7949 = size_binop_loc (input_location, PLUS_EXPR,
7950 bit_position (constructor_fields),
7951 DECL_SIZE (constructor_fields));
7953 /* If the current field was the first one not yet written out,
7954 it isn't now, so update. */
7955 if (constructor_unfilled_fields == constructor_fields)
7957 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
7958 /* Skip any nameless bit fields. */
7959 while (constructor_unfilled_fields != 0
7960 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7961 && DECL_NAME (constructor_unfilled_fields) == 0)
7962 constructor_unfilled_fields =
7963 TREE_CHAIN (constructor_unfilled_fields);
7967 constructor_fields = TREE_CHAIN (constructor_fields);
7968 /* Skip any nameless bit fields at the beginning. */
7969 while (constructor_fields != 0
7970 && DECL_C_BIT_FIELD (constructor_fields)
7971 && DECL_NAME (constructor_fields) == 0)
7972 constructor_fields = TREE_CHAIN (constructor_fields);
7974 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7976 tree fieldtype;
7977 enum tree_code fieldcode;
7979 if (constructor_fields == 0)
7981 pedwarn_init (input_location, 0,
7982 "excess elements in union initializer");
7983 break;
7986 fieldtype = TREE_TYPE (constructor_fields);
7987 if (fieldtype != error_mark_node)
7988 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
7989 fieldcode = TREE_CODE (fieldtype);
7991 /* Warn that traditional C rejects initialization of unions.
7992 We skip the warning if the value is zero. This is done
7993 under the assumption that the zero initializer in user
7994 code appears conditioned on e.g. __STDC__ to avoid
7995 "missing initializer" warnings and relies on default
7996 initialization to zero in the traditional C case.
7997 We also skip the warning if the initializer is designated,
7998 again on the assumption that this must be conditional on
7999 __STDC__ anyway (and we've already complained about the
8000 member-designator already). */
8001 if (!in_system_header && !constructor_designated
8002 && !(value.value && (integer_zerop (value.value)
8003 || real_zerop (value.value))))
8004 warning (OPT_Wtraditional, "traditional C rejects initialization "
8005 "of unions");
8007 /* Accept a string constant to initialize a subarray. */
8008 if (value.value != 0
8009 && fieldcode == ARRAY_TYPE
8010 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8011 && string_flag)
8012 value.value = orig_value;
8013 /* Otherwise, if we have come to a subaggregate,
8014 and we don't have an element of its type, push into it. */
8015 else if (value.value != 0
8016 && value.value != error_mark_node
8017 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8018 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8019 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8021 push_init_level (1, braced_init_obstack);
8022 continue;
8025 if (value.value)
8027 push_member_name (constructor_fields);
8028 output_init_element (value.value, value.original_type,
8029 strict_string, fieldtype,
8030 constructor_fields, 1, implicit,
8031 braced_init_obstack);
8032 RESTORE_SPELLING_DEPTH (constructor_depth);
8034 else
8035 /* Do the bookkeeping for an element that was
8036 directly output as a constructor. */
8038 constructor_bit_index = DECL_SIZE (constructor_fields);
8039 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
8042 constructor_fields = 0;
8044 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8046 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8047 enum tree_code eltcode = TREE_CODE (elttype);
8049 /* Accept a string constant to initialize a subarray. */
8050 if (value.value != 0
8051 && eltcode == ARRAY_TYPE
8052 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8053 && string_flag)
8054 value.value = orig_value;
8055 /* Otherwise, if we have come to a subaggregate,
8056 and we don't have an element of its type, push into it. */
8057 else if (value.value != 0
8058 && value.value != error_mark_node
8059 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8060 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8061 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8063 push_init_level (1, braced_init_obstack);
8064 continue;
8067 if (constructor_max_index != 0
8068 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8069 || integer_all_onesp (constructor_max_index)))
8071 pedwarn_init (input_location, 0,
8072 "excess elements in array initializer");
8073 break;
8076 /* Now output the actual element. */
8077 if (value.value)
8079 push_array_bounds (tree_low_cst (constructor_index, 1));
8080 output_init_element (value.value, value.original_type,
8081 strict_string, elttype,
8082 constructor_index, 1, implicit,
8083 braced_init_obstack);
8084 RESTORE_SPELLING_DEPTH (constructor_depth);
8087 constructor_index
8088 = size_binop_loc (input_location, PLUS_EXPR,
8089 constructor_index, bitsize_one_node);
8091 if (!value.value)
8092 /* If we are doing the bookkeeping for an element that was
8093 directly output as a constructor, we must update
8094 constructor_unfilled_index. */
8095 constructor_unfilled_index = constructor_index;
8097 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8099 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8101 /* Do a basic check of initializer size. Note that vectors
8102 always have a fixed size derived from their type. */
8103 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8105 pedwarn_init (input_location, 0,
8106 "excess elements in vector initializer");
8107 break;
8110 /* Now output the actual element. */
8111 if (value.value)
8113 if (TREE_CODE (value.value) == VECTOR_CST)
8114 elttype = TYPE_MAIN_VARIANT (constructor_type);
8115 output_init_element (value.value, value.original_type,
8116 strict_string, elttype,
8117 constructor_index, 1, implicit,
8118 braced_init_obstack);
8121 constructor_index
8122 = size_binop_loc (input_location,
8123 PLUS_EXPR, constructor_index, bitsize_one_node);
8125 if (!value.value)
8126 /* If we are doing the bookkeeping for an element that was
8127 directly output as a constructor, we must update
8128 constructor_unfilled_index. */
8129 constructor_unfilled_index = constructor_index;
8132 /* Handle the sole element allowed in a braced initializer
8133 for a scalar variable. */
8134 else if (constructor_type != error_mark_node
8135 && constructor_fields == 0)
8137 pedwarn_init (input_location, 0,
8138 "excess elements in scalar initializer");
8139 break;
8141 else
8143 if (value.value)
8144 output_init_element (value.value, value.original_type,
8145 strict_string, constructor_type,
8146 NULL_TREE, 1, implicit,
8147 braced_init_obstack);
8148 constructor_fields = 0;
8151 /* Handle range initializers either at this level or anywhere higher
8152 in the designator stack. */
8153 if (constructor_range_stack)
8155 struct constructor_range_stack *p, *range_stack;
8156 int finish = 0;
8158 range_stack = constructor_range_stack;
8159 constructor_range_stack = 0;
8160 while (constructor_stack != range_stack->stack)
8162 gcc_assert (constructor_stack->implicit);
8163 process_init_element (pop_init_level (1,
8164 braced_init_obstack),
8165 true, braced_init_obstack);
8167 for (p = range_stack;
8168 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8169 p = p->prev)
8171 gcc_assert (constructor_stack->implicit);
8172 process_init_element (pop_init_level (1, braced_init_obstack),
8173 true, braced_init_obstack);
8176 p->index = size_binop_loc (input_location,
8177 PLUS_EXPR, p->index, bitsize_one_node);
8178 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8179 finish = 1;
8181 while (1)
8183 constructor_index = p->index;
8184 constructor_fields = p->fields;
8185 if (finish && p->range_end && p->index == p->range_start)
8187 finish = 0;
8188 p->prev = 0;
8190 p = p->next;
8191 if (!p)
8192 break;
8193 push_init_level (2, braced_init_obstack);
8194 p->stack = constructor_stack;
8195 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8196 p->index = p->range_start;
8199 if (!finish)
8200 constructor_range_stack = range_stack;
8201 continue;
8204 break;
8207 constructor_range_stack = 0;
8210 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8211 (guaranteed to be 'volatile' or null) and ARGS (represented using
8212 an ASM_EXPR node). */
8213 tree
8214 build_asm_stmt (tree cv_qualifier, tree args)
8216 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8217 ASM_VOLATILE_P (args) = 1;
8218 return add_stmt (args);
8221 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8222 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8223 SIMPLE indicates whether there was anything at all after the
8224 string in the asm expression -- asm("blah") and asm("blah" : )
8225 are subtly different. We use a ASM_EXPR node to represent this. */
8226 tree
8227 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8228 tree clobbers, tree labels, bool simple)
8230 tree tail;
8231 tree args;
8232 int i;
8233 const char *constraint;
8234 const char **oconstraints;
8235 bool allows_mem, allows_reg, is_inout;
8236 int ninputs, noutputs;
8238 ninputs = list_length (inputs);
8239 noutputs = list_length (outputs);
8240 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8242 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8244 /* Remove output conversions that change the type but not the mode. */
8245 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8247 tree output = TREE_VALUE (tail);
8249 /* ??? Really, this should not be here. Users should be using a
8250 proper lvalue, dammit. But there's a long history of using casts
8251 in the output operands. In cases like longlong.h, this becomes a
8252 primitive form of typechecking -- if the cast can be removed, then
8253 the output operand had a type of the proper width; otherwise we'll
8254 get an error. Gross, but ... */
8255 STRIP_NOPS (output);
8257 if (!lvalue_or_else (output, lv_asm))
8258 output = error_mark_node;
8260 if (output != error_mark_node
8261 && (TREE_READONLY (output)
8262 || TYPE_READONLY (TREE_TYPE (output))
8263 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8264 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8265 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8266 readonly_error (output, lv_asm);
8268 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8269 oconstraints[i] = constraint;
8271 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8272 &allows_mem, &allows_reg, &is_inout))
8274 /* If the operand is going to end up in memory,
8275 mark it addressable. */
8276 if (!allows_reg && !c_mark_addressable (output))
8277 output = error_mark_node;
8279 else
8280 output = error_mark_node;
8282 TREE_VALUE (tail) = output;
8285 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8287 tree input;
8289 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8290 input = TREE_VALUE (tail);
8292 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8293 oconstraints, &allows_mem, &allows_reg))
8295 /* If the operand is going to end up in memory,
8296 mark it addressable. */
8297 if (!allows_reg && allows_mem)
8299 /* Strip the nops as we allow this case. FIXME, this really
8300 should be rejected or made deprecated. */
8301 STRIP_NOPS (input);
8302 if (!c_mark_addressable (input))
8303 input = error_mark_node;
8306 else
8307 input = error_mark_node;
8309 TREE_VALUE (tail) = input;
8312 /* ASMs with labels cannot have outputs. This should have been
8313 enforced by the parser. */
8314 gcc_assert (outputs == NULL || labels == NULL);
8316 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8318 /* asm statements without outputs, including simple ones, are treated
8319 as volatile. */
8320 ASM_INPUT_P (args) = simple;
8321 ASM_VOLATILE_P (args) = (noutputs == 0);
8323 return args;
8326 /* Generate a goto statement to LABEL. LOC is the location of the
8327 GOTO. */
8329 tree
8330 c_finish_goto_label (location_t loc, tree label)
8332 tree decl = lookup_label_for_goto (loc, label);
8333 if (!decl)
8334 return NULL_TREE;
8335 TREE_USED (decl) = 1;
8337 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8338 SET_EXPR_LOCATION (t, loc);
8339 return add_stmt (t);
8343 /* Generate a computed goto statement to EXPR. LOC is the location of
8344 the GOTO. */
8346 tree
8347 c_finish_goto_ptr (location_t loc, tree expr)
8349 tree t;
8350 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8351 expr = c_fully_fold (expr, false, NULL);
8352 expr = convert (ptr_type_node, expr);
8353 t = build1 (GOTO_EXPR, void_type_node, expr);
8354 SET_EXPR_LOCATION (t, loc);
8355 return add_stmt (t);
8358 /* Generate a C `return' statement. RETVAL is the expression for what
8359 to return, or a null pointer for `return;' with no value. LOC is
8360 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8361 is the original type of RETVAL. */
8363 tree
8364 c_finish_return (location_t loc, tree retval, tree origtype)
8366 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8367 bool no_warning = false;
8368 bool npc = false;
8370 if (TREE_THIS_VOLATILE (current_function_decl))
8371 warning_at (loc, 0,
8372 "function declared %<noreturn%> has a %<return%> statement");
8374 if (retval)
8376 tree semantic_type = NULL_TREE;
8377 npc = null_pointer_constant_p (retval);
8378 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8380 semantic_type = TREE_TYPE (retval);
8381 retval = TREE_OPERAND (retval, 0);
8383 retval = c_fully_fold (retval, false, NULL);
8384 if (semantic_type)
8385 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8388 if (!retval)
8390 current_function_returns_null = 1;
8391 if ((warn_return_type || flag_isoc99)
8392 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8394 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8395 "%<return%> with no value, in "
8396 "function returning non-void");
8397 no_warning = true;
8400 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8402 current_function_returns_null = 1;
8403 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8404 pedwarn (loc, 0,
8405 "%<return%> with a value, in function returning void");
8406 else
8407 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8408 "%<return%> with expression, in function returning void");
8410 else
8412 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8413 ic_return,
8414 npc, NULL_TREE, NULL_TREE, 0);
8415 tree res = DECL_RESULT (current_function_decl);
8416 tree inner;
8418 current_function_returns_value = 1;
8419 if (t == error_mark_node)
8420 return NULL_TREE;
8422 inner = t = convert (TREE_TYPE (res), t);
8424 /* Strip any conversions, additions, and subtractions, and see if
8425 we are returning the address of a local variable. Warn if so. */
8426 while (1)
8428 switch (TREE_CODE (inner))
8430 CASE_CONVERT:
8431 case NON_LVALUE_EXPR:
8432 case PLUS_EXPR:
8433 case POINTER_PLUS_EXPR:
8434 inner = TREE_OPERAND (inner, 0);
8435 continue;
8437 case MINUS_EXPR:
8438 /* If the second operand of the MINUS_EXPR has a pointer
8439 type (or is converted from it), this may be valid, so
8440 don't give a warning. */
8442 tree op1 = TREE_OPERAND (inner, 1);
8444 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8445 && (CONVERT_EXPR_P (op1)
8446 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8447 op1 = TREE_OPERAND (op1, 0);
8449 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8450 break;
8452 inner = TREE_OPERAND (inner, 0);
8453 continue;
8456 case ADDR_EXPR:
8457 inner = TREE_OPERAND (inner, 0);
8459 while (REFERENCE_CLASS_P (inner)
8460 && TREE_CODE (inner) != INDIRECT_REF)
8461 inner = TREE_OPERAND (inner, 0);
8463 if (DECL_P (inner)
8464 && !DECL_EXTERNAL (inner)
8465 && !TREE_STATIC (inner)
8466 && DECL_CONTEXT (inner) == current_function_decl)
8467 warning_at (loc,
8468 0, "function returns address of local variable");
8469 break;
8471 default:
8472 break;
8475 break;
8478 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8479 SET_EXPR_LOCATION (retval, loc);
8481 if (warn_sequence_point)
8482 verify_sequence_points (retval);
8485 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8486 TREE_NO_WARNING (ret_stmt) |= no_warning;
8487 return add_stmt (ret_stmt);
8490 struct c_switch {
8491 /* The SWITCH_EXPR being built. */
8492 tree switch_expr;
8494 /* The original type of the testing expression, i.e. before the
8495 default conversion is applied. */
8496 tree orig_type;
8498 /* A splay-tree mapping the low element of a case range to the high
8499 element, or NULL_TREE if there is no high element. Used to
8500 determine whether or not a new case label duplicates an old case
8501 label. We need a tree, rather than simply a hash table, because
8502 of the GNU case range extension. */
8503 splay_tree cases;
8505 /* The bindings at the point of the switch. This is used for
8506 warnings crossing decls when branching to a case label. */
8507 struct c_spot_bindings *bindings;
8509 /* The next node on the stack. */
8510 struct c_switch *next;
8513 /* A stack of the currently active switch statements. The innermost
8514 switch statement is on the top of the stack. There is no need to
8515 mark the stack for garbage collection because it is only active
8516 during the processing of the body of a function, and we never
8517 collect at that point. */
8519 struct c_switch *c_switch_stack;
8521 /* Start a C switch statement, testing expression EXP. Return the new
8522 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8523 SWITCH_COND_LOC is the location of the switch's condition. */
8525 tree
8526 c_start_case (location_t switch_loc,
8527 location_t switch_cond_loc,
8528 tree exp)
8530 tree orig_type = error_mark_node;
8531 struct c_switch *cs;
8533 if (exp != error_mark_node)
8535 orig_type = TREE_TYPE (exp);
8537 if (!INTEGRAL_TYPE_P (orig_type))
8539 if (orig_type != error_mark_node)
8541 error_at (switch_cond_loc, "switch quantity not an integer");
8542 orig_type = error_mark_node;
8544 exp = integer_zero_node;
8546 else
8548 tree type = TYPE_MAIN_VARIANT (orig_type);
8550 if (!in_system_header
8551 && (type == long_integer_type_node
8552 || type == long_unsigned_type_node))
8553 warning_at (switch_cond_loc,
8554 OPT_Wtraditional, "%<long%> switch expression not "
8555 "converted to %<int%> in ISO C");
8557 exp = c_fully_fold (exp, false, NULL);
8558 exp = default_conversion (exp);
8560 if (warn_sequence_point)
8561 verify_sequence_points (exp);
8565 /* Add this new SWITCH_EXPR to the stack. */
8566 cs = XNEW (struct c_switch);
8567 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8568 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8569 cs->orig_type = orig_type;
8570 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8571 cs->bindings = c_get_switch_bindings ();
8572 cs->next = c_switch_stack;
8573 c_switch_stack = cs;
8575 return add_stmt (cs->switch_expr);
8578 /* Process a case label at location LOC. */
8580 tree
8581 do_case (location_t loc, tree low_value, tree high_value)
8583 tree label = NULL_TREE;
8585 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8587 low_value = c_fully_fold (low_value, false, NULL);
8588 if (TREE_CODE (low_value) == INTEGER_CST)
8589 pedwarn (input_location, OPT_pedantic,
8590 "case label is not an integer constant expression");
8593 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8595 high_value = c_fully_fold (high_value, false, NULL);
8596 if (TREE_CODE (high_value) == INTEGER_CST)
8597 pedwarn (input_location, OPT_pedantic,
8598 "case label is not an integer constant expression");
8601 if (c_switch_stack == NULL)
8603 if (low_value)
8604 error_at (loc, "case label not within a switch statement");
8605 else
8606 error_at (loc, "%<default%> label not within a switch statement");
8607 return NULL_TREE;
8610 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8611 EXPR_LOCATION (c_switch_stack->switch_expr),
8612 loc))
8613 return NULL_TREE;
8615 label = c_add_case_label (loc, c_switch_stack->cases,
8616 SWITCH_COND (c_switch_stack->switch_expr),
8617 c_switch_stack->orig_type,
8618 low_value, high_value);
8619 if (label == error_mark_node)
8620 label = NULL_TREE;
8621 return label;
8624 /* Finish the switch statement. */
8626 void
8627 c_finish_case (tree body)
8629 struct c_switch *cs = c_switch_stack;
8630 location_t switch_location;
8632 SWITCH_BODY (cs->switch_expr) = body;
8634 /* Emit warnings as needed. */
8635 switch_location = EXPR_LOCATION (cs->switch_expr);
8636 c_do_switch_warnings (cs->cases, switch_location,
8637 TREE_TYPE (cs->switch_expr),
8638 SWITCH_COND (cs->switch_expr));
8640 /* Pop the stack. */
8641 c_switch_stack = cs->next;
8642 splay_tree_delete (cs->cases);
8643 c_release_switch_bindings (cs->bindings);
8644 XDELETE (cs);
8647 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8648 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8649 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8650 statement, and was not surrounded with parenthesis. */
8652 void
8653 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8654 tree else_block, bool nested_if)
8656 tree stmt;
8658 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8659 if (warn_parentheses && nested_if && else_block == NULL)
8661 tree inner_if = then_block;
8663 /* We know from the grammar productions that there is an IF nested
8664 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8665 it might not be exactly THEN_BLOCK, but should be the last
8666 non-container statement within. */
8667 while (1)
8668 switch (TREE_CODE (inner_if))
8670 case COND_EXPR:
8671 goto found;
8672 case BIND_EXPR:
8673 inner_if = BIND_EXPR_BODY (inner_if);
8674 break;
8675 case STATEMENT_LIST:
8676 inner_if = expr_last (then_block);
8677 break;
8678 case TRY_FINALLY_EXPR:
8679 case TRY_CATCH_EXPR:
8680 inner_if = TREE_OPERAND (inner_if, 0);
8681 break;
8682 default:
8683 gcc_unreachable ();
8685 found:
8687 if (COND_EXPR_ELSE (inner_if))
8688 warning_at (if_locus, OPT_Wparentheses,
8689 "suggest explicit braces to avoid ambiguous %<else%>");
8692 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8693 SET_EXPR_LOCATION (stmt, if_locus);
8694 add_stmt (stmt);
8697 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8698 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8699 is false for DO loops. INCR is the FOR increment expression. BODY is
8700 the statement controlled by the loop. BLAB is the break label. CLAB is
8701 the continue label. Everything is allowed to be NULL. */
8703 void
8704 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8705 tree blab, tree clab, bool cond_is_first)
8707 tree entry = NULL, exit = NULL, t;
8709 /* If the condition is zero don't generate a loop construct. */
8710 if (cond && integer_zerop (cond))
8712 if (cond_is_first)
8714 t = build_and_jump (&blab);
8715 SET_EXPR_LOCATION (t, start_locus);
8716 add_stmt (t);
8719 else
8721 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8723 /* If we have an exit condition, then we build an IF with gotos either
8724 out of the loop, or to the top of it. If there's no exit condition,
8725 then we just build a jump back to the top. */
8726 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8728 if (cond && !integer_nonzerop (cond))
8730 /* Canonicalize the loop condition to the end. This means
8731 generating a branch to the loop condition. Reuse the
8732 continue label, if possible. */
8733 if (cond_is_first)
8735 if (incr || !clab)
8737 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8738 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8740 else
8741 t = build1 (GOTO_EXPR, void_type_node, clab);
8742 SET_EXPR_LOCATION (t, start_locus);
8743 add_stmt (t);
8746 t = build_and_jump (&blab);
8747 if (cond_is_first)
8748 exit = fold_build3_loc (start_locus,
8749 COND_EXPR, void_type_node, cond, exit, t);
8750 else
8751 exit = fold_build3_loc (input_location,
8752 COND_EXPR, void_type_node, cond, exit, t);
8755 add_stmt (top);
8758 if (body)
8759 add_stmt (body);
8760 if (clab)
8761 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8762 if (incr)
8763 add_stmt (incr);
8764 if (entry)
8765 add_stmt (entry);
8766 if (exit)
8767 add_stmt (exit);
8768 if (blab)
8769 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8772 tree
8773 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
8775 bool skip;
8776 tree label = *label_p;
8778 /* In switch statements break is sometimes stylistically used after
8779 a return statement. This can lead to spurious warnings about
8780 control reaching the end of a non-void function when it is
8781 inlined. Note that we are calling block_may_fallthru with
8782 language specific tree nodes; this works because
8783 block_may_fallthru returns true when given something it does not
8784 understand. */
8785 skip = !block_may_fallthru (cur_stmt_list);
8787 if (!label)
8789 if (!skip)
8790 *label_p = label = create_artificial_label (loc);
8792 else if (TREE_CODE (label) == LABEL_DECL)
8794 else switch (TREE_INT_CST_LOW (label))
8796 case 0:
8797 if (is_break)
8798 error_at (loc, "break statement not within loop or switch");
8799 else
8800 error_at (loc, "continue statement not within a loop");
8801 return NULL_TREE;
8803 case 1:
8804 gcc_assert (is_break);
8805 error_at (loc, "break statement used with OpenMP for loop");
8806 return NULL_TREE;
8808 default:
8809 gcc_unreachable ();
8812 if (skip)
8813 return NULL_TREE;
8815 if (!is_break)
8816 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
8818 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
8821 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8823 static void
8824 emit_side_effect_warnings (location_t loc, tree expr)
8826 if (expr == error_mark_node)
8828 else if (!TREE_SIDE_EFFECTS (expr))
8830 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
8831 warning_at (loc, OPT_Wunused_value, "statement with no effect");
8833 else
8834 warn_if_unused_value (expr, loc);
8837 /* Process an expression as if it were a complete statement. Emit
8838 diagnostics, but do not call ADD_STMT. LOC is the location of the
8839 statement. */
8841 tree
8842 c_process_expr_stmt (location_t loc, tree expr)
8844 tree exprv;
8846 if (!expr)
8847 return NULL_TREE;
8849 expr = c_fully_fold (expr, false, NULL);
8851 if (warn_sequence_point)
8852 verify_sequence_points (expr);
8854 if (TREE_TYPE (expr) != error_mark_node
8855 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
8856 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
8857 error_at (loc, "expression statement has incomplete type");
8859 /* If we're not processing a statement expression, warn about unused values.
8860 Warnings for statement expressions will be emitted later, once we figure
8861 out which is the result. */
8862 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8863 && warn_unused_value)
8864 emit_side_effect_warnings (loc, expr);
8866 exprv = expr;
8867 while (TREE_CODE (exprv) == COMPOUND_EXPR)
8868 exprv = TREE_OPERAND (exprv, 1);
8869 if (DECL_P (exprv) || handled_component_p (exprv))
8870 mark_exp_read (exprv);
8872 /* If the expression is not of a type to which we cannot assign a line
8873 number, wrap the thing in a no-op NOP_EXPR. */
8874 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
8876 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
8877 SET_EXPR_LOCATION (expr, loc);
8880 return expr;
8883 /* Emit an expression as a statement. LOC is the location of the
8884 expression. */
8886 tree
8887 c_finish_expr_stmt (location_t loc, tree expr)
8889 if (expr)
8890 return add_stmt (c_process_expr_stmt (loc, expr));
8891 else
8892 return NULL;
8895 /* Do the opposite and emit a statement as an expression. To begin,
8896 create a new binding level and return it. */
8898 tree
8899 c_begin_stmt_expr (void)
8901 tree ret;
8903 /* We must force a BLOCK for this level so that, if it is not expanded
8904 later, there is a way to turn off the entire subtree of blocks that
8905 are contained in it. */
8906 keep_next_level ();
8907 ret = c_begin_compound_stmt (true);
8909 c_bindings_start_stmt_expr (c_switch_stack == NULL
8910 ? NULL
8911 : c_switch_stack->bindings);
8913 /* Mark the current statement list as belonging to a statement list. */
8914 STATEMENT_LIST_STMT_EXPR (ret) = 1;
8916 return ret;
8919 /* LOC is the location of the compound statement to which this body
8920 belongs. */
8922 tree
8923 c_finish_stmt_expr (location_t loc, tree body)
8925 tree last, type, tmp, val;
8926 tree *last_p;
8928 body = c_end_compound_stmt (loc, body, true);
8930 c_bindings_end_stmt_expr (c_switch_stack == NULL
8931 ? NULL
8932 : c_switch_stack->bindings);
8934 /* Locate the last statement in BODY. See c_end_compound_stmt
8935 about always returning a BIND_EXPR. */
8936 last_p = &BIND_EXPR_BODY (body);
8937 last = BIND_EXPR_BODY (body);
8939 continue_searching:
8940 if (TREE_CODE (last) == STATEMENT_LIST)
8942 tree_stmt_iterator i;
8944 /* This can happen with degenerate cases like ({ }). No value. */
8945 if (!TREE_SIDE_EFFECTS (last))
8946 return body;
8948 /* If we're supposed to generate side effects warnings, process
8949 all of the statements except the last. */
8950 if (warn_unused_value)
8952 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
8954 location_t tloc;
8955 tree t = tsi_stmt (i);
8957 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
8958 emit_side_effect_warnings (tloc, t);
8961 else
8962 i = tsi_last (last);
8963 last_p = tsi_stmt_ptr (i);
8964 last = *last_p;
8967 /* If the end of the list is exception related, then the list was split
8968 by a call to push_cleanup. Continue searching. */
8969 if (TREE_CODE (last) == TRY_FINALLY_EXPR
8970 || TREE_CODE (last) == TRY_CATCH_EXPR)
8972 last_p = &TREE_OPERAND (last, 0);
8973 last = *last_p;
8974 goto continue_searching;
8977 if (last == error_mark_node)
8978 return last;
8980 /* In the case that the BIND_EXPR is not necessary, return the
8981 expression out from inside it. */
8982 if (last == BIND_EXPR_BODY (body)
8983 && BIND_EXPR_VARS (body) == NULL)
8985 /* Even if this looks constant, do not allow it in a constant
8986 expression. */
8987 last = c_wrap_maybe_const (last, true);
8988 /* Do not warn if the return value of a statement expression is
8989 unused. */
8990 TREE_NO_WARNING (last) = 1;
8991 return last;
8994 /* Extract the type of said expression. */
8995 type = TREE_TYPE (last);
8997 /* If we're not returning a value at all, then the BIND_EXPR that
8998 we already have is a fine expression to return. */
8999 if (!type || VOID_TYPE_P (type))
9000 return body;
9002 /* Now that we've located the expression containing the value, it seems
9003 silly to make voidify_wrapper_expr repeat the process. Create a
9004 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9005 tmp = create_tmp_var_raw (type, NULL);
9007 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9008 tree_expr_nonnegative_p giving up immediately. */
9009 val = last;
9010 if (TREE_CODE (val) == NOP_EXPR
9011 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9012 val = TREE_OPERAND (val, 0);
9014 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9015 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9018 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9019 SET_EXPR_LOCATION (t, loc);
9020 return t;
9024 /* Begin and end compound statements. This is as simple as pushing
9025 and popping new statement lists from the tree. */
9027 tree
9028 c_begin_compound_stmt (bool do_scope)
9030 tree stmt = push_stmt_list ();
9031 if (do_scope)
9032 push_scope ();
9033 return stmt;
9036 /* End a compound statement. STMT is the statement. LOC is the
9037 location of the compound statement-- this is usually the location
9038 of the opening brace. */
9040 tree
9041 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9043 tree block = NULL;
9045 if (do_scope)
9047 if (c_dialect_objc ())
9048 objc_clear_super_receiver ();
9049 block = pop_scope ();
9052 stmt = pop_stmt_list (stmt);
9053 stmt = c_build_bind_expr (loc, block, stmt);
9055 /* If this compound statement is nested immediately inside a statement
9056 expression, then force a BIND_EXPR to be created. Otherwise we'll
9057 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9058 STATEMENT_LISTs merge, and thus we can lose track of what statement
9059 was really last. */
9060 if (cur_stmt_list
9061 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9062 && TREE_CODE (stmt) != BIND_EXPR)
9064 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9065 TREE_SIDE_EFFECTS (stmt) = 1;
9066 SET_EXPR_LOCATION (stmt, loc);
9069 return stmt;
9072 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9073 when the current scope is exited. EH_ONLY is true when this is not
9074 meant to apply to normal control flow transfer. */
9076 void
9077 push_cleanup (tree decl, tree cleanup, bool eh_only)
9079 enum tree_code code;
9080 tree stmt, list;
9081 bool stmt_expr;
9083 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9084 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9085 add_stmt (stmt);
9086 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9087 list = push_stmt_list ();
9088 TREE_OPERAND (stmt, 0) = list;
9089 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9092 /* Build a binary-operation expression without default conversions.
9093 CODE is the kind of expression to build.
9094 LOCATION is the operator's location.
9095 This function differs from `build' in several ways:
9096 the data type of the result is computed and recorded in it,
9097 warnings are generated if arg data types are invalid,
9098 special handling for addition and subtraction of pointers is known,
9099 and some optimization is done (operations on narrow ints
9100 are done in the narrower type when that gives the same result).
9101 Constant folding is also done before the result is returned.
9103 Note that the operands will never have enumeral types, or function
9104 or array types, because either they will have the default conversions
9105 performed or they have both just been converted to some other type in which
9106 the arithmetic is to be done. */
9108 tree
9109 build_binary_op (location_t location, enum tree_code code,
9110 tree orig_op0, tree orig_op1, int convert_p)
9112 tree type0, type1, orig_type0, orig_type1;
9113 tree eptype;
9114 enum tree_code code0, code1;
9115 tree op0, op1;
9116 tree ret = error_mark_node;
9117 const char *invalid_op_diag;
9118 bool op0_int_operands, op1_int_operands;
9119 bool int_const, int_const_or_overflow, int_operands;
9121 /* Expression code to give to the expression when it is built.
9122 Normally this is CODE, which is what the caller asked for,
9123 but in some special cases we change it. */
9124 enum tree_code resultcode = code;
9126 /* Data type in which the computation is to be performed.
9127 In the simplest cases this is the common type of the arguments. */
9128 tree result_type = NULL;
9130 /* When the computation is in excess precision, the type of the
9131 final EXCESS_PRECISION_EXPR. */
9132 tree semantic_result_type = NULL;
9134 /* Nonzero means operands have already been type-converted
9135 in whatever way is necessary.
9136 Zero means they need to be converted to RESULT_TYPE. */
9137 int converted = 0;
9139 /* Nonzero means create the expression with this type, rather than
9140 RESULT_TYPE. */
9141 tree build_type = 0;
9143 /* Nonzero means after finally constructing the expression
9144 convert it to this type. */
9145 tree final_type = 0;
9147 /* Nonzero if this is an operation like MIN or MAX which can
9148 safely be computed in short if both args are promoted shorts.
9149 Also implies COMMON.
9150 -1 indicates a bitwise operation; this makes a difference
9151 in the exact conditions for when it is safe to do the operation
9152 in a narrower mode. */
9153 int shorten = 0;
9155 /* Nonzero if this is a comparison operation;
9156 if both args are promoted shorts, compare the original shorts.
9157 Also implies COMMON. */
9158 int short_compare = 0;
9160 /* Nonzero if this is a right-shift operation, which can be computed on the
9161 original short and then promoted if the operand is a promoted short. */
9162 int short_shift = 0;
9164 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9165 int common = 0;
9167 /* True means types are compatible as far as ObjC is concerned. */
9168 bool objc_ok;
9170 /* True means this is an arithmetic operation that may need excess
9171 precision. */
9172 bool may_need_excess_precision;
9174 if (location == UNKNOWN_LOCATION)
9175 location = input_location;
9177 op0 = orig_op0;
9178 op1 = orig_op1;
9180 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9181 if (op0_int_operands)
9182 op0 = remove_c_maybe_const_expr (op0);
9183 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9184 if (op1_int_operands)
9185 op1 = remove_c_maybe_const_expr (op1);
9186 int_operands = (op0_int_operands && op1_int_operands);
9187 if (int_operands)
9189 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9190 && TREE_CODE (orig_op1) == INTEGER_CST);
9191 int_const = (int_const_or_overflow
9192 && !TREE_OVERFLOW (orig_op0)
9193 && !TREE_OVERFLOW (orig_op1));
9195 else
9196 int_const = int_const_or_overflow = false;
9198 if (convert_p)
9200 op0 = default_conversion (op0);
9201 op1 = default_conversion (op1);
9204 orig_type0 = type0 = TREE_TYPE (op0);
9205 orig_type1 = type1 = TREE_TYPE (op1);
9207 /* The expression codes of the data types of the arguments tell us
9208 whether the arguments are integers, floating, pointers, etc. */
9209 code0 = TREE_CODE (type0);
9210 code1 = TREE_CODE (type1);
9212 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9213 STRIP_TYPE_NOPS (op0);
9214 STRIP_TYPE_NOPS (op1);
9216 /* If an error was already reported for one of the arguments,
9217 avoid reporting another error. */
9219 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9220 return error_mark_node;
9222 if ((invalid_op_diag
9223 = targetm.invalid_binary_op (code, type0, type1)))
9225 error_at (location, invalid_op_diag);
9226 return error_mark_node;
9229 switch (code)
9231 case PLUS_EXPR:
9232 case MINUS_EXPR:
9233 case MULT_EXPR:
9234 case TRUNC_DIV_EXPR:
9235 case CEIL_DIV_EXPR:
9236 case FLOOR_DIV_EXPR:
9237 case ROUND_DIV_EXPR:
9238 case EXACT_DIV_EXPR:
9239 may_need_excess_precision = true;
9240 break;
9241 default:
9242 may_need_excess_precision = false;
9243 break;
9245 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9247 op0 = TREE_OPERAND (op0, 0);
9248 type0 = TREE_TYPE (op0);
9250 else if (may_need_excess_precision
9251 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9253 type0 = eptype;
9254 op0 = convert (eptype, op0);
9256 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9258 op1 = TREE_OPERAND (op1, 0);
9259 type1 = TREE_TYPE (op1);
9261 else if (may_need_excess_precision
9262 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9264 type1 = eptype;
9265 op1 = convert (eptype, op1);
9268 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9270 switch (code)
9272 case PLUS_EXPR:
9273 /* Handle the pointer + int case. */
9274 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9276 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9277 goto return_build_binary_op;
9279 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9281 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9282 goto return_build_binary_op;
9284 else
9285 common = 1;
9286 break;
9288 case MINUS_EXPR:
9289 /* Subtraction of two similar pointers.
9290 We must subtract them as integers, then divide by object size. */
9291 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9292 && comp_target_types (location, type0, type1))
9294 ret = pointer_diff (location, op0, op1);
9295 goto return_build_binary_op;
9297 /* Handle pointer minus int. Just like pointer plus int. */
9298 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9300 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9301 goto return_build_binary_op;
9303 else
9304 common = 1;
9305 break;
9307 case MULT_EXPR:
9308 common = 1;
9309 break;
9311 case TRUNC_DIV_EXPR:
9312 case CEIL_DIV_EXPR:
9313 case FLOOR_DIV_EXPR:
9314 case ROUND_DIV_EXPR:
9315 case EXACT_DIV_EXPR:
9316 warn_for_div_by_zero (location, op1);
9318 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9319 || code0 == FIXED_POINT_TYPE
9320 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9321 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9322 || code1 == FIXED_POINT_TYPE
9323 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9325 enum tree_code tcode0 = code0, tcode1 = code1;
9327 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9328 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9329 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9330 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9332 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9333 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9334 resultcode = RDIV_EXPR;
9335 else
9336 /* Although it would be tempting to shorten always here, that
9337 loses on some targets, since the modulo instruction is
9338 undefined if the quotient can't be represented in the
9339 computation mode. We shorten only if unsigned or if
9340 dividing by something we know != -1. */
9341 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9342 || (TREE_CODE (op1) == INTEGER_CST
9343 && !integer_all_onesp (op1)));
9344 common = 1;
9346 break;
9348 case BIT_AND_EXPR:
9349 case BIT_IOR_EXPR:
9350 case BIT_XOR_EXPR:
9351 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9352 shorten = -1;
9353 /* Allow vector types which are not floating point types. */
9354 else if (code0 == VECTOR_TYPE
9355 && code1 == VECTOR_TYPE
9356 && !VECTOR_FLOAT_TYPE_P (type0)
9357 && !VECTOR_FLOAT_TYPE_P (type1))
9358 common = 1;
9359 break;
9361 case TRUNC_MOD_EXPR:
9362 case FLOOR_MOD_EXPR:
9363 warn_for_div_by_zero (location, op1);
9365 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9366 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9367 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9368 common = 1;
9369 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9371 /* Although it would be tempting to shorten always here, that loses
9372 on some targets, since the modulo instruction is undefined if the
9373 quotient can't be represented in the computation mode. We shorten
9374 only if unsigned or if dividing by something we know != -1. */
9375 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9376 || (TREE_CODE (op1) == INTEGER_CST
9377 && !integer_all_onesp (op1)));
9378 common = 1;
9380 break;
9382 case TRUTH_ANDIF_EXPR:
9383 case TRUTH_ORIF_EXPR:
9384 case TRUTH_AND_EXPR:
9385 case TRUTH_OR_EXPR:
9386 case TRUTH_XOR_EXPR:
9387 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9388 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9389 || code0 == FIXED_POINT_TYPE)
9390 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9391 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9392 || code1 == FIXED_POINT_TYPE))
9394 /* Result of these operations is always an int,
9395 but that does not mean the operands should be
9396 converted to ints! */
9397 result_type = integer_type_node;
9398 op0 = c_common_truthvalue_conversion (location, op0);
9399 op1 = c_common_truthvalue_conversion (location, op1);
9400 converted = 1;
9402 if (code == TRUTH_ANDIF_EXPR)
9404 int_const_or_overflow = (int_operands
9405 && TREE_CODE (orig_op0) == INTEGER_CST
9406 && (op0 == truthvalue_false_node
9407 || TREE_CODE (orig_op1) == INTEGER_CST));
9408 int_const = (int_const_or_overflow
9409 && !TREE_OVERFLOW (orig_op0)
9410 && (op0 == truthvalue_false_node
9411 || !TREE_OVERFLOW (orig_op1)));
9413 else if (code == TRUTH_ORIF_EXPR)
9415 int_const_or_overflow = (int_operands
9416 && TREE_CODE (orig_op0) == INTEGER_CST
9417 && (op0 == truthvalue_true_node
9418 || TREE_CODE (orig_op1) == INTEGER_CST));
9419 int_const = (int_const_or_overflow
9420 && !TREE_OVERFLOW (orig_op0)
9421 && (op0 == truthvalue_true_node
9422 || !TREE_OVERFLOW (orig_op1)));
9424 break;
9426 /* Shift operations: result has same type as first operand;
9427 always convert second operand to int.
9428 Also set SHORT_SHIFT if shifting rightward. */
9430 case RSHIFT_EXPR:
9431 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9432 && code1 == INTEGER_TYPE)
9434 if (TREE_CODE (op1) == INTEGER_CST)
9436 if (tree_int_cst_sgn (op1) < 0)
9438 int_const = false;
9439 if (c_inhibit_evaluation_warnings == 0)
9440 warning (0, "right shift count is negative");
9442 else
9444 if (!integer_zerop (op1))
9445 short_shift = 1;
9447 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9449 int_const = false;
9450 if (c_inhibit_evaluation_warnings == 0)
9451 warning (0, "right shift count >= width of type");
9456 /* Use the type of the value to be shifted. */
9457 result_type = type0;
9458 /* Convert the shift-count to an integer, regardless of size
9459 of value being shifted. */
9460 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9461 op1 = convert (integer_type_node, op1);
9462 /* Avoid converting op1 to result_type later. */
9463 converted = 1;
9465 break;
9467 case LSHIFT_EXPR:
9468 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9469 && code1 == INTEGER_TYPE)
9471 if (TREE_CODE (op1) == INTEGER_CST)
9473 if (tree_int_cst_sgn (op1) < 0)
9475 int_const = false;
9476 if (c_inhibit_evaluation_warnings == 0)
9477 warning (0, "left shift count is negative");
9480 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9482 int_const = false;
9483 if (c_inhibit_evaluation_warnings == 0)
9484 warning (0, "left shift count >= width of type");
9488 /* Use the type of the value to be shifted. */
9489 result_type = type0;
9490 /* Convert the shift-count to an integer, regardless of size
9491 of value being shifted. */
9492 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9493 op1 = convert (integer_type_node, op1);
9494 /* Avoid converting op1 to result_type later. */
9495 converted = 1;
9497 break;
9499 case EQ_EXPR:
9500 case NE_EXPR:
9501 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9502 warning_at (location,
9503 OPT_Wfloat_equal,
9504 "comparing floating point with == or != is unsafe");
9505 /* Result of comparison is always int,
9506 but don't convert the args to int! */
9507 build_type = integer_type_node;
9508 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9509 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9510 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9511 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9512 short_compare = 1;
9513 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9515 if (TREE_CODE (op0) == ADDR_EXPR
9516 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9518 if (code == EQ_EXPR)
9519 warning_at (location,
9520 OPT_Waddress,
9521 "the comparison will always evaluate as %<false%> "
9522 "for the address of %qD will never be NULL",
9523 TREE_OPERAND (op0, 0));
9524 else
9525 warning_at (location,
9526 OPT_Waddress,
9527 "the comparison will always evaluate as %<true%> "
9528 "for the address of %qD will never be NULL",
9529 TREE_OPERAND (op0, 0));
9531 result_type = type0;
9533 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9535 if (TREE_CODE (op1) == ADDR_EXPR
9536 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9538 if (code == EQ_EXPR)
9539 warning_at (location,
9540 OPT_Waddress,
9541 "the comparison will always evaluate as %<false%> "
9542 "for the address of %qD will never be NULL",
9543 TREE_OPERAND (op1, 0));
9544 else
9545 warning_at (location,
9546 OPT_Waddress,
9547 "the comparison will always evaluate as %<true%> "
9548 "for the address of %qD will never be NULL",
9549 TREE_OPERAND (op1, 0));
9551 result_type = type1;
9553 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9555 tree tt0 = TREE_TYPE (type0);
9556 tree tt1 = TREE_TYPE (type1);
9557 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
9558 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
9559 addr_space_t as_common = ADDR_SPACE_GENERIC;
9561 /* Anything compares with void *. void * compares with anything.
9562 Otherwise, the targets must be compatible
9563 and both must be object or both incomplete. */
9564 if (comp_target_types (location, type0, type1))
9565 result_type = common_pointer_type (type0, type1);
9566 else if (!addr_space_superset (as0, as1, &as_common))
9568 error_at (location, "comparison of pointers to "
9569 "disjoint address spaces");
9570 return error_mark_node;
9572 else if (VOID_TYPE_P (tt0))
9574 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
9575 pedwarn (location, OPT_pedantic, "ISO C forbids "
9576 "comparison of %<void *%> with function pointer");
9578 else if (VOID_TYPE_P (tt1))
9580 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
9581 pedwarn (location, OPT_pedantic, "ISO C forbids "
9582 "comparison of %<void *%> with function pointer");
9584 else
9585 /* Avoid warning about the volatile ObjC EH puts on decls. */
9586 if (!objc_ok)
9587 pedwarn (location, 0,
9588 "comparison of distinct pointer types lacks a cast");
9590 if (result_type == NULL_TREE)
9592 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9593 result_type = build_pointer_type
9594 (build_qualified_type (void_type_node, qual));
9597 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9599 result_type = type0;
9600 pedwarn (location, 0, "comparison between pointer and integer");
9602 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9604 result_type = type1;
9605 pedwarn (location, 0, "comparison between pointer and integer");
9607 break;
9609 case LE_EXPR:
9610 case GE_EXPR:
9611 case LT_EXPR:
9612 case GT_EXPR:
9613 build_type = integer_type_node;
9614 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9615 || code0 == FIXED_POINT_TYPE)
9616 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9617 || code1 == FIXED_POINT_TYPE))
9618 short_compare = 1;
9619 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9621 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
9622 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
9623 addr_space_t as_common;
9625 if (comp_target_types (location, type0, type1))
9627 result_type = common_pointer_type (type0, type1);
9628 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9629 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9630 pedwarn (location, 0,
9631 "comparison of complete and incomplete pointers");
9632 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9633 pedwarn (location, OPT_pedantic, "ISO C forbids "
9634 "ordered comparisons of pointers to functions");
9635 else if (null_pointer_constant_p (orig_op0)
9636 || null_pointer_constant_p (orig_op1))
9637 warning_at (location, OPT_Wextra,
9638 "ordered comparison of pointer with null pointer");
9641 else if (!addr_space_superset (as0, as1, &as_common))
9643 error_at (location, "comparison of pointers to "
9644 "disjoint address spaces");
9645 return error_mark_node;
9647 else
9649 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9650 result_type = build_pointer_type
9651 (build_qualified_type (void_type_node, qual));
9652 pedwarn (location, 0,
9653 "comparison of distinct pointer types lacks a cast");
9656 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9658 result_type = type0;
9659 if (pedantic)
9660 pedwarn (location, OPT_pedantic,
9661 "ordered comparison of pointer with integer zero");
9662 else if (extra_warnings)
9663 warning_at (location, OPT_Wextra,
9664 "ordered comparison of pointer with integer zero");
9666 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9668 result_type = type1;
9669 if (pedantic)
9670 pedwarn (location, OPT_pedantic,
9671 "ordered comparison of pointer with integer zero");
9672 else if (extra_warnings)
9673 warning_at (location, OPT_Wextra,
9674 "ordered comparison of pointer with integer zero");
9676 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9678 result_type = type0;
9679 pedwarn (location, 0, "comparison between pointer and integer");
9681 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9683 result_type = type1;
9684 pedwarn (location, 0, "comparison between pointer and integer");
9686 break;
9688 default:
9689 gcc_unreachable ();
9692 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9693 return error_mark_node;
9695 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9696 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9697 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9698 TREE_TYPE (type1))))
9700 binary_op_error (location, code, type0, type1);
9701 return error_mark_node;
9704 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9705 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9707 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9708 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9710 bool first_complex = (code0 == COMPLEX_TYPE);
9711 bool second_complex = (code1 == COMPLEX_TYPE);
9712 int none_complex = (!first_complex && !second_complex);
9714 if (shorten || common || short_compare)
9716 result_type = c_common_type (type0, type1);
9717 if (result_type == error_mark_node)
9718 return error_mark_node;
9721 if (first_complex != second_complex
9722 && (code == PLUS_EXPR
9723 || code == MINUS_EXPR
9724 || code == MULT_EXPR
9725 || (code == TRUNC_DIV_EXPR && first_complex))
9726 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
9727 && flag_signed_zeros)
9729 /* An operation on mixed real/complex operands must be
9730 handled specially, but the language-independent code can
9731 more easily optimize the plain complex arithmetic if
9732 -fno-signed-zeros. */
9733 tree real_type = TREE_TYPE (result_type);
9734 tree real, imag;
9735 if (type0 != orig_type0 || type1 != orig_type1)
9737 gcc_assert (may_need_excess_precision && common);
9738 semantic_result_type = c_common_type (orig_type0, orig_type1);
9740 if (first_complex)
9742 if (TREE_TYPE (op0) != result_type)
9743 op0 = convert_and_check (result_type, op0);
9744 if (TREE_TYPE (op1) != real_type)
9745 op1 = convert_and_check (real_type, op1);
9747 else
9749 if (TREE_TYPE (op0) != real_type)
9750 op0 = convert_and_check (real_type, op0);
9751 if (TREE_TYPE (op1) != result_type)
9752 op1 = convert_and_check (result_type, op1);
9754 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9755 return error_mark_node;
9756 if (first_complex)
9758 op0 = c_save_expr (op0);
9759 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
9760 op0, 1);
9761 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
9762 op0, 1);
9763 switch (code)
9765 case MULT_EXPR:
9766 case TRUNC_DIV_EXPR:
9767 imag = build2 (resultcode, real_type, imag, op1);
9768 /* Fall through. */
9769 case PLUS_EXPR:
9770 case MINUS_EXPR:
9771 real = build2 (resultcode, real_type, real, op1);
9772 break;
9773 default:
9774 gcc_unreachable();
9777 else
9779 op1 = c_save_expr (op1);
9780 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
9781 op1, 1);
9782 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
9783 op1, 1);
9784 switch (code)
9786 case MULT_EXPR:
9787 imag = build2 (resultcode, real_type, op0, imag);
9788 /* Fall through. */
9789 case PLUS_EXPR:
9790 real = build2 (resultcode, real_type, op0, real);
9791 break;
9792 case MINUS_EXPR:
9793 real = build2 (resultcode, real_type, op0, real);
9794 imag = build1 (NEGATE_EXPR, real_type, imag);
9795 break;
9796 default:
9797 gcc_unreachable();
9800 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
9801 goto return_build_binary_op;
9804 /* For certain operations (which identify themselves by shorten != 0)
9805 if both args were extended from the same smaller type,
9806 do the arithmetic in that type and then extend.
9808 shorten !=0 and !=1 indicates a bitwise operation.
9809 For them, this optimization is safe only if
9810 both args are zero-extended or both are sign-extended.
9811 Otherwise, we might change the result.
9812 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9813 but calculated in (unsigned short) it would be (unsigned short)-1. */
9815 if (shorten && none_complex)
9817 final_type = result_type;
9818 result_type = shorten_binary_op (result_type, op0, op1,
9819 shorten == -1);
9822 /* Shifts can be shortened if shifting right. */
9824 if (short_shift)
9826 int unsigned_arg;
9827 tree arg0 = get_narrower (op0, &unsigned_arg);
9829 final_type = result_type;
9831 if (arg0 == op0 && final_type == TREE_TYPE (op0))
9832 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
9834 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9835 && tree_int_cst_sgn (op1) > 0
9836 /* We can shorten only if the shift count is less than the
9837 number of bits in the smaller type size. */
9838 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
9839 /* We cannot drop an unsigned shift after sign-extension. */
9840 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
9842 /* Do an unsigned shift if the operand was zero-extended. */
9843 result_type
9844 = c_common_signed_or_unsigned_type (unsigned_arg,
9845 TREE_TYPE (arg0));
9846 /* Convert value-to-be-shifted to that type. */
9847 if (TREE_TYPE (op0) != result_type)
9848 op0 = convert (result_type, op0);
9849 converted = 1;
9853 /* Comparison operations are shortened too but differently.
9854 They identify themselves by setting short_compare = 1. */
9856 if (short_compare)
9858 /* Don't write &op0, etc., because that would prevent op0
9859 from being kept in a register.
9860 Instead, make copies of the our local variables and
9861 pass the copies by reference, then copy them back afterward. */
9862 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
9863 enum tree_code xresultcode = resultcode;
9864 tree val
9865 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
9867 if (val != 0)
9869 ret = val;
9870 goto return_build_binary_op;
9873 op0 = xop0, op1 = xop1;
9874 converted = 1;
9875 resultcode = xresultcode;
9877 if (c_inhibit_evaluation_warnings == 0)
9879 bool op0_maybe_const = true;
9880 bool op1_maybe_const = true;
9881 tree orig_op0_folded, orig_op1_folded;
9883 if (in_late_binary_op)
9885 orig_op0_folded = orig_op0;
9886 orig_op1_folded = orig_op1;
9888 else
9890 /* Fold for the sake of possible warnings, as in
9891 build_conditional_expr. This requires the
9892 "original" values to be folded, not just op0 and
9893 op1. */
9894 c_inhibit_evaluation_warnings++;
9895 op0 = c_fully_fold (op0, require_constant_value,
9896 &op0_maybe_const);
9897 op1 = c_fully_fold (op1, require_constant_value,
9898 &op1_maybe_const);
9899 c_inhibit_evaluation_warnings--;
9900 orig_op0_folded = c_fully_fold (orig_op0,
9901 require_constant_value,
9902 NULL);
9903 orig_op1_folded = c_fully_fold (orig_op1,
9904 require_constant_value,
9905 NULL);
9908 if (warn_sign_compare)
9909 warn_for_sign_compare (location, orig_op0_folded,
9910 orig_op1_folded, op0, op1,
9911 result_type, resultcode);
9912 if (!in_late_binary_op)
9914 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
9915 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
9916 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
9917 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
9923 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
9924 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
9925 Then the expression will be built.
9926 It will be given type FINAL_TYPE if that is nonzero;
9927 otherwise, it will be given type RESULT_TYPE. */
9929 if (!result_type)
9931 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
9932 return error_mark_node;
9935 if (build_type == NULL_TREE)
9937 build_type = result_type;
9938 if (type0 != orig_type0 || type1 != orig_type1)
9940 gcc_assert (may_need_excess_precision && common);
9941 semantic_result_type = c_common_type (orig_type0, orig_type1);
9945 if (!converted)
9947 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
9948 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
9950 /* This can happen if one operand has a vector type, and the other
9951 has a different type. */
9952 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9953 return error_mark_node;
9956 /* Treat expressions in initializers specially as they can't trap. */
9957 if (int_const_or_overflow)
9958 ret = (require_constant_value
9959 ? fold_build2_initializer_loc (location, resultcode, build_type,
9960 op0, op1)
9961 : fold_build2_loc (location, resultcode, build_type, op0, op1));
9962 else
9963 ret = build2 (resultcode, build_type, op0, op1);
9964 if (final_type != 0)
9965 ret = convert (final_type, ret);
9967 return_build_binary_op:
9968 gcc_assert (ret != error_mark_node);
9969 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
9970 ret = (int_operands
9971 ? note_integer_operands (ret)
9972 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
9973 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
9974 && !in_late_binary_op)
9975 ret = note_integer_operands (ret);
9976 if (semantic_result_type)
9977 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
9978 protected_set_expr_location (ret, location);
9979 return ret;
9983 /* Convert EXPR to be a truth-value, validating its type for this
9984 purpose. LOCATION is the source location for the expression. */
9986 tree
9987 c_objc_common_truthvalue_conversion (location_t location, tree expr)
9989 bool int_const, int_operands;
9991 switch (TREE_CODE (TREE_TYPE (expr)))
9993 case ARRAY_TYPE:
9994 error_at (location, "used array that cannot be converted to pointer where scalar is required");
9995 return error_mark_node;
9997 case RECORD_TYPE:
9998 error_at (location, "used struct type value where scalar is required");
9999 return error_mark_node;
10001 case UNION_TYPE:
10002 error_at (location, "used union type value where scalar is required");
10003 return error_mark_node;
10005 case FUNCTION_TYPE:
10006 gcc_unreachable ();
10008 default:
10009 break;
10012 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10013 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10014 if (int_operands)
10015 expr = remove_c_maybe_const_expr (expr);
10017 /* ??? Should we also give an error for void and vectors rather than
10018 leaving those to give errors later? */
10019 expr = c_common_truthvalue_conversion (location, expr);
10021 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10023 if (TREE_OVERFLOW (expr))
10024 return expr;
10025 else
10026 return note_integer_operands (expr);
10028 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10029 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10030 return expr;
10034 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10035 required. */
10037 tree
10038 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10040 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10042 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10043 /* Executing a compound literal inside a function reinitializes
10044 it. */
10045 if (!TREE_STATIC (decl))
10046 *se = true;
10047 return decl;
10049 else
10050 return expr;
10053 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10055 tree
10056 c_begin_omp_parallel (void)
10058 tree block;
10060 keep_next_level ();
10061 block = c_begin_compound_stmt (true);
10063 return block;
10066 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10067 statement. LOC is the location of the OMP_PARALLEL. */
10069 tree
10070 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10072 tree stmt;
10074 block = c_end_compound_stmt (loc, block, true);
10076 stmt = make_node (OMP_PARALLEL);
10077 TREE_TYPE (stmt) = void_type_node;
10078 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10079 OMP_PARALLEL_BODY (stmt) = block;
10080 SET_EXPR_LOCATION (stmt, loc);
10082 return add_stmt (stmt);
10085 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10087 tree
10088 c_begin_omp_task (void)
10090 tree block;
10092 keep_next_level ();
10093 block = c_begin_compound_stmt (true);
10095 return block;
10098 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10099 statement. LOC is the location of the #pragma. */
10101 tree
10102 c_finish_omp_task (location_t loc, tree clauses, tree block)
10104 tree stmt;
10106 block = c_end_compound_stmt (loc, block, true);
10108 stmt = make_node (OMP_TASK);
10109 TREE_TYPE (stmt) = void_type_node;
10110 OMP_TASK_CLAUSES (stmt) = clauses;
10111 OMP_TASK_BODY (stmt) = block;
10112 SET_EXPR_LOCATION (stmt, loc);
10114 return add_stmt (stmt);
10117 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10118 Remove any elements from the list that are invalid. */
10120 tree
10121 c_finish_omp_clauses (tree clauses)
10123 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10124 tree c, t, *pc = &clauses;
10125 const char *name;
10127 bitmap_obstack_initialize (NULL);
10128 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10129 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10130 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10132 for (pc = &clauses, c = clauses; c ; c = *pc)
10134 bool remove = false;
10135 bool need_complete = false;
10136 bool need_implicitly_determined = false;
10138 switch (OMP_CLAUSE_CODE (c))
10140 case OMP_CLAUSE_SHARED:
10141 name = "shared";
10142 need_implicitly_determined = true;
10143 goto check_dup_generic;
10145 case OMP_CLAUSE_PRIVATE:
10146 name = "private";
10147 need_complete = true;
10148 need_implicitly_determined = true;
10149 goto check_dup_generic;
10151 case OMP_CLAUSE_REDUCTION:
10152 name = "reduction";
10153 need_implicitly_determined = true;
10154 t = OMP_CLAUSE_DECL (c);
10155 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10156 || POINTER_TYPE_P (TREE_TYPE (t)))
10158 error_at (OMP_CLAUSE_LOCATION (c),
10159 "%qE has invalid type for %<reduction%>", t);
10160 remove = true;
10162 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10164 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10165 const char *r_name = NULL;
10167 switch (r_code)
10169 case PLUS_EXPR:
10170 case MULT_EXPR:
10171 case MINUS_EXPR:
10172 break;
10173 case BIT_AND_EXPR:
10174 r_name = "&";
10175 break;
10176 case BIT_XOR_EXPR:
10177 r_name = "^";
10178 break;
10179 case BIT_IOR_EXPR:
10180 r_name = "|";
10181 break;
10182 case TRUTH_ANDIF_EXPR:
10183 r_name = "&&";
10184 break;
10185 case TRUTH_ORIF_EXPR:
10186 r_name = "||";
10187 break;
10188 default:
10189 gcc_unreachable ();
10191 if (r_name)
10193 error_at (OMP_CLAUSE_LOCATION (c),
10194 "%qE has invalid type for %<reduction(%s)%>",
10195 t, r_name);
10196 remove = true;
10199 goto check_dup_generic;
10201 case OMP_CLAUSE_COPYPRIVATE:
10202 name = "copyprivate";
10203 goto check_dup_generic;
10205 case OMP_CLAUSE_COPYIN:
10206 name = "copyin";
10207 t = OMP_CLAUSE_DECL (c);
10208 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10210 error_at (OMP_CLAUSE_LOCATION (c),
10211 "%qE must be %<threadprivate%> for %<copyin%>", t);
10212 remove = true;
10214 goto check_dup_generic;
10216 check_dup_generic:
10217 t = OMP_CLAUSE_DECL (c);
10218 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10220 error_at (OMP_CLAUSE_LOCATION (c),
10221 "%qE is not a variable in clause %qs", t, name);
10222 remove = true;
10224 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10225 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10226 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10228 error_at (OMP_CLAUSE_LOCATION (c),
10229 "%qE appears more than once in data clauses", t);
10230 remove = true;
10232 else
10233 bitmap_set_bit (&generic_head, DECL_UID (t));
10234 break;
10236 case OMP_CLAUSE_FIRSTPRIVATE:
10237 name = "firstprivate";
10238 t = OMP_CLAUSE_DECL (c);
10239 need_complete = true;
10240 need_implicitly_determined = true;
10241 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10243 error_at (OMP_CLAUSE_LOCATION (c),
10244 "%qE is not a variable in clause %<firstprivate%>", t);
10245 remove = true;
10247 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10248 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10250 error_at (OMP_CLAUSE_LOCATION (c),
10251 "%qE appears more than once in data clauses", t);
10252 remove = true;
10254 else
10255 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10256 break;
10258 case OMP_CLAUSE_LASTPRIVATE:
10259 name = "lastprivate";
10260 t = OMP_CLAUSE_DECL (c);
10261 need_complete = true;
10262 need_implicitly_determined = true;
10263 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10265 error_at (OMP_CLAUSE_LOCATION (c),
10266 "%qE is not a variable in clause %<lastprivate%>", t);
10267 remove = true;
10269 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10270 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10272 error_at (OMP_CLAUSE_LOCATION (c),
10273 "%qE appears more than once in data clauses", t);
10274 remove = true;
10276 else
10277 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10278 break;
10280 case OMP_CLAUSE_IF:
10281 case OMP_CLAUSE_NUM_THREADS:
10282 case OMP_CLAUSE_SCHEDULE:
10283 case OMP_CLAUSE_NOWAIT:
10284 case OMP_CLAUSE_ORDERED:
10285 case OMP_CLAUSE_DEFAULT:
10286 case OMP_CLAUSE_UNTIED:
10287 case OMP_CLAUSE_COLLAPSE:
10288 pc = &OMP_CLAUSE_CHAIN (c);
10289 continue;
10291 default:
10292 gcc_unreachable ();
10295 if (!remove)
10297 t = OMP_CLAUSE_DECL (c);
10299 if (need_complete)
10301 t = require_complete_type (t);
10302 if (t == error_mark_node)
10303 remove = true;
10306 if (need_implicitly_determined)
10308 const char *share_name = NULL;
10310 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10311 share_name = "threadprivate";
10312 else switch (c_omp_predetermined_sharing (t))
10314 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10315 break;
10316 case OMP_CLAUSE_DEFAULT_SHARED:
10317 share_name = "shared";
10318 break;
10319 case OMP_CLAUSE_DEFAULT_PRIVATE:
10320 share_name = "private";
10321 break;
10322 default:
10323 gcc_unreachable ();
10325 if (share_name)
10327 error_at (OMP_CLAUSE_LOCATION (c),
10328 "%qE is predetermined %qs for %qs",
10329 t, share_name, name);
10330 remove = true;
10335 if (remove)
10336 *pc = OMP_CLAUSE_CHAIN (c);
10337 else
10338 pc = &OMP_CLAUSE_CHAIN (c);
10341 bitmap_obstack_release (NULL);
10342 return clauses;
10345 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10346 down to the element type of an array. */
10348 tree
10349 c_build_qualified_type (tree type, int type_quals)
10351 if (type == error_mark_node)
10352 return type;
10354 if (TREE_CODE (type) == ARRAY_TYPE)
10356 tree t;
10357 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10358 type_quals);
10360 /* See if we already have an identically qualified type. */
10361 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10363 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10364 && TYPE_NAME (t) == TYPE_NAME (type)
10365 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10366 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10367 TYPE_ATTRIBUTES (type)))
10368 break;
10370 if (!t)
10372 tree domain = TYPE_DOMAIN (type);
10374 t = build_variant_type_copy (type);
10375 TREE_TYPE (t) = element_type;
10377 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10378 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10379 SET_TYPE_STRUCTURAL_EQUALITY (t);
10380 else if (TYPE_CANONICAL (element_type) != element_type
10381 || (domain && TYPE_CANONICAL (domain) != domain))
10383 tree unqualified_canon
10384 = build_array_type (TYPE_CANONICAL (element_type),
10385 domain? TYPE_CANONICAL (domain)
10386 : NULL_TREE);
10387 TYPE_CANONICAL (t)
10388 = c_build_qualified_type (unqualified_canon, type_quals);
10390 else
10391 TYPE_CANONICAL (t) = t;
10393 return t;
10396 /* A restrict-qualified pointer type must be a pointer to object or
10397 incomplete type. Note that the use of POINTER_TYPE_P also allows
10398 REFERENCE_TYPEs, which is appropriate for C++. */
10399 if ((type_quals & TYPE_QUAL_RESTRICT)
10400 && (!POINTER_TYPE_P (type)
10401 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10403 error ("invalid use of %<restrict%>");
10404 type_quals &= ~TYPE_QUAL_RESTRICT;
10407 return build_qualified_type (type, type_quals);
10410 /* Build a VA_ARG_EXPR for the C parser. */
10412 tree
10413 c_build_va_arg (location_t loc, tree expr, tree type)
10415 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10416 warning_at (loc, OPT_Wc___compat,
10417 "C++ requires promoted type, not enum type, in %<va_arg%>");
10418 return build_va_arg (loc, expr, type);