OSDN Git Service

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