OSDN Git Service

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