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