OSDN Git Service

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