OSDN Git Service

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