OSDN Git Service

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