OSDN Git Service

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