OSDN Git Service

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