* config/xtensa/lib2funcs.S: Fix whitespace.
[official-gcc.git] / gcc / c-typeck.c
blob7d29b196e957d3f200c6984271391646786f873f
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 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 "c-tree.h"
39 #include "tm_p.h"
40 #include "flags.h"
41 #include "output.h"
42 #include "expr.h"
43 #include "toplev.h"
44 #include "intl.h"
45 #include "ggc.h"
46 #include "target.h"
48 /* Nonzero if we've already printed a "missing braces around initializer"
49 message within this initializer. */
50 static int missing_braces_mentioned;
52 static tree qualify_type (tree, tree);
53 static int tagged_types_tu_compatible_p (tree, tree, int);
54 static int comp_target_types (tree, tree, int);
55 static int function_types_compatible_p (tree, tree, int);
56 static int type_lists_compatible_p (tree, tree, int);
57 static tree decl_constant_value_for_broken_optimization (tree);
58 static tree default_function_array_conversion (tree);
59 static tree lookup_field (tree, tree);
60 static tree convert_arguments (tree, tree, tree, tree);
61 static tree pointer_diff (tree, tree);
62 static tree unary_complex_lvalue (enum tree_code, tree, int);
63 static void pedantic_lvalue_warning (enum tree_code);
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 /* Treat an enum type as the integer type of the same width and
477 signedness. */
479 if (TREE_CODE (t1) == ENUMERAL_TYPE)
480 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
481 if (TREE_CODE (t2) == ENUMERAL_TYPE)
482 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
484 if (t1 == t2)
485 return 1;
487 /* Different classes of types can't be compatible. */
489 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
491 /* Qualifiers must match. */
493 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
494 return 0;
496 /* Allow for two different type nodes which have essentially the same
497 definition. Note that we already checked for equality of the type
498 qualifiers (just above). */
500 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
501 return 1;
503 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
504 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
505 return 0;
507 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
508 val = 0;
510 switch (TREE_CODE (t1))
512 case POINTER_TYPE:
513 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
514 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
515 break;
517 case FUNCTION_TYPE:
518 val = function_types_compatible_p (t1, t2, flags);
519 break;
521 case ARRAY_TYPE:
523 tree d1 = TYPE_DOMAIN (t1);
524 tree d2 = TYPE_DOMAIN (t2);
525 bool d1_variable, d2_variable;
526 bool d1_zero, d2_zero;
527 val = 1;
529 /* Target types must match incl. qualifiers. */
530 if (TREE_TYPE (t1) != TREE_TYPE (t2)
531 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
532 flags)))
533 return 0;
535 /* Sizes must match unless one is missing or variable. */
536 if (d1 == 0 || d2 == 0 || d1 == d2)
537 break;
539 d1_zero = ! TYPE_MAX_VALUE (d1);
540 d2_zero = ! TYPE_MAX_VALUE (d2);
542 d1_variable = (! d1_zero
543 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
544 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
545 d2_variable = (! d2_zero
546 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
547 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
549 if (d1_variable || d2_variable)
550 break;
551 if (d1_zero && d2_zero)
552 break;
553 if (d1_zero || d2_zero
554 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
555 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
556 val = 0;
558 break;
561 case RECORD_TYPE:
562 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
563 val = 1;
565 case ENUMERAL_TYPE:
566 case UNION_TYPE:
567 if (val != 1 && (flags & COMPARE_DIFFERENT_TU))
568 val = tagged_types_tu_compatible_p (t1, t2, flags);
569 break;
571 case VECTOR_TYPE:
572 /* The target might allow certain vector types to be compatible. */
573 val = (*targetm.vector_opaque_p) (t1)
574 || (*targetm.vector_opaque_p) (t2);
575 break;
577 default:
578 break;
580 return attrval == 2 && val == 1 ? 2 : val;
583 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
584 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
585 to 1 or 0 depending if the check of the pointer types is meant to
586 be reflexive or not (typically, assignments are not reflexive,
587 while comparisons are reflexive).
590 static int
591 comp_target_types (tree ttl, tree ttr, int reflexive)
593 int val;
595 /* Give objc_comptypes a crack at letting these types through. */
596 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
597 return val;
599 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
600 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
602 if (val == 2 && pedantic)
603 pedwarn ("types are not quite compatible");
604 return val;
607 /* Subroutines of `comptypes'. */
609 /* The C standard says that two structures in different translation
610 units are compatible with each other only if the types of their
611 fields are compatible (among other things). So, consider two copies
612 of this structure: */
614 struct tagged_tu_seen {
615 const struct tagged_tu_seen * next;
616 tree t1;
617 tree t2;
620 /* Can they be compatible with each other? We choose to break the
621 recursion by allowing those types to be compatible. */
623 static const struct tagged_tu_seen * tagged_tu_seen_base;
625 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
626 compatible. If the two types are not the same (which has been
627 checked earlier), this can only happen when multiple translation
628 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
629 rules. */
631 static int
632 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
634 tree s1, s2;
635 bool needs_warning = false;
637 /* We have to verify that the tags of the types are the same. This
638 is harder than it looks because this may be a typedef, so we have
639 to go look at the original type. It may even be a typedef of a
640 typedef... */
641 while (TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL)
642 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
644 while (TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL)
645 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
647 /* C90 didn't have the requirement that the two tags be the same. */
648 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
649 return 0;
651 /* C90 didn't say what happened if one or both of the types were
652 incomplete; we choose to follow C99 rules here, which is that they
653 are compatible. */
654 if (TYPE_SIZE (t1) == NULL
655 || TYPE_SIZE (t2) == NULL)
656 return 1;
659 const struct tagged_tu_seen * tts_i;
660 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
661 if (tts_i->t1 == t1 && tts_i->t2 == t2)
662 return 1;
665 switch (TREE_CODE (t1))
667 case ENUMERAL_TYPE:
669 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
670 return 0;
672 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
674 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
675 if (s2 == NULL
676 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
677 return 0;
679 return 1;
682 case UNION_TYPE:
684 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
685 return 0;
687 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
689 bool ok = false;
690 struct tagged_tu_seen tts;
692 tts.next = tagged_tu_seen_base;
693 tts.t1 = t1;
694 tts.t2 = t2;
695 tagged_tu_seen_base = &tts;
697 if (DECL_NAME (s1) != NULL)
698 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
699 if (DECL_NAME (s1) == DECL_NAME (s2))
701 int result;
702 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
703 if (result == 0)
704 break;
705 if (result == 2)
706 needs_warning = true;
708 if (TREE_CODE (s1) == FIELD_DECL
709 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
710 DECL_FIELD_BIT_OFFSET (s2)) != 1)
711 break;
713 ok = true;
714 break;
716 tagged_tu_seen_base = tts.next;
717 if (! ok)
718 return 0;
720 return needs_warning ? 2 : 1;
723 case RECORD_TYPE:
725 struct tagged_tu_seen tts;
727 tts.next = tagged_tu_seen_base;
728 tts.t1 = t1;
729 tts.t2 = t2;
730 tagged_tu_seen_base = &tts;
732 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
733 s1 && s2;
734 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
736 int result;
737 if (TREE_CODE (s1) != TREE_CODE (s2)
738 || DECL_NAME (s1) != DECL_NAME (s2))
739 break;
740 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
741 if (result == 0)
742 break;
743 if (result == 2)
744 needs_warning = true;
746 if (TREE_CODE (s1) == FIELD_DECL
747 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
748 DECL_FIELD_BIT_OFFSET (s2)) != 1)
749 break;
751 tagged_tu_seen_base = tts.next;
752 if (s1 && s2)
753 return 0;
754 return needs_warning ? 2 : 1;
757 default:
758 abort ();
762 /* Return 1 if two function types F1 and F2 are compatible.
763 If either type specifies no argument types,
764 the other must specify a fixed number of self-promoting arg types.
765 Otherwise, if one type specifies only the number of arguments,
766 the other must specify that number of self-promoting arg types.
767 Otherwise, the argument types must match. */
769 static int
770 function_types_compatible_p (tree f1, tree f2, int flags)
772 tree args1, args2;
773 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
774 int val = 1;
775 int val1;
776 tree ret1, ret2;
778 ret1 = TREE_TYPE (f1);
779 ret2 = TREE_TYPE (f2);
781 /* 'volatile' qualifiers on a function's return type mean the function
782 is noreturn. */
783 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
784 pedwarn ("function return types not compatible due to `volatile'");
785 if (TYPE_VOLATILE (ret1))
786 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
787 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
788 if (TYPE_VOLATILE (ret2))
789 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
790 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
791 val = comptypes (ret1, ret2, flags);
792 if (val == 0)
793 return 0;
795 args1 = TYPE_ARG_TYPES (f1);
796 args2 = TYPE_ARG_TYPES (f2);
798 /* An unspecified parmlist matches any specified parmlist
799 whose argument types don't need default promotions. */
801 if (args1 == 0)
803 if (!self_promoting_args_p (args2))
804 return 0;
805 /* If one of these types comes from a non-prototype fn definition,
806 compare that with the other type's arglist.
807 If they don't match, ask for a warning (but no error). */
808 if (TYPE_ACTUAL_ARG_TYPES (f1)
809 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
810 flags))
811 val = 2;
812 return val;
814 if (args2 == 0)
816 if (!self_promoting_args_p (args1))
817 return 0;
818 if (TYPE_ACTUAL_ARG_TYPES (f2)
819 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
820 flags))
821 val = 2;
822 return val;
825 /* Both types have argument lists: compare them and propagate results. */
826 val1 = type_lists_compatible_p (args1, args2, flags);
827 return val1 != 1 ? val1 : val;
830 /* Check two lists of types for compatibility,
831 returning 0 for incompatible, 1 for compatible,
832 or 2 for compatible with warning. */
834 static int
835 type_lists_compatible_p (tree args1, tree args2, int flags)
837 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
838 int val = 1;
839 int newval = 0;
841 while (1)
843 if (args1 == 0 && args2 == 0)
844 return val;
845 /* If one list is shorter than the other,
846 they fail to match. */
847 if (args1 == 0 || args2 == 0)
848 return 0;
849 /* A null pointer instead of a type
850 means there is supposed to be an argument
851 but nothing is specified about what type it has.
852 So match anything that self-promotes. */
853 if (TREE_VALUE (args1) == 0)
855 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
856 return 0;
858 else if (TREE_VALUE (args2) == 0)
860 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
861 return 0;
863 /* If one of the lists has an error marker, ignore this arg. */
864 else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
865 || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
867 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
868 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
869 flags)))
871 /* Allow wait (union {union wait *u; int *i} *)
872 and wait (union wait *) to be compatible. */
873 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
874 && (TYPE_NAME (TREE_VALUE (args1)) == 0
875 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
876 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
877 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
878 TYPE_SIZE (TREE_VALUE (args2))))
880 tree memb;
881 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
882 memb; memb = TREE_CHAIN (memb))
883 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
884 flags))
885 break;
886 if (memb == 0)
887 return 0;
889 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
890 && (TYPE_NAME (TREE_VALUE (args2)) == 0
891 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
892 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
893 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
894 TYPE_SIZE (TREE_VALUE (args1))))
896 tree memb;
897 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
898 memb; memb = TREE_CHAIN (memb))
899 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
900 flags))
901 break;
902 if (memb == 0)
903 return 0;
905 else
906 return 0;
909 /* comptypes said ok, but record if it said to warn. */
910 if (newval > val)
911 val = newval;
913 args1 = TREE_CHAIN (args1);
914 args2 = TREE_CHAIN (args2);
918 /* Compute the size to increment a pointer by. */
920 tree
921 c_size_in_bytes (tree type)
923 enum tree_code code = TREE_CODE (type);
925 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
926 return size_one_node;
928 if (!COMPLETE_OR_VOID_TYPE_P (type))
930 error ("arithmetic on pointer to an incomplete type");
931 return size_one_node;
934 /* Convert in case a char is more than one unit. */
935 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
936 size_int (TYPE_PRECISION (char_type_node)
937 / BITS_PER_UNIT));
940 /* Return either DECL or its known constant value (if it has one). */
942 tree
943 decl_constant_value (tree decl)
945 if (/* Don't change a variable array bound or initial value to a constant
946 in a place where a variable is invalid. */
947 current_function_decl != 0
948 && ! TREE_THIS_VOLATILE (decl)
949 && TREE_READONLY (decl)
950 && DECL_INITIAL (decl) != 0
951 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
952 /* This is invalid if initial value is not constant.
953 If it has either a function call, a memory reference,
954 or a variable, then re-evaluating it could give different results. */
955 && TREE_CONSTANT (DECL_INITIAL (decl))
956 /* Check for cases where this is sub-optimal, even though valid. */
957 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
958 return DECL_INITIAL (decl);
959 return decl;
962 /* Return either DECL or its known constant value (if it has one), but
963 return DECL if pedantic or DECL has mode BLKmode. This is for
964 bug-compatibility with the old behavior of decl_constant_value
965 (before GCC 3.0); every use of this function is a bug and it should
966 be removed before GCC 3.1. It is not appropriate to use pedantic
967 in a way that affects optimization, and BLKmode is probably not the
968 right test for avoiding misoptimizations either. */
970 static tree
971 decl_constant_value_for_broken_optimization (tree decl)
973 if (pedantic || DECL_MODE (decl) == BLKmode)
974 return decl;
975 else
976 return decl_constant_value (decl);
980 /* Perform the default conversion of arrays and functions to pointers.
981 Return the result of converting EXP. For any other expression, just
982 return EXP. */
984 static tree
985 default_function_array_conversion (tree exp)
987 tree orig_exp;
988 tree type = TREE_TYPE (exp);
989 enum tree_code code = TREE_CODE (type);
990 int not_lvalue = 0;
992 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
993 an lvalue.
995 Do not use STRIP_NOPS here! It will remove conversions from pointer
996 to integer and cause infinite recursion. */
997 orig_exp = exp;
998 while (TREE_CODE (exp) == NON_LVALUE_EXPR
999 || (TREE_CODE (exp) == NOP_EXPR
1000 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1002 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1003 not_lvalue = 1;
1004 exp = TREE_OPERAND (exp, 0);
1007 /* Preserve the original expression code. */
1008 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1009 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1011 if (code == FUNCTION_TYPE)
1013 return build_unary_op (ADDR_EXPR, exp, 0);
1015 if (code == ARRAY_TYPE)
1017 tree adr;
1018 tree restype = TREE_TYPE (type);
1019 tree ptrtype;
1020 int constp = 0;
1021 int volatilep = 0;
1022 int lvalue_array_p;
1024 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1026 constp = TREE_READONLY (exp);
1027 volatilep = TREE_THIS_VOLATILE (exp);
1030 if (TYPE_QUALS (type) || constp || volatilep)
1031 restype
1032 = c_build_qualified_type (restype,
1033 TYPE_QUALS (type)
1034 | (constp * TYPE_QUAL_CONST)
1035 | (volatilep * TYPE_QUAL_VOLATILE));
1037 if (TREE_CODE (exp) == INDIRECT_REF)
1038 return convert (TYPE_POINTER_TO (restype),
1039 TREE_OPERAND (exp, 0));
1041 if (TREE_CODE (exp) == COMPOUND_EXPR)
1043 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1044 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1045 TREE_OPERAND (exp, 0), op1);
1048 lvalue_array_p = !not_lvalue && lvalue_p (exp);
1049 if (!flag_isoc99 && !lvalue_array_p)
1051 /* Before C99, non-lvalue arrays do not decay to pointers.
1052 Normally, using such an array would be invalid; but it can
1053 be used correctly inside sizeof or as a statement expression.
1054 Thus, do not give an error here; an error will result later. */
1055 return exp;
1058 ptrtype = build_pointer_type (restype);
1060 if (TREE_CODE (exp) == VAR_DECL)
1062 /* ??? This is not really quite correct
1063 in that the type of the operand of ADDR_EXPR
1064 is not the target type of the type of the ADDR_EXPR itself.
1065 Question is, can this lossage be avoided? */
1066 adr = build1 (ADDR_EXPR, ptrtype, exp);
1067 if (!c_mark_addressable (exp))
1068 return error_mark_node;
1069 TREE_CONSTANT (adr) = staticp (exp);
1070 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1071 return adr;
1073 /* This way is better for a COMPONENT_REF since it can
1074 simplify the offset for a component. */
1075 adr = build_unary_op (ADDR_EXPR, exp, 1);
1076 return convert (ptrtype, adr);
1078 return exp;
1081 /* Perform default promotions for C data used in expressions.
1082 Arrays and functions are converted to pointers;
1083 enumeral types or short or char, to int.
1084 In addition, manifest constants symbols are replaced by their values. */
1086 tree
1087 default_conversion (tree exp)
1089 tree orig_exp;
1090 tree type = TREE_TYPE (exp);
1091 enum tree_code code = TREE_CODE (type);
1093 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1094 return default_function_array_conversion (exp);
1096 /* Constants can be used directly unless they're not loadable. */
1097 if (TREE_CODE (exp) == CONST_DECL)
1098 exp = DECL_INITIAL (exp);
1100 /* Replace a nonvolatile const static variable with its value unless
1101 it is an array, in which case we must be sure that taking the
1102 address of the array produces consistent results. */
1103 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1105 exp = decl_constant_value_for_broken_optimization (exp);
1106 type = TREE_TYPE (exp);
1109 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1110 an lvalue.
1112 Do not use STRIP_NOPS here! It will remove conversions from pointer
1113 to integer and cause infinite recursion. */
1114 orig_exp = exp;
1115 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1116 || (TREE_CODE (exp) == NOP_EXPR
1117 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1118 exp = TREE_OPERAND (exp, 0);
1120 /* Preserve the original expression code. */
1121 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1122 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1124 /* Normally convert enums to int,
1125 but convert wide enums to something wider. */
1126 if (code == ENUMERAL_TYPE)
1128 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1129 TYPE_PRECISION (integer_type_node)),
1130 ((TYPE_PRECISION (type)
1131 >= TYPE_PRECISION (integer_type_node))
1132 && TREE_UNSIGNED (type)));
1134 return convert (type, exp);
1137 if (TREE_CODE (exp) == COMPONENT_REF
1138 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1139 /* If it's thinner than an int, promote it like a
1140 c_promoting_integer_type_p, otherwise leave it alone. */
1141 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1142 TYPE_PRECISION (integer_type_node)))
1143 return convert (integer_type_node, exp);
1145 if (c_promoting_integer_type_p (type))
1147 /* Preserve unsignedness if not really getting any wider. */
1148 if (TREE_UNSIGNED (type)
1149 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1150 return convert (unsigned_type_node, exp);
1152 return convert (integer_type_node, exp);
1155 if (code == VOID_TYPE)
1157 error ("void value not ignored as it ought to be");
1158 return error_mark_node;
1160 return exp;
1163 /* Look up COMPONENT in a structure or union DECL.
1165 If the component name is not found, returns NULL_TREE. Otherwise,
1166 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1167 stepping down the chain to the component, which is in the last
1168 TREE_VALUE of the list. Normally the list is of length one, but if
1169 the component is embedded within (nested) anonymous structures or
1170 unions, the list steps down the chain to the component. */
1172 static tree
1173 lookup_field (tree decl, tree component)
1175 tree type = TREE_TYPE (decl);
1176 tree field;
1178 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1179 to the field elements. Use a binary search on this array to quickly
1180 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1181 will always be set for structures which have many elements. */
1183 if (TYPE_LANG_SPECIFIC (type))
1185 int bot, top, half;
1186 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1188 field = TYPE_FIELDS (type);
1189 bot = 0;
1190 top = TYPE_LANG_SPECIFIC (type)->s->len;
1191 while (top - bot > 1)
1193 half = (top - bot + 1) >> 1;
1194 field = field_array[bot+half];
1196 if (DECL_NAME (field) == NULL_TREE)
1198 /* Step through all anon unions in linear fashion. */
1199 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1201 field = field_array[bot++];
1202 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1203 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1205 tree anon = lookup_field (field, component);
1207 if (anon)
1208 return tree_cons (NULL_TREE, field, anon);
1212 /* Entire record is only anon unions. */
1213 if (bot > top)
1214 return NULL_TREE;
1216 /* Restart the binary search, with new lower bound. */
1217 continue;
1220 if (DECL_NAME (field) == component)
1221 break;
1222 if (DECL_NAME (field) < component)
1223 bot += half;
1224 else
1225 top = bot + half;
1228 if (DECL_NAME (field_array[bot]) == component)
1229 field = field_array[bot];
1230 else if (DECL_NAME (field) != component)
1231 return NULL_TREE;
1233 else
1235 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1237 if (DECL_NAME (field) == NULL_TREE
1238 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1239 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1241 tree anon = lookup_field (field, component);
1243 if (anon)
1244 return tree_cons (NULL_TREE, field, anon);
1247 if (DECL_NAME (field) == component)
1248 break;
1251 if (field == NULL_TREE)
1252 return NULL_TREE;
1255 return tree_cons (NULL_TREE, field, NULL_TREE);
1258 /* Make an expression to refer to the COMPONENT field of
1259 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1261 tree
1262 build_component_ref (tree datum, tree component)
1264 tree type = TREE_TYPE (datum);
1265 enum tree_code code = TREE_CODE (type);
1266 tree field = NULL;
1267 tree ref;
1269 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1270 If pedantic ensure that the arguments are not lvalues; otherwise,
1271 if the component is an array, it would wrongly decay to a pointer in
1272 C89 mode.
1273 We cannot do this with a COND_EXPR, because in a conditional expression
1274 the default promotions are applied to both sides, and this would yield
1275 the wrong type of the result; for example, if the components have
1276 type "char". */
1277 switch (TREE_CODE (datum))
1279 case COMPOUND_EXPR:
1281 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1282 return build (COMPOUND_EXPR, TREE_TYPE (value),
1283 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
1285 default:
1286 break;
1289 /* See if there is a field or component with name COMPONENT. */
1291 if (code == RECORD_TYPE || code == UNION_TYPE)
1293 if (!COMPLETE_TYPE_P (type))
1295 c_incomplete_type_error (NULL_TREE, type);
1296 return error_mark_node;
1299 field = lookup_field (datum, component);
1301 if (!field)
1303 error ("%s has no member named `%s'",
1304 code == RECORD_TYPE ? "structure" : "union",
1305 IDENTIFIER_POINTER (component));
1306 return error_mark_node;
1309 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1310 This might be better solved in future the way the C++ front
1311 end does it - by giving the anonymous entities each a
1312 separate name and type, and then have build_component_ref
1313 recursively call itself. We can't do that here. */
1316 tree subdatum = TREE_VALUE (field);
1318 if (TREE_TYPE (subdatum) == error_mark_node)
1319 return error_mark_node;
1321 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1322 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1323 TREE_READONLY (ref) = 1;
1324 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1325 TREE_THIS_VOLATILE (ref) = 1;
1327 if (TREE_DEPRECATED (subdatum))
1328 warn_deprecated_use (subdatum);
1330 datum = ref;
1332 field = TREE_CHAIN (field);
1334 while (field);
1336 return ref;
1338 else if (code != ERROR_MARK)
1339 error ("request for member `%s' in something not a structure or union",
1340 IDENTIFIER_POINTER (component));
1342 return error_mark_node;
1345 /* Given an expression PTR for a pointer, return an expression
1346 for the value pointed to.
1347 ERRORSTRING is the name of the operator to appear in error messages. */
1349 tree
1350 build_indirect_ref (tree ptr, const char *errorstring)
1352 tree pointer = default_conversion (ptr);
1353 tree type = TREE_TYPE (pointer);
1355 if (TREE_CODE (type) == POINTER_TYPE)
1357 if (TREE_CODE (pointer) == ADDR_EXPR
1358 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1359 == TREE_TYPE (type)))
1360 return TREE_OPERAND (pointer, 0);
1361 else
1363 tree t = TREE_TYPE (type);
1364 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1366 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1368 error ("dereferencing pointer to incomplete type");
1369 return error_mark_node;
1371 if (VOID_TYPE_P (t) && skip_evaluation == 0)
1372 warning ("dereferencing `void *' pointer");
1374 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1375 so that we get the proper error message if the result is used
1376 to assign to. Also, &* is supposed to be a no-op.
1377 And ANSI C seems to specify that the type of the result
1378 should be the const type. */
1379 /* A de-reference of a pointer to const is not a const. It is valid
1380 to change it via some other pointer. */
1381 TREE_READONLY (ref) = TYPE_READONLY (t);
1382 TREE_SIDE_EFFECTS (ref)
1383 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1384 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1385 return ref;
1388 else if (TREE_CODE (pointer) != ERROR_MARK)
1389 error ("invalid type argument of `%s'", errorstring);
1390 return error_mark_node;
1393 /* This handles expressions of the form "a[i]", which denotes
1394 an array reference.
1396 This is logically equivalent in C to *(a+i), but we may do it differently.
1397 If A is a variable or a member, we generate a primitive ARRAY_REF.
1398 This avoids forcing the array out of registers, and can work on
1399 arrays that are not lvalues (for example, members of structures returned
1400 by functions). */
1402 tree
1403 build_array_ref (tree array, tree index)
1405 if (index == 0)
1407 error ("subscript missing in array reference");
1408 return error_mark_node;
1411 if (TREE_TYPE (array) == error_mark_node
1412 || TREE_TYPE (index) == error_mark_node)
1413 return error_mark_node;
1415 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1416 && TREE_CODE (array) != INDIRECT_REF)
1418 tree rval, type;
1420 /* Subscripting with type char is likely to lose
1421 on a machine where chars are signed.
1422 So warn on any machine, but optionally.
1423 Don't warn for unsigned char since that type is safe.
1424 Don't warn for signed char because anyone who uses that
1425 must have done so deliberately. */
1426 if (warn_char_subscripts
1427 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1428 warning ("array subscript has type `char'");
1430 /* Apply default promotions *after* noticing character types. */
1431 index = default_conversion (index);
1433 /* Require integer *after* promotion, for sake of enums. */
1434 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1436 error ("array subscript is not an integer");
1437 return error_mark_node;
1440 /* An array that is indexed by a non-constant
1441 cannot be stored in a register; we must be able to do
1442 address arithmetic on its address.
1443 Likewise an array of elements of variable size. */
1444 if (TREE_CODE (index) != INTEGER_CST
1445 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1446 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1448 if (!c_mark_addressable (array))
1449 return error_mark_node;
1451 /* An array that is indexed by a constant value which is not within
1452 the array bounds cannot be stored in a register either; because we
1453 would get a crash in store_bit_field/extract_bit_field when trying
1454 to access a non-existent part of the register. */
1455 if (TREE_CODE (index) == INTEGER_CST
1456 && TYPE_VALUES (TREE_TYPE (array))
1457 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1459 if (!c_mark_addressable (array))
1460 return error_mark_node;
1463 if (pedantic)
1465 tree foo = array;
1466 while (TREE_CODE (foo) == COMPONENT_REF)
1467 foo = TREE_OPERAND (foo, 0);
1468 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1469 pedwarn ("ISO C forbids subscripting `register' array");
1470 else if (! flag_isoc99 && ! lvalue_p (foo))
1471 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1474 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1475 rval = build (ARRAY_REF, type, array, index);
1476 /* Array ref is const/volatile if the array elements are
1477 or if the array is. */
1478 TREE_READONLY (rval)
1479 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1480 | TREE_READONLY (array));
1481 TREE_SIDE_EFFECTS (rval)
1482 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1483 | TREE_SIDE_EFFECTS (array));
1484 TREE_THIS_VOLATILE (rval)
1485 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1486 /* This was added by rms on 16 Nov 91.
1487 It fixes vol struct foo *a; a->elts[1]
1488 in an inline function.
1489 Hope it doesn't break something else. */
1490 | TREE_THIS_VOLATILE (array));
1491 return require_complete_type (fold (rval));
1495 tree ar = default_conversion (array);
1496 tree ind = default_conversion (index);
1498 /* Do the same warning check as above, but only on the part that's
1499 syntactically the index and only if it is also semantically
1500 the index. */
1501 if (warn_char_subscripts
1502 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1503 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1504 warning ("subscript has type `char'");
1506 /* Put the integer in IND to simplify error checking. */
1507 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1509 tree temp = ar;
1510 ar = ind;
1511 ind = temp;
1514 if (ar == error_mark_node)
1515 return ar;
1517 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1518 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1520 error ("subscripted value is neither array nor pointer");
1521 return error_mark_node;
1523 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1525 error ("array subscript is not an integer");
1526 return error_mark_node;
1529 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1530 "array indexing");
1534 /* Build an external reference to identifier ID. FUN indicates
1535 whether this will be used for a function call. */
1536 tree
1537 build_external_ref (tree id, int fun)
1539 tree ref;
1540 tree decl = lookup_name (id);
1541 tree objc_ivar = lookup_objc_ivar (id);
1543 if (decl && decl != error_mark_node)
1545 /* Properly declared variable or function reference. */
1546 if (!objc_ivar)
1547 ref = decl;
1548 else if (decl != objc_ivar && !C_DECL_FILE_SCOPE (decl))
1550 warning ("local declaration of `%s' hides instance variable",
1551 IDENTIFIER_POINTER (id));
1552 ref = decl;
1554 else
1555 ref = objc_ivar;
1557 else if (objc_ivar)
1558 ref = objc_ivar;
1559 else if (fun)
1560 /* Implicit function declaration. */
1561 ref = implicitly_declare (id);
1562 else if (decl == error_mark_node)
1563 /* Don't complain about something that's already been
1564 complained about. */
1565 return error_mark_node;
1566 else
1568 undeclared_variable (id);
1569 return error_mark_node;
1572 if (TREE_TYPE (ref) == error_mark_node)
1573 return error_mark_node;
1575 if (TREE_DEPRECATED (ref))
1576 warn_deprecated_use (ref);
1578 if (!skip_evaluation)
1579 assemble_external (ref);
1580 TREE_USED (ref) = 1;
1582 if (TREE_CODE (ref) == CONST_DECL)
1584 ref = DECL_INITIAL (ref);
1585 TREE_CONSTANT (ref) = 1;
1587 else if (current_function_decl != 0
1588 && !C_DECL_FILE_SCOPE (current_function_decl)
1589 && (TREE_CODE (ref) == VAR_DECL
1590 || TREE_CODE (ref) == PARM_DECL
1591 || TREE_CODE (ref) == FUNCTION_DECL))
1593 tree context = decl_function_context (ref);
1595 if (context != 0 && context != current_function_decl)
1596 DECL_NONLOCAL (ref) = 1;
1599 return ref;
1602 /* Build a function call to function FUNCTION with parameters PARAMS.
1603 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1604 TREE_VALUE of each node is a parameter-expression.
1605 FUNCTION's data type may be a function type or a pointer-to-function. */
1607 tree
1608 build_function_call (tree function, tree params)
1610 tree fntype, fundecl = 0;
1611 tree coerced_params;
1612 tree name = NULL_TREE, result;
1614 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
1615 STRIP_TYPE_NOPS (function);
1617 /* Convert anything with function type to a pointer-to-function. */
1618 if (TREE_CODE (function) == FUNCTION_DECL)
1620 name = DECL_NAME (function);
1622 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1623 (because calling an inline function does not mean the function
1624 needs to be separately compiled). */
1625 fntype = build_type_variant (TREE_TYPE (function),
1626 TREE_READONLY (function),
1627 TREE_THIS_VOLATILE (function));
1628 fundecl = function;
1629 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1631 else
1632 function = default_conversion (function);
1634 fntype = TREE_TYPE (function);
1636 if (TREE_CODE (fntype) == ERROR_MARK)
1637 return error_mark_node;
1639 if (!(TREE_CODE (fntype) == POINTER_TYPE
1640 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1642 error ("called object is not a function");
1643 return error_mark_node;
1646 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1647 current_function_returns_abnormally = 1;
1649 /* fntype now gets the type of function pointed to. */
1650 fntype = TREE_TYPE (fntype);
1652 /* Convert the parameters to the types declared in the
1653 function prototype, or apply default promotions. */
1655 coerced_params
1656 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1658 /* Check that the arguments to the function are valid. */
1660 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1662 /* Recognize certain built-in functions so we can make tree-codes
1663 other than CALL_EXPR. We do this when it enables fold-const.c
1664 to do something useful. */
1666 if (TREE_CODE (function) == ADDR_EXPR
1667 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1668 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1670 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1671 params, coerced_params);
1672 if (result)
1673 return result;
1676 result = build (CALL_EXPR, TREE_TYPE (fntype),
1677 function, coerced_params, NULL_TREE);
1678 TREE_SIDE_EFFECTS (result) = 1;
1679 result = fold (result);
1681 if (VOID_TYPE_P (TREE_TYPE (result)))
1682 return result;
1683 return require_complete_type (result);
1686 /* Convert the argument expressions in the list VALUES
1687 to the types in the list TYPELIST. The result is a list of converted
1688 argument expressions.
1690 If TYPELIST is exhausted, or when an element has NULL as its type,
1691 perform the default conversions.
1693 PARMLIST is the chain of parm decls for the function being called.
1694 It may be 0, if that info is not available.
1695 It is used only for generating error messages.
1697 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1699 This is also where warnings about wrong number of args are generated.
1701 Both VALUES and the returned value are chains of TREE_LIST nodes
1702 with the elements of the list in the TREE_VALUE slots of those nodes. */
1704 static tree
1705 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1707 tree typetail, valtail;
1708 tree result = NULL;
1709 int parmnum;
1711 /* Scan the given expressions and types, producing individual
1712 converted arguments and pushing them on RESULT in reverse order. */
1714 for (valtail = values, typetail = typelist, parmnum = 0;
1715 valtail;
1716 valtail = TREE_CHAIN (valtail), parmnum++)
1718 tree type = typetail ? TREE_VALUE (typetail) : 0;
1719 tree val = TREE_VALUE (valtail);
1721 if (type == void_type_node)
1723 if (name)
1724 error ("too many arguments to function `%s'",
1725 IDENTIFIER_POINTER (name));
1726 else
1727 error ("too many arguments to function");
1728 break;
1731 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1732 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1733 to convert automatically to a pointer. */
1734 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1735 val = TREE_OPERAND (val, 0);
1737 val = default_function_array_conversion (val);
1739 val = require_complete_type (val);
1741 if (type != 0)
1743 /* Formal parm type is specified by a function prototype. */
1744 tree parmval;
1746 if (!COMPLETE_TYPE_P (type))
1748 error ("type of formal parameter %d is incomplete", parmnum + 1);
1749 parmval = val;
1751 else
1753 /* Optionally warn about conversions that
1754 differ from the default conversions. */
1755 if (warn_conversion || warn_traditional)
1757 int formal_prec = TYPE_PRECISION (type);
1759 if (INTEGRAL_TYPE_P (type)
1760 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1761 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1762 if (INTEGRAL_TYPE_P (type)
1763 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1764 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1765 else if (TREE_CODE (type) == COMPLEX_TYPE
1766 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1767 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1768 else if (TREE_CODE (type) == REAL_TYPE
1769 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1770 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1771 else if (TREE_CODE (type) == COMPLEX_TYPE
1772 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1773 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1774 else if (TREE_CODE (type) == REAL_TYPE
1775 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1776 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1777 /* ??? At some point, messages should be written about
1778 conversions between complex types, but that's too messy
1779 to do now. */
1780 else if (TREE_CODE (type) == REAL_TYPE
1781 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1783 /* Warn if any argument is passed as `float',
1784 since without a prototype it would be `double'. */
1785 if (formal_prec == TYPE_PRECISION (float_type_node))
1786 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1788 /* Detect integer changing in width or signedness.
1789 These warnings are only activated with
1790 -Wconversion, not with -Wtraditional. */
1791 else if (warn_conversion && INTEGRAL_TYPE_P (type)
1792 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1794 tree would_have_been = default_conversion (val);
1795 tree type1 = TREE_TYPE (would_have_been);
1797 if (TREE_CODE (type) == ENUMERAL_TYPE
1798 && (TYPE_MAIN_VARIANT (type)
1799 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1800 /* No warning if function asks for enum
1801 and the actual arg is that enum type. */
1803 else if (formal_prec != TYPE_PRECISION (type1))
1804 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1805 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1807 /* Don't complain if the formal parameter type
1808 is an enum, because we can't tell now whether
1809 the value was an enum--even the same enum. */
1810 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1812 else if (TREE_CODE (val) == INTEGER_CST
1813 && int_fits_type_p (val, type))
1814 /* Change in signedness doesn't matter
1815 if a constant value is unaffected. */
1817 /* Likewise for a constant in a NOP_EXPR. */
1818 else if (TREE_CODE (val) == NOP_EXPR
1819 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1820 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1822 /* If the value is extended from a narrower
1823 unsigned type, it doesn't matter whether we
1824 pass it as signed or unsigned; the value
1825 certainly is the same either way. */
1826 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1827 && TREE_UNSIGNED (TREE_TYPE (val)))
1829 else if (TREE_UNSIGNED (type))
1830 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1831 else
1832 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1836 parmval = convert_for_assignment (type, val,
1837 (char *) 0, /* arg passing */
1838 fundecl, name, parmnum + 1);
1840 if (PROMOTE_PROTOTYPES
1841 && INTEGRAL_TYPE_P (type)
1842 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1843 parmval = default_conversion (parmval);
1845 result = tree_cons (NULL_TREE, parmval, result);
1847 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1848 && (TYPE_PRECISION (TREE_TYPE (val))
1849 < TYPE_PRECISION (double_type_node)))
1850 /* Convert `float' to `double'. */
1851 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1852 else
1853 /* Convert `short' and `char' to full-size `int'. */
1854 result = tree_cons (NULL_TREE, default_conversion (val), result);
1856 if (typetail)
1857 typetail = TREE_CHAIN (typetail);
1860 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1862 if (name)
1863 error ("too few arguments to function `%s'",
1864 IDENTIFIER_POINTER (name));
1865 else
1866 error ("too few arguments to function");
1869 return nreverse (result);
1872 /* This is the entry point used by the parser
1873 for binary operators in the input.
1874 In addition to constructing the expression,
1875 we check for operands that were written with other binary operators
1876 in a way that is likely to confuse the user. */
1878 tree
1879 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1881 tree result = build_binary_op (code, arg1, arg2, 1);
1883 char class;
1884 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1885 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1886 enum tree_code code1 = ERROR_MARK;
1887 enum tree_code code2 = ERROR_MARK;
1889 if (TREE_CODE (result) == ERROR_MARK)
1890 return error_mark_node;
1892 if (IS_EXPR_CODE_CLASS (class1))
1893 code1 = C_EXP_ORIGINAL_CODE (arg1);
1894 if (IS_EXPR_CODE_CLASS (class2))
1895 code2 = C_EXP_ORIGINAL_CODE (arg2);
1897 /* Check for cases such as x+y<<z which users are likely
1898 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1899 is cleared to prevent these warnings. */
1900 if (warn_parentheses)
1902 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1904 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1905 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1906 warning ("suggest parentheses around + or - inside shift");
1909 if (code == TRUTH_ORIF_EXPR)
1911 if (code1 == TRUTH_ANDIF_EXPR
1912 || code2 == TRUTH_ANDIF_EXPR)
1913 warning ("suggest parentheses around && within ||");
1916 if (code == BIT_IOR_EXPR)
1918 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1919 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1920 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1921 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1922 warning ("suggest parentheses around arithmetic in operand of |");
1923 /* Check cases like x|y==z */
1924 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1925 warning ("suggest parentheses around comparison in operand of |");
1928 if (code == BIT_XOR_EXPR)
1930 if (code1 == BIT_AND_EXPR
1931 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1932 || code2 == BIT_AND_EXPR
1933 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1934 warning ("suggest parentheses around arithmetic in operand of ^");
1935 /* Check cases like x^y==z */
1936 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1937 warning ("suggest parentheses around comparison in operand of ^");
1940 if (code == BIT_AND_EXPR)
1942 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1943 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1944 warning ("suggest parentheses around + or - in operand of &");
1945 /* Check cases like x&y==z */
1946 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1947 warning ("suggest parentheses around comparison in operand of &");
1951 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
1952 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
1953 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1954 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1956 unsigned_conversion_warning (result, arg1);
1957 unsigned_conversion_warning (result, arg2);
1958 overflow_warning (result);
1960 class = TREE_CODE_CLASS (TREE_CODE (result));
1962 /* Record the code that was specified in the source,
1963 for the sake of warnings about confusing nesting. */
1964 if (IS_EXPR_CODE_CLASS (class))
1965 C_SET_EXP_ORIGINAL_CODE (result, code);
1966 else
1968 int flag = TREE_CONSTANT (result);
1969 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
1970 so that convert_for_assignment wouldn't strip it.
1971 That way, we got warnings for things like p = (1 - 1).
1972 But it turns out we should not get those warnings. */
1973 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
1974 C_SET_EXP_ORIGINAL_CODE (result, code);
1975 TREE_CONSTANT (result) = flag;
1978 return result;
1982 /* Return true if `t' is known to be non-negative. */
1985 c_tree_expr_nonnegative_p (tree t)
1987 if (TREE_CODE (t) == STMT_EXPR)
1989 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
1991 /* Find the last statement in the chain, ignoring the final
1992 * scope statement */
1993 while (TREE_CHAIN (t) != NULL_TREE
1994 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
1995 t = TREE_CHAIN (t);
1996 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
1998 return tree_expr_nonnegative_p (t);
2001 /* Return a tree for the difference of pointers OP0 and OP1.
2002 The resulting tree has type int. */
2004 static tree
2005 pointer_diff (tree op0, tree op1)
2007 tree result, folded;
2008 tree restype = ptrdiff_type_node;
2010 tree target_type = TREE_TYPE (TREE_TYPE (op0));
2011 tree con0, con1, lit0, lit1;
2012 tree orig_op1 = op1;
2014 if (pedantic || warn_pointer_arith)
2016 if (TREE_CODE (target_type) == VOID_TYPE)
2017 pedwarn ("pointer of type `void *' used in subtraction");
2018 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2019 pedwarn ("pointer to a function used in subtraction");
2022 /* If the conversion to ptrdiff_type does anything like widening or
2023 converting a partial to an integral mode, we get a convert_expression
2024 that is in the way to do any simplifications.
2025 (fold-const.c doesn't know that the extra bits won't be needed.
2026 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2027 different mode in place.)
2028 So first try to find a common term here 'by hand'; we want to cover
2029 at least the cases that occur in legal static initializers. */
2030 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2031 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2033 if (TREE_CODE (con0) == PLUS_EXPR)
2035 lit0 = TREE_OPERAND (con0, 1);
2036 con0 = TREE_OPERAND (con0, 0);
2038 else
2039 lit0 = integer_zero_node;
2041 if (TREE_CODE (con1) == PLUS_EXPR)
2043 lit1 = TREE_OPERAND (con1, 1);
2044 con1 = TREE_OPERAND (con1, 0);
2046 else
2047 lit1 = integer_zero_node;
2049 if (operand_equal_p (con0, con1, 0))
2051 op0 = lit0;
2052 op1 = lit1;
2056 /* First do the subtraction as integers;
2057 then drop through to build the divide operator.
2058 Do not do default conversions on the minus operator
2059 in case restype is a short type. */
2061 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2062 convert (restype, op1), 0);
2063 /* This generates an error if op1 is pointer to incomplete type. */
2064 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2065 error ("arithmetic on pointer to an incomplete type");
2067 /* This generates an error if op0 is pointer to incomplete type. */
2068 op1 = c_size_in_bytes (target_type);
2070 /* Divide by the size, in easiest possible way. */
2072 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2074 folded = fold (result);
2075 if (folded == result)
2076 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2077 return folded;
2080 /* Construct and perhaps optimize a tree representation
2081 for a unary operation. CODE, a tree_code, specifies the operation
2082 and XARG is the operand.
2083 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2084 the default promotions (such as from short to int).
2085 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2086 allows non-lvalues; this is only used to handle conversion of non-lvalue
2087 arrays to pointers in C99. */
2089 tree
2090 build_unary_op (enum tree_code code, tree xarg, int flag)
2092 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
2093 tree arg = xarg;
2094 tree argtype = 0;
2095 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2096 tree val;
2097 int noconvert = flag;
2099 if (typecode == ERROR_MARK)
2100 return error_mark_node;
2101 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2102 typecode = INTEGER_TYPE;
2104 switch (code)
2106 case CONVERT_EXPR:
2107 /* This is used for unary plus, because a CONVERT_EXPR
2108 is enough to prevent anybody from looking inside for
2109 associativity, but won't generate any code. */
2110 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2111 || typecode == COMPLEX_TYPE))
2113 error ("wrong type argument to unary plus");
2114 return error_mark_node;
2116 else if (!noconvert)
2117 arg = default_conversion (arg);
2118 arg = non_lvalue (arg);
2119 break;
2121 case NEGATE_EXPR:
2122 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2123 || typecode == COMPLEX_TYPE
2124 || typecode == VECTOR_TYPE))
2126 error ("wrong type argument to unary minus");
2127 return error_mark_node;
2129 else if (!noconvert)
2130 arg = default_conversion (arg);
2131 break;
2133 case BIT_NOT_EXPR:
2134 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2136 if (!noconvert)
2137 arg = default_conversion (arg);
2139 else if (typecode == COMPLEX_TYPE)
2141 code = CONJ_EXPR;
2142 if (pedantic)
2143 pedwarn ("ISO C does not support `~' for complex conjugation");
2144 if (!noconvert)
2145 arg = default_conversion (arg);
2147 else
2149 error ("wrong type argument to bit-complement");
2150 return error_mark_node;
2152 break;
2154 case ABS_EXPR:
2155 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2156 || typecode == COMPLEX_TYPE))
2158 error ("wrong type argument to abs");
2159 return error_mark_node;
2161 else if (!noconvert)
2162 arg = default_conversion (arg);
2163 break;
2165 case CONJ_EXPR:
2166 /* Conjugating a real value is a no-op, but allow it anyway. */
2167 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2168 || typecode == COMPLEX_TYPE))
2170 error ("wrong type argument to conjugation");
2171 return error_mark_node;
2173 else if (!noconvert)
2174 arg = default_conversion (arg);
2175 break;
2177 case TRUTH_NOT_EXPR:
2178 if (typecode != INTEGER_TYPE
2179 && typecode != REAL_TYPE && typecode != POINTER_TYPE
2180 && typecode != COMPLEX_TYPE
2181 /* These will convert to a pointer. */
2182 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2184 error ("wrong type argument to unary exclamation mark");
2185 return error_mark_node;
2187 arg = c_common_truthvalue_conversion (arg);
2188 return invert_truthvalue (arg);
2190 case NOP_EXPR:
2191 break;
2193 case REALPART_EXPR:
2194 if (TREE_CODE (arg) == COMPLEX_CST)
2195 return TREE_REALPART (arg);
2196 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2197 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2198 else
2199 return arg;
2201 case IMAGPART_EXPR:
2202 if (TREE_CODE (arg) == COMPLEX_CST)
2203 return TREE_IMAGPART (arg);
2204 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2205 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2206 else
2207 return convert (TREE_TYPE (arg), integer_zero_node);
2209 case PREINCREMENT_EXPR:
2210 case POSTINCREMENT_EXPR:
2211 case PREDECREMENT_EXPR:
2212 case POSTDECREMENT_EXPR:
2213 /* Handle complex lvalues (when permitted)
2214 by reduction to simpler cases. */
2216 val = unary_complex_lvalue (code, arg, 0);
2217 if (val != 0)
2218 return val;
2220 /* Increment or decrement the real part of the value,
2221 and don't change the imaginary part. */
2222 if (typecode == COMPLEX_TYPE)
2224 tree real, imag;
2226 if (pedantic)
2227 pedwarn ("ISO C does not support `++' and `--' on complex types");
2229 arg = stabilize_reference (arg);
2230 real = build_unary_op (REALPART_EXPR, arg, 1);
2231 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2232 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2233 build_unary_op (code, real, 1), imag);
2236 /* Report invalid types. */
2238 if (typecode != POINTER_TYPE
2239 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2241 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2242 error ("wrong type argument to increment");
2243 else
2244 error ("wrong type argument to decrement");
2246 return error_mark_node;
2250 tree inc;
2251 tree result_type = TREE_TYPE (arg);
2253 arg = get_unwidened (arg, 0);
2254 argtype = TREE_TYPE (arg);
2256 /* Compute the increment. */
2258 if (typecode == POINTER_TYPE)
2260 /* If pointer target is an undefined struct,
2261 we just cannot know how to do the arithmetic. */
2262 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2264 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2265 error ("increment of pointer to unknown structure");
2266 else
2267 error ("decrement of pointer to unknown structure");
2269 else if ((pedantic || warn_pointer_arith)
2270 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2271 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2273 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2274 pedwarn ("wrong type argument to increment");
2275 else
2276 pedwarn ("wrong type argument to decrement");
2279 inc = c_size_in_bytes (TREE_TYPE (result_type));
2281 else
2282 inc = integer_one_node;
2284 inc = convert (argtype, inc);
2286 /* Handle incrementing a cast-expression. */
2288 while (1)
2289 switch (TREE_CODE (arg))
2291 case NOP_EXPR:
2292 case CONVERT_EXPR:
2293 case FLOAT_EXPR:
2294 case FIX_TRUNC_EXPR:
2295 case FIX_FLOOR_EXPR:
2296 case FIX_ROUND_EXPR:
2297 case FIX_CEIL_EXPR:
2298 pedantic_lvalue_warning (CONVERT_EXPR);
2299 /* If the real type has the same machine representation
2300 as the type it is cast to, we can make better output
2301 by adding directly to the inside of the cast. */
2302 if ((TREE_CODE (TREE_TYPE (arg))
2303 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
2304 && (TYPE_MODE (TREE_TYPE (arg))
2305 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
2306 arg = TREE_OPERAND (arg, 0);
2307 else
2309 tree incremented, modify, value;
2310 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2311 value = boolean_increment (code, arg);
2312 else
2314 arg = stabilize_reference (arg);
2315 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2316 value = arg;
2317 else
2318 value = save_expr (arg);
2319 incremented = build (((code == PREINCREMENT_EXPR
2320 || code == POSTINCREMENT_EXPR)
2321 ? PLUS_EXPR : MINUS_EXPR),
2322 argtype, value, inc);
2323 TREE_SIDE_EFFECTS (incremented) = 1;
2324 modify = build_modify_expr (arg, NOP_EXPR, incremented);
2325 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
2327 TREE_USED (value) = 1;
2328 return value;
2330 break;
2332 default:
2333 goto give_up;
2335 give_up:
2337 /* Complain about anything else that is not a true lvalue. */
2338 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2339 || code == POSTINCREMENT_EXPR)
2340 ? "invalid lvalue in increment"
2341 : "invalid lvalue in decrement")))
2342 return error_mark_node;
2344 /* Report a read-only lvalue. */
2345 if (TREE_READONLY (arg))
2346 readonly_warning (arg,
2347 ((code == PREINCREMENT_EXPR
2348 || code == POSTINCREMENT_EXPR)
2349 ? "increment" : "decrement"));
2351 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2352 val = boolean_increment (code, arg);
2353 else
2354 val = build (code, TREE_TYPE (arg), arg, inc);
2355 TREE_SIDE_EFFECTS (val) = 1;
2356 val = convert (result_type, val);
2357 if (TREE_CODE (val) != code)
2358 TREE_NO_UNUSED_WARNING (val) = 1;
2359 return val;
2362 case ADDR_EXPR:
2363 /* Note that this operation never does default_conversion. */
2365 /* Let &* cancel out to simplify resulting code. */
2366 if (TREE_CODE (arg) == INDIRECT_REF)
2368 /* Don't let this be an lvalue. */
2369 if (lvalue_p (TREE_OPERAND (arg, 0)))
2370 return non_lvalue (TREE_OPERAND (arg, 0));
2371 return TREE_OPERAND (arg, 0);
2374 /* For &x[y], return x+y */
2375 if (TREE_CODE (arg) == ARRAY_REF)
2377 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2378 return error_mark_node;
2379 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2380 TREE_OPERAND (arg, 1), 1);
2383 /* Handle complex lvalues (when permitted)
2384 by reduction to simpler cases. */
2385 val = unary_complex_lvalue (code, arg, flag);
2386 if (val != 0)
2387 return val;
2389 /* Anything not already handled and not a true memory reference
2390 or a non-lvalue array is an error. */
2391 else if (typecode != FUNCTION_TYPE && !flag
2392 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2393 return error_mark_node;
2395 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
2396 argtype = TREE_TYPE (arg);
2398 /* If the lvalue is const or volatile, merge that into the type
2399 to which the address will point. Note that you can't get a
2400 restricted pointer by taking the address of something, so we
2401 only have to deal with `const' and `volatile' here. */
2402 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2403 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2404 argtype = c_build_type_variant (argtype,
2405 TREE_READONLY (arg),
2406 TREE_THIS_VOLATILE (arg));
2408 argtype = build_pointer_type (argtype);
2410 if (!c_mark_addressable (arg))
2411 return error_mark_node;
2414 tree addr;
2416 if (TREE_CODE (arg) == COMPONENT_REF)
2418 tree field = TREE_OPERAND (arg, 1);
2420 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2422 if (DECL_C_BIT_FIELD (field))
2424 error ("attempt to take address of bit-field structure member `%s'",
2425 IDENTIFIER_POINTER (DECL_NAME (field)));
2426 return error_mark_node;
2429 addr = fold (build (PLUS_EXPR, argtype,
2430 convert (argtype, addr),
2431 convert (argtype, byte_position (field))));
2433 else
2434 addr = build1 (code, argtype, arg);
2436 /* Address of a static or external variable or
2437 file-scope function counts as a constant. */
2438 if (staticp (arg)
2439 && ! (TREE_CODE (arg) == FUNCTION_DECL
2440 && !C_DECL_FILE_SCOPE (arg)))
2441 TREE_CONSTANT (addr) = 1;
2442 return addr;
2445 default:
2446 break;
2449 if (argtype == 0)
2450 argtype = TREE_TYPE (arg);
2451 return fold (build1 (code, argtype, arg));
2454 /* Return nonzero if REF is an lvalue valid for this language.
2455 Lvalues can be assigned, unless their type has TYPE_READONLY.
2456 Lvalues can have their address taken, unless they have DECL_REGISTER. */
2459 lvalue_p (tree ref)
2461 enum tree_code code = TREE_CODE (ref);
2463 switch (code)
2465 case REALPART_EXPR:
2466 case IMAGPART_EXPR:
2467 case COMPONENT_REF:
2468 return lvalue_p (TREE_OPERAND (ref, 0));
2470 case COMPOUND_LITERAL_EXPR:
2471 case STRING_CST:
2472 return 1;
2474 case INDIRECT_REF:
2475 case ARRAY_REF:
2476 case VAR_DECL:
2477 case PARM_DECL:
2478 case RESULT_DECL:
2479 case ERROR_MARK:
2480 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2481 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2483 case BIND_EXPR:
2484 case RTL_EXPR:
2485 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2487 default:
2488 return 0;
2492 /* Return nonzero if REF is an lvalue valid for this language;
2493 otherwise, print an error message and return zero. */
2496 lvalue_or_else (tree ref, const char *msgid)
2498 int win = lvalue_p (ref);
2500 if (! win)
2501 error ("%s", msgid);
2503 return win;
2506 /* Apply unary lvalue-demanding operator CODE to the expression ARG
2507 for certain kinds of expressions which are not really lvalues
2508 but which we can accept as lvalues. If FLAG is nonzero, then
2509 non-lvalues are OK since we may be converting a non-lvalue array to
2510 a pointer in C99.
2512 If ARG is not a kind of expression we can handle, return zero. */
2514 static tree
2515 unary_complex_lvalue (enum tree_code code, tree arg, int flag)
2517 /* Handle (a, b) used as an "lvalue". */
2518 if (TREE_CODE (arg) == COMPOUND_EXPR)
2520 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
2522 /* If this returns a function type, it isn't really being used as
2523 an lvalue, so don't issue a warning about it. */
2524 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2525 pedantic_lvalue_warning (COMPOUND_EXPR);
2527 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
2528 TREE_OPERAND (arg, 0), real_result);
2531 /* Handle (a ? b : c) used as an "lvalue". */
2532 if (TREE_CODE (arg) == COND_EXPR)
2534 if (!flag)
2535 pedantic_lvalue_warning (COND_EXPR);
2536 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
2537 pedantic_lvalue_warning (COMPOUND_EXPR);
2539 return (build_conditional_expr
2540 (TREE_OPERAND (arg, 0),
2541 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
2542 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
2545 return 0;
2548 /* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
2549 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
2551 static void
2552 pedantic_lvalue_warning (enum tree_code code)
2554 if (pedantic)
2555 switch (code)
2557 case COND_EXPR:
2558 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
2559 break;
2560 case COMPOUND_EXPR:
2561 pedwarn ("ISO C forbids use of compound expressions as lvalues");
2562 break;
2563 default:
2564 pedwarn ("ISO C forbids use of cast expressions as lvalues");
2565 break;
2569 /* Warn about storing in something that is `const'. */
2571 void
2572 readonly_warning (tree arg, const char *msgid)
2574 if (TREE_CODE (arg) == COMPONENT_REF)
2576 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2577 readonly_warning (TREE_OPERAND (arg, 0), msgid);
2578 else
2579 pedwarn ("%s of read-only member `%s'", _(msgid),
2580 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2582 else if (TREE_CODE (arg) == VAR_DECL)
2583 pedwarn ("%s of read-only variable `%s'", _(msgid),
2584 IDENTIFIER_POINTER (DECL_NAME (arg)));
2585 else
2586 pedwarn ("%s of read-only location", _(msgid));
2589 /* Mark EXP saying that we need to be able to take the
2590 address of it; it should not be allocated in a register.
2591 Returns true if successful. */
2593 bool
2594 c_mark_addressable (tree exp)
2596 tree x = exp;
2598 while (1)
2599 switch (TREE_CODE (x))
2601 case COMPONENT_REF:
2602 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2604 error ("cannot take address of bit-field `%s'",
2605 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2606 return false;
2609 /* ... fall through ... */
2611 case ADDR_EXPR:
2612 case ARRAY_REF:
2613 case REALPART_EXPR:
2614 case IMAGPART_EXPR:
2615 x = TREE_OPERAND (x, 0);
2616 break;
2618 case COMPOUND_LITERAL_EXPR:
2619 case CONSTRUCTOR:
2620 TREE_ADDRESSABLE (x) = 1;
2621 return true;
2623 case VAR_DECL:
2624 case CONST_DECL:
2625 case PARM_DECL:
2626 case RESULT_DECL:
2627 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
2628 && DECL_NONLOCAL (x))
2630 if (TREE_PUBLIC (x))
2632 error ("global register variable `%s' used in nested function",
2633 IDENTIFIER_POINTER (DECL_NAME (x)));
2634 return false;
2636 pedwarn ("register variable `%s' used in nested function",
2637 IDENTIFIER_POINTER (DECL_NAME (x)));
2639 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
2641 if (TREE_PUBLIC (x))
2643 error ("address of global register variable `%s' requested",
2644 IDENTIFIER_POINTER (DECL_NAME (x)));
2645 return false;
2648 /* If we are making this addressable due to its having
2649 volatile components, give a different error message. Also
2650 handle the case of an unnamed parameter by not trying
2651 to give the name. */
2653 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
2655 error ("cannot put object with volatile field into register");
2656 return false;
2659 pedwarn ("address of register variable `%s' requested",
2660 IDENTIFIER_POINTER (DECL_NAME (x)));
2662 put_var_into_stack (x, /*rescan=*/true);
2664 /* drops in */
2665 case FUNCTION_DECL:
2666 TREE_ADDRESSABLE (x) = 1;
2667 /* drops out */
2668 default:
2669 return true;
2673 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
2675 tree
2676 build_conditional_expr (tree ifexp, tree op1, tree op2)
2678 tree type1;
2679 tree type2;
2680 enum tree_code code1;
2681 enum tree_code code2;
2682 tree result_type = NULL;
2683 tree orig_op1 = op1, orig_op2 = op2;
2685 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
2687 /* Promote both alternatives. */
2689 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2690 op1 = default_conversion (op1);
2691 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2692 op2 = default_conversion (op2);
2694 if (TREE_CODE (ifexp) == ERROR_MARK
2695 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2696 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2697 return error_mark_node;
2699 type1 = TREE_TYPE (op1);
2700 code1 = TREE_CODE (type1);
2701 type2 = TREE_TYPE (op2);
2702 code2 = TREE_CODE (type2);
2704 /* Quickly detect the usual case where op1 and op2 have the same type
2705 after promotion. */
2706 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2708 if (type1 == type2)
2709 result_type = type1;
2710 else
2711 result_type = TYPE_MAIN_VARIANT (type1);
2713 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2714 || code1 == COMPLEX_TYPE)
2715 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2716 || code2 == COMPLEX_TYPE))
2718 result_type = common_type (type1, type2);
2720 /* If -Wsign-compare, warn here if type1 and type2 have
2721 different signedness. We'll promote the signed to unsigned
2722 and later code won't know it used to be different.
2723 Do this check on the original types, so that explicit casts
2724 will be considered, but default promotions won't. */
2725 if (warn_sign_compare && !skip_evaluation)
2727 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
2728 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
2730 if (unsigned_op1 ^ unsigned_op2)
2732 /* Do not warn if the result type is signed, since the
2733 signed type will only be chosen if it can represent
2734 all the values of the unsigned type. */
2735 if (! TREE_UNSIGNED (result_type))
2736 /* OK */;
2737 /* Do not warn if the signed quantity is an unsuffixed
2738 integer literal (or some static constant expression
2739 involving such literals) and it is non-negative. */
2740 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2741 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2742 /* OK */;
2743 else
2744 warning ("signed and unsigned type in conditional expression");
2748 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2750 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2751 pedwarn ("ISO C forbids conditional expr with only one void side");
2752 result_type = void_type_node;
2754 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2756 if (comp_target_types (type1, type2, 1))
2757 result_type = common_type (type1, type2);
2758 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2759 && TREE_CODE (orig_op1) != NOP_EXPR)
2760 result_type = qualify_type (type2, type1);
2761 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2762 && TREE_CODE (orig_op2) != NOP_EXPR)
2763 result_type = qualify_type (type1, type2);
2764 else if (VOID_TYPE_P (TREE_TYPE (type1)))
2766 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2767 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2768 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2769 TREE_TYPE (type2)));
2771 else if (VOID_TYPE_P (TREE_TYPE (type2)))
2773 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2774 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2775 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2776 TREE_TYPE (type1)));
2778 else
2780 pedwarn ("pointer type mismatch in conditional expression");
2781 result_type = build_pointer_type (void_type_node);
2784 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2786 if (! integer_zerop (op2))
2787 pedwarn ("pointer/integer type mismatch in conditional expression");
2788 else
2790 op2 = null_pointer_node;
2792 result_type = type1;
2794 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2796 if (!integer_zerop (op1))
2797 pedwarn ("pointer/integer type mismatch in conditional expression");
2798 else
2800 op1 = null_pointer_node;
2802 result_type = type2;
2805 if (!result_type)
2807 if (flag_cond_mismatch)
2808 result_type = void_type_node;
2809 else
2811 error ("type mismatch in conditional expression");
2812 return error_mark_node;
2816 /* Merge const and volatile flags of the incoming types. */
2817 result_type
2818 = build_type_variant (result_type,
2819 TREE_READONLY (op1) || TREE_READONLY (op2),
2820 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2822 if (result_type != TREE_TYPE (op1))
2823 op1 = convert_and_check (result_type, op1);
2824 if (result_type != TREE_TYPE (op2))
2825 op2 = convert_and_check (result_type, op2);
2827 if (TREE_CODE (ifexp) == INTEGER_CST)
2828 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2830 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2833 /* Given a list of expressions, return a compound expression
2834 that performs them all and returns the value of the last of them. */
2836 tree
2837 build_compound_expr (tree list)
2839 return internal_build_compound_expr (list, TRUE);
2842 static tree
2843 internal_build_compound_expr (tree list, int first_p)
2845 tree rest;
2847 if (TREE_CHAIN (list) == 0)
2849 /* Convert arrays and functions to pointers when there
2850 really is a comma operator. */
2851 if (!first_p)
2852 TREE_VALUE (list)
2853 = default_function_array_conversion (TREE_VALUE (list));
2855 /* Don't let (0, 0) be null pointer constant. */
2856 if (!first_p && integer_zerop (TREE_VALUE (list)))
2857 return non_lvalue (TREE_VALUE (list));
2858 return TREE_VALUE (list);
2861 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2863 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2865 /* The left-hand operand of a comma expression is like an expression
2866 statement: with -Wextra or -Wunused, we should warn if it doesn't have
2867 any side-effects, unless it was explicitly cast to (void). */
2868 if (warn_unused_value
2869 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2870 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2871 warning ("left-hand operand of comma expression has no effect");
2873 /* When pedantic, a compound expression can be neither an lvalue
2874 nor an integer constant expression. */
2875 if (! pedantic)
2876 return rest;
2879 /* With -Wunused, we should also warn if the left-hand operand does have
2880 side-effects, but computes a value which is not used. For example, in
2881 `foo() + bar(), baz()' the result of the `+' operator is not used,
2882 so we should issue a warning. */
2883 else if (warn_unused_value)
2884 warn_if_unused_value (TREE_VALUE (list));
2886 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2889 /* Build an expression representing a cast to type TYPE of expression EXPR. */
2891 tree
2892 build_c_cast (tree type, tree expr)
2894 tree value = expr;
2896 if (type == error_mark_node || expr == error_mark_node)
2897 return error_mark_node;
2899 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2900 only in <protocol> qualifications. But when constructing cast expressions,
2901 the protocols do matter and must be kept around. */
2902 if (!c_dialect_objc () || !objc_is_id (type))
2903 type = TYPE_MAIN_VARIANT (type);
2905 if (TREE_CODE (type) == ARRAY_TYPE)
2907 error ("cast specifies array type");
2908 return error_mark_node;
2911 if (TREE_CODE (type) == FUNCTION_TYPE)
2913 error ("cast specifies function type");
2914 return error_mark_node;
2917 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2919 if (pedantic)
2921 if (TREE_CODE (type) == RECORD_TYPE
2922 || TREE_CODE (type) == UNION_TYPE)
2923 pedwarn ("ISO C forbids casting nonscalar to the same type");
2926 else if (TREE_CODE (type) == UNION_TYPE)
2928 tree field;
2929 value = default_function_array_conversion (value);
2931 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2932 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2933 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
2934 break;
2936 if (field)
2938 tree t;
2940 if (pedantic)
2941 pedwarn ("ISO C forbids casts to union type");
2942 t = digest_init (type,
2943 build_constructor (type,
2944 build_tree_list (field, value)),
2946 TREE_CONSTANT (t) = TREE_CONSTANT (value);
2947 return t;
2949 error ("cast to union type from type not present in union");
2950 return error_mark_node;
2952 else
2954 tree otype, ovalue;
2956 /* If casting to void, avoid the error that would come
2957 from default_conversion in the case of a non-lvalue array. */
2958 if (type == void_type_node)
2959 return build1 (CONVERT_EXPR, type, value);
2961 /* Convert functions and arrays to pointers,
2962 but don't convert any other types. */
2963 value = default_function_array_conversion (value);
2964 otype = TREE_TYPE (value);
2966 /* Optionally warn about potentially worrisome casts. */
2968 if (warn_cast_qual
2969 && TREE_CODE (type) == POINTER_TYPE
2970 && TREE_CODE (otype) == POINTER_TYPE)
2972 tree in_type = type;
2973 tree in_otype = otype;
2974 int added = 0;
2975 int discarded = 0;
2977 /* Check that the qualifiers on IN_TYPE are a superset of
2978 the qualifiers of IN_OTYPE. The outermost level of
2979 POINTER_TYPE nodes is uninteresting and we stop as soon
2980 as we hit a non-POINTER_TYPE node on either type. */
2983 in_otype = TREE_TYPE (in_otype);
2984 in_type = TREE_TYPE (in_type);
2986 /* GNU C allows cv-qualified function types. 'const'
2987 means the function is very pure, 'volatile' means it
2988 can't return. We need to warn when such qualifiers
2989 are added, not when they're taken away. */
2990 if (TREE_CODE (in_otype) == FUNCTION_TYPE
2991 && TREE_CODE (in_type) == FUNCTION_TYPE)
2992 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
2993 else
2994 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
2996 while (TREE_CODE (in_type) == POINTER_TYPE
2997 && TREE_CODE (in_otype) == POINTER_TYPE);
2999 if (added)
3000 warning ("cast adds new qualifiers to function type");
3002 if (discarded)
3003 /* There are qualifiers present in IN_OTYPE that are not
3004 present in IN_TYPE. */
3005 warning ("cast discards qualifiers from pointer target type");
3008 /* Warn about possible alignment problems. */
3009 if (STRICT_ALIGNMENT && warn_cast_align
3010 && TREE_CODE (type) == POINTER_TYPE
3011 && TREE_CODE (otype) == POINTER_TYPE
3012 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3013 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3014 /* Don't warn about opaque types, where the actual alignment
3015 restriction is unknown. */
3016 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3017 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3018 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
3019 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3020 warning ("cast increases required alignment of target type");
3022 if (TREE_CODE (type) == INTEGER_TYPE
3023 && TREE_CODE (otype) == POINTER_TYPE
3024 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3025 && !TREE_CONSTANT (value))
3026 warning ("cast from pointer to integer of different size");
3028 if (warn_bad_function_cast
3029 && TREE_CODE (value) == CALL_EXPR
3030 && TREE_CODE (type) != TREE_CODE (otype))
3031 warning ("cast does not match function type");
3033 if (TREE_CODE (type) == POINTER_TYPE
3034 && TREE_CODE (otype) == INTEGER_TYPE
3035 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3036 /* Don't warn about converting any constant. */
3037 && !TREE_CONSTANT (value))
3038 warning ("cast to pointer from integer of different size");
3040 if (TREE_CODE (type) == POINTER_TYPE
3041 && TREE_CODE (otype) == POINTER_TYPE
3042 && TREE_CODE (expr) == ADDR_EXPR
3043 && DECL_P (TREE_OPERAND (expr, 0))
3044 && flag_strict_aliasing && warn_strict_aliasing
3045 && !VOID_TYPE_P (TREE_TYPE (type)))
3047 /* Casting the address of a decl to non void pointer. Warn
3048 if the cast breaks type based aliasing. */
3049 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3050 warning ("type-punning to incomplete type might break strict-aliasing rules");
3051 else if (!alias_sets_conflict_p
3052 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3053 get_alias_set (TREE_TYPE (type))))
3054 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3057 ovalue = value;
3058 /* Replace a nonvolatile const static variable with its value. */
3059 if (optimize && TREE_CODE (value) == VAR_DECL)
3060 value = decl_constant_value (value);
3061 value = convert (type, value);
3063 /* Ignore any integer overflow caused by the cast. */
3064 if (TREE_CODE (value) == INTEGER_CST)
3066 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3067 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3071 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
3072 if (pedantic && TREE_CODE (value) == INTEGER_CST
3073 && TREE_CODE (expr) == INTEGER_CST
3074 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3075 value = non_lvalue (value);
3077 /* If pedantic, don't let a cast be an lvalue. */
3078 if (value == expr && pedantic)
3079 value = non_lvalue (value);
3081 return value;
3084 /* Interpret a cast of expression EXPR to type TYPE. */
3085 tree
3086 c_cast_expr (tree type, tree expr)
3088 int saved_wsp = warn_strict_prototypes;
3090 /* This avoids warnings about unprototyped casts on
3091 integers. E.g. "#define SIG_DFL (void(*)())0". */
3092 if (TREE_CODE (expr) == INTEGER_CST)
3093 warn_strict_prototypes = 0;
3094 type = groktypename (type);
3095 warn_strict_prototypes = saved_wsp;
3097 return build_c_cast (type, expr);
3101 /* Build an assignment expression of lvalue LHS from value RHS.
3102 MODIFYCODE is the code for a binary operator that we use
3103 to combine the old value of LHS with RHS to get the new value.
3104 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3106 tree
3107 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3109 tree result;
3110 tree newrhs;
3111 tree lhstype = TREE_TYPE (lhs);
3112 tree olhstype = lhstype;
3114 /* Types that aren't fully specified cannot be used in assignments. */
3115 lhs = require_complete_type (lhs);
3117 /* Avoid duplicate error messages from operands that had errors. */
3118 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3119 return error_mark_node;
3121 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3122 /* Do not use STRIP_NOPS here. We do not want an enumerator
3123 whose value is 0 to count as a null pointer constant. */
3124 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3125 rhs = TREE_OPERAND (rhs, 0);
3127 newrhs = rhs;
3129 /* Handle control structure constructs used as "lvalues". */
3131 switch (TREE_CODE (lhs))
3133 /* Handle (a, b) used as an "lvalue". */
3134 case COMPOUND_EXPR:
3135 pedantic_lvalue_warning (COMPOUND_EXPR);
3136 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
3137 if (TREE_CODE (newrhs) == ERROR_MARK)
3138 return error_mark_node;
3139 return build (COMPOUND_EXPR, lhstype,
3140 TREE_OPERAND (lhs, 0), newrhs);
3142 /* Handle (a ? b : c) used as an "lvalue". */
3143 case COND_EXPR:
3144 pedantic_lvalue_warning (COND_EXPR);
3145 rhs = save_expr (rhs);
3147 /* Produce (a ? (b = rhs) : (c = rhs))
3148 except that the RHS goes through a save-expr
3149 so the code to compute it is only emitted once. */
3150 tree cond
3151 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3152 build_modify_expr (TREE_OPERAND (lhs, 1),
3153 modifycode, rhs),
3154 build_modify_expr (TREE_OPERAND (lhs, 2),
3155 modifycode, rhs));
3156 if (TREE_CODE (cond) == ERROR_MARK)
3157 return cond;
3158 /* Make sure the code to compute the rhs comes out
3159 before the split. */
3160 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3161 /* But cast it to void to avoid an "unused" error. */
3162 convert (void_type_node, rhs), cond);
3164 default:
3165 break;
3168 /* If a binary op has been requested, combine the old LHS value with the RHS
3169 producing the value we should actually store into the LHS. */
3171 if (modifycode != NOP_EXPR)
3173 lhs = stabilize_reference (lhs);
3174 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3177 /* Handle a cast used as an "lvalue".
3178 We have already performed any binary operator using the value as cast.
3179 Now convert the result to the cast type of the lhs,
3180 and then true type of the lhs and store it there;
3181 then convert result back to the cast type to be the value
3182 of the assignment. */
3184 switch (TREE_CODE (lhs))
3186 case NOP_EXPR:
3187 case CONVERT_EXPR:
3188 case FLOAT_EXPR:
3189 case FIX_TRUNC_EXPR:
3190 case FIX_FLOOR_EXPR:
3191 case FIX_ROUND_EXPR:
3192 case FIX_CEIL_EXPR:
3193 newrhs = default_function_array_conversion (newrhs);
3195 tree inner_lhs = TREE_OPERAND (lhs, 0);
3196 tree result;
3197 result = build_modify_expr (inner_lhs, NOP_EXPR,
3198 convert (TREE_TYPE (inner_lhs),
3199 convert (lhstype, newrhs)));
3200 if (TREE_CODE (result) == ERROR_MARK)
3201 return result;
3202 pedantic_lvalue_warning (CONVERT_EXPR);
3203 return convert (TREE_TYPE (lhs), result);
3206 default:
3207 break;
3210 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3211 Reject anything strange now. */
3213 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3214 return error_mark_node;
3216 /* Warn about storing in something that is `const'. */
3218 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3219 || ((TREE_CODE (lhstype) == RECORD_TYPE
3220 || TREE_CODE (lhstype) == UNION_TYPE)
3221 && C_TYPE_FIELDS_READONLY (lhstype)))
3222 readonly_warning (lhs, "assignment");
3224 /* If storing into a structure or union member,
3225 it has probably been given type `int'.
3226 Compute the type that would go with
3227 the actual amount of storage the member occupies. */
3229 if (TREE_CODE (lhs) == COMPONENT_REF
3230 && (TREE_CODE (lhstype) == INTEGER_TYPE
3231 || TREE_CODE (lhstype) == BOOLEAN_TYPE
3232 || TREE_CODE (lhstype) == REAL_TYPE
3233 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3234 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3236 /* If storing in a field that is in actuality a short or narrower than one,
3237 we must store in the field in its actual type. */
3239 if (lhstype != TREE_TYPE (lhs))
3241 lhs = copy_node (lhs);
3242 TREE_TYPE (lhs) = lhstype;
3245 /* Convert new value to destination type. */
3247 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3248 NULL_TREE, NULL_TREE, 0);
3249 if (TREE_CODE (newrhs) == ERROR_MARK)
3250 return error_mark_node;
3252 /* Scan operands */
3254 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3255 TREE_SIDE_EFFECTS (result) = 1;
3257 /* If we got the LHS in a different type for storing in,
3258 convert the result back to the nominal type of LHS
3259 so that the value we return always has the same type
3260 as the LHS argument. */
3262 if (olhstype == TREE_TYPE (result))
3263 return result;
3264 return convert_for_assignment (olhstype, result, _("assignment"),
3265 NULL_TREE, NULL_TREE, 0);
3268 /* Convert value RHS to type TYPE as preparation for an assignment
3269 to an lvalue of type TYPE.
3270 The real work of conversion is done by `convert'.
3271 The purpose of this function is to generate error messages
3272 for assignments that are not allowed in C.
3273 ERRTYPE is a string to use in error messages:
3274 "assignment", "return", etc. If it is null, this is parameter passing
3275 for a function call (and different error messages are output).
3277 FUNNAME is the name of the function being called,
3278 as an IDENTIFIER_NODE, or null.
3279 PARMNUM is the number of the argument, for printing in error messages. */
3281 static tree
3282 convert_for_assignment (tree type, tree rhs, const char *errtype,
3283 tree fundecl, tree funname, int parmnum)
3285 enum tree_code codel = TREE_CODE (type);
3286 tree rhstype;
3287 enum tree_code coder;
3289 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3290 /* Do not use STRIP_NOPS here. We do not want an enumerator
3291 whose value is 0 to count as a null pointer constant. */
3292 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3293 rhs = TREE_OPERAND (rhs, 0);
3295 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3296 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3297 rhs = default_conversion (rhs);
3298 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3299 rhs = decl_constant_value_for_broken_optimization (rhs);
3301 rhstype = TREE_TYPE (rhs);
3302 coder = TREE_CODE (rhstype);
3304 if (coder == ERROR_MARK)
3305 return error_mark_node;
3307 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3309 overflow_warning (rhs);
3310 /* Check for Objective-C protocols. This will automatically
3311 issue a warning if there are protocol violations. No need to
3312 use the return value. */
3313 if (c_dialect_objc ())
3314 objc_comptypes (type, rhstype, 0);
3315 return rhs;
3318 if (coder == VOID_TYPE)
3320 error ("void value not ignored as it ought to be");
3321 return error_mark_node;
3323 /* A type converts to a reference to it.
3324 This code doesn't fully support references, it's just for the
3325 special case of va_start and va_copy. */
3326 if (codel == REFERENCE_TYPE
3327 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3329 if (!lvalue_p (rhs))
3331 error ("cannot pass rvalue to reference parameter");
3332 return error_mark_node;
3334 if (!c_mark_addressable (rhs))
3335 return error_mark_node;
3336 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3338 /* We already know that these two types are compatible, but they
3339 may not be exactly identical. In fact, `TREE_TYPE (type)' is
3340 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3341 likely to be va_list, a typedef to __builtin_va_list, which
3342 is different enough that it will cause problems later. */
3343 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3344 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3346 rhs = build1 (NOP_EXPR, type, rhs);
3347 return rhs;
3349 /* Some types can interconvert without explicit casts. */
3350 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3351 && ((*targetm.vector_opaque_p) (type)
3352 || (*targetm.vector_opaque_p) (rhstype)))
3353 return convert (type, rhs);
3354 /* Arithmetic types all interconvert, and enum is treated like int. */
3355 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3356 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3357 || codel == BOOLEAN_TYPE)
3358 && (coder == INTEGER_TYPE || coder == REAL_TYPE
3359 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3360 || coder == BOOLEAN_TYPE))
3361 return convert_and_check (type, rhs);
3363 /* Conversion to a transparent union from its member types.
3364 This applies only to function arguments. */
3365 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3367 tree memb_types;
3368 tree marginal_memb_type = 0;
3370 for (memb_types = TYPE_FIELDS (type); memb_types;
3371 memb_types = TREE_CHAIN (memb_types))
3373 tree memb_type = TREE_TYPE (memb_types);
3375 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3376 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3377 break;
3379 if (TREE_CODE (memb_type) != POINTER_TYPE)
3380 continue;
3382 if (coder == POINTER_TYPE)
3384 tree ttl = TREE_TYPE (memb_type);
3385 tree ttr = TREE_TYPE (rhstype);
3387 /* Any non-function converts to a [const][volatile] void *
3388 and vice versa; otherwise, targets must be the same.
3389 Meanwhile, the lhs target must have all the qualifiers of
3390 the rhs. */
3391 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3392 || comp_target_types (memb_type, rhstype, 0))
3394 /* If this type won't generate any warnings, use it. */
3395 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3396 || ((TREE_CODE (ttr) == FUNCTION_TYPE
3397 && TREE_CODE (ttl) == FUNCTION_TYPE)
3398 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3399 == TYPE_QUALS (ttr))
3400 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3401 == TYPE_QUALS (ttl))))
3402 break;
3404 /* Keep looking for a better type, but remember this one. */
3405 if (! marginal_memb_type)
3406 marginal_memb_type = memb_type;
3410 /* Can convert integer zero to any pointer type. */
3411 if (integer_zerop (rhs)
3412 || (TREE_CODE (rhs) == NOP_EXPR
3413 && integer_zerop (TREE_OPERAND (rhs, 0))))
3415 rhs = null_pointer_node;
3416 break;
3420 if (memb_types || marginal_memb_type)
3422 if (! memb_types)
3424 /* We have only a marginally acceptable member type;
3425 it needs a warning. */
3426 tree ttl = TREE_TYPE (marginal_memb_type);
3427 tree ttr = TREE_TYPE (rhstype);
3429 /* Const and volatile mean something different for function
3430 types, so the usual warnings are not appropriate. */
3431 if (TREE_CODE (ttr) == FUNCTION_TYPE
3432 && TREE_CODE (ttl) == FUNCTION_TYPE)
3434 /* Because const and volatile on functions are
3435 restrictions that say the function will not do
3436 certain things, it is okay to use a const or volatile
3437 function where an ordinary one is wanted, but not
3438 vice-versa. */
3439 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3440 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3441 errtype, funname, parmnum);
3443 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3444 warn_for_assignment ("%s discards qualifiers from pointer target type",
3445 errtype, funname,
3446 parmnum);
3449 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3450 pedwarn ("ISO C prohibits argument conversion to union type");
3452 return build1 (NOP_EXPR, type, rhs);
3456 /* Conversions among pointers */
3457 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3458 && (coder == codel))
3460 tree ttl = TREE_TYPE (type);
3461 tree ttr = TREE_TYPE (rhstype);
3462 bool is_opaque_pointer;
3464 /* Opaque pointers are treated like void pointers. */
3465 is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
3466 || (*targetm.vector_opaque_p) (rhstype))
3467 && TREE_CODE (ttl) == VECTOR_TYPE
3468 && TREE_CODE (ttr) == VECTOR_TYPE;
3470 /* Any non-function converts to a [const][volatile] void *
3471 and vice versa; otherwise, targets must be the same.
3472 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
3473 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3474 || comp_target_types (type, rhstype, 0)
3475 || is_opaque_pointer
3476 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3477 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3479 if (pedantic
3480 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3482 (VOID_TYPE_P (ttr)
3483 /* Check TREE_CODE to catch cases like (void *) (char *) 0
3484 which are not ANSI null ptr constants. */
3485 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3486 && TREE_CODE (ttl) == FUNCTION_TYPE)))
3487 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3488 errtype, funname, parmnum);
3489 /* Const and volatile mean something different for function types,
3490 so the usual warnings are not appropriate. */
3491 else if (TREE_CODE (ttr) != FUNCTION_TYPE
3492 && TREE_CODE (ttl) != FUNCTION_TYPE)
3494 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3495 warn_for_assignment ("%s discards qualifiers from pointer target type",
3496 errtype, funname, parmnum);
3497 /* If this is not a case of ignoring a mismatch in signedness,
3498 no warning. */
3499 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3500 || comp_target_types (type, rhstype, 0))
3502 /* If there is a mismatch, do warn. */
3503 else if (pedantic)
3504 warn_for_assignment ("pointer targets in %s differ in signedness",
3505 errtype, funname, parmnum);
3507 else if (TREE_CODE (ttl) == FUNCTION_TYPE
3508 && TREE_CODE (ttr) == FUNCTION_TYPE)
3510 /* Because const and volatile on functions are restrictions
3511 that say the function will not do certain things,
3512 it is okay to use a const or volatile function
3513 where an ordinary one is wanted, but not vice-versa. */
3514 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3515 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3516 errtype, funname, parmnum);
3519 else
3520 warn_for_assignment ("%s from incompatible pointer type",
3521 errtype, funname, parmnum);
3522 return convert (type, rhs);
3524 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3526 /* An explicit constant 0 can convert to a pointer,
3527 or one that results from arithmetic, even including
3528 a cast to integer type. */
3529 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3531 ! (TREE_CODE (rhs) == NOP_EXPR
3532 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3533 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3534 && integer_zerop (TREE_OPERAND (rhs, 0))))
3536 warn_for_assignment ("%s makes pointer from integer without a cast",
3537 errtype, funname, parmnum);
3538 return convert (type, rhs);
3540 return null_pointer_node;
3542 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3544 warn_for_assignment ("%s makes integer from pointer without a cast",
3545 errtype, funname, parmnum);
3546 return convert (type, rhs);
3548 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3549 return convert (type, rhs);
3551 if (!errtype)
3553 if (funname)
3555 tree selector = objc_message_selector ();
3557 if (selector && parmnum > 2)
3558 error ("incompatible type for argument %d of `%s'",
3559 parmnum - 2, IDENTIFIER_POINTER (selector));
3560 else
3561 error ("incompatible type for argument %d of `%s'",
3562 parmnum, IDENTIFIER_POINTER (funname));
3564 else
3565 error ("incompatible type for argument %d of indirect function call",
3566 parmnum);
3568 else
3569 error ("incompatible types in %s", errtype);
3571 return error_mark_node;
3574 /* Convert VALUE for assignment into inlined parameter PARM. */
3576 tree
3577 c_convert_parm_for_inlining (tree parm, tree value, tree fn)
3579 tree ret, type;
3581 /* If FN was prototyped, the value has been converted already
3582 in convert_arguments. */
3583 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3584 return value;
3586 type = TREE_TYPE (parm);
3587 ret = convert_for_assignment (type, value,
3588 (char *) 0 /* arg passing */, fn,
3589 DECL_NAME (fn), 0);
3590 if (PROMOTE_PROTOTYPES
3591 && INTEGRAL_TYPE_P (type)
3592 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3593 ret = default_conversion (ret);
3594 return ret;
3597 /* Print a warning using MSGID.
3598 It gets OPNAME as its one parameter.
3599 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3600 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3601 FUNCTION and ARGNUM are handled specially if we are building an
3602 Objective-C selector. */
3604 static void
3605 warn_for_assignment (const char *msgid, const char *opname, tree function,
3606 int argnum)
3608 if (opname == 0)
3610 tree selector = objc_message_selector ();
3611 char * new_opname;
3613 if (selector && argnum > 2)
3615 function = selector;
3616 argnum -= 2;
3618 if (argnum == 0)
3620 if (function)
3622 /* Function name is known; supply it. */
3623 const char *const argstring = _("passing arg of `%s'");
3624 new_opname = alloca (IDENTIFIER_LENGTH (function)
3625 + strlen (argstring) + 1 + 1);
3626 sprintf (new_opname, argstring,
3627 IDENTIFIER_POINTER (function));
3629 else
3631 /* Function name unknown (call through ptr). */
3632 const char *const argnofun = _("passing arg of pointer to function");
3633 new_opname = alloca (strlen (argnofun) + 1 + 1);
3634 sprintf (new_opname, argnofun);
3637 else if (function)
3639 /* Function name is known; supply it. */
3640 const char *const argstring = _("passing arg %d of `%s'");
3641 new_opname = alloca (IDENTIFIER_LENGTH (function)
3642 + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3643 sprintf (new_opname, argstring, argnum,
3644 IDENTIFIER_POINTER (function));
3646 else
3648 /* Function name unknown (call through ptr); just give arg number. */
3649 const char *const argnofun = _("passing arg %d of pointer to function");
3650 new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3651 sprintf (new_opname, argnofun, argnum);
3653 opname = new_opname;
3655 pedwarn (msgid, opname);
3658 /* If VALUE is a compound expr all of whose expressions are constant, then
3659 return its value. Otherwise, return error_mark_node.
3661 This is for handling COMPOUND_EXPRs as initializer elements
3662 which is allowed with a warning when -pedantic is specified. */
3664 static tree
3665 valid_compound_expr_initializer (tree value, tree endtype)
3667 if (TREE_CODE (value) == COMPOUND_EXPR)
3669 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3670 == error_mark_node)
3671 return error_mark_node;
3672 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3673 endtype);
3675 else if (! TREE_CONSTANT (value)
3676 && ! initializer_constant_valid_p (value, endtype))
3677 return error_mark_node;
3678 else
3679 return value;
3682 /* Perform appropriate conversions on the initial value of a variable,
3683 store it in the declaration DECL,
3684 and print any error messages that are appropriate.
3685 If the init is invalid, store an ERROR_MARK. */
3687 void
3688 store_init_value (tree decl, tree init)
3690 tree value, type;
3692 /* If variable's type was invalidly declared, just ignore it. */
3694 type = TREE_TYPE (decl);
3695 if (TREE_CODE (type) == ERROR_MARK)
3696 return;
3698 /* Digest the specified initializer into an expression. */
3700 value = digest_init (type, init, TREE_STATIC (decl));
3702 /* Store the expression if valid; else report error. */
3704 if (warn_traditional && !in_system_header
3705 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3706 warning ("traditional C rejects automatic aggregate initialization");
3708 DECL_INITIAL (decl) = value;
3710 /* ANSI wants warnings about out-of-range constant initializers. */
3711 STRIP_TYPE_NOPS (value);
3712 constant_expression_warning (value);
3714 /* Check if we need to set array size from compound literal size. */
3715 if (TREE_CODE (type) == ARRAY_TYPE
3716 && TYPE_DOMAIN (type) == 0
3717 && value != error_mark_node)
3719 tree inside_init = init;
3721 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3722 inside_init = TREE_OPERAND (init, 0);
3723 inside_init = fold (inside_init);
3725 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3727 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3729 if (TYPE_DOMAIN (TREE_TYPE (decl)))
3731 /* For int foo[] = (int [3]){1}; we need to set array size
3732 now since later on array initializer will be just the
3733 brace enclosed list of the compound literal. */
3734 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3735 layout_type (type);
3736 layout_decl (decl, 0);
3742 /* Methods for storing and printing names for error messages. */
3744 /* Implement a spelling stack that allows components of a name to be pushed
3745 and popped. Each element on the stack is this structure. */
3747 struct spelling
3749 int kind;
3750 union
3752 int i;
3753 const char *s;
3754 } u;
3757 #define SPELLING_STRING 1
3758 #define SPELLING_MEMBER 2
3759 #define SPELLING_BOUNDS 3
3761 static struct spelling *spelling; /* Next stack element (unused). */
3762 static struct spelling *spelling_base; /* Spelling stack base. */
3763 static int spelling_size; /* Size of the spelling stack. */
3765 /* Macros to save and restore the spelling stack around push_... functions.
3766 Alternative to SAVE_SPELLING_STACK. */
3768 #define SPELLING_DEPTH() (spelling - spelling_base)
3769 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3771 /* Push an element on the spelling stack with type KIND and assign VALUE
3772 to MEMBER. */
3774 #define PUSH_SPELLING(KIND, VALUE, MEMBER) \
3776 int depth = SPELLING_DEPTH (); \
3778 if (depth >= spelling_size) \
3780 spelling_size += 10; \
3781 if (spelling_base == 0) \
3782 spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3783 else \
3784 spelling_base = xrealloc (spelling_base, \
3785 spelling_size * sizeof (struct spelling)); \
3786 RESTORE_SPELLING_DEPTH (depth); \
3789 spelling->kind = (KIND); \
3790 spelling->MEMBER = (VALUE); \
3791 spelling++; \
3794 /* Push STRING on the stack. Printed literally. */
3796 static void
3797 push_string (const char *string)
3799 PUSH_SPELLING (SPELLING_STRING, string, u.s);
3802 /* Push a member name on the stack. Printed as '.' STRING. */
3804 static void
3805 push_member_name (tree decl)
3807 const char *const string
3808 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3809 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3812 /* Push an array bounds on the stack. Printed as [BOUNDS]. */
3814 static void
3815 push_array_bounds (int bounds)
3817 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3820 /* Compute the maximum size in bytes of the printed spelling. */
3822 static int
3823 spelling_length (void)
3825 int size = 0;
3826 struct spelling *p;
3828 for (p = spelling_base; p < spelling; p++)
3830 if (p->kind == SPELLING_BOUNDS)
3831 size += 25;
3832 else
3833 size += strlen (p->u.s) + 1;
3836 return size;
3839 /* Print the spelling to BUFFER and return it. */
3841 static char *
3842 print_spelling (char *buffer)
3844 char *d = buffer;
3845 struct spelling *p;
3847 for (p = spelling_base; p < spelling; p++)
3848 if (p->kind == SPELLING_BOUNDS)
3850 sprintf (d, "[%d]", p->u.i);
3851 d += strlen (d);
3853 else
3855 const char *s;
3856 if (p->kind == SPELLING_MEMBER)
3857 *d++ = '.';
3858 for (s = p->u.s; (*d = *s++); d++)
3861 *d++ = '\0';
3862 return buffer;
3865 /* Issue an error message for a bad initializer component.
3866 MSGID identifies the message.
3867 The component name is taken from the spelling stack. */
3869 void
3870 error_init (const char *msgid)
3872 char *ofwhat;
3874 error ("%s", _(msgid));
3875 ofwhat = print_spelling (alloca (spelling_length () + 1));
3876 if (*ofwhat)
3877 error ("(near initialization for `%s')", ofwhat);
3880 /* Issue a pedantic warning for a bad initializer component.
3881 MSGID identifies the message.
3882 The component name is taken from the spelling stack. */
3884 void
3885 pedwarn_init (const char *msgid)
3887 char *ofwhat;
3889 pedwarn ("%s", _(msgid));
3890 ofwhat = print_spelling (alloca (spelling_length () + 1));
3891 if (*ofwhat)
3892 pedwarn ("(near initialization for `%s')", ofwhat);
3895 /* Issue a warning for a bad initializer component.
3896 MSGID identifies the message.
3897 The component name is taken from the spelling stack. */
3899 static void
3900 warning_init (const char *msgid)
3902 char *ofwhat;
3904 warning ("%s", _(msgid));
3905 ofwhat = print_spelling (alloca (spelling_length () + 1));
3906 if (*ofwhat)
3907 warning ("(near initialization for `%s')", ofwhat);
3910 /* Digest the parser output INIT as an initializer for type TYPE.
3911 Return a C expression of type TYPE to represent the initial value.
3913 REQUIRE_CONSTANT requests an error if non-constant initializers or
3914 elements are seen. */
3916 static tree
3917 digest_init (tree type, tree init, int require_constant)
3919 enum tree_code code = TREE_CODE (type);
3920 tree inside_init = init;
3922 if (type == error_mark_node
3923 || init == error_mark_node
3924 || TREE_TYPE (init) == error_mark_node)
3925 return error_mark_node;
3927 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3928 /* Do not use STRIP_NOPS here. We do not want an enumerator
3929 whose value is 0 to count as a null pointer constant. */
3930 if (TREE_CODE (init) == NON_LVALUE_EXPR)
3931 inside_init = TREE_OPERAND (init, 0);
3933 inside_init = fold (inside_init);
3935 /* Initialization of an array of chars from a string constant
3936 optionally enclosed in braces. */
3938 if (code == ARRAY_TYPE)
3940 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3941 if ((typ1 == char_type_node
3942 || typ1 == signed_char_type_node
3943 || typ1 == unsigned_char_type_node
3944 || typ1 == unsigned_wchar_type_node
3945 || typ1 == signed_wchar_type_node)
3946 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3948 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3949 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
3950 return inside_init;
3952 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3953 != char_type_node)
3954 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3956 error_init ("char-array initialized from wide string");
3957 return error_mark_node;
3959 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3960 == char_type_node)
3961 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3963 error_init ("int-array initialized from non-wide string");
3964 return error_mark_node;
3967 TREE_TYPE (inside_init) = type;
3968 if (TYPE_DOMAIN (type) != 0
3969 && TYPE_SIZE (type) != 0
3970 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3971 /* Subtract 1 (or sizeof (wchar_t))
3972 because it's ok to ignore the terminating null char
3973 that is counted in the length of the constant. */
3974 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
3975 TREE_STRING_LENGTH (inside_init)
3976 - ((TYPE_PRECISION (typ1)
3977 != TYPE_PRECISION (char_type_node))
3978 ? (TYPE_PRECISION (wchar_type_node)
3979 / BITS_PER_UNIT)
3980 : 1)))
3981 pedwarn_init ("initializer-string for array of chars is too long");
3983 return inside_init;
3987 /* Build a VECTOR_CST from a *constant* vector constructor. If the
3988 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
3989 below and handle as a constructor. */
3990 if (code == VECTOR_TYPE
3991 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT)
3992 && TREE_CONSTANT (inside_init))
3994 if (TREE_CODE (inside_init) == VECTOR_CST
3995 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3996 TYPE_MAIN_VARIANT (type),
3997 COMPARE_STRICT))
3998 return inside_init;
3999 else
4000 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4003 /* Any type can be initialized
4004 from an expression of the same type, optionally with braces. */
4006 if (inside_init && TREE_TYPE (inside_init) != 0
4007 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
4008 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
4009 || (code == ARRAY_TYPE
4010 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4011 || (code == VECTOR_TYPE
4012 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
4013 || (code == POINTER_TYPE
4014 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4015 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4016 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
4017 TREE_TYPE (type), COMPARE_STRICT))))
4019 if (code == POINTER_TYPE)
4020 inside_init = default_function_array_conversion (inside_init);
4022 if (code == VECTOR_TYPE)
4023 /* Although the types are compatible, we may require a
4024 conversion. */
4025 inside_init = convert (type, inside_init);
4027 if (require_constant && !flag_isoc99
4028 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4030 /* As an extension, allow initializing objects with static storage
4031 duration with compound literals (which are then treated just as
4032 the brace enclosed list they contain). */
4033 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4034 inside_init = DECL_INITIAL (decl);
4037 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4038 && TREE_CODE (inside_init) != CONSTRUCTOR)
4040 error_init ("array initialized from non-constant array expression");
4041 return error_mark_node;
4044 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
4045 inside_init = decl_constant_value_for_broken_optimization (inside_init);
4047 /* Compound expressions can only occur here if -pedantic or
4048 -pedantic-errors is specified. In the later case, we always want
4049 an error. In the former case, we simply want a warning. */
4050 if (require_constant && pedantic
4051 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4053 inside_init
4054 = valid_compound_expr_initializer (inside_init,
4055 TREE_TYPE (inside_init));
4056 if (inside_init == error_mark_node)
4057 error_init ("initializer element is not constant");
4058 else
4059 pedwarn_init ("initializer element is not constant");
4060 if (flag_pedantic_errors)
4061 inside_init = error_mark_node;
4063 else if (require_constant
4064 && (!TREE_CONSTANT (inside_init)
4065 /* This test catches things like `7 / 0' which
4066 result in an expression for which TREE_CONSTANT
4067 is true, but which is not actually something
4068 that is a legal constant. We really should not
4069 be using this function, because it is a part of
4070 the back-end. Instead, the expression should
4071 already have been turned into ERROR_MARK_NODE. */
4072 || !initializer_constant_valid_p (inside_init,
4073 TREE_TYPE (inside_init))))
4075 error_init ("initializer element is not constant");
4076 inside_init = error_mark_node;
4079 return inside_init;
4082 /* Handle scalar types, including conversions. */
4084 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4085 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4087 /* Note that convert_for_assignment calls default_conversion
4088 for arrays and functions. We must not call it in the
4089 case where inside_init is a null pointer constant. */
4090 inside_init
4091 = convert_for_assignment (type, init, _("initialization"),
4092 NULL_TREE, NULL_TREE, 0);
4094 if (require_constant && ! TREE_CONSTANT (inside_init))
4096 error_init ("initializer element is not constant");
4097 inside_init = error_mark_node;
4099 else if (require_constant
4100 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4102 error_init ("initializer element is not computable at load time");
4103 inside_init = error_mark_node;
4106 return inside_init;
4109 /* Come here only for records and arrays. */
4111 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4113 error_init ("variable-sized object may not be initialized");
4114 return error_mark_node;
4117 error_init ("invalid initializer");
4118 return error_mark_node;
4121 /* Handle initializers that use braces. */
4123 /* Type of object we are accumulating a constructor for.
4124 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4125 static tree constructor_type;
4127 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4128 left to fill. */
4129 static tree constructor_fields;
4131 /* For an ARRAY_TYPE, this is the specified index
4132 at which to store the next element we get. */
4133 static tree constructor_index;
4135 /* For an ARRAY_TYPE, this is the maximum index. */
4136 static tree constructor_max_index;
4138 /* For a RECORD_TYPE, this is the first field not yet written out. */
4139 static tree constructor_unfilled_fields;
4141 /* For an ARRAY_TYPE, this is the index of the first element
4142 not yet written out. */
4143 static tree constructor_unfilled_index;
4145 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4146 This is so we can generate gaps between fields, when appropriate. */
4147 static tree constructor_bit_index;
4149 /* If we are saving up the elements rather than allocating them,
4150 this is the list of elements so far (in reverse order,
4151 most recent first). */
4152 static tree constructor_elements;
4154 /* 1 if constructor should be incrementally stored into a constructor chain,
4155 0 if all the elements should be kept in AVL tree. */
4156 static int constructor_incremental;
4158 /* 1 if so far this constructor's elements are all compile-time constants. */
4159 static int constructor_constant;
4161 /* 1 if so far this constructor's elements are all valid address constants. */
4162 static int constructor_simple;
4164 /* 1 if this constructor is erroneous so far. */
4165 static int constructor_erroneous;
4167 /* Structure for managing pending initializer elements, organized as an
4168 AVL tree. */
4170 struct init_node
4172 struct init_node *left, *right;
4173 struct init_node *parent;
4174 int balance;
4175 tree purpose;
4176 tree value;
4179 /* Tree of pending elements at this constructor level.
4180 These are elements encountered out of order
4181 which belong at places we haven't reached yet in actually
4182 writing the output.
4183 Will never hold tree nodes across GC runs. */
4184 static struct init_node *constructor_pending_elts;
4186 /* The SPELLING_DEPTH of this constructor. */
4187 static int constructor_depth;
4189 /* 0 if implicitly pushing constructor levels is allowed. */
4190 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
4192 static int require_constant_value;
4193 static int require_constant_elements;
4195 /* DECL node for which an initializer is being read.
4196 0 means we are reading a constructor expression
4197 such as (struct foo) {...}. */
4198 static tree constructor_decl;
4200 /* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
4201 static const char *constructor_asmspec;
4203 /* Nonzero if this is an initializer for a top-level decl. */
4204 static int constructor_top_level;
4206 /* Nonzero if there were any member designators in this initializer. */
4207 static int constructor_designated;
4209 /* Nesting depth of designator list. */
4210 static int designator_depth;
4212 /* Nonzero if there were diagnosed errors in this designator list. */
4213 static int designator_errorneous;
4216 /* This stack has a level for each implicit or explicit level of
4217 structuring in the initializer, including the outermost one. It
4218 saves the values of most of the variables above. */
4220 struct constructor_range_stack;
4222 struct constructor_stack
4224 struct constructor_stack *next;
4225 tree type;
4226 tree fields;
4227 tree index;
4228 tree max_index;
4229 tree unfilled_index;
4230 tree unfilled_fields;
4231 tree bit_index;
4232 tree elements;
4233 struct init_node *pending_elts;
4234 int offset;
4235 int depth;
4236 /* If nonzero, this value should replace the entire
4237 constructor at this level. */
4238 tree replacement_value;
4239 struct constructor_range_stack *range_stack;
4240 char constant;
4241 char simple;
4242 char implicit;
4243 char erroneous;
4244 char outer;
4245 char incremental;
4246 char designated;
4249 struct constructor_stack *constructor_stack;
4251 /* This stack represents designators from some range designator up to
4252 the last designator in the list. */
4254 struct constructor_range_stack
4256 struct constructor_range_stack *next, *prev;
4257 struct constructor_stack *stack;
4258 tree range_start;
4259 tree index;
4260 tree range_end;
4261 tree fields;
4264 struct constructor_range_stack *constructor_range_stack;
4266 /* This stack records separate initializers that are nested.
4267 Nested initializers can't happen in ANSI C, but GNU C allows them
4268 in cases like { ... (struct foo) { ... } ... }. */
4270 struct initializer_stack
4272 struct initializer_stack *next;
4273 tree decl;
4274 const char *asmspec;
4275 struct constructor_stack *constructor_stack;
4276 struct constructor_range_stack *constructor_range_stack;
4277 tree elements;
4278 struct spelling *spelling;
4279 struct spelling *spelling_base;
4280 int spelling_size;
4281 char top_level;
4282 char require_constant_value;
4283 char require_constant_elements;
4286 struct initializer_stack *initializer_stack;
4288 /* Prepare to parse and output the initializer for variable DECL. */
4290 void
4291 start_init (tree decl, tree asmspec_tree, int top_level)
4293 const char *locus;
4294 struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4295 const char *asmspec = 0;
4297 if (asmspec_tree)
4298 asmspec = TREE_STRING_POINTER (asmspec_tree);
4300 p->decl = constructor_decl;
4301 p->asmspec = constructor_asmspec;
4302 p->require_constant_value = require_constant_value;
4303 p->require_constant_elements = require_constant_elements;
4304 p->constructor_stack = constructor_stack;
4305 p->constructor_range_stack = constructor_range_stack;
4306 p->elements = constructor_elements;
4307 p->spelling = spelling;
4308 p->spelling_base = spelling_base;
4309 p->spelling_size = spelling_size;
4310 p->top_level = constructor_top_level;
4311 p->next = initializer_stack;
4312 initializer_stack = p;
4314 constructor_decl = decl;
4315 constructor_asmspec = asmspec;
4316 constructor_designated = 0;
4317 constructor_top_level = top_level;
4319 if (decl != 0)
4321 require_constant_value = TREE_STATIC (decl);
4322 require_constant_elements
4323 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4324 /* For a scalar, you can always use any value to initialize,
4325 even within braces. */
4326 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4327 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4328 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4329 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4330 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4332 else
4334 require_constant_value = 0;
4335 require_constant_elements = 0;
4336 locus = "(anonymous)";
4339 constructor_stack = 0;
4340 constructor_range_stack = 0;
4342 missing_braces_mentioned = 0;
4344 spelling_base = 0;
4345 spelling_size = 0;
4346 RESTORE_SPELLING_DEPTH (0);
4348 if (locus)
4349 push_string (locus);
4352 void
4353 finish_init (void)
4355 struct initializer_stack *p = initializer_stack;
4357 /* Free the whole constructor stack of this initializer. */
4358 while (constructor_stack)
4360 struct constructor_stack *q = constructor_stack;
4361 constructor_stack = q->next;
4362 free (q);
4365 if (constructor_range_stack)
4366 abort ();
4368 /* Pop back to the data of the outer initializer (if any). */
4369 constructor_decl = p->decl;
4370 constructor_asmspec = p->asmspec;
4371 require_constant_value = p->require_constant_value;
4372 require_constant_elements = p->require_constant_elements;
4373 constructor_stack = p->constructor_stack;
4374 constructor_range_stack = p->constructor_range_stack;
4375 constructor_elements = p->elements;
4376 spelling = p->spelling;
4377 spelling_base = p->spelling_base;
4378 spelling_size = p->spelling_size;
4379 constructor_top_level = p->top_level;
4380 initializer_stack = p->next;
4381 free (p);
4384 /* Call here when we see the initializer is surrounded by braces.
4385 This is instead of a call to push_init_level;
4386 it is matched by a call to pop_init_level.
4388 TYPE is the type to initialize, for a constructor expression.
4389 For an initializer for a decl, TYPE is zero. */
4391 void
4392 really_start_incremental_init (tree type)
4394 struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4396 if (type == 0)
4397 type = TREE_TYPE (constructor_decl);
4399 if ((*targetm.vector_opaque_p) (type))
4400 error ("opaque vector types cannot be initialized");
4402 p->type = constructor_type;
4403 p->fields = constructor_fields;
4404 p->index = constructor_index;
4405 p->max_index = constructor_max_index;
4406 p->unfilled_index = constructor_unfilled_index;
4407 p->unfilled_fields = constructor_unfilled_fields;
4408 p->bit_index = constructor_bit_index;
4409 p->elements = constructor_elements;
4410 p->constant = constructor_constant;
4411 p->simple = constructor_simple;
4412 p->erroneous = constructor_erroneous;
4413 p->pending_elts = constructor_pending_elts;
4414 p->depth = constructor_depth;
4415 p->replacement_value = 0;
4416 p->implicit = 0;
4417 p->range_stack = 0;
4418 p->outer = 0;
4419 p->incremental = constructor_incremental;
4420 p->designated = constructor_designated;
4421 p->next = 0;
4422 constructor_stack = p;
4424 constructor_constant = 1;
4425 constructor_simple = 1;
4426 constructor_depth = SPELLING_DEPTH ();
4427 constructor_elements = 0;
4428 constructor_pending_elts = 0;
4429 constructor_type = type;
4430 constructor_incremental = 1;
4431 constructor_designated = 0;
4432 designator_depth = 0;
4433 designator_errorneous = 0;
4435 if (TREE_CODE (constructor_type) == RECORD_TYPE
4436 || TREE_CODE (constructor_type) == UNION_TYPE)
4438 constructor_fields = TYPE_FIELDS (constructor_type);
4439 /* Skip any nameless bit fields at the beginning. */
4440 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4441 && DECL_NAME (constructor_fields) == 0)
4442 constructor_fields = TREE_CHAIN (constructor_fields);
4444 constructor_unfilled_fields = constructor_fields;
4445 constructor_bit_index = bitsize_zero_node;
4447 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4449 if (TYPE_DOMAIN (constructor_type))
4451 constructor_max_index
4452 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4454 /* Detect non-empty initializations of zero-length arrays. */
4455 if (constructor_max_index == NULL_TREE
4456 && TYPE_SIZE (constructor_type))
4457 constructor_max_index = build_int_2 (-1, -1);
4459 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4460 to initialize VLAs will cause a proper error; avoid tree
4461 checking errors as well by setting a safe value. */
4462 if (constructor_max_index
4463 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4464 constructor_max_index = build_int_2 (-1, -1);
4466 constructor_index
4467 = convert (bitsizetype,
4468 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4470 else
4471 constructor_index = bitsize_zero_node;
4473 constructor_unfilled_index = constructor_index;
4475 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4477 /* Vectors are like simple fixed-size arrays. */
4478 constructor_max_index =
4479 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4480 constructor_index = convert (bitsizetype, bitsize_zero_node);
4481 constructor_unfilled_index = constructor_index;
4483 else
4485 /* Handle the case of int x = {5}; */
4486 constructor_fields = constructor_type;
4487 constructor_unfilled_fields = constructor_type;
4491 /* Push down into a subobject, for initialization.
4492 If this is for an explicit set of braces, IMPLICIT is 0.
4493 If it is because the next element belongs at a lower level,
4494 IMPLICIT is 1 (or 2 if the push is because of designator list). */
4496 void
4497 push_init_level (int implicit)
4499 struct constructor_stack *p;
4500 tree value = NULL_TREE;
4502 /* If we've exhausted any levels that didn't have braces,
4503 pop them now. */
4504 while (constructor_stack->implicit)
4506 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4507 || TREE_CODE (constructor_type) == UNION_TYPE)
4508 && constructor_fields == 0)
4509 process_init_element (pop_init_level (1));
4510 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4511 && constructor_max_index
4512 && tree_int_cst_lt (constructor_max_index, constructor_index))
4513 process_init_element (pop_init_level (1));
4514 else
4515 break;
4518 /* Unless this is an explicit brace, we need to preserve previous
4519 content if any. */
4520 if (implicit)
4522 if ((TREE_CODE (constructor_type) == RECORD_TYPE
4523 || TREE_CODE (constructor_type) == UNION_TYPE)
4524 && constructor_fields)
4525 value = find_init_member (constructor_fields);
4526 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4527 value = find_init_member (constructor_index);
4530 p = xmalloc (sizeof (struct constructor_stack));
4531 p->type = constructor_type;
4532 p->fields = constructor_fields;
4533 p->index = constructor_index;
4534 p->max_index = constructor_max_index;
4535 p->unfilled_index = constructor_unfilled_index;
4536 p->unfilled_fields = constructor_unfilled_fields;
4537 p->bit_index = constructor_bit_index;
4538 p->elements = constructor_elements;
4539 p->constant = constructor_constant;
4540 p->simple = constructor_simple;
4541 p->erroneous = constructor_erroneous;
4542 p->pending_elts = constructor_pending_elts;
4543 p->depth = constructor_depth;
4544 p->replacement_value = 0;
4545 p->implicit = implicit;
4546 p->outer = 0;
4547 p->incremental = constructor_incremental;
4548 p->designated = constructor_designated;
4549 p->next = constructor_stack;
4550 p->range_stack = 0;
4551 constructor_stack = p;
4553 constructor_constant = 1;
4554 constructor_simple = 1;
4555 constructor_depth = SPELLING_DEPTH ();
4556 constructor_elements = 0;
4557 constructor_incremental = 1;
4558 constructor_designated = 0;
4559 constructor_pending_elts = 0;
4560 if (!implicit)
4562 p->range_stack = constructor_range_stack;
4563 constructor_range_stack = 0;
4564 designator_depth = 0;
4565 designator_errorneous = 0;
4568 /* Don't die if an entire brace-pair level is superfluous
4569 in the containing level. */
4570 if (constructor_type == 0)
4572 else if (TREE_CODE (constructor_type) == RECORD_TYPE
4573 || TREE_CODE (constructor_type) == UNION_TYPE)
4575 /* Don't die if there are extra init elts at the end. */
4576 if (constructor_fields == 0)
4577 constructor_type = 0;
4578 else
4580 constructor_type = TREE_TYPE (constructor_fields);
4581 push_member_name (constructor_fields);
4582 constructor_depth++;
4585 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4587 constructor_type = TREE_TYPE (constructor_type);
4588 push_array_bounds (tree_low_cst (constructor_index, 0));
4589 constructor_depth++;
4592 if (constructor_type == 0)
4594 error_init ("extra brace group at end of initializer");
4595 constructor_fields = 0;
4596 constructor_unfilled_fields = 0;
4597 return;
4600 if (value && TREE_CODE (value) == CONSTRUCTOR)
4602 constructor_constant = TREE_CONSTANT (value);
4603 constructor_simple = TREE_STATIC (value);
4604 constructor_elements = CONSTRUCTOR_ELTS (value);
4605 if (constructor_elements
4606 && (TREE_CODE (constructor_type) == RECORD_TYPE
4607 || TREE_CODE (constructor_type) == ARRAY_TYPE))
4608 set_nonincremental_init ();
4611 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4613 missing_braces_mentioned = 1;
4614 warning_init ("missing braces around initializer");
4617 if (TREE_CODE (constructor_type) == RECORD_TYPE
4618 || TREE_CODE (constructor_type) == UNION_TYPE)
4620 constructor_fields = TYPE_FIELDS (constructor_type);
4621 /* Skip any nameless bit fields at the beginning. */
4622 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4623 && DECL_NAME (constructor_fields) == 0)
4624 constructor_fields = TREE_CHAIN (constructor_fields);
4626 constructor_unfilled_fields = constructor_fields;
4627 constructor_bit_index = bitsize_zero_node;
4629 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4631 /* Vectors are like simple fixed-size arrays. */
4632 constructor_max_index =
4633 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4634 constructor_index = convert (bitsizetype, integer_zero_node);
4635 constructor_unfilled_index = constructor_index;
4637 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4639 if (TYPE_DOMAIN (constructor_type))
4641 constructor_max_index
4642 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4644 /* Detect non-empty initializations of zero-length arrays. */
4645 if (constructor_max_index == NULL_TREE
4646 && TYPE_SIZE (constructor_type))
4647 constructor_max_index = build_int_2 (-1, -1);
4649 /* constructor_max_index needs to be an INTEGER_CST. Attempts
4650 to initialize VLAs will cause a proper error; avoid tree
4651 checking errors as well by setting a safe value. */
4652 if (constructor_max_index
4653 && TREE_CODE (constructor_max_index) != INTEGER_CST)
4654 constructor_max_index = build_int_2 (-1, -1);
4656 constructor_index
4657 = convert (bitsizetype,
4658 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4660 else
4661 constructor_index = bitsize_zero_node;
4663 constructor_unfilled_index = constructor_index;
4664 if (value && TREE_CODE (value) == STRING_CST)
4666 /* We need to split the char/wchar array into individual
4667 characters, so that we don't have to special case it
4668 everywhere. */
4669 set_nonincremental_init_from_string (value);
4672 else
4674 warning_init ("braces around scalar initializer");
4675 constructor_fields = constructor_type;
4676 constructor_unfilled_fields = constructor_type;
4680 /* At the end of an implicit or explicit brace level,
4681 finish up that level of constructor.
4682 If we were outputting the elements as they are read, return 0
4683 from inner levels (process_init_element ignores that),
4684 but return error_mark_node from the outermost level
4685 (that's what we want to put in DECL_INITIAL).
4686 Otherwise, return a CONSTRUCTOR expression. */
4688 tree
4689 pop_init_level (int implicit)
4691 struct constructor_stack *p;
4692 tree constructor = 0;
4694 if (implicit == 0)
4696 /* When we come to an explicit close brace,
4697 pop any inner levels that didn't have explicit braces. */
4698 while (constructor_stack->implicit)
4699 process_init_element (pop_init_level (1));
4701 if (constructor_range_stack)
4702 abort ();
4705 p = constructor_stack;
4707 /* Error for initializing a flexible array member, or a zero-length
4708 array member in an inappropriate context. */
4709 if (constructor_type && constructor_fields
4710 && TREE_CODE (constructor_type) == ARRAY_TYPE
4711 && TYPE_DOMAIN (constructor_type)
4712 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4714 /* Silently discard empty initializations. The parser will
4715 already have pedwarned for empty brackets. */
4716 if (integer_zerop (constructor_unfilled_index))
4717 constructor_type = NULL_TREE;
4718 else if (! TYPE_SIZE (constructor_type))
4720 if (constructor_depth > 2)
4721 error_init ("initialization of flexible array member in a nested context");
4722 else if (pedantic)
4723 pedwarn_init ("initialization of a flexible array member");
4725 /* We have already issued an error message for the existence
4726 of a flexible array member not at the end of the structure.
4727 Discard the initializer so that we do not abort later. */
4728 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4729 constructor_type = NULL_TREE;
4731 else
4732 /* Zero-length arrays are no longer special, so we should no longer
4733 get here. */
4734 abort ();
4737 /* Warn when some struct elements are implicitly initialized to zero. */
4738 if (extra_warnings
4739 && constructor_type
4740 && TREE_CODE (constructor_type) == RECORD_TYPE
4741 && constructor_unfilled_fields)
4743 /* Do not warn for flexible array members or zero-length arrays. */
4744 while (constructor_unfilled_fields
4745 && (! DECL_SIZE (constructor_unfilled_fields)
4746 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4747 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4749 /* Do not warn if this level of the initializer uses member
4750 designators; it is likely to be deliberate. */
4751 if (constructor_unfilled_fields && !constructor_designated)
4753 push_member_name (constructor_unfilled_fields);
4754 warning_init ("missing initializer");
4755 RESTORE_SPELLING_DEPTH (constructor_depth);
4759 /* Now output all pending elements. */
4760 constructor_incremental = 1;
4761 output_pending_init_elements (1);
4763 /* Pad out the end of the structure. */
4764 if (p->replacement_value)
4765 /* If this closes a superfluous brace pair,
4766 just pass out the element between them. */
4767 constructor = p->replacement_value;
4768 else if (constructor_type == 0)
4770 else if (TREE_CODE (constructor_type) != RECORD_TYPE
4771 && TREE_CODE (constructor_type) != UNION_TYPE
4772 && TREE_CODE (constructor_type) != ARRAY_TYPE
4773 && TREE_CODE (constructor_type) != VECTOR_TYPE)
4775 /* A nonincremental scalar initializer--just return
4776 the element, after verifying there is just one. */
4777 if (constructor_elements == 0)
4779 if (!constructor_erroneous)
4780 error_init ("empty scalar initializer");
4781 constructor = error_mark_node;
4783 else if (TREE_CHAIN (constructor_elements) != 0)
4785 error_init ("extra elements in scalar initializer");
4786 constructor = TREE_VALUE (constructor_elements);
4788 else
4789 constructor = TREE_VALUE (constructor_elements);
4791 else
4793 if (constructor_erroneous)
4794 constructor = error_mark_node;
4795 else
4797 constructor = build_constructor (constructor_type,
4798 nreverse (constructor_elements));
4799 if (constructor_constant)
4800 TREE_CONSTANT (constructor) = 1;
4801 if (constructor_constant && constructor_simple)
4802 TREE_STATIC (constructor) = 1;
4806 constructor_type = p->type;
4807 constructor_fields = p->fields;
4808 constructor_index = p->index;
4809 constructor_max_index = p->max_index;
4810 constructor_unfilled_index = p->unfilled_index;
4811 constructor_unfilled_fields = p->unfilled_fields;
4812 constructor_bit_index = p->bit_index;
4813 constructor_elements = p->elements;
4814 constructor_constant = p->constant;
4815 constructor_simple = p->simple;
4816 constructor_erroneous = p->erroneous;
4817 constructor_incremental = p->incremental;
4818 constructor_designated = p->designated;
4819 constructor_pending_elts = p->pending_elts;
4820 constructor_depth = p->depth;
4821 if (!p->implicit)
4822 constructor_range_stack = p->range_stack;
4823 RESTORE_SPELLING_DEPTH (constructor_depth);
4825 constructor_stack = p->next;
4826 free (p);
4828 if (constructor == 0)
4830 if (constructor_stack == 0)
4831 return error_mark_node;
4832 return NULL_TREE;
4834 return constructor;
4837 /* Common handling for both array range and field name designators.
4838 ARRAY argument is nonzero for array ranges. Returns zero for success. */
4840 static int
4841 set_designator (int array)
4843 tree subtype;
4844 enum tree_code subcode;
4846 /* Don't die if an entire brace-pair level is superfluous
4847 in the containing level. */
4848 if (constructor_type == 0)
4849 return 1;
4851 /* If there were errors in this designator list already, bail out silently. */
4852 if (designator_errorneous)
4853 return 1;
4855 if (!designator_depth)
4857 if (constructor_range_stack)
4858 abort ();
4860 /* Designator list starts at the level of closest explicit
4861 braces. */
4862 while (constructor_stack->implicit)
4863 process_init_element (pop_init_level (1));
4864 constructor_designated = 1;
4865 return 0;
4868 if (constructor_no_implicit)
4870 error_init ("initialization designators may not nest");
4871 return 1;
4874 if (TREE_CODE (constructor_type) == RECORD_TYPE
4875 || TREE_CODE (constructor_type) == UNION_TYPE)
4877 subtype = TREE_TYPE (constructor_fields);
4878 if (subtype != error_mark_node)
4879 subtype = TYPE_MAIN_VARIANT (subtype);
4881 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4883 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4885 else
4886 abort ();
4888 subcode = TREE_CODE (subtype);
4889 if (array && subcode != ARRAY_TYPE)
4891 error_init ("array index in non-array initializer");
4892 return 1;
4894 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4896 error_init ("field name not in record or union initializer");
4897 return 1;
4900 constructor_designated = 1;
4901 push_init_level (2);
4902 return 0;
4905 /* If there are range designators in designator list, push a new designator
4906 to constructor_range_stack. RANGE_END is end of such stack range or
4907 NULL_TREE if there is no range designator at this level. */
4909 static void
4910 push_range_stack (tree range_end)
4912 struct constructor_range_stack *p;
4914 p = ggc_alloc (sizeof (struct constructor_range_stack));
4915 p->prev = constructor_range_stack;
4916 p->next = 0;
4917 p->fields = constructor_fields;
4918 p->range_start = constructor_index;
4919 p->index = constructor_index;
4920 p->stack = constructor_stack;
4921 p->range_end = range_end;
4922 if (constructor_range_stack)
4923 constructor_range_stack->next = p;
4924 constructor_range_stack = p;
4927 /* Within an array initializer, specify the next index to be initialized.
4928 FIRST is that index. If LAST is nonzero, then initialize a range
4929 of indices, running from FIRST through LAST. */
4931 void
4932 set_init_index (tree first, tree last)
4934 if (set_designator (1))
4935 return;
4937 designator_errorneous = 1;
4939 while ((TREE_CODE (first) == NOP_EXPR
4940 || TREE_CODE (first) == CONVERT_EXPR
4941 || TREE_CODE (first) == NON_LVALUE_EXPR)
4942 && (TYPE_MODE (TREE_TYPE (first))
4943 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4944 first = TREE_OPERAND (first, 0);
4946 if (last)
4947 while ((TREE_CODE (last) == NOP_EXPR
4948 || TREE_CODE (last) == CONVERT_EXPR
4949 || TREE_CODE (last) == NON_LVALUE_EXPR)
4950 && (TYPE_MODE (TREE_TYPE (last))
4951 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4952 last = TREE_OPERAND (last, 0);
4954 if (TREE_CODE (first) != INTEGER_CST)
4955 error_init ("nonconstant array index in initializer");
4956 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
4957 error_init ("nonconstant array index in initializer");
4958 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
4959 error_init ("array index in non-array initializer");
4960 else if (constructor_max_index
4961 && tree_int_cst_lt (constructor_max_index, first))
4962 error_init ("array index in initializer exceeds array bounds");
4963 else
4965 constructor_index = convert (bitsizetype, first);
4967 if (last)
4969 if (tree_int_cst_equal (first, last))
4970 last = 0;
4971 else if (tree_int_cst_lt (last, first))
4973 error_init ("empty index range in initializer");
4974 last = 0;
4976 else
4978 last = convert (bitsizetype, last);
4979 if (constructor_max_index != 0
4980 && tree_int_cst_lt (constructor_max_index, last))
4982 error_init ("array index range in initializer exceeds array bounds");
4983 last = 0;
4988 designator_depth++;
4989 designator_errorneous = 0;
4990 if (constructor_range_stack || last)
4991 push_range_stack (last);
4995 /* Within a struct initializer, specify the next field to be initialized. */
4997 void
4998 set_init_label (tree fieldname)
5000 tree tail;
5002 if (set_designator (0))
5003 return;
5005 designator_errorneous = 1;
5007 if (TREE_CODE (constructor_type) != RECORD_TYPE
5008 && TREE_CODE (constructor_type) != UNION_TYPE)
5010 error_init ("field name not in record or union initializer");
5011 return;
5014 for (tail = TYPE_FIELDS (constructor_type); tail;
5015 tail = TREE_CHAIN (tail))
5017 if (DECL_NAME (tail) == fieldname)
5018 break;
5021 if (tail == 0)
5022 error ("unknown field `%s' specified in initializer",
5023 IDENTIFIER_POINTER (fieldname));
5024 else
5026 constructor_fields = tail;
5027 designator_depth++;
5028 designator_errorneous = 0;
5029 if (constructor_range_stack)
5030 push_range_stack (NULL_TREE);
5034 /* Add a new initializer to the tree of pending initializers. PURPOSE
5035 identifies the initializer, either array index or field in a structure.
5036 VALUE is the value of that index or field. */
5038 static void
5039 add_pending_init (tree purpose, tree value)
5041 struct init_node *p, **q, *r;
5043 q = &constructor_pending_elts;
5044 p = 0;
5046 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5048 while (*q != 0)
5050 p = *q;
5051 if (tree_int_cst_lt (purpose, p->purpose))
5052 q = &p->left;
5053 else if (tree_int_cst_lt (p->purpose, purpose))
5054 q = &p->right;
5055 else
5057 if (TREE_SIDE_EFFECTS (p->value))
5058 warning_init ("initialized field with side-effects overwritten");
5059 p->value = value;
5060 return;
5064 else
5066 tree bitpos;
5068 bitpos = bit_position (purpose);
5069 while (*q != NULL)
5071 p = *q;
5072 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5073 q = &p->left;
5074 else if (p->purpose != purpose)
5075 q = &p->right;
5076 else
5078 if (TREE_SIDE_EFFECTS (p->value))
5079 warning_init ("initialized field with side-effects overwritten");
5080 p->value = value;
5081 return;
5086 r = ggc_alloc (sizeof (struct init_node));
5087 r->purpose = purpose;
5088 r->value = value;
5090 *q = r;
5091 r->parent = p;
5092 r->left = 0;
5093 r->right = 0;
5094 r->balance = 0;
5096 while (p)
5098 struct init_node *s;
5100 if (r == p->left)
5102 if (p->balance == 0)
5103 p->balance = -1;
5104 else if (p->balance < 0)
5106 if (r->balance < 0)
5108 /* L rotation. */
5109 p->left = r->right;
5110 if (p->left)
5111 p->left->parent = p;
5112 r->right = p;
5114 p->balance = 0;
5115 r->balance = 0;
5117 s = p->parent;
5118 p->parent = r;
5119 r->parent = s;
5120 if (s)
5122 if (s->left == p)
5123 s->left = r;
5124 else
5125 s->right = r;
5127 else
5128 constructor_pending_elts = r;
5130 else
5132 /* LR rotation. */
5133 struct init_node *t = r->right;
5135 r->right = t->left;
5136 if (r->right)
5137 r->right->parent = r;
5138 t->left = r;
5140 p->left = t->right;
5141 if (p->left)
5142 p->left->parent = p;
5143 t->right = p;
5145 p->balance = t->balance < 0;
5146 r->balance = -(t->balance > 0);
5147 t->balance = 0;
5149 s = p->parent;
5150 p->parent = t;
5151 r->parent = t;
5152 t->parent = s;
5153 if (s)
5155 if (s->left == p)
5156 s->left = t;
5157 else
5158 s->right = t;
5160 else
5161 constructor_pending_elts = t;
5163 break;
5165 else
5167 /* p->balance == +1; growth of left side balances the node. */
5168 p->balance = 0;
5169 break;
5172 else /* r == p->right */
5174 if (p->balance == 0)
5175 /* Growth propagation from right side. */
5176 p->balance++;
5177 else if (p->balance > 0)
5179 if (r->balance > 0)
5181 /* R rotation. */
5182 p->right = r->left;
5183 if (p->right)
5184 p->right->parent = p;
5185 r->left = p;
5187 p->balance = 0;
5188 r->balance = 0;
5190 s = p->parent;
5191 p->parent = r;
5192 r->parent = s;
5193 if (s)
5195 if (s->left == p)
5196 s->left = r;
5197 else
5198 s->right = r;
5200 else
5201 constructor_pending_elts = r;
5203 else /* r->balance == -1 */
5205 /* RL rotation */
5206 struct init_node *t = r->left;
5208 r->left = t->right;
5209 if (r->left)
5210 r->left->parent = r;
5211 t->right = r;
5213 p->right = t->left;
5214 if (p->right)
5215 p->right->parent = p;
5216 t->left = p;
5218 r->balance = (t->balance < 0);
5219 p->balance = -(t->balance > 0);
5220 t->balance = 0;
5222 s = p->parent;
5223 p->parent = t;
5224 r->parent = t;
5225 t->parent = s;
5226 if (s)
5228 if (s->left == p)
5229 s->left = t;
5230 else
5231 s->right = t;
5233 else
5234 constructor_pending_elts = t;
5236 break;
5238 else
5240 /* p->balance == -1; growth of right side balances the node. */
5241 p->balance = 0;
5242 break;
5246 r = p;
5247 p = p->parent;
5251 /* Build AVL tree from a sorted chain. */
5253 static void
5254 set_nonincremental_init (void)
5256 tree chain;
5258 if (TREE_CODE (constructor_type) != RECORD_TYPE
5259 && TREE_CODE (constructor_type) != ARRAY_TYPE)
5260 return;
5262 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5263 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5264 constructor_elements = 0;
5265 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5267 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5268 /* Skip any nameless bit fields at the beginning. */
5269 while (constructor_unfilled_fields != 0
5270 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5271 && DECL_NAME (constructor_unfilled_fields) == 0)
5272 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5275 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5277 if (TYPE_DOMAIN (constructor_type))
5278 constructor_unfilled_index
5279 = convert (bitsizetype,
5280 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5281 else
5282 constructor_unfilled_index = bitsize_zero_node;
5284 constructor_incremental = 0;
5287 /* Build AVL tree from a string constant. */
5289 static void
5290 set_nonincremental_init_from_string (tree str)
5292 tree value, purpose, type;
5293 HOST_WIDE_INT val[2];
5294 const char *p, *end;
5295 int byte, wchar_bytes, charwidth, bitpos;
5297 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5298 abort ();
5300 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5301 == TYPE_PRECISION (char_type_node))
5302 wchar_bytes = 1;
5303 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5304 == TYPE_PRECISION (wchar_type_node))
5305 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5306 else
5307 abort ();
5309 charwidth = TYPE_PRECISION (char_type_node);
5310 type = TREE_TYPE (constructor_type);
5311 p = TREE_STRING_POINTER (str);
5312 end = p + TREE_STRING_LENGTH (str);
5314 for (purpose = bitsize_zero_node;
5315 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5316 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5318 if (wchar_bytes == 1)
5320 val[1] = (unsigned char) *p++;
5321 val[0] = 0;
5323 else
5325 val[0] = 0;
5326 val[1] = 0;
5327 for (byte = 0; byte < wchar_bytes; byte++)
5329 if (BYTES_BIG_ENDIAN)
5330 bitpos = (wchar_bytes - byte - 1) * charwidth;
5331 else
5332 bitpos = byte * charwidth;
5333 val[bitpos < HOST_BITS_PER_WIDE_INT]
5334 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5335 << (bitpos % HOST_BITS_PER_WIDE_INT);
5339 if (!TREE_UNSIGNED (type))
5341 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5342 if (bitpos < HOST_BITS_PER_WIDE_INT)
5344 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5346 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5347 val[0] = -1;
5350 else if (bitpos == HOST_BITS_PER_WIDE_INT)
5352 if (val[1] < 0)
5353 val[0] = -1;
5355 else if (val[0] & (((HOST_WIDE_INT) 1)
5356 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5357 val[0] |= ((HOST_WIDE_INT) -1)
5358 << (bitpos - HOST_BITS_PER_WIDE_INT);
5361 value = build_int_2 (val[1], val[0]);
5362 TREE_TYPE (value) = type;
5363 add_pending_init (purpose, value);
5366 constructor_incremental = 0;
5369 /* Return value of FIELD in pending initializer or zero if the field was
5370 not initialized yet. */
5372 static tree
5373 find_init_member (tree field)
5375 struct init_node *p;
5377 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5379 if (constructor_incremental
5380 && tree_int_cst_lt (field, constructor_unfilled_index))
5381 set_nonincremental_init ();
5383 p = constructor_pending_elts;
5384 while (p)
5386 if (tree_int_cst_lt (field, p->purpose))
5387 p = p->left;
5388 else if (tree_int_cst_lt (p->purpose, field))
5389 p = p->right;
5390 else
5391 return p->value;
5394 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5396 tree bitpos = bit_position (field);
5398 if (constructor_incremental
5399 && (!constructor_unfilled_fields
5400 || tree_int_cst_lt (bitpos,
5401 bit_position (constructor_unfilled_fields))))
5402 set_nonincremental_init ();
5404 p = constructor_pending_elts;
5405 while (p)
5407 if (field == p->purpose)
5408 return p->value;
5409 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5410 p = p->left;
5411 else
5412 p = p->right;
5415 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5417 if (constructor_elements
5418 && TREE_PURPOSE (constructor_elements) == field)
5419 return TREE_VALUE (constructor_elements);
5421 return 0;
5424 /* "Output" the next constructor element.
5425 At top level, really output it to assembler code now.
5426 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5427 TYPE is the data type that the containing data type wants here.
5428 FIELD is the field (a FIELD_DECL) or the index that this element fills.
5430 PENDING if non-nil means output pending elements that belong
5431 right after this element. (PENDING is normally 1;
5432 it is 0 while outputting pending elements, to avoid recursion.) */
5434 static void
5435 output_init_element (tree value, tree type, tree field, int pending)
5437 if (type == error_mark_node)
5439 constructor_erroneous = 1;
5440 return;
5442 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5443 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5444 && !(TREE_CODE (value) == STRING_CST
5445 && TREE_CODE (type) == ARRAY_TYPE
5446 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5447 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5448 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
5449 value = default_conversion (value);
5451 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5452 && require_constant_value && !flag_isoc99 && pending)
5454 /* As an extension, allow initializing objects with static storage
5455 duration with compound literals (which are then treated just as
5456 the brace enclosed list they contain). */
5457 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5458 value = DECL_INITIAL (decl);
5461 if (value == error_mark_node)
5462 constructor_erroneous = 1;
5463 else if (!TREE_CONSTANT (value))
5464 constructor_constant = 0;
5465 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5466 || ((TREE_CODE (constructor_type) == RECORD_TYPE
5467 || TREE_CODE (constructor_type) == UNION_TYPE)
5468 && DECL_C_BIT_FIELD (field)
5469 && TREE_CODE (value) != INTEGER_CST))
5470 constructor_simple = 0;
5472 if (require_constant_value && ! TREE_CONSTANT (value))
5474 error_init ("initializer element is not constant");
5475 value = error_mark_node;
5477 else if (require_constant_elements
5478 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5479 pedwarn ("initializer element is not computable at load time");
5481 /* If this field is empty (and not at the end of structure),
5482 don't do anything other than checking the initializer. */
5483 if (field
5484 && (TREE_TYPE (field) == error_mark_node
5485 || (COMPLETE_TYPE_P (TREE_TYPE (field))
5486 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5487 && (TREE_CODE (constructor_type) == ARRAY_TYPE
5488 || TREE_CHAIN (field)))))
5489 return;
5491 value = digest_init (type, value, require_constant_value);
5492 if (value == error_mark_node)
5494 constructor_erroneous = 1;
5495 return;
5498 /* If this element doesn't come next in sequence,
5499 put it on constructor_pending_elts. */
5500 if (TREE_CODE (constructor_type) == ARRAY_TYPE
5501 && (!constructor_incremental
5502 || !tree_int_cst_equal (field, constructor_unfilled_index)))
5504 if (constructor_incremental
5505 && tree_int_cst_lt (field, constructor_unfilled_index))
5506 set_nonincremental_init ();
5508 add_pending_init (field, value);
5509 return;
5511 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5512 && (!constructor_incremental
5513 || field != constructor_unfilled_fields))
5515 /* We do this for records but not for unions. In a union,
5516 no matter which field is specified, it can be initialized
5517 right away since it starts at the beginning of the union. */
5518 if (constructor_incremental)
5520 if (!constructor_unfilled_fields)
5521 set_nonincremental_init ();
5522 else
5524 tree bitpos, unfillpos;
5526 bitpos = bit_position (field);
5527 unfillpos = bit_position (constructor_unfilled_fields);
5529 if (tree_int_cst_lt (bitpos, unfillpos))
5530 set_nonincremental_init ();
5534 add_pending_init (field, value);
5535 return;
5537 else if (TREE_CODE (constructor_type) == UNION_TYPE
5538 && constructor_elements)
5540 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5541 warning_init ("initialized field with side-effects overwritten");
5543 /* We can have just one union field set. */
5544 constructor_elements = 0;
5547 /* Otherwise, output this element either to
5548 constructor_elements or to the assembler file. */
5550 if (field && TREE_CODE (field) == INTEGER_CST)
5551 field = copy_node (field);
5552 constructor_elements
5553 = tree_cons (field, value, constructor_elements);
5555 /* Advance the variable that indicates sequential elements output. */
5556 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5557 constructor_unfilled_index
5558 = size_binop (PLUS_EXPR, constructor_unfilled_index,
5559 bitsize_one_node);
5560 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5562 constructor_unfilled_fields
5563 = TREE_CHAIN (constructor_unfilled_fields);
5565 /* Skip any nameless bit fields. */
5566 while (constructor_unfilled_fields != 0
5567 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5568 && DECL_NAME (constructor_unfilled_fields) == 0)
5569 constructor_unfilled_fields =
5570 TREE_CHAIN (constructor_unfilled_fields);
5572 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5573 constructor_unfilled_fields = 0;
5575 /* Now output any pending elements which have become next. */
5576 if (pending)
5577 output_pending_init_elements (0);
5580 /* Output any pending elements which have become next.
5581 As we output elements, constructor_unfilled_{fields,index}
5582 advances, which may cause other elements to become next;
5583 if so, they too are output.
5585 If ALL is 0, we return when there are
5586 no more pending elements to output now.
5588 If ALL is 1, we output space as necessary so that
5589 we can output all the pending elements. */
5591 static void
5592 output_pending_init_elements (int all)
5594 struct init_node *elt = constructor_pending_elts;
5595 tree next;
5597 retry:
5599 /* Look thru the whole pending tree.
5600 If we find an element that should be output now,
5601 output it. Otherwise, set NEXT to the element
5602 that comes first among those still pending. */
5604 next = 0;
5605 while (elt)
5607 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5609 if (tree_int_cst_equal (elt->purpose,
5610 constructor_unfilled_index))
5611 output_init_element (elt->value,
5612 TREE_TYPE (constructor_type),
5613 constructor_unfilled_index, 0);
5614 else if (tree_int_cst_lt (constructor_unfilled_index,
5615 elt->purpose))
5617 /* Advance to the next smaller node. */
5618 if (elt->left)
5619 elt = elt->left;
5620 else
5622 /* We have reached the smallest node bigger than the
5623 current unfilled index. Fill the space first. */
5624 next = elt->purpose;
5625 break;
5628 else
5630 /* Advance to the next bigger node. */
5631 if (elt->right)
5632 elt = elt->right;
5633 else
5635 /* We have reached the biggest node in a subtree. Find
5636 the parent of it, which is the next bigger node. */
5637 while (elt->parent && elt->parent->right == elt)
5638 elt = elt->parent;
5639 elt = elt->parent;
5640 if (elt && tree_int_cst_lt (constructor_unfilled_index,
5641 elt->purpose))
5643 next = elt->purpose;
5644 break;
5649 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5650 || TREE_CODE (constructor_type) == UNION_TYPE)
5652 tree ctor_unfilled_bitpos, elt_bitpos;
5654 /* If the current record is complete we are done. */
5655 if (constructor_unfilled_fields == 0)
5656 break;
5658 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5659 elt_bitpos = bit_position (elt->purpose);
5660 /* We can't compare fields here because there might be empty
5661 fields in between. */
5662 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5664 constructor_unfilled_fields = elt->purpose;
5665 output_init_element (elt->value, TREE_TYPE (elt->purpose),
5666 elt->purpose, 0);
5668 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5670 /* Advance to the next smaller node. */
5671 if (elt->left)
5672 elt = elt->left;
5673 else
5675 /* We have reached the smallest node bigger than the
5676 current unfilled field. Fill the space first. */
5677 next = elt->purpose;
5678 break;
5681 else
5683 /* Advance to the next bigger node. */
5684 if (elt->right)
5685 elt = elt->right;
5686 else
5688 /* We have reached the biggest node in a subtree. Find
5689 the parent of it, which is the next bigger node. */
5690 while (elt->parent && elt->parent->right == elt)
5691 elt = elt->parent;
5692 elt = elt->parent;
5693 if (elt
5694 && (tree_int_cst_lt (ctor_unfilled_bitpos,
5695 bit_position (elt->purpose))))
5697 next = elt->purpose;
5698 break;
5705 /* Ordinarily return, but not if we want to output all
5706 and there are elements left. */
5707 if (! (all && next != 0))
5708 return;
5710 /* If it's not incremental, just skip over the gap, so that after
5711 jumping to retry we will output the next successive element. */
5712 if (TREE_CODE (constructor_type) == RECORD_TYPE
5713 || TREE_CODE (constructor_type) == UNION_TYPE)
5714 constructor_unfilled_fields = next;
5715 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5716 constructor_unfilled_index = next;
5718 /* ELT now points to the node in the pending tree with the next
5719 initializer to output. */
5720 goto retry;
5723 /* Add one non-braced element to the current constructor level.
5724 This adjusts the current position within the constructor's type.
5725 This may also start or terminate implicit levels
5726 to handle a partly-braced initializer.
5728 Once this has found the correct level for the new element,
5729 it calls output_init_element. */
5731 void
5732 process_init_element (tree value)
5734 tree orig_value = value;
5735 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5737 designator_depth = 0;
5738 designator_errorneous = 0;
5740 /* Handle superfluous braces around string cst as in
5741 char x[] = {"foo"}; */
5742 if (string_flag
5743 && constructor_type
5744 && TREE_CODE (constructor_type) == ARRAY_TYPE
5745 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5746 && integer_zerop (constructor_unfilled_index))
5748 if (constructor_stack->replacement_value)
5749 error_init ("excess elements in char array initializer");
5750 constructor_stack->replacement_value = value;
5751 return;
5754 if (constructor_stack->replacement_value != 0)
5756 error_init ("excess elements in struct initializer");
5757 return;
5760 /* Ignore elements of a brace group if it is entirely superfluous
5761 and has already been diagnosed. */
5762 if (constructor_type == 0)
5763 return;
5765 /* If we've exhausted any levels that didn't have braces,
5766 pop them now. */
5767 while (constructor_stack->implicit)
5769 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5770 || TREE_CODE (constructor_type) == UNION_TYPE)
5771 && constructor_fields == 0)
5772 process_init_element (pop_init_level (1));
5773 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5774 && (constructor_max_index == 0
5775 || tree_int_cst_lt (constructor_max_index,
5776 constructor_index)))
5777 process_init_element (pop_init_level (1));
5778 else
5779 break;
5782 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
5783 if (constructor_range_stack)
5785 /* If value is a compound literal and we'll be just using its
5786 content, don't put it into a SAVE_EXPR. */
5787 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5788 || !require_constant_value
5789 || flag_isoc99)
5790 value = save_expr (value);
5793 while (1)
5795 if (TREE_CODE (constructor_type) == RECORD_TYPE)
5797 tree fieldtype;
5798 enum tree_code fieldcode;
5800 if (constructor_fields == 0)
5802 pedwarn_init ("excess elements in struct initializer");
5803 break;
5806 fieldtype = TREE_TYPE (constructor_fields);
5807 if (fieldtype != error_mark_node)
5808 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5809 fieldcode = TREE_CODE (fieldtype);
5811 /* Error for non-static initialization of a flexible array member. */
5812 if (fieldcode == ARRAY_TYPE
5813 && !require_constant_value
5814 && TYPE_SIZE (fieldtype) == NULL_TREE
5815 && TREE_CHAIN (constructor_fields) == NULL_TREE)
5817 error_init ("non-static initialization of a flexible array member");
5818 break;
5821 /* Accept a string constant to initialize a subarray. */
5822 if (value != 0
5823 && fieldcode == ARRAY_TYPE
5824 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5825 && string_flag)
5826 value = orig_value;
5827 /* Otherwise, if we have come to a subaggregate,
5828 and we don't have an element of its type, push into it. */
5829 else if (value != 0 && !constructor_no_implicit
5830 && value != error_mark_node
5831 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5832 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5833 || fieldcode == UNION_TYPE))
5835 push_init_level (1);
5836 continue;
5839 if (value)
5841 push_member_name (constructor_fields);
5842 output_init_element (value, fieldtype, constructor_fields, 1);
5843 RESTORE_SPELLING_DEPTH (constructor_depth);
5845 else
5846 /* Do the bookkeeping for an element that was
5847 directly output as a constructor. */
5849 /* For a record, keep track of end position of last field. */
5850 if (DECL_SIZE (constructor_fields))
5851 constructor_bit_index
5852 = size_binop (PLUS_EXPR,
5853 bit_position (constructor_fields),
5854 DECL_SIZE (constructor_fields));
5856 /* If the current field was the first one not yet written out,
5857 it isn't now, so update. */
5858 if (constructor_unfilled_fields == constructor_fields)
5860 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5861 /* Skip any nameless bit fields. */
5862 while (constructor_unfilled_fields != 0
5863 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5864 && DECL_NAME (constructor_unfilled_fields) == 0)
5865 constructor_unfilled_fields =
5866 TREE_CHAIN (constructor_unfilled_fields);
5870 constructor_fields = TREE_CHAIN (constructor_fields);
5871 /* Skip any nameless bit fields at the beginning. */
5872 while (constructor_fields != 0
5873 && DECL_C_BIT_FIELD (constructor_fields)
5874 && DECL_NAME (constructor_fields) == 0)
5875 constructor_fields = TREE_CHAIN (constructor_fields);
5877 else if (TREE_CODE (constructor_type) == UNION_TYPE)
5879 tree fieldtype;
5880 enum tree_code fieldcode;
5882 if (constructor_fields == 0)
5884 pedwarn_init ("excess elements in union initializer");
5885 break;
5888 fieldtype = TREE_TYPE (constructor_fields);
5889 if (fieldtype != error_mark_node)
5890 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5891 fieldcode = TREE_CODE (fieldtype);
5893 /* Warn that traditional C rejects initialization of unions.
5894 We skip the warning if the value is zero. This is done
5895 under the assumption that the zero initializer in user
5896 code appears conditioned on e.g. __STDC__ to avoid
5897 "missing initializer" warnings and relies on default
5898 initialization to zero in the traditional C case.
5899 We also skip the warning if the initializer is designated,
5900 again on the assumption that this must be conditional on
5901 __STDC__ anyway (and we've already complained about the
5902 member-designator already). */
5903 if (warn_traditional && !in_system_header && !constructor_designated
5904 && !(value && (integer_zerop (value) || real_zerop (value))))
5905 warning ("traditional C rejects initialization of unions");
5907 /* Accept a string constant to initialize a subarray. */
5908 if (value != 0
5909 && fieldcode == ARRAY_TYPE
5910 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5911 && string_flag)
5912 value = orig_value;
5913 /* Otherwise, if we have come to a subaggregate,
5914 and we don't have an element of its type, push into it. */
5915 else if (value != 0 && !constructor_no_implicit
5916 && value != error_mark_node
5917 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5918 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5919 || fieldcode == UNION_TYPE))
5921 push_init_level (1);
5922 continue;
5925 if (value)
5927 push_member_name (constructor_fields);
5928 output_init_element (value, fieldtype, constructor_fields, 1);
5929 RESTORE_SPELLING_DEPTH (constructor_depth);
5931 else
5932 /* Do the bookkeeping for an element that was
5933 directly output as a constructor. */
5935 constructor_bit_index = DECL_SIZE (constructor_fields);
5936 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5939 constructor_fields = 0;
5941 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5943 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5944 enum tree_code eltcode = TREE_CODE (elttype);
5946 /* Accept a string constant to initialize a subarray. */
5947 if (value != 0
5948 && eltcode == ARRAY_TYPE
5949 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5950 && string_flag)
5951 value = orig_value;
5952 /* Otherwise, if we have come to a subaggregate,
5953 and we don't have an element of its type, push into it. */
5954 else if (value != 0 && !constructor_no_implicit
5955 && value != error_mark_node
5956 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
5957 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5958 || eltcode == UNION_TYPE))
5960 push_init_level (1);
5961 continue;
5964 if (constructor_max_index != 0
5965 && (tree_int_cst_lt (constructor_max_index, constructor_index)
5966 || integer_all_onesp (constructor_max_index)))
5968 pedwarn_init ("excess elements in array initializer");
5969 break;
5972 /* Now output the actual element. */
5973 if (value)
5975 push_array_bounds (tree_low_cst (constructor_index, 0));
5976 output_init_element (value, elttype, constructor_index, 1);
5977 RESTORE_SPELLING_DEPTH (constructor_depth);
5980 constructor_index
5981 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5983 if (! value)
5984 /* If we are doing the bookkeeping for an element that was
5985 directly output as a constructor, we must update
5986 constructor_unfilled_index. */
5987 constructor_unfilled_index = constructor_index;
5989 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5991 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5993 /* Do a basic check of initializer size. Note that vectors
5994 always have a fixed size derived from their type. */
5995 if (tree_int_cst_lt (constructor_max_index, constructor_index))
5997 pedwarn_init ("excess elements in vector initializer");
5998 break;
6001 /* Now output the actual element. */
6002 if (value)
6003 output_init_element (value, elttype, constructor_index, 1);
6005 constructor_index
6006 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6008 if (! value)
6009 /* If we are doing the bookkeeping for an element that was
6010 directly output as a constructor, we must update
6011 constructor_unfilled_index. */
6012 constructor_unfilled_index = constructor_index;
6015 /* Handle the sole element allowed in a braced initializer
6016 for a scalar variable. */
6017 else if (constructor_fields == 0)
6019 pedwarn_init ("excess elements in scalar initializer");
6020 break;
6022 else
6024 if (value)
6025 output_init_element (value, constructor_type, NULL_TREE, 1);
6026 constructor_fields = 0;
6029 /* Handle range initializers either at this level or anywhere higher
6030 in the designator stack. */
6031 if (constructor_range_stack)
6033 struct constructor_range_stack *p, *range_stack;
6034 int finish = 0;
6036 range_stack = constructor_range_stack;
6037 constructor_range_stack = 0;
6038 while (constructor_stack != range_stack->stack)
6040 if (!constructor_stack->implicit)
6041 abort ();
6042 process_init_element (pop_init_level (1));
6044 for (p = range_stack;
6045 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6046 p = p->prev)
6048 if (!constructor_stack->implicit)
6049 abort ();
6050 process_init_element (pop_init_level (1));
6053 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6054 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6055 finish = 1;
6057 while (1)
6059 constructor_index = p->index;
6060 constructor_fields = p->fields;
6061 if (finish && p->range_end && p->index == p->range_start)
6063 finish = 0;
6064 p->prev = 0;
6066 p = p->next;
6067 if (!p)
6068 break;
6069 push_init_level (2);
6070 p->stack = constructor_stack;
6071 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6072 p->index = p->range_start;
6075 if (!finish)
6076 constructor_range_stack = range_stack;
6077 continue;
6080 break;
6083 constructor_range_stack = 0;
6086 /* Build a simple asm-statement, from one string literal. */
6087 tree
6088 simple_asm_stmt (tree expr)
6090 STRIP_NOPS (expr);
6092 if (TREE_CODE (expr) == ADDR_EXPR)
6093 expr = TREE_OPERAND (expr, 0);
6095 if (TREE_CODE (expr) == STRING_CST)
6097 tree stmt;
6099 /* Simple asm statements are treated as volatile. */
6100 stmt = add_stmt (build_stmt (ASM_STMT, ridpointers[(int) RID_VOLATILE],
6101 expr, NULL_TREE, NULL_TREE, NULL_TREE));
6102 ASM_INPUT_P (stmt) = 1;
6103 return stmt;
6106 error ("argument of `asm' is not a constant string");
6107 return NULL_TREE;
6110 /* Build an asm-statement, whose components are a CV_QUALIFIER, a
6111 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
6113 tree
6114 build_asm_stmt (tree cv_qualifier, tree string, tree outputs, tree inputs,
6115 tree clobbers)
6117 tree tail;
6119 if (TREE_CODE (string) != STRING_CST)
6121 error ("asm template is not a string constant");
6122 return NULL_TREE;
6125 if (cv_qualifier != NULL_TREE
6126 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6128 warning ("%s qualifier ignored on asm",
6129 IDENTIFIER_POINTER (cv_qualifier));
6130 cv_qualifier = NULL_TREE;
6133 /* We can remove output conversions that change the type,
6134 but not the mode. */
6135 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6137 tree output = TREE_VALUE (tail);
6139 STRIP_NOPS (output);
6140 TREE_VALUE (tail) = output;
6142 /* Allow conversions as LHS here. build_modify_expr as called below
6143 will do the right thing with them. */
6144 while (TREE_CODE (output) == NOP_EXPR
6145 || TREE_CODE (output) == CONVERT_EXPR
6146 || TREE_CODE (output) == FLOAT_EXPR
6147 || TREE_CODE (output) == FIX_TRUNC_EXPR
6148 || TREE_CODE (output) == FIX_FLOOR_EXPR
6149 || TREE_CODE (output) == FIX_ROUND_EXPR
6150 || TREE_CODE (output) == FIX_CEIL_EXPR)
6151 output = TREE_OPERAND (output, 0);
6153 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6156 /* Remove output conversions that change the type but not the mode. */
6157 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6159 tree output = TREE_VALUE (tail);
6160 STRIP_NOPS (output);
6161 TREE_VALUE (tail) = output;
6164 /* Perform default conversions on array and function inputs.
6165 Don't do this for other types as it would screw up operands
6166 expected to be in memory. */
6167 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6168 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6170 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6171 outputs, inputs, clobbers));
6174 /* Expand an ASM statement with operands, handling output operands
6175 that are not variables or INDIRECT_REFS by transforming such
6176 cases into cases that expand_asm_operands can handle.
6178 Arguments are same as for expand_asm_operands. */
6180 void
6181 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6182 tree clobbers, int vol, const char *filename,
6183 int line)
6185 int noutputs = list_length (outputs);
6186 int i;
6187 /* o[I] is the place that output number I should be written. */
6188 tree *o = alloca (noutputs * sizeof (tree));
6189 tree tail;
6191 /* Record the contents of OUTPUTS before it is modified. */
6192 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6194 o[i] = TREE_VALUE (tail);
6195 if (o[i] == error_mark_node)
6196 return;
6199 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6200 OUTPUTS some trees for where the values were actually stored. */
6201 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6203 /* Copy all the intermediate outputs into the specified outputs. */
6204 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6206 if (o[i] != TREE_VALUE (tail))
6208 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6209 NULL_RTX, VOIDmode, EXPAND_NORMAL);
6210 free_temp_slots ();
6212 /* Restore the original value so that it's correct the next
6213 time we expand this function. */
6214 TREE_VALUE (tail) = o[i];
6216 /* Detect modification of read-only values.
6217 (Otherwise done by build_modify_expr.) */
6218 else
6220 tree type = TREE_TYPE (o[i]);
6221 if (TREE_READONLY (o[i])
6222 || TYPE_READONLY (type)
6223 || ((TREE_CODE (type) == RECORD_TYPE
6224 || TREE_CODE (type) == UNION_TYPE)
6225 && C_TYPE_FIELDS_READONLY (type)))
6226 readonly_warning (o[i], "modification by `asm'");
6230 /* Those MODIFY_EXPRs could do autoincrements. */
6231 emit_queue ();
6234 /* Expand a C `return' statement.
6235 RETVAL is the expression for what to return,
6236 or a null pointer for `return;' with no value. */
6238 tree
6239 c_expand_return (tree retval)
6241 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6243 if (TREE_THIS_VOLATILE (current_function_decl))
6244 warning ("function declared `noreturn' has a `return' statement");
6246 if (!retval)
6248 current_function_returns_null = 1;
6249 if ((warn_return_type || flag_isoc99)
6250 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6251 pedwarn_c99 ("`return' with no value, in function returning non-void");
6253 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6255 current_function_returns_null = 1;
6256 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6257 pedwarn ("`return' with a value, in function returning void");
6259 else
6261 tree t = convert_for_assignment (valtype, retval, _("return"),
6262 NULL_TREE, NULL_TREE, 0);
6263 tree res = DECL_RESULT (current_function_decl);
6264 tree inner;
6266 current_function_returns_value = 1;
6267 if (t == error_mark_node)
6268 return NULL_TREE;
6270 inner = t = convert (TREE_TYPE (res), t);
6272 /* Strip any conversions, additions, and subtractions, and see if
6273 we are returning the address of a local variable. Warn if so. */
6274 while (1)
6276 switch (TREE_CODE (inner))
6278 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
6279 case PLUS_EXPR:
6280 inner = TREE_OPERAND (inner, 0);
6281 continue;
6283 case MINUS_EXPR:
6284 /* If the second operand of the MINUS_EXPR has a pointer
6285 type (or is converted from it), this may be valid, so
6286 don't give a warning. */
6288 tree op1 = TREE_OPERAND (inner, 1);
6290 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6291 && (TREE_CODE (op1) == NOP_EXPR
6292 || TREE_CODE (op1) == NON_LVALUE_EXPR
6293 || TREE_CODE (op1) == CONVERT_EXPR))
6294 op1 = TREE_OPERAND (op1, 0);
6296 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6297 break;
6299 inner = TREE_OPERAND (inner, 0);
6300 continue;
6303 case ADDR_EXPR:
6304 inner = TREE_OPERAND (inner, 0);
6306 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6307 inner = TREE_OPERAND (inner, 0);
6309 if (TREE_CODE (inner) == VAR_DECL
6310 && ! DECL_EXTERNAL (inner)
6311 && ! TREE_STATIC (inner)
6312 && DECL_CONTEXT (inner) == current_function_decl)
6313 warning ("function returns address of local variable");
6314 break;
6316 default:
6317 break;
6320 break;
6323 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6326 return add_stmt (build_return_stmt (retval));
6329 struct c_switch {
6330 /* The SWITCH_STMT being built. */
6331 tree switch_stmt;
6332 /* A splay-tree mapping the low element of a case range to the high
6333 element, or NULL_TREE if there is no high element. Used to
6334 determine whether or not a new case label duplicates an old case
6335 label. We need a tree, rather than simply a hash table, because
6336 of the GNU case range extension. */
6337 splay_tree cases;
6338 /* The next node on the stack. */
6339 struct c_switch *next;
6342 /* A stack of the currently active switch statements. The innermost
6343 switch statement is on the top of the stack. There is no need to
6344 mark the stack for garbage collection because it is only active
6345 during the processing of the body of a function, and we never
6346 collect at that point. */
6348 static struct c_switch *switch_stack;
6350 /* Start a C switch statement, testing expression EXP. Return the new
6351 SWITCH_STMT. */
6353 tree
6354 c_start_case (tree exp)
6356 enum tree_code code;
6357 tree type, orig_type = error_mark_node;
6358 struct c_switch *cs;
6360 if (exp != error_mark_node)
6362 code = TREE_CODE (TREE_TYPE (exp));
6363 orig_type = TREE_TYPE (exp);
6365 if (! INTEGRAL_TYPE_P (orig_type)
6366 && code != ERROR_MARK)
6368 error ("switch quantity not an integer");
6369 exp = integer_zero_node;
6371 else
6373 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6375 if (warn_traditional && !in_system_header
6376 && (type == long_integer_type_node
6377 || type == long_unsigned_type_node))
6378 warning ("`long' switch expression not converted to `int' in ISO C");
6380 exp = default_conversion (exp);
6381 type = TREE_TYPE (exp);
6385 /* Add this new SWITCH_STMT to the stack. */
6386 cs = xmalloc (sizeof (*cs));
6387 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6388 cs->cases = splay_tree_new (case_compare, NULL, NULL);
6389 cs->next = switch_stack;
6390 switch_stack = cs;
6392 return add_stmt (switch_stack->switch_stmt);
6395 /* Process a case label. */
6397 tree
6398 do_case (tree low_value, tree high_value)
6400 tree label = NULL_TREE;
6402 if (switch_stack)
6404 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
6406 label = c_add_case_label (switch_stack->cases,
6407 SWITCH_COND (switch_stack->switch_stmt),
6408 low_value, high_value);
6409 if (label == error_mark_node)
6410 label = NULL_TREE;
6411 else if (switch_was_empty_p)
6413 /* Attach the first case label to the SWITCH_BODY. */
6414 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
6415 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
6418 else if (low_value)
6419 error ("case label not within a switch statement");
6420 else
6421 error ("`default' label not within a switch statement");
6423 return label;
6426 /* Finish the switch statement. */
6428 void
6429 c_finish_case (void)
6431 struct c_switch *cs = switch_stack;
6433 /* Rechain the next statements to the SWITCH_STMT. */
6434 last_tree = cs->switch_stmt;
6436 /* Pop the stack. */
6437 switch_stack = switch_stack->next;
6438 splay_tree_delete (cs->cases);
6439 free (cs);
6442 /* Build a binary-operation expression without default conversions.
6443 CODE is the kind of expression to build.
6444 This function differs from `build' in several ways:
6445 the data type of the result is computed and recorded in it,
6446 warnings are generated if arg data types are invalid,
6447 special handling for addition and subtraction of pointers is known,
6448 and some optimization is done (operations on narrow ints
6449 are done in the narrower type when that gives the same result).
6450 Constant folding is also done before the result is returned.
6452 Note that the operands will never have enumeral types, or function
6453 or array types, because either they will have the default conversions
6454 performed or they have both just been converted to some other type in which
6455 the arithmetic is to be done. */
6457 tree
6458 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6459 int convert_p)
6461 tree type0, type1;
6462 enum tree_code code0, code1;
6463 tree op0, op1;
6465 /* Expression code to give to the expression when it is built.
6466 Normally this is CODE, which is what the caller asked for,
6467 but in some special cases we change it. */
6468 enum tree_code resultcode = code;
6470 /* Data type in which the computation is to be performed.
6471 In the simplest cases this is the common type of the arguments. */
6472 tree result_type = NULL;
6474 /* Nonzero means operands have already been type-converted
6475 in whatever way is necessary.
6476 Zero means they need to be converted to RESULT_TYPE. */
6477 int converted = 0;
6479 /* Nonzero means create the expression with this type, rather than
6480 RESULT_TYPE. */
6481 tree build_type = 0;
6483 /* Nonzero means after finally constructing the expression
6484 convert it to this type. */
6485 tree final_type = 0;
6487 /* Nonzero if this is an operation like MIN or MAX which can
6488 safely be computed in short if both args are promoted shorts.
6489 Also implies COMMON.
6490 -1 indicates a bitwise operation; this makes a difference
6491 in the exact conditions for when it is safe to do the operation
6492 in a narrower mode. */
6493 int shorten = 0;
6495 /* Nonzero if this is a comparison operation;
6496 if both args are promoted shorts, compare the original shorts.
6497 Also implies COMMON. */
6498 int short_compare = 0;
6500 /* Nonzero if this is a right-shift operation, which can be computed on the
6501 original short and then promoted if the operand is a promoted short. */
6502 int short_shift = 0;
6504 /* Nonzero means set RESULT_TYPE to the common type of the args. */
6505 int common = 0;
6507 if (convert_p)
6509 op0 = default_conversion (orig_op0);
6510 op1 = default_conversion (orig_op1);
6512 else
6514 op0 = orig_op0;
6515 op1 = orig_op1;
6518 type0 = TREE_TYPE (op0);
6519 type1 = TREE_TYPE (op1);
6521 /* The expression codes of the data types of the arguments tell us
6522 whether the arguments are integers, floating, pointers, etc. */
6523 code0 = TREE_CODE (type0);
6524 code1 = TREE_CODE (type1);
6526 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
6527 STRIP_TYPE_NOPS (op0);
6528 STRIP_TYPE_NOPS (op1);
6530 /* If an error was already reported for one of the arguments,
6531 avoid reporting another error. */
6533 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6534 return error_mark_node;
6536 switch (code)
6538 case PLUS_EXPR:
6539 /* Handle the pointer + int case. */
6540 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6541 return pointer_int_sum (PLUS_EXPR, op0, op1);
6542 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6543 return pointer_int_sum (PLUS_EXPR, op1, op0);
6544 else
6545 common = 1;
6546 break;
6548 case MINUS_EXPR:
6549 /* Subtraction of two similar pointers.
6550 We must subtract them as integers, then divide by object size. */
6551 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6552 && comp_target_types (type0, type1, 1))
6553 return pointer_diff (op0, op1);
6554 /* Handle pointer minus int. Just like pointer plus int. */
6555 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6556 return pointer_int_sum (MINUS_EXPR, op0, op1);
6557 else
6558 common = 1;
6559 break;
6561 case MULT_EXPR:
6562 common = 1;
6563 break;
6565 case TRUNC_DIV_EXPR:
6566 case CEIL_DIV_EXPR:
6567 case FLOOR_DIV_EXPR:
6568 case ROUND_DIV_EXPR:
6569 case EXACT_DIV_EXPR:
6570 /* Floating point division by zero is a legitimate way to obtain
6571 infinities and NaNs. */
6572 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6573 warning ("division by zero");
6575 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6576 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6577 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6578 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6580 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6581 resultcode = RDIV_EXPR;
6582 else
6583 /* Although it would be tempting to shorten always here, that
6584 loses on some targets, since the modulo instruction is
6585 undefined if the quotient can't be represented in the
6586 computation mode. We shorten only if unsigned or if
6587 dividing by something we know != -1. */
6588 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6589 || (TREE_CODE (op1) == INTEGER_CST
6590 && ! integer_all_onesp (op1)));
6591 common = 1;
6593 break;
6595 case BIT_AND_EXPR:
6596 case BIT_ANDTC_EXPR:
6597 case BIT_IOR_EXPR:
6598 case BIT_XOR_EXPR:
6599 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6600 shorten = -1;
6601 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6602 common = 1;
6603 break;
6605 case TRUNC_MOD_EXPR:
6606 case FLOOR_MOD_EXPR:
6607 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6608 warning ("division by zero");
6610 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6612 /* Although it would be tempting to shorten always here, that loses
6613 on some targets, since the modulo instruction is undefined if the
6614 quotient can't be represented in the computation mode. We shorten
6615 only if unsigned or if dividing by something we know != -1. */
6616 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6617 || (TREE_CODE (op1) == INTEGER_CST
6618 && ! integer_all_onesp (op1)));
6619 common = 1;
6621 break;
6623 case TRUTH_ANDIF_EXPR:
6624 case TRUTH_ORIF_EXPR:
6625 case TRUTH_AND_EXPR:
6626 case TRUTH_OR_EXPR:
6627 case TRUTH_XOR_EXPR:
6628 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6629 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6630 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6631 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6633 /* Result of these operations is always an int,
6634 but that does not mean the operands should be
6635 converted to ints! */
6636 result_type = integer_type_node;
6637 op0 = c_common_truthvalue_conversion (op0);
6638 op1 = c_common_truthvalue_conversion (op1);
6639 converted = 1;
6641 break;
6643 /* Shift operations: result has same type as first operand;
6644 always convert second operand to int.
6645 Also set SHORT_SHIFT if shifting rightward. */
6647 case RSHIFT_EXPR:
6648 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6650 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6652 if (tree_int_cst_sgn (op1) < 0)
6653 warning ("right shift count is negative");
6654 else
6656 if (! integer_zerop (op1))
6657 short_shift = 1;
6659 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6660 warning ("right shift count >= width of type");
6664 /* Use the type of the value to be shifted. */
6665 result_type = type0;
6666 /* Convert the shift-count to an integer, regardless of size
6667 of value being shifted. */
6668 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6669 op1 = convert (integer_type_node, op1);
6670 /* Avoid converting op1 to result_type later. */
6671 converted = 1;
6673 break;
6675 case LSHIFT_EXPR:
6676 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6678 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6680 if (tree_int_cst_sgn (op1) < 0)
6681 warning ("left shift count is negative");
6683 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6684 warning ("left shift count >= width of type");
6687 /* Use the type of the value to be shifted. */
6688 result_type = type0;
6689 /* Convert the shift-count to an integer, regardless of size
6690 of value being shifted. */
6691 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6692 op1 = convert (integer_type_node, op1);
6693 /* Avoid converting op1 to result_type later. */
6694 converted = 1;
6696 break;
6698 case RROTATE_EXPR:
6699 case LROTATE_EXPR:
6700 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6702 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6704 if (tree_int_cst_sgn (op1) < 0)
6705 warning ("shift count is negative");
6706 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6707 warning ("shift count >= width of type");
6710 /* Use the type of the value to be shifted. */
6711 result_type = type0;
6712 /* Convert the shift-count to an integer, regardless of size
6713 of value being shifted. */
6714 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6715 op1 = convert (integer_type_node, op1);
6716 /* Avoid converting op1 to result_type later. */
6717 converted = 1;
6719 break;
6721 case EQ_EXPR:
6722 case NE_EXPR:
6723 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
6724 warning ("comparing floating point with == or != is unsafe");
6725 /* Result of comparison is always int,
6726 but don't convert the args to int! */
6727 build_type = integer_type_node;
6728 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6729 || code0 == COMPLEX_TYPE
6730 || code0 == VECTOR_TYPE)
6731 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6732 || code1 == COMPLEX_TYPE
6733 || code1 == VECTOR_TYPE))
6734 short_compare = 1;
6735 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6737 tree tt0 = TREE_TYPE (type0);
6738 tree tt1 = TREE_TYPE (type1);
6739 /* Anything compares with void *. void * compares with anything.
6740 Otherwise, the targets must be compatible
6741 and both must be object or both incomplete. */
6742 if (comp_target_types (type0, type1, 1))
6743 result_type = common_type (type0, type1);
6744 else if (VOID_TYPE_P (tt0))
6746 /* op0 != orig_op0 detects the case of something
6747 whose value is 0 but which isn't a valid null ptr const. */
6748 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
6749 && TREE_CODE (tt1) == FUNCTION_TYPE)
6750 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6752 else if (VOID_TYPE_P (tt1))
6754 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
6755 && TREE_CODE (tt0) == FUNCTION_TYPE)
6756 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6758 else
6759 pedwarn ("comparison of distinct pointer types lacks a cast");
6761 if (result_type == NULL_TREE)
6762 result_type = ptr_type_node;
6764 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6765 && integer_zerop (op1))
6766 result_type = type0;
6767 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6768 && integer_zerop (op0))
6769 result_type = type1;
6770 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6772 result_type = type0;
6773 pedwarn ("comparison between pointer and integer");
6775 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6777 result_type = type1;
6778 pedwarn ("comparison between pointer and integer");
6780 break;
6782 case MAX_EXPR:
6783 case MIN_EXPR:
6784 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6785 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6786 shorten = 1;
6787 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6789 if (comp_target_types (type0, type1, 1))
6791 result_type = common_type (type0, type1);
6792 if (pedantic
6793 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6794 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6796 else
6798 result_type = ptr_type_node;
6799 pedwarn ("comparison of distinct pointer types lacks a cast");
6802 break;
6804 case LE_EXPR:
6805 case GE_EXPR:
6806 case LT_EXPR:
6807 case GT_EXPR:
6808 build_type = integer_type_node;
6809 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6810 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6811 short_compare = 1;
6812 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6814 if (comp_target_types (type0, type1, 1))
6816 result_type = common_type (type0, type1);
6817 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
6818 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
6819 pedwarn ("comparison of complete and incomplete pointers");
6820 else if (pedantic
6821 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6822 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6824 else
6826 result_type = ptr_type_node;
6827 pedwarn ("comparison of distinct pointer types lacks a cast");
6830 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6831 && integer_zerop (op1))
6833 result_type = type0;
6834 if (pedantic || extra_warnings)
6835 pedwarn ("ordered comparison of pointer with integer zero");
6837 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6838 && integer_zerop (op0))
6840 result_type = type1;
6841 if (pedantic)
6842 pedwarn ("ordered comparison of pointer with integer zero");
6844 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6846 result_type = type0;
6847 pedwarn ("comparison between pointer and integer");
6849 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6851 result_type = type1;
6852 pedwarn ("comparison between pointer and integer");
6854 break;
6856 case UNORDERED_EXPR:
6857 case ORDERED_EXPR:
6858 case UNLT_EXPR:
6859 case UNLE_EXPR:
6860 case UNGT_EXPR:
6861 case UNGE_EXPR:
6862 case UNEQ_EXPR:
6863 build_type = integer_type_node;
6864 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6866 error ("unordered comparison on non-floating point argument");
6867 return error_mark_node;
6869 common = 1;
6870 break;
6872 default:
6873 break;
6876 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6877 || code0 == VECTOR_TYPE)
6879 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
6880 || code1 == VECTOR_TYPE))
6882 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
6884 if (shorten || common || short_compare)
6885 result_type = common_type (type0, type1);
6887 /* For certain operations (which identify themselves by shorten != 0)
6888 if both args were extended from the same smaller type,
6889 do the arithmetic in that type and then extend.
6891 shorten !=0 and !=1 indicates a bitwise operation.
6892 For them, this optimization is safe only if
6893 both args are zero-extended or both are sign-extended.
6894 Otherwise, we might change the result.
6895 Eg, (short)-1 | (unsigned short)-1 is (int)-1
6896 but calculated in (unsigned short) it would be (unsigned short)-1. */
6898 if (shorten && none_complex)
6900 int unsigned0, unsigned1;
6901 tree arg0 = get_narrower (op0, &unsigned0);
6902 tree arg1 = get_narrower (op1, &unsigned1);
6903 /* UNS is 1 if the operation to be done is an unsigned one. */
6904 int uns = TREE_UNSIGNED (result_type);
6905 tree type;
6907 final_type = result_type;
6909 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6910 but it *requires* conversion to FINAL_TYPE. */
6912 if ((TYPE_PRECISION (TREE_TYPE (op0))
6913 == TYPE_PRECISION (TREE_TYPE (arg0)))
6914 && TREE_TYPE (op0) != final_type)
6915 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
6916 if ((TYPE_PRECISION (TREE_TYPE (op1))
6917 == TYPE_PRECISION (TREE_TYPE (arg1)))
6918 && TREE_TYPE (op1) != final_type)
6919 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
6921 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
6923 /* For bitwise operations, signedness of nominal type
6924 does not matter. Consider only how operands were extended. */
6925 if (shorten == -1)
6926 uns = unsigned0;
6928 /* Note that in all three cases below we refrain from optimizing
6929 an unsigned operation on sign-extended args.
6930 That would not be valid. */
6932 /* Both args variable: if both extended in same way
6933 from same width, do it in that width.
6934 Do it unsigned if args were zero-extended. */
6935 if ((TYPE_PRECISION (TREE_TYPE (arg0))
6936 < TYPE_PRECISION (result_type))
6937 && (TYPE_PRECISION (TREE_TYPE (arg1))
6938 == TYPE_PRECISION (TREE_TYPE (arg0)))
6939 && unsigned0 == unsigned1
6940 && (unsigned0 || !uns))
6941 result_type
6942 = c_common_signed_or_unsigned_type
6943 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
6944 else if (TREE_CODE (arg0) == INTEGER_CST
6945 && (unsigned1 || !uns)
6946 && (TYPE_PRECISION (TREE_TYPE (arg1))
6947 < TYPE_PRECISION (result_type))
6948 && (type
6949 = c_common_signed_or_unsigned_type (unsigned1,
6950 TREE_TYPE (arg1)),
6951 int_fits_type_p (arg0, type)))
6952 result_type = type;
6953 else if (TREE_CODE (arg1) == INTEGER_CST
6954 && (unsigned0 || !uns)
6955 && (TYPE_PRECISION (TREE_TYPE (arg0))
6956 < TYPE_PRECISION (result_type))
6957 && (type
6958 = c_common_signed_or_unsigned_type (unsigned0,
6959 TREE_TYPE (arg0)),
6960 int_fits_type_p (arg1, type)))
6961 result_type = type;
6964 /* Shifts can be shortened if shifting right. */
6966 if (short_shift)
6968 int unsigned_arg;
6969 tree arg0 = get_narrower (op0, &unsigned_arg);
6971 final_type = result_type;
6973 if (arg0 == op0 && final_type == TREE_TYPE (op0))
6974 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
6976 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6977 /* We can shorten only if the shift count is less than the
6978 number of bits in the smaller type size. */
6979 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6980 /* We cannot drop an unsigned shift after sign-extension. */
6981 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
6983 /* Do an unsigned shift if the operand was zero-extended. */
6984 result_type
6985 = c_common_signed_or_unsigned_type (unsigned_arg,
6986 TREE_TYPE (arg0));
6987 /* Convert value-to-be-shifted to that type. */
6988 if (TREE_TYPE (op0) != result_type)
6989 op0 = convert (result_type, op0);
6990 converted = 1;
6994 /* Comparison operations are shortened too but differently.
6995 They identify themselves by setting short_compare = 1. */
6997 if (short_compare)
6999 /* Don't write &op0, etc., because that would prevent op0
7000 from being kept in a register.
7001 Instead, make copies of the our local variables and
7002 pass the copies by reference, then copy them back afterward. */
7003 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
7004 enum tree_code xresultcode = resultcode;
7005 tree val
7006 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
7008 if (val != 0)
7009 return val;
7011 op0 = xop0, op1 = xop1;
7012 converted = 1;
7013 resultcode = xresultcode;
7015 if (warn_sign_compare && skip_evaluation == 0)
7017 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
7018 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
7019 int unsignedp0, unsignedp1;
7020 tree primop0 = get_narrower (op0, &unsignedp0);
7021 tree primop1 = get_narrower (op1, &unsignedp1);
7023 xop0 = orig_op0;
7024 xop1 = orig_op1;
7025 STRIP_TYPE_NOPS (xop0);
7026 STRIP_TYPE_NOPS (xop1);
7028 /* Give warnings for comparisons between signed and unsigned
7029 quantities that may fail.
7031 Do the checking based on the original operand trees, so that
7032 casts will be considered, but default promotions won't be.
7034 Do not warn if the comparison is being done in a signed type,
7035 since the signed type will only be chosen if it can represent
7036 all the values of the unsigned type. */
7037 if (! TREE_UNSIGNED (result_type))
7038 /* OK */;
7039 /* Do not warn if both operands are the same signedness. */
7040 else if (op0_signed == op1_signed)
7041 /* OK */;
7042 else
7044 tree sop, uop;
7046 if (op0_signed)
7047 sop = xop0, uop = xop1;
7048 else
7049 sop = xop1, uop = xop0;
7051 /* Do not warn if the signed quantity is an
7052 unsuffixed integer literal (or some static
7053 constant expression involving such literals or a
7054 conditional expression involving such literals)
7055 and it is non-negative. */
7056 if (c_tree_expr_nonnegative_p (sop))
7057 /* OK */;
7058 /* Do not warn if the comparison is an equality operation,
7059 the unsigned quantity is an integral constant, and it
7060 would fit in the result if the result were signed. */
7061 else if (TREE_CODE (uop) == INTEGER_CST
7062 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
7063 && int_fits_type_p
7064 (uop, c_common_signed_type (result_type)))
7065 /* OK */;
7066 /* Do not warn if the unsigned quantity is an enumeration
7067 constant and its maximum value would fit in the result
7068 if the result were signed. */
7069 else if (TREE_CODE (uop) == INTEGER_CST
7070 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7071 && int_fits_type_p
7072 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7073 c_common_signed_type (result_type)))
7074 /* OK */;
7075 else
7076 warning ("comparison between signed and unsigned");
7079 /* Warn if two unsigned values are being compared in a size
7080 larger than their original size, and one (and only one) is the
7081 result of a `~' operator. This comparison will always fail.
7083 Also warn if one operand is a constant, and the constant
7084 does not have all bits set that are set in the ~ operand
7085 when it is extended. */
7087 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7088 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7090 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7091 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7092 &unsignedp0);
7093 else
7094 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7095 &unsignedp1);
7097 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7099 tree primop;
7100 HOST_WIDE_INT constant, mask;
7101 int unsignedp, bits;
7103 if (host_integerp (primop0, 0))
7105 primop = primop1;
7106 unsignedp = unsignedp1;
7107 constant = tree_low_cst (primop0, 0);
7109 else
7111 primop = primop0;
7112 unsignedp = unsignedp0;
7113 constant = tree_low_cst (primop1, 0);
7116 bits = TYPE_PRECISION (TREE_TYPE (primop));
7117 if (bits < TYPE_PRECISION (result_type)
7118 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7120 mask = (~ (HOST_WIDE_INT) 0) << bits;
7121 if ((mask & constant) != mask)
7122 warning ("comparison of promoted ~unsigned with constant");
7125 else if (unsignedp0 && unsignedp1
7126 && (TYPE_PRECISION (TREE_TYPE (primop0))
7127 < TYPE_PRECISION (result_type))
7128 && (TYPE_PRECISION (TREE_TYPE (primop1))
7129 < TYPE_PRECISION (result_type)))
7130 warning ("comparison of promoted ~unsigned with unsigned");
7136 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7137 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7138 Then the expression will be built.
7139 It will be given type FINAL_TYPE if that is nonzero;
7140 otherwise, it will be given type RESULT_TYPE. */
7142 if (!result_type)
7144 binary_op_error (code);
7145 return error_mark_node;
7148 if (! converted)
7150 if (TREE_TYPE (op0) != result_type)
7151 op0 = convert (result_type, op0);
7152 if (TREE_TYPE (op1) != result_type)
7153 op1 = convert (result_type, op1);
7156 if (build_type == NULL_TREE)
7157 build_type = result_type;
7160 tree result = build (resultcode, build_type, op0, op1);
7161 tree folded;
7163 /* Treat expressions in initializers specially as they can't trap. */
7164 folded = initializer_stack ? fold_initializer (result)
7165 : fold (result);
7166 if (folded == result)
7167 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
7168 if (final_type != 0)
7169 return convert (final_type, folded);
7170 return folded;