OSDN Git Service

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