OSDN Git Service

New option -Wstrict-aliasing=2.
[pf3gnuchains/gcc-fork.git] / gcc / c-typeck.c
1 /* Build expressions with type checking for C compiler.
2    Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3    1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
22
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.
27
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).  */
31
32 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "rtl.h"
37 #include "tree.h"
38 #include "langhooks.h"
39 #include "c-tree.h"
40 #include "tm_p.h"
41 #include "flags.h"
42 #include "output.h"
43 #include "expr.h"
44 #include "toplev.h"
45 #include "intl.h"
46 #include "ggc.h"
47 #include "target.h"
48
49 /* Nonzero if we've already printed a "missing braces around initializer"
50    message within this initializer.  */
51 static int missing_braces_mentioned;
52
53 static tree qualify_type (tree, tree);
54 static int same_translation_unit_p (tree, tree);
55 static int tagged_types_tu_compatible_p (tree, tree, int);
56 static int comp_target_types (tree, tree, int);
57 static int function_types_compatible_p (tree, tree, int);
58 static int type_lists_compatible_p (tree, tree, int);
59 static tree decl_constant_value_for_broken_optimization (tree);
60 static tree default_function_array_conversion (tree);
61 static tree lookup_field (tree, tree);
62 static tree convert_arguments (tree, tree, tree, tree);
63 static tree pointer_diff (tree, tree);
64 static tree internal_build_compound_expr (tree, int);
65 static tree convert_for_assignment (tree, tree, const char *, tree, tree,
66                                     int);
67 static void warn_for_assignment (const char *, const char *, tree, int);
68 static tree valid_compound_expr_initializer (tree, tree);
69 static void push_string (const char *);
70 static void push_member_name (tree);
71 static void push_array_bounds (int);
72 static int spelling_length (void);
73 static char *print_spelling (char *);
74 static void warning_init (const char *);
75 static tree digest_init (tree, tree, int);
76 static void output_init_element (tree, tree, tree, int);
77 static void output_pending_init_elements (int);
78 static int set_designator (int);
79 static void push_range_stack (tree);
80 static void add_pending_init (tree, tree);
81 static void set_nonincremental_init (void);
82 static void set_nonincremental_init_from_string (tree);
83 static tree find_init_member (tree);
84 \f
85 /* Do `exp = require_complete_type (exp);' to make sure exp
86    does not have an incomplete type.  (That includes void types.)  */
87
88 tree
89 require_complete_type (tree value)
90 {
91   tree type = TREE_TYPE (value);
92
93   if (value == error_mark_node || type == error_mark_node)
94     return error_mark_node;
95
96   /* First, detect a valid value with a complete type.  */
97   if (COMPLETE_TYPE_P (type))
98     return value;
99
100   c_incomplete_type_error (value, type);
101   return error_mark_node;
102 }
103
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.  */
107
108 void
109 c_incomplete_type_error (tree value, tree type)
110 {
111   const char *type_code_string;
112
113   /* Avoid duplicate error message.  */
114   if (TREE_CODE (type) == ERROR_MARK)
115     return;
116
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
122     {
123     retry:
124       /* We must print an error message.  Be clever about what it says.  */
125
126       switch (TREE_CODE (type))
127         {
128         case RECORD_TYPE:
129           type_code_string = "struct";
130           break;
131
132         case UNION_TYPE:
133           type_code_string = "union";
134           break;
135
136         case ENUMERAL_TYPE:
137           type_code_string = "enum";
138           break;
139
140         case VOID_TYPE:
141           error ("invalid use of void expression");
142           return;
143
144         case ARRAY_TYPE:
145           if (TYPE_DOMAIN (type))
146             {
147               if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
148                 {
149                   error ("invalid use of flexible array member");
150                   return;
151                 }
152               type = TREE_TYPE (type);
153               goto retry;
154             }
155           error ("invalid use of array with unspecified bounds");
156           return;
157
158         default:
159           abort ();
160         }
161
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))));
169     }
170 }
171
172 /* Given a type, apply default promotions wrt unnamed function
173    arguments and return the new type.  */
174
175 tree
176 c_type_promotes_to (tree type)
177 {
178   if (TYPE_MAIN_VARIANT (type) == float_type_node)
179     return double_type_node;
180
181   if (c_promoting_integer_type_p (type))
182     {
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;
188     }
189
190   return type;
191 }
192
193 /* Return a variant of TYPE which has all the type qualifiers of LIKE
194    as well as those of TYPE.  */
195
196 static tree
197 qualify_type (tree type, tree like)
198 {
199   return c_build_qualified_type (type,
200                                  TYPE_QUALS (type) | TYPE_QUALS (like));
201 }
202 \f
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.
207
208    This is the type for the result of most arithmetic operations
209    if the operands have the given two types.  */
210
211 tree
212 common_type (tree t1, tree t2)
213 {
214   enum tree_code code1;
215   enum tree_code code2;
216   tree attributes;
217
218   /* Save time if the two types are the same.  */
219
220   if (t1 == t2) return t1;
221
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;
227
228   /* Merge the attributes.  */
229   attributes = (*targetm.merge_type_attributes) (t1, t2);
230
231   /* Treat an enum type as the unsigned integer type of the same width.  */
232
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);
237
238   code1 = TREE_CODE (t1);
239   code2 = TREE_CODE (t2);
240
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)
245     {
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);
249
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);
257     }
258
259   switch (code1)
260     {
261     case INTEGER_TYPE:
262     case REAL_TYPE:
263       /* If only one is real, use it as the result.  */
264
265       if (code1 == REAL_TYPE && code2 != REAL_TYPE)
266         return build_type_attribute_variant (t1, attributes);
267
268       if (code2 == REAL_TYPE && code1 != REAL_TYPE)
269         return build_type_attribute_variant (t2, attributes);
270
271       /* Both real or both integers; use the one with greater precision.  */
272
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);
277
278       /* Same precision.  Prefer longs to ints even when same size.  */
279
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);
284
285       if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
286           || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
287         {
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);
295         }
296
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);
302
303       /* Otherwise prefer the unsigned one.  */
304
305       if (TREE_UNSIGNED (t1))
306         return build_type_attribute_variant (t1, attributes);
307       else
308         return build_type_attribute_variant (t2, attributes);
309
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.  */
316       {
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);
326       }
327
328     case ARRAY_TYPE:
329       {
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);
339       }
340
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.  */
344       {
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;
351
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);
357
358         /* Simple way if one arg fails to specify argument types.  */
359         if (TYPE_ARG_TYPES (t1) == 0)
360          {
361            t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
362            return build_type_attribute_variant (t1, attributes);
363          }
364         if (TYPE_ARG_TYPES (t2) == 0)
365          {
366            t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
367            return build_type_attribute_variant (t1, attributes);
368          }
369
370         /* If both args specify argument types, we must merge the two
371            lists, argument by argument.  */
372
373         pushlevel (0);
374         declare_parm_level ();
375
376         len = list_length (p1);
377         newargs = 0;
378
379         for (i = 0; i < len; i++)
380           newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
381
382         n = newargs;
383
384         for (; p1;
385              p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
386           {
387             /* A null type means arg type is not specified.
388                Take whatever the other function type has.  */
389             if (TREE_VALUE (p1) == 0)
390               {
391                 TREE_VALUE (n) = TREE_VALUE (p2);
392                 goto parm_done;
393               }
394             if (TREE_VALUE (p2) == 0)
395               {
396                 TREE_VALUE (n) = TREE_VALUE (p1);
397                 goto parm_done;
398               }
399
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))
405               {
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))
411                     {
412                       TREE_VALUE (n) = TREE_VALUE (p2);
413                       if (pedantic)
414                         pedwarn ("function types not truly compatible in ISO C");
415                       goto parm_done;
416                     }
417               }
418             if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
419                 && TREE_VALUE (p2) != TREE_VALUE (p1))
420               {
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))
426                     {
427                       TREE_VALUE (n) = TREE_VALUE (p1);
428                       if (pedantic)
429                         pedwarn ("function types not truly compatible in ISO C");
430                       goto parm_done;
431                     }
432               }
433             TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
434           parm_done: ;
435           }
436
437         poplevel (0, 0, 0);
438
439         t1 = build_function_type (valtype, newargs);
440         /* ... falls through ...  */
441       }
442
443     default:
444       return build_type_attribute_variant (t1, attributes);
445     }
446
447 }
448 \f
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.  */
452
453 int
454 comptypes (tree type1, tree type2, int flags)
455 {
456   tree t1 = type1;
457   tree t2 = type2;
458   int attrval, val;
459
460   /* Suppress errors caused by previously reported errors.  */
461
462   if (t1 == t2 || !t1 || !t2
463       || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
464     return 1;
465
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);
471
472   if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
473       && TYPE_DOMAIN (t2) != 0)
474     t2 = TYPE_DOMAIN (t2);
475
476   /* Enumerated types are compatible with integer types, but this is
477      not transitive: two enumerated types in the same translation unit
478      are compatible with each other only if they are the same type.  */
479
480   if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
481     t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
482   else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
483     t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
484
485   if (t1 == t2)
486     return 1;
487
488   /* Different classes of types can't be compatible.  */
489
490   if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
491
492   /* Qualifiers must match.  */
493
494   if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
495     return 0;
496
497   /* Allow for two different type nodes which have essentially the same
498      definition.  Note that we already checked for equality of the type
499      qualifiers (just above).  */
500
501   if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
502     return 1;
503
504   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
505   if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
506      return 0;
507
508   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
509   val = 0;
510
511   switch (TREE_CODE (t1))
512     {
513     case POINTER_TYPE:
514       /* We must give ObjC the first crack at comparing pointers, since
515            protocol qualifiers may be involved.  */
516       if (c_dialect_objc () && (val = objc_comptypes (t1, t2, 0)) >= 0)
517         break;
518       val = (TREE_TYPE (t1) == TREE_TYPE (t2)
519              ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
520       break;
521
522     case FUNCTION_TYPE:
523       val = function_types_compatible_p (t1, t2, flags);
524       break;
525
526     case ARRAY_TYPE:
527       {
528         tree d1 = TYPE_DOMAIN (t1);
529         tree d2 = TYPE_DOMAIN (t2);
530         bool d1_variable, d2_variable;
531         bool d1_zero, d2_zero;
532         val = 1;
533
534         /* Target types must match incl. qualifiers.  */
535         if (TREE_TYPE (t1) != TREE_TYPE (t2)
536             && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
537                                       flags)))
538           return 0;
539
540         /* Sizes must match unless one is missing or variable.  */
541         if (d1 == 0 || d2 == 0 || d1 == d2)
542           break;
543
544         d1_zero = ! TYPE_MAX_VALUE (d1);
545         d2_zero = ! TYPE_MAX_VALUE (d2);
546
547         d1_variable = (! d1_zero
548                        && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
549                            || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
550         d2_variable = (! d2_zero
551                        && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
552                            || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
553
554         if (d1_variable || d2_variable)
555           break;
556         if (d1_zero && d2_zero)
557           break;
558         if (d1_zero || d2_zero
559             || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
560             || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
561           val = 0;
562
563         break;
564       }
565
566     case RECORD_TYPE:
567       /* We are dealing with two distinct structs.  In assorted Objective-C
568          corner cases, however, these can still be deemed equivalent.  */
569       if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
570         val = 1;
571
572     case ENUMERAL_TYPE:
573     case UNION_TYPE:
574       if (val != 1 && !same_translation_unit_p (t1, t2))
575         val = tagged_types_tu_compatible_p (t1, t2, flags);
576       break;
577
578     case VECTOR_TYPE:
579       /* The target might allow certain vector types to be compatible.  */
580       val = (*targetm.vector_opaque_p) (t1)
581         || (*targetm.vector_opaque_p) (t2);
582       break;
583
584     default:
585       break;
586     }
587   return attrval == 2 && val == 1 ? 2 : val;
588 }
589
590 /* Return 1 if TTL and TTR are pointers to types that are equivalent,
591    ignoring their qualifiers.  REFLEXIVE is only used by ObjC - set it
592    to 1 or 0 depending if the check of the pointer types is meant to
593    be reflexive or not (typically, assignments are not reflexive,
594    while comparisons are reflexive).
595 */
596
597 static int
598 comp_target_types (tree ttl, tree ttr, int reflexive)
599 {
600   int val;
601
602   /* Give objc_comptypes a crack at letting these types through.  */
603   if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
604     return val;
605
606   val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
607                    TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
608
609   if (val == 2 && pedantic)
610     pedwarn ("types are not quite compatible");
611   return val;
612 }
613 \f
614 /* Subroutines of `comptypes'.  */
615
616 /* Determine whether two types derive from the same translation unit.
617    If the CONTEXT chain ends in a null, that type's context is still
618    being parsed, so if two types have context chains ending in null,
619    they're in the same translation unit.  */
620 static int
621 same_translation_unit_p (tree t1, tree t2)
622 {
623   while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
624     switch (TREE_CODE_CLASS (TREE_CODE (t1)))
625       {
626       case 'd': t1 = DECL_CONTEXT (t1); break;
627       case 't': t1 = TYPE_CONTEXT (t1); break;
628       case 'b': t1 = BLOCK_SUPERCONTEXT (t1); break;
629       default: abort ();
630       }
631
632   while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
633     switch (TREE_CODE_CLASS (TREE_CODE (t2)))
634       {
635       case 'd': t2 = DECL_CONTEXT (t1); break;
636       case 't': t2 = TYPE_CONTEXT (t2); break;
637       case 'b': t2 = BLOCK_SUPERCONTEXT (t2); break;
638       default: abort ();
639       }
640
641   return t1 == t2;
642 }
643
644 /* The C standard says that two structures in different translation
645    units are compatible with each other only if the types of their
646    fields are compatible (among other things).  So, consider two copies
647    of this structure:  */
648
649 struct tagged_tu_seen {
650   const struct tagged_tu_seen * next;
651   tree t1;
652   tree t2;
653 };
654
655 /* Can they be compatible with each other?  We choose to break the
656    recursion by allowing those types to be compatible.  */
657
658 static const struct tagged_tu_seen * tagged_tu_seen_base;
659
660 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
661    compatible.  If the two types are not the same (which has been
662    checked earlier), this can only happen when multiple translation
663    units are being compiled.  See C99 6.2.7 paragraph 1 for the exact
664    rules.  */
665
666 static int
667 tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
668 {
669   tree s1, s2;
670   bool needs_warning = false;
671   
672   /* We have to verify that the tags of the types are the same.  This
673      is harder than it looks because this may be a typedef, so we have
674      to go look at the original type.  It may even be a typedef of a
675      typedef...  */
676   while (TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL)
677     t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
678
679   while (TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL)
680     t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
681
682   /* C90 didn't have the requirement that the two tags be the same.  */
683   if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
684     return 0;
685   
686   /* C90 didn't say what happened if one or both of the types were
687      incomplete; we choose to follow C99 rules here, which is that they
688      are compatible.  */
689   if (TYPE_SIZE (t1) == NULL
690       || TYPE_SIZE (t2) == NULL)
691     return 1;
692   
693   {
694     const struct tagged_tu_seen * tts_i;
695     for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
696       if (tts_i->t1 == t1 && tts_i->t2 == t2)
697         return 1;
698   }
699   
700   switch (TREE_CODE (t1))
701     {
702     case ENUMERAL_TYPE:
703       {
704       
705         /* Speed up the case where the type values are in the same order. */
706         tree tv1 = TYPE_VALUES (t1);
707         tree tv2 = TYPE_VALUES (t2);
708         
709         if (tv1 == tv2)
710           return 1;
711         
712         for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
713           {
714             if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
715               break;
716             if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
717               return 0;
718           }
719         
720         if (tv1 == NULL_TREE && tv2 == NULL_TREE)
721           return 1;
722         if (tv1 == NULL_TREE || tv2 == NULL_TREE)
723           return 0;
724         
725         if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
726           return 0;
727         
728         for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
729           {
730             s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
731             if (s2 == NULL
732                 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
733               return 0;
734           }
735         return 1;
736       }
737
738     case UNION_TYPE:
739       {
740         if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
741           return 0;
742
743         for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
744           {
745             bool ok = false;
746             struct tagged_tu_seen tts;
747
748             tts.next = tagged_tu_seen_base;
749             tts.t1 = t1;
750             tts.t2 = t2;
751             tagged_tu_seen_base = &tts;
752         
753             if (DECL_NAME (s1) != NULL)
754               for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
755                 if (DECL_NAME (s1) == DECL_NAME (s2))
756                   {
757                     int result;
758                     result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
759                     if (result == 0)
760                       break;
761                     if (result == 2)
762                       needs_warning = true;
763                     
764                     if (TREE_CODE (s1) == FIELD_DECL
765                         && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
766                                              DECL_FIELD_BIT_OFFSET (s2)) != 1)
767                       break;
768
769                     ok = true;
770                     break;
771                   }
772             tagged_tu_seen_base = tts.next;
773             if (! ok)
774               return 0;
775           }
776         return needs_warning ? 2 : 1;
777       }
778
779     case RECORD_TYPE:
780       {
781         struct tagged_tu_seen tts;
782         
783         tts.next = tagged_tu_seen_base;
784         tts.t1 = t1;
785         tts.t2 = t2;
786         tagged_tu_seen_base = &tts;
787           
788         for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); 
789              s1 && s2;
790              s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
791           {
792             int result;
793             if (TREE_CODE (s1) != TREE_CODE (s2)
794                 || DECL_NAME (s1) != DECL_NAME (s2))
795               break;
796             result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
797             if (result == 0)
798               break;
799             if (result == 2)
800               needs_warning = true;
801             
802             if (TREE_CODE (s1) == FIELD_DECL
803                 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
804                                      DECL_FIELD_BIT_OFFSET (s2)) != 1)
805               break;
806           }
807         tagged_tu_seen_base = tts.next;
808         if (s1 && s2)
809           return 0;
810         return needs_warning ? 2 : 1;
811       }
812
813     default:
814       abort ();
815     }
816 }
817
818 /* Return 1 if two function types F1 and F2 are compatible.
819    If either type specifies no argument types,
820    the other must specify a fixed number of self-promoting arg types.
821    Otherwise, if one type specifies only the number of arguments,
822    the other must specify that number of self-promoting arg types.
823    Otherwise, the argument types must match.  */
824
825 static int
826 function_types_compatible_p (tree f1, tree f2, int flags)
827 {
828   tree args1, args2;
829   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
830   int val = 1;
831   int val1;
832   tree ret1, ret2;
833
834   ret1 = TREE_TYPE (f1);
835   ret2 = TREE_TYPE (f2);
836
837   /* 'volatile' qualifiers on a function's return type mean the function
838      is noreturn.  */
839   if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
840     pedwarn ("function return types not compatible due to `volatile'");
841   if (TYPE_VOLATILE (ret1))
842     ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
843                                  TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
844   if (TYPE_VOLATILE (ret2))
845     ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
846                                  TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
847   val = comptypes (ret1, ret2, flags);
848   if (val == 0)
849     return 0;
850
851   args1 = TYPE_ARG_TYPES (f1);
852   args2 = TYPE_ARG_TYPES (f2);
853
854   /* An unspecified parmlist matches any specified parmlist
855      whose argument types don't need default promotions.  */
856
857   if (args1 == 0)
858     {
859       if (!self_promoting_args_p (args2))
860         return 0;
861       /* If one of these types comes from a non-prototype fn definition,
862          compare that with the other type's arglist.
863          If they don't match, ask for a warning (but no error).  */
864       if (TYPE_ACTUAL_ARG_TYPES (f1)
865           && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
866                                            flags))
867         val = 2;
868       return val;
869     }
870   if (args2 == 0)
871     {
872       if (!self_promoting_args_p (args1))
873         return 0;
874       if (TYPE_ACTUAL_ARG_TYPES (f2)
875           && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
876                                            flags))
877         val = 2;
878       return val;
879     }
880
881   /* Both types have argument lists: compare them and propagate results.  */
882   val1 = type_lists_compatible_p (args1, args2, flags);
883   return val1 != 1 ? val1 : val;
884 }
885
886 /* Check two lists of types for compatibility,
887    returning 0 for incompatible, 1 for compatible,
888    or 2 for compatible with warning.  */
889
890 static int
891 type_lists_compatible_p (tree args1, tree args2, int flags)
892 {
893   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
894   int val = 1;
895   int newval = 0;
896
897   while (1)
898     {
899       if (args1 == 0 && args2 == 0)
900         return val;
901       /* If one list is shorter than the other,
902          they fail to match.  */
903       if (args1 == 0 || args2 == 0)
904         return 0;
905       /* A null pointer instead of a type
906          means there is supposed to be an argument
907          but nothing is specified about what type it has.
908          So match anything that self-promotes.  */
909       if (TREE_VALUE (args1) == 0)
910         {
911           if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
912             return 0;
913         }
914       else if (TREE_VALUE (args2) == 0)
915         {
916           if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
917             return 0;
918         }
919       /* If one of the lists has an error marker, ignore this arg.  */
920       else if (TREE_CODE (TREE_VALUE (args1)) == ERROR_MARK
921                || TREE_CODE (TREE_VALUE (args2)) == ERROR_MARK)
922         ;
923       else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
924                                       TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
925                                       flags)))
926         {
927           /* Allow  wait (union {union wait *u; int *i} *)
928              and  wait (union wait *)  to be compatible.  */
929           if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
930               && (TYPE_NAME (TREE_VALUE (args1)) == 0
931                   || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
932               && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
933               && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
934                                      TYPE_SIZE (TREE_VALUE (args2))))
935             {
936               tree memb;
937               for (memb = TYPE_FIELDS (TREE_VALUE (args1));
938                    memb; memb = TREE_CHAIN (memb))
939                 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
940                                flags))
941                   break;
942               if (memb == 0)
943                 return 0;
944             }
945           else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
946                    && (TYPE_NAME (TREE_VALUE (args2)) == 0
947                        || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
948                    && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
949                    && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
950                                           TYPE_SIZE (TREE_VALUE (args1))))
951             {
952               tree memb;
953               for (memb = TYPE_FIELDS (TREE_VALUE (args2));
954                    memb; memb = TREE_CHAIN (memb))
955                 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
956                                flags))
957                   break;
958               if (memb == 0)
959                 return 0;
960             }
961           else
962             return 0;
963         }
964
965       /* comptypes said ok, but record if it said to warn.  */
966       if (newval > val)
967         val = newval;
968
969       args1 = TREE_CHAIN (args1);
970       args2 = TREE_CHAIN (args2);
971     }
972 }
973 \f
974 /* Compute the size to increment a pointer by.  */
975
976 tree
977 c_size_in_bytes (tree type)
978 {
979   enum tree_code code = TREE_CODE (type);
980
981   if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
982     return size_one_node;
983
984   if (!COMPLETE_OR_VOID_TYPE_P (type))
985     {
986       error ("arithmetic on pointer to an incomplete type");
987       return size_one_node;
988     }
989
990   /* Convert in case a char is more than one unit.  */
991   return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
992                      size_int (TYPE_PRECISION (char_type_node)
993                                / BITS_PER_UNIT));
994 }
995 \f
996 /* Return either DECL or its known constant value (if it has one).  */
997
998 tree
999 decl_constant_value (tree decl)
1000 {
1001   if (/* Don't change a variable array bound or initial value to a constant
1002          in a place where a variable is invalid.  */
1003       current_function_decl != 0
1004       && ! TREE_THIS_VOLATILE (decl)
1005       && TREE_READONLY (decl)
1006       && DECL_INITIAL (decl) != 0
1007       && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1008       /* This is invalid if initial value is not constant.
1009          If it has either a function call, a memory reference,
1010          or a variable, then re-evaluating it could give different results.  */
1011       && TREE_CONSTANT (DECL_INITIAL (decl))
1012       /* Check for cases where this is sub-optimal, even though valid.  */
1013       && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1014     return DECL_INITIAL (decl);
1015   return decl;
1016 }
1017
1018 /* Return either DECL or its known constant value (if it has one), but
1019    return DECL if pedantic or DECL has mode BLKmode.  This is for
1020    bug-compatibility with the old behavior of decl_constant_value
1021    (before GCC 3.0); every use of this function is a bug and it should
1022    be removed before GCC 3.1.  It is not appropriate to use pedantic
1023    in a way that affects optimization, and BLKmode is probably not the
1024    right test for avoiding misoptimizations either.  */
1025
1026 static tree
1027 decl_constant_value_for_broken_optimization (tree decl)
1028 {
1029   if (pedantic || DECL_MODE (decl) == BLKmode)
1030     return decl;
1031   else
1032     return decl_constant_value (decl);
1033 }
1034
1035
1036 /* Perform the default conversion of arrays and functions to pointers.
1037    Return the result of converting EXP.  For any other expression, just
1038    return EXP.  */
1039
1040 static tree
1041 default_function_array_conversion (tree exp)
1042 {
1043   tree orig_exp;
1044   tree type = TREE_TYPE (exp);
1045   enum tree_code code = TREE_CODE (type);
1046   int not_lvalue = 0;
1047
1048   /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1049      an lvalue.
1050
1051      Do not use STRIP_NOPS here!  It will remove conversions from pointer
1052      to integer and cause infinite recursion.  */
1053   orig_exp = exp;
1054   while (TREE_CODE (exp) == NON_LVALUE_EXPR
1055          || (TREE_CODE (exp) == NOP_EXPR
1056              && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1057     {
1058       if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1059         not_lvalue = 1;
1060       exp = TREE_OPERAND (exp, 0);
1061     }
1062
1063   /* Preserve the original expression code.  */
1064   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1065     C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1066
1067   if (code == FUNCTION_TYPE)
1068     {
1069       return build_unary_op (ADDR_EXPR, exp, 0);
1070     }
1071   if (code == ARRAY_TYPE)
1072     {
1073       tree adr;
1074       tree restype = TREE_TYPE (type);
1075       tree ptrtype;
1076       int constp = 0;
1077       int volatilep = 0;
1078       int lvalue_array_p;
1079
1080       if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1081         {
1082           constp = TREE_READONLY (exp);
1083           volatilep = TREE_THIS_VOLATILE (exp);
1084         }
1085
1086       if (TYPE_QUALS (type) || constp || volatilep)
1087         restype
1088           = c_build_qualified_type (restype,
1089                                     TYPE_QUALS (type)
1090                                     | (constp * TYPE_QUAL_CONST)
1091                                     | (volatilep * TYPE_QUAL_VOLATILE));
1092
1093       if (TREE_CODE (exp) == INDIRECT_REF)
1094         return convert (TYPE_POINTER_TO (restype),
1095                         TREE_OPERAND (exp, 0));
1096
1097       if (TREE_CODE (exp) == COMPOUND_EXPR)
1098         {
1099           tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1100           return build (COMPOUND_EXPR, TREE_TYPE (op1),
1101                         TREE_OPERAND (exp, 0), op1);
1102         }
1103
1104       lvalue_array_p = !not_lvalue && lvalue_p (exp);
1105       if (!flag_isoc99 && !lvalue_array_p)
1106         {
1107           /* Before C99, non-lvalue arrays do not decay to pointers.
1108              Normally, using such an array would be invalid; but it can
1109              be used correctly inside sizeof or as a statement expression.
1110              Thus, do not give an error here; an error will result later.  */
1111           return exp;
1112         }
1113
1114       ptrtype = build_pointer_type (restype);
1115
1116       if (TREE_CODE (exp) == VAR_DECL)
1117         {
1118           /* ??? This is not really quite correct
1119              in that the type of the operand of ADDR_EXPR
1120              is not the target type of the type of the ADDR_EXPR itself.
1121              Question is, can this lossage be avoided?  */
1122           adr = build1 (ADDR_EXPR, ptrtype, exp);
1123           if (!c_mark_addressable (exp))
1124             return error_mark_node;
1125           TREE_CONSTANT (adr) = staticp (exp);
1126           TREE_SIDE_EFFECTS (adr) = 0;   /* Default would be, same as EXP.  */
1127           return adr;
1128         }
1129       /* This way is better for a COMPONENT_REF since it can
1130          simplify the offset for a component.  */
1131       adr = build_unary_op (ADDR_EXPR, exp, 1);
1132       return convert (ptrtype, adr);
1133     }
1134   return exp;
1135 }
1136
1137 /* Perform default promotions for C data used in expressions.
1138    Arrays and functions are converted to pointers;
1139    enumeral types or short or char, to int.
1140    In addition, manifest constants symbols are replaced by their values.  */
1141
1142 tree
1143 default_conversion (tree exp)
1144 {
1145   tree orig_exp;
1146   tree type = TREE_TYPE (exp);
1147   enum tree_code code = TREE_CODE (type);
1148
1149   if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1150     return default_function_array_conversion (exp);
1151
1152   /* Constants can be used directly unless they're not loadable.  */
1153   if (TREE_CODE (exp) == CONST_DECL)
1154     exp = DECL_INITIAL (exp);
1155
1156   /* Replace a nonvolatile const static variable with its value unless
1157      it is an array, in which case we must be sure that taking the
1158      address of the array produces consistent results.  */
1159   else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
1160     {
1161       exp = decl_constant_value_for_broken_optimization (exp);
1162       type = TREE_TYPE (exp);
1163     }
1164
1165   /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
1166      an lvalue.
1167
1168      Do not use STRIP_NOPS here!  It will remove conversions from pointer
1169      to integer and cause infinite recursion.  */
1170   orig_exp = exp;
1171   while (TREE_CODE (exp) == NON_LVALUE_EXPR
1172          || (TREE_CODE (exp) == NOP_EXPR
1173              && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1174     exp = TREE_OPERAND (exp, 0);
1175
1176   /* Preserve the original expression code.  */
1177   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1178     C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1179
1180   /* Normally convert enums to int,
1181      but convert wide enums to something wider.  */
1182   if (code == ENUMERAL_TYPE)
1183     {
1184       type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1185                                           TYPE_PRECISION (integer_type_node)),
1186                                      ((TYPE_PRECISION (type)
1187                                        >= TYPE_PRECISION (integer_type_node))
1188                                       && TREE_UNSIGNED (type)));
1189
1190       return convert (type, exp);
1191     }
1192
1193   if (TREE_CODE (exp) == COMPONENT_REF
1194       && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1195       /* If it's thinner than an int, promote it like a
1196          c_promoting_integer_type_p, otherwise leave it alone.  */
1197       && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1198                                TYPE_PRECISION (integer_type_node)))
1199     return convert (integer_type_node, exp);
1200
1201   if (c_promoting_integer_type_p (type))
1202     {
1203       /* Preserve unsignedness if not really getting any wider.  */
1204       if (TREE_UNSIGNED (type)
1205           && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1206         return convert (unsigned_type_node, exp);
1207
1208       return convert (integer_type_node, exp);
1209     }
1210
1211   if (code == VOID_TYPE)
1212     {
1213       error ("void value not ignored as it ought to be");
1214       return error_mark_node;
1215     }
1216   return exp;
1217 }
1218 \f
1219 /* Look up COMPONENT in a structure or union DECL.
1220
1221    If the component name is not found, returns NULL_TREE.  Otherwise,
1222    the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1223    stepping down the chain to the component, which is in the last
1224    TREE_VALUE of the list.  Normally the list is of length one, but if
1225    the component is embedded within (nested) anonymous structures or
1226    unions, the list steps down the chain to the component.  */
1227
1228 static tree
1229 lookup_field (tree decl, tree component)
1230 {
1231   tree type = TREE_TYPE (decl);
1232   tree field;
1233
1234   /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1235      to the field elements.  Use a binary search on this array to quickly
1236      find the element.  Otherwise, do a linear search.  TYPE_LANG_SPECIFIC
1237      will always be set for structures which have many elements.  */
1238
1239   if (TYPE_LANG_SPECIFIC (type))
1240     {
1241       int bot, top, half;
1242       tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
1243
1244       field = TYPE_FIELDS (type);
1245       bot = 0;
1246       top = TYPE_LANG_SPECIFIC (type)->s->len;
1247       while (top - bot > 1)
1248         {
1249           half = (top - bot + 1) >> 1;
1250           field = field_array[bot+half];
1251
1252           if (DECL_NAME (field) == NULL_TREE)
1253             {
1254               /* Step through all anon unions in linear fashion.  */
1255               while (DECL_NAME (field_array[bot]) == NULL_TREE)
1256                 {
1257                   field = field_array[bot++];
1258                   if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1259                       || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1260                     {
1261                       tree anon = lookup_field (field, component);
1262
1263                       if (anon)
1264                         return tree_cons (NULL_TREE, field, anon);
1265                     }
1266                 }
1267
1268               /* Entire record is only anon unions.  */
1269               if (bot > top)
1270                 return NULL_TREE;
1271
1272               /* Restart the binary search, with new lower bound.  */
1273               continue;
1274             }
1275
1276           if (DECL_NAME (field) == component)
1277             break;
1278           if (DECL_NAME (field) < component)
1279             bot += half;
1280           else
1281             top = bot + half;
1282         }
1283
1284       if (DECL_NAME (field_array[bot]) == component)
1285         field = field_array[bot];
1286       else if (DECL_NAME (field) != component)
1287         return NULL_TREE;
1288     }
1289   else
1290     {
1291       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1292         {
1293           if (DECL_NAME (field) == NULL_TREE
1294               && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1295                   || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
1296             {
1297               tree anon = lookup_field (field, component);
1298
1299               if (anon)
1300                 return tree_cons (NULL_TREE, field, anon);
1301             }
1302
1303           if (DECL_NAME (field) == component)
1304             break;
1305         }
1306
1307       if (field == NULL_TREE)
1308         return NULL_TREE;
1309     }
1310
1311   return tree_cons (NULL_TREE, field, NULL_TREE);
1312 }
1313
1314 /* Make an expression to refer to the COMPONENT field of
1315    structure or union value DATUM.  COMPONENT is an IDENTIFIER_NODE.  */
1316
1317 tree
1318 build_component_ref (tree datum, tree component)
1319 {
1320   tree type = TREE_TYPE (datum);
1321   enum tree_code code = TREE_CODE (type);
1322   tree field = NULL;
1323   tree ref;
1324
1325   /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1326      Ensure that the arguments are not lvalues; otherwise,
1327      if the component is an array, it would wrongly decay to a pointer in
1328      C89 mode.
1329      We cannot do this with a COND_EXPR, because in a conditional expression
1330      the default promotions are applied to both sides, and this would yield
1331      the wrong type of the result; for example, if the components have
1332      type "char".  */
1333   switch (TREE_CODE (datum))
1334     {
1335     case COMPOUND_EXPR:
1336       {
1337         tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
1338         return build (COMPOUND_EXPR, TREE_TYPE (value),
1339                       TREE_OPERAND (datum, 0), non_lvalue (value));
1340       }
1341     default:
1342       break;
1343     }
1344
1345   /* See if there is a field or component with name COMPONENT.  */
1346
1347   if (code == RECORD_TYPE || code == UNION_TYPE)
1348     {
1349       if (!COMPLETE_TYPE_P (type))
1350         {
1351           c_incomplete_type_error (NULL_TREE, type);
1352           return error_mark_node;
1353         }
1354
1355       field = lookup_field (datum, component);
1356
1357       if (!field)
1358         {
1359           error ("%s has no member named `%s'",
1360                  code == RECORD_TYPE ? "structure" : "union",
1361                  IDENTIFIER_POINTER (component));
1362           return error_mark_node;
1363         }
1364
1365       /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1366          This might be better solved in future the way the C++ front
1367          end does it - by giving the anonymous entities each a
1368          separate name and type, and then have build_component_ref
1369          recursively call itself.  We can't do that here.  */
1370       do
1371         {
1372           tree subdatum = TREE_VALUE (field);
1373
1374           if (TREE_TYPE (subdatum) == error_mark_node)
1375             return error_mark_node;
1376
1377           ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1378           if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
1379             TREE_READONLY (ref) = 1;
1380           if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
1381             TREE_THIS_VOLATILE (ref) = 1;
1382
1383           if (TREE_DEPRECATED (subdatum))
1384             warn_deprecated_use (subdatum);
1385
1386           datum = ref;
1387
1388           field = TREE_CHAIN (field);
1389         }
1390       while (field);
1391
1392       return ref;
1393     }
1394   else if (code != ERROR_MARK)
1395     error ("request for member `%s' in something not a structure or union",
1396             IDENTIFIER_POINTER (component));
1397
1398   return error_mark_node;
1399 }
1400 \f
1401 /* Given an expression PTR for a pointer, return an expression
1402    for the value pointed to.
1403    ERRORSTRING is the name of the operator to appear in error messages.  */
1404
1405 tree
1406 build_indirect_ref (tree ptr, const char *errorstring)
1407 {
1408   tree pointer = default_conversion (ptr);
1409   tree type = TREE_TYPE (pointer);
1410
1411   if (TREE_CODE (type) == POINTER_TYPE)
1412     {
1413       if (TREE_CODE (pointer) == ADDR_EXPR
1414           && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1415               == TREE_TYPE (type)))
1416         return TREE_OPERAND (pointer, 0);
1417       else
1418         {
1419           tree t = TREE_TYPE (type);
1420           tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
1421
1422           if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
1423             {
1424               error ("dereferencing pointer to incomplete type");
1425               return error_mark_node;
1426             }
1427           if (VOID_TYPE_P (t) && skip_evaluation == 0)
1428             warning ("dereferencing `void *' pointer");
1429
1430           /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1431              so that we get the proper error message if the result is used
1432              to assign to.  Also, &* is supposed to be a no-op.
1433              And ANSI C seems to specify that the type of the result
1434              should be the const type.  */
1435           /* A de-reference of a pointer to const is not a const.  It is valid
1436              to change it via some other pointer.  */
1437           TREE_READONLY (ref) = TYPE_READONLY (t);
1438           TREE_SIDE_EFFECTS (ref)
1439             = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
1440           TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
1441           return ref;
1442         }
1443     }
1444   else if (TREE_CODE (pointer) != ERROR_MARK)
1445     error ("invalid type argument of `%s'", errorstring);
1446   return error_mark_node;
1447 }
1448
1449 /* This handles expressions of the form "a[i]", which denotes
1450    an array reference.
1451
1452    This is logically equivalent in C to *(a+i), but we may do it differently.
1453    If A is a variable or a member, we generate a primitive ARRAY_REF.
1454    This avoids forcing the array out of registers, and can work on
1455    arrays that are not lvalues (for example, members of structures returned
1456    by functions).  */
1457
1458 tree
1459 build_array_ref (tree array, tree index)
1460 {
1461   if (index == 0)
1462     {
1463       error ("subscript missing in array reference");
1464       return error_mark_node;
1465     }
1466
1467   if (TREE_TYPE (array) == error_mark_node
1468       || TREE_TYPE (index) == error_mark_node)
1469     return error_mark_node;
1470
1471   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1472       && TREE_CODE (array) != INDIRECT_REF)
1473     {
1474       tree rval, type;
1475
1476       /* Subscripting with type char is likely to lose
1477          on a machine where chars are signed.
1478          So warn on any machine, but optionally.
1479          Don't warn for unsigned char since that type is safe.
1480          Don't warn for signed char because anyone who uses that
1481          must have done so deliberately.  */
1482       if (warn_char_subscripts
1483           && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1484         warning ("array subscript has type `char'");
1485
1486       /* Apply default promotions *after* noticing character types.  */
1487       index = default_conversion (index);
1488
1489       /* Require integer *after* promotion, for sake of enums.  */
1490       if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1491         {
1492           error ("array subscript is not an integer");
1493           return error_mark_node;
1494         }
1495
1496       /* An array that is indexed by a non-constant
1497          cannot be stored in a register; we must be able to do
1498          address arithmetic on its address.
1499          Likewise an array of elements of variable size.  */
1500       if (TREE_CODE (index) != INTEGER_CST
1501           || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
1502               && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1503         {
1504           if (!c_mark_addressable (array))
1505             return error_mark_node;
1506         }
1507       /* An array that is indexed by a constant value which is not within
1508          the array bounds cannot be stored in a register either; because we
1509          would get a crash in store_bit_field/extract_bit_field when trying
1510          to access a non-existent part of the register.  */
1511       if (TREE_CODE (index) == INTEGER_CST
1512           && TYPE_VALUES (TREE_TYPE (array))
1513           && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1514         {
1515           if (!c_mark_addressable (array))
1516             return error_mark_node;
1517         }
1518
1519       if (pedantic)
1520         {
1521           tree foo = array;
1522           while (TREE_CODE (foo) == COMPONENT_REF)
1523             foo = TREE_OPERAND (foo, 0);
1524           if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
1525             pedwarn ("ISO C forbids subscripting `register' array");
1526           else if (! flag_isoc99 && ! lvalue_p (foo))
1527             pedwarn ("ISO C90 forbids subscripting non-lvalue array");
1528         }
1529
1530       type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1531       rval = build (ARRAY_REF, type, array, index);
1532       /* Array ref is const/volatile if the array elements are
1533          or if the array is.  */
1534       TREE_READONLY (rval)
1535         |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1536             | TREE_READONLY (array));
1537       TREE_SIDE_EFFECTS (rval)
1538         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1539             | TREE_SIDE_EFFECTS (array));
1540       TREE_THIS_VOLATILE (rval)
1541         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1542             /* This was added by rms on 16 Nov 91.
1543                It fixes  vol struct foo *a;  a->elts[1]
1544                in an inline function.
1545                Hope it doesn't break something else.  */
1546             | TREE_THIS_VOLATILE (array));
1547       return require_complete_type (fold (rval));
1548     }
1549
1550   {
1551     tree ar = default_conversion (array);
1552     tree ind = default_conversion (index);
1553
1554     /* Do the same warning check as above, but only on the part that's
1555        syntactically the index and only if it is also semantically
1556        the index.  */
1557     if (warn_char_subscripts
1558         && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1559         && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1560       warning ("subscript has type `char'");
1561
1562     /* Put the integer in IND to simplify error checking.  */
1563     if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1564       {
1565         tree temp = ar;
1566         ar = ind;
1567         ind = temp;
1568       }
1569
1570     if (ar == error_mark_node)
1571       return ar;
1572
1573     if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1574         || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
1575       {
1576         error ("subscripted value is neither array nor pointer");
1577         return error_mark_node;
1578       }
1579     if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1580       {
1581         error ("array subscript is not an integer");
1582         return error_mark_node;
1583       }
1584
1585     return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1586                                "array indexing");
1587   }
1588 }
1589 \f
1590 /* Build an external reference to identifier ID.  FUN indicates
1591    whether this will be used for a function call.  */
1592 tree
1593 build_external_ref (tree id, int fun)
1594 {
1595   tree ref;
1596   tree decl = lookup_name (id);
1597   tree objc_ivar = lookup_objc_ivar (id);
1598
1599   if (decl && decl != error_mark_node)
1600     {
1601       /* Properly declared variable or function reference.  */
1602       if (!objc_ivar)
1603         ref = decl;
1604       else if (decl != objc_ivar && !DECL_FILE_SCOPE_P (decl))
1605         {
1606           warning ("local declaration of `%s' hides instance variable",
1607                    IDENTIFIER_POINTER (id));
1608           ref = decl;
1609         }
1610       else
1611         ref = objc_ivar;
1612     }
1613   else if (objc_ivar)
1614     ref = objc_ivar;
1615   else if (fun)
1616     /* Implicit function declaration.  */
1617     ref = implicitly_declare (id);
1618   else if (decl == error_mark_node)
1619     /* Don't complain about something that's already been
1620        complained about.  */
1621     return error_mark_node;
1622   else
1623     {
1624       undeclared_variable (id);
1625       return error_mark_node;
1626     }
1627
1628   if (TREE_TYPE (ref) == error_mark_node)
1629     return error_mark_node;
1630
1631   if (TREE_DEPRECATED (ref))
1632     warn_deprecated_use (ref);
1633
1634   if (!skip_evaluation)
1635     assemble_external (ref);
1636   TREE_USED (ref) = 1;
1637
1638   if (TREE_CODE (ref) == CONST_DECL)
1639     {
1640       ref = DECL_INITIAL (ref);
1641       TREE_CONSTANT (ref) = 1;
1642     }
1643   else if (current_function_decl != 0
1644            && !DECL_FILE_SCOPE_P (current_function_decl)
1645            && (TREE_CODE (ref) == VAR_DECL
1646                || TREE_CODE (ref) == PARM_DECL
1647                || TREE_CODE (ref) == FUNCTION_DECL))
1648     {
1649       tree context = decl_function_context (ref);
1650
1651       if (context != 0 && context != current_function_decl)
1652         DECL_NONLOCAL (ref) = 1;
1653     }
1654
1655   return ref;
1656 }
1657
1658 /* Build a function call to function FUNCTION with parameters PARAMS.
1659    PARAMS is a list--a chain of TREE_LIST nodes--in which the
1660    TREE_VALUE of each node is a parameter-expression.
1661    FUNCTION's data type may be a function type or a pointer-to-function.  */
1662
1663 tree
1664 build_function_call (tree function, tree params)
1665 {
1666   tree fntype, fundecl = 0;
1667   tree coerced_params;
1668   tree name = NULL_TREE, result;
1669   tree tem;
1670
1671   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
1672   STRIP_TYPE_NOPS (function);
1673
1674   /* Convert anything with function type to a pointer-to-function.  */
1675   if (TREE_CODE (function) == FUNCTION_DECL)
1676     {
1677       name = DECL_NAME (function);
1678
1679       /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1680          (because calling an inline function does not mean the function
1681          needs to be separately compiled).  */
1682       fntype = build_type_variant (TREE_TYPE (function),
1683                                    TREE_READONLY (function),
1684                                    TREE_THIS_VOLATILE (function));
1685       fundecl = function;
1686       function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1687     }
1688   else
1689     function = default_conversion (function);
1690
1691   fntype = TREE_TYPE (function);
1692
1693   if (TREE_CODE (fntype) == ERROR_MARK)
1694     return error_mark_node;
1695
1696   if (!(TREE_CODE (fntype) == POINTER_TYPE
1697         && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1698     {
1699       error ("called object is not a function");
1700       return error_mark_node;
1701     }
1702
1703   if (fundecl && TREE_THIS_VOLATILE (fundecl))
1704     current_function_returns_abnormally = 1;
1705
1706   /* fntype now gets the type of function pointed to.  */
1707   fntype = TREE_TYPE (fntype);
1708
1709   /* Check that the function is called through a compatible prototype.
1710      If it is not, replace the call by a trap, wrapped up in a compound
1711      expression if necessary.  This has the nice side-effect to prevent
1712      the tree-inliner from generating invalid assignment trees which may
1713      blow up in the RTL expander later.
1714
1715      ??? This doesn't work for Objective-C because objc_comptypes
1716      refuses to compare function prototypes, yet the compiler appears
1717      to build calls that are flagged as invalid by C's comptypes.  */
1718   if (! c_dialect_objc ()
1719       && TREE_CODE (function) == NOP_EXPR
1720       && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
1721       && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
1722       && ! comptypes (fntype, TREE_TYPE (tem), COMPARE_STRICT))
1723     {
1724       tree return_type = TREE_TYPE (fntype);
1725       tree trap = build_function_call (built_in_decls[BUILT_IN_TRAP],
1726                                        NULL_TREE);
1727
1728       /* This situation leads to run-time undefined behavior.  We can't,
1729          therefore, simply error unless we can prove that all possible
1730          executions of the program must execute the code.  */
1731       warning ("function called through a non-compatible type");
1732
1733       /* We can, however, treat "undefined" any way we please.
1734          Call abort to encourage the user to fix the program.  */
1735       inform ("if this code is reached, the program will abort");
1736
1737       if (VOID_TYPE_P (return_type))
1738         return trap;
1739       else
1740         {
1741           tree rhs;
1742
1743           if (AGGREGATE_TYPE_P (return_type))
1744             rhs = build_compound_literal (return_type,
1745                                           build_constructor (return_type,
1746                                                              NULL_TREE));
1747           else
1748             rhs = fold (build1 (NOP_EXPR, return_type, integer_zero_node));
1749
1750           return build (COMPOUND_EXPR, return_type, trap, rhs);
1751         }
1752     }
1753
1754   /* Convert the parameters to the types declared in the
1755      function prototype, or apply default promotions.  */
1756
1757   coerced_params
1758     = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
1759
1760   /* Check that the arguments to the function are valid.  */
1761
1762   check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
1763
1764   /* Recognize certain built-in functions so we can make tree-codes
1765      other than CALL_EXPR.  We do this when it enables fold-const.c
1766      to do something useful.  */
1767
1768   if (TREE_CODE (function) == ADDR_EXPR
1769       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1770       && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1771     {
1772       result = expand_tree_builtin (TREE_OPERAND (function, 0),
1773                                     params, coerced_params);
1774       if (result)
1775         return result;
1776     }
1777
1778   result = build (CALL_EXPR, TREE_TYPE (fntype),
1779                   function, coerced_params, NULL_TREE);
1780   TREE_SIDE_EFFECTS (result) = 1;
1781   result = fold (result);
1782
1783   if (VOID_TYPE_P (TREE_TYPE (result)))
1784     return result;
1785   return require_complete_type (result);
1786 }
1787 \f
1788 /* Convert the argument expressions in the list VALUES
1789    to the types in the list TYPELIST.  The result is a list of converted
1790    argument expressions.
1791
1792    If TYPELIST is exhausted, or when an element has NULL as its type,
1793    perform the default conversions.
1794
1795    PARMLIST is the chain of parm decls for the function being called.
1796    It may be 0, if that info is not available.
1797    It is used only for generating error messages.
1798
1799    NAME is an IDENTIFIER_NODE or 0.  It is used only for error messages.
1800
1801    This is also where warnings about wrong number of args are generated.
1802
1803    Both VALUES and the returned value are chains of TREE_LIST nodes
1804    with the elements of the list in the TREE_VALUE slots of those nodes.  */
1805
1806 static tree
1807 convert_arguments (tree typelist, tree values, tree name, tree fundecl)
1808 {
1809   tree typetail, valtail;
1810   tree result = NULL;
1811   int parmnum;
1812
1813   /* Scan the given expressions and types, producing individual
1814      converted arguments and pushing them on RESULT in reverse order.  */
1815
1816   for (valtail = values, typetail = typelist, parmnum = 0;
1817        valtail;
1818        valtail = TREE_CHAIN (valtail), parmnum++)
1819     {
1820       tree type = typetail ? TREE_VALUE (typetail) : 0;
1821       tree val = TREE_VALUE (valtail);
1822
1823       if (type == void_type_node)
1824         {
1825           if (name)
1826             error ("too many arguments to function `%s'",
1827                    IDENTIFIER_POINTER (name));
1828           else
1829             error ("too many arguments to function");
1830           break;
1831         }
1832
1833       /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
1834       /* Do not use STRIP_NOPS here!  We do not want an enumerator with value 0
1835          to convert automatically to a pointer.  */
1836       if (TREE_CODE (val) == NON_LVALUE_EXPR)
1837         val = TREE_OPERAND (val, 0);
1838
1839       val = default_function_array_conversion (val);
1840
1841       val = require_complete_type (val);
1842
1843       if (type != 0)
1844         {
1845           /* Formal parm type is specified by a function prototype.  */
1846           tree parmval;
1847
1848           if (!COMPLETE_TYPE_P (type))
1849             {
1850               error ("type of formal parameter %d is incomplete", parmnum + 1);
1851               parmval = val;
1852             }
1853           else
1854             {
1855               /* Optionally warn about conversions that
1856                  differ from the default conversions.  */
1857               if (warn_conversion || warn_traditional)
1858                 {
1859                   int formal_prec = TYPE_PRECISION (type);
1860
1861                   if (INTEGRAL_TYPE_P (type)
1862                       && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1863                     warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1864                   if (INTEGRAL_TYPE_P (type)
1865                       && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1866                     warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1867                   else if (TREE_CODE (type) == COMPLEX_TYPE
1868                            && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1869                     warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
1870                   else if (TREE_CODE (type) == REAL_TYPE
1871                            && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1872                     warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1873                   else if (TREE_CODE (type) == COMPLEX_TYPE
1874                            && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1875                     warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
1876                   else if (TREE_CODE (type) == REAL_TYPE
1877                            && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1878                     warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1879                   /* ??? At some point, messages should be written about
1880                      conversions between complex types, but that's too messy
1881                      to do now.  */
1882                   else if (TREE_CODE (type) == REAL_TYPE
1883                            && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1884                     {
1885                       /* Warn if any argument is passed as `float',
1886                          since without a prototype it would be `double'.  */
1887                       if (formal_prec == TYPE_PRECISION (float_type_node))
1888                         warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
1889                     }
1890                   /* Detect integer changing in width or signedness.
1891                      These warnings are only activated with
1892                      -Wconversion, not with -Wtraditional.  */
1893                   else if (warn_conversion && INTEGRAL_TYPE_P (type)
1894                            && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1895                     {
1896                       tree would_have_been = default_conversion (val);
1897                       tree type1 = TREE_TYPE (would_have_been);
1898
1899                       if (TREE_CODE (type) == ENUMERAL_TYPE
1900                           && (TYPE_MAIN_VARIANT (type)
1901                               == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
1902                         /* No warning if function asks for enum
1903                            and the actual arg is that enum type.  */
1904                         ;
1905                       else if (formal_prec != TYPE_PRECISION (type1))
1906                         warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
1907                       else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1908                         ;
1909                       /* Don't complain if the formal parameter type
1910                          is an enum, because we can't tell now whether
1911                          the value was an enum--even the same enum.  */
1912                       else if (TREE_CODE (type) == ENUMERAL_TYPE)
1913                         ;
1914                       else if (TREE_CODE (val) == INTEGER_CST
1915                                && int_fits_type_p (val, type))
1916                         /* Change in signedness doesn't matter
1917                            if a constant value is unaffected.  */
1918                         ;
1919                       /* Likewise for a constant in a NOP_EXPR.  */
1920                       else if (TREE_CODE (val) == NOP_EXPR
1921                                && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1922                                && int_fits_type_p (TREE_OPERAND (val, 0), type))
1923                         ;
1924                       /* If the value is extended from a narrower
1925                          unsigned type, it doesn't matter whether we
1926                          pass it as signed or unsigned; the value
1927                          certainly is the same either way.  */
1928                       else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1929                                && TREE_UNSIGNED (TREE_TYPE (val)))
1930                         ;
1931                       else if (TREE_UNSIGNED (type))
1932                         warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1933                       else
1934                         warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
1935                     }
1936                 }
1937
1938               parmval = convert_for_assignment (type, val,
1939                                                 (char *) 0, /* arg passing  */
1940                                                 fundecl, name, parmnum + 1);
1941
1942               if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
1943                   && INTEGRAL_TYPE_P (type)
1944                   && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1945                 parmval = default_conversion (parmval);
1946             }
1947           result = tree_cons (NULL_TREE, parmval, result);
1948         }
1949       else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1950                && (TYPE_PRECISION (TREE_TYPE (val))
1951                    < TYPE_PRECISION (double_type_node)))
1952         /* Convert `float' to `double'.  */
1953         result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1954       else
1955         /* Convert `short' and `char' to full-size `int'.  */
1956         result = tree_cons (NULL_TREE, default_conversion (val), result);
1957
1958       if (typetail)
1959         typetail = TREE_CHAIN (typetail);
1960     }
1961
1962   if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1963     {
1964       if (name)
1965         error ("too few arguments to function `%s'",
1966                IDENTIFIER_POINTER (name));
1967       else
1968         error ("too few arguments to function");
1969     }
1970
1971   return nreverse (result);
1972 }
1973 \f
1974 /* This is the entry point used by the parser
1975    for binary operators in the input.
1976    In addition to constructing the expression,
1977    we check for operands that were written with other binary operators
1978    in a way that is likely to confuse the user.  */
1979
1980 tree
1981 parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
1982 {
1983   tree result = build_binary_op (code, arg1, arg2, 1);
1984
1985   char class;
1986   char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1987   char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1988   enum tree_code code1 = ERROR_MARK;
1989   enum tree_code code2 = ERROR_MARK;
1990
1991   if (TREE_CODE (result) == ERROR_MARK)
1992     return error_mark_node;
1993
1994   if (IS_EXPR_CODE_CLASS (class1))
1995     code1 = C_EXP_ORIGINAL_CODE (arg1);
1996   if (IS_EXPR_CODE_CLASS (class2))
1997     code2 = C_EXP_ORIGINAL_CODE (arg2);
1998
1999   /* Check for cases such as x+y<<z which users are likely
2000      to misinterpret.  If parens are used, C_EXP_ORIGINAL_CODE
2001      is cleared to prevent these warnings.  */
2002   if (warn_parentheses)
2003     {
2004       if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
2005         {
2006           if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2007               || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2008             warning ("suggest parentheses around + or - inside shift");
2009         }
2010
2011       if (code == TRUTH_ORIF_EXPR)
2012         {
2013           if (code1 == TRUTH_ANDIF_EXPR
2014               || code2 == TRUTH_ANDIF_EXPR)
2015             warning ("suggest parentheses around && within ||");
2016         }
2017
2018       if (code == BIT_IOR_EXPR)
2019         {
2020           if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
2021               || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2022               || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
2023               || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2024             warning ("suggest parentheses around arithmetic in operand of |");
2025           /* Check cases like x|y==z */
2026           if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2027             warning ("suggest parentheses around comparison in operand of |");
2028         }
2029
2030       if (code == BIT_XOR_EXPR)
2031         {
2032           if (code1 == BIT_AND_EXPR
2033               || code1 == PLUS_EXPR || code1 == MINUS_EXPR
2034               || code2 == BIT_AND_EXPR
2035               || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2036             warning ("suggest parentheses around arithmetic in operand of ^");
2037           /* Check cases like x^y==z */
2038           if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2039             warning ("suggest parentheses around comparison in operand of ^");
2040         }
2041
2042       if (code == BIT_AND_EXPR)
2043         {
2044           if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
2045               || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
2046             warning ("suggest parentheses around + or - in operand of &");
2047           /* Check cases like x&y==z */
2048           if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
2049             warning ("suggest parentheses around comparison in operand of &");
2050         }
2051     }
2052
2053   /* Similarly, check for cases like 1<=i<=10 that are probably errors.  */
2054   if (TREE_CODE_CLASS (code) == '<' && extra_warnings
2055       && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
2056     warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
2057
2058   unsigned_conversion_warning (result, arg1);
2059   unsigned_conversion_warning (result, arg2);
2060   overflow_warning (result);
2061
2062   class = TREE_CODE_CLASS (TREE_CODE (result));
2063
2064   /* Record the code that was specified in the source,
2065      for the sake of warnings about confusing nesting.  */
2066   if (IS_EXPR_CODE_CLASS (class))
2067     C_SET_EXP_ORIGINAL_CODE (result, code);
2068   else
2069     {
2070       int flag = TREE_CONSTANT (result);
2071       /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2072          so that convert_for_assignment wouldn't strip it.
2073          That way, we got warnings for things like p = (1 - 1).
2074          But it turns out we should not get those warnings.  */
2075       result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
2076       C_SET_EXP_ORIGINAL_CODE (result, code);
2077       TREE_CONSTANT (result) = flag;
2078     }
2079
2080   return result;
2081 }
2082 \f
2083
2084 /* Return true if `t' is known to be non-negative.  */
2085
2086 int
2087 c_tree_expr_nonnegative_p (tree t)
2088 {
2089   if (TREE_CODE (t) == STMT_EXPR)
2090     {
2091       t = COMPOUND_BODY (STMT_EXPR_STMT (t));
2092
2093       /* Find the last statement in the chain, ignoring the final
2094              * scope statement */
2095       while (TREE_CHAIN (t) != NULL_TREE
2096              && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
2097         t = TREE_CHAIN (t);
2098       return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2099     }
2100   return tree_expr_nonnegative_p (t);
2101 }
2102
2103 /* Return a tree for the difference of pointers OP0 and OP1.
2104    The resulting tree has type int.  */
2105
2106 static tree
2107 pointer_diff (tree op0, tree op1)
2108 {
2109   tree result, folded;
2110   tree restype = ptrdiff_type_node;
2111
2112   tree target_type = TREE_TYPE (TREE_TYPE (op0));
2113   tree con0, con1, lit0, lit1;
2114   tree orig_op1 = op1;
2115
2116   if (pedantic || warn_pointer_arith)
2117     {
2118       if (TREE_CODE (target_type) == VOID_TYPE)
2119         pedwarn ("pointer of type `void *' used in subtraction");
2120       if (TREE_CODE (target_type) == FUNCTION_TYPE)
2121         pedwarn ("pointer to a function used in subtraction");
2122     }
2123
2124   /* If the conversion to ptrdiff_type does anything like widening or
2125      converting a partial to an integral mode, we get a convert_expression
2126      that is in the way to do any simplifications.
2127      (fold-const.c doesn't know that the extra bits won't be needed.
2128      split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2129      different mode in place.)
2130      So first try to find a common term here 'by hand'; we want to cover
2131      at least the cases that occur in legal static initializers.  */
2132   con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2133   con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2134
2135   if (TREE_CODE (con0) == PLUS_EXPR)
2136     {
2137       lit0 = TREE_OPERAND (con0, 1);
2138       con0 = TREE_OPERAND (con0, 0);
2139     }
2140   else
2141     lit0 = integer_zero_node;
2142
2143   if (TREE_CODE (con1) == PLUS_EXPR)
2144     {
2145       lit1 = TREE_OPERAND (con1, 1);
2146       con1 = TREE_OPERAND (con1, 0);
2147     }
2148   else
2149     lit1 = integer_zero_node;
2150
2151   if (operand_equal_p (con0, con1, 0))
2152     {
2153       op0 = lit0;
2154       op1 = lit1;
2155     }
2156
2157
2158   /* First do the subtraction as integers;
2159      then drop through to build the divide operator.
2160      Do not do default conversions on the minus operator
2161      in case restype is a short type.  */
2162
2163   op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
2164                          convert (restype, op1), 0);
2165   /* This generates an error if op1 is pointer to incomplete type.  */
2166   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
2167     error ("arithmetic on pointer to an incomplete type");
2168
2169   /* This generates an error if op0 is pointer to incomplete type.  */
2170   op1 = c_size_in_bytes (target_type);
2171
2172   /* Divide by the size, in easiest possible way.  */
2173
2174   result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
2175
2176   folded = fold (result);
2177   if (folded == result)
2178     TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2179   return folded;
2180 }
2181 \f
2182 /* Construct and perhaps optimize a tree representation
2183    for a unary operation.  CODE, a tree_code, specifies the operation
2184    and XARG is the operand.
2185    For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2186    the default promotions (such as from short to int).
2187    For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2188    allows non-lvalues; this is only used to handle conversion of non-lvalue
2189    arrays to pointers in C99.  */
2190
2191 tree
2192 build_unary_op (enum tree_code code, tree xarg, int flag)
2193 {
2194   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
2195   tree arg = xarg;
2196   tree argtype = 0;
2197   enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
2198   tree val;
2199   int noconvert = flag;
2200
2201   if (typecode == ERROR_MARK)
2202     return error_mark_node;
2203   if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
2204     typecode = INTEGER_TYPE;
2205
2206   switch (code)
2207     {
2208     case CONVERT_EXPR:
2209       /* This is used for unary plus, because a CONVERT_EXPR
2210          is enough to prevent anybody from looking inside for
2211          associativity, but won't generate any code.  */
2212       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2213             || typecode == COMPLEX_TYPE))
2214         {
2215           error ("wrong type argument to unary plus");
2216           return error_mark_node;
2217         }
2218       else if (!noconvert)
2219         arg = default_conversion (arg);
2220       arg = non_lvalue (arg);
2221       break;
2222
2223     case NEGATE_EXPR:
2224       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2225             || typecode == COMPLEX_TYPE
2226             || typecode == VECTOR_TYPE))
2227         {
2228           error ("wrong type argument to unary minus");
2229           return error_mark_node;
2230         }
2231       else if (!noconvert)
2232         arg = default_conversion (arg);
2233       break;
2234
2235     case BIT_NOT_EXPR:
2236       if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2237         {
2238           if (!noconvert)
2239             arg = default_conversion (arg);
2240         }
2241       else if (typecode == COMPLEX_TYPE)
2242         {
2243           code = CONJ_EXPR;
2244           if (pedantic)
2245             pedwarn ("ISO C does not support `~' for complex conjugation");
2246           if (!noconvert)
2247             arg = default_conversion (arg);
2248         }
2249       else
2250         {
2251           error ("wrong type argument to bit-complement");
2252           return error_mark_node;
2253         }
2254       break;
2255
2256     case ABS_EXPR:
2257       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
2258         {
2259           error ("wrong type argument to abs");
2260           return error_mark_node;
2261         }
2262       else if (!noconvert)
2263         arg = default_conversion (arg);
2264       break;
2265
2266     case CONJ_EXPR:
2267       /* Conjugating a real value is a no-op, but allow it anyway.  */
2268       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2269             || typecode == COMPLEX_TYPE))
2270         {
2271           error ("wrong type argument to conjugation");
2272           return error_mark_node;
2273         }
2274       else if (!noconvert)
2275         arg = default_conversion (arg);
2276       break;
2277
2278     case TRUTH_NOT_EXPR:
2279       if (typecode != INTEGER_TYPE
2280           && typecode != REAL_TYPE && typecode != POINTER_TYPE
2281           && typecode != COMPLEX_TYPE
2282           /* These will convert to a pointer.  */
2283           && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2284         {
2285           error ("wrong type argument to unary exclamation mark");
2286           return error_mark_node;
2287         }
2288       arg = (*lang_hooks.truthvalue_conversion) (arg);
2289       return invert_truthvalue (arg);
2290
2291     case NOP_EXPR:
2292       break;
2293
2294     case REALPART_EXPR:
2295       if (TREE_CODE (arg) == COMPLEX_CST)
2296         return TREE_REALPART (arg);
2297       else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2298         return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2299       else
2300         return arg;
2301
2302     case IMAGPART_EXPR:
2303       if (TREE_CODE (arg) == COMPLEX_CST)
2304         return TREE_IMAGPART (arg);
2305       else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2306         return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2307       else
2308         return convert (TREE_TYPE (arg), integer_zero_node);
2309
2310     case PREINCREMENT_EXPR:
2311     case POSTINCREMENT_EXPR:
2312     case PREDECREMENT_EXPR:
2313     case POSTDECREMENT_EXPR:
2314
2315       /* Increment or decrement the real part of the value,
2316          and don't change the imaginary part.  */
2317       if (typecode == COMPLEX_TYPE)
2318         {
2319           tree real, imag;
2320
2321           if (pedantic)
2322             pedwarn ("ISO C does not support `++' and `--' on complex types");
2323
2324           arg = stabilize_reference (arg);
2325           real = build_unary_op (REALPART_EXPR, arg, 1);
2326           imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2327           return build (COMPLEX_EXPR, TREE_TYPE (arg),
2328                         build_unary_op (code, real, 1), imag);
2329         }
2330
2331       /* Report invalid types.  */
2332
2333       if (typecode != POINTER_TYPE
2334           && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
2335         {
2336           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2337             error ("wrong type argument to increment");
2338           else
2339             error ("wrong type argument to decrement");
2340
2341           return error_mark_node;
2342         }
2343
2344       {
2345         tree inc;
2346         tree result_type = TREE_TYPE (arg);
2347
2348         arg = get_unwidened (arg, 0);
2349         argtype = TREE_TYPE (arg);
2350
2351         /* Compute the increment.  */
2352
2353         if (typecode == POINTER_TYPE)
2354           {
2355             /* If pointer target is an undefined struct,
2356                we just cannot know how to do the arithmetic.  */
2357             if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
2358               {
2359                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2360                   error ("increment of pointer to unknown structure");
2361                 else
2362                   error ("decrement of pointer to unknown structure");
2363               }
2364             else if ((pedantic || warn_pointer_arith)
2365                      && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
2366                          || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
2367               {
2368                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
2369                   pedwarn ("wrong type argument to increment");
2370                 else
2371                   pedwarn ("wrong type argument to decrement");
2372               }
2373
2374             inc = c_size_in_bytes (TREE_TYPE (result_type));
2375           }
2376         else
2377           inc = integer_one_node;
2378
2379         inc = convert (argtype, inc);
2380
2381         /* Complain about anything else that is not a true lvalue.  */
2382         if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
2383                                     || code == POSTINCREMENT_EXPR)
2384                                    ? "invalid lvalue in increment"
2385                                    : "invalid lvalue in decrement")))
2386           return error_mark_node;
2387
2388         /* Report a read-only lvalue.  */
2389         if (TREE_READONLY (arg))
2390           readonly_error (arg,
2391                           ((code == PREINCREMENT_EXPR
2392                             || code == POSTINCREMENT_EXPR)
2393                            ? "increment" : "decrement"));
2394
2395         if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2396           val = boolean_increment (code, arg);
2397         else
2398           val = build (code, TREE_TYPE (arg), arg, inc);
2399         TREE_SIDE_EFFECTS (val) = 1;
2400         val = convert (result_type, val);
2401         if (TREE_CODE (val) != code)
2402           TREE_NO_UNUSED_WARNING (val) = 1;
2403         return val;
2404       }
2405
2406     case ADDR_EXPR:
2407       /* Note that this operation never does default_conversion.  */
2408
2409       /* Let &* cancel out to simplify resulting code.  */
2410       if (TREE_CODE (arg) == INDIRECT_REF)
2411         {
2412           /* Don't let this be an lvalue.  */
2413           if (lvalue_p (TREE_OPERAND (arg, 0)))
2414             return non_lvalue (TREE_OPERAND (arg, 0));
2415           return TREE_OPERAND (arg, 0);
2416         }
2417
2418       /* For &x[y], return x+y */
2419       if (TREE_CODE (arg) == ARRAY_REF)
2420         {
2421           if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
2422             return error_mark_node;
2423           return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
2424                                   TREE_OPERAND (arg, 1), 1);
2425         }
2426
2427       /* Anything not already handled and not a true memory reference
2428          or a non-lvalue array is an error.  */
2429       else if (typecode != FUNCTION_TYPE && !flag
2430                && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
2431         return error_mark_node;
2432
2433       /* Ordinary case; arg is a COMPONENT_REF or a decl.  */
2434       argtype = TREE_TYPE (arg);
2435
2436       /* If the lvalue is const or volatile, merge that into the type
2437          to which the address will point.  Note that you can't get a
2438          restricted pointer by taking the address of something, so we
2439          only have to deal with `const' and `volatile' here.  */
2440       if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
2441           && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
2442           argtype = c_build_type_variant (argtype,
2443                                           TREE_READONLY (arg),
2444                                           TREE_THIS_VOLATILE (arg));
2445
2446       argtype = build_pointer_type (argtype);
2447
2448       if (!c_mark_addressable (arg))
2449         return error_mark_node;
2450
2451       {
2452         tree addr;
2453
2454         if (TREE_CODE (arg) == COMPONENT_REF)
2455           {
2456             tree field = TREE_OPERAND (arg, 1);
2457
2458             addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
2459
2460             if (DECL_C_BIT_FIELD (field))
2461               {
2462                 error ("attempt to take address of bit-field structure member `%s'",
2463                        IDENTIFIER_POINTER (DECL_NAME (field)));
2464                 return error_mark_node;
2465               }
2466
2467             addr = fold (build (PLUS_EXPR, argtype,
2468                                 convert (argtype, addr),
2469                                 convert (argtype, byte_position (field))));
2470           }
2471         else
2472           addr = build1 (code, argtype, arg);
2473
2474         /* Address of a static or external variable or
2475            file-scope function counts as a constant.  */
2476         if (staticp (arg)
2477             && ! (TREE_CODE (arg) == FUNCTION_DECL
2478                   && !DECL_FILE_SCOPE_P (arg)))
2479           TREE_CONSTANT (addr) = 1;
2480         return addr;
2481       }
2482
2483     default:
2484       break;
2485     }
2486
2487   if (argtype == 0)
2488     argtype = TREE_TYPE (arg);
2489   return fold (build1 (code, argtype, arg));
2490 }
2491
2492 /* Return nonzero if REF is an lvalue valid for this language.
2493    Lvalues can be assigned, unless their type has TYPE_READONLY.
2494    Lvalues can have their address taken, unless they have DECL_REGISTER.  */
2495
2496 int
2497 lvalue_p (tree ref)
2498 {
2499   enum tree_code code = TREE_CODE (ref);
2500
2501   switch (code)
2502     {
2503     case REALPART_EXPR:
2504     case IMAGPART_EXPR:
2505     case COMPONENT_REF:
2506       return lvalue_p (TREE_OPERAND (ref, 0));
2507
2508     case COMPOUND_LITERAL_EXPR:
2509     case STRING_CST:
2510       return 1;
2511
2512     case INDIRECT_REF:
2513     case ARRAY_REF:
2514     case VAR_DECL:
2515     case PARM_DECL:
2516     case RESULT_DECL:
2517     case ERROR_MARK:
2518       return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
2519               && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
2520
2521     case BIND_EXPR:
2522     case RTL_EXPR:
2523       return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
2524
2525     default:
2526       return 0;
2527     }
2528 }
2529
2530 /* Return nonzero if REF is an lvalue valid for this language;
2531    otherwise, print an error message and return zero.  */
2532
2533 int
2534 lvalue_or_else (tree ref, const char *msgid)
2535 {
2536   int win = lvalue_p (ref);
2537
2538   if (! win)
2539     error ("%s", msgid);
2540
2541   return win;
2542 }
2543
2544 \f
2545 /* Warn about storing in something that is `const'.  */
2546
2547 void
2548 readonly_error (tree arg, const char *msgid)
2549 {
2550   if (TREE_CODE (arg) == COMPONENT_REF)
2551     {
2552       if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
2553         readonly_error (TREE_OPERAND (arg, 0), msgid);
2554       else
2555         error ("%s of read-only member `%s'", _(msgid),
2556                IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
2557     }
2558   else if (TREE_CODE (arg) == VAR_DECL)
2559     error ("%s of read-only variable `%s'", _(msgid),
2560            IDENTIFIER_POINTER (DECL_NAME (arg)));
2561   else
2562     error ("%s of read-only location", _(msgid));
2563 }
2564 \f
2565 /* Mark EXP saying that we need to be able to take the
2566    address of it; it should not be allocated in a register.
2567    Returns true if successful.  */
2568
2569 bool
2570 c_mark_addressable (tree exp)
2571 {
2572   tree x = exp;
2573
2574   while (1)
2575     switch (TREE_CODE (x))
2576       {
2577       case COMPONENT_REF:
2578         if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
2579           {
2580             error ("cannot take address of bit-field `%s'",
2581                    IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
2582             return false;
2583           }
2584
2585         /* ... fall through ...  */
2586
2587       case ADDR_EXPR:
2588       case ARRAY_REF:
2589       case REALPART_EXPR:
2590       case IMAGPART_EXPR:
2591         x = TREE_OPERAND (x, 0);
2592         break;
2593
2594       case COMPOUND_LITERAL_EXPR:
2595       case CONSTRUCTOR:
2596         TREE_ADDRESSABLE (x) = 1;
2597         return true;
2598
2599       case VAR_DECL:
2600       case CONST_DECL:
2601       case PARM_DECL:
2602       case RESULT_DECL:
2603         if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
2604             && DECL_NONLOCAL (x))
2605           {
2606             if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2607               {
2608                 error ("global register variable `%s' used in nested function",
2609                        IDENTIFIER_POINTER (DECL_NAME (x)));
2610                 return false;
2611               }
2612             pedwarn ("register variable `%s' used in nested function",
2613                      IDENTIFIER_POINTER (DECL_NAME (x)));
2614           }
2615         else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
2616           {
2617             if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
2618               {
2619                 error ("address of global register variable `%s' requested",
2620                        IDENTIFIER_POINTER (DECL_NAME (x)));
2621                 return false;
2622               }
2623
2624             /* If we are making this addressable due to its having
2625                volatile components, give a different error message.  Also
2626                handle the case of an unnamed parameter by not trying
2627                to give the name.  */
2628
2629             else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
2630               {
2631                 error ("cannot put object with volatile field into register");
2632                 return false;
2633               }
2634
2635             pedwarn ("address of register variable `%s' requested",
2636                      IDENTIFIER_POINTER (DECL_NAME (x)));
2637           }
2638         put_var_into_stack (x, /*rescan=*/true);
2639
2640         /* drops in */
2641       case FUNCTION_DECL:
2642         TREE_ADDRESSABLE (x) = 1;
2643         /* drops out */
2644       default:
2645         return true;
2646     }
2647 }
2648 \f
2649 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  */
2650
2651 tree
2652 build_conditional_expr (tree ifexp, tree op1, tree op2)
2653 {
2654   tree type1;
2655   tree type2;
2656   enum tree_code code1;
2657   enum tree_code code2;
2658   tree result_type = NULL;
2659   tree orig_op1 = op1, orig_op2 = op2;
2660
2661   ifexp = (*lang_hooks.truthvalue_conversion) (default_conversion (ifexp));
2662
2663   /* Promote both alternatives.  */
2664
2665   if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
2666     op1 = default_conversion (op1);
2667   if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
2668     op2 = default_conversion (op2);
2669
2670   if (TREE_CODE (ifexp) == ERROR_MARK
2671       || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
2672       || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
2673     return error_mark_node;
2674
2675   type1 = TREE_TYPE (op1);
2676   code1 = TREE_CODE (type1);
2677   type2 = TREE_TYPE (op2);
2678   code2 = TREE_CODE (type2);
2679
2680   /* C90 does not permit non-lvalue arrays in conditional expressions.
2681      In C99 they will be pointers by now.  */
2682   if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
2683     {
2684       error ("non-lvalue array in conditional expression");
2685       return error_mark_node;
2686     }
2687
2688   /* Quickly detect the usual case where op1 and op2 have the same type
2689      after promotion.  */
2690   if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
2691     {
2692       if (type1 == type2)
2693         result_type = type1;
2694       else
2695         result_type = TYPE_MAIN_VARIANT (type1);
2696     }
2697   else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
2698             || code1 == COMPLEX_TYPE)
2699            && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
2700                || code2 == COMPLEX_TYPE))
2701     {
2702       result_type = common_type (type1, type2);
2703
2704       /* If -Wsign-compare, warn here if type1 and type2 have
2705          different signedness.  We'll promote the signed to unsigned
2706          and later code won't know it used to be different.
2707          Do this check on the original types, so that explicit casts
2708          will be considered, but default promotions won't.  */
2709       if (warn_sign_compare && !skip_evaluation)
2710         {
2711           int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
2712           int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
2713
2714           if (unsigned_op1 ^ unsigned_op2)
2715             {
2716               /* Do not warn if the result type is signed, since the
2717                  signed type will only be chosen if it can represent
2718                  all the values of the unsigned type.  */
2719               if (! TREE_UNSIGNED (result_type))
2720                 /* OK */;
2721               /* Do not warn if the signed quantity is an unsuffixed
2722                  integer literal (or some static constant expression
2723                  involving such literals) and it is non-negative.  */
2724               else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
2725                        || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
2726                 /* OK */;
2727               else
2728                 warning ("signed and unsigned type in conditional expression");
2729             }
2730         }
2731     }
2732   else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
2733     {
2734       if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
2735         pedwarn ("ISO C forbids conditional expr with only one void side");
2736       result_type = void_type_node;
2737     }
2738   else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
2739     {
2740       if (comp_target_types (type1, type2, 1))
2741         result_type = common_type (type1, type2);
2742       else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
2743                && TREE_CODE (orig_op1) != NOP_EXPR)
2744         result_type = qualify_type (type2, type1);
2745       else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
2746                && TREE_CODE (orig_op2) != NOP_EXPR)
2747         result_type = qualify_type (type1, type2);
2748       else if (VOID_TYPE_P (TREE_TYPE (type1)))
2749         {
2750           if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
2751             pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2752           result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
2753                                                           TREE_TYPE (type2)));
2754         }
2755       else if (VOID_TYPE_P (TREE_TYPE (type2)))
2756         {
2757           if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
2758             pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
2759           result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
2760                                                           TREE_TYPE (type1)));
2761         }
2762       else
2763         {
2764           pedwarn ("pointer type mismatch in conditional expression");
2765           result_type = build_pointer_type (void_type_node);
2766         }
2767     }
2768   else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
2769     {
2770       if (! integer_zerop (op2))
2771         pedwarn ("pointer/integer type mismatch in conditional expression");
2772       else
2773         {
2774           op2 = null_pointer_node;
2775         }
2776       result_type = type1;
2777     }
2778   else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
2779     {
2780       if (!integer_zerop (op1))
2781         pedwarn ("pointer/integer type mismatch in conditional expression");
2782       else
2783         {
2784           op1 = null_pointer_node;
2785         }
2786       result_type = type2;
2787     }
2788
2789   if (!result_type)
2790     {
2791       if (flag_cond_mismatch)
2792         result_type = void_type_node;
2793       else
2794         {
2795           error ("type mismatch in conditional expression");
2796           return error_mark_node;
2797         }
2798     }
2799
2800   /* Merge const and volatile flags of the incoming types.  */
2801   result_type
2802     = build_type_variant (result_type,
2803                           TREE_READONLY (op1) || TREE_READONLY (op2),
2804                           TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
2805
2806   if (result_type != TREE_TYPE (op1))
2807     op1 = convert_and_check (result_type, op1);
2808   if (result_type != TREE_TYPE (op2))
2809     op2 = convert_and_check (result_type, op2);
2810
2811   if (TREE_CODE (ifexp) == INTEGER_CST)
2812     return non_lvalue (integer_zerop (ifexp) ? op2 : op1);
2813
2814   return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
2815 }
2816 \f
2817 /* Given a list of expressions, return a compound expression
2818    that performs them all and returns the value of the last of them.  */
2819
2820 tree
2821 build_compound_expr (tree list)
2822 {
2823   return internal_build_compound_expr (list, TRUE);
2824 }
2825
2826 static tree
2827 internal_build_compound_expr (tree list, int first_p)
2828 {
2829   tree rest;
2830
2831   if (TREE_CHAIN (list) == 0)
2832     {
2833       /* Convert arrays and functions to pointers when there
2834          really is a comma operator.  */
2835       if (!first_p)
2836         TREE_VALUE (list)
2837           = default_function_array_conversion (TREE_VALUE (list));
2838
2839       /* Don't let (0, 0) be null pointer constant.  */
2840       if (!first_p && integer_zerop (TREE_VALUE (list)))
2841         return non_lvalue (TREE_VALUE (list));
2842       return TREE_VALUE (list);
2843     }
2844
2845   rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
2846
2847   if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
2848     {
2849       /* The left-hand operand of a comma expression is like an expression
2850          statement: with -Wextra or -Wunused, we should warn if it doesn't have
2851          any side-effects, unless it was explicitly cast to (void).  */
2852       if (warn_unused_value
2853            && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
2854                 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
2855         warning ("left-hand operand of comma expression has no effect");
2856     }
2857
2858   /* With -Wunused, we should also warn if the left-hand operand does have
2859      side-effects, but computes a value which is not used.  For example, in
2860      `foo() + bar(), baz()' the result of the `+' operator is not used,
2861      so we should issue a warning.  */
2862   else if (warn_unused_value)
2863     warn_if_unused_value (TREE_VALUE (list));
2864
2865   return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
2866 }
2867
2868 /* Build an expression representing a cast to type TYPE of expression EXPR.  */
2869
2870 tree
2871 build_c_cast (tree type, tree expr)
2872 {
2873   tree value = expr;
2874
2875   if (type == error_mark_node || expr == error_mark_node)
2876     return error_mark_node;
2877
2878   /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
2879      only in <protocol> qualifications.  But when constructing cast expressions,
2880      the protocols do matter and must be kept around.  */
2881   if (!c_dialect_objc () || !objc_is_object_ptr (type))
2882     type = TYPE_MAIN_VARIANT (type);
2883
2884   if (TREE_CODE (type) == ARRAY_TYPE)
2885     {
2886       error ("cast specifies array type");
2887       return error_mark_node;
2888     }
2889
2890   if (TREE_CODE (type) == FUNCTION_TYPE)
2891     {
2892       error ("cast specifies function type");
2893       return error_mark_node;
2894     }
2895
2896   if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
2897     {
2898       if (pedantic)
2899         {
2900           if (TREE_CODE (type) == RECORD_TYPE
2901               || TREE_CODE (type) == UNION_TYPE)
2902             pedwarn ("ISO C forbids casting nonscalar to the same type");
2903         }
2904     }
2905   else if (TREE_CODE (type) == UNION_TYPE)
2906     {
2907       tree field;
2908       value = default_function_array_conversion (value);
2909
2910       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2911         if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
2912                        TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
2913           break;
2914
2915       if (field)
2916         {
2917           tree t;
2918
2919           if (pedantic)
2920             pedwarn ("ISO C forbids casts to union type");
2921           t = digest_init (type,
2922                            build_constructor (type,
2923                                               build_tree_list (field, value)),
2924                            0);
2925           TREE_CONSTANT (t) = TREE_CONSTANT (value);
2926           return t;
2927         }
2928       error ("cast to union type from type not present in union");
2929       return error_mark_node;
2930     }
2931   else
2932     {
2933       tree otype, ovalue;
2934
2935       /* If casting to void, avoid the error that would come
2936          from default_conversion in the case of a non-lvalue array.  */
2937       if (type == void_type_node)
2938         return build1 (CONVERT_EXPR, type, value);
2939
2940       /* Convert functions and arrays to pointers,
2941          but don't convert any other types.  */
2942       value = default_function_array_conversion (value);
2943       otype = TREE_TYPE (value);
2944
2945       /* Optionally warn about potentially worrisome casts.  */
2946
2947       if (warn_cast_qual
2948           && TREE_CODE (type) == POINTER_TYPE
2949           && TREE_CODE (otype) == POINTER_TYPE)
2950         {
2951           tree in_type = type;
2952           tree in_otype = otype;
2953           int added = 0;
2954           int discarded = 0;
2955
2956           /* Check that the qualifiers on IN_TYPE are a superset of
2957              the qualifiers of IN_OTYPE.  The outermost level of
2958              POINTER_TYPE nodes is uninteresting and we stop as soon
2959              as we hit a non-POINTER_TYPE node on either type.  */
2960           do
2961             {
2962               in_otype = TREE_TYPE (in_otype);
2963               in_type = TREE_TYPE (in_type);
2964
2965               /* GNU C allows cv-qualified function types.  'const'
2966                  means the function is very pure, 'volatile' means it
2967                  can't return.  We need to warn when such qualifiers
2968                  are added, not when they're taken away.  */
2969               if (TREE_CODE (in_otype) == FUNCTION_TYPE
2970                   && TREE_CODE (in_type) == FUNCTION_TYPE)
2971                 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
2972               else
2973                 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
2974             }
2975           while (TREE_CODE (in_type) == POINTER_TYPE
2976                  && TREE_CODE (in_otype) == POINTER_TYPE);
2977
2978           if (added)
2979             warning ("cast adds new qualifiers to function type");
2980
2981           if (discarded)
2982             /* There are qualifiers present in IN_OTYPE that are not
2983                present in IN_TYPE.  */
2984             warning ("cast discards qualifiers from pointer target type");
2985         }
2986
2987       /* Warn about possible alignment problems.  */
2988       if (STRICT_ALIGNMENT && warn_cast_align
2989           && TREE_CODE (type) == POINTER_TYPE
2990           && TREE_CODE (otype) == POINTER_TYPE
2991           && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
2992           && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
2993           /* Don't warn about opaque types, where the actual alignment
2994              restriction is unknown.  */
2995           && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
2996                 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
2997                && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
2998           && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
2999         warning ("cast increases required alignment of target type");
3000
3001       if (TREE_CODE (type) == INTEGER_TYPE
3002           && TREE_CODE (otype) == POINTER_TYPE
3003           && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3004           && !TREE_CONSTANT (value))
3005         warning ("cast from pointer to integer of different size");
3006
3007       if (warn_bad_function_cast
3008           && TREE_CODE (value) == CALL_EXPR
3009           && TREE_CODE (type) != TREE_CODE (otype))
3010         warning ("cast does not match function type");
3011
3012       if (TREE_CODE (type) == POINTER_TYPE
3013           && TREE_CODE (otype) == INTEGER_TYPE
3014           && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3015           /* Don't warn about converting any constant.  */
3016           && !TREE_CONSTANT (value))
3017         warning ("cast to pointer from integer of different size");
3018
3019       if (TREE_CODE (type) == POINTER_TYPE
3020           && TREE_CODE (otype) == POINTER_TYPE
3021           && TREE_CODE (expr) == ADDR_EXPR
3022           && DECL_P (TREE_OPERAND (expr, 0))
3023           && flag_strict_aliasing && warn_strict_aliasing
3024           && !VOID_TYPE_P (TREE_TYPE (type)))
3025         {
3026           /* Casting the address of a decl to non void pointer. Warn
3027              if the cast breaks type based aliasing.  */
3028           if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
3029             warning ("type-punning to incomplete type might break strict-aliasing rules");
3030           else
3031             {
3032               HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
3033               HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type));
3034
3035               if (!alias_sets_conflict_p (set1, set2))
3036                 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
3037               else if (warn_strict_aliasing > 1
3038                        && !alias_sets_might_conflict_p (set1, set2))
3039                 warning ("dereferencing type-punned pointer might break strict-aliasing rules");
3040             }
3041         }
3042
3043       /* If pedantic, warn for conversions between function and object
3044          pointer types, except for converting a null pointer constant
3045          to function pointer type.  */
3046       if (pedantic
3047           && TREE_CODE (type) == POINTER_TYPE
3048           && TREE_CODE (otype) == POINTER_TYPE
3049           && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
3050           && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
3051         pedwarn ("ISO C forbids conversion of function pointer to object pointer type");
3052
3053       if (pedantic
3054           && TREE_CODE (type) == POINTER_TYPE
3055           && TREE_CODE (otype) == POINTER_TYPE
3056           && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3057           && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
3058           && !(integer_zerop (value) && TREE_TYPE (otype) == void_type_node
3059                && TREE_CODE (expr) != NOP_EXPR))
3060         pedwarn ("ISO C forbids conversion of object pointer to function pointer type");
3061
3062       ovalue = value;
3063       /* Replace a nonvolatile const static variable with its value.  */
3064       if (optimize && TREE_CODE (value) == VAR_DECL)
3065         value = decl_constant_value (value);
3066       value = convert (type, value);
3067
3068       /* Ignore any integer overflow caused by the cast.  */
3069       if (TREE_CODE (value) == INTEGER_CST)
3070         {
3071           TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3072           TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3073         }
3074     }
3075
3076   /* Don't let (void *) (FOO *) 0 be a null pointer constant.  */
3077   if (TREE_CODE (value) == INTEGER_CST
3078       && TREE_CODE (expr) == INTEGER_CST
3079       && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3080     value = non_lvalue (value);
3081
3082   /* Don't let a cast be an lvalue.  */
3083   if (value == expr)
3084     value = non_lvalue (value);
3085
3086   return value;
3087 }
3088
3089 /* Interpret a cast of expression EXPR to type TYPE.  */
3090 tree
3091 c_cast_expr (tree type, tree expr)
3092 {
3093   int saved_wsp = warn_strict_prototypes;
3094
3095   /* This avoids warnings about unprototyped casts on
3096      integers.  E.g. "#define SIG_DFL (void(*)())0".  */
3097   if (TREE_CODE (expr) == INTEGER_CST)
3098     warn_strict_prototypes = 0;
3099   type = groktypename (type);
3100   warn_strict_prototypes = saved_wsp;
3101
3102   return build_c_cast (type, expr);
3103 }
3104
3105 \f
3106 /* Build an assignment expression of lvalue LHS from value RHS.
3107    MODIFYCODE is the code for a binary operator that we use
3108    to combine the old value of LHS with RHS to get the new value.
3109    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.  */
3110
3111 tree
3112 build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
3113 {
3114   tree result;
3115   tree newrhs;
3116   tree lhstype = TREE_TYPE (lhs);
3117   tree olhstype = lhstype;
3118
3119   /* Types that aren't fully specified cannot be used in assignments.  */
3120   lhs = require_complete_type (lhs);
3121
3122   /* Avoid duplicate error messages from operands that had errors.  */
3123   if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3124     return error_mark_node;
3125
3126   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
3127   /* Do not use STRIP_NOPS here.  We do not want an enumerator
3128      whose value is 0 to count as a null pointer constant.  */
3129   if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3130     rhs = TREE_OPERAND (rhs, 0);
3131
3132   newrhs = rhs;
3133
3134   /* If a binary op has been requested, combine the old LHS value with the RHS
3135      producing the value we should actually store into the LHS.  */
3136
3137   if (modifycode != NOP_EXPR)
3138     {
3139       lhs = stabilize_reference (lhs);
3140       newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3141     }
3142
3143   if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
3144     return error_mark_node;
3145
3146   /* Warn about storing in something that is `const'.  */
3147
3148   if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3149       || ((TREE_CODE (lhstype) == RECORD_TYPE
3150            || TREE_CODE (lhstype) == UNION_TYPE)
3151           && C_TYPE_FIELDS_READONLY (lhstype)))
3152     readonly_error (lhs, "assignment");
3153
3154   /* If storing into a structure or union member,
3155      it has probably been given type `int'.
3156      Compute the type that would go with
3157      the actual amount of storage the member occupies.  */
3158
3159   if (TREE_CODE (lhs) == COMPONENT_REF
3160       && (TREE_CODE (lhstype) == INTEGER_TYPE
3161           || TREE_CODE (lhstype) == BOOLEAN_TYPE
3162           || TREE_CODE (lhstype) == REAL_TYPE
3163           || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3164     lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3165
3166   /* If storing in a field that is in actuality a short or narrower than one,
3167      we must store in the field in its actual type.  */
3168
3169   if (lhstype != TREE_TYPE (lhs))
3170     {
3171       lhs = copy_node (lhs);
3172       TREE_TYPE (lhs) = lhstype;
3173     }
3174
3175   /* Convert new value to destination type.  */
3176
3177   newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
3178                                    NULL_TREE, NULL_TREE, 0);
3179   if (TREE_CODE (newrhs) == ERROR_MARK)
3180     return error_mark_node;
3181
3182   /* Scan operands */
3183
3184   result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
3185   TREE_SIDE_EFFECTS (result) = 1;
3186
3187   /* If we got the LHS in a different type for storing in,
3188      convert the result back to the nominal type of LHS
3189      so that the value we return always has the same type
3190      as the LHS argument.  */
3191
3192   if (olhstype == TREE_TYPE (result))
3193     return result;
3194   return convert_for_assignment (olhstype, result, _("assignment"),
3195                                  NULL_TREE, NULL_TREE, 0);
3196 }
3197 \f
3198 /* Convert value RHS to type TYPE as preparation for an assignment
3199    to an lvalue of type TYPE.
3200    The real work of conversion is done by `convert'.
3201    The purpose of this function is to generate error messages
3202    for assignments that are not allowed in C.
3203    ERRTYPE is a string to use in error messages:
3204    "assignment", "return", etc.  If it is null, this is parameter passing
3205    for a function call (and different error messages are output).
3206
3207    FUNNAME is the name of the function being called,
3208    as an IDENTIFIER_NODE, or null.
3209    PARMNUM is the number of the argument, for printing in error messages.  */
3210
3211 static tree
3212 convert_for_assignment (tree type, tree rhs, const char *errtype,
3213                         tree fundecl, tree funname, int parmnum)
3214 {
3215   enum tree_code codel = TREE_CODE (type);
3216   tree rhstype;
3217   enum tree_code coder;
3218
3219   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
3220   /* Do not use STRIP_NOPS here.  We do not want an enumerator
3221      whose value is 0 to count as a null pointer constant.  */
3222   if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3223     rhs = TREE_OPERAND (rhs, 0);
3224
3225   if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
3226       || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
3227     rhs = default_conversion (rhs);
3228   else if (optimize && TREE_CODE (rhs) == VAR_DECL)
3229     rhs = decl_constant_value_for_broken_optimization (rhs);
3230
3231   rhstype = TREE_TYPE (rhs);
3232   coder = TREE_CODE (rhstype);
3233
3234   if (coder == ERROR_MARK)
3235     return error_mark_node;
3236
3237   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
3238     {
3239       overflow_warning (rhs);
3240       /* Check for Objective-C protocols.  This will automatically
3241          issue a warning if there are protocol violations.  No need to
3242          use the return value.  */
3243       if (c_dialect_objc ())
3244         objc_comptypes (type, rhstype, 0);
3245       return rhs;
3246     }
3247
3248   if (coder == VOID_TYPE)
3249     {
3250       error ("void value not ignored as it ought to be");
3251       return error_mark_node;
3252     }
3253   /* A type converts to a reference to it.
3254      This code doesn't fully support references, it's just for the
3255      special case of va_start and va_copy.  */
3256   if (codel == REFERENCE_TYPE
3257       && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
3258     {
3259       if (!lvalue_p (rhs))
3260         {
3261           error ("cannot pass rvalue to reference parameter");
3262           return error_mark_node;
3263         }
3264       if (!c_mark_addressable (rhs))
3265         return error_mark_node;
3266       rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
3267
3268       /* We already know that these two types are compatible, but they
3269          may not be exactly identical.  In fact, `TREE_TYPE (type)' is
3270          likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
3271          likely to be va_list, a typedef to __builtin_va_list, which
3272          is different enough that it will cause problems later.  */
3273       if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
3274         rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
3275
3276       rhs = build1 (NOP_EXPR, type, rhs);
3277       return rhs;
3278     }
3279   /* Some types can interconvert without explicit casts.  */
3280   else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
3281            && ((*targetm.vector_opaque_p) (type)
3282                || (*targetm.vector_opaque_p) (rhstype)))
3283     return convert (type, rhs);
3284   /* Arithmetic types all interconvert, and enum is treated like int.  */
3285   else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
3286             || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
3287             || codel == BOOLEAN_TYPE)
3288            && (coder == INTEGER_TYPE || coder == REAL_TYPE
3289                || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
3290                || coder == BOOLEAN_TYPE))
3291     return convert_and_check (type, rhs);
3292
3293   /* Conversion to a transparent union from its member types.
3294      This applies only to function arguments.  */
3295   else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
3296     {
3297       tree memb_types;
3298       tree marginal_memb_type = 0;
3299
3300       for (memb_types = TYPE_FIELDS (type); memb_types;
3301            memb_types = TREE_CHAIN (memb_types))
3302         {
3303           tree memb_type = TREE_TYPE (memb_types);
3304
3305           if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3306                          TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
3307             break;
3308
3309           if (TREE_CODE (memb_type) != POINTER_TYPE)
3310             continue;
3311
3312           if (coder == POINTER_TYPE)
3313             {
3314               tree ttl = TREE_TYPE (memb_type);
3315               tree ttr = TREE_TYPE (rhstype);
3316
3317               /* Any non-function converts to a [const][volatile] void *
3318                  and vice versa; otherwise, targets must be the same.
3319                  Meanwhile, the lhs target must have all the qualifiers of
3320                  the rhs.  */
3321               if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3322                   || comp_target_types (memb_type, rhstype, 0))
3323                 {
3324                   /* If this type won't generate any warnings, use it.  */
3325                   if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
3326                       || ((TREE_CODE (ttr) == FUNCTION_TYPE
3327                            && TREE_CODE (ttl) == FUNCTION_TYPE)
3328                           ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3329                              == TYPE_QUALS (ttr))
3330                           : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3331                              == TYPE_QUALS (ttl))))
3332                     break;
3333
3334                   /* Keep looking for a better type, but remember this one.  */
3335                   if (! marginal_memb_type)
3336                     marginal_memb_type = memb_type;
3337                 }
3338             }
3339
3340           /* Can convert integer zero to any pointer type.  */
3341           if (integer_zerop (rhs)
3342               || (TREE_CODE (rhs) == NOP_EXPR
3343                   && integer_zerop (TREE_OPERAND (rhs, 0))))
3344             {
3345               rhs = null_pointer_node;
3346               break;
3347             }
3348         }
3349
3350       if (memb_types || marginal_memb_type)
3351         {
3352           if (! memb_types)
3353             {
3354               /* We have only a marginally acceptable member type;
3355                  it needs a warning.  */
3356               tree ttl = TREE_TYPE (marginal_memb_type);
3357               tree ttr = TREE_TYPE (rhstype);
3358
3359               /* Const and volatile mean something different for function
3360                  types, so the usual warnings are not appropriate.  */
3361               if (TREE_CODE (ttr) == FUNCTION_TYPE
3362                   && TREE_CODE (ttl) == FUNCTION_TYPE)
3363                 {
3364                   /* Because const and volatile on functions are
3365                      restrictions that say the function will not do
3366                      certain things, it is okay to use a const or volatile
3367                      function where an ordinary one is wanted, but not
3368                      vice-versa.  */
3369                   if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3370                     warn_for_assignment ("%s makes qualified function pointer from unqualified",
3371                                          errtype, funname, parmnum);
3372                 }
3373               else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3374                 warn_for_assignment ("%s discards qualifiers from pointer target type",
3375                                      errtype, funname,
3376                                      parmnum);
3377             }
3378
3379           if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
3380             pedwarn ("ISO C prohibits argument conversion to union type");
3381
3382           return build1 (NOP_EXPR, type, rhs);
3383         }
3384     }
3385
3386   /* Conversions among pointers */
3387   else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
3388            && (coder == codel))
3389     {
3390       tree ttl = TREE_TYPE (type);
3391       tree ttr = TREE_TYPE (rhstype);
3392       bool is_opaque_pointer;
3393       int target_cmp = 0;   /* Cache comp_target_types () result.  */
3394
3395       /* Opaque pointers are treated like void pointers.  */
3396       is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
3397                            || (*targetm.vector_opaque_p) (rhstype))
3398         && TREE_CODE (ttl) == VECTOR_TYPE
3399         && TREE_CODE (ttr) == VECTOR_TYPE;
3400
3401       /* Any non-function converts to a [const][volatile] void *
3402          and vice versa; otherwise, targets must be the same.
3403          Meanwhile, the lhs target must have all the qualifiers of the rhs.  */
3404       if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3405           || (target_cmp = comp_target_types (type, rhstype, 0))
3406           || is_opaque_pointer
3407           || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
3408               == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
3409         {
3410           if (pedantic
3411               && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
3412                   ||
3413                   (VOID_TYPE_P (ttr)
3414                    /* Check TREE_CODE to catch cases like (void *) (char *) 0
3415                       which are not ANSI null ptr constants.  */
3416                    && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
3417                    && TREE_CODE (ttl) == FUNCTION_TYPE)))
3418             warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
3419                                  errtype, funname, parmnum);
3420           /* Const and volatile mean something different for function types,
3421              so the usual warnings are not appropriate.  */
3422           else if (TREE_CODE (ttr) != FUNCTION_TYPE
3423                    && TREE_CODE (ttl) != FUNCTION_TYPE)
3424             {
3425               if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
3426                 warn_for_assignment ("%s discards qualifiers from pointer target type",
3427                                      errtype, funname, parmnum);
3428               /* If this is not a case of ignoring a mismatch in signedness,
3429                  no warning.  */
3430               else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
3431                        || target_cmp)
3432                 ;
3433               /* If there is a mismatch, do warn.  */
3434               else if (pedantic)
3435                 warn_for_assignment ("pointer targets in %s differ in signedness",
3436                                      errtype, funname, parmnum);
3437             }
3438           else if (TREE_CODE (ttl) == FUNCTION_TYPE
3439                    && TREE_CODE (ttr) == FUNCTION_TYPE)
3440             {
3441               /* Because const and volatile on functions are restrictions
3442                  that say the function will not do certain things,
3443                  it is okay to use a const or volatile function
3444                  where an ordinary one is wanted, but not vice-versa.  */
3445               if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
3446                 warn_for_assignment ("%s makes qualified function pointer from unqualified",
3447                                      errtype, funname, parmnum);
3448             }
3449         }
3450       else
3451         warn_for_assignment ("%s from incompatible pointer type",
3452                              errtype, funname, parmnum);
3453       return convert (type, rhs);
3454     }
3455   else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
3456     {
3457       error ("invalid use of non-lvalue array");
3458       return error_mark_node;
3459     }
3460   else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3461     {
3462       /* An explicit constant 0 can convert to a pointer,
3463          or one that results from arithmetic, even including
3464          a cast to integer type.  */
3465       if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
3466           &&
3467           ! (TREE_CODE (rhs) == NOP_EXPR
3468              && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
3469              && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
3470              && integer_zerop (TREE_OPERAND (rhs, 0))))
3471           warn_for_assignment ("%s makes pointer from integer without a cast",
3472                                errtype, funname, parmnum);
3473
3474       return convert (type, rhs);
3475     }
3476   else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
3477     {
3478       warn_for_assignment ("%s makes integer from pointer without a cast",
3479                            errtype, funname, parmnum);
3480       return convert (type, rhs);
3481     }
3482   else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
3483     return convert (type, rhs);
3484
3485   if (!errtype)
3486     {
3487       if (funname)
3488         {
3489           tree selector = objc_message_selector ();
3490
3491           if (selector && parmnum > 2)
3492             error ("incompatible type for argument %d of `%s'",
3493                    parmnum - 2, IDENTIFIER_POINTER (selector));
3494           else
3495             error ("incompatible type for argument %d of `%s'",
3496                    parmnum, IDENTIFIER_POINTER (funname));
3497         }
3498       else
3499         error ("incompatible type for argument %d of indirect function call",
3500                parmnum);
3501     }
3502   else
3503     error ("incompatible types in %s", errtype);
3504
3505   return error_mark_node;
3506 }
3507
3508 /* Convert VALUE for assignment into inlined parameter PARM.  ARGNUM
3509    is used for error and waring reporting and indicates which argument
3510    is being processed.  */
3511
3512 tree
3513 c_convert_parm_for_inlining (tree parm, tree value, tree fn, int argnum)
3514 {
3515   tree ret, type;
3516
3517   /* If FN was prototyped, the value has been converted already
3518      in convert_arguments.  */
3519   if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
3520     return value;
3521
3522   type = TREE_TYPE (parm);
3523   ret = convert_for_assignment (type, value,
3524                                 (char *) 0 /* arg passing  */, fn,
3525                                 DECL_NAME (fn), argnum);
3526   if (targetm.calls.promote_prototypes (TREE_TYPE (fn))
3527       && INTEGRAL_TYPE_P (type)
3528       && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3529     ret = default_conversion (ret);
3530   return ret;
3531 }
3532
3533 /* Print a warning using MSGID.
3534    It gets OPNAME as its one parameter.
3535    if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
3536    Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
3537    FUNCTION and ARGNUM are handled specially if we are building an
3538    Objective-C selector.  */
3539
3540 static void
3541 warn_for_assignment (const char *msgid, const char *opname, tree function,
3542                      int argnum)
3543 {
3544   if (opname == 0)
3545     {
3546       tree selector = objc_message_selector ();
3547       char * new_opname;
3548
3549       if (selector && argnum > 2)
3550         {
3551           function = selector;
3552           argnum -= 2;
3553         }
3554       if (argnum == 0)
3555         {
3556           if (function)
3557             {
3558               /* Function name is known; supply it.  */
3559               const char *const argstring = _("passing arg of `%s'");
3560               new_opname = alloca (IDENTIFIER_LENGTH (function)
3561                                    + strlen (argstring) + 1 + 1);
3562               sprintf (new_opname, argstring,
3563                        IDENTIFIER_POINTER (function));
3564             }
3565           else
3566             {
3567               /* Function name unknown (call through ptr).  */
3568               const char *const argnofun = _("passing arg of pointer to function");
3569               new_opname = alloca (strlen (argnofun) + 1 + 1);
3570               sprintf (new_opname, argnofun);
3571             }
3572         }
3573       else if (function)
3574         {
3575           /* Function name is known; supply it.  */
3576           const char *const argstring = _("passing arg %d of `%s'");
3577           new_opname = alloca (IDENTIFIER_LENGTH (function)
3578                                + strlen (argstring) + 1 + 25 /*%d*/ + 1);
3579           sprintf (new_opname, argstring, argnum,
3580                    IDENTIFIER_POINTER (function));
3581         }
3582       else
3583         {
3584           /* Function name unknown (call through ptr); just give arg number.  */
3585           const char *const argnofun = _("passing arg %d of pointer to function");
3586           new_opname = alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
3587           sprintf (new_opname, argnofun, argnum);
3588         }
3589       opname = new_opname;
3590     }
3591   pedwarn (msgid, opname);
3592 }
3593 \f
3594 /* If VALUE is a compound expr all of whose expressions are constant, then
3595    return its value.  Otherwise, return error_mark_node.
3596
3597    This is for handling COMPOUND_EXPRs as initializer elements
3598    which is allowed with a warning when -pedantic is specified.  */
3599
3600 static tree
3601 valid_compound_expr_initializer (tree value, tree endtype)
3602 {
3603   if (TREE_CODE (value) == COMPOUND_EXPR)
3604     {
3605       if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
3606           == error_mark_node)
3607         return error_mark_node;
3608       return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
3609                                               endtype);
3610     }
3611   else if (! TREE_CONSTANT (value)
3612            && ! initializer_constant_valid_p (value, endtype))
3613     return error_mark_node;
3614   else
3615     return value;
3616 }
3617 \f
3618 /* Perform appropriate conversions on the initial value of a variable,
3619    store it in the declaration DECL,
3620    and print any error messages that are appropriate.
3621    If the init is invalid, store an ERROR_MARK.  */
3622
3623 void
3624 store_init_value (tree decl, tree init)
3625 {
3626   tree value, type;
3627
3628   /* If variable's type was invalidly declared, just ignore it.  */
3629
3630   type = TREE_TYPE (decl);
3631   if (TREE_CODE (type) == ERROR_MARK)
3632     return;
3633
3634   /* Digest the specified initializer into an expression.  */
3635
3636   value = digest_init (type, init, TREE_STATIC (decl));
3637
3638   /* Store the expression if valid; else report error.  */
3639
3640   if (warn_traditional && !in_system_header
3641       && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
3642     warning ("traditional C rejects automatic aggregate initialization");
3643
3644   DECL_INITIAL (decl) = value;
3645
3646   /* ANSI wants warnings about out-of-range constant initializers.  */
3647   STRIP_TYPE_NOPS (value);
3648   constant_expression_warning (value);
3649
3650   /* Check if we need to set array size from compound literal size.  */
3651   if (TREE_CODE (type) == ARRAY_TYPE
3652       && TYPE_DOMAIN (type) == 0
3653       && value != error_mark_node)
3654     {
3655       tree inside_init = init;
3656
3657       if (TREE_CODE (init) == NON_LVALUE_EXPR)
3658         inside_init = TREE_OPERAND (init, 0);
3659       inside_init = fold (inside_init);
3660
3661       if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3662         {
3663           tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3664
3665           if (TYPE_DOMAIN (TREE_TYPE (decl)))
3666             {
3667               /* For int foo[] = (int [3]){1}; we need to set array size
3668                  now since later on array initializer will be just the
3669                  brace enclosed list of the compound literal.  */
3670               TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
3671               layout_type (type);
3672               layout_decl (decl, 0);
3673             }
3674         }
3675     }
3676 }
3677 \f
3678 /* Methods for storing and printing names for error messages.  */
3679
3680 /* Implement a spelling stack that allows components of a name to be pushed
3681    and popped.  Each element on the stack is this structure.  */
3682
3683 struct spelling
3684 {
3685   int kind;
3686   union
3687     {
3688       int i;
3689       const char *s;
3690     } u;
3691 };
3692
3693 #define SPELLING_STRING 1
3694 #define SPELLING_MEMBER 2
3695 #define SPELLING_BOUNDS 3
3696
3697 static struct spelling *spelling;       /* Next stack element (unused).  */
3698 static struct spelling *spelling_base;  /* Spelling stack base.  */
3699 static int spelling_size;               /* Size of the spelling stack.  */
3700
3701 /* Macros to save and restore the spelling stack around push_... functions.
3702    Alternative to SAVE_SPELLING_STACK.  */
3703
3704 #define SPELLING_DEPTH() (spelling - spelling_base)
3705 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
3706
3707 /* Push an element on the spelling stack with type KIND and assign VALUE
3708    to MEMBER.  */
3709
3710 #define PUSH_SPELLING(KIND, VALUE, MEMBER)                              \
3711 {                                                                       \
3712   int depth = SPELLING_DEPTH ();                                        \
3713                                                                         \
3714   if (depth >= spelling_size)                                           \
3715     {                                                                   \
3716       spelling_size += 10;                                              \
3717       if (spelling_base == 0)                                           \
3718         spelling_base = xmalloc (spelling_size * sizeof (struct spelling)); \
3719       else                                                              \
3720         spelling_base = xrealloc (spelling_base,                \
3721                                   spelling_size * sizeof (struct spelling)); \
3722       RESTORE_SPELLING_DEPTH (depth);                                   \
3723     }                                                                   \
3724                                                                         \
3725   spelling->kind = (KIND);                                              \
3726   spelling->MEMBER = (VALUE);                                           \
3727   spelling++;                                                           \
3728 }
3729
3730 /* Push STRING on the stack.  Printed literally.  */
3731
3732 static void
3733 push_string (const char *string)
3734 {
3735   PUSH_SPELLING (SPELLING_STRING, string, u.s);
3736 }
3737
3738 /* Push a member name on the stack.  Printed as '.' STRING.  */
3739
3740 static void
3741 push_member_name (tree decl)
3742 {
3743   const char *const string
3744     = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
3745   PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
3746 }
3747
3748 /* Push an array bounds on the stack.  Printed as [BOUNDS].  */
3749
3750 static void
3751 push_array_bounds (int bounds)
3752 {
3753   PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
3754 }
3755
3756 /* Compute the maximum size in bytes of the printed spelling.  */
3757
3758 static int
3759 spelling_length (void)
3760 {
3761   int size = 0;
3762   struct spelling *p;
3763
3764   for (p = spelling_base; p < spelling; p++)
3765     {
3766       if (p->kind == SPELLING_BOUNDS)
3767         size += 25;
3768       else
3769         size += strlen (p->u.s) + 1;
3770     }
3771
3772   return size;
3773 }
3774
3775 /* Print the spelling to BUFFER and return it.  */
3776
3777 static char *
3778 print_spelling (char *buffer)
3779 {
3780   char *d = buffer;
3781   struct spelling *p;
3782
3783   for (p = spelling_base; p < spelling; p++)
3784     if (p->kind == SPELLING_BOUNDS)
3785       {
3786         sprintf (d, "[%d]", p->u.i);
3787         d += strlen (d);
3788       }
3789     else
3790       {
3791         const char *s;
3792         if (p->kind == SPELLING_MEMBER)
3793           *d++ = '.';
3794         for (s = p->u.s; (*d = *s++); d++)
3795           ;
3796       }
3797   *d++ = '\0';
3798   return buffer;
3799 }
3800
3801 /* Issue an error message for a bad initializer component.
3802    MSGID identifies the message.
3803    The component name is taken from the spelling stack.  */
3804
3805 void
3806 error_init (const char *msgid)
3807 {
3808   char *ofwhat;
3809
3810   error ("%s", _(msgid));
3811   ofwhat = print_spelling (alloca (spelling_length () + 1));
3812   if (*ofwhat)
3813     error ("(near initialization for `%s')", ofwhat);
3814 }
3815
3816 /* Issue a pedantic warning for a bad initializer component.
3817    MSGID identifies the message.
3818    The component name is taken from the spelling stack.  */
3819
3820 void
3821 pedwarn_init (const char *msgid)
3822 {
3823   char *ofwhat;
3824
3825   pedwarn ("%s", _(msgid));
3826   ofwhat = print_spelling (alloca (spelling_length () + 1));
3827   if (*ofwhat)
3828     pedwarn ("(near initialization for `%s')", ofwhat);
3829 }
3830
3831 /* Issue a warning for a bad initializer component.
3832    MSGID identifies the message.
3833    The component name is taken from the spelling stack.  */
3834
3835 static void
3836 warning_init (const char *msgid)
3837 {
3838   char *ofwhat;
3839
3840   warning ("%s", _(msgid));
3841   ofwhat = print_spelling (alloca (spelling_length () + 1));
3842   if (*ofwhat)
3843     warning ("(near initialization for `%s')", ofwhat);
3844 }
3845 \f
3846 /* Digest the parser output INIT as an initializer for type TYPE.
3847    Return a C expression of type TYPE to represent the initial value.
3848
3849    REQUIRE_CONSTANT requests an error if non-constant initializers or
3850    elements are seen.  */
3851
3852 static tree
3853 digest_init (tree type, tree init, int require_constant)
3854 {
3855   enum tree_code code = TREE_CODE (type);
3856   tree inside_init = init;
3857
3858   if (type == error_mark_node
3859       || init == error_mark_node
3860       || TREE_TYPE (init) == error_mark_node)
3861     return error_mark_node;
3862
3863   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
3864   /* Do not use STRIP_NOPS here.  We do not want an enumerator
3865      whose value is 0 to count as a null pointer constant.  */
3866   if (TREE_CODE (init) == NON_LVALUE_EXPR)
3867     inside_init = TREE_OPERAND (init, 0);
3868
3869   inside_init = fold (inside_init);
3870
3871   /* Initialization of an array of chars from a string constant
3872      optionally enclosed in braces.  */
3873
3874   if (code == ARRAY_TYPE)
3875     {
3876       tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3877       if ((typ1 == char_type_node
3878            || typ1 == signed_char_type_node
3879            || typ1 == unsigned_char_type_node
3880            || typ1 == unsigned_wchar_type_node
3881            || typ1 == signed_wchar_type_node)
3882           && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
3883         {
3884           if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3885                          TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
3886             return inside_init;
3887
3888           if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3889                != char_type_node)
3890               && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
3891             {
3892               error_init ("char-array initialized from wide string");
3893               return error_mark_node;
3894             }
3895           if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
3896                == char_type_node)
3897               && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
3898             {
3899               error_init ("int-array initialized from non-wide string");
3900               return error_mark_node;
3901             }
3902
3903           TREE_TYPE (inside_init) = type;
3904           if (TYPE_DOMAIN (type) != 0
3905               && TYPE_SIZE (type) != 0
3906               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
3907               /* Subtract 1 (or sizeof (wchar_t))
3908                  because it's ok to ignore the terminating null char
3909                  that is counted in the length of the constant.  */
3910               && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
3911                                        TREE_STRING_LENGTH (inside_init)
3912                                        - ((TYPE_PRECISION (typ1)
3913                                            != TYPE_PRECISION (char_type_node))
3914                                           ? (TYPE_PRECISION (wchar_type_node)
3915                                              / BITS_PER_UNIT)
3916                                           : 1)))
3917             pedwarn_init ("initializer-string for array of chars is too long");
3918
3919           return inside_init;
3920         }
3921     }
3922
3923   /* Build a VECTOR_CST from a *constant* vector constructor.  If the
3924      vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
3925      below and handle as a constructor.  */
3926     if (code == VECTOR_TYPE
3927         && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT)
3928         && TREE_CONSTANT (inside_init))
3929       {
3930         if (TREE_CODE (inside_init) == VECTOR_CST
3931             && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3932                           TYPE_MAIN_VARIANT (type),
3933                           COMPARE_STRICT))
3934           return inside_init;
3935         else
3936           return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
3937       }
3938
3939   /* Any type can be initialized
3940      from an expression of the same type, optionally with braces.  */
3941
3942   if (inside_init && TREE_TYPE (inside_init) != 0
3943       && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3944                      TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
3945           || (code == ARRAY_TYPE
3946               && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3947           || (code == VECTOR_TYPE
3948               && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3949           || (code == POINTER_TYPE
3950               && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3951               && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
3952                             TREE_TYPE (type), COMPARE_STRICT))
3953           || (code == POINTER_TYPE
3954               && TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE
3955               && comptypes (TREE_TYPE (inside_init),
3956                             TREE_TYPE (type), COMPARE_STRICT))))
3957     {
3958       if (code == POINTER_TYPE)
3959         {
3960           inside_init = default_function_array_conversion (inside_init);
3961
3962           if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
3963             {
3964               error_init ("invalid use of non-lvalue array");
3965               return error_mark_node;
3966             }
3967          }
3968
3969       if (code == VECTOR_TYPE)
3970         /* Although the types are compatible, we may require a
3971            conversion.  */
3972         inside_init = convert (type, inside_init);
3973
3974       if (require_constant && !flag_isoc99
3975           && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
3976         {
3977           /* As an extension, allow initializing objects with static storage
3978              duration with compound literals (which are then treated just as
3979              the brace enclosed list they contain).  */
3980           tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3981           inside_init = DECL_INITIAL (decl);
3982         }
3983
3984       if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
3985           && TREE_CODE (inside_init) != CONSTRUCTOR)
3986         {
3987           error_init ("array initialized from non-constant array expression");
3988           return error_mark_node;
3989         }
3990
3991       if (optimize && TREE_CODE (inside_init) == VAR_DECL)
3992         inside_init = decl_constant_value_for_broken_optimization (inside_init);
3993
3994       /* Compound expressions can only occur here if -pedantic or
3995          -pedantic-errors is specified.  In the later case, we always want
3996          an error.  In the former case, we simply want a warning.  */
3997       if (require_constant && pedantic
3998           && TREE_CODE (inside_init) == COMPOUND_EXPR)
3999         {
4000           inside_init
4001             = valid_compound_expr_initializer (inside_init,
4002                                                TREE_TYPE (inside_init));
4003           if (inside_init == error_mark_node)
4004             error_init ("initializer element is not constant");
4005           else
4006             pedwarn_init ("initializer element is not constant");
4007           if (flag_pedantic_errors)
4008             inside_init = error_mark_node;
4009         }
4010       else if (require_constant
4011                && (!TREE_CONSTANT (inside_init)
4012                    /* This test catches things like `7 / 0' which
4013                       result in an expression for which TREE_CONSTANT
4014                       is true, but which is not actually something
4015                       that is a legal constant.  We really should not
4016                       be using this function, because it is a part of
4017                       the back-end.  Instead, the expression should
4018                       already have been turned into ERROR_MARK_NODE.  */
4019                    || !initializer_constant_valid_p (inside_init,
4020                                                      TREE_TYPE (inside_init))))
4021         {
4022           error_init ("initializer element is not constant");
4023           inside_init = error_mark_node;
4024         }
4025
4026       return inside_init;
4027     }
4028
4029   /* Handle scalar types, including conversions.  */
4030
4031   if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
4032       || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
4033     {
4034       /* Note that convert_for_assignment calls default_conversion
4035          for arrays and functions.  We must not call it in the
4036          case where inside_init is a null pointer constant.  */
4037       inside_init
4038         = convert_for_assignment (type, init, _("initialization"),
4039                                   NULL_TREE, NULL_TREE, 0);
4040
4041       if (require_constant && ! TREE_CONSTANT (inside_init))
4042         {
4043           error_init ("initializer element is not constant");
4044           inside_init = error_mark_node;
4045         }
4046       else if (require_constant
4047                && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
4048         {
4049           error_init ("initializer element is not computable at load time");
4050           inside_init = error_mark_node;
4051         }
4052
4053       return inside_init;
4054     }
4055
4056   /* Come here only for records and arrays.  */
4057
4058   if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
4059     {
4060       error_init ("variable-sized object may not be initialized");
4061       return error_mark_node;
4062     }
4063
4064   error_init ("invalid initializer");
4065   return error_mark_node;
4066 }
4067 \f
4068 /* Handle initializers that use braces.  */
4069
4070 /* Type of object we are accumulating a constructor for.
4071    This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE.  */
4072 static tree constructor_type;
4073
4074 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4075    left to fill.  */
4076 static tree constructor_fields;
4077
4078 /* For an ARRAY_TYPE, this is the specified index
4079    at which to store the next element we get.  */
4080 static tree constructor_index;
4081
4082 /* For an ARRAY_TYPE, this is the maximum index.  */
4083 static tree constructor_max_index;
4084
4085 /* For a RECORD_TYPE, this is the first field not yet written out.  */
4086 static tree constructor_unfilled_fields;
4087
4088 /* For an ARRAY_TYPE, this is the index of the first element
4089    not yet written out.  */
4090 static tree constructor_unfilled_index;
4091
4092 /* In a RECORD_TYPE, the byte index of the next consecutive field.
4093    This is so we can generate gaps between fields, when appropriate.  */
4094 static tree constructor_bit_index;
4095
4096 /* If we are saving up the elements rather than allocating them,
4097    this is the list of elements so far (in reverse order,
4098    most recent first).  */
4099 static tree constructor_elements;
4100
4101 /* 1 if constructor should be incrementally stored into a constructor chain,
4102    0 if all the elements should be kept in AVL tree.  */
4103 static int constructor_incremental;
4104
4105 /* 1 if so far this constructor's elements are all compile-time constants.  */
4106 static int constructor_constant;
4107
4108 /* 1 if so far this constructor's elements are all valid address constants.  */
4109 static int constructor_simple;
4110
4111 /* 1 if this constructor is erroneous so far.  */
4112 static int constructor_erroneous;
4113
4114 /* Structure for managing pending initializer elements, organized as an
4115    AVL tree.  */
4116
4117 struct init_node
4118 {
4119   struct init_node *left, *right;
4120   struct init_node *parent;
4121   int balance;
4122   tree purpose;
4123   tree value;
4124 };
4125
4126 /* Tree of pending elements at this constructor level.
4127    These are elements encountered out of order
4128    which belong at places we haven't reached yet in actually
4129    writing the output.
4130    Will never hold tree nodes across GC runs.  */
4131 static struct init_node *constructor_pending_elts;
4132
4133 /* The SPELLING_DEPTH of this constructor.  */
4134 static int constructor_depth;
4135
4136 /* 0 if implicitly pushing constructor levels is allowed.  */
4137 int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages.  */
4138
4139 static int require_constant_value;
4140 static int require_constant_elements;
4141
4142 /* DECL node for which an initializer is being read.
4143    0 means we are reading a constructor expression
4144    such as (struct foo) {...}.  */
4145 static tree constructor_decl;
4146
4147 /* start_init saves the ASMSPEC arg here for really_start_incremental_init.  */
4148 static const char *constructor_asmspec;
4149
4150 /* Nonzero if this is an initializer for a top-level decl.  */
4151 static int constructor_top_level;
4152
4153 /* Nonzero if there were any member designators in this initializer.  */
4154 static int constructor_designated;
4155
4156 /* Nesting depth of designator list.  */
4157 static int designator_depth;
4158
4159 /* Nonzero if there were diagnosed errors in this designator list.  */
4160 static int designator_errorneous;
4161
4162 \f
4163 /* This stack has a level for each implicit or explicit level of
4164    structuring in the initializer, including the outermost one.  It
4165    saves the values of most of the variables above.  */
4166
4167 struct constructor_range_stack;
4168
4169 struct constructor_stack
4170 {
4171   struct constructor_stack *next;
4172   tree type;
4173   tree fields;
4174   tree index;
4175   tree max_index;
4176   tree unfilled_index;
4177   tree unfilled_fields;
4178   tree bit_index;
4179   tree elements;
4180   struct init_node *pending_elts;
4181   int offset;
4182   int depth;
4183   /* If nonzero, this value should replace the entire
4184      constructor at this level.  */
4185   tree replacement_value;
4186   struct constructor_range_stack *range_stack;
4187   char constant;
4188   char simple;
4189   char implicit;
4190   char erroneous;
4191   char outer;
4192   char incremental;
4193   char designated;
4194 };
4195
4196 struct constructor_stack *constructor_stack;
4197
4198 /* This stack represents designators from some range designator up to
4199    the last designator in the list.  */
4200
4201 struct constructor_range_stack
4202 {
4203   struct constructor_range_stack *next, *prev;
4204   struct constructor_stack *stack;
4205   tree range_start;
4206   tree index;
4207   tree range_end;
4208   tree fields;
4209 };
4210
4211 struct constructor_range_stack *constructor_range_stack;
4212
4213 /* This stack records separate initializers that are nested.
4214    Nested initializers can't happen in ANSI C, but GNU C allows them
4215    in cases like { ... (struct foo) { ... } ... }.  */
4216
4217 struct initializer_stack
4218 {
4219   struct initializer_stack *next;
4220   tree decl;
4221   const char *asmspec;
4222   struct constructor_stack *constructor_stack;
4223   struct constructor_range_stack *constructor_range_stack;
4224   tree elements;
4225   struct spelling *spelling;
4226   struct spelling *spelling_base;
4227   int spelling_size;
4228   char top_level;
4229   char require_constant_value;
4230   char require_constant_elements;
4231 };
4232
4233 struct initializer_stack *initializer_stack;
4234 \f
4235 /* Prepare to parse and output the initializer for variable DECL.  */
4236
4237 void
4238 start_init (tree decl, tree asmspec_tree, int top_level)
4239 {
4240   const char *locus;
4241   struct initializer_stack *p = xmalloc (sizeof (struct initializer_stack));
4242   const char *asmspec = 0;
4243
4244   if (asmspec_tree)
4245     asmspec = TREE_STRING_POINTER (asmspec_tree);
4246
4247   p->decl = constructor_decl;
4248   p->asmspec = constructor_asmspec;
4249   p->require_constant_value = require_constant_value;
4250   p->require_constant_elements = require_constant_elements;
4251   p->constructor_stack = constructor_stack;
4252   p->constructor_range_stack = constructor_range_stack;
4253   p->elements = constructor_elements;
4254   p->spelling = spelling;
4255   p->spelling_base = spelling_base;
4256   p->spelling_size = spelling_size;
4257   p->top_level = constructor_top_level;
4258   p->next = initializer_stack;
4259   initializer_stack = p;
4260
4261   constructor_decl = decl;
4262   constructor_asmspec = asmspec;
4263   constructor_designated = 0;
4264   constructor_top_level = top_level;
4265
4266   if (decl != 0)
4267     {
4268       require_constant_value = TREE_STATIC (decl);
4269       require_constant_elements
4270         = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
4271            /* For a scalar, you can always use any value to initialize,
4272               even within braces.  */
4273            && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
4274                || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
4275                || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
4276                || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
4277       locus = IDENTIFIER_POINTER (DECL_NAME (decl));
4278     }
4279   else
4280     {
4281       require_constant_value = 0;
4282       require_constant_elements = 0;
4283       locus = "(anonymous)";
4284     }
4285
4286   constructor_stack = 0;
4287   constructor_range_stack = 0;
4288
4289   missing_braces_mentioned = 0;
4290
4291   spelling_base = 0;
4292   spelling_size = 0;
4293   RESTORE_SPELLING_DEPTH (0);
4294
4295   if (locus)
4296     push_string (locus);
4297 }
4298
4299 void
4300 finish_init (void)
4301 {
4302   struct initializer_stack *p = initializer_stack;
4303
4304   /* Free the whole constructor stack of this initializer.  */
4305   while (constructor_stack)
4306     {
4307       struct constructor_stack *q = constructor_stack;
4308       constructor_stack = q->next;
4309       free (q);
4310     }
4311
4312   if (constructor_range_stack)
4313     abort ();
4314
4315   /* Pop back to the data of the outer initializer (if any).  */
4316   free (spelling_base);
4317   
4318   constructor_decl = p->decl;
4319   constructor_asmspec = p->asmspec;
4320   require_constant_value = p->require_constant_value;
4321   require_constant_elements = p->require_constant_elements;
4322   constructor_stack = p->constructor_stack;
4323   constructor_range_stack = p->constructor_range_stack;
4324   constructor_elements = p->elements;
4325   spelling = p->spelling;
4326   spelling_base = p->spelling_base;
4327   spelling_size = p->spelling_size;
4328   constructor_top_level = p->top_level;
4329   initializer_stack = p->next;
4330   free (p);
4331 }
4332 \f
4333 /* Call here when we see the initializer is surrounded by braces.
4334    This is instead of a call to push_init_level;
4335    it is matched by a call to pop_init_level.
4336
4337    TYPE is the type to initialize, for a constructor expression.
4338    For an initializer for a decl, TYPE is zero.  */
4339
4340 void
4341 really_start_incremental_init (tree type)
4342 {
4343   struct constructor_stack *p = xmalloc (sizeof (struct constructor_stack));
4344
4345   if (type == 0)
4346     type = TREE_TYPE (constructor_decl);
4347
4348   if ((*targetm.vector_opaque_p) (type))
4349     error ("opaque vector types cannot be initialized");
4350
4351   p->type = constructor_type;
4352   p->fields = constructor_fields;
4353   p->index = constructor_index;
4354   p->max_index = constructor_max_index;
4355   p->unfilled_index = constructor_unfilled_index;
4356   p->unfilled_fields = constructor_unfilled_fields;
4357   p->bit_index = constructor_bit_index;
4358   p->elements = constructor_elements;
4359   p->constant = constructor_constant;
4360   p->simple = constructor_simple;
4361   p->erroneous = constructor_erroneous;
4362   p->pending_elts = constructor_pending_elts;
4363   p->depth = constructor_depth;
4364   p->replacement_value = 0;
4365   p->implicit = 0;
4366   p->range_stack = 0;
4367   p->outer = 0;
4368   p->incremental = constructor_incremental;
4369   p->designated = constructor_designated;
4370   p->next = 0;
4371   constructor_stack = p;
4372
4373   constructor_constant = 1;
4374   constructor_simple = 1;
4375   constructor_depth = SPELLING_DEPTH ();
4376   constructor_elements = 0;
4377   constructor_pending_elts = 0;
4378   constructor_type = type;
4379   constructor_incremental = 1;
4380   constructor_designated = 0;
4381   designator_depth = 0;
4382   designator_errorneous = 0;
4383
4384   if (TREE_CODE (constructor_type) == RECORD_TYPE
4385       || TREE_CODE (constructor_type) == UNION_TYPE)
4386     {
4387       constructor_fields = TYPE_FIELDS (constructor_type);
4388       /* Skip any nameless bit fields at the beginning.  */
4389       while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4390              && DECL_NAME (constructor_fields) == 0)
4391         constructor_fields = TREE_CHAIN (constructor_fields);
4392
4393       constructor_unfilled_fields = constructor_fields;
4394       constructor_bit_index = bitsize_zero_node;
4395     }
4396   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4397     {
4398       if (TYPE_DOMAIN (constructor_type))
4399         {
4400           constructor_max_index
4401             = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4402
4403           /* Detect non-empty initializations of zero-length arrays.  */
4404           if (constructor_max_index == NULL_TREE
4405               && TYPE_SIZE (constructor_type))
4406             constructor_max_index = build_int_2 (-1, -1);
4407
4408           /* constructor_max_index needs to be an INTEGER_CST.  Attempts
4409              to initialize VLAs will cause a proper error; avoid tree
4410              checking errors as well by setting a safe value.  */
4411           if (constructor_max_index
4412               && TREE_CODE (constructor_max_index) != INTEGER_CST)
4413             constructor_max_index = build_int_2 (-1, -1);
4414
4415           constructor_index
4416             = convert (bitsizetype,
4417                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4418         }
4419       else
4420         constructor_index = bitsize_zero_node;
4421
4422       constructor_unfilled_index = constructor_index;
4423     }
4424   else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4425     {
4426       /* Vectors are like simple fixed-size arrays.  */
4427       constructor_max_index =
4428         build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4429       constructor_index = convert (bitsizetype, bitsize_zero_node);
4430       constructor_unfilled_index = constructor_index;
4431     }
4432   else
4433     {
4434       /* Handle the case of int x = {5}; */
4435       constructor_fields = constructor_type;
4436       constructor_unfilled_fields = constructor_type;
4437     }
4438 }
4439 \f
4440 /* Push down into a subobject, for initialization.
4441    If this is for an explicit set of braces, IMPLICIT is 0.
4442    If it is because the next element belongs at a lower level,
4443    IMPLICIT is 1 (or 2 if the push is because of designator list).  */
4444
4445 void
4446 push_init_level (int implicit)
4447 {
4448   struct constructor_stack *p;
4449   tree value = NULL_TREE;
4450
4451   /* If we've exhausted any levels that didn't have braces,
4452      pop them now.  */
4453   while (constructor_stack->implicit)
4454     {
4455       if ((TREE_CODE (constructor_type) == RECORD_TYPE
4456            || TREE_CODE (constructor_type) == UNION_TYPE)
4457           && constructor_fields == 0)
4458         process_init_element (pop_init_level (1));
4459       else if (TREE_CODE (constructor_type) == ARRAY_TYPE
4460                && constructor_max_index
4461                && tree_int_cst_lt (constructor_max_index, constructor_index))
4462         process_init_element (pop_init_level (1));
4463       else
4464         break;
4465     }
4466
4467   /* Unless this is an explicit brace, we need to preserve previous
4468      content if any.  */
4469   if (implicit)
4470     {
4471       if ((TREE_CODE (constructor_type) == RECORD_TYPE
4472            || TREE_CODE (constructor_type) == UNION_TYPE)
4473           && constructor_fields)
4474         value = find_init_member (constructor_fields);
4475       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4476         value = find_init_member (constructor_index);
4477     }
4478
4479   p = xmalloc (sizeof (struct constructor_stack));
4480   p->type = constructor_type;
4481   p->fields = constructor_fields;
4482   p->index = constructor_index;
4483   p->max_index = constructor_max_index;
4484   p->unfilled_index = constructor_unfilled_index;
4485   p->unfilled_fields = constructor_unfilled_fields;
4486   p->bit_index = constructor_bit_index;
4487   p->elements = constructor_elements;
4488   p->constant = constructor_constant;
4489   p->simple = constructor_simple;
4490   p->erroneous = constructor_erroneous;
4491   p->pending_elts = constructor_pending_elts;
4492   p->depth = constructor_depth;
4493   p->replacement_value = 0;
4494   p->implicit = implicit;
4495   p->outer = 0;
4496   p->incremental = constructor_incremental;
4497   p->designated = constructor_designated;
4498   p->next = constructor_stack;
4499   p->range_stack = 0;
4500   constructor_stack = p;
4501
4502   constructor_constant = 1;
4503   constructor_simple = 1;
4504   constructor_depth = SPELLING_DEPTH ();
4505   constructor_elements = 0;
4506   constructor_incremental = 1;
4507   constructor_designated = 0;
4508   constructor_pending_elts = 0;
4509   if (!implicit)
4510     {
4511       p->range_stack = constructor_range_stack;
4512       constructor_range_stack = 0;
4513       designator_depth = 0;
4514       designator_errorneous = 0;
4515     }
4516
4517   /* Don't die if an entire brace-pair level is superfluous
4518      in the containing level.  */
4519   if (constructor_type == 0)
4520     ;
4521   else if (TREE_CODE (constructor_type) == RECORD_TYPE
4522            || TREE_CODE (constructor_type) == UNION_TYPE)
4523     {
4524       /* Don't die if there are extra init elts at the end.  */
4525       if (constructor_fields == 0)
4526         constructor_type = 0;
4527       else
4528         {
4529           constructor_type = TREE_TYPE (constructor_fields);
4530           push_member_name (constructor_fields);
4531           constructor_depth++;
4532         }
4533     }
4534   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4535     {
4536       constructor_type = TREE_TYPE (constructor_type);
4537       push_array_bounds (tree_low_cst (constructor_index, 0));
4538       constructor_depth++;
4539     }
4540
4541   if (constructor_type == 0)
4542     {
4543       error_init ("extra brace group at end of initializer");
4544       constructor_fields = 0;
4545       constructor_unfilled_fields = 0;
4546       return;
4547     }
4548
4549   if (value && TREE_CODE (value) == CONSTRUCTOR)
4550     {
4551       constructor_constant = TREE_CONSTANT (value);
4552       constructor_simple = TREE_STATIC (value);
4553       constructor_elements = CONSTRUCTOR_ELTS (value);
4554       if (constructor_elements
4555           && (TREE_CODE (constructor_type) == RECORD_TYPE
4556               || TREE_CODE (constructor_type) == ARRAY_TYPE))
4557         set_nonincremental_init ();
4558     }
4559
4560   if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
4561     {
4562       missing_braces_mentioned = 1;
4563       warning_init ("missing braces around initializer");
4564     }
4565
4566   if (TREE_CODE (constructor_type) == RECORD_TYPE
4567            || TREE_CODE (constructor_type) == UNION_TYPE)
4568     {
4569       constructor_fields = TYPE_FIELDS (constructor_type);
4570       /* Skip any nameless bit fields at the beginning.  */
4571       while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
4572              && DECL_NAME (constructor_fields) == 0)
4573         constructor_fields = TREE_CHAIN (constructor_fields);
4574
4575       constructor_unfilled_fields = constructor_fields;
4576       constructor_bit_index = bitsize_zero_node;
4577     }
4578   else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
4579     {
4580       /* Vectors are like simple fixed-size arrays.  */
4581       constructor_max_index =
4582         build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
4583       constructor_index = convert (bitsizetype, integer_zero_node);
4584       constructor_unfilled_index = constructor_index;
4585     }
4586   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4587     {
4588       if (TYPE_DOMAIN (constructor_type))
4589         {
4590           constructor_max_index
4591             = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
4592
4593           /* Detect non-empty initializations of zero-length arrays.  */
4594           if (constructor_max_index == NULL_TREE
4595               && TYPE_SIZE (constructor_type))
4596             constructor_max_index = build_int_2 (-1, -1);
4597
4598           /* constructor_max_index needs to be an INTEGER_CST.  Attempts
4599              to initialize VLAs will cause a proper error; avoid tree
4600              checking errors as well by setting a safe value.  */
4601           if (constructor_max_index
4602               && TREE_CODE (constructor_max_index) != INTEGER_CST)
4603             constructor_max_index = build_int_2 (-1, -1);
4604
4605           constructor_index
4606             = convert (bitsizetype,
4607                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4608         }
4609       else
4610         constructor_index = bitsize_zero_node;
4611
4612       constructor_unfilled_index = constructor_index;
4613       if (value && TREE_CODE (value) == STRING_CST)
4614         {
4615           /* We need to split the char/wchar array into individual
4616              characters, so that we don't have to special case it
4617              everywhere.  */
4618           set_nonincremental_init_from_string (value);
4619         }
4620     }
4621   else
4622     {
4623       warning_init ("braces around scalar initializer");
4624       constructor_fields = constructor_type;
4625       constructor_unfilled_fields = constructor_type;
4626     }
4627 }
4628
4629 /* At the end of an implicit or explicit brace level,
4630    finish up that level of constructor.
4631    If we were outputting the elements as they are read, return 0
4632    from inner levels (process_init_element ignores that),
4633    but return error_mark_node from the outermost level
4634    (that's what we want to put in DECL_INITIAL).
4635    Otherwise, return a CONSTRUCTOR expression.  */
4636
4637 tree
4638 pop_init_level (int implicit)
4639 {
4640   struct constructor_stack *p;
4641   tree constructor = 0;
4642
4643   if (implicit == 0)
4644     {
4645       /* When we come to an explicit close brace,
4646          pop any inner levels that didn't have explicit braces.  */
4647       while (constructor_stack->implicit)
4648         process_init_element (pop_init_level (1));
4649
4650       if (constructor_range_stack)
4651         abort ();
4652     }
4653
4654   p = constructor_stack;
4655
4656   /* Error for initializing a flexible array member, or a zero-length
4657      array member in an inappropriate context.  */
4658   if (constructor_type && constructor_fields
4659       && TREE_CODE (constructor_type) == ARRAY_TYPE
4660       && TYPE_DOMAIN (constructor_type)
4661       && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
4662     {
4663       /* Silently discard empty initializations.  The parser will
4664          already have pedwarned for empty brackets.  */
4665       if (integer_zerop (constructor_unfilled_index))
4666         constructor_type = NULL_TREE;
4667       else if (! TYPE_SIZE (constructor_type))
4668         {
4669           if (constructor_depth > 2)
4670             error_init ("initialization of flexible array member in a nested context");
4671           else if (pedantic)
4672             pedwarn_init ("initialization of a flexible array member");
4673
4674           /* We have already issued an error message for the existence
4675              of a flexible array member not at the end of the structure.
4676              Discard the initializer so that we do not abort later.  */
4677           if (TREE_CHAIN (constructor_fields) != NULL_TREE)
4678             constructor_type = NULL_TREE;
4679         }
4680       else
4681         /* Zero-length arrays are no longer special, so we should no longer
4682            get here.  */
4683         abort ();
4684     }
4685
4686   /* Warn when some struct elements are implicitly initialized to zero.  */
4687   if (extra_warnings
4688       && constructor_type
4689       && TREE_CODE (constructor_type) == RECORD_TYPE
4690       && constructor_unfilled_fields)
4691     {
4692         /* Do not warn for flexible array members or zero-length arrays.  */
4693         while (constructor_unfilled_fields
4694                && (! DECL_SIZE (constructor_unfilled_fields)
4695                    || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
4696           constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
4697
4698         /* Do not warn if this level of the initializer uses member
4699            designators; it is likely to be deliberate.  */
4700         if (constructor_unfilled_fields && !constructor_designated)
4701           {
4702             push_member_name (constructor_unfilled_fields);
4703             warning_init ("missing initializer");
4704             RESTORE_SPELLING_DEPTH (constructor_depth);
4705           }
4706     }
4707
4708   /* Now output all pending elements.  */
4709   constructor_incremental = 1;
4710   output_pending_init_elements (1);
4711
4712   /* Pad out the end of the structure.  */
4713   if (p->replacement_value)
4714     /* If this closes a superfluous brace pair,
4715        just pass out the element between them.  */
4716     constructor = p->replacement_value;
4717   else if (constructor_type == 0)
4718     ;
4719   else if (TREE_CODE (constructor_type) != RECORD_TYPE
4720            && TREE_CODE (constructor_type) != UNION_TYPE
4721            && TREE_CODE (constructor_type) != ARRAY_TYPE
4722            && TREE_CODE (constructor_type) != VECTOR_TYPE)
4723     {
4724       /* A nonincremental scalar initializer--just return
4725          the element, after verifying there is just one.  */
4726       if (constructor_elements == 0)
4727         {
4728           if (!constructor_erroneous)
4729             error_init ("empty scalar initializer");
4730           constructor = error_mark_node;
4731         }
4732       else if (TREE_CHAIN (constructor_elements) != 0)
4733         {
4734           error_init ("extra elements in scalar initializer");
4735           constructor = TREE_VALUE (constructor_elements);
4736         }
4737       else
4738         constructor = TREE_VALUE (constructor_elements);
4739     }
4740   else
4741     {
4742       if (constructor_erroneous)
4743         constructor = error_mark_node;
4744       else
4745         {
4746           constructor = build_constructor (constructor_type,
4747                                            nreverse (constructor_elements));
4748           if (constructor_constant)
4749             TREE_CONSTANT (constructor) = 1;
4750           if (constructor_constant && constructor_simple)
4751             TREE_STATIC (constructor) = 1;
4752         }
4753     }
4754
4755   constructor_type = p->type;
4756   constructor_fields = p->fields;
4757   constructor_index = p->index;
4758   constructor_max_index = p->max_index;
4759   constructor_unfilled_index = p->unfilled_index;
4760   constructor_unfilled_fields = p->unfilled_fields;
4761   constructor_bit_index = p->bit_index;
4762   constructor_elements = p->elements;
4763   constructor_constant = p->constant;
4764   constructor_simple = p->simple;
4765   constructor_erroneous = p->erroneous;
4766   constructor_incremental = p->incremental;
4767   constructor_designated = p->designated;
4768   constructor_pending_elts = p->pending_elts;
4769   constructor_depth = p->depth;
4770   if (!p->implicit)
4771     constructor_range_stack = p->range_stack;
4772   RESTORE_SPELLING_DEPTH (constructor_depth);
4773
4774   constructor_stack = p->next;
4775   free (p);
4776
4777   if (constructor == 0)
4778     {
4779       if (constructor_stack == 0)
4780         return error_mark_node;
4781       return NULL_TREE;
4782     }
4783   return constructor;
4784 }
4785
4786 /* Common handling for both array range and field name designators.
4787    ARRAY argument is nonzero for array ranges.  Returns zero for success.  */
4788
4789 static int
4790 set_designator (int array)
4791 {
4792   tree subtype;
4793   enum tree_code subcode;
4794
4795   /* Don't die if an entire brace-pair level is superfluous
4796      in the containing level.  */
4797   if (constructor_type == 0)
4798     return 1;
4799
4800   /* If there were errors in this designator list already, bail out silently.  */
4801   if (designator_errorneous)
4802     return 1;
4803
4804   if (!designator_depth)
4805     {
4806       if (constructor_range_stack)
4807         abort ();
4808
4809       /* Designator list starts at the level of closest explicit
4810          braces.  */
4811       while (constructor_stack->implicit)
4812         process_init_element (pop_init_level (1));
4813       constructor_designated = 1;
4814       return 0;
4815     }
4816
4817   if (constructor_no_implicit)
4818     {
4819       error_init ("initialization designators may not nest");
4820       return 1;
4821     }
4822
4823   if (TREE_CODE (constructor_type) == RECORD_TYPE
4824       || TREE_CODE (constructor_type) == UNION_TYPE)
4825     {
4826       subtype = TREE_TYPE (constructor_fields);
4827       if (subtype != error_mark_node)
4828         subtype = TYPE_MAIN_VARIANT (subtype);
4829     }
4830   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4831     {
4832       subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
4833     }
4834   else
4835     abort ();
4836
4837   subcode = TREE_CODE (subtype);
4838   if (array && subcode != ARRAY_TYPE)
4839     {
4840       error_init ("array index in non-array initializer");
4841       return 1;
4842     }
4843   else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
4844     {
4845       error_init ("field name not in record or union initializer");
4846       return 1;
4847     }
4848
4849   constructor_designated = 1;
4850   push_init_level (2);
4851   return 0;
4852 }
4853
4854 /* If there are range designators in designator list, push a new designator
4855    to constructor_range_stack.  RANGE_END is end of such stack range or
4856    NULL_TREE if there is no range designator at this level.  */
4857
4858 static void
4859 push_range_stack (tree range_end)
4860 {
4861   struct constructor_range_stack *p;
4862
4863   p = ggc_alloc (sizeof (struct constructor_range_stack));
4864   p->prev = constructor_range_stack;
4865   p->next = 0;
4866   p->fields = constructor_fields;
4867   p->range_start = constructor_index;
4868   p->index = constructor_index;
4869   p->stack = constructor_stack;
4870   p->range_end = range_end;
4871   if (constructor_range_stack)
4872     constructor_range_stack->next = p;
4873   constructor_range_stack = p;
4874 }
4875
4876 /* Within an array initializer, specify the next index to be initialized.
4877    FIRST is that index.  If LAST is nonzero, then initialize a range
4878    of indices, running from FIRST through LAST.  */
4879
4880 void
4881 set_init_index (tree first, tree last)
4882 {
4883   if (set_designator (1))
4884     return;
4885
4886   designator_errorneous = 1;
4887
4888   while ((TREE_CODE (first) == NOP_EXPR
4889           || TREE_CODE (first) == CONVERT_EXPR
4890           || TREE_CODE (first) == NON_LVALUE_EXPR)
4891          && (TYPE_MODE (TREE_TYPE (first))
4892              == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
4893     first = TREE_OPERAND (first, 0);
4894
4895   if (last)
4896     while ((TREE_CODE (last) == NOP_EXPR
4897             || TREE_CODE (last) == CONVERT_EXPR
4898             || TREE_CODE (last) == NON_LVALUE_EXPR)
4899            && (TYPE_MODE (TREE_TYPE (last))
4900                == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
4901       last = TREE_OPERAND (last, 0);
4902
4903   if (TREE_CODE (first) != INTEGER_CST)
4904     error_init ("nonconstant array index in initializer");
4905   else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
4906     error_init ("nonconstant array index in initializer");
4907   else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
4908     error_init ("array index in non-array initializer");
4909   else if (tree_int_cst_sgn (first) == -1)
4910     error_init ("array index in initializer exceeds array bounds");
4911   else if (constructor_max_index
4912            && tree_int_cst_lt (constructor_max_index, first))
4913     error_init ("array index in initializer exceeds array bounds");
4914   else
4915     {
4916       constructor_index = convert (bitsizetype, first);
4917
4918       if (last)
4919         {
4920           if (tree_int_cst_equal (first, last))
4921             last = 0;
4922           else if (tree_int_cst_lt (last, first))
4923             {
4924               error_init ("empty index range in initializer");
4925               last = 0;
4926             }
4927           else
4928             {
4929               last = convert (bitsizetype, last);
4930               if (constructor_max_index != 0
4931                   && tree_int_cst_lt (constructor_max_index, last))
4932                 {
4933                   error_init ("array index range in initializer exceeds array bounds");
4934                   last = 0;
4935                 }
4936             }
4937         }
4938
4939       designator_depth++;
4940       designator_errorneous = 0;
4941       if (constructor_range_stack || last)
4942         push_range_stack (last);
4943     }
4944 }
4945
4946 /* Within a struct initializer, specify the next field to be initialized.  */
4947
4948 void
4949 set_init_label (tree fieldname)
4950 {
4951   tree tail;
4952
4953   if (set_designator (0))
4954     return;
4955
4956   designator_errorneous = 1;
4957
4958   if (TREE_CODE (constructor_type) != RECORD_TYPE
4959       && TREE_CODE (constructor_type) != UNION_TYPE)
4960     {
4961       error_init ("field name not in record or union initializer");
4962       return;
4963     }
4964
4965   for (tail = TYPE_FIELDS (constructor_type); tail;
4966        tail = TREE_CHAIN (tail))
4967     {
4968       if (DECL_NAME (tail) == fieldname)
4969         break;
4970     }
4971
4972   if (tail == 0)
4973     error ("unknown field `%s' specified in initializer",
4974            IDENTIFIER_POINTER (fieldname));
4975   else
4976     {
4977       constructor_fields = tail;
4978       designator_depth++;
4979       designator_errorneous = 0;
4980       if (constructor_range_stack)
4981         push_range_stack (NULL_TREE);
4982     }
4983 }
4984 \f
4985 /* Add a new initializer to the tree of pending initializers.  PURPOSE
4986    identifies the initializer, either array index or field in a structure.
4987    VALUE is the value of that index or field.  */
4988
4989 static void
4990 add_pending_init (tree purpose, tree value)
4991 {
4992   struct init_node *p, **q, *r;
4993
4994   q = &constructor_pending_elts;
4995   p = 0;
4996
4997   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
4998     {
4999       while (*q != 0)
5000         {
5001           p = *q;
5002           if (tree_int_cst_lt (purpose, p->purpose))
5003             q = &p->left;
5004           else if (tree_int_cst_lt (p->purpose, purpose))
5005             q = &p->right;
5006           else
5007             {
5008               if (TREE_SIDE_EFFECTS (p->value))
5009                 warning_init ("initialized field with side-effects overwritten");
5010               p->value = value;
5011               return;
5012             }
5013         }
5014     }
5015   else
5016     {
5017       tree bitpos;
5018
5019       bitpos = bit_position (purpose);
5020       while (*q != NULL)
5021         {
5022           p = *q;
5023           if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5024             q = &p->left;
5025           else if (p->purpose != purpose)
5026             q = &p->right;
5027           else
5028             {
5029               if (TREE_SIDE_EFFECTS (p->value))
5030                 warning_init ("initialized field with side-effects overwritten");
5031               p->value = value;
5032               return;
5033             }
5034         }
5035     }
5036
5037   r = ggc_alloc (sizeof (struct init_node));
5038   r->purpose = purpose;
5039   r->value = value;
5040
5041   *q = r;
5042   r->parent = p;
5043   r->left = 0;
5044   r->right = 0;
5045   r->balance = 0;
5046
5047   while (p)
5048     {
5049       struct init_node *s;
5050
5051       if (r == p->left)
5052         {
5053           if (p->balance == 0)
5054             p->balance = -1;
5055           else if (p->balance < 0)
5056             {
5057               if (r->balance < 0)
5058                 {
5059                   /* L rotation.  */
5060                   p->left = r->right;
5061                   if (p->left)
5062                     p->left->parent = p;
5063                   r->right = p;
5064
5065                   p->balance = 0;
5066                   r->balance = 0;
5067
5068                   s = p->parent;
5069                   p->parent = r;
5070                   r->parent = s;
5071                   if (s)
5072                     {
5073                       if (s->left == p)
5074                         s->left = r;
5075                       else
5076                         s->right = r;
5077                     }
5078                   else
5079                     constructor_pending_elts = r;
5080                 }
5081               else
5082                 {
5083                   /* LR rotation.  */
5084                   struct init_node *t = r->right;
5085
5086                   r->right = t->left;
5087                   if (r->right)
5088                     r->right->parent = r;
5089                   t->left = r;
5090
5091                   p->left = t->right;
5092                   if (p->left)
5093                     p->left->parent = p;
5094                   t->right = p;
5095
5096                   p->balance = t->balance < 0;
5097                   r->balance = -(t->balance > 0);
5098                   t->balance = 0;
5099
5100                   s = p->parent;
5101                   p->parent = t;
5102                   r->parent = t;
5103                   t->parent = s;
5104                   if (s)
5105                     {
5106                       if (s->left == p)
5107                         s->left = t;
5108                       else
5109                         s->right = t;
5110                     }
5111                   else
5112                     constructor_pending_elts = t;
5113                 }
5114               break;
5115             }
5116           else
5117             {
5118               /* p->balance == +1; growth of left side balances the node.  */
5119               p->balance = 0;
5120               break;
5121             }
5122         }
5123       else /* r == p->right */
5124         {
5125           if (p->balance == 0)
5126             /* Growth propagation from right side.  */
5127             p->balance++;
5128           else if (p->balance > 0)
5129             {
5130               if (r->balance > 0)
5131                 {
5132                   /* R rotation.  */
5133                   p->right = r->left;
5134                   if (p->right)
5135                     p->right->parent = p;
5136                   r->left = p;
5137
5138                   p->balance = 0;
5139                   r->balance = 0;
5140
5141                   s = p->parent;
5142                   p->parent = r;
5143                   r->parent = s;
5144                   if (s)
5145                     {
5146                       if (s->left == p)
5147                         s->left = r;
5148                       else
5149                         s->right = r;
5150                     }
5151                   else
5152                     constructor_pending_elts = r;
5153                 }
5154               else /* r->balance == -1 */
5155                 {
5156                   /* RL rotation */
5157                   struct init_node *t = r->left;
5158
5159                   r->left = t->right;
5160                   if (r->left)
5161                     r->left->parent = r;
5162                   t->right = r;
5163
5164                   p->right = t->left;
5165                   if (p->right)
5166                     p->right->parent = p;
5167                   t->left = p;
5168
5169                   r->balance = (t->balance < 0);
5170                   p->balance = -(t->balance > 0);
5171                   t->balance = 0;
5172
5173                   s = p->parent;
5174                   p->parent = t;
5175                   r->parent = t;
5176                   t->parent = s;
5177                   if (s)
5178                     {
5179                       if (s->left == p)
5180                         s->left = t;
5181                       else
5182                         s->right = t;
5183                     }
5184                   else
5185                     constructor_pending_elts = t;
5186                 }
5187               break;
5188             }
5189           else
5190             {
5191               /* p->balance == -1; growth of right side balances the node.  */
5192               p->balance = 0;
5193               break;
5194             }
5195         }
5196
5197       r = p;
5198       p = p->parent;
5199     }
5200 }
5201
5202 /* Build AVL tree from a sorted chain.  */
5203
5204 static void
5205 set_nonincremental_init (void)
5206 {
5207   tree chain;
5208
5209   if (TREE_CODE (constructor_type) != RECORD_TYPE
5210       && TREE_CODE (constructor_type) != ARRAY_TYPE)
5211     return;
5212
5213   for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
5214     add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
5215   constructor_elements = 0;
5216   if (TREE_CODE (constructor_type) == RECORD_TYPE)
5217     {
5218       constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
5219       /* Skip any nameless bit fields at the beginning.  */
5220       while (constructor_unfilled_fields != 0
5221              && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5222              && DECL_NAME (constructor_unfilled_fields) == 0)
5223         constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
5224
5225     }
5226   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5227     {
5228       if (TYPE_DOMAIN (constructor_type))
5229         constructor_unfilled_index
5230             = convert (bitsizetype,
5231                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
5232       else
5233         constructor_unfilled_index = bitsize_zero_node;
5234     }
5235   constructor_incremental = 0;
5236 }
5237
5238 /* Build AVL tree from a string constant.  */
5239
5240 static void
5241 set_nonincremental_init_from_string (tree str)
5242 {
5243   tree value, purpose, type;
5244   HOST_WIDE_INT val[2];
5245   const char *p, *end;
5246   int byte, wchar_bytes, charwidth, bitpos;
5247
5248   if (TREE_CODE (constructor_type) != ARRAY_TYPE)
5249     abort ();
5250
5251   if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5252       == TYPE_PRECISION (char_type_node))
5253     wchar_bytes = 1;
5254   else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
5255            == TYPE_PRECISION (wchar_type_node))
5256     wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
5257   else
5258     abort ();
5259
5260   charwidth = TYPE_PRECISION (char_type_node);
5261   type = TREE_TYPE (constructor_type);
5262   p = TREE_STRING_POINTER (str);
5263   end = p + TREE_STRING_LENGTH (str);
5264
5265   for (purpose = bitsize_zero_node;
5266        p < end && !tree_int_cst_lt (constructor_max_index, purpose);
5267        purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
5268     {
5269       if (wchar_bytes == 1)
5270         {
5271           val[1] = (unsigned char) *p++;
5272           val[0] = 0;
5273         }
5274       else
5275         {
5276           val[0] = 0;
5277           val[1] = 0;
5278           for (byte = 0; byte < wchar_bytes; byte++)
5279             {
5280               if (BYTES_BIG_ENDIAN)
5281                 bitpos = (wchar_bytes - byte - 1) * charwidth;
5282               else
5283                 bitpos = byte * charwidth;
5284               val[bitpos < HOST_BITS_PER_WIDE_INT]
5285                 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
5286                    << (bitpos % HOST_BITS_PER_WIDE_INT);
5287             }
5288         }
5289
5290       if (!TREE_UNSIGNED (type))
5291         {
5292           bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
5293           if (bitpos < HOST_BITS_PER_WIDE_INT)
5294             {
5295               if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
5296                 {
5297                   val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
5298                   val[0] = -1;
5299                 }
5300             }
5301           else if (bitpos == HOST_BITS_PER_WIDE_INT)
5302             {
5303               if (val[1] < 0)
5304                 val[0] = -1;
5305             }
5306           else if (val[0] & (((HOST_WIDE_INT) 1)
5307                              << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
5308             val[0] |= ((HOST_WIDE_INT) -1)
5309                       << (bitpos - HOST_BITS_PER_WIDE_INT);
5310         }
5311
5312       value = build_int_2 (val[1], val[0]);
5313       TREE_TYPE (value) = type;
5314       add_pending_init (purpose, value);
5315     }
5316
5317   constructor_incremental = 0;
5318 }
5319
5320 /* Return value of FIELD in pending initializer or zero if the field was
5321    not initialized yet.  */
5322
5323 static tree
5324 find_init_member (tree field)
5325 {
5326   struct init_node *p;
5327
5328   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5329     {
5330       if (constructor_incremental
5331           && tree_int_cst_lt (field, constructor_unfilled_index))
5332         set_nonincremental_init ();
5333
5334       p = constructor_pending_elts;
5335       while (p)
5336         {
5337           if (tree_int_cst_lt (field, p->purpose))
5338             p = p->left;
5339           else if (tree_int_cst_lt (p->purpose, field))
5340             p = p->right;
5341           else
5342             return p->value;
5343         }
5344     }
5345   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5346     {
5347       tree bitpos = bit_position (field);
5348
5349       if (constructor_incremental
5350           && (!constructor_unfilled_fields
5351               || tree_int_cst_lt (bitpos,
5352                                   bit_position (constructor_unfilled_fields))))
5353         set_nonincremental_init ();
5354
5355       p = constructor_pending_elts;
5356       while (p)
5357         {
5358           if (field == p->purpose)
5359             return p->value;
5360           else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
5361             p = p->left;
5362           else
5363             p = p->right;
5364         }
5365     }
5366   else if (TREE_CODE (constructor_type) == UNION_TYPE)
5367     {
5368       if (constructor_elements
5369           && TREE_PURPOSE (constructor_elements) == field)
5370         return TREE_VALUE (constructor_elements);
5371     }
5372   return 0;
5373 }
5374
5375 /* "Output" the next constructor element.
5376    At top level, really output it to assembler code now.
5377    Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
5378    TYPE is the data type that the containing data type wants here.
5379    FIELD is the field (a FIELD_DECL) or the index that this element fills.
5380
5381    PENDING if non-nil means output pending elements that belong
5382    right after this element.  (PENDING is normally 1;
5383    it is 0 while outputting pending elements, to avoid recursion.)  */
5384
5385 static void
5386 output_init_element (tree value, tree type, tree field, int pending)
5387 {
5388   if (type == error_mark_node)
5389     {
5390       constructor_erroneous = 1;
5391       return;
5392     }
5393   if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5394       || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5395           && !(TREE_CODE (value) == STRING_CST
5396                && TREE_CODE (type) == ARRAY_TYPE
5397                && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
5398           && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
5399                          TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
5400     value = default_conversion (value);
5401
5402   if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
5403       && require_constant_value && !flag_isoc99 && pending)
5404     {
5405       /* As an extension, allow initializing objects with static storage
5406          duration with compound literals (which are then treated just as
5407          the brace enclosed list they contain).  */
5408       tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
5409       value = DECL_INITIAL (decl);
5410     }
5411
5412   if (value == error_mark_node)
5413     constructor_erroneous = 1;
5414   else if (!TREE_CONSTANT (value))
5415     constructor_constant = 0;
5416   else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
5417            || ((TREE_CODE (constructor_type) == RECORD_TYPE
5418                 || TREE_CODE (constructor_type) == UNION_TYPE)
5419                && DECL_C_BIT_FIELD (field)
5420                && TREE_CODE (value) != INTEGER_CST))
5421     constructor_simple = 0;
5422
5423   if (require_constant_value && ! TREE_CONSTANT (value))
5424     {
5425       error_init ("initializer element is not constant");
5426       value = error_mark_node;
5427     }
5428   else if (require_constant_elements
5429            && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
5430     pedwarn ("initializer element is not computable at load time");
5431
5432   /* If this field is empty (and not at the end of structure),
5433      don't do anything other than checking the initializer.  */
5434   if (field
5435       && (TREE_TYPE (field) == error_mark_node
5436           || (COMPLETE_TYPE_P (TREE_TYPE (field))
5437               && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
5438               && (TREE_CODE (constructor_type) == ARRAY_TYPE
5439                   || TREE_CHAIN (field)))))
5440     return;
5441
5442   value = digest_init (type, value, require_constant_value);
5443   if (value == error_mark_node)
5444     {
5445       constructor_erroneous = 1;
5446       return;
5447     }
5448
5449   /* If this element doesn't come next in sequence,
5450      put it on constructor_pending_elts.  */
5451   if (TREE_CODE (constructor_type) == ARRAY_TYPE
5452       && (!constructor_incremental
5453           || !tree_int_cst_equal (field, constructor_unfilled_index)))
5454     {
5455       if (constructor_incremental
5456           && tree_int_cst_lt (field, constructor_unfilled_index))
5457         set_nonincremental_init ();
5458
5459       add_pending_init (field, value);
5460       return;
5461     }
5462   else if (TREE_CODE (constructor_type) == RECORD_TYPE
5463            && (!constructor_incremental
5464                || field != constructor_unfilled_fields))
5465     {
5466       /* We do this for records but not for unions.  In a union,
5467          no matter which field is specified, it can be initialized
5468          right away since it starts at the beginning of the union.  */
5469       if (constructor_incremental)
5470         {
5471           if (!constructor_unfilled_fields)
5472             set_nonincremental_init ();
5473           else
5474             {
5475               tree bitpos, unfillpos;
5476
5477               bitpos = bit_position (field);
5478               unfillpos = bit_position (constructor_unfilled_fields);
5479
5480               if (tree_int_cst_lt (bitpos, unfillpos))
5481                 set_nonincremental_init ();
5482             }
5483         }
5484
5485       add_pending_init (field, value);
5486       return;
5487     }
5488   else if (TREE_CODE (constructor_type) == UNION_TYPE
5489            && constructor_elements)
5490     {
5491       if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
5492         warning_init ("initialized field with side-effects overwritten");
5493
5494       /* We can have just one union field set.  */
5495       constructor_elements = 0;
5496     }
5497
5498   /* Otherwise, output this element either to
5499      constructor_elements or to the assembler file.  */
5500
5501   if (field && TREE_CODE (field) == INTEGER_CST)
5502     field = copy_node (field);
5503   constructor_elements
5504     = tree_cons (field, value, constructor_elements);
5505
5506   /* Advance the variable that indicates sequential elements output.  */
5507   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5508     constructor_unfilled_index
5509       = size_binop (PLUS_EXPR, constructor_unfilled_index,
5510                     bitsize_one_node);
5511   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
5512     {
5513       constructor_unfilled_fields
5514         = TREE_CHAIN (constructor_unfilled_fields);
5515
5516       /* Skip any nameless bit fields.  */
5517       while (constructor_unfilled_fields != 0
5518              && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5519              && DECL_NAME (constructor_unfilled_fields) == 0)
5520         constructor_unfilled_fields =
5521           TREE_CHAIN (constructor_unfilled_fields);
5522     }
5523   else if (TREE_CODE (constructor_type) == UNION_TYPE)
5524     constructor_unfilled_fields = 0;
5525
5526   /* Now output any pending elements which have become next.  */
5527   if (pending)
5528     output_pending_init_elements (0);
5529 }
5530
5531 /* Output any pending elements which have become next.
5532    As we output elements, constructor_unfilled_{fields,index}
5533    advances, which may cause other elements to become next;
5534    if so, they too are output.
5535
5536    If ALL is 0, we return when there are
5537    no more pending elements to output now.
5538
5539    If ALL is 1, we output space as necessary so that
5540    we can output all the pending elements.  */
5541
5542 static void
5543 output_pending_init_elements (int all)
5544 {
5545   struct init_node *elt = constructor_pending_elts;
5546   tree next;
5547
5548  retry:
5549
5550   /* Look through the whole pending tree.
5551      If we find an element that should be output now,
5552      output it.  Otherwise, set NEXT to the element
5553      that comes first among those still pending.  */
5554
5555   next = 0;
5556   while (elt)
5557     {
5558       if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5559         {
5560           if (tree_int_cst_equal (elt->purpose,
5561                                   constructor_unfilled_index))
5562             output_init_element (elt->value,
5563                                  TREE_TYPE (constructor_type),
5564                                  constructor_unfilled_index, 0);
5565           else if (tree_int_cst_lt (constructor_unfilled_index,
5566                                     elt->purpose))
5567             {
5568               /* Advance to the next smaller node.  */
5569               if (elt->left)
5570                 elt = elt->left;
5571               else
5572                 {
5573                   /* We have reached the smallest node bigger than the
5574                      current unfilled index.  Fill the space first.  */
5575                   next = elt->purpose;
5576                   break;
5577                 }
5578             }
5579           else
5580             {
5581               /* Advance to the next bigger node.  */
5582               if (elt->right)
5583                 elt = elt->right;
5584               else
5585                 {
5586                   /* We have reached the biggest node in a subtree.  Find
5587                      the parent of it, which is the next bigger node.  */
5588                   while (elt->parent && elt->parent->right == elt)
5589                     elt = elt->parent;
5590                   elt = elt->parent;
5591                   if (elt && tree_int_cst_lt (constructor_unfilled_index,
5592                                               elt->purpose))
5593                     {
5594                       next = elt->purpose;
5595                       break;
5596                     }
5597                 }
5598             }
5599         }
5600       else if (TREE_CODE (constructor_type) == RECORD_TYPE
5601                || TREE_CODE (constructor_type) == UNION_TYPE)
5602         {
5603           tree ctor_unfilled_bitpos, elt_bitpos;
5604
5605           /* If the current record is complete we are done.  */
5606           if (constructor_unfilled_fields == 0)
5607             break;
5608
5609           ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
5610           elt_bitpos = bit_position (elt->purpose);
5611           /* We can't compare fields here because there might be empty
5612              fields in between.  */
5613           if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
5614             {
5615               constructor_unfilled_fields = elt->purpose;
5616               output_init_element (elt->value, TREE_TYPE (elt->purpose),
5617                                    elt->purpose, 0);
5618             }
5619           else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
5620             {
5621               /* Advance to the next smaller node.  */
5622               if (elt->left)
5623                 elt = elt->left;
5624               else
5625                 {
5626                   /* We have reached the smallest node bigger than the
5627                      current unfilled field.  Fill the space first.  */
5628                   next = elt->purpose;
5629                   break;
5630                 }
5631             }
5632           else
5633             {
5634               /* Advance to the next bigger node.  */
5635               if (elt->right)
5636                 elt = elt->right;
5637               else
5638                 {
5639                   /* We have reached the biggest node in a subtree.  Find
5640                      the parent of it, which is the next bigger node.  */
5641                   while (elt->parent && elt->parent->right == elt)
5642                     elt = elt->parent;
5643                   elt = elt->parent;
5644                   if (elt
5645                       && (tree_int_cst_lt (ctor_unfilled_bitpos,
5646                                            bit_position (elt->purpose))))
5647                     {
5648                       next = elt->purpose;
5649                       break;
5650                     }
5651                 }
5652             }
5653         }
5654     }
5655
5656   /* Ordinarily return, but not if we want to output all
5657      and there are elements left.  */
5658   if (! (all && next != 0))
5659     return;
5660
5661   /* If it's not incremental, just skip over the gap, so that after
5662      jumping to retry we will output the next successive element.  */
5663   if (TREE_CODE (constructor_type) == RECORD_TYPE
5664       || TREE_CODE (constructor_type) == UNION_TYPE)
5665     constructor_unfilled_fields = next;
5666   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5667     constructor_unfilled_index = next;
5668
5669   /* ELT now points to the node in the pending tree with the next
5670      initializer to output.  */
5671   goto retry;
5672 }
5673 \f
5674 /* Add one non-braced element to the current constructor level.
5675    This adjusts the current position within the constructor's type.
5676    This may also start or terminate implicit levels
5677    to handle a partly-braced initializer.
5678
5679    Once this has found the correct level for the new element,
5680    it calls output_init_element.  */
5681
5682 void
5683 process_init_element (tree value)
5684 {
5685   tree orig_value = value;
5686   int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
5687
5688   designator_depth = 0;
5689   designator_errorneous = 0;
5690
5691   /* Handle superfluous braces around string cst as in
5692      char x[] = {"foo"}; */
5693   if (string_flag
5694       && constructor_type
5695       && TREE_CODE (constructor_type) == ARRAY_TYPE
5696       && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
5697       && integer_zerop (constructor_unfilled_index))
5698     {
5699       if (constructor_stack->replacement_value)
5700         error_init ("excess elements in char array initializer");
5701       constructor_stack->replacement_value = value;
5702       return;
5703     }
5704
5705   if (constructor_stack->replacement_value != 0)
5706     {
5707       error_init ("excess elements in struct initializer");
5708       return;
5709     }
5710
5711   /* Ignore elements of a brace group if it is entirely superfluous
5712      and has already been diagnosed.  */
5713   if (constructor_type == 0)
5714     return;
5715
5716   /* If we've exhausted any levels that didn't have braces,
5717      pop them now.  */
5718   while (constructor_stack->implicit)
5719     {
5720       if ((TREE_CODE (constructor_type) == RECORD_TYPE
5721            || TREE_CODE (constructor_type) == UNION_TYPE)
5722           && constructor_fields == 0)
5723         process_init_element (pop_init_level (1));
5724       else if (TREE_CODE (constructor_type) == ARRAY_TYPE
5725                && (constructor_max_index == 0
5726                    || tree_int_cst_lt (constructor_max_index,
5727                                        constructor_index)))
5728         process_init_element (pop_init_level (1));
5729       else
5730         break;
5731     }
5732
5733   /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once.  */
5734   if (constructor_range_stack)
5735     {
5736       /* If value is a compound literal and we'll be just using its
5737          content, don't put it into a SAVE_EXPR.  */
5738       if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
5739           || !require_constant_value
5740           || flag_isoc99)
5741         value = save_expr (value);
5742     }
5743
5744   while (1)
5745     {
5746       if (TREE_CODE (constructor_type) == RECORD_TYPE)
5747         {
5748           tree fieldtype;
5749           enum tree_code fieldcode;
5750
5751           if (constructor_fields == 0)
5752             {
5753               pedwarn_init ("excess elements in struct initializer");
5754               break;
5755             }
5756
5757           fieldtype = TREE_TYPE (constructor_fields);
5758           if (fieldtype != error_mark_node)
5759             fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5760           fieldcode = TREE_CODE (fieldtype);
5761
5762           /* Error for non-static initialization of a flexible array member.  */
5763           if (fieldcode == ARRAY_TYPE
5764               && !require_constant_value
5765               && TYPE_SIZE (fieldtype) == NULL_TREE
5766               && TREE_CHAIN (constructor_fields) == NULL_TREE)
5767             {
5768               error_init ("non-static initialization of a flexible array member");
5769               break;
5770             }
5771
5772           /* Accept a string constant to initialize a subarray.  */
5773           if (value != 0
5774               && fieldcode == ARRAY_TYPE
5775               && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5776               && string_flag)
5777             value = orig_value;
5778           /* Otherwise, if we have come to a subaggregate,
5779              and we don't have an element of its type, push into it.  */
5780           else if (value != 0 && !constructor_no_implicit
5781                    && value != error_mark_node
5782                    && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5783                    && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5784                        || fieldcode == UNION_TYPE))
5785             {
5786               push_init_level (1);
5787               continue;
5788             }
5789
5790           if (value)
5791             {
5792               push_member_name (constructor_fields);
5793               output_init_element (value, fieldtype, constructor_fields, 1);
5794               RESTORE_SPELLING_DEPTH (constructor_depth);
5795             }
5796           else
5797             /* Do the bookkeeping for an element that was
5798                directly output as a constructor.  */
5799             {
5800               /* For a record, keep track of end position of last field.  */
5801               if (DECL_SIZE (constructor_fields))
5802                 constructor_bit_index
5803                   = size_binop (PLUS_EXPR,
5804                                 bit_position (constructor_fields),
5805                                 DECL_SIZE (constructor_fields));
5806
5807               /* If the current field was the first one not yet written out,
5808                  it isn't now, so update.  */
5809               if (constructor_unfilled_fields == constructor_fields)
5810                 {
5811                   constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5812                   /* Skip any nameless bit fields.  */
5813                   while (constructor_unfilled_fields != 0
5814                          && DECL_C_BIT_FIELD (constructor_unfilled_fields)
5815                          && DECL_NAME (constructor_unfilled_fields) == 0)
5816                     constructor_unfilled_fields =
5817                       TREE_CHAIN (constructor_unfilled_fields);
5818                 }
5819             }
5820
5821           constructor_fields = TREE_CHAIN (constructor_fields);
5822           /* Skip any nameless bit fields at the beginning.  */
5823           while (constructor_fields != 0
5824                  && DECL_C_BIT_FIELD (constructor_fields)
5825                  && DECL_NAME (constructor_fields) == 0)
5826             constructor_fields = TREE_CHAIN (constructor_fields);
5827         }
5828       else if (TREE_CODE (constructor_type) == UNION_TYPE)
5829         {
5830           tree fieldtype;
5831           enum tree_code fieldcode;
5832
5833           if (constructor_fields == 0)
5834             {
5835               pedwarn_init ("excess elements in union initializer");
5836               break;
5837             }
5838
5839           fieldtype = TREE_TYPE (constructor_fields);
5840           if (fieldtype != error_mark_node)
5841             fieldtype = TYPE_MAIN_VARIANT (fieldtype);
5842           fieldcode = TREE_CODE (fieldtype);
5843
5844           /* Warn that traditional C rejects initialization of unions.
5845              We skip the warning if the value is zero.  This is done
5846              under the assumption that the zero initializer in user
5847              code appears conditioned on e.g. __STDC__ to avoid
5848              "missing initializer" warnings and relies on default
5849              initialization to zero in the traditional C case.
5850              We also skip the warning if the initializer is designated,
5851              again on the assumption that this must be conditional on
5852              __STDC__ anyway (and we've already complained about the
5853              member-designator already).  */
5854           if (warn_traditional && !in_system_header && !constructor_designated
5855               && !(value && (integer_zerop (value) || real_zerop (value))))
5856             warning ("traditional C rejects initialization of unions");
5857
5858           /* Accept a string constant to initialize a subarray.  */
5859           if (value != 0
5860               && fieldcode == ARRAY_TYPE
5861               && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
5862               && string_flag)
5863             value = orig_value;
5864           /* Otherwise, if we have come to a subaggregate,
5865              and we don't have an element of its type, push into it.  */
5866           else if (value != 0 && !constructor_no_implicit
5867                    && value != error_mark_node
5868                    && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
5869                    && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
5870                        || fieldcode == UNION_TYPE))
5871             {
5872               push_init_level (1);
5873               continue;
5874             }
5875
5876           if (value)
5877             {
5878               push_member_name (constructor_fields);
5879               output_init_element (value, fieldtype, constructor_fields, 1);
5880               RESTORE_SPELLING_DEPTH (constructor_depth);
5881             }
5882           else
5883             /* Do the bookkeeping for an element that was
5884                directly output as a constructor.  */
5885             {
5886               constructor_bit_index = DECL_SIZE (constructor_fields);
5887               constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
5888             }
5889
5890           constructor_fields = 0;
5891         }
5892       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5893         {
5894           tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5895           enum tree_code eltcode = TREE_CODE (elttype);
5896
5897           /* Accept a string constant to initialize a subarray.  */
5898           if (value != 0
5899               && eltcode == ARRAY_TYPE
5900               && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
5901               && string_flag)
5902             value = orig_value;
5903           /* Otherwise, if we have come to a subaggregate,
5904              and we don't have an element of its type, push into it.  */
5905           else if (value != 0 && !constructor_no_implicit
5906                    && value != error_mark_node
5907                    && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
5908                    && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
5909                        || eltcode == UNION_TYPE))
5910             {
5911               push_init_level (1);
5912               continue;
5913             }
5914
5915           if (constructor_max_index != 0
5916               && (tree_int_cst_lt (constructor_max_index, constructor_index)
5917                   || integer_all_onesp (constructor_max_index)))
5918             {
5919               pedwarn_init ("excess elements in array initializer");
5920               break;
5921             }
5922
5923           /* Now output the actual element.  */
5924           if (value)
5925             {
5926               push_array_bounds (tree_low_cst (constructor_index, 0));
5927               output_init_element (value, elttype, constructor_index, 1);
5928               RESTORE_SPELLING_DEPTH (constructor_depth);
5929             }
5930
5931           constructor_index
5932             = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5933
5934           if (! value)
5935             /* If we are doing the bookkeeping for an element that was
5936                directly output as a constructor, we must update
5937                constructor_unfilled_index.  */
5938             constructor_unfilled_index = constructor_index;
5939         }
5940       else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5941         {
5942           tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5943
5944          /* Do a basic check of initializer size.  Note that vectors
5945             always have a fixed size derived from their type.  */
5946           if (tree_int_cst_lt (constructor_max_index, constructor_index))
5947             {
5948               pedwarn_init ("excess elements in vector initializer");
5949               break;
5950             }
5951
5952           /* Now output the actual element.  */
5953           if (value)
5954             output_init_element (value, elttype, constructor_index, 1);
5955
5956           constructor_index
5957             = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
5958
5959           if (! value)
5960             /* If we are doing the bookkeeping for an element that was
5961                directly output as a constructor, we must update
5962                constructor_unfilled_index.  */
5963             constructor_unfilled_index = constructor_index;
5964         }
5965
5966       /* Handle the sole element allowed in a braced initializer
5967          for a scalar variable.  */
5968       else if (constructor_fields == 0)
5969         {
5970           pedwarn_init ("excess elements in scalar initializer");
5971           break;
5972         }
5973       else
5974         {
5975           if (value)
5976             output_init_element (value, constructor_type, NULL_TREE, 1);
5977           constructor_fields = 0;
5978         }
5979
5980       /* Handle range initializers either at this level or anywhere higher
5981          in the designator stack.  */
5982       if (constructor_range_stack)
5983         {
5984           struct constructor_range_stack *p, *range_stack;
5985           int finish = 0;
5986
5987           range_stack = constructor_range_stack;
5988           constructor_range_stack = 0;
5989           while (constructor_stack != range_stack->stack)
5990             {
5991               if (!constructor_stack->implicit)
5992                 abort ();
5993               process_init_element (pop_init_level (1));
5994             }
5995           for (p = range_stack;
5996                !p->range_end || tree_int_cst_equal (p->index, p->range_end);
5997                p = p->prev)
5998             {
5999               if (!constructor_stack->implicit)
6000                 abort ();
6001               process_init_element (pop_init_level (1));
6002             }
6003
6004           p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6005           if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6006             finish = 1;
6007
6008           while (1)
6009             {
6010               constructor_index = p->index;
6011               constructor_fields = p->fields;
6012               if (finish && p->range_end && p->index == p->range_start)
6013                 {
6014                   finish = 0;
6015                   p->prev = 0;
6016                 }
6017               p = p->next;
6018               if (!p)
6019                 break;
6020               push_init_level (2);
6021               p->stack = constructor_stack;
6022               if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6023                 p->index = p->range_start;
6024             }
6025
6026           if (!finish)
6027             constructor_range_stack = range_stack;
6028           continue;
6029         }
6030
6031       break;
6032     }
6033
6034   constructor_range_stack = 0;
6035 }
6036 \f
6037 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
6038    (guaranteed to be 'volatile' or null) and ARGS (represented using
6039    an ASM_STMT node).  */
6040 tree
6041 build_asm_stmt (tree cv_qualifier, tree args)
6042 {
6043   if (!TREE_OPERAND (args, 0))
6044     TREE_OPERAND (args, 0) = cv_qualifier;
6045   return add_stmt (args);
6046 }
6047
6048 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
6049    some INPUTS, and some CLOBBERS.  The latter three may be NULL.
6050    SIMPLE indicates whether there was anything at all after the
6051    string in the asm expression -- asm("blah") and asm("blah" : )
6052    are subtly different.  We use a ASM_STMT node to represent this.  */
6053 tree
6054 build_asm_expr (tree string, tree outputs, tree inputs, tree clobbers,
6055                 bool simple)
6056 {
6057   tree tail;
6058   tree args;
6059
6060   /* We can remove output conversions that change the type,
6061      but not the mode.  */
6062   for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6063     {
6064       tree output = TREE_VALUE (tail);
6065
6066       STRIP_NOPS (output);
6067       TREE_VALUE (tail) = output;
6068
6069       /* Allow conversions as LHS here.  build_modify_expr as called below
6070          will do the right thing with them.  */
6071       while (TREE_CODE (output) == NOP_EXPR
6072              || TREE_CODE (output) == CONVERT_EXPR
6073              || TREE_CODE (output) == FLOAT_EXPR
6074              || TREE_CODE (output) == FIX_TRUNC_EXPR
6075              || TREE_CODE (output) == FIX_FLOOR_EXPR
6076              || TREE_CODE (output) == FIX_ROUND_EXPR
6077              || TREE_CODE (output) == FIX_CEIL_EXPR)
6078         output = TREE_OPERAND (output, 0);
6079
6080       lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6081     }
6082
6083   /* Remove output conversions that change the type but not the mode.  */
6084   for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6085     {
6086       tree output = TREE_VALUE (tail);
6087       STRIP_NOPS (output);
6088       TREE_VALUE (tail) = output;
6089     }
6090
6091   /* Perform default conversions on array and function inputs.
6092      Don't do this for other types as it would screw up operands
6093      expected to be in memory.  */
6094   for (tail = inputs; tail; tail = TREE_CHAIN (tail))
6095     TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
6096
6097   args = build_stmt (ASM_STMT, 0, string, outputs, inputs, clobbers);
6098
6099   /* Simple asm statements are treated as volatile.  */
6100   if (simple)
6101     {
6102       TREE_OPERAND (args, 0) = ridpointers[RID_VOLATILE];
6103       ASM_INPUT_P (args) = 1;
6104     }
6105   return args;
6106 }
6107
6108 /* Expand an ASM statement with operands, handling output operands
6109    that are not variables or INDIRECT_REFS by transforming such
6110    cases into cases that expand_asm_operands can handle.
6111
6112    Arguments are same as for expand_asm_operands.  */
6113
6114 void
6115 c_expand_asm_operands (tree string, tree outputs, tree inputs,
6116                        tree clobbers, int vol, location_t locus)
6117 {
6118   int noutputs = list_length (outputs);
6119   int i;
6120   /* o[I] is the place that output number I should be written.  */
6121   tree *o = alloca (noutputs * sizeof (tree));
6122   tree tail;
6123
6124   /* Record the contents of OUTPUTS before it is modified.  */
6125   for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6126     {
6127       o[i] = TREE_VALUE (tail);
6128       if (o[i] == error_mark_node)
6129         return;
6130     }
6131
6132   /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6133      OUTPUTS some trees for where the values were actually stored.  */
6134   expand_asm_operands (string, outputs, inputs, clobbers, vol, locus);
6135
6136   /* Copy all the intermediate outputs into the specified outputs.  */
6137   for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6138     {
6139       if (o[i] != TREE_VALUE (tail))
6140         {
6141           expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6142                        NULL_RTX, VOIDmode, EXPAND_NORMAL);
6143           free_temp_slots ();
6144
6145           /* Restore the original value so that it's correct the next
6146              time we expand this function.  */
6147           TREE_VALUE (tail) = o[i];
6148         }
6149       /* Detect modification of read-only values.
6150          (Otherwise done by build_modify_expr.)  */
6151       else
6152         {
6153           tree type = TREE_TYPE (o[i]);
6154           if (TREE_READONLY (o[i])
6155               || TYPE_READONLY (type)
6156               || ((TREE_CODE (type) == RECORD_TYPE
6157                    || TREE_CODE (type) == UNION_TYPE)
6158                   && C_TYPE_FIELDS_READONLY (type)))
6159             readonly_error (o[i], "modification by `asm'");
6160         }
6161     }
6162
6163   /* Those MODIFY_EXPRs could do autoincrements.  */
6164   emit_queue ();
6165 }
6166 \f
6167 /* Expand a C `return' statement.
6168    RETVAL is the expression for what to return,
6169    or a null pointer for `return;' with no value.  */
6170
6171 tree
6172 c_expand_return (tree retval)
6173 {
6174   tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
6175
6176   if (TREE_THIS_VOLATILE (current_function_decl))
6177     warning ("function declared `noreturn' has a `return' statement");
6178
6179   if (!retval)
6180     {
6181       current_function_returns_null = 1;
6182       if ((warn_return_type || flag_isoc99)
6183           && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
6184         pedwarn_c99 ("`return' with no value, in function returning non-void");
6185     }
6186   else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
6187     {
6188       current_function_returns_null = 1;
6189       if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6190         pedwarn ("`return' with a value, in function returning void");
6191     }
6192   else
6193     {
6194       tree t = convert_for_assignment (valtype, retval, _("return"),
6195                                        NULL_TREE, NULL_TREE, 0);
6196       tree res = DECL_RESULT (current_function_decl);
6197       tree inner;
6198
6199       current_function_returns_value = 1;
6200       if (t == error_mark_node)
6201         return NULL_TREE;
6202
6203       inner = t = convert (TREE_TYPE (res), t);
6204
6205       /* Strip any conversions, additions, and subtractions, and see if
6206          we are returning the address of a local variable.  Warn if so.  */
6207       while (1)
6208         {
6209           switch (TREE_CODE (inner))
6210             {
6211             case NOP_EXPR:   case NON_LVALUE_EXPR:  case CONVERT_EXPR:
6212             case PLUS_EXPR:
6213               inner = TREE_OPERAND (inner, 0);
6214               continue;
6215
6216             case MINUS_EXPR:
6217               /* If the second operand of the MINUS_EXPR has a pointer
6218                  type (or is converted from it), this may be valid, so
6219                  don't give a warning.  */
6220               {
6221                 tree op1 = TREE_OPERAND (inner, 1);
6222
6223                 while (! POINTER_TYPE_P (TREE_TYPE (op1))
6224                        && (TREE_CODE (op1) == NOP_EXPR
6225                            || TREE_CODE (op1) == NON_LVALUE_EXPR
6226                            || TREE_CODE (op1) == CONVERT_EXPR))
6227                   op1 = TREE_OPERAND (op1, 0);
6228
6229                 if (POINTER_TYPE_P (TREE_TYPE (op1)))
6230                   break;
6231
6232                 inner = TREE_OPERAND (inner, 0);
6233                 continue;
6234               }
6235
6236             case ADDR_EXPR:
6237               inner = TREE_OPERAND (inner, 0);
6238
6239               while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
6240                 inner = TREE_OPERAND (inner, 0);
6241
6242               if (DECL_P (inner)
6243                   && ! DECL_EXTERNAL (inner)
6244                   && ! TREE_STATIC (inner)
6245                   && DECL_CONTEXT (inner) == current_function_decl)
6246                 warning ("function returns address of local variable");
6247               break;
6248
6249             default:
6250               break;
6251             }
6252
6253           break;
6254         }
6255
6256       retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
6257     }
6258
6259  return add_stmt (build_return_stmt (retval));
6260 }
6261 \f
6262 struct c_switch {
6263   /* The SWITCH_STMT being built.  */
6264   tree switch_stmt;
6265   /* A splay-tree mapping the low element of a case range to the high
6266      element, or NULL_TREE if there is no high element.  Used to
6267      determine whether or not a new case label duplicates an old case
6268      label.  We need a tree, rather than simply a hash table, because
6269      of the GNU case range extension.  */
6270   splay_tree cases;
6271   /* The next node on the stack.  */
6272   struct c_switch *next;
6273 };
6274
6275 /* A stack of the currently active switch statements.  The innermost
6276    switch statement is on the top of the stack.  There is no need to
6277    mark the stack for garbage collection because it is only active
6278    during the processing of the body of a function, and we never
6279    collect at that point.  */
6280
6281 static struct c_switch *switch_stack;
6282
6283 /* Start a C switch statement, testing expression EXP.  Return the new
6284    SWITCH_STMT.  */
6285
6286 tree
6287 c_start_case (tree exp)
6288 {
6289   enum tree_code code;
6290   tree type, orig_type = error_mark_node;
6291   struct c_switch *cs;
6292
6293   if (exp != error_mark_node)
6294     {
6295       code = TREE_CODE (TREE_TYPE (exp));
6296       orig_type = TREE_TYPE (exp);
6297
6298       if (! INTEGRAL_TYPE_P (orig_type)
6299           && code != ERROR_MARK)
6300         {
6301           error ("switch quantity not an integer");
6302           exp = integer_zero_node;
6303         }
6304       else
6305         {
6306           type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
6307
6308           if (warn_traditional && !in_system_header
6309               && (type == long_integer_type_node
6310                   || type == long_unsigned_type_node))
6311             warning ("`long' switch expression not converted to `int' in ISO C");
6312
6313           exp = default_conversion (exp);
6314           type = TREE_TYPE (exp);
6315         }
6316     }
6317
6318   /* Add this new SWITCH_STMT to the stack.  */
6319   cs = xmalloc (sizeof (*cs));
6320   cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
6321   cs->cases = splay_tree_new (case_compare, NULL, NULL);
6322   cs->next = switch_stack;
6323   switch_stack = cs;
6324
6325   return add_stmt (switch_stack->switch_stmt);
6326 }
6327
6328 /* Process a case label.  */
6329
6330 tree
6331 do_case (tree low_value, tree high_value)
6332 {
6333   tree label = NULL_TREE;
6334
6335   if (switch_stack)
6336     {
6337       bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
6338
6339       label = c_add_case_label (switch_stack->cases,
6340                                 SWITCH_COND (switch_stack->switch_stmt),
6341                                 low_value, high_value);
6342       if (label == error_mark_node)
6343         label = NULL_TREE;
6344       else if (switch_was_empty_p)
6345         {
6346           /* Attach the first case label to the SWITCH_BODY.  */
6347           SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
6348           TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
6349         }
6350     }
6351   else if (low_value)
6352     error ("case label not within a switch statement");
6353   else
6354     error ("`default' label not within a switch statement");
6355
6356   return label;
6357 }
6358
6359 /* Finish the switch statement.  */
6360
6361 void
6362 c_finish_case (void)
6363 {
6364   struct c_switch *cs = switch_stack;
6365
6366   /* Rechain the next statements to the SWITCH_STMT.  */
6367   last_tree = cs->switch_stmt;
6368
6369   /* Pop the stack.  */
6370   switch_stack = switch_stack->next;
6371   splay_tree_delete (cs->cases);
6372   free (cs);
6373 }
6374
6375 /* Build a binary-operation expression without default conversions.
6376    CODE is the kind of expression to build.
6377    This function differs from `build' in several ways:
6378    the data type of the result is computed and recorded in it,
6379    warnings are generated if arg data types are invalid,
6380    special handling for addition and subtraction of pointers is known,
6381    and some optimization is done (operations on narrow ints
6382    are done in the narrower type when that gives the same result).
6383    Constant folding is also done before the result is returned.
6384
6385    Note that the operands will never have enumeral types, or function
6386    or array types, because either they will have the default conversions
6387    performed or they have both just been converted to some other type in which
6388    the arithmetic is to be done.  */
6389
6390 tree
6391 build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
6392                  int convert_p)
6393 {
6394   tree type0, type1;
6395   enum tree_code code0, code1;
6396   tree op0, op1;
6397
6398   /* Expression code to give to the expression when it is built.
6399      Normally this is CODE, which is what the caller asked for,
6400      but in some special cases we change it.  */
6401   enum tree_code resultcode = code;
6402
6403   /* Data type in which the computation is to be performed.
6404      In the simplest cases this is the common type of the arguments.  */
6405   tree result_type = NULL;
6406
6407   /* Nonzero means operands have already been type-converted
6408      in whatever way is necessary.
6409      Zero means they need to be converted to RESULT_TYPE.  */
6410   int converted = 0;
6411
6412   /* Nonzero means create the expression with this type, rather than
6413      RESULT_TYPE.  */
6414   tree build_type = 0;
6415
6416   /* Nonzero means after finally constructing the expression
6417      convert it to this type.  */
6418   tree final_type = 0;
6419
6420   /* Nonzero if this is an operation like MIN or MAX which can
6421      safely be computed in short if both args are promoted shorts.
6422      Also implies COMMON.
6423      -1 indicates a bitwise operation; this makes a difference
6424      in the exact conditions for when it is safe to do the operation
6425      in a narrower mode.  */
6426   int shorten = 0;
6427
6428   /* Nonzero if this is a comparison operation;
6429      if both args are promoted shorts, compare the original shorts.
6430      Also implies COMMON.  */
6431   int short_compare = 0;
6432
6433   /* Nonzero if this is a right-shift operation, which can be computed on the
6434      original short and then promoted if the operand is a promoted short.  */
6435   int short_shift = 0;
6436
6437   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
6438   int common = 0;
6439
6440   if (convert_p)
6441     {
6442       op0 = default_conversion (orig_op0);
6443       op1 = default_conversion (orig_op1);
6444     }
6445   else
6446     {
6447       op0 = orig_op0;
6448       op1 = orig_op1;
6449     }
6450
6451   type0 = TREE_TYPE (op0);
6452   type1 = TREE_TYPE (op1);
6453
6454   /* The expression codes of the data types of the arguments tell us
6455      whether the arguments are integers, floating, pointers, etc.  */
6456   code0 = TREE_CODE (type0);
6457   code1 = TREE_CODE (type1);
6458
6459   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
6460   STRIP_TYPE_NOPS (op0);
6461   STRIP_TYPE_NOPS (op1);
6462
6463   /* If an error was already reported for one of the arguments,
6464      avoid reporting another error.  */
6465
6466   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6467     return error_mark_node;
6468
6469   switch (code)
6470     {
6471     case PLUS_EXPR:
6472       /* Handle the pointer + int case.  */
6473       if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6474         return pointer_int_sum (PLUS_EXPR, op0, op1);
6475       else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
6476         return pointer_int_sum (PLUS_EXPR, op1, op0);
6477       else
6478         common = 1;
6479       break;
6480
6481     case MINUS_EXPR:
6482       /* Subtraction of two similar pointers.
6483          We must subtract them as integers, then divide by object size.  */
6484       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
6485           && comp_target_types (type0, type1, 1))
6486         return pointer_diff (op0, op1);
6487       /* Handle pointer minus int.  Just like pointer plus int.  */
6488       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6489         return pointer_int_sum (MINUS_EXPR, op0, op1);
6490       else
6491         common = 1;
6492       break;
6493
6494     case MULT_EXPR:
6495       common = 1;
6496       break;
6497
6498     case TRUNC_DIV_EXPR:
6499     case CEIL_DIV_EXPR:
6500     case FLOOR_DIV_EXPR:
6501     case ROUND_DIV_EXPR:
6502     case EXACT_DIV_EXPR:
6503       /* Floating point division by zero is a legitimate way to obtain
6504          infinities and NaNs.  */
6505       if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6506         warning ("division by zero");
6507
6508       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6509            || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
6510           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6511               || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
6512         {
6513           if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
6514             resultcode = RDIV_EXPR;
6515           else
6516             /* Although it would be tempting to shorten always here, that
6517                loses on some targets, since the modulo instruction is
6518                undefined if the quotient can't be represented in the
6519                computation mode.  We shorten only if unsigned or if
6520                dividing by something we know != -1.  */
6521             shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6522                        || (TREE_CODE (op1) == INTEGER_CST
6523                            && ! integer_all_onesp (op1)));
6524           common = 1;
6525         }
6526       break;
6527
6528     case BIT_AND_EXPR:
6529     case BIT_IOR_EXPR:
6530     case BIT_XOR_EXPR:
6531       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6532         shorten = -1;
6533       else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
6534         common = 1;
6535       break;
6536
6537     case TRUNC_MOD_EXPR:
6538     case FLOOR_MOD_EXPR:
6539       if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
6540         warning ("division by zero");
6541
6542       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6543         {
6544           /* Although it would be tempting to shorten always here, that loses
6545              on some targets, since the modulo instruction is undefined if the
6546              quotient can't be represented in the computation mode.  We shorten
6547              only if unsigned or if dividing by something we know != -1.  */
6548           shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
6549                      || (TREE_CODE (op1) == INTEGER_CST
6550                          && ! integer_all_onesp (op1)));
6551           common = 1;
6552         }
6553       break;
6554
6555     case TRUTH_ANDIF_EXPR:
6556     case TRUTH_ORIF_EXPR:
6557     case TRUTH_AND_EXPR:
6558     case TRUTH_OR_EXPR:
6559     case TRUTH_XOR_EXPR:
6560       if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
6561            || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
6562           && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
6563               || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
6564         {
6565           /* Result of these operations is always an int,
6566              but that does not mean the operands should be
6567              converted to ints!  */
6568           result_type = integer_type_node;
6569           op0 = (*lang_hooks.truthvalue_conversion) (op0);
6570           op1 = (*lang_hooks.truthvalue_conversion) (op1);
6571           converted = 1;
6572         }
6573       break;
6574
6575       /* Shift operations: result has same type as first operand;
6576          always convert second operand to int.
6577          Also set SHORT_SHIFT if shifting rightward.  */
6578
6579     case RSHIFT_EXPR:
6580       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6581         {
6582           if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6583             {
6584               if (tree_int_cst_sgn (op1) < 0)
6585                 warning ("right shift count is negative");
6586               else
6587                 {
6588                   if (! integer_zerop (op1))
6589                     short_shift = 1;
6590
6591                   if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6592                     warning ("right shift count >= width of type");
6593                 }
6594             }
6595
6596           /* Use the type of the value to be shifted.  */
6597           result_type = type0;
6598           /* Convert the shift-count to an integer, regardless of size
6599              of value being shifted.  */
6600           if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6601             op1 = convert (integer_type_node, op1);
6602           /* Avoid converting op1 to result_type later.  */
6603           converted = 1;
6604         }
6605       break;
6606
6607     case LSHIFT_EXPR:
6608       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6609         {
6610           if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6611             {
6612               if (tree_int_cst_sgn (op1) < 0)
6613                 warning ("left shift count is negative");
6614
6615               else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6616                 warning ("left shift count >= width of type");
6617             }
6618
6619           /* Use the type of the value to be shifted.  */
6620           result_type = type0;
6621           /* Convert the shift-count to an integer, regardless of size
6622              of value being shifted.  */
6623           if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6624             op1 = convert (integer_type_node, op1);
6625           /* Avoid converting op1 to result_type later.  */
6626           converted = 1;
6627         }
6628       break;
6629
6630     case RROTATE_EXPR:
6631     case LROTATE_EXPR:
6632       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
6633         {
6634           if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
6635             {
6636               if (tree_int_cst_sgn (op1) < 0)
6637                 warning ("shift count is negative");
6638               else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
6639                 warning ("shift count >= width of type");
6640             }
6641
6642           /* Use the type of the value to be shifted.  */
6643           result_type = type0;
6644           /* Convert the shift-count to an integer, regardless of size
6645              of value being shifted.  */
6646           if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
6647             op1 = convert (integer_type_node, op1);
6648           /* Avoid converting op1 to result_type later.  */
6649           converted = 1;
6650         }
6651       break;
6652
6653     case EQ_EXPR:
6654     case NE_EXPR:
6655       if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
6656         warning ("comparing floating point with == or != is unsafe");
6657       /* Result of comparison is always int,
6658          but don't convert the args to int!  */
6659       build_type = integer_type_node;
6660       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
6661            || code0 == COMPLEX_TYPE
6662            || code0 == VECTOR_TYPE)
6663           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
6664               || code1 == COMPLEX_TYPE
6665               || code1 == VECTOR_TYPE))
6666         short_compare = 1;
6667       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6668         {
6669           tree tt0 = TREE_TYPE (type0);
6670           tree tt1 = TREE_TYPE (type1);
6671           /* Anything compares with void *.  void * compares with anything.
6672              Otherwise, the targets must be compatible
6673              and both must be object or both incomplete.  */
6674           if (comp_target_types (type0, type1, 1))
6675             result_type = common_type (type0, type1);
6676           else if (VOID_TYPE_P (tt0))
6677             {
6678               /* op0 != orig_op0 detects the case of something
6679                  whose value is 0 but which isn't a valid null ptr const.  */
6680               if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
6681                   && TREE_CODE (tt1) == FUNCTION_TYPE)
6682                 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6683             }
6684           else if (VOID_TYPE_P (tt1))
6685             {
6686               if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
6687                   && TREE_CODE (tt0) == FUNCTION_TYPE)
6688                 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
6689             }
6690           else
6691             pedwarn ("comparison of distinct pointer types lacks a cast");
6692
6693           if (result_type == NULL_TREE)
6694             result_type = ptr_type_node;
6695         }
6696       else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6697                && integer_zerop (op1))
6698         result_type = type0;
6699       else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6700                && integer_zerop (op0))
6701         result_type = type1;
6702       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6703         {
6704           result_type = type0;
6705           pedwarn ("comparison between pointer and integer");
6706         }
6707       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6708         {
6709           result_type = type1;
6710           pedwarn ("comparison between pointer and integer");
6711         }
6712       break;
6713
6714     case MAX_EXPR:
6715     case MIN_EXPR:
6716       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6717           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6718         shorten = 1;
6719       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6720         {
6721           if (comp_target_types (type0, type1, 1))
6722             {
6723               result_type = common_type (type0, type1);
6724               if (pedantic
6725                   && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6726                 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6727             }
6728           else
6729             {
6730               result_type = ptr_type_node;
6731               pedwarn ("comparison of distinct pointer types lacks a cast");
6732             }
6733         }
6734       break;
6735
6736     case LE_EXPR:
6737     case GE_EXPR:
6738     case LT_EXPR:
6739     case GT_EXPR:
6740       build_type = integer_type_node;
6741       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
6742           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
6743         short_compare = 1;
6744       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
6745         {
6746           if (comp_target_types (type0, type1, 1))
6747             {
6748               result_type = common_type (type0, type1);
6749               if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
6750                   != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
6751                 pedwarn ("comparison of complete and incomplete pointers");
6752               else if (pedantic
6753                        && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
6754                 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
6755             }
6756           else
6757             {
6758               result_type = ptr_type_node;
6759               pedwarn ("comparison of distinct pointer types lacks a cast");
6760             }
6761         }
6762       else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
6763                && integer_zerop (op1))
6764         {
6765           result_type = type0;
6766           if (pedantic || extra_warnings)
6767             pedwarn ("ordered comparison of pointer with integer zero");
6768         }
6769       else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
6770                && integer_zerop (op0))
6771         {
6772           result_type = type1;
6773           if (pedantic)
6774             pedwarn ("ordered comparison of pointer with integer zero");
6775         }
6776       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
6777         {
6778           result_type = type0;
6779           pedwarn ("comparison between pointer and integer");
6780         }
6781       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
6782         {
6783           result_type = type1;
6784           pedwarn ("comparison between pointer and integer");
6785         }
6786       break;
6787
6788     case UNORDERED_EXPR:
6789     case ORDERED_EXPR:
6790     case UNLT_EXPR:
6791     case UNLE_EXPR:
6792     case UNGT_EXPR:
6793     case UNGE_EXPR:
6794     case UNEQ_EXPR:
6795       build_type = integer_type_node;
6796       if (code0 != REAL_TYPE || code1 != REAL_TYPE)
6797         {
6798           error ("unordered comparison on non-floating point argument");
6799           return error_mark_node;
6800         }
6801       common = 1;
6802       break;
6803
6804     default:
6805       break;
6806     }
6807
6808   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
6809     return error_mark_node;
6810
6811   if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
6812        || code0 == VECTOR_TYPE)
6813       &&
6814       (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
6815        || code1 == VECTOR_TYPE))
6816     {
6817       int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
6818
6819       if (shorten || common || short_compare)
6820         result_type = common_type (type0, type1);
6821
6822       /* For certain operations (which identify themselves by shorten != 0)
6823          if both args were extended from the same smaller type,
6824          do the arithmetic in that type and then extend.
6825
6826          shorten !=0 and !=1 indicates a bitwise operation.
6827          For them, this optimization is safe only if
6828          both args are zero-extended or both are sign-extended.
6829          Otherwise, we might change the result.
6830          Eg, (short)-1 | (unsigned short)-1 is (int)-1
6831          but calculated in (unsigned short) it would be (unsigned short)-1.  */
6832
6833       if (shorten && none_complex)
6834         {
6835           int unsigned0, unsigned1;
6836           tree arg0 = get_narrower (op0, &unsigned0);
6837           tree arg1 = get_narrower (op1, &unsigned1);
6838           /* UNS is 1 if the operation to be done is an unsigned one.  */
6839           int uns = TREE_UNSIGNED (result_type);
6840           tree type;
6841
6842           final_type = result_type;
6843
6844           /* Handle the case that OP0 (or OP1) does not *contain* a conversion
6845              but it *requires* conversion to FINAL_TYPE.  */
6846
6847           if ((TYPE_PRECISION (TREE_TYPE (op0))
6848                == TYPE_PRECISION (TREE_TYPE (arg0)))
6849               && TREE_TYPE (op0) != final_type)
6850             unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
6851           if ((TYPE_PRECISION (TREE_TYPE (op1))
6852                == TYPE_PRECISION (TREE_TYPE (arg1)))
6853               && TREE_TYPE (op1) != final_type)
6854             unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
6855
6856           /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE.  */
6857
6858           /* For bitwise operations, signedness of nominal type
6859              does not matter.  Consider only how operands were extended.  */
6860           if (shorten == -1)
6861             uns = unsigned0;
6862
6863           /* Note that in all three cases below we refrain from optimizing
6864              an unsigned operation on sign-extended args.
6865              That would not be valid.  */
6866
6867           /* Both args variable: if both extended in same way
6868              from same width, do it in that width.
6869              Do it unsigned if args were zero-extended.  */
6870           if ((TYPE_PRECISION (TREE_TYPE (arg0))
6871                < TYPE_PRECISION (result_type))
6872               && (TYPE_PRECISION (TREE_TYPE (arg1))
6873                   == TYPE_PRECISION (TREE_TYPE (arg0)))
6874               && unsigned0 == unsigned1
6875               && (unsigned0 || !uns))
6876             result_type
6877               = c_common_signed_or_unsigned_type
6878               (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
6879           else if (TREE_CODE (arg0) == INTEGER_CST
6880                    && (unsigned1 || !uns)
6881                    && (TYPE_PRECISION (TREE_TYPE (arg1))
6882                        < TYPE_PRECISION (result_type))
6883                    && (type
6884                        = c_common_signed_or_unsigned_type (unsigned1,
6885                                                            TREE_TYPE (arg1)),
6886                        int_fits_type_p (arg0, type)))
6887             result_type = type;
6888           else if (TREE_CODE (arg1) == INTEGER_CST
6889                    && (unsigned0 || !uns)
6890                    && (TYPE_PRECISION (TREE_TYPE (arg0))
6891                        < TYPE_PRECISION (result_type))
6892                    && (type
6893                        = c_common_signed_or_unsigned_type (unsigned0,
6894                                                            TREE_TYPE (arg0)),
6895                        int_fits_type_p (arg1, type)))
6896             result_type = type;
6897         }
6898
6899       /* Shifts can be shortened if shifting right.  */
6900
6901       if (short_shift)
6902         {
6903           int unsigned_arg;
6904           tree arg0 = get_narrower (op0, &unsigned_arg);
6905
6906           final_type = result_type;
6907
6908           if (arg0 == op0 && final_type == TREE_TYPE (op0))
6909             unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
6910
6911           if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6912               /* We can shorten only if the shift count is less than the
6913                  number of bits in the smaller type size.  */
6914               && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
6915               /* We cannot drop an unsigned shift after sign-extension.  */
6916               && (!TREE_UNSIGNED (final_type) || unsigned_arg))
6917             {
6918               /* Do an unsigned shift if the operand was zero-extended.  */
6919               result_type
6920                 = c_common_signed_or_unsigned_type (unsigned_arg,
6921                                                     TREE_TYPE (arg0));
6922               /* Convert value-to-be-shifted to that type.  */
6923               if (TREE_TYPE (op0) != result_type)
6924                 op0 = convert (result_type, op0);
6925               converted = 1;
6926             }
6927         }
6928
6929       /* Comparison operations are shortened too but differently.
6930          They identify themselves by setting short_compare = 1.  */
6931
6932       if (short_compare)
6933         {
6934           /* Don't write &op0, etc., because that would prevent op0
6935              from being kept in a register.
6936              Instead, make copies of the our local variables and
6937              pass the copies by reference, then copy them back afterward.  */
6938           tree xop0 = op0, xop1 = op1, xresult_type = result_type;
6939           enum tree_code xresultcode = resultcode;
6940           tree val
6941             = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
6942
6943           if (val != 0)
6944             return val;
6945
6946           op0 = xop0, op1 = xop1;
6947           converted = 1;
6948           resultcode = xresultcode;
6949
6950           if (warn_sign_compare && skip_evaluation == 0)
6951             {
6952               int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
6953               int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
6954               int unsignedp0, unsignedp1;
6955               tree primop0 = get_narrower (op0, &unsignedp0);
6956               tree primop1 = get_narrower (op1, &unsignedp1);
6957
6958               xop0 = orig_op0;
6959               xop1 = orig_op1;
6960               STRIP_TYPE_NOPS (xop0);
6961               STRIP_TYPE_NOPS (xop1);
6962
6963               /* Give warnings for comparisons between signed and unsigned
6964                  quantities that may fail.
6965
6966                  Do the checking based on the original operand trees, so that
6967                  casts will be considered, but default promotions won't be.
6968
6969                  Do not warn if the comparison is being done in a signed type,
6970                  since the signed type will only be chosen if it can represent
6971                  all the values of the unsigned type.  */
6972               if (! TREE_UNSIGNED (result_type))
6973                 /* OK */;
6974               /* Do not warn if both operands are the same signedness.  */
6975               else if (op0_signed == op1_signed)
6976                 /* OK */;
6977               else
6978                 {
6979                   tree sop, uop;
6980
6981                   if (op0_signed)
6982                     sop = xop0, uop = xop1;
6983                   else
6984                     sop = xop1, uop = xop0;
6985
6986                   /* Do not warn if the signed quantity is an
6987                      unsuffixed integer literal (or some static
6988                      constant expression involving such literals or a
6989                      conditional expression involving such literals)
6990                      and it is non-negative.  */
6991                   if (c_tree_expr_nonnegative_p (sop))
6992                     /* OK */;
6993                   /* Do not warn if the comparison is an equality operation,
6994                      the unsigned quantity is an integral constant, and it
6995                      would fit in the result if the result were signed.  */
6996                   else if (TREE_CODE (uop) == INTEGER_CST
6997                            && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
6998                            && int_fits_type_p
6999                            (uop, c_common_signed_type (result_type)))
7000                     /* OK */;
7001                   /* Do not warn if the unsigned quantity is an enumeration
7002                      constant and its maximum value would fit in the result
7003                      if the result were signed.  */
7004                   else if (TREE_CODE (uop) == INTEGER_CST
7005                            && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
7006                            && int_fits_type_p
7007                            (TYPE_MAX_VALUE (TREE_TYPE(uop)),
7008                             c_common_signed_type (result_type)))
7009                     /* OK */;
7010                   else
7011                     warning ("comparison between signed and unsigned");
7012                 }
7013
7014               /* Warn if two unsigned values are being compared in a size
7015                  larger than their original size, and one (and only one) is the
7016                  result of a `~' operator.  This comparison will always fail.
7017
7018                  Also warn if one operand is a constant, and the constant
7019                  does not have all bits set that are set in the ~ operand
7020                  when it is extended.  */
7021
7022               if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
7023                   != (TREE_CODE (primop1) == BIT_NOT_EXPR))
7024                 {
7025                   if (TREE_CODE (primop0) == BIT_NOT_EXPR)
7026                     primop0 = get_narrower (TREE_OPERAND (primop0, 0),
7027                                             &unsignedp0);
7028                   else
7029                     primop1 = get_narrower (TREE_OPERAND (primop1, 0),
7030                                             &unsignedp1);
7031
7032                   if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
7033                     {
7034                       tree primop;
7035                       HOST_WIDE_INT constant, mask;
7036                       int unsignedp, bits;
7037
7038                       if (host_integerp (primop0, 0))
7039                         {
7040                           primop = primop1;
7041                           unsignedp = unsignedp1;
7042                           constant = tree_low_cst (primop0, 0);
7043                         }
7044                       else
7045                         {
7046                           primop = primop0;
7047                           unsignedp = unsignedp0;
7048                           constant = tree_low_cst (primop1, 0);
7049                         }
7050
7051                       bits = TYPE_PRECISION (TREE_TYPE (primop));
7052                       if (bits < TYPE_PRECISION (result_type)
7053                           && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
7054                         {
7055                           mask = (~ (HOST_WIDE_INT) 0) << bits;
7056                           if ((mask & constant) != mask)
7057                             warning ("comparison of promoted ~unsigned with constant");
7058                         }
7059                     }
7060                   else if (unsignedp0 && unsignedp1
7061                            && (TYPE_PRECISION (TREE_TYPE (primop0))
7062                                < TYPE_PRECISION (result_type))
7063                            && (TYPE_PRECISION (TREE_TYPE (primop1))
7064                                < TYPE_PRECISION (result_type)))
7065                     warning ("comparison of promoted ~unsigned with unsigned");
7066                 }
7067             }
7068         }
7069     }
7070
7071   /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
7072      If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
7073      Then the expression will be built.
7074      It will be given type FINAL_TYPE if that is nonzero;
7075      otherwise, it will be given type RESULT_TYPE.  */
7076
7077   if (!result_type)
7078     {
7079       binary_op_error (code);
7080       return error_mark_node;
7081     }
7082
7083   if (! converted)
7084     {
7085       if (TREE_TYPE (op0) != result_type)
7086         op0 = convert (result_type, op0);
7087       if (TREE_TYPE (op1) != result_type)
7088         op1 = convert (result_type, op1);
7089     }
7090
7091   if (build_type == NULL_TREE)
7092     build_type = result_type;
7093
7094   {
7095     tree result = build (resultcode, build_type, op0, op1);
7096     tree folded;
7097
7098     /* Treat expressions in initializers specially as they can't trap.  */
7099     folded = initializer_stack ? fold_initializer (result)
7100                                : fold (result);
7101     if (folded == result)
7102       TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
7103     if (final_type != 0)
7104       return convert (final_type, folded);
7105     return folded;
7106   }
7107 }
7108