OSDN Git Service

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