OSDN Git Service

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