PR c/12373
[official-gcc.git] / gcc / c-typeck.c
blob1cfc8039244fe0cfcc35226da4dd386168b1d65f
1 /* Build expressions with type checking for C compiler.
2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 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 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "rtl.h"
37 #include "tree.h"
38 #include "langhooks.h"
39 #include "c-tree.h"
40 #include "tm_p.h"
41 #include "flags.h"
42 #include "output.h"
43 #include "expr.h"
44 #include "toplev.h"
45 #include "intl.h"
46 #include "ggc.h"
47 #include "target.h"
49 /* Nonzero if we've already printed a "missing braces around initializer"
50 message within this initializer. */
51 static int missing_braces_mentioned;
53 static tree qualify_type (tree, tree);
54 static int same_translation_unit_p (tree, tree);
55 static int tagged_types_tu_compatible_p (tree, tree, int);
56 static int comp_target_types (tree, tree, int);
57 static int function_types_compatible_p (tree, tree, int);
58 static int type_lists_compatible_p (tree, tree, int);
59 static tree decl_constant_value_for_broken_optimization (tree);
60 static tree default_function_array_conversion (tree);
61 static tree lookup_field (tree, tree);
62 static tree convert_arguments (tree, tree, tree, tree);
63 static tree pointer_diff (tree, tree);
64 static tree internal_build_compound_expr (tree, int);
65 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
66 int);
67 static void warn_for_assignment (const char *, const char *, tree, int);
68 static tree valid_compound_expr_initializer (tree, tree);
69 static void push_string (const char *);
70 static void push_member_name (tree);
71 static void push_array_bounds (int);
72 static int spelling_length (void);
73 static char *print_spelling (char *);
74 static void warning_init (const char *);
75 static tree digest_init (tree, tree, int);
76 static void output_init_element (tree, tree, tree, int);
77 static void output_pending_init_elements (int);
78 static int set_designator (int);
79 static void push_range_stack (tree);
80 static void add_pending_init (tree, tree);
81 static void set_nonincremental_init (void);
82 static void set_nonincremental_init_from_string (tree);
83 static tree find_init_member (tree);
85 /* Do `exp = require_complete_type (exp);' to make sure exp
86 does not have an incomplete type. (That includes void types.) */
88 tree
89 require_complete_type (tree value)
91 tree type = TREE_TYPE (value);
93 if (value == error_mark_node || type == error_mark_node)
94 return error_mark_node;
96 /* First, detect a valid value with a complete type. */
97 if (COMPLETE_TYPE_P (type))
98 return value;
100 c_incomplete_type_error (value, type);
101 return error_mark_node;
104 /* Print an error message for invalid use of an incomplete type.
105 VALUE is the expression that was used (or 0 if that isn't known)
106 and TYPE is the type that was invalid. */
108 void
109 c_incomplete_type_error (tree value, tree type)
111 const char *type_code_string;
113 /* Avoid duplicate error message. */
114 if (TREE_CODE (type) == ERROR_MARK)
115 return;
117 if (value != 0 && (TREE_CODE (value) == VAR_DECL
118 || TREE_CODE (value) == PARM_DECL))
119 error ("`%s' has an incomplete type",
120 IDENTIFIER_POINTER (DECL_NAME (value)));
121 else
123 retry:
124 /* We must print an error message. Be clever about what it says. */
126 switch (TREE_CODE (type))
128 case RECORD_TYPE:
129 type_code_string = "struct";
130 break;
132 case UNION_TYPE:
133 type_code_string = "union";
134 break;
136 case ENUMERAL_TYPE:
137 type_code_string = "enum";
138 break;
140 case VOID_TYPE:
141 error ("invalid use of void expression");
142 return;
144 case ARRAY_TYPE:
145 if (TYPE_DOMAIN (type))
147 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
149 error ("invalid use of flexible array member");
150 return;
152 type = TREE_TYPE (type);
153 goto retry;
155 error ("invalid use of array with unspecified bounds");
156 return;
158 default:
159 abort ();
162 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
163 error ("invalid use of undefined type `%s %s'",
164 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
165 else
166 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
167 error ("invalid use of incomplete typedef `%s'",
168 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
172 /* Given a type, apply default promotions wrt unnamed function
173 arguments and return the new type. */
175 tree
176 c_type_promotes_to (tree type)
178 if (TYPE_MAIN_VARIANT (type) == float_type_node)
179 return double_type_node;
181 if (c_promoting_integer_type_p (type))
183 /* Preserve unsignedness if not really getting any wider. */
184 if (TREE_UNSIGNED (type)
185 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
186 return unsigned_type_node;
187 return integer_type_node;
190 return type;
193 /* Return a variant of TYPE which has all the type qualifiers of LIKE
194 as well as those of TYPE. */
196 static tree
197 qualify_type (tree type, tree like)
199 return c_build_qualified_type (type,
200 TYPE_QUALS (type) | TYPE_QUALS (like));
203 /* Return the common type of two types.
204 We assume that comptypes has already been done and returned 1;
205 if that isn't so, this may crash. In particular, we assume that qualifiers
206 match.
208 This is the type for the result of most arithmetic operations
209 if the operands have the given two types. */
211 tree
212 common_type (tree t1, tree t2)
214 enum tree_code code1;
215 enum tree_code code2;
216 tree attributes;
218 /* Save time if the two types are the same. */
220 if (t1 == t2) return t1;
222 /* If one type is nonsense, use the other. */
223 if (t1 == error_mark_node)
224 return t2;
225 if (t2 == error_mark_node)
226 return t1;
228 /* Merge the attributes. */
229 attributes = targetm.merge_type_attributes (t1, t2);
231 /* Treat an enum type as the unsigned integer type of the same width. */
233 if (TREE_CODE (t1) == ENUMERAL_TYPE)
234 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
235 if (TREE_CODE (t2) == ENUMERAL_TYPE)
236 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
238 code1 = TREE_CODE (t1);
239 code2 = TREE_CODE (t2);
241 /* If one type is complex, form the common type of the non-complex
242 components, then make that complex. Use T1 or T2 if it is the
243 required type. */
244 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
246 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
247 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
248 tree subtype = common_type (subtype1, subtype2);
250 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
251 return build_type_attribute_variant (t1, attributes);
252 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
253 return build_type_attribute_variant (t2, attributes);
254 else
255 return build_type_attribute_variant (build_complex_type (subtype),
256 attributes);
259 switch (code1)
261 case INTEGER_TYPE:
262 case REAL_TYPE:
263 /* If only one is real, use it as the result. */
265 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
266 return build_type_attribute_variant (t1, attributes);
268 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
269 return build_type_attribute_variant (t2, attributes);
271 /* Both real or both integers; use the one with greater precision. */
273 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
274 return build_type_attribute_variant (t1, attributes);
275 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
276 return build_type_attribute_variant (t2, attributes);
278 /* Same precision. Prefer longs to ints even when same size. */
280 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
281 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
282 return build_type_attribute_variant (long_unsigned_type_node,
283 attributes);
285 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
286 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
288 /* But preserve unsignedness from the other type,
289 since long cannot hold all the values of an unsigned int. */
290 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
291 t1 = long_unsigned_type_node;
292 else
293 t1 = long_integer_type_node;
294 return build_type_attribute_variant (t1, attributes);
297 /* Likewise, prefer long double to double even if same size. */
298 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
299 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
300 return build_type_attribute_variant (long_double_type_node,
301 attributes);
303 /* Otherwise prefer the unsigned one. */
305 if (TREE_UNSIGNED (t1))
306 return build_type_attribute_variant (t1, attributes);
307 else
308 return build_type_attribute_variant (t2, attributes);
310 case POINTER_TYPE:
311 /* For two pointers, do this recursively on the target type,
312 and combine the qualifiers of the two types' targets. */
313 /* This code was turned off; I don't know why.
314 But ANSI C specifies doing this with the qualifiers.
315 So I turned it on again. */
317 tree pointed_to_1 = TREE_TYPE (t1);
318 tree pointed_to_2 = TREE_TYPE (t2);
319 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
320 TYPE_MAIN_VARIANT (pointed_to_2));
321 t1 = build_pointer_type (c_build_qualified_type
322 (target,
323 TYPE_QUALS (pointed_to_1) |
324 TYPE_QUALS (pointed_to_2)));
325 return build_type_attribute_variant (t1, attributes);
328 case ARRAY_TYPE:
330 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
331 /* Save space: see if the result is identical to one of the args. */
332 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
333 return build_type_attribute_variant (t1, attributes);
334 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
335 return build_type_attribute_variant (t2, attributes);
336 /* Merge the element types, and have a size if either arg has one. */
337 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
338 return build_type_attribute_variant (t1, attributes);
341 case FUNCTION_TYPE:
342 /* Function types: prefer the one that specified arg types.
343 If both do, merge the arg types. Also merge the return types. */
345 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
346 tree p1 = TYPE_ARG_TYPES (t1);
347 tree p2 = TYPE_ARG_TYPES (t2);
348 int len;
349 tree newargs, n;
350 int i;
352 /* Save space: see if the result is identical to one of the args. */
353 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
354 return build_type_attribute_variant (t1, attributes);
355 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
356 return build_type_attribute_variant (t2, attributes);
358 /* Simple way if one arg fails to specify argument types. */
359 if (TYPE_ARG_TYPES (t1) == 0)
361 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
362 return build_type_attribute_variant (t1, attributes);
364 if (TYPE_ARG_TYPES (t2) == 0)
366 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
367 return build_type_attribute_variant (t1, attributes);
370 /* If both args specify argument types, we must merge the two
371 lists, argument by argument. */
373 pushlevel (0);
374 declare_parm_level ();
376 len = list_length (p1);
377 newargs = 0;
379 for (i = 0; i < len; i++)
380 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
382 n = newargs;
384 for (; p1;
385 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
387 /* A null type means arg type is not specified.
388 Take whatever the other function type has. */
389 if (TREE_VALUE (p1) == 0)
391 TREE_VALUE (n) = TREE_VALUE (p2);
392 goto parm_done;
394 if (TREE_VALUE (p2) == 0)
396 TREE_VALUE (n) = TREE_VALUE (p1);
397 goto parm_done;
400 /* Given wait (union {union wait *u; int *i} *)
401 and wait (union wait *),
402 prefer union wait * as type of parm. */
403 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
404 && TREE_VALUE (p1) != TREE_VALUE (p2))
406 tree memb;
407 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
408 memb; memb = TREE_CHAIN (memb))
409 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
410 COMPARE_STRICT))
412 TREE_VALUE (n) = TREE_VALUE (p2);
413 if (pedantic)
414 pedwarn ("function types not truly compatible in ISO C");
415 goto parm_done;
418 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
419 && TREE_VALUE (p2) != TREE_VALUE (p1))
421 tree memb;
422 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
423 memb; memb = TREE_CHAIN (memb))
424 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
425 COMPARE_STRICT))
427 TREE_VALUE (n) = TREE_VALUE (p1);
428 if (pedantic)
429 pedwarn ("function types not truly compatible in ISO C");
430 goto parm_done;
433 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
434 parm_done: ;
437 poplevel (0, 0, 0);
439 t1 = build_function_type (valtype, newargs);
440 /* ... falls through ... */
443 default:
444 return build_type_attribute_variant (t1, attributes);
449 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
450 or various other operations. Return 2 if they are compatible
451 but a warning may be needed if you use them together. */
454 comptypes (tree type1, tree type2, int flags)
456 tree t1 = type1;
457 tree t2 = type2;
458 int attrval, val;
460 /* Suppress errors caused by previously reported errors. */
462 if (t1 == t2 || !t1 || !t2
463 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
464 return 1;
466 /* If either type is the internal version of sizetype, return the
467 language version. */
468 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
469 && TYPE_DOMAIN (t1) != 0)
470 t1 = TYPE_DOMAIN (t1);
472 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
473 && TYPE_DOMAIN (t2) != 0)
474 t2 = TYPE_DOMAIN (t2);
476 /* Enumerated types are compatible with integer types, but this is
477 not transitive: two enumerated types in the same translation unit
478 are compatible with each other only if they are the same type. */
480 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
481 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
482 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
483 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
485 if (t1 == t2)
486 return 1;
488 /* Different classes of types can't be compatible. */
490 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
492 /* Qualifiers must match. */
494 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
495 return 0;
497 /* Allow for two different type nodes which have essentially the same
498 definition. Note that we already checked for equality of the type
499 qualifiers (just above). */
501 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
502 return 1;
504 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
505 if (! (attrval = targetm.comp_type_attributes (t1, t2)))
506 return 0;
508 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
509 val = 0;
511 switch (TREE_CODE (t1))
513 case POINTER_TYPE:
514 /* We must give ObjC the first crack at comparing pointers, since
515 protocol qualifiers may be involved. */
516 if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
517 break;
518 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
519 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
520 break;
522 case FUNCTION_TYPE:
523 val = function_types_compatible_p (t1, t2, flags);
524 break;
526 case ARRAY_TYPE:
528 tree d1 = TYPE_DOMAIN (t1);
529 tree d2 = TYPE_DOMAIN (t2);
530 bool d1_variable, d2_variable;
531 bool d1_zero, d2_zero;
532 val = 1;
534 /* Target types must match incl. qualifiers. */
535 if (TREE_TYPE (t1) != TREE_TYPE (t2)
536 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
537 flags)))
538 return 0;
540 /* Sizes must match unless one is missing or variable. */
541 if (d1 == 0 || d2 == 0 || d1 == d2)
542 break;
544 d1_zero = ! TYPE_MAX_VALUE (d1);
545 d2_zero = ! TYPE_MAX_VALUE (d2);
547 d1_variable = (! d1_zero
548 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
549 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
550 d2_variable = (! d2_zero
551 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
552 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
554 if (d1_variable || d2_variable)
555 break;
556 if (d1_zero && d2_zero)
557 break;
558 if (d1_zero || d2_zero
559 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
560 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
561 val = 0;
563 break;
566 case RECORD_TYPE:
567 /* We are dealing with two distinct structs. In assorted Objective-C
568 corner cases, however, these can still be deemed equivalent. */
569 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
570 val = 1;
572 case ENUMERAL_TYPE:
573 case UNION_TYPE:
574 if (val != 1 && !same_translation_unit_p (t1, t2))
575 val = tagged_types_tu_compatible_p (t1, t2, flags);
576 break;
578 case VECTOR_TYPE:
579 /* The target might allow certain vector types to be compatible. */
580 val = targetm.vector_opaque_p (t1)
581 || targetm.vector_opaque_p (t2)
582 || TYPE_MODE (t1) == TYPE_MODE (t2);
583 break;
585 default:
586 break;
588 return attrval == 2 && val == 1 ? 2 : val;
591 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
592 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
593 to 1 or 0 depending if the check of the pointer types is meant to
594 be reflexive or not (typically, assignments are not reflexive,
595 while comparisons are reflexive).
598 static int
599 comp_target_types (tree ttl, tree ttr, int reflexive)
601 int val;
603 /* Give objc_comptypes a crack at letting these types through. */
604 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
605 return val;
607 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
608 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
610 if (val == 2 && pedantic)
611 pedwarn ("types are not quite compatible");
612 return val;
615 /* Subroutines of `comptypes'. */
617 /* Determine whether two types derive from the same translation unit.
618 If the CONTEXT chain ends in a null, that type's context is still
619 being parsed, so if two types have context chains ending in null,
620 they're in the same translation unit. */
621 static int
622 same_translation_unit_p (tree t1, tree t2)
624 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
625 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
627 case 'd': t1 = DECL_CONTEXT (t1); break;
628 case 't': t1 = TYPE_CONTEXT (t1); break;
629 case 'b': t1 = BLOCK_SUPERCONTEXT (t1); break;
630 default: abort ();
633 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
634 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
636 case 'd': t2 = DECL_CONTEXT (t1); break;
637 case 't': t2 = TYPE_CONTEXT (t2); break;
638 case 'b': t2 = BLOCK_SUPERCONTEXT (t2); break;
639 default: abort ();
642 return t1 == t2;
645 /* The C standard says that two structures in different translation
646 units are compatible with each other only if the types of their
647 fields are compatible (among other things). So, consider two copies
648 of this structure: */
650 struct tagged_tu_seen {
651 const struct tagged_tu_seen * next;
652 tree t1;
653 tree t2;
656 /* Can they be compatible with each other? We choose to break the
657 recursion by allowing those types to be compatible. */
659 static const struct tagged_tu_seen * tagged_tu_seen_base;
661 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
662 compatible. If the two types are not the same (which has been
663 checked earlier), this can only happen when multiple translation
664 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
665 rules. */
667 static int
668 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
670 tree s1, s2;
671 bool needs_warning = false;
673 /* We have to verify that the tags of the types are the same. This
674 is harder than it looks because this may be a typedef, so we have
675 to go look at the original type. It may even be a typedef of a
676 typedef... */
677 while (TYPE_NAME (t1)
678 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
679 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
680 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
682 while (TYPE_NAME (t2)
683 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
684 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
685 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
687 /* C90 didn't have the requirement that the two tags be the same. */
688 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
689 return 0;
691 /* C90 didn't say what happened if one or both of the types were
692 incomplete; we choose to follow C99 rules here, which is that they
693 are compatible. */
694 if (TYPE_SIZE (t1) == NULL
695 || TYPE_SIZE (t2) == NULL)
696 return 1;
699 const struct tagged_tu_seen * tts_i;
700 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
701 if (tts_i->t1 == t1 && tts_i->t2 == t2)
702 return 1;
705 switch (TREE_CODE (t1))
707 case ENUMERAL_TYPE:
710 /* Speed up the case where the type values are in the same order. */
711 tree tv1 = TYPE_VALUES (t1);
712 tree tv2 = TYPE_VALUES (t2);
714 if (tv1 == tv2)
715 return 1;
717 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
719 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
720 break;
721 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
722 return 0;
725 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
726 return 1;
727 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
728 return 0;
730 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
731 return 0;
733 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
735 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
736 if (s2 == NULL
737 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
738 return 0;
740 return 1;
743 case UNION_TYPE:
745 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
746 return 0;
748 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
750 bool ok = false;
751 struct tagged_tu_seen tts;
753 tts.next = tagged_tu_seen_base;
754 tts.t1 = t1;
755 tts.t2 = t2;
756 tagged_tu_seen_base = &tts;
758 if (DECL_NAME (s1) != NULL)
759 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
760 if (DECL_NAME (s1) == DECL_NAME (s2))
762 int result;
763 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
764 if (result == 0)
765 break;
766 if (result == 2)
767 needs_warning = true;
769 if (TREE_CODE (s1) == FIELD_DECL
770 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
771 DECL_FIELD_BIT_OFFSET (s2)) != 1)
772 break;
774 ok = true;
775 break;
777 tagged_tu_seen_base = tts.next;
778 if (! ok)
779 return 0;
781 return needs_warning ? 2 : 1;
784 case RECORD_TYPE:
786 struct tagged_tu_seen tts;
788 tts.next = tagged_tu_seen_base;
789 tts.t1 = t1;
790 tts.t2 = t2;
791 tagged_tu_seen_base = &tts;
793 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
794 s1 && s2;
795 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
797 int result;
798 if (TREE_CODE (s1) != TREE_CODE (s2)
799 || DECL_NAME (s1) != DECL_NAME (s2))
800 break;
801 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
802 if (result == 0)
803 break;
804 if (result == 2)
805 needs_warning = true;
807 if (TREE_CODE (s1) == FIELD_DECL
808 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
809 DECL_FIELD_BIT_OFFSET (s2)) != 1)
810 break;
812 tagged_tu_seen_base = tts.next;
813 if (s1 && s2)
814 return 0;
815 return needs_warning ? 2 : 1;
818 default:
819 abort ();
823 /* Return 1 if two function types F1 and F2 are compatible.
824 If either type specifies no argument types,
825 the other must specify a fixed number of self-promoting arg types.
826 Otherwise, if one type specifies only the number of arguments,
827 the other must specify that number of self-promoting arg types.
828 Otherwise, the argument types must match. */
830 static int
831 function_types_compatible_p (tree f1, tree f2, int flags)
833 tree args1, args2;
834 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
835 int val = 1;
836 int val1;
837 tree ret1, ret2;
839 ret1 = TREE_TYPE (f1);
840 ret2 = TREE_TYPE (f2);
842 /* 'volatile' qualifiers on a function's return type mean the function
843 is noreturn. */
844 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
845 pedwarn ("function return types not compatible due to `volatile'");
846 if (TYPE_VOLATILE (ret1))
847 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
848 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
849 if (TYPE_VOLATILE (ret2))
850 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
851 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
852 val = comptypes (ret1, ret2, flags);
853 if (val == 0)
854 return 0;
856 args1 = TYPE_ARG_TYPES (f1);
857 args2 = TYPE_ARG_TYPES (f2);
859 /* An unspecified parmlist matches any specified parmlist
860 whose argument types don't need default promotions. */
862 if (args1 == 0)
864 if (!self_promoting_args_p (args2))
865 return 0;
866 /* If one of these types comes from a non-prototype fn definition,
867 compare that with the other type's arglist.
868 If they don't match, ask for a warning (but no error). */
869 if (TYPE_ACTUAL_ARG_TYPES (f1)
870 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
871 flags))
872 val = 2;
873 return val;
875 if (args2 == 0)
877 if (!self_promoting_args_p (args1))
878 return 0;
879 if (TYPE_ACTUAL_ARG_TYPES (f2)
880 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
881 flags))
882 val = 2;
883 return val;
886 /* Both types have argument lists: compare them and propagate results. */
887 val1 = type_lists_compatible_p (args1, args2, flags);
888 return val1 != 1 ? val1 : val;
891 /* Check two lists of types for compatibility,
892 returning 0 for incompatible, 1 for compatible,
893 or 2 for compatible with warning. */
895 static int
896 type_lists_compatible_p (tree args1, tree args2, int flags)
898 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
899 int val = 1;
900 int newval = 0;
902 while (1)
904 if (args1 == 0 && args2 == 0)
905 return val;
906 /* If one list is shorter than the other,
907 they fail to match. */
908 if (args1 == 0 || args2 == 0)
909 return 0;
910 /* A null pointer instead of a type
911 means there is supposed to be an argument
912 but nothing is specified about what type it has.
913 So match anything that self-promotes. */
914 if (TREE_VALUE (args1) == 0)
916 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
917 return 0;
919 else if (TREE_VALUE (args2) == 0)
921 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
922 return 0;
924 /* If one of the lists has an error marker, ignore this arg. */
925 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
926 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
928 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
929 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
930 flags)))
932 /* Allow wait (union {union wait *u; int *i} *)
933 and wait (union wait *) to be compatible. */
934 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
935 && (TYPE_NAME (TREE_VALUE (args1)) == 0
936 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
937 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
938 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
939 TYPE_SIZE (TREE_VALUE (args2))))
941 tree memb;
942 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
943 memb; memb = TREE_CHAIN (memb))
944 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
945 flags))
946 break;
947 if (memb == 0)
948 return 0;
950 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
951 && (TYPE_NAME (TREE_VALUE (args2)) == 0
952 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
953 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
954 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
955 TYPE_SIZE (TREE_VALUE (args1))))
957 tree memb;
958 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
959 memb; memb = TREE_CHAIN (memb))
960 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
961 flags))
962 break;
963 if (memb == 0)
964 return 0;
966 else
967 return 0;
970 /* comptypes said ok, but record if it said to warn. */
971 if (newval > val)
972 val = newval;
974 args1 = TREE_CHAIN (args1);
975 args2 = TREE_CHAIN (args2);
979 /* Compute the size to increment a pointer by. */
981 tree
982 c_size_in_bytes (tree type)
984 enum tree_code code = TREE_CODE (type);
986 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
987 return size_one_node;
989 if (!COMPLETE_OR_VOID_TYPE_P (type))
991 error ("arithmetic on pointer to an incomplete type");
992 return size_one_node;
995 /* Convert in case a char is more than one unit. */
996 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
997 size_int (TYPE_PRECISION (char_type_node)
998 / BITS_PER_UNIT));
1001 /* Return either DECL or its known constant value (if it has one). */
1003 tree
1004 decl_constant_value (tree decl)
1006 if (/* Don't change a variable array bound or initial value to a constant
1007 in a place where a variable is invalid. */
1008 current_function_decl != 0
1009 && ! TREE_THIS_VOLATILE (decl)
1010 && TREE_READONLY (decl)
1011 && DECL_INITIAL (decl) != 0
1012 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1013 /* This is invalid if initial value is not constant.
1014 If it has either a function call, a memory reference,
1015 or a variable, then re-evaluating it could give different results. */
1016 && TREE_CONSTANT (DECL_INITIAL (decl))
1017 /* Check for cases where this is sub-optimal, even though valid. */
1018 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1019 return DECL_INITIAL (decl);
1020 return decl;
1023 /* Return either DECL or its known constant value (if it has one), but
1024 return DECL if pedantic or DECL has mode BLKmode. This is for
1025 bug-compatibility with the old behavior of decl_constant_value
1026 (before GCC 3.0); every use of this function is a bug and it should
1027 be removed before GCC 3.1. It is not appropriate to use pedantic
1028 in a way that affects optimization, and BLKmode is probably not the
1029 right test for avoiding misoptimizations either. */
1031 static tree
1032 decl_constant_value_for_broken_optimization (tree decl)
1034 if (pedantic || DECL_MODE (decl) == BLKmode)
1035 return decl;
1036 else
1037 return decl_constant_value (decl);
1041 /* Perform the default conversion of arrays and functions to pointers.
1042 Return the result of converting EXP. For any other expression, just
1043 return EXP. */
1045 static tree
1046 default_function_array_conversion (tree exp)
1048 tree orig_exp;
1049 tree type = TREE_TYPE (exp);
1050 enum tree_code code = TREE_CODE (type);
1051 int not_lvalue = 0;
1053 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1054 an lvalue.
1056 Do not use STRIP_NOPS here! It will remove conversions from pointer
1057 to integer and cause infinite recursion. */
1058 orig_exp = exp;
1059 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1060 || (TREE_CODE (exp) == NOP_EXPR
1061 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1063 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1064 not_lvalue = 1;
1065 exp = TREE_OPERAND (exp, 0);
1068 /* Preserve the original expression code. */
1069 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1070 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1072 if (code == FUNCTION_TYPE)
1074 return build_unary_op (ADDR_EXPR, exp, 0);
1076 if (code == ARRAY_TYPE)
1078 tree adr;
1079 tree restype = TREE_TYPE (type);
1080 tree ptrtype;
1081 int constp = 0;
1082 int volatilep = 0;
1083 int lvalue_array_p;
1085 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1087 constp = TREE_READONLY (exp);
1088 volatilep = TREE_THIS_VOLATILE (exp);
1091 if (TYPE_QUALS (type) || constp || volatilep)
1092 restype
1093 = c_build_qualified_type (restype,
1094 TYPE_QUALS (type)
1095 | (constp * TYPE_QUAL_CONST)
1096 | (volatilep * TYPE_QUAL_VOLATILE));
1098 if (TREE_CODE (exp) == INDIRECT_REF)
1099 return convert (TYPE_POINTER_TO (restype),
1100 TREE_OPERAND (exp, 0));
1102 if (TREE_CODE (exp) == COMPOUND_EXPR)
1104 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1105 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1106 TREE_OPERAND (exp, 0), op1);
1109 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1110 if (!flag_isoc99 && !lvalue_array_p)
1112 /* Before C99, non-lvalue arrays do not decay to pointers.
1113 Normally, using such an array would be invalid; but it can
1114 be used correctly inside sizeof or as a statement expression.
1115 Thus, do not give an error here; an error will result later. */
1116 return exp;
1119 ptrtype = build_pointer_type (restype);
1121 if (TREE_CODE (exp) == VAR_DECL)
1123 /* ??? This is not really quite correct
1124 in that the type of the operand of ADDR_EXPR
1125 is not the target type of the type of the ADDR_EXPR itself.
1126 Question is, can this lossage be avoided? */
1127 adr = build1 (ADDR_EXPR, ptrtype, exp);
1128 if (!c_mark_addressable (exp))
1129 return error_mark_node;
1130 TREE_CONSTANT (adr) = staticp (exp);
1131 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1132 return adr;
1134 /* This way is better for a COMPONENT_REF since it can
1135 simplify the offset for a component. */
1136 adr = build_unary_op (ADDR_EXPR, exp, 1);
1137 return convert (ptrtype, adr);
1139 return exp;
1142 /* Perform default promotions for C data used in expressions.
1143 Arrays and functions are converted to pointers;
1144 enumeral types or short or char, to int.
1145 In addition, manifest constants symbols are replaced by their values. */
1147 tree
1148 default_conversion (tree exp)
1150 tree orig_exp;
1151 tree type = TREE_TYPE (exp);
1152 enum tree_code code = TREE_CODE (type);
1154 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1155 return default_function_array_conversion (exp);
1157 /* Constants can be used directly unless they're not loadable. */
1158 if (TREE_CODE (exp) == CONST_DECL)
1159 exp = DECL_INITIAL (exp);
1161 /* Replace a nonvolatile const static variable with its value unless
1162 it is an array, in which case we must be sure that taking the
1163 address of the array produces consistent results. */
1164 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1166 exp = decl_constant_value_for_broken_optimization (exp);
1167 type = TREE_TYPE (exp);
1170 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1171 an lvalue.
1173 Do not use STRIP_NOPS here! It will remove conversions from pointer
1174 to integer and cause infinite recursion. */
1175 orig_exp = exp;
1176 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1177 || (TREE_CODE (exp) == NOP_EXPR
1178 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1179 exp = TREE_OPERAND (exp, 0);
1181 /* Preserve the original expression code. */
1182 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1183 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1185 /* Normally convert enums to int,
1186 but convert wide enums to something wider. */
1187 if (code == ENUMERAL_TYPE)
1189 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1190 TYPE_PRECISION (integer_type_node)),
1191 ((TYPE_PRECISION (type)
1192 >= TYPE_PRECISION (integer_type_node))
1193 && TREE_UNSIGNED (type)));
1195 return convert (type, exp);
1198 if (TREE_CODE (exp) == COMPONENT_REF
1199 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1200 /* If it's thinner than an int, promote it like a
1201 c_promoting_integer_type_p, otherwise leave it alone. */
1202 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1203 TYPE_PRECISION (integer_type_node)))
1204 return convert (integer_type_node, exp);
1206 if (c_promoting_integer_type_p (type))
1208 /* Preserve unsignedness if not really getting any wider. */
1209 if (TREE_UNSIGNED (type)
1210 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1211 return convert (unsigned_type_node, exp);
1213 return convert (integer_type_node, exp);
1216 if (code == VOID_TYPE)
1218 error ("void value not ignored as it ought to be");
1219 return error_mark_node;
1221 return exp;
1224 /* Look up COMPONENT in a structure or union DECL.
1226 If the component name is not found, returns NULL_TREE. Otherwise,
1227 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1228 stepping down the chain to the component, which is in the last
1229 TREE_VALUE of the list. Normally the list is of length one, but if
1230 the component is embedded within (nested) anonymous structures or
1231 unions, the list steps down the chain to the component. */
1233 static tree
1234 lookup_field (tree decl, tree component)
1236 tree type = TREE_TYPE (decl);
1237 tree field;
1239 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1240 to the field elements. Use a binary search on this array to quickly
1241 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1242 will always be set for structures which have many elements. */
1244 if (TYPE_LANG_SPECIFIC (type))
1246 int bot, top, half;
1247 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1249 field = TYPE_FIELDS (type);
1250 bot = 0;
1251 top = TYPE_LANG_SPECIFIC (type)->s->len;
1252 while (top - bot > 1)
1254 half = (top - bot + 1) >> 1;
1255 field = field_array[bot+half];
1257 if (DECL_NAME (field) == NULL_TREE)
1259 /* Step through all anon unions in linear fashion. */
1260 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1262 field = field_array[bot++];
1263 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1264 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1266 tree anon = lookup_field (field, component);
1268 if (anon)
1269 return tree_cons (NULL_TREE, field, anon);
1273 /* Entire record is only anon unions. */
1274 if (bot > top)
1275 return NULL_TREE;
1277 /* Restart the binary search, with new lower bound. */
1278 continue;
1281 if (DECL_NAME (field) == component)
1282 break;
1283 if (DECL_NAME (field) < component)
1284 bot += half;
1285 else
1286 top = bot + half;
1289 if (DECL_NAME (field_array[bot]) == component)
1290 field = field_array[bot];
1291 else if (DECL_NAME (field) != component)
1292 return NULL_TREE;
1294 else
1296 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1298 if (DECL_NAME (field) == NULL_TREE
1299 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1300 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1302 tree anon = lookup_field (field, component);
1304 if (anon)
1305 return tree_cons (NULL_TREE, field, anon);
1308 if (DECL_NAME (field) == component)
1309 break;
1312 if (field == NULL_TREE)
1313 return NULL_TREE;
1316 return tree_cons (NULL_TREE, field, NULL_TREE);
1319 /* Make an expression to refer to the COMPONENT field of
1320 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1322 tree
1323 build_component_ref (tree datum, tree component)
1325 tree type = TREE_TYPE (datum);
1326 enum tree_code code = TREE_CODE (type);
1327 tree field = NULL;
1328 tree ref;
1330 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1331 Ensure that the arguments are not lvalues; otherwise,
1332 if the component is an array, it would wrongly decay to a pointer in
1333 C89 mode.
1334 We cannot do this with a COND_EXPR, because in a conditional expression
1335 the default promotions are applied to both sides, and this would yield
1336 the wrong type of the result; for example, if the components have
1337 type "char". */
1338 switch (TREE_CODE (datum))
1340 case COMPOUND_EXPR:
1342 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1343 return build (COMPOUND_EXPR, TREE_TYPE (value),
1344 TREE_OPERAND (datum, 0), non_lvalue (value));
1346 default:
1347 break;
1350 /* See if there is a field or component with name COMPONENT. */
1352 if (code == RECORD_TYPE || code == UNION_TYPE)
1354 if (!COMPLETE_TYPE_P (type))
1356 c_incomplete_type_error (NULL_TREE, type);
1357 return error_mark_node;
1360 field = lookup_field (datum, component);
1362 if (!field)
1364 error ("%s has no member named `%s'",
1365 code == RECORD_TYPE ? "structure" : "union",
1366 IDENTIFIER_POINTER (component));
1367 return error_mark_node;
1370 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1371 This might be better solved in future the way the C++ front
1372 end does it - by giving the anonymous entities each a
1373 separate name and type, and then have build_component_ref
1374 recursively call itself. We can't do that here. */
1377 tree subdatum = TREE_VALUE (field);
1379 if (TREE_TYPE (subdatum) == error_mark_node)
1380 return error_mark_node;
1382 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1383 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1384 TREE_READONLY (ref) = 1;
1385 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1386 TREE_THIS_VOLATILE (ref) = 1;
1388 if (TREE_DEPRECATED (subdatum))
1389 warn_deprecated_use (subdatum);
1391 datum = ref;
1393 field = TREE_CHAIN (field);
1395 while (field);
1397 return ref;
1399 else if (code != ERROR_MARK)
1400 error ("request for member `%s' in something not a structure or union",
1401 IDENTIFIER_POINTER (component));
1403 return error_mark_node;
1406 /* Given an expression PTR for a pointer, return an expression
1407 for the value pointed to.
1408 ERRORSTRING is the name of the operator to appear in error messages. */
1410 tree
1411 build_indirect_ref (tree ptr, const char *errorstring)
1413 tree pointer = default_conversion (ptr);
1414 tree type = TREE_TYPE (pointer);
1416 if (TREE_CODE (type) == POINTER_TYPE)
1418 if (TREE_CODE (pointer) == ADDR_EXPR
1419 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1420 == TREE_TYPE (type)))
1421 return TREE_OPERAND (pointer, 0);
1422 else
1424 tree t = TREE_TYPE (type);
1425 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1427 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1429 error ("dereferencing pointer to incomplete type");
1430 return error_mark_node;
1432 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1433 warning ("dereferencing `void *' pointer");
1435 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1436 so that we get the proper error message if the result is used
1437 to assign to. Also, &* is supposed to be a no-op.
1438 And ANSI C seems to specify that the type of the result
1439 should be the const type. */
1440 /* A de-reference of a pointer to const is not a const. It is valid
1441 to change it via some other pointer. */
1442 TREE_READONLY (ref) = TYPE_READONLY (t);
1443 TREE_SIDE_EFFECTS (ref)
1444 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1445 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1446 return ref;
1449 else if (TREE_CODE (pointer) != ERROR_MARK)
1450 error ("invalid type argument of `%s'", errorstring);
1451 return error_mark_node;
1454 /* This handles expressions of the form "a[i]", which denotes
1455 an array reference.
1457 This is logically equivalent in C to *(a+i), but we may do it differently.
1458 If A is a variable or a member, we generate a primitive ARRAY_REF.
1459 This avoids forcing the array out of registers, and can work on
1460 arrays that are not lvalues (for example, members of structures returned
1461 by functions). */
1463 tree
1464 build_array_ref (tree array, tree index)
1466 if (index == 0)
1468 error ("subscript missing in array reference");
1469 return error_mark_node;
1472 if (TREE_TYPE (array) == error_mark_node
1473 || TREE_TYPE (index) == error_mark_node)
1474 return error_mark_node;
1476 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1477 && TREE_CODE (array) != INDIRECT_REF)
1479 tree rval, type;
1481 /* Subscripting with type char is likely to lose
1482 on a machine where chars are signed.
1483 So warn on any machine, but optionally.
1484 Don't warn for unsigned char since that type is safe.
1485 Don't warn for signed char because anyone who uses that
1486 must have done so deliberately. */
1487 if (warn_char_subscripts
1488 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1489 warning ("array subscript has type `char'");
1491 /* Apply default promotions *after* noticing character types. */
1492 index = default_conversion (index);
1494 /* Require integer *after* promotion, for sake of enums. */
1495 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1497 error ("array subscript is not an integer");
1498 return error_mark_node;
1501 /* An array that is indexed by a non-constant
1502 cannot be stored in a register; we must be able to do
1503 address arithmetic on its address.
1504 Likewise an array of elements of variable size. */
1505 if (TREE_CODE (index) != INTEGER_CST
1506 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1507 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1509 if (!c_mark_addressable (array))
1510 return error_mark_node;
1512 /* An array that is indexed by a constant value which is not within
1513 the array bounds cannot be stored in a register either; because we
1514 would get a crash in store_bit_field/extract_bit_field when trying
1515 to access a non-existent part of the register. */
1516 if (TREE_CODE (index) == INTEGER_CST
1517 && TYPE_DOMAIN (TREE_TYPE (array))
1518 && ! int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
1520 if (!c_mark_addressable (array))
1521 return error_mark_node;
1524 if (pedantic)
1526 tree foo = array;
1527 while (TREE_CODE (foo) == COMPONENT_REF)
1528 foo = TREE_OPERAND (foo, 0);
1529 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1530 pedwarn ("ISO C forbids subscripting `register' array");
1531 else if (! flag_isoc99 && ! lvalue_p (foo))
1532 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1535 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1536 rval = build (ARRAY_REF, type, array, index);
1537 /* Array ref is const/volatile if the array elements are
1538 or if the array is. */
1539 TREE_READONLY (rval)
1540 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1541 | TREE_READONLY (array));
1542 TREE_SIDE_EFFECTS (rval)
1543 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1544 | TREE_SIDE_EFFECTS (array));
1545 TREE_THIS_VOLATILE (rval)
1546 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1547 /* This was added by rms on 16 Nov 91.
1548 It fixes vol struct foo *a; a->elts[1]
1549 in an inline function.
1550 Hope it doesn't break something else. */
1551 | TREE_THIS_VOLATILE (array));
1552 return require_complete_type (fold (rval));
1556 tree ar = default_conversion (array);
1557 tree ind = default_conversion (index);
1559 /* Do the same warning check as above, but only on the part that's
1560 syntactically the index and only if it is also semantically
1561 the index. */
1562 if (warn_char_subscripts
1563 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1564 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1565 warning ("subscript has type `char'");
1567 /* Put the integer in IND to simplify error checking. */
1568 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1570 tree temp = ar;
1571 ar = ind;
1572 ind = temp;
1575 if (ar == error_mark_node)
1576 return ar;
1578 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1579 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1581 error ("subscripted value is neither array nor pointer");
1582 return error_mark_node;
1584 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1586 error ("array subscript is not an integer");
1587 return error_mark_node;
1590 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1591 "array indexing");
1595 /* Build an external reference to identifier ID. FUN indicates
1596 whether this will be used for a function call. */
1597 tree
1598 build_external_ref (tree id, int fun)
1600 tree ref;
1601 tree decl = lookup_name (id);
1602 tree objc_ivar = lookup_objc_ivar (id);
1604 if (decl && decl != error_mark_node)
1606 /* Properly declared variable or function reference. */
1607 if (!objc_ivar)
1608 ref = decl;
1609 else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1611 warning ("local declaration of `%s' hides instance variable",
1612 IDENTIFIER_POINTER (id));
1613 ref = decl;
1615 else
1616 ref = objc_ivar;
1618 else if (objc_ivar)
1619 ref = objc_ivar;
1620 else if (fun)
1621 /* Implicit function declaration. */
1622 ref = implicitly_declare (id);
1623 else if (decl == error_mark_node)
1624 /* Don't complain about something that's already been
1625 complained about. */
1626 return error_mark_node;
1627 else
1629 undeclared_variable (id);
1630 return error_mark_node;
1633 if (TREE_TYPE (ref) == error_mark_node)
1634 return error_mark_node;
1636 if (TREE_DEPRECATED (ref))
1637 warn_deprecated_use (ref);
1639 if (!skip_evaluation)
1640 assemble_external (ref);
1641 TREE_USED (ref) = 1;
1643 if (TREE_CODE (ref) == CONST_DECL)
1645 ref = DECL_INITIAL (ref);
1646 TREE_CONSTANT (ref) = 1;
1648 else if (current_function_decl != 0
1649 && !DECL_FILE_SCOPE_P (current_function_decl)
1650 && (TREE_CODE (ref) == VAR_DECL
1651 || TREE_CODE (ref) == PARM_DECL
1652 || TREE_CODE (ref) == FUNCTION_DECL))
1654 tree context = decl_function_context (ref);
1656 if (context != 0 && context != current_function_decl)
1657 DECL_NONLOCAL (ref) = 1;
1660 return ref;
1663 /* Build a function call to function FUNCTION with parameters PARAMS.
1664 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1665 TREE_VALUE of each node is a parameter-expression.
1666 FUNCTION's data type may be a function type or a pointer-to-function. */
1668 tree
1669 build_function_call (tree function, tree params)
1671 tree fntype, fundecl = 0;
1672 tree coerced_params;
1673 tree name = NULL_TREE, result;
1674 tree tem;
1676 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1677 STRIP_TYPE_NOPS (function);
1679 /* Convert anything with function type to a pointer-to-function. */
1680 if (TREE_CODE (function) == FUNCTION_DECL)
1682 name = DECL_NAME (function);
1684 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1685 (because calling an inline function does not mean the function
1686 needs to be separately compiled). */
1687 fntype = build_type_variant (TREE_TYPE (function),
1688 TREE_READONLY (function),
1689 TREE_THIS_VOLATILE (function));
1690 fundecl = function;
1691 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1693 else
1694 function = default_conversion (function);
1696 fntype = TREE_TYPE (function);
1698 if (TREE_CODE (fntype) == ERROR_MARK)
1699 return error_mark_node;
1701 if (!(TREE_CODE (fntype) == POINTER_TYPE
1702 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1704 error ("called object is not a function");
1705 return error_mark_node;
1708 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1709 current_function_returns_abnormally = 1;
1711 /* fntype now gets the type of function pointed to. */
1712 fntype = TREE_TYPE (fntype);
1714 /* Check that the function is called through a compatible prototype.
1715 If it is not, replace the call by a trap, wrapped up in a compound
1716 expression if necessary. This has the nice side-effect to prevent
1717 the tree-inliner from generating invalid assignment trees which may
1718 blow up in the RTL expander later.
1720 ??? This doesn't work for Objective-C because objc_comptypes
1721 refuses to compare function prototypes, yet the compiler appears
1722 to build calls that are flagged as invalid by C's comptypes. */
1723 if (! c_dialect_objc ()
1724 && TREE_CODE (function) == NOP_EXPR
1725 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1726 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1727 && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1729 tree return_type = TREE_TYPE (fntype);
1730 tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1731 NULL_TREE);
1733 /* This situation leads to run-time undefined behavior. We can't,
1734 therefore, simply error unless we can prove that all possible
1735 executions of the program must execute the code. */
1736 warning ("function called through a non-compatible type");
1738 /* We can, however, treat "undefined" any way we please.
1739 Call abort to encourage the user to fix the program. */
1740 inform ("if this code is reached, the program will abort");
1742 if (VOID_TYPE_P (return_type))
1743 return trap;
1744 else
1746 tree rhs;
1748 if (AGGREGATE_TYPE_P (return_type))
1749 rhs = build_compound_literal (return_type,
1750 build_constructor (return_type,
1751 NULL_TREE));
1752 else
1753 rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1755 return build (COMPOUND_EXPR, return_type, trap, rhs);
1759 /* Convert the parameters to the types declared in the
1760 function prototype, or apply default promotions. */
1762 coerced_params
1763 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1765 /* Check that the arguments to the function are valid. */
1767 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1769 /* Recognize certain built-in functions so we can make tree-codes
1770 other than CALL_EXPR. We do this when it enables fold-const.c
1771 to do something useful. */
1773 if (TREE_CODE (function) == ADDR_EXPR
1774 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1775 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1777 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1778 params, coerced_params);
1779 if (result)
1780 return result;
1783 result = build (CALL_EXPR, TREE_TYPE (fntype),
1784 function, coerced_params, NULL_TREE);
1785 TREE_SIDE_EFFECTS (result) = 1;
1786 result = fold (result);
1788 if (VOID_TYPE_P (TREE_TYPE (result)))
1789 return result;
1790 return require_complete_type (result);
1793 /* Convert the argument expressions in the list VALUES
1794 to the types in the list TYPELIST. The result is a list of converted
1795 argument expressions.
1797 If TYPELIST is exhausted, or when an element has NULL as its type,
1798 perform the default conversions.
1800 PARMLIST is the chain of parm decls for the function being called.
1801 It may be 0, if that info is not available.
1802 It is used only for generating error messages.
1804 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1806 This is also where warnings about wrong number of args are generated.
1808 Both VALUES and the returned value are chains of TREE_LIST nodes
1809 with the elements of the list in the TREE_VALUE slots of those nodes. */
1811 static tree
1812 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1814 tree typetail, valtail;
1815 tree result = NULL;
1816 int parmnum;
1818 /* Scan the given expressions and types, producing individual
1819 converted arguments and pushing them on RESULT in reverse order. */
1821 for (valtail = values, typetail = typelist, parmnum = 0;
1822 valtail;
1823 valtail = TREE_CHAIN (valtail), parmnum++)
1825 tree type = typetail ? TREE_VALUE (typetail) : 0;
1826 tree val = TREE_VALUE (valtail);
1828 if (type == void_type_node)
1830 if (name)
1831 error ("too many arguments to function `%s'",
1832 IDENTIFIER_POINTER (name));
1833 else
1834 error ("too many arguments to function");
1835 break;
1838 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1839 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1840 to convert automatically to a pointer. */
1841 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1842 val = TREE_OPERAND (val, 0);
1844 val = default_function_array_conversion (val);
1846 val = require_complete_type (val);
1848 if (type != 0)
1850 /* Formal parm type is specified by a function prototype. */
1851 tree parmval;
1853 if (!COMPLETE_TYPE_P (type))
1855 error ("type of formal parameter %d is incomplete", parmnum + 1);
1856 parmval = val;
1858 else
1860 /* Optionally warn about conversions that
1861 differ from the default conversions. */
1862 if (warn_conversion || warn_traditional)
1864 int formal_prec = TYPE_PRECISION (type);
1866 if (INTEGRAL_TYPE_P (type)
1867 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1868 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1869 if (INTEGRAL_TYPE_P (type)
1870 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1871 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1872 else if (TREE_CODE (type) == COMPLEX_TYPE
1873 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1874 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1875 else if (TREE_CODE (type) == REAL_TYPE
1876 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1877 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1878 else if (TREE_CODE (type) == COMPLEX_TYPE
1879 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1880 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1881 else if (TREE_CODE (type) == REAL_TYPE
1882 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1883 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1884 /* ??? At some point, messages should be written about
1885 conversions between complex types, but that's too messy
1886 to do now. */
1887 else if (TREE_CODE (type) == REAL_TYPE
1888 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1890 /* Warn if any argument is passed as `float',
1891 since without a prototype it would be `double'. */
1892 if (formal_prec == TYPE_PRECISION (float_type_node))
1893 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1895 /* Detect integer changing in width or signedness.
1896 These warnings are only activated with
1897 -Wconversion, not with -Wtraditional. */
1898 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1899 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1901 tree would_have_been = default_conversion (val);
1902 tree type1 = TREE_TYPE (would_have_been);
1904 if (TREE_CODE (type) == ENUMERAL_TYPE
1905 && (TYPE_MAIN_VARIANT (type)
1906 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1907 /* No warning if function asks for enum
1908 and the actual arg is that enum type. */
1910 else if (formal_prec != TYPE_PRECISION (type1))
1911 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1912 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1914 /* Don't complain if the formal parameter type
1915 is an enum, because we can't tell now whether
1916 the value was an enum--even the same enum. */
1917 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1919 else if (TREE_CODE (val) == INTEGER_CST
1920 && int_fits_type_p (val, type))
1921 /* Change in signedness doesn't matter
1922 if a constant value is unaffected. */
1924 /* Likewise for a constant in a NOP_EXPR. */
1925 else if (TREE_CODE (val) == NOP_EXPR
1926 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1927 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1929 /* If the value is extended from a narrower
1930 unsigned type, it doesn't matter whether we
1931 pass it as signed or unsigned; the value
1932 certainly is the same either way. */
1933 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1934 && TREE_UNSIGNED (TREE_TYPE (val)))
1936 else if (TREE_UNSIGNED (type))
1937 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1938 else
1939 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1943 parmval = convert_for_assignment (type, val,
1944 (char *) 0, /* arg passing */
1945 fundecl, name, parmnum + 1);
1947 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
1948 && INTEGRAL_TYPE_P (type)
1949 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1950 parmval = default_conversion (parmval);
1952 result = tree_cons (NULL_TREE, parmval, result);
1954 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1955 && (TYPE_PRECISION (TREE_TYPE (val))
1956 < TYPE_PRECISION (double_type_node)))
1957 /* Convert `float' to `double'. */
1958 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1959 else
1960 /* Convert `short' and `char' to full-size `int'. */
1961 result = tree_cons (NULL_TREE, default_conversion (val), result);
1963 if (typetail)
1964 typetail = TREE_CHAIN (typetail);
1967 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1969 if (name)
1970 error ("too few arguments to function `%s'",
1971 IDENTIFIER_POINTER (name));
1972 else
1973 error ("too few arguments to function");
1976 return nreverse (result);
1979 /* This is the entry point used by the parser
1980 for binary operators in the input.
1981 In addition to constructing the expression,
1982 we check for operands that were written with other binary operators
1983 in a way that is likely to confuse the user. */
1985 tree
1986 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1988 tree result = build_binary_op (code, arg1, arg2, 1);
1990 char class;
1991 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1992 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1993 enum tree_code code1 = ERROR_MARK;
1994 enum tree_code code2 = ERROR_MARK;
1996 if (TREE_CODE (result) == ERROR_MARK)
1997 return error_mark_node;
1999 if (IS_EXPR_CODE_CLASS (class1))
2000 code1 = C_EXP_ORIGINAL_CODE (arg1);
2001 if (IS_EXPR_CODE_CLASS (class2))
2002 code2 = C_EXP_ORIGINAL_CODE (arg2);
2004 /* Check for cases such as x+y<<z which users are likely
2005 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
2006 is cleared to prevent these warnings. */
2007 if (warn_parentheses)
2009 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2011 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2012 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2013 warning ("suggest parentheses around + or - inside shift");
2016 if (code == TRUTH_ORIF_EXPR)
2018 if (code1 == TRUTH_ANDIF_EXPR
2019 || code2 == TRUTH_ANDIF_EXPR)
2020 warning ("suggest parentheses around && within ||");
2023 if (code == BIT_IOR_EXPR)
2025 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2026 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2027 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2028 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2029 warning ("suggest parentheses around arithmetic in operand of |");
2030 /* Check cases like x|y==z */
2031 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2032 warning ("suggest parentheses around comparison in operand of |");
2035 if (code == BIT_XOR_EXPR)
2037 if (code1 == BIT_AND_EXPR
2038 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2039 || code2 == BIT_AND_EXPR
2040 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2041 warning ("suggest parentheses around arithmetic in operand of ^");
2042 /* Check cases like x^y==z */
2043 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2044 warning ("suggest parentheses around comparison in operand of ^");
2047 if (code == BIT_AND_EXPR)
2049 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2050 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2051 warning ("suggest parentheses around + or - in operand of &");
2052 /* Check cases like x&y==z */
2053 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2054 warning ("suggest parentheses around comparison in operand of &");
2058 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
2059 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2060 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2061 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2063 unsigned_conversion_warning (result, arg1);
2064 unsigned_conversion_warning (result, arg2);
2065 overflow_warning (result);
2067 class = TREE_CODE_CLASS (TREE_CODE (result));
2069 /* Record the code that was specified in the source,
2070 for the sake of warnings about confusing nesting. */
2071 if (IS_EXPR_CODE_CLASS (class))
2072 C_SET_EXP_ORIGINAL_CODE (result, code);
2073 else
2075 int flag = TREE_CONSTANT (result);
2076 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2077 so that convert_for_assignment wouldn't strip it.
2078 That way, we got warnings for things like p = (1 - 1).
2079 But it turns out we should not get those warnings. */
2080 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2081 C_SET_EXP_ORIGINAL_CODE (result, code);
2082 TREE_CONSTANT (result) = flag;
2085 return result;
2089 /* Return true if `t' is known to be non-negative. */
2092 c_tree_expr_nonnegative_p (tree t)
2094 if (TREE_CODE (t) == STMT_EXPR)
2096 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2098 /* Find the last statement in the chain, ignoring the final
2099 * scope statement */
2100 while (TREE_CHAIN (t) != NULL_TREE
2101 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2102 t = TREE_CHAIN (t);
2103 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2105 return tree_expr_nonnegative_p (t);
2108 /* Return a tree for the difference of pointers OP0 and OP1.
2109 The resulting tree has type int. */
2111 static tree
2112 pointer_diff (tree op0, tree op1)
2114 tree result, folded;
2115 tree restype = ptrdiff_type_node;
2117 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2118 tree con0, con1, lit0, lit1;
2119 tree orig_op1 = op1;
2121 if (pedantic || warn_pointer_arith)
2123 if (TREE_CODE (target_type) == VOID_TYPE)
2124 pedwarn ("pointer of type `void *' used in subtraction");
2125 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2126 pedwarn ("pointer to a function used in subtraction");
2129 /* If the conversion to ptrdiff_type does anything like widening or
2130 converting a partial to an integral mode, we get a convert_expression
2131 that is in the way to do any simplifications.
2132 (fold-const.c doesn't know that the extra bits won't be needed.
2133 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2134 different mode in place.)
2135 So first try to find a common term here 'by hand'; we want to cover
2136 at least the cases that occur in legal static initializers. */
2137 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2138 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2140 if (TREE_CODE (con0) == PLUS_EXPR)
2142 lit0 = TREE_OPERAND (con0, 1);
2143 con0 = TREE_OPERAND (con0, 0);
2145 else
2146 lit0 = integer_zero_node;
2148 if (TREE_CODE (con1) == PLUS_EXPR)
2150 lit1 = TREE_OPERAND (con1, 1);
2151 con1 = TREE_OPERAND (con1, 0);
2153 else
2154 lit1 = integer_zero_node;
2156 if (operand_equal_p (con0, con1, 0))
2158 op0 = lit0;
2159 op1 = lit1;
2163 /* First do the subtraction as integers;
2164 then drop through to build the divide operator.
2165 Do not do default conversions on the minus operator
2166 in case restype is a short type. */
2168 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2169 convert (restype, op1), 0);
2170 /* This generates an error if op1 is pointer to incomplete type. */
2171 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2172 error ("arithmetic on pointer to an incomplete type");
2174 /* This generates an error if op0 is pointer to incomplete type. */
2175 op1 = c_size_in_bytes (target_type);
2177 /* Divide by the size, in easiest possible way. */
2179 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2181 folded = fold (result);
2182 if (folded == result)
2183 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2184 return folded;
2187 /* Construct and perhaps optimize a tree representation
2188 for a unary operation. CODE, a tree_code, specifies the operation
2189 and XARG is the operand.
2190 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2191 the default promotions (such as from short to int).
2192 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2193 allows non-lvalues; this is only used to handle conversion of non-lvalue
2194 arrays to pointers in C99. */
2196 tree
2197 build_unary_op (enum tree_code code, tree xarg, int flag)
2199 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2200 tree arg = xarg;
2201 tree argtype = 0;
2202 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2203 tree val;
2204 int noconvert = flag;
2206 if (typecode == ERROR_MARK)
2207 return error_mark_node;
2208 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2209 typecode = INTEGER_TYPE;
2211 switch (code)
2213 case CONVERT_EXPR:
2214 /* This is used for unary plus, because a CONVERT_EXPR
2215 is enough to prevent anybody from looking inside for
2216 associativity, but won't generate any code. */
2217 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2218 || typecode == COMPLEX_TYPE))
2220 error ("wrong type argument to unary plus");
2221 return error_mark_node;
2223 else if (!noconvert)
2224 arg = default_conversion (arg);
2225 arg = non_lvalue (arg);
2226 break;
2228 case NEGATE_EXPR:
2229 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2230 || typecode == COMPLEX_TYPE
2231 || typecode == VECTOR_TYPE))
2233 error ("wrong type argument to unary minus");
2234 return error_mark_node;
2236 else if (!noconvert)
2237 arg = default_conversion (arg);
2238 break;
2240 case BIT_NOT_EXPR:
2241 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2243 if (!noconvert)
2244 arg = default_conversion (arg);
2246 else if (typecode == COMPLEX_TYPE)
2248 code = CONJ_EXPR;
2249 if (pedantic)
2250 pedwarn ("ISO C does not support `~' for complex conjugation");
2251 if (!noconvert)
2252 arg = default_conversion (arg);
2254 else
2256 error ("wrong type argument to bit-complement");
2257 return error_mark_node;
2259 break;
2261 case ABS_EXPR:
2262 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2264 error ("wrong type argument to abs");
2265 return error_mark_node;
2267 else if (!noconvert)
2268 arg = default_conversion (arg);
2269 break;
2271 case CONJ_EXPR:
2272 /* Conjugating a real value is a no-op, but allow it anyway. */
2273 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2274 || typecode == COMPLEX_TYPE))
2276 error ("wrong type argument to conjugation");
2277 return error_mark_node;
2279 else if (!noconvert)
2280 arg = default_conversion (arg);
2281 break;
2283 case TRUTH_NOT_EXPR:
2284 if (typecode != INTEGER_TYPE
2285 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2286 && typecode != COMPLEX_TYPE
2287 /* These will convert to a pointer. */
2288 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2290 error ("wrong type argument to unary exclamation mark");
2291 return error_mark_node;
2293 arg = lang_hooks.truthvalue_conversion (arg);
2294 return invert_truthvalue (arg);
2296 case NOP_EXPR:
2297 break;
2299 case REALPART_EXPR:
2300 if (TREE_CODE (arg) == COMPLEX_CST)
2301 return TREE_REALPART (arg);
2302 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2303 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2304 else
2305 return arg;
2307 case IMAGPART_EXPR:
2308 if (TREE_CODE (arg) == COMPLEX_CST)
2309 return TREE_IMAGPART (arg);
2310 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2311 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2312 else
2313 return convert (TREE_TYPE (arg), integer_zero_node);
2315 case PREINCREMENT_EXPR:
2316 case POSTINCREMENT_EXPR:
2317 case PREDECREMENT_EXPR:
2318 case POSTDECREMENT_EXPR:
2320 /* Increment or decrement the real part of the value,
2321 and don't change the imaginary part. */
2322 if (typecode == COMPLEX_TYPE)
2324 tree real, imag;
2326 if (pedantic)
2327 pedwarn ("ISO C does not support `++' and `--' on complex types");
2329 arg = stabilize_reference (arg);
2330 real = build_unary_op (REALPART_EXPR, arg, 1);
2331 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2332 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2333 build_unary_op (code, real, 1), imag);
2336 /* Report invalid types. */
2338 if (typecode != POINTER_TYPE
2339 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2341 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2342 error ("wrong type argument to increment");
2343 else
2344 error ("wrong type argument to decrement");
2346 return error_mark_node;
2350 tree inc;
2351 tree result_type = TREE_TYPE (arg);
2353 arg = get_unwidened (arg, 0);
2354 argtype = TREE_TYPE (arg);
2356 /* Compute the increment. */
2358 if (typecode == POINTER_TYPE)
2360 /* If pointer target is an undefined struct,
2361 we just cannot know how to do the arithmetic. */
2362 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2364 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2365 error ("increment of pointer to unknown structure");
2366 else
2367 error ("decrement of pointer to unknown structure");
2369 else if ((pedantic || warn_pointer_arith)
2370 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2371 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2373 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2374 pedwarn ("wrong type argument to increment");
2375 else
2376 pedwarn ("wrong type argument to decrement");
2379 inc = c_size_in_bytes (TREE_TYPE (result_type));
2381 else
2382 inc = integer_one_node;
2384 inc = convert (argtype, inc);
2386 /* Complain about anything else that is not a true lvalue. */
2387 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2388 || code == POSTINCREMENT_EXPR)
2389 ? "invalid lvalue in increment"
2390 : "invalid lvalue in decrement")))
2391 return error_mark_node;
2393 /* Report a read-only lvalue. */
2394 if (TREE_READONLY (arg))
2395 readonly_error (arg,
2396 ((code == PREINCREMENT_EXPR
2397 || code == POSTINCREMENT_EXPR)
2398 ? "increment" : "decrement"));
2400 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2401 val = boolean_increment (code, arg);
2402 else
2403 val = build (code, TREE_TYPE (arg), arg, inc);
2404 TREE_SIDE_EFFECTS (val) = 1;
2405 val = convert (result_type, val);
2406 if (TREE_CODE (val) != code)
2407 TREE_NO_UNUSED_WARNING (val) = 1;
2408 return val;
2411 case ADDR_EXPR:
2412 /* Note that this operation never does default_conversion. */
2414 /* Let &* cancel out to simplify resulting code. */
2415 if (TREE_CODE (arg) == INDIRECT_REF)
2417 /* Don't let this be an lvalue. */
2418 if (lvalue_p (TREE_OPERAND (arg, 0)))
2419 return non_lvalue (TREE_OPERAND (arg, 0));
2420 return TREE_OPERAND (arg, 0);
2423 /* For &x[y], return x+y */
2424 if (TREE_CODE (arg) == ARRAY_REF)
2426 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2427 return error_mark_node;
2428 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2429 TREE_OPERAND (arg, 1), 1);
2432 /* Anything not already handled and not a true memory reference
2433 or a non-lvalue array is an error. */
2434 else if (typecode != FUNCTION_TYPE && !flag
2435 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2436 return error_mark_node;
2438 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2439 argtype = TREE_TYPE (arg);
2441 /* If the lvalue is const or volatile, merge that into the type
2442 to which the address will point. Note that you can't get a
2443 restricted pointer by taking the address of something, so we
2444 only have to deal with `const' and `volatile' here. */
2445 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2446 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2447 argtype = c_build_type_variant (argtype,
2448 TREE_READONLY (arg),
2449 TREE_THIS_VOLATILE (arg));
2451 argtype = build_pointer_type (argtype);
2453 if (!c_mark_addressable (arg))
2454 return error_mark_node;
2457 tree addr;
2459 if (TREE_CODE (arg) == COMPONENT_REF)
2461 tree field = TREE_OPERAND (arg, 1);
2463 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2465 if (DECL_C_BIT_FIELD (field))
2467 error ("attempt to take address of bit-field structure member `%s'",
2468 IDENTIFIER_POINTER (DECL_NAME (field)));
2469 return error_mark_node;
2472 addr = fold (build (PLUS_EXPR, argtype,
2473 convert (argtype, addr),
2474 convert (argtype, byte_position (field))));
2476 else
2477 addr = build1 (code, argtype, arg);
2479 /* Address of a static or external variable or
2480 file-scope function counts as a constant. */
2481 if (staticp (arg)
2482 && ! (TREE_CODE (arg) == FUNCTION_DECL
2483 && !DECL_FILE_SCOPE_P (arg)))
2484 TREE_CONSTANT (addr) = 1;
2485 return addr;
2488 default:
2489 break;
2492 if (argtype == 0)
2493 argtype = TREE_TYPE (arg);
2494 return fold (build1 (code, argtype, arg));
2497 /* Return nonzero if REF is an lvalue valid for this language.
2498 Lvalues can be assigned, unless their type has TYPE_READONLY.
2499 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2502 lvalue_p (tree ref)
2504 enum tree_code code = TREE_CODE (ref);
2506 switch (code)
2508 case REALPART_EXPR:
2509 case IMAGPART_EXPR:
2510 case COMPONENT_REF:
2511 return lvalue_p (TREE_OPERAND (ref, 0));
2513 case COMPOUND_LITERAL_EXPR:
2514 case STRING_CST:
2515 return 1;
2517 case INDIRECT_REF:
2518 case ARRAY_REF:
2519 case VAR_DECL:
2520 case PARM_DECL:
2521 case RESULT_DECL:
2522 case ERROR_MARK:
2523 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2524 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2526 case BIND_EXPR:
2527 case RTL_EXPR:
2528 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2530 default:
2531 return 0;
2535 /* Return nonzero if REF is an lvalue valid for this language;
2536 otherwise, print an error message and return zero. */
2539 lvalue_or_else (tree ref, const char *msgid)
2541 int win = lvalue_p (ref);
2543 if (! win)
2544 error ("%s", msgid);
2546 return win;
2550 /* Warn about storing in something that is `const'. */
2552 void
2553 readonly_error (tree arg, const char *msgid)
2555 if (TREE_CODE (arg) == COMPONENT_REF)
2557 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2558 readonly_error (TREE_OPERAND (arg, 0), msgid);
2559 else
2560 error ("%s of read-only member `%s'", _(msgid),
2561 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2563 else if (TREE_CODE (arg) == VAR_DECL)
2564 error ("%s of read-only variable `%s'", _(msgid),
2565 IDENTIFIER_POINTER (DECL_NAME (arg)));
2566 else
2567 error ("%s of read-only location", _(msgid));
2570 /* Mark EXP saying that we need to be able to take the
2571 address of it; it should not be allocated in a register.
2572 Returns true if successful. */
2574 bool
2575 c_mark_addressable (tree exp)
2577 tree x = exp;
2579 while (1)
2580 switch (TREE_CODE (x))
2582 case COMPONENT_REF:
2583 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2585 error ("cannot take address of bit-field `%s'",
2586 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2587 return false;
2590 /* ... fall through ... */
2592 case ADDR_EXPR:
2593 case ARRAY_REF:
2594 case REALPART_EXPR:
2595 case IMAGPART_EXPR:
2596 x = TREE_OPERAND (x, 0);
2597 break;
2599 case COMPOUND_LITERAL_EXPR:
2600 case CONSTRUCTOR:
2601 TREE_ADDRESSABLE (x) = 1;
2602 return true;
2604 case VAR_DECL:
2605 case CONST_DECL:
2606 case PARM_DECL:
2607 case RESULT_DECL:
2608 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
2609 && DECL_NONLOCAL (x))
2611 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2613 error ("global register variable `%s' used in nested function",
2614 IDENTIFIER_POINTER (DECL_NAME (x)));
2615 return false;
2617 pedwarn ("register variable `%s' used in nested function",
2618 IDENTIFIER_POINTER (DECL_NAME (x)));
2620 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
2622 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2624 error ("address of global register variable `%s' requested",
2625 IDENTIFIER_POINTER (DECL_NAME (x)));
2626 return false;
2629 /* If we are making this addressable due to its having
2630 volatile components, give a different error message. Also
2631 handle the case of an unnamed parameter by not trying
2632 to give the name. */
2634 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
2636 error ("cannot put object with volatile field into register");
2637 return false;
2640 pedwarn ("address of register variable `%s' requested",
2641 IDENTIFIER_POINTER (DECL_NAME (x)));
2643 put_var_into_stack (x, /*rescan=*/true);
2645 /* drops in */
2646 case FUNCTION_DECL:
2647 TREE_ADDRESSABLE (x) = 1;
2648 /* drops out */
2649 default:
2650 return true;
2654 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2656 tree
2657 build_conditional_expr (tree ifexp, tree op1, tree op2)
2659 tree type1;
2660 tree type2;
2661 enum tree_code code1;
2662 enum tree_code code2;
2663 tree result_type = NULL;
2664 tree orig_op1 = op1, orig_op2 = op2;
2666 ifexp = lang_hooks.truthvalue_conversion (default_conversion (ifexp));
2668 /* Promote both alternatives. */
2670 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2671 op1 = default_conversion (op1);
2672 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2673 op2 = default_conversion (op2);
2675 if (TREE_CODE (ifexp) == ERROR_MARK
2676 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2677 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2678 return error_mark_node;
2680 type1 = TREE_TYPE (op1);
2681 code1 = TREE_CODE (type1);
2682 type2 = TREE_TYPE (op2);
2683 code2 = TREE_CODE (type2);
2685 /* C90 does not permit non-lvalue arrays in conditional expressions.
2686 In C99 they will be pointers by now. */
2687 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2689 error ("non-lvalue array in conditional expression");
2690 return error_mark_node;
2693 /* Quickly detect the usual case where op1 and op2 have the same type
2694 after promotion. */
2695 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2697 if (type1 == type2)
2698 result_type = type1;
2699 else
2700 result_type = TYPE_MAIN_VARIANT (type1);
2702 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2703 || code1 == COMPLEX_TYPE)
2704 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2705 || code2 == COMPLEX_TYPE))
2707 result_type = common_type (type1, type2);
2709 /* If -Wsign-compare, warn here if type1 and type2 have
2710 different signedness. We'll promote the signed to unsigned
2711 and later code won't know it used to be different.
2712 Do this check on the original types, so that explicit casts
2713 will be considered, but default promotions won't. */
2714 if (warn_sign_compare && !skip_evaluation)
2716 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
2717 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
2719 if (unsigned_op1 ^ unsigned_op2)
2721 /* Do not warn if the result type is signed, since the
2722 signed type will only be chosen if it can represent
2723 all the values of the unsigned type. */
2724 if (! TREE_UNSIGNED (result_type))
2725 /* OK */;
2726 /* Do not warn if the signed quantity is an unsuffixed
2727 integer literal (or some static constant expression
2728 involving such literals) and it is non-negative. */
2729 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2730 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2731 /* OK */;
2732 else
2733 warning ("signed and unsigned type in conditional expression");
2737 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2739 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2740 pedwarn ("ISO C forbids conditional expr with only one void side");
2741 result_type = void_type_node;
2743 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2745 if (comp_target_types (type1, type2, 1))
2746 result_type = common_type (type1, type2);
2747 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2748 && TREE_CODE (orig_op1) != NOP_EXPR)
2749 result_type = qualify_type (type2, type1);
2750 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2751 && TREE_CODE (orig_op2) != NOP_EXPR)
2752 result_type = qualify_type (type1, type2);
2753 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2755 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2756 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2757 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2758 TREE_TYPE (type2)));
2760 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2762 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2763 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2764 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2765 TREE_TYPE (type1)));
2767 else
2769 pedwarn ("pointer type mismatch in conditional expression");
2770 result_type = build_pointer_type (void_type_node);
2773 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2775 if (! integer_zerop (op2))
2776 pedwarn ("pointer/integer type mismatch in conditional expression");
2777 else
2779 op2 = null_pointer_node;
2781 result_type = type1;
2783 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2785 if (!integer_zerop (op1))
2786 pedwarn ("pointer/integer type mismatch in conditional expression");
2787 else
2789 op1 = null_pointer_node;
2791 result_type = type2;
2794 if (!result_type)
2796 if (flag_cond_mismatch)
2797 result_type = void_type_node;
2798 else
2800 error ("type mismatch in conditional expression");
2801 return error_mark_node;
2805 /* Merge const and volatile flags of the incoming types. */
2806 result_type
2807 = build_type_variant (result_type,
2808 TREE_READONLY (op1) || TREE_READONLY (op2),
2809 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2811 if (result_type != TREE_TYPE (op1))
2812 op1 = convert_and_check (result_type, op1);
2813 if (result_type != TREE_TYPE (op2))
2814 op2 = convert_and_check (result_type, op2);
2816 if (TREE_CODE (ifexp) == INTEGER_CST)
2817 return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2819 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2822 /* Given a list of expressions, return a compound expression
2823 that performs them all and returns the value of the last of them. */
2825 tree
2826 build_compound_expr (tree list)
2828 return internal_build_compound_expr (list, TRUE);
2831 static tree
2832 internal_build_compound_expr (tree list, int first_p)
2834 tree rest;
2836 if (TREE_CHAIN (list) == 0)
2838 /* Convert arrays and functions to pointers when there
2839 really is a comma operator. */
2840 if (!first_p)
2841 TREE_VALUE (list)
2842 = default_function_array_conversion (TREE_VALUE (list));
2844 /* Don't let (0, 0) be null pointer constant. */
2845 if (!first_p && integer_zerop (TREE_VALUE (list)))
2846 return non_lvalue (TREE_VALUE (list));
2847 return TREE_VALUE (list);
2850 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2852 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2854 /* The left-hand operand of a comma expression is like an expression
2855 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2856 any side-effects, unless it was explicitly cast to (void). */
2857 if (warn_unused_value
2858 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2859 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2860 warning ("left-hand operand of comma expression has no effect");
2863 /* With -Wunused, we should also warn if the left-hand operand does have
2864 side-effects, but computes a value which is not used. For example, in
2865 `foo() + bar(), baz()' the result of the `+' operator is not used,
2866 so we should issue a warning. */
2867 else if (warn_unused_value)
2868 warn_if_unused_value (TREE_VALUE (list));
2870 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2873 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2875 tree
2876 build_c_cast (tree type, tree expr)
2878 tree value = expr;
2880 if (type == error_mark_node || expr == error_mark_node)
2881 return error_mark_node;
2883 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2884 only in <protocol> qualifications. But when constructing cast expressions,
2885 the protocols do matter and must be kept around. */
2886 if (!c_dialect_objc () || !objc_is_object_ptr (type))
2887 type = TYPE_MAIN_VARIANT (type);
2889 if (TREE_CODE (type) == ARRAY_TYPE)
2891 error ("cast specifies array type");
2892 return error_mark_node;
2895 if (TREE_CODE (type) == FUNCTION_TYPE)
2897 error ("cast specifies function type");
2898 return error_mark_node;
2901 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2903 if (pedantic)
2905 if (TREE_CODE (type) == RECORD_TYPE
2906 || TREE_CODE (type) == UNION_TYPE)
2907 pedwarn ("ISO C forbids casting nonscalar to the same type");
2910 else if (TREE_CODE (type) == UNION_TYPE)
2912 tree field;
2913 value = default_function_array_conversion (value);
2915 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2916 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2917 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
2918 break;
2920 if (field)
2922 tree t;
2924 if (pedantic)
2925 pedwarn ("ISO C forbids casts to union type");
2926 t = digest_init (type,
2927 build_constructor (type,
2928 build_tree_list (field, value)),
2930 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2931 return t;
2933 error ("cast to union type from type not present in union");
2934 return error_mark_node;
2936 else
2938 tree otype, ovalue;
2940 /* If casting to void, avoid the error that would come
2941 from default_conversion in the case of a non-lvalue array. */
2942 if (type == void_type_node)
2943 return build1 (CONVERT_EXPR, type, value);
2945 /* Convert functions and arrays to pointers,
2946 but don't convert any other types. */
2947 value = default_function_array_conversion (value);
2948 otype = TREE_TYPE (value);
2950 /* Optionally warn about potentially worrisome casts. */
2952 if (warn_cast_qual
2953 && TREE_CODE (type) == POINTER_TYPE
2954 && TREE_CODE (otype) == POINTER_TYPE)
2956 tree in_type = type;
2957 tree in_otype = otype;
2958 int added = 0;
2959 int discarded = 0;
2961 /* Check that the qualifiers on IN_TYPE are a superset of
2962 the qualifiers of IN_OTYPE. The outermost level of
2963 POINTER_TYPE nodes is uninteresting and we stop as soon
2964 as we hit a non-POINTER_TYPE node on either type. */
2967 in_otype = TREE_TYPE (in_otype);
2968 in_type = TREE_TYPE (in_type);
2970 /* GNU C allows cv-qualified function types. 'const'
2971 means the function is very pure, 'volatile' means it
2972 can't return. We need to warn when such qualifiers
2973 are added, not when they're taken away. */
2974 if (TREE_CODE (in_otype) == FUNCTION_TYPE
2975 && TREE_CODE (in_type) == FUNCTION_TYPE)
2976 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
2977 else
2978 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
2980 while (TREE_CODE (in_type) == POINTER_TYPE
2981 && TREE_CODE (in_otype) == POINTER_TYPE);
2983 if (added)
2984 warning ("cast adds new qualifiers to function type");
2986 if (discarded)
2987 /* There are qualifiers present in IN_OTYPE that are not
2988 present in IN_TYPE. */
2989 warning ("cast discards qualifiers from pointer target type");
2992 /* Warn about possible alignment problems. */
2993 if (STRICT_ALIGNMENT && warn_cast_align
2994 && TREE_CODE (type) == POINTER_TYPE
2995 && TREE_CODE (otype) == POINTER_TYPE
2996 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
2997 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
2998 /* Don't warn about opaque types, where the actual alignment
2999 restriction is unknown. */
3000 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3001 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3002 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3003 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3004 warning ("cast increases required alignment of target type");
3006 if (TREE_CODE (type) == INTEGER_TYPE
3007 && TREE_CODE (otype) == POINTER_TYPE
3008 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3009 && !TREE_CONSTANT (value))
3010 warning ("cast from pointer to integer of different size");
3012 if (warn_bad_function_cast
3013 && TREE_CODE (value) == CALL_EXPR
3014 && TREE_CODE (type) != TREE_CODE (otype))
3015 warning ("cast does not match function type");
3017 if (TREE_CODE (type) == POINTER_TYPE
3018 && TREE_CODE (otype) == INTEGER_TYPE
3019 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3020 /* Don't warn about converting any constant. */
3021 && !TREE_CONSTANT (value))
3022 warning ("cast to pointer from integer of different size");
3024 if (TREE_CODE (type) == POINTER_TYPE
3025 && TREE_CODE (otype) == POINTER_TYPE
3026 && TREE_CODE (expr) == ADDR_EXPR
3027 && DECL_P (TREE_OPERAND (expr, 0))
3028 && flag_strict_aliasing && warn_strict_aliasing
3029 && !VOID_TYPE_P (TREE_TYPE (type)))
3031 /* Casting the address of a decl to non void pointer. Warn
3032 if the cast breaks type based aliasing. */
3033 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3034 warning ("type-punning to incomplete type might break strict-aliasing rules");
3035 else
3037 HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3038 HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3040 if (!alias_sets_conflict_p (set1, set2))
3041 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3042 else if (warn_strict_aliasing > 1
3043 && !alias_sets_might_conflict_p (set1, set2))
3044 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3048 /* If pedantic, warn for conversions between function and object
3049 pointer types, except for converting a null pointer constant
3050 to function pointer type. */
3051 if (pedantic
3052 && TREE_CODE (type) == POINTER_TYPE
3053 && TREE_CODE (otype) == POINTER_TYPE
3054 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3055 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3056 pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3058 if (pedantic
3059 && TREE_CODE (type) == POINTER_TYPE
3060 && TREE_CODE (otype) == POINTER_TYPE
3061 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3062 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3063 && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3064 && TREE_CODE (expr) != NOP_EXPR))
3065 pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3067 ovalue = value;
3068 /* Replace a nonvolatile const static variable with its value. */
3069 if (optimize && TREE_CODE (value) == VAR_DECL)
3070 value = decl_constant_value (value);
3071 value = convert (type, value);
3073 /* Ignore any integer overflow caused by the cast. */
3074 if (TREE_CODE (value) == INTEGER_CST)
3076 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3077 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3081 /* Don't let (void *) (FOO *) 0 be a null pointer constant. */
3082 if (TREE_CODE (value) == INTEGER_CST
3083 && TREE_CODE (expr) == INTEGER_CST
3084 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3085 value = non_lvalue (value);
3087 /* Don't let a cast be an lvalue. */
3088 if (value == expr)
3089 value = non_lvalue (value);
3091 return value;
3094 /* Interpret a cast of expression EXPR to type TYPE. */
3095 tree
3096 c_cast_expr (tree type, tree expr)
3098 int saved_wsp = warn_strict_prototypes;
3100 /* This avoids warnings about unprototyped casts on
3101 integers. E.g. "#define SIG_DFL (void(*)())0". */
3102 if (TREE_CODE (expr) == INTEGER_CST)
3103 warn_strict_prototypes = 0;
3104 type = groktypename (type);
3105 warn_strict_prototypes = saved_wsp;
3107 return build_c_cast (type, expr);
3111 /* Build an assignment expression of lvalue LHS from value RHS.
3112 MODIFYCODE is the code for a binary operator that we use
3113 to combine the old value of LHS with RHS to get the new value.
3114 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3116 tree
3117 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3119 tree result;
3120 tree newrhs;
3121 tree lhstype = TREE_TYPE (lhs);
3122 tree olhstype = lhstype;
3124 /* Types that aren't fully specified cannot be used in assignments. */
3125 lhs = require_complete_type (lhs);
3127 /* Avoid duplicate error messages from operands that had errors. */
3128 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3129 return error_mark_node;
3131 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3132 /* Do not use STRIP_NOPS here. We do not want an enumerator
3133 whose value is 0 to count as a null pointer constant. */
3134 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3135 rhs = TREE_OPERAND (rhs, 0);
3137 newrhs = rhs;
3139 /* If a binary op has been requested, combine the old LHS value with the RHS
3140 producing the value we should actually store into the LHS. */
3142 if (modifycode != NOP_EXPR)
3144 lhs = stabilize_reference (lhs);
3145 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3148 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3149 return error_mark_node;
3151 /* Warn about storing in something that is `const'. */
3153 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3154 || ((TREE_CODE (lhstype) == RECORD_TYPE
3155 || TREE_CODE (lhstype) == UNION_TYPE)
3156 && C_TYPE_FIELDS_READONLY (lhstype)))
3157 readonly_error (lhs, "assignment");
3159 /* If storing into a structure or union member,
3160 it has probably been given type `int'.
3161 Compute the type that would go with
3162 the actual amount of storage the member occupies. */
3164 if (TREE_CODE (lhs) == COMPONENT_REF
3165 && (TREE_CODE (lhstype) == INTEGER_TYPE
3166 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3167 || TREE_CODE (lhstype) == REAL_TYPE
3168 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3169 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3171 /* If storing in a field that is in actuality a short or narrower than one,
3172 we must store in the field in its actual type. */
3174 if (lhstype != TREE_TYPE (lhs))
3176 lhs = copy_node (lhs);
3177 TREE_TYPE (lhs) = lhstype;
3180 /* Convert new value to destination type. */
3182 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3183 NULL_TREE, NULL_TREE, 0);
3184 if (TREE_CODE (newrhs) == ERROR_MARK)
3185 return error_mark_node;
3187 /* Scan operands */
3189 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3190 TREE_SIDE_EFFECTS (result) = 1;
3192 /* If we got the LHS in a different type for storing in,
3193 convert the result back to the nominal type of LHS
3194 so that the value we return always has the same type
3195 as the LHS argument. */
3197 if (olhstype == TREE_TYPE (result))
3198 return result;
3199 return convert_for_assignment (olhstype, result, _("assignment"),
3200 NULL_TREE, NULL_TREE, 0);
3203 /* Convert value RHS to type TYPE as preparation for an assignment
3204 to an lvalue of type TYPE.
3205 The real work of conversion is done by `convert'.
3206 The purpose of this function is to generate error messages
3207 for assignments that are not allowed in C.
3208 ERRTYPE is a string to use in error messages:
3209 "assignment", "return", etc. If it is null, this is parameter passing
3210 for a function call (and different error messages are output).
3212 FUNNAME is the name of the function being called,
3213 as an IDENTIFIER_NODE, or null.
3214 PARMNUM is the number of the argument, for printing in error messages. */
3216 static tree
3217 convert_for_assignment (tree type, tree rhs, const char *errtype,
3218 tree fundecl, tree funname, int parmnum)
3220 enum tree_code codel = TREE_CODE (type);
3221 tree rhstype;
3222 enum tree_code coder;
3224 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3225 /* Do not use STRIP_NOPS here. We do not want an enumerator
3226 whose value is 0 to count as a null pointer constant. */
3227 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3228 rhs = TREE_OPERAND (rhs, 0);
3230 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3231 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3232 rhs = default_conversion (rhs);
3233 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3234 rhs = decl_constant_value_for_broken_optimization (rhs);
3236 rhstype = TREE_TYPE (rhs);
3237 coder = TREE_CODE (rhstype);
3239 if (coder == ERROR_MARK)
3240 return error_mark_node;
3242 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3244 overflow_warning (rhs);
3245 /* Check for Objective-C protocols. This will automatically
3246 issue a warning if there are protocol violations. No need to
3247 use the return value. */
3248 if (c_dialect_objc ())
3249 objc_comptypes (type, rhstype, 0);
3250 return rhs;
3253 if (coder == VOID_TYPE)
3255 error ("void value not ignored as it ought to be");
3256 return error_mark_node;
3258 /* A type converts to a reference to it.
3259 This code doesn't fully support references, it's just for the
3260 special case of va_start and va_copy. */
3261 if (codel == REFERENCE_TYPE
3262 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3264 if (!lvalue_p (rhs))
3266 error ("cannot pass rvalue to reference parameter");
3267 return error_mark_node;
3269 if (!c_mark_addressable (rhs))
3270 return error_mark_node;
3271 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3273 /* We already know that these two types are compatible, but they
3274 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3275 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3276 likely to be va_list, a typedef to __builtin_va_list, which
3277 is different enough that it will cause problems later. */
3278 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3279 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3281 rhs = build1 (NOP_EXPR, type, rhs);
3282 return rhs;
3284 /* Some types can interconvert without explicit casts. */
3285 else if (codel == VECTOR_TYPE
3286 && comptypes (type, TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3287 return convert (type, rhs);
3288 /* Arithmetic types all interconvert, and enum is treated like int. */
3289 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3290 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3291 || codel == BOOLEAN_TYPE)
3292 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3293 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3294 || coder == BOOLEAN_TYPE))
3295 return convert_and_check (type, rhs);
3297 /* Conversion to a transparent union from its member types.
3298 This applies only to function arguments. */
3299 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3301 tree memb_types;
3302 tree marginal_memb_type = 0;
3304 for (memb_types = TYPE_FIELDS (type); memb_types;
3305 memb_types = TREE_CHAIN (memb_types))
3307 tree memb_type = TREE_TYPE (memb_types);
3309 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3310 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3311 break;
3313 if (TREE_CODE (memb_type) != POINTER_TYPE)
3314 continue;
3316 if (coder == POINTER_TYPE)
3318 tree ttl = TREE_TYPE (memb_type);
3319 tree ttr = TREE_TYPE (rhstype);
3321 /* Any non-function converts to a [const][volatile] void *
3322 and vice versa; otherwise, targets must be the same.
3323 Meanwhile, the lhs target must have all the qualifiers of
3324 the rhs. */
3325 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3326 || comp_target_types (memb_type, rhstype, 0))
3328 /* If this type won't generate any warnings, use it. */
3329 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3330 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3331 && TREE_CODE (ttl) == FUNCTION_TYPE)
3332 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3333 == TYPE_QUALS (ttr))
3334 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3335 == TYPE_QUALS (ttl))))
3336 break;
3338 /* Keep looking for a better type, but remember this one. */
3339 if (! marginal_memb_type)
3340 marginal_memb_type = memb_type;
3344 /* Can convert integer zero to any pointer type. */
3345 if (integer_zerop (rhs)
3346 || (TREE_CODE (rhs) == NOP_EXPR
3347 && integer_zerop (TREE_OPERAND (rhs, 0))))
3349 rhs = null_pointer_node;
3350 break;
3354 if (memb_types || marginal_memb_type)
3356 if (! memb_types)
3358 /* We have only a marginally acceptable member type;
3359 it needs a warning. */
3360 tree ttl = TREE_TYPE (marginal_memb_type);
3361 tree ttr = TREE_TYPE (rhstype);
3363 /* Const and volatile mean something different for function
3364 types, so the usual warnings are not appropriate. */
3365 if (TREE_CODE (ttr) == FUNCTION_TYPE
3366 && TREE_CODE (ttl) == FUNCTION_TYPE)
3368 /* Because const and volatile on functions are
3369 restrictions that say the function will not do
3370 certain things, it is okay to use a const or volatile
3371 function where an ordinary one is wanted, but not
3372 vice-versa. */
3373 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3374 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3375 errtype, funname, parmnum);
3377 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3378 warn_for_assignment ("%s discards qualifiers from pointer target type",
3379 errtype, funname,
3380 parmnum);
3383 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3384 pedwarn ("ISO C prohibits argument conversion to union type");
3386 return build1 (NOP_EXPR, type, rhs);
3390 /* Conversions among pointers */
3391 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3392 && (coder == codel))
3394 tree ttl = TREE_TYPE (type);
3395 tree ttr = TREE_TYPE (rhstype);
3396 bool is_opaque_pointer;
3397 int target_cmp = 0; /* Cache comp_target_types () result. */
3399 /* Opaque pointers are treated like void pointers. */
3400 is_opaque_pointer = (targetm.vector_opaque_p (type)
3401 || targetm.vector_opaque_p (rhstype))
3402 && TREE_CODE (ttl) == VECTOR_TYPE
3403 && TREE_CODE (ttr) == VECTOR_TYPE;
3405 /* Any non-function converts to a [const][volatile] void *
3406 and vice versa; otherwise, targets must be the same.
3407 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3408 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3409 || (target_cmp = comp_target_types (type, rhstype, 0))
3410 || is_opaque_pointer
3411 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3412 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3414 if (pedantic
3415 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3417 (VOID_TYPE_P (ttr)
3418 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3419 which are not ANSI null ptr constants. */
3420 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3421 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3422 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3423 errtype, funname, parmnum);
3424 /* Const and volatile mean something different for function types,
3425 so the usual warnings are not appropriate. */
3426 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3427 && TREE_CODE (ttl) != FUNCTION_TYPE)
3429 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3430 warn_for_assignment ("%s discards qualifiers from pointer target type",
3431 errtype, funname, parmnum);
3432 /* If this is not a case of ignoring a mismatch in signedness,
3433 no warning. */
3434 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3435 || target_cmp)
3437 /* If there is a mismatch, do warn. */
3438 else if (pedantic)
3439 warn_for_assignment ("pointer targets in %s differ in signedness",
3440 errtype, funname, parmnum);
3442 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3443 && TREE_CODE (ttr) == FUNCTION_TYPE)
3445 /* Because const and volatile on functions are restrictions
3446 that say the function will not do certain things,
3447 it is okay to use a const or volatile function
3448 where an ordinary one is wanted, but not vice-versa. */
3449 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3450 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3451 errtype, funname, parmnum);
3454 else
3455 warn_for_assignment ("%s from incompatible pointer type",
3456 errtype, funname, parmnum);
3457 return convert (type, rhs);
3459 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3461 error ("invalid use of non-lvalue array");
3462 return error_mark_node;
3464 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3466 /* An explicit constant 0 can convert to a pointer,
3467 or one that results from arithmetic, even including
3468 a cast to integer type. */
3469 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3471 ! (TREE_CODE (rhs) == NOP_EXPR
3472 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3473 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3474 && integer_zerop (TREE_OPERAND (rhs, 0))))
3475 warn_for_assignment ("%s makes pointer from integer without a cast",
3476 errtype, funname, parmnum);
3478 return convert (type, rhs);
3480 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3482 warn_for_assignment ("%s makes integer from pointer without a cast",
3483 errtype, funname, parmnum);
3484 return convert (type, rhs);
3486 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3487 return convert (type, rhs);
3489 if (!errtype)
3491 if (funname)
3493 tree selector = objc_message_selector ();
3495 if (selector && parmnum > 2)
3496 error ("incompatible type for argument %d of `%s'",
3497 parmnum - 2, IDENTIFIER_POINTER (selector));
3498 else
3499 error ("incompatible type for argument %d of `%s'",
3500 parmnum, IDENTIFIER_POINTER (funname));
3502 else
3503 error ("incompatible type for argument %d of indirect function call",
3504 parmnum);
3506 else
3507 error ("incompatible types in %s", errtype);
3509 return error_mark_node;
3512 /* Convert VALUE for assignment into inlined parameter PARM. ARGNUM
3513 is used for error and waring reporting and indicates which argument
3514 is being processed. */
3516 tree
3517 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3519 tree ret, type;
3521 /* If FN was prototyped, the value has been converted already
3522 in convert_arguments. */
3523 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3524 return value;
3526 type = TREE_TYPE (parm);
3527 ret = convert_for_assignment (type, value,
3528 (char *) 0 /* arg passing */, fn,
3529 DECL_NAME (fn), argnum);
3530 if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3531 && INTEGRAL_TYPE_P (type)
3532 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3533 ret = default_conversion (ret);
3534 return ret;
3537 /* Print a warning using MSGID.
3538 It gets OPNAME as its one parameter.
3539 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3540 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3541 FUNCTION and ARGNUM are handled specially if we are building an
3542 Objective-C selector. */
3544 static void
3545 warn_for_assignment (const char *msgid, const char *opname, tree function,
3546 int argnum)
3548 if (opname == 0)
3550 tree selector = objc_message_selector ();
3551 char * new_opname;
3553 if (selector && argnum > 2)
3555 function = selector;
3556 argnum -= 2;
3558 if (argnum == 0)
3560 if (function)
3562 /* Function name is known; supply it. */
3563 const char *const argstring = _("passing arg of `%s'");
3564 new_opname = alloca (IDENTIFIER_LENGTH (function)
3565 + strlen (argstring) + 1 + 1);
3566 sprintf (new_opname, argstring,
3567 IDENTIFIER_POINTER (function));
3569 else
3571 /* Function name unknown (call through ptr). */
3572 const char *const argnofun = _("passing arg of pointer to function");
3573 new_opname = alloca (strlen (argnofun) + 1 + 1);
3574 sprintf (new_opname, argnofun);
3577 else if (function)
3579 /* Function name is known; supply it. */
3580 const char *const argstring = _("passing arg %d of `%s'");
3581 new_opname = alloca (IDENTIFIER_LENGTH (function)
3582 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3583 sprintf (new_opname, argstring, argnum,
3584 IDENTIFIER_POINTER (function));
3586 else
3588 /* Function name unknown (call through ptr); just give arg number. */
3589 const char *const argnofun = _("passing arg %d of pointer to function");
3590 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3591 sprintf (new_opname, argnofun, argnum);
3593 opname = new_opname;
3595 pedwarn (msgid, opname);
3598 /* If VALUE is a compound expr all of whose expressions are constant, then
3599 return its value. Otherwise, return error_mark_node.
3601 This is for handling COMPOUND_EXPRs as initializer elements
3602 which is allowed with a warning when -pedantic is specified. */
3604 static tree
3605 valid_compound_expr_initializer (tree value, tree endtype)
3607 if (TREE_CODE (value) == COMPOUND_EXPR)
3609 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3610 == error_mark_node)
3611 return error_mark_node;
3612 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3613 endtype);
3615 else if (! TREE_CONSTANT (value)
3616 && ! initializer_constant_valid_p (value, endtype))
3617 return error_mark_node;
3618 else
3619 return value;
3622 /* Perform appropriate conversions on the initial value of a variable,
3623 store it in the declaration DECL,
3624 and print any error messages that are appropriate.
3625 If the init is invalid, store an ERROR_MARK. */
3627 void
3628 store_init_value (tree decl, tree init)
3630 tree value, type;
3632 /* If variable's type was invalidly declared, just ignore it. */
3634 type = TREE_TYPE (decl);
3635 if (TREE_CODE (type) == ERROR_MARK)
3636 return;
3638 /* Digest the specified initializer into an expression. */
3640 value = digest_init (type, init, TREE_STATIC (decl));
3642 /* Store the expression if valid; else report error. */
3644 if (warn_traditional && !in_system_header
3645 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3646 warning ("traditional C rejects automatic aggregate initialization");
3648 DECL_INITIAL (decl) = value;
3650 /* ANSI wants warnings about out-of-range constant initializers. */
3651 STRIP_TYPE_NOPS (value);
3652 constant_expression_warning (value);
3654 /* Check if we need to set array size from compound literal size. */
3655 if (TREE_CODE (type) == ARRAY_TYPE
3656 && TYPE_DOMAIN (type) == 0
3657 && value != error_mark_node)
3659 tree inside_init = init;
3661 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3662 inside_init = TREE_OPERAND (init, 0);
3663 inside_init = fold (inside_init);
3665 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3667 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3669 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3671 /* For int foo[] = (int [3]){1}; we need to set array size
3672 now since later on array initializer will be just the
3673 brace enclosed list of the compound literal. */
3674 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3675 layout_type (type);
3676 layout_decl (decl, 0);
3682 /* Methods for storing and printing names for error messages. */
3684 /* Implement a spelling stack that allows components of a name to be pushed
3685 and popped. Each element on the stack is this structure. */
3687 struct spelling
3689 int kind;
3690 union
3692 int i;
3693 const char *s;
3694 } u;
3697 #define SPELLING_STRING 1
3698 #define SPELLING_MEMBER 2
3699 #define SPELLING_BOUNDS 3
3701 static struct spelling *spelling; /* Next stack element (unused). */
3702 static struct spelling *spelling_base; /* Spelling stack base. */
3703 static int spelling_size; /* Size of the spelling stack. */
3705 /* Macros to save and restore the spelling stack around push_... functions.
3706 Alternative to SAVE_SPELLING_STACK. */
3708 #define SPELLING_DEPTH() (spelling - spelling_base)
3709 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3711 /* Push an element on the spelling stack with type KIND and assign VALUE
3712 to MEMBER. */
3714 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3716 int depth = SPELLING_DEPTH (); \
3718 if (depth >= spelling_size) \
3720 spelling_size += 10; \
3721 if (spelling_base == 0) \
3722 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3723 else \
3724 spelling_base = xrealloc (spelling_base, \
3725 spelling_size * sizeof (struct spelling)); \
3726 RESTORE_SPELLING_DEPTH (depth); \
3729 spelling->kind = (KIND); \
3730 spelling->MEMBER = (VALUE); \
3731 spelling++; \
3734 /* Push STRING on the stack. Printed literally. */
3736 static void
3737 push_string (const char *string)
3739 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3742 /* Push a member name on the stack. Printed as '.' STRING. */
3744 static void
3745 push_member_name (tree decl)
3747 const char *const string
3748 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3749 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3752 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3754 static void
3755 push_array_bounds (int bounds)
3757 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3760 /* Compute the maximum size in bytes of the printed spelling. */
3762 static int
3763 spelling_length (void)
3765 int size = 0;
3766 struct spelling *p;
3768 for (p = spelling_base; p < spelling; p++)
3770 if (p->kind == SPELLING_BOUNDS)
3771 size += 25;
3772 else
3773 size += strlen (p->u.s) + 1;
3776 return size;
3779 /* Print the spelling to BUFFER and return it. */
3781 static char *
3782 print_spelling (char *buffer)
3784 char *d = buffer;
3785 struct spelling *p;
3787 for (p = spelling_base; p < spelling; p++)
3788 if (p->kind == SPELLING_BOUNDS)
3790 sprintf (d, "[%d]", p->u.i);
3791 d += strlen (d);
3793 else
3795 const char *s;
3796 if (p->kind == SPELLING_MEMBER)
3797 *d++ = '.';
3798 for (s = p->u.s; (*d = *s++); d++)
3801 *d++ = '\0';
3802 return buffer;
3805 /* Issue an error message for a bad initializer component.
3806 MSGID identifies the message.
3807 The component name is taken from the spelling stack. */
3809 void
3810 error_init (const char *msgid)
3812 char *ofwhat;
3814 error ("%s", _(msgid));
3815 ofwhat = print_spelling (alloca (spelling_length () + 1));
3816 if (*ofwhat)
3817 error ("(near initialization for `%s')", ofwhat);
3820 /* Issue a pedantic warning for a bad initializer component.
3821 MSGID identifies the message.
3822 The component name is taken from the spelling stack. */
3824 void
3825 pedwarn_init (const char *msgid)
3827 char *ofwhat;
3829 pedwarn ("%s", _(msgid));
3830 ofwhat = print_spelling (alloca (spelling_length () + 1));
3831 if (*ofwhat)
3832 pedwarn ("(near initialization for `%s')", ofwhat);
3835 /* Issue a warning for a bad initializer component.
3836 MSGID identifies the message.
3837 The component name is taken from the spelling stack. */
3839 static void
3840 warning_init (const char *msgid)
3842 char *ofwhat;
3844 warning ("%s", _(msgid));
3845 ofwhat = print_spelling (alloca (spelling_length () + 1));
3846 if (*ofwhat)
3847 warning ("(near initialization for `%s')", ofwhat);
3850 /* Digest the parser output INIT as an initializer for type TYPE.
3851 Return a C expression of type TYPE to represent the initial value.
3853 REQUIRE_CONSTANT requests an error if non-constant initializers or
3854 elements are seen. */
3856 static tree
3857 digest_init (tree type, tree init, int require_constant)
3859 enum tree_code code = TREE_CODE (type);
3860 tree inside_init = init;
3862 if (type == error_mark_node
3863 || init == error_mark_node
3864 || TREE_TYPE (init) == error_mark_node)
3865 return error_mark_node;
3867 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3868 /* Do not use STRIP_NOPS here. We do not want an enumerator
3869 whose value is 0 to count as a null pointer constant. */
3870 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3871 inside_init = TREE_OPERAND (init, 0);
3873 inside_init = fold (inside_init);
3875 /* Initialization of an array of chars from a string constant
3876 optionally enclosed in braces. */
3878 if (code == ARRAY_TYPE)
3880 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3881 if ((typ1 == char_type_node
3882 || typ1 == signed_char_type_node
3883 || typ1 == unsigned_char_type_node
3884 || typ1 == unsigned_wchar_type_node
3885 || typ1 == signed_wchar_type_node)
3886 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3888 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3889 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
3890 return inside_init;
3892 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3893 != char_type_node)
3894 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3896 error_init ("char-array initialized from wide string");
3897 return error_mark_node;
3899 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3900 == char_type_node)
3901 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3903 error_init ("int-array initialized from non-wide string");
3904 return error_mark_node;
3907 TREE_TYPE (inside_init) = type;
3908 if (TYPE_DOMAIN (type) != 0
3909 && TYPE_SIZE (type) != 0
3910 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3911 /* Subtract 1 (or sizeof (wchar_t))
3912 because it's ok to ignore the terminating null char
3913 that is counted in the length of the constant. */
3914 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
3915 TREE_STRING_LENGTH (inside_init)
3916 - ((TYPE_PRECISION (typ1)
3917 != TYPE_PRECISION (char_type_node))
3918 ? (TYPE_PRECISION (wchar_type_node)
3919 / BITS_PER_UNIT)
3920 : 1)))
3921 pedwarn_init ("initializer-string for array of chars is too long");
3923 return inside_init;
3927 /* Build a VECTOR_CST from a *constant* vector constructor. If the
3928 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
3929 below and handle as a constructor. */
3930 if (code == VECTOR_TYPE
3931 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT)
3932 && TREE_CONSTANT (inside_init))
3934 if (TREE_CODE (inside_init) == VECTOR_CST
3935 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3936 TYPE_MAIN_VARIANT (type),
3937 COMPARE_STRICT))
3938 return inside_init;
3939 else
3940 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
3943 /* Any type can be initialized
3944 from an expression of the same type, optionally with braces. */
3946 if (inside_init && TREE_TYPE (inside_init) != 0
3947 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3948 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
3949 || (code == ARRAY_TYPE
3950 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3951 || (code == VECTOR_TYPE
3952 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3953 || (code == POINTER_TYPE
3954 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3955 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
3956 TREE_TYPE (type), COMPARE_STRICT))
3957 || (code == POINTER_TYPE
3958 && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
3959 && comptypes (TREE_TYPE (inside_init),
3960 TREE_TYPE (type), COMPARE_STRICT))))
3962 if (code == POINTER_TYPE)
3964 inside_init = default_function_array_conversion (inside_init);
3966 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
3968 error_init ("invalid use of non-lvalue array");
3969 return error_mark_node;
3973 if (code == VECTOR_TYPE)
3974 /* Although the types are compatible, we may require a
3975 conversion. */
3976 inside_init = convert (type, inside_init);
3978 if (require_constant && !flag_isoc99
3979 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3981 /* As an extension, allow initializing objects with static storage
3982 duration with compound literals (which are then treated just as
3983 the brace enclosed list they contain). */
3984 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3985 inside_init = DECL_INITIAL (decl);
3988 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
3989 && TREE_CODE (inside_init) != CONSTRUCTOR)
3991 error_init ("array initialized from non-constant array expression");
3992 return error_mark_node;
3995 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
3996 inside_init = decl_constant_value_for_broken_optimization (inside_init);
3998 /* Compound expressions can only occur here if -pedantic or
3999 -pedantic-errors is specified. In the later case, we always want
4000 an error. In the former case, we simply want a warning. */
4001 if (require_constant && pedantic
4002 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4004 inside_init
4005 = valid_compound_expr_initializer (inside_init,
4006 TREE_TYPE (inside_init));
4007 if (inside_init == error_mark_node)
4008 error_init ("initializer element is not constant");
4009 else
4010 pedwarn_init ("initializer element is not constant");
4011 if (flag_pedantic_errors)
4012 inside_init = error_mark_node;
4014 else if (require_constant
4015 && (!TREE_CONSTANT (inside_init)
4016 /* This test catches things like `7 / 0' which
4017 result in an expression for which TREE_CONSTANT
4018 is true, but which is not actually something
4019 that is a legal constant. We really should not
4020 be using this function, because it is a part of
4021 the back-end. Instead, the expression should
4022 already have been turned into ERROR_MARK_NODE. */
4023 || !initializer_constant_valid_p (inside_init,
4024 TREE_TYPE (inside_init))))
4026 error_init ("initializer element is not constant");
4027 inside_init = error_mark_node;
4030 return inside_init;
4033 /* Handle scalar types, including conversions. */
4035 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4036 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4038 /* Note that convert_for_assignment calls default_conversion
4039 for arrays and functions. We must not call it in the
4040 case where inside_init is a null pointer constant. */
4041 inside_init
4042 = convert_for_assignment (type, init, _("initialization"),
4043 NULL_TREE, NULL_TREE, 0);
4045 if (require_constant && ! TREE_CONSTANT (inside_init))
4047 error_init ("initializer element is not constant");
4048 inside_init = error_mark_node;
4050 else if (require_constant
4051 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4053 error_init ("initializer element is not computable at load time");
4054 inside_init = error_mark_node;
4057 return inside_init;
4060 /* Come here only for records and arrays. */
4062 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4064 error_init ("variable-sized object may not be initialized");
4065 return error_mark_node;
4068 error_init ("invalid initializer");
4069 return error_mark_node;
4072 /* Handle initializers that use braces. */
4074 /* Type of object we are accumulating a constructor for.
4075 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4076 static tree constructor_type;
4078 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4079 left to fill. */
4080 static tree constructor_fields;
4082 /* For an ARRAY_TYPE, this is the specified index
4083 at which to store the next element we get. */
4084 static tree constructor_index;
4086 /* For an ARRAY_TYPE, this is the maximum index. */
4087 static tree constructor_max_index;
4089 /* For a RECORD_TYPE, this is the first field not yet written out. */
4090 static tree constructor_unfilled_fields;
4092 /* For an ARRAY_TYPE, this is the index of the first element
4093 not yet written out. */
4094 static tree constructor_unfilled_index;
4096 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4097 This is so we can generate gaps between fields, when appropriate. */
4098 static tree constructor_bit_index;
4100 /* If we are saving up the elements rather than allocating them,
4101 this is the list of elements so far (in reverse order,
4102 most recent first). */
4103 static tree constructor_elements;
4105 /* 1 if constructor should be incrementally stored into a constructor chain,
4106 0 if all the elements should be kept in AVL tree. */
4107 static int constructor_incremental;
4109 /* 1 if so far this constructor's elements are all compile-time constants. */
4110 static int constructor_constant;
4112 /* 1 if so far this constructor's elements are all valid address constants. */
4113 static int constructor_simple;
4115 /* 1 if this constructor is erroneous so far. */
4116 static int constructor_erroneous;
4118 /* Structure for managing pending initializer elements, organized as an
4119 AVL tree. */
4121 struct init_node
4123 struct init_node *left, *right;
4124 struct init_node *parent;
4125 int balance;
4126 tree purpose;
4127 tree value;
4130 /* Tree of pending elements at this constructor level.
4131 These are elements encountered out of order
4132 which belong at places we haven't reached yet in actually
4133 writing the output.
4134 Will never hold tree nodes across GC runs. */
4135 static struct init_node *constructor_pending_elts;
4137 /* The SPELLING_DEPTH of this constructor. */
4138 static int constructor_depth;
4140 /* 0 if implicitly pushing constructor levels is allowed. */
4141 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4143 static int require_constant_value;
4144 static int require_constant_elements;
4146 /* DECL node for which an initializer is being read.
4147 0 means we are reading a constructor expression
4148 such as (struct foo) {...}. */
4149 static tree constructor_decl;
4151 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4152 static const char *constructor_asmspec;
4154 /* Nonzero if this is an initializer for a top-level decl. */
4155 static int constructor_top_level;
4157 /* Nonzero if there were any member designators in this initializer. */
4158 static int constructor_designated;
4160 /* Nesting depth of designator list. */
4161 static int designator_depth;
4163 /* Nonzero if there were diagnosed errors in this designator list. */
4164 static int designator_errorneous;
4167 /* This stack has a level for each implicit or explicit level of
4168 structuring in the initializer, including the outermost one. It
4169 saves the values of most of the variables above. */
4171 struct constructor_range_stack;
4173 struct constructor_stack
4175 struct constructor_stack *next;
4176 tree type;
4177 tree fields;
4178 tree index;
4179 tree max_index;
4180 tree unfilled_index;
4181 tree unfilled_fields;
4182 tree bit_index;
4183 tree elements;
4184 struct init_node *pending_elts;
4185 int offset;
4186 int depth;
4187 /* If nonzero, this value should replace the entire
4188 constructor at this level. */
4189 tree replacement_value;
4190 struct constructor_range_stack *range_stack;
4191 char constant;
4192 char simple;
4193 char implicit;
4194 char erroneous;
4195 char outer;
4196 char incremental;
4197 char designated;
4200 struct constructor_stack *constructor_stack;
4202 /* This stack represents designators from some range designator up to
4203 the last designator in the list. */
4205 struct constructor_range_stack
4207 struct constructor_range_stack *next, *prev;
4208 struct constructor_stack *stack;
4209 tree range_start;
4210 tree index;
4211 tree range_end;
4212 tree fields;
4215 struct constructor_range_stack *constructor_range_stack;
4217 /* This stack records separate initializers that are nested.
4218 Nested initializers can't happen in ANSI C, but GNU C allows them
4219 in cases like { ... (struct foo) { ... } ... }. */
4221 struct initializer_stack
4223 struct initializer_stack *next;
4224 tree decl;
4225 const char *asmspec;
4226 struct constructor_stack *constructor_stack;
4227 struct constructor_range_stack *constructor_range_stack;
4228 tree elements;
4229 struct spelling *spelling;
4230 struct spelling *spelling_base;
4231 int spelling_size;
4232 char top_level;
4233 char require_constant_value;
4234 char require_constant_elements;
4237 struct initializer_stack *initializer_stack;
4239 /* Prepare to parse and output the initializer for variable DECL. */
4241 void
4242 start_init (tree decl, tree asmspec_tree, int top_level)
4244 const char *locus;
4245 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4246 const char *asmspec = 0;
4248 if (asmspec_tree)
4249 asmspec = TREE_STRING_POINTER (asmspec_tree);
4251 p->decl = constructor_decl;
4252 p->asmspec = constructor_asmspec;
4253 p->require_constant_value = require_constant_value;
4254 p->require_constant_elements = require_constant_elements;
4255 p->constructor_stack = constructor_stack;
4256 p->constructor_range_stack = constructor_range_stack;
4257 p->elements = constructor_elements;
4258 p->spelling = spelling;
4259 p->spelling_base = spelling_base;
4260 p->spelling_size = spelling_size;
4261 p->top_level = constructor_top_level;
4262 p->next = initializer_stack;
4263 initializer_stack = p;
4265 constructor_decl = decl;
4266 constructor_asmspec = asmspec;
4267 constructor_designated = 0;
4268 constructor_top_level = top_level;
4270 if (decl != 0)
4272 require_constant_value = TREE_STATIC (decl);
4273 require_constant_elements
4274 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4275 /* For a scalar, you can always use any value to initialize,
4276 even within braces. */
4277 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4278 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4279 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4280 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4281 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4283 else
4285 require_constant_value = 0;
4286 require_constant_elements = 0;
4287 locus = "(anonymous)";
4290 constructor_stack = 0;
4291 constructor_range_stack = 0;
4293 missing_braces_mentioned = 0;
4295 spelling_base = 0;
4296 spelling_size = 0;
4297 RESTORE_SPELLING_DEPTH (0);
4299 if (locus)
4300 push_string (locus);
4303 void
4304 finish_init (void)
4306 struct initializer_stack *p = initializer_stack;
4308 /* Free the whole constructor stack of this initializer. */
4309 while (constructor_stack)
4311 struct constructor_stack *q = constructor_stack;
4312 constructor_stack = q->next;
4313 free (q);
4316 if (constructor_range_stack)
4317 abort ();
4319 /* Pop back to the data of the outer initializer (if any). */
4320 free (spelling_base);
4322 constructor_decl = p->decl;
4323 constructor_asmspec = p->asmspec;
4324 require_constant_value = p->require_constant_value;
4325 require_constant_elements = p->require_constant_elements;
4326 constructor_stack = p->constructor_stack;
4327 constructor_range_stack = p->constructor_range_stack;
4328 constructor_elements = p->elements;
4329 spelling = p->spelling;
4330 spelling_base = p->spelling_base;
4331 spelling_size = p->spelling_size;
4332 constructor_top_level = p->top_level;
4333 initializer_stack = p->next;
4334 free (p);
4337 /* Call here when we see the initializer is surrounded by braces.
4338 This is instead of a call to push_init_level;
4339 it is matched by a call to pop_init_level.
4341 TYPE is the type to initialize, for a constructor expression.
4342 For an initializer for a decl, TYPE is zero. */
4344 void
4345 really_start_incremental_init (tree type)
4347 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4349 if (type == 0)
4350 type = TREE_TYPE (constructor_decl);
4352 if (targetm.vector_opaque_p (type))
4353 error ("opaque vector types cannot be initialized");
4355 p->type = constructor_type;
4356 p->fields = constructor_fields;
4357 p->index = constructor_index;
4358 p->max_index = constructor_max_index;
4359 p->unfilled_index = constructor_unfilled_index;
4360 p->unfilled_fields = constructor_unfilled_fields;
4361 p->bit_index = constructor_bit_index;
4362 p->elements = constructor_elements;
4363 p->constant = constructor_constant;
4364 p->simple = constructor_simple;
4365 p->erroneous = constructor_erroneous;
4366 p->pending_elts = constructor_pending_elts;
4367 p->depth = constructor_depth;
4368 p->replacement_value = 0;
4369 p->implicit = 0;
4370 p->range_stack = 0;
4371 p->outer = 0;
4372 p->incremental = constructor_incremental;
4373 p->designated = constructor_designated;
4374 p->next = 0;
4375 constructor_stack = p;
4377 constructor_constant = 1;
4378 constructor_simple = 1;
4379 constructor_depth = SPELLING_DEPTH ();
4380 constructor_elements = 0;
4381 constructor_pending_elts = 0;
4382 constructor_type = type;
4383 constructor_incremental = 1;
4384 constructor_designated = 0;
4385 designator_depth = 0;
4386 designator_errorneous = 0;
4388 if (TREE_CODE (constructor_type) == RECORD_TYPE
4389 || TREE_CODE (constructor_type) == UNION_TYPE)
4391 constructor_fields = TYPE_FIELDS (constructor_type);
4392 /* Skip any nameless bit fields at the beginning. */
4393 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4394 && DECL_NAME (constructor_fields) == 0)
4395 constructor_fields = TREE_CHAIN (constructor_fields);
4397 constructor_unfilled_fields = constructor_fields;
4398 constructor_bit_index = bitsize_zero_node;
4400 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4402 if (TYPE_DOMAIN (constructor_type))
4404 constructor_max_index
4405 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4407 /* Detect non-empty initializations of zero-length arrays. */
4408 if (constructor_max_index == NULL_TREE
4409 && TYPE_SIZE (constructor_type))
4410 constructor_max_index = build_int_2 (-1, -1);
4412 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4413 to initialize VLAs will cause a proper error; avoid tree
4414 checking errors as well by setting a safe value. */
4415 if (constructor_max_index
4416 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4417 constructor_max_index = build_int_2 (-1, -1);
4419 constructor_index
4420 = convert (bitsizetype,
4421 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4423 else
4424 constructor_index = bitsize_zero_node;
4426 constructor_unfilled_index = constructor_index;
4428 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4430 /* Vectors are like simple fixed-size arrays. */
4431 constructor_max_index =
4432 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4433 constructor_index = convert (bitsizetype, bitsize_zero_node);
4434 constructor_unfilled_index = constructor_index;
4436 else
4438 /* Handle the case of int x = {5}; */
4439 constructor_fields = constructor_type;
4440 constructor_unfilled_fields = constructor_type;
4444 /* Push down into a subobject, for initialization.
4445 If this is for an explicit set of braces, IMPLICIT is 0.
4446 If it is because the next element belongs at a lower level,
4447 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4449 void
4450 push_init_level (int implicit)
4452 struct constructor_stack *p;
4453 tree value = NULL_TREE;
4455 /* If we've exhausted any levels that didn't have braces,
4456 pop them now. */
4457 while (constructor_stack->implicit)
4459 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4460 || TREE_CODE (constructor_type) == UNION_TYPE)
4461 && constructor_fields == 0)
4462 process_init_element (pop_init_level (1));
4463 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4464 && constructor_max_index
4465 && tree_int_cst_lt (constructor_max_index, constructor_index))
4466 process_init_element (pop_init_level (1));
4467 else
4468 break;
4471 /* Unless this is an explicit brace, we need to preserve previous
4472 content if any. */
4473 if (implicit)
4475 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4476 || TREE_CODE (constructor_type) == UNION_TYPE)
4477 && constructor_fields)
4478 value = find_init_member (constructor_fields);
4479 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4480 value = find_init_member (constructor_index);
4483 p = xmalloc (sizeof (struct constructor_stack));
4484 p->type = constructor_type;
4485 p->fields = constructor_fields;
4486 p->index = constructor_index;
4487 p->max_index = constructor_max_index;
4488 p->unfilled_index = constructor_unfilled_index;
4489 p->unfilled_fields = constructor_unfilled_fields;
4490 p->bit_index = constructor_bit_index;
4491 p->elements = constructor_elements;
4492 p->constant = constructor_constant;
4493 p->simple = constructor_simple;
4494 p->erroneous = constructor_erroneous;
4495 p->pending_elts = constructor_pending_elts;
4496 p->depth = constructor_depth;
4497 p->replacement_value = 0;
4498 p->implicit = implicit;
4499 p->outer = 0;
4500 p->incremental = constructor_incremental;
4501 p->designated = constructor_designated;
4502 p->next = constructor_stack;
4503 p->range_stack = 0;
4504 constructor_stack = p;
4506 constructor_constant = 1;
4507 constructor_simple = 1;
4508 constructor_depth = SPELLING_DEPTH ();
4509 constructor_elements = 0;
4510 constructor_incremental = 1;
4511 constructor_designated = 0;
4512 constructor_pending_elts = 0;
4513 if (!implicit)
4515 p->range_stack = constructor_range_stack;
4516 constructor_range_stack = 0;
4517 designator_depth = 0;
4518 designator_errorneous = 0;
4521 /* Don't die if an entire brace-pair level is superfluous
4522 in the containing level. */
4523 if (constructor_type == 0)
4525 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4526 || TREE_CODE (constructor_type) == UNION_TYPE)
4528 /* Don't die if there are extra init elts at the end. */
4529 if (constructor_fields == 0)
4530 constructor_type = 0;
4531 else
4533 constructor_type = TREE_TYPE (constructor_fields);
4534 push_member_name (constructor_fields);
4535 constructor_depth++;
4538 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4540 constructor_type = TREE_TYPE (constructor_type);
4541 push_array_bounds (tree_low_cst (constructor_index, 0));
4542 constructor_depth++;
4545 if (constructor_type == 0)
4547 error_init ("extra brace group at end of initializer");
4548 constructor_fields = 0;
4549 constructor_unfilled_fields = 0;
4550 return;
4553 if (value && TREE_CODE (value) == CONSTRUCTOR)
4555 constructor_constant = TREE_CONSTANT (value);
4556 constructor_simple = TREE_STATIC (value);
4557 constructor_elements = CONSTRUCTOR_ELTS (value);
4558 if (constructor_elements
4559 && (TREE_CODE (constructor_type) == RECORD_TYPE
4560 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4561 set_nonincremental_init ();
4564 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4566 missing_braces_mentioned = 1;
4567 warning_init ("missing braces around initializer");
4570 if (TREE_CODE (constructor_type) == RECORD_TYPE
4571 || TREE_CODE (constructor_type) == UNION_TYPE)
4573 constructor_fields = TYPE_FIELDS (constructor_type);
4574 /* Skip any nameless bit fields at the beginning. */
4575 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4576 && DECL_NAME (constructor_fields) == 0)
4577 constructor_fields = TREE_CHAIN (constructor_fields);
4579 constructor_unfilled_fields = constructor_fields;
4580 constructor_bit_index = bitsize_zero_node;
4582 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4584 /* Vectors are like simple fixed-size arrays. */
4585 constructor_max_index =
4586 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4587 constructor_index = convert (bitsizetype, integer_zero_node);
4588 constructor_unfilled_index = constructor_index;
4590 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4592 if (TYPE_DOMAIN (constructor_type))
4594 constructor_max_index
4595 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4597 /* Detect non-empty initializations of zero-length arrays. */
4598 if (constructor_max_index == NULL_TREE
4599 && TYPE_SIZE (constructor_type))
4600 constructor_max_index = build_int_2 (-1, -1);
4602 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4603 to initialize VLAs will cause a proper error; avoid tree
4604 checking errors as well by setting a safe value. */
4605 if (constructor_max_index
4606 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4607 constructor_max_index = build_int_2 (-1, -1);
4609 constructor_index
4610 = convert (bitsizetype,
4611 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4613 else
4614 constructor_index = bitsize_zero_node;
4616 constructor_unfilled_index = constructor_index;
4617 if (value && TREE_CODE (value) == STRING_CST)
4619 /* We need to split the char/wchar array into individual
4620 characters, so that we don't have to special case it
4621 everywhere. */
4622 set_nonincremental_init_from_string (value);
4625 else
4627 warning_init ("braces around scalar initializer");
4628 constructor_fields = constructor_type;
4629 constructor_unfilled_fields = constructor_type;
4633 /* At the end of an implicit or explicit brace level,
4634 finish up that level of constructor.
4635 If we were outputting the elements as they are read, return 0
4636 from inner levels (process_init_element ignores that),
4637 but return error_mark_node from the outermost level
4638 (that's what we want to put in DECL_INITIAL).
4639 Otherwise, return a CONSTRUCTOR expression. */
4641 tree
4642 pop_init_level (int implicit)
4644 struct constructor_stack *p;
4645 tree constructor = 0;
4647 if (implicit == 0)
4649 /* When we come to an explicit close brace,
4650 pop any inner levels that didn't have explicit braces. */
4651 while (constructor_stack->implicit)
4652 process_init_element (pop_init_level (1));
4654 if (constructor_range_stack)
4655 abort ();
4658 p = constructor_stack;
4660 /* Error for initializing a flexible array member, or a zero-length
4661 array member in an inappropriate context. */
4662 if (constructor_type && constructor_fields
4663 && TREE_CODE (constructor_type) == ARRAY_TYPE
4664 && TYPE_DOMAIN (constructor_type)
4665 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4667 /* Silently discard empty initializations. The parser will
4668 already have pedwarned for empty brackets. */
4669 if (integer_zerop (constructor_unfilled_index))
4670 constructor_type = NULL_TREE;
4671 else if (! TYPE_SIZE (constructor_type))
4673 if (constructor_depth > 2)
4674 error_init ("initialization of flexible array member in a nested context");
4675 else if (pedantic)
4676 pedwarn_init ("initialization of a flexible array member");
4678 /* We have already issued an error message for the existence
4679 of a flexible array member not at the end of the structure.
4680 Discard the initializer so that we do not abort later. */
4681 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4682 constructor_type = NULL_TREE;
4684 else
4685 /* Zero-length arrays are no longer special, so we should no longer
4686 get here. */
4687 abort ();
4690 /* Warn when some struct elements are implicitly initialized to zero. */
4691 if (extra_warnings
4692 && constructor_type
4693 && TREE_CODE (constructor_type) == RECORD_TYPE
4694 && constructor_unfilled_fields)
4696 /* Do not warn for flexible array members or zero-length arrays. */
4697 while (constructor_unfilled_fields
4698 && (! DECL_SIZE (constructor_unfilled_fields)
4699 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4700 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4702 /* Do not warn if this level of the initializer uses member
4703 designators; it is likely to be deliberate. */
4704 if (constructor_unfilled_fields && !constructor_designated)
4706 push_member_name (constructor_unfilled_fields);
4707 warning_init ("missing initializer");
4708 RESTORE_SPELLING_DEPTH (constructor_depth);
4712 /* Now output all pending elements. */
4713 constructor_incremental = 1;
4714 output_pending_init_elements (1);
4716 /* Pad out the end of the structure. */
4717 if (p->replacement_value)
4718 /* If this closes a superfluous brace pair,
4719 just pass out the element between them. */
4720 constructor = p->replacement_value;
4721 else if (constructor_type == 0)
4723 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4724 && TREE_CODE (constructor_type) != UNION_TYPE
4725 && TREE_CODE (constructor_type) != ARRAY_TYPE
4726 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4728 /* A nonincremental scalar initializer--just return
4729 the element, after verifying there is just one. */
4730 if (constructor_elements == 0)
4732 if (!constructor_erroneous)
4733 error_init ("empty scalar initializer");
4734 constructor = error_mark_node;
4736 else if (TREE_CHAIN (constructor_elements) != 0)
4738 error_init ("extra elements in scalar initializer");
4739 constructor = TREE_VALUE (constructor_elements);
4741 else
4742 constructor = TREE_VALUE (constructor_elements);
4744 else
4746 if (constructor_erroneous)
4747 constructor = error_mark_node;
4748 else
4750 constructor = build_constructor (constructor_type,
4751 nreverse (constructor_elements));
4752 if (constructor_constant)
4753 TREE_CONSTANT (constructor) = 1;
4754 if (constructor_constant && constructor_simple)
4755 TREE_STATIC (constructor) = 1;
4759 constructor_type = p->type;
4760 constructor_fields = p->fields;
4761 constructor_index = p->index;
4762 constructor_max_index = p->max_index;
4763 constructor_unfilled_index = p->unfilled_index;
4764 constructor_unfilled_fields = p->unfilled_fields;
4765 constructor_bit_index = p->bit_index;
4766 constructor_elements = p->elements;
4767 constructor_constant = p->constant;
4768 constructor_simple = p->simple;
4769 constructor_erroneous = p->erroneous;
4770 constructor_incremental = p->incremental;
4771 constructor_designated = p->designated;
4772 constructor_pending_elts = p->pending_elts;
4773 constructor_depth = p->depth;
4774 if (!p->implicit)
4775 constructor_range_stack = p->range_stack;
4776 RESTORE_SPELLING_DEPTH (constructor_depth);
4778 constructor_stack = p->next;
4779 free (p);
4781 if (constructor == 0)
4783 if (constructor_stack == 0)
4784 return error_mark_node;
4785 return NULL_TREE;
4787 return constructor;
4790 /* Common handling for both array range and field name designators.
4791 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4793 static int
4794 set_designator (int array)
4796 tree subtype;
4797 enum tree_code subcode;
4799 /* Don't die if an entire brace-pair level is superfluous
4800 in the containing level. */
4801 if (constructor_type == 0)
4802 return 1;
4804 /* If there were errors in this designator list already, bail out silently. */
4805 if (designator_errorneous)
4806 return 1;
4808 if (!designator_depth)
4810 if (constructor_range_stack)
4811 abort ();
4813 /* Designator list starts at the level of closest explicit
4814 braces. */
4815 while (constructor_stack->implicit)
4816 process_init_element (pop_init_level (1));
4817 constructor_designated = 1;
4818 return 0;
4821 if (constructor_no_implicit)
4823 error_init ("initialization designators may not nest");
4824 return 1;
4827 if (TREE_CODE (constructor_type) == RECORD_TYPE
4828 || TREE_CODE (constructor_type) == UNION_TYPE)
4830 subtype = TREE_TYPE (constructor_fields);
4831 if (subtype != error_mark_node)
4832 subtype = TYPE_MAIN_VARIANT (subtype);
4834 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4836 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4838 else
4839 abort ();
4841 subcode = TREE_CODE (subtype);
4842 if (array && subcode != ARRAY_TYPE)
4844 error_init ("array index in non-array initializer");
4845 return 1;
4847 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4849 error_init ("field name not in record or union initializer");
4850 return 1;
4853 constructor_designated = 1;
4854 push_init_level (2);
4855 return 0;
4858 /* If there are range designators in designator list, push a new designator
4859 to constructor_range_stack. RANGE_END is end of such stack range or
4860 NULL_TREE if there is no range designator at this level. */
4862 static void
4863 push_range_stack (tree range_end)
4865 struct constructor_range_stack *p;
4867 p = ggc_alloc (sizeof (struct constructor_range_stack));
4868 p->prev = constructor_range_stack;
4869 p->next = 0;
4870 p->fields = constructor_fields;
4871 p->range_start = constructor_index;
4872 p->index = constructor_index;
4873 p->stack = constructor_stack;
4874 p->range_end = range_end;
4875 if (constructor_range_stack)
4876 constructor_range_stack->next = p;
4877 constructor_range_stack = p;
4880 /* Within an array initializer, specify the next index to be initialized.
4881 FIRST is that index. If LAST is nonzero, then initialize a range
4882 of indices, running from FIRST through LAST. */
4884 void
4885 set_init_index (tree first, tree last)
4887 if (set_designator (1))
4888 return;
4890 designator_errorneous = 1;
4892 while ((TREE_CODE (first) == NOP_EXPR
4893 || TREE_CODE (first) == CONVERT_EXPR
4894 || TREE_CODE (first) == NON_LVALUE_EXPR)
4895 && (TYPE_MODE (TREE_TYPE (first))
4896 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4897 first = TREE_OPERAND (first, 0);
4899 if (last)
4900 while ((TREE_CODE (last) == NOP_EXPR
4901 || TREE_CODE (last) == CONVERT_EXPR
4902 || TREE_CODE (last) == NON_LVALUE_EXPR)
4903 && (TYPE_MODE (TREE_TYPE (last))
4904 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4905 last = TREE_OPERAND (last, 0);
4907 if (TREE_CODE (first) != INTEGER_CST)
4908 error_init ("nonconstant array index in initializer");
4909 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
4910 error_init ("nonconstant array index in initializer");
4911 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
4912 error_init ("array index in non-array initializer");
4913 else if (tree_int_cst_sgn (first) == -1)
4914 error_init ("array index in initializer exceeds array bounds");
4915 else if (constructor_max_index
4916 && tree_int_cst_lt (constructor_max_index, first))
4917 error_init ("array index in initializer exceeds array bounds");
4918 else
4920 constructor_index = convert (bitsizetype, first);
4922 if (last)
4924 if (tree_int_cst_equal (first, last))
4925 last = 0;
4926 else if (tree_int_cst_lt (last, first))
4928 error_init ("empty index range in initializer");
4929 last = 0;
4931 else
4933 last = convert (bitsizetype, last);
4934 if (constructor_max_index != 0
4935 && tree_int_cst_lt (constructor_max_index, last))
4937 error_init ("array index range in initializer exceeds array bounds");
4938 last = 0;
4943 designator_depth++;
4944 designator_errorneous = 0;
4945 if (constructor_range_stack || last)
4946 push_range_stack (last);
4950 /* Within a struct initializer, specify the next field to be initialized. */
4952 void
4953 set_init_label (tree fieldname)
4955 tree tail;
4957 if (set_designator (0))
4958 return;
4960 designator_errorneous = 1;
4962 if (TREE_CODE (constructor_type) != RECORD_TYPE
4963 && TREE_CODE (constructor_type) != UNION_TYPE)
4965 error_init ("field name not in record or union initializer");
4966 return;
4969 for (tail = TYPE_FIELDS (constructor_type); tail;
4970 tail = TREE_CHAIN (tail))
4972 if (DECL_NAME (tail) == fieldname)
4973 break;
4976 if (tail == 0)
4977 error ("unknown field `%s' specified in initializer",
4978 IDENTIFIER_POINTER (fieldname));
4979 else
4981 constructor_fields = tail;
4982 designator_depth++;
4983 designator_errorneous = 0;
4984 if (constructor_range_stack)
4985 push_range_stack (NULL_TREE);
4989 /* Add a new initializer to the tree of pending initializers. PURPOSE
4990 identifies the initializer, either array index or field in a structure.
4991 VALUE is the value of that index or field. */
4993 static void
4994 add_pending_init (tree purpose, tree value)
4996 struct init_node *p, **q, *r;
4998 q = &constructor_pending_elts;
4999 p = 0;
5001 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5003 while (*q != 0)
5005 p = *q;
5006 if (tree_int_cst_lt (purpose, p->purpose))
5007 q = &p->left;
5008 else if (tree_int_cst_lt (p->purpose, purpose))
5009 q = &p->right;
5010 else
5012 if (TREE_SIDE_EFFECTS (p->value))
5013 warning_init ("initialized field with side-effects overwritten");
5014 p->value = value;
5015 return;
5019 else
5021 tree bitpos;
5023 bitpos = bit_position (purpose);
5024 while (*q != NULL)
5026 p = *q;
5027 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5028 q = &p->left;
5029 else if (p->purpose != purpose)
5030 q = &p->right;
5031 else
5033 if (TREE_SIDE_EFFECTS (p->value))
5034 warning_init ("initialized field with side-effects overwritten");
5035 p->value = value;
5036 return;
5041 r = ggc_alloc (sizeof (struct init_node));
5042 r->purpose = purpose;
5043 r->value = value;
5045 *q = r;
5046 r->parent = p;
5047 r->left = 0;
5048 r->right = 0;
5049 r->balance = 0;
5051 while (p)
5053 struct init_node *s;
5055 if (r == p->left)
5057 if (p->balance == 0)
5058 p->balance = -1;
5059 else if (p->balance < 0)
5061 if (r->balance < 0)
5063 /* L rotation. */
5064 p->left = r->right;
5065 if (p->left)
5066 p->left->parent = p;
5067 r->right = p;
5069 p->balance = 0;
5070 r->balance = 0;
5072 s = p->parent;
5073 p->parent = r;
5074 r->parent = s;
5075 if (s)
5077 if (s->left == p)
5078 s->left = r;
5079 else
5080 s->right = r;
5082 else
5083 constructor_pending_elts = r;
5085 else
5087 /* LR rotation. */
5088 struct init_node *t = r->right;
5090 r->right = t->left;
5091 if (r->right)
5092 r->right->parent = r;
5093 t->left = r;
5095 p->left = t->right;
5096 if (p->left)
5097 p->left->parent = p;
5098 t->right = p;
5100 p->balance = t->balance < 0;
5101 r->balance = -(t->balance > 0);
5102 t->balance = 0;
5104 s = p->parent;
5105 p->parent = t;
5106 r->parent = t;
5107 t->parent = s;
5108 if (s)
5110 if (s->left == p)
5111 s->left = t;
5112 else
5113 s->right = t;
5115 else
5116 constructor_pending_elts = t;
5118 break;
5120 else
5122 /* p->balance == +1; growth of left side balances the node. */
5123 p->balance = 0;
5124 break;
5127 else /* r == p->right */
5129 if (p->balance == 0)
5130 /* Growth propagation from right side. */
5131 p->balance++;
5132 else if (p->balance > 0)
5134 if (r->balance > 0)
5136 /* R rotation. */
5137 p->right = r->left;
5138 if (p->right)
5139 p->right->parent = p;
5140 r->left = p;
5142 p->balance = 0;
5143 r->balance = 0;
5145 s = p->parent;
5146 p->parent = r;
5147 r->parent = s;
5148 if (s)
5150 if (s->left == p)
5151 s->left = r;
5152 else
5153 s->right = r;
5155 else
5156 constructor_pending_elts = r;
5158 else /* r->balance == -1 */
5160 /* RL rotation */
5161 struct init_node *t = r->left;
5163 r->left = t->right;
5164 if (r->left)
5165 r->left->parent = r;
5166 t->right = r;
5168 p->right = t->left;
5169 if (p->right)
5170 p->right->parent = p;
5171 t->left = p;
5173 r->balance = (t->balance < 0);
5174 p->balance = -(t->balance > 0);
5175 t->balance = 0;
5177 s = p->parent;
5178 p->parent = t;
5179 r->parent = t;
5180 t->parent = s;
5181 if (s)
5183 if (s->left == p)
5184 s->left = t;
5185 else
5186 s->right = t;
5188 else
5189 constructor_pending_elts = t;
5191 break;
5193 else
5195 /* p->balance == -1; growth of right side balances the node. */
5196 p->balance = 0;
5197 break;
5201 r = p;
5202 p = p->parent;
5206 /* Build AVL tree from a sorted chain. */
5208 static void
5209 set_nonincremental_init (void)
5211 tree chain;
5213 if (TREE_CODE (constructor_type) != RECORD_TYPE
5214 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5215 return;
5217 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5218 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5219 constructor_elements = 0;
5220 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5222 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5223 /* Skip any nameless bit fields at the beginning. */
5224 while (constructor_unfilled_fields != 0
5225 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5226 && DECL_NAME (constructor_unfilled_fields) == 0)
5227 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5230 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5232 if (TYPE_DOMAIN (constructor_type))
5233 constructor_unfilled_index
5234 = convert (bitsizetype,
5235 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5236 else
5237 constructor_unfilled_index = bitsize_zero_node;
5239 constructor_incremental = 0;
5242 /* Build AVL tree from a string constant. */
5244 static void
5245 set_nonincremental_init_from_string (tree str)
5247 tree value, purpose, type;
5248 HOST_WIDE_INT val[2];
5249 const char *p, *end;
5250 int byte, wchar_bytes, charwidth, bitpos;
5252 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5253 abort ();
5255 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5256 == TYPE_PRECISION (char_type_node))
5257 wchar_bytes = 1;
5258 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5259 == TYPE_PRECISION (wchar_type_node))
5260 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5261 else
5262 abort ();
5264 charwidth = TYPE_PRECISION (char_type_node);
5265 type = TREE_TYPE (constructor_type);
5266 p = TREE_STRING_POINTER (str);
5267 end = p + TREE_STRING_LENGTH (str);
5269 for (purpose = bitsize_zero_node;
5270 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5271 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5273 if (wchar_bytes == 1)
5275 val[1] = (unsigned char) *p++;
5276 val[0] = 0;
5278 else
5280 val[0] = 0;
5281 val[1] = 0;
5282 for (byte = 0; byte < wchar_bytes; byte++)
5284 if (BYTES_BIG_ENDIAN)
5285 bitpos = (wchar_bytes - byte - 1) * charwidth;
5286 else
5287 bitpos = byte * charwidth;
5288 val[bitpos < HOST_BITS_PER_WIDE_INT]
5289 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5290 << (bitpos % HOST_BITS_PER_WIDE_INT);
5294 if (!TREE_UNSIGNED (type))
5296 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5297 if (bitpos < HOST_BITS_PER_WIDE_INT)
5299 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5301 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5302 val[0] = -1;
5305 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5307 if (val[1] < 0)
5308 val[0] = -1;
5310 else if (val[0] & (((HOST_WIDE_INT) 1)
5311 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5312 val[0] |= ((HOST_WIDE_INT) -1)
5313 << (bitpos - HOST_BITS_PER_WIDE_INT);
5316 value = build_int_2 (val[1], val[0]);
5317 TREE_TYPE (value) = type;
5318 add_pending_init (purpose, value);
5321 constructor_incremental = 0;
5324 /* Return value of FIELD in pending initializer or zero if the field was
5325 not initialized yet. */
5327 static tree
5328 find_init_member (tree field)
5330 struct init_node *p;
5332 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5334 if (constructor_incremental
5335 && tree_int_cst_lt (field, constructor_unfilled_index))
5336 set_nonincremental_init ();
5338 p = constructor_pending_elts;
5339 while (p)
5341 if (tree_int_cst_lt (field, p->purpose))
5342 p = p->left;
5343 else if (tree_int_cst_lt (p->purpose, field))
5344 p = p->right;
5345 else
5346 return p->value;
5349 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5351 tree bitpos = bit_position (field);
5353 if (constructor_incremental
5354 && (!constructor_unfilled_fields
5355 || tree_int_cst_lt (bitpos,
5356 bit_position (constructor_unfilled_fields))))
5357 set_nonincremental_init ();
5359 p = constructor_pending_elts;
5360 while (p)
5362 if (field == p->purpose)
5363 return p->value;
5364 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5365 p = p->left;
5366 else
5367 p = p->right;
5370 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5372 if (constructor_elements
5373 && TREE_PURPOSE (constructor_elements) == field)
5374 return TREE_VALUE (constructor_elements);
5376 return 0;
5379 /* "Output" the next constructor element.
5380 At top level, really output it to assembler code now.
5381 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5382 TYPE is the data type that the containing data type wants here.
5383 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5385 PENDING if non-nil means output pending elements that belong
5386 right after this element. (PENDING is normally 1;
5387 it is 0 while outputting pending elements, to avoid recursion.) */
5389 static void
5390 output_init_element (tree value, tree type, tree field, int pending)
5392 if (type == error_mark_node)
5394 constructor_erroneous = 1;
5395 return;
5397 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5398 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5399 && !(TREE_CODE (value) == STRING_CST
5400 && TREE_CODE (type) == ARRAY_TYPE
5401 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5402 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5403 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
5404 value = default_conversion (value);
5406 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5407 && require_constant_value && !flag_isoc99 && pending)
5409 /* As an extension, allow initializing objects with static storage
5410 duration with compound literals (which are then treated just as
5411 the brace enclosed list they contain). */
5412 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5413 value = DECL_INITIAL (decl);
5416 if (value == error_mark_node)
5417 constructor_erroneous = 1;
5418 else if (!TREE_CONSTANT (value))
5419 constructor_constant = 0;
5420 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5421 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5422 || TREE_CODE (constructor_type) == UNION_TYPE)
5423 && DECL_C_BIT_FIELD (field)
5424 && TREE_CODE (value) != INTEGER_CST))
5425 constructor_simple = 0;
5427 if (require_constant_value && ! TREE_CONSTANT (value))
5429 error_init ("initializer element is not constant");
5430 value = error_mark_node;
5432 else if (require_constant_elements
5433 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5434 pedwarn ("initializer element is not computable at load time");
5436 /* If this field is empty (and not at the end of structure),
5437 don't do anything other than checking the initializer. */
5438 if (field
5439 && (TREE_TYPE (field) == error_mark_node
5440 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5441 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5442 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5443 || TREE_CHAIN (field)))))
5444 return;
5446 value = digest_init (type, value, require_constant_value);
5447 if (value == error_mark_node)
5449 constructor_erroneous = 1;
5450 return;
5453 /* If this element doesn't come next in sequence,
5454 put it on constructor_pending_elts. */
5455 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5456 && (!constructor_incremental
5457 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5459 if (constructor_incremental
5460 && tree_int_cst_lt (field, constructor_unfilled_index))
5461 set_nonincremental_init ();
5463 add_pending_init (field, value);
5464 return;
5466 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5467 && (!constructor_incremental
5468 || field != constructor_unfilled_fields))
5470 /* We do this for records but not for unions. In a union,
5471 no matter which field is specified, it can be initialized
5472 right away since it starts at the beginning of the union. */
5473 if (constructor_incremental)
5475 if (!constructor_unfilled_fields)
5476 set_nonincremental_init ();
5477 else
5479 tree bitpos, unfillpos;
5481 bitpos = bit_position (field);
5482 unfillpos = bit_position (constructor_unfilled_fields);
5484 if (tree_int_cst_lt (bitpos, unfillpos))
5485 set_nonincremental_init ();
5489 add_pending_init (field, value);
5490 return;
5492 else if (TREE_CODE (constructor_type) == UNION_TYPE
5493 && constructor_elements)
5495 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5496 warning_init ("initialized field with side-effects overwritten");
5498 /* We can have just one union field set. */
5499 constructor_elements = 0;
5502 /* Otherwise, output this element either to
5503 constructor_elements or to the assembler file. */
5505 if (field && TREE_CODE (field) == INTEGER_CST)
5506 field = copy_node (field);
5507 constructor_elements
5508 = tree_cons (field, value, constructor_elements);
5510 /* Advance the variable that indicates sequential elements output. */
5511 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5512 constructor_unfilled_index
5513 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5514 bitsize_one_node);
5515 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5517 constructor_unfilled_fields
5518 = TREE_CHAIN (constructor_unfilled_fields);
5520 /* Skip any nameless bit fields. */
5521 while (constructor_unfilled_fields != 0
5522 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5523 && DECL_NAME (constructor_unfilled_fields) == 0)
5524 constructor_unfilled_fields =
5525 TREE_CHAIN (constructor_unfilled_fields);
5527 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5528 constructor_unfilled_fields = 0;
5530 /* Now output any pending elements which have become next. */
5531 if (pending)
5532 output_pending_init_elements (0);
5535 /* Output any pending elements which have become next.
5536 As we output elements, constructor_unfilled_{fields,index}
5537 advances, which may cause other elements to become next;
5538 if so, they too are output.
5540 If ALL is 0, we return when there are
5541 no more pending elements to output now.
5543 If ALL is 1, we output space as necessary so that
5544 we can output all the pending elements. */
5546 static void
5547 output_pending_init_elements (int all)
5549 struct init_node *elt = constructor_pending_elts;
5550 tree next;
5552 retry:
5554 /* Look through the whole pending tree.
5555 If we find an element that should be output now,
5556 output it. Otherwise, set NEXT to the element
5557 that comes first among those still pending. */
5559 next = 0;
5560 while (elt)
5562 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5564 if (tree_int_cst_equal (elt->purpose,
5565 constructor_unfilled_index))
5566 output_init_element (elt->value,
5567 TREE_TYPE (constructor_type),
5568 constructor_unfilled_index, 0);
5569 else if (tree_int_cst_lt (constructor_unfilled_index,
5570 elt->purpose))
5572 /* Advance to the next smaller node. */
5573 if (elt->left)
5574 elt = elt->left;
5575 else
5577 /* We have reached the smallest node bigger than the
5578 current unfilled index. Fill the space first. */
5579 next = elt->purpose;
5580 break;
5583 else
5585 /* Advance to the next bigger node. */
5586 if (elt->right)
5587 elt = elt->right;
5588 else
5590 /* We have reached the biggest node in a subtree. Find
5591 the parent of it, which is the next bigger node. */
5592 while (elt->parent && elt->parent->right == elt)
5593 elt = elt->parent;
5594 elt = elt->parent;
5595 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5596 elt->purpose))
5598 next = elt->purpose;
5599 break;
5604 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5605 || TREE_CODE (constructor_type) == UNION_TYPE)
5607 tree ctor_unfilled_bitpos, elt_bitpos;
5609 /* If the current record is complete we are done. */
5610 if (constructor_unfilled_fields == 0)
5611 break;
5613 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5614 elt_bitpos = bit_position (elt->purpose);
5615 /* We can't compare fields here because there might be empty
5616 fields in between. */
5617 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5619 constructor_unfilled_fields = elt->purpose;
5620 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5621 elt->purpose, 0);
5623 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5625 /* Advance to the next smaller node. */
5626 if (elt->left)
5627 elt = elt->left;
5628 else
5630 /* We have reached the smallest node bigger than the
5631 current unfilled field. Fill the space first. */
5632 next = elt->purpose;
5633 break;
5636 else
5638 /* Advance to the next bigger node. */
5639 if (elt->right)
5640 elt = elt->right;
5641 else
5643 /* We have reached the biggest node in a subtree. Find
5644 the parent of it, which is the next bigger node. */
5645 while (elt->parent && elt->parent->right == elt)
5646 elt = elt->parent;
5647 elt = elt->parent;
5648 if (elt
5649 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5650 bit_position (elt->purpose))))
5652 next = elt->purpose;
5653 break;
5660 /* Ordinarily return, but not if we want to output all
5661 and there are elements left. */
5662 if (! (all && next != 0))
5663 return;
5665 /* If it's not incremental, just skip over the gap, so that after
5666 jumping to retry we will output the next successive element. */
5667 if (TREE_CODE (constructor_type) == RECORD_TYPE
5668 || TREE_CODE (constructor_type) == UNION_TYPE)
5669 constructor_unfilled_fields = next;
5670 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5671 constructor_unfilled_index = next;
5673 /* ELT now points to the node in the pending tree with the next
5674 initializer to output. */
5675 goto retry;
5678 /* Add one non-braced element to the current constructor level.
5679 This adjusts the current position within the constructor's type.
5680 This may also start or terminate implicit levels
5681 to handle a partly-braced initializer.
5683 Once this has found the correct level for the new element,
5684 it calls output_init_element. */
5686 void
5687 process_init_element (tree value)
5689 tree orig_value = value;
5690 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5692 designator_depth = 0;
5693 designator_errorneous = 0;
5695 /* Handle superfluous braces around string cst as in
5696 char x[] = {"foo"}; */
5697 if (string_flag
5698 && constructor_type
5699 && TREE_CODE (constructor_type) == ARRAY_TYPE
5700 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5701 && integer_zerop (constructor_unfilled_index))
5703 if (constructor_stack->replacement_value)
5704 error_init ("excess elements in char array initializer");
5705 constructor_stack->replacement_value = value;
5706 return;
5709 if (constructor_stack->replacement_value != 0)
5711 error_init ("excess elements in struct initializer");
5712 return;
5715 /* Ignore elements of a brace group if it is entirely superfluous
5716 and has already been diagnosed. */
5717 if (constructor_type == 0)
5718 return;
5720 /* If we've exhausted any levels that didn't have braces,
5721 pop them now. */
5722 while (constructor_stack->implicit)
5724 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5725 || TREE_CODE (constructor_type) == UNION_TYPE)
5726 && constructor_fields == 0)
5727 process_init_element (pop_init_level (1));
5728 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5729 && (constructor_max_index == 0
5730 || tree_int_cst_lt (constructor_max_index,
5731 constructor_index)))
5732 process_init_element (pop_init_level (1));
5733 else
5734 break;
5737 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5738 if (constructor_range_stack)
5740 /* If value is a compound literal and we'll be just using its
5741 content, don't put it into a SAVE_EXPR. */
5742 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5743 || !require_constant_value
5744 || flag_isoc99)
5745 value = save_expr (value);
5748 while (1)
5750 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5752 tree fieldtype;
5753 enum tree_code fieldcode;
5755 if (constructor_fields == 0)
5757 pedwarn_init ("excess elements in struct initializer");
5758 break;
5761 fieldtype = TREE_TYPE (constructor_fields);
5762 if (fieldtype != error_mark_node)
5763 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5764 fieldcode = TREE_CODE (fieldtype);
5766 /* Error for non-static initialization of a flexible array member. */
5767 if (fieldcode == ARRAY_TYPE
5768 && !require_constant_value
5769 && TYPE_SIZE (fieldtype) == NULL_TREE
5770 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5772 error_init ("non-static initialization of a flexible array member");
5773 break;
5776 /* Accept a string constant to initialize a subarray. */
5777 if (value != 0
5778 && fieldcode == ARRAY_TYPE
5779 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5780 && string_flag)
5781 value = orig_value;
5782 /* Otherwise, if we have come to a subaggregate,
5783 and we don't have an element of its type, push into it. */
5784 else if (value != 0 && !constructor_no_implicit
5785 && value != error_mark_node
5786 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5787 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5788 || fieldcode == UNION_TYPE))
5790 push_init_level (1);
5791 continue;
5794 if (value)
5796 push_member_name (constructor_fields);
5797 output_init_element (value, fieldtype, constructor_fields, 1);
5798 RESTORE_SPELLING_DEPTH (constructor_depth);
5800 else
5801 /* Do the bookkeeping for an element that was
5802 directly output as a constructor. */
5804 /* For a record, keep track of end position of last field. */
5805 if (DECL_SIZE (constructor_fields))
5806 constructor_bit_index
5807 = size_binop (PLUS_EXPR,
5808 bit_position (constructor_fields),
5809 DECL_SIZE (constructor_fields));
5811 /* If the current field was the first one not yet written out,
5812 it isn't now, so update. */
5813 if (constructor_unfilled_fields == constructor_fields)
5815 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5816 /* Skip any nameless bit fields. */
5817 while (constructor_unfilled_fields != 0
5818 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5819 && DECL_NAME (constructor_unfilled_fields) == 0)
5820 constructor_unfilled_fields =
5821 TREE_CHAIN (constructor_unfilled_fields);
5825 constructor_fields = TREE_CHAIN (constructor_fields);
5826 /* Skip any nameless bit fields at the beginning. */
5827 while (constructor_fields != 0
5828 && DECL_C_BIT_FIELD (constructor_fields)
5829 && DECL_NAME (constructor_fields) == 0)
5830 constructor_fields = TREE_CHAIN (constructor_fields);
5832 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5834 tree fieldtype;
5835 enum tree_code fieldcode;
5837 if (constructor_fields == 0)
5839 pedwarn_init ("excess elements in union initializer");
5840 break;
5843 fieldtype = TREE_TYPE (constructor_fields);
5844 if (fieldtype != error_mark_node)
5845 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5846 fieldcode = TREE_CODE (fieldtype);
5848 /* Warn that traditional C rejects initialization of unions.
5849 We skip the warning if the value is zero. This is done
5850 under the assumption that the zero initializer in user
5851 code appears conditioned on e.g. __STDC__ to avoid
5852 "missing initializer" warnings and relies on default
5853 initialization to zero in the traditional C case.
5854 We also skip the warning if the initializer is designated,
5855 again on the assumption that this must be conditional on
5856 __STDC__ anyway (and we've already complained about the
5857 member-designator already). */
5858 if (warn_traditional && !in_system_header && !constructor_designated
5859 && !(value && (integer_zerop (value) || real_zerop (value))))
5860 warning ("traditional C rejects initialization of unions");
5862 /* Accept a string constant to initialize a subarray. */
5863 if (value != 0
5864 && fieldcode == ARRAY_TYPE
5865 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5866 && string_flag)
5867 value = orig_value;
5868 /* Otherwise, if we have come to a subaggregate,
5869 and we don't have an element of its type, push into it. */
5870 else if (value != 0 && !constructor_no_implicit
5871 && value != error_mark_node
5872 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5873 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5874 || fieldcode == UNION_TYPE))
5876 push_init_level (1);
5877 continue;
5880 if (value)
5882 push_member_name (constructor_fields);
5883 output_init_element (value, fieldtype, constructor_fields, 1);
5884 RESTORE_SPELLING_DEPTH (constructor_depth);
5886 else
5887 /* Do the bookkeeping for an element that was
5888 directly output as a constructor. */
5890 constructor_bit_index = DECL_SIZE (constructor_fields);
5891 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5894 constructor_fields = 0;
5896 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5898 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5899 enum tree_code eltcode = TREE_CODE (elttype);
5901 /* Accept a string constant to initialize a subarray. */
5902 if (value != 0
5903 && eltcode == ARRAY_TYPE
5904 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5905 && string_flag)
5906 value = orig_value;
5907 /* Otherwise, if we have come to a subaggregate,
5908 and we don't have an element of its type, push into it. */
5909 else if (value != 0 && !constructor_no_implicit
5910 && value != error_mark_node
5911 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
5912 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5913 || eltcode == UNION_TYPE))
5915 push_init_level (1);
5916 continue;
5919 if (constructor_max_index != 0
5920 && (tree_int_cst_lt (constructor_max_index, constructor_index)
5921 || integer_all_onesp (constructor_max_index)))
5923 pedwarn_init ("excess elements in array initializer");
5924 break;
5927 /* Now output the actual element. */
5928 if (value)
5930 push_array_bounds (tree_low_cst (constructor_index, 0));
5931 output_init_element (value, elttype, constructor_index, 1);
5932 RESTORE_SPELLING_DEPTH (constructor_depth);
5935 constructor_index
5936 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5938 if (! value)
5939 /* If we are doing the bookkeeping for an element that was
5940 directly output as a constructor, we must update
5941 constructor_unfilled_index. */
5942 constructor_unfilled_index = constructor_index;
5944 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5946 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5948 /* Do a basic check of initializer size. Note that vectors
5949 always have a fixed size derived from their type. */
5950 if (tree_int_cst_lt (constructor_max_index, constructor_index))
5952 pedwarn_init ("excess elements in vector initializer");
5953 break;
5956 /* Now output the actual element. */
5957 if (value)
5958 output_init_element (value, elttype, constructor_index, 1);
5960 constructor_index
5961 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5963 if (! value)
5964 /* If we are doing the bookkeeping for an element that was
5965 directly output as a constructor, we must update
5966 constructor_unfilled_index. */
5967 constructor_unfilled_index = constructor_index;
5970 /* Handle the sole element allowed in a braced initializer
5971 for a scalar variable. */
5972 else if (constructor_fields == 0)
5974 pedwarn_init ("excess elements in scalar initializer");
5975 break;
5977 else
5979 if (value)
5980 output_init_element (value, constructor_type, NULL_TREE, 1);
5981 constructor_fields = 0;
5984 /* Handle range initializers either at this level or anywhere higher
5985 in the designator stack. */
5986 if (constructor_range_stack)
5988 struct constructor_range_stack *p, *range_stack;
5989 int finish = 0;
5991 range_stack = constructor_range_stack;
5992 constructor_range_stack = 0;
5993 while (constructor_stack != range_stack->stack)
5995 if (!constructor_stack->implicit)
5996 abort ();
5997 process_init_element (pop_init_level (1));
5999 for (p = range_stack;
6000 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6001 p = p->prev)
6003 if (!constructor_stack->implicit)
6004 abort ();
6005 process_init_element (pop_init_level (1));
6008 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6009 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6010 finish = 1;
6012 while (1)
6014 constructor_index = p->index;
6015 constructor_fields = p->fields;
6016 if (finish && p->range_end && p->index == p->range_start)
6018 finish = 0;
6019 p->prev = 0;
6021 p = p->next;
6022 if (!p)
6023 break;
6024 push_init_level (2);
6025 p->stack = constructor_stack;
6026 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6027 p->index = p->range_start;
6030 if (!finish)
6031 constructor_range_stack = range_stack;
6032 continue;
6035 break;
6038 constructor_range_stack = 0;
6041 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6042 (guaranteed to be 'volatile' or null) and ARGS (represented using
6043 an ASM_STMT node). */
6044 tree
6045 build_asm_stmt (tree cv_qualifier, tree args)
6047 if (!TREE_OPERAND (args, 0))
6048 TREE_OPERAND (args, 0) = cv_qualifier;
6049 return add_stmt (args);
6052 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6053 some INPUTS, and some CLOBBERS. The latter three may be NULL.
6054 SIMPLE indicates whether there was anything at all after the
6055 string in the asm expression -- asm("blah") and asm("blah" : )
6056 are subtly different. We use a ASM_STMT node to represent this. */
6057 tree
6058 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6059 bool simple)
6061 tree tail;
6062 tree args;
6064 /* We can remove output conversions that change the type,
6065 but not the mode. */
6066 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6068 tree output = TREE_VALUE (tail);
6070 STRIP_NOPS (output);
6071 TREE_VALUE (tail) = output;
6073 /* Allow conversions as LHS here. build_modify_expr as called below
6074 will do the right thing with them. */
6075 while (TREE_CODE (output) == NOP_EXPR
6076 || TREE_CODE (output) == CONVERT_EXPR
6077 || TREE_CODE (output) == FLOAT_EXPR
6078 || TREE_CODE (output) == FIX_TRUNC_EXPR
6079 || TREE_CODE (output) == FIX_FLOOR_EXPR
6080 || TREE_CODE (output) == FIX_ROUND_EXPR
6081 || TREE_CODE (output) == FIX_CEIL_EXPR)
6082 output = TREE_OPERAND (output, 0);
6084 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6087 /* Remove output conversions that change the type but not the mode. */
6088 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6090 tree output = TREE_VALUE (tail);
6091 STRIP_NOPS (output);
6092 TREE_VALUE (tail) = output;
6095 /* Perform default conversions on array and function inputs.
6096 Don't do this for other types as it would screw up operands
6097 expected to be in memory. */
6098 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6099 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6101 args = build_stmt (ASM_STMT, 0, string, outputs, inputs, clobbers);
6103 /* Simple asm statements are treated as volatile. */
6104 if (simple)
6106 TREE_OPERAND (args, 0) = ridpointers[RID_VOLATILE];
6107 ASM_INPUT_P (args) = 1;
6109 return args;
6112 /* Expand an ASM statement with operands, handling output operands
6113 that are not variables or INDIRECT_REFS by transforming such
6114 cases into cases that expand_asm_operands can handle.
6116 Arguments are same as for expand_asm_operands. */
6118 void
6119 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6120 tree clobbers, int vol, location_t locus)
6122 int noutputs = list_length (outputs);
6123 int i;
6124 /* o[I] is the place that output number I should be written. */
6125 tree *o = alloca (noutputs * sizeof (tree));
6126 tree tail;
6128 /* Record the contents of OUTPUTS before it is modified. */
6129 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6131 o[i] = TREE_VALUE (tail);
6132 if (o[i] == error_mark_node)
6133 return;
6136 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6137 OUTPUTS some trees for where the values were actually stored. */
6138 expand_asm_operands (string, outputs, inputs, clobbers, vol, locus);
6140 /* Copy all the intermediate outputs into the specified outputs. */
6141 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6143 if (o[i] != TREE_VALUE (tail))
6145 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6146 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6147 free_temp_slots ();
6149 /* Restore the original value so that it's correct the next
6150 time we expand this function. */
6151 TREE_VALUE (tail) = o[i];
6153 /* Detect modification of read-only values.
6154 (Otherwise done by build_modify_expr.) */
6155 else
6157 tree type = TREE_TYPE (o[i]);
6158 if (TREE_READONLY (o[i])
6159 || TYPE_READONLY (type)
6160 || ((TREE_CODE (type) == RECORD_TYPE
6161 || TREE_CODE (type) == UNION_TYPE)
6162 && C_TYPE_FIELDS_READONLY (type)))
6163 readonly_error (o[i], "modification by `asm'");
6167 /* Those MODIFY_EXPRs could do autoincrements. */
6168 emit_queue ();
6171 /* Expand a C `return' statement.
6172 RETVAL is the expression for what to return,
6173 or a null pointer for `return;' with no value. */
6175 tree
6176 c_expand_return (tree retval)
6178 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6180 if (TREE_THIS_VOLATILE (current_function_decl))
6181 warning ("function declared `noreturn' has a `return' statement");
6183 if (!retval)
6185 current_function_returns_null = 1;
6186 if ((warn_return_type || flag_isoc99)
6187 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6188 pedwarn_c99 ("`return' with no value, in function returning non-void");
6190 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6192 current_function_returns_null = 1;
6193 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6194 pedwarn ("`return' with a value, in function returning void");
6196 else
6198 tree t = convert_for_assignment (valtype, retval, _("return"),
6199 NULL_TREE, NULL_TREE, 0);
6200 tree res = DECL_RESULT (current_function_decl);
6201 tree inner;
6203 current_function_returns_value = 1;
6204 if (t == error_mark_node)
6205 return NULL_TREE;
6207 inner = t = convert (TREE_TYPE (res), t);
6209 /* Strip any conversions, additions, and subtractions, and see if
6210 we are returning the address of a local variable. Warn if so. */
6211 while (1)
6213 switch (TREE_CODE (inner))
6215 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6216 case PLUS_EXPR:
6217 inner = TREE_OPERAND (inner, 0);
6218 continue;
6220 case MINUS_EXPR:
6221 /* If the second operand of the MINUS_EXPR has a pointer
6222 type (or is converted from it), this may be valid, so
6223 don't give a warning. */
6225 tree op1 = TREE_OPERAND (inner, 1);
6227 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6228 && (TREE_CODE (op1) == NOP_EXPR
6229 || TREE_CODE (op1) == NON_LVALUE_EXPR
6230 || TREE_CODE (op1) == CONVERT_EXPR))
6231 op1 = TREE_OPERAND (op1, 0);
6233 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6234 break;
6236 inner = TREE_OPERAND (inner, 0);
6237 continue;
6240 case ADDR_EXPR:
6241 inner = TREE_OPERAND (inner, 0);
6243 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6244 inner = TREE_OPERAND (inner, 0);
6246 if (DECL_P (inner)
6247 && ! DECL_EXTERNAL (inner)
6248 && ! TREE_STATIC (inner)
6249 && DECL_CONTEXT (inner) == current_function_decl)
6250 warning ("function returns address of local variable");
6251 break;
6253 default:
6254 break;
6257 break;
6260 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6263 return add_stmt (build_return_stmt (retval));
6266 struct c_switch {
6267 /* The SWITCH_STMT being built. */
6268 tree switch_stmt;
6269 /* A splay-tree mapping the low element of a case range to the high
6270 element, or NULL_TREE if there is no high element. Used to
6271 determine whether or not a new case label duplicates an old case
6272 label. We need a tree, rather than simply a hash table, because
6273 of the GNU case range extension. */
6274 splay_tree cases;
6275 /* The next node on the stack. */
6276 struct c_switch *next;
6279 /* A stack of the currently active switch statements. The innermost
6280 switch statement is on the top of the stack. There is no need to
6281 mark the stack for garbage collection because it is only active
6282 during the processing of the body of a function, and we never
6283 collect at that point. */
6285 static struct c_switch *switch_stack;
6287 /* Start a C switch statement, testing expression EXP. Return the new
6288 SWITCH_STMT. */
6290 tree
6291 c_start_case (tree exp)
6293 enum tree_code code;
6294 tree type, orig_type = error_mark_node;
6295 struct c_switch *cs;
6297 if (exp != error_mark_node)
6299 code = TREE_CODE (TREE_TYPE (exp));
6300 orig_type = TREE_TYPE (exp);
6302 if (! INTEGRAL_TYPE_P (orig_type)
6303 && code != ERROR_MARK)
6305 error ("switch quantity not an integer");
6306 exp = integer_zero_node;
6308 else
6310 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6312 if (warn_traditional && !in_system_header
6313 && (type == long_integer_type_node
6314 || type == long_unsigned_type_node))
6315 warning ("`long' switch expression not converted to `int' in ISO C");
6317 exp = default_conversion (exp);
6318 type = TREE_TYPE (exp);
6322 /* Add this new SWITCH_STMT to the stack. */
6323 cs = xmalloc (sizeof (*cs));
6324 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6325 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6326 cs->next = switch_stack;
6327 switch_stack = cs;
6329 return add_stmt (switch_stack->switch_stmt);
6332 /* Process a case label. */
6334 tree
6335 do_case (tree low_value, tree high_value)
6337 tree label = NULL_TREE;
6339 if (switch_stack)
6341 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
6343 label = c_add_case_label (switch_stack->cases,
6344 SWITCH_COND (switch_stack->switch_stmt),
6345 low_value, high_value);
6346 if (label == error_mark_node)
6347 label = NULL_TREE;
6348 else if (switch_was_empty_p)
6350 /* Attach the first case label to the SWITCH_BODY. */
6351 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
6352 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
6355 else if (low_value)
6356 error ("case label not within a switch statement");
6357 else
6358 error ("`default' label not within a switch statement");
6360 return label;
6363 /* Finish the switch statement. */
6365 void
6366 c_finish_case (void)
6368 struct c_switch *cs = switch_stack;
6370 /* Rechain the next statements to the SWITCH_STMT. */
6371 last_tree = cs->switch_stmt;
6373 /* Pop the stack. */
6374 switch_stack = switch_stack->next;
6375 splay_tree_delete (cs->cases);
6376 free (cs);
6379 /* Build a binary-operation expression without default conversions.
6380 CODE is the kind of expression to build.
6381 This function differs from `build' in several ways:
6382 the data type of the result is computed and recorded in it,
6383 warnings are generated if arg data types are invalid,
6384 special handling for addition and subtraction of pointers is known,
6385 and some optimization is done (operations on narrow ints
6386 are done in the narrower type when that gives the same result).
6387 Constant folding is also done before the result is returned.
6389 Note that the operands will never have enumeral types, or function
6390 or array types, because either they will have the default conversions
6391 performed or they have both just been converted to some other type in which
6392 the arithmetic is to be done. */
6394 tree
6395 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6396 int convert_p)
6398 tree type0, type1;
6399 enum tree_code code0, code1;
6400 tree op0, op1;
6402 /* Expression code to give to the expression when it is built.
6403 Normally this is CODE, which is what the caller asked for,
6404 but in some special cases we change it. */
6405 enum tree_code resultcode = code;
6407 /* Data type in which the computation is to be performed.
6408 In the simplest cases this is the common type of the arguments. */
6409 tree result_type = NULL;
6411 /* Nonzero means operands have already been type-converted
6412 in whatever way is necessary.
6413 Zero means they need to be converted to RESULT_TYPE. */
6414 int converted = 0;
6416 /* Nonzero means create the expression with this type, rather than
6417 RESULT_TYPE. */
6418 tree build_type = 0;
6420 /* Nonzero means after finally constructing the expression
6421 convert it to this type. */
6422 tree final_type = 0;
6424 /* Nonzero if this is an operation like MIN or MAX which can
6425 safely be computed in short if both args are promoted shorts.
6426 Also implies COMMON.
6427 -1 indicates a bitwise operation; this makes a difference
6428 in the exact conditions for when it is safe to do the operation
6429 in a narrower mode. */
6430 int shorten = 0;
6432 /* Nonzero if this is a comparison operation;
6433 if both args are promoted shorts, compare the original shorts.
6434 Also implies COMMON. */
6435 int short_compare = 0;
6437 /* Nonzero if this is a right-shift operation, which can be computed on the
6438 original short and then promoted if the operand is a promoted short. */
6439 int short_shift = 0;
6441 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6442 int common = 0;
6444 if (convert_p)
6446 op0 = default_conversion (orig_op0);
6447 op1 = default_conversion (orig_op1);
6449 else
6451 op0 = orig_op0;
6452 op1 = orig_op1;
6455 type0 = TREE_TYPE (op0);
6456 type1 = TREE_TYPE (op1);
6458 /* The expression codes of the data types of the arguments tell us
6459 whether the arguments are integers, floating, pointers, etc. */
6460 code0 = TREE_CODE (type0);
6461 code1 = TREE_CODE (type1);
6463 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6464 STRIP_TYPE_NOPS (op0);
6465 STRIP_TYPE_NOPS (op1);
6467 /* If an error was already reported for one of the arguments,
6468 avoid reporting another error. */
6470 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6471 return error_mark_node;
6473 switch (code)
6475 case PLUS_EXPR:
6476 /* Handle the pointer + int case. */
6477 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6478 return pointer_int_sum (PLUS_EXPR, op0, op1);
6479 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6480 return pointer_int_sum (PLUS_EXPR, op1, op0);
6481 else
6482 common = 1;
6483 break;
6485 case MINUS_EXPR:
6486 /* Subtraction of two similar pointers.
6487 We must subtract them as integers, then divide by object size. */
6488 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6489 && comp_target_types (type0, type1, 1))
6490 return pointer_diff (op0, op1);
6491 /* Handle pointer minus int. Just like pointer plus int. */
6492 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6493 return pointer_int_sum (MINUS_EXPR, op0, op1);
6494 else
6495 common = 1;
6496 break;
6498 case MULT_EXPR:
6499 common = 1;
6500 break;
6502 case TRUNC_DIV_EXPR:
6503 case CEIL_DIV_EXPR:
6504 case FLOOR_DIV_EXPR:
6505 case ROUND_DIV_EXPR:
6506 case EXACT_DIV_EXPR:
6507 /* Floating point division by zero is a legitimate way to obtain
6508 infinities and NaNs. */
6509 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6510 warning ("division by zero");
6512 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6513 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6514 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6515 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6517 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6518 resultcode = RDIV_EXPR;
6519 else
6520 /* Although it would be tempting to shorten always here, that
6521 loses on some targets, since the modulo instruction is
6522 undefined if the quotient can't be represented in the
6523 computation mode. We shorten only if unsigned or if
6524 dividing by something we know != -1. */
6525 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6526 || (TREE_CODE (op1) == INTEGER_CST
6527 && ! integer_all_onesp (op1)));
6528 common = 1;
6530 break;
6532 case BIT_AND_EXPR:
6533 case BIT_IOR_EXPR:
6534 case BIT_XOR_EXPR:
6535 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6536 shorten = -1;
6537 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6538 common = 1;
6539 break;
6541 case TRUNC_MOD_EXPR:
6542 case FLOOR_MOD_EXPR:
6543 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6544 warning ("division by zero");
6546 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6548 /* Although it would be tempting to shorten always here, that loses
6549 on some targets, since the modulo instruction is undefined if the
6550 quotient can't be represented in the computation mode. We shorten
6551 only if unsigned or if dividing by something we know != -1. */
6552 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6553 || (TREE_CODE (op1) == INTEGER_CST
6554 && ! integer_all_onesp (op1)));
6555 common = 1;
6557 break;
6559 case TRUTH_ANDIF_EXPR:
6560 case TRUTH_ORIF_EXPR:
6561 case TRUTH_AND_EXPR:
6562 case TRUTH_OR_EXPR:
6563 case TRUTH_XOR_EXPR:
6564 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6565 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6566 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6567 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6569 /* Result of these operations is always an int,
6570 but that does not mean the operands should be
6571 converted to ints! */
6572 result_type = integer_type_node;
6573 op0 = lang_hooks.truthvalue_conversion (op0);
6574 op1 = lang_hooks.truthvalue_conversion (op1);
6575 converted = 1;
6577 break;
6579 /* Shift operations: result has same type as first operand;
6580 always convert second operand to int.
6581 Also set SHORT_SHIFT if shifting rightward. */
6583 case RSHIFT_EXPR:
6584 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6586 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6588 if (tree_int_cst_sgn (op1) < 0)
6589 warning ("right shift count is negative");
6590 else
6592 if (! integer_zerop (op1))
6593 short_shift = 1;
6595 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6596 warning ("right shift count >= width of type");
6600 /* Use the type of the value to be shifted. */
6601 result_type = type0;
6602 /* Convert the shift-count to an integer, regardless of size
6603 of value being shifted. */
6604 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6605 op1 = convert (integer_type_node, op1);
6606 /* Avoid converting op1 to result_type later. */
6607 converted = 1;
6609 break;
6611 case LSHIFT_EXPR:
6612 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6614 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6616 if (tree_int_cst_sgn (op1) < 0)
6617 warning ("left shift count is negative");
6619 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6620 warning ("left shift count >= width of type");
6623 /* Use the type of the value to be shifted. */
6624 result_type = type0;
6625 /* Convert the shift-count to an integer, regardless of size
6626 of value being shifted. */
6627 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6628 op1 = convert (integer_type_node, op1);
6629 /* Avoid converting op1 to result_type later. */
6630 converted = 1;
6632 break;
6634 case RROTATE_EXPR:
6635 case LROTATE_EXPR:
6636 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6638 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6640 if (tree_int_cst_sgn (op1) < 0)
6641 warning ("shift count is negative");
6642 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6643 warning ("shift count >= width of type");
6646 /* Use the type of the value to be shifted. */
6647 result_type = type0;
6648 /* Convert the shift-count to an integer, regardless of size
6649 of value being shifted. */
6650 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6651 op1 = convert (integer_type_node, op1);
6652 /* Avoid converting op1 to result_type later. */
6653 converted = 1;
6655 break;
6657 case EQ_EXPR:
6658 case NE_EXPR:
6659 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
6660 warning ("comparing floating point with == or != is unsafe");
6661 /* Result of comparison is always int,
6662 but don't convert the args to int! */
6663 build_type = integer_type_node;
6664 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6665 || code0 == COMPLEX_TYPE
6666 || code0 == VECTOR_TYPE)
6667 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6668 || code1 == COMPLEX_TYPE
6669 || code1 == VECTOR_TYPE))
6670 short_compare = 1;
6671 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6673 tree tt0 = TREE_TYPE (type0);
6674 tree tt1 = TREE_TYPE (type1);
6675 /* Anything compares with void *. void * compares with anything.
6676 Otherwise, the targets must be compatible
6677 and both must be object or both incomplete. */
6678 if (comp_target_types (type0, type1, 1))
6679 result_type = common_type (type0, type1);
6680 else if (VOID_TYPE_P (tt0))
6682 /* op0 != orig_op0 detects the case of something
6683 whose value is 0 but which isn't a valid null ptr const. */
6684 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
6685 && TREE_CODE (tt1) == FUNCTION_TYPE)
6686 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6688 else if (VOID_TYPE_P (tt1))
6690 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
6691 && TREE_CODE (tt0) == FUNCTION_TYPE)
6692 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6694 else
6695 pedwarn ("comparison of distinct pointer types lacks a cast");
6697 if (result_type == NULL_TREE)
6698 result_type = ptr_type_node;
6700 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6701 && integer_zerop (op1))
6702 result_type = type0;
6703 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6704 && integer_zerop (op0))
6705 result_type = type1;
6706 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6708 result_type = type0;
6709 pedwarn ("comparison between pointer and integer");
6711 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6713 result_type = type1;
6714 pedwarn ("comparison between pointer and integer");
6716 break;
6718 case MAX_EXPR:
6719 case MIN_EXPR:
6720 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6721 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6722 shorten = 1;
6723 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6725 if (comp_target_types (type0, type1, 1))
6727 result_type = common_type (type0, type1);
6728 if (pedantic
6729 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6730 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6732 else
6734 result_type = ptr_type_node;
6735 pedwarn ("comparison of distinct pointer types lacks a cast");
6738 break;
6740 case LE_EXPR:
6741 case GE_EXPR:
6742 case LT_EXPR:
6743 case GT_EXPR:
6744 build_type = integer_type_node;
6745 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6746 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6747 short_compare = 1;
6748 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6750 if (comp_target_types (type0, type1, 1))
6752 result_type = common_type (type0, type1);
6753 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
6754 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
6755 pedwarn ("comparison of complete and incomplete pointers");
6756 else if (pedantic
6757 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6758 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6760 else
6762 result_type = ptr_type_node;
6763 pedwarn ("comparison of distinct pointer types lacks a cast");
6766 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6767 && integer_zerop (op1))
6769 result_type = type0;
6770 if (pedantic || extra_warnings)
6771 pedwarn ("ordered comparison of pointer with integer zero");
6773 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6774 && integer_zerop (op0))
6776 result_type = type1;
6777 if (pedantic)
6778 pedwarn ("ordered comparison of pointer with integer zero");
6780 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6782 result_type = type0;
6783 pedwarn ("comparison between pointer and integer");
6785 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6787 result_type = type1;
6788 pedwarn ("comparison between pointer and integer");
6790 break;
6792 case UNORDERED_EXPR:
6793 case ORDERED_EXPR:
6794 case UNLT_EXPR:
6795 case UNLE_EXPR:
6796 case UNGT_EXPR:
6797 case UNGE_EXPR:
6798 case UNEQ_EXPR:
6799 build_type = integer_type_node;
6800 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6802 error ("unordered comparison on non-floating point argument");
6803 return error_mark_node;
6805 common = 1;
6806 break;
6808 default:
6809 break;
6812 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6813 return error_mark_node;
6815 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6816 || code0 == VECTOR_TYPE)
6818 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
6819 || code1 == VECTOR_TYPE))
6821 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
6823 if (shorten || common || short_compare)
6824 result_type = common_type (type0, type1);
6826 /* For certain operations (which identify themselves by shorten != 0)
6827 if both args were extended from the same smaller type,
6828 do the arithmetic in that type and then extend.
6830 shorten !=0 and !=1 indicates a bitwise operation.
6831 For them, this optimization is safe only if
6832 both args are zero-extended or both are sign-extended.
6833 Otherwise, we might change the result.
6834 Eg, (short)-1 | (unsigned short)-1 is (int)-1
6835 but calculated in (unsigned short) it would be (unsigned short)-1. */
6837 if (shorten && none_complex)
6839 int unsigned0, unsigned1;
6840 tree arg0 = get_narrower (op0, &unsigned0);
6841 tree arg1 = get_narrower (op1, &unsigned1);
6842 /* UNS is 1 if the operation to be done is an unsigned one. */
6843 int uns = TREE_UNSIGNED (result_type);
6844 tree type;
6846 final_type = result_type;
6848 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6849 but it *requires* conversion to FINAL_TYPE. */
6851 if ((TYPE_PRECISION (TREE_TYPE (op0))
6852 == TYPE_PRECISION (TREE_TYPE (arg0)))
6853 && TREE_TYPE (op0) != final_type)
6854 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
6855 if ((TYPE_PRECISION (TREE_TYPE (op1))
6856 == TYPE_PRECISION (TREE_TYPE (arg1)))
6857 && TREE_TYPE (op1) != final_type)
6858 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
6860 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
6862 /* For bitwise operations, signedness of nominal type
6863 does not matter. Consider only how operands were extended. */
6864 if (shorten == -1)
6865 uns = unsigned0;
6867 /* Note that in all three cases below we refrain from optimizing
6868 an unsigned operation on sign-extended args.
6869 That would not be valid. */
6871 /* Both args variable: if both extended in same way
6872 from same width, do it in that width.
6873 Do it unsigned if args were zero-extended. */
6874 if ((TYPE_PRECISION (TREE_TYPE (arg0))
6875 < TYPE_PRECISION (result_type))
6876 && (TYPE_PRECISION (TREE_TYPE (arg1))
6877 == TYPE_PRECISION (TREE_TYPE (arg0)))
6878 && unsigned0 == unsigned1
6879 && (unsigned0 || !uns))
6880 result_type
6881 = c_common_signed_or_unsigned_type
6882 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
6883 else if (TREE_CODE (arg0) == INTEGER_CST
6884 && (unsigned1 || !uns)
6885 && (TYPE_PRECISION (TREE_TYPE (arg1))
6886 < TYPE_PRECISION (result_type))
6887 && (type
6888 = c_common_signed_or_unsigned_type (unsigned1,
6889 TREE_TYPE (arg1)),
6890 int_fits_type_p (arg0, type)))
6891 result_type = type;
6892 else if (TREE_CODE (arg1) == INTEGER_CST
6893 && (unsigned0 || !uns)
6894 && (TYPE_PRECISION (TREE_TYPE (arg0))
6895 < TYPE_PRECISION (result_type))
6896 && (type
6897 = c_common_signed_or_unsigned_type (unsigned0,
6898 TREE_TYPE (arg0)),
6899 int_fits_type_p (arg1, type)))
6900 result_type = type;
6903 /* Shifts can be shortened if shifting right. */
6905 if (short_shift)
6907 int unsigned_arg;
6908 tree arg0 = get_narrower (op0, &unsigned_arg);
6910 final_type = result_type;
6912 if (arg0 == op0 && final_type == TREE_TYPE (op0))
6913 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
6915 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6916 /* We can shorten only if the shift count is less than the
6917 number of bits in the smaller type size. */
6918 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6919 /* We cannot drop an unsigned shift after sign-extension. */
6920 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
6922 /* Do an unsigned shift if the operand was zero-extended. */
6923 result_type
6924 = c_common_signed_or_unsigned_type (unsigned_arg,
6925 TREE_TYPE (arg0));
6926 /* Convert value-to-be-shifted to that type. */
6927 if (TREE_TYPE (op0) != result_type)
6928 op0 = convert (result_type, op0);
6929 converted = 1;
6933 /* Comparison operations are shortened too but differently.
6934 They identify themselves by setting short_compare = 1. */
6936 if (short_compare)
6938 /* Don't write &op0, etc., because that would prevent op0
6939 from being kept in a register.
6940 Instead, make copies of the our local variables and
6941 pass the copies by reference, then copy them back afterward. */
6942 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
6943 enum tree_code xresultcode = resultcode;
6944 tree val
6945 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
6947 if (val != 0)
6948 return val;
6950 op0 = xop0, op1 = xop1;
6951 converted = 1;
6952 resultcode = xresultcode;
6954 if (warn_sign_compare && skip_evaluation == 0)
6956 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
6957 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
6958 int unsignedp0, unsignedp1;
6959 tree primop0 = get_narrower (op0, &unsignedp0);
6960 tree primop1 = get_narrower (op1, &unsignedp1);
6962 xop0 = orig_op0;
6963 xop1 = orig_op1;
6964 STRIP_TYPE_NOPS (xop0);
6965 STRIP_TYPE_NOPS (xop1);
6967 /* Give warnings for comparisons between signed and unsigned
6968 quantities that may fail.
6970 Do the checking based on the original operand trees, so that
6971 casts will be considered, but default promotions won't be.
6973 Do not warn if the comparison is being done in a signed type,
6974 since the signed type will only be chosen if it can represent
6975 all the values of the unsigned type. */
6976 if (! TREE_UNSIGNED (result_type))
6977 /* OK */;
6978 /* Do not warn if both operands are the same signedness. */
6979 else if (op0_signed == op1_signed)
6980 /* OK */;
6981 else
6983 tree sop, uop;
6985 if (op0_signed)
6986 sop = xop0, uop = xop1;
6987 else
6988 sop = xop1, uop = xop0;
6990 /* Do not warn if the signed quantity is an
6991 unsuffixed integer literal (or some static
6992 constant expression involving such literals or a
6993 conditional expression involving such literals)
6994 and it is non-negative. */
6995 if (c_tree_expr_nonnegative_p (sop))
6996 /* OK */;
6997 /* Do not warn if the comparison is an equality operation,
6998 the unsigned quantity is an integral constant, and it
6999 would fit in the result if the result were signed. */
7000 else if (TREE_CODE (uop) == INTEGER_CST
7001 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7002 && int_fits_type_p
7003 (uop, c_common_signed_type (result_type)))
7004 /* OK */;
7005 /* Do not warn if the unsigned quantity is an enumeration
7006 constant and its maximum value would fit in the result
7007 if the result were signed. */
7008 else if (TREE_CODE (uop) == INTEGER_CST
7009 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7010 && int_fits_type_p
7011 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7012 c_common_signed_type (result_type)))
7013 /* OK */;
7014 else
7015 warning ("comparison between signed and unsigned");
7018 /* Warn if two unsigned values are being compared in a size
7019 larger than their original size, and one (and only one) is the
7020 result of a `~' operator. This comparison will always fail.
7022 Also warn if one operand is a constant, and the constant
7023 does not have all bits set that are set in the ~ operand
7024 when it is extended. */
7026 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7027 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7029 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7030 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7031 &unsignedp0);
7032 else
7033 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7034 &unsignedp1);
7036 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7038 tree primop;
7039 HOST_WIDE_INT constant, mask;
7040 int unsignedp, bits;
7042 if (host_integerp (primop0, 0))
7044 primop = primop1;
7045 unsignedp = unsignedp1;
7046 constant = tree_low_cst (primop0, 0);
7048 else
7050 primop = primop0;
7051 unsignedp = unsignedp0;
7052 constant = tree_low_cst (primop1, 0);
7055 bits = TYPE_PRECISION (TREE_TYPE (primop));
7056 if (bits < TYPE_PRECISION (result_type)
7057 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7059 mask = (~ (HOST_WIDE_INT) 0) << bits;
7060 if ((mask & constant) != mask)
7061 warning ("comparison of promoted ~unsigned with constant");
7064 else if (unsignedp0 && unsignedp1
7065 && (TYPE_PRECISION (TREE_TYPE (primop0))
7066 < TYPE_PRECISION (result_type))
7067 && (TYPE_PRECISION (TREE_TYPE (primop1))
7068 < TYPE_PRECISION (result_type)))
7069 warning ("comparison of promoted ~unsigned with unsigned");
7075 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7076 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7077 Then the expression will be built.
7078 It will be given type FINAL_TYPE if that is nonzero;
7079 otherwise, it will be given type RESULT_TYPE. */
7081 if (!result_type)
7083 binary_op_error (code);
7084 return error_mark_node;
7087 if (! converted)
7089 if (TREE_TYPE (op0) != result_type)
7090 op0 = convert (result_type, op0);
7091 if (TREE_TYPE (op1) != result_type)
7092 op1 = convert (result_type, op1);
7095 if (build_type == NULL_TREE)
7096 build_type = result_type;
7099 tree result = build (resultcode, build_type, op0, op1);
7100 tree folded;
7102 /* Treat expressions in initializers specially as they can't trap. */
7103 folded = initializer_stack ? fold_initializer (result)
7104 : fold (result);
7105 if (folded == result)
7106 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
7107 if (final_type != 0)
7108 return convert (final_type, folded);
7109 return folded;