OSDN Git Service

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