OSDN Git Service

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