OSDN Git Service

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