OSDN Git Service

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