OSDN Git Service

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