OSDN Git Service

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