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