OSDN Git Service

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