OSDN Git Service

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