OSDN Git Service

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