OSDN Git Service

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