OSDN Git Service

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