OSDN Git Service

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