OSDN Git Service

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