OSDN Git Service

e7528a715ba4f2172555afa454cfbfd8e60153f2
[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 (target);
398         t1 = build_type_attribute_variant (t1, attributes);
399         return qualify_type (t1, t2);
400       }
401
402     case ARRAY_TYPE:
403       {
404         tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
405         int quals;
406         tree unqual_elt;
407         tree d1 = TYPE_DOMAIN (t1);
408         tree d2 = TYPE_DOMAIN (t2);
409         bool d1_variable, d2_variable;
410         bool d1_zero, d2_zero;
411         bool t1_complete, t2_complete;
412
413         /* We should not have any type quals on arrays at all.  */
414         gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
415                     && !TYPE_QUALS_NO_ADDR_SPACE (t2));
416
417         t1_complete = COMPLETE_TYPE_P (t1);
418         t2_complete = COMPLETE_TYPE_P (t2);
419
420         d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
421         d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
422
423         d1_variable = (!d1_zero
424                        && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
425                            || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
426         d2_variable = (!d2_zero
427                        && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
428                            || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
429         d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
430         d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
431
432         /* Save space: see if the result is identical to one of the args.  */
433         if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
434             && (d2_variable || d2_zero || !d1_variable))
435           return build_type_attribute_variant (t1, attributes);
436         if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
437             && (d1_variable || d1_zero || !d2_variable))
438           return build_type_attribute_variant (t2, attributes);
439
440         if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
441           return build_type_attribute_variant (t1, attributes);
442         if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443           return build_type_attribute_variant (t2, attributes);
444
445         /* Merge the element types, and have a size if either arg has
446            one.  We may have qualifiers on the element types.  To set
447            up TYPE_MAIN_VARIANT correctly, we need to form the
448            composite of the unqualified types and add the qualifiers
449            back at the end.  */
450         quals = TYPE_QUALS (strip_array_types (elt));
451         unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
452         t1 = build_array_type (unqual_elt,
453                                TYPE_DOMAIN ((TYPE_DOMAIN (t1)
454                                              && (d2_variable
455                                                  || d2_zero
456                                                  || !d1_variable))
457                                             ? t1
458                                             : t2));
459         /* Ensure a composite type involving a zero-length array type
460            is a zero-length type not an incomplete type.  */
461         if (d1_zero && d2_zero
462             && (t1_complete || t2_complete)
463             && !COMPLETE_TYPE_P (t1))
464           {
465             TYPE_SIZE (t1) = bitsize_zero_node;
466             TYPE_SIZE_UNIT (t1) = size_zero_node;
467           }
468         t1 = c_build_qualified_type (t1, quals);
469         return build_type_attribute_variant (t1, attributes);
470       }
471
472     case ENUMERAL_TYPE:
473     case RECORD_TYPE:
474     case UNION_TYPE:
475       if (attributes != NULL)
476         {
477           /* Try harder not to create a new aggregate type.  */
478           if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
479             return t1;
480           if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
481             return t2;
482         }
483       return build_type_attribute_variant (t1, attributes);
484
485     case FUNCTION_TYPE:
486       /* Function types: prefer the one that specified arg types.
487          If both do, merge the arg types.  Also merge the return types.  */
488       {
489         tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
490         tree p1 = TYPE_ARG_TYPES (t1);
491         tree p2 = TYPE_ARG_TYPES (t2);
492         int len;
493         tree newargs, n;
494         int i;
495
496         /* Save space: see if the result is identical to one of the args.  */
497         if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
498           return build_type_attribute_variant (t1, attributes);
499         if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
500           return build_type_attribute_variant (t2, attributes);
501
502         /* Simple way if one arg fails to specify argument types.  */
503         if (TYPE_ARG_TYPES (t1) == 0)
504          {
505             t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
506             t1 = build_type_attribute_variant (t1, attributes);
507             return qualify_type (t1, t2);
508          }
509         if (TYPE_ARG_TYPES (t2) == 0)
510          {
511            t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
512            t1 = build_type_attribute_variant (t1, attributes);
513            return qualify_type (t1, t2);
514          }
515
516         /* If both args specify argument types, we must merge the two
517            lists, argument by argument.  */
518
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 C1X 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       fundecl = function;
2720     }
2721   if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2722     function = function_to_pointer_conversion (loc, function);
2723
2724   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2725      expressions, like those used for ObjC messenger dispatches.  */
2726   if (!VEC_empty (tree, params))
2727     function = objc_rewrite_function_call (function,
2728                                            VEC_index (tree, params, 0));
2729
2730   function = c_fully_fold (function, false, NULL);
2731
2732   fntype = TREE_TYPE (function);
2733
2734   if (TREE_CODE (fntype) == ERROR_MARK)
2735     return error_mark_node;
2736
2737   if (!(TREE_CODE (fntype) == POINTER_TYPE
2738         && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2739     {
2740       error_at (loc, "called object %qE is not a function", function);
2741       return error_mark_node;
2742     }
2743
2744   if (fundecl && TREE_THIS_VOLATILE (fundecl))
2745     current_function_returns_abnormally = 1;
2746
2747   /* fntype now gets the type of function pointed to.  */
2748   fntype = TREE_TYPE (fntype);
2749
2750   /* Convert the parameters to the types declared in the
2751      function prototype, or apply default promotions.  */
2752
2753   nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2754                              function, fundecl);
2755   if (nargs < 0)
2756     return error_mark_node;
2757
2758   /* Check that the function is called through a compatible prototype.
2759      If it is not, replace the call by a trap, wrapped up in a compound
2760      expression if necessary.  This has the nice side-effect to prevent
2761      the tree-inliner from generating invalid assignment trees which may
2762      blow up in the RTL expander later.  */
2763   if (CONVERT_EXPR_P (function)
2764       && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2765       && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2766       && !comptypes (fntype, TREE_TYPE (tem)))
2767     {
2768       tree return_type = TREE_TYPE (fntype);
2769       tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2770                                        NULL_TREE);
2771       int i;
2772
2773       /* This situation leads to run-time undefined behavior.  We can't,
2774          therefore, simply error unless we can prove that all possible
2775          executions of the program must execute the code.  */
2776       if (warning_at (loc, 0, "function called through a non-compatible type"))
2777         /* We can, however, treat "undefined" any way we please.
2778            Call abort to encourage the user to fix the program.  */
2779         inform (loc, "if this code is reached, the program will abort");
2780       /* Before the abort, allow the function arguments to exit or
2781          call longjmp.  */
2782       for (i = 0; i < nargs; i++)
2783         trap = build2 (COMPOUND_EXPR, void_type_node,
2784                        VEC_index (tree, params, i), trap);
2785
2786       if (VOID_TYPE_P (return_type))
2787         {
2788           if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2789             pedwarn (loc, 0,
2790                      "function with qualified void return type called");
2791           return trap;
2792         }
2793       else
2794         {
2795           tree rhs;
2796
2797           if (AGGREGATE_TYPE_P (return_type))
2798             rhs = build_compound_literal (loc, return_type,
2799                                           build_constructor (return_type, 0),
2800                                           false);
2801           else
2802             rhs = build_zero_cst (return_type);
2803
2804           return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2805                                                 trap, rhs));
2806         }
2807     }
2808
2809   argarray = VEC_address (tree, params);
2810
2811   /* Check that arguments to builtin functions match the expectations.  */
2812   if (fundecl
2813       && DECL_BUILT_IN (fundecl)
2814       && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2815       && !check_builtin_function_arguments (fundecl, nargs, argarray))
2816     return error_mark_node;
2817
2818   /* Check that the arguments to the function are valid.  */
2819   check_function_arguments (fntype, nargs, argarray);
2820
2821   if (name != NULL_TREE
2822       && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2823     {
2824       if (require_constant_value)
2825         result =
2826           fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2827                                                  function, nargs, argarray);
2828       else
2829         result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2830                                             function, nargs, argarray);
2831       if (TREE_CODE (result) == NOP_EXPR
2832           && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2833         STRIP_TYPE_NOPS (result);
2834     }
2835   else
2836     result = build_call_array_loc (loc, TREE_TYPE (fntype),
2837                                    function, nargs, argarray);
2838
2839   if (VOID_TYPE_P (TREE_TYPE (result)))
2840     {
2841       if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2842         pedwarn (loc, 0,
2843                  "function with qualified void return type called");
2844       return result;
2845     }
2846   return require_complete_type (result);
2847 }
2848
2849 /* Build a VEC_SHUFFLE_EXPR if V0, V1 and MASK are not error_mark_nodes
2850    and have vector types, V0 has the same type as V1, and the number of
2851    elements of V0, V1, MASK is the same.
2852
2853    In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2854    called with two arguments.  In this case implementation passes the
2855    first argument twice in order to share the same tree code.  This fact
2856    could enable the mask-values being twice the vector length.  This is
2857    an implementation accident and this semantics is not guaranteed to
2858    the user.  */
2859 tree
2860 c_build_vec_shuffle_expr (location_t loc, tree v0, tree v1, tree mask)
2861 {
2862   tree vec_shuffle;
2863   bool wrap = true;
2864   bool maybe_const = false;
2865   bool two_arguments = false;
2866
2867   if (v1 == NULL_TREE)
2868     {
2869       two_arguments = true;
2870       v1 = v0;
2871     }
2872
2873   if (v0 == error_mark_node || v1 == error_mark_node
2874       || mask == error_mark_node)
2875     return error_mark_node;
2876
2877   if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2878       || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2879     {
2880       error_at (loc, "__builtin_shuffle last argument must "
2881                      "be an integer vector");
2882       return error_mark_node;
2883     }
2884
2885   if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2886       || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2887     {
2888       error_at (loc, "__builtin_shuffle arguments must be vectors");
2889       return error_mark_node;
2890     }
2891
2892   if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2893     {
2894       error_at (loc, "__builtin_shuffle argument vectors must be of "
2895                      "the same type");
2896       return error_mark_node;
2897     }
2898
2899   if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2900       != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2901       && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2902          != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2903     {
2904       error_at (loc, "__builtin_shuffle number of elements of the "
2905                      "argument vector(s) and the mask vector should "
2906                      "be the same");
2907       return error_mark_node;
2908     }
2909
2910   if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2911       != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2912     {
2913       error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2914                      "must have the same size as inner type of the mask");
2915       return error_mark_node;
2916     }
2917
2918   /* Avoid C_MAYBE_CONST_EXPRs inside VEC_SHUFFLE_EXPR.  */
2919   v0 = c_fully_fold (v0, false, &maybe_const);
2920   wrap &= maybe_const;
2921
2922   if (two_arguments)
2923     v1 = v0 = save_expr (v0);
2924   else
2925     {
2926       v1 = c_fully_fold (v1, false, &maybe_const);
2927       wrap &= maybe_const;
2928     }
2929
2930   mask = c_fully_fold (mask, false, &maybe_const);
2931   wrap &= maybe_const;
2932
2933   vec_shuffle = build3 (VEC_SHUFFLE_EXPR, TREE_TYPE (v0), v0, v1, mask);
2934
2935   if (!wrap)
2936     vec_shuffle = c_wrap_maybe_const (vec_shuffle, true);
2937
2938   return vec_shuffle;
2939 }
2940 \f
2941 /* Convert the argument expressions in the vector VALUES
2942    to the types in the list TYPELIST.
2943
2944    If TYPELIST is exhausted, or when an element has NULL as its type,
2945    perform the default conversions.
2946
2947    ORIGTYPES is the original types of the expressions in VALUES.  This
2948    holds the type of enum values which have been converted to integral
2949    types.  It may be NULL.
2950
2951    FUNCTION is a tree for the called function.  It is used only for
2952    error messages, where it is formatted with %qE.
2953
2954    This is also where warnings about wrong number of args are generated.
2955
2956    Returns the actual number of arguments processed (which may be less
2957    than the length of VALUES in some error situations), or -1 on
2958    failure.  */
2959
2960 static int
2961 convert_arguments (tree typelist, VEC(tree,gc) *values,
2962                    VEC(tree,gc) *origtypes, tree function, tree fundecl)
2963 {
2964   tree typetail, val;
2965   unsigned int parmnum;
2966   bool error_args = false;
2967   const bool type_generic = fundecl
2968     && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2969   bool type_generic_remove_excess_precision = false;
2970   tree selector;
2971
2972   /* Change pointer to function to the function itself for
2973      diagnostics.  */
2974   if (TREE_CODE (function) == ADDR_EXPR
2975       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2976     function = TREE_OPERAND (function, 0);
2977
2978   /* Handle an ObjC selector specially for diagnostics.  */
2979   selector = objc_message_selector ();
2980
2981   /* For type-generic built-in functions, determine whether excess
2982      precision should be removed (classification) or not
2983      (comparison).  */
2984   if (type_generic
2985       && DECL_BUILT_IN (fundecl)
2986       && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2987     {
2988       switch (DECL_FUNCTION_CODE (fundecl))
2989         {
2990         case BUILT_IN_ISFINITE:
2991         case BUILT_IN_ISINF:
2992         case BUILT_IN_ISINF_SIGN:
2993         case BUILT_IN_ISNAN:
2994         case BUILT_IN_ISNORMAL:
2995         case BUILT_IN_FPCLASSIFY:
2996           type_generic_remove_excess_precision = true;
2997           break;
2998
2999         default:
3000           type_generic_remove_excess_precision = false;
3001           break;
3002         }
3003     }
3004
3005   /* Scan the given expressions and types, producing individual
3006      converted arguments.  */
3007
3008   for (typetail = typelist, parmnum = 0;
3009        VEC_iterate (tree, values, parmnum, val);
3010        ++parmnum)
3011     {
3012       tree type = typetail ? TREE_VALUE (typetail) : 0;
3013       tree valtype = TREE_TYPE (val);
3014       tree rname = function;
3015       int argnum = parmnum + 1;
3016       const char *invalid_func_diag;
3017       bool excess_precision = false;
3018       bool npc;
3019       tree parmval;
3020
3021       if (type == void_type_node)
3022         {
3023           if (selector)
3024             error_at (input_location,
3025                       "too many arguments to method %qE", selector);
3026           else
3027             error_at (input_location,
3028                       "too many arguments to function %qE", function);
3029
3030           if (fundecl && !DECL_BUILT_IN (fundecl))
3031             inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3032           return parmnum;
3033         }
3034
3035       if (selector && argnum > 2)
3036         {
3037           rname = selector;
3038           argnum -= 2;
3039         }
3040
3041       npc = null_pointer_constant_p (val);
3042
3043       /* If there is excess precision and a prototype, convert once to
3044          the required type rather than converting via the semantic
3045          type.  Likewise without a prototype a float value represented
3046          as long double should be converted once to double.  But for
3047          type-generic classification functions excess precision must
3048          be removed here.  */
3049       if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3050           && (type || !type_generic || !type_generic_remove_excess_precision))
3051         {
3052           val = TREE_OPERAND (val, 0);
3053           excess_precision = true;
3054         }
3055       val = c_fully_fold (val, false, NULL);
3056       STRIP_TYPE_NOPS (val);
3057
3058       val = require_complete_type (val);
3059
3060       if (type != 0)
3061         {
3062           /* Formal parm type is specified by a function prototype.  */
3063
3064           if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3065             {
3066               error ("type of formal parameter %d is incomplete", parmnum + 1);
3067               parmval = val;
3068             }
3069           else
3070             {
3071               tree origtype;
3072
3073               /* Optionally warn about conversions that
3074                  differ from the default conversions.  */
3075               if (warn_traditional_conversion || warn_traditional)
3076                 {
3077                   unsigned int formal_prec = TYPE_PRECISION (type);
3078
3079                   if (INTEGRAL_TYPE_P (type)
3080                       && TREE_CODE (valtype) == REAL_TYPE)
3081                     warning (0, "passing argument %d of %qE as integer "
3082                              "rather than floating due to prototype",
3083                              argnum, rname);
3084                   if (INTEGRAL_TYPE_P (type)
3085                       && TREE_CODE (valtype) == COMPLEX_TYPE)
3086                     warning (0, "passing argument %d of %qE as integer "
3087                              "rather than complex due to prototype",
3088                              argnum, rname);
3089                   else if (TREE_CODE (type) == COMPLEX_TYPE
3090                            && TREE_CODE (valtype) == REAL_TYPE)
3091                     warning (0, "passing argument %d of %qE as complex "
3092                              "rather than floating due to prototype",
3093                              argnum, rname);
3094                   else if (TREE_CODE (type) == REAL_TYPE
3095                            && INTEGRAL_TYPE_P (valtype))
3096                     warning (0, "passing argument %d of %qE as floating "
3097                              "rather than integer due to prototype",
3098                              argnum, rname);
3099                   else if (TREE_CODE (type) == COMPLEX_TYPE
3100                            && INTEGRAL_TYPE_P (valtype))
3101                     warning (0, "passing argument %d of %qE as complex "
3102                              "rather than integer due to prototype",
3103                              argnum, rname);
3104                   else if (TREE_CODE (type) == REAL_TYPE
3105                            && TREE_CODE (valtype) == COMPLEX_TYPE)
3106                     warning (0, "passing argument %d of %qE as floating "
3107                              "rather than complex due to prototype",
3108                              argnum, rname);
3109                   /* ??? At some point, messages should be written about
3110                      conversions between complex types, but that's too messy
3111                      to do now.  */
3112                   else if (TREE_CODE (type) == REAL_TYPE
3113                            && TREE_CODE (valtype) == REAL_TYPE)
3114                     {
3115                       /* Warn if any argument is passed as `float',
3116                          since without a prototype it would be `double'.  */
3117                       if (formal_prec == TYPE_PRECISION (float_type_node)
3118                           && type != dfloat32_type_node)
3119                         warning (0, "passing argument %d of %qE as %<float%> "
3120                                  "rather than %<double%> due to prototype",
3121                                  argnum, rname);
3122
3123                       /* Warn if mismatch between argument and prototype
3124                          for decimal float types.  Warn of conversions with
3125                          binary float types and of precision narrowing due to
3126                          prototype. */
3127                       else if (type != valtype
3128                                && (type == dfloat32_type_node
3129                                    || type == dfloat64_type_node
3130                                    || type == dfloat128_type_node
3131                                    || valtype == dfloat32_type_node
3132                                    || valtype == dfloat64_type_node
3133                                    || valtype == dfloat128_type_node)
3134                                && (formal_prec
3135                                    <= TYPE_PRECISION (valtype)
3136                                    || (type == dfloat128_type_node
3137                                        && (valtype
3138                                            != dfloat64_type_node
3139                                            && (valtype
3140                                                != dfloat32_type_node)))
3141                                    || (type == dfloat64_type_node
3142                                        && (valtype
3143                                            != dfloat32_type_node))))
3144                         warning (0, "passing argument %d of %qE as %qT "
3145                                  "rather than %qT due to prototype",
3146                                  argnum, rname, type, valtype);
3147
3148                     }
3149                   /* Detect integer changing in width or signedness.
3150                      These warnings are only activated with
3151                      -Wtraditional-conversion, not with -Wtraditional.  */
3152                   else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3153                            && INTEGRAL_TYPE_P (valtype))
3154                     {
3155                       tree would_have_been = default_conversion (val);
3156                       tree type1 = TREE_TYPE (would_have_been);
3157
3158                       if (TREE_CODE (type) == ENUMERAL_TYPE
3159                           && (TYPE_MAIN_VARIANT (type)
3160                               == TYPE_MAIN_VARIANT (valtype)))
3161                         /* No warning if function asks for enum
3162                            and the actual arg is that enum type.  */
3163                         ;
3164                       else if (formal_prec != TYPE_PRECISION (type1))
3165                         warning (OPT_Wtraditional_conversion,
3166                                  "passing argument %d of %qE "
3167                                  "with different width due to prototype",
3168                                  argnum, rname);
3169                       else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3170                         ;
3171                       /* Don't complain if the formal parameter type
3172                          is an enum, because we can't tell now whether
3173                          the value was an enum--even the same enum.  */
3174                       else if (TREE_CODE (type) == ENUMERAL_TYPE)
3175                         ;
3176                       else if (TREE_CODE (val) == INTEGER_CST
3177                                && int_fits_type_p (val, type))
3178                         /* Change in signedness doesn't matter
3179                            if a constant value is unaffected.  */
3180                         ;
3181                       /* If the value is extended from a narrower
3182                          unsigned type, it doesn't matter whether we
3183                          pass it as signed or unsigned; the value
3184                          certainly is the same either way.  */
3185                       else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3186                                && TYPE_UNSIGNED (valtype))
3187                         ;
3188                       else if (TYPE_UNSIGNED (type))
3189                         warning (OPT_Wtraditional_conversion,
3190                                  "passing argument %d of %qE "
3191                                  "as unsigned due to prototype",
3192                                  argnum, rname);
3193                       else
3194                         warning (OPT_Wtraditional_conversion,
3195                                  "passing argument %d of %qE "
3196                                  "as signed due to prototype", argnum, rname);
3197                     }
3198                 }
3199
3200               /* Possibly restore an EXCESS_PRECISION_EXPR for the
3201                  sake of better warnings from convert_and_check.  */
3202               if (excess_precision)
3203                 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3204               origtype = (origtypes == NULL
3205                           ? NULL_TREE
3206                           : VEC_index (tree, origtypes, parmnum));
3207               parmval = convert_for_assignment (input_location, type, val,
3208                                                 origtype, ic_argpass, npc,
3209                                                 fundecl, function,
3210                                                 parmnum + 1);
3211
3212               if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3213                   && INTEGRAL_TYPE_P (type)
3214                   && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3215                 parmval = default_conversion (parmval);
3216             }
3217         }
3218       else if (TREE_CODE (valtype) == REAL_TYPE
3219                && (TYPE_PRECISION (valtype)
3220                    < TYPE_PRECISION (double_type_node))
3221                && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3222         {
3223           if (type_generic)
3224             parmval = val;
3225           else
3226             {
3227               /* Convert `float' to `double'.  */
3228               if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3229                 warning (OPT_Wdouble_promotion,
3230                          "implicit conversion from %qT to %qT when passing "
3231                          "argument to function",
3232                          valtype, double_type_node);
3233               parmval = convert (double_type_node, val);
3234             }
3235         }
3236       else if (excess_precision && !type_generic)
3237         /* A "double" argument with excess precision being passed
3238            without a prototype or in variable arguments.  */
3239         parmval = convert (valtype, val);
3240       else if ((invalid_func_diag =
3241                 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3242         {
3243           error (invalid_func_diag);
3244           return -1;
3245         }
3246       else
3247         /* Convert `short' and `char' to full-size `int'.  */
3248         parmval = default_conversion (val);
3249
3250       VEC_replace (tree, values, parmnum, parmval);
3251       if (parmval == error_mark_node)
3252         error_args = true;
3253
3254       if (typetail)
3255         typetail = TREE_CHAIN (typetail);
3256     }
3257
3258   gcc_assert (parmnum == VEC_length (tree, values));
3259
3260   if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3261     {
3262       error_at (input_location,
3263                 "too few arguments to function %qE", function);
3264       if (fundecl && !DECL_BUILT_IN (fundecl))
3265         inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3266       return -1;
3267     }
3268
3269   return error_args ? -1 : (int) parmnum;
3270 }
3271 \f
3272 /* This is the entry point used by the parser to build unary operators
3273    in the input.  CODE, a tree_code, specifies the unary operator, and
3274    ARG is the operand.  For unary plus, the C parser currently uses
3275    CONVERT_EXPR for code.
3276
3277    LOC is the location to use for the tree generated.
3278 */
3279
3280 struct c_expr
3281 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3282 {
3283   struct c_expr result;
3284
3285   result.value = build_unary_op (loc, code, arg.value, 0);
3286   result.original_code = code;
3287   result.original_type = NULL;
3288
3289   if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3290     overflow_warning (loc, result.value);
3291
3292   return result;
3293 }
3294
3295 /* This is the entry point used by the parser to build binary operators
3296    in the input.  CODE, a tree_code, specifies the binary operator, and
3297    ARG1 and ARG2 are the operands.  In addition to constructing the
3298    expression, we check for operands that were written with other binary
3299    operators in a way that is likely to confuse the user.
3300
3301    LOCATION is the location of the binary operator.  */
3302
3303 struct c_expr
3304 parser_build_binary_op (location_t location, enum tree_code code,
3305                         struct c_expr arg1, struct c_expr arg2)
3306 {
3307   struct c_expr result;
3308
3309   enum tree_code code1 = arg1.original_code;
3310   enum tree_code code2 = arg2.original_code;
3311   tree type1 = (arg1.original_type
3312                 ? arg1.original_type
3313                 : TREE_TYPE (arg1.value));
3314   tree type2 = (arg2.original_type
3315                 ? arg2.original_type
3316                 : TREE_TYPE (arg2.value));
3317
3318   result.value = build_binary_op (location, code,
3319                                   arg1.value, arg2.value, 1);
3320   result.original_code = code;
3321   result.original_type = NULL;
3322
3323   if (TREE_CODE (result.value) == ERROR_MARK)
3324     return result;
3325
3326   if (location != UNKNOWN_LOCATION)
3327     protected_set_expr_location (result.value, location);
3328
3329   /* Check for cases such as x+y<<z which users are likely
3330      to misinterpret.  */
3331   if (warn_parentheses)
3332     warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3333
3334   if (warn_logical_op)
3335     warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3336                            code1, arg1.value, code2, arg2.value);
3337
3338   /* Warn about comparisons against string literals, with the exception
3339      of testing for equality or inequality of a string literal with NULL.  */
3340   if (code == EQ_EXPR || code == NE_EXPR)
3341     {
3342       if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3343           || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3344         warning_at (location, OPT_Waddress,
3345                     "comparison with string literal results in unspecified behavior");
3346     }
3347   else if (TREE_CODE_CLASS (code) == tcc_comparison
3348            && (code1 == STRING_CST || code2 == STRING_CST))
3349     warning_at (location, OPT_Waddress,
3350                 "comparison with string literal results in unspecified behavior");
3351
3352   if (TREE_OVERFLOW_P (result.value)
3353       && !TREE_OVERFLOW_P (arg1.value)
3354       && !TREE_OVERFLOW_P (arg2.value))
3355     overflow_warning (location, result.value);
3356
3357   /* Warn about comparisons of different enum types.  */
3358   if (warn_enum_compare
3359       && TREE_CODE_CLASS (code) == tcc_comparison
3360       && TREE_CODE (type1) == ENUMERAL_TYPE
3361       && TREE_CODE (type2) == ENUMERAL_TYPE
3362       && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3363     warning_at (location, OPT_Wenum_compare,
3364                 "comparison between %qT and %qT",
3365                 type1, type2);
3366
3367   return result;
3368 }
3369 \f
3370 /* Return a tree for the difference of pointers OP0 and OP1.
3371    The resulting tree has type int.  */
3372
3373 static tree
3374 pointer_diff (location_t loc, tree op0, tree op1)
3375 {
3376   tree restype = ptrdiff_type_node;
3377   tree result, inttype;
3378
3379   addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3380   addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3381   tree target_type = TREE_TYPE (TREE_TYPE (op0));
3382   tree con0, con1, lit0, lit1;
3383   tree orig_op1 = op1;
3384
3385   /* If the operands point into different address spaces, we need to
3386      explicitly convert them to pointers into the common address space
3387      before we can subtract the numerical address values.  */
3388   if (as0 != as1)
3389     {
3390       addr_space_t as_common;
3391       tree common_type;
3392
3393       /* Determine the common superset address space.  This is guaranteed
3394          to exist because the caller verified that comp_target_types
3395          returned non-zero.  */
3396       if (!addr_space_superset (as0, as1, &as_common))
3397         gcc_unreachable ();
3398
3399       common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3400       op0 = convert (common_type, op0);
3401       op1 = convert (common_type, op1);
3402     }
3403
3404   /* Determine integer type to perform computations in.  This will usually
3405      be the same as the result type (ptrdiff_t), but may need to be a wider
3406      type if pointers for the address space are wider than ptrdiff_t.  */
3407   if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3408     inttype = lang_hooks.types.type_for_size
3409                 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3410   else
3411     inttype = restype;
3412
3413
3414   if (TREE_CODE (target_type) == VOID_TYPE)
3415     pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3416              "pointer of type %<void *%> used in subtraction");
3417   if (TREE_CODE (target_type) == FUNCTION_TYPE)
3418     pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3419              "pointer to a function used in subtraction");
3420
3421   /* If the conversion to ptrdiff_type does anything like widening or
3422      converting a partial to an integral mode, we get a convert_expression
3423      that is in the way to do any simplifications.
3424      (fold-const.c doesn't know that the extra bits won't be needed.
3425      split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3426      different mode in place.)
3427      So first try to find a common term here 'by hand'; we want to cover
3428      at least the cases that occur in legal static initializers.  */
3429   if (CONVERT_EXPR_P (op0)
3430       && (TYPE_PRECISION (TREE_TYPE (op0))
3431           == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3432     con0 = TREE_OPERAND (op0, 0);
3433   else
3434     con0 = op0;
3435   if (CONVERT_EXPR_P (op1)
3436       && (TYPE_PRECISION (TREE_TYPE (op1))
3437           == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3438     con1 = TREE_OPERAND (op1, 0);
3439   else
3440     con1 = op1;
3441
3442   if (TREE_CODE (con0) == PLUS_EXPR)
3443     {
3444       lit0 = TREE_OPERAND (con0, 1);
3445       con0 = TREE_OPERAND (con0, 0);
3446     }
3447   else
3448     lit0 = integer_zero_node;
3449
3450   if (TREE_CODE (con1) == PLUS_EXPR)
3451     {
3452       lit1 = TREE_OPERAND (con1, 1);
3453       con1 = TREE_OPERAND (con1, 0);
3454     }
3455   else
3456     lit1 = integer_zero_node;
3457
3458   if (operand_equal_p (con0, con1, 0))
3459     {
3460       op0 = lit0;
3461       op1 = lit1;
3462     }
3463
3464
3465   /* First do the subtraction as integers;
3466      then drop through to build the divide operator.
3467      Do not do default conversions on the minus operator
3468      in case restype is a short type.  */
3469
3470   op0 = build_binary_op (loc,
3471                          MINUS_EXPR, convert (inttype, op0),
3472                          convert (inttype, op1), 0);
3473   /* This generates an error if op1 is pointer to incomplete type.  */
3474   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3475     error_at (loc, "arithmetic on pointer to an incomplete type");
3476
3477   /* This generates an error if op0 is pointer to incomplete type.  */
3478   op1 = c_size_in_bytes (target_type);
3479
3480   /* Divide by the size, in easiest possible way.  */
3481   result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3482                             op0, convert (inttype, op1));
3483
3484   /* Convert to final result type if necessary.  */
3485   return convert (restype, result);
3486 }
3487 \f
3488 /* Construct and perhaps optimize a tree representation
3489    for a unary operation.  CODE, a tree_code, specifies the operation
3490    and XARG is the operand.
3491    For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3492    the default promotions (such as from short to int).
3493    For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3494    allows non-lvalues; this is only used to handle conversion of non-lvalue
3495    arrays to pointers in C99.
3496
3497    LOCATION is the location of the operator.  */
3498
3499 tree
3500 build_unary_op (location_t location,
3501                 enum tree_code code, tree xarg, int flag)
3502 {
3503   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
3504   tree arg = xarg;
3505   tree argtype = 0;
3506   enum tree_code typecode;
3507   tree val;
3508   tree ret = error_mark_node;
3509   tree eptype = NULL_TREE;
3510   int noconvert = flag;
3511   const char *invalid_op_diag;
3512   bool int_operands;
3513
3514   int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3515   if (int_operands)
3516     arg = remove_c_maybe_const_expr (arg);
3517
3518   if (code != ADDR_EXPR)
3519     arg = require_complete_type (arg);
3520
3521   typecode = TREE_CODE (TREE_TYPE (arg));
3522   if (typecode == ERROR_MARK)
3523     return error_mark_node;
3524   if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3525     typecode = INTEGER_TYPE;
3526
3527   if ((invalid_op_diag
3528        = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3529     {
3530       error_at (location, invalid_op_diag);
3531       return error_mark_node;
3532     }
3533
3534   if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3535     {
3536       eptype = TREE_TYPE (arg);
3537       arg = TREE_OPERAND (arg, 0);
3538     }
3539
3540   switch (code)
3541     {
3542     case CONVERT_EXPR:
3543       /* This is used for unary plus, because a CONVERT_EXPR
3544          is enough to prevent anybody from looking inside for
3545          associativity, but won't generate any code.  */
3546       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3547             || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3548             || typecode == VECTOR_TYPE))
3549         {
3550           error_at (location, "wrong type argument to unary plus");
3551           return error_mark_node;
3552         }
3553       else if (!noconvert)
3554         arg = default_conversion (arg);
3555       arg = non_lvalue_loc (location, arg);
3556       break;
3557
3558     case NEGATE_EXPR:
3559       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3560             || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3561             || typecode == VECTOR_TYPE))
3562         {
3563           error_at (location, "wrong type argument to unary minus");
3564           return error_mark_node;
3565         }
3566       else if (!noconvert)
3567         arg = default_conversion (arg);
3568       break;
3569
3570     case BIT_NOT_EXPR:
3571       /* ~ works on integer types and non float vectors. */
3572       if (typecode == INTEGER_TYPE
3573           || (typecode == VECTOR_TYPE
3574               && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3575         {
3576           if (!noconvert)
3577             arg = default_conversion (arg);
3578         }
3579       else if (typecode == COMPLEX_TYPE)
3580         {
3581           code = CONJ_EXPR;
3582           pedwarn (location, OPT_pedantic,
3583                    "ISO C does not support %<~%> for complex conjugation");
3584           if (!noconvert)
3585             arg = default_conversion (arg);
3586         }
3587       else
3588         {
3589           error_at (location, "wrong type argument to bit-complement");
3590           return error_mark_node;
3591         }
3592       break;
3593
3594     case ABS_EXPR:
3595       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3596         {
3597           error_at (location, "wrong type argument to abs");
3598           return error_mark_node;
3599         }
3600       else if (!noconvert)
3601         arg = default_conversion (arg);
3602       break;
3603
3604     case CONJ_EXPR:
3605       /* Conjugating a real value is a no-op, but allow it anyway.  */
3606       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3607             || typecode == COMPLEX_TYPE))
3608         {
3609           error_at (location, "wrong type argument to conjugation");
3610           return error_mark_node;
3611         }
3612       else if (!noconvert)
3613         arg = default_conversion (arg);
3614       break;
3615
3616     case TRUTH_NOT_EXPR:
3617       if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3618           && typecode != REAL_TYPE && typecode != POINTER_TYPE
3619           && typecode != COMPLEX_TYPE)
3620         {
3621           error_at (location,
3622                     "wrong type argument to unary exclamation mark");
3623           return error_mark_node;
3624         }
3625       arg = c_objc_common_truthvalue_conversion (location, arg);
3626       ret = invert_truthvalue_loc (location, arg);
3627       /* If the TRUTH_NOT_EXPR has been folded, reset the location.  */
3628       if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3629         location = EXPR_LOCATION (ret);
3630       goto return_build_unary_op;
3631
3632     case REALPART_EXPR:
3633     case IMAGPART_EXPR:
3634       ret = build_real_imag_expr (location, code, arg);
3635       if (ret == error_mark_node)
3636         return error_mark_node;
3637       if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3638         eptype = TREE_TYPE (eptype);
3639       goto return_build_unary_op;
3640
3641     case PREINCREMENT_EXPR:
3642     case POSTINCREMENT_EXPR:
3643     case PREDECREMENT_EXPR:
3644     case POSTDECREMENT_EXPR:
3645
3646       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3647         {
3648           tree inner = build_unary_op (location, code,
3649                                        C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3650           if (inner == error_mark_node)
3651             return error_mark_node;
3652           ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3653                         C_MAYBE_CONST_EXPR_PRE (arg), inner);
3654           gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3655           C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3656           goto return_build_unary_op;
3657         }
3658
3659       /* Complain about anything that is not a true lvalue.  In
3660          Objective-C, skip this check for property_refs.  */
3661       if (!objc_is_property_ref (arg)
3662           && !lvalue_or_else (location,
3663                               arg, ((code == PREINCREMENT_EXPR
3664                                      || code == POSTINCREMENT_EXPR)
3665                                     ? lv_increment
3666                                     : lv_decrement)))
3667         return error_mark_node;
3668
3669       if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3670         {
3671           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3672             warning_at (location, OPT_Wc___compat,
3673                         "increment of enumeration value is invalid in C++");
3674           else
3675             warning_at (location, OPT_Wc___compat,
3676                         "decrement of enumeration value is invalid in C++");
3677         }
3678
3679       /* Ensure the argument is fully folded inside any SAVE_EXPR.  */
3680       arg = c_fully_fold (arg, false, NULL);
3681
3682       /* Increment or decrement the real part of the value,
3683          and don't change the imaginary part.  */
3684       if (typecode == COMPLEX_TYPE)
3685         {
3686           tree real, imag;
3687
3688           pedwarn (location, OPT_pedantic,
3689                    "ISO C does not support %<++%> and %<--%> on complex types");
3690
3691           arg = stabilize_reference (arg);
3692           real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3693           imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3694           real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3695           if (real == error_mark_node || imag == error_mark_node)
3696             return error_mark_node;
3697           ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3698                         real, imag);
3699           goto return_build_unary_op;
3700         }
3701
3702       /* Report invalid types.  */
3703
3704       if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3705           && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3706         {
3707           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3708             error_at (location, "wrong type argument to increment");
3709           else
3710             error_at (location, "wrong type argument to decrement");
3711
3712           return error_mark_node;
3713         }
3714
3715       {
3716         tree inc;
3717
3718         argtype = TREE_TYPE (arg);
3719
3720         /* Compute the increment.  */
3721
3722         if (typecode == POINTER_TYPE)
3723           {
3724             /* If pointer target is an undefined struct,
3725                we just cannot know how to do the arithmetic.  */
3726             if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3727               {
3728                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3729                   error_at (location,
3730                             "increment of pointer to unknown structure");
3731                 else
3732                   error_at (location,
3733                             "decrement of pointer to unknown structure");
3734               }
3735             else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3736                      || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3737               {
3738                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3739                   pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3740                            "wrong type argument to increment");
3741                 else
3742                   pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3743                            "wrong type argument to decrement");
3744               }
3745
3746             inc = c_size_in_bytes (TREE_TYPE (argtype));
3747             inc = convert_to_ptrofftype_loc (location, inc);
3748           }
3749         else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3750           {
3751             /* For signed fract types, we invert ++ to -- or
3752                -- to ++, and change inc from 1 to -1, because
3753                it is not possible to represent 1 in signed fract constants.
3754                For unsigned fract types, the result always overflows and
3755                we get an undefined (original) or the maximum value.  */
3756             if (code == PREINCREMENT_EXPR)
3757               code = PREDECREMENT_EXPR;
3758             else if (code == PREDECREMENT_EXPR)
3759               code = PREINCREMENT_EXPR;
3760             else if (code == POSTINCREMENT_EXPR)
3761               code = POSTDECREMENT_EXPR;
3762             else /* code == POSTDECREMENT_EXPR  */
3763               code = POSTINCREMENT_EXPR;
3764
3765             inc = integer_minus_one_node;
3766             inc = convert (argtype, inc);
3767           }
3768         else
3769           {
3770             inc = integer_one_node;
3771             inc = convert (argtype, inc);
3772           }
3773
3774         /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3775            need to ask Objective-C to build the increment or decrement
3776            expression for it.  */
3777         if (objc_is_property_ref (arg))
3778           return objc_build_incr_expr_for_property_ref (location, code,
3779                                                         arg, inc);
3780
3781         /* Report a read-only lvalue.  */
3782         if (TYPE_READONLY (argtype))
3783           {
3784             readonly_error (arg,
3785                             ((code == PREINCREMENT_EXPR
3786                               || code == POSTINCREMENT_EXPR)
3787                              ? lv_increment : lv_decrement));
3788             return error_mark_node;
3789           }
3790         else if (TREE_READONLY (arg))
3791           readonly_warning (arg,
3792                             ((code == PREINCREMENT_EXPR
3793                               || code == POSTINCREMENT_EXPR)
3794                              ? lv_increment : lv_decrement));
3795
3796         if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3797           val = boolean_increment (code, arg);
3798         else
3799           val = build2 (code, TREE_TYPE (arg), arg, inc);
3800         TREE_SIDE_EFFECTS (val) = 1;
3801         if (TREE_CODE (val) != code)
3802           TREE_NO_WARNING (val) = 1;
3803         ret = val;
3804         goto return_build_unary_op;
3805       }
3806
3807     case ADDR_EXPR:
3808       /* Note that this operation never does default_conversion.  */
3809
3810       /* The operand of unary '&' must be an lvalue (which excludes
3811          expressions of type void), or, in C99, the result of a [] or
3812          unary '*' operator.  */
3813       if (VOID_TYPE_P (TREE_TYPE (arg))
3814           && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3815           && (TREE_CODE (arg) != INDIRECT_REF
3816               || !flag_isoc99))
3817         pedwarn (location, 0, "taking address of expression of type %<void%>");
3818
3819       /* Let &* cancel out to simplify resulting code.  */
3820       if (TREE_CODE (arg) == INDIRECT_REF)
3821         {
3822           /* Don't let this be an lvalue.  */
3823           if (lvalue_p (TREE_OPERAND (arg, 0)))
3824             return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3825           ret = TREE_OPERAND (arg, 0);
3826           goto return_build_unary_op;
3827         }
3828
3829       /* For &x[y], return x+y */
3830       if (TREE_CODE (arg) == ARRAY_REF)
3831         {
3832           tree op0 = TREE_OPERAND (arg, 0);
3833           if (!c_mark_addressable (op0))
3834             return error_mark_node;
3835         }
3836
3837       /* Anything not already handled and not a true memory reference
3838          or a non-lvalue array is an error.  */
3839       else if (typecode != FUNCTION_TYPE && !flag
3840                && !lvalue_or_else (location, arg, lv_addressof))
3841         return error_mark_node;
3842
3843       /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3844          folding later.  */
3845       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3846         {
3847           tree inner = build_unary_op (location, code,
3848                                        C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3849           ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3850                         C_MAYBE_CONST_EXPR_PRE (arg), inner);
3851           gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3852           C_MAYBE_CONST_EXPR_NON_CONST (ret)
3853             = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3854           goto return_build_unary_op;
3855         }
3856
3857       /* Ordinary case; arg is a COMPONENT_REF or a decl.  */
3858       argtype = TREE_TYPE (arg);
3859
3860       /* If the lvalue is const or volatile, merge that into the type
3861          to which the address will point.  This is only needed
3862          for function types.  */
3863       if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3864           && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3865           && TREE_CODE (argtype) == FUNCTION_TYPE)
3866         {
3867           int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3868           int quals = orig_quals;
3869
3870           if (TREE_READONLY (arg))
3871             quals |= TYPE_QUAL_CONST;
3872           if (TREE_THIS_VOLATILE (arg))
3873             quals |= TYPE_QUAL_VOLATILE;
3874
3875           argtype = c_build_qualified_type (argtype, quals);
3876         }
3877
3878       if (!c_mark_addressable (arg))
3879         return error_mark_node;
3880
3881       gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3882                   || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3883
3884       argtype = build_pointer_type (argtype);
3885
3886       /* ??? Cope with user tricks that amount to offsetof.  Delete this
3887          when we have proper support for integer constant expressions.  */
3888       val = get_base_address (arg);
3889       if (val && TREE_CODE (val) == INDIRECT_REF
3890           && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3891         {
3892           tree op0 = fold_offsetof (arg, val), op1;
3893
3894           op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3895           ret = fold_build_pointer_plus_loc (location, op1, op0);
3896           goto return_build_unary_op;
3897         }
3898
3899       val = build1 (ADDR_EXPR, argtype, arg);
3900
3901       ret = val;
3902       goto return_build_unary_op;
3903
3904     default:
3905       gcc_unreachable ();
3906     }
3907
3908   if (argtype == 0)
3909     argtype = TREE_TYPE (arg);
3910   if (TREE_CODE (arg) == INTEGER_CST)
3911     ret = (require_constant_value
3912            ? fold_build1_initializer_loc (location, code, argtype, arg)
3913            : fold_build1_loc (location, code, argtype, arg));
3914   else
3915     ret = build1 (code, argtype, arg);
3916  return_build_unary_op:
3917   gcc_assert (ret != error_mark_node);
3918   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3919       && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3920     ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3921   else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3922     ret = note_integer_operands (ret);
3923   if (eptype)
3924     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3925   protected_set_expr_location (ret, location);
3926   return ret;
3927 }
3928
3929 /* Return nonzero if REF is an lvalue valid for this language.
3930    Lvalues can be assigned, unless their type has TYPE_READONLY.
3931    Lvalues can have their address taken, unless they have C_DECL_REGISTER.  */
3932
3933 bool
3934 lvalue_p (const_tree ref)
3935 {
3936   const enum tree_code code = TREE_CODE (ref);
3937
3938   switch (code)
3939     {
3940     case REALPART_EXPR:
3941     case IMAGPART_EXPR:
3942     case COMPONENT_REF:
3943       return lvalue_p (TREE_OPERAND (ref, 0));
3944
3945     case C_MAYBE_CONST_EXPR:
3946       return lvalue_p (TREE_OPERAND (ref, 1));
3947
3948     case COMPOUND_LITERAL_EXPR:
3949     case STRING_CST:
3950       return 1;
3951
3952     case INDIRECT_REF:
3953     case ARRAY_REF:
3954     case VAR_DECL:
3955     case PARM_DECL:
3956     case RESULT_DECL:
3957     case ERROR_MARK:
3958       return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3959               && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3960
3961     case BIND_EXPR:
3962       return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3963
3964     default:
3965       return 0;
3966     }
3967 }
3968 \f
3969 /* Give a warning for storing in something that is read-only in GCC
3970    terms but not const in ISO C terms.  */
3971
3972 static void
3973 readonly_warning (tree arg, enum lvalue_use use)
3974 {
3975   switch (use)
3976     {
3977     case lv_assign:
3978       warning (0, "assignment of read-only location %qE", arg);
3979       break;
3980     case lv_increment:
3981       warning (0, "increment of read-only location %qE", arg);
3982       break;
3983     case lv_decrement:
3984       warning (0, "decrement of read-only location %qE", arg);
3985       break;
3986     default:
3987       gcc_unreachable ();
3988     }
3989   return;
3990 }
3991
3992
3993 /* Return nonzero if REF is an lvalue valid for this language;
3994    otherwise, print an error message and return zero.  USE says
3995    how the lvalue is being used and so selects the error message.
3996    LOCATION is the location at which any error should be reported.  */
3997
3998 static int
3999 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4000 {
4001   int win = lvalue_p (ref);
4002
4003   if (!win)
4004     lvalue_error (loc, use);
4005
4006   return win;
4007 }
4008 \f
4009 /* Mark EXP saying that we need to be able to take the
4010    address of it; it should not be allocated in a register.
4011    Returns true if successful.  */
4012
4013 bool
4014 c_mark_addressable (tree exp)
4015 {
4016   tree x = exp;
4017
4018   while (1)
4019     switch (TREE_CODE (x))
4020       {
4021       case COMPONENT_REF:
4022         if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4023           {
4024             error
4025               ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
4026             return false;
4027           }
4028
4029         /* ... fall through ...  */
4030
4031       case ADDR_EXPR:
4032       case ARRAY_REF:
4033       case REALPART_EXPR:
4034       case IMAGPART_EXPR:
4035         x = TREE_OPERAND (x, 0);
4036         break;
4037
4038       case COMPOUND_LITERAL_EXPR:
4039       case CONSTRUCTOR:
4040         TREE_ADDRESSABLE (x) = 1;
4041         return true;
4042
4043       case VAR_DECL:
4044       case CONST_DECL:
4045       case PARM_DECL:
4046       case RESULT_DECL:
4047         if (C_DECL_REGISTER (x)
4048             && DECL_NONLOCAL (x))
4049           {
4050             if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4051               {
4052                 error
4053                   ("global register variable %qD used in nested function", x);
4054                 return false;
4055               }
4056             pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4057           }
4058         else if (C_DECL_REGISTER (x))
4059           {
4060             if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4061               error ("address of global register variable %qD requested", x);
4062             else
4063               error ("address of register variable %qD requested", x);
4064             return false;
4065           }
4066
4067         /* drops in */
4068       case FUNCTION_DECL:
4069         TREE_ADDRESSABLE (x) = 1;
4070         /* drops out */
4071       default:
4072         return true;
4073     }
4074 }
4075 \f
4076 /* Convert EXPR to TYPE, warning about conversion problems with
4077    constants.  SEMANTIC_TYPE is the type this conversion would use
4078    without excess precision. If SEMANTIC_TYPE is NULL, this function
4079    is equivalent to convert_and_check. This function is a wrapper that
4080    handles conversions that may be different than
4081    the usual ones because of excess precision.  */
4082
4083 static tree
4084 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4085 {
4086   if (TREE_TYPE (expr) == type)
4087     return expr;
4088
4089   if (!semantic_type)
4090     return convert_and_check (type, expr);
4091
4092   if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4093       && TREE_TYPE (expr) != semantic_type)
4094     {
4095       /* For integers, we need to check the real conversion, not
4096          the conversion to the excess precision type.  */
4097       expr = convert_and_check (semantic_type, expr);
4098     }
4099   /* Result type is the excess precision type, which should be
4100      large enough, so do not check.  */
4101   return convert (type, expr);
4102 }
4103
4104 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  If
4105    IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4106    if folded to an integer constant then the unselected half may
4107    contain arbitrary operations not normally permitted in constant
4108    expressions.  Set the location of the expression to LOC.  */
4109
4110 tree
4111 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4112                         tree op1, tree op1_original_type, tree op2,
4113                         tree op2_original_type)
4114 {
4115   tree type1;
4116   tree type2;
4117   enum tree_code code1;
4118   enum tree_code code2;
4119   tree result_type = NULL;
4120   tree semantic_result_type = NULL;
4121   tree orig_op1 = op1, orig_op2 = op2;
4122   bool int_const, op1_int_operands, op2_int_operands, int_operands;
4123   bool ifexp_int_operands;
4124   tree ret;
4125
4126   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4127   if (op1_int_operands)
4128     op1 = remove_c_maybe_const_expr (op1);
4129   op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4130   if (op2_int_operands)
4131     op2 = remove_c_maybe_const_expr (op2);
4132   ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4133   if (ifexp_int_operands)
4134     ifexp = remove_c_maybe_const_expr (ifexp);
4135
4136   /* Promote both alternatives.  */
4137
4138   if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4139     op1 = default_conversion (op1);
4140   if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4141     op2 = default_conversion (op2);
4142
4143   if (TREE_CODE (ifexp) == ERROR_MARK
4144       || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4145       || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4146     return error_mark_node;
4147
4148   type1 = TREE_TYPE (op1);
4149   code1 = TREE_CODE (type1);
4150   type2 = TREE_TYPE (op2);
4151   code2 = TREE_CODE (type2);
4152
4153   /* C90 does not permit non-lvalue arrays in conditional expressions.
4154      In C99 they will be pointers by now.  */
4155   if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4156     {
4157       error_at (colon_loc, "non-lvalue array in conditional expression");
4158       return error_mark_node;
4159     }
4160
4161   if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4162        || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4163       && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4164           || code1 == COMPLEX_TYPE)
4165       && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4166           || code2 == COMPLEX_TYPE))
4167     {
4168       semantic_result_type = c_common_type (type1, type2);
4169       if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4170         {
4171           op1 = TREE_OPERAND (op1, 0);
4172           type1 = TREE_TYPE (op1);
4173           gcc_assert (TREE_CODE (type1) == code1);
4174         }
4175       if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4176         {
4177           op2 = TREE_OPERAND (op2, 0);
4178           type2 = TREE_TYPE (op2);
4179           gcc_assert (TREE_CODE (type2) == code2);
4180         }
4181     }
4182
4183   if (warn_cxx_compat)
4184     {
4185       tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4186       tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4187
4188       if (TREE_CODE (t1) == ENUMERAL_TYPE
4189           && TREE_CODE (t2) == ENUMERAL_TYPE
4190           && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4191         warning_at (colon_loc, OPT_Wc___compat,
4192                     ("different enum types in conditional is "
4193                      "invalid in C++: %qT vs %qT"),
4194                     t1, t2);
4195     }
4196
4197   /* Quickly detect the usual case where op1 and op2 have the same type
4198      after promotion.  */
4199   if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4200     {
4201       if (type1 == type2)
4202         result_type = type1;
4203       else
4204         result_type = TYPE_MAIN_VARIANT (type1);
4205     }
4206   else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4207             || code1 == COMPLEX_TYPE)
4208            && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4209                || code2 == COMPLEX_TYPE))
4210     {
4211       result_type = c_common_type (type1, type2);
4212       do_warn_double_promotion (result_type, type1, type2,
4213                                 "implicit conversion from %qT to %qT to "
4214                                 "match other result of conditional",
4215                                 colon_loc);
4216
4217       /* If -Wsign-compare, warn here if type1 and type2 have
4218          different signedness.  We'll promote the signed to unsigned
4219          and later code won't know it used to be different.
4220          Do this check on the original types, so that explicit casts
4221          will be considered, but default promotions won't.  */
4222       if (c_inhibit_evaluation_warnings == 0)
4223         {
4224           int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4225           int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4226
4227           if (unsigned_op1 ^ unsigned_op2)
4228             {
4229               bool ovf;
4230
4231               /* Do not warn if the result type is signed, since the
4232                  signed type will only be chosen if it can represent
4233                  all the values of the unsigned type.  */
4234               if (!TYPE_UNSIGNED (result_type))
4235                 /* OK */;
4236               else
4237                 {
4238                   bool op1_maybe_const = true;
4239                   bool op2_maybe_const = true;
4240
4241                   /* Do not warn if the signed quantity is an
4242                      unsuffixed integer literal (or some static
4243                      constant expression involving such literals) and
4244                      it is non-negative.  This warning requires the
4245                      operands to be folded for best results, so do
4246                      that folding in this case even without
4247                      warn_sign_compare to avoid warning options
4248                      possibly affecting code generation.  */
4249                   c_inhibit_evaluation_warnings
4250                     += (ifexp == truthvalue_false_node);
4251                   op1 = c_fully_fold (op1, require_constant_value,
4252                                       &op1_maybe_const);
4253                   c_inhibit_evaluation_warnings
4254                     -= (ifexp == truthvalue_false_node);
4255
4256                   c_inhibit_evaluation_warnings
4257                     += (ifexp == truthvalue_true_node);
4258                   op2 = c_fully_fold (op2, require_constant_value,
4259                                       &op2_maybe_const);
4260                   c_inhibit_evaluation_warnings
4261                     -= (ifexp == truthvalue_true_node);
4262
4263                   if (warn_sign_compare)
4264                     {
4265                       if ((unsigned_op2
4266                            && tree_expr_nonnegative_warnv_p (op1, &ovf))
4267                           || (unsigned_op1
4268                               && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4269                         /* OK */;
4270                       else
4271                         warning_at (colon_loc, OPT_Wsign_compare,
4272                                     ("signed and unsigned type in "
4273                                      "conditional expression"));
4274                     }
4275                   if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4276                     op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4277                   if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4278                     op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4279                 }
4280             }
4281         }
4282     }
4283   else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4284     {
4285       if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4286         pedwarn (colon_loc, OPT_pedantic,
4287                  "ISO C forbids conditional expr with only one void side");
4288       result_type = void_type_node;
4289     }
4290   else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4291     {
4292       addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4293       addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4294       addr_space_t as_common;
4295
4296       if (comp_target_types (colon_loc, type1, type2))
4297         result_type = common_pointer_type (type1, type2);
4298       else if (null_pointer_constant_p (orig_op1))
4299         result_type = type2;
4300       else if (null_pointer_constant_p (orig_op2))
4301         result_type = type1;
4302       else if (!addr_space_superset (as1, as2, &as_common))
4303         {
4304           error_at (colon_loc, "pointers to disjoint address spaces "
4305                     "used in conditional expression");
4306           return error_mark_node;
4307         }
4308       else if (VOID_TYPE_P (TREE_TYPE (type1)))
4309         {
4310           if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4311             pedwarn (colon_loc, OPT_pedantic,
4312                      "ISO C forbids conditional expr between "
4313                      "%<void *%> and function pointer");
4314           result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4315                                                           TREE_TYPE (type2)));
4316         }
4317       else if (VOID_TYPE_P (TREE_TYPE (type2)))
4318         {
4319           if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4320             pedwarn (colon_loc, OPT_pedantic,
4321                      "ISO C forbids conditional expr between "
4322                      "%<void *%> and function pointer");
4323           result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4324                                                           TREE_TYPE (type1)));
4325         }
4326       /* Objective-C pointer comparisons are a bit more lenient.  */
4327       else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4328         result_type = objc_common_type (type1, type2);
4329       else
4330         {
4331           int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4332
4333           pedwarn (colon_loc, 0,
4334                    "pointer type mismatch in conditional expression");
4335           result_type = build_pointer_type
4336                           (build_qualified_type (void_type_node, qual));
4337         }
4338     }
4339   else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4340     {
4341       if (!null_pointer_constant_p (orig_op2))
4342         pedwarn (colon_loc, 0,
4343                  "pointer/integer type mismatch in conditional expression");
4344       else
4345         {
4346           op2 = null_pointer_node;
4347         }
4348       result_type = type1;
4349     }
4350   else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4351     {
4352       if (!null_pointer_constant_p (orig_op1))
4353         pedwarn (colon_loc, 0,
4354                  "pointer/integer type mismatch in conditional expression");
4355       else
4356         {
4357           op1 = null_pointer_node;
4358         }
4359       result_type = type2;
4360     }
4361
4362   if (!result_type)
4363     {
4364       if (flag_cond_mismatch)
4365         result_type = void_type_node;
4366       else
4367         {
4368           error_at (colon_loc, "type mismatch in conditional expression");
4369           return error_mark_node;
4370         }
4371     }
4372
4373   /* Merge const and volatile flags of the incoming types.  */
4374   result_type
4375     = build_type_variant (result_type,
4376                           TYPE_READONLY (type1) || TYPE_READONLY (type2),
4377                           TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4378
4379   op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4380   op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4381
4382   if (ifexp_bcp && ifexp == truthvalue_true_node)
4383     {
4384       op2_int_operands = true;
4385       op1 = c_fully_fold (op1, require_constant_value, NULL);
4386     }
4387   if (ifexp_bcp && ifexp == truthvalue_false_node)
4388     {
4389       op1_int_operands = true;
4390       op2 = c_fully_fold (op2, require_constant_value, NULL);
4391     }
4392   int_const = int_operands = (ifexp_int_operands
4393                               && op1_int_operands
4394                               && op2_int_operands);
4395   if (int_operands)
4396     {
4397       int_const = ((ifexp == truthvalue_true_node
4398                     && TREE_CODE (orig_op1) == INTEGER_CST
4399                     && !TREE_OVERFLOW (orig_op1))
4400                    || (ifexp == truthvalue_false_node
4401                        && TREE_CODE (orig_op2) == INTEGER_CST
4402                        && !TREE_OVERFLOW (orig_op2)));
4403     }
4404   if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4405     ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4406   else
4407     {
4408       ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4409       if (int_operands)
4410         ret = note_integer_operands (ret);
4411     }
4412   if (semantic_result_type)
4413     ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4414
4415   protected_set_expr_location (ret, colon_loc);
4416   return ret;
4417 }
4418 \f
4419 /* Return a compound expression that performs two expressions and
4420    returns the value of the second of them.
4421
4422    LOC is the location of the COMPOUND_EXPR.  */
4423
4424 tree
4425 build_compound_expr (location_t loc, tree expr1, tree expr2)
4426 {
4427   bool expr1_int_operands, expr2_int_operands;
4428   tree eptype = NULL_TREE;
4429   tree ret;
4430
4431   expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4432   if (expr1_int_operands)
4433     expr1 = remove_c_maybe_const_expr (expr1);
4434   expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4435   if (expr2_int_operands)
4436     expr2 = remove_c_maybe_const_expr (expr2);
4437
4438   if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4439     expr1 = TREE_OPERAND (expr1, 0);
4440   if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4441     {
4442       eptype = TREE_TYPE (expr2);
4443       expr2 = TREE_OPERAND (expr2, 0);
4444     }
4445
4446   if (!TREE_SIDE_EFFECTS (expr1))
4447     {
4448       /* The left-hand operand of a comma expression is like an expression
4449          statement: with -Wunused, we should warn if it doesn't have
4450          any side-effects, unless it was explicitly cast to (void).  */
4451       if (warn_unused_value)
4452         {
4453           if (VOID_TYPE_P (TREE_TYPE (expr1))
4454               && CONVERT_EXPR_P (expr1))
4455             ; /* (void) a, b */
4456           else if (VOID_TYPE_P (TREE_TYPE (expr1))
4457                    && TREE_CODE (expr1) == COMPOUND_EXPR
4458                    && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4459             ; /* (void) a, (void) b, c */
4460           else
4461             warning_at (loc, OPT_Wunused_value,
4462                         "left-hand operand of comma expression has no effect");
4463         }
4464     }
4465
4466   /* With -Wunused, we should also warn if the left-hand operand does have
4467      side-effects, but computes a value which is not used.  For example, in
4468      `foo() + bar(), baz()' the result of the `+' operator is not used,
4469      so we should issue a warning.  */
4470   else if (warn_unused_value)
4471     warn_if_unused_value (expr1, loc);
4472
4473   if (expr2 == error_mark_node)
4474     return error_mark_node;
4475
4476   ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4477
4478   if (flag_isoc99
4479       && expr1_int_operands
4480       && expr2_int_operands)
4481     ret = note_integer_operands (ret);
4482
4483   if (eptype)
4484     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4485
4486   protected_set_expr_location (ret, loc);
4487   return ret;
4488 }
4489
4490 /* Issue -Wcast-qual warnings when appropriate.  TYPE is the type to
4491    which we are casting.  OTYPE is the type of the expression being
4492    cast.  Both TYPE and OTYPE are pointer types.  LOC is the location
4493    of the cast.  -Wcast-qual appeared on the command line.  Named
4494    address space qualifiers are not handled here, because they result
4495    in different warnings.  */
4496
4497 static void
4498 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4499 {
4500   tree in_type = type;
4501   tree in_otype = otype;
4502   int added = 0;
4503   int discarded = 0;
4504   bool is_const;
4505
4506   /* Check that the qualifiers on IN_TYPE are a superset of the
4507      qualifiers of IN_OTYPE.  The outermost level of POINTER_TYPE
4508      nodes is uninteresting and we stop as soon as we hit a
4509      non-POINTER_TYPE node on either type.  */
4510   do
4511     {
4512       in_otype = TREE_TYPE (in_otype);
4513       in_type = TREE_TYPE (in_type);
4514
4515       /* GNU C allows cv-qualified function types.  'const' means the
4516          function is very pure, 'volatile' means it can't return.  We
4517          need to warn when such qualifiers are added, not when they're
4518          taken away.  */
4519       if (TREE_CODE (in_otype) == FUNCTION_TYPE
4520           && TREE_CODE (in_type) == FUNCTION_TYPE)
4521         added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4522                   & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4523       else
4524         discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4525                       & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4526     }
4527   while (TREE_CODE (in_type) == POINTER_TYPE
4528          && TREE_CODE (in_otype) == POINTER_TYPE);
4529
4530   if (added)
4531     warning_at (loc, OPT_Wcast_qual,
4532                 "cast adds %q#v qualifier to function type", added);
4533
4534   if (discarded)
4535     /* There are qualifiers present in IN_OTYPE that are not present
4536        in IN_TYPE.  */
4537     warning_at (loc, OPT_Wcast_qual,
4538                 "cast discards %q#v qualifier from pointer target type",
4539                 discarded);
4540
4541   if (added || discarded)
4542     return;
4543
4544   /* A cast from **T to const **T is unsafe, because it can cause a
4545      const value to be changed with no additional warning.  We only
4546      issue this warning if T is the same on both sides, and we only
4547      issue the warning if there are the same number of pointers on
4548      both sides, as otherwise the cast is clearly unsafe anyhow.  A
4549      cast is unsafe when a qualifier is added at one level and const
4550      is not present at all outer levels.
4551
4552      To issue this warning, we check at each level whether the cast
4553      adds new qualifiers not already seen.  We don't need to special
4554      case function types, as they won't have the same
4555      TYPE_MAIN_VARIANT.  */
4556
4557   if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4558     return;
4559   if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4560     return;
4561
4562   in_type = type;
4563   in_otype = otype;
4564   is_const = TYPE_READONLY (TREE_TYPE (in_type));
4565   do
4566     {
4567       in_type = TREE_TYPE (in_type);
4568       in_otype = TREE_TYPE (in_otype);
4569       if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4570           && !is_const)
4571         {
4572           warning_at (loc, OPT_Wcast_qual,
4573                       "to be safe all intermediate pointers in cast from "
4574                       "%qT to %qT must be %<const%> qualified",
4575                       otype, type);
4576           break;
4577         }
4578       if (is_const)
4579         is_const = TYPE_READONLY (in_type);
4580     }
4581   while (TREE_CODE (in_type) == POINTER_TYPE);
4582 }
4583
4584 /* Build an expression representing a cast to type TYPE of expression EXPR.
4585    LOC is the location of the cast-- typically the open paren of the cast.  */
4586
4587 tree
4588 build_c_cast (location_t loc, tree type, tree expr)
4589 {
4590   tree value;
4591
4592   if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4593     expr = TREE_OPERAND (expr, 0);
4594
4595   value = expr;
4596
4597   if (type == error_mark_node || expr == error_mark_node)
4598     return error_mark_node;
4599
4600   /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4601      only in <protocol> qualifications.  But when constructing cast expressions,
4602      the protocols do matter and must be kept around.  */
4603   if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4604     return build1 (NOP_EXPR, type, expr);
4605
4606   type = TYPE_MAIN_VARIANT (type);
4607
4608   if (TREE_CODE (type) == ARRAY_TYPE)
4609     {
4610       error_at (loc, "cast specifies array type");
4611       return error_mark_node;
4612     }
4613
4614   if (TREE_CODE (type) == FUNCTION_TYPE)
4615     {
4616       error_at (loc, "cast specifies function type");
4617       return error_mark_node;
4618     }
4619
4620   if (!VOID_TYPE_P (type))
4621     {
4622       value = require_complete_type (value);
4623       if (value == error_mark_node)
4624         return error_mark_node;
4625     }
4626
4627   if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4628     {
4629       if (TREE_CODE (type) == RECORD_TYPE
4630           || TREE_CODE (type) == UNION_TYPE)
4631         pedwarn (loc, OPT_pedantic,
4632                  "ISO C forbids casting nonscalar to the same type");
4633     }
4634   else if (TREE_CODE (type) == UNION_TYPE)
4635     {
4636       tree field;
4637
4638       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4639         if (TREE_TYPE (field) != error_mark_node
4640             && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4641                           TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4642           break;
4643
4644       if (field)
4645         {
4646           tree t;
4647           bool maybe_const = true;
4648
4649           pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4650           t = c_fully_fold (value, false, &maybe_const);
4651           t = build_constructor_single (type, field, t);
4652           if (!maybe_const)
4653             t = c_wrap_maybe_const (t, true);
4654           t = digest_init (loc, type, t,
4655                            NULL_TREE, false, true, 0);
4656           TREE_CONSTANT (t) = TREE_CONSTANT (value);
4657           return t;
4658         }
4659       error_at (loc, "cast to union type from type not present in union");
4660       return error_mark_node;
4661     }
4662   else
4663     {
4664       tree otype, ovalue;
4665
4666       if (type == void_type_node)
4667         {
4668           tree t = build1 (CONVERT_EXPR, type, value);
4669           SET_EXPR_LOCATION (t, loc);
4670           return t;
4671         }
4672
4673       otype = TREE_TYPE (value);
4674
4675       /* Optionally warn about potentially worrisome casts.  */
4676       if (warn_cast_qual
4677           && TREE_CODE (type) == POINTER_TYPE
4678           && TREE_CODE (otype) == POINTER_TYPE)
4679         handle_warn_cast_qual (loc, type, otype);
4680
4681       /* Warn about conversions between pointers to disjoint
4682          address spaces.  */
4683       if (TREE_CODE (type) == POINTER_TYPE
4684           && TREE_CODE (otype) == POINTER_TYPE
4685           && !null_pointer_constant_p (value))
4686         {
4687           addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4688           addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4689           addr_space_t as_common;
4690
4691           if (!addr_space_superset (as_to, as_from, &as_common))
4692             {
4693               if (ADDR_SPACE_GENERIC_P (as_from))
4694                 warning_at (loc, 0, "cast to %s address space pointer "
4695                             "from disjoint generic address space pointer",
4696                             c_addr_space_name (as_to));
4697
4698               else if (ADDR_SPACE_GENERIC_P (as_to))
4699                 warning_at (loc, 0, "cast to generic address space pointer "
4700                             "from disjoint %s address space pointer",
4701                             c_addr_space_name (as_from));
4702
4703               else
4704                 warning_at (loc, 0, "cast to %s address space pointer "
4705                             "from disjoint %s address space pointer",
4706                             c_addr_space_name (as_to),
4707                             c_addr_space_name (as_from));
4708             }
4709         }
4710
4711       /* Warn about possible alignment problems.  */
4712       if (STRICT_ALIGNMENT
4713           && TREE_CODE (type) == POINTER_TYPE
4714           && TREE_CODE (otype) == POINTER_TYPE
4715           && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4716           && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4717           /* Don't warn about opaque types, where the actual alignment
4718              restriction is unknown.  */
4719           && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4720                 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4721                && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4722           && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4723         warning_at (loc, OPT_Wcast_align,
4724                     "cast increases required alignment of target type");
4725
4726       if (TREE_CODE (type) == INTEGER_TYPE
4727           && TREE_CODE (otype) == POINTER_TYPE
4728           && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4729       /* Unlike conversion of integers to pointers, where the
4730          warning is disabled for converting constants because
4731          of cases such as SIG_*, warn about converting constant
4732          pointers to integers. In some cases it may cause unwanted
4733          sign extension, and a warning is appropriate.  */
4734         warning_at (loc, OPT_Wpointer_to_int_cast,
4735                     "cast from pointer to integer of different size");
4736
4737       if (TREE_CODE (value) == CALL_EXPR
4738           && TREE_CODE (type) != TREE_CODE (otype))
4739         warning_at (loc, OPT_Wbad_function_cast,
4740                     "cast from function call of type %qT "
4741                     "to non-matching type %qT", otype, type);
4742
4743       if (TREE_CODE (type) == POINTER_TYPE
4744           && TREE_CODE (otype) == INTEGER_TYPE
4745           && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4746           /* Don't warn about converting any constant.  */
4747           && !TREE_CONSTANT (value))
4748         warning_at (loc,
4749                     OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4750                     "of different size");
4751
4752       if (warn_strict_aliasing <= 2)
4753         strict_aliasing_warning (otype, type, expr);
4754
4755       /* If pedantic, warn for conversions between function and object
4756          pointer types, except for converting a null pointer constant
4757          to function pointer type.  */
4758       if (pedantic
4759           && TREE_CODE (type) == POINTER_TYPE
4760           && TREE_CODE (otype) == POINTER_TYPE
4761           && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4762           && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4763         pedwarn (loc, OPT_pedantic, "ISO C forbids "
4764                  "conversion of function pointer to object pointer type");
4765
4766       if (pedantic
4767           && TREE_CODE (type) == POINTER_TYPE
4768           && TREE_CODE (otype) == POINTER_TYPE
4769           && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4770           && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4771           && !null_pointer_constant_p (value))
4772         pedwarn (loc, OPT_pedantic, "ISO C forbids "
4773                  "conversion of object pointer to function pointer type");
4774
4775       ovalue = value;
4776       value = convert (type, value);
4777
4778       /* Ignore any integer overflow caused by the cast.  */
4779       if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4780         {
4781           if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4782             {
4783               if (!TREE_OVERFLOW (value))
4784                 {
4785                   /* Avoid clobbering a shared constant.  */
4786                   value = copy_node (value);
4787                   TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4788                 }
4789             }
4790           else if (TREE_OVERFLOW (value))
4791             /* Reset VALUE's overflow flags, ensuring constant sharing.  */
4792             value = build_int_cst_wide (TREE_TYPE (value),
4793                                         TREE_INT_CST_LOW (value),
4794                                         TREE_INT_CST_HIGH (value));
4795         }
4796     }
4797
4798   /* Don't let a cast be an lvalue.  */
4799   if (value == expr)
4800     value = non_lvalue_loc (loc, value);
4801
4802   /* Don't allow the results of casting to floating-point or complex
4803      types be confused with actual constants, or casts involving
4804      integer and pointer types other than direct integer-to-integer
4805      and integer-to-pointer be confused with integer constant
4806      expressions and null pointer constants.  */
4807   if (TREE_CODE (value) == REAL_CST
4808       || TREE_CODE (value) == COMPLEX_CST
4809       || (TREE_CODE (value) == INTEGER_CST
4810           && !((TREE_CODE (expr) == INTEGER_CST
4811                 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4812                || TREE_CODE (expr) == REAL_CST
4813                || TREE_CODE (expr) == COMPLEX_CST)))
4814       value = build1 (NOP_EXPR, type, value);
4815
4816   if (CAN_HAVE_LOCATION_P (value))
4817     SET_EXPR_LOCATION (value, loc);
4818   return value;
4819 }
4820
4821 /* Interpret a cast of expression EXPR to type TYPE.  LOC is the
4822    location of the open paren of the cast, or the position of the cast
4823    expr.  */
4824 tree
4825 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4826 {
4827   tree type;
4828   tree type_expr = NULL_TREE;
4829   bool type_expr_const = true;
4830   tree ret;
4831   int saved_wsp = warn_strict_prototypes;
4832
4833   /* This avoids warnings about unprototyped casts on
4834      integers.  E.g. "#define SIG_DFL (void(*)())0".  */
4835   if (TREE_CODE (expr) == INTEGER_CST)
4836     warn_strict_prototypes = 0;
4837   type = groktypename (type_name, &type_expr, &type_expr_const);
4838   warn_strict_prototypes = saved_wsp;
4839
4840   ret = build_c_cast (loc, type, expr);
4841   if (type_expr)
4842     {
4843       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4844       C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4845       SET_EXPR_LOCATION (ret, loc);
4846     }
4847
4848   if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4849     SET_EXPR_LOCATION (ret, loc);
4850
4851   /* C++ does not permits types to be defined in a cast, but it
4852      allows references to incomplete types.  */
4853   if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4854     warning_at (loc, OPT_Wc___compat,
4855                 "defining a type in a cast is invalid in C++");
4856
4857   return ret;
4858 }
4859 \f
4860 /* Build an assignment expression of lvalue LHS from value RHS.
4861    If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4862    may differ from TREE_TYPE (LHS) for an enum bitfield.
4863    MODIFYCODE is the code for a binary operator that we use
4864    to combine the old value of LHS with RHS to get the new value.
4865    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4866    If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4867    which may differ from TREE_TYPE (RHS) for an enum value.
4868
4869    LOCATION is the location of the MODIFYCODE operator.
4870    RHS_LOC is the location of the RHS.  */
4871
4872 tree
4873 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4874                    enum tree_code modifycode,
4875                    location_t rhs_loc, tree rhs, tree rhs_origtype)
4876 {
4877   tree result;
4878   tree newrhs;
4879   tree rhs_semantic_type = NULL_TREE;
4880   tree lhstype = TREE_TYPE (lhs);
4881   tree olhstype = lhstype;
4882   bool npc;
4883
4884   /* Types that aren't fully specified cannot be used in assignments.  */
4885   lhs = require_complete_type (lhs);
4886
4887   /* Avoid duplicate error messages from operands that had errors.  */
4888   if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4889     return error_mark_node;
4890
4891   /* For ObjC properties, defer this check.  */
4892   if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4893     return error_mark_node;
4894
4895   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4896     {
4897       rhs_semantic_type = TREE_TYPE (rhs);
4898       rhs = TREE_OPERAND (rhs, 0);
4899     }
4900
4901   newrhs = rhs;
4902
4903   if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4904     {
4905       tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4906                                       lhs_origtype, modifycode, rhs_loc, rhs,
4907                                       rhs_origtype);
4908       if (inner == error_mark_node)
4909         return error_mark_node;
4910       result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4911                        C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4912       gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4913       C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4914       protected_set_expr_location (result, location);
4915       return result;
4916     }
4917
4918   /* If a binary op has been requested, combine the old LHS value with the RHS
4919      producing the value we should actually store into the LHS.  */
4920
4921   if (modifycode != NOP_EXPR)
4922     {
4923       lhs = c_fully_fold (lhs, false, NULL);
4924       lhs = stabilize_reference (lhs);
4925       newrhs = build_binary_op (location,
4926                                 modifycode, lhs, rhs, 1);
4927
4928       /* The original type of the right hand side is no longer
4929          meaningful.  */
4930       rhs_origtype = NULL_TREE;
4931     }
4932
4933   if (c_dialect_objc ())
4934     {
4935       /* Check if we are modifying an Objective-C property reference;
4936          if so, we need to generate setter calls.  */
4937       result = objc_maybe_build_modify_expr (lhs, newrhs);
4938       if (result)
4939         return result;
4940
4941       /* Else, do the check that we postponed for Objective-C.  */
4942       if (!lvalue_or_else (location, lhs, lv_assign))
4943         return error_mark_node;
4944     }
4945
4946   /* Give an error for storing in something that is 'const'.  */
4947
4948   if (TYPE_READONLY (lhstype)
4949       || ((TREE_CODE (lhstype) == RECORD_TYPE
4950            || TREE_CODE (lhstype) == UNION_TYPE)
4951           && C_TYPE_FIELDS_READONLY (lhstype)))
4952     {
4953       readonly_error (lhs, lv_assign);
4954       return error_mark_node;
4955     }
4956   else if (TREE_READONLY (lhs))
4957     readonly_warning (lhs, lv_assign);
4958
4959   /* If storing into a structure or union member,
4960      it has probably been given type `int'.
4961      Compute the type that would go with
4962      the actual amount of storage the member occupies.  */
4963
4964   if (TREE_CODE (lhs) == COMPONENT_REF
4965       && (TREE_CODE (lhstype) == INTEGER_TYPE
4966           || TREE_CODE (lhstype) == BOOLEAN_TYPE
4967           || TREE_CODE (lhstype) == REAL_TYPE
4968           || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4969     lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4970
4971   /* If storing in a field that is in actuality a short or narrower than one,
4972      we must store in the field in its actual type.  */
4973
4974   if (lhstype != TREE_TYPE (lhs))
4975     {
4976       lhs = copy_node (lhs);
4977       TREE_TYPE (lhs) = lhstype;
4978     }
4979
4980   /* Issue -Wc++-compat warnings about an assignment to an enum type
4981      when LHS does not have its original type.  This happens for,
4982      e.g., an enum bitfield in a struct.  */
4983   if (warn_cxx_compat
4984       && lhs_origtype != NULL_TREE
4985       && lhs_origtype != lhstype
4986       && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4987     {
4988       tree checktype = (rhs_origtype != NULL_TREE
4989                         ? rhs_origtype
4990                         : TREE_TYPE (rhs));
4991       if (checktype != error_mark_node
4992           && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4993         warning_at (location, OPT_Wc___compat,
4994                     "enum conversion in assignment is invalid in C++");
4995     }
4996
4997   /* Convert new value to destination type.  Fold it first, then
4998      restore any excess precision information, for the sake of
4999      conversion warnings.  */
5000
5001   npc = null_pointer_constant_p (newrhs);
5002   newrhs = c_fully_fold (newrhs, false, NULL);
5003   if (rhs_semantic_type)
5004     newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5005   newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
5006                                    ic_assign, npc, NULL_TREE, NULL_TREE, 0);
5007   if (TREE_CODE (newrhs) == ERROR_MARK)
5008     return error_mark_node;
5009
5010   /* Emit ObjC write barrier, if necessary.  */
5011   if (c_dialect_objc () && flag_objc_gc)
5012     {
5013       result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5014       if (result)
5015         {
5016           protected_set_expr_location (result, location);
5017           return result;
5018         }
5019     }
5020
5021   /* Scan operands.  */
5022
5023   result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5024   TREE_SIDE_EFFECTS (result) = 1;
5025   protected_set_expr_location (result, location);
5026
5027   /* If we got the LHS in a different type for storing in,
5028      convert the result back to the nominal type of LHS
5029      so that the value we return always has the same type
5030      as the LHS argument.  */
5031
5032   if (olhstype == TREE_TYPE (result))
5033     return result;
5034
5035   result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5036                                    ic_assign, false, NULL_TREE, NULL_TREE, 0);
5037   protected_set_expr_location (result, location);
5038   return result;
5039 }
5040 \f
5041 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5042    This is used to implement -fplan9-extensions.  */
5043
5044 static bool
5045 find_anonymous_field_with_type (tree struct_type, tree type)
5046 {
5047   tree field;
5048   bool found;
5049
5050   gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5051               || TREE_CODE (struct_type) == UNION_TYPE);
5052   found = false;
5053   for (field = TYPE_FIELDS (struct_type);
5054        field != NULL_TREE;
5055        field = TREE_CHAIN (field))
5056     {
5057       if (DECL_NAME (field) == NULL
5058           && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5059         {
5060           if (found)
5061             return false;
5062           found = true;
5063         }
5064       else if (DECL_NAME (field) == NULL
5065                && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5066                    || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5067                && find_anonymous_field_with_type (TREE_TYPE (field), type))
5068         {
5069           if (found)
5070             return false;
5071           found = true;
5072         }
5073     }
5074   return found;
5075 }
5076
5077 /* RHS is an expression whose type is pointer to struct.  If there is
5078    an anonymous field in RHS with type TYPE, then return a pointer to
5079    that field in RHS.  This is used with -fplan9-extensions.  This
5080    returns NULL if no conversion could be found.  */
5081
5082 static tree
5083 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5084 {
5085   tree rhs_struct_type, lhs_main_type;
5086   tree field, found_field;
5087   bool found_sub_field;
5088   tree ret;
5089
5090   gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5091   rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5092   gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5093               || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5094
5095   gcc_assert (POINTER_TYPE_P (type));
5096   lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5097
5098   found_field = NULL_TREE;
5099   found_sub_field = false;
5100   for (field = TYPE_FIELDS (rhs_struct_type);
5101        field != NULL_TREE;
5102        field = TREE_CHAIN (field))
5103     {
5104       if (DECL_NAME (field) != NULL_TREE
5105           || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5106               && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5107         continue;
5108       if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5109         {
5110           if (found_field != NULL_TREE)
5111             return NULL_TREE;
5112           found_field = field;
5113         }
5114       else if (find_anonymous_field_with_type (TREE_TYPE (field),
5115                                                lhs_main_type))
5116         {
5117           if (found_field != NULL_TREE)
5118             return NULL_TREE;
5119           found_field = field;
5120           found_sub_field = true;
5121         }
5122     }
5123
5124   if (found_field == NULL_TREE)
5125     return NULL_TREE;
5126
5127   ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5128                          build_fold_indirect_ref (rhs), found_field,
5129                          NULL_TREE);
5130   ret = build_fold_addr_expr_loc (location, ret);
5131
5132   if (found_sub_field)
5133     {
5134       ret = convert_to_anonymous_field (location, type, ret);
5135       gcc_assert (ret != NULL_TREE);
5136     }
5137
5138   return ret;
5139 }
5140
5141 /* Convert value RHS to type TYPE as preparation for an assignment to
5142    an lvalue of type TYPE.  If ORIGTYPE is not NULL_TREE, it is the
5143    original type of RHS; this differs from TREE_TYPE (RHS) for enum
5144    types.  NULL_POINTER_CONSTANT says whether RHS was a null pointer
5145    constant before any folding.
5146    The real work of conversion is done by `convert'.
5147    The purpose of this function is to generate error messages
5148    for assignments that are not allowed in C.
5149    ERRTYPE says whether it is argument passing, assignment,
5150    initialization or return.
5151
5152    LOCATION is the location of the RHS.
5153    FUNCTION is a tree for the function being called.
5154    PARMNUM is the number of the argument, for printing in error messages.  */
5155
5156 static tree
5157 convert_for_assignment (location_t location, tree type, tree rhs,
5158                         tree origtype, enum impl_conv errtype,
5159                         bool null_pointer_constant, tree fundecl,
5160                         tree function, int parmnum)
5161 {
5162   enum tree_code codel = TREE_CODE (type);
5163   tree orig_rhs = rhs;
5164   tree rhstype;
5165   enum tree_code coder;
5166   tree rname = NULL_TREE;
5167   bool objc_ok = false;
5168
5169   if (errtype == ic_argpass)
5170     {
5171       tree selector;
5172       /* Change pointer to function to the function itself for
5173          diagnostics.  */
5174       if (TREE_CODE (function) == ADDR_EXPR
5175           && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5176         function = TREE_OPERAND (function, 0);
5177
5178       /* Handle an ObjC selector specially for diagnostics.  */
5179       selector = objc_message_selector ();
5180       rname = function;
5181       if (selector && parmnum > 2)
5182         {
5183           rname = selector;
5184           parmnum -= 2;
5185         }
5186     }
5187
5188   /* This macro is used to emit diagnostics to ensure that all format
5189      strings are complete sentences, visible to gettext and checked at
5190      compile time.  */
5191 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE)               \
5192   do {                                                                   \
5193     switch (errtype)                                                     \
5194       {                                                                  \
5195       case ic_argpass:                                                   \
5196         if (pedwarn (LOCATION, OPT, AR, parmnum, rname))                 \
5197           inform ((fundecl && !DECL_IS_BUILTIN (fundecl))                \
5198                   ? DECL_SOURCE_LOCATION (fundecl) : LOCATION,           \
5199                   "expected %qT but argument is of type %qT",            \
5200                   type, rhstype);                                        \
5201         break;                                                           \
5202       case ic_assign:                                                    \
5203         pedwarn (LOCATION, OPT, AS);                                     \
5204         break;                                                           \
5205       case ic_init:                                                      \
5206         pedwarn_init (LOCATION, OPT, IN);                                \
5207         break;                                                           \
5208       case ic_return:                                                    \
5209         pedwarn (LOCATION, OPT, RE);                                     \
5210         break;                                                           \
5211       default:                                                           \
5212         gcc_unreachable ();                                              \
5213       }                                                                  \
5214   } while (0)
5215
5216   /* This macro is used to emit diagnostics to ensure that all format
5217      strings are complete sentences, visible to gettext and checked at
5218      compile time.  It is the same as WARN_FOR_ASSIGNMENT but with an
5219      extra parameter to enumerate qualifiers.  */
5220
5221 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS)        \
5222   do {                                                                   \
5223     switch (errtype)                                                     \
5224       {                                                                  \
5225       case ic_argpass:                                                   \
5226         if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS))          \
5227           inform ((fundecl && !DECL_IS_BUILTIN (fundecl))                \
5228                   ? DECL_SOURCE_LOCATION (fundecl) : LOCATION,           \
5229                   "expected %qT but argument is of type %qT",            \
5230                   type, rhstype);                                        \
5231         break;                                                           \
5232       case ic_assign:                                                    \
5233         pedwarn (LOCATION, OPT, AS, QUALS);                          \
5234         break;                                                           \
5235       case ic_init:                                                      \
5236         pedwarn (LOCATION, OPT, IN, QUALS);                          \
5237         break;                                                           \
5238       case ic_return:                                                    \
5239         pedwarn (LOCATION, OPT, RE, QUALS);                              \
5240         break;                                                           \
5241       default:                                                           \
5242         gcc_unreachable ();                                              \
5243       }                                                                  \
5244   } while (0)
5245
5246   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5247     rhs = TREE_OPERAND (rhs, 0);
5248
5249   rhstype = TREE_TYPE (rhs);
5250   coder = TREE_CODE (rhstype);
5251
5252   if (coder == ERROR_MARK)
5253     return error_mark_node;
5254
5255   if (c_dialect_objc ())
5256     {
5257       int parmno;
5258
5259       switch (errtype)
5260         {
5261         case ic_return:
5262           parmno = 0;
5263           break;
5264
5265         case ic_assign:
5266           parmno = -1;
5267           break;
5268
5269         case ic_init:
5270           parmno = -2;
5271           break;
5272
5273         default:
5274           parmno = parmnum;
5275           break;
5276         }
5277
5278       objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5279     }
5280
5281   if (warn_cxx_compat)
5282     {
5283       tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5284       if (checktype != error_mark_node
5285           && TREE_CODE (type) == ENUMERAL_TYPE
5286           && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5287         {
5288           WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5289                                G_("enum conversion when passing argument "
5290                                   "%d of %qE is invalid in C++"),
5291                                G_("enum conversion in assignment is "
5292                                   "invalid in C++"),
5293                                G_("enum conversion in initialization is "
5294                                   "invalid in C++"),
5295                                G_("enum conversion in return is "
5296                                   "invalid in C++"));
5297         }
5298     }
5299
5300   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5301     return rhs;
5302
5303   if (coder == VOID_TYPE)
5304     {
5305       /* Except for passing an argument to an unprototyped function,
5306          this is a constraint violation.  When passing an argument to
5307          an unprototyped function, it is compile-time undefined;
5308          making it a constraint in that case was rejected in
5309          DR#252.  */
5310       error_at (location, "void value not ignored as it ought to be");
5311       return error_mark_node;
5312     }
5313   rhs = require_complete_type (rhs);
5314   if (rhs == error_mark_node)
5315     return error_mark_node;
5316   /* A type converts to a reference to it.
5317      This code doesn't fully support references, it's just for the
5318      special case of va_start and va_copy.  */
5319   if (codel == REFERENCE_TYPE
5320       && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5321     {
5322       if (!lvalue_p (rhs))
5323         {
5324           error_at (location, "cannot pass rvalue to reference parameter");
5325           return error_mark_node;
5326         }
5327       if (!c_mark_addressable (rhs))
5328         return error_mark_node;
5329       rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5330       SET_EXPR_LOCATION (rhs, location);
5331
5332       /* We already know that these two types are compatible, but they
5333          may not be exactly identical.  In fact, `TREE_TYPE (type)' is
5334          likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5335          likely to be va_list, a typedef to __builtin_va_list, which
5336          is different enough that it will cause problems later.  */
5337       if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5338         {
5339           rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5340           SET_EXPR_LOCATION (rhs, location);
5341         }
5342
5343       rhs = build1 (NOP_EXPR, type, rhs);
5344       SET_EXPR_LOCATION (rhs, location);
5345       return rhs;
5346     }
5347   /* Some types can interconvert without explicit casts.  */
5348   else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5349            && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5350     return convert (type, rhs);
5351   /* Arithmetic types all interconvert, and enum is treated like int.  */
5352   else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5353             || codel == FIXED_POINT_TYPE
5354             || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5355             || codel == BOOLEAN_TYPE)
5356            && (coder == INTEGER_TYPE || coder == REAL_TYPE
5357                || coder == FIXED_POINT_TYPE
5358                || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5359                || coder == BOOLEAN_TYPE))
5360     {
5361       tree ret;
5362       bool save = in_late_binary_op;
5363       if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5364         in_late_binary_op = true;
5365       ret = convert_and_check (type, orig_rhs);
5366       if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5367         in_late_binary_op = save;
5368       return ret;
5369     }
5370
5371   /* Aggregates in different TUs might need conversion.  */
5372   if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5373       && codel == coder
5374       && comptypes (type, rhstype))
5375     return convert_and_check (type, rhs);
5376
5377   /* Conversion to a transparent union or record from its member types.
5378      This applies only to function arguments.  */
5379   if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5380       && TYPE_TRANSPARENT_AGGR (type))
5381       && errtype == ic_argpass)
5382     {
5383       tree memb, marginal_memb = NULL_TREE;
5384
5385       for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5386         {
5387           tree memb_type = TREE_TYPE (memb);
5388
5389           if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5390                          TYPE_MAIN_VARIANT (rhstype)))
5391             break;
5392
5393           if (TREE_CODE (memb_type) != POINTER_TYPE)
5394             continue;
5395
5396           if (coder == POINTER_TYPE)
5397             {
5398               tree ttl = TREE_TYPE (memb_type);
5399               tree ttr = TREE_TYPE (rhstype);
5400
5401               /* Any non-function converts to a [const][volatile] void *
5402                  and vice versa; otherwise, targets must be the same.
5403                  Meanwhile, the lhs target must have all the qualifiers of
5404                  the rhs.  */
5405               if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5406                   || comp_target_types (location, memb_type, rhstype))
5407                 {
5408                   /* If this type won't generate any warnings, use it.  */
5409                   if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5410                       || ((TREE_CODE (ttr) == FUNCTION_TYPE
5411                            && TREE_CODE (ttl) == FUNCTION_TYPE)
5412                           ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5413                              == TYPE_QUALS (ttr))
5414                           : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5415                              == TYPE_QUALS (ttl))))
5416                     break;
5417
5418                   /* Keep looking for a better type, but remember this one.  */
5419                   if (!marginal_memb)
5420                     marginal_memb = memb;
5421                 }
5422             }
5423
5424           /* Can convert integer zero to any pointer type.  */
5425           if (null_pointer_constant)
5426             {
5427               rhs = null_pointer_node;
5428               break;
5429             }
5430         }
5431
5432       if (memb || marginal_memb)
5433         {
5434           if (!memb)
5435             {
5436               /* We have only a marginally acceptable member type;
5437                  it needs a warning.  */
5438               tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5439               tree ttr = TREE_TYPE (rhstype);
5440
5441               /* Const and volatile mean something different for function
5442                  types, so the usual warnings are not appropriate.  */
5443               if (TREE_CODE (ttr) == FUNCTION_TYPE
5444                   && TREE_CODE (ttl) == FUNCTION_TYPE)
5445                 {
5446                   /* Because const and volatile on functions are
5447                      restrictions that say the function will not do
5448                      certain things, it is okay to use a const or volatile
5449                      function where an ordinary one is wanted, but not
5450                      vice-versa.  */
5451                   if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5452                       & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5453                     WARN_FOR_QUALIFIERS (location, 0,
5454                                          G_("passing argument %d of %qE "
5455                                             "makes %q#v qualified function "
5456                                             "pointer from unqualified"),
5457                                          G_("assignment makes %q#v qualified "
5458                                             "function pointer from "
5459                                             "unqualified"),
5460                                          G_("initialization makes %q#v qualified "
5461                                             "function pointer from "
5462                                             "unqualified"),
5463                                          G_("return makes %q#v qualified function "
5464                                             "pointer from unqualified"),
5465                                          TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5466                 }
5467               else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5468                        & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5469                 WARN_FOR_QUALIFIERS (location, 0,
5470                                      G_("passing argument %d of %qE discards "
5471                                         "%qv qualifier from pointer target type"),
5472                                      G_("assignment discards %qv qualifier "
5473                                         "from pointer target type"),
5474                                      G_("initialization discards %qv qualifier "
5475                                         "from pointer target type"),
5476                                      G_("return discards %qv qualifier from "
5477                                         "pointer target type"),
5478                                      TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5479
5480               memb = marginal_memb;
5481             }
5482
5483           if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5484             pedwarn (location, OPT_pedantic,
5485                      "ISO C prohibits argument conversion to union type");
5486
5487           rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5488           return build_constructor_single (type, memb, rhs);
5489         }
5490     }
5491
5492   /* Conversions among pointers */
5493   else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5494            && (coder == codel))
5495     {
5496       tree ttl = TREE_TYPE (type);
5497       tree ttr = TREE_TYPE (rhstype);
5498       tree mvl = ttl;
5499       tree mvr = ttr;
5500       bool is_opaque_pointer;
5501       int target_cmp = 0;   /* Cache comp_target_types () result.  */
5502       addr_space_t asl;
5503       addr_space_t asr;
5504
5505       if (TREE_CODE (mvl) != ARRAY_TYPE)
5506         mvl = TYPE_MAIN_VARIANT (mvl);
5507       if (TREE_CODE (mvr) != ARRAY_TYPE)
5508         mvr = TYPE_MAIN_VARIANT (mvr);
5509       /* Opaque pointers are treated like void pointers.  */
5510       is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5511
5512       /* The Plan 9 compiler permits a pointer to a struct to be
5513          automatically converted into a pointer to an anonymous field
5514          within the struct.  */
5515       if (flag_plan9_extensions
5516           && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5517           && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5518           && mvl != mvr)
5519         {
5520           tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5521           if (new_rhs != NULL_TREE)
5522             {
5523               rhs = new_rhs;
5524               rhstype = TREE_TYPE (rhs);
5525               coder = TREE_CODE (rhstype);
5526               ttr = TREE_TYPE (rhstype);
5527               mvr = TYPE_MAIN_VARIANT (ttr);
5528             }
5529         }
5530
5531       /* C++ does not allow the implicit conversion void* -> T*.  However,
5532          for the purpose of reducing the number of false positives, we
5533          tolerate the special case of
5534
5535                 int *p = NULL;
5536
5537          where NULL is typically defined in C to be '(void *) 0'.  */
5538       if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5539         warning_at (location, OPT_Wc___compat,
5540                     "request for implicit conversion "
5541                     "from %qT to %qT not permitted in C++", rhstype, type);
5542
5543       /* See if the pointers point to incompatible address spaces.  */
5544       asl = TYPE_ADDR_SPACE (ttl);
5545       asr = TYPE_ADDR_SPACE (ttr);
5546       if (!null_pointer_constant_p (rhs)
5547           && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5548         {
5549           switch (errtype)
5550             {
5551             case ic_argpass:
5552               error_at (location, "passing argument %d of %qE from pointer to "
5553                         "non-enclosed address space", parmnum, rname);
5554               break;
5555             case ic_assign:
5556               error_at (location, "assignment from pointer to "
5557                         "non-enclosed address space");
5558               break;
5559             case ic_init:
5560               error_at (location, "initialization from pointer to "
5561                         "non-enclosed address space");
5562               break;
5563             case ic_return:
5564               error_at (location, "return from pointer to "
5565                         "non-enclosed address space");
5566               break;
5567             default:
5568               gcc_unreachable ();
5569             }
5570           return error_mark_node;
5571         }
5572
5573       /* Check if the right-hand side has a format attribute but the
5574          left-hand side doesn't.  */
5575       if (warn_missing_format_attribute
5576           && check_missing_format_attribute (type, rhstype))
5577         {
5578           switch (errtype)
5579           {
5580           case ic_argpass:
5581             warning_at (location, OPT_Wmissing_format_attribute,
5582                         "argument %d of %qE might be "
5583                         "a candidate for a format attribute",
5584                         parmnum, rname);
5585             break;
5586           case ic_assign:
5587             warning_at (location, OPT_Wmissing_format_attribute,
5588                         "assignment left-hand side might be "
5589                         "a candidate for a format attribute");
5590             break;
5591           case ic_init:
5592             warning_at (location, OPT_Wmissing_format_attribute,
5593                         "initialization left-hand side might be "
5594                         "a candidate for a format attribute");
5595             break;
5596           case ic_return:
5597             warning_at (location, OPT_Wmissing_format_attribute,
5598                         "return type might be "
5599                         "a candidate for a format attribute");
5600             break;
5601           default:
5602             gcc_unreachable ();
5603           }
5604         }
5605
5606       /* Any non-function converts to a [const][volatile] void *
5607          and vice versa; otherwise, targets must be the same.
5608          Meanwhile, the lhs target must have all the qualifiers of the rhs.  */
5609       if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5610           || (target_cmp = comp_target_types (location, type, rhstype))
5611           || is_opaque_pointer
5612           || (c_common_unsigned_type (mvl)
5613               == c_common_unsigned_type (mvr)))
5614         {
5615           if (pedantic
5616               && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5617                   ||
5618                   (VOID_TYPE_P (ttr)
5619                    && !null_pointer_constant
5620                    && TREE_CODE (ttl) == FUNCTION_TYPE)))
5621             WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5622                                  G_("ISO C forbids passing argument %d of "
5623                                     "%qE between function pointer "
5624                                     "and %<void *%>"),
5625                                  G_("ISO C forbids assignment between "
5626                                     "function pointer and %<void *%>"),
5627                                  G_("ISO C forbids initialization between "
5628                                     "function pointer and %<void *%>"),
5629                                  G_("ISO C forbids return between function "
5630                                     "pointer and %<void *%>"));
5631           /* Const and volatile mean something different for function types,
5632              so the usual warnings are not appropriate.  */
5633           else if (TREE_CODE (ttr) != FUNCTION_TYPE
5634                    && TREE_CODE (ttl) != FUNCTION_TYPE)
5635             {
5636               if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5637                   & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5638                 {
5639                   WARN_FOR_QUALIFIERS (location, 0,
5640                                        G_("passing argument %d of %qE discards "
5641                                           "%qv qualifier from pointer target type"),
5642                                        G_("assignment discards %qv qualifier "
5643                                           "from pointer target type"),
5644                                        G_("initialization discards %qv qualifier "
5645                                           "from pointer target type"),
5646                                        G_("return discards %qv qualifier from "
5647                                           "pointer target type"),
5648                                        TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5649                 }
5650               /* If this is not a case of ignoring a mismatch in signedness,
5651                  no warning.  */
5652               else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5653                        || target_cmp)
5654                 ;
5655               /* If there is a mismatch, do warn.  */
5656               else if (warn_pointer_sign)
5657                 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5658                                      G_("pointer targets in passing argument "
5659                                         "%d of %qE differ in signedness"),
5660                                      G_("pointer targets in assignment "
5661                                         "differ in signedness"),
5662                                      G_("pointer targets in initialization "
5663                                         "differ in signedness"),
5664                                      G_("pointer targets in return differ "
5665                                         "in signedness"));
5666             }
5667           else if (TREE_CODE (ttl) == FUNCTION_TYPE
5668                    && TREE_CODE (ttr) == FUNCTION_TYPE)
5669             {
5670               /* Because const and volatile on functions are restrictions
5671                  that say the function will not do certain things,
5672                  it is okay to use a const or volatile function
5673                  where an ordinary one is wanted, but not vice-versa.  */
5674               if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5675                   & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5676                 WARN_FOR_QUALIFIERS (location, 0,
5677                                      G_("passing argument %d of %qE makes "
5678                                         "%q#v qualified function pointer "
5679                                         "from unqualified"),
5680                                      G_("assignment makes %q#v qualified function "
5681                                         "pointer from unqualified"),
5682                                      G_("initialization makes %q#v qualified "
5683                                         "function pointer from unqualified"),
5684                                      G_("return makes %q#v qualified function "
5685                                         "pointer from unqualified"),
5686                                      TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5687             }
5688         }
5689       else
5690         /* Avoid warning about the volatile ObjC EH puts on decls.  */
5691         if (!objc_ok)
5692           WARN_FOR_ASSIGNMENT (location, 0,
5693                                G_("passing argument %d of %qE from "
5694                                   "incompatible pointer type"),
5695                                G_("assignment from incompatible pointer type"),
5696                                G_("initialization from incompatible "
5697                                   "pointer type"),
5698                                G_("return from incompatible pointer type"));
5699
5700       return convert (type, rhs);
5701     }
5702   else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5703     {
5704       /* ??? This should not be an error when inlining calls to
5705          unprototyped functions.  */
5706       error_at (location, "invalid use of non-lvalue array");
5707       return error_mark_node;
5708     }
5709   else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5710     {
5711       /* An explicit constant 0 can convert to a pointer,
5712          or one that results from arithmetic, even including
5713          a cast to integer type.  */
5714       if (!null_pointer_constant)
5715         WARN_FOR_ASSIGNMENT (location, 0,
5716                              G_("passing argument %d of %qE makes "
5717                                 "pointer from integer without a cast"),
5718                              G_("assignment makes pointer from integer "
5719                                 "without a cast"),
5720                              G_("initialization makes pointer from "
5721                                 "integer without a cast"),
5722                              G_("return makes pointer from integer "
5723                                 "without a cast"));
5724
5725       return convert (type, rhs);
5726     }
5727   else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5728     {
5729       WARN_FOR_ASSIGNMENT (location, 0,
5730                            G_("passing argument %d of %qE makes integer "
5731                               "from pointer without a cast"),
5732                            G_("assignment makes integer from pointer "
5733                               "without a cast"),
5734                            G_("initialization makes integer from pointer "
5735                               "without a cast"),
5736                            G_("return makes integer from pointer "
5737                               "without a cast"));
5738       return convert (type, rhs);
5739     }
5740   else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5741     {
5742       tree ret;
5743       bool save = in_late_binary_op;
5744       in_late_binary_op = true;
5745       ret = convert (type, rhs);
5746       in_late_binary_op = save;
5747       return ret;
5748     }
5749
5750   switch (errtype)
5751     {
5752     case ic_argpass:
5753       error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5754       inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5755               ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5756               "expected %qT but argument is of type %qT", type, rhstype);
5757       break;
5758     case ic_assign:
5759       error_at (location, "incompatible types when assigning to type %qT from "
5760                 "type %qT", type, rhstype);
5761       break;
5762     case ic_init:
5763       error_at (location,
5764                 "incompatible types when initializing type %qT using type %qT",
5765                 type, rhstype);
5766       break;
5767     case ic_return:
5768       error_at (location,
5769                 "incompatible types when returning type %qT but %qT was "
5770                 "expected", rhstype, type);
5771       break;
5772     default:
5773       gcc_unreachable ();
5774     }
5775
5776   return error_mark_node;
5777 }
5778 \f
5779 /* If VALUE is a compound expr all of whose expressions are constant, then
5780    return its value.  Otherwise, return error_mark_node.
5781
5782    This is for handling COMPOUND_EXPRs as initializer elements
5783    which is allowed with a warning when -pedantic is specified.  */
5784
5785 static tree
5786 valid_compound_expr_initializer (tree value, tree endtype)
5787 {
5788   if (TREE_CODE (value) == COMPOUND_EXPR)
5789     {
5790       if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5791           == error_mark_node)
5792         return error_mark_node;
5793       return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5794                                               endtype);
5795     }
5796   else if (!initializer_constant_valid_p (value, endtype))
5797     return error_mark_node;
5798   else
5799     return value;
5800 }
5801 \f
5802 /* Perform appropriate conversions on the initial value of a variable,
5803    store it in the declaration DECL,
5804    and print any error messages that are appropriate.
5805    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5806    If the init is invalid, store an ERROR_MARK.
5807
5808    INIT_LOC is the location of the initial value.  */
5809
5810 void
5811 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5812 {
5813   tree value, type;
5814   bool npc = false;
5815
5816   /* If variable's type was invalidly declared, just ignore it.  */
5817
5818   type = TREE_TYPE (decl);
5819   if (TREE_CODE (type) == ERROR_MARK)
5820     return;
5821
5822   /* Digest the specified initializer into an expression.  */
5823
5824   if (init)
5825     npc = null_pointer_constant_p (init);
5826   value = digest_init (init_loc, type, init, origtype, npc,
5827                        true, TREE_STATIC (decl));
5828
5829   /* Store the expression if valid; else report error.  */
5830
5831   if (!in_system_header
5832       && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5833     warning (OPT_Wtraditional, "traditional C rejects automatic "
5834              "aggregate initialization");
5835
5836   DECL_INITIAL (decl) = value;
5837
5838   /* ANSI wants warnings about out-of-range constant initializers.  */
5839   STRIP_TYPE_NOPS (value);
5840   if (TREE_STATIC (decl))
5841     constant_expression_warning (value);
5842
5843   /* Check if we need to set array size from compound literal size.  */
5844   if (TREE_CODE (type) == ARRAY_TYPE
5845       && TYPE_DOMAIN (type) == 0
5846       && value != error_mark_node)
5847     {
5848       tree inside_init = init;
5849
5850       STRIP_TYPE_NOPS (inside_init);
5851       inside_init = fold (inside_init);
5852
5853       if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5854         {
5855           tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5856
5857           if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5858             {
5859               /* For int foo[] = (int [3]){1}; we need to set array size
5860                  now since later on array initializer will be just the
5861                  brace enclosed list of the compound literal.  */
5862               tree etype = strip_array_types (TREE_TYPE (decl));
5863               type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5864               TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5865               layout_type (type);
5866               layout_decl (cldecl, 0);
5867               TREE_TYPE (decl)
5868                 = c_build_qualified_type (type, TYPE_QUALS (etype));
5869             }
5870         }
5871     }
5872 }
5873 \f
5874 /* Methods for storing and printing names for error messages.  */
5875
5876 /* Implement a spelling stack that allows components of a name to be pushed
5877    and popped.  Each element on the stack is this structure.  */
5878
5879 struct spelling
5880 {
5881   int kind;
5882   union
5883     {
5884       unsigned HOST_WIDE_INT i;
5885       const char *s;
5886     } u;
5887 };
5888
5889 #define SPELLING_STRING 1
5890 #define SPELLING_MEMBER 2
5891 #define SPELLING_BOUNDS 3
5892
5893 static struct spelling *spelling;       /* Next stack element (unused).  */
5894 static struct spelling *spelling_base;  /* Spelling stack base.  */
5895 static int spelling_size;               /* Size of the spelling stack.  */
5896
5897 /* Macros to save and restore the spelling stack around push_... functions.
5898    Alternative to SAVE_SPELLING_STACK.  */
5899
5900 #define SPELLING_DEPTH() (spelling - spelling_base)
5901 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5902
5903 /* Push an element on the spelling stack with type KIND and assign VALUE
5904    to MEMBER.  */
5905
5906 #define PUSH_SPELLING(KIND, VALUE, MEMBER)                              \
5907 {                                                                       \
5908   int depth = SPELLING_DEPTH ();                                        \
5909                                                                         \
5910   if (depth >= spelling_size)                                           \
5911     {                                                                   \
5912       spelling_size += 10;                                              \
5913       spelling_base = XRESIZEVEC (struct spelling, spelling_base,       \
5914                                   spelling_size);                       \
5915       RESTORE_SPELLING_DEPTH (depth);                                   \
5916     }                                                                   \
5917                                                                         \
5918   spelling->kind = (KIND);                                              \
5919   spelling->MEMBER = (VALUE);                                           \
5920   spelling++;                                                           \
5921 }
5922
5923 /* Push STRING on the stack.  Printed literally.  */
5924
5925 static void
5926 push_string (const char *string)
5927 {
5928   PUSH_SPELLING (SPELLING_STRING, string, u.s);
5929 }
5930
5931 /* Push a member name on the stack.  Printed as '.' STRING.  */
5932
5933 static void
5934 push_member_name (tree decl)
5935 {
5936   const char *const string
5937     = (DECL_NAME (decl)
5938        ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5939        : _("<anonymous>"));
5940   PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5941 }
5942
5943 /* Push an array bounds on the stack.  Printed as [BOUNDS].  */
5944
5945 static void
5946 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5947 {
5948   PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5949 }
5950
5951 /* Compute the maximum size in bytes of the printed spelling.  */
5952
5953 static int
5954 spelling_length (void)
5955 {
5956   int size = 0;
5957   struct spelling *p;
5958
5959   for (p = spelling_base; p < spelling; p++)
5960     {
5961       if (p->kind == SPELLING_BOUNDS)
5962         size += 25;
5963       else
5964         size += strlen (p->u.s) + 1;
5965     }
5966
5967   return size;
5968 }
5969
5970 /* Print the spelling to BUFFER and return it.  */
5971
5972 static char *
5973 print_spelling (char *buffer)
5974 {
5975   char *d = buffer;
5976   struct spelling *p;
5977
5978   for (p = spelling_base; p < spelling; p++)
5979     if (p->kind == SPELLING_BOUNDS)
5980       {
5981         sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5982         d += strlen (d);
5983       }
5984     else
5985       {
5986         const char *s;
5987         if (p->kind == SPELLING_MEMBER)
5988           *d++ = '.';
5989         for (s = p->u.s; (*d = *s++); d++)
5990           ;
5991       }
5992   *d++ = '\0';
5993   return buffer;
5994 }
5995
5996 /* Issue an error message for a bad initializer component.
5997    GMSGID identifies the message.
5998    The component name is taken from the spelling stack.  */
5999
6000 void
6001 error_init (const char *gmsgid)
6002 {
6003   char *ofwhat;
6004
6005   /* The gmsgid may be a format string with %< and %>. */
6006   error (gmsgid);
6007   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6008   if (*ofwhat)
6009     error ("(near initialization for %qs)", ofwhat);
6010 }
6011
6012 /* Issue a pedantic warning for a bad initializer component.  OPT is
6013    the option OPT_* (from options.h) controlling this warning or 0 if
6014    it is unconditionally given.  GMSGID identifies the message.  The
6015    component name is taken from the spelling stack.  */
6016
6017 void
6018 pedwarn_init (location_t location, int opt, const char *gmsgid)
6019 {
6020   char *ofwhat;
6021
6022   /* The gmsgid may be a format string with %< and %>. */
6023   pedwarn (location, opt, gmsgid);
6024   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6025   if (*ofwhat)
6026     pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
6027 }
6028
6029 /* Issue a warning for a bad initializer component.
6030
6031    OPT is the OPT_W* value corresponding to the warning option that
6032    controls this warning.  GMSGID identifies the message.  The
6033    component name is taken from the spelling stack.  */
6034
6035 static void
6036 warning_init (int opt, const char *gmsgid)
6037 {
6038   char *ofwhat;
6039
6040   /* The gmsgid may be a format string with %< and %>. */
6041   warning (opt, gmsgid);
6042   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6043   if (*ofwhat)
6044     warning (opt, "(near initialization for %qs)", ofwhat);
6045 }
6046 \f
6047 /* If TYPE is an array type and EXPR is a parenthesized string
6048    constant, warn if pedantic that EXPR is being used to initialize an
6049    object of type TYPE.  */
6050
6051 void
6052 maybe_warn_string_init (tree type, struct c_expr expr)
6053 {
6054   if (pedantic
6055       && TREE_CODE (type) == ARRAY_TYPE
6056       && TREE_CODE (expr.value) == STRING_CST
6057       && expr.original_code != STRING_CST)
6058     pedwarn_init (input_location, OPT_pedantic,
6059                   "array initialized from parenthesized string constant");
6060 }
6061
6062 /* Digest the parser output INIT as an initializer for type TYPE.
6063    Return a C expression of type TYPE to represent the initial value.
6064
6065    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6066
6067    NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6068
6069    If INIT is a string constant, STRICT_STRING is true if it is
6070    unparenthesized or we should not warn here for it being parenthesized.
6071    For other types of INIT, STRICT_STRING is not used.
6072
6073    INIT_LOC is the location of the INIT.
6074
6075    REQUIRE_CONSTANT requests an error if non-constant initializers or
6076    elements are seen.  */
6077
6078 static tree
6079 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6080              bool null_pointer_constant, bool strict_string,
6081              int require_constant)
6082 {
6083   enum tree_code code = TREE_CODE (type);
6084   tree inside_init = init;
6085   tree semantic_type = NULL_TREE;
6086   bool maybe_const = true;
6087
6088   if (type == error_mark_node
6089       || !init
6090       || init == error_mark_node
6091       || TREE_TYPE (init) == error_mark_node)
6092     return error_mark_node;
6093
6094   STRIP_TYPE_NOPS (inside_init);
6095
6096   if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6097     {
6098       semantic_type = TREE_TYPE (inside_init);
6099       inside_init = TREE_OPERAND (inside_init, 0);
6100     }
6101   inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6102   inside_init = decl_constant_value_for_optimization (inside_init);
6103
6104   /* Initialization of an array of chars from a string constant
6105      optionally enclosed in braces.  */
6106
6107   if (code == ARRAY_TYPE && inside_init
6108       && TREE_CODE (inside_init) == STRING_CST)
6109     {
6110       tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6111       /* Note that an array could be both an array of character type
6112          and an array of wchar_t if wchar_t is signed char or unsigned
6113          char.  */
6114       bool char_array = (typ1 == char_type_node
6115                          || typ1 == signed_char_type_node
6116                          || typ1 == unsigned_char_type_node);
6117       bool wchar_array = !!comptypes (typ1, wchar_type_node);
6118       bool char16_array = !!comptypes (typ1, char16_type_node);
6119       bool char32_array = !!comptypes (typ1, char32_type_node);
6120
6121       if (char_array || wchar_array || char16_array || char32_array)
6122         {
6123           struct c_expr expr;
6124           tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6125           expr.value = inside_init;
6126           expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6127           expr.original_type = NULL;
6128           maybe_warn_string_init (type, expr);
6129
6130           if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6131             pedwarn_init (init_loc, OPT_pedantic,
6132                           "initialization of a flexible array member");
6133
6134           if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6135                          TYPE_MAIN_VARIANT (type)))
6136             return inside_init;
6137
6138           if (char_array)
6139             {
6140               if (typ2 != char_type_node)
6141                 {
6142                   error_init ("char-array initialized from wide string");
6143                   return error_mark_node;
6144                 }
6145             }
6146           else
6147             {
6148               if (typ2 == char_type_node)
6149                 {
6150                   error_init ("wide character array initialized from non-wide "
6151                               "string");
6152                   return error_mark_node;
6153                 }
6154               else if (!comptypes(typ1, typ2))
6155                 {
6156                   error_init ("wide character array initialized from "
6157                               "incompatible wide string");
6158                   return error_mark_node;
6159                 }
6160             }
6161
6162           TREE_TYPE (inside_init) = type;
6163           if (TYPE_DOMAIN (type) != 0
6164               && TYPE_SIZE (type) != 0
6165               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6166             {
6167               unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6168
6169               /* Subtract the size of a single (possibly wide) character
6170                  because it's ok to ignore the terminating null char
6171                  that is counted in the length of the constant.  */
6172               if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6173                                         (len
6174                                          - (TYPE_PRECISION (typ1)
6175                                             / BITS_PER_UNIT))))
6176                 pedwarn_init (init_loc, 0,
6177                               ("initializer-string for array of chars "
6178                                "is too long"));
6179               else if (warn_cxx_compat
6180                        && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6181                 warning_at (init_loc, OPT_Wc___compat,
6182                             ("initializer-string for array chars "
6183                              "is too long for C++"));
6184             }
6185
6186           return inside_init;
6187         }
6188       else if (INTEGRAL_TYPE_P (typ1))
6189         {
6190           error_init ("array of inappropriate type initialized "
6191                       "from string constant");
6192           return error_mark_node;
6193         }
6194     }
6195
6196   /* Build a VECTOR_CST from a *constant* vector constructor.  If the
6197      vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6198      below and handle as a constructor.  */
6199   if (code == VECTOR_TYPE
6200       && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6201       && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6202       && TREE_CONSTANT (inside_init))
6203     {
6204       if (TREE_CODE (inside_init) == VECTOR_CST
6205           && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6206                         TYPE_MAIN_VARIANT (type)))
6207         return inside_init;
6208
6209       if (TREE_CODE (inside_init) == CONSTRUCTOR)
6210         {
6211           unsigned HOST_WIDE_INT ix;
6212           tree value;
6213           bool constant_p = true;
6214
6215           /* Iterate through elements and check if all constructor
6216              elements are *_CSTs.  */
6217           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6218             if (!CONSTANT_CLASS_P (value))
6219               {
6220                 constant_p = false;
6221                 break;
6222               }
6223
6224           if (constant_p)
6225             return build_vector_from_ctor (type,
6226                                            CONSTRUCTOR_ELTS (inside_init));
6227         }
6228     }
6229
6230   if (warn_sequence_point)
6231     verify_sequence_points (inside_init);
6232
6233   /* Any type can be initialized
6234      from an expression of the same type, optionally with braces.  */
6235
6236   if (inside_init && TREE_TYPE (inside_init) != 0
6237       && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6238                      TYPE_MAIN_VARIANT (type))
6239           || (code == ARRAY_TYPE
6240               && comptypes (TREE_TYPE (inside_init), type))
6241           || (code == VECTOR_TYPE
6242               && comptypes (TREE_TYPE (inside_init), type))
6243           || (code == POINTER_TYPE
6244               && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6245               && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6246                             TREE_TYPE (type)))))
6247     {
6248       if (code == POINTER_TYPE)
6249         {
6250           if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6251             {
6252               if (TREE_CODE (inside_init) == STRING_CST
6253                   || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6254                 inside_init = array_to_pointer_conversion
6255                   (init_loc, inside_init);
6256               else
6257                 {
6258                   error_init ("invalid use of non-lvalue array");
6259                   return error_mark_node;
6260                 }
6261             }
6262         }
6263
6264       if (code == VECTOR_TYPE)
6265         /* Although the types are compatible, we may require a
6266            conversion.  */
6267         inside_init = convert (type, inside_init);
6268
6269       if (require_constant
6270           && (code == VECTOR_TYPE || !flag_isoc99)
6271           && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6272         {
6273           /* As an extension, allow initializing objects with static storage
6274              duration with compound literals (which are then treated just as
6275              the brace enclosed list they contain).  Also allow this for
6276              vectors, as we can only assign them with compound literals.  */
6277           tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6278           inside_init = DECL_INITIAL (decl);
6279         }
6280
6281       if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6282           && TREE_CODE (inside_init) != CONSTRUCTOR)
6283         {
6284           error_init ("array initialized from non-constant array expression");
6285           return error_mark_node;
6286         }
6287
6288       /* Compound expressions can only occur here if -pedantic or
6289          -pedantic-errors is specified.  In the later case, we always want
6290          an error.  In the former case, we simply want a warning.  */
6291       if (require_constant && pedantic
6292           && TREE_CODE (inside_init) == COMPOUND_EXPR)
6293         {
6294           inside_init
6295             = valid_compound_expr_initializer (inside_init,
6296                                                TREE_TYPE (inside_init));
6297           if (inside_init == error_mark_node)
6298             error_init ("initializer element is not constant");
6299           else
6300             pedwarn_init (init_loc, OPT_pedantic,
6301                           "initializer element is not constant");
6302           if (flag_pedantic_errors)
6303             inside_init = error_mark_node;
6304         }
6305       else if (require_constant
6306                && !initializer_constant_valid_p (inside_init,
6307                                                  TREE_TYPE (inside_init)))
6308         {
6309           error_init ("initializer element is not constant");
6310           inside_init = error_mark_node;
6311         }
6312       else if (require_constant && !maybe_const)
6313         pedwarn_init (init_loc, 0,
6314                       "initializer element is not a constant expression");
6315
6316       /* Added to enable additional -Wmissing-format-attribute warnings.  */
6317       if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6318         inside_init = convert_for_assignment (init_loc, type, inside_init,
6319                                               origtype,
6320                                               ic_init, null_pointer_constant,
6321                                               NULL_TREE, NULL_TREE, 0);
6322       return inside_init;
6323     }
6324
6325   /* Handle scalar types, including conversions.  */
6326
6327   if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6328       || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6329       || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6330     {
6331       if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6332           && (TREE_CODE (init) == STRING_CST
6333               || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6334         inside_init = init = array_to_pointer_conversion (init_loc, init);
6335       if (semantic_type)
6336         inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6337                               inside_init);
6338       inside_init
6339         = convert_for_assignment (init_loc, type, inside_init, origtype,
6340                                   ic_init, null_pointer_constant,
6341                                   NULL_TREE, NULL_TREE, 0);
6342
6343       /* Check to see if we have already given an error message.  */
6344       if (inside_init == error_mark_node)
6345         ;
6346       else if (require_constant && !TREE_CONSTANT (inside_init))
6347         {
6348           error_init ("initializer element is not constant");
6349           inside_init = error_mark_node;
6350         }
6351       else if (require_constant
6352                && !initializer_constant_valid_p (inside_init,
6353                                                  TREE_TYPE (inside_init)))
6354         {
6355           error_init ("initializer element is not computable at load time");
6356           inside_init = error_mark_node;
6357         }
6358       else if (require_constant && !maybe_const)
6359         pedwarn_init (init_loc, 0,
6360                       "initializer element is not a constant expression");
6361
6362       return inside_init;
6363     }
6364
6365   /* Come here only for records and arrays.  */
6366
6367   if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6368     {
6369       error_init ("variable-sized object may not be initialized");
6370       return error_mark_node;
6371     }
6372
6373   error_init ("invalid initializer");
6374   return error_mark_node;
6375 }
6376 \f
6377 /* Handle initializers that use braces.  */
6378
6379 /* Type of object we are accumulating a constructor for.
6380    This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE.  */
6381 static tree constructor_type;
6382
6383 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6384    left to fill.  */
6385 static tree constructor_fields;
6386
6387 /* For an ARRAY_TYPE, this is the specified index
6388    at which to store the next element we get.  */
6389 static tree constructor_index;
6390
6391 /* For an ARRAY_TYPE, this is the maximum index.  */
6392 static tree constructor_max_index;
6393
6394 /* For a RECORD_TYPE, this is the first field not yet written out.  */
6395 static tree constructor_unfilled_fields;
6396
6397 /* For an ARRAY_TYPE, this is the index of the first element
6398    not yet written out.  */
6399 static tree constructor_unfilled_index;
6400
6401 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6402    This is so we can generate gaps between fields, when appropriate.  */
6403 static tree constructor_bit_index;
6404
6405 /* If we are saving up the elements rather than allocating them,
6406    this is the list of elements so far (in reverse order,
6407    most recent first).  */
6408 static VEC(constructor_elt,gc) *constructor_elements;
6409
6410 /* 1 if constructor should be incrementally stored into a constructor chain,
6411    0 if all the elements should be kept in AVL tree.  */
6412 static int constructor_incremental;
6413
6414 /* 1 if so far this constructor's elements are all compile-time constants.  */
6415 static int constructor_constant;
6416
6417 /* 1 if so far this constructor's elements are all valid address constants.  */
6418 static int constructor_simple;
6419
6420 /* 1 if this constructor has an element that cannot be part of a
6421    constant expression.  */
6422 static int constructor_nonconst;
6423
6424 /* 1 if this constructor is erroneous so far.  */
6425 static int constructor_erroneous;
6426
6427 /* Structure for managing pending initializer elements, organized as an
6428    AVL tree.  */
6429
6430 struct init_node
6431 {
6432   struct init_node *left, *right;
6433   struct init_node *parent;
6434   int balance;
6435   tree purpose;
6436   tree value;
6437   tree origtype;
6438 };
6439
6440 /* Tree of pending elements at this constructor level.
6441    These are elements encountered out of order
6442    which belong at places we haven't reached yet in actually
6443    writing the output.
6444    Will never hold tree nodes across GC runs.  */
6445 static struct init_node *constructor_pending_elts;
6446
6447 /* The SPELLING_DEPTH of this constructor.  */
6448 static int constructor_depth;
6449
6450 /* DECL node for which an initializer is being read.
6451    0 means we are reading a constructor expression
6452    such as (struct foo) {...}.  */
6453 static tree constructor_decl;
6454
6455 /* Nonzero if this is an initializer for a top-level decl.  */
6456 static int constructor_top_level;
6457
6458 /* Nonzero if there were any member designators in this initializer.  */
6459 static int constructor_designated;
6460
6461 /* Nesting depth of designator list.  */
6462 static int designator_depth;
6463
6464 /* Nonzero if there were diagnosed errors in this designator list.  */
6465 static int designator_erroneous;
6466
6467 \f
6468 /* This stack has a level for each implicit or explicit level of
6469    structuring in the initializer, including the outermost one.  It
6470    saves the values of most of the variables above.  */
6471
6472 struct constructor_range_stack;
6473
6474 struct constructor_stack
6475 {
6476   struct constructor_stack *next;
6477   tree type;
6478   tree fields;
6479   tree index;
6480   tree max_index;
6481   tree unfilled_index;
6482   tree unfilled_fields;
6483   tree bit_index;
6484   VEC(constructor_elt,gc) *elements;
6485   struct init_node *pending_elts;
6486   int offset;
6487   int depth;
6488   /* If value nonzero, this value should replace the entire
6489      constructor at this level.  */
6490   struct c_expr replacement_value;
6491   struct constructor_range_stack *range_stack;
6492   char constant;
6493   char simple;
6494   char nonconst;
6495   char implicit;
6496   char erroneous;
6497   char outer;
6498   char incremental;
6499   char designated;
6500 };
6501
6502 static struct constructor_stack *constructor_stack;
6503
6504 /* This stack represents designators from some range designator up to
6505    the last designator in the list.  */
6506
6507 struct constructor_range_stack
6508 {
6509   struct constructor_range_stack *next, *prev;
6510   struct constructor_stack *stack;
6511   tree range_start;
6512   tree index;
6513   tree range_end;
6514   tree fields;
6515 };
6516
6517 static struct constructor_range_stack *constructor_range_stack;
6518
6519 /* This stack records separate initializers that are nested.
6520    Nested initializers can't happen in ANSI C, but GNU C allows them
6521    in cases like { ... (struct foo) { ... } ... }.  */
6522
6523 struct initializer_stack
6524 {
6525   struct initializer_stack *next;
6526   tree decl;
6527   struct constructor_stack *constructor_stack;
6528   struct constructor_range_stack *constructor_range_stack;
6529   VEC(constructor_elt,gc) *elements;
6530   struct spelling *spelling;
6531   struct spelling *spelling_base;
6532   int spelling_size;
6533   char top_level;
6534   char require_constant_value;
6535   char require_constant_elements;
6536 };
6537
6538 static struct initializer_stack *initializer_stack;
6539 \f
6540 /* Prepare to parse and output the initializer for variable DECL.  */
6541
6542 void
6543 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6544 {
6545   const char *locus;
6546   struct initializer_stack *p = XNEW (struct initializer_stack);
6547
6548   p->decl = constructor_decl;
6549   p->require_constant_value = require_constant_value;
6550   p->require_constant_elements = require_constant_elements;
6551   p->constructor_stack = constructor_stack;
6552   p->constructor_range_stack = constructor_range_stack;
6553   p->elements = constructor_elements;
6554   p->spelling = spelling;
6555   p->spelling_base = spelling_base;
6556   p->spelling_size = spelling_size;
6557   p->top_level = constructor_top_level;
6558   p->next = initializer_stack;
6559   initializer_stack = p;
6560
6561   constructor_decl = decl;
6562   constructor_designated = 0;
6563   constructor_top_level = top_level;
6564
6565   if (decl != 0 && decl != error_mark_node)
6566     {
6567       require_constant_value = TREE_STATIC (decl);
6568       require_constant_elements
6569         = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6570            /* For a scalar, you can always use any value to initialize,
6571               even within braces.  */
6572            && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6573                || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6574                || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6575                || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6576       locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6577     }
6578   else
6579     {
6580       require_constant_value = 0;
6581       require_constant_elements = 0;
6582       locus = _("(anonymous)");
6583     }
6584
6585   constructor_stack = 0;
6586   constructor_range_stack = 0;
6587
6588   missing_braces_mentioned = 0;
6589
6590   spelling_base = 0;
6591   spelling_size = 0;
6592   RESTORE_SPELLING_DEPTH (0);
6593
6594   if (locus)
6595     push_string (locus);
6596 }
6597
6598 void
6599 finish_init (void)
6600 {
6601   struct initializer_stack *p = initializer_stack;
6602
6603   /* Free the whole constructor stack of this initializer.  */
6604   while (constructor_stack)
6605     {
6606       struct constructor_stack *q = constructor_stack;
6607       constructor_stack = q->next;
6608       free (q);
6609     }
6610
6611   gcc_assert (!constructor_range_stack);
6612
6613   /* Pop back to the data of the outer initializer (if any).  */
6614   free (spelling_base);
6615
6616   constructor_decl = p->decl;
6617   require_constant_value = p->require_constant_value;
6618   require_constant_elements = p->require_constant_elements;
6619   constructor_stack = p->constructor_stack;
6620   constructor_range_stack = p->constructor_range_stack;
6621   constructor_elements = p->elements;
6622   spelling = p->spelling;
6623   spelling_base = p->spelling_base;
6624   spelling_size = p->spelling_size;
6625   constructor_top_level = p->top_level;
6626   initializer_stack = p->next;
6627   free (p);
6628 }
6629 \f
6630 /* Call here when we see the initializer is surrounded by braces.
6631    This is instead of a call to push_init_level;
6632    it is matched by a call to pop_init_level.
6633
6634    TYPE is the type to initialize, for a constructor expression.
6635    For an initializer for a decl, TYPE is zero.  */
6636
6637 void
6638 really_start_incremental_init (tree type)
6639 {
6640   struct constructor_stack *p = XNEW (struct constructor_stack);
6641
6642   if (type == 0)
6643     type = TREE_TYPE (constructor_decl);
6644
6645   if (TREE_CODE (type) == VECTOR_TYPE
6646       && TYPE_VECTOR_OPAQUE (type))
6647     error ("opaque vector types cannot be initialized");
6648
6649   p->type = constructor_type;
6650   p->fields = constructor_fields;
6651   p->index = constructor_index;
6652   p->max_index = constructor_max_index;
6653   p->unfilled_index = constructor_unfilled_index;
6654   p->unfilled_fields = constructor_unfilled_fields;
6655   p->bit_index = constructor_bit_index;
6656   p->elements = constructor_elements;
6657   p->constant = constructor_constant;
6658   p->simple = constructor_simple;
6659   p->nonconst = constructor_nonconst;
6660   p->erroneous = constructor_erroneous;
6661   p->pending_elts = constructor_pending_elts;
6662   p->depth = constructor_depth;
6663   p->replacement_value.value = 0;
6664   p->replacement_value.original_code = ERROR_MARK;
6665   p->replacement_value.original_type = NULL;
6666   p->implicit = 0;
6667   p->range_stack = 0;
6668   p->outer = 0;
6669   p->incremental = constructor_incremental;
6670   p->designated = constructor_designated;
6671   p->next = 0;
6672   constructor_stack = p;
6673
6674   constructor_constant = 1;
6675   constructor_simple = 1;
6676   constructor_nonconst = 0;
6677   constructor_depth = SPELLING_DEPTH ();
6678   constructor_elements = 0;
6679   constructor_pending_elts = 0;
6680   constructor_type = type;
6681   constructor_incremental = 1;
6682   constructor_designated = 0;
6683   designator_depth = 0;
6684   designator_erroneous = 0;
6685
6686   if (TREE_CODE (constructor_type) == RECORD_TYPE
6687       || TREE_CODE (constructor_type) == UNION_TYPE)
6688     {
6689       constructor_fields = TYPE_FIELDS (constructor_type);
6690       /* Skip any nameless bit fields at the beginning.  */
6691       while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6692              && DECL_NAME (constructor_fields) == 0)
6693         constructor_fields = DECL_CHAIN (constructor_fields);
6694
6695       constructor_unfilled_fields = constructor_fields;
6696       constructor_bit_index = bitsize_zero_node;
6697     }
6698   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6699     {
6700       if (TYPE_DOMAIN (constructor_type))
6701         {
6702           constructor_max_index
6703             = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6704
6705           /* Detect non-empty initializations of zero-length arrays.  */
6706           if (constructor_max_index == NULL_TREE
6707               && TYPE_SIZE (constructor_type))
6708             constructor_max_index = integer_minus_one_node;
6709
6710           /* constructor_max_index needs to be an INTEGER_CST.  Attempts
6711              to initialize VLAs will cause a proper error; avoid tree
6712              checking errors as well by setting a safe value.  */
6713           if (constructor_max_index
6714               && TREE_CODE (constructor_max_index) != INTEGER_CST)
6715             constructor_max_index = integer_minus_one_node;
6716
6717           constructor_index
6718             = convert (bitsizetype,
6719                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6720         }
6721       else
6722         {
6723           constructor_index = bitsize_zero_node;
6724           constructor_max_index = NULL_TREE;
6725         }
6726
6727       constructor_unfilled_index = constructor_index;
6728     }
6729   else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6730     {
6731       /* Vectors are like simple fixed-size arrays.  */
6732       constructor_max_index =
6733         bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6734       constructor_index = bitsize_zero_node;
6735       constructor_unfilled_index = constructor_index;
6736     }
6737   else
6738     {
6739       /* Handle the case of int x = {5}; */
6740       constructor_fields = constructor_type;
6741       constructor_unfilled_fields = constructor_type;
6742     }
6743 }
6744 \f
6745 /* Push down into a subobject, for initialization.
6746    If this is for an explicit set of braces, IMPLICIT is 0.
6747    If it is because the next element belongs at a lower level,
6748    IMPLICIT is 1 (or 2 if the push is because of designator list).  */
6749
6750 void
6751 push_init_level (int implicit, struct obstack * braced_init_obstack)
6752 {
6753   struct constructor_stack *p;
6754   tree value = NULL_TREE;
6755
6756   /* If we've exhausted any levels that didn't have braces,
6757      pop them now.  If implicit == 1, this will have been done in
6758      process_init_element; do not repeat it here because in the case
6759      of excess initializers for an empty aggregate this leads to an
6760      infinite cycle of popping a level and immediately recreating
6761      it.  */
6762   if (implicit != 1)
6763     {
6764       while (constructor_stack->implicit)
6765         {
6766           if ((TREE_CODE (constructor_type) == RECORD_TYPE
6767                || TREE_CODE (constructor_type) == UNION_TYPE)
6768               && constructor_fields == 0)
6769             process_init_element (pop_init_level (1, braced_init_obstack),
6770                                   true, braced_init_obstack);
6771           else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6772                    && constructor_max_index
6773                    && tree_int_cst_lt (constructor_max_index,
6774                                        constructor_index))
6775             process_init_element (pop_init_level (1, braced_init_obstack),
6776                                   true, braced_init_obstack);
6777           else
6778             break;
6779         }
6780     }
6781
6782   /* Unless this is an explicit brace, we need to preserve previous
6783      content if any.  */
6784   if (implicit)
6785     {
6786       if ((TREE_CODE (constructor_type) == RECORD_TYPE
6787            || TREE_CODE (constructor_type) == UNION_TYPE)
6788           && constructor_fields)
6789         value = find_init_member (constructor_fields, braced_init_obstack);
6790       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6791         value = find_init_member (constructor_index, braced_init_obstack);
6792     }
6793
6794   p = XNEW (struct constructor_stack);
6795   p->type = constructor_type;
6796   p->fields = constructor_fields;
6797   p->index = constructor_index;
6798   p->max_index = constructor_max_index;
6799   p->unfilled_index = constructor_unfilled_index;
6800   p->unfilled_fields = constructor_unfilled_fields;
6801   p->bit_index = constructor_bit_index;
6802   p->elements = constructor_elements;
6803   p->constant = constructor_constant;
6804   p->simple = constructor_simple;
6805   p->nonconst = constructor_nonconst;
6806   p->erroneous = constructor_erroneous;
6807   p->pending_elts = constructor_pending_elts;
6808   p->depth = constructor_depth;
6809   p->replacement_value.value = 0;
6810   p->replacement_value.original_code = ERROR_MARK;
6811   p->replacement_value.original_type = NULL;
6812   p->implicit = implicit;
6813   p->outer = 0;
6814   p->incremental = constructor_incremental;
6815   p->designated = constructor_designated;
6816   p->next = constructor_stack;
6817   p->range_stack = 0;
6818   constructor_stack = p;
6819
6820   constructor_constant = 1;
6821   constructor_simple = 1;
6822   constructor_nonconst = 0;
6823   constructor_depth = SPELLING_DEPTH ();
6824   constructor_elements = 0;
6825   constructor_incremental = 1;
6826   constructor_designated = 0;
6827   constructor_pending_elts = 0;
6828   if (!implicit)
6829     {
6830       p->range_stack = constructor_range_stack;
6831       constructor_range_stack = 0;
6832       designator_depth = 0;
6833       designator_erroneous = 0;
6834     }
6835
6836   /* Don't die if an entire brace-pair level is superfluous
6837      in the containing level.  */
6838   if (constructor_type == 0)
6839     ;
6840   else if (TREE_CODE (constructor_type) == RECORD_TYPE
6841            || TREE_CODE (constructor_type) == UNION_TYPE)
6842     {
6843       /* Don't die if there are extra init elts at the end.  */
6844       if (constructor_fields == 0)
6845         constructor_type = 0;
6846       else
6847         {
6848           constructor_type = TREE_TYPE (constructor_fields);
6849           push_member_name (constructor_fields);
6850           constructor_depth++;
6851         }
6852     }
6853   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6854     {
6855       constructor_type = TREE_TYPE (constructor_type);
6856       push_array_bounds (tree_low_cst (constructor_index, 1));
6857       constructor_depth++;
6858     }
6859
6860   if (constructor_type == 0)
6861     {
6862       error_init ("extra brace group at end of initializer");
6863       constructor_fields = 0;
6864       constructor_unfilled_fields = 0;
6865       return;
6866     }
6867
6868   if (value && TREE_CODE (value) == CONSTRUCTOR)
6869     {
6870       constructor_constant = TREE_CONSTANT (value);
6871       constructor_simple = TREE_STATIC (value);
6872       constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6873       constructor_elements = CONSTRUCTOR_ELTS (value);
6874       if (!VEC_empty (constructor_elt, constructor_elements)
6875           && (TREE_CODE (constructor_type) == RECORD_TYPE
6876               || TREE_CODE (constructor_type) == ARRAY_TYPE))
6877         set_nonincremental_init (braced_init_obstack);
6878     }
6879
6880   if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6881     {
6882       missing_braces_mentioned = 1;
6883       warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6884     }
6885
6886   if (TREE_CODE (constructor_type) == RECORD_TYPE
6887            || TREE_CODE (constructor_type) == UNION_TYPE)
6888     {
6889       constructor_fields = TYPE_FIELDS (constructor_type);
6890       /* Skip any nameless bit fields at the beginning.  */
6891       while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6892              && DECL_NAME (constructor_fields) == 0)
6893         constructor_fields = DECL_CHAIN (constructor_fields);
6894
6895       constructor_unfilled_fields = constructor_fields;
6896       constructor_bit_index = bitsize_zero_node;
6897     }
6898   else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6899     {
6900       /* Vectors are like simple fixed-size arrays.  */
6901       constructor_max_index =
6902         bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6903       constructor_index = bitsize_int (0);
6904       constructor_unfilled_index = constructor_index;
6905     }
6906   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6907     {
6908       if (TYPE_DOMAIN (constructor_type))
6909         {
6910           constructor_max_index
6911             = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6912
6913           /* Detect non-empty initializations of zero-length arrays.  */
6914           if (constructor_max_index == NULL_TREE
6915               && TYPE_SIZE (constructor_type))
6916             constructor_max_index = integer_minus_one_node;
6917
6918           /* constructor_max_index needs to be an INTEGER_CST.  Attempts
6919              to initialize VLAs will cause a proper error; avoid tree
6920              checking errors as well by setting a safe value.  */
6921           if (constructor_max_index
6922               && TREE_CODE (constructor_max_index) != INTEGER_CST)
6923             constructor_max_index = integer_minus_one_node;
6924
6925           constructor_index
6926             = convert (bitsizetype,
6927                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6928         }
6929       else
6930         constructor_index = bitsize_zero_node;
6931
6932       constructor_unfilled_index = constructor_index;
6933       if (value && TREE_CODE (value) == STRING_CST)
6934         {
6935           /* We need to split the char/wchar array into individual
6936              characters, so that we don't have to special case it
6937              everywhere.  */
6938           set_nonincremental_init_from_string (value, braced_init_obstack);
6939         }
6940     }
6941   else
6942     {
6943       if (constructor_type != error_mark_node)
6944         warning_init (0, "braces around scalar initializer");
6945       constructor_fields = constructor_type;
6946       constructor_unfilled_fields = constructor_type;
6947     }
6948 }
6949
6950 /* At the end of an implicit or explicit brace level,
6951    finish up that level of constructor.  If a single expression
6952    with redundant braces initialized that level, return the
6953    c_expr structure for that expression.  Otherwise, the original_code
6954    element is set to ERROR_MARK.
6955    If we were outputting the elements as they are read, return 0 as the value
6956    from inner levels (process_init_element ignores that),
6957    but return error_mark_node as the value from the outermost level
6958    (that's what we want to put in DECL_INITIAL).
6959    Otherwise, return a CONSTRUCTOR expression as the value.  */
6960
6961 struct c_expr
6962 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6963 {
6964   struct constructor_stack *p;
6965   struct c_expr ret;
6966   ret.value = 0;
6967   ret.original_code = ERROR_MARK;
6968   ret.original_type = NULL;
6969
6970   if (implicit == 0)
6971     {
6972       /* When we come to an explicit close brace,
6973          pop any inner levels that didn't have explicit braces.  */
6974       while (constructor_stack->implicit)
6975         {
6976           process_init_element (pop_init_level (1, braced_init_obstack),
6977                                 true, braced_init_obstack);
6978         }
6979       gcc_assert (!constructor_range_stack);
6980     }
6981
6982   /* Now output all pending elements.  */
6983   constructor_incremental = 1;
6984   output_pending_init_elements (1, braced_init_obstack);
6985
6986   p = constructor_stack;
6987
6988   /* Error for initializing a flexible array member, or a zero-length
6989      array member in an inappropriate context.  */
6990   if (constructor_type && constructor_fields
6991       && TREE_CODE (constructor_type) == ARRAY_TYPE
6992       && TYPE_DOMAIN (constructor_type)
6993       && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6994     {
6995       /* Silently discard empty initializations.  The parser will
6996          already have pedwarned for empty brackets.  */
6997       if (integer_zerop (constructor_unfilled_index))
6998         constructor_type = NULL_TREE;
6999       else
7000         {
7001           gcc_assert (!TYPE_SIZE (constructor_type));
7002
7003           if (constructor_depth > 2)
7004             error_init ("initialization of flexible array member in a nested context");
7005           else
7006             pedwarn_init (input_location, OPT_pedantic,
7007                           "initialization of a flexible array member");
7008
7009           /* We have already issued an error message for the existence
7010              of a flexible array member not at the end of the structure.
7011              Discard the initializer so that we do not die later.  */
7012           if (DECL_CHAIN (constructor_fields) != NULL_TREE)
7013             constructor_type = NULL_TREE;
7014         }
7015     }
7016
7017   /* Warn when some struct elements are implicitly initialized to zero.  */
7018   if (warn_missing_field_initializers
7019       && constructor_type
7020       && TREE_CODE (constructor_type) == RECORD_TYPE
7021       && constructor_unfilled_fields)
7022     {
7023         bool constructor_zeroinit =
7024          (VEC_length (constructor_elt, constructor_elements) == 1
7025           && integer_zerop
7026               (VEC_index (constructor_elt, constructor_elements, 0)->value));
7027
7028         /* Do not warn for flexible array members or zero-length arrays.  */
7029         while (constructor_unfilled_fields
7030                && (!DECL_SIZE (constructor_unfilled_fields)
7031                    || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
7032           constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
7033
7034         if (constructor_unfilled_fields
7035             /* Do not warn if this level of the initializer uses member
7036                designators; it is likely to be deliberate.  */
7037             && !constructor_designated
7038             /* Do not warn about initializing with ` = {0}'.  */
7039             && !constructor_zeroinit)
7040           {
7041             push_member_name (constructor_unfilled_fields);
7042             warning_init (OPT_Wmissing_field_initializers,
7043                           "missing initializer");
7044             RESTORE_SPELLING_DEPTH (constructor_depth);
7045           }
7046     }
7047
7048   /* Pad out the end of the structure.  */
7049   if (p->replacement_value.value)
7050     /* If this closes a superfluous brace pair,
7051        just pass out the element between them.  */
7052     ret = p->replacement_value;
7053   else if (constructor_type == 0)
7054     ;
7055   else if (TREE_CODE (constructor_type) != RECORD_TYPE
7056            && TREE_CODE (constructor_type) != UNION_TYPE
7057            && TREE_CODE (constructor_type) != ARRAY_TYPE
7058            && TREE_CODE (constructor_type) != VECTOR_TYPE)
7059     {
7060       /* A nonincremental scalar initializer--just return
7061          the element, after verifying there is just one.  */
7062       if (VEC_empty (constructor_elt,constructor_elements))
7063         {
7064           if (!constructor_erroneous)
7065             error_init ("empty scalar initializer");
7066           ret.value = error_mark_node;
7067         }
7068       else if (VEC_length (constructor_elt,constructor_elements) != 1)
7069         {
7070           error_init ("extra elements in scalar initializer");
7071           ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7072         }
7073       else
7074         ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
7075     }
7076   else
7077     {
7078       if (constructor_erroneous)
7079         ret.value = error_mark_node;
7080       else
7081         {
7082           ret.value = build_constructor (constructor_type,
7083                                          constructor_elements);
7084           if (constructor_constant)
7085             TREE_CONSTANT (ret.value) = 1;
7086           if (constructor_constant && constructor_simple)
7087             TREE_STATIC (ret.value) = 1;
7088           if (constructor_nonconst)
7089             CONSTRUCTOR_NON_CONST (ret.value) = 1;
7090         }
7091     }
7092
7093   if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7094     {
7095       if (constructor_nonconst)
7096         ret.original_code = C_MAYBE_CONST_EXPR;
7097       else if (ret.original_code == C_MAYBE_CONST_EXPR)
7098         ret.original_code = ERROR_MARK;
7099     }
7100
7101   constructor_type = p->type;
7102   constructor_fields = p->fields;
7103   constructor_index = p->index;
7104   constructor_max_index = p->max_index;
7105   constructor_unfilled_index = p->unfilled_index;
7106   constructor_unfilled_fields = p->unfilled_fields;
7107   constructor_bit_index = p->bit_index;
7108   constructor_elements = p->elements;
7109   constructor_constant = p->constant;
7110   constructor_simple = p->simple;
7111   constructor_nonconst = p->nonconst;
7112   constructor_erroneous = p->erroneous;
7113   constructor_incremental = p->incremental;
7114   constructor_designated = p->designated;
7115   constructor_pending_elts = p->pending_elts;
7116   constructor_depth = p->depth;
7117   if (!p->implicit)
7118     constructor_range_stack = p->range_stack;
7119   RESTORE_SPELLING_DEPTH (constructor_depth);
7120
7121   constructor_stack = p->next;
7122   free (p);
7123
7124   if (ret.value == 0 && constructor_stack == 0)
7125     ret.value = error_mark_node;
7126   return ret;
7127 }
7128
7129 /* Common handling for both array range and field name designators.
7130    ARRAY argument is nonzero for array ranges.  Returns zero for success.  */
7131
7132 static int
7133 set_designator (int array, struct obstack * braced_init_obstack)
7134 {
7135   tree subtype;
7136   enum tree_code subcode;
7137
7138   /* Don't die if an entire brace-pair level is superfluous
7139      in the containing level.  */
7140   if (constructor_type == 0)
7141     return 1;
7142
7143   /* If there were errors in this designator list already, bail out
7144      silently.  */
7145   if (designator_erroneous)
7146     return 1;
7147
7148   if (!designator_depth)
7149     {
7150       gcc_assert (!constructor_range_stack);
7151
7152       /* Designator list starts at the level of closest explicit
7153          braces.  */
7154       while (constructor_stack->implicit)
7155         {
7156           process_init_element (pop_init_level (1, braced_init_obstack),
7157                                 true, braced_init_obstack);
7158         }
7159       constructor_designated = 1;
7160       return 0;
7161     }
7162
7163   switch (TREE_CODE (constructor_type))
7164     {
7165     case  RECORD_TYPE:
7166     case  UNION_TYPE:
7167       subtype = TREE_TYPE (constructor_fields);
7168       if (subtype != error_mark_node)
7169         subtype = TYPE_MAIN_VARIANT (subtype);
7170       break;
7171     case ARRAY_TYPE:
7172       subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7173       break;
7174     default:
7175       gcc_unreachable ();
7176     }
7177
7178   subcode = TREE_CODE (subtype);
7179   if (array && subcode != ARRAY_TYPE)
7180     {
7181       error_init ("array index in non-array initializer");
7182       return 1;
7183     }
7184   else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7185     {
7186       error_init ("field name not in record or union initializer");
7187       return 1;
7188     }
7189
7190   constructor_designated = 1;
7191   push_init_level (2, braced_init_obstack);
7192   return 0;
7193 }
7194
7195 /* If there are range designators in designator list, push a new designator
7196    to constructor_range_stack.  RANGE_END is end of such stack range or
7197    NULL_TREE if there is no range designator at this level.  */
7198
7199 static void
7200 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7201 {
7202   struct constructor_range_stack *p;
7203
7204   p = (struct constructor_range_stack *)
7205     obstack_alloc (braced_init_obstack,
7206                    sizeof (struct constructor_range_stack));
7207   p->prev = constructor_range_stack;
7208   p->next = 0;
7209   p->fields = constructor_fields;
7210   p->range_start = constructor_index;
7211   p->index = constructor_index;
7212   p->stack = constructor_stack;
7213   p->range_end = range_end;
7214   if (constructor_range_stack)
7215     constructor_range_stack->next = p;
7216   constructor_range_stack = p;
7217 }
7218
7219 /* Within an array initializer, specify the next index to be initialized.
7220    FIRST is that index.  If LAST is nonzero, then initialize a range
7221    of indices, running from FIRST through LAST.  */
7222
7223 void
7224 set_init_index (tree first, tree last,
7225                 struct obstack * braced_init_obstack)
7226 {
7227   if (set_designator (1, braced_init_obstack))
7228     return;
7229
7230   designator_erroneous = 1;
7231
7232   if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7233       || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7234     {
7235       error_init ("array index in initializer not of integer type");
7236       return;
7237     }
7238
7239   if (TREE_CODE (first) != INTEGER_CST)
7240     {
7241       first = c_fully_fold (first, false, NULL);
7242       if (TREE_CODE (first) == INTEGER_CST)
7243         pedwarn_init (input_location, OPT_pedantic,
7244                       "array index in initializer is not "
7245                       "an integer constant expression");
7246     }
7247
7248   if (last && TREE_CODE (last) != INTEGER_CST)
7249     {
7250       last = c_fully_fold (last, false, NULL);
7251       if (TREE_CODE (last) == INTEGER_CST)
7252         pedwarn_init (input_location, OPT_pedantic,
7253                       "array index in initializer is not "
7254                       "an integer constant expression");
7255     }
7256
7257   if (TREE_CODE (first) != INTEGER_CST)
7258     error_init ("nonconstant array index in initializer");
7259   else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7260     error_init ("nonconstant array index in initializer");
7261   else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7262     error_init ("array index in non-array initializer");
7263   else if (tree_int_cst_sgn (first) == -1)
7264     error_init ("array index in initializer exceeds array bounds");
7265   else if (constructor_max_index
7266            && tree_int_cst_lt (constructor_max_index, first))
7267     error_init ("array index in initializer exceeds array bounds");
7268   else
7269     {
7270       constant_expression_warning (first);
7271       if (last)
7272         constant_expression_warning (last);
7273       constructor_index = convert (bitsizetype, first);
7274
7275       if (last)
7276         {
7277           if (tree_int_cst_equal (first, last))
7278             last = 0;
7279           else if (tree_int_cst_lt (last, first))
7280             {
7281               error_init ("empty index range in initializer");
7282               last = 0;
7283             }
7284           else
7285             {
7286               last = convert (bitsizetype, last);
7287               if (constructor_max_index != 0
7288                   && tree_int_cst_lt (constructor_max_index, last))
7289                 {
7290                   error_init ("array index range in initializer exceeds array bounds");
7291                   last = 0;
7292                 }
7293             }
7294         }
7295
7296       designator_depth++;
7297       designator_erroneous = 0;
7298       if (constructor_range_stack || last)
7299         push_range_stack (last, braced_init_obstack);
7300     }
7301 }
7302
7303 /* Within a struct initializer, specify the next field to be initialized.  */
7304
7305 void
7306 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7307 {
7308   tree field;
7309
7310   if (set_designator (0, braced_init_obstack))
7311     return;
7312
7313   designator_erroneous = 1;
7314
7315   if (TREE_CODE (constructor_type) != RECORD_TYPE
7316       && TREE_CODE (constructor_type) != UNION_TYPE)
7317     {
7318       error_init ("field name not in record or union initializer");
7319       return;
7320     }
7321
7322   field = lookup_field (constructor_type, fieldname);
7323
7324   if (field == 0)
7325     error ("unknown field %qE specified in initializer", fieldname);
7326   else
7327     do
7328       {
7329         constructor_fields = TREE_VALUE (field);
7330         designator_depth++;
7331         designator_erroneous = 0;
7332         if (constructor_range_stack)
7333           push_range_stack (NULL_TREE, braced_init_obstack);
7334         field = TREE_CHAIN (field);
7335         if (field)
7336           {
7337             if (set_designator (0, braced_init_obstack))
7338               return;
7339           }
7340       }
7341     while (field != NULL_TREE);
7342 }
7343 \f
7344 /* Add a new initializer to the tree of pending initializers.  PURPOSE
7345    identifies the initializer, either array index or field in a structure.
7346    VALUE is the value of that index or field.  If ORIGTYPE is not
7347    NULL_TREE, it is the original type of VALUE.
7348
7349    IMPLICIT is true if value comes from pop_init_level (1),
7350    the new initializer has been merged with the existing one
7351    and thus no warnings should be emitted about overriding an
7352    existing initializer.  */
7353
7354 static void
7355 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7356                   struct obstack * braced_init_obstack)
7357 {
7358   struct init_node *p, **q, *r;
7359
7360   q = &constructor_pending_elts;
7361   p = 0;
7362
7363   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7364     {
7365       while (*q != 0)
7366         {
7367           p = *q;
7368           if (tree_int_cst_lt (purpose, p->purpose))
7369             q = &p->left;
7370           else if (tree_int_cst_lt (p->purpose, purpose))
7371             q = &p->right;
7372           else
7373             {
7374               if (!implicit)
7375                 {
7376                   if (TREE_SIDE_EFFECTS (p->value))
7377                     warning_init (0, "initialized field with side-effects overwritten");
7378                   else if (warn_override_init)
7379                     warning_init (OPT_Woverride_init, "initialized field overwritten");
7380                 }
7381               p->value = value;
7382               p->origtype = origtype;
7383               return;
7384             }
7385         }
7386     }
7387   else
7388     {
7389       tree bitpos;
7390
7391       bitpos = bit_position (purpose);
7392       while (*q != NULL)
7393         {
7394           p = *q;
7395           if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7396             q = &p->left;
7397           else if (p->purpose != purpose)
7398             q = &p->right;
7399           else
7400             {
7401               if (!implicit)
7402                 {
7403                   if (TREE_SIDE_EFFECTS (p->value))
7404                     warning_init (0, "initialized field with side-effects overwritten");
7405                   else if (warn_override_init)
7406                     warning_init (OPT_Woverride_init, "initialized field overwritten");
7407                 }
7408               p->value = value;
7409               p->origtype = origtype;
7410               return;
7411             }
7412         }
7413     }
7414
7415   r = (struct init_node *) obstack_alloc (braced_init_obstack,
7416                                           sizeof (struct init_node));
7417   r->purpose = purpose;
7418   r->value = value;
7419   r->origtype = origtype;
7420
7421   *q = r;
7422   r->parent = p;
7423   r->left = 0;
7424   r->right = 0;
7425   r->balance = 0;
7426
7427   while (p)
7428     {
7429       struct init_node *s;
7430
7431       if (r == p->left)
7432         {
7433           if (p->balance == 0)
7434             p->balance = -1;
7435           else if (p->balance < 0)
7436             {
7437               if (r->balance < 0)
7438                 {
7439                   /* L rotation.  */
7440                   p->left = r->right;
7441                   if (p->left)
7442                     p->left->parent = p;
7443                   r->right = p;
7444
7445                   p->balance = 0;
7446                   r->balance = 0;
7447
7448                   s = p->parent;
7449                   p->parent = r;
7450                   r->parent = s;
7451                   if (s)
7452                     {
7453                       if (s->left == p)
7454                         s->left = r;
7455                       else
7456                         s->right = r;
7457                     }
7458                   else
7459                     constructor_pending_elts = r;
7460                 }
7461               else
7462                 {
7463                   /* LR rotation.  */
7464                   struct init_node *t = r->right;
7465
7466                   r->right = t->left;
7467                   if (r->right)
7468                     r->right->parent = r;
7469                   t->left = r;
7470
7471                   p->left = t->right;
7472                   if (p->left)
7473                     p->left->parent = p;
7474                   t->right = p;
7475
7476                   p->balance = t->balance < 0;
7477                   r->balance = -(t->balance > 0);
7478                   t->balance = 0;
7479
7480                   s = p->parent;
7481                   p->parent = t;
7482                   r->parent = t;
7483                   t->parent = s;
7484                   if (s)
7485                     {
7486                       if (s->left == p)
7487                         s->left = t;
7488                       else
7489                         s->right = t;
7490                     }
7491                   else
7492                     constructor_pending_elts = t;
7493                 }
7494               break;
7495             }
7496           else
7497             {
7498               /* p->balance == +1; growth of left side balances the node.  */
7499               p->balance = 0;
7500               break;
7501             }
7502         }
7503       else /* r == p->right */
7504         {
7505           if (p->balance == 0)
7506             /* Growth propagation from right side.  */
7507             p->balance++;
7508           else if (p->balance > 0)
7509             {
7510               if (r->balance > 0)
7511                 {
7512                   /* R rotation.  */
7513                   p->right = r->left;
7514                   if (p->right)
7515                     p->right->parent = p;
7516                   r->left = p;
7517
7518                   p->balance = 0;
7519                   r->balance = 0;
7520
7521                   s = p->parent;
7522                   p->parent = r;
7523                   r->parent = s;
7524                   if (s)
7525                     {
7526                       if (s->left == p)
7527                         s->left = r;
7528                       else
7529                         s->right = r;
7530                     }
7531                   else
7532                     constructor_pending_elts = r;
7533                 }
7534               else /* r->balance == -1 */
7535                 {
7536                   /* RL rotation */
7537                   struct init_node *t = r->left;
7538
7539                   r->left = t->right;
7540                   if (r->left)
7541                     r->left->parent = r;
7542                   t->right = r;
7543
7544                   p->right = t->left;
7545                   if (p->right)
7546                     p->right->parent = p;
7547                   t->left = p;
7548
7549                   r->balance = (t->balance < 0);
7550                   p->balance = -(t->balance > 0);
7551                   t->balance = 0;
7552
7553                   s = p->parent;
7554                   p->parent = t;
7555                   r->parent = t;
7556                   t->parent = s;
7557                   if (s)
7558                     {
7559                       if (s->left == p)
7560                         s->left = t;
7561                       else
7562                         s->right = t;
7563                     }
7564                   else
7565                     constructor_pending_elts = t;
7566                 }
7567               break;
7568             }
7569           else
7570             {
7571               /* p->balance == -1; growth of right side balances the node.  */
7572               p->balance = 0;
7573               break;
7574             }
7575         }
7576
7577       r = p;
7578       p = p->parent;
7579     }
7580 }
7581
7582 /* Build AVL tree from a sorted chain.  */
7583
7584 static void
7585 set_nonincremental_init (struct obstack * braced_init_obstack)
7586 {
7587   unsigned HOST_WIDE_INT ix;
7588   tree index, value;
7589
7590   if (TREE_CODE (constructor_type) != RECORD_TYPE
7591       && TREE_CODE (constructor_type) != ARRAY_TYPE)
7592     return;
7593
7594   FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7595     {
7596       add_pending_init (index, value, NULL_TREE, false,
7597                         braced_init_obstack);
7598     }
7599   constructor_elements = 0;
7600   if (TREE_CODE (constructor_type) == RECORD_TYPE)
7601     {
7602       constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7603       /* Skip any nameless bit fields at the beginning.  */
7604       while (constructor_unfilled_fields != 0
7605              && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7606              && DECL_NAME (constructor_unfilled_fields) == 0)
7607         constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7608
7609     }
7610   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7611     {
7612       if (TYPE_DOMAIN (constructor_type))
7613         constructor_unfilled_index
7614             = convert (bitsizetype,
7615                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7616       else
7617         constructor_unfilled_index = bitsize_zero_node;
7618     }
7619   constructor_incremental = 0;
7620 }
7621
7622 /* Build AVL tree from a string constant.  */
7623
7624 static void
7625 set_nonincremental_init_from_string (tree str,
7626                                      struct obstack * braced_init_obstack)
7627 {
7628   tree value, purpose, type;
7629   HOST_WIDE_INT val[2];
7630   const char *p, *end;
7631   int byte, wchar_bytes, charwidth, bitpos;
7632
7633   gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7634
7635   wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7636   charwidth = TYPE_PRECISION (char_type_node);
7637   type = TREE_TYPE (constructor_type);
7638   p = TREE_STRING_POINTER (str);
7639   end = p + TREE_STRING_LENGTH (str);
7640
7641   for (purpose = bitsize_zero_node;
7642        p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7643        purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7644     {
7645       if (wchar_bytes == 1)
7646         {
7647           val[1] = (unsigned char) *p++;
7648           val[0] = 0;
7649         }
7650       else
7651         {
7652           val[0] = 0;
7653           val[1] = 0;
7654           for (byte = 0; byte < wchar_bytes; byte++)
7655             {
7656               if (BYTES_BIG_ENDIAN)
7657                 bitpos = (wchar_bytes - byte - 1) * charwidth;
7658               else
7659                 bitpos = byte * charwidth;
7660               val[bitpos < HOST_BITS_PER_WIDE_INT]
7661                 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7662                    << (bitpos % HOST_BITS_PER_WIDE_INT);
7663             }
7664         }
7665
7666       if (!TYPE_UNSIGNED (type))
7667         {
7668           bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7669           if (bitpos < HOST_BITS_PER_WIDE_INT)
7670             {
7671               if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7672                 {
7673                   val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7674                   val[0] = -1;
7675                 }
7676             }
7677           else if (bitpos == HOST_BITS_PER_WIDE_INT)
7678             {
7679               if (val[1] < 0)
7680                 val[0] = -1;
7681             }
7682           else if (val[0] & (((HOST_WIDE_INT) 1)
7683                              << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7684             val[0] |= ((HOST_WIDE_INT) -1)
7685                       << (bitpos - HOST_BITS_PER_WIDE_INT);
7686         }
7687
7688       value = build_int_cst_wide (type, val[1], val[0]);
7689       add_pending_init (purpose, value, NULL_TREE, false,
7690                         braced_init_obstack);
7691     }
7692
7693   constructor_incremental = 0;
7694 }
7695
7696 /* Return value of FIELD in pending initializer or zero if the field was
7697    not initialized yet.  */
7698
7699 static tree
7700 find_init_member (tree field, struct obstack * braced_init_obstack)
7701 {
7702   struct init_node *p;
7703
7704   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7705     {
7706       if (constructor_incremental
7707           && tree_int_cst_lt (field, constructor_unfilled_index))
7708         set_nonincremental_init (braced_init_obstack);
7709
7710       p = constructor_pending_elts;
7711       while (p)
7712         {
7713           if (tree_int_cst_lt (field, p->purpose))
7714             p = p->left;
7715           else if (tree_int_cst_lt (p->purpose, field))
7716             p = p->right;
7717           else
7718             return p->value;
7719         }
7720     }
7721   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7722     {
7723       tree bitpos = bit_position (field);
7724
7725       if (constructor_incremental
7726           && (!constructor_unfilled_fields
7727               || tree_int_cst_lt (bitpos,
7728                                   bit_position (constructor_unfilled_fields))))
7729         set_nonincremental_init (braced_init_obstack);
7730
7731       p = constructor_pending_elts;
7732       while (p)
7733         {
7734           if (field == p->purpose)
7735             return p->value;
7736           else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7737             p = p->left;
7738           else
7739             p = p->right;
7740         }
7741     }
7742   else if (TREE_CODE (constructor_type) == UNION_TYPE)
7743     {
7744       if (!VEC_empty (constructor_elt, constructor_elements)
7745           && (VEC_last (constructor_elt, constructor_elements)->index
7746               == field))
7747         return VEC_last (constructor_elt, constructor_elements)->value;
7748     }
7749   return 0;
7750 }
7751
7752 /* "Output" the next constructor element.
7753    At top level, really output it to assembler code now.
7754    Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7755    If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7756    TYPE is the data type that the containing data type wants here.
7757    FIELD is the field (a FIELD_DECL) or the index that this element fills.
7758    If VALUE is a string constant, STRICT_STRING is true if it is
7759    unparenthesized or we should not warn here for it being parenthesized.
7760    For other types of VALUE, STRICT_STRING is not used.
7761
7762    PENDING if non-nil means output pending elements that belong
7763    right after this element.  (PENDING is normally 1;
7764    it is 0 while outputting pending elements, to avoid recursion.)
7765
7766    IMPLICIT is true if value comes from pop_init_level (1),
7767    the new initializer has been merged with the existing one
7768    and thus no warnings should be emitted about overriding an
7769    existing initializer.  */
7770
7771 static void
7772 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7773                      tree field, int pending, bool implicit,
7774                      struct obstack * braced_init_obstack)
7775 {
7776   tree semantic_type = NULL_TREE;
7777   constructor_elt *celt;
7778   bool maybe_const = true;
7779   bool npc;
7780
7781   if (type == error_mark_node || value == error_mark_node)
7782     {
7783       constructor_erroneous = 1;
7784       return;
7785     }
7786   if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7787       && (TREE_CODE (value) == STRING_CST
7788           || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7789       && !(TREE_CODE (value) == STRING_CST
7790            && TREE_CODE (type) == ARRAY_TYPE
7791            && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7792       && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7793                      TYPE_MAIN_VARIANT (type)))
7794     value = array_to_pointer_conversion (input_location, value);
7795
7796   if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7797       && require_constant_value && !flag_isoc99 && pending)
7798     {
7799       /* As an extension, allow initializing objects with static storage
7800          duration with compound literals (which are then treated just as
7801          the brace enclosed list they contain).  */
7802       tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7803       value = DECL_INITIAL (decl);
7804     }
7805
7806   npc = null_pointer_constant_p (value);
7807   if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7808     {
7809       semantic_type = TREE_TYPE (value);
7810       value = TREE_OPERAND (value, 0);
7811     }
7812   value = c_fully_fold (value, require_constant_value, &maybe_const);
7813
7814   if (value == error_mark_node)
7815     constructor_erroneous = 1;
7816   else if (!TREE_CONSTANT (value))
7817     constructor_constant = 0;
7818   else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7819            || ((TREE_CODE (constructor_type) == RECORD_TYPE
7820                 || TREE_CODE (constructor_type) == UNION_TYPE)
7821                && DECL_C_BIT_FIELD (field)
7822                && TREE_CODE (value) != INTEGER_CST))
7823     constructor_simple = 0;
7824   if (!maybe_const)
7825     constructor_nonconst = 1;
7826
7827   if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7828     {
7829       if (require_constant_value)
7830         {
7831           error_init ("initializer element is not constant");
7832           value = error_mark_node;
7833         }
7834       else if (require_constant_elements)
7835         pedwarn (input_location, 0,
7836                  "initializer element is not computable at load time");
7837     }
7838   else if (!maybe_const
7839            && (require_constant_value || require_constant_elements))
7840     pedwarn_init (input_location, 0,
7841                   "initializer element is not a constant expression");
7842
7843   /* Issue -Wc++-compat warnings about initializing a bitfield with
7844      enum type.  */
7845   if (warn_cxx_compat
7846       && field != NULL_TREE
7847       && TREE_CODE (field) == FIELD_DECL
7848       && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7849       && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7850           != TYPE_MAIN_VARIANT (type))
7851       && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7852     {
7853       tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7854       if (checktype != error_mark_node
7855           && (TYPE_MAIN_VARIANT (checktype)
7856               != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7857         warning_init (OPT_Wc___compat,
7858                       "enum conversion in initialization is invalid in C++");
7859     }
7860
7861   /* If this field is empty (and not at the end of structure),
7862      don't do anything other than checking the initializer.  */
7863   if (field
7864       && (TREE_TYPE (field) == error_mark_node
7865           || (COMPLETE_TYPE_P (TREE_TYPE (field))
7866               && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7867               && (TREE_CODE (constructor_type) == ARRAY_TYPE
7868                   || DECL_CHAIN (field)))))
7869     return;
7870
7871   if (semantic_type)
7872     value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7873   value = digest_init (input_location, type, value, origtype, npc,
7874                        strict_string, require_constant_value);
7875   if (value == error_mark_node)
7876     {
7877       constructor_erroneous = 1;
7878       return;
7879     }
7880   if (require_constant_value || require_constant_elements)
7881     constant_expression_warning (value);
7882
7883   /* If this element doesn't come next in sequence,
7884      put it on constructor_pending_elts.  */
7885   if (TREE_CODE (constructor_type) == ARRAY_TYPE
7886       && (!constructor_incremental
7887           || !tree_int_cst_equal (field, constructor_unfilled_index)))
7888     {
7889       if (constructor_incremental
7890           && tree_int_cst_lt (field, constructor_unfilled_index))
7891         set_nonincremental_init (braced_init_obstack);
7892
7893       add_pending_init (field, value, origtype, implicit,
7894                         braced_init_obstack);
7895       return;
7896     }
7897   else if (TREE_CODE (constructor_type) == RECORD_TYPE
7898            && (!constructor_incremental
7899                || field != constructor_unfilled_fields))
7900     {
7901       /* We do this for records but not for unions.  In a union,
7902          no matter which field is specified, it can be initialized
7903          right away since it starts at the beginning of the union.  */
7904       if (constructor_incremental)
7905         {
7906           if (!constructor_unfilled_fields)
7907             set_nonincremental_init (braced_init_obstack);
7908           else
7909             {
7910               tree bitpos, unfillpos;
7911
7912               bitpos = bit_position (field);
7913               unfillpos = bit_position (constructor_unfilled_fields);
7914
7915               if (tree_int_cst_lt (bitpos, unfillpos))
7916                 set_nonincremental_init (braced_init_obstack);
7917             }
7918         }
7919
7920       add_pending_init (field, value, origtype, implicit,
7921                         braced_init_obstack);
7922       return;
7923     }
7924   else if (TREE_CODE (constructor_type) == UNION_TYPE
7925            && !VEC_empty (constructor_elt, constructor_elements))
7926     {
7927       if (!implicit)
7928         {
7929           if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7930                                            constructor_elements)->value))
7931             warning_init (0,
7932                           "initialized field with side-effects overwritten");
7933           else if (warn_override_init)
7934             warning_init (OPT_Woverride_init, "initialized field overwritten");
7935         }
7936
7937       /* We can have just one union field set.  */
7938       constructor_elements = 0;
7939     }
7940
7941   /* Otherwise, output this element either to
7942      constructor_elements or to the assembler file.  */
7943
7944   celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7945   celt->index = field;
7946   celt->value = value;
7947
7948   /* Advance the variable that indicates sequential elements output.  */
7949   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7950     constructor_unfilled_index
7951       = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7952                         bitsize_one_node);
7953   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7954     {
7955       constructor_unfilled_fields
7956         = DECL_CHAIN (constructor_unfilled_fields);
7957
7958       /* Skip any nameless bit fields.  */
7959       while (constructor_unfilled_fields != 0
7960              && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7961              && DECL_NAME (constructor_unfilled_fields) == 0)
7962         constructor_unfilled_fields =
7963           DECL_CHAIN (constructor_unfilled_fields);
7964     }
7965   else if (TREE_CODE (constructor_type) == UNION_TYPE)
7966     constructor_unfilled_fields = 0;
7967
7968   /* Now output any pending elements which have become next.  */
7969   if (pending)
7970     output_pending_init_elements (0, braced_init_obstack);
7971 }
7972
7973 /* Output any pending elements which have become next.
7974    As we output elements, constructor_unfilled_{fields,index}
7975    advances, which may cause other elements to become next;
7976    if so, they too are output.
7977
7978    If ALL is 0, we return when there are
7979    no more pending elements to output now.
7980
7981    If ALL is 1, we output space as necessary so that
7982    we can output all the pending elements.  */
7983 static void
7984 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7985 {
7986   struct init_node *elt = constructor_pending_elts;
7987   tree next;
7988
7989  retry:
7990
7991   /* Look through the whole pending tree.
7992      If we find an element that should be output now,
7993      output it.  Otherwise, set NEXT to the element
7994      that comes first among those still pending.  */
7995
7996   next = 0;
7997   while (elt)
7998     {
7999       if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8000         {
8001           if (tree_int_cst_equal (elt->purpose,
8002                                   constructor_unfilled_index))
8003             output_init_element (elt->value, elt->origtype, true,
8004                                  TREE_TYPE (constructor_type),
8005                                  constructor_unfilled_index, 0, false,
8006                                  braced_init_obstack);
8007           else if (tree_int_cst_lt (constructor_unfilled_index,
8008                                     elt->purpose))
8009             {
8010               /* Advance to the next smaller node.  */
8011               if (elt->left)
8012                 elt = elt->left;
8013               else
8014                 {
8015                   /* We have reached the smallest node bigger than the
8016                      current unfilled index.  Fill the space first.  */
8017                   next = elt->purpose;
8018                   break;
8019                 }
8020             }
8021           else
8022             {
8023               /* Advance to the next bigger node.  */
8024               if (elt->right)
8025                 elt = elt->right;
8026               else
8027                 {
8028                   /* We have reached the biggest node in a subtree.  Find
8029                      the parent of it, which is the next bigger node.  */
8030                   while (elt->parent && elt->parent->right == elt)
8031                     elt = elt->parent;
8032                   elt = elt->parent;
8033                   if (elt && tree_int_cst_lt (constructor_unfilled_index,
8034                                               elt->purpose))
8035                     {
8036                       next = elt->purpose;
8037                       break;
8038                     }
8039                 }
8040             }
8041         }
8042       else if (TREE_CODE (constructor_type) == RECORD_TYPE
8043                || TREE_CODE (constructor_type) == UNION_TYPE)
8044         {
8045           tree ctor_unfilled_bitpos, elt_bitpos;
8046
8047           /* If the current record is complete we are done.  */
8048           if (constructor_unfilled_fields == 0)
8049             break;
8050
8051           ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8052           elt_bitpos = bit_position (elt->purpose);
8053           /* We can't compare fields here because there might be empty
8054              fields in between.  */
8055           if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8056             {
8057               constructor_unfilled_fields = elt->purpose;
8058               output_init_element (elt->value, elt->origtype, true,
8059                                    TREE_TYPE (elt->purpose),
8060                                    elt->purpose, 0, false,
8061                                    braced_init_obstack);
8062             }
8063           else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8064             {
8065               /* Advance to the next smaller node.  */
8066               if (elt->left)
8067                 elt = elt->left;
8068               else
8069                 {
8070                   /* We have reached the smallest node bigger than the
8071                      current unfilled field.  Fill the space first.  */
8072                   next = elt->purpose;
8073                   break;
8074                 }
8075             }
8076           else
8077             {
8078               /* Advance to the next bigger node.  */
8079               if (elt->right)
8080                 elt = elt->right;
8081               else
8082                 {
8083                   /* We have reached the biggest node in a subtree.  Find
8084                      the parent of it, which is the next bigger node.  */
8085                   while (elt->parent && elt->parent->right == elt)
8086                     elt = elt->parent;
8087                   elt = elt->parent;
8088                   if (elt
8089                       && (tree_int_cst_lt (ctor_unfilled_bitpos,
8090                                            bit_position (elt->purpose))))
8091                     {
8092                       next = elt->purpose;
8093                       break;
8094                     }
8095                 }
8096             }
8097         }
8098     }
8099
8100   /* Ordinarily return, but not if we want to output all
8101      and there are elements left.  */
8102   if (!(all && next != 0))
8103     return;
8104
8105   /* If it's not incremental, just skip over the gap, so that after
8106      jumping to retry we will output the next successive element.  */
8107   if (TREE_CODE (constructor_type) == RECORD_TYPE
8108       || TREE_CODE (constructor_type) == UNION_TYPE)
8109     constructor_unfilled_fields = next;
8110   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8111     constructor_unfilled_index = next;
8112
8113   /* ELT now points to the node in the pending tree with the next
8114      initializer to output.  */
8115   goto retry;
8116 }
8117 \f
8118 /* Add one non-braced element to the current constructor level.
8119    This adjusts the current position within the constructor's type.
8120    This may also start or terminate implicit levels
8121    to handle a partly-braced initializer.
8122
8123    Once this has found the correct level for the new element,
8124    it calls output_init_element.
8125
8126    IMPLICIT is true if value comes from pop_init_level (1),
8127    the new initializer has been merged with the existing one
8128    and thus no warnings should be emitted about overriding an
8129    existing initializer.  */
8130
8131 void
8132 process_init_element (struct c_expr value, bool implicit,
8133                       struct obstack * braced_init_obstack)
8134 {
8135   tree orig_value = value.value;
8136   int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8137   bool strict_string = value.original_code == STRING_CST;
8138
8139   designator_depth = 0;
8140   designator_erroneous = 0;
8141
8142   /* Handle superfluous braces around string cst as in
8143      char x[] = {"foo"}; */
8144   if (string_flag
8145       && constructor_type
8146       && TREE_CODE (constructor_type) == ARRAY_TYPE
8147       && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8148       && integer_zerop (constructor_unfilled_index))
8149     {
8150       if (constructor_stack->replacement_value.value)
8151         error_init ("excess elements in char array initializer");
8152       constructor_stack->replacement_value = value;
8153       return;
8154     }
8155
8156   if (constructor_stack->replacement_value.value != 0)
8157     {
8158       error_init ("excess elements in struct initializer");
8159       return;
8160     }
8161
8162   /* Ignore elements of a brace group if it is entirely superfluous
8163      and has already been diagnosed.  */
8164   if (constructor_type == 0)
8165     return;
8166
8167   /* If we've exhausted any levels that didn't have braces,
8168      pop them now.  */
8169   while (constructor_stack->implicit)
8170     {
8171       if ((TREE_CODE (constructor_type) == RECORD_TYPE
8172            || TREE_CODE (constructor_type) == UNION_TYPE)
8173           && constructor_fields == 0)
8174         process_init_element (pop_init_level (1, braced_init_obstack),
8175                               true, braced_init_obstack);
8176       else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8177                 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8178                && (constructor_max_index == 0
8179                    || tree_int_cst_lt (constructor_max_index,
8180                                        constructor_index)))
8181         process_init_element (pop_init_level (1, braced_init_obstack),
8182                               true, braced_init_obstack);
8183       else
8184         break;
8185     }
8186
8187   /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once.  */
8188   if (constructor_range_stack)
8189     {
8190       /* If value is a compound literal and we'll be just using its
8191          content, don't put it into a SAVE_EXPR.  */
8192       if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8193           || !require_constant_value
8194           || flag_isoc99)
8195         {
8196           tree semantic_type = NULL_TREE;
8197           if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8198             {
8199               semantic_type = TREE_TYPE (value.value);
8200               value.value = TREE_OPERAND (value.value, 0);
8201             }
8202           value.value = c_save_expr (value.value);
8203           if (semantic_type)
8204             value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8205                                   value.value);
8206         }
8207     }
8208
8209   while (1)
8210     {
8211       if (TREE_CODE (constructor_type) == RECORD_TYPE)
8212         {
8213           tree fieldtype;
8214           enum tree_code fieldcode;
8215
8216           if (constructor_fields == 0)
8217             {
8218               pedwarn_init (input_location, 0,
8219                             "excess elements in struct initializer");
8220               break;
8221             }
8222
8223           fieldtype = TREE_TYPE (constructor_fields);
8224           if (fieldtype != error_mark_node)
8225             fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8226           fieldcode = TREE_CODE (fieldtype);
8227
8228           /* Error for non-static initialization of a flexible array member.  */
8229           if (fieldcode == ARRAY_TYPE
8230               && !require_constant_value
8231               && TYPE_SIZE (fieldtype) == NULL_TREE
8232               && DECL_CHAIN (constructor_fields) == NULL_TREE)
8233             {
8234               error_init ("non-static initialization of a flexible array member");
8235               break;
8236             }
8237
8238           /* Accept a string constant to initialize a subarray.  */
8239           if (value.value != 0
8240               && fieldcode == ARRAY_TYPE
8241               && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8242               && string_flag)
8243             value.value = orig_value;
8244           /* Otherwise, if we have come to a subaggregate,
8245              and we don't have an element of its type, push into it.  */
8246           else if (value.value != 0
8247                    && value.value != error_mark_node
8248                    && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8249                    && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8250                        || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8251             {
8252               push_init_level (1, braced_init_obstack);
8253               continue;
8254             }
8255
8256           if (value.value)
8257             {
8258               push_member_name (constructor_fields);
8259               output_init_element (value.value, value.original_type,
8260                                    strict_string, fieldtype,
8261                                    constructor_fields, 1, implicit,
8262                                    braced_init_obstack);
8263               RESTORE_SPELLING_DEPTH (constructor_depth);
8264             }
8265           else
8266             /* Do the bookkeeping for an element that was
8267                directly output as a constructor.  */
8268             {
8269               /* For a record, keep track of end position of last field.  */
8270               if (DECL_SIZE (constructor_fields))
8271                 constructor_bit_index
8272                   = size_binop_loc (input_location, PLUS_EXPR,
8273                                     bit_position (constructor_fields),
8274                                     DECL_SIZE (constructor_fields));
8275
8276               /* If the current field was the first one not yet written out,
8277                  it isn't now, so update.  */
8278               if (constructor_unfilled_fields == constructor_fields)
8279                 {
8280                   constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8281                   /* Skip any nameless bit fields.  */
8282                   while (constructor_unfilled_fields != 0
8283                          && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8284                          && DECL_NAME (constructor_unfilled_fields) == 0)
8285                     constructor_unfilled_fields =
8286                       DECL_CHAIN (constructor_unfilled_fields);
8287                 }
8288             }
8289
8290           constructor_fields = DECL_CHAIN (constructor_fields);
8291           /* Skip any nameless bit fields at the beginning.  */
8292           while (constructor_fields != 0
8293                  && DECL_C_BIT_FIELD (constructor_fields)
8294                  && DECL_NAME (constructor_fields) == 0)
8295             constructor_fields = DECL_CHAIN (constructor_fields);
8296         }
8297       else if (TREE_CODE (constructor_type) == UNION_TYPE)
8298         {
8299           tree fieldtype;
8300           enum tree_code fieldcode;
8301
8302           if (constructor_fields == 0)
8303             {
8304               pedwarn_init (input_location, 0,
8305                             "excess elements in union initializer");
8306               break;
8307             }
8308
8309           fieldtype = TREE_TYPE (constructor_fields);
8310           if (fieldtype != error_mark_node)
8311             fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8312           fieldcode = TREE_CODE (fieldtype);
8313
8314           /* Warn that traditional C rejects initialization of unions.
8315              We skip the warning if the value is zero.  This is done
8316              under the assumption that the zero initializer in user
8317              code appears conditioned on e.g. __STDC__ to avoid
8318              "missing initializer" warnings and relies on default
8319              initialization to zero in the traditional C case.
8320              We also skip the warning if the initializer is designated,
8321              again on the assumption that this must be conditional on
8322              __STDC__ anyway (and we've already complained about the
8323              member-designator already).  */
8324           if (!in_system_header && !constructor_designated
8325               && !(value.value && (integer_zerop (value.value)
8326                                    || real_zerop (value.value))))
8327             warning (OPT_Wtraditional, "traditional C rejects initialization "
8328                      "of unions");
8329
8330           /* Accept a string constant to initialize a subarray.  */
8331           if (value.value != 0
8332               && fieldcode == ARRAY_TYPE
8333               && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8334               && string_flag)
8335             value.value = orig_value;
8336           /* Otherwise, if we have come to a subaggregate,
8337              and we don't have an element of its type, push into it.  */
8338           else if (value.value != 0
8339                    && value.value != error_mark_node
8340                    && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8341                    && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8342                        || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8343             {
8344               push_init_level (1, braced_init_obstack);
8345               continue;
8346             }
8347
8348           if (value.value)
8349             {
8350               push_member_name (constructor_fields);
8351               output_init_element (value.value, value.original_type,
8352                                    strict_string, fieldtype,
8353                                    constructor_fields, 1, implicit,
8354                                    braced_init_obstack);
8355               RESTORE_SPELLING_DEPTH (constructor_depth);
8356             }
8357           else
8358             /* Do the bookkeeping for an element that was
8359                directly output as a constructor.  */
8360             {
8361               constructor_bit_index = DECL_SIZE (constructor_fields);
8362               constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8363             }
8364
8365           constructor_fields = 0;
8366         }
8367       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8368         {
8369           tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8370           enum tree_code eltcode = TREE_CODE (elttype);
8371
8372           /* Accept a string constant to initialize a subarray.  */
8373           if (value.value != 0
8374               && eltcode == ARRAY_TYPE
8375               && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8376               && string_flag)
8377             value.value = orig_value;
8378           /* Otherwise, if we have come to a subaggregate,
8379              and we don't have an element of its type, push into it.  */
8380           else if (value.value != 0
8381                    && value.value != error_mark_node
8382                    && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8383                    && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8384                        || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8385             {
8386               push_init_level (1, braced_init_obstack);
8387               continue;
8388             }
8389
8390           if (constructor_max_index != 0
8391               && (tree_int_cst_lt (constructor_max_index, constructor_index)
8392                   || integer_all_onesp (constructor_max_index)))
8393             {
8394               pedwarn_init (input_location, 0,
8395                             "excess elements in array initializer");
8396               break;
8397             }
8398
8399           /* Now output the actual element.  */
8400           if (value.value)
8401             {
8402               push_array_bounds (tree_low_cst (constructor_index, 1));
8403               output_init_element (value.value, value.original_type,
8404                                    strict_string, elttype,
8405                                    constructor_index, 1, implicit,
8406                                    braced_init_obstack);
8407               RESTORE_SPELLING_DEPTH (constructor_depth);
8408             }
8409
8410           constructor_index
8411             = size_binop_loc (input_location, PLUS_EXPR,
8412                               constructor_index, bitsize_one_node);
8413
8414           if (!value.value)
8415             /* If we are doing the bookkeeping for an element that was
8416                directly output as a constructor, we must update
8417                constructor_unfilled_index.  */
8418             constructor_unfilled_index = constructor_index;
8419         }
8420       else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8421         {
8422           tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8423
8424          /* Do a basic check of initializer size.  Note that vectors
8425             always have a fixed size derived from their type.  */
8426           if (tree_int_cst_lt (constructor_max_index, constructor_index))
8427             {
8428               pedwarn_init (input_location, 0,
8429                             "excess elements in vector initializer");
8430               break;
8431             }
8432
8433           /* Now output the actual element.  */
8434           if (value.value)
8435             {
8436               if (TREE_CODE (value.value) == VECTOR_CST)
8437                 elttype = TYPE_MAIN_VARIANT (constructor_type);
8438               output_init_element (value.value, value.original_type,
8439                                    strict_string, elttype,
8440                                    constructor_index, 1, implicit,
8441                                    braced_init_obstack);
8442             }
8443
8444           constructor_index
8445             = size_binop_loc (input_location,
8446                               PLUS_EXPR, constructor_index, bitsize_one_node);
8447
8448           if (!value.value)
8449             /* If we are doing the bookkeeping for an element that was
8450                directly output as a constructor, we must update
8451                constructor_unfilled_index.  */
8452             constructor_unfilled_index = constructor_index;
8453         }
8454
8455       /* Handle the sole element allowed in a braced initializer
8456          for a scalar variable.  */
8457       else if (constructor_type != error_mark_node
8458                && constructor_fields == 0)
8459         {
8460           pedwarn_init (input_location, 0,
8461                         "excess elements in scalar initializer");
8462           break;
8463         }
8464       else
8465         {
8466           if (value.value)
8467             output_init_element (value.value, value.original_type,
8468                                  strict_string, constructor_type,
8469                                  NULL_TREE, 1, implicit,
8470                                  braced_init_obstack);
8471           constructor_fields = 0;
8472         }
8473
8474       /* Handle range initializers either at this level or anywhere higher
8475          in the designator stack.  */
8476       if (constructor_range_stack)
8477         {
8478           struct constructor_range_stack *p, *range_stack;
8479           int finish = 0;
8480
8481           range_stack = constructor_range_stack;
8482           constructor_range_stack = 0;
8483           while (constructor_stack != range_stack->stack)
8484             {
8485               gcc_assert (constructor_stack->implicit);
8486               process_init_element (pop_init_level (1,
8487                                                     braced_init_obstack),
8488                                     true, braced_init_obstack);
8489             }
8490           for (p = range_stack;
8491                !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8492                p = p->prev)
8493             {
8494               gcc_assert (constructor_stack->implicit);
8495               process_init_element (pop_init_level (1, braced_init_obstack),
8496                                     true, braced_init_obstack);
8497             }
8498
8499           p->index = size_binop_loc (input_location,
8500                                      PLUS_EXPR, p->index, bitsize_one_node);
8501           if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8502             finish = 1;
8503
8504           while (1)
8505             {
8506               constructor_index = p->index;
8507               constructor_fields = p->fields;
8508               if (finish && p->range_end && p->index == p->range_start)
8509                 {
8510                   finish = 0;
8511                   p->prev = 0;
8512                 }
8513               p = p->next;
8514               if (!p)
8515                 break;
8516               push_init_level (2, braced_init_obstack);
8517               p->stack = constructor_stack;
8518               if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8519                 p->index = p->range_start;
8520             }
8521
8522           if (!finish)
8523             constructor_range_stack = range_stack;
8524           continue;
8525         }
8526
8527       break;
8528     }
8529
8530   constructor_range_stack = 0;
8531 }
8532 \f
8533 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8534    (guaranteed to be 'volatile' or null) and ARGS (represented using
8535    an ASM_EXPR node).  */
8536 tree
8537 build_asm_stmt (tree cv_qualifier, tree args)
8538 {
8539   if (!ASM_VOLATILE_P (args) && cv_qualifier)
8540     ASM_VOLATILE_P (args) = 1;
8541   return add_stmt (args);
8542 }
8543
8544 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8545    some INPUTS, and some CLOBBERS.  The latter three may be NULL.
8546    SIMPLE indicates whether there was anything at all after the
8547    string in the asm expression -- asm("blah") and asm("blah" : )
8548    are subtly different.  We use a ASM_EXPR node to represent this.  */
8549 tree
8550 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8551                 tree clobbers, tree labels, bool simple)
8552 {
8553   tree tail;
8554   tree args;
8555   int i;
8556   const char *constraint;
8557   const char **oconstraints;
8558   bool allows_mem, allows_reg, is_inout;
8559   int ninputs, noutputs;
8560
8561   ninputs = list_length (inputs);
8562   noutputs = list_length (outputs);
8563   oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8564
8565   string = resolve_asm_operand_names (string, outputs, inputs, labels);
8566
8567   /* Remove output conversions that change the type but not the mode.  */
8568   for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8569     {
8570       tree output = TREE_VALUE (tail);
8571
8572       /* ??? Really, this should not be here.  Users should be using a
8573          proper lvalue, dammit.  But there's a long history of using casts
8574          in the output operands.  In cases like longlong.h, this becomes a
8575          primitive form of typechecking -- if the cast can be removed, then
8576          the output operand had a type of the proper width; otherwise we'll
8577          get an error.  Gross, but ...  */
8578       STRIP_NOPS (output);
8579
8580       if (!lvalue_or_else (loc, output, lv_asm))
8581         output = error_mark_node;
8582
8583       if (output != error_mark_node
8584           && (TREE_READONLY (output)
8585               || TYPE_READONLY (TREE_TYPE (output))
8586               || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8587                    || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8588                   && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8589         readonly_error (output, lv_asm);
8590
8591       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8592       oconstraints[i] = constraint;
8593
8594       if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8595                                    &allows_mem, &allows_reg, &is_inout))
8596         {
8597           /* If the operand is going to end up in memory,
8598              mark it addressable.  */
8599           if (!allows_reg && !c_mark_addressable (output))
8600             output = error_mark_node;
8601           if (!(!allows_reg && allows_mem)
8602               && output != error_mark_node
8603               && VOID_TYPE_P (TREE_TYPE (output)))
8604             {
8605               error_at (loc, "invalid use of void expression");
8606               output = error_mark_node;
8607             }
8608         }
8609       else
8610         output = error_mark_node;
8611
8612       TREE_VALUE (tail) = output;
8613     }
8614
8615   for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8616     {
8617       tree input;
8618
8619       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8620       input = TREE_VALUE (tail);
8621
8622       if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8623                                   oconstraints, &allows_mem, &allows_reg))
8624         {
8625           /* If the operand is going to end up in memory,
8626              mark it addressable.  */
8627           if (!allows_reg && allows_mem)
8628             {
8629               /* Strip the nops as we allow this case.  FIXME, this really
8630                  should be rejected or made deprecated.  */
8631               STRIP_NOPS (input);
8632               if (!c_mark_addressable (input))
8633                 input = error_mark_node;
8634             }
8635           else if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8636             {
8637               error_at (loc, "invalid use of void expression");
8638               input = error_mark_node;
8639             }
8640         }
8641       else
8642         input = error_mark_node;
8643
8644       TREE_VALUE (tail) = input;
8645     }
8646
8647   /* ASMs with labels cannot have outputs.  This should have been
8648      enforced by the parser.  */
8649   gcc_assert (outputs == NULL || labels == NULL);
8650
8651   args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8652
8653   /* asm statements without outputs, including simple ones, are treated
8654      as volatile.  */
8655   ASM_INPUT_P (args) = simple;
8656   ASM_VOLATILE_P (args) = (noutputs == 0);
8657
8658   return args;
8659 }
8660 \f
8661 /* Generate a goto statement to LABEL.  LOC is the location of the
8662    GOTO.  */
8663
8664 tree
8665 c_finish_goto_label (location_t loc, tree label)
8666 {
8667   tree decl = lookup_label_for_goto (loc, label);
8668   if (!decl)
8669     return NULL_TREE;
8670   TREE_USED (decl) = 1;
8671   {
8672     tree t = build1 (GOTO_EXPR, void_type_node, decl);
8673     SET_EXPR_LOCATION (t, loc);
8674     return add_stmt (t);
8675   }
8676 }
8677
8678 /* Generate a computed goto statement to EXPR.  LOC is the location of
8679    the GOTO.  */
8680
8681 tree
8682 c_finish_goto_ptr (location_t loc, tree expr)
8683 {
8684   tree t;
8685   pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8686   expr = c_fully_fold (expr, false, NULL);
8687   expr = convert (ptr_type_node, expr);
8688   t = build1 (GOTO_EXPR, void_type_node, expr);
8689   SET_EXPR_LOCATION (t, loc);
8690   return add_stmt (t);
8691 }
8692
8693 /* Generate a C `return' statement.  RETVAL is the expression for what
8694    to return, or a null pointer for `return;' with no value.  LOC is
8695    the location of the return statement.  If ORIGTYPE is not NULL_TREE, it
8696    is the original type of RETVAL.  */
8697
8698 tree
8699 c_finish_return (location_t loc, tree retval, tree origtype)
8700 {
8701   tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8702   bool no_warning = false;
8703   bool npc = false;
8704
8705   if (TREE_THIS_VOLATILE (current_function_decl))
8706     warning_at (loc, 0,
8707                 "function declared %<noreturn%> has a %<return%> statement");
8708
8709   if (retval)
8710     {
8711       tree semantic_type = NULL_TREE;
8712       npc = null_pointer_constant_p (retval);
8713       if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8714         {
8715           semantic_type = TREE_TYPE (retval);
8716           retval = TREE_OPERAND (retval, 0);
8717         }
8718       retval = c_fully_fold (retval, false, NULL);
8719       if (semantic_type)
8720         retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8721     }
8722
8723   if (!retval)
8724     {
8725       current_function_returns_null = 1;
8726       if ((warn_return_type || flag_isoc99)
8727           && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8728         {
8729           pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8730                        "%<return%> with no value, in "
8731                        "function returning non-void");
8732           no_warning = true;
8733         }
8734     }
8735   else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8736     {
8737       current_function_returns_null = 1;
8738       if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8739         pedwarn (loc, 0,
8740                  "%<return%> with a value, in function returning void");
8741       else
8742         pedwarn (loc, OPT_pedantic, "ISO C forbids "
8743                  "%<return%> with expression, in function returning void");
8744     }
8745   else
8746     {
8747       tree t = convert_for_assignment (loc, valtype, retval, origtype,
8748                                        ic_return,
8749                                        npc, NULL_TREE, NULL_TREE, 0);
8750       tree res = DECL_RESULT (current_function_decl);
8751       tree inner;
8752
8753       current_function_returns_value = 1;
8754       if (t == error_mark_node)
8755         return NULL_TREE;
8756
8757       inner = t = convert (TREE_TYPE (res), t);
8758
8759       /* Strip any conversions, additions, and subtractions, and see if
8760          we are returning the address of a local variable.  Warn if so.  */
8761       while (1)
8762         {
8763           switch (TREE_CODE (inner))
8764             {
8765             CASE_CONVERT:
8766             case NON_LVALUE_EXPR:
8767             case PLUS_EXPR:
8768             case POINTER_PLUS_EXPR:
8769               inner = TREE_OPERAND (inner, 0);
8770               continue;
8771
8772             case MINUS_EXPR:
8773               /* If the second operand of the MINUS_EXPR has a pointer
8774                  type (or is converted from it), this may be valid, so
8775                  don't give a warning.  */
8776               {
8777                 tree op1 = TREE_OPERAND (inner, 1);
8778
8779                 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8780                        && (CONVERT_EXPR_P (op1)
8781                            || TREE_CODE (op1) == NON_LVALUE_EXPR))
8782                   op1 = TREE_OPERAND (op1, 0);
8783
8784                 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8785                   break;
8786
8787                 inner = TREE_OPERAND (inner, 0);
8788                 continue;
8789               }
8790
8791             case ADDR_EXPR:
8792               inner = TREE_OPERAND (inner, 0);
8793
8794               while (REFERENCE_CLASS_P (inner)
8795                      && TREE_CODE (inner) != INDIRECT_REF)
8796                 inner = TREE_OPERAND (inner, 0);
8797
8798               if (DECL_P (inner)
8799                   && !DECL_EXTERNAL (inner)
8800                   && !TREE_STATIC (inner)
8801                   && DECL_CONTEXT (inner) == current_function_decl)
8802                 warning_at (loc,
8803                             0, "function returns address of local variable");
8804               break;
8805
8806             default:
8807               break;
8808             }
8809
8810           break;
8811         }
8812
8813       retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8814       SET_EXPR_LOCATION (retval, loc);
8815
8816       if (warn_sequence_point)
8817         verify_sequence_points (retval);
8818     }
8819
8820   ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8821   TREE_NO_WARNING (ret_stmt) |= no_warning;
8822   return add_stmt (ret_stmt);
8823 }
8824 \f
8825 struct c_switch {
8826   /* The SWITCH_EXPR being built.  */
8827   tree switch_expr;
8828
8829   /* The original type of the testing expression, i.e. before the
8830      default conversion is applied.  */
8831   tree orig_type;
8832
8833   /* A splay-tree mapping the low element of a case range to the high
8834      element, or NULL_TREE if there is no high element.  Used to
8835      determine whether or not a new case label duplicates an old case
8836      label.  We need a tree, rather than simply a hash table, because
8837      of the GNU case range extension.  */
8838   splay_tree cases;
8839
8840   /* The bindings at the point of the switch.  This is used for
8841      warnings crossing decls when branching to a case label.  */
8842   struct c_spot_bindings *bindings;
8843
8844   /* The next node on the stack.  */
8845   struct c_switch *next;
8846 };
8847
8848 /* A stack of the currently active switch statements.  The innermost
8849    switch statement is on the top of the stack.  There is no need to
8850    mark the stack for garbage collection because it is only active
8851    during the processing of the body of a function, and we never
8852    collect at that point.  */
8853
8854 struct c_switch *c_switch_stack;
8855
8856 /* Start a C switch statement, testing expression EXP.  Return the new
8857    SWITCH_EXPR.  SWITCH_LOC is the location of the `switch'.
8858    SWITCH_COND_LOC is the location of the switch's condition.  */
8859
8860 tree
8861 c_start_case (location_t switch_loc,
8862               location_t switch_cond_loc,
8863               tree exp)
8864 {
8865   tree orig_type = error_mark_node;
8866   struct c_switch *cs;
8867
8868   if (exp != error_mark_node)
8869     {
8870       orig_type = TREE_TYPE (exp);
8871
8872       if (!INTEGRAL_TYPE_P (orig_type))
8873         {
8874           if (orig_type != error_mark_node)
8875             {
8876               error_at (switch_cond_loc, "switch quantity not an integer");
8877               orig_type = error_mark_node;
8878             }
8879           exp = integer_zero_node;
8880         }
8881       else
8882         {
8883           tree type = TYPE_MAIN_VARIANT (orig_type);
8884
8885           if (!in_system_header
8886               && (type == long_integer_type_node
8887                   || type == long_unsigned_type_node))
8888             warning_at (switch_cond_loc,
8889                         OPT_Wtraditional, "%<long%> switch expression not "
8890                         "converted to %<int%> in ISO C");
8891
8892           exp = c_fully_fold (exp, false, NULL);
8893           exp = default_conversion (exp);
8894
8895           if (warn_sequence_point)
8896             verify_sequence_points (exp);
8897         }
8898     }
8899
8900   /* Add this new SWITCH_EXPR to the stack.  */
8901   cs = XNEW (struct c_switch);
8902   cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8903   SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8904   cs->orig_type = orig_type;
8905   cs->cases = splay_tree_new (case_compare, NULL, NULL);
8906   cs->bindings = c_get_switch_bindings ();
8907   cs->next = c_switch_stack;
8908   c_switch_stack = cs;
8909
8910   return add_stmt (cs->switch_expr);
8911 }
8912
8913 /* Process a case label at location LOC.  */
8914
8915 tree
8916 do_case (location_t loc, tree low_value, tree high_value)
8917 {
8918   tree label = NULL_TREE;
8919
8920   if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8921     {
8922       low_value = c_fully_fold (low_value, false, NULL);
8923       if (TREE_CODE (low_value) == INTEGER_CST)
8924         pedwarn (input_location, OPT_pedantic,
8925                  "case label is not an integer constant expression");
8926     }
8927
8928   if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8929     {
8930       high_value = c_fully_fold (high_value, false, NULL);
8931       if (TREE_CODE (high_value) == INTEGER_CST)
8932         pedwarn (input_location, OPT_pedantic,
8933                  "case label is not an integer constant expression");
8934     }
8935
8936   if (c_switch_stack == NULL)
8937     {
8938       if (low_value)
8939         error_at (loc, "case label not within a switch statement");
8940       else
8941         error_at (loc, "%<default%> label not within a switch statement");
8942       return NULL_TREE;
8943     }
8944
8945   if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8946                                     EXPR_LOCATION (c_switch_stack->switch_expr),
8947                                     loc))
8948     return NULL_TREE;
8949
8950   label = c_add_case_label (loc, c_switch_stack->cases,
8951                             SWITCH_COND (c_switch_stack->switch_expr),
8952                             c_switch_stack->orig_type,
8953                             low_value, high_value);
8954   if (label == error_mark_node)
8955     label = NULL_TREE;
8956   return label;
8957 }
8958
8959 /* Finish the switch statement.  */
8960
8961 void
8962 c_finish_case (tree body)
8963 {
8964   struct c_switch *cs = c_switch_stack;
8965   location_t switch_location;
8966
8967   SWITCH_BODY (cs->switch_expr) = body;
8968
8969   /* Emit warnings as needed.  */
8970   switch_location = EXPR_LOCATION (cs->switch_expr);
8971   c_do_switch_warnings (cs->cases, switch_location,
8972                         TREE_TYPE (cs->switch_expr),
8973                         SWITCH_COND (cs->switch_expr));
8974
8975   /* Pop the stack.  */
8976   c_switch_stack = cs->next;
8977   splay_tree_delete (cs->cases);
8978   c_release_switch_bindings (cs->bindings);
8979   XDELETE (cs);
8980 }
8981 \f
8982 /* Emit an if statement.  IF_LOCUS is the location of the 'if'.  COND,
8983    THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8984    may be null.  NESTED_IF is true if THEN_BLOCK contains another IF
8985    statement, and was not surrounded with parenthesis.  */
8986
8987 void
8988 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8989                   tree else_block, bool nested_if)
8990 {
8991   tree stmt;
8992
8993   /* Diagnose an ambiguous else if if-then-else is nested inside if-then.  */
8994   if (warn_parentheses && nested_if && else_block == NULL)
8995     {
8996       tree inner_if = then_block;
8997
8998       /* We know from the grammar productions that there is an IF nested
8999          within THEN_BLOCK.  Due to labels and c99 conditional declarations,
9000          it might not be exactly THEN_BLOCK, but should be the last
9001          non-container statement within.  */
9002       while (1)
9003         switch (TREE_CODE (inner_if))
9004           {
9005           case COND_EXPR:
9006             goto found;
9007           case BIND_EXPR:
9008             inner_if = BIND_EXPR_BODY (inner_if);
9009             break;
9010           case STATEMENT_LIST:
9011             inner_if = expr_last (then_block);
9012             break;
9013           case TRY_FINALLY_EXPR:
9014           case TRY_CATCH_EXPR:
9015             inner_if = TREE_OPERAND (inner_if, 0);
9016             break;
9017           default:
9018             gcc_unreachable ();
9019           }
9020     found:
9021
9022       if (COND_EXPR_ELSE (inner_if))
9023          warning_at (if_locus, OPT_Wparentheses,
9024                      "suggest explicit braces to avoid ambiguous %<else%>");
9025     }
9026
9027   stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9028   SET_EXPR_LOCATION (stmt, if_locus);
9029   add_stmt (stmt);
9030 }
9031
9032 /* Emit a general-purpose loop construct.  START_LOCUS is the location of
9033    the beginning of the loop.  COND is the loop condition.  COND_IS_FIRST
9034    is false for DO loops.  INCR is the FOR increment expression.  BODY is
9035    the statement controlled by the loop.  BLAB is the break label.  CLAB is
9036    the continue label.  Everything is allowed to be NULL.  */
9037
9038 void
9039 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9040                tree blab, tree clab, bool cond_is_first)
9041 {
9042   tree entry = NULL, exit = NULL, t;
9043
9044   /* If the condition is zero don't generate a loop construct.  */
9045   if (cond && integer_zerop (cond))
9046     {
9047       if (cond_is_first)
9048         {
9049           t = build_and_jump (&blab);
9050           SET_EXPR_LOCATION (t, start_locus);
9051           add_stmt (t);
9052         }
9053     }
9054   else
9055     {
9056       tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9057
9058       /* If we have an exit condition, then we build an IF with gotos either
9059          out of the loop, or to the top of it.  If there's no exit condition,
9060          then we just build a jump back to the top.  */
9061       exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9062
9063       if (cond && !integer_nonzerop (cond))
9064         {
9065           /* Canonicalize the loop condition to the end.  This means
9066              generating a branch to the loop condition.  Reuse the
9067              continue label, if possible.  */
9068           if (cond_is_first)
9069             {
9070               if (incr || !clab)
9071                 {
9072                   entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9073                   t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9074                 }
9075               else
9076                 t = build1 (GOTO_EXPR, void_type_node, clab);
9077               SET_EXPR_LOCATION (t, start_locus);
9078               add_stmt (t);
9079             }
9080
9081           t = build_and_jump (&blab);
9082           if (cond_is_first)
9083             exit = fold_build3_loc (start_locus,
9084                                 COND_EXPR, void_type_node, cond, exit, t);
9085           else
9086             exit = fold_build3_loc (input_location,
9087                                 COND_EXPR, void_type_node, cond, exit, t);
9088         }
9089
9090       add_stmt (top);
9091     }
9092
9093   if (body)
9094     add_stmt (body);
9095   if (clab)
9096     add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9097   if (incr)
9098     add_stmt (incr);
9099   if (entry)
9100     add_stmt (entry);
9101   if (exit)
9102     add_stmt (exit);
9103   if (blab)
9104     add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9105 }
9106
9107 tree
9108 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9109 {
9110   bool skip;
9111   tree label = *label_p;
9112
9113   /* In switch statements break is sometimes stylistically used after
9114      a return statement.  This can lead to spurious warnings about
9115      control reaching the end of a non-void function when it is
9116      inlined.  Note that we are calling block_may_fallthru with
9117      language specific tree nodes; this works because
9118      block_may_fallthru returns true when given something it does not
9119      understand.  */
9120   skip = !block_may_fallthru (cur_stmt_list);
9121
9122   if (!label)
9123     {
9124       if (!skip)
9125         *label_p = label = create_artificial_label (loc);
9126     }
9127   else if (TREE_CODE (label) == LABEL_DECL)
9128     ;
9129   else switch (TREE_INT_CST_LOW (label))
9130     {
9131     case 0:
9132       if (is_break)
9133         error_at (loc, "break statement not within loop or switch");
9134       else
9135         error_at (loc, "continue statement not within a loop");
9136       return NULL_TREE;
9137
9138     case 1:
9139       gcc_assert (is_break);
9140       error_at (loc, "break statement used with OpenMP for loop");
9141       return NULL_TREE;
9142
9143     default:
9144       gcc_unreachable ();
9145     }
9146
9147   if (skip)
9148     return NULL_TREE;
9149
9150   if (!is_break)
9151     add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9152
9153   return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9154 }
9155
9156 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr.  */
9157
9158 static void
9159 emit_side_effect_warnings (location_t loc, tree expr)
9160 {
9161   if (expr == error_mark_node)
9162     ;
9163   else if (!TREE_SIDE_EFFECTS (expr))
9164     {
9165       if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9166         warning_at (loc, OPT_Wunused_value, "statement with no effect");
9167     }
9168   else
9169     warn_if_unused_value (expr, loc);
9170 }
9171
9172 /* Process an expression as if it were a complete statement.  Emit
9173    diagnostics, but do not call ADD_STMT.  LOC is the location of the
9174    statement.  */
9175
9176 tree
9177 c_process_expr_stmt (location_t loc, tree expr)
9178 {
9179   tree exprv;
9180
9181   if (!expr)
9182     return NULL_TREE;
9183
9184   expr = c_fully_fold (expr, false, NULL);
9185
9186   if (warn_sequence_point)
9187     verify_sequence_points (expr);
9188
9189   if (TREE_TYPE (expr) != error_mark_node
9190       && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9191       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9192     error_at (loc, "expression statement has incomplete type");
9193
9194   /* If we're not processing a statement expression, warn about unused values.
9195      Warnings for statement expressions will be emitted later, once we figure
9196      out which is the result.  */
9197   if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9198       && warn_unused_value)
9199     emit_side_effect_warnings (loc, expr);
9200
9201   exprv = expr;
9202   while (TREE_CODE (exprv) == COMPOUND_EXPR)
9203     exprv = TREE_OPERAND (exprv, 1);
9204   while (CONVERT_EXPR_P (exprv))
9205     exprv = TREE_OPERAND (exprv, 0);
9206   if (DECL_P (exprv)
9207       || handled_component_p (exprv)
9208       || TREE_CODE (exprv) == ADDR_EXPR)
9209     mark_exp_read (exprv);
9210
9211   /* If the expression is not of a type to which we cannot assign a line
9212      number, wrap the thing in a no-op NOP_EXPR.  */
9213   if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9214     {
9215       expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9216       SET_EXPR_LOCATION (expr, loc);
9217     }
9218
9219   return expr;
9220 }
9221
9222 /* Emit an expression as a statement.  LOC is the location of the
9223    expression.  */
9224
9225 tree
9226 c_finish_expr_stmt (location_t loc, tree expr)
9227 {
9228   if (expr)
9229     return add_stmt (c_process_expr_stmt (loc, expr));
9230   else
9231     return NULL;
9232 }
9233
9234 /* Do the opposite and emit a statement as an expression.  To begin,
9235    create a new binding level and return it.  */
9236
9237 tree
9238 c_begin_stmt_expr (void)
9239 {
9240   tree ret;
9241
9242   /* We must force a BLOCK for this level so that, if it is not expanded
9243      later, there is a way to turn off the entire subtree of blocks that
9244      are contained in it.  */
9245   keep_next_level ();
9246   ret = c_begin_compound_stmt (true);
9247
9248   c_bindings_start_stmt_expr (c_switch_stack == NULL
9249                               ? NULL
9250                               : c_switch_stack->bindings);
9251
9252   /* Mark the current statement list as belonging to a statement list.  */
9253   STATEMENT_LIST_STMT_EXPR (ret) = 1;
9254
9255   return ret;
9256 }
9257
9258 /* LOC is the location of the compound statement to which this body
9259    belongs.  */
9260
9261 tree
9262 c_finish_stmt_expr (location_t loc, tree body)
9263 {
9264   tree last, type, tmp, val;
9265   tree *last_p;
9266
9267   body = c_end_compound_stmt (loc, body, true);
9268
9269   c_bindings_end_stmt_expr (c_switch_stack == NULL
9270                             ? NULL
9271                             : c_switch_stack->bindings);
9272
9273   /* Locate the last statement in BODY.  See c_end_compound_stmt
9274      about always returning a BIND_EXPR.  */
9275   last_p = &BIND_EXPR_BODY (body);
9276   last = BIND_EXPR_BODY (body);
9277
9278  continue_searching:
9279   if (TREE_CODE (last) == STATEMENT_LIST)
9280     {
9281       tree_stmt_iterator i;
9282
9283       /* This can happen with degenerate cases like ({ }).  No value.  */
9284       if (!TREE_SIDE_EFFECTS (last))
9285         return body;
9286
9287       /* If we're supposed to generate side effects warnings, process
9288          all of the statements except the last.  */
9289       if (warn_unused_value)
9290         {
9291           for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9292             {
9293               location_t tloc;
9294               tree t = tsi_stmt (i);
9295
9296               tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9297               emit_side_effect_warnings (tloc, t);
9298             }
9299         }
9300       else
9301         i = tsi_last (last);
9302       last_p = tsi_stmt_ptr (i);
9303       last = *last_p;
9304     }
9305
9306   /* If the end of the list is exception related, then the list was split
9307      by a call to push_cleanup.  Continue searching.  */
9308   if (TREE_CODE (last) == TRY_FINALLY_EXPR
9309       || TREE_CODE (last) == TRY_CATCH_EXPR)
9310     {
9311       last_p = &TREE_OPERAND (last, 0);
9312       last = *last_p;
9313       goto continue_searching;
9314     }
9315
9316   if (last == error_mark_node)
9317     return last;
9318
9319   /* In the case that the BIND_EXPR is not necessary, return the
9320      expression out from inside it.  */
9321   if (last == BIND_EXPR_BODY (body)
9322       && BIND_EXPR_VARS (body) == NULL)
9323     {
9324       /* Even if this looks constant, do not allow it in a constant
9325          expression.  */
9326       last = c_wrap_maybe_const (last, true);
9327       /* Do not warn if the return value of a statement expression is
9328          unused.  */
9329       TREE_NO_WARNING (last) = 1;
9330       return last;
9331     }
9332
9333   /* Extract the type of said expression.  */
9334   type = TREE_TYPE (last);
9335
9336   /* If we're not returning a value at all, then the BIND_EXPR that
9337      we already have is a fine expression to return.  */
9338   if (!type || VOID_TYPE_P (type))
9339     return body;
9340
9341   /* Now that we've located the expression containing the value, it seems
9342      silly to make voidify_wrapper_expr repeat the process.  Create a
9343      temporary of the appropriate type and stick it in a TARGET_EXPR.  */
9344   tmp = create_tmp_var_raw (type, NULL);
9345
9346   /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt.  This avoids
9347      tree_expr_nonnegative_p giving up immediately.  */
9348   val = last;
9349   if (TREE_CODE (val) == NOP_EXPR
9350       && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9351     val = TREE_OPERAND (val, 0);
9352
9353   *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9354   SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9355
9356   {
9357     tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9358     SET_EXPR_LOCATION (t, loc);
9359     return t;
9360   }
9361 }
9362 \f
9363 /* Begin and end compound statements.  This is as simple as pushing
9364    and popping new statement lists from the tree.  */
9365
9366 tree
9367 c_begin_compound_stmt (bool do_scope)
9368 {
9369   tree stmt = push_stmt_list ();
9370   if (do_scope)
9371     push_scope ();
9372   return stmt;
9373 }
9374
9375 /* End a compound statement.  STMT is the statement.  LOC is the
9376    location of the compound statement-- this is usually the location
9377    of the opening brace.  */
9378
9379 tree
9380 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9381 {
9382   tree block = NULL;
9383
9384   if (do_scope)
9385     {
9386       if (c_dialect_objc ())
9387         objc_clear_super_receiver ();
9388       block = pop_scope ();
9389     }
9390
9391   stmt = pop_stmt_list (stmt);
9392   stmt = c_build_bind_expr (loc, block, stmt);
9393
9394   /* If this compound statement is nested immediately inside a statement
9395      expression, then force a BIND_EXPR to be created.  Otherwise we'll
9396      do the wrong thing for ({ { 1; } }) or ({ 1; { } }).  In particular,
9397      STATEMENT_LISTs merge, and thus we can lose track of what statement
9398      was really last.  */
9399   if (building_stmt_list_p ()
9400       && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9401       && TREE_CODE (stmt) != BIND_EXPR)
9402     {
9403       stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9404       TREE_SIDE_EFFECTS (stmt) = 1;
9405       SET_EXPR_LOCATION (stmt, loc);
9406     }
9407
9408   return stmt;
9409 }
9410
9411 /* Queue a cleanup.  CLEANUP is an expression/statement to be executed
9412    when the current scope is exited.  EH_ONLY is true when this is not
9413    meant to apply to normal control flow transfer.  */
9414
9415 void
9416 push_cleanup (tree decl, tree cleanup, bool eh_only)
9417 {
9418   enum tree_code code;
9419   tree stmt, list;
9420   bool stmt_expr;
9421
9422   code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9423   stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9424   add_stmt (stmt);
9425   stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9426   list = push_stmt_list ();
9427   TREE_OPERAND (stmt, 0) = list;
9428   STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9429 }
9430
9431 /* Convert scalar to vector for the range of operations.  */
9432 static enum stv_conv
9433 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1)
9434 {
9435   tree type0 = TREE_TYPE (op0);
9436   tree type1 = TREE_TYPE (op1);
9437   bool integer_only_op = false;
9438   enum stv_conv ret = stv_firstarg;
9439
9440   gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
9441               || TREE_CODE (type1) == VECTOR_TYPE);
9442   switch (code)
9443     {
9444       case RSHIFT_EXPR:
9445       case LSHIFT_EXPR:
9446         if (TREE_CODE (type0) == INTEGER_TYPE
9447             && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9448           {
9449             if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9450               {
9451                 error_at (loc, "conversion of scalar to vector "
9452                                "involves truncation");
9453                 return stv_error;
9454               }
9455             else
9456               return stv_firstarg;
9457           }
9458         break;
9459
9460       case BIT_IOR_EXPR:
9461       case BIT_XOR_EXPR:
9462       case BIT_AND_EXPR:
9463         integer_only_op = true;
9464         /* ... fall through ...  */
9465
9466       case PLUS_EXPR:
9467       case MINUS_EXPR:
9468       case MULT_EXPR:
9469       case TRUNC_DIV_EXPR:
9470       case TRUNC_MOD_EXPR:
9471       case RDIV_EXPR:
9472         if (TREE_CODE (type0) == VECTOR_TYPE)
9473           {
9474             tree tmp;
9475             ret = stv_secondarg;
9476             /* Swap TYPE0 with TYPE1 and OP0 with OP1  */
9477             tmp = type0; type0 = type1; type1 = tmp;
9478             tmp = op0; op0 = op1; op1 = tmp;
9479           }
9480
9481         if (TREE_CODE (type0) == INTEGER_TYPE
9482             && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9483           {
9484             if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9485               {
9486                 error_at (loc, "conversion of scalar to vector "
9487                                "involves truncation");
9488                 return stv_error;
9489               }
9490             return ret;
9491           }
9492         else if (!integer_only_op
9493                     /* Allow integer --> real conversion if safe.  */
9494                  && (TREE_CODE (type0) == REAL_TYPE
9495                      || TREE_CODE (type0) == INTEGER_TYPE)
9496                  && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
9497           {
9498             if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9499               {
9500                 error_at (loc, "conversion of scalar to vector "
9501                                "involves truncation");
9502                 return stv_error;
9503               }
9504             return ret;
9505           }
9506       default:
9507         break;
9508     }
9509
9510   return stv_nothing;
9511 }
9512 \f
9513 /* Build a binary-operation expression without default conversions.
9514    CODE is the kind of expression to build.
9515    LOCATION is the operator's location.
9516    This function differs from `build' in several ways:
9517    the data type of the result is computed and recorded in it,
9518    warnings are generated if arg data types are invalid,
9519    special handling for addition and subtraction of pointers is known,
9520    and some optimization is done (operations on narrow ints
9521    are done in the narrower type when that gives the same result).
9522    Constant folding is also done before the result is returned.
9523
9524    Note that the operands will never have enumeral types, or function
9525    or array types, because either they will have the default conversions
9526    performed or they have both just been converted to some other type in which
9527    the arithmetic is to be done.  */
9528
9529 tree
9530 build_binary_op (location_t location, enum tree_code code,
9531                  tree orig_op0, tree orig_op1, int convert_p)
9532 {
9533   tree type0, type1, orig_type0, orig_type1;
9534   tree eptype;
9535   enum tree_code code0, code1;
9536   tree op0, op1;
9537   tree ret = error_mark_node;
9538   const char *invalid_op_diag;
9539   bool op0_int_operands, op1_int_operands;
9540   bool int_const, int_const_or_overflow, int_operands;
9541
9542   /* Expression code to give to the expression when it is built.
9543      Normally this is CODE, which is what the caller asked for,
9544      but in some special cases we change it.  */
9545   enum tree_code resultcode = code;
9546
9547   /* Data type in which the computation is to be performed.
9548      In the simplest cases this is the common type of the arguments.  */
9549   tree result_type = NULL;
9550
9551   /* When the computation is in excess precision, the type of the
9552      final EXCESS_PRECISION_EXPR.  */
9553   tree semantic_result_type = NULL;
9554
9555   /* Nonzero means operands have already been type-converted
9556      in whatever way is necessary.
9557      Zero means they need to be converted to RESULT_TYPE.  */
9558   int converted = 0;
9559
9560   /* Nonzero means create the expression with this type, rather than
9561      RESULT_TYPE.  */
9562   tree build_type = 0;
9563
9564   /* Nonzero means after finally constructing the expression
9565      convert it to this type.  */
9566   tree final_type = 0;
9567
9568   /* Nonzero if this is an operation like MIN or MAX which can
9569      safely be computed in short if both args are promoted shorts.
9570      Also implies COMMON.
9571      -1 indicates a bitwise operation; this makes a difference
9572      in the exact conditions for when it is safe to do the operation
9573      in a narrower mode.  */
9574   int shorten = 0;
9575
9576   /* Nonzero if this is a comparison operation;
9577      if both args are promoted shorts, compare the original shorts.
9578      Also implies COMMON.  */
9579   int short_compare = 0;
9580
9581   /* Nonzero if this is a right-shift operation, which can be computed on the
9582      original short and then promoted if the operand is a promoted short.  */
9583   int short_shift = 0;
9584
9585   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
9586   int common = 0;
9587
9588   /* True means types are compatible as far as ObjC is concerned.  */
9589   bool objc_ok;
9590
9591   /* True means this is an arithmetic operation that may need excess
9592      precision.  */
9593   bool may_need_excess_precision;
9594
9595   /* True means this is a boolean operation that converts both its
9596      operands to truth-values.  */
9597   bool boolean_op = false;
9598
9599   if (location == UNKNOWN_LOCATION)
9600     location = input_location;
9601
9602   op0 = orig_op0;
9603   op1 = orig_op1;
9604
9605   op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9606   if (op0_int_operands)
9607     op0 = remove_c_maybe_const_expr (op0);
9608   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9609   if (op1_int_operands)
9610     op1 = remove_c_maybe_const_expr (op1);
9611   int_operands = (op0_int_operands && op1_int_operands);
9612   if (int_operands)
9613     {
9614       int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9615                                && TREE_CODE (orig_op1) == INTEGER_CST);
9616       int_const = (int_const_or_overflow
9617                    && !TREE_OVERFLOW (orig_op0)
9618                    && !TREE_OVERFLOW (orig_op1));
9619     }
9620   else
9621     int_const = int_const_or_overflow = false;
9622
9623   /* Do not apply default conversion in mixed vector/scalar expression.  */
9624   if (convert_p
9625       && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
9626            != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
9627     {
9628       op0 = default_conversion (op0);
9629       op1 = default_conversion (op1);
9630     }
9631
9632   orig_type0 = type0 = TREE_TYPE (op0);
9633   orig_type1 = type1 = TREE_TYPE (op1);
9634
9635   /* The expression codes of the data types of the arguments tell us
9636      whether the arguments are integers, floating, pointers, etc.  */
9637   code0 = TREE_CODE (type0);
9638   code1 = TREE_CODE (type1);
9639
9640   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
9641   STRIP_TYPE_NOPS (op0);
9642   STRIP_TYPE_NOPS (op1);
9643
9644   /* If an error was already reported for one of the arguments,
9645      avoid reporting another error.  */
9646
9647   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9648     return error_mark_node;
9649
9650   if ((invalid_op_diag
9651        = targetm.invalid_binary_op (code, type0, type1)))
9652     {
9653       error_at (location, invalid_op_diag);
9654       return error_mark_node;
9655     }
9656
9657   switch (code)
9658     {
9659     case PLUS_EXPR:
9660     case MINUS_EXPR:
9661     case MULT_EXPR:
9662     case TRUNC_DIV_EXPR:
9663     case CEIL_DIV_EXPR:
9664     case FLOOR_DIV_EXPR:
9665     case ROUND_DIV_EXPR:
9666     case EXACT_DIV_EXPR:
9667       may_need_excess_precision = true;
9668       break;
9669     default:
9670       may_need_excess_precision = false;
9671       break;
9672     }
9673   if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9674     {
9675       op0 = TREE_OPERAND (op0, 0);
9676       type0 = TREE_TYPE (op0);
9677     }
9678   else if (may_need_excess_precision
9679            && (eptype = excess_precision_type (type0)) != NULL_TREE)
9680     {
9681       type0 = eptype;
9682       op0 = convert (eptype, op0);
9683     }
9684   if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9685     {
9686       op1 = TREE_OPERAND (op1, 0);
9687       type1 = TREE_TYPE (op1);
9688     }
9689   else if (may_need_excess_precision
9690            && (eptype = excess_precision_type (type1)) != NULL_TREE)
9691     {
9692       type1 = eptype;
9693       op1 = convert (eptype, op1);
9694     }
9695
9696   objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9697
9698   /* In case when one of the operands of the binary operation is
9699      a vector and another is a scalar -- convert scalar to vector.  */
9700   if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
9701     {
9702       enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1);
9703
9704       switch (convert_flag)
9705         {
9706           case stv_error:
9707             return error_mark_node;
9708           case stv_firstarg:
9709             {
9710               bool maybe_const = true;
9711               tree sc;
9712               sc = c_fully_fold (op0, false, &maybe_const);
9713               sc = save_expr (sc);
9714               sc = convert (TREE_TYPE (type1), sc);
9715               op0 = build_vector_from_val (type1, sc);
9716               if (!maybe_const)
9717                 op0 = c_wrap_maybe_const (op0, true);
9718               orig_type0 = type0 = TREE_TYPE (op0);
9719               code0 = TREE_CODE (type0);
9720               converted = 1;
9721               break;
9722             }
9723           case stv_secondarg:
9724             {
9725               bool maybe_const = true;
9726               tree sc;
9727               sc = c_fully_fold (op1, false, &maybe_const);
9728               sc = save_expr (sc);
9729               sc = convert (TREE_TYPE (type0), sc);
9730               op1 = build_vector_from_val (type0, sc);
9731               if (!maybe_const)
9732                 op0 = c_wrap_maybe_const (op1, true);
9733               orig_type1 = type1 = TREE_TYPE (op1);
9734               code1 = TREE_CODE (type1);
9735               converted = 1;
9736               break;
9737             }
9738           default:
9739             break;
9740         }
9741     }
9742
9743   switch (code)
9744     {
9745     case PLUS_EXPR:
9746       /* Handle the pointer + int case.  */
9747       if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9748         {
9749           ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9750           goto return_build_binary_op;
9751         }
9752       else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9753         {
9754           ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9755           goto return_build_binary_op;
9756         }
9757       else
9758         common = 1;
9759       break;
9760
9761     case MINUS_EXPR:
9762       /* Subtraction of two similar pointers.
9763          We must subtract them as integers, then divide by object size.  */
9764       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9765           && comp_target_types (location, type0, type1))
9766         {
9767           ret = pointer_diff (location, op0, op1);
9768           goto return_build_binary_op;
9769         }
9770       /* Handle pointer minus int.  Just like pointer plus int.  */
9771       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9772         {
9773           ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9774           goto return_build_binary_op;
9775         }
9776       else
9777         common = 1;
9778       break;
9779
9780     case MULT_EXPR:
9781       common = 1;
9782       break;
9783
9784     case TRUNC_DIV_EXPR:
9785     case CEIL_DIV_EXPR:
9786     case FLOOR_DIV_EXPR:
9787     case ROUND_DIV_EXPR:
9788     case EXACT_DIV_EXPR:
9789       warn_for_div_by_zero (location, op1);
9790
9791       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9792            || code0 == FIXED_POINT_TYPE
9793            || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9794           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9795               || code1 == FIXED_POINT_TYPE
9796               || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9797         {
9798           enum tree_code tcode0 = code0, tcode1 = code1;
9799
9800           if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9801             tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9802           if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9803             tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9804
9805           if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9806               || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9807             resultcode = RDIV_EXPR;
9808           else
9809             /* Although it would be tempting to shorten always here, that
9810                loses on some targets, since the modulo instruction is
9811                undefined if the quotient can't be represented in the
9812                computation mode.  We shorten only if unsigned or if
9813                dividing by something we know != -1.  */
9814             shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9815                        || (TREE_CODE (op1) == INTEGER_CST
9816                            && !integer_all_onesp (op1)));
9817           common = 1;
9818         }
9819       break;
9820
9821     case BIT_AND_EXPR:
9822     case BIT_IOR_EXPR:
9823     case BIT_XOR_EXPR:
9824       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9825         shorten = -1;
9826       /* Allow vector types which are not floating point types.   */
9827       else if (code0 == VECTOR_TYPE
9828                && code1 == VECTOR_TYPE
9829                && !VECTOR_FLOAT_TYPE_P (type0)
9830                && !VECTOR_FLOAT_TYPE_P (type1))
9831         common = 1;
9832       break;
9833
9834     case TRUNC_MOD_EXPR:
9835     case FLOOR_MOD_EXPR:
9836       warn_for_div_by_zero (location, op1);
9837
9838       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9839           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9840           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9841         common = 1;
9842       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9843         {
9844           /* Although it would be tempting to shorten always here, that loses
9845              on some targets, since the modulo instruction is undefined if the
9846              quotient can't be represented in the computation mode.  We shorten
9847              only if unsigned or if dividing by something we know != -1.  */
9848           shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9849                      || (TREE_CODE (op1) == INTEGER_CST
9850                          && !integer_all_onesp (op1)));
9851           common = 1;
9852         }
9853       break;
9854
9855     case TRUTH_ANDIF_EXPR:
9856     case TRUTH_ORIF_EXPR:
9857     case TRUTH_AND_EXPR:
9858     case TRUTH_OR_EXPR:
9859     case TRUTH_XOR_EXPR:
9860       if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9861            || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9862            || code0 == FIXED_POINT_TYPE)
9863           && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9864               || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9865               || code1 == FIXED_POINT_TYPE))
9866         {
9867           /* Result of these operations is always an int,
9868              but that does not mean the operands should be
9869              converted to ints!  */
9870           result_type = integer_type_node;
9871           op0 = c_common_truthvalue_conversion (location, op0);
9872           op1 = c_common_truthvalue_conversion (location, op1);
9873           converted = 1;
9874           boolean_op = true;
9875         }
9876       if (code == TRUTH_ANDIF_EXPR)
9877         {
9878           int_const_or_overflow = (int_operands
9879                                    && TREE_CODE (orig_op0) == INTEGER_CST
9880                                    && (op0 == truthvalue_false_node
9881                                        || TREE_CODE (orig_op1) == INTEGER_CST));
9882           int_const = (int_const_or_overflow
9883                        && !TREE_OVERFLOW (orig_op0)
9884                        && (op0 == truthvalue_false_node
9885                            || !TREE_OVERFLOW (orig_op1)));
9886         }
9887       else if (code == TRUTH_ORIF_EXPR)
9888         {
9889           int_const_or_overflow = (int_operands
9890                                    && TREE_CODE (orig_op0) == INTEGER_CST
9891                                    && (op0 == truthvalue_true_node
9892                                        || TREE_CODE (orig_op1) == INTEGER_CST));
9893           int_const = (int_const_or_overflow
9894                        && !TREE_OVERFLOW (orig_op0)
9895                        && (op0 == truthvalue_true_node
9896                            || !TREE_OVERFLOW (orig_op1)));
9897         }
9898       break;
9899
9900       /* Shift operations: result has same type as first operand;
9901          always convert second operand to int.
9902          Also set SHORT_SHIFT if shifting rightward.  */
9903
9904     case RSHIFT_EXPR:
9905       if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9906           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9907         {
9908           result_type = type0;
9909           converted = 1;
9910         }
9911       else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9912           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9913           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9914           && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9915         {
9916           result_type = type0;
9917           converted = 1;
9918         }
9919       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9920           && code1 == INTEGER_TYPE)
9921         {
9922           if (TREE_CODE (op1) == INTEGER_CST)
9923             {
9924               if (tree_int_cst_sgn (op1) < 0)
9925                 {
9926                   int_const = false;
9927                   if (c_inhibit_evaluation_warnings == 0)
9928                     warning (0, "right shift count is negative");
9929                 }
9930               else
9931                 {
9932                   if (!integer_zerop (op1))
9933                     short_shift = 1;
9934
9935                   if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9936                     {
9937                       int_const = false;
9938                       if (c_inhibit_evaluation_warnings == 0)
9939                         warning (0, "right shift count >= width of type");
9940                     }
9941                 }
9942             }
9943
9944           /* Use the type of the value to be shifted.  */
9945           result_type = type0;
9946           /* Convert the non vector shift-count to an integer, regardless
9947              of size of value being shifted.  */
9948           if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9949               && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9950             op1 = convert (integer_type_node, op1);
9951           /* Avoid converting op1 to result_type later.  */
9952           converted = 1;
9953         }
9954       break;
9955
9956     case LSHIFT_EXPR:
9957       if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9958           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9959         {
9960           result_type = type0;
9961           converted = 1;
9962         }
9963       else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9964           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9965           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9966           && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9967         {
9968           result_type = type0;
9969           converted = 1;
9970         }
9971       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9972           && code1 == INTEGER_TYPE)
9973         {
9974           if (TREE_CODE (op1) == INTEGER_CST)
9975             {
9976               if (tree_int_cst_sgn (op1) < 0)
9977                 {
9978                   int_const = false;
9979                   if (c_inhibit_evaluation_warnings == 0)
9980                     warning (0, "left shift count is negative");
9981                 }
9982
9983               else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9984                 {
9985                   int_const = false;
9986                   if (c_inhibit_evaluation_warnings == 0)
9987                     warning (0, "left shift count >= width of type");
9988                 }
9989             }
9990
9991           /* Use the type of the value to be shifted.  */
9992           result_type = type0;
9993           /* Convert the non vector shift-count to an integer, regardless
9994              of size of value being shifted.  */
9995           if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9996               && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9997             op1 = convert (integer_type_node, op1);
9998           /* Avoid converting op1 to result_type later.  */
9999           converted = 1;
10000         }
10001       break;
10002
10003     case EQ_EXPR:
10004     case NE_EXPR:
10005       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10006         {
10007           tree intt;
10008           if (TREE_TYPE (type0) != TREE_TYPE (type1))
10009             {
10010               error_at (location, "comparing vectors with different "
10011                                   "element types");
10012               return error_mark_node;
10013             }
10014
10015           if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10016             {
10017               error_at (location, "comparing vectors with different "
10018                                   "number of elements");
10019               return error_mark_node;
10020             }
10021
10022           /* Always construct signed integer vector type.  */
10023           intt = c_common_type_for_size (GET_MODE_BITSIZE
10024                                            (TYPE_MODE (TREE_TYPE (type0))), 0);
10025           result_type = build_opaque_vector_type (intt,
10026                                                   TYPE_VECTOR_SUBPARTS (type0));
10027           converted = 1;
10028           break;
10029         }
10030       if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
10031         warning_at (location,
10032                     OPT_Wfloat_equal,
10033                     "comparing floating point with == or != is unsafe");
10034       /* Result of comparison is always int,
10035          but don't convert the args to int!  */
10036       build_type = integer_type_node;
10037       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10038            || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
10039           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10040               || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
10041         short_compare = 1;
10042       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10043         {
10044           if (TREE_CODE (op0) == ADDR_EXPR
10045               && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10046             {
10047               if (code == EQ_EXPR)
10048                 warning_at (location,
10049                             OPT_Waddress,
10050                             "the comparison will always evaluate as %<false%> "
10051                             "for the address of %qD will never be NULL",
10052                             TREE_OPERAND (op0, 0));
10053               else
10054                 warning_at (location,
10055                             OPT_Waddress,
10056                             "the comparison will always evaluate as %<true%> "
10057                             "for the address of %qD will never be NULL",
10058                             TREE_OPERAND (op0, 0));
10059             }
10060           result_type = type0;
10061         }
10062       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10063         {
10064           if (TREE_CODE (op1) == ADDR_EXPR
10065               && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10066             {
10067               if (code == EQ_EXPR)
10068                 warning_at (location,
10069                             OPT_Waddress,
10070                             "the comparison will always evaluate as %<false%> "
10071                             "for the address of %qD will never be NULL",
10072                             TREE_OPERAND (op1, 0));
10073               else
10074                 warning_at (location,
10075                             OPT_Waddress,
10076                             "the comparison will always evaluate as %<true%> "
10077                             "for the address of %qD will never be NULL",
10078                             TREE_OPERAND (op1, 0));
10079             }
10080           result_type = type1;
10081         }
10082       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10083         {
10084           tree tt0 = TREE_TYPE (type0);
10085           tree tt1 = TREE_TYPE (type1);
10086           addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10087           addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10088           addr_space_t as_common = ADDR_SPACE_GENERIC;
10089
10090           /* Anything compares with void *.  void * compares with anything.
10091              Otherwise, the targets must be compatible
10092              and both must be object or both incomplete.  */
10093           if (comp_target_types (location, type0, type1))
10094             result_type = common_pointer_type (type0, type1);
10095           else if (!addr_space_superset (as0, as1, &as_common))
10096             {
10097               error_at (location, "comparison of pointers to "
10098                         "disjoint address spaces");
10099               return error_mark_node;
10100             }
10101           else if (VOID_TYPE_P (tt0))
10102             {
10103               if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10104                 pedwarn (location, OPT_pedantic, "ISO C forbids "
10105                          "comparison of %<void *%> with function pointer");
10106             }
10107           else if (VOID_TYPE_P (tt1))
10108             {
10109               if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10110                 pedwarn (location, OPT_pedantic, "ISO C forbids "
10111                          "comparison of %<void *%> with function pointer");
10112             }
10113           else
10114             /* Avoid warning about the volatile ObjC EH puts on decls.  */
10115             if (!objc_ok)
10116               pedwarn (location, 0,
10117                        "comparison of distinct pointer types lacks a cast");
10118
10119           if (result_type == NULL_TREE)
10120             {
10121               int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10122               result_type = build_pointer_type
10123                               (build_qualified_type (void_type_node, qual));
10124             }
10125         }
10126       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10127         {
10128           result_type = type0;
10129           pedwarn (location, 0, "comparison between pointer and integer");
10130         }
10131       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10132         {
10133           result_type = type1;
10134           pedwarn (location, 0, "comparison between pointer and integer");
10135         }
10136       break;
10137
10138     case LE_EXPR:
10139     case GE_EXPR:
10140     case LT_EXPR:
10141     case GT_EXPR:
10142       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10143         {
10144           tree intt;
10145           if (TREE_TYPE (type0) != TREE_TYPE (type1))
10146             {
10147               error_at (location, "comparing vectors with different "
10148                                   "element types");
10149               return error_mark_node;
10150             }
10151
10152           if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10153             {
10154               error_at (location, "comparing vectors with different "
10155                                   "number of elements");
10156               return error_mark_node;
10157             }
10158
10159           /* Always construct signed integer vector type.  */
10160           intt = c_common_type_for_size (GET_MODE_BITSIZE
10161                                            (TYPE_MODE (TREE_TYPE (type0))), 0);
10162           result_type = build_opaque_vector_type (intt,
10163                                                   TYPE_VECTOR_SUBPARTS (type0));
10164           converted = 1;
10165           break;
10166         }
10167       build_type = integer_type_node;
10168       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10169            || code0 == FIXED_POINT_TYPE)
10170           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10171               || code1 == FIXED_POINT_TYPE))
10172         short_compare = 1;
10173       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10174         {
10175           addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10176           addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10177           addr_space_t as_common;
10178
10179           if (comp_target_types (location, type0, type1))
10180             {
10181               result_type = common_pointer_type (type0, type1);
10182               if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10183                   != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10184                 pedwarn (location, 0,
10185                          "comparison of complete and incomplete pointers");
10186               else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10187                 pedwarn (location, OPT_pedantic, "ISO C forbids "
10188                          "ordered comparisons of pointers to functions");
10189               else if (null_pointer_constant_p (orig_op0)
10190                        || null_pointer_constant_p (orig_op1))
10191                 warning_at (location, OPT_Wextra,
10192                             "ordered comparison of pointer with null pointer");
10193
10194             }
10195           else if (!addr_space_superset (as0, as1, &as_common))
10196             {
10197               error_at (location, "comparison of pointers to "
10198                         "disjoint address spaces");
10199               return error_mark_node;
10200             }
10201           else
10202             {
10203               int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10204               result_type = build_pointer_type
10205                               (build_qualified_type (void_type_node, qual));
10206               pedwarn (location, 0,
10207                        "comparison of distinct pointer types lacks a cast");
10208             }
10209         }
10210       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10211         {
10212           result_type = type0;
10213           if (pedantic)
10214             pedwarn (location, OPT_pedantic,
10215                      "ordered comparison of pointer with integer zero");
10216           else if (extra_warnings)
10217             warning_at (location, OPT_Wextra,
10218                         "ordered comparison of pointer with integer zero");
10219         }
10220       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10221         {
10222           result_type = type1;
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 (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10231         {
10232           result_type = type0;
10233           pedwarn (location, 0, "comparison between pointer and integer");
10234         }
10235       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10236         {
10237           result_type = type1;
10238           pedwarn (location, 0, "comparison between pointer and integer");
10239         }
10240       break;
10241
10242     default:
10243       gcc_unreachable ();
10244     }
10245
10246   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10247     return error_mark_node;
10248
10249   if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10250       && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10251           || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10252                                                     TREE_TYPE (type1))))
10253     {
10254       binary_op_error (location, code, type0, type1);
10255       return error_mark_node;
10256     }
10257
10258   if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10259        || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10260       &&
10261       (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10262        || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10263     {
10264       bool first_complex = (code0 == COMPLEX_TYPE);
10265       bool second_complex = (code1 == COMPLEX_TYPE);
10266       int none_complex = (!first_complex && !second_complex);
10267
10268       if (shorten || common || short_compare)
10269         {
10270           result_type = c_common_type (type0, type1);
10271           do_warn_double_promotion (result_type, type0, type1,
10272                                     "implicit conversion from %qT to %qT "
10273                                     "to match other operand of binary "
10274                                     "expression",
10275                                     location);
10276           if (result_type == error_mark_node)
10277             return error_mark_node;
10278         }
10279
10280       if (first_complex != second_complex
10281           && (code == PLUS_EXPR
10282               || code == MINUS_EXPR
10283               || code == MULT_EXPR
10284               || (code == TRUNC_DIV_EXPR && first_complex))
10285           && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10286           && flag_signed_zeros)
10287         {
10288           /* An operation on mixed real/complex operands must be
10289              handled specially, but the language-independent code can
10290              more easily optimize the plain complex arithmetic if
10291              -fno-signed-zeros.  */
10292           tree real_type = TREE_TYPE (result_type);
10293           tree real, imag;
10294           if (type0 != orig_type0 || type1 != orig_type1)
10295             {
10296               gcc_assert (may_need_excess_precision && common);
10297               semantic_result_type = c_common_type (orig_type0, orig_type1);
10298             }
10299           if (first_complex)
10300             {
10301               if (TREE_TYPE (op0) != result_type)
10302                 op0 = convert_and_check (result_type, op0);
10303               if (TREE_TYPE (op1) != real_type)
10304                 op1 = convert_and_check (real_type, op1);
10305             }
10306           else
10307             {
10308               if (TREE_TYPE (op0) != real_type)
10309                 op0 = convert_and_check (real_type, op0);
10310               if (TREE_TYPE (op1) != result_type)
10311                 op1 = convert_and_check (result_type, op1);
10312             }
10313           if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10314             return error_mark_node;
10315           if (first_complex)
10316             {
10317               op0 = c_save_expr (op0);
10318               real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10319                                      op0, 1);
10320               imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10321                                      op0, 1);
10322               switch (code)
10323                 {
10324                 case MULT_EXPR:
10325                 case TRUNC_DIV_EXPR:
10326                   op1 = c_save_expr (op1);
10327                   imag = build2 (resultcode, real_type, imag, op1);
10328                   /* Fall through.  */
10329                 case PLUS_EXPR:
10330                 case MINUS_EXPR:
10331                   real = build2 (resultcode, real_type, real, op1);
10332                   break;
10333                 default:
10334                   gcc_unreachable();
10335                 }
10336             }
10337           else
10338             {
10339               op1 = c_save_expr (op1);
10340               real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10341                                      op1, 1);
10342               imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10343                                      op1, 1);
10344               switch (code)
10345                 {
10346                 case MULT_EXPR:
10347                   op0 = c_save_expr (op0);
10348                   imag = build2 (resultcode, real_type, op0, imag);
10349                   /* Fall through.  */
10350                 case PLUS_EXPR:
10351                   real = build2 (resultcode, real_type, op0, real);
10352                   break;
10353                 case MINUS_EXPR:
10354                   real = build2 (resultcode, real_type, op0, real);
10355                   imag = build1 (NEGATE_EXPR, real_type, imag);
10356                   break;
10357                 default:
10358                   gcc_unreachable();
10359                 }
10360             }
10361           ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10362           goto return_build_binary_op;
10363         }
10364
10365       /* For certain operations (which identify themselves by shorten != 0)
10366          if both args were extended from the same smaller type,
10367          do the arithmetic in that type and then extend.
10368
10369          shorten !=0 and !=1 indicates a bitwise operation.
10370          For them, this optimization is safe only if
10371          both args are zero-extended or both are sign-extended.
10372          Otherwise, we might change the result.
10373          Eg, (short)-1 | (unsigned short)-1 is (int)-1
10374          but calculated in (unsigned short) it would be (unsigned short)-1.  */
10375
10376       if (shorten && none_complex)
10377         {
10378           final_type = result_type;
10379           result_type = shorten_binary_op (result_type, op0, op1,
10380                                            shorten == -1);
10381         }
10382
10383       /* Shifts can be shortened if shifting right.  */
10384
10385       if (short_shift)
10386         {
10387           int unsigned_arg;
10388           tree arg0 = get_narrower (op0, &unsigned_arg);
10389
10390           final_type = result_type;
10391
10392           if (arg0 == op0 && final_type == TREE_TYPE (op0))
10393             unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10394
10395           if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10396               && tree_int_cst_sgn (op1) > 0
10397               /* We can shorten only if the shift count is less than the
10398                  number of bits in the smaller type size.  */
10399               && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10400               /* We cannot drop an unsigned shift after sign-extension.  */
10401               && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10402             {
10403               /* Do an unsigned shift if the operand was zero-extended.  */
10404               result_type
10405                 = c_common_signed_or_unsigned_type (unsigned_arg,
10406                                                     TREE_TYPE (arg0));
10407               /* Convert value-to-be-shifted to that type.  */
10408               if (TREE_TYPE (op0) != result_type)
10409                 op0 = convert (result_type, op0);
10410               converted = 1;
10411             }
10412         }
10413
10414       /* Comparison operations are shortened too but differently.
10415          They identify themselves by setting short_compare = 1.  */
10416
10417       if (short_compare)
10418         {
10419           /* Don't write &op0, etc., because that would prevent op0
10420              from being kept in a register.
10421              Instead, make copies of the our local variables and
10422              pass the copies by reference, then copy them back afterward.  */
10423           tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10424           enum tree_code xresultcode = resultcode;
10425           tree val
10426             = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10427
10428           if (val != 0)
10429             {
10430               ret = val;
10431               goto return_build_binary_op;
10432             }
10433
10434           op0 = xop0, op1 = xop1;
10435           converted = 1;
10436           resultcode = xresultcode;
10437
10438           if (c_inhibit_evaluation_warnings == 0)
10439             {
10440               bool op0_maybe_const = true;
10441               bool op1_maybe_const = true;
10442               tree orig_op0_folded, orig_op1_folded;
10443
10444               if (in_late_binary_op)
10445                 {
10446                   orig_op0_folded = orig_op0;
10447                   orig_op1_folded = orig_op1;
10448                 }
10449               else
10450                 {
10451                   /* Fold for the sake of possible warnings, as in
10452                      build_conditional_expr.  This requires the
10453                      "original" values to be folded, not just op0 and
10454                      op1.  */
10455                   c_inhibit_evaluation_warnings++;
10456                   op0 = c_fully_fold (op0, require_constant_value,
10457                                       &op0_maybe_const);
10458                   op1 = c_fully_fold (op1, require_constant_value,
10459                                       &op1_maybe_const);
10460                   c_inhibit_evaluation_warnings--;
10461                   orig_op0_folded = c_fully_fold (orig_op0,
10462                                                   require_constant_value,
10463                                                   NULL);
10464                   orig_op1_folded = c_fully_fold (orig_op1,
10465                                                   require_constant_value,
10466                                                   NULL);
10467                 }
10468
10469               if (warn_sign_compare)
10470                 warn_for_sign_compare (location, orig_op0_folded,
10471                                        orig_op1_folded, op0, op1,
10472                                        result_type, resultcode);
10473               if (!in_late_binary_op && !int_operands)
10474                 {
10475                   if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10476                     op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10477                   if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10478                     op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10479                 }
10480             }
10481         }
10482     }
10483
10484   /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10485      If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10486      Then the expression will be built.
10487      It will be given type FINAL_TYPE if that is nonzero;
10488      otherwise, it will be given type RESULT_TYPE.  */
10489
10490   if (!result_type)
10491     {
10492       binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10493       return error_mark_node;
10494     }
10495
10496   if (build_type == NULL_TREE)
10497     {
10498       build_type = result_type;
10499       if ((type0 != orig_type0 || type1 != orig_type1)
10500           && !boolean_op)
10501         {
10502           gcc_assert (may_need_excess_precision && common);
10503           semantic_result_type = c_common_type (orig_type0, orig_type1);
10504         }
10505     }
10506
10507   if (!converted)
10508     {
10509       op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10510       op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10511
10512       /* This can happen if one operand has a vector type, and the other
10513          has a different type.  */
10514       if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10515         return error_mark_node;
10516     }
10517
10518   /* Treat expressions in initializers specially as they can't trap.  */
10519   if (int_const_or_overflow)
10520     ret = (require_constant_value
10521            ? fold_build2_initializer_loc (location, resultcode, build_type,
10522                                           op0, op1)
10523            : fold_build2_loc (location, resultcode, build_type, op0, op1));
10524   else
10525     ret = build2 (resultcode, build_type, op0, op1);
10526   if (final_type != 0)
10527     ret = convert (final_type, ret);
10528
10529  return_build_binary_op:
10530   gcc_assert (ret != error_mark_node);
10531   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10532     ret = (int_operands
10533            ? note_integer_operands (ret)
10534            : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10535   else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10536            && !in_late_binary_op)
10537     ret = note_integer_operands (ret);
10538   if (semantic_result_type)
10539     ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10540   protected_set_expr_location (ret, location);
10541   return ret;
10542 }
10543
10544
10545 /* Convert EXPR to be a truth-value, validating its type for this
10546    purpose.  LOCATION is the source location for the expression.  */
10547
10548 tree
10549 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10550 {
10551   bool int_const, int_operands;
10552
10553   switch (TREE_CODE (TREE_TYPE (expr)))
10554     {
10555     case ARRAY_TYPE:
10556       error_at (location, "used array that cannot be converted to pointer where scalar is required");
10557       return error_mark_node;
10558
10559     case RECORD_TYPE:
10560       error_at (location, "used struct type value where scalar is required");
10561       return error_mark_node;
10562
10563     case UNION_TYPE:
10564       error_at (location, "used union type value where scalar is required");
10565       return error_mark_node;
10566
10567     case VOID_TYPE:
10568       error_at (location, "void value not ignored as it ought to be");
10569       return error_mark_node;
10570
10571     case FUNCTION_TYPE:
10572       gcc_unreachable ();
10573
10574     case VECTOR_TYPE:
10575       error_at (location, "used vector type where scalar is required");
10576       return error_mark_node;
10577
10578     default:
10579       break;
10580     }
10581
10582   int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10583   int_operands = EXPR_INT_CONST_OPERANDS (expr);
10584   if (int_operands)
10585     expr = remove_c_maybe_const_expr (expr);
10586
10587   /* ??? Should we also give an error for vectors rather than leaving
10588      those to give errors later?  */
10589   expr = c_common_truthvalue_conversion (location, expr);
10590
10591   if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10592     {
10593       if (TREE_OVERFLOW (expr))
10594         return expr;
10595       else
10596         return note_integer_operands (expr);
10597     }
10598   if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10599     return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10600   return expr;
10601 }
10602 \f
10603
10604 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10605    required.  */
10606
10607 tree
10608 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10609 {
10610   if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10611     {
10612       tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10613       /* Executing a compound literal inside a function reinitializes
10614          it.  */
10615       if (!TREE_STATIC (decl))
10616         *se = true;
10617       return decl;
10618     }
10619   else
10620     return expr;
10621 }
10622 \f
10623 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
10624
10625 tree
10626 c_begin_omp_parallel (void)
10627 {
10628   tree block;
10629
10630   keep_next_level ();
10631   block = c_begin_compound_stmt (true);
10632
10633   return block;
10634 }
10635
10636 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10637    statement.  LOC is the location of the OMP_PARALLEL.  */
10638
10639 tree
10640 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10641 {
10642   tree stmt;
10643
10644   block = c_end_compound_stmt (loc, block, true);
10645
10646   stmt = make_node (OMP_PARALLEL);
10647   TREE_TYPE (stmt) = void_type_node;
10648   OMP_PARALLEL_CLAUSES (stmt) = clauses;
10649   OMP_PARALLEL_BODY (stmt) = block;
10650   SET_EXPR_LOCATION (stmt, loc);
10651
10652   return add_stmt (stmt);
10653 }
10654
10655 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
10656
10657 tree
10658 c_begin_omp_task (void)
10659 {
10660   tree block;
10661
10662   keep_next_level ();
10663   block = c_begin_compound_stmt (true);
10664
10665   return block;
10666 }
10667
10668 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10669    statement.  LOC is the location of the #pragma.  */
10670
10671 tree
10672 c_finish_omp_task (location_t loc, tree clauses, tree block)
10673 {
10674   tree stmt;
10675
10676   block = c_end_compound_stmt (loc, block, true);
10677
10678   stmt = make_node (OMP_TASK);
10679   TREE_TYPE (stmt) = void_type_node;
10680   OMP_TASK_CLAUSES (stmt) = clauses;
10681   OMP_TASK_BODY (stmt) = block;
10682   SET_EXPR_LOCATION (stmt, loc);
10683
10684   return add_stmt (stmt);
10685 }
10686
10687 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10688    Remove any elements from the list that are invalid.  */
10689
10690 tree
10691 c_finish_omp_clauses (tree clauses)
10692 {
10693   bitmap_head generic_head, firstprivate_head, lastprivate_head;
10694   tree c, t, *pc = &clauses;
10695   const char *name;
10696
10697   bitmap_obstack_initialize (NULL);
10698   bitmap_initialize (&generic_head, &bitmap_default_obstack);
10699   bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10700   bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10701
10702   for (pc = &clauses, c = clauses; c ; c = *pc)
10703     {
10704       bool remove = false;
10705       bool need_complete = false;
10706       bool need_implicitly_determined = false;
10707
10708       switch (OMP_CLAUSE_CODE (c))
10709         {
10710         case OMP_CLAUSE_SHARED:
10711           name = "shared";
10712           need_implicitly_determined = true;
10713           goto check_dup_generic;
10714
10715         case OMP_CLAUSE_PRIVATE:
10716           name = "private";
10717           need_complete = true;
10718           need_implicitly_determined = true;
10719           goto check_dup_generic;
10720
10721         case OMP_CLAUSE_REDUCTION:
10722           name = "reduction";
10723           need_implicitly_determined = true;
10724           t = OMP_CLAUSE_DECL (c);
10725           if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10726               || POINTER_TYPE_P (TREE_TYPE (t)))
10727             {
10728               error_at (OMP_CLAUSE_LOCATION (c),
10729                         "%qE has invalid type for %<reduction%>", t);
10730               remove = true;
10731             }
10732           else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10733             {
10734               enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10735               const char *r_name = NULL;
10736
10737               switch (r_code)
10738                 {
10739                 case PLUS_EXPR:
10740                 case MULT_EXPR:
10741                 case MINUS_EXPR:
10742                 case MIN_EXPR:
10743                 case MAX_EXPR:
10744                   break;
10745                 case BIT_AND_EXPR:
10746                   r_name = "&";
10747                   break;
10748                 case BIT_XOR_EXPR:
10749                   r_name = "^";
10750                   break;
10751                 case BIT_IOR_EXPR:
10752                   r_name = "|";
10753                   break;
10754                 case TRUTH_ANDIF_EXPR:
10755                   r_name = "&&";
10756                   break;
10757                 case TRUTH_ORIF_EXPR:
10758                   r_name = "||";
10759                   break;
10760                 default:
10761                   gcc_unreachable ();
10762                 }
10763               if (r_name)
10764                 {
10765                   error_at (OMP_CLAUSE_LOCATION (c),
10766                             "%qE has invalid type for %<reduction(%s)%>",
10767                             t, r_name);
10768                   remove = true;
10769                 }
10770             }
10771           goto check_dup_generic;
10772
10773         case OMP_CLAUSE_COPYPRIVATE:
10774           name = "copyprivate";
10775           goto check_dup_generic;
10776
10777         case OMP_CLAUSE_COPYIN:
10778           name = "copyin";
10779           t = OMP_CLAUSE_DECL (c);
10780           if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10781             {
10782               error_at (OMP_CLAUSE_LOCATION (c),
10783                         "%qE must be %<threadprivate%> for %<copyin%>", t);
10784               remove = true;
10785             }
10786           goto check_dup_generic;
10787
10788         check_dup_generic:
10789           t = OMP_CLAUSE_DECL (c);
10790           if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10791             {
10792               error_at (OMP_CLAUSE_LOCATION (c),
10793                         "%qE is not a variable in clause %qs", t, name);
10794               remove = true;
10795             }
10796           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10797                    || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10798                    || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10799             {
10800               error_at (OMP_CLAUSE_LOCATION (c),
10801                         "%qE appears more than once in data clauses", t);
10802               remove = true;
10803             }
10804           else
10805             bitmap_set_bit (&generic_head, DECL_UID (t));
10806           break;
10807
10808         case OMP_CLAUSE_FIRSTPRIVATE:
10809           name = "firstprivate";
10810           t = OMP_CLAUSE_DECL (c);
10811           need_complete = true;
10812           need_implicitly_determined = true;
10813           if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10814             {
10815               error_at (OMP_CLAUSE_LOCATION (c),
10816                         "%qE is not a variable in clause %<firstprivate%>", t);
10817               remove = true;
10818             }
10819           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10820                    || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10821             {
10822               error_at (OMP_CLAUSE_LOCATION (c),
10823                         "%qE appears more than once in data clauses", t);
10824               remove = true;
10825             }
10826           else
10827             bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10828           break;
10829
10830         case OMP_CLAUSE_LASTPRIVATE:
10831           name = "lastprivate";
10832           t = OMP_CLAUSE_DECL (c);
10833           need_complete = true;
10834           need_implicitly_determined = true;
10835           if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10836             {
10837               error_at (OMP_CLAUSE_LOCATION (c),
10838                         "%qE is not a variable in clause %<lastprivate%>", t);
10839               remove = true;
10840             }
10841           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10842                    || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10843             {
10844               error_at (OMP_CLAUSE_LOCATION (c),
10845                      "%qE appears more than once in data clauses", t);
10846               remove = true;
10847             }
10848           else
10849             bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10850           break;
10851
10852         case OMP_CLAUSE_IF:
10853         case OMP_CLAUSE_NUM_THREADS:
10854         case OMP_CLAUSE_SCHEDULE:
10855         case OMP_CLAUSE_NOWAIT:
10856         case OMP_CLAUSE_ORDERED:
10857         case OMP_CLAUSE_DEFAULT:
10858         case OMP_CLAUSE_UNTIED:
10859         case OMP_CLAUSE_COLLAPSE:
10860         case OMP_CLAUSE_FINAL:
10861         case OMP_CLAUSE_MERGEABLE:
10862           pc = &OMP_CLAUSE_CHAIN (c);
10863           continue;
10864
10865         default:
10866           gcc_unreachable ();
10867         }
10868
10869       if (!remove)
10870         {
10871           t = OMP_CLAUSE_DECL (c);
10872
10873           if (need_complete)
10874             {
10875               t = require_complete_type (t);
10876               if (t == error_mark_node)
10877                 remove = true;
10878             }
10879
10880           if (need_implicitly_determined)
10881             {
10882               const char *share_name = NULL;
10883
10884               if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10885                 share_name = "threadprivate";
10886               else switch (c_omp_predetermined_sharing (t))
10887                 {
10888                 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10889                   break;
10890                 case OMP_CLAUSE_DEFAULT_SHARED:
10891                   /* const vars may be specified in firstprivate clause.  */
10892                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10893                       && TREE_READONLY (t))
10894                     break;
10895                   share_name = "shared";
10896                   break;
10897                 case OMP_CLAUSE_DEFAULT_PRIVATE:
10898                   share_name = "private";
10899                   break;
10900                 default:
10901                   gcc_unreachable ();
10902                 }
10903               if (share_name)
10904                 {
10905                   error_at (OMP_CLAUSE_LOCATION (c),
10906                             "%qE is predetermined %qs for %qs",
10907                             t, share_name, name);
10908                   remove = true;
10909                 }
10910             }
10911         }
10912
10913       if (remove)
10914         *pc = OMP_CLAUSE_CHAIN (c);
10915       else
10916         pc = &OMP_CLAUSE_CHAIN (c);
10917     }
10918
10919   bitmap_obstack_release (NULL);
10920   return clauses;
10921 }
10922
10923 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10924    down to the element type of an array.  */
10925
10926 tree
10927 c_build_qualified_type (tree type, int type_quals)
10928 {
10929   if (type == error_mark_node)
10930     return type;
10931
10932   if (TREE_CODE (type) == ARRAY_TYPE)
10933     {
10934       tree t;
10935       tree element_type = c_build_qualified_type (TREE_TYPE (type),
10936                                                   type_quals);
10937
10938       /* See if we already have an identically qualified type.  */
10939       for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10940         {
10941           if (TYPE_QUALS (strip_array_types (t)) == type_quals
10942               && TYPE_NAME (t) == TYPE_NAME (type)
10943               && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10944               && attribute_list_equal (TYPE_ATTRIBUTES (t),
10945                                        TYPE_ATTRIBUTES (type)))
10946             break;
10947         }
10948       if (!t)
10949         {
10950           tree domain = TYPE_DOMAIN (type);
10951
10952           t = build_variant_type_copy (type);
10953           TREE_TYPE (t) = element_type;
10954
10955           if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10956               || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10957             SET_TYPE_STRUCTURAL_EQUALITY (t);
10958           else if (TYPE_CANONICAL (element_type) != element_type
10959                    || (domain && TYPE_CANONICAL (domain) != domain))
10960             {
10961               tree unqualified_canon
10962                 = build_array_type (TYPE_CANONICAL (element_type),
10963                                     domain? TYPE_CANONICAL (domain)
10964                                           : NULL_TREE);
10965               TYPE_CANONICAL (t)
10966                 = c_build_qualified_type (unqualified_canon, type_quals);
10967             }
10968           else
10969             TYPE_CANONICAL (t) = t;
10970         }
10971       return t;
10972     }
10973
10974   /* A restrict-qualified pointer type must be a pointer to object or
10975      incomplete type.  Note that the use of POINTER_TYPE_P also allows
10976      REFERENCE_TYPEs, which is appropriate for C++.  */
10977   if ((type_quals & TYPE_QUAL_RESTRICT)
10978       && (!POINTER_TYPE_P (type)
10979           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10980     {
10981       error ("invalid use of %<restrict%>");
10982       type_quals &= ~TYPE_QUAL_RESTRICT;
10983     }
10984
10985   return build_qualified_type (type, type_quals);
10986 }
10987
10988 /* Build a VA_ARG_EXPR for the C parser.  */
10989
10990 tree
10991 c_build_va_arg (location_t loc, tree expr, tree type)
10992 {
10993   if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10994     warning_at (loc, OPT_Wc___compat,
10995                 "C++ requires promoted type, not enum type, in %<va_arg%>");
10996   return build_va_arg (loc, expr, type);
10997 }