OSDN Git Service

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