OSDN Git Service

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