OSDN Git Service

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