OSDN Git Service

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