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