OSDN Git Service

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