OSDN Git Service

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