OSDN Git Service

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