OSDN Git Service

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