Mark ChangeLog
[official-gcc.git] / gcc / c-typeck.c
blob09aa0de8f78d8c52be1cbec1e0b167ea5de43c03
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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, 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, int);
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 default_function_array_conversion (tree);
84 static tree lookup_field (tree, tree);
85 static tree convert_arguments (tree, tree, tree, tree);
86 static tree pointer_diff (tree, tree);
87 static tree convert_for_assignment (tree, tree, enum impl_conv, tree, tree,
88 int);
89 static tree valid_compound_expr_initializer (tree, tree);
90 static void push_string (const char *);
91 static void push_member_name (tree);
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 void record_maybe_used_decl (tree);
107 /* Do `exp = require_complete_type (exp);' to make sure exp
108 does not have an incomplete type. (That includes void types.) */
110 tree
111 require_complete_type (tree value)
113 tree type = TREE_TYPE (value);
115 if (value == error_mark_node || type == error_mark_node)
116 return error_mark_node;
118 /* First, detect a valid value with a complete type. */
119 if (COMPLETE_TYPE_P (type))
120 return value;
122 c_incomplete_type_error (value, type);
123 return error_mark_node;
126 /* Print an error message for invalid use of an incomplete type.
127 VALUE is the expression that was used (or 0 if that isn't known)
128 and TYPE is the type that was invalid. */
130 void
131 c_incomplete_type_error (tree value, tree type)
133 const char *type_code_string;
135 /* Avoid duplicate error message. */
136 if (TREE_CODE (type) == ERROR_MARK)
137 return;
139 if (value != 0 && (TREE_CODE (value) == VAR_DECL
140 || TREE_CODE (value) == PARM_DECL))
141 error ("%qs has an incomplete type",
142 IDENTIFIER_POINTER (DECL_NAME (value)));
143 else
145 retry:
146 /* We must print an error message. Be clever about what it says. */
148 switch (TREE_CODE (type))
150 case RECORD_TYPE:
151 type_code_string = "struct";
152 break;
154 case UNION_TYPE:
155 type_code_string = "union";
156 break;
158 case ENUMERAL_TYPE:
159 type_code_string = "enum";
160 break;
162 case VOID_TYPE:
163 error ("invalid use of void expression");
164 return;
166 case ARRAY_TYPE:
167 if (TYPE_DOMAIN (type))
169 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
171 error ("invalid use of flexible array member");
172 return;
174 type = TREE_TYPE (type);
175 goto retry;
177 error ("invalid use of array with unspecified bounds");
178 return;
180 default:
181 gcc_unreachable ();
184 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
185 error ("invalid use of undefined type %<%s %s%>",
186 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
187 else
188 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
189 error ("invalid use of incomplete typedef %qs",
190 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
194 /* Given a type, apply default promotions wrt unnamed function
195 arguments and return the new type. */
197 tree
198 c_type_promotes_to (tree type)
200 if (TYPE_MAIN_VARIANT (type) == float_type_node)
201 return double_type_node;
203 if (c_promoting_integer_type_p (type))
205 /* Preserve unsignedness if not really getting any wider. */
206 if (TYPE_UNSIGNED (type)
207 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
208 return unsigned_type_node;
209 return integer_type_node;
212 return type;
215 /* Return a variant of TYPE which has all the type qualifiers of LIKE
216 as well as those of TYPE. */
218 static tree
219 qualify_type (tree type, tree like)
221 return c_build_qualified_type (type,
222 TYPE_QUALS (type) | TYPE_QUALS (like));
225 /* Return the composite type of two compatible types.
227 We assume that comptypes has already been done and returned
228 nonzero; if that isn't so, this may crash. In particular, we
229 assume that qualifiers match. */
231 tree
232 composite_type (tree t1, tree t2)
234 enum tree_code code1;
235 enum tree_code code2;
236 tree attributes;
238 /* Save time if the two types are the same. */
240 if (t1 == t2) return t1;
242 /* If one type is nonsense, use the other. */
243 if (t1 == error_mark_node)
244 return t2;
245 if (t2 == error_mark_node)
246 return t1;
248 code1 = TREE_CODE (t1);
249 code2 = TREE_CODE (t2);
251 /* Merge the attributes. */
252 attributes = targetm.merge_type_attributes (t1, t2);
254 /* If one is an enumerated type and the other is the compatible
255 integer type, the composite type might be either of the two
256 (DR#013 question 3). For consistency, use the enumerated type as
257 the composite type. */
259 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
260 return t1;
261 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
262 return t2;
264 gcc_assert (code1 == code2);
266 switch (code1)
268 case POINTER_TYPE:
269 /* For two pointers, do this recursively on the target type. */
271 tree pointed_to_1 = TREE_TYPE (t1);
272 tree pointed_to_2 = TREE_TYPE (t2);
273 tree target = composite_type (pointed_to_1, pointed_to_2);
274 t1 = build_pointer_type (target);
275 t1 = build_type_attribute_variant (t1, attributes);
276 return qualify_type (t1, t2);
279 case ARRAY_TYPE:
281 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
282 int quals;
283 tree unqual_elt;
285 /* We should not have any type quals on arrays at all. */
286 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
288 /* Save space: see if the result is identical to one of the args. */
289 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
290 return build_type_attribute_variant (t1, attributes);
291 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
292 return build_type_attribute_variant (t2, attributes);
294 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
295 return build_type_attribute_variant (t1, attributes);
296 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
297 return build_type_attribute_variant (t2, attributes);
299 /* Merge the element types, and have a size if either arg has
300 one. We may have qualifiers on the element types. To set
301 up TYPE_MAIN_VARIANT correctly, we need to form the
302 composite of the unqualified types and add the qualifiers
303 back at the end. */
304 quals = TYPE_QUALS (strip_array_types (elt));
305 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
306 t1 = build_array_type (unqual_elt,
307 TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
308 t1 = c_build_qualified_type (t1, quals);
309 return build_type_attribute_variant (t1, attributes);
312 case FUNCTION_TYPE:
313 /* Function types: prefer the one that specified arg types.
314 If both do, merge the arg types. Also merge the return types. */
316 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
317 tree p1 = TYPE_ARG_TYPES (t1);
318 tree p2 = TYPE_ARG_TYPES (t2);
319 int len;
320 tree newargs, n;
321 int i;
323 /* Save space: see if the result is identical to one of the args. */
324 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
325 return build_type_attribute_variant (t1, attributes);
326 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
327 return build_type_attribute_variant (t2, attributes);
329 /* Simple way if one arg fails to specify argument types. */
330 if (TYPE_ARG_TYPES (t1) == 0)
332 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
333 t1 = build_type_attribute_variant (t1, attributes);
334 return qualify_type (t1, t2);
336 if (TYPE_ARG_TYPES (t2) == 0)
338 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
339 t1 = build_type_attribute_variant (t1, attributes);
340 return qualify_type (t1, t2);
343 /* If both args specify argument types, we must merge the two
344 lists, argument by argument. */
345 /* Tell global_bindings_p to return false so that variable_size
346 doesn't abort on VLAs in parameter types. */
347 c_override_global_bindings_to_false = true;
349 len = list_length (p1);
350 newargs = 0;
352 for (i = 0; i < len; i++)
353 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
355 n = newargs;
357 for (; p1;
358 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
360 /* A null type means arg type is not specified.
361 Take whatever the other function type has. */
362 if (TREE_VALUE (p1) == 0)
364 TREE_VALUE (n) = TREE_VALUE (p2);
365 goto parm_done;
367 if (TREE_VALUE (p2) == 0)
369 TREE_VALUE (n) = TREE_VALUE (p1);
370 goto parm_done;
373 /* Given wait (union {union wait *u; int *i} *)
374 and wait (union wait *),
375 prefer union wait * as type of parm. */
376 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
377 && TREE_VALUE (p1) != TREE_VALUE (p2))
379 tree memb;
380 tree mv2 = TREE_VALUE (p2);
381 if (mv2 && mv2 != error_mark_node
382 && TREE_CODE (mv2) != ARRAY_TYPE)
383 mv2 = TYPE_MAIN_VARIANT (mv2);
384 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
385 memb; memb = TREE_CHAIN (memb))
387 tree mv3 = TREE_TYPE (memb);
388 if (mv3 && mv3 != error_mark_node
389 && TREE_CODE (mv3) != ARRAY_TYPE)
390 mv3 = TYPE_MAIN_VARIANT (mv3);
391 if (comptypes (mv3, mv2))
393 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
394 TREE_VALUE (p2));
395 if (pedantic)
396 pedwarn ("function types not truly compatible in ISO C");
397 goto parm_done;
401 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
402 && TREE_VALUE (p2) != TREE_VALUE (p1))
404 tree memb;
405 tree mv1 = TREE_VALUE (p1);
406 if (mv1 && mv1 != error_mark_node
407 && TREE_CODE (mv1) != ARRAY_TYPE)
408 mv1 = TYPE_MAIN_VARIANT (mv1);
409 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
410 memb; memb = TREE_CHAIN (memb))
412 tree mv3 = TREE_TYPE (memb);
413 if (mv3 && mv3 != error_mark_node
414 && TREE_CODE (mv3) != ARRAY_TYPE)
415 mv3 = TYPE_MAIN_VARIANT (mv3);
416 if (comptypes (mv3, mv1))
418 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
419 TREE_VALUE (p1));
420 if (pedantic)
421 pedwarn ("function types not truly compatible in ISO C");
422 goto parm_done;
426 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
427 parm_done: ;
430 c_override_global_bindings_to_false = false;
431 t1 = build_function_type (valtype, newargs);
432 t1 = qualify_type (t1, t2);
433 /* ... falls through ... */
436 default:
437 return build_type_attribute_variant (t1, attributes);
442 /* Return the type of a conditional expression between pointers to
443 possibly differently qualified versions of compatible types.
445 We assume that comp_target_types has already been done and returned
446 nonzero; if that isn't so, this may crash. */
448 static tree
449 common_pointer_type (tree t1, tree t2)
451 tree attributes;
452 tree pointed_to_1, mv1;
453 tree pointed_to_2, mv2;
454 tree target;
456 /* Save time if the two types are the same. */
458 if (t1 == t2) return t1;
460 /* If one type is nonsense, use the other. */
461 if (t1 == error_mark_node)
462 return t2;
463 if (t2 == error_mark_node)
464 return t1;
466 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
467 && TREE_CODE (t2) == POINTER_TYPE);
469 /* Merge the attributes. */
470 attributes = targetm.merge_type_attributes (t1, t2);
472 /* Find the composite type of the target types, and combine the
473 qualifiers of the two types' targets. Do not lose qualifiers on
474 array element types by taking the TYPE_MAIN_VARIANT. */
475 mv1 = pointed_to_1 = TREE_TYPE (t1);
476 mv2 = pointed_to_2 = TREE_TYPE (t2);
477 if (TREE_CODE (mv1) != ARRAY_TYPE)
478 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
479 if (TREE_CODE (mv2) != ARRAY_TYPE)
480 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
481 target = composite_type (mv1, mv2);
482 t1 = build_pointer_type (c_build_qualified_type
483 (target,
484 TYPE_QUALS (pointed_to_1) |
485 TYPE_QUALS (pointed_to_2)));
486 return build_type_attribute_variant (t1, attributes);
489 /* Return the common type for two arithmetic types under the usual
490 arithmetic conversions. The default conversions have already been
491 applied, and enumerated types converted to their compatible integer
492 types. The resulting type is unqualified and has no attributes.
494 This is the type for the result of most arithmetic operations
495 if the operands have the given two types. */
497 static tree
498 c_common_type (tree t1, tree t2)
500 enum tree_code code1;
501 enum tree_code code2;
503 /* If one type is nonsense, use the other. */
504 if (t1 == error_mark_node)
505 return t2;
506 if (t2 == error_mark_node)
507 return t1;
509 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
510 t1 = TYPE_MAIN_VARIANT (t1);
512 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
513 t2 = TYPE_MAIN_VARIANT (t2);
515 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
516 t1 = build_type_attribute_variant (t1, NULL_TREE);
518 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
519 t2 = build_type_attribute_variant (t2, NULL_TREE);
521 /* Save time if the two types are the same. */
523 if (t1 == t2) return t1;
525 code1 = TREE_CODE (t1);
526 code2 = TREE_CODE (t2);
528 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
529 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
530 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
531 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
533 /* If one type is a vector type, return that type. (How the usual
534 arithmetic conversions apply to the vector types extension is not
535 precisely specified.) */
536 if (code1 == VECTOR_TYPE)
537 return t1;
539 if (code2 == VECTOR_TYPE)
540 return t2;
542 /* If one type is complex, form the common type of the non-complex
543 components, then make that complex. Use T1 or T2 if it is the
544 required type. */
545 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
547 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
548 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
549 tree subtype = c_common_type (subtype1, subtype2);
551 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
552 return t1;
553 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
554 return t2;
555 else
556 return build_complex_type (subtype);
559 /* If only one is real, use it as the result. */
561 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
562 return t1;
564 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
565 return t2;
567 /* Both real or both integers; use the one with greater precision. */
569 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
570 return t1;
571 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
572 return t2;
574 /* Same precision. Prefer long longs to longs to ints when the
575 same precision, following the C99 rules on integer type rank
576 (which are equivalent to the C90 rules for C90 types). */
578 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
579 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
580 return long_long_unsigned_type_node;
582 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
583 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
585 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
586 return long_long_unsigned_type_node;
587 else
588 return long_long_integer_type_node;
591 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
592 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
593 return long_unsigned_type_node;
595 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
596 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
598 /* But preserve unsignedness from the other type,
599 since long cannot hold all the values of an unsigned int. */
600 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
601 return long_unsigned_type_node;
602 else
603 return long_integer_type_node;
606 /* Likewise, prefer long double to double even if same size. */
607 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
608 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
609 return long_double_type_node;
611 /* Otherwise prefer the unsigned one. */
613 if (TYPE_UNSIGNED (t1))
614 return t1;
615 else
616 return t2;
619 /* Wrapper around c_common_type that is used by c-common.c and other
620 front end optimizations that remove promotions. ENUMERAL_TYPEs
621 are allowed here and are converted to their compatible integer types. */
622 tree
623 common_type (tree t1, tree t2)
625 if (TREE_CODE (t1) == ENUMERAL_TYPE)
626 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
627 if (TREE_CODE (t2) == ENUMERAL_TYPE)
628 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
629 return c_common_type (t1, t2);
632 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
633 or various other operations. Return 2 if they are compatible
634 but a warning may be needed if you use them together. */
637 comptypes (tree type1, tree type2)
639 tree t1 = type1;
640 tree t2 = type2;
641 int attrval, val;
643 /* Suppress errors caused by previously reported errors. */
645 if (t1 == t2 || !t1 || !t2
646 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
647 return 1;
649 /* If either type is the internal version of sizetype, return the
650 language version. */
651 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
652 && TYPE_ORIG_SIZE_TYPE (t1))
653 t1 = TYPE_ORIG_SIZE_TYPE (t1);
655 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
656 && TYPE_ORIG_SIZE_TYPE (t2))
657 t2 = TYPE_ORIG_SIZE_TYPE (t2);
660 /* Enumerated types are compatible with integer types, but this is
661 not transitive: two enumerated types in the same translation unit
662 are compatible with each other only if they are the same type. */
664 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
665 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
666 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
667 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
669 if (t1 == t2)
670 return 1;
672 /* Different classes of types can't be compatible. */
674 if (TREE_CODE (t1) != TREE_CODE (t2))
675 return 0;
677 /* Qualifiers must match. C99 6.7.3p9 */
679 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
680 return 0;
682 /* Allow for two different type nodes which have essentially the same
683 definition. Note that we already checked for equality of the type
684 qualifiers (just above). */
686 if (TREE_CODE (t1) != ARRAY_TYPE
687 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
688 return 1;
690 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
691 if (!(attrval = targetm.comp_type_attributes (t1, t2)))
692 return 0;
694 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
695 val = 0;
697 switch (TREE_CODE (t1))
699 case POINTER_TYPE:
700 /* We must give ObjC the first crack at comparing pointers, since
701 protocol qualifiers may be involved. */
702 if (c_dialect_objc ())
704 val = objc_comptypes (t1, t2, 0);
705 if (val >= 0)
706 break;
707 val = 0;
709 /* Do not remove mode or aliasing information. */
710 if (TYPE_MODE (t1) != TYPE_MODE (t2)
711 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
712 break;
713 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
714 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
715 break;
717 case FUNCTION_TYPE:
718 val = function_types_compatible_p (t1, t2);
719 break;
721 case ARRAY_TYPE:
723 tree d1 = TYPE_DOMAIN (t1);
724 tree d2 = TYPE_DOMAIN (t2);
725 bool d1_variable, d2_variable;
726 bool d1_zero, d2_zero;
727 val = 1;
729 /* Target types must match incl. qualifiers. */
730 if (TREE_TYPE (t1) != TREE_TYPE (t2)
731 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
732 return 0;
734 /* Sizes must match unless one is missing or variable. */
735 if (d1 == 0 || d2 == 0 || d1 == d2)
736 break;
738 d1_zero = !TYPE_MAX_VALUE (d1);
739 d2_zero = !TYPE_MAX_VALUE (d2);
741 d1_variable = (!d1_zero
742 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
743 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
744 d2_variable = (!d2_zero
745 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
746 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
748 if (d1_variable || d2_variable)
749 break;
750 if (d1_zero && d2_zero)
751 break;
752 if (d1_zero || d2_zero
753 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
754 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
755 val = 0;
757 break;
760 case RECORD_TYPE:
761 /* We are dealing with two distinct structs. In assorted Objective-C
762 corner cases, however, these can still be deemed equivalent. */
763 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
764 val = 1;
766 case ENUMERAL_TYPE:
767 case UNION_TYPE:
768 if (val != 1 && !same_translation_unit_p (t1, t2))
769 val = tagged_types_tu_compatible_p (t1, t2);
770 break;
772 case VECTOR_TYPE:
773 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
774 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
775 break;
777 default:
778 break;
780 return attrval == 2 && val == 1 ? 2 : val;
783 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
784 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
785 to 1 or 0 depending if the check of the pointer types is meant to
786 be reflexive or not (typically, assignments are not reflexive,
787 while comparisons are reflexive).
790 static int
791 comp_target_types (tree ttl, tree ttr, int reflexive)
793 int val;
794 tree mvl, mvr;
796 /* Give objc_comptypes a crack at letting these types through. */
797 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
798 return val;
800 /* Do not lose qualifiers on element types of array types that are
801 pointer targets by taking their TYPE_MAIN_VARIANT. */
802 mvl = TREE_TYPE (ttl);
803 mvr = TREE_TYPE (ttr);
804 if (TREE_CODE (mvl) != ARRAY_TYPE)
805 mvl = TYPE_MAIN_VARIANT (mvl);
806 if (TREE_CODE (mvr) != ARRAY_TYPE)
807 mvr = TYPE_MAIN_VARIANT (mvr);
808 val = comptypes (mvl, mvr);
810 if (val == 2 && pedantic)
811 pedwarn ("types are not quite compatible");
812 return val;
815 /* Subroutines of `comptypes'. */
817 /* Determine whether two trees derive from the same translation unit.
818 If the CONTEXT chain ends in a null, that tree's context is still
819 being parsed, so if two trees have context chains ending in null,
820 they're in the same translation unit. */
822 same_translation_unit_p (tree t1, tree t2)
824 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
825 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
827 case tcc_declaration:
828 t1 = DECL_CONTEXT (t1); break;
829 case tcc_type:
830 t1 = TYPE_CONTEXT (t1); break;
831 case tcc_exceptional:
832 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
833 default: gcc_unreachable ();
836 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
837 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
839 case tcc_declaration:
840 t2 = DECL_CONTEXT (t2); break;
841 case tcc_type:
842 t2 = TYPE_CONTEXT (t2); break;
843 case tcc_exceptional:
844 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
845 default: gcc_unreachable ();
848 return t1 == t2;
851 /* The C standard says that two structures in different translation
852 units are compatible with each other only if the types of their
853 fields are compatible (among other things). So, consider two copies
854 of this structure: */
856 struct tagged_tu_seen {
857 const struct tagged_tu_seen * next;
858 tree t1;
859 tree t2;
862 /* Can they be compatible with each other? We choose to break the
863 recursion by allowing those types to be compatible. */
865 static const struct tagged_tu_seen * tagged_tu_seen_base;
867 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
868 compatible. If the two types are not the same (which has been
869 checked earlier), this can only happen when multiple translation
870 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
871 rules. */
873 static int
874 tagged_types_tu_compatible_p (tree t1, tree t2)
876 tree s1, s2;
877 bool needs_warning = false;
879 /* We have to verify that the tags of the types are the same. This
880 is harder than it looks because this may be a typedef, so we have
881 to go look at the original type. It may even be a typedef of a
882 typedef...
883 In the case of compiler-created builtin structs the TYPE_DECL
884 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
885 while (TYPE_NAME (t1)
886 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
887 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
888 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
890 while (TYPE_NAME (t2)
891 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
892 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
893 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
895 /* C90 didn't have the requirement that the two tags be the same. */
896 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
897 return 0;
899 /* C90 didn't say what happened if one or both of the types were
900 incomplete; we choose to follow C99 rules here, which is that they
901 are compatible. */
902 if (TYPE_SIZE (t1) == NULL
903 || TYPE_SIZE (t2) == NULL)
904 return 1;
907 const struct tagged_tu_seen * tts_i;
908 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
909 if (tts_i->t1 == t1 && tts_i->t2 == t2)
910 return 1;
913 switch (TREE_CODE (t1))
915 case ENUMERAL_TYPE:
918 /* Speed up the case where the type values are in the same order. */
919 tree tv1 = TYPE_VALUES (t1);
920 tree tv2 = TYPE_VALUES (t2);
922 if (tv1 == tv2)
923 return 1;
925 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
927 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
928 break;
929 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
930 return 0;
933 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
934 return 1;
935 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
936 return 0;
938 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
939 return 0;
941 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
943 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
944 if (s2 == NULL
945 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
946 return 0;
948 return 1;
951 case UNION_TYPE:
953 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
954 return 0;
956 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
958 bool ok = false;
959 struct tagged_tu_seen tts;
961 tts.next = tagged_tu_seen_base;
962 tts.t1 = t1;
963 tts.t2 = t2;
964 tagged_tu_seen_base = &tts;
966 if (DECL_NAME (s1) != NULL)
967 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
968 if (DECL_NAME (s1) == DECL_NAME (s2))
970 int result;
971 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
972 if (result == 0)
973 break;
974 if (result == 2)
975 needs_warning = true;
977 if (TREE_CODE (s1) == FIELD_DECL
978 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
979 DECL_FIELD_BIT_OFFSET (s2)) != 1)
980 break;
982 ok = true;
983 break;
985 tagged_tu_seen_base = tts.next;
986 if (!ok)
987 return 0;
989 return needs_warning ? 2 : 1;
992 case RECORD_TYPE:
994 struct tagged_tu_seen tts;
996 tts.next = tagged_tu_seen_base;
997 tts.t1 = t1;
998 tts.t2 = t2;
999 tagged_tu_seen_base = &tts;
1001 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1002 s1 && s2;
1003 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1005 int result;
1006 if (TREE_CODE (s1) != TREE_CODE (s2)
1007 || DECL_NAME (s1) != DECL_NAME (s2))
1008 break;
1009 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
1010 if (result == 0)
1011 break;
1012 if (result == 2)
1013 needs_warning = true;
1015 if (TREE_CODE (s1) == FIELD_DECL
1016 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1017 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1018 break;
1020 tagged_tu_seen_base = tts.next;
1021 if (s1 && s2)
1022 return 0;
1023 return needs_warning ? 2 : 1;
1026 default:
1027 gcc_unreachable ();
1031 /* Return 1 if two function types F1 and F2 are compatible.
1032 If either type specifies no argument types,
1033 the other must specify a fixed number of self-promoting arg types.
1034 Otherwise, if one type specifies only the number of arguments,
1035 the other must specify that number of self-promoting arg types.
1036 Otherwise, the argument types must match. */
1038 static int
1039 function_types_compatible_p (tree f1, tree f2)
1041 tree args1, args2;
1042 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1043 int val = 1;
1044 int val1;
1045 tree ret1, ret2;
1047 ret1 = TREE_TYPE (f1);
1048 ret2 = TREE_TYPE (f2);
1050 /* 'volatile' qualifiers on a function's return type used to mean
1051 the function is noreturn. */
1052 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1053 pedwarn ("function return types not compatible due to %<volatile%>");
1054 if (TYPE_VOLATILE (ret1))
1055 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1056 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1057 if (TYPE_VOLATILE (ret2))
1058 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1059 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1060 val = comptypes (ret1, ret2);
1061 if (val == 0)
1062 return 0;
1064 args1 = TYPE_ARG_TYPES (f1);
1065 args2 = TYPE_ARG_TYPES (f2);
1067 /* An unspecified parmlist matches any specified parmlist
1068 whose argument types don't need default promotions. */
1070 if (args1 == 0)
1072 if (!self_promoting_args_p (args2))
1073 return 0;
1074 /* If one of these types comes from a non-prototype fn definition,
1075 compare that with the other type's arglist.
1076 If they don't match, ask for a warning (but no error). */
1077 if (TYPE_ACTUAL_ARG_TYPES (f1)
1078 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1079 val = 2;
1080 return val;
1082 if (args2 == 0)
1084 if (!self_promoting_args_p (args1))
1085 return 0;
1086 if (TYPE_ACTUAL_ARG_TYPES (f2)
1087 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1088 val = 2;
1089 return val;
1092 /* Both types have argument lists: compare them and propagate results. */
1093 val1 = type_lists_compatible_p (args1, args2);
1094 return val1 != 1 ? val1 : val;
1097 /* Check two lists of types for compatibility,
1098 returning 0 for incompatible, 1 for compatible,
1099 or 2 for compatible with warning. */
1101 static int
1102 type_lists_compatible_p (tree args1, tree args2)
1104 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1105 int val = 1;
1106 int newval = 0;
1108 while (1)
1110 tree a1, mv1, a2, mv2;
1111 if (args1 == 0 && args2 == 0)
1112 return val;
1113 /* If one list is shorter than the other,
1114 they fail to match. */
1115 if (args1 == 0 || args2 == 0)
1116 return 0;
1117 mv1 = a1 = TREE_VALUE (args1);
1118 mv2 = a2 = TREE_VALUE (args2);
1119 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1120 mv1 = TYPE_MAIN_VARIANT (mv1);
1121 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1122 mv2 = TYPE_MAIN_VARIANT (mv2);
1123 /* A null pointer instead of a type
1124 means there is supposed to be an argument
1125 but nothing is specified about what type it has.
1126 So match anything that self-promotes. */
1127 if (a1 == 0)
1129 if (c_type_promotes_to (a2) != a2)
1130 return 0;
1132 else if (a2 == 0)
1134 if (c_type_promotes_to (a1) != a1)
1135 return 0;
1137 /* If one of the lists has an error marker, ignore this arg. */
1138 else if (TREE_CODE (a1) == ERROR_MARK
1139 || TREE_CODE (a2) == ERROR_MARK)
1141 else if (!(newval = comptypes (mv1, mv2)))
1143 /* Allow wait (union {union wait *u; int *i} *)
1144 and wait (union wait *) to be compatible. */
1145 if (TREE_CODE (a1) == UNION_TYPE
1146 && (TYPE_NAME (a1) == 0
1147 || TYPE_TRANSPARENT_UNION (a1))
1148 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1149 && tree_int_cst_equal (TYPE_SIZE (a1),
1150 TYPE_SIZE (a2)))
1152 tree memb;
1153 for (memb = TYPE_FIELDS (a1);
1154 memb; memb = TREE_CHAIN (memb))
1156 tree mv3 = TREE_TYPE (memb);
1157 if (mv3 && mv3 != error_mark_node
1158 && TREE_CODE (mv3) != ARRAY_TYPE)
1159 mv3 = TYPE_MAIN_VARIANT (mv3);
1160 if (comptypes (mv3, mv2))
1161 break;
1163 if (memb == 0)
1164 return 0;
1166 else if (TREE_CODE (a2) == UNION_TYPE
1167 && (TYPE_NAME (a2) == 0
1168 || TYPE_TRANSPARENT_UNION (a2))
1169 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1170 && tree_int_cst_equal (TYPE_SIZE (a2),
1171 TYPE_SIZE (a1)))
1173 tree memb;
1174 for (memb = TYPE_FIELDS (a2);
1175 memb; memb = TREE_CHAIN (memb))
1177 tree mv3 = TREE_TYPE (memb);
1178 if (mv3 && mv3 != error_mark_node
1179 && TREE_CODE (mv3) != ARRAY_TYPE)
1180 mv3 = TYPE_MAIN_VARIANT (mv3);
1181 if (comptypes (mv3, mv1))
1182 break;
1184 if (memb == 0)
1185 return 0;
1187 else
1188 return 0;
1191 /* comptypes said ok, but record if it said to warn. */
1192 if (newval > val)
1193 val = newval;
1195 args1 = TREE_CHAIN (args1);
1196 args2 = TREE_CHAIN (args2);
1200 /* Compute the size to increment a pointer by. */
1202 static tree
1203 c_size_in_bytes (tree type)
1205 enum tree_code code = TREE_CODE (type);
1207 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1208 return size_one_node;
1210 if (!COMPLETE_OR_VOID_TYPE_P (type))
1212 error ("arithmetic on pointer to an incomplete type");
1213 return size_one_node;
1216 /* Convert in case a char is more than one unit. */
1217 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1218 size_int (TYPE_PRECISION (char_type_node)
1219 / BITS_PER_UNIT));
1222 /* Return either DECL or its known constant value (if it has one). */
1224 tree
1225 decl_constant_value (tree decl)
1227 if (/* Don't change a variable array bound or initial value to a constant
1228 in a place where a variable is invalid. Note that DECL_INITIAL
1229 isn't valid for a PARM_DECL. */
1230 current_function_decl != 0
1231 && TREE_CODE (decl) != PARM_DECL
1232 && !TREE_THIS_VOLATILE (decl)
1233 && TREE_READONLY (decl)
1234 && DECL_INITIAL (decl) != 0
1235 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1236 /* This is invalid if initial value is not constant.
1237 If it has either a function call, a memory reference,
1238 or a variable, then re-evaluating it could give different results. */
1239 && TREE_CONSTANT (DECL_INITIAL (decl))
1240 /* Check for cases where this is sub-optimal, even though valid. */
1241 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1242 return DECL_INITIAL (decl);
1243 return decl;
1246 /* Return either DECL or its known constant value (if it has one), but
1247 return DECL if pedantic or DECL has mode BLKmode. This is for
1248 bug-compatibility with the old behavior of decl_constant_value
1249 (before GCC 3.0); every use of this function is a bug and it should
1250 be removed before GCC 3.1. It is not appropriate to use pedantic
1251 in a way that affects optimization, and BLKmode is probably not the
1252 right test for avoiding misoptimizations either. */
1254 static tree
1255 decl_constant_value_for_broken_optimization (tree decl)
1257 tree ret;
1259 if (pedantic || DECL_MODE (decl) == BLKmode)
1260 return decl;
1262 ret = decl_constant_value (decl);
1263 /* Avoid unwanted tree sharing between the initializer and current
1264 function's body where the tree can be modified e.g. by the
1265 gimplifier. */
1266 if (ret != decl && TREE_STATIC (decl))
1267 ret = unshare_expr (ret);
1268 return ret;
1272 /* Perform the default conversion of arrays and functions to pointers.
1273 Return the result of converting EXP. For any other expression, just
1274 return EXP. */
1276 static tree
1277 default_function_array_conversion (tree exp)
1279 tree orig_exp;
1280 tree type = TREE_TYPE (exp);
1281 enum tree_code code = TREE_CODE (type);
1282 int not_lvalue = 0;
1284 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1285 an lvalue.
1287 Do not use STRIP_NOPS here! It will remove conversions from pointer
1288 to integer and cause infinite recursion. */
1289 orig_exp = exp;
1290 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1291 || (TREE_CODE (exp) == NOP_EXPR
1292 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1294 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1295 not_lvalue = 1;
1296 exp = TREE_OPERAND (exp, 0);
1299 if (TREE_NO_WARNING (orig_exp))
1300 TREE_NO_WARNING (exp) = 1;
1302 if (code == FUNCTION_TYPE)
1304 return build_unary_op (ADDR_EXPR, exp, 0);
1306 if (code == ARRAY_TYPE)
1308 tree adr;
1309 tree restype = TREE_TYPE (type);
1310 tree ptrtype;
1311 int constp = 0;
1312 int volatilep = 0;
1313 int lvalue_array_p;
1315 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1317 constp = TREE_READONLY (exp);
1318 volatilep = TREE_THIS_VOLATILE (exp);
1321 if (TYPE_QUALS (type) || constp || volatilep)
1322 restype
1323 = c_build_qualified_type (restype,
1324 TYPE_QUALS (type)
1325 | (constp * TYPE_QUAL_CONST)
1326 | (volatilep * TYPE_QUAL_VOLATILE));
1328 if (TREE_CODE (exp) == INDIRECT_REF)
1329 return convert (build_pointer_type (restype),
1330 TREE_OPERAND (exp, 0));
1332 if (TREE_CODE (exp) == COMPOUND_EXPR)
1334 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1335 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1336 TREE_OPERAND (exp, 0), op1);
1339 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1340 if (!flag_isoc99 && !lvalue_array_p)
1342 /* Before C99, non-lvalue arrays do not decay to pointers.
1343 Normally, using such an array would be invalid; but it can
1344 be used correctly inside sizeof or as a statement expression.
1345 Thus, do not give an error here; an error will result later. */
1346 return exp;
1349 ptrtype = build_pointer_type (restype);
1351 if (TREE_CODE (exp) == VAR_DECL)
1353 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1354 ADDR_EXPR because it's the best way of representing what
1355 happens in C when we take the address of an array and place
1356 it in a pointer to the element type. */
1357 adr = build1 (ADDR_EXPR, ptrtype, exp);
1358 if (!c_mark_addressable (exp))
1359 return error_mark_node;
1360 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1361 return adr;
1363 /* This way is better for a COMPONENT_REF since it can
1364 simplify the offset for a component. */
1365 adr = build_unary_op (ADDR_EXPR, exp, 1);
1366 return convert (ptrtype, adr);
1368 return exp;
1371 /* Perform default promotions for C data used in expressions.
1372 Arrays and functions are converted to pointers;
1373 enumeral types or short or char, to int.
1374 In addition, manifest constants symbols are replaced by their values. */
1376 tree
1377 default_conversion (tree exp)
1379 tree orig_exp;
1380 tree type = TREE_TYPE (exp);
1381 enum tree_code code = TREE_CODE (type);
1383 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1384 return default_function_array_conversion (exp);
1386 /* Constants can be used directly unless they're not loadable. */
1387 if (TREE_CODE (exp) == CONST_DECL)
1388 exp = DECL_INITIAL (exp);
1390 /* Replace a nonvolatile const static variable with its value unless
1391 it is an array, in which case we must be sure that taking the
1392 address of the array produces consistent results. */
1393 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1395 exp = decl_constant_value_for_broken_optimization (exp);
1396 type = TREE_TYPE (exp);
1399 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1400 an lvalue.
1402 Do not use STRIP_NOPS here! It will remove conversions from pointer
1403 to integer and cause infinite recursion. */
1404 orig_exp = exp;
1405 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1406 || (TREE_CODE (exp) == NOP_EXPR
1407 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1408 exp = TREE_OPERAND (exp, 0);
1410 if (TREE_NO_WARNING (orig_exp))
1411 TREE_NO_WARNING (exp) = 1;
1413 /* Normally convert enums to int,
1414 but convert wide enums to something wider. */
1415 if (code == ENUMERAL_TYPE)
1417 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1418 TYPE_PRECISION (integer_type_node)),
1419 ((TYPE_PRECISION (type)
1420 >= TYPE_PRECISION (integer_type_node))
1421 && TYPE_UNSIGNED (type)));
1423 return convert (type, exp);
1426 if (TREE_CODE (exp) == COMPONENT_REF
1427 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1428 /* If it's thinner than an int, promote it like a
1429 c_promoting_integer_type_p, otherwise leave it alone. */
1430 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1431 TYPE_PRECISION (integer_type_node)))
1432 return convert (integer_type_node, exp);
1434 if (c_promoting_integer_type_p (type))
1436 /* Preserve unsignedness if not really getting any wider. */
1437 if (TYPE_UNSIGNED (type)
1438 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1439 return convert (unsigned_type_node, exp);
1441 return convert (integer_type_node, exp);
1444 if (code == VOID_TYPE)
1446 error ("void value not ignored as it ought to be");
1447 return error_mark_node;
1449 return exp;
1452 /* Look up COMPONENT in a structure or union DECL.
1454 If the component name is not found, returns NULL_TREE. Otherwise,
1455 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1456 stepping down the chain to the component, which is in the last
1457 TREE_VALUE of the list. Normally the list is of length one, but if
1458 the component is embedded within (nested) anonymous structures or
1459 unions, the list steps down the chain to the component. */
1461 static tree
1462 lookup_field (tree decl, tree component)
1464 tree type = TREE_TYPE (decl);
1465 tree field;
1467 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1468 to the field elements. Use a binary search on this array to quickly
1469 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1470 will always be set for structures which have many elements. */
1472 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1474 int bot, top, half;
1475 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1477 field = TYPE_FIELDS (type);
1478 bot = 0;
1479 top = TYPE_LANG_SPECIFIC (type)->s->len;
1480 while (top - bot > 1)
1482 half = (top - bot + 1) >> 1;
1483 field = field_array[bot+half];
1485 if (DECL_NAME (field) == NULL_TREE)
1487 /* Step through all anon unions in linear fashion. */
1488 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1490 field = field_array[bot++];
1491 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1492 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1494 tree anon = lookup_field (field, component);
1496 if (anon)
1497 return tree_cons (NULL_TREE, field, anon);
1501 /* Entire record is only anon unions. */
1502 if (bot > top)
1503 return NULL_TREE;
1505 /* Restart the binary search, with new lower bound. */
1506 continue;
1509 if (DECL_NAME (field) == component)
1510 break;
1511 if (DECL_NAME (field) < component)
1512 bot += half;
1513 else
1514 top = bot + half;
1517 if (DECL_NAME (field_array[bot]) == component)
1518 field = field_array[bot];
1519 else if (DECL_NAME (field) != component)
1520 return NULL_TREE;
1522 else
1524 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1526 if (DECL_NAME (field) == NULL_TREE
1527 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1528 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1530 tree anon = lookup_field (field, component);
1532 if (anon)
1533 return tree_cons (NULL_TREE, field, anon);
1536 if (DECL_NAME (field) == component)
1537 break;
1540 if (field == NULL_TREE)
1541 return NULL_TREE;
1544 return tree_cons (NULL_TREE, field, NULL_TREE);
1547 /* Make an expression to refer to the COMPONENT field of
1548 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1550 tree
1551 build_component_ref (tree datum, tree component)
1553 tree type = TREE_TYPE (datum);
1554 enum tree_code code = TREE_CODE (type);
1555 tree field = NULL;
1556 tree ref;
1558 if (!objc_is_public (datum, component))
1559 return error_mark_node;
1561 /* See if there is a field or component with name COMPONENT. */
1563 if (code == RECORD_TYPE || code == UNION_TYPE)
1565 if (!COMPLETE_TYPE_P (type))
1567 c_incomplete_type_error (NULL_TREE, type);
1568 return error_mark_node;
1571 field = lookup_field (datum, component);
1573 if (!field)
1575 error ("%qT has no member named %qs", type,
1576 IDENTIFIER_POINTER (component));
1577 return error_mark_node;
1580 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1581 This might be better solved in future the way the C++ front
1582 end does it - by giving the anonymous entities each a
1583 separate name and type, and then have build_component_ref
1584 recursively call itself. We can't do that here. */
1587 tree subdatum = TREE_VALUE (field);
1589 if (TREE_TYPE (subdatum) == error_mark_node)
1590 return error_mark_node;
1592 ref = build3 (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum,
1593 NULL_TREE);
1594 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1595 TREE_READONLY (ref) = 1;
1596 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1597 TREE_THIS_VOLATILE (ref) = 1;
1599 if (TREE_DEPRECATED (subdatum))
1600 warn_deprecated_use (subdatum);
1602 datum = ref;
1604 field = TREE_CHAIN (field);
1606 while (field);
1608 return ref;
1610 else if (code != ERROR_MARK)
1611 error ("request for member %qs in something not a structure or union",
1612 IDENTIFIER_POINTER (component));
1614 return error_mark_node;
1617 /* Given an expression PTR for a pointer, return an expression
1618 for the value pointed to.
1619 ERRORSTRING is the name of the operator to appear in error messages. */
1621 tree
1622 build_indirect_ref (tree ptr, const char *errorstring)
1624 tree pointer = default_conversion (ptr);
1625 tree type = TREE_TYPE (pointer);
1627 if (TREE_CODE (type) == POINTER_TYPE)
1629 if (TREE_CODE (pointer) == ADDR_EXPR
1630 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1631 == TREE_TYPE (type)))
1632 return TREE_OPERAND (pointer, 0);
1633 else
1635 tree t = TREE_TYPE (type);
1636 tree mvt = t;
1637 tree ref;
1639 if (TREE_CODE (mvt) != ARRAY_TYPE)
1640 mvt = TYPE_MAIN_VARIANT (mvt);
1641 ref = build1 (INDIRECT_REF, mvt, pointer);
1643 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1645 error ("dereferencing pointer to incomplete type");
1646 return error_mark_node;
1648 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1649 warning ("dereferencing %<void *%> pointer");
1651 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1652 so that we get the proper error message if the result is used
1653 to assign to. Also, &* is supposed to be a no-op.
1654 And ANSI C seems to specify that the type of the result
1655 should be the const type. */
1656 /* A de-reference of a pointer to const is not a const. It is valid
1657 to change it via some other pointer. */
1658 TREE_READONLY (ref) = TYPE_READONLY (t);
1659 TREE_SIDE_EFFECTS (ref)
1660 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1661 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1662 return ref;
1665 else if (TREE_CODE (pointer) != ERROR_MARK)
1666 error ("invalid type argument of %qs", errorstring);
1667 return error_mark_node;
1670 /* This handles expressions of the form "a[i]", which denotes
1671 an array reference.
1673 This is logically equivalent in C to *(a+i), but we may do it differently.
1674 If A is a variable or a member, we generate a primitive ARRAY_REF.
1675 This avoids forcing the array out of registers, and can work on
1676 arrays that are not lvalues (for example, members of structures returned
1677 by functions). */
1679 tree
1680 build_array_ref (tree array, tree index)
1682 bool swapped = false;
1683 if (TREE_TYPE (array) == error_mark_node
1684 || TREE_TYPE (index) == error_mark_node)
1685 return error_mark_node;
1687 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
1688 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
1690 tree temp;
1691 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
1692 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
1694 error ("subscripted value is neither array nor pointer");
1695 return error_mark_node;
1697 temp = array;
1698 array = index;
1699 index = temp;
1700 swapped = true;
1703 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
1705 error ("array subscript is not an integer");
1706 return error_mark_node;
1709 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
1711 error ("subscripted value is pointer to function");
1712 return error_mark_node;
1715 /* Subscripting with type char is likely to lose on a machine where
1716 chars are signed. So warn on any machine, but optionally. Don't
1717 warn for unsigned char since that type is safe. Don't warn for
1718 signed char because anyone who uses that must have done so
1719 deliberately. ??? Existing practice has also been to warn only
1720 when the char index is syntactically the index, not for
1721 char[array]. */
1722 if (warn_char_subscripts && !swapped
1723 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1724 warning ("array subscript has type %<char%>");
1726 /* Apply default promotions *after* noticing character types. */
1727 index = default_conversion (index);
1729 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
1731 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
1733 tree rval, type;
1735 /* An array that is indexed by a non-constant
1736 cannot be stored in a register; we must be able to do
1737 address arithmetic on its address.
1738 Likewise an array of elements of variable size. */
1739 if (TREE_CODE (index) != INTEGER_CST
1740 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1741 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1743 if (!c_mark_addressable (array))
1744 return error_mark_node;
1746 /* An array that is indexed by a constant value which is not within
1747 the array bounds cannot be stored in a register either; because we
1748 would get a crash in store_bit_field/extract_bit_field when trying
1749 to access a non-existent part of the register. */
1750 if (TREE_CODE (index) == INTEGER_CST
1751 && TYPE_DOMAIN (TREE_TYPE (array))
1752 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1754 if (!c_mark_addressable (array))
1755 return error_mark_node;
1758 if (pedantic)
1760 tree foo = array;
1761 while (TREE_CODE (foo) == COMPONENT_REF)
1762 foo = TREE_OPERAND (foo, 0);
1763 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
1764 pedwarn ("ISO C forbids subscripting %<register%> array");
1765 else if (!flag_isoc99 && !lvalue_p (foo))
1766 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1769 type = TREE_TYPE (TREE_TYPE (array));
1770 if (TREE_CODE (type) != ARRAY_TYPE)
1771 type = TYPE_MAIN_VARIANT (type);
1772 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
1773 /* Array ref is const/volatile if the array elements are
1774 or if the array is. */
1775 TREE_READONLY (rval)
1776 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1777 | TREE_READONLY (array));
1778 TREE_SIDE_EFFECTS (rval)
1779 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1780 | TREE_SIDE_EFFECTS (array));
1781 TREE_THIS_VOLATILE (rval)
1782 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1783 /* This was added by rms on 16 Nov 91.
1784 It fixes vol struct foo *a; a->elts[1]
1785 in an inline function.
1786 Hope it doesn't break something else. */
1787 | TREE_THIS_VOLATILE (array));
1788 return require_complete_type (fold (rval));
1790 else
1792 tree ar = default_conversion (array);
1794 if (ar == error_mark_node)
1795 return ar;
1797 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
1798 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
1800 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, index, 0),
1801 "array indexing");
1805 /* Build an external reference to identifier ID. FUN indicates
1806 whether this will be used for a function call. */
1807 tree
1808 build_external_ref (tree id, int fun)
1810 tree ref;
1811 tree decl = lookup_name (id);
1813 /* In Objective-C, an instance variable (ivar) may be preferred to
1814 whatever lookup_name() found. */
1815 decl = objc_lookup_ivar (decl, id);
1817 if (decl && decl != error_mark_node)
1818 ref = decl;
1819 else if (fun)
1820 /* Implicit function declaration. */
1821 ref = implicitly_declare (id);
1822 else if (decl == error_mark_node)
1823 /* Don't complain about something that's already been
1824 complained about. */
1825 return error_mark_node;
1826 else
1828 undeclared_variable (id);
1829 return error_mark_node;
1832 if (TREE_TYPE (ref) == error_mark_node)
1833 return error_mark_node;
1835 if (TREE_DEPRECATED (ref))
1836 warn_deprecated_use (ref);
1838 if (!skip_evaluation)
1839 assemble_external (ref);
1840 TREE_USED (ref) = 1;
1842 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1844 if (!in_sizeof && !in_typeof)
1845 C_DECL_USED (ref) = 1;
1846 else if (DECL_INITIAL (ref) == 0
1847 && DECL_EXTERNAL (ref)
1848 && !TREE_PUBLIC (ref))
1849 record_maybe_used_decl (ref);
1852 if (TREE_CODE (ref) == CONST_DECL)
1854 ref = DECL_INITIAL (ref);
1855 TREE_CONSTANT (ref) = 1;
1856 TREE_INVARIANT (ref) = 1;
1858 else if (current_function_decl != 0
1859 && !DECL_FILE_SCOPE_P (current_function_decl)
1860 && (TREE_CODE (ref) == VAR_DECL
1861 || TREE_CODE (ref) == PARM_DECL
1862 || TREE_CODE (ref) == FUNCTION_DECL))
1864 tree context = decl_function_context (ref);
1866 if (context != 0 && context != current_function_decl)
1867 DECL_NONLOCAL (ref) = 1;
1870 return ref;
1873 /* Record details of decls possibly used inside sizeof or typeof. */
1874 struct maybe_used_decl
1876 /* The decl. */
1877 tree decl;
1878 /* The level seen at (in_sizeof + in_typeof). */
1879 int level;
1880 /* The next one at this level or above, or NULL. */
1881 struct maybe_used_decl *next;
1884 static struct maybe_used_decl *maybe_used_decls;
1886 /* Record that DECL, an undefined static function reference seen
1887 inside sizeof or typeof, might be used if the operand of sizeof is
1888 a VLA type or the operand of typeof is a variably modified
1889 type. */
1891 static void
1892 record_maybe_used_decl (tree decl)
1894 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1895 t->decl = decl;
1896 t->level = in_sizeof + in_typeof;
1897 t->next = maybe_used_decls;
1898 maybe_used_decls = t;
1901 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1902 USED is false, just discard them. If it is true, mark them used
1903 (if no longer inside sizeof or typeof) or move them to the next
1904 level up (if still inside sizeof or typeof). */
1906 void
1907 pop_maybe_used (bool used)
1909 struct maybe_used_decl *p = maybe_used_decls;
1910 int cur_level = in_sizeof + in_typeof;
1911 while (p && p->level > cur_level)
1913 if (used)
1915 if (cur_level == 0)
1916 C_DECL_USED (p->decl) = 1;
1917 else
1918 p->level = cur_level;
1920 p = p->next;
1922 if (!used || cur_level == 0)
1923 maybe_used_decls = p;
1926 /* Return the result of sizeof applied to EXPR. */
1928 struct c_expr
1929 c_expr_sizeof_expr (struct c_expr expr)
1931 struct c_expr ret;
1932 if (expr.value == error_mark_node)
1934 ret.value = error_mark_node;
1935 ret.original_code = ERROR_MARK;
1936 pop_maybe_used (false);
1938 else
1940 ret.value = c_sizeof (TREE_TYPE (expr.value));
1941 ret.original_code = ERROR_MARK;
1942 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1944 return ret;
1947 /* Return the result of sizeof applied to T, a structure for the type
1948 name passed to sizeof (rather than the type itself). */
1950 struct c_expr
1951 c_expr_sizeof_type (struct c_type_name *t)
1953 tree type;
1954 struct c_expr ret;
1955 type = groktypename (t);
1956 ret.value = c_sizeof (type);
1957 ret.original_code = ERROR_MARK;
1958 pop_maybe_used (type != error_mark_node
1959 ? C_TYPE_VARIABLE_SIZE (type) : false);
1960 return ret;
1963 /* Build a function call to function FUNCTION with parameters PARAMS.
1964 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1965 TREE_VALUE of each node is a parameter-expression.
1966 FUNCTION's data type may be a function type or a pointer-to-function. */
1968 tree
1969 build_function_call (tree function, tree params)
1971 tree fntype, fundecl = 0;
1972 tree coerced_params;
1973 tree name = NULL_TREE, result;
1974 tree tem;
1976 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1977 STRIP_TYPE_NOPS (function);
1979 /* Convert anything with function type to a pointer-to-function. */
1980 if (TREE_CODE (function) == FUNCTION_DECL)
1982 name = DECL_NAME (function);
1984 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1985 (because calling an inline function does not mean the function
1986 needs to be separately compiled). */
1987 fntype = build_type_variant (TREE_TYPE (function),
1988 TREE_READONLY (function),
1989 TREE_THIS_VOLATILE (function));
1990 fundecl = function;
1991 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1993 else
1994 function = default_conversion (function);
1996 fntype = TREE_TYPE (function);
1998 if (TREE_CODE (fntype) == ERROR_MARK)
1999 return error_mark_node;
2001 if (!(TREE_CODE (fntype) == POINTER_TYPE
2002 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2004 error ("called object %qE is not a function", function);
2005 return error_mark_node;
2008 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2009 current_function_returns_abnormally = 1;
2011 /* fntype now gets the type of function pointed to. */
2012 fntype = TREE_TYPE (fntype);
2014 /* Check that the function is called through a compatible prototype.
2015 If it is not, replace the call by a trap, wrapped up in a compound
2016 expression if necessary. This has the nice side-effect to prevent
2017 the tree-inliner from generating invalid assignment trees which may
2018 blow up in the RTL expander later.
2020 ??? This doesn't work for Objective-C because objc_comptypes
2021 refuses to compare function prototypes, yet the compiler appears
2022 to build calls that are flagged as invalid by C's comptypes. */
2023 if (!c_dialect_objc ()
2024 && TREE_CODE (function) == NOP_EXPR
2025 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2026 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2027 && !comptypes (fntype, TREE_TYPE (tem)))
2029 tree return_type = TREE_TYPE (fntype);
2030 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2031 NULL_TREE);
2033 /* This situation leads to run-time undefined behavior. We can't,
2034 therefore, simply error unless we can prove that all possible
2035 executions of the program must execute the code. */
2036 warning ("function called through a non-compatible type");
2038 /* We can, however, treat "undefined" any way we please.
2039 Call abort to encourage the user to fix the program. */
2040 inform ("if this code is reached, the program will abort");
2042 if (VOID_TYPE_P (return_type))
2043 return trap;
2044 else
2046 tree rhs;
2048 if (AGGREGATE_TYPE_P (return_type))
2049 rhs = build_compound_literal (return_type,
2050 build_constructor (return_type,
2051 NULL_TREE));
2052 else
2053 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
2055 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2059 /* Convert the parameters to the types declared in the
2060 function prototype, or apply default promotions. */
2062 coerced_params
2063 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2065 if (coerced_params == error_mark_node)
2066 return error_mark_node;
2068 /* Check that the arguments to the function are valid. */
2070 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params,
2071 TYPE_ARG_TYPES (fntype));
2073 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
2074 function, coerced_params, NULL_TREE);
2075 TREE_SIDE_EFFECTS (result) = 1;
2077 if (require_constant_value)
2079 result = fold_initializer (result);
2081 if (TREE_CONSTANT (result)
2082 && (name == NULL_TREE
2083 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2084 pedwarn_init ("initializer element is not constant");
2086 else
2087 result = fold (result);
2089 if (VOID_TYPE_P (TREE_TYPE (result)))
2090 return result;
2091 return require_complete_type (result);
2094 /* Convert the argument expressions in the list VALUES
2095 to the types in the list TYPELIST. The result is a list of converted
2096 argument expressions, unless there are too few arguments in which
2097 case it is error_mark_node.
2099 If TYPELIST is exhausted, or when an element has NULL as its type,
2100 perform the default conversions.
2102 PARMLIST is the chain of parm decls for the function being called.
2103 It may be 0, if that info is not available.
2104 It is used only for generating error messages.
2106 FUNCTION is a tree for the called function. It is used only for
2107 error messages, where it is formatted with %qE.
2109 This is also where warnings about wrong number of args are generated.
2111 Both VALUES and the returned value are chains of TREE_LIST nodes
2112 with the elements of the list in the TREE_VALUE slots of those nodes. */
2114 static tree
2115 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2117 tree typetail, valtail;
2118 tree result = NULL;
2119 int parmnum;
2120 tree selector;
2122 /* Change pointer to function to the function itself for
2123 diagnostics. */
2124 if (TREE_CODE (function) == ADDR_EXPR
2125 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2126 function = TREE_OPERAND (function, 0);
2128 /* Handle an ObjC selector specially for diagnostics. */
2129 selector = objc_message_selector ();
2131 /* Scan the given expressions and types, producing individual
2132 converted arguments and pushing them on RESULT in reverse order. */
2134 for (valtail = values, typetail = typelist, parmnum = 0;
2135 valtail;
2136 valtail = TREE_CHAIN (valtail), parmnum++)
2138 tree type = typetail ? TREE_VALUE (typetail) : 0;
2139 tree val = TREE_VALUE (valtail);
2140 tree rname = function;
2141 int argnum = parmnum + 1;
2143 if (type == void_type_node)
2145 error ("too many arguments to function %qE", function);
2146 break;
2149 if (selector && argnum > 2)
2151 rname = selector;
2152 argnum -= 2;
2155 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2156 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
2157 to convert automatically to a pointer. */
2158 if (TREE_CODE (val) == NON_LVALUE_EXPR)
2159 val = TREE_OPERAND (val, 0);
2161 val = default_function_array_conversion (val);
2163 val = require_complete_type (val);
2165 if (type != 0)
2167 /* Formal parm type is specified by a function prototype. */
2168 tree parmval;
2170 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2172 error ("type of formal parameter %d is incomplete", parmnum + 1);
2173 parmval = val;
2175 else
2177 /* Optionally warn about conversions that
2178 differ from the default conversions. */
2179 if (warn_conversion || warn_traditional)
2181 unsigned int formal_prec = TYPE_PRECISION (type);
2183 if (INTEGRAL_TYPE_P (type)
2184 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2185 warning ("passing argument %d of %qE as integer "
2186 "rather than floating due to prototype",
2187 argnum, rname);
2188 if (INTEGRAL_TYPE_P (type)
2189 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2190 warning ("passing argument %d of %qE as integer "
2191 "rather than complex due to prototype",
2192 argnum, rname);
2193 else if (TREE_CODE (type) == COMPLEX_TYPE
2194 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2195 warning ("passing argument %d of %qE as complex "
2196 "rather than floating due to prototype",
2197 argnum, rname);
2198 else if (TREE_CODE (type) == REAL_TYPE
2199 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2200 warning ("passing argument %d of %qE as floating "
2201 "rather than integer due to prototype",
2202 argnum, rname);
2203 else if (TREE_CODE (type) == COMPLEX_TYPE
2204 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2205 warning ("passing argument %d of %qE as complex "
2206 "rather than integer due to prototype",
2207 argnum, rname);
2208 else if (TREE_CODE (type) == REAL_TYPE
2209 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2210 warning ("passing argument %d of %qE as floating "
2211 "rather than complex due to prototype",
2212 argnum, rname);
2213 /* ??? At some point, messages should be written about
2214 conversions between complex types, but that's too messy
2215 to do now. */
2216 else if (TREE_CODE (type) == REAL_TYPE
2217 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2219 /* Warn if any argument is passed as `float',
2220 since without a prototype it would be `double'. */
2221 if (formal_prec == TYPE_PRECISION (float_type_node))
2222 warning ("passing argument %d of %qE as %<float%> "
2223 "rather than %<double%> due to prototype",
2224 argnum, rname);
2226 /* Detect integer changing in width or signedness.
2227 These warnings are only activated with
2228 -Wconversion, not with -Wtraditional. */
2229 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2230 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2232 tree would_have_been = default_conversion (val);
2233 tree type1 = TREE_TYPE (would_have_been);
2235 if (TREE_CODE (type) == ENUMERAL_TYPE
2236 && (TYPE_MAIN_VARIANT (type)
2237 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2238 /* No warning if function asks for enum
2239 and the actual arg is that enum type. */
2241 else if (formal_prec != TYPE_PRECISION (type1))
2242 warning ("passing argument %d of %qE with different "
2243 "width due to prototype", argnum, rname);
2244 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2246 /* Don't complain if the formal parameter type
2247 is an enum, because we can't tell now whether
2248 the value was an enum--even the same enum. */
2249 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2251 else if (TREE_CODE (val) == INTEGER_CST
2252 && int_fits_type_p (val, type))
2253 /* Change in signedness doesn't matter
2254 if a constant value is unaffected. */
2256 /* Likewise for a constant in a NOP_EXPR. */
2257 else if (TREE_CODE (val) == NOP_EXPR
2258 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
2259 && int_fits_type_p (TREE_OPERAND (val, 0), type))
2261 /* If the value is extended from a narrower
2262 unsigned type, it doesn't matter whether we
2263 pass it as signed or unsigned; the value
2264 certainly is the same either way. */
2265 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2266 && TYPE_UNSIGNED (TREE_TYPE (val)))
2268 else if (TYPE_UNSIGNED (type))
2269 warning ("passing argument %d of %qE as unsigned "
2270 "due to prototype", argnum, rname);
2271 else
2272 warning ("passing argument %d of %qE as signed "
2273 "due to prototype", argnum, rname);
2277 parmval = convert_for_assignment (type, val, ic_argpass,
2278 fundecl, function,
2279 parmnum + 1);
2281 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2282 && INTEGRAL_TYPE_P (type)
2283 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2284 parmval = default_conversion (parmval);
2286 result = tree_cons (NULL_TREE, parmval, result);
2288 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2289 && (TYPE_PRECISION (TREE_TYPE (val))
2290 < TYPE_PRECISION (double_type_node)))
2291 /* Convert `float' to `double'. */
2292 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2293 else
2294 /* Convert `short' and `char' to full-size `int'. */
2295 result = tree_cons (NULL_TREE, default_conversion (val), result);
2297 if (typetail)
2298 typetail = TREE_CHAIN (typetail);
2301 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2303 error ("too few arguments to function %qE", function);
2304 return error_mark_node;
2307 return nreverse (result);
2310 /* This is the entry point used by the parser
2311 for binary operators in the input.
2312 In addition to constructing the expression,
2313 we check for operands that were written with other binary operators
2314 in a way that is likely to confuse the user. */
2316 struct c_expr
2317 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2318 struct c_expr arg2)
2320 struct c_expr result;
2322 enum tree_code code1 = arg1.original_code;
2323 enum tree_code code2 = arg2.original_code;
2325 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2326 result.original_code = code;
2328 if (TREE_CODE (result.value) == ERROR_MARK)
2329 return result;
2331 /* Check for cases such as x+y<<z which users are likely
2332 to misinterpret. */
2333 if (warn_parentheses)
2335 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2337 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2338 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2339 warning ("suggest parentheses around + or - inside shift");
2342 if (code == TRUTH_ORIF_EXPR)
2344 if (code1 == TRUTH_ANDIF_EXPR
2345 || code2 == TRUTH_ANDIF_EXPR)
2346 warning ("suggest parentheses around && within ||");
2349 if (code == BIT_IOR_EXPR)
2351 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2352 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2353 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2354 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2355 warning ("suggest parentheses around arithmetic in operand of |");
2356 /* Check cases like x|y==z */
2357 if (TREE_CODE_CLASS (code1) == tcc_comparison
2358 || TREE_CODE_CLASS (code2) == tcc_comparison)
2359 warning ("suggest parentheses around comparison in operand of |");
2362 if (code == BIT_XOR_EXPR)
2364 if (code1 == BIT_AND_EXPR
2365 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2366 || code2 == BIT_AND_EXPR
2367 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2368 warning ("suggest parentheses around arithmetic in operand of ^");
2369 /* Check cases like x^y==z */
2370 if (TREE_CODE_CLASS (code1) == tcc_comparison
2371 || TREE_CODE_CLASS (code2) == tcc_comparison)
2372 warning ("suggest parentheses around comparison in operand of ^");
2375 if (code == BIT_AND_EXPR)
2377 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2378 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2379 warning ("suggest parentheses around + or - in operand of &");
2380 /* Check cases like x&y==z */
2381 if (TREE_CODE_CLASS (code1) == tcc_comparison
2382 || TREE_CODE_CLASS (code2) == tcc_comparison)
2383 warning ("suggest parentheses around comparison in operand of &");
2385 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2386 if (TREE_CODE_CLASS (code) == tcc_comparison
2387 && (TREE_CODE_CLASS (code1) == tcc_comparison
2388 || TREE_CODE_CLASS (code2) == tcc_comparison))
2389 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2393 unsigned_conversion_warning (result.value, arg1.value);
2394 unsigned_conversion_warning (result.value, arg2.value);
2395 overflow_warning (result.value);
2397 return result;
2400 /* Return a tree for the difference of pointers OP0 and OP1.
2401 The resulting tree has type int. */
2403 static tree
2404 pointer_diff (tree op0, tree op1)
2406 tree restype = ptrdiff_type_node;
2408 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2409 tree con0, con1, lit0, lit1;
2410 tree orig_op1 = op1;
2412 if (pedantic || warn_pointer_arith)
2414 if (TREE_CODE (target_type) == VOID_TYPE)
2415 pedwarn ("pointer of type %<void *%> used in subtraction");
2416 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2417 pedwarn ("pointer to a function used in subtraction");
2420 /* If the conversion to ptrdiff_type does anything like widening or
2421 converting a partial to an integral mode, we get a convert_expression
2422 that is in the way to do any simplifications.
2423 (fold-const.c doesn't know that the extra bits won't be needed.
2424 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2425 different mode in place.)
2426 So first try to find a common term here 'by hand'; we want to cover
2427 at least the cases that occur in legal static initializers. */
2428 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2429 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2431 if (TREE_CODE (con0) == PLUS_EXPR)
2433 lit0 = TREE_OPERAND (con0, 1);
2434 con0 = TREE_OPERAND (con0, 0);
2436 else
2437 lit0 = integer_zero_node;
2439 if (TREE_CODE (con1) == PLUS_EXPR)
2441 lit1 = TREE_OPERAND (con1, 1);
2442 con1 = TREE_OPERAND (con1, 0);
2444 else
2445 lit1 = integer_zero_node;
2447 if (operand_equal_p (con0, con1, 0))
2449 op0 = lit0;
2450 op1 = lit1;
2454 /* First do the subtraction as integers;
2455 then drop through to build the divide operator.
2456 Do not do default conversions on the minus operator
2457 in case restype is a short type. */
2459 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2460 convert (restype, op1), 0);
2461 /* This generates an error if op1 is pointer to incomplete type. */
2462 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2463 error ("arithmetic on pointer to an incomplete type");
2465 /* This generates an error if op0 is pointer to incomplete type. */
2466 op1 = c_size_in_bytes (target_type);
2468 /* Divide by the size, in easiest possible way. */
2469 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2472 /* Construct and perhaps optimize a tree representation
2473 for a unary operation. CODE, a tree_code, specifies the operation
2474 and XARG is the operand.
2475 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2476 the default promotions (such as from short to int).
2477 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2478 allows non-lvalues; this is only used to handle conversion of non-lvalue
2479 arrays to pointers in C99. */
2481 tree
2482 build_unary_op (enum tree_code code, tree xarg, int flag)
2484 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2485 tree arg = xarg;
2486 tree argtype = 0;
2487 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2488 tree val;
2489 int noconvert = flag;
2491 if (typecode == ERROR_MARK)
2492 return error_mark_node;
2493 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2494 typecode = INTEGER_TYPE;
2496 switch (code)
2498 case CONVERT_EXPR:
2499 /* This is used for unary plus, because a CONVERT_EXPR
2500 is enough to prevent anybody from looking inside for
2501 associativity, but won't generate any code. */
2502 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2503 || typecode == COMPLEX_TYPE
2504 || typecode == VECTOR_TYPE))
2506 error ("wrong type argument to unary plus");
2507 return error_mark_node;
2509 else if (!noconvert)
2510 arg = default_conversion (arg);
2511 arg = non_lvalue (arg);
2512 break;
2514 case NEGATE_EXPR:
2515 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2516 || typecode == COMPLEX_TYPE
2517 || typecode == VECTOR_TYPE))
2519 error ("wrong type argument to unary minus");
2520 return error_mark_node;
2522 else if (!noconvert)
2523 arg = default_conversion (arg);
2524 break;
2526 case BIT_NOT_EXPR:
2527 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2529 if (!noconvert)
2530 arg = default_conversion (arg);
2532 else if (typecode == COMPLEX_TYPE)
2534 code = CONJ_EXPR;
2535 if (pedantic)
2536 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2537 if (!noconvert)
2538 arg = default_conversion (arg);
2540 else
2542 error ("wrong type argument to bit-complement");
2543 return error_mark_node;
2545 break;
2547 case ABS_EXPR:
2548 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2550 error ("wrong type argument to abs");
2551 return error_mark_node;
2553 else if (!noconvert)
2554 arg = default_conversion (arg);
2555 break;
2557 case CONJ_EXPR:
2558 /* Conjugating a real value is a no-op, but allow it anyway. */
2559 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2560 || typecode == COMPLEX_TYPE))
2562 error ("wrong type argument to conjugation");
2563 return error_mark_node;
2565 else if (!noconvert)
2566 arg = default_conversion (arg);
2567 break;
2569 case TRUTH_NOT_EXPR:
2570 if (typecode != INTEGER_TYPE
2571 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2572 && typecode != COMPLEX_TYPE
2573 /* These will convert to a pointer. */
2574 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2576 error ("wrong type argument to unary exclamation mark");
2577 return error_mark_node;
2579 arg = lang_hooks.truthvalue_conversion (arg);
2580 return invert_truthvalue (arg);
2582 case NOP_EXPR:
2583 break;
2585 case REALPART_EXPR:
2586 if (TREE_CODE (arg) == COMPLEX_CST)
2587 return TREE_REALPART (arg);
2588 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2589 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2590 else
2591 return arg;
2593 case IMAGPART_EXPR:
2594 if (TREE_CODE (arg) == COMPLEX_CST)
2595 return TREE_IMAGPART (arg);
2596 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2597 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2598 else
2599 return convert (TREE_TYPE (arg), integer_zero_node);
2601 case PREINCREMENT_EXPR:
2602 case POSTINCREMENT_EXPR:
2603 case PREDECREMENT_EXPR:
2604 case POSTDECREMENT_EXPR:
2606 /* Increment or decrement the real part of the value,
2607 and don't change the imaginary part. */
2608 if (typecode == COMPLEX_TYPE)
2610 tree real, imag;
2612 if (pedantic)
2613 pedwarn ("ISO C does not support %<++%> and %<--%>"
2614 " on complex types");
2616 arg = stabilize_reference (arg);
2617 real = build_unary_op (REALPART_EXPR, arg, 1);
2618 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2619 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2620 build_unary_op (code, real, 1), imag);
2623 /* Report invalid types. */
2625 if (typecode != POINTER_TYPE
2626 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2628 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2629 error ("wrong type argument to increment");
2630 else
2631 error ("wrong type argument to decrement");
2633 return error_mark_node;
2637 tree inc;
2638 tree result_type = TREE_TYPE (arg);
2640 arg = get_unwidened (arg, 0);
2641 argtype = TREE_TYPE (arg);
2643 /* Compute the increment. */
2645 if (typecode == POINTER_TYPE)
2647 /* If pointer target is an undefined struct,
2648 we just cannot know how to do the arithmetic. */
2649 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2651 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2652 error ("increment of pointer to unknown structure");
2653 else
2654 error ("decrement of pointer to unknown structure");
2656 else if ((pedantic || warn_pointer_arith)
2657 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2658 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2660 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2661 pedwarn ("wrong type argument to increment");
2662 else
2663 pedwarn ("wrong type argument to decrement");
2666 inc = c_size_in_bytes (TREE_TYPE (result_type));
2668 else
2669 inc = integer_one_node;
2671 inc = convert (argtype, inc);
2673 /* Complain about anything else that is not a true lvalue. */
2674 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2675 || code == POSTINCREMENT_EXPR)
2676 ? lv_increment
2677 : lv_decrement)))
2678 return error_mark_node;
2680 /* Report a read-only lvalue. */
2681 if (TREE_READONLY (arg))
2682 readonly_error (arg,
2683 ((code == PREINCREMENT_EXPR
2684 || code == POSTINCREMENT_EXPR)
2685 ? lv_increment : lv_decrement));
2687 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2688 val = boolean_increment (code, arg);
2689 else
2690 val = build2 (code, TREE_TYPE (arg), arg, inc);
2691 TREE_SIDE_EFFECTS (val) = 1;
2692 val = convert (result_type, val);
2693 if (TREE_CODE (val) != code)
2694 TREE_NO_WARNING (val) = 1;
2695 return val;
2698 case ADDR_EXPR:
2699 /* Note that this operation never does default_conversion. */
2701 /* Let &* cancel out to simplify resulting code. */
2702 if (TREE_CODE (arg) == INDIRECT_REF)
2704 /* Don't let this be an lvalue. */
2705 if (lvalue_p (TREE_OPERAND (arg, 0)))
2706 return non_lvalue (TREE_OPERAND (arg, 0));
2707 return TREE_OPERAND (arg, 0);
2710 /* For &x[y], return x+y */
2711 if (TREE_CODE (arg) == ARRAY_REF)
2713 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2714 return error_mark_node;
2715 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2716 TREE_OPERAND (arg, 1), 1);
2719 /* Anything not already handled and not a true memory reference
2720 or a non-lvalue array is an error. */
2721 else if (typecode != FUNCTION_TYPE && !flag
2722 && !lvalue_or_else (arg, lv_addressof))
2723 return error_mark_node;
2725 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2726 argtype = TREE_TYPE (arg);
2728 /* If the lvalue is const or volatile, merge that into the type
2729 to which the address will point. Note that you can't get a
2730 restricted pointer by taking the address of something, so we
2731 only have to deal with `const' and `volatile' here. */
2732 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2733 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2734 argtype = c_build_type_variant (argtype,
2735 TREE_READONLY (arg),
2736 TREE_THIS_VOLATILE (arg));
2738 if (!c_mark_addressable (arg))
2739 return error_mark_node;
2741 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2742 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2744 argtype = build_pointer_type (argtype);
2746 /* ??? Cope with user tricks that amount to offsetof. Delete this
2747 when we have proper support for integer constant expressions. */
2748 val = get_base_address (arg);
2749 if (val && TREE_CODE (val) == INDIRECT_REF
2750 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
2752 tree op0 = fold_convert (argtype, fold_offsetof (arg)), op1;
2754 op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
2755 return fold (build2 (PLUS_EXPR, argtype, op0, op1));
2758 val = build1 (ADDR_EXPR, argtype, arg);
2760 return val;
2762 default:
2763 break;
2766 if (argtype == 0)
2767 argtype = TREE_TYPE (arg);
2768 val = build1 (code, argtype, arg);
2769 return require_constant_value ? fold_initializer (val) : fold (val);
2772 /* Return nonzero if REF is an lvalue valid for this language.
2773 Lvalues can be assigned, unless their type has TYPE_READONLY.
2774 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2777 lvalue_p (tree ref)
2779 enum tree_code code = TREE_CODE (ref);
2781 switch (code)
2783 case REALPART_EXPR:
2784 case IMAGPART_EXPR:
2785 case COMPONENT_REF:
2786 return lvalue_p (TREE_OPERAND (ref, 0));
2788 case COMPOUND_LITERAL_EXPR:
2789 case STRING_CST:
2790 return 1;
2792 case INDIRECT_REF:
2793 case ARRAY_REF:
2794 case VAR_DECL:
2795 case PARM_DECL:
2796 case RESULT_DECL:
2797 case ERROR_MARK:
2798 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2799 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2801 case BIND_EXPR:
2802 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2804 default:
2805 return 0;
2809 /* Give an error for storing in something that is 'const'. */
2811 static void
2812 readonly_error (tree arg, enum lvalue_use use)
2814 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
2815 || use == lv_asm);
2816 /* Using this macro rather than (for example) arrays of messages
2817 ensures that all the format strings are checked at compile
2818 time. */
2819 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
2820 : (use == lv_increment ? (I) \
2821 : (use == lv_decrement ? (D) : (AS))))
2822 if (TREE_CODE (arg) == COMPONENT_REF)
2824 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2825 readonly_error (TREE_OPERAND (arg, 0), use);
2826 else
2827 error (READONLY_MSG (G_("assignment of read-only member %qs"),
2828 G_("increment of read-only member %qs"),
2829 G_("decrement of read-only member %qs"),
2830 G_("read-only member %qs used as %<asm%> output")),
2831 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2833 else if (TREE_CODE (arg) == VAR_DECL)
2834 error (READONLY_MSG (G_("assignment of read-only variable %qs"),
2835 G_("increment of read-only variable %qs"),
2836 G_("decrement of read-only variable %qs"),
2837 G_("read-only variable %qs used as %<asm%> output")),
2838 IDENTIFIER_POINTER (DECL_NAME (arg)));
2839 else
2840 error (READONLY_MSG (G_("assignment of read-only location"),
2841 G_("increment of read-only location"),
2842 G_("decrement of read-only location"),
2843 G_("read-only location used as %<asm%> output")));
2846 /* Mark EXP saying that we need to be able to take the
2847 address of it; it should not be allocated in a register.
2848 Returns true if successful. */
2850 bool
2851 c_mark_addressable (tree exp)
2853 tree x = exp;
2855 while (1)
2856 switch (TREE_CODE (x))
2858 case COMPONENT_REF:
2859 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2861 error
2862 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
2863 return false;
2866 /* ... fall through ... */
2868 case ADDR_EXPR:
2869 case ARRAY_REF:
2870 case REALPART_EXPR:
2871 case IMAGPART_EXPR:
2872 x = TREE_OPERAND (x, 0);
2873 break;
2875 case COMPOUND_LITERAL_EXPR:
2876 case CONSTRUCTOR:
2877 TREE_ADDRESSABLE (x) = 1;
2878 return true;
2880 case VAR_DECL:
2881 case CONST_DECL:
2882 case PARM_DECL:
2883 case RESULT_DECL:
2884 if (C_DECL_REGISTER (x)
2885 && DECL_NONLOCAL (x))
2887 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2889 error
2890 ("global register variable %qD used in nested function", x);
2891 return false;
2893 pedwarn ("register variable %qD used in nested function", x);
2895 else if (C_DECL_REGISTER (x))
2897 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2898 error ("address of global register variable %qD requested", x);
2899 else
2900 error ("address of register variable %qD requested", x);
2901 return false;
2904 /* drops in */
2905 case FUNCTION_DECL:
2906 TREE_ADDRESSABLE (x) = 1;
2907 /* drops out */
2908 default:
2909 return true;
2913 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2915 tree
2916 build_conditional_expr (tree ifexp, tree op1, tree op2)
2918 tree type1;
2919 tree type2;
2920 enum tree_code code1;
2921 enum tree_code code2;
2922 tree result_type = NULL;
2923 tree orig_op1 = op1, orig_op2 = op2;
2925 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2927 /* Promote both alternatives. */
2929 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2930 op1 = default_conversion (op1);
2931 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2932 op2 = default_conversion (op2);
2934 if (TREE_CODE (ifexp) == ERROR_MARK
2935 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2936 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2937 return error_mark_node;
2939 type1 = TREE_TYPE (op1);
2940 code1 = TREE_CODE (type1);
2941 type2 = TREE_TYPE (op2);
2942 code2 = TREE_CODE (type2);
2944 /* C90 does not permit non-lvalue arrays in conditional expressions.
2945 In C99 they will be pointers by now. */
2946 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2948 error ("non-lvalue array in conditional expression");
2949 return error_mark_node;
2952 /* Quickly detect the usual case where op1 and op2 have the same type
2953 after promotion. */
2954 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2956 if (type1 == type2)
2957 result_type = type1;
2958 else
2959 result_type = TYPE_MAIN_VARIANT (type1);
2961 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2962 || code1 == COMPLEX_TYPE)
2963 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2964 || code2 == COMPLEX_TYPE))
2966 result_type = c_common_type (type1, type2);
2968 /* If -Wsign-compare, warn here if type1 and type2 have
2969 different signedness. We'll promote the signed to unsigned
2970 and later code won't know it used to be different.
2971 Do this check on the original types, so that explicit casts
2972 will be considered, but default promotions won't. */
2973 if (warn_sign_compare && !skip_evaluation)
2975 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2976 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2978 if (unsigned_op1 ^ unsigned_op2)
2980 /* Do not warn if the result type is signed, since the
2981 signed type will only be chosen if it can represent
2982 all the values of the unsigned type. */
2983 if (!TYPE_UNSIGNED (result_type))
2984 /* OK */;
2985 /* Do not warn if the signed quantity is an unsuffixed
2986 integer literal (or some static constant expression
2987 involving such literals) and it is non-negative. */
2988 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
2989 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
2990 /* OK */;
2991 else
2992 warning ("signed and unsigned type in conditional expression");
2996 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2998 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2999 pedwarn ("ISO C forbids conditional expr with only one void side");
3000 result_type = void_type_node;
3002 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3004 if (comp_target_types (type1, type2, 1))
3005 result_type = common_pointer_type (type1, type2);
3006 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3007 && TREE_CODE (orig_op1) != NOP_EXPR)
3008 result_type = qualify_type (type2, type1);
3009 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3010 && TREE_CODE (orig_op2) != NOP_EXPR)
3011 result_type = qualify_type (type1, type2);
3012 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3014 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3015 pedwarn ("ISO C forbids conditional expr between "
3016 "%<void *%> and function pointer");
3017 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3018 TREE_TYPE (type2)));
3020 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3022 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3023 pedwarn ("ISO C forbids conditional expr between "
3024 "%<void *%> and function pointer");
3025 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3026 TREE_TYPE (type1)));
3028 else
3030 pedwarn ("pointer type mismatch in conditional expression");
3031 result_type = build_pointer_type (void_type_node);
3034 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3036 if (!integer_zerop (op2))
3037 pedwarn ("pointer/integer type mismatch in conditional expression");
3038 else
3040 op2 = null_pointer_node;
3042 result_type = type1;
3044 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3046 if (!integer_zerop (op1))
3047 pedwarn ("pointer/integer type mismatch in conditional expression");
3048 else
3050 op1 = null_pointer_node;
3052 result_type = type2;
3055 if (!result_type)
3057 if (flag_cond_mismatch)
3058 result_type = void_type_node;
3059 else
3061 error ("type mismatch in conditional expression");
3062 return error_mark_node;
3066 /* Merge const and volatile flags of the incoming types. */
3067 result_type
3068 = build_type_variant (result_type,
3069 TREE_READONLY (op1) || TREE_READONLY (op2),
3070 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3072 if (result_type != TREE_TYPE (op1))
3073 op1 = convert_and_check (result_type, op1);
3074 if (result_type != TREE_TYPE (op2))
3075 op2 = convert_and_check (result_type, op2);
3077 if (TREE_CODE (ifexp) == INTEGER_CST)
3078 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3080 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
3083 /* Return a compound expression that performs two expressions and
3084 returns the value of the second of them. */
3086 tree
3087 build_compound_expr (tree expr1, tree expr2)
3089 /* Convert arrays and functions to pointers. */
3090 expr2 = default_function_array_conversion (expr2);
3092 if (!TREE_SIDE_EFFECTS (expr1))
3094 /* The left-hand operand of a comma expression is like an expression
3095 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3096 any side-effects, unless it was explicitly cast to (void). */
3097 if (warn_unused_value)
3099 if (VOID_TYPE_P (TREE_TYPE (expr1))
3100 && TREE_CODE (expr1) == CONVERT_EXPR)
3101 ; /* (void) a, b */
3102 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3103 && TREE_CODE (expr1) == COMPOUND_EXPR
3104 && TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR)
3105 ; /* (void) a, (void) b, c */
3106 else
3107 warning ("left-hand operand of comma expression has no effect");
3111 /* With -Wunused, we should also warn if the left-hand operand does have
3112 side-effects, but computes a value which is not used. For example, in
3113 `foo() + bar(), baz()' the result of the `+' operator is not used,
3114 so we should issue a warning. */
3115 else if (warn_unused_value)
3116 warn_if_unused_value (expr1, input_location);
3118 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3121 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3123 tree
3124 build_c_cast (tree type, tree expr)
3126 tree value = expr;
3128 if (type == error_mark_node || expr == error_mark_node)
3129 return error_mark_node;
3131 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3132 only in <protocol> qualifications. But when constructing cast expressions,
3133 the protocols do matter and must be kept around. */
3134 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3135 return build1 (NOP_EXPR, type, expr);
3137 type = TYPE_MAIN_VARIANT (type);
3139 if (TREE_CODE (type) == ARRAY_TYPE)
3141 error ("cast specifies array type");
3142 return error_mark_node;
3145 if (TREE_CODE (type) == FUNCTION_TYPE)
3147 error ("cast specifies function type");
3148 return error_mark_node;
3151 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3153 if (pedantic)
3155 if (TREE_CODE (type) == RECORD_TYPE
3156 || TREE_CODE (type) == UNION_TYPE)
3157 pedwarn ("ISO C forbids casting nonscalar to the same type");
3160 else if (TREE_CODE (type) == UNION_TYPE)
3162 tree field;
3163 value = default_function_array_conversion (value);
3165 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3166 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3167 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3168 break;
3170 if (field)
3172 tree t;
3174 if (pedantic)
3175 pedwarn ("ISO C forbids casts to union type");
3176 t = digest_init (type,
3177 build_constructor (type,
3178 build_tree_list (field, value)),
3179 true, 0);
3180 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3181 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3182 return t;
3184 error ("cast to union type from type not present in union");
3185 return error_mark_node;
3187 else
3189 tree otype, ovalue;
3191 /* If casting to void, avoid the error that would come
3192 from default_conversion in the case of a non-lvalue array. */
3193 if (type == void_type_node)
3194 return build1 (CONVERT_EXPR, type, value);
3196 /* Convert functions and arrays to pointers,
3197 but don't convert any other types. */
3198 value = default_function_array_conversion (value);
3199 otype = TREE_TYPE (value);
3201 /* Optionally warn about potentially worrisome casts. */
3203 if (warn_cast_qual
3204 && TREE_CODE (type) == POINTER_TYPE
3205 && TREE_CODE (otype) == POINTER_TYPE)
3207 tree in_type = type;
3208 tree in_otype = otype;
3209 int added = 0;
3210 int discarded = 0;
3212 /* Check that the qualifiers on IN_TYPE are a superset of
3213 the qualifiers of IN_OTYPE. The outermost level of
3214 POINTER_TYPE nodes is uninteresting and we stop as soon
3215 as we hit a non-POINTER_TYPE node on either type. */
3218 in_otype = TREE_TYPE (in_otype);
3219 in_type = TREE_TYPE (in_type);
3221 /* GNU C allows cv-qualified function types. 'const'
3222 means the function is very pure, 'volatile' means it
3223 can't return. We need to warn when such qualifiers
3224 are added, not when they're taken away. */
3225 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3226 && TREE_CODE (in_type) == FUNCTION_TYPE)
3227 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3228 else
3229 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3231 while (TREE_CODE (in_type) == POINTER_TYPE
3232 && TREE_CODE (in_otype) == POINTER_TYPE);
3234 if (added)
3235 warning ("cast adds new qualifiers to function type");
3237 if (discarded)
3238 /* There are qualifiers present in IN_OTYPE that are not
3239 present in IN_TYPE. */
3240 warning ("cast discards qualifiers from pointer target type");
3243 /* Warn about possible alignment problems. */
3244 if (STRICT_ALIGNMENT && warn_cast_align
3245 && TREE_CODE (type) == POINTER_TYPE
3246 && TREE_CODE (otype) == POINTER_TYPE
3247 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3248 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3249 /* Don't warn about opaque types, where the actual alignment
3250 restriction is unknown. */
3251 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3252 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3253 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3254 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3255 warning ("cast increases required alignment of target type");
3257 if (TREE_CODE (type) == INTEGER_TYPE
3258 && TREE_CODE (otype) == POINTER_TYPE
3259 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3260 && !TREE_CONSTANT (value))
3261 warning ("cast from pointer to integer of different size");
3263 if (warn_bad_function_cast
3264 && TREE_CODE (value) == CALL_EXPR
3265 && TREE_CODE (type) != TREE_CODE (otype))
3266 warning ("cast from function call of type %qT to non-matching "
3267 "type %qT", otype, type);
3269 if (TREE_CODE (type) == POINTER_TYPE
3270 && TREE_CODE (otype) == INTEGER_TYPE
3271 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3272 /* Don't warn about converting any constant. */
3273 && !TREE_CONSTANT (value))
3274 warning ("cast to pointer from integer of different size");
3276 if (TREE_CODE (type) == POINTER_TYPE
3277 && TREE_CODE (otype) == POINTER_TYPE
3278 && TREE_CODE (expr) == ADDR_EXPR
3279 && DECL_P (TREE_OPERAND (expr, 0))
3280 && flag_strict_aliasing && warn_strict_aliasing
3281 && !VOID_TYPE_P (TREE_TYPE (type)))
3283 /* Casting the address of a decl to non void pointer. Warn
3284 if the cast breaks type based aliasing. */
3285 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3286 warning ("type-punning to incomplete type might break strict-aliasing rules");
3287 else
3289 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3290 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3292 if (!alias_sets_conflict_p (set1, set2))
3293 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3294 else if (warn_strict_aliasing > 1
3295 && !alias_sets_might_conflict_p (set1, set2))
3296 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3300 /* If pedantic, warn for conversions between function and object
3301 pointer types, except for converting a null pointer constant
3302 to function pointer type. */
3303 if (pedantic
3304 && TREE_CODE (type) == POINTER_TYPE
3305 && TREE_CODE (otype) == POINTER_TYPE
3306 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3307 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3308 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3310 if (pedantic
3311 && TREE_CODE (type) == POINTER_TYPE
3312 && TREE_CODE (otype) == POINTER_TYPE
3313 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3314 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3315 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3316 && TREE_CODE (expr) != NOP_EXPR))
3317 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3319 ovalue = value;
3320 value = convert (type, value);
3322 /* Ignore any integer overflow caused by the cast. */
3323 if (TREE_CODE (value) == INTEGER_CST)
3325 /* The code here is quite complicated. To fix PR/24599, we have
3326 to 1) copy VALUE if it will have TREE_CONSTANT_OVERFLOW or
3327 TREE_OVERFLOW set, 2) use the shared constant it if it will have
3328 the flags reset. To mimic exactly what was happening before
3329 when the code was mucking with the bits directly, the resulting
3330 conditions are quite involuted. The code in 4.1 and later is much
3331 simpler (Nov 7 2005). */
3333 if (TREE_OVERFLOW (ovalue) && EXPR_P (ovalue))
3335 /* Copy TREE_OVERFLOW from OVALUE if it was an expression.
3336 Avoid clobbering a shared constant. */
3337 value = copy_node (value);
3338 TREE_OVERFLOW (value) = true;
3340 else if (CONSTANT_CLASS_P (ovalue) && TREE_CONSTANT_OVERFLOW (ovalue))
3342 /* When casting a constant, always reset TREE_OVERFLOW, but
3343 still copy TREE_CONSTANT_OVERFLOW from OVALUE. */
3344 value = copy_node (value);
3345 TREE_OVERFLOW (value) = false;
3346 TREE_CONSTANT_OVERFLOW (value) = true;
3348 else if (TREE_CONSTANT_OVERFLOW (value)
3349 ? /* If TREE_CONSTANT_OVERFLOW is true and OVALUE was
3350 constant, it was not set in OVALUE and we should reset
3351 it in VALUE too. And for constant OVALUE, we should
3352 reset TREE_OVERFLOW as well. */
3353 CONSTANT_CLASS_P (ovalue)
3354 : /* TREE_CONSTANT_OVERFLOW is not set, we only care about
3355 TREE_OVERFLOW. If it is true and we arrived here,
3356 OVALUE is not an expression and we reset the flag. */
3357 TREE_OVERFLOW (value))
3358 /* After resetting TREE_OVERFLOW both flags are reset, ensure
3359 constant sharing. */
3360 value = build_int_cst_wide (TREE_TYPE (value),
3361 TREE_INT_CST_LOW (value),
3362 TREE_INT_CST_HIGH (value));
3366 /* Don't let a cast be an lvalue. */
3367 if (value == expr)
3368 value = non_lvalue (value);
3370 return value;
3373 /* Interpret a cast of expression EXPR to type TYPE. */
3374 tree
3375 c_cast_expr (struct c_type_name *type_name, tree expr)
3377 tree type;
3378 int saved_wsp = warn_strict_prototypes;
3380 /* This avoids warnings about unprototyped casts on
3381 integers. E.g. "#define SIG_DFL (void(*)())0". */
3382 if (TREE_CODE (expr) == INTEGER_CST)
3383 warn_strict_prototypes = 0;
3384 type = groktypename (type_name);
3385 warn_strict_prototypes = saved_wsp;
3387 return build_c_cast (type, expr);
3391 /* Build an assignment expression of lvalue LHS from value RHS.
3392 MODIFYCODE is the code for a binary operator that we use
3393 to combine the old value of LHS with RHS to get the new value.
3394 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3396 tree
3397 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3399 tree result;
3400 tree newrhs;
3401 tree lhstype = TREE_TYPE (lhs);
3402 tree olhstype = lhstype;
3404 /* Types that aren't fully specified cannot be used in assignments. */
3405 lhs = require_complete_type (lhs);
3407 /* Avoid duplicate error messages from operands that had errors. */
3408 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3409 return error_mark_node;
3411 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3412 /* Do not use STRIP_NOPS here. We do not want an enumerator
3413 whose value is 0 to count as a null pointer constant. */
3414 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3415 rhs = TREE_OPERAND (rhs, 0);
3417 newrhs = rhs;
3419 /* If a binary op has been requested, combine the old LHS value with the RHS
3420 producing the value we should actually store into the LHS. */
3422 if (modifycode != NOP_EXPR)
3424 lhs = stabilize_reference (lhs);
3425 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3428 if (!lvalue_or_else (lhs, lv_assign))
3429 return error_mark_node;
3431 /* Give an error for storing in something that is 'const'. */
3433 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3434 || ((TREE_CODE (lhstype) == RECORD_TYPE
3435 || TREE_CODE (lhstype) == UNION_TYPE)
3436 && C_TYPE_FIELDS_READONLY (lhstype)))
3437 readonly_error (lhs, lv_assign);
3439 /* If storing into a structure or union member,
3440 it has probably been given type `int'.
3441 Compute the type that would go with
3442 the actual amount of storage the member occupies. */
3444 if (TREE_CODE (lhs) == COMPONENT_REF
3445 && (TREE_CODE (lhstype) == INTEGER_TYPE
3446 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3447 || TREE_CODE (lhstype) == REAL_TYPE
3448 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3449 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3451 /* If storing in a field that is in actuality a short or narrower than one,
3452 we must store in the field in its actual type. */
3454 if (lhstype != TREE_TYPE (lhs))
3456 lhs = copy_node (lhs);
3457 TREE_TYPE (lhs) = lhstype;
3460 /* Convert new value to destination type. */
3462 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3463 NULL_TREE, NULL_TREE, 0);
3464 if (TREE_CODE (newrhs) == ERROR_MARK)
3465 return error_mark_node;
3467 /* Scan operands. */
3469 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3470 TREE_SIDE_EFFECTS (result) = 1;
3472 /* If we got the LHS in a different type for storing in,
3473 convert the result back to the nominal type of LHS
3474 so that the value we return always has the same type
3475 as the LHS argument. */
3477 if (olhstype == TREE_TYPE (result))
3478 return result;
3479 return convert_for_assignment (olhstype, result, ic_assign,
3480 NULL_TREE, NULL_TREE, 0);
3483 /* Convert value RHS to type TYPE as preparation for an assignment
3484 to an lvalue of type TYPE.
3485 The real work of conversion is done by `convert'.
3486 The purpose of this function is to generate error messages
3487 for assignments that are not allowed in C.
3488 ERRTYPE says whether it is argument passing, assignment,
3489 initialization or return.
3491 FUNCTION is a tree for the function being called.
3492 PARMNUM is the number of the argument, for printing in error messages. */
3494 static tree
3495 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3496 tree fundecl, tree function, int parmnum)
3498 enum tree_code codel = TREE_CODE (type);
3499 tree rhstype;
3500 enum tree_code coder;
3501 tree rname = NULL_TREE;
3503 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3505 tree selector;
3506 /* Change pointer to function to the function itself for
3507 diagnostics. */
3508 if (TREE_CODE (function) == ADDR_EXPR
3509 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3510 function = TREE_OPERAND (function, 0);
3512 /* Handle an ObjC selector specially for diagnostics. */
3513 selector = objc_message_selector ();
3514 rname = function;
3515 if (selector && parmnum > 2)
3517 rname = selector;
3518 parmnum -= 2;
3522 /* This macro is used to emit diagnostics to ensure that all format
3523 strings are complete sentences, visible to gettext and checked at
3524 compile time. */
3525 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3526 do { \
3527 switch (errtype) \
3529 case ic_argpass: \
3530 pedwarn (AR, parmnum, rname); \
3531 break; \
3532 case ic_argpass_nonproto: \
3533 warning (AR, parmnum, rname); \
3534 break; \
3535 case ic_assign: \
3536 pedwarn (AS); \
3537 break; \
3538 case ic_init: \
3539 pedwarn (IN); \
3540 break; \
3541 case ic_return: \
3542 pedwarn (RE); \
3543 break; \
3544 default: \
3545 gcc_unreachable (); \
3547 } while (0)
3549 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3550 /* Do not use STRIP_NOPS here. We do not want an enumerator
3551 whose value is 0 to count as a null pointer constant. */
3552 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3553 rhs = TREE_OPERAND (rhs, 0);
3555 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3556 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3557 rhs = default_conversion (rhs);
3558 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3559 rhs = decl_constant_value_for_broken_optimization (rhs);
3561 rhstype = TREE_TYPE (rhs);
3562 coder = TREE_CODE (rhstype);
3564 if (coder == ERROR_MARK)
3565 return error_mark_node;
3567 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3569 overflow_warning (rhs);
3570 /* Check for Objective-C protocols. This will automatically
3571 issue a warning if there are protocol violations. No need to
3572 use the return value. */
3573 if (c_dialect_objc ())
3574 objc_comptypes (type, rhstype, 0);
3575 return rhs;
3578 if (coder == VOID_TYPE)
3580 /* Except for passing an argument to an unprototyped function,
3581 this is a constraint violation. When passing an argument to
3582 an unprototyped function, it is compile-time undefined;
3583 making it a constraint in that case was rejected in
3584 DR#252. */
3585 error ("void value not ignored as it ought to be");
3586 return error_mark_node;
3588 /* A type converts to a reference to it.
3589 This code doesn't fully support references, it's just for the
3590 special case of va_start and va_copy. */
3591 if (codel == REFERENCE_TYPE
3592 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3594 if (!lvalue_p (rhs))
3596 error ("cannot pass rvalue to reference parameter");
3597 return error_mark_node;
3599 if (!c_mark_addressable (rhs))
3600 return error_mark_node;
3601 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3603 /* We already know that these two types are compatible, but they
3604 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3605 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3606 likely to be va_list, a typedef to __builtin_va_list, which
3607 is different enough that it will cause problems later. */
3608 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3609 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3611 rhs = build1 (NOP_EXPR, type, rhs);
3612 return rhs;
3614 /* Some types can interconvert without explicit casts. */
3615 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3616 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3617 return convert (type, rhs);
3618 /* Arithmetic types all interconvert, and enum is treated like int. */
3619 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3620 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3621 || codel == BOOLEAN_TYPE)
3622 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3623 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3624 || coder == BOOLEAN_TYPE))
3625 return convert_and_check (type, rhs);
3627 /* Conversion to a transparent union from its member types.
3628 This applies only to function arguments. */
3629 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3630 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3632 tree memb_types;
3633 tree marginal_memb_type = 0;
3635 for (memb_types = TYPE_FIELDS (type); memb_types;
3636 memb_types = TREE_CHAIN (memb_types))
3638 tree memb_type = TREE_TYPE (memb_types);
3640 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3641 TYPE_MAIN_VARIANT (rhstype)))
3642 break;
3644 if (TREE_CODE (memb_type) != POINTER_TYPE)
3645 continue;
3647 if (coder == POINTER_TYPE)
3649 tree ttl = TREE_TYPE (memb_type);
3650 tree ttr = TREE_TYPE (rhstype);
3652 /* Any non-function converts to a [const][volatile] void *
3653 and vice versa; otherwise, targets must be the same.
3654 Meanwhile, the lhs target must have all the qualifiers of
3655 the rhs. */
3656 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3657 || comp_target_types (memb_type, rhstype, 0))
3659 /* If this type won't generate any warnings, use it. */
3660 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3661 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3662 && TREE_CODE (ttl) == FUNCTION_TYPE)
3663 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3664 == TYPE_QUALS (ttr))
3665 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3666 == TYPE_QUALS (ttl))))
3667 break;
3669 /* Keep looking for a better type, but remember this one. */
3670 if (!marginal_memb_type)
3671 marginal_memb_type = memb_type;
3675 /* Can convert integer zero to any pointer type. */
3676 if (integer_zerop (rhs)
3677 || (TREE_CODE (rhs) == NOP_EXPR
3678 && integer_zerop (TREE_OPERAND (rhs, 0))))
3680 rhs = null_pointer_node;
3681 break;
3685 if (memb_types || marginal_memb_type)
3687 if (!memb_types)
3689 /* We have only a marginally acceptable member type;
3690 it needs a warning. */
3691 tree ttl = TREE_TYPE (marginal_memb_type);
3692 tree ttr = TREE_TYPE (rhstype);
3694 /* Const and volatile mean something different for function
3695 types, so the usual warnings are not appropriate. */
3696 if (TREE_CODE (ttr) == FUNCTION_TYPE
3697 && TREE_CODE (ttl) == FUNCTION_TYPE)
3699 /* Because const and volatile on functions are
3700 restrictions that say the function will not do
3701 certain things, it is okay to use a const or volatile
3702 function where an ordinary one is wanted, but not
3703 vice-versa. */
3704 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3705 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE "
3706 "makes qualified function "
3707 "pointer from unqualified"),
3708 G_("assignment makes qualified "
3709 "function pointer from "
3710 "unqualified"),
3711 G_("initialization makes qualified "
3712 "function pointer from "
3713 "unqualified"),
3714 G_("return makes qualified function "
3715 "pointer from unqualified"));
3717 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3718 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
3719 "qualifiers from pointer target type"),
3720 G_("assignment discards qualifiers "
3721 "from pointer target type"),
3722 G_("initialization discards qualifiers "
3723 "from pointer target type"),
3724 G_("return discards qualifiers from "
3725 "pointer target type"));
3728 if (pedantic && !DECL_IN_SYSTEM_HEADER (fundecl))
3729 pedwarn ("ISO C prohibits argument conversion to union type");
3731 return build1 (NOP_EXPR, type, rhs);
3735 /* Conversions among pointers */
3736 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3737 && (coder == codel))
3739 tree ttl = TREE_TYPE (type);
3740 tree ttr = TREE_TYPE (rhstype);
3741 tree mvl = ttl;
3742 tree mvr = ttr;
3743 bool is_opaque_pointer;
3744 int target_cmp = 0; /* Cache comp_target_types () result. */
3746 if (TREE_CODE (mvl) != ARRAY_TYPE)
3747 mvl = TYPE_MAIN_VARIANT (mvl);
3748 if (TREE_CODE (mvr) != ARRAY_TYPE)
3749 mvr = TYPE_MAIN_VARIANT (mvr);
3750 /* Opaque pointers are treated like void pointers. */
3751 is_opaque_pointer = (targetm.vector_opaque_p (type)
3752 || targetm.vector_opaque_p (rhstype))
3753 && TREE_CODE (ttl) == VECTOR_TYPE
3754 && TREE_CODE (ttr) == VECTOR_TYPE;
3756 /* Any non-function converts to a [const][volatile] void *
3757 and vice versa; otherwise, targets must be the same.
3758 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3759 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3760 || (target_cmp = comp_target_types (type, rhstype, 0))
3761 || is_opaque_pointer
3762 || (c_common_unsigned_type (mvl)
3763 == c_common_unsigned_type (mvr)))
3765 if (pedantic
3766 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3768 (VOID_TYPE_P (ttr)
3769 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3770 which are not ANSI null ptr constants. */
3771 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3772 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3773 WARN_FOR_ASSIGNMENT (G_("ISO C forbids passing argument %d of "
3774 "%qE between function pointer "
3775 "and %<void *%>"),
3776 G_("ISO C forbids assignment between "
3777 "function pointer and %<void *%>"),
3778 G_("ISO C forbids initialization between "
3779 "function pointer and %<void *%>"),
3780 G_("ISO C forbids return between function "
3781 "pointer and %<void *%>"));
3782 /* Const and volatile mean something different for function types,
3783 so the usual warnings are not appropriate. */
3784 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3785 && TREE_CODE (ttl) != FUNCTION_TYPE)
3787 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3788 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE discards "
3789 "qualifiers from pointer target type"),
3790 G_("assignment discards qualifiers "
3791 "from pointer target type"),
3792 G_("initialization discards qualifiers "
3793 "from pointer target type"),
3794 G_("return discards qualifiers from "
3795 "pointer target type"));
3796 /* If this is not a case of ignoring a mismatch in signedness,
3797 no warning. */
3798 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3799 || target_cmp)
3801 /* If there is a mismatch, do warn. */
3802 else if (warn_pointer_sign)
3803 WARN_FOR_ASSIGNMENT (G_("pointer targets in passing argument "
3804 "%d of %qE differ in signedness"),
3805 G_("pointer targets in assignment "
3806 "differ in signedness"),
3807 G_("pointer targets in initialization "
3808 "differ in signedness"),
3809 G_("pointer targets in return differ "
3810 "in signedness"));
3812 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3813 && TREE_CODE (ttr) == FUNCTION_TYPE)
3815 /* Because const and volatile on functions are restrictions
3816 that say the function will not do certain things,
3817 it is okay to use a const or volatile function
3818 where an ordinary one is wanted, but not vice-versa. */
3819 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3820 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
3821 "qualified function pointer "
3822 "from unqualified"),
3823 G_("assignment makes qualified function "
3824 "pointer from unqualified"),
3825 G_("initialization makes qualified "
3826 "function pointer from unqualified"),
3827 G_("return makes qualified function "
3828 "pointer from unqualified"));
3831 else
3832 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE from "
3833 "incompatible pointer type"),
3834 G_("assignment from incompatible pointer type"),
3835 G_("initialization from incompatible "
3836 "pointer type"),
3837 G_("return from incompatible pointer type"));
3838 return convert (type, rhs);
3840 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3842 /* ??? This should not be an error when inlining calls to
3843 unprototyped functions. */
3844 error ("invalid use of non-lvalue array");
3845 return error_mark_node;
3847 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3849 /* An explicit constant 0 can convert to a pointer,
3850 or one that results from arithmetic, even including
3851 a cast to integer type. */
3852 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3854 !(TREE_CODE (rhs) == NOP_EXPR
3855 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3856 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3857 && integer_zerop (TREE_OPERAND (rhs, 0))))
3858 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes "
3859 "pointer from integer without a cast"),
3860 G_("assignment makes pointer from integer "
3861 "without a cast"),
3862 G_("initialization makes pointer from "
3863 "integer without a cast"),
3864 G_("return makes pointer from integer "
3865 "without a cast"));
3867 return convert (type, rhs);
3869 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3871 WARN_FOR_ASSIGNMENT (G_("passing argument %d of %qE makes integer "
3872 "from pointer without a cast"),
3873 G_("assignment makes integer from pointer "
3874 "without a cast"),
3875 G_("initialization makes integer from pointer "
3876 "without a cast"),
3877 G_("return makes integer from pointer "
3878 "without a cast"));
3879 return convert (type, rhs);
3881 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3882 return convert (type, rhs);
3884 switch (errtype)
3886 case ic_argpass:
3887 case ic_argpass_nonproto:
3888 /* ??? This should not be an error when inlining calls to
3889 unprototyped functions. */
3890 error ("incompatible type for argument %d of %qE", parmnum, rname);
3891 break;
3892 case ic_assign:
3893 error ("incompatible types in assignment");
3894 break;
3895 case ic_init:
3896 error ("incompatible types in initialization");
3897 break;
3898 case ic_return:
3899 error ("incompatible types in return");
3900 break;
3901 default:
3902 gcc_unreachable ();
3905 return error_mark_node;
3908 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3909 is used for error and waring reporting and indicates which argument
3910 is being processed. */
3912 tree
3913 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3915 tree ret, type;
3917 /* If FN was prototyped, the value has been converted already
3918 in convert_arguments. */
3919 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3920 return value;
3922 type = TREE_TYPE (parm);
3923 ret = convert_for_assignment (type, value,
3924 ic_argpass_nonproto, fn,
3925 fn, argnum);
3926 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3927 && INTEGRAL_TYPE_P (type)
3928 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3929 ret = default_conversion (ret);
3930 return ret;
3933 /* If VALUE is a compound expr all of whose expressions are constant, then
3934 return its value. Otherwise, return error_mark_node.
3936 This is for handling COMPOUND_EXPRs as initializer elements
3937 which is allowed with a warning when -pedantic is specified. */
3939 static tree
3940 valid_compound_expr_initializer (tree value, tree endtype)
3942 if (TREE_CODE (value) == COMPOUND_EXPR)
3944 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3945 == error_mark_node)
3946 return error_mark_node;
3947 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3948 endtype);
3950 else if (!initializer_constant_valid_p (value, endtype))
3951 return error_mark_node;
3952 else
3953 return value;
3956 /* Perform appropriate conversions on the initial value of a variable,
3957 store it in the declaration DECL,
3958 and print any error messages that are appropriate.
3959 If the init is invalid, store an ERROR_MARK. */
3961 void
3962 store_init_value (tree decl, tree init)
3964 tree value, type;
3966 /* If variable's type was invalidly declared, just ignore it. */
3968 type = TREE_TYPE (decl);
3969 if (TREE_CODE (type) == ERROR_MARK)
3970 return;
3972 /* Digest the specified initializer into an expression. */
3974 value = digest_init (type, init, true, TREE_STATIC (decl));
3976 /* Store the expression if valid; else report error. */
3978 if (warn_traditional && !in_system_header
3979 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3980 warning ("traditional C rejects automatic aggregate initialization");
3982 DECL_INITIAL (decl) = value;
3984 /* ANSI wants warnings about out-of-range constant initializers. */
3985 STRIP_TYPE_NOPS (value);
3986 constant_expression_warning (value);
3988 /* Check if we need to set array size from compound literal size. */
3989 if (TREE_CODE (type) == ARRAY_TYPE
3990 && TYPE_DOMAIN (type) == 0
3991 && value != error_mark_node)
3993 tree inside_init = init;
3995 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3996 inside_init = TREE_OPERAND (init, 0);
3997 inside_init = fold (inside_init);
3999 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4001 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4003 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4005 /* For int foo[] = (int [3]){1}; we need to set array size
4006 now since later on array initializer will be just the
4007 brace enclosed list of the compound literal. */
4008 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4009 layout_type (type);
4010 layout_decl (decl, 0);
4016 /* Methods for storing and printing names for error messages. */
4018 /* Implement a spelling stack that allows components of a name to be pushed
4019 and popped. Each element on the stack is this structure. */
4021 struct spelling
4023 int kind;
4024 union
4026 unsigned HOST_WIDE_INT i;
4027 const char *s;
4028 } u;
4031 #define SPELLING_STRING 1
4032 #define SPELLING_MEMBER 2
4033 #define SPELLING_BOUNDS 3
4035 static struct spelling *spelling; /* Next stack element (unused). */
4036 static struct spelling *spelling_base; /* Spelling stack base. */
4037 static int spelling_size; /* Size of the spelling stack. */
4039 /* Macros to save and restore the spelling stack around push_... functions.
4040 Alternative to SAVE_SPELLING_STACK. */
4042 #define SPELLING_DEPTH() (spelling - spelling_base)
4043 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4045 /* Push an element on the spelling stack with type KIND and assign VALUE
4046 to MEMBER. */
4048 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4050 int depth = SPELLING_DEPTH (); \
4052 if (depth >= spelling_size) \
4054 spelling_size += 10; \
4055 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4056 spelling_size); \
4057 RESTORE_SPELLING_DEPTH (depth); \
4060 spelling->kind = (KIND); \
4061 spelling->MEMBER = (VALUE); \
4062 spelling++; \
4065 /* Push STRING on the stack. Printed literally. */
4067 static void
4068 push_string (const char *string)
4070 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4073 /* Push a member name on the stack. Printed as '.' STRING. */
4075 static void
4076 push_member_name (tree decl)
4078 const char *const string
4079 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4080 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4083 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4085 static void
4086 push_array_bounds (unsigned HOST_WIDE_INT bounds)
4088 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4091 /* Compute the maximum size in bytes of the printed spelling. */
4093 static int
4094 spelling_length (void)
4096 int size = 0;
4097 struct spelling *p;
4099 for (p = spelling_base; p < spelling; p++)
4101 if (p->kind == SPELLING_BOUNDS)
4102 size += 25;
4103 else
4104 size += strlen (p->u.s) + 1;
4107 return size;
4110 /* Print the spelling to BUFFER and return it. */
4112 static char *
4113 print_spelling (char *buffer)
4115 char *d = buffer;
4116 struct spelling *p;
4118 for (p = spelling_base; p < spelling; p++)
4119 if (p->kind == SPELLING_BOUNDS)
4121 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
4122 d += strlen (d);
4124 else
4126 const char *s;
4127 if (p->kind == SPELLING_MEMBER)
4128 *d++ = '.';
4129 for (s = p->u.s; (*d = *s++); d++)
4132 *d++ = '\0';
4133 return buffer;
4136 /* Issue an error message for a bad initializer component.
4137 MSGID identifies the message.
4138 The component name is taken from the spelling stack. */
4140 void
4141 error_init (const char *msgid)
4143 char *ofwhat;
4145 error ("%s", _(msgid));
4146 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4147 if (*ofwhat)
4148 error ("(near initialization for %qs)", ofwhat);
4151 /* Issue a pedantic warning for a bad initializer component.
4152 MSGID identifies the message.
4153 The component name is taken from the spelling stack. */
4155 void
4156 pedwarn_init (const char *msgid)
4158 char *ofwhat;
4160 pedwarn ("%s", _(msgid));
4161 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4162 if (*ofwhat)
4163 pedwarn ("(near initialization for %qs)", ofwhat);
4166 /* Issue a warning for a bad initializer component.
4167 MSGID identifies the message.
4168 The component name is taken from the spelling stack. */
4170 static void
4171 warning_init (const char *msgid)
4173 char *ofwhat;
4175 warning ("%s", _(msgid));
4176 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4177 if (*ofwhat)
4178 warning ("(near initialization for %qs)", ofwhat);
4181 /* If TYPE is an array type and EXPR is a parenthesized string
4182 constant, warn if pedantic that EXPR is being used to initialize an
4183 object of type TYPE. */
4185 void
4186 maybe_warn_string_init (tree type, struct c_expr expr)
4188 if (pedantic
4189 && TREE_CODE (type) == ARRAY_TYPE
4190 && TREE_CODE (expr.value) == STRING_CST
4191 && expr.original_code != STRING_CST)
4192 pedwarn_init ("array initialized from parenthesized string constant");
4195 /* Digest the parser output INIT as an initializer for type TYPE.
4196 Return a C expression of type TYPE to represent the initial value.
4198 If INIT is a string constant, STRICT_STRING is true if it is
4199 unparenthesized or we should not warn here for it being parenthesized.
4200 For other types of INIT, STRICT_STRING is not used.
4202 REQUIRE_CONSTANT requests an error if non-constant initializers or
4203 elements are seen. */
4205 static tree
4206 digest_init (tree type, tree init, bool strict_string, int require_constant)
4208 enum tree_code code = TREE_CODE (type);
4209 tree inside_init = init;
4211 if (type == error_mark_node
4212 || init == error_mark_node
4213 || TREE_TYPE (init) == error_mark_node)
4214 return error_mark_node;
4216 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4217 /* Do not use STRIP_NOPS here. We do not want an enumerator
4218 whose value is 0 to count as a null pointer constant. */
4219 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4220 inside_init = TREE_OPERAND (init, 0);
4222 inside_init = fold (inside_init);
4224 /* Initialization of an array of chars from a string constant
4225 optionally enclosed in braces. */
4227 if (code == ARRAY_TYPE && inside_init
4228 && TREE_CODE (inside_init) == STRING_CST)
4230 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4231 /* Note that an array could be both an array of character type
4232 and an array of wchar_t if wchar_t is signed char or unsigned
4233 char. */
4234 bool char_array = (typ1 == char_type_node
4235 || typ1 == signed_char_type_node
4236 || typ1 == unsigned_char_type_node);
4237 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4238 if (char_array || wchar_array)
4240 struct c_expr expr;
4241 bool char_string;
4242 expr.value = inside_init;
4243 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4244 maybe_warn_string_init (type, expr);
4246 char_string
4247 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4248 == char_type_node);
4250 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4251 TYPE_MAIN_VARIANT (type)))
4252 return inside_init;
4254 if (!wchar_array && !char_string)
4256 error_init ("char-array initialized from wide string");
4257 return error_mark_node;
4259 if (char_string && !char_array)
4261 error_init ("wchar_t-array initialized from non-wide string");
4262 return error_mark_node;
4265 TREE_TYPE (inside_init) = type;
4266 if (TYPE_DOMAIN (type) != 0
4267 && TYPE_SIZE (type) != 0
4268 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4269 /* Subtract 1 (or sizeof (wchar_t))
4270 because it's ok to ignore the terminating null char
4271 that is counted in the length of the constant. */
4272 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4273 TREE_STRING_LENGTH (inside_init)
4274 - ((TYPE_PRECISION (typ1)
4275 != TYPE_PRECISION (char_type_node))
4276 ? (TYPE_PRECISION (wchar_type_node)
4277 / BITS_PER_UNIT)
4278 : 1)))
4279 pedwarn_init ("initializer-string for array of chars is too long");
4281 return inside_init;
4283 else if (INTEGRAL_TYPE_P (typ1))
4285 error_init ("array of inappropriate type initialized "
4286 "from string constant");
4287 return error_mark_node;
4291 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4292 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4293 below and handle as a constructor. */
4294 if (code == VECTOR_TYPE
4295 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4296 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4297 && TREE_CONSTANT (inside_init))
4299 if (TREE_CODE (inside_init) == VECTOR_CST
4300 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4301 TYPE_MAIN_VARIANT (type)))
4302 return inside_init;
4304 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4306 tree link;
4308 /* Iterate through elements and check if all constructor
4309 elements are *_CSTs. */
4310 for (link = CONSTRUCTOR_ELTS (inside_init);
4311 link;
4312 link = TREE_CHAIN (link))
4313 if (! CONSTANT_CLASS_P (TREE_VALUE (link)))
4314 break;
4316 if (link == NULL)
4317 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4321 /* Any type can be initialized
4322 from an expression of the same type, optionally with braces. */
4324 if (inside_init && TREE_TYPE (inside_init) != 0
4325 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4326 TYPE_MAIN_VARIANT (type))
4327 || (code == ARRAY_TYPE
4328 && comptypes (TREE_TYPE (inside_init), type))
4329 || (code == VECTOR_TYPE
4330 && comptypes (TREE_TYPE (inside_init), type))
4331 || (code == POINTER_TYPE
4332 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4333 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4334 TREE_TYPE (type)))
4335 || (code == POINTER_TYPE
4336 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4337 && comptypes (TREE_TYPE (inside_init),
4338 TREE_TYPE (type)))))
4340 if (code == POINTER_TYPE)
4342 inside_init = default_function_array_conversion (inside_init);
4344 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4346 error_init ("invalid use of non-lvalue array");
4347 return error_mark_node;
4351 if (code == VECTOR_TYPE)
4352 /* Although the types are compatible, we may require a
4353 conversion. */
4354 inside_init = convert (type, inside_init);
4356 if (require_constant
4357 && (code == VECTOR_TYPE || !flag_isoc99)
4358 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4360 /* As an extension, allow initializing objects with static storage
4361 duration with compound literals (which are then treated just as
4362 the brace enclosed list they contain). Also allow this for
4363 vectors, as we can only assign them with compound literals. */
4364 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4365 inside_init = DECL_INITIAL (decl);
4368 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4369 && TREE_CODE (inside_init) != CONSTRUCTOR)
4371 error_init ("array initialized from non-constant array expression");
4372 return error_mark_node;
4375 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4376 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4378 /* Compound expressions can only occur here if -pedantic or
4379 -pedantic-errors is specified. In the later case, we always want
4380 an error. In the former case, we simply want a warning. */
4381 if (require_constant && pedantic
4382 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4384 inside_init
4385 = valid_compound_expr_initializer (inside_init,
4386 TREE_TYPE (inside_init));
4387 if (inside_init == error_mark_node)
4388 error_init ("initializer element is not constant");
4389 else
4390 pedwarn_init ("initializer element is not constant");
4391 if (flag_pedantic_errors)
4392 inside_init = error_mark_node;
4394 else if (require_constant
4395 && !initializer_constant_valid_p (inside_init,
4396 TREE_TYPE (inside_init)))
4398 error_init ("initializer element is not constant");
4399 inside_init = error_mark_node;
4402 return inside_init;
4405 /* Handle scalar types, including conversions. */
4407 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4408 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4409 || code == VECTOR_TYPE)
4411 /* Note that convert_for_assignment calls default_conversion
4412 for arrays and functions. We must not call it in the
4413 case where inside_init is a null pointer constant. */
4414 inside_init
4415 = convert_for_assignment (type, init, ic_init,
4416 NULL_TREE, NULL_TREE, 0);
4418 /* Check to see if we have already given an error message. */
4419 if (inside_init == error_mark_node)
4421 else if (require_constant && !TREE_CONSTANT (inside_init))
4423 error_init ("initializer element is not constant");
4424 inside_init = error_mark_node;
4426 else if (require_constant
4427 && !initializer_constant_valid_p (inside_init,
4428 TREE_TYPE (inside_init)))
4430 error_init ("initializer element is not computable at load time");
4431 inside_init = error_mark_node;
4434 return inside_init;
4437 /* Come here only for records and arrays. */
4439 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4441 error_init ("variable-sized object may not be initialized");
4442 return error_mark_node;
4445 error_init ("invalid initializer");
4446 return error_mark_node;
4449 /* Handle initializers that use braces. */
4451 /* Type of object we are accumulating a constructor for.
4452 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4453 static tree constructor_type;
4455 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4456 left to fill. */
4457 static tree constructor_fields;
4459 /* For an ARRAY_TYPE, this is the specified index
4460 at which to store the next element we get. */
4461 static tree constructor_index;
4463 /* For an ARRAY_TYPE, this is the maximum index. */
4464 static tree constructor_max_index;
4466 /* For a RECORD_TYPE, this is the first field not yet written out. */
4467 static tree constructor_unfilled_fields;
4469 /* For an ARRAY_TYPE, this is the index of the first element
4470 not yet written out. */
4471 static tree constructor_unfilled_index;
4473 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4474 This is so we can generate gaps between fields, when appropriate. */
4475 static tree constructor_bit_index;
4477 /* If we are saving up the elements rather than allocating them,
4478 this is the list of elements so far (in reverse order,
4479 most recent first). */
4480 static tree constructor_elements;
4482 /* 1 if constructor should be incrementally stored into a constructor chain,
4483 0 if all the elements should be kept in AVL tree. */
4484 static int constructor_incremental;
4486 /* 1 if so far this constructor's elements are all compile-time constants. */
4487 static int constructor_constant;
4489 /* 1 if so far this constructor's elements are all valid address constants. */
4490 static int constructor_simple;
4492 /* 1 if this constructor is erroneous so far. */
4493 static int constructor_erroneous;
4495 /* Structure for managing pending initializer elements, organized as an
4496 AVL tree. */
4498 struct init_node
4500 struct init_node *left, *right;
4501 struct init_node *parent;
4502 int balance;
4503 tree purpose;
4504 tree value;
4507 /* Tree of pending elements at this constructor level.
4508 These are elements encountered out of order
4509 which belong at places we haven't reached yet in actually
4510 writing the output.
4511 Will never hold tree nodes across GC runs. */
4512 static struct init_node *constructor_pending_elts;
4514 /* The SPELLING_DEPTH of this constructor. */
4515 static int constructor_depth;
4517 /* DECL node for which an initializer is being read.
4518 0 means we are reading a constructor expression
4519 such as (struct foo) {...}. */
4520 static tree constructor_decl;
4522 /* Nonzero if this is an initializer for a top-level decl. */
4523 static int constructor_top_level;
4525 /* Nonzero if there were any member designators in this initializer. */
4526 static int constructor_designated;
4528 /* Nesting depth of designator list. */
4529 static int designator_depth;
4531 /* Nonzero if there were diagnosed errors in this designator list. */
4532 static int designator_errorneous;
4535 /* This stack has a level for each implicit or explicit level of
4536 structuring in the initializer, including the outermost one. It
4537 saves the values of most of the variables above. */
4539 struct constructor_range_stack;
4541 struct constructor_stack
4543 struct constructor_stack *next;
4544 tree type;
4545 tree fields;
4546 tree index;
4547 tree max_index;
4548 tree unfilled_index;
4549 tree unfilled_fields;
4550 tree bit_index;
4551 tree elements;
4552 struct init_node *pending_elts;
4553 int offset;
4554 int depth;
4555 /* If value nonzero, this value should replace the entire
4556 constructor at this level. */
4557 struct c_expr replacement_value;
4558 struct constructor_range_stack *range_stack;
4559 char constant;
4560 char simple;
4561 char implicit;
4562 char erroneous;
4563 char outer;
4564 char incremental;
4565 char designated;
4568 struct constructor_stack *constructor_stack;
4570 /* This stack represents designators from some range designator up to
4571 the last designator in the list. */
4573 struct constructor_range_stack
4575 struct constructor_range_stack *next, *prev;
4576 struct constructor_stack *stack;
4577 tree range_start;
4578 tree index;
4579 tree range_end;
4580 tree fields;
4583 struct constructor_range_stack *constructor_range_stack;
4585 /* This stack records separate initializers that are nested.
4586 Nested initializers can't happen in ANSI C, but GNU C allows them
4587 in cases like { ... (struct foo) { ... } ... }. */
4589 struct initializer_stack
4591 struct initializer_stack *next;
4592 tree decl;
4593 struct constructor_stack *constructor_stack;
4594 struct constructor_range_stack *constructor_range_stack;
4595 tree elements;
4596 struct spelling *spelling;
4597 struct spelling *spelling_base;
4598 int spelling_size;
4599 char top_level;
4600 char require_constant_value;
4601 char require_constant_elements;
4604 struct initializer_stack *initializer_stack;
4606 /* Prepare to parse and output the initializer for variable DECL. */
4608 void
4609 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4611 const char *locus;
4612 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4614 p->decl = constructor_decl;
4615 p->require_constant_value = require_constant_value;
4616 p->require_constant_elements = require_constant_elements;
4617 p->constructor_stack = constructor_stack;
4618 p->constructor_range_stack = constructor_range_stack;
4619 p->elements = constructor_elements;
4620 p->spelling = spelling;
4621 p->spelling_base = spelling_base;
4622 p->spelling_size = spelling_size;
4623 p->top_level = constructor_top_level;
4624 p->next = initializer_stack;
4625 initializer_stack = p;
4627 constructor_decl = decl;
4628 constructor_designated = 0;
4629 constructor_top_level = top_level;
4631 if (decl != 0 && decl != error_mark_node)
4633 require_constant_value = TREE_STATIC (decl);
4634 require_constant_elements
4635 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4636 /* For a scalar, you can always use any value to initialize,
4637 even within braces. */
4638 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4639 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4640 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4641 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4642 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4644 else
4646 require_constant_value = 0;
4647 require_constant_elements = 0;
4648 locus = "(anonymous)";
4651 constructor_stack = 0;
4652 constructor_range_stack = 0;
4654 missing_braces_mentioned = 0;
4656 spelling_base = 0;
4657 spelling_size = 0;
4658 RESTORE_SPELLING_DEPTH (0);
4660 if (locus)
4661 push_string (locus);
4664 void
4665 finish_init (void)
4667 struct initializer_stack *p = initializer_stack;
4669 /* Free the whole constructor stack of this initializer. */
4670 while (constructor_stack)
4672 struct constructor_stack *q = constructor_stack;
4673 constructor_stack = q->next;
4674 free (q);
4677 gcc_assert (!constructor_range_stack);
4679 /* Pop back to the data of the outer initializer (if any). */
4680 free (spelling_base);
4682 constructor_decl = p->decl;
4683 require_constant_value = p->require_constant_value;
4684 require_constant_elements = p->require_constant_elements;
4685 constructor_stack = p->constructor_stack;
4686 constructor_range_stack = p->constructor_range_stack;
4687 constructor_elements = p->elements;
4688 spelling = p->spelling;
4689 spelling_base = p->spelling_base;
4690 spelling_size = p->spelling_size;
4691 constructor_top_level = p->top_level;
4692 initializer_stack = p->next;
4693 free (p);
4696 /* Call here when we see the initializer is surrounded by braces.
4697 This is instead of a call to push_init_level;
4698 it is matched by a call to pop_init_level.
4700 TYPE is the type to initialize, for a constructor expression.
4701 For an initializer for a decl, TYPE is zero. */
4703 void
4704 really_start_incremental_init (tree type)
4706 struct constructor_stack *p = XNEW (struct constructor_stack);
4708 if (type == 0)
4709 type = TREE_TYPE (constructor_decl);
4711 if (targetm.vector_opaque_p (type))
4712 error ("opaque vector types cannot be initialized");
4714 p->type = constructor_type;
4715 p->fields = constructor_fields;
4716 p->index = constructor_index;
4717 p->max_index = constructor_max_index;
4718 p->unfilled_index = constructor_unfilled_index;
4719 p->unfilled_fields = constructor_unfilled_fields;
4720 p->bit_index = constructor_bit_index;
4721 p->elements = constructor_elements;
4722 p->constant = constructor_constant;
4723 p->simple = constructor_simple;
4724 p->erroneous = constructor_erroneous;
4725 p->pending_elts = constructor_pending_elts;
4726 p->depth = constructor_depth;
4727 p->replacement_value.value = 0;
4728 p->replacement_value.original_code = ERROR_MARK;
4729 p->implicit = 0;
4730 p->range_stack = 0;
4731 p->outer = 0;
4732 p->incremental = constructor_incremental;
4733 p->designated = constructor_designated;
4734 p->next = 0;
4735 constructor_stack = p;
4737 constructor_constant = 1;
4738 constructor_simple = 1;
4739 constructor_depth = SPELLING_DEPTH ();
4740 constructor_elements = 0;
4741 constructor_pending_elts = 0;
4742 constructor_type = type;
4743 constructor_incremental = 1;
4744 constructor_designated = 0;
4745 designator_depth = 0;
4746 designator_errorneous = 0;
4748 if (TREE_CODE (constructor_type) == RECORD_TYPE
4749 || TREE_CODE (constructor_type) == UNION_TYPE)
4751 constructor_fields = TYPE_FIELDS (constructor_type);
4752 /* Skip any nameless bit fields at the beginning. */
4753 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4754 && DECL_NAME (constructor_fields) == 0)
4755 constructor_fields = TREE_CHAIN (constructor_fields);
4757 constructor_unfilled_fields = constructor_fields;
4758 constructor_bit_index = bitsize_zero_node;
4760 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4762 if (TYPE_DOMAIN (constructor_type))
4764 constructor_max_index
4765 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4767 /* Detect non-empty initializations of zero-length arrays. */
4768 if (constructor_max_index == NULL_TREE
4769 && TYPE_SIZE (constructor_type))
4770 constructor_max_index = build_int_cst (NULL_TREE, -1);
4772 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4773 to initialize VLAs will cause a proper error; avoid tree
4774 checking errors as well by setting a safe value. */
4775 if (constructor_max_index
4776 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4777 constructor_max_index = build_int_cst (NULL_TREE, -1);
4779 constructor_index
4780 = convert (bitsizetype,
4781 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4783 else
4785 constructor_index = bitsize_zero_node;
4786 constructor_max_index = NULL_TREE;
4789 constructor_unfilled_index = constructor_index;
4791 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4793 /* Vectors are like simple fixed-size arrays. */
4794 constructor_max_index =
4795 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4796 constructor_index = bitsize_zero_node;
4797 constructor_unfilled_index = constructor_index;
4799 else
4801 /* Handle the case of int x = {5}; */
4802 constructor_fields = constructor_type;
4803 constructor_unfilled_fields = constructor_type;
4807 /* Push down into a subobject, for initialization.
4808 If this is for an explicit set of braces, IMPLICIT is 0.
4809 If it is because the next element belongs at a lower level,
4810 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4812 void
4813 push_init_level (int implicit)
4815 struct constructor_stack *p;
4816 tree value = NULL_TREE;
4818 /* If we've exhausted any levels that didn't have braces,
4819 pop them now. If implicit == 1, this will have been done in
4820 process_init_element; do not repeat it here because in the case
4821 of excess initializers for an empty aggregate this leads to an
4822 infinite cycle of popping a level and immediately recreating
4823 it. */
4824 if (implicit != 1)
4826 while (constructor_stack->implicit)
4828 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4829 || TREE_CODE (constructor_type) == UNION_TYPE)
4830 && constructor_fields == 0)
4831 process_init_element (pop_init_level (1));
4832 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4833 && constructor_max_index
4834 && tree_int_cst_lt (constructor_max_index,
4835 constructor_index))
4836 process_init_element (pop_init_level (1));
4837 else
4838 break;
4842 /* Unless this is an explicit brace, we need to preserve previous
4843 content if any. */
4844 if (implicit)
4846 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4847 || TREE_CODE (constructor_type) == UNION_TYPE)
4848 && constructor_fields)
4849 value = find_init_member (constructor_fields);
4850 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4851 value = find_init_member (constructor_index);
4854 p = XNEW (struct constructor_stack);
4855 p->type = constructor_type;
4856 p->fields = constructor_fields;
4857 p->index = constructor_index;
4858 p->max_index = constructor_max_index;
4859 p->unfilled_index = constructor_unfilled_index;
4860 p->unfilled_fields = constructor_unfilled_fields;
4861 p->bit_index = constructor_bit_index;
4862 p->elements = constructor_elements;
4863 p->constant = constructor_constant;
4864 p->simple = constructor_simple;
4865 p->erroneous = constructor_erroneous;
4866 p->pending_elts = constructor_pending_elts;
4867 p->depth = constructor_depth;
4868 p->replacement_value.value = 0;
4869 p->replacement_value.original_code = ERROR_MARK;
4870 p->implicit = implicit;
4871 p->outer = 0;
4872 p->incremental = constructor_incremental;
4873 p->designated = constructor_designated;
4874 p->next = constructor_stack;
4875 p->range_stack = 0;
4876 constructor_stack = p;
4878 constructor_constant = 1;
4879 constructor_simple = 1;
4880 constructor_depth = SPELLING_DEPTH ();
4881 constructor_elements = 0;
4882 constructor_incremental = 1;
4883 constructor_designated = 0;
4884 constructor_pending_elts = 0;
4885 if (!implicit)
4887 p->range_stack = constructor_range_stack;
4888 constructor_range_stack = 0;
4889 designator_depth = 0;
4890 designator_errorneous = 0;
4893 /* Don't die if an entire brace-pair level is superfluous
4894 in the containing level. */
4895 if (constructor_type == 0)
4897 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4898 || TREE_CODE (constructor_type) == UNION_TYPE)
4900 /* Don't die if there are extra init elts at the end. */
4901 if (constructor_fields == 0)
4902 constructor_type = 0;
4903 else
4905 constructor_type = TREE_TYPE (constructor_fields);
4906 push_member_name (constructor_fields);
4907 constructor_depth++;
4910 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4912 constructor_type = TREE_TYPE (constructor_type);
4913 push_array_bounds (tree_low_cst (constructor_index, 1));
4914 constructor_depth++;
4917 if (constructor_type == 0)
4919 error_init ("extra brace group at end of initializer");
4920 constructor_fields = 0;
4921 constructor_unfilled_fields = 0;
4922 return;
4925 if (value && TREE_CODE (value) == CONSTRUCTOR)
4927 constructor_constant = TREE_CONSTANT (value);
4928 constructor_simple = TREE_STATIC (value);
4929 constructor_elements = CONSTRUCTOR_ELTS (value);
4930 if (constructor_elements
4931 && (TREE_CODE (constructor_type) == RECORD_TYPE
4932 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4933 set_nonincremental_init ();
4936 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4938 missing_braces_mentioned = 1;
4939 warning_init ("missing braces around initializer");
4942 if (TREE_CODE (constructor_type) == RECORD_TYPE
4943 || TREE_CODE (constructor_type) == UNION_TYPE)
4945 constructor_fields = TYPE_FIELDS (constructor_type);
4946 /* Skip any nameless bit fields at the beginning. */
4947 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4948 && DECL_NAME (constructor_fields) == 0)
4949 constructor_fields = TREE_CHAIN (constructor_fields);
4951 constructor_unfilled_fields = constructor_fields;
4952 constructor_bit_index = bitsize_zero_node;
4954 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4956 /* Vectors are like simple fixed-size arrays. */
4957 constructor_max_index =
4958 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4959 constructor_index = convert (bitsizetype, integer_zero_node);
4960 constructor_unfilled_index = constructor_index;
4962 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4964 if (TYPE_DOMAIN (constructor_type))
4966 constructor_max_index
4967 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4969 /* Detect non-empty initializations of zero-length arrays. */
4970 if (constructor_max_index == NULL_TREE
4971 && TYPE_SIZE (constructor_type))
4972 constructor_max_index = build_int_cst (NULL_TREE, -1);
4974 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4975 to initialize VLAs will cause a proper error; avoid tree
4976 checking errors as well by setting a safe value. */
4977 if (constructor_max_index
4978 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4979 constructor_max_index = build_int_cst (NULL_TREE, -1);
4981 constructor_index
4982 = convert (bitsizetype,
4983 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4985 else
4986 constructor_index = bitsize_zero_node;
4988 constructor_unfilled_index = constructor_index;
4989 if (value && TREE_CODE (value) == STRING_CST)
4991 /* We need to split the char/wchar array into individual
4992 characters, so that we don't have to special case it
4993 everywhere. */
4994 set_nonincremental_init_from_string (value);
4997 else
4999 if (constructor_type != error_mark_node)
5000 warning_init ("braces around scalar initializer");
5001 constructor_fields = constructor_type;
5002 constructor_unfilled_fields = constructor_type;
5006 /* At the end of an implicit or explicit brace level,
5007 finish up that level of constructor. If a single expression
5008 with redundant braces initialized that level, return the
5009 c_expr structure for that expression. Otherwise, the original_code
5010 element is set to ERROR_MARK.
5011 If we were outputting the elements as they are read, return 0 as the value
5012 from inner levels (process_init_element ignores that),
5013 but return error_mark_node as the value from the outermost level
5014 (that's what we want to put in DECL_INITIAL).
5015 Otherwise, return a CONSTRUCTOR expression as the value. */
5017 struct c_expr
5018 pop_init_level (int implicit)
5020 struct constructor_stack *p;
5021 struct c_expr ret;
5022 ret.value = 0;
5023 ret.original_code = ERROR_MARK;
5025 if (implicit == 0)
5027 /* When we come to an explicit close brace,
5028 pop any inner levels that didn't have explicit braces. */
5029 while (constructor_stack->implicit)
5030 process_init_element (pop_init_level (1));
5032 gcc_assert (!constructor_range_stack);
5035 /* Now output all pending elements. */
5036 constructor_incremental = 1;
5037 output_pending_init_elements (1);
5039 p = constructor_stack;
5041 /* Error for initializing a flexible array member, or a zero-length
5042 array member in an inappropriate context. */
5043 if (constructor_type && constructor_fields
5044 && TREE_CODE (constructor_type) == ARRAY_TYPE
5045 && TYPE_DOMAIN (constructor_type)
5046 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5048 /* Silently discard empty initializations. The parser will
5049 already have pedwarned for empty brackets. */
5050 if (integer_zerop (constructor_unfilled_index))
5051 constructor_type = NULL_TREE;
5052 else
5054 gcc_assert (!TYPE_SIZE (constructor_type));
5056 if (constructor_depth > 2)
5057 error_init ("initialization of flexible array member in a nested context");
5058 else if (pedantic)
5059 pedwarn_init ("initialization of a flexible array member");
5061 /* We have already issued an error message for the existence
5062 of a flexible array member not at the end of the structure.
5063 Discard the initializer so that we do not abort later. */
5064 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5065 constructor_type = NULL_TREE;
5069 /* Warn when some struct elements are implicitly initialized to zero. */
5070 if (warn_missing_field_initializers
5071 && constructor_type
5072 && TREE_CODE (constructor_type) == RECORD_TYPE
5073 && constructor_unfilled_fields)
5075 /* Do not warn for flexible array members or zero-length arrays. */
5076 while (constructor_unfilled_fields
5077 && (!DECL_SIZE (constructor_unfilled_fields)
5078 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5079 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5081 /* Do not warn if this level of the initializer uses member
5082 designators; it is likely to be deliberate. */
5083 if (constructor_unfilled_fields && !constructor_designated)
5085 push_member_name (constructor_unfilled_fields);
5086 warning_init ("missing initializer");
5087 RESTORE_SPELLING_DEPTH (constructor_depth);
5091 /* Pad out the end of the structure. */
5092 if (p->replacement_value.value)
5093 /* If this closes a superfluous brace pair,
5094 just pass out the element between them. */
5095 ret = p->replacement_value;
5096 else if (constructor_type == 0)
5098 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5099 && TREE_CODE (constructor_type) != UNION_TYPE
5100 && TREE_CODE (constructor_type) != ARRAY_TYPE
5101 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5103 /* A nonincremental scalar initializer--just return
5104 the element, after verifying there is just one. */
5105 if (constructor_elements == 0)
5107 if (!constructor_erroneous)
5108 error_init ("empty scalar initializer");
5109 ret.value = error_mark_node;
5111 else if (TREE_CHAIN (constructor_elements) != 0)
5113 error_init ("extra elements in scalar initializer");
5114 ret.value = TREE_VALUE (constructor_elements);
5116 else
5117 ret.value = TREE_VALUE (constructor_elements);
5119 else
5121 if (constructor_erroneous)
5122 ret.value = error_mark_node;
5123 else
5125 ret.value = build_constructor (constructor_type,
5126 nreverse (constructor_elements));
5127 if (constructor_constant)
5128 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5129 if (constructor_constant && constructor_simple)
5130 TREE_STATIC (ret.value) = 1;
5134 constructor_type = p->type;
5135 constructor_fields = p->fields;
5136 constructor_index = p->index;
5137 constructor_max_index = p->max_index;
5138 constructor_unfilled_index = p->unfilled_index;
5139 constructor_unfilled_fields = p->unfilled_fields;
5140 constructor_bit_index = p->bit_index;
5141 constructor_elements = p->elements;
5142 constructor_constant = p->constant;
5143 constructor_simple = p->simple;
5144 constructor_erroneous = p->erroneous;
5145 constructor_incremental = p->incremental;
5146 constructor_designated = p->designated;
5147 constructor_pending_elts = p->pending_elts;
5148 constructor_depth = p->depth;
5149 if (!p->implicit)
5150 constructor_range_stack = p->range_stack;
5151 RESTORE_SPELLING_DEPTH (constructor_depth);
5153 constructor_stack = p->next;
5154 free (p);
5156 if (ret.value == 0)
5158 if (constructor_stack == 0)
5160 ret.value = error_mark_node;
5161 return ret;
5163 return ret;
5165 return ret;
5168 /* Common handling for both array range and field name designators.
5169 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5171 static int
5172 set_designator (int array)
5174 tree subtype;
5175 enum tree_code subcode;
5177 /* Don't die if an entire brace-pair level is superfluous
5178 in the containing level. */
5179 if (constructor_type == 0)
5180 return 1;
5182 /* If there were errors in this designator list already, bail out
5183 silently. */
5184 if (designator_errorneous)
5185 return 1;
5187 if (!designator_depth)
5189 gcc_assert (!constructor_range_stack);
5191 /* Designator list starts at the level of closest explicit
5192 braces. */
5193 while (constructor_stack->implicit)
5194 process_init_element (pop_init_level (1));
5195 constructor_designated = 1;
5196 return 0;
5199 switch (TREE_CODE (constructor_type))
5201 case RECORD_TYPE:
5202 case UNION_TYPE:
5203 subtype = TREE_TYPE (constructor_fields);
5204 if (subtype != error_mark_node)
5205 subtype = TYPE_MAIN_VARIANT (subtype);
5206 break;
5207 case ARRAY_TYPE:
5208 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5209 break;
5210 default:
5211 gcc_unreachable ();
5214 subcode = TREE_CODE (subtype);
5215 if (array && subcode != ARRAY_TYPE)
5217 error_init ("array index in non-array initializer");
5218 return 1;
5220 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5222 error_init ("field name not in record or union initializer");
5223 return 1;
5226 constructor_designated = 1;
5227 push_init_level (2);
5228 return 0;
5231 /* If there are range designators in designator list, push a new designator
5232 to constructor_range_stack. RANGE_END is end of such stack range or
5233 NULL_TREE if there is no range designator at this level. */
5235 static void
5236 push_range_stack (tree range_end)
5238 struct constructor_range_stack *p;
5240 p = GGC_NEW (struct constructor_range_stack);
5241 p->prev = constructor_range_stack;
5242 p->next = 0;
5243 p->fields = constructor_fields;
5244 p->range_start = constructor_index;
5245 p->index = constructor_index;
5246 p->stack = constructor_stack;
5247 p->range_end = range_end;
5248 if (constructor_range_stack)
5249 constructor_range_stack->next = p;
5250 constructor_range_stack = p;
5253 /* Within an array initializer, specify the next index to be initialized.
5254 FIRST is that index. If LAST is nonzero, then initialize a range
5255 of indices, running from FIRST through LAST. */
5257 void
5258 set_init_index (tree first, tree last)
5260 if (set_designator (1))
5261 return;
5263 designator_errorneous = 1;
5265 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5266 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5268 error_init ("array index in initializer not of integer type");
5269 return;
5272 while ((TREE_CODE (first) == NOP_EXPR
5273 || TREE_CODE (first) == CONVERT_EXPR
5274 || TREE_CODE (first) == NON_LVALUE_EXPR)
5275 && (TYPE_MODE (TREE_TYPE (first))
5276 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
5277 first = TREE_OPERAND (first, 0);
5279 if (last)
5280 while ((TREE_CODE (last) == NOP_EXPR
5281 || TREE_CODE (last) == CONVERT_EXPR
5282 || TREE_CODE (last) == NON_LVALUE_EXPR)
5283 && (TYPE_MODE (TREE_TYPE (last))
5284 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
5285 last = TREE_OPERAND (last, 0);
5287 if (TREE_CODE (first) != INTEGER_CST)
5288 error_init ("nonconstant array index in initializer");
5289 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5290 error_init ("nonconstant array index in initializer");
5291 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5292 error_init ("array index in non-array initializer");
5293 else if (tree_int_cst_sgn (first) == -1)
5294 error_init ("array index in initializer exceeds array bounds");
5295 else if (constructor_max_index
5296 && tree_int_cst_lt (constructor_max_index, first))
5297 error_init ("array index in initializer exceeds array bounds");
5298 else
5300 constructor_index = convert (bitsizetype, first);
5302 if (last)
5304 if (tree_int_cst_equal (first, last))
5305 last = 0;
5306 else if (tree_int_cst_lt (last, first))
5308 error_init ("empty index range in initializer");
5309 last = 0;
5311 else
5313 last = convert (bitsizetype, last);
5314 if (constructor_max_index != 0
5315 && tree_int_cst_lt (constructor_max_index, last))
5317 error_init ("array index range in initializer exceeds array bounds");
5318 last = 0;
5323 designator_depth++;
5324 designator_errorneous = 0;
5325 if (constructor_range_stack || last)
5326 push_range_stack (last);
5330 /* Within a struct initializer, specify the next field to be initialized. */
5332 void
5333 set_init_label (tree fieldname)
5335 tree tail;
5337 if (set_designator (0))
5338 return;
5340 designator_errorneous = 1;
5342 if (TREE_CODE (constructor_type) != RECORD_TYPE
5343 && TREE_CODE (constructor_type) != UNION_TYPE)
5345 error_init ("field name not in record or union initializer");
5346 return;
5349 for (tail = TYPE_FIELDS (constructor_type); tail;
5350 tail = TREE_CHAIN (tail))
5352 if (DECL_NAME (tail) == fieldname)
5353 break;
5356 if (tail == 0)
5357 error ("unknown field %qs specified in initializer",
5358 IDENTIFIER_POINTER (fieldname));
5359 else
5361 constructor_fields = tail;
5362 designator_depth++;
5363 designator_errorneous = 0;
5364 if (constructor_range_stack)
5365 push_range_stack (NULL_TREE);
5369 /* Add a new initializer to the tree of pending initializers. PURPOSE
5370 identifies the initializer, either array index or field in a structure.
5371 VALUE is the value of that index or field. */
5373 static void
5374 add_pending_init (tree purpose, tree value)
5376 struct init_node *p, **q, *r;
5378 q = &constructor_pending_elts;
5379 p = 0;
5381 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5383 while (*q != 0)
5385 p = *q;
5386 if (tree_int_cst_lt (purpose, p->purpose))
5387 q = &p->left;
5388 else if (tree_int_cst_lt (p->purpose, purpose))
5389 q = &p->right;
5390 else
5392 if (TREE_SIDE_EFFECTS (p->value))
5393 warning_init ("initialized field with side-effects overwritten");
5394 p->value = value;
5395 return;
5399 else
5401 tree bitpos;
5403 bitpos = bit_position (purpose);
5404 while (*q != NULL)
5406 p = *q;
5407 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5408 q = &p->left;
5409 else if (p->purpose != purpose)
5410 q = &p->right;
5411 else
5413 if (TREE_SIDE_EFFECTS (p->value))
5414 warning_init ("initialized field with side-effects overwritten");
5415 p->value = value;
5416 return;
5421 r = GGC_NEW (struct init_node);
5422 r->purpose = purpose;
5423 r->value = value;
5425 *q = r;
5426 r->parent = p;
5427 r->left = 0;
5428 r->right = 0;
5429 r->balance = 0;
5431 while (p)
5433 struct init_node *s;
5435 if (r == p->left)
5437 if (p->balance == 0)
5438 p->balance = -1;
5439 else if (p->balance < 0)
5441 if (r->balance < 0)
5443 /* L rotation. */
5444 p->left = r->right;
5445 if (p->left)
5446 p->left->parent = p;
5447 r->right = p;
5449 p->balance = 0;
5450 r->balance = 0;
5452 s = p->parent;
5453 p->parent = r;
5454 r->parent = s;
5455 if (s)
5457 if (s->left == p)
5458 s->left = r;
5459 else
5460 s->right = r;
5462 else
5463 constructor_pending_elts = r;
5465 else
5467 /* LR rotation. */
5468 struct init_node *t = r->right;
5470 r->right = t->left;
5471 if (r->right)
5472 r->right->parent = r;
5473 t->left = r;
5475 p->left = t->right;
5476 if (p->left)
5477 p->left->parent = p;
5478 t->right = p;
5480 p->balance = t->balance < 0;
5481 r->balance = -(t->balance > 0);
5482 t->balance = 0;
5484 s = p->parent;
5485 p->parent = t;
5486 r->parent = t;
5487 t->parent = s;
5488 if (s)
5490 if (s->left == p)
5491 s->left = t;
5492 else
5493 s->right = t;
5495 else
5496 constructor_pending_elts = t;
5498 break;
5500 else
5502 /* p->balance == +1; growth of left side balances the node. */
5503 p->balance = 0;
5504 break;
5507 else /* r == p->right */
5509 if (p->balance == 0)
5510 /* Growth propagation from right side. */
5511 p->balance++;
5512 else if (p->balance > 0)
5514 if (r->balance > 0)
5516 /* R rotation. */
5517 p->right = r->left;
5518 if (p->right)
5519 p->right->parent = p;
5520 r->left = p;
5522 p->balance = 0;
5523 r->balance = 0;
5525 s = p->parent;
5526 p->parent = r;
5527 r->parent = s;
5528 if (s)
5530 if (s->left == p)
5531 s->left = r;
5532 else
5533 s->right = r;
5535 else
5536 constructor_pending_elts = r;
5538 else /* r->balance == -1 */
5540 /* RL rotation */
5541 struct init_node *t = r->left;
5543 r->left = t->right;
5544 if (r->left)
5545 r->left->parent = r;
5546 t->right = r;
5548 p->right = t->left;
5549 if (p->right)
5550 p->right->parent = p;
5551 t->left = p;
5553 r->balance = (t->balance < 0);
5554 p->balance = -(t->balance > 0);
5555 t->balance = 0;
5557 s = p->parent;
5558 p->parent = t;
5559 r->parent = t;
5560 t->parent = s;
5561 if (s)
5563 if (s->left == p)
5564 s->left = t;
5565 else
5566 s->right = t;
5568 else
5569 constructor_pending_elts = t;
5571 break;
5573 else
5575 /* p->balance == -1; growth of right side balances the node. */
5576 p->balance = 0;
5577 break;
5581 r = p;
5582 p = p->parent;
5586 /* Build AVL tree from a sorted chain. */
5588 static void
5589 set_nonincremental_init (void)
5591 tree chain;
5593 if (TREE_CODE (constructor_type) != RECORD_TYPE
5594 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5595 return;
5597 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5598 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5599 constructor_elements = 0;
5600 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5602 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5603 /* Skip any nameless bit fields at the beginning. */
5604 while (constructor_unfilled_fields != 0
5605 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5606 && DECL_NAME (constructor_unfilled_fields) == 0)
5607 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5610 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5612 if (TYPE_DOMAIN (constructor_type))
5613 constructor_unfilled_index
5614 = convert (bitsizetype,
5615 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5616 else
5617 constructor_unfilled_index = bitsize_zero_node;
5619 constructor_incremental = 0;
5622 /* Build AVL tree from a string constant. */
5624 static void
5625 set_nonincremental_init_from_string (tree str)
5627 tree value, purpose, type;
5628 HOST_WIDE_INT val[2];
5629 const char *p, *end;
5630 int byte, wchar_bytes, charwidth, bitpos;
5632 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5634 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5635 == TYPE_PRECISION (char_type_node))
5636 wchar_bytes = 1;
5637 else
5639 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5640 == TYPE_PRECISION (wchar_type_node));
5641 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5643 charwidth = TYPE_PRECISION (char_type_node);
5644 type = TREE_TYPE (constructor_type);
5645 p = TREE_STRING_POINTER (str);
5646 end = p + TREE_STRING_LENGTH (str);
5648 for (purpose = bitsize_zero_node;
5649 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5650 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5652 if (wchar_bytes == 1)
5654 val[1] = (unsigned char) *p++;
5655 val[0] = 0;
5657 else
5659 val[0] = 0;
5660 val[1] = 0;
5661 for (byte = 0; byte < wchar_bytes; byte++)
5663 if (BYTES_BIG_ENDIAN)
5664 bitpos = (wchar_bytes - byte - 1) * charwidth;
5665 else
5666 bitpos = byte * charwidth;
5667 val[bitpos < HOST_BITS_PER_WIDE_INT]
5668 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5669 << (bitpos % HOST_BITS_PER_WIDE_INT);
5673 if (!TYPE_UNSIGNED (type))
5675 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5676 if (bitpos < HOST_BITS_PER_WIDE_INT)
5678 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5680 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5681 val[0] = -1;
5684 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5686 if (val[1] < 0)
5687 val[0] = -1;
5689 else if (val[0] & (((HOST_WIDE_INT) 1)
5690 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5691 val[0] |= ((HOST_WIDE_INT) -1)
5692 << (bitpos - HOST_BITS_PER_WIDE_INT);
5695 value = build_int_cst_wide (type, val[1], val[0]);
5696 add_pending_init (purpose, value);
5699 constructor_incremental = 0;
5702 /* Return value of FIELD in pending initializer or zero if the field was
5703 not initialized yet. */
5705 static tree
5706 find_init_member (tree field)
5708 struct init_node *p;
5710 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5712 if (constructor_incremental
5713 && tree_int_cst_lt (field, constructor_unfilled_index))
5714 set_nonincremental_init ();
5716 p = constructor_pending_elts;
5717 while (p)
5719 if (tree_int_cst_lt (field, p->purpose))
5720 p = p->left;
5721 else if (tree_int_cst_lt (p->purpose, field))
5722 p = p->right;
5723 else
5724 return p->value;
5727 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5729 tree bitpos = bit_position (field);
5731 if (constructor_incremental
5732 && (!constructor_unfilled_fields
5733 || tree_int_cst_lt (bitpos,
5734 bit_position (constructor_unfilled_fields))))
5735 set_nonincremental_init ();
5737 p = constructor_pending_elts;
5738 while (p)
5740 if (field == p->purpose)
5741 return p->value;
5742 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5743 p = p->left;
5744 else
5745 p = p->right;
5748 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5750 if (constructor_elements
5751 && TREE_PURPOSE (constructor_elements) == field)
5752 return TREE_VALUE (constructor_elements);
5754 return 0;
5757 /* "Output" the next constructor element.
5758 At top level, really output it to assembler code now.
5759 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5760 TYPE is the data type that the containing data type wants here.
5761 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5762 If VALUE is a string constant, STRICT_STRING is true if it is
5763 unparenthesized or we should not warn here for it being parenthesized.
5764 For other types of VALUE, STRICT_STRING is not used.
5766 PENDING if non-nil means output pending elements that belong
5767 right after this element. (PENDING is normally 1;
5768 it is 0 while outputting pending elements, to avoid recursion.) */
5770 static void
5771 output_init_element (tree value, bool strict_string, tree type, tree field,
5772 int pending)
5774 if (type == error_mark_node || value == error_mark_node)
5776 constructor_erroneous = 1;
5777 return;
5779 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5780 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5781 && !(TREE_CODE (value) == STRING_CST
5782 && TREE_CODE (type) == ARRAY_TYPE
5783 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5784 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5785 TYPE_MAIN_VARIANT (type))))
5786 value = default_conversion (value);
5788 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5789 && require_constant_value && !flag_isoc99 && pending)
5791 /* As an extension, allow initializing objects with static storage
5792 duration with compound literals (which are then treated just as
5793 the brace enclosed list they contain). */
5794 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5795 value = DECL_INITIAL (decl);
5798 if (value == error_mark_node)
5799 constructor_erroneous = 1;
5800 else if (!TREE_CONSTANT (value))
5801 constructor_constant = 0;
5802 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
5803 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5804 || TREE_CODE (constructor_type) == UNION_TYPE)
5805 && DECL_C_BIT_FIELD (field)
5806 && TREE_CODE (value) != INTEGER_CST))
5807 constructor_simple = 0;
5809 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
5811 if (require_constant_value)
5813 error_init ("initializer element is not constant");
5814 value = error_mark_node;
5816 else if (require_constant_elements)
5817 pedwarn ("initializer element is not computable at load time");
5820 /* If this field is empty (and not at the end of structure),
5821 don't do anything other than checking the initializer. */
5822 if (field
5823 && (TREE_TYPE (field) == error_mark_node
5824 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5825 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5826 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5827 || TREE_CHAIN (field)))))
5828 return;
5830 value = digest_init (type, value, strict_string, require_constant_value);
5831 if (value == error_mark_node)
5833 constructor_erroneous = 1;
5834 return;
5837 /* If this element doesn't come next in sequence,
5838 put it on constructor_pending_elts. */
5839 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5840 && (!constructor_incremental
5841 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5843 if (constructor_incremental
5844 && tree_int_cst_lt (field, constructor_unfilled_index))
5845 set_nonincremental_init ();
5847 add_pending_init (field, value);
5848 return;
5850 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5851 && (!constructor_incremental
5852 || field != constructor_unfilled_fields))
5854 /* We do this for records but not for unions. In a union,
5855 no matter which field is specified, it can be initialized
5856 right away since it starts at the beginning of the union. */
5857 if (constructor_incremental)
5859 if (!constructor_unfilled_fields)
5860 set_nonincremental_init ();
5861 else
5863 tree bitpos, unfillpos;
5865 bitpos = bit_position (field);
5866 unfillpos = bit_position (constructor_unfilled_fields);
5868 if (tree_int_cst_lt (bitpos, unfillpos))
5869 set_nonincremental_init ();
5873 add_pending_init (field, value);
5874 return;
5876 else if (TREE_CODE (constructor_type) == UNION_TYPE
5877 && constructor_elements)
5879 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5880 warning_init ("initialized field with side-effects overwritten");
5882 /* We can have just one union field set. */
5883 constructor_elements = 0;
5886 /* Otherwise, output this element either to
5887 constructor_elements or to the assembler file. */
5889 if (field && TREE_CODE (field) == INTEGER_CST)
5890 field = copy_node (field);
5891 constructor_elements
5892 = tree_cons (field, value, constructor_elements);
5894 /* Advance the variable that indicates sequential elements output. */
5895 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5896 constructor_unfilled_index
5897 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5898 bitsize_one_node);
5899 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5901 constructor_unfilled_fields
5902 = TREE_CHAIN (constructor_unfilled_fields);
5904 /* Skip any nameless bit fields. */
5905 while (constructor_unfilled_fields != 0
5906 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5907 && DECL_NAME (constructor_unfilled_fields) == 0)
5908 constructor_unfilled_fields =
5909 TREE_CHAIN (constructor_unfilled_fields);
5911 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5912 constructor_unfilled_fields = 0;
5914 /* Now output any pending elements which have become next. */
5915 if (pending)
5916 output_pending_init_elements (0);
5919 /* Output any pending elements which have become next.
5920 As we output elements, constructor_unfilled_{fields,index}
5921 advances, which may cause other elements to become next;
5922 if so, they too are output.
5924 If ALL is 0, we return when there are
5925 no more pending elements to output now.
5927 If ALL is 1, we output space as necessary so that
5928 we can output all the pending elements. */
5930 static void
5931 output_pending_init_elements (int all)
5933 struct init_node *elt = constructor_pending_elts;
5934 tree next;
5936 retry:
5938 /* Look through the whole pending tree.
5939 If we find an element that should be output now,
5940 output it. Otherwise, set NEXT to the element
5941 that comes first among those still pending. */
5943 next = 0;
5944 while (elt)
5946 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5948 if (tree_int_cst_equal (elt->purpose,
5949 constructor_unfilled_index))
5950 output_init_element (elt->value, true,
5951 TREE_TYPE (constructor_type),
5952 constructor_unfilled_index, 0);
5953 else if (tree_int_cst_lt (constructor_unfilled_index,
5954 elt->purpose))
5956 /* Advance to the next smaller node. */
5957 if (elt->left)
5958 elt = elt->left;
5959 else
5961 /* We have reached the smallest node bigger than the
5962 current unfilled index. Fill the space first. */
5963 next = elt->purpose;
5964 break;
5967 else
5969 /* Advance to the next bigger node. */
5970 if (elt->right)
5971 elt = elt->right;
5972 else
5974 /* We have reached the biggest node in a subtree. Find
5975 the parent of it, which is the next bigger node. */
5976 while (elt->parent && elt->parent->right == elt)
5977 elt = elt->parent;
5978 elt = elt->parent;
5979 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5980 elt->purpose))
5982 next = elt->purpose;
5983 break;
5988 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5989 || TREE_CODE (constructor_type) == UNION_TYPE)
5991 tree ctor_unfilled_bitpos, elt_bitpos;
5993 /* If the current record is complete we are done. */
5994 if (constructor_unfilled_fields == 0)
5995 break;
5997 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5998 elt_bitpos = bit_position (elt->purpose);
5999 /* We can't compare fields here because there might be empty
6000 fields in between. */
6001 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
6003 constructor_unfilled_fields = elt->purpose;
6004 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
6005 elt->purpose, 0);
6007 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
6009 /* Advance to the next smaller node. */
6010 if (elt->left)
6011 elt = elt->left;
6012 else
6014 /* We have reached the smallest node bigger than the
6015 current unfilled field. Fill the space first. */
6016 next = elt->purpose;
6017 break;
6020 else
6022 /* Advance to the next bigger node. */
6023 if (elt->right)
6024 elt = elt->right;
6025 else
6027 /* We have reached the biggest node in a subtree. Find
6028 the parent of it, which is the next bigger node. */
6029 while (elt->parent && elt->parent->right == elt)
6030 elt = elt->parent;
6031 elt = elt->parent;
6032 if (elt
6033 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6034 bit_position (elt->purpose))))
6036 next = elt->purpose;
6037 break;
6044 /* Ordinarily return, but not if we want to output all
6045 and there are elements left. */
6046 if (!(all && next != 0))
6047 return;
6049 /* If it's not incremental, just skip over the gap, so that after
6050 jumping to retry we will output the next successive element. */
6051 if (TREE_CODE (constructor_type) == RECORD_TYPE
6052 || TREE_CODE (constructor_type) == UNION_TYPE)
6053 constructor_unfilled_fields = next;
6054 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6055 constructor_unfilled_index = next;
6057 /* ELT now points to the node in the pending tree with the next
6058 initializer to output. */
6059 goto retry;
6062 /* Add one non-braced element to the current constructor level.
6063 This adjusts the current position within the constructor's type.
6064 This may also start or terminate implicit levels
6065 to handle a partly-braced initializer.
6067 Once this has found the correct level for the new element,
6068 it calls output_init_element. */
6070 void
6071 process_init_element (struct c_expr value)
6073 tree orig_value = value.value;
6074 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6075 bool strict_string = value.original_code == STRING_CST;
6077 designator_depth = 0;
6078 designator_errorneous = 0;
6080 /* Handle superfluous braces around string cst as in
6081 char x[] = {"foo"}; */
6082 if (string_flag
6083 && constructor_type
6084 && TREE_CODE (constructor_type) == ARRAY_TYPE
6085 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6086 && integer_zerop (constructor_unfilled_index))
6088 if (constructor_stack->replacement_value.value)
6089 error_init ("excess elements in char array initializer");
6090 constructor_stack->replacement_value = value;
6091 return;
6094 if (constructor_stack->replacement_value.value != 0)
6096 error_init ("excess elements in struct initializer");
6097 return;
6100 /* Ignore elements of a brace group if it is entirely superfluous
6101 and has already been diagnosed. */
6102 if (constructor_type == 0)
6103 return;
6105 /* If we've exhausted any levels that didn't have braces,
6106 pop them now. */
6107 while (constructor_stack->implicit)
6109 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6110 || TREE_CODE (constructor_type) == UNION_TYPE)
6111 && constructor_fields == 0)
6112 process_init_element (pop_init_level (1));
6113 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6114 && (constructor_max_index == 0
6115 || tree_int_cst_lt (constructor_max_index,
6116 constructor_index)))
6117 process_init_element (pop_init_level (1));
6118 else
6119 break;
6122 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6123 if (constructor_range_stack)
6125 /* If value is a compound literal and we'll be just using its
6126 content, don't put it into a SAVE_EXPR. */
6127 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6128 || !require_constant_value
6129 || flag_isoc99)
6130 value.value = save_expr (value.value);
6133 while (1)
6135 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6137 tree fieldtype;
6138 enum tree_code fieldcode;
6140 if (constructor_fields == 0)
6142 pedwarn_init ("excess elements in struct initializer");
6143 break;
6146 fieldtype = TREE_TYPE (constructor_fields);
6147 if (fieldtype != error_mark_node)
6148 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6149 fieldcode = TREE_CODE (fieldtype);
6151 /* Error for non-static initialization of a flexible array member. */
6152 if (fieldcode == ARRAY_TYPE
6153 && !require_constant_value
6154 && TYPE_SIZE (fieldtype) == NULL_TREE
6155 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6157 error_init ("non-static initialization of a flexible array member");
6158 break;
6161 /* Accept a string constant to initialize a subarray. */
6162 if (value.value != 0
6163 && fieldcode == ARRAY_TYPE
6164 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6165 && string_flag)
6166 value.value = orig_value;
6167 /* Otherwise, if we have come to a subaggregate,
6168 and we don't have an element of its type, push into it. */
6169 else if (value.value != 0
6170 && value.value != error_mark_node
6171 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6172 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6173 || fieldcode == UNION_TYPE))
6175 push_init_level (1);
6176 continue;
6179 if (value.value)
6181 push_member_name (constructor_fields);
6182 output_init_element (value.value, strict_string,
6183 fieldtype, constructor_fields, 1);
6184 RESTORE_SPELLING_DEPTH (constructor_depth);
6186 else
6187 /* Do the bookkeeping for an element that was
6188 directly output as a constructor. */
6190 /* For a record, keep track of end position of last field. */
6191 if (DECL_SIZE (constructor_fields))
6192 constructor_bit_index
6193 = size_binop (PLUS_EXPR,
6194 bit_position (constructor_fields),
6195 DECL_SIZE (constructor_fields));
6197 /* If the current field was the first one not yet written out,
6198 it isn't now, so update. */
6199 if (constructor_unfilled_fields == constructor_fields)
6201 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6202 /* Skip any nameless bit fields. */
6203 while (constructor_unfilled_fields != 0
6204 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6205 && DECL_NAME (constructor_unfilled_fields) == 0)
6206 constructor_unfilled_fields =
6207 TREE_CHAIN (constructor_unfilled_fields);
6211 constructor_fields = TREE_CHAIN (constructor_fields);
6212 /* Skip any nameless bit fields at the beginning. */
6213 while (constructor_fields != 0
6214 && DECL_C_BIT_FIELD (constructor_fields)
6215 && DECL_NAME (constructor_fields) == 0)
6216 constructor_fields = TREE_CHAIN (constructor_fields);
6218 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6220 tree fieldtype;
6221 enum tree_code fieldcode;
6223 if (constructor_fields == 0)
6225 pedwarn_init ("excess elements in union initializer");
6226 break;
6229 fieldtype = TREE_TYPE (constructor_fields);
6230 if (fieldtype != error_mark_node)
6231 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6232 fieldcode = TREE_CODE (fieldtype);
6234 /* Warn that traditional C rejects initialization of unions.
6235 We skip the warning if the value is zero. This is done
6236 under the assumption that the zero initializer in user
6237 code appears conditioned on e.g. __STDC__ to avoid
6238 "missing initializer" warnings and relies on default
6239 initialization to zero in the traditional C case.
6240 We also skip the warning if the initializer is designated,
6241 again on the assumption that this must be conditional on
6242 __STDC__ anyway (and we've already complained about the
6243 member-designator already). */
6244 if (warn_traditional && !in_system_header && !constructor_designated
6245 && !(value.value && (integer_zerop (value.value)
6246 || real_zerop (value.value))))
6247 warning ("traditional C rejects initialization of unions");
6249 /* Accept a string constant to initialize a subarray. */
6250 if (value.value != 0
6251 && fieldcode == ARRAY_TYPE
6252 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6253 && string_flag)
6254 value.value = orig_value;
6255 /* Otherwise, if we have come to a subaggregate,
6256 and we don't have an element of its type, push into it. */
6257 else if (value.value != 0
6258 && value.value != error_mark_node
6259 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6260 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6261 || fieldcode == UNION_TYPE))
6263 push_init_level (1);
6264 continue;
6267 if (value.value)
6269 push_member_name (constructor_fields);
6270 output_init_element (value.value, strict_string,
6271 fieldtype, constructor_fields, 1);
6272 RESTORE_SPELLING_DEPTH (constructor_depth);
6274 else
6275 /* Do the bookkeeping for an element that was
6276 directly output as a constructor. */
6278 constructor_bit_index = DECL_SIZE (constructor_fields);
6279 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6282 constructor_fields = 0;
6284 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6286 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6287 enum tree_code eltcode = TREE_CODE (elttype);
6289 /* Accept a string constant to initialize a subarray. */
6290 if (value.value != 0
6291 && eltcode == ARRAY_TYPE
6292 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6293 && string_flag)
6294 value.value = orig_value;
6295 /* Otherwise, if we have come to a subaggregate,
6296 and we don't have an element of its type, push into it. */
6297 else if (value.value != 0
6298 && value.value != error_mark_node
6299 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6300 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6301 || eltcode == UNION_TYPE))
6303 push_init_level (1);
6304 continue;
6307 if (constructor_max_index != 0
6308 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6309 || integer_all_onesp (constructor_max_index)))
6311 pedwarn_init ("excess elements in array initializer");
6312 break;
6315 /* Now output the actual element. */
6316 if (value.value)
6318 push_array_bounds (tree_low_cst (constructor_index, 1));
6319 output_init_element (value.value, strict_string,
6320 elttype, constructor_index, 1);
6321 RESTORE_SPELLING_DEPTH (constructor_depth);
6324 constructor_index
6325 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6327 if (!value.value)
6328 /* If we are doing the bookkeeping for an element that was
6329 directly output as a constructor, we must update
6330 constructor_unfilled_index. */
6331 constructor_unfilled_index = constructor_index;
6333 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6335 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6337 /* Do a basic check of initializer size. Note that vectors
6338 always have a fixed size derived from their type. */
6339 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6341 pedwarn_init ("excess elements in vector initializer");
6342 break;
6345 /* Now output the actual element. */
6346 if (value.value)
6347 output_init_element (value.value, strict_string,
6348 elttype, constructor_index, 1);
6350 constructor_index
6351 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6353 if (!value.value)
6354 /* If we are doing the bookkeeping for an element that was
6355 directly output as a constructor, we must update
6356 constructor_unfilled_index. */
6357 constructor_unfilled_index = constructor_index;
6360 /* Handle the sole element allowed in a braced initializer
6361 for a scalar variable. */
6362 else if (constructor_type != error_mark_node
6363 && constructor_fields == 0)
6365 pedwarn_init ("excess elements in scalar initializer");
6366 break;
6368 else
6370 if (value.value)
6371 output_init_element (value.value, strict_string,
6372 constructor_type, NULL_TREE, 1);
6373 constructor_fields = 0;
6376 /* Handle range initializers either at this level or anywhere higher
6377 in the designator stack. */
6378 if (constructor_range_stack)
6380 struct constructor_range_stack *p, *range_stack;
6381 int finish = 0;
6383 range_stack = constructor_range_stack;
6384 constructor_range_stack = 0;
6385 while (constructor_stack != range_stack->stack)
6387 gcc_assert (constructor_stack->implicit);
6388 process_init_element (pop_init_level (1));
6390 for (p = range_stack;
6391 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6392 p = p->prev)
6394 gcc_assert (constructor_stack->implicit);
6395 process_init_element (pop_init_level (1));
6398 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6399 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6400 finish = 1;
6402 while (1)
6404 constructor_index = p->index;
6405 constructor_fields = p->fields;
6406 if (finish && p->range_end && p->index == p->range_start)
6408 finish = 0;
6409 p->prev = 0;
6411 p = p->next;
6412 if (!p)
6413 break;
6414 push_init_level (2);
6415 p->stack = constructor_stack;
6416 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6417 p->index = p->range_start;
6420 if (!finish)
6421 constructor_range_stack = range_stack;
6422 continue;
6425 break;
6428 constructor_range_stack = 0;
6431 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6432 (guaranteed to be 'volatile' or null) and ARGS (represented using
6433 an ASM_EXPR node). */
6434 tree
6435 build_asm_stmt (tree cv_qualifier, tree args)
6437 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6438 ASM_VOLATILE_P (args) = 1;
6439 return add_stmt (args);
6442 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6443 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6444 SIMPLE indicates whether there was anything at all after the
6445 string in the asm expression -- asm("blah") and asm("blah" : )
6446 are subtly different. We use a ASM_EXPR node to represent this. */
6447 tree
6448 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6449 bool simple)
6451 tree tail;
6452 tree args;
6453 int i;
6454 const char *constraint;
6455 const char **oconstraints;
6456 bool allows_mem, allows_reg, is_inout;
6457 int ninputs, noutputs;
6459 ninputs = list_length (inputs);
6460 noutputs = list_length (outputs);
6461 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6463 string = resolve_asm_operand_names (string, outputs, inputs);
6465 /* Remove output conversions that change the type but not the mode. */
6466 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6468 tree output = TREE_VALUE (tail);
6470 /* ??? Really, this should not be here. Users should be using a
6471 proper lvalue, dammit. But there's a long history of using casts
6472 in the output operands. In cases like longlong.h, this becomes a
6473 primitive form of typechecking -- if the cast can be removed, then
6474 the output operand had a type of the proper width; otherwise we'll
6475 get an error. Gross, but ... */
6476 STRIP_NOPS (output);
6478 if (!lvalue_or_else (output, lv_asm))
6479 output = error_mark_node;
6481 if (output != error_mark_node
6482 && (TREE_READONLY (output)
6483 || TYPE_READONLY (TREE_TYPE (output))
6484 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
6485 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
6486 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
6487 readonly_error (output, lv_asm);
6489 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6490 oconstraints[i] = constraint;
6492 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6493 &allows_mem, &allows_reg, &is_inout))
6495 /* If the operand is going to end up in memory,
6496 mark it addressable. */
6497 if (!allows_reg && !c_mark_addressable (output))
6498 output = error_mark_node;
6500 else
6501 output = error_mark_node;
6503 TREE_VALUE (tail) = output;
6506 /* Perform default conversions on array and function inputs.
6507 Don't do this for other types as it would screw up operands
6508 expected to be in memory. */
6509 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6511 tree input;
6513 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6514 input = TREE_VALUE (tail);
6516 input = default_function_array_conversion (input);
6518 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6519 oconstraints, &allows_mem, &allows_reg))
6521 /* If the operand is going to end up in memory,
6522 mark it addressable. */
6523 if (!allows_reg && allows_mem)
6525 /* Strip the nops as we allow this case. FIXME, this really
6526 should be rejected or made deprecated. */
6527 STRIP_NOPS (input);
6528 if (!c_mark_addressable (input))
6529 input = error_mark_node;
6532 else
6533 input = error_mark_node;
6535 TREE_VALUE (tail) = input;
6538 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6540 /* asm statements without outputs, including simple ones, are treated
6541 as volatile. */
6542 ASM_INPUT_P (args) = simple;
6543 ASM_VOLATILE_P (args) = (noutputs == 0);
6545 return args;
6548 /* Generate a goto statement to LABEL. */
6550 tree
6551 c_finish_goto_label (tree label)
6553 tree decl = lookup_label (label);
6554 if (!decl)
6555 return NULL_TREE;
6557 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
6559 error ("jump into statement expression");
6560 return NULL_TREE;
6563 if (C_DECL_UNJUMPABLE_VM (decl))
6565 error ("jump into scope of identifier with variably modified type");
6566 return NULL_TREE;
6569 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
6571 /* No jump from outside this statement expression context, so
6572 record that there is a jump from within this context. */
6573 struct c_label_list *nlist;
6574 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6575 nlist->next = label_context_stack_se->labels_used;
6576 nlist->label = decl;
6577 label_context_stack_se->labels_used = nlist;
6580 if (!C_DECL_UNDEFINABLE_VM (decl))
6582 /* No jump from outside this context context of identifiers with
6583 variably modified type, so record that there is a jump from
6584 within this context. */
6585 struct c_label_list *nlist;
6586 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6587 nlist->next = label_context_stack_vm->labels_used;
6588 nlist->label = decl;
6589 label_context_stack_vm->labels_used = nlist;
6592 TREE_USED (decl) = 1;
6593 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6596 /* Generate a computed goto statement to EXPR. */
6598 tree
6599 c_finish_goto_ptr (tree expr)
6601 if (pedantic)
6602 pedwarn ("ISO C forbids %<goto *expr;%>");
6603 expr = convert (ptr_type_node, expr);
6604 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6607 /* Generate a C `return' statement. RETVAL is the expression for what
6608 to return, or a null pointer for `return;' with no value. */
6610 tree
6611 c_finish_return (tree retval)
6613 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
6614 bool no_warning = false;
6616 if (TREE_THIS_VOLATILE (current_function_decl))
6617 warning ("function declared %<noreturn%> has a %<return%> statement");
6619 if (!retval)
6621 current_function_returns_null = 1;
6622 if ((warn_return_type || flag_isoc99)
6623 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6625 pedwarn_c99 ("%<return%> with no value, in "
6626 "function returning non-void");
6627 no_warning = true;
6630 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6632 current_function_returns_null = 1;
6633 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6634 pedwarn ("%<return%> with a value, in function returning void");
6636 else
6638 tree t = convert_for_assignment (valtype, retval, ic_return,
6639 NULL_TREE, NULL_TREE, 0);
6640 tree res = DECL_RESULT (current_function_decl);
6641 tree inner;
6643 current_function_returns_value = 1;
6644 if (t == error_mark_node)
6645 return NULL_TREE;
6647 inner = t = convert (TREE_TYPE (res), t);
6649 /* Strip any conversions, additions, and subtractions, and see if
6650 we are returning the address of a local variable. Warn if so. */
6651 while (1)
6653 switch (TREE_CODE (inner))
6655 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6656 case PLUS_EXPR:
6657 inner = TREE_OPERAND (inner, 0);
6658 continue;
6660 case MINUS_EXPR:
6661 /* If the second operand of the MINUS_EXPR has a pointer
6662 type (or is converted from it), this may be valid, so
6663 don't give a warning. */
6665 tree op1 = TREE_OPERAND (inner, 1);
6667 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6668 && (TREE_CODE (op1) == NOP_EXPR
6669 || TREE_CODE (op1) == NON_LVALUE_EXPR
6670 || TREE_CODE (op1) == CONVERT_EXPR))
6671 op1 = TREE_OPERAND (op1, 0);
6673 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6674 break;
6676 inner = TREE_OPERAND (inner, 0);
6677 continue;
6680 case ADDR_EXPR:
6681 inner = TREE_OPERAND (inner, 0);
6683 while (REFERENCE_CLASS_P (inner)
6684 && TREE_CODE (inner) != INDIRECT_REF)
6685 inner = TREE_OPERAND (inner, 0);
6687 if (DECL_P (inner)
6688 && !DECL_EXTERNAL (inner)
6689 && !TREE_STATIC (inner)
6690 && DECL_CONTEXT (inner) == current_function_decl)
6691 warning ("function returns address of local variable");
6692 break;
6694 default:
6695 break;
6698 break;
6701 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6704 ret_stmt = build_stmt (RETURN_EXPR, retval);
6705 TREE_NO_WARNING (ret_stmt) |= no_warning;
6706 return add_stmt (ret_stmt);
6709 struct c_switch {
6710 /* The SWITCH_STMT being built. */
6711 tree switch_stmt;
6713 /* The original type of the testing expression, i.e. before the
6714 default conversion is applied. */
6715 tree orig_type;
6717 /* A splay-tree mapping the low element of a case range to the high
6718 element, or NULL_TREE if there is no high element. Used to
6719 determine whether or not a new case label duplicates an old case
6720 label. We need a tree, rather than simply a hash table, because
6721 of the GNU case range extension. */
6722 splay_tree cases;
6724 /* Number of nested statement expressions within this switch
6725 statement; if nonzero, case and default labels may not
6726 appear. */
6727 unsigned int blocked_stmt_expr;
6729 /* Scope of outermost declarations of identifiers with variably
6730 modified type within this switch statement; if nonzero, case and
6731 default labels may not appear. */
6732 unsigned int blocked_vm;
6734 /* The next node on the stack. */
6735 struct c_switch *next;
6738 /* A stack of the currently active switch statements. The innermost
6739 switch statement is on the top of the stack. There is no need to
6740 mark the stack for garbage collection because it is only active
6741 during the processing of the body of a function, and we never
6742 collect at that point. */
6744 struct c_switch *c_switch_stack;
6746 /* Start a C switch statement, testing expression EXP. Return the new
6747 SWITCH_STMT. */
6749 tree
6750 c_start_case (tree exp)
6752 enum tree_code code;
6753 tree type, orig_type = error_mark_node;
6754 struct c_switch *cs;
6756 if (exp != error_mark_node)
6758 code = TREE_CODE (TREE_TYPE (exp));
6759 orig_type = TREE_TYPE (exp);
6761 if (!INTEGRAL_TYPE_P (orig_type)
6762 && code != ERROR_MARK)
6764 error ("switch quantity not an integer");
6765 exp = integer_zero_node;
6766 orig_type = error_mark_node;
6768 else
6770 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6772 if (warn_traditional && !in_system_header
6773 && (type == long_integer_type_node
6774 || type == long_unsigned_type_node))
6775 warning ("%<long%> switch expression not converted to "
6776 "%<int%> in ISO C");
6778 exp = default_conversion (exp);
6779 type = TREE_TYPE (exp);
6783 /* Add this new SWITCH_STMT to the stack. */
6784 cs = XNEW (struct c_switch);
6785 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6786 cs->orig_type = orig_type;
6787 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6788 cs->blocked_stmt_expr = 0;
6789 cs->blocked_vm = 0;
6790 cs->next = c_switch_stack;
6791 c_switch_stack = cs;
6793 return add_stmt (cs->switch_stmt);
6796 /* Process a case label. */
6798 tree
6799 do_case (tree low_value, tree high_value)
6801 tree label = NULL_TREE;
6803 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
6804 && !c_switch_stack->blocked_vm)
6806 label = c_add_case_label (c_switch_stack->cases,
6807 SWITCH_STMT_COND (c_switch_stack->switch_stmt),
6808 c_switch_stack->orig_type,
6809 low_value, high_value);
6810 if (label == error_mark_node)
6811 label = NULL_TREE;
6813 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
6815 if (low_value)
6816 error ("case label in statement expression not containing "
6817 "enclosing switch statement");
6818 else
6819 error ("%<default%> label in statement expression not containing "
6820 "enclosing switch statement");
6822 else if (c_switch_stack && c_switch_stack->blocked_vm)
6824 if (low_value)
6825 error ("case label in scope of identifier with variably modified "
6826 "type not containing enclosing switch statement");
6827 else
6828 error ("%<default%> label in scope of identifier with variably "
6829 "modified type not containing enclosing switch statement");
6831 else if (low_value)
6832 error ("case label not within a switch statement");
6833 else
6834 error ("%<default%> label not within a switch statement");
6836 return label;
6839 /* Finish the switch statement. */
6841 void
6842 c_finish_case (tree body)
6844 struct c_switch *cs = c_switch_stack;
6846 SWITCH_STMT_BODY (cs->switch_stmt) = body;
6848 /* We must not be within a statement expression nested in the switch
6849 at this point; we might, however, be within the scope of an
6850 identifier with variably modified type nested in the switch. */
6851 gcc_assert (!cs->blocked_stmt_expr);
6853 /* Emit warnings as needed. */
6854 c_do_switch_warnings (cs->cases, cs->switch_stmt);
6856 /* Pop the stack. */
6857 c_switch_stack = cs->next;
6858 splay_tree_delete (cs->cases);
6859 XDELETE (cs);
6862 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6863 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6864 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6865 statement, and was not surrounded with parenthesis. */
6867 void
6868 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6869 tree else_block, bool nested_if)
6871 tree stmt;
6873 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6874 if (warn_parentheses && nested_if && else_block == NULL)
6876 tree inner_if = then_block;
6878 /* We know from the grammar productions that there is an IF nested
6879 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6880 it might not be exactly THEN_BLOCK, but should be the last
6881 non-container statement within. */
6882 while (1)
6883 switch (TREE_CODE (inner_if))
6885 case COND_EXPR:
6886 goto found;
6887 case BIND_EXPR:
6888 inner_if = BIND_EXPR_BODY (inner_if);
6889 break;
6890 case STATEMENT_LIST:
6891 inner_if = expr_last (then_block);
6892 break;
6893 case TRY_FINALLY_EXPR:
6894 case TRY_CATCH_EXPR:
6895 inner_if = TREE_OPERAND (inner_if, 0);
6896 break;
6897 default:
6898 gcc_unreachable ();
6900 found:
6902 if (COND_EXPR_ELSE (inner_if))
6903 warning ("%Hsuggest explicit braces to avoid ambiguous %<else%>",
6904 &if_locus);
6907 /* Diagnose ";" via the special empty statement node that we create. */
6908 if (extra_warnings)
6910 tree *inner_then = &then_block, *inner_else = &else_block;
6912 if (TREE_CODE (*inner_then) == STATEMENT_LIST
6913 && STATEMENT_LIST_TAIL (*inner_then))
6914 inner_then = &STATEMENT_LIST_TAIL (*inner_then)->stmt;
6915 if (*inner_else && TREE_CODE (*inner_else) == STATEMENT_LIST
6916 && STATEMENT_LIST_TAIL (*inner_else))
6917 inner_else = &STATEMENT_LIST_TAIL (*inner_else)->stmt;
6919 if (TREE_CODE (*inner_then) == NOP_EXPR && !TREE_TYPE (*inner_then))
6921 if (!*inner_else)
6922 warning ("%Hempty body in an if-statement",
6923 EXPR_LOCUS (*inner_then));
6925 *inner_then = alloc_stmt_list ();
6927 if (*inner_else
6928 && TREE_CODE (*inner_else) == NOP_EXPR
6929 && !TREE_TYPE (*inner_else))
6931 warning ("%Hempty body in an else-statement",
6932 EXPR_LOCUS (*inner_else));
6934 *inner_else = alloc_stmt_list ();
6938 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6939 SET_EXPR_LOCATION (stmt, if_locus);
6940 add_stmt (stmt);
6943 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6944 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6945 is false for DO loops. INCR is the FOR increment expression. BODY is
6946 the statement controlled by the loop. BLAB is the break label. CLAB is
6947 the continue label. Everything is allowed to be NULL. */
6949 void
6950 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6951 tree blab, tree clab, bool cond_is_first)
6953 tree entry = NULL, exit = NULL, t;
6955 /* If the condition is zero don't generate a loop construct. */
6956 if (cond && integer_zerop (cond))
6958 if (cond_is_first)
6960 t = build_and_jump (&blab);
6961 SET_EXPR_LOCATION (t, start_locus);
6962 add_stmt (t);
6965 else
6967 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6969 /* If we have an exit condition, then we build an IF with gotos either
6970 out of the loop, or to the top of it. If there's no exit condition,
6971 then we just build a jump back to the top. */
6972 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6974 if (cond && !integer_nonzerop (cond))
6976 /* Canonicalize the loop condition to the end. This means
6977 generating a branch to the loop condition. Reuse the
6978 continue label, if possible. */
6979 if (cond_is_first)
6981 if (incr || !clab)
6983 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6984 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6986 else
6987 t = build1 (GOTO_EXPR, void_type_node, clab);
6988 SET_EXPR_LOCATION (t, start_locus);
6989 add_stmt (t);
6992 t = build_and_jump (&blab);
6993 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6994 exit = fold (exit);
6995 if (cond_is_first)
6996 SET_EXPR_LOCATION (exit, start_locus);
6997 else
6998 SET_EXPR_LOCATION (exit, input_location);
7001 add_stmt (top);
7004 if (body)
7005 add_stmt (body);
7006 if (clab)
7007 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
7008 if (incr)
7009 add_stmt (incr);
7010 if (entry)
7011 add_stmt (entry);
7012 if (exit)
7013 add_stmt (exit);
7014 if (blab)
7015 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
7018 tree
7019 c_finish_bc_stmt (tree *label_p, bool is_break)
7021 bool skip;
7022 tree label = *label_p;
7024 /* In switch statements break is sometimes stylistically used after
7025 a return statement. This can lead to spurious warnings about
7026 control reaching the end of a non-void function when it is
7027 inlined. Note that we are calling block_may_fallthru with
7028 language specific tree nodes; this works because
7029 block_may_fallthru returns true when given something it does not
7030 understand. */
7031 skip = !block_may_fallthru (cur_stmt_list);
7033 if (!label)
7035 if (!skip)
7036 *label_p = label = create_artificial_label ();
7038 else if (TREE_CODE (label) != LABEL_DECL)
7040 if (is_break)
7041 error ("break statement not within loop or switch");
7042 else
7043 error ("continue statement not within a loop");
7044 return NULL_TREE;
7047 if (skip)
7048 return NULL_TREE;
7050 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
7053 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
7055 static void
7056 emit_side_effect_warnings (tree expr)
7058 if (expr == error_mark_node)
7060 else if (!TREE_SIDE_EFFECTS (expr))
7062 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
7063 warning ("%Hstatement with no effect",
7064 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
7066 else if (warn_unused_value)
7067 warn_if_unused_value (expr, input_location);
7070 /* Process an expression as if it were a complete statement. Emit
7071 diagnostics, but do not call ADD_STMT. */
7073 tree
7074 c_process_expr_stmt (tree expr)
7076 if (!expr)
7077 return NULL_TREE;
7079 /* Do default conversion if safe and possibly important,
7080 in case within ({...}). */
7081 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
7082 && (flag_isoc99 || lvalue_p (expr)))
7083 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
7084 expr = default_conversion (expr);
7086 if (warn_sequence_point)
7087 verify_sequence_points (expr);
7089 if (TREE_TYPE (expr) != error_mark_node
7090 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7091 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7092 error ("expression statement has incomplete type");
7094 /* If we're not processing a statement expression, warn about unused values.
7095 Warnings for statement expressions will be emitted later, once we figure
7096 out which is the result. */
7097 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7098 && (extra_warnings || warn_unused_value))
7099 emit_side_effect_warnings (expr);
7101 /* If the expression is not of a type to which we cannot assign a line
7102 number, wrap the thing in a no-op NOP_EXPR. */
7103 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
7104 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7106 if (EXPR_P (expr))
7107 SET_EXPR_LOCATION (expr, input_location);
7109 return expr;
7112 /* Emit an expression as a statement. */
7114 tree
7115 c_finish_expr_stmt (tree expr)
7117 if (expr)
7118 return add_stmt (c_process_expr_stmt (expr));
7119 else
7120 return NULL;
7123 /* Do the opposite and emit a statement as an expression. To begin,
7124 create a new binding level and return it. */
7126 tree
7127 c_begin_stmt_expr (void)
7129 tree ret;
7130 struct c_label_context_se *nstack;
7131 struct c_label_list *glist;
7133 /* We must force a BLOCK for this level so that, if it is not expanded
7134 later, there is a way to turn off the entire subtree of blocks that
7135 are contained in it. */
7136 keep_next_level ();
7137 ret = c_begin_compound_stmt (true);
7138 if (c_switch_stack)
7140 c_switch_stack->blocked_stmt_expr++;
7141 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7143 for (glist = label_context_stack_se->labels_used;
7144 glist != NULL;
7145 glist = glist->next)
7147 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7149 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
7150 nstack->labels_def = NULL;
7151 nstack->labels_used = NULL;
7152 nstack->next = label_context_stack_se;
7153 label_context_stack_se = nstack;
7155 /* Mark the current statement list as belonging to a statement list. */
7156 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7158 return ret;
7161 tree
7162 c_finish_stmt_expr (tree body)
7164 tree last, type, tmp, val;
7165 tree *last_p;
7166 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7168 body = c_end_compound_stmt (body, true);
7169 if (c_switch_stack)
7171 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7172 c_switch_stack->blocked_stmt_expr--;
7174 /* It is no longer possible to jump to labels defined within this
7175 statement expression. */
7176 for (dlist = label_context_stack_se->labels_def;
7177 dlist != NULL;
7178 dlist = dlist->next)
7180 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7182 /* It is again possible to define labels with a goto just outside
7183 this statement expression. */
7184 for (glist = label_context_stack_se->next->labels_used;
7185 glist != NULL;
7186 glist = glist->next)
7188 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7189 glist_prev = glist;
7191 if (glist_prev != NULL)
7192 glist_prev->next = label_context_stack_se->labels_used;
7193 else
7194 label_context_stack_se->next->labels_used
7195 = label_context_stack_se->labels_used;
7196 label_context_stack_se = label_context_stack_se->next;
7198 /* Locate the last statement in BODY. See c_end_compound_stmt
7199 about always returning a BIND_EXPR. */
7200 last_p = &BIND_EXPR_BODY (body);
7201 last = BIND_EXPR_BODY (body);
7203 continue_searching:
7204 if (TREE_CODE (last) == STATEMENT_LIST)
7206 tree_stmt_iterator i;
7208 /* This can happen with degenerate cases like ({ }). No value. */
7209 if (!TREE_SIDE_EFFECTS (last))
7210 return body;
7212 /* If we're supposed to generate side effects warnings, process
7213 all of the statements except the last. */
7214 if (extra_warnings || warn_unused_value)
7216 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7217 emit_side_effect_warnings (tsi_stmt (i));
7219 else
7220 i = tsi_last (last);
7221 last_p = tsi_stmt_ptr (i);
7222 last = *last_p;
7225 /* If the end of the list is exception related, then the list was split
7226 by a call to push_cleanup. Continue searching. */
7227 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7228 || TREE_CODE (last) == TRY_CATCH_EXPR)
7230 last_p = &TREE_OPERAND (last, 0);
7231 last = *last_p;
7232 goto continue_searching;
7235 /* In the case that the BIND_EXPR is not necessary, return the
7236 expression out from inside it. */
7237 if (last == error_mark_node
7238 || (last == BIND_EXPR_BODY (body)
7239 && BIND_EXPR_VARS (body) == NULL))
7240 return last;
7242 /* Extract the type of said expression. */
7243 type = TREE_TYPE (last);
7245 /* If we're not returning a value at all, then the BIND_EXPR that
7246 we already have is a fine expression to return. */
7247 if (!type || VOID_TYPE_P (type))
7248 return body;
7250 /* Now that we've located the expression containing the value, it seems
7251 silly to make voidify_wrapper_expr repeat the process. Create a
7252 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7253 tmp = create_tmp_var_raw (type, NULL);
7255 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7256 tree_expr_nonnegative_p giving up immediately. */
7257 val = last;
7258 if (TREE_CODE (val) == NOP_EXPR
7259 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7260 val = TREE_OPERAND (val, 0);
7262 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7263 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7265 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7268 /* Begin the scope of an identifier of variably modified type, scope
7269 number SCOPE. Jumping from outside this scope to inside it is not
7270 permitted. */
7272 void
7273 c_begin_vm_scope (unsigned int scope)
7275 struct c_label_context_vm *nstack;
7276 struct c_label_list *glist;
7278 gcc_assert (scope > 0);
7279 if (c_switch_stack && !c_switch_stack->blocked_vm)
7280 c_switch_stack->blocked_vm = scope;
7281 for (glist = label_context_stack_vm->labels_used;
7282 glist != NULL;
7283 glist = glist->next)
7285 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
7287 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
7288 nstack->labels_def = NULL;
7289 nstack->labels_used = NULL;
7290 nstack->scope = scope;
7291 nstack->next = label_context_stack_vm;
7292 label_context_stack_vm = nstack;
7295 /* End a scope which may contain identifiers of variably modified
7296 type, scope number SCOPE. */
7298 void
7299 c_end_vm_scope (unsigned int scope)
7301 if (label_context_stack_vm == NULL)
7302 return;
7303 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
7304 c_switch_stack->blocked_vm = 0;
7305 /* We may have a number of nested scopes of identifiers with
7306 variably modified type, all at this depth. Pop each in turn. */
7307 while (label_context_stack_vm->scope == scope)
7309 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7311 /* It is no longer possible to jump to labels defined within this
7312 scope. */
7313 for (dlist = label_context_stack_vm->labels_def;
7314 dlist != NULL;
7315 dlist = dlist->next)
7317 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
7319 /* It is again possible to define labels with a goto just outside
7320 this scope. */
7321 for (glist = label_context_stack_vm->next->labels_used;
7322 glist != NULL;
7323 glist = glist->next)
7325 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
7326 glist_prev = glist;
7328 if (glist_prev != NULL)
7329 glist_prev->next = label_context_stack_vm->labels_used;
7330 else
7331 label_context_stack_vm->next->labels_used
7332 = label_context_stack_vm->labels_used;
7333 label_context_stack_vm = label_context_stack_vm->next;
7337 /* Begin and end compound statements. This is as simple as pushing
7338 and popping new statement lists from the tree. */
7340 tree
7341 c_begin_compound_stmt (bool do_scope)
7343 tree stmt = push_stmt_list ();
7344 if (do_scope)
7345 push_scope ();
7346 return stmt;
7349 tree
7350 c_end_compound_stmt (tree stmt, bool do_scope)
7352 tree block = NULL;
7354 if (do_scope)
7356 if (c_dialect_objc ())
7357 objc_clear_super_receiver ();
7358 block = pop_scope ();
7361 stmt = pop_stmt_list (stmt);
7362 stmt = c_build_bind_expr (block, stmt);
7364 /* If this compound statement is nested immediately inside a statement
7365 expression, then force a BIND_EXPR to be created. Otherwise we'll
7366 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7367 STATEMENT_LISTs merge, and thus we can lose track of what statement
7368 was really last. */
7369 if (cur_stmt_list
7370 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7371 && TREE_CODE (stmt) != BIND_EXPR)
7373 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7374 TREE_SIDE_EFFECTS (stmt) = 1;
7377 return stmt;
7380 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7381 when the current scope is exited. EH_ONLY is true when this is not
7382 meant to apply to normal control flow transfer. */
7384 void
7385 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7387 enum tree_code code;
7388 tree stmt, list;
7389 bool stmt_expr;
7391 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7392 stmt = build_stmt (code, NULL, cleanup);
7393 add_stmt (stmt);
7394 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7395 list = push_stmt_list ();
7396 TREE_OPERAND (stmt, 0) = list;
7397 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7400 /* Build a binary-operation expression without default conversions.
7401 CODE is the kind of expression to build.
7402 This function differs from `build' in several ways:
7403 the data type of the result is computed and recorded in it,
7404 warnings are generated if arg data types are invalid,
7405 special handling for addition and subtraction of pointers is known,
7406 and some optimization is done (operations on narrow ints
7407 are done in the narrower type when that gives the same result).
7408 Constant folding is also done before the result is returned.
7410 Note that the operands will never have enumeral types, or function
7411 or array types, because either they will have the default conversions
7412 performed or they have both just been converted to some other type in which
7413 the arithmetic is to be done. */
7415 tree
7416 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7417 int convert_p)
7419 tree type0, type1;
7420 enum tree_code code0, code1;
7421 tree op0, op1;
7423 /* Expression code to give to the expression when it is built.
7424 Normally this is CODE, which is what the caller asked for,
7425 but in some special cases we change it. */
7426 enum tree_code resultcode = code;
7428 /* Data type in which the computation is to be performed.
7429 In the simplest cases this is the common type of the arguments. */
7430 tree result_type = NULL;
7432 /* Nonzero means operands have already been type-converted
7433 in whatever way is necessary.
7434 Zero means they need to be converted to RESULT_TYPE. */
7435 int converted = 0;
7437 /* Nonzero means create the expression with this type, rather than
7438 RESULT_TYPE. */
7439 tree build_type = 0;
7441 /* Nonzero means after finally constructing the expression
7442 convert it to this type. */
7443 tree final_type = 0;
7445 /* Nonzero if this is an operation like MIN or MAX which can
7446 safely be computed in short if both args are promoted shorts.
7447 Also implies COMMON.
7448 -1 indicates a bitwise operation; this makes a difference
7449 in the exact conditions for when it is safe to do the operation
7450 in a narrower mode. */
7451 int shorten = 0;
7453 /* Nonzero if this is a comparison operation;
7454 if both args are promoted shorts, compare the original shorts.
7455 Also implies COMMON. */
7456 int short_compare = 0;
7458 /* Nonzero if this is a right-shift operation, which can be computed on the
7459 original short and then promoted if the operand is a promoted short. */
7460 int short_shift = 0;
7462 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7463 int common = 0;
7465 if (convert_p)
7467 op0 = default_conversion (orig_op0);
7468 op1 = default_conversion (orig_op1);
7470 else
7472 op0 = orig_op0;
7473 op1 = orig_op1;
7476 type0 = TREE_TYPE (op0);
7477 type1 = TREE_TYPE (op1);
7479 /* The expression codes of the data types of the arguments tell us
7480 whether the arguments are integers, floating, pointers, etc. */
7481 code0 = TREE_CODE (type0);
7482 code1 = TREE_CODE (type1);
7484 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7485 STRIP_TYPE_NOPS (op0);
7486 STRIP_TYPE_NOPS (op1);
7488 /* If an error was already reported for one of the arguments,
7489 avoid reporting another error. */
7491 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7492 return error_mark_node;
7494 switch (code)
7496 case PLUS_EXPR:
7497 /* Handle the pointer + int case. */
7498 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7499 return pointer_int_sum (PLUS_EXPR, op0, op1);
7500 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7501 return pointer_int_sum (PLUS_EXPR, op1, op0);
7502 else
7503 common = 1;
7504 break;
7506 case MINUS_EXPR:
7507 /* Subtraction of two similar pointers.
7508 We must subtract them as integers, then divide by object size. */
7509 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7510 && comp_target_types (type0, type1, 1))
7511 return pointer_diff (op0, op1);
7512 /* Handle pointer minus int. Just like pointer plus int. */
7513 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7514 return pointer_int_sum (MINUS_EXPR, op0, op1);
7515 else
7516 common = 1;
7517 break;
7519 case MULT_EXPR:
7520 common = 1;
7521 break;
7523 case TRUNC_DIV_EXPR:
7524 case CEIL_DIV_EXPR:
7525 case FLOOR_DIV_EXPR:
7526 case ROUND_DIV_EXPR:
7527 case EXACT_DIV_EXPR:
7528 /* Floating point division by zero is a legitimate way to obtain
7529 infinities and NaNs. */
7530 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7531 warning ("division by zero");
7533 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7534 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7535 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7536 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7538 enum tree_code tcode0 = code0, tcode1 = code1;
7540 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7541 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7542 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7543 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7545 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
7546 resultcode = RDIV_EXPR;
7547 else
7548 /* Although it would be tempting to shorten always here, that
7549 loses on some targets, since the modulo instruction is
7550 undefined if the quotient can't be represented in the
7551 computation mode. We shorten only if unsigned or if
7552 dividing by something we know != -1. */
7553 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7554 || (TREE_CODE (op1) == INTEGER_CST
7555 && !integer_all_onesp (op1)));
7556 common = 1;
7558 break;
7560 case BIT_AND_EXPR:
7561 case BIT_IOR_EXPR:
7562 case BIT_XOR_EXPR:
7563 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7564 shorten = -1;
7565 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7566 common = 1;
7567 break;
7569 case TRUNC_MOD_EXPR:
7570 case FLOOR_MOD_EXPR:
7571 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7572 warning ("division by zero");
7574 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7576 /* Although it would be tempting to shorten always here, that loses
7577 on some targets, since the modulo instruction is undefined if the
7578 quotient can't be represented in the computation mode. We shorten
7579 only if unsigned or if dividing by something we know != -1. */
7580 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7581 || (TREE_CODE (op1) == INTEGER_CST
7582 && !integer_all_onesp (op1)));
7583 common = 1;
7585 break;
7587 case TRUTH_ANDIF_EXPR:
7588 case TRUTH_ORIF_EXPR:
7589 case TRUTH_AND_EXPR:
7590 case TRUTH_OR_EXPR:
7591 case TRUTH_XOR_EXPR:
7592 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7593 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7594 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7595 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7597 /* Result of these operations is always an int,
7598 but that does not mean the operands should be
7599 converted to ints! */
7600 result_type = integer_type_node;
7601 op0 = lang_hooks.truthvalue_conversion (op0);
7602 op1 = lang_hooks.truthvalue_conversion (op1);
7603 converted = 1;
7605 break;
7607 /* Shift operations: result has same type as first operand;
7608 always convert second operand to int.
7609 Also set SHORT_SHIFT if shifting rightward. */
7611 case RSHIFT_EXPR:
7612 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7614 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7616 if (tree_int_cst_sgn (op1) < 0)
7617 warning ("right shift count is negative");
7618 else
7620 if (!integer_zerop (op1))
7621 short_shift = 1;
7623 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7624 warning ("right shift count >= width of type");
7628 /* Use the type of the value to be shifted. */
7629 result_type = type0;
7630 /* Convert the shift-count to an integer, regardless of size
7631 of value being shifted. */
7632 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7633 op1 = convert (integer_type_node, op1);
7634 /* Avoid converting op1 to result_type later. */
7635 converted = 1;
7637 break;
7639 case LSHIFT_EXPR:
7640 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7642 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7644 if (tree_int_cst_sgn (op1) < 0)
7645 warning ("left shift count is negative");
7647 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7648 warning ("left shift count >= width of type");
7651 /* Use the type of the value to be shifted. */
7652 result_type = type0;
7653 /* Convert the shift-count to an integer, regardless of size
7654 of value being shifted. */
7655 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7656 op1 = convert (integer_type_node, op1);
7657 /* Avoid converting op1 to result_type later. */
7658 converted = 1;
7660 break;
7662 case EQ_EXPR:
7663 case NE_EXPR:
7664 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7665 warning ("comparing floating point with == or != is unsafe");
7666 /* Result of comparison is always int,
7667 but don't convert the args to int! */
7668 build_type = integer_type_node;
7669 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7670 || code0 == COMPLEX_TYPE)
7671 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7672 || code1 == COMPLEX_TYPE))
7673 short_compare = 1;
7674 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7676 tree tt0 = TREE_TYPE (type0);
7677 tree tt1 = TREE_TYPE (type1);
7678 /* Anything compares with void *. void * compares with anything.
7679 Otherwise, the targets must be compatible
7680 and both must be object or both incomplete. */
7681 if (comp_target_types (type0, type1, 1))
7682 result_type = common_pointer_type (type0, type1);
7683 else if (VOID_TYPE_P (tt0))
7685 /* op0 != orig_op0 detects the case of something
7686 whose value is 0 but which isn't a valid null ptr const. */
7687 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7688 && TREE_CODE (tt1) == FUNCTION_TYPE)
7689 pedwarn ("ISO C forbids comparison of %<void *%>"
7690 " with function pointer");
7692 else if (VOID_TYPE_P (tt1))
7694 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7695 && TREE_CODE (tt0) == FUNCTION_TYPE)
7696 pedwarn ("ISO C forbids comparison of %<void *%>"
7697 " with function pointer");
7699 else
7700 pedwarn ("comparison of distinct pointer types lacks a cast");
7702 if (result_type == NULL_TREE)
7703 result_type = ptr_type_node;
7705 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7706 && integer_zerop (op1))
7707 result_type = type0;
7708 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7709 && integer_zerop (op0))
7710 result_type = type1;
7711 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7713 result_type = type0;
7714 pedwarn ("comparison between pointer and integer");
7716 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7718 result_type = type1;
7719 pedwarn ("comparison between pointer and integer");
7721 break;
7723 case LE_EXPR:
7724 case GE_EXPR:
7725 case LT_EXPR:
7726 case GT_EXPR:
7727 build_type = integer_type_node;
7728 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7729 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7730 short_compare = 1;
7731 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7733 if (comp_target_types (type0, type1, 1))
7735 result_type = common_pointer_type (type0, type1);
7736 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7737 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7738 pedwarn ("comparison of complete and incomplete pointers");
7739 else if (pedantic
7740 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7741 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7743 else
7745 result_type = ptr_type_node;
7746 pedwarn ("comparison of distinct pointer types lacks a cast");
7749 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7750 && integer_zerop (op1))
7752 result_type = type0;
7753 if (pedantic || extra_warnings)
7754 pedwarn ("ordered comparison of pointer with integer zero");
7756 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7757 && integer_zerop (op0))
7759 result_type = type1;
7760 if (pedantic)
7761 pedwarn ("ordered comparison of pointer with integer zero");
7763 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7765 result_type = type0;
7766 pedwarn ("comparison between pointer and integer");
7768 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7770 result_type = type1;
7771 pedwarn ("comparison between pointer and integer");
7773 break;
7775 default:
7776 gcc_unreachable ();
7779 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7780 return error_mark_node;
7782 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
7783 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
7784 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
7785 TREE_TYPE (type1))))
7787 binary_op_error (code);
7788 return error_mark_node;
7791 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7792 || code0 == VECTOR_TYPE)
7794 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7795 || code1 == VECTOR_TYPE))
7797 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7799 if (shorten || common || short_compare)
7800 result_type = c_common_type (type0, type1);
7802 /* For certain operations (which identify themselves by shorten != 0)
7803 if both args were extended from the same smaller type,
7804 do the arithmetic in that type and then extend.
7806 shorten !=0 and !=1 indicates a bitwise operation.
7807 For them, this optimization is safe only if
7808 both args are zero-extended or both are sign-extended.
7809 Otherwise, we might change the result.
7810 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7811 but calculated in (unsigned short) it would be (unsigned short)-1. */
7813 if (shorten && none_complex)
7815 int unsigned0, unsigned1;
7816 tree arg0 = get_narrower (op0, &unsigned0);
7817 tree arg1 = get_narrower (op1, &unsigned1);
7818 /* UNS is 1 if the operation to be done is an unsigned one. */
7819 int uns = TYPE_UNSIGNED (result_type);
7820 tree type;
7822 final_type = result_type;
7824 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7825 but it *requires* conversion to FINAL_TYPE. */
7827 if ((TYPE_PRECISION (TREE_TYPE (op0))
7828 == TYPE_PRECISION (TREE_TYPE (arg0)))
7829 && TREE_TYPE (op0) != final_type)
7830 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7831 if ((TYPE_PRECISION (TREE_TYPE (op1))
7832 == TYPE_PRECISION (TREE_TYPE (arg1)))
7833 && TREE_TYPE (op1) != final_type)
7834 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7836 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7838 /* For bitwise operations, signedness of nominal type
7839 does not matter. Consider only how operands were extended. */
7840 if (shorten == -1)
7841 uns = unsigned0;
7843 /* Note that in all three cases below we refrain from optimizing
7844 an unsigned operation on sign-extended args.
7845 That would not be valid. */
7847 /* Both args variable: if both extended in same way
7848 from same width, do it in that width.
7849 Do it unsigned if args were zero-extended. */
7850 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7851 < TYPE_PRECISION (result_type))
7852 && (TYPE_PRECISION (TREE_TYPE (arg1))
7853 == TYPE_PRECISION (TREE_TYPE (arg0)))
7854 && unsigned0 == unsigned1
7855 && (unsigned0 || !uns))
7856 result_type
7857 = c_common_signed_or_unsigned_type
7858 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7859 else if (TREE_CODE (arg0) == INTEGER_CST
7860 && (unsigned1 || !uns)
7861 && (TYPE_PRECISION (TREE_TYPE (arg1))
7862 < TYPE_PRECISION (result_type))
7863 && (type
7864 = c_common_signed_or_unsigned_type (unsigned1,
7865 TREE_TYPE (arg1)),
7866 int_fits_type_p (arg0, type)))
7867 result_type = type;
7868 else if (TREE_CODE (arg1) == INTEGER_CST
7869 && (unsigned0 || !uns)
7870 && (TYPE_PRECISION (TREE_TYPE (arg0))
7871 < TYPE_PRECISION (result_type))
7872 && (type
7873 = c_common_signed_or_unsigned_type (unsigned0,
7874 TREE_TYPE (arg0)),
7875 int_fits_type_p (arg1, type)))
7876 result_type = type;
7879 /* Shifts can be shortened if shifting right. */
7881 if (short_shift)
7883 int unsigned_arg;
7884 tree arg0 = get_narrower (op0, &unsigned_arg);
7886 final_type = result_type;
7888 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7889 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7891 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7892 /* We can shorten only if the shift count is less than the
7893 number of bits in the smaller type size. */
7894 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7895 /* We cannot drop an unsigned shift after sign-extension. */
7896 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7898 /* Do an unsigned shift if the operand was zero-extended. */
7899 result_type
7900 = c_common_signed_or_unsigned_type (unsigned_arg,
7901 TREE_TYPE (arg0));
7902 /* Convert value-to-be-shifted to that type. */
7903 if (TREE_TYPE (op0) != result_type)
7904 op0 = convert (result_type, op0);
7905 converted = 1;
7909 /* Comparison operations are shortened too but differently.
7910 They identify themselves by setting short_compare = 1. */
7912 if (short_compare)
7914 /* Don't write &op0, etc., because that would prevent op0
7915 from being kept in a register.
7916 Instead, make copies of the our local variables and
7917 pass the copies by reference, then copy them back afterward. */
7918 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7919 enum tree_code xresultcode = resultcode;
7920 tree val
7921 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7923 if (val != 0)
7924 return val;
7926 op0 = xop0, op1 = xop1;
7927 converted = 1;
7928 resultcode = xresultcode;
7930 if (warn_sign_compare && skip_evaluation == 0)
7932 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7933 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7934 int unsignedp0, unsignedp1;
7935 tree primop0 = get_narrower (op0, &unsignedp0);
7936 tree primop1 = get_narrower (op1, &unsignedp1);
7938 xop0 = orig_op0;
7939 xop1 = orig_op1;
7940 STRIP_TYPE_NOPS (xop0);
7941 STRIP_TYPE_NOPS (xop1);
7943 /* Give warnings for comparisons between signed and unsigned
7944 quantities that may fail.
7946 Do the checking based on the original operand trees, so that
7947 casts will be considered, but default promotions won't be.
7949 Do not warn if the comparison is being done in a signed type,
7950 since the signed type will only be chosen if it can represent
7951 all the values of the unsigned type. */
7952 if (!TYPE_UNSIGNED (result_type))
7953 /* OK */;
7954 /* Do not warn if both operands are the same signedness. */
7955 else if (op0_signed == op1_signed)
7956 /* OK */;
7957 else
7959 tree sop, uop;
7961 if (op0_signed)
7962 sop = xop0, uop = xop1;
7963 else
7964 sop = xop1, uop = xop0;
7966 /* Do not warn if the signed quantity is an
7967 unsuffixed integer literal (or some static
7968 constant expression involving such literals or a
7969 conditional expression involving such literals)
7970 and it is non-negative. */
7971 if (tree_expr_nonnegative_p (sop))
7972 /* OK */;
7973 /* Do not warn if the comparison is an equality operation,
7974 the unsigned quantity is an integral constant, and it
7975 would fit in the result if the result were signed. */
7976 else if (TREE_CODE (uop) == INTEGER_CST
7977 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7978 && int_fits_type_p
7979 (uop, c_common_signed_type (result_type)))
7980 /* OK */;
7981 /* Do not warn if the unsigned quantity is an enumeration
7982 constant and its maximum value would fit in the result
7983 if the result were signed. */
7984 else if (TREE_CODE (uop) == INTEGER_CST
7985 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7986 && int_fits_type_p
7987 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
7988 c_common_signed_type (result_type)))
7989 /* OK */;
7990 else
7991 warning ("comparison between signed and unsigned");
7994 /* Warn if two unsigned values are being compared in a size
7995 larger than their original size, and one (and only one) is the
7996 result of a `~' operator. This comparison will always fail.
7998 Also warn if one operand is a constant, and the constant
7999 does not have all bits set that are set in the ~ operand
8000 when it is extended. */
8002 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
8003 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
8005 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
8006 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
8007 &unsignedp0);
8008 else
8009 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
8010 &unsignedp1);
8012 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
8014 tree primop;
8015 HOST_WIDE_INT constant, mask;
8016 int unsignedp, bits;
8018 if (host_integerp (primop0, 0))
8020 primop = primop1;
8021 unsignedp = unsignedp1;
8022 constant = tree_low_cst (primop0, 0);
8024 else
8026 primop = primop0;
8027 unsignedp = unsignedp0;
8028 constant = tree_low_cst (primop1, 0);
8031 bits = TYPE_PRECISION (TREE_TYPE (primop));
8032 if (bits < TYPE_PRECISION (result_type)
8033 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
8035 mask = (~(HOST_WIDE_INT) 0) << bits;
8036 if ((mask & constant) != mask)
8037 warning ("comparison of promoted ~unsigned with constant");
8040 else if (unsignedp0 && unsignedp1
8041 && (TYPE_PRECISION (TREE_TYPE (primop0))
8042 < TYPE_PRECISION (result_type))
8043 && (TYPE_PRECISION (TREE_TYPE (primop1))
8044 < TYPE_PRECISION (result_type)))
8045 warning ("comparison of promoted ~unsigned with unsigned");
8051 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
8052 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
8053 Then the expression will be built.
8054 It will be given type FINAL_TYPE if that is nonzero;
8055 otherwise, it will be given type RESULT_TYPE. */
8057 if (!result_type)
8059 binary_op_error (code);
8060 return error_mark_node;
8063 if (!converted)
8065 if (TREE_TYPE (op0) != result_type)
8066 op0 = convert (result_type, op0);
8067 if (TREE_TYPE (op1) != result_type)
8068 op1 = convert (result_type, op1);
8070 /* This can happen if one operand has a vector type, and the other
8071 has a different type. */
8072 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
8073 return error_mark_node;
8076 if (build_type == NULL_TREE)
8077 build_type = result_type;
8080 tree result = build2 (resultcode, build_type, op0, op1);
8082 /* Treat expressions in initializers specially as they can't trap. */
8083 result = require_constant_value ? fold_initializer (result)
8084 : fold (result);
8086 if (final_type != 0)
8087 result = convert (final_type, result);
8088 return result;
8093 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
8094 required. */
8096 tree
8097 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED,
8098 bool *ti ATTRIBUTE_UNUSED, bool *se)
8100 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
8102 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
8103 /* Executing a compound literal inside a function reinitializes
8104 it. */
8105 if (!TREE_STATIC (decl))
8106 *se = true;
8107 return decl;
8109 else
8110 return expr;