OSDN Git Service

2010-09-28 Joel Sherrill <joel.sherrill@oarcorp.com>
[pf3gnuchains/gcc-fork.git] / gcc / c-typeck.c
1 /* Build expressions with type checking for C compiler.
2    Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 "tree.h"
33 #include "langhooks.h"
34 #include "c-tree.h"
35 #include "c-lang.h"
36 #include "flags.h"
37 #include "output.h"
38 #include "toplev.h"
39 #include "intl.h"
40 #include "target.h"
41 #include "tree-iterator.h"
42 #include "bitmap.h"
43 #include "gimple.h"
44
45 /* Possible cases of implicit bad conversions.  Used to select
46    diagnostic messages in convert_for_assignment.  */
47 enum impl_conv {
48   ic_argpass,
49   ic_assign,
50   ic_init,
51   ic_return
52 };
53
54 /* Whether we are building a boolean conversion inside
55    convert_for_assignment, or some other late binary operation.  If
56    build_binary_op is called (from code shared with C++) in this case,
57    then the operands have already been folded and the result will not
58    be folded again, so C_MAYBE_CONST_EXPR should not be generated.  */
59 bool in_late_binary_op;
60
61 /* The level of nesting inside "__alignof__".  */
62 int in_alignof;
63
64 /* The level of nesting inside "sizeof".  */
65 int in_sizeof;
66
67 /* The level of nesting inside "typeof".  */
68 int in_typeof;
69
70 /* Nonzero if we've already printed a "missing braces around initializer"
71    message within this initializer.  */
72 static int missing_braces_mentioned;
73
74 static int require_constant_value;
75 static int require_constant_elements;
76
77 static bool null_pointer_constant_p (const_tree);
78 static tree qualify_type (tree, tree);
79 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
80                                          bool *);
81 static int comp_target_types (location_t, tree, tree);
82 static int function_types_compatible_p (const_tree, const_tree, bool *,
83                                         bool *);
84 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
85 static tree lookup_field (tree, tree);
86 static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
87                               tree);
88 static tree pointer_diff (location_t, tree, tree);
89 static tree convert_for_assignment (location_t, tree, tree, tree,
90                                     enum impl_conv, bool, tree, tree, int);
91 static tree valid_compound_expr_initializer (tree, tree);
92 static void push_string (const char *);
93 static void push_member_name (tree);
94 static int spelling_length (void);
95 static char *print_spelling (char *);
96 static void warning_init (int, const char *);
97 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
98 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
99                                  struct obstack *);
100 static void output_pending_init_elements (int, struct obstack *);
101 static int set_designator (int, struct obstack *);
102 static void push_range_stack (tree, struct obstack *);
103 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
104 static void set_nonincremental_init (struct obstack *);
105 static void set_nonincremental_init_from_string (tree, struct obstack *);
106 static tree find_init_member (tree, struct obstack *);
107 static void readonly_error (tree, enum lvalue_use);
108 static void readonly_warning (tree, enum lvalue_use);
109 static int lvalue_or_else (const_tree, enum lvalue_use);
110 static void record_maybe_used_decl (tree);
111 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
112 \f
113 /* Return true if EXP is a null pointer constant, false otherwise.  */
114
115 static bool
116 null_pointer_constant_p (const_tree expr)
117 {
118   /* This should really operate on c_expr structures, but they aren't
119      yet available everywhere required.  */
120   tree type = TREE_TYPE (expr);
121   return (TREE_CODE (expr) == INTEGER_CST
122           && !TREE_OVERFLOW (expr)
123           && integer_zerop (expr)
124           && (INTEGRAL_TYPE_P (type)
125               || (TREE_CODE (type) == POINTER_TYPE
126                   && VOID_TYPE_P (TREE_TYPE (type))
127                   && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
128 }
129
130 /* EXPR may appear in an unevaluated part of an integer constant
131    expression, but not in an evaluated part.  Wrap it in a
132    C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
133    INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR.  */
134
135 static tree
136 note_integer_operands (tree expr)
137 {
138   tree ret;
139   if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
140     {
141       ret = copy_node (expr);
142       TREE_OVERFLOW (ret) = 1;
143     }
144   else
145     {
146       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
147       C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
148     }
149   return ret;
150 }
151
152 /* Having checked whether EXPR may appear in an unevaluated part of an
153    integer constant expression and found that it may, remove any
154    C_MAYBE_CONST_EXPR noting this fact and return the resulting
155    expression.  */
156
157 static inline tree
158 remove_c_maybe_const_expr (tree expr)
159 {
160   if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
161     return C_MAYBE_CONST_EXPR_EXPR (expr);
162   else
163     return expr;
164 }
165
166 \f/* This is a cache to hold if two types are compatible or not.  */
167
168 struct tagged_tu_seen_cache {
169   const struct tagged_tu_seen_cache * next;
170   const_tree t1;
171   const_tree t2;
172   /* The return value of tagged_types_tu_compatible_p if we had seen
173      these two types already.  */
174   int val;
175 };
176
177 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
178 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
179
180 /* Do `exp = require_complete_type (exp);' to make sure exp
181    does not have an incomplete type.  (That includes void types.)  */
182
183 tree
184 require_complete_type (tree value)
185 {
186   tree type = TREE_TYPE (value);
187
188   if (value == error_mark_node || type == error_mark_node)
189     return error_mark_node;
190
191   /* First, detect a valid value with a complete type.  */
192   if (COMPLETE_TYPE_P (type))
193     return value;
194
195   c_incomplete_type_error (value, type);
196   return error_mark_node;
197 }
198
199 /* Print an error message for invalid use of an incomplete type.
200    VALUE is the expression that was used (or 0 if that isn't known)
201    and TYPE is the type that was invalid.  */
202
203 void
204 c_incomplete_type_error (const_tree value, const_tree type)
205 {
206   const char *type_code_string;
207
208   /* Avoid duplicate error message.  */
209   if (TREE_CODE (type) == ERROR_MARK)
210     return;
211
212   if (value != 0 && (TREE_CODE (value) == VAR_DECL
213                      || TREE_CODE (value) == PARM_DECL))
214     error ("%qD has an incomplete type", value);
215   else
216     {
217     retry:
218       /* We must print an error message.  Be clever about what it says.  */
219
220       switch (TREE_CODE (type))
221         {
222         case RECORD_TYPE:
223           type_code_string = "struct";
224           break;
225
226         case UNION_TYPE:
227           type_code_string = "union";
228           break;
229
230         case ENUMERAL_TYPE:
231           type_code_string = "enum";
232           break;
233
234         case VOID_TYPE:
235           error ("invalid use of void expression");
236           return;
237
238         case ARRAY_TYPE:
239           if (TYPE_DOMAIN (type))
240             {
241               if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
242                 {
243                   error ("invalid use of flexible array member");
244                   return;
245                 }
246               type = TREE_TYPE (type);
247               goto retry;
248             }
249           error ("invalid use of array with unspecified bounds");
250           return;
251
252         default:
253           gcc_unreachable ();
254         }
255
256       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
257         error ("invalid use of undefined type %<%s %E%>",
258                type_code_string, TYPE_NAME (type));
259       else
260         /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL.  */
261         error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
262     }
263 }
264
265 /* Given a type, apply default promotions wrt unnamed function
266    arguments and return the new type.  */
267
268 tree
269 c_type_promotes_to (tree type)
270 {
271   if (TYPE_MAIN_VARIANT (type) == float_type_node)
272     return double_type_node;
273
274   if (c_promoting_integer_type_p (type))
275     {
276       /* Preserve unsignedness if not really getting any wider.  */
277       if (TYPE_UNSIGNED (type)
278           && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
279         return unsigned_type_node;
280       return integer_type_node;
281     }
282
283   return type;
284 }
285
286 /* Return true if between two named address spaces, whether there is a superset
287    named address space that encompasses both address spaces.  If there is a
288    superset, return which address space is the superset.  */
289
290 static bool
291 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
292 {
293   if (as1 == as2)
294     {
295       *common = as1;
296       return true;
297     }
298   else if (targetm.addr_space.subset_p (as1, as2))
299     {
300       *common = as2;
301       return true;
302     }
303   else if (targetm.addr_space.subset_p (as2, as1))
304     {
305       *common = as1;
306       return true;
307     }
308   else
309     return false;
310 }
311
312 /* Return a variant of TYPE which has all the type qualifiers of LIKE
313    as well as those of TYPE.  */
314
315 static tree
316 qualify_type (tree type, tree like)
317 {
318   addr_space_t as_type = TYPE_ADDR_SPACE (type);
319   addr_space_t as_like = TYPE_ADDR_SPACE (like);
320   addr_space_t as_common;
321
322   /* If the two named address spaces are different, determine the common
323      superset address space.  If there isn't one, raise an error.  */
324   if (!addr_space_superset (as_type, as_like, &as_common))
325     {
326       as_common = as_type;
327       error ("%qT and %qT are in disjoint named address spaces",
328              type, like);
329     }
330
331   return c_build_qualified_type (type,
332                                  TYPE_QUALS_NO_ADDR_SPACE (type)
333                                  | TYPE_QUALS_NO_ADDR_SPACE (like)
334                                  | ENCODE_QUAL_ADDR_SPACE (as_common));
335 }
336
337 /* Return true iff the given tree T is a variable length array.  */
338
339 bool
340 c_vla_type_p (const_tree t)
341 {
342   if (TREE_CODE (t) == ARRAY_TYPE
343       && C_TYPE_VARIABLE_SIZE (t))
344     return true;
345   return false;
346 }
347 \f
348 /* Return the composite type of two compatible types.
349
350    We assume that comptypes has already been done and returned
351    nonzero; if that isn't so, this may crash.  In particular, we
352    assume that qualifiers match.  */
353
354 tree
355 composite_type (tree t1, tree t2)
356 {
357   enum tree_code code1;
358   enum tree_code code2;
359   tree attributes;
360
361   /* Save time if the two types are the same.  */
362
363   if (t1 == t2) return t1;
364
365   /* If one type is nonsense, use the other.  */
366   if (t1 == error_mark_node)
367     return t2;
368   if (t2 == error_mark_node)
369     return t1;
370
371   code1 = TREE_CODE (t1);
372   code2 = TREE_CODE (t2);
373
374   /* Merge the attributes.  */
375   attributes = targetm.merge_type_attributes (t1, t2);
376
377   /* If one is an enumerated type and the other is the compatible
378      integer type, the composite type might be either of the two
379      (DR#013 question 3).  For consistency, use the enumerated type as
380      the composite type.  */
381
382   if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
383     return t1;
384   if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
385     return t2;
386
387   gcc_assert (code1 == code2);
388
389   switch (code1)
390     {
391     case POINTER_TYPE:
392       /* For two pointers, do this recursively on the target type.  */
393       {
394         tree pointed_to_1 = TREE_TYPE (t1);
395         tree pointed_to_2 = TREE_TYPE (t2);
396         tree target = composite_type (pointed_to_1, pointed_to_2);
397         t1 = build_pointer_type (target);
398         t1 = build_type_attribute_variant (t1, attributes);
399         return qualify_type (t1, t2);
400       }
401
402     case ARRAY_TYPE:
403       {
404         tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
405         int quals;
406         tree unqual_elt;
407         tree d1 = TYPE_DOMAIN (t1);
408         tree d2 = TYPE_DOMAIN (t2);
409         bool d1_variable, d2_variable;
410         bool d1_zero, d2_zero;
411         bool t1_complete, t2_complete;
412
413         /* We should not have any type quals on arrays at all.  */
414         gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
415                     && !TYPE_QUALS_NO_ADDR_SPACE (t2));
416
417         t1_complete = COMPLETE_TYPE_P (t1);
418         t2_complete = COMPLETE_TYPE_P (t2);
419
420         d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
421         d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
422
423         d1_variable = (!d1_zero
424                        && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
425                            || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
426         d2_variable = (!d2_zero
427                        && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
428                            || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
429         d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
430         d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
431
432         /* Save space: see if the result is identical to one of the args.  */
433         if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
434             && (d2_variable || d2_zero || !d1_variable))
435           return build_type_attribute_variant (t1, attributes);
436         if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
437             && (d1_variable || d1_zero || !d2_variable))
438           return build_type_attribute_variant (t2, attributes);
439
440         if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
441           return build_type_attribute_variant (t1, attributes);
442         if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443           return build_type_attribute_variant (t2, attributes);
444
445         /* Merge the element types, and have a size if either arg has
446            one.  We may have qualifiers on the element types.  To set
447            up TYPE_MAIN_VARIANT correctly, we need to form the
448            composite of the unqualified types and add the qualifiers
449            back at the end.  */
450         quals = TYPE_QUALS (strip_array_types (elt));
451         unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
452         t1 = build_array_type (unqual_elt,
453                                TYPE_DOMAIN ((TYPE_DOMAIN (t1)
454                                              && (d2_variable
455                                                  || d2_zero
456                                                  || !d1_variable))
457                                             ? t1
458                                             : t2));
459         /* Ensure a composite type involving a zero-length array type
460            is a zero-length type not an incomplete type.  */
461         if (d1_zero && d2_zero
462             && (t1_complete || t2_complete)
463             && !COMPLETE_TYPE_P (t1))
464           {
465             TYPE_SIZE (t1) = bitsize_zero_node;
466             TYPE_SIZE_UNIT (t1) = size_zero_node;
467           }
468         t1 = c_build_qualified_type (t1, quals);
469         return build_type_attribute_variant (t1, attributes);
470       }
471
472     case ENUMERAL_TYPE:
473     case RECORD_TYPE:
474     case UNION_TYPE:
475       if (attributes != NULL)
476         {
477           /* Try harder not to create a new aggregate type.  */
478           if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
479             return t1;
480           if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
481             return t2;
482         }
483       return build_type_attribute_variant (t1, attributes);
484
485     case FUNCTION_TYPE:
486       /* Function types: prefer the one that specified arg types.
487          If both do, merge the arg types.  Also merge the return types.  */
488       {
489         tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
490         tree p1 = TYPE_ARG_TYPES (t1);
491         tree p2 = TYPE_ARG_TYPES (t2);
492         int len;
493         tree newargs, n;
494         int i;
495
496         /* Save space: see if the result is identical to one of the args.  */
497         if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
498           return build_type_attribute_variant (t1, attributes);
499         if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
500           return build_type_attribute_variant (t2, attributes);
501
502         /* Simple way if one arg fails to specify argument types.  */
503         if (TYPE_ARG_TYPES (t1) == 0)
504          {
505             t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
506             t1 = build_type_attribute_variant (t1, attributes);
507             return qualify_type (t1, t2);
508          }
509         if (TYPE_ARG_TYPES (t2) == 0)
510          {
511            t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
512            t1 = build_type_attribute_variant (t1, attributes);
513            return qualify_type (t1, t2);
514          }
515
516         /* If both args specify argument types, we must merge the two
517            lists, argument by argument.  */
518         /* Tell global_bindings_p to return false so that variable_size
519            doesn't die on VLAs in parameter types.  */
520         c_override_global_bindings_to_false = true;
521
522         len = list_length (p1);
523         newargs = 0;
524
525         for (i = 0; i < len; i++)
526           newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
527
528         n = newargs;
529
530         for (; p1;
531              p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
532           {
533             /* A null type means arg type is not specified.
534                Take whatever the other function type has.  */
535             if (TREE_VALUE (p1) == 0)
536               {
537                 TREE_VALUE (n) = TREE_VALUE (p2);
538                 goto parm_done;
539               }
540             if (TREE_VALUE (p2) == 0)
541               {
542                 TREE_VALUE (n) = TREE_VALUE (p1);
543                 goto parm_done;
544               }
545
546             /* Given  wait (union {union wait *u; int *i} *)
547                and  wait (union wait *),
548                prefer  union wait *  as type of parm.  */
549             if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
550                 && TREE_VALUE (p1) != TREE_VALUE (p2))
551               {
552                 tree memb;
553                 tree mv2 = TREE_VALUE (p2);
554                 if (mv2 && mv2 != error_mark_node
555                     && TREE_CODE (mv2) != ARRAY_TYPE)
556                   mv2 = TYPE_MAIN_VARIANT (mv2);
557                 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
558                      memb; memb = DECL_CHAIN (memb))
559                   {
560                     tree mv3 = TREE_TYPE (memb);
561                     if (mv3 && mv3 != error_mark_node
562                         && TREE_CODE (mv3) != ARRAY_TYPE)
563                       mv3 = TYPE_MAIN_VARIANT (mv3);
564                     if (comptypes (mv3, mv2))
565                       {
566                         TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
567                                                          TREE_VALUE (p2));
568                         pedwarn (input_location, OPT_pedantic,
569                                  "function types not truly compatible in ISO C");
570                         goto parm_done;
571                       }
572                   }
573               }
574             if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
575                 && TREE_VALUE (p2) != TREE_VALUE (p1))
576               {
577                 tree memb;
578                 tree mv1 = TREE_VALUE (p1);
579                 if (mv1 && mv1 != error_mark_node
580                     && TREE_CODE (mv1) != ARRAY_TYPE)
581                   mv1 = TYPE_MAIN_VARIANT (mv1);
582                 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
583                      memb; memb = DECL_CHAIN (memb))
584                   {
585                     tree mv3 = TREE_TYPE (memb);
586                     if (mv3 && mv3 != error_mark_node
587                         && TREE_CODE (mv3) != ARRAY_TYPE)
588                       mv3 = TYPE_MAIN_VARIANT (mv3);
589                     if (comptypes (mv3, mv1))
590                       {
591                         TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
592                                                          TREE_VALUE (p1));
593                         pedwarn (input_location, OPT_pedantic,
594                                  "function types not truly compatible in ISO C");
595                         goto parm_done;
596                       }
597                   }
598               }
599             TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
600           parm_done: ;
601           }
602
603         c_override_global_bindings_to_false = false;
604         t1 = build_function_type (valtype, newargs);
605         t1 = qualify_type (t1, t2);
606         /* ... falls through ...  */
607       }
608
609     default:
610       return build_type_attribute_variant (t1, attributes);
611     }
612
613 }
614
615 /* Return the type of a conditional expression between pointers to
616    possibly differently qualified versions of compatible types.
617
618    We assume that comp_target_types has already been done and returned
619    nonzero; if that isn't so, this may crash.  */
620
621 static tree
622 common_pointer_type (tree t1, tree t2)
623 {
624   tree attributes;
625   tree pointed_to_1, mv1;
626   tree pointed_to_2, mv2;
627   tree target;
628   unsigned target_quals;
629   addr_space_t as1, as2, as_common;
630   int quals1, quals2;
631
632   /* Save time if the two types are the same.  */
633
634   if (t1 == t2) return t1;
635
636   /* If one type is nonsense, use the other.  */
637   if (t1 == error_mark_node)
638     return t2;
639   if (t2 == error_mark_node)
640     return t1;
641
642   gcc_assert (TREE_CODE (t1) == POINTER_TYPE
643               && TREE_CODE (t2) == POINTER_TYPE);
644
645   /* Merge the attributes.  */
646   attributes = targetm.merge_type_attributes (t1, t2);
647
648   /* Find the composite type of the target types, and combine the
649      qualifiers of the two types' targets.  Do not lose qualifiers on
650      array element types by taking the TYPE_MAIN_VARIANT.  */
651   mv1 = pointed_to_1 = TREE_TYPE (t1);
652   mv2 = pointed_to_2 = TREE_TYPE (t2);
653   if (TREE_CODE (mv1) != ARRAY_TYPE)
654     mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
655   if (TREE_CODE (mv2) != ARRAY_TYPE)
656     mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
657   target = composite_type (mv1, mv2);
658
659   /* For function types do not merge const qualifiers, but drop them
660      if used inconsistently.  The middle-end uses these to mark const
661      and noreturn functions.  */
662   quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
663   quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
664
665   if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
666     target_quals = (quals1 & quals2);
667   else
668     target_quals = (quals1 | quals2);
669
670   /* If the two named address spaces are different, determine the common
671      superset address space.  This is guaranteed to exist due to the
672      assumption that comp_target_type returned non-zero.  */
673   as1 = TYPE_ADDR_SPACE (pointed_to_1);
674   as2 = TYPE_ADDR_SPACE (pointed_to_2);
675   if (!addr_space_superset (as1, as2, &as_common))
676     gcc_unreachable ();
677
678   target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
679
680   t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
681   return build_type_attribute_variant (t1, attributes);
682 }
683
684 /* Return the common type for two arithmetic types under the usual
685    arithmetic conversions.  The default conversions have already been
686    applied, and enumerated types converted to their compatible integer
687    types.  The resulting type is unqualified and has no attributes.
688
689    This is the type for the result of most arithmetic operations
690    if the operands have the given two types.  */
691
692 static tree
693 c_common_type (tree t1, tree t2)
694 {
695   enum tree_code code1;
696   enum tree_code code2;
697
698   /* If one type is nonsense, use the other.  */
699   if (t1 == error_mark_node)
700     return t2;
701   if (t2 == error_mark_node)
702     return t1;
703
704   if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
705     t1 = TYPE_MAIN_VARIANT (t1);
706
707   if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
708     t2 = TYPE_MAIN_VARIANT (t2);
709
710   if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
711     t1 = build_type_attribute_variant (t1, NULL_TREE);
712
713   if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
714     t2 = build_type_attribute_variant (t2, NULL_TREE);
715
716   /* Save time if the two types are the same.  */
717
718   if (t1 == t2) return t1;
719
720   code1 = TREE_CODE (t1);
721   code2 = TREE_CODE (t2);
722
723   gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
724               || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
725               || code1 == INTEGER_TYPE);
726   gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
727               || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
728               || code2 == INTEGER_TYPE);
729
730   /* When one operand is a decimal float type, the other operand cannot be
731      a generic float type or a complex type.  We also disallow vector types
732      here.  */
733   if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
734       && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
735     {
736       if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
737         {
738           error ("can%'t mix operands of decimal float and vector types");
739           return error_mark_node;
740         }
741       if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
742         {
743           error ("can%'t mix operands of decimal float and complex types");
744           return error_mark_node;
745         }
746       if (code1 == REAL_TYPE && code2 == REAL_TYPE)
747         {
748           error ("can%'t mix operands of decimal float and other float types");
749           return error_mark_node;
750         }
751     }
752
753   /* If one type is a vector type, return that type.  (How the usual
754      arithmetic conversions apply to the vector types extension is not
755      precisely specified.)  */
756   if (code1 == VECTOR_TYPE)
757     return t1;
758
759   if (code2 == VECTOR_TYPE)
760     return t2;
761
762   /* If one type is complex, form the common type of the non-complex
763      components, then make that complex.  Use T1 or T2 if it is the
764      required type.  */
765   if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
766     {
767       tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
768       tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
769       tree subtype = c_common_type (subtype1, subtype2);
770
771       if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
772         return t1;
773       else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
774         return t2;
775       else
776         return build_complex_type (subtype);
777     }
778
779   /* If only one is real, use it as the result.  */
780
781   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
782     return t1;
783
784   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
785     return t2;
786
787   /* If both are real and either are decimal floating point types, use
788      the decimal floating point type with the greater precision. */
789
790   if (code1 == REAL_TYPE && code2 == REAL_TYPE)
791     {
792       if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
793           || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
794         return dfloat128_type_node;
795       else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
796                || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
797         return dfloat64_type_node;
798       else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
799                || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
800         return dfloat32_type_node;
801     }
802
803   /* Deal with fixed-point types.  */
804   if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
805     {
806       unsigned int unsignedp = 0, satp = 0;
807       enum machine_mode m1, m2;
808       unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
809
810       m1 = TYPE_MODE (t1);
811       m2 = TYPE_MODE (t2);
812
813       /* If one input type is saturating, the result type is saturating.  */
814       if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
815         satp = 1;
816
817       /* If both fixed-point types are unsigned, the result type is unsigned.
818          When mixing fixed-point and integer types, follow the sign of the
819          fixed-point type.
820          Otherwise, the result type is signed.  */
821       if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
822            && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
823           || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
824               && TYPE_UNSIGNED (t1))
825           || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
826               && TYPE_UNSIGNED (t2)))
827         unsignedp = 1;
828
829       /* The result type is signed.  */
830       if (unsignedp == 0)
831         {
832           /* If the input type is unsigned, we need to convert to the
833              signed type.  */
834           if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
835             {
836               enum mode_class mclass = (enum mode_class) 0;
837               if (GET_MODE_CLASS (m1) == MODE_UFRACT)
838                 mclass = MODE_FRACT;
839               else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
840                 mclass = MODE_ACCUM;
841               else
842                 gcc_unreachable ();
843               m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
844             }
845           if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
846             {
847               enum mode_class mclass = (enum mode_class) 0;
848               if (GET_MODE_CLASS (m2) == MODE_UFRACT)
849                 mclass = MODE_FRACT;
850               else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
851                 mclass = MODE_ACCUM;
852               else
853                 gcc_unreachable ();
854               m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
855             }
856         }
857
858       if (code1 == FIXED_POINT_TYPE)
859         {
860           fbit1 = GET_MODE_FBIT (m1);
861           ibit1 = GET_MODE_IBIT (m1);
862         }
863       else
864         {
865           fbit1 = 0;
866           /* Signed integers need to subtract one sign bit.  */
867           ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
868         }
869
870       if (code2 == FIXED_POINT_TYPE)
871         {
872           fbit2 = GET_MODE_FBIT (m2);
873           ibit2 = GET_MODE_IBIT (m2);
874         }
875       else
876         {
877           fbit2 = 0;
878           /* Signed integers need to subtract one sign bit.  */
879           ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
880         }
881
882       max_ibit = ibit1 >= ibit2 ?  ibit1 : ibit2;
883       max_fbit = fbit1 >= fbit2 ?  fbit1 : fbit2;
884       return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
885                                                  satp);
886     }
887
888   /* Both real or both integers; use the one with greater precision.  */
889
890   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
891     return t1;
892   else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
893     return t2;
894
895   /* Same precision.  Prefer long longs to longs to ints when the
896      same precision, following the C99 rules on integer type rank
897      (which are equivalent to the C90 rules for C90 types).  */
898
899   if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
900       || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
901     return long_long_unsigned_type_node;
902
903   if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
904       || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
905     {
906       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
907         return long_long_unsigned_type_node;
908       else
909         return long_long_integer_type_node;
910     }
911
912   if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
913       || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
914     return long_unsigned_type_node;
915
916   if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
917       || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
918     {
919       /* But preserve unsignedness from the other type,
920          since long cannot hold all the values of an unsigned int.  */
921       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
922         return long_unsigned_type_node;
923       else
924         return long_integer_type_node;
925     }
926
927   /* Likewise, prefer long double to double even if same size.  */
928   if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
929       || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
930     return long_double_type_node;
931
932   /* Otherwise prefer the unsigned one.  */
933
934   if (TYPE_UNSIGNED (t1))
935     return t1;
936   else
937     return t2;
938 }
939 \f
940 /* Wrapper around c_common_type that is used by c-common.c and other
941    front end optimizations that remove promotions.  ENUMERAL_TYPEs
942    are allowed here and are converted to their compatible integer types.
943    BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
944    preferably a non-Boolean type as the common type.  */
945 tree
946 common_type (tree t1, tree t2)
947 {
948   if (TREE_CODE (t1) == ENUMERAL_TYPE)
949     t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
950   if (TREE_CODE (t2) == ENUMERAL_TYPE)
951     t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
952
953   /* If both types are BOOLEAN_TYPE, then return boolean_type_node.  */
954   if (TREE_CODE (t1) == BOOLEAN_TYPE
955       && TREE_CODE (t2) == BOOLEAN_TYPE)
956     return boolean_type_node;
957
958   /* If either type is BOOLEAN_TYPE, then return the other.  */
959   if (TREE_CODE (t1) == BOOLEAN_TYPE)
960     return t2;
961   if (TREE_CODE (t2) == BOOLEAN_TYPE)
962     return t1;
963
964   return c_common_type (t1, t2);
965 }
966
967 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
968    or various other operations.  Return 2 if they are compatible
969    but a warning may be needed if you use them together.  */
970
971 int
972 comptypes (tree type1, tree type2)
973 {
974   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
975   int val;
976
977   val = comptypes_internal (type1, type2, NULL, NULL);
978   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
979
980   return val;
981 }
982
983 /* Like comptypes, but if it returns non-zero because enum and int are
984    compatible, it sets *ENUM_AND_INT_P to true.  */
985
986 static int
987 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
988 {
989   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
990   int val;
991
992   val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
993   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
994
995   return val;
996 }
997
998 /* Like comptypes, but if it returns nonzero for different types, it
999    sets *DIFFERENT_TYPES_P to true.  */
1000
1001 int
1002 comptypes_check_different_types (tree type1, tree type2,
1003                                  bool *different_types_p)
1004 {
1005   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1006   int val;
1007
1008   val = comptypes_internal (type1, type2, NULL, different_types_p);
1009   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1010
1011   return val;
1012 }
1013 \f
1014 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1015    or various other operations.  Return 2 if they are compatible
1016    but a warning may be needed if you use them together.  If
1017    ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1018    compatible integer type, then this sets *ENUM_AND_INT_P to true;
1019    *ENUM_AND_INT_P is never set to false.  If DIFFERENT_TYPES_P is not
1020    NULL, and the types are compatible but different enough not to be
1021    permitted in C1X typedef redeclarations, then this sets
1022    *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1023    false, but may or may not be set if the types are incompatible.
1024    This differs from comptypes, in that we don't free the seen
1025    types.  */
1026
1027 static int
1028 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1029                     bool *different_types_p)
1030 {
1031   const_tree t1 = type1;
1032   const_tree t2 = type2;
1033   int attrval, val;
1034
1035   /* Suppress errors caused by previously reported errors.  */
1036
1037   if (t1 == t2 || !t1 || !t2
1038       || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1039     return 1;
1040
1041   /* Enumerated types are compatible with integer types, but this is
1042      not transitive: two enumerated types in the same translation unit
1043      are compatible with each other only if they are the same type.  */
1044
1045   if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1046     {
1047       t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1048       if (TREE_CODE (t2) != VOID_TYPE)
1049         {
1050           if (enum_and_int_p != NULL)
1051             *enum_and_int_p = true;
1052           if (different_types_p != NULL)
1053             *different_types_p = true;
1054         }
1055     }
1056   else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1057     {
1058       t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1059       if (TREE_CODE (t1) != VOID_TYPE)
1060         {
1061           if (enum_and_int_p != NULL)
1062             *enum_and_int_p = true;
1063           if (different_types_p != NULL)
1064             *different_types_p = true;
1065         }
1066     }
1067
1068   if (t1 == t2)
1069     return 1;
1070
1071   /* Different classes of types can't be compatible.  */
1072
1073   if (TREE_CODE (t1) != TREE_CODE (t2))
1074     return 0;
1075
1076   /* Qualifiers must match. C99 6.7.3p9 */
1077
1078   if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1079     return 0;
1080
1081   /* Allow for two different type nodes which have essentially the same
1082      definition.  Note that we already checked for equality of the type
1083      qualifiers (just above).  */
1084
1085   if (TREE_CODE (t1) != ARRAY_TYPE
1086       && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1087     return 1;
1088
1089   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1090   if (!(attrval = targetm.comp_type_attributes (t1, t2)))
1091      return 0;
1092
1093   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1094   val = 0;
1095
1096   switch (TREE_CODE (t1))
1097     {
1098     case POINTER_TYPE:
1099       /* Do not remove mode or aliasing information.  */
1100       if (TYPE_MODE (t1) != TYPE_MODE (t2)
1101           || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1102         break;
1103       val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1104              ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1105                                        enum_and_int_p, different_types_p));
1106       break;
1107
1108     case FUNCTION_TYPE:
1109       val = function_types_compatible_p (t1, t2, enum_and_int_p,
1110                                          different_types_p);
1111       break;
1112
1113     case ARRAY_TYPE:
1114       {
1115         tree d1 = TYPE_DOMAIN (t1);
1116         tree d2 = TYPE_DOMAIN (t2);
1117         bool d1_variable, d2_variable;
1118         bool d1_zero, d2_zero;
1119         val = 1;
1120
1121         /* Target types must match incl. qualifiers.  */
1122         if (TREE_TYPE (t1) != TREE_TYPE (t2)
1123             && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1124                                                enum_and_int_p,
1125                                                different_types_p)))
1126           return 0;
1127
1128         if (different_types_p != NULL
1129             && (d1 == 0) != (d2 == 0))
1130           *different_types_p = true;
1131         /* Sizes must match unless one is missing or variable.  */
1132         if (d1 == 0 || d2 == 0 || d1 == d2)
1133           break;
1134
1135         d1_zero = !TYPE_MAX_VALUE (d1);
1136         d2_zero = !TYPE_MAX_VALUE (d2);
1137
1138         d1_variable = (!d1_zero
1139                        && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1140                            || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1141         d2_variable = (!d2_zero
1142                        && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1143                            || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1144         d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1145         d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1146
1147         if (different_types_p != NULL
1148             && d1_variable != d2_variable)
1149           *different_types_p = true;
1150         if (d1_variable || d2_variable)
1151           break;
1152         if (d1_zero && d2_zero)
1153           break;
1154         if (d1_zero || d2_zero
1155             || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1156             || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1157           val = 0;
1158
1159         break;
1160       }
1161
1162     case ENUMERAL_TYPE:
1163     case RECORD_TYPE:
1164     case UNION_TYPE:
1165       if (val != 1 && !same_translation_unit_p (t1, t2))
1166         {
1167           tree a1 = TYPE_ATTRIBUTES (t1);
1168           tree a2 = TYPE_ATTRIBUTES (t2);
1169
1170           if (! attribute_list_contained (a1, a2)
1171               && ! attribute_list_contained (a2, a1))
1172             break;
1173
1174           if (attrval != 2)
1175             return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1176                                                  different_types_p);
1177           val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1178                                               different_types_p);
1179         }
1180       break;
1181
1182     case VECTOR_TYPE:
1183       val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1184              && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1185                                     enum_and_int_p, different_types_p));
1186       break;
1187
1188     default:
1189       break;
1190     }
1191   return attrval == 2 && val == 1 ? 2 : val;
1192 }
1193
1194 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1195    their qualifiers, except for named address spaces.  If the pointers point to
1196    different named addresses, then we must determine if one address space is a
1197    subset of the other.  */
1198
1199 static int
1200 comp_target_types (location_t location, tree ttl, tree ttr)
1201 {
1202   int val;
1203   tree mvl = TREE_TYPE (ttl);
1204   tree mvr = TREE_TYPE (ttr);
1205   addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1206   addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1207   addr_space_t as_common;
1208   bool enum_and_int_p;
1209
1210   /* Fail if pointers point to incompatible address spaces.  */
1211   if (!addr_space_superset (asl, asr, &as_common))
1212     return 0;
1213
1214   /* Do not lose qualifiers on element types of array types that are
1215      pointer targets by taking their TYPE_MAIN_VARIANT.  */
1216   if (TREE_CODE (mvl) != ARRAY_TYPE)
1217     mvl = TYPE_MAIN_VARIANT (mvl);
1218   if (TREE_CODE (mvr) != ARRAY_TYPE)
1219     mvr = TYPE_MAIN_VARIANT (mvr);
1220   enum_and_int_p = false;
1221   val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1222
1223   if (val == 2)
1224     pedwarn (location, OPT_pedantic, "types are not quite compatible");
1225
1226   if (val == 1 && enum_and_int_p && warn_cxx_compat)
1227     warning_at (location, OPT_Wc___compat,
1228                 "pointer target types incompatible in C++");
1229
1230   return val;
1231 }
1232 \f
1233 /* Subroutines of `comptypes'.  */
1234
1235 /* Determine whether two trees derive from the same translation unit.
1236    If the CONTEXT chain ends in a null, that tree's context is still
1237    being parsed, so if two trees have context chains ending in null,
1238    they're in the same translation unit.  */
1239 int
1240 same_translation_unit_p (const_tree t1, const_tree t2)
1241 {
1242   while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1243     switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1244       {
1245       case tcc_declaration:
1246         t1 = DECL_CONTEXT (t1); break;
1247       case tcc_type:
1248         t1 = TYPE_CONTEXT (t1); break;
1249       case tcc_exceptional:
1250         t1 = BLOCK_SUPERCONTEXT (t1); break;  /* assume block */
1251       default: gcc_unreachable ();
1252       }
1253
1254   while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1255     switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1256       {
1257       case tcc_declaration:
1258         t2 = DECL_CONTEXT (t2); break;
1259       case tcc_type:
1260         t2 = TYPE_CONTEXT (t2); break;
1261       case tcc_exceptional:
1262         t2 = BLOCK_SUPERCONTEXT (t2); break;  /* assume block */
1263       default: gcc_unreachable ();
1264       }
1265
1266   return t1 == t2;
1267 }
1268
1269 /* Allocate the seen two types, assuming that they are compatible. */
1270
1271 static struct tagged_tu_seen_cache *
1272 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1273 {
1274   struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1275   tu->next = tagged_tu_seen_base;
1276   tu->t1 = t1;
1277   tu->t2 = t2;
1278
1279   tagged_tu_seen_base = tu;
1280
1281   /* The C standard says that two structures in different translation
1282      units are compatible with each other only if the types of their
1283      fields are compatible (among other things).  We assume that they
1284      are compatible until proven otherwise when building the cache.
1285      An example where this can occur is:
1286      struct a
1287      {
1288        struct a *next;
1289      };
1290      If we are comparing this against a similar struct in another TU,
1291      and did not assume they were compatible, we end up with an infinite
1292      loop.  */
1293   tu->val = 1;
1294   return tu;
1295 }
1296
1297 /* Free the seen types until we get to TU_TIL. */
1298
1299 static void
1300 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1301 {
1302   const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1303   while (tu != tu_til)
1304     {
1305       const struct tagged_tu_seen_cache *const tu1
1306         = (const struct tagged_tu_seen_cache *) tu;
1307       tu = tu1->next;
1308       free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1309     }
1310   tagged_tu_seen_base = tu_til;
1311 }
1312
1313 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1314    compatible.  If the two types are not the same (which has been
1315    checked earlier), this can only happen when multiple translation
1316    units are being compiled.  See C99 6.2.7 paragraph 1 for the exact
1317    rules.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1318    comptypes_internal.  */
1319
1320 static int
1321 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1322                               bool *enum_and_int_p, bool *different_types_p)
1323 {
1324   tree s1, s2;
1325   bool needs_warning = false;
1326
1327   /* We have to verify that the tags of the types are the same.  This
1328      is harder than it looks because this may be a typedef, so we have
1329      to go look at the original type.  It may even be a typedef of a
1330      typedef...
1331      In the case of compiler-created builtin structs the TYPE_DECL
1332      may be a dummy, with no DECL_ORIGINAL_TYPE.  Don't fault.  */
1333   while (TYPE_NAME (t1)
1334          && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1335          && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1336     t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1337
1338   while (TYPE_NAME (t2)
1339          && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1340          && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1341     t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1342
1343   /* C90 didn't have the requirement that the two tags be the same.  */
1344   if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1345     return 0;
1346
1347   /* C90 didn't say what happened if one or both of the types were
1348      incomplete; we choose to follow C99 rules here, which is that they
1349      are compatible.  */
1350   if (TYPE_SIZE (t1) == NULL
1351       || TYPE_SIZE (t2) == NULL)
1352     return 1;
1353
1354   {
1355     const struct tagged_tu_seen_cache * tts_i;
1356     for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1357       if (tts_i->t1 == t1 && tts_i->t2 == t2)
1358         return tts_i->val;
1359   }
1360
1361   switch (TREE_CODE (t1))
1362     {
1363     case ENUMERAL_TYPE:
1364       {
1365         struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1366         /* Speed up the case where the type values are in the same order.  */
1367         tree tv1 = TYPE_VALUES (t1);
1368         tree tv2 = TYPE_VALUES (t2);
1369
1370         if (tv1 == tv2)
1371           {
1372             return 1;
1373           }
1374
1375         for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1376           {
1377             if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1378               break;
1379             if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1380               {
1381                 tu->val = 0;
1382                 return 0;
1383               }
1384           }
1385
1386         if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1387           {
1388             return 1;
1389           }
1390         if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1391           {
1392             tu->val = 0;
1393             return 0;
1394           }
1395
1396         if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1397           {
1398             tu->val = 0;
1399             return 0;
1400           }
1401
1402         for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1403           {
1404             s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1405             if (s2 == NULL
1406                 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1407               {
1408                 tu->val = 0;
1409                 return 0;
1410               }
1411           }
1412         return 1;
1413       }
1414
1415     case UNION_TYPE:
1416       {
1417         struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1418         if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1419           {
1420             tu->val = 0;
1421             return 0;
1422           }
1423
1424         /*  Speed up the common case where the fields are in the same order. */
1425         for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1426              s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1427           {
1428             int result;
1429
1430             if (DECL_NAME (s1) != DECL_NAME (s2))
1431               break;
1432             result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1433                                          enum_and_int_p, different_types_p);
1434
1435             if (result != 1 && !DECL_NAME (s1))
1436               break;
1437             if (result == 0)
1438               {
1439                 tu->val = 0;
1440                 return 0;
1441               }
1442             if (result == 2)
1443               needs_warning = true;
1444
1445             if (TREE_CODE (s1) == FIELD_DECL
1446                 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1447                                      DECL_FIELD_BIT_OFFSET (s2)) != 1)
1448               {
1449                 tu->val = 0;
1450                 return 0;
1451               }
1452           }
1453         if (!s1 && !s2)
1454           {
1455             tu->val = needs_warning ? 2 : 1;
1456             return tu->val;
1457           }
1458
1459         for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1460           {
1461             bool ok = false;
1462
1463             for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1464               if (DECL_NAME (s1) == DECL_NAME (s2))
1465                 {
1466                   int result;
1467
1468                   result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1469                                                enum_and_int_p,
1470                                                different_types_p);
1471
1472                   if (result != 1 && !DECL_NAME (s1))
1473                     continue;
1474                   if (result == 0)
1475                     {
1476                       tu->val = 0;
1477                       return 0;
1478                     }
1479                   if (result == 2)
1480                     needs_warning = true;
1481
1482                   if (TREE_CODE (s1) == FIELD_DECL
1483                       && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1484                                            DECL_FIELD_BIT_OFFSET (s2)) != 1)
1485                     break;
1486
1487                   ok = true;
1488                   break;
1489                 }
1490             if (!ok)
1491               {
1492                 tu->val = 0;
1493                 return 0;
1494               }
1495           }
1496         tu->val = needs_warning ? 2 : 10;
1497         return tu->val;
1498       }
1499
1500     case RECORD_TYPE:
1501       {
1502         struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1503
1504         for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1505              s1 && s2;
1506              s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1507           {
1508             int result;
1509             if (TREE_CODE (s1) != TREE_CODE (s2)
1510                 || DECL_NAME (s1) != DECL_NAME (s2))
1511               break;
1512             result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1513                                          enum_and_int_p, different_types_p);
1514             if (result == 0)
1515               break;
1516             if (result == 2)
1517               needs_warning = true;
1518
1519             if (TREE_CODE (s1) == FIELD_DECL
1520                 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1521                                      DECL_FIELD_BIT_OFFSET (s2)) != 1)
1522               break;
1523           }
1524         if (s1 && s2)
1525           tu->val = 0;
1526         else
1527           tu->val = needs_warning ? 2 : 1;
1528         return tu->val;
1529       }
1530
1531     default:
1532       gcc_unreachable ();
1533     }
1534 }
1535
1536 /* Return 1 if two function types F1 and F2 are compatible.
1537    If either type specifies no argument types,
1538    the other must specify a fixed number of self-promoting arg types.
1539    Otherwise, if one type specifies only the number of arguments,
1540    the other must specify that number of self-promoting arg types.
1541    Otherwise, the argument types must match.
1542    ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal.  */
1543
1544 static int
1545 function_types_compatible_p (const_tree f1, const_tree f2,
1546                              bool *enum_and_int_p, bool *different_types_p)
1547 {
1548   tree args1, args2;
1549   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1550   int val = 1;
1551   int val1;
1552   tree ret1, ret2;
1553
1554   ret1 = TREE_TYPE (f1);
1555   ret2 = TREE_TYPE (f2);
1556
1557   /* 'volatile' qualifiers on a function's return type used to mean
1558      the function is noreturn.  */
1559   if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1560     pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1561   if (TYPE_VOLATILE (ret1))
1562     ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1563                                  TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1564   if (TYPE_VOLATILE (ret2))
1565     ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1566                                  TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1567   val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1568   if (val == 0)
1569     return 0;
1570
1571   args1 = TYPE_ARG_TYPES (f1);
1572   args2 = TYPE_ARG_TYPES (f2);
1573
1574   if (different_types_p != NULL
1575       && (args1 == 0) != (args2 == 0))
1576     *different_types_p = true;
1577
1578   /* An unspecified parmlist matches any specified parmlist
1579      whose argument types don't need default promotions.  */
1580
1581   if (args1 == 0)
1582     {
1583       if (!self_promoting_args_p (args2))
1584         return 0;
1585       /* If one of these types comes from a non-prototype fn definition,
1586          compare that with the other type's arglist.
1587          If they don't match, ask for a warning (but no error).  */
1588       if (TYPE_ACTUAL_ARG_TYPES (f1)
1589           && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1590                                            enum_and_int_p, different_types_p))
1591         val = 2;
1592       return val;
1593     }
1594   if (args2 == 0)
1595     {
1596       if (!self_promoting_args_p (args1))
1597         return 0;
1598       if (TYPE_ACTUAL_ARG_TYPES (f2)
1599           && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1600                                            enum_and_int_p, different_types_p))
1601         val = 2;
1602       return val;
1603     }
1604
1605   /* Both types have argument lists: compare them and propagate results.  */
1606   val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1607                                   different_types_p);
1608   return val1 != 1 ? val1 : val;
1609 }
1610
1611 /* Check two lists of types for compatibility, returning 0 for
1612    incompatible, 1 for compatible, or 2 for compatible with
1613    warning.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1614    comptypes_internal.  */
1615
1616 static int
1617 type_lists_compatible_p (const_tree args1, const_tree args2,
1618                          bool *enum_and_int_p, bool *different_types_p)
1619 {
1620   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1621   int val = 1;
1622   int newval = 0;
1623
1624   while (1)
1625     {
1626       tree a1, mv1, a2, mv2;
1627       if (args1 == 0 && args2 == 0)
1628         return val;
1629       /* If one list is shorter than the other,
1630          they fail to match.  */
1631       if (args1 == 0 || args2 == 0)
1632         return 0;
1633       mv1 = a1 = TREE_VALUE (args1);
1634       mv2 = a2 = TREE_VALUE (args2);
1635       if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1636         mv1 = TYPE_MAIN_VARIANT (mv1);
1637       if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1638         mv2 = TYPE_MAIN_VARIANT (mv2);
1639       /* A null pointer instead of a type
1640          means there is supposed to be an argument
1641          but nothing is specified about what type it has.
1642          So match anything that self-promotes.  */
1643       if (different_types_p != NULL
1644           && (a1 == 0) != (a2 == 0))
1645         *different_types_p = true;
1646       if (a1 == 0)
1647         {
1648           if (c_type_promotes_to (a2) != a2)
1649             return 0;
1650         }
1651       else if (a2 == 0)
1652         {
1653           if (c_type_promotes_to (a1) != a1)
1654             return 0;
1655         }
1656       /* If one of the lists has an error marker, ignore this arg.  */
1657       else if (TREE_CODE (a1) == ERROR_MARK
1658                || TREE_CODE (a2) == ERROR_MARK)
1659         ;
1660       else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1661                                               different_types_p)))
1662         {
1663           if (different_types_p != NULL)
1664             *different_types_p = true;
1665           /* Allow  wait (union {union wait *u; int *i} *)
1666              and  wait (union wait *)  to be compatible.  */
1667           if (TREE_CODE (a1) == UNION_TYPE
1668               && (TYPE_NAME (a1) == 0
1669                   || TYPE_TRANSPARENT_AGGR (a1))
1670               && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1671               && tree_int_cst_equal (TYPE_SIZE (a1),
1672                                      TYPE_SIZE (a2)))
1673             {
1674               tree memb;
1675               for (memb = TYPE_FIELDS (a1);
1676                    memb; memb = DECL_CHAIN (memb))
1677                 {
1678                   tree mv3 = TREE_TYPE (memb);
1679                   if (mv3 && mv3 != error_mark_node
1680                       && TREE_CODE (mv3) != ARRAY_TYPE)
1681                     mv3 = TYPE_MAIN_VARIANT (mv3);
1682                   if (comptypes_internal (mv3, mv2, enum_and_int_p,
1683                                           different_types_p))
1684                     break;
1685                 }
1686               if (memb == 0)
1687                 return 0;
1688             }
1689           else if (TREE_CODE (a2) == UNION_TYPE
1690                    && (TYPE_NAME (a2) == 0
1691                        || TYPE_TRANSPARENT_AGGR (a2))
1692                    && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1693                    && tree_int_cst_equal (TYPE_SIZE (a2),
1694                                           TYPE_SIZE (a1)))
1695             {
1696               tree memb;
1697               for (memb = TYPE_FIELDS (a2);
1698                    memb; memb = DECL_CHAIN (memb))
1699                 {
1700                   tree mv3 = TREE_TYPE (memb);
1701                   if (mv3 && mv3 != error_mark_node
1702                       && TREE_CODE (mv3) != ARRAY_TYPE)
1703                     mv3 = TYPE_MAIN_VARIANT (mv3);
1704                   if (comptypes_internal (mv3, mv1, enum_and_int_p,
1705                                           different_types_p))
1706                     break;
1707                 }
1708               if (memb == 0)
1709                 return 0;
1710             }
1711           else
1712             return 0;
1713         }
1714
1715       /* comptypes said ok, but record if it said to warn.  */
1716       if (newval > val)
1717         val = newval;
1718
1719       args1 = TREE_CHAIN (args1);
1720       args2 = TREE_CHAIN (args2);
1721     }
1722 }
1723 \f
1724 /* Compute the size to increment a pointer by.  */
1725
1726 static tree
1727 c_size_in_bytes (const_tree type)
1728 {
1729   enum tree_code code = TREE_CODE (type);
1730
1731   if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1732     return size_one_node;
1733
1734   if (!COMPLETE_OR_VOID_TYPE_P (type))
1735     {
1736       error ("arithmetic on pointer to an incomplete type");
1737       return size_one_node;
1738     }
1739
1740   /* Convert in case a char is more than one unit.  */
1741   return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1742                          size_int (TYPE_PRECISION (char_type_node)
1743                                    / BITS_PER_UNIT));
1744 }
1745 \f
1746 /* Return either DECL or its known constant value (if it has one).  */
1747
1748 tree
1749 decl_constant_value (tree decl)
1750 {
1751   if (/* Don't change a variable array bound or initial value to a constant
1752          in a place where a variable is invalid.  Note that DECL_INITIAL
1753          isn't valid for a PARM_DECL.  */
1754       current_function_decl != 0
1755       && TREE_CODE (decl) != PARM_DECL
1756       && !TREE_THIS_VOLATILE (decl)
1757       && TREE_READONLY (decl)
1758       && DECL_INITIAL (decl) != 0
1759       && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1760       /* This is invalid if initial value is not constant.
1761          If it has either a function call, a memory reference,
1762          or a variable, then re-evaluating it could give different results.  */
1763       && TREE_CONSTANT (DECL_INITIAL (decl))
1764       /* Check for cases where this is sub-optimal, even though valid.  */
1765       && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1766     return DECL_INITIAL (decl);
1767   return decl;
1768 }
1769
1770 /* Convert the array expression EXP to a pointer.  */
1771 static tree
1772 array_to_pointer_conversion (location_t loc, tree exp)
1773 {
1774   tree orig_exp = exp;
1775   tree type = TREE_TYPE (exp);
1776   tree adr;
1777   tree restype = TREE_TYPE (type);
1778   tree ptrtype;
1779
1780   gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1781
1782   STRIP_TYPE_NOPS (exp);
1783
1784   if (TREE_NO_WARNING (orig_exp))
1785     TREE_NO_WARNING (exp) = 1;
1786
1787   ptrtype = build_pointer_type (restype);
1788
1789   if (TREE_CODE (exp) == INDIRECT_REF)
1790     return convert (ptrtype, TREE_OPERAND (exp, 0));
1791
1792   adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1793   return convert (ptrtype, adr);
1794 }
1795
1796 /* Convert the function expression EXP to a pointer.  */
1797 static tree
1798 function_to_pointer_conversion (location_t loc, tree exp)
1799 {
1800   tree orig_exp = exp;
1801
1802   gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1803
1804   STRIP_TYPE_NOPS (exp);
1805
1806   if (TREE_NO_WARNING (orig_exp))
1807     TREE_NO_WARNING (exp) = 1;
1808
1809   return build_unary_op (loc, ADDR_EXPR, exp, 0);
1810 }
1811
1812 /* Mark EXP as read, not just set, for set but not used -Wunused
1813    warning purposes.  */
1814
1815 void
1816 mark_exp_read (tree exp)
1817 {
1818   switch (TREE_CODE (exp))
1819     {
1820     case VAR_DECL:
1821     case PARM_DECL:
1822       DECL_READ_P (exp) = 1;
1823       break;
1824     case ARRAY_REF:
1825     case COMPONENT_REF:
1826     case MODIFY_EXPR:
1827     case REALPART_EXPR:
1828     case IMAGPART_EXPR:
1829     CASE_CONVERT:
1830     case ADDR_EXPR:
1831       mark_exp_read (TREE_OPERAND (exp, 0));
1832       break;
1833     case COMPOUND_EXPR:
1834     case C_MAYBE_CONST_EXPR:
1835       mark_exp_read (TREE_OPERAND (exp, 1));
1836       break;
1837     default:
1838       break;
1839     }
1840 }
1841
1842 /* Perform the default conversion of arrays and functions to pointers.
1843    Return the result of converting EXP.  For any other expression, just
1844    return EXP.
1845
1846    LOC is the location of the expression.  */
1847
1848 struct c_expr
1849 default_function_array_conversion (location_t loc, struct c_expr exp)
1850 {
1851   tree orig_exp = exp.value;
1852   tree type = TREE_TYPE (exp.value);
1853   enum tree_code code = TREE_CODE (type);
1854
1855   switch (code)
1856     {
1857     case ARRAY_TYPE:
1858       {
1859         bool not_lvalue = false;
1860         bool lvalue_array_p;
1861
1862         while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1863                 || CONVERT_EXPR_P (exp.value))
1864                && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1865           {
1866             if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1867               not_lvalue = true;
1868             exp.value = TREE_OPERAND (exp.value, 0);
1869           }
1870
1871         if (TREE_NO_WARNING (orig_exp))
1872           TREE_NO_WARNING (exp.value) = 1;
1873
1874         lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1875         if (!flag_isoc99 && !lvalue_array_p)
1876           {
1877             /* Before C99, non-lvalue arrays do not decay to pointers.
1878                Normally, using such an array would be invalid; but it can
1879                be used correctly inside sizeof or as a statement expression.
1880                Thus, do not give an error here; an error will result later.  */
1881             return exp;
1882           }
1883
1884         exp.value = array_to_pointer_conversion (loc, exp.value);
1885       }
1886       break;
1887     case FUNCTION_TYPE:
1888       exp.value = function_to_pointer_conversion (loc, exp.value);
1889       break;
1890     default:
1891       break;
1892     }
1893
1894   return exp;
1895 }
1896
1897 struct c_expr
1898 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1899 {
1900   mark_exp_read (exp.value);
1901   return default_function_array_conversion (loc, exp);
1902 }
1903
1904 /* EXP is an expression of integer type.  Apply the integer promotions
1905    to it and return the promoted value.  */
1906
1907 tree
1908 perform_integral_promotions (tree exp)
1909 {
1910   tree type = TREE_TYPE (exp);
1911   enum tree_code code = TREE_CODE (type);
1912
1913   gcc_assert (INTEGRAL_TYPE_P (type));
1914
1915   /* Normally convert enums to int,
1916      but convert wide enums to something wider.  */
1917   if (code == ENUMERAL_TYPE)
1918     {
1919       type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1920                                           TYPE_PRECISION (integer_type_node)),
1921                                      ((TYPE_PRECISION (type)
1922                                        >= TYPE_PRECISION (integer_type_node))
1923                                       && TYPE_UNSIGNED (type)));
1924
1925       return convert (type, exp);
1926     }
1927
1928   /* ??? This should no longer be needed now bit-fields have their
1929      proper types.  */
1930   if (TREE_CODE (exp) == COMPONENT_REF
1931       && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1932       /* If it's thinner than an int, promote it like a
1933          c_promoting_integer_type_p, otherwise leave it alone.  */
1934       && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1935                                TYPE_PRECISION (integer_type_node)))
1936     return convert (integer_type_node, exp);
1937
1938   if (c_promoting_integer_type_p (type))
1939     {
1940       /* Preserve unsignedness if not really getting any wider.  */
1941       if (TYPE_UNSIGNED (type)
1942           && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1943         return convert (unsigned_type_node, exp);
1944
1945       return convert (integer_type_node, exp);
1946     }
1947
1948   return exp;
1949 }
1950
1951
1952 /* Perform default promotions for C data used in expressions.
1953    Enumeral types or short or char are converted to int.
1954    In addition, manifest constants symbols are replaced by their values.  */
1955
1956 tree
1957 default_conversion (tree exp)
1958 {
1959   tree orig_exp;
1960   tree type = TREE_TYPE (exp);
1961   enum tree_code code = TREE_CODE (type);
1962   tree promoted_type;
1963
1964   mark_exp_read (exp);
1965
1966   /* Functions and arrays have been converted during parsing.  */
1967   gcc_assert (code != FUNCTION_TYPE);
1968   if (code == ARRAY_TYPE)
1969     return exp;
1970
1971   /* Constants can be used directly unless they're not loadable.  */
1972   if (TREE_CODE (exp) == CONST_DECL)
1973     exp = DECL_INITIAL (exp);
1974
1975   /* Strip no-op conversions.  */
1976   orig_exp = exp;
1977   STRIP_TYPE_NOPS (exp);
1978
1979   if (TREE_NO_WARNING (orig_exp))
1980     TREE_NO_WARNING (exp) = 1;
1981
1982   if (code == VOID_TYPE)
1983     {
1984       error ("void value not ignored as it ought to be");
1985       return error_mark_node;
1986     }
1987
1988   exp = require_complete_type (exp);
1989   if (exp == error_mark_node)
1990     return error_mark_node;
1991
1992   promoted_type = targetm.promoted_type (type);
1993   if (promoted_type)
1994     return convert (promoted_type, exp);
1995
1996   if (INTEGRAL_TYPE_P (type))
1997     return perform_integral_promotions (exp);
1998
1999   return exp;
2000 }
2001 \f
2002 /* Look up COMPONENT in a structure or union TYPE.
2003
2004    If the component name is not found, returns NULL_TREE.  Otherwise,
2005    the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2006    stepping down the chain to the component, which is in the last
2007    TREE_VALUE of the list.  Normally the list is of length one, but if
2008    the component is embedded within (nested) anonymous structures or
2009    unions, the list steps down the chain to the component.  */
2010
2011 static tree
2012 lookup_field (tree type, tree component)
2013 {
2014   tree field;
2015
2016   /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2017      to the field elements.  Use a binary search on this array to quickly
2018      find the element.  Otherwise, do a linear search.  TYPE_LANG_SPECIFIC
2019      will always be set for structures which have many elements.  */
2020
2021   if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2022     {
2023       int bot, top, half;
2024       tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2025
2026       field = TYPE_FIELDS (type);
2027       bot = 0;
2028       top = TYPE_LANG_SPECIFIC (type)->s->len;
2029       while (top - bot > 1)
2030         {
2031           half = (top - bot + 1) >> 1;
2032           field = field_array[bot+half];
2033
2034           if (DECL_NAME (field) == NULL_TREE)
2035             {
2036               /* Step through all anon unions in linear fashion.  */
2037               while (DECL_NAME (field_array[bot]) == NULL_TREE)
2038                 {
2039                   field = field_array[bot++];
2040                   if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2041                       || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2042                     {
2043                       tree anon = lookup_field (TREE_TYPE (field), component);
2044
2045                       if (anon)
2046                         return tree_cons (NULL_TREE, field, anon);
2047                     }
2048                 }
2049
2050               /* Entire record is only anon unions.  */
2051               if (bot > top)
2052                 return NULL_TREE;
2053
2054               /* Restart the binary search, with new lower bound.  */
2055               continue;
2056             }
2057
2058           if (DECL_NAME (field) == component)
2059             break;
2060           if (DECL_NAME (field) < component)
2061             bot += half;
2062           else
2063             top = bot + half;
2064         }
2065
2066       if (DECL_NAME (field_array[bot]) == component)
2067         field = field_array[bot];
2068       else if (DECL_NAME (field) != component)
2069         return NULL_TREE;
2070     }
2071   else
2072     {
2073       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2074         {
2075           if (DECL_NAME (field) == NULL_TREE
2076               && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2077                   || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2078             {
2079               tree anon = lookup_field (TREE_TYPE (field), component);
2080
2081               if (anon)
2082                 return tree_cons (NULL_TREE, field, anon);
2083             }
2084
2085           if (DECL_NAME (field) == component)
2086             break;
2087         }
2088
2089       if (field == NULL_TREE)
2090         return NULL_TREE;
2091     }
2092
2093   return tree_cons (NULL_TREE, field, NULL_TREE);
2094 }
2095
2096 /* Make an expression to refer to the COMPONENT field of structure or
2097    union value DATUM.  COMPONENT is an IDENTIFIER_NODE.  LOC is the
2098    location of the COMPONENT_REF.  */
2099
2100 tree
2101 build_component_ref (location_t loc, tree datum, tree component)
2102 {
2103   tree type = TREE_TYPE (datum);
2104   enum tree_code code = TREE_CODE (type);
2105   tree field = NULL;
2106   tree ref;
2107   bool datum_lvalue = lvalue_p (datum);
2108
2109   if (!objc_is_public (datum, component))
2110     return error_mark_node;
2111
2112   /* See if there is a field or component with name COMPONENT.  */
2113
2114   if (code == RECORD_TYPE || code == UNION_TYPE)
2115     {
2116       if (!COMPLETE_TYPE_P (type))
2117         {
2118           c_incomplete_type_error (NULL_TREE, type);
2119           return error_mark_node;
2120         }
2121
2122       field = lookup_field (type, component);
2123
2124       if (!field)
2125         {
2126           error_at (loc, "%qT has no member named %qE", type, component);
2127           return error_mark_node;
2128         }
2129
2130       /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2131          This might be better solved in future the way the C++ front
2132          end does it - by giving the anonymous entities each a
2133          separate name and type, and then have build_component_ref
2134          recursively call itself.  We can't do that here.  */
2135       do
2136         {
2137           tree subdatum = TREE_VALUE (field);
2138           int quals;
2139           tree subtype;
2140           bool use_datum_quals;
2141
2142           if (TREE_TYPE (subdatum) == error_mark_node)
2143             return error_mark_node;
2144
2145           /* If this is an rvalue, it does not have qualifiers in C
2146              standard terms and we must avoid propagating such
2147              qualifiers down to a non-lvalue array that is then
2148              converted to a pointer.  */
2149           use_datum_quals = (datum_lvalue
2150                              || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2151
2152           quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2153           if (use_datum_quals)
2154             quals |= TYPE_QUALS (TREE_TYPE (datum));
2155           subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2156
2157           ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2158                         NULL_TREE);
2159           SET_EXPR_LOCATION (ref, loc);
2160           if (TREE_READONLY (subdatum)
2161               || (use_datum_quals && TREE_READONLY (datum)))
2162             TREE_READONLY (ref) = 1;
2163           if (TREE_THIS_VOLATILE (subdatum)
2164               || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2165             TREE_THIS_VOLATILE (ref) = 1;
2166
2167           if (TREE_DEPRECATED (subdatum))
2168             warn_deprecated_use (subdatum, NULL_TREE);
2169
2170           datum = ref;
2171
2172           field = TREE_CHAIN (field);
2173         }
2174       while (field);
2175
2176       return ref;
2177     }
2178   else if (code != ERROR_MARK)
2179     error_at (loc,
2180               "request for member %qE in something not a structure or union",
2181               component);
2182
2183   return error_mark_node;
2184 }
2185 \f
2186 /* Given an expression PTR for a pointer, return an expression
2187    for the value pointed to.
2188    ERRORSTRING is the name of the operator to appear in error messages.
2189
2190    LOC is the location to use for the generated tree.  */
2191
2192 tree
2193 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2194 {
2195   tree pointer = default_conversion (ptr);
2196   tree type = TREE_TYPE (pointer);
2197   tree ref;
2198
2199   if (TREE_CODE (type) == POINTER_TYPE)
2200     {
2201       if (CONVERT_EXPR_P (pointer)
2202           || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2203         {
2204           /* If a warning is issued, mark it to avoid duplicates from
2205              the backend.  This only needs to be done at
2206              warn_strict_aliasing > 2.  */
2207           if (warn_strict_aliasing > 2)
2208             if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2209                                          type, TREE_OPERAND (pointer, 0)))
2210               TREE_NO_WARNING (pointer) = 1;
2211         }
2212
2213       if (TREE_CODE (pointer) == ADDR_EXPR
2214           && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2215               == TREE_TYPE (type)))
2216         {
2217           ref = TREE_OPERAND (pointer, 0);
2218           protected_set_expr_location (ref, loc);
2219           return ref;
2220         }
2221       else
2222         {
2223           tree t = TREE_TYPE (type);
2224
2225           ref = build1 (INDIRECT_REF, t, pointer);
2226
2227           if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2228             {
2229               error_at (loc, "dereferencing pointer to incomplete type");
2230               return error_mark_node;
2231             }
2232           if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2233             warning_at (loc, 0, "dereferencing %<void *%> pointer");
2234
2235           /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2236              so that we get the proper error message if the result is used
2237              to assign to.  Also, &* is supposed to be a no-op.
2238              And ANSI C seems to specify that the type of the result
2239              should be the const type.  */
2240           /* A de-reference of a pointer to const is not a const.  It is valid
2241              to change it via some other pointer.  */
2242           TREE_READONLY (ref) = TYPE_READONLY (t);
2243           TREE_SIDE_EFFECTS (ref)
2244             = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2245           TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2246           protected_set_expr_location (ref, loc);
2247           return ref;
2248         }
2249     }
2250   else if (TREE_CODE (pointer) != ERROR_MARK)
2251     switch (errstring)
2252       {
2253          case RO_ARRAY_INDEXING:
2254            error_at (loc,
2255                      "invalid type argument of array indexing (have %qT)",
2256                      type);
2257            break;
2258          case RO_UNARY_STAR:
2259            error_at (loc,
2260                      "invalid type argument of unary %<*%> (have %qT)",
2261                      type);
2262            break;
2263          case RO_ARROW:
2264            error_at (loc,
2265                      "invalid type argument of %<->%> (have %qT)",
2266                      type);
2267            break;
2268          default:
2269            gcc_unreachable ();
2270       }
2271   return error_mark_node;
2272 }
2273
2274 /* This handles expressions of the form "a[i]", which denotes
2275    an array reference.
2276
2277    This is logically equivalent in C to *(a+i), but we may do it differently.
2278    If A is a variable or a member, we generate a primitive ARRAY_REF.
2279    This avoids forcing the array out of registers, and can work on
2280    arrays that are not lvalues (for example, members of structures returned
2281    by functions).
2282
2283    LOC is the location to use for the returned expression.  */
2284
2285 tree
2286 build_array_ref (location_t loc, tree array, tree index)
2287 {
2288   tree ret;
2289   bool swapped = false;
2290   if (TREE_TYPE (array) == error_mark_node
2291       || TREE_TYPE (index) == error_mark_node)
2292     return error_mark_node;
2293
2294   if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2295       && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE)
2296     {
2297       tree temp;
2298       if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2299           && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2300         {
2301           error_at (loc, "subscripted value is neither array nor pointer");
2302           return error_mark_node;
2303         }
2304       temp = array;
2305       array = index;
2306       index = temp;
2307       swapped = true;
2308     }
2309
2310   if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2311     {
2312       error_at (loc, "array subscript is not an integer");
2313       return error_mark_node;
2314     }
2315
2316   if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2317     {
2318       error_at (loc, "subscripted value is pointer to function");
2319       return error_mark_node;
2320     }
2321
2322   /* ??? Existing practice has been to warn only when the char
2323      index is syntactically the index, not for char[array].  */
2324   if (!swapped)
2325      warn_array_subscript_with_type_char (index);
2326
2327   /* Apply default promotions *after* noticing character types.  */
2328   index = default_conversion (index);
2329
2330   gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2331
2332   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2333     {
2334       tree rval, type;
2335
2336       /* An array that is indexed by a non-constant
2337          cannot be stored in a register; we must be able to do
2338          address arithmetic on its address.
2339          Likewise an array of elements of variable size.  */
2340       if (TREE_CODE (index) != INTEGER_CST
2341           || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2342               && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2343         {
2344           if (!c_mark_addressable (array))
2345             return error_mark_node;
2346         }
2347       /* An array that is indexed by a constant value which is not within
2348          the array bounds cannot be stored in a register either; because we
2349          would get a crash in store_bit_field/extract_bit_field when trying
2350          to access a non-existent part of the register.  */
2351       if (TREE_CODE (index) == INTEGER_CST
2352           && TYPE_DOMAIN (TREE_TYPE (array))
2353           && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2354         {
2355           if (!c_mark_addressable (array))
2356             return error_mark_node;
2357         }
2358
2359       if (pedantic)
2360         {
2361           tree foo = array;
2362           while (TREE_CODE (foo) == COMPONENT_REF)
2363             foo = TREE_OPERAND (foo, 0);
2364           if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2365             pedwarn (loc, OPT_pedantic,
2366                      "ISO C forbids subscripting %<register%> array");
2367           else if (!flag_isoc99 && !lvalue_p (foo))
2368             pedwarn (loc, OPT_pedantic,
2369                      "ISO C90 forbids subscripting non-lvalue array");
2370         }
2371
2372       type = TREE_TYPE (TREE_TYPE (array));
2373       rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2374       /* Array ref is const/volatile if the array elements are
2375          or if the array is.  */
2376       TREE_READONLY (rval)
2377         |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2378             | TREE_READONLY (array));
2379       TREE_SIDE_EFFECTS (rval)
2380         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2381             | TREE_SIDE_EFFECTS (array));
2382       TREE_THIS_VOLATILE (rval)
2383         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2384             /* This was added by rms on 16 Nov 91.
2385                It fixes  vol struct foo *a;  a->elts[1]
2386                in an inline function.
2387                Hope it doesn't break something else.  */
2388             | TREE_THIS_VOLATILE (array));
2389       ret = require_complete_type (rval);
2390       protected_set_expr_location (ret, loc);
2391       return ret;
2392     }
2393   else
2394     {
2395       tree ar = default_conversion (array);
2396
2397       if (ar == error_mark_node)
2398         return ar;
2399
2400       gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2401       gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2402
2403       return build_indirect_ref
2404         (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2405          RO_ARRAY_INDEXING);
2406     }
2407 }
2408 \f
2409 /* Build an external reference to identifier ID.  FUN indicates
2410    whether this will be used for a function call.  LOC is the source
2411    location of the identifier.  This sets *TYPE to the type of the
2412    identifier, which is not the same as the type of the returned value
2413    for CONST_DECLs defined as enum constants.  If the type of the
2414    identifier is not available, *TYPE is set to NULL.  */
2415 tree
2416 build_external_ref (location_t loc, tree id, int fun, tree *type)
2417 {
2418   tree ref;
2419   tree decl = lookup_name (id);
2420
2421   /* In Objective-C, an instance variable (ivar) may be preferred to
2422      whatever lookup_name() found.  */
2423   decl = objc_lookup_ivar (decl, id);
2424
2425   *type = NULL;
2426   if (decl && decl != error_mark_node)
2427     {
2428       ref = decl;
2429       *type = TREE_TYPE (ref);
2430     }
2431   else if (fun)
2432     /* Implicit function declaration.  */
2433     ref = implicitly_declare (loc, id);
2434   else if (decl == error_mark_node)
2435     /* Don't complain about something that's already been
2436        complained about.  */
2437     return error_mark_node;
2438   else
2439     {
2440       undeclared_variable (loc, id);
2441       return error_mark_node;
2442     }
2443
2444   if (TREE_TYPE (ref) == error_mark_node)
2445     return error_mark_node;
2446
2447   if (TREE_DEPRECATED (ref))
2448     warn_deprecated_use (ref, NULL_TREE);
2449
2450   /* Recursive call does not count as usage.  */
2451   if (ref != current_function_decl)
2452     {
2453       TREE_USED (ref) = 1;
2454     }
2455
2456   if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2457     {
2458       if (!in_sizeof && !in_typeof)
2459         C_DECL_USED (ref) = 1;
2460       else if (DECL_INITIAL (ref) == 0
2461                && DECL_EXTERNAL (ref)
2462                && !TREE_PUBLIC (ref))
2463         record_maybe_used_decl (ref);
2464     }
2465
2466   if (TREE_CODE (ref) == CONST_DECL)
2467     {
2468       used_types_insert (TREE_TYPE (ref));
2469
2470       if (warn_cxx_compat
2471           && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2472           && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2473         {
2474           warning_at (loc, OPT_Wc___compat,
2475                       ("enum constant defined in struct or union "
2476                        "is not visible in C++"));
2477           inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2478         }
2479
2480       ref = DECL_INITIAL (ref);
2481       TREE_CONSTANT (ref) = 1;
2482     }
2483   else if (current_function_decl != 0
2484            && !DECL_FILE_SCOPE_P (current_function_decl)
2485            && (TREE_CODE (ref) == VAR_DECL
2486                || TREE_CODE (ref) == PARM_DECL
2487                || TREE_CODE (ref) == FUNCTION_DECL))
2488     {
2489       tree context = decl_function_context (ref);
2490
2491       if (context != 0 && context != current_function_decl)
2492         DECL_NONLOCAL (ref) = 1;
2493     }
2494   /* C99 6.7.4p3: An inline definition of a function with external
2495      linkage ... shall not contain a reference to an identifier with
2496      internal linkage.  */
2497   else if (current_function_decl != 0
2498            && DECL_DECLARED_INLINE_P (current_function_decl)
2499            && DECL_EXTERNAL (current_function_decl)
2500            && VAR_OR_FUNCTION_DECL_P (ref)
2501            && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2502            && ! TREE_PUBLIC (ref)
2503            && DECL_CONTEXT (ref) != current_function_decl)
2504     record_inline_static (loc, current_function_decl, ref,
2505                           csi_internal);
2506
2507   return ref;
2508 }
2509
2510 /* Record details of decls possibly used inside sizeof or typeof.  */
2511 struct maybe_used_decl
2512 {
2513   /* The decl.  */
2514   tree decl;
2515   /* The level seen at (in_sizeof + in_typeof).  */
2516   int level;
2517   /* The next one at this level or above, or NULL.  */
2518   struct maybe_used_decl *next;
2519 };
2520
2521 static struct maybe_used_decl *maybe_used_decls;
2522
2523 /* Record that DECL, an undefined static function reference seen
2524    inside sizeof or typeof, might be used if the operand of sizeof is
2525    a VLA type or the operand of typeof is a variably modified
2526    type.  */
2527
2528 static void
2529 record_maybe_used_decl (tree decl)
2530 {
2531   struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2532   t->decl = decl;
2533   t->level = in_sizeof + in_typeof;
2534   t->next = maybe_used_decls;
2535   maybe_used_decls = t;
2536 }
2537
2538 /* Pop the stack of decls possibly used inside sizeof or typeof.  If
2539    USED is false, just discard them.  If it is true, mark them used
2540    (if no longer inside sizeof or typeof) or move them to the next
2541    level up (if still inside sizeof or typeof).  */
2542
2543 void
2544 pop_maybe_used (bool used)
2545 {
2546   struct maybe_used_decl *p = maybe_used_decls;
2547   int cur_level = in_sizeof + in_typeof;
2548   while (p && p->level > cur_level)
2549     {
2550       if (used)
2551         {
2552           if (cur_level == 0)
2553             C_DECL_USED (p->decl) = 1;
2554           else
2555             p->level = cur_level;
2556         }
2557       p = p->next;
2558     }
2559   if (!used || cur_level == 0)
2560     maybe_used_decls = p;
2561 }
2562
2563 /* Return the result of sizeof applied to EXPR.  */
2564
2565 struct c_expr
2566 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2567 {
2568   struct c_expr ret;
2569   if (expr.value == error_mark_node)
2570     {
2571       ret.value = error_mark_node;
2572       ret.original_code = ERROR_MARK;
2573       ret.original_type = NULL;
2574       pop_maybe_used (false);
2575     }
2576   else
2577     {
2578       bool expr_const_operands = true;
2579       tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2580                                        &expr_const_operands);
2581       ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2582       ret.original_code = ERROR_MARK;
2583       ret.original_type = NULL;
2584       if (c_vla_type_p (TREE_TYPE (folded_expr)))
2585         {
2586           /* sizeof is evaluated when given a vla (C99 6.5.3.4p2).  */
2587           ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2588                               folded_expr, ret.value);
2589           C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2590           SET_EXPR_LOCATION (ret.value, loc);
2591         }
2592       pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2593     }
2594   return ret;
2595 }
2596
2597 /* Return the result of sizeof applied to T, a structure for the type
2598    name passed to sizeof (rather than the type itself).  LOC is the
2599    location of the original expression.  */
2600
2601 struct c_expr
2602 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2603 {
2604   tree type;
2605   struct c_expr ret;
2606   tree type_expr = NULL_TREE;
2607   bool type_expr_const = true;
2608   type = groktypename (t, &type_expr, &type_expr_const);
2609   ret.value = c_sizeof (loc, type);
2610   ret.original_code = ERROR_MARK;
2611   ret.original_type = NULL;
2612   if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2613       && c_vla_type_p (type))
2614     {
2615       /* If the type is a [*] array, it is a VLA but is represented as
2616          having a size of zero.  In such a case we must ensure that
2617          the result of sizeof does not get folded to a constant by
2618          c_fully_fold, because if the size is evaluated the result is
2619          not constant and so constraints on zero or negative size
2620          arrays must not be applied when this sizeof call is inside
2621          another array declarator.  */
2622       if (!type_expr)
2623         type_expr = integer_zero_node;
2624       ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2625                           type_expr, ret.value);
2626       C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2627     }
2628   pop_maybe_used (type != error_mark_node
2629                   ? C_TYPE_VARIABLE_SIZE (type) : false);
2630   return ret;
2631 }
2632
2633 /* Build a function call to function FUNCTION with parameters PARAMS.
2634    The function call is at LOC.
2635    PARAMS is a list--a chain of TREE_LIST nodes--in which the
2636    TREE_VALUE of each node is a parameter-expression.
2637    FUNCTION's data type may be a function type or a pointer-to-function.  */
2638
2639 tree
2640 build_function_call (location_t loc, tree function, tree params)
2641 {
2642   VEC(tree,gc) *vec;
2643   tree ret;
2644
2645   vec = VEC_alloc (tree, gc, list_length (params));
2646   for (; params; params = TREE_CHAIN (params))
2647     VEC_quick_push (tree, vec, TREE_VALUE (params));
2648   ret = build_function_call_vec (loc, function, vec, NULL);
2649   VEC_free (tree, gc, vec);
2650   return ret;
2651 }
2652
2653 /* Build a function call to function FUNCTION with parameters PARAMS.
2654    ORIGTYPES, if not NULL, is a vector of types; each element is
2655    either NULL or the original type of the corresponding element in
2656    PARAMS.  The original type may differ from TREE_TYPE of the
2657    parameter for enums.  FUNCTION's data type may be a function type
2658    or pointer-to-function.  This function changes the elements of
2659    PARAMS.  */
2660
2661 tree
2662 build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
2663                          VEC(tree,gc) *origtypes)
2664 {
2665   tree fntype, fundecl = 0;
2666   tree name = NULL_TREE, result;
2667   tree tem;
2668   int nargs;
2669   tree *argarray;
2670
2671
2672   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
2673   STRIP_TYPE_NOPS (function);
2674
2675   /* Convert anything with function type to a pointer-to-function.  */
2676   if (TREE_CODE (function) == FUNCTION_DECL)
2677     {
2678       /* Implement type-directed function overloading for builtins.
2679          resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2680          handle all the type checking.  The result is a complete expression
2681          that implements this function call.  */
2682       tem = resolve_overloaded_builtin (loc, function, params);
2683       if (tem)
2684         return tem;
2685
2686       name = DECL_NAME (function);
2687       fundecl = function;
2688     }
2689   if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2690     function = function_to_pointer_conversion (loc, function);
2691
2692   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2693      expressions, like those used for ObjC messenger dispatches.  */
2694   if (!VEC_empty (tree, params))
2695     function = objc_rewrite_function_call (function,
2696                                            VEC_index (tree, params, 0));
2697
2698   function = c_fully_fold (function, false, NULL);
2699
2700   fntype = TREE_TYPE (function);
2701
2702   if (TREE_CODE (fntype) == ERROR_MARK)
2703     return error_mark_node;
2704
2705   if (!(TREE_CODE (fntype) == POINTER_TYPE
2706         && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2707     {
2708       error_at (loc, "called object %qE is not a function", function);
2709       return error_mark_node;
2710     }
2711
2712   if (fundecl && TREE_THIS_VOLATILE (fundecl))
2713     current_function_returns_abnormally = 1;
2714
2715   /* fntype now gets the type of function pointed to.  */
2716   fntype = TREE_TYPE (fntype);
2717
2718   /* Convert the parameters to the types declared in the
2719      function prototype, or apply default promotions.  */
2720
2721   nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2722                              function, fundecl);
2723   if (nargs < 0)
2724     return error_mark_node;
2725
2726   /* Check that the function is called through a compatible prototype.
2727      If it is not, replace the call by a trap, wrapped up in a compound
2728      expression if necessary.  This has the nice side-effect to prevent
2729      the tree-inliner from generating invalid assignment trees which may
2730      blow up in the RTL expander later.  */
2731   if (CONVERT_EXPR_P (function)
2732       && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2733       && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2734       && !comptypes (fntype, TREE_TYPE (tem)))
2735     {
2736       tree return_type = TREE_TYPE (fntype);
2737       tree trap = build_function_call (loc, built_in_decls[BUILT_IN_TRAP],
2738                                        NULL_TREE);
2739       int i;
2740
2741       /* This situation leads to run-time undefined behavior.  We can't,
2742          therefore, simply error unless we can prove that all possible
2743          executions of the program must execute the code.  */
2744       if (warning_at (loc, 0, "function called through a non-compatible type"))
2745         /* We can, however, treat "undefined" any way we please.
2746            Call abort to encourage the user to fix the program.  */
2747         inform (loc, "if this code is reached, the program will abort");
2748       /* Before the abort, allow the function arguments to exit or
2749          call longjmp.  */
2750       for (i = 0; i < nargs; i++)
2751         trap = build2 (COMPOUND_EXPR, void_type_node,
2752                        VEC_index (tree, params, i), trap);
2753
2754       if (VOID_TYPE_P (return_type))
2755         {
2756           if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2757             pedwarn (loc, 0,
2758                      "function with qualified void return type called");
2759           return trap;
2760         }
2761       else
2762         {
2763           tree rhs;
2764
2765           if (AGGREGATE_TYPE_P (return_type))
2766             rhs = build_compound_literal (loc, return_type,
2767                                           build_constructor (return_type, 0),
2768                                           false);
2769           else
2770             rhs = fold_convert_loc (loc, return_type, integer_zero_node);
2771
2772           return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2773                                                 trap, rhs));
2774         }
2775     }
2776
2777   argarray = VEC_address (tree, params);
2778
2779   /* Check that arguments to builtin functions match the expectations.  */
2780   if (fundecl
2781       && DECL_BUILT_IN (fundecl)
2782       && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2783       && !check_builtin_function_arguments (fundecl, nargs, argarray))
2784     return error_mark_node;
2785
2786   /* Check that the arguments to the function are valid.  */
2787   check_function_arguments (TYPE_ATTRIBUTES (fntype), nargs, argarray,
2788                             TYPE_ARG_TYPES (fntype));
2789
2790   if (name != NULL_TREE
2791       && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2792     {
2793       if (require_constant_value)
2794         result =
2795           fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2796                                                  function, nargs, argarray);
2797       else
2798         result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2799                                             function, nargs, argarray);
2800       if (TREE_CODE (result) == NOP_EXPR
2801           && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2802         STRIP_TYPE_NOPS (result);
2803     }
2804   else
2805     result = build_call_array_loc (loc, TREE_TYPE (fntype),
2806                                    function, nargs, argarray);
2807
2808   if (VOID_TYPE_P (TREE_TYPE (result)))
2809     {
2810       if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2811         pedwarn (loc, 0,
2812                  "function with qualified void return type called");
2813       return result;
2814     }
2815   return require_complete_type (result);
2816 }
2817 \f
2818 /* Convert the argument expressions in the vector VALUES
2819    to the types in the list TYPELIST.
2820
2821    If TYPELIST is exhausted, or when an element has NULL as its type,
2822    perform the default conversions.
2823
2824    ORIGTYPES is the original types of the expressions in VALUES.  This
2825    holds the type of enum values which have been converted to integral
2826    types.  It may be NULL.
2827
2828    FUNCTION is a tree for the called function.  It is used only for
2829    error messages, where it is formatted with %qE.
2830
2831    This is also where warnings about wrong number of args are generated.
2832
2833    Returns the actual number of arguments processed (which may be less
2834    than the length of VALUES in some error situations), or -1 on
2835    failure.  */
2836
2837 static int
2838 convert_arguments (tree typelist, VEC(tree,gc) *values,
2839                    VEC(tree,gc) *origtypes, tree function, tree fundecl)
2840 {
2841   tree typetail, val;
2842   unsigned int parmnum;
2843   bool error_args = false;
2844   const bool type_generic = fundecl
2845     && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2846   bool type_generic_remove_excess_precision = false;
2847   tree selector;
2848
2849   /* Change pointer to function to the function itself for
2850      diagnostics.  */
2851   if (TREE_CODE (function) == ADDR_EXPR
2852       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2853     function = TREE_OPERAND (function, 0);
2854
2855   /* Handle an ObjC selector specially for diagnostics.  */
2856   selector = objc_message_selector ();
2857
2858   /* For type-generic built-in functions, determine whether excess
2859      precision should be removed (classification) or not
2860      (comparison).  */
2861   if (type_generic
2862       && DECL_BUILT_IN (fundecl)
2863       && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2864     {
2865       switch (DECL_FUNCTION_CODE (fundecl))
2866         {
2867         case BUILT_IN_ISFINITE:
2868         case BUILT_IN_ISINF:
2869         case BUILT_IN_ISINF_SIGN:
2870         case BUILT_IN_ISNAN:
2871         case BUILT_IN_ISNORMAL:
2872         case BUILT_IN_FPCLASSIFY:
2873           type_generic_remove_excess_precision = true;
2874           break;
2875
2876         default:
2877           type_generic_remove_excess_precision = false;
2878           break;
2879         }
2880     }
2881
2882   /* Scan the given expressions and types, producing individual
2883      converted arguments.  */
2884
2885   for (typetail = typelist, parmnum = 0;
2886        VEC_iterate (tree, values, parmnum, val);
2887        ++parmnum)
2888     {
2889       tree type = typetail ? TREE_VALUE (typetail) : 0;
2890       tree valtype = TREE_TYPE (val);
2891       tree rname = function;
2892       int argnum = parmnum + 1;
2893       const char *invalid_func_diag;
2894       bool excess_precision = false;
2895       bool npc;
2896       tree parmval;
2897
2898       if (type == void_type_node)
2899         {
2900           if (selector)
2901             error_at (input_location,
2902                       "too many arguments to method %qE", selector);
2903           else
2904             error_at (input_location,
2905                       "too many arguments to function %qE", function);
2906
2907           if (fundecl && !DECL_BUILT_IN (fundecl))
2908             inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
2909           return parmnum;
2910         }
2911
2912       if (selector && argnum > 2)
2913         {
2914           rname = selector;
2915           argnum -= 2;
2916         }
2917
2918       npc = null_pointer_constant_p (val);
2919
2920       /* If there is excess precision and a prototype, convert once to
2921          the required type rather than converting via the semantic
2922          type.  Likewise without a prototype a float value represented
2923          as long double should be converted once to double.  But for
2924          type-generic classification functions excess precision must
2925          be removed here.  */
2926       if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2927           && (type || !type_generic || !type_generic_remove_excess_precision))
2928         {
2929           val = TREE_OPERAND (val, 0);
2930           excess_precision = true;
2931         }
2932       val = c_fully_fold (val, false, NULL);
2933       STRIP_TYPE_NOPS (val);
2934
2935       val = require_complete_type (val);
2936
2937       if (type != 0)
2938         {
2939           /* Formal parm type is specified by a function prototype.  */
2940
2941           if (type == error_mark_node || !COMPLETE_TYPE_P (type))
2942             {
2943               error ("type of formal parameter %d is incomplete", parmnum + 1);
2944               parmval = val;
2945             }
2946           else
2947             {
2948               tree origtype;
2949
2950               /* Optionally warn about conversions that
2951                  differ from the default conversions.  */
2952               if (warn_traditional_conversion || warn_traditional)
2953                 {
2954                   unsigned int formal_prec = TYPE_PRECISION (type);
2955
2956                   if (INTEGRAL_TYPE_P (type)
2957                       && TREE_CODE (valtype) == REAL_TYPE)
2958                     warning (0, "passing argument %d of %qE as integer "
2959                              "rather than floating due to prototype",
2960                              argnum, rname);
2961                   if (INTEGRAL_TYPE_P (type)
2962                       && TREE_CODE (valtype) == COMPLEX_TYPE)
2963                     warning (0, "passing argument %d of %qE as integer "
2964                              "rather than complex due to prototype",
2965                              argnum, rname);
2966                   else if (TREE_CODE (type) == COMPLEX_TYPE
2967                            && TREE_CODE (valtype) == REAL_TYPE)
2968                     warning (0, "passing argument %d of %qE as complex "
2969                              "rather than floating due to prototype",
2970                              argnum, rname);
2971                   else if (TREE_CODE (type) == REAL_TYPE
2972                            && INTEGRAL_TYPE_P (valtype))
2973                     warning (0, "passing argument %d of %qE as floating "
2974                              "rather than integer due to prototype",
2975                              argnum, rname);
2976                   else if (TREE_CODE (type) == COMPLEX_TYPE
2977                            && INTEGRAL_TYPE_P (valtype))
2978                     warning (0, "passing argument %d of %qE as complex "
2979                              "rather than integer due to prototype",
2980                              argnum, rname);
2981                   else if (TREE_CODE (type) == REAL_TYPE
2982                            && TREE_CODE (valtype) == COMPLEX_TYPE)
2983                     warning (0, "passing argument %d of %qE as floating "
2984                              "rather than complex due to prototype",
2985                              argnum, rname);
2986                   /* ??? At some point, messages should be written about
2987                      conversions between complex types, but that's too messy
2988                      to do now.  */
2989                   else if (TREE_CODE (type) == REAL_TYPE
2990                            && TREE_CODE (valtype) == REAL_TYPE)
2991                     {
2992                       /* Warn if any argument is passed as `float',
2993                          since without a prototype it would be `double'.  */
2994                       if (formal_prec == TYPE_PRECISION (float_type_node)
2995                           && type != dfloat32_type_node)
2996                         warning (0, "passing argument %d of %qE as %<float%> "
2997                                  "rather than %<double%> due to prototype",
2998                                  argnum, rname);
2999
3000                       /* Warn if mismatch between argument and prototype
3001                          for decimal float types.  Warn of conversions with
3002                          binary float types and of precision narrowing due to
3003                          prototype. */
3004                       else if (type != valtype
3005                                && (type == dfloat32_type_node
3006                                    || type == dfloat64_type_node
3007                                    || type == dfloat128_type_node
3008                                    || valtype == dfloat32_type_node
3009                                    || valtype == dfloat64_type_node
3010                                    || valtype == dfloat128_type_node)
3011                                && (formal_prec
3012                                    <= TYPE_PRECISION (valtype)
3013                                    || (type == dfloat128_type_node
3014                                        && (valtype
3015                                            != dfloat64_type_node
3016                                            && (valtype
3017                                                != dfloat32_type_node)))
3018                                    || (type == dfloat64_type_node
3019                                        && (valtype
3020                                            != dfloat32_type_node))))
3021                         warning (0, "passing argument %d of %qE as %qT "
3022                                  "rather than %qT due to prototype",
3023                                  argnum, rname, type, valtype);
3024
3025                     }
3026                   /* Detect integer changing in width or signedness.
3027                      These warnings are only activated with
3028                      -Wtraditional-conversion, not with -Wtraditional.  */
3029                   else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3030                            && INTEGRAL_TYPE_P (valtype))
3031                     {
3032                       tree would_have_been = default_conversion (val);
3033                       tree type1 = TREE_TYPE (would_have_been);
3034
3035                       if (TREE_CODE (type) == ENUMERAL_TYPE
3036                           && (TYPE_MAIN_VARIANT (type)
3037                               == TYPE_MAIN_VARIANT (valtype)))
3038                         /* No warning if function asks for enum
3039                            and the actual arg is that enum type.  */
3040                         ;
3041                       else if (formal_prec != TYPE_PRECISION (type1))
3042                         warning (OPT_Wtraditional_conversion,
3043                                  "passing argument %d of %qE "
3044                                  "with different width due to prototype",
3045                                  argnum, rname);
3046                       else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3047                         ;
3048                       /* Don't complain if the formal parameter type
3049                          is an enum, because we can't tell now whether
3050                          the value was an enum--even the same enum.  */
3051                       else if (TREE_CODE (type) == ENUMERAL_TYPE)
3052                         ;
3053                       else if (TREE_CODE (val) == INTEGER_CST
3054                                && int_fits_type_p (val, type))
3055                         /* Change in signedness doesn't matter
3056                            if a constant value is unaffected.  */
3057                         ;
3058                       /* If the value is extended from a narrower
3059                          unsigned type, it doesn't matter whether we
3060                          pass it as signed or unsigned; the value
3061                          certainly is the same either way.  */
3062                       else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3063                                && TYPE_UNSIGNED (valtype))
3064                         ;
3065                       else if (TYPE_UNSIGNED (type))
3066                         warning (OPT_Wtraditional_conversion,
3067                                  "passing argument %d of %qE "
3068                                  "as unsigned due to prototype",
3069                                  argnum, rname);
3070                       else
3071                         warning (OPT_Wtraditional_conversion,
3072                                  "passing argument %d of %qE "
3073                                  "as signed due to prototype", argnum, rname);
3074                     }
3075                 }
3076
3077               /* Possibly restore an EXCESS_PRECISION_EXPR for the
3078                  sake of better warnings from convert_and_check.  */
3079               if (excess_precision)
3080                 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3081               origtype = (origtypes == NULL
3082                           ? NULL_TREE
3083                           : VEC_index (tree, origtypes, parmnum));
3084               parmval = convert_for_assignment (input_location, type, val,
3085                                                 origtype, ic_argpass, npc,
3086                                                 fundecl, function,
3087                                                 parmnum + 1);
3088
3089               if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3090                   && INTEGRAL_TYPE_P (type)
3091                   && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3092                 parmval = default_conversion (parmval);
3093             }
3094         }
3095       else if (TREE_CODE (valtype) == REAL_TYPE
3096                && (TYPE_PRECISION (valtype)
3097                    < TYPE_PRECISION (double_type_node))
3098                && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3099         {
3100           if (type_generic)
3101             parmval = val;
3102           else
3103             {
3104               /* Convert `float' to `double'.  */
3105               if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3106                 warning (OPT_Wdouble_promotion,
3107                          "implicit conversion from %qT to %qT when passing "
3108                          "argument to function",
3109                          valtype, double_type_node);
3110               parmval = convert (double_type_node, val);
3111             }
3112         }
3113       else if (excess_precision && !type_generic)
3114         /* A "double" argument with excess precision being passed
3115            without a prototype or in variable arguments.  */
3116         parmval = convert (valtype, val);
3117       else if ((invalid_func_diag =
3118                 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3119         {
3120           error (invalid_func_diag);
3121           return -1;
3122         }
3123       else
3124         /* Convert `short' and `char' to full-size `int'.  */
3125         parmval = default_conversion (val);
3126
3127       VEC_replace (tree, values, parmnum, parmval);
3128       if (parmval == error_mark_node)
3129         error_args = true;
3130
3131       if (typetail)
3132         typetail = TREE_CHAIN (typetail);
3133     }
3134
3135   gcc_assert (parmnum == VEC_length (tree, values));
3136
3137   if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3138     {
3139       error_at (input_location, 
3140                 "too few arguments to function %qE", function);
3141       if (fundecl && !DECL_BUILT_IN (fundecl))
3142         inform (DECL_SOURCE_LOCATION (fundecl), "declared here");
3143       return -1;
3144     }
3145
3146   return error_args ? -1 : (int) parmnum;
3147 }
3148 \f
3149 /* This is the entry point used by the parser to build unary operators
3150    in the input.  CODE, a tree_code, specifies the unary operator, and
3151    ARG is the operand.  For unary plus, the C parser currently uses
3152    CONVERT_EXPR for code.
3153
3154    LOC is the location to use for the tree generated.
3155 */
3156
3157 struct c_expr
3158 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3159 {
3160   struct c_expr result;
3161
3162   result.value = build_unary_op (loc, code, arg.value, 0);
3163   result.original_code = code;
3164   result.original_type = NULL;
3165
3166   if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3167     overflow_warning (loc, result.value);
3168
3169   return result;
3170 }
3171
3172 /* This is the entry point used by the parser to build binary operators
3173    in the input.  CODE, a tree_code, specifies the binary operator, and
3174    ARG1 and ARG2 are the operands.  In addition to constructing the
3175    expression, we check for operands that were written with other binary
3176    operators in a way that is likely to confuse the user.
3177
3178    LOCATION is the location of the binary operator.  */
3179
3180 struct c_expr
3181 parser_build_binary_op (location_t location, enum tree_code code,
3182                         struct c_expr arg1, struct c_expr arg2)
3183 {
3184   struct c_expr result;
3185
3186   enum tree_code code1 = arg1.original_code;
3187   enum tree_code code2 = arg2.original_code;
3188   tree type1 = (arg1.original_type
3189                 ? arg1.original_type
3190                 : TREE_TYPE (arg1.value));
3191   tree type2 = (arg2.original_type
3192                 ? arg2.original_type
3193                 : TREE_TYPE (arg2.value));
3194
3195   result.value = build_binary_op (location, code,
3196                                   arg1.value, arg2.value, 1);
3197   result.original_code = code;
3198   result.original_type = NULL;
3199
3200   if (TREE_CODE (result.value) == ERROR_MARK)
3201     return result;
3202
3203   if (location != UNKNOWN_LOCATION)
3204     protected_set_expr_location (result.value, location);
3205
3206   /* Check for cases such as x+y<<z which users are likely
3207      to misinterpret.  */
3208   if (warn_parentheses)
3209     warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
3210
3211   if (warn_logical_op)
3212     warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3213                            code1, arg1.value, code2, arg2.value);
3214
3215   /* Warn about comparisons against string literals, with the exception
3216      of testing for equality or inequality of a string literal with NULL.  */
3217   if (code == EQ_EXPR || code == NE_EXPR)
3218     {
3219       if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3220           || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3221         warning_at (location, OPT_Waddress,
3222                     "comparison with string literal results in unspecified behavior");
3223     }
3224   else if (TREE_CODE_CLASS (code) == tcc_comparison
3225            && (code1 == STRING_CST || code2 == STRING_CST))
3226     warning_at (location, OPT_Waddress,
3227                 "comparison with string literal results in unspecified behavior");
3228
3229   if (TREE_OVERFLOW_P (result.value)
3230       && !TREE_OVERFLOW_P (arg1.value)
3231       && !TREE_OVERFLOW_P (arg2.value))
3232     overflow_warning (location, result.value);
3233
3234   /* Warn about comparisons of different enum types.  */
3235   if (warn_enum_compare
3236       && TREE_CODE_CLASS (code) == tcc_comparison
3237       && TREE_CODE (type1) == ENUMERAL_TYPE
3238       && TREE_CODE (type2) == ENUMERAL_TYPE
3239       && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3240     warning_at (location, OPT_Wenum_compare,
3241                 "comparison between %qT and %qT",
3242                 type1, type2);
3243
3244   return result;
3245 }
3246 \f
3247 /* Return a tree for the difference of pointers OP0 and OP1.
3248    The resulting tree has type int.  */
3249
3250 static tree
3251 pointer_diff (location_t loc, tree op0, tree op1)
3252 {
3253   tree restype = ptrdiff_type_node;
3254   tree result, inttype;
3255
3256   addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3257   addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3258   tree target_type = TREE_TYPE (TREE_TYPE (op0));
3259   tree con0, con1, lit0, lit1;
3260   tree orig_op1 = op1;
3261
3262   /* If the operands point into different address spaces, we need to
3263      explicitly convert them to pointers into the common address space
3264      before we can subtract the numerical address values.  */
3265   if (as0 != as1)
3266     {
3267       addr_space_t as_common;
3268       tree common_type;
3269
3270       /* Determine the common superset address space.  This is guaranteed
3271          to exist because the caller verified that comp_target_types
3272          returned non-zero.  */
3273       if (!addr_space_superset (as0, as1, &as_common))
3274         gcc_unreachable ();
3275
3276       common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3277       op0 = convert (common_type, op0);
3278       op1 = convert (common_type, op1);
3279     }
3280
3281   /* Determine integer type to perform computations in.  This will usually
3282      be the same as the result type (ptrdiff_t), but may need to be a wider
3283      type if pointers for the address space are wider than ptrdiff_t.  */
3284   if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3285     inttype = lang_hooks.types.type_for_size
3286                 (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3287   else
3288     inttype = restype;
3289
3290
3291   if (TREE_CODE (target_type) == VOID_TYPE)
3292     pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3293              "pointer of type %<void *%> used in subtraction");
3294   if (TREE_CODE (target_type) == FUNCTION_TYPE)
3295     pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3296              "pointer to a function used in subtraction");
3297
3298   /* If the conversion to ptrdiff_type does anything like widening or
3299      converting a partial to an integral mode, we get a convert_expression
3300      that is in the way to do any simplifications.
3301      (fold-const.c doesn't know that the extra bits won't be needed.
3302      split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3303      different mode in place.)
3304      So first try to find a common term here 'by hand'; we want to cover
3305      at least the cases that occur in legal static initializers.  */
3306   if (CONVERT_EXPR_P (op0)
3307       && (TYPE_PRECISION (TREE_TYPE (op0))
3308           == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3309     con0 = TREE_OPERAND (op0, 0);
3310   else
3311     con0 = op0;
3312   if (CONVERT_EXPR_P (op1)
3313       && (TYPE_PRECISION (TREE_TYPE (op1))
3314           == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3315     con1 = TREE_OPERAND (op1, 0);
3316   else
3317     con1 = op1;
3318
3319   if (TREE_CODE (con0) == PLUS_EXPR)
3320     {
3321       lit0 = TREE_OPERAND (con0, 1);
3322       con0 = TREE_OPERAND (con0, 0);
3323     }
3324   else
3325     lit0 = integer_zero_node;
3326
3327   if (TREE_CODE (con1) == PLUS_EXPR)
3328     {
3329       lit1 = TREE_OPERAND (con1, 1);
3330       con1 = TREE_OPERAND (con1, 0);
3331     }
3332   else
3333     lit1 = integer_zero_node;
3334
3335   if (operand_equal_p (con0, con1, 0))
3336     {
3337       op0 = lit0;
3338       op1 = lit1;
3339     }
3340
3341
3342   /* First do the subtraction as integers;
3343      then drop through to build the divide operator.
3344      Do not do default conversions on the minus operator
3345      in case restype is a short type.  */
3346
3347   op0 = build_binary_op (loc,
3348                          MINUS_EXPR, convert (inttype, op0),
3349                          convert (inttype, op1), 0);
3350   /* This generates an error if op1 is pointer to incomplete type.  */
3351   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3352     error_at (loc, "arithmetic on pointer to an incomplete type");
3353
3354   /* This generates an error if op0 is pointer to incomplete type.  */
3355   op1 = c_size_in_bytes (target_type);
3356
3357   /* Divide by the size, in easiest possible way.  */
3358   result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3359                             op0, convert (inttype, op1));
3360
3361   /* Convert to final result type if necessary.  */
3362   return convert (restype, result);
3363 }
3364 \f
3365 /* Construct and perhaps optimize a tree representation
3366    for a unary operation.  CODE, a tree_code, specifies the operation
3367    and XARG is the operand.
3368    For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3369    the default promotions (such as from short to int).
3370    For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3371    allows non-lvalues; this is only used to handle conversion of non-lvalue
3372    arrays to pointers in C99.
3373
3374    LOCATION is the location of the operator.  */
3375
3376 tree
3377 build_unary_op (location_t location,
3378                 enum tree_code code, tree xarg, int flag)
3379 {
3380   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
3381   tree arg = xarg;
3382   tree argtype = 0;
3383   enum tree_code typecode;
3384   tree val;
3385   tree ret = error_mark_node;
3386   tree eptype = NULL_TREE;
3387   int noconvert = flag;
3388   const char *invalid_op_diag;
3389   bool int_operands;
3390
3391   int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3392   if (int_operands)
3393     arg = remove_c_maybe_const_expr (arg);
3394
3395   if (code != ADDR_EXPR)
3396     arg = require_complete_type (arg);
3397
3398   typecode = TREE_CODE (TREE_TYPE (arg));
3399   if (typecode == ERROR_MARK)
3400     return error_mark_node;
3401   if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3402     typecode = INTEGER_TYPE;
3403
3404   if ((invalid_op_diag
3405        = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3406     {
3407       error_at (location, invalid_op_diag);
3408       return error_mark_node;
3409     }
3410
3411   if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3412     {
3413       eptype = TREE_TYPE (arg);
3414       arg = TREE_OPERAND (arg, 0);
3415     }
3416
3417   switch (code)
3418     {
3419     case CONVERT_EXPR:
3420       /* This is used for unary plus, because a CONVERT_EXPR
3421          is enough to prevent anybody from looking inside for
3422          associativity, but won't generate any code.  */
3423       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3424             || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3425             || typecode == VECTOR_TYPE))
3426         {
3427           error_at (location, "wrong type argument to unary plus");
3428           return error_mark_node;
3429         }
3430       else if (!noconvert)
3431         arg = default_conversion (arg);
3432       arg = non_lvalue_loc (location, arg);
3433       break;
3434
3435     case NEGATE_EXPR:
3436       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3437             || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3438             || typecode == VECTOR_TYPE))
3439         {
3440           error_at (location, "wrong type argument to unary minus");
3441           return error_mark_node;
3442         }
3443       else if (!noconvert)
3444         arg = default_conversion (arg);
3445       break;
3446
3447     case BIT_NOT_EXPR:
3448       /* ~ works on integer types and non float vectors. */
3449       if (typecode == INTEGER_TYPE
3450           || (typecode == VECTOR_TYPE
3451               && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3452         {
3453           if (!noconvert)
3454             arg = default_conversion (arg);
3455         }
3456       else if (typecode == COMPLEX_TYPE)
3457         {
3458           code = CONJ_EXPR;
3459           pedwarn (location, OPT_pedantic,
3460                    "ISO C does not support %<~%> for complex conjugation");
3461           if (!noconvert)
3462             arg = default_conversion (arg);
3463         }
3464       else
3465         {
3466           error_at (location, "wrong type argument to bit-complement");
3467           return error_mark_node;
3468         }
3469       break;
3470
3471     case ABS_EXPR:
3472       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3473         {
3474           error_at (location, "wrong type argument to abs");
3475           return error_mark_node;
3476         }
3477       else if (!noconvert)
3478         arg = default_conversion (arg);
3479       break;
3480
3481     case CONJ_EXPR:
3482       /* Conjugating a real value is a no-op, but allow it anyway.  */
3483       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3484             || typecode == COMPLEX_TYPE))
3485         {
3486           error_at (location, "wrong type argument to conjugation");
3487           return error_mark_node;
3488         }
3489       else if (!noconvert)
3490         arg = default_conversion (arg);
3491       break;
3492
3493     case TRUTH_NOT_EXPR:
3494       if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3495           && typecode != REAL_TYPE && typecode != POINTER_TYPE
3496           && typecode != COMPLEX_TYPE)
3497         {
3498           error_at (location,
3499                     "wrong type argument to unary exclamation mark");
3500           return error_mark_node;
3501         }
3502       arg = c_objc_common_truthvalue_conversion (location, arg);
3503       ret = invert_truthvalue_loc (location, arg);
3504       /* If the TRUTH_NOT_EXPR has been folded, reset the location.  */
3505       if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3506         location = EXPR_LOCATION (ret);
3507       goto return_build_unary_op;
3508
3509     case REALPART_EXPR:
3510       if (TREE_CODE (arg) == COMPLEX_CST)
3511         ret = TREE_REALPART (arg);
3512       else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3513         ret = fold_build1_loc (location,
3514                                REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3515       else
3516         ret = arg;
3517       if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3518         eptype = TREE_TYPE (eptype);
3519       goto return_build_unary_op;
3520
3521     case IMAGPART_EXPR:
3522       if (TREE_CODE (arg) == COMPLEX_CST)
3523         ret = TREE_IMAGPART (arg);
3524       else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
3525         ret = fold_build1_loc (location,
3526                                IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg);
3527       else
3528         ret = omit_one_operand_loc (location, TREE_TYPE (arg),
3529                                 integer_zero_node, arg);
3530       if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3531         eptype = TREE_TYPE (eptype);
3532       goto return_build_unary_op;
3533
3534     case PREINCREMENT_EXPR:
3535     case POSTINCREMENT_EXPR:
3536     case PREDECREMENT_EXPR:
3537     case POSTDECREMENT_EXPR:
3538
3539       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3540         {
3541           tree inner = build_unary_op (location, code,
3542                                        C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3543           if (inner == error_mark_node)
3544             return error_mark_node;
3545           ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3546                         C_MAYBE_CONST_EXPR_PRE (arg), inner);
3547           gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3548           C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3549           goto return_build_unary_op;
3550         }
3551
3552       /* Complain about anything that is not a true lvalue.  */
3553       if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3554                                   || code == POSTINCREMENT_EXPR)
3555                                  ? lv_increment
3556                                  : lv_decrement)))
3557         return error_mark_node;
3558
3559       if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3560         {
3561           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3562             warning_at (location, OPT_Wc___compat,
3563                         "increment of enumeration value is invalid in C++");
3564           else
3565             warning_at (location, OPT_Wc___compat,
3566                         "decrement of enumeration value is invalid in C++");
3567         }
3568
3569       /* Ensure the argument is fully folded inside any SAVE_EXPR.  */
3570       arg = c_fully_fold (arg, false, NULL);
3571
3572       /* Increment or decrement the real part of the value,
3573          and don't change the imaginary part.  */
3574       if (typecode == COMPLEX_TYPE)
3575         {
3576           tree real, imag;
3577
3578           pedwarn (location, OPT_pedantic,
3579                    "ISO C does not support %<++%> and %<--%> on complex types");
3580
3581           arg = stabilize_reference (arg);
3582           real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3583           imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3584           real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3585           if (real == error_mark_node || imag == error_mark_node)
3586             return error_mark_node;
3587           ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3588                         real, imag);
3589           goto return_build_unary_op;
3590         }
3591
3592       /* Report invalid types.  */
3593
3594       if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3595           && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3596         {
3597           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3598             error_at (location, "wrong type argument to increment");
3599           else
3600             error_at (location, "wrong type argument to decrement");
3601
3602           return error_mark_node;
3603         }
3604
3605       {
3606         tree inc;
3607
3608         argtype = TREE_TYPE (arg);
3609
3610         /* Compute the increment.  */
3611
3612         if (typecode == POINTER_TYPE)
3613           {
3614             /* If pointer target is an undefined struct,
3615                we just cannot know how to do the arithmetic.  */
3616             if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3617               {
3618                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3619                   error_at (location,
3620                             "increment of pointer to unknown structure");
3621                 else
3622                   error_at (location,
3623                             "decrement of pointer to unknown structure");
3624               }
3625             else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3626                      || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3627               {
3628                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3629                   pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3630                            "wrong type argument to increment");
3631                 else
3632                   pedwarn (location, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
3633                            "wrong type argument to decrement");
3634               }
3635
3636             inc = c_size_in_bytes (TREE_TYPE (argtype));
3637             inc = fold_convert_loc (location, sizetype, inc);
3638           }
3639         else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3640           {
3641             /* For signed fract types, we invert ++ to -- or
3642                -- to ++, and change inc from 1 to -1, because
3643                it is not possible to represent 1 in signed fract constants.
3644                For unsigned fract types, the result always overflows and
3645                we get an undefined (original) or the maximum value.  */
3646             if (code == PREINCREMENT_EXPR)
3647               code = PREDECREMENT_EXPR;
3648             else if (code == PREDECREMENT_EXPR)
3649               code = PREINCREMENT_EXPR;
3650             else if (code == POSTINCREMENT_EXPR)
3651               code = POSTDECREMENT_EXPR;
3652             else /* code == POSTDECREMENT_EXPR  */
3653               code = POSTINCREMENT_EXPR;
3654
3655             inc = integer_minus_one_node;
3656             inc = convert (argtype, inc);
3657           }
3658         else
3659           {
3660             inc = integer_one_node;
3661             inc = convert (argtype, inc);
3662           }
3663
3664         /* Report a read-only lvalue.  */
3665         if (TYPE_READONLY (argtype))
3666           {
3667             readonly_error (arg,
3668                             ((code == PREINCREMENT_EXPR
3669                               || code == POSTINCREMENT_EXPR)
3670                              ? lv_increment : lv_decrement));
3671             return error_mark_node;
3672           }
3673         else if (TREE_READONLY (arg))
3674           readonly_warning (arg,
3675                             ((code == PREINCREMENT_EXPR
3676                               || code == POSTINCREMENT_EXPR)
3677                              ? lv_increment : lv_decrement));
3678
3679         if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3680           val = boolean_increment (code, arg);
3681         else
3682           val = build2 (code, TREE_TYPE (arg), arg, inc);
3683         TREE_SIDE_EFFECTS (val) = 1;
3684         if (TREE_CODE (val) != code)
3685           TREE_NO_WARNING (val) = 1;
3686         ret = val;
3687         goto return_build_unary_op;
3688       }
3689
3690     case ADDR_EXPR:
3691       /* Note that this operation never does default_conversion.  */
3692
3693       /* The operand of unary '&' must be an lvalue (which excludes
3694          expressions of type void), or, in C99, the result of a [] or
3695          unary '*' operator.  */
3696       if (VOID_TYPE_P (TREE_TYPE (arg))
3697           && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3698           && (TREE_CODE (arg) != INDIRECT_REF
3699               || !flag_isoc99))
3700         pedwarn (location, 0, "taking address of expression of type %<void%>");
3701
3702       /* Let &* cancel out to simplify resulting code.  */
3703       if (TREE_CODE (arg) == INDIRECT_REF)
3704         {
3705           /* Don't let this be an lvalue.  */
3706           if (lvalue_p (TREE_OPERAND (arg, 0)))
3707             return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3708           ret = TREE_OPERAND (arg, 0);
3709           goto return_build_unary_op;
3710         }
3711
3712       /* For &x[y], return x+y */
3713       if (TREE_CODE (arg) == ARRAY_REF)
3714         {
3715           tree op0 = TREE_OPERAND (arg, 0);
3716           if (!c_mark_addressable (op0))
3717             return error_mark_node;
3718           return build_binary_op (location, PLUS_EXPR,
3719                                   (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE
3720                                    ? array_to_pointer_conversion (location,
3721                                                                   op0)
3722                                    : op0),
3723                                   TREE_OPERAND (arg, 1), 1);
3724         }
3725
3726       /* Anything not already handled and not a true memory reference
3727          or a non-lvalue array is an error.  */
3728       else if (typecode != FUNCTION_TYPE && !flag
3729                && !lvalue_or_else (arg, lv_addressof))
3730         return error_mark_node;
3731
3732       /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3733          folding later.  */
3734       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3735         {
3736           tree inner = build_unary_op (location, code,
3737                                        C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3738           ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3739                         C_MAYBE_CONST_EXPR_PRE (arg), inner);
3740           gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3741           C_MAYBE_CONST_EXPR_NON_CONST (ret)
3742             = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3743           goto return_build_unary_op;
3744         }
3745
3746       /* Ordinary case; arg is a COMPONENT_REF or a decl.  */
3747       argtype = TREE_TYPE (arg);
3748
3749       /* If the lvalue is const or volatile, merge that into the type
3750          to which the address will point.  This should only be needed
3751          for function types.  */
3752       if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3753           && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3754         {
3755           int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3756           int quals = orig_quals;
3757
3758           if (TREE_READONLY (arg))
3759             quals |= TYPE_QUAL_CONST;
3760           if (TREE_THIS_VOLATILE (arg))
3761             quals |= TYPE_QUAL_VOLATILE;
3762
3763           gcc_assert (quals == orig_quals
3764                       || TREE_CODE (argtype) == FUNCTION_TYPE);
3765
3766           argtype = c_build_qualified_type (argtype, quals);
3767         }
3768
3769       if (!c_mark_addressable (arg))
3770         return error_mark_node;
3771
3772       gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3773                   || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3774
3775       argtype = build_pointer_type (argtype);
3776
3777       /* ??? Cope with user tricks that amount to offsetof.  Delete this
3778          when we have proper support for integer constant expressions.  */
3779       val = get_base_address (arg);
3780       if (val && TREE_CODE (val) == INDIRECT_REF
3781           && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3782         {
3783           tree op0 = fold_convert_loc (location, sizetype,
3784                                        fold_offsetof (arg, val)), op1;
3785
3786           op1 = fold_convert_loc (location, argtype, TREE_OPERAND (val, 0));
3787           ret = fold_build2_loc (location, POINTER_PLUS_EXPR, argtype, op1, op0);
3788           goto return_build_unary_op;
3789         }
3790
3791       val = build1 (ADDR_EXPR, argtype, arg);
3792
3793       ret = val;
3794       goto return_build_unary_op;
3795
3796     default:
3797       gcc_unreachable ();
3798     }
3799
3800   if (argtype == 0)
3801     argtype = TREE_TYPE (arg);
3802   if (TREE_CODE (arg) == INTEGER_CST)
3803     ret = (require_constant_value
3804            ? fold_build1_initializer_loc (location, code, argtype, arg)
3805            : fold_build1_loc (location, code, argtype, arg));
3806   else
3807     ret = build1 (code, argtype, arg);
3808  return_build_unary_op:
3809   gcc_assert (ret != error_mark_node);
3810   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3811       && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3812     ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3813   else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3814     ret = note_integer_operands (ret);
3815   if (eptype)
3816     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3817   protected_set_expr_location (ret, location);
3818   return ret;
3819 }
3820
3821 /* Return nonzero if REF is an lvalue valid for this language.
3822    Lvalues can be assigned, unless their type has TYPE_READONLY.
3823    Lvalues can have their address taken, unless they have C_DECL_REGISTER.  */
3824
3825 bool
3826 lvalue_p (const_tree ref)
3827 {
3828   const enum tree_code code = TREE_CODE (ref);
3829
3830   switch (code)
3831     {
3832     case REALPART_EXPR:
3833     case IMAGPART_EXPR:
3834     case COMPONENT_REF:
3835       return lvalue_p (TREE_OPERAND (ref, 0));
3836
3837     case C_MAYBE_CONST_EXPR:
3838       return lvalue_p (TREE_OPERAND (ref, 1));
3839
3840     case COMPOUND_LITERAL_EXPR:
3841     case STRING_CST:
3842       return 1;
3843
3844     case INDIRECT_REF:
3845     case ARRAY_REF:
3846     case VAR_DECL:
3847     case PARM_DECL:
3848     case RESULT_DECL:
3849     case ERROR_MARK:
3850       return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3851               && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3852
3853     case BIND_EXPR:
3854       return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3855
3856     default:
3857       return 0;
3858     }
3859 }
3860 \f
3861 /* Give an error for storing in something that is 'const'.  */
3862
3863 static void
3864 readonly_error (tree arg, enum lvalue_use use)
3865 {
3866   gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
3867               || use == lv_asm);
3868   /* Using this macro rather than (for example) arrays of messages
3869      ensures that all the format strings are checked at compile
3870      time.  */
3871 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A)               \
3872                                    : (use == lv_increment ? (I)         \
3873                                    : (use == lv_decrement ? (D) : (AS))))
3874   if (TREE_CODE (arg) == COMPONENT_REF)
3875     {
3876       if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
3877         readonly_error (TREE_OPERAND (arg, 0), use);
3878       else
3879         error (READONLY_MSG (G_("assignment of read-only member %qD"),
3880                              G_("increment of read-only member %qD"),
3881                              G_("decrement of read-only member %qD"),
3882                              G_("read-only member %qD used as %<asm%> output")),
3883                TREE_OPERAND (arg, 1));
3884     }
3885   else if (TREE_CODE (arg) == VAR_DECL)
3886     error (READONLY_MSG (G_("assignment of read-only variable %qD"),
3887                          G_("increment of read-only variable %qD"),
3888                          G_("decrement of read-only variable %qD"),
3889                          G_("read-only variable %qD used as %<asm%> output")),
3890            arg);
3891   else
3892     error (READONLY_MSG (G_("assignment of read-only location %qE"),
3893                          G_("increment of read-only location %qE"),
3894                          G_("decrement of read-only location %qE"),
3895                          G_("read-only location %qE used as %<asm%> output")),
3896            arg);
3897 }
3898
3899 /* Give a warning for storing in something that is read-only in GCC
3900    terms but not const in ISO C terms.  */
3901
3902 static void
3903 readonly_warning (tree arg, enum lvalue_use use)
3904 {
3905   switch (use)
3906     {
3907     case lv_assign:
3908       warning (0, "assignment of read-only location %qE", arg);
3909       break;
3910     case lv_increment:
3911       warning (0, "increment of read-only location %qE", arg);
3912       break;
3913     case lv_decrement:
3914       warning (0, "decrement of read-only location %qE", arg);
3915       break;
3916     default:
3917       gcc_unreachable ();
3918     }
3919   return;
3920 }
3921
3922
3923 /* Return nonzero if REF is an lvalue valid for this language;
3924    otherwise, print an error message and return zero.  USE says
3925    how the lvalue is being used and so selects the error message.  */
3926
3927 static int
3928 lvalue_or_else (const_tree ref, enum lvalue_use use)
3929 {
3930   int win = lvalue_p (ref);
3931
3932   if (!win)
3933     lvalue_error (use);
3934
3935   return win;
3936 }
3937 \f
3938 /* Mark EXP saying that we need to be able to take the
3939    address of it; it should not be allocated in a register.
3940    Returns true if successful.  */
3941
3942 bool
3943 c_mark_addressable (tree exp)
3944 {
3945   tree x = exp;
3946
3947   while (1)
3948     switch (TREE_CODE (x))
3949       {
3950       case COMPONENT_REF:
3951         if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3952           {
3953             error
3954               ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3955             return false;
3956           }
3957
3958         /* ... fall through ...  */
3959
3960       case ADDR_EXPR:
3961       case ARRAY_REF:
3962       case REALPART_EXPR:
3963       case IMAGPART_EXPR:
3964         x = TREE_OPERAND (x, 0);
3965         break;
3966
3967       case COMPOUND_LITERAL_EXPR:
3968       case CONSTRUCTOR:
3969         TREE_ADDRESSABLE (x) = 1;
3970         return true;
3971
3972       case VAR_DECL:
3973       case CONST_DECL:
3974       case PARM_DECL:
3975       case RESULT_DECL:
3976         if (C_DECL_REGISTER (x)
3977             && DECL_NONLOCAL (x))
3978           {
3979             if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3980               {
3981                 error
3982                   ("global register variable %qD used in nested function", x);
3983                 return false;
3984               }
3985             pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3986           }
3987         else if (C_DECL_REGISTER (x))
3988           {
3989             if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3990               error ("address of global register variable %qD requested", x);
3991             else
3992               error ("address of register variable %qD requested", x);
3993             return false;
3994           }
3995
3996         /* drops in */
3997       case FUNCTION_DECL:
3998         TREE_ADDRESSABLE (x) = 1;
3999         /* drops out */
4000       default:
4001         return true;
4002     }
4003 }
4004 \f
4005 /* Convert EXPR to TYPE, warning about conversion problems with
4006    constants.  SEMANTIC_TYPE is the type this conversion would use
4007    without excess precision. If SEMANTIC_TYPE is NULL, this function
4008    is equivalent to convert_and_check. This function is a wrapper that
4009    handles conversions that may be different than
4010    the usual ones because of excess precision.  */
4011
4012 static tree
4013 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4014 {
4015   if (TREE_TYPE (expr) == type)
4016     return expr;
4017
4018   if (!semantic_type)
4019     return convert_and_check (type, expr);
4020
4021   if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4022       && TREE_TYPE (expr) != semantic_type)
4023     {
4024       /* For integers, we need to check the real conversion, not
4025          the conversion to the excess precision type.  */
4026       expr = convert_and_check (semantic_type, expr);
4027     }
4028   /* Result type is the excess precision type, which should be
4029      large enough, so do not check.  */
4030   return convert (type, expr);
4031 }
4032
4033 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  If
4034    IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4035    if folded to an integer constant then the unselected half may
4036    contain arbitrary operations not normally permitted in constant
4037    expressions.  Set the location of the expression to LOC.  */
4038
4039 tree
4040 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4041                         tree op1, tree op1_original_type, tree op2,
4042                         tree op2_original_type)
4043 {
4044   tree type1;
4045   tree type2;
4046   enum tree_code code1;
4047   enum tree_code code2;
4048   tree result_type = NULL;
4049   tree semantic_result_type = NULL;
4050   tree orig_op1 = op1, orig_op2 = op2;
4051   bool int_const, op1_int_operands, op2_int_operands, int_operands;
4052   bool ifexp_int_operands;
4053   tree ret;
4054
4055   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4056   if (op1_int_operands)
4057     op1 = remove_c_maybe_const_expr (op1);
4058   op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4059   if (op2_int_operands)
4060     op2 = remove_c_maybe_const_expr (op2);
4061   ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4062   if (ifexp_int_operands)
4063     ifexp = remove_c_maybe_const_expr (ifexp);
4064
4065   /* Promote both alternatives.  */
4066
4067   if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4068     op1 = default_conversion (op1);
4069   if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4070     op2 = default_conversion (op2);
4071
4072   if (TREE_CODE (ifexp) == ERROR_MARK
4073       || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4074       || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4075     return error_mark_node;
4076
4077   type1 = TREE_TYPE (op1);
4078   code1 = TREE_CODE (type1);
4079   type2 = TREE_TYPE (op2);
4080   code2 = TREE_CODE (type2);
4081
4082   /* C90 does not permit non-lvalue arrays in conditional expressions.
4083      In C99 they will be pointers by now.  */
4084   if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4085     {
4086       error_at (colon_loc, "non-lvalue array in conditional expression");
4087       return error_mark_node;
4088     }
4089
4090   if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4091        || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4092       && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4093           || code1 == COMPLEX_TYPE)
4094       && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4095           || code2 == COMPLEX_TYPE))
4096     {
4097       semantic_result_type = c_common_type (type1, type2);
4098       if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4099         {
4100           op1 = TREE_OPERAND (op1, 0);
4101           type1 = TREE_TYPE (op1);
4102           gcc_assert (TREE_CODE (type1) == code1);
4103         }
4104       if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4105         {
4106           op2 = TREE_OPERAND (op2, 0);
4107           type2 = TREE_TYPE (op2);
4108           gcc_assert (TREE_CODE (type2) == code2);
4109         }
4110     }
4111
4112   if (warn_cxx_compat)
4113     {
4114       tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4115       tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4116
4117       if (TREE_CODE (t1) == ENUMERAL_TYPE
4118           && TREE_CODE (t2) == ENUMERAL_TYPE
4119           && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4120         warning_at (colon_loc, OPT_Wc___compat,
4121                     ("different enum types in conditional is "
4122                      "invalid in C++: %qT vs %qT"),
4123                     t1, t2);
4124     }
4125
4126   /* Quickly detect the usual case where op1 and op2 have the same type
4127      after promotion.  */
4128   if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4129     {
4130       if (type1 == type2)
4131         result_type = type1;
4132       else
4133         result_type = TYPE_MAIN_VARIANT (type1);
4134     }
4135   else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4136             || code1 == COMPLEX_TYPE)
4137            && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4138                || code2 == COMPLEX_TYPE))
4139     {
4140       result_type = c_common_type (type1, type2);
4141       do_warn_double_promotion (result_type, type1, type2,
4142                                 "implicit conversion from %qT to %qT to "
4143                                 "match other result of conditional",
4144                                 colon_loc);
4145
4146       /* If -Wsign-compare, warn here if type1 and type2 have
4147          different signedness.  We'll promote the signed to unsigned
4148          and later code won't know it used to be different.
4149          Do this check on the original types, so that explicit casts
4150          will be considered, but default promotions won't.  */
4151       if (c_inhibit_evaluation_warnings == 0)
4152         {
4153           int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4154           int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4155
4156           if (unsigned_op1 ^ unsigned_op2)
4157             {
4158               bool ovf;
4159
4160               /* Do not warn if the result type is signed, since the
4161                  signed type will only be chosen if it can represent
4162                  all the values of the unsigned type.  */
4163               if (!TYPE_UNSIGNED (result_type))
4164                 /* OK */;
4165               else
4166                 {
4167                   bool op1_maybe_const = true;
4168                   bool op2_maybe_const = true;
4169
4170                   /* Do not warn if the signed quantity is an
4171                      unsuffixed integer literal (or some static
4172                      constant expression involving such literals) and
4173                      it is non-negative.  This warning requires the
4174                      operands to be folded for best results, so do
4175                      that folding in this case even without
4176                      warn_sign_compare to avoid warning options
4177                      possibly affecting code generation.  */
4178                   c_inhibit_evaluation_warnings
4179                     += (ifexp == truthvalue_false_node);
4180                   op1 = c_fully_fold (op1, require_constant_value,
4181                                       &op1_maybe_const);
4182                   c_inhibit_evaluation_warnings
4183                     -= (ifexp == truthvalue_false_node);
4184
4185                   c_inhibit_evaluation_warnings
4186                     += (ifexp == truthvalue_true_node);
4187                   op2 = c_fully_fold (op2, require_constant_value,
4188                                       &op2_maybe_const);
4189                   c_inhibit_evaluation_warnings
4190                     -= (ifexp == truthvalue_true_node);
4191
4192                   if (warn_sign_compare)
4193                     {
4194                       if ((unsigned_op2
4195                            && tree_expr_nonnegative_warnv_p (op1, &ovf))
4196                           || (unsigned_op1
4197                               && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4198                         /* OK */;
4199                       else
4200                         warning_at (colon_loc, OPT_Wsign_compare,
4201                                     ("signed and unsigned type in "
4202                                      "conditional expression"));
4203                     }
4204                   if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4205                     op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4206                   if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4207                     op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4208                 }
4209             }
4210         }
4211     }
4212   else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4213     {
4214       if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4215         pedwarn (colon_loc, OPT_pedantic,
4216                  "ISO C forbids conditional expr with only one void side");
4217       result_type = void_type_node;
4218     }
4219   else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4220     {
4221       addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4222       addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4223       addr_space_t as_common;
4224
4225       if (comp_target_types (colon_loc, type1, type2))
4226         result_type = common_pointer_type (type1, type2);
4227       else if (null_pointer_constant_p (orig_op1))
4228         result_type = type2;
4229       else if (null_pointer_constant_p (orig_op2))
4230         result_type = type1;
4231       else if (!addr_space_superset (as1, as2, &as_common))
4232         {
4233           error_at (colon_loc, "pointers to disjoint address spaces "
4234                     "used in conditional expression");
4235           return error_mark_node;
4236         }
4237       else if (VOID_TYPE_P (TREE_TYPE (type1)))
4238         {
4239           if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4240             pedwarn (colon_loc, OPT_pedantic,
4241                      "ISO C forbids conditional expr between "
4242                      "%<void *%> and function pointer");
4243           result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4244                                                           TREE_TYPE (type2)));
4245         }
4246       else if (VOID_TYPE_P (TREE_TYPE (type2)))
4247         {
4248           if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4249             pedwarn (colon_loc, OPT_pedantic,
4250                      "ISO C forbids conditional expr between "
4251                      "%<void *%> and function pointer");
4252           result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4253                                                           TREE_TYPE (type1)));
4254         }
4255       /* Objective-C pointer comparisons are a bit more lenient.  */
4256       else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4257         result_type = objc_common_type (type1, type2);
4258       else
4259         {
4260           int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4261
4262           pedwarn (colon_loc, 0,
4263                    "pointer type mismatch in conditional expression");
4264           result_type = build_pointer_type
4265                           (build_qualified_type (void_type_node, qual));
4266         }
4267     }
4268   else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4269     {
4270       if (!null_pointer_constant_p (orig_op2))
4271         pedwarn (colon_loc, 0,
4272                  "pointer/integer type mismatch in conditional expression");
4273       else
4274         {
4275           op2 = null_pointer_node;
4276         }
4277       result_type = type1;
4278     }
4279   else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4280     {
4281       if (!null_pointer_constant_p (orig_op1))
4282         pedwarn (colon_loc, 0,
4283                  "pointer/integer type mismatch in conditional expression");
4284       else
4285         {
4286           op1 = null_pointer_node;
4287         }
4288       result_type = type2;
4289     }
4290
4291   if (!result_type)
4292     {
4293       if (flag_cond_mismatch)
4294         result_type = void_type_node;
4295       else
4296         {
4297           error_at (colon_loc, "type mismatch in conditional expression");
4298           return error_mark_node;
4299         }
4300     }
4301
4302   /* Merge const and volatile flags of the incoming types.  */
4303   result_type
4304     = build_type_variant (result_type,
4305                           TYPE_READONLY (type1) || TYPE_READONLY (type2),
4306                           TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4307
4308   op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4309   op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4310
4311   if (ifexp_bcp && ifexp == truthvalue_true_node)
4312     {
4313       op2_int_operands = true;
4314       op1 = c_fully_fold (op1, require_constant_value, NULL);
4315     }
4316   if (ifexp_bcp && ifexp == truthvalue_false_node)
4317     {
4318       op1_int_operands = true;
4319       op2 = c_fully_fold (op2, require_constant_value, NULL);
4320     }
4321   int_const = int_operands = (ifexp_int_operands
4322                               && op1_int_operands
4323                               && op2_int_operands);
4324   if (int_operands)
4325     {
4326       int_const = ((ifexp == truthvalue_true_node
4327                     && TREE_CODE (orig_op1) == INTEGER_CST
4328                     && !TREE_OVERFLOW (orig_op1))
4329                    || (ifexp == truthvalue_false_node
4330                        && TREE_CODE (orig_op2) == INTEGER_CST
4331                        && !TREE_OVERFLOW (orig_op2)));
4332     }
4333   if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4334     ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4335   else
4336     {
4337       ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4338       if (int_operands)
4339         ret = note_integer_operands (ret);
4340     }
4341   if (semantic_result_type)
4342     ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4343
4344   protected_set_expr_location (ret, colon_loc);
4345   return ret;
4346 }
4347 \f
4348 /* Return a compound expression that performs two expressions and
4349    returns the value of the second of them.
4350
4351    LOC is the location of the COMPOUND_EXPR.  */
4352
4353 tree
4354 build_compound_expr (location_t loc, tree expr1, tree expr2)
4355 {
4356   bool expr1_int_operands, expr2_int_operands;
4357   tree eptype = NULL_TREE;
4358   tree ret;
4359
4360   expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4361   if (expr1_int_operands)
4362     expr1 = remove_c_maybe_const_expr (expr1);
4363   expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4364   if (expr2_int_operands)
4365     expr2 = remove_c_maybe_const_expr (expr2);
4366
4367   if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4368     expr1 = TREE_OPERAND (expr1, 0);
4369   if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4370     {
4371       eptype = TREE_TYPE (expr2);
4372       expr2 = TREE_OPERAND (expr2, 0);
4373     }
4374
4375   if (!TREE_SIDE_EFFECTS (expr1))
4376     {
4377       /* The left-hand operand of a comma expression is like an expression
4378          statement: with -Wunused, we should warn if it doesn't have
4379          any side-effects, unless it was explicitly cast to (void).  */
4380       if (warn_unused_value)
4381         {
4382           if (VOID_TYPE_P (TREE_TYPE (expr1))
4383               && CONVERT_EXPR_P (expr1))
4384             ; /* (void) a, b */
4385           else if (VOID_TYPE_P (TREE_TYPE (expr1))
4386                    && TREE_CODE (expr1) == COMPOUND_EXPR
4387                    && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4388             ; /* (void) a, (void) b, c */
4389           else
4390             warning_at (loc, OPT_Wunused_value,
4391                         "left-hand operand of comma expression has no effect");
4392         }
4393     }
4394
4395   /* With -Wunused, we should also warn if the left-hand operand does have
4396      side-effects, but computes a value which is not used.  For example, in
4397      `foo() + bar(), baz()' the result of the `+' operator is not used,
4398      so we should issue a warning.  */
4399   else if (warn_unused_value)
4400     warn_if_unused_value (expr1, loc);
4401
4402   if (expr2 == error_mark_node)
4403     return error_mark_node;
4404
4405   ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4406
4407   if (flag_isoc99
4408       && expr1_int_operands
4409       && expr2_int_operands)
4410     ret = note_integer_operands (ret);
4411
4412   if (eptype)
4413     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4414
4415   protected_set_expr_location (ret, loc);
4416   return ret;
4417 }
4418
4419 /* Issue -Wcast-qual warnings when appropriate.  TYPE is the type to
4420    which we are casting.  OTYPE is the type of the expression being
4421    cast.  Both TYPE and OTYPE are pointer types.  LOC is the location
4422    of the cast.  -Wcast-qual appeared on the command line.  Named
4423    address space qualifiers are not handled here, because they result
4424    in different warnings.  */
4425
4426 static void
4427 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4428 {
4429   tree in_type = type;
4430   tree in_otype = otype;
4431   int added = 0;
4432   int discarded = 0;
4433   bool is_const;
4434
4435   /* Check that the qualifiers on IN_TYPE are a superset of the
4436      qualifiers of IN_OTYPE.  The outermost level of POINTER_TYPE
4437      nodes is uninteresting and we stop as soon as we hit a
4438      non-POINTER_TYPE node on either type.  */
4439   do
4440     {
4441       in_otype = TREE_TYPE (in_otype);
4442       in_type = TREE_TYPE (in_type);
4443
4444       /* GNU C allows cv-qualified function types.  'const' means the
4445          function is very pure, 'volatile' means it can't return.  We
4446          need to warn when such qualifiers are added, not when they're
4447          taken away.  */
4448       if (TREE_CODE (in_otype) == FUNCTION_TYPE
4449           && TREE_CODE (in_type) == FUNCTION_TYPE)
4450         added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4451                   & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4452       else
4453         discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4454                       & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4455     }
4456   while (TREE_CODE (in_type) == POINTER_TYPE
4457          && TREE_CODE (in_otype) == POINTER_TYPE);
4458
4459   if (added)
4460     warning_at (loc, OPT_Wcast_qual,
4461                 "cast adds %q#v qualifier to function type", added);
4462
4463   if (discarded)
4464     /* There are qualifiers present in IN_OTYPE that are not present
4465        in IN_TYPE.  */
4466     warning_at (loc, OPT_Wcast_qual,
4467                 "cast discards %q#v qualifier from pointer target type",
4468                 discarded);
4469
4470   if (added || discarded)
4471     return;
4472
4473   /* A cast from **T to const **T is unsafe, because it can cause a
4474      const value to be changed with no additional warning.  We only
4475      issue this warning if T is the same on both sides, and we only
4476      issue the warning if there are the same number of pointers on
4477      both sides, as otherwise the cast is clearly unsafe anyhow.  A
4478      cast is unsafe when a qualifier is added at one level and const
4479      is not present at all outer levels.
4480
4481      To issue this warning, we check at each level whether the cast
4482      adds new qualifiers not already seen.  We don't need to special
4483      case function types, as they won't have the same
4484      TYPE_MAIN_VARIANT.  */
4485
4486   if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4487     return;
4488   if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4489     return;
4490
4491   in_type = type;
4492   in_otype = otype;
4493   is_const = TYPE_READONLY (TREE_TYPE (in_type));
4494   do
4495     {
4496       in_type = TREE_TYPE (in_type);
4497       in_otype = TREE_TYPE (in_otype);
4498       if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4499           && !is_const)
4500         {
4501           warning_at (loc, OPT_Wcast_qual,
4502                       "to be safe all intermediate pointers in cast from "
4503                       "%qT to %qT must be %<const%> qualified",
4504                       otype, type);
4505           break;
4506         }
4507       if (is_const)
4508         is_const = TYPE_READONLY (in_type);
4509     }
4510   while (TREE_CODE (in_type) == POINTER_TYPE);
4511 }
4512
4513 /* Build an expression representing a cast to type TYPE of expression EXPR.
4514    LOC is the location of the cast-- typically the open paren of the cast.  */
4515
4516 tree
4517 build_c_cast (location_t loc, tree type, tree expr)
4518 {
4519   tree value;
4520
4521   if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4522     expr = TREE_OPERAND (expr, 0);
4523
4524   value = expr;
4525
4526   if (type == error_mark_node || expr == error_mark_node)
4527     return error_mark_node;
4528
4529   /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4530      only in <protocol> qualifications.  But when constructing cast expressions,
4531      the protocols do matter and must be kept around.  */
4532   if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4533     return build1 (NOP_EXPR, type, expr);
4534
4535   type = TYPE_MAIN_VARIANT (type);
4536
4537   if (TREE_CODE (type) == ARRAY_TYPE)
4538     {
4539       error_at (loc, "cast specifies array type");
4540       return error_mark_node;
4541     }
4542
4543   if (TREE_CODE (type) == FUNCTION_TYPE)
4544     {
4545       error_at (loc, "cast specifies function type");
4546       return error_mark_node;
4547     }
4548
4549   if (!VOID_TYPE_P (type))
4550     {
4551       value = require_complete_type (value);
4552       if (value == error_mark_node)
4553         return error_mark_node;
4554     }
4555
4556   if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4557     {
4558       if (TREE_CODE (type) == RECORD_TYPE
4559           || TREE_CODE (type) == UNION_TYPE)
4560         pedwarn (loc, OPT_pedantic,
4561                  "ISO C forbids casting nonscalar to the same type");
4562     }
4563   else if (TREE_CODE (type) == UNION_TYPE)
4564     {
4565       tree field;
4566
4567       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4568         if (TREE_TYPE (field) != error_mark_node
4569             && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4570                           TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4571           break;
4572
4573       if (field)
4574         {
4575           tree t;
4576           bool maybe_const = true;
4577
4578           pedwarn (loc, OPT_pedantic, "ISO C forbids casts to union type");
4579           t = c_fully_fold (value, false, &maybe_const);
4580           t = build_constructor_single (type, field, t);
4581           if (!maybe_const)
4582             t = c_wrap_maybe_const (t, true);
4583           t = digest_init (loc, type, t,
4584                            NULL_TREE, false, true, 0);
4585           TREE_CONSTANT (t) = TREE_CONSTANT (value);
4586           return t;
4587         }
4588       error_at (loc, "cast to union type from type not present in union");
4589       return error_mark_node;
4590     }
4591   else
4592     {
4593       tree otype, ovalue;
4594
4595       if (type == void_type_node)
4596         {
4597           tree t = build1 (CONVERT_EXPR, type, value);
4598           SET_EXPR_LOCATION (t, loc);
4599           return t;
4600         }
4601
4602       otype = TREE_TYPE (value);
4603
4604       /* Optionally warn about potentially worrisome casts.  */
4605       if (warn_cast_qual
4606           && TREE_CODE (type) == POINTER_TYPE
4607           && TREE_CODE (otype) == POINTER_TYPE)
4608         handle_warn_cast_qual (loc, type, otype);
4609
4610       /* Warn about conversions between pointers to disjoint
4611          address spaces.  */
4612       if (TREE_CODE (type) == POINTER_TYPE
4613           && TREE_CODE (otype) == POINTER_TYPE
4614           && !null_pointer_constant_p (value))
4615         {
4616           addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4617           addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4618           addr_space_t as_common;
4619
4620           if (!addr_space_superset (as_to, as_from, &as_common))
4621             {
4622               if (ADDR_SPACE_GENERIC_P (as_from))
4623                 warning_at (loc, 0, "cast to %s address space pointer "
4624                             "from disjoint generic address space pointer",
4625                             c_addr_space_name (as_to));
4626
4627               else if (ADDR_SPACE_GENERIC_P (as_to))
4628                 warning_at (loc, 0, "cast to generic address space pointer "
4629                             "from disjoint %s address space pointer",
4630                             c_addr_space_name (as_from));
4631
4632               else
4633                 warning_at (loc, 0, "cast to %s address space pointer "
4634                             "from disjoint %s address space pointer",
4635                             c_addr_space_name (as_to),
4636                             c_addr_space_name (as_from));
4637             }
4638         }
4639
4640       /* Warn about possible alignment problems.  */
4641       if (STRICT_ALIGNMENT
4642           && TREE_CODE (type) == POINTER_TYPE
4643           && TREE_CODE (otype) == POINTER_TYPE
4644           && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4645           && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4646           /* Don't warn about opaque types, where the actual alignment
4647              restriction is unknown.  */
4648           && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4649                 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4650                && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4651           && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4652         warning_at (loc, OPT_Wcast_align,
4653                     "cast increases required alignment of target type");
4654
4655       if (TREE_CODE (type) == INTEGER_TYPE
4656           && TREE_CODE (otype) == POINTER_TYPE
4657           && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4658       /* Unlike conversion of integers to pointers, where the
4659          warning is disabled for converting constants because
4660          of cases such as SIG_*, warn about converting constant
4661          pointers to integers. In some cases it may cause unwanted
4662          sign extension, and a warning is appropriate.  */
4663         warning_at (loc, OPT_Wpointer_to_int_cast,
4664                     "cast from pointer to integer of different size");
4665
4666       if (TREE_CODE (value) == CALL_EXPR
4667           && TREE_CODE (type) != TREE_CODE (otype))
4668         warning_at (loc, OPT_Wbad_function_cast,
4669                     "cast from function call of type %qT "
4670                     "to non-matching type %qT", otype, type);
4671
4672       if (TREE_CODE (type) == POINTER_TYPE
4673           && TREE_CODE (otype) == INTEGER_TYPE
4674           && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4675           /* Don't warn about converting any constant.  */
4676           && !TREE_CONSTANT (value))
4677         warning_at (loc,
4678                     OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4679                     "of different size");
4680
4681       if (warn_strict_aliasing <= 2)
4682         strict_aliasing_warning (otype, type, expr);
4683
4684       /* If pedantic, warn for conversions between function and object
4685          pointer types, except for converting a null pointer constant
4686          to function pointer type.  */
4687       if (pedantic
4688           && TREE_CODE (type) == POINTER_TYPE
4689           && TREE_CODE (otype) == POINTER_TYPE
4690           && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4691           && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4692         pedwarn (loc, OPT_pedantic, "ISO C forbids "
4693                  "conversion of function pointer to object pointer type");
4694
4695       if (pedantic
4696           && TREE_CODE (type) == POINTER_TYPE
4697           && TREE_CODE (otype) == POINTER_TYPE
4698           && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4699           && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4700           && !null_pointer_constant_p (value))
4701         pedwarn (loc, OPT_pedantic, "ISO C forbids "
4702                  "conversion of object pointer to function pointer type");
4703
4704       ovalue = value;
4705       value = convert (type, value);
4706
4707       /* Ignore any integer overflow caused by the cast.  */
4708       if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4709         {
4710           if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4711             {
4712               if (!TREE_OVERFLOW (value))
4713                 {
4714                   /* Avoid clobbering a shared constant.  */
4715                   value = copy_node (value);
4716                   TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4717                 }
4718             }
4719           else if (TREE_OVERFLOW (value))
4720             /* Reset VALUE's overflow flags, ensuring constant sharing.  */
4721             value = build_int_cst_wide (TREE_TYPE (value),
4722                                         TREE_INT_CST_LOW (value),
4723                                         TREE_INT_CST_HIGH (value));
4724         }
4725     }
4726
4727   /* Don't let a cast be an lvalue.  */
4728   if (value == expr)
4729     value = non_lvalue_loc (loc, value);
4730
4731   /* Don't allow the results of casting to floating-point or complex
4732      types be confused with actual constants, or casts involving
4733      integer and pointer types other than direct integer-to-integer
4734      and integer-to-pointer be confused with integer constant
4735      expressions and null pointer constants.  */
4736   if (TREE_CODE (value) == REAL_CST
4737       || TREE_CODE (value) == COMPLEX_CST
4738       || (TREE_CODE (value) == INTEGER_CST
4739           && !((TREE_CODE (expr) == INTEGER_CST
4740                 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4741                || TREE_CODE (expr) == REAL_CST
4742                || TREE_CODE (expr) == COMPLEX_CST)))
4743       value = build1 (NOP_EXPR, type, value);
4744
4745   if (CAN_HAVE_LOCATION_P (value))
4746     SET_EXPR_LOCATION (value, loc);
4747   return value;
4748 }
4749
4750 /* Interpret a cast of expression EXPR to type TYPE.  LOC is the
4751    location of the open paren of the cast, or the position of the cast
4752    expr.  */
4753 tree
4754 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4755 {
4756   tree type;
4757   tree type_expr = NULL_TREE;
4758   bool type_expr_const = true;
4759   tree ret;
4760   int saved_wsp = warn_strict_prototypes;
4761
4762   /* This avoids warnings about unprototyped casts on
4763      integers.  E.g. "#define SIG_DFL (void(*)())0".  */
4764   if (TREE_CODE (expr) == INTEGER_CST)
4765     warn_strict_prototypes = 0;
4766   type = groktypename (type_name, &type_expr, &type_expr_const);
4767   warn_strict_prototypes = saved_wsp;
4768
4769   ret = build_c_cast (loc, type, expr);
4770   if (type_expr)
4771     {
4772       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4773       C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
4774       SET_EXPR_LOCATION (ret, loc);
4775     }
4776
4777   if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4778     SET_EXPR_LOCATION (ret, loc);
4779
4780   /* C++ does not permits types to be defined in a cast.  */
4781   if (warn_cxx_compat && type_name->specs->tag_defined_p)
4782     warning_at (loc, OPT_Wc___compat,
4783                 "defining a type in a cast is invalid in C++");
4784
4785   return ret;
4786 }
4787 \f
4788 /* Build an assignment expression of lvalue LHS from value RHS.
4789    If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4790    may differ from TREE_TYPE (LHS) for an enum bitfield.
4791    MODIFYCODE is the code for a binary operator that we use
4792    to combine the old value of LHS with RHS to get the new value.
4793    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4794    If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4795    which may differ from TREE_TYPE (RHS) for an enum value.
4796
4797    LOCATION is the location of the MODIFYCODE operator.
4798    RHS_LOC is the location of the RHS.  */
4799
4800 tree
4801 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4802                    enum tree_code modifycode,
4803                    location_t rhs_loc, tree rhs, tree rhs_origtype)
4804 {
4805   tree result;
4806   tree newrhs;
4807   tree rhs_semantic_type = NULL_TREE;
4808   tree lhstype = TREE_TYPE (lhs);
4809   tree olhstype = lhstype;
4810   bool npc;
4811
4812   /* Types that aren't fully specified cannot be used in assignments.  */
4813   lhs = require_complete_type (lhs);
4814
4815   /* Avoid duplicate error messages from operands that had errors.  */
4816   if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4817     return error_mark_node;
4818
4819   if (!lvalue_or_else (lhs, lv_assign))
4820     return error_mark_node;
4821
4822   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4823     {
4824       rhs_semantic_type = TREE_TYPE (rhs);
4825       rhs = TREE_OPERAND (rhs, 0);
4826     }
4827
4828   newrhs = rhs;
4829
4830   if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4831     {
4832       tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4833                                       lhs_origtype, modifycode, rhs_loc, rhs,
4834                                       rhs_origtype);
4835       if (inner == error_mark_node)
4836         return error_mark_node;
4837       result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4838                        C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4839       gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4840       C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4841       protected_set_expr_location (result, location);
4842       return result;
4843     }
4844
4845   /* If a binary op has been requested, combine the old LHS value with the RHS
4846      producing the value we should actually store into the LHS.  */
4847
4848   if (modifycode != NOP_EXPR)
4849     {
4850       lhs = c_fully_fold (lhs, false, NULL);
4851       lhs = stabilize_reference (lhs);
4852       newrhs = build_binary_op (location,
4853                                 modifycode, lhs, rhs, 1);
4854
4855       /* The original type of the right hand side is no longer
4856          meaningful.  */
4857       rhs_origtype = NULL_TREE;
4858     }
4859
4860   /* Give an error for storing in something that is 'const'.  */
4861
4862   if (TYPE_READONLY (lhstype)
4863       || ((TREE_CODE (lhstype) == RECORD_TYPE
4864            || TREE_CODE (lhstype) == UNION_TYPE)
4865           && C_TYPE_FIELDS_READONLY (lhstype)))
4866     {
4867       readonly_error (lhs, lv_assign);
4868       return error_mark_node;
4869     }
4870   else if (TREE_READONLY (lhs))
4871     readonly_warning (lhs, lv_assign);
4872
4873   /* If storing into a structure or union member,
4874      it has probably been given type `int'.
4875      Compute the type that would go with
4876      the actual amount of storage the member occupies.  */
4877
4878   if (TREE_CODE (lhs) == COMPONENT_REF
4879       && (TREE_CODE (lhstype) == INTEGER_TYPE
4880           || TREE_CODE (lhstype) == BOOLEAN_TYPE
4881           || TREE_CODE (lhstype) == REAL_TYPE
4882           || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4883     lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4884
4885   /* If storing in a field that is in actuality a short or narrower than one,
4886      we must store in the field in its actual type.  */
4887
4888   if (lhstype != TREE_TYPE (lhs))
4889     {
4890       lhs = copy_node (lhs);
4891       TREE_TYPE (lhs) = lhstype;
4892     }
4893
4894   /* Issue -Wc++-compat warnings about an assignment to an enum type
4895      when LHS does not have its original type.  This happens for,
4896      e.g., an enum bitfield in a struct.  */
4897   if (warn_cxx_compat
4898       && lhs_origtype != NULL_TREE
4899       && lhs_origtype != lhstype
4900       && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4901     {
4902       tree checktype = (rhs_origtype != NULL_TREE
4903                         ? rhs_origtype
4904                         : TREE_TYPE (rhs));
4905       if (checktype != error_mark_node
4906           && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4907         warning_at (location, OPT_Wc___compat,
4908                     "enum conversion in assignment is invalid in C++");
4909     }
4910
4911   /* Convert new value to destination type.  Fold it first, then
4912      restore any excess precision information, for the sake of
4913      conversion warnings.  */
4914
4915   npc = null_pointer_constant_p (newrhs);
4916   newrhs = c_fully_fold (newrhs, false, NULL);
4917   if (rhs_semantic_type)
4918     newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4919   newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4920                                    ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4921   if (TREE_CODE (newrhs) == ERROR_MARK)
4922     return error_mark_node;
4923
4924   /* Emit ObjC write barrier, if necessary.  */
4925   if (c_dialect_objc () && flag_objc_gc)
4926     {
4927       result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4928       if (result)
4929         {
4930           protected_set_expr_location (result, location);
4931           return result;
4932         }
4933     }
4934
4935   /* Scan operands.  */
4936
4937   result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4938   TREE_SIDE_EFFECTS (result) = 1;
4939   protected_set_expr_location (result, location);
4940
4941   /* If we got the LHS in a different type for storing in,
4942      convert the result back to the nominal type of LHS
4943      so that the value we return always has the same type
4944      as the LHS argument.  */
4945
4946   if (olhstype == TREE_TYPE (result))
4947     return result;
4948
4949   result = convert_for_assignment (location, olhstype, result, rhs_origtype,
4950                                    ic_assign, false, NULL_TREE, NULL_TREE, 0);
4951   protected_set_expr_location (result, location);
4952   return result;
4953 }
4954 \f
4955 /* Convert value RHS to type TYPE as preparation for an assignment to
4956    an lvalue of type TYPE.  If ORIGTYPE is not NULL_TREE, it is the
4957    original type of RHS; this differs from TREE_TYPE (RHS) for enum
4958    types.  NULL_POINTER_CONSTANT says whether RHS was a null pointer
4959    constant before any folding.
4960    The real work of conversion is done by `convert'.
4961    The purpose of this function is to generate error messages
4962    for assignments that are not allowed in C.
4963    ERRTYPE says whether it is argument passing, assignment,
4964    initialization or return.
4965
4966    LOCATION is the location of the RHS.
4967    FUNCTION is a tree for the function being called.
4968    PARMNUM is the number of the argument, for printing in error messages.  */
4969
4970 static tree
4971 convert_for_assignment (location_t location, tree type, tree rhs,
4972                         tree origtype, enum impl_conv errtype,
4973                         bool null_pointer_constant, tree fundecl,
4974                         tree function, int parmnum)
4975 {
4976   enum tree_code codel = TREE_CODE (type);
4977   tree orig_rhs = rhs;
4978   tree rhstype;
4979   enum tree_code coder;
4980   tree rname = NULL_TREE;
4981   bool objc_ok = false;
4982
4983   if (errtype == ic_argpass)
4984     {
4985       tree selector;
4986       /* Change pointer to function to the function itself for
4987          diagnostics.  */
4988       if (TREE_CODE (function) == ADDR_EXPR
4989           && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
4990         function = TREE_OPERAND (function, 0);
4991
4992       /* Handle an ObjC selector specially for diagnostics.  */
4993       selector = objc_message_selector ();
4994       rname = function;
4995       if (selector && parmnum > 2)
4996         {
4997           rname = selector;
4998           parmnum -= 2;
4999         }
5000     }
5001
5002   /* This macro is used to emit diagnostics to ensure that all format
5003      strings are complete sentences, visible to gettext and checked at
5004      compile time.  */
5005 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE)               \
5006   do {                                                                   \
5007     switch (errtype)                                                     \
5008       {                                                                  \
5009       case ic_argpass:                                                   \
5010         if (pedwarn (LOCATION, OPT, AR, parmnum, rname))                 \
5011           inform ((fundecl && !DECL_IS_BUILTIN (fundecl))                \
5012                   ? DECL_SOURCE_LOCATION (fundecl) : LOCATION,           \
5013                   "expected %qT but argument is of type %qT",            \
5014                   type, rhstype);                                        \
5015         break;                                                           \
5016       case ic_assign:                                                    \
5017         pedwarn (LOCATION, OPT, AS);                                     \
5018         break;                                                           \
5019       case ic_init:                                                      \
5020         pedwarn_init (LOCATION, OPT, IN);                                \
5021         break;                                                           \
5022       case ic_return:                                                    \
5023         pedwarn (LOCATION, OPT, RE);                                     \
5024         break;                                                           \
5025       default:                                                           \
5026         gcc_unreachable ();                                              \
5027       }                                                                  \
5028   } while (0)
5029
5030   /* This macro is used to emit diagnostics to ensure that all format
5031      strings are complete sentences, visible to gettext and checked at
5032      compile time.  It is the same as WARN_FOR_ASSIGNMENT but with an
5033      extra parameter to enumerate qualifiers.  */
5034
5035 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS)        \
5036   do {                                                                   \
5037     switch (errtype)                                                     \
5038       {                                                                  \
5039       case ic_argpass:                                                   \
5040         if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS))          \
5041           inform ((fundecl && !DECL_IS_BUILTIN (fundecl))                \
5042                   ? DECL_SOURCE_LOCATION (fundecl) : LOCATION,           \
5043                   "expected %qT but argument is of type %qT",            \
5044                   type, rhstype);                                        \
5045         break;                                                           \
5046       case ic_assign:                                                    \
5047         pedwarn (LOCATION, OPT, AS, QUALS);                          \
5048         break;                                                           \
5049       case ic_init:                                                      \
5050         pedwarn (LOCATION, OPT, IN, QUALS);                          \
5051         break;                                                           \
5052       case ic_return:                                                    \
5053         pedwarn (LOCATION, OPT, RE, QUALS);                              \
5054         break;                                                           \
5055       default:                                                           \
5056         gcc_unreachable ();                                              \
5057       }                                                                  \
5058   } while (0)
5059
5060   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5061     rhs = TREE_OPERAND (rhs, 0);
5062
5063   rhstype = TREE_TYPE (rhs);
5064   coder = TREE_CODE (rhstype);
5065
5066   if (coder == ERROR_MARK)
5067     return error_mark_node;
5068
5069   if (c_dialect_objc ())
5070     {
5071       int parmno;
5072
5073       switch (errtype)
5074         {
5075         case ic_return:
5076           parmno = 0;
5077           break;
5078
5079         case ic_assign:
5080           parmno = -1;
5081           break;
5082
5083         case ic_init:
5084           parmno = -2;
5085           break;
5086
5087         default:
5088           parmno = parmnum;
5089           break;
5090         }
5091
5092       objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5093     }
5094
5095   if (warn_cxx_compat)
5096     {
5097       tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5098       if (checktype != error_mark_node
5099           && TREE_CODE (type) == ENUMERAL_TYPE
5100           && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5101         {
5102           WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5103                                G_("enum conversion when passing argument "
5104                                   "%d of %qE is invalid in C++"),
5105                                G_("enum conversion in assignment is "
5106                                   "invalid in C++"),
5107                                G_("enum conversion in initialization is "
5108                                   "invalid in C++"),
5109                                G_("enum conversion in return is "
5110                                   "invalid in C++"));
5111         }
5112     }
5113
5114   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5115     return rhs;
5116
5117   if (coder == VOID_TYPE)
5118     {
5119       /* Except for passing an argument to an unprototyped function,
5120          this is a constraint violation.  When passing an argument to
5121          an unprototyped function, it is compile-time undefined;
5122          making it a constraint in that case was rejected in
5123          DR#252.  */
5124       error_at (location, "void value not ignored as it ought to be");
5125       return error_mark_node;
5126     }
5127   rhs = require_complete_type (rhs);
5128   if (rhs == error_mark_node)
5129     return error_mark_node;
5130   /* A type converts to a reference to it.
5131      This code doesn't fully support references, it's just for the
5132      special case of va_start and va_copy.  */
5133   if (codel == REFERENCE_TYPE
5134       && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5135     {
5136       if (!lvalue_p (rhs))
5137         {
5138           error_at (location, "cannot pass rvalue to reference parameter");
5139           return error_mark_node;
5140         }
5141       if (!c_mark_addressable (rhs))
5142         return error_mark_node;
5143       rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5144       SET_EXPR_LOCATION (rhs, location);
5145
5146       /* We already know that these two types are compatible, but they
5147          may not be exactly identical.  In fact, `TREE_TYPE (type)' is
5148          likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5149          likely to be va_list, a typedef to __builtin_va_list, which
5150          is different enough that it will cause problems later.  */
5151       if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5152         {
5153           rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5154           SET_EXPR_LOCATION (rhs, location);
5155         }
5156
5157       rhs = build1 (NOP_EXPR, type, rhs);
5158       SET_EXPR_LOCATION (rhs, location);
5159       return rhs;
5160     }
5161   /* Some types can interconvert without explicit casts.  */
5162   else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5163            && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5164     return convert (type, rhs);
5165   /* Arithmetic types all interconvert, and enum is treated like int.  */
5166   else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5167             || codel == FIXED_POINT_TYPE
5168             || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5169             || codel == BOOLEAN_TYPE)
5170            && (coder == INTEGER_TYPE || coder == REAL_TYPE
5171                || coder == FIXED_POINT_TYPE
5172                || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5173                || coder == BOOLEAN_TYPE))
5174     {
5175       tree ret;
5176       bool save = in_late_binary_op;
5177       if (codel == BOOLEAN_TYPE)
5178         in_late_binary_op = true;
5179       ret = convert_and_check (type, orig_rhs);
5180       if (codel == BOOLEAN_TYPE)
5181         in_late_binary_op = save;
5182       return ret;
5183     }
5184
5185   /* Aggregates in different TUs might need conversion.  */
5186   if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5187       && codel == coder
5188       && comptypes (type, rhstype))
5189     return convert_and_check (type, rhs);
5190
5191   /* Conversion to a transparent union or record from its member types.
5192      This applies only to function arguments.  */
5193   if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5194       && TYPE_TRANSPARENT_AGGR (type))
5195       && errtype == ic_argpass)
5196     {
5197       tree memb, marginal_memb = NULL_TREE;
5198
5199       for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5200         {
5201           tree memb_type = TREE_TYPE (memb);
5202
5203           if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5204                          TYPE_MAIN_VARIANT (rhstype)))
5205             break;
5206
5207           if (TREE_CODE (memb_type) != POINTER_TYPE)
5208             continue;
5209
5210           if (coder == POINTER_TYPE)
5211             {
5212               tree ttl = TREE_TYPE (memb_type);
5213               tree ttr = TREE_TYPE (rhstype);
5214
5215               /* Any non-function converts to a [const][volatile] void *
5216                  and vice versa; otherwise, targets must be the same.
5217                  Meanwhile, the lhs target must have all the qualifiers of
5218                  the rhs.  */
5219               if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5220                   || comp_target_types (location, memb_type, rhstype))
5221                 {
5222                   /* If this type won't generate any warnings, use it.  */
5223                   if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5224                       || ((TREE_CODE (ttr) == FUNCTION_TYPE
5225                            && TREE_CODE (ttl) == FUNCTION_TYPE)
5226                           ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5227                              == TYPE_QUALS (ttr))
5228                           : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5229                              == TYPE_QUALS (ttl))))
5230                     break;
5231
5232                   /* Keep looking for a better type, but remember this one.  */
5233                   if (!marginal_memb)
5234                     marginal_memb = memb;
5235                 }
5236             }
5237
5238           /* Can convert integer zero to any pointer type.  */
5239           if (null_pointer_constant)
5240             {
5241               rhs = null_pointer_node;
5242               break;
5243             }
5244         }
5245
5246       if (memb || marginal_memb)
5247         {
5248           if (!memb)
5249             {
5250               /* We have only a marginally acceptable member type;
5251                  it needs a warning.  */
5252               tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5253               tree ttr = TREE_TYPE (rhstype);
5254
5255               /* Const and volatile mean something different for function
5256                  types, so the usual warnings are not appropriate.  */
5257               if (TREE_CODE (ttr) == FUNCTION_TYPE
5258                   && TREE_CODE (ttl) == FUNCTION_TYPE)
5259                 {
5260                   /* Because const and volatile on functions are
5261                      restrictions that say the function will not do
5262                      certain things, it is okay to use a const or volatile
5263                      function where an ordinary one is wanted, but not
5264                      vice-versa.  */
5265                   if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5266                       & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5267                     WARN_FOR_QUALIFIERS (location, 0,
5268                                          G_("passing argument %d of %qE "
5269                                             "makes %q#v qualified function "
5270                                             "pointer from unqualified"),
5271                                          G_("assignment makes %q#v qualified "
5272                                             "function pointer from "
5273                                             "unqualified"),
5274                                          G_("initialization makes %q#v qualified "
5275                                             "function pointer from "
5276                                             "unqualified"),
5277                                          G_("return makes %q#v qualified function "
5278                                             "pointer from unqualified"),
5279                                          TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5280                 }
5281               else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5282                        & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5283                 WARN_FOR_QUALIFIERS (location, 0,
5284                                      G_("passing argument %d of %qE discards "
5285                                         "%qv qualifier from pointer target type"),
5286                                      G_("assignment discards %qv qualifier "
5287                                         "from pointer target type"),
5288                                      G_("initialization discards %qv qualifier "
5289                                         "from pointer target type"),
5290                                      G_("return discards %qv qualifier from "
5291                                         "pointer target type"),
5292                                      TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5293
5294               memb = marginal_memb;
5295             }
5296
5297           if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5298             pedwarn (location, OPT_pedantic,
5299                      "ISO C prohibits argument conversion to union type");
5300
5301           rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5302           return build_constructor_single (type, memb, rhs);
5303         }
5304     }
5305
5306   /* Conversions among pointers */
5307   else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5308            && (coder == codel))
5309     {
5310       tree ttl = TREE_TYPE (type);
5311       tree ttr = TREE_TYPE (rhstype);
5312       tree mvl = ttl;
5313       tree mvr = ttr;
5314       bool is_opaque_pointer;
5315       int target_cmp = 0;   /* Cache comp_target_types () result.  */
5316       addr_space_t asl;
5317       addr_space_t asr;
5318
5319       if (TREE_CODE (mvl) != ARRAY_TYPE)
5320         mvl = TYPE_MAIN_VARIANT (mvl);
5321       if (TREE_CODE (mvr) != ARRAY_TYPE)
5322         mvr = TYPE_MAIN_VARIANT (mvr);
5323       /* Opaque pointers are treated like void pointers.  */
5324       is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5325
5326       /* C++ does not allow the implicit conversion void* -> T*.  However,
5327          for the purpose of reducing the number of false positives, we
5328          tolerate the special case of
5329
5330                 int *p = NULL;
5331
5332          where NULL is typically defined in C to be '(void *) 0'.  */
5333       if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5334         warning_at (location, OPT_Wc___compat,
5335                     "request for implicit conversion "
5336                     "from %qT to %qT not permitted in C++", rhstype, type);
5337
5338       /* See if the pointers point to incompatible address spaces.  */
5339       asl = TYPE_ADDR_SPACE (ttl);
5340       asr = TYPE_ADDR_SPACE (ttr);
5341       if (!null_pointer_constant_p (rhs)
5342           && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5343         {
5344           switch (errtype)
5345             {
5346             case ic_argpass:
5347               error_at (location, "passing argument %d of %qE from pointer to "
5348                         "non-enclosed address space", parmnum, rname);
5349               break;
5350             case ic_assign:
5351               error_at (location, "assignment from pointer to "
5352                         "non-enclosed address space");
5353               break;
5354             case ic_init:
5355               error_at (location, "initialization from pointer to "
5356                         "non-enclosed address space");
5357               break;
5358             case ic_return:
5359               error_at (location, "return from pointer to "
5360                         "non-enclosed address space");
5361               break;
5362             default:
5363               gcc_unreachable ();
5364             }
5365           return error_mark_node;
5366         }
5367
5368       /* Check if the right-hand side has a format attribute but the
5369          left-hand side doesn't.  */
5370       if (warn_missing_format_attribute
5371           && check_missing_format_attribute (type, rhstype))
5372         {
5373           switch (errtype)
5374           {
5375           case ic_argpass:
5376             warning_at (location, OPT_Wmissing_format_attribute,
5377                         "argument %d of %qE might be "
5378                         "a candidate for a format attribute",
5379                         parmnum, rname);
5380             break;
5381           case ic_assign:
5382             warning_at (location, OPT_Wmissing_format_attribute,
5383                         "assignment left-hand side might be "
5384                         "a candidate for a format attribute");
5385             break;
5386           case ic_init:
5387             warning_at (location, OPT_Wmissing_format_attribute,
5388                         "initialization left-hand side might be "
5389                         "a candidate for a format attribute");
5390             break;
5391           case ic_return:
5392             warning_at (location, OPT_Wmissing_format_attribute,
5393                         "return type might be "
5394                         "a candidate for a format attribute");
5395             break;
5396           default:
5397             gcc_unreachable ();
5398           }
5399         }
5400
5401       /* Any non-function converts to a [const][volatile] void *
5402          and vice versa; otherwise, targets must be the same.
5403          Meanwhile, the lhs target must have all the qualifiers of the rhs.  */
5404       if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5405           || (target_cmp = comp_target_types (location, type, rhstype))
5406           || is_opaque_pointer
5407           || (c_common_unsigned_type (mvl)
5408               == c_common_unsigned_type (mvr)))
5409         {
5410           if (pedantic
5411               && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5412                   ||
5413                   (VOID_TYPE_P (ttr)
5414                    && !null_pointer_constant
5415                    && TREE_CODE (ttl) == FUNCTION_TYPE)))
5416             WARN_FOR_ASSIGNMENT (location, OPT_pedantic,
5417                                  G_("ISO C forbids passing argument %d of "
5418                                     "%qE between function pointer "
5419                                     "and %<void *%>"),
5420                                  G_("ISO C forbids assignment between "
5421                                     "function pointer and %<void *%>"),
5422                                  G_("ISO C forbids initialization between "
5423                                     "function pointer and %<void *%>"),
5424                                  G_("ISO C forbids return between function "
5425                                     "pointer and %<void *%>"));
5426           /* Const and volatile mean something different for function types,
5427              so the usual warnings are not appropriate.  */
5428           else if (TREE_CODE (ttr) != FUNCTION_TYPE
5429                    && TREE_CODE (ttl) != FUNCTION_TYPE)
5430             {
5431               if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5432                   & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5433                 {
5434                   /* Types differing only by the presence of the 'volatile'
5435                      qualifier are acceptable if the 'volatile' has been added
5436                      in by the Objective-C EH machinery.  */
5437                   if (!objc_type_quals_match (ttl, ttr))
5438                     WARN_FOR_QUALIFIERS (location, 0,
5439                                          G_("passing argument %d of %qE discards "
5440                                             "%qv qualifier from pointer target type"),
5441                                          G_("assignment discards %qv qualifier "
5442                                             "from pointer target type"),
5443                                          G_("initialization discards %qv qualifier "
5444                                             "from pointer target type"),
5445                                          G_("return discards %qv qualifier from "
5446                                             "pointer target type"),
5447                                          TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5448                 }
5449               /* If this is not a case of ignoring a mismatch in signedness,
5450                  no warning.  */
5451               else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5452                        || target_cmp)
5453                 ;
5454               /* If there is a mismatch, do warn.  */
5455               else if (warn_pointer_sign)
5456                 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5457                                      G_("pointer targets in passing argument "
5458                                         "%d of %qE differ in signedness"),
5459                                      G_("pointer targets in assignment "
5460                                         "differ in signedness"),
5461                                      G_("pointer targets in initialization "
5462                                         "differ in signedness"),
5463                                      G_("pointer targets in return differ "
5464                                         "in signedness"));
5465             }
5466           else if (TREE_CODE (ttl) == FUNCTION_TYPE
5467                    && TREE_CODE (ttr) == FUNCTION_TYPE)
5468             {
5469               /* Because const and volatile on functions are restrictions
5470                  that say the function will not do certain things,
5471                  it is okay to use a const or volatile function
5472                  where an ordinary one is wanted, but not vice-versa.  */
5473               if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5474                   & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5475                 WARN_FOR_QUALIFIERS (location, 0,
5476                                      G_("passing argument %d of %qE makes "
5477                                         "%q#v qualified function pointer "
5478                                         "from unqualified"),
5479                                      G_("assignment makes %q#v qualified function "
5480                                         "pointer from unqualified"),
5481                                      G_("initialization makes %q#v qualified "
5482                                         "function pointer from unqualified"),
5483                                      G_("return makes %q#v qualified function "
5484                                         "pointer from unqualified"),
5485                                      TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5486             }
5487         }
5488       else
5489         /* Avoid warning about the volatile ObjC EH puts on decls.  */
5490         if (!objc_ok)
5491           WARN_FOR_ASSIGNMENT (location, 0,
5492                                G_("passing argument %d of %qE from "
5493                                   "incompatible pointer type"),
5494                                G_("assignment from incompatible pointer type"),
5495                                G_("initialization from incompatible "
5496                                   "pointer type"),
5497                                G_("return from incompatible pointer type"));
5498
5499       return convert (type, rhs);
5500     }
5501   else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5502     {
5503       /* ??? This should not be an error when inlining calls to
5504          unprototyped functions.  */
5505       error_at (location, "invalid use of non-lvalue array");
5506       return error_mark_node;
5507     }
5508   else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5509     {
5510       /* An explicit constant 0 can convert to a pointer,
5511          or one that results from arithmetic, even including
5512          a cast to integer type.  */
5513       if (!null_pointer_constant)
5514         WARN_FOR_ASSIGNMENT (location, 0,
5515                              G_("passing argument %d of %qE makes "
5516                                 "pointer from integer without a cast"),
5517                              G_("assignment makes pointer from integer "
5518                                 "without a cast"),
5519                              G_("initialization makes pointer from "
5520                                 "integer without a cast"),
5521                              G_("return makes pointer from integer "
5522                                 "without a cast"));
5523
5524       return convert (type, rhs);
5525     }
5526   else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5527     {
5528       WARN_FOR_ASSIGNMENT (location, 0,
5529                            G_("passing argument %d of %qE makes integer "
5530                               "from pointer without a cast"),
5531                            G_("assignment makes integer from pointer "
5532                               "without a cast"),
5533                            G_("initialization makes integer from pointer "
5534                               "without a cast"),
5535                            G_("return makes integer from pointer "
5536                               "without a cast"));
5537       return convert (type, rhs);
5538     }
5539   else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5540     {
5541       tree ret;
5542       bool save = in_late_binary_op;
5543       in_late_binary_op = true;
5544       ret = convert (type, rhs);
5545       in_late_binary_op = save;
5546       return ret;
5547     }
5548
5549   switch (errtype)
5550     {
5551     case ic_argpass:
5552       error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5553       inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5554               ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5555               "expected %qT but argument is of type %qT", type, rhstype);
5556       break;
5557     case ic_assign:
5558       error_at (location, "incompatible types when assigning to type %qT from "
5559                 "type %qT", type, rhstype);
5560       break;
5561     case ic_init:
5562       error_at (location,
5563                 "incompatible types when initializing type %qT using type %qT",
5564                 type, rhstype);
5565       break;
5566     case ic_return:
5567       error_at (location,
5568                 "incompatible types when returning type %qT but %qT was "
5569                 "expected", rhstype, type);
5570       break;
5571     default:
5572       gcc_unreachable ();
5573     }
5574
5575   return error_mark_node;
5576 }
5577 \f
5578 /* If VALUE is a compound expr all of whose expressions are constant, then
5579    return its value.  Otherwise, return error_mark_node.
5580
5581    This is for handling COMPOUND_EXPRs as initializer elements
5582    which is allowed with a warning when -pedantic is specified.  */
5583
5584 static tree
5585 valid_compound_expr_initializer (tree value, tree endtype)
5586 {
5587   if (TREE_CODE (value) == COMPOUND_EXPR)
5588     {
5589       if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5590           == error_mark_node)
5591         return error_mark_node;
5592       return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5593                                               endtype);
5594     }
5595   else if (!initializer_constant_valid_p (value, endtype))
5596     return error_mark_node;
5597   else
5598     return value;
5599 }
5600 \f
5601 /* Perform appropriate conversions on the initial value of a variable,
5602    store it in the declaration DECL,
5603    and print any error messages that are appropriate.
5604    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5605    If the init is invalid, store an ERROR_MARK.
5606
5607    INIT_LOC is the location of the initial value.  */
5608
5609 void
5610 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5611 {
5612   tree value, type;
5613   bool npc = false;
5614
5615   /* If variable's type was invalidly declared, just ignore it.  */
5616
5617   type = TREE_TYPE (decl);
5618   if (TREE_CODE (type) == ERROR_MARK)
5619     return;
5620
5621   /* Digest the specified initializer into an expression.  */
5622
5623   if (init)
5624     npc = null_pointer_constant_p (init);
5625   value = digest_init (init_loc, type, init, origtype, npc,
5626                        true, TREE_STATIC (decl));
5627
5628   /* Store the expression if valid; else report error.  */
5629
5630   if (!in_system_header
5631       && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5632     warning (OPT_Wtraditional, "traditional C rejects automatic "
5633              "aggregate initialization");
5634
5635   DECL_INITIAL (decl) = value;
5636
5637   /* ANSI wants warnings about out-of-range constant initializers.  */
5638   STRIP_TYPE_NOPS (value);
5639   if (TREE_STATIC (decl))
5640     constant_expression_warning (value);
5641
5642   /* Check if we need to set array size from compound literal size.  */
5643   if (TREE_CODE (type) == ARRAY_TYPE
5644       && TYPE_DOMAIN (type) == 0
5645       && value != error_mark_node)
5646     {
5647       tree inside_init = init;
5648
5649       STRIP_TYPE_NOPS (inside_init);
5650       inside_init = fold (inside_init);
5651
5652       if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5653         {
5654           tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5655
5656           if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5657             {
5658               /* For int foo[] = (int [3]){1}; we need to set array size
5659                  now since later on array initializer will be just the
5660                  brace enclosed list of the compound literal.  */
5661               type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5662               TREE_TYPE (decl) = type;
5663               TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5664               layout_type (type);
5665               layout_decl (cldecl, 0);
5666             }
5667         }
5668     }
5669 }
5670 \f
5671 /* Methods for storing and printing names for error messages.  */
5672
5673 /* Implement a spelling stack that allows components of a name to be pushed
5674    and popped.  Each element on the stack is this structure.  */
5675
5676 struct spelling
5677 {
5678   int kind;
5679   union
5680     {
5681       unsigned HOST_WIDE_INT i;
5682       const char *s;
5683     } u;
5684 };
5685
5686 #define SPELLING_STRING 1
5687 #define SPELLING_MEMBER 2
5688 #define SPELLING_BOUNDS 3
5689
5690 static struct spelling *spelling;       /* Next stack element (unused).  */
5691 static struct spelling *spelling_base;  /* Spelling stack base.  */
5692 static int spelling_size;               /* Size of the spelling stack.  */
5693
5694 /* Macros to save and restore the spelling stack around push_... functions.
5695    Alternative to SAVE_SPELLING_STACK.  */
5696
5697 #define SPELLING_DEPTH() (spelling - spelling_base)
5698 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5699
5700 /* Push an element on the spelling stack with type KIND and assign VALUE
5701    to MEMBER.  */
5702
5703 #define PUSH_SPELLING(KIND, VALUE, MEMBER)                              \
5704 {                                                                       \
5705   int depth = SPELLING_DEPTH ();                                        \
5706                                                                         \
5707   if (depth >= spelling_size)                                           \
5708     {                                                                   \
5709       spelling_size += 10;                                              \
5710       spelling_base = XRESIZEVEC (struct spelling, spelling_base,       \
5711                                   spelling_size);                       \
5712       RESTORE_SPELLING_DEPTH (depth);                                   \
5713     }                                                                   \
5714                                                                         \
5715   spelling->kind = (KIND);                                              \
5716   spelling->MEMBER = (VALUE);                                           \
5717   spelling++;                                                           \
5718 }
5719
5720 /* Push STRING on the stack.  Printed literally.  */
5721
5722 static void
5723 push_string (const char *string)
5724 {
5725   PUSH_SPELLING (SPELLING_STRING, string, u.s);
5726 }
5727
5728 /* Push a member name on the stack.  Printed as '.' STRING.  */
5729
5730 static void
5731 push_member_name (tree decl)
5732 {
5733   const char *const string
5734     = (DECL_NAME (decl)
5735        ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5736        : _("<anonymous>"));
5737   PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5738 }
5739
5740 /* Push an array bounds on the stack.  Printed as [BOUNDS].  */
5741
5742 static void
5743 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5744 {
5745   PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5746 }
5747
5748 /* Compute the maximum size in bytes of the printed spelling.  */
5749
5750 static int
5751 spelling_length (void)
5752 {
5753   int size = 0;
5754   struct spelling *p;
5755
5756   for (p = spelling_base; p < spelling; p++)
5757     {
5758       if (p->kind == SPELLING_BOUNDS)
5759         size += 25;
5760       else
5761         size += strlen (p->u.s) + 1;
5762     }
5763
5764   return size;
5765 }
5766
5767 /* Print the spelling to BUFFER and return it.  */
5768
5769 static char *
5770 print_spelling (char *buffer)
5771 {
5772   char *d = buffer;
5773   struct spelling *p;
5774
5775   for (p = spelling_base; p < spelling; p++)
5776     if (p->kind == SPELLING_BOUNDS)
5777       {
5778         sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5779         d += strlen (d);
5780       }
5781     else
5782       {
5783         const char *s;
5784         if (p->kind == SPELLING_MEMBER)
5785           *d++ = '.';
5786         for (s = p->u.s; (*d = *s++); d++)
5787           ;
5788       }
5789   *d++ = '\0';
5790   return buffer;
5791 }
5792
5793 /* Issue an error message for a bad initializer component.
5794    GMSGID identifies the message.
5795    The component name is taken from the spelling stack.  */
5796
5797 void
5798 error_init (const char *gmsgid)
5799 {
5800   char *ofwhat;
5801
5802   /* The gmsgid may be a format string with %< and %>. */
5803   error (gmsgid);
5804   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5805   if (*ofwhat)
5806     error ("(near initialization for %qs)", ofwhat);
5807 }
5808
5809 /* Issue a pedantic warning for a bad initializer component.  OPT is
5810    the option OPT_* (from options.h) controlling this warning or 0 if
5811    it is unconditionally given.  GMSGID identifies the message.  The
5812    component name is taken from the spelling stack.  */
5813
5814 void
5815 pedwarn_init (location_t location, int opt, const char *gmsgid)
5816 {
5817   char *ofwhat;
5818   
5819   /* The gmsgid may be a format string with %< and %>. */
5820   pedwarn (location, opt, gmsgid);
5821   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5822   if (*ofwhat)
5823     pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5824 }
5825
5826 /* Issue a warning for a bad initializer component.
5827
5828    OPT is the OPT_W* value corresponding to the warning option that
5829    controls this warning.  GMSGID identifies the message.  The
5830    component name is taken from the spelling stack.  */
5831
5832 static void
5833 warning_init (int opt, const char *gmsgid)
5834 {
5835   char *ofwhat;
5836
5837   /* The gmsgid may be a format string with %< and %>. */
5838   warning (opt, gmsgid);
5839   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5840   if (*ofwhat)
5841     warning (opt, "(near initialization for %qs)", ofwhat);
5842 }
5843 \f
5844 /* If TYPE is an array type and EXPR is a parenthesized string
5845    constant, warn if pedantic that EXPR is being used to initialize an
5846    object of type TYPE.  */
5847
5848 void
5849 maybe_warn_string_init (tree type, struct c_expr expr)
5850 {
5851   if (pedantic
5852       && TREE_CODE (type) == ARRAY_TYPE
5853       && TREE_CODE (expr.value) == STRING_CST
5854       && expr.original_code != STRING_CST)
5855     pedwarn_init (input_location, OPT_pedantic,
5856                   "array initialized from parenthesized string constant");
5857 }
5858
5859 /* Digest the parser output INIT as an initializer for type TYPE.
5860    Return a C expression of type TYPE to represent the initial value.
5861
5862    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5863
5864    NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
5865
5866    If INIT is a string constant, STRICT_STRING is true if it is
5867    unparenthesized or we should not warn here for it being parenthesized.
5868    For other types of INIT, STRICT_STRING is not used.
5869
5870    INIT_LOC is the location of the INIT.
5871
5872    REQUIRE_CONSTANT requests an error if non-constant initializers or
5873    elements are seen.  */
5874
5875 static tree
5876 digest_init (location_t init_loc, tree type, tree init, tree origtype,
5877              bool null_pointer_constant, bool strict_string,
5878              int require_constant)
5879 {
5880   enum tree_code code = TREE_CODE (type);
5881   tree inside_init = init;
5882   tree semantic_type = NULL_TREE;
5883   bool maybe_const = true;
5884
5885   if (type == error_mark_node
5886       || !init
5887       || init == error_mark_node
5888       || TREE_TYPE (init) == error_mark_node)
5889     return error_mark_node;
5890
5891   STRIP_TYPE_NOPS (inside_init);
5892
5893   if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
5894     {
5895       semantic_type = TREE_TYPE (inside_init);
5896       inside_init = TREE_OPERAND (inside_init, 0);
5897     }
5898   inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
5899   inside_init = decl_constant_value_for_optimization (inside_init);
5900
5901   /* Initialization of an array of chars from a string constant
5902      optionally enclosed in braces.  */
5903
5904   if (code == ARRAY_TYPE && inside_init
5905       && TREE_CODE (inside_init) == STRING_CST)
5906     {
5907       tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5908       /* Note that an array could be both an array of character type
5909          and an array of wchar_t if wchar_t is signed char or unsigned
5910          char.  */
5911       bool char_array = (typ1 == char_type_node
5912                          || typ1 == signed_char_type_node
5913                          || typ1 == unsigned_char_type_node);
5914       bool wchar_array = !!comptypes (typ1, wchar_type_node);
5915       bool char16_array = !!comptypes (typ1, char16_type_node);
5916       bool char32_array = !!comptypes (typ1, char32_type_node);
5917
5918       if (char_array || wchar_array || char16_array || char32_array)
5919         {
5920           struct c_expr expr;
5921           tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
5922           expr.value = inside_init;
5923           expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
5924           expr.original_type = NULL;
5925           maybe_warn_string_init (type, expr);
5926
5927           if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5928             pedwarn_init (init_loc, OPT_pedantic,
5929                           "initialization of a flexible array member");
5930
5931           if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
5932                          TYPE_MAIN_VARIANT (type)))
5933             return inside_init;
5934
5935           if (char_array)
5936             {
5937               if (typ2 != char_type_node)
5938                 {
5939                   error_init ("char-array initialized from wide string");
5940                   return error_mark_node;
5941                 }
5942             }
5943           else
5944             {
5945               if (typ2 == char_type_node)
5946                 {
5947                   error_init ("wide character array initialized from non-wide "
5948                               "string");
5949                   return error_mark_node;
5950                 }
5951               else if (!comptypes(typ1, typ2))
5952                 {
5953                   error_init ("wide character array initialized from "
5954                               "incompatible wide string");
5955                   return error_mark_node;
5956                 }
5957             }
5958
5959           TREE_TYPE (inside_init) = type;
5960           if (TYPE_DOMAIN (type) != 0
5961               && TYPE_SIZE (type) != 0
5962               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
5963             {
5964               unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
5965
5966               /* Subtract the size of a single (possibly wide) character
5967                  because it's ok to ignore the terminating null char
5968                  that is counted in the length of the constant.  */
5969               if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
5970                                         (len
5971                                          - (TYPE_PRECISION (typ1)
5972                                             / BITS_PER_UNIT))))
5973                 pedwarn_init (init_loc, 0,
5974                               ("initializer-string for array of chars "
5975                                "is too long"));
5976               else if (warn_cxx_compat
5977                        && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
5978                 warning_at (init_loc, OPT_Wc___compat,
5979                             ("initializer-string for array chars "
5980                              "is too long for C++"));
5981             }
5982
5983           return inside_init;
5984         }
5985       else if (INTEGRAL_TYPE_P (typ1))
5986         {
5987           error_init ("array of inappropriate type initialized "
5988                       "from string constant");
5989           return error_mark_node;
5990         }
5991     }
5992
5993   /* Build a VECTOR_CST from a *constant* vector constructor.  If the
5994      vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
5995      below and handle as a constructor.  */
5996   if (code == VECTOR_TYPE
5997       && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
5998       && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
5999       && TREE_CONSTANT (inside_init))
6000     {
6001       if (TREE_CODE (inside_init) == VECTOR_CST
6002           && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6003                         TYPE_MAIN_VARIANT (type)))
6004         return inside_init;
6005
6006       if (TREE_CODE (inside_init) == CONSTRUCTOR)
6007         {
6008           unsigned HOST_WIDE_INT ix;
6009           tree value;
6010           bool constant_p = true;
6011
6012           /* Iterate through elements and check if all constructor
6013              elements are *_CSTs.  */
6014           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6015             if (!CONSTANT_CLASS_P (value))
6016               {
6017                 constant_p = false;
6018                 break;
6019               }
6020
6021           if (constant_p)
6022             return build_vector_from_ctor (type,
6023                                            CONSTRUCTOR_ELTS (inside_init));
6024         }
6025     }
6026
6027   if (warn_sequence_point)
6028     verify_sequence_points (inside_init);
6029
6030   /* Any type can be initialized
6031      from an expression of the same type, optionally with braces.  */
6032
6033   if (inside_init && TREE_TYPE (inside_init) != 0
6034       && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6035                      TYPE_MAIN_VARIANT (type))
6036           || (code == ARRAY_TYPE
6037               && comptypes (TREE_TYPE (inside_init), type))
6038           || (code == VECTOR_TYPE
6039               && comptypes (TREE_TYPE (inside_init), type))
6040           || (code == POINTER_TYPE
6041               && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6042               && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6043                             TREE_TYPE (type)))))
6044     {
6045       if (code == POINTER_TYPE)
6046         {
6047           if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6048             {
6049               if (TREE_CODE (inside_init) == STRING_CST
6050                   || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6051                 inside_init = array_to_pointer_conversion
6052                   (init_loc, inside_init);
6053               else
6054                 {
6055                   error_init ("invalid use of non-lvalue array");
6056                   return error_mark_node;
6057                 }
6058             }
6059         }
6060
6061       if (code == VECTOR_TYPE)
6062         /* Although the types are compatible, we may require a
6063            conversion.  */
6064         inside_init = convert (type, inside_init);
6065
6066       if (require_constant
6067           && (code == VECTOR_TYPE || !flag_isoc99)
6068           && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6069         {
6070           /* As an extension, allow initializing objects with static storage
6071              duration with compound literals (which are then treated just as
6072              the brace enclosed list they contain).  Also allow this for
6073              vectors, as we can only assign them with compound literals.  */
6074           tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6075           inside_init = DECL_INITIAL (decl);
6076         }
6077
6078       if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6079           && TREE_CODE (inside_init) != CONSTRUCTOR)
6080         {
6081           error_init ("array initialized from non-constant array expression");
6082           return error_mark_node;
6083         }
6084
6085       /* Compound expressions can only occur here if -pedantic or
6086          -pedantic-errors is specified.  In the later case, we always want
6087          an error.  In the former case, we simply want a warning.  */
6088       if (require_constant && pedantic
6089           && TREE_CODE (inside_init) == COMPOUND_EXPR)
6090         {
6091           inside_init
6092             = valid_compound_expr_initializer (inside_init,
6093                                                TREE_TYPE (inside_init));
6094           if (inside_init == error_mark_node)
6095             error_init ("initializer element is not constant");
6096           else
6097             pedwarn_init (init_loc, OPT_pedantic,
6098                           "initializer element is not constant");
6099           if (flag_pedantic_errors)
6100             inside_init = error_mark_node;
6101         }
6102       else if (require_constant
6103                && !initializer_constant_valid_p (inside_init,
6104                                                  TREE_TYPE (inside_init)))
6105         {
6106           error_init ("initializer element is not constant");
6107           inside_init = error_mark_node;
6108         }
6109       else if (require_constant && !maybe_const)
6110         pedwarn_init (init_loc, 0,
6111                       "initializer element is not a constant expression");
6112
6113       /* Added to enable additional -Wmissing-format-attribute warnings.  */
6114       if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6115         inside_init = convert_for_assignment (init_loc, type, inside_init,
6116                                               origtype,
6117                                               ic_init, null_pointer_constant,
6118                                               NULL_TREE, NULL_TREE, 0);
6119       return inside_init;
6120     }
6121
6122   /* Handle scalar types, including conversions.  */
6123
6124   if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6125       || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6126       || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6127     {
6128       if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6129           && (TREE_CODE (init) == STRING_CST
6130               || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6131         inside_init = init = array_to_pointer_conversion (init_loc, init);
6132       if (semantic_type)
6133         inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6134                               inside_init);
6135       inside_init
6136         = convert_for_assignment (init_loc, type, inside_init, origtype,
6137                                   ic_init, null_pointer_constant,
6138                                   NULL_TREE, NULL_TREE, 0);
6139
6140       /* Check to see if we have already given an error message.  */
6141       if (inside_init == error_mark_node)
6142         ;
6143       else if (require_constant && !TREE_CONSTANT (inside_init))
6144         {
6145           error_init ("initializer element is not constant");
6146           inside_init = error_mark_node;
6147         }
6148       else if (require_constant
6149                && !initializer_constant_valid_p (inside_init,
6150                                                  TREE_TYPE (inside_init)))
6151         {
6152           error_init ("initializer element is not computable at load time");
6153           inside_init = error_mark_node;
6154         }
6155       else if (require_constant && !maybe_const)
6156         pedwarn_init (init_loc, 0,
6157                       "initializer element is not a constant expression");
6158
6159       return inside_init;
6160     }
6161
6162   /* Come here only for records and arrays.  */
6163
6164   if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6165     {
6166       error_init ("variable-sized object may not be initialized");
6167       return error_mark_node;
6168     }
6169
6170   error_init ("invalid initializer");
6171   return error_mark_node;
6172 }
6173 \f
6174 /* Handle initializers that use braces.  */
6175
6176 /* Type of object we are accumulating a constructor for.
6177    This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE.  */
6178 static tree constructor_type;
6179
6180 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6181    left to fill.  */
6182 static tree constructor_fields;
6183
6184 /* For an ARRAY_TYPE, this is the specified index
6185    at which to store the next element we get.  */
6186 static tree constructor_index;
6187
6188 /* For an ARRAY_TYPE, this is the maximum index.  */
6189 static tree constructor_max_index;
6190
6191 /* For a RECORD_TYPE, this is the first field not yet written out.  */
6192 static tree constructor_unfilled_fields;
6193
6194 /* For an ARRAY_TYPE, this is the index of the first element
6195    not yet written out.  */
6196 static tree constructor_unfilled_index;
6197
6198 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6199    This is so we can generate gaps between fields, when appropriate.  */
6200 static tree constructor_bit_index;
6201
6202 /* If we are saving up the elements rather than allocating them,
6203    this is the list of elements so far (in reverse order,
6204    most recent first).  */
6205 static VEC(constructor_elt,gc) *constructor_elements;
6206
6207 /* 1 if constructor should be incrementally stored into a constructor chain,
6208    0 if all the elements should be kept in AVL tree.  */
6209 static int constructor_incremental;
6210
6211 /* 1 if so far this constructor's elements are all compile-time constants.  */
6212 static int constructor_constant;
6213
6214 /* 1 if so far this constructor's elements are all valid address constants.  */
6215 static int constructor_simple;
6216
6217 /* 1 if this constructor has an element that cannot be part of a
6218    constant expression.  */
6219 static int constructor_nonconst;
6220
6221 /* 1 if this constructor is erroneous so far.  */
6222 static int constructor_erroneous;
6223
6224 /* Structure for managing pending initializer elements, organized as an
6225    AVL tree.  */
6226
6227 struct init_node
6228 {
6229   struct init_node *left, *right;
6230   struct init_node *parent;
6231   int balance;
6232   tree purpose;
6233   tree value;
6234   tree origtype;
6235 };
6236
6237 /* Tree of pending elements at this constructor level.
6238    These are elements encountered out of order
6239    which belong at places we haven't reached yet in actually
6240    writing the output.
6241    Will never hold tree nodes across GC runs.  */
6242 static struct init_node *constructor_pending_elts;
6243
6244 /* The SPELLING_DEPTH of this constructor.  */
6245 static int constructor_depth;
6246
6247 /* DECL node for which an initializer is being read.
6248    0 means we are reading a constructor expression
6249    such as (struct foo) {...}.  */
6250 static tree constructor_decl;
6251
6252 /* Nonzero if this is an initializer for a top-level decl.  */
6253 static int constructor_top_level;
6254
6255 /* Nonzero if there were any member designators in this initializer.  */
6256 static int constructor_designated;
6257
6258 /* Nesting depth of designator list.  */
6259 static int designator_depth;
6260
6261 /* Nonzero if there were diagnosed errors in this designator list.  */
6262 static int designator_erroneous;
6263
6264 \f
6265 /* This stack has a level for each implicit or explicit level of
6266    structuring in the initializer, including the outermost one.  It
6267    saves the values of most of the variables above.  */
6268
6269 struct constructor_range_stack;
6270
6271 struct constructor_stack
6272 {
6273   struct constructor_stack *next;
6274   tree type;
6275   tree fields;
6276   tree index;
6277   tree max_index;
6278   tree unfilled_index;
6279   tree unfilled_fields;
6280   tree bit_index;
6281   VEC(constructor_elt,gc) *elements;
6282   struct init_node *pending_elts;
6283   int offset;
6284   int depth;
6285   /* If value nonzero, this value should replace the entire
6286      constructor at this level.  */
6287   struct c_expr replacement_value;
6288   struct constructor_range_stack *range_stack;
6289   char constant;
6290   char simple;
6291   char nonconst;
6292   char implicit;
6293   char erroneous;
6294   char outer;
6295   char incremental;
6296   char designated;
6297 };
6298
6299 static struct constructor_stack *constructor_stack;
6300
6301 /* This stack represents designators from some range designator up to
6302    the last designator in the list.  */
6303
6304 struct constructor_range_stack
6305 {
6306   struct constructor_range_stack *next, *prev;
6307   struct constructor_stack *stack;
6308   tree range_start;
6309   tree index;
6310   tree range_end;
6311   tree fields;
6312 };
6313
6314 static struct constructor_range_stack *constructor_range_stack;
6315
6316 /* This stack records separate initializers that are nested.
6317    Nested initializers can't happen in ANSI C, but GNU C allows them
6318    in cases like { ... (struct foo) { ... } ... }.  */
6319
6320 struct initializer_stack
6321 {
6322   struct initializer_stack *next;
6323   tree decl;
6324   struct constructor_stack *constructor_stack;
6325   struct constructor_range_stack *constructor_range_stack;
6326   VEC(constructor_elt,gc) *elements;
6327   struct spelling *spelling;
6328   struct spelling *spelling_base;
6329   int spelling_size;
6330   char top_level;
6331   char require_constant_value;
6332   char require_constant_elements;
6333 };
6334
6335 static struct initializer_stack *initializer_stack;
6336 \f
6337 /* Prepare to parse and output the initializer for variable DECL.  */
6338
6339 void
6340 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6341 {
6342   const char *locus;
6343   struct initializer_stack *p = XNEW (struct initializer_stack);
6344
6345   p->decl = constructor_decl;
6346   p->require_constant_value = require_constant_value;
6347   p->require_constant_elements = require_constant_elements;
6348   p->constructor_stack = constructor_stack;
6349   p->constructor_range_stack = constructor_range_stack;
6350   p->elements = constructor_elements;
6351   p->spelling = spelling;
6352   p->spelling_base = spelling_base;
6353   p->spelling_size = spelling_size;
6354   p->top_level = constructor_top_level;
6355   p->next = initializer_stack;
6356   initializer_stack = p;
6357
6358   constructor_decl = decl;
6359   constructor_designated = 0;
6360   constructor_top_level = top_level;
6361
6362   if (decl != 0 && decl != error_mark_node)
6363     {
6364       require_constant_value = TREE_STATIC (decl);
6365       require_constant_elements
6366         = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6367            /* For a scalar, you can always use any value to initialize,
6368               even within braces.  */
6369            && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6370                || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6371                || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6372                || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6373       locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6374     }
6375   else
6376     {
6377       require_constant_value = 0;
6378       require_constant_elements = 0;
6379       locus = _("(anonymous)");
6380     }
6381
6382   constructor_stack = 0;
6383   constructor_range_stack = 0;
6384
6385   missing_braces_mentioned = 0;
6386
6387   spelling_base = 0;
6388   spelling_size = 0;
6389   RESTORE_SPELLING_DEPTH (0);
6390
6391   if (locus)
6392     push_string (locus);
6393 }
6394
6395 void
6396 finish_init (void)
6397 {
6398   struct initializer_stack *p = initializer_stack;
6399
6400   /* Free the whole constructor stack of this initializer.  */
6401   while (constructor_stack)
6402     {
6403       struct constructor_stack *q = constructor_stack;
6404       constructor_stack = q->next;
6405       free (q);
6406     }
6407
6408   gcc_assert (!constructor_range_stack);
6409
6410   /* Pop back to the data of the outer initializer (if any).  */
6411   free (spelling_base);
6412
6413   constructor_decl = p->decl;
6414   require_constant_value = p->require_constant_value;
6415   require_constant_elements = p->require_constant_elements;
6416   constructor_stack = p->constructor_stack;
6417   constructor_range_stack = p->constructor_range_stack;
6418   constructor_elements = p->elements;
6419   spelling = p->spelling;
6420   spelling_base = p->spelling_base;
6421   spelling_size = p->spelling_size;
6422   constructor_top_level = p->top_level;
6423   initializer_stack = p->next;
6424   free (p);
6425 }
6426 \f
6427 /* Call here when we see the initializer is surrounded by braces.
6428    This is instead of a call to push_init_level;
6429    it is matched by a call to pop_init_level.
6430
6431    TYPE is the type to initialize, for a constructor expression.
6432    For an initializer for a decl, TYPE is zero.  */
6433
6434 void
6435 really_start_incremental_init (tree type)
6436 {
6437   struct constructor_stack *p = XNEW (struct constructor_stack);
6438
6439   if (type == 0)
6440     type = TREE_TYPE (constructor_decl);
6441
6442   if (TREE_CODE (type) == VECTOR_TYPE
6443       && TYPE_VECTOR_OPAQUE (type))
6444     error ("opaque vector types cannot be initialized");
6445
6446   p->type = constructor_type;
6447   p->fields = constructor_fields;
6448   p->index = constructor_index;
6449   p->max_index = constructor_max_index;
6450   p->unfilled_index = constructor_unfilled_index;
6451   p->unfilled_fields = constructor_unfilled_fields;
6452   p->bit_index = constructor_bit_index;
6453   p->elements = constructor_elements;
6454   p->constant = constructor_constant;
6455   p->simple = constructor_simple;
6456   p->nonconst = constructor_nonconst;
6457   p->erroneous = constructor_erroneous;
6458   p->pending_elts = constructor_pending_elts;
6459   p->depth = constructor_depth;
6460   p->replacement_value.value = 0;
6461   p->replacement_value.original_code = ERROR_MARK;
6462   p->replacement_value.original_type = NULL;
6463   p->implicit = 0;
6464   p->range_stack = 0;
6465   p->outer = 0;
6466   p->incremental = constructor_incremental;
6467   p->designated = constructor_designated;
6468   p->next = 0;
6469   constructor_stack = p;
6470
6471   constructor_constant = 1;
6472   constructor_simple = 1;
6473   constructor_nonconst = 0;
6474   constructor_depth = SPELLING_DEPTH ();
6475   constructor_elements = 0;
6476   constructor_pending_elts = 0;
6477   constructor_type = type;
6478   constructor_incremental = 1;
6479   constructor_designated = 0;
6480   designator_depth = 0;
6481   designator_erroneous = 0;
6482
6483   if (TREE_CODE (constructor_type) == RECORD_TYPE
6484       || TREE_CODE (constructor_type) == UNION_TYPE)
6485     {
6486       constructor_fields = TYPE_FIELDS (constructor_type);
6487       /* Skip any nameless bit fields at the beginning.  */
6488       while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6489              && DECL_NAME (constructor_fields) == 0)
6490         constructor_fields = DECL_CHAIN (constructor_fields);
6491
6492       constructor_unfilled_fields = constructor_fields;
6493       constructor_bit_index = bitsize_zero_node;
6494     }
6495   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6496     {
6497       if (TYPE_DOMAIN (constructor_type))
6498         {
6499           constructor_max_index
6500             = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6501
6502           /* Detect non-empty initializations of zero-length arrays.  */
6503           if (constructor_max_index == NULL_TREE
6504               && TYPE_SIZE (constructor_type))
6505             constructor_max_index = integer_minus_one_node;
6506
6507           /* constructor_max_index needs to be an INTEGER_CST.  Attempts
6508              to initialize VLAs will cause a proper error; avoid tree
6509              checking errors as well by setting a safe value.  */
6510           if (constructor_max_index
6511               && TREE_CODE (constructor_max_index) != INTEGER_CST)
6512             constructor_max_index = integer_minus_one_node;
6513
6514           constructor_index
6515             = convert (bitsizetype,
6516                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6517         }
6518       else
6519         {
6520           constructor_index = bitsize_zero_node;
6521           constructor_max_index = NULL_TREE;
6522         }
6523
6524       constructor_unfilled_index = constructor_index;
6525     }
6526   else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6527     {
6528       /* Vectors are like simple fixed-size arrays.  */
6529       constructor_max_index =
6530         build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6531       constructor_index = bitsize_zero_node;
6532       constructor_unfilled_index = constructor_index;
6533     }
6534   else
6535     {
6536       /* Handle the case of int x = {5}; */
6537       constructor_fields = constructor_type;
6538       constructor_unfilled_fields = constructor_type;
6539     }
6540 }
6541 \f
6542 /* Push down into a subobject, for initialization.
6543    If this is for an explicit set of braces, IMPLICIT is 0.
6544    If it is because the next element belongs at a lower level,
6545    IMPLICIT is 1 (or 2 if the push is because of designator list).  */
6546
6547 void
6548 push_init_level (int implicit, struct obstack * braced_init_obstack)
6549 {
6550   struct constructor_stack *p;
6551   tree value = NULL_TREE;
6552
6553   /* If we've exhausted any levels that didn't have braces,
6554      pop them now.  If implicit == 1, this will have been done in
6555      process_init_element; do not repeat it here because in the case
6556      of excess initializers for an empty aggregate this leads to an
6557      infinite cycle of popping a level and immediately recreating
6558      it.  */
6559   if (implicit != 1)
6560     {
6561       while (constructor_stack->implicit)
6562         {
6563           if ((TREE_CODE (constructor_type) == RECORD_TYPE
6564                || TREE_CODE (constructor_type) == UNION_TYPE)
6565               && constructor_fields == 0)
6566             process_init_element (pop_init_level (1, braced_init_obstack),
6567                                   true, braced_init_obstack);
6568           else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6569                    && constructor_max_index
6570                    && tree_int_cst_lt (constructor_max_index,
6571                                        constructor_index))
6572             process_init_element (pop_init_level (1, braced_init_obstack),
6573                                   true, braced_init_obstack);
6574           else
6575             break;
6576         }
6577     }
6578
6579   /* Unless this is an explicit brace, we need to preserve previous
6580      content if any.  */
6581   if (implicit)
6582     {
6583       if ((TREE_CODE (constructor_type) == RECORD_TYPE
6584            || TREE_CODE (constructor_type) == UNION_TYPE)
6585           && constructor_fields)
6586         value = find_init_member (constructor_fields, braced_init_obstack);
6587       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6588         value = find_init_member (constructor_index, braced_init_obstack);
6589     }
6590
6591   p = XNEW (struct constructor_stack);
6592   p->type = constructor_type;
6593   p->fields = constructor_fields;
6594   p->index = constructor_index;
6595   p->max_index = constructor_max_index;
6596   p->unfilled_index = constructor_unfilled_index;
6597   p->unfilled_fields = constructor_unfilled_fields;
6598   p->bit_index = constructor_bit_index;
6599   p->elements = constructor_elements;
6600   p->constant = constructor_constant;
6601   p->simple = constructor_simple;
6602   p->nonconst = constructor_nonconst;
6603   p->erroneous = constructor_erroneous;
6604   p->pending_elts = constructor_pending_elts;
6605   p->depth = constructor_depth;
6606   p->replacement_value.value = 0;
6607   p->replacement_value.original_code = ERROR_MARK;
6608   p->replacement_value.original_type = NULL;
6609   p->implicit = implicit;
6610   p->outer = 0;
6611   p->incremental = constructor_incremental;
6612   p->designated = constructor_designated;
6613   p->next = constructor_stack;
6614   p->range_stack = 0;
6615   constructor_stack = p;
6616
6617   constructor_constant = 1;
6618   constructor_simple = 1;
6619   constructor_nonconst = 0;
6620   constructor_depth = SPELLING_DEPTH ();
6621   constructor_elements = 0;
6622   constructor_incremental = 1;
6623   constructor_designated = 0;
6624   constructor_pending_elts = 0;
6625   if (!implicit)
6626     {
6627       p->range_stack = constructor_range_stack;
6628       constructor_range_stack = 0;
6629       designator_depth = 0;
6630       designator_erroneous = 0;
6631     }
6632
6633   /* Don't die if an entire brace-pair level is superfluous
6634      in the containing level.  */
6635   if (constructor_type == 0)
6636     ;
6637   else if (TREE_CODE (constructor_type) == RECORD_TYPE
6638            || TREE_CODE (constructor_type) == UNION_TYPE)
6639     {
6640       /* Don't die if there are extra init elts at the end.  */
6641       if (constructor_fields == 0)
6642         constructor_type = 0;
6643       else
6644         {
6645           constructor_type = TREE_TYPE (constructor_fields);
6646           push_member_name (constructor_fields);
6647           constructor_depth++;
6648         }
6649     }
6650   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6651     {
6652       constructor_type = TREE_TYPE (constructor_type);
6653       push_array_bounds (tree_low_cst (constructor_index, 1));
6654       constructor_depth++;
6655     }
6656
6657   if (constructor_type == 0)
6658     {
6659       error_init ("extra brace group at end of initializer");
6660       constructor_fields = 0;
6661       constructor_unfilled_fields = 0;
6662       return;
6663     }
6664
6665   if (value && TREE_CODE (value) == CONSTRUCTOR)
6666     {
6667       constructor_constant = TREE_CONSTANT (value);
6668       constructor_simple = TREE_STATIC (value);
6669       constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6670       constructor_elements = CONSTRUCTOR_ELTS (value);
6671       if (!VEC_empty (constructor_elt, constructor_elements)
6672           && (TREE_CODE (constructor_type) == RECORD_TYPE
6673               || TREE_CODE (constructor_type) == ARRAY_TYPE))
6674         set_nonincremental_init (braced_init_obstack);
6675     }
6676
6677   if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6678     {
6679       missing_braces_mentioned = 1;
6680       warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6681     }
6682
6683   if (TREE_CODE (constructor_type) == RECORD_TYPE
6684            || TREE_CODE (constructor_type) == UNION_TYPE)
6685     {
6686       constructor_fields = TYPE_FIELDS (constructor_type);
6687       /* Skip any nameless bit fields at the beginning.  */
6688       while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6689              && DECL_NAME (constructor_fields) == 0)
6690         constructor_fields = DECL_CHAIN (constructor_fields);
6691
6692       constructor_unfilled_fields = constructor_fields;
6693       constructor_bit_index = bitsize_zero_node;
6694     }
6695   else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6696     {
6697       /* Vectors are like simple fixed-size arrays.  */
6698       constructor_max_index =
6699         build_int_cst (NULL_TREE, TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6700       constructor_index = convert (bitsizetype, integer_zero_node);
6701       constructor_unfilled_index = constructor_index;
6702     }
6703   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6704     {
6705       if (TYPE_DOMAIN (constructor_type))
6706         {
6707           constructor_max_index
6708             = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6709
6710           /* Detect non-empty initializations of zero-length arrays.  */
6711           if (constructor_max_index == NULL_TREE
6712               && TYPE_SIZE (constructor_type))
6713             constructor_max_index = integer_minus_one_node;
6714
6715           /* constructor_max_index needs to be an INTEGER_CST.  Attempts
6716              to initialize VLAs will cause a proper error; avoid tree
6717              checking errors as well by setting a safe value.  */
6718           if (constructor_max_index
6719               && TREE_CODE (constructor_max_index) != INTEGER_CST)
6720             constructor_max_index = integer_minus_one_node;
6721
6722           constructor_index
6723             = convert (bitsizetype,
6724                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6725         }
6726       else
6727         constructor_index = bitsize_zero_node;
6728
6729       constructor_unfilled_index = constructor_index;
6730       if (value && TREE_CODE (value) == STRING_CST)
6731         {
6732           /* We need to split the char/wchar array into individual
6733              characters, so that we don't have to special case it
6734              everywhere.  */
6735           set_nonincremental_init_from_string (value, braced_init_obstack);
6736         }
6737     }
6738   else
6739     {
6740       if (constructor_type != error_mark_node)
6741         warning_init (0, "braces around scalar initializer");
6742       constructor_fields = constructor_type;
6743       constructor_unfilled_fields = constructor_type;
6744     }
6745 }
6746
6747 /* At the end of an implicit or explicit brace level,
6748    finish up that level of constructor.  If a single expression
6749    with redundant braces initialized that level, return the
6750    c_expr structure for that expression.  Otherwise, the original_code
6751    element is set to ERROR_MARK.
6752    If we were outputting the elements as they are read, return 0 as the value
6753    from inner levels (process_init_element ignores that),
6754    but return error_mark_node as the value from the outermost level
6755    (that's what we want to put in DECL_INITIAL).
6756    Otherwise, return a CONSTRUCTOR expression as the value.  */
6757
6758 struct c_expr
6759 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6760 {
6761   struct constructor_stack *p;
6762   struct c_expr ret;
6763   ret.value = 0;
6764   ret.original_code = ERROR_MARK;
6765   ret.original_type = NULL;
6766
6767   if (implicit == 0)
6768     {
6769       /* When we come to an explicit close brace,
6770          pop any inner levels that didn't have explicit braces.  */
6771       while (constructor_stack->implicit)
6772         {
6773           process_init_element (pop_init_level (1, braced_init_obstack),
6774                                 true, braced_init_obstack);
6775         }
6776       gcc_assert (!constructor_range_stack);
6777     }
6778
6779   /* Now output all pending elements.  */
6780   constructor_incremental = 1;
6781   output_pending_init_elements (1, braced_init_obstack);
6782
6783   p = constructor_stack;
6784
6785   /* Error for initializing a flexible array member, or a zero-length
6786      array member in an inappropriate context.  */
6787   if (constructor_type && constructor_fields
6788       && TREE_CODE (constructor_type) == ARRAY_TYPE
6789       && TYPE_DOMAIN (constructor_type)
6790       && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6791     {
6792       /* Silently discard empty initializations.  The parser will
6793          already have pedwarned for empty brackets.  */
6794       if (integer_zerop (constructor_unfilled_index))
6795         constructor_type = NULL_TREE;
6796       else
6797         {
6798           gcc_assert (!TYPE_SIZE (constructor_type));
6799
6800           if (constructor_depth > 2)
6801             error_init ("initialization of flexible array member in a nested context");
6802           else
6803             pedwarn_init (input_location, OPT_pedantic,
6804                           "initialization of a flexible array member");
6805
6806           /* We have already issued an error message for the existence
6807              of a flexible array member not at the end of the structure.
6808              Discard the initializer so that we do not die later.  */
6809           if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6810             constructor_type = NULL_TREE;
6811         }
6812     }
6813
6814   /* Warn when some struct elements are implicitly initialized to zero.  */
6815   if (warn_missing_field_initializers
6816       && constructor_type
6817       && TREE_CODE (constructor_type) == RECORD_TYPE
6818       && constructor_unfilled_fields)
6819     {
6820         /* Do not warn for flexible array members or zero-length arrays.  */
6821         while (constructor_unfilled_fields
6822                && (!DECL_SIZE (constructor_unfilled_fields)
6823                    || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6824           constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
6825
6826         /* Do not warn if this level of the initializer uses member
6827            designators; it is likely to be deliberate.  */
6828         if (constructor_unfilled_fields && !constructor_designated)
6829           {
6830             push_member_name (constructor_unfilled_fields);
6831             warning_init (OPT_Wmissing_field_initializers,
6832                           "missing initializer");
6833             RESTORE_SPELLING_DEPTH (constructor_depth);
6834           }
6835     }
6836
6837   /* Pad out the end of the structure.  */
6838   if (p->replacement_value.value)
6839     /* If this closes a superfluous brace pair,
6840        just pass out the element between them.  */
6841     ret = p->replacement_value;
6842   else if (constructor_type == 0)
6843     ;
6844   else if (TREE_CODE (constructor_type) != RECORD_TYPE
6845            && TREE_CODE (constructor_type) != UNION_TYPE
6846            && TREE_CODE (constructor_type) != ARRAY_TYPE
6847            && TREE_CODE (constructor_type) != VECTOR_TYPE)
6848     {
6849       /* A nonincremental scalar initializer--just return
6850          the element, after verifying there is just one.  */
6851       if (VEC_empty (constructor_elt,constructor_elements))
6852         {
6853           if (!constructor_erroneous)
6854             error_init ("empty scalar initializer");
6855           ret.value = error_mark_node;
6856         }
6857       else if (VEC_length (constructor_elt,constructor_elements) != 1)
6858         {
6859           error_init ("extra elements in scalar initializer");
6860           ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6861         }
6862       else
6863         ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
6864     }
6865   else
6866     {
6867       if (constructor_erroneous)
6868         ret.value = error_mark_node;
6869       else
6870         {
6871           ret.value = build_constructor (constructor_type,
6872                                          constructor_elements);
6873           if (constructor_constant)
6874             TREE_CONSTANT (ret.value) = 1;
6875           if (constructor_constant && constructor_simple)
6876             TREE_STATIC (ret.value) = 1;
6877           if (constructor_nonconst)
6878             CONSTRUCTOR_NON_CONST (ret.value) = 1;
6879         }
6880     }
6881
6882   if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
6883     {
6884       if (constructor_nonconst)
6885         ret.original_code = C_MAYBE_CONST_EXPR;
6886       else if (ret.original_code == C_MAYBE_CONST_EXPR)
6887         ret.original_code = ERROR_MARK;
6888     }
6889
6890   constructor_type = p->type;
6891   constructor_fields = p->fields;
6892   constructor_index = p->index;
6893   constructor_max_index = p->max_index;
6894   constructor_unfilled_index = p->unfilled_index;
6895   constructor_unfilled_fields = p->unfilled_fields;
6896   constructor_bit_index = p->bit_index;
6897   constructor_elements = p->elements;
6898   constructor_constant = p->constant;
6899   constructor_simple = p->simple;
6900   constructor_nonconst = p->nonconst;
6901   constructor_erroneous = p->erroneous;
6902   constructor_incremental = p->incremental;
6903   constructor_designated = p->designated;
6904   constructor_pending_elts = p->pending_elts;
6905   constructor_depth = p->depth;
6906   if (!p->implicit)
6907     constructor_range_stack = p->range_stack;
6908   RESTORE_SPELLING_DEPTH (constructor_depth);
6909
6910   constructor_stack = p->next;
6911   free (p);
6912
6913   if (ret.value == 0 && constructor_stack == 0)
6914     ret.value = error_mark_node;
6915   return ret;
6916 }
6917
6918 /* Common handling for both array range and field name designators.
6919    ARRAY argument is nonzero for array ranges.  Returns zero for success.  */
6920
6921 static int
6922 set_designator (int array, struct obstack * braced_init_obstack)
6923 {
6924   tree subtype;
6925   enum tree_code subcode;
6926
6927   /* Don't die if an entire brace-pair level is superfluous
6928      in the containing level.  */
6929   if (constructor_type == 0)
6930     return 1;
6931
6932   /* If there were errors in this designator list already, bail out
6933      silently.  */
6934   if (designator_erroneous)
6935     return 1;
6936
6937   if (!designator_depth)
6938     {
6939       gcc_assert (!constructor_range_stack);
6940
6941       /* Designator list starts at the level of closest explicit
6942          braces.  */
6943       while (constructor_stack->implicit)
6944         {
6945           process_init_element (pop_init_level (1, braced_init_obstack),
6946                                 true, braced_init_obstack);
6947         }
6948       constructor_designated = 1;
6949       return 0;
6950     }
6951
6952   switch (TREE_CODE (constructor_type))
6953     {
6954     case  RECORD_TYPE:
6955     case  UNION_TYPE:
6956       subtype = TREE_TYPE (constructor_fields);
6957       if (subtype != error_mark_node)
6958         subtype = TYPE_MAIN_VARIANT (subtype);
6959       break;
6960     case ARRAY_TYPE:
6961       subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6962       break;
6963     default:
6964       gcc_unreachable ();
6965     }
6966
6967   subcode = TREE_CODE (subtype);
6968   if (array && subcode != ARRAY_TYPE)
6969     {
6970       error_init ("array index in non-array initializer");
6971       return 1;
6972     }
6973   else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
6974     {
6975       error_init ("field name not in record or union initializer");
6976       return 1;
6977     }
6978
6979   constructor_designated = 1;
6980   push_init_level (2, braced_init_obstack);
6981   return 0;
6982 }
6983
6984 /* If there are range designators in designator list, push a new designator
6985    to constructor_range_stack.  RANGE_END is end of such stack range or
6986    NULL_TREE if there is no range designator at this level.  */
6987
6988 static void
6989 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
6990 {
6991   struct constructor_range_stack *p;
6992
6993   p = (struct constructor_range_stack *)
6994     obstack_alloc (braced_init_obstack,
6995                    sizeof (struct constructor_range_stack));
6996   p->prev = constructor_range_stack;
6997   p->next = 0;
6998   p->fields = constructor_fields;
6999   p->range_start = constructor_index;
7000   p->index = constructor_index;
7001   p->stack = constructor_stack;
7002   p->range_end = range_end;
7003   if (constructor_range_stack)
7004     constructor_range_stack->next = p;
7005   constructor_range_stack = p;
7006 }
7007
7008 /* Within an array initializer, specify the next index to be initialized.
7009    FIRST is that index.  If LAST is nonzero, then initialize a range
7010    of indices, running from FIRST through LAST.  */
7011
7012 void
7013 set_init_index (tree first, tree last,
7014                 struct obstack * braced_init_obstack)
7015 {
7016   if (set_designator (1, braced_init_obstack))
7017     return;
7018
7019   designator_erroneous = 1;
7020
7021   if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7022       || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7023     {
7024       error_init ("array index in initializer not of integer type");
7025       return;
7026     }
7027
7028   if (TREE_CODE (first) != INTEGER_CST)
7029     {
7030       first = c_fully_fold (first, false, NULL);
7031       if (TREE_CODE (first) == INTEGER_CST)
7032         pedwarn_init (input_location, OPT_pedantic,
7033                       "array index in initializer is not "
7034                       "an integer constant expression");
7035     }
7036
7037   if (last && TREE_CODE (last) != INTEGER_CST)
7038     {
7039       last = c_fully_fold (last, false, NULL);
7040       if (TREE_CODE (last) == INTEGER_CST)
7041         pedwarn_init (input_location, OPT_pedantic,
7042                       "array index in initializer is not "
7043                       "an integer constant expression");
7044     }
7045
7046   if (TREE_CODE (first) != INTEGER_CST)
7047     error_init ("nonconstant array index in initializer");
7048   else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7049     error_init ("nonconstant array index in initializer");
7050   else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7051     error_init ("array index in non-array initializer");
7052   else if (tree_int_cst_sgn (first) == -1)
7053     error_init ("array index in initializer exceeds array bounds");
7054   else if (constructor_max_index
7055            && tree_int_cst_lt (constructor_max_index, first))
7056     error_init ("array index in initializer exceeds array bounds");
7057   else
7058     {
7059       constant_expression_warning (first);
7060       if (last)
7061         constant_expression_warning (last);
7062       constructor_index = convert (bitsizetype, first);
7063
7064       if (last)
7065         {
7066           if (tree_int_cst_equal (first, last))
7067             last = 0;
7068           else if (tree_int_cst_lt (last, first))
7069             {
7070               error_init ("empty index range in initializer");
7071               last = 0;
7072             }
7073           else
7074             {
7075               last = convert (bitsizetype, last);
7076               if (constructor_max_index != 0
7077                   && tree_int_cst_lt (constructor_max_index, last))
7078                 {
7079                   error_init ("array index range in initializer exceeds array bounds");
7080                   last = 0;
7081                 }
7082             }
7083         }
7084
7085       designator_depth++;
7086       designator_erroneous = 0;
7087       if (constructor_range_stack || last)
7088         push_range_stack (last, braced_init_obstack);
7089     }
7090 }
7091
7092 /* Within a struct initializer, specify the next field to be initialized.  */
7093
7094 void
7095 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7096 {
7097   tree field;
7098
7099   if (set_designator (0, braced_init_obstack))
7100     return;
7101
7102   designator_erroneous = 1;
7103
7104   if (TREE_CODE (constructor_type) != RECORD_TYPE
7105       && TREE_CODE (constructor_type) != UNION_TYPE)
7106     {
7107       error_init ("field name not in record or union initializer");
7108       return;
7109     }
7110
7111   field = lookup_field (constructor_type, fieldname);
7112
7113   if (field == 0)
7114     error ("unknown field %qE specified in initializer", fieldname);
7115   else
7116     do
7117       {
7118         constructor_fields = TREE_VALUE (field);
7119         designator_depth++;
7120         designator_erroneous = 0;
7121         if (constructor_range_stack)
7122           push_range_stack (NULL_TREE, braced_init_obstack);
7123         field = TREE_CHAIN (field);
7124         if (field)
7125           {
7126             if (set_designator (0, braced_init_obstack))
7127               return;
7128           }
7129       }
7130     while (field != NULL_TREE);
7131 }
7132 \f
7133 /* Add a new initializer to the tree of pending initializers.  PURPOSE
7134    identifies the initializer, either array index or field in a structure.
7135    VALUE is the value of that index or field.  If ORIGTYPE is not
7136    NULL_TREE, it is the original type of VALUE.
7137
7138    IMPLICIT is true if value comes from pop_init_level (1),
7139    the new initializer has been merged with the existing one
7140    and thus no warnings should be emitted about overriding an
7141    existing initializer.  */
7142
7143 static void
7144 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7145                   struct obstack * braced_init_obstack)
7146 {
7147   struct init_node *p, **q, *r;
7148
7149   q = &constructor_pending_elts;
7150   p = 0;
7151
7152   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7153     {
7154       while (*q != 0)
7155         {
7156           p = *q;
7157           if (tree_int_cst_lt (purpose, p->purpose))
7158             q = &p->left;
7159           else if (tree_int_cst_lt (p->purpose, purpose))
7160             q = &p->right;
7161           else
7162             {
7163               if (!implicit)
7164                 {
7165                   if (TREE_SIDE_EFFECTS (p->value))
7166                     warning_init (0, "initialized field with side-effects overwritten");
7167                   else if (warn_override_init)
7168                     warning_init (OPT_Woverride_init, "initialized field overwritten");
7169                 }
7170               p->value = value;
7171               p->origtype = origtype;
7172               return;
7173             }
7174         }
7175     }
7176   else
7177     {
7178       tree bitpos;
7179
7180       bitpos = bit_position (purpose);
7181       while (*q != NULL)
7182         {
7183           p = *q;
7184           if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7185             q = &p->left;
7186           else if (p->purpose != purpose)
7187             q = &p->right;
7188           else
7189             {
7190               if (!implicit)
7191                 {
7192                   if (TREE_SIDE_EFFECTS (p->value))
7193                     warning_init (0, "initialized field with side-effects overwritten");
7194                   else if (warn_override_init)
7195                     warning_init (OPT_Woverride_init, "initialized field overwritten");
7196                 }
7197               p->value = value;
7198               p->origtype = origtype;
7199               return;
7200             }
7201         }
7202     }
7203
7204   r = (struct init_node *) obstack_alloc (braced_init_obstack,
7205                                           sizeof (struct init_node));
7206   r->purpose = purpose;
7207   r->value = value;
7208   r->origtype = origtype;
7209
7210   *q = r;
7211   r->parent = p;
7212   r->left = 0;
7213   r->right = 0;
7214   r->balance = 0;
7215
7216   while (p)
7217     {
7218       struct init_node *s;
7219
7220       if (r == p->left)
7221         {
7222           if (p->balance == 0)
7223             p->balance = -1;
7224           else if (p->balance < 0)
7225             {
7226               if (r->balance < 0)
7227                 {
7228                   /* L rotation.  */
7229                   p->left = r->right;
7230                   if (p->left)
7231                     p->left->parent = p;
7232                   r->right = p;
7233
7234                   p->balance = 0;
7235                   r->balance = 0;
7236
7237                   s = p->parent;
7238                   p->parent = r;
7239                   r->parent = s;
7240                   if (s)
7241                     {
7242                       if (s->left == p)
7243                         s->left = r;
7244                       else
7245                         s->right = r;
7246                     }
7247                   else
7248                     constructor_pending_elts = r;
7249                 }
7250               else
7251                 {
7252                   /* LR rotation.  */
7253                   struct init_node *t = r->right;
7254
7255                   r->right = t->left;
7256                   if (r->right)
7257                     r->right->parent = r;
7258                   t->left = r;
7259
7260                   p->left = t->right;
7261                   if (p->left)
7262                     p->left->parent = p;
7263                   t->right = p;
7264
7265                   p->balance = t->balance < 0;
7266                   r->balance = -(t->balance > 0);
7267                   t->balance = 0;
7268
7269                   s = p->parent;
7270                   p->parent = t;
7271                   r->parent = t;
7272                   t->parent = s;
7273                   if (s)
7274                     {
7275                       if (s->left == p)
7276                         s->left = t;
7277                       else
7278                         s->right = t;
7279                     }
7280                   else
7281                     constructor_pending_elts = t;
7282                 }
7283               break;
7284             }
7285           else
7286             {
7287               /* p->balance == +1; growth of left side balances the node.  */
7288               p->balance = 0;
7289               break;
7290             }
7291         }
7292       else /* r == p->right */
7293         {
7294           if (p->balance == 0)
7295             /* Growth propagation from right side.  */
7296             p->balance++;
7297           else if (p->balance > 0)
7298             {
7299               if (r->balance > 0)
7300                 {
7301                   /* R rotation.  */
7302                   p->right = r->left;
7303                   if (p->right)
7304                     p->right->parent = p;
7305                   r->left = p;
7306
7307                   p->balance = 0;
7308                   r->balance = 0;
7309
7310                   s = p->parent;
7311                   p->parent = r;
7312                   r->parent = s;
7313                   if (s)
7314                     {
7315                       if (s->left == p)
7316                         s->left = r;
7317                       else
7318                         s->right = r;
7319                     }
7320                   else
7321                     constructor_pending_elts = r;
7322                 }
7323               else /* r->balance == -1 */
7324                 {
7325                   /* RL rotation */
7326                   struct init_node *t = r->left;
7327
7328                   r->left = t->right;
7329                   if (r->left)
7330                     r->left->parent = r;
7331                   t->right = r;
7332
7333                   p->right = t->left;
7334                   if (p->right)
7335                     p->right->parent = p;
7336                   t->left = p;
7337
7338                   r->balance = (t->balance < 0);
7339                   p->balance = -(t->balance > 0);
7340                   t->balance = 0;
7341
7342                   s = p->parent;
7343                   p->parent = t;
7344                   r->parent = t;
7345                   t->parent = s;
7346                   if (s)
7347                     {
7348                       if (s->left == p)
7349                         s->left = t;
7350                       else
7351                         s->right = t;
7352                     }
7353                   else
7354                     constructor_pending_elts = t;
7355                 }
7356               break;
7357             }
7358           else
7359             {
7360               /* p->balance == -1; growth of right side balances the node.  */
7361               p->balance = 0;
7362               break;
7363             }
7364         }
7365
7366       r = p;
7367       p = p->parent;
7368     }
7369 }
7370
7371 /* Build AVL tree from a sorted chain.  */
7372
7373 static void
7374 set_nonincremental_init (struct obstack * braced_init_obstack)
7375 {
7376   unsigned HOST_WIDE_INT ix;
7377   tree index, value;
7378
7379   if (TREE_CODE (constructor_type) != RECORD_TYPE
7380       && TREE_CODE (constructor_type) != ARRAY_TYPE)
7381     return;
7382
7383   FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7384     {
7385       add_pending_init (index, value, NULL_TREE, false,
7386                         braced_init_obstack);
7387     }
7388   constructor_elements = 0;
7389   if (TREE_CODE (constructor_type) == RECORD_TYPE)
7390     {
7391       constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7392       /* Skip any nameless bit fields at the beginning.  */
7393       while (constructor_unfilled_fields != 0
7394              && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7395              && DECL_NAME (constructor_unfilled_fields) == 0)
7396         constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7397
7398     }
7399   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7400     {
7401       if (TYPE_DOMAIN (constructor_type))
7402         constructor_unfilled_index
7403             = convert (bitsizetype,
7404                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7405       else
7406         constructor_unfilled_index = bitsize_zero_node;
7407     }
7408   constructor_incremental = 0;
7409 }
7410
7411 /* Build AVL tree from a string constant.  */
7412
7413 static void
7414 set_nonincremental_init_from_string (tree str,
7415                                      struct obstack * braced_init_obstack)
7416 {
7417   tree value, purpose, type;
7418   HOST_WIDE_INT val[2];
7419   const char *p, *end;
7420   int byte, wchar_bytes, charwidth, bitpos;
7421
7422   gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7423
7424   wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7425   charwidth = TYPE_PRECISION (char_type_node);
7426   type = TREE_TYPE (constructor_type);
7427   p = TREE_STRING_POINTER (str);
7428   end = p + TREE_STRING_LENGTH (str);
7429
7430   for (purpose = bitsize_zero_node;
7431        p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7432        purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7433     {
7434       if (wchar_bytes == 1)
7435         {
7436           val[1] = (unsigned char) *p++;
7437           val[0] = 0;
7438         }
7439       else
7440         {
7441           val[0] = 0;
7442           val[1] = 0;
7443           for (byte = 0; byte < wchar_bytes; byte++)
7444             {
7445               if (BYTES_BIG_ENDIAN)
7446                 bitpos = (wchar_bytes - byte - 1) * charwidth;
7447               else
7448                 bitpos = byte * charwidth;
7449               val[bitpos < HOST_BITS_PER_WIDE_INT]
7450                 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7451                    << (bitpos % HOST_BITS_PER_WIDE_INT);
7452             }
7453         }
7454
7455       if (!TYPE_UNSIGNED (type))
7456         {
7457           bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7458           if (bitpos < HOST_BITS_PER_WIDE_INT)
7459             {
7460               if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7461                 {
7462                   val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7463                   val[0] = -1;
7464                 }
7465             }
7466           else if (bitpos == HOST_BITS_PER_WIDE_INT)
7467             {
7468               if (val[1] < 0)
7469                 val[0] = -1;
7470             }
7471           else if (val[0] & (((HOST_WIDE_INT) 1)
7472                              << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7473             val[0] |= ((HOST_WIDE_INT) -1)
7474                       << (bitpos - HOST_BITS_PER_WIDE_INT);
7475         }
7476
7477       value = build_int_cst_wide (type, val[1], val[0]);
7478       add_pending_init (purpose, value, NULL_TREE, false,
7479                         braced_init_obstack);
7480     }
7481
7482   constructor_incremental = 0;
7483 }
7484
7485 /* Return value of FIELD in pending initializer or zero if the field was
7486    not initialized yet.  */
7487
7488 static tree
7489 find_init_member (tree field, struct obstack * braced_init_obstack)
7490 {
7491   struct init_node *p;
7492
7493   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7494     {
7495       if (constructor_incremental
7496           && tree_int_cst_lt (field, constructor_unfilled_index))
7497         set_nonincremental_init (braced_init_obstack);
7498
7499       p = constructor_pending_elts;
7500       while (p)
7501         {
7502           if (tree_int_cst_lt (field, p->purpose))
7503             p = p->left;
7504           else if (tree_int_cst_lt (p->purpose, field))
7505             p = p->right;
7506           else
7507             return p->value;
7508         }
7509     }
7510   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7511     {
7512       tree bitpos = bit_position (field);
7513
7514       if (constructor_incremental
7515           && (!constructor_unfilled_fields
7516               || tree_int_cst_lt (bitpos,
7517                                   bit_position (constructor_unfilled_fields))))
7518         set_nonincremental_init (braced_init_obstack);
7519
7520       p = constructor_pending_elts;
7521       while (p)
7522         {
7523           if (field == p->purpose)
7524             return p->value;
7525           else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7526             p = p->left;
7527           else
7528             p = p->right;
7529         }
7530     }
7531   else if (TREE_CODE (constructor_type) == UNION_TYPE)
7532     {
7533       if (!VEC_empty (constructor_elt, constructor_elements)
7534           && (VEC_last (constructor_elt, constructor_elements)->index
7535               == field))
7536         return VEC_last (constructor_elt, constructor_elements)->value;
7537     }
7538   return 0;
7539 }
7540
7541 /* "Output" the next constructor element.
7542    At top level, really output it to assembler code now.
7543    Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7544    If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7545    TYPE is the data type that the containing data type wants here.
7546    FIELD is the field (a FIELD_DECL) or the index that this element fills.
7547    If VALUE is a string constant, STRICT_STRING is true if it is
7548    unparenthesized or we should not warn here for it being parenthesized.
7549    For other types of VALUE, STRICT_STRING is not used.
7550
7551    PENDING if non-nil means output pending elements that belong
7552    right after this element.  (PENDING is normally 1;
7553    it is 0 while outputting pending elements, to avoid recursion.)
7554
7555    IMPLICIT is true if value comes from pop_init_level (1),
7556    the new initializer has been merged with the existing one
7557    and thus no warnings should be emitted about overriding an
7558    existing initializer.  */
7559
7560 static void
7561 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7562                      tree field, int pending, bool implicit,
7563                      struct obstack * braced_init_obstack)
7564 {
7565   tree semantic_type = NULL_TREE;
7566   constructor_elt *celt;
7567   bool maybe_const = true;
7568   bool npc;
7569
7570   if (type == error_mark_node || value == error_mark_node)
7571     {
7572       constructor_erroneous = 1;
7573       return;
7574     }
7575   if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7576       && (TREE_CODE (value) == STRING_CST
7577           || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7578       && !(TREE_CODE (value) == STRING_CST
7579            && TREE_CODE (type) == ARRAY_TYPE
7580            && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7581       && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7582                      TYPE_MAIN_VARIANT (type)))
7583     value = array_to_pointer_conversion (input_location, value);
7584
7585   if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7586       && require_constant_value && !flag_isoc99 && pending)
7587     {
7588       /* As an extension, allow initializing objects with static storage
7589          duration with compound literals (which are then treated just as
7590          the brace enclosed list they contain).  */
7591       tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7592       value = DECL_INITIAL (decl);
7593     }
7594
7595   npc = null_pointer_constant_p (value);
7596   if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7597     {
7598       semantic_type = TREE_TYPE (value);
7599       value = TREE_OPERAND (value, 0);
7600     }
7601   value = c_fully_fold (value, require_constant_value, &maybe_const);
7602
7603   if (value == error_mark_node)
7604     constructor_erroneous = 1;
7605   else if (!TREE_CONSTANT (value))
7606     constructor_constant = 0;
7607   else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7608            || ((TREE_CODE (constructor_type) == RECORD_TYPE
7609                 || TREE_CODE (constructor_type) == UNION_TYPE)
7610                && DECL_C_BIT_FIELD (field)
7611                && TREE_CODE (value) != INTEGER_CST))
7612     constructor_simple = 0;
7613   if (!maybe_const)
7614     constructor_nonconst = 1;
7615
7616   if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7617     {
7618       if (require_constant_value)
7619         {
7620           error_init ("initializer element is not constant");
7621           value = error_mark_node;
7622         }
7623       else if (require_constant_elements)
7624         pedwarn (input_location, 0,
7625                  "initializer element is not computable at load time");
7626     }
7627   else if (!maybe_const
7628            && (require_constant_value || require_constant_elements))
7629     pedwarn_init (input_location, 0,
7630                   "initializer element is not a constant expression");
7631
7632   /* Issue -Wc++-compat warnings about initializing a bitfield with
7633      enum type.  */
7634   if (warn_cxx_compat
7635       && field != NULL_TREE
7636       && TREE_CODE (field) == FIELD_DECL
7637       && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7638       && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7639           != TYPE_MAIN_VARIANT (type))
7640       && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7641     {
7642       tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7643       if (checktype != error_mark_node
7644           && (TYPE_MAIN_VARIANT (checktype)
7645               != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7646         warning_init (OPT_Wc___compat,
7647                       "enum conversion in initialization is invalid in C++");
7648     }
7649
7650   /* If this field is empty (and not at the end of structure),
7651      don't do anything other than checking the initializer.  */
7652   if (field
7653       && (TREE_TYPE (field) == error_mark_node
7654           || (COMPLETE_TYPE_P (TREE_TYPE (field))
7655               && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7656               && (TREE_CODE (constructor_type) == ARRAY_TYPE
7657                   || DECL_CHAIN (field)))))
7658     return;
7659
7660   if (semantic_type)
7661     value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7662   value = digest_init (input_location, type, value, origtype, npc,
7663                        strict_string, require_constant_value);
7664   if (value == error_mark_node)
7665     {
7666       constructor_erroneous = 1;
7667       return;
7668     }
7669   if (require_constant_value || require_constant_elements)
7670     constant_expression_warning (value);
7671
7672   /* If this element doesn't come next in sequence,
7673      put it on constructor_pending_elts.  */
7674   if (TREE_CODE (constructor_type) == ARRAY_TYPE
7675       && (!constructor_incremental
7676           || !tree_int_cst_equal (field, constructor_unfilled_index)))
7677     {
7678       if (constructor_incremental
7679           && tree_int_cst_lt (field, constructor_unfilled_index))
7680         set_nonincremental_init (braced_init_obstack);
7681
7682       add_pending_init (field, value, origtype, implicit,
7683                         braced_init_obstack);
7684       return;
7685     }
7686   else if (TREE_CODE (constructor_type) == RECORD_TYPE
7687            && (!constructor_incremental
7688                || field != constructor_unfilled_fields))
7689     {
7690       /* We do this for records but not for unions.  In a union,
7691          no matter which field is specified, it can be initialized
7692          right away since it starts at the beginning of the union.  */
7693       if (constructor_incremental)
7694         {
7695           if (!constructor_unfilled_fields)
7696             set_nonincremental_init (braced_init_obstack);
7697           else
7698             {
7699               tree bitpos, unfillpos;
7700
7701               bitpos = bit_position (field);
7702               unfillpos = bit_position (constructor_unfilled_fields);
7703
7704               if (tree_int_cst_lt (bitpos, unfillpos))
7705                 set_nonincremental_init (braced_init_obstack);
7706             }
7707         }
7708
7709       add_pending_init (field, value, origtype, implicit,
7710                         braced_init_obstack);
7711       return;
7712     }
7713   else if (TREE_CODE (constructor_type) == UNION_TYPE
7714            && !VEC_empty (constructor_elt, constructor_elements))
7715     {
7716       if (!implicit)
7717         {
7718           if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7719                                            constructor_elements)->value))
7720             warning_init (0,
7721                           "initialized field with side-effects overwritten");
7722           else if (warn_override_init)
7723             warning_init (OPT_Woverride_init, "initialized field overwritten");
7724         }
7725
7726       /* We can have just one union field set.  */
7727       constructor_elements = 0;
7728     }
7729
7730   /* Otherwise, output this element either to
7731      constructor_elements or to the assembler file.  */
7732
7733   celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7734   celt->index = field;
7735   celt->value = value;
7736
7737   /* Advance the variable that indicates sequential elements output.  */
7738   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7739     constructor_unfilled_index
7740       = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7741                         bitsize_one_node);
7742   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7743     {
7744       constructor_unfilled_fields
7745         = DECL_CHAIN (constructor_unfilled_fields);
7746
7747       /* Skip any nameless bit fields.  */
7748       while (constructor_unfilled_fields != 0
7749              && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7750              && DECL_NAME (constructor_unfilled_fields) == 0)
7751         constructor_unfilled_fields =
7752           DECL_CHAIN (constructor_unfilled_fields);
7753     }
7754   else if (TREE_CODE (constructor_type) == UNION_TYPE)
7755     constructor_unfilled_fields = 0;
7756
7757   /* Now output any pending elements which have become next.  */
7758   if (pending)
7759     output_pending_init_elements (0, braced_init_obstack);
7760 }
7761
7762 /* Output any pending elements which have become next.
7763    As we output elements, constructor_unfilled_{fields,index}
7764    advances, which may cause other elements to become next;
7765    if so, they too are output.
7766
7767    If ALL is 0, we return when there are
7768    no more pending elements to output now.
7769
7770    If ALL is 1, we output space as necessary so that
7771    we can output all the pending elements.  */
7772 static void
7773 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7774 {
7775   struct init_node *elt = constructor_pending_elts;
7776   tree next;
7777
7778  retry:
7779
7780   /* Look through the whole pending tree.
7781      If we find an element that should be output now,
7782      output it.  Otherwise, set NEXT to the element
7783      that comes first among those still pending.  */
7784
7785   next = 0;
7786   while (elt)
7787     {
7788       if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7789         {
7790           if (tree_int_cst_equal (elt->purpose,
7791                                   constructor_unfilled_index))
7792             output_init_element (elt->value, elt->origtype, true,
7793                                  TREE_TYPE (constructor_type),
7794                                  constructor_unfilled_index, 0, false,
7795                                  braced_init_obstack);
7796           else if (tree_int_cst_lt (constructor_unfilled_index,
7797                                     elt->purpose))
7798             {
7799               /* Advance to the next smaller node.  */
7800               if (elt->left)
7801                 elt = elt->left;
7802               else
7803                 {
7804                   /* We have reached the smallest node bigger than the
7805                      current unfilled index.  Fill the space first.  */
7806                   next = elt->purpose;
7807                   break;
7808                 }
7809             }
7810           else
7811             {
7812               /* Advance to the next bigger node.  */
7813               if (elt->right)
7814                 elt = elt->right;
7815               else
7816                 {
7817                   /* We have reached the biggest node in a subtree.  Find
7818                      the parent of it, which is the next bigger node.  */
7819                   while (elt->parent && elt->parent->right == elt)
7820                     elt = elt->parent;
7821                   elt = elt->parent;
7822                   if (elt && tree_int_cst_lt (constructor_unfilled_index,
7823                                               elt->purpose))
7824                     {
7825                       next = elt->purpose;
7826                       break;
7827                     }
7828                 }
7829             }
7830         }
7831       else if (TREE_CODE (constructor_type) == RECORD_TYPE
7832                || TREE_CODE (constructor_type) == UNION_TYPE)
7833         {
7834           tree ctor_unfilled_bitpos, elt_bitpos;
7835
7836           /* If the current record is complete we are done.  */
7837           if (constructor_unfilled_fields == 0)
7838             break;
7839
7840           ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
7841           elt_bitpos = bit_position (elt->purpose);
7842           /* We can't compare fields here because there might be empty
7843              fields in between.  */
7844           if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
7845             {
7846               constructor_unfilled_fields = elt->purpose;
7847               output_init_element (elt->value, elt->origtype, true,
7848                                    TREE_TYPE (elt->purpose),
7849                                    elt->purpose, 0, false,
7850                                    braced_init_obstack);
7851             }
7852           else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
7853             {
7854               /* Advance to the next smaller node.  */
7855               if (elt->left)
7856                 elt = elt->left;
7857               else
7858                 {
7859                   /* We have reached the smallest node bigger than the
7860                      current unfilled field.  Fill the space first.  */
7861                   next = elt->purpose;
7862                   break;
7863                 }
7864             }
7865           else
7866             {
7867               /* Advance to the next bigger node.  */
7868               if (elt->right)
7869                 elt = elt->right;
7870               else
7871                 {
7872                   /* We have reached the biggest node in a subtree.  Find
7873                      the parent of it, which is the next bigger node.  */
7874                   while (elt->parent && elt->parent->right == elt)
7875                     elt = elt->parent;
7876                   elt = elt->parent;
7877                   if (elt
7878                       && (tree_int_cst_lt (ctor_unfilled_bitpos,
7879                                            bit_position (elt->purpose))))
7880                     {
7881                       next = elt->purpose;
7882                       break;
7883                     }
7884                 }
7885             }
7886         }
7887     }
7888
7889   /* Ordinarily return, but not if we want to output all
7890      and there are elements left.  */
7891   if (!(all && next != 0))
7892     return;
7893
7894   /* If it's not incremental, just skip over the gap, so that after
7895      jumping to retry we will output the next successive element.  */
7896   if (TREE_CODE (constructor_type) == RECORD_TYPE
7897       || TREE_CODE (constructor_type) == UNION_TYPE)
7898     constructor_unfilled_fields = next;
7899   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7900     constructor_unfilled_index = next;
7901
7902   /* ELT now points to the node in the pending tree with the next
7903      initializer to output.  */
7904   goto retry;
7905 }
7906 \f
7907 /* Add one non-braced element to the current constructor level.
7908    This adjusts the current position within the constructor's type.
7909    This may also start or terminate implicit levels
7910    to handle a partly-braced initializer.
7911
7912    Once this has found the correct level for the new element,
7913    it calls output_init_element.
7914
7915    IMPLICIT is true if value comes from pop_init_level (1),
7916    the new initializer has been merged with the existing one
7917    and thus no warnings should be emitted about overriding an
7918    existing initializer.  */
7919
7920 void
7921 process_init_element (struct c_expr value, bool implicit,
7922                       struct obstack * braced_init_obstack)
7923 {
7924   tree orig_value = value.value;
7925   int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
7926   bool strict_string = value.original_code == STRING_CST;
7927
7928   designator_depth = 0;
7929   designator_erroneous = 0;
7930
7931   /* Handle superfluous braces around string cst as in
7932      char x[] = {"foo"}; */
7933   if (string_flag
7934       && constructor_type
7935       && TREE_CODE (constructor_type) == ARRAY_TYPE
7936       && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
7937       && integer_zerop (constructor_unfilled_index))
7938     {
7939       if (constructor_stack->replacement_value.value)
7940         error_init ("excess elements in char array initializer");
7941       constructor_stack->replacement_value = value;
7942       return;
7943     }
7944
7945   if (constructor_stack->replacement_value.value != 0)
7946     {
7947       error_init ("excess elements in struct initializer");
7948       return;
7949     }
7950
7951   /* Ignore elements of a brace group if it is entirely superfluous
7952      and has already been diagnosed.  */
7953   if (constructor_type == 0)
7954     return;
7955
7956   /* If we've exhausted any levels that didn't have braces,
7957      pop them now.  */
7958   while (constructor_stack->implicit)
7959     {
7960       if ((TREE_CODE (constructor_type) == RECORD_TYPE
7961            || TREE_CODE (constructor_type) == UNION_TYPE)
7962           && constructor_fields == 0)
7963         process_init_element (pop_init_level (1, braced_init_obstack),
7964                               true, braced_init_obstack);
7965       else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
7966                 || TREE_CODE (constructor_type) == VECTOR_TYPE)
7967                && (constructor_max_index == 0
7968                    || tree_int_cst_lt (constructor_max_index,
7969                                        constructor_index)))
7970         process_init_element (pop_init_level (1, braced_init_obstack),
7971                               true, braced_init_obstack);
7972       else
7973         break;
7974     }
7975
7976   /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once.  */
7977   if (constructor_range_stack)
7978     {
7979       /* If value is a compound literal and we'll be just using its
7980          content, don't put it into a SAVE_EXPR.  */
7981       if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
7982           || !require_constant_value
7983           || flag_isoc99)
7984         {
7985           tree semantic_type = NULL_TREE;
7986           if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
7987             {
7988               semantic_type = TREE_TYPE (value.value);
7989               value.value = TREE_OPERAND (value.value, 0);
7990             }
7991           value.value = c_save_expr (value.value);
7992           if (semantic_type)
7993             value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7994                                   value.value);
7995         }
7996     }
7997
7998   while (1)
7999     {
8000       if (TREE_CODE (constructor_type) == RECORD_TYPE)
8001         {
8002           tree fieldtype;
8003           enum tree_code fieldcode;
8004
8005           if (constructor_fields == 0)
8006             {
8007               pedwarn_init (input_location, 0,
8008                             "excess elements in struct initializer");
8009               break;
8010             }
8011
8012           fieldtype = TREE_TYPE (constructor_fields);
8013           if (fieldtype != error_mark_node)
8014             fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8015           fieldcode = TREE_CODE (fieldtype);
8016
8017           /* Error for non-static initialization of a flexible array member.  */
8018           if (fieldcode == ARRAY_TYPE
8019               && !require_constant_value
8020               && TYPE_SIZE (fieldtype) == NULL_TREE
8021               && DECL_CHAIN (constructor_fields) == NULL_TREE)
8022             {
8023               error_init ("non-static initialization of a flexible array member");
8024               break;
8025             }
8026
8027           /* Accept a string constant to initialize a subarray.  */
8028           if (value.value != 0
8029               && fieldcode == ARRAY_TYPE
8030               && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8031               && string_flag)
8032             value.value = orig_value;
8033           /* Otherwise, if we have come to a subaggregate,
8034              and we don't have an element of its type, push into it.  */
8035           else if (value.value != 0
8036                    && value.value != error_mark_node
8037                    && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8038                    && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8039                        || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8040             {
8041               push_init_level (1, braced_init_obstack);
8042               continue;
8043             }
8044
8045           if (value.value)
8046             {
8047               push_member_name (constructor_fields);
8048               output_init_element (value.value, value.original_type,
8049                                    strict_string, fieldtype,
8050                                    constructor_fields, 1, implicit,
8051                                    braced_init_obstack);
8052               RESTORE_SPELLING_DEPTH (constructor_depth);
8053             }
8054           else
8055             /* Do the bookkeeping for an element that was
8056                directly output as a constructor.  */
8057             {
8058               /* For a record, keep track of end position of last field.  */
8059               if (DECL_SIZE (constructor_fields))
8060                 constructor_bit_index
8061                   = size_binop_loc (input_location, PLUS_EXPR,
8062                                     bit_position (constructor_fields),
8063                                     DECL_SIZE (constructor_fields));
8064
8065               /* If the current field was the first one not yet written out,
8066                  it isn't now, so update.  */
8067               if (constructor_unfilled_fields == constructor_fields)
8068                 {
8069                   constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8070                   /* Skip any nameless bit fields.  */
8071                   while (constructor_unfilled_fields != 0
8072                          && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8073                          && DECL_NAME (constructor_unfilled_fields) == 0)
8074                     constructor_unfilled_fields =
8075                       DECL_CHAIN (constructor_unfilled_fields);
8076                 }
8077             }
8078
8079           constructor_fields = DECL_CHAIN (constructor_fields);
8080           /* Skip any nameless bit fields at the beginning.  */
8081           while (constructor_fields != 0
8082                  && DECL_C_BIT_FIELD (constructor_fields)
8083                  && DECL_NAME (constructor_fields) == 0)
8084             constructor_fields = DECL_CHAIN (constructor_fields);
8085         }
8086       else if (TREE_CODE (constructor_type) == UNION_TYPE)
8087         {
8088           tree fieldtype;
8089           enum tree_code fieldcode;
8090
8091           if (constructor_fields == 0)
8092             {
8093               pedwarn_init (input_location, 0,
8094                             "excess elements in union initializer");
8095               break;
8096             }
8097
8098           fieldtype = TREE_TYPE (constructor_fields);
8099           if (fieldtype != error_mark_node)
8100             fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8101           fieldcode = TREE_CODE (fieldtype);
8102
8103           /* Warn that traditional C rejects initialization of unions.
8104              We skip the warning if the value is zero.  This is done
8105              under the assumption that the zero initializer in user
8106              code appears conditioned on e.g. __STDC__ to avoid
8107              "missing initializer" warnings and relies on default
8108              initialization to zero in the traditional C case.
8109              We also skip the warning if the initializer is designated,
8110              again on the assumption that this must be conditional on
8111              __STDC__ anyway (and we've already complained about the
8112              member-designator already).  */
8113           if (!in_system_header && !constructor_designated
8114               && !(value.value && (integer_zerop (value.value)
8115                                    || real_zerop (value.value))))
8116             warning (OPT_Wtraditional, "traditional C rejects initialization "
8117                      "of unions");
8118
8119           /* Accept a string constant to initialize a subarray.  */
8120           if (value.value != 0
8121               && fieldcode == ARRAY_TYPE
8122               && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8123               && string_flag)
8124             value.value = orig_value;
8125           /* Otherwise, if we have come to a subaggregate,
8126              and we don't have an element of its type, push into it.  */
8127           else if (value.value != 0
8128                    && value.value != error_mark_node
8129                    && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8130                    && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8131                        || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8132             {
8133               push_init_level (1, braced_init_obstack);
8134               continue;
8135             }
8136
8137           if (value.value)
8138             {
8139               push_member_name (constructor_fields);
8140               output_init_element (value.value, value.original_type,
8141                                    strict_string, fieldtype,
8142                                    constructor_fields, 1, implicit,
8143                                    braced_init_obstack);
8144               RESTORE_SPELLING_DEPTH (constructor_depth);
8145             }
8146           else
8147             /* Do the bookkeeping for an element that was
8148                directly output as a constructor.  */
8149             {
8150               constructor_bit_index = DECL_SIZE (constructor_fields);
8151               constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8152             }
8153
8154           constructor_fields = 0;
8155         }
8156       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8157         {
8158           tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8159           enum tree_code eltcode = TREE_CODE (elttype);
8160
8161           /* Accept a string constant to initialize a subarray.  */
8162           if (value.value != 0
8163               && eltcode == ARRAY_TYPE
8164               && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8165               && string_flag)
8166             value.value = orig_value;
8167           /* Otherwise, if we have come to a subaggregate,
8168              and we don't have an element of its type, push into it.  */
8169           else if (value.value != 0
8170                    && value.value != error_mark_node
8171                    && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8172                    && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8173                        || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8174             {
8175               push_init_level (1, braced_init_obstack);
8176               continue;
8177             }
8178
8179           if (constructor_max_index != 0
8180               && (tree_int_cst_lt (constructor_max_index, constructor_index)
8181                   || integer_all_onesp (constructor_max_index)))
8182             {
8183               pedwarn_init (input_location, 0,
8184                             "excess elements in array initializer");
8185               break;
8186             }
8187
8188           /* Now output the actual element.  */
8189           if (value.value)
8190             {
8191               push_array_bounds (tree_low_cst (constructor_index, 1));
8192               output_init_element (value.value, value.original_type,
8193                                    strict_string, elttype,
8194                                    constructor_index, 1, implicit,
8195                                    braced_init_obstack);
8196               RESTORE_SPELLING_DEPTH (constructor_depth);
8197             }
8198
8199           constructor_index
8200             = size_binop_loc (input_location, PLUS_EXPR,
8201                               constructor_index, bitsize_one_node);
8202
8203           if (!value.value)
8204             /* If we are doing the bookkeeping for an element that was
8205                directly output as a constructor, we must update
8206                constructor_unfilled_index.  */
8207             constructor_unfilled_index = constructor_index;
8208         }
8209       else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8210         {
8211           tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8212
8213          /* Do a basic check of initializer size.  Note that vectors
8214             always have a fixed size derived from their type.  */
8215           if (tree_int_cst_lt (constructor_max_index, constructor_index))
8216             {
8217               pedwarn_init (input_location, 0,
8218                             "excess elements in vector initializer");
8219               break;
8220             }
8221
8222           /* Now output the actual element.  */
8223           if (value.value)
8224             {
8225               if (TREE_CODE (value.value) == VECTOR_CST)
8226                 elttype = TYPE_MAIN_VARIANT (constructor_type);
8227               output_init_element (value.value, value.original_type,
8228                                    strict_string, elttype,
8229                                    constructor_index, 1, implicit,
8230                                    braced_init_obstack);
8231             }
8232
8233           constructor_index
8234             = size_binop_loc (input_location,
8235                               PLUS_EXPR, constructor_index, bitsize_one_node);
8236
8237           if (!value.value)
8238             /* If we are doing the bookkeeping for an element that was
8239                directly output as a constructor, we must update
8240                constructor_unfilled_index.  */
8241             constructor_unfilled_index = constructor_index;
8242         }
8243
8244       /* Handle the sole element allowed in a braced initializer
8245          for a scalar variable.  */
8246       else if (constructor_type != error_mark_node
8247                && constructor_fields == 0)
8248         {
8249           pedwarn_init (input_location, 0,
8250                         "excess elements in scalar initializer");
8251           break;
8252         }
8253       else
8254         {
8255           if (value.value)
8256             output_init_element (value.value, value.original_type,
8257                                  strict_string, constructor_type,
8258                                  NULL_TREE, 1, implicit,
8259                                  braced_init_obstack);
8260           constructor_fields = 0;
8261         }
8262
8263       /* Handle range initializers either at this level or anywhere higher
8264          in the designator stack.  */
8265       if (constructor_range_stack)
8266         {
8267           struct constructor_range_stack *p, *range_stack;
8268           int finish = 0;
8269
8270           range_stack = constructor_range_stack;
8271           constructor_range_stack = 0;
8272           while (constructor_stack != range_stack->stack)
8273             {
8274               gcc_assert (constructor_stack->implicit);
8275               process_init_element (pop_init_level (1,
8276                                                     braced_init_obstack),
8277                                     true, braced_init_obstack);
8278             }
8279           for (p = range_stack;
8280                !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8281                p = p->prev)
8282             {
8283               gcc_assert (constructor_stack->implicit);
8284               process_init_element (pop_init_level (1, braced_init_obstack),
8285                                     true, braced_init_obstack);
8286             }
8287
8288           p->index = size_binop_loc (input_location,
8289                                      PLUS_EXPR, p->index, bitsize_one_node);
8290           if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8291             finish = 1;
8292
8293           while (1)
8294             {
8295               constructor_index = p->index;
8296               constructor_fields = p->fields;
8297               if (finish && p->range_end && p->index == p->range_start)
8298                 {
8299                   finish = 0;
8300                   p->prev = 0;
8301                 }
8302               p = p->next;
8303               if (!p)
8304                 break;
8305               push_init_level (2, braced_init_obstack);
8306               p->stack = constructor_stack;
8307               if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8308                 p->index = p->range_start;
8309             }
8310
8311           if (!finish)
8312             constructor_range_stack = range_stack;
8313           continue;
8314         }
8315
8316       break;
8317     }
8318
8319   constructor_range_stack = 0;
8320 }
8321 \f
8322 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8323    (guaranteed to be 'volatile' or null) and ARGS (represented using
8324    an ASM_EXPR node).  */
8325 tree
8326 build_asm_stmt (tree cv_qualifier, tree args)
8327 {
8328   if (!ASM_VOLATILE_P (args) && cv_qualifier)
8329     ASM_VOLATILE_P (args) = 1;
8330   return add_stmt (args);
8331 }
8332
8333 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8334    some INPUTS, and some CLOBBERS.  The latter three may be NULL.
8335    SIMPLE indicates whether there was anything at all after the
8336    string in the asm expression -- asm("blah") and asm("blah" : )
8337    are subtly different.  We use a ASM_EXPR node to represent this.  */
8338 tree
8339 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8340                 tree clobbers, tree labels, bool simple)
8341 {
8342   tree tail;
8343   tree args;
8344   int i;
8345   const char *constraint;
8346   const char **oconstraints;
8347   bool allows_mem, allows_reg, is_inout;
8348   int ninputs, noutputs;
8349
8350   ninputs = list_length (inputs);
8351   noutputs = list_length (outputs);
8352   oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8353
8354   string = resolve_asm_operand_names (string, outputs, inputs, labels);
8355
8356   /* Remove output conversions that change the type but not the mode.  */
8357   for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8358     {
8359       tree output = TREE_VALUE (tail);
8360
8361       /* ??? Really, this should not be here.  Users should be using a
8362          proper lvalue, dammit.  But there's a long history of using casts
8363          in the output operands.  In cases like longlong.h, this becomes a
8364          primitive form of typechecking -- if the cast can be removed, then
8365          the output operand had a type of the proper width; otherwise we'll
8366          get an error.  Gross, but ...  */
8367       STRIP_NOPS (output);
8368
8369       if (!lvalue_or_else (output, lv_asm))
8370         output = error_mark_node;
8371
8372       if (output != error_mark_node
8373           && (TREE_READONLY (output)
8374               || TYPE_READONLY (TREE_TYPE (output))
8375               || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8376                    || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8377                   && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8378         readonly_error (output, lv_asm);
8379
8380       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8381       oconstraints[i] = constraint;
8382
8383       if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8384                                    &allows_mem, &allows_reg, &is_inout))
8385         {
8386           /* If the operand is going to end up in memory,
8387              mark it addressable.  */
8388           if (!allows_reg && !c_mark_addressable (output))
8389             output = error_mark_node;
8390         }
8391       else
8392         output = error_mark_node;
8393
8394       TREE_VALUE (tail) = output;
8395     }
8396
8397   for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8398     {
8399       tree input;
8400
8401       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8402       input = TREE_VALUE (tail);
8403
8404       if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8405                                   oconstraints, &allows_mem, &allows_reg))
8406         {
8407           /* If the operand is going to end up in memory,
8408              mark it addressable.  */
8409           if (!allows_reg && allows_mem)
8410             {
8411               /* Strip the nops as we allow this case.  FIXME, this really
8412                  should be rejected or made deprecated.  */
8413               STRIP_NOPS (input);
8414               if (!c_mark_addressable (input))
8415                 input = error_mark_node;
8416           }
8417         }
8418       else
8419         input = error_mark_node;
8420
8421       TREE_VALUE (tail) = input;
8422     }
8423
8424   /* ASMs with labels cannot have outputs.  This should have been
8425      enforced by the parser.  */
8426   gcc_assert (outputs == NULL || labels == NULL);
8427
8428   args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8429
8430   /* asm statements without outputs, including simple ones, are treated
8431      as volatile.  */
8432   ASM_INPUT_P (args) = simple;
8433   ASM_VOLATILE_P (args) = (noutputs == 0);
8434
8435   return args;
8436 }
8437 \f
8438 /* Generate a goto statement to LABEL.  LOC is the location of the
8439    GOTO.  */
8440
8441 tree
8442 c_finish_goto_label (location_t loc, tree label)
8443 {
8444   tree decl = lookup_label_for_goto (loc, label);
8445   if (!decl)
8446     return NULL_TREE;
8447   TREE_USED (decl) = 1;
8448   {
8449     tree t = build1 (GOTO_EXPR, void_type_node, decl);
8450     SET_EXPR_LOCATION (t, loc);
8451     return add_stmt (t);
8452   }
8453 }
8454
8455 /* Generate a computed goto statement to EXPR.  LOC is the location of
8456    the GOTO.  */
8457
8458 tree
8459 c_finish_goto_ptr (location_t loc, tree expr)
8460 {
8461   tree t;
8462   pedwarn (loc, OPT_pedantic, "ISO C forbids %<goto *expr;%>");
8463   expr = c_fully_fold (expr, false, NULL);
8464   expr = convert (ptr_type_node, expr);
8465   t = build1 (GOTO_EXPR, void_type_node, expr);
8466   SET_EXPR_LOCATION (t, loc);
8467   return add_stmt (t);
8468 }
8469
8470 /* Generate a C `return' statement.  RETVAL is the expression for what
8471    to return, or a null pointer for `return;' with no value.  LOC is
8472    the location of the return statement.  If ORIGTYPE is not NULL_TREE, it
8473    is the original type of RETVAL.  */
8474
8475 tree
8476 c_finish_return (location_t loc, tree retval, tree origtype)
8477 {
8478   tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8479   bool no_warning = false;
8480   bool npc = false;
8481
8482   if (TREE_THIS_VOLATILE (current_function_decl))
8483     warning_at (loc, 0,
8484                 "function declared %<noreturn%> has a %<return%> statement");
8485
8486   if (retval)
8487     {
8488       tree semantic_type = NULL_TREE;
8489       npc = null_pointer_constant_p (retval);
8490       if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8491         {
8492           semantic_type = TREE_TYPE (retval);
8493           retval = TREE_OPERAND (retval, 0);
8494         }
8495       retval = c_fully_fold (retval, false, NULL);
8496       if (semantic_type)
8497         retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8498     }
8499
8500   if (!retval)
8501     {
8502       current_function_returns_null = 1;
8503       if ((warn_return_type || flag_isoc99)
8504           && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8505         {
8506           pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8507                        "%<return%> with no value, in "
8508                        "function returning non-void");
8509           no_warning = true;
8510         }
8511     }
8512   else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8513     {
8514       current_function_returns_null = 1;
8515       if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8516         pedwarn (loc, 0,
8517                  "%<return%> with a value, in function returning void");
8518       else
8519         pedwarn (loc, OPT_pedantic, "ISO C forbids "
8520                  "%<return%> with expression, in function returning void");
8521     }
8522   else
8523     {
8524       tree t = convert_for_assignment (loc, valtype, retval, origtype,
8525                                        ic_return,
8526                                        npc, NULL_TREE, NULL_TREE, 0);
8527       tree res = DECL_RESULT (current_function_decl);
8528       tree inner;
8529
8530       current_function_returns_value = 1;
8531       if (t == error_mark_node)
8532         return NULL_TREE;
8533
8534       inner = t = convert (TREE_TYPE (res), t);
8535
8536       /* Strip any conversions, additions, and subtractions, and see if
8537          we are returning the address of a local variable.  Warn if so.  */
8538       while (1)
8539         {
8540           switch (TREE_CODE (inner))
8541             {
8542             CASE_CONVERT:
8543             case NON_LVALUE_EXPR:
8544             case PLUS_EXPR:
8545             case POINTER_PLUS_EXPR:
8546               inner = TREE_OPERAND (inner, 0);
8547               continue;
8548
8549             case MINUS_EXPR:
8550               /* If the second operand of the MINUS_EXPR has a pointer
8551                  type (or is converted from it), this may be valid, so
8552                  don't give a warning.  */
8553               {
8554                 tree op1 = TREE_OPERAND (inner, 1);
8555
8556                 while (!POINTER_TYPE_P (TREE_TYPE (op1))
8557                        && (CONVERT_EXPR_P (op1)
8558                            || TREE_CODE (op1) == NON_LVALUE_EXPR))
8559                   op1 = TREE_OPERAND (op1, 0);
8560
8561                 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8562                   break;
8563
8564                 inner = TREE_OPERAND (inner, 0);
8565                 continue;
8566               }
8567
8568             case ADDR_EXPR:
8569               inner = TREE_OPERAND (inner, 0);
8570
8571               while (REFERENCE_CLASS_P (inner)
8572                      && TREE_CODE (inner) != INDIRECT_REF)
8573                 inner = TREE_OPERAND (inner, 0);
8574
8575               if (DECL_P (inner)
8576                   && !DECL_EXTERNAL (inner)
8577                   && !TREE_STATIC (inner)
8578                   && DECL_CONTEXT (inner) == current_function_decl)
8579                 warning_at (loc,
8580                             0, "function returns address of local variable");
8581               break;
8582
8583             default:
8584               break;
8585             }
8586
8587           break;
8588         }
8589
8590       retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8591       SET_EXPR_LOCATION (retval, loc);
8592
8593       if (warn_sequence_point)
8594         verify_sequence_points (retval);
8595     }
8596
8597   ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8598   TREE_NO_WARNING (ret_stmt) |= no_warning;
8599   return add_stmt (ret_stmt);
8600 }
8601 \f
8602 struct c_switch {
8603   /* The SWITCH_EXPR being built.  */
8604   tree switch_expr;
8605
8606   /* The original type of the testing expression, i.e. before the
8607      default conversion is applied.  */
8608   tree orig_type;
8609
8610   /* A splay-tree mapping the low element of a case range to the high
8611      element, or NULL_TREE if there is no high element.  Used to
8612      determine whether or not a new case label duplicates an old case
8613      label.  We need a tree, rather than simply a hash table, because
8614      of the GNU case range extension.  */
8615   splay_tree cases;
8616
8617   /* The bindings at the point of the switch.  This is used for
8618      warnings crossing decls when branching to a case label.  */
8619   struct c_spot_bindings *bindings;
8620
8621   /* The next node on the stack.  */
8622   struct c_switch *next;
8623 };
8624
8625 /* A stack of the currently active switch statements.  The innermost
8626    switch statement is on the top of the stack.  There is no need to
8627    mark the stack for garbage collection because it is only active
8628    during the processing of the body of a function, and we never
8629    collect at that point.  */
8630
8631 struct c_switch *c_switch_stack;
8632
8633 /* Start a C switch statement, testing expression EXP.  Return the new
8634    SWITCH_EXPR.  SWITCH_LOC is the location of the `switch'.
8635    SWITCH_COND_LOC is the location of the switch's condition.  */
8636
8637 tree
8638 c_start_case (location_t switch_loc,
8639               location_t switch_cond_loc,
8640               tree exp)
8641 {
8642   tree orig_type = error_mark_node;
8643   struct c_switch *cs;
8644
8645   if (exp != error_mark_node)
8646     {
8647       orig_type = TREE_TYPE (exp);
8648
8649       if (!INTEGRAL_TYPE_P (orig_type))
8650         {
8651           if (orig_type != error_mark_node)
8652             {
8653               error_at (switch_cond_loc, "switch quantity not an integer");
8654               orig_type = error_mark_node;
8655             }
8656           exp = integer_zero_node;
8657         }
8658       else
8659         {
8660           tree type = TYPE_MAIN_VARIANT (orig_type);
8661
8662           if (!in_system_header
8663               && (type == long_integer_type_node
8664                   || type == long_unsigned_type_node))
8665             warning_at (switch_cond_loc,
8666                         OPT_Wtraditional, "%<long%> switch expression not "
8667                         "converted to %<int%> in ISO C");
8668
8669           exp = c_fully_fold (exp, false, NULL);
8670           exp = default_conversion (exp);
8671
8672           if (warn_sequence_point)
8673             verify_sequence_points (exp);
8674         }
8675     }
8676
8677   /* Add this new SWITCH_EXPR to the stack.  */
8678   cs = XNEW (struct c_switch);
8679   cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8680   SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8681   cs->orig_type = orig_type;
8682   cs->cases = splay_tree_new (case_compare, NULL, NULL);
8683   cs->bindings = c_get_switch_bindings ();
8684   cs->next = c_switch_stack;
8685   c_switch_stack = cs;
8686
8687   return add_stmt (cs->switch_expr);
8688 }
8689
8690 /* Process a case label at location LOC.  */
8691
8692 tree
8693 do_case (location_t loc, tree low_value, tree high_value)
8694 {
8695   tree label = NULL_TREE;
8696
8697   if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8698     {
8699       low_value = c_fully_fold (low_value, false, NULL);
8700       if (TREE_CODE (low_value) == INTEGER_CST)
8701         pedwarn (input_location, OPT_pedantic,
8702                  "case label is not an integer constant expression");
8703     }
8704
8705   if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8706     {
8707       high_value = c_fully_fold (high_value, false, NULL);
8708       if (TREE_CODE (high_value) == INTEGER_CST)
8709         pedwarn (input_location, OPT_pedantic,
8710                  "case label is not an integer constant expression");
8711     }
8712
8713   if (c_switch_stack == NULL)
8714     {
8715       if (low_value)
8716         error_at (loc, "case label not within a switch statement");
8717       else
8718         error_at (loc, "%<default%> label not within a switch statement");
8719       return NULL_TREE;
8720     }
8721
8722   if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8723                                     EXPR_LOCATION (c_switch_stack->switch_expr),
8724                                     loc))
8725     return NULL_TREE;
8726
8727   label = c_add_case_label (loc, c_switch_stack->cases,
8728                             SWITCH_COND (c_switch_stack->switch_expr),
8729                             c_switch_stack->orig_type,
8730                             low_value, high_value);
8731   if (label == error_mark_node)
8732     label = NULL_TREE;
8733   return label;
8734 }
8735
8736 /* Finish the switch statement.  */
8737
8738 void
8739 c_finish_case (tree body)
8740 {
8741   struct c_switch *cs = c_switch_stack;
8742   location_t switch_location;
8743
8744   SWITCH_BODY (cs->switch_expr) = body;
8745
8746   /* Emit warnings as needed.  */
8747   switch_location = EXPR_LOCATION (cs->switch_expr);
8748   c_do_switch_warnings (cs->cases, switch_location,
8749                         TREE_TYPE (cs->switch_expr),
8750                         SWITCH_COND (cs->switch_expr));
8751
8752   /* Pop the stack.  */
8753   c_switch_stack = cs->next;
8754   splay_tree_delete (cs->cases);
8755   c_release_switch_bindings (cs->bindings);
8756   XDELETE (cs);
8757 }
8758 \f
8759 /* Emit an if statement.  IF_LOCUS is the location of the 'if'.  COND,
8760    THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8761    may be null.  NESTED_IF is true if THEN_BLOCK contains another IF
8762    statement, and was not surrounded with parenthesis.  */
8763
8764 void
8765 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8766                   tree else_block, bool nested_if)
8767 {
8768   tree stmt;
8769
8770   /* Diagnose an ambiguous else if if-then-else is nested inside if-then.  */
8771   if (warn_parentheses && nested_if && else_block == NULL)
8772     {
8773       tree inner_if = then_block;
8774
8775       /* We know from the grammar productions that there is an IF nested
8776          within THEN_BLOCK.  Due to labels and c99 conditional declarations,
8777          it might not be exactly THEN_BLOCK, but should be the last
8778          non-container statement within.  */
8779       while (1)
8780         switch (TREE_CODE (inner_if))
8781           {
8782           case COND_EXPR:
8783             goto found;
8784           case BIND_EXPR:
8785             inner_if = BIND_EXPR_BODY (inner_if);
8786             break;
8787           case STATEMENT_LIST:
8788             inner_if = expr_last (then_block);
8789             break;
8790           case TRY_FINALLY_EXPR:
8791           case TRY_CATCH_EXPR:
8792             inner_if = TREE_OPERAND (inner_if, 0);
8793             break;
8794           default:
8795             gcc_unreachable ();
8796           }
8797     found:
8798
8799       if (COND_EXPR_ELSE (inner_if))
8800          warning_at (if_locus, OPT_Wparentheses,
8801                      "suggest explicit braces to avoid ambiguous %<else%>");
8802     }
8803
8804   stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
8805   SET_EXPR_LOCATION (stmt, if_locus);
8806   add_stmt (stmt);
8807 }
8808
8809 /* Emit a general-purpose loop construct.  START_LOCUS is the location of
8810    the beginning of the loop.  COND is the loop condition.  COND_IS_FIRST
8811    is false for DO loops.  INCR is the FOR increment expression.  BODY is
8812    the statement controlled by the loop.  BLAB is the break label.  CLAB is
8813    the continue label.  Everything is allowed to be NULL.  */
8814
8815 void
8816 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
8817                tree blab, tree clab, bool cond_is_first)
8818 {
8819   tree entry = NULL, exit = NULL, t;
8820
8821   /* If the condition is zero don't generate a loop construct.  */
8822   if (cond && integer_zerop (cond))
8823     {
8824       if (cond_is_first)
8825         {
8826           t = build_and_jump (&blab);
8827           SET_EXPR_LOCATION (t, start_locus);
8828           add_stmt (t);
8829         }
8830     }
8831   else
8832     {
8833       tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8834
8835       /* If we have an exit condition, then we build an IF with gotos either
8836          out of the loop, or to the top of it.  If there's no exit condition,
8837          then we just build a jump back to the top.  */
8838       exit = build_and_jump (&LABEL_EXPR_LABEL (top));
8839
8840       if (cond && !integer_nonzerop (cond))
8841         {
8842           /* Canonicalize the loop condition to the end.  This means
8843              generating a branch to the loop condition.  Reuse the
8844              continue label, if possible.  */
8845           if (cond_is_first)
8846             {
8847               if (incr || !clab)
8848                 {
8849                   entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
8850                   t = build_and_jump (&LABEL_EXPR_LABEL (entry));
8851                 }
8852               else
8853                 t = build1 (GOTO_EXPR, void_type_node, clab);
8854               SET_EXPR_LOCATION (t, start_locus);
8855               add_stmt (t);
8856             }
8857
8858           t = build_and_jump (&blab);
8859           if (cond_is_first)
8860             exit = fold_build3_loc (start_locus,
8861                                 COND_EXPR, void_type_node, cond, exit, t);
8862           else
8863             exit = fold_build3_loc (input_location,
8864                                 COND_EXPR, void_type_node, cond, exit, t);
8865         }
8866
8867       add_stmt (top);
8868     }
8869
8870   if (body)
8871     add_stmt (body);
8872   if (clab)
8873     add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
8874   if (incr)
8875     add_stmt (incr);
8876   if (entry)
8877     add_stmt (entry);
8878   if (exit)
8879     add_stmt (exit);
8880   if (blab)
8881     add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
8882 }
8883
8884 tree
8885 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
8886 {
8887   bool skip;
8888   tree label = *label_p;
8889
8890   /* In switch statements break is sometimes stylistically used after
8891      a return statement.  This can lead to spurious warnings about
8892      control reaching the end of a non-void function when it is
8893      inlined.  Note that we are calling block_may_fallthru with
8894      language specific tree nodes; this works because
8895      block_may_fallthru returns true when given something it does not
8896      understand.  */
8897   skip = !block_may_fallthru (cur_stmt_list);
8898
8899   if (!label)
8900     {
8901       if (!skip)
8902         *label_p = label = create_artificial_label (loc);
8903     }
8904   else if (TREE_CODE (label) == LABEL_DECL)
8905     ;
8906   else switch (TREE_INT_CST_LOW (label))
8907     {
8908     case 0:
8909       if (is_break)
8910         error_at (loc, "break statement not within loop or switch");
8911       else
8912         error_at (loc, "continue statement not within a loop");
8913       return NULL_TREE;
8914
8915     case 1:
8916       gcc_assert (is_break);
8917       error_at (loc, "break statement used with OpenMP for loop");
8918       return NULL_TREE;
8919
8920     default:
8921       gcc_unreachable ();
8922     }
8923
8924   if (skip)
8925     return NULL_TREE;
8926
8927   if (!is_break)
8928     add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
8929
8930   return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
8931 }
8932
8933 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr.  */
8934
8935 static void
8936 emit_side_effect_warnings (location_t loc, tree expr)
8937 {
8938   if (expr == error_mark_node)
8939     ;
8940   else if (!TREE_SIDE_EFFECTS (expr))
8941     {
8942       if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
8943         warning_at (loc, OPT_Wunused_value, "statement with no effect");
8944     }
8945   else
8946     warn_if_unused_value (expr, loc);
8947 }
8948
8949 /* Process an expression as if it were a complete statement.  Emit
8950    diagnostics, but do not call ADD_STMT.  LOC is the location of the
8951    statement.  */
8952
8953 tree
8954 c_process_expr_stmt (location_t loc, tree expr)
8955 {
8956   tree exprv;
8957
8958   if (!expr)
8959     return NULL_TREE;
8960
8961   expr = c_fully_fold (expr, false, NULL);
8962
8963   if (warn_sequence_point)
8964     verify_sequence_points (expr);
8965
8966   if (TREE_TYPE (expr) != error_mark_node
8967       && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
8968       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
8969     error_at (loc, "expression statement has incomplete type");
8970
8971   /* If we're not processing a statement expression, warn about unused values.
8972      Warnings for statement expressions will be emitted later, once we figure
8973      out which is the result.  */
8974   if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
8975       && warn_unused_value)
8976     emit_side_effect_warnings (loc, expr);
8977
8978   exprv = expr;
8979   while (TREE_CODE (exprv) == COMPOUND_EXPR)
8980     exprv = TREE_OPERAND (exprv, 1);
8981   if (DECL_P (exprv) || handled_component_p (exprv))
8982     mark_exp_read (exprv);
8983
8984   /* If the expression is not of a type to which we cannot assign a line
8985      number, wrap the thing in a no-op NOP_EXPR.  */
8986   if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
8987     {
8988       expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
8989       SET_EXPR_LOCATION (expr, loc);
8990     }
8991
8992   return expr;
8993 }
8994
8995 /* Emit an expression as a statement.  LOC is the location of the
8996    expression.  */
8997
8998 tree
8999 c_finish_expr_stmt (location_t loc, tree expr)
9000 {
9001   if (expr)
9002     return add_stmt (c_process_expr_stmt (loc, expr));
9003   else
9004     return NULL;
9005 }
9006
9007 /* Do the opposite and emit a statement as an expression.  To begin,
9008    create a new binding level and return it.  */
9009
9010 tree
9011 c_begin_stmt_expr (void)
9012 {
9013   tree ret;
9014
9015   /* We must force a BLOCK for this level so that, if it is not expanded
9016      later, there is a way to turn off the entire subtree of blocks that
9017      are contained in it.  */
9018   keep_next_level ();
9019   ret = c_begin_compound_stmt (true);
9020
9021   c_bindings_start_stmt_expr (c_switch_stack == NULL
9022                               ? NULL
9023                               : c_switch_stack->bindings);
9024
9025   /* Mark the current statement list as belonging to a statement list.  */
9026   STATEMENT_LIST_STMT_EXPR (ret) = 1;
9027
9028   return ret;
9029 }
9030
9031 /* LOC is the location of the compound statement to which this body
9032    belongs.  */
9033
9034 tree
9035 c_finish_stmt_expr (location_t loc, tree body)
9036 {
9037   tree last, type, tmp, val;
9038   tree *last_p;
9039
9040   body = c_end_compound_stmt (loc, body, true);
9041
9042   c_bindings_end_stmt_expr (c_switch_stack == NULL
9043                             ? NULL
9044                             : c_switch_stack->bindings);
9045
9046   /* Locate the last statement in BODY.  See c_end_compound_stmt
9047      about always returning a BIND_EXPR.  */
9048   last_p = &BIND_EXPR_BODY (body);
9049   last = BIND_EXPR_BODY (body);
9050
9051  continue_searching:
9052   if (TREE_CODE (last) == STATEMENT_LIST)
9053     {
9054       tree_stmt_iterator i;
9055
9056       /* This can happen with degenerate cases like ({ }).  No value.  */
9057       if (!TREE_SIDE_EFFECTS (last))
9058         return body;
9059
9060       /* If we're supposed to generate side effects warnings, process
9061          all of the statements except the last.  */
9062       if (warn_unused_value)
9063         {
9064           for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9065             {
9066               location_t tloc;
9067               tree t = tsi_stmt (i);
9068
9069               tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9070               emit_side_effect_warnings (tloc, t);
9071             }
9072         }
9073       else
9074         i = tsi_last (last);
9075       last_p = tsi_stmt_ptr (i);
9076       last = *last_p;
9077     }
9078
9079   /* If the end of the list is exception related, then the list was split
9080      by a call to push_cleanup.  Continue searching.  */
9081   if (TREE_CODE (last) == TRY_FINALLY_EXPR
9082       || TREE_CODE (last) == TRY_CATCH_EXPR)
9083     {
9084       last_p = &TREE_OPERAND (last, 0);
9085       last = *last_p;
9086       goto continue_searching;
9087     }
9088
9089   if (last == error_mark_node)
9090     return last;
9091
9092   /* In the case that the BIND_EXPR is not necessary, return the
9093      expression out from inside it.  */
9094   if (last == BIND_EXPR_BODY (body)
9095       && BIND_EXPR_VARS (body) == NULL)
9096     {
9097       /* Even if this looks constant, do not allow it in a constant
9098          expression.  */
9099       last = c_wrap_maybe_const (last, true);
9100       /* Do not warn if the return value of a statement expression is
9101          unused.  */
9102       TREE_NO_WARNING (last) = 1;
9103       return last;
9104     }
9105
9106   /* Extract the type of said expression.  */
9107   type = TREE_TYPE (last);
9108
9109   /* If we're not returning a value at all, then the BIND_EXPR that
9110      we already have is a fine expression to return.  */
9111   if (!type || VOID_TYPE_P (type))
9112     return body;
9113
9114   /* Now that we've located the expression containing the value, it seems
9115      silly to make voidify_wrapper_expr repeat the process.  Create a
9116      temporary of the appropriate type and stick it in a TARGET_EXPR.  */
9117   tmp = create_tmp_var_raw (type, NULL);
9118
9119   /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt.  This avoids
9120      tree_expr_nonnegative_p giving up immediately.  */
9121   val = last;
9122   if (TREE_CODE (val) == NOP_EXPR
9123       && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9124     val = TREE_OPERAND (val, 0);
9125
9126   *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9127   SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9128
9129   {
9130     tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9131     SET_EXPR_LOCATION (t, loc);
9132     return t;
9133   }
9134 }
9135 \f
9136 /* Begin and end compound statements.  This is as simple as pushing
9137    and popping new statement lists from the tree.  */
9138
9139 tree
9140 c_begin_compound_stmt (bool do_scope)
9141 {
9142   tree stmt = push_stmt_list ();
9143   if (do_scope)
9144     push_scope ();
9145   return stmt;
9146 }
9147
9148 /* End a compound statement.  STMT is the statement.  LOC is the
9149    location of the compound statement-- this is usually the location
9150    of the opening brace.  */
9151
9152 tree
9153 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9154 {
9155   tree block = NULL;
9156
9157   if (do_scope)
9158     {
9159       if (c_dialect_objc ())
9160         objc_clear_super_receiver ();
9161       block = pop_scope ();
9162     }
9163
9164   stmt = pop_stmt_list (stmt);
9165   stmt = c_build_bind_expr (loc, block, stmt);
9166
9167   /* If this compound statement is nested immediately inside a statement
9168      expression, then force a BIND_EXPR to be created.  Otherwise we'll
9169      do the wrong thing for ({ { 1; } }) or ({ 1; { } }).  In particular,
9170      STATEMENT_LISTs merge, and thus we can lose track of what statement
9171      was really last.  */
9172   if (cur_stmt_list
9173       && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9174       && TREE_CODE (stmt) != BIND_EXPR)
9175     {
9176       stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9177       TREE_SIDE_EFFECTS (stmt) = 1;
9178       SET_EXPR_LOCATION (stmt, loc);
9179     }
9180
9181   return stmt;
9182 }
9183
9184 /* Queue a cleanup.  CLEANUP is an expression/statement to be executed
9185    when the current scope is exited.  EH_ONLY is true when this is not
9186    meant to apply to normal control flow transfer.  */
9187
9188 void
9189 push_cleanup (tree decl, tree cleanup, bool eh_only)
9190 {
9191   enum tree_code code;
9192   tree stmt, list;
9193   bool stmt_expr;
9194
9195   code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9196   stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9197   add_stmt (stmt);
9198   stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9199   list = push_stmt_list ();
9200   TREE_OPERAND (stmt, 0) = list;
9201   STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9202 }
9203 \f
9204 /* Build a binary-operation expression without default conversions.
9205    CODE is the kind of expression to build.
9206    LOCATION is the operator's location.
9207    This function differs from `build' in several ways:
9208    the data type of the result is computed and recorded in it,
9209    warnings are generated if arg data types are invalid,
9210    special handling for addition and subtraction of pointers is known,
9211    and some optimization is done (operations on narrow ints
9212    are done in the narrower type when that gives the same result).
9213    Constant folding is also done before the result is returned.
9214
9215    Note that the operands will never have enumeral types, or function
9216    or array types, because either they will have the default conversions
9217    performed or they have both just been converted to some other type in which
9218    the arithmetic is to be done.  */
9219
9220 tree
9221 build_binary_op (location_t location, enum tree_code code,
9222                  tree orig_op0, tree orig_op1, int convert_p)
9223 {
9224   tree type0, type1, orig_type0, orig_type1;
9225   tree eptype;
9226   enum tree_code code0, code1;
9227   tree op0, op1;
9228   tree ret = error_mark_node;
9229   const char *invalid_op_diag;
9230   bool op0_int_operands, op1_int_operands;
9231   bool int_const, int_const_or_overflow, int_operands;
9232
9233   /* Expression code to give to the expression when it is built.
9234      Normally this is CODE, which is what the caller asked for,
9235      but in some special cases we change it.  */
9236   enum tree_code resultcode = code;
9237
9238   /* Data type in which the computation is to be performed.
9239      In the simplest cases this is the common type of the arguments.  */
9240   tree result_type = NULL;
9241
9242   /* When the computation is in excess precision, the type of the
9243      final EXCESS_PRECISION_EXPR.  */
9244   tree semantic_result_type = NULL;
9245
9246   /* Nonzero means operands have already been type-converted
9247      in whatever way is necessary.
9248      Zero means they need to be converted to RESULT_TYPE.  */
9249   int converted = 0;
9250
9251   /* Nonzero means create the expression with this type, rather than
9252      RESULT_TYPE.  */
9253   tree build_type = 0;
9254
9255   /* Nonzero means after finally constructing the expression
9256      convert it to this type.  */
9257   tree final_type = 0;
9258
9259   /* Nonzero if this is an operation like MIN or MAX which can
9260      safely be computed in short if both args are promoted shorts.
9261      Also implies COMMON.
9262      -1 indicates a bitwise operation; this makes a difference
9263      in the exact conditions for when it is safe to do the operation
9264      in a narrower mode.  */
9265   int shorten = 0;
9266
9267   /* Nonzero if this is a comparison operation;
9268      if both args are promoted shorts, compare the original shorts.
9269      Also implies COMMON.  */
9270   int short_compare = 0;
9271
9272   /* Nonzero if this is a right-shift operation, which can be computed on the
9273      original short and then promoted if the operand is a promoted short.  */
9274   int short_shift = 0;
9275
9276   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
9277   int common = 0;
9278
9279   /* True means types are compatible as far as ObjC is concerned.  */
9280   bool objc_ok;
9281
9282   /* True means this is an arithmetic operation that may need excess
9283      precision.  */
9284   bool may_need_excess_precision;
9285
9286   if (location == UNKNOWN_LOCATION)
9287     location = input_location;
9288
9289   op0 = orig_op0;
9290   op1 = orig_op1;
9291
9292   op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9293   if (op0_int_operands)
9294     op0 = remove_c_maybe_const_expr (op0);
9295   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9296   if (op1_int_operands)
9297     op1 = remove_c_maybe_const_expr (op1);
9298   int_operands = (op0_int_operands && op1_int_operands);
9299   if (int_operands)
9300     {
9301       int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9302                                && TREE_CODE (orig_op1) == INTEGER_CST);
9303       int_const = (int_const_or_overflow
9304                    && !TREE_OVERFLOW (orig_op0)
9305                    && !TREE_OVERFLOW (orig_op1));
9306     }
9307   else
9308     int_const = int_const_or_overflow = false;
9309
9310   if (convert_p)
9311     {
9312       op0 = default_conversion (op0);
9313       op1 = default_conversion (op1);
9314     }
9315
9316   orig_type0 = type0 = TREE_TYPE (op0);
9317   orig_type1 = type1 = TREE_TYPE (op1);
9318
9319   /* The expression codes of the data types of the arguments tell us
9320      whether the arguments are integers, floating, pointers, etc.  */
9321   code0 = TREE_CODE (type0);
9322   code1 = TREE_CODE (type1);
9323
9324   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
9325   STRIP_TYPE_NOPS (op0);
9326   STRIP_TYPE_NOPS (op1);
9327
9328   /* If an error was already reported for one of the arguments,
9329      avoid reporting another error.  */
9330
9331   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9332     return error_mark_node;
9333
9334   if ((invalid_op_diag
9335        = targetm.invalid_binary_op (code, type0, type1)))
9336     {
9337       error_at (location, invalid_op_diag);
9338       return error_mark_node;
9339     }
9340
9341   switch (code)
9342     {
9343     case PLUS_EXPR:
9344     case MINUS_EXPR:
9345     case MULT_EXPR:
9346     case TRUNC_DIV_EXPR:
9347     case CEIL_DIV_EXPR:
9348     case FLOOR_DIV_EXPR:
9349     case ROUND_DIV_EXPR:
9350     case EXACT_DIV_EXPR:
9351       may_need_excess_precision = true;
9352       break;
9353     default:
9354       may_need_excess_precision = false;
9355       break;
9356     }
9357   if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9358     {
9359       op0 = TREE_OPERAND (op0, 0);
9360       type0 = TREE_TYPE (op0);
9361     }
9362   else if (may_need_excess_precision
9363            && (eptype = excess_precision_type (type0)) != NULL_TREE)
9364     {
9365       type0 = eptype;
9366       op0 = convert (eptype, op0);
9367     }
9368   if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9369     {
9370       op1 = TREE_OPERAND (op1, 0);
9371       type1 = TREE_TYPE (op1);
9372     }
9373   else if (may_need_excess_precision
9374            && (eptype = excess_precision_type (type1)) != NULL_TREE)
9375     {
9376       type1 = eptype;
9377       op1 = convert (eptype, op1);
9378     }
9379
9380   objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9381
9382   switch (code)
9383     {
9384     case PLUS_EXPR:
9385       /* Handle the pointer + int case.  */
9386       if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9387         {
9388           ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9389           goto return_build_binary_op;
9390         }
9391       else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9392         {
9393           ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9394           goto return_build_binary_op;
9395         }
9396       else
9397         common = 1;
9398       break;
9399
9400     case MINUS_EXPR:
9401       /* Subtraction of two similar pointers.
9402          We must subtract them as integers, then divide by object size.  */
9403       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9404           && comp_target_types (location, type0, type1))
9405         {
9406           ret = pointer_diff (location, op0, op1);
9407           goto return_build_binary_op;
9408         }
9409       /* Handle pointer minus int.  Just like pointer plus int.  */
9410       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9411         {
9412           ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9413           goto return_build_binary_op;
9414         }
9415       else
9416         common = 1;
9417       break;
9418
9419     case MULT_EXPR:
9420       common = 1;
9421       break;
9422
9423     case TRUNC_DIV_EXPR:
9424     case CEIL_DIV_EXPR:
9425     case FLOOR_DIV_EXPR:
9426     case ROUND_DIV_EXPR:
9427     case EXACT_DIV_EXPR:
9428       warn_for_div_by_zero (location, op1);
9429
9430       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9431            || code0 == FIXED_POINT_TYPE
9432            || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9433           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9434               || code1 == FIXED_POINT_TYPE
9435               || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9436         {
9437           enum tree_code tcode0 = code0, tcode1 = code1;
9438
9439           if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9440             tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9441           if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9442             tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9443
9444           if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9445               || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9446             resultcode = RDIV_EXPR;
9447           else
9448             /* Although it would be tempting to shorten always here, that
9449                loses on some targets, since the modulo instruction is
9450                undefined if the quotient can't be represented in the
9451                computation mode.  We shorten only if unsigned or if
9452                dividing by something we know != -1.  */
9453             shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9454                        || (TREE_CODE (op1) == INTEGER_CST
9455                            && !integer_all_onesp (op1)));
9456           common = 1;
9457         }
9458       break;
9459
9460     case BIT_AND_EXPR:
9461     case BIT_IOR_EXPR:
9462     case BIT_XOR_EXPR:
9463       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9464         shorten = -1;
9465       /* Allow vector types which are not floating point types.   */
9466       else if (code0 == VECTOR_TYPE
9467                && code1 == VECTOR_TYPE
9468                && !VECTOR_FLOAT_TYPE_P (type0)
9469                && !VECTOR_FLOAT_TYPE_P (type1))
9470         common = 1;
9471       break;
9472
9473     case TRUNC_MOD_EXPR:
9474     case FLOOR_MOD_EXPR:
9475       warn_for_div_by_zero (location, op1);
9476
9477       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9478           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9479           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9480         common = 1;
9481       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9482         {
9483           /* Although it would be tempting to shorten always here, that loses
9484              on some targets, since the modulo instruction is undefined if the
9485              quotient can't be represented in the computation mode.  We shorten
9486              only if unsigned or if dividing by something we know != -1.  */
9487           shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9488                      || (TREE_CODE (op1) == INTEGER_CST
9489                          && !integer_all_onesp (op1)));
9490           common = 1;
9491         }
9492       break;
9493
9494     case TRUTH_ANDIF_EXPR:
9495     case TRUTH_ORIF_EXPR:
9496     case TRUTH_AND_EXPR:
9497     case TRUTH_OR_EXPR:
9498     case TRUTH_XOR_EXPR:
9499       if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9500            || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9501            || code0 == FIXED_POINT_TYPE)
9502           && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9503               || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9504               || code1 == FIXED_POINT_TYPE))
9505         {
9506           /* Result of these operations is always an int,
9507              but that does not mean the operands should be
9508              converted to ints!  */
9509           result_type = integer_type_node;
9510           op0 = c_common_truthvalue_conversion (location, op0);
9511           op1 = c_common_truthvalue_conversion (location, op1);
9512           converted = 1;
9513         }
9514       if (code == TRUTH_ANDIF_EXPR)
9515         {
9516           int_const_or_overflow = (int_operands
9517                                    && TREE_CODE (orig_op0) == INTEGER_CST
9518                                    && (op0 == truthvalue_false_node
9519                                        || TREE_CODE (orig_op1) == INTEGER_CST));
9520           int_const = (int_const_or_overflow
9521                        && !TREE_OVERFLOW (orig_op0)
9522                        && (op0 == truthvalue_false_node
9523                            || !TREE_OVERFLOW (orig_op1)));
9524         }
9525       else if (code == TRUTH_ORIF_EXPR)
9526         {
9527           int_const_or_overflow = (int_operands
9528                                    && TREE_CODE (orig_op0) == INTEGER_CST
9529                                    && (op0 == truthvalue_true_node
9530                                        || TREE_CODE (orig_op1) == INTEGER_CST));
9531           int_const = (int_const_or_overflow
9532                        && !TREE_OVERFLOW (orig_op0)
9533                        && (op0 == truthvalue_true_node
9534                            || !TREE_OVERFLOW (orig_op1)));
9535         }
9536       break;
9537
9538       /* Shift operations: result has same type as first operand;
9539          always convert second operand to int.
9540          Also set SHORT_SHIFT if shifting rightward.  */
9541
9542     case RSHIFT_EXPR:
9543       if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9544           && code1 == INTEGER_TYPE)
9545         {
9546           if (TREE_CODE (op1) == INTEGER_CST)
9547             {
9548               if (tree_int_cst_sgn (op1) < 0)
9549                 {
9550                   int_const = false;
9551                   if (c_inhibit_evaluation_warnings == 0)
9552                     warning (0, "right shift count is negative");
9553                 }
9554               else
9555                 {
9556                   if (!integer_zerop (op1))
9557                     short_shift = 1;
9558
9559                   if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9560                     {
9561                       int_const = false;
9562                       if (c_inhibit_evaluation_warnings == 0)
9563                         warning (0, "right shift count >= width of type");
9564                     }
9565                 }
9566             }
9567
9568           /* Use the type of the value to be shifted.  */
9569           result_type = type0;
9570           /* Convert the shift-count to an integer, regardless of size
9571              of value being shifted.  */
9572           if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9573             op1 = convert (integer_type_node, op1);
9574           /* Avoid converting op1 to result_type later.  */
9575           converted = 1;
9576         }
9577       break;
9578
9579     case LSHIFT_EXPR:
9580       if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9581           && code1 == INTEGER_TYPE)
9582         {
9583           if (TREE_CODE (op1) == INTEGER_CST)
9584             {
9585               if (tree_int_cst_sgn (op1) < 0)
9586                 {
9587                   int_const = false;
9588                   if (c_inhibit_evaluation_warnings == 0)
9589                     warning (0, "left shift count is negative");
9590                 }
9591
9592               else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9593                 {
9594                   int_const = false;
9595                   if (c_inhibit_evaluation_warnings == 0)
9596                     warning (0, "left shift count >= width of type");
9597                 }
9598             }
9599
9600           /* Use the type of the value to be shifted.  */
9601           result_type = type0;
9602           /* Convert the shift-count to an integer, regardless of size
9603              of value being shifted.  */
9604           if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9605             op1 = convert (integer_type_node, op1);
9606           /* Avoid converting op1 to result_type later.  */
9607           converted = 1;
9608         }
9609       break;
9610
9611     case EQ_EXPR:
9612     case NE_EXPR:
9613       if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9614         warning_at (location,
9615                     OPT_Wfloat_equal,
9616                     "comparing floating point with == or != is unsafe");
9617       /* Result of comparison is always int,
9618          but don't convert the args to int!  */
9619       build_type = integer_type_node;
9620       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9621            || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9622           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9623               || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9624         short_compare = 1;
9625       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9626         {
9627           if (TREE_CODE (op0) == ADDR_EXPR
9628               && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9629             {
9630               if (code == EQ_EXPR)
9631                 warning_at (location,
9632                             OPT_Waddress,
9633                             "the comparison will always evaluate as %<false%> "
9634                             "for the address of %qD will never be NULL",
9635                             TREE_OPERAND (op0, 0));
9636               else
9637                 warning_at (location,
9638                             OPT_Waddress,
9639                             "the comparison will always evaluate as %<true%> "
9640                             "for the address of %qD will never be NULL",
9641                             TREE_OPERAND (op0, 0));
9642             }
9643           result_type = type0;
9644         }
9645       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9646         {
9647           if (TREE_CODE (op1) == ADDR_EXPR
9648               && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9649             {
9650               if (code == EQ_EXPR)
9651                 warning_at (location,
9652                             OPT_Waddress, 
9653                             "the comparison will always evaluate as %<false%> "
9654                             "for the address of %qD will never be NULL",
9655                             TREE_OPERAND (op1, 0));
9656               else
9657                 warning_at (location,
9658                             OPT_Waddress,
9659                             "the comparison will always evaluate as %<true%> "
9660                             "for the address of %qD will never be NULL",
9661                             TREE_OPERAND (op1, 0));
9662             }
9663           result_type = type1;
9664         }
9665       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9666         {
9667           tree tt0 = TREE_TYPE (type0);
9668           tree tt1 = TREE_TYPE (type1);
9669           addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
9670           addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
9671           addr_space_t as_common = ADDR_SPACE_GENERIC;
9672
9673           /* Anything compares with void *.  void * compares with anything.
9674              Otherwise, the targets must be compatible
9675              and both must be object or both incomplete.  */
9676           if (comp_target_types (location, type0, type1))
9677             result_type = common_pointer_type (type0, type1);
9678           else if (!addr_space_superset (as0, as1, &as_common))
9679             {
9680               error_at (location, "comparison of pointers to "
9681                         "disjoint address spaces");
9682               return error_mark_node;
9683             }
9684           else if (VOID_TYPE_P (tt0))
9685             {
9686               if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
9687                 pedwarn (location, OPT_pedantic, "ISO C forbids "
9688                          "comparison of %<void *%> with function pointer");
9689             }
9690           else if (VOID_TYPE_P (tt1))
9691             {
9692               if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
9693                 pedwarn (location, OPT_pedantic, "ISO C forbids "
9694                          "comparison of %<void *%> with function pointer");
9695             }
9696           else
9697             /* Avoid warning about the volatile ObjC EH puts on decls.  */
9698             if (!objc_ok)
9699               pedwarn (location, 0,
9700                        "comparison of distinct pointer types lacks a cast");
9701
9702           if (result_type == NULL_TREE)
9703             {
9704               int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9705               result_type = build_pointer_type
9706                               (build_qualified_type (void_type_node, qual));
9707             }
9708         }
9709       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9710         {
9711           result_type = type0;
9712           pedwarn (location, 0, "comparison between pointer and integer");
9713         }
9714       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9715         {
9716           result_type = type1;
9717           pedwarn (location, 0, "comparison between pointer and integer");
9718         }
9719       break;
9720
9721     case LE_EXPR:
9722     case GE_EXPR:
9723     case LT_EXPR:
9724     case GT_EXPR:
9725       build_type = integer_type_node;
9726       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9727            || code0 == FIXED_POINT_TYPE)
9728           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9729               || code1 == FIXED_POINT_TYPE))
9730         short_compare = 1;
9731       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9732         {
9733           addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
9734           addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
9735           addr_space_t as_common;
9736
9737           if (comp_target_types (location, type0, type1))
9738             {
9739               result_type = common_pointer_type (type0, type1);
9740               if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
9741                   != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
9742                 pedwarn (location, 0,
9743                          "comparison of complete and incomplete pointers");
9744               else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
9745                 pedwarn (location, OPT_pedantic, "ISO C forbids "
9746                          "ordered comparisons of pointers to functions");
9747               else if (null_pointer_constant_p (orig_op0)
9748                        || null_pointer_constant_p (orig_op1))
9749                 warning_at (location, OPT_Wextra,
9750                             "ordered comparison of pointer with null pointer");
9751
9752             }
9753           else if (!addr_space_superset (as0, as1, &as_common))
9754             {
9755               error_at (location, "comparison of pointers to "
9756                         "disjoint address spaces");
9757               return error_mark_node;
9758             }
9759           else
9760             {
9761               int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
9762               result_type = build_pointer_type
9763                               (build_qualified_type (void_type_node, qual));
9764               pedwarn (location, 0,
9765                        "comparison of distinct pointer types lacks a cast");
9766             }
9767         }
9768       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9769         {
9770           result_type = type0;
9771           if (pedantic)
9772             pedwarn (location, OPT_pedantic,
9773                      "ordered comparison of pointer with integer zero");
9774           else if (extra_warnings)
9775             warning_at (location, OPT_Wextra,
9776                         "ordered comparison of pointer with integer zero");
9777         }
9778       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9779         {
9780           result_type = type1;
9781           if (pedantic)
9782             pedwarn (location, OPT_pedantic,
9783                      "ordered comparison of pointer with integer zero");
9784           else if (extra_warnings)
9785             warning_at (location, OPT_Wextra,
9786                         "ordered comparison of pointer with integer zero");
9787         }
9788       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9789         {
9790           result_type = type0;
9791           pedwarn (location, 0, "comparison between pointer and integer");
9792         }
9793       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
9794         {
9795           result_type = type1;
9796           pedwarn (location, 0, "comparison between pointer and integer");
9797         }
9798       break;
9799
9800     default:
9801       gcc_unreachable ();
9802     }
9803
9804   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9805     return error_mark_node;
9806
9807   if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9808       && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
9809           || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
9810                                                     TREE_TYPE (type1))))
9811     {
9812       binary_op_error (location, code, type0, type1);
9813       return error_mark_node;
9814     }
9815
9816   if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9817        || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
9818       &&
9819       (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9820        || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
9821     {
9822       bool first_complex = (code0 == COMPLEX_TYPE);
9823       bool second_complex = (code1 == COMPLEX_TYPE);
9824       int none_complex = (!first_complex && !second_complex);
9825
9826       if (shorten || common || short_compare)
9827         {
9828           result_type = c_common_type (type0, type1);
9829           do_warn_double_promotion (result_type, type0, type1,
9830                                     "implicit conversion from %qT to %qT "
9831                                     "to match other operand of binary "
9832                                     "expression",
9833                                     location);
9834           if (result_type == error_mark_node)
9835             return error_mark_node;
9836         }
9837
9838       if (first_complex != second_complex
9839           && (code == PLUS_EXPR
9840               || code == MINUS_EXPR
9841               || code == MULT_EXPR
9842               || (code == TRUNC_DIV_EXPR && first_complex))
9843           && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
9844           && flag_signed_zeros)
9845         {
9846           /* An operation on mixed real/complex operands must be
9847              handled specially, but the language-independent code can
9848              more easily optimize the plain complex arithmetic if
9849              -fno-signed-zeros.  */
9850           tree real_type = TREE_TYPE (result_type);
9851           tree real, imag;
9852           if (type0 != orig_type0 || type1 != orig_type1)
9853             {
9854               gcc_assert (may_need_excess_precision && common);
9855               semantic_result_type = c_common_type (orig_type0, orig_type1);
9856             }
9857           if (first_complex)
9858             {
9859               if (TREE_TYPE (op0) != result_type)
9860                 op0 = convert_and_check (result_type, op0);
9861               if (TREE_TYPE (op1) != real_type)
9862                 op1 = convert_and_check (real_type, op1);
9863             }
9864           else
9865             {
9866               if (TREE_TYPE (op0) != real_type)
9867                 op0 = convert_and_check (real_type, op0);
9868               if (TREE_TYPE (op1) != result_type)
9869                 op1 = convert_and_check (result_type, op1);
9870             }
9871           if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
9872             return error_mark_node;
9873           if (first_complex)
9874             {
9875               op0 = c_save_expr (op0);
9876               real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
9877                                      op0, 1);
9878               imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
9879                                      op0, 1);
9880               switch (code)
9881                 {
9882                 case MULT_EXPR:
9883                 case TRUNC_DIV_EXPR:
9884                   imag = build2 (resultcode, real_type, imag, op1);
9885                   /* Fall through.  */
9886                 case PLUS_EXPR:
9887                 case MINUS_EXPR:
9888                   real = build2 (resultcode, real_type, real, op1);
9889                   break;
9890                 default:
9891                   gcc_unreachable();
9892                 }
9893             }
9894           else
9895             {
9896               op1 = c_save_expr (op1);
9897               real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
9898                                      op1, 1);
9899               imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
9900                                      op1, 1);
9901               switch (code)
9902                 {
9903                 case MULT_EXPR:
9904                   imag = build2 (resultcode, real_type, op0, imag);
9905                   /* Fall through.  */
9906                 case PLUS_EXPR:
9907                   real = build2 (resultcode, real_type, op0, real);
9908                   break;
9909                 case MINUS_EXPR:
9910                   real = build2 (resultcode, real_type, op0, real);
9911                   imag = build1 (NEGATE_EXPR, real_type, imag);
9912                   break;
9913                 default:
9914                   gcc_unreachable();
9915                 }
9916             }
9917           ret = build2 (COMPLEX_EXPR, result_type, real, imag);
9918           goto return_build_binary_op;
9919         }
9920
9921       /* For certain operations (which identify themselves by shorten != 0)
9922          if both args were extended from the same smaller type,
9923          do the arithmetic in that type and then extend.
9924
9925          shorten !=0 and !=1 indicates a bitwise operation.
9926          For them, this optimization is safe only if
9927          both args are zero-extended or both are sign-extended.
9928          Otherwise, we might change the result.
9929          Eg, (short)-1 | (unsigned short)-1 is (int)-1
9930          but calculated in (unsigned short) it would be (unsigned short)-1.  */
9931
9932       if (shorten && none_complex)
9933         {
9934           final_type = result_type;
9935           result_type = shorten_binary_op (result_type, op0, op1,
9936                                            shorten == -1);
9937         }
9938
9939       /* Shifts can be shortened if shifting right.  */
9940
9941       if (short_shift)
9942         {
9943           int unsigned_arg;
9944           tree arg0 = get_narrower (op0, &unsigned_arg);
9945
9946           final_type = result_type;
9947
9948           if (arg0 == op0 && final_type == TREE_TYPE (op0))
9949             unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
9950
9951           if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9952               && tree_int_cst_sgn (op1) > 0
9953               /* We can shorten only if the shift count is less than the
9954                  number of bits in the smaller type size.  */
9955               && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
9956               /* We cannot drop an unsigned shift after sign-extension.  */
9957               && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
9958             {
9959               /* Do an unsigned shift if the operand was zero-extended.  */
9960               result_type
9961                 = c_common_signed_or_unsigned_type (unsigned_arg,
9962                                                     TREE_TYPE (arg0));
9963               /* Convert value-to-be-shifted to that type.  */
9964               if (TREE_TYPE (op0) != result_type)
9965                 op0 = convert (result_type, op0);
9966               converted = 1;
9967             }
9968         }
9969
9970       /* Comparison operations are shortened too but differently.
9971          They identify themselves by setting short_compare = 1.  */
9972
9973       if (short_compare)
9974         {
9975           /* Don't write &op0, etc., because that would prevent op0
9976              from being kept in a register.
9977              Instead, make copies of the our local variables and
9978              pass the copies by reference, then copy them back afterward.  */
9979           tree xop0 = op0, xop1 = op1, xresult_type = result_type;
9980           enum tree_code xresultcode = resultcode;
9981           tree val
9982             = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
9983
9984           if (val != 0)
9985             {
9986               ret = val;
9987               goto return_build_binary_op;
9988             }
9989
9990           op0 = xop0, op1 = xop1;
9991           converted = 1;
9992           resultcode = xresultcode;
9993
9994           if (c_inhibit_evaluation_warnings == 0)
9995             {
9996               bool op0_maybe_const = true;
9997               bool op1_maybe_const = true;
9998               tree orig_op0_folded, orig_op1_folded;
9999
10000               if (in_late_binary_op)
10001                 {
10002                   orig_op0_folded = orig_op0;
10003                   orig_op1_folded = orig_op1;
10004                 }
10005               else
10006                 {
10007                   /* Fold for the sake of possible warnings, as in
10008                      build_conditional_expr.  This requires the
10009                      "original" values to be folded, not just op0 and
10010                      op1.  */
10011                   c_inhibit_evaluation_warnings++;
10012                   op0 = c_fully_fold (op0, require_constant_value,
10013                                       &op0_maybe_const);
10014                   op1 = c_fully_fold (op1, require_constant_value,
10015                                       &op1_maybe_const);
10016                   c_inhibit_evaluation_warnings--;
10017                   orig_op0_folded = c_fully_fold (orig_op0,
10018                                                   require_constant_value,
10019                                                   NULL);
10020                   orig_op1_folded = c_fully_fold (orig_op1,
10021                                                   require_constant_value,
10022                                                   NULL);
10023                 }
10024
10025               if (warn_sign_compare)
10026                 warn_for_sign_compare (location, orig_op0_folded,
10027                                        orig_op1_folded, op0, op1,
10028                                        result_type, resultcode);
10029               if (!in_late_binary_op)
10030                 {
10031                   if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10032                     op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10033                   if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10034                     op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10035                 }
10036             }
10037         }
10038     }
10039
10040   /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10041      If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10042      Then the expression will be built.
10043      It will be given type FINAL_TYPE if that is nonzero;
10044      otherwise, it will be given type RESULT_TYPE.  */
10045
10046   if (!result_type)
10047     {
10048       binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10049       return error_mark_node;
10050     }
10051
10052   if (build_type == NULL_TREE)
10053     {
10054       build_type = result_type;
10055       if (type0 != orig_type0 || type1 != orig_type1)
10056         {
10057           gcc_assert (may_need_excess_precision && common);
10058           semantic_result_type = c_common_type (orig_type0, orig_type1);
10059         }
10060     }
10061
10062   if (!converted)
10063     {
10064       op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10065       op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10066
10067       /* This can happen if one operand has a vector type, and the other
10068          has a different type.  */
10069       if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10070         return error_mark_node;
10071     }
10072
10073   /* Treat expressions in initializers specially as they can't trap.  */
10074   if (int_const_or_overflow)
10075     ret = (require_constant_value
10076            ? fold_build2_initializer_loc (location, resultcode, build_type,
10077                                           op0, op1)
10078            : fold_build2_loc (location, resultcode, build_type, op0, op1));
10079   else
10080     ret = build2 (resultcode, build_type, op0, op1);
10081   if (final_type != 0)
10082     ret = convert (final_type, ret);
10083
10084  return_build_binary_op:
10085   gcc_assert (ret != error_mark_node);
10086   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10087     ret = (int_operands
10088            ? note_integer_operands (ret)
10089            : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10090   else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10091            && !in_late_binary_op)
10092     ret = note_integer_operands (ret);
10093   if (semantic_result_type)
10094     ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10095   protected_set_expr_location (ret, location);
10096   return ret;
10097 }
10098
10099
10100 /* Convert EXPR to be a truth-value, validating its type for this
10101    purpose.  LOCATION is the source location for the expression.  */
10102
10103 tree
10104 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10105 {
10106   bool int_const, int_operands;
10107
10108   switch (TREE_CODE (TREE_TYPE (expr)))
10109     {
10110     case ARRAY_TYPE:
10111       error_at (location, "used array that cannot be converted to pointer where scalar is required");
10112       return error_mark_node;
10113
10114     case RECORD_TYPE:
10115       error_at (location, "used struct type value where scalar is required");
10116       return error_mark_node;
10117
10118     case UNION_TYPE:
10119       error_at (location, "used union type value where scalar is required");
10120       return error_mark_node;
10121
10122     case FUNCTION_TYPE:
10123       gcc_unreachable ();
10124
10125     default:
10126       break;
10127     }
10128
10129   int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10130   int_operands = EXPR_INT_CONST_OPERANDS (expr);
10131   if (int_operands)
10132     expr = remove_c_maybe_const_expr (expr);
10133
10134   /* ??? Should we also give an error for void and vectors rather than
10135      leaving those to give errors later?  */
10136   expr = c_common_truthvalue_conversion (location, expr);
10137
10138   if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10139     {
10140       if (TREE_OVERFLOW (expr))
10141         return expr;
10142       else
10143         return note_integer_operands (expr);
10144     }
10145   if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10146     return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10147   return expr;
10148 }
10149 \f
10150
10151 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10152    required.  */
10153
10154 tree
10155 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10156 {
10157   if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10158     {
10159       tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10160       /* Executing a compound literal inside a function reinitializes
10161          it.  */
10162       if (!TREE_STATIC (decl))
10163         *se = true;
10164       return decl;
10165     }
10166   else
10167     return expr;
10168 }
10169 \f
10170 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
10171
10172 tree
10173 c_begin_omp_parallel (void)
10174 {
10175   tree block;
10176
10177   keep_next_level ();
10178   block = c_begin_compound_stmt (true);
10179
10180   return block;
10181 }
10182
10183 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10184    statement.  LOC is the location of the OMP_PARALLEL.  */
10185
10186 tree
10187 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10188 {
10189   tree stmt;
10190
10191   block = c_end_compound_stmt (loc, block, true);
10192
10193   stmt = make_node (OMP_PARALLEL);
10194   TREE_TYPE (stmt) = void_type_node;
10195   OMP_PARALLEL_CLAUSES (stmt) = clauses;
10196   OMP_PARALLEL_BODY (stmt) = block;
10197   SET_EXPR_LOCATION (stmt, loc);
10198
10199   return add_stmt (stmt);
10200 }
10201
10202 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
10203
10204 tree
10205 c_begin_omp_task (void)
10206 {
10207   tree block;
10208
10209   keep_next_level ();
10210   block = c_begin_compound_stmt (true);
10211
10212   return block;
10213 }
10214
10215 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10216    statement.  LOC is the location of the #pragma.  */
10217
10218 tree
10219 c_finish_omp_task (location_t loc, tree clauses, tree block)
10220 {
10221   tree stmt;
10222
10223   block = c_end_compound_stmt (loc, block, true);
10224
10225   stmt = make_node (OMP_TASK);
10226   TREE_TYPE (stmt) = void_type_node;
10227   OMP_TASK_CLAUSES (stmt) = clauses;
10228   OMP_TASK_BODY (stmt) = block;
10229   SET_EXPR_LOCATION (stmt, loc);
10230
10231   return add_stmt (stmt);
10232 }
10233
10234 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10235    Remove any elements from the list that are invalid.  */
10236
10237 tree
10238 c_finish_omp_clauses (tree clauses)
10239 {
10240   bitmap_head generic_head, firstprivate_head, lastprivate_head;
10241   tree c, t, *pc = &clauses;
10242   const char *name;
10243
10244   bitmap_obstack_initialize (NULL);
10245   bitmap_initialize (&generic_head, &bitmap_default_obstack);
10246   bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10247   bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10248
10249   for (pc = &clauses, c = clauses; c ; c = *pc)
10250     {
10251       bool remove = false;
10252       bool need_complete = false;
10253       bool need_implicitly_determined = false;
10254
10255       switch (OMP_CLAUSE_CODE (c))
10256         {
10257         case OMP_CLAUSE_SHARED:
10258           name = "shared";
10259           need_implicitly_determined = true;
10260           goto check_dup_generic;
10261
10262         case OMP_CLAUSE_PRIVATE:
10263           name = "private";
10264           need_complete = true;
10265           need_implicitly_determined = true;
10266           goto check_dup_generic;
10267
10268         case OMP_CLAUSE_REDUCTION:
10269           name = "reduction";
10270           need_implicitly_determined = true;
10271           t = OMP_CLAUSE_DECL (c);
10272           if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10273               || POINTER_TYPE_P (TREE_TYPE (t)))
10274             {
10275               error_at (OMP_CLAUSE_LOCATION (c),
10276                         "%qE has invalid type for %<reduction%>", t);
10277               remove = true;
10278             }
10279           else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10280             {
10281               enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10282               const char *r_name = NULL;
10283
10284               switch (r_code)
10285                 {
10286                 case PLUS_EXPR:
10287                 case MULT_EXPR:
10288                 case MINUS_EXPR:
10289                   break;
10290                 case BIT_AND_EXPR:
10291                   r_name = "&";
10292                   break;
10293                 case BIT_XOR_EXPR:
10294                   r_name = "^";
10295                   break;
10296                 case BIT_IOR_EXPR:
10297                   r_name = "|";
10298                   break;
10299                 case TRUTH_ANDIF_EXPR:
10300                   r_name = "&&";
10301                   break;
10302                 case TRUTH_ORIF_EXPR:
10303                   r_name = "||";
10304                   break;
10305                 default:
10306                   gcc_unreachable ();
10307                 }
10308               if (r_name)
10309                 {
10310                   error_at (OMP_CLAUSE_LOCATION (c),
10311                             "%qE has invalid type for %<reduction(%s)%>",
10312                             t, r_name);
10313                   remove = true;
10314                 }
10315             }
10316           goto check_dup_generic;
10317
10318         case OMP_CLAUSE_COPYPRIVATE:
10319           name = "copyprivate";
10320           goto check_dup_generic;
10321
10322         case OMP_CLAUSE_COPYIN:
10323           name = "copyin";
10324           t = OMP_CLAUSE_DECL (c);
10325           if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10326             {
10327               error_at (OMP_CLAUSE_LOCATION (c),
10328                         "%qE must be %<threadprivate%> for %<copyin%>", t);
10329               remove = true;
10330             }
10331           goto check_dup_generic;
10332
10333         check_dup_generic:
10334           t = OMP_CLAUSE_DECL (c);
10335           if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10336             {
10337               error_at (OMP_CLAUSE_LOCATION (c),
10338                         "%qE is not a variable in clause %qs", t, name);
10339               remove = true;
10340             }
10341           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10342                    || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10343                    || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10344             {
10345               error_at (OMP_CLAUSE_LOCATION (c),
10346                         "%qE appears more than once in data clauses", t);
10347               remove = true;
10348             }
10349           else
10350             bitmap_set_bit (&generic_head, DECL_UID (t));
10351           break;
10352
10353         case OMP_CLAUSE_FIRSTPRIVATE:
10354           name = "firstprivate";
10355           t = OMP_CLAUSE_DECL (c);
10356           need_complete = true;
10357           need_implicitly_determined = true;
10358           if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10359             {
10360               error_at (OMP_CLAUSE_LOCATION (c),
10361                         "%qE is not a variable in clause %<firstprivate%>", t);
10362               remove = true;
10363             }
10364           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10365                    || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10366             {
10367               error_at (OMP_CLAUSE_LOCATION (c),
10368                         "%qE appears more than once in data clauses", t);
10369               remove = true;
10370             }
10371           else
10372             bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10373           break;
10374
10375         case OMP_CLAUSE_LASTPRIVATE:
10376           name = "lastprivate";
10377           t = OMP_CLAUSE_DECL (c);
10378           need_complete = true;
10379           need_implicitly_determined = true;
10380           if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10381             {
10382               error_at (OMP_CLAUSE_LOCATION (c),
10383                         "%qE is not a variable in clause %<lastprivate%>", t);
10384               remove = true;
10385             }
10386           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10387                    || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10388             {
10389               error_at (OMP_CLAUSE_LOCATION (c),
10390                      "%qE appears more than once in data clauses", t);
10391               remove = true;
10392             }
10393           else
10394             bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10395           break;
10396
10397         case OMP_CLAUSE_IF:
10398         case OMP_CLAUSE_NUM_THREADS:
10399         case OMP_CLAUSE_SCHEDULE:
10400         case OMP_CLAUSE_NOWAIT:
10401         case OMP_CLAUSE_ORDERED:
10402         case OMP_CLAUSE_DEFAULT:
10403         case OMP_CLAUSE_UNTIED:
10404         case OMP_CLAUSE_COLLAPSE:
10405           pc = &OMP_CLAUSE_CHAIN (c);
10406           continue;
10407
10408         default:
10409           gcc_unreachable ();
10410         }
10411
10412       if (!remove)
10413         {
10414           t = OMP_CLAUSE_DECL (c);
10415
10416           if (need_complete)
10417             {
10418               t = require_complete_type (t);
10419               if (t == error_mark_node)
10420                 remove = true;
10421             }
10422
10423           if (need_implicitly_determined)
10424             {
10425               const char *share_name = NULL;
10426
10427               if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10428                 share_name = "threadprivate";
10429               else switch (c_omp_predetermined_sharing (t))
10430                 {
10431                 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10432                   break;
10433                 case OMP_CLAUSE_DEFAULT_SHARED:
10434                   share_name = "shared";
10435                   break;
10436                 case OMP_CLAUSE_DEFAULT_PRIVATE:
10437                   share_name = "private";
10438                   break;
10439                 default:
10440                   gcc_unreachable ();
10441                 }
10442               if (share_name)
10443                 {
10444                   error_at (OMP_CLAUSE_LOCATION (c),
10445                             "%qE is predetermined %qs for %qs",
10446                             t, share_name, name);
10447                   remove = true;
10448                 }
10449             }
10450         }
10451
10452       if (remove)
10453         *pc = OMP_CLAUSE_CHAIN (c);
10454       else
10455         pc = &OMP_CLAUSE_CHAIN (c);
10456     }
10457
10458   bitmap_obstack_release (NULL);
10459   return clauses;
10460 }
10461
10462 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10463    down to the element type of an array.  */
10464
10465 tree
10466 c_build_qualified_type (tree type, int type_quals)
10467 {
10468   if (type == error_mark_node)
10469     return type;
10470
10471   if (TREE_CODE (type) == ARRAY_TYPE)
10472     {
10473       tree t;
10474       tree element_type = c_build_qualified_type (TREE_TYPE (type),
10475                                                   type_quals);
10476
10477       /* See if we already have an identically qualified type.  */
10478       for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10479         {
10480           if (TYPE_QUALS (strip_array_types (t)) == type_quals
10481               && TYPE_NAME (t) == TYPE_NAME (type)
10482               && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10483               && attribute_list_equal (TYPE_ATTRIBUTES (t),
10484                                        TYPE_ATTRIBUTES (type)))
10485             break;
10486         }
10487       if (!t)
10488         {
10489           tree domain = TYPE_DOMAIN (type);
10490
10491           t = build_variant_type_copy (type);
10492           TREE_TYPE (t) = element_type;
10493
10494           if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10495               || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10496             SET_TYPE_STRUCTURAL_EQUALITY (t);
10497           else if (TYPE_CANONICAL (element_type) != element_type
10498                    || (domain && TYPE_CANONICAL (domain) != domain))
10499             {
10500               tree unqualified_canon
10501                 = build_array_type (TYPE_CANONICAL (element_type),
10502                                     domain? TYPE_CANONICAL (domain)
10503                                           : NULL_TREE);
10504               TYPE_CANONICAL (t)
10505                 = c_build_qualified_type (unqualified_canon, type_quals);
10506             }
10507           else
10508             TYPE_CANONICAL (t) = t;
10509         }
10510       return t;
10511     }
10512
10513   /* A restrict-qualified pointer type must be a pointer to object or
10514      incomplete type.  Note that the use of POINTER_TYPE_P also allows
10515      REFERENCE_TYPEs, which is appropriate for C++.  */
10516   if ((type_quals & TYPE_QUAL_RESTRICT)
10517       && (!POINTER_TYPE_P (type)
10518           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10519     {
10520       error ("invalid use of %<restrict%>");
10521       type_quals &= ~TYPE_QUAL_RESTRICT;
10522     }
10523
10524   return build_qualified_type (type, type_quals);
10525 }
10526
10527 /* Build a VA_ARG_EXPR for the C parser.  */
10528
10529 tree
10530 c_build_va_arg (location_t loc, tree expr, tree type)
10531 {
10532   if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10533     warning_at (loc, OPT_Wc___compat,
10534                 "C++ requires promoted type, not enum type, in %<va_arg%>");
10535   return build_va_arg (loc, expr, type);
10536 }