OSDN Git Service

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