OSDN Git Service

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