OSDN Git Service

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