OSDN Git Service

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