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