OSDN Git Service

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