2005-04-29 Jim Tison <jtison@us.ibm.com>
[official-gcc.git] / gcc / c-typeck.c
blob3a5d11d1f421b7ac0286c17f57f2c30efbb3999f
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 void push_array_bounds (int);
93 static int spelling_length (void);
94 static char *print_spelling (char *);
95 static void warning_init (const char *);
96 static tree digest_init (tree, tree, bool, int);
97 static void output_init_element (tree, bool, tree, tree, int);
98 static void output_pending_init_elements (int);
99 static int set_designator (int);
100 static void push_range_stack (tree);
101 static void add_pending_init (tree, tree);
102 static void set_nonincremental_init (void);
103 static void set_nonincremental_init_from_string (tree);
104 static tree find_init_member (tree);
105 static void readonly_error (tree, enum lvalue_use);
106 static int lvalue_or_else (tree, enum lvalue_use);
107 static int lvalue_p (tree);
108 static void record_maybe_used_decl (tree);
110 /* Do `exp = require_complete_type (exp);' to make sure exp
111 does not have an incomplete type. (That includes void types.) */
113 tree
114 require_complete_type (tree value)
116 tree type = TREE_TYPE (value);
118 if (value == error_mark_node || type == error_mark_node)
119 return error_mark_node;
121 /* First, detect a valid value with a complete type. */
122 if (COMPLETE_TYPE_P (type))
123 return value;
125 c_incomplete_type_error (value, type);
126 return error_mark_node;
129 /* Print an error message for invalid use of an incomplete type.
130 VALUE is the expression that was used (or 0 if that isn't known)
131 and TYPE is the type that was invalid. */
133 void
134 c_incomplete_type_error (tree value, tree type)
136 const char *type_code_string;
138 /* Avoid duplicate error message. */
139 if (TREE_CODE (type) == ERROR_MARK)
140 return;
142 if (value != 0 && (TREE_CODE (value) == VAR_DECL
143 || TREE_CODE (value) == PARM_DECL))
144 error ("%qD has an incomplete type", value);
145 else
147 retry:
148 /* We must print an error message. Be clever about what it says. */
150 switch (TREE_CODE (type))
152 case RECORD_TYPE:
153 type_code_string = "struct";
154 break;
156 case UNION_TYPE:
157 type_code_string = "union";
158 break;
160 case ENUMERAL_TYPE:
161 type_code_string = "enum";
162 break;
164 case VOID_TYPE:
165 error ("invalid use of void expression");
166 return;
168 case ARRAY_TYPE:
169 if (TYPE_DOMAIN (type))
171 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
173 error ("invalid use of flexible array member");
174 return;
176 type = TREE_TYPE (type);
177 goto retry;
179 error ("invalid use of array with unspecified bounds");
180 return;
182 default:
183 gcc_unreachable ();
186 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
187 error ("invalid use of undefined type %<%s %E%>",
188 type_code_string, TYPE_NAME (type));
189 else
190 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
191 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
195 /* Given a type, apply default promotions wrt unnamed function
196 arguments and return the new type. */
198 tree
199 c_type_promotes_to (tree type)
201 if (TYPE_MAIN_VARIANT (type) == float_type_node)
202 return double_type_node;
204 if (c_promoting_integer_type_p (type))
206 /* Preserve unsignedness if not really getting any wider. */
207 if (TYPE_UNSIGNED (type)
208 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
209 return unsigned_type_node;
210 return integer_type_node;
213 return type;
216 /* Return a variant of TYPE which has all the type qualifiers of LIKE
217 as well as those of TYPE. */
219 static tree
220 qualify_type (tree type, tree like)
222 return c_build_qualified_type (type,
223 TYPE_QUALS (type) | TYPE_QUALS (like));
226 /* Return the composite type of two compatible types.
228 We assume that comptypes has already been done and returned
229 nonzero; if that isn't so, this may crash. In particular, we
230 assume that qualifiers match. */
232 tree
233 composite_type (tree t1, tree t2)
235 enum tree_code code1;
236 enum tree_code code2;
237 tree attributes;
239 /* Save time if the two types are the same. */
241 if (t1 == t2) return t1;
243 /* If one type is nonsense, use the other. */
244 if (t1 == error_mark_node)
245 return t2;
246 if (t2 == error_mark_node)
247 return t1;
249 code1 = TREE_CODE (t1);
250 code2 = TREE_CODE (t2);
252 /* Merge the attributes. */
253 attributes = targetm.merge_type_attributes (t1, t2);
255 /* If one is an enumerated type and the other is the compatible
256 integer type, the composite type might be either of the two
257 (DR#013 question 3). For consistency, use the enumerated type as
258 the composite type. */
260 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
261 return t1;
262 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
263 return t2;
265 gcc_assert (code1 == code2);
267 switch (code1)
269 case POINTER_TYPE:
270 /* For two pointers, do this recursively on the target type. */
272 tree pointed_to_1 = TREE_TYPE (t1);
273 tree pointed_to_2 = TREE_TYPE (t2);
274 tree target = composite_type (pointed_to_1, pointed_to_2);
275 t1 = build_pointer_type (target);
276 t1 = build_type_attribute_variant (t1, attributes);
277 return qualify_type (t1, t2);
280 case ARRAY_TYPE:
282 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
283 int quals;
284 tree unqual_elt;
286 /* We should not have any type quals on arrays at all. */
287 gcc_assert (!TYPE_QUALS (t1) && !TYPE_QUALS (t2));
289 /* Save space: see if the result is identical to one of the args. */
290 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
291 return build_type_attribute_variant (t1, attributes);
292 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
293 return build_type_attribute_variant (t2, attributes);
295 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
296 return build_type_attribute_variant (t1, attributes);
297 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
298 return build_type_attribute_variant (t2, attributes);
300 /* Merge the element types, and have a size if either arg has
301 one. We may have qualifiers on the element types. To set
302 up TYPE_MAIN_VARIANT correctly, we need to form the
303 composite of the unqualified types and add the qualifiers
304 back at the end. */
305 quals = TYPE_QUALS (strip_array_types (elt));
306 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
307 t1 = build_array_type (unqual_elt,
308 TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
309 t1 = c_build_qualified_type (t1, quals);
310 return build_type_attribute_variant (t1, attributes);
313 case FUNCTION_TYPE:
314 /* Function types: prefer the one that specified arg types.
315 If both do, merge the arg types. Also merge the return types. */
317 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
318 tree p1 = TYPE_ARG_TYPES (t1);
319 tree p2 = TYPE_ARG_TYPES (t2);
320 int len;
321 tree newargs, n;
322 int i;
324 /* Save space: see if the result is identical to one of the args. */
325 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
326 return build_type_attribute_variant (t1, attributes);
327 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
328 return build_type_attribute_variant (t2, attributes);
330 /* Simple way if one arg fails to specify argument types. */
331 if (TYPE_ARG_TYPES (t1) == 0)
333 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
334 t1 = build_type_attribute_variant (t1, attributes);
335 return qualify_type (t1, t2);
337 if (TYPE_ARG_TYPES (t2) == 0)
339 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
340 t1 = build_type_attribute_variant (t1, attributes);
341 return qualify_type (t1, t2);
344 /* If both args specify argument types, we must merge the two
345 lists, argument by argument. */
346 /* Tell global_bindings_p to return false so that variable_size
347 doesn't die on VLAs in parameter types. */
348 c_override_global_bindings_to_false = true;
350 len = list_length (p1);
351 newargs = 0;
353 for (i = 0; i < len; i++)
354 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
356 n = newargs;
358 for (; p1;
359 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
361 /* A null type means arg type is not specified.
362 Take whatever the other function type has. */
363 if (TREE_VALUE (p1) == 0)
365 TREE_VALUE (n) = TREE_VALUE (p2);
366 goto parm_done;
368 if (TREE_VALUE (p2) == 0)
370 TREE_VALUE (n) = TREE_VALUE (p1);
371 goto parm_done;
374 /* Given wait (union {union wait *u; int *i} *)
375 and wait (union wait *),
376 prefer union wait * as type of parm. */
377 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
378 && TREE_VALUE (p1) != TREE_VALUE (p2))
380 tree memb;
381 tree mv2 = TREE_VALUE (p2);
382 if (mv2 && mv2 != error_mark_node
383 && TREE_CODE (mv2) != ARRAY_TYPE)
384 mv2 = TYPE_MAIN_VARIANT (mv2);
385 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
386 memb; memb = TREE_CHAIN (memb))
388 tree mv3 = TREE_TYPE (memb);
389 if (mv3 && mv3 != error_mark_node
390 && TREE_CODE (mv3) != ARRAY_TYPE)
391 mv3 = TYPE_MAIN_VARIANT (mv3);
392 if (comptypes (mv3, mv2))
394 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
395 TREE_VALUE (p2));
396 if (pedantic)
397 pedwarn ("function types not truly compatible in ISO C");
398 goto parm_done;
402 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
403 && TREE_VALUE (p2) != TREE_VALUE (p1))
405 tree memb;
406 tree mv1 = TREE_VALUE (p1);
407 if (mv1 && mv1 != error_mark_node
408 && TREE_CODE (mv1) != ARRAY_TYPE)
409 mv1 = TYPE_MAIN_VARIANT (mv1);
410 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
411 memb; memb = TREE_CHAIN (memb))
413 tree mv3 = TREE_TYPE (memb);
414 if (mv3 && mv3 != error_mark_node
415 && TREE_CODE (mv3) != ARRAY_TYPE)
416 mv3 = TYPE_MAIN_VARIANT (mv3);
417 if (comptypes (mv3, mv1))
419 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
420 TREE_VALUE (p1));
421 if (pedantic)
422 pedwarn ("function types not truly compatible in ISO C");
423 goto parm_done;
427 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
428 parm_done: ;
431 c_override_global_bindings_to_false = false;
432 t1 = build_function_type (valtype, newargs);
433 t1 = qualify_type (t1, t2);
434 /* ... falls through ... */
437 default:
438 return build_type_attribute_variant (t1, attributes);
443 /* Return the type of a conditional expression between pointers to
444 possibly differently qualified versions of compatible types.
446 We assume that comp_target_types has already been done and returned
447 nonzero; if that isn't so, this may crash. */
449 static tree
450 common_pointer_type (tree t1, tree t2)
452 tree attributes;
453 tree pointed_to_1, mv1;
454 tree pointed_to_2, mv2;
455 tree target;
457 /* Save time if the two types are the same. */
459 if (t1 == t2) return t1;
461 /* If one type is nonsense, use the other. */
462 if (t1 == error_mark_node)
463 return t2;
464 if (t2 == error_mark_node)
465 return t1;
467 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
468 && TREE_CODE (t2) == POINTER_TYPE);
470 /* Merge the attributes. */
471 attributes = targetm.merge_type_attributes (t1, t2);
473 /* Find the composite type of the target types, and combine the
474 qualifiers of the two types' targets. Do not lose qualifiers on
475 array element types by taking the TYPE_MAIN_VARIANT. */
476 mv1 = pointed_to_1 = TREE_TYPE (t1);
477 mv2 = pointed_to_2 = TREE_TYPE (t2);
478 if (TREE_CODE (mv1) != ARRAY_TYPE)
479 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
480 if (TREE_CODE (mv2) != ARRAY_TYPE)
481 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
482 target = composite_type (mv1, mv2);
483 t1 = build_pointer_type (c_build_qualified_type
484 (target,
485 TYPE_QUALS (pointed_to_1) |
486 TYPE_QUALS (pointed_to_2)));
487 return build_type_attribute_variant (t1, attributes);
490 /* Return the common type for two arithmetic types under the usual
491 arithmetic conversions. The default conversions have already been
492 applied, and enumerated types converted to their compatible integer
493 types. The resulting type is unqualified and has no attributes.
495 This is the type for the result of most arithmetic operations
496 if the operands have the given two types. */
498 static tree
499 c_common_type (tree t1, tree t2)
501 enum tree_code code1;
502 enum tree_code code2;
504 /* If one type is nonsense, use the other. */
505 if (t1 == error_mark_node)
506 return t2;
507 if (t2 == error_mark_node)
508 return t1;
510 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
511 t1 = TYPE_MAIN_VARIANT (t1);
513 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
514 t2 = TYPE_MAIN_VARIANT (t2);
516 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
517 t1 = build_type_attribute_variant (t1, NULL_TREE);
519 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
520 t2 = build_type_attribute_variant (t2, NULL_TREE);
522 /* Save time if the two types are the same. */
524 if (t1 == t2) return t1;
526 code1 = TREE_CODE (t1);
527 code2 = TREE_CODE (t2);
529 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
530 || code1 == REAL_TYPE || code1 == INTEGER_TYPE);
531 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
532 || code2 == REAL_TYPE || code2 == INTEGER_TYPE);
534 /* If one type is a vector type, return that type. (How the usual
535 arithmetic conversions apply to the vector types extension is not
536 precisely specified.) */
537 if (code1 == VECTOR_TYPE)
538 return t1;
540 if (code2 == VECTOR_TYPE)
541 return t2;
543 /* If one type is complex, form the common type of the non-complex
544 components, then make that complex. Use T1 or T2 if it is the
545 required type. */
546 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
548 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
549 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
550 tree subtype = c_common_type (subtype1, subtype2);
552 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
553 return t1;
554 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
555 return t2;
556 else
557 return build_complex_type (subtype);
560 /* If only one is real, use it as the result. */
562 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
563 return t1;
565 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
566 return t2;
568 /* Both real or both integers; use the one with greater precision. */
570 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
571 return t1;
572 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
573 return t2;
575 /* Same precision. Prefer long longs to longs to ints when the
576 same precision, following the C99 rules on integer type rank
577 (which are equivalent to the C90 rules for C90 types). */
579 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
580 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
581 return long_long_unsigned_type_node;
583 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
584 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
586 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
587 return long_long_unsigned_type_node;
588 else
589 return long_long_integer_type_node;
592 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
593 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
594 return long_unsigned_type_node;
596 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
597 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
599 /* But preserve unsignedness from the other type,
600 since long cannot hold all the values of an unsigned int. */
601 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
602 return long_unsigned_type_node;
603 else
604 return long_integer_type_node;
607 /* Likewise, prefer long double to double even if same size. */
608 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
609 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
610 return long_double_type_node;
612 /* Otherwise prefer the unsigned one. */
614 if (TYPE_UNSIGNED (t1))
615 return t1;
616 else
617 return t2;
620 /* Wrapper around c_common_type that is used by c-common.c. 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 () && (val = objc_comptypes (t1, t2, 0)) >= 0)
703 break;
704 /* Do not remove mode or aliasing information. */
705 if (TYPE_MODE (t1) != TYPE_MODE (t2)
706 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
707 break;
708 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
709 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2)));
710 break;
712 case FUNCTION_TYPE:
713 val = function_types_compatible_p (t1, t2);
714 break;
716 case ARRAY_TYPE:
718 tree d1 = TYPE_DOMAIN (t1);
719 tree d2 = TYPE_DOMAIN (t2);
720 bool d1_variable, d2_variable;
721 bool d1_zero, d2_zero;
722 val = 1;
724 /* Target types must match incl. qualifiers. */
725 if (TREE_TYPE (t1) != TREE_TYPE (t2)
726 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2))))
727 return 0;
729 /* Sizes must match unless one is missing or variable. */
730 if (d1 == 0 || d2 == 0 || d1 == d2)
731 break;
733 d1_zero = !TYPE_MAX_VALUE (d1);
734 d2_zero = !TYPE_MAX_VALUE (d2);
736 d1_variable = (!d1_zero
737 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
738 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
739 d2_variable = (!d2_zero
740 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
741 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
743 if (d1_variable || d2_variable)
744 break;
745 if (d1_zero && d2_zero)
746 break;
747 if (d1_zero || d2_zero
748 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
749 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
750 val = 0;
752 break;
755 case RECORD_TYPE:
756 /* We are dealing with two distinct structs. In assorted Objective-C
757 corner cases, however, these can still be deemed equivalent. */
758 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
759 val = 1;
761 case ENUMERAL_TYPE:
762 case UNION_TYPE:
763 if (val != 1 && !same_translation_unit_p (t1, t2))
764 val = tagged_types_tu_compatible_p (t1, t2);
765 break;
767 case VECTOR_TYPE:
768 val = TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
769 && comptypes (TREE_TYPE (t1), TREE_TYPE (t2));
770 break;
772 default:
773 break;
775 return attrval == 2 && val == 1 ? 2 : val;
778 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
779 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
780 to 1 or 0 depending if the check of the pointer types is meant to
781 be reflexive or not (typically, assignments are not reflexive,
782 while comparisons are reflexive).
785 static int
786 comp_target_types (tree ttl, tree ttr, int reflexive)
788 int val;
789 tree mvl, mvr;
791 /* Give objc_comptypes a crack at letting these types through. */
792 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
793 return val;
795 /* Do not lose qualifiers on element types of array types that are
796 pointer targets by taking their TYPE_MAIN_VARIANT. */
797 mvl = TREE_TYPE (ttl);
798 mvr = TREE_TYPE (ttr);
799 if (TREE_CODE (mvl) != ARRAY_TYPE)
800 mvl = TYPE_MAIN_VARIANT (mvl);
801 if (TREE_CODE (mvr) != ARRAY_TYPE)
802 mvr = TYPE_MAIN_VARIANT (mvr);
803 val = comptypes (mvl, mvr);
805 if (val == 2 && pedantic)
806 pedwarn ("types are not quite compatible");
807 return val;
810 /* Subroutines of `comptypes'. */
812 /* Determine whether two trees derive from the same translation unit.
813 If the CONTEXT chain ends in a null, that tree's context is still
814 being parsed, so if two trees have context chains ending in null,
815 they're in the same translation unit. */
817 same_translation_unit_p (tree t1, tree t2)
819 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
820 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
822 case tcc_declaration:
823 t1 = DECL_CONTEXT (t1); break;
824 case tcc_type:
825 t1 = TYPE_CONTEXT (t1); break;
826 case tcc_exceptional:
827 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
828 default: gcc_unreachable ();
831 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
832 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
834 case tcc_declaration:
835 t2 = DECL_CONTEXT (t2); break;
836 case tcc_type:
837 t2 = TYPE_CONTEXT (t2); break;
838 case tcc_exceptional:
839 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
840 default: gcc_unreachable ();
843 return t1 == t2;
846 /* The C standard says that two structures in different translation
847 units are compatible with each other only if the types of their
848 fields are compatible (among other things). So, consider two copies
849 of this structure: */
851 struct tagged_tu_seen {
852 const struct tagged_tu_seen * next;
853 tree t1;
854 tree t2;
857 /* Can they be compatible with each other? We choose to break the
858 recursion by allowing those types to be compatible. */
860 static const struct tagged_tu_seen * tagged_tu_seen_base;
862 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
863 compatible. If the two types are not the same (which has been
864 checked earlier), this can only happen when multiple translation
865 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
866 rules. */
868 static int
869 tagged_types_tu_compatible_p (tree t1, tree t2)
871 tree s1, s2;
872 bool needs_warning = false;
874 /* We have to verify that the tags of the types are the same. This
875 is harder than it looks because this may be a typedef, so we have
876 to go look at the original type. It may even be a typedef of a
877 typedef...
878 In the case of compiler-created builtin structs the TYPE_DECL
879 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
880 while (TYPE_NAME (t1)
881 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
882 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
883 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
885 while (TYPE_NAME (t2)
886 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
887 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
888 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
890 /* C90 didn't have the requirement that the two tags be the same. */
891 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
892 return 0;
894 /* C90 didn't say what happened if one or both of the types were
895 incomplete; we choose to follow C99 rules here, which is that they
896 are compatible. */
897 if (TYPE_SIZE (t1) == NULL
898 || TYPE_SIZE (t2) == NULL)
899 return 1;
902 const struct tagged_tu_seen * tts_i;
903 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
904 if (tts_i->t1 == t1 && tts_i->t2 == t2)
905 return 1;
908 switch (TREE_CODE (t1))
910 case ENUMERAL_TYPE:
913 /* Speed up the case where the type values are in the same order. */
914 tree tv1 = TYPE_VALUES (t1);
915 tree tv2 = TYPE_VALUES (t2);
917 if (tv1 == tv2)
918 return 1;
920 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
922 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
923 break;
924 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
925 return 0;
928 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
929 return 1;
930 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
931 return 0;
933 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
934 return 0;
936 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
938 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
939 if (s2 == NULL
940 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
941 return 0;
943 return 1;
946 case UNION_TYPE:
948 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
949 return 0;
951 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
953 bool ok = false;
954 struct tagged_tu_seen tts;
956 tts.next = tagged_tu_seen_base;
957 tts.t1 = t1;
958 tts.t2 = t2;
959 tagged_tu_seen_base = &tts;
961 if (DECL_NAME (s1) != NULL)
962 for (s2 = TYPE_FIELDS (t2); s2; s2 = TREE_CHAIN (s2))
963 if (DECL_NAME (s1) == DECL_NAME (s2))
965 int result;
966 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
967 if (result == 0)
968 break;
969 if (result == 2)
970 needs_warning = true;
972 if (TREE_CODE (s1) == FIELD_DECL
973 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
974 DECL_FIELD_BIT_OFFSET (s2)) != 1)
975 break;
977 ok = true;
978 break;
980 tagged_tu_seen_base = tts.next;
981 if (!ok)
982 return 0;
984 return needs_warning ? 2 : 1;
987 case RECORD_TYPE:
989 struct tagged_tu_seen tts;
991 tts.next = tagged_tu_seen_base;
992 tts.t1 = t1;
993 tts.t2 = t2;
994 tagged_tu_seen_base = &tts;
996 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
997 s1 && s2;
998 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
1000 int result;
1001 if (TREE_CODE (s1) != TREE_CODE (s2)
1002 || DECL_NAME (s1) != DECL_NAME (s2))
1003 break;
1004 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2));
1005 if (result == 0)
1006 break;
1007 if (result == 2)
1008 needs_warning = true;
1010 if (TREE_CODE (s1) == FIELD_DECL
1011 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1012 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1013 break;
1015 tagged_tu_seen_base = tts.next;
1016 if (s1 && s2)
1017 return 0;
1018 return needs_warning ? 2 : 1;
1021 default:
1022 gcc_unreachable ();
1026 /* Return 1 if two function types F1 and F2 are compatible.
1027 If either type specifies no argument types,
1028 the other must specify a fixed number of self-promoting arg types.
1029 Otherwise, if one type specifies only the number of arguments,
1030 the other must specify that number of self-promoting arg types.
1031 Otherwise, the argument types must match. */
1033 static int
1034 function_types_compatible_p (tree f1, tree f2)
1036 tree args1, args2;
1037 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1038 int val = 1;
1039 int val1;
1040 tree ret1, ret2;
1042 ret1 = TREE_TYPE (f1);
1043 ret2 = TREE_TYPE (f2);
1045 /* 'volatile' qualifiers on a function's return type used to mean
1046 the function is noreturn. */
1047 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1048 pedwarn ("function return types not compatible due to %<volatile%>");
1049 if (TYPE_VOLATILE (ret1))
1050 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1051 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1052 if (TYPE_VOLATILE (ret2))
1053 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1054 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1055 val = comptypes (ret1, ret2);
1056 if (val == 0)
1057 return 0;
1059 args1 = TYPE_ARG_TYPES (f1);
1060 args2 = TYPE_ARG_TYPES (f2);
1062 /* An unspecified parmlist matches any specified parmlist
1063 whose argument types don't need default promotions. */
1065 if (args1 == 0)
1067 if (!self_promoting_args_p (args2))
1068 return 0;
1069 /* If one of these types comes from a non-prototype fn definition,
1070 compare that with the other type's arglist.
1071 If they don't match, ask for a warning (0, but no error). */
1072 if (TYPE_ACTUAL_ARG_TYPES (f1)
1073 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1)))
1074 val = 2;
1075 return val;
1077 if (args2 == 0)
1079 if (!self_promoting_args_p (args1))
1080 return 0;
1081 if (TYPE_ACTUAL_ARG_TYPES (f2)
1082 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2)))
1083 val = 2;
1084 return val;
1087 /* Both types have argument lists: compare them and propagate results. */
1088 val1 = type_lists_compatible_p (args1, args2);
1089 return val1 != 1 ? val1 : val;
1092 /* Check two lists of types for compatibility,
1093 returning 0 for incompatible, 1 for compatible,
1094 or 2 for compatible with warning. */
1096 static int
1097 type_lists_compatible_p (tree args1, tree args2)
1099 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1100 int val = 1;
1101 int newval = 0;
1103 while (1)
1105 tree a1, mv1, a2, mv2;
1106 if (args1 == 0 && args2 == 0)
1107 return val;
1108 /* If one list is shorter than the other,
1109 they fail to match. */
1110 if (args1 == 0 || args2 == 0)
1111 return 0;
1112 mv1 = a1 = TREE_VALUE (args1);
1113 mv2 = a2 = TREE_VALUE (args2);
1114 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1115 mv1 = TYPE_MAIN_VARIANT (mv1);
1116 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1117 mv2 = TYPE_MAIN_VARIANT (mv2);
1118 /* A null pointer instead of a type
1119 means there is supposed to be an argument
1120 but nothing is specified about what type it has.
1121 So match anything that self-promotes. */
1122 if (a1 == 0)
1124 if (c_type_promotes_to (a2) != a2)
1125 return 0;
1127 else if (a2 == 0)
1129 if (c_type_promotes_to (a1) != a1)
1130 return 0;
1132 /* If one of the lists has an error marker, ignore this arg. */
1133 else if (TREE_CODE (a1) == ERROR_MARK
1134 || TREE_CODE (a2) == ERROR_MARK)
1136 else if (!(newval = comptypes (mv1, mv2)))
1138 /* Allow wait (union {union wait *u; int *i} *)
1139 and wait (union wait *) to be compatible. */
1140 if (TREE_CODE (a1) == UNION_TYPE
1141 && (TYPE_NAME (a1) == 0
1142 || TYPE_TRANSPARENT_UNION (a1))
1143 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1144 && tree_int_cst_equal (TYPE_SIZE (a1),
1145 TYPE_SIZE (a2)))
1147 tree memb;
1148 for (memb = TYPE_FIELDS (a1);
1149 memb; memb = TREE_CHAIN (memb))
1151 tree mv3 = TREE_TYPE (memb);
1152 if (mv3 && mv3 != error_mark_node
1153 && TREE_CODE (mv3) != ARRAY_TYPE)
1154 mv3 = TYPE_MAIN_VARIANT (mv3);
1155 if (comptypes (mv3, mv2))
1156 break;
1158 if (memb == 0)
1159 return 0;
1161 else if (TREE_CODE (a2) == UNION_TYPE
1162 && (TYPE_NAME (a2) == 0
1163 || TYPE_TRANSPARENT_UNION (a2))
1164 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1165 && tree_int_cst_equal (TYPE_SIZE (a2),
1166 TYPE_SIZE (a1)))
1168 tree memb;
1169 for (memb = TYPE_FIELDS (a2);
1170 memb; memb = TREE_CHAIN (memb))
1172 tree mv3 = TREE_TYPE (memb);
1173 if (mv3 && mv3 != error_mark_node
1174 && TREE_CODE (mv3) != ARRAY_TYPE)
1175 mv3 = TYPE_MAIN_VARIANT (mv3);
1176 if (comptypes (mv3, mv1))
1177 break;
1179 if (memb == 0)
1180 return 0;
1182 else
1183 return 0;
1186 /* comptypes said ok, but record if it said to warn. */
1187 if (newval > val)
1188 val = newval;
1190 args1 = TREE_CHAIN (args1);
1191 args2 = TREE_CHAIN (args2);
1195 /* Compute the size to increment a pointer by. */
1197 static tree
1198 c_size_in_bytes (tree type)
1200 enum tree_code code = TREE_CODE (type);
1202 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1203 return size_one_node;
1205 if (!COMPLETE_OR_VOID_TYPE_P (type))
1207 error ("arithmetic on pointer to an incomplete type");
1208 return size_one_node;
1211 /* Convert in case a char is more than one unit. */
1212 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1213 size_int (TYPE_PRECISION (char_type_node)
1214 / BITS_PER_UNIT));
1217 /* Return either DECL or its known constant value (if it has one). */
1219 tree
1220 decl_constant_value (tree decl)
1222 if (/* Don't change a variable array bound or initial value to a constant
1223 in a place where a variable is invalid. Note that DECL_INITIAL
1224 isn't valid for a PARM_DECL. */
1225 current_function_decl != 0
1226 && TREE_CODE (decl) != PARM_DECL
1227 && !TREE_THIS_VOLATILE (decl)
1228 && TREE_READONLY (decl)
1229 && DECL_INITIAL (decl) != 0
1230 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1231 /* This is invalid if initial value is not constant.
1232 If it has either a function call, a memory reference,
1233 or a variable, then re-evaluating it could give different results. */
1234 && TREE_CONSTANT (DECL_INITIAL (decl))
1235 /* Check for cases where this is sub-optimal, even though valid. */
1236 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1237 return DECL_INITIAL (decl);
1238 return decl;
1241 /* Return either DECL or its known constant value (if it has one), but
1242 return DECL if pedantic or DECL has mode BLKmode. This is for
1243 bug-compatibility with the old behavior of decl_constant_value
1244 (before GCC 3.0); every use of this function is a bug and it should
1245 be removed before GCC 3.1. It is not appropriate to use pedantic
1246 in a way that affects optimization, and BLKmode is probably not the
1247 right test for avoiding misoptimizations either. */
1249 static tree
1250 decl_constant_value_for_broken_optimization (tree decl)
1252 if (pedantic || DECL_MODE (decl) == BLKmode)
1253 return decl;
1254 else
1255 return decl_constant_value (decl);
1259 /* Perform the default conversion of arrays and functions to pointers.
1260 Return the result of converting EXP. For any other expression, just
1261 return EXP. */
1263 static tree
1264 default_function_array_conversion (tree exp)
1266 tree orig_exp;
1267 tree type = TREE_TYPE (exp);
1268 enum tree_code code = TREE_CODE (type);
1269 int not_lvalue = 0;
1271 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1272 an lvalue.
1274 Do not use STRIP_NOPS here! It will remove conversions from pointer
1275 to integer and cause infinite recursion. */
1276 orig_exp = exp;
1277 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1278 || (TREE_CODE (exp) == NOP_EXPR
1279 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1281 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1282 not_lvalue = 1;
1283 exp = TREE_OPERAND (exp, 0);
1286 if (TREE_NO_WARNING (orig_exp))
1287 TREE_NO_WARNING (exp) = 1;
1289 if (code == FUNCTION_TYPE)
1291 return build_unary_op (ADDR_EXPR, exp, 0);
1293 if (code == ARRAY_TYPE)
1295 tree adr;
1296 tree restype = TREE_TYPE (type);
1297 tree ptrtype;
1298 int constp = 0;
1299 int volatilep = 0;
1300 int lvalue_array_p;
1302 if (REFERENCE_CLASS_P (exp) || DECL_P (exp))
1304 constp = TREE_READONLY (exp);
1305 volatilep = TREE_THIS_VOLATILE (exp);
1308 if (TYPE_QUALS (type) || constp || volatilep)
1309 restype
1310 = c_build_qualified_type (restype,
1311 TYPE_QUALS (type)
1312 | (constp * TYPE_QUAL_CONST)
1313 | (volatilep * TYPE_QUAL_VOLATILE));
1315 if (TREE_CODE (exp) == INDIRECT_REF)
1316 return convert (build_pointer_type (restype),
1317 TREE_OPERAND (exp, 0));
1319 if (TREE_CODE (exp) == COMPOUND_EXPR)
1321 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1322 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1323 TREE_OPERAND (exp, 0), op1);
1326 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1327 if (!flag_isoc99 && !lvalue_array_p)
1329 /* Before C99, non-lvalue arrays do not decay to pointers.
1330 Normally, using such an array would be invalid; but it can
1331 be used correctly inside sizeof or as a statement expression.
1332 Thus, do not give an error here; an error will result later. */
1333 return exp;
1336 ptrtype = build_pointer_type (restype);
1338 if (TREE_CODE (exp) == VAR_DECL)
1340 /* We are making an ADDR_EXPR of ptrtype. This is a valid
1341 ADDR_EXPR because it's the best way of representing what
1342 happens in C when we take the address of an array and place
1343 it in a pointer to the element type. */
1344 adr = build1 (ADDR_EXPR, ptrtype, exp);
1345 if (!c_mark_addressable (exp))
1346 return error_mark_node;
1347 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1348 return adr;
1350 /* This way is better for a COMPONENT_REF since it can
1351 simplify the offset for a component. */
1352 adr = build_unary_op (ADDR_EXPR, exp, 1);
1353 return convert (ptrtype, adr);
1355 return exp;
1359 /* EXP is an expression of integer type. Apply the integer promotions
1360 to it and return the promoted value. */
1362 tree
1363 perform_integral_promotions (tree exp)
1365 tree type = TREE_TYPE (exp);
1366 enum tree_code code = TREE_CODE (type);
1368 gcc_assert (INTEGRAL_TYPE_P (type));
1370 /* Normally convert enums to int,
1371 but convert wide enums to something wider. */
1372 if (code == ENUMERAL_TYPE)
1374 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1375 TYPE_PRECISION (integer_type_node)),
1376 ((TYPE_PRECISION (type)
1377 >= TYPE_PRECISION (integer_type_node))
1378 && TYPE_UNSIGNED (type)));
1380 return convert (type, exp);
1383 /* ??? This should no longer be needed now bit-fields have their
1384 proper types. */
1385 if (TREE_CODE (exp) == COMPONENT_REF
1386 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1387 /* If it's thinner than an int, promote it like a
1388 c_promoting_integer_type_p, otherwise leave it alone. */
1389 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1390 TYPE_PRECISION (integer_type_node)))
1391 return convert (integer_type_node, exp);
1393 if (c_promoting_integer_type_p (type))
1395 /* Preserve unsignedness if not really getting any wider. */
1396 if (TYPE_UNSIGNED (type)
1397 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1398 return convert (unsigned_type_node, exp);
1400 return convert (integer_type_node, exp);
1403 return exp;
1407 /* Perform default promotions for C data used in expressions.
1408 Arrays and functions are converted to pointers;
1409 enumeral types or short or char, to int.
1410 In addition, manifest constants symbols are replaced by their values. */
1412 tree
1413 default_conversion (tree exp)
1415 tree orig_exp;
1416 tree type = TREE_TYPE (exp);
1417 enum tree_code code = TREE_CODE (type);
1419 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1420 return default_function_array_conversion (exp);
1422 /* Constants can be used directly unless they're not loadable. */
1423 if (TREE_CODE (exp) == CONST_DECL)
1424 exp = DECL_INITIAL (exp);
1426 /* Replace a nonvolatile const static variable with its value unless
1427 it is an array, in which case we must be sure that taking the
1428 address of the array produces consistent results. */
1429 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1431 exp = decl_constant_value_for_broken_optimization (exp);
1432 type = TREE_TYPE (exp);
1435 /* Strip no-op conversions. */
1436 orig_exp = exp;
1437 STRIP_TYPE_NOPS (exp);
1439 if (TREE_NO_WARNING (orig_exp))
1440 TREE_NO_WARNING (exp) = 1;
1442 if (INTEGRAL_TYPE_P (type))
1443 return perform_integral_promotions (exp);
1445 if (code == VOID_TYPE)
1447 error ("void value not ignored as it ought to be");
1448 return error_mark_node;
1450 return exp;
1453 /* Look up COMPONENT in a structure or union DECL.
1455 If the component name is not found, returns NULL_TREE. Otherwise,
1456 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1457 stepping down the chain to the component, which is in the last
1458 TREE_VALUE of the list. Normally the list is of length one, but if
1459 the component is embedded within (nested) anonymous structures or
1460 unions, the list steps down the chain to the component. */
1462 static tree
1463 lookup_field (tree decl, tree component)
1465 tree type = TREE_TYPE (decl);
1466 tree field;
1468 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1469 to the field elements. Use a binary search on this array to quickly
1470 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1471 will always be set for structures which have many elements. */
1473 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
1475 int bot, top, half;
1476 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1478 field = TYPE_FIELDS (type);
1479 bot = 0;
1480 top = TYPE_LANG_SPECIFIC (type)->s->len;
1481 while (top - bot > 1)
1483 half = (top - bot + 1) >> 1;
1484 field = field_array[bot+half];
1486 if (DECL_NAME (field) == NULL_TREE)
1488 /* Step through all anon unions in linear fashion. */
1489 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1491 field = field_array[bot++];
1492 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1493 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1495 tree anon = lookup_field (field, component);
1497 if (anon)
1498 return tree_cons (NULL_TREE, field, anon);
1502 /* Entire record is only anon unions. */
1503 if (bot > top)
1504 return NULL_TREE;
1506 /* Restart the binary search, with new lower bound. */
1507 continue;
1510 if (DECL_NAME (field) == component)
1511 break;
1512 if (DECL_NAME (field) < component)
1513 bot += half;
1514 else
1515 top = bot + half;
1518 if (DECL_NAME (field_array[bot]) == component)
1519 field = field_array[bot];
1520 else if (DECL_NAME (field) != component)
1521 return NULL_TREE;
1523 else
1525 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1527 if (DECL_NAME (field) == NULL_TREE
1528 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1529 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1531 tree anon = lookup_field (field, component);
1533 if (anon)
1534 return tree_cons (NULL_TREE, field, anon);
1537 if (DECL_NAME (field) == component)
1538 break;
1541 if (field == NULL_TREE)
1542 return NULL_TREE;
1545 return tree_cons (NULL_TREE, field, NULL_TREE);
1548 /* Make an expression to refer to the COMPONENT field of
1549 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1551 tree
1552 build_component_ref (tree datum, tree component)
1554 tree type = TREE_TYPE (datum);
1555 enum tree_code code = TREE_CODE (type);
1556 tree field = NULL;
1557 tree ref;
1559 if (!objc_is_public (datum, component))
1560 return error_mark_node;
1562 /* See if there is a field or component with name COMPONENT. */
1564 if (code == RECORD_TYPE || code == UNION_TYPE)
1566 if (!COMPLETE_TYPE_P (type))
1568 c_incomplete_type_error (NULL_TREE, type);
1569 return error_mark_node;
1572 field = lookup_field (datum, component);
1574 if (!field)
1576 error ("%qT has no member named %qE", type, 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 %qE in something not a structure or union",
1612 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 (0, "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 (0, "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. LOC is the source
1807 location of the identifier. */
1808 tree
1809 build_external_ref (tree id, int fun, location_t loc)
1811 tree ref;
1812 tree decl = lookup_name (id);
1814 /* In Objective-C, an instance variable (ivar) may be preferred to
1815 whatever lookup_name() found. */
1816 decl = objc_lookup_ivar (decl, id);
1818 if (decl && decl != error_mark_node)
1819 ref = decl;
1820 else if (fun)
1821 /* Implicit function declaration. */
1822 ref = implicitly_declare (id);
1823 else if (decl == error_mark_node)
1824 /* Don't complain about something that's already been
1825 complained about. */
1826 return error_mark_node;
1827 else
1829 undeclared_variable (id, loc);
1830 return error_mark_node;
1833 if (TREE_TYPE (ref) == error_mark_node)
1834 return error_mark_node;
1836 if (TREE_DEPRECATED (ref))
1837 warn_deprecated_use (ref);
1839 if (!skip_evaluation)
1840 assemble_external (ref);
1841 TREE_USED (ref) = 1;
1843 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
1845 if (!in_sizeof && !in_typeof)
1846 C_DECL_USED (ref) = 1;
1847 else if (DECL_INITIAL (ref) == 0
1848 && DECL_EXTERNAL (ref)
1849 && !TREE_PUBLIC (ref))
1850 record_maybe_used_decl (ref);
1853 if (TREE_CODE (ref) == CONST_DECL)
1855 ref = DECL_INITIAL (ref);
1856 TREE_CONSTANT (ref) = 1;
1857 TREE_INVARIANT (ref) = 1;
1859 else if (current_function_decl != 0
1860 && !DECL_FILE_SCOPE_P (current_function_decl)
1861 && (TREE_CODE (ref) == VAR_DECL
1862 || TREE_CODE (ref) == PARM_DECL
1863 || TREE_CODE (ref) == FUNCTION_DECL))
1865 tree context = decl_function_context (ref);
1867 if (context != 0 && context != current_function_decl)
1868 DECL_NONLOCAL (ref) = 1;
1871 return ref;
1874 /* Record details of decls possibly used inside sizeof or typeof. */
1875 struct maybe_used_decl
1877 /* The decl. */
1878 tree decl;
1879 /* The level seen at (in_sizeof + in_typeof). */
1880 int level;
1881 /* The next one at this level or above, or NULL. */
1882 struct maybe_used_decl *next;
1885 static struct maybe_used_decl *maybe_used_decls;
1887 /* Record that DECL, an undefined static function reference seen
1888 inside sizeof or typeof, might be used if the operand of sizeof is
1889 a VLA type or the operand of typeof is a variably modified
1890 type. */
1892 static void
1893 record_maybe_used_decl (tree decl)
1895 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
1896 t->decl = decl;
1897 t->level = in_sizeof + in_typeof;
1898 t->next = maybe_used_decls;
1899 maybe_used_decls = t;
1902 /* Pop the stack of decls possibly used inside sizeof or typeof. If
1903 USED is false, just discard them. If it is true, mark them used
1904 (if no longer inside sizeof or typeof) or move them to the next
1905 level up (if still inside sizeof or typeof). */
1907 void
1908 pop_maybe_used (bool used)
1910 struct maybe_used_decl *p = maybe_used_decls;
1911 int cur_level = in_sizeof + in_typeof;
1912 while (p && p->level > cur_level)
1914 if (used)
1916 if (cur_level == 0)
1917 C_DECL_USED (p->decl) = 1;
1918 else
1919 p->level = cur_level;
1921 p = p->next;
1923 if (!used || cur_level == 0)
1924 maybe_used_decls = p;
1927 /* Return the result of sizeof applied to EXPR. */
1929 struct c_expr
1930 c_expr_sizeof_expr (struct c_expr expr)
1932 struct c_expr ret;
1933 if (expr.value == error_mark_node)
1935 ret.value = error_mark_node;
1936 ret.original_code = ERROR_MARK;
1937 pop_maybe_used (false);
1939 else
1941 ret.value = c_sizeof (TREE_TYPE (expr.value));
1942 ret.original_code = ERROR_MARK;
1943 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (expr.value)));
1945 return ret;
1948 /* Return the result of sizeof applied to T, a structure for the type
1949 name passed to sizeof (rather than the type itself). */
1951 struct c_expr
1952 c_expr_sizeof_type (struct c_type_name *t)
1954 tree type;
1955 struct c_expr ret;
1956 type = groktypename (t);
1957 ret.value = c_sizeof (type);
1958 ret.original_code = ERROR_MARK;
1959 pop_maybe_used (C_TYPE_VARIABLE_SIZE (type));
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 if (DECL_BUILT_IN_CLASS (function) == BUILT_IN_NORMAL)
1984 tem = resolve_overloaded_builtin (function, params);
1985 if (tem)
1986 return tem;
1989 name = DECL_NAME (function);
1991 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1992 (because calling an inline function does not mean the function
1993 needs to be separately compiled). */
1994 fntype = build_type_variant (TREE_TYPE (function),
1995 TREE_READONLY (function),
1996 TREE_THIS_VOLATILE (function));
1997 fundecl = function;
1998 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
2000 else
2001 function = default_conversion (function);
2003 fntype = TREE_TYPE (function);
2005 if (TREE_CODE (fntype) == ERROR_MARK)
2006 return error_mark_node;
2008 if (!(TREE_CODE (fntype) == POINTER_TYPE
2009 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2011 error ("called object %qE is not a function", function);
2012 return error_mark_node;
2015 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2016 current_function_returns_abnormally = 1;
2018 /* fntype now gets the type of function pointed to. */
2019 fntype = TREE_TYPE (fntype);
2021 /* Check that the function is called through a compatible prototype.
2022 If it is not, replace the call by a trap, wrapped up in a compound
2023 expression if necessary. This has the nice side-effect to prevent
2024 the tree-inliner from generating invalid assignment trees which may
2025 blow up in the RTL expander later.
2027 ??? This doesn't work for Objective-C because objc_comptypes
2028 refuses to compare function prototypes, yet the compiler appears
2029 to build calls that are flagged as invalid by C's comptypes. */
2030 if (!c_dialect_objc ()
2031 && TREE_CODE (function) == NOP_EXPR
2032 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2033 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2034 && !comptypes (fntype, TREE_TYPE (tem)))
2036 tree return_type = TREE_TYPE (fntype);
2037 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
2038 NULL_TREE);
2040 /* This situation leads to run-time undefined behavior. We can't,
2041 therefore, simply error unless we can prove that all possible
2042 executions of the program must execute the code. */
2043 warning (0, "function called through a non-compatible type");
2045 /* We can, however, treat "undefined" any way we please.
2046 Call abort to encourage the user to fix the program. */
2047 inform ("if this code is reached, the program will abort");
2049 if (VOID_TYPE_P (return_type))
2050 return trap;
2051 else
2053 tree rhs;
2055 if (AGGREGATE_TYPE_P (return_type))
2056 rhs = build_compound_literal (return_type,
2057 build_constructor (return_type,
2058 NULL_TREE));
2059 else
2060 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
2062 return build2 (COMPOUND_EXPR, return_type, trap, rhs);
2066 /* Convert the parameters to the types declared in the
2067 function prototype, or apply default promotions. */
2069 coerced_params
2070 = convert_arguments (TYPE_ARG_TYPES (fntype), params, function, fundecl);
2072 if (coerced_params == error_mark_node)
2073 return error_mark_node;
2075 /* Check that the arguments to the function are valid. */
2077 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
2079 result = build3 (CALL_EXPR, TREE_TYPE (fntype),
2080 function, coerced_params, NULL_TREE);
2081 TREE_SIDE_EFFECTS (result) = 1;
2083 if (require_constant_value)
2085 result = fold_initializer (result);
2087 if (TREE_CONSTANT (result)
2088 && (name == NULL_TREE
2089 || strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10) != 0))
2090 pedwarn_init ("initializer element is not constant");
2092 else
2093 result = fold (result);
2095 if (VOID_TYPE_P (TREE_TYPE (result)))
2096 return result;
2097 return require_complete_type (result);
2100 /* Convert the argument expressions in the list VALUES
2101 to the types in the list TYPELIST. The result is a list of converted
2102 argument expressions, unless there are too few arguments in which
2103 case it is error_mark_node.
2105 If TYPELIST is exhausted, or when an element has NULL as its type,
2106 perform the default conversions.
2108 PARMLIST is the chain of parm decls for the function being called.
2109 It may be 0, if that info is not available.
2110 It is used only for generating error messages.
2112 FUNCTION is a tree for the called function. It is used only for
2113 error messages, where it is formatted with %qE.
2115 This is also where warnings about wrong number of args are generated.
2117 Both VALUES and the returned value are chains of TREE_LIST nodes
2118 with the elements of the list in the TREE_VALUE slots of those nodes. */
2120 static tree
2121 convert_arguments (tree typelist, tree values, tree function, tree fundecl)
2123 tree typetail, valtail;
2124 tree result = NULL;
2125 int parmnum;
2126 tree selector;
2128 /* Change pointer to function to the function itself for
2129 diagnostics. */
2130 if (TREE_CODE (function) == ADDR_EXPR
2131 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2132 function = TREE_OPERAND (function, 0);
2134 /* Handle an ObjC selector specially for diagnostics. */
2135 selector = objc_message_selector ();
2137 /* Scan the given expressions and types, producing individual
2138 converted arguments and pushing them on RESULT in reverse order. */
2140 for (valtail = values, typetail = typelist, parmnum = 0;
2141 valtail;
2142 valtail = TREE_CHAIN (valtail), parmnum++)
2144 tree type = typetail ? TREE_VALUE (typetail) : 0;
2145 tree val = TREE_VALUE (valtail);
2146 tree rname = function;
2147 int argnum = parmnum + 1;
2148 const char *invalid_func_diag;
2150 if (type == void_type_node)
2152 error ("too many arguments to function %qE", function);
2153 break;
2156 if (selector && argnum > 2)
2158 rname = selector;
2159 argnum -= 2;
2162 STRIP_TYPE_NOPS (val);
2164 val = default_function_array_conversion (val);
2166 val = require_complete_type (val);
2168 if (type != 0)
2170 /* Formal parm type is specified by a function prototype. */
2171 tree parmval;
2173 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2175 error ("type of formal parameter %d is incomplete", parmnum + 1);
2176 parmval = val;
2178 else
2180 /* Optionally warn about conversions that
2181 differ from the default conversions. */
2182 if (warn_conversion || warn_traditional)
2184 unsigned int formal_prec = TYPE_PRECISION (type);
2186 if (INTEGRAL_TYPE_P (type)
2187 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2188 warning (0, "passing argument %d of %qE as integer "
2189 "rather than floating due to prototype",
2190 argnum, rname);
2191 if (INTEGRAL_TYPE_P (type)
2192 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2193 warning (0, "passing argument %d of %qE as integer "
2194 "rather than complex due to prototype",
2195 argnum, rname);
2196 else if (TREE_CODE (type) == COMPLEX_TYPE
2197 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2198 warning (0, "passing argument %d of %qE as complex "
2199 "rather than floating due to prototype",
2200 argnum, rname);
2201 else if (TREE_CODE (type) == REAL_TYPE
2202 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2203 warning (0, "passing argument %d of %qE as floating "
2204 "rather than integer due to prototype",
2205 argnum, rname);
2206 else if (TREE_CODE (type) == COMPLEX_TYPE
2207 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2208 warning (0, "passing argument %d of %qE as complex "
2209 "rather than integer due to prototype",
2210 argnum, rname);
2211 else if (TREE_CODE (type) == REAL_TYPE
2212 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
2213 warning (0, "passing argument %d of %qE as floating "
2214 "rather than complex due to prototype",
2215 argnum, rname);
2216 /* ??? At some point, messages should be written about
2217 conversions between complex types, but that's too messy
2218 to do now. */
2219 else if (TREE_CODE (type) == REAL_TYPE
2220 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
2222 /* Warn if any argument is passed as `float',
2223 since without a prototype it would be `double'. */
2224 if (formal_prec == TYPE_PRECISION (float_type_node))
2225 warning (0, "passing argument %d of %qE as %<float%> "
2226 "rather than %<double%> due to prototype",
2227 argnum, rname);
2229 /* Detect integer changing in width or signedness.
2230 These warnings are only activated with
2231 -Wconversion, not with -Wtraditional. */
2232 else if (warn_conversion && INTEGRAL_TYPE_P (type)
2233 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
2235 tree would_have_been = default_conversion (val);
2236 tree type1 = TREE_TYPE (would_have_been);
2238 if (TREE_CODE (type) == ENUMERAL_TYPE
2239 && (TYPE_MAIN_VARIANT (type)
2240 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
2241 /* No warning if function asks for enum
2242 and the actual arg is that enum type. */
2244 else if (formal_prec != TYPE_PRECISION (type1))
2245 warning (0, "passing argument %d of %qE with different "
2246 "width due to prototype", argnum, rname);
2247 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
2249 /* Don't complain if the formal parameter type
2250 is an enum, because we can't tell now whether
2251 the value was an enum--even the same enum. */
2252 else if (TREE_CODE (type) == ENUMERAL_TYPE)
2254 else if (TREE_CODE (val) == INTEGER_CST
2255 && int_fits_type_p (val, type))
2256 /* Change in signedness doesn't matter
2257 if a constant value is unaffected. */
2259 /* If the value is extended from a narrower
2260 unsigned type, it doesn't matter whether we
2261 pass it as signed or unsigned; the value
2262 certainly is the same either way. */
2263 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
2264 && TYPE_UNSIGNED (TREE_TYPE (val)))
2266 else if (TYPE_UNSIGNED (type))
2267 warning (0, "passing argument %d of %qE as unsigned "
2268 "due to prototype", argnum, rname);
2269 else
2270 warning (0, "passing argument %d of %qE as signed "
2271 "due to prototype", argnum, rname);
2275 parmval = convert_for_assignment (type, val, ic_argpass,
2276 fundecl, function,
2277 parmnum + 1);
2279 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
2280 && INTEGRAL_TYPE_P (type)
2281 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
2282 parmval = default_conversion (parmval);
2284 result = tree_cons (NULL_TREE, parmval, result);
2286 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
2287 && (TYPE_PRECISION (TREE_TYPE (val))
2288 < TYPE_PRECISION (double_type_node)))
2289 /* Convert `float' to `double'. */
2290 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
2291 else if ((invalid_func_diag =
2292 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
2294 error (invalid_func_diag);
2295 return error_mark_node;
2297 else
2298 /* Convert `short' and `char' to full-size `int'. */
2299 result = tree_cons (NULL_TREE, default_conversion (val), result);
2301 if (typetail)
2302 typetail = TREE_CHAIN (typetail);
2305 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
2307 error ("too few arguments to function %qE", function);
2308 return error_mark_node;
2311 return nreverse (result);
2314 /* This is the entry point used by the parser
2315 for binary operators in the input.
2316 In addition to constructing the expression,
2317 we check for operands that were written with other binary operators
2318 in a way that is likely to confuse the user. */
2320 struct c_expr
2321 parser_build_binary_op (enum tree_code code, struct c_expr arg1,
2322 struct c_expr arg2)
2324 struct c_expr result;
2326 enum tree_code code1 = arg1.original_code;
2327 enum tree_code code2 = arg2.original_code;
2329 result.value = build_binary_op (code, arg1.value, arg2.value, 1);
2330 result.original_code = code;
2332 if (TREE_CODE (result.value) == ERROR_MARK)
2333 return result;
2335 /* Check for cases such as x+y<<z which users are likely
2336 to misinterpret. */
2337 if (warn_parentheses)
2339 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2341 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2342 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2343 warning (0, "suggest parentheses around + or - inside shift");
2346 if (code == TRUTH_ORIF_EXPR)
2348 if (code1 == TRUTH_ANDIF_EXPR
2349 || code2 == TRUTH_ANDIF_EXPR)
2350 warning (0, "suggest parentheses around && within ||");
2353 if (code == BIT_IOR_EXPR)
2355 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2356 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2357 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2358 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2359 warning (0, "suggest parentheses around arithmetic in operand of |");
2360 /* Check cases like x|y==z */
2361 if (TREE_CODE_CLASS (code1) == tcc_comparison
2362 || TREE_CODE_CLASS (code2) == tcc_comparison)
2363 warning (0, "suggest parentheses around comparison in operand of |");
2366 if (code == BIT_XOR_EXPR)
2368 if (code1 == BIT_AND_EXPR
2369 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2370 || code2 == BIT_AND_EXPR
2371 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2372 warning (0, "suggest parentheses around arithmetic in operand of ^");
2373 /* Check cases like x^y==z */
2374 if (TREE_CODE_CLASS (code1) == tcc_comparison
2375 || TREE_CODE_CLASS (code2) == tcc_comparison)
2376 warning (0, "suggest parentheses around comparison in operand of ^");
2379 if (code == BIT_AND_EXPR)
2381 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2382 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2383 warning (0, "suggest parentheses around + or - in operand of &");
2384 /* Check cases like x&y==z */
2385 if (TREE_CODE_CLASS (code1) == tcc_comparison
2386 || TREE_CODE_CLASS (code2) == tcc_comparison)
2387 warning (0, "suggest parentheses around comparison in operand of &");
2389 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2390 if (TREE_CODE_CLASS (code) == tcc_comparison
2391 && (TREE_CODE_CLASS (code1) == tcc_comparison
2392 || TREE_CODE_CLASS (code2) == tcc_comparison))
2393 warning (0, "comparisons like X<=Y<=Z do not have their mathematical meaning");
2397 unsigned_conversion_warning (result.value, arg1.value);
2398 unsigned_conversion_warning (result.value, arg2.value);
2399 overflow_warning (result.value);
2401 return result;
2404 /* Return a tree for the difference of pointers OP0 and OP1.
2405 The resulting tree has type int. */
2407 static tree
2408 pointer_diff (tree op0, tree op1)
2410 tree restype = ptrdiff_type_node;
2412 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2413 tree con0, con1, lit0, lit1;
2414 tree orig_op1 = op1;
2416 if (pedantic || warn_pointer_arith)
2418 if (TREE_CODE (target_type) == VOID_TYPE)
2419 pedwarn ("pointer of type %<void *%> used in subtraction");
2420 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2421 pedwarn ("pointer to a function used in subtraction");
2424 /* If the conversion to ptrdiff_type does anything like widening or
2425 converting a partial to an integral mode, we get a convert_expression
2426 that is in the way to do any simplifications.
2427 (fold-const.c doesn't know that the extra bits won't be needed.
2428 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2429 different mode in place.)
2430 So first try to find a common term here 'by hand'; we want to cover
2431 at least the cases that occur in legal static initializers. */
2432 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2433 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2435 if (TREE_CODE (con0) == PLUS_EXPR)
2437 lit0 = TREE_OPERAND (con0, 1);
2438 con0 = TREE_OPERAND (con0, 0);
2440 else
2441 lit0 = integer_zero_node;
2443 if (TREE_CODE (con1) == PLUS_EXPR)
2445 lit1 = TREE_OPERAND (con1, 1);
2446 con1 = TREE_OPERAND (con1, 0);
2448 else
2449 lit1 = integer_zero_node;
2451 if (operand_equal_p (con0, con1, 0))
2453 op0 = lit0;
2454 op1 = lit1;
2458 /* First do the subtraction as integers;
2459 then drop through to build the divide operator.
2460 Do not do default conversions on the minus operator
2461 in case restype is a short type. */
2463 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2464 convert (restype, op1), 0);
2465 /* This generates an error if op1 is pointer to incomplete type. */
2466 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2467 error ("arithmetic on pointer to an incomplete type");
2469 /* This generates an error if op0 is pointer to incomplete type. */
2470 op1 = c_size_in_bytes (target_type);
2472 /* Divide by the size, in easiest possible way. */
2473 return fold (build2 (EXACT_DIV_EXPR, restype, op0, convert (restype, op1)));
2476 /* Construct and perhaps optimize a tree representation
2477 for a unary operation. CODE, a tree_code, specifies the operation
2478 and XARG is the operand.
2479 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2480 the default promotions (such as from short to int).
2481 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2482 allows non-lvalues; this is only used to handle conversion of non-lvalue
2483 arrays to pointers in C99. */
2485 tree
2486 build_unary_op (enum tree_code code, tree xarg, int flag)
2488 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2489 tree arg = xarg;
2490 tree argtype = 0;
2491 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2492 tree val;
2493 int noconvert = flag;
2495 if (typecode == ERROR_MARK)
2496 return error_mark_node;
2497 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2498 typecode = INTEGER_TYPE;
2500 switch (code)
2502 case CONVERT_EXPR:
2503 /* This is used for unary plus, because a CONVERT_EXPR
2504 is enough to prevent anybody from looking inside for
2505 associativity, but won't generate any code. */
2506 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2507 || typecode == COMPLEX_TYPE
2508 || typecode == VECTOR_TYPE))
2510 error ("wrong type argument to unary plus");
2511 return error_mark_node;
2513 else if (!noconvert)
2514 arg = default_conversion (arg);
2515 arg = non_lvalue (arg);
2516 break;
2518 case NEGATE_EXPR:
2519 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2520 || typecode == COMPLEX_TYPE
2521 || typecode == VECTOR_TYPE))
2523 error ("wrong type argument to unary minus");
2524 return error_mark_node;
2526 else if (!noconvert)
2527 arg = default_conversion (arg);
2528 break;
2530 case BIT_NOT_EXPR:
2531 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2533 if (!noconvert)
2534 arg = default_conversion (arg);
2536 else if (typecode == COMPLEX_TYPE)
2538 code = CONJ_EXPR;
2539 if (pedantic)
2540 pedwarn ("ISO C does not support %<~%> for complex conjugation");
2541 if (!noconvert)
2542 arg = default_conversion (arg);
2544 else
2546 error ("wrong type argument to bit-complement");
2547 return error_mark_node;
2549 break;
2551 case ABS_EXPR:
2552 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2554 error ("wrong type argument to abs");
2555 return error_mark_node;
2557 else if (!noconvert)
2558 arg = default_conversion (arg);
2559 break;
2561 case CONJ_EXPR:
2562 /* Conjugating a real value is a no-op, but allow it anyway. */
2563 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2564 || typecode == COMPLEX_TYPE))
2566 error ("wrong type argument to conjugation");
2567 return error_mark_node;
2569 else if (!noconvert)
2570 arg = default_conversion (arg);
2571 break;
2573 case TRUTH_NOT_EXPR:
2574 /* ??? Why do most validation here but that for non-lvalue arrays
2575 in c_objc_common_truthvalue_conversion? */
2576 if (typecode != INTEGER_TYPE
2577 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2578 && typecode != COMPLEX_TYPE
2579 /* These will convert to a pointer. */
2580 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2582 error ("wrong type argument to unary exclamation mark");
2583 return error_mark_node;
2585 arg = c_objc_common_truthvalue_conversion (arg);
2586 return invert_truthvalue (arg);
2588 case NOP_EXPR:
2589 break;
2591 case REALPART_EXPR:
2592 if (TREE_CODE (arg) == COMPLEX_CST)
2593 return TREE_REALPART (arg);
2594 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2595 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2596 else
2597 return arg;
2599 case IMAGPART_EXPR:
2600 if (TREE_CODE (arg) == COMPLEX_CST)
2601 return TREE_IMAGPART (arg);
2602 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2603 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2604 else
2605 return convert (TREE_TYPE (arg), integer_zero_node);
2607 case PREINCREMENT_EXPR:
2608 case POSTINCREMENT_EXPR:
2609 case PREDECREMENT_EXPR:
2610 case POSTDECREMENT_EXPR:
2612 /* Increment or decrement the real part of the value,
2613 and don't change the imaginary part. */
2614 if (typecode == COMPLEX_TYPE)
2616 tree real, imag;
2618 if (pedantic)
2619 pedwarn ("ISO C does not support %<++%> and %<--%>"
2620 " on complex types");
2622 arg = stabilize_reference (arg);
2623 real = build_unary_op (REALPART_EXPR, arg, 1);
2624 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2625 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
2626 build_unary_op (code, real, 1), imag);
2629 /* Report invalid types. */
2631 if (typecode != POINTER_TYPE
2632 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2634 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2635 error ("wrong type argument to increment");
2636 else
2637 error ("wrong type argument to decrement");
2639 return error_mark_node;
2643 tree inc;
2644 tree result_type = TREE_TYPE (arg);
2646 arg = get_unwidened (arg, 0);
2647 argtype = TREE_TYPE (arg);
2649 /* Compute the increment. */
2651 if (typecode == POINTER_TYPE)
2653 /* If pointer target is an undefined struct,
2654 we just cannot know how to do the arithmetic. */
2655 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2657 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2658 error ("increment of pointer to unknown structure");
2659 else
2660 error ("decrement of pointer to unknown structure");
2662 else if ((pedantic || warn_pointer_arith)
2663 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2664 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2666 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2667 pedwarn ("wrong type argument to increment");
2668 else
2669 pedwarn ("wrong type argument to decrement");
2672 inc = c_size_in_bytes (TREE_TYPE (result_type));
2674 else
2675 inc = integer_one_node;
2677 inc = convert (argtype, inc);
2679 /* Complain about anything else that is not a true lvalue. */
2680 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2681 || code == POSTINCREMENT_EXPR)
2682 ? lv_increment
2683 : lv_decrement)))
2684 return error_mark_node;
2686 /* Report a read-only lvalue. */
2687 if (TREE_READONLY (arg))
2688 readonly_error (arg,
2689 ((code == PREINCREMENT_EXPR
2690 || code == POSTINCREMENT_EXPR)
2691 ? lv_increment : lv_decrement));
2693 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2694 val = boolean_increment (code, arg);
2695 else
2696 val = build2 (code, TREE_TYPE (arg), arg, inc);
2697 TREE_SIDE_EFFECTS (val) = 1;
2698 val = convert (result_type, val);
2699 if (TREE_CODE (val) != code)
2700 TREE_NO_WARNING (val) = 1;
2701 return val;
2704 case ADDR_EXPR:
2705 /* Note that this operation never does default_conversion. */
2707 /* Let &* cancel out to simplify resulting code. */
2708 if (TREE_CODE (arg) == INDIRECT_REF)
2710 /* Don't let this be an lvalue. */
2711 if (lvalue_p (TREE_OPERAND (arg, 0)))
2712 return non_lvalue (TREE_OPERAND (arg, 0));
2713 return TREE_OPERAND (arg, 0);
2716 /* For &x[y], return x+y */
2717 if (TREE_CODE (arg) == ARRAY_REF)
2719 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2720 return error_mark_node;
2721 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2722 TREE_OPERAND (arg, 1), 1);
2725 /* Anything not already handled and not a true memory reference
2726 or a non-lvalue array is an error. */
2727 else if (typecode != FUNCTION_TYPE && !flag
2728 && !lvalue_or_else (arg, lv_addressof))
2729 return error_mark_node;
2731 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2732 argtype = TREE_TYPE (arg);
2734 /* If the lvalue is const or volatile, merge that into the type
2735 to which the address will point. Note that you can't get a
2736 restricted pointer by taking the address of something, so we
2737 only have to deal with `const' and `volatile' here. */
2738 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
2739 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2740 argtype = c_build_type_variant (argtype,
2741 TREE_READONLY (arg),
2742 TREE_THIS_VOLATILE (arg));
2744 if (!c_mark_addressable (arg))
2745 return error_mark_node;
2747 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
2748 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
2750 argtype = build_pointer_type (argtype);
2752 /* ??? Cope with user tricks that amount to offsetof. Delete this
2753 when we have proper support for integer constant expressions. */
2754 val = get_base_address (arg);
2755 if (val && TREE_CODE (val) == INDIRECT_REF
2756 && integer_zerop (TREE_OPERAND (val, 0)))
2757 return fold_convert (argtype, fold_offsetof (arg));
2759 val = build1 (ADDR_EXPR, argtype, arg);
2761 if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR)
2762 TREE_INVARIANT (val) = TREE_CONSTANT (val) = 1;
2764 return val;
2766 default:
2767 break;
2770 if (argtype == 0)
2771 argtype = TREE_TYPE (arg);
2772 val = build1 (code, argtype, arg);
2773 return require_constant_value ? fold_initializer (val) : fold (val);
2776 /* Return nonzero if REF is an lvalue valid for this language.
2777 Lvalues can be assigned, unless their type has TYPE_READONLY.
2778 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
2780 static int
2781 lvalue_p (tree ref)
2783 enum tree_code code = TREE_CODE (ref);
2785 switch (code)
2787 case REALPART_EXPR:
2788 case IMAGPART_EXPR:
2789 case COMPONENT_REF:
2790 return lvalue_p (TREE_OPERAND (ref, 0));
2792 case COMPOUND_LITERAL_EXPR:
2793 case STRING_CST:
2794 return 1;
2796 case INDIRECT_REF:
2797 case ARRAY_REF:
2798 case VAR_DECL:
2799 case PARM_DECL:
2800 case RESULT_DECL:
2801 case ERROR_MARK:
2802 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2803 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2805 case BIND_EXPR:
2806 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2808 default:
2809 return 0;
2813 /* Give an error for storing in something that is 'const'. */
2815 static void
2816 readonly_error (tree arg, enum lvalue_use use)
2818 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement);
2819 /* Using this macro rather than (for example) arrays of messages
2820 ensures that all the format strings are checked at compile
2821 time. */
2822 #define READONLY_MSG(A, I, D) (use == lv_assign \
2823 ? (A) \
2824 : (use == lv_increment ? (I) : (D)))
2825 if (TREE_CODE (arg) == COMPONENT_REF)
2827 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2828 readonly_error (TREE_OPERAND (arg, 0), use);
2829 else
2830 error (READONLY_MSG (N_("assignment of read-only member %qD"),
2831 N_("increment of read-only member %qD"),
2832 N_("decrement of read-only member %qD")),
2833 TREE_OPERAND (arg, 1));
2835 else if (TREE_CODE (arg) == VAR_DECL)
2836 error (READONLY_MSG (N_("assignment of read-only variable %qD"),
2837 N_("increment of read-only variable %qD"),
2838 N_("decrement of read-only variable %qD")),
2839 arg);
2840 else
2841 error (READONLY_MSG (N_("assignment of read-only location"),
2842 N_("increment of read-only location"),
2843 N_("decrement of read-only location")));
2847 /* Return nonzero if REF is an lvalue valid for this language;
2848 otherwise, print an error message and return zero. USE says
2849 how the lvalue is being used and so selects the error message. */
2851 static int
2852 lvalue_or_else (tree ref, enum lvalue_use use)
2854 int win = lvalue_p (ref);
2856 if (!win)
2857 lvalue_error (use);
2859 return win;
2862 /* Mark EXP saying that we need to be able to take the
2863 address of it; it should not be allocated in a register.
2864 Returns true if successful. */
2866 bool
2867 c_mark_addressable (tree exp)
2869 tree x = exp;
2871 while (1)
2872 switch (TREE_CODE (x))
2874 case COMPONENT_REF:
2875 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2877 error
2878 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
2879 return false;
2882 /* ... fall through ... */
2884 case ADDR_EXPR:
2885 case ARRAY_REF:
2886 case REALPART_EXPR:
2887 case IMAGPART_EXPR:
2888 x = TREE_OPERAND (x, 0);
2889 break;
2891 case COMPOUND_LITERAL_EXPR:
2892 case CONSTRUCTOR:
2893 TREE_ADDRESSABLE (x) = 1;
2894 return true;
2896 case VAR_DECL:
2897 case CONST_DECL:
2898 case PARM_DECL:
2899 case RESULT_DECL:
2900 if (C_DECL_REGISTER (x)
2901 && DECL_NONLOCAL (x))
2903 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2905 error
2906 ("global register variable %qD used in nested function", x);
2907 return false;
2909 pedwarn ("register variable %qD used in nested function", x);
2911 else if (C_DECL_REGISTER (x))
2913 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2914 error ("address of global register variable %qD requested", x);
2915 else
2916 error ("address of register variable %qD requested", x);
2917 return false;
2920 /* drops in */
2921 case FUNCTION_DECL:
2922 TREE_ADDRESSABLE (x) = 1;
2923 /* drops out */
2924 default:
2925 return true;
2929 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2931 tree
2932 build_conditional_expr (tree ifexp, tree op1, tree op2)
2934 tree type1;
2935 tree type2;
2936 enum tree_code code1;
2937 enum tree_code code2;
2938 tree result_type = NULL;
2939 tree orig_op1 = op1, orig_op2 = op2;
2941 /* Promote both alternatives. */
2943 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2944 op1 = default_conversion (op1);
2945 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2946 op2 = default_conversion (op2);
2948 if (TREE_CODE (ifexp) == ERROR_MARK
2949 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2950 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2951 return error_mark_node;
2953 type1 = TREE_TYPE (op1);
2954 code1 = TREE_CODE (type1);
2955 type2 = TREE_TYPE (op2);
2956 code2 = TREE_CODE (type2);
2958 /* C90 does not permit non-lvalue arrays in conditional expressions.
2959 In C99 they will be pointers by now. */
2960 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2962 error ("non-lvalue array in conditional expression");
2963 return error_mark_node;
2966 /* Quickly detect the usual case where op1 and op2 have the same type
2967 after promotion. */
2968 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2970 if (type1 == type2)
2971 result_type = type1;
2972 else
2973 result_type = TYPE_MAIN_VARIANT (type1);
2975 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2976 || code1 == COMPLEX_TYPE)
2977 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2978 || code2 == COMPLEX_TYPE))
2980 result_type = c_common_type (type1, type2);
2982 /* If -Wsign-compare, warn here if type1 and type2 have
2983 different signedness. We'll promote the signed to unsigned
2984 and later code won't know it used to be different.
2985 Do this check on the original types, so that explicit casts
2986 will be considered, but default promotions won't. */
2987 if (warn_sign_compare && !skip_evaluation)
2989 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
2990 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
2992 if (unsigned_op1 ^ unsigned_op2)
2994 /* Do not warn if the result type is signed, since the
2995 signed type will only be chosen if it can represent
2996 all the values of the unsigned type. */
2997 if (!TYPE_UNSIGNED (result_type))
2998 /* OK */;
2999 /* Do not warn if the signed quantity is an unsuffixed
3000 integer literal (or some static constant expression
3001 involving such literals) and it is non-negative. */
3002 else if ((unsigned_op2 && tree_expr_nonnegative_p (op1))
3003 || (unsigned_op1 && tree_expr_nonnegative_p (op2)))
3004 /* OK */;
3005 else
3006 warning (0, "signed and unsigned type in conditional expression");
3010 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3012 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
3013 pedwarn ("ISO C forbids conditional expr with only one void side");
3014 result_type = void_type_node;
3016 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3018 if (comp_target_types (type1, type2, 1))
3019 result_type = common_pointer_type (type1, type2);
3020 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3021 && TREE_CODE (orig_op1) != NOP_EXPR)
3022 result_type = qualify_type (type2, type1);
3023 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3024 && TREE_CODE (orig_op2) != NOP_EXPR)
3025 result_type = qualify_type (type1, type2);
3026 else if (VOID_TYPE_P (TREE_TYPE (type1)))
3028 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
3029 pedwarn ("ISO C forbids conditional expr between "
3030 "%<void *%> and function pointer");
3031 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3032 TREE_TYPE (type2)));
3034 else if (VOID_TYPE_P (TREE_TYPE (type2)))
3036 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
3037 pedwarn ("ISO C forbids conditional expr between "
3038 "%<void *%> and function pointer");
3039 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3040 TREE_TYPE (type1)));
3042 else
3044 pedwarn ("pointer type mismatch in conditional expression");
3045 result_type = build_pointer_type (void_type_node);
3048 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3050 if (!integer_zerop (op2))
3051 pedwarn ("pointer/integer type mismatch in conditional expression");
3052 else
3054 op2 = null_pointer_node;
3056 result_type = type1;
3058 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3060 if (!integer_zerop (op1))
3061 pedwarn ("pointer/integer type mismatch in conditional expression");
3062 else
3064 op1 = null_pointer_node;
3066 result_type = type2;
3069 if (!result_type)
3071 if (flag_cond_mismatch)
3072 result_type = void_type_node;
3073 else
3075 error ("type mismatch in conditional expression");
3076 return error_mark_node;
3080 /* Merge const and volatile flags of the incoming types. */
3081 result_type
3082 = build_type_variant (result_type,
3083 TREE_READONLY (op1) || TREE_READONLY (op2),
3084 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
3086 if (result_type != TREE_TYPE (op1))
3087 op1 = convert_and_check (result_type, op1);
3088 if (result_type != TREE_TYPE (op2))
3089 op2 = convert_and_check (result_type, op2);
3091 if (TREE_CODE (ifexp) == INTEGER_CST)
3092 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
3094 return fold (build3 (COND_EXPR, result_type, ifexp, op1, op2));
3097 /* Return a compound expression that performs two expressions and
3098 returns the value of the second of them. */
3100 tree
3101 build_compound_expr (tree expr1, tree expr2)
3103 /* Convert arrays and functions to pointers. */
3104 expr2 = default_function_array_conversion (expr2);
3106 if (!TREE_SIDE_EFFECTS (expr1))
3108 /* The left-hand operand of a comma expression is like an expression
3109 statement: with -Wextra or -Wunused, we should warn if it doesn't have
3110 any side-effects, unless it was explicitly cast to (void). */
3111 if (warn_unused_value)
3113 if (VOID_TYPE_P (TREE_TYPE (expr1))
3114 && TREE_CODE (expr1) == CONVERT_EXPR)
3115 ; /* (void) a, b */
3116 else if (VOID_TYPE_P (TREE_TYPE (expr1))
3117 && TREE_CODE (expr1) == COMPOUND_EXPR
3118 && TREE_CODE (TREE_OPERAND (expr1, 1)) == CONVERT_EXPR)
3119 ; /* (void) a, (void) b, c */
3120 else
3121 warning (0, "left-hand operand of comma expression has no effect");
3125 /* With -Wunused, we should also warn if the left-hand operand does have
3126 side-effects, but computes a value which is not used. For example, in
3127 `foo() + bar(), baz()' the result of the `+' operator is not used,
3128 so we should issue a warning. */
3129 else if (warn_unused_value)
3130 warn_if_unused_value (expr1, input_location);
3132 return build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
3135 /* Build an expression representing a cast to type TYPE of expression EXPR. */
3137 tree
3138 build_c_cast (tree type, tree expr)
3140 tree value = expr;
3142 if (type == error_mark_node || expr == error_mark_node)
3143 return error_mark_node;
3145 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3146 only in <protocol> qualifications. But when constructing cast expressions,
3147 the protocols do matter and must be kept around. */
3148 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
3149 return build1 (NOP_EXPR, type, expr);
3151 type = TYPE_MAIN_VARIANT (type);
3153 if (TREE_CODE (type) == ARRAY_TYPE)
3155 error ("cast specifies array type");
3156 return error_mark_node;
3159 if (TREE_CODE (type) == FUNCTION_TYPE)
3161 error ("cast specifies function type");
3162 return error_mark_node;
3165 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
3167 if (pedantic)
3169 if (TREE_CODE (type) == RECORD_TYPE
3170 || TREE_CODE (type) == UNION_TYPE)
3171 pedwarn ("ISO C forbids casting nonscalar to the same type");
3174 else if (TREE_CODE (type) == UNION_TYPE)
3176 tree field;
3177 value = default_function_array_conversion (value);
3179 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3180 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
3181 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3182 break;
3184 if (field)
3186 tree t;
3188 if (pedantic)
3189 pedwarn ("ISO C forbids casts to union type");
3190 t = digest_init (type,
3191 build_constructor (type,
3192 build_tree_list (field, value)),
3193 true, 0);
3194 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3195 TREE_INVARIANT (t) = TREE_INVARIANT (value);
3196 return t;
3198 error ("cast to union type from type not present in union");
3199 return error_mark_node;
3201 else
3203 tree otype, ovalue;
3205 /* If casting to void, avoid the error that would come
3206 from default_conversion in the case of a non-lvalue array. */
3207 if (type == void_type_node)
3208 return build1 (CONVERT_EXPR, type, value);
3210 /* Convert functions and arrays to pointers,
3211 but don't convert any other types. */
3212 value = default_function_array_conversion (value);
3213 otype = TREE_TYPE (value);
3215 /* Optionally warn about potentially worrisome casts. */
3217 if (warn_cast_qual
3218 && TREE_CODE (type) == POINTER_TYPE
3219 && TREE_CODE (otype) == POINTER_TYPE)
3221 tree in_type = type;
3222 tree in_otype = otype;
3223 int added = 0;
3224 int discarded = 0;
3226 /* Check that the qualifiers on IN_TYPE are a superset of
3227 the qualifiers of IN_OTYPE. The outermost level of
3228 POINTER_TYPE nodes is uninteresting and we stop as soon
3229 as we hit a non-POINTER_TYPE node on either type. */
3232 in_otype = TREE_TYPE (in_otype);
3233 in_type = TREE_TYPE (in_type);
3235 /* GNU C allows cv-qualified function types. 'const'
3236 means the function is very pure, 'volatile' means it
3237 can't return. We need to warn when such qualifiers
3238 are added, not when they're taken away. */
3239 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3240 && TREE_CODE (in_type) == FUNCTION_TYPE)
3241 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3242 else
3243 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
3245 while (TREE_CODE (in_type) == POINTER_TYPE
3246 && TREE_CODE (in_otype) == POINTER_TYPE);
3248 if (added)
3249 warning (0, "cast adds new qualifiers to function type");
3251 if (discarded)
3252 /* There are qualifiers present in IN_OTYPE that are not
3253 present in IN_TYPE. */
3254 warning (0, "cast discards qualifiers from pointer target type");
3257 /* Warn about possible alignment problems. */
3258 if (STRICT_ALIGNMENT && warn_cast_align
3259 && TREE_CODE (type) == POINTER_TYPE
3260 && TREE_CODE (otype) == POINTER_TYPE
3261 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3262 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3263 /* Don't warn about opaque types, where the actual alignment
3264 restriction is unknown. */
3265 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3266 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3267 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3268 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3269 warning (0, "cast increases required alignment of target type");
3271 if (warn_pointer_to_int_cast
3272 && TREE_CODE (type) == INTEGER_TYPE
3273 && TREE_CODE (otype) == POINTER_TYPE
3274 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3275 && !TREE_CONSTANT (value))
3276 warning (0, "cast from pointer to integer of different size");
3278 if (warn_bad_function_cast
3279 && TREE_CODE (value) == CALL_EXPR
3280 && TREE_CODE (type) != TREE_CODE (otype))
3281 warning (0, "cast from function call of type %qT to non-matching "
3282 "type %qT", otype, type);
3284 if (warn_int_to_pointer_cast
3285 && TREE_CODE (type) == POINTER_TYPE
3286 && TREE_CODE (otype) == INTEGER_TYPE
3287 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3288 /* Don't warn about converting any constant. */
3289 && !TREE_CONSTANT (value))
3290 warning (0, "cast to pointer from integer of different size");
3292 if (TREE_CODE (type) == POINTER_TYPE
3293 && TREE_CODE (otype) == POINTER_TYPE
3294 && TREE_CODE (expr) == ADDR_EXPR
3295 && DECL_P (TREE_OPERAND (expr, 0))
3296 && flag_strict_aliasing && warn_strict_aliasing
3297 && !VOID_TYPE_P (TREE_TYPE (type)))
3299 /* Casting the address of a decl to non void pointer. Warn
3300 if the cast breaks type based aliasing. */
3301 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3302 warning (0, "type-punning to incomplete type might break strict-aliasing rules");
3303 else
3305 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3306 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3308 if (!alias_sets_conflict_p (set1, set2))
3309 warning (0, "dereferencing type-punned pointer will break strict-aliasing rules");
3310 else if (warn_strict_aliasing > 1
3311 && !alias_sets_might_conflict_p (set1, set2))
3312 warning (0, "dereferencing type-punned pointer might break strict-aliasing rules");
3316 /* If pedantic, warn for conversions between function and object
3317 pointer types, except for converting a null pointer constant
3318 to function pointer type. */
3319 if (pedantic
3320 && TREE_CODE (type) == POINTER_TYPE
3321 && TREE_CODE (otype) == POINTER_TYPE
3322 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3323 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3324 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3326 if (pedantic
3327 && TREE_CODE (type) == POINTER_TYPE
3328 && TREE_CODE (otype) == POINTER_TYPE
3329 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3330 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3331 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3332 && TREE_CODE (expr) != NOP_EXPR))
3333 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3335 ovalue = value;
3336 value = convert (type, value);
3338 /* Ignore any integer overflow caused by the cast. */
3339 if (TREE_CODE (value) == INTEGER_CST)
3341 if (EXPR_P (ovalue))
3342 /* If OVALUE had overflow set, then so will VALUE, so it
3343 is safe to overwrite. */
3344 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3345 else
3346 TREE_OVERFLOW (value) = 0;
3348 if (CONSTANT_CLASS_P (ovalue))
3349 /* Similarly, constant_overflow cannot have become
3350 cleared. */
3351 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3355 /* Don't let a cast be an lvalue. */
3356 if (value == expr)
3357 value = non_lvalue (value);
3359 return value;
3362 /* Interpret a cast of expression EXPR to type TYPE. */
3363 tree
3364 c_cast_expr (struct c_type_name *type_name, tree expr)
3366 tree type;
3367 int saved_wsp = warn_strict_prototypes;
3369 /* This avoids warnings about unprototyped casts on
3370 integers. E.g. "#define SIG_DFL (void(*)())0". */
3371 if (TREE_CODE (expr) == INTEGER_CST)
3372 warn_strict_prototypes = 0;
3373 type = groktypename (type_name);
3374 warn_strict_prototypes = saved_wsp;
3376 return build_c_cast (type, expr);
3380 /* Build an assignment expression of lvalue LHS from value RHS.
3381 MODIFYCODE is the code for a binary operator that we use
3382 to combine the old value of LHS with RHS to get the new value.
3383 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3385 tree
3386 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3388 tree result;
3389 tree newrhs;
3390 tree lhstype = TREE_TYPE (lhs);
3391 tree olhstype = lhstype;
3393 /* Types that aren't fully specified cannot be used in assignments. */
3394 lhs = require_complete_type (lhs);
3396 /* Avoid duplicate error messages from operands that had errors. */
3397 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3398 return error_mark_node;
3400 STRIP_TYPE_NOPS (rhs);
3402 newrhs = rhs;
3404 /* If a binary op has been requested, combine the old LHS value with the RHS
3405 producing the value we should actually store into the LHS. */
3407 if (modifycode != NOP_EXPR)
3409 lhs = stabilize_reference (lhs);
3410 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3413 if (!lvalue_or_else (lhs, lv_assign))
3414 return error_mark_node;
3416 /* Give an error for storing in something that is 'const'. */
3418 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3419 || ((TREE_CODE (lhstype) == RECORD_TYPE
3420 || TREE_CODE (lhstype) == UNION_TYPE)
3421 && C_TYPE_FIELDS_READONLY (lhstype)))
3422 readonly_error (lhs, lv_assign);
3424 /* If storing into a structure or union member,
3425 it has probably been given type `int'.
3426 Compute the type that would go with
3427 the actual amount of storage the member occupies. */
3429 if (TREE_CODE (lhs) == COMPONENT_REF
3430 && (TREE_CODE (lhstype) == INTEGER_TYPE
3431 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3432 || TREE_CODE (lhstype) == REAL_TYPE
3433 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3434 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3436 /* If storing in a field that is in actuality a short or narrower than one,
3437 we must store in the field in its actual type. */
3439 if (lhstype != TREE_TYPE (lhs))
3441 lhs = copy_node (lhs);
3442 TREE_TYPE (lhs) = lhstype;
3445 /* Convert new value to destination type. */
3447 newrhs = convert_for_assignment (lhstype, newrhs, ic_assign,
3448 NULL_TREE, NULL_TREE, 0);
3449 if (TREE_CODE (newrhs) == ERROR_MARK)
3450 return error_mark_node;
3452 /* Scan operands. */
3454 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3455 TREE_SIDE_EFFECTS (result) = 1;
3457 /* If we got the LHS in a different type for storing in,
3458 convert the result back to the nominal type of LHS
3459 so that the value we return always has the same type
3460 as the LHS argument. */
3462 if (olhstype == TREE_TYPE (result))
3463 return result;
3464 return convert_for_assignment (olhstype, result, ic_assign,
3465 NULL_TREE, NULL_TREE, 0);
3468 /* Convert value RHS to type TYPE as preparation for an assignment
3469 to an lvalue of type TYPE.
3470 The real work of conversion is done by `convert'.
3471 The purpose of this function is to generate error messages
3472 for assignments that are not allowed in C.
3473 ERRTYPE says whether it is argument passing, assignment,
3474 initialization or return.
3476 FUNCTION is a tree for the function being called.
3477 PARMNUM is the number of the argument, for printing in error messages. */
3479 static tree
3480 convert_for_assignment (tree type, tree rhs, enum impl_conv errtype,
3481 tree fundecl, tree function, int parmnum)
3483 enum tree_code codel = TREE_CODE (type);
3484 tree rhstype;
3485 enum tree_code coder;
3486 tree rname = NULL_TREE;
3488 if (errtype == ic_argpass || errtype == ic_argpass_nonproto)
3490 tree selector;
3491 /* Change pointer to function to the function itself for
3492 diagnostics. */
3493 if (TREE_CODE (function) == ADDR_EXPR
3494 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3495 function = TREE_OPERAND (function, 0);
3497 /* Handle an ObjC selector specially for diagnostics. */
3498 selector = objc_message_selector ();
3499 rname = function;
3500 if (selector && parmnum > 2)
3502 rname = selector;
3503 parmnum -= 2;
3507 /* This macro is used to emit diagnostics to ensure that all format
3508 strings are complete sentences, visible to gettext and checked at
3509 compile time. */
3510 #define WARN_FOR_ASSIGNMENT(AR, AS, IN, RE) \
3511 do { \
3512 switch (errtype) \
3514 case ic_argpass: \
3515 pedwarn (AR, parmnum, rname); \
3516 break; \
3517 case ic_argpass_nonproto: \
3518 warning (0, AR, parmnum, rname); \
3519 break; \
3520 case ic_assign: \
3521 pedwarn (AS); \
3522 break; \
3523 case ic_init: \
3524 pedwarn (IN); \
3525 break; \
3526 case ic_return: \
3527 pedwarn (RE); \
3528 break; \
3529 default: \
3530 gcc_unreachable (); \
3532 } while (0)
3534 STRIP_TYPE_NOPS (rhs);
3536 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3537 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3538 rhs = default_conversion (rhs);
3539 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3540 rhs = decl_constant_value_for_broken_optimization (rhs);
3542 rhstype = TREE_TYPE (rhs);
3543 coder = TREE_CODE (rhstype);
3545 if (coder == ERROR_MARK)
3546 return error_mark_node;
3548 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3550 overflow_warning (rhs);
3551 /* Check for Objective-C protocols. This will automatically
3552 issue a warning if there are protocol violations. No need to
3553 use the return value. */
3554 if (c_dialect_objc ())
3555 objc_comptypes (type, rhstype, 0);
3556 return rhs;
3559 if (coder == VOID_TYPE)
3561 /* Except for passing an argument to an unprototyped function,
3562 this is a constraint violation. When passing an argument to
3563 an unprototyped function, it is compile-time undefined;
3564 making it a constraint in that case was rejected in
3565 DR#252. */
3566 error ("void value not ignored as it ought to be");
3567 return error_mark_node;
3569 /* A type converts to a reference to it.
3570 This code doesn't fully support references, it's just for the
3571 special case of va_start and va_copy. */
3572 if (codel == REFERENCE_TYPE
3573 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
3575 if (!lvalue_p (rhs))
3577 error ("cannot pass rvalue to reference parameter");
3578 return error_mark_node;
3580 if (!c_mark_addressable (rhs))
3581 return error_mark_node;
3582 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3584 /* We already know that these two types are compatible, but they
3585 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3586 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3587 likely to be va_list, a typedef to __builtin_va_list, which
3588 is different enough that it will cause problems later. */
3589 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3590 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3592 rhs = build1 (NOP_EXPR, type, rhs);
3593 return rhs;
3595 /* Some types can interconvert without explicit casts. */
3596 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3597 && vector_types_convertible_p (type, TREE_TYPE (rhs)))
3598 return convert (type, rhs);
3599 /* Arithmetic types all interconvert, and enum is treated like int. */
3600 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3601 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3602 || codel == BOOLEAN_TYPE)
3603 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3604 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3605 || coder == BOOLEAN_TYPE))
3606 return convert_and_check (type, rhs);
3608 /* Conversion to a transparent union from its member types.
3609 This applies only to function arguments. */
3610 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type)
3611 && (errtype == ic_argpass || errtype == ic_argpass_nonproto))
3613 tree memb_types;
3614 tree marginal_memb_type = 0;
3616 for (memb_types = TYPE_FIELDS (type); memb_types;
3617 memb_types = TREE_CHAIN (memb_types))
3619 tree memb_type = TREE_TYPE (memb_types);
3621 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3622 TYPE_MAIN_VARIANT (rhstype)))
3623 break;
3625 if (TREE_CODE (memb_type) != POINTER_TYPE)
3626 continue;
3628 if (coder == POINTER_TYPE)
3630 tree ttl = TREE_TYPE (memb_type);
3631 tree ttr = TREE_TYPE (rhstype);
3633 /* Any non-function converts to a [const][volatile] void *
3634 and vice versa; otherwise, targets must be the same.
3635 Meanwhile, the lhs target must have all the qualifiers of
3636 the rhs. */
3637 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3638 || comp_target_types (memb_type, rhstype, 0))
3640 /* If this type won't generate any warnings, use it. */
3641 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3642 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3643 && TREE_CODE (ttl) == FUNCTION_TYPE)
3644 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3645 == TYPE_QUALS (ttr))
3646 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3647 == TYPE_QUALS (ttl))))
3648 break;
3650 /* Keep looking for a better type, but remember this one. */
3651 if (!marginal_memb_type)
3652 marginal_memb_type = memb_type;
3656 /* Can convert integer zero to any pointer type. */
3657 if (integer_zerop (rhs)
3658 || (TREE_CODE (rhs) == NOP_EXPR
3659 && integer_zerop (TREE_OPERAND (rhs, 0))))
3661 rhs = null_pointer_node;
3662 break;
3666 if (memb_types || marginal_memb_type)
3668 if (!memb_types)
3670 /* We have only a marginally acceptable member type;
3671 it needs a warning. */
3672 tree ttl = TREE_TYPE (marginal_memb_type);
3673 tree ttr = TREE_TYPE (rhstype);
3675 /* Const and volatile mean something different for function
3676 types, so the usual warnings are not appropriate. */
3677 if (TREE_CODE (ttr) == FUNCTION_TYPE
3678 && TREE_CODE (ttl) == FUNCTION_TYPE)
3680 /* Because const and volatile on functions are
3681 restrictions that say the function will not do
3682 certain things, it is okay to use a const or volatile
3683 function where an ordinary one is wanted, but not
3684 vice-versa. */
3685 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3686 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE "
3687 "makes qualified function "
3688 "pointer from unqualified"),
3689 N_("assignment makes qualified "
3690 "function pointer from "
3691 "unqualified"),
3692 N_("initialization makes qualified "
3693 "function pointer from "
3694 "unqualified"),
3695 N_("return makes qualified function "
3696 "pointer from unqualified"));
3698 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3699 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3700 "qualifiers from pointer target type"),
3701 N_("assignment discards qualifiers "
3702 "from pointer target type"),
3703 N_("initialization discards qualifiers "
3704 "from pointer target type"),
3705 N_("return discards qualifiers from "
3706 "pointer target type"));
3709 if (pedantic && !DECL_IN_SYSTEM_HEADER (fundecl))
3710 pedwarn ("ISO C prohibits argument conversion to union type");
3712 return build1 (NOP_EXPR, type, rhs);
3716 /* Conversions among pointers */
3717 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3718 && (coder == codel))
3720 tree ttl = TREE_TYPE (type);
3721 tree ttr = TREE_TYPE (rhstype);
3722 tree mvl = ttl;
3723 tree mvr = ttr;
3724 bool is_opaque_pointer;
3725 int target_cmp = 0; /* Cache comp_target_types () result. */
3727 if (TREE_CODE (mvl) != ARRAY_TYPE)
3728 mvl = TYPE_MAIN_VARIANT (mvl);
3729 if (TREE_CODE (mvr) != ARRAY_TYPE)
3730 mvr = TYPE_MAIN_VARIANT (mvr);
3731 /* Opaque pointers are treated like void pointers. */
3732 is_opaque_pointer = (targetm.vector_opaque_p (type)
3733 || targetm.vector_opaque_p (rhstype))
3734 && TREE_CODE (ttl) == VECTOR_TYPE
3735 && TREE_CODE (ttr) == VECTOR_TYPE;
3737 /* Any non-function converts to a [const][volatile] void *
3738 and vice versa; otherwise, targets must be the same.
3739 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3740 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3741 || (target_cmp = comp_target_types (type, rhstype, 0))
3742 || is_opaque_pointer
3743 || (c_common_unsigned_type (mvl)
3744 == c_common_unsigned_type (mvr)))
3746 if (pedantic
3747 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3749 (VOID_TYPE_P (ttr)
3750 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3751 which are not ANSI null ptr constants. */
3752 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3753 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3754 WARN_FOR_ASSIGNMENT (N_("ISO C forbids passing argument %d of "
3755 "%qE between function pointer "
3756 "and %<void *%>"),
3757 N_("ISO C forbids assignment between "
3758 "function pointer and %<void *%>"),
3759 N_("ISO C forbids initialization between "
3760 "function pointer and %<void *%>"),
3761 N_("ISO C forbids return between function "
3762 "pointer and %<void *%>"));
3763 /* Const and volatile mean something different for function types,
3764 so the usual warnings are not appropriate. */
3765 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3766 && TREE_CODE (ttl) != FUNCTION_TYPE)
3768 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3769 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE discards "
3770 "qualifiers from pointer target type"),
3771 N_("assignment discards qualifiers "
3772 "from pointer target type"),
3773 N_("initialization discards qualifiers "
3774 "from pointer target type"),
3775 N_("return discards qualifiers from "
3776 "pointer target type"));
3777 /* If this is not a case of ignoring a mismatch in signedness,
3778 no warning. */
3779 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3780 || target_cmp)
3782 /* If there is a mismatch, do warn. */
3783 else if (warn_pointer_sign)
3784 WARN_FOR_ASSIGNMENT (N_("pointer targets in passing argument "
3785 "%d of %qE differ in signedness"),
3786 N_("pointer targets in assignment "
3787 "differ in signedness"),
3788 N_("pointer targets in initialization "
3789 "differ in signedness"),
3790 N_("pointer targets in return differ "
3791 "in signedness"));
3793 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3794 && TREE_CODE (ttr) == FUNCTION_TYPE)
3796 /* Because const and volatile on functions are restrictions
3797 that say the function will not do certain things,
3798 it is okay to use a const or volatile function
3799 where an ordinary one is wanted, but not vice-versa. */
3800 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3801 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3802 "qualified function pointer "
3803 "from unqualified"),
3804 N_("assignment makes qualified function "
3805 "pointer from unqualified"),
3806 N_("initialization makes qualified "
3807 "function pointer from unqualified"),
3808 N_("return makes qualified function "
3809 "pointer from unqualified"));
3812 else
3813 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE from "
3814 "incompatible pointer type"),
3815 N_("assignment from incompatible pointer type"),
3816 N_("initialization from incompatible "
3817 "pointer type"),
3818 N_("return from incompatible pointer type"));
3819 return convert (type, rhs);
3821 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3823 /* ??? This should not be an error when inlining calls to
3824 unprototyped functions. */
3825 error ("invalid use of non-lvalue array");
3826 return error_mark_node;
3828 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3830 /* An explicit constant 0 can convert to a pointer,
3831 or one that results from arithmetic, even including
3832 a cast to integer type. */
3833 if (!(TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3835 !(TREE_CODE (rhs) == NOP_EXPR
3836 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3837 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3838 && integer_zerop (TREE_OPERAND (rhs, 0))))
3839 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes "
3840 "pointer from integer without a cast"),
3841 N_("assignment makes pointer from integer "
3842 "without a cast"),
3843 N_("initialization makes pointer from "
3844 "integer without a cast"),
3845 N_("return makes pointer from integer "
3846 "without a cast"));
3848 return convert (type, rhs);
3850 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3852 WARN_FOR_ASSIGNMENT (N_("passing argument %d of %qE makes integer "
3853 "from pointer without a cast"),
3854 N_("assignment makes integer from pointer "
3855 "without a cast"),
3856 N_("initialization makes integer from pointer "
3857 "without a cast"),
3858 N_("return makes integer from pointer "
3859 "without a cast"));
3860 return convert (type, rhs);
3862 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3863 return convert (type, rhs);
3865 switch (errtype)
3867 case ic_argpass:
3868 case ic_argpass_nonproto:
3869 /* ??? This should not be an error when inlining calls to
3870 unprototyped functions. */
3871 error ("incompatible type for argument %d of %qE", parmnum, rname);
3872 break;
3873 case ic_assign:
3874 error ("incompatible types in assignment");
3875 break;
3876 case ic_init:
3877 error ("incompatible types in initialization");
3878 break;
3879 case ic_return:
3880 error ("incompatible types in return");
3881 break;
3882 default:
3883 gcc_unreachable ();
3886 return error_mark_node;
3889 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3890 is used for error and waring reporting and indicates which argument
3891 is being processed. */
3893 tree
3894 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3896 tree ret, type;
3898 /* If FN was prototyped, the value has been converted already
3899 in convert_arguments. */
3900 if (!value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3901 return value;
3903 type = TREE_TYPE (parm);
3904 ret = convert_for_assignment (type, value,
3905 ic_argpass_nonproto, fn,
3906 fn, argnum);
3907 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3908 && INTEGRAL_TYPE_P (type)
3909 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3910 ret = default_conversion (ret);
3911 return ret;
3914 /* If VALUE is a compound expr all of whose expressions are constant, then
3915 return its value. Otherwise, return error_mark_node.
3917 This is for handling COMPOUND_EXPRs as initializer elements
3918 which is allowed with a warning when -pedantic is specified. */
3920 static tree
3921 valid_compound_expr_initializer (tree value, tree endtype)
3923 if (TREE_CODE (value) == COMPOUND_EXPR)
3925 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3926 == error_mark_node)
3927 return error_mark_node;
3928 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3929 endtype);
3931 else if (!initializer_constant_valid_p (value, endtype))
3932 return error_mark_node;
3933 else
3934 return value;
3937 /* Perform appropriate conversions on the initial value of a variable,
3938 store it in the declaration DECL,
3939 and print any error messages that are appropriate.
3940 If the init is invalid, store an ERROR_MARK. */
3942 void
3943 store_init_value (tree decl, tree init)
3945 tree value, type;
3947 /* If variable's type was invalidly declared, just ignore it. */
3949 type = TREE_TYPE (decl);
3950 if (TREE_CODE (type) == ERROR_MARK)
3951 return;
3953 /* Digest the specified initializer into an expression. */
3955 value = digest_init (type, init, true, TREE_STATIC (decl));
3957 /* Store the expression if valid; else report error. */
3959 if (warn_traditional && !in_system_header
3960 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3961 warning (0, "traditional C rejects automatic aggregate initialization");
3963 DECL_INITIAL (decl) = value;
3965 /* ANSI wants warnings about out-of-range constant initializers. */
3966 STRIP_TYPE_NOPS (value);
3967 constant_expression_warning (value);
3969 /* Check if we need to set array size from compound literal size. */
3970 if (TREE_CODE (type) == ARRAY_TYPE
3971 && TYPE_DOMAIN (type) == 0
3972 && value != error_mark_node)
3974 tree inside_init = init;
3976 STRIP_TYPE_NOPS (inside_init);
3977 inside_init = fold (inside_init);
3979 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3981 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3983 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3985 /* For int foo[] = (int [3]){1}; we need to set array size
3986 now since later on array initializer will be just the
3987 brace enclosed list of the compound literal. */
3988 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3989 layout_type (type);
3990 layout_decl (decl, 0);
3996 /* Methods for storing and printing names for error messages. */
3998 /* Implement a spelling stack that allows components of a name to be pushed
3999 and popped. Each element on the stack is this structure. */
4001 struct spelling
4003 int kind;
4004 union
4006 int i;
4007 const char *s;
4008 } u;
4011 #define SPELLING_STRING 1
4012 #define SPELLING_MEMBER 2
4013 #define SPELLING_BOUNDS 3
4015 static struct spelling *spelling; /* Next stack element (unused). */
4016 static struct spelling *spelling_base; /* Spelling stack base. */
4017 static int spelling_size; /* Size of the spelling stack. */
4019 /* Macros to save and restore the spelling stack around push_... functions.
4020 Alternative to SAVE_SPELLING_STACK. */
4022 #define SPELLING_DEPTH() (spelling - spelling_base)
4023 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
4025 /* Push an element on the spelling stack with type KIND and assign VALUE
4026 to MEMBER. */
4028 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4030 int depth = SPELLING_DEPTH (); \
4032 if (depth >= spelling_size) \
4034 spelling_size += 10; \
4035 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
4036 spelling_size); \
4037 RESTORE_SPELLING_DEPTH (depth); \
4040 spelling->kind = (KIND); \
4041 spelling->MEMBER = (VALUE); \
4042 spelling++; \
4045 /* Push STRING on the stack. Printed literally. */
4047 static void
4048 push_string (const char *string)
4050 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4053 /* Push a member name on the stack. Printed as '.' STRING. */
4055 static void
4056 push_member_name (tree decl)
4058 const char *const string
4059 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
4060 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4063 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
4065 static void
4066 push_array_bounds (int bounds)
4068 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4071 /* Compute the maximum size in bytes of the printed spelling. */
4073 static int
4074 spelling_length (void)
4076 int size = 0;
4077 struct spelling *p;
4079 for (p = spelling_base; p < spelling; p++)
4081 if (p->kind == SPELLING_BOUNDS)
4082 size += 25;
4083 else
4084 size += strlen (p->u.s) + 1;
4087 return size;
4090 /* Print the spelling to BUFFER and return it. */
4092 static char *
4093 print_spelling (char *buffer)
4095 char *d = buffer;
4096 struct spelling *p;
4098 for (p = spelling_base; p < spelling; p++)
4099 if (p->kind == SPELLING_BOUNDS)
4101 sprintf (d, "[%d]", p->u.i);
4102 d += strlen (d);
4104 else
4106 const char *s;
4107 if (p->kind == SPELLING_MEMBER)
4108 *d++ = '.';
4109 for (s = p->u.s; (*d = *s++); d++)
4112 *d++ = '\0';
4113 return buffer;
4116 /* Issue an error message for a bad initializer component.
4117 MSGID identifies the message.
4118 The component name is taken from the spelling stack. */
4120 void
4121 error_init (const char *msgid)
4123 char *ofwhat;
4125 error ("%s", _(msgid));
4126 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4127 if (*ofwhat)
4128 error ("(near initialization for %qs)", ofwhat);
4131 /* Issue a pedantic warning for a bad initializer component.
4132 MSGID identifies the message.
4133 The component name is taken from the spelling stack. */
4135 void
4136 pedwarn_init (const char *msgid)
4138 char *ofwhat;
4140 pedwarn ("%s", _(msgid));
4141 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4142 if (*ofwhat)
4143 pedwarn ("(near initialization for %qs)", ofwhat);
4146 /* Issue a warning for a bad initializer component.
4147 MSGID identifies the message.
4148 The component name is taken from the spelling stack. */
4150 static void
4151 warning_init (const char *msgid)
4153 char *ofwhat;
4155 warning (0, "%s", _(msgid));
4156 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
4157 if (*ofwhat)
4158 warning (0, "(near initialization for %qs)", ofwhat);
4161 /* If TYPE is an array type and EXPR is a parenthesized string
4162 constant, warn if pedantic that EXPR is being used to initialize an
4163 object of type TYPE. */
4165 void
4166 maybe_warn_string_init (tree type, struct c_expr expr)
4168 if (pedantic
4169 && TREE_CODE (type) == ARRAY_TYPE
4170 && TREE_CODE (expr.value) == STRING_CST
4171 && expr.original_code != STRING_CST)
4172 pedwarn_init ("array initialized from parenthesized string constant");
4175 /* Digest the parser output INIT as an initializer for type TYPE.
4176 Return a C expression of type TYPE to represent the initial value.
4178 If INIT is a string constant, STRICT_STRING is true if it is
4179 unparenthesized or we should not warn here for it being parenthesized.
4180 For other types of INIT, STRICT_STRING is not used.
4182 REQUIRE_CONSTANT requests an error if non-constant initializers or
4183 elements are seen. */
4185 static tree
4186 digest_init (tree type, tree init, bool strict_string, int require_constant)
4188 enum tree_code code = TREE_CODE (type);
4189 tree inside_init = init;
4191 if (type == error_mark_node
4192 || init == error_mark_node
4193 || TREE_TYPE (init) == error_mark_node)
4194 return error_mark_node;
4196 STRIP_TYPE_NOPS (inside_init);
4198 inside_init = fold (inside_init);
4200 /* Initialization of an array of chars from a string constant
4201 optionally enclosed in braces. */
4203 if (code == ARRAY_TYPE && inside_init
4204 && TREE_CODE (inside_init) == STRING_CST)
4206 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4207 /* Note that an array could be both an array of character type
4208 and an array of wchar_t if wchar_t is signed char or unsigned
4209 char. */
4210 bool char_array = (typ1 == char_type_node
4211 || typ1 == signed_char_type_node
4212 || typ1 == unsigned_char_type_node);
4213 bool wchar_array = !!comptypes (typ1, wchar_type_node);
4214 if (char_array || wchar_array)
4216 struct c_expr expr;
4217 bool char_string;
4218 expr.value = inside_init;
4219 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
4220 maybe_warn_string_init (type, expr);
4222 char_string
4223 = (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
4224 == char_type_node);
4226 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4227 TYPE_MAIN_VARIANT (type)))
4228 return inside_init;
4230 if (!wchar_array && !char_string)
4232 error_init ("char-array initialized from wide string");
4233 return error_mark_node;
4235 if (char_string && !char_array)
4237 error_init ("wchar_t-array initialized from non-wide string");
4238 return error_mark_node;
4241 TREE_TYPE (inside_init) = type;
4242 if (TYPE_DOMAIN (type) != 0
4243 && TYPE_SIZE (type) != 0
4244 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4245 /* Subtract 1 (or sizeof (wchar_t))
4246 because it's ok to ignore the terminating null char
4247 that is counted in the length of the constant. */
4248 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4249 TREE_STRING_LENGTH (inside_init)
4250 - ((TYPE_PRECISION (typ1)
4251 != TYPE_PRECISION (char_type_node))
4252 ? (TYPE_PRECISION (wchar_type_node)
4253 / BITS_PER_UNIT)
4254 : 1)))
4255 pedwarn_init ("initializer-string for array of chars is too long");
4257 return inside_init;
4259 else if (INTEGRAL_TYPE_P (typ1))
4261 error_init ("array of inappropriate type initialized "
4262 "from string constant");
4263 return error_mark_node;
4267 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4268 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4269 below and handle as a constructor. */
4270 if (code == VECTOR_TYPE
4271 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
4272 && vector_types_convertible_p (TREE_TYPE (inside_init), type)
4273 && TREE_CONSTANT (inside_init))
4275 if (TREE_CODE (inside_init) == VECTOR_CST
4276 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4277 TYPE_MAIN_VARIANT (type)))
4278 return inside_init;
4280 if (TREE_CODE (inside_init) == CONSTRUCTOR)
4282 tree link;
4284 /* Iterate through elements and check if all constructor
4285 elements are *_CSTs. */
4286 for (link = CONSTRUCTOR_ELTS (inside_init);
4287 link;
4288 link = TREE_CHAIN (link))
4289 if (! CONSTANT_CLASS_P (TREE_VALUE (link)))
4290 break;
4292 if (link == NULL)
4293 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4297 /* Any type can be initialized
4298 from an expression of the same type, optionally with braces. */
4300 if (inside_init && TREE_TYPE (inside_init) != 0
4301 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4302 TYPE_MAIN_VARIANT (type))
4303 || (code == ARRAY_TYPE
4304 && comptypes (TREE_TYPE (inside_init), type))
4305 || (code == VECTOR_TYPE
4306 && comptypes (TREE_TYPE (inside_init), type))
4307 || (code == POINTER_TYPE
4308 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4309 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4310 TREE_TYPE (type)))
4311 || (code == POINTER_TYPE
4312 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
4313 && comptypes (TREE_TYPE (inside_init),
4314 TREE_TYPE (type)))))
4316 if (code == POINTER_TYPE)
4318 inside_init = default_function_array_conversion (inside_init);
4320 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
4322 error_init ("invalid use of non-lvalue array");
4323 return error_mark_node;
4327 if (code == VECTOR_TYPE)
4328 /* Although the types are compatible, we may require a
4329 conversion. */
4330 inside_init = convert (type, inside_init);
4332 if (require_constant && !flag_isoc99
4333 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4335 /* As an extension, allow initializing objects with static storage
4336 duration with compound literals (which are then treated just as
4337 the brace enclosed list they contain). */
4338 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4339 inside_init = DECL_INITIAL (decl);
4342 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4343 && TREE_CODE (inside_init) != CONSTRUCTOR)
4345 error_init ("array initialized from non-constant array expression");
4346 return error_mark_node;
4349 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4350 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4352 /* Compound expressions can only occur here if -pedantic or
4353 -pedantic-errors is specified. In the later case, we always want
4354 an error. In the former case, we simply want a warning. */
4355 if (require_constant && pedantic
4356 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4358 inside_init
4359 = valid_compound_expr_initializer (inside_init,
4360 TREE_TYPE (inside_init));
4361 if (inside_init == error_mark_node)
4362 error_init ("initializer element is not constant");
4363 else
4364 pedwarn_init ("initializer element is not constant");
4365 if (flag_pedantic_errors)
4366 inside_init = error_mark_node;
4368 else if (require_constant
4369 && !initializer_constant_valid_p (inside_init,
4370 TREE_TYPE (inside_init)))
4372 error_init ("initializer element is not constant");
4373 inside_init = error_mark_node;
4376 return inside_init;
4379 /* Handle scalar types, including conversions. */
4381 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4382 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
4383 || code == VECTOR_TYPE)
4385 /* Note that convert_for_assignment calls default_conversion
4386 for arrays and functions. We must not call it in the
4387 case where inside_init is a null pointer constant. */
4388 inside_init
4389 = convert_for_assignment (type, init, ic_init,
4390 NULL_TREE, NULL_TREE, 0);
4392 /* Check to see if we have already given an error message. */
4393 if (inside_init == error_mark_node)
4395 else if (require_constant && !TREE_CONSTANT (inside_init))
4397 error_init ("initializer element is not constant");
4398 inside_init = error_mark_node;
4400 else if (require_constant
4401 && !initializer_constant_valid_p (inside_init,
4402 TREE_TYPE (inside_init)))
4404 error_init ("initializer element is not computable at load time");
4405 inside_init = error_mark_node;
4408 return inside_init;
4411 /* Come here only for records and arrays. */
4413 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4415 error_init ("variable-sized object may not be initialized");
4416 return error_mark_node;
4419 error_init ("invalid initializer");
4420 return error_mark_node;
4423 /* Handle initializers that use braces. */
4425 /* Type of object we are accumulating a constructor for.
4426 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4427 static tree constructor_type;
4429 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4430 left to fill. */
4431 static tree constructor_fields;
4433 /* For an ARRAY_TYPE, this is the specified index
4434 at which to store the next element we get. */
4435 static tree constructor_index;
4437 /* For an ARRAY_TYPE, this is the maximum index. */
4438 static tree constructor_max_index;
4440 /* For a RECORD_TYPE, this is the first field not yet written out. */
4441 static tree constructor_unfilled_fields;
4443 /* For an ARRAY_TYPE, this is the index of the first element
4444 not yet written out. */
4445 static tree constructor_unfilled_index;
4447 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4448 This is so we can generate gaps between fields, when appropriate. */
4449 static tree constructor_bit_index;
4451 /* If we are saving up the elements rather than allocating them,
4452 this is the list of elements so far (in reverse order,
4453 most recent first). */
4454 static tree constructor_elements;
4456 /* 1 if constructor should be incrementally stored into a constructor chain,
4457 0 if all the elements should be kept in AVL tree. */
4458 static int constructor_incremental;
4460 /* 1 if so far this constructor's elements are all compile-time constants. */
4461 static int constructor_constant;
4463 /* 1 if so far this constructor's elements are all valid address constants. */
4464 static int constructor_simple;
4466 /* 1 if this constructor is erroneous so far. */
4467 static int constructor_erroneous;
4469 /* Structure for managing pending initializer elements, organized as an
4470 AVL tree. */
4472 struct init_node
4474 struct init_node *left, *right;
4475 struct init_node *parent;
4476 int balance;
4477 tree purpose;
4478 tree value;
4481 /* Tree of pending elements at this constructor level.
4482 These are elements encountered out of order
4483 which belong at places we haven't reached yet in actually
4484 writing the output.
4485 Will never hold tree nodes across GC runs. */
4486 static struct init_node *constructor_pending_elts;
4488 /* The SPELLING_DEPTH of this constructor. */
4489 static int constructor_depth;
4491 /* DECL node for which an initializer is being read.
4492 0 means we are reading a constructor expression
4493 such as (struct foo) {...}. */
4494 static tree constructor_decl;
4496 /* Nonzero if this is an initializer for a top-level decl. */
4497 static int constructor_top_level;
4499 /* Nonzero if there were any member designators in this initializer. */
4500 static int constructor_designated;
4502 /* Nesting depth of designator list. */
4503 static int designator_depth;
4505 /* Nonzero if there were diagnosed errors in this designator list. */
4506 static int designator_errorneous;
4509 /* This stack has a level for each implicit or explicit level of
4510 structuring in the initializer, including the outermost one. It
4511 saves the values of most of the variables above. */
4513 struct constructor_range_stack;
4515 struct constructor_stack
4517 struct constructor_stack *next;
4518 tree type;
4519 tree fields;
4520 tree index;
4521 tree max_index;
4522 tree unfilled_index;
4523 tree unfilled_fields;
4524 tree bit_index;
4525 tree elements;
4526 struct init_node *pending_elts;
4527 int offset;
4528 int depth;
4529 /* If value nonzero, this value should replace the entire
4530 constructor at this level. */
4531 struct c_expr replacement_value;
4532 struct constructor_range_stack *range_stack;
4533 char constant;
4534 char simple;
4535 char implicit;
4536 char erroneous;
4537 char outer;
4538 char incremental;
4539 char designated;
4542 static struct constructor_stack *constructor_stack;
4544 /* This stack represents designators from some range designator up to
4545 the last designator in the list. */
4547 struct constructor_range_stack
4549 struct constructor_range_stack *next, *prev;
4550 struct constructor_stack *stack;
4551 tree range_start;
4552 tree index;
4553 tree range_end;
4554 tree fields;
4557 static struct constructor_range_stack *constructor_range_stack;
4559 /* This stack records separate initializers that are nested.
4560 Nested initializers can't happen in ANSI C, but GNU C allows them
4561 in cases like { ... (struct foo) { ... } ... }. */
4563 struct initializer_stack
4565 struct initializer_stack *next;
4566 tree decl;
4567 struct constructor_stack *constructor_stack;
4568 struct constructor_range_stack *constructor_range_stack;
4569 tree elements;
4570 struct spelling *spelling;
4571 struct spelling *spelling_base;
4572 int spelling_size;
4573 char top_level;
4574 char require_constant_value;
4575 char require_constant_elements;
4578 static struct initializer_stack *initializer_stack;
4580 /* Prepare to parse and output the initializer for variable DECL. */
4582 void
4583 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
4585 const char *locus;
4586 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4588 p->decl = constructor_decl;
4589 p->require_constant_value = require_constant_value;
4590 p->require_constant_elements = require_constant_elements;
4591 p->constructor_stack = constructor_stack;
4592 p->constructor_range_stack = constructor_range_stack;
4593 p->elements = constructor_elements;
4594 p->spelling = spelling;
4595 p->spelling_base = spelling_base;
4596 p->spelling_size = spelling_size;
4597 p->top_level = constructor_top_level;
4598 p->next = initializer_stack;
4599 initializer_stack = p;
4601 constructor_decl = decl;
4602 constructor_designated = 0;
4603 constructor_top_level = top_level;
4605 if (decl != 0 && decl != error_mark_node)
4607 require_constant_value = TREE_STATIC (decl);
4608 require_constant_elements
4609 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4610 /* For a scalar, you can always use any value to initialize,
4611 even within braces. */
4612 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4613 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4614 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4615 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4616 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4618 else
4620 require_constant_value = 0;
4621 require_constant_elements = 0;
4622 locus = "(anonymous)";
4625 constructor_stack = 0;
4626 constructor_range_stack = 0;
4628 missing_braces_mentioned = 0;
4630 spelling_base = 0;
4631 spelling_size = 0;
4632 RESTORE_SPELLING_DEPTH (0);
4634 if (locus)
4635 push_string (locus);
4638 void
4639 finish_init (void)
4641 struct initializer_stack *p = initializer_stack;
4643 /* Free the whole constructor stack of this initializer. */
4644 while (constructor_stack)
4646 struct constructor_stack *q = constructor_stack;
4647 constructor_stack = q->next;
4648 free (q);
4651 gcc_assert (!constructor_range_stack);
4653 /* Pop back to the data of the outer initializer (if any). */
4654 free (spelling_base);
4656 constructor_decl = p->decl;
4657 require_constant_value = p->require_constant_value;
4658 require_constant_elements = p->require_constant_elements;
4659 constructor_stack = p->constructor_stack;
4660 constructor_range_stack = p->constructor_range_stack;
4661 constructor_elements = p->elements;
4662 spelling = p->spelling;
4663 spelling_base = p->spelling_base;
4664 spelling_size = p->spelling_size;
4665 constructor_top_level = p->top_level;
4666 initializer_stack = p->next;
4667 free (p);
4670 /* Call here when we see the initializer is surrounded by braces.
4671 This is instead of a call to push_init_level;
4672 it is matched by a call to pop_init_level.
4674 TYPE is the type to initialize, for a constructor expression.
4675 For an initializer for a decl, TYPE is zero. */
4677 void
4678 really_start_incremental_init (tree type)
4680 struct constructor_stack *p = XNEW (struct constructor_stack);
4682 if (type == 0)
4683 type = TREE_TYPE (constructor_decl);
4685 if (targetm.vector_opaque_p (type))
4686 error ("opaque vector types cannot be initialized");
4688 p->type = constructor_type;
4689 p->fields = constructor_fields;
4690 p->index = constructor_index;
4691 p->max_index = constructor_max_index;
4692 p->unfilled_index = constructor_unfilled_index;
4693 p->unfilled_fields = constructor_unfilled_fields;
4694 p->bit_index = constructor_bit_index;
4695 p->elements = constructor_elements;
4696 p->constant = constructor_constant;
4697 p->simple = constructor_simple;
4698 p->erroneous = constructor_erroneous;
4699 p->pending_elts = constructor_pending_elts;
4700 p->depth = constructor_depth;
4701 p->replacement_value.value = 0;
4702 p->replacement_value.original_code = ERROR_MARK;
4703 p->implicit = 0;
4704 p->range_stack = 0;
4705 p->outer = 0;
4706 p->incremental = constructor_incremental;
4707 p->designated = constructor_designated;
4708 p->next = 0;
4709 constructor_stack = p;
4711 constructor_constant = 1;
4712 constructor_simple = 1;
4713 constructor_depth = SPELLING_DEPTH ();
4714 constructor_elements = 0;
4715 constructor_pending_elts = 0;
4716 constructor_type = type;
4717 constructor_incremental = 1;
4718 constructor_designated = 0;
4719 designator_depth = 0;
4720 designator_errorneous = 0;
4722 if (TREE_CODE (constructor_type) == RECORD_TYPE
4723 || TREE_CODE (constructor_type) == UNION_TYPE)
4725 constructor_fields = TYPE_FIELDS (constructor_type);
4726 /* Skip any nameless bit fields at the beginning. */
4727 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4728 && DECL_NAME (constructor_fields) == 0)
4729 constructor_fields = TREE_CHAIN (constructor_fields);
4731 constructor_unfilled_fields = constructor_fields;
4732 constructor_bit_index = bitsize_zero_node;
4734 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4736 if (TYPE_DOMAIN (constructor_type))
4738 constructor_max_index
4739 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4741 /* Detect non-empty initializations of zero-length arrays. */
4742 if (constructor_max_index == NULL_TREE
4743 && TYPE_SIZE (constructor_type))
4744 constructor_max_index = build_int_cst (NULL_TREE, -1);
4746 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4747 to initialize VLAs will cause a proper error; avoid tree
4748 checking errors as well by setting a safe value. */
4749 if (constructor_max_index
4750 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4751 constructor_max_index = build_int_cst (NULL_TREE, -1);
4753 constructor_index
4754 = convert (bitsizetype,
4755 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4757 else
4759 constructor_index = bitsize_zero_node;
4760 constructor_max_index = NULL_TREE;
4763 constructor_unfilled_index = constructor_index;
4765 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4767 /* Vectors are like simple fixed-size arrays. */
4768 constructor_max_index =
4769 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4770 constructor_index = convert (bitsizetype, bitsize_zero_node);
4771 constructor_unfilled_index = constructor_index;
4773 else
4775 /* Handle the case of int x = {5}; */
4776 constructor_fields = constructor_type;
4777 constructor_unfilled_fields = constructor_type;
4781 /* Push down into a subobject, for initialization.
4782 If this is for an explicit set of braces, IMPLICIT is 0.
4783 If it is because the next element belongs at a lower level,
4784 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4786 void
4787 push_init_level (int implicit)
4789 struct constructor_stack *p;
4790 tree value = NULL_TREE;
4792 /* If we've exhausted any levels that didn't have braces,
4793 pop them now. */
4794 while (constructor_stack->implicit)
4796 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4797 || TREE_CODE (constructor_type) == UNION_TYPE)
4798 && constructor_fields == 0)
4799 process_init_element (pop_init_level (1));
4800 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4801 && constructor_max_index
4802 && tree_int_cst_lt (constructor_max_index, constructor_index))
4803 process_init_element (pop_init_level (1));
4804 else
4805 break;
4808 /* Unless this is an explicit brace, we need to preserve previous
4809 content if any. */
4810 if (implicit)
4812 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4813 || TREE_CODE (constructor_type) == UNION_TYPE)
4814 && constructor_fields)
4815 value = find_init_member (constructor_fields);
4816 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4817 value = find_init_member (constructor_index);
4820 p = XNEW (struct constructor_stack);
4821 p->type = constructor_type;
4822 p->fields = constructor_fields;
4823 p->index = constructor_index;
4824 p->max_index = constructor_max_index;
4825 p->unfilled_index = constructor_unfilled_index;
4826 p->unfilled_fields = constructor_unfilled_fields;
4827 p->bit_index = constructor_bit_index;
4828 p->elements = constructor_elements;
4829 p->constant = constructor_constant;
4830 p->simple = constructor_simple;
4831 p->erroneous = constructor_erroneous;
4832 p->pending_elts = constructor_pending_elts;
4833 p->depth = constructor_depth;
4834 p->replacement_value.value = 0;
4835 p->replacement_value.original_code = ERROR_MARK;
4836 p->implicit = implicit;
4837 p->outer = 0;
4838 p->incremental = constructor_incremental;
4839 p->designated = constructor_designated;
4840 p->next = constructor_stack;
4841 p->range_stack = 0;
4842 constructor_stack = p;
4844 constructor_constant = 1;
4845 constructor_simple = 1;
4846 constructor_depth = SPELLING_DEPTH ();
4847 constructor_elements = 0;
4848 constructor_incremental = 1;
4849 constructor_designated = 0;
4850 constructor_pending_elts = 0;
4851 if (!implicit)
4853 p->range_stack = constructor_range_stack;
4854 constructor_range_stack = 0;
4855 designator_depth = 0;
4856 designator_errorneous = 0;
4859 /* Don't die if an entire brace-pair level is superfluous
4860 in the containing level. */
4861 if (constructor_type == 0)
4863 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4864 || TREE_CODE (constructor_type) == UNION_TYPE)
4866 /* Don't die if there are extra init elts at the end. */
4867 if (constructor_fields == 0)
4868 constructor_type = 0;
4869 else
4871 constructor_type = TREE_TYPE (constructor_fields);
4872 push_member_name (constructor_fields);
4873 constructor_depth++;
4876 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4878 constructor_type = TREE_TYPE (constructor_type);
4879 push_array_bounds (tree_low_cst (constructor_index, 0));
4880 constructor_depth++;
4883 if (constructor_type == 0)
4885 error_init ("extra brace group at end of initializer");
4886 constructor_fields = 0;
4887 constructor_unfilled_fields = 0;
4888 return;
4891 if (value && TREE_CODE (value) == CONSTRUCTOR)
4893 constructor_constant = TREE_CONSTANT (value);
4894 constructor_simple = TREE_STATIC (value);
4895 constructor_elements = CONSTRUCTOR_ELTS (value);
4896 if (constructor_elements
4897 && (TREE_CODE (constructor_type) == RECORD_TYPE
4898 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4899 set_nonincremental_init ();
4902 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4904 missing_braces_mentioned = 1;
4905 warning_init ("missing braces around initializer");
4908 if (TREE_CODE (constructor_type) == RECORD_TYPE
4909 || TREE_CODE (constructor_type) == UNION_TYPE)
4911 constructor_fields = TYPE_FIELDS (constructor_type);
4912 /* Skip any nameless bit fields at the beginning. */
4913 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4914 && DECL_NAME (constructor_fields) == 0)
4915 constructor_fields = TREE_CHAIN (constructor_fields);
4917 constructor_unfilled_fields = constructor_fields;
4918 constructor_bit_index = bitsize_zero_node;
4920 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4922 /* Vectors are like simple fixed-size arrays. */
4923 constructor_max_index =
4924 build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4925 constructor_index = convert (bitsizetype, integer_zero_node);
4926 constructor_unfilled_index = constructor_index;
4928 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4930 if (TYPE_DOMAIN (constructor_type))
4932 constructor_max_index
4933 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4935 /* Detect non-empty initializations of zero-length arrays. */
4936 if (constructor_max_index == NULL_TREE
4937 && TYPE_SIZE (constructor_type))
4938 constructor_max_index = build_int_cst (NULL_TREE, -1);
4940 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4941 to initialize VLAs will cause a proper error; avoid tree
4942 checking errors as well by setting a safe value. */
4943 if (constructor_max_index
4944 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4945 constructor_max_index = build_int_cst (NULL_TREE, -1);
4947 constructor_index
4948 = convert (bitsizetype,
4949 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4951 else
4952 constructor_index = bitsize_zero_node;
4954 constructor_unfilled_index = constructor_index;
4955 if (value && TREE_CODE (value) == STRING_CST)
4957 /* We need to split the char/wchar array into individual
4958 characters, so that we don't have to special case it
4959 everywhere. */
4960 set_nonincremental_init_from_string (value);
4963 else
4965 if (constructor_type != error_mark_node)
4966 warning_init ("braces around scalar initializer");
4967 constructor_fields = constructor_type;
4968 constructor_unfilled_fields = constructor_type;
4972 /* At the end of an implicit or explicit brace level,
4973 finish up that level of constructor. If a single expression
4974 with redundant braces initialized that level, return the
4975 c_expr structure for that expression. Otherwise, the original_code
4976 element is set to ERROR_MARK.
4977 If we were outputting the elements as they are read, return 0 as the value
4978 from inner levels (process_init_element ignores that),
4979 but return error_mark_node as the value from the outermost level
4980 (that's what we want to put in DECL_INITIAL).
4981 Otherwise, return a CONSTRUCTOR expression as the value. */
4983 struct c_expr
4984 pop_init_level (int implicit)
4986 struct constructor_stack *p;
4987 struct c_expr ret;
4988 ret.value = 0;
4989 ret.original_code = ERROR_MARK;
4991 if (implicit == 0)
4993 /* When we come to an explicit close brace,
4994 pop any inner levels that didn't have explicit braces. */
4995 while (constructor_stack->implicit)
4996 process_init_element (pop_init_level (1));
4998 gcc_assert (!constructor_range_stack);
5001 /* Now output all pending elements. */
5002 constructor_incremental = 1;
5003 output_pending_init_elements (1);
5005 p = constructor_stack;
5007 /* Error for initializing a flexible array member, or a zero-length
5008 array member in an inappropriate context. */
5009 if (constructor_type && constructor_fields
5010 && TREE_CODE (constructor_type) == ARRAY_TYPE
5011 && TYPE_DOMAIN (constructor_type)
5012 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5014 /* Silently discard empty initializations. The parser will
5015 already have pedwarned for empty brackets. */
5016 if (integer_zerop (constructor_unfilled_index))
5017 constructor_type = NULL_TREE;
5018 else
5020 gcc_assert (!TYPE_SIZE (constructor_type));
5022 if (constructor_depth > 2)
5023 error_init ("initialization of flexible array member in a nested context");
5024 else if (pedantic)
5025 pedwarn_init ("initialization of a flexible array member");
5027 /* We have already issued an error message for the existence
5028 of a flexible array member not at the end of the structure.
5029 Discard the initializer so that we do not die later. */
5030 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5031 constructor_type = NULL_TREE;
5035 /* Warn when some struct elements are implicitly initialized to zero. */
5036 if (warn_missing_field_initializers
5037 && constructor_type
5038 && TREE_CODE (constructor_type) == RECORD_TYPE
5039 && constructor_unfilled_fields)
5041 /* Do not warn for flexible array members or zero-length arrays. */
5042 while (constructor_unfilled_fields
5043 && (!DECL_SIZE (constructor_unfilled_fields)
5044 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5045 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5047 /* Do not warn if this level of the initializer uses member
5048 designators; it is likely to be deliberate. */
5049 if (constructor_unfilled_fields && !constructor_designated)
5051 push_member_name (constructor_unfilled_fields);
5052 warning_init ("missing initializer");
5053 RESTORE_SPELLING_DEPTH (constructor_depth);
5057 /* Pad out the end of the structure. */
5058 if (p->replacement_value.value)
5059 /* If this closes a superfluous brace pair,
5060 just pass out the element between them. */
5061 ret = p->replacement_value;
5062 else if (constructor_type == 0)
5064 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5065 && TREE_CODE (constructor_type) != UNION_TYPE
5066 && TREE_CODE (constructor_type) != ARRAY_TYPE
5067 && TREE_CODE (constructor_type) != VECTOR_TYPE)
5069 /* A nonincremental scalar initializer--just return
5070 the element, after verifying there is just one. */
5071 if (constructor_elements == 0)
5073 if (!constructor_erroneous)
5074 error_init ("empty scalar initializer");
5075 ret.value = error_mark_node;
5077 else if (TREE_CHAIN (constructor_elements) != 0)
5079 error_init ("extra elements in scalar initializer");
5080 ret.value = TREE_VALUE (constructor_elements);
5082 else
5083 ret.value = TREE_VALUE (constructor_elements);
5085 else
5087 if (constructor_erroneous)
5088 ret.value = error_mark_node;
5089 else
5091 ret.value = build_constructor (constructor_type,
5092 nreverse (constructor_elements));
5093 if (constructor_constant)
5094 TREE_CONSTANT (ret.value) = TREE_INVARIANT (ret.value) = 1;
5095 if (constructor_constant && constructor_simple)
5096 TREE_STATIC (ret.value) = 1;
5100 constructor_type = p->type;
5101 constructor_fields = p->fields;
5102 constructor_index = p->index;
5103 constructor_max_index = p->max_index;
5104 constructor_unfilled_index = p->unfilled_index;
5105 constructor_unfilled_fields = p->unfilled_fields;
5106 constructor_bit_index = p->bit_index;
5107 constructor_elements = p->elements;
5108 constructor_constant = p->constant;
5109 constructor_simple = p->simple;
5110 constructor_erroneous = p->erroneous;
5111 constructor_incremental = p->incremental;
5112 constructor_designated = p->designated;
5113 constructor_pending_elts = p->pending_elts;
5114 constructor_depth = p->depth;
5115 if (!p->implicit)
5116 constructor_range_stack = p->range_stack;
5117 RESTORE_SPELLING_DEPTH (constructor_depth);
5119 constructor_stack = p->next;
5120 free (p);
5122 if (ret.value == 0)
5124 if (constructor_stack == 0)
5126 ret.value = error_mark_node;
5127 return ret;
5129 return ret;
5131 return ret;
5134 /* Common handling for both array range and field name designators.
5135 ARRAY argument is nonzero for array ranges. Returns zero for success. */
5137 static int
5138 set_designator (int array)
5140 tree subtype;
5141 enum tree_code subcode;
5143 /* Don't die if an entire brace-pair level is superfluous
5144 in the containing level. */
5145 if (constructor_type == 0)
5146 return 1;
5148 /* If there were errors in this designator list already, bail out
5149 silently. */
5150 if (designator_errorneous)
5151 return 1;
5153 if (!designator_depth)
5155 gcc_assert (!constructor_range_stack);
5157 /* Designator list starts at the level of closest explicit
5158 braces. */
5159 while (constructor_stack->implicit)
5160 process_init_element (pop_init_level (1));
5161 constructor_designated = 1;
5162 return 0;
5165 switch (TREE_CODE (constructor_type))
5167 case RECORD_TYPE:
5168 case UNION_TYPE:
5169 subtype = TREE_TYPE (constructor_fields);
5170 if (subtype != error_mark_node)
5171 subtype = TYPE_MAIN_VARIANT (subtype);
5172 break;
5173 case ARRAY_TYPE:
5174 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5175 break;
5176 default:
5177 gcc_unreachable ();
5180 subcode = TREE_CODE (subtype);
5181 if (array && subcode != ARRAY_TYPE)
5183 error_init ("array index in non-array initializer");
5184 return 1;
5186 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5188 error_init ("field name not in record or union initializer");
5189 return 1;
5192 constructor_designated = 1;
5193 push_init_level (2);
5194 return 0;
5197 /* If there are range designators in designator list, push a new designator
5198 to constructor_range_stack. RANGE_END is end of such stack range or
5199 NULL_TREE if there is no range designator at this level. */
5201 static void
5202 push_range_stack (tree range_end)
5204 struct constructor_range_stack *p;
5206 p = GGC_NEW (struct constructor_range_stack);
5207 p->prev = constructor_range_stack;
5208 p->next = 0;
5209 p->fields = constructor_fields;
5210 p->range_start = constructor_index;
5211 p->index = constructor_index;
5212 p->stack = constructor_stack;
5213 p->range_end = range_end;
5214 if (constructor_range_stack)
5215 constructor_range_stack->next = p;
5216 constructor_range_stack = p;
5219 /* Within an array initializer, specify the next index to be initialized.
5220 FIRST is that index. If LAST is nonzero, then initialize a range
5221 of indices, running from FIRST through LAST. */
5223 void
5224 set_init_index (tree first, tree last)
5226 if (set_designator (1))
5227 return;
5229 designator_errorneous = 1;
5231 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
5232 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
5234 error_init ("array index in initializer not of integer type");
5235 return;
5238 if (TREE_CODE (first) != INTEGER_CST)
5239 error_init ("nonconstant array index in initializer");
5240 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
5241 error_init ("nonconstant array index in initializer");
5242 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5243 error_init ("array index in non-array initializer");
5244 else if (tree_int_cst_sgn (first) == -1)
5245 error_init ("array index in initializer exceeds array bounds");
5246 else if (constructor_max_index
5247 && tree_int_cst_lt (constructor_max_index, first))
5248 error_init ("array index in initializer exceeds array bounds");
5249 else
5251 constructor_index = convert (bitsizetype, first);
5253 if (last)
5255 if (tree_int_cst_equal (first, last))
5256 last = 0;
5257 else if (tree_int_cst_lt (last, first))
5259 error_init ("empty index range in initializer");
5260 last = 0;
5262 else
5264 last = convert (bitsizetype, last);
5265 if (constructor_max_index != 0
5266 && tree_int_cst_lt (constructor_max_index, last))
5268 error_init ("array index range in initializer exceeds array bounds");
5269 last = 0;
5274 designator_depth++;
5275 designator_errorneous = 0;
5276 if (constructor_range_stack || last)
5277 push_range_stack (last);
5281 /* Within a struct initializer, specify the next field to be initialized. */
5283 void
5284 set_init_label (tree fieldname)
5286 tree tail;
5288 if (set_designator (0))
5289 return;
5291 designator_errorneous = 1;
5293 if (TREE_CODE (constructor_type) != RECORD_TYPE
5294 && TREE_CODE (constructor_type) != UNION_TYPE)
5296 error_init ("field name not in record or union initializer");
5297 return;
5300 for (tail = TYPE_FIELDS (constructor_type); tail;
5301 tail = TREE_CHAIN (tail))
5303 if (DECL_NAME (tail) == fieldname)
5304 break;
5307 if (tail == 0)
5308 error ("unknown field %qE specified in initializer", fieldname);
5309 else
5311 constructor_fields = tail;
5312 designator_depth++;
5313 designator_errorneous = 0;
5314 if (constructor_range_stack)
5315 push_range_stack (NULL_TREE);
5319 /* Add a new initializer to the tree of pending initializers. PURPOSE
5320 identifies the initializer, either array index or field in a structure.
5321 VALUE is the value of that index or field. */
5323 static void
5324 add_pending_init (tree purpose, tree value)
5326 struct init_node *p, **q, *r;
5328 q = &constructor_pending_elts;
5329 p = 0;
5331 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5333 while (*q != 0)
5335 p = *q;
5336 if (tree_int_cst_lt (purpose, p->purpose))
5337 q = &p->left;
5338 else if (tree_int_cst_lt (p->purpose, purpose))
5339 q = &p->right;
5340 else
5342 if (TREE_SIDE_EFFECTS (p->value))
5343 warning_init ("initialized field with side-effects overwritten");
5344 p->value = value;
5345 return;
5349 else
5351 tree bitpos;
5353 bitpos = bit_position (purpose);
5354 while (*q != NULL)
5356 p = *q;
5357 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5358 q = &p->left;
5359 else if (p->purpose != purpose)
5360 q = &p->right;
5361 else
5363 if (TREE_SIDE_EFFECTS (p->value))
5364 warning_init ("initialized field with side-effects overwritten");
5365 p->value = value;
5366 return;
5371 r = GGC_NEW (struct init_node);
5372 r->purpose = purpose;
5373 r->value = value;
5375 *q = r;
5376 r->parent = p;
5377 r->left = 0;
5378 r->right = 0;
5379 r->balance = 0;
5381 while (p)
5383 struct init_node *s;
5385 if (r == p->left)
5387 if (p->balance == 0)
5388 p->balance = -1;
5389 else if (p->balance < 0)
5391 if (r->balance < 0)
5393 /* L rotation. */
5394 p->left = r->right;
5395 if (p->left)
5396 p->left->parent = p;
5397 r->right = p;
5399 p->balance = 0;
5400 r->balance = 0;
5402 s = p->parent;
5403 p->parent = r;
5404 r->parent = s;
5405 if (s)
5407 if (s->left == p)
5408 s->left = r;
5409 else
5410 s->right = r;
5412 else
5413 constructor_pending_elts = r;
5415 else
5417 /* LR rotation. */
5418 struct init_node *t = r->right;
5420 r->right = t->left;
5421 if (r->right)
5422 r->right->parent = r;
5423 t->left = r;
5425 p->left = t->right;
5426 if (p->left)
5427 p->left->parent = p;
5428 t->right = p;
5430 p->balance = t->balance < 0;
5431 r->balance = -(t->balance > 0);
5432 t->balance = 0;
5434 s = p->parent;
5435 p->parent = t;
5436 r->parent = t;
5437 t->parent = s;
5438 if (s)
5440 if (s->left == p)
5441 s->left = t;
5442 else
5443 s->right = t;
5445 else
5446 constructor_pending_elts = t;
5448 break;
5450 else
5452 /* p->balance == +1; growth of left side balances the node. */
5453 p->balance = 0;
5454 break;
5457 else /* r == p->right */
5459 if (p->balance == 0)
5460 /* Growth propagation from right side. */
5461 p->balance++;
5462 else if (p->balance > 0)
5464 if (r->balance > 0)
5466 /* R rotation. */
5467 p->right = r->left;
5468 if (p->right)
5469 p->right->parent = p;
5470 r->left = p;
5472 p->balance = 0;
5473 r->balance = 0;
5475 s = p->parent;
5476 p->parent = r;
5477 r->parent = s;
5478 if (s)
5480 if (s->left == p)
5481 s->left = r;
5482 else
5483 s->right = r;
5485 else
5486 constructor_pending_elts = r;
5488 else /* r->balance == -1 */
5490 /* RL rotation */
5491 struct init_node *t = r->left;
5493 r->left = t->right;
5494 if (r->left)
5495 r->left->parent = r;
5496 t->right = r;
5498 p->right = t->left;
5499 if (p->right)
5500 p->right->parent = p;
5501 t->left = p;
5503 r->balance = (t->balance < 0);
5504 p->balance = -(t->balance > 0);
5505 t->balance = 0;
5507 s = p->parent;
5508 p->parent = t;
5509 r->parent = t;
5510 t->parent = s;
5511 if (s)
5513 if (s->left == p)
5514 s->left = t;
5515 else
5516 s->right = t;
5518 else
5519 constructor_pending_elts = t;
5521 break;
5523 else
5525 /* p->balance == -1; growth of right side balances the node. */
5526 p->balance = 0;
5527 break;
5531 r = p;
5532 p = p->parent;
5536 /* Build AVL tree from a sorted chain. */
5538 static void
5539 set_nonincremental_init (void)
5541 tree chain;
5543 if (TREE_CODE (constructor_type) != RECORD_TYPE
5544 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5545 return;
5547 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5548 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5549 constructor_elements = 0;
5550 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5552 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5553 /* Skip any nameless bit fields at the beginning. */
5554 while (constructor_unfilled_fields != 0
5555 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5556 && DECL_NAME (constructor_unfilled_fields) == 0)
5557 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5560 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5562 if (TYPE_DOMAIN (constructor_type))
5563 constructor_unfilled_index
5564 = convert (bitsizetype,
5565 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5566 else
5567 constructor_unfilled_index = bitsize_zero_node;
5569 constructor_incremental = 0;
5572 /* Build AVL tree from a string constant. */
5574 static void
5575 set_nonincremental_init_from_string (tree str)
5577 tree value, purpose, type;
5578 HOST_WIDE_INT val[2];
5579 const char *p, *end;
5580 int byte, wchar_bytes, charwidth, bitpos;
5582 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
5584 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5585 == TYPE_PRECISION (char_type_node))
5586 wchar_bytes = 1;
5587 else
5589 gcc_assert (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5590 == TYPE_PRECISION (wchar_type_node));
5591 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5593 charwidth = TYPE_PRECISION (char_type_node);
5594 type = TREE_TYPE (constructor_type);
5595 p = TREE_STRING_POINTER (str);
5596 end = p + TREE_STRING_LENGTH (str);
5598 for (purpose = bitsize_zero_node;
5599 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5600 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5602 if (wchar_bytes == 1)
5604 val[1] = (unsigned char) *p++;
5605 val[0] = 0;
5607 else
5609 val[0] = 0;
5610 val[1] = 0;
5611 for (byte = 0; byte < wchar_bytes; byte++)
5613 if (BYTES_BIG_ENDIAN)
5614 bitpos = (wchar_bytes - byte - 1) * charwidth;
5615 else
5616 bitpos = byte * charwidth;
5617 val[bitpos < HOST_BITS_PER_WIDE_INT]
5618 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5619 << (bitpos % HOST_BITS_PER_WIDE_INT);
5623 if (!TYPE_UNSIGNED (type))
5625 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5626 if (bitpos < HOST_BITS_PER_WIDE_INT)
5628 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5630 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5631 val[0] = -1;
5634 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5636 if (val[1] < 0)
5637 val[0] = -1;
5639 else if (val[0] & (((HOST_WIDE_INT) 1)
5640 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5641 val[0] |= ((HOST_WIDE_INT) -1)
5642 << (bitpos - HOST_BITS_PER_WIDE_INT);
5645 value = build_int_cst_wide (type, val[1], val[0]);
5646 add_pending_init (purpose, value);
5649 constructor_incremental = 0;
5652 /* Return value of FIELD in pending initializer or zero if the field was
5653 not initialized yet. */
5655 static tree
5656 find_init_member (tree field)
5658 struct init_node *p;
5660 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5662 if (constructor_incremental
5663 && tree_int_cst_lt (field, constructor_unfilled_index))
5664 set_nonincremental_init ();
5666 p = constructor_pending_elts;
5667 while (p)
5669 if (tree_int_cst_lt (field, p->purpose))
5670 p = p->left;
5671 else if (tree_int_cst_lt (p->purpose, field))
5672 p = p->right;
5673 else
5674 return p->value;
5677 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5679 tree bitpos = bit_position (field);
5681 if (constructor_incremental
5682 && (!constructor_unfilled_fields
5683 || tree_int_cst_lt (bitpos,
5684 bit_position (constructor_unfilled_fields))))
5685 set_nonincremental_init ();
5687 p = constructor_pending_elts;
5688 while (p)
5690 if (field == p->purpose)
5691 return p->value;
5692 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5693 p = p->left;
5694 else
5695 p = p->right;
5698 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5700 if (constructor_elements
5701 && TREE_PURPOSE (constructor_elements) == field)
5702 return TREE_VALUE (constructor_elements);
5704 return 0;
5707 /* "Output" the next constructor element.
5708 At top level, really output it to assembler code now.
5709 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5710 TYPE is the data type that the containing data type wants here.
5711 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5712 If VALUE is a string constant, STRICT_STRING is true if it is
5713 unparenthesized or we should not warn here for it being parenthesized.
5714 For other types of VALUE, STRICT_STRING is not used.
5716 PENDING if non-nil means output pending elements that belong
5717 right after this element. (PENDING is normally 1;
5718 it is 0 while outputting pending elements, to avoid recursion.) */
5720 static void
5721 output_init_element (tree value, bool strict_string, tree type, tree field,
5722 int pending)
5724 if (type == error_mark_node || value == error_mark_node)
5726 constructor_erroneous = 1;
5727 return;
5729 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5730 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5731 && !(TREE_CODE (value) == STRING_CST
5732 && TREE_CODE (type) == ARRAY_TYPE
5733 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
5734 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5735 TYPE_MAIN_VARIANT (type))))
5736 value = default_conversion (value);
5738 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5739 && require_constant_value && !flag_isoc99 && pending)
5741 /* As an extension, allow initializing objects with static storage
5742 duration with compound literals (which are then treated just as
5743 the brace enclosed list they contain). */
5744 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5745 value = DECL_INITIAL (decl);
5748 if (value == error_mark_node)
5749 constructor_erroneous = 1;
5750 else if (!TREE_CONSTANT (value))
5751 constructor_constant = 0;
5752 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
5753 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5754 || TREE_CODE (constructor_type) == UNION_TYPE)
5755 && DECL_C_BIT_FIELD (field)
5756 && TREE_CODE (value) != INTEGER_CST))
5757 constructor_simple = 0;
5759 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
5761 if (require_constant_value)
5763 error_init ("initializer element is not constant");
5764 value = error_mark_node;
5766 else if (require_constant_elements)
5767 pedwarn ("initializer element is not computable at load time");
5770 /* If this field is empty (and not at the end of structure),
5771 don't do anything other than checking the initializer. */
5772 if (field
5773 && (TREE_TYPE (field) == error_mark_node
5774 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5775 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5776 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5777 || TREE_CHAIN (field)))))
5778 return;
5780 value = digest_init (type, value, strict_string, require_constant_value);
5781 if (value == error_mark_node)
5783 constructor_erroneous = 1;
5784 return;
5787 /* If this element doesn't come next in sequence,
5788 put it on constructor_pending_elts. */
5789 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5790 && (!constructor_incremental
5791 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5793 if (constructor_incremental
5794 && tree_int_cst_lt (field, constructor_unfilled_index))
5795 set_nonincremental_init ();
5797 add_pending_init (field, value);
5798 return;
5800 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5801 && (!constructor_incremental
5802 || field != constructor_unfilled_fields))
5804 /* We do this for records but not for unions. In a union,
5805 no matter which field is specified, it can be initialized
5806 right away since it starts at the beginning of the union. */
5807 if (constructor_incremental)
5809 if (!constructor_unfilled_fields)
5810 set_nonincremental_init ();
5811 else
5813 tree bitpos, unfillpos;
5815 bitpos = bit_position (field);
5816 unfillpos = bit_position (constructor_unfilled_fields);
5818 if (tree_int_cst_lt (bitpos, unfillpos))
5819 set_nonincremental_init ();
5823 add_pending_init (field, value);
5824 return;
5826 else if (TREE_CODE (constructor_type) == UNION_TYPE
5827 && constructor_elements)
5829 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5830 warning_init ("initialized field with side-effects overwritten");
5832 /* We can have just one union field set. */
5833 constructor_elements = 0;
5836 /* Otherwise, output this element either to
5837 constructor_elements or to the assembler file. */
5839 if (field && TREE_CODE (field) == INTEGER_CST)
5840 field = copy_node (field);
5841 constructor_elements
5842 = tree_cons (field, value, constructor_elements);
5844 /* Advance the variable that indicates sequential elements output. */
5845 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5846 constructor_unfilled_index
5847 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5848 bitsize_one_node);
5849 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5851 constructor_unfilled_fields
5852 = TREE_CHAIN (constructor_unfilled_fields);
5854 /* Skip any nameless bit fields. */
5855 while (constructor_unfilled_fields != 0
5856 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5857 && DECL_NAME (constructor_unfilled_fields) == 0)
5858 constructor_unfilled_fields =
5859 TREE_CHAIN (constructor_unfilled_fields);
5861 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5862 constructor_unfilled_fields = 0;
5864 /* Now output any pending elements which have become next. */
5865 if (pending)
5866 output_pending_init_elements (0);
5869 /* Output any pending elements which have become next.
5870 As we output elements, constructor_unfilled_{fields,index}
5871 advances, which may cause other elements to become next;
5872 if so, they too are output.
5874 If ALL is 0, we return when there are
5875 no more pending elements to output now.
5877 If ALL is 1, we output space as necessary so that
5878 we can output all the pending elements. */
5880 static void
5881 output_pending_init_elements (int all)
5883 struct init_node *elt = constructor_pending_elts;
5884 tree next;
5886 retry:
5888 /* Look through the whole pending tree.
5889 If we find an element that should be output now,
5890 output it. Otherwise, set NEXT to the element
5891 that comes first among those still pending. */
5893 next = 0;
5894 while (elt)
5896 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5898 if (tree_int_cst_equal (elt->purpose,
5899 constructor_unfilled_index))
5900 output_init_element (elt->value, true,
5901 TREE_TYPE (constructor_type),
5902 constructor_unfilled_index, 0);
5903 else if (tree_int_cst_lt (constructor_unfilled_index,
5904 elt->purpose))
5906 /* Advance to the next smaller node. */
5907 if (elt->left)
5908 elt = elt->left;
5909 else
5911 /* We have reached the smallest node bigger than the
5912 current unfilled index. Fill the space first. */
5913 next = elt->purpose;
5914 break;
5917 else
5919 /* Advance to the next bigger node. */
5920 if (elt->right)
5921 elt = elt->right;
5922 else
5924 /* We have reached the biggest node in a subtree. Find
5925 the parent of it, which is the next bigger node. */
5926 while (elt->parent && elt->parent->right == elt)
5927 elt = elt->parent;
5928 elt = elt->parent;
5929 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5930 elt->purpose))
5932 next = elt->purpose;
5933 break;
5938 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5939 || TREE_CODE (constructor_type) == UNION_TYPE)
5941 tree ctor_unfilled_bitpos, elt_bitpos;
5943 /* If the current record is complete we are done. */
5944 if (constructor_unfilled_fields == 0)
5945 break;
5947 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5948 elt_bitpos = bit_position (elt->purpose);
5949 /* We can't compare fields here because there might be empty
5950 fields in between. */
5951 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5953 constructor_unfilled_fields = elt->purpose;
5954 output_init_element (elt->value, true, TREE_TYPE (elt->purpose),
5955 elt->purpose, 0);
5957 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5959 /* Advance to the next smaller node. */
5960 if (elt->left)
5961 elt = elt->left;
5962 else
5964 /* We have reached the smallest node bigger than the
5965 current unfilled field. Fill the space first. */
5966 next = elt->purpose;
5967 break;
5970 else
5972 /* Advance to the next bigger node. */
5973 if (elt->right)
5974 elt = elt->right;
5975 else
5977 /* We have reached the biggest node in a subtree. Find
5978 the parent of it, which is the next bigger node. */
5979 while (elt->parent && elt->parent->right == elt)
5980 elt = elt->parent;
5981 elt = elt->parent;
5982 if (elt
5983 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5984 bit_position (elt->purpose))))
5986 next = elt->purpose;
5987 break;
5994 /* Ordinarily return, but not if we want to output all
5995 and there are elements left. */
5996 if (!(all && next != 0))
5997 return;
5999 /* If it's not incremental, just skip over the gap, so that after
6000 jumping to retry we will output the next successive element. */
6001 if (TREE_CODE (constructor_type) == RECORD_TYPE
6002 || TREE_CODE (constructor_type) == UNION_TYPE)
6003 constructor_unfilled_fields = next;
6004 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6005 constructor_unfilled_index = next;
6007 /* ELT now points to the node in the pending tree with the next
6008 initializer to output. */
6009 goto retry;
6012 /* Add one non-braced element to the current constructor level.
6013 This adjusts the current position within the constructor's type.
6014 This may also start or terminate implicit levels
6015 to handle a partly-braced initializer.
6017 Once this has found the correct level for the new element,
6018 it calls output_init_element. */
6020 void
6021 process_init_element (struct c_expr value)
6023 tree orig_value = value.value;
6024 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
6025 bool strict_string = value.original_code == STRING_CST;
6027 designator_depth = 0;
6028 designator_errorneous = 0;
6030 /* Handle superfluous braces around string cst as in
6031 char x[] = {"foo"}; */
6032 if (string_flag
6033 && constructor_type
6034 && TREE_CODE (constructor_type) == ARRAY_TYPE
6035 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
6036 && integer_zerop (constructor_unfilled_index))
6038 if (constructor_stack->replacement_value.value)
6039 error_init ("excess elements in char array initializer");
6040 constructor_stack->replacement_value = value;
6041 return;
6044 if (constructor_stack->replacement_value.value != 0)
6046 error_init ("excess elements in struct initializer");
6047 return;
6050 /* Ignore elements of a brace group if it is entirely superfluous
6051 and has already been diagnosed. */
6052 if (constructor_type == 0)
6053 return;
6055 /* If we've exhausted any levels that didn't have braces,
6056 pop them now. */
6057 while (constructor_stack->implicit)
6059 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6060 || TREE_CODE (constructor_type) == UNION_TYPE)
6061 && constructor_fields == 0)
6062 process_init_element (pop_init_level (1));
6063 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6064 && (constructor_max_index == 0
6065 || tree_int_cst_lt (constructor_max_index,
6066 constructor_index)))
6067 process_init_element (pop_init_level (1));
6068 else
6069 break;
6072 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6073 if (constructor_range_stack)
6075 /* If value is a compound literal and we'll be just using its
6076 content, don't put it into a SAVE_EXPR. */
6077 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
6078 || !require_constant_value
6079 || flag_isoc99)
6080 value.value = save_expr (value.value);
6083 while (1)
6085 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6087 tree fieldtype;
6088 enum tree_code fieldcode;
6090 if (constructor_fields == 0)
6092 pedwarn_init ("excess elements in struct initializer");
6093 break;
6096 fieldtype = TREE_TYPE (constructor_fields);
6097 if (fieldtype != error_mark_node)
6098 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6099 fieldcode = TREE_CODE (fieldtype);
6101 /* Error for non-static initialization of a flexible array member. */
6102 if (fieldcode == ARRAY_TYPE
6103 && !require_constant_value
6104 && TYPE_SIZE (fieldtype) == NULL_TREE
6105 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6107 error_init ("non-static initialization of a flexible array member");
6108 break;
6111 /* Accept a string constant to initialize a subarray. */
6112 if (value.value != 0
6113 && fieldcode == ARRAY_TYPE
6114 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6115 && string_flag)
6116 value.value = orig_value;
6117 /* Otherwise, if we have come to a subaggregate,
6118 and we don't have an element of its type, push into it. */
6119 else if (value.value != 0
6120 && value.value != error_mark_node
6121 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6122 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6123 || fieldcode == UNION_TYPE))
6125 push_init_level (1);
6126 continue;
6129 if (value.value)
6131 push_member_name (constructor_fields);
6132 output_init_element (value.value, strict_string,
6133 fieldtype, constructor_fields, 1);
6134 RESTORE_SPELLING_DEPTH (constructor_depth);
6136 else
6137 /* Do the bookkeeping for an element that was
6138 directly output as a constructor. */
6140 /* For a record, keep track of end position of last field. */
6141 if (DECL_SIZE (constructor_fields))
6142 constructor_bit_index
6143 = size_binop (PLUS_EXPR,
6144 bit_position (constructor_fields),
6145 DECL_SIZE (constructor_fields));
6147 /* If the current field was the first one not yet written out,
6148 it isn't now, so update. */
6149 if (constructor_unfilled_fields == constructor_fields)
6151 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6152 /* Skip any nameless bit fields. */
6153 while (constructor_unfilled_fields != 0
6154 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6155 && DECL_NAME (constructor_unfilled_fields) == 0)
6156 constructor_unfilled_fields =
6157 TREE_CHAIN (constructor_unfilled_fields);
6161 constructor_fields = TREE_CHAIN (constructor_fields);
6162 /* Skip any nameless bit fields at the beginning. */
6163 while (constructor_fields != 0
6164 && DECL_C_BIT_FIELD (constructor_fields)
6165 && DECL_NAME (constructor_fields) == 0)
6166 constructor_fields = TREE_CHAIN (constructor_fields);
6168 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6170 tree fieldtype;
6171 enum tree_code fieldcode;
6173 if (constructor_fields == 0)
6175 pedwarn_init ("excess elements in union initializer");
6176 break;
6179 fieldtype = TREE_TYPE (constructor_fields);
6180 if (fieldtype != error_mark_node)
6181 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
6182 fieldcode = TREE_CODE (fieldtype);
6184 /* Warn that traditional C rejects initialization of unions.
6185 We skip the warning if the value is zero. This is done
6186 under the assumption that the zero initializer in user
6187 code appears conditioned on e.g. __STDC__ to avoid
6188 "missing initializer" warnings and relies on default
6189 initialization to zero in the traditional C case.
6190 We also skip the warning if the initializer is designated,
6191 again on the assumption that this must be conditional on
6192 __STDC__ anyway (and we've already complained about the
6193 member-designator already). */
6194 if (warn_traditional && !in_system_header && !constructor_designated
6195 && !(value.value && (integer_zerop (value.value)
6196 || real_zerop (value.value))))
6197 warning (0, "traditional C rejects initialization of unions");
6199 /* Accept a string constant to initialize a subarray. */
6200 if (value.value != 0
6201 && fieldcode == ARRAY_TYPE
6202 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
6203 && string_flag)
6204 value.value = orig_value;
6205 /* Otherwise, if we have come to a subaggregate,
6206 and we don't have an element of its type, push into it. */
6207 else if (value.value != 0
6208 && value.value != error_mark_node
6209 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
6210 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6211 || fieldcode == UNION_TYPE))
6213 push_init_level (1);
6214 continue;
6217 if (value.value)
6219 push_member_name (constructor_fields);
6220 output_init_element (value.value, strict_string,
6221 fieldtype, constructor_fields, 1);
6222 RESTORE_SPELLING_DEPTH (constructor_depth);
6224 else
6225 /* Do the bookkeeping for an element that was
6226 directly output as a constructor. */
6228 constructor_bit_index = DECL_SIZE (constructor_fields);
6229 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6232 constructor_fields = 0;
6234 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6236 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6237 enum tree_code eltcode = TREE_CODE (elttype);
6239 /* Accept a string constant to initialize a subarray. */
6240 if (value.value != 0
6241 && eltcode == ARRAY_TYPE
6242 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
6243 && string_flag)
6244 value.value = orig_value;
6245 /* Otherwise, if we have come to a subaggregate,
6246 and we don't have an element of its type, push into it. */
6247 else if (value.value != 0
6248 && value.value != error_mark_node
6249 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
6250 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6251 || eltcode == UNION_TYPE))
6253 push_init_level (1);
6254 continue;
6257 if (constructor_max_index != 0
6258 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6259 || integer_all_onesp (constructor_max_index)))
6261 pedwarn_init ("excess elements in array initializer");
6262 break;
6265 /* Now output the actual element. */
6266 if (value.value)
6268 push_array_bounds (tree_low_cst (constructor_index, 0));
6269 output_init_element (value.value, strict_string,
6270 elttype, constructor_index, 1);
6271 RESTORE_SPELLING_DEPTH (constructor_depth);
6274 constructor_index
6275 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6277 if (!value.value)
6278 /* If we are doing the bookkeeping for an element that was
6279 directly output as a constructor, we must update
6280 constructor_unfilled_index. */
6281 constructor_unfilled_index = constructor_index;
6283 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6285 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6287 /* Do a basic check of initializer size. Note that vectors
6288 always have a fixed size derived from their type. */
6289 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6291 pedwarn_init ("excess elements in vector initializer");
6292 break;
6295 /* Now output the actual element. */
6296 if (value.value)
6297 output_init_element (value.value, strict_string,
6298 elttype, constructor_index, 1);
6300 constructor_index
6301 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6303 if (!value.value)
6304 /* If we are doing the bookkeeping for an element that was
6305 directly output as a constructor, we must update
6306 constructor_unfilled_index. */
6307 constructor_unfilled_index = constructor_index;
6310 /* Handle the sole element allowed in a braced initializer
6311 for a scalar variable. */
6312 else if (constructor_type != error_mark_node
6313 && constructor_fields == 0)
6315 pedwarn_init ("excess elements in scalar initializer");
6316 break;
6318 else
6320 if (value.value)
6321 output_init_element (value.value, strict_string,
6322 constructor_type, NULL_TREE, 1);
6323 constructor_fields = 0;
6326 /* Handle range initializers either at this level or anywhere higher
6327 in the designator stack. */
6328 if (constructor_range_stack)
6330 struct constructor_range_stack *p, *range_stack;
6331 int finish = 0;
6333 range_stack = constructor_range_stack;
6334 constructor_range_stack = 0;
6335 while (constructor_stack != range_stack->stack)
6337 gcc_assert (constructor_stack->implicit);
6338 process_init_element (pop_init_level (1));
6340 for (p = range_stack;
6341 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6342 p = p->prev)
6344 gcc_assert (constructor_stack->implicit);
6345 process_init_element (pop_init_level (1));
6348 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6349 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6350 finish = 1;
6352 while (1)
6354 constructor_index = p->index;
6355 constructor_fields = p->fields;
6356 if (finish && p->range_end && p->index == p->range_start)
6358 finish = 0;
6359 p->prev = 0;
6361 p = p->next;
6362 if (!p)
6363 break;
6364 push_init_level (2);
6365 p->stack = constructor_stack;
6366 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6367 p->index = p->range_start;
6370 if (!finish)
6371 constructor_range_stack = range_stack;
6372 continue;
6375 break;
6378 constructor_range_stack = 0;
6381 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6382 (guaranteed to be 'volatile' or null) and ARGS (represented using
6383 an ASM_EXPR node). */
6384 tree
6385 build_asm_stmt (tree cv_qualifier, tree args)
6387 if (!ASM_VOLATILE_P (args) && cv_qualifier)
6388 ASM_VOLATILE_P (args) = 1;
6389 return add_stmt (args);
6392 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6393 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6394 SIMPLE indicates whether there was anything at all after the
6395 string in the asm expression -- asm("blah") and asm("blah" : )
6396 are subtly different. We use a ASM_EXPR node to represent this. */
6397 tree
6398 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6399 bool simple)
6401 tree tail;
6402 tree args;
6403 int i;
6404 const char *constraint;
6405 const char **oconstraints;
6406 bool allows_mem, allows_reg, is_inout;
6407 int ninputs, noutputs;
6409 ninputs = list_length (inputs);
6410 noutputs = list_length (outputs);
6411 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
6413 string = resolve_asm_operand_names (string, outputs, inputs);
6415 /* Remove output conversions that change the type but not the mode. */
6416 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
6418 tree output = TREE_VALUE (tail);
6420 /* ??? Really, this should not be here. Users should be using a
6421 proper lvalue, dammit. But there's a long history of using casts
6422 in the output operands. In cases like longlong.h, this becomes a
6423 primitive form of typechecking -- if the cast can be removed, then
6424 the output operand had a type of the proper width; otherwise we'll
6425 get an error. Gross, but ... */
6426 STRIP_NOPS (output);
6428 if (!lvalue_or_else (output, lv_asm))
6429 output = error_mark_node;
6431 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6432 oconstraints[i] = constraint;
6434 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6435 &allows_mem, &allows_reg, &is_inout))
6437 /* If the operand is going to end up in memory,
6438 mark it addressable. */
6439 if (!allows_reg && !c_mark_addressable (output))
6440 output = error_mark_node;
6442 else
6443 output = error_mark_node;
6445 TREE_VALUE (tail) = output;
6448 /* Perform default conversions on array and function inputs.
6449 Don't do this for other types as it would screw up operands
6450 expected to be in memory. */
6451 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
6453 tree input;
6455 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6456 input = TREE_VALUE (tail);
6458 input = default_function_array_conversion (input);
6460 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
6461 oconstraints, &allows_mem, &allows_reg))
6463 /* If the operand is going to end up in memory,
6464 mark it addressable. */
6465 if (!allows_reg && allows_mem)
6467 /* Strip the nops as we allow this case. FIXME, this really
6468 should be rejected or made deprecated. */
6469 STRIP_NOPS (input);
6470 if (!c_mark_addressable (input))
6471 input = error_mark_node;
6474 else
6475 input = error_mark_node;
6477 TREE_VALUE (tail) = input;
6480 args = build_stmt (ASM_EXPR, string, outputs, inputs, clobbers);
6482 /* Simple asm statements are treated as volatile. */
6483 if (simple)
6485 ASM_VOLATILE_P (args) = 1;
6486 ASM_INPUT_P (args) = 1;
6489 return args;
6492 /* Generate a goto statement to LABEL. */
6494 tree
6495 c_finish_goto_label (tree label)
6497 tree decl = lookup_label (label);
6498 if (!decl)
6499 return NULL_TREE;
6501 if (C_DECL_UNJUMPABLE_STMT_EXPR (decl))
6503 error ("jump into statement expression");
6504 return NULL_TREE;
6507 if (C_DECL_UNJUMPABLE_VM (decl))
6509 error ("jump into scope of identifier with variably modified type");
6510 return NULL_TREE;
6513 if (!C_DECL_UNDEFINABLE_STMT_EXPR (decl))
6515 /* No jump from outside this statement expression context, so
6516 record that there is a jump from within this context. */
6517 struct c_label_list *nlist;
6518 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6519 nlist->next = label_context_stack_se->labels_used;
6520 nlist->label = decl;
6521 label_context_stack_se->labels_used = nlist;
6524 if (!C_DECL_UNDEFINABLE_VM (decl))
6526 /* No jump from outside this context context of identifiers with
6527 variably modified type, so record that there is a jump from
6528 within this context. */
6529 struct c_label_list *nlist;
6530 nlist = XOBNEW (&parser_obstack, struct c_label_list);
6531 nlist->next = label_context_stack_vm->labels_used;
6532 nlist->label = decl;
6533 label_context_stack_vm->labels_used = nlist;
6536 TREE_USED (decl) = 1;
6537 return add_stmt (build1 (GOTO_EXPR, void_type_node, decl));
6540 /* Generate a computed goto statement to EXPR. */
6542 tree
6543 c_finish_goto_ptr (tree expr)
6545 if (pedantic)
6546 pedwarn ("ISO C forbids %<goto *expr;%>");
6547 expr = convert (ptr_type_node, expr);
6548 return add_stmt (build1 (GOTO_EXPR, void_type_node, expr));
6551 /* Generate a C `return' statement. RETVAL is the expression for what
6552 to return, or a null pointer for `return;' with no value. */
6554 tree
6555 c_finish_return (tree retval)
6557 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6559 if (TREE_THIS_VOLATILE (current_function_decl))
6560 warning (0, "function declared %<noreturn%> has a %<return%> statement");
6562 if (!retval)
6564 current_function_returns_null = 1;
6565 if ((warn_return_type || flag_isoc99)
6566 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6567 pedwarn_c99 ("%<return%> with no value, in "
6568 "function returning non-void");
6570 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6572 current_function_returns_null = 1;
6573 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6574 pedwarn ("%<return%> with a value, in function returning void");
6576 else
6578 tree t = convert_for_assignment (valtype, retval, ic_return,
6579 NULL_TREE, NULL_TREE, 0);
6580 tree res = DECL_RESULT (current_function_decl);
6581 tree inner;
6583 current_function_returns_value = 1;
6584 if (t == error_mark_node)
6585 return NULL_TREE;
6587 inner = t = convert (TREE_TYPE (res), t);
6589 /* Strip any conversions, additions, and subtractions, and see if
6590 we are returning the address of a local variable. Warn if so. */
6591 while (1)
6593 switch (TREE_CODE (inner))
6595 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6596 case PLUS_EXPR:
6597 inner = TREE_OPERAND (inner, 0);
6598 continue;
6600 case MINUS_EXPR:
6601 /* If the second operand of the MINUS_EXPR has a pointer
6602 type (or is converted from it), this may be valid, so
6603 don't give a warning. */
6605 tree op1 = TREE_OPERAND (inner, 1);
6607 while (!POINTER_TYPE_P (TREE_TYPE (op1))
6608 && (TREE_CODE (op1) == NOP_EXPR
6609 || TREE_CODE (op1) == NON_LVALUE_EXPR
6610 || TREE_CODE (op1) == CONVERT_EXPR))
6611 op1 = TREE_OPERAND (op1, 0);
6613 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6614 break;
6616 inner = TREE_OPERAND (inner, 0);
6617 continue;
6620 case ADDR_EXPR:
6621 inner = TREE_OPERAND (inner, 0);
6623 while (REFERENCE_CLASS_P (inner)
6624 && TREE_CODE (inner) != INDIRECT_REF)
6625 inner = TREE_OPERAND (inner, 0);
6627 if (DECL_P (inner)
6628 && !DECL_EXTERNAL (inner)
6629 && !TREE_STATIC (inner)
6630 && DECL_CONTEXT (inner) == current_function_decl)
6631 warning (0, "function returns address of local variable");
6632 break;
6634 default:
6635 break;
6638 break;
6641 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
6644 return add_stmt (build_stmt (RETURN_EXPR, retval));
6647 struct c_switch {
6648 /* The SWITCH_EXPR being built. */
6649 tree switch_expr;
6651 /* The original type of the testing expression, i.e. before the
6652 default conversion is applied. */
6653 tree orig_type;
6655 /* A splay-tree mapping the low element of a case range to the high
6656 element, or NULL_TREE if there is no high element. Used to
6657 determine whether or not a new case label duplicates an old case
6658 label. We need a tree, rather than simply a hash table, because
6659 of the GNU case range extension. */
6660 splay_tree cases;
6662 /* Number of nested statement expressions within this switch
6663 statement; if nonzero, case and default labels may not
6664 appear. */
6665 unsigned int blocked_stmt_expr;
6667 /* Scope of outermost declarations of identifiers with variably
6668 modified type within this switch statement; if nonzero, case and
6669 default labels may not appear. */
6670 unsigned int blocked_vm;
6672 /* The next node on the stack. */
6673 struct c_switch *next;
6676 /* A stack of the currently active switch statements. The innermost
6677 switch statement is on the top of the stack. There is no need to
6678 mark the stack for garbage collection because it is only active
6679 during the processing of the body of a function, and we never
6680 collect at that point. */
6682 struct c_switch *c_switch_stack;
6684 /* Start a C switch statement, testing expression EXP. Return the new
6685 SWITCH_EXPR. */
6687 tree
6688 c_start_case (tree exp)
6690 enum tree_code code;
6691 tree type, orig_type = error_mark_node;
6692 struct c_switch *cs;
6694 if (exp != error_mark_node)
6696 code = TREE_CODE (TREE_TYPE (exp));
6697 orig_type = TREE_TYPE (exp);
6699 if (!INTEGRAL_TYPE_P (orig_type)
6700 && code != ERROR_MARK)
6702 error ("switch quantity not an integer");
6703 exp = integer_zero_node;
6704 orig_type = error_mark_node;
6706 else
6708 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6710 if (warn_traditional && !in_system_header
6711 && (type == long_integer_type_node
6712 || type == long_unsigned_type_node))
6713 warning (0, "%<long%> switch expression not converted to "
6714 "%<int%> in ISO C");
6716 exp = default_conversion (exp);
6717 type = TREE_TYPE (exp);
6721 /* Add this new SWITCH_EXPR to the stack. */
6722 cs = XNEW (struct c_switch);
6723 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
6724 cs->orig_type = orig_type;
6725 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6726 cs->blocked_stmt_expr = 0;
6727 cs->blocked_vm = 0;
6728 cs->next = c_switch_stack;
6729 c_switch_stack = cs;
6731 return add_stmt (cs->switch_expr);
6734 /* Process a case label. */
6736 tree
6737 do_case (tree low_value, tree high_value)
6739 tree label = NULL_TREE;
6741 if (c_switch_stack && !c_switch_stack->blocked_stmt_expr
6742 && !c_switch_stack->blocked_vm)
6744 label = c_add_case_label (c_switch_stack->cases,
6745 SWITCH_COND (c_switch_stack->switch_expr),
6746 c_switch_stack->orig_type,
6747 low_value, high_value);
6748 if (label == error_mark_node)
6749 label = NULL_TREE;
6751 else if (c_switch_stack && c_switch_stack->blocked_stmt_expr)
6753 if (low_value)
6754 error ("case label in statement expression not containing "
6755 "enclosing switch statement");
6756 else
6757 error ("%<default%> label in statement expression not containing "
6758 "enclosing switch statement");
6760 else if (c_switch_stack && c_switch_stack->blocked_vm)
6762 if (low_value)
6763 error ("case label in scope of identifier with variably modified "
6764 "type not containing enclosing switch statement");
6765 else
6766 error ("%<default%> label in scope of identifier with variably "
6767 "modified type not containing enclosing switch statement");
6769 else if (low_value)
6770 error ("case label not within a switch statement");
6771 else
6772 error ("%<default%> label not within a switch statement");
6774 return label;
6777 /* Finish the switch statement. */
6779 void
6780 c_finish_case (tree body)
6782 struct c_switch *cs = c_switch_stack;
6783 location_t switch_location;
6785 SWITCH_BODY (cs->switch_expr) = body;
6787 /* We must not be within a statement expression nested in the switch
6788 at this point; we might, however, be within the scope of an
6789 identifier with variably modified type nested in the switch. */
6790 gcc_assert (!cs->blocked_stmt_expr);
6792 /* Emit warnings as needed. */
6793 if (EXPR_HAS_LOCATION (cs->switch_expr))
6794 switch_location = EXPR_LOCATION (cs->switch_expr);
6795 else
6796 switch_location = input_location;
6797 c_do_switch_warnings (cs->cases, switch_location,
6798 TREE_TYPE (cs->switch_expr),
6799 SWITCH_COND (cs->switch_expr));
6801 /* Pop the stack. */
6802 c_switch_stack = cs->next;
6803 splay_tree_delete (cs->cases);
6804 XDELETE (cs);
6807 /* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
6808 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
6809 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
6810 statement, and was not surrounded with parenthesis. */
6812 void
6813 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
6814 tree else_block, bool nested_if)
6816 tree stmt;
6818 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
6819 if (warn_parentheses && nested_if && else_block == NULL)
6821 tree inner_if = then_block;
6823 /* We know from the grammar productions that there is an IF nested
6824 within THEN_BLOCK. Due to labels and c99 conditional declarations,
6825 it might not be exactly THEN_BLOCK, but should be the last
6826 non-container statement within. */
6827 while (1)
6828 switch (TREE_CODE (inner_if))
6830 case COND_EXPR:
6831 goto found;
6832 case BIND_EXPR:
6833 inner_if = BIND_EXPR_BODY (inner_if);
6834 break;
6835 case STATEMENT_LIST:
6836 inner_if = expr_last (then_block);
6837 break;
6838 case TRY_FINALLY_EXPR:
6839 case TRY_CATCH_EXPR:
6840 inner_if = TREE_OPERAND (inner_if, 0);
6841 break;
6842 default:
6843 gcc_unreachable ();
6845 found:
6847 if (COND_EXPR_ELSE (inner_if))
6848 warning (0, "%Hsuggest explicit braces to avoid ambiguous %<else%>",
6849 &if_locus);
6852 /* Diagnose ";" via the special empty statement node that we create. */
6853 if (extra_warnings)
6855 if (TREE_CODE (then_block) == NOP_EXPR && !TREE_TYPE (then_block))
6857 if (!else_block)
6858 warning (0, "%Hempty body in an if-statement",
6859 EXPR_LOCUS (then_block));
6860 then_block = alloc_stmt_list ();
6862 if (else_block
6863 && TREE_CODE (else_block) == NOP_EXPR
6864 && !TREE_TYPE (else_block))
6866 warning (0, "%Hempty body in an else-statement",
6867 EXPR_LOCUS (else_block));
6868 else_block = alloc_stmt_list ();
6872 stmt = build3 (COND_EXPR, NULL_TREE, cond, then_block, else_block);
6873 SET_EXPR_LOCATION (stmt, if_locus);
6874 add_stmt (stmt);
6877 /* Emit a general-purpose loop construct. START_LOCUS is the location of
6878 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
6879 is false for DO loops. INCR is the FOR increment expression. BODY is
6880 the statement controlled by the loop. BLAB is the break label. CLAB is
6881 the continue label. Everything is allowed to be NULL. */
6883 void
6884 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
6885 tree blab, tree clab, bool cond_is_first)
6887 tree entry = NULL, exit = NULL, t;
6889 /* If the condition is zero don't generate a loop construct. */
6890 if (cond && integer_zerop (cond))
6892 if (cond_is_first)
6894 t = build_and_jump (&blab);
6895 SET_EXPR_LOCATION (t, start_locus);
6896 add_stmt (t);
6899 else
6901 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6903 /* If we have an exit condition, then we build an IF with gotos either
6904 out of the loop, or to the top of it. If there's no exit condition,
6905 then we just build a jump back to the top. */
6906 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
6908 if (cond && !integer_nonzerop (cond))
6910 /* Canonicalize the loop condition to the end. This means
6911 generating a branch to the loop condition. Reuse the
6912 continue label, if possible. */
6913 if (cond_is_first)
6915 if (incr || !clab)
6917 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
6918 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
6920 else
6921 t = build1 (GOTO_EXPR, void_type_node, clab);
6922 SET_EXPR_LOCATION (t, start_locus);
6923 add_stmt (t);
6926 t = build_and_jump (&blab);
6927 exit = build3 (COND_EXPR, void_type_node, cond, exit, t);
6928 exit = fold (exit);
6929 if (cond_is_first)
6930 SET_EXPR_LOCATION (exit, start_locus);
6931 else
6932 SET_EXPR_LOCATION (exit, input_location);
6935 add_stmt (top);
6938 if (body)
6939 add_stmt (body);
6940 if (clab)
6941 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
6942 if (incr)
6943 add_stmt (incr);
6944 if (entry)
6945 add_stmt (entry);
6946 if (exit)
6947 add_stmt (exit);
6948 if (blab)
6949 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
6952 tree
6953 c_finish_bc_stmt (tree *label_p, bool is_break)
6955 bool skip;
6956 tree label = *label_p;
6958 /* In switch statements break is sometimes stylistically used after
6959 a return statement. This can lead to spurious warnings about
6960 control reaching the end of a non-void function when it is
6961 inlined. Note that we are calling block_may_fallthru with
6962 language specific tree nodes; this works because
6963 block_may_fallthru returns true when given something it does not
6964 understand. */
6965 skip = !block_may_fallthru (cur_stmt_list);
6967 if (!label)
6969 if (!skip)
6970 *label_p = label = create_artificial_label ();
6972 else if (TREE_CODE (label) != LABEL_DECL)
6974 if (is_break)
6975 error ("break statement not within loop or switch");
6976 else
6977 error ("continue statement not within a loop");
6978 return NULL_TREE;
6981 if (skip)
6982 return NULL_TREE;
6984 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
6987 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
6989 static void
6990 emit_side_effect_warnings (tree expr)
6992 if (expr == error_mark_node)
6994 else if (!TREE_SIDE_EFFECTS (expr))
6996 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
6997 warning (0, "%Hstatement with no effect",
6998 EXPR_HAS_LOCATION (expr) ? EXPR_LOCUS (expr) : &input_location);
7000 else if (warn_unused_value)
7001 warn_if_unused_value (expr, input_location);
7004 /* Process an expression as if it were a complete statement. Emit
7005 diagnostics, but do not call ADD_STMT. */
7007 tree
7008 c_process_expr_stmt (tree expr)
7010 if (!expr)
7011 return NULL_TREE;
7013 /* Do default conversion if safe and possibly important,
7014 in case within ({...}). */
7015 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
7016 && (flag_isoc99 || lvalue_p (expr)))
7017 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
7018 expr = default_conversion (expr);
7020 if (warn_sequence_point)
7021 verify_sequence_points (expr);
7023 if (TREE_TYPE (expr) != error_mark_node
7024 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
7025 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
7026 error ("expression statement has incomplete type");
7028 /* If we're not processing a statement expression, warn about unused values.
7029 Warnings for statement expressions will be emitted later, once we figure
7030 out which is the result. */
7031 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7032 && (extra_warnings || warn_unused_value))
7033 emit_side_effect_warnings (expr);
7035 /* If the expression is not of a type to which we cannot assign a line
7036 number, wrap the thing in a no-op NOP_EXPR. */
7037 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
7038 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
7040 if (EXPR_P (expr))
7041 SET_EXPR_LOCATION (expr, input_location);
7043 return expr;
7046 /* Emit an expression as a statement. */
7048 tree
7049 c_finish_expr_stmt (tree expr)
7051 if (expr)
7052 return add_stmt (c_process_expr_stmt (expr));
7053 else
7054 return NULL;
7057 /* Do the opposite and emit a statement as an expression. To begin,
7058 create a new binding level and return it. */
7060 tree
7061 c_begin_stmt_expr (void)
7063 tree ret;
7064 struct c_label_context_se *nstack;
7065 struct c_label_list *glist;
7067 /* We must force a BLOCK for this level so that, if it is not expanded
7068 later, there is a way to turn off the entire subtree of blocks that
7069 are contained in it. */
7070 keep_next_level ();
7071 ret = c_begin_compound_stmt (true);
7072 if (c_switch_stack)
7074 c_switch_stack->blocked_stmt_expr++;
7075 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7077 for (glist = label_context_stack_se->labels_used;
7078 glist != NULL;
7079 glist = glist->next)
7081 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 1;
7083 nstack = XOBNEW (&parser_obstack, struct c_label_context_se);
7084 nstack->labels_def = NULL;
7085 nstack->labels_used = NULL;
7086 nstack->next = label_context_stack_se;
7087 label_context_stack_se = nstack;
7089 /* Mark the current statement list as belonging to a statement list. */
7090 STATEMENT_LIST_STMT_EXPR (ret) = 1;
7092 return ret;
7095 tree
7096 c_finish_stmt_expr (tree body)
7098 tree last, type, tmp, val;
7099 tree *last_p;
7100 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7102 body = c_end_compound_stmt (body, true);
7103 if (c_switch_stack)
7105 gcc_assert (c_switch_stack->blocked_stmt_expr != 0);
7106 c_switch_stack->blocked_stmt_expr--;
7108 /* It is no longer possible to jump to labels defined within this
7109 statement expression. */
7110 for (dlist = label_context_stack_se->labels_def;
7111 dlist != NULL;
7112 dlist = dlist->next)
7114 C_DECL_UNJUMPABLE_STMT_EXPR (dlist->label) = 1;
7116 /* It is again possible to define labels with a goto just outside
7117 this statement expression. */
7118 for (glist = label_context_stack_se->next->labels_used;
7119 glist != NULL;
7120 glist = glist->next)
7122 C_DECL_UNDEFINABLE_STMT_EXPR (glist->label) = 0;
7123 glist_prev = glist;
7125 if (glist_prev != NULL)
7126 glist_prev->next = label_context_stack_se->labels_used;
7127 else
7128 label_context_stack_se->next->labels_used
7129 = label_context_stack_se->labels_used;
7130 label_context_stack_se = label_context_stack_se->next;
7132 /* Locate the last statement in BODY. See c_end_compound_stmt
7133 about always returning a BIND_EXPR. */
7134 last_p = &BIND_EXPR_BODY (body);
7135 last = BIND_EXPR_BODY (body);
7137 continue_searching:
7138 if (TREE_CODE (last) == STATEMENT_LIST)
7140 tree_stmt_iterator i;
7142 /* This can happen with degenerate cases like ({ }). No value. */
7143 if (!TREE_SIDE_EFFECTS (last))
7144 return body;
7146 /* If we're supposed to generate side effects warnings, process
7147 all of the statements except the last. */
7148 if (extra_warnings || warn_unused_value)
7150 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
7151 emit_side_effect_warnings (tsi_stmt (i));
7153 else
7154 i = tsi_last (last);
7155 last_p = tsi_stmt_ptr (i);
7156 last = *last_p;
7159 /* If the end of the list is exception related, then the list was split
7160 by a call to push_cleanup. Continue searching. */
7161 if (TREE_CODE (last) == TRY_FINALLY_EXPR
7162 || TREE_CODE (last) == TRY_CATCH_EXPR)
7164 last_p = &TREE_OPERAND (last, 0);
7165 last = *last_p;
7166 goto continue_searching;
7169 /* In the case that the BIND_EXPR is not necessary, return the
7170 expression out from inside it. */
7171 if (last == error_mark_node
7172 || (last == BIND_EXPR_BODY (body)
7173 && BIND_EXPR_VARS (body) == NULL))
7174 return last;
7176 /* Extract the type of said expression. */
7177 type = TREE_TYPE (last);
7179 /* If we're not returning a value at all, then the BIND_EXPR that
7180 we already have is a fine expression to return. */
7181 if (!type || VOID_TYPE_P (type))
7182 return body;
7184 /* Now that we've located the expression containing the value, it seems
7185 silly to make voidify_wrapper_expr repeat the process. Create a
7186 temporary of the appropriate type and stick it in a TARGET_EXPR. */
7187 tmp = create_tmp_var_raw (type, NULL);
7189 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
7190 tree_expr_nonnegative_p giving up immediately. */
7191 val = last;
7192 if (TREE_CODE (val) == NOP_EXPR
7193 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
7194 val = TREE_OPERAND (val, 0);
7196 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
7197 SET_EXPR_LOCUS (*last_p, EXPR_LOCUS (last));
7199 return build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
7202 /* Begin the scope of an identifier of variably modified type, scope
7203 number SCOPE. Jumping from outside this scope to inside it is not
7204 permitted. */
7206 void
7207 c_begin_vm_scope (unsigned int scope)
7209 struct c_label_context_vm *nstack;
7210 struct c_label_list *glist;
7212 gcc_assert (scope > 0);
7213 if (c_switch_stack && !c_switch_stack->blocked_vm)
7214 c_switch_stack->blocked_vm = scope;
7215 for (glist = label_context_stack_vm->labels_used;
7216 glist != NULL;
7217 glist = glist->next)
7219 C_DECL_UNDEFINABLE_VM (glist->label) = 1;
7221 nstack = XOBNEW (&parser_obstack, struct c_label_context_vm);
7222 nstack->labels_def = NULL;
7223 nstack->labels_used = NULL;
7224 nstack->scope = scope;
7225 nstack->next = label_context_stack_vm;
7226 label_context_stack_vm = nstack;
7229 /* End a scope which may contain identifiers of variably modified
7230 type, scope number SCOPE. */
7232 void
7233 c_end_vm_scope (unsigned int scope)
7235 if (label_context_stack_vm == NULL)
7236 return;
7237 if (c_switch_stack && c_switch_stack->blocked_vm == scope)
7238 c_switch_stack->blocked_vm = 0;
7239 /* We may have a number of nested scopes of identifiers with
7240 variably modified type, all at this depth. Pop each in turn. */
7241 while (label_context_stack_vm->scope == scope)
7243 struct c_label_list *dlist, *glist, *glist_prev = NULL;
7245 /* It is no longer possible to jump to labels defined within this
7246 scope. */
7247 for (dlist = label_context_stack_vm->labels_def;
7248 dlist != NULL;
7249 dlist = dlist->next)
7251 C_DECL_UNJUMPABLE_VM (dlist->label) = 1;
7253 /* It is again possible to define labels with a goto just outside
7254 this scope. */
7255 for (glist = label_context_stack_vm->next->labels_used;
7256 glist != NULL;
7257 glist = glist->next)
7259 C_DECL_UNDEFINABLE_VM (glist->label) = 0;
7260 glist_prev = glist;
7262 if (glist_prev != NULL)
7263 glist_prev->next = label_context_stack_vm->labels_used;
7264 else
7265 label_context_stack_vm->next->labels_used
7266 = label_context_stack_vm->labels_used;
7267 label_context_stack_vm = label_context_stack_vm->next;
7271 /* Begin and end compound statements. This is as simple as pushing
7272 and popping new statement lists from the tree. */
7274 tree
7275 c_begin_compound_stmt (bool do_scope)
7277 tree stmt = push_stmt_list ();
7278 if (do_scope)
7279 push_scope ();
7280 return stmt;
7283 tree
7284 c_end_compound_stmt (tree stmt, bool do_scope)
7286 tree block = NULL;
7288 if (do_scope)
7290 if (c_dialect_objc ())
7291 objc_clear_super_receiver ();
7292 block = pop_scope ();
7295 stmt = pop_stmt_list (stmt);
7296 stmt = c_build_bind_expr (block, stmt);
7298 /* If this compound statement is nested immediately inside a statement
7299 expression, then force a BIND_EXPR to be created. Otherwise we'll
7300 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
7301 STATEMENT_LISTs merge, and thus we can lose track of what statement
7302 was really last. */
7303 if (cur_stmt_list
7304 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
7305 && TREE_CODE (stmt) != BIND_EXPR)
7307 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
7308 TREE_SIDE_EFFECTS (stmt) = 1;
7311 return stmt;
7314 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
7315 when the current scope is exited. EH_ONLY is true when this is not
7316 meant to apply to normal control flow transfer. */
7318 void
7319 push_cleanup (tree ARG_UNUSED (decl), tree cleanup, bool eh_only)
7321 enum tree_code code;
7322 tree stmt, list;
7323 bool stmt_expr;
7325 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
7326 stmt = build_stmt (code, NULL, cleanup);
7327 add_stmt (stmt);
7328 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
7329 list = push_stmt_list ();
7330 TREE_OPERAND (stmt, 0) = list;
7331 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
7334 /* Build a binary-operation expression without default conversions.
7335 CODE is the kind of expression to build.
7336 This function differs from `build' in several ways:
7337 the data type of the result is computed and recorded in it,
7338 warnings are generated if arg data types are invalid,
7339 special handling for addition and subtraction of pointers is known,
7340 and some optimization is done (operations on narrow ints
7341 are done in the narrower type when that gives the same result).
7342 Constant folding is also done before the result is returned.
7344 Note that the operands will never have enumeral types, or function
7345 or array types, because either they will have the default conversions
7346 performed or they have both just been converted to some other type in which
7347 the arithmetic is to be done. */
7349 tree
7350 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
7351 int convert_p)
7353 tree type0, type1;
7354 enum tree_code code0, code1;
7355 tree op0, op1;
7357 /* Expression code to give to the expression when it is built.
7358 Normally this is CODE, which is what the caller asked for,
7359 but in some special cases we change it. */
7360 enum tree_code resultcode = code;
7362 /* Data type in which the computation is to be performed.
7363 In the simplest cases this is the common type of the arguments. */
7364 tree result_type = NULL;
7366 /* Nonzero means operands have already been type-converted
7367 in whatever way is necessary.
7368 Zero means they need to be converted to RESULT_TYPE. */
7369 int converted = 0;
7371 /* Nonzero means create the expression with this type, rather than
7372 RESULT_TYPE. */
7373 tree build_type = 0;
7375 /* Nonzero means after finally constructing the expression
7376 convert it to this type. */
7377 tree final_type = 0;
7379 /* Nonzero if this is an operation like MIN or MAX which can
7380 safely be computed in short if both args are promoted shorts.
7381 Also implies COMMON.
7382 -1 indicates a bitwise operation; this makes a difference
7383 in the exact conditions for when it is safe to do the operation
7384 in a narrower mode. */
7385 int shorten = 0;
7387 /* Nonzero if this is a comparison operation;
7388 if both args are promoted shorts, compare the original shorts.
7389 Also implies COMMON. */
7390 int short_compare = 0;
7392 /* Nonzero if this is a right-shift operation, which can be computed on the
7393 original short and then promoted if the operand is a promoted short. */
7394 int short_shift = 0;
7396 /* Nonzero means set RESULT_TYPE to the common type of the args. */
7397 int common = 0;
7399 if (convert_p)
7401 op0 = default_conversion (orig_op0);
7402 op1 = default_conversion (orig_op1);
7404 else
7406 op0 = orig_op0;
7407 op1 = orig_op1;
7410 type0 = TREE_TYPE (op0);
7411 type1 = TREE_TYPE (op1);
7413 /* The expression codes of the data types of the arguments tell us
7414 whether the arguments are integers, floating, pointers, etc. */
7415 code0 = TREE_CODE (type0);
7416 code1 = TREE_CODE (type1);
7418 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
7419 STRIP_TYPE_NOPS (op0);
7420 STRIP_TYPE_NOPS (op1);
7422 /* If an error was already reported for one of the arguments,
7423 avoid reporting another error. */
7425 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7426 return error_mark_node;
7428 switch (code)
7430 case PLUS_EXPR:
7431 /* Handle the pointer + int case. */
7432 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7433 return pointer_int_sum (PLUS_EXPR, op0, op1);
7434 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
7435 return pointer_int_sum (PLUS_EXPR, op1, op0);
7436 else
7437 common = 1;
7438 break;
7440 case MINUS_EXPR:
7441 /* Subtraction of two similar pointers.
7442 We must subtract them as integers, then divide by object size. */
7443 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
7444 && comp_target_types (type0, type1, 1))
7445 return pointer_diff (op0, op1);
7446 /* Handle pointer minus int. Just like pointer plus int. */
7447 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7448 return pointer_int_sum (MINUS_EXPR, op0, op1);
7449 else
7450 common = 1;
7451 break;
7453 case MULT_EXPR:
7454 common = 1;
7455 break;
7457 case TRUNC_DIV_EXPR:
7458 case CEIL_DIV_EXPR:
7459 case FLOOR_DIV_EXPR:
7460 case ROUND_DIV_EXPR:
7461 case EXACT_DIV_EXPR:
7462 /* Floating point division by zero is a legitimate way to obtain
7463 infinities and NaNs. */
7464 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7465 warning (0, "division by zero");
7467 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7468 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7469 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7470 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
7472 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
7473 code0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
7474 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
7475 code1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
7477 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
7478 resultcode = RDIV_EXPR;
7479 else
7480 /* Although it would be tempting to shorten always here, that
7481 loses on some targets, since the modulo instruction is
7482 undefined if the quotient can't be represented in the
7483 computation mode. We shorten only if unsigned or if
7484 dividing by something we know != -1. */
7485 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7486 || (TREE_CODE (op1) == INTEGER_CST
7487 && !integer_all_onesp (op1)));
7488 common = 1;
7490 break;
7492 case BIT_AND_EXPR:
7493 case BIT_IOR_EXPR:
7494 case BIT_XOR_EXPR:
7495 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7496 shorten = -1;
7497 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
7498 common = 1;
7499 break;
7501 case TRUNC_MOD_EXPR:
7502 case FLOOR_MOD_EXPR:
7503 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
7504 warning (0, "division by zero");
7506 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7508 /* Although it would be tempting to shorten always here, that loses
7509 on some targets, since the modulo instruction is undefined if the
7510 quotient can't be represented in the computation mode. We shorten
7511 only if unsigned or if dividing by something we know != -1. */
7512 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
7513 || (TREE_CODE (op1) == INTEGER_CST
7514 && !integer_all_onesp (op1)));
7515 common = 1;
7517 break;
7519 case TRUTH_ANDIF_EXPR:
7520 case TRUTH_ORIF_EXPR:
7521 case TRUTH_AND_EXPR:
7522 case TRUTH_OR_EXPR:
7523 case TRUTH_XOR_EXPR:
7524 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
7525 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
7526 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
7527 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
7529 /* Result of these operations is always an int,
7530 but that does not mean the operands should be
7531 converted to ints! */
7532 result_type = integer_type_node;
7533 op0 = c_common_truthvalue_conversion (op0);
7534 op1 = c_common_truthvalue_conversion (op1);
7535 converted = 1;
7537 break;
7539 /* Shift operations: result has same type as first operand;
7540 always convert second operand to int.
7541 Also set SHORT_SHIFT if shifting rightward. */
7543 case RSHIFT_EXPR:
7544 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7546 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7548 if (tree_int_cst_sgn (op1) < 0)
7549 warning (0, "right shift count is negative");
7550 else
7552 if (!integer_zerop (op1))
7553 short_shift = 1;
7555 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7556 warning (0, "right shift count >= width of type");
7560 /* Use the type of the value to be shifted. */
7561 result_type = type0;
7562 /* Convert the shift-count to an integer, regardless of size
7563 of value being shifted. */
7564 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7565 op1 = convert (integer_type_node, op1);
7566 /* Avoid converting op1 to result_type later. */
7567 converted = 1;
7569 break;
7571 case LSHIFT_EXPR:
7572 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
7574 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
7576 if (tree_int_cst_sgn (op1) < 0)
7577 warning (0, "left shift count is negative");
7579 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
7580 warning (0, "left shift count >= width of type");
7583 /* Use the type of the value to be shifted. */
7584 result_type = type0;
7585 /* Convert the shift-count to an integer, regardless of size
7586 of value being shifted. */
7587 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
7588 op1 = convert (integer_type_node, op1);
7589 /* Avoid converting op1 to result_type later. */
7590 converted = 1;
7592 break;
7594 case EQ_EXPR:
7595 case NE_EXPR:
7596 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
7597 warning (0, "comparing floating point with == or != is unsafe");
7598 /* Result of comparison is always int,
7599 but don't convert the args to int! */
7600 build_type = integer_type_node;
7601 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
7602 || code0 == COMPLEX_TYPE)
7603 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
7604 || code1 == COMPLEX_TYPE))
7605 short_compare = 1;
7606 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7608 tree tt0 = TREE_TYPE (type0);
7609 tree tt1 = TREE_TYPE (type1);
7610 /* Anything compares with void *. void * compares with anything.
7611 Otherwise, the targets must be compatible
7612 and both must be object or both incomplete. */
7613 if (comp_target_types (type0, type1, 1))
7614 result_type = common_pointer_type (type0, type1);
7615 else if (VOID_TYPE_P (tt0))
7617 /* op0 != orig_op0 detects the case of something
7618 whose value is 0 but which isn't a valid null ptr const. */
7619 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
7620 && TREE_CODE (tt1) == FUNCTION_TYPE)
7621 pedwarn ("ISO C forbids comparison of %<void *%>"
7622 " with function pointer");
7624 else if (VOID_TYPE_P (tt1))
7626 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
7627 && TREE_CODE (tt0) == FUNCTION_TYPE)
7628 pedwarn ("ISO C forbids comparison of %<void *%>"
7629 " with function pointer");
7631 else
7632 pedwarn ("comparison of distinct pointer types lacks a cast");
7634 if (result_type == NULL_TREE)
7635 result_type = ptr_type_node;
7637 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7638 && integer_zerop (op1))
7639 result_type = type0;
7640 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7641 && integer_zerop (op0))
7642 result_type = type1;
7643 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7645 result_type = type0;
7646 pedwarn ("comparison between pointer and integer");
7648 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7650 result_type = type1;
7651 pedwarn ("comparison between pointer and integer");
7653 break;
7655 case LE_EXPR:
7656 case GE_EXPR:
7657 case LT_EXPR:
7658 case GT_EXPR:
7659 build_type = integer_type_node;
7660 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
7661 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
7662 short_compare = 1;
7663 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
7665 if (comp_target_types (type0, type1, 1))
7667 result_type = common_pointer_type (type0, type1);
7668 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
7669 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
7670 pedwarn ("comparison of complete and incomplete pointers");
7671 else if (pedantic
7672 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
7673 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
7675 else
7677 result_type = ptr_type_node;
7678 pedwarn ("comparison of distinct pointer types lacks a cast");
7681 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
7682 && integer_zerop (op1))
7684 result_type = type0;
7685 if (pedantic || extra_warnings)
7686 pedwarn ("ordered comparison of pointer with integer zero");
7688 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
7689 && integer_zerop (op0))
7691 result_type = type1;
7692 if (pedantic)
7693 pedwarn ("ordered comparison of pointer with integer zero");
7695 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
7697 result_type = type0;
7698 pedwarn ("comparison between pointer and integer");
7700 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
7702 result_type = type1;
7703 pedwarn ("comparison between pointer and integer");
7705 break;
7707 default:
7708 gcc_unreachable ();
7711 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
7712 return error_mark_node;
7714 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
7715 || code0 == VECTOR_TYPE)
7717 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
7718 || code1 == VECTOR_TYPE))
7720 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
7722 if (shorten || common || short_compare)
7723 result_type = c_common_type (type0, type1);
7725 /* For certain operations (which identify themselves by shorten != 0)
7726 if both args were extended from the same smaller type,
7727 do the arithmetic in that type and then extend.
7729 shorten !=0 and !=1 indicates a bitwise operation.
7730 For them, this optimization is safe only if
7731 both args are zero-extended or both are sign-extended.
7732 Otherwise, we might change the result.
7733 Eg, (short)-1 | (unsigned short)-1 is (int)-1
7734 but calculated in (unsigned short) it would be (unsigned short)-1. */
7736 if (shorten && none_complex)
7738 int unsigned0, unsigned1;
7739 tree arg0 = get_narrower (op0, &unsigned0);
7740 tree arg1 = get_narrower (op1, &unsigned1);
7741 /* UNS is 1 if the operation to be done is an unsigned one. */
7742 int uns = TYPE_UNSIGNED (result_type);
7743 tree type;
7745 final_type = result_type;
7747 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
7748 but it *requires* conversion to FINAL_TYPE. */
7750 if ((TYPE_PRECISION (TREE_TYPE (op0))
7751 == TYPE_PRECISION (TREE_TYPE (arg0)))
7752 && TREE_TYPE (op0) != final_type)
7753 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
7754 if ((TYPE_PRECISION (TREE_TYPE (op1))
7755 == TYPE_PRECISION (TREE_TYPE (arg1)))
7756 && TREE_TYPE (op1) != final_type)
7757 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
7759 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
7761 /* For bitwise operations, signedness of nominal type
7762 does not matter. Consider only how operands were extended. */
7763 if (shorten == -1)
7764 uns = unsigned0;
7766 /* Note that in all three cases below we refrain from optimizing
7767 an unsigned operation on sign-extended args.
7768 That would not be valid. */
7770 /* Both args variable: if both extended in same way
7771 from same width, do it in that width.
7772 Do it unsigned if args were zero-extended. */
7773 if ((TYPE_PRECISION (TREE_TYPE (arg0))
7774 < TYPE_PRECISION (result_type))
7775 && (TYPE_PRECISION (TREE_TYPE (arg1))
7776 == TYPE_PRECISION (TREE_TYPE (arg0)))
7777 && unsigned0 == unsigned1
7778 && (unsigned0 || !uns))
7779 result_type
7780 = c_common_signed_or_unsigned_type
7781 (unsigned0, c_common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
7782 else if (TREE_CODE (arg0) == INTEGER_CST
7783 && (unsigned1 || !uns)
7784 && (TYPE_PRECISION (TREE_TYPE (arg1))
7785 < TYPE_PRECISION (result_type))
7786 && (type
7787 = c_common_signed_or_unsigned_type (unsigned1,
7788 TREE_TYPE (arg1)),
7789 int_fits_type_p (arg0, type)))
7790 result_type = type;
7791 else if (TREE_CODE (arg1) == INTEGER_CST
7792 && (unsigned0 || !uns)
7793 && (TYPE_PRECISION (TREE_TYPE (arg0))
7794 < TYPE_PRECISION (result_type))
7795 && (type
7796 = c_common_signed_or_unsigned_type (unsigned0,
7797 TREE_TYPE (arg0)),
7798 int_fits_type_p (arg1, type)))
7799 result_type = type;
7802 /* Shifts can be shortened if shifting right. */
7804 if (short_shift)
7806 int unsigned_arg;
7807 tree arg0 = get_narrower (op0, &unsigned_arg);
7809 final_type = result_type;
7811 if (arg0 == op0 && final_type == TREE_TYPE (op0))
7812 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
7814 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
7815 /* We can shorten only if the shift count is less than the
7816 number of bits in the smaller type size. */
7817 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
7818 /* We cannot drop an unsigned shift after sign-extension. */
7819 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
7821 /* Do an unsigned shift if the operand was zero-extended. */
7822 result_type
7823 = c_common_signed_or_unsigned_type (unsigned_arg,
7824 TREE_TYPE (arg0));
7825 /* Convert value-to-be-shifted to that type. */
7826 if (TREE_TYPE (op0) != result_type)
7827 op0 = convert (result_type, op0);
7828 converted = 1;
7832 /* Comparison operations are shortened too but differently.
7833 They identify themselves by setting short_compare = 1. */
7835 if (short_compare)
7837 /* Don't write &op0, etc., because that would prevent op0
7838 from being kept in a register.
7839 Instead, make copies of the our local variables and
7840 pass the copies by reference, then copy them back afterward. */
7841 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7842 enum tree_code xresultcode = resultcode;
7843 tree val
7844 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7846 if (val != 0)
7847 return val;
7849 op0 = xop0, op1 = xop1;
7850 converted = 1;
7851 resultcode = xresultcode;
7853 if (warn_sign_compare && skip_evaluation == 0)
7855 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
7856 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
7857 int unsignedp0, unsignedp1;
7858 tree primop0 = get_narrower (op0, &unsignedp0);
7859 tree primop1 = get_narrower (op1, &unsignedp1);
7861 xop0 = orig_op0;
7862 xop1 = orig_op1;
7863 STRIP_TYPE_NOPS (xop0);
7864 STRIP_TYPE_NOPS (xop1);
7866 /* Give warnings for comparisons between signed and unsigned
7867 quantities that may fail.
7869 Do the checking based on the original operand trees, so that
7870 casts will be considered, but default promotions won't be.
7872 Do not warn if the comparison is being done in a signed type,
7873 since the signed type will only be chosen if it can represent
7874 all the values of the unsigned type. */
7875 if (!TYPE_UNSIGNED (result_type))
7876 /* OK */;
7877 /* Do not warn if both operands are the same signedness. */
7878 else if (op0_signed == op1_signed)
7879 /* OK */;
7880 else
7882 tree sop, uop;
7884 if (op0_signed)
7885 sop = xop0, uop = xop1;
7886 else
7887 sop = xop1, uop = xop0;
7889 /* Do not warn if the signed quantity is an
7890 unsuffixed integer literal (or some static
7891 constant expression involving such literals or a
7892 conditional expression involving such literals)
7893 and it is non-negative. */
7894 if (tree_expr_nonnegative_p (sop))
7895 /* OK */;
7896 /* Do not warn if the comparison is an equality operation,
7897 the unsigned quantity is an integral constant, and it
7898 would fit in the result if the result were signed. */
7899 else if (TREE_CODE (uop) == INTEGER_CST
7900 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7901 && int_fits_type_p
7902 (uop, c_common_signed_type (result_type)))
7903 /* OK */;
7904 /* Do not warn if the unsigned quantity is an enumeration
7905 constant and its maximum value would fit in the result
7906 if the result were signed. */
7907 else if (TREE_CODE (uop) == INTEGER_CST
7908 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7909 && int_fits_type_p
7910 (TYPE_MAX_VALUE (TREE_TYPE (uop)),
7911 c_common_signed_type (result_type)))
7912 /* OK */;
7913 else
7914 warning (0, "comparison between signed and unsigned");
7917 /* Warn if two unsigned values are being compared in a size
7918 larger than their original size, and one (and only one) is the
7919 result of a `~' operator. This comparison will always fail.
7921 Also warn if one operand is a constant, and the constant
7922 does not have all bits set that are set in the ~ operand
7923 when it is extended. */
7925 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7926 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7928 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7929 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7930 &unsignedp0);
7931 else
7932 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7933 &unsignedp1);
7935 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7937 tree primop;
7938 HOST_WIDE_INT constant, mask;
7939 int unsignedp, bits;
7941 if (host_integerp (primop0, 0))
7943 primop = primop1;
7944 unsignedp = unsignedp1;
7945 constant = tree_low_cst (primop0, 0);
7947 else
7949 primop = primop0;
7950 unsignedp = unsignedp0;
7951 constant = tree_low_cst (primop1, 0);
7954 bits = TYPE_PRECISION (TREE_TYPE (primop));
7955 if (bits < TYPE_PRECISION (result_type)
7956 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7958 mask = (~(HOST_WIDE_INT) 0) << bits;
7959 if ((mask & constant) != mask)
7960 warning (0, "comparison of promoted ~unsigned with constant");
7963 else if (unsignedp0 && unsignedp1
7964 && (TYPE_PRECISION (TREE_TYPE (primop0))
7965 < TYPE_PRECISION (result_type))
7966 && (TYPE_PRECISION (TREE_TYPE (primop1))
7967 < TYPE_PRECISION (result_type)))
7968 warning (0, "comparison of promoted ~unsigned with unsigned");
7974 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7975 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7976 Then the expression will be built.
7977 It will be given type FINAL_TYPE if that is nonzero;
7978 otherwise, it will be given type RESULT_TYPE. */
7980 if (!result_type)
7982 binary_op_error (code);
7983 return error_mark_node;
7986 if (!converted)
7988 if (TREE_TYPE (op0) != result_type)
7989 op0 = convert (result_type, op0);
7990 if (TREE_TYPE (op1) != result_type)
7991 op1 = convert (result_type, op1);
7993 /* This can happen if one operand has a vector type, and the other
7994 has a different type. */
7995 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
7996 return error_mark_node;
7999 if (build_type == NULL_TREE)
8000 build_type = result_type;
8003 tree result = build2 (resultcode, build_type, op0, op1);
8005 /* Treat expressions in initializers specially as they can't trap. */
8006 result = require_constant_value ? fold_initializer (result)
8007 : fold (result);
8009 if (final_type != 0)
8010 result = convert (final_type, result);
8011 return result;
8016 /* Convert EXPR to be a truth-value, validating its type for this
8017 purpose. Passes EXPR to default_function_array_conversion. */
8019 tree
8020 c_objc_common_truthvalue_conversion (tree expr)
8022 expr = default_function_array_conversion (expr);
8023 switch (TREE_CODE (TREE_TYPE (expr)))
8025 case ARRAY_TYPE:
8026 error ("used array that cannot be converted to pointer where scalar is required");
8027 return error_mark_node;
8029 case RECORD_TYPE:
8030 error ("used struct type value where scalar is required");
8031 return error_mark_node;
8033 case UNION_TYPE:
8034 error ("used union type value where scalar is required");
8035 return error_mark_node;
8037 default:
8038 break;
8041 /* ??? Should we also give an error for void and vectors rather than
8042 leaving those to give errors later? */
8043 return c_common_truthvalue_conversion (expr);