Merge branch 'master' into python
[official-gcc.git] / gcc / c-typeck.c
blob08a3825a436f67bb157c61ec39e5b43911cbf649
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "langhooks.h"
34 #include "c-tree.h"
35 #include "c-lang.h"
36 #include "flags.h"
37 #include "output.h"
38 #include "toplev.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "tree-iterator.h"
42 #include "bitmap.h"
43 #include "gimple.h"
45 /* Possible cases of implicit bad conversions. Used to select
46 diagnostic messages in convert_for_assignment. */
47 enum impl_conv {
48 ic_argpass,
49 ic_assign,
50 ic_init,
51 ic_return
54 /* Whether we are building a boolean conversion inside
55 convert_for_assignment, or some other late binary operation. If
56 build_binary_op is called (from code shared with C++) in this case,
57 then the operands have already been folded and the result will not
58 be folded again, so C_MAYBE_CONST_EXPR should not be generated. */
59 bool in_late_binary_op;
61 /* The level of nesting inside "__alignof__". */
62 int in_alignof;
64 /* The level of nesting inside "sizeof". */
65 int in_sizeof;
67 /* The level of nesting inside "typeof". */
68 int in_typeof;
70 /* Nonzero if we've already printed a "missing braces around initializer"
71 message within this initializer. */
72 static int missing_braces_mentioned;
74 static int require_constant_value;
75 static int require_constant_elements;
77 static bool null_pointer_constant_p (const_tree);
78 static tree qualify_type (tree, tree);
79 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
80 bool *);
81 static int comp_target_types (location_t, tree, tree);
82 static int function_types_compatible_p (const_tree, const_tree, bool *,
83 bool *);
84 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
85 static tree lookup_field (tree, tree);
86 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
87 tree);
88 static tree pointer_diff (location_t, tree, tree);
89 static tree convert_for_assignment (location_t, tree, tree, tree,
90 enum impl_conv, bool, tree, tree, int);
91 static tree valid_compound_expr_initializer (tree, tree);
92 static void push_string (const char *);
93 static void push_member_name (tree);
94 static int spelling_length (void);
95 static char *print_spelling (char *);
96 static void warning_init (int, const char *);
97 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
98 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
99 struct obstack *);
100 static void output_pending_init_elements (int, struct obstack *);
101 static int set_designator (int, struct obstack *);
102 static void push_range_stack (tree, struct obstack *);
103 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
104 static void set_nonincremental_init (struct obstack *);
105 static void set_nonincremental_init_from_string (tree, struct obstack *);
106 static tree find_init_member (tree, struct obstack *);
107 static void readonly_error (tree, enum lvalue_use);
108 static void readonly_warning (tree, enum lvalue_use);
109 static int lvalue_or_else (const_tree, enum lvalue_use);
110 static void record_maybe_used_decl (tree);
111 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
113 /* Return true if EXP is a null pointer constant, false otherwise. */
115 static bool
116 null_pointer_constant_p (const_tree expr)
118 /* This should really operate on c_expr structures, but they aren't
119 yet available everywhere required. */
120 tree type = TREE_TYPE (expr);
121 return (TREE_CODE (expr) == INTEGER_CST
122 && !TREE_OVERFLOW (expr)
123 && integer_zerop (expr)
124 && (INTEGRAL_TYPE_P (type)
125 || (TREE_CODE (type) == POINTER_TYPE
126 && VOID_TYPE_P (TREE_TYPE (type))
127 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
130 /* EXPR may appear in an unevaluated part of an integer constant
131 expression, but not in an evaluated part. Wrap it in a
132 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
133 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
135 static tree
136 note_integer_operands (tree expr)
138 tree ret;
139 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
141 ret = copy_node (expr);
142 TREE_OVERFLOW (ret) = 1;
144 else
146 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
147 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
149 return ret;
152 /* Having checked whether EXPR may appear in an unevaluated part of an
153 integer constant expression and found that it may, remove any
154 C_MAYBE_CONST_EXPR noting this fact and return the resulting
155 expression. */
157 static inline tree
158 remove_c_maybe_const_expr (tree expr)
160 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
161 return C_MAYBE_CONST_EXPR_EXPR (expr);
162 else
163 return expr;
166 \f/* This is a cache to hold if two types are compatible or not. */
168 struct tagged_tu_seen_cache {
169 const struct tagged_tu_seen_cache * next;
170 const_tree t1;
171 const_tree t2;
172 /* The return value of tagged_types_tu_compatible_p if we had seen
173 these two types already. */
174 int val;
177 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
178 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
180 /* Do `exp = require_complete_type (exp);' to make sure exp
181 does not have an incomplete type. (That includes void types.) */
183 tree
184 require_complete_type (tree value)
186 tree type = TREE_TYPE (value);
188 if (value == error_mark_node || type == error_mark_node)
189 return error_mark_node;
191 /* First, detect a valid value with a complete type. */
192 if (COMPLETE_TYPE_P (type))
193 return value;
195 c_incomplete_type_error (value, type);
196 return error_mark_node;
199 /* Print an error message for invalid use of an incomplete type.
200 VALUE is the expression that was used (or 0 if that isn't known)
201 and TYPE is the type that was invalid. */
203 void
204 c_incomplete_type_error (const_tree value, const_tree type)
206 const char *type_code_string;
208 /* Avoid duplicate error message. */
209 if (TREE_CODE (type) == ERROR_MARK)
210 return;
212 if (value != 0 && (TREE_CODE (value) == VAR_DECL
213 || TREE_CODE (value) == PARM_DECL))
214 error ("%qD has an incomplete type", value);
215 else
217 retry:
218 /* We must print an error message. Be clever about what it says. */
220 switch (TREE_CODE (type))
222 case RECORD_TYPE:
223 type_code_string = "struct";
224 break;
226 case UNION_TYPE:
227 type_code_string = "union";
228 break;
230 case ENUMERAL_TYPE:
231 type_code_string = "enum";
232 break;
234 case VOID_TYPE:
235 error ("invalid use of void expression");
236 return;
238 case ARRAY_TYPE:
239 if (TYPE_DOMAIN (type))
241 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
243 error ("invalid use of flexible array member");
244 return;
246 type = TREE_TYPE (type);
247 goto retry;
249 error ("invalid use of array with unspecified bounds");
250 return;
252 default:
253 gcc_unreachable ();
256 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
257 error ("invalid use of undefined type %<%s %E%>",
258 type_code_string, TYPE_NAME (type));
259 else
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
261 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
265 /* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
268 tree
269 c_type_promotes_to (tree type)
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 return double_type_node;
274 if (c_promoting_integer_type_p (type))
276 /* Preserve unsignedness if not really getting any wider. */
277 if (TYPE_UNSIGNED (type)
278 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
279 return unsigned_type_node;
280 return integer_type_node;
283 return type;
286 /* Return true if between two named address spaces, whether there is a superset
287 named address space that encompasses both address spaces. If there is a
288 superset, return which address space is the superset. */
290 static bool
291 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
293 if (as1 == as2)
295 *common = as1;
296 return true;
298 else if (targetm.addr_space.subset_p (as1, as2))
300 *common = as2;
301 return true;
303 else if (targetm.addr_space.subset_p (as2, as1))
305 *common = as1;
306 return true;
308 else
309 return false;
312 /* Return a variant of TYPE which has all the type qualifiers of LIKE
313 as well as those of TYPE. */
315 static tree
316 qualify_type (tree type, tree like)
318 addr_space_t as_type = TYPE_ADDR_SPACE (type);
319 addr_space_t as_like = TYPE_ADDR_SPACE (like);
320 addr_space_t as_common;
322 /* If the two named address spaces are different, determine the common
323 superset address space. If there isn't one, raise an error. */
324 if (!addr_space_superset (as_type, as_like, &as_common))
326 as_common = as_type;
327 error ("%qT and %qT are in disjoint named address spaces",
328 type, like);
331 return c_build_qualified_type (type,
332 TYPE_QUALS_NO_ADDR_SPACE (type)
333 | TYPE_QUALS_NO_ADDR_SPACE (like)
334 | ENCODE_QUAL_ADDR_SPACE (as_common));
337 /* Return true iff the given tree T is a variable length array. */
339 bool
340 c_vla_type_p (const_tree t)
342 if (TREE_CODE (t) == ARRAY_TYPE
343 && C_TYPE_VARIABLE_SIZE (t))
344 return true;
345 return false;
348 /* Return the composite type of two compatible types.
350 We assume that comptypes has already been done and returned
351 nonzero; if that isn't so, this may crash. In particular, we
352 assume that qualifiers match. */
354 tree
355 composite_type (tree t1, tree t2)
357 enum tree_code code1;
358 enum tree_code code2;
359 tree attributes;
361 /* Save time if the two types are the same. */
363 if (t1 == t2) return t1;
365 /* If one type is nonsense, use the other. */
366 if (t1 == error_mark_node)
367 return t2;
368 if (t2 == error_mark_node)
369 return t1;
371 code1 = TREE_CODE (t1);
372 code2 = TREE_CODE (t2);
374 /* Merge the attributes. */
375 attributes = targetm.merge_type_attributes (t1, t2);
377 /* If one is an enumerated type and the other is the compatible
378 integer type, the composite type might be either of the two
379 (DR#013 question 3). For consistency, use the enumerated type as
380 the composite type. */
382 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
383 return t1;
384 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
385 return t2;
387 gcc_assert (code1 == code2);
389 switch (code1)
391 case POINTER_TYPE:
392 /* For two pointers, do this recursively on the target type. */
394 tree pointed_to_1 = TREE_TYPE (t1);
395 tree pointed_to_2 = TREE_TYPE (t2);
396 tree target = composite_type (pointed_to_1, pointed_to_2);
397 t1 = build_pointer_type (target);
398 t1 = build_type_attribute_variant (t1, attributes);
399 return qualify_type (t1, t2);
402 case ARRAY_TYPE:
404 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
405 int quals;
406 tree unqual_elt;
407 tree d1 = TYPE_DOMAIN (t1);
408 tree d2 = TYPE_DOMAIN (t2);
409 bool d1_variable, d2_variable;
410 bool d1_zero, d2_zero;
411 bool t1_complete, t2_complete;
413 /* We should not have any type quals on arrays at all. */
414 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
415 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
417 t1_complete = COMPLETE_TYPE_P (t1);
418 t2_complete = COMPLETE_TYPE_P (t2);
420 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
421 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
423 d1_variable = (!d1_zero
424 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
425 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
426 d2_variable = (!d2_zero
427 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
428 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
429 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
430 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
432 /* Save space: see if the result is identical to one of the args. */
433 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
434 && (d2_variable || d2_zero || !d1_variable))
435 return build_type_attribute_variant (t1, attributes);
436 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
437 && (d1_variable || d1_zero || !d2_variable))
438 return build_type_attribute_variant (t2, attributes);
440 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
441 return build_type_attribute_variant (t1, attributes);
442 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t2, attributes);
445 /* Merge the element types, and have a size if either arg has
446 one. We may have qualifiers on the element types. To set
447 up TYPE_MAIN_VARIANT correctly, we need to form the
448 composite of the unqualified types and add the qualifiers
449 back at the end. */
450 quals = TYPE_QUALS (strip_array_types (elt));
451 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
452 t1 = build_array_type (unqual_elt,
453 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
454 && (d2_variable
455 || d2_zero
456 || !d1_variable))
457 ? t1
458 : t2));
459 /* Ensure a composite type involving a zero-length array type
460 is a zero-length type not an incomplete type. */
461 if (d1_zero && d2_zero
462 && (t1_complete || t2_complete)
463 && !COMPLETE_TYPE_P (t1))
465 TYPE_SIZE (t1) = bitsize_zero_node;
466 TYPE_SIZE_UNIT (t1) = size_zero_node;
468 t1 = c_build_qualified_type (t1, quals);
469 return build_type_attribute_variant (t1, attributes);
472 case ENUMERAL_TYPE:
473 case RECORD_TYPE:
474 case UNION_TYPE:
475 if (attributes != NULL)
477 /* Try harder not to create a new aggregate type. */
478 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
479 return t1;
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
481 return t2;
483 return build_type_attribute_variant (t1, attributes);
485 case FUNCTION_TYPE:
486 /* Function types: prefer the one that specified arg types.
487 If both do, merge the arg types. Also merge the return types. */
489 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
490 tree p1 = TYPE_ARG_TYPES (t1);
491 tree p2 = TYPE_ARG_TYPES (t2);
492 int len;
493 tree newargs, n;
494 int i;
496 /* Save space: see if the result is identical to one of the args. */
497 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
498 return build_type_attribute_variant (t1, attributes);
499 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
500 return build_type_attribute_variant (t2, attributes);
502 /* Simple way if one arg fails to specify argument types. */
503 if (TYPE_ARG_TYPES (t1) == 0)
505 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
506 t1 = build_type_attribute_variant (t1, attributes);
507 return qualify_type (t1, t2);
509 if (TYPE_ARG_TYPES (t2) == 0)
511 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
512 t1 = build_type_attribute_variant (t1, attributes);
513 return qualify_type (t1, t2);
516 /* If both args specify argument types, we must merge the two
517 lists, argument by argument. */
518 /* Tell global_bindings_p to return false so that variable_size
519 doesn't die on VLAs in parameter types. */
520 c_override_global_bindings_to_false = true;
522 len = list_length (p1);
523 newargs = 0;
525 for (i = 0; i < len; i++)
526 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
528 n = newargs;
530 for (; p1;
531 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
533 /* A null type means arg type is not specified.
534 Take whatever the other function type has. */
535 if (TREE_VALUE (p1) == 0)
537 TREE_VALUE (n) = TREE_VALUE (p2);
538 goto parm_done;
540 if (TREE_VALUE (p2) == 0)
542 TREE_VALUE (n) = TREE_VALUE (p1);
543 goto parm_done;
546 /* Given wait (union {union wait *u; int *i} *)
547 and wait (union wait *),
548 prefer union wait * as type of parm. */
549 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
550 && TREE_VALUE (p1) != TREE_VALUE (p2))
552 tree memb;
553 tree mv2 = TREE_VALUE (p2);
554 if (mv2 && mv2 != error_mark_node
555 && TREE_CODE (mv2) != ARRAY_TYPE)
556 mv2 = TYPE_MAIN_VARIANT (mv2);
557 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
558 memb; memb = DECL_CHAIN (memb))
560 tree mv3 = TREE_TYPE (memb);
561 if (mv3 && mv3 != error_mark_node
562 && TREE_CODE (mv3) != ARRAY_TYPE)
563 mv3 = TYPE_MAIN_VARIANT (mv3);
564 if (comptypes (mv3, mv2))
566 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
567 TREE_VALUE (p2));
568 pedwarn (input_location, OPT_pedantic,
569 "function types not truly compatible in ISO C");
570 goto parm_done;
574 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
575 && TREE_VALUE (p2) != TREE_VALUE (p1))
577 tree memb;
578 tree mv1 = TREE_VALUE (p1);
579 if (mv1 && mv1 != error_mark_node
580 && TREE_CODE (mv1) != ARRAY_TYPE)
581 mv1 = TYPE_MAIN_VARIANT (mv1);
582 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
583 memb; memb = DECL_CHAIN (memb))
585 tree mv3 = TREE_TYPE (memb);
586 if (mv3 && mv3 != error_mark_node
587 && TREE_CODE (mv3) != ARRAY_TYPE)
588 mv3 = TYPE_MAIN_VARIANT (mv3);
589 if (comptypes (mv3, mv1))
591 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
592 TREE_VALUE (p1));
593 pedwarn (input_location, OPT_pedantic,
594 "function types not truly compatible in ISO C");
595 goto parm_done;
599 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
600 parm_done: ;
603 c_override_global_bindings_to_false = false;
604 t1 = build_function_type (valtype, newargs);
605 t1 = qualify_type (t1, t2);
606 /* ... falls through ... */
609 default:
610 return build_type_attribute_variant (t1, attributes);
615 /* Return the type of a conditional expression between pointers to
616 possibly differently qualified versions of compatible types.
618 We assume that comp_target_types has already been done and returned
619 nonzero; if that isn't so, this may crash. */
621 static tree
622 common_pointer_type (tree t1, tree t2)
624 tree attributes;
625 tree pointed_to_1, mv1;
626 tree pointed_to_2, mv2;
627 tree target;
628 unsigned target_quals;
629 addr_space_t as1, as2, as_common;
630 int quals1, quals2;
632 /* Save time if the two types are the same. */
634 if (t1 == t2) return t1;
636 /* If one type is nonsense, use the other. */
637 if (t1 == error_mark_node)
638 return t2;
639 if (t2 == error_mark_node)
640 return t1;
642 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
643 && TREE_CODE (t2) == POINTER_TYPE);
645 /* Merge the attributes. */
646 attributes = targetm.merge_type_attributes (t1, t2);
648 /* Find the composite type of the target types, and combine the
649 qualifiers of the two types' targets. Do not lose qualifiers on
650 array element types by taking the TYPE_MAIN_VARIANT. */
651 mv1 = pointed_to_1 = TREE_TYPE (t1);
652 mv2 = pointed_to_2 = TREE_TYPE (t2);
653 if (TREE_CODE (mv1) != ARRAY_TYPE)
654 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
655 if (TREE_CODE (mv2) != ARRAY_TYPE)
656 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
657 target = composite_type (mv1, mv2);
659 /* For function types do not merge const qualifiers, but drop them
660 if used inconsistently. The middle-end uses these to mark const
661 and noreturn functions. */
662 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
663 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
665 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
666 target_quals = (quals1 & quals2);
667 else
668 target_quals = (quals1 | quals2);
670 /* If the two named address spaces are different, determine the common
671 superset address space. This is guaranteed to exist due to the
672 assumption that comp_target_type returned non-zero. */
673 as1 = TYPE_ADDR_SPACE (pointed_to_1);
674 as2 = TYPE_ADDR_SPACE (pointed_to_2);
675 if (!addr_space_superset (as1, as2, &as_common))
676 gcc_unreachable ();
678 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
680 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
681 return build_type_attribute_variant (t1, attributes);
684 /* Return the common type for two arithmetic types under the usual
685 arithmetic conversions. The default conversions have already been
686 applied, and enumerated types converted to their compatible integer
687 types. The resulting type is unqualified and has no attributes.
689 This is the type for the result of most arithmetic operations
690 if the operands have the given two types. */
692 static tree
693 c_common_type (tree t1, tree t2)
695 enum tree_code code1;
696 enum tree_code code2;
698 /* If one type is nonsense, use the other. */
699 if (t1 == error_mark_node)
700 return t2;
701 if (t2 == error_mark_node)
702 return t1;
704 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
705 t1 = TYPE_MAIN_VARIANT (t1);
707 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
708 t2 = TYPE_MAIN_VARIANT (t2);
710 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
711 t1 = build_type_attribute_variant (t1, NULL_TREE);
713 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
714 t2 = build_type_attribute_variant (t2, NULL_TREE);
716 /* Save time if the two types are the same. */
718 if (t1 == t2) return t1;
720 code1 = TREE_CODE (t1);
721 code2 = TREE_CODE (t2);
723 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
724 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
725 || code1 == INTEGER_TYPE);
726 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
727 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
728 || code2 == INTEGER_TYPE);
730 /* When one operand is a decimal float type, the other operand cannot be
731 a generic float type or a complex type. We also disallow vector types
732 here. */
733 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
734 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
736 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
738 error ("can%'t mix operands of decimal float and vector types");
739 return error_mark_node;
741 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
743 error ("can%'t mix operands of decimal float and complex types");
744 return error_mark_node;
746 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
748 error ("can%'t mix operands of decimal float and other float types");
749 return error_mark_node;
753 /* If one type is a vector type, return that type. (How the usual
754 arithmetic conversions apply to the vector types extension is not
755 precisely specified.) */
756 if (code1 == VECTOR_TYPE)
757 return t1;
759 if (code2 == VECTOR_TYPE)
760 return t2;
762 /* If one type is complex, form the common type of the non-complex
763 components, then make that complex. Use T1 or T2 if it is the
764 required type. */
765 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
767 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
768 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
769 tree subtype = c_common_type (subtype1, subtype2);
771 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
772 return t1;
773 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
774 return t2;
775 else
776 return build_complex_type (subtype);
779 /* If only one is real, use it as the result. */
781 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
782 return t1;
784 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
785 return t2;
787 /* If both are real and either are decimal floating point types, use
788 the decimal floating point type with the greater precision. */
790 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
792 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
793 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
794 return dfloat128_type_node;
795 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
796 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
797 return dfloat64_type_node;
798 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
799 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
800 return dfloat32_type_node;
803 /* Deal with fixed-point types. */
804 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
806 unsigned int unsignedp = 0, satp = 0;
807 enum machine_mode m1, m2;
808 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
810 m1 = TYPE_MODE (t1);
811 m2 = TYPE_MODE (t2);
813 /* If one input type is saturating, the result type is saturating. */
814 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
815 satp = 1;
817 /* If both fixed-point types are unsigned, the result type is unsigned.
818 When mixing fixed-point and integer types, follow the sign of the
819 fixed-point type.
820 Otherwise, the result type is signed. */
821 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
822 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
823 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
824 && TYPE_UNSIGNED (t1))
825 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
826 && TYPE_UNSIGNED (t2)))
827 unsignedp = 1;
829 /* The result type is signed. */
830 if (unsignedp == 0)
832 /* If the input type is unsigned, we need to convert to the
833 signed type. */
834 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
836 enum mode_class mclass = (enum mode_class) 0;
837 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
838 mclass = MODE_FRACT;
839 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
840 mclass = MODE_ACCUM;
841 else
842 gcc_unreachable ();
843 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
845 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
847 enum mode_class mclass = (enum mode_class) 0;
848 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
849 mclass = MODE_FRACT;
850 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
851 mclass = MODE_ACCUM;
852 else
853 gcc_unreachable ();
854 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
858 if (code1 == FIXED_POINT_TYPE)
860 fbit1 = GET_MODE_FBIT (m1);
861 ibit1 = GET_MODE_IBIT (m1);
863 else
865 fbit1 = 0;
866 /* Signed integers need to subtract one sign bit. */
867 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
870 if (code2 == FIXED_POINT_TYPE)
872 fbit2 = GET_MODE_FBIT (m2);
873 ibit2 = GET_MODE_IBIT (m2);
875 else
877 fbit2 = 0;
878 /* Signed integers need to subtract one sign bit. */
879 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
882 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
883 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
884 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
885 satp);
888 /* Both real or both integers; use the one with greater precision. */
890 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
891 return t1;
892 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
893 return t2;
895 /* Same precision. Prefer long longs to longs to ints when the
896 same precision, following the C99 rules on integer type rank
897 (which are equivalent to the C90 rules for C90 types). */
899 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
900 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
901 return long_long_unsigned_type_node;
903 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
906 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
907 return long_long_unsigned_type_node;
908 else
909 return long_long_integer_type_node;
912 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
914 return long_unsigned_type_node;
916 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
919 /* But preserve unsignedness from the other type,
920 since long cannot hold all the values of an unsigned int. */
921 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
922 return long_unsigned_type_node;
923 else
924 return long_integer_type_node;
927 /* Likewise, prefer long double to double even if same size. */
928 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
929 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
930 return long_double_type_node;
932 /* Otherwise prefer the unsigned one. */
934 if (TYPE_UNSIGNED (t1))
935 return t1;
936 else
937 return t2;
940 /* Wrapper around c_common_type that is used by c-common.c and other
941 front end optimizations that remove promotions. ENUMERAL_TYPEs
942 are allowed here and are converted to their compatible integer types.
943 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
944 preferably a non-Boolean type as the common type. */
945 tree
946 common_type (tree t1, tree t2)
948 if (TREE_CODE (t1) == ENUMERAL_TYPE)
949 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
950 if (TREE_CODE (t2) == ENUMERAL_TYPE)
951 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
953 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
954 if (TREE_CODE (t1) == BOOLEAN_TYPE
955 && TREE_CODE (t2) == BOOLEAN_TYPE)
956 return boolean_type_node;
958 /* If either type is BOOLEAN_TYPE, then return the other. */
959 if (TREE_CODE (t1) == BOOLEAN_TYPE)
960 return t2;
961 if (TREE_CODE (t2) == BOOLEAN_TYPE)
962 return t1;
964 return c_common_type (t1, t2);
967 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
968 or various other operations. Return 2 if they are compatible
969 but a warning may be needed if you use them together. */
972 comptypes (tree type1, tree type2)
974 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
975 int val;
977 val = comptypes_internal (type1, type2, NULL, NULL);
978 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
980 return val;
983 /* Like comptypes, but if it returns non-zero because enum and int are
984 compatible, it sets *ENUM_AND_INT_P to true. */
986 static int
987 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
989 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
990 int val;
992 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
993 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
995 return val;
998 /* Like comptypes, but if it returns nonzero for different types, it
999 sets *DIFFERENT_TYPES_P to true. */
1002 comptypes_check_different_types (tree type1, tree type2,
1003 bool *different_types_p)
1005 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1006 int val;
1008 val = comptypes_internal (type1, type2, NULL, different_types_p);
1009 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1011 return val;
1014 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1015 or various other operations. Return 2 if they are compatible
1016 but a warning may be needed if you use them together. If
1017 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1018 compatible integer type, then this sets *ENUM_AND_INT_P to true;
1019 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1020 NULL, and the types are compatible but different enough not to be
1021 permitted in C1X typedef redeclarations, then this sets
1022 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1023 false, but may or may not be set if the types are incompatible.
1024 This differs from comptypes, in that we don't free the seen
1025 types. */
1027 static int
1028 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1029 bool *different_types_p)
1031 const_tree t1 = type1;
1032 const_tree t2 = type2;
1033 int attrval, val;
1035 /* Suppress errors caused by previously reported errors. */
1037 if (t1 == t2 || !t1 || !t2
1038 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1039 return 1;
1041 /* If either type is the internal version of sizetype, return the
1042 language version. */
1043 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
1044 && TYPE_ORIG_SIZE_TYPE (t1))
1045 t1 = TYPE_ORIG_SIZE_TYPE (t1);
1047 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
1048 && TYPE_ORIG_SIZE_TYPE (t2))
1049 t2 = TYPE_ORIG_SIZE_TYPE (t2);
1052 /* Enumerated types are compatible with integer types, but this is
1053 not transitive: two enumerated types in the same translation unit
1054 are compatible with each other only if they are the same type. */
1056 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1058 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1059 if (TREE_CODE (t2) != VOID_TYPE)
1061 if (enum_and_int_p != NULL)
1062 *enum_and_int_p = true;
1063 if (different_types_p != NULL)
1064 *different_types_p = true;
1067 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1069 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1070 if (TREE_CODE (t1) != VOID_TYPE)
1072 if (enum_and_int_p != NULL)
1073 *enum_and_int_p = true;
1074 if (different_types_p != NULL)
1075 *different_types_p = true;
1079 if (t1 == t2)
1080 return 1;
1082 /* Different classes of types can't be compatible. */
1084 if (TREE_CODE (t1) != TREE_CODE (t2))
1085 return 0;
1087 /* Qualifiers must match. C99 6.7.3p9 */
1089 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1090 return 0;
1092 /* Allow for two different type nodes which have essentially the same
1093 definition. Note that we already checked for equality of the type
1094 qualifiers (just above). */
1096 if (TREE_CODE (t1) != ARRAY_TYPE
1097 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1098 return 1;
1100 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1101 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1102 return 0;
1104 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1105 val = 0;
1107 switch (TREE_CODE (t1))
1109 case POINTER_TYPE:
1110 /* Do not remove mode or aliasing information. */
1111 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1112 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1113 break;
1114 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1115 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1116 enum_and_int_p, different_types_p));
1117 break;
1119 case FUNCTION_TYPE:
1120 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1121 different_types_p);
1122 break;
1124 case ARRAY_TYPE:
1126 tree d1 = TYPE_DOMAIN (t1);
1127 tree d2 = TYPE_DOMAIN (t2);
1128 bool d1_variable, d2_variable;
1129 bool d1_zero, d2_zero;
1130 val = 1;
1132 /* Target types must match incl. qualifiers. */
1133 if (TREE_TYPE (t1) != TREE_TYPE (t2)
1134 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1135 enum_and_int_p,
1136 different_types_p)))
1137 return 0;
1139 if (different_types_p != NULL
1140 && (d1 == 0) != (d2 == 0))
1141 *different_types_p = true;
1142 /* Sizes must match unless one is missing or variable. */
1143 if (d1 == 0 || d2 == 0 || d1 == d2)
1144 break;
1146 d1_zero = !TYPE_MAX_VALUE (d1);
1147 d2_zero = !TYPE_MAX_VALUE (d2);
1149 d1_variable = (!d1_zero
1150 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1151 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1152 d2_variable = (!d2_zero
1153 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1154 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1155 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1156 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1158 if (different_types_p != NULL
1159 && d1_variable != d2_variable)
1160 *different_types_p = true;
1161 if (d1_variable || d2_variable)
1162 break;
1163 if (d1_zero && d2_zero)
1164 break;
1165 if (d1_zero || d2_zero
1166 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1167 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1168 val = 0;
1170 break;
1173 case ENUMERAL_TYPE:
1174 case RECORD_TYPE:
1175 case UNION_TYPE:
1176 if (val != 1 && !same_translation_unit_p (t1, t2))
1178 tree a1 = TYPE_ATTRIBUTES (t1);
1179 tree a2 = TYPE_ATTRIBUTES (t2);
1181 if (! attribute_list_contained (a1, a2)
1182 && ! attribute_list_contained (a2, a1))
1183 break;
1185 if (attrval != 2)
1186 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1187 different_types_p);
1188 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1189 different_types_p);
1191 break;
1193 case VECTOR_TYPE:
1194 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1195 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1196 enum_and_int_p, different_types_p));
1197 break;
1199 default:
1200 break;
1202 return attrval == 2 && val == 1 ? 2 : val;
1205 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1206 their qualifiers, except for named address spaces. If the pointers point to
1207 different named addresses, then we must determine if one address space is a
1208 subset of the other. */
1210 static int
1211 comp_target_types (location_t location, tree ttl, tree ttr)
1213 int val;
1214 tree mvl = TREE_TYPE (ttl);
1215 tree mvr = TREE_TYPE (ttr);
1216 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1217 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1218 addr_space_t as_common;
1219 bool enum_and_int_p;
1221 /* Fail if pointers point to incompatible address spaces. */
1222 if (!addr_space_superset (asl, asr, &as_common))
1223 return 0;
1225 /* Do not lose qualifiers on element types of array types that are
1226 pointer targets by taking their TYPE_MAIN_VARIANT. */
1227 if (TREE_CODE (mvl) != ARRAY_TYPE)
1228 mvl = TYPE_MAIN_VARIANT (mvl);
1229 if (TREE_CODE (mvr) != ARRAY_TYPE)
1230 mvr = TYPE_MAIN_VARIANT (mvr);
1231 enum_and_int_p = false;
1232 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1234 if (val == 2)
1235 pedwarn (location, OPT_pedantic, "types are not quite compatible");
1237 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1238 warning_at (location, OPT_Wc___compat,
1239 "pointer target types incompatible in C++");
1241 return val;
1244 /* Subroutines of `comptypes'. */
1246 /* Determine whether two trees derive from the same translation unit.
1247 If the CONTEXT chain ends in a null, that tree's context is still
1248 being parsed, so if two trees have context chains ending in null,
1249 they're in the same translation unit. */
1251 same_translation_unit_p (const_tree t1, const_tree t2)
1253 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1254 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1256 case tcc_declaration:
1257 t1 = DECL_CONTEXT (t1); break;
1258 case tcc_type:
1259 t1 = TYPE_CONTEXT (t1); break;
1260 case tcc_exceptional:
1261 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
1262 default: gcc_unreachable ();
1265 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1266 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1268 case tcc_declaration:
1269 t2 = DECL_CONTEXT (t2); break;
1270 case tcc_type:
1271 t2 = TYPE_CONTEXT (t2); break;
1272 case tcc_exceptional:
1273 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
1274 default: gcc_unreachable ();
1277 return t1 == t2;
1280 /* Allocate the seen two types, assuming that they are compatible. */
1282 static struct tagged_tu_seen_cache *
1283 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1285 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1286 tu->next = tagged_tu_seen_base;
1287 tu->t1 = t1;
1288 tu->t2 = t2;
1290 tagged_tu_seen_base = tu;
1292 /* The C standard says that two structures in different translation
1293 units are compatible with each other only if the types of their
1294 fields are compatible (among other things). We assume that they
1295 are compatible until proven otherwise when building the cache.
1296 An example where this can occur is:
1297 struct a
1299 struct a *next;
1301 If we are comparing this against a similar struct in another TU,
1302 and did not assume they were compatible, we end up with an infinite
1303 loop. */
1304 tu->val = 1;
1305 return tu;
1308 /* Free the seen types until we get to TU_TIL. */
1310 static void
1311 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1313 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1314 while (tu != tu_til)
1316 const struct tagged_tu_seen_cache *const tu1
1317 = (const struct tagged_tu_seen_cache *) tu;
1318 tu = tu1->next;
1319 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1321 tagged_tu_seen_base = tu_til;
1324 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1325 compatible. If the two types are not the same (which has been
1326 checked earlier), this can only happen when multiple translation
1327 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
1328 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1329 comptypes_internal. */
1331 static int
1332 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1333 bool *enum_and_int_p, bool *different_types_p)
1335 tree s1, s2;
1336 bool needs_warning = false;
1338 /* We have to verify that the tags of the types are the same. This
1339 is harder than it looks because this may be a typedef, so we have
1340 to go look at the original type. It may even be a typedef of a
1341 typedef...
1342 In the case of compiler-created builtin structs the TYPE_DECL
1343 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
1344 while (TYPE_NAME (t1)
1345 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1346 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1347 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1349 while (TYPE_NAME (t2)
1350 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1351 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1352 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1354 /* C90 didn't have the requirement that the two tags be the same. */
1355 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1356 return 0;
1358 /* C90 didn't say what happened if one or both of the types were
1359 incomplete; we choose to follow C99 rules here, which is that they
1360 are compatible. */
1361 if (TYPE_SIZE (t1) == NULL
1362 || TYPE_SIZE (t2) == NULL)
1363 return 1;
1366 const struct tagged_tu_seen_cache * tts_i;
1367 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1368 if (tts_i->t1 == t1 && tts_i->t2 == t2)
1369 return tts_i->val;
1372 switch (TREE_CODE (t1))
1374 case ENUMERAL_TYPE:
1376 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1377 /* Speed up the case where the type values are in the same order. */
1378 tree tv1 = TYPE_VALUES (t1);
1379 tree tv2 = TYPE_VALUES (t2);
1381 if (tv1 == tv2)
1383 return 1;
1386 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1388 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1389 break;
1390 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1392 tu->val = 0;
1393 return 0;
1397 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1399 return 1;
1401 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1403 tu->val = 0;
1404 return 0;
1407 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1409 tu->val = 0;
1410 return 0;
1413 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1415 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1416 if (s2 == NULL
1417 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1419 tu->val = 0;
1420 return 0;
1423 return 1;
1426 case UNION_TYPE:
1428 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1429 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1431 tu->val = 0;
1432 return 0;
1435 /* Speed up the common case where the fields are in the same order. */
1436 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1437 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1439 int result;
1441 if (DECL_NAME (s1) != DECL_NAME (s2))
1442 break;
1443 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1444 enum_and_int_p, different_types_p);
1446 if (result != 1 && !DECL_NAME (s1))
1447 break;
1448 if (result == 0)
1450 tu->val = 0;
1451 return 0;
1453 if (result == 2)
1454 needs_warning = true;
1456 if (TREE_CODE (s1) == FIELD_DECL
1457 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1458 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1460 tu->val = 0;
1461 return 0;
1464 if (!s1 && !s2)
1466 tu->val = needs_warning ? 2 : 1;
1467 return tu->val;
1470 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1472 bool ok = false;
1474 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1475 if (DECL_NAME (s1) == DECL_NAME (s2))
1477 int result;
1479 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1480 enum_and_int_p,
1481 different_types_p);
1483 if (result != 1 && !DECL_NAME (s1))
1484 continue;
1485 if (result == 0)
1487 tu->val = 0;
1488 return 0;
1490 if (result == 2)
1491 needs_warning = true;
1493 if (TREE_CODE (s1) == FIELD_DECL
1494 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1495 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1496 break;
1498 ok = true;
1499 break;
1501 if (!ok)
1503 tu->val = 0;
1504 return 0;
1507 tu->val = needs_warning ? 2 : 10;
1508 return tu->val;
1511 case RECORD_TYPE:
1513 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1515 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1516 s1 && s2;
1517 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1519 int result;
1520 if (TREE_CODE (s1) != TREE_CODE (s2)
1521 || DECL_NAME (s1) != DECL_NAME (s2))
1522 break;
1523 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1524 enum_and_int_p, different_types_p);
1525 if (result == 0)
1526 break;
1527 if (result == 2)
1528 needs_warning = true;
1530 if (TREE_CODE (s1) == FIELD_DECL
1531 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1532 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1533 break;
1535 if (s1 && s2)
1536 tu->val = 0;
1537 else
1538 tu->val = needs_warning ? 2 : 1;
1539 return tu->val;
1542 default:
1543 gcc_unreachable ();
1547 /* Return 1 if two function types F1 and F2 are compatible.
1548 If either type specifies no argument types,
1549 the other must specify a fixed number of self-promoting arg types.
1550 Otherwise, if one type specifies only the number of arguments,
1551 the other must specify that number of self-promoting arg types.
1552 Otherwise, the argument types must match.
1553 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
1555 static int
1556 function_types_compatible_p (const_tree f1, const_tree f2,
1557 bool *enum_and_int_p, bool *different_types_p)
1559 tree args1, args2;
1560 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1561 int val = 1;
1562 int val1;
1563 tree ret1, ret2;
1565 ret1 = TREE_TYPE (f1);
1566 ret2 = TREE_TYPE (f2);
1568 /* 'volatile' qualifiers on a function's return type used to mean
1569 the function is noreturn. */
1570 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1571 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1572 if (TYPE_VOLATILE (ret1))
1573 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1574 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1575 if (TYPE_VOLATILE (ret2))
1576 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1577 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1578 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1579 if (val == 0)
1580 return 0;
1582 args1 = TYPE_ARG_TYPES (f1);
1583 args2 = TYPE_ARG_TYPES (f2);
1585 if (different_types_p != NULL
1586 && (args1 == 0) != (args2 == 0))
1587 *different_types_p = true;
1589 /* An unspecified parmlist matches any specified parmlist
1590 whose argument types don't need default promotions. */
1592 if (args1 == 0)
1594 if (!self_promoting_args_p (args2))
1595 return 0;
1596 /* If one of these types comes from a non-prototype fn definition,
1597 compare that with the other type's arglist.
1598 If they don't match, ask for a warning (but no error). */
1599 if (TYPE_ACTUAL_ARG_TYPES (f1)
1600 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1601 enum_and_int_p, different_types_p))
1602 val = 2;
1603 return val;
1605 if (args2 == 0)
1607 if (!self_promoting_args_p (args1))
1608 return 0;
1609 if (TYPE_ACTUAL_ARG_TYPES (f2)
1610 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1611 enum_and_int_p, different_types_p))
1612 val = 2;
1613 return val;
1616 /* Both types have argument lists: compare them and propagate results. */
1617 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1618 different_types_p);
1619 return val1 != 1 ? val1 : val;
1622 /* Check two lists of types for compatibility, returning 0 for
1623 incompatible, 1 for compatible, or 2 for compatible with
1624 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1625 comptypes_internal. */
1627 static int
1628 type_lists_compatible_p (const_tree args1, const_tree args2,
1629 bool *enum_and_int_p, bool *different_types_p)
1631 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1632 int val = 1;
1633 int newval = 0;
1635 while (1)
1637 tree a1, mv1, a2, mv2;
1638 if (args1 == 0 && args2 == 0)
1639 return val;
1640 /* If one list is shorter than the other,
1641 they fail to match. */
1642 if (args1 == 0 || args2 == 0)
1643 return 0;
1644 mv1 = a1 = TREE_VALUE (args1);
1645 mv2 = a2 = TREE_VALUE (args2);
1646 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1647 mv1 = TYPE_MAIN_VARIANT (mv1);
1648 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1649 mv2 = TYPE_MAIN_VARIANT (mv2);
1650 /* A null pointer instead of a type
1651 means there is supposed to be an argument
1652 but nothing is specified about what type it has.
1653 So match anything that self-promotes. */
1654 if (different_types_p != NULL
1655 && (a1 == 0) != (a2 == 0))
1656 *different_types_p = true;
1657 if (a1 == 0)
1659 if (c_type_promotes_to (a2) != a2)
1660 return 0;
1662 else if (a2 == 0)
1664 if (c_type_promotes_to (a1) != a1)
1665 return 0;
1667 /* If one of the lists has an error marker, ignore this arg. */
1668 else if (TREE_CODE (a1) == ERROR_MARK
1669 || TREE_CODE (a2) == ERROR_MARK)
1671 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1672 different_types_p)))
1674 if (different_types_p != NULL)
1675 *different_types_p = true;
1676 /* Allow wait (union {union wait *u; int *i} *)
1677 and wait (union wait *) to be compatible. */
1678 if (TREE_CODE (a1) == UNION_TYPE
1679 && (TYPE_NAME (a1) == 0
1680 || TYPE_TRANSPARENT_AGGR (a1))
1681 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1682 && tree_int_cst_equal (TYPE_SIZE (a1),
1683 TYPE_SIZE (a2)))
1685 tree memb;
1686 for (memb = TYPE_FIELDS (a1);
1687 memb; memb = DECL_CHAIN (memb))
1689 tree mv3 = TREE_TYPE (memb);
1690 if (mv3 && mv3 != error_mark_node
1691 && TREE_CODE (mv3) != ARRAY_TYPE)
1692 mv3 = TYPE_MAIN_VARIANT (mv3);
1693 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1694 different_types_p))
1695 break;
1697 if (memb == 0)
1698 return 0;
1700 else if (TREE_CODE (a2) == UNION_TYPE
1701 && (TYPE_NAME (a2) == 0
1702 || TYPE_TRANSPARENT_AGGR (a2))
1703 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1704 && tree_int_cst_equal (TYPE_SIZE (a2),
1705 TYPE_SIZE (a1)))
1707 tree memb;
1708 for (memb = TYPE_FIELDS (a2);
1709 memb; memb = DECL_CHAIN (memb))
1711 tree mv3 = TREE_TYPE (memb);
1712 if (mv3 && mv3 != error_mark_node
1713 && TREE_CODE (mv3) != ARRAY_TYPE)
1714 mv3 = TYPE_MAIN_VARIANT (mv3);
1715 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1716 different_types_p))
1717 break;
1719 if (memb == 0)
1720 return 0;
1722 else
1723 return 0;
1726 /* comptypes said ok, but record if it said to warn. */
1727 if (newval > val)
1728 val = newval;
1730 args1 = TREE_CHAIN (args1);
1731 args2 = TREE_CHAIN (args2);
1735 /* Compute the size to increment a pointer by. */
1737 static tree
1738 c_size_in_bytes (const_tree type)
1740 enum tree_code code = TREE_CODE (type);
1742 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1743 return size_one_node;
1745 if (!COMPLETE_OR_VOID_TYPE_P (type))
1747 error ("arithmetic on pointer to an incomplete type");
1748 return size_one_node;
1751 /* Convert in case a char is more than one unit. */
1752 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1753 size_int (TYPE_PRECISION (char_type_node)
1754 / BITS_PER_UNIT));
1757 /* Return either DECL or its known constant value (if it has one). */
1759 tree
1760 decl_constant_value (tree decl)
1762 if (/* Don't change a variable array bound or initial value to a constant
1763 in a place where a variable is invalid. Note that DECL_INITIAL
1764 isn't valid for a PARM_DECL. */
1765 current_function_decl != 0
1766 && TREE_CODE (decl) != PARM_DECL
1767 && !TREE_THIS_VOLATILE (decl)
1768 && TREE_READONLY (decl)
1769 && DECL_INITIAL (decl) != 0
1770 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1771 /* This is invalid if initial value is not constant.
1772 If it has either a function call, a memory reference,
1773 or a variable, then re-evaluating it could give different results. */
1774 && TREE_CONSTANT (DECL_INITIAL (decl))
1775 /* Check for cases where this is sub-optimal, even though valid. */
1776 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1777 return DECL_INITIAL (decl);
1778 return decl;
1781 /* Convert the array expression EXP to a pointer. */
1782 static tree
1783 array_to_pointer_conversion (location_t loc, tree exp)
1785 tree orig_exp = exp;
1786 tree type = TREE_TYPE (exp);
1787 tree adr;
1788 tree restype = TREE_TYPE (type);
1789 tree ptrtype;
1791 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1793 STRIP_TYPE_NOPS (exp);
1795 if (TREE_NO_WARNING (orig_exp))
1796 TREE_NO_WARNING (exp) = 1;
1798 ptrtype = build_pointer_type (restype);
1800 if (TREE_CODE (exp) == INDIRECT_REF)
1801 return convert (ptrtype, TREE_OPERAND (exp, 0));
1803 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1804 return convert (ptrtype, adr);
1807 /* Convert the function expression EXP to a pointer. */
1808 static tree
1809 function_to_pointer_conversion (location_t loc, tree exp)
1811 tree orig_exp = exp;
1813 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1815 STRIP_TYPE_NOPS (exp);
1817 if (TREE_NO_WARNING (orig_exp))
1818 TREE_NO_WARNING (exp) = 1;
1820 return build_unary_op (loc, ADDR_EXPR, exp, 0);
1823 /* Mark EXP as read, not just set, for set but not used -Wunused
1824 warning purposes. */
1826 void
1827 mark_exp_read (tree exp)
1829 switch (TREE_CODE (exp))
1831 case VAR_DECL:
1832 case PARM_DECL:
1833 DECL_READ_P (exp) = 1;
1834 break;
1835 case ARRAY_REF:
1836 case COMPONENT_REF:
1837 case MODIFY_EXPR:
1838 case REALPART_EXPR:
1839 case IMAGPART_EXPR:
1840 CASE_CONVERT:
1841 case ADDR_EXPR:
1842 mark_exp_read (TREE_OPERAND (exp, 0));
1843 break;
1844 case COMPOUND_EXPR:
1845 case C_MAYBE_CONST_EXPR:
1846 mark_exp_read (TREE_OPERAND (exp, 1));
1847 break;
1848 default:
1849 break;
1853 /* Perform the default conversion of arrays and functions to pointers.
1854 Return the result of converting EXP. For any other expression, just
1855 return EXP.
1857 LOC is the location of the expression. */
1859 struct c_expr
1860 default_function_array_conversion (location_t loc, struct c_expr exp)
1862 tree orig_exp = exp.value;
1863 tree type = TREE_TYPE (exp.value);
1864 enum tree_code code = TREE_CODE (type);
1866 switch (code)
1868 case ARRAY_TYPE:
1870 bool not_lvalue = false;
1871 bool lvalue_array_p;
1873 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1874 || CONVERT_EXPR_P (exp.value))
1875 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1877 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1878 not_lvalue = true;
1879 exp.value = TREE_OPERAND (exp.value, 0);
1882 if (TREE_NO_WARNING (orig_exp))
1883 TREE_NO_WARNING (exp.value) = 1;
1885 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1886 if (!flag_isoc99 && !lvalue_array_p)
1888 /* Before C99, non-lvalue arrays do not decay to pointers.
1889 Normally, using such an array would be invalid; but it can
1890 be used correctly inside sizeof or as a statement expression.
1891 Thus, do not give an error here; an error will result later. */
1892 return exp;
1895 exp.value = array_to_pointer_conversion (loc, exp.value);
1897 break;
1898 case FUNCTION_TYPE:
1899 exp.value = function_to_pointer_conversion (loc, exp.value);
1900 break;
1901 default:
1902 break;
1905 return exp;
1908 struct c_expr
1909 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1911 mark_exp_read (exp.value);
1912 return default_function_array_conversion (loc, exp);
1915 /* EXP is an expression of integer type. Apply the integer promotions
1916 to it and return the promoted value. */
1918 tree
1919 perform_integral_promotions (tree exp)
1921 tree type = TREE_TYPE (exp);
1922 enum tree_code code = TREE_CODE (type);
1924 gcc_assert (INTEGRAL_TYPE_P (type));
1926 /* Normally convert enums to int,
1927 but convert wide enums to something wider. */
1928 if (code == ENUMERAL_TYPE)
1930 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1931 TYPE_PRECISION (integer_type_node)),
1932 ((TYPE_PRECISION (type)
1933 >= TYPE_PRECISION (integer_type_node))
1934 && TYPE_UNSIGNED (type)));
1936 return convert (type, exp);
1939 /* ??? This should no longer be needed now bit-fields have their
1940 proper types. */
1941 if (TREE_CODE (exp) == COMPONENT_REF
1942 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1943 /* If it's thinner than an int, promote it like a
1944 c_promoting_integer_type_p, otherwise leave it alone. */
1945 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1946 TYPE_PRECISION (integer_type_node)))
1947 return convert (integer_type_node, exp);
1949 if (c_promoting_integer_type_p (type))
1951 /* Preserve unsignedness if not really getting any wider. */
1952 if (TYPE_UNSIGNED (type)
1953 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1954 return convert (unsigned_type_node, exp);
1956 return convert (integer_type_node, exp);
1959 return exp;
1963 /* Perform default promotions for C data used in expressions.
1964 Enumeral types or short or char are converted to int.
1965 In addition, manifest constants symbols are replaced by their values. */
1967 tree
1968 default_conversion (tree exp)
1970 tree orig_exp;
1971 tree type = TREE_TYPE (exp);
1972 enum tree_code code = TREE_CODE (type);
1973 tree promoted_type;
1975 mark_exp_read (exp);
1977 /* Functions and arrays have been converted during parsing. */
1978 gcc_assert (code != FUNCTION_TYPE);
1979 if (code == ARRAY_TYPE)
1980 return exp;
1982 /* Constants can be used directly unless they're not loadable. */
1983 if (TREE_CODE (exp) == CONST_DECL)
1984 exp = DECL_INITIAL (exp);
1986 /* Strip no-op conversions. */
1987 orig_exp = exp;
1988 STRIP_TYPE_NOPS (exp);
1990 if (TREE_NO_WARNING (orig_exp))
1991 TREE_NO_WARNING (exp) = 1;
1993 if (code == VOID_TYPE)
1995 error ("void value not ignored as it ought to be");
1996 return error_mark_node;
1999 exp = require_complete_type (exp);
2000 if (exp == error_mark_node)
2001 return error_mark_node;
2003 promoted_type = targetm.promoted_type (type);
2004 if (promoted_type)
2005 return convert (promoted_type, exp);
2007 if (INTEGRAL_TYPE_P (type))
2008 return perform_integral_promotions (exp);
2010 return exp;
2013 /* Look up COMPONENT in a structure or union TYPE.
2015 If the component name is not found, returns NULL_TREE. Otherwise,
2016 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2017 stepping down the chain to the component, which is in the last
2018 TREE_VALUE of the list. Normally the list is of length one, but if
2019 the component is embedded within (nested) anonymous structures or
2020 unions, the list steps down the chain to the component. */
2022 static tree
2023 lookup_field (tree type, tree component)
2025 tree field;
2027 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2028 to the field elements. Use a binary search on this array to quickly
2029 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2030 will always be set for structures which have many elements. */
2032 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2034 int bot, top, half;
2035 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2037 field = TYPE_FIELDS (type);
2038 bot = 0;
2039 top = TYPE_LANG_SPECIFIC (type)->s->len;
2040 while (top - bot > 1)
2042 half = (top - bot + 1) >> 1;
2043 field = field_array[bot+half];
2045 if (DECL_NAME (field) == NULL_TREE)
2047 /* Step through all anon unions in linear fashion. */
2048 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2050 field = field_array[bot++];
2051 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2052 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2054 tree anon = lookup_field (TREE_TYPE (field), component);
2056 if (anon)
2057 return tree_cons (NULL_TREE, field, anon);
2061 /* Entire record is only anon unions. */
2062 if (bot > top)
2063 return NULL_TREE;
2065 /* Restart the binary search, with new lower bound. */
2066 continue;
2069 if (DECL_NAME (field) == component)
2070 break;
2071 if (DECL_NAME (field) < component)
2072 bot += half;
2073 else
2074 top = bot + half;
2077 if (DECL_NAME (field_array[bot]) == component)
2078 field = field_array[bot];
2079 else if (DECL_NAME (field) != component)
2080 return NULL_TREE;
2082 else
2084 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2086 if (DECL_NAME (field) == NULL_TREE
2087 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2088 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2090 tree anon = lookup_field (TREE_TYPE (field), component);
2092 if (anon)
2093 return tree_cons (NULL_TREE, field, anon);
2096 if (DECL_NAME (field) == component)
2097 break;
2100 if (field == NULL_TREE)
2101 return NULL_TREE;
2104 return tree_cons (NULL_TREE, field, NULL_TREE);
2107 /* Make an expression to refer to the COMPONENT field of structure or
2108 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2109 location of the COMPONENT_REF. */
2111 tree
2112 build_component_ref (location_t loc, tree datum, tree component)
2114 tree type = TREE_TYPE (datum);
2115 enum tree_code code = TREE_CODE (type);
2116 tree field = NULL;
2117 tree ref;
2118 bool datum_lvalue = lvalue_p (datum);
2120 if (!objc_is_public (datum, component))
2121 return error_mark_node;
2123 /* See if there is a field or component with name COMPONENT. */
2125 if (code == RECORD_TYPE || code == UNION_TYPE)
2127 if (!COMPLETE_TYPE_P (type))
2129 c_incomplete_type_error (NULL_TREE, type);
2130 return error_mark_node;
2133 field = lookup_field (type, component);
2135 if (!field)
2137 error_at (loc, "%qT has no member named %qE", type, component);
2138 return error_mark_node;
2141 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2142 This might be better solved in future the way the C++ front
2143 end does it - by giving the anonymous entities each a
2144 separate name and type, and then have build_component_ref
2145 recursively call itself. We can't do that here. */
2148 tree subdatum = TREE_VALUE (field);
2149 int quals;
2150 tree subtype;
2151 bool use_datum_quals;
2153 if (TREE_TYPE (subdatum) == error_mark_node)
2154 return error_mark_node;
2156 /* If this is an rvalue, it does not have qualifiers in C
2157 standard terms and we must avoid propagating such
2158 qualifiers down to a non-lvalue array that is then
2159 converted to a pointer. */
2160 use_datum_quals = (datum_lvalue
2161 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2163 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2164 if (use_datum_quals)
2165 quals |= TYPE_QUALS (TREE_TYPE (datum));
2166 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2168 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2169 NULL_TREE);
2170 SET_EXPR_LOCATION (ref, loc);
2171 if (TREE_READONLY (subdatum)
2172 || (use_datum_quals && TREE_READONLY (datum)))
2173 TREE_READONLY (ref) = 1;
2174 if (TREE_THIS_VOLATILE (subdatum)
2175 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2176 TREE_THIS_VOLATILE (ref) = 1;
2178 if (TREE_DEPRECATED (subdatum))
2179 warn_deprecated_use (subdatum, NULL_TREE);
2181 datum = ref;
2183 field = TREE_CHAIN (field);
2185 while (field);
2187 return ref;
2189 else if (code != ERROR_MARK)
2190 error_at (loc,
2191 "request for member %qE in something not a structure or union",
2192 component);
2194 return error_mark_node;
2197 /* Given an expression PTR for a pointer, return an expression
2198 for the value pointed to.
2199 ERRORSTRING is the name of the operator to appear in error messages.
2201 LOC is the location to use for the generated tree. */
2203 tree
2204 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2206 tree pointer = default_conversion (ptr);
2207 tree type = TREE_TYPE (pointer);
2208 tree ref;
2210 if (TREE_CODE (type) == POINTER_TYPE)
2212 if (CONVERT_EXPR_P (pointer)
2213 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2215 /* If a warning is issued, mark it to avoid duplicates from
2216 the backend. This only needs to be done at
2217 warn_strict_aliasing > 2. */
2218 if (warn_strict_aliasing > 2)
2219 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2220 type, TREE_OPERAND (pointer, 0)))
2221 TREE_NO_WARNING (pointer) = 1;
2224 if (TREE_CODE (pointer) == ADDR_EXPR
2225 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2226 == TREE_TYPE (type)))
2228 ref = TREE_OPERAND (pointer, 0);
2229 protected_set_expr_location (ref, loc);
2230 return ref;
2232 else
2234 tree t = TREE_TYPE (type);
2236 ref = build1 (INDIRECT_REF, t, pointer);
2238 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2240 error_at (loc, "dereferencing pointer to incomplete type");
2241 return error_mark_node;
2243 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2244 warning_at (loc, 0, "dereferencing %<void *%> pointer");
2246 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2247 so that we get the proper error message if the result is used
2248 to assign to. Also, &* is supposed to be a no-op.
2249 And ANSI C seems to specify that the type of the result
2250 should be the const type. */
2251 /* A de-reference of a pointer to const is not a const. It is valid
2252 to change it via some other pointer. */
2253 TREE_READONLY (ref) = TYPE_READONLY (t);
2254 TREE_SIDE_EFFECTS (ref)
2255 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2256 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2257 protected_set_expr_location (ref, loc);
2258 return ref;
2261 else if (TREE_CODE (pointer) != ERROR_MARK)
2262 switch (errstring)
2264 case RO_ARRAY_INDEXING:
2265 error_at (loc,
2266 "invalid type argument of array indexing (have %qT)",
2267 type);
2268 break;
2269 case RO_UNARY_STAR:
2270 error_at (loc,
2271 "invalid type argument of unary %<*%> (have %qT)",
2272 type);
2273 break;
2274 case RO_ARROW:
2275 error_at (loc,
2276 "invalid type argument of %<->%> (have %qT)",
2277 type);
2278 break;
2279 default:
2280 gcc_unreachable ();
2282 return error_mark_node;
2285 /* This handles expressions of the form "a[i]", which denotes
2286 an array reference.
2288 This is logically equivalent in C to *(a+i), but we may do it differently.
2289 If A is a variable or a member, we generate a primitive ARRAY_REF.
2290 This avoids forcing the array out of registers, and can work on
2291 arrays that are not lvalues (for example, members of structures returned
2292 by functions).
2294 LOC is the location to use for the returned expression. */
2296 tree
2297 build_array_ref (location_t loc, tree array, tree index)
2299 tree ret;
2300 bool swapped = false;
2301 if (TREE_TYPE (array) == error_mark_node
2302 || TREE_TYPE (index) == error_mark_node)
2303 return error_mark_node;
2305 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2306 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2308 tree temp;
2309 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2310 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2312 error_at (loc, "subscripted value is neither array nor pointer");
2313 return error_mark_node;
2315 temp = array;
2316 array = index;
2317 index = temp;
2318 swapped = true;
2321 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2323 error_at (loc, "array subscript is not an integer");
2324 return error_mark_node;
2327 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2329 error_at (loc, "subscripted value is pointer to function");
2330 return error_mark_node;
2333 /* ??? Existing practice has been to warn only when the char
2334 index is syntactically the index, not for char[array]. */
2335 if (!swapped)
2336 warn_array_subscript_with_type_char (index);
2338 /* Apply default promotions *after* noticing character types. */
2339 index = default_conversion (index);
2341 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2343 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2345 tree rval, type;
2347 /* An array that is indexed by a non-constant
2348 cannot be stored in a register; we must be able to do
2349 address arithmetic on its address.
2350 Likewise an array of elements of variable size. */
2351 if (TREE_CODE (index) != INTEGER_CST
2352 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2353 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2355 if (!c_mark_addressable (array))
2356 return error_mark_node;
2358 /* An array that is indexed by a constant value which is not within
2359 the array bounds cannot be stored in a register either; because we
2360 would get a crash in store_bit_field/extract_bit_field when trying
2361 to access a non-existent part of the register. */
2362 if (TREE_CODE (index) == INTEGER_CST
2363 && TYPE_DOMAIN (TREE_TYPE (array))
2364 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2366 if (!c_mark_addressable (array))
2367 return error_mark_node;
2370 if (pedantic)
2372 tree foo = array;
2373 while (TREE_CODE (foo) == COMPONENT_REF)
2374 foo = TREE_OPERAND (foo, 0);
2375 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2376 pedwarn (loc, OPT_pedantic,
2377 "ISO C forbids subscripting %<register%> array");
2378 else if (!flag_isoc99 && !lvalue_p (foo))
2379 pedwarn (loc, OPT_pedantic,
2380 "ISO C90 forbids subscripting non-lvalue array");
2383 type = TREE_TYPE (TREE_TYPE (array));
2384 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2385 /* Array ref is const/volatile if the array elements are
2386 or if the array is. */
2387 TREE_READONLY (rval)
2388 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2389 | TREE_READONLY (array));
2390 TREE_SIDE_EFFECTS (rval)
2391 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2392 | TREE_SIDE_EFFECTS (array));
2393 TREE_THIS_VOLATILE (rval)
2394 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2395 /* This was added by rms on 16 Nov 91.
2396 It fixes vol struct foo *a; a->elts[1]
2397 in an inline function.
2398 Hope it doesn't break something else. */
2399 | TREE_THIS_VOLATILE (array));
2400 ret = require_complete_type (rval);
2401 protected_set_expr_location (ret, loc);
2402 return ret;
2404 else
2406 tree ar = default_conversion (array);
2408 if (ar == error_mark_node)
2409 return ar;
2411 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2412 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2414 return build_indirect_ref
2415 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2416 RO_ARRAY_INDEXING);
2420 /* Build an external reference to identifier ID. FUN indicates
2421 whether this will be used for a function call. LOC is the source
2422 location of the identifier. This sets *TYPE to the type of the
2423 identifier, which is not the same as the type of the returned value
2424 for CONST_DECLs defined as enum constants. If the type of the
2425 identifier is not available, *TYPE is set to NULL. */
2426 tree
2427 build_external_ref (location_t loc, tree id, int fun, tree *type)
2429 tree ref;
2430 tree decl = lookup_name (id);
2432 /* In Objective-C, an instance variable (ivar) may be preferred to
2433 whatever lookup_name() found. */
2434 decl = objc_lookup_ivar (decl, id);
2436 *type = NULL;
2437 if (decl && decl != error_mark_node)
2439 ref = decl;
2440 *type = TREE_TYPE (ref);
2442 else if (fun)
2443 /* Implicit function declaration. */
2444 ref = implicitly_declare (loc, id);
2445 else if (decl == error_mark_node)
2446 /* Don't complain about something that's already been
2447 complained about. */
2448 return error_mark_node;
2449 else
2451 undeclared_variable (loc, id);
2452 return error_mark_node;
2455 if (TREE_TYPE (ref) == error_mark_node)
2456 return error_mark_node;
2458 if (TREE_DEPRECATED (ref))
2459 warn_deprecated_use (ref, NULL_TREE);
2461 /* Recursive call does not count as usage. */
2462 if (ref != current_function_decl)
2464 TREE_USED (ref) = 1;
2467 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2469 if (!in_sizeof && !in_typeof)
2470 C_DECL_USED (ref) = 1;
2471 else if (DECL_INITIAL (ref) == 0
2472 && DECL_EXTERNAL (ref)
2473 && !TREE_PUBLIC (ref))
2474 record_maybe_used_decl (ref);
2477 if (TREE_CODE (ref) == CONST_DECL)
2479 used_types_insert (TREE_TYPE (ref));
2481 if (warn_cxx_compat
2482 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2483 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2485 warning_at (loc, OPT_Wc___compat,
2486 ("enum constant defined in struct or union "
2487 "is not visible in C++"));
2488 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2491 ref = DECL_INITIAL (ref);
2492 TREE_CONSTANT (ref) = 1;
2494 else if (current_function_decl != 0
2495 && !DECL_FILE_SCOPE_P (current_function_decl)
2496 && (TREE_CODE (ref) == VAR_DECL
2497 || TREE_CODE (ref) == PARM_DECL
2498 || TREE_CODE (ref) == FUNCTION_DECL))
2500 tree context = decl_function_context (ref);
2502 if (context != 0 && context != current_function_decl)
2503 DECL_NONLOCAL (ref) = 1;
2505 /* C99 6.7.4p3: An inline definition of a function with external
2506 linkage ... shall not contain a reference to an identifier with
2507 internal linkage. */
2508 else if (current_function_decl != 0
2509 && DECL_DECLARED_INLINE_P (current_function_decl)
2510 && DECL_EXTERNAL (current_function_decl)
2511 && VAR_OR_FUNCTION_DECL_P (ref)
2512 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2513 && ! TREE_PUBLIC (ref)
2514 && DECL_CONTEXT (ref) != current_function_decl)
2515 record_inline_static (loc, current_function_decl, ref,
2516 csi_internal);
2518 return ref;
2521 /* Record details of decls possibly used inside sizeof or typeof. */
2522 struct maybe_used_decl
2524 /* The decl. */
2525 tree decl;
2526 /* The level seen at (in_sizeof + in_typeof). */
2527 int level;
2528 /* The next one at this level or above, or NULL. */
2529 struct maybe_used_decl *next;
2532 static struct maybe_used_decl *maybe_used_decls;
2534 /* Record that DECL, an undefined static function reference seen
2535 inside sizeof or typeof, might be used if the operand of sizeof is
2536 a VLA type or the operand of typeof is a variably modified
2537 type. */
2539 static void
2540 record_maybe_used_decl (tree decl)
2542 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2543 t->decl = decl;
2544 t->level = in_sizeof + in_typeof;
2545 t->next = maybe_used_decls;
2546 maybe_used_decls = t;
2549 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2550 USED is false, just discard them. If it is true, mark them used
2551 (if no longer inside sizeof or typeof) or move them to the next
2552 level up (if still inside sizeof or typeof). */
2554 void
2555 pop_maybe_used (bool used)
2557 struct maybe_used_decl *p = maybe_used_decls;
2558 int cur_level = in_sizeof + in_typeof;
2559 while (p && p->level > cur_level)
2561 if (used)
2563 if (cur_level == 0)
2564 C_DECL_USED (p->decl) = 1;
2565 else
2566 p->level = cur_level;
2568 p = p->next;
2570 if (!used || cur_level == 0)
2571 maybe_used_decls = p;
2574 /* Return the result of sizeof applied to EXPR. */
2576 struct c_expr
2577 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2579 struct c_expr ret;
2580 if (expr.value == error_mark_node)
2582 ret.value = error_mark_node;
2583 ret.original_code = ERROR_MARK;
2584 ret.original_type = NULL;
2585 pop_maybe_used (false);
2587 else
2589 bool expr_const_operands = true;
2590 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2591 &expr_const_operands);
2592 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2593 ret.original_code = ERROR_MARK;
2594 ret.original_type = NULL;
2595 if (c_vla_type_p (TREE_TYPE (folded_expr)))
2597 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
2598 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2599 folded_expr, ret.value);
2600 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2601 SET_EXPR_LOCATION (ret.value, loc);
2603 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2605 return ret;
2608 /* Return the result of sizeof applied to T, a structure for the type
2609 name passed to sizeof (rather than the type itself). LOC is the
2610 location of the original expression. */
2612 struct c_expr
2613 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2615 tree type;
2616 struct c_expr ret;
2617 tree type_expr = NULL_TREE;
2618 bool type_expr_const = true;
2619 type = groktypename (t, &type_expr, &type_expr_const);
2620 ret.value = c_sizeof (loc, type);
2621 ret.original_code = ERROR_MARK;
2622 ret.original_type = NULL;
2623 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2624 && c_vla_type_p (type))
2626 /* If the type is a [*] array, it is a VLA but is represented as
2627 having a size of zero. In such a case we must ensure that
2628 the result of sizeof does not get folded to a constant by
2629 c_fully_fold, because if the size is evaluated the result is
2630 not constant and so constraints on zero or negative size
2631 arrays must not be applied when this sizeof call is inside
2632 another array declarator. */
2633 if (!type_expr)
2634 type_expr = integer_zero_node;
2635 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2636 type_expr, ret.value);
2637 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2639 pop_maybe_used (type != error_mark_node
2640 ? C_TYPE_VARIABLE_SIZE (type) : false);
2641 return ret;
2644 /* Build a function call to function FUNCTION with parameters PARAMS.
2645 The function call is at LOC.
2646 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2647 TREE_VALUE of each node is a parameter-expression.
2648 FUNCTION's data type may be a function type or a pointer-to-function. */
2650 tree
2651 build_function_call (location_t loc, tree function, tree params)
2653 VEC(tree,gc) *vec;
2654 tree ret;
2656 vec = VEC_alloc (tree, gc, list_length (params));
2657 for (; params; params = TREE_CHAIN (params))
2658 VEC_quick_push (tree, vec, TREE_VALUE (params));
2659 ret = build_function_call_vec (loc, function, vec, NULL);
2660 VEC_free (tree, gc, vec);
2661 return ret;
2664 /* Build a function call to function FUNCTION with parameters PARAMS.
2665 ORIGTYPES, if not NULL, is a vector of types; each element is
2666 either NULL or the original type of the corresponding element in
2667 PARAMS. The original type may differ from TREE_TYPE of the
2668 parameter for enums. FUNCTION's data type may be a function type
2669 or pointer-to-function. This function changes the elements of
2670 PARAMS. */
2672 tree
2673 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2674 VEC(tree,gc) *origtypes)
2676 tree fntype, fundecl = 0;
2677 tree name = NULL_TREE, result;
2678 tree tem;
2679 int nargs;
2680 tree *argarray;
2683 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2684 STRIP_TYPE_NOPS (function);
2686 /* Convert anything with function type to a pointer-to-function. */
2687 if (TREE_CODE (function) == FUNCTION_DECL)
2689 /* Implement type-directed function overloading for builtins.
2690 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2691 handle all the type checking. The result is a complete expression
2692 that implements this function call. */
2693 tem = resolve_overloaded_builtin (loc, function, params);
2694 if (tem)
2695 return tem;
2697 name = DECL_NAME (function);
2698 fundecl = function;
2700 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2701 function = function_to_pointer_conversion (loc, function);
2703 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2704 expressions, like those used for ObjC messenger dispatches. */
2705 if (!VEC_empty (tree, params))
2706 function = objc_rewrite_function_call (function,
2707 VEC_index (tree, params, 0));
2709 function = c_fully_fold (function, false, NULL);
2711 fntype = TREE_TYPE (function);
2713 if (TREE_CODE (fntype) == ERROR_MARK)
2714 return error_mark_node;
2716 if (!(TREE_CODE (fntype) == POINTER_TYPE
2717 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2719 error_at (loc, "called object %qE is not a function", function);
2720 return error_mark_node;
2723 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2724 current_function_returns_abnormally = 1;
2726 /* fntype now gets the type of function pointed to. */
2727 fntype = TREE_TYPE (fntype);
2729 /* Convert the parameters to the types declared in the
2730 function prototype, or apply default promotions. */
2732 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2733 function, fundecl);
2734 if (nargs < 0)
2735 return error_mark_node;
2737 /* Check that the function is called through a compatible prototype.
2738 If it is not, replace the call by a trap, wrapped up in a compound
2739 expression if necessary. This has the nice side-effect to prevent
2740 the tree-inliner from generating invalid assignment trees which may
2741 blow up in the RTL expander later. */
2742 if (CONVERT_EXPR_P (function)
2743 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2744 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2745 && !comptypes (fntype, TREE_TYPE (tem)))
2747 tree return_type = TREE_TYPE (fntype);
2748 tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2749 NULL_TREE);
2750 int i;
2752 /* This situation leads to run-time undefined behavior. We can't,
2753 therefore, simply error unless we can prove that all possible
2754 executions of the program must execute the code. */
2755 if (warning_at (loc, 0, "function called through a non-compatible type"))
2756 /* We can, however, treat "undefined" any way we please.
2757 Call abort to encourage the user to fix the program. */
2758 inform (loc, "if this code is reached, the program will abort");
2759 /* Before the abort, allow the function arguments to exit or
2760 call longjmp. */
2761 for (i = 0; i < nargs; i++)
2762 trap = build2 (COMPOUND_EXPR, void_type_node,
2763 VEC_index (tree, params, i), trap);
2765 if (VOID_TYPE_P (return_type))
2767 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2768 pedwarn (loc, 0,
2769 "function with qualified void return type called");
2770 return trap;
2772 else
2774 tree rhs;
2776 if (AGGREGATE_TYPE_P (return_type))
2777 rhs = build_compound_literal (loc, return_type,
2778 build_constructor (return_type, 0),
2779 false);
2780 else
2781 rhs = fold_convert_loc (loc, return_type, integer_zero_node);
2783 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2784 trap, rhs));
2788 argarray = VEC_address (tree, params);
2790 /* Check that arguments to builtin functions match the expectations. */
2791 if (fundecl
2792 && DECL_BUILT_IN (fundecl)
2793 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2794 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2795 return error_mark_node;
2797 /* Check that the arguments to the function are valid. */
2798 check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2799 TYPE_ARG_TYPES (fntype));
2801 if (name != NULL_TREE
2802 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2804 if (require_constant_value)
2805 result =
2806 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2807 function, nargs, argarray);
2808 else
2809 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2810 function, nargs, argarray);
2811 if (TREE_CODE (result) == NOP_EXPR
2812 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2813 STRIP_TYPE_NOPS (result);
2815 else
2816 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2817 function, nargs, argarray);
2819 if (VOID_TYPE_P (TREE_TYPE (result)))
2821 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2822 pedwarn (loc, 0,
2823 "function with qualified void return type called");
2824 return result;
2826 return require_complete_type (result);
2829 /* Convert the argument expressions in the vector VALUES
2830 to the types in the list TYPELIST.
2832 If TYPELIST is exhausted, or when an element has NULL as its type,
2833 perform the default conversions.
2835 ORIGTYPES is the original types of the expressions in VALUES. This
2836 holds the type of enum values which have been converted to integral
2837 types. It may be NULL.
2839 FUNCTION is a tree for the called function. It is used only for
2840 error messages, where it is formatted with %qE.
2842 This is also where warnings about wrong number of args are generated.
2844 Returns the actual number of arguments processed (which may be less
2845 than the length of VALUES in some error situations), or -1 on
2846 failure. */
2848 static int
2849 convert_arguments (tree typelist, VEC(tree,gc) *values,
2850 VEC(tree,gc) *origtypes, tree function, tree fundecl)
2852 tree typetail, val;
2853 unsigned int parmnum;
2854 bool error_args = false;
2855 const bool type_generic = fundecl
2856 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2857 bool type_generic_remove_excess_precision = false;
2858 tree selector;
2860 /* Change pointer to function to the function itself for
2861 diagnostics. */
2862 if (TREE_CODE (function) == ADDR_EXPR
2863 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2864 function = TREE_OPERAND (function, 0);
2866 /* Handle an ObjC selector specially for diagnostics. */
2867 selector = objc_message_selector ();
2869 /* For type-generic built-in functions, determine whether excess
2870 precision should be removed (classification) or not
2871 (comparison). */
2872 if (type_generic
2873 && DECL_BUILT_IN (fundecl)
2874 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2876 switch (DECL_FUNCTION_CODE (fundecl))
2878 case BUILT_IN_ISFINITE:
2879 case BUILT_IN_ISINF:
2880 case BUILT_IN_ISINF_SIGN:
2881 case BUILT_IN_ISNAN:
2882 case BUILT_IN_ISNORMAL:
2883 case BUILT_IN_FPCLASSIFY:
2884 type_generic_remove_excess_precision = true;
2885 break;
2887 default:
2888 type_generic_remove_excess_precision = false;
2889 break;
2893 /* Scan the given expressions and types, producing individual
2894 converted arguments. */
2896 for (typetail = typelist, parmnum = 0;
2897 VEC_iterate (tree, values, parmnum, val);
2898 ++parmnum)
2900 tree type = typetail ? TREE_VALUE (typetail) : 0;
2901 tree valtype = TREE_TYPE (val);
2902 tree rname = function;
2903 int argnum = parmnum + 1;
2904 const char *invalid_func_diag;
2905 bool excess_precision = false;
2906 bool npc;
2907 tree parmval;
2909 if (type == void_type_node)
2911 error_at (input_location,
2912 "too many arguments to function %qE", function);
2913 if (fundecl && !DECL_BUILT_IN (fundecl))
2914 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2915 return parmnum;
2918 if (selector && argnum > 2)
2920 rname = selector;
2921 argnum -= 2;
2924 npc = null_pointer_constant_p (val);
2926 /* If there is excess precision and a prototype, convert once to
2927 the required type rather than converting via the semantic
2928 type. Likewise without a prototype a float value represented
2929 as long double should be converted once to double. But for
2930 type-generic classification functions excess precision must
2931 be removed here. */
2932 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2933 && (type || !type_generic || !type_generic_remove_excess_precision))
2935 val = TREE_OPERAND (val, 0);
2936 excess_precision = true;
2938 val = c_fully_fold (val, false, NULL);
2939 STRIP_TYPE_NOPS (val);
2941 val = require_complete_type (val);
2943 if (type != 0)
2945 /* Formal parm type is specified by a function prototype. */
2947 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2949 error ("type of formal parameter %d is incomplete", parmnum + 1);
2950 parmval = val;
2952 else
2954 tree origtype;
2956 /* Optionally warn about conversions that
2957 differ from the default conversions. */
2958 if (warn_traditional_conversion || warn_traditional)
2960 unsigned int formal_prec = TYPE_PRECISION (type);
2962 if (INTEGRAL_TYPE_P (type)
2963 && TREE_CODE (valtype) == REAL_TYPE)
2964 warning (0, "passing argument %d of %qE as integer "
2965 "rather than floating due to prototype",
2966 argnum, rname);
2967 if (INTEGRAL_TYPE_P (type)
2968 && TREE_CODE (valtype) == COMPLEX_TYPE)
2969 warning (0, "passing argument %d of %qE as integer "
2970 "rather than complex due to prototype",
2971 argnum, rname);
2972 else if (TREE_CODE (type) == COMPLEX_TYPE
2973 && TREE_CODE (valtype) == REAL_TYPE)
2974 warning (0, "passing argument %d of %qE as complex "
2975 "rather than floating due to prototype",
2976 argnum, rname);
2977 else if (TREE_CODE (type) == REAL_TYPE
2978 && INTEGRAL_TYPE_P (valtype))
2979 warning (0, "passing argument %d of %qE as floating "
2980 "rather than integer due to prototype",
2981 argnum, rname);
2982 else if (TREE_CODE (type) == COMPLEX_TYPE
2983 && INTEGRAL_TYPE_P (valtype))
2984 warning (0, "passing argument %d of %qE as complex "
2985 "rather than integer due to prototype",
2986 argnum, rname);
2987 else if (TREE_CODE (type) == REAL_TYPE
2988 && TREE_CODE (valtype) == COMPLEX_TYPE)
2989 warning (0, "passing argument %d of %qE as floating "
2990 "rather than complex due to prototype",
2991 argnum, rname);
2992 /* ??? At some point, messages should be written about
2993 conversions between complex types, but that's too messy
2994 to do now. */
2995 else if (TREE_CODE (type) == REAL_TYPE
2996 && TREE_CODE (valtype) == REAL_TYPE)
2998 /* Warn if any argument is passed as `float',
2999 since without a prototype it would be `double'. */
3000 if (formal_prec == TYPE_PRECISION (float_type_node)
3001 && type != dfloat32_type_node)
3002 warning (0, "passing argument %d of %qE as %<float%> "
3003 "rather than %<double%> due to prototype",
3004 argnum, rname);
3006 /* Warn if mismatch between argument and prototype
3007 for decimal float types. Warn of conversions with
3008 binary float types and of precision narrowing due to
3009 prototype. */
3010 else if (type != valtype
3011 && (type == dfloat32_type_node
3012 || type == dfloat64_type_node
3013 || type == dfloat128_type_node
3014 || valtype == dfloat32_type_node
3015 || valtype == dfloat64_type_node
3016 || valtype == dfloat128_type_node)
3017 && (formal_prec
3018 <= TYPE_PRECISION (valtype)
3019 || (type == dfloat128_type_node
3020 && (valtype
3021 != dfloat64_type_node
3022 && (valtype
3023 != dfloat32_type_node)))
3024 || (type == dfloat64_type_node
3025 && (valtype
3026 != dfloat32_type_node))))
3027 warning (0, "passing argument %d of %qE as %qT "
3028 "rather than %qT due to prototype",
3029 argnum, rname, type, valtype);
3032 /* Detect integer changing in width or signedness.
3033 These warnings are only activated with
3034 -Wtraditional-conversion, not with -Wtraditional. */
3035 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3036 && INTEGRAL_TYPE_P (valtype))
3038 tree would_have_been = default_conversion (val);
3039 tree type1 = TREE_TYPE (would_have_been);
3041 if (TREE_CODE (type) == ENUMERAL_TYPE
3042 && (TYPE_MAIN_VARIANT (type)
3043 == TYPE_MAIN_VARIANT (valtype)))
3044 /* No warning if function asks for enum
3045 and the actual arg is that enum type. */
3047 else if (formal_prec != TYPE_PRECISION (type1))
3048 warning (OPT_Wtraditional_conversion,
3049 "passing argument %d of %qE "
3050 "with different width due to prototype",
3051 argnum, rname);
3052 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3054 /* Don't complain if the formal parameter type
3055 is an enum, because we can't tell now whether
3056 the value was an enum--even the same enum. */
3057 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3059 else if (TREE_CODE (val) == INTEGER_CST
3060 && int_fits_type_p (val, type))
3061 /* Change in signedness doesn't matter
3062 if a constant value is unaffected. */
3064 /* If the value is extended from a narrower
3065 unsigned type, it doesn't matter whether we
3066 pass it as signed or unsigned; the value
3067 certainly is the same either way. */
3068 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3069 && TYPE_UNSIGNED (valtype))
3071 else if (TYPE_UNSIGNED (type))
3072 warning (OPT_Wtraditional_conversion,
3073 "passing argument %d of %qE "
3074 "as unsigned due to prototype",
3075 argnum, rname);
3076 else
3077 warning (OPT_Wtraditional_conversion,
3078 "passing argument %d of %qE "
3079 "as signed due to prototype", argnum, rname);
3083 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3084 sake of better warnings from convert_and_check. */
3085 if (excess_precision)
3086 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3087 origtype = (origtypes == NULL
3088 ? NULL_TREE
3089 : VEC_index (tree, origtypes, parmnum));
3090 parmval = convert_for_assignment (input_location, type, val,
3091 origtype, ic_argpass, npc,
3092 fundecl, function,
3093 parmnum + 1);
3095 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3096 && INTEGRAL_TYPE_P (type)
3097 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3098 parmval = default_conversion (parmval);
3101 else if (TREE_CODE (valtype) == REAL_TYPE
3102 && (TYPE_PRECISION (valtype)
3103 < TYPE_PRECISION (double_type_node))
3104 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3106 if (type_generic)
3107 parmval = val;
3108 else
3110 /* Convert `float' to `double'. */
3111 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3112 warning (OPT_Wdouble_promotion,
3113 "implicit conversion from %qT to %qT when passing "
3114 "argument to function",
3115 valtype, double_type_node);
3116 parmval = convert (double_type_node, val);
3119 else if (excess_precision && !type_generic)
3120 /* A "double" argument with excess precision being passed
3121 without a prototype or in variable arguments. */
3122 parmval = convert (valtype, val);
3123 else if ((invalid_func_diag =
3124 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3126 error (invalid_func_diag);
3127 return -1;
3129 else
3130 /* Convert `short' and `char' to full-size `int'. */
3131 parmval = default_conversion (val);
3133 VEC_replace (tree, values, parmnum, parmval);
3134 if (parmval == error_mark_node)
3135 error_args = true;
3137 if (typetail)
3138 typetail = TREE_CHAIN (typetail);
3141 gcc_assert (parmnum == VEC_length (tree, values));
3143 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3145 error_at (input_location,
3146 "too few arguments to function %qE", function);
3147 if (fundecl && !DECL_BUILT_IN (fundecl))
3148 inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3149 return -1;
3152 return error_args ? -1 : (int) parmnum;
3155 /* This is the entry point used by the parser to build unary operators
3156 in the input. CODE, a tree_code, specifies the unary operator, and
3157 ARG is the operand. For unary plus, the C parser currently uses
3158 CONVERT_EXPR for code.
3160 LOC is the location to use for the tree generated.
3163 struct c_expr
3164 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3166 struct c_expr result;
3168 result.value = build_unary_op (loc, code, arg.value, 0);
3169 result.original_code = code;
3170 result.original_type = NULL;
3172 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3173 overflow_warning (loc, result.value);
3175 return result;
3178 /* This is the entry point used by the parser to build binary operators
3179 in the input. CODE, a tree_code, specifies the binary operator, and
3180 ARG1 and ARG2 are the operands. In addition to constructing the
3181 expression, we check for operands that were written with other binary
3182 operators in a way that is likely to confuse the user.
3184 LOCATION is the location of the binary operator. */
3186 struct c_expr
3187 parser_build_binary_op (location_t location, enum tree_code code,
3188 struct c_expr arg1, struct c_expr arg2)
3190 struct c_expr result;
3192 enum tree_code code1 = arg1.original_code;
3193 enum tree_code code2 = arg2.original_code;
3194 tree type1 = (arg1.original_type
3195 ? arg1.original_type
3196 : TREE_TYPE (arg1.value));
3197 tree type2 = (arg2.original_type
3198 ? arg2.original_type
3199 : TREE_TYPE (arg2.value));
3201 result.value = build_binary_op (location, code,
3202 arg1.value, arg2.value, 1);
3203 result.original_code = code;
3204 result.original_type = NULL;
3206 if (TREE_CODE (result.value) == ERROR_MARK)
3207 return result;
3209 if (location != UNKNOWN_LOCATION)
3210 protected_set_expr_location (result.value, location);
3212 /* Check for cases such as x+y<<z which users are likely
3213 to misinterpret. */
3214 if (warn_parentheses)
3215 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3217 if (warn_logical_op)
3218 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3219 code1, arg1.value, code2, arg2.value);
3221 /* Warn about comparisons against string literals, with the exception
3222 of testing for equality or inequality of a string literal with NULL. */
3223 if (code == EQ_EXPR || code == NE_EXPR)
3225 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3226 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3227 warning_at (location, OPT_Waddress,
3228 "comparison with string literal results in unspecified behavior");
3230 else if (TREE_CODE_CLASS (code) == tcc_comparison
3231 && (code1 == STRING_CST || code2 == STRING_CST))
3232 warning_at (location, OPT_Waddress,
3233 "comparison with string literal results in unspecified behavior");
3235 if (TREE_OVERFLOW_P (result.value)
3236 && !TREE_OVERFLOW_P (arg1.value)
3237 && !TREE_OVERFLOW_P (arg2.value))
3238 overflow_warning (location, result.value);
3240 /* Warn about comparisons of different enum types. */
3241 if (warn_enum_compare
3242 && TREE_CODE_CLASS (code) == tcc_comparison
3243 && TREE_CODE (type1) == ENUMERAL_TYPE
3244 && TREE_CODE (type2) == ENUMERAL_TYPE
3245 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3246 warning_at (location, OPT_Wenum_compare,
3247 "comparison between %qT and %qT",
3248 type1, type2);
3250 return result;
3253 /* Return a tree for the difference of pointers OP0 and OP1.
3254 The resulting tree has type int. */
3256 static tree
3257 pointer_diff (location_t loc, tree op0, tree op1)
3259 tree restype = ptrdiff_type_node;
3260 tree result, inttype;
3262 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3263 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3264 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3265 tree con0, con1, lit0, lit1;
3266 tree orig_op1 = op1;
3268 /* If the operands point into different address spaces, we need to
3269 explicitly convert them to pointers into the common address space
3270 before we can subtract the numerical address values. */
3271 if (as0 != as1)
3273 addr_space_t as_common;
3274 tree common_type;
3276 /* Determine the common superset address space. This is guaranteed
3277 to exist because the caller verified that comp_target_types
3278 returned non-zero. */
3279 if (!addr_space_superset (as0, as1, &as_common))
3280 gcc_unreachable ();
3282 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3283 op0 = convert (common_type, op0);
3284 op1 = convert (common_type, op1);
3287 /* Determine integer type to perform computations in. This will usually
3288 be the same as the result type (ptrdiff_t), but may need to be a wider
3289 type if pointers for the address space are wider than ptrdiff_t. */
3290 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3291 inttype = lang_hooks.types.type_for_size
3292 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3293 else
3294 inttype = restype;
3297 if (TREE_CODE (target_type) == VOID_TYPE)
3298 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3299 "pointer of type %<void *%> used in subtraction");
3300 if (TREE_CODE (target_type) == FUNCTION_TYPE)
3301 pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3302 "pointer to a function used in subtraction");
3304 /* If the conversion to ptrdiff_type does anything like widening or
3305 converting a partial to an integral mode, we get a convert_expression
3306 that is in the way to do any simplifications.
3307 (fold-const.c doesn't know that the extra bits won't be needed.
3308 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3309 different mode in place.)
3310 So first try to find a common term here 'by hand'; we want to cover
3311 at least the cases that occur in legal static initializers. */
3312 if (CONVERT_EXPR_P (op0)
3313 && (TYPE_PRECISION (TREE_TYPE (op0))
3314 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3315 con0 = TREE_OPERAND (op0, 0);
3316 else
3317 con0 = op0;
3318 if (CONVERT_EXPR_P (op1)
3319 && (TYPE_PRECISION (TREE_TYPE (op1))
3320 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3321 con1 = TREE_OPERAND (op1, 0);
3322 else
3323 con1 = op1;
3325 if (TREE_CODE (con0) == PLUS_EXPR)
3327 lit0 = TREE_OPERAND (con0, 1);
3328 con0 = TREE_OPERAND (con0, 0);
3330 else
3331 lit0 = integer_zero_node;
3333 if (TREE_CODE (con1) == PLUS_EXPR)
3335 lit1 = TREE_OPERAND (con1, 1);
3336 con1 = TREE_OPERAND (con1, 0);
3338 else
3339 lit1 = integer_zero_node;
3341 if (operand_equal_p (con0, con1, 0))
3343 op0 = lit0;
3344 op1 = lit1;
3348 /* First do the subtraction as integers;
3349 then drop through to build the divide operator.
3350 Do not do default conversions on the minus operator
3351 in case restype is a short type. */
3353 op0 = build_binary_op (loc,
3354 MINUS_EXPR, convert (inttype, op0),
3355 convert (inttype, op1), 0);
3356 /* This generates an error if op1 is pointer to incomplete type. */
3357 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3358 error_at (loc, "arithmetic on pointer to an incomplete type");
3360 /* This generates an error if op0 is pointer to incomplete type. */
3361 op1 = c_size_in_bytes (target_type);
3363 /* Divide by the size, in easiest possible way. */
3364 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3365 op0, convert (inttype, op1));
3367 /* Convert to final result type if necessary. */
3368 return convert (restype, result);
3371 /* Construct and perhaps optimize a tree representation
3372 for a unary operation. CODE, a tree_code, specifies the operation
3373 and XARG is the operand.
3374 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3375 the default promotions (such as from short to int).
3376 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3377 allows non-lvalues; this is only used to handle conversion of non-lvalue
3378 arrays to pointers in C99.
3380 LOCATION is the location of the operator. */
3382 tree
3383 build_unary_op (location_t location,
3384 enum tree_code code, tree xarg, int flag)
3386 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3387 tree arg = xarg;
3388 tree argtype = 0;
3389 enum tree_code typecode;
3390 tree val;
3391 tree ret = error_mark_node;
3392 tree eptype = NULL_TREE;
3393 int noconvert = flag;
3394 const char *invalid_op_diag;
3395 bool int_operands;
3397 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3398 if (int_operands)
3399 arg = remove_c_maybe_const_expr (arg);
3401 if (code != ADDR_EXPR)
3402 arg = require_complete_type (arg);
3404 typecode = TREE_CODE (TREE_TYPE (arg));
3405 if (typecode == ERROR_MARK)
3406 return error_mark_node;
3407 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3408 typecode = INTEGER_TYPE;
3410 if ((invalid_op_diag
3411 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3413 error_at (location, invalid_op_diag);
3414 return error_mark_node;
3417 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3419 eptype = TREE_TYPE (arg);
3420 arg = TREE_OPERAND (arg, 0);
3423 switch (code)
3425 case CONVERT_EXPR:
3426 /* This is used for unary plus, because a CONVERT_EXPR
3427 is enough to prevent anybody from looking inside for
3428 associativity, but won't generate any code. */
3429 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3430 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3431 || typecode == VECTOR_TYPE))
3433 error_at (location, "wrong type argument to unary plus");
3434 return error_mark_node;
3436 else if (!noconvert)
3437 arg = default_conversion (arg);
3438 arg = non_lvalue_loc (location, arg);
3439 break;
3441 case NEGATE_EXPR:
3442 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3443 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3444 || typecode == VECTOR_TYPE))
3446 error_at (location, "wrong type argument to unary minus");
3447 return error_mark_node;
3449 else if (!noconvert)
3450 arg = default_conversion (arg);
3451 break;
3453 case BIT_NOT_EXPR:
3454 /* ~ works on integer types and non float vectors. */
3455 if (typecode == INTEGER_TYPE
3456 || (typecode == VECTOR_TYPE
3457 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3459 if (!noconvert)
3460 arg = default_conversion (arg);
3462 else if (typecode == COMPLEX_TYPE)
3464 code = CONJ_EXPR;
3465 pedwarn (location, OPT_pedantic,
3466 "ISO C does not support %<~%> for complex conjugation");
3467 if (!noconvert)
3468 arg = default_conversion (arg);
3470 else
3472 error_at (location, "wrong type argument to bit-complement");
3473 return error_mark_node;
3475 break;
3477 case ABS_EXPR:
3478 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3480 error_at (location, "wrong type argument to abs");
3481 return error_mark_node;
3483 else if (!noconvert)
3484 arg = default_conversion (arg);
3485 break;
3487 case CONJ_EXPR:
3488 /* Conjugating a real value is a no-op, but allow it anyway. */
3489 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3490 || typecode == COMPLEX_TYPE))
3492 error_at (location, "wrong type argument to conjugation");
3493 return error_mark_node;
3495 else if (!noconvert)
3496 arg = default_conversion (arg);
3497 break;
3499 case TRUTH_NOT_EXPR:
3500 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3501 && typecode != REAL_TYPE && typecode != POINTER_TYPE
3502 && typecode != COMPLEX_TYPE)
3504 error_at (location,
3505 "wrong type argument to unary exclamation mark");
3506 return error_mark_node;
3508 arg = c_objc_common_truthvalue_conversion (location, arg);
3509 ret = invert_truthvalue_loc (location, arg);
3510 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3511 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3512 location = EXPR_LOCATION (ret);
3513 goto return_build_unary_op;
3515 case REALPART_EXPR:
3516 if (TREE_CODE (arg) == COMPLEX_CST)
3517 ret = TREE_REALPART (arg);
3518 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3519 ret = fold_build1_loc (location,
3520 REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3521 else
3522 ret = arg;
3523 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3524 eptype = TREE_TYPE (eptype);
3525 goto return_build_unary_op;
3527 case IMAGPART_EXPR:
3528 if (TREE_CODE (arg) == COMPLEX_CST)
3529 ret = TREE_IMAGPART (arg);
3530 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3531 ret = fold_build1_loc (location,
3532 IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3533 else
3534 ret = omit_one_operand_loc (location, TREE_TYPE (arg),
3535 integer_zero_node, arg);
3536 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3537 eptype = TREE_TYPE (eptype);
3538 goto return_build_unary_op;
3540 case PREINCREMENT_EXPR:
3541 case POSTINCREMENT_EXPR:
3542 case PREDECREMENT_EXPR:
3543 case POSTDECREMENT_EXPR:
3545 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3547 tree inner = build_unary_op (location, code,
3548 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3549 if (inner == error_mark_node)
3550 return error_mark_node;
3551 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3552 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3553 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3554 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3555 goto return_build_unary_op;
3558 /* Complain about anything that is not a true lvalue. */
3559 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3560 || code == POSTINCREMENT_EXPR)
3561 ? lv_increment
3562 : lv_decrement)))
3563 return error_mark_node;
3565 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3567 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3568 warning_at (location, OPT_Wc___compat,
3569 "increment of enumeration value is invalid in C++");
3570 else
3571 warning_at (location, OPT_Wc___compat,
3572 "decrement of enumeration value is invalid in C++");
3575 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3576 arg = c_fully_fold (arg, false, NULL);
3578 /* Increment or decrement the real part of the value,
3579 and don't change the imaginary part. */
3580 if (typecode == COMPLEX_TYPE)
3582 tree real, imag;
3584 pedwarn (location, OPT_pedantic,
3585 "ISO C does not support %<++%> and %<--%> on complex types");
3587 arg = stabilize_reference (arg);
3588 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3589 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3590 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3591 if (real == error_mark_node || imag == error_mark_node)
3592 return error_mark_node;
3593 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3594 real, imag);
3595 goto return_build_unary_op;
3598 /* Report invalid types. */
3600 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3601 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3603 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3604 error_at (location, "wrong type argument to increment");
3605 else
3606 error_at (location, "wrong type argument to decrement");
3608 return error_mark_node;
3612 tree inc;
3614 argtype = TREE_TYPE (arg);
3616 /* Compute the increment. */
3618 if (typecode == POINTER_TYPE)
3620 /* If pointer target is an undefined struct,
3621 we just cannot know how to do the arithmetic. */
3622 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3624 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3625 error_at (location,
3626 "increment of pointer to unknown structure");
3627 else
3628 error_at (location,
3629 "decrement of pointer to unknown structure");
3631 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3632 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3634 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3635 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3636 "wrong type argument to increment");
3637 else
3638 pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3639 "wrong type argument to decrement");
3642 inc = c_size_in_bytes (TREE_TYPE (argtype));
3643 inc = fold_convert_loc (location, sizetype, inc);
3645 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3647 /* For signed fract types, we invert ++ to -- or
3648 -- to ++, and change inc from 1 to -1, because
3649 it is not possible to represent 1 in signed fract constants.
3650 For unsigned fract types, the result always overflows and
3651 we get an undefined (original) or the maximum value. */
3652 if (code == PREINCREMENT_EXPR)
3653 code = PREDECREMENT_EXPR;
3654 else if (code == PREDECREMENT_EXPR)
3655 code = PREINCREMENT_EXPR;
3656 else if (code == POSTINCREMENT_EXPR)
3657 code = POSTDECREMENT_EXPR;
3658 else /* code == POSTDECREMENT_EXPR */
3659 code = POSTINCREMENT_EXPR;
3661 inc = integer_minus_one_node;
3662 inc = convert (argtype, inc);
3664 else
3666 inc = integer_one_node;
3667 inc = convert (argtype, inc);
3670 /* Report a read-only lvalue. */
3671 if (TYPE_READONLY (argtype))
3673 readonly_error (arg,
3674 ((code == PREINCREMENT_EXPR
3675 || code == POSTINCREMENT_EXPR)
3676 ? lv_increment : lv_decrement));
3677 return error_mark_node;
3679 else if (TREE_READONLY (arg))
3680 readonly_warning (arg,
3681 ((code == PREINCREMENT_EXPR
3682 || code == POSTINCREMENT_EXPR)
3683 ? lv_increment : lv_decrement));
3685 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3686 val = boolean_increment (code, arg);
3687 else
3688 val = build2 (code, TREE_TYPE (arg), arg, inc);
3689 TREE_SIDE_EFFECTS (val) = 1;
3690 if (TREE_CODE (val) != code)
3691 TREE_NO_WARNING (val) = 1;
3692 ret = val;
3693 goto return_build_unary_op;
3696 case ADDR_EXPR:
3697 /* Note that this operation never does default_conversion. */
3699 /* The operand of unary '&' must be an lvalue (which excludes
3700 expressions of type void), or, in C99, the result of a [] or
3701 unary '*' operator. */
3702 if (VOID_TYPE_P (TREE_TYPE (arg))
3703 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3704 && (TREE_CODE (arg) != INDIRECT_REF
3705 || !flag_isoc99))
3706 pedwarn (location, 0, "taking address of expression of type %<void%>");
3708 /* Let &* cancel out to simplify resulting code. */
3709 if (TREE_CODE (arg) == INDIRECT_REF)
3711 /* Don't let this be an lvalue. */
3712 if (lvalue_p (TREE_OPERAND (arg, 0)))
3713 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3714 ret = TREE_OPERAND (arg, 0);
3715 goto return_build_unary_op;
3718 /* For &x[y], return x+y */
3719 if (TREE_CODE (arg) == ARRAY_REF)
3721 tree op0 = TREE_OPERAND (arg, 0);
3722 if (!c_mark_addressable (op0))
3723 return error_mark_node;
3724 return build_binary_op (location, PLUS_EXPR,
3725 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3726 ? array_to_pointer_conversion (location,
3727 op0)
3728 : op0),
3729 TREE_OPERAND (arg, 1), 1);
3732 /* Anything not already handled and not a true memory reference
3733 or a non-lvalue array is an error. */
3734 else if (typecode != FUNCTION_TYPE && !flag
3735 && !lvalue_or_else (arg, lv_addressof))
3736 return error_mark_node;
3738 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3739 folding later. */
3740 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3742 tree inner = build_unary_op (location, code,
3743 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3744 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3745 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3746 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3747 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3748 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3749 goto return_build_unary_op;
3752 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3753 argtype = TREE_TYPE (arg);
3755 /* If the lvalue is const or volatile, merge that into the type
3756 to which the address will point. This should only be needed
3757 for function types. */
3758 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3759 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3761 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3762 int quals = orig_quals;
3764 if (TREE_READONLY (arg))
3765 quals |= TYPE_QUAL_CONST;
3766 if (TREE_THIS_VOLATILE (arg))
3767 quals |= TYPE_QUAL_VOLATILE;
3769 gcc_assert (quals == orig_quals
3770 || TREE_CODE (argtype) == FUNCTION_TYPE);
3772 argtype = c_build_qualified_type (argtype, quals);
3775 if (!c_mark_addressable (arg))
3776 return error_mark_node;
3778 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3779 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3781 argtype = build_pointer_type (argtype);
3783 /* ??? Cope with user tricks that amount to offsetof. Delete this
3784 when we have proper support for integer constant expressions. */
3785 val = get_base_address (arg);
3786 if (val && TREE_CODE (val) == INDIRECT_REF
3787 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3789 tree op0 = fold_convert_loc (location, sizetype,
3790 fold_offsetof (arg, val)), op1;
3792 op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3793 ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3794 goto return_build_unary_op;
3797 val = build1 (ADDR_EXPR, argtype, arg);
3799 ret = val;
3800 goto return_build_unary_op;
3802 default:
3803 gcc_unreachable ();
3806 if (argtype == 0)
3807 argtype = TREE_TYPE (arg);
3808 if (TREE_CODE (arg) == INTEGER_CST)
3809 ret = (require_constant_value
3810 ? fold_build1_initializer_loc (location, code, argtype, arg)
3811 : fold_build1_loc (location, code, argtype, arg));
3812 else
3813 ret = build1 (code, argtype, arg);
3814 return_build_unary_op:
3815 gcc_assert (ret != error_mark_node);
3816 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3817 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3818 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3819 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3820 ret = note_integer_operands (ret);
3821 if (eptype)
3822 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3823 protected_set_expr_location (ret, location);
3824 return ret;
3827 /* Return nonzero if REF is an lvalue valid for this language.
3828 Lvalues can be assigned, unless their type has TYPE_READONLY.
3829 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
3831 bool
3832 lvalue_p (const_tree ref)
3834 const enum tree_code code = TREE_CODE (ref);
3836 switch (code)
3838 case REALPART_EXPR:
3839 case IMAGPART_EXPR:
3840 case COMPONENT_REF:
3841 return lvalue_p (TREE_OPERAND (ref, 0));
3843 case C_MAYBE_CONST_EXPR:
3844 return lvalue_p (TREE_OPERAND (ref, 1));
3846 case COMPOUND_LITERAL_EXPR:
3847 case STRING_CST:
3848 return 1;
3850 case INDIRECT_REF:
3851 case ARRAY_REF:
3852 case VAR_DECL:
3853 case PARM_DECL:
3854 case RESULT_DECL:
3855 case ERROR_MARK:
3856 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3857 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3859 case BIND_EXPR:
3860 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3862 default:
3863 return 0;
3867 /* Give an error for storing in something that is 'const'. */
3869 static void
3870 readonly_error (tree arg, enum lvalue_use use)
3872 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3873 || use == lv_asm);
3874 /* Using this macro rather than (for example) arrays of messages
3875 ensures that all the format strings are checked at compile
3876 time. */
3877 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
3878 : (use == lv_increment ? (I) \
3879 : (use == lv_decrement ? (D) : (AS))))
3880 if (TREE_CODE (arg) == COMPONENT_REF)
3882 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3883 readonly_error (TREE_OPERAND (arg, 0), use);
3884 else
3885 error (READONLY_MSG (G_("assignment of read-only member %qD"),
3886 G_("increment of read-only member %qD"),
3887 G_("decrement of read-only member %qD"),
3888 G_("read-only member %qD used as %<asm%> output")),
3889 TREE_OPERAND (arg, 1));
3891 else if (TREE_CODE (arg) == VAR_DECL)
3892 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3893 G_("increment of read-only variable %qD"),
3894 G_("decrement of read-only variable %qD"),
3895 G_("read-only variable %qD used as %<asm%> output")),
3896 arg);
3897 else
3898 error (READONLY_MSG (G_("assignment of read-only location %qE"),
3899 G_("increment of read-only location %qE"),
3900 G_("decrement of read-only location %qE"),
3901 G_("read-only location %qE used as %<asm%> output")),
3902 arg);
3905 /* Give a warning for storing in something that is read-only in GCC
3906 terms but not const in ISO C terms. */
3908 static void
3909 readonly_warning (tree arg, enum lvalue_use use)
3911 switch (use)
3913 case lv_assign:
3914 warning (0, "assignment of read-only location %qE", arg);
3915 break;
3916 case lv_increment:
3917 warning (0, "increment of read-only location %qE", arg);
3918 break;
3919 case lv_decrement:
3920 warning (0, "decrement of read-only location %qE", arg);
3921 break;
3922 default:
3923 gcc_unreachable ();
3925 return;
3929 /* Return nonzero if REF is an lvalue valid for this language;
3930 otherwise, print an error message and return zero. USE says
3931 how the lvalue is being used and so selects the error message. */
3933 static int
3934 lvalue_or_else (const_tree ref, enum lvalue_use use)
3936 int win = lvalue_p (ref);
3938 if (!win)
3939 lvalue_error (use);
3941 return win;
3944 /* Mark EXP saying that we need to be able to take the
3945 address of it; it should not be allocated in a register.
3946 Returns true if successful. */
3948 bool
3949 c_mark_addressable (tree exp)
3951 tree x = exp;
3953 while (1)
3954 switch (TREE_CODE (x))
3956 case COMPONENT_REF:
3957 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3959 error
3960 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3961 return false;
3964 /* ... fall through ... */
3966 case ADDR_EXPR:
3967 case ARRAY_REF:
3968 case REALPART_EXPR:
3969 case IMAGPART_EXPR:
3970 x = TREE_OPERAND (x, 0);
3971 break;
3973 case COMPOUND_LITERAL_EXPR:
3974 case CONSTRUCTOR:
3975 TREE_ADDRESSABLE (x) = 1;
3976 return true;
3978 case VAR_DECL:
3979 case CONST_DECL:
3980 case PARM_DECL:
3981 case RESULT_DECL:
3982 if (C_DECL_REGISTER (x)
3983 && DECL_NONLOCAL (x))
3985 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3987 error
3988 ("global register variable %qD used in nested function", x);
3989 return false;
3991 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3993 else if (C_DECL_REGISTER (x))
3995 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3996 error ("address of global register variable %qD requested", x);
3997 else
3998 error ("address of register variable %qD requested", x);
3999 return false;
4002 /* drops in */
4003 case FUNCTION_DECL:
4004 TREE_ADDRESSABLE (x) = 1;
4005 /* drops out */
4006 default:
4007 return true;
4011 /* Convert EXPR to TYPE, warning about conversion problems with
4012 constants. SEMANTIC_TYPE is the type this conversion would use
4013 without excess precision. If SEMANTIC_TYPE is NULL, this function
4014 is equivalent to convert_and_check. This function is a wrapper that
4015 handles conversions that may be different than
4016 the usual ones because of excess precision. */
4018 static tree
4019 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4021 if (TREE_TYPE (expr) == type)
4022 return expr;
4024 if (!semantic_type)
4025 return convert_and_check (type, expr);
4027 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4028 && TREE_TYPE (expr) != semantic_type)
4030 /* For integers, we need to check the real conversion, not
4031 the conversion to the excess precision type. */
4032 expr = convert_and_check (semantic_type, expr);
4034 /* Result type is the excess precision type, which should be
4035 large enough, so do not check. */
4036 return convert (type, expr);
4039 /* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4040 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4041 if folded to an integer constant then the unselected half may
4042 contain arbitrary operations not normally permitted in constant
4043 expressions. Set the location of the expression to LOC. */
4045 tree
4046 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4047 tree op1, tree op1_original_type, tree op2,
4048 tree op2_original_type)
4050 tree type1;
4051 tree type2;
4052 enum tree_code code1;
4053 enum tree_code code2;
4054 tree result_type = NULL;
4055 tree semantic_result_type = NULL;
4056 tree orig_op1 = op1, orig_op2 = op2;
4057 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4058 bool ifexp_int_operands;
4059 tree ret;
4060 bool objc_ok;
4062 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4063 if (op1_int_operands)
4064 op1 = remove_c_maybe_const_expr (op1);
4065 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4066 if (op2_int_operands)
4067 op2 = remove_c_maybe_const_expr (op2);
4068 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4069 if (ifexp_int_operands)
4070 ifexp = remove_c_maybe_const_expr (ifexp);
4072 /* Promote both alternatives. */
4074 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4075 op1 = default_conversion (op1);
4076 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4077 op2 = default_conversion (op2);
4079 if (TREE_CODE (ifexp) == ERROR_MARK
4080 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4081 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4082 return error_mark_node;
4084 type1 = TREE_TYPE (op1);
4085 code1 = TREE_CODE (type1);
4086 type2 = TREE_TYPE (op2);
4087 code2 = TREE_CODE (type2);
4089 /* C90 does not permit non-lvalue arrays in conditional expressions.
4090 In C99 they will be pointers by now. */
4091 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4093 error_at (colon_loc, "non-lvalue array in conditional expression");
4094 return error_mark_node;
4097 objc_ok = objc_compare_types (type1, type2, -3, NULL_TREE);
4099 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4100 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4101 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4102 || code1 == COMPLEX_TYPE)
4103 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4104 || code2 == COMPLEX_TYPE))
4106 semantic_result_type = c_common_type (type1, type2);
4107 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4109 op1 = TREE_OPERAND (op1, 0);
4110 type1 = TREE_TYPE (op1);
4111 gcc_assert (TREE_CODE (type1) == code1);
4113 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4115 op2 = TREE_OPERAND (op2, 0);
4116 type2 = TREE_TYPE (op2);
4117 gcc_assert (TREE_CODE (type2) == code2);
4121 if (warn_cxx_compat)
4123 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4124 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4126 if (TREE_CODE (t1) == ENUMERAL_TYPE
4127 && TREE_CODE (t2) == ENUMERAL_TYPE
4128 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4129 warning_at (colon_loc, OPT_Wc___compat,
4130 ("different enum types in conditional is "
4131 "invalid in C++: %qT vs %qT"),
4132 t1, t2);
4135 /* Quickly detect the usual case where op1 and op2 have the same type
4136 after promotion. */
4137 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4139 if (type1 == type2)
4140 result_type = type1;
4141 else
4142 result_type = TYPE_MAIN_VARIANT (type1);
4144 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4145 || code1 == COMPLEX_TYPE)
4146 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4147 || code2 == COMPLEX_TYPE))
4149 result_type = c_common_type (type1, type2);
4150 do_warn_double_promotion (result_type, type1, type2,
4151 "implicit conversion from %qT to %qT to "
4152 "match other result of conditional",
4153 colon_loc);
4155 /* If -Wsign-compare, warn here if type1 and type2 have
4156 different signedness. We'll promote the signed to unsigned
4157 and later code won't know it used to be different.
4158 Do this check on the original types, so that explicit casts
4159 will be considered, but default promotions won't. */
4160 if (c_inhibit_evaluation_warnings == 0)
4162 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4163 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4165 if (unsigned_op1 ^ unsigned_op2)
4167 bool ovf;
4169 /* Do not warn if the result type is signed, since the
4170 signed type will only be chosen if it can represent
4171 all the values of the unsigned type. */
4172 if (!TYPE_UNSIGNED (result_type))
4173 /* OK */;
4174 else
4176 bool op1_maybe_const = true;
4177 bool op2_maybe_const = true;
4179 /* Do not warn if the signed quantity is an
4180 unsuffixed integer literal (or some static
4181 constant expression involving such literals) and
4182 it is non-negative. This warning requires the
4183 operands to be folded for best results, so do
4184 that folding in this case even without
4185 warn_sign_compare to avoid warning options
4186 possibly affecting code generation. */
4187 c_inhibit_evaluation_warnings
4188 += (ifexp == truthvalue_false_node);
4189 op1 = c_fully_fold (op1, require_constant_value,
4190 &op1_maybe_const);
4191 c_inhibit_evaluation_warnings
4192 -= (ifexp == truthvalue_false_node);
4194 c_inhibit_evaluation_warnings
4195 += (ifexp == truthvalue_true_node);
4196 op2 = c_fully_fold (op2, require_constant_value,
4197 &op2_maybe_const);
4198 c_inhibit_evaluation_warnings
4199 -= (ifexp == truthvalue_true_node);
4201 if (warn_sign_compare)
4203 if ((unsigned_op2
4204 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4205 || (unsigned_op1
4206 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4207 /* OK */;
4208 else
4209 warning_at (colon_loc, OPT_Wsign_compare,
4210 ("signed and unsigned type in "
4211 "conditional expression"));
4213 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4214 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4215 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4216 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4221 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4223 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4224 pedwarn (colon_loc, OPT_pedantic,
4225 "ISO C forbids conditional expr with only one void side");
4226 result_type = void_type_node;
4228 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4230 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4231 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4232 addr_space_t as_common;
4234 if (comp_target_types (colon_loc, type1, type2))
4235 result_type = common_pointer_type (type1, type2);
4236 else if (null_pointer_constant_p (orig_op1))
4237 result_type = type2;
4238 else if (null_pointer_constant_p (orig_op2))
4239 result_type = type1;
4240 else if (!addr_space_superset (as1, as2, &as_common))
4242 error_at (colon_loc, "pointers to disjoint address spaces "
4243 "used in conditional expression");
4244 return error_mark_node;
4246 else if (VOID_TYPE_P (TREE_TYPE (type1)))
4248 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4249 pedwarn (colon_loc, OPT_pedantic,
4250 "ISO C forbids conditional expr between "
4251 "%<void *%> and function pointer");
4252 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4253 TREE_TYPE (type2)));
4255 else if (VOID_TYPE_P (TREE_TYPE (type2)))
4257 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4258 pedwarn (colon_loc, OPT_pedantic,
4259 "ISO C forbids conditional expr between "
4260 "%<void *%> and function pointer");
4261 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4262 TREE_TYPE (type1)));
4264 else
4266 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4268 if (!objc_ok)
4269 pedwarn (colon_loc, 0,
4270 "pointer type mismatch in conditional expression");
4271 result_type = build_pointer_type
4272 (build_qualified_type (void_type_node, qual));
4275 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4277 if (!null_pointer_constant_p (orig_op2))
4278 pedwarn (colon_loc, 0,
4279 "pointer/integer type mismatch in conditional expression");
4280 else
4282 op2 = null_pointer_node;
4284 result_type = type1;
4286 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4288 if (!null_pointer_constant_p (orig_op1))
4289 pedwarn (colon_loc, 0,
4290 "pointer/integer type mismatch in conditional expression");
4291 else
4293 op1 = null_pointer_node;
4295 result_type = type2;
4298 if (!result_type)
4300 if (flag_cond_mismatch)
4301 result_type = void_type_node;
4302 else
4304 error_at (colon_loc, "type mismatch in conditional expression");
4305 return error_mark_node;
4309 /* Merge const and volatile flags of the incoming types. */
4310 result_type
4311 = build_type_variant (result_type,
4312 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4313 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4315 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4316 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4318 if (ifexp_bcp && ifexp == truthvalue_true_node)
4320 op2_int_operands = true;
4321 op1 = c_fully_fold (op1, require_constant_value, NULL);
4323 if (ifexp_bcp && ifexp == truthvalue_false_node)
4325 op1_int_operands = true;
4326 op2 = c_fully_fold (op2, require_constant_value, NULL);
4328 int_const = int_operands = (ifexp_int_operands
4329 && op1_int_operands
4330 && op2_int_operands);
4331 if (int_operands)
4333 int_const = ((ifexp == truthvalue_true_node
4334 && TREE_CODE (orig_op1) == INTEGER_CST
4335 && !TREE_OVERFLOW (orig_op1))
4336 || (ifexp == truthvalue_false_node
4337 && TREE_CODE (orig_op2) == INTEGER_CST
4338 && !TREE_OVERFLOW (orig_op2)));
4340 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4341 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4342 else
4344 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4345 if (int_operands)
4346 ret = note_integer_operands (ret);
4348 if (semantic_result_type)
4349 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4351 protected_set_expr_location (ret, colon_loc);
4352 return ret;
4355 /* Return a compound expression that performs two expressions and
4356 returns the value of the second of them.
4358 LOC is the location of the COMPOUND_EXPR. */
4360 tree
4361 build_compound_expr (location_t loc, tree expr1, tree expr2)
4363 bool expr1_int_operands, expr2_int_operands;
4364 tree eptype = NULL_TREE;
4365 tree ret;
4367 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4368 if (expr1_int_operands)
4369 expr1 = remove_c_maybe_const_expr (expr1);
4370 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4371 if (expr2_int_operands)
4372 expr2 = remove_c_maybe_const_expr (expr2);
4374 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4375 expr1 = TREE_OPERAND (expr1, 0);
4376 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4378 eptype = TREE_TYPE (expr2);
4379 expr2 = TREE_OPERAND (expr2, 0);
4382 if (!TREE_SIDE_EFFECTS (expr1))
4384 /* The left-hand operand of a comma expression is like an expression
4385 statement: with -Wunused, we should warn if it doesn't have
4386 any side-effects, unless it was explicitly cast to (void). */
4387 if (warn_unused_value)
4389 if (VOID_TYPE_P (TREE_TYPE (expr1))
4390 && CONVERT_EXPR_P (expr1))
4391 ; /* (void) a, b */
4392 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4393 && TREE_CODE (expr1) == COMPOUND_EXPR
4394 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4395 ; /* (void) a, (void) b, c */
4396 else
4397 warning_at (loc, OPT_Wunused_value,
4398 "left-hand operand of comma expression has no effect");
4402 /* With -Wunused, we should also warn if the left-hand operand does have
4403 side-effects, but computes a value which is not used. For example, in
4404 `foo() + bar(), baz()' the result of the `+' operator is not used,
4405 so we should issue a warning. */
4406 else if (warn_unused_value)
4407 warn_if_unused_value (expr1, loc);
4409 if (expr2 == error_mark_node)
4410 return error_mark_node;
4412 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4414 if (flag_isoc99
4415 && expr1_int_operands
4416 && expr2_int_operands)
4417 ret = note_integer_operands (ret);
4419 if (eptype)
4420 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4422 protected_set_expr_location (ret, loc);
4423 return ret;
4426 /* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4427 which we are casting. OTYPE is the type of the expression being
4428 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4429 of the cast. -Wcast-qual appeared on the command line. Named
4430 address space qualifiers are not handled here, because they result
4431 in different warnings. */
4433 static void
4434 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4436 tree in_type = type;
4437 tree in_otype = otype;
4438 int added = 0;
4439 int discarded = 0;
4440 bool is_const;
4442 /* Check that the qualifiers on IN_TYPE are a superset of the
4443 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4444 nodes is uninteresting and we stop as soon as we hit a
4445 non-POINTER_TYPE node on either type. */
4448 in_otype = TREE_TYPE (in_otype);
4449 in_type = TREE_TYPE (in_type);
4451 /* GNU C allows cv-qualified function types. 'const' means the
4452 function is very pure, 'volatile' means it can't return. We
4453 need to warn when such qualifiers are added, not when they're
4454 taken away. */
4455 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4456 && TREE_CODE (in_type) == FUNCTION_TYPE)
4457 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4458 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4459 else
4460 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4461 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4463 while (TREE_CODE (in_type) == POINTER_TYPE
4464 && TREE_CODE (in_otype) == POINTER_TYPE);
4466 if (added)
4467 warning_at (loc, OPT_Wcast_qual,
4468 "cast adds %q#v qualifier to function type", added);
4470 if (discarded)
4471 /* There are qualifiers present in IN_OTYPE that are not present
4472 in IN_TYPE. */
4473 warning_at (loc, OPT_Wcast_qual,
4474 "cast discards %q#v qualifier from pointer target type",
4475 discarded);
4477 if (added || discarded)
4478 return;
4480 /* A cast from **T to const **T is unsafe, because it can cause a
4481 const value to be changed with no additional warning. We only
4482 issue this warning if T is the same on both sides, and we only
4483 issue the warning if there are the same number of pointers on
4484 both sides, as otherwise the cast is clearly unsafe anyhow. A
4485 cast is unsafe when a qualifier is added at one level and const
4486 is not present at all outer levels.
4488 To issue this warning, we check at each level whether the cast
4489 adds new qualifiers not already seen. We don't need to special
4490 case function types, as they won't have the same
4491 TYPE_MAIN_VARIANT. */
4493 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4494 return;
4495 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4496 return;
4498 in_type = type;
4499 in_otype = otype;
4500 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4503 in_type = TREE_TYPE (in_type);
4504 in_otype = TREE_TYPE (in_otype);
4505 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4506 && !is_const)
4508 warning_at (loc, OPT_Wcast_qual,
4509 "to be safe all intermediate pointers in cast from "
4510 "%qT to %qT must be %<const%> qualified",
4511 otype, type);
4512 break;
4514 if (is_const)
4515 is_const = TYPE_READONLY (in_type);
4517 while (TREE_CODE (in_type) == POINTER_TYPE);
4520 /* Build an expression representing a cast to type TYPE of expression EXPR.
4521 LOC is the location of the cast-- typically the open paren of the cast. */
4523 tree
4524 build_c_cast (location_t loc, tree type, tree expr)
4526 tree value;
4528 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4529 expr = TREE_OPERAND (expr, 0);
4531 value = expr;
4533 if (type == error_mark_node || expr == error_mark_node)
4534 return error_mark_node;
4536 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4537 only in <protocol> qualifications. But when constructing cast expressions,
4538 the protocols do matter and must be kept around. */
4539 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4540 return build1 (NOP_EXPR, type, expr);
4542 type = TYPE_MAIN_VARIANT (type);
4544 if (TREE_CODE (type) == ARRAY_TYPE)
4546 error_at (loc, "cast specifies array type");
4547 return error_mark_node;
4550 if (TREE_CODE (type) == FUNCTION_TYPE)
4552 error_at (loc, "cast specifies function type");
4553 return error_mark_node;
4556 if (!VOID_TYPE_P (type))
4558 value = require_complete_type (value);
4559 if (value == error_mark_node)
4560 return error_mark_node;
4563 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4565 if (TREE_CODE (type) == RECORD_TYPE
4566 || TREE_CODE (type) == UNION_TYPE)
4567 pedwarn (loc, OPT_pedantic,
4568 "ISO C forbids casting nonscalar to the same type");
4570 else if (TREE_CODE (type) == UNION_TYPE)
4572 tree field;
4574 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4575 if (TREE_TYPE (field) != error_mark_node
4576 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4577 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4578 break;
4580 if (field)
4582 tree t;
4583 bool maybe_const = true;
4585 pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4586 t = c_fully_fold (value, false, &maybe_const);
4587 t = build_constructor_single (type, field, t);
4588 if (!maybe_const)
4589 t = c_wrap_maybe_const (t, true);
4590 t = digest_init (loc, type, t,
4591 NULL_TREE, false, true, 0);
4592 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4593 return t;
4595 error_at (loc, "cast to union type from type not present in union");
4596 return error_mark_node;
4598 else
4600 tree otype, ovalue;
4602 if (type == void_type_node)
4604 tree t = build1 (CONVERT_EXPR, type, value);
4605 SET_EXPR_LOCATION (t, loc);
4606 return t;
4609 otype = TREE_TYPE (value);
4611 /* Optionally warn about potentially worrisome casts. */
4612 if (warn_cast_qual
4613 && TREE_CODE (type) == POINTER_TYPE
4614 && TREE_CODE (otype) == POINTER_TYPE)
4615 handle_warn_cast_qual (loc, type, otype);
4617 /* Warn about conversions between pointers to disjoint
4618 address spaces. */
4619 if (TREE_CODE (type) == POINTER_TYPE
4620 && TREE_CODE (otype) == POINTER_TYPE
4621 && !null_pointer_constant_p (value))
4623 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4624 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4625 addr_space_t as_common;
4627 if (!addr_space_superset (as_to, as_from, &as_common))
4629 if (ADDR_SPACE_GENERIC_P (as_from))
4630 warning_at (loc, 0, "cast to %s address space pointer "
4631 "from disjoint generic address space pointer",
4632 c_addr_space_name (as_to));
4634 else if (ADDR_SPACE_GENERIC_P (as_to))
4635 warning_at (loc, 0, "cast to generic address space pointer "
4636 "from disjoint %s address space pointer",
4637 c_addr_space_name (as_from));
4639 else
4640 warning_at (loc, 0, "cast to %s address space pointer "
4641 "from disjoint %s address space pointer",
4642 c_addr_space_name (as_to),
4643 c_addr_space_name (as_from));
4647 /* Warn about possible alignment problems. */
4648 if (STRICT_ALIGNMENT
4649 && TREE_CODE (type) == POINTER_TYPE
4650 && TREE_CODE (otype) == POINTER_TYPE
4651 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4652 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4653 /* Don't warn about opaque types, where the actual alignment
4654 restriction is unknown. */
4655 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4656 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4657 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4658 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4659 warning_at (loc, OPT_Wcast_align,
4660 "cast increases required alignment of target type");
4662 if (TREE_CODE (type) == INTEGER_TYPE
4663 && TREE_CODE (otype) == POINTER_TYPE
4664 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4665 /* Unlike conversion of integers to pointers, where the
4666 warning is disabled for converting constants because
4667 of cases such as SIG_*, warn about converting constant
4668 pointers to integers. In some cases it may cause unwanted
4669 sign extension, and a warning is appropriate. */
4670 warning_at (loc, OPT_Wpointer_to_int_cast,
4671 "cast from pointer to integer of different size");
4673 if (TREE_CODE (value) == CALL_EXPR
4674 && TREE_CODE (type) != TREE_CODE (otype))
4675 warning_at (loc, OPT_Wbad_function_cast,
4676 "cast from function call of type %qT "
4677 "to non-matching type %qT", otype, type);
4679 if (TREE_CODE (type) == POINTER_TYPE
4680 && TREE_CODE (otype) == INTEGER_TYPE
4681 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4682 /* Don't warn about converting any constant. */
4683 && !TREE_CONSTANT (value))
4684 warning_at (loc,
4685 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4686 "of different size");
4688 if (warn_strict_aliasing <= 2)
4689 strict_aliasing_warning (otype, type, expr);
4691 /* If pedantic, warn for conversions between function and object
4692 pointer types, except for converting a null pointer constant
4693 to function pointer type. */
4694 if (pedantic
4695 && TREE_CODE (type) == POINTER_TYPE
4696 && TREE_CODE (otype) == POINTER_TYPE
4697 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4698 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4699 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4700 "conversion of function pointer to object pointer type");
4702 if (pedantic
4703 && TREE_CODE (type) == POINTER_TYPE
4704 && TREE_CODE (otype) == POINTER_TYPE
4705 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4706 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4707 && !null_pointer_constant_p (value))
4708 pedwarn (loc, OPT_pedantic, "ISO C forbids "
4709 "conversion of object pointer to function pointer type");
4711 ovalue = value;
4712 value = convert (type, value);
4714 /* Ignore any integer overflow caused by the cast. */
4715 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4717 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4719 if (!TREE_OVERFLOW (value))
4721 /* Avoid clobbering a shared constant. */
4722 value = copy_node (value);
4723 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4726 else if (TREE_OVERFLOW (value))
4727 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4728 value = build_int_cst_wide (TREE_TYPE (value),
4729 TREE_INT_CST_LOW (value),
4730 TREE_INT_CST_HIGH (value));
4734 /* Don't let a cast be an lvalue. */
4735 if (value == expr)
4736 value = non_lvalue_loc (loc, value);
4738 /* Don't allow the results of casting to floating-point or complex
4739 types be confused with actual constants, or casts involving
4740 integer and pointer types other than direct integer-to-integer
4741 and integer-to-pointer be confused with integer constant
4742 expressions and null pointer constants. */
4743 if (TREE_CODE (value) == REAL_CST
4744 || TREE_CODE (value) == COMPLEX_CST
4745 || (TREE_CODE (value) == INTEGER_CST
4746 && !((TREE_CODE (expr) == INTEGER_CST
4747 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4748 || TREE_CODE (expr) == REAL_CST
4749 || TREE_CODE (expr) == COMPLEX_CST)))
4750 value = build1 (NOP_EXPR, type, value);
4752 if (CAN_HAVE_LOCATION_P (value))
4753 SET_EXPR_LOCATION (value, loc);
4754 return value;
4757 /* Interpret a cast of expression EXPR to type TYPE. LOC is the
4758 location of the open paren of the cast, or the position of the cast
4759 expr. */
4760 tree
4761 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4763 tree type;
4764 tree type_expr = NULL_TREE;
4765 bool type_expr_const = true;
4766 tree ret;
4767 int saved_wsp = warn_strict_prototypes;
4769 /* This avoids warnings about unprototyped casts on
4770 integers. E.g. "#define SIG_DFL (void(*)())0". */
4771 if (TREE_CODE (expr) == INTEGER_CST)
4772 warn_strict_prototypes = 0;
4773 type = groktypename (type_name, &type_expr, &type_expr_const);
4774 warn_strict_prototypes = saved_wsp;
4776 ret = build_c_cast (loc, type, expr);
4777 if (type_expr)
4779 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4780 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4781 SET_EXPR_LOCATION (ret, loc);
4784 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4785 SET_EXPR_LOCATION (ret, loc);
4787 /* C++ does not permits types to be defined in a cast. */
4788 if (warn_cxx_compat && type_name->specs->tag_defined_p)
4789 warning_at (loc, OPT_Wc___compat,
4790 "defining a type in a cast is invalid in C++");
4792 return ret;
4795 /* Build an assignment expression of lvalue LHS from value RHS.
4796 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4797 may differ from TREE_TYPE (LHS) for an enum bitfield.
4798 MODIFYCODE is the code for a binary operator that we use
4799 to combine the old value of LHS with RHS to get the new value.
4800 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4801 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4802 which may differ from TREE_TYPE (RHS) for an enum value.
4804 LOCATION is the location of the MODIFYCODE operator.
4805 RHS_LOC is the location of the RHS. */
4807 tree
4808 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4809 enum tree_code modifycode,
4810 location_t rhs_loc, tree rhs, tree rhs_origtype)
4812 tree result;
4813 tree newrhs;
4814 tree rhs_semantic_type = NULL_TREE;
4815 tree lhstype = TREE_TYPE (lhs);
4816 tree olhstype = lhstype;
4817 bool npc;
4819 /* Types that aren't fully specified cannot be used in assignments. */
4820 lhs = require_complete_type (lhs);
4822 /* Avoid duplicate error messages from operands that had errors. */
4823 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4824 return error_mark_node;
4826 if (!lvalue_or_else (lhs, lv_assign))
4827 return error_mark_node;
4829 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4831 rhs_semantic_type = TREE_TYPE (rhs);
4832 rhs = TREE_OPERAND (rhs, 0);
4835 newrhs = rhs;
4837 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4839 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4840 lhs_origtype, modifycode, rhs_loc, rhs,
4841 rhs_origtype);
4842 if (inner == error_mark_node)
4843 return error_mark_node;
4844 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4845 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4846 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4847 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4848 protected_set_expr_location (result, location);
4849 return result;
4852 /* If a binary op has been requested, combine the old LHS value with the RHS
4853 producing the value we should actually store into the LHS. */
4855 if (modifycode != NOP_EXPR)
4857 lhs = c_fully_fold (lhs, false, NULL);
4858 lhs = stabilize_reference (lhs);
4859 newrhs = build_binary_op (location,
4860 modifycode, lhs, rhs, 1);
4862 /* The original type of the right hand side is no longer
4863 meaningful. */
4864 rhs_origtype = NULL_TREE;
4867 /* Give an error for storing in something that is 'const'. */
4869 if (TYPE_READONLY (lhstype)
4870 || ((TREE_CODE (lhstype) == RECORD_TYPE
4871 || TREE_CODE (lhstype) == UNION_TYPE)
4872 && C_TYPE_FIELDS_READONLY (lhstype)))
4874 readonly_error (lhs, lv_assign);
4875 return error_mark_node;
4877 else if (TREE_READONLY (lhs))
4878 readonly_warning (lhs, lv_assign);
4880 /* If storing into a structure or union member,
4881 it has probably been given type `int'.
4882 Compute the type that would go with
4883 the actual amount of storage the member occupies. */
4885 if (TREE_CODE (lhs) == COMPONENT_REF
4886 && (TREE_CODE (lhstype) == INTEGER_TYPE
4887 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4888 || TREE_CODE (lhstype) == REAL_TYPE
4889 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4890 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4892 /* If storing in a field that is in actuality a short or narrower than one,
4893 we must store in the field in its actual type. */
4895 if (lhstype != TREE_TYPE (lhs))
4897 lhs = copy_node (lhs);
4898 TREE_TYPE (lhs) = lhstype;
4901 /* Issue -Wc++-compat warnings about an assignment to an enum type
4902 when LHS does not have its original type. This happens for,
4903 e.g., an enum bitfield in a struct. */
4904 if (warn_cxx_compat
4905 && lhs_origtype != NULL_TREE
4906 && lhs_origtype != lhstype
4907 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4909 tree checktype = (rhs_origtype != NULL_TREE
4910 ? rhs_origtype
4911 : TREE_TYPE (rhs));
4912 if (checktype != error_mark_node
4913 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4914 warning_at (location, OPT_Wc___compat,
4915 "enum conversion in assignment is invalid in C++");
4918 /* Convert new value to destination type. Fold it first, then
4919 restore any excess precision information, for the sake of
4920 conversion warnings. */
4922 npc = null_pointer_constant_p (newrhs);
4923 newrhs = c_fully_fold (newrhs, false, NULL);
4924 if (rhs_semantic_type)
4925 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4926 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4927 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4928 if (TREE_CODE (newrhs) == ERROR_MARK)
4929 return error_mark_node;
4931 /* Emit ObjC write barrier, if necessary. */
4932 if (c_dialect_objc () && flag_objc_gc)
4934 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4935 if (result)
4937 protected_set_expr_location (result, location);
4938 return result;
4942 /* Scan operands. */
4944 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4945 TREE_SIDE_EFFECTS (result) = 1;
4946 protected_set_expr_location (result, location);
4948 /* If we got the LHS in a different type for storing in,
4949 convert the result back to the nominal type of LHS
4950 so that the value we return always has the same type
4951 as the LHS argument. */
4953 if (olhstype == TREE_TYPE (result))
4954 return result;
4956 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
4957 ic_assign, false, NULL_TREE, NULL_TREE, 0);
4958 protected_set_expr_location (result, location);
4959 return result;
4962 /* Convert value RHS to type TYPE as preparation for an assignment to
4963 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
4964 original type of RHS; this differs from TREE_TYPE (RHS) for enum
4965 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
4966 constant before any folding.
4967 The real work of conversion is done by `convert'.
4968 The purpose of this function is to generate error messages
4969 for assignments that are not allowed in C.
4970 ERRTYPE says whether it is argument passing, assignment,
4971 initialization or return.
4973 LOCATION is the location of the RHS.
4974 FUNCTION is a tree for the function being called.
4975 PARMNUM is the number of the argument, for printing in error messages. */
4977 static tree
4978 convert_for_assignment (location_t location, tree type, tree rhs,
4979 tree origtype, enum impl_conv errtype,
4980 bool null_pointer_constant, tree fundecl,
4981 tree function, int parmnum)
4983 enum tree_code codel = TREE_CODE (type);
4984 tree orig_rhs = rhs;
4985 tree rhstype;
4986 enum tree_code coder;
4987 tree rname = NULL_TREE;
4988 bool objc_ok = false;
4990 if (errtype == ic_argpass)
4992 tree selector;
4993 /* Change pointer to function to the function itself for
4994 diagnostics. */
4995 if (TREE_CODE (function) == ADDR_EXPR
4996 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4997 function = TREE_OPERAND (function, 0);
4999 /* Handle an ObjC selector specially for diagnostics. */
5000 selector = objc_message_selector ();
5001 rname = function;
5002 if (selector && parmnum > 2)
5004 rname = selector;
5005 parmnum -= 2;
5009 /* This macro is used to emit diagnostics to ensure that all format
5010 strings are complete sentences, visible to gettext and checked at
5011 compile time. */
5012 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
5013 do { \
5014 switch (errtype) \
5016 case ic_argpass: \
5017 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
5018 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5019 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5020 "expected %qT but argument is of type %qT", \
5021 type, rhstype); \
5022 break; \
5023 case ic_assign: \
5024 pedwarn (LOCATION, OPT, AS); \
5025 break; \
5026 case ic_init: \
5027 pedwarn_init (LOCATION, OPT, IN); \
5028 break; \
5029 case ic_return: \
5030 pedwarn (LOCATION, OPT, RE); \
5031 break; \
5032 default: \
5033 gcc_unreachable (); \
5035 } while (0)
5037 /* This macro is used to emit diagnostics to ensure that all format
5038 strings are complete sentences, visible to gettext and checked at
5039 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5040 extra parameter to enumerate qualifiers. */
5042 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5043 do { \
5044 switch (errtype) \
5046 case ic_argpass: \
5047 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5048 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5049 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5050 "expected %qT but argument is of type %qT", \
5051 type, rhstype); \
5052 break; \
5053 case ic_assign: \
5054 pedwarn (LOCATION, OPT, AS, QUALS); \
5055 break; \
5056 case ic_init: \
5057 pedwarn (LOCATION, OPT, IN, QUALS); \
5058 break; \
5059 case ic_return: \
5060 pedwarn (LOCATION, OPT, RE, QUALS); \
5061 break; \
5062 default: \
5063 gcc_unreachable (); \
5065 } while (0)
5067 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5068 rhs = TREE_OPERAND (rhs, 0);
5070 rhstype = TREE_TYPE (rhs);
5071 coder = TREE_CODE (rhstype);
5073 if (coder == ERROR_MARK)
5074 return error_mark_node;
5076 if (c_dialect_objc ())
5078 int parmno;
5080 switch (errtype)
5082 case ic_return:
5083 parmno = 0;
5084 break;
5086 case ic_assign:
5087 parmno = -1;
5088 break;
5090 case ic_init:
5091 parmno = -2;
5092 break;
5094 default:
5095 parmno = parmnum;
5096 break;
5099 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5102 if (warn_cxx_compat)
5104 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5105 if (checktype != error_mark_node
5106 && TREE_CODE (type) == ENUMERAL_TYPE
5107 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5109 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5110 G_("enum conversion when passing argument "
5111 "%d of %qE is invalid in C++"),
5112 G_("enum conversion in assignment is "
5113 "invalid in C++"),
5114 G_("enum conversion in initialization is "
5115 "invalid in C++"),
5116 G_("enum conversion in return is "
5117 "invalid in C++"));
5121 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5122 return rhs;
5124 if (coder == VOID_TYPE)
5126 /* Except for passing an argument to an unprototyped function,
5127 this is a constraint violation. When passing an argument to
5128 an unprototyped function, it is compile-time undefined;
5129 making it a constraint in that case was rejected in
5130 DR#252. */
5131 error_at (location, "void value not ignored as it ought to be");
5132 return error_mark_node;
5134 rhs = require_complete_type (rhs);
5135 if (rhs == error_mark_node)
5136 return error_mark_node;
5137 /* A type converts to a reference to it.
5138 This code doesn't fully support references, it's just for the
5139 special case of va_start and va_copy. */
5140 if (codel == REFERENCE_TYPE
5141 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5143 if (!lvalue_p (rhs))
5145 error_at (location, "cannot pass rvalue to reference parameter");
5146 return error_mark_node;
5148 if (!c_mark_addressable (rhs))
5149 return error_mark_node;
5150 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5151 SET_EXPR_LOCATION (rhs, location);
5153 /* We already know that these two types are compatible, but they
5154 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5155 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5156 likely to be va_list, a typedef to __builtin_va_list, which
5157 is different enough that it will cause problems later. */
5158 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5160 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5161 SET_EXPR_LOCATION (rhs, location);
5164 rhs = build1 (NOP_EXPR, type, rhs);
5165 SET_EXPR_LOCATION (rhs, location);
5166 return rhs;
5168 /* Some types can interconvert without explicit casts. */
5169 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5170 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5171 return convert (type, rhs);
5172 /* Arithmetic types all interconvert, and enum is treated like int. */
5173 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5174 || codel == FIXED_POINT_TYPE
5175 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5176 || codel == BOOLEAN_TYPE)
5177 && (coder == INTEGER_TYPE || coder == REAL_TYPE
5178 || coder == FIXED_POINT_TYPE
5179 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5180 || coder == BOOLEAN_TYPE))
5182 tree ret;
5183 bool save = in_late_binary_op;
5184 if (codel == BOOLEAN_TYPE)
5185 in_late_binary_op = true;
5186 ret = convert_and_check (type, orig_rhs);
5187 if (codel == BOOLEAN_TYPE)
5188 in_late_binary_op = save;
5189 return ret;
5192 /* Aggregates in different TUs might need conversion. */
5193 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5194 && codel == coder
5195 && comptypes (type, rhstype))
5196 return convert_and_check (type, rhs);
5198 /* Conversion to a transparent union or record from its member types.
5199 This applies only to function arguments. */
5200 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5201 && TYPE_TRANSPARENT_AGGR (type))
5202 && errtype == ic_argpass)
5204 tree memb, marginal_memb = NULL_TREE;
5206 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5208 tree memb_type = TREE_TYPE (memb);
5210 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5211 TYPE_MAIN_VARIANT (rhstype)))
5212 break;
5214 if (TREE_CODE (memb_type) != POINTER_TYPE)
5215 continue;
5217 if (coder == POINTER_TYPE)
5219 tree ttl = TREE_TYPE (memb_type);
5220 tree ttr = TREE_TYPE (rhstype);
5222 /* Any non-function converts to a [const][volatile] void *
5223 and vice versa; otherwise, targets must be the same.
5224 Meanwhile, the lhs target must have all the qualifiers of
5225 the rhs. */
5226 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5227 || comp_target_types (location, memb_type, rhstype))
5229 /* If this type won't generate any warnings, use it. */
5230 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5231 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5232 && TREE_CODE (ttl) == FUNCTION_TYPE)
5233 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5234 == TYPE_QUALS (ttr))
5235 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5236 == TYPE_QUALS (ttl))))
5237 break;
5239 /* Keep looking for a better type, but remember this one. */
5240 if (!marginal_memb)
5241 marginal_memb = memb;
5245 /* Can convert integer zero to any pointer type. */
5246 if (null_pointer_constant)
5248 rhs = null_pointer_node;
5249 break;
5253 if (memb || marginal_memb)
5255 if (!memb)
5257 /* We have only a marginally acceptable member type;
5258 it needs a warning. */
5259 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5260 tree ttr = TREE_TYPE (rhstype);
5262 /* Const and volatile mean something different for function
5263 types, so the usual warnings are not appropriate. */
5264 if (TREE_CODE (ttr) == FUNCTION_TYPE
5265 && TREE_CODE (ttl) == FUNCTION_TYPE)
5267 /* Because const and volatile on functions are
5268 restrictions that say the function will not do
5269 certain things, it is okay to use a const or volatile
5270 function where an ordinary one is wanted, but not
5271 vice-versa. */
5272 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5273 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5274 WARN_FOR_QUALIFIERS (location, 0,
5275 G_("passing argument %d of %qE "
5276 "makes %q#v qualified function "
5277 "pointer from unqualified"),
5278 G_("assignment makes %q#v qualified "
5279 "function pointer from "
5280 "unqualified"),
5281 G_("initialization makes %q#v qualified "
5282 "function pointer from "
5283 "unqualified"),
5284 G_("return makes %q#v qualified function "
5285 "pointer from unqualified"),
5286 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5288 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5289 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5290 WARN_FOR_QUALIFIERS (location, 0,
5291 G_("passing argument %d of %qE discards "
5292 "%qv qualifier from pointer target type"),
5293 G_("assignment discards %qv qualifier "
5294 "from pointer target type"),
5295 G_("initialization discards %qv qualifier "
5296 "from pointer target type"),
5297 G_("return discards %qv qualifier from "
5298 "pointer target type"),
5299 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5301 memb = marginal_memb;
5304 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5305 pedwarn (location, OPT_pedantic,
5306 "ISO C prohibits argument conversion to union type");
5308 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5309 return build_constructor_single (type, memb, rhs);
5313 /* Conversions among pointers */
5314 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5315 && (coder == codel))
5317 tree ttl = TREE_TYPE (type);
5318 tree ttr = TREE_TYPE (rhstype);
5319 tree mvl = ttl;
5320 tree mvr = ttr;
5321 bool is_opaque_pointer;
5322 int target_cmp = 0; /* Cache comp_target_types () result. */
5323 addr_space_t asl;
5324 addr_space_t asr;
5326 if (TREE_CODE (mvl) != ARRAY_TYPE)
5327 mvl = TYPE_MAIN_VARIANT (mvl);
5328 if (TREE_CODE (mvr) != ARRAY_TYPE)
5329 mvr = TYPE_MAIN_VARIANT (mvr);
5330 /* Opaque pointers are treated like void pointers. */
5331 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5333 /* C++ does not allow the implicit conversion void* -> T*. However,
5334 for the purpose of reducing the number of false positives, we
5335 tolerate the special case of
5337 int *p = NULL;
5339 where NULL is typically defined in C to be '(void *) 0'. */
5340 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5341 warning_at (location, OPT_Wc___compat,
5342 "request for implicit conversion "
5343 "from %qT to %qT not permitted in C++", rhstype, type);
5345 /* See if the pointers point to incompatible address spaces. */
5346 asl = TYPE_ADDR_SPACE (ttl);
5347 asr = TYPE_ADDR_SPACE (ttr);
5348 if (!null_pointer_constant_p (rhs)
5349 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5351 switch (errtype)
5353 case ic_argpass:
5354 error_at (location, "passing argument %d of %qE from pointer to "
5355 "non-enclosed address space", parmnum, rname);
5356 break;
5357 case ic_assign:
5358 error_at (location, "assignment from pointer to "
5359 "non-enclosed address space");
5360 break;
5361 case ic_init:
5362 error_at (location, "initialization from pointer to "
5363 "non-enclosed address space");
5364 break;
5365 case ic_return:
5366 error_at (location, "return from pointer to "
5367 "non-enclosed address space");
5368 break;
5369 default:
5370 gcc_unreachable ();
5372 return error_mark_node;
5375 /* Check if the right-hand side has a format attribute but the
5376 left-hand side doesn't. */
5377 if (warn_missing_format_attribute
5378 && check_missing_format_attribute (type, rhstype))
5380 switch (errtype)
5382 case ic_argpass:
5383 warning_at (location, OPT_Wmissing_format_attribute,
5384 "argument %d of %qE might be "
5385 "a candidate for a format attribute",
5386 parmnum, rname);
5387 break;
5388 case ic_assign:
5389 warning_at (location, OPT_Wmissing_format_attribute,
5390 "assignment left-hand side might be "
5391 "a candidate for a format attribute");
5392 break;
5393 case ic_init:
5394 warning_at (location, OPT_Wmissing_format_attribute,
5395 "initialization left-hand side might be "
5396 "a candidate for a format attribute");
5397 break;
5398 case ic_return:
5399 warning_at (location, OPT_Wmissing_format_attribute,
5400 "return type might be "
5401 "a candidate for a format attribute");
5402 break;
5403 default:
5404 gcc_unreachable ();
5408 /* Any non-function converts to a [const][volatile] void *
5409 and vice versa; otherwise, targets must be the same.
5410 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5411 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5412 || (target_cmp = comp_target_types (location, type, rhstype))
5413 || is_opaque_pointer
5414 || (c_common_unsigned_type (mvl)
5415 == c_common_unsigned_type (mvr)))
5417 if (pedantic
5418 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5420 (VOID_TYPE_P (ttr)
5421 && !null_pointer_constant
5422 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5423 WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5424 G_("ISO C forbids passing argument %d of "
5425 "%qE between function pointer "
5426 "and %<void *%>"),
5427 G_("ISO C forbids assignment between "
5428 "function pointer and %<void *%>"),
5429 G_("ISO C forbids initialization between "
5430 "function pointer and %<void *%>"),
5431 G_("ISO C forbids return between function "
5432 "pointer and %<void *%>"));
5433 /* Const and volatile mean something different for function types,
5434 so the usual warnings are not appropriate. */
5435 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5436 && TREE_CODE (ttl) != FUNCTION_TYPE)
5438 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5439 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5441 /* Types differing only by the presence of the 'volatile'
5442 qualifier are acceptable if the 'volatile' has been added
5443 in by the Objective-C EH machinery. */
5444 if (!objc_type_quals_match (ttl, ttr))
5445 WARN_FOR_QUALIFIERS (location, 0,
5446 G_("passing argument %d of %qE discards "
5447 "%qv qualifier from pointer target type"),
5448 G_("assignment discards %qv qualifier "
5449 "from pointer target type"),
5450 G_("initialization discards %qv qualifier "
5451 "from pointer target type"),
5452 G_("return discards %qv qualifier from "
5453 "pointer target type"),
5454 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5456 /* If this is not a case of ignoring a mismatch in signedness,
5457 no warning. */
5458 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5459 || target_cmp)
5461 /* If there is a mismatch, do warn. */
5462 else if (warn_pointer_sign)
5463 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5464 G_("pointer targets in passing argument "
5465 "%d of %qE differ in signedness"),
5466 G_("pointer targets in assignment "
5467 "differ in signedness"),
5468 G_("pointer targets in initialization "
5469 "differ in signedness"),
5470 G_("pointer targets in return differ "
5471 "in signedness"));
5473 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5474 && TREE_CODE (ttr) == FUNCTION_TYPE)
5476 /* Because const and volatile on functions are restrictions
5477 that say the function will not do certain things,
5478 it is okay to use a const or volatile function
5479 where an ordinary one is wanted, but not vice-versa. */
5480 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5481 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5482 WARN_FOR_QUALIFIERS (location, 0,
5483 G_("passing argument %d of %qE makes "
5484 "%q#v qualified function pointer "
5485 "from unqualified"),
5486 G_("assignment makes %q#v qualified function "
5487 "pointer from unqualified"),
5488 G_("initialization makes %q#v qualified "
5489 "function pointer from unqualified"),
5490 G_("return makes %q#v qualified function "
5491 "pointer from unqualified"),
5492 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5495 else
5496 /* Avoid warning about the volatile ObjC EH puts on decls. */
5497 if (!objc_ok)
5498 WARN_FOR_ASSIGNMENT (location, 0,
5499 G_("passing argument %d of %qE from "
5500 "incompatible pointer type"),
5501 G_("assignment from incompatible pointer type"),
5502 G_("initialization from incompatible "
5503 "pointer type"),
5504 G_("return from incompatible pointer type"));
5506 return convert (type, rhs);
5508 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5510 /* ??? This should not be an error when inlining calls to
5511 unprototyped functions. */
5512 error_at (location, "invalid use of non-lvalue array");
5513 return error_mark_node;
5515 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5517 /* An explicit constant 0 can convert to a pointer,
5518 or one that results from arithmetic, even including
5519 a cast to integer type. */
5520 if (!null_pointer_constant)
5521 WARN_FOR_ASSIGNMENT (location, 0,
5522 G_("passing argument %d of %qE makes "
5523 "pointer from integer without a cast"),
5524 G_("assignment makes pointer from integer "
5525 "without a cast"),
5526 G_("initialization makes pointer from "
5527 "integer without a cast"),
5528 G_("return makes pointer from integer "
5529 "without a cast"));
5531 return convert (type, rhs);
5533 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5535 WARN_FOR_ASSIGNMENT (location, 0,
5536 G_("passing argument %d of %qE makes integer "
5537 "from pointer without a cast"),
5538 G_("assignment makes integer from pointer "
5539 "without a cast"),
5540 G_("initialization makes integer from pointer "
5541 "without a cast"),
5542 G_("return makes integer from pointer "
5543 "without a cast"));
5544 return convert (type, rhs);
5546 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5548 tree ret;
5549 bool save = in_late_binary_op;
5550 in_late_binary_op = true;
5551 ret = convert (type, rhs);
5552 in_late_binary_op = save;
5553 return ret;
5556 switch (errtype)
5558 case ic_argpass:
5559 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5560 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5561 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5562 "expected %qT but argument is of type %qT", type, rhstype);
5563 break;
5564 case ic_assign:
5565 error_at (location, "incompatible types when assigning to type %qT from "
5566 "type %qT", type, rhstype);
5567 break;
5568 case ic_init:
5569 error_at (location,
5570 "incompatible types when initializing type %qT using type %qT",
5571 type, rhstype);
5572 break;
5573 case ic_return:
5574 error_at (location,
5575 "incompatible types when returning type %qT but %qT was "
5576 "expected", rhstype, type);
5577 break;
5578 default:
5579 gcc_unreachable ();
5582 return error_mark_node;
5585 /* If VALUE is a compound expr all of whose expressions are constant, then
5586 return its value. Otherwise, return error_mark_node.
5588 This is for handling COMPOUND_EXPRs as initializer elements
5589 which is allowed with a warning when -pedantic is specified. */
5591 static tree
5592 valid_compound_expr_initializer (tree value, tree endtype)
5594 if (TREE_CODE (value) == COMPOUND_EXPR)
5596 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5597 == error_mark_node)
5598 return error_mark_node;
5599 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5600 endtype);
5602 else if (!initializer_constant_valid_p (value, endtype))
5603 return error_mark_node;
5604 else
5605 return value;
5608 /* Perform appropriate conversions on the initial value of a variable,
5609 store it in the declaration DECL,
5610 and print any error messages that are appropriate.
5611 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5612 If the init is invalid, store an ERROR_MARK.
5614 INIT_LOC is the location of the initial value. */
5616 void
5617 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5619 tree value, type;
5620 bool npc = false;
5622 /* If variable's type was invalidly declared, just ignore it. */
5624 type = TREE_TYPE (decl);
5625 if (TREE_CODE (type) == ERROR_MARK)
5626 return;
5628 /* Digest the specified initializer into an expression. */
5630 if (init)
5631 npc = null_pointer_constant_p (init);
5632 value = digest_init (init_loc, type, init, origtype, npc,
5633 true, TREE_STATIC (decl));
5635 /* Store the expression if valid; else report error. */
5637 if (!in_system_header
5638 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5639 warning (OPT_Wtraditional, "traditional C rejects automatic "
5640 "aggregate initialization");
5642 DECL_INITIAL (decl) = value;
5644 /* ANSI wants warnings about out-of-range constant initializers. */
5645 STRIP_TYPE_NOPS (value);
5646 if (TREE_STATIC (decl))
5647 constant_expression_warning (value);
5649 /* Check if we need to set array size from compound literal size. */
5650 if (TREE_CODE (type) == ARRAY_TYPE
5651 && TYPE_DOMAIN (type) == 0
5652 && value != error_mark_node)
5654 tree inside_init = init;
5656 STRIP_TYPE_NOPS (inside_init);
5657 inside_init = fold (inside_init);
5659 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5661 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5663 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5665 /* For int foo[] = (int [3]){1}; we need to set array size
5666 now since later on array initializer will be just the
5667 brace enclosed list of the compound literal. */
5668 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5669 TREE_TYPE (decl) = type;
5670 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5671 layout_type (type);
5672 layout_decl (cldecl, 0);
5678 /* Methods for storing and printing names for error messages. */
5680 /* Implement a spelling stack that allows components of a name to be pushed
5681 and popped. Each element on the stack is this structure. */
5683 struct spelling
5685 int kind;
5686 union
5688 unsigned HOST_WIDE_INT i;
5689 const char *s;
5690 } u;
5693 #define SPELLING_STRING 1
5694 #define SPELLING_MEMBER 2
5695 #define SPELLING_BOUNDS 3
5697 static struct spelling *spelling; /* Next stack element (unused). */
5698 static struct spelling *spelling_base; /* Spelling stack base. */
5699 static int spelling_size; /* Size of the spelling stack. */
5701 /* Macros to save and restore the spelling stack around push_... functions.
5702 Alternative to SAVE_SPELLING_STACK. */
5704 #define SPELLING_DEPTH() (spelling - spelling_base)
5705 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5707 /* Push an element on the spelling stack with type KIND and assign VALUE
5708 to MEMBER. */
5710 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5712 int depth = SPELLING_DEPTH (); \
5714 if (depth >= spelling_size) \
5716 spelling_size += 10; \
5717 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5718 spelling_size); \
5719 RESTORE_SPELLING_DEPTH (depth); \
5722 spelling->kind = (KIND); \
5723 spelling->MEMBER = (VALUE); \
5724 spelling++; \
5727 /* Push STRING on the stack. Printed literally. */
5729 static void
5730 push_string (const char *string)
5732 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5735 /* Push a member name on the stack. Printed as '.' STRING. */
5737 static void
5738 push_member_name (tree decl)
5740 const char *const string
5741 = (DECL_NAME (decl)
5742 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5743 : _("<anonymous>"));
5744 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5747 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
5749 static void
5750 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5752 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5755 /* Compute the maximum size in bytes of the printed spelling. */
5757 static int
5758 spelling_length (void)
5760 int size = 0;
5761 struct spelling *p;
5763 for (p = spelling_base; p < spelling; p++)
5765 if (p->kind == SPELLING_BOUNDS)
5766 size += 25;
5767 else
5768 size += strlen (p->u.s) + 1;
5771 return size;
5774 /* Print the spelling to BUFFER and return it. */
5776 static char *
5777 print_spelling (char *buffer)
5779 char *d = buffer;
5780 struct spelling *p;
5782 for (p = spelling_base; p < spelling; p++)
5783 if (p->kind == SPELLING_BOUNDS)
5785 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5786 d += strlen (d);
5788 else
5790 const char *s;
5791 if (p->kind == SPELLING_MEMBER)
5792 *d++ = '.';
5793 for (s = p->u.s; (*d = *s++); d++)
5796 *d++ = '\0';
5797 return buffer;
5800 /* Issue an error message for a bad initializer component.
5801 GMSGID identifies the message.
5802 The component name is taken from the spelling stack. */
5804 void
5805 error_init (const char *gmsgid)
5807 char *ofwhat;
5809 /* The gmsgid may be a format string with %< and %>. */
5810 error (gmsgid);
5811 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5812 if (*ofwhat)
5813 error ("(near initialization for %qs)", ofwhat);
5816 /* Issue a pedantic warning for a bad initializer component. OPT is
5817 the option OPT_* (from options.h) controlling this warning or 0 if
5818 it is unconditionally given. GMSGID identifies the message. The
5819 component name is taken from the spelling stack. */
5821 void
5822 pedwarn_init (location_t location, int opt, const char *gmsgid)
5824 char *ofwhat;
5826 /* The gmsgid may be a format string with %< and %>. */
5827 pedwarn (location, opt, gmsgid);
5828 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5829 if (*ofwhat)
5830 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5833 /* Issue a warning for a bad initializer component.
5835 OPT is the OPT_W* value corresponding to the warning option that
5836 controls this warning. GMSGID identifies the message. The
5837 component name is taken from the spelling stack. */
5839 static void
5840 warning_init (int opt, const char *gmsgid)
5842 char *ofwhat;
5844 /* The gmsgid may be a format string with %< and %>. */
5845 warning (opt, gmsgid);
5846 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5847 if (*ofwhat)
5848 warning (opt, "(near initialization for %qs)", ofwhat);
5851 /* If TYPE is an array type and EXPR is a parenthesized string
5852 constant, warn if pedantic that EXPR is being used to initialize an
5853 object of type TYPE. */
5855 void
5856 maybe_warn_string_init (tree type, struct c_expr expr)
5858 if (pedantic
5859 && TREE_CODE (type) == ARRAY_TYPE
5860 && TREE_CODE (expr.value) == STRING_CST
5861 && expr.original_code != STRING_CST)
5862 pedwarn_init (input_location, OPT_pedantic,
5863 "array initialized from parenthesized string constant");
5866 /* Digest the parser output INIT as an initializer for type TYPE.
5867 Return a C expression of type TYPE to represent the initial value.
5869 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5871 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5873 If INIT is a string constant, STRICT_STRING is true if it is
5874 unparenthesized or we should not warn here for it being parenthesized.
5875 For other types of INIT, STRICT_STRING is not used.
5877 INIT_LOC is the location of the INIT.
5879 REQUIRE_CONSTANT requests an error if non-constant initializers or
5880 elements are seen. */
5882 static tree
5883 digest_init (location_t init_loc, tree type, tree init, tree origtype,
5884 bool null_pointer_constant, bool strict_string,
5885 int require_constant)
5887 enum tree_code code = TREE_CODE (type);
5888 tree inside_init = init;
5889 tree semantic_type = NULL_TREE;
5890 bool maybe_const = true;
5892 if (type == error_mark_node
5893 || !init
5894 || init == error_mark_node
5895 || TREE_TYPE (init) == error_mark_node)
5896 return error_mark_node;
5898 STRIP_TYPE_NOPS (inside_init);
5900 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5902 semantic_type = TREE_TYPE (inside_init);
5903 inside_init = TREE_OPERAND (inside_init, 0);
5905 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
5906 inside_init = decl_constant_value_for_optimization (inside_init);
5908 /* Initialization of an array of chars from a string constant
5909 optionally enclosed in braces. */
5911 if (code == ARRAY_TYPE && inside_init
5912 && TREE_CODE (inside_init) == STRING_CST)
5914 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5915 /* Note that an array could be both an array of character type
5916 and an array of wchar_t if wchar_t is signed char or unsigned
5917 char. */
5918 bool char_array = (typ1 == char_type_node
5919 || typ1 == signed_char_type_node
5920 || typ1 == unsigned_char_type_node);
5921 bool wchar_array = !!comptypes (typ1, wchar_type_node);
5922 bool char16_array = !!comptypes (typ1, char16_type_node);
5923 bool char32_array = !!comptypes (typ1, char32_type_node);
5925 if (char_array || wchar_array || char16_array || char32_array)
5927 struct c_expr expr;
5928 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
5929 expr.value = inside_init;
5930 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
5931 expr.original_type = NULL;
5932 maybe_warn_string_init (type, expr);
5934 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5935 pedwarn_init (init_loc, OPT_pedantic,
5936 "initialization of a flexible array member");
5938 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5939 TYPE_MAIN_VARIANT (type)))
5940 return inside_init;
5942 if (char_array)
5944 if (typ2 != char_type_node)
5946 error_init ("char-array initialized from wide string");
5947 return error_mark_node;
5950 else
5952 if (typ2 == char_type_node)
5954 error_init ("wide character array initialized from non-wide "
5955 "string");
5956 return error_mark_node;
5958 else if (!comptypes(typ1, typ2))
5960 error_init ("wide character array initialized from "
5961 "incompatible wide string");
5962 return error_mark_node;
5966 TREE_TYPE (inside_init) = type;
5967 if (TYPE_DOMAIN (type) != 0
5968 && TYPE_SIZE (type) != 0
5969 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
5971 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
5973 /* Subtract the size of a single (possibly wide) character
5974 because it's ok to ignore the terminating null char
5975 that is counted in the length of the constant. */
5976 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
5977 (len
5978 - (TYPE_PRECISION (typ1)
5979 / BITS_PER_UNIT))))
5980 pedwarn_init (init_loc, 0,
5981 ("initializer-string for array of chars "
5982 "is too long"));
5983 else if (warn_cxx_compat
5984 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
5985 warning_at (init_loc, OPT_Wc___compat,
5986 ("initializer-string for array chars "
5987 "is too long for C++"));
5990 return inside_init;
5992 else if (INTEGRAL_TYPE_P (typ1))
5994 error_init ("array of inappropriate type initialized "
5995 "from string constant");
5996 return error_mark_node;
6000 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6001 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6002 below and handle as a constructor. */
6003 if (code == VECTOR_TYPE
6004 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6005 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6006 && TREE_CONSTANT (inside_init))
6008 if (TREE_CODE (inside_init) == VECTOR_CST
6009 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6010 TYPE_MAIN_VARIANT (type)))
6011 return inside_init;
6013 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6015 unsigned HOST_WIDE_INT ix;
6016 tree value;
6017 bool constant_p = true;
6019 /* Iterate through elements and check if all constructor
6020 elements are *_CSTs. */
6021 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6022 if (!CONSTANT_CLASS_P (value))
6024 constant_p = false;
6025 break;
6028 if (constant_p)
6029 return build_vector_from_ctor (type,
6030 CONSTRUCTOR_ELTS (inside_init));
6034 if (warn_sequence_point)
6035 verify_sequence_points (inside_init);
6037 /* Any type can be initialized
6038 from an expression of the same type, optionally with braces. */
6040 if (inside_init && TREE_TYPE (inside_init) != 0
6041 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6042 TYPE_MAIN_VARIANT (type))
6043 || (code == ARRAY_TYPE
6044 && comptypes (TREE_TYPE (inside_init), type))
6045 || (code == VECTOR_TYPE
6046 && comptypes (TREE_TYPE (inside_init), type))
6047 || (code == POINTER_TYPE
6048 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6049 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6050 TREE_TYPE (type)))))
6052 if (code == POINTER_TYPE)
6054 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6056 if (TREE_CODE (inside_init) == STRING_CST
6057 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6058 inside_init = array_to_pointer_conversion
6059 (init_loc, inside_init);
6060 else
6062 error_init ("invalid use of non-lvalue array");
6063 return error_mark_node;
6068 if (code == VECTOR_TYPE)
6069 /* Although the types are compatible, we may require a
6070 conversion. */
6071 inside_init = convert (type, inside_init);
6073 if (require_constant
6074 && (code == VECTOR_TYPE || !flag_isoc99)
6075 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6077 /* As an extension, allow initializing objects with static storage
6078 duration with compound literals (which are then treated just as
6079 the brace enclosed list they contain). Also allow this for
6080 vectors, as we can only assign them with compound literals. */
6081 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6082 inside_init = DECL_INITIAL (decl);
6085 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6086 && TREE_CODE (inside_init) != CONSTRUCTOR)
6088 error_init ("array initialized from non-constant array expression");
6089 return error_mark_node;
6092 /* Compound expressions can only occur here if -pedantic or
6093 -pedantic-errors is specified. In the later case, we always want
6094 an error. In the former case, we simply want a warning. */
6095 if (require_constant && pedantic
6096 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6098 inside_init
6099 = valid_compound_expr_initializer (inside_init,
6100 TREE_TYPE (inside_init));
6101 if (inside_init == error_mark_node)
6102 error_init ("initializer element is not constant");
6103 else
6104 pedwarn_init (init_loc, OPT_pedantic,
6105 "initializer element is not constant");
6106 if (flag_pedantic_errors)
6107 inside_init = error_mark_node;
6109 else if (require_constant
6110 && !initializer_constant_valid_p (inside_init,
6111 TREE_TYPE (inside_init)))
6113 error_init ("initializer element is not constant");
6114 inside_init = error_mark_node;
6116 else if (require_constant && !maybe_const)
6117 pedwarn_init (init_loc, 0,
6118 "initializer element is not a constant expression");
6120 /* Added to enable additional -Wmissing-format-attribute warnings. */
6121 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6122 inside_init = convert_for_assignment (init_loc, type, inside_init,
6123 origtype,
6124 ic_init, null_pointer_constant,
6125 NULL_TREE, NULL_TREE, 0);
6126 return inside_init;
6129 /* Handle scalar types, including conversions. */
6131 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6132 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6133 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6135 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6136 && (TREE_CODE (init) == STRING_CST
6137 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6138 inside_init = init = array_to_pointer_conversion (init_loc, init);
6139 if (semantic_type)
6140 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6141 inside_init);
6142 inside_init
6143 = convert_for_assignment (init_loc, type, inside_init, origtype,
6144 ic_init, null_pointer_constant,
6145 NULL_TREE, NULL_TREE, 0);
6147 /* Check to see if we have already given an error message. */
6148 if (inside_init == error_mark_node)
6150 else if (require_constant && !TREE_CONSTANT (inside_init))
6152 error_init ("initializer element is not constant");
6153 inside_init = error_mark_node;
6155 else if (require_constant
6156 && !initializer_constant_valid_p (inside_init,
6157 TREE_TYPE (inside_init)))
6159 error_init ("initializer element is not computable at load time");
6160 inside_init = error_mark_node;
6162 else if (require_constant && !maybe_const)
6163 pedwarn_init (init_loc, 0,
6164 "initializer element is not a constant expression");
6166 return inside_init;
6169 /* Come here only for records and arrays. */
6171 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6173 error_init ("variable-sized object may not be initialized");
6174 return error_mark_node;
6177 error_init ("invalid initializer");
6178 return error_mark_node;
6181 /* Handle initializers that use braces. */
6183 /* Type of object we are accumulating a constructor for.
6184 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6185 static tree constructor_type;
6187 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6188 left to fill. */
6189 static tree constructor_fields;
6191 /* For an ARRAY_TYPE, this is the specified index
6192 at which to store the next element we get. */
6193 static tree constructor_index;
6195 /* For an ARRAY_TYPE, this is the maximum index. */
6196 static tree constructor_max_index;
6198 /* For a RECORD_TYPE, this is the first field not yet written out. */
6199 static tree constructor_unfilled_fields;
6201 /* For an ARRAY_TYPE, this is the index of the first element
6202 not yet written out. */
6203 static tree constructor_unfilled_index;
6205 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6206 This is so we can generate gaps between fields, when appropriate. */
6207 static tree constructor_bit_index;
6209 /* If we are saving up the elements rather than allocating them,
6210 this is the list of elements so far (in reverse order,
6211 most recent first). */
6212 static VEC(constructor_elt,gc) *constructor_elements;
6214 /* 1 if constructor should be incrementally stored into a constructor chain,
6215 0 if all the elements should be kept in AVL tree. */
6216 static int constructor_incremental;
6218 /* 1 if so far this constructor's elements are all compile-time constants. */
6219 static int constructor_constant;
6221 /* 1 if so far this constructor's elements are all valid address constants. */
6222 static int constructor_simple;
6224 /* 1 if this constructor has an element that cannot be part of a
6225 constant expression. */
6226 static int constructor_nonconst;
6228 /* 1 if this constructor is erroneous so far. */
6229 static int constructor_erroneous;
6231 /* Structure for managing pending initializer elements, organized as an
6232 AVL tree. */
6234 struct init_node
6236 struct init_node *left, *right;
6237 struct init_node *parent;
6238 int balance;
6239 tree purpose;
6240 tree value;
6241 tree origtype;
6244 /* Tree of pending elements at this constructor level.
6245 These are elements encountered out of order
6246 which belong at places we haven't reached yet in actually
6247 writing the output.
6248 Will never hold tree nodes across GC runs. */
6249 static struct init_node *constructor_pending_elts;
6251 /* The SPELLING_DEPTH of this constructor. */
6252 static int constructor_depth;
6254 /* DECL node for which an initializer is being read.
6255 0 means we are reading a constructor expression
6256 such as (struct foo) {...}. */
6257 static tree constructor_decl;
6259 /* Nonzero if this is an initializer for a top-level decl. */
6260 static int constructor_top_level;
6262 /* Nonzero if there were any member designators in this initializer. */
6263 static int constructor_designated;
6265 /* Nesting depth of designator list. */
6266 static int designator_depth;
6268 /* Nonzero if there were diagnosed errors in this designator list. */
6269 static int designator_erroneous;
6272 /* This stack has a level for each implicit or explicit level of
6273 structuring in the initializer, including the outermost one. It
6274 saves the values of most of the variables above. */
6276 struct constructor_range_stack;
6278 struct constructor_stack
6280 struct constructor_stack *next;
6281 tree type;
6282 tree fields;
6283 tree index;
6284 tree max_index;
6285 tree unfilled_index;
6286 tree unfilled_fields;
6287 tree bit_index;
6288 VEC(constructor_elt,gc) *elements;
6289 struct init_node *pending_elts;
6290 int offset;
6291 int depth;
6292 /* If value nonzero, this value should replace the entire
6293 constructor at this level. */
6294 struct c_expr replacement_value;
6295 struct constructor_range_stack *range_stack;
6296 char constant;
6297 char simple;
6298 char nonconst;
6299 char implicit;
6300 char erroneous;
6301 char outer;
6302 char incremental;
6303 char designated;
6306 static struct constructor_stack *constructor_stack;
6308 /* This stack represents designators from some range designator up to
6309 the last designator in the list. */
6311 struct constructor_range_stack
6313 struct constructor_range_stack *next, *prev;
6314 struct constructor_stack *stack;
6315 tree range_start;
6316 tree index;
6317 tree range_end;
6318 tree fields;
6321 static struct constructor_range_stack *constructor_range_stack;
6323 /* This stack records separate initializers that are nested.
6324 Nested initializers can't happen in ANSI C, but GNU C allows them
6325 in cases like { ... (struct foo) { ... } ... }. */
6327 struct initializer_stack
6329 struct initializer_stack *next;
6330 tree decl;
6331 struct constructor_stack *constructor_stack;
6332 struct constructor_range_stack *constructor_range_stack;
6333 VEC(constructor_elt,gc) *elements;
6334 struct spelling *spelling;
6335 struct spelling *spelling_base;
6336 int spelling_size;
6337 char top_level;
6338 char require_constant_value;
6339 char require_constant_elements;
6342 static struct initializer_stack *initializer_stack;
6344 /* Prepare to parse and output the initializer for variable DECL. */
6346 void
6347 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6349 const char *locus;
6350 struct initializer_stack *p = XNEW (struct initializer_stack);
6352 p->decl = constructor_decl;
6353 p->require_constant_value = require_constant_value;
6354 p->require_constant_elements = require_constant_elements;
6355 p->constructor_stack = constructor_stack;
6356 p->constructor_range_stack = constructor_range_stack;
6357 p->elements = constructor_elements;
6358 p->spelling = spelling;
6359 p->spelling_base = spelling_base;
6360 p->spelling_size = spelling_size;
6361 p->top_level = constructor_top_level;
6362 p->next = initializer_stack;
6363 initializer_stack = p;
6365 constructor_decl = decl;
6366 constructor_designated = 0;
6367 constructor_top_level = top_level;
6369 if (decl != 0 && decl != error_mark_node)
6371 require_constant_value = TREE_STATIC (decl);
6372 require_constant_elements
6373 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6374 /* For a scalar, you can always use any value to initialize,
6375 even within braces. */
6376 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6377 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6378 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6379 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6380 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6382 else
6384 require_constant_value = 0;
6385 require_constant_elements = 0;
6386 locus = _("(anonymous)");
6389 constructor_stack = 0;
6390 constructor_range_stack = 0;
6392 missing_braces_mentioned = 0;
6394 spelling_base = 0;
6395 spelling_size = 0;
6396 RESTORE_SPELLING_DEPTH (0);
6398 if (locus)
6399 push_string (locus);
6402 void
6403 finish_init (void)
6405 struct initializer_stack *p = initializer_stack;
6407 /* Free the whole constructor stack of this initializer. */
6408 while (constructor_stack)
6410 struct constructor_stack *q = constructor_stack;
6411 constructor_stack = q->next;
6412 free (q);
6415 gcc_assert (!constructor_range_stack);
6417 /* Pop back to the data of the outer initializer (if any). */
6418 free (spelling_base);
6420 constructor_decl = p->decl;
6421 require_constant_value = p->require_constant_value;
6422 require_constant_elements = p->require_constant_elements;
6423 constructor_stack = p->constructor_stack;
6424 constructor_range_stack = p->constructor_range_stack;
6425 constructor_elements = p->elements;
6426 spelling = p->spelling;
6427 spelling_base = p->spelling_base;
6428 spelling_size = p->spelling_size;
6429 constructor_top_level = p->top_level;
6430 initializer_stack = p->next;
6431 free (p);
6434 /* Call here when we see the initializer is surrounded by braces.
6435 This is instead of a call to push_init_level;
6436 it is matched by a call to pop_init_level.
6438 TYPE is the type to initialize, for a constructor expression.
6439 For an initializer for a decl, TYPE is zero. */
6441 void
6442 really_start_incremental_init (tree type)
6444 struct constructor_stack *p = XNEW (struct constructor_stack);
6446 if (type == 0)
6447 type = TREE_TYPE (constructor_decl);
6449 if (TREE_CODE (type) == VECTOR_TYPE
6450 && TYPE_VECTOR_OPAQUE (type))
6451 error ("opaque vector types cannot be initialized");
6453 p->type = constructor_type;
6454 p->fields = constructor_fields;
6455 p->index = constructor_index;
6456 p->max_index = constructor_max_index;
6457 p->unfilled_index = constructor_unfilled_index;
6458 p->unfilled_fields = constructor_unfilled_fields;
6459 p->bit_index = constructor_bit_index;
6460 p->elements = constructor_elements;
6461 p->constant = constructor_constant;
6462 p->simple = constructor_simple;
6463 p->nonconst = constructor_nonconst;
6464 p->erroneous = constructor_erroneous;
6465 p->pending_elts = constructor_pending_elts;
6466 p->depth = constructor_depth;
6467 p->replacement_value.value = 0;
6468 p->replacement_value.original_code = ERROR_MARK;
6469 p->replacement_value.original_type = NULL;
6470 p->implicit = 0;
6471 p->range_stack = 0;
6472 p->outer = 0;
6473 p->incremental = constructor_incremental;
6474 p->designated = constructor_designated;
6475 p->next = 0;
6476 constructor_stack = p;
6478 constructor_constant = 1;
6479 constructor_simple = 1;
6480 constructor_nonconst = 0;
6481 constructor_depth = SPELLING_DEPTH ();
6482 constructor_elements = 0;
6483 constructor_pending_elts = 0;
6484 constructor_type = type;
6485 constructor_incremental = 1;
6486 constructor_designated = 0;
6487 designator_depth = 0;
6488 designator_erroneous = 0;
6490 if (TREE_CODE (constructor_type) == RECORD_TYPE
6491 || TREE_CODE (constructor_type) == UNION_TYPE)
6493 constructor_fields = TYPE_FIELDS (constructor_type);
6494 /* Skip any nameless bit fields at the beginning. */
6495 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6496 && DECL_NAME (constructor_fields) == 0)
6497 constructor_fields = DECL_CHAIN (constructor_fields);
6499 constructor_unfilled_fields = constructor_fields;
6500 constructor_bit_index = bitsize_zero_node;
6502 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6504 if (TYPE_DOMAIN (constructor_type))
6506 constructor_max_index
6507 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6509 /* Detect non-empty initializations of zero-length arrays. */
6510 if (constructor_max_index == NULL_TREE
6511 && TYPE_SIZE (constructor_type))
6512 constructor_max_index = integer_minus_one_node;
6514 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6515 to initialize VLAs will cause a proper error; avoid tree
6516 checking errors as well by setting a safe value. */
6517 if (constructor_max_index
6518 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6519 constructor_max_index = integer_minus_one_node;
6521 constructor_index
6522 = convert (bitsizetype,
6523 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6525 else
6527 constructor_index = bitsize_zero_node;
6528 constructor_max_index = NULL_TREE;
6531 constructor_unfilled_index = constructor_index;
6533 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6535 /* Vectors are like simple fixed-size arrays. */
6536 constructor_max_index =
6537 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6538 constructor_index = bitsize_zero_node;
6539 constructor_unfilled_index = constructor_index;
6541 else
6543 /* Handle the case of int x = {5}; */
6544 constructor_fields = constructor_type;
6545 constructor_unfilled_fields = constructor_type;
6549 /* Push down into a subobject, for initialization.
6550 If this is for an explicit set of braces, IMPLICIT is 0.
6551 If it is because the next element belongs at a lower level,
6552 IMPLICIT is 1 (or 2 if the push is because of designator list). */
6554 void
6555 push_init_level (int implicit, struct obstack * braced_init_obstack)
6557 struct constructor_stack *p;
6558 tree value = NULL_TREE;
6560 /* If we've exhausted any levels that didn't have braces,
6561 pop them now. If implicit == 1, this will have been done in
6562 process_init_element; do not repeat it here because in the case
6563 of excess initializers for an empty aggregate this leads to an
6564 infinite cycle of popping a level and immediately recreating
6565 it. */
6566 if (implicit != 1)
6568 while (constructor_stack->implicit)
6570 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6571 || TREE_CODE (constructor_type) == UNION_TYPE)
6572 && constructor_fields == 0)
6573 process_init_element (pop_init_level (1, braced_init_obstack),
6574 true, braced_init_obstack);
6575 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6576 && constructor_max_index
6577 && tree_int_cst_lt (constructor_max_index,
6578 constructor_index))
6579 process_init_element (pop_init_level (1, braced_init_obstack),
6580 true, braced_init_obstack);
6581 else
6582 break;
6586 /* Unless this is an explicit brace, we need to preserve previous
6587 content if any. */
6588 if (implicit)
6590 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6591 || TREE_CODE (constructor_type) == UNION_TYPE)
6592 && constructor_fields)
6593 value = find_init_member (constructor_fields, braced_init_obstack);
6594 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6595 value = find_init_member (constructor_index, braced_init_obstack);
6598 p = XNEW (struct constructor_stack);
6599 p->type = constructor_type;
6600 p->fields = constructor_fields;
6601 p->index = constructor_index;
6602 p->max_index = constructor_max_index;
6603 p->unfilled_index = constructor_unfilled_index;
6604 p->unfilled_fields = constructor_unfilled_fields;
6605 p->bit_index = constructor_bit_index;
6606 p->elements = constructor_elements;
6607 p->constant = constructor_constant;
6608 p->simple = constructor_simple;
6609 p->nonconst = constructor_nonconst;
6610 p->erroneous = constructor_erroneous;
6611 p->pending_elts = constructor_pending_elts;
6612 p->depth = constructor_depth;
6613 p->replacement_value.value = 0;
6614 p->replacement_value.original_code = ERROR_MARK;
6615 p->replacement_value.original_type = NULL;
6616 p->implicit = implicit;
6617 p->outer = 0;
6618 p->incremental = constructor_incremental;
6619 p->designated = constructor_designated;
6620 p->next = constructor_stack;
6621 p->range_stack = 0;
6622 constructor_stack = p;
6624 constructor_constant = 1;
6625 constructor_simple = 1;
6626 constructor_nonconst = 0;
6627 constructor_depth = SPELLING_DEPTH ();
6628 constructor_elements = 0;
6629 constructor_incremental = 1;
6630 constructor_designated = 0;
6631 constructor_pending_elts = 0;
6632 if (!implicit)
6634 p->range_stack = constructor_range_stack;
6635 constructor_range_stack = 0;
6636 designator_depth = 0;
6637 designator_erroneous = 0;
6640 /* Don't die if an entire brace-pair level is superfluous
6641 in the containing level. */
6642 if (constructor_type == 0)
6644 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6645 || TREE_CODE (constructor_type) == UNION_TYPE)
6647 /* Don't die if there are extra init elts at the end. */
6648 if (constructor_fields == 0)
6649 constructor_type = 0;
6650 else
6652 constructor_type = TREE_TYPE (constructor_fields);
6653 push_member_name (constructor_fields);
6654 constructor_depth++;
6657 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6659 constructor_type = TREE_TYPE (constructor_type);
6660 push_array_bounds (tree_low_cst (constructor_index, 1));
6661 constructor_depth++;
6664 if (constructor_type == 0)
6666 error_init ("extra brace group at end of initializer");
6667 constructor_fields = 0;
6668 constructor_unfilled_fields = 0;
6669 return;
6672 if (value && TREE_CODE (value) == CONSTRUCTOR)
6674 constructor_constant = TREE_CONSTANT (value);
6675 constructor_simple = TREE_STATIC (value);
6676 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6677 constructor_elements = CONSTRUCTOR_ELTS (value);
6678 if (!VEC_empty (constructor_elt, constructor_elements)
6679 && (TREE_CODE (constructor_type) == RECORD_TYPE
6680 || TREE_CODE (constructor_type) == ARRAY_TYPE))
6681 set_nonincremental_init (braced_init_obstack);
6684 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6686 missing_braces_mentioned = 1;
6687 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6690 if (TREE_CODE (constructor_type) == RECORD_TYPE
6691 || TREE_CODE (constructor_type) == UNION_TYPE)
6693 constructor_fields = TYPE_FIELDS (constructor_type);
6694 /* Skip any nameless bit fields at the beginning. */
6695 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6696 && DECL_NAME (constructor_fields) == 0)
6697 constructor_fields = DECL_CHAIN (constructor_fields);
6699 constructor_unfilled_fields = constructor_fields;
6700 constructor_bit_index = bitsize_zero_node;
6702 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6704 /* Vectors are like simple fixed-size arrays. */
6705 constructor_max_index =
6706 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6707 constructor_index = convert (bitsizetype, integer_zero_node);
6708 constructor_unfilled_index = constructor_index;
6710 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6712 if (TYPE_DOMAIN (constructor_type))
6714 constructor_max_index
6715 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6717 /* Detect non-empty initializations of zero-length arrays. */
6718 if (constructor_max_index == NULL_TREE
6719 && TYPE_SIZE (constructor_type))
6720 constructor_max_index = integer_minus_one_node;
6722 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6723 to initialize VLAs will cause a proper error; avoid tree
6724 checking errors as well by setting a safe value. */
6725 if (constructor_max_index
6726 && TREE_CODE (constructor_max_index) != INTEGER_CST)
6727 constructor_max_index = integer_minus_one_node;
6729 constructor_index
6730 = convert (bitsizetype,
6731 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6733 else
6734 constructor_index = bitsize_zero_node;
6736 constructor_unfilled_index = constructor_index;
6737 if (value && TREE_CODE (value) == STRING_CST)
6739 /* We need to split the char/wchar array into individual
6740 characters, so that we don't have to special case it
6741 everywhere. */
6742 set_nonincremental_init_from_string (value, braced_init_obstack);
6745 else
6747 if (constructor_type != error_mark_node)
6748 warning_init (0, "braces around scalar initializer");
6749 constructor_fields = constructor_type;
6750 constructor_unfilled_fields = constructor_type;
6754 /* At the end of an implicit or explicit brace level,
6755 finish up that level of constructor. If a single expression
6756 with redundant braces initialized that level, return the
6757 c_expr structure for that expression. Otherwise, the original_code
6758 element is set to ERROR_MARK.
6759 If we were outputting the elements as they are read, return 0 as the value
6760 from inner levels (process_init_element ignores that),
6761 but return error_mark_node as the value from the outermost level
6762 (that's what we want to put in DECL_INITIAL).
6763 Otherwise, return a CONSTRUCTOR expression as the value. */
6765 struct c_expr
6766 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6768 struct constructor_stack *p;
6769 struct c_expr ret;
6770 ret.value = 0;
6771 ret.original_code = ERROR_MARK;
6772 ret.original_type = NULL;
6774 if (implicit == 0)
6776 /* When we come to an explicit close brace,
6777 pop any inner levels that didn't have explicit braces. */
6778 while (constructor_stack->implicit)
6780 process_init_element (pop_init_level (1, braced_init_obstack),
6781 true, braced_init_obstack);
6783 gcc_assert (!constructor_range_stack);
6786 /* Now output all pending elements. */
6787 constructor_incremental = 1;
6788 output_pending_init_elements (1, braced_init_obstack);
6790 p = constructor_stack;
6792 /* Error for initializing a flexible array member, or a zero-length
6793 array member in an inappropriate context. */
6794 if (constructor_type && constructor_fields
6795 && TREE_CODE (constructor_type) == ARRAY_TYPE
6796 && TYPE_DOMAIN (constructor_type)
6797 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6799 /* Silently discard empty initializations. The parser will
6800 already have pedwarned for empty brackets. */
6801 if (integer_zerop (constructor_unfilled_index))
6802 constructor_type = NULL_TREE;
6803 else
6805 gcc_assert (!TYPE_SIZE (constructor_type));
6807 if (constructor_depth > 2)
6808 error_init ("initialization of flexible array member in a nested context");
6809 else
6810 pedwarn_init (input_location, OPT_pedantic,
6811 "initialization of a flexible array member");
6813 /* We have already issued an error message for the existence
6814 of a flexible array member not at the end of the structure.
6815 Discard the initializer so that we do not die later. */
6816 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6817 constructor_type = NULL_TREE;
6821 /* Warn when some struct elements are implicitly initialized to zero. */
6822 if (warn_missing_field_initializers
6823 && constructor_type
6824 && TREE_CODE (constructor_type) == RECORD_TYPE
6825 && constructor_unfilled_fields)
6827 /* Do not warn for flexible array members or zero-length arrays. */
6828 while (constructor_unfilled_fields
6829 && (!DECL_SIZE (constructor_unfilled_fields)
6830 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6831 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
6833 /* Do not warn if this level of the initializer uses member
6834 designators; it is likely to be deliberate. */
6835 if (constructor_unfilled_fields && !constructor_designated)
6837 push_member_name (constructor_unfilled_fields);
6838 warning_init (OPT_Wmissing_field_initializers,
6839 "missing initializer");
6840 RESTORE_SPELLING_DEPTH (constructor_depth);
6844 /* Pad out the end of the structure. */
6845 if (p->replacement_value.value)
6846 /* If this closes a superfluous brace pair,
6847 just pass out the element between them. */
6848 ret = p->replacement_value;
6849 else if (constructor_type == 0)
6851 else if (TREE_CODE (constructor_type) != RECORD_TYPE
6852 && TREE_CODE (constructor_type) != UNION_TYPE
6853 && TREE_CODE (constructor_type) != ARRAY_TYPE
6854 && TREE_CODE (constructor_type) != VECTOR_TYPE)
6856 /* A nonincremental scalar initializer--just return
6857 the element, after verifying there is just one. */
6858 if (VEC_empty (constructor_elt,constructor_elements))
6860 if (!constructor_erroneous)
6861 error_init ("empty scalar initializer");
6862 ret.value = error_mark_node;
6864 else if (VEC_length (constructor_elt,constructor_elements) != 1)
6866 error_init ("extra elements in scalar initializer");
6867 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6869 else
6870 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6872 else
6874 if (constructor_erroneous)
6875 ret.value = error_mark_node;
6876 else
6878 ret.value = build_constructor (constructor_type,
6879 constructor_elements);
6880 if (constructor_constant)
6881 TREE_CONSTANT (ret.value) = 1;
6882 if (constructor_constant && constructor_simple)
6883 TREE_STATIC (ret.value) = 1;
6884 if (constructor_nonconst)
6885 CONSTRUCTOR_NON_CONST (ret.value) = 1;
6889 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6891 if (constructor_nonconst)
6892 ret.original_code = C_MAYBE_CONST_EXPR;
6893 else if (ret.original_code == C_MAYBE_CONST_EXPR)
6894 ret.original_code = ERROR_MARK;
6897 constructor_type = p->type;
6898 constructor_fields = p->fields;
6899 constructor_index = p->index;
6900 constructor_max_index = p->max_index;
6901 constructor_unfilled_index = p->unfilled_index;
6902 constructor_unfilled_fields = p->unfilled_fields;
6903 constructor_bit_index = p->bit_index;
6904 constructor_elements = p->elements;
6905 constructor_constant = p->constant;
6906 constructor_simple = p->simple;
6907 constructor_nonconst = p->nonconst;
6908 constructor_erroneous = p->erroneous;
6909 constructor_incremental = p->incremental;
6910 constructor_designated = p->designated;
6911 constructor_pending_elts = p->pending_elts;
6912 constructor_depth = p->depth;
6913 if (!p->implicit)
6914 constructor_range_stack = p->range_stack;
6915 RESTORE_SPELLING_DEPTH (constructor_depth);
6917 constructor_stack = p->next;
6918 free (p);
6920 if (ret.value == 0 && constructor_stack == 0)
6921 ret.value = error_mark_node;
6922 return ret;
6925 /* Common handling for both array range and field name designators.
6926 ARRAY argument is nonzero for array ranges. Returns zero for success. */
6928 static int
6929 set_designator (int array, struct obstack * braced_init_obstack)
6931 tree subtype;
6932 enum tree_code subcode;
6934 /* Don't die if an entire brace-pair level is superfluous
6935 in the containing level. */
6936 if (constructor_type == 0)
6937 return 1;
6939 /* If there were errors in this designator list already, bail out
6940 silently. */
6941 if (designator_erroneous)
6942 return 1;
6944 if (!designator_depth)
6946 gcc_assert (!constructor_range_stack);
6948 /* Designator list starts at the level of closest explicit
6949 braces. */
6950 while (constructor_stack->implicit)
6952 process_init_element (pop_init_level (1, braced_init_obstack),
6953 true, braced_init_obstack);
6955 constructor_designated = 1;
6956 return 0;
6959 switch (TREE_CODE (constructor_type))
6961 case RECORD_TYPE:
6962 case UNION_TYPE:
6963 subtype = TREE_TYPE (constructor_fields);
6964 if (subtype != error_mark_node)
6965 subtype = TYPE_MAIN_VARIANT (subtype);
6966 break;
6967 case ARRAY_TYPE:
6968 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6969 break;
6970 default:
6971 gcc_unreachable ();
6974 subcode = TREE_CODE (subtype);
6975 if (array && subcode != ARRAY_TYPE)
6977 error_init ("array index in non-array initializer");
6978 return 1;
6980 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
6982 error_init ("field name not in record or union initializer");
6983 return 1;
6986 constructor_designated = 1;
6987 push_init_level (2, braced_init_obstack);
6988 return 0;
6991 /* If there are range designators in designator list, push a new designator
6992 to constructor_range_stack. RANGE_END is end of such stack range or
6993 NULL_TREE if there is no range designator at this level. */
6995 static void
6996 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
6998 struct constructor_range_stack *p;
7000 p = (struct constructor_range_stack *)
7001 obstack_alloc (braced_init_obstack,
7002 sizeof (struct constructor_range_stack));
7003 p->prev = constructor_range_stack;
7004 p->next = 0;
7005 p->fields = constructor_fields;
7006 p->range_start = constructor_index;
7007 p->index = constructor_index;
7008 p->stack = constructor_stack;
7009 p->range_end = range_end;
7010 if (constructor_range_stack)
7011 constructor_range_stack->next = p;
7012 constructor_range_stack = p;
7015 /* Within an array initializer, specify the next index to be initialized.
7016 FIRST is that index. If LAST is nonzero, then initialize a range
7017 of indices, running from FIRST through LAST. */
7019 void
7020 set_init_index (tree first, tree last,
7021 struct obstack * braced_init_obstack)
7023 if (set_designator (1, braced_init_obstack))
7024 return;
7026 designator_erroneous = 1;
7028 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7029 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7031 error_init ("array index in initializer not of integer type");
7032 return;
7035 if (TREE_CODE (first) != INTEGER_CST)
7037 first = c_fully_fold (first, false, NULL);
7038 if (TREE_CODE (first) == INTEGER_CST)
7039 pedwarn_init (input_location, OPT_pedantic,
7040 "array index in initializer is not "
7041 "an integer constant expression");
7044 if (last && TREE_CODE (last) != INTEGER_CST)
7046 last = c_fully_fold (last, false, NULL);
7047 if (TREE_CODE (last) == INTEGER_CST)
7048 pedwarn_init (input_location, OPT_pedantic,
7049 "array index in initializer is not "
7050 "an integer constant expression");
7053 if (TREE_CODE (first) != INTEGER_CST)
7054 error_init ("nonconstant array index in initializer");
7055 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7056 error_init ("nonconstant array index in initializer");
7057 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7058 error_init ("array index in non-array initializer");
7059 else if (tree_int_cst_sgn (first) == -1)
7060 error_init ("array index in initializer exceeds array bounds");
7061 else if (constructor_max_index
7062 && tree_int_cst_lt (constructor_max_index, first))
7063 error_init ("array index in initializer exceeds array bounds");
7064 else
7066 constant_expression_warning (first);
7067 if (last)
7068 constant_expression_warning (last);
7069 constructor_index = convert (bitsizetype, first);
7071 if (last)
7073 if (tree_int_cst_equal (first, last))
7074 last = 0;
7075 else if (tree_int_cst_lt (last, first))
7077 error_init ("empty index range in initializer");
7078 last = 0;
7080 else
7082 last = convert (bitsizetype, last);
7083 if (constructor_max_index != 0
7084 && tree_int_cst_lt (constructor_max_index, last))
7086 error_init ("array index range in initializer exceeds array bounds");
7087 last = 0;
7092 designator_depth++;
7093 designator_erroneous = 0;
7094 if (constructor_range_stack || last)
7095 push_range_stack (last, braced_init_obstack);
7099 /* Within a struct initializer, specify the next field to be initialized. */
7101 void
7102 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7104 tree field;
7106 if (set_designator (0, braced_init_obstack))
7107 return;
7109 designator_erroneous = 1;
7111 if (TREE_CODE (constructor_type) != RECORD_TYPE
7112 && TREE_CODE (constructor_type) != UNION_TYPE)
7114 error_init ("field name not in record or union initializer");
7115 return;
7118 field = lookup_field (constructor_type, fieldname);
7120 if (field == 0)
7121 error ("unknown field %qE specified in initializer", fieldname);
7122 else
7125 constructor_fields = TREE_VALUE (field);
7126 designator_depth++;
7127 designator_erroneous = 0;
7128 if (constructor_range_stack)
7129 push_range_stack (NULL_TREE, braced_init_obstack);
7130 field = TREE_CHAIN (field);
7131 if (field)
7133 if (set_designator (0, braced_init_obstack))
7134 return;
7137 while (field != NULL_TREE);
7140 /* Add a new initializer to the tree of pending initializers. PURPOSE
7141 identifies the initializer, either array index or field in a structure.
7142 VALUE is the value of that index or field. If ORIGTYPE is not
7143 NULL_TREE, it is the original type of VALUE.
7145 IMPLICIT is true if value comes from pop_init_level (1),
7146 the new initializer has been merged with the existing one
7147 and thus no warnings should be emitted about overriding an
7148 existing initializer. */
7150 static void
7151 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7152 struct obstack * braced_init_obstack)
7154 struct init_node *p, **q, *r;
7156 q = &constructor_pending_elts;
7157 p = 0;
7159 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7161 while (*q != 0)
7163 p = *q;
7164 if (tree_int_cst_lt (purpose, p->purpose))
7165 q = &p->left;
7166 else if (tree_int_cst_lt (p->purpose, purpose))
7167 q = &p->right;
7168 else
7170 if (!implicit)
7172 if (TREE_SIDE_EFFECTS (p->value))
7173 warning_init (0, "initialized field with side-effects overwritten");
7174 else if (warn_override_init)
7175 warning_init (OPT_Woverride_init, "initialized field overwritten");
7177 p->value = value;
7178 p->origtype = origtype;
7179 return;
7183 else
7185 tree bitpos;
7187 bitpos = bit_position (purpose);
7188 while (*q != NULL)
7190 p = *q;
7191 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7192 q = &p->left;
7193 else if (p->purpose != purpose)
7194 q = &p->right;
7195 else
7197 if (!implicit)
7199 if (TREE_SIDE_EFFECTS (p->value))
7200 warning_init (0, "initialized field with side-effects overwritten");
7201 else if (warn_override_init)
7202 warning_init (OPT_Woverride_init, "initialized field overwritten");
7204 p->value = value;
7205 p->origtype = origtype;
7206 return;
7211 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7212 sizeof (struct init_node));
7213 r->purpose = purpose;
7214 r->value = value;
7215 r->origtype = origtype;
7217 *q = r;
7218 r->parent = p;
7219 r->left = 0;
7220 r->right = 0;
7221 r->balance = 0;
7223 while (p)
7225 struct init_node *s;
7227 if (r == p->left)
7229 if (p->balance == 0)
7230 p->balance = -1;
7231 else if (p->balance < 0)
7233 if (r->balance < 0)
7235 /* L rotation. */
7236 p->left = r->right;
7237 if (p->left)
7238 p->left->parent = p;
7239 r->right = p;
7241 p->balance = 0;
7242 r->balance = 0;
7244 s = p->parent;
7245 p->parent = r;
7246 r->parent = s;
7247 if (s)
7249 if (s->left == p)
7250 s->left = r;
7251 else
7252 s->right = r;
7254 else
7255 constructor_pending_elts = r;
7257 else
7259 /* LR rotation. */
7260 struct init_node *t = r->right;
7262 r->right = t->left;
7263 if (r->right)
7264 r->right->parent = r;
7265 t->left = r;
7267 p->left = t->right;
7268 if (p->left)
7269 p->left->parent = p;
7270 t->right = p;
7272 p->balance = t->balance < 0;
7273 r->balance = -(t->balance > 0);
7274 t->balance = 0;
7276 s = p->parent;
7277 p->parent = t;
7278 r->parent = t;
7279 t->parent = s;
7280 if (s)
7282 if (s->left == p)
7283 s->left = t;
7284 else
7285 s->right = t;
7287 else
7288 constructor_pending_elts = t;
7290 break;
7292 else
7294 /* p->balance == +1; growth of left side balances the node. */
7295 p->balance = 0;
7296 break;
7299 else /* r == p->right */
7301 if (p->balance == 0)
7302 /* Growth propagation from right side. */
7303 p->balance++;
7304 else if (p->balance > 0)
7306 if (r->balance > 0)
7308 /* R rotation. */
7309 p->right = r->left;
7310 if (p->right)
7311 p->right->parent = p;
7312 r->left = p;
7314 p->balance = 0;
7315 r->balance = 0;
7317 s = p->parent;
7318 p->parent = r;
7319 r->parent = s;
7320 if (s)
7322 if (s->left == p)
7323 s->left = r;
7324 else
7325 s->right = r;
7327 else
7328 constructor_pending_elts = r;
7330 else /* r->balance == -1 */
7332 /* RL rotation */
7333 struct init_node *t = r->left;
7335 r->left = t->right;
7336 if (r->left)
7337 r->left->parent = r;
7338 t->right = r;
7340 p->right = t->left;
7341 if (p->right)
7342 p->right->parent = p;
7343 t->left = p;
7345 r->balance = (t->balance < 0);
7346 p->balance = -(t->balance > 0);
7347 t->balance = 0;
7349 s = p->parent;
7350 p->parent = t;
7351 r->parent = t;
7352 t->parent = s;
7353 if (s)
7355 if (s->left == p)
7356 s->left = t;
7357 else
7358 s->right = t;
7360 else
7361 constructor_pending_elts = t;
7363 break;
7365 else
7367 /* p->balance == -1; growth of right side balances the node. */
7368 p->balance = 0;
7369 break;
7373 r = p;
7374 p = p->parent;
7378 /* Build AVL tree from a sorted chain. */
7380 static void
7381 set_nonincremental_init (struct obstack * braced_init_obstack)
7383 unsigned HOST_WIDE_INT ix;
7384 tree index, value;
7386 if (TREE_CODE (constructor_type) != RECORD_TYPE
7387 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7388 return;
7390 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7392 add_pending_init (index, value, NULL_TREE, false,
7393 braced_init_obstack);
7395 constructor_elements = 0;
7396 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7398 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7399 /* Skip any nameless bit fields at the beginning. */
7400 while (constructor_unfilled_fields != 0
7401 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7402 && DECL_NAME (constructor_unfilled_fields) == 0)
7403 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7406 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7408 if (TYPE_DOMAIN (constructor_type))
7409 constructor_unfilled_index
7410 = convert (bitsizetype,
7411 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7412 else
7413 constructor_unfilled_index = bitsize_zero_node;
7415 constructor_incremental = 0;
7418 /* Build AVL tree from a string constant. */
7420 static void
7421 set_nonincremental_init_from_string (tree str,
7422 struct obstack * braced_init_obstack)
7424 tree value, purpose, type;
7425 HOST_WIDE_INT val[2];
7426 const char *p, *end;
7427 int byte, wchar_bytes, charwidth, bitpos;
7429 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7431 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7432 charwidth = TYPE_PRECISION (char_type_node);
7433 type = TREE_TYPE (constructor_type);
7434 p = TREE_STRING_POINTER (str);
7435 end = p + TREE_STRING_LENGTH (str);
7437 for (purpose = bitsize_zero_node;
7438 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7439 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7441 if (wchar_bytes == 1)
7443 val[1] = (unsigned char) *p++;
7444 val[0] = 0;
7446 else
7448 val[0] = 0;
7449 val[1] = 0;
7450 for (byte = 0; byte < wchar_bytes; byte++)
7452 if (BYTES_BIG_ENDIAN)
7453 bitpos = (wchar_bytes - byte - 1) * charwidth;
7454 else
7455 bitpos = byte * charwidth;
7456 val[bitpos < HOST_BITS_PER_WIDE_INT]
7457 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7458 << (bitpos % HOST_BITS_PER_WIDE_INT);
7462 if (!TYPE_UNSIGNED (type))
7464 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7465 if (bitpos < HOST_BITS_PER_WIDE_INT)
7467 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7469 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7470 val[0] = -1;
7473 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7475 if (val[1] < 0)
7476 val[0] = -1;
7478 else if (val[0] & (((HOST_WIDE_INT) 1)
7479 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7480 val[0] |= ((HOST_WIDE_INT) -1)
7481 << (bitpos - HOST_BITS_PER_WIDE_INT);
7484 value = build_int_cst_wide (type, val[1], val[0]);
7485 add_pending_init (purpose, value, NULL_TREE, false,
7486 braced_init_obstack);
7489 constructor_incremental = 0;
7492 /* Return value of FIELD in pending initializer or zero if the field was
7493 not initialized yet. */
7495 static tree
7496 find_init_member (tree field, struct obstack * braced_init_obstack)
7498 struct init_node *p;
7500 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7502 if (constructor_incremental
7503 && tree_int_cst_lt (field, constructor_unfilled_index))
7504 set_nonincremental_init (braced_init_obstack);
7506 p = constructor_pending_elts;
7507 while (p)
7509 if (tree_int_cst_lt (field, p->purpose))
7510 p = p->left;
7511 else if (tree_int_cst_lt (p->purpose, field))
7512 p = p->right;
7513 else
7514 return p->value;
7517 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7519 tree bitpos = bit_position (field);
7521 if (constructor_incremental
7522 && (!constructor_unfilled_fields
7523 || tree_int_cst_lt (bitpos,
7524 bit_position (constructor_unfilled_fields))))
7525 set_nonincremental_init (braced_init_obstack);
7527 p = constructor_pending_elts;
7528 while (p)
7530 if (field == p->purpose)
7531 return p->value;
7532 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7533 p = p->left;
7534 else
7535 p = p->right;
7538 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7540 if (!VEC_empty (constructor_elt, constructor_elements)
7541 && (VEC_last (constructor_elt, constructor_elements)->index
7542 == field))
7543 return VEC_last (constructor_elt, constructor_elements)->value;
7545 return 0;
7548 /* "Output" the next constructor element.
7549 At top level, really output it to assembler code now.
7550 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7551 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7552 TYPE is the data type that the containing data type wants here.
7553 FIELD is the field (a FIELD_DECL) or the index that this element fills.
7554 If VALUE is a string constant, STRICT_STRING is true if it is
7555 unparenthesized or we should not warn here for it being parenthesized.
7556 For other types of VALUE, STRICT_STRING is not used.
7558 PENDING if non-nil means output pending elements that belong
7559 right after this element. (PENDING is normally 1;
7560 it is 0 while outputting pending elements, to avoid recursion.)
7562 IMPLICIT is true if value comes from pop_init_level (1),
7563 the new initializer has been merged with the existing one
7564 and thus no warnings should be emitted about overriding an
7565 existing initializer. */
7567 static void
7568 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7569 tree field, int pending, bool implicit,
7570 struct obstack * braced_init_obstack)
7572 tree semantic_type = NULL_TREE;
7573 constructor_elt *celt;
7574 bool maybe_const = true;
7575 bool npc;
7577 if (type == error_mark_node || value == error_mark_node)
7579 constructor_erroneous = 1;
7580 return;
7582 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7583 && (TREE_CODE (value) == STRING_CST
7584 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7585 && !(TREE_CODE (value) == STRING_CST
7586 && TREE_CODE (type) == ARRAY_TYPE
7587 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7588 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7589 TYPE_MAIN_VARIANT (type)))
7590 value = array_to_pointer_conversion (input_location, value);
7592 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7593 && require_constant_value && !flag_isoc99 && pending)
7595 /* As an extension, allow initializing objects with static storage
7596 duration with compound literals (which are then treated just as
7597 the brace enclosed list they contain). */
7598 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7599 value = DECL_INITIAL (decl);
7602 npc = null_pointer_constant_p (value);
7603 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7605 semantic_type = TREE_TYPE (value);
7606 value = TREE_OPERAND (value, 0);
7608 value = c_fully_fold (value, require_constant_value, &maybe_const);
7610 if (value == error_mark_node)
7611 constructor_erroneous = 1;
7612 else if (!TREE_CONSTANT (value))
7613 constructor_constant = 0;
7614 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7615 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7616 || TREE_CODE (constructor_type) == UNION_TYPE)
7617 && DECL_C_BIT_FIELD (field)
7618 && TREE_CODE (value) != INTEGER_CST))
7619 constructor_simple = 0;
7620 if (!maybe_const)
7621 constructor_nonconst = 1;
7623 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7625 if (require_constant_value)
7627 error_init ("initializer element is not constant");
7628 value = error_mark_node;
7630 else if (require_constant_elements)
7631 pedwarn (input_location, 0,
7632 "initializer element is not computable at load time");
7634 else if (!maybe_const
7635 && (require_constant_value || require_constant_elements))
7636 pedwarn_init (input_location, 0,
7637 "initializer element is not a constant expression");
7639 /* Issue -Wc++-compat warnings about initializing a bitfield with
7640 enum type. */
7641 if (warn_cxx_compat
7642 && field != NULL_TREE
7643 && TREE_CODE (field) == FIELD_DECL
7644 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7645 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7646 != TYPE_MAIN_VARIANT (type))
7647 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7649 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7650 if (checktype != error_mark_node
7651 && (TYPE_MAIN_VARIANT (checktype)
7652 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7653 warning_init (OPT_Wc___compat,
7654 "enum conversion in initialization is invalid in C++");
7657 /* If this field is empty (and not at the end of structure),
7658 don't do anything other than checking the initializer. */
7659 if (field
7660 && (TREE_TYPE (field) == error_mark_node
7661 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7662 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7663 && (TREE_CODE (constructor_type) == ARRAY_TYPE
7664 || DECL_CHAIN (field)))))
7665 return;
7667 if (semantic_type)
7668 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7669 value = digest_init (input_location, type, value, origtype, npc,
7670 strict_string, require_constant_value);
7671 if (value == error_mark_node)
7673 constructor_erroneous = 1;
7674 return;
7676 if (require_constant_value || require_constant_elements)
7677 constant_expression_warning (value);
7679 /* If this element doesn't come next in sequence,
7680 put it on constructor_pending_elts. */
7681 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7682 && (!constructor_incremental
7683 || !tree_int_cst_equal (field, constructor_unfilled_index)))
7685 if (constructor_incremental
7686 && tree_int_cst_lt (field, constructor_unfilled_index))
7687 set_nonincremental_init (braced_init_obstack);
7689 add_pending_init (field, value, origtype, implicit,
7690 braced_init_obstack);
7691 return;
7693 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7694 && (!constructor_incremental
7695 || field != constructor_unfilled_fields))
7697 /* We do this for records but not for unions. In a union,
7698 no matter which field is specified, it can be initialized
7699 right away since it starts at the beginning of the union. */
7700 if (constructor_incremental)
7702 if (!constructor_unfilled_fields)
7703 set_nonincremental_init (braced_init_obstack);
7704 else
7706 tree bitpos, unfillpos;
7708 bitpos = bit_position (field);
7709 unfillpos = bit_position (constructor_unfilled_fields);
7711 if (tree_int_cst_lt (bitpos, unfillpos))
7712 set_nonincremental_init (braced_init_obstack);
7716 add_pending_init (field, value, origtype, implicit,
7717 braced_init_obstack);
7718 return;
7720 else if (TREE_CODE (constructor_type) == UNION_TYPE
7721 && !VEC_empty (constructor_elt, constructor_elements))
7723 if (!implicit)
7725 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7726 constructor_elements)->value))
7727 warning_init (0,
7728 "initialized field with side-effects overwritten");
7729 else if (warn_override_init)
7730 warning_init (OPT_Woverride_init, "initialized field overwritten");
7733 /* We can have just one union field set. */
7734 constructor_elements = 0;
7737 /* Otherwise, output this element either to
7738 constructor_elements or to the assembler file. */
7740 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7741 celt->index = field;
7742 celt->value = value;
7744 /* Advance the variable that indicates sequential elements output. */
7745 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7746 constructor_unfilled_index
7747 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7748 bitsize_one_node);
7749 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7751 constructor_unfilled_fields
7752 = DECL_CHAIN (constructor_unfilled_fields);
7754 /* Skip any nameless bit fields. */
7755 while (constructor_unfilled_fields != 0
7756 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7757 && DECL_NAME (constructor_unfilled_fields) == 0)
7758 constructor_unfilled_fields =
7759 DECL_CHAIN (constructor_unfilled_fields);
7761 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7762 constructor_unfilled_fields = 0;
7764 /* Now output any pending elements which have become next. */
7765 if (pending)
7766 output_pending_init_elements (0, braced_init_obstack);
7769 /* Output any pending elements which have become next.
7770 As we output elements, constructor_unfilled_{fields,index}
7771 advances, which may cause other elements to become next;
7772 if so, they too are output.
7774 If ALL is 0, we return when there are
7775 no more pending elements to output now.
7777 If ALL is 1, we output space as necessary so that
7778 we can output all the pending elements. */
7779 static void
7780 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7782 struct init_node *elt = constructor_pending_elts;
7783 tree next;
7785 retry:
7787 /* Look through the whole pending tree.
7788 If we find an element that should be output now,
7789 output it. Otherwise, set NEXT to the element
7790 that comes first among those still pending. */
7792 next = 0;
7793 while (elt)
7795 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7797 if (tree_int_cst_equal (elt->purpose,
7798 constructor_unfilled_index))
7799 output_init_element (elt->value, elt->origtype, true,
7800 TREE_TYPE (constructor_type),
7801 constructor_unfilled_index, 0, false,
7802 braced_init_obstack);
7803 else if (tree_int_cst_lt (constructor_unfilled_index,
7804 elt->purpose))
7806 /* Advance to the next smaller node. */
7807 if (elt->left)
7808 elt = elt->left;
7809 else
7811 /* We have reached the smallest node bigger than the
7812 current unfilled index. Fill the space first. */
7813 next = elt->purpose;
7814 break;
7817 else
7819 /* Advance to the next bigger node. */
7820 if (elt->right)
7821 elt = elt->right;
7822 else
7824 /* We have reached the biggest node in a subtree. Find
7825 the parent of it, which is the next bigger node. */
7826 while (elt->parent && elt->parent->right == elt)
7827 elt = elt->parent;
7828 elt = elt->parent;
7829 if (elt && tree_int_cst_lt (constructor_unfilled_index,
7830 elt->purpose))
7832 next = elt->purpose;
7833 break;
7838 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7839 || TREE_CODE (constructor_type) == UNION_TYPE)
7841 tree ctor_unfilled_bitpos, elt_bitpos;
7843 /* If the current record is complete we are done. */
7844 if (constructor_unfilled_fields == 0)
7845 break;
7847 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7848 elt_bitpos = bit_position (elt->purpose);
7849 /* We can't compare fields here because there might be empty
7850 fields in between. */
7851 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7853 constructor_unfilled_fields = elt->purpose;
7854 output_init_element (elt->value, elt->origtype, true,
7855 TREE_TYPE (elt->purpose),
7856 elt->purpose, 0, false,
7857 braced_init_obstack);
7859 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7861 /* Advance to the next smaller node. */
7862 if (elt->left)
7863 elt = elt->left;
7864 else
7866 /* We have reached the smallest node bigger than the
7867 current unfilled field. Fill the space first. */
7868 next = elt->purpose;
7869 break;
7872 else
7874 /* Advance to the next bigger node. */
7875 if (elt->right)
7876 elt = elt->right;
7877 else
7879 /* We have reached the biggest node in a subtree. Find
7880 the parent of it, which is the next bigger node. */
7881 while (elt->parent && elt->parent->right == elt)
7882 elt = elt->parent;
7883 elt = elt->parent;
7884 if (elt
7885 && (tree_int_cst_lt (ctor_unfilled_bitpos,
7886 bit_position (elt->purpose))))
7888 next = elt->purpose;
7889 break;
7896 /* Ordinarily return, but not if we want to output all
7897 and there are elements left. */
7898 if (!(all && next != 0))
7899 return;
7901 /* If it's not incremental, just skip over the gap, so that after
7902 jumping to retry we will output the next successive element. */
7903 if (TREE_CODE (constructor_type) == RECORD_TYPE
7904 || TREE_CODE (constructor_type) == UNION_TYPE)
7905 constructor_unfilled_fields = next;
7906 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7907 constructor_unfilled_index = next;
7909 /* ELT now points to the node in the pending tree with the next
7910 initializer to output. */
7911 goto retry;
7914 /* Add one non-braced element to the current constructor level.
7915 This adjusts the current position within the constructor's type.
7916 This may also start or terminate implicit levels
7917 to handle a partly-braced initializer.
7919 Once this has found the correct level for the new element,
7920 it calls output_init_element.
7922 IMPLICIT is true if value comes from pop_init_level (1),
7923 the new initializer has been merged with the existing one
7924 and thus no warnings should be emitted about overriding an
7925 existing initializer. */
7927 void
7928 process_init_element (struct c_expr value, bool implicit,
7929 struct obstack * braced_init_obstack)
7931 tree orig_value = value.value;
7932 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
7933 bool strict_string = value.original_code == STRING_CST;
7935 designator_depth = 0;
7936 designator_erroneous = 0;
7938 /* Handle superfluous braces around string cst as in
7939 char x[] = {"foo"}; */
7940 if (string_flag
7941 && constructor_type
7942 && TREE_CODE (constructor_type) == ARRAY_TYPE
7943 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
7944 && integer_zerop (constructor_unfilled_index))
7946 if (constructor_stack->replacement_value.value)
7947 error_init ("excess elements in char array initializer");
7948 constructor_stack->replacement_value = value;
7949 return;
7952 if (constructor_stack->replacement_value.value != 0)
7954 error_init ("excess elements in struct initializer");
7955 return;
7958 /* Ignore elements of a brace group if it is entirely superfluous
7959 and has already been diagnosed. */
7960 if (constructor_type == 0)
7961 return;
7963 /* If we've exhausted any levels that didn't have braces,
7964 pop them now. */
7965 while (constructor_stack->implicit)
7967 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7968 || TREE_CODE (constructor_type) == UNION_TYPE)
7969 && constructor_fields == 0)
7970 process_init_element (pop_init_level (1, braced_init_obstack),
7971 true, braced_init_obstack);
7972 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
7973 || TREE_CODE (constructor_type) == VECTOR_TYPE)
7974 && (constructor_max_index == 0
7975 || tree_int_cst_lt (constructor_max_index,
7976 constructor_index)))
7977 process_init_element (pop_init_level (1, braced_init_obstack),
7978 true, braced_init_obstack);
7979 else
7980 break;
7983 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
7984 if (constructor_range_stack)
7986 /* If value is a compound literal and we'll be just using its
7987 content, don't put it into a SAVE_EXPR. */
7988 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
7989 || !require_constant_value
7990 || flag_isoc99)
7992 tree semantic_type = NULL_TREE;
7993 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
7995 semantic_type = TREE_TYPE (value.value);
7996 value.value = TREE_OPERAND (value.value, 0);
7998 value.value = c_save_expr (value.value);
7999 if (semantic_type)
8000 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8001 value.value);
8005 while (1)
8007 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8009 tree fieldtype;
8010 enum tree_code fieldcode;
8012 if (constructor_fields == 0)
8014 pedwarn_init (input_location, 0,
8015 "excess elements in struct initializer");
8016 break;
8019 fieldtype = TREE_TYPE (constructor_fields);
8020 if (fieldtype != error_mark_node)
8021 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8022 fieldcode = TREE_CODE (fieldtype);
8024 /* Error for non-static initialization of a flexible array member. */
8025 if (fieldcode == ARRAY_TYPE
8026 && !require_constant_value
8027 && TYPE_SIZE (fieldtype) == NULL_TREE
8028 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8030 error_init ("non-static initialization of a flexible array member");
8031 break;
8034 /* Accept a string constant to initialize a subarray. */
8035 if (value.value != 0
8036 && fieldcode == ARRAY_TYPE
8037 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8038 && string_flag)
8039 value.value = orig_value;
8040 /* Otherwise, if we have come to a subaggregate,
8041 and we don't have an element of its type, push into it. */
8042 else if (value.value != 0
8043 && value.value != error_mark_node
8044 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8045 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8046 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8048 push_init_level (1, braced_init_obstack);
8049 continue;
8052 if (value.value)
8054 push_member_name (constructor_fields);
8055 output_init_element (value.value, value.original_type,
8056 strict_string, fieldtype,
8057 constructor_fields, 1, implicit,
8058 braced_init_obstack);
8059 RESTORE_SPELLING_DEPTH (constructor_depth);
8061 else
8062 /* Do the bookkeeping for an element that was
8063 directly output as a constructor. */
8065 /* For a record, keep track of end position of last field. */
8066 if (DECL_SIZE (constructor_fields))
8067 constructor_bit_index
8068 = size_binop_loc (input_location, PLUS_EXPR,
8069 bit_position (constructor_fields),
8070 DECL_SIZE (constructor_fields));
8072 /* If the current field was the first one not yet written out,
8073 it isn't now, so update. */
8074 if (constructor_unfilled_fields == constructor_fields)
8076 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8077 /* Skip any nameless bit fields. */
8078 while (constructor_unfilled_fields != 0
8079 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8080 && DECL_NAME (constructor_unfilled_fields) == 0)
8081 constructor_unfilled_fields =
8082 DECL_CHAIN (constructor_unfilled_fields);
8086 constructor_fields = DECL_CHAIN (constructor_fields);
8087 /* Skip any nameless bit fields at the beginning. */
8088 while (constructor_fields != 0
8089 && DECL_C_BIT_FIELD (constructor_fields)
8090 && DECL_NAME (constructor_fields) == 0)
8091 constructor_fields = DECL_CHAIN (constructor_fields);
8093 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8095 tree fieldtype;
8096 enum tree_code fieldcode;
8098 if (constructor_fields == 0)
8100 pedwarn_init (input_location, 0,
8101 "excess elements in union initializer");
8102 break;
8105 fieldtype = TREE_TYPE (constructor_fields);
8106 if (fieldtype != error_mark_node)
8107 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8108 fieldcode = TREE_CODE (fieldtype);
8110 /* Warn that traditional C rejects initialization of unions.
8111 We skip the warning if the value is zero. This is done
8112 under the assumption that the zero initializer in user
8113 code appears conditioned on e.g. __STDC__ to avoid
8114 "missing initializer" warnings and relies on default
8115 initialization to zero in the traditional C case.
8116 We also skip the warning if the initializer is designated,
8117 again on the assumption that this must be conditional on
8118 __STDC__ anyway (and we've already complained about the
8119 member-designator already). */
8120 if (!in_system_header && !constructor_designated
8121 && !(value.value && (integer_zerop (value.value)
8122 || real_zerop (value.value))))
8123 warning (OPT_Wtraditional, "traditional C rejects initialization "
8124 "of unions");
8126 /* Accept a string constant to initialize a subarray. */
8127 if (value.value != 0
8128 && fieldcode == ARRAY_TYPE
8129 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8130 && string_flag)
8131 value.value = orig_value;
8132 /* Otherwise, if we have come to a subaggregate,
8133 and we don't have an element of its type, push into it. */
8134 else if (value.value != 0
8135 && value.value != error_mark_node
8136 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8137 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8138 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8140 push_init_level (1, braced_init_obstack);
8141 continue;
8144 if (value.value)
8146 push_member_name (constructor_fields);
8147 output_init_element (value.value, value.original_type,
8148 strict_string, fieldtype,
8149 constructor_fields, 1, implicit,
8150 braced_init_obstack);
8151 RESTORE_SPELLING_DEPTH (constructor_depth);
8153 else
8154 /* Do the bookkeeping for an element that was
8155 directly output as a constructor. */
8157 constructor_bit_index = DECL_SIZE (constructor_fields);
8158 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8161 constructor_fields = 0;
8163 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8165 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8166 enum tree_code eltcode = TREE_CODE (elttype);
8168 /* Accept a string constant to initialize a subarray. */
8169 if (value.value != 0
8170 && eltcode == ARRAY_TYPE
8171 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8172 && string_flag)
8173 value.value = orig_value;
8174 /* Otherwise, if we have come to a subaggregate,
8175 and we don't have an element of its type, push into it. */
8176 else if (value.value != 0
8177 && value.value != error_mark_node
8178 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8179 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8180 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8182 push_init_level (1, braced_init_obstack);
8183 continue;
8186 if (constructor_max_index != 0
8187 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8188 || integer_all_onesp (constructor_max_index)))
8190 pedwarn_init (input_location, 0,
8191 "excess elements in array initializer");
8192 break;
8195 /* Now output the actual element. */
8196 if (value.value)
8198 push_array_bounds (tree_low_cst (constructor_index, 1));
8199 output_init_element (value.value, value.original_type,
8200 strict_string, elttype,
8201 constructor_index, 1, implicit,
8202 braced_init_obstack);
8203 RESTORE_SPELLING_DEPTH (constructor_depth);
8206 constructor_index
8207 = size_binop_loc (input_location, PLUS_EXPR,
8208 constructor_index, bitsize_one_node);
8210 if (!value.value)
8211 /* If we are doing the bookkeeping for an element that was
8212 directly output as a constructor, we must update
8213 constructor_unfilled_index. */
8214 constructor_unfilled_index = constructor_index;
8216 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8218 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8220 /* Do a basic check of initializer size. Note that vectors
8221 always have a fixed size derived from their type. */
8222 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8224 pedwarn_init (input_location, 0,
8225 "excess elements in vector initializer");
8226 break;
8229 /* Now output the actual element. */
8230 if (value.value)
8232 if (TREE_CODE (value.value) == VECTOR_CST)
8233 elttype = TYPE_MAIN_VARIANT (constructor_type);
8234 output_init_element (value.value, value.original_type,
8235 strict_string, elttype,
8236 constructor_index, 1, implicit,
8237 braced_init_obstack);
8240 constructor_index
8241 = size_binop_loc (input_location,
8242 PLUS_EXPR, constructor_index, bitsize_one_node);
8244 if (!value.value)
8245 /* If we are doing the bookkeeping for an element that was
8246 directly output as a constructor, we must update
8247 constructor_unfilled_index. */
8248 constructor_unfilled_index = constructor_index;
8251 /* Handle the sole element allowed in a braced initializer
8252 for a scalar variable. */
8253 else if (constructor_type != error_mark_node
8254 && constructor_fields == 0)
8256 pedwarn_init (input_location, 0,
8257 "excess elements in scalar initializer");
8258 break;
8260 else
8262 if (value.value)
8263 output_init_element (value.value, value.original_type,
8264 strict_string, constructor_type,
8265 NULL_TREE, 1, implicit,
8266 braced_init_obstack);
8267 constructor_fields = 0;
8270 /* Handle range initializers either at this level or anywhere higher
8271 in the designator stack. */
8272 if (constructor_range_stack)
8274 struct constructor_range_stack *p, *range_stack;
8275 int finish = 0;
8277 range_stack = constructor_range_stack;
8278 constructor_range_stack = 0;
8279 while (constructor_stack != range_stack->stack)
8281 gcc_assert (constructor_stack->implicit);
8282 process_init_element (pop_init_level (1,
8283 braced_init_obstack),
8284 true, braced_init_obstack);
8286 for (p = range_stack;
8287 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8288 p = p->prev)
8290 gcc_assert (constructor_stack->implicit);
8291 process_init_element (pop_init_level (1, braced_init_obstack),
8292 true, braced_init_obstack);
8295 p->index = size_binop_loc (input_location,
8296 PLUS_EXPR, p->index, bitsize_one_node);
8297 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8298 finish = 1;
8300 while (1)
8302 constructor_index = p->index;
8303 constructor_fields = p->fields;
8304 if (finish && p->range_end && p->index == p->range_start)
8306 finish = 0;
8307 p->prev = 0;
8309 p = p->next;
8310 if (!p)
8311 break;
8312 push_init_level (2, braced_init_obstack);
8313 p->stack = constructor_stack;
8314 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8315 p->index = p->range_start;
8318 if (!finish)
8319 constructor_range_stack = range_stack;
8320 continue;
8323 break;
8326 constructor_range_stack = 0;
8329 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8330 (guaranteed to be 'volatile' or null) and ARGS (represented using
8331 an ASM_EXPR node). */
8332 tree
8333 build_asm_stmt (tree cv_qualifier, tree args)
8335 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8336 ASM_VOLATILE_P (args) = 1;
8337 return add_stmt (args);
8340 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8341 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8342 SIMPLE indicates whether there was anything at all after the
8343 string in the asm expression -- asm("blah") and asm("blah" : )
8344 are subtly different. We use a ASM_EXPR node to represent this. */
8345 tree
8346 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8347 tree clobbers, tree labels, bool simple)
8349 tree tail;
8350 tree args;
8351 int i;
8352 const char *constraint;
8353 const char **oconstraints;
8354 bool allows_mem, allows_reg, is_inout;
8355 int ninputs, noutputs;
8357 ninputs = list_length (inputs);
8358 noutputs = list_length (outputs);
8359 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8361 string = resolve_asm_operand_names (string, outputs, inputs, labels);
8363 /* Remove output conversions that change the type but not the mode. */
8364 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8366 tree output = TREE_VALUE (tail);
8368 /* ??? Really, this should not be here. Users should be using a
8369 proper lvalue, dammit. But there's a long history of using casts
8370 in the output operands. In cases like longlong.h, this becomes a
8371 primitive form of typechecking -- if the cast can be removed, then
8372 the output operand had a type of the proper width; otherwise we'll
8373 get an error. Gross, but ... */
8374 STRIP_NOPS (output);
8376 if (!lvalue_or_else (output, lv_asm))
8377 output = error_mark_node;
8379 if (output != error_mark_node
8380 && (TREE_READONLY (output)
8381 || TYPE_READONLY (TREE_TYPE (output))
8382 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8383 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8384 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8385 readonly_error (output, lv_asm);
8387 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8388 oconstraints[i] = constraint;
8390 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8391 &allows_mem, &allows_reg, &is_inout))
8393 /* If the operand is going to end up in memory,
8394 mark it addressable. */
8395 if (!allows_reg && !c_mark_addressable (output))
8396 output = error_mark_node;
8398 else
8399 output = error_mark_node;
8401 TREE_VALUE (tail) = output;
8404 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8406 tree input;
8408 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8409 input = TREE_VALUE (tail);
8411 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8412 oconstraints, &allows_mem, &allows_reg))
8414 /* If the operand is going to end up in memory,
8415 mark it addressable. */
8416 if (!allows_reg && allows_mem)
8418 /* Strip the nops as we allow this case. FIXME, this really
8419 should be rejected or made deprecated. */
8420 STRIP_NOPS (input);
8421 if (!c_mark_addressable (input))
8422 input = error_mark_node;
8425 else
8426 input = error_mark_node;
8428 TREE_VALUE (tail) = input;
8431 /* ASMs with labels cannot have outputs. This should have been
8432 enforced by the parser. */
8433 gcc_assert (outputs == NULL || labels == NULL);
8435 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8437 /* asm statements without outputs, including simple ones, are treated
8438 as volatile. */
8439 ASM_INPUT_P (args) = simple;
8440 ASM_VOLATILE_P (args) = (noutputs == 0);
8442 return args;
8445 /* Generate a goto statement to LABEL. LOC is the location of the
8446 GOTO. */
8448 tree
8449 c_finish_goto_label (location_t loc, tree label)
8451 tree decl = lookup_label_for_goto (loc, label);
8452 if (!decl)
8453 return NULL_TREE;
8454 TREE_USED (decl) = 1;
8456 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8457 SET_EXPR_LOCATION (t, loc);
8458 return add_stmt (t);
8462 /* Generate a computed goto statement to EXPR. LOC is the location of
8463 the GOTO. */
8465 tree
8466 c_finish_goto_ptr (location_t loc, tree expr)
8468 tree t;
8469 pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8470 expr = c_fully_fold (expr, false, NULL);
8471 expr = convert (ptr_type_node, expr);
8472 t = build1 (GOTO_EXPR, void_type_node, expr);
8473 SET_EXPR_LOCATION (t, loc);
8474 return add_stmt (t);
8477 /* Generate a C `return' statement. RETVAL is the expression for what
8478 to return, or a null pointer for `return;' with no value. LOC is
8479 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8480 is the original type of RETVAL. */
8482 tree
8483 c_finish_return (location_t loc, tree retval, tree origtype)
8485 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8486 bool no_warning = false;
8487 bool npc = false;
8489 if (TREE_THIS_VOLATILE (current_function_decl))
8490 warning_at (loc, 0,
8491 "function declared %<noreturn%> has a %<return%> statement");
8493 if (retval)
8495 tree semantic_type = NULL_TREE;
8496 npc = null_pointer_constant_p (retval);
8497 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8499 semantic_type = TREE_TYPE (retval);
8500 retval = TREE_OPERAND (retval, 0);
8502 retval = c_fully_fold (retval, false, NULL);
8503 if (semantic_type)
8504 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8507 if (!retval)
8509 current_function_returns_null = 1;
8510 if ((warn_return_type || flag_isoc99)
8511 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8513 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8514 "%<return%> with no value, in "
8515 "function returning non-void");
8516 no_warning = true;
8519 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8521 current_function_returns_null = 1;
8522 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8523 pedwarn (loc, 0,
8524 "%<return%> with a value, in function returning void");
8525 else
8526 pedwarn (loc, OPT_pedantic, "ISO C forbids "
8527 "%<return%> with expression, in function returning void");
8529 else
8531 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8532 ic_return,
8533 npc, NULL_TREE, NULL_TREE, 0);
8534 tree res = DECL_RESULT (current_function_decl);
8535 tree inner;
8537 current_function_returns_value = 1;
8538 if (t == error_mark_node)
8539 return NULL_TREE;
8541 inner = t = convert (TREE_TYPE (res), t);
8543 /* Strip any conversions, additions, and subtractions, and see if
8544 we are returning the address of a local variable. Warn if so. */
8545 while (1)
8547 switch (TREE_CODE (inner))
8549 CASE_CONVERT:
8550 case NON_LVALUE_EXPR:
8551 case PLUS_EXPR:
8552 case POINTER_PLUS_EXPR:
8553 inner = TREE_OPERAND (inner, 0);
8554 continue;
8556 case MINUS_EXPR:
8557 /* If the second operand of the MINUS_EXPR has a pointer
8558 type (or is converted from it), this may be valid, so
8559 don't give a warning. */
8561 tree op1 = TREE_OPERAND (inner, 1);
8563 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8564 && (CONVERT_EXPR_P (op1)
8565 || TREE_CODE (op1) == NON_LVALUE_EXPR))
8566 op1 = TREE_OPERAND (op1, 0);
8568 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8569 break;
8571 inner = TREE_OPERAND (inner, 0);
8572 continue;
8575 case ADDR_EXPR:
8576 inner = TREE_OPERAND (inner, 0);
8578 while (REFERENCE_CLASS_P (inner)
8579 && TREE_CODE (inner) != INDIRECT_REF)
8580 inner = TREE_OPERAND (inner, 0);
8582 if (DECL_P (inner)
8583 && !DECL_EXTERNAL (inner)
8584 && !TREE_STATIC (inner)
8585 && DECL_CONTEXT (inner) == current_function_decl)
8586 warning_at (loc,
8587 0, "function returns address of local variable");
8588 break;
8590 default:
8591 break;
8594 break;
8597 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8598 SET_EXPR_LOCATION (retval, loc);
8600 if (warn_sequence_point)
8601 verify_sequence_points (retval);
8604 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8605 TREE_NO_WARNING (ret_stmt) |= no_warning;
8606 return add_stmt (ret_stmt);
8609 struct c_switch {
8610 /* The SWITCH_EXPR being built. */
8611 tree switch_expr;
8613 /* The original type of the testing expression, i.e. before the
8614 default conversion is applied. */
8615 tree orig_type;
8617 /* A splay-tree mapping the low element of a case range to the high
8618 element, or NULL_TREE if there is no high element. Used to
8619 determine whether or not a new case label duplicates an old case
8620 label. We need a tree, rather than simply a hash table, because
8621 of the GNU case range extension. */
8622 splay_tree cases;
8624 /* The bindings at the point of the switch. This is used for
8625 warnings crossing decls when branching to a case label. */
8626 struct c_spot_bindings *bindings;
8628 /* The next node on the stack. */
8629 struct c_switch *next;
8632 /* A stack of the currently active switch statements. The innermost
8633 switch statement is on the top of the stack. There is no need to
8634 mark the stack for garbage collection because it is only active
8635 during the processing of the body of a function, and we never
8636 collect at that point. */
8638 struct c_switch *c_switch_stack;
8640 /* Start a C switch statement, testing expression EXP. Return the new
8641 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8642 SWITCH_COND_LOC is the location of the switch's condition. */
8644 tree
8645 c_start_case (location_t switch_loc,
8646 location_t switch_cond_loc,
8647 tree exp)
8649 tree orig_type = error_mark_node;
8650 struct c_switch *cs;
8652 if (exp != error_mark_node)
8654 orig_type = TREE_TYPE (exp);
8656 if (!INTEGRAL_TYPE_P (orig_type))
8658 if (orig_type != error_mark_node)
8660 error_at (switch_cond_loc, "switch quantity not an integer");
8661 orig_type = error_mark_node;
8663 exp = integer_zero_node;
8665 else
8667 tree type = TYPE_MAIN_VARIANT (orig_type);
8669 if (!in_system_header
8670 && (type == long_integer_type_node
8671 || type == long_unsigned_type_node))
8672 warning_at (switch_cond_loc,
8673 OPT_Wtraditional, "%<long%> switch expression not "
8674 "converted to %<int%> in ISO C");
8676 exp = c_fully_fold (exp, false, NULL);
8677 exp = default_conversion (exp);
8679 if (warn_sequence_point)
8680 verify_sequence_points (exp);
8684 /* Add this new SWITCH_EXPR to the stack. */
8685 cs = XNEW (struct c_switch);
8686 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8687 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8688 cs->orig_type = orig_type;
8689 cs->cases = splay_tree_new (case_compare, NULL, NULL);
8690 cs->bindings = c_get_switch_bindings ();
8691 cs->next = c_switch_stack;
8692 c_switch_stack = cs;
8694 return add_stmt (cs->switch_expr);
8697 /* Process a case label at location LOC. */
8699 tree
8700 do_case (location_t loc, tree low_value, tree high_value)
8702 tree label = NULL_TREE;
8704 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8706 low_value = c_fully_fold (low_value, false, NULL);
8707 if (TREE_CODE (low_value) == INTEGER_CST)
8708 pedwarn (input_location, OPT_pedantic,
8709 "case label is not an integer constant expression");
8712 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8714 high_value = c_fully_fold (high_value, false, NULL);
8715 if (TREE_CODE (high_value) == INTEGER_CST)
8716 pedwarn (input_location, OPT_pedantic,
8717 "case label is not an integer constant expression");
8720 if (c_switch_stack == NULL)
8722 if (low_value)
8723 error_at (loc, "case label not within a switch statement");
8724 else
8725 error_at (loc, "%<default%> label not within a switch statement");
8726 return NULL_TREE;
8729 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8730 EXPR_LOCATION (c_switch_stack->switch_expr),
8731 loc))
8732 return NULL_TREE;
8734 label = c_add_case_label (loc, c_switch_stack->cases,
8735 SWITCH_COND (c_switch_stack->switch_expr),
8736 c_switch_stack->orig_type,
8737 low_value, high_value);
8738 if (label == error_mark_node)
8739 label = NULL_TREE;
8740 return label;
8743 /* Finish the switch statement. */
8745 void
8746 c_finish_case (tree body)
8748 struct c_switch *cs = c_switch_stack;
8749 location_t switch_location;
8751 SWITCH_BODY (cs->switch_expr) = body;
8753 /* Emit warnings as needed. */
8754 switch_location = EXPR_LOCATION (cs->switch_expr);
8755 c_do_switch_warnings (cs->cases, switch_location,
8756 TREE_TYPE (cs->switch_expr),
8757 SWITCH_COND (cs->switch_expr));
8759 /* Pop the stack. */
8760 c_switch_stack = cs->next;
8761 splay_tree_delete (cs->cases);
8762 c_release_switch_bindings (cs->bindings);
8763 XDELETE (cs);
8766 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
8767 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8768 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
8769 statement, and was not surrounded with parenthesis. */
8771 void
8772 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8773 tree else_block, bool nested_if)
8775 tree stmt;
8777 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
8778 if (warn_parentheses && nested_if && else_block == NULL)
8780 tree inner_if = then_block;
8782 /* We know from the grammar productions that there is an IF nested
8783 within THEN_BLOCK. Due to labels and c99 conditional declarations,
8784 it might not be exactly THEN_BLOCK, but should be the last
8785 non-container statement within. */
8786 while (1)
8787 switch (TREE_CODE (inner_if))
8789 case COND_EXPR:
8790 goto found;
8791 case BIND_EXPR:
8792 inner_if = BIND_EXPR_BODY (inner_if);
8793 break;
8794 case STATEMENT_LIST:
8795 inner_if = expr_last (then_block);
8796 break;
8797 case TRY_FINALLY_EXPR:
8798 case TRY_CATCH_EXPR:
8799 inner_if = TREE_OPERAND (inner_if, 0);
8800 break;
8801 default:
8802 gcc_unreachable ();
8804 found:
8806 if (COND_EXPR_ELSE (inner_if))
8807 warning_at (if_locus, OPT_Wparentheses,
8808 "suggest explicit braces to avoid ambiguous %<else%>");
8811 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8812 SET_EXPR_LOCATION (stmt, if_locus);
8813 add_stmt (stmt);
8816 /* Emit a general-purpose loop construct. START_LOCUS is the location of
8817 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
8818 is false for DO loops. INCR is the FOR increment expression. BODY is
8819 the statement controlled by the loop. BLAB is the break label. CLAB is
8820 the continue label. Everything is allowed to be NULL. */
8822 void
8823 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8824 tree blab, tree clab, bool cond_is_first)
8826 tree entry = NULL, exit = NULL, t;
8828 /* If the condition is zero don't generate a loop construct. */
8829 if (cond && integer_zerop (cond))
8831 if (cond_is_first)
8833 t = build_and_jump (&blab);
8834 SET_EXPR_LOCATION (t, start_locus);
8835 add_stmt (t);
8838 else
8840 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8842 /* If we have an exit condition, then we build an IF with gotos either
8843 out of the loop, or to the top of it. If there's no exit condition,
8844 then we just build a jump back to the top. */
8845 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8847 if (cond && !integer_nonzerop (cond))
8849 /* Canonicalize the loop condition to the end. This means
8850 generating a branch to the loop condition. Reuse the
8851 continue label, if possible. */
8852 if (cond_is_first)
8854 if (incr || !clab)
8856 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8857 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8859 else
8860 t = build1 (GOTO_EXPR, void_type_node, clab);
8861 SET_EXPR_LOCATION (t, start_locus);
8862 add_stmt (t);
8865 t = build_and_jump (&blab);
8866 if (cond_is_first)
8867 exit = fold_build3_loc (start_locus,
8868 COND_EXPR, void_type_node, cond, exit, t);
8869 else
8870 exit = fold_build3_loc (input_location,
8871 COND_EXPR, void_type_node, cond, exit, t);
8874 add_stmt (top);
8877 if (body)
8878 add_stmt (body);
8879 if (clab)
8880 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8881 if (incr)
8882 add_stmt (incr);
8883 if (entry)
8884 add_stmt (entry);
8885 if (exit)
8886 add_stmt (exit);
8887 if (blab)
8888 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8891 tree
8892 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
8894 bool skip;
8895 tree label = *label_p;
8897 /* In switch statements break is sometimes stylistically used after
8898 a return statement. This can lead to spurious warnings about
8899 control reaching the end of a non-void function when it is
8900 inlined. Note that we are calling block_may_fallthru with
8901 language specific tree nodes; this works because
8902 block_may_fallthru returns true when given something it does not
8903 understand. */
8904 skip = !block_may_fallthru (cur_stmt_list);
8906 if (!label)
8908 if (!skip)
8909 *label_p = label = create_artificial_label (loc);
8911 else if (TREE_CODE (label) == LABEL_DECL)
8913 else switch (TREE_INT_CST_LOW (label))
8915 case 0:
8916 if (is_break)
8917 error_at (loc, "break statement not within loop or switch");
8918 else
8919 error_at (loc, "continue statement not within a loop");
8920 return NULL_TREE;
8922 case 1:
8923 gcc_assert (is_break);
8924 error_at (loc, "break statement used with OpenMP for loop");
8925 return NULL_TREE;
8927 default:
8928 gcc_unreachable ();
8931 if (skip)
8932 return NULL_TREE;
8934 if (!is_break)
8935 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
8937 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
8940 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
8942 static void
8943 emit_side_effect_warnings (location_t loc, tree expr)
8945 if (expr == error_mark_node)
8947 else if (!TREE_SIDE_EFFECTS (expr))
8949 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
8950 warning_at (loc, OPT_Wunused_value, "statement with no effect");
8952 else
8953 warn_if_unused_value (expr, loc);
8956 /* Process an expression as if it were a complete statement. Emit
8957 diagnostics, but do not call ADD_STMT. LOC is the location of the
8958 statement. */
8960 tree
8961 c_process_expr_stmt (location_t loc, tree expr)
8963 tree exprv;
8965 if (!expr)
8966 return NULL_TREE;
8968 expr = c_fully_fold (expr, false, NULL);
8970 if (warn_sequence_point)
8971 verify_sequence_points (expr);
8973 if (TREE_TYPE (expr) != error_mark_node
8974 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
8975 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
8976 error_at (loc, "expression statement has incomplete type");
8978 /* If we're not processing a statement expression, warn about unused values.
8979 Warnings for statement expressions will be emitted later, once we figure
8980 out which is the result. */
8981 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8982 && warn_unused_value)
8983 emit_side_effect_warnings (loc, expr);
8985 exprv = expr;
8986 while (TREE_CODE (exprv) == COMPOUND_EXPR)
8987 exprv = TREE_OPERAND (exprv, 1);
8988 if (DECL_P (exprv) || handled_component_p (exprv))
8989 mark_exp_read (exprv);
8991 /* If the expression is not of a type to which we cannot assign a line
8992 number, wrap the thing in a no-op NOP_EXPR. */
8993 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
8995 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
8996 SET_EXPR_LOCATION (expr, loc);
8999 return expr;
9002 /* Emit an expression as a statement. LOC is the location of the
9003 expression. */
9005 tree
9006 c_finish_expr_stmt (location_t loc, tree expr)
9008 if (expr)
9009 return add_stmt (c_process_expr_stmt (loc, expr));
9010 else
9011 return NULL;
9014 /* Do the opposite and emit a statement as an expression. To begin,
9015 create a new binding level and return it. */
9017 tree
9018 c_begin_stmt_expr (void)
9020 tree ret;
9022 /* We must force a BLOCK for this level so that, if it is not expanded
9023 later, there is a way to turn off the entire subtree of blocks that
9024 are contained in it. */
9025 keep_next_level ();
9026 ret = c_begin_compound_stmt (true);
9028 c_bindings_start_stmt_expr (c_switch_stack == NULL
9029 ? NULL
9030 : c_switch_stack->bindings);
9032 /* Mark the current statement list as belonging to a statement list. */
9033 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9035 return ret;
9038 /* LOC is the location of the compound statement to which this body
9039 belongs. */
9041 tree
9042 c_finish_stmt_expr (location_t loc, tree body)
9044 tree last, type, tmp, val;
9045 tree *last_p;
9047 body = c_end_compound_stmt (loc, body, true);
9049 c_bindings_end_stmt_expr (c_switch_stack == NULL
9050 ? NULL
9051 : c_switch_stack->bindings);
9053 /* Locate the last statement in BODY. See c_end_compound_stmt
9054 about always returning a BIND_EXPR. */
9055 last_p = &BIND_EXPR_BODY (body);
9056 last = BIND_EXPR_BODY (body);
9058 continue_searching:
9059 if (TREE_CODE (last) == STATEMENT_LIST)
9061 tree_stmt_iterator i;
9063 /* This can happen with degenerate cases like ({ }). No value. */
9064 if (!TREE_SIDE_EFFECTS (last))
9065 return body;
9067 /* If we're supposed to generate side effects warnings, process
9068 all of the statements except the last. */
9069 if (warn_unused_value)
9071 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9073 location_t tloc;
9074 tree t = tsi_stmt (i);
9076 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9077 emit_side_effect_warnings (tloc, t);
9080 else
9081 i = tsi_last (last);
9082 last_p = tsi_stmt_ptr (i);
9083 last = *last_p;
9086 /* If the end of the list is exception related, then the list was split
9087 by a call to push_cleanup. Continue searching. */
9088 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9089 || TREE_CODE (last) == TRY_CATCH_EXPR)
9091 last_p = &TREE_OPERAND (last, 0);
9092 last = *last_p;
9093 goto continue_searching;
9096 if (last == error_mark_node)
9097 return last;
9099 /* In the case that the BIND_EXPR is not necessary, return the
9100 expression out from inside it. */
9101 if (last == BIND_EXPR_BODY (body)
9102 && BIND_EXPR_VARS (body) == NULL)
9104 /* Even if this looks constant, do not allow it in a constant
9105 expression. */
9106 last = c_wrap_maybe_const (last, true);
9107 /* Do not warn if the return value of a statement expression is
9108 unused. */
9109 TREE_NO_WARNING (last) = 1;
9110 return last;
9113 /* Extract the type of said expression. */
9114 type = TREE_TYPE (last);
9116 /* If we're not returning a value at all, then the BIND_EXPR that
9117 we already have is a fine expression to return. */
9118 if (!type || VOID_TYPE_P (type))
9119 return body;
9121 /* Now that we've located the expression containing the value, it seems
9122 silly to make voidify_wrapper_expr repeat the process. Create a
9123 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9124 tmp = create_tmp_var_raw (type, NULL);
9126 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9127 tree_expr_nonnegative_p giving up immediately. */
9128 val = last;
9129 if (TREE_CODE (val) == NOP_EXPR
9130 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9131 val = TREE_OPERAND (val, 0);
9133 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9134 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9137 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9138 SET_EXPR_LOCATION (t, loc);
9139 return t;
9143 /* Begin and end compound statements. This is as simple as pushing
9144 and popping new statement lists from the tree. */
9146 tree
9147 c_begin_compound_stmt (bool do_scope)
9149 tree stmt = push_stmt_list ();
9150 if (do_scope)
9151 push_scope ();
9152 return stmt;
9155 /* End a compound statement. STMT is the statement. LOC is the
9156 location of the compound statement-- this is usually the location
9157 of the opening brace. */
9159 tree
9160 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9162 tree block = NULL;
9164 if (do_scope)
9166 if (c_dialect_objc ())
9167 objc_clear_super_receiver ();
9168 block = pop_scope ();
9171 stmt = pop_stmt_list (stmt);
9172 stmt = c_build_bind_expr (loc, block, stmt);
9174 /* If this compound statement is nested immediately inside a statement
9175 expression, then force a BIND_EXPR to be created. Otherwise we'll
9176 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9177 STATEMENT_LISTs merge, and thus we can lose track of what statement
9178 was really last. */
9179 if (cur_stmt_list
9180 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9181 && TREE_CODE (stmt) != BIND_EXPR)
9183 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9184 TREE_SIDE_EFFECTS (stmt) = 1;
9185 SET_EXPR_LOCATION (stmt, loc);
9188 return stmt;
9191 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
9192 when the current scope is exited. EH_ONLY is true when this is not
9193 meant to apply to normal control flow transfer. */
9195 void
9196 push_cleanup (tree decl, tree cleanup, bool eh_only)
9198 enum tree_code code;
9199 tree stmt, list;
9200 bool stmt_expr;
9202 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9203 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9204 add_stmt (stmt);
9205 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9206 list = push_stmt_list ();
9207 TREE_OPERAND (stmt, 0) = list;
9208 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9211 /* Build a binary-operation expression without default conversions.
9212 CODE is the kind of expression to build.
9213 LOCATION is the operator's location.
9214 This function differs from `build' in several ways:
9215 the data type of the result is computed and recorded in it,
9216 warnings are generated if arg data types are invalid,
9217 special handling for addition and subtraction of pointers is known,
9218 and some optimization is done (operations on narrow ints
9219 are done in the narrower type when that gives the same result).
9220 Constant folding is also done before the result is returned.
9222 Note that the operands will never have enumeral types, or function
9223 or array types, because either they will have the default conversions
9224 performed or they have both just been converted to some other type in which
9225 the arithmetic is to be done. */
9227 tree
9228 build_binary_op (location_t location, enum tree_code code,
9229 tree orig_op0, tree orig_op1, int convert_p)
9231 tree type0, type1, orig_type0, orig_type1;
9232 tree eptype;
9233 enum tree_code code0, code1;
9234 tree op0, op1;
9235 tree ret = error_mark_node;
9236 const char *invalid_op_diag;
9237 bool op0_int_operands, op1_int_operands;
9238 bool int_const, int_const_or_overflow, int_operands;
9240 /* Expression code to give to the expression when it is built.
9241 Normally this is CODE, which is what the caller asked for,
9242 but in some special cases we change it. */
9243 enum tree_code resultcode = code;
9245 /* Data type in which the computation is to be performed.
9246 In the simplest cases this is the common type of the arguments. */
9247 tree result_type = NULL;
9249 /* When the computation is in excess precision, the type of the
9250 final EXCESS_PRECISION_EXPR. */
9251 tree semantic_result_type = NULL;
9253 /* Nonzero means operands have already been type-converted
9254 in whatever way is necessary.
9255 Zero means they need to be converted to RESULT_TYPE. */
9256 int converted = 0;
9258 /* Nonzero means create the expression with this type, rather than
9259 RESULT_TYPE. */
9260 tree build_type = 0;
9262 /* Nonzero means after finally constructing the expression
9263 convert it to this type. */
9264 tree final_type = 0;
9266 /* Nonzero if this is an operation like MIN or MAX which can
9267 safely be computed in short if both args are promoted shorts.
9268 Also implies COMMON.
9269 -1 indicates a bitwise operation; this makes a difference
9270 in the exact conditions for when it is safe to do the operation
9271 in a narrower mode. */
9272 int shorten = 0;
9274 /* Nonzero if this is a comparison operation;
9275 if both args are promoted shorts, compare the original shorts.
9276 Also implies COMMON. */
9277 int short_compare = 0;
9279 /* Nonzero if this is a right-shift operation, which can be computed on the
9280 original short and then promoted if the operand is a promoted short. */
9281 int short_shift = 0;
9283 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9284 int common = 0;
9286 /* True means types are compatible as far as ObjC is concerned. */
9287 bool objc_ok;
9289 /* True means this is an arithmetic operation that may need excess
9290 precision. */
9291 bool may_need_excess_precision;
9293 if (location == UNKNOWN_LOCATION)
9294 location = input_location;
9296 op0 = orig_op0;
9297 op1 = orig_op1;
9299 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9300 if (op0_int_operands)
9301 op0 = remove_c_maybe_const_expr (op0);
9302 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9303 if (op1_int_operands)
9304 op1 = remove_c_maybe_const_expr (op1);
9305 int_operands = (op0_int_operands && op1_int_operands);
9306 if (int_operands)
9308 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9309 && TREE_CODE (orig_op1) == INTEGER_CST);
9310 int_const = (int_const_or_overflow
9311 && !TREE_OVERFLOW (orig_op0)
9312 && !TREE_OVERFLOW (orig_op1));
9314 else
9315 int_const = int_const_or_overflow = false;
9317 if (convert_p)
9319 op0 = default_conversion (op0);
9320 op1 = default_conversion (op1);
9323 orig_type0 = type0 = TREE_TYPE (op0);
9324 orig_type1 = type1 = TREE_TYPE (op1);
9326 /* The expression codes of the data types of the arguments tell us
9327 whether the arguments are integers, floating, pointers, etc. */
9328 code0 = TREE_CODE (type0);
9329 code1 = TREE_CODE (type1);
9331 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9332 STRIP_TYPE_NOPS (op0);
9333 STRIP_TYPE_NOPS (op1);
9335 /* If an error was already reported for one of the arguments,
9336 avoid reporting another error. */
9338 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9339 return error_mark_node;
9341 if ((invalid_op_diag
9342 = targetm.invalid_binary_op (code, type0, type1)))
9344 error_at (location, invalid_op_diag);
9345 return error_mark_node;
9348 switch (code)
9350 case PLUS_EXPR:
9351 case MINUS_EXPR:
9352 case MULT_EXPR:
9353 case TRUNC_DIV_EXPR:
9354 case CEIL_DIV_EXPR:
9355 case FLOOR_DIV_EXPR:
9356 case ROUND_DIV_EXPR:
9357 case EXACT_DIV_EXPR:
9358 may_need_excess_precision = true;
9359 break;
9360 default:
9361 may_need_excess_precision = false;
9362 break;
9364 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9366 op0 = TREE_OPERAND (op0, 0);
9367 type0 = TREE_TYPE (op0);
9369 else if (may_need_excess_precision
9370 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9372 type0 = eptype;
9373 op0 = convert (eptype, op0);
9375 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9377 op1 = TREE_OPERAND (op1, 0);
9378 type1 = TREE_TYPE (op1);
9380 else if (may_need_excess_precision
9381 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9383 type1 = eptype;
9384 op1 = convert (eptype, op1);
9387 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9389 switch (code)
9391 case PLUS_EXPR:
9392 /* Handle the pointer + int case. */
9393 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9395 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9396 goto return_build_binary_op;
9398 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9400 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9401 goto return_build_binary_op;
9403 else
9404 common = 1;
9405 break;
9407 case MINUS_EXPR:
9408 /* Subtraction of two similar pointers.
9409 We must subtract them as integers, then divide by object size. */
9410 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9411 && comp_target_types (location, type0, type1))
9413 ret = pointer_diff (location, op0, op1);
9414 goto return_build_binary_op;
9416 /* Handle pointer minus int. Just like pointer plus int. */
9417 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9419 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9420 goto return_build_binary_op;
9422 else
9423 common = 1;
9424 break;
9426 case MULT_EXPR:
9427 common = 1;
9428 break;
9430 case TRUNC_DIV_EXPR:
9431 case CEIL_DIV_EXPR:
9432 case FLOOR_DIV_EXPR:
9433 case ROUND_DIV_EXPR:
9434 case EXACT_DIV_EXPR:
9435 warn_for_div_by_zero (location, op1);
9437 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9438 || code0 == FIXED_POINT_TYPE
9439 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9440 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9441 || code1 == FIXED_POINT_TYPE
9442 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9444 enum tree_code tcode0 = code0, tcode1 = code1;
9446 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9447 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9448 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9449 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9451 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9452 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9453 resultcode = RDIV_EXPR;
9454 else
9455 /* Although it would be tempting to shorten always here, that
9456 loses on some targets, since the modulo instruction is
9457 undefined if the quotient can't be represented in the
9458 computation mode. We shorten only if unsigned or if
9459 dividing by something we know != -1. */
9460 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9461 || (TREE_CODE (op1) == INTEGER_CST
9462 && !integer_all_onesp (op1)));
9463 common = 1;
9465 break;
9467 case BIT_AND_EXPR:
9468 case BIT_IOR_EXPR:
9469 case BIT_XOR_EXPR:
9470 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9471 shorten = -1;
9472 /* Allow vector types which are not floating point types. */
9473 else if (code0 == VECTOR_TYPE
9474 && code1 == VECTOR_TYPE
9475 && !VECTOR_FLOAT_TYPE_P (type0)
9476 && !VECTOR_FLOAT_TYPE_P (type1))
9477 common = 1;
9478 break;
9480 case TRUNC_MOD_EXPR:
9481 case FLOOR_MOD_EXPR:
9482 warn_for_div_by_zero (location, op1);
9484 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9485 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9486 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9487 common = 1;
9488 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9490 /* Although it would be tempting to shorten always here, that loses
9491 on some targets, since the modulo instruction is undefined if the
9492 quotient can't be represented in the computation mode. We shorten
9493 only if unsigned or if dividing by something we know != -1. */
9494 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9495 || (TREE_CODE (op1) == INTEGER_CST
9496 && !integer_all_onesp (op1)));
9497 common = 1;
9499 break;
9501 case TRUTH_ANDIF_EXPR:
9502 case TRUTH_ORIF_EXPR:
9503 case TRUTH_AND_EXPR:
9504 case TRUTH_OR_EXPR:
9505 case TRUTH_XOR_EXPR:
9506 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9507 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9508 || code0 == FIXED_POINT_TYPE)
9509 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9510 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9511 || code1 == FIXED_POINT_TYPE))
9513 /* Result of these operations is always an int,
9514 but that does not mean the operands should be
9515 converted to ints! */
9516 result_type = integer_type_node;
9517 op0 = c_common_truthvalue_conversion (location, op0);
9518 op1 = c_common_truthvalue_conversion (location, op1);
9519 converted = 1;
9521 if (code == TRUTH_ANDIF_EXPR)
9523 int_const_or_overflow = (int_operands
9524 && TREE_CODE (orig_op0) == INTEGER_CST
9525 && (op0 == truthvalue_false_node
9526 || TREE_CODE (orig_op1) == INTEGER_CST));
9527 int_const = (int_const_or_overflow
9528 && !TREE_OVERFLOW (orig_op0)
9529 && (op0 == truthvalue_false_node
9530 || !TREE_OVERFLOW (orig_op1)));
9532 else if (code == TRUTH_ORIF_EXPR)
9534 int_const_or_overflow = (int_operands
9535 && TREE_CODE (orig_op0) == INTEGER_CST
9536 && (op0 == truthvalue_true_node
9537 || TREE_CODE (orig_op1) == INTEGER_CST));
9538 int_const = (int_const_or_overflow
9539 && !TREE_OVERFLOW (orig_op0)
9540 && (op0 == truthvalue_true_node
9541 || !TREE_OVERFLOW (orig_op1)));
9543 break;
9545 /* Shift operations: result has same type as first operand;
9546 always convert second operand to int.
9547 Also set SHORT_SHIFT if shifting rightward. */
9549 case RSHIFT_EXPR:
9550 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9551 && code1 == INTEGER_TYPE)
9553 if (TREE_CODE (op1) == INTEGER_CST)
9555 if (tree_int_cst_sgn (op1) < 0)
9557 int_const = false;
9558 if (c_inhibit_evaluation_warnings == 0)
9559 warning (0, "right shift count is negative");
9561 else
9563 if (!integer_zerop (op1))
9564 short_shift = 1;
9566 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9568 int_const = false;
9569 if (c_inhibit_evaluation_warnings == 0)
9570 warning (0, "right shift count >= width of type");
9575 /* Use the type of the value to be shifted. */
9576 result_type = type0;
9577 /* Convert the shift-count to an integer, regardless of size
9578 of value being shifted. */
9579 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9580 op1 = convert (integer_type_node, op1);
9581 /* Avoid converting op1 to result_type later. */
9582 converted = 1;
9584 break;
9586 case LSHIFT_EXPR:
9587 if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9588 && code1 == INTEGER_TYPE)
9590 if (TREE_CODE (op1) == INTEGER_CST)
9592 if (tree_int_cst_sgn (op1) < 0)
9594 int_const = false;
9595 if (c_inhibit_evaluation_warnings == 0)
9596 warning (0, "left shift count is negative");
9599 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9601 int_const = false;
9602 if (c_inhibit_evaluation_warnings == 0)
9603 warning (0, "left shift count >= width of type");
9607 /* Use the type of the value to be shifted. */
9608 result_type = type0;
9609 /* Convert the shift-count to an integer, regardless of size
9610 of value being shifted. */
9611 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9612 op1 = convert (integer_type_node, op1);
9613 /* Avoid converting op1 to result_type later. */
9614 converted = 1;
9616 break;
9618 case EQ_EXPR:
9619 case NE_EXPR:
9620 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9621 warning_at (location,
9622 OPT_Wfloat_equal,
9623 "comparing floating point with == or != is unsafe");
9624 /* Result of comparison is always int,
9625 but don't convert the args to int! */
9626 build_type = integer_type_node;
9627 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9628 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9629 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9630 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9631 short_compare = 1;
9632 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9634 if (TREE_CODE (op0) == ADDR_EXPR
9635 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9637 if (code == EQ_EXPR)
9638 warning_at (location,
9639 OPT_Waddress,
9640 "the comparison will always evaluate as %<false%> "
9641 "for the address of %qD will never be NULL",
9642 TREE_OPERAND (op0, 0));
9643 else
9644 warning_at (location,
9645 OPT_Waddress,
9646 "the comparison will always evaluate as %<true%> "
9647 "for the address of %qD will never be NULL",
9648 TREE_OPERAND (op0, 0));
9650 result_type = type0;
9652 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9654 if (TREE_CODE (op1) == ADDR_EXPR
9655 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9657 if (code == EQ_EXPR)
9658 warning_at (location,
9659 OPT_Waddress,
9660 "the comparison will always evaluate as %<false%> "
9661 "for the address of %qD will never be NULL",
9662 TREE_OPERAND (op1, 0));
9663 else
9664 warning_at (location,
9665 OPT_Waddress,
9666 "the comparison will always evaluate as %<true%> "
9667 "for the address of %qD will never be NULL",
9668 TREE_OPERAND (op1, 0));
9670 result_type = type1;
9672 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9674 tree tt0 = TREE_TYPE (type0);
9675 tree tt1 = TREE_TYPE (type1);
9676 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
9677 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
9678 addr_space_t as_common = ADDR_SPACE_GENERIC;
9680 /* Anything compares with void *. void * compares with anything.
9681 Otherwise, the targets must be compatible
9682 and both must be object or both incomplete. */
9683 if (comp_target_types (location, type0, type1))
9684 result_type = common_pointer_type (type0, type1);
9685 else if (!addr_space_superset (as0, as1, &as_common))
9687 error_at (location, "comparison of pointers to "
9688 "disjoint address spaces");
9689 return error_mark_node;
9691 else if (VOID_TYPE_P (tt0))
9693 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
9694 pedwarn (location, OPT_pedantic, "ISO C forbids "
9695 "comparison of %<void *%> with function pointer");
9697 else if (VOID_TYPE_P (tt1))
9699 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
9700 pedwarn (location, OPT_pedantic, "ISO C forbids "
9701 "comparison of %<void *%> with function pointer");
9703 else
9704 /* Avoid warning about the volatile ObjC EH puts on decls. */
9705 if (!objc_ok)
9706 pedwarn (location, 0,
9707 "comparison of distinct pointer types lacks a cast");
9709 if (result_type == NULL_TREE)
9711 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9712 result_type = build_pointer_type
9713 (build_qualified_type (void_type_node, qual));
9716 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9718 result_type = type0;
9719 pedwarn (location, 0, "comparison between pointer and integer");
9721 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9723 result_type = type1;
9724 pedwarn (location, 0, "comparison between pointer and integer");
9726 break;
9728 case LE_EXPR:
9729 case GE_EXPR:
9730 case LT_EXPR:
9731 case GT_EXPR:
9732 build_type = integer_type_node;
9733 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9734 || code0 == FIXED_POINT_TYPE)
9735 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9736 || code1 == FIXED_POINT_TYPE))
9737 short_compare = 1;
9738 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9740 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
9741 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
9742 addr_space_t as_common;
9744 if (comp_target_types (location, type0, type1))
9746 result_type = common_pointer_type (type0, type1);
9747 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9748 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9749 pedwarn (location, 0,
9750 "comparison of complete and incomplete pointers");
9751 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9752 pedwarn (location, OPT_pedantic, "ISO C forbids "
9753 "ordered comparisons of pointers to functions");
9754 else if (null_pointer_constant_p (orig_op0)
9755 || null_pointer_constant_p (orig_op1))
9756 warning_at (location, OPT_Wextra,
9757 "ordered comparison of pointer with null pointer");
9760 else if (!addr_space_superset (as0, as1, &as_common))
9762 error_at (location, "comparison of pointers to "
9763 "disjoint address spaces");
9764 return error_mark_node;
9766 else
9768 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9769 result_type = build_pointer_type
9770 (build_qualified_type (void_type_node, qual));
9771 pedwarn (location, 0,
9772 "comparison of distinct pointer types lacks a cast");
9775 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9777 result_type = type0;
9778 if (pedantic)
9779 pedwarn (location, OPT_pedantic,
9780 "ordered comparison of pointer with integer zero");
9781 else if (extra_warnings)
9782 warning_at (location, OPT_Wextra,
9783 "ordered comparison of pointer with integer zero");
9785 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9787 result_type = type1;
9788 if (pedantic)
9789 pedwarn (location, OPT_pedantic,
9790 "ordered comparison of pointer with integer zero");
9791 else if (extra_warnings)
9792 warning_at (location, OPT_Wextra,
9793 "ordered comparison of pointer with integer zero");
9795 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9797 result_type = type0;
9798 pedwarn (location, 0, "comparison between pointer and integer");
9800 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9802 result_type = type1;
9803 pedwarn (location, 0, "comparison between pointer and integer");
9805 break;
9807 default:
9808 gcc_unreachable ();
9811 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9812 return error_mark_node;
9814 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9815 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9816 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9817 TREE_TYPE (type1))))
9819 binary_op_error (location, code, type0, type1);
9820 return error_mark_node;
9823 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9824 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9826 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9827 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9829 bool first_complex = (code0 == COMPLEX_TYPE);
9830 bool second_complex = (code1 == COMPLEX_TYPE);
9831 int none_complex = (!first_complex && !second_complex);
9833 if (shorten || common || short_compare)
9835 result_type = c_common_type (type0, type1);
9836 do_warn_double_promotion (result_type, type0, type1,
9837 "implicit conversion from %qT to %qT "
9838 "to match other operand of binary "
9839 "expression",
9840 location);
9841 if (result_type == error_mark_node)
9842 return error_mark_node;
9845 if (first_complex != second_complex
9846 && (code == PLUS_EXPR
9847 || code == MINUS_EXPR
9848 || code == MULT_EXPR
9849 || (code == TRUNC_DIV_EXPR && first_complex))
9850 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
9851 && flag_signed_zeros)
9853 /* An operation on mixed real/complex operands must be
9854 handled specially, but the language-independent code can
9855 more easily optimize the plain complex arithmetic if
9856 -fno-signed-zeros. */
9857 tree real_type = TREE_TYPE (result_type);
9858 tree real, imag;
9859 if (type0 != orig_type0 || type1 != orig_type1)
9861 gcc_assert (may_need_excess_precision && common);
9862 semantic_result_type = c_common_type (orig_type0, orig_type1);
9864 if (first_complex)
9866 if (TREE_TYPE (op0) != result_type)
9867 op0 = convert_and_check (result_type, op0);
9868 if (TREE_TYPE (op1) != real_type)
9869 op1 = convert_and_check (real_type, op1);
9871 else
9873 if (TREE_TYPE (op0) != real_type)
9874 op0 = convert_and_check (real_type, op0);
9875 if (TREE_TYPE (op1) != result_type)
9876 op1 = convert_and_check (result_type, op1);
9878 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9879 return error_mark_node;
9880 if (first_complex)
9882 op0 = c_save_expr (op0);
9883 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
9884 op0, 1);
9885 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
9886 op0, 1);
9887 switch (code)
9889 case MULT_EXPR:
9890 case TRUNC_DIV_EXPR:
9891 imag = build2 (resultcode, real_type, imag, op1);
9892 /* Fall through. */
9893 case PLUS_EXPR:
9894 case MINUS_EXPR:
9895 real = build2 (resultcode, real_type, real, op1);
9896 break;
9897 default:
9898 gcc_unreachable();
9901 else
9903 op1 = c_save_expr (op1);
9904 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
9905 op1, 1);
9906 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
9907 op1, 1);
9908 switch (code)
9910 case MULT_EXPR:
9911 imag = build2 (resultcode, real_type, op0, imag);
9912 /* Fall through. */
9913 case PLUS_EXPR:
9914 real = build2 (resultcode, real_type, op0, real);
9915 break;
9916 case MINUS_EXPR:
9917 real = build2 (resultcode, real_type, op0, real);
9918 imag = build1 (NEGATE_EXPR, real_type, imag);
9919 break;
9920 default:
9921 gcc_unreachable();
9924 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
9925 goto return_build_binary_op;
9928 /* For certain operations (which identify themselves by shorten != 0)
9929 if both args were extended from the same smaller type,
9930 do the arithmetic in that type and then extend.
9932 shorten !=0 and !=1 indicates a bitwise operation.
9933 For them, this optimization is safe only if
9934 both args are zero-extended or both are sign-extended.
9935 Otherwise, we might change the result.
9936 Eg, (short)-1 | (unsigned short)-1 is (int)-1
9937 but calculated in (unsigned short) it would be (unsigned short)-1. */
9939 if (shorten && none_complex)
9941 final_type = result_type;
9942 result_type = shorten_binary_op (result_type, op0, op1,
9943 shorten == -1);
9946 /* Shifts can be shortened if shifting right. */
9948 if (short_shift)
9950 int unsigned_arg;
9951 tree arg0 = get_narrower (op0, &unsigned_arg);
9953 final_type = result_type;
9955 if (arg0 == op0 && final_type == TREE_TYPE (op0))
9956 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
9958 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9959 && tree_int_cst_sgn (op1) > 0
9960 /* We can shorten only if the shift count is less than the
9961 number of bits in the smaller type size. */
9962 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
9963 /* We cannot drop an unsigned shift after sign-extension. */
9964 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
9966 /* Do an unsigned shift if the operand was zero-extended. */
9967 result_type
9968 = c_common_signed_or_unsigned_type (unsigned_arg,
9969 TREE_TYPE (arg0));
9970 /* Convert value-to-be-shifted to that type. */
9971 if (TREE_TYPE (op0) != result_type)
9972 op0 = convert (result_type, op0);
9973 converted = 1;
9977 /* Comparison operations are shortened too but differently.
9978 They identify themselves by setting short_compare = 1. */
9980 if (short_compare)
9982 /* Don't write &op0, etc., because that would prevent op0
9983 from being kept in a register.
9984 Instead, make copies of the our local variables and
9985 pass the copies by reference, then copy them back afterward. */
9986 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
9987 enum tree_code xresultcode = resultcode;
9988 tree val
9989 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
9991 if (val != 0)
9993 ret = val;
9994 goto return_build_binary_op;
9997 op0 = xop0, op1 = xop1;
9998 converted = 1;
9999 resultcode = xresultcode;
10001 if (c_inhibit_evaluation_warnings == 0)
10003 bool op0_maybe_const = true;
10004 bool op1_maybe_const = true;
10005 tree orig_op0_folded, orig_op1_folded;
10007 if (in_late_binary_op)
10009 orig_op0_folded = orig_op0;
10010 orig_op1_folded = orig_op1;
10012 else
10014 /* Fold for the sake of possible warnings, as in
10015 build_conditional_expr. This requires the
10016 "original" values to be folded, not just op0 and
10017 op1. */
10018 c_inhibit_evaluation_warnings++;
10019 op0 = c_fully_fold (op0, require_constant_value,
10020 &op0_maybe_const);
10021 op1 = c_fully_fold (op1, require_constant_value,
10022 &op1_maybe_const);
10023 c_inhibit_evaluation_warnings--;
10024 orig_op0_folded = c_fully_fold (orig_op0,
10025 require_constant_value,
10026 NULL);
10027 orig_op1_folded = c_fully_fold (orig_op1,
10028 require_constant_value,
10029 NULL);
10032 if (warn_sign_compare)
10033 warn_for_sign_compare (location, orig_op0_folded,
10034 orig_op1_folded, op0, op1,
10035 result_type, resultcode);
10036 if (!in_late_binary_op)
10038 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10039 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10040 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10041 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10047 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10048 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10049 Then the expression will be built.
10050 It will be given type FINAL_TYPE if that is nonzero;
10051 otherwise, it will be given type RESULT_TYPE. */
10053 if (!result_type)
10055 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10056 return error_mark_node;
10059 if (build_type == NULL_TREE)
10061 build_type = result_type;
10062 if (type0 != orig_type0 || type1 != orig_type1)
10064 gcc_assert (may_need_excess_precision && common);
10065 semantic_result_type = c_common_type (orig_type0, orig_type1);
10069 if (!converted)
10071 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10072 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10074 /* This can happen if one operand has a vector type, and the other
10075 has a different type. */
10076 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10077 return error_mark_node;
10080 /* Treat expressions in initializers specially as they can't trap. */
10081 if (int_const_or_overflow)
10082 ret = (require_constant_value
10083 ? fold_build2_initializer_loc (location, resultcode, build_type,
10084 op0, op1)
10085 : fold_build2_loc (location, resultcode, build_type, op0, op1));
10086 else
10087 ret = build2 (resultcode, build_type, op0, op1);
10088 if (final_type != 0)
10089 ret = convert (final_type, ret);
10091 return_build_binary_op:
10092 gcc_assert (ret != error_mark_node);
10093 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10094 ret = (int_operands
10095 ? note_integer_operands (ret)
10096 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10097 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10098 && !in_late_binary_op)
10099 ret = note_integer_operands (ret);
10100 if (semantic_result_type)
10101 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10102 protected_set_expr_location (ret, location);
10103 return ret;
10107 /* Convert EXPR to be a truth-value, validating its type for this
10108 purpose. LOCATION is the source location for the expression. */
10110 tree
10111 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10113 bool int_const, int_operands;
10115 switch (TREE_CODE (TREE_TYPE (expr)))
10117 case ARRAY_TYPE:
10118 error_at (location, "used array that cannot be converted to pointer where scalar is required");
10119 return error_mark_node;
10121 case RECORD_TYPE:
10122 error_at (location, "used struct type value where scalar is required");
10123 return error_mark_node;
10125 case UNION_TYPE:
10126 error_at (location, "used union type value where scalar is required");
10127 return error_mark_node;
10129 case FUNCTION_TYPE:
10130 gcc_unreachable ();
10132 default:
10133 break;
10136 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10137 int_operands = EXPR_INT_CONST_OPERANDS (expr);
10138 if (int_operands)
10139 expr = remove_c_maybe_const_expr (expr);
10141 /* ??? Should we also give an error for void and vectors rather than
10142 leaving those to give errors later? */
10143 expr = c_common_truthvalue_conversion (location, expr);
10145 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10147 if (TREE_OVERFLOW (expr))
10148 return expr;
10149 else
10150 return note_integer_operands (expr);
10152 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10153 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10154 return expr;
10158 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10159 required. */
10161 tree
10162 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10164 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10166 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10167 /* Executing a compound literal inside a function reinitializes
10168 it. */
10169 if (!TREE_STATIC (decl))
10170 *se = true;
10171 return decl;
10173 else
10174 return expr;
10177 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10179 tree
10180 c_begin_omp_parallel (void)
10182 tree block;
10184 keep_next_level ();
10185 block = c_begin_compound_stmt (true);
10187 return block;
10190 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10191 statement. LOC is the location of the OMP_PARALLEL. */
10193 tree
10194 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10196 tree stmt;
10198 block = c_end_compound_stmt (loc, block, true);
10200 stmt = make_node (OMP_PARALLEL);
10201 TREE_TYPE (stmt) = void_type_node;
10202 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10203 OMP_PARALLEL_BODY (stmt) = block;
10204 SET_EXPR_LOCATION (stmt, loc);
10206 return add_stmt (stmt);
10209 /* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10211 tree
10212 c_begin_omp_task (void)
10214 tree block;
10216 keep_next_level ();
10217 block = c_begin_compound_stmt (true);
10219 return block;
10222 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10223 statement. LOC is the location of the #pragma. */
10225 tree
10226 c_finish_omp_task (location_t loc, tree clauses, tree block)
10228 tree stmt;
10230 block = c_end_compound_stmt (loc, block, true);
10232 stmt = make_node (OMP_TASK);
10233 TREE_TYPE (stmt) = void_type_node;
10234 OMP_TASK_CLAUSES (stmt) = clauses;
10235 OMP_TASK_BODY (stmt) = block;
10236 SET_EXPR_LOCATION (stmt, loc);
10238 return add_stmt (stmt);
10241 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10242 Remove any elements from the list that are invalid. */
10244 tree
10245 c_finish_omp_clauses (tree clauses)
10247 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10248 tree c, t, *pc = &clauses;
10249 const char *name;
10251 bitmap_obstack_initialize (NULL);
10252 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10253 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10254 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10256 for (pc = &clauses, c = clauses; c ; c = *pc)
10258 bool remove = false;
10259 bool need_complete = false;
10260 bool need_implicitly_determined = false;
10262 switch (OMP_CLAUSE_CODE (c))
10264 case OMP_CLAUSE_SHARED:
10265 name = "shared";
10266 need_implicitly_determined = true;
10267 goto check_dup_generic;
10269 case OMP_CLAUSE_PRIVATE:
10270 name = "private";
10271 need_complete = true;
10272 need_implicitly_determined = true;
10273 goto check_dup_generic;
10275 case OMP_CLAUSE_REDUCTION:
10276 name = "reduction";
10277 need_implicitly_determined = true;
10278 t = OMP_CLAUSE_DECL (c);
10279 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10280 || POINTER_TYPE_P (TREE_TYPE (t)))
10282 error_at (OMP_CLAUSE_LOCATION (c),
10283 "%qE has invalid type for %<reduction%>", t);
10284 remove = true;
10286 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10288 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10289 const char *r_name = NULL;
10291 switch (r_code)
10293 case PLUS_EXPR:
10294 case MULT_EXPR:
10295 case MINUS_EXPR:
10296 break;
10297 case BIT_AND_EXPR:
10298 r_name = "&";
10299 break;
10300 case BIT_XOR_EXPR:
10301 r_name = "^";
10302 break;
10303 case BIT_IOR_EXPR:
10304 r_name = "|";
10305 break;
10306 case TRUTH_ANDIF_EXPR:
10307 r_name = "&&";
10308 break;
10309 case TRUTH_ORIF_EXPR:
10310 r_name = "||";
10311 break;
10312 default:
10313 gcc_unreachable ();
10315 if (r_name)
10317 error_at (OMP_CLAUSE_LOCATION (c),
10318 "%qE has invalid type for %<reduction(%s)%>",
10319 t, r_name);
10320 remove = true;
10323 goto check_dup_generic;
10325 case OMP_CLAUSE_COPYPRIVATE:
10326 name = "copyprivate";
10327 goto check_dup_generic;
10329 case OMP_CLAUSE_COPYIN:
10330 name = "copyin";
10331 t = OMP_CLAUSE_DECL (c);
10332 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10334 error_at (OMP_CLAUSE_LOCATION (c),
10335 "%qE must be %<threadprivate%> for %<copyin%>", t);
10336 remove = true;
10338 goto check_dup_generic;
10340 check_dup_generic:
10341 t = OMP_CLAUSE_DECL (c);
10342 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10344 error_at (OMP_CLAUSE_LOCATION (c),
10345 "%qE is not a variable in clause %qs", t, name);
10346 remove = true;
10348 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10349 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10350 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10352 error_at (OMP_CLAUSE_LOCATION (c),
10353 "%qE appears more than once in data clauses", t);
10354 remove = true;
10356 else
10357 bitmap_set_bit (&generic_head, DECL_UID (t));
10358 break;
10360 case OMP_CLAUSE_FIRSTPRIVATE:
10361 name = "firstprivate";
10362 t = OMP_CLAUSE_DECL (c);
10363 need_complete = true;
10364 need_implicitly_determined = true;
10365 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10367 error_at (OMP_CLAUSE_LOCATION (c),
10368 "%qE is not a variable in clause %<firstprivate%>", t);
10369 remove = true;
10371 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10372 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10374 error_at (OMP_CLAUSE_LOCATION (c),
10375 "%qE appears more than once in data clauses", t);
10376 remove = true;
10378 else
10379 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10380 break;
10382 case OMP_CLAUSE_LASTPRIVATE:
10383 name = "lastprivate";
10384 t = OMP_CLAUSE_DECL (c);
10385 need_complete = true;
10386 need_implicitly_determined = true;
10387 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10389 error_at (OMP_CLAUSE_LOCATION (c),
10390 "%qE is not a variable in clause %<lastprivate%>", t);
10391 remove = true;
10393 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10394 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10396 error_at (OMP_CLAUSE_LOCATION (c),
10397 "%qE appears more than once in data clauses", t);
10398 remove = true;
10400 else
10401 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10402 break;
10404 case OMP_CLAUSE_IF:
10405 case OMP_CLAUSE_NUM_THREADS:
10406 case OMP_CLAUSE_SCHEDULE:
10407 case OMP_CLAUSE_NOWAIT:
10408 case OMP_CLAUSE_ORDERED:
10409 case OMP_CLAUSE_DEFAULT:
10410 case OMP_CLAUSE_UNTIED:
10411 case OMP_CLAUSE_COLLAPSE:
10412 pc = &OMP_CLAUSE_CHAIN (c);
10413 continue;
10415 default:
10416 gcc_unreachable ();
10419 if (!remove)
10421 t = OMP_CLAUSE_DECL (c);
10423 if (need_complete)
10425 t = require_complete_type (t);
10426 if (t == error_mark_node)
10427 remove = true;
10430 if (need_implicitly_determined)
10432 const char *share_name = NULL;
10434 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10435 share_name = "threadprivate";
10436 else switch (c_omp_predetermined_sharing (t))
10438 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10439 break;
10440 case OMP_CLAUSE_DEFAULT_SHARED:
10441 share_name = "shared";
10442 break;
10443 case OMP_CLAUSE_DEFAULT_PRIVATE:
10444 share_name = "private";
10445 break;
10446 default:
10447 gcc_unreachable ();
10449 if (share_name)
10451 error_at (OMP_CLAUSE_LOCATION (c),
10452 "%qE is predetermined %qs for %qs",
10453 t, share_name, name);
10454 remove = true;
10459 if (remove)
10460 *pc = OMP_CLAUSE_CHAIN (c);
10461 else
10462 pc = &OMP_CLAUSE_CHAIN (c);
10465 bitmap_obstack_release (NULL);
10466 return clauses;
10469 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10470 down to the element type of an array. */
10472 tree
10473 c_build_qualified_type (tree type, int type_quals)
10475 if (type == error_mark_node)
10476 return type;
10478 if (TREE_CODE (type) == ARRAY_TYPE)
10480 tree t;
10481 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10482 type_quals);
10484 /* See if we already have an identically qualified type. */
10485 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10487 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10488 && TYPE_NAME (t) == TYPE_NAME (type)
10489 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10490 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10491 TYPE_ATTRIBUTES (type)))
10492 break;
10494 if (!t)
10496 tree domain = TYPE_DOMAIN (type);
10498 t = build_variant_type_copy (type);
10499 TREE_TYPE (t) = element_type;
10501 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10502 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10503 SET_TYPE_STRUCTURAL_EQUALITY (t);
10504 else if (TYPE_CANONICAL (element_type) != element_type
10505 || (domain && TYPE_CANONICAL (domain) != domain))
10507 tree unqualified_canon
10508 = build_array_type (TYPE_CANONICAL (element_type),
10509 domain? TYPE_CANONICAL (domain)
10510 : NULL_TREE);
10511 TYPE_CANONICAL (t)
10512 = c_build_qualified_type (unqualified_canon, type_quals);
10514 else
10515 TYPE_CANONICAL (t) = t;
10517 return t;
10520 /* A restrict-qualified pointer type must be a pointer to object or
10521 incomplete type. Note that the use of POINTER_TYPE_P also allows
10522 REFERENCE_TYPEs, which is appropriate for C++. */
10523 if ((type_quals & TYPE_QUAL_RESTRICT)
10524 && (!POINTER_TYPE_P (type)
10525 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10527 error ("invalid use of %<restrict%>");
10528 type_quals &= ~TYPE_QUAL_RESTRICT;
10531 return build_qualified_type (type, type_quals);
10534 /* Build a VA_ARG_EXPR for the C parser. */
10536 tree
10537 c_build_va_arg (location_t loc, tree expr, tree type)
10539 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10540 warning_at (loc, OPT_Wc___compat,
10541 "C++ requires promoted type, not enum type, in %<va_arg%>");
10542 return build_va_arg (loc, expr, type);