OSDN Git Service

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