OSDN Git Service

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