OSDN Git Service

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