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