OSDN Git Service

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