OSDN Git Service

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