OSDN Git Service

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