OSDN Git Service

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