kernel - Fix races created by a comedy of circumstansces (3)
[dragonfly.git] / contrib / gcc-4.7 / gcc / c-typeck.c
blobc42c08745c25240700d8bde0f037b9078ee3dec6
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, 2011
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "langhooks.h"
34 #include "c-tree.h"
35 #include "c-lang.h"
36 #include "flags.h"
37 #include "output.h"
38 #include "intl.h"
39 #include "target.h"
40 #include "tree-iterator.h"
41 #include "bitmap.h"
42 #include "gimple.h"
43 #include "c-family/c-objc.h"
45 /* Possible cases of implicit bad conversions. Used to select
46 diagnostic messages in convert_for_assignment. */
47 enum impl_conv {
48 ic_argpass,
49 ic_assign,
50 ic_init,
51 ic_return
54 /* Possibe cases of scalar_to_vector conversion. */
55 enum stv_conv {
56 stv_error, /* Error occured. */
57 stv_nothing, /* Nothing happened. */
58 stv_firstarg, /* First argument must be expanded. */
59 stv_secondarg /* Second argument must be expanded. */
62 /* The level of nesting inside "__alignof__". */
63 int in_alignof;
65 /* The level of nesting inside "sizeof". */
66 int in_sizeof;
68 /* The level of nesting inside "typeof". */
69 int in_typeof;
71 /* Nonzero if we've already printed a "missing braces around initializer"
72 message within this initializer. */
73 static int missing_braces_mentioned;
75 static int require_constant_value;
76 static int require_constant_elements;
78 static bool null_pointer_constant_p (const_tree);
79 static tree qualify_type (tree, tree);
80 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
81 bool *);
82 static int comp_target_types (location_t, tree, tree);
83 static int function_types_compatible_p (const_tree, const_tree, bool *,
84 bool *);
85 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
86 static tree lookup_field (tree, tree);
87 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
88 tree);
89 static tree pointer_diff (location_t, tree, tree);
90 static tree convert_for_assignment (location_t, tree, tree, tree,
91 enum impl_conv, bool, tree, tree, int);
92 static tree valid_compound_expr_initializer (tree, tree);
93 static void push_string (const char *);
94 static void push_member_name (tree);
95 static int spelling_length (void);
96 static char *print_spelling (char *);
97 static void warning_init (int, const char *);
98 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
99 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
100 struct obstack *);
101 static void output_pending_init_elements (int, struct obstack *);
102 static int set_designator (int, struct obstack *);
103 static void push_range_stack (tree, struct obstack *);
104 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
105 static void set_nonincremental_init (struct obstack *);
106 static void set_nonincremental_init_from_string (tree, struct obstack *);
107 static tree find_init_member (tree, struct obstack *);
108 static void readonly_warning (tree, enum lvalue_use);
109 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
110 static void record_maybe_used_decl (tree);
111 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
113 /* Return true if EXP is a null pointer constant, false otherwise. */
115 static bool
116 null_pointer_constant_p (const_tree expr)
118 /* This should really operate on c_expr structures, but they aren't
119 yet available everywhere required. */
120 tree type = TREE_TYPE (expr);
121 return (TREE_CODE (expr) == INTEGER_CST
122 && !TREE_OVERFLOW (expr)
123 && integer_zerop (expr)
124 && (INTEGRAL_TYPE_P (type)
125 || (TREE_CODE (type) == POINTER_TYPE
126 && VOID_TYPE_P (TREE_TYPE (type))
127 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
130 /* EXPR may appear in an unevaluated part of an integer constant
131 expression, but not in an evaluated part. Wrap it in a
132 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
133 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
135 static tree
136 note_integer_operands (tree expr)
138 tree ret;
139 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
141 ret = copy_node (expr);
142 TREE_OVERFLOW (ret) = 1;
144 else
146 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
147 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
149 return ret;
152 /* Having checked whether EXPR may appear in an unevaluated part of an
153 integer constant expression and found that it may, remove any
154 C_MAYBE_CONST_EXPR noting this fact and return the resulting
155 expression. */
157 static inline tree
158 remove_c_maybe_const_expr (tree expr)
160 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
161 return C_MAYBE_CONST_EXPR_EXPR (expr);
162 else
163 return expr;
166 \f/* This is a cache to hold if two types are compatible or not. */
168 struct tagged_tu_seen_cache {
169 const struct tagged_tu_seen_cache * next;
170 const_tree t1;
171 const_tree t2;
172 /* The return value of tagged_types_tu_compatible_p if we had seen
173 these two types already. */
174 int val;
177 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
178 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
180 /* Do `exp = require_complete_type (exp);' to make sure exp
181 does not have an incomplete type. (That includes void types.) */
183 tree
184 require_complete_type (tree value)
186 tree type = TREE_TYPE (value);
188 if (value == error_mark_node || type == error_mark_node)
189 return error_mark_node;
191 /* First, detect a valid value with a complete type. */
192 if (COMPLETE_TYPE_P (type))
193 return value;
195 c_incomplete_type_error (value, type);
196 return error_mark_node;
199 /* Print an error message for invalid use of an incomplete type.
200 VALUE is the expression that was used (or 0 if that isn't known)
201 and TYPE is the type that was invalid. */
203 void
204 c_incomplete_type_error (const_tree value, const_tree type)
206 const char *type_code_string;
208 /* Avoid duplicate error message. */
209 if (TREE_CODE (type) == ERROR_MARK)
210 return;
212 if (value != 0 && (TREE_CODE (value) == VAR_DECL
213 || TREE_CODE (value) == PARM_DECL))
214 error ("%qD has an incomplete type", value);
215 else
217 retry:
218 /* We must print an error message. Be clever about what it says. */
220 switch (TREE_CODE (type))
222 case RECORD_TYPE:
223 type_code_string = "struct";
224 break;
226 case UNION_TYPE:
227 type_code_string = "union";
228 break;
230 case ENUMERAL_TYPE:
231 type_code_string = "enum";
232 break;
234 case VOID_TYPE:
235 error ("invalid use of void expression");
236 return;
238 case ARRAY_TYPE:
239 if (TYPE_DOMAIN (type))
241 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
243 error ("invalid use of flexible array member");
244 return;
246 type = TREE_TYPE (type);
247 goto retry;
249 error ("invalid use of array with unspecified bounds");
250 return;
252 default:
253 gcc_unreachable ();
256 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
257 error ("invalid use of undefined type %<%s %E%>",
258 type_code_string, TYPE_NAME (type));
259 else
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
261 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
265 /* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
268 tree
269 c_type_promotes_to (tree type)
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 return double_type_node;
274 if (c_promoting_integer_type_p (type))
276 /* Preserve unsignedness if not really getting any wider. */
277 if (TYPE_UNSIGNED (type)
278 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
279 return unsigned_type_node;
280 return integer_type_node;
283 return type;
286 /* Return true if between two named address spaces, whether there is a superset
287 named address space that encompasses both address spaces. If there is a
288 superset, return which address space is the superset. */
290 static bool
291 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
293 if (as1 == as2)
295 *common = as1;
296 return true;
298 else if (targetm.addr_space.subset_p (as1, as2))
300 *common = as2;
301 return true;
303 else if (targetm.addr_space.subset_p (as2, as1))
305 *common = as1;
306 return true;
308 else
309 return false;
312 /* Return a variant of TYPE which has all the type qualifiers of LIKE
313 as well as those of TYPE. */
315 static tree
316 qualify_type (tree type, tree like)
318 addr_space_t as_type = TYPE_ADDR_SPACE (type);
319 addr_space_t as_like = TYPE_ADDR_SPACE (like);
320 addr_space_t as_common;
322 /* If the two named address spaces are different, determine the common
323 superset address space. If there isn't one, raise an error. */
324 if (!addr_space_superset (as_type, as_like, &as_common))
326 as_common = as_type;
327 error ("%qT and %qT are in disjoint named address spaces",
328 type, like);
331 return c_build_qualified_type (type,
332 TYPE_QUALS_NO_ADDR_SPACE (type)
333 | TYPE_QUALS_NO_ADDR_SPACE (like)
334 | ENCODE_QUAL_ADDR_SPACE (as_common));
337 /* Return true iff the given tree T is a variable length array. */
339 bool
340 c_vla_type_p (const_tree t)
342 if (TREE_CODE (t) == ARRAY_TYPE
343 && C_TYPE_VARIABLE_SIZE (t))
344 return true;
345 return false;
348 /* Return the composite type of two compatible types.
350 We assume that comptypes has already been done and returned
351 nonzero; if that isn't so, this may crash. In particular, we
352 assume that qualifiers match. */
354 tree
355 composite_type (tree t1, tree t2)
357 enum tree_code code1;
358 enum tree_code code2;
359 tree attributes;
361 /* Save time if the two types are the same. */
363 if (t1 == t2) return t1;
365 /* If one type is nonsense, use the other. */
366 if (t1 == error_mark_node)
367 return t2;
368 if (t2 == error_mark_node)
369 return t1;
371 code1 = TREE_CODE (t1);
372 code2 = TREE_CODE (t2);
374 /* Merge the attributes. */
375 attributes = targetm.merge_type_attributes (t1, t2);
377 /* If one is an enumerated type and the other is the compatible
378 integer type, the composite type might be either of the two
379 (DR#013 question 3). For consistency, use the enumerated type as
380 the composite type. */
382 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
383 return t1;
384 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
385 return t2;
387 gcc_assert (code1 == code2);
389 switch (code1)
391 case POINTER_TYPE:
392 /* For two pointers, do this recursively on the target type. */
394 tree pointed_to_1 = TREE_TYPE (t1);
395 tree pointed_to_2 = TREE_TYPE (t2);
396 tree target = composite_type (pointed_to_1, pointed_to_2);
397 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
398 t1 = build_type_attribute_variant (t1, attributes);
399 return qualify_type (t1, t2);
402 case ARRAY_TYPE:
404 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
405 int quals;
406 tree unqual_elt;
407 tree d1 = TYPE_DOMAIN (t1);
408 tree d2 = TYPE_DOMAIN (t2);
409 bool d1_variable, d2_variable;
410 bool d1_zero, d2_zero;
411 bool t1_complete, t2_complete;
413 /* We should not have any type quals on arrays at all. */
414 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
415 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
417 t1_complete = COMPLETE_TYPE_P (t1);
418 t2_complete = COMPLETE_TYPE_P (t2);
420 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
421 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
423 d1_variable = (!d1_zero
424 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
425 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
426 d2_variable = (!d2_zero
427 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
428 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
429 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
430 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
432 /* Save space: see if the result is identical to one of the args. */
433 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
434 && (d2_variable || d2_zero || !d1_variable))
435 return build_type_attribute_variant (t1, attributes);
436 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
437 && (d1_variable || d1_zero || !d2_variable))
438 return build_type_attribute_variant (t2, attributes);
440 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
441 return build_type_attribute_variant (t1, attributes);
442 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t2, attributes);
445 /* Merge the element types, and have a size if either arg has
446 one. We may have qualifiers on the element types. To set
447 up TYPE_MAIN_VARIANT correctly, we need to form the
448 composite of the unqualified types and add the qualifiers
449 back at the end. */
450 quals = TYPE_QUALS (strip_array_types (elt));
451 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
452 t1 = build_array_type (unqual_elt,
453 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
454 && (d2_variable
455 || d2_zero
456 || !d1_variable))
457 ? t1
458 : t2));
459 /* Ensure a composite type involving a zero-length array type
460 is a zero-length type not an incomplete type. */
461 if (d1_zero && d2_zero
462 && (t1_complete || t2_complete)
463 && !COMPLETE_TYPE_P (t1))
465 TYPE_SIZE (t1) = bitsize_zero_node;
466 TYPE_SIZE_UNIT (t1) = size_zero_node;
468 t1 = c_build_qualified_type (t1, quals);
469 return build_type_attribute_variant (t1, attributes);
472 case ENUMERAL_TYPE:
473 case RECORD_TYPE:
474 case UNION_TYPE:
475 if (attributes != NULL)
477 /* Try harder not to create a new aggregate type. */
478 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
479 return t1;
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
481 return t2;
483 return build_type_attribute_variant (t1, attributes);
485 case FUNCTION_TYPE:
486 /* Function types: prefer the one that specified arg types.
487 If both do, merge the arg types. Also merge the return types. */
489 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
490 tree p1 = TYPE_ARG_TYPES (t1);
491 tree p2 = TYPE_ARG_TYPES (t2);
492 int len;
493 tree newargs, n;
494 int i;
496 /* Save space: see if the result is identical to one of the args. */
497 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
498 return build_type_attribute_variant (t1, attributes);
499 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
500 return build_type_attribute_variant (t2, attributes);
502 /* Simple way if one arg fails to specify argument types. */
503 if (TYPE_ARG_TYPES (t1) == 0)
505 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
506 t1 = build_type_attribute_variant (t1, attributes);
507 return qualify_type (t1, t2);
509 if (TYPE_ARG_TYPES (t2) == 0)
511 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
512 t1 = build_type_attribute_variant (t1, attributes);
513 return qualify_type (t1, t2);
516 /* If both args specify argument types, we must merge the two
517 lists, argument by argument. */
519 len = list_length (p1);
520 newargs = 0;
522 for (i = 0; i < len; i++)
523 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
525 n = newargs;
527 for (; p1;
528 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
530 /* A null type means arg type is not specified.
531 Take whatever the other function type has. */
532 if (TREE_VALUE (p1) == 0)
534 TREE_VALUE (n) = TREE_VALUE (p2);
535 goto parm_done;
537 if (TREE_VALUE (p2) == 0)
539 TREE_VALUE (n) = TREE_VALUE (p1);
540 goto parm_done;
543 /* Given wait (union {union wait *u; int *i} *)
544 and wait (union wait *),
545 prefer union wait * as type of parm. */
546 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
547 && TREE_VALUE (p1) != TREE_VALUE (p2))
549 tree memb;
550 tree mv2 = TREE_VALUE (p2);
551 if (mv2 && mv2 != error_mark_node
552 && TREE_CODE (mv2) != ARRAY_TYPE)
553 mv2 = TYPE_MAIN_VARIANT (mv2);
554 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
555 memb; memb = DECL_CHAIN (memb))
557 tree mv3 = TREE_TYPE (memb);
558 if (mv3 && mv3 != error_mark_node
559 && TREE_CODE (mv3) != ARRAY_TYPE)
560 mv3 = TYPE_MAIN_VARIANT (mv3);
561 if (comptypes (mv3, mv2))
563 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
564 TREE_VALUE (p2));
565 pedwarn (input_location, OPT_pedantic,
566 "function types not truly compatible in ISO C");
567 goto parm_done;
571 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
572 && TREE_VALUE (p2) != TREE_VALUE (p1))
574 tree memb;
575 tree mv1 = TREE_VALUE (p1);
576 if (mv1 && mv1 != error_mark_node
577 && TREE_CODE (mv1) != ARRAY_TYPE)
578 mv1 = TYPE_MAIN_VARIANT (mv1);
579 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
580 memb; memb = DECL_CHAIN (memb))
582 tree mv3 = TREE_TYPE (memb);
583 if (mv3 && mv3 != error_mark_node
584 && TREE_CODE (mv3) != ARRAY_TYPE)
585 mv3 = TYPE_MAIN_VARIANT (mv3);
586 if (comptypes (mv3, mv1))
588 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
589 TREE_VALUE (p1));
590 pedwarn (input_location, OPT_pedantic,
591 "function types not truly compatible in ISO C");
592 goto parm_done;
596 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
597 parm_done: ;
600 t1 = build_function_type (valtype, newargs);
601 t1 = qualify_type (t1, t2);
602 /* ... falls through ... */
605 default:
606 return build_type_attribute_variant (t1, attributes);
611 /* Return the type of a conditional expression between pointers to
612 possibly differently qualified versions of compatible types.
614 We assume that comp_target_types has already been done and returned
615 nonzero; if that isn't so, this may crash. */
617 static tree
618 common_pointer_type (tree t1, tree t2)
620 tree attributes;
621 tree pointed_to_1, mv1;
622 tree pointed_to_2, mv2;
623 tree target;
624 unsigned target_quals;
625 addr_space_t as1, as2, as_common;
626 int quals1, quals2;
628 /* Save time if the two types are the same. */
630 if (t1 == t2) return t1;
632 /* If one type is nonsense, use the other. */
633 if (t1 == error_mark_node)
634 return t2;
635 if (t2 == error_mark_node)
636 return t1;
638 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
639 && TREE_CODE (t2) == POINTER_TYPE);
641 /* Merge the attributes. */
642 attributes = targetm.merge_type_attributes (t1, t2);
644 /* Find the composite type of the target types, and combine the
645 qualifiers of the two types' targets. Do not lose qualifiers on
646 array element types by taking the TYPE_MAIN_VARIANT. */
647 mv1 = pointed_to_1 = TREE_TYPE (t1);
648 mv2 = pointed_to_2 = TREE_TYPE (t2);
649 if (TREE_CODE (mv1) != ARRAY_TYPE)
650 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
651 if (TREE_CODE (mv2) != ARRAY_TYPE)
652 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
653 target = composite_type (mv1, mv2);
655 /* For function types do not merge const qualifiers, but drop them
656 if used inconsistently. The middle-end uses these to mark const
657 and noreturn functions. */
658 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
659 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
661 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
662 target_quals = (quals1 & quals2);
663 else
664 target_quals = (quals1 | quals2);
666 /* If the two named address spaces are different, determine the common
667 superset address space. This is guaranteed to exist due to the
668 assumption that comp_target_type returned non-zero. */
669 as1 = TYPE_ADDR_SPACE (pointed_to_1);
670 as2 = TYPE_ADDR_SPACE (pointed_to_2);
671 if (!addr_space_superset (as1, as2, &as_common))
672 gcc_unreachable ();
674 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
676 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
677 return build_type_attribute_variant (t1, attributes);
680 /* Return the common type for two arithmetic types under the usual
681 arithmetic conversions. The default conversions have already been
682 applied, and enumerated types converted to their compatible integer
683 types. The resulting type is unqualified and has no attributes.
685 This is the type for the result of most arithmetic operations
686 if the operands have the given two types. */
688 static tree
689 c_common_type (tree t1, tree t2)
691 enum tree_code code1;
692 enum tree_code code2;
694 /* If one type is nonsense, use the other. */
695 if (t1 == error_mark_node)
696 return t2;
697 if (t2 == error_mark_node)
698 return t1;
700 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
701 t1 = TYPE_MAIN_VARIANT (t1);
703 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
704 t2 = TYPE_MAIN_VARIANT (t2);
706 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
707 t1 = build_type_attribute_variant (t1, NULL_TREE);
709 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
710 t2 = build_type_attribute_variant (t2, NULL_TREE);
712 /* Save time if the two types are the same. */
714 if (t1 == t2) return t1;
716 code1 = TREE_CODE (t1);
717 code2 = TREE_CODE (t2);
719 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
720 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
721 || code1 == INTEGER_TYPE);
722 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
723 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
724 || code2 == INTEGER_TYPE);
726 /* When one operand is a decimal float type, the other operand cannot be
727 a generic float type or a complex type. We also disallow vector types
728 here. */
729 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
730 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
732 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
734 error ("can%'t mix operands of decimal float and vector types");
735 return error_mark_node;
737 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
739 error ("can%'t mix operands of decimal float and complex types");
740 return error_mark_node;
742 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
744 error ("can%'t mix operands of decimal float and other float types");
745 return error_mark_node;
749 /* If one type is a vector type, return that type. (How the usual
750 arithmetic conversions apply to the vector types extension is not
751 precisely specified.) */
752 if (code1 == VECTOR_TYPE)
753 return t1;
755 if (code2 == VECTOR_TYPE)
756 return t2;
758 /* If one type is complex, form the common type of the non-complex
759 components, then make that complex. Use T1 or T2 if it is the
760 required type. */
761 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
763 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
764 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
765 tree subtype = c_common_type (subtype1, subtype2);
767 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
768 return t1;
769 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
770 return t2;
771 else
772 return build_complex_type (subtype);
775 /* If only one is real, use it as the result. */
777 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
778 return t1;
780 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
781 return t2;
783 /* If both are real and either are decimal floating point types, use
784 the decimal floating point type with the greater precision. */
786 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
788 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
789 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
790 return dfloat128_type_node;
791 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
792 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
793 return dfloat64_type_node;
794 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
795 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
796 return dfloat32_type_node;
799 /* Deal with fixed-point types. */
800 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
802 unsigned int unsignedp = 0, satp = 0;
803 enum machine_mode m1, m2;
804 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
806 m1 = TYPE_MODE (t1);
807 m2 = TYPE_MODE (t2);
809 /* If one input type is saturating, the result type is saturating. */
810 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
811 satp = 1;
813 /* If both fixed-point types are unsigned, the result type is unsigned.
814 When mixing fixed-point and integer types, follow the sign of the
815 fixed-point type.
816 Otherwise, the result type is signed. */
817 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
818 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
819 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
820 && TYPE_UNSIGNED (t1))
821 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
822 && TYPE_UNSIGNED (t2)))
823 unsignedp = 1;
825 /* The result type is signed. */
826 if (unsignedp == 0)
828 /* If the input type is unsigned, we need to convert to the
829 signed type. */
830 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
832 enum mode_class mclass = (enum mode_class) 0;
833 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
834 mclass = MODE_FRACT;
835 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
836 mclass = MODE_ACCUM;
837 else
838 gcc_unreachable ();
839 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
841 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
843 enum mode_class mclass = (enum mode_class) 0;
844 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
845 mclass = MODE_FRACT;
846 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
847 mclass = MODE_ACCUM;
848 else
849 gcc_unreachable ();
850 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
854 if (code1 == FIXED_POINT_TYPE)
856 fbit1 = GET_MODE_FBIT (m1);
857 ibit1 = GET_MODE_IBIT (m1);
859 else
861 fbit1 = 0;
862 /* Signed integers need to subtract one sign bit. */
863 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
866 if (code2 == FIXED_POINT_TYPE)
868 fbit2 = GET_MODE_FBIT (m2);
869 ibit2 = GET_MODE_IBIT (m2);
871 else
873 fbit2 = 0;
874 /* Signed integers need to subtract one sign bit. */
875 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
878 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
879 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
880 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
881 satp);
884 /* Both real or both integers; use the one with greater precision. */
886 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
887 return t1;
888 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
889 return t2;
891 /* Same precision. Prefer long longs to longs to ints when the
892 same precision, following the C99 rules on integer type rank
893 (which are equivalent to the C90 rules for C90 types). */
895 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
896 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
897 return long_long_unsigned_type_node;
899 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
900 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
902 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
903 return long_long_unsigned_type_node;
904 else
905 return long_long_integer_type_node;
908 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
910 return long_unsigned_type_node;
912 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
915 /* But preserve unsignedness from the other type,
916 since long cannot hold all the values of an unsigned int. */
917 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
918 return long_unsigned_type_node;
919 else
920 return long_integer_type_node;
923 /* Likewise, prefer long double to double even if same size. */
924 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
925 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
926 return long_double_type_node;
928 /* Otherwise prefer the unsigned one. */
930 if (TYPE_UNSIGNED (t1))
931 return t1;
932 else
933 return t2;
936 /* Wrapper around c_common_type that is used by c-common.c and other
937 front end optimizations that remove promotions. ENUMERAL_TYPEs
938 are allowed here and are converted to their compatible integer types.
939 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
940 preferably a non-Boolean type as the common type. */
941 tree
942 common_type (tree t1, tree t2)
944 if (TREE_CODE (t1) == ENUMERAL_TYPE)
945 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
946 if (TREE_CODE (t2) == ENUMERAL_TYPE)
947 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
949 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
950 if (TREE_CODE (t1) == BOOLEAN_TYPE
951 && TREE_CODE (t2) == BOOLEAN_TYPE)
952 return boolean_type_node;
954 /* If either type is BOOLEAN_TYPE, then return the other. */
955 if (TREE_CODE (t1) == BOOLEAN_TYPE)
956 return t2;
957 if (TREE_CODE (t2) == BOOLEAN_TYPE)
958 return t1;
960 return c_common_type (t1, t2);
963 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
964 or various other operations. Return 2 if they are compatible
965 but a warning may be needed if you use them together. */
968 comptypes (tree type1, tree type2)
970 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
971 int val;
973 val = comptypes_internal (type1, type2, NULL, NULL);
974 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
976 return val;
979 /* Like comptypes, but if it returns non-zero because enum and int are
980 compatible, it sets *ENUM_AND_INT_P to true. */
982 static int
983 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
985 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
986 int val;
988 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
989 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
991 return val;
994 /* Like comptypes, but if it returns nonzero for different types, it
995 sets *DIFFERENT_TYPES_P to true. */
998 comptypes_check_different_types (tree type1, tree type2,
999 bool *different_types_p)
1001 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1002 int val;
1004 val = comptypes_internal (type1, type2, NULL, different_types_p);
1005 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1007 return val;
1010 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1011 or various other operations. Return 2 if they are compatible
1012 but a warning may be needed if you use them together. If
1013 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1014 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1015 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1016 NULL, and the types are compatible but different enough not to be
1017 permitted in C11 typedef redeclarations, then this sets
1018 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1019 false, but may or may not be set if the types are incompatible.
1020 This differs from comptypes, in that we don't free the seen
1021 types. */
1023 static int
1024 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1025 bool *different_types_p)
1027 const_tree t1 = type1;
1028 const_tree t2 = type2;
1029 int attrval, val;
1031 /* Suppress errors caused by previously reported errors. */
1033 if (t1 == t2 || !t1 || !t2
1034 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1035 return 1;
1037 /* Enumerated types are compatible with integer types, but this is
1038 not transitive: two enumerated types in the same translation unit
1039 are compatible with each other only if they are the same type. */
1041 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1043 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1044 if (TREE_CODE (t2) != VOID_TYPE)
1046 if (enum_and_int_p != NULL)
1047 *enum_and_int_p = true;
1048 if (different_types_p != NULL)
1049 *different_types_p = true;
1052 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1054 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1055 if (TREE_CODE (t1) != VOID_TYPE)
1057 if (enum_and_int_p != NULL)
1058 *enum_and_int_p = true;
1059 if (different_types_p != NULL)
1060 *different_types_p = true;
1064 if (t1 == t2)
1065 return 1;
1067 /* Different classes of types can't be compatible. */
1069 if (TREE_CODE (t1) != TREE_CODE (t2))
1070 return 0;
1072 /* Qualifiers must match. C99 6.7.3p9 */
1074 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1075 return 0;
1077 /* Allow for two different type nodes which have essentially the same
1078 definition. Note that we already checked for equality of the type
1079 qualifiers (just above). */
1081 if (TREE_CODE (t1) != ARRAY_TYPE
1082 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1083 return 1;
1085 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1086 if (!(attrval = comp_type_attributes (t1, t2)))
1087 return 0;
1089 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1090 val = 0;
1092 switch (TREE_CODE (t1))
1094 case POINTER_TYPE:
1095 /* Do not remove mode or aliasing information. */
1096 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1097 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1098 break;
1099 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1100 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1101 enum_and_int_p, different_types_p));
1102 break;
1104 case FUNCTION_TYPE:
1105 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1106 different_types_p);
1107 break;
1109 case ARRAY_TYPE:
1111 tree d1 = TYPE_DOMAIN (t1);
1112 tree d2 = TYPE_DOMAIN (t2);
1113 bool d1_variable, d2_variable;
1114 bool d1_zero, d2_zero;
1115 val = 1;
1117 /* Target types must match incl. qualifiers. */
1118 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1119 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1120 enum_and_int_p,
1121 different_types_p)))
1122 return 0;
1124 if (different_types_p != NULL
1125 && (d1 == 0) != (d2 == 0))
1126 *different_types_p = true;
1127 /* Sizes must match unless one is missing or variable. */
1128 if (d1 == 0 || d2 == 0 || d1 == d2)
1129 break;
1131 d1_zero = !TYPE_MAX_VALUE (d1);
1132 d2_zero = !TYPE_MAX_VALUE (d2);
1134 d1_variable = (!d1_zero
1135 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1136 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1137 d2_variable = (!d2_zero
1138 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1139 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1140 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1141 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1143 if (different_types_p != NULL
1144 && d1_variable != d2_variable)
1145 *different_types_p = true;
1146 if (d1_variable || d2_variable)
1147 break;
1148 if (d1_zero && d2_zero)
1149 break;
1150 if (d1_zero || d2_zero
1151 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1152 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1153 val = 0;
1155 break;
1158 case ENUMERAL_TYPE:
1159 case RECORD_TYPE:
1160 case UNION_TYPE:
1161 if (val != 1 && !same_translation_unit_p (t1, t2))
1163 tree a1 = TYPE_ATTRIBUTES (t1);
1164 tree a2 = TYPE_ATTRIBUTES (t2);
1166 if (! attribute_list_contained (a1, a2)
1167 && ! attribute_list_contained (a2, a1))
1168 break;
1170 if (attrval != 2)
1171 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1172 different_types_p);
1173 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1174 different_types_p);
1176 break;
1178 case VECTOR_TYPE:
1179 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1180 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1181 enum_and_int_p, different_types_p));
1182 break;
1184 default:
1185 break;
1187 return attrval == 2 && val == 1 ? 2 : val;
1190 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1191 their qualifiers, except for named address spaces. If the pointers point to
1192 different named addresses, then we must determine if one address space is a
1193 subset of the other. */
1195 static int
1196 comp_target_types (location_t location, tree ttl, tree ttr)
1198 int val;
1199 tree mvl = TREE_TYPE (ttl);
1200 tree mvr = TREE_TYPE (ttr);
1201 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1202 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1203 addr_space_t as_common;
1204 bool enum_and_int_p;
1206 /* Fail if pointers point to incompatible address spaces. */
1207 if (!addr_space_superset (asl, asr, &as_common))
1208 return 0;
1210 /* Do not lose qualifiers on element types of array types that are
1211 pointer targets by taking their TYPE_MAIN_VARIANT. */
1212 if (TREE_CODE (mvl) != ARRAY_TYPE)
1213 mvl = TYPE_MAIN_VARIANT (mvl);
1214 if (TREE_CODE (mvr) != ARRAY_TYPE)
1215 mvr = TYPE_MAIN_VARIANT (mvr);
1216 enum_and_int_p = false;
1217 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1219 if (val == 2)
1220 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1222 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1223 warning_at (location, OPT_Wc___compat,
1224 "pointer target types incompatible in C++");
1226 return val;
1229 /* Subroutines of `comptypes'. */
1231 /* Determine whether two trees derive from the same translation unit.
1232 If the CONTEXT chain ends in a null, that tree's context is still
1233 being parsed, so if two trees have context chains ending in null,
1234 they're in the same translation unit. */
1236 same_translation_unit_p (const_tree t1, const_tree t2)
1238 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1239 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1241 case tcc_declaration:
1242 t1 = DECL_CONTEXT (t1); break;
1243 case tcc_type:
1244 t1 = TYPE_CONTEXT (t1); break;
1245 case tcc_exceptional:
1246 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1247 default: gcc_unreachable ();
1250 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1251 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1253 case tcc_declaration:
1254 t2 = DECL_CONTEXT (t2); break;
1255 case tcc_type:
1256 t2 = TYPE_CONTEXT (t2); break;
1257 case tcc_exceptional:
1258 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1259 default: gcc_unreachable ();
1262 return t1 == t2;
1265 /* Allocate the seen two types, assuming that they are compatible. */
1267 static struct tagged_tu_seen_cache *
1268 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1270 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1271 tu->next = tagged_tu_seen_base;
1272 tu->t1 = t1;
1273 tu->t2 = t2;
1275 tagged_tu_seen_base = tu;
1277 /* The C standard says that two structures in different translation
1278 units are compatible with each other only if the types of their
1279 fields are compatible (among other things). We assume that they
1280 are compatible until proven otherwise when building the cache.
1281 An example where this can occur is:
1282 struct a
1284 struct a *next;
1286 If we are comparing this against a similar struct in another TU,
1287 and did not assume they were compatible, we end up with an infinite
1288 loop. */
1289 tu->val = 1;
1290 return tu;
1293 /* Free the seen types until we get to TU_TIL. */
1295 static void
1296 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1298 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1299 while (tu != tu_til)
1301 const struct tagged_tu_seen_cache *const tu1
1302 = (const struct tagged_tu_seen_cache *) tu;
1303 tu = tu1->next;
1304 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1306 tagged_tu_seen_base = tu_til;
1309 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1310 compatible. If the two types are not the same (which has been
1311 checked earlier), this can only happen when multiple translation
1312 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1313 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1314 comptypes_internal. */
1316 static int
1317 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1318 bool *enum_and_int_p, bool *different_types_p)
1320 tree s1, s2;
1321 bool needs_warning = false;
1323 /* We have to verify that the tags of the types are the same. This
1324 is harder than it looks because this may be a typedef, so we have
1325 to go look at the original type. It may even be a typedef of a
1326 typedef...
1327 In the case of compiler-created builtin structs the TYPE_DECL
1328 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1329 while (TYPE_NAME (t1)
1330 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1331 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1332 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1334 while (TYPE_NAME (t2)
1335 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1336 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1337 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1339 /* C90 didn't have the requirement that the two tags be the same. */
1340 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1341 return 0;
1343 /* C90 didn't say what happened if one or both of the types were
1344 incomplete; we choose to follow C99 rules here, which is that they
1345 are compatible. */
1346 if (TYPE_SIZE (t1) == NULL
1347 || TYPE_SIZE (t2) == NULL)
1348 return 1;
1351 const struct tagged_tu_seen_cache * tts_i;
1352 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1353 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1354 return tts_i->val;
1357 switch (TREE_CODE (t1))
1359 case ENUMERAL_TYPE:
1361 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1362 /* Speed up the case where the type values are in the same order. */
1363 tree tv1 = TYPE_VALUES (t1);
1364 tree tv2 = TYPE_VALUES (t2);
1366 if (tv1 == tv2)
1368 return 1;
1371 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1373 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1374 break;
1375 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1377 tu->val = 0;
1378 return 0;
1382 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1384 return 1;
1386 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1388 tu->val = 0;
1389 return 0;
1392 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1394 tu->val = 0;
1395 return 0;
1398 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1400 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1401 if (s2 == NULL
1402 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1404 tu->val = 0;
1405 return 0;
1408 return 1;
1411 case UNION_TYPE:
1413 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1414 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1416 tu->val = 0;
1417 return 0;
1420 /* Speed up the common case where the fields are in the same order. */
1421 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1422 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1424 int result;
1426 if (DECL_NAME (s1) != DECL_NAME (s2))
1427 break;
1428 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1429 enum_and_int_p, different_types_p);
1431 if (result != 1 && !DECL_NAME (s1))
1432 break;
1433 if (result == 0)
1435 tu->val = 0;
1436 return 0;
1438 if (result == 2)
1439 needs_warning = true;
1441 if (TREE_CODE (s1) == FIELD_DECL
1442 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1443 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1445 tu->val = 0;
1446 return 0;
1449 if (!s1 && !s2)
1451 tu->val = needs_warning ? 2 : 1;
1452 return tu->val;
1455 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1457 bool ok = false;
1459 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1460 if (DECL_NAME (s1) == DECL_NAME (s2))
1462 int result;
1464 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1465 enum_and_int_p,
1466 different_types_p);
1468 if (result != 1 && !DECL_NAME (s1))
1469 continue;
1470 if (result == 0)
1472 tu->val = 0;
1473 return 0;
1475 if (result == 2)
1476 needs_warning = true;
1478 if (TREE_CODE (s1) == FIELD_DECL
1479 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1480 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1481 break;
1483 ok = true;
1484 break;
1486 if (!ok)
1488 tu->val = 0;
1489 return 0;
1492 tu->val = needs_warning ? 2 : 10;
1493 return tu->val;
1496 case RECORD_TYPE:
1498 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1500 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1501 s1 && s2;
1502 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1504 int result;
1505 if (TREE_CODE (s1) != TREE_CODE (s2)
1506 || DECL_NAME (s1) != DECL_NAME (s2))
1507 break;
1508 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1509 enum_and_int_p, different_types_p);
1510 if (result == 0)
1511 break;
1512 if (result == 2)
1513 needs_warning = true;
1515 if (TREE_CODE (s1) == FIELD_DECL
1516 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1517 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1518 break;
1520 if (s1 && s2)
1521 tu->val = 0;
1522 else
1523 tu->val = needs_warning ? 2 : 1;
1524 return tu->val;
1527 default:
1528 gcc_unreachable ();
1532 /* Return 1 if two function types F1 and F2 are compatible.
1533 If either type specifies no argument types,
1534 the other must specify a fixed number of self-promoting arg types.
1535 Otherwise, if one type specifies only the number of arguments,
1536 the other must specify that number of self-promoting arg types.
1537 Otherwise, the argument types must match.
1538 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1540 static int
1541 function_types_compatible_p (const_tree f1, const_tree f2,
1542 bool *enum_and_int_p, bool *different_types_p)
1544 tree args1, args2;
1545 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1546 int val = 1;
1547 int val1;
1548 tree ret1, ret2;
1550 ret1 = TREE_TYPE (f1);
1551 ret2 = TREE_TYPE (f2);
1553 /* 'volatile' qualifiers on a function's return type used to mean
1554 the function is noreturn. */
1555 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1556 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1557 if (TYPE_VOLATILE (ret1))
1558 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1559 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1560 if (TYPE_VOLATILE (ret2))
1561 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1562 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1563 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1564 if (val == 0)
1565 return 0;
1567 args1 = TYPE_ARG_TYPES (f1);
1568 args2 = TYPE_ARG_TYPES (f2);
1570 if (different_types_p != NULL
1571 && (args1 == 0) != (args2 == 0))
1572 *different_types_p = true;
1574 /* An unspecified parmlist matches any specified parmlist
1575 whose argument types don't need default promotions. */
1577 if (args1 == 0)
1579 if (!self_promoting_args_p (args2))
1580 return 0;
1581 /* If one of these types comes from a non-prototype fn definition,
1582 compare that with the other type's arglist.
1583 If they don't match, ask for a warning (but no error). */
1584 if (TYPE_ACTUAL_ARG_TYPES (f1)
1585 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1586 enum_and_int_p, different_types_p))
1587 val = 2;
1588 return val;
1590 if (args2 == 0)
1592 if (!self_promoting_args_p (args1))
1593 return 0;
1594 if (TYPE_ACTUAL_ARG_TYPES (f2)
1595 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1596 enum_and_int_p, different_types_p))
1597 val = 2;
1598 return val;
1601 /* Both types have argument lists: compare them and propagate results. */
1602 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1603 different_types_p);
1604 return val1 != 1 ? val1 : val;
1607 /* Check two lists of types for compatibility, returning 0 for
1608 incompatible, 1 for compatible, or 2 for compatible with
1609 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1610 comptypes_internal. */
1612 static int
1613 type_lists_compatible_p (const_tree args1, const_tree args2,
1614 bool *enum_and_int_p, bool *different_types_p)
1616 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1617 int val = 1;
1618 int newval = 0;
1620 while (1)
1622 tree a1, mv1, a2, mv2;
1623 if (args1 == 0 && args2 == 0)
1624 return val;
1625 /* If one list is shorter than the other,
1626 they fail to match. */
1627 if (args1 == 0 || args2 == 0)
1628 return 0;
1629 mv1 = a1 = TREE_VALUE (args1);
1630 mv2 = a2 = TREE_VALUE (args2);
1631 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1632 mv1 = TYPE_MAIN_VARIANT (mv1);
1633 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1634 mv2 = TYPE_MAIN_VARIANT (mv2);
1635 /* A null pointer instead of a type
1636 means there is supposed to be an argument
1637 but nothing is specified about what type it has.
1638 So match anything that self-promotes. */
1639 if (different_types_p != NULL
1640 && (a1 == 0) != (a2 == 0))
1641 *different_types_p = true;
1642 if (a1 == 0)
1644 if (c_type_promotes_to (a2) != a2)
1645 return 0;
1647 else if (a2 == 0)
1649 if (c_type_promotes_to (a1) != a1)
1650 return 0;
1652 /* If one of the lists has an error marker, ignore this arg. */
1653 else if (TREE_CODE (a1) == ERROR_MARK
1654 || TREE_CODE (a2) == ERROR_MARK)
1656 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1657 different_types_p)))
1659 if (different_types_p != NULL)
1660 *different_types_p = true;
1661 /* Allow wait (union {union wait *u; int *i} *)
1662 and wait (union wait *) to be compatible. */
1663 if (TREE_CODE (a1) == UNION_TYPE
1664 && (TYPE_NAME (a1) == 0
1665 || TYPE_TRANSPARENT_AGGR (a1))
1666 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1667 && tree_int_cst_equal (TYPE_SIZE (a1),
1668 TYPE_SIZE (a2)))
1670 tree memb;
1671 for (memb = TYPE_FIELDS (a1);
1672 memb; memb = DECL_CHAIN (memb))
1674 tree mv3 = TREE_TYPE (memb);
1675 if (mv3 && mv3 != error_mark_node
1676 && TREE_CODE (mv3) != ARRAY_TYPE)
1677 mv3 = TYPE_MAIN_VARIANT (mv3);
1678 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1679 different_types_p))
1680 break;
1682 if (memb == 0)
1683 return 0;
1685 else if (TREE_CODE (a2) == UNION_TYPE
1686 && (TYPE_NAME (a2) == 0
1687 || TYPE_TRANSPARENT_AGGR (a2))
1688 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1689 && tree_int_cst_equal (TYPE_SIZE (a2),
1690 TYPE_SIZE (a1)))
1692 tree memb;
1693 for (memb = TYPE_FIELDS (a2);
1694 memb; memb = DECL_CHAIN (memb))
1696 tree mv3 = TREE_TYPE (memb);
1697 if (mv3 && mv3 != error_mark_node
1698 && TREE_CODE (mv3) != ARRAY_TYPE)
1699 mv3 = TYPE_MAIN_VARIANT (mv3);
1700 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1701 different_types_p))
1702 break;
1704 if (memb == 0)
1705 return 0;
1707 else
1708 return 0;
1711 /* comptypes said ok, but record if it said to warn. */
1712 if (newval > val)
1713 val = newval;
1715 args1 = TREE_CHAIN (args1);
1716 args2 = TREE_CHAIN (args2);
1720 /* Compute the size to increment a pointer by. */
1722 static tree
1723 c_size_in_bytes (const_tree type)
1725 enum tree_code code = TREE_CODE (type);
1727 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1728 return size_one_node;
1730 if (!COMPLETE_OR_VOID_TYPE_P (type))
1732 error ("arithmetic on pointer to an incomplete type");
1733 return size_one_node;
1736 /* Convert in case a char is more than one unit. */
1737 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1738 size_int (TYPE_PRECISION (char_type_node)
1739 / BITS_PER_UNIT));
1742 /* Return either DECL or its known constant value (if it has one). */
1744 tree
1745 decl_constant_value (tree decl)
1747 if (/* Don't change a variable array bound or initial value to a constant
1748 in a place where a variable is invalid. Note that DECL_INITIAL
1749 isn't valid for a PARM_DECL. */
1750 current_function_decl != 0
1751 && TREE_CODE (decl) != PARM_DECL
1752 && !TREE_THIS_VOLATILE (decl)
1753 && TREE_READONLY (decl)
1754 && DECL_INITIAL (decl) != 0
1755 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1756 /* This is invalid if initial value is not constant.
1757 If it has either a function call, a memory reference,
1758 or a variable, then re-evaluating it could give different results. */
1759 && TREE_CONSTANT (DECL_INITIAL (decl))
1760 /* Check for cases where this is sub-optimal, even though valid. */
1761 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1762 return DECL_INITIAL (decl);
1763 return decl;
1766 /* Convert the array expression EXP to a pointer. */
1767 static tree
1768 array_to_pointer_conversion (location_t loc, tree exp)
1770 tree orig_exp = exp;
1771 tree type = TREE_TYPE (exp);
1772 tree adr;
1773 tree restype = TREE_TYPE (type);
1774 tree ptrtype;
1776 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1778 STRIP_TYPE_NOPS (exp);
1780 if (TREE_NO_WARNING (orig_exp))
1781 TREE_NO_WARNING (exp) = 1;
1783 ptrtype = build_pointer_type (restype);
1785 if (TREE_CODE (exp) == INDIRECT_REF)
1786 return convert (ptrtype, TREE_OPERAND (exp, 0));
1788 /* In C++ array compound literals are temporary objects unless they are
1789 const or appear in namespace scope, so they are destroyed too soon
1790 to use them for much of anything (c++/53220). */
1791 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1793 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1794 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1795 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1796 "converting an array compound literal to a pointer "
1797 "is ill-formed in C++");
1800 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1801 return convert (ptrtype, adr);
1804 /* Convert the function expression EXP to a pointer. */
1805 static tree
1806 function_to_pointer_conversion (location_t loc, tree exp)
1808 tree orig_exp = exp;
1810 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1812 STRIP_TYPE_NOPS (exp);
1814 if (TREE_NO_WARNING (orig_exp))
1815 TREE_NO_WARNING (exp) = 1;
1817 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1820 /* Mark EXP as read, not just set, for set but not used -Wunused
1821 warning purposes. */
1823 void
1824 mark_exp_read (tree exp)
1826 switch (TREE_CODE (exp))
1828 case VAR_DECL:
1829 case PARM_DECL:
1830 DECL_READ_P (exp) = 1;
1831 break;
1832 case ARRAY_REF:
1833 case COMPONENT_REF:
1834 case MODIFY_EXPR:
1835 case REALPART_EXPR:
1836 case IMAGPART_EXPR:
1837 CASE_CONVERT:
1838 case ADDR_EXPR:
1839 mark_exp_read (TREE_OPERAND (exp, 0));
1840 break;
1841 case COMPOUND_EXPR:
1842 case C_MAYBE_CONST_EXPR:
1843 mark_exp_read (TREE_OPERAND (exp, 1));
1844 break;
1845 default:
1846 break;
1850 /* Perform the default conversion of arrays and functions to pointers.
1851 Return the result of converting EXP. For any other expression, just
1852 return EXP.
1854 LOC is the location of the expression. */
1856 struct c_expr
1857 default_function_array_conversion (location_t loc, struct c_expr exp)
1859 tree orig_exp = exp.value;
1860 tree type = TREE_TYPE (exp.value);
1861 enum tree_code code = TREE_CODE (type);
1863 switch (code)
1865 case ARRAY_TYPE:
1867 bool not_lvalue = false;
1868 bool lvalue_array_p;
1870 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1871 || CONVERT_EXPR_P (exp.value))
1872 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1874 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1875 not_lvalue = true;
1876 exp.value = TREE_OPERAND (exp.value, 0);
1879 if (TREE_NO_WARNING (orig_exp))
1880 TREE_NO_WARNING (exp.value) = 1;
1882 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1883 if (!flag_isoc99 && !lvalue_array_p)
1885 /* Before C99, non-lvalue arrays do not decay to pointers.
1886 Normally, using such an array would be invalid; but it can
1887 be used correctly inside sizeof or as a statement expression.
1888 Thus, do not give an error here; an error will result later. */
1889 return exp;
1892 exp.value = array_to_pointer_conversion (loc, exp.value);
1894 break;
1895 case FUNCTION_TYPE:
1896 exp.value = function_to_pointer_conversion (loc, exp.value);
1897 break;
1898 default:
1899 break;
1902 return exp;
1905 struct c_expr
1906 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1908 mark_exp_read (exp.value);
1909 return default_function_array_conversion (loc, exp);
1912 /* EXP is an expression of integer type. Apply the integer promotions
1913 to it and return the promoted value. */
1915 tree
1916 perform_integral_promotions (tree exp)
1918 tree type = TREE_TYPE (exp);
1919 enum tree_code code = TREE_CODE (type);
1921 gcc_assert (INTEGRAL_TYPE_P (type));
1923 /* Normally convert enums to int,
1924 but convert wide enums to something wider. */
1925 if (code == ENUMERAL_TYPE)
1927 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1928 TYPE_PRECISION (integer_type_node)),
1929 ((TYPE_PRECISION (type)
1930 >= TYPE_PRECISION (integer_type_node))
1931 && TYPE_UNSIGNED (type)));
1933 return convert (type, exp);
1936 /* ??? This should no longer be needed now bit-fields have their
1937 proper types. */
1938 if (TREE_CODE (exp) == COMPONENT_REF
1939 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1940 /* If it's thinner than an int, promote it like a
1941 c_promoting_integer_type_p, otherwise leave it alone. */
1942 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1943 TYPE_PRECISION (integer_type_node)))
1944 return convert (integer_type_node, exp);
1946 if (c_promoting_integer_type_p (type))
1948 /* Preserve unsignedness if not really getting any wider. */
1949 if (TYPE_UNSIGNED (type)
1950 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1951 return convert (unsigned_type_node, exp);
1953 return convert (integer_type_node, exp);
1956 return exp;
1960 /* Perform default promotions for C data used in expressions.
1961 Enumeral types or short or char are converted to int.
1962 In addition, manifest constants symbols are replaced by their values. */
1964 tree
1965 default_conversion (tree exp)
1967 tree orig_exp;
1968 tree type = TREE_TYPE (exp);
1969 enum tree_code code = TREE_CODE (type);
1970 tree promoted_type;
1972 mark_exp_read (exp);
1974 /* Functions and arrays have been converted during parsing. */
1975 gcc_assert (code != FUNCTION_TYPE);
1976 if (code == ARRAY_TYPE)
1977 return exp;
1979 /* Constants can be used directly unless they're not loadable. */
1980 if (TREE_CODE (exp) == CONST_DECL)
1981 exp = DECL_INITIAL (exp);
1983 /* Strip no-op conversions. */
1984 orig_exp = exp;
1985 STRIP_TYPE_NOPS (exp);
1987 if (TREE_NO_WARNING (orig_exp))
1988 TREE_NO_WARNING (exp) = 1;
1990 if (code == VOID_TYPE)
1992 error ("void value not ignored as it ought to be");
1993 return error_mark_node;
1996 exp = require_complete_type (exp);
1997 if (exp == error_mark_node)
1998 return error_mark_node;
2000 promoted_type = targetm.promoted_type (type);
2001 if (promoted_type)
2002 return convert (promoted_type, exp);
2004 if (INTEGRAL_TYPE_P (type))
2005 return perform_integral_promotions (exp);
2007 return exp;
2010 /* Look up COMPONENT in a structure or union TYPE.
2012 If the component name is not found, returns NULL_TREE. Otherwise,
2013 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2014 stepping down the chain to the component, which is in the last
2015 TREE_VALUE of the list. Normally the list is of length one, but if
2016 the component is embedded within (nested) anonymous structures or
2017 unions, the list steps down the chain to the component. */
2019 static tree
2020 lookup_field (tree type, tree component)
2022 tree field;
2024 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2025 to the field elements. Use a binary search on this array to quickly
2026 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2027 will always be set for structures which have many elements. */
2029 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2031 int bot, top, half;
2032 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2034 field = TYPE_FIELDS (type);
2035 bot = 0;
2036 top = TYPE_LANG_SPECIFIC (type)->s->len;
2037 while (top - bot > 1)
2039 half = (top - bot + 1) >> 1;
2040 field = field_array[bot+half];
2042 if (DECL_NAME (field) == NULL_TREE)
2044 /* Step through all anon unions in linear fashion. */
2045 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2047 field = field_array[bot++];
2048 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2049 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2051 tree anon = lookup_field (TREE_TYPE (field), component);
2053 if (anon)
2054 return tree_cons (NULL_TREE, field, anon);
2056 /* The Plan 9 compiler permits referring
2057 directly to an anonymous struct/union field
2058 using a typedef name. */
2059 if (flag_plan9_extensions
2060 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2061 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2062 == TYPE_DECL)
2063 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2064 == component))
2065 break;
2069 /* Entire record is only anon unions. */
2070 if (bot > top)
2071 return NULL_TREE;
2073 /* Restart the binary search, with new lower bound. */
2074 continue;
2077 if (DECL_NAME (field) == component)
2078 break;
2079 if (DECL_NAME (field) < component)
2080 bot += half;
2081 else
2082 top = bot + half;
2085 if (DECL_NAME (field_array[bot]) == component)
2086 field = field_array[bot];
2087 else if (DECL_NAME (field) != component)
2088 return NULL_TREE;
2090 else
2092 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2094 if (DECL_NAME (field) == NULL_TREE
2095 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2096 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2098 tree anon = lookup_field (TREE_TYPE (field), component);
2100 if (anon)
2101 return tree_cons (NULL_TREE, field, anon);
2103 /* The Plan 9 compiler permits referring directly to an
2104 anonymous struct/union field using a typedef
2105 name. */
2106 if (flag_plan9_extensions
2107 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2108 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2109 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2110 == component))
2111 break;
2114 if (DECL_NAME (field) == component)
2115 break;
2118 if (field == NULL_TREE)
2119 return NULL_TREE;
2122 return tree_cons (NULL_TREE, field, NULL_TREE);
2125 /* Make an expression to refer to the COMPONENT field of structure or
2126 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2127 location of the COMPONENT_REF. */
2129 tree
2130 build_component_ref (location_t loc, tree datum, tree component)
2132 tree type = TREE_TYPE (datum);
2133 enum tree_code code = TREE_CODE (type);
2134 tree field = NULL;
2135 tree ref;
2136 bool datum_lvalue = lvalue_p (datum);
2138 if (!objc_is_public (datum, component))
2139 return error_mark_node;
2141 /* Detect Objective-C property syntax object.property. */
2142 if (c_dialect_objc ()
2143 && (ref = objc_maybe_build_component_ref (datum, component)))
2144 return ref;
2146 /* See if there is a field or component with name COMPONENT. */
2148 if (code == RECORD_TYPE || code == UNION_TYPE)
2150 if (!COMPLETE_TYPE_P (type))
2152 c_incomplete_type_error (NULL_TREE, type);
2153 return error_mark_node;
2156 field = lookup_field (type, component);
2158 if (!field)
2160 error_at (loc, "%qT has no member named %qE", type, component);
2161 return error_mark_node;
2164 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2165 This might be better solved in future the way the C++ front
2166 end does it - by giving the anonymous entities each a
2167 separate name and type, and then have build_component_ref
2168 recursively call itself. We can't do that here. */
2171 tree subdatum = TREE_VALUE (field);
2172 int quals;
2173 tree subtype;
2174 bool use_datum_quals;
2176 if (TREE_TYPE (subdatum) == error_mark_node)
2177 return error_mark_node;
2179 /* If this is an rvalue, it does not have qualifiers in C
2180 standard terms and we must avoid propagating such
2181 qualifiers down to a non-lvalue array that is then
2182 converted to a pointer. */
2183 use_datum_quals = (datum_lvalue
2184 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2186 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2187 if (use_datum_quals)
2188 quals |= TYPE_QUALS (TREE_TYPE (datum));
2189 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2191 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2192 NULL_TREE);
2193 SET_EXPR_LOCATION (ref, loc);
2194 if (TREE_READONLY (subdatum)
2195 || (use_datum_quals && TREE_READONLY (datum)))
2196 TREE_READONLY (ref) = 1;
2197 if (TREE_THIS_VOLATILE (subdatum)
2198 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2199 TREE_THIS_VOLATILE (ref) = 1;
2201 if (TREE_DEPRECATED (subdatum))
2202 warn_deprecated_use (subdatum, NULL_TREE);
2204 datum = ref;
2206 field = TREE_CHAIN (field);
2208 while (field);
2210 return ref;
2212 else if (code != ERROR_MARK)
2213 error_at (loc,
2214 "request for member %qE in something not a structure or union",
2215 component);
2217 return error_mark_node;
2220 /* Given an expression PTR for a pointer, return an expression
2221 for the value pointed to.
2222 ERRORSTRING is the name of the operator to appear in error messages.
2224 LOC is the location to use for the generated tree. */
2226 tree
2227 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2229 tree pointer = default_conversion (ptr);
2230 tree type = TREE_TYPE (pointer);
2231 tree ref;
2233 if (TREE_CODE (type) == POINTER_TYPE)
2235 if (CONVERT_EXPR_P (pointer)
2236 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2238 /* If a warning is issued, mark it to avoid duplicates from
2239 the backend. This only needs to be done at
2240 warn_strict_aliasing > 2. */
2241 if (warn_strict_aliasing > 2)
2242 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2243 type, TREE_OPERAND (pointer, 0)))
2244 TREE_NO_WARNING (pointer) = 1;
2247 if (TREE_CODE (pointer) == ADDR_EXPR
2248 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2249 == TREE_TYPE (type)))
2251 ref = TREE_OPERAND (pointer, 0);
2252 protected_set_expr_location (ref, loc);
2253 return ref;
2255 else
2257 tree t = TREE_TYPE (type);
2259 ref = build1 (INDIRECT_REF, t, pointer);
2261 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2263 error_at (loc, "dereferencing pointer to incomplete type");
2264 return error_mark_node;
2266 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2267 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2269 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2270 so that we get the proper error message if the result is used
2271 to assign to. Also, &* is supposed to be a no-op.
2272 And ANSI C seems to specify that the type of the result
2273 should be the const type. */
2274 /* A de-reference of a pointer to const is not a const. It is valid
2275 to change it via some other pointer. */
2276 TREE_READONLY (ref) = TYPE_READONLY (t);
2277 TREE_SIDE_EFFECTS (ref)
2278 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2279 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2280 protected_set_expr_location (ref, loc);
2281 return ref;
2284 else if (TREE_CODE (pointer) != ERROR_MARK)
2285 invalid_indirection_error (loc, type, errstring);
2287 return error_mark_node;
2290 /* This handles expressions of the form "a[i]", which denotes
2291 an array reference.
2293 This is logically equivalent in C to *(a+i), but we may do it differently.
2294 If A is a variable or a member, we generate a primitive ARRAY_REF.
2295 This avoids forcing the array out of registers, and can work on
2296 arrays that are not lvalues (for example, members of structures returned
2297 by functions).
2299 For vector types, allow vector[i] but not i[vector], and create
2300 *(((type*)&vectortype) + i) for the expression.
2302 LOC is the location to use for the returned expression. */
2304 tree
2305 build_array_ref (location_t loc, tree array, tree index)
2307 tree ret;
2308 bool swapped = false;
2309 if (TREE_TYPE (array) == error_mark_node
2310 || TREE_TYPE (index) == error_mark_node)
2311 return error_mark_node;
2313 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2314 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2315 /* Allow vector[index] but not index[vector]. */
2316 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2318 tree temp;
2319 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2320 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2322 error_at (loc,
2323 "subscripted value is neither array nor pointer nor vector");
2325 return error_mark_node;
2327 temp = array;
2328 array = index;
2329 index = temp;
2330 swapped = true;
2333 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2335 error_at (loc, "array subscript is not an integer");
2336 return error_mark_node;
2339 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2341 error_at (loc, "subscripted value is pointer to function");
2342 return error_mark_node;
2345 /* ??? Existing practice has been to warn only when the char
2346 index is syntactically the index, not for char[array]. */
2347 if (!swapped)
2348 warn_array_subscript_with_type_char (index);
2350 /* Apply default promotions *after* noticing character types. */
2351 index = default_conversion (index);
2353 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2355 /* For vector[index], convert the vector to a
2356 pointer of the underlying type. */
2357 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2359 tree type = TREE_TYPE (array);
2360 tree type1;
2362 if (TREE_CODE (index) == INTEGER_CST)
2363 if (!host_integerp (index, 1)
2364 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
2365 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2366 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
2368 c_common_mark_addressable_vec (array);
2369 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2370 type = build_pointer_type (type);
2371 type1 = build_pointer_type (TREE_TYPE (array));
2372 array = build1 (ADDR_EXPR, type1, array);
2373 array = convert (type, array);
2376 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2378 tree rval, type;
2380 /* An array that is indexed by a non-constant
2381 cannot be stored in a register; we must be able to do
2382 address arithmetic on its address.
2383 Likewise an array of elements of variable size. */
2384 if (TREE_CODE (index) != INTEGER_CST
2385 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2386 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2388 if (!c_mark_addressable (array))
2389 return error_mark_node;
2391 /* An array that is indexed by a constant value which is not within
2392 the array bounds cannot be stored in a register either; because we
2393 would get a crash in store_bit_field/extract_bit_field when trying
2394 to access a non-existent part of the register. */
2395 if (TREE_CODE (index) == INTEGER_CST
2396 && TYPE_DOMAIN (TREE_TYPE (array))
2397 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2399 if (!c_mark_addressable (array))
2400 return error_mark_node;
2403 if (pedantic)
2405 tree foo = array;
2406 while (TREE_CODE (foo) == COMPONENT_REF)
2407 foo = TREE_OPERAND (foo, 0);
2408 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2409 pedwarn (loc, OPT_pedantic,
2410 "ISO C forbids subscripting %<register%> array");
2411 else if (!flag_isoc99 && !lvalue_p (foo))
2412 pedwarn (loc, OPT_pedantic,
2413 "ISO C90 forbids subscripting non-lvalue array");
2416 type = TREE_TYPE (TREE_TYPE (array));
2417 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2418 /* Array ref is const/volatile if the array elements are
2419 or if the array is. */
2420 TREE_READONLY (rval)
2421 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2422 | TREE_READONLY (array));
2423 TREE_SIDE_EFFECTS (rval)
2424 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2425 | TREE_SIDE_EFFECTS (array));
2426 TREE_THIS_VOLATILE (rval)
2427 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2428 /* This was added by rms on 16 Nov 91.
2429 It fixes vol struct foo *a; a->elts[1]
2430 in an inline function.
2431 Hope it doesn't break something else. */
2432 | TREE_THIS_VOLATILE (array));
2433 ret = require_complete_type (rval);
2434 protected_set_expr_location (ret, loc);
2435 return ret;
2437 else
2439 tree ar = default_conversion (array);
2441 if (ar == error_mark_node)
2442 return ar;
2444 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2445 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2447 return build_indirect_ref
2448 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2449 RO_ARRAY_INDEXING);
2453 /* Build an external reference to identifier ID. FUN indicates
2454 whether this will be used for a function call. LOC is the source
2455 location of the identifier. This sets *TYPE to the type of the
2456 identifier, which is not the same as the type of the returned value
2457 for CONST_DECLs defined as enum constants. If the type of the
2458 identifier is not available, *TYPE is set to NULL. */
2459 tree
2460 build_external_ref (location_t loc, tree id, int fun, tree *type)
2462 tree ref;
2463 tree decl = lookup_name (id);
2465 /* In Objective-C, an instance variable (ivar) may be preferred to
2466 whatever lookup_name() found. */
2467 decl = objc_lookup_ivar (decl, id);
2469 *type = NULL;
2470 if (decl && decl != error_mark_node)
2472 ref = decl;
2473 *type = TREE_TYPE (ref);
2475 else if (fun)
2476 /* Implicit function declaration. */
2477 ref = implicitly_declare (loc, id);
2478 else if (decl == error_mark_node)
2479 /* Don't complain about something that's already been
2480 complained about. */
2481 return error_mark_node;
2482 else
2484 undeclared_variable (loc, id);
2485 return error_mark_node;
2488 if (TREE_TYPE (ref) == error_mark_node)
2489 return error_mark_node;
2491 if (TREE_DEPRECATED (ref))
2492 warn_deprecated_use (ref, NULL_TREE);
2494 /* Recursive call does not count as usage. */
2495 if (ref != current_function_decl)
2497 TREE_USED (ref) = 1;
2500 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2502 if (!in_sizeof && !in_typeof)
2503 C_DECL_USED (ref) = 1;
2504 else if (DECL_INITIAL (ref) == 0
2505 && DECL_EXTERNAL (ref)
2506 && !TREE_PUBLIC (ref))
2507 record_maybe_used_decl (ref);
2510 if (TREE_CODE (ref) == CONST_DECL)
2512 used_types_insert (TREE_TYPE (ref));
2514 if (warn_cxx_compat
2515 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2516 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2518 warning_at (loc, OPT_Wc___compat,
2519 ("enum constant defined in struct or union "
2520 "is not visible in C++"));
2521 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2524 ref = DECL_INITIAL (ref);
2525 TREE_CONSTANT (ref) = 1;
2527 else if (current_function_decl != 0
2528 && !DECL_FILE_SCOPE_P (current_function_decl)
2529 && (TREE_CODE (ref) == VAR_DECL
2530 || TREE_CODE (ref) == PARM_DECL
2531 || TREE_CODE (ref) == FUNCTION_DECL))
2533 tree context = decl_function_context (ref);
2535 if (context != 0 && context != current_function_decl)
2536 DECL_NONLOCAL (ref) = 1;
2538 /* C99 6.7.4p3: An inline definition of a function with external
2539 linkage ... shall not contain a reference to an identifier with
2540 internal linkage. */
2541 else if (current_function_decl != 0
2542 && DECL_DECLARED_INLINE_P (current_function_decl)
2543 && DECL_EXTERNAL (current_function_decl)
2544 && VAR_OR_FUNCTION_DECL_P (ref)
2545 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2546 && ! TREE_PUBLIC (ref)
2547 && DECL_CONTEXT (ref) != current_function_decl)
2548 record_inline_static (loc, current_function_decl, ref,
2549 csi_internal);
2551 return ref;
2554 /* Record details of decls possibly used inside sizeof or typeof. */
2555 struct maybe_used_decl
2557 /* The decl. */
2558 tree decl;
2559 /* The level seen at (in_sizeof + in_typeof). */
2560 int level;
2561 /* The next one at this level or above, or NULL. */
2562 struct maybe_used_decl *next;
2565 static struct maybe_used_decl *maybe_used_decls;
2567 /* Record that DECL, an undefined static function reference seen
2568 inside sizeof or typeof, might be used if the operand of sizeof is
2569 a VLA type or the operand of typeof is a variably modified
2570 type. */
2572 static void
2573 record_maybe_used_decl (tree decl)
2575 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2576 t->decl = decl;
2577 t->level = in_sizeof + in_typeof;
2578 t->next = maybe_used_decls;
2579 maybe_used_decls = t;
2582 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2583 USED is false, just discard them. If it is true, mark them used
2584 (if no longer inside sizeof or typeof) or move them to the next
2585 level up (if still inside sizeof or typeof). */
2587 void
2588 pop_maybe_used (bool used)
2590 struct maybe_used_decl *p = maybe_used_decls;
2591 int cur_level = in_sizeof + in_typeof;
2592 while (p && p->level > cur_level)
2594 if (used)
2596 if (cur_level == 0)
2597 C_DECL_USED (p->decl) = 1;
2598 else
2599 p->level = cur_level;
2601 p = p->next;
2603 if (!used || cur_level == 0)
2604 maybe_used_decls = p;
2607 /* Return the result of sizeof applied to EXPR. */
2609 struct c_expr
2610 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2612 struct c_expr ret;
2613 if (expr.value == error_mark_node)
2615 ret.value = error_mark_node;
2616 ret.original_code = ERROR_MARK;
2617 ret.original_type = NULL;
2618 pop_maybe_used (false);
2620 else
2622 bool expr_const_operands = true;
2623 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2624 &expr_const_operands);
2625 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2626 ret.original_code = ERROR_MARK;
2627 ret.original_type = NULL;
2628 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2630 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2631 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2632 folded_expr, ret.value);
2633 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2634 SET_EXPR_LOCATION (ret.value, loc);
2636 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2638 return ret;
2641 /* Return the result of sizeof applied to T, a structure for the type
2642 name passed to sizeof (rather than the type itself). LOC is the
2643 location of the original expression. */
2645 struct c_expr
2646 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2648 tree type;
2649 struct c_expr ret;
2650 tree type_expr = NULL_TREE;
2651 bool type_expr_const = true;
2652 type = groktypename (t, &type_expr, &type_expr_const);
2653 ret.value = c_sizeof (loc, type);
2654 ret.original_code = ERROR_MARK;
2655 ret.original_type = NULL;
2656 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2657 && c_vla_type_p (type))
2659 /* If the type is a [*] array, it is a VLA but is represented as
2660 having a size of zero. In such a case we must ensure that
2661 the result of sizeof does not get folded to a constant by
2662 c_fully_fold, because if the size is evaluated the result is
2663 not constant and so constraints on zero or negative size
2664 arrays must not be applied when this sizeof call is inside
2665 another array declarator. */
2666 if (!type_expr)
2667 type_expr = integer_zero_node;
2668 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2669 type_expr, ret.value);
2670 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2672 pop_maybe_used (type != error_mark_node
2673 ? C_TYPE_VARIABLE_SIZE (type) : false);
2674 return ret;
2677 /* Build a function call to function FUNCTION with parameters PARAMS.
2678 The function call is at LOC.
2679 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2680 TREE_VALUE of each node is a parameter-expression.
2681 FUNCTION's data type may be a function type or a pointer-to-function. */
2683 tree
2684 build_function_call (location_t loc, tree function, tree params)
2686 VEC(tree,gc) *vec;
2687 tree ret;
2689 vec = VEC_alloc (tree, gc, list_length (params));
2690 for (; params; params = TREE_CHAIN (params))
2691 VEC_quick_push (tree, vec, TREE_VALUE (params));
2692 ret = build_function_call_vec (loc, function, vec, NULL);
2693 VEC_free (tree, gc, vec);
2694 return ret;
2697 /* Build a function call to function FUNCTION with parameters PARAMS.
2698 ORIGTYPES, if not NULL, is a vector of types; each element is
2699 either NULL or the original type of the corresponding element in
2700 PARAMS. The original type may differ from TREE_TYPE of the
2701 parameter for enums. FUNCTION's data type may be a function type
2702 or pointer-to-function. This function changes the elements of
2703 PARAMS. */
2705 tree
2706 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2707 VEC(tree,gc) *origtypes)
2709 tree fntype, fundecl = 0;
2710 tree name = NULL_TREE, result;
2711 tree tem;
2712 int nargs;
2713 tree *argarray;
2716 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2717 STRIP_TYPE_NOPS (function);
2719 /* Convert anything with function type to a pointer-to-function. */
2720 if (TREE_CODE (function) == FUNCTION_DECL)
2722 /* Implement type-directed function overloading for builtins.
2723 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2724 handle all the type checking. The result is a complete expression
2725 that implements this function call. */
2726 tem = resolve_overloaded_builtin (loc, function, params);
2727 if (tem)
2728 return tem;
2730 name = DECL_NAME (function);
2732 if (flag_tm)
2733 tm_malloc_replacement (function);
2734 fundecl = function;
2735 /* Atomic functions have type checking/casting already done. They are
2736 often rewritten and don't match the original parameter list. */
2737 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2738 origtypes = NULL;
2740 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2741 function = function_to_pointer_conversion (loc, function);
2743 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2744 expressions, like those used for ObjC messenger dispatches. */
2745 if (!VEC_empty (tree, params))
2746 function = objc_rewrite_function_call (function,
2747 VEC_index (tree, params, 0));
2749 function = c_fully_fold (function, false, NULL);
2751 fntype = TREE_TYPE (function);
2753 if (TREE_CODE (fntype) == ERROR_MARK)
2754 return error_mark_node;
2756 if (!(TREE_CODE (fntype) == POINTER_TYPE
2757 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2759 error_at (loc, "called object %qE is not a function", function);
2760 return error_mark_node;
2763 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2764 current_function_returns_abnormally = 1;
2766 /* fntype now gets the type of function pointed to. */
2767 fntype = TREE_TYPE (fntype);
2769 /* Convert the parameters to the types declared in the
2770 function prototype, or apply default promotions. */
2772 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2773 function, fundecl);
2774 if (nargs < 0)
2775 return error_mark_node;
2777 /* Check that the function is called through a compatible prototype.
2778 If it is not, replace the call by a trap, wrapped up in a compound
2779 expression if necessary. This has the nice side-effect to prevent
2780 the tree-inliner from generating invalid assignment trees which may
2781 blow up in the RTL expander later. */
2782 if (CONVERT_EXPR_P (function)
2783 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2784 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2785 && !comptypes (fntype, TREE_TYPE (tem)))
2787 tree return_type = TREE_TYPE (fntype);
2788 tree trap = build_function_call (loc,
2789 builtin_decl_explicit (BUILT_IN_TRAP),
2790 NULL_TREE);
2791 int i;
2793 /* This situation leads to run-time undefined behavior. We can't,
2794 therefore, simply error unless we can prove that all possible
2795 executions of the program must execute the code. */
2796 if (warning_at (loc, 0, "function called through a non-compatible type"))
2797 /* We can, however, treat "undefined" any way we please.
2798 Call abort to encourage the user to fix the program. */
2799 inform (loc, "if this code is reached, the program will abort");
2800 /* Before the abort, allow the function arguments to exit or
2801 call longjmp. */
2802 for (i = 0; i < nargs; i++)
2803 trap = build2 (COMPOUND_EXPR, void_type_node,
2804 VEC_index (tree, params, i), trap);
2806 if (VOID_TYPE_P (return_type))
2808 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2809 pedwarn (loc, 0,
2810 "function with qualified void return type called");
2811 return trap;
2813 else
2815 tree rhs;
2817 if (AGGREGATE_TYPE_P (return_type))
2818 rhs = build_compound_literal (loc, return_type,
2819 build_constructor (return_type, 0),
2820 false);
2821 else
2822 rhs = build_zero_cst (return_type);
2824 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2825 trap, rhs));
2829 argarray = VEC_address (tree, params);
2831 /* Check that arguments to builtin functions match the expectations. */
2832 if (fundecl
2833 && DECL_BUILT_IN (fundecl)
2834 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2835 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2836 return error_mark_node;
2838 /* Check that the arguments to the function are valid. */
2839 check_function_arguments (fntype, nargs, argarray);
2841 if (name != NULL_TREE
2842 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2844 if (require_constant_value)
2845 result =
2846 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2847 function, nargs, argarray);
2848 else
2849 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2850 function, nargs, argarray);
2851 if (TREE_CODE (result) == NOP_EXPR
2852 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2853 STRIP_TYPE_NOPS (result);
2855 else
2856 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2857 function, nargs, argarray);
2859 if (VOID_TYPE_P (TREE_TYPE (result)))
2861 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2862 pedwarn (loc, 0,
2863 "function with qualified void return type called");
2864 return result;
2866 return require_complete_type (result);
2869 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2870 and have vector types, V0 has the same type as V1, and the number of
2871 elements of V0, V1, MASK is the same.
2873 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2874 called with two arguments. In this case implementation passes the
2875 first argument twice in order to share the same tree code. This fact
2876 could enable the mask-values being twice the vector length. This is
2877 an implementation accident and this semantics is not guaranteed to
2878 the user. */
2879 tree
2880 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
2882 tree ret;
2883 bool wrap = true;
2884 bool maybe_const = false;
2885 bool two_arguments = false;
2887 if (v1 == NULL_TREE)
2889 two_arguments = true;
2890 v1 = v0;
2893 if (v0 == error_mark_node || v1 == error_mark_node
2894 || mask == error_mark_node)
2895 return error_mark_node;
2897 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2898 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2900 error_at (loc, "__builtin_shuffle last argument must "
2901 "be an integer vector");
2902 return error_mark_node;
2905 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2906 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2908 error_at (loc, "__builtin_shuffle arguments must be vectors");
2909 return error_mark_node;
2912 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2914 error_at (loc, "__builtin_shuffle argument vectors must be of "
2915 "the same type");
2916 return error_mark_node;
2919 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2920 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2921 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2922 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2924 error_at (loc, "__builtin_shuffle number of elements of the "
2925 "argument vector(s) and the mask vector should "
2926 "be the same");
2927 return error_mark_node;
2930 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2931 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2933 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2934 "must have the same size as inner type of the mask");
2935 return error_mark_node;
2938 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2939 v0 = c_fully_fold (v0, false, &maybe_const);
2940 wrap &= maybe_const;
2942 if (two_arguments)
2943 v1 = v0 = save_expr (v0);
2944 else
2946 v1 = c_fully_fold (v1, false, &maybe_const);
2947 wrap &= maybe_const;
2950 mask = c_fully_fold (mask, false, &maybe_const);
2951 wrap &= maybe_const;
2953 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2955 if (!wrap)
2956 ret = c_wrap_maybe_const (ret, true);
2958 return ret;
2961 /* Convert the argument expressions in the vector VALUES
2962 to the types in the list TYPELIST.
2964 If TYPELIST is exhausted, or when an element has NULL as its type,
2965 perform the default conversions.
2967 ORIGTYPES is the original types of the expressions in VALUES. This
2968 holds the type of enum values which have been converted to integral
2969 types. It may be NULL.
2971 FUNCTION is a tree for the called function. It is used only for
2972 error messages, where it is formatted with %qE.
2974 This is also where warnings about wrong number of args are generated.
2976 Returns the actual number of arguments processed (which may be less
2977 than the length of VALUES in some error situations), or -1 on
2978 failure. */
2980 static int
2981 convert_arguments (tree typelist, VEC(tree,gc) *values,
2982 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2984 tree typetail, val;
2985 unsigned int parmnum;
2986 bool error_args = false;
2987 const bool type_generic = fundecl
2988 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2989 bool type_generic_remove_excess_precision = false;
2990 tree selector;
2992 /* Change pointer to function to the function itself for
2993 diagnostics. */
2994 if (TREE_CODE (function) == ADDR_EXPR
2995 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2996 function = TREE_OPERAND (function, 0);
2998 /* Handle an ObjC selector specially for diagnostics. */
2999 selector = objc_message_selector ();
3001 /* For type-generic built-in functions, determine whether excess
3002 precision should be removed (classification) or not
3003 (comparison). */
3004 if (type_generic
3005 && DECL_BUILT_IN (fundecl)
3006 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3008 switch (DECL_FUNCTION_CODE (fundecl))
3010 case BUILT_IN_ISFINITE:
3011 case BUILT_IN_ISINF:
3012 case BUILT_IN_ISINF_SIGN:
3013 case BUILT_IN_ISNAN:
3014 case BUILT_IN_ISNORMAL:
3015 case BUILT_IN_FPCLASSIFY:
3016 type_generic_remove_excess_precision = true;
3017 break;
3019 default:
3020 type_generic_remove_excess_precision = false;
3021 break;
3025 /* Scan the given expressions and types, producing individual
3026 converted arguments. */
3028 for (typetail = typelist, parmnum = 0;
3029 VEC_iterate (tree, values, parmnum, val);
3030 ++parmnum)
3032 tree type = typetail ? TREE_VALUE (typetail) : 0;
3033 tree valtype = TREE_TYPE (val);
3034 tree rname = function;
3035 int argnum = parmnum + 1;
3036 const char *invalid_func_diag;
3037 bool excess_precision = false;
3038 bool npc;
3039 tree parmval;
3041 if (type == void_type_node)
3043 if (selector)
3044 error_at (input_location,
3045 "too many arguments to method %qE", selector);
3046 else
3047 error_at (input_location,
3048 "too many arguments to function %qE", function);
3050 if (fundecl && !DECL_BUILT_IN (fundecl))
3051 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3052 return parmnum;
3055 if (selector && argnum > 2)
3057 rname = selector;
3058 argnum -= 2;
3061 npc = null_pointer_constant_p (val);
3063 /* If there is excess precision and a prototype, convert once to
3064 the required type rather than converting via the semantic
3065 type. Likewise without a prototype a float value represented
3066 as long double should be converted once to double. But for
3067 type-generic classification functions excess precision must
3068 be removed here. */
3069 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3070 && (type || !type_generic || !type_generic_remove_excess_precision))
3072 val = TREE_OPERAND (val, 0);
3073 excess_precision = true;
3075 val = c_fully_fold (val, false, NULL);
3076 STRIP_TYPE_NOPS (val);
3078 val = require_complete_type (val);
3080 if (type != 0)
3082 /* Formal parm type is specified by a function prototype. */
3084 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3086 error ("type of formal parameter %d is incomplete", parmnum + 1);
3087 parmval = val;
3089 else
3091 tree origtype;
3093 /* Optionally warn about conversions that
3094 differ from the default conversions. */
3095 if (warn_traditional_conversion || warn_traditional)
3097 unsigned int formal_prec = TYPE_PRECISION (type);
3099 if (INTEGRAL_TYPE_P (type)
3100 && TREE_CODE (valtype) == REAL_TYPE)
3101 warning (0, "passing argument %d of %qE as integer "
3102 "rather than floating due to prototype",
3103 argnum, rname);
3104 if (INTEGRAL_TYPE_P (type)
3105 && TREE_CODE (valtype) == COMPLEX_TYPE)
3106 warning (0, "passing argument %d of %qE as integer "
3107 "rather than complex due to prototype",
3108 argnum, rname);
3109 else if (TREE_CODE (type) == COMPLEX_TYPE
3110 && TREE_CODE (valtype) == REAL_TYPE)
3111 warning (0, "passing argument %d of %qE as complex "
3112 "rather than floating due to prototype",
3113 argnum, rname);
3114 else if (TREE_CODE (type) == REAL_TYPE
3115 && INTEGRAL_TYPE_P (valtype))
3116 warning (0, "passing argument %d of %qE as floating "
3117 "rather than integer due to prototype",
3118 argnum, rname);
3119 else if (TREE_CODE (type) == COMPLEX_TYPE
3120 && INTEGRAL_TYPE_P (valtype))
3121 warning (0, "passing argument %d of %qE as complex "
3122 "rather than integer due to prototype",
3123 argnum, rname);
3124 else if (TREE_CODE (type) == REAL_TYPE
3125 && TREE_CODE (valtype) == COMPLEX_TYPE)
3126 warning (0, "passing argument %d of %qE as floating "
3127 "rather than complex due to prototype",
3128 argnum, rname);
3129 /* ??? At some point, messages should be written about
3130 conversions between complex types, but that's too messy
3131 to do now. */
3132 else if (TREE_CODE (type) == REAL_TYPE
3133 && TREE_CODE (valtype) == REAL_TYPE)
3135 /* Warn if any argument is passed as `float',
3136 since without a prototype it would be `double'. */
3137 if (formal_prec == TYPE_PRECISION (float_type_node)
3138 && type != dfloat32_type_node)
3139 warning (0, "passing argument %d of %qE as %<float%> "
3140 "rather than %<double%> due to prototype",
3141 argnum, rname);
3143 /* Warn if mismatch between argument and prototype
3144 for decimal float types. Warn of conversions with
3145 binary float types and of precision narrowing due to
3146 prototype. */
3147 else if (type != valtype
3148 && (type == dfloat32_type_node
3149 || type == dfloat64_type_node
3150 || type == dfloat128_type_node
3151 || valtype == dfloat32_type_node
3152 || valtype == dfloat64_type_node
3153 || valtype == dfloat128_type_node)
3154 && (formal_prec
3155 <= TYPE_PRECISION (valtype)
3156 || (type == dfloat128_type_node
3157 && (valtype
3158 != dfloat64_type_node
3159 && (valtype
3160 != dfloat32_type_node)))
3161 || (type == dfloat64_type_node
3162 && (valtype
3163 != dfloat32_type_node))))
3164 warning (0, "passing argument %d of %qE as %qT "
3165 "rather than %qT due to prototype",
3166 argnum, rname, type, valtype);
3169 /* Detect integer changing in width or signedness.
3170 These warnings are only activated with
3171 -Wtraditional-conversion, not with -Wtraditional. */
3172 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3173 && INTEGRAL_TYPE_P (valtype))
3175 tree would_have_been = default_conversion (val);
3176 tree type1 = TREE_TYPE (would_have_been);
3178 if (TREE_CODE (type) == ENUMERAL_TYPE
3179 && (TYPE_MAIN_VARIANT (type)
3180 == TYPE_MAIN_VARIANT (valtype)))
3181 /* No warning if function asks for enum
3182 and the actual arg is that enum type. */
3184 else if (formal_prec != TYPE_PRECISION (type1))
3185 warning (OPT_Wtraditional_conversion,
3186 "passing argument %d of %qE "
3187 "with different width due to prototype",
3188 argnum, rname);
3189 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3191 /* Don't complain if the formal parameter type
3192 is an enum, because we can't tell now whether
3193 the value was an enum--even the same enum. */
3194 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3196 else if (TREE_CODE (val) == INTEGER_CST
3197 && int_fits_type_p (val, type))
3198 /* Change in signedness doesn't matter
3199 if a constant value is unaffected. */
3201 /* If the value is extended from a narrower
3202 unsigned type, it doesn't matter whether we
3203 pass it as signed or unsigned; the value
3204 certainly is the same either way. */
3205 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3206 && TYPE_UNSIGNED (valtype))
3208 else if (TYPE_UNSIGNED (type))
3209 warning (OPT_Wtraditional_conversion,
3210 "passing argument %d of %qE "
3211 "as unsigned due to prototype",
3212 argnum, rname);
3213 else
3214 warning (OPT_Wtraditional_conversion,
3215 "passing argument %d of %qE "
3216 "as signed due to prototype", argnum, rname);
3220 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3221 sake of better warnings from convert_and_check. */
3222 if (excess_precision)
3223 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3224 origtype = (origtypes == NULL
3225 ? NULL_TREE
3226 : VEC_index (tree, origtypes, parmnum));
3227 parmval = convert_for_assignment (input_location, type, val,
3228 origtype, ic_argpass, npc,
3229 fundecl, function,
3230 parmnum + 1);
3232 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3233 && INTEGRAL_TYPE_P (type)
3234 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3235 parmval = default_conversion (parmval);
3238 else if (TREE_CODE (valtype) == REAL_TYPE
3239 && (TYPE_PRECISION (valtype)
3240 < TYPE_PRECISION (double_type_node))
3241 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3243 if (type_generic)
3244 parmval = val;
3245 else
3247 /* Convert `float' to `double'. */
3248 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3249 warning (OPT_Wdouble_promotion,
3250 "implicit conversion from %qT to %qT when passing "
3251 "argument to function",
3252 valtype, double_type_node);
3253 parmval = convert (double_type_node, val);
3256 else if (excess_precision && !type_generic)
3257 /* A "double" argument with excess precision being passed
3258 without a prototype or in variable arguments. */
3259 parmval = convert (valtype, val);
3260 else if ((invalid_func_diag =
3261 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3263 error (invalid_func_diag);
3264 return -1;
3266 else
3267 /* Convert `short' and `char' to full-size `int'. */
3268 parmval = default_conversion (val);
3270 VEC_replace (tree, values, parmnum, parmval);
3271 if (parmval == error_mark_node)
3272 error_args = true;
3274 if (typetail)
3275 typetail = TREE_CHAIN (typetail);
3278 gcc_assert (parmnum == VEC_length (tree, values));
3280 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3282 error_at (input_location,
3283 "too few arguments to function %qE", function);
3284 if (fundecl && !DECL_BUILT_IN (fundecl))
3285 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3286 return -1;
3289 return error_args ? -1 : (int) parmnum;
3292 /* This is the entry point used by the parser to build unary operators
3293 in the input. CODE, a tree_code, specifies the unary operator, and
3294 ARG is the operand. For unary plus, the C parser currently uses
3295 CONVERT_EXPR for code.
3297 LOC is the location to use for the tree generated.
3300 struct c_expr
3301 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3303 struct c_expr result;
3305 result.value = build_unary_op (loc, code, arg.value, 0);
3306 result.original_code = code;
3307 result.original_type = NULL;
3309 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3310 overflow_warning (loc, result.value);
3312 return result;
3315 /* This is the entry point used by the parser to build binary operators
3316 in the input. CODE, a tree_code, specifies the binary operator, and
3317 ARG1 and ARG2 are the operands. In addition to constructing the
3318 expression, we check for operands that were written with other binary
3319 operators in a way that is likely to confuse the user.
3321 LOCATION is the location of the binary operator. */
3323 struct c_expr
3324 parser_build_binary_op (location_t location, enum tree_code code,
3325 struct c_expr arg1, struct c_expr arg2)
3327 struct c_expr result;
3329 enum tree_code code1 = arg1.original_code;
3330 enum tree_code code2 = arg2.original_code;
3331 tree type1 = (arg1.original_type
3332 ? arg1.original_type
3333 : TREE_TYPE (arg1.value));
3334 tree type2 = (arg2.original_type
3335 ? arg2.original_type
3336 : TREE_TYPE (arg2.value));
3338 result.value = build_binary_op (location, code,
3339 arg1.value, arg2.value, 1);
3340 result.original_code = code;
3341 result.original_type = NULL;
3343 if (TREE_CODE (result.value) == ERROR_MARK)
3344 return result;
3346 if (location != UNKNOWN_LOCATION)
3347 protected_set_expr_location (result.value, location);
3349 /* Check for cases such as x+y<<z which users are likely
3350 to misinterpret. */
3351 if (warn_parentheses)
3352 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3354 if (warn_logical_op)
3355 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3356 code1, arg1.value, code2, arg2.value);
3358 /* Warn about comparisons against string literals, with the exception
3359 of testing for equality or inequality of a string literal with NULL. */
3360 if (code == EQ_EXPR || code == NE_EXPR)
3362 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3363 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3364 warning_at (location, OPT_Waddress,
3365 "comparison with string literal results in unspecified behavior");
3367 else if (TREE_CODE_CLASS (code) == tcc_comparison
3368 && (code1 == STRING_CST || code2 == STRING_CST))
3369 warning_at (location, OPT_Waddress,
3370 "comparison with string literal results in unspecified behavior");
3372 if (TREE_OVERFLOW_P (result.value)
3373 && !TREE_OVERFLOW_P (arg1.value)
3374 && !TREE_OVERFLOW_P (arg2.value))
3375 overflow_warning (location, result.value);
3377 /* Warn about comparisons of different enum types. */
3378 if (warn_enum_compare
3379 && TREE_CODE_CLASS (code) == tcc_comparison
3380 && TREE_CODE (type1) == ENUMERAL_TYPE
3381 && TREE_CODE (type2) == ENUMERAL_TYPE
3382 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3383 warning_at (location, OPT_Wenum_compare,
3384 "comparison between %qT and %qT",
3385 type1, type2);
3387 return result;
3390 /* Return a tree for the difference of pointers OP0 and OP1.
3391 The resulting tree has type int. */
3393 static tree
3394 pointer_diff (location_t loc, tree op0, tree op1)
3396 tree restype = ptrdiff_type_node;
3397 tree result, inttype;
3399 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3400 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3401 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3402 tree con0, con1, lit0, lit1;
3403 tree orig_op1 = op1;
3405 /* If the operands point into different address spaces, we need to
3406 explicitly convert them to pointers into the common address space
3407 before we can subtract the numerical address values. */
3408 if (as0 != as1)
3410 addr_space_t as_common;
3411 tree common_type;
3413 /* Determine the common superset address space. This is guaranteed
3414 to exist because the caller verified that comp_target_types
3415 returned non-zero. */
3416 if (!addr_space_superset (as0, as1, &as_common))
3417 gcc_unreachable ();
3419 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3420 op0 = convert (common_type, op0);
3421 op1 = convert (common_type, op1);
3424 /* Determine integer type to perform computations in. This will usually
3425 be the same as the result type (ptrdiff_t), but may need to be a wider
3426 type if pointers for the address space are wider than ptrdiff_t. */
3427 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3428 inttype = lang_hooks.types.type_for_size
3429 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3430 else
3431 inttype = restype;
3434 if (TREE_CODE (target_type) == VOID_TYPE)
3435 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3436 "pointer of type %<void *%> used in subtraction");
3437 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3438 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3439 "pointer to a function used in subtraction");
3441 /* If the conversion to ptrdiff_type does anything like widening or
3442 converting a partial to an integral mode, we get a convert_expression
3443 that is in the way to do any simplifications.
3444 (fold-const.c doesn't know that the extra bits won't be needed.
3445 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3446 different mode in place.)
3447 So first try to find a common term here 'by hand'; we want to cover
3448 at least the cases that occur in legal static initializers. */
3449 if (CONVERT_EXPR_P (op0)
3450 && (TYPE_PRECISION (TREE_TYPE (op0))
3451 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3452 con0 = TREE_OPERAND (op0, 0);
3453 else
3454 con0 = op0;
3455 if (CONVERT_EXPR_P (op1)
3456 && (TYPE_PRECISION (TREE_TYPE (op1))
3457 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3458 con1 = TREE_OPERAND (op1, 0);
3459 else
3460 con1 = op1;
3462 if (TREE_CODE (con0) == PLUS_EXPR)
3464 lit0 = TREE_OPERAND (con0, 1);
3465 con0 = TREE_OPERAND (con0, 0);
3467 else
3468 lit0 = integer_zero_node;
3470 if (TREE_CODE (con1) == PLUS_EXPR)
3472 lit1 = TREE_OPERAND (con1, 1);
3473 con1 = TREE_OPERAND (con1, 0);
3475 else
3476 lit1 = integer_zero_node;
3478 if (operand_equal_p (con0, con1, 0))
3480 op0 = lit0;
3481 op1 = lit1;
3485 /* First do the subtraction as integers;
3486 then drop through to build the divide operator.
3487 Do not do default conversions on the minus operator
3488 in case restype is a short type. */
3490 op0 = build_binary_op (loc,
3491 MINUS_EXPR, convert (inttype, op0),
3492 convert (inttype, op1), 0);
3493 /* This generates an error if op1 is pointer to incomplete type. */
3494 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3495 error_at (loc, "arithmetic on pointer to an incomplete type");
3497 /* This generates an error if op0 is pointer to incomplete type. */
3498 op1 = c_size_in_bytes (target_type);
3500 /* Divide by the size, in easiest possible way. */
3501 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3502 op0, convert (inttype, op1));
3504 /* Convert to final result type if necessary. */
3505 return convert (restype, result);
3508 /* Construct and perhaps optimize a tree representation
3509 for a unary operation. CODE, a tree_code, specifies the operation
3510 and XARG is the operand.
3511 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3512 the default promotions (such as from short to int).
3513 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3514 allows non-lvalues; this is only used to handle conversion of non-lvalue
3515 arrays to pointers in C99.
3517 LOCATION is the location of the operator. */
3519 tree
3520 build_unary_op (location_t location,
3521 enum tree_code code, tree xarg, int flag)
3523 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3524 tree arg = xarg;
3525 tree argtype = 0;
3526 enum tree_code typecode;
3527 tree val;
3528 tree ret = error_mark_node;
3529 tree eptype = NULL_TREE;
3530 int noconvert = flag;
3531 const char *invalid_op_diag;
3532 bool int_operands;
3534 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3535 if (int_operands)
3536 arg = remove_c_maybe_const_expr (arg);
3538 if (code != ADDR_EXPR)
3539 arg = require_complete_type (arg);
3541 typecode = TREE_CODE (TREE_TYPE (arg));
3542 if (typecode == ERROR_MARK)
3543 return error_mark_node;
3544 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3545 typecode = INTEGER_TYPE;
3547 if ((invalid_op_diag
3548 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3550 error_at (location, invalid_op_diag);
3551 return error_mark_node;
3554 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3556 eptype = TREE_TYPE (arg);
3557 arg = TREE_OPERAND (arg, 0);
3560 switch (code)
3562 case CONVERT_EXPR:
3563 /* This is used for unary plus, because a CONVERT_EXPR
3564 is enough to prevent anybody from looking inside for
3565 associativity, but won't generate any code. */
3566 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3567 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3568 || typecode == VECTOR_TYPE))
3570 error_at (location, "wrong type argument to unary plus");
3571 return error_mark_node;
3573 else if (!noconvert)
3574 arg = default_conversion (arg);
3575 arg = non_lvalue_loc (location, arg);
3576 break;
3578 case NEGATE_EXPR:
3579 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3580 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3581 || typecode == VECTOR_TYPE))
3583 error_at (location, "wrong type argument to unary minus");
3584 return error_mark_node;
3586 else if (!noconvert)
3587 arg = default_conversion (arg);
3588 break;
3590 case BIT_NOT_EXPR:
3591 /* ~ works on integer types and non float vectors. */
3592 if (typecode == INTEGER_TYPE
3593 || (typecode == VECTOR_TYPE
3594 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3596 if (!noconvert)
3597 arg = default_conversion (arg);
3599 else if (typecode == COMPLEX_TYPE)
3601 code = CONJ_EXPR;
3602 pedwarn (location, OPT_pedantic,
3603 "ISO C does not support %<~%> for complex conjugation");
3604 if (!noconvert)
3605 arg = default_conversion (arg);
3607 else
3609 error_at (location, "wrong type argument to bit-complement");
3610 return error_mark_node;
3612 break;
3614 case ABS_EXPR:
3615 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3617 error_at (location, "wrong type argument to abs");
3618 return error_mark_node;
3620 else if (!noconvert)
3621 arg = default_conversion (arg);
3622 break;
3624 case CONJ_EXPR:
3625 /* Conjugating a real value is a no-op, but allow it anyway. */
3626 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3627 || typecode == COMPLEX_TYPE))
3629 error_at (location, "wrong type argument to conjugation");
3630 return error_mark_node;
3632 else if (!noconvert)
3633 arg = default_conversion (arg);
3634 break;
3636 case TRUTH_NOT_EXPR:
3637 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3638 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3639 && typecode != COMPLEX_TYPE)
3641 error_at (location,
3642 "wrong type argument to unary exclamation mark");
3643 return error_mark_node;
3645 if (int_operands)
3647 arg = c_objc_common_truthvalue_conversion (location, xarg);
3648 arg = remove_c_maybe_const_expr (arg);
3650 else
3651 arg = c_objc_common_truthvalue_conversion (location, arg);
3652 ret = invert_truthvalue_loc (location, arg);
3653 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3654 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3655 location = EXPR_LOCATION (ret);
3656 goto return_build_unary_op;
3658 case REALPART_EXPR:
3659 case IMAGPART_EXPR:
3660 ret = build_real_imag_expr (location, code, arg);
3661 if (ret == error_mark_node)
3662 return error_mark_node;
3663 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3664 eptype = TREE_TYPE (eptype);
3665 goto return_build_unary_op;
3667 case PREINCREMENT_EXPR:
3668 case POSTINCREMENT_EXPR:
3669 case PREDECREMENT_EXPR:
3670 case POSTDECREMENT_EXPR:
3672 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3674 tree inner = build_unary_op (location, code,
3675 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3676 if (inner == error_mark_node)
3677 return error_mark_node;
3678 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3679 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3680 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3681 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3682 goto return_build_unary_op;
3685 /* Complain about anything that is not a true lvalue. In
3686 Objective-C, skip this check for property_refs. */
3687 if (!objc_is_property_ref (arg)
3688 && !lvalue_or_else (location,
3689 arg, ((code == PREINCREMENT_EXPR
3690 || code == POSTINCREMENT_EXPR)
3691 ? lv_increment
3692 : lv_decrement)))
3693 return error_mark_node;
3695 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3697 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3698 warning_at (location, OPT_Wc___compat,
3699 "increment of enumeration value is invalid in C++");
3700 else
3701 warning_at (location, OPT_Wc___compat,
3702 "decrement of enumeration value is invalid in C++");
3705 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3706 arg = c_fully_fold (arg, false, NULL);
3708 /* Increment or decrement the real part of the value,
3709 and don't change the imaginary part. */
3710 if (typecode == COMPLEX_TYPE)
3712 tree real, imag;
3714 pedwarn (location, OPT_pedantic,
3715 "ISO C does not support %<++%> and %<--%> on complex types");
3717 arg = stabilize_reference (arg);
3718 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3719 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3720 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3721 if (real == error_mark_node || imag == error_mark_node)
3722 return error_mark_node;
3723 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3724 real, imag);
3725 goto return_build_unary_op;
3728 /* Report invalid types. */
3730 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3731 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3733 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3734 error_at (location, "wrong type argument to increment");
3735 else
3736 error_at (location, "wrong type argument to decrement");
3738 return error_mark_node;
3742 tree inc;
3744 argtype = TREE_TYPE (arg);
3746 /* Compute the increment. */
3748 if (typecode == POINTER_TYPE)
3750 /* If pointer target is an undefined struct,
3751 we just cannot know how to do the arithmetic. */
3752 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3754 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3755 error_at (location,
3756 "increment of pointer to unknown structure");
3757 else
3758 error_at (location,
3759 "decrement of pointer to unknown structure");
3761 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3762 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3764 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3765 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3766 "wrong type argument to increment");
3767 else
3768 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3769 "wrong type argument to decrement");
3772 inc = c_size_in_bytes (TREE_TYPE (argtype));
3773 inc = convert_to_ptrofftype_loc (location, inc);
3775 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3777 /* For signed fract types, we invert ++ to -- or
3778 -- to ++, and change inc from 1 to -1, because
3779 it is not possible to represent 1 in signed fract constants.
3780 For unsigned fract types, the result always overflows and
3781 we get an undefined (original) or the maximum value. */
3782 if (code == PREINCREMENT_EXPR)
3783 code = PREDECREMENT_EXPR;
3784 else if (code == PREDECREMENT_EXPR)
3785 code = PREINCREMENT_EXPR;
3786 else if (code == POSTINCREMENT_EXPR)
3787 code = POSTDECREMENT_EXPR;
3788 else /* code == POSTDECREMENT_EXPR */
3789 code = POSTINCREMENT_EXPR;
3791 inc = integer_minus_one_node;
3792 inc = convert (argtype, inc);
3794 else
3796 inc = integer_one_node;
3797 inc = convert (argtype, inc);
3800 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3801 need to ask Objective-C to build the increment or decrement
3802 expression for it. */
3803 if (objc_is_property_ref (arg))
3804 return objc_build_incr_expr_for_property_ref (location, code,
3805 arg, inc);
3807 /* Report a read-only lvalue. */
3808 if (TYPE_READONLY (argtype))
3810 readonly_error (arg,
3811 ((code == PREINCREMENT_EXPR
3812 || code == POSTINCREMENT_EXPR)
3813 ? lv_increment : lv_decrement));
3814 return error_mark_node;
3816 else if (TREE_READONLY (arg))
3817 readonly_warning (arg,
3818 ((code == PREINCREMENT_EXPR
3819 || code == POSTINCREMENT_EXPR)
3820 ? lv_increment : lv_decrement));
3822 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3823 val = boolean_increment (code, arg);
3824 else
3825 val = build2 (code, TREE_TYPE (arg), arg, inc);
3826 TREE_SIDE_EFFECTS (val) = 1;
3827 if (TREE_CODE (val) != code)
3828 TREE_NO_WARNING (val) = 1;
3829 ret = val;
3830 goto return_build_unary_op;
3833 case ADDR_EXPR:
3834 /* Note that this operation never does default_conversion. */
3836 /* The operand of unary '&' must be an lvalue (which excludes
3837 expressions of type void), or, in C99, the result of a [] or
3838 unary '*' operator. */
3839 if (VOID_TYPE_P (TREE_TYPE (arg))
3840 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3841 && (TREE_CODE (arg) != INDIRECT_REF
3842 || !flag_isoc99))
3843 pedwarn (location, 0, "taking address of expression of type %<void%>");
3845 /* Let &* cancel out to simplify resulting code. */
3846 if (TREE_CODE (arg) == INDIRECT_REF)
3848 /* Don't let this be an lvalue. */
3849 if (lvalue_p (TREE_OPERAND (arg, 0)))
3850 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3851 ret = TREE_OPERAND (arg, 0);
3852 goto return_build_unary_op;
3855 /* For &x[y], return x+y */
3856 if (TREE_CODE (arg) == ARRAY_REF)
3858 tree op0 = TREE_OPERAND (arg, 0);
3859 if (!c_mark_addressable (op0))
3860 return error_mark_node;
3863 /* Anything not already handled and not a true memory reference
3864 or a non-lvalue array is an error. */
3865 else if (typecode != FUNCTION_TYPE && !flag
3866 && !lvalue_or_else (location, arg, lv_addressof))
3867 return error_mark_node;
3869 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3870 folding later. */
3871 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3873 tree inner = build_unary_op (location, code,
3874 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3875 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3876 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3877 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3878 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3879 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3880 goto return_build_unary_op;
3883 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3884 argtype = TREE_TYPE (arg);
3886 /* If the lvalue is const or volatile, merge that into the type
3887 to which the address will point. This is only needed
3888 for function types. */
3889 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3890 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3891 && TREE_CODE (argtype) == FUNCTION_TYPE)
3893 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3894 int quals = orig_quals;
3896 if (TREE_READONLY (arg))
3897 quals |= TYPE_QUAL_CONST;
3898 if (TREE_THIS_VOLATILE (arg))
3899 quals |= TYPE_QUAL_VOLATILE;
3901 argtype = c_build_qualified_type (argtype, quals);
3904 if (!c_mark_addressable (arg))
3905 return error_mark_node;
3907 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3908 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3910 argtype = build_pointer_type (argtype);
3912 /* ??? Cope with user tricks that amount to offsetof. Delete this
3913 when we have proper support for integer constant expressions. */
3914 val = get_base_address (arg);
3915 if (val && TREE_CODE (val) == INDIRECT_REF
3916 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3918 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
3919 goto return_build_unary_op;
3922 val = build1 (ADDR_EXPR, argtype, arg);
3924 ret = val;
3925 goto return_build_unary_op;
3927 default:
3928 gcc_unreachable ();
3931 if (argtype == 0)
3932 argtype = TREE_TYPE (arg);
3933 if (TREE_CODE (arg) == INTEGER_CST)
3934 ret = (require_constant_value
3935 ? fold_build1_initializer_loc (location, code, argtype, arg)
3936 : fold_build1_loc (location, code, argtype, arg));
3937 else
3938 ret = build1 (code, argtype, arg);
3939 return_build_unary_op:
3940 gcc_assert (ret != error_mark_node);
3941 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3942 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3943 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3944 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3945 ret = note_integer_operands (ret);
3946 if (eptype)
3947 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3948 protected_set_expr_location (ret, location);
3949 return ret;
3952 /* Return nonzero if REF is an lvalue valid for this language.
3953 Lvalues can be assigned, unless their type has TYPE_READONLY.
3954 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3956 bool
3957 lvalue_p (const_tree ref)
3959 const enum tree_code code = TREE_CODE (ref);
3961 switch (code)
3963 case REALPART_EXPR:
3964 case IMAGPART_EXPR:
3965 case COMPONENT_REF:
3966 return lvalue_p (TREE_OPERAND (ref, 0));
3968 case C_MAYBE_CONST_EXPR:
3969 return lvalue_p (TREE_OPERAND (ref, 1));
3971 case COMPOUND_LITERAL_EXPR:
3972 case STRING_CST:
3973 return 1;
3975 case INDIRECT_REF:
3976 case ARRAY_REF:
3977 case VAR_DECL:
3978 case PARM_DECL:
3979 case RESULT_DECL:
3980 case ERROR_MARK:
3981 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3982 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3984 case BIND_EXPR:
3985 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3987 default:
3988 return 0;
3992 /* Give a warning for storing in something that is read-only in GCC
3993 terms but not const in ISO C terms. */
3995 static void
3996 readonly_warning (tree arg, enum lvalue_use use)
3998 switch (use)
4000 case lv_assign:
4001 warning (0, "assignment of read-only location %qE", arg);
4002 break;
4003 case lv_increment:
4004 warning (0, "increment of read-only location %qE", arg);
4005 break;
4006 case lv_decrement:
4007 warning (0, "decrement of read-only location %qE", arg);
4008 break;
4009 default:
4010 gcc_unreachable ();
4012 return;
4016 /* Return nonzero if REF is an lvalue valid for this language;
4017 otherwise, print an error message and return zero. USE says
4018 how the lvalue is being used and so selects the error message.
4019 LOCATION is the location at which any error should be reported. */
4021 static int
4022 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4024 int win = lvalue_p (ref);
4026 if (!win)
4027 lvalue_error (loc, use);
4029 return win;
4032 /* Mark EXP saying that we need to be able to take the
4033 address of it; it should not be allocated in a register.
4034 Returns true if successful. */
4036 bool
4037 c_mark_addressable (tree exp)
4039 tree x = exp;
4041 while (1)
4042 switch (TREE_CODE (x))
4044 case COMPONENT_REF:
4045 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4047 error
4048 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4049 return false;
4052 /* ... fall through ... */
4054 case ADDR_EXPR:
4055 case ARRAY_REF:
4056 case REALPART_EXPR:
4057 case IMAGPART_EXPR:
4058 x = TREE_OPERAND (x, 0);
4059 break;
4061 case COMPOUND_LITERAL_EXPR:
4062 case CONSTRUCTOR:
4063 TREE_ADDRESSABLE (x) = 1;
4064 return true;
4066 case VAR_DECL:
4067 case CONST_DECL:
4068 case PARM_DECL:
4069 case RESULT_DECL:
4070 if (C_DECL_REGISTER (x)
4071 && DECL_NONLOCAL (x))
4073 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4075 error
4076 ("global register variable %qD used in nested function", x);
4077 return false;
4079 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4081 else if (C_DECL_REGISTER (x))
4083 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4084 error ("address of global register variable %qD requested", x);
4085 else
4086 error ("address of register variable %qD requested", x);
4087 return false;
4090 /* drops in */
4091 case FUNCTION_DECL:
4092 TREE_ADDRESSABLE (x) = 1;
4093 /* drops out */
4094 default:
4095 return true;
4099 /* Convert EXPR to TYPE, warning about conversion problems with
4100 constants. SEMANTIC_TYPE is the type this conversion would use
4101 without excess precision. If SEMANTIC_TYPE is NULL, this function
4102 is equivalent to convert_and_check. This function is a wrapper that
4103 handles conversions that may be different than
4104 the usual ones because of excess precision. */
4106 static tree
4107 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4109 if (TREE_TYPE (expr) == type)
4110 return expr;
4112 if (!semantic_type)
4113 return convert_and_check (type, expr);
4115 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4116 && TREE_TYPE (expr) != semantic_type)
4118 /* For integers, we need to check the real conversion, not
4119 the conversion to the excess precision type. */
4120 expr = convert_and_check (semantic_type, expr);
4122 /* Result type is the excess precision type, which should be
4123 large enough, so do not check. */
4124 return convert (type, expr);
4127 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4128 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4129 if folded to an integer constant then the unselected half may
4130 contain arbitrary operations not normally permitted in constant
4131 expressions. Set the location of the expression to LOC. */
4133 tree
4134 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4135 tree op1, tree op1_original_type, tree op2,
4136 tree op2_original_type)
4138 tree type1;
4139 tree type2;
4140 enum tree_code code1;
4141 enum tree_code code2;
4142 tree result_type = NULL;
4143 tree semantic_result_type = NULL;
4144 tree orig_op1 = op1, orig_op2 = op2;
4145 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4146 bool ifexp_int_operands;
4147 tree ret;
4149 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4150 if (op1_int_operands)
4151 op1 = remove_c_maybe_const_expr (op1);
4152 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4153 if (op2_int_operands)
4154 op2 = remove_c_maybe_const_expr (op2);
4155 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4156 if (ifexp_int_operands)
4157 ifexp = remove_c_maybe_const_expr (ifexp);
4159 /* Promote both alternatives. */
4161 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4162 op1 = default_conversion (op1);
4163 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4164 op2 = default_conversion (op2);
4166 if (TREE_CODE (ifexp) == ERROR_MARK
4167 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4168 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4169 return error_mark_node;
4171 type1 = TREE_TYPE (op1);
4172 code1 = TREE_CODE (type1);
4173 type2 = TREE_TYPE (op2);
4174 code2 = TREE_CODE (type2);
4176 /* C90 does not permit non-lvalue arrays in conditional expressions.
4177 In C99 they will be pointers by now. */
4178 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4180 error_at (colon_loc, "non-lvalue array in conditional expression");
4181 return error_mark_node;
4184 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4185 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4186 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4187 || code1 == COMPLEX_TYPE)
4188 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4189 || code2 == COMPLEX_TYPE))
4191 semantic_result_type = c_common_type (type1, type2);
4192 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4194 op1 = TREE_OPERAND (op1, 0);
4195 type1 = TREE_TYPE (op1);
4196 gcc_assert (TREE_CODE (type1) == code1);
4198 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4200 op2 = TREE_OPERAND (op2, 0);
4201 type2 = TREE_TYPE (op2);
4202 gcc_assert (TREE_CODE (type2) == code2);
4206 if (warn_cxx_compat)
4208 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4209 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4211 if (TREE_CODE (t1) == ENUMERAL_TYPE
4212 && TREE_CODE (t2) == ENUMERAL_TYPE
4213 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4214 warning_at (colon_loc, OPT_Wc___compat,
4215 ("different enum types in conditional is "
4216 "invalid in C++: %qT vs %qT"),
4217 t1, t2);
4220 /* Quickly detect the usual case where op1 and op2 have the same type
4221 after promotion. */
4222 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4224 if (type1 == type2)
4225 result_type = type1;
4226 else
4227 result_type = TYPE_MAIN_VARIANT (type1);
4229 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4230 || code1 == COMPLEX_TYPE)
4231 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4232 || code2 == COMPLEX_TYPE))
4234 result_type = c_common_type (type1, type2);
4235 do_warn_double_promotion (result_type, type1, type2,
4236 "implicit conversion from %qT to %qT to "
4237 "match other result of conditional",
4238 colon_loc);
4240 /* If -Wsign-compare, warn here if type1 and type2 have
4241 different signedness. We'll promote the signed to unsigned
4242 and later code won't know it used to be different.
4243 Do this check on the original types, so that explicit casts
4244 will be considered, but default promotions won't. */
4245 if (c_inhibit_evaluation_warnings == 0)
4247 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4248 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4250 if (unsigned_op1 ^ unsigned_op2)
4252 bool ovf;
4254 /* Do not warn if the result type is signed, since the
4255 signed type will only be chosen if it can represent
4256 all the values of the unsigned type. */
4257 if (!TYPE_UNSIGNED (result_type))
4258 /* OK */;
4259 else
4261 bool op1_maybe_const = true;
4262 bool op2_maybe_const = true;
4264 /* Do not warn if the signed quantity is an
4265 unsuffixed integer literal (or some static
4266 constant expression involving such literals) and
4267 it is non-negative. This warning requires the
4268 operands to be folded for best results, so do
4269 that folding in this case even without
4270 warn_sign_compare to avoid warning options
4271 possibly affecting code generation. */
4272 c_inhibit_evaluation_warnings
4273 += (ifexp == truthvalue_false_node);
4274 op1 = c_fully_fold (op1, require_constant_value,
4275 &op1_maybe_const);
4276 c_inhibit_evaluation_warnings
4277 -= (ifexp == truthvalue_false_node);
4279 c_inhibit_evaluation_warnings
4280 += (ifexp == truthvalue_true_node);
4281 op2 = c_fully_fold (op2, require_constant_value,
4282 &op2_maybe_const);
4283 c_inhibit_evaluation_warnings
4284 -= (ifexp == truthvalue_true_node);
4286 if (warn_sign_compare)
4288 if ((unsigned_op2
4289 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4290 || (unsigned_op1
4291 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4292 /* OK */;
4293 else
4294 warning_at (colon_loc, OPT_Wsign_compare,
4295 ("signed and unsigned type in "
4296 "conditional expression"));
4298 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4299 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4300 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4301 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4306 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4308 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4309 pedwarn (colon_loc, OPT_pedantic,
4310 "ISO C forbids conditional expr with only one void side");
4311 result_type = void_type_node;
4313 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4315 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4316 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4317 addr_space_t as_common;
4319 if (comp_target_types (colon_loc, type1, type2))
4320 result_type = common_pointer_type (type1, type2);
4321 else if (null_pointer_constant_p (orig_op1))
4322 result_type = type2;
4323 else if (null_pointer_constant_p (orig_op2))
4324 result_type = type1;
4325 else if (!addr_space_superset (as1, as2, &as_common))
4327 error_at (colon_loc, "pointers to disjoint address spaces "
4328 "used in conditional expression");
4329 return error_mark_node;
4331 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4333 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4334 pedwarn (colon_loc, OPT_pedantic,
4335 "ISO C forbids conditional expr between "
4336 "%<void *%> and function pointer");
4337 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4338 TREE_TYPE (type2)));
4340 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4342 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4343 pedwarn (colon_loc, OPT_pedantic,
4344 "ISO C forbids conditional expr between "
4345 "%<void *%> and function pointer");
4346 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4347 TREE_TYPE (type1)));
4349 /* Objective-C pointer comparisons are a bit more lenient. */
4350 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4351 result_type = objc_common_type (type1, type2);
4352 else
4354 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4356 pedwarn (colon_loc, 0,
4357 "pointer type mismatch in conditional expression");
4358 result_type = build_pointer_type
4359 (build_qualified_type (void_type_node, qual));
4362 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4364 if (!null_pointer_constant_p (orig_op2))
4365 pedwarn (colon_loc, 0,
4366 "pointer/integer type mismatch in conditional expression");
4367 else
4369 op2 = null_pointer_node;
4371 result_type = type1;
4373 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4375 if (!null_pointer_constant_p (orig_op1))
4376 pedwarn (colon_loc, 0,
4377 "pointer/integer type mismatch in conditional expression");
4378 else
4380 op1 = null_pointer_node;
4382 result_type = type2;
4385 if (!result_type)
4387 if (flag_cond_mismatch)
4388 result_type = void_type_node;
4389 else
4391 error_at (colon_loc, "type mismatch in conditional expression");
4392 return error_mark_node;
4396 /* Merge const and volatile flags of the incoming types. */
4397 result_type
4398 = build_type_variant (result_type,
4399 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4400 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4402 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4403 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4405 if (ifexp_bcp && ifexp == truthvalue_true_node)
4407 op2_int_operands = true;
4408 op1 = c_fully_fold (op1, require_constant_value, NULL);
4410 if (ifexp_bcp && ifexp == truthvalue_false_node)
4412 op1_int_operands = true;
4413 op2 = c_fully_fold (op2, require_constant_value, NULL);
4415 int_const = int_operands = (ifexp_int_operands
4416 && op1_int_operands
4417 && op2_int_operands);
4418 if (int_operands)
4420 int_const = ((ifexp == truthvalue_true_node
4421 && TREE_CODE (orig_op1) == INTEGER_CST
4422 && !TREE_OVERFLOW (orig_op1))
4423 || (ifexp == truthvalue_false_node
4424 && TREE_CODE (orig_op2) == INTEGER_CST
4425 && !TREE_OVERFLOW (orig_op2)));
4427 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4428 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4429 else
4431 if (int_operands)
4433 op1 = remove_c_maybe_const_expr (op1);
4434 op2 = remove_c_maybe_const_expr (op2);
4436 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4437 if (int_operands)
4438 ret = note_integer_operands (ret);
4440 if (semantic_result_type)
4441 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4443 protected_set_expr_location (ret, colon_loc);
4444 return ret;
4447 /* Return a compound expression that performs two expressions and
4448 returns the value of the second of them.
4450 LOC is the location of the COMPOUND_EXPR. */
4452 tree
4453 build_compound_expr (location_t loc, tree expr1, tree expr2)
4455 bool expr1_int_operands, expr2_int_operands;
4456 tree eptype = NULL_TREE;
4457 tree ret;
4459 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4460 if (expr1_int_operands)
4461 expr1 = remove_c_maybe_const_expr (expr1);
4462 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4463 if (expr2_int_operands)
4464 expr2 = remove_c_maybe_const_expr (expr2);
4466 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4467 expr1 = TREE_OPERAND (expr1, 0);
4468 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4470 eptype = TREE_TYPE (expr2);
4471 expr2 = TREE_OPERAND (expr2, 0);
4474 if (!TREE_SIDE_EFFECTS (expr1))
4476 /* The left-hand operand of a comma expression is like an expression
4477 statement: with -Wunused, we should warn if it doesn't have
4478 any side-effects, unless it was explicitly cast to (void). */
4479 if (warn_unused_value)
4481 if (VOID_TYPE_P (TREE_TYPE (expr1))
4482 && CONVERT_EXPR_P (expr1))
4483 ; /* (void) a, b */
4484 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4485 && TREE_CODE (expr1) == COMPOUND_EXPR
4486 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4487 ; /* (void) a, (void) b, c */
4488 else
4489 warning_at (loc, OPT_Wunused_value,
4490 "left-hand operand of comma expression has no effect");
4494 /* With -Wunused, we should also warn if the left-hand operand does have
4495 side-effects, but computes a value which is not used. For example, in
4496 `foo() + bar(), baz()' the result of the `+' operator is not used,
4497 so we should issue a warning. */
4498 else if (warn_unused_value)
4499 warn_if_unused_value (expr1, loc);
4501 if (expr2 == error_mark_node)
4502 return error_mark_node;
4504 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4506 if (flag_isoc99
4507 && expr1_int_operands
4508 && expr2_int_operands)
4509 ret = note_integer_operands (ret);
4511 if (eptype)
4512 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4514 protected_set_expr_location (ret, loc);
4515 return ret;
4518 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4519 which we are casting. OTYPE is the type of the expression being
4520 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4521 of the cast. -Wcast-qual appeared on the command line. Named
4522 address space qualifiers are not handled here, because they result
4523 in different warnings. */
4525 static void
4526 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4528 tree in_type = type;
4529 tree in_otype = otype;
4530 int added = 0;
4531 int discarded = 0;
4532 bool is_const;
4534 /* Check that the qualifiers on IN_TYPE are a superset of the
4535 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4536 nodes is uninteresting and we stop as soon as we hit a
4537 non-POINTER_TYPE node on either type. */
4540 in_otype = TREE_TYPE (in_otype);
4541 in_type = TREE_TYPE (in_type);
4543 /* GNU C allows cv-qualified function types. 'const' means the
4544 function is very pure, 'volatile' means it can't return. We
4545 need to warn when such qualifiers are added, not when they're
4546 taken away. */
4547 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4548 && TREE_CODE (in_type) == FUNCTION_TYPE)
4549 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4550 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4551 else
4552 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4553 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4555 while (TREE_CODE (in_type) == POINTER_TYPE
4556 && TREE_CODE (in_otype) == POINTER_TYPE);
4558 if (added)
4559 warning_at (loc, OPT_Wcast_qual,
4560 "cast adds %q#v qualifier to function type", added);
4562 if (discarded)
4563 /* There are qualifiers present in IN_OTYPE that are not present
4564 in IN_TYPE. */
4565 warning_at (loc, OPT_Wcast_qual,
4566 "cast discards %q#v qualifier from pointer target type",
4567 discarded);
4569 if (added || discarded)
4570 return;
4572 /* A cast from **T to const **T is unsafe, because it can cause a
4573 const value to be changed with no additional warning. We only
4574 issue this warning if T is the same on both sides, and we only
4575 issue the warning if there are the same number of pointers on
4576 both sides, as otherwise the cast is clearly unsafe anyhow. A
4577 cast is unsafe when a qualifier is added at one level and const
4578 is not present at all outer levels.
4580 To issue this warning, we check at each level whether the cast
4581 adds new qualifiers not already seen. We don't need to special
4582 case function types, as they won't have the same
4583 TYPE_MAIN_VARIANT. */
4585 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4586 return;
4587 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4588 return;
4590 in_type = type;
4591 in_otype = otype;
4592 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4595 in_type = TREE_TYPE (in_type);
4596 in_otype = TREE_TYPE (in_otype);
4597 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4598 && !is_const)
4600 warning_at (loc, OPT_Wcast_qual,
4601 "to be safe all intermediate pointers in cast from "
4602 "%qT to %qT must be %<const%> qualified",
4603 otype, type);
4604 break;
4606 if (is_const)
4607 is_const = TYPE_READONLY (in_type);
4609 while (TREE_CODE (in_type) == POINTER_TYPE);
4612 /* Build an expression representing a cast to type TYPE of expression EXPR.
4613 LOC is the location of the cast-- typically the open paren of the cast. */
4615 tree
4616 build_c_cast (location_t loc, tree type, tree expr)
4618 tree value;
4620 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4621 expr = TREE_OPERAND (expr, 0);
4623 value = expr;
4625 if (type == error_mark_node || expr == error_mark_node)
4626 return error_mark_node;
4628 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4629 only in <protocol> qualifications. But when constructing cast expressions,
4630 the protocols do matter and must be kept around. */
4631 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4632 return build1 (NOP_EXPR, type, expr);
4634 type = TYPE_MAIN_VARIANT (type);
4636 if (TREE_CODE (type) == ARRAY_TYPE)
4638 error_at (loc, "cast specifies array type");
4639 return error_mark_node;
4642 if (TREE_CODE (type) == FUNCTION_TYPE)
4644 error_at (loc, "cast specifies function type");
4645 return error_mark_node;
4648 if (!VOID_TYPE_P (type))
4650 value = require_complete_type (value);
4651 if (value == error_mark_node)
4652 return error_mark_node;
4655 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4657 if (TREE_CODE (type) == RECORD_TYPE
4658 || TREE_CODE (type) == UNION_TYPE)
4659 pedwarn (loc, OPT_pedantic,
4660 "ISO C forbids casting nonscalar to the same type");
4662 else if (TREE_CODE (type) == UNION_TYPE)
4664 tree field;
4666 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4667 if (TREE_TYPE (field) != error_mark_node
4668 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4669 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4670 break;
4672 if (field)
4674 tree t;
4675 bool maybe_const = true;
4677 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4678 t = c_fully_fold (value, false, &maybe_const);
4679 t = build_constructor_single (type, field, t);
4680 if (!maybe_const)
4681 t = c_wrap_maybe_const (t, true);
4682 t = digest_init (loc, type, t,
4683 NULL_TREE, false, true, 0);
4684 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4685 return t;
4687 error_at (loc, "cast to union type from type not present in union");
4688 return error_mark_node;
4690 else
4692 tree otype, ovalue;
4694 if (type == void_type_node)
4696 tree t = build1 (CONVERT_EXPR, type, value);
4697 SET_EXPR_LOCATION (t, loc);
4698 return t;
4701 otype = TREE_TYPE (value);
4703 /* Optionally warn about potentially worrisome casts. */
4704 if (warn_cast_qual
4705 && TREE_CODE (type) == POINTER_TYPE
4706 && TREE_CODE (otype) == POINTER_TYPE)
4707 handle_warn_cast_qual (loc, type, otype);
4709 /* Warn about conversions between pointers to disjoint
4710 address spaces. */
4711 if (TREE_CODE (type) == POINTER_TYPE
4712 && TREE_CODE (otype) == POINTER_TYPE
4713 && !null_pointer_constant_p (value))
4715 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4716 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4717 addr_space_t as_common;
4719 if (!addr_space_superset (as_to, as_from, &as_common))
4721 if (ADDR_SPACE_GENERIC_P (as_from))
4722 warning_at (loc, 0, "cast to %s address space pointer "
4723 "from disjoint generic address space pointer",
4724 c_addr_space_name (as_to));
4726 else if (ADDR_SPACE_GENERIC_P (as_to))
4727 warning_at (loc, 0, "cast to generic address space pointer "
4728 "from disjoint %s address space pointer",
4729 c_addr_space_name (as_from));
4731 else
4732 warning_at (loc, 0, "cast to %s address space pointer "
4733 "from disjoint %s address space pointer",
4734 c_addr_space_name (as_to),
4735 c_addr_space_name (as_from));
4739 /* Warn about possible alignment problems. */
4740 if (STRICT_ALIGNMENT
4741 && TREE_CODE (type) == POINTER_TYPE
4742 && TREE_CODE (otype) == POINTER_TYPE
4743 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4744 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4745 /* Don't warn about opaque types, where the actual alignment
4746 restriction is unknown. */
4747 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4748 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4749 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4750 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4751 warning_at (loc, OPT_Wcast_align,
4752 "cast increases required alignment of target type");
4754 if (TREE_CODE (type) == INTEGER_TYPE
4755 && TREE_CODE (otype) == POINTER_TYPE
4756 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4757 /* Unlike conversion of integers to pointers, where the
4758 warning is disabled for converting constants because
4759 of cases such as SIG_*, warn about converting constant
4760 pointers to integers. In some cases it may cause unwanted
4761 sign extension, and a warning is appropriate. */
4762 warning_at (loc, OPT_Wpointer_to_int_cast,
4763 "cast from pointer to integer of different size");
4765 if (TREE_CODE (value) == CALL_EXPR
4766 && TREE_CODE (type) != TREE_CODE (otype))
4767 warning_at (loc, OPT_Wbad_function_cast,
4768 "cast from function call of type %qT "
4769 "to non-matching type %qT", otype, type);
4771 if (TREE_CODE (type) == POINTER_TYPE
4772 && TREE_CODE (otype) == INTEGER_TYPE
4773 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4774 /* Don't warn about converting any constant. */
4775 && !TREE_CONSTANT (value))
4776 warning_at (loc,
4777 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4778 "of different size");
4780 if (warn_strict_aliasing <= 2)
4781 strict_aliasing_warning (otype, type, expr);
4783 /* If pedantic, warn for conversions between function and object
4784 pointer types, except for converting a null pointer constant
4785 to function pointer type. */
4786 if (pedantic
4787 && TREE_CODE (type) == POINTER_TYPE
4788 && TREE_CODE (otype) == POINTER_TYPE
4789 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4790 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4791 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4792 "conversion of function pointer to object pointer type");
4794 if (pedantic
4795 && TREE_CODE (type) == POINTER_TYPE
4796 && TREE_CODE (otype) == POINTER_TYPE
4797 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4798 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4799 && !null_pointer_constant_p (value))
4800 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4801 "conversion of object pointer to function pointer type");
4803 ovalue = value;
4804 value = convert (type, value);
4806 /* Ignore any integer overflow caused by the cast. */
4807 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4809 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4811 if (!TREE_OVERFLOW (value))
4813 /* Avoid clobbering a shared constant. */
4814 value = copy_node (value);
4815 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4818 else if (TREE_OVERFLOW (value))
4819 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4820 value = build_int_cst_wide (TREE_TYPE (value),
4821 TREE_INT_CST_LOW (value),
4822 TREE_INT_CST_HIGH (value));
4826 /* Don't let a cast be an lvalue. */
4827 if (value == expr)
4828 value = non_lvalue_loc (loc, value);
4830 /* Don't allow the results of casting to floating-point or complex
4831 types be confused with actual constants, or casts involving
4832 integer and pointer types other than direct integer-to-integer
4833 and integer-to-pointer be confused with integer constant
4834 expressions and null pointer constants. */
4835 if (TREE_CODE (value) == REAL_CST
4836 || TREE_CODE (value) == COMPLEX_CST
4837 || (TREE_CODE (value) == INTEGER_CST
4838 && !((TREE_CODE (expr) == INTEGER_CST
4839 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4840 || TREE_CODE (expr) == REAL_CST
4841 || TREE_CODE (expr) == COMPLEX_CST)))
4842 value = build1 (NOP_EXPR, type, value);
4844 if (CAN_HAVE_LOCATION_P (value))
4845 SET_EXPR_LOCATION (value, loc);
4846 return value;
4849 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4850 location of the open paren of the cast, or the position of the cast
4851 expr. */
4852 tree
4853 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4855 tree type;
4856 tree type_expr = NULL_TREE;
4857 bool type_expr_const = true;
4858 tree ret;
4859 int saved_wsp = warn_strict_prototypes;
4861 /* This avoids warnings about unprototyped casts on
4862 integers. E.g. "#define SIG_DFL (void(*)())0". */
4863 if (TREE_CODE (expr) == INTEGER_CST)
4864 warn_strict_prototypes = 0;
4865 type = groktypename (type_name, &type_expr, &type_expr_const);
4866 warn_strict_prototypes = saved_wsp;
4868 ret = build_c_cast (loc, type, expr);
4869 if (type_expr)
4871 bool inner_expr_const = true;
4872 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
4873 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4874 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
4875 && inner_expr_const);
4876 SET_EXPR_LOCATION (ret, loc);
4879 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4880 SET_EXPR_LOCATION (ret, loc);
4882 /* C++ does not permits types to be defined in a cast, but it
4883 allows references to incomplete types. */
4884 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4885 warning_at (loc, OPT_Wc___compat,
4886 "defining a type in a cast is invalid in C++");
4888 return ret;
4891 /* Build an assignment expression of lvalue LHS from value RHS.
4892 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4893 may differ from TREE_TYPE (LHS) for an enum bitfield.
4894 MODIFYCODE is the code for a binary operator that we use
4895 to combine the old value of LHS with RHS to get the new value.
4896 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4897 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4898 which may differ from TREE_TYPE (RHS) for an enum value.
4900 LOCATION is the location of the MODIFYCODE operator.
4901 RHS_LOC is the location of the RHS. */
4903 tree
4904 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4905 enum tree_code modifycode,
4906 location_t rhs_loc, tree rhs, tree rhs_origtype)
4908 tree result;
4909 tree newrhs;
4910 tree rhs_semantic_type = NULL_TREE;
4911 tree lhstype = TREE_TYPE (lhs);
4912 tree olhstype = lhstype;
4913 bool npc;
4915 /* Types that aren't fully specified cannot be used in assignments. */
4916 lhs = require_complete_type (lhs);
4918 /* Avoid duplicate error messages from operands that had errors. */
4919 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4920 return error_mark_node;
4922 /* For ObjC properties, defer this check. */
4923 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4924 return error_mark_node;
4926 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4928 rhs_semantic_type = TREE_TYPE (rhs);
4929 rhs = TREE_OPERAND (rhs, 0);
4932 newrhs = rhs;
4934 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4936 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4937 lhs_origtype, modifycode, rhs_loc, rhs,
4938 rhs_origtype);
4939 if (inner == error_mark_node)
4940 return error_mark_node;
4941 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4942 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4943 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4944 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4945 protected_set_expr_location (result, location);
4946 return result;
4949 /* If a binary op has been requested, combine the old LHS value with the RHS
4950 producing the value we should actually store into the LHS. */
4952 if (modifycode != NOP_EXPR)
4954 lhs = c_fully_fold (lhs, false, NULL);
4955 lhs = stabilize_reference (lhs);
4956 newrhs = build_binary_op (location,
4957 modifycode, lhs, rhs, 1);
4959 /* The original type of the right hand side is no longer
4960 meaningful. */
4961 rhs_origtype = NULL_TREE;
4964 if (c_dialect_objc ())
4966 /* Check if we are modifying an Objective-C property reference;
4967 if so, we need to generate setter calls. */
4968 result = objc_maybe_build_modify_expr (lhs, newrhs);
4969 if (result)
4970 return result;
4972 /* Else, do the check that we postponed for Objective-C. */
4973 if (!lvalue_or_else (location, lhs, lv_assign))
4974 return error_mark_node;
4977 /* Give an error for storing in something that is 'const'. */
4979 if (TYPE_READONLY (lhstype)
4980 || ((TREE_CODE (lhstype) == RECORD_TYPE
4981 || TREE_CODE (lhstype) == UNION_TYPE)
4982 && C_TYPE_FIELDS_READONLY (lhstype)))
4984 readonly_error (lhs, lv_assign);
4985 return error_mark_node;
4987 else if (TREE_READONLY (lhs))
4988 readonly_warning (lhs, lv_assign);
4990 /* If storing into a structure or union member,
4991 it has probably been given type `int'.
4992 Compute the type that would go with
4993 the actual amount of storage the member occupies. */
4995 if (TREE_CODE (lhs) == COMPONENT_REF
4996 && (TREE_CODE (lhstype) == INTEGER_TYPE
4997 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4998 || TREE_CODE (lhstype) == REAL_TYPE
4999 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5000 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5002 /* If storing in a field that is in actuality a short or narrower than one,
5003 we must store in the field in its actual type. */
5005 if (lhstype != TREE_TYPE (lhs))
5007 lhs = copy_node (lhs);
5008 TREE_TYPE (lhs) = lhstype;
5011 /* Issue -Wc++-compat warnings about an assignment to an enum type
5012 when LHS does not have its original type. This happens for,
5013 e.g., an enum bitfield in a struct. */
5014 if (warn_cxx_compat
5015 && lhs_origtype != NULL_TREE
5016 && lhs_origtype != lhstype
5017 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5019 tree checktype = (rhs_origtype != NULL_TREE
5020 ? rhs_origtype
5021 : TREE_TYPE (rhs));
5022 if (checktype != error_mark_node
5023 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
5024 warning_at (location, OPT_Wc___compat,
5025 "enum conversion in assignment is invalid in C++");
5028 /* Convert new value to destination type. Fold it first, then
5029 restore any excess precision information, for the sake of
5030 conversion warnings. */
5032 npc = null_pointer_constant_p (newrhs);
5033 newrhs = c_fully_fold (newrhs, false, NULL);
5034 if (rhs_semantic_type)
5035 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5036 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
5037 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
5038 if (TREE_CODE (newrhs) == ERROR_MARK)
5039 return error_mark_node;
5041 /* Emit ObjC write barrier, if necessary. */
5042 if (c_dialect_objc () && flag_objc_gc)
5044 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5045 if (result)
5047 protected_set_expr_location (result, location);
5048 return result;
5052 /* Scan operands. */
5054 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5055 TREE_SIDE_EFFECTS (result) = 1;
5056 protected_set_expr_location (result, location);
5058 /* If we got the LHS in a different type for storing in,
5059 convert the result back to the nominal type of LHS
5060 so that the value we return always has the same type
5061 as the LHS argument. */
5063 if (olhstype == TREE_TYPE (result))
5064 return result;
5066 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5067 ic_assign, false, NULL_TREE, NULL_TREE, 0);
5068 protected_set_expr_location (result, location);
5069 return result;
5072 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5073 This is used to implement -fplan9-extensions. */
5075 static bool
5076 find_anonymous_field_with_type (tree struct_type, tree type)
5078 tree field;
5079 bool found;
5081 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5082 || TREE_CODE (struct_type) == UNION_TYPE);
5083 found = false;
5084 for (field = TYPE_FIELDS (struct_type);
5085 field != NULL_TREE;
5086 field = TREE_CHAIN (field))
5088 if (DECL_NAME (field) == NULL
5089 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5091 if (found)
5092 return false;
5093 found = true;
5095 else if (DECL_NAME (field) == NULL
5096 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5097 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5098 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5100 if (found)
5101 return false;
5102 found = true;
5105 return found;
5108 /* RHS is an expression whose type is pointer to struct. If there is
5109 an anonymous field in RHS with type TYPE, then return a pointer to
5110 that field in RHS. This is used with -fplan9-extensions. This
5111 returns NULL if no conversion could be found. */
5113 static tree
5114 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5116 tree rhs_struct_type, lhs_main_type;
5117 tree field, found_field;
5118 bool found_sub_field;
5119 tree ret;
5121 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5122 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5123 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5124 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5126 gcc_assert (POINTER_TYPE_P (type));
5127 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5129 found_field = NULL_TREE;
5130 found_sub_field = false;
5131 for (field = TYPE_FIELDS (rhs_struct_type);
5132 field != NULL_TREE;
5133 field = TREE_CHAIN (field))
5135 if (DECL_NAME (field) != NULL_TREE
5136 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5137 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5138 continue;
5139 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5141 if (found_field != NULL_TREE)
5142 return NULL_TREE;
5143 found_field = field;
5145 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5146 lhs_main_type))
5148 if (found_field != NULL_TREE)
5149 return NULL_TREE;
5150 found_field = field;
5151 found_sub_field = true;
5155 if (found_field == NULL_TREE)
5156 return NULL_TREE;
5158 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5159 build_fold_indirect_ref (rhs), found_field,
5160 NULL_TREE);
5161 ret = build_fold_addr_expr_loc (location, ret);
5163 if (found_sub_field)
5165 ret = convert_to_anonymous_field (location, type, ret);
5166 gcc_assert (ret != NULL_TREE);
5169 return ret;
5172 /* Convert value RHS to type TYPE as preparation for an assignment to
5173 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5174 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5175 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5176 constant before any folding.
5177 The real work of conversion is done by `convert'.
5178 The purpose of this function is to generate error messages
5179 for assignments that are not allowed in C.
5180 ERRTYPE says whether it is argument passing, assignment,
5181 initialization or return.
5183 LOCATION is the location of the RHS.
5184 FUNCTION is a tree for the function being called.
5185 PARMNUM is the number of the argument, for printing in error messages. */
5187 static tree
5188 convert_for_assignment (location_t location, tree type, tree rhs,
5189 tree origtype, enum impl_conv errtype,
5190 bool null_pointer_constant, tree fundecl,
5191 tree function, int parmnum)
5193 enum tree_code codel = TREE_CODE (type);
5194 tree orig_rhs = rhs;
5195 tree rhstype;
5196 enum tree_code coder;
5197 tree rname = NULL_TREE;
5198 bool objc_ok = false;
5200 if (errtype == ic_argpass)
5202 tree selector;
5203 /* Change pointer to function to the function itself for
5204 diagnostics. */
5205 if (TREE_CODE (function) == ADDR_EXPR
5206 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5207 function = TREE_OPERAND (function, 0);
5209 /* Handle an ObjC selector specially for diagnostics. */
5210 selector = objc_message_selector ();
5211 rname = function;
5212 if (selector && parmnum > 2)
5214 rname = selector;
5215 parmnum -= 2;
5219 /* This macro is used to emit diagnostics to ensure that all format
5220 strings are complete sentences, visible to gettext and checked at
5221 compile time. */
5222 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5223 do { \
5224 switch (errtype) \
5226 case ic_argpass: \
5227 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5228 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5229 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5230 "expected %qT but argument is of type %qT", \
5231 type, rhstype); \
5232 break; \
5233 case ic_assign: \
5234 pedwarn (LOCATION, OPT, AS); \
5235 break; \
5236 case ic_init: \
5237 pedwarn_init (LOCATION, OPT, IN); \
5238 break; \
5239 case ic_return: \
5240 pedwarn (LOCATION, OPT, RE); \
5241 break; \
5242 default: \
5243 gcc_unreachable (); \
5245 } while (0)
5247 /* This macro is used to emit diagnostics to ensure that all format
5248 strings are complete sentences, visible to gettext and checked at
5249 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5250 extra parameter to enumerate qualifiers. */
5252 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5253 do { \
5254 switch (errtype) \
5256 case ic_argpass: \
5257 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5258 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5259 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5260 "expected %qT but argument is of type %qT", \
5261 type, rhstype); \
5262 break; \
5263 case ic_assign: \
5264 pedwarn (LOCATION, OPT, AS, QUALS); \
5265 break; \
5266 case ic_init: \
5267 pedwarn (LOCATION, OPT, IN, QUALS); \
5268 break; \
5269 case ic_return: \
5270 pedwarn (LOCATION, OPT, RE, QUALS); \
5271 break; \
5272 default: \
5273 gcc_unreachable (); \
5275 } while (0)
5277 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5278 rhs = TREE_OPERAND (rhs, 0);
5280 rhstype = TREE_TYPE (rhs);
5281 coder = TREE_CODE (rhstype);
5283 if (coder == ERROR_MARK)
5284 return error_mark_node;
5286 if (c_dialect_objc ())
5288 int parmno;
5290 switch (errtype)
5292 case ic_return:
5293 parmno = 0;
5294 break;
5296 case ic_assign:
5297 parmno = -1;
5298 break;
5300 case ic_init:
5301 parmno = -2;
5302 break;
5304 default:
5305 parmno = parmnum;
5306 break;
5309 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5312 if (warn_cxx_compat)
5314 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5315 if (checktype != error_mark_node
5316 && TREE_CODE (type) == ENUMERAL_TYPE
5317 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5319 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5320 G_("enum conversion when passing argument "
5321 "%d of %qE is invalid in C++"),
5322 G_("enum conversion in assignment is "
5323 "invalid in C++"),
5324 G_("enum conversion in initialization is "
5325 "invalid in C++"),
5326 G_("enum conversion in return is "
5327 "invalid in C++"));
5331 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5332 return rhs;
5334 if (coder == VOID_TYPE)
5336 /* Except for passing an argument to an unprototyped function,
5337 this is a constraint violation. When passing an argument to
5338 an unprototyped function, it is compile-time undefined;
5339 making it a constraint in that case was rejected in
5340 DR#252. */
5341 error_at (location, "void value not ignored as it ought to be");
5342 return error_mark_node;
5344 rhs = require_complete_type (rhs);
5345 if (rhs == error_mark_node)
5346 return error_mark_node;
5347 /* A type converts to a reference to it.
5348 This code doesn't fully support references, it's just for the
5349 special case of va_start and va_copy. */
5350 if (codel == REFERENCE_TYPE
5351 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5353 if (!lvalue_p (rhs))
5355 error_at (location, "cannot pass rvalue to reference parameter");
5356 return error_mark_node;
5358 if (!c_mark_addressable (rhs))
5359 return error_mark_node;
5360 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5361 SET_EXPR_LOCATION (rhs, location);
5363 /* We already know that these two types are compatible, but they
5364 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5365 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5366 likely to be va_list, a typedef to __builtin_va_list, which
5367 is different enough that it will cause problems later. */
5368 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5370 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5371 SET_EXPR_LOCATION (rhs, location);
5374 rhs = build1 (NOP_EXPR, type, rhs);
5375 SET_EXPR_LOCATION (rhs, location);
5376 return rhs;
5378 /* Some types can interconvert without explicit casts. */
5379 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5380 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5381 return convert (type, rhs);
5382 /* Arithmetic types all interconvert, and enum is treated like int. */
5383 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5384 || codel == FIXED_POINT_TYPE
5385 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5386 || codel == BOOLEAN_TYPE)
5387 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5388 || coder == FIXED_POINT_TYPE
5389 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5390 || coder == BOOLEAN_TYPE))
5392 tree ret;
5393 bool save = in_late_binary_op;
5394 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5395 in_late_binary_op = true;
5396 ret = convert_and_check (type, orig_rhs);
5397 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5398 in_late_binary_op = save;
5399 return ret;
5402 /* Aggregates in different TUs might need conversion. */
5403 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5404 && codel == coder
5405 && comptypes (type, rhstype))
5406 return convert_and_check (type, rhs);
5408 /* Conversion to a transparent union or record from its member types.
5409 This applies only to function arguments. */
5410 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5411 && TYPE_TRANSPARENT_AGGR (type))
5412 && errtype == ic_argpass)
5414 tree memb, marginal_memb = NULL_TREE;
5416 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5418 tree memb_type = TREE_TYPE (memb);
5420 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5421 TYPE_MAIN_VARIANT (rhstype)))
5422 break;
5424 if (TREE_CODE (memb_type) != POINTER_TYPE)
5425 continue;
5427 if (coder == POINTER_TYPE)
5429 tree ttl = TREE_TYPE (memb_type);
5430 tree ttr = TREE_TYPE (rhstype);
5432 /* Any non-function converts to a [const][volatile] void *
5433 and vice versa; otherwise, targets must be the same.
5434 Meanwhile, the lhs target must have all the qualifiers of
5435 the rhs. */
5436 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5437 || comp_target_types (location, memb_type, rhstype))
5439 /* If this type won't generate any warnings, use it. */
5440 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5441 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5442 && TREE_CODE (ttl) == FUNCTION_TYPE)
5443 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5444 == TYPE_QUALS (ttr))
5445 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5446 == TYPE_QUALS (ttl))))
5447 break;
5449 /* Keep looking for a better type, but remember this one. */
5450 if (!marginal_memb)
5451 marginal_memb = memb;
5455 /* Can convert integer zero to any pointer type. */
5456 if (null_pointer_constant)
5458 rhs = null_pointer_node;
5459 break;
5463 if (memb || marginal_memb)
5465 if (!memb)
5467 /* We have only a marginally acceptable member type;
5468 it needs a warning. */
5469 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5470 tree ttr = TREE_TYPE (rhstype);
5472 /* Const and volatile mean something different for function
5473 types, so the usual warnings are not appropriate. */
5474 if (TREE_CODE (ttr) == FUNCTION_TYPE
5475 && TREE_CODE (ttl) == FUNCTION_TYPE)
5477 /* Because const and volatile on functions are
5478 restrictions that say the function will not do
5479 certain things, it is okay to use a const or volatile
5480 function where an ordinary one is wanted, but not
5481 vice-versa. */
5482 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5483 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5484 WARN_FOR_QUALIFIERS (location, 0,
5485 G_("passing argument %d of %qE "
5486 "makes %q#v qualified function "
5487 "pointer from unqualified"),
5488 G_("assignment makes %q#v qualified "
5489 "function pointer from "
5490 "unqualified"),
5491 G_("initialization makes %q#v qualified "
5492 "function pointer from "
5493 "unqualified"),
5494 G_("return makes %q#v qualified function "
5495 "pointer from unqualified"),
5496 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5498 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5499 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5500 WARN_FOR_QUALIFIERS (location, 0,
5501 G_("passing argument %d of %qE discards "
5502 "%qv qualifier from pointer target type"),
5503 G_("assignment discards %qv qualifier "
5504 "from pointer target type"),
5505 G_("initialization discards %qv qualifier "
5506 "from pointer target type"),
5507 G_("return discards %qv qualifier from "
5508 "pointer target type"),
5509 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5511 memb = marginal_memb;
5514 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5515 pedwarn (location, OPT_pedantic,
5516 "ISO C prohibits argument conversion to union type");
5518 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5519 return build_constructor_single (type, memb, rhs);
5523 /* Conversions among pointers */
5524 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5525 && (coder == codel))
5527 tree ttl = TREE_TYPE (type);
5528 tree ttr = TREE_TYPE (rhstype);
5529 tree mvl = ttl;
5530 tree mvr = ttr;
5531 bool is_opaque_pointer;
5532 int target_cmp = 0; /* Cache comp_target_types () result. */
5533 addr_space_t asl;
5534 addr_space_t asr;
5536 if (TREE_CODE (mvl) != ARRAY_TYPE)
5537 mvl = TYPE_MAIN_VARIANT (mvl);
5538 if (TREE_CODE (mvr) != ARRAY_TYPE)
5539 mvr = TYPE_MAIN_VARIANT (mvr);
5540 /* Opaque pointers are treated like void pointers. */
5541 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5543 /* The Plan 9 compiler permits a pointer to a struct to be
5544 automatically converted into a pointer to an anonymous field
5545 within the struct. */
5546 if (flag_plan9_extensions
5547 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5548 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5549 && mvl != mvr)
5551 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5552 if (new_rhs != NULL_TREE)
5554 rhs = new_rhs;
5555 rhstype = TREE_TYPE (rhs);
5556 coder = TREE_CODE (rhstype);
5557 ttr = TREE_TYPE (rhstype);
5558 mvr = TYPE_MAIN_VARIANT (ttr);
5562 /* C++ does not allow the implicit conversion void* -> T*. However,
5563 for the purpose of reducing the number of false positives, we
5564 tolerate the special case of
5566 int *p = NULL;
5568 where NULL is typically defined in C to be '(void *) 0'. */
5569 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5570 warning_at (location, OPT_Wc___compat,
5571 "request for implicit conversion "
5572 "from %qT to %qT not permitted in C++", rhstype, type);
5574 /* See if the pointers point to incompatible address spaces. */
5575 asl = TYPE_ADDR_SPACE (ttl);
5576 asr = TYPE_ADDR_SPACE (ttr);
5577 if (!null_pointer_constant_p (rhs)
5578 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5580 switch (errtype)
5582 case ic_argpass:
5583 error_at (location, "passing argument %d of %qE from pointer to "
5584 "non-enclosed address space", parmnum, rname);
5585 break;
5586 case ic_assign:
5587 error_at (location, "assignment from pointer to "
5588 "non-enclosed address space");
5589 break;
5590 case ic_init:
5591 error_at (location, "initialization from pointer to "
5592 "non-enclosed address space");
5593 break;
5594 case ic_return:
5595 error_at (location, "return from pointer to "
5596 "non-enclosed address space");
5597 break;
5598 default:
5599 gcc_unreachable ();
5601 return error_mark_node;
5604 /* Check if the right-hand side has a format attribute but the
5605 left-hand side doesn't. */
5606 if (warn_missing_format_attribute
5607 && check_missing_format_attribute (type, rhstype))
5609 switch (errtype)
5611 case ic_argpass:
5612 warning_at (location, OPT_Wmissing_format_attribute,
5613 "argument %d of %qE might be "
5614 "a candidate for a format attribute",
5615 parmnum, rname);
5616 break;
5617 case ic_assign:
5618 warning_at (location, OPT_Wmissing_format_attribute,
5619 "assignment left-hand side might be "
5620 "a candidate for a format attribute");
5621 break;
5622 case ic_init:
5623 warning_at (location, OPT_Wmissing_format_attribute,
5624 "initialization left-hand side might be "
5625 "a candidate for a format attribute");
5626 break;
5627 case ic_return:
5628 warning_at (location, OPT_Wmissing_format_attribute,
5629 "return type might be "
5630 "a candidate for a format attribute");
5631 break;
5632 default:
5633 gcc_unreachable ();
5637 /* Any non-function converts to a [const][volatile] void *
5638 and vice versa; otherwise, targets must be the same.
5639 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5640 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5641 || (target_cmp = comp_target_types (location, type, rhstype))
5642 || is_opaque_pointer
5643 || (c_common_unsigned_type (mvl)
5644 == c_common_unsigned_type (mvr)))
5646 if (pedantic
5647 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5649 (VOID_TYPE_P (ttr)
5650 && !null_pointer_constant
5651 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5652 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5653 G_("ISO C forbids passing argument %d of "
5654 "%qE between function pointer "
5655 "and %<void *%>"),
5656 G_("ISO C forbids assignment between "
5657 "function pointer and %<void *%>"),
5658 G_("ISO C forbids initialization between "
5659 "function pointer and %<void *%>"),
5660 G_("ISO C forbids return between function "
5661 "pointer and %<void *%>"));
5662 /* Const and volatile mean something different for function types,
5663 so the usual warnings are not appropriate. */
5664 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5665 && TREE_CODE (ttl) != FUNCTION_TYPE)
5667 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5668 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5670 WARN_FOR_QUALIFIERS (location, 0,
5671 G_("passing argument %d of %qE discards "
5672 "%qv qualifier from pointer target type"),
5673 G_("assignment discards %qv qualifier "
5674 "from pointer target type"),
5675 G_("initialization discards %qv qualifier "
5676 "from pointer target type"),
5677 G_("return discards %qv qualifier from "
5678 "pointer target type"),
5679 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5681 /* If this is not a case of ignoring a mismatch in signedness,
5682 no warning. */
5683 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5684 || target_cmp)
5686 /* If there is a mismatch, do warn. */
5687 else if (warn_pointer_sign)
5688 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5689 G_("pointer targets in passing argument "
5690 "%d of %qE differ in signedness"),
5691 G_("pointer targets in assignment "
5692 "differ in signedness"),
5693 G_("pointer targets in initialization "
5694 "differ in signedness"),
5695 G_("pointer targets in return differ "
5696 "in signedness"));
5698 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5699 && TREE_CODE (ttr) == FUNCTION_TYPE)
5701 /* Because const and volatile on functions are restrictions
5702 that say the function will not do certain things,
5703 it is okay to use a const or volatile function
5704 where an ordinary one is wanted, but not vice-versa. */
5705 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5706 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5707 WARN_FOR_QUALIFIERS (location, 0,
5708 G_("passing argument %d of %qE makes "
5709 "%q#v qualified function pointer "
5710 "from unqualified"),
5711 G_("assignment makes %q#v qualified function "
5712 "pointer from unqualified"),
5713 G_("initialization makes %q#v qualified "
5714 "function pointer from unqualified"),
5715 G_("return makes %q#v qualified function "
5716 "pointer from unqualified"),
5717 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5720 else
5721 /* Avoid warning about the volatile ObjC EH puts on decls. */
5722 if (!objc_ok)
5723 WARN_FOR_ASSIGNMENT (location, 0,
5724 G_("passing argument %d of %qE from "
5725 "incompatible pointer type"),
5726 G_("assignment from incompatible pointer type"),
5727 G_("initialization from incompatible "
5728 "pointer type"),
5729 G_("return from incompatible pointer type"));
5731 return convert (type, rhs);
5733 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5735 /* ??? This should not be an error when inlining calls to
5736 unprototyped functions. */
5737 error_at (location, "invalid use of non-lvalue array");
5738 return error_mark_node;
5740 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5742 /* An explicit constant 0 can convert to a pointer,
5743 or one that results from arithmetic, even including
5744 a cast to integer type. */
5745 if (!null_pointer_constant)
5746 WARN_FOR_ASSIGNMENT (location, 0,
5747 G_("passing argument %d of %qE makes "
5748 "pointer from integer without a cast"),
5749 G_("assignment makes pointer from integer "
5750 "without a cast"),
5751 G_("initialization makes pointer from "
5752 "integer without a cast"),
5753 G_("return makes pointer from integer "
5754 "without a cast"));
5756 return convert (type, rhs);
5758 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5760 WARN_FOR_ASSIGNMENT (location, 0,
5761 G_("passing argument %d of %qE makes integer "
5762 "from pointer without a cast"),
5763 G_("assignment makes integer from pointer "
5764 "without a cast"),
5765 G_("initialization makes integer from pointer "
5766 "without a cast"),
5767 G_("return makes integer from pointer "
5768 "without a cast"));
5769 return convert (type, rhs);
5771 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5773 tree ret;
5774 bool save = in_late_binary_op;
5775 in_late_binary_op = true;
5776 ret = convert (type, rhs);
5777 in_late_binary_op = save;
5778 return ret;
5781 switch (errtype)
5783 case ic_argpass:
5784 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5785 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5786 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5787 "expected %qT but argument is of type %qT", type, rhstype);
5788 break;
5789 case ic_assign:
5790 error_at (location, "incompatible types when assigning to type %qT from "
5791 "type %qT", type, rhstype);
5792 break;
5793 case ic_init:
5794 error_at (location,
5795 "incompatible types when initializing type %qT using type %qT",
5796 type, rhstype);
5797 break;
5798 case ic_return:
5799 error_at (location,
5800 "incompatible types when returning type %qT but %qT was "
5801 "expected", rhstype, type);
5802 break;
5803 default:
5804 gcc_unreachable ();
5807 return error_mark_node;
5810 /* If VALUE is a compound expr all of whose expressions are constant, then
5811 return its value. Otherwise, return error_mark_node.
5813 This is for handling COMPOUND_EXPRs as initializer elements
5814 which is allowed with a warning when -pedantic is specified. */
5816 static tree
5817 valid_compound_expr_initializer (tree value, tree endtype)
5819 if (TREE_CODE (value) == COMPOUND_EXPR)
5821 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5822 == error_mark_node)
5823 return error_mark_node;
5824 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5825 endtype);
5827 else if (!initializer_constant_valid_p (value, endtype))
5828 return error_mark_node;
5829 else
5830 return value;
5833 /* Perform appropriate conversions on the initial value of a variable,
5834 store it in the declaration DECL,
5835 and print any error messages that are appropriate.
5836 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5837 If the init is invalid, store an ERROR_MARK.
5839 INIT_LOC is the location of the initial value. */
5841 void
5842 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5844 tree value, type;
5845 bool npc = false;
5847 /* If variable's type was invalidly declared, just ignore it. */
5849 type = TREE_TYPE (decl);
5850 if (TREE_CODE (type) == ERROR_MARK)
5851 return;
5853 /* Digest the specified initializer into an expression. */
5855 if (init)
5856 npc = null_pointer_constant_p (init);
5857 value = digest_init (init_loc, type, init, origtype, npc,
5858 true, TREE_STATIC (decl));
5860 /* Store the expression if valid; else report error. */
5862 if (!in_system_header
5863 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5864 warning (OPT_Wtraditional, "traditional C rejects automatic "
5865 "aggregate initialization");
5867 DECL_INITIAL (decl) = value;
5869 /* ANSI wants warnings about out-of-range constant initializers. */
5870 STRIP_TYPE_NOPS (value);
5871 if (TREE_STATIC (decl))
5872 constant_expression_warning (value);
5874 /* Check if we need to set array size from compound literal size. */
5875 if (TREE_CODE (type) == ARRAY_TYPE
5876 && TYPE_DOMAIN (type) == 0
5877 && value != error_mark_node)
5879 tree inside_init = init;
5881 STRIP_TYPE_NOPS (inside_init);
5882 inside_init = fold (inside_init);
5884 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5886 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5888 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5890 /* For int foo[] = (int [3]){1}; we need to set array size
5891 now since later on array initializer will be just the
5892 brace enclosed list of the compound literal. */
5893 tree etype = strip_array_types (TREE_TYPE (decl));
5894 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5895 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5896 layout_type (type);
5897 layout_decl (cldecl, 0);
5898 TREE_TYPE (decl)
5899 = c_build_qualified_type (type, TYPE_QUALS (etype));
5905 /* Methods for storing and printing names for error messages. */
5907 /* Implement a spelling stack that allows components of a name to be pushed
5908 and popped. Each element on the stack is this structure. */
5910 struct spelling
5912 int kind;
5913 union
5915 unsigned HOST_WIDE_INT i;
5916 const char *s;
5917 } u;
5920 #define SPELLING_STRING 1
5921 #define SPELLING_MEMBER 2
5922 #define SPELLING_BOUNDS 3
5924 static struct spelling *spelling; /* Next stack element (unused). */
5925 static struct spelling *spelling_base; /* Spelling stack base. */
5926 static int spelling_size; /* Size of the spelling stack. */
5928 /* Macros to save and restore the spelling stack around push_... functions.
5929 Alternative to SAVE_SPELLING_STACK. */
5931 #define SPELLING_DEPTH() (spelling - spelling_base)
5932 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5934 /* Push an element on the spelling stack with type KIND and assign VALUE
5935 to MEMBER. */
5937 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5939 int depth = SPELLING_DEPTH (); \
5941 if (depth >= spelling_size) \
5943 spelling_size += 10; \
5944 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5945 spelling_size); \
5946 RESTORE_SPELLING_DEPTH (depth); \
5949 spelling->kind = (KIND); \
5950 spelling->MEMBER = (VALUE); \
5951 spelling++; \
5954 /* Push STRING on the stack. Printed literally. */
5956 static void
5957 push_string (const char *string)
5959 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5962 /* Push a member name on the stack. Printed as '.' STRING. */
5964 static void
5965 push_member_name (tree decl)
5967 const char *const string
5968 = (DECL_NAME (decl)
5969 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5970 : _("<anonymous>"));
5971 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5974 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5976 static void
5977 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5979 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5982 /* Compute the maximum size in bytes of the printed spelling. */
5984 static int
5985 spelling_length (void)
5987 int size = 0;
5988 struct spelling *p;
5990 for (p = spelling_base; p < spelling; p++)
5992 if (p->kind == SPELLING_BOUNDS)
5993 size += 25;
5994 else
5995 size += strlen (p->u.s) + 1;
5998 return size;
6001 /* Print the spelling to BUFFER and return it. */
6003 static char *
6004 print_spelling (char *buffer)
6006 char *d = buffer;
6007 struct spelling *p;
6009 for (p = spelling_base; p < spelling; p++)
6010 if (p->kind == SPELLING_BOUNDS)
6012 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
6013 d += strlen (d);
6015 else
6017 const char *s;
6018 if (p->kind == SPELLING_MEMBER)
6019 *d++ = '.';
6020 for (s = p->u.s; (*d = *s++); d++)
6023 *d++ = '\0';
6024 return buffer;
6027 /* Issue an error message for a bad initializer component.
6028 GMSGID identifies the message.
6029 The component name is taken from the spelling stack. */
6031 void
6032 error_init (const char *gmsgid)
6034 char *ofwhat;
6036 /* The gmsgid may be a format string with %< and %>. */
6037 error (gmsgid);
6038 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6039 if (*ofwhat)
6040 error ("(near initialization for %qs)", ofwhat);
6043 /* Issue a pedantic warning for a bad initializer component. OPT is
6044 the option OPT_* (from options.h) controlling this warning or 0 if
6045 it is unconditionally given. GMSGID identifies the message. The
6046 component name is taken from the spelling stack. */
6048 void
6049 pedwarn_init (location_t location, int opt, const char *gmsgid)
6051 char *ofwhat;
6053 /* The gmsgid may be a format string with %< and %>. */
6054 pedwarn (location, opt, gmsgid);
6055 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6056 if (*ofwhat)
6057 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6060 /* Issue a warning for a bad initializer component.
6062 OPT is the OPT_W* value corresponding to the warning option that
6063 controls this warning. GMSGID identifies the message. The
6064 component name is taken from the spelling stack. */
6066 static void
6067 warning_init (int opt, const char *gmsgid)
6069 char *ofwhat;
6071 /* The gmsgid may be a format string with %< and %>. */
6072 warning (opt, gmsgid);
6073 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6074 if (*ofwhat)
6075 warning (opt, "(near initialization for %qs)", ofwhat);
6078 /* If TYPE is an array type and EXPR is a parenthesized string
6079 constant, warn if pedantic that EXPR is being used to initialize an
6080 object of type TYPE. */
6082 void
6083 maybe_warn_string_init (tree type, struct c_expr expr)
6085 if (pedantic
6086 && TREE_CODE (type) == ARRAY_TYPE
6087 && TREE_CODE (expr.value) == STRING_CST
6088 && expr.original_code != STRING_CST)
6089 pedwarn_init (input_location, OPT_pedantic,
6090 "array initialized from parenthesized string constant");
6093 /* Digest the parser output INIT as an initializer for type TYPE.
6094 Return a C expression of type TYPE to represent the initial value.
6096 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6098 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6100 If INIT is a string constant, STRICT_STRING is true if it is
6101 unparenthesized or we should not warn here for it being parenthesized.
6102 For other types of INIT, STRICT_STRING is not used.
6104 INIT_LOC is the location of the INIT.
6106 REQUIRE_CONSTANT requests an error if non-constant initializers or
6107 elements are seen. */
6109 static tree
6110 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6111 bool null_pointer_constant, bool strict_string,
6112 int require_constant)
6114 enum tree_code code = TREE_CODE (type);
6115 tree inside_init = init;
6116 tree semantic_type = NULL_TREE;
6117 bool maybe_const = true;
6119 if (type == error_mark_node
6120 || !init
6121 || init == error_mark_node
6122 || TREE_TYPE (init) == error_mark_node)
6123 return error_mark_node;
6125 STRIP_TYPE_NOPS (inside_init);
6127 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6129 semantic_type = TREE_TYPE (inside_init);
6130 inside_init = TREE_OPERAND (inside_init, 0);
6132 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6133 inside_init = decl_constant_value_for_optimization (inside_init);
6135 /* Initialization of an array of chars from a string constant
6136 optionally enclosed in braces. */
6138 if (code == ARRAY_TYPE && inside_init
6139 && TREE_CODE (inside_init) == STRING_CST)
6141 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6142 /* Note that an array could be both an array of character type
6143 and an array of wchar_t if wchar_t is signed char or unsigned
6144 char. */
6145 bool char_array = (typ1 == char_type_node
6146 || typ1 == signed_char_type_node
6147 || typ1 == unsigned_char_type_node);
6148 bool wchar_array = !!comptypes (typ1, wchar_type_node);
6149 bool char16_array = !!comptypes (typ1, char16_type_node);
6150 bool char32_array = !!comptypes (typ1, char32_type_node);
6152 if (char_array || wchar_array || char16_array || char32_array)
6154 struct c_expr expr;
6155 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6156 expr.value = inside_init;
6157 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6158 expr.original_type = NULL;
6159 maybe_warn_string_init (type, expr);
6161 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6162 pedwarn_init (init_loc, OPT_pedantic,
6163 "initialization of a flexible array member");
6165 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6166 TYPE_MAIN_VARIANT (type)))
6167 return inside_init;
6169 if (char_array)
6171 if (typ2 != char_type_node)
6173 error_init ("char-array initialized from wide string");
6174 return error_mark_node;
6177 else
6179 if (typ2 == char_type_node)
6181 error_init ("wide character array initialized from non-wide "
6182 "string");
6183 return error_mark_node;
6185 else if (!comptypes(typ1, typ2))
6187 error_init ("wide character array initialized from "
6188 "incompatible wide string");
6189 return error_mark_node;
6193 TREE_TYPE (inside_init) = type;
6194 if (TYPE_DOMAIN (type) != 0
6195 && TYPE_SIZE (type) != 0
6196 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6198 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6200 /* Subtract the size of a single (possibly wide) character
6201 because it's ok to ignore the terminating null char
6202 that is counted in the length of the constant. */
6203 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6204 (len
6205 - (TYPE_PRECISION (typ1)
6206 / BITS_PER_UNIT))))
6207 pedwarn_init (init_loc, 0,
6208 ("initializer-string for array of chars "
6209 "is too long"));
6210 else if (warn_cxx_compat
6211 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6212 warning_at (init_loc, OPT_Wc___compat,
6213 ("initializer-string for array chars "
6214 "is too long for C++"));
6217 return inside_init;
6219 else if (INTEGRAL_TYPE_P (typ1))
6221 error_init ("array of inappropriate type initialized "
6222 "from string constant");
6223 return error_mark_node;
6227 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6228 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6229 below and handle as a constructor. */
6230 if (code == VECTOR_TYPE
6231 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6232 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6233 && TREE_CONSTANT (inside_init))
6235 if (TREE_CODE (inside_init) == VECTOR_CST
6236 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6237 TYPE_MAIN_VARIANT (type)))
6238 return inside_init;
6240 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6242 unsigned HOST_WIDE_INT ix;
6243 tree value;
6244 bool constant_p = true;
6246 /* Iterate through elements and check if all constructor
6247 elements are *_CSTs. */
6248 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6249 if (!CONSTANT_CLASS_P (value))
6251 constant_p = false;
6252 break;
6255 if (constant_p)
6256 return build_vector_from_ctor (type,
6257 CONSTRUCTOR_ELTS (inside_init));
6261 if (warn_sequence_point)
6262 verify_sequence_points (inside_init);
6264 /* Any type can be initialized
6265 from an expression of the same type, optionally with braces. */
6267 if (inside_init && TREE_TYPE (inside_init) != 0
6268 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6269 TYPE_MAIN_VARIANT (type))
6270 || (code == ARRAY_TYPE
6271 && comptypes (TREE_TYPE (inside_init), type))
6272 || (code == VECTOR_TYPE
6273 && comptypes (TREE_TYPE (inside_init), type))
6274 || (code == POINTER_TYPE
6275 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6276 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6277 TREE_TYPE (type)))))
6279 if (code == POINTER_TYPE)
6281 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6283 if (TREE_CODE (inside_init) == STRING_CST
6284 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6285 inside_init = array_to_pointer_conversion
6286 (init_loc, inside_init);
6287 else
6289 error_init ("invalid use of non-lvalue array");
6290 return error_mark_node;
6295 if (code == VECTOR_TYPE)
6296 /* Although the types are compatible, we may require a
6297 conversion. */
6298 inside_init = convert (type, inside_init);
6300 if (require_constant
6301 && (code == VECTOR_TYPE || !flag_isoc99)
6302 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6304 /* As an extension, allow initializing objects with static storage
6305 duration with compound literals (which are then treated just as
6306 the brace enclosed list they contain). Also allow this for
6307 vectors, as we can only assign them with compound literals. */
6308 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6309 inside_init = DECL_INITIAL (decl);
6312 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6313 && TREE_CODE (inside_init) != CONSTRUCTOR)
6315 error_init ("array initialized from non-constant array expression");
6316 return error_mark_node;
6319 /* Compound expressions can only occur here if -pedantic or
6320 -pedantic-errors is specified. In the later case, we always want
6321 an error. In the former case, we simply want a warning. */
6322 if (require_constant && pedantic
6323 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6325 inside_init
6326 = valid_compound_expr_initializer (inside_init,
6327 TREE_TYPE (inside_init));
6328 if (inside_init == error_mark_node)
6329 error_init ("initializer element is not constant");
6330 else
6331 pedwarn_init (init_loc, OPT_pedantic,
6332 "initializer element is not constant");
6333 if (flag_pedantic_errors)
6334 inside_init = error_mark_node;
6336 else if (require_constant
6337 && !initializer_constant_valid_p (inside_init,
6338 TREE_TYPE (inside_init)))
6340 error_init ("initializer element is not constant");
6341 inside_init = error_mark_node;
6343 else if (require_constant && !maybe_const)
6344 pedwarn_init (init_loc, 0,
6345 "initializer element is not a constant expression");
6347 /* Added to enable additional -Wmissing-format-attribute warnings. */
6348 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6349 inside_init = convert_for_assignment (init_loc, type, inside_init,
6350 origtype,
6351 ic_init, null_pointer_constant,
6352 NULL_TREE, NULL_TREE, 0);
6353 return inside_init;
6356 /* Handle scalar types, including conversions. */
6358 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6359 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6360 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6362 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6363 && (TREE_CODE (init) == STRING_CST
6364 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6365 inside_init = init = array_to_pointer_conversion (init_loc, init);
6366 if (semantic_type)
6367 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6368 inside_init);
6369 inside_init
6370 = convert_for_assignment (init_loc, type, inside_init, origtype,
6371 ic_init, null_pointer_constant,
6372 NULL_TREE, NULL_TREE, 0);
6374 /* Check to see if we have already given an error message. */
6375 if (inside_init == error_mark_node)
6377 else if (require_constant && !TREE_CONSTANT (inside_init))
6379 error_init ("initializer element is not constant");
6380 inside_init = error_mark_node;
6382 else if (require_constant
6383 && !initializer_constant_valid_p (inside_init,
6384 TREE_TYPE (inside_init)))
6386 error_init ("initializer element is not computable at load time");
6387 inside_init = error_mark_node;
6389 else if (require_constant && !maybe_const)
6390 pedwarn_init (init_loc, 0,
6391 "initializer element is not a constant expression");
6393 return inside_init;
6396 /* Come here only for records and arrays. */
6398 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6400 error_init ("variable-sized object may not be initialized");
6401 return error_mark_node;
6404 error_init ("invalid initializer");
6405 return error_mark_node;
6408 /* Handle initializers that use braces. */
6410 /* Type of object we are accumulating a constructor for.
6411 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6412 static tree constructor_type;
6414 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6415 left to fill. */
6416 static tree constructor_fields;
6418 /* For an ARRAY_TYPE, this is the specified index
6419 at which to store the next element we get. */
6420 static tree constructor_index;
6422 /* For an ARRAY_TYPE, this is the maximum index. */
6423 static tree constructor_max_index;
6425 /* For a RECORD_TYPE, this is the first field not yet written out. */
6426 static tree constructor_unfilled_fields;
6428 /* For an ARRAY_TYPE, this is the index of the first element
6429 not yet written out. */
6430 static tree constructor_unfilled_index;
6432 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6433 This is so we can generate gaps between fields, when appropriate. */
6434 static tree constructor_bit_index;
6436 /* If we are saving up the elements rather than allocating them,
6437 this is the list of elements so far (in reverse order,
6438 most recent first). */
6439 static VEC(constructor_elt,gc) *constructor_elements;
6441 /* 1 if constructor should be incrementally stored into a constructor chain,
6442 0 if all the elements should be kept in AVL tree. */
6443 static int constructor_incremental;
6445 /* 1 if so far this constructor's elements are all compile-time constants. */
6446 static int constructor_constant;
6448 /* 1 if so far this constructor's elements are all valid address constants. */
6449 static int constructor_simple;
6451 /* 1 if this constructor has an element that cannot be part of a
6452 constant expression. */
6453 static int constructor_nonconst;
6455 /* 1 if this constructor is erroneous so far. */
6456 static int constructor_erroneous;
6458 /* Structure for managing pending initializer elements, organized as an
6459 AVL tree. */
6461 struct init_node
6463 struct init_node *left, *right;
6464 struct init_node *parent;
6465 int balance;
6466 tree purpose;
6467 tree value;
6468 tree origtype;
6471 /* Tree of pending elements at this constructor level.
6472 These are elements encountered out of order
6473 which belong at places we haven't reached yet in actually
6474 writing the output.
6475 Will never hold tree nodes across GC runs. */
6476 static struct init_node *constructor_pending_elts;
6478 /* The SPELLING_DEPTH of this constructor. */
6479 static int constructor_depth;
6481 /* DECL node for which an initializer is being read.
6482 0 means we are reading a constructor expression
6483 such as (struct foo) {...}. */
6484 static tree constructor_decl;
6486 /* Nonzero if this is an initializer for a top-level decl. */
6487 static int constructor_top_level;
6489 /* Nonzero if there were any member designators in this initializer. */
6490 static int constructor_designated;
6492 /* Nesting depth of designator list. */
6493 static int designator_depth;
6495 /* Nonzero if there were diagnosed errors in this designator list. */
6496 static int designator_erroneous;
6499 /* This stack has a level for each implicit or explicit level of
6500 structuring in the initializer, including the outermost one. It
6501 saves the values of most of the variables above. */
6503 struct constructor_range_stack;
6505 struct constructor_stack
6507 struct constructor_stack *next;
6508 tree type;
6509 tree fields;
6510 tree index;
6511 tree max_index;
6512 tree unfilled_index;
6513 tree unfilled_fields;
6514 tree bit_index;
6515 VEC(constructor_elt,gc) *elements;
6516 struct init_node *pending_elts;
6517 int offset;
6518 int depth;
6519 /* If value nonzero, this value should replace the entire
6520 constructor at this level. */
6521 struct c_expr replacement_value;
6522 struct constructor_range_stack *range_stack;
6523 char constant;
6524 char simple;
6525 char nonconst;
6526 char implicit;
6527 char erroneous;
6528 char outer;
6529 char incremental;
6530 char designated;
6533 static struct constructor_stack *constructor_stack;
6535 /* This stack represents designators from some range designator up to
6536 the last designator in the list. */
6538 struct constructor_range_stack
6540 struct constructor_range_stack *next, *prev;
6541 struct constructor_stack *stack;
6542 tree range_start;
6543 tree index;
6544 tree range_end;
6545 tree fields;
6548 static struct constructor_range_stack *constructor_range_stack;
6550 /* This stack records separate initializers that are nested.
6551 Nested initializers can't happen in ANSI C, but GNU C allows them
6552 in cases like { ... (struct foo) { ... } ... }. */
6554 struct initializer_stack
6556 struct initializer_stack *next;
6557 tree decl;
6558 struct constructor_stack *constructor_stack;
6559 struct constructor_range_stack *constructor_range_stack;
6560 VEC(constructor_elt,gc) *elements;
6561 struct spelling *spelling;
6562 struct spelling *spelling_base;
6563 int spelling_size;
6564 char top_level;
6565 char require_constant_value;
6566 char require_constant_elements;
6569 static struct initializer_stack *initializer_stack;
6571 /* Prepare to parse and output the initializer for variable DECL. */
6573 void
6574 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6576 const char *locus;
6577 struct initializer_stack *p = XNEW (struct initializer_stack);
6579 p->decl = constructor_decl;
6580 p->require_constant_value = require_constant_value;
6581 p->require_constant_elements = require_constant_elements;
6582 p->constructor_stack = constructor_stack;
6583 p->constructor_range_stack = constructor_range_stack;
6584 p->elements = constructor_elements;
6585 p->spelling = spelling;
6586 p->spelling_base = spelling_base;
6587 p->spelling_size = spelling_size;
6588 p->top_level = constructor_top_level;
6589 p->next = initializer_stack;
6590 initializer_stack = p;
6592 constructor_decl = decl;
6593 constructor_designated = 0;
6594 constructor_top_level = top_level;
6596 if (decl != 0 && decl != error_mark_node)
6598 require_constant_value = TREE_STATIC (decl);
6599 require_constant_elements
6600 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6601 /* For a scalar, you can always use any value to initialize,
6602 even within braces. */
6603 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6604 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6605 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6606 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6607 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6609 else
6611 require_constant_value = 0;
6612 require_constant_elements = 0;
6613 locus = _("(anonymous)");
6616 constructor_stack = 0;
6617 constructor_range_stack = 0;
6619 missing_braces_mentioned = 0;
6621 spelling_base = 0;
6622 spelling_size = 0;
6623 RESTORE_SPELLING_DEPTH (0);
6625 if (locus)
6626 push_string (locus);
6629 void
6630 finish_init (void)
6632 struct initializer_stack *p = initializer_stack;
6634 /* Free the whole constructor stack of this initializer. */
6635 while (constructor_stack)
6637 struct constructor_stack *q = constructor_stack;
6638 constructor_stack = q->next;
6639 free (q);
6642 gcc_assert (!constructor_range_stack);
6644 /* Pop back to the data of the outer initializer (if any). */
6645 free (spelling_base);
6647 constructor_decl = p->decl;
6648 require_constant_value = p->require_constant_value;
6649 require_constant_elements = p->require_constant_elements;
6650 constructor_stack = p->constructor_stack;
6651 constructor_range_stack = p->constructor_range_stack;
6652 constructor_elements = p->elements;
6653 spelling = p->spelling;
6654 spelling_base = p->spelling_base;
6655 spelling_size = p->spelling_size;
6656 constructor_top_level = p->top_level;
6657 initializer_stack = p->next;
6658 free (p);
6661 /* Call here when we see the initializer is surrounded by braces.
6662 This is instead of a call to push_init_level;
6663 it is matched by a call to pop_init_level.
6665 TYPE is the type to initialize, for a constructor expression.
6666 For an initializer for a decl, TYPE is zero. */
6668 void
6669 really_start_incremental_init (tree type)
6671 struct constructor_stack *p = XNEW (struct constructor_stack);
6673 if (type == 0)
6674 type = TREE_TYPE (constructor_decl);
6676 if (TREE_CODE (type) == VECTOR_TYPE
6677 && TYPE_VECTOR_OPAQUE (type))
6678 error ("opaque vector types cannot be initialized");
6680 p->type = constructor_type;
6681 p->fields = constructor_fields;
6682 p->index = constructor_index;
6683 p->max_index = constructor_max_index;
6684 p->unfilled_index = constructor_unfilled_index;
6685 p->unfilled_fields = constructor_unfilled_fields;
6686 p->bit_index = constructor_bit_index;
6687 p->elements = constructor_elements;
6688 p->constant = constructor_constant;
6689 p->simple = constructor_simple;
6690 p->nonconst = constructor_nonconst;
6691 p->erroneous = constructor_erroneous;
6692 p->pending_elts = constructor_pending_elts;
6693 p->depth = constructor_depth;
6694 p->replacement_value.value = 0;
6695 p->replacement_value.original_code = ERROR_MARK;
6696 p->replacement_value.original_type = NULL;
6697 p->implicit = 0;
6698 p->range_stack = 0;
6699 p->outer = 0;
6700 p->incremental = constructor_incremental;
6701 p->designated = constructor_designated;
6702 p->next = 0;
6703 constructor_stack = p;
6705 constructor_constant = 1;
6706 constructor_simple = 1;
6707 constructor_nonconst = 0;
6708 constructor_depth = SPELLING_DEPTH ();
6709 constructor_elements = 0;
6710 constructor_pending_elts = 0;
6711 constructor_type = type;
6712 constructor_incremental = 1;
6713 constructor_designated = 0;
6714 designator_depth = 0;
6715 designator_erroneous = 0;
6717 if (TREE_CODE (constructor_type) == RECORD_TYPE
6718 || TREE_CODE (constructor_type) == UNION_TYPE)
6720 constructor_fields = TYPE_FIELDS (constructor_type);
6721 /* Skip any nameless bit fields at the beginning. */
6722 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6723 && DECL_NAME (constructor_fields) == 0)
6724 constructor_fields = DECL_CHAIN (constructor_fields);
6726 constructor_unfilled_fields = constructor_fields;
6727 constructor_bit_index = bitsize_zero_node;
6729 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6731 if (TYPE_DOMAIN (constructor_type))
6733 constructor_max_index
6734 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6736 /* Detect non-empty initializations of zero-length arrays. */
6737 if (constructor_max_index == NULL_TREE
6738 && TYPE_SIZE (constructor_type))
6739 constructor_max_index = integer_minus_one_node;
6741 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6742 to initialize VLAs will cause a proper error; avoid tree
6743 checking errors as well by setting a safe value. */
6744 if (constructor_max_index
6745 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6746 constructor_max_index = integer_minus_one_node;
6748 constructor_index
6749 = convert (bitsizetype,
6750 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6752 else
6754 constructor_index = bitsize_zero_node;
6755 constructor_max_index = NULL_TREE;
6758 constructor_unfilled_index = constructor_index;
6760 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6762 /* Vectors are like simple fixed-size arrays. */
6763 constructor_max_index =
6764 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6765 constructor_index = bitsize_zero_node;
6766 constructor_unfilled_index = constructor_index;
6768 else
6770 /* Handle the case of int x = {5}; */
6771 constructor_fields = constructor_type;
6772 constructor_unfilled_fields = constructor_type;
6776 /* Push down into a subobject, for initialization.
6777 If this is for an explicit set of braces, IMPLICIT is 0.
6778 If it is because the next element belongs at a lower level,
6779 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6781 void
6782 push_init_level (int implicit, struct obstack * braced_init_obstack)
6784 struct constructor_stack *p;
6785 tree value = NULL_TREE;
6787 /* If we've exhausted any levels that didn't have braces,
6788 pop them now. If implicit == 1, this will have been done in
6789 process_init_element; do not repeat it here because in the case
6790 of excess initializers for an empty aggregate this leads to an
6791 infinite cycle of popping a level and immediately recreating
6792 it. */
6793 if (implicit != 1)
6795 while (constructor_stack->implicit)
6797 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6798 || TREE_CODE (constructor_type) == UNION_TYPE)
6799 && constructor_fields == 0)
6800 process_init_element (pop_init_level (1, braced_init_obstack),
6801 true, braced_init_obstack);
6802 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6803 && constructor_max_index
6804 && tree_int_cst_lt (constructor_max_index,
6805 constructor_index))
6806 process_init_element (pop_init_level (1, braced_init_obstack),
6807 true, braced_init_obstack);
6808 else
6809 break;
6813 /* Unless this is an explicit brace, we need to preserve previous
6814 content if any. */
6815 if (implicit)
6817 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6818 || TREE_CODE (constructor_type) == UNION_TYPE)
6819 && constructor_fields)
6820 value = find_init_member (constructor_fields, braced_init_obstack);
6821 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6822 value = find_init_member (constructor_index, braced_init_obstack);
6825 p = XNEW (struct constructor_stack);
6826 p->type = constructor_type;
6827 p->fields = constructor_fields;
6828 p->index = constructor_index;
6829 p->max_index = constructor_max_index;
6830 p->unfilled_index = constructor_unfilled_index;
6831 p->unfilled_fields = constructor_unfilled_fields;
6832 p->bit_index = constructor_bit_index;
6833 p->elements = constructor_elements;
6834 p->constant = constructor_constant;
6835 p->simple = constructor_simple;
6836 p->nonconst = constructor_nonconst;
6837 p->erroneous = constructor_erroneous;
6838 p->pending_elts = constructor_pending_elts;
6839 p->depth = constructor_depth;
6840 p->replacement_value.value = 0;
6841 p->replacement_value.original_code = ERROR_MARK;
6842 p->replacement_value.original_type = NULL;
6843 p->implicit = implicit;
6844 p->outer = 0;
6845 p->incremental = constructor_incremental;
6846 p->designated = constructor_designated;
6847 p->next = constructor_stack;
6848 p->range_stack = 0;
6849 constructor_stack = p;
6851 constructor_constant = 1;
6852 constructor_simple = 1;
6853 constructor_nonconst = 0;
6854 constructor_depth = SPELLING_DEPTH ();
6855 constructor_elements = 0;
6856 constructor_incremental = 1;
6857 constructor_designated = 0;
6858 constructor_pending_elts = 0;
6859 if (!implicit)
6861 p->range_stack = constructor_range_stack;
6862 constructor_range_stack = 0;
6863 designator_depth = 0;
6864 designator_erroneous = 0;
6867 /* Don't die if an entire brace-pair level is superfluous
6868 in the containing level. */
6869 if (constructor_type == 0)
6871 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6872 || TREE_CODE (constructor_type) == UNION_TYPE)
6874 /* Don't die if there are extra init elts at the end. */
6875 if (constructor_fields == 0)
6876 constructor_type = 0;
6877 else
6879 constructor_type = TREE_TYPE (constructor_fields);
6880 push_member_name (constructor_fields);
6881 constructor_depth++;
6884 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6886 constructor_type = TREE_TYPE (constructor_type);
6887 push_array_bounds (tree_low_cst (constructor_index, 1));
6888 constructor_depth++;
6891 if (constructor_type == 0)
6893 error_init ("extra brace group at end of initializer");
6894 constructor_fields = 0;
6895 constructor_unfilled_fields = 0;
6896 return;
6899 if (value && TREE_CODE (value) == CONSTRUCTOR)
6901 constructor_constant = TREE_CONSTANT (value);
6902 constructor_simple = TREE_STATIC (value);
6903 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6904 constructor_elements = CONSTRUCTOR_ELTS (value);
6905 if (!VEC_empty (constructor_elt, constructor_elements)
6906 && (TREE_CODE (constructor_type) == RECORD_TYPE
6907 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6908 set_nonincremental_init (braced_init_obstack);
6911 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6913 missing_braces_mentioned = 1;
6914 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6917 if (TREE_CODE (constructor_type) == RECORD_TYPE
6918 || TREE_CODE (constructor_type) == UNION_TYPE)
6920 constructor_fields = TYPE_FIELDS (constructor_type);
6921 /* Skip any nameless bit fields at the beginning. */
6922 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6923 && DECL_NAME (constructor_fields) == 0)
6924 constructor_fields = DECL_CHAIN (constructor_fields);
6926 constructor_unfilled_fields = constructor_fields;
6927 constructor_bit_index = bitsize_zero_node;
6929 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6931 /* Vectors are like simple fixed-size arrays. */
6932 constructor_max_index =
6933 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6934 constructor_index = bitsize_int (0);
6935 constructor_unfilled_index = constructor_index;
6937 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6939 if (TYPE_DOMAIN (constructor_type))
6941 constructor_max_index
6942 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6944 /* Detect non-empty initializations of zero-length arrays. */
6945 if (constructor_max_index == NULL_TREE
6946 && TYPE_SIZE (constructor_type))
6947 constructor_max_index = integer_minus_one_node;
6949 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6950 to initialize VLAs will cause a proper error; avoid tree
6951 checking errors as well by setting a safe value. */
6952 if (constructor_max_index
6953 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6954 constructor_max_index = integer_minus_one_node;
6956 constructor_index
6957 = convert (bitsizetype,
6958 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6960 else
6961 constructor_index = bitsize_zero_node;
6963 constructor_unfilled_index = constructor_index;
6964 if (value && TREE_CODE (value) == STRING_CST)
6966 /* We need to split the char/wchar array into individual
6967 characters, so that we don't have to special case it
6968 everywhere. */
6969 set_nonincremental_init_from_string (value, braced_init_obstack);
6972 else
6974 if (constructor_type != error_mark_node)
6975 warning_init (0, "braces around scalar initializer");
6976 constructor_fields = constructor_type;
6977 constructor_unfilled_fields = constructor_type;
6981 /* At the end of an implicit or explicit brace level,
6982 finish up that level of constructor. If a single expression
6983 with redundant braces initialized that level, return the
6984 c_expr structure for that expression. Otherwise, the original_code
6985 element is set to ERROR_MARK.
6986 If we were outputting the elements as they are read, return 0 as the value
6987 from inner levels (process_init_element ignores that),
6988 but return error_mark_node as the value from the outermost level
6989 (that's what we want to put in DECL_INITIAL).
6990 Otherwise, return a CONSTRUCTOR expression as the value. */
6992 struct c_expr
6993 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6995 struct constructor_stack *p;
6996 struct c_expr ret;
6997 ret.value = 0;
6998 ret.original_code = ERROR_MARK;
6999 ret.original_type = NULL;
7001 if (implicit == 0)
7003 /* When we come to an explicit close brace,
7004 pop any inner levels that didn't have explicit braces. */
7005 while (constructor_stack->implicit)
7007 process_init_element (pop_init_level (1, braced_init_obstack),
7008 true, braced_init_obstack);
7010 gcc_assert (!constructor_range_stack);
7013 /* Now output all pending elements. */
7014 constructor_incremental = 1;
7015 output_pending_init_elements (1, braced_init_obstack);
7017 p = constructor_stack;
7019 /* Error for initializing a flexible array member, or a zero-length
7020 array member in an inappropriate context. */
7021 if (constructor_type && constructor_fields
7022 && TREE_CODE (constructor_type) == ARRAY_TYPE
7023 && TYPE_DOMAIN (constructor_type)
7024 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
7026 /* Silently discard empty initializations. The parser will
7027 already have pedwarned for empty brackets. */
7028 if (integer_zerop (constructor_unfilled_index))
7029 constructor_type = NULL_TREE;
7030 else
7032 gcc_assert (!TYPE_SIZE (constructor_type));
7034 if (constructor_depth > 2)
7035 error_init ("initialization of flexible array member in a nested context");
7036 else
7037 pedwarn_init (input_location, OPT_pedantic,
7038 "initialization of a flexible array member");
7040 /* We have already issued an error message for the existence
7041 of a flexible array member not at the end of the structure.
7042 Discard the initializer so that we do not die later. */
7043 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7044 constructor_type = NULL_TREE;
7048 /* Warn when some struct elements are implicitly initialized to zero. */
7049 if (warn_missing_field_initializers
7050 && constructor_type
7051 && TREE_CODE (constructor_type) == RECORD_TYPE
7052 && constructor_unfilled_fields)
7054 bool constructor_zeroinit =
7055 (VEC_length (constructor_elt, constructor_elements) == 1
7056 && integer_zerop
7057 (VEC_index (constructor_elt, constructor_elements, 0)->value));
7059 /* Do not warn for flexible array members or zero-length arrays. */
7060 while (constructor_unfilled_fields
7061 && (!DECL_SIZE (constructor_unfilled_fields)
7062 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7063 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7065 if (constructor_unfilled_fields
7066 /* Do not warn if this level of the initializer uses member
7067 designators; it is likely to be deliberate. */
7068 && !constructor_designated
7069 /* Do not warn about initializing with ` = {0}'. */
7070 && !constructor_zeroinit)
7072 push_member_name (constructor_unfilled_fields);
7073 warning_init (OPT_Wmissing_field_initializers,
7074 "missing initializer");
7075 RESTORE_SPELLING_DEPTH (constructor_depth);
7079 /* Pad out the end of the structure. */
7080 if (p->replacement_value.value)
7081 /* If this closes a superfluous brace pair,
7082 just pass out the element between them. */
7083 ret = p->replacement_value;
7084 else if (constructor_type == 0)
7086 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7087 && TREE_CODE (constructor_type) != UNION_TYPE
7088 && TREE_CODE (constructor_type) != ARRAY_TYPE
7089 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7091 /* A nonincremental scalar initializer--just return
7092 the element, after verifying there is just one. */
7093 if (VEC_empty (constructor_elt,constructor_elements))
7095 if (!constructor_erroneous)
7096 error_init ("empty scalar initializer");
7097 ret.value = error_mark_node;
7099 else if (VEC_length (constructor_elt,constructor_elements) != 1)
7101 error_init ("extra elements in scalar initializer");
7102 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7104 else
7105 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7107 else
7109 if (constructor_erroneous)
7110 ret.value = error_mark_node;
7111 else
7113 ret.value = build_constructor (constructor_type,
7114 constructor_elements);
7115 if (constructor_constant)
7116 TREE_CONSTANT (ret.value) = 1;
7117 if (constructor_constant && constructor_simple)
7118 TREE_STATIC (ret.value) = 1;
7119 if (constructor_nonconst)
7120 CONSTRUCTOR_NON_CONST (ret.value) = 1;
7124 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7126 if (constructor_nonconst)
7127 ret.original_code = C_MAYBE_CONST_EXPR;
7128 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7129 ret.original_code = ERROR_MARK;
7132 constructor_type = p->type;
7133 constructor_fields = p->fields;
7134 constructor_index = p->index;
7135 constructor_max_index = p->max_index;
7136 constructor_unfilled_index = p->unfilled_index;
7137 constructor_unfilled_fields = p->unfilled_fields;
7138 constructor_bit_index = p->bit_index;
7139 constructor_elements = p->elements;
7140 constructor_constant = p->constant;
7141 constructor_simple = p->simple;
7142 constructor_nonconst = p->nonconst;
7143 constructor_erroneous = p->erroneous;
7144 constructor_incremental = p->incremental;
7145 constructor_designated = p->designated;
7146 constructor_pending_elts = p->pending_elts;
7147 constructor_depth = p->depth;
7148 if (!p->implicit)
7149 constructor_range_stack = p->range_stack;
7150 RESTORE_SPELLING_DEPTH (constructor_depth);
7152 constructor_stack = p->next;
7153 free (p);
7155 if (ret.value == 0 && constructor_stack == 0)
7156 ret.value = error_mark_node;
7157 return ret;
7160 /* Common handling for both array range and field name designators.
7161 ARRAY argument is nonzero for array ranges. Returns zero for success. */
7163 static int
7164 set_designator (int array, struct obstack * braced_init_obstack)
7166 tree subtype;
7167 enum tree_code subcode;
7169 /* Don't die if an entire brace-pair level is superfluous
7170 in the containing level. */
7171 if (constructor_type == 0)
7172 return 1;
7174 /* If there were errors in this designator list already, bail out
7175 silently. */
7176 if (designator_erroneous)
7177 return 1;
7179 if (!designator_depth)
7181 gcc_assert (!constructor_range_stack);
7183 /* Designator list starts at the level of closest explicit
7184 braces. */
7185 while (constructor_stack->implicit)
7187 process_init_element (pop_init_level (1, braced_init_obstack),
7188 true, braced_init_obstack);
7190 constructor_designated = 1;
7191 return 0;
7194 switch (TREE_CODE (constructor_type))
7196 case RECORD_TYPE:
7197 case UNION_TYPE:
7198 subtype = TREE_TYPE (constructor_fields);
7199 if (subtype != error_mark_node)
7200 subtype = TYPE_MAIN_VARIANT (subtype);
7201 break;
7202 case ARRAY_TYPE:
7203 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7204 break;
7205 default:
7206 gcc_unreachable ();
7209 subcode = TREE_CODE (subtype);
7210 if (array && subcode != ARRAY_TYPE)
7212 error_init ("array index in non-array initializer");
7213 return 1;
7215 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7217 error_init ("field name not in record or union initializer");
7218 return 1;
7221 constructor_designated = 1;
7222 push_init_level (2, braced_init_obstack);
7223 return 0;
7226 /* If there are range designators in designator list, push a new designator
7227 to constructor_range_stack. RANGE_END is end of such stack range or
7228 NULL_TREE if there is no range designator at this level. */
7230 static void
7231 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7233 struct constructor_range_stack *p;
7235 p = (struct constructor_range_stack *)
7236 obstack_alloc (braced_init_obstack,
7237 sizeof (struct constructor_range_stack));
7238 p->prev = constructor_range_stack;
7239 p->next = 0;
7240 p->fields = constructor_fields;
7241 p->range_start = constructor_index;
7242 p->index = constructor_index;
7243 p->stack = constructor_stack;
7244 p->range_end = range_end;
7245 if (constructor_range_stack)
7246 constructor_range_stack->next = p;
7247 constructor_range_stack = p;
7250 /* Within an array initializer, specify the next index to be initialized.
7251 FIRST is that index. If LAST is nonzero, then initialize a range
7252 of indices, running from FIRST through LAST. */
7254 void
7255 set_init_index (tree first, tree last,
7256 struct obstack * braced_init_obstack)
7258 if (set_designator (1, braced_init_obstack))
7259 return;
7261 designator_erroneous = 1;
7263 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7264 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7266 error_init ("array index in initializer not of integer type");
7267 return;
7270 if (TREE_CODE (first) != INTEGER_CST)
7272 first = c_fully_fold (first, false, NULL);
7273 if (TREE_CODE (first) == INTEGER_CST)
7274 pedwarn_init (input_location, OPT_pedantic,
7275 "array index in initializer is not "
7276 "an integer constant expression");
7279 if (last && TREE_CODE (last) != INTEGER_CST)
7281 last = c_fully_fold (last, false, NULL);
7282 if (TREE_CODE (last) == INTEGER_CST)
7283 pedwarn_init (input_location, OPT_pedantic,
7284 "array index in initializer is not "
7285 "an integer constant expression");
7288 if (TREE_CODE (first) != INTEGER_CST)
7289 error_init ("nonconstant array index in initializer");
7290 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7291 error_init ("nonconstant array index in initializer");
7292 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7293 error_init ("array index in non-array initializer");
7294 else if (tree_int_cst_sgn (first) == -1)
7295 error_init ("array index in initializer exceeds array bounds");
7296 else if (constructor_max_index
7297 && tree_int_cst_lt (constructor_max_index, first))
7298 error_init ("array index in initializer exceeds array bounds");
7299 else
7301 constant_expression_warning (first);
7302 if (last)
7303 constant_expression_warning (last);
7304 constructor_index = convert (bitsizetype, first);
7306 if (last)
7308 if (tree_int_cst_equal (first, last))
7309 last = 0;
7310 else if (tree_int_cst_lt (last, first))
7312 error_init ("empty index range in initializer");
7313 last = 0;
7315 else
7317 last = convert (bitsizetype, last);
7318 if (constructor_max_index != 0
7319 && tree_int_cst_lt (constructor_max_index, last))
7321 error_init ("array index range in initializer exceeds array bounds");
7322 last = 0;
7327 designator_depth++;
7328 designator_erroneous = 0;
7329 if (constructor_range_stack || last)
7330 push_range_stack (last, braced_init_obstack);
7334 /* Within a struct initializer, specify the next field to be initialized. */
7336 void
7337 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7339 tree field;
7341 if (set_designator (0, braced_init_obstack))
7342 return;
7344 designator_erroneous = 1;
7346 if (TREE_CODE (constructor_type) != RECORD_TYPE
7347 && TREE_CODE (constructor_type) != UNION_TYPE)
7349 error_init ("field name not in record or union initializer");
7350 return;
7353 field = lookup_field (constructor_type, fieldname);
7355 if (field == 0)
7356 error ("unknown field %qE specified in initializer", fieldname);
7357 else
7360 constructor_fields = TREE_VALUE (field);
7361 designator_depth++;
7362 designator_erroneous = 0;
7363 if (constructor_range_stack)
7364 push_range_stack (NULL_TREE, braced_init_obstack);
7365 field = TREE_CHAIN (field);
7366 if (field)
7368 if (set_designator (0, braced_init_obstack))
7369 return;
7372 while (field != NULL_TREE);
7375 /* Add a new initializer to the tree of pending initializers. PURPOSE
7376 identifies the initializer, either array index or field in a structure.
7377 VALUE is the value of that index or field. If ORIGTYPE is not
7378 NULL_TREE, it is the original type of VALUE.
7380 IMPLICIT is true if value comes from pop_init_level (1),
7381 the new initializer has been merged with the existing one
7382 and thus no warnings should be emitted about overriding an
7383 existing initializer. */
7385 static void
7386 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7387 struct obstack * braced_init_obstack)
7389 struct init_node *p, **q, *r;
7391 q = &constructor_pending_elts;
7392 p = 0;
7394 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7396 while (*q != 0)
7398 p = *q;
7399 if (tree_int_cst_lt (purpose, p->purpose))
7400 q = &p->left;
7401 else if (tree_int_cst_lt (p->purpose, purpose))
7402 q = &p->right;
7403 else
7405 if (!implicit)
7407 if (TREE_SIDE_EFFECTS (p->value))
7408 warning_init (0, "initialized field with side-effects overwritten");
7409 else if (warn_override_init)
7410 warning_init (OPT_Woverride_init, "initialized field overwritten");
7412 p->value = value;
7413 p->origtype = origtype;
7414 return;
7418 else
7420 tree bitpos;
7422 bitpos = bit_position (purpose);
7423 while (*q != NULL)
7425 p = *q;
7426 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7427 q = &p->left;
7428 else if (p->purpose != purpose)
7429 q = &p->right;
7430 else
7432 if (!implicit)
7434 if (TREE_SIDE_EFFECTS (p->value))
7435 warning_init (0, "initialized field with side-effects overwritten");
7436 else if (warn_override_init)
7437 warning_init (OPT_Woverride_init, "initialized field overwritten");
7439 p->value = value;
7440 p->origtype = origtype;
7441 return;
7446 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7447 sizeof (struct init_node));
7448 r->purpose = purpose;
7449 r->value = value;
7450 r->origtype = origtype;
7452 *q = r;
7453 r->parent = p;
7454 r->left = 0;
7455 r->right = 0;
7456 r->balance = 0;
7458 while (p)
7460 struct init_node *s;
7462 if (r == p->left)
7464 if (p->balance == 0)
7465 p->balance = -1;
7466 else if (p->balance < 0)
7468 if (r->balance < 0)
7470 /* L rotation. */
7471 p->left = r->right;
7472 if (p->left)
7473 p->left->parent = p;
7474 r->right = p;
7476 p->balance = 0;
7477 r->balance = 0;
7479 s = p->parent;
7480 p->parent = r;
7481 r->parent = s;
7482 if (s)
7484 if (s->left == p)
7485 s->left = r;
7486 else
7487 s->right = r;
7489 else
7490 constructor_pending_elts = r;
7492 else
7494 /* LR rotation. */
7495 struct init_node *t = r->right;
7497 r->right = t->left;
7498 if (r->right)
7499 r->right->parent = r;
7500 t->left = r;
7502 p->left = t->right;
7503 if (p->left)
7504 p->left->parent = p;
7505 t->right = p;
7507 p->balance = t->balance < 0;
7508 r->balance = -(t->balance > 0);
7509 t->balance = 0;
7511 s = p->parent;
7512 p->parent = t;
7513 r->parent = t;
7514 t->parent = s;
7515 if (s)
7517 if (s->left == p)
7518 s->left = t;
7519 else
7520 s->right = t;
7522 else
7523 constructor_pending_elts = t;
7525 break;
7527 else
7529 /* p->balance == +1; growth of left side balances the node. */
7530 p->balance = 0;
7531 break;
7534 else /* r == p->right */
7536 if (p->balance == 0)
7537 /* Growth propagation from right side. */
7538 p->balance++;
7539 else if (p->balance > 0)
7541 if (r->balance > 0)
7543 /* R rotation. */
7544 p->right = r->left;
7545 if (p->right)
7546 p->right->parent = p;
7547 r->left = p;
7549 p->balance = 0;
7550 r->balance = 0;
7552 s = p->parent;
7553 p->parent = r;
7554 r->parent = s;
7555 if (s)
7557 if (s->left == p)
7558 s->left = r;
7559 else
7560 s->right = r;
7562 else
7563 constructor_pending_elts = r;
7565 else /* r->balance == -1 */
7567 /* RL rotation */
7568 struct init_node *t = r->left;
7570 r->left = t->right;
7571 if (r->left)
7572 r->left->parent = r;
7573 t->right = r;
7575 p->right = t->left;
7576 if (p->right)
7577 p->right->parent = p;
7578 t->left = p;
7580 r->balance = (t->balance < 0);
7581 p->balance = -(t->balance > 0);
7582 t->balance = 0;
7584 s = p->parent;
7585 p->parent = t;
7586 r->parent = t;
7587 t->parent = s;
7588 if (s)
7590 if (s->left == p)
7591 s->left = t;
7592 else
7593 s->right = t;
7595 else
7596 constructor_pending_elts = t;
7598 break;
7600 else
7602 /* p->balance == -1; growth of right side balances the node. */
7603 p->balance = 0;
7604 break;
7608 r = p;
7609 p = p->parent;
7613 /* Build AVL tree from a sorted chain. */
7615 static void
7616 set_nonincremental_init (struct obstack * braced_init_obstack)
7618 unsigned HOST_WIDE_INT ix;
7619 tree index, value;
7621 if (TREE_CODE (constructor_type) != RECORD_TYPE
7622 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7623 return;
7625 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7627 add_pending_init (index, value, NULL_TREE, true,
7628 braced_init_obstack);
7630 constructor_elements = 0;
7631 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7633 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7634 /* Skip any nameless bit fields at the beginning. */
7635 while (constructor_unfilled_fields != 0
7636 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7637 && DECL_NAME (constructor_unfilled_fields) == 0)
7638 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7641 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7643 if (TYPE_DOMAIN (constructor_type))
7644 constructor_unfilled_index
7645 = convert (bitsizetype,
7646 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7647 else
7648 constructor_unfilled_index = bitsize_zero_node;
7650 constructor_incremental = 0;
7653 /* Build AVL tree from a string constant. */
7655 static void
7656 set_nonincremental_init_from_string (tree str,
7657 struct obstack * braced_init_obstack)
7659 tree value, purpose, type;
7660 HOST_WIDE_INT val[2];
7661 const char *p, *end;
7662 int byte, wchar_bytes, charwidth, bitpos;
7664 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7666 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7667 charwidth = TYPE_PRECISION (char_type_node);
7668 type = TREE_TYPE (constructor_type);
7669 p = TREE_STRING_POINTER (str);
7670 end = p + TREE_STRING_LENGTH (str);
7672 for (purpose = bitsize_zero_node;
7673 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7674 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7676 if (wchar_bytes == 1)
7678 val[1] = (unsigned char) *p++;
7679 val[0] = 0;
7681 else
7683 val[0] = 0;
7684 val[1] = 0;
7685 for (byte = 0; byte < wchar_bytes; byte++)
7687 if (BYTES_BIG_ENDIAN)
7688 bitpos = (wchar_bytes - byte - 1) * charwidth;
7689 else
7690 bitpos = byte * charwidth;
7691 val[bitpos < HOST_BITS_PER_WIDE_INT]
7692 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7693 << (bitpos % HOST_BITS_PER_WIDE_INT);
7697 if (!TYPE_UNSIGNED (type))
7699 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7700 if (bitpos < HOST_BITS_PER_WIDE_INT)
7702 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7704 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7705 val[0] = -1;
7708 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7710 if (val[1] < 0)
7711 val[0] = -1;
7713 else if (val[0] & (((HOST_WIDE_INT) 1)
7714 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7715 val[0] |= ((HOST_WIDE_INT) -1)
7716 << (bitpos - HOST_BITS_PER_WIDE_INT);
7719 value = build_int_cst_wide (type, val[1], val[0]);
7720 add_pending_init (purpose, value, NULL_TREE, true,
7721 braced_init_obstack);
7724 constructor_incremental = 0;
7727 /* Return value of FIELD in pending initializer or zero if the field was
7728 not initialized yet. */
7730 static tree
7731 find_init_member (tree field, struct obstack * braced_init_obstack)
7733 struct init_node *p;
7735 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7737 if (constructor_incremental
7738 && tree_int_cst_lt (field, constructor_unfilled_index))
7739 set_nonincremental_init (braced_init_obstack);
7741 p = constructor_pending_elts;
7742 while (p)
7744 if (tree_int_cst_lt (field, p->purpose))
7745 p = p->left;
7746 else if (tree_int_cst_lt (p->purpose, field))
7747 p = p->right;
7748 else
7749 return p->value;
7752 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7754 tree bitpos = bit_position (field);
7756 if (constructor_incremental
7757 && (!constructor_unfilled_fields
7758 || tree_int_cst_lt (bitpos,
7759 bit_position (constructor_unfilled_fields))))
7760 set_nonincremental_init (braced_init_obstack);
7762 p = constructor_pending_elts;
7763 while (p)
7765 if (field == p->purpose)
7766 return p->value;
7767 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7768 p = p->left;
7769 else
7770 p = p->right;
7773 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7775 if (!VEC_empty (constructor_elt, constructor_elements)
7776 && (VEC_last (constructor_elt, constructor_elements)->index
7777 == field))
7778 return VEC_last (constructor_elt, constructor_elements)->value;
7780 return 0;
7783 /* "Output" the next constructor element.
7784 At top level, really output it to assembler code now.
7785 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7786 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7787 TYPE is the data type that the containing data type wants here.
7788 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7789 If VALUE is a string constant, STRICT_STRING is true if it is
7790 unparenthesized or we should not warn here for it being parenthesized.
7791 For other types of VALUE, STRICT_STRING is not used.
7793 PENDING if non-nil means output pending elements that belong
7794 right after this element. (PENDING is normally 1;
7795 it is 0 while outputting pending elements, to avoid recursion.)
7797 IMPLICIT is true if value comes from pop_init_level (1),
7798 the new initializer has been merged with the existing one
7799 and thus no warnings should be emitted about overriding an
7800 existing initializer. */
7802 static void
7803 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7804 tree field, int pending, bool implicit,
7805 struct obstack * braced_init_obstack)
7807 tree semantic_type = NULL_TREE;
7808 constructor_elt *celt;
7809 bool maybe_const = true;
7810 bool npc;
7812 if (type == error_mark_node || value == error_mark_node)
7814 constructor_erroneous = 1;
7815 return;
7817 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7818 && (TREE_CODE (value) == STRING_CST
7819 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7820 && !(TREE_CODE (value) == STRING_CST
7821 && TREE_CODE (type) == ARRAY_TYPE
7822 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7823 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7824 TYPE_MAIN_VARIANT (type)))
7825 value = array_to_pointer_conversion (input_location, value);
7827 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7828 && require_constant_value && !flag_isoc99 && pending)
7830 /* As an extension, allow initializing objects with static storage
7831 duration with compound literals (which are then treated just as
7832 the brace enclosed list they contain). */
7833 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7834 value = DECL_INITIAL (decl);
7837 npc = null_pointer_constant_p (value);
7838 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7840 semantic_type = TREE_TYPE (value);
7841 value = TREE_OPERAND (value, 0);
7843 value = c_fully_fold (value, require_constant_value, &maybe_const);
7845 if (value == error_mark_node)
7846 constructor_erroneous = 1;
7847 else if (!TREE_CONSTANT (value))
7848 constructor_constant = 0;
7849 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7850 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7851 || TREE_CODE (constructor_type) == UNION_TYPE)
7852 && DECL_C_BIT_FIELD (field)
7853 && TREE_CODE (value) != INTEGER_CST))
7854 constructor_simple = 0;
7855 if (!maybe_const)
7856 constructor_nonconst = 1;
7858 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7860 if (require_constant_value)
7862 error_init ("initializer element is not constant");
7863 value = error_mark_node;
7865 else if (require_constant_elements)
7866 pedwarn (input_location, 0,
7867 "initializer element is not computable at load time");
7869 else if (!maybe_const
7870 && (require_constant_value || require_constant_elements))
7871 pedwarn_init (input_location, 0,
7872 "initializer element is not a constant expression");
7874 /* Issue -Wc++-compat warnings about initializing a bitfield with
7875 enum type. */
7876 if (warn_cxx_compat
7877 && field != NULL_TREE
7878 && TREE_CODE (field) == FIELD_DECL
7879 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7880 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7881 != TYPE_MAIN_VARIANT (type))
7882 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7884 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7885 if (checktype != error_mark_node
7886 && (TYPE_MAIN_VARIANT (checktype)
7887 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7888 warning_init (OPT_Wc___compat,
7889 "enum conversion in initialization is invalid in C++");
7892 /* If this field is empty (and not at the end of structure),
7893 don't do anything other than checking the initializer. */
7894 if (field
7895 && (TREE_TYPE (field) == error_mark_node
7896 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7897 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7898 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7899 || DECL_CHAIN (field)))))
7900 return;
7902 if (semantic_type)
7903 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7904 value = digest_init (input_location, type, value, origtype, npc,
7905 strict_string, require_constant_value);
7906 if (value == error_mark_node)
7908 constructor_erroneous = 1;
7909 return;
7911 if (require_constant_value || require_constant_elements)
7912 constant_expression_warning (value);
7914 /* If this element doesn't come next in sequence,
7915 put it on constructor_pending_elts. */
7916 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7917 && (!constructor_incremental
7918 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7920 if (constructor_incremental
7921 && tree_int_cst_lt (field, constructor_unfilled_index))
7922 set_nonincremental_init (braced_init_obstack);
7924 add_pending_init (field, value, origtype, implicit,
7925 braced_init_obstack);
7926 return;
7928 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7929 && (!constructor_incremental
7930 || field != constructor_unfilled_fields))
7932 /* We do this for records but not for unions. In a union,
7933 no matter which field is specified, it can be initialized
7934 right away since it starts at the beginning of the union. */
7935 if (constructor_incremental)
7937 if (!constructor_unfilled_fields)
7938 set_nonincremental_init (braced_init_obstack);
7939 else
7941 tree bitpos, unfillpos;
7943 bitpos = bit_position (field);
7944 unfillpos = bit_position (constructor_unfilled_fields);
7946 if (tree_int_cst_lt (bitpos, unfillpos))
7947 set_nonincremental_init (braced_init_obstack);
7951 add_pending_init (field, value, origtype, implicit,
7952 braced_init_obstack);
7953 return;
7955 else if (TREE_CODE (constructor_type) == UNION_TYPE
7956 && !VEC_empty (constructor_elt, constructor_elements))
7958 if (!implicit)
7960 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7961 constructor_elements)->value))
7962 warning_init (0,
7963 "initialized field with side-effects overwritten");
7964 else if (warn_override_init)
7965 warning_init (OPT_Woverride_init, "initialized field overwritten");
7968 /* We can have just one union field set. */
7969 constructor_elements = 0;
7972 /* Otherwise, output this element either to
7973 constructor_elements or to the assembler file. */
7975 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7976 celt->index = field;
7977 celt->value = value;
7979 /* Advance the variable that indicates sequential elements output. */
7980 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7981 constructor_unfilled_index
7982 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7983 bitsize_one_node);
7984 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7986 constructor_unfilled_fields
7987 = DECL_CHAIN (constructor_unfilled_fields);
7989 /* Skip any nameless bit fields. */
7990 while (constructor_unfilled_fields != 0
7991 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7992 && DECL_NAME (constructor_unfilled_fields) == 0)
7993 constructor_unfilled_fields =
7994 DECL_CHAIN (constructor_unfilled_fields);
7996 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7997 constructor_unfilled_fields = 0;
7999 /* Now output any pending elements which have become next. */
8000 if (pending)
8001 output_pending_init_elements (0, braced_init_obstack);
8004 /* Output any pending elements which have become next.
8005 As we output elements, constructor_unfilled_{fields,index}
8006 advances, which may cause other elements to become next;
8007 if so, they too are output.
8009 If ALL is 0, we return when there are
8010 no more pending elements to output now.
8012 If ALL is 1, we output space as necessary so that
8013 we can output all the pending elements. */
8014 static void
8015 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
8017 struct init_node *elt = constructor_pending_elts;
8018 tree next;
8020 retry:
8022 /* Look through the whole pending tree.
8023 If we find an element that should be output now,
8024 output it. Otherwise, set NEXT to the element
8025 that comes first among those still pending. */
8027 next = 0;
8028 while (elt)
8030 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8032 if (tree_int_cst_equal (elt->purpose,
8033 constructor_unfilled_index))
8034 output_init_element (elt->value, elt->origtype, true,
8035 TREE_TYPE (constructor_type),
8036 constructor_unfilled_index, 0, false,
8037 braced_init_obstack);
8038 else if (tree_int_cst_lt (constructor_unfilled_index,
8039 elt->purpose))
8041 /* Advance to the next smaller node. */
8042 if (elt->left)
8043 elt = elt->left;
8044 else
8046 /* We have reached the smallest node bigger than the
8047 current unfilled index. Fill the space first. */
8048 next = elt->purpose;
8049 break;
8052 else
8054 /* Advance to the next bigger node. */
8055 if (elt->right)
8056 elt = elt->right;
8057 else
8059 /* We have reached the biggest node in a subtree. Find
8060 the parent of it, which is the next bigger node. */
8061 while (elt->parent && elt->parent->right == elt)
8062 elt = elt->parent;
8063 elt = elt->parent;
8064 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8065 elt->purpose))
8067 next = elt->purpose;
8068 break;
8073 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8074 || TREE_CODE (constructor_type) == UNION_TYPE)
8076 tree ctor_unfilled_bitpos, elt_bitpos;
8078 /* If the current record is complete we are done. */
8079 if (constructor_unfilled_fields == 0)
8080 break;
8082 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8083 elt_bitpos = bit_position (elt->purpose);
8084 /* We can't compare fields here because there might be empty
8085 fields in between. */
8086 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8088 constructor_unfilled_fields = elt->purpose;
8089 output_init_element (elt->value, elt->origtype, true,
8090 TREE_TYPE (elt->purpose),
8091 elt->purpose, 0, false,
8092 braced_init_obstack);
8094 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8096 /* Advance to the next smaller node. */
8097 if (elt->left)
8098 elt = elt->left;
8099 else
8101 /* We have reached the smallest node bigger than the
8102 current unfilled field. Fill the space first. */
8103 next = elt->purpose;
8104 break;
8107 else
8109 /* Advance to the next bigger node. */
8110 if (elt->right)
8111 elt = elt->right;
8112 else
8114 /* We have reached the biggest node in a subtree. Find
8115 the parent of it, which is the next bigger node. */
8116 while (elt->parent && elt->parent->right == elt)
8117 elt = elt->parent;
8118 elt = elt->parent;
8119 if (elt
8120 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8121 bit_position (elt->purpose))))
8123 next = elt->purpose;
8124 break;
8131 /* Ordinarily return, but not if we want to output all
8132 and there are elements left. */
8133 if (!(all && next != 0))
8134 return;
8136 /* If it's not incremental, just skip over the gap, so that after
8137 jumping to retry we will output the next successive element. */
8138 if (TREE_CODE (constructor_type) == RECORD_TYPE
8139 || TREE_CODE (constructor_type) == UNION_TYPE)
8140 constructor_unfilled_fields = next;
8141 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8142 constructor_unfilled_index = next;
8144 /* ELT now points to the node in the pending tree with the next
8145 initializer to output. */
8146 goto retry;
8149 /* Add one non-braced element to the current constructor level.
8150 This adjusts the current position within the constructor's type.
8151 This may also start or terminate implicit levels
8152 to handle a partly-braced initializer.
8154 Once this has found the correct level for the new element,
8155 it calls output_init_element.
8157 IMPLICIT is true if value comes from pop_init_level (1),
8158 the new initializer has been merged with the existing one
8159 and thus no warnings should be emitted about overriding an
8160 existing initializer. */
8162 void
8163 process_init_element (struct c_expr value, bool implicit,
8164 struct obstack * braced_init_obstack)
8166 tree orig_value = value.value;
8167 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8168 bool strict_string = value.original_code == STRING_CST;
8170 designator_depth = 0;
8171 designator_erroneous = 0;
8173 /* Handle superfluous braces around string cst as in
8174 char x[] = {"foo"}; */
8175 if (string_flag
8176 && constructor_type
8177 && TREE_CODE (constructor_type) == ARRAY_TYPE
8178 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8179 && integer_zerop (constructor_unfilled_index))
8181 if (constructor_stack->replacement_value.value)
8182 error_init ("excess elements in char array initializer");
8183 constructor_stack->replacement_value = value;
8184 return;
8187 if (constructor_stack->replacement_value.value != 0)
8189 error_init ("excess elements in struct initializer");
8190 return;
8193 /* Ignore elements of a brace group if it is entirely superfluous
8194 and has already been diagnosed. */
8195 if (constructor_type == 0)
8196 return;
8198 /* If we've exhausted any levels that didn't have braces,
8199 pop them now. */
8200 while (constructor_stack->implicit)
8202 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8203 || TREE_CODE (constructor_type) == UNION_TYPE)
8204 && constructor_fields == 0)
8205 process_init_element (pop_init_level (1, braced_init_obstack),
8206 true, braced_init_obstack);
8207 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8208 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8209 && (constructor_max_index == 0
8210 || tree_int_cst_lt (constructor_max_index,
8211 constructor_index)))
8212 process_init_element (pop_init_level (1, braced_init_obstack),
8213 true, braced_init_obstack);
8214 else
8215 break;
8218 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8219 if (constructor_range_stack)
8221 /* If value is a compound literal and we'll be just using its
8222 content, don't put it into a SAVE_EXPR. */
8223 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8224 || !require_constant_value
8225 || flag_isoc99)
8227 tree semantic_type = NULL_TREE;
8228 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8230 semantic_type = TREE_TYPE (value.value);
8231 value.value = TREE_OPERAND (value.value, 0);
8233 value.value = c_save_expr (value.value);
8234 if (semantic_type)
8235 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8236 value.value);
8240 while (1)
8242 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8244 tree fieldtype;
8245 enum tree_code fieldcode;
8247 if (constructor_fields == 0)
8249 pedwarn_init (input_location, 0,
8250 "excess elements in struct initializer");
8251 break;
8254 fieldtype = TREE_TYPE (constructor_fields);
8255 if (fieldtype != error_mark_node)
8256 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8257 fieldcode = TREE_CODE (fieldtype);
8259 /* Error for non-static initialization of a flexible array member. */
8260 if (fieldcode == ARRAY_TYPE
8261 && !require_constant_value
8262 && TYPE_SIZE (fieldtype) == NULL_TREE
8263 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8265 error_init ("non-static initialization of a flexible array member");
8266 break;
8269 /* Accept a string constant to initialize a subarray. */
8270 if (value.value != 0
8271 && fieldcode == ARRAY_TYPE
8272 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8273 && string_flag)
8274 value.value = orig_value;
8275 /* Otherwise, if we have come to a subaggregate,
8276 and we don't have an element of its type, push into it. */
8277 else if (value.value != 0
8278 && value.value != error_mark_node
8279 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8280 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8281 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8283 push_init_level (1, braced_init_obstack);
8284 continue;
8287 if (value.value)
8289 push_member_name (constructor_fields);
8290 output_init_element (value.value, value.original_type,
8291 strict_string, fieldtype,
8292 constructor_fields, 1, implicit,
8293 braced_init_obstack);
8294 RESTORE_SPELLING_DEPTH (constructor_depth);
8296 else
8297 /* Do the bookkeeping for an element that was
8298 directly output as a constructor. */
8300 /* For a record, keep track of end position of last field. */
8301 if (DECL_SIZE (constructor_fields))
8302 constructor_bit_index
8303 = size_binop_loc (input_location, PLUS_EXPR,
8304 bit_position (constructor_fields),
8305 DECL_SIZE (constructor_fields));
8307 /* If the current field was the first one not yet written out,
8308 it isn't now, so update. */
8309 if (constructor_unfilled_fields == constructor_fields)
8311 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8312 /* Skip any nameless bit fields. */
8313 while (constructor_unfilled_fields != 0
8314 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8315 && DECL_NAME (constructor_unfilled_fields) == 0)
8316 constructor_unfilled_fields =
8317 DECL_CHAIN (constructor_unfilled_fields);
8321 constructor_fields = DECL_CHAIN (constructor_fields);
8322 /* Skip any nameless bit fields at the beginning. */
8323 while (constructor_fields != 0
8324 && DECL_C_BIT_FIELD (constructor_fields)
8325 && DECL_NAME (constructor_fields) == 0)
8326 constructor_fields = DECL_CHAIN (constructor_fields);
8328 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8330 tree fieldtype;
8331 enum tree_code fieldcode;
8333 if (constructor_fields == 0)
8335 pedwarn_init (input_location, 0,
8336 "excess elements in union initializer");
8337 break;
8340 fieldtype = TREE_TYPE (constructor_fields);
8341 if (fieldtype != error_mark_node)
8342 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8343 fieldcode = TREE_CODE (fieldtype);
8345 /* Warn that traditional C rejects initialization of unions.
8346 We skip the warning if the value is zero. This is done
8347 under the assumption that the zero initializer in user
8348 code appears conditioned on e.g. __STDC__ to avoid
8349 "missing initializer" warnings and relies on default
8350 initialization to zero in the traditional C case.
8351 We also skip the warning if the initializer is designated,
8352 again on the assumption that this must be conditional on
8353 __STDC__ anyway (and we've already complained about the
8354 member-designator already). */
8355 if (!in_system_header && !constructor_designated
8356 && !(value.value && (integer_zerop (value.value)
8357 || real_zerop (value.value))))
8358 warning (OPT_Wtraditional, "traditional C rejects initialization "
8359 "of unions");
8361 /* Accept a string constant to initialize a subarray. */
8362 if (value.value != 0
8363 && fieldcode == ARRAY_TYPE
8364 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8365 && string_flag)
8366 value.value = orig_value;
8367 /* Otherwise, if we have come to a subaggregate,
8368 and we don't have an element of its type, push into it. */
8369 else if (value.value != 0
8370 && value.value != error_mark_node
8371 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8372 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8373 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8375 push_init_level (1, braced_init_obstack);
8376 continue;
8379 if (value.value)
8381 push_member_name (constructor_fields);
8382 output_init_element (value.value, value.original_type,
8383 strict_string, fieldtype,
8384 constructor_fields, 1, implicit,
8385 braced_init_obstack);
8386 RESTORE_SPELLING_DEPTH (constructor_depth);
8388 else
8389 /* Do the bookkeeping for an element that was
8390 directly output as a constructor. */
8392 constructor_bit_index = DECL_SIZE (constructor_fields);
8393 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8396 constructor_fields = 0;
8398 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8400 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8401 enum tree_code eltcode = TREE_CODE (elttype);
8403 /* Accept a string constant to initialize a subarray. */
8404 if (value.value != 0
8405 && eltcode == ARRAY_TYPE
8406 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8407 && string_flag)
8408 value.value = orig_value;
8409 /* Otherwise, if we have come to a subaggregate,
8410 and we don't have an element of its type, push into it. */
8411 else if (value.value != 0
8412 && value.value != error_mark_node
8413 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8414 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8415 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8417 push_init_level (1, braced_init_obstack);
8418 continue;
8421 if (constructor_max_index != 0
8422 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8423 || integer_all_onesp (constructor_max_index)))
8425 pedwarn_init (input_location, 0,
8426 "excess elements in array initializer");
8427 break;
8430 /* Now output the actual element. */
8431 if (value.value)
8433 push_array_bounds (tree_low_cst (constructor_index, 1));
8434 output_init_element (value.value, value.original_type,
8435 strict_string, elttype,
8436 constructor_index, 1, implicit,
8437 braced_init_obstack);
8438 RESTORE_SPELLING_DEPTH (constructor_depth);
8441 constructor_index
8442 = size_binop_loc (input_location, PLUS_EXPR,
8443 constructor_index, bitsize_one_node);
8445 if (!value.value)
8446 /* If we are doing the bookkeeping for an element that was
8447 directly output as a constructor, we must update
8448 constructor_unfilled_index. */
8449 constructor_unfilled_index = constructor_index;
8451 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8453 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8455 /* Do a basic check of initializer size. Note that vectors
8456 always have a fixed size derived from their type. */
8457 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8459 pedwarn_init (input_location, 0,
8460 "excess elements in vector initializer");
8461 break;
8464 /* Now output the actual element. */
8465 if (value.value)
8467 if (TREE_CODE (value.value) == VECTOR_CST)
8468 elttype = TYPE_MAIN_VARIANT (constructor_type);
8469 output_init_element (value.value, value.original_type,
8470 strict_string, elttype,
8471 constructor_index, 1, implicit,
8472 braced_init_obstack);
8475 constructor_index
8476 = size_binop_loc (input_location,
8477 PLUS_EXPR, constructor_index, bitsize_one_node);
8479 if (!value.value)
8480 /* If we are doing the bookkeeping for an element that was
8481 directly output as a constructor, we must update
8482 constructor_unfilled_index. */
8483 constructor_unfilled_index = constructor_index;
8486 /* Handle the sole element allowed in a braced initializer
8487 for a scalar variable. */
8488 else if (constructor_type != error_mark_node
8489 && constructor_fields == 0)
8491 pedwarn_init (input_location, 0,
8492 "excess elements in scalar initializer");
8493 break;
8495 else
8497 if (value.value)
8498 output_init_element (value.value, value.original_type,
8499 strict_string, constructor_type,
8500 NULL_TREE, 1, implicit,
8501 braced_init_obstack);
8502 constructor_fields = 0;
8505 /* Handle range initializers either at this level or anywhere higher
8506 in the designator stack. */
8507 if (constructor_range_stack)
8509 struct constructor_range_stack *p, *range_stack;
8510 int finish = 0;
8512 range_stack = constructor_range_stack;
8513 constructor_range_stack = 0;
8514 while (constructor_stack != range_stack->stack)
8516 gcc_assert (constructor_stack->implicit);
8517 process_init_element (pop_init_level (1,
8518 braced_init_obstack),
8519 true, braced_init_obstack);
8521 for (p = range_stack;
8522 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8523 p = p->prev)
8525 gcc_assert (constructor_stack->implicit);
8526 process_init_element (pop_init_level (1, braced_init_obstack),
8527 true, braced_init_obstack);
8530 p->index = size_binop_loc (input_location,
8531 PLUS_EXPR, p->index, bitsize_one_node);
8532 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8533 finish = 1;
8535 while (1)
8537 constructor_index = p->index;
8538 constructor_fields = p->fields;
8539 if (finish && p->range_end && p->index == p->range_start)
8541 finish = 0;
8542 p->prev = 0;
8544 p = p->next;
8545 if (!p)
8546 break;
8547 push_init_level (2, braced_init_obstack);
8548 p->stack = constructor_stack;
8549 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8550 p->index = p->range_start;
8553 if (!finish)
8554 constructor_range_stack = range_stack;
8555 continue;
8558 break;
8561 constructor_range_stack = 0;
8564 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8565 (guaranteed to be 'volatile' or null) and ARGS (represented using
8566 an ASM_EXPR node). */
8567 tree
8568 build_asm_stmt (tree cv_qualifier, tree args)
8570 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8571 ASM_VOLATILE_P (args) = 1;
8572 return add_stmt (args);
8575 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8576 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8577 SIMPLE indicates whether there was anything at all after the
8578 string in the asm expression -- asm("blah") and asm("blah" : )
8579 are subtly different. We use a ASM_EXPR node to represent this. */
8580 tree
8581 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8582 tree clobbers, tree labels, bool simple)
8584 tree tail;
8585 tree args;
8586 int i;
8587 const char *constraint;
8588 const char **oconstraints;
8589 bool allows_mem, allows_reg, is_inout;
8590 int ninputs, noutputs;
8592 ninputs = list_length (inputs);
8593 noutputs = list_length (outputs);
8594 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8596 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8598 /* Remove output conversions that change the type but not the mode. */
8599 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8601 tree output = TREE_VALUE (tail);
8603 /* ??? Really, this should not be here. Users should be using a
8604 proper lvalue, dammit. But there's a long history of using casts
8605 in the output operands. In cases like longlong.h, this becomes a
8606 primitive form of typechecking -- if the cast can be removed, then
8607 the output operand had a type of the proper width; otherwise we'll
8608 get an error. Gross, but ... */
8609 STRIP_NOPS (output);
8611 if (!lvalue_or_else (loc, output, lv_asm))
8612 output = error_mark_node;
8614 if (output != error_mark_node
8615 && (TREE_READONLY (output)
8616 || TYPE_READONLY (TREE_TYPE (output))
8617 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8618 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8619 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8620 readonly_error (output, lv_asm);
8622 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8623 oconstraints[i] = constraint;
8625 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8626 &allows_mem, &allows_reg, &is_inout))
8628 /* If the operand is going to end up in memory,
8629 mark it addressable. */
8630 if (!allows_reg && !c_mark_addressable (output))
8631 output = error_mark_node;
8632 if (!(!allows_reg && allows_mem)
8633 && output != error_mark_node
8634 && VOID_TYPE_P (TREE_TYPE (output)))
8636 error_at (loc, "invalid use of void expression");
8637 output = error_mark_node;
8640 else
8641 output = error_mark_node;
8643 TREE_VALUE (tail) = output;
8646 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8648 tree input;
8650 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8651 input = TREE_VALUE (tail);
8653 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8654 oconstraints, &allows_mem, &allows_reg))
8656 /* If the operand is going to end up in memory,
8657 mark it addressable. */
8658 if (!allows_reg && allows_mem)
8660 /* Strip the nops as we allow this case. FIXME, this really
8661 should be rejected or made deprecated. */
8662 STRIP_NOPS (input);
8663 if (!c_mark_addressable (input))
8664 input = error_mark_node;
8666 else if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8668 error_at (loc, "invalid use of void expression");
8669 input = error_mark_node;
8672 else
8673 input = error_mark_node;
8675 TREE_VALUE (tail) = input;
8678 /* ASMs with labels cannot have outputs. This should have been
8679 enforced by the parser. */
8680 gcc_assert (outputs == NULL || labels == NULL);
8682 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8684 /* asm statements without outputs, including simple ones, are treated
8685 as volatile. */
8686 ASM_INPUT_P (args) = simple;
8687 ASM_VOLATILE_P (args) = (noutputs == 0);
8689 return args;
8692 /* Generate a goto statement to LABEL. LOC is the location of the
8693 GOTO. */
8695 tree
8696 c_finish_goto_label (location_t loc, tree label)
8698 tree decl = lookup_label_for_goto (loc, label);
8699 if (!decl)
8700 return NULL_TREE;
8701 TREE_USED (decl) = 1;
8703 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8704 SET_EXPR_LOCATION (t, loc);
8705 return add_stmt (t);
8709 /* Generate a computed goto statement to EXPR. LOC is the location of
8710 the GOTO. */
8712 tree
8713 c_finish_goto_ptr (location_t loc, tree expr)
8715 tree t;
8716 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8717 expr = c_fully_fold (expr, false, NULL);
8718 expr = convert (ptr_type_node, expr);
8719 t = build1 (GOTO_EXPR, void_type_node, expr);
8720 SET_EXPR_LOCATION (t, loc);
8721 return add_stmt (t);
8724 /* Generate a C `return' statement. RETVAL is the expression for what
8725 to return, or a null pointer for `return;' with no value. LOC is
8726 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8727 is the original type of RETVAL. */
8729 tree
8730 c_finish_return (location_t loc, tree retval, tree origtype)
8732 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8733 bool no_warning = false;
8734 bool npc = false;
8736 if (TREE_THIS_VOLATILE (current_function_decl))
8737 warning_at (loc, 0,
8738 "function declared %<noreturn%> has a %<return%> statement");
8740 if (retval)
8742 tree semantic_type = NULL_TREE;
8743 npc = null_pointer_constant_p (retval);
8744 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8746 semantic_type = TREE_TYPE (retval);
8747 retval = TREE_OPERAND (retval, 0);
8749 retval = c_fully_fold (retval, false, NULL);
8750 if (semantic_type)
8751 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8754 if (!retval)
8756 current_function_returns_null = 1;
8757 if ((warn_return_type || flag_isoc99)
8758 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8760 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8761 "%<return%> with no value, in "
8762 "function returning non-void");
8763 no_warning = true;
8766 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8768 current_function_returns_null = 1;
8769 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8770 pedwarn (loc, 0,
8771 "%<return%> with a value, in function returning void");
8772 else
8773 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8774 "%<return%> with expression, in function returning void");
8776 else
8778 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8779 ic_return,
8780 npc, NULL_TREE, NULL_TREE, 0);
8781 tree res = DECL_RESULT (current_function_decl);
8782 tree inner;
8783 bool save;
8785 current_function_returns_value = 1;
8786 if (t == error_mark_node)
8787 return NULL_TREE;
8789 save = in_late_binary_op;
8790 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
8791 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
8792 in_late_binary_op = true;
8793 inner = t = convert (TREE_TYPE (res), t);
8794 in_late_binary_op = save;
8796 /* Strip any conversions, additions, and subtractions, and see if
8797 we are returning the address of a local variable. Warn if so. */
8798 while (1)
8800 switch (TREE_CODE (inner))
8802 CASE_CONVERT:
8803 case NON_LVALUE_EXPR:
8804 case PLUS_EXPR:
8805 case POINTER_PLUS_EXPR:
8806 inner = TREE_OPERAND (inner, 0);
8807 continue;
8809 case MINUS_EXPR:
8810 /* If the second operand of the MINUS_EXPR has a pointer
8811 type (or is converted from it), this may be valid, so
8812 don't give a warning. */
8814 tree op1 = TREE_OPERAND (inner, 1);
8816 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8817 && (CONVERT_EXPR_P (op1)
8818 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8819 op1 = TREE_OPERAND (op1, 0);
8821 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8822 break;
8824 inner = TREE_OPERAND (inner, 0);
8825 continue;
8828 case ADDR_EXPR:
8829 inner = TREE_OPERAND (inner, 0);
8831 while (REFERENCE_CLASS_P (inner)
8832 && TREE_CODE (inner) != INDIRECT_REF)
8833 inner = TREE_OPERAND (inner, 0);
8835 if (DECL_P (inner)
8836 && !DECL_EXTERNAL (inner)
8837 && !TREE_STATIC (inner)
8838 && DECL_CONTEXT (inner) == current_function_decl)
8839 warning_at (loc,
8840 0, "function returns address of local variable");
8841 break;
8843 default:
8844 break;
8847 break;
8850 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8851 SET_EXPR_LOCATION (retval, loc);
8853 if (warn_sequence_point)
8854 verify_sequence_points (retval);
8857 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8858 TREE_NO_WARNING (ret_stmt) |= no_warning;
8859 return add_stmt (ret_stmt);
8862 struct c_switch {
8863 /* The SWITCH_EXPR being built. */
8864 tree switch_expr;
8866 /* The original type of the testing expression, i.e. before the
8867 default conversion is applied. */
8868 tree orig_type;
8870 /* A splay-tree mapping the low element of a case range to the high
8871 element, or NULL_TREE if there is no high element. Used to
8872 determine whether or not a new case label duplicates an old case
8873 label. We need a tree, rather than simply a hash table, because
8874 of the GNU case range extension. */
8875 splay_tree cases;
8877 /* The bindings at the point of the switch. This is used for
8878 warnings crossing decls when branching to a case label. */
8879 struct c_spot_bindings *bindings;
8881 /* The next node on the stack. */
8882 struct c_switch *next;
8885 /* A stack of the currently active switch statements. The innermost
8886 switch statement is on the top of the stack. There is no need to
8887 mark the stack for garbage collection because it is only active
8888 during the processing of the body of a function, and we never
8889 collect at that point. */
8891 struct c_switch *c_switch_stack;
8893 /* Start a C switch statement, testing expression EXP. Return the new
8894 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8895 SWITCH_COND_LOC is the location of the switch's condition. */
8897 tree
8898 c_start_case (location_t switch_loc,
8899 location_t switch_cond_loc,
8900 tree exp)
8902 tree orig_type = error_mark_node;
8903 struct c_switch *cs;
8905 if (exp != error_mark_node)
8907 orig_type = TREE_TYPE (exp);
8909 if (!INTEGRAL_TYPE_P (orig_type))
8911 if (orig_type != error_mark_node)
8913 error_at (switch_cond_loc, "switch quantity not an integer");
8914 orig_type = error_mark_node;
8916 exp = integer_zero_node;
8918 else
8920 tree type = TYPE_MAIN_VARIANT (orig_type);
8922 if (!in_system_header
8923 && (type == long_integer_type_node
8924 || type == long_unsigned_type_node))
8925 warning_at (switch_cond_loc,
8926 OPT_Wtraditional, "%<long%> switch expression not "
8927 "converted to %<int%> in ISO C");
8929 exp = c_fully_fold (exp, false, NULL);
8930 exp = default_conversion (exp);
8932 if (warn_sequence_point)
8933 verify_sequence_points (exp);
8937 /* Add this new SWITCH_EXPR to the stack. */
8938 cs = XNEW (struct c_switch);
8939 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8940 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8941 cs->orig_type = orig_type;
8942 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8943 cs->bindings = c_get_switch_bindings ();
8944 cs->next = c_switch_stack;
8945 c_switch_stack = cs;
8947 return add_stmt (cs->switch_expr);
8950 /* Process a case label at location LOC. */
8952 tree
8953 do_case (location_t loc, tree low_value, tree high_value)
8955 tree label = NULL_TREE;
8957 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8959 low_value = c_fully_fold (low_value, false, NULL);
8960 if (TREE_CODE (low_value) == INTEGER_CST)
8961 pedwarn (input_location, OPT_pedantic,
8962 "case label is not an integer constant expression");
8965 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8967 high_value = c_fully_fold (high_value, false, NULL);
8968 if (TREE_CODE (high_value) == INTEGER_CST)
8969 pedwarn (input_location, OPT_pedantic,
8970 "case label is not an integer constant expression");
8973 if (c_switch_stack == NULL)
8975 if (low_value)
8976 error_at (loc, "case label not within a switch statement");
8977 else
8978 error_at (loc, "%<default%> label not within a switch statement");
8979 return NULL_TREE;
8982 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8983 EXPR_LOCATION (c_switch_stack->switch_expr),
8984 loc))
8985 return NULL_TREE;
8987 label = c_add_case_label (loc, c_switch_stack->cases,
8988 SWITCH_COND (c_switch_stack->switch_expr),
8989 c_switch_stack->orig_type,
8990 low_value, high_value);
8991 if (label == error_mark_node)
8992 label = NULL_TREE;
8993 return label;
8996 /* Finish the switch statement. */
8998 void
8999 c_finish_case (tree body)
9001 struct c_switch *cs = c_switch_stack;
9002 location_t switch_location;
9004 SWITCH_BODY (cs->switch_expr) = body;
9006 /* Emit warnings as needed. */
9007 switch_location = EXPR_LOCATION (cs->switch_expr);
9008 c_do_switch_warnings (cs->cases, switch_location,
9009 TREE_TYPE (cs->switch_expr),
9010 SWITCH_COND (cs->switch_expr));
9012 /* Pop the stack. */
9013 c_switch_stack = cs->next;
9014 splay_tree_delete (cs->cases);
9015 c_release_switch_bindings (cs->bindings);
9016 XDELETE (cs);
9019 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9020 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9021 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9022 statement, and was not surrounded with parenthesis. */
9024 void
9025 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9026 tree else_block, bool nested_if)
9028 tree stmt;
9030 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9031 if (warn_parentheses && nested_if && else_block == NULL)
9033 tree inner_if = then_block;
9035 /* We know from the grammar productions that there is an IF nested
9036 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9037 it might not be exactly THEN_BLOCK, but should be the last
9038 non-container statement within. */
9039 while (1)
9040 switch (TREE_CODE (inner_if))
9042 case COND_EXPR:
9043 goto found;
9044 case BIND_EXPR:
9045 inner_if = BIND_EXPR_BODY (inner_if);
9046 break;
9047 case STATEMENT_LIST:
9048 inner_if = expr_last (then_block);
9049 break;
9050 case TRY_FINALLY_EXPR:
9051 case TRY_CATCH_EXPR:
9052 inner_if = TREE_OPERAND (inner_if, 0);
9053 break;
9054 default:
9055 gcc_unreachable ();
9057 found:
9059 if (COND_EXPR_ELSE (inner_if))
9060 warning_at (if_locus, OPT_Wparentheses,
9061 "suggest explicit braces to avoid ambiguous %<else%>");
9064 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9065 SET_EXPR_LOCATION (stmt, if_locus);
9066 add_stmt (stmt);
9069 /* Emit a general-purpose loop construct. START_LOCUS is the location of
9070 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9071 is false for DO loops. INCR is the FOR increment expression. BODY is
9072 the statement controlled by the loop. BLAB is the break label. CLAB is
9073 the continue label. Everything is allowed to be NULL. */
9075 void
9076 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9077 tree blab, tree clab, bool cond_is_first)
9079 tree entry = NULL, exit = NULL, t;
9081 /* If the condition is zero don't generate a loop construct. */
9082 if (cond && integer_zerop (cond))
9084 if (cond_is_first)
9086 t = build_and_jump (&blab);
9087 SET_EXPR_LOCATION (t, start_locus);
9088 add_stmt (t);
9091 else
9093 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9095 /* If we have an exit condition, then we build an IF with gotos either
9096 out of the loop, or to the top of it. If there's no exit condition,
9097 then we just build a jump back to the top. */
9098 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9100 if (cond && !integer_nonzerop (cond))
9102 /* Canonicalize the loop condition to the end. This means
9103 generating a branch to the loop condition. Reuse the
9104 continue label, if possible. */
9105 if (cond_is_first)
9107 if (incr || !clab)
9109 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9110 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9112 else
9113 t = build1 (GOTO_EXPR, void_type_node, clab);
9114 SET_EXPR_LOCATION (t, start_locus);
9115 add_stmt (t);
9118 t = build_and_jump (&blab);
9119 if (cond_is_first)
9120 exit = fold_build3_loc (start_locus,
9121 COND_EXPR, void_type_node, cond, exit, t);
9122 else
9123 exit = fold_build3_loc (input_location,
9124 COND_EXPR, void_type_node, cond, exit, t);
9127 add_stmt (top);
9130 if (body)
9131 add_stmt (body);
9132 if (clab)
9133 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9134 if (incr)
9135 add_stmt (incr);
9136 if (entry)
9137 add_stmt (entry);
9138 if (exit)
9139 add_stmt (exit);
9140 if (blab)
9141 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9144 tree
9145 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9147 bool skip;
9148 tree label = *label_p;
9150 /* In switch statements break is sometimes stylistically used after
9151 a return statement. This can lead to spurious warnings about
9152 control reaching the end of a non-void function when it is
9153 inlined. Note that we are calling block_may_fallthru with
9154 language specific tree nodes; this works because
9155 block_may_fallthru returns true when given something it does not
9156 understand. */
9157 skip = !block_may_fallthru (cur_stmt_list);
9159 if (!label)
9161 if (!skip)
9162 *label_p = label = create_artificial_label (loc);
9164 else if (TREE_CODE (label) == LABEL_DECL)
9166 else switch (TREE_INT_CST_LOW (label))
9168 case 0:
9169 if (is_break)
9170 error_at (loc, "break statement not within loop or switch");
9171 else
9172 error_at (loc, "continue statement not within a loop");
9173 return NULL_TREE;
9175 case 1:
9176 gcc_assert (is_break);
9177 error_at (loc, "break statement used with OpenMP for loop");
9178 return NULL_TREE;
9180 default:
9181 gcc_unreachable ();
9184 if (skip)
9185 return NULL_TREE;
9187 if (!is_break)
9188 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9190 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9193 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
9195 static void
9196 emit_side_effect_warnings (location_t loc, tree expr)
9198 if (expr == error_mark_node)
9200 else if (!TREE_SIDE_EFFECTS (expr))
9202 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9203 warning_at (loc, OPT_Wunused_value, "statement with no effect");
9205 else
9206 warn_if_unused_value (expr, loc);
9209 /* Process an expression as if it were a complete statement. Emit
9210 diagnostics, but do not call ADD_STMT. LOC is the location of the
9211 statement. */
9213 tree
9214 c_process_expr_stmt (location_t loc, tree expr)
9216 tree exprv;
9218 if (!expr)
9219 return NULL_TREE;
9221 expr = c_fully_fold (expr, false, NULL);
9223 if (warn_sequence_point)
9224 verify_sequence_points (expr);
9226 if (TREE_TYPE (expr) != error_mark_node
9227 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9228 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9229 error_at (loc, "expression statement has incomplete type");
9231 /* If we're not processing a statement expression, warn about unused values.
9232 Warnings for statement expressions will be emitted later, once we figure
9233 out which is the result. */
9234 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9235 && warn_unused_value)
9236 emit_side_effect_warnings (loc, expr);
9238 exprv = expr;
9239 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9240 exprv = TREE_OPERAND (exprv, 1);
9241 while (CONVERT_EXPR_P (exprv))
9242 exprv = TREE_OPERAND (exprv, 0);
9243 if (DECL_P (exprv)
9244 || handled_component_p (exprv)
9245 || TREE_CODE (exprv) == ADDR_EXPR)
9246 mark_exp_read (exprv);
9248 /* If the expression is not of a type to which we cannot assign a line
9249 number, wrap the thing in a no-op NOP_EXPR. */
9250 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9252 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9253 SET_EXPR_LOCATION (expr, loc);
9256 return expr;
9259 /* Emit an expression as a statement. LOC is the location of the
9260 expression. */
9262 tree
9263 c_finish_expr_stmt (location_t loc, tree expr)
9265 if (expr)
9266 return add_stmt (c_process_expr_stmt (loc, expr));
9267 else
9268 return NULL;
9271 /* Do the opposite and emit a statement as an expression. To begin,
9272 create a new binding level and return it. */
9274 tree
9275 c_begin_stmt_expr (void)
9277 tree ret;
9279 /* We must force a BLOCK for this level so that, if it is not expanded
9280 later, there is a way to turn off the entire subtree of blocks that
9281 are contained in it. */
9282 keep_next_level ();
9283 ret = c_begin_compound_stmt (true);
9285 c_bindings_start_stmt_expr (c_switch_stack == NULL
9286 ? NULL
9287 : c_switch_stack->bindings);
9289 /* Mark the current statement list as belonging to a statement list. */
9290 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9292 return ret;
9295 /* LOC is the location of the compound statement to which this body
9296 belongs. */
9298 tree
9299 c_finish_stmt_expr (location_t loc, tree body)
9301 tree last, type, tmp, val;
9302 tree *last_p;
9304 body = c_end_compound_stmt (loc, body, true);
9306 c_bindings_end_stmt_expr (c_switch_stack == NULL
9307 ? NULL
9308 : c_switch_stack->bindings);
9310 /* Locate the last statement in BODY. See c_end_compound_stmt
9311 about always returning a BIND_EXPR. */
9312 last_p = &BIND_EXPR_BODY (body);
9313 last = BIND_EXPR_BODY (body);
9315 continue_searching:
9316 if (TREE_CODE (last) == STATEMENT_LIST)
9318 tree_stmt_iterator i;
9320 /* This can happen with degenerate cases like ({ }). No value. */
9321 if (!TREE_SIDE_EFFECTS (last))
9322 return body;
9324 /* If we're supposed to generate side effects warnings, process
9325 all of the statements except the last. */
9326 if (warn_unused_value)
9328 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9330 location_t tloc;
9331 tree t = tsi_stmt (i);
9333 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9334 emit_side_effect_warnings (tloc, t);
9337 else
9338 i = tsi_last (last);
9339 last_p = tsi_stmt_ptr (i);
9340 last = *last_p;
9343 /* If the end of the list is exception related, then the list was split
9344 by a call to push_cleanup. Continue searching. */
9345 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9346 || TREE_CODE (last) == TRY_CATCH_EXPR)
9348 last_p = &TREE_OPERAND (last, 0);
9349 last = *last_p;
9350 goto continue_searching;
9353 if (last == error_mark_node)
9354 return last;
9356 /* In the case that the BIND_EXPR is not necessary, return the
9357 expression out from inside it. */
9358 if (last == BIND_EXPR_BODY (body)
9359 && BIND_EXPR_VARS (body) == NULL)
9361 /* Even if this looks constant, do not allow it in a constant
9362 expression. */
9363 last = c_wrap_maybe_const (last, true);
9364 /* Do not warn if the return value of a statement expression is
9365 unused. */
9366 TREE_NO_WARNING (last) = 1;
9367 return last;
9370 /* Extract the type of said expression. */
9371 type = TREE_TYPE (last);
9373 /* If we're not returning a value at all, then the BIND_EXPR that
9374 we already have is a fine expression to return. */
9375 if (!type || VOID_TYPE_P (type))
9376 return body;
9378 /* Now that we've located the expression containing the value, it seems
9379 silly to make voidify_wrapper_expr repeat the process. Create a
9380 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9381 tmp = create_tmp_var_raw (type, NULL);
9383 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9384 tree_expr_nonnegative_p giving up immediately. */
9385 val = last;
9386 if (TREE_CODE (val) == NOP_EXPR
9387 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9388 val = TREE_OPERAND (val, 0);
9390 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9391 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9394 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9395 SET_EXPR_LOCATION (t, loc);
9396 return t;
9400 /* Begin and end compound statements. This is as simple as pushing
9401 and popping new statement lists from the tree. */
9403 tree
9404 c_begin_compound_stmt (bool do_scope)
9406 tree stmt = push_stmt_list ();
9407 if (do_scope)
9408 push_scope ();
9409 return stmt;
9412 /* End a compound statement. STMT is the statement. LOC is the
9413 location of the compound statement-- this is usually the location
9414 of the opening brace. */
9416 tree
9417 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9419 tree block = NULL;
9421 if (do_scope)
9423 if (c_dialect_objc ())
9424 objc_clear_super_receiver ();
9425 block = pop_scope ();
9428 stmt = pop_stmt_list (stmt);
9429 stmt = c_build_bind_expr (loc, block, stmt);
9431 /* If this compound statement is nested immediately inside a statement
9432 expression, then force a BIND_EXPR to be created. Otherwise we'll
9433 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9434 STATEMENT_LISTs merge, and thus we can lose track of what statement
9435 was really last. */
9436 if (building_stmt_list_p ()
9437 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9438 && TREE_CODE (stmt) != BIND_EXPR)
9440 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9441 TREE_SIDE_EFFECTS (stmt) = 1;
9442 SET_EXPR_LOCATION (stmt, loc);
9445 return stmt;
9448 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9449 when the current scope is exited. EH_ONLY is true when this is not
9450 meant to apply to normal control flow transfer. */
9452 void
9453 push_cleanup (tree decl, tree cleanup, bool eh_only)
9455 enum tree_code code;
9456 tree stmt, list;
9457 bool stmt_expr;
9459 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9460 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9461 add_stmt (stmt);
9462 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9463 list = push_stmt_list ();
9464 TREE_OPERAND (stmt, 0) = list;
9465 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9468 /* Convert scalar to vector for the range of operations. */
9469 static enum stv_conv
9470 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1)
9472 tree type0 = TREE_TYPE (op0);
9473 tree type1 = TREE_TYPE (op1);
9474 bool integer_only_op = false;
9475 enum stv_conv ret = stv_firstarg;
9477 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
9478 || TREE_CODE (type1) == VECTOR_TYPE);
9479 switch (code)
9481 case RSHIFT_EXPR:
9482 case LSHIFT_EXPR:
9483 if (TREE_CODE (type0) == INTEGER_TYPE
9484 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9486 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9488 error_at (loc, "conversion of scalar to vector "
9489 "involves truncation");
9490 return stv_error;
9492 else
9493 return stv_firstarg;
9495 break;
9497 case BIT_IOR_EXPR:
9498 case BIT_XOR_EXPR:
9499 case BIT_AND_EXPR:
9500 integer_only_op = true;
9501 /* ... fall through ... */
9503 case PLUS_EXPR:
9504 case MINUS_EXPR:
9505 case MULT_EXPR:
9506 case TRUNC_DIV_EXPR:
9507 case TRUNC_MOD_EXPR:
9508 case RDIV_EXPR:
9509 if (TREE_CODE (type0) == VECTOR_TYPE)
9511 tree tmp;
9512 ret = stv_secondarg;
9513 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
9514 tmp = type0; type0 = type1; type1 = tmp;
9515 tmp = op0; op0 = op1; op1 = tmp;
9518 if (TREE_CODE (type0) == INTEGER_TYPE
9519 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9521 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9523 error_at (loc, "conversion of scalar to vector "
9524 "involves truncation");
9525 return stv_error;
9527 return ret;
9529 else if (!integer_only_op
9530 /* Allow integer --> real conversion if safe. */
9531 && (TREE_CODE (type0) == REAL_TYPE
9532 || TREE_CODE (type0) == INTEGER_TYPE)
9533 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
9535 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9537 error_at (loc, "conversion of scalar to vector "
9538 "involves truncation");
9539 return stv_error;
9541 return ret;
9543 default:
9544 break;
9547 return stv_nothing;
9550 /* Build a binary-operation expression without default conversions.
9551 CODE is the kind of expression to build.
9552 LOCATION is the operator's location.
9553 This function differs from `build' in several ways:
9554 the data type of the result is computed and recorded in it,
9555 warnings are generated if arg data types are invalid,
9556 special handling for addition and subtraction of pointers is known,
9557 and some optimization is done (operations on narrow ints
9558 are done in the narrower type when that gives the same result).
9559 Constant folding is also done before the result is returned.
9561 Note that the operands will never have enumeral types, or function
9562 or array types, because either they will have the default conversions
9563 performed or they have both just been converted to some other type in which
9564 the arithmetic is to be done. */
9566 tree
9567 build_binary_op (location_t location, enum tree_code code,
9568 tree orig_op0, tree orig_op1, int convert_p)
9570 tree type0, type1, orig_type0, orig_type1;
9571 tree eptype;
9572 enum tree_code code0, code1;
9573 tree op0, op1;
9574 tree ret = error_mark_node;
9575 const char *invalid_op_diag;
9576 bool op0_int_operands, op1_int_operands;
9577 bool int_const, int_const_or_overflow, int_operands;
9579 /* Expression code to give to the expression when it is built.
9580 Normally this is CODE, which is what the caller asked for,
9581 but in some special cases we change it. */
9582 enum tree_code resultcode = code;
9584 /* Data type in which the computation is to be performed.
9585 In the simplest cases this is the common type of the arguments. */
9586 tree result_type = NULL;
9588 /* When the computation is in excess precision, the type of the
9589 final EXCESS_PRECISION_EXPR. */
9590 tree semantic_result_type = NULL;
9592 /* Nonzero means operands have already been type-converted
9593 in whatever way is necessary.
9594 Zero means they need to be converted to RESULT_TYPE. */
9595 int converted = 0;
9597 /* Nonzero means create the expression with this type, rather than
9598 RESULT_TYPE. */
9599 tree build_type = 0;
9601 /* Nonzero means after finally constructing the expression
9602 convert it to this type. */
9603 tree final_type = 0;
9605 /* Nonzero if this is an operation like MIN or MAX which can
9606 safely be computed in short if both args are promoted shorts.
9607 Also implies COMMON.
9608 -1 indicates a bitwise operation; this makes a difference
9609 in the exact conditions for when it is safe to do the operation
9610 in a narrower mode. */
9611 int shorten = 0;
9613 /* Nonzero if this is a comparison operation;
9614 if both args are promoted shorts, compare the original shorts.
9615 Also implies COMMON. */
9616 int short_compare = 0;
9618 /* Nonzero if this is a right-shift operation, which can be computed on the
9619 original short and then promoted if the operand is a promoted short. */
9620 int short_shift = 0;
9622 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9623 int common = 0;
9625 /* True means types are compatible as far as ObjC is concerned. */
9626 bool objc_ok;
9628 /* True means this is an arithmetic operation that may need excess
9629 precision. */
9630 bool may_need_excess_precision;
9632 /* True means this is a boolean operation that converts both its
9633 operands to truth-values. */
9634 bool boolean_op = false;
9636 if (location == UNKNOWN_LOCATION)
9637 location = input_location;
9639 op0 = orig_op0;
9640 op1 = orig_op1;
9642 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9643 if (op0_int_operands)
9644 op0 = remove_c_maybe_const_expr (op0);
9645 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9646 if (op1_int_operands)
9647 op1 = remove_c_maybe_const_expr (op1);
9648 int_operands = (op0_int_operands && op1_int_operands);
9649 if (int_operands)
9651 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9652 && TREE_CODE (orig_op1) == INTEGER_CST);
9653 int_const = (int_const_or_overflow
9654 && !TREE_OVERFLOW (orig_op0)
9655 && !TREE_OVERFLOW (orig_op1));
9657 else
9658 int_const = int_const_or_overflow = false;
9660 /* Do not apply default conversion in mixed vector/scalar expression. */
9661 if (convert_p
9662 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
9663 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
9665 op0 = default_conversion (op0);
9666 op1 = default_conversion (op1);
9669 orig_type0 = type0 = TREE_TYPE (op0);
9670 orig_type1 = type1 = TREE_TYPE (op1);
9672 /* The expression codes of the data types of the arguments tell us
9673 whether the arguments are integers, floating, pointers, etc. */
9674 code0 = TREE_CODE (type0);
9675 code1 = TREE_CODE (type1);
9677 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9678 STRIP_TYPE_NOPS (op0);
9679 STRIP_TYPE_NOPS (op1);
9681 /* If an error was already reported for one of the arguments,
9682 avoid reporting another error. */
9684 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9685 return error_mark_node;
9687 if ((invalid_op_diag
9688 = targetm.invalid_binary_op (code, type0, type1)))
9690 error_at (location, invalid_op_diag);
9691 return error_mark_node;
9694 switch (code)
9696 case PLUS_EXPR:
9697 case MINUS_EXPR:
9698 case MULT_EXPR:
9699 case TRUNC_DIV_EXPR:
9700 case CEIL_DIV_EXPR:
9701 case FLOOR_DIV_EXPR:
9702 case ROUND_DIV_EXPR:
9703 case EXACT_DIV_EXPR:
9704 may_need_excess_precision = true;
9705 break;
9706 default:
9707 may_need_excess_precision = false;
9708 break;
9710 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9712 op0 = TREE_OPERAND (op0, 0);
9713 type0 = TREE_TYPE (op0);
9715 else if (may_need_excess_precision
9716 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9718 type0 = eptype;
9719 op0 = convert (eptype, op0);
9721 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9723 op1 = TREE_OPERAND (op1, 0);
9724 type1 = TREE_TYPE (op1);
9726 else if (may_need_excess_precision
9727 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9729 type1 = eptype;
9730 op1 = convert (eptype, op1);
9733 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9735 /* In case when one of the operands of the binary operation is
9736 a vector and another is a scalar -- convert scalar to vector. */
9737 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
9739 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1);
9741 switch (convert_flag)
9743 case stv_error:
9744 return error_mark_node;
9745 case stv_firstarg:
9747 bool maybe_const = true;
9748 tree sc;
9749 sc = c_fully_fold (op0, false, &maybe_const);
9750 sc = save_expr (sc);
9751 sc = convert (TREE_TYPE (type1), sc);
9752 op0 = build_vector_from_val (type1, sc);
9753 if (!maybe_const)
9754 op0 = c_wrap_maybe_const (op0, true);
9755 orig_type0 = type0 = TREE_TYPE (op0);
9756 code0 = TREE_CODE (type0);
9757 converted = 1;
9758 break;
9760 case stv_secondarg:
9762 bool maybe_const = true;
9763 tree sc;
9764 sc = c_fully_fold (op1, false, &maybe_const);
9765 sc = save_expr (sc);
9766 sc = convert (TREE_TYPE (type0), sc);
9767 op1 = build_vector_from_val (type0, sc);
9768 if (!maybe_const)
9769 op1 = c_wrap_maybe_const (op1, true);
9770 orig_type1 = type1 = TREE_TYPE (op1);
9771 code1 = TREE_CODE (type1);
9772 converted = 1;
9773 break;
9775 default:
9776 break;
9780 switch (code)
9782 case PLUS_EXPR:
9783 /* Handle the pointer + int case. */
9784 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9786 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9787 goto return_build_binary_op;
9789 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9791 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9792 goto return_build_binary_op;
9794 else
9795 common = 1;
9796 break;
9798 case MINUS_EXPR:
9799 /* Subtraction of two similar pointers.
9800 We must subtract them as integers, then divide by object size. */
9801 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9802 && comp_target_types (location, type0, type1))
9804 ret = pointer_diff (location, op0, op1);
9805 goto return_build_binary_op;
9807 /* Handle pointer minus int. Just like pointer plus int. */
9808 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9810 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9811 goto return_build_binary_op;
9813 else
9814 common = 1;
9815 break;
9817 case MULT_EXPR:
9818 common = 1;
9819 break;
9821 case TRUNC_DIV_EXPR:
9822 case CEIL_DIV_EXPR:
9823 case FLOOR_DIV_EXPR:
9824 case ROUND_DIV_EXPR:
9825 case EXACT_DIV_EXPR:
9826 warn_for_div_by_zero (location, op1);
9828 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9829 || code0 == FIXED_POINT_TYPE
9830 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9831 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9832 || code1 == FIXED_POINT_TYPE
9833 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9835 enum tree_code tcode0 = code0, tcode1 = code1;
9837 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9838 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9839 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9840 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9842 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9843 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9844 resultcode = RDIV_EXPR;
9845 else
9846 /* Although it would be tempting to shorten always here, that
9847 loses on some targets, since the modulo instruction is
9848 undefined if the quotient can't be represented in the
9849 computation mode. We shorten only if unsigned or if
9850 dividing by something we know != -1. */
9851 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9852 || (TREE_CODE (op1) == INTEGER_CST
9853 && !integer_all_onesp (op1)));
9854 common = 1;
9856 break;
9858 case BIT_AND_EXPR:
9859 case BIT_IOR_EXPR:
9860 case BIT_XOR_EXPR:
9861 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9862 shorten = -1;
9863 /* Allow vector types which are not floating point types. */
9864 else if (code0 == VECTOR_TYPE
9865 && code1 == VECTOR_TYPE
9866 && !VECTOR_FLOAT_TYPE_P (type0)
9867 && !VECTOR_FLOAT_TYPE_P (type1))
9868 common = 1;
9869 break;
9871 case TRUNC_MOD_EXPR:
9872 case FLOOR_MOD_EXPR:
9873 warn_for_div_by_zero (location, op1);
9875 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9876 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9877 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9878 common = 1;
9879 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9881 /* Although it would be tempting to shorten always here, that loses
9882 on some targets, since the modulo instruction is undefined if the
9883 quotient can't be represented in the computation mode. We shorten
9884 only if unsigned or if dividing by something we know != -1. */
9885 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9886 || (TREE_CODE (op1) == INTEGER_CST
9887 && !integer_all_onesp (op1)));
9888 common = 1;
9890 break;
9892 case TRUTH_ANDIF_EXPR:
9893 case TRUTH_ORIF_EXPR:
9894 case TRUTH_AND_EXPR:
9895 case TRUTH_OR_EXPR:
9896 case TRUTH_XOR_EXPR:
9897 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9898 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9899 || code0 == FIXED_POINT_TYPE)
9900 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9901 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9902 || code1 == FIXED_POINT_TYPE))
9904 /* Result of these operations is always an int,
9905 but that does not mean the operands should be
9906 converted to ints! */
9907 result_type = integer_type_node;
9908 if (op0_int_operands)
9910 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
9911 op0 = remove_c_maybe_const_expr (op0);
9913 else
9914 op0 = c_objc_common_truthvalue_conversion (location, op0);
9915 if (op1_int_operands)
9917 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
9918 op1 = remove_c_maybe_const_expr (op1);
9920 else
9921 op1 = c_objc_common_truthvalue_conversion (location, op1);
9922 converted = 1;
9923 boolean_op = true;
9925 if (code == TRUTH_ANDIF_EXPR)
9927 int_const_or_overflow = (int_operands
9928 && TREE_CODE (orig_op0) == INTEGER_CST
9929 && (op0 == truthvalue_false_node
9930 || TREE_CODE (orig_op1) == INTEGER_CST));
9931 int_const = (int_const_or_overflow
9932 && !TREE_OVERFLOW (orig_op0)
9933 && (op0 == truthvalue_false_node
9934 || !TREE_OVERFLOW (orig_op1)));
9936 else if (code == TRUTH_ORIF_EXPR)
9938 int_const_or_overflow = (int_operands
9939 && TREE_CODE (orig_op0) == INTEGER_CST
9940 && (op0 == truthvalue_true_node
9941 || TREE_CODE (orig_op1) == INTEGER_CST));
9942 int_const = (int_const_or_overflow
9943 && !TREE_OVERFLOW (orig_op0)
9944 && (op0 == truthvalue_true_node
9945 || !TREE_OVERFLOW (orig_op1)));
9947 break;
9949 /* Shift operations: result has same type as first operand;
9950 always convert second operand to int.
9951 Also set SHORT_SHIFT if shifting rightward. */
9953 case RSHIFT_EXPR:
9954 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9955 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9957 result_type = type0;
9958 converted = 1;
9960 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9961 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9962 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9963 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9965 result_type = type0;
9966 converted = 1;
9968 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9969 && code1 == INTEGER_TYPE)
9971 if (TREE_CODE (op1) == INTEGER_CST)
9973 if (tree_int_cst_sgn (op1) < 0)
9975 int_const = false;
9976 if (c_inhibit_evaluation_warnings == 0)
9977 warning (0, "right shift count is negative");
9979 else
9981 if (!integer_zerop (op1))
9982 short_shift = 1;
9984 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9986 int_const = false;
9987 if (c_inhibit_evaluation_warnings == 0)
9988 warning (0, "right shift count >= width of type");
9993 /* Use the type of the value to be shifted. */
9994 result_type = type0;
9995 /* Convert the non vector shift-count to an integer, regardless
9996 of size of value being shifted. */
9997 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9998 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9999 op1 = convert (integer_type_node, op1);
10000 /* Avoid converting op1 to result_type later. */
10001 converted = 1;
10003 break;
10005 case LSHIFT_EXPR:
10006 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10007 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10009 result_type = type0;
10010 converted = 1;
10012 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10013 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10014 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10015 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10017 result_type = type0;
10018 converted = 1;
10020 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
10021 && code1 == INTEGER_TYPE)
10023 if (TREE_CODE (op1) == INTEGER_CST)
10025 if (tree_int_cst_sgn (op1) < 0)
10027 int_const = false;
10028 if (c_inhibit_evaluation_warnings == 0)
10029 warning (0, "left shift count is negative");
10032 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
10034 int_const = false;
10035 if (c_inhibit_evaluation_warnings == 0)
10036 warning (0, "left shift count >= width of type");
10040 /* Use the type of the value to be shifted. */
10041 result_type = type0;
10042 /* Convert the non vector shift-count to an integer, regardless
10043 of size of value being shifted. */
10044 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10045 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
10046 op1 = convert (integer_type_node, op1);
10047 /* Avoid converting op1 to result_type later. */
10048 converted = 1;
10050 break;
10052 case EQ_EXPR:
10053 case NE_EXPR:
10054 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10056 tree intt;
10057 if (TREE_TYPE (type0) != TREE_TYPE (type1))
10059 error_at (location, "comparing vectors with different "
10060 "element types");
10061 return error_mark_node;
10064 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10066 error_at (location, "comparing vectors with different "
10067 "number of elements");
10068 return error_mark_node;
10071 /* Always construct signed integer vector type. */
10072 intt = c_common_type_for_size (GET_MODE_BITSIZE
10073 (TYPE_MODE (TREE_TYPE (type0))), 0);
10074 result_type = build_opaque_vector_type (intt,
10075 TYPE_VECTOR_SUBPARTS (type0));
10076 converted = 1;
10077 break;
10079 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10080 warning_at (location,
10081 OPT_Wfloat_equal,
10082 "comparing floating point with == or != is unsafe");
10083 /* Result of comparison is always int,
10084 but don't convert the args to int! */
10085 build_type = integer_type_node;
10086 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10087 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10088 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10089 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10090 short_compare = 1;
10091 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10093 if (TREE_CODE (op0) == ADDR_EXPR
10094 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10096 if (code == EQ_EXPR)
10097 warning_at (location,
10098 OPT_Waddress,
10099 "the comparison will always evaluate as %<false%> "
10100 "for the address of %qD will never be NULL",
10101 TREE_OPERAND (op0, 0));
10102 else
10103 warning_at (location,
10104 OPT_Waddress,
10105 "the comparison will always evaluate as %<true%> "
10106 "for the address of %qD will never be NULL",
10107 TREE_OPERAND (op0, 0));
10109 result_type = type0;
10111 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10113 if (TREE_CODE (op1) == ADDR_EXPR
10114 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10116 if (code == EQ_EXPR)
10117 warning_at (location,
10118 OPT_Waddress,
10119 "the comparison will always evaluate as %<false%> "
10120 "for the address of %qD will never be NULL",
10121 TREE_OPERAND (op1, 0));
10122 else
10123 warning_at (location,
10124 OPT_Waddress,
10125 "the comparison will always evaluate as %<true%> "
10126 "for the address of %qD will never be NULL",
10127 TREE_OPERAND (op1, 0));
10129 result_type = type1;
10131 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10133 tree tt0 = TREE_TYPE (type0);
10134 tree tt1 = TREE_TYPE (type1);
10135 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10136 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10137 addr_space_t as_common = ADDR_SPACE_GENERIC;
10139 /* Anything compares with void *. void * compares with anything.
10140 Otherwise, the targets must be compatible
10141 and both must be object or both incomplete. */
10142 if (comp_target_types (location, type0, type1))
10143 result_type = common_pointer_type (type0, type1);
10144 else if (!addr_space_superset (as0, as1, &as_common))
10146 error_at (location, "comparison of pointers to "
10147 "disjoint address spaces");
10148 return error_mark_node;
10150 else if (VOID_TYPE_P (tt0))
10152 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10153 pedwarn (location, OPT_pedantic, "ISO C forbids "
10154 "comparison of %<void *%> with function pointer");
10156 else if (VOID_TYPE_P (tt1))
10158 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10159 pedwarn (location, OPT_pedantic, "ISO C forbids "
10160 "comparison of %<void *%> with function pointer");
10162 else
10163 /* Avoid warning about the volatile ObjC EH puts on decls. */
10164 if (!objc_ok)
10165 pedwarn (location, 0,
10166 "comparison of distinct pointer types lacks a cast");
10168 if (result_type == NULL_TREE)
10170 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10171 result_type = build_pointer_type
10172 (build_qualified_type (void_type_node, qual));
10175 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10177 result_type = type0;
10178 pedwarn (location, 0, "comparison between pointer and integer");
10180 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10182 result_type = type1;
10183 pedwarn (location, 0, "comparison between pointer and integer");
10185 break;
10187 case LE_EXPR:
10188 case GE_EXPR:
10189 case LT_EXPR:
10190 case GT_EXPR:
10191 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10193 tree intt;
10194 if (TREE_TYPE (type0) != TREE_TYPE (type1))
10196 error_at (location, "comparing vectors with different "
10197 "element types");
10198 return error_mark_node;
10201 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10203 error_at (location, "comparing vectors with different "
10204 "number of elements");
10205 return error_mark_node;
10208 /* Always construct signed integer vector type. */
10209 intt = c_common_type_for_size (GET_MODE_BITSIZE
10210 (TYPE_MODE (TREE_TYPE (type0))), 0);
10211 result_type = build_opaque_vector_type (intt,
10212 TYPE_VECTOR_SUBPARTS (type0));
10213 converted = 1;
10214 break;
10216 build_type = integer_type_node;
10217 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10218 || code0 == FIXED_POINT_TYPE)
10219 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10220 || code1 == FIXED_POINT_TYPE))
10221 short_compare = 1;
10222 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10224 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10225 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10226 addr_space_t as_common;
10228 if (comp_target_types (location, type0, type1))
10230 result_type = common_pointer_type (type0, type1);
10231 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10232 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10233 pedwarn (location, 0,
10234 "comparison of complete and incomplete pointers");
10235 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10236 pedwarn (location, OPT_pedantic, "ISO C forbids "
10237 "ordered comparisons of pointers to functions");
10238 else if (null_pointer_constant_p (orig_op0)
10239 || null_pointer_constant_p (orig_op1))
10240 warning_at (location, OPT_Wextra,
10241 "ordered comparison of pointer with null pointer");
10244 else if (!addr_space_superset (as0, as1, &as_common))
10246 error_at (location, "comparison of pointers to "
10247 "disjoint address spaces");
10248 return error_mark_node;
10250 else
10252 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10253 result_type = build_pointer_type
10254 (build_qualified_type (void_type_node, qual));
10255 pedwarn (location, 0,
10256 "comparison of distinct pointer types lacks a cast");
10259 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10261 result_type = type0;
10262 if (pedantic)
10263 pedwarn (location, OPT_pedantic,
10264 "ordered comparison of pointer with integer zero");
10265 else if (extra_warnings)
10266 warning_at (location, OPT_Wextra,
10267 "ordered comparison of pointer with integer zero");
10269 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10271 result_type = type1;
10272 if (pedantic)
10273 pedwarn (location, OPT_pedantic,
10274 "ordered comparison of pointer with integer zero");
10275 else if (extra_warnings)
10276 warning_at (location, OPT_Wextra,
10277 "ordered comparison of pointer with integer zero");
10279 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10281 result_type = type0;
10282 pedwarn (location, 0, "comparison between pointer and integer");
10284 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10286 result_type = type1;
10287 pedwarn (location, 0, "comparison between pointer and integer");
10289 break;
10291 default:
10292 gcc_unreachable ();
10295 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10296 return error_mark_node;
10298 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10299 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10300 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10301 TREE_TYPE (type1))))
10303 binary_op_error (location, code, type0, type1);
10304 return error_mark_node;
10307 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10308 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10310 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10311 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10313 bool first_complex = (code0 == COMPLEX_TYPE);
10314 bool second_complex = (code1 == COMPLEX_TYPE);
10315 int none_complex = (!first_complex && !second_complex);
10317 if (shorten || common || short_compare)
10319 result_type = c_common_type (type0, type1);
10320 do_warn_double_promotion (result_type, type0, type1,
10321 "implicit conversion from %qT to %qT "
10322 "to match other operand of binary "
10323 "expression",
10324 location);
10325 if (result_type == error_mark_node)
10326 return error_mark_node;
10329 if (first_complex != second_complex
10330 && (code == PLUS_EXPR
10331 || code == MINUS_EXPR
10332 || code == MULT_EXPR
10333 || (code == TRUNC_DIV_EXPR && first_complex))
10334 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10335 && flag_signed_zeros)
10337 /* An operation on mixed real/complex operands must be
10338 handled specially, but the language-independent code can
10339 more easily optimize the plain complex arithmetic if
10340 -fno-signed-zeros. */
10341 tree real_type = TREE_TYPE (result_type);
10342 tree real, imag;
10343 if (type0 != orig_type0 || type1 != orig_type1)
10345 gcc_assert (may_need_excess_precision && common);
10346 semantic_result_type = c_common_type (orig_type0, orig_type1);
10348 if (first_complex)
10350 if (TREE_TYPE (op0) != result_type)
10351 op0 = convert_and_check (result_type, op0);
10352 if (TREE_TYPE (op1) != real_type)
10353 op1 = convert_and_check (real_type, op1);
10355 else
10357 if (TREE_TYPE (op0) != real_type)
10358 op0 = convert_and_check (real_type, op0);
10359 if (TREE_TYPE (op1) != result_type)
10360 op1 = convert_and_check (result_type, op1);
10362 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10363 return error_mark_node;
10364 if (first_complex)
10366 op0 = c_save_expr (op0);
10367 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10368 op0, 1);
10369 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10370 op0, 1);
10371 switch (code)
10373 case MULT_EXPR:
10374 case TRUNC_DIV_EXPR:
10375 op1 = c_save_expr (op1);
10376 imag = build2 (resultcode, real_type, imag, op1);
10377 /* Fall through. */
10378 case PLUS_EXPR:
10379 case MINUS_EXPR:
10380 real = build2 (resultcode, real_type, real, op1);
10381 break;
10382 default:
10383 gcc_unreachable();
10386 else
10388 op1 = c_save_expr (op1);
10389 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10390 op1, 1);
10391 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10392 op1, 1);
10393 switch (code)
10395 case MULT_EXPR:
10396 op0 = c_save_expr (op0);
10397 imag = build2 (resultcode, real_type, op0, imag);
10398 /* Fall through. */
10399 case PLUS_EXPR:
10400 real = build2 (resultcode, real_type, op0, real);
10401 break;
10402 case MINUS_EXPR:
10403 real = build2 (resultcode, real_type, op0, real);
10404 imag = build1 (NEGATE_EXPR, real_type, imag);
10405 break;
10406 default:
10407 gcc_unreachable();
10410 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10411 goto return_build_binary_op;
10414 /* For certain operations (which identify themselves by shorten != 0)
10415 if both args were extended from the same smaller type,
10416 do the arithmetic in that type and then extend.
10418 shorten !=0 and !=1 indicates a bitwise operation.
10419 For them, this optimization is safe only if
10420 both args are zero-extended or both are sign-extended.
10421 Otherwise, we might change the result.
10422 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10423 but calculated in (unsigned short) it would be (unsigned short)-1. */
10425 if (shorten && none_complex)
10427 final_type = result_type;
10428 result_type = shorten_binary_op (result_type, op0, op1,
10429 shorten == -1);
10432 /* Shifts can be shortened if shifting right. */
10434 if (short_shift)
10436 int unsigned_arg;
10437 tree arg0 = get_narrower (op0, &unsigned_arg);
10439 final_type = result_type;
10441 if (arg0 == op0 && final_type == TREE_TYPE (op0))
10442 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10444 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10445 && tree_int_cst_sgn (op1) > 0
10446 /* We can shorten only if the shift count is less than the
10447 number of bits in the smaller type size. */
10448 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10449 /* We cannot drop an unsigned shift after sign-extension. */
10450 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10452 /* Do an unsigned shift if the operand was zero-extended. */
10453 result_type
10454 = c_common_signed_or_unsigned_type (unsigned_arg,
10455 TREE_TYPE (arg0));
10456 /* Convert value-to-be-shifted to that type. */
10457 if (TREE_TYPE (op0) != result_type)
10458 op0 = convert (result_type, op0);
10459 converted = 1;
10463 /* Comparison operations are shortened too but differently.
10464 They identify themselves by setting short_compare = 1. */
10466 if (short_compare)
10468 /* Don't write &op0, etc., because that would prevent op0
10469 from being kept in a register.
10470 Instead, make copies of the our local variables and
10471 pass the copies by reference, then copy them back afterward. */
10472 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10473 enum tree_code xresultcode = resultcode;
10474 tree val
10475 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10477 if (val != 0)
10479 ret = val;
10480 goto return_build_binary_op;
10483 op0 = xop0, op1 = xop1;
10484 converted = 1;
10485 resultcode = xresultcode;
10487 if (c_inhibit_evaluation_warnings == 0)
10489 bool op0_maybe_const = true;
10490 bool op1_maybe_const = true;
10491 tree orig_op0_folded, orig_op1_folded;
10493 if (in_late_binary_op)
10495 orig_op0_folded = orig_op0;
10496 orig_op1_folded = orig_op1;
10498 else
10500 /* Fold for the sake of possible warnings, as in
10501 build_conditional_expr. This requires the
10502 "original" values to be folded, not just op0 and
10503 op1. */
10504 c_inhibit_evaluation_warnings++;
10505 op0 = c_fully_fold (op0, require_constant_value,
10506 &op0_maybe_const);
10507 op1 = c_fully_fold (op1, require_constant_value,
10508 &op1_maybe_const);
10509 c_inhibit_evaluation_warnings--;
10510 orig_op0_folded = c_fully_fold (orig_op0,
10511 require_constant_value,
10512 NULL);
10513 orig_op1_folded = c_fully_fold (orig_op1,
10514 require_constant_value,
10515 NULL);
10518 if (warn_sign_compare)
10519 warn_for_sign_compare (location, orig_op0_folded,
10520 orig_op1_folded, op0, op1,
10521 result_type, resultcode);
10522 if (!in_late_binary_op && !int_operands)
10524 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10525 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10526 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10527 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10533 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10534 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10535 Then the expression will be built.
10536 It will be given type FINAL_TYPE if that is nonzero;
10537 otherwise, it will be given type RESULT_TYPE. */
10539 if (!result_type)
10541 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10542 return error_mark_node;
10545 if (build_type == NULL_TREE)
10547 build_type = result_type;
10548 if ((type0 != orig_type0 || type1 != orig_type1)
10549 && !boolean_op)
10551 gcc_assert (may_need_excess_precision && common);
10552 semantic_result_type = c_common_type (orig_type0, orig_type1);
10556 if (!converted)
10558 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10559 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10561 /* This can happen if one operand has a vector type, and the other
10562 has a different type. */
10563 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10564 return error_mark_node;
10567 /* Treat expressions in initializers specially as they can't trap. */
10568 if (int_const_or_overflow)
10569 ret = (require_constant_value
10570 ? fold_build2_initializer_loc (location, resultcode, build_type,
10571 op0, op1)
10572 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10573 else
10574 ret = build2 (resultcode, build_type, op0, op1);
10575 if (final_type != 0)
10576 ret = convert (final_type, ret);
10578 return_build_binary_op:
10579 gcc_assert (ret != error_mark_node);
10580 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10581 ret = (int_operands
10582 ? note_integer_operands (ret)
10583 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10584 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10585 && !in_late_binary_op)
10586 ret = note_integer_operands (ret);
10587 if (semantic_result_type)
10588 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10589 protected_set_expr_location (ret, location);
10590 return ret;
10594 /* Convert EXPR to be a truth-value, validating its type for this
10595 purpose. LOCATION is the source location for the expression. */
10597 tree
10598 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10600 bool int_const, int_operands;
10602 switch (TREE_CODE (TREE_TYPE (expr)))
10604 case ARRAY_TYPE:
10605 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10606 return error_mark_node;
10608 case RECORD_TYPE:
10609 error_at (location, "used struct type value where scalar is required");
10610 return error_mark_node;
10612 case UNION_TYPE:
10613 error_at (location, "used union type value where scalar is required");
10614 return error_mark_node;
10616 case VOID_TYPE:
10617 error_at (location, "void value not ignored as it ought to be");
10618 return error_mark_node;
10620 case FUNCTION_TYPE:
10621 gcc_unreachable ();
10623 case VECTOR_TYPE:
10624 error_at (location, "used vector type where scalar is required");
10625 return error_mark_node;
10627 default:
10628 break;
10631 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10632 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10633 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
10635 expr = remove_c_maybe_const_expr (expr);
10636 expr = build2 (NE_EXPR, integer_type_node, expr,
10637 convert (TREE_TYPE (expr), integer_zero_node));
10638 expr = note_integer_operands (expr);
10640 else
10641 /* ??? Should we also give an error for vectors rather than leaving
10642 those to give errors later? */
10643 expr = c_common_truthvalue_conversion (location, expr);
10645 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10647 if (TREE_OVERFLOW (expr))
10648 return expr;
10649 else
10650 return note_integer_operands (expr);
10652 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10653 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10654 return expr;
10658 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10659 required. */
10661 tree
10662 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10664 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10666 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10667 /* Executing a compound literal inside a function reinitializes
10668 it. */
10669 if (!TREE_STATIC (decl))
10670 *se = true;
10671 return decl;
10673 else
10674 return expr;
10677 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10679 tree
10680 c_begin_omp_parallel (void)
10682 tree block;
10684 keep_next_level ();
10685 block = c_begin_compound_stmt (true);
10687 return block;
10690 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10691 statement. LOC is the location of the OMP_PARALLEL. */
10693 tree
10694 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10696 tree stmt;
10698 block = c_end_compound_stmt (loc, block, true);
10700 stmt = make_node (OMP_PARALLEL);
10701 TREE_TYPE (stmt) = void_type_node;
10702 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10703 OMP_PARALLEL_BODY (stmt) = block;
10704 SET_EXPR_LOCATION (stmt, loc);
10706 return add_stmt (stmt);
10709 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10711 tree
10712 c_begin_omp_task (void)
10714 tree block;
10716 keep_next_level ();
10717 block = c_begin_compound_stmt (true);
10719 return block;
10722 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10723 statement. LOC is the location of the #pragma. */
10725 tree
10726 c_finish_omp_task (location_t loc, tree clauses, tree block)
10728 tree stmt;
10730 block = c_end_compound_stmt (loc, block, true);
10732 stmt = make_node (OMP_TASK);
10733 TREE_TYPE (stmt) = void_type_node;
10734 OMP_TASK_CLAUSES (stmt) = clauses;
10735 OMP_TASK_BODY (stmt) = block;
10736 SET_EXPR_LOCATION (stmt, loc);
10738 return add_stmt (stmt);
10741 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10742 Remove any elements from the list that are invalid. */
10744 tree
10745 c_finish_omp_clauses (tree clauses)
10747 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10748 tree c, t, *pc = &clauses;
10749 const char *name;
10751 bitmap_obstack_initialize (NULL);
10752 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10753 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10754 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10756 for (pc = &clauses, c = clauses; c ; c = *pc)
10758 bool remove = false;
10759 bool need_complete = false;
10760 bool need_implicitly_determined = false;
10762 switch (OMP_CLAUSE_CODE (c))
10764 case OMP_CLAUSE_SHARED:
10765 name = "shared";
10766 need_implicitly_determined = true;
10767 goto check_dup_generic;
10769 case OMP_CLAUSE_PRIVATE:
10770 name = "private";
10771 need_complete = true;
10772 need_implicitly_determined = true;
10773 goto check_dup_generic;
10775 case OMP_CLAUSE_REDUCTION:
10776 name = "reduction";
10777 need_implicitly_determined = true;
10778 t = OMP_CLAUSE_DECL (c);
10779 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10780 || POINTER_TYPE_P (TREE_TYPE (t)))
10782 error_at (OMP_CLAUSE_LOCATION (c),
10783 "%qE has invalid type for %<reduction%>", t);
10784 remove = true;
10786 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10788 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10789 const char *r_name = NULL;
10791 switch (r_code)
10793 case PLUS_EXPR:
10794 case MULT_EXPR:
10795 case MINUS_EXPR:
10796 case MIN_EXPR:
10797 case MAX_EXPR:
10798 break;
10799 case BIT_AND_EXPR:
10800 r_name = "&";
10801 break;
10802 case BIT_XOR_EXPR:
10803 r_name = "^";
10804 break;
10805 case BIT_IOR_EXPR:
10806 r_name = "|";
10807 break;
10808 case TRUTH_ANDIF_EXPR:
10809 r_name = "&&";
10810 break;
10811 case TRUTH_ORIF_EXPR:
10812 r_name = "||";
10813 break;
10814 default:
10815 gcc_unreachable ();
10817 if (r_name)
10819 error_at (OMP_CLAUSE_LOCATION (c),
10820 "%qE has invalid type for %<reduction(%s)%>",
10821 t, r_name);
10822 remove = true;
10825 goto check_dup_generic;
10827 case OMP_CLAUSE_COPYPRIVATE:
10828 name = "copyprivate";
10829 goto check_dup_generic;
10831 case OMP_CLAUSE_COPYIN:
10832 name = "copyin";
10833 t = OMP_CLAUSE_DECL (c);
10834 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10836 error_at (OMP_CLAUSE_LOCATION (c),
10837 "%qE must be %<threadprivate%> for %<copyin%>", t);
10838 remove = true;
10840 goto check_dup_generic;
10842 check_dup_generic:
10843 t = OMP_CLAUSE_DECL (c);
10844 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10846 error_at (OMP_CLAUSE_LOCATION (c),
10847 "%qE is not a variable in clause %qs", t, name);
10848 remove = true;
10850 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10851 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10852 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10854 error_at (OMP_CLAUSE_LOCATION (c),
10855 "%qE appears more than once in data clauses", t);
10856 remove = true;
10858 else
10859 bitmap_set_bit (&generic_head, DECL_UID (t));
10860 break;
10862 case OMP_CLAUSE_FIRSTPRIVATE:
10863 name = "firstprivate";
10864 t = OMP_CLAUSE_DECL (c);
10865 need_complete = true;
10866 need_implicitly_determined = true;
10867 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10869 error_at (OMP_CLAUSE_LOCATION (c),
10870 "%qE is not a variable in clause %<firstprivate%>", t);
10871 remove = true;
10873 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10874 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10876 error_at (OMP_CLAUSE_LOCATION (c),
10877 "%qE appears more than once in data clauses", t);
10878 remove = true;
10880 else
10881 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10882 break;
10884 case OMP_CLAUSE_LASTPRIVATE:
10885 name = "lastprivate";
10886 t = OMP_CLAUSE_DECL (c);
10887 need_complete = true;
10888 need_implicitly_determined = true;
10889 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10891 error_at (OMP_CLAUSE_LOCATION (c),
10892 "%qE is not a variable in clause %<lastprivate%>", t);
10893 remove = true;
10895 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10896 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10898 error_at (OMP_CLAUSE_LOCATION (c),
10899 "%qE appears more than once in data clauses", t);
10900 remove = true;
10902 else
10903 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10904 break;
10906 case OMP_CLAUSE_IF:
10907 case OMP_CLAUSE_NUM_THREADS:
10908 case OMP_CLAUSE_SCHEDULE:
10909 case OMP_CLAUSE_NOWAIT:
10910 case OMP_CLAUSE_ORDERED:
10911 case OMP_CLAUSE_DEFAULT:
10912 case OMP_CLAUSE_UNTIED:
10913 case OMP_CLAUSE_COLLAPSE:
10914 case OMP_CLAUSE_FINAL:
10915 case OMP_CLAUSE_MERGEABLE:
10916 pc = &OMP_CLAUSE_CHAIN (c);
10917 continue;
10919 default:
10920 gcc_unreachable ();
10923 if (!remove)
10925 t = OMP_CLAUSE_DECL (c);
10927 if (need_complete)
10929 t = require_complete_type (t);
10930 if (t == error_mark_node)
10931 remove = true;
10934 if (need_implicitly_determined)
10936 const char *share_name = NULL;
10938 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10939 share_name = "threadprivate";
10940 else switch (c_omp_predetermined_sharing (t))
10942 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10943 break;
10944 case OMP_CLAUSE_DEFAULT_SHARED:
10945 /* const vars may be specified in firstprivate clause. */
10946 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10947 && TREE_READONLY (t))
10948 break;
10949 share_name = "shared";
10950 break;
10951 case OMP_CLAUSE_DEFAULT_PRIVATE:
10952 share_name = "private";
10953 break;
10954 default:
10955 gcc_unreachable ();
10957 if (share_name)
10959 error_at (OMP_CLAUSE_LOCATION (c),
10960 "%qE is predetermined %qs for %qs",
10961 t, share_name, name);
10962 remove = true;
10967 if (remove)
10968 *pc = OMP_CLAUSE_CHAIN (c);
10969 else
10970 pc = &OMP_CLAUSE_CHAIN (c);
10973 bitmap_obstack_release (NULL);
10974 return clauses;
10977 /* Create a transaction node. */
10979 tree
10980 c_finish_transaction (location_t loc, tree block, int flags)
10982 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
10983 if (flags & TM_STMT_ATTR_OUTER)
10984 TRANSACTION_EXPR_OUTER (stmt) = 1;
10985 if (flags & TM_STMT_ATTR_RELAXED)
10986 TRANSACTION_EXPR_RELAXED (stmt) = 1;
10987 return add_stmt (stmt);
10990 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10991 down to the element type of an array. */
10993 tree
10994 c_build_qualified_type (tree type, int type_quals)
10996 if (type == error_mark_node)
10997 return type;
10999 if (TREE_CODE (type) == ARRAY_TYPE)
11001 tree t;
11002 tree element_type = c_build_qualified_type (TREE_TYPE (type),
11003 type_quals);
11005 /* See if we already have an identically qualified type. */
11006 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11008 if (TYPE_QUALS (strip_array_types (t)) == type_quals
11009 && TYPE_NAME (t) == TYPE_NAME (type)
11010 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
11011 && attribute_list_equal (TYPE_ATTRIBUTES (t),
11012 TYPE_ATTRIBUTES (type)))
11013 break;
11015 if (!t)
11017 tree domain = TYPE_DOMAIN (type);
11019 t = build_variant_type_copy (type);
11020 TREE_TYPE (t) = element_type;
11022 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
11023 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
11024 SET_TYPE_STRUCTURAL_EQUALITY (t);
11025 else if (TYPE_CANONICAL (element_type) != element_type
11026 || (domain && TYPE_CANONICAL (domain) != domain))
11028 tree unqualified_canon
11029 = build_array_type (TYPE_CANONICAL (element_type),
11030 domain? TYPE_CANONICAL (domain)
11031 : NULL_TREE);
11032 TYPE_CANONICAL (t)
11033 = c_build_qualified_type (unqualified_canon, type_quals);
11035 else
11036 TYPE_CANONICAL (t) = t;
11038 return t;
11041 /* A restrict-qualified pointer type must be a pointer to object or
11042 incomplete type. Note that the use of POINTER_TYPE_P also allows
11043 REFERENCE_TYPEs, which is appropriate for C++. */
11044 if ((type_quals & TYPE_QUAL_RESTRICT)
11045 && (!POINTER_TYPE_P (type)
11046 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
11048 error ("invalid use of %<restrict%>");
11049 type_quals &= ~TYPE_QUAL_RESTRICT;
11052 return build_qualified_type (type, type_quals);
11055 /* Build a VA_ARG_EXPR for the C parser. */
11057 tree
11058 c_build_va_arg (location_t loc, tree expr, tree type)
11060 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
11061 warning_at (loc, OPT_Wc___compat,
11062 "C++ requires promoted type, not enum type, in %<va_arg%>");
11063 return build_va_arg (loc, expr, type);