OSDN Git Service

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