OSDN Git Service

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