OSDN Git Service

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