OSDN Git Service

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