OSDN Git Service

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