re PR c++/21667 (misleading warning about array subscription)
[official-gcc.git] / gcc / c-typeck.c
blob2150238ec6e1abc5c445b5c28d520eb15a2fafa4
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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
23 /* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "langhooks.h"
35 #include "c-tree.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41 #include "intl.h"
42 #include "ggc.h"
43 #include "target.h"
44 #include "tree-iterator.h"
45 #include "tree-gimple.h"
46 #include "tree-flow.h"
48 /* Possible cases of implicit bad conversions. Used to select
49 diagnostic messages in convert_for_assignment. */
50 enum impl_conv {
51 ic_argpass,
52 ic_argpass_nonproto,
53 ic_assign,
54 ic_init,
55 ic_return
58 /* The level of nesting inside "__alignof__". */
59 int in_alignof;
61 /* The level of nesting inside "sizeof". */
62 int in_sizeof;
64 /* The level of nesting inside "typeof". */
65 int in_typeof;
67 struct c_label_context_se *label_context_stack_se;
68 struct c_label_context_vm *label_context_stack_vm;
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 tree qualify_type (tree, tree);
78 static int tagged_types_tu_compatible_p (tree, tree);
79 static int comp_target_types (tree, tree);
80 static int function_types_compatible_p (tree, tree);
81 static int type_lists_compatible_p (tree, tree);
82 static tree decl_constant_value_for_broken_optimization (tree);
83 static tree lookup_field (tree, tree);
84 static tree convert_arguments (tree, tree, tree, tree);
85 static tree pointer_diff (tree, tree);
86 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
87 int);
88 static tree valid_compound_expr_initializer (tree, tree);
89 static void push_string (const char *);
90 static void push_member_name (tree);
91 static void push_array_bounds (int);
92 static int spelling_length (void);
93 static char *print_spelling (char *);
94 static void warning_init (const char *);
95 static tree digest_init (tree, tree, bool, int);
96 static void output_init_element (tree, bool, tree, tree, int);
97 static void output_pending_init_elements (int);
98 static int set_designator (int);
99 static void push_range_stack (tree);
100 static void add_pending_init (tree, tree);
101 static void set_nonincremental_init (void);
102 static void set_nonincremental_init_from_string (tree);
103 static tree find_init_member (tree);
104 static void readonly_error (tree, enum lvalue_use);
105 static int lvalue_or_else (tree, enum lvalue_use);
106 static int lvalue_p (tree);
107 static void record_maybe_used_decl (tree);
108 static int comptypes_internal (tree, tree);
109 \f/* This is a cache to hold if two types are compatible or not. */
111 struct tagged_tu_seen_cache {
112 const struct tagged_tu_seen_cache * next;
113 tree t1;
114 tree t2;
115 /* The return value of tagged_types_tu_compatible_p if we had seen
116 these two types already. */
117 int val;
120 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
121 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
123 /* Do `exp = require_complete_type (exp);' to make sure exp
124 does not have an incomplete type. (That includes void types.) */
126 tree
127 require_complete_type (tree value)
129 tree type = TREE_TYPE (value);
131 if (value == error_mark_node || type == error_mark_node)
132 return error_mark_node;
134 /* First, detect a valid value with a complete type. */
135 if (COMPLETE_TYPE_P (type))
136 return value;
138 c_incomplete_type_error (value, type);
139 return error_mark_node;
142 /* Print an error message for invalid use of an incomplete type.
143 VALUE is the expression that was used (or 0 if that isn't known)
144 and TYPE is the type that was invalid. */
146 void
147 c_incomplete_type_error (tree value, tree type)
149 const char *type_code_string;
151 /* Avoid duplicate error message. */
152 if (TREE_CODE (type) == ERROR_MARK)
153 return;
155 if (value != 0 && (TREE_CODE (value) == VAR_DECL
156 || TREE_CODE (value) == PARM_DECL))
157 error ("%qD has an incomplete type", value);
158 else
160 retry:
161 /* We must print an error message. Be clever about what it says. */
163 switch (TREE_CODE (type))
165 case RECORD_TYPE:
166 type_code_string = "struct";
167 break;
169 case UNION_TYPE:
170 type_code_string = "union";
171 break;
173 case ENUMERAL_TYPE:
174 type_code_string = "enum";
175 break;
177 case VOID_TYPE:
178 error ("invalid use of void expression");
179 return;
181 case ARRAY_TYPE:
182 if (TYPE_DOMAIN (type))
184 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
186 error ("invalid use of flexible array member");
187 return;
189 type = TREE_TYPE (type);
190 goto retry;
192 error ("invalid use of array with unspecified bounds");
193 return;
195 default:
196 gcc_unreachable ();
199 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
200 error ("invalid use of undefined type %<%s %E%>",
201 type_code_string, TYPE_NAME (type));
202 else
203 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
204 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
208 /* Given a type, apply default promotions wrt unnamed function
209 arguments and return the new type. */
211 tree
212 c_type_promotes_to (tree type)
214 if (TYPE_MAIN_VARIANT (type) == float_type_node)
215 return double_type_node;
217 if (c_promoting_integer_type_p (type))
219 /* Preserve unsignedness if not really getting any wider. */
220 if (TYPE_UNSIGNED (type)
221 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
222 return unsigned_type_node;
223 return integer_type_node;
226 return type;
229 /* Return a variant of TYPE which has all the type qualifiers of LIKE
230 as well as those of TYPE. */
232 static tree
233 qualify_type (tree type, tree like)
235 return c_build_qualified_type (type,
236 TYPE_QUALS (type) | TYPE_QUALS (like));
239 /* Return the composite type of two compatible types.
241 We assume that comptypes has already been done and returned
242 nonzero; if that isn't so, this may crash. In particular, we
243 assume that qualifiers match. */
245 tree
246 composite_type (tree t1, tree t2)
248 enum tree_code code1;
249 enum tree_code code2;
250 tree attributes;
252 /* Save time if the two types are the same. */
254 if (t1 == t2) return t1;
256 /* If one type is nonsense, use the other. */
257 if (t1 == error_mark_node)
258 return t2;
259 if (t2 == error_mark_node)
260 return t1;
262 code1 = TREE_CODE (t1);
263 code2 = TREE_CODE (t2);
265 /* Merge the attributes. */
266 attributes = targetm.merge_type_attributes (t1, t2);
268 /* If one is an enumerated type and the other is the compatible
269 integer type, the composite type might be either of the two
270 (DR#013 question 3). For consistency, use the enumerated type as
271 the composite type. */
273 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
274 return t1;
275 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
276 return t2;
278 gcc_assert (code1 == code2);
280 switch (code1)
282 case POINTER_TYPE:
283 /* For two pointers, do this recursively on the target type. */
285 tree pointed_to_1 = TREE_TYPE (t1);
286 tree pointed_to_2 = TREE_TYPE (t2);
287 tree target = composite_type (pointed_to_1, pointed_to_2);
288 t1 = build_pointer_type (target);
289 t1 = build_type_attribute_variant (t1, attributes);
290 return qualify_type (t1, t2);
293 case ARRAY_TYPE:
295 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
296 int quals;
297 tree unqual_elt;
298 tree d1 = TYPE_DOMAIN (t1);
299 tree d2 = TYPE_DOMAIN (t2);
300 bool d1_variable, d2_variable;
301 bool d1_zero, d2_zero;
303 /* We should not have any type quals on arrays at all. */
304 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
306 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
307 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
309 d1_variable = (!d1_zero
310 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
311 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
312 d2_variable = (!d2_zero
313 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
314 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
316 /* Save space: see if the result is identical to one of the args. */
317 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
318 && (d2_variable || d2_zero || !d1_variable))
319 return build_type_attribute_variant (t1, attributes);
320 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
321 && (d1_variable || d1_zero || !d2_variable))
322 return build_type_attribute_variant (t2, attributes);
324 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
325 return build_type_attribute_variant (t1, attributes);
326 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
327 return build_type_attribute_variant (t2, attributes);
329 /* Merge the element types, and have a size if either arg has
330 one. We may have qualifiers on the element types. To set
331 up TYPE_MAIN_VARIANT correctly, we need to form the
332 composite of the unqualified types and add the qualifiers
333 back at the end. */
334 quals = TYPE_QUALS (strip_array_types (elt));
335 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
336 t1 = build_array_type (unqual_elt,
337 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
338 && (d2_variable
339 || d2_zero
340 || !d1_variable))
341 ? t1
342 : t2));
343 t1 = c_build_qualified_type (t1, quals);
344 return build_type_attribute_variant (t1, attributes);
347 case FUNCTION_TYPE:
348 /* Function types: prefer the one that specified arg types.
349 If both do, merge the arg types. Also merge the return types. */
351 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
352 tree p1 = TYPE_ARG_TYPES (t1);
353 tree p2 = TYPE_ARG_TYPES (t2);
354 int len;
355 tree newargs, n;
356 int i;
358 /* Save space: see if the result is identical to one of the args. */
359 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
360 return build_type_attribute_variant (t1, attributes);
361 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
362 return build_type_attribute_variant (t2, attributes);
364 /* Simple way if one arg fails to specify argument types. */
365 if (TYPE_ARG_TYPES (t1) == 0)
367 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
368 t1 = build_type_attribute_variant (t1, attributes);
369 return qualify_type (t1, t2);
371 if (TYPE_ARG_TYPES (t2) == 0)
373 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
374 t1 = build_type_attribute_variant (t1, attributes);
375 return qualify_type (t1, t2);
378 /* If both args specify argument types, we must merge the two
379 lists, argument by argument. */
380 /* Tell global_bindings_p to return false so that variable_size
381 doesn't die on VLAs in parameter types. */
382 c_override_global_bindings_to_false = true;
384 len = list_length (p1);
385 newargs = 0;
387 for (i = 0; i < len; i++)
388 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
390 n = newargs;
392 for (; p1;
393 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
395 /* A null type means arg type is not specified.
396 Take whatever the other function type has. */
397 if (TREE_VALUE (p1) == 0)
399 TREE_VALUE (n) = TREE_VALUE (p2);
400 goto parm_done;
402 if (TREE_VALUE (p2) == 0)
404 TREE_VALUE (n) = TREE_VALUE (p1);
405 goto parm_done;
408 /* Given wait (union {union wait *u; int *i} *)
409 and wait (union wait *),
410 prefer union wait * as type of parm. */
411 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
412 && TREE_VALUE (p1) != TREE_VALUE (p2))
414 tree memb;
415 tree mv2 = TREE_VALUE (p2);
416 if (mv2 && mv2 != error_mark_node
417 && TREE_CODE (mv2) != ARRAY_TYPE)
418 mv2 = TYPE_MAIN_VARIANT (mv2);
419 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
420 memb; memb = TREE_CHAIN (memb))
422 tree mv3 = TREE_TYPE (memb);
423 if (mv3 && mv3 != error_mark_node
424 && TREE_CODE (mv3) != ARRAY_TYPE)
425 mv3 = TYPE_MAIN_VARIANT (mv3);
426 if (comptypes (mv3, mv2))
428 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
429 TREE_VALUE (p2));
430 if (pedantic)
431 pedwarn ("function types not truly compatible in ISO C");
432 goto parm_done;
436 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
437 && TREE_VALUE (p2) != TREE_VALUE (p1))
439 tree memb;
440 tree mv1 = TREE_VALUE (p1);
441 if (mv1 && mv1 != error_mark_node
442 && TREE_CODE (mv1) != ARRAY_TYPE)
443 mv1 = TYPE_MAIN_VARIANT (mv1);
444 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
445 memb; memb = TREE_CHAIN (memb))
447 tree mv3 = TREE_TYPE (memb);
448 if (mv3 && mv3 != error_mark_node
449 && TREE_CODE (mv3) != ARRAY_TYPE)
450 mv3 = TYPE_MAIN_VARIANT (mv3);
451 if (comptypes (mv3, mv1))
453 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
454 TREE_VALUE (p1));
455 if (pedantic)
456 pedwarn ("function types not truly compatible in ISO C");
457 goto parm_done;
461 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
462 parm_done: ;
465 c_override_global_bindings_to_false = false;
466 t1 = build_function_type (valtype, newargs);
467 t1 = qualify_type (t1, t2);
468 /* ... falls through ... */
471 default:
472 return build_type_attribute_variant (t1, attributes);
477 /* Return the type of a conditional expression between pointers to
478 possibly differently qualified versions of compatible types.
480 We assume that comp_target_types has already been done and returned
481 nonzero; if that isn't so, this may crash. */
483 static tree
484 common_pointer_type (tree t1, tree t2)
486 tree attributes;
487 tree pointed_to_1, mv1;
488 tree pointed_to_2, mv2;
489 tree target;
491 /* Save time if the two types are the same. */
493 if (t1 == t2) return t1;
495 /* If one type is nonsense, use the other. */
496 if (t1 == error_mark_node)
497 return t2;
498 if (t2 == error_mark_node)
499 return t1;
501 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
502 && TREE_CODE (t2) == POINTER_TYPE);
504 /* Merge the attributes. */
505 attributes = targetm.merge_type_attributes (t1, t2);
507 /* Find the composite type of the target types, and combine the
508 qualifiers of the two types' targets. Do not lose qualifiers on
509 array element types by taking the TYPE_MAIN_VARIANT. */
510 mv1 = pointed_to_1 = TREE_TYPE (t1);
511 mv2 = pointed_to_2 = TREE_TYPE (t2);
512 if (TREE_CODE (mv1) != ARRAY_TYPE)
513 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
514 if (TREE_CODE (mv2) != ARRAY_TYPE)
515 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
516 target = composite_type (mv1, mv2);
517 t1 = build_pointer_type (c_build_qualified_type
518 (target,
519 TYPE_QUALS (pointed_to_1) |
520 TYPE_QUALS (pointed_to_2)));
521 return build_type_attribute_variant (t1, attributes);
524 /* Return the common type for two arithmetic types under the usual
525 arithmetic conversions. The default conversions have already been
526 applied, and enumerated types converted to their compatible integer
527 types. The resulting type is unqualified and has no attributes.
529 This is the type for the result of most arithmetic operations
530 if the operands have the given two types. */
532 static tree
533 c_common_type (tree t1, tree t2)
535 enum tree_code code1;
536 enum tree_code code2;
538 /* If one type is nonsense, use the other. */
539 if (t1 == error_mark_node)
540 return t2;
541 if (t2 == error_mark_node)
542 return t1;
544 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
545 t1 = TYPE_MAIN_VARIANT (t1);
547 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
548 t2 = TYPE_MAIN_VARIANT (t2);
550 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
551 t1 = build_type_attribute_variant (t1, NULL_TREE);
553 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
554 t2 = build_type_attribute_variant (t2, NULL_TREE);
556 /* Save time if the two types are the same. */
558 if (t1 == t2) return t1;
560 code1 = TREE_CODE (t1);
561 code2 = TREE_CODE (t2);
563 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
564 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
565 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
566 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
568 /* If one type is a vector type, return that type. (How the usual
569 arithmetic conversions apply to the vector types extension is not
570 precisely specified.) */
571 if (code1 == VECTOR_TYPE)
572 return t1;
574 if (code2 == VECTOR_TYPE)
575 return t2;
577 /* If one type is complex, form the common type of the non-complex
578 components, then make that complex. Use T1 or T2 if it is the
579 required type. */
580 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
582 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
583 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
584 tree subtype = c_common_type (subtype1, subtype2);
586 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
587 return t1;
588 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
589 return t2;
590 else
591 return build_complex_type (subtype);
594 /* If only one is real, use it as the result. */
596 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
597 return t1;
599 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
600 return t2;
602 /* Both real or both integers; use the one with greater precision. */
604 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
605 return t1;
606 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
607 return t2;
609 /* Same precision. Prefer long longs to longs to ints when the
610 same precision, following the C99 rules on integer type rank
611 (which are equivalent to the C90 rules for C90 types). */
613 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
614 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
615 return long_long_unsigned_type_node;
617 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
618 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
620 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
621 return long_long_unsigned_type_node;
622 else
623 return long_long_integer_type_node;
626 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
627 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
628 return long_unsigned_type_node;
630 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
631 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
633 /* But preserve unsignedness from the other type,
634 since long cannot hold all the values of an unsigned int. */
635 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
636 return long_unsigned_type_node;
637 else
638 return long_integer_type_node;
641 /* Likewise, prefer long double to double even if same size. */
642 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
643 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
644 return long_double_type_node;
646 /* Otherwise prefer the unsigned one. */
648 if (TYPE_UNSIGNED (t1))
649 return t1;
650 else
651 return t2;
654 /* Wrapper around c_common_type that is used by c-common.c and other
655 front end optimizations that remove promotions. ENUMERAL_TYPEs
656 are allowed here and are converted to their compatible integer types.
657 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
658 preferably a non-Boolean type as the common type. */
659 tree
660 common_type (tree t1, tree t2)
662 if (TREE_CODE (t1) == ENUMERAL_TYPE)
663 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
664 if (TREE_CODE (t2) == ENUMERAL_TYPE)
665 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
667 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
668 if (TREE_CODE (t1) == BOOLEAN_TYPE
669 && TREE_CODE (t2) == BOOLEAN_TYPE)
670 return boolean_type_node;
672 /* If either type is BOOLEAN_TYPE, then return the other. */
673 if (TREE_CODE (t1) == BOOLEAN_TYPE)
674 return t2;
675 if (TREE_CODE (t2) == BOOLEAN_TYPE)
676 return t1;
678 return c_common_type (t1, t2);
681 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
682 or various other operations. Return 2 if they are compatible
683 but a warning may be needed if you use them together. */
686 comptypes (tree type1, tree type2)
688 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
689 int val;
691 val = comptypes_internal (type1, type2);
692 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
694 return val;
696 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
697 or various other operations. Return 2 if they are compatible
698 but a warning may be needed if you use them together. This
699 differs from comptypes, in that we don't free the seen types. */
701 static int
702 comptypes_internal (tree type1, tree type2)
704 tree t1 = type1;
705 tree t2 = type2;
706 int attrval, val;
708 /* Suppress errors caused by previously reported errors. */
710 if (t1 == t2 || !t1 || !t2
711 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
712 return 1;
714 /* If either type is the internal version of sizetype, return the
715 language version. */
716 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
717 && TYPE_ORIG_SIZE_TYPE (t1))
718 t1 = TYPE_ORIG_SIZE_TYPE (t1);
720 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
721 && TYPE_ORIG_SIZE_TYPE (t2))
722 t2 = TYPE_ORIG_SIZE_TYPE (t2);
725 /* Enumerated types are compatible with integer types, but this is
726 not transitive: two enumerated types in the same translation unit
727 are compatible with each other only if they are the same type. */
729 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
730 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
731 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
732 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
734 if (t1 == t2)
735 return 1;
737 /* Different classes of types can't be compatible. */
739 if (TREE_CODE (t1) != TREE_CODE (t2))
740 return 0;
742 /* Qualifiers must match. C99 6.7.3p9 */
744 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
745 return 0;
747 /* Allow for two different type nodes which have essentially the same
748 definition. Note that we already checked for equality of the type
749 qualifiers (just above). */
751 if (TREE_CODE (t1) != ARRAY_TYPE
752 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
753 return 1;
755 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
756 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
757 return 0;
759 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
760 val = 0;
762 switch (TREE_CODE (t1))
764 case POINTER_TYPE:
765 /* Do not remove mode or aliasing information. */
766 if (TYPE_MODE (t1) != TYPE_MODE (t2)
767 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
768 break;
769 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
770 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2)));
771 break;
773 case FUNCTION_TYPE:
774 val = function_types_compatible_p (t1, t2);
775 break;
777 case ARRAY_TYPE:
779 tree d1 = TYPE_DOMAIN (t1);
780 tree d2 = TYPE_DOMAIN (t2);
781 bool d1_variable, d2_variable;
782 bool d1_zero, d2_zero;
783 val = 1;
785 /* Target types must match incl. qualifiers. */
786 if (TREE_TYPE (t1) != TREE_TYPE (t2)
787 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2))))
788 return 0;
790 /* Sizes must match unless one is missing or variable. */
791 if (d1 == 0 || d2 == 0 || d1 == d2)
792 break;
794 d1_zero = !TYPE_MAX_VALUE (d1);
795 d2_zero = !TYPE_MAX_VALUE (d2);
797 d1_variable = (!d1_zero
798 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
799 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
800 d2_variable = (!d2_zero
801 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
802 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
804 if (d1_variable || d2_variable)
805 break;
806 if (d1_zero && d2_zero)
807 break;
808 if (d1_zero || d2_zero
809 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
810 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
811 val = 0;
813 break;
816 case ENUMERAL_TYPE:
817 case RECORD_TYPE:
818 case UNION_TYPE:
819 if (val != 1 && !same_translation_unit_p (t1, t2))
821 if (attrval != 2)
822 return tagged_types_tu_compatible_p (t1, t2);
823 val = tagged_types_tu_compatible_p (t1, t2);
825 break;
827 case VECTOR_TYPE:
828 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
829 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2));
830 break;
832 default:
833 break;
835 return attrval == 2 && val == 1 ? 2 : val;
838 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
839 ignoring their qualifiers. */
841 static int
842 comp_target_types (tree ttl, tree ttr)
844 int val;
845 tree mvl, mvr;
847 /* Do not lose qualifiers on element types of array types that are
848 pointer targets by taking their TYPE_MAIN_VARIANT. */
849 mvl = TREE_TYPE (ttl);
850 mvr = TREE_TYPE (ttr);
851 if (TREE_CODE (mvl) != ARRAY_TYPE)
852 mvl = TYPE_MAIN_VARIANT (mvl);
853 if (TREE_CODE (mvr) != ARRAY_TYPE)
854 mvr = TYPE_MAIN_VARIANT (mvr);
855 val = comptypes (mvl, mvr);
857 if (val == 2 && pedantic)
858 pedwarn ("types are not quite compatible");
859 return val;
862 /* Subroutines of `comptypes'. */
864 /* Determine whether two trees derive from the same translation unit.
865 If the CONTEXT chain ends in a null, that tree's context is still
866 being parsed, so if two trees have context chains ending in null,
867 they're in the same translation unit. */
869 same_translation_unit_p (tree t1, tree t2)
871 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
872 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
874 case tcc_declaration:
875 t1 = DECL_CONTEXT (t1); break;
876 case tcc_type:
877 t1 = TYPE_CONTEXT (t1); break;
878 case tcc_exceptional:
879 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
880 default: gcc_unreachable ();
883 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
884 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
886 case tcc_declaration:
887 t2 = DECL_CONTEXT (t2); break;
888 case tcc_type:
889 t2 = TYPE_CONTEXT (t2); break;
890 case tcc_exceptional:
891 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
892 default: gcc_unreachable ();
895 return t1 == t2;
898 /* Allocate the seen two types, assuming that they are compatible. */
900 static struct tagged_tu_seen_cache *
901 alloc_tagged_tu_seen_cache (tree t1, tree t2)
903 struct tagged_tu_seen_cache *tu = xmalloc (sizeof (struct tagged_tu_seen_cache));
904 tu->next = tagged_tu_seen_base;
905 tu->t1 = t1;
906 tu->t2 = t2;
908 tagged_tu_seen_base = tu;
910 /* The C standard says that two structures in different translation
911 units are compatible with each other only if the types of their
912 fields are compatible (among other things). We assume that they
913 are compatible until proven otherwise when building the cache.
914 An example where this can occur is:
915 struct a
917 struct a *next;
919 If we are comparing this against a similar struct in another TU,
920 and did not assume they were compatible, we end up with an infinite
921 loop. */
922 tu->val = 1;
923 return tu;
926 /* Free the seen types until we get to TU_TIL. */
928 static void
929 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
931 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
932 while (tu != tu_til)
934 struct tagged_tu_seen_cache *tu1 = (struct tagged_tu_seen_cache*)tu;
935 tu = tu1->next;
936 free (tu1);
938 tagged_tu_seen_base = tu_til;
941 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
942 compatible. If the two types are not the same (which has been
943 checked earlier), this can only happen when multiple translation
944 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
945 rules. */
947 static int
948 tagged_types_tu_compatible_p (tree t1, tree t2)
950 tree s1, s2;
951 bool needs_warning = false;
953 /* We have to verify that the tags of the types are the same. This
954 is harder than it looks because this may be a typedef, so we have
955 to go look at the original type. It may even be a typedef of a
956 typedef...
957 In the case of compiler-created builtin structs the TYPE_DECL
958 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
959 while (TYPE_NAME (t1)
960 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
961 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
962 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
964 while (TYPE_NAME (t2)
965 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
966 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
967 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
969 /* C90 didn't have the requirement that the two tags be the same. */
970 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
971 return 0;
973 /* C90 didn't say what happened if one or both of the types were
974 incomplete; we choose to follow C99 rules here, which is that they
975 are compatible. */
976 if (TYPE_SIZE (t1) == NULL
977 || TYPE_SIZE (t2) == NULL)
978 return 1;
981 const struct tagged_tu_seen_cache * tts_i;
982 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
983 if (tts_i->t1 == t1 && tts_i->t2 == t2)
984 return tts_i->val;
987 switch (TREE_CODE (t1))
989 case ENUMERAL_TYPE:
991 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
992 /* Speed up the case where the type values are in the same order. */
993 tree tv1 = TYPE_VALUES (t1);
994 tree tv2 = TYPE_VALUES (t2);
996 if (tv1 == tv2)
998 return 1;
1001 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1003 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1004 break;
1005 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1007 tu->val = 0;
1008 return 0;
1012 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1014 return 1;
1016 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1018 tu->val = 0;
1019 return 0;
1022 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1024 tu->val = 0;
1025 return 0;
1028 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1030 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1031 if (s2 == NULL
1032 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1034 tu->val = 0;
1035 return 0;
1038 return 1;
1041 case UNION_TYPE:
1043 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1044 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1046 tu->val = 0;
1047 return 0;
1050 /* Speed up the common case where the fields are in the same order. */
1051 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1052 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1054 int result;
1057 if (DECL_NAME (s1) == NULL
1058 || DECL_NAME (s1) != DECL_NAME (s2))
1059 break;
1060 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1061 if (result == 0)
1063 tu->val = 0;
1064 return 0;
1066 if (result == 2)
1067 needs_warning = true;
1069 if (TREE_CODE (s1) == FIELD_DECL
1070 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1071 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1073 tu->val = 0;
1074 return 0;
1077 if (!s1 && !s2)
1079 tu->val = needs_warning ? 2 : 1;
1080 return tu->val;
1083 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
1085 bool ok = false;
1087 if (DECL_NAME (s1) != NULL)
1088 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
1089 if (DECL_NAME (s1) == DECL_NAME (s2))
1091 int result;
1092 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1093 if (result == 0)
1095 tu->val = 0;
1096 return 0;
1098 if (result == 2)
1099 needs_warning = true;
1101 if (TREE_CODE (s1) == FIELD_DECL
1102 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1103 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1104 break;
1106 ok = true;
1107 break;
1109 if (!ok)
1111 tu->val = 0;
1112 return 0;
1115 tu->val = needs_warning ? 2 : 10;
1116 return tu->val;
1119 case RECORD_TYPE:
1121 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1123 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1124 s1 && s2;
1125 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1127 int result;
1128 if (TREE_CODE (s1) != TREE_CODE (s2)
1129 || DECL_NAME (s1) != DECL_NAME (s2))
1130 break;
1131 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2));
1132 if (result == 0)
1133 break;
1134 if (result == 2)
1135 needs_warning = true;
1137 if (TREE_CODE (s1) == FIELD_DECL
1138 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1139 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1140 break;
1142 if (s1 && s2)
1143 tu->val = 0;
1144 else
1145 tu->val = needs_warning ? 2 : 1;
1146 return tu->val;
1149 default:
1150 gcc_unreachable ();
1154 /* Return 1 if two function types F1 and F2 are compatible.
1155 If either type specifies no argument types,
1156 the other must specify a fixed number of self-promoting arg types.
1157 Otherwise, if one type specifies only the number of arguments,
1158 the other must specify that number of self-promoting arg types.
1159 Otherwise, the argument types must match. */
1161 static int
1162 function_types_compatible_p (tree f1, tree f2)
1164 tree args1, args2;
1165 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1166 int val = 1;
1167 int val1;
1168 tree ret1, ret2;
1170 ret1 = TREE_TYPE (f1);
1171 ret2 = TREE_TYPE (f2);
1173 /* 'volatile' qualifiers on a function's return type used to mean
1174 the function is noreturn. */
1175 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1176 pedwarn ("function return types not compatible due to %<volatile%>");
1177 if (TYPE_VOLATILE (ret1))
1178 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1179 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1180 if (TYPE_VOLATILE (ret2))
1181 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1182 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1183 val = comptypes_internal (ret1, ret2);
1184 if (val == 0)
1185 return 0;
1187 args1 = TYPE_ARG_TYPES (f1);
1188 args2 = TYPE_ARG_TYPES (f2);
1190 /* An unspecified parmlist matches any specified parmlist
1191 whose argument types don't need default promotions. */
1193 if (args1 == 0)
1195 if (!self_promoting_args_p (args2))
1196 return 0;
1197 /* If one of these types comes from a non-prototype fn definition,
1198 compare that with the other type's arglist.
1199 If they don't match, ask for a warning (but no error). */
1200 if (TYPE_ACTUAL_ARG_TYPES (f1)
1201 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1202 val = 2;
1203 return val;
1205 if (args2 == 0)
1207 if (!self_promoting_args_p (args1))
1208 return 0;
1209 if (TYPE_ACTUAL_ARG_TYPES (f2)
1210 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1211 val = 2;
1212 return val;
1215 /* Both types have argument lists: compare them and propagate results. */
1216 val1 = type_lists_compatible_p (args1, args2);
1217 return val1 != 1 ? val1 : val;
1220 /* Check two lists of types for compatibility,
1221 returning 0 for incompatible, 1 for compatible,
1222 or 2 for compatible with warning. */
1224 static int
1225 type_lists_compatible_p (tree args1, tree args2)
1227 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1228 int val = 1;
1229 int newval = 0;
1231 while (1)
1233 tree a1, mv1, a2, mv2;
1234 if (args1 == 0 && args2 == 0)
1235 return val;
1236 /* If one list is shorter than the other,
1237 they fail to match. */
1238 if (args1 == 0 || args2 == 0)
1239 return 0;
1240 mv1 = a1 = TREE_VALUE (args1);
1241 mv2 = a2 = TREE_VALUE (args2);
1242 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1243 mv1 = TYPE_MAIN_VARIANT (mv1);
1244 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1245 mv2 = TYPE_MAIN_VARIANT (mv2);
1246 /* A null pointer instead of a type
1247 means there is supposed to be an argument
1248 but nothing is specified about what type it has.
1249 So match anything that self-promotes. */
1250 if (a1 == 0)
1252 if (c_type_promotes_to (a2) != a2)
1253 return 0;
1255 else if (a2 == 0)
1257 if (c_type_promotes_to (a1) != a1)
1258 return 0;
1260 /* If one of the lists has an error marker, ignore this arg. */
1261 else if (TREE_CODE (a1) == ERROR_MARK
1262 || TREE_CODE (a2) == ERROR_MARK)
1264 else if (!(newval = comptypes_internal (mv1, mv2)))
1266 /* Allow wait (union {union wait *u; int *i} *)
1267 and wait (union wait *) to be compatible. */
1268 if (TREE_CODE (a1) == UNION_TYPE
1269 && (TYPE_NAME (a1) == 0
1270 || TYPE_TRANSPARENT_UNION (a1))
1271 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1272 && tree_int_cst_equal (TYPE_SIZE (a1),
1273 TYPE_SIZE (a2)))
1275 tree memb;
1276 for (memb = TYPE_FIELDS (a1);
1277 memb; memb = TREE_CHAIN (memb))
1279 tree mv3 = TREE_TYPE (memb);
1280 if (mv3 && mv3 != error_mark_node
1281 && TREE_CODE (mv3) != ARRAY_TYPE)
1282 mv3 = TYPE_MAIN_VARIANT (mv3);
1283 if (comptypes_internal (mv3, mv2))
1284 break;
1286 if (memb == 0)
1287 return 0;
1289 else if (TREE_CODE (a2) == UNION_TYPE
1290 && (TYPE_NAME (a2) == 0
1291 || TYPE_TRANSPARENT_UNION (a2))
1292 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1293 && tree_int_cst_equal (TYPE_SIZE (a2),
1294 TYPE_SIZE (a1)))
1296 tree memb;
1297 for (memb = TYPE_FIELDS (a2);
1298 memb; memb = TREE_CHAIN (memb))
1300 tree mv3 = TREE_TYPE (memb);
1301 if (mv3 && mv3 != error_mark_node
1302 && TREE_CODE (mv3) != ARRAY_TYPE)
1303 mv3 = TYPE_MAIN_VARIANT (mv3);
1304 if (comptypes_internal (mv3, mv1))
1305 break;
1307 if (memb == 0)
1308 return 0;
1310 else
1311 return 0;
1314 /* comptypes said ok, but record if it said to warn. */
1315 if (newval > val)
1316 val = newval;
1318 args1 = TREE_CHAIN (args1);
1319 args2 = TREE_CHAIN (args2);
1323 /* Compute the size to increment a pointer by. */
1325 static tree
1326 c_size_in_bytes (tree type)
1328 enum tree_code code = TREE_CODE (type);
1330 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1331 return size_one_node;
1333 if (!COMPLETE_OR_VOID_TYPE_P (type))
1335 error ("arithmetic on pointer to an incomplete type");
1336 return size_one_node;
1339 /* Convert in case a char is more than one unit. */
1340 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1341 size_int (TYPE_PRECISION (char_type_node)
1342 / BITS_PER_UNIT));
1345 /* Return either DECL or its known constant value (if it has one). */
1347 tree
1348 decl_constant_value (tree decl)
1350 if (/* Don't change a variable array bound or initial value to a constant
1351 in a place where a variable is invalid. Note that DECL_INITIAL
1352 isn't valid for a PARM_DECL. */
1353 current_function_decl != 0
1354 && TREE_CODE (decl) != PARM_DECL
1355 && !TREE_THIS_VOLATILE (decl)
1356 && TREE_READONLY (decl)
1357 && DECL_INITIAL (decl) != 0
1358 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1359 /* This is invalid if initial value is not constant.
1360 If it has either a function call, a memory reference,
1361 or a variable, then re-evaluating it could give different results. */
1362 && TREE_CONSTANT (DECL_INITIAL (decl))
1363 /* Check for cases where this is sub-optimal, even though valid. */
1364 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1365 return DECL_INITIAL (decl);
1366 return decl;
1369 /* Return either DECL or its known constant value (if it has one), but
1370 return DECL if pedantic or DECL has mode BLKmode. This is for
1371 bug-compatibility with the old behavior of decl_constant_value
1372 (before GCC 3.0); every use of this function is a bug and it should
1373 be removed before GCC 3.1. It is not appropriate to use pedantic
1374 in a way that affects optimization, and BLKmode is probably not the
1375 right test for avoiding misoptimizations either. */
1377 static tree
1378 decl_constant_value_for_broken_optimization (tree decl)
1380 tree ret;
1382 if (pedantic || DECL_MODE (decl) == BLKmode)
1383 return decl;
1385 ret = decl_constant_value (decl);
1386 /* Avoid unwanted tree sharing between the initializer and current
1387 function's body where the tree can be modified e.g. by the
1388 gimplifier. */
1389 if (ret != decl && TREE_STATIC (decl))
1390 ret = unshare_expr (ret);
1391 return ret;
1394 /* Convert the array expression EXP to a pointer. */
1395 static tree
1396 array_to_pointer_conversion (tree exp)
1398 tree orig_exp = exp;
1399 tree type = TREE_TYPE (exp);
1400 tree adr;
1401 tree restype = TREE_TYPE (type);
1402 tree ptrtype;
1404 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1406 STRIP_TYPE_NOPS (exp);
1408 if (TREE_NO_WARNING (orig_exp))
1409 TREE_NO_WARNING (exp) = 1;
1411 ptrtype = build_pointer_type (restype);
1413 if (TREE_CODE (exp) == INDIRECT_REF)
1414 return convert (ptrtype, TREE_OPERAND (exp, 0));
1416 if (TREE_CODE (exp) == VAR_DECL)
1418 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1419 ADDR_EXPR because it's the best way of representing what
1420 happens in C when we take the address of an array and place
1421 it in a pointer to the element type. */
1422 adr = build1 (ADDR_EXPR, ptrtype, exp);
1423 if (!c_mark_addressable (exp))
1424 return error_mark_node;
1425 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1426 return adr;
1429 /* This way is better for a COMPONENT_REF since it can
1430 simplify the offset for a component. */
1431 adr = build_unary_op (ADDR_EXPR, exp, 1);
1432 return convert (ptrtype, adr);
1435 /* Convert the function expression EXP to a pointer. */
1436 static tree
1437 function_to_pointer_conversion (tree exp)
1439 tree orig_exp = exp;
1441 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1443 STRIP_TYPE_NOPS (exp);
1445 if (TREE_NO_WARNING (orig_exp))
1446 TREE_NO_WARNING (exp) = 1;
1448 return build_unary_op (ADDR_EXPR, exp, 0);
1451 /* Perform the default conversion of arrays and functions to pointers.
1452 Return the result of converting EXP. For any other expression, just
1453 return EXP after removing NOPs. */
1455 struct c_expr
1456 default_function_array_conversion (struct c_expr exp)
1458 tree orig_exp = exp.value;
1459 tree type = TREE_TYPE (exp.value);
1460 enum tree_code code = TREE_CODE (type);
1462 switch (code)
1464 case ARRAY_TYPE:
1466 bool not_lvalue = false;
1467 bool lvalue_array_p;
1469 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1470 || TREE_CODE (exp.value) == NOP_EXPR)
1471 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1473 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1474 not_lvalue = true;
1475 exp.value = TREE_OPERAND (exp.value, 0);
1478 if (TREE_NO_WARNING (orig_exp))
1479 TREE_NO_WARNING (exp.value) = 1;
1481 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1482 if (!flag_isoc99 && !lvalue_array_p)
1484 /* Before C99, non-lvalue arrays do not decay to pointers.
1485 Normally, using such an array would be invalid; but it can
1486 be used correctly inside sizeof or as a statement expression.
1487 Thus, do not give an error here; an error will result later. */
1488 return exp;
1491 exp.value = array_to_pointer_conversion (exp.value);
1493 break;
1494 case FUNCTION_TYPE:
1495 exp.value = function_to_pointer_conversion (exp.value);
1496 break;
1497 default:
1498 STRIP_TYPE_NOPS (exp.value);
1499 if (TREE_NO_WARNING (orig_exp))
1500 TREE_NO_WARNING (exp.value) = 1;
1501 break;
1504 return exp;
1508 /* EXP is an expression of integer type. Apply the integer promotions
1509 to it and return the promoted value. */
1511 tree
1512 perform_integral_promotions (tree exp)
1514 tree type = TREE_TYPE (exp);
1515 enum tree_code code = TREE_CODE (type);
1517 gcc_assert (INTEGRAL_TYPE_P (type));
1519 /* Normally convert enums to int,
1520 but convert wide enums to something wider. */
1521 if (code == ENUMERAL_TYPE)
1523 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1524 TYPE_PRECISION (integer_type_node)),
1525 ((TYPE_PRECISION (type)
1526 >= TYPE_PRECISION (integer_type_node))
1527 && TYPE_UNSIGNED (type)));
1529 return convert (type, exp);
1532 /* ??? This should no longer be needed now bit-fields have their
1533 proper types. */
1534 if (TREE_CODE (exp) == COMPONENT_REF
1535 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1536 /* If it's thinner than an int, promote it like a
1537 c_promoting_integer_type_p, otherwise leave it alone. */
1538 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1539 TYPE_PRECISION (integer_type_node)))
1540 return convert (integer_type_node, exp);
1542 if (c_promoting_integer_type_p (type))
1544 /* Preserve unsignedness if not really getting any wider. */
1545 if (TYPE_UNSIGNED (type)
1546 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1547 return convert (unsigned_type_node, exp);
1549 return convert (integer_type_node, exp);
1552 return exp;
1556 /* Perform default promotions for C data used in expressions.
1557 Enumeral types or short or char are converted to int.
1558 In addition, manifest constants symbols are replaced by their values. */
1560 tree
1561 default_conversion (tree exp)
1563 tree orig_exp;
1564 tree type = TREE_TYPE (exp);
1565 enum tree_code code = TREE_CODE (type);
1567 /* Functions and arrays have been converted during parsing. */
1568 gcc_assert (code != FUNCTION_TYPE);
1569 if (code == ARRAY_TYPE)
1570 return exp;
1572 /* Constants can be used directly unless they're not loadable. */
1573 if (TREE_CODE (exp) == CONST_DECL)
1574 exp = DECL_INITIAL (exp);
1576 /* Replace a nonvolatile const static variable with its value unless
1577 it is an array, in which case we must be sure that taking the
1578 address of the array produces consistent results. */
1579 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1581 exp = decl_constant_value_for_broken_optimization (exp);
1582 type = TREE_TYPE (exp);
1585 /* Strip no-op conversions. */
1586 orig_exp = exp;
1587 STRIP_TYPE_NOPS (exp);
1589 if (TREE_NO_WARNING (orig_exp))
1590 TREE_NO_WARNING (exp) = 1;
1592 if (INTEGRAL_TYPE_P (type))
1593 return perform_integral_promotions (exp);
1595 if (code == VOID_TYPE)
1597 error ("void value not ignored as it ought to be");
1598 return error_mark_node;
1600 return exp;
1603 /* Look up COMPONENT in a structure or union DECL.
1605 If the component name is not found, returns NULL_TREE. Otherwise,
1606 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1607 stepping down the chain to the component, which is in the last
1608 TREE_VALUE of the list. Normally the list is of length one, but if
1609 the component is embedded within (nested) anonymous structures or
1610 unions, the list steps down the chain to the component. */
1612 static tree
1613 lookup_field (tree decl, tree component)
1615 tree type = TREE_TYPE (decl);
1616 tree field;
1618 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1619 to the field elements. Use a binary search on this array to quickly
1620 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1621 will always be set for structures which have many elements. */
1623 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1625 int bot, top, half;
1626 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1628 field = TYPE_FIELDS (type);
1629 bot = 0;
1630 top = TYPE_LANG_SPECIFIC (type)->s->len;
1631 while (top - bot > 1)
1633 half = (top - bot + 1) >> 1;
1634 field = field_array[bot+half];
1636 if (DECL_NAME (field) == NULL_TREE)
1638 /* Step through all anon unions in linear fashion. */
1639 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1641 field = field_array[bot++];
1642 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1643 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1645 tree anon = lookup_field (field, component);
1647 if (anon)
1648 return tree_cons (NULL_TREE, field, anon);
1652 /* Entire record is only anon unions. */
1653 if (bot > top)
1654 return NULL_TREE;
1656 /* Restart the binary search, with new lower bound. */
1657 continue;
1660 if (DECL_NAME (field) == component)
1661 break;
1662 if (DECL_NAME (field) < component)
1663 bot += half;
1664 else
1665 top = bot + half;
1668 if (DECL_NAME (field_array[bot]) == component)
1669 field = field_array[bot];
1670 else if (DECL_NAME (field) != component)
1671 return NULL_TREE;
1673 else
1675 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1677 if (DECL_NAME (field) == NULL_TREE
1678 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1679 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1681 tree anon = lookup_field (field, component);
1683 if (anon)
1684 return tree_cons (NULL_TREE, field, anon);
1687 if (DECL_NAME (field) == component)
1688 break;
1691 if (field == NULL_TREE)
1692 return NULL_TREE;
1695 return tree_cons (NULL_TREE, field, NULL_TREE);
1698 /* Make an expression to refer to the COMPONENT field of
1699 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1701 tree
1702 build_component_ref (tree datum, tree component)
1704 tree type = TREE_TYPE (datum);
1705 enum tree_code code = TREE_CODE (type);
1706 tree field = NULL;
1707 tree ref;
1709 if (!objc_is_public (datum, component))
1710 return error_mark_node;
1712 /* See if there is a field or component with name COMPONENT. */
1714 if (code == RECORD_TYPE || code == UNION_TYPE)
1716 if (!COMPLETE_TYPE_P (type))
1718 c_incomplete_type_error (NULL_TREE, type);
1719 return error_mark_node;
1722 field = lookup_field (datum, component);
1724 if (!field)
1726 error ("%qT has no member named %qE", type, component);
1727 return error_mark_node;
1730 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1731 This might be better solved in future the way the C++ front
1732 end does it - by giving the anonymous entities each a
1733 separate name and type, and then have build_component_ref
1734 recursively call itself. We can't do that here. */
1737 tree subdatum = TREE_VALUE (field);
1739 if (TREE_TYPE (subdatum) == error_mark_node)
1740 return error_mark_node;
1742 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1743 NULL_TREE);
1744 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1745 TREE_READONLY (ref) = 1;
1746 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1747 TREE_THIS_VOLATILE (ref) = 1;
1749 if (TREE_DEPRECATED (subdatum))
1750 warn_deprecated_use (subdatum);
1752 datum = ref;
1754 field = TREE_CHAIN (field);
1756 while (field);
1758 return ref;
1760 else if (code != ERROR_MARK)
1761 error ("request for member %qE in something not a structure or union",
1762 component);
1764 return error_mark_node;
1767 /* Given an expression PTR for a pointer, return an expression
1768 for the value pointed to.
1769 ERRORSTRING is the name of the operator to appear in error messages. */
1771 tree
1772 build_indirect_ref (tree ptr, const char *errorstring)
1774 tree pointer = default_conversion (ptr);
1775 tree type = TREE_TYPE (pointer);
1777 if (TREE_CODE (type) == POINTER_TYPE)
1779 if (TREE_CODE (pointer) == ADDR_EXPR
1780 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1781 == TREE_TYPE (type)))
1782 return TREE_OPERAND (pointer, 0);
1783 else
1785 tree t = TREE_TYPE (type);
1786 tree ref;
1788 ref = build1 (INDIRECT_REF, t, pointer);
1790 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1792 error ("dereferencing pointer to incomplete type");
1793 return error_mark_node;
1795 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1796 warning (0, "dereferencing %<void *%> pointer");
1798 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1799 so that we get the proper error message if the result is used
1800 to assign to. Also, &* is supposed to be a no-op.
1801 And ANSI C seems to specify that the type of the result
1802 should be the const type. */
1803 /* A de-reference of a pointer to const is not a const. It is valid
1804 to change it via some other pointer. */
1805 TREE_READONLY (ref) = TYPE_READONLY (t);
1806 TREE_SIDE_EFFECTS (ref)
1807 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1808 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1809 return ref;
1812 else if (TREE_CODE (pointer) != ERROR_MARK)
1813 error ("invalid type argument of %qs", errorstring);
1814 return error_mark_node;
1817 /* This handles expressions of the form "a[i]", which denotes
1818 an array reference.
1820 This is logically equivalent in C to *(a+i), but we may do it differently.
1821 If A is a variable or a member, we generate a primitive ARRAY_REF.
1822 This avoids forcing the array out of registers, and can work on
1823 arrays that are not lvalues (for example, members of structures returned
1824 by functions). */
1826 tree
1827 build_array_ref (tree array, tree index)
1829 bool swapped = false;
1830 if (TREE_TYPE (array) == error_mark_node
1831 || TREE_TYPE (index) == error_mark_node)
1832 return error_mark_node;
1834 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1835 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1837 tree temp;
1838 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1839 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1841 error ("subscripted value is neither array nor pointer");
1842 return error_mark_node;
1844 temp = array;
1845 array = index;
1846 index = temp;
1847 swapped = true;
1850 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1852 error ("array subscript is not an integer");
1853 return error_mark_node;
1856 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1858 error ("subscripted value is pointer to function");
1859 return error_mark_node;
1862 /* ??? Existing practice has been to warn only when the char
1863 index is syntactically the index, not for char[array]. */
1864 if (!swapped)
1865 warn_array_subscript_with_type_char (index);
1867 /* Apply default promotions *after* noticing character types. */
1868 index = default_conversion (index);
1870 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1872 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1874 tree rval, type;
1876 /* An array that is indexed by a non-constant
1877 cannot be stored in a register; we must be able to do
1878 address arithmetic on its address.
1879 Likewise an array of elements of variable size. */
1880 if (TREE_CODE (index) != INTEGER_CST
1881 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1882 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1884 if (!c_mark_addressable (array))
1885 return error_mark_node;
1887 /* An array that is indexed by a constant value which is not within
1888 the array bounds cannot be stored in a register either; because we
1889 would get a crash in store_bit_field/extract_bit_field when trying
1890 to access a non-existent part of the register. */
1891 if (TREE_CODE (index) == INTEGER_CST
1892 && TYPE_DOMAIN (TREE_TYPE (array))
1893 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1895 if (!c_mark_addressable (array))
1896 return error_mark_node;
1899 if (pedantic)
1901 tree foo = array;
1902 while (TREE_CODE (foo) == COMPONENT_REF)
1903 foo = TREE_OPERAND (foo, 0);
1904 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1905 pedwarn ("ISO C forbids subscripting %<register%> array");
1906 else if (!flag_isoc99 && !lvalue_p (foo))
1907 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1910 type = TREE_TYPE (TREE_TYPE (array));
1911 if (TREE_CODE (type) != ARRAY_TYPE)
1912 type = TYPE_MAIN_VARIANT (type);
1913 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1914 /* Array ref is const/volatile if the array elements are
1915 or if the array is. */
1916 TREE_READONLY (rval)
1917 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1918 | TREE_READONLY (array));
1919 TREE_SIDE_EFFECTS (rval)
1920 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1921 | TREE_SIDE_EFFECTS (array));
1922 TREE_THIS_VOLATILE (rval)
1923 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1924 /* This was added by rms on 16 Nov 91.
1925 It fixes vol struct foo *a; a->elts[1]
1926 in an inline function.
1927 Hope it doesn't break something else. */
1928 | TREE_THIS_VOLATILE (array));
1929 return require_complete_type (fold (rval));
1931 else
1933 tree ar = default_conversion (array);
1935 if (ar == error_mark_node)
1936 return ar;
1938 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1939 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1941 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1942 "array indexing");
1946 /* Build an external reference to identifier ID. FUN indicates
1947 whether this will be used for a function call. LOC is the source
1948 location of the identifier. */
1949 tree
1950 build_external_ref (tree id, int fun, location_t loc)
1952 tree ref;
1953 tree decl = lookup_name (id);
1955 /* In Objective-C, an instance variable (ivar) may be preferred to
1956 whatever lookup_name() found. */
1957 decl = objc_lookup_ivar (decl, id);
1959 if (decl && decl != error_mark_node)
1960 ref = decl;
1961 else if (fun)
1962 /* Implicit function declaration. */
1963 ref = implicitly_declare (id);
1964 else if (decl == error_mark_node)
1965 /* Don't complain about something that's already been
1966 complained about. */
1967 return error_mark_node;
1968 else
1970 undeclared_variable (id, loc);
1971 return error_mark_node;
1974 if (TREE_TYPE (ref) == error_mark_node)
1975 return error_mark_node;
1977 if (TREE_DEPRECATED (ref))
1978 warn_deprecated_use (ref);
1980 if (!skip_evaluation)
1981 assemble_external (ref);
1982 TREE_USED (ref) = 1;
1984 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1986 if (!in_sizeof && !in_typeof)
1987 C_DECL_USED (ref) = 1;
1988 else if (DECL_INITIAL (ref) == 0
1989 && DECL_EXTERNAL (ref)
1990 && !TREE_PUBLIC (ref))
1991 record_maybe_used_decl (ref);
1994 if (TREE_CODE (ref) == CONST_DECL)
1996 ref = DECL_INITIAL (ref);
1997 TREE_CONSTANT (ref) = 1;
1998 TREE_INVARIANT (ref) = 1;
2000 else if (current_function_decl != 0
2001 && !DECL_FILE_SCOPE_P (current_function_decl)
2002 && (TREE_CODE (ref) == VAR_DECL
2003 || TREE_CODE (ref) == PARM_DECL
2004 || TREE_CODE (ref) == FUNCTION_DECL))
2006 tree context = decl_function_context (ref);
2008 if (context != 0 && context != current_function_decl)
2009 DECL_NONLOCAL (ref) = 1;
2012 return ref;
2015 /* Record details of decls possibly used inside sizeof or typeof. */
2016 struct maybe_used_decl
2018 /* The decl. */
2019 tree decl;
2020 /* The level seen at (in_sizeof + in_typeof). */
2021 int level;
2022 /* The next one at this level or above, or NULL. */
2023 struct maybe_used_decl *next;
2026 static struct maybe_used_decl *maybe_used_decls;
2028 /* Record that DECL, an undefined static function reference seen
2029 inside sizeof or typeof, might be used if the operand of sizeof is
2030 a VLA type or the operand of typeof is a variably modified
2031 type. */
2033 static void
2034 record_maybe_used_decl (tree decl)
2036 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2037 t->decl = decl;
2038 t->level = in_sizeof + in_typeof;
2039 t->next = maybe_used_decls;
2040 maybe_used_decls = t;
2043 /* Pop the stack of decls possibly used inside sizeof or typeof. If
2044 USED is false, just discard them. If it is true, mark them used
2045 (if no longer inside sizeof or typeof) or move them to the next
2046 level up (if still inside sizeof or typeof). */
2048 void
2049 pop_maybe_used (bool used)
2051 struct maybe_used_decl *p = maybe_used_decls;
2052 int cur_level = in_sizeof + in_typeof;
2053 while (p && p->level > cur_level)
2055 if (used)
2057 if (cur_level == 0)
2058 C_DECL_USED (p->decl) = 1;
2059 else
2060 p->level = cur_level;
2062 p = p->next;
2064 if (!used || cur_level == 0)
2065 maybe_used_decls = p;
2068 /* Return the result of sizeof applied to EXPR. */
2070 struct c_expr
2071 c_expr_sizeof_expr (struct c_expr expr)
2073 struct c_expr ret;
2074 if (expr.value == error_mark_node)
2076 ret.value = error_mark_node;
2077 ret.original_code = ERROR_MARK;
2078 pop_maybe_used (false);
2080 else
2082 ret.value = c_sizeof (TREE_TYPE (expr.value));
2083 ret.original_code = ERROR_MARK;
2084 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
2086 return ret;
2089 /* Return the result of sizeof applied to T, a structure for the type
2090 name passed to sizeof (rather than the type itself). */
2092 struct c_expr
2093 c_expr_sizeof_type (struct c_type_name *t)
2095 tree type;
2096 struct c_expr ret;
2097 type = groktypename (t);
2098 ret.value = c_sizeof (type);
2099 ret.original_code = ERROR_MARK;
2100 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
2101 return ret;
2104 /* Build a function call to function FUNCTION with parameters PARAMS.
2105 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2106 TREE_VALUE of each node is a parameter-expression.
2107 FUNCTION's data type may be a function type or a pointer-to-function. */
2109 tree
2110 build_function_call (tree function, tree params)
2112 tree fntype, fundecl = 0;
2113 tree coerced_params;
2114 tree name = NULL_TREE, result;
2115 tree tem;
2117 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2118 STRIP_TYPE_NOPS (function);
2120 /* Convert anything with function type to a pointer-to-function. */
2121 if (TREE_CODE (function) == FUNCTION_DECL)
2123 /* Implement type-directed function overloading for builtins.
2124 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2125 handle all the type checking. The result is a complete expression
2126 that implements this function call. */
2127 tem = resolve_overloaded_builtin (function, params);
2128 if (tem)
2129 return tem;
2131 name = DECL_NAME (function);
2132 fundecl = function;
2134 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2135 function = function_to_pointer_conversion (function);
2137 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2138 expressions, like those used for ObjC messenger dispatches. */
2139 function = objc_rewrite_function_call (function, params);
2141 fntype = TREE_TYPE (function);
2143 if (TREE_CODE (fntype) == ERROR_MARK)
2144 return error_mark_node;
2146 if (!(TREE_CODE (fntype) == POINTER_TYPE
2147 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2149 error ("called object %qE is not a function", function);
2150 return error_mark_node;
2153 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2154 current_function_returns_abnormally = 1;
2156 /* fntype now gets the type of function pointed to. */
2157 fntype = TREE_TYPE (fntype);
2159 /* Check that the function is called through a compatible prototype.
2160 If it is not, replace the call by a trap, wrapped up in a compound
2161 expression if necessary. This has the nice side-effect to prevent
2162 the tree-inliner from generating invalid assignment trees which may
2163 blow up in the RTL expander later. */
2164 if (TREE_CODE (function) == NOP_EXPR
2165 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2166 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2167 && !comptypes (fntype, TREE_TYPE (tem)))
2169 tree return_type = TREE_TYPE (fntype);
2170 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2171 NULL_TREE);
2173 /* This situation leads to run-time undefined behavior. We can't,
2174 therefore, simply error unless we can prove that all possible
2175 executions of the program must execute the code. */
2176 warning (0, "function called through a non-compatible type");
2178 /* We can, however, treat "undefined" any way we please.
2179 Call abort to encourage the user to fix the program. */
2180 inform ("if this code is reached, the program will abort");
2182 if (VOID_TYPE_P (return_type))
2183 return trap;
2184 else
2186 tree rhs;
2188 if (AGGREGATE_TYPE_P (return_type))
2189 rhs = build_compound_literal (return_type,
2190 build_constructor (return_type, 0));
2191 else
2192 rhs = fold_build1 (NOP_EXPR, return_type, integer_zero_node);
2194 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2198 /* Convert the parameters to the types declared in the
2199 function prototype, or apply default promotions. */
2201 coerced_params
2202 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2204 if (coerced_params == error_mark_node)
2205 return error_mark_node;
2207 /* Check that the arguments to the function are valid. */
2209 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2210 TYPE_ARG_TYPES (fntype));
2212 if (require_constant_value)
2214 result = fold_build3_initializer (CALL_EXPR, TREE_TYPE (fntype),
2215 function, coerced_params, NULL_TREE);
2217 if (TREE_CONSTANT (result)
2218 && (name == NULL_TREE
2219 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2220 pedwarn_init ("initializer element is not constant");
2222 else
2223 result = fold_build3 (CALL_EXPR, TREE_TYPE (fntype),
2224 function, coerced_params, NULL_TREE);
2226 if (VOID_TYPE_P (TREE_TYPE (result)))
2227 return result;
2228 return require_complete_type (result);
2231 /* Convert the argument expressions in the list VALUES
2232 to the types in the list TYPELIST. The result is a list of converted
2233 argument expressions, unless there are too few arguments in which
2234 case it is error_mark_node.
2236 If TYPELIST is exhausted, or when an element has NULL as its type,
2237 perform the default conversions.
2239 PARMLIST is the chain of parm decls for the function being called.
2240 It may be 0, if that info is not available.
2241 It is used only for generating error messages.
2243 FUNCTION is a tree for the called function. It is used only for
2244 error messages, where it is formatted with %qE.
2246 This is also where warnings about wrong number of args are generated.
2248 Both VALUES and the returned value are chains of TREE_LIST nodes
2249 with the elements of the list in the TREE_VALUE slots of those nodes. */
2251 static tree
2252 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2254 tree typetail, valtail;
2255 tree result = NULL;
2256 int parmnum;
2257 tree selector;
2259 /* Change pointer to function to the function itself for
2260 diagnostics. */
2261 if (TREE_CODE (function) == ADDR_EXPR
2262 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2263 function = TREE_OPERAND (function, 0);
2265 /* Handle an ObjC selector specially for diagnostics. */
2266 selector = objc_message_selector ();
2268 /* Scan the given expressions and types, producing individual
2269 converted arguments and pushing them on RESULT in reverse order. */
2271 for (valtail = values, typetail = typelist, parmnum = 0;
2272 valtail;
2273 valtail = TREE_CHAIN (valtail), parmnum++)
2275 tree type = typetail ? TREE_VALUE (typetail) : 0;
2276 tree val = TREE_VALUE (valtail);
2277 tree rname = function;
2278 int argnum = parmnum + 1;
2279 const char *invalid_func_diag;
2281 if (type == void_type_node)
2283 error ("too many arguments to function %qE", function);
2284 break;
2287 if (selector && argnum > 2)
2289 rname = selector;
2290 argnum -= 2;
2293 STRIP_TYPE_NOPS (val);
2295 val = require_complete_type (val);
2297 if (type != 0)
2299 /* Formal parm type is specified by a function prototype. */
2300 tree parmval;
2302 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2304 error ("type of formal parameter %d is incomplete", parmnum + 1);
2305 parmval = val;
2307 else
2309 /* Optionally warn about conversions that
2310 differ from the default conversions. */
2311 if (warn_conversion || warn_traditional)
2313 unsigned int formal_prec = TYPE_PRECISION (type);
2315 if (INTEGRAL_TYPE_P (type)
2316 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2317 warning (0, "passing argument %d of %qE as integer "
2318 "rather than floating due to prototype",
2319 argnum, rname);
2320 if (INTEGRAL_TYPE_P (type)
2321 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2322 warning (0, "passing argument %d of %qE as integer "
2323 "rather than complex due to prototype",
2324 argnum, rname);
2325 else if (TREE_CODE (type) == COMPLEX_TYPE
2326 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2327 warning (0, "passing argument %d of %qE as complex "
2328 "rather than floating due to prototype",
2329 argnum, rname);
2330 else if (TREE_CODE (type) == REAL_TYPE
2331 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2332 warning (0, "passing argument %d of %qE as floating "
2333 "rather than integer due to prototype",
2334 argnum, rname);
2335 else if (TREE_CODE (type) == COMPLEX_TYPE
2336 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2337 warning (0, "passing argument %d of %qE as complex "
2338 "rather than integer due to prototype",
2339 argnum, rname);
2340 else if (TREE_CODE (type) == REAL_TYPE
2341 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2342 warning (0, "passing argument %d of %qE as floating "
2343 "rather than complex due to prototype",
2344 argnum, rname);
2345 /* ??? At some point, messages should be written about
2346 conversions between complex types, but that's too messy
2347 to do now. */
2348 else if (TREE_CODE (type) == REAL_TYPE
2349 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2351 /* Warn if any argument is passed as `float',
2352 since without a prototype it would be `double'. */
2353 if (formal_prec == TYPE_PRECISION (float_type_node))
2354 warning (0, "passing argument %d of %qE as %<float%> "
2355 "rather than %<double%> due to prototype",
2356 argnum, rname);
2358 /* Detect integer changing in width or signedness.
2359 These warnings are only activated with
2360 -Wconversion, not with -Wtraditional. */
2361 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2362 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2364 tree would_have_been = default_conversion (val);
2365 tree type1 = TREE_TYPE (would_have_been);
2367 if (TREE_CODE (type) == ENUMERAL_TYPE
2368 && (TYPE_MAIN_VARIANT (type)
2369 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2370 /* No warning if function asks for enum
2371 and the actual arg is that enum type. */
2373 else if (formal_prec != TYPE_PRECISION (type1))
2374 warning (OPT_Wconversion, "passing argument %d of %qE "
2375 "with different width due to prototype",
2376 argnum, rname);
2377 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2379 /* Don't complain if the formal parameter type
2380 is an enum, because we can't tell now whether
2381 the value was an enum--even the same enum. */
2382 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2384 else if (TREE_CODE (val) == INTEGER_CST
2385 && int_fits_type_p (val, type))
2386 /* Change in signedness doesn't matter
2387 if a constant value is unaffected. */
2389 /* If the value is extended from a narrower
2390 unsigned type, it doesn't matter whether we
2391 pass it as signed or unsigned; the value
2392 certainly is the same either way. */
2393 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2394 && TYPE_UNSIGNED (TREE_TYPE (val)))
2396 else if (TYPE_UNSIGNED (type))
2397 warning (OPT_Wconversion, "passing argument %d of %qE "
2398 "as unsigned due to prototype",
2399 argnum, rname);
2400 else
2401 warning (OPT_Wconversion, "passing argument %d of %qE "
2402 "as signed due to prototype", argnum, rname);
2406 parmval = convert_for_assignment (type, val, ic_argpass,
2407 fundecl, function,
2408 parmnum + 1);
2410 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2411 && INTEGRAL_TYPE_P (type)
2412 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2413 parmval = default_conversion (parmval);
2415 result = tree_cons (NULL_TREE, parmval, result);
2417 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2418 && (TYPE_PRECISION (TREE_TYPE (val))
2419 < TYPE_PRECISION (double_type_node)))
2420 /* Convert `float' to `double'. */
2421 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2422 else if ((invalid_func_diag =
2423 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2425 error (invalid_func_diag);
2426 return error_mark_node;
2428 else
2429 /* Convert `short' and `char' to full-size `int'. */
2430 result = tree_cons (NULL_TREE, default_conversion (val), result);
2432 if (typetail)
2433 typetail = TREE_CHAIN (typetail);
2436 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2438 error ("too few arguments to function %qE", function);
2439 return error_mark_node;
2442 return nreverse (result);
2445 /* This is the entry point used by the parser to build unary operators
2446 in the input. CODE, a tree_code, specifies the unary operator, and
2447 ARG is the operand. For unary plus, the C parser currently uses
2448 CONVERT_EXPR for code. */
2450 struct c_expr
2451 parser_build_unary_op (enum tree_code code, struct c_expr arg)
2453 struct c_expr result;
2455 result.original_code = ERROR_MARK;
2456 result.value = build_unary_op (code, arg.value, 0);
2457 overflow_warning (result.value);
2458 return result;
2461 /* This is the entry point used by the parser to build binary operators
2462 in the input. CODE, a tree_code, specifies the binary operator, and
2463 ARG1 and ARG2 are the operands. In addition to constructing the
2464 expression, we check for operands that were written with other binary
2465 operators in a way that is likely to confuse the user. */
2467 struct c_expr
2468 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2469 struct c_expr arg2)
2471 struct c_expr result;
2473 enum tree_code code1 = arg1.original_code;
2474 enum tree_code code2 = arg2.original_code;
2476 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2477 result.original_code = code;
2479 if (TREE_CODE (result.value) == ERROR_MARK)
2480 return result;
2482 /* Check for cases such as x+y<<z which users are likely
2483 to misinterpret. */
2484 if (warn_parentheses)
2486 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2488 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2489 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2490 warning (OPT_Wparentheses,
2491 "suggest parentheses around + or - inside shift");
2494 if (code == TRUTH_ORIF_EXPR)
2496 if (code1 == TRUTH_ANDIF_EXPR
2497 || code2 == TRUTH_ANDIF_EXPR)
2498 warning (OPT_Wparentheses,
2499 "suggest parentheses around && within ||");
2502 if (code == BIT_IOR_EXPR)
2504 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2505 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2506 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2507 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2508 warning (OPT_Wparentheses,
2509 "suggest parentheses around arithmetic in operand of |");
2510 /* Check cases like x|y==z */
2511 if (TREE_CODE_CLASS (code1) == tcc_comparison
2512 || TREE_CODE_CLASS (code2) == tcc_comparison)
2513 warning (OPT_Wparentheses,
2514 "suggest parentheses around comparison in operand of |");
2517 if (code == BIT_XOR_EXPR)
2519 if (code1 == BIT_AND_EXPR
2520 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2521 || code2 == BIT_AND_EXPR
2522 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2523 warning (OPT_Wparentheses,
2524 "suggest parentheses around arithmetic in operand of ^");
2525 /* Check cases like x^y==z */
2526 if (TREE_CODE_CLASS (code1) == tcc_comparison
2527 || TREE_CODE_CLASS (code2) == tcc_comparison)
2528 warning (OPT_Wparentheses,
2529 "suggest parentheses around comparison in operand of ^");
2532 if (code == BIT_AND_EXPR)
2534 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2535 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2536 warning (OPT_Wparentheses,
2537 "suggest parentheses around + or - in operand of &");
2538 /* Check cases like x&y==z */
2539 if (TREE_CODE_CLASS (code1) == tcc_comparison
2540 || TREE_CODE_CLASS (code2) == tcc_comparison)
2541 warning (OPT_Wparentheses,
2542 "suggest parentheses around comparison in operand of &");
2544 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2545 if (TREE_CODE_CLASS (code) == tcc_comparison
2546 && (TREE_CODE_CLASS (code1) == tcc_comparison
2547 || TREE_CODE_CLASS (code2) == tcc_comparison))
2548 warning (OPT_Wparentheses, "comparisons like X<=Y<=Z do not "
2549 "have their mathematical meaning");
2553 unsigned_conversion_warning (result.value, arg1.value);
2554 unsigned_conversion_warning (result.value, arg2.value);
2555 overflow_warning (result.value);
2557 return result;
2560 /* Return a tree for the difference of pointers OP0 and OP1.
2561 The resulting tree has type int. */
2563 static tree
2564 pointer_diff (tree op0, tree op1)
2566 tree restype = ptrdiff_type_node;
2568 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2569 tree con0, con1, lit0, lit1;
2570 tree orig_op1 = op1;
2572 if (pedantic || warn_pointer_arith)
2574 if (TREE_CODE (target_type) == VOID_TYPE)
2575 pedwarn ("pointer of type %<void *%> used in subtraction");
2576 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2577 pedwarn ("pointer to a function used in subtraction");
2580 /* If the conversion to ptrdiff_type does anything like widening or
2581 converting a partial to an integral mode, we get a convert_expression
2582 that is in the way to do any simplifications.
2583 (fold-const.c doesn't know that the extra bits won't be needed.
2584 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2585 different mode in place.)
2586 So first try to find a common term here 'by hand'; we want to cover
2587 at least the cases that occur in legal static initializers. */
2588 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2589 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2591 if (TREE_CODE (con0) == PLUS_EXPR)
2593 lit0 = TREE_OPERAND (con0, 1);
2594 con0 = TREE_OPERAND (con0, 0);
2596 else
2597 lit0 = integer_zero_node;
2599 if (TREE_CODE (con1) == PLUS_EXPR)
2601 lit1 = TREE_OPERAND (con1, 1);
2602 con1 = TREE_OPERAND (con1, 0);
2604 else
2605 lit1 = integer_zero_node;
2607 if (operand_equal_p (con0, con1, 0))
2609 op0 = lit0;
2610 op1 = lit1;
2614 /* First do the subtraction as integers;
2615 then drop through to build the divide operator.
2616 Do not do default conversions on the minus operator
2617 in case restype is a short type. */
2619 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2620 convert (restype, op1), 0);
2621 /* This generates an error if op1 is pointer to incomplete type. */
2622 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2623 error ("arithmetic on pointer to an incomplete type");
2625 /* This generates an error if op0 is pointer to incomplete type. */
2626 op1 = c_size_in_bytes (target_type);
2628 /* Divide by the size, in easiest possible way. */
2629 return fold_build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2632 /* Construct and perhaps optimize a tree representation
2633 for a unary operation. CODE, a tree_code, specifies the operation
2634 and XARG is the operand.
2635 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2636 the default promotions (such as from short to int).
2637 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2638 allows non-lvalues; this is only used to handle conversion of non-lvalue
2639 arrays to pointers in C99. */
2641 tree
2642 build_unary_op (enum tree_code code, tree xarg, int flag)
2644 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2645 tree arg = xarg;
2646 tree argtype = 0;
2647 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2648 tree val;
2649 int noconvert = flag;
2650 const char *invalid_op_diag;
2652 if (typecode == ERROR_MARK)
2653 return error_mark_node;
2654 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2655 typecode = INTEGER_TYPE;
2657 if ((invalid_op_diag
2658 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
2660 error (invalid_op_diag);
2661 return error_mark_node;
2664 switch (code)
2666 case CONVERT_EXPR:
2667 /* This is used for unary plus, because a CONVERT_EXPR
2668 is enough to prevent anybody from looking inside for
2669 associativity, but won't generate any code. */
2670 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2671 || typecode == COMPLEX_TYPE
2672 || typecode == VECTOR_TYPE))
2674 error ("wrong type argument to unary plus");
2675 return error_mark_node;
2677 else if (!noconvert)
2678 arg = default_conversion (arg);
2679 arg = non_lvalue (arg);
2680 break;
2682 case NEGATE_EXPR:
2683 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2684 || typecode == COMPLEX_TYPE
2685 || typecode == VECTOR_TYPE))
2687 error ("wrong type argument to unary minus");
2688 return error_mark_node;
2690 else if (!noconvert)
2691 arg = default_conversion (arg);
2692 break;
2694 case BIT_NOT_EXPR:
2695 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2697 if (!noconvert)
2698 arg = default_conversion (arg);
2700 else if (typecode == COMPLEX_TYPE)
2702 code = CONJ_EXPR;
2703 if (pedantic)
2704 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2705 if (!noconvert)
2706 arg = default_conversion (arg);
2708 else
2710 error ("wrong type argument to bit-complement");
2711 return error_mark_node;
2713 break;
2715 case ABS_EXPR:
2716 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2718 error ("wrong type argument to abs");
2719 return error_mark_node;
2721 else if (!noconvert)
2722 arg = default_conversion (arg);
2723 break;
2725 case CONJ_EXPR:
2726 /* Conjugating a real value is a no-op, but allow it anyway. */
2727 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2728 || typecode == COMPLEX_TYPE))
2730 error ("wrong type argument to conjugation");
2731 return error_mark_node;
2733 else if (!noconvert)
2734 arg = default_conversion (arg);
2735 break;
2737 case TRUTH_NOT_EXPR:
2738 if (typecode != INTEGER_TYPE
2739 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2740 && typecode != COMPLEX_TYPE)
2742 error ("wrong type argument to unary exclamation mark");
2743 return error_mark_node;
2745 arg = c_objc_common_truthvalue_conversion (arg);
2746 return invert_truthvalue (arg);
2748 case NOP_EXPR:
2749 break;
2751 case REALPART_EXPR:
2752 if (TREE_CODE (arg) == COMPLEX_CST)
2753 return TREE_REALPART (arg);
2754 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2755 return fold_build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2756 else
2757 return arg;
2759 case IMAGPART_EXPR:
2760 if (TREE_CODE (arg) == COMPLEX_CST)
2761 return TREE_IMAGPART (arg);
2762 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2763 return fold_build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
2764 else
2765 return convert (TREE_TYPE (arg), integer_zero_node);
2767 case PREINCREMENT_EXPR:
2768 case POSTINCREMENT_EXPR:
2769 case PREDECREMENT_EXPR:
2770 case POSTDECREMENT_EXPR:
2772 /* Increment or decrement the real part of the value,
2773 and don't change the imaginary part. */
2774 if (typecode == COMPLEX_TYPE)
2776 tree real, imag;
2778 if (pedantic)
2779 pedwarn ("ISO C does not support %<++%> and %<--%>"
2780 " on complex types");
2782 arg = stabilize_reference (arg);
2783 real = build_unary_op (REALPART_EXPR, arg, 1);
2784 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2785 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2786 build_unary_op (code, real, 1), imag);
2789 /* Report invalid types. */
2791 if (typecode != POINTER_TYPE
2792 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2794 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2795 error ("wrong type argument to increment");
2796 else
2797 error ("wrong type argument to decrement");
2799 return error_mark_node;
2803 tree inc;
2804 tree result_type = TREE_TYPE (arg);
2806 arg = get_unwidened (arg, 0);
2807 argtype = TREE_TYPE (arg);
2809 /* Compute the increment. */
2811 if (typecode == POINTER_TYPE)
2813 /* If pointer target is an undefined struct,
2814 we just cannot know how to do the arithmetic. */
2815 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2817 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2818 error ("increment of pointer to unknown structure");
2819 else
2820 error ("decrement of pointer to unknown structure");
2822 else if ((pedantic || warn_pointer_arith)
2823 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2824 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2826 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2827 pedwarn ("wrong type argument to increment");
2828 else
2829 pedwarn ("wrong type argument to decrement");
2832 inc = c_size_in_bytes (TREE_TYPE (result_type));
2834 else
2835 inc = integer_one_node;
2837 inc = convert (argtype, inc);
2839 /* Complain about anything else that is not a true lvalue. */
2840 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2841 || code == POSTINCREMENT_EXPR)
2842 ? lv_increment
2843 : lv_decrement)))
2844 return error_mark_node;
2846 /* Report a read-only lvalue. */
2847 if (TREE_READONLY (arg))
2848 readonly_error (arg,
2849 ((code == PREINCREMENT_EXPR
2850 || code == POSTINCREMENT_EXPR)
2851 ? lv_increment : lv_decrement));
2853 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2854 val = boolean_increment (code, arg);
2855 else
2856 val = build2 (code, TREE_TYPE (arg), arg, inc);
2857 TREE_SIDE_EFFECTS (val) = 1;
2858 val = convert (result_type, val);
2859 if (TREE_CODE (val) != code)
2860 TREE_NO_WARNING (val) = 1;
2861 return val;
2864 case ADDR_EXPR:
2865 /* Note that this operation never does default_conversion. */
2867 /* Let &* cancel out to simplify resulting code. */
2868 if (TREE_CODE (arg) == INDIRECT_REF)
2870 /* Don't let this be an lvalue. */
2871 if (lvalue_p (TREE_OPERAND (arg, 0)))
2872 return non_lvalue (TREE_OPERAND (arg, 0));
2873 return TREE_OPERAND (arg, 0);
2876 /* For &x[y], return x+y */
2877 if (TREE_CODE (arg) == ARRAY_REF)
2879 tree op0 = TREE_OPERAND (arg, 0);
2880 if (!c_mark_addressable (op0))
2881 return error_mark_node;
2882 return build_binary_op (PLUS_EXPR,
2883 (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
2884 ? array_to_pointer_conversion (op0)
2885 : op0),
2886 TREE_OPERAND (arg, 1), 1);
2889 /* Anything not already handled and not a true memory reference
2890 or a non-lvalue array is an error. */
2891 else if (typecode != FUNCTION_TYPE && !flag
2892 && !lvalue_or_else (arg, lv_addressof))
2893 return error_mark_node;
2895 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2896 argtype = TREE_TYPE (arg);
2898 /* If the lvalue is const or volatile, merge that into the type
2899 to which the address will point. Note that you can't get a
2900 restricted pointer by taking the address of something, so we
2901 only have to deal with `const' and `volatile' here. */
2902 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2903 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2904 argtype = c_build_type_variant (argtype,
2905 TREE_READONLY (arg),
2906 TREE_THIS_VOLATILE (arg));
2908 if (!c_mark_addressable (arg))
2909 return error_mark_node;
2911 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2912 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2914 argtype = build_pointer_type (argtype);
2916 /* ??? Cope with user tricks that amount to offsetof. Delete this
2917 when we have proper support for integer constant expressions. */
2918 val = get_base_address (arg);
2919 if (val && TREE_CODE (val) == INDIRECT_REF
2920 && integer_zerop (TREE_OPERAND (val, 0)))
2921 return fold_convert (argtype, fold_offsetof (arg));
2923 val = build1 (ADDR_EXPR, argtype, arg);
2925 return val;
2927 default:
2928 break;
2931 if (argtype == 0)
2932 argtype = TREE_TYPE (arg);
2933 return require_constant_value ? fold_build1_initializer (code, argtype, arg)
2934 : fold_build1 (code, argtype, arg);
2937 /* Return nonzero if REF is an lvalue valid for this language.
2938 Lvalues can be assigned, unless their type has TYPE_READONLY.
2939 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2941 static int
2942 lvalue_p (tree ref)
2944 enum tree_code code = TREE_CODE (ref);
2946 switch (code)
2948 case REALPART_EXPR:
2949 case IMAGPART_EXPR:
2950 case COMPONENT_REF:
2951 return lvalue_p (TREE_OPERAND (ref, 0));
2953 case COMPOUND_LITERAL_EXPR:
2954 case STRING_CST:
2955 return 1;
2957 case INDIRECT_REF:
2958 case ARRAY_REF:
2959 case VAR_DECL:
2960 case PARM_DECL:
2961 case RESULT_DECL:
2962 case ERROR_MARK:
2963 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2964 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2966 case BIND_EXPR:
2967 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2969 default:
2970 return 0;
2974 /* Give an error for storing in something that is 'const'. */
2976 static void
2977 readonly_error (tree arg, enum lvalue_use use)
2979 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
2980 || use == lv_asm);
2981 /* Using this macro rather than (for example) arrays of messages
2982 ensures that all the format strings are checked at compile
2983 time. */
2984 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
2985 : (use == lv_increment ? (I) \
2986 : (use == lv_decrement ? (D) : (AS))))
2987 if (TREE_CODE (arg) == COMPONENT_REF)
2989 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2990 readonly_error (TREE_OPERAND (arg, 0), use);
2991 else
2992 error (READONLY_MSG (G_("assignment of read-only member %qD"),
2993 G_("increment of read-only member %qD"),
2994 G_("decrement of read-only member %qD"),
2995 G_("read-only member %qD used as %<asm%> output")),
2996 TREE_OPERAND (arg, 1));
2998 else if (TREE_CODE (arg) == VAR_DECL)
2999 error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3000 G_("increment of read-only variable %qD"),
3001 G_("decrement of read-only variable %qD"),
3002 G_("read-only variable %qD used as %<asm%> output")),
3003 arg);
3004 else
3005 error (READONLY_MSG (G_("assignment of read-only location"),
3006 G_("increment of read-only location"),
3007 G_("decrement of read-only location"),
3008 G_("read-only location used as %<asm%> output")));
3012 /* Return nonzero if REF is an lvalue valid for this language;
3013 otherwise, print an error message and return zero. USE says
3014 how the lvalue is being used and so selects the error message. */
3016 static int
3017 lvalue_or_else (tree ref, enum lvalue_use use)
3019 int win = lvalue_p (ref);
3021 if (!win)
3022 lvalue_error (use);
3024 return win;
3027 /* Mark EXP saying that we need to be able to take the
3028 address of it; it should not be allocated in a register.
3029 Returns true if successful. */
3031 bool
3032 c_mark_addressable (tree exp)
3034 tree x = exp;
3036 while (1)
3037 switch (TREE_CODE (x))
3039 case COMPONENT_REF:
3040 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3042 error
3043 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3044 return false;
3047 /* ... fall through ... */
3049 case ADDR_EXPR:
3050 case ARRAY_REF:
3051 case REALPART_EXPR:
3052 case IMAGPART_EXPR:
3053 x = TREE_OPERAND (x, 0);
3054 break;
3056 case COMPOUND_LITERAL_EXPR:
3057 case CONSTRUCTOR:
3058 TREE_ADDRESSABLE (x) = 1;
3059 return true;
3061 case VAR_DECL:
3062 case CONST_DECL:
3063 case PARM_DECL:
3064 case RESULT_DECL:
3065 if (C_DECL_REGISTER (x)
3066 && DECL_NONLOCAL (x))
3068 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3070 error
3071 ("global register variable %qD used in nested function", x);
3072 return false;
3074 pedwarn ("register variable %qD used in nested function", x);
3076 else if (C_DECL_REGISTER (x))
3078 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3079 error ("address of global register variable %qD requested", x);
3080 else
3081 error ("address of register variable %qD requested", x);
3082 return false;
3085 /* drops in */
3086 case FUNCTION_DECL:
3087 TREE_ADDRESSABLE (x) = 1;
3088 /* drops out */
3089 default:
3090 return true;
3094 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3096 tree
3097 build_conditional_expr (tree ifexp, tree op1, tree op2)
3099 tree type1;
3100 tree type2;
3101 enum tree_code code1;
3102 enum tree_code code2;
3103 tree result_type = NULL;
3104 tree orig_op1 = op1, orig_op2 = op2;
3106 /* Promote both alternatives. */
3108 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3109 op1 = default_conversion (op1);
3110 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3111 op2 = default_conversion (op2);
3113 if (TREE_CODE (ifexp) == ERROR_MARK
3114 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3115 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3116 return error_mark_node;
3118 type1 = TREE_TYPE (op1);
3119 code1 = TREE_CODE (type1);
3120 type2 = TREE_TYPE (op2);
3121 code2 = TREE_CODE (type2);
3123 /* C90 does not permit non-lvalue arrays in conditional expressions.
3124 In C99 they will be pointers by now. */
3125 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
3127 error ("non-lvalue array in conditional expression");
3128 return error_mark_node;
3131 /* Quickly detect the usual case where op1 and op2 have the same type
3132 after promotion. */
3133 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3135 if (type1 == type2)
3136 result_type = type1;
3137 else
3138 result_type = TYPE_MAIN_VARIANT (type1);
3140 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3141 || code1 == COMPLEX_TYPE)
3142 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3143 || code2 == COMPLEX_TYPE))
3145 result_type = c_common_type (type1, type2);
3147 /* If -Wsign-compare, warn here if type1 and type2 have
3148 different signedness. We'll promote the signed to unsigned
3149 and later code won't know it used to be different.
3150 Do this check on the original types, so that explicit casts
3151 will be considered, but default promotions won't. */
3152 if (warn_sign_compare && !skip_evaluation)
3154 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
3155 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
3157 if (unsigned_op1 ^ unsigned_op2)
3159 /* Do not warn if the result type is signed, since the
3160 signed type will only be chosen if it can represent
3161 all the values of the unsigned type. */
3162 if (!TYPE_UNSIGNED (result_type))
3163 /* OK */;
3164 /* Do not warn if the signed quantity is an unsuffixed
3165 integer literal (or some static constant expression
3166 involving such literals) and it is non-negative. */
3167 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3168 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3169 /* OK */;
3170 else
3171 warning (0, "signed and unsigned type in conditional expression");
3175 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3177 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3178 pedwarn ("ISO C forbids conditional expr with only one void side");
3179 result_type = void_type_node;
3181 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3183 if (comp_target_types (type1, type2))
3184 result_type = common_pointer_type (type1, type2);
3185 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3186 && TREE_CODE (orig_op1) != NOP_EXPR)
3187 result_type = qualify_type (type2, type1);
3188 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3189 && TREE_CODE (orig_op2) != NOP_EXPR)
3190 result_type = qualify_type (type1, type2);
3191 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3193 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3194 pedwarn ("ISO C forbids conditional expr between "
3195 "%<void *%> and function pointer");
3196 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3197 TREE_TYPE (type2)));
3199 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3201 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3202 pedwarn ("ISO C forbids conditional expr between "
3203 "%<void *%> and function pointer");
3204 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3205 TREE_TYPE (type1)));
3207 else
3209 pedwarn ("pointer type mismatch in conditional expression");
3210 result_type = build_pointer_type (void_type_node);
3213 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3215 if (!integer_zerop (op2))
3216 pedwarn ("pointer/integer type mismatch in conditional expression");
3217 else
3219 op2 = null_pointer_node;
3221 result_type = type1;
3223 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3225 if (!integer_zerop (op1))
3226 pedwarn ("pointer/integer type mismatch in conditional expression");
3227 else
3229 op1 = null_pointer_node;
3231 result_type = type2;
3234 if (!result_type)
3236 if (flag_cond_mismatch)
3237 result_type = void_type_node;
3238 else
3240 error ("type mismatch in conditional expression");
3241 return error_mark_node;
3245 /* Merge const and volatile flags of the incoming types. */
3246 result_type
3247 = build_type_variant (result_type,
3248 TREE_READONLY (op1) || TREE_READONLY (op2),
3249 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3251 if (result_type != TREE_TYPE (op1))
3252 op1 = convert_and_check (result_type, op1);
3253 if (result_type != TREE_TYPE (op2))
3254 op2 = convert_and_check (result_type, op2);
3256 return fold_build3 (COND_EXPR, result_type, ifexp, op1, op2);
3259 /* Return a compound expression that performs two expressions and
3260 returns the value of the second of them. */
3262 tree
3263 build_compound_expr (tree expr1, tree expr2)
3265 if (!TREE_SIDE_EFFECTS (expr1))
3267 /* The left-hand operand of a comma expression is like an expression
3268 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3269 any side-effects, unless it was explicitly cast to (void). */
3270 if (warn_unused_value)
3272 if (VOID_TYPE_P (TREE_TYPE (expr1))
3273 && TREE_CODE (expr1) == CONVERT_EXPR)
3274 ; /* (void) a, b */
3275 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3276 && TREE_CODE (expr1) == COMPOUND_EXPR
3277 && TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR)
3278 ; /* (void) a, (void) b, c */
3279 else
3280 warning (0, "left-hand operand of comma expression has no effect");
3284 /* With -Wunused, we should also warn if the left-hand operand does have
3285 side-effects, but computes a value which is not used. For example, in
3286 `foo() + bar(), baz()' the result of the `+' operator is not used,
3287 so we should issue a warning. */
3288 else if (warn_unused_value)
3289 warn_if_unused_value (expr1, input_location);
3291 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3294 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3296 tree
3297 build_c_cast (tree type, tree expr)
3299 tree value = expr;
3301 if (type == error_mark_node || expr == error_mark_node)
3302 return error_mark_node;
3304 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3305 only in <protocol> qualifications. But when constructing cast expressions,
3306 the protocols do matter and must be kept around. */
3307 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3308 return build1 (NOP_EXPR, type, expr);
3310 type = TYPE_MAIN_VARIANT (type);
3312 if (TREE_CODE (type) == ARRAY_TYPE)
3314 error ("cast specifies array type");
3315 return error_mark_node;
3318 if (TREE_CODE (type) == FUNCTION_TYPE)
3320 error ("cast specifies function type");
3321 return error_mark_node;
3324 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3326 if (pedantic)
3328 if (TREE_CODE (type) == RECORD_TYPE
3329 || TREE_CODE (type) == UNION_TYPE)
3330 pedwarn ("ISO C forbids casting nonscalar to the same type");
3333 else if (TREE_CODE (type) == UNION_TYPE)
3335 tree field;
3337 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3338 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3339 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3340 break;
3342 if (field)
3344 tree t;
3346 if (pedantic)
3347 pedwarn ("ISO C forbids casts to union type");
3348 t = digest_init (type,
3349 build_constructor_single (type, field, value),
3350 true, 0);
3351 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3352 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3353 return t;
3355 error ("cast to union type from type not present in union");
3356 return error_mark_node;
3358 else
3360 tree otype, ovalue;
3362 if (type == void_type_node)
3363 return build1 (CONVERT_EXPR, type, value);
3365 otype = TREE_TYPE (value);
3367 /* Optionally warn about potentially worrisome casts. */
3369 if (warn_cast_qual
3370 && TREE_CODE (type) == POINTER_TYPE
3371 && TREE_CODE (otype) == POINTER_TYPE)
3373 tree in_type = type;
3374 tree in_otype = otype;
3375 int added = 0;
3376 int discarded = 0;
3378 /* Check that the qualifiers on IN_TYPE are a superset of
3379 the qualifiers of IN_OTYPE. The outermost level of
3380 POINTER_TYPE nodes is uninteresting and we stop as soon
3381 as we hit a non-POINTER_TYPE node on either type. */
3384 in_otype = TREE_TYPE (in_otype);
3385 in_type = TREE_TYPE (in_type);
3387 /* GNU C allows cv-qualified function types. 'const'
3388 means the function is very pure, 'volatile' means it
3389 can't return. We need to warn when such qualifiers
3390 are added, not when they're taken away. */
3391 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3392 && TREE_CODE (in_type) == FUNCTION_TYPE)
3393 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3394 else
3395 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3397 while (TREE_CODE (in_type) == POINTER_TYPE
3398 && TREE_CODE (in_otype) == POINTER_TYPE);
3400 if (added)
3401 warning (0, "cast adds new qualifiers to function type");
3403 if (discarded)
3404 /* There are qualifiers present in IN_OTYPE that are not
3405 present in IN_TYPE. */
3406 warning (0, "cast discards qualifiers from pointer target type");
3409 /* Warn about possible alignment problems. */
3410 if (STRICT_ALIGNMENT
3411 && TREE_CODE (type) == POINTER_TYPE
3412 && TREE_CODE (otype) == POINTER_TYPE
3413 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3414 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3415 /* Don't warn about opaque types, where the actual alignment
3416 restriction is unknown. */
3417 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3418 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3419 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3420 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3421 warning (OPT_Wcast_align,
3422 "cast increases required alignment of target type");
3424 if (TREE_CODE (type) == INTEGER_TYPE
3425 && TREE_CODE (otype) == POINTER_TYPE
3426 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3427 && !TREE_CONSTANT (value))
3428 warning (OPT_Wpointer_to_int_cast,
3429 "cast from pointer to integer of different size");
3431 if (TREE_CODE (value) == CALL_EXPR
3432 && TREE_CODE (type) != TREE_CODE (otype))
3433 warning (OPT_Wbad_function_cast, "cast from function call of type %qT "
3434 "to non-matching type %qT", otype, type);
3436 if (TREE_CODE (type) == POINTER_TYPE
3437 && TREE_CODE (otype) == INTEGER_TYPE
3438 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3439 /* Don't warn about converting any constant. */
3440 && !TREE_CONSTANT (value))
3441 warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer "
3442 "of different size");
3444 if (flag_strict_aliasing && warn_strict_aliasing
3445 && TREE_CODE (type) == POINTER_TYPE
3446 && TREE_CODE (otype) == POINTER_TYPE
3447 && TREE_CODE (expr) == ADDR_EXPR
3448 && (DECL_P (TREE_OPERAND (expr, 0))
3449 || TREE_CODE (TREE_OPERAND (expr, 0)) == COMPONENT_REF)
3450 && !VOID_TYPE_P (TREE_TYPE (type)))
3452 /* Casting the address of an object to non void pointer. Warn
3453 if the cast breaks type based aliasing. */
3454 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3455 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
3456 "might break strict-aliasing rules");
3457 else
3459 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3460 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3462 if (!alias_sets_conflict_p (set1, set2))
3463 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
3464 "pointer will break strict-aliasing rules");
3465 else if (warn_strict_aliasing > 1
3466 && !alias_sets_might_conflict_p (set1, set2))
3467 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
3468 "pointer might break strict-aliasing rules");
3472 /* If pedantic, warn for conversions between function and object
3473 pointer types, except for converting a null pointer constant
3474 to function pointer type. */
3475 if (pedantic
3476 && TREE_CODE (type) == POINTER_TYPE
3477 && TREE_CODE (otype) == POINTER_TYPE
3478 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3479 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3480 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3482 if (pedantic
3483 && TREE_CODE (type) == POINTER_TYPE
3484 && TREE_CODE (otype) == POINTER_TYPE
3485 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3486 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3487 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3488 && TREE_CODE (expr) != NOP_EXPR))
3489 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3491 ovalue = value;
3492 value = convert (type, value);
3494 /* Ignore any integer overflow caused by the cast. */
3495 if (TREE_CODE (value) == INTEGER_CST)
3497 if (CONSTANT_CLASS_P (ovalue)
3498 && (TREE_OVERFLOW (ovalue) || TREE_CONSTANT_OVERFLOW (ovalue)))
3500 /* Avoid clobbering a shared constant. */
3501 value = copy_node (value);
3502 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3503 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3505 else if (TREE_OVERFLOW (value) || TREE_CONSTANT_OVERFLOW (value))
3506 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3507 value = build_int_cst_wide (TREE_TYPE (value),
3508 TREE_INT_CST_LOW (value),
3509 TREE_INT_CST_HIGH (value));
3513 /* Don't let a cast be an lvalue. */
3514 if (value == expr)
3515 value = non_lvalue (value);
3517 return value;
3520 /* Interpret a cast of expression EXPR to type TYPE. */
3521 tree
3522 c_cast_expr (struct c_type_name *type_name, tree expr)
3524 tree type;
3525 int saved_wsp = warn_strict_prototypes;
3527 /* This avoids warnings about unprototyped casts on
3528 integers. E.g. "#define SIG_DFL (void(*)())0". */
3529 if (TREE_CODE (expr) == INTEGER_CST)
3530 warn_strict_prototypes = 0;
3531 type = groktypename (type_name);
3532 warn_strict_prototypes = saved_wsp;
3534 return build_c_cast (type, expr);
3538 /* Build an assignment expression of lvalue LHS from value RHS.
3539 MODIFYCODE is the code for a binary operator that we use
3540 to combine the old value of LHS with RHS to get the new value.
3541 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3543 tree
3544 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3546 tree result;
3547 tree newrhs;
3548 tree lhstype = TREE_TYPE (lhs);
3549 tree olhstype = lhstype;
3551 /* Types that aren't fully specified cannot be used in assignments. */
3552 lhs = require_complete_type (lhs);
3554 /* Avoid duplicate error messages from operands that had errors. */
3555 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3556 return error_mark_node;
3558 STRIP_TYPE_NOPS (rhs);
3560 newrhs = rhs;
3562 /* If a binary op has been requested, combine the old LHS value with the RHS
3563 producing the value we should actually store into the LHS. */
3565 if (modifycode != NOP_EXPR)
3567 lhs = stabilize_reference (lhs);
3568 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3571 if (!lvalue_or_else (lhs, lv_assign))
3572 return error_mark_node;
3574 /* Give an error for storing in something that is 'const'. */
3576 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3577 || ((TREE_CODE (lhstype) == RECORD_TYPE
3578 || TREE_CODE (lhstype) == UNION_TYPE)
3579 && C_TYPE_FIELDS_READONLY (lhstype)))
3580 readonly_error (lhs, lv_assign);
3582 /* If storing into a structure or union member,
3583 it has probably been given type `int'.
3584 Compute the type that would go with
3585 the actual amount of storage the member occupies. */
3587 if (TREE_CODE (lhs) == COMPONENT_REF
3588 && (TREE_CODE (lhstype) == INTEGER_TYPE
3589 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3590 || TREE_CODE (lhstype) == REAL_TYPE
3591 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3592 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3594 /* If storing in a field that is in actuality a short or narrower than one,
3595 we must store in the field in its actual type. */
3597 if (lhstype != TREE_TYPE (lhs))
3599 lhs = copy_node (lhs);
3600 TREE_TYPE (lhs) = lhstype;
3603 /* Convert new value to destination type. */
3605 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3606 NULL_TREE, NULL_TREE, 0);
3607 if (TREE_CODE (newrhs) == ERROR_MARK)
3608 return error_mark_node;
3610 /* Emit ObjC write barrier, if necessary. */
3611 if (c_dialect_objc () && flag_objc_gc)
3613 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
3614 if (result)
3615 return result;
3618 /* Scan operands. */
3620 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3621 TREE_SIDE_EFFECTS (result) = 1;
3623 /* If we got the LHS in a different type for storing in,
3624 convert the result back to the nominal type of LHS
3625 so that the value we return always has the same type
3626 as the LHS argument. */
3628 if (olhstype == TREE_TYPE (result))
3629 return result;
3630 return convert_for_assignment (olhstype, result, ic_assign,
3631 NULL_TREE, NULL_TREE, 0);
3634 /* Convert value RHS to type TYPE as preparation for an assignment
3635 to an lvalue of type TYPE.
3636 The real work of conversion is done by `convert'.
3637 The purpose of this function is to generate error messages
3638 for assignments that are not allowed in C.
3639 ERRTYPE says whether it is argument passing, assignment,
3640 initialization or return.
3642 FUNCTION is a tree for the function being called.
3643 PARMNUM is the number of the argument, for printing in error messages. */
3645 static tree
3646 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3647 tree fundecl, tree function, int parmnum)
3649 enum tree_code codel = TREE_CODE (type);
3650 tree rhstype;
3651 enum tree_code coder;
3652 tree rname = NULL_TREE;
3653 bool objc_ok = false;
3655 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3657 tree selector;
3658 /* Change pointer to function to the function itself for
3659 diagnostics. */
3660 if (TREE_CODE (function) == ADDR_EXPR
3661 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3662 function = TREE_OPERAND (function, 0);
3664 /* Handle an ObjC selector specially for diagnostics. */
3665 selector = objc_message_selector ();
3666 rname = function;
3667 if (selector && parmnum > 2)
3669 rname = selector;
3670 parmnum -= 2;
3674 /* This macro is used to emit diagnostics to ensure that all format
3675 strings are complete sentences, visible to gettext and checked at
3676 compile time. */
3677 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3678 do { \
3679 switch (errtype) \
3681 case ic_argpass: \
3682 pedwarn (AR, parmnum, rname); \
3683 break; \
3684 case ic_argpass_nonproto: \
3685 warning (0, AR, parmnum, rname); \
3686 break; \
3687 case ic_assign: \
3688 pedwarn (AS); \
3689 break; \
3690 case ic_init: \
3691 pedwarn (IN); \
3692 break; \
3693 case ic_return: \
3694 pedwarn (RE); \
3695 break; \
3696 default: \
3697 gcc_unreachable (); \
3699 } while (0)
3701 STRIP_TYPE_NOPS (rhs);
3703 if (optimize && TREE_CODE (rhs) == VAR_DECL
3704 && TREE_CODE (TREE_TYPE (rhs)) != ARRAY_TYPE)
3705 rhs = decl_constant_value_for_broken_optimization (rhs);
3707 rhstype = TREE_TYPE (rhs);
3708 coder = TREE_CODE (rhstype);
3710 if (coder == ERROR_MARK)
3711 return error_mark_node;
3713 if (c_dialect_objc ())
3715 int parmno;
3717 switch (errtype)
3719 case ic_return:
3720 parmno = 0;
3721 break;
3723 case ic_assign:
3724 parmno = -1;
3725 break;
3727 case ic_init:
3728 parmno = -2;
3729 break;
3731 default:
3732 parmno = parmnum;
3733 break;
3736 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
3739 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3741 overflow_warning (rhs);
3742 return rhs;
3745 if (coder == VOID_TYPE)
3747 /* Except for passing an argument to an unprototyped function,
3748 this is a constraint violation. When passing an argument to
3749 an unprototyped function, it is compile-time undefined;
3750 making it a constraint in that case was rejected in
3751 DR#252. */
3752 error ("void value not ignored as it ought to be");
3753 return error_mark_node;
3755 /* A type converts to a reference to it.
3756 This code doesn't fully support references, it's just for the
3757 special case of va_start and va_copy. */
3758 if (codel == REFERENCE_TYPE
3759 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3761 if (!lvalue_p (rhs))
3763 error ("cannot pass rvalue to reference parameter");
3764 return error_mark_node;
3766 if (!c_mark_addressable (rhs))
3767 return error_mark_node;
3768 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3770 /* We already know that these two types are compatible, but they
3771 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3772 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3773 likely to be va_list, a typedef to __builtin_va_list, which
3774 is different enough that it will cause problems later. */
3775 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3776 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3778 rhs = build1 (NOP_EXPR, type, rhs);
3779 return rhs;
3781 /* Some types can interconvert without explicit casts. */
3782 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3783 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3784 return convert (type, rhs);
3785 /* Arithmetic types all interconvert, and enum is treated like int. */
3786 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3787 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3788 || codel == BOOLEAN_TYPE)
3789 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3790 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3791 || coder == BOOLEAN_TYPE))
3792 return convert_and_check (type, rhs);
3794 /* Conversion to a transparent union from its member types.
3795 This applies only to function arguments. */
3796 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3797 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3799 tree memb, marginal_memb = NULL_TREE;
3801 for (memb = TYPE_FIELDS (type); memb ; memb = TREE_CHAIN (memb))
3803 tree memb_type = TREE_TYPE (memb);
3805 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3806 TYPE_MAIN_VARIANT (rhstype)))
3807 break;
3809 if (TREE_CODE (memb_type) != POINTER_TYPE)
3810 continue;
3812 if (coder == POINTER_TYPE)
3814 tree ttl = TREE_TYPE (memb_type);
3815 tree ttr = TREE_TYPE (rhstype);
3817 /* Any non-function converts to a [const][volatile] void *
3818 and vice versa; otherwise, targets must be the same.
3819 Meanwhile, the lhs target must have all the qualifiers of
3820 the rhs. */
3821 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3822 || comp_target_types (memb_type, rhstype))
3824 /* If this type won't generate any warnings, use it. */
3825 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3826 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3827 && TREE_CODE (ttl) == FUNCTION_TYPE)
3828 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3829 == TYPE_QUALS (ttr))
3830 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3831 == TYPE_QUALS (ttl))))
3832 break;
3834 /* Keep looking for a better type, but remember this one. */
3835 if (!marginal_memb)
3836 marginal_memb = memb;
3840 /* Can convert integer zero to any pointer type. */
3841 if (integer_zerop (rhs)
3842 || (TREE_CODE (rhs) == NOP_EXPR
3843 && integer_zerop (TREE_OPERAND (rhs, 0))))
3845 rhs = null_pointer_node;
3846 break;
3850 if (memb || marginal_memb)
3852 if (!memb)
3854 /* We have only a marginally acceptable member type;
3855 it needs a warning. */
3856 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3857 tree ttr = TREE_TYPE (rhstype);
3859 /* Const and volatile mean something different for function
3860 types, so the usual warnings are not appropriate. */
3861 if (TREE_CODE (ttr) == FUNCTION_TYPE
3862 && TREE_CODE (ttl) == FUNCTION_TYPE)
3864 /* Because const and volatile on functions are
3865 restrictions that say the function will not do
3866 certain things, it is okay to use a const or volatile
3867 function where an ordinary one is wanted, but not
3868 vice-versa. */
3869 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3870 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
3871 "makes qualified function "
3872 "pointer from unqualified"),
3873 G_("assignment makes qualified "
3874 "function pointer from "
3875 "unqualified"),
3876 G_("initialization makes qualified "
3877 "function pointer from "
3878 "unqualified"),
3879 G_("return makes qualified function "
3880 "pointer from unqualified"));
3882 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3883 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
3884 "qualifiers from pointer target type"),
3885 G_("assignment discards qualifiers "
3886 "from pointer target type"),
3887 G_("initialization discards qualifiers "
3888 "from pointer target type"),
3889 G_("return discards qualifiers from "
3890 "pointer target type"));
3892 memb = marginal_memb;
3895 if (pedantic && (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl)))
3896 pedwarn ("ISO C prohibits argument conversion to union type");
3898 return build_constructor_single (type, memb, rhs);
3902 /* Conversions among pointers */
3903 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3904 && (coder == codel))
3906 tree ttl = TREE_TYPE (type);
3907 tree ttr = TREE_TYPE (rhstype);
3908 tree mvl = ttl;
3909 tree mvr = ttr;
3910 bool is_opaque_pointer;
3911 int target_cmp = 0; /* Cache comp_target_types () result. */
3913 if (TREE_CODE (mvl) != ARRAY_TYPE)
3914 mvl = TYPE_MAIN_VARIANT (mvl);
3915 if (TREE_CODE (mvr) != ARRAY_TYPE)
3916 mvr = TYPE_MAIN_VARIANT (mvr);
3917 /* Opaque pointers are treated like void pointers. */
3918 is_opaque_pointer = (targetm.vector_opaque_p (type)
3919 || targetm.vector_opaque_p (rhstype))
3920 && TREE_CODE (ttl) == VECTOR_TYPE
3921 && TREE_CODE (ttr) == VECTOR_TYPE;
3923 /* C++ does not allow the implicit conversion void* -> T*. However,
3924 for the purpose of reducing the number of false positives, we
3925 tolerate the special case of
3927 int *p = NULL;
3929 where NULL is typically defined in C to be '(void *) 0'. */
3930 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
3931 warning (OPT_Wc___compat, "request for implicit conversion from "
3932 "%qT to %qT not permitted in C++", rhstype, type);
3934 /* Check if the right-hand side has a format attribute but the
3935 left-hand side doesn't. */
3936 if (warn_missing_format_attribute
3937 && check_missing_format_attribute (type, rhstype))
3939 switch (errtype)
3941 case ic_argpass:
3942 case ic_argpass_nonproto:
3943 warning (OPT_Wmissing_format_attribute,
3944 "argument %d of %qE might be "
3945 "a candidate for a format attribute",
3946 parmnum, rname);
3947 break;
3948 case ic_assign:
3949 warning (OPT_Wmissing_format_attribute,
3950 "assignment left-hand side might be "
3951 "a candidate for a format attribute");
3952 break;
3953 case ic_init:
3954 warning (OPT_Wmissing_format_attribute,
3955 "initialization left-hand side might be "
3956 "a candidate for a format attribute");
3957 break;
3958 case ic_return:
3959 warning (OPT_Wmissing_format_attribute,
3960 "return type might be "
3961 "a candidate for a format attribute");
3962 break;
3963 default:
3964 gcc_unreachable ();
3968 /* Any non-function converts to a [const][volatile] void *
3969 and vice versa; otherwise, targets must be the same.
3970 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3971 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3972 || (target_cmp = comp_target_types (type, rhstype))
3973 || is_opaque_pointer
3974 || (c_common_unsigned_type (mvl)
3975 == c_common_unsigned_type (mvr)))
3977 if (pedantic
3978 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3980 (VOID_TYPE_P (ttr)
3981 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3982 which are not ANSI null ptr constants. */
3983 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3984 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3985 WARN_FOR_ASSIGNMENT (G_("ISO C forbids passing argument %d of "
3986 "%qE between function pointer "
3987 "and %<void *%>"),
3988 G_("ISO C forbids assignment between "
3989 "function pointer and %<void *%>"),
3990 G_("ISO C forbids initialization between "
3991 "function pointer and %<void *%>"),
3992 G_("ISO C forbids return between function "
3993 "pointer and %<void *%>"));
3994 /* Const and volatile mean something different for function types,
3995 so the usual warnings are not appropriate. */
3996 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3997 && TREE_CODE (ttl) != FUNCTION_TYPE)
3999 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4001 /* Types differing only by the presence of the 'volatile'
4002 qualifier are acceptable if the 'volatile' has been added
4003 in by the Objective-C EH machinery. */
4004 if (!objc_type_quals_match (ttl, ttr))
4005 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
4006 "qualifiers from pointer target type"),
4007 G_("assignment discards qualifiers "
4008 "from pointer target type"),
4009 G_("initialization discards qualifiers "
4010 "from pointer target type"),
4011 G_("return discards qualifiers from "
4012 "pointer target type"));
4014 /* If this is not a case of ignoring a mismatch in signedness,
4015 no warning. */
4016 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
4017 || target_cmp)
4019 /* If there is a mismatch, do warn. */
4020 else if (warn_pointer_sign)
4021 WARN_FOR_ASSIGNMENT (G_("pointer targets in passing argument "
4022 "%d of %qE differ in signedness"),
4023 G_("pointer targets in assignment "
4024 "differ in signedness"),
4025 G_("pointer targets in initialization "
4026 "differ in signedness"),
4027 G_("pointer targets in return differ "
4028 "in signedness"));
4030 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4031 && TREE_CODE (ttr) == FUNCTION_TYPE)
4033 /* Because const and volatile on functions are restrictions
4034 that say the function will not do certain things,
4035 it is okay to use a const or volatile function
4036 where an ordinary one is wanted, but not vice-versa. */
4037 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4038 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4039 "qualified function pointer "
4040 "from unqualified"),
4041 G_("assignment makes qualified function "
4042 "pointer from unqualified"),
4043 G_("initialization makes qualified "
4044 "function pointer from unqualified"),
4045 G_("return makes qualified function "
4046 "pointer from unqualified"));
4049 else
4050 /* Avoid warning about the volatile ObjC EH puts on decls. */
4051 if (!objc_ok)
4052 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE from "
4053 "incompatible pointer type"),
4054 G_("assignment from incompatible pointer type"),
4055 G_("initialization from incompatible "
4056 "pointer type"),
4057 G_("return from incompatible pointer type"));
4059 return convert (type, rhs);
4061 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
4063 /* ??? This should not be an error when inlining calls to
4064 unprototyped functions. */
4065 error ("invalid use of non-lvalue array");
4066 return error_mark_node;
4068 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4070 /* An explicit constant 0 can convert to a pointer,
4071 or one that results from arithmetic, even including
4072 a cast to integer type. */
4073 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4075 !(TREE_CODE (rhs) == NOP_EXPR
4076 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4077 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4078 && integer_zerop (TREE_OPERAND (rhs, 0))))
4079 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
4080 "pointer from integer without a cast"),
4081 G_("assignment makes pointer from integer "
4082 "without a cast"),
4083 G_("initialization makes pointer from "
4084 "integer without a cast"),
4085 G_("return makes pointer from integer "
4086 "without a cast"));
4088 return convert (type, rhs);
4090 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4092 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes integer "
4093 "from pointer without a cast"),
4094 G_("assignment makes integer from pointer "
4095 "without a cast"),
4096 G_("initialization makes integer from pointer "
4097 "without a cast"),
4098 G_("return makes integer from pointer "
4099 "without a cast"));
4100 return convert (type, rhs);
4102 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4103 return convert (type, rhs);
4105 switch (errtype)
4107 case ic_argpass:
4108 case ic_argpass_nonproto:
4109 /* ??? This should not be an error when inlining calls to
4110 unprototyped functions. */
4111 error ("incompatible type for argument %d of %qE", parmnum, rname);
4112 break;
4113 case ic_assign:
4114 error ("incompatible types in assignment");
4115 break;
4116 case ic_init:
4117 error ("incompatible types in initialization");
4118 break;
4119 case ic_return:
4120 error ("incompatible types in return");
4121 break;
4122 default:
4123 gcc_unreachable ();
4126 return error_mark_node;
4129 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
4130 is used for error and waring reporting and indicates which argument
4131 is being processed. */
4133 tree
4134 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
4136 tree ret, type;
4138 /* If FN was prototyped, the value has been converted already
4139 in convert_arguments. */
4140 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4141 return value;
4143 type = TREE_TYPE (parm);
4144 ret = convert_for_assignment (type, value,
4145 ic_argpass_nonproto, fn,
4146 fn, argnum);
4147 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
4148 && INTEGRAL_TYPE_P (type)
4149 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4150 ret = default_conversion (ret);
4151 return ret;
4154 /* If VALUE is a compound expr all of whose expressions are constant, then
4155 return its value. Otherwise, return error_mark_node.
4157 This is for handling COMPOUND_EXPRs as initializer elements
4158 which is allowed with a warning when -pedantic is specified. */
4160 static tree
4161 valid_compound_expr_initializer (tree value, tree endtype)
4163 if (TREE_CODE (value) == COMPOUND_EXPR)
4165 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4166 == error_mark_node)
4167 return error_mark_node;
4168 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4169 endtype);
4171 else if (!initializer_constant_valid_p (value, endtype))
4172 return error_mark_node;
4173 else
4174 return value;
4177 /* Perform appropriate conversions on the initial value of a variable,
4178 store it in the declaration DECL,
4179 and print any error messages that are appropriate.
4180 If the init is invalid, store an ERROR_MARK. */
4182 void
4183 store_init_value (tree decl, tree init)
4185 tree value, type;
4187 /* If variable's type was invalidly declared, just ignore it. */
4189 type = TREE_TYPE (decl);
4190 if (TREE_CODE (type) == ERROR_MARK)
4191 return;
4193 /* Digest the specified initializer into an expression. */
4195 value = digest_init (type, init, true, TREE_STATIC (decl));
4197 /* Store the expression if valid; else report error. */
4199 if (!in_system_header
4200 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
4201 warning (OPT_Wtraditional, "traditional C rejects automatic "
4202 "aggregate initialization");
4204 DECL_INITIAL (decl) = value;
4206 /* ANSI wants warnings about out-of-range constant initializers. */
4207 STRIP_TYPE_NOPS (value);
4208 constant_expression_warning (value);
4210 /* Check if we need to set array size from compound literal size. */
4211 if (TREE_CODE (type) == ARRAY_TYPE
4212 && TYPE_DOMAIN (type) == 0
4213 && value != error_mark_node)
4215 tree inside_init = init;
4217 STRIP_TYPE_NOPS (inside_init);
4218 inside_init = fold (inside_init);
4220 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4222 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4224 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4226 /* For int foo[] = (int [3]){1}; we need to set array size
4227 now since later on array initializer will be just the
4228 brace enclosed list of the compound literal. */
4229 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4230 layout_type (type);
4231 layout_decl (decl, 0);
4237 /* Methods for storing and printing names for error messages. */
4239 /* Implement a spelling stack that allows components of a name to be pushed
4240 and popped. Each element on the stack is this structure. */
4242 struct spelling
4244 int kind;
4245 union
4247 int i;
4248 const char *s;
4249 } u;
4252 #define SPELLING_STRING 1
4253 #define SPELLING_MEMBER 2
4254 #define SPELLING_BOUNDS 3
4256 static struct spelling *spelling; /* Next stack element (unused). */
4257 static struct spelling *spelling_base; /* Spelling stack base. */
4258 static int spelling_size; /* Size of the spelling stack. */
4260 /* Macros to save and restore the spelling stack around push_... functions.
4261 Alternative to SAVE_SPELLING_STACK. */
4263 #define SPELLING_DEPTH() (spelling - spelling_base)
4264 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4266 /* Push an element on the spelling stack with type KIND and assign VALUE
4267 to MEMBER. */
4269 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4271 int depth = SPELLING_DEPTH (); \
4273 if (depth >= spelling_size) \
4275 spelling_size += 10; \
4276 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4277 spelling_size); \
4278 RESTORE_SPELLING_DEPTH (depth); \
4281 spelling->kind = (KIND); \
4282 spelling->MEMBER = (VALUE); \
4283 spelling++; \
4286 /* Push STRING on the stack. Printed literally. */
4288 static void
4289 push_string (const char *string)
4291 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4294 /* Push a member name on the stack. Printed as '.' STRING. */
4296 static void
4297 push_member_name (tree decl)
4299 const char *const string
4300 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4301 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4304 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4306 static void
4307 push_array_bounds (int bounds)
4309 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4312 /* Compute the maximum size in bytes of the printed spelling. */
4314 static int
4315 spelling_length (void)
4317 int size = 0;
4318 struct spelling *p;
4320 for (p = spelling_base; p < spelling; p++)
4322 if (p->kind == SPELLING_BOUNDS)
4323 size += 25;
4324 else
4325 size += strlen (p->u.s) + 1;
4328 return size;
4331 /* Print the spelling to BUFFER and return it. */
4333 static char *
4334 print_spelling (char *buffer)
4336 char *d = buffer;
4337 struct spelling *p;
4339 for (p = spelling_base; p < spelling; p++)
4340 if (p->kind == SPELLING_BOUNDS)
4342 sprintf (d, "[%d]", p->u.i);
4343 d += strlen (d);
4345 else
4347 const char *s;
4348 if (p->kind == SPELLING_MEMBER)
4349 *d++ = '.';
4350 for (s = p->u.s; (*d = *s++); d++)
4353 *d++ = '\0';
4354 return buffer;
4357 /* Issue an error message for a bad initializer component.
4358 MSGID identifies the message.
4359 The component name is taken from the spelling stack. */
4361 void
4362 error_init (const char *msgid)
4364 char *ofwhat;
4366 error ("%s", _(msgid));
4367 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4368 if (*ofwhat)
4369 error ("(near initialization for %qs)", ofwhat);
4372 /* Issue a pedantic warning for a bad initializer component.
4373 MSGID identifies the message.
4374 The component name is taken from the spelling stack. */
4376 void
4377 pedwarn_init (const char *msgid)
4379 char *ofwhat;
4381 pedwarn ("%s", _(msgid));
4382 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4383 if (*ofwhat)
4384 pedwarn ("(near initialization for %qs)", ofwhat);
4387 /* Issue a warning for a bad initializer component.
4388 MSGID identifies the message.
4389 The component name is taken from the spelling stack. */
4391 static void
4392 warning_init (const char *msgid)
4394 char *ofwhat;
4396 warning (0, "%s", _(msgid));
4397 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4398 if (*ofwhat)
4399 warning (0, "(near initialization for %qs)", ofwhat);
4402 /* If TYPE is an array type and EXPR is a parenthesized string
4403 constant, warn if pedantic that EXPR is being used to initialize an
4404 object of type TYPE. */
4406 void
4407 maybe_warn_string_init (tree type, struct c_expr expr)
4409 if (pedantic
4410 && TREE_CODE (type) == ARRAY_TYPE
4411 && TREE_CODE (expr.value) == STRING_CST
4412 && expr.original_code != STRING_CST)
4413 pedwarn_init ("array initialized from parenthesized string constant");
4416 /* Digest the parser output INIT as an initializer for type TYPE.
4417 Return a C expression of type TYPE to represent the initial value.
4419 If INIT is a string constant, STRICT_STRING is true if it is
4420 unparenthesized or we should not warn here for it being parenthesized.
4421 For other types of INIT, STRICT_STRING is not used.
4423 REQUIRE_CONSTANT requests an error if non-constant initializers or
4424 elements are seen. */
4426 static tree
4427 digest_init (tree type, tree init, bool strict_string, int require_constant)
4429 enum tree_code code = TREE_CODE (type);
4430 tree inside_init = init;
4432 if (type == error_mark_node
4433 || init == error_mark_node
4434 || TREE_TYPE (init) == error_mark_node)
4435 return error_mark_node;
4437 STRIP_TYPE_NOPS (inside_init);
4439 inside_init = fold (inside_init);
4441 /* Initialization of an array of chars from a string constant
4442 optionally enclosed in braces. */
4444 if (code == ARRAY_TYPE && inside_init
4445 && TREE_CODE (inside_init) == STRING_CST)
4447 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4448 /* Note that an array could be both an array of character type
4449 and an array of wchar_t if wchar_t is signed char or unsigned
4450 char. */
4451 bool char_array = (typ1 == char_type_node
4452 || typ1 == signed_char_type_node
4453 || typ1 == unsigned_char_type_node);
4454 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4455 if (char_array || wchar_array)
4457 struct c_expr expr;
4458 bool char_string;
4459 expr.value = inside_init;
4460 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4461 maybe_warn_string_init (type, expr);
4463 char_string
4464 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4465 == char_type_node);
4467 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4468 TYPE_MAIN_VARIANT (type)))
4469 return inside_init;
4471 if (!wchar_array && !char_string)
4473 error_init ("char-array initialized from wide string");
4474 return error_mark_node;
4476 if (char_string && !char_array)
4478 error_init ("wchar_t-array initialized from non-wide string");
4479 return error_mark_node;
4482 TREE_TYPE (inside_init) = type;
4483 if (TYPE_DOMAIN (type) != 0
4484 && TYPE_SIZE (type) != 0
4485 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4486 /* Subtract 1 (or sizeof (wchar_t))
4487 because it's ok to ignore the terminating null char
4488 that is counted in the length of the constant. */
4489 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4490 TREE_STRING_LENGTH (inside_init)
4491 - ((TYPE_PRECISION (typ1)
4492 != TYPE_PRECISION (char_type_node))
4493 ? (TYPE_PRECISION (wchar_type_node)
4494 / BITS_PER_UNIT)
4495 : 1)))
4496 pedwarn_init ("initializer-string for array of chars is too long");
4498 return inside_init;
4500 else if (INTEGRAL_TYPE_P (typ1))
4502 error_init ("array of inappropriate type initialized "
4503 "from string constant");
4504 return error_mark_node;
4508 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4509 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4510 below and handle as a constructor. */
4511 if (code == VECTOR_TYPE
4512 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4513 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4514 && TREE_CONSTANT (inside_init))
4516 if (TREE_CODE (inside_init) == VECTOR_CST
4517 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4518 TYPE_MAIN_VARIANT (type)))
4519 return inside_init;
4521 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4523 unsigned HOST_WIDE_INT ix;
4524 tree value;
4525 bool constant_p = true;
4527 /* Iterate through elements and check if all constructor
4528 elements are *_CSTs. */
4529 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
4530 if (!CONSTANT_CLASS_P (value))
4532 constant_p = false;
4533 break;
4536 if (constant_p)
4537 return build_vector_from_ctor (type,
4538 CONSTRUCTOR_ELTS (inside_init));
4542 /* Any type can be initialized
4543 from an expression of the same type, optionally with braces. */
4545 if (inside_init && TREE_TYPE (inside_init) != 0
4546 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4547 TYPE_MAIN_VARIANT (type))
4548 || (code == ARRAY_TYPE
4549 && comptypes (TREE_TYPE (inside_init), type))
4550 || (code == VECTOR_TYPE
4551 && comptypes (TREE_TYPE (inside_init), type))
4552 || (code == POINTER_TYPE
4553 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4554 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4555 TREE_TYPE (type)))))
4557 if (code == POINTER_TYPE)
4559 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4561 if (TREE_CODE (inside_init) == STRING_CST
4562 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4563 inside_init = array_to_pointer_conversion (inside_init);
4564 else
4566 error_init ("invalid use of non-lvalue array");
4567 return error_mark_node;
4572 if (code == VECTOR_TYPE)
4573 /* Although the types are compatible, we may require a
4574 conversion. */
4575 inside_init = convert (type, inside_init);
4577 if (require_constant && !flag_isoc99
4578 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4580 /* As an extension, allow initializing objects with static storage
4581 duration with compound literals (which are then treated just as
4582 the brace enclosed list they contain). */
4583 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4584 inside_init = DECL_INITIAL (decl);
4587 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4588 && TREE_CODE (inside_init) != CONSTRUCTOR)
4590 error_init ("array initialized from non-constant array expression");
4591 return error_mark_node;
4594 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4595 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4597 /* Compound expressions can only occur here if -pedantic or
4598 -pedantic-errors is specified. In the later case, we always want
4599 an error. In the former case, we simply want a warning. */
4600 if (require_constant && pedantic
4601 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4603 inside_init
4604 = valid_compound_expr_initializer (inside_init,
4605 TREE_TYPE (inside_init));
4606 if (inside_init == error_mark_node)
4607 error_init ("initializer element is not constant");
4608 else
4609 pedwarn_init ("initializer element is not constant");
4610 if (flag_pedantic_errors)
4611 inside_init = error_mark_node;
4613 else if (require_constant
4614 && !initializer_constant_valid_p (inside_init,
4615 TREE_TYPE (inside_init)))
4617 error_init ("initializer element is not constant");
4618 inside_init = error_mark_node;
4621 /* Added to enable additional -Wmissing-format-attribute warnings. */
4622 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
4623 inside_init = convert_for_assignment (type, inside_init, ic_init, NULL_TREE,
4624 NULL_TREE, 0);
4625 return inside_init;
4628 /* Handle scalar types, including conversions. */
4630 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4631 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4632 || code == VECTOR_TYPE)
4634 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
4635 && (TREE_CODE (init) == STRING_CST
4636 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
4637 init = array_to_pointer_conversion (init);
4638 inside_init
4639 = convert_for_assignment (type, init, ic_init,
4640 NULL_TREE, NULL_TREE, 0);
4642 /* Check to see if we have already given an error message. */
4643 if (inside_init == error_mark_node)
4645 else if (require_constant && !TREE_CONSTANT (inside_init))
4647 error_init ("initializer element is not constant");
4648 inside_init = error_mark_node;
4650 else if (require_constant
4651 && !initializer_constant_valid_p (inside_init,
4652 TREE_TYPE (inside_init)))
4654 error_init ("initializer element is not computable at load time");
4655 inside_init = error_mark_node;
4658 return inside_init;
4661 /* Come here only for records and arrays. */
4663 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4665 error_init ("variable-sized object may not be initialized");
4666 return error_mark_node;
4669 error_init ("invalid initializer");
4670 return error_mark_node;
4673 /* Handle initializers that use braces. */
4675 /* Type of object we are accumulating a constructor for.
4676 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4677 static tree constructor_type;
4679 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4680 left to fill. */
4681 static tree constructor_fields;
4683 /* For an ARRAY_TYPE, this is the specified index
4684 at which to store the next element we get. */
4685 static tree constructor_index;
4687 /* For an ARRAY_TYPE, this is the maximum index. */
4688 static tree constructor_max_index;
4690 /* For a RECORD_TYPE, this is the first field not yet written out. */
4691 static tree constructor_unfilled_fields;
4693 /* For an ARRAY_TYPE, this is the index of the first element
4694 not yet written out. */
4695 static tree constructor_unfilled_index;
4697 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4698 This is so we can generate gaps between fields, when appropriate. */
4699 static tree constructor_bit_index;
4701 /* If we are saving up the elements rather than allocating them,
4702 this is the list of elements so far (in reverse order,
4703 most recent first). */
4704 static VEC(constructor_elt,gc) *constructor_elements;
4706 /* 1 if constructor should be incrementally stored into a constructor chain,
4707 0 if all the elements should be kept in AVL tree. */
4708 static int constructor_incremental;
4710 /* 1 if so far this constructor's elements are all compile-time constants. */
4711 static int constructor_constant;
4713 /* 1 if so far this constructor's elements are all valid address constants. */
4714 static int constructor_simple;
4716 /* 1 if this constructor is erroneous so far. */
4717 static int constructor_erroneous;
4719 /* Structure for managing pending initializer elements, organized as an
4720 AVL tree. */
4722 struct init_node
4724 struct init_node *left, *right;
4725 struct init_node *parent;
4726 int balance;
4727 tree purpose;
4728 tree value;
4731 /* Tree of pending elements at this constructor level.
4732 These are elements encountered out of order
4733 which belong at places we haven't reached yet in actually
4734 writing the output.
4735 Will never hold tree nodes across GC runs. */
4736 static struct init_node *constructor_pending_elts;
4738 /* The SPELLING_DEPTH of this constructor. */
4739 static int constructor_depth;
4741 /* DECL node for which an initializer is being read.
4742 0 means we are reading a constructor expression
4743 such as (struct foo) {...}. */
4744 static tree constructor_decl;
4746 /* Nonzero if this is an initializer for a top-level decl. */
4747 static int constructor_top_level;
4749 /* Nonzero if there were any member designators in this initializer. */
4750 static int constructor_designated;
4752 /* Nesting depth of designator list. */
4753 static int designator_depth;
4755 /* Nonzero if there were diagnosed errors in this designator list. */
4756 static int designator_erroneous;
4759 /* This stack has a level for each implicit or explicit level of
4760 structuring in the initializer, including the outermost one. It
4761 saves the values of most of the variables above. */
4763 struct constructor_range_stack;
4765 struct constructor_stack
4767 struct constructor_stack *next;
4768 tree type;
4769 tree fields;
4770 tree index;
4771 tree max_index;
4772 tree unfilled_index;
4773 tree unfilled_fields;
4774 tree bit_index;
4775 VEC(constructor_elt,gc) *elements;
4776 struct init_node *pending_elts;
4777 int offset;
4778 int depth;
4779 /* If value nonzero, this value should replace the entire
4780 constructor at this level. */
4781 struct c_expr replacement_value;
4782 struct constructor_range_stack *range_stack;
4783 char constant;
4784 char simple;
4785 char implicit;
4786 char erroneous;
4787 char outer;
4788 char incremental;
4789 char designated;
4792 static struct constructor_stack *constructor_stack;
4794 /* This stack represents designators from some range designator up to
4795 the last designator in the list. */
4797 struct constructor_range_stack
4799 struct constructor_range_stack *next, *prev;
4800 struct constructor_stack *stack;
4801 tree range_start;
4802 tree index;
4803 tree range_end;
4804 tree fields;
4807 static struct constructor_range_stack *constructor_range_stack;
4809 /* This stack records separate initializers that are nested.
4810 Nested initializers can't happen in ANSI C, but GNU C allows them
4811 in cases like { ... (struct foo) { ... } ... }. */
4813 struct initializer_stack
4815 struct initializer_stack *next;
4816 tree decl;
4817 struct constructor_stack *constructor_stack;
4818 struct constructor_range_stack *constructor_range_stack;
4819 VEC(constructor_elt,gc) *elements;
4820 struct spelling *spelling;
4821 struct spelling *spelling_base;
4822 int spelling_size;
4823 char top_level;
4824 char require_constant_value;
4825 char require_constant_elements;
4828 static struct initializer_stack *initializer_stack;
4830 /* Prepare to parse and output the initializer for variable DECL. */
4832 void
4833 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4835 const char *locus;
4836 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4838 p->decl = constructor_decl;
4839 p->require_constant_value = require_constant_value;
4840 p->require_constant_elements = require_constant_elements;
4841 p->constructor_stack = constructor_stack;
4842 p->constructor_range_stack = constructor_range_stack;
4843 p->elements = constructor_elements;
4844 p->spelling = spelling;
4845 p->spelling_base = spelling_base;
4846 p->spelling_size = spelling_size;
4847 p->top_level = constructor_top_level;
4848 p->next = initializer_stack;
4849 initializer_stack = p;
4851 constructor_decl = decl;
4852 constructor_designated = 0;
4853 constructor_top_level = top_level;
4855 if (decl != 0 && decl != error_mark_node)
4857 require_constant_value = TREE_STATIC (decl);
4858 require_constant_elements
4859 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4860 /* For a scalar, you can always use any value to initialize,
4861 even within braces. */
4862 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4863 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4864 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4865 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4866 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4868 else
4870 require_constant_value = 0;
4871 require_constant_elements = 0;
4872 locus = "(anonymous)";
4875 constructor_stack = 0;
4876 constructor_range_stack = 0;
4878 missing_braces_mentioned = 0;
4880 spelling_base = 0;
4881 spelling_size = 0;
4882 RESTORE_SPELLING_DEPTH (0);
4884 if (locus)
4885 push_string (locus);
4888 void
4889 finish_init (void)
4891 struct initializer_stack *p = initializer_stack;
4893 /* Free the whole constructor stack of this initializer. */
4894 while (constructor_stack)
4896 struct constructor_stack *q = constructor_stack;
4897 constructor_stack = q->next;
4898 free (q);
4901 gcc_assert (!constructor_range_stack);
4903 /* Pop back to the data of the outer initializer (if any). */
4904 free (spelling_base);
4906 constructor_decl = p->decl;
4907 require_constant_value = p->require_constant_value;
4908 require_constant_elements = p->require_constant_elements;
4909 constructor_stack = p->constructor_stack;
4910 constructor_range_stack = p->constructor_range_stack;
4911 constructor_elements = p->elements;
4912 spelling = p->spelling;
4913 spelling_base = p->spelling_base;
4914 spelling_size = p->spelling_size;
4915 constructor_top_level = p->top_level;
4916 initializer_stack = p->next;
4917 free (p);
4920 /* Call here when we see the initializer is surrounded by braces.
4921 This is instead of a call to push_init_level;
4922 it is matched by a call to pop_init_level.
4924 TYPE is the type to initialize, for a constructor expression.
4925 For an initializer for a decl, TYPE is zero. */
4927 void
4928 really_start_incremental_init (tree type)
4930 struct constructor_stack *p = XNEW (struct constructor_stack);
4932 if (type == 0)
4933 type = TREE_TYPE (constructor_decl);
4935 if (targetm.vector_opaque_p (type))
4936 error ("opaque vector types cannot be initialized");
4938 p->type = constructor_type;
4939 p->fields = constructor_fields;
4940 p->index = constructor_index;
4941 p->max_index = constructor_max_index;
4942 p->unfilled_index = constructor_unfilled_index;
4943 p->unfilled_fields = constructor_unfilled_fields;
4944 p->bit_index = constructor_bit_index;
4945 p->elements = constructor_elements;
4946 p->constant = constructor_constant;
4947 p->simple = constructor_simple;
4948 p->erroneous = constructor_erroneous;
4949 p->pending_elts = constructor_pending_elts;
4950 p->depth = constructor_depth;
4951 p->replacement_value.value = 0;
4952 p->replacement_value.original_code = ERROR_MARK;
4953 p->implicit = 0;
4954 p->range_stack = 0;
4955 p->outer = 0;
4956 p->incremental = constructor_incremental;
4957 p->designated = constructor_designated;
4958 p->next = 0;
4959 constructor_stack = p;
4961 constructor_constant = 1;
4962 constructor_simple = 1;
4963 constructor_depth = SPELLING_DEPTH ();
4964 constructor_elements = 0;
4965 constructor_pending_elts = 0;
4966 constructor_type = type;
4967 constructor_incremental = 1;
4968 constructor_designated = 0;
4969 designator_depth = 0;
4970 designator_erroneous = 0;
4972 if (TREE_CODE (constructor_type) == RECORD_TYPE
4973 || TREE_CODE (constructor_type) == UNION_TYPE)
4975 constructor_fields = TYPE_FIELDS (constructor_type);
4976 /* Skip any nameless bit fields at the beginning. */
4977 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4978 && DECL_NAME (constructor_fields) == 0)
4979 constructor_fields = TREE_CHAIN (constructor_fields);
4981 constructor_unfilled_fields = constructor_fields;
4982 constructor_bit_index = bitsize_zero_node;
4984 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4986 if (TYPE_DOMAIN (constructor_type))
4988 constructor_max_index
4989 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4991 /* Detect non-empty initializations of zero-length arrays. */
4992 if (constructor_max_index == NULL_TREE
4993 && TYPE_SIZE (constructor_type))
4994 constructor_max_index = build_int_cst (NULL_TREE, -1);
4996 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4997 to initialize VLAs will cause a proper error; avoid tree
4998 checking errors as well by setting a safe value. */
4999 if (constructor_max_index
5000 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5001 constructor_max_index = build_int_cst (NULL_TREE, -1);
5003 constructor_index
5004 = convert (bitsizetype,
5005 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5007 else
5009 constructor_index = bitsize_zero_node;
5010 constructor_max_index = NULL_TREE;
5013 constructor_unfilled_index = constructor_index;
5015 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5017 /* Vectors are like simple fixed-size arrays. */
5018 constructor_max_index =
5019 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5020 constructor_index = convert (bitsizetype, bitsize_zero_node);
5021 constructor_unfilled_index = constructor_index;
5023 else
5025 /* Handle the case of int x = {5}; */
5026 constructor_fields = constructor_type;
5027 constructor_unfilled_fields = constructor_type;
5031 /* Push down into a subobject, for initialization.
5032 If this is for an explicit set of braces, IMPLICIT is 0.
5033 If it is because the next element belongs at a lower level,
5034 IMPLICIT is 1 (or 2 if the push is because of designator list). */
5036 void
5037 push_init_level (int implicit)
5039 struct constructor_stack *p;
5040 tree value = NULL_TREE;
5042 /* If we've exhausted any levels that didn't have braces,
5043 pop them now. If implicit == 1, this will have been done in
5044 process_init_element; do not repeat it here because in the case
5045 of excess initializers for an empty aggregate this leads to an
5046 infinite cycle of popping a level and immediately recreating
5047 it. */
5048 if (implicit != 1)
5050 while (constructor_stack->implicit)
5052 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5053 || TREE_CODE (constructor_type) == UNION_TYPE)
5054 && constructor_fields == 0)
5055 process_init_element (pop_init_level (1));
5056 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5057 && constructor_max_index
5058 && tree_int_cst_lt (constructor_max_index,
5059 constructor_index))
5060 process_init_element (pop_init_level (1));
5061 else
5062 break;
5066 /* Unless this is an explicit brace, we need to preserve previous
5067 content if any. */
5068 if (implicit)
5070 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5071 || TREE_CODE (constructor_type) == UNION_TYPE)
5072 && constructor_fields)
5073 value = find_init_member (constructor_fields);
5074 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5075 value = find_init_member (constructor_index);
5078 p = XNEW (struct constructor_stack);
5079 p->type = constructor_type;
5080 p->fields = constructor_fields;
5081 p->index = constructor_index;
5082 p->max_index = constructor_max_index;
5083 p->unfilled_index = constructor_unfilled_index;
5084 p->unfilled_fields = constructor_unfilled_fields;
5085 p->bit_index = constructor_bit_index;
5086 p->elements = constructor_elements;
5087 p->constant = constructor_constant;
5088 p->simple = constructor_simple;
5089 p->erroneous = constructor_erroneous;
5090 p->pending_elts = constructor_pending_elts;
5091 p->depth = constructor_depth;
5092 p->replacement_value.value = 0;
5093 p->replacement_value.original_code = ERROR_MARK;
5094 p->implicit = implicit;
5095 p->outer = 0;
5096 p->incremental = constructor_incremental;
5097 p->designated = constructor_designated;
5098 p->next = constructor_stack;
5099 p->range_stack = 0;
5100 constructor_stack = p;
5102 constructor_constant = 1;
5103 constructor_simple = 1;
5104 constructor_depth = SPELLING_DEPTH ();
5105 constructor_elements = 0;
5106 constructor_incremental = 1;
5107 constructor_designated = 0;
5108 constructor_pending_elts = 0;
5109 if (!implicit)
5111 p->range_stack = constructor_range_stack;
5112 constructor_range_stack = 0;
5113 designator_depth = 0;
5114 designator_erroneous = 0;
5117 /* Don't die if an entire brace-pair level is superfluous
5118 in the containing level. */
5119 if (constructor_type == 0)
5121 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5122 || TREE_CODE (constructor_type) == UNION_TYPE)
5124 /* Don't die if there are extra init elts at the end. */
5125 if (constructor_fields == 0)
5126 constructor_type = 0;
5127 else
5129 constructor_type = TREE_TYPE (constructor_fields);
5130 push_member_name (constructor_fields);
5131 constructor_depth++;
5134 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5136 constructor_type = TREE_TYPE (constructor_type);
5137 push_array_bounds (tree_low_cst (constructor_index, 0));
5138 constructor_depth++;
5141 if (constructor_type == 0)
5143 error_init ("extra brace group at end of initializer");
5144 constructor_fields = 0;
5145 constructor_unfilled_fields = 0;
5146 return;
5149 if (value && TREE_CODE (value) == CONSTRUCTOR)
5151 constructor_constant = TREE_CONSTANT (value);
5152 constructor_simple = TREE_STATIC (value);
5153 constructor_elements = CONSTRUCTOR_ELTS (value);
5154 if (!VEC_empty (constructor_elt, constructor_elements)
5155 && (TREE_CODE (constructor_type) == RECORD_TYPE
5156 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5157 set_nonincremental_init ();
5160 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
5162 missing_braces_mentioned = 1;
5163 warning_init ("missing braces around initializer");
5166 if (TREE_CODE (constructor_type) == RECORD_TYPE
5167 || TREE_CODE (constructor_type) == UNION_TYPE)
5169 constructor_fields = TYPE_FIELDS (constructor_type);
5170 /* Skip any nameless bit fields at the beginning. */
5171 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
5172 && DECL_NAME (constructor_fields) == 0)
5173 constructor_fields = TREE_CHAIN (constructor_fields);
5175 constructor_unfilled_fields = constructor_fields;
5176 constructor_bit_index = bitsize_zero_node;
5178 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5180 /* Vectors are like simple fixed-size arrays. */
5181 constructor_max_index =
5182 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
5183 constructor_index = convert (bitsizetype, integer_zero_node);
5184 constructor_unfilled_index = constructor_index;
5186 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5188 if (TYPE_DOMAIN (constructor_type))
5190 constructor_max_index
5191 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
5193 /* Detect non-empty initializations of zero-length arrays. */
5194 if (constructor_max_index == NULL_TREE
5195 && TYPE_SIZE (constructor_type))
5196 constructor_max_index = build_int_cst (NULL_TREE, -1);
5198 /* constructor_max_index needs to be an INTEGER_CST. Attempts
5199 to initialize VLAs will cause a proper error; avoid tree
5200 checking errors as well by setting a safe value. */
5201 if (constructor_max_index
5202 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5203 constructor_max_index = build_int_cst (NULL_TREE, -1);
5205 constructor_index
5206 = convert (bitsizetype,
5207 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5209 else
5210 constructor_index = bitsize_zero_node;
5212 constructor_unfilled_index = constructor_index;
5213 if (value && TREE_CODE (value) == STRING_CST)
5215 /* We need to split the char/wchar array into individual
5216 characters, so that we don't have to special case it
5217 everywhere. */
5218 set_nonincremental_init_from_string (value);
5221 else
5223 if (constructor_type != error_mark_node)
5224 warning_init ("braces around scalar initializer");
5225 constructor_fields = constructor_type;
5226 constructor_unfilled_fields = constructor_type;
5230 /* At the end of an implicit or explicit brace level,
5231 finish up that level of constructor. If a single expression
5232 with redundant braces initialized that level, return the
5233 c_expr structure for that expression. Otherwise, the original_code
5234 element is set to ERROR_MARK.
5235 If we were outputting the elements as they are read, return 0 as the value
5236 from inner levels (process_init_element ignores that),
5237 but return error_mark_node as the value from the outermost level
5238 (that's what we want to put in DECL_INITIAL).
5239 Otherwise, return a CONSTRUCTOR expression as the value. */
5241 struct c_expr
5242 pop_init_level (int implicit)
5244 struct constructor_stack *p;
5245 struct c_expr ret;
5246 ret.value = 0;
5247 ret.original_code = ERROR_MARK;
5249 if (implicit == 0)
5251 /* When we come to an explicit close brace,
5252 pop any inner levels that didn't have explicit braces. */
5253 while (constructor_stack->implicit)
5254 process_init_element (pop_init_level (1));
5256 gcc_assert (!constructor_range_stack);
5259 /* Now output all pending elements. */
5260 constructor_incremental = 1;
5261 output_pending_init_elements (1);
5263 p = constructor_stack;
5265 /* Error for initializing a flexible array member, or a zero-length
5266 array member in an inappropriate context. */
5267 if (constructor_type && constructor_fields
5268 && TREE_CODE (constructor_type) == ARRAY_TYPE
5269 && TYPE_DOMAIN (constructor_type)
5270 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5272 /* Silently discard empty initializations. The parser will
5273 already have pedwarned for empty brackets. */
5274 if (integer_zerop (constructor_unfilled_index))
5275 constructor_type = NULL_TREE;
5276 else
5278 gcc_assert (!TYPE_SIZE (constructor_type));
5280 if (constructor_depth > 2)
5281 error_init ("initialization of flexible array member in a nested context");
5282 else if (pedantic)
5283 pedwarn_init ("initialization of a flexible array member");
5285 /* We have already issued an error message for the existence
5286 of a flexible array member not at the end of the structure.
5287 Discard the initializer so that we do not die later. */
5288 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5289 constructor_type = NULL_TREE;
5293 /* Warn when some struct elements are implicitly initialized to zero. */
5294 if (warn_missing_field_initializers
5295 && constructor_type
5296 && TREE_CODE (constructor_type) == RECORD_TYPE
5297 && constructor_unfilled_fields)
5299 /* Do not warn for flexible array members or zero-length arrays. */
5300 while (constructor_unfilled_fields
5301 && (!DECL_SIZE (constructor_unfilled_fields)
5302 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5303 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5305 /* Do not warn if this level of the initializer uses member
5306 designators; it is likely to be deliberate. */
5307 if (constructor_unfilled_fields && !constructor_designated)
5309 push_member_name (constructor_unfilled_fields);
5310 warning_init ("missing initializer");
5311 RESTORE_SPELLING_DEPTH (constructor_depth);
5315 /* Pad out the end of the structure. */
5316 if (p->replacement_value.value)
5317 /* If this closes a superfluous brace pair,
5318 just pass out the element between them. */
5319 ret = p->replacement_value;
5320 else if (constructor_type == 0)
5322 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5323 && TREE_CODE (constructor_type) != UNION_TYPE
5324 && TREE_CODE (constructor_type) != ARRAY_TYPE
5325 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5327 /* A nonincremental scalar initializer--just return
5328 the element, after verifying there is just one. */
5329 if (VEC_empty (constructor_elt,constructor_elements))
5331 if (!constructor_erroneous)
5332 error_init ("empty scalar initializer");
5333 ret.value = error_mark_node;
5335 else if (VEC_length (constructor_elt,constructor_elements) != 1)
5337 error_init ("extra elements in scalar initializer");
5338 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5340 else
5341 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
5343 else
5345 if (constructor_erroneous)
5346 ret.value = error_mark_node;
5347 else
5349 ret.value = build_constructor (constructor_type,
5350 constructor_elements);
5351 if (constructor_constant)
5352 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5353 if (constructor_constant && constructor_simple)
5354 TREE_STATIC (ret.value) = 1;
5358 constructor_type = p->type;
5359 constructor_fields = p->fields;
5360 constructor_index = p->index;
5361 constructor_max_index = p->max_index;
5362 constructor_unfilled_index = p->unfilled_index;
5363 constructor_unfilled_fields = p->unfilled_fields;
5364 constructor_bit_index = p->bit_index;
5365 constructor_elements = p->elements;
5366 constructor_constant = p->constant;
5367 constructor_simple = p->simple;
5368 constructor_erroneous = p->erroneous;
5369 constructor_incremental = p->incremental;
5370 constructor_designated = p->designated;
5371 constructor_pending_elts = p->pending_elts;
5372 constructor_depth = p->depth;
5373 if (!p->implicit)
5374 constructor_range_stack = p->range_stack;
5375 RESTORE_SPELLING_DEPTH (constructor_depth);
5377 constructor_stack = p->next;
5378 free (p);
5380 if (ret.value == 0)
5382 if (constructor_stack == 0)
5384 ret.value = error_mark_node;
5385 return ret;
5387 return ret;
5389 return ret;
5392 /* Common handling for both array range and field name designators.
5393 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5395 static int
5396 set_designator (int array)
5398 tree subtype;
5399 enum tree_code subcode;
5401 /* Don't die if an entire brace-pair level is superfluous
5402 in the containing level. */
5403 if (constructor_type == 0)
5404 return 1;
5406 /* If there were errors in this designator list already, bail out
5407 silently. */
5408 if (designator_erroneous)
5409 return 1;
5411 if (!designator_depth)
5413 gcc_assert (!constructor_range_stack);
5415 /* Designator list starts at the level of closest explicit
5416 braces. */
5417 while (constructor_stack->implicit)
5418 process_init_element (pop_init_level (1));
5419 constructor_designated = 1;
5420 return 0;
5423 switch (TREE_CODE (constructor_type))
5425 case RECORD_TYPE:
5426 case UNION_TYPE:
5427 subtype = TREE_TYPE (constructor_fields);
5428 if (subtype != error_mark_node)
5429 subtype = TYPE_MAIN_VARIANT (subtype);
5430 break;
5431 case ARRAY_TYPE:
5432 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5433 break;
5434 default:
5435 gcc_unreachable ();
5438 subcode = TREE_CODE (subtype);
5439 if (array && subcode != ARRAY_TYPE)
5441 error_init ("array index in non-array initializer");
5442 return 1;
5444 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5446 error_init ("field name not in record or union initializer");
5447 return 1;
5450 constructor_designated = 1;
5451 push_init_level (2);
5452 return 0;
5455 /* If there are range designators in designator list, push a new designator
5456 to constructor_range_stack. RANGE_END is end of such stack range or
5457 NULL_TREE if there is no range designator at this level. */
5459 static void
5460 push_range_stack (tree range_end)
5462 struct constructor_range_stack *p;
5464 p = GGC_NEW (struct constructor_range_stack);
5465 p->prev = constructor_range_stack;
5466 p->next = 0;
5467 p->fields = constructor_fields;
5468 p->range_start = constructor_index;
5469 p->index = constructor_index;
5470 p->stack = constructor_stack;
5471 p->range_end = range_end;
5472 if (constructor_range_stack)
5473 constructor_range_stack->next = p;
5474 constructor_range_stack = p;
5477 /* Within an array initializer, specify the next index to be initialized.
5478 FIRST is that index. If LAST is nonzero, then initialize a range
5479 of indices, running from FIRST through LAST. */
5481 void
5482 set_init_index (tree first, tree last)
5484 if (set_designator (1))
5485 return;
5487 designator_erroneous = 1;
5489 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5490 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5492 error_init ("array index in initializer not of integer type");
5493 return;
5496 if (TREE_CODE (first) != INTEGER_CST)
5497 error_init ("nonconstant array index in initializer");
5498 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5499 error_init ("nonconstant array index in initializer");
5500 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5501 error_init ("array index in non-array initializer");
5502 else if (tree_int_cst_sgn (first) == -1)
5503 error_init ("array index in initializer exceeds array bounds");
5504 else if (constructor_max_index
5505 && tree_int_cst_lt (constructor_max_index, first))
5506 error_init ("array index in initializer exceeds array bounds");
5507 else
5509 constructor_index = convert (bitsizetype, first);
5511 if (last)
5513 if (tree_int_cst_equal (first, last))
5514 last = 0;
5515 else if (tree_int_cst_lt (last, first))
5517 error_init ("empty index range in initializer");
5518 last = 0;
5520 else
5522 last = convert (bitsizetype, last);
5523 if (constructor_max_index != 0
5524 && tree_int_cst_lt (constructor_max_index, last))
5526 error_init ("array index range in initializer exceeds array bounds");
5527 last = 0;
5532 designator_depth++;
5533 designator_erroneous = 0;
5534 if (constructor_range_stack || last)
5535 push_range_stack (last);
5539 /* Within a struct initializer, specify the next field to be initialized. */
5541 void
5542 set_init_label (tree fieldname)
5544 tree tail;
5546 if (set_designator (0))
5547 return;
5549 designator_erroneous = 1;
5551 if (TREE_CODE (constructor_type) != RECORD_TYPE
5552 && TREE_CODE (constructor_type) != UNION_TYPE)
5554 error_init ("field name not in record or union initializer");
5555 return;
5558 for (tail = TYPE_FIELDS (constructor_type); tail;
5559 tail = TREE_CHAIN (tail))
5561 if (DECL_NAME (tail) == fieldname)
5562 break;
5565 if (tail == 0)
5566 error ("unknown field %qE specified in initializer", fieldname);
5567 else
5569 constructor_fields = tail;
5570 designator_depth++;
5571 designator_erroneous = 0;
5572 if (constructor_range_stack)
5573 push_range_stack (NULL_TREE);
5577 /* Add a new initializer to the tree of pending initializers. PURPOSE
5578 identifies the initializer, either array index or field in a structure.
5579 VALUE is the value of that index or field. */
5581 static void
5582 add_pending_init (tree purpose, tree value)
5584 struct init_node *p, **q, *r;
5586 q = &constructor_pending_elts;
5587 p = 0;
5589 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5591 while (*q != 0)
5593 p = *q;
5594 if (tree_int_cst_lt (purpose, p->purpose))
5595 q = &p->left;
5596 else if (tree_int_cst_lt (p->purpose, purpose))
5597 q = &p->right;
5598 else
5600 if (TREE_SIDE_EFFECTS (p->value))
5601 warning_init ("initialized field with side-effects overwritten");
5602 p->value = value;
5603 return;
5607 else
5609 tree bitpos;
5611 bitpos = bit_position (purpose);
5612 while (*q != NULL)
5614 p = *q;
5615 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5616 q = &p->left;
5617 else if (p->purpose != purpose)
5618 q = &p->right;
5619 else
5621 if (TREE_SIDE_EFFECTS (p->value))
5622 warning_init ("initialized field with side-effects overwritten");
5623 p->value = value;
5624 return;
5629 r = GGC_NEW (struct init_node);
5630 r->purpose = purpose;
5631 r->value = value;
5633 *q = r;
5634 r->parent = p;
5635 r->left = 0;
5636 r->right = 0;
5637 r->balance = 0;
5639 while (p)
5641 struct init_node *s;
5643 if (r == p->left)
5645 if (p->balance == 0)
5646 p->balance = -1;
5647 else if (p->balance < 0)
5649 if (r->balance < 0)
5651 /* L rotation. */
5652 p->left = r->right;
5653 if (p->left)
5654 p->left->parent = p;
5655 r->right = p;
5657 p->balance = 0;
5658 r->balance = 0;
5660 s = p->parent;
5661 p->parent = r;
5662 r->parent = s;
5663 if (s)
5665 if (s->left == p)
5666 s->left = r;
5667 else
5668 s->right = r;
5670 else
5671 constructor_pending_elts = r;
5673 else
5675 /* LR rotation. */
5676 struct init_node *t = r->right;
5678 r->right = t->left;
5679 if (r->right)
5680 r->right->parent = r;
5681 t->left = r;
5683 p->left = t->right;
5684 if (p->left)
5685 p->left->parent = p;
5686 t->right = p;
5688 p->balance = t->balance < 0;
5689 r->balance = -(t->balance > 0);
5690 t->balance = 0;
5692 s = p->parent;
5693 p->parent = t;
5694 r->parent = t;
5695 t->parent = s;
5696 if (s)
5698 if (s->left == p)
5699 s->left = t;
5700 else
5701 s->right = t;
5703 else
5704 constructor_pending_elts = t;
5706 break;
5708 else
5710 /* p->balance == +1; growth of left side balances the node. */
5711 p->balance = 0;
5712 break;
5715 else /* r == p->right */
5717 if (p->balance == 0)
5718 /* Growth propagation from right side. */
5719 p->balance++;
5720 else if (p->balance > 0)
5722 if (r->balance > 0)
5724 /* R rotation. */
5725 p->right = r->left;
5726 if (p->right)
5727 p->right->parent = p;
5728 r->left = p;
5730 p->balance = 0;
5731 r->balance = 0;
5733 s = p->parent;
5734 p->parent = r;
5735 r->parent = s;
5736 if (s)
5738 if (s->left == p)
5739 s->left = r;
5740 else
5741 s->right = r;
5743 else
5744 constructor_pending_elts = r;
5746 else /* r->balance == -1 */
5748 /* RL rotation */
5749 struct init_node *t = r->left;
5751 r->left = t->right;
5752 if (r->left)
5753 r->left->parent = r;
5754 t->right = r;
5756 p->right = t->left;
5757 if (p->right)
5758 p->right->parent = p;
5759 t->left = p;
5761 r->balance = (t->balance < 0);
5762 p->balance = -(t->balance > 0);
5763 t->balance = 0;
5765 s = p->parent;
5766 p->parent = t;
5767 r->parent = t;
5768 t->parent = s;
5769 if (s)
5771 if (s->left == p)
5772 s->left = t;
5773 else
5774 s->right = t;
5776 else
5777 constructor_pending_elts = t;
5779 break;
5781 else
5783 /* p->balance == -1; growth of right side balances the node. */
5784 p->balance = 0;
5785 break;
5789 r = p;
5790 p = p->parent;
5794 /* Build AVL tree from a sorted chain. */
5796 static void
5797 set_nonincremental_init (void)
5799 unsigned HOST_WIDE_INT ix;
5800 tree index, value;
5802 if (TREE_CODE (constructor_type) != RECORD_TYPE
5803 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5804 return;
5806 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
5807 add_pending_init (index, value);
5808 constructor_elements = 0;
5809 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5811 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5812 /* Skip any nameless bit fields at the beginning. */
5813 while (constructor_unfilled_fields != 0
5814 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5815 && DECL_NAME (constructor_unfilled_fields) == 0)
5816 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5819 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5821 if (TYPE_DOMAIN (constructor_type))
5822 constructor_unfilled_index
5823 = convert (bitsizetype,
5824 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5825 else
5826 constructor_unfilled_index = bitsize_zero_node;
5828 constructor_incremental = 0;
5831 /* Build AVL tree from a string constant. */
5833 static void
5834 set_nonincremental_init_from_string (tree str)
5836 tree value, purpose, type;
5837 HOST_WIDE_INT val[2];
5838 const char *p, *end;
5839 int byte, wchar_bytes, charwidth, bitpos;
5841 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5843 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5844 == TYPE_PRECISION (char_type_node))
5845 wchar_bytes = 1;
5846 else
5848 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5849 == TYPE_PRECISION (wchar_type_node));
5850 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5852 charwidth = TYPE_PRECISION (char_type_node);
5853 type = TREE_TYPE (constructor_type);
5854 p = TREE_STRING_POINTER (str);
5855 end = p + TREE_STRING_LENGTH (str);
5857 for (purpose = bitsize_zero_node;
5858 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5859 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5861 if (wchar_bytes == 1)
5863 val[1] = (unsigned char) *p++;
5864 val[0] = 0;
5866 else
5868 val[0] = 0;
5869 val[1] = 0;
5870 for (byte = 0; byte < wchar_bytes; byte++)
5872 if (BYTES_BIG_ENDIAN)
5873 bitpos = (wchar_bytes - byte - 1) * charwidth;
5874 else
5875 bitpos = byte * charwidth;
5876 val[bitpos < HOST_BITS_PER_WIDE_INT]
5877 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5878 << (bitpos % HOST_BITS_PER_WIDE_INT);
5882 if (!TYPE_UNSIGNED (type))
5884 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5885 if (bitpos < HOST_BITS_PER_WIDE_INT)
5887 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5889 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5890 val[0] = -1;
5893 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5895 if (val[1] < 0)
5896 val[0] = -1;
5898 else if (val[0] & (((HOST_WIDE_INT) 1)
5899 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5900 val[0] |= ((HOST_WIDE_INT) -1)
5901 << (bitpos - HOST_BITS_PER_WIDE_INT);
5904 value = build_int_cst_wide (type, val[1], val[0]);
5905 add_pending_init (purpose, value);
5908 constructor_incremental = 0;
5911 /* Return value of FIELD in pending initializer or zero if the field was
5912 not initialized yet. */
5914 static tree
5915 find_init_member (tree field)
5917 struct init_node *p;
5919 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5921 if (constructor_incremental
5922 && tree_int_cst_lt (field, constructor_unfilled_index))
5923 set_nonincremental_init ();
5925 p = constructor_pending_elts;
5926 while (p)
5928 if (tree_int_cst_lt (field, p->purpose))
5929 p = p->left;
5930 else if (tree_int_cst_lt (p->purpose, field))
5931 p = p->right;
5932 else
5933 return p->value;
5936 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5938 tree bitpos = bit_position (field);
5940 if (constructor_incremental
5941 && (!constructor_unfilled_fields
5942 || tree_int_cst_lt (bitpos,
5943 bit_position (constructor_unfilled_fields))))
5944 set_nonincremental_init ();
5946 p = constructor_pending_elts;
5947 while (p)
5949 if (field == p->purpose)
5950 return p->value;
5951 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5952 p = p->left;
5953 else
5954 p = p->right;
5957 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5959 if (!VEC_empty (constructor_elt, constructor_elements)
5960 && (VEC_last (constructor_elt, constructor_elements)->index
5961 == field))
5962 return VEC_last (constructor_elt, constructor_elements)->value;
5964 return 0;
5967 /* "Output" the next constructor element.
5968 At top level, really output it to assembler code now.
5969 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5970 TYPE is the data type that the containing data type wants here.
5971 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5972 If VALUE is a string constant, STRICT_STRING is true if it is
5973 unparenthesized or we should not warn here for it being parenthesized.
5974 For other types of VALUE, STRICT_STRING is not used.
5976 PENDING if non-nil means output pending elements that belong
5977 right after this element. (PENDING is normally 1;
5978 it is 0 while outputting pending elements, to avoid recursion.) */
5980 static void
5981 output_init_element (tree value, bool strict_string, tree type, tree field,
5982 int pending)
5984 constructor_elt *celt;
5986 if (type == error_mark_node || value == error_mark_node)
5988 constructor_erroneous = 1;
5989 return;
5991 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5992 && (TREE_CODE (value) == STRING_CST
5993 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
5994 && !(TREE_CODE (value) == STRING_CST
5995 && TREE_CODE (type) == ARRAY_TYPE
5996 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5997 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5998 TYPE_MAIN_VARIANT (type)))
5999 value = array_to_pointer_conversion (value);
6001 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6002 && require_constant_value && !flag_isoc99 && pending)
6004 /* As an extension, allow initializing objects with static storage
6005 duration with compound literals (which are then treated just as
6006 the brace enclosed list they contain). */
6007 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6008 value = DECL_INITIAL (decl);
6011 if (value == error_mark_node)
6012 constructor_erroneous = 1;
6013 else if (!TREE_CONSTANT (value))
6014 constructor_constant = 0;
6015 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
6016 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6017 || TREE_CODE (constructor_type) == UNION_TYPE)
6018 && DECL_C_BIT_FIELD (field)
6019 && TREE_CODE (value) != INTEGER_CST))
6020 constructor_simple = 0;
6022 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
6024 if (require_constant_value)
6026 error_init ("initializer element is not constant");
6027 value = error_mark_node;
6029 else if (require_constant_elements)
6030 pedwarn ("initializer element is not computable at load time");
6033 /* If this field is empty (and not at the end of structure),
6034 don't do anything other than checking the initializer. */
6035 if (field
6036 && (TREE_TYPE (field) == error_mark_node
6037 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6038 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6039 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6040 || TREE_CHAIN (field)))))
6041 return;
6043 value = digest_init (type, value, strict_string, require_constant_value);
6044 if (value == error_mark_node)
6046 constructor_erroneous = 1;
6047 return;
6050 /* If this element doesn't come next in sequence,
6051 put it on constructor_pending_elts. */
6052 if (TREE_CODE (constructor_type) == ARRAY_TYPE
6053 && (!constructor_incremental
6054 || !tree_int_cst_equal (field, constructor_unfilled_index)))
6056 if (constructor_incremental
6057 && tree_int_cst_lt (field, constructor_unfilled_index))
6058 set_nonincremental_init ();
6060 add_pending_init (field, value);
6061 return;
6063 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6064 && (!constructor_incremental
6065 || field != constructor_unfilled_fields))
6067 /* We do this for records but not for unions. In a union,
6068 no matter which field is specified, it can be initialized
6069 right away since it starts at the beginning of the union. */
6070 if (constructor_incremental)
6072 if (!constructor_unfilled_fields)
6073 set_nonincremental_init ();
6074 else
6076 tree bitpos, unfillpos;
6078 bitpos = bit_position (field);
6079 unfillpos = bit_position (constructor_unfilled_fields);
6081 if (tree_int_cst_lt (bitpos, unfillpos))
6082 set_nonincremental_init ();
6086 add_pending_init (field, value);
6087 return;
6089 else if (TREE_CODE (constructor_type) == UNION_TYPE
6090 && !VEC_empty (constructor_elt, constructor_elements))
6092 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
6093 constructor_elements)->value))
6094 warning_init ("initialized field with side-effects overwritten");
6096 /* We can have just one union field set. */
6097 constructor_elements = 0;
6100 /* Otherwise, output this element either to
6101 constructor_elements or to the assembler file. */
6103 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
6104 celt->index = field;
6105 celt->value = value;
6107 /* Advance the variable that indicates sequential elements output. */
6108 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6109 constructor_unfilled_index
6110 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6111 bitsize_one_node);
6112 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6114 constructor_unfilled_fields
6115 = TREE_CHAIN (constructor_unfilled_fields);
6117 /* Skip any nameless bit fields. */
6118 while (constructor_unfilled_fields != 0
6119 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6120 && DECL_NAME (constructor_unfilled_fields) == 0)
6121 constructor_unfilled_fields =
6122 TREE_CHAIN (constructor_unfilled_fields);
6124 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6125 constructor_unfilled_fields = 0;
6127 /* Now output any pending elements which have become next. */
6128 if (pending)
6129 output_pending_init_elements (0);
6132 /* Output any pending elements which have become next.
6133 As we output elements, constructor_unfilled_{fields,index}
6134 advances, which may cause other elements to become next;
6135 if so, they too are output.
6137 If ALL is 0, we return when there are
6138 no more pending elements to output now.
6140 If ALL is 1, we output space as necessary so that
6141 we can output all the pending elements. */
6143 static void
6144 output_pending_init_elements (int all)
6146 struct init_node *elt = constructor_pending_elts;
6147 tree next;
6149 retry:
6151 /* Look through the whole pending tree.
6152 If we find an element that should be output now,
6153 output it. Otherwise, set NEXT to the element
6154 that comes first among those still pending. */
6156 next = 0;
6157 while (elt)
6159 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6161 if (tree_int_cst_equal (elt->purpose,
6162 constructor_unfilled_index))
6163 output_init_element (elt->value, true,
6164 TREE_TYPE (constructor_type),
6165 constructor_unfilled_index, 0);
6166 else if (tree_int_cst_lt (constructor_unfilled_index,
6167 elt->purpose))
6169 /* Advance to the next smaller node. */
6170 if (elt->left)
6171 elt = elt->left;
6172 else
6174 /* We have reached the smallest node bigger than the
6175 current unfilled index. Fill the space first. */
6176 next = elt->purpose;
6177 break;
6180 else
6182 /* Advance to the next bigger node. */
6183 if (elt->right)
6184 elt = elt->right;
6185 else
6187 /* We have reached the biggest node in a subtree. Find
6188 the parent of it, which is the next bigger node. */
6189 while (elt->parent && elt->parent->right == elt)
6190 elt = elt->parent;
6191 elt = elt->parent;
6192 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6193 elt->purpose))
6195 next = elt->purpose;
6196 break;
6201 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6202 || TREE_CODE (constructor_type) == UNION_TYPE)
6204 tree ctor_unfilled_bitpos, elt_bitpos;
6206 /* If the current record is complete we are done. */
6207 if (constructor_unfilled_fields == 0)
6208 break;
6210 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6211 elt_bitpos = bit_position (elt->purpose);
6212 /* We can't compare fields here because there might be empty
6213 fields in between. */
6214 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6216 constructor_unfilled_fields = elt->purpose;
6217 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
6218 elt->purpose, 0);
6220 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6222 /* Advance to the next smaller node. */
6223 if (elt->left)
6224 elt = elt->left;
6225 else
6227 /* We have reached the smallest node bigger than the
6228 current unfilled field. Fill the space first. */
6229 next = elt->purpose;
6230 break;
6233 else
6235 /* Advance to the next bigger node. */
6236 if (elt->right)
6237 elt = elt->right;
6238 else
6240 /* We have reached the biggest node in a subtree. Find
6241 the parent of it, which is the next bigger node. */
6242 while (elt->parent && elt->parent->right == elt)
6243 elt = elt->parent;
6244 elt = elt->parent;
6245 if (elt
6246 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6247 bit_position (elt->purpose))))
6249 next = elt->purpose;
6250 break;
6257 /* Ordinarily return, but not if we want to output all
6258 and there are elements left. */
6259 if (!(all && next != 0))
6260 return;
6262 /* If it's not incremental, just skip over the gap, so that after
6263 jumping to retry we will output the next successive element. */
6264 if (TREE_CODE (constructor_type) == RECORD_TYPE
6265 || TREE_CODE (constructor_type) == UNION_TYPE)
6266 constructor_unfilled_fields = next;
6267 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6268 constructor_unfilled_index = next;
6270 /* ELT now points to the node in the pending tree with the next
6271 initializer to output. */
6272 goto retry;
6275 /* Add one non-braced element to the current constructor level.
6276 This adjusts the current position within the constructor's type.
6277 This may also start or terminate implicit levels
6278 to handle a partly-braced initializer.
6280 Once this has found the correct level for the new element,
6281 it calls output_init_element. */
6283 void
6284 process_init_element (struct c_expr value)
6286 tree orig_value = value.value;
6287 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6288 bool strict_string = value.original_code == STRING_CST;
6290 designator_depth = 0;
6291 designator_erroneous = 0;
6293 /* Handle superfluous braces around string cst as in
6294 char x[] = {"foo"}; */
6295 if (string_flag
6296 && constructor_type
6297 && TREE_CODE (constructor_type) == ARRAY_TYPE
6298 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6299 && integer_zerop (constructor_unfilled_index))
6301 if (constructor_stack->replacement_value.value)
6302 error_init ("excess elements in char array initializer");
6303 constructor_stack->replacement_value = value;
6304 return;
6307 if (constructor_stack->replacement_value.value != 0)
6309 error_init ("excess elements in struct initializer");
6310 return;
6313 /* Ignore elements of a brace group if it is entirely superfluous
6314 and has already been diagnosed. */
6315 if (constructor_type == 0)
6316 return;
6318 /* If we've exhausted any levels that didn't have braces,
6319 pop them now. */
6320 while (constructor_stack->implicit)
6322 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6323 || TREE_CODE (constructor_type) == UNION_TYPE)
6324 && constructor_fields == 0)
6325 process_init_element (pop_init_level (1));
6326 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6327 && (constructor_max_index == 0
6328 || tree_int_cst_lt (constructor_max_index,
6329 constructor_index)))
6330 process_init_element (pop_init_level (1));
6331 else
6332 break;
6335 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6336 if (constructor_range_stack)
6338 /* If value is a compound literal and we'll be just using its
6339 content, don't put it into a SAVE_EXPR. */
6340 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6341 || !require_constant_value
6342 || flag_isoc99)
6343 value.value = save_expr (value.value);
6346 while (1)
6348 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6350 tree fieldtype;
6351 enum tree_code fieldcode;
6353 if (constructor_fields == 0)
6355 pedwarn_init ("excess elements in struct initializer");
6356 break;
6359 fieldtype = TREE_TYPE (constructor_fields);
6360 if (fieldtype != error_mark_node)
6361 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6362 fieldcode = TREE_CODE (fieldtype);
6364 /* Error for non-static initialization of a flexible array member. */
6365 if (fieldcode == ARRAY_TYPE
6366 && !require_constant_value
6367 && TYPE_SIZE (fieldtype) == NULL_TREE
6368 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6370 error_init ("non-static initialization of a flexible array member");
6371 break;
6374 /* Accept a string constant to initialize a subarray. */
6375 if (value.value != 0
6376 && fieldcode == ARRAY_TYPE
6377 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6378 && string_flag)
6379 value.value = orig_value;
6380 /* Otherwise, if we have come to a subaggregate,
6381 and we don't have an element of its type, push into it. */
6382 else if (value.value != 0
6383 && value.value != error_mark_node
6384 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6385 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6386 || fieldcode == UNION_TYPE))
6388 push_init_level (1);
6389 continue;
6392 if (value.value)
6394 push_member_name (constructor_fields);
6395 output_init_element (value.value, strict_string,
6396 fieldtype, constructor_fields, 1);
6397 RESTORE_SPELLING_DEPTH (constructor_depth);
6399 else
6400 /* Do the bookkeeping for an element that was
6401 directly output as a constructor. */
6403 /* For a record, keep track of end position of last field. */
6404 if (DECL_SIZE (constructor_fields))
6405 constructor_bit_index
6406 = size_binop (PLUS_EXPR,
6407 bit_position (constructor_fields),
6408 DECL_SIZE (constructor_fields));
6410 /* If the current field was the first one not yet written out,
6411 it isn't now, so update. */
6412 if (constructor_unfilled_fields == constructor_fields)
6414 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6415 /* Skip any nameless bit fields. */
6416 while (constructor_unfilled_fields != 0
6417 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6418 && DECL_NAME (constructor_unfilled_fields) == 0)
6419 constructor_unfilled_fields =
6420 TREE_CHAIN (constructor_unfilled_fields);
6424 constructor_fields = TREE_CHAIN (constructor_fields);
6425 /* Skip any nameless bit fields at the beginning. */
6426 while (constructor_fields != 0
6427 && DECL_C_BIT_FIELD (constructor_fields)
6428 && DECL_NAME (constructor_fields) == 0)
6429 constructor_fields = TREE_CHAIN (constructor_fields);
6431 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6433 tree fieldtype;
6434 enum tree_code fieldcode;
6436 if (constructor_fields == 0)
6438 pedwarn_init ("excess elements in union initializer");
6439 break;
6442 fieldtype = TREE_TYPE (constructor_fields);
6443 if (fieldtype != error_mark_node)
6444 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6445 fieldcode = TREE_CODE (fieldtype);
6447 /* Warn that traditional C rejects initialization of unions.
6448 We skip the warning if the value is zero. This is done
6449 under the assumption that the zero initializer in user
6450 code appears conditioned on e.g. __STDC__ to avoid
6451 "missing initializer" warnings and relies on default
6452 initialization to zero in the traditional C case.
6453 We also skip the warning if the initializer is designated,
6454 again on the assumption that this must be conditional on
6455 __STDC__ anyway (and we've already complained about the
6456 member-designator already). */
6457 if (!in_system_header && !constructor_designated
6458 && !(value.value && (integer_zerop (value.value)
6459 || real_zerop (value.value))))
6460 warning (OPT_Wtraditional, "traditional C rejects initialization "
6461 "of unions");
6463 /* Accept a string constant to initialize a subarray. */
6464 if (value.value != 0
6465 && fieldcode == ARRAY_TYPE
6466 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6467 && string_flag)
6468 value.value = orig_value;
6469 /* Otherwise, if we have come to a subaggregate,
6470 and we don't have an element of its type, push into it. */
6471 else if (value.value != 0
6472 && value.value != error_mark_node
6473 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6474 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6475 || fieldcode == UNION_TYPE))
6477 push_init_level (1);
6478 continue;
6481 if (value.value)
6483 push_member_name (constructor_fields);
6484 output_init_element (value.value, strict_string,
6485 fieldtype, constructor_fields, 1);
6486 RESTORE_SPELLING_DEPTH (constructor_depth);
6488 else
6489 /* Do the bookkeeping for an element that was
6490 directly output as a constructor. */
6492 constructor_bit_index = DECL_SIZE (constructor_fields);
6493 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6496 constructor_fields = 0;
6498 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6500 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6501 enum tree_code eltcode = TREE_CODE (elttype);
6503 /* Accept a string constant to initialize a subarray. */
6504 if (value.value != 0
6505 && eltcode == ARRAY_TYPE
6506 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6507 && string_flag)
6508 value.value = orig_value;
6509 /* Otherwise, if we have come to a subaggregate,
6510 and we don't have an element of its type, push into it. */
6511 else if (value.value != 0
6512 && value.value != error_mark_node
6513 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6514 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6515 || eltcode == UNION_TYPE))
6517 push_init_level (1);
6518 continue;
6521 if (constructor_max_index != 0
6522 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6523 || integer_all_onesp (constructor_max_index)))
6525 pedwarn_init ("excess elements in array initializer");
6526 break;
6529 /* Now output the actual element. */
6530 if (value.value)
6532 push_array_bounds (tree_low_cst (constructor_index, 0));
6533 output_init_element (value.value, strict_string,
6534 elttype, constructor_index, 1);
6535 RESTORE_SPELLING_DEPTH (constructor_depth);
6538 constructor_index
6539 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6541 if (!value.value)
6542 /* If we are doing the bookkeeping for an element that was
6543 directly output as a constructor, we must update
6544 constructor_unfilled_index. */
6545 constructor_unfilled_index = constructor_index;
6547 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6549 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6551 /* Do a basic check of initializer size. Note that vectors
6552 always have a fixed size derived from their type. */
6553 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6555 pedwarn_init ("excess elements in vector initializer");
6556 break;
6559 /* Now output the actual element. */
6560 if (value.value)
6561 output_init_element (value.value, strict_string,
6562 elttype, constructor_index, 1);
6564 constructor_index
6565 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6567 if (!value.value)
6568 /* If we are doing the bookkeeping for an element that was
6569 directly output as a constructor, we must update
6570 constructor_unfilled_index. */
6571 constructor_unfilled_index = constructor_index;
6574 /* Handle the sole element allowed in a braced initializer
6575 for a scalar variable. */
6576 else if (constructor_type != error_mark_node
6577 && constructor_fields == 0)
6579 pedwarn_init ("excess elements in scalar initializer");
6580 break;
6582 else
6584 if (value.value)
6585 output_init_element (value.value, strict_string,
6586 constructor_type, NULL_TREE, 1);
6587 constructor_fields = 0;
6590 /* Handle range initializers either at this level or anywhere higher
6591 in the designator stack. */
6592 if (constructor_range_stack)
6594 struct constructor_range_stack *p, *range_stack;
6595 int finish = 0;
6597 range_stack = constructor_range_stack;
6598 constructor_range_stack = 0;
6599 while (constructor_stack != range_stack->stack)
6601 gcc_assert (constructor_stack->implicit);
6602 process_init_element (pop_init_level (1));
6604 for (p = range_stack;
6605 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6606 p = p->prev)
6608 gcc_assert (constructor_stack->implicit);
6609 process_init_element (pop_init_level (1));
6612 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6613 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6614 finish = 1;
6616 while (1)
6618 constructor_index = p->index;
6619 constructor_fields = p->fields;
6620 if (finish && p->range_end && p->index == p->range_start)
6622 finish = 0;
6623 p->prev = 0;
6625 p = p->next;
6626 if (!p)
6627 break;
6628 push_init_level (2);
6629 p->stack = constructor_stack;
6630 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6631 p->index = p->range_start;
6634 if (!finish)
6635 constructor_range_stack = range_stack;
6636 continue;
6639 break;
6642 constructor_range_stack = 0;
6645 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6646 (guaranteed to be 'volatile' or null) and ARGS (represented using
6647 an ASM_EXPR node). */
6648 tree
6649 build_asm_stmt (tree cv_qualifier, tree args)
6651 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6652 ASM_VOLATILE_P (args) = 1;
6653 return add_stmt (args);
6656 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6657 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6658 SIMPLE indicates whether there was anything at all after the
6659 string in the asm expression -- asm("blah") and asm("blah" : )
6660 are subtly different. We use a ASM_EXPR node to represent this. */
6661 tree
6662 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6663 bool simple)
6665 tree tail;
6666 tree args;
6667 int i;
6668 const char *constraint;
6669 const char **oconstraints;
6670 bool allows_mem, allows_reg, is_inout;
6671 int ninputs, noutputs;
6673 ninputs = list_length (inputs);
6674 noutputs = list_length (outputs);
6675 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6677 string = resolve_asm_operand_names (string, outputs, inputs);
6679 /* Remove output conversions that change the type but not the mode. */
6680 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6682 tree output = TREE_VALUE (tail);
6684 /* ??? Really, this should not be here. Users should be using a
6685 proper lvalue, dammit. But there's a long history of using casts
6686 in the output operands. In cases like longlong.h, this becomes a
6687 primitive form of typechecking -- if the cast can be removed, then
6688 the output operand had a type of the proper width; otherwise we'll
6689 get an error. Gross, but ... */
6690 STRIP_NOPS (output);
6692 if (!lvalue_or_else (output, lv_asm))
6693 output = error_mark_node;
6695 if (output != error_mark_node
6696 && (TREE_READONLY (output)
6697 || TYPE_READONLY (TREE_TYPE (output))
6698 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
6699 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
6700 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
6701 readonly_error (output, lv_asm);
6703 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6704 oconstraints[i] = constraint;
6706 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6707 &allows_mem, &allows_reg, &is_inout))
6709 /* If the operand is going to end up in memory,
6710 mark it addressable. */
6711 if (!allows_reg && !c_mark_addressable (output))
6712 output = error_mark_node;
6714 else
6715 output = error_mark_node;
6717 TREE_VALUE (tail) = output;
6720 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6722 tree input;
6724 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6725 input = TREE_VALUE (tail);
6727 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6728 oconstraints, &allows_mem, &allows_reg))
6730 /* If the operand is going to end up in memory,
6731 mark it addressable. */
6732 if (!allows_reg && allows_mem)
6734 /* Strip the nops as we allow this case. FIXME, this really
6735 should be rejected or made deprecated. */
6736 STRIP_NOPS (input);
6737 if (!c_mark_addressable (input))
6738 input = error_mark_node;
6741 else
6742 input = error_mark_node;
6744 TREE_VALUE (tail) = input;
6747 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6749 /* asm statements without outputs, including simple ones, are treated
6750 as volatile. */
6751 ASM_INPUT_P (args) = simple;
6752 ASM_VOLATILE_P (args) = (noutputs == 0);
6754 return args;
6757 /* Generate a goto statement to LABEL. */
6759 tree
6760 c_finish_goto_label (tree label)
6762 tree decl = lookup_label (label);
6763 if (!decl)
6764 return NULL_TREE;
6766 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
6768 error ("jump into statement expression");
6769 return NULL_TREE;
6772 if (C_DECL_UNJUMPABLE_VM (decl))
6774 error ("jump into scope of identifier with variably modified type");
6775 return NULL_TREE;
6778 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
6780 /* No jump from outside this statement expression context, so
6781 record that there is a jump from within this context. */
6782 struct c_label_list *nlist;
6783 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6784 nlist->next = label_context_stack_se->labels_used;
6785 nlist->label = decl;
6786 label_context_stack_se->labels_used = nlist;
6789 if (!C_DECL_UNDEFINABLE_VM (decl))
6791 /* No jump from outside this context context of identifiers with
6792 variably modified type, so record that there is a jump from
6793 within this context. */
6794 struct c_label_list *nlist;
6795 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6796 nlist->next = label_context_stack_vm->labels_used;
6797 nlist->label = decl;
6798 label_context_stack_vm->labels_used = nlist;
6801 TREE_USED (decl) = 1;
6802 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6805 /* Generate a computed goto statement to EXPR. */
6807 tree
6808 c_finish_goto_ptr (tree expr)
6810 if (pedantic)
6811 pedwarn ("ISO C forbids %<goto *expr;%>");
6812 expr = convert (ptr_type_node, expr);
6813 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6816 /* Generate a C `return' statement. RETVAL is the expression for what
6817 to return, or a null pointer for `return;' with no value. */
6819 tree
6820 c_finish_return (tree retval)
6822 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
6823 bool no_warning = false;
6825 if (TREE_THIS_VOLATILE (current_function_decl))
6826 warning (0, "function declared %<noreturn%> has a %<return%> statement");
6828 if (!retval)
6830 current_function_returns_null = 1;
6831 if ((warn_return_type || flag_isoc99)
6832 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6834 pedwarn_c99 ("%<return%> with no value, in "
6835 "function returning non-void");
6836 no_warning = true;
6839 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6841 current_function_returns_null = 1;
6842 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6843 pedwarn ("%<return%> with a value, in function returning void");
6845 else
6847 tree t = convert_for_assignment (valtype, retval, ic_return,
6848 NULL_TREE, NULL_TREE, 0);
6849 tree res = DECL_RESULT (current_function_decl);
6850 tree inner;
6852 current_function_returns_value = 1;
6853 if (t == error_mark_node)
6854 return NULL_TREE;
6856 inner = t = convert (TREE_TYPE (res), t);
6858 /* Strip any conversions, additions, and subtractions, and see if
6859 we are returning the address of a local variable. Warn if so. */
6860 while (1)
6862 switch (TREE_CODE (inner))
6864 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6865 case PLUS_EXPR:
6866 inner = TREE_OPERAND (inner, 0);
6867 continue;
6869 case MINUS_EXPR:
6870 /* If the second operand of the MINUS_EXPR has a pointer
6871 type (or is converted from it), this may be valid, so
6872 don't give a warning. */
6874 tree op1 = TREE_OPERAND (inner, 1);
6876 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6877 && (TREE_CODE (op1) == NOP_EXPR
6878 || TREE_CODE (op1) == NON_LVALUE_EXPR
6879 || TREE_CODE (op1) == CONVERT_EXPR))
6880 op1 = TREE_OPERAND (op1, 0);
6882 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6883 break;
6885 inner = TREE_OPERAND (inner, 0);
6886 continue;
6889 case ADDR_EXPR:
6890 inner = TREE_OPERAND (inner, 0);
6892 while (REFERENCE_CLASS_P (inner)
6893 && TREE_CODE (inner) != INDIRECT_REF)
6894 inner = TREE_OPERAND (inner, 0);
6896 if (DECL_P (inner)
6897 && !DECL_EXTERNAL (inner)
6898 && !TREE_STATIC (inner)
6899 && DECL_CONTEXT (inner) == current_function_decl)
6900 warning (0, "function returns address of local variable");
6901 break;
6903 default:
6904 break;
6907 break;
6910 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6913 ret_stmt = build_stmt (RETURN_EXPR, retval);
6914 TREE_NO_WARNING (ret_stmt) |= no_warning;
6915 return add_stmt (ret_stmt);
6918 struct c_switch {
6919 /* The SWITCH_EXPR being built. */
6920 tree switch_expr;
6922 /* The original type of the testing expression, i.e. before the
6923 default conversion is applied. */
6924 tree orig_type;
6926 /* A splay-tree mapping the low element of a case range to the high
6927 element, or NULL_TREE if there is no high element. Used to
6928 determine whether or not a new case label duplicates an old case
6929 label. We need a tree, rather than simply a hash table, because
6930 of the GNU case range extension. */
6931 splay_tree cases;
6933 /* Number of nested statement expressions within this switch
6934 statement; if nonzero, case and default labels may not
6935 appear. */
6936 unsigned int blocked_stmt_expr;
6938 /* Scope of outermost declarations of identifiers with variably
6939 modified type within this switch statement; if nonzero, case and
6940 default labels may not appear. */
6941 unsigned int blocked_vm;
6943 /* The next node on the stack. */
6944 struct c_switch *next;
6947 /* A stack of the currently active switch statements. The innermost
6948 switch statement is on the top of the stack. There is no need to
6949 mark the stack for garbage collection because it is only active
6950 during the processing of the body of a function, and we never
6951 collect at that point. */
6953 struct c_switch *c_switch_stack;
6955 /* Start a C switch statement, testing expression EXP. Return the new
6956 SWITCH_EXPR. */
6958 tree
6959 c_start_case (tree exp)
6961 enum tree_code code;
6962 tree type, orig_type = error_mark_node;
6963 struct c_switch *cs;
6965 if (exp != error_mark_node)
6967 code = TREE_CODE (TREE_TYPE (exp));
6968 orig_type = TREE_TYPE (exp);
6970 if (!INTEGRAL_TYPE_P (orig_type)
6971 && code != ERROR_MARK)
6973 error ("switch quantity not an integer");
6974 exp = integer_zero_node;
6975 orig_type = error_mark_node;
6977 else
6979 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6981 if (!in_system_header
6982 && (type == long_integer_type_node
6983 || type == long_unsigned_type_node))
6984 warning (OPT_Wtraditional, "%<long%> switch expression not "
6985 "converted to %<int%> in ISO C");
6987 exp = default_conversion (exp);
6988 type = TREE_TYPE (exp);
6992 /* Add this new SWITCH_EXPR to the stack. */
6993 cs = XNEW (struct c_switch);
6994 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
6995 cs->orig_type = orig_type;
6996 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6997 cs->blocked_stmt_expr = 0;
6998 cs->blocked_vm = 0;
6999 cs->next = c_switch_stack;
7000 c_switch_stack = cs;
7002 return add_stmt (cs->switch_expr);
7005 /* Process a case label. */
7007 tree
7008 do_case (tree low_value, tree high_value)
7010 tree label = NULL_TREE;
7012 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
7013 && !c_switch_stack->blocked_vm)
7015 label = c_add_case_label (c_switch_stack->cases,
7016 SWITCH_COND (c_switch_stack->switch_expr),
7017 c_switch_stack->orig_type,
7018 low_value, high_value);
7019 if (label == error_mark_node)
7020 label = NULL_TREE;
7022 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
7024 if (low_value)
7025 error ("case label in statement expression not containing "
7026 "enclosing switch statement");
7027 else
7028 error ("%<default%> label in statement expression not containing "
7029 "enclosing switch statement");
7031 else if (c_switch_stack && c_switch_stack->blocked_vm)
7033 if (low_value)
7034 error ("case label in scope of identifier with variably modified "
7035 "type not containing enclosing switch statement");
7036 else
7037 error ("%<default%> label in scope of identifier with variably "
7038 "modified type not containing enclosing switch statement");
7040 else if (low_value)
7041 error ("case label not within a switch statement");
7042 else
7043 error ("%<default%> label not within a switch statement");
7045 return label;
7048 /* Finish the switch statement. */
7050 void
7051 c_finish_case (tree body)
7053 struct c_switch *cs = c_switch_stack;
7054 location_t switch_location;
7056 SWITCH_BODY (cs->switch_expr) = body;
7058 /* We must not be within a statement expression nested in the switch
7059 at this point; we might, however, be within the scope of an
7060 identifier with variably modified type nested in the switch. */
7061 gcc_assert (!cs->blocked_stmt_expr);
7063 /* Emit warnings as needed. */
7064 if (EXPR_HAS_LOCATION (cs->switch_expr))
7065 switch_location = EXPR_LOCATION (cs->switch_expr);
7066 else
7067 switch_location = input_location;
7068 c_do_switch_warnings (cs->cases, switch_location,
7069 TREE_TYPE (cs->switch_expr),
7070 SWITCH_COND (cs->switch_expr));
7072 /* Pop the stack. */
7073 c_switch_stack = cs->next;
7074 splay_tree_delete (cs->cases);
7075 XDELETE (cs);
7078 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
7079 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
7080 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
7081 statement, and was not surrounded with parenthesis. */
7083 void
7084 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
7085 tree else_block, bool nested_if)
7087 tree stmt;
7089 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
7090 if (warn_parentheses && nested_if && else_block == NULL)
7092 tree inner_if = then_block;
7094 /* We know from the grammar productions that there is an IF nested
7095 within THEN_BLOCK. Due to labels and c99 conditional declarations,
7096 it might not be exactly THEN_BLOCK, but should be the last
7097 non-container statement within. */
7098 while (1)
7099 switch (TREE_CODE (inner_if))
7101 case COND_EXPR:
7102 goto found;
7103 case BIND_EXPR:
7104 inner_if = BIND_EXPR_BODY (inner_if);
7105 break;
7106 case STATEMENT_LIST:
7107 inner_if = expr_last (then_block);
7108 break;
7109 case TRY_FINALLY_EXPR:
7110 case TRY_CATCH_EXPR:
7111 inner_if = TREE_OPERAND (inner_if, 0);
7112 break;
7113 default:
7114 gcc_unreachable ();
7116 found:
7118 if (COND_EXPR_ELSE (inner_if))
7119 warning (OPT_Wparentheses,
7120 "%Hsuggest explicit braces to avoid ambiguous %<else%>",
7121 &if_locus);
7124 /* Diagnose ";" via the special empty statement node that we create. */
7125 if (extra_warnings)
7127 tree *inner_then = &then_block, *inner_else = &else_block;
7129 if (TREE_CODE (*inner_then) == STATEMENT_LIST
7130 && STATEMENT_LIST_TAIL (*inner_then))
7131 inner_then = &STATEMENT_LIST_TAIL (*inner_then)->stmt;
7132 if (*inner_else && TREE_CODE (*inner_else) == STATEMENT_LIST
7133 && STATEMENT_LIST_TAIL (*inner_else))
7134 inner_else = &STATEMENT_LIST_TAIL (*inner_else)->stmt;
7136 if (TREE_CODE (*inner_then) == NOP_EXPR && !TREE_TYPE (*inner_then))
7138 if (!*inner_else)
7139 warning (0, "%Hempty body in an if-statement",
7140 EXPR_LOCUS (*inner_then));
7142 *inner_then = alloc_stmt_list ();
7144 if (*inner_else
7145 && TREE_CODE (*inner_else) == NOP_EXPR
7146 && !TREE_TYPE (*inner_else))
7148 warning (0, "%Hempty body in an else-statement",
7149 EXPR_LOCUS (*inner_else));
7151 *inner_else = alloc_stmt_list ();
7155 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
7156 SET_EXPR_LOCATION (stmt, if_locus);
7157 add_stmt (stmt);
7160 /* Emit a general-purpose loop construct. START_LOCUS is the location of
7161 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
7162 is false for DO loops. INCR is the FOR increment expression. BODY is
7163 the statement controlled by the loop. BLAB is the break label. CLAB is
7164 the continue label. Everything is allowed to be NULL. */
7166 void
7167 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
7168 tree blab, tree clab, bool cond_is_first)
7170 tree entry = NULL, exit = NULL, t;
7172 /* If the condition is zero don't generate a loop construct. */
7173 if (cond && integer_zerop (cond))
7175 if (cond_is_first)
7177 t = build_and_jump (&blab);
7178 SET_EXPR_LOCATION (t, start_locus);
7179 add_stmt (t);
7182 else
7184 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7186 /* If we have an exit condition, then we build an IF with gotos either
7187 out of the loop, or to the top of it. If there's no exit condition,
7188 then we just build a jump back to the top. */
7189 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
7191 if (cond && !integer_nonzerop (cond))
7193 /* Canonicalize the loop condition to the end. This means
7194 generating a branch to the loop condition. Reuse the
7195 continue label, if possible. */
7196 if (cond_is_first)
7198 if (incr || !clab)
7200 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
7201 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
7203 else
7204 t = build1 (GOTO_EXPR, void_type_node, clab);
7205 SET_EXPR_LOCATION (t, start_locus);
7206 add_stmt (t);
7209 t = build_and_jump (&blab);
7210 exit = fold_build3 (COND_EXPR, void_type_node, cond, exit, t);
7211 if (cond_is_first)
7212 SET_EXPR_LOCATION (exit, start_locus);
7213 else
7214 SET_EXPR_LOCATION (exit, input_location);
7217 add_stmt (top);
7220 if (body)
7221 add_stmt (body);
7222 if (clab)
7223 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
7224 if (incr)
7225 add_stmt (incr);
7226 if (entry)
7227 add_stmt (entry);
7228 if (exit)
7229 add_stmt (exit);
7230 if (blab)
7231 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
7234 tree
7235 c_finish_bc_stmt (tree *label_p, bool is_break)
7237 bool skip;
7238 tree label = *label_p;
7240 /* In switch statements break is sometimes stylistically used after
7241 a return statement. This can lead to spurious warnings about
7242 control reaching the end of a non-void function when it is
7243 inlined. Note that we are calling block_may_fallthru with
7244 language specific tree nodes; this works because
7245 block_may_fallthru returns true when given something it does not
7246 understand. */
7247 skip = !block_may_fallthru (cur_stmt_list);
7249 if (!label)
7251 if (!skip)
7252 *label_p = label = create_artificial_label ();
7254 else if (TREE_CODE (label) != LABEL_DECL)
7256 if (is_break)
7257 error ("break statement not within loop or switch");
7258 else
7259 error ("continue statement not within a loop");
7260 return NULL_TREE;
7263 if (skip)
7264 return NULL_TREE;
7266 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
7269 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
7271 static void
7272 emit_side_effect_warnings (tree expr)
7274 if (expr == error_mark_node)
7276 else if (!TREE_SIDE_EFFECTS (expr))
7278 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
7279 warning (0, "%Hstatement with no effect",
7280 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
7282 else if (warn_unused_value)
7283 warn_if_unused_value (expr, input_location);
7286 /* Process an expression as if it were a complete statement. Emit
7287 diagnostics, but do not call ADD_STMT. */
7289 tree
7290 c_process_expr_stmt (tree expr)
7292 if (!expr)
7293 return NULL_TREE;
7295 if (warn_sequence_point)
7296 verify_sequence_points (expr);
7298 if (TREE_TYPE (expr) != error_mark_node
7299 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7300 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7301 error ("expression statement has incomplete type");
7303 /* If we're not processing a statement expression, warn about unused values.
7304 Warnings for statement expressions will be emitted later, once we figure
7305 out which is the result. */
7306 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7307 && (extra_warnings || warn_unused_value))
7308 emit_side_effect_warnings (expr);
7310 /* If the expression is not of a type to which we cannot assign a line
7311 number, wrap the thing in a no-op NOP_EXPR. */
7312 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
7313 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7315 if (EXPR_P (expr))
7316 SET_EXPR_LOCATION (expr, input_location);
7318 return expr;
7321 /* Emit an expression as a statement. */
7323 tree
7324 c_finish_expr_stmt (tree expr)
7326 if (expr)
7327 return add_stmt (c_process_expr_stmt (expr));
7328 else
7329 return NULL;
7332 /* Do the opposite and emit a statement as an expression. To begin,
7333 create a new binding level and return it. */
7335 tree
7336 c_begin_stmt_expr (void)
7338 tree ret;
7339 struct c_label_context_se *nstack;
7340 struct c_label_list *glist;
7342 /* We must force a BLOCK for this level so that, if it is not expanded
7343 later, there is a way to turn off the entire subtree of blocks that
7344 are contained in it. */
7345 keep_next_level ();
7346 ret = c_begin_compound_stmt (true);
7347 if (c_switch_stack)
7349 c_switch_stack->blocked_stmt_expr++;
7350 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7352 for (glist = label_context_stack_se->labels_used;
7353 glist != NULL;
7354 glist = glist->next)
7356 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7358 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
7359 nstack->labels_def = NULL;
7360 nstack->labels_used = NULL;
7361 nstack->next = label_context_stack_se;
7362 label_context_stack_se = nstack;
7364 /* Mark the current statement list as belonging to a statement list. */
7365 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7367 return ret;
7370 tree
7371 c_finish_stmt_expr (tree body)
7373 tree last, type, tmp, val;
7374 tree *last_p;
7375 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7377 body = c_end_compound_stmt (body, true);
7378 if (c_switch_stack)
7380 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7381 c_switch_stack->blocked_stmt_expr--;
7383 /* It is no longer possible to jump to labels defined within this
7384 statement expression. */
7385 for (dlist = label_context_stack_se->labels_def;
7386 dlist != NULL;
7387 dlist = dlist->next)
7389 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7391 /* It is again possible to define labels with a goto just outside
7392 this statement expression. */
7393 for (glist = label_context_stack_se->next->labels_used;
7394 glist != NULL;
7395 glist = glist->next)
7397 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7398 glist_prev = glist;
7400 if (glist_prev != NULL)
7401 glist_prev->next = label_context_stack_se->labels_used;
7402 else
7403 label_context_stack_se->next->labels_used
7404 = label_context_stack_se->labels_used;
7405 label_context_stack_se = label_context_stack_se->next;
7407 /* Locate the last statement in BODY. See c_end_compound_stmt
7408 about always returning a BIND_EXPR. */
7409 last_p = &BIND_EXPR_BODY (body);
7410 last = BIND_EXPR_BODY (body);
7412 continue_searching:
7413 if (TREE_CODE (last) == STATEMENT_LIST)
7415 tree_stmt_iterator i;
7417 /* This can happen with degenerate cases like ({ }). No value. */
7418 if (!TREE_SIDE_EFFECTS (last))
7419 return body;
7421 /* If we're supposed to generate side effects warnings, process
7422 all of the statements except the last. */
7423 if (extra_warnings || warn_unused_value)
7425 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7426 emit_side_effect_warnings (tsi_stmt (i));
7428 else
7429 i = tsi_last (last);
7430 last_p = tsi_stmt_ptr (i);
7431 last = *last_p;
7434 /* If the end of the list is exception related, then the list was split
7435 by a call to push_cleanup. Continue searching. */
7436 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7437 || TREE_CODE (last) == TRY_CATCH_EXPR)
7439 last_p = &TREE_OPERAND (last, 0);
7440 last = *last_p;
7441 goto continue_searching;
7444 /* In the case that the BIND_EXPR is not necessary, return the
7445 expression out from inside it. */
7446 if (last == error_mark_node
7447 || (last == BIND_EXPR_BODY (body)
7448 && BIND_EXPR_VARS (body) == NULL))
7450 /* Do not warn if the return value of a statement expression is
7451 unused. */
7452 if (EXPR_P (last))
7453 TREE_NO_WARNING (last) = 1;
7454 return last;
7457 /* Extract the type of said expression. */
7458 type = TREE_TYPE (last);
7460 /* If we're not returning a value at all, then the BIND_EXPR that
7461 we already have is a fine expression to return. */
7462 if (!type || VOID_TYPE_P (type))
7463 return body;
7465 /* Now that we've located the expression containing the value, it seems
7466 silly to make voidify_wrapper_expr repeat the process. Create a
7467 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7468 tmp = create_tmp_var_raw (type, NULL);
7470 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7471 tree_expr_nonnegative_p giving up immediately. */
7472 val = last;
7473 if (TREE_CODE (val) == NOP_EXPR
7474 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7475 val = TREE_OPERAND (val, 0);
7477 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7478 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7480 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7483 /* Begin the scope of an identifier of variably modified type, scope
7484 number SCOPE. Jumping from outside this scope to inside it is not
7485 permitted. */
7487 void
7488 c_begin_vm_scope (unsigned int scope)
7490 struct c_label_context_vm *nstack;
7491 struct c_label_list *glist;
7493 gcc_assert (scope > 0);
7494 if (c_switch_stack && !c_switch_stack->blocked_vm)
7495 c_switch_stack->blocked_vm = scope;
7496 for (glist = label_context_stack_vm->labels_used;
7497 glist != NULL;
7498 glist = glist->next)
7500 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
7502 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
7503 nstack->labels_def = NULL;
7504 nstack->labels_used = NULL;
7505 nstack->scope = scope;
7506 nstack->next = label_context_stack_vm;
7507 label_context_stack_vm = nstack;
7510 /* End a scope which may contain identifiers of variably modified
7511 type, scope number SCOPE. */
7513 void
7514 c_end_vm_scope (unsigned int scope)
7516 if (label_context_stack_vm == NULL)
7517 return;
7518 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
7519 c_switch_stack->blocked_vm = 0;
7520 /* We may have a number of nested scopes of identifiers with
7521 variably modified type, all at this depth. Pop each in turn. */
7522 while (label_context_stack_vm->scope == scope)
7524 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7526 /* It is no longer possible to jump to labels defined within this
7527 scope. */
7528 for (dlist = label_context_stack_vm->labels_def;
7529 dlist != NULL;
7530 dlist = dlist->next)
7532 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
7534 /* It is again possible to define labels with a goto just outside
7535 this scope. */
7536 for (glist = label_context_stack_vm->next->labels_used;
7537 glist != NULL;
7538 glist = glist->next)
7540 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
7541 glist_prev = glist;
7543 if (glist_prev != NULL)
7544 glist_prev->next = label_context_stack_vm->labels_used;
7545 else
7546 label_context_stack_vm->next->labels_used
7547 = label_context_stack_vm->labels_used;
7548 label_context_stack_vm = label_context_stack_vm->next;
7552 /* Begin and end compound statements. This is as simple as pushing
7553 and popping new statement lists from the tree. */
7555 tree
7556 c_begin_compound_stmt (bool do_scope)
7558 tree stmt = push_stmt_list ();
7559 if (do_scope)
7560 push_scope ();
7561 return stmt;
7564 tree
7565 c_end_compound_stmt (tree stmt, bool do_scope)
7567 tree block = NULL;
7569 if (do_scope)
7571 if (c_dialect_objc ())
7572 objc_clear_super_receiver ();
7573 block = pop_scope ();
7576 stmt = pop_stmt_list (stmt);
7577 stmt = c_build_bind_expr (block, stmt);
7579 /* If this compound statement is nested immediately inside a statement
7580 expression, then force a BIND_EXPR to be created. Otherwise we'll
7581 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7582 STATEMENT_LISTs merge, and thus we can lose track of what statement
7583 was really last. */
7584 if (cur_stmt_list
7585 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7586 && TREE_CODE (stmt) != BIND_EXPR)
7588 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7589 TREE_SIDE_EFFECTS (stmt) = 1;
7592 return stmt;
7595 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7596 when the current scope is exited. EH_ONLY is true when this is not
7597 meant to apply to normal control flow transfer. */
7599 void
7600 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7602 enum tree_code code;
7603 tree stmt, list;
7604 bool stmt_expr;
7606 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7607 stmt = build_stmt (code, NULL, cleanup);
7608 add_stmt (stmt);
7609 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7610 list = push_stmt_list ();
7611 TREE_OPERAND (stmt, 0) = list;
7612 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7615 /* Build a binary-operation expression without default conversions.
7616 CODE is the kind of expression to build.
7617 This function differs from `build' in several ways:
7618 the data type of the result is computed and recorded in it,
7619 warnings are generated if arg data types are invalid,
7620 special handling for addition and subtraction of pointers is known,
7621 and some optimization is done (operations on narrow ints
7622 are done in the narrower type when that gives the same result).
7623 Constant folding is also done before the result is returned.
7625 Note that the operands will never have enumeral types, or function
7626 or array types, because either they will have the default conversions
7627 performed or they have both just been converted to some other type in which
7628 the arithmetic is to be done. */
7630 tree
7631 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7632 int convert_p)
7634 tree type0, type1;
7635 enum tree_code code0, code1;
7636 tree op0, op1;
7637 const char *invalid_op_diag;
7639 /* Expression code to give to the expression when it is built.
7640 Normally this is CODE, which is what the caller asked for,
7641 but in some special cases we change it. */
7642 enum tree_code resultcode = code;
7644 /* Data type in which the computation is to be performed.
7645 In the simplest cases this is the common type of the arguments. */
7646 tree result_type = NULL;
7648 /* Nonzero means operands have already been type-converted
7649 in whatever way is necessary.
7650 Zero means they need to be converted to RESULT_TYPE. */
7651 int converted = 0;
7653 /* Nonzero means create the expression with this type, rather than
7654 RESULT_TYPE. */
7655 tree build_type = 0;
7657 /* Nonzero means after finally constructing the expression
7658 convert it to this type. */
7659 tree final_type = 0;
7661 /* Nonzero if this is an operation like MIN or MAX which can
7662 safely be computed in short if both args are promoted shorts.
7663 Also implies COMMON.
7664 -1 indicates a bitwise operation; this makes a difference
7665 in the exact conditions for when it is safe to do the operation
7666 in a narrower mode. */
7667 int shorten = 0;
7669 /* Nonzero if this is a comparison operation;
7670 if both args are promoted shorts, compare the original shorts.
7671 Also implies COMMON. */
7672 int short_compare = 0;
7674 /* Nonzero if this is a right-shift operation, which can be computed on the
7675 original short and then promoted if the operand is a promoted short. */
7676 int short_shift = 0;
7678 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7679 int common = 0;
7681 /* True means types are compatible as far as ObjC is concerned. */
7682 bool objc_ok;
7684 if (convert_p)
7686 op0 = default_conversion (orig_op0);
7687 op1 = default_conversion (orig_op1);
7689 else
7691 op0 = orig_op0;
7692 op1 = orig_op1;
7695 type0 = TREE_TYPE (op0);
7696 type1 = TREE_TYPE (op1);
7698 /* The expression codes of the data types of the arguments tell us
7699 whether the arguments are integers, floating, pointers, etc. */
7700 code0 = TREE_CODE (type0);
7701 code1 = TREE_CODE (type1);
7703 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7704 STRIP_TYPE_NOPS (op0);
7705 STRIP_TYPE_NOPS (op1);
7707 /* If an error was already reported for one of the arguments,
7708 avoid reporting another error. */
7710 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7711 return error_mark_node;
7713 if ((invalid_op_diag
7714 = targetm.invalid_binary_op (code, type0, type1)))
7716 error (invalid_op_diag);
7717 return error_mark_node;
7720 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
7722 switch (code)
7724 case PLUS_EXPR:
7725 /* Handle the pointer + int case. */
7726 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7727 return pointer_int_sum (PLUS_EXPR, op0, op1);
7728 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7729 return pointer_int_sum (PLUS_EXPR, op1, op0);
7730 else
7731 common = 1;
7732 break;
7734 case MINUS_EXPR:
7735 /* Subtraction of two similar pointers.
7736 We must subtract them as integers, then divide by object size. */
7737 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7738 && comp_target_types (type0, type1))
7739 return pointer_diff (op0, op1);
7740 /* Handle pointer minus int. Just like pointer plus int. */
7741 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7742 return pointer_int_sum (MINUS_EXPR, op0, op1);
7743 else
7744 common = 1;
7745 break;
7747 case MULT_EXPR:
7748 common = 1;
7749 break;
7751 case TRUNC_DIV_EXPR:
7752 case CEIL_DIV_EXPR:
7753 case FLOOR_DIV_EXPR:
7754 case ROUND_DIV_EXPR:
7755 case EXACT_DIV_EXPR:
7756 /* Floating point division by zero is a legitimate way to obtain
7757 infinities and NaNs. */
7758 if (skip_evaluation == 0 && integer_zerop (op1))
7759 warning (OPT_Wdiv_by_zero, "division by zero");
7761 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7762 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7763 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7764 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7766 enum tree_code tcode0 = code0, tcode1 = code1;
7768 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7769 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7770 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7771 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7773 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
7774 resultcode = RDIV_EXPR;
7775 else
7776 /* Although it would be tempting to shorten always here, that
7777 loses on some targets, since the modulo instruction is
7778 undefined if the quotient can't be represented in the
7779 computation mode. We shorten only if unsigned or if
7780 dividing by something we know != -1. */
7781 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7782 || (TREE_CODE (op1) == INTEGER_CST
7783 && !integer_all_onesp (op1)));
7784 common = 1;
7786 break;
7788 case BIT_AND_EXPR:
7789 case BIT_IOR_EXPR:
7790 case BIT_XOR_EXPR:
7791 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7792 shorten = -1;
7793 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7794 common = 1;
7795 break;
7797 case TRUNC_MOD_EXPR:
7798 case FLOOR_MOD_EXPR:
7799 if (skip_evaluation == 0 && integer_zerop (op1))
7800 warning (OPT_Wdiv_by_zero, "division by zero");
7802 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7804 /* Although it would be tempting to shorten always here, that loses
7805 on some targets, since the modulo instruction is undefined if the
7806 quotient can't be represented in the computation mode. We shorten
7807 only if unsigned or if dividing by something we know != -1. */
7808 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7809 || (TREE_CODE (op1) == INTEGER_CST
7810 && !integer_all_onesp (op1)));
7811 common = 1;
7813 break;
7815 case TRUTH_ANDIF_EXPR:
7816 case TRUTH_ORIF_EXPR:
7817 case TRUTH_AND_EXPR:
7818 case TRUTH_OR_EXPR:
7819 case TRUTH_XOR_EXPR:
7820 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7821 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7822 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7823 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7825 /* Result of these operations is always an int,
7826 but that does not mean the operands should be
7827 converted to ints! */
7828 result_type = integer_type_node;
7829 op0 = c_common_truthvalue_conversion (op0);
7830 op1 = c_common_truthvalue_conversion (op1);
7831 converted = 1;
7833 break;
7835 /* Shift operations: result has same type as first operand;
7836 always convert second operand to int.
7837 Also set SHORT_SHIFT if shifting rightward. */
7839 case RSHIFT_EXPR:
7840 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7842 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7844 if (tree_int_cst_sgn (op1) < 0)
7845 warning (0, "right shift count is negative");
7846 else
7848 if (!integer_zerop (op1))
7849 short_shift = 1;
7851 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7852 warning (0, "right shift count >= width of type");
7856 /* Use the type of the value to be shifted. */
7857 result_type = type0;
7858 /* Convert the shift-count to an integer, regardless of size
7859 of value being shifted. */
7860 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7861 op1 = convert (integer_type_node, op1);
7862 /* Avoid converting op1 to result_type later. */
7863 converted = 1;
7865 break;
7867 case LSHIFT_EXPR:
7868 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7870 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7872 if (tree_int_cst_sgn (op1) < 0)
7873 warning (0, "left shift count is negative");
7875 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7876 warning (0, "left shift count >= width of type");
7879 /* Use the type of the value to be shifted. */
7880 result_type = type0;
7881 /* Convert the shift-count to an integer, regardless of size
7882 of value being shifted. */
7883 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7884 op1 = convert (integer_type_node, op1);
7885 /* Avoid converting op1 to result_type later. */
7886 converted = 1;
7888 break;
7890 case EQ_EXPR:
7891 case NE_EXPR:
7892 if (code0 == REAL_TYPE || code1 == REAL_TYPE)
7893 warning (OPT_Wfloat_equal,
7894 "comparing floating point with == or != is unsafe");
7895 /* Result of comparison is always int,
7896 but don't convert the args to int! */
7897 build_type = integer_type_node;
7898 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7899 || code0 == COMPLEX_TYPE)
7900 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7901 || code1 == COMPLEX_TYPE))
7902 short_compare = 1;
7903 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7905 tree tt0 = TREE_TYPE (type0);
7906 tree tt1 = TREE_TYPE (type1);
7907 /* Anything compares with void *. void * compares with anything.
7908 Otherwise, the targets must be compatible
7909 and both must be object or both incomplete. */
7910 if (comp_target_types (type0, type1))
7911 result_type = common_pointer_type (type0, type1);
7912 else if (VOID_TYPE_P (tt0))
7914 /* op0 != orig_op0 detects the case of something
7915 whose value is 0 but which isn't a valid null ptr const. */
7916 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7917 && TREE_CODE (tt1) == FUNCTION_TYPE)
7918 pedwarn ("ISO C forbids comparison of %<void *%>"
7919 " with function pointer");
7921 else if (VOID_TYPE_P (tt1))
7923 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7924 && TREE_CODE (tt0) == FUNCTION_TYPE)
7925 pedwarn ("ISO C forbids comparison of %<void *%>"
7926 " with function pointer");
7928 else
7929 /* Avoid warning about the volatile ObjC EH puts on decls. */
7930 if (!objc_ok)
7931 pedwarn ("comparison of distinct pointer types lacks a cast");
7933 if (result_type == NULL_TREE)
7934 result_type = ptr_type_node;
7936 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7937 && integer_zerop (op1))
7938 result_type = type0;
7939 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7940 && integer_zerop (op0))
7941 result_type = type1;
7942 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7944 result_type = type0;
7945 pedwarn ("comparison between pointer and integer");
7947 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7949 result_type = type1;
7950 pedwarn ("comparison between pointer and integer");
7952 break;
7954 case LE_EXPR:
7955 case GE_EXPR:
7956 case LT_EXPR:
7957 case GT_EXPR:
7958 build_type = integer_type_node;
7959 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7960 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7961 short_compare = 1;
7962 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7964 if (comp_target_types (type0, type1))
7966 result_type = common_pointer_type (type0, type1);
7967 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7968 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7969 pedwarn ("comparison of complete and incomplete pointers");
7970 else if (pedantic
7971 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7972 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7974 else
7976 result_type = ptr_type_node;
7977 pedwarn ("comparison of distinct pointer types lacks a cast");
7980 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7981 && integer_zerop (op1))
7983 result_type = type0;
7984 if (pedantic || extra_warnings)
7985 pedwarn ("ordered comparison of pointer with integer zero");
7987 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7988 && integer_zerop (op0))
7990 result_type = type1;
7991 if (pedantic)
7992 pedwarn ("ordered comparison of pointer with integer zero");
7994 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7996 result_type = type0;
7997 pedwarn ("comparison between pointer and integer");
7999 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8001 result_type = type1;
8002 pedwarn ("comparison between pointer and integer");
8004 break;
8006 default:
8007 gcc_unreachable ();
8010 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
8011 return error_mark_node;
8013 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
8014 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
8015 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
8016 TREE_TYPE (type1))))
8018 binary_op_error (code);
8019 return error_mark_node;
8022 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
8023 || code0 == VECTOR_TYPE)
8025 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
8026 || code1 == VECTOR_TYPE))
8028 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
8030 if (shorten || common || short_compare)
8031 result_type = c_common_type (type0, type1);
8033 /* For certain operations (which identify themselves by shorten != 0)
8034 if both args were extended from the same smaller type,
8035 do the arithmetic in that type and then extend.
8037 shorten !=0 and !=1 indicates a bitwise operation.
8038 For them, this optimization is safe only if
8039 both args are zero-extended or both are sign-extended.
8040 Otherwise, we might change the result.
8041 Eg, (short)-1 | (unsigned short)-1 is (int)-1
8042 but calculated in (unsigned short) it would be (unsigned short)-1. */
8044 if (shorten && none_complex)
8046 int unsigned0, unsigned1;
8047 tree arg0 = get_narrower (op0, &unsigned0);
8048 tree arg1 = get_narrower (op1, &unsigned1);
8049 /* UNS is 1 if the operation to be done is an unsigned one. */
8050 int uns = TYPE_UNSIGNED (result_type);
8051 tree type;
8053 final_type = result_type;
8055 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
8056 but it *requires* conversion to FINAL_TYPE. */
8058 if ((TYPE_PRECISION (TREE_TYPE (op0))
8059 == TYPE_PRECISION (TREE_TYPE (arg0)))
8060 && TREE_TYPE (op0) != final_type)
8061 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
8062 if ((TYPE_PRECISION (TREE_TYPE (op1))
8063 == TYPE_PRECISION (TREE_TYPE (arg1)))
8064 && TREE_TYPE (op1) != final_type)
8065 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
8067 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
8069 /* For bitwise operations, signedness of nominal type
8070 does not matter. Consider only how operands were extended. */
8071 if (shorten == -1)
8072 uns = unsigned0;
8074 /* Note that in all three cases below we refrain from optimizing
8075 an unsigned operation on sign-extended args.
8076 That would not be valid. */
8078 /* Both args variable: if both extended in same way
8079 from same width, do it in that width.
8080 Do it unsigned if args were zero-extended. */
8081 if ((TYPE_PRECISION (TREE_TYPE (arg0))
8082 < TYPE_PRECISION (result_type))
8083 && (TYPE_PRECISION (TREE_TYPE (arg1))
8084 == TYPE_PRECISION (TREE_TYPE (arg0)))
8085 && unsigned0 == unsigned1
8086 && (unsigned0 || !uns))
8087 result_type
8088 = c_common_signed_or_unsigned_type
8089 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
8090 else if (TREE_CODE (arg0) == INTEGER_CST
8091 && (unsigned1 || !uns)
8092 && (TYPE_PRECISION (TREE_TYPE (arg1))
8093 < TYPE_PRECISION (result_type))
8094 && (type
8095 = c_common_signed_or_unsigned_type (unsigned1,
8096 TREE_TYPE (arg1)),
8097 int_fits_type_p (arg0, type)))
8098 result_type = type;
8099 else if (TREE_CODE (arg1) == INTEGER_CST
8100 && (unsigned0 || !uns)
8101 && (TYPE_PRECISION (TREE_TYPE (arg0))
8102 < TYPE_PRECISION (result_type))
8103 && (type
8104 = c_common_signed_or_unsigned_type (unsigned0,
8105 TREE_TYPE (arg0)),
8106 int_fits_type_p (arg1, type)))
8107 result_type = type;
8110 /* Shifts can be shortened if shifting right. */
8112 if (short_shift)
8114 int unsigned_arg;
8115 tree arg0 = get_narrower (op0, &unsigned_arg);
8117 final_type = result_type;
8119 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8120 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
8122 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
8123 /* We can shorten only if the shift count is less than the
8124 number of bits in the smaller type size. */
8125 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
8126 /* We cannot drop an unsigned shift after sign-extension. */
8127 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
8129 /* Do an unsigned shift if the operand was zero-extended. */
8130 result_type
8131 = c_common_signed_or_unsigned_type (unsigned_arg,
8132 TREE_TYPE (arg0));
8133 /* Convert value-to-be-shifted to that type. */
8134 if (TREE_TYPE (op0) != result_type)
8135 op0 = convert (result_type, op0);
8136 converted = 1;
8140 /* Comparison operations are shortened too but differently.
8141 They identify themselves by setting short_compare = 1. */
8143 if (short_compare)
8145 /* Don't write &op0, etc., because that would prevent op0
8146 from being kept in a register.
8147 Instead, make copies of the our local variables and
8148 pass the copies by reference, then copy them back afterward. */
8149 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
8150 enum tree_code xresultcode = resultcode;
8151 tree val
8152 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8154 if (val != 0)
8155 return val;
8157 op0 = xop0, op1 = xop1;
8158 converted = 1;
8159 resultcode = xresultcode;
8161 if (warn_sign_compare && skip_evaluation == 0)
8163 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
8164 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
8165 int unsignedp0, unsignedp1;
8166 tree primop0 = get_narrower (op0, &unsignedp0);
8167 tree primop1 = get_narrower (op1, &unsignedp1);
8169 xop0 = orig_op0;
8170 xop1 = orig_op1;
8171 STRIP_TYPE_NOPS (xop0);
8172 STRIP_TYPE_NOPS (xop1);
8174 /* Give warnings for comparisons between signed and unsigned
8175 quantities that may fail.
8177 Do the checking based on the original operand trees, so that
8178 casts will be considered, but default promotions won't be.
8180 Do not warn if the comparison is being done in a signed type,
8181 since the signed type will only be chosen if it can represent
8182 all the values of the unsigned type. */
8183 if (!TYPE_UNSIGNED (result_type))
8184 /* OK */;
8185 /* Do not warn if both operands are the same signedness. */
8186 else if (op0_signed == op1_signed)
8187 /* OK */;
8188 else
8190 tree sop, uop;
8192 if (op0_signed)
8193 sop = xop0, uop = xop1;
8194 else
8195 sop = xop1, uop = xop0;
8197 /* Do not warn if the signed quantity is an
8198 unsuffixed integer literal (or some static
8199 constant expression involving such literals or a
8200 conditional expression involving such literals)
8201 and it is non-negative. */
8202 if (tree_expr_nonnegative_p (sop))
8203 /* OK */;
8204 /* Do not warn if the comparison is an equality operation,
8205 the unsigned quantity is an integral constant, and it
8206 would fit in the result if the result were signed. */
8207 else if (TREE_CODE (uop) == INTEGER_CST
8208 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
8209 && int_fits_type_p
8210 (uop, c_common_signed_type (result_type)))
8211 /* OK */;
8212 /* Do not warn if the unsigned quantity is an enumeration
8213 constant and its maximum value would fit in the result
8214 if the result were signed. */
8215 else if (TREE_CODE (uop) == INTEGER_CST
8216 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
8217 && int_fits_type_p
8218 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
8219 c_common_signed_type (result_type)))
8220 /* OK */;
8221 else
8222 warning (0, "comparison between signed and unsigned");
8225 /* Warn if two unsigned values are being compared in a size
8226 larger than their original size, and one (and only one) is the
8227 result of a `~' operator. This comparison will always fail.
8229 Also warn if one operand is a constant, and the constant
8230 does not have all bits set that are set in the ~ operand
8231 when it is extended. */
8233 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
8234 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
8236 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
8237 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
8238 &unsignedp0);
8239 else
8240 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
8241 &unsignedp1);
8243 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
8245 tree primop;
8246 HOST_WIDE_INT constant, mask;
8247 int unsignedp, bits;
8249 if (host_integerp (primop0, 0))
8251 primop = primop1;
8252 unsignedp = unsignedp1;
8253 constant = tree_low_cst (primop0, 0);
8255 else
8257 primop = primop0;
8258 unsignedp = unsignedp0;
8259 constant = tree_low_cst (primop1, 0);
8262 bits = TYPE_PRECISION (TREE_TYPE (primop));
8263 if (bits < TYPE_PRECISION (result_type)
8264 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
8266 mask = (~(HOST_WIDE_INT) 0) << bits;
8267 if ((mask & constant) != mask)
8268 warning (0, "comparison of promoted ~unsigned with constant");
8271 else if (unsignedp0 && unsignedp1
8272 && (TYPE_PRECISION (TREE_TYPE (primop0))
8273 < TYPE_PRECISION (result_type))
8274 && (TYPE_PRECISION (TREE_TYPE (primop1))
8275 < TYPE_PRECISION (result_type)))
8276 warning (0, "comparison of promoted ~unsigned with unsigned");
8282 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
8283 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
8284 Then the expression will be built.
8285 It will be given type FINAL_TYPE if that is nonzero;
8286 otherwise, it will be given type RESULT_TYPE. */
8288 if (!result_type)
8290 binary_op_error (code);
8291 return error_mark_node;
8294 if (!converted)
8296 if (TREE_TYPE (op0) != result_type)
8297 op0 = convert (result_type, op0);
8298 if (TREE_TYPE (op1) != result_type)
8299 op1 = convert (result_type, op1);
8301 /* This can happen if one operand has a vector type, and the other
8302 has a different type. */
8303 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
8304 return error_mark_node;
8307 if (build_type == NULL_TREE)
8308 build_type = result_type;
8311 /* Treat expressions in initializers specially as they can't trap. */
8312 tree result = require_constant_value ? fold_build2_initializer (resultcode,
8313 build_type,
8314 op0, op1)
8315 : fold_build2 (resultcode, build_type,
8316 op0, op1);
8318 if (final_type != 0)
8319 result = convert (final_type, result);
8320 return result;
8325 /* Convert EXPR to be a truth-value, validating its type for this
8326 purpose. */
8328 tree
8329 c_objc_common_truthvalue_conversion (tree expr)
8331 switch (TREE_CODE (TREE_TYPE (expr)))
8333 case ARRAY_TYPE:
8334 error ("used array that cannot be converted to pointer where scalar is required");
8335 return error_mark_node;
8337 case RECORD_TYPE:
8338 error ("used struct type value where scalar is required");
8339 return error_mark_node;
8341 case UNION_TYPE:
8342 error ("used union type value where scalar is required");
8343 return error_mark_node;
8345 case FUNCTION_TYPE:
8346 gcc_unreachable ();
8348 default:
8349 break;
8352 /* ??? Should we also give an error for void and vectors rather than
8353 leaving those to give errors later? */
8354 return c_common_truthvalue_conversion (expr);
8358 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
8359 required. */
8361 tree
8362 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED,
8363 bool *ti ATTRIBUTE_UNUSED, bool *se)
8365 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
8367 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
8368 /* Executing a compound literal inside a function reinitializes
8369 it. */
8370 if (!TREE_STATIC (decl))
8371 *se = true;
8372 return decl;
8374 else
8375 return expr;