OSDN Git Service

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