OSDN Git Service

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