OSDN Git Service

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