OSDN Git Service

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