2737b14ea187db1a150d6f6c140dab313aa43daa
[platform/upstream/gcc.git] / gcc / c / c-typeck.cc
1 /* Build expressions with type checking for C compiler.
2    Copyright (C) 1987-2023 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 "memmodel.h"
30 #include "target.h"
31 #include "function.h"
32 #include "bitmap.h"
33 #include "c-tree.h"
34 #include "gimple-expr.h"
35 #include "predict.h"
36 #include "stor-layout.h"
37 #include "trans-mem.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "langhooks.h"
41 #include "c-lang.h"
42 #include "intl.h"
43 #include "tree-iterator.h"
44 #include "gimplify.h"
45 #include "tree-inline.h"
46 #include "omp-general.h"
47 #include "c-family/c-objc.h"
48 #include "c-family/c-ubsan.h"
49 #include "gomp-constants.h"
50 #include "spellcheck-tree.h"
51 #include "gcc-rich-location.h"
52 #include "stringpool.h"
53 #include "attribs.h"
54 #include "asan.h"
55
56 /* Possible cases of implicit conversions.  Used to select diagnostic messages
57    and control folding initializers in convert_for_assignment.  */
58 enum impl_conv {
59   ic_argpass,
60   ic_assign,
61   ic_init,
62   ic_init_const,
63   ic_return
64 };
65
66 /* The level of nesting inside "__alignof__".  */
67 int in_alignof;
68
69 /* The level of nesting inside "sizeof".  */
70 int in_sizeof;
71
72 /* The level of nesting inside "typeof".  */
73 int in_typeof;
74
75 /* True when parsing OpenMP loop expressions.  */
76 bool c_in_omp_for;
77
78 /* The argument of last parsed sizeof expression, only to be tested
79    if expr.original_code == SIZEOF_EXPR.  */
80 tree c_last_sizeof_arg;
81 location_t c_last_sizeof_loc;
82
83 /* Nonzero if we might need to print a "missing braces around
84    initializer" message within this initializer.  */
85 static int found_missing_braces;
86
87 static bool require_constant_value;
88 static bool require_constant_elements;
89 static bool require_constexpr_value;
90
91 static bool null_pointer_constant_p (const_tree);
92 static tree qualify_type (tree, tree);
93 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
94                                          bool *);
95 static int comp_target_types (location_t, tree, tree);
96 static int function_types_compatible_p (const_tree, const_tree, bool *,
97                                         bool *);
98 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
99 static tree lookup_field (tree, tree);
100 static int convert_arguments (location_t, vec<location_t>, tree,
101                               vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
102                               tree);
103 static tree pointer_diff (location_t, tree, tree, tree *);
104 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
105                                     enum impl_conv, bool, tree, tree, int,
106                                     int = 0);
107 static tree valid_compound_expr_initializer (tree, tree);
108 static void push_string (const char *);
109 static void push_member_name (tree);
110 static int spelling_length (void);
111 static char *print_spelling (char *);
112 static void warning_init (location_t, int, const char *);
113 static tree digest_init (location_t, tree, tree, tree, bool, bool, bool, bool,
114                          bool, bool);
115 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
116                                  bool, struct obstack *);
117 static void output_pending_init_elements (int, struct obstack *);
118 static bool set_designator (location_t, bool, struct obstack *);
119 static void push_range_stack (tree, struct obstack *);
120 static void add_pending_init (location_t, tree, tree, tree, bool,
121                               struct obstack *);
122 static void set_nonincremental_init (struct obstack *);
123 static void set_nonincremental_init_from_string (tree, struct obstack *);
124 static tree find_init_member (tree, struct obstack *);
125 static void readonly_warning (tree, enum lvalue_use);
126 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
127 static void record_maybe_used_decl (tree);
128 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
129 \f
130 /* Return true if EXP is a null pointer constant, false otherwise.  */
131
132 static bool
133 null_pointer_constant_p (const_tree expr)
134 {
135   /* This should really operate on c_expr structures, but they aren't
136      yet available everywhere required.  */
137   tree type = TREE_TYPE (expr);
138
139   /* An integer constant expression with the value 0, such an expression
140      cast to type void*, or the predefined constant nullptr, are a null
141      pointer constant.  */
142   if (expr == nullptr_node)
143     return true;
144
145   return (TREE_CODE (expr) == INTEGER_CST
146           && !TREE_OVERFLOW (expr)
147           && integer_zerop (expr)
148           && (INTEGRAL_TYPE_P (type)
149               || (TREE_CODE (type) == POINTER_TYPE
150                   && VOID_TYPE_P (TREE_TYPE (type))
151                   && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
152 }
153
154 /* EXPR may appear in an unevaluated part of an integer constant
155    expression, but not in an evaluated part.  Wrap it in a
156    C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
157    INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR.  */
158
159 static tree
160 note_integer_operands (tree expr)
161 {
162   tree ret;
163   if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
164     {
165       ret = copy_node (expr);
166       TREE_OVERFLOW (ret) = 1;
167     }
168   else
169     {
170       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
171       C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
172     }
173   return ret;
174 }
175
176 /* Having checked whether EXPR may appear in an unevaluated part of an
177    integer constant expression and found that it may, remove any
178    C_MAYBE_CONST_EXPR noting this fact and return the resulting
179    expression.  */
180
181 static inline tree
182 remove_c_maybe_const_expr (tree expr)
183 {
184   if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
185     return C_MAYBE_CONST_EXPR_EXPR (expr);
186   else
187     return expr;
188 }
189
190 \f/* This is a cache to hold if two types are compatible or not.  */
191
192 struct tagged_tu_seen_cache {
193   const struct tagged_tu_seen_cache * next;
194   const_tree t1;
195   const_tree t2;
196   /* The return value of tagged_types_tu_compatible_p if we had seen
197      these two types already.  */
198   int val;
199 };
200
201 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
202 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
203
204 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
205    does not have an incomplete type.  (That includes void types.)
206    LOC is the location of the use.  */
207
208 tree
209 require_complete_type (location_t loc, tree value)
210 {
211   tree type = TREE_TYPE (value);
212
213   if (error_operand_p (value))
214     return error_mark_node;
215
216   /* First, detect a valid value with a complete type.  */
217   if (COMPLETE_TYPE_P (type))
218     return value;
219
220   c_incomplete_type_error (loc, value, type);
221   return error_mark_node;
222 }
223
224 /* Print an error message for invalid use of an incomplete type.
225    VALUE is the expression that was used (or 0 if that isn't known)
226    and TYPE is the type that was invalid.  LOC is the location for
227    the error.  */
228
229 void
230 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
231 {
232   /* Avoid duplicate error message.  */
233   if (TREE_CODE (type) == ERROR_MARK)
234     return;
235
236   if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
237     error_at (loc, "%qD has an incomplete type %qT", value, type);
238   else
239     {
240     retry:
241       /* We must print an error message.  Be clever about what it says.  */
242
243       switch (TREE_CODE (type))
244         {
245         case RECORD_TYPE:
246         case UNION_TYPE:
247         case ENUMERAL_TYPE:
248           break;
249
250         case VOID_TYPE:
251           error_at (loc, "invalid use of void expression");
252           return;
253
254         case ARRAY_TYPE:
255           if (TYPE_DOMAIN (type))
256             {
257               if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
258                 {
259                   error_at (loc, "invalid use of flexible array member");
260                   return;
261                 }
262               type = TREE_TYPE (type);
263               goto retry;
264             }
265           error_at (loc, "invalid use of array with unspecified bounds");
266           return;
267
268         default:
269           gcc_unreachable ();
270         }
271
272       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
273         error_at (loc, "invalid use of undefined type %qT", type);
274       else
275         /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL.  */
276         error_at (loc, "invalid use of incomplete typedef %qT", type);
277     }
278 }
279
280 /* Given a type, apply default promotions wrt unnamed function
281    arguments and return the new type.  */
282
283 tree
284 c_type_promotes_to (tree type)
285 {
286   tree ret = NULL_TREE;
287
288   if (TYPE_MAIN_VARIANT (type) == float_type_node)
289     ret = double_type_node;
290   else if (c_promoting_integer_type_p (type))
291     {
292       /* Preserve unsignedness if not really getting any wider.  */
293       if (TYPE_UNSIGNED (type)
294           && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
295         ret = unsigned_type_node;
296       else
297         ret = integer_type_node;
298     }
299
300   if (ret != NULL_TREE)
301     return (TYPE_ATOMIC (type)
302             ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
303             : ret);
304
305   return type;
306 }
307
308 /* Return true if between two named address spaces, whether there is a superset
309    named address space that encompasses both address spaces.  If there is a
310    superset, return which address space is the superset.  */
311
312 static bool
313 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
314 {
315   if (as1 == as2)
316     {
317       *common = as1;
318       return true;
319     }
320   else if (targetm.addr_space.subset_p (as1, as2))
321     {
322       *common = as2;
323       return true;
324     }
325   else if (targetm.addr_space.subset_p (as2, as1))
326     {
327       *common = as1;
328       return true;
329     }
330   else
331     return false;
332 }
333
334 /* Return a variant of TYPE which has all the type qualifiers of LIKE
335    as well as those of TYPE.  */
336
337 static tree
338 qualify_type (tree type, tree like)
339 {
340   addr_space_t as_type = TYPE_ADDR_SPACE (type);
341   addr_space_t as_like = TYPE_ADDR_SPACE (like);
342   addr_space_t as_common;
343
344   /* If the two named address spaces are different, determine the common
345      superset address space.  If there isn't one, raise an error.  */
346   if (!addr_space_superset (as_type, as_like, &as_common))
347     {
348       as_common = as_type;
349       error ("%qT and %qT are in disjoint named address spaces",
350              type, like);
351     }
352
353   return c_build_qualified_type (type,
354                                  TYPE_QUALS_NO_ADDR_SPACE (type)
355                                  | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
356                                  | ENCODE_QUAL_ADDR_SPACE (as_common));
357 }
358
359 /* Return true iff the given tree T is a variable length array.  */
360
361 bool
362 c_vla_type_p (const_tree t)
363 {
364   if (TREE_CODE (t) == ARRAY_TYPE
365       && C_TYPE_VARIABLE_SIZE (t))
366     return true;
367   return false;
368 }
369
370 /* If NTYPE is a type of a non-variadic function with a prototype
371    and OTYPE is a type of a function without a prototype and ATTRS
372    contains attribute format, diagnosess and removes it from ATTRS.
373    Returns the result of build_type_attribute_variant of NTYPE and
374    the (possibly) modified ATTRS.  */
375
376 static tree
377 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
378 {
379   if (!prototype_p (otype)
380       && prototype_p (ntype)
381       && lookup_attribute ("format", attrs))
382     {
383       warning_at (input_location, OPT_Wattributes,
384                   "%qs attribute cannot be applied to a function that "
385                   "does not take variable arguments", "format");
386       attrs = remove_attribute ("format", attrs);
387     }
388   return build_type_attribute_variant (ntype, attrs);
389
390 }
391 /* Return the composite type of two compatible types.
392
393    We assume that comptypes has already been done and returned
394    nonzero; if that isn't so, this may crash.  In particular, we
395    assume that qualifiers match.  */
396
397 tree
398 composite_type (tree t1, tree t2)
399 {
400   enum tree_code code1;
401   enum tree_code code2;
402   tree attributes;
403
404   /* Save time if the two types are the same.  */
405
406   if (t1 == t2) return t1;
407
408   /* If one type is nonsense, use the other.  */
409   if (t1 == error_mark_node)
410     return t2;
411   if (t2 == error_mark_node)
412     return t1;
413
414   code1 = TREE_CODE (t1);
415   code2 = TREE_CODE (t2);
416
417   /* Merge the attributes.  */
418   attributes = targetm.merge_type_attributes (t1, t2);
419
420   /* If one is an enumerated type and the other is the compatible
421      integer type, the composite type might be either of the two
422      (DR#013 question 3).  For consistency, use the enumerated type as
423      the composite type.  */
424
425   if (code1 == ENUMERAL_TYPE
426       && (code2 == INTEGER_TYPE || code2 == BOOLEAN_TYPE))
427     return t1;
428   if (code2 == ENUMERAL_TYPE
429       && (code1 == INTEGER_TYPE || code1 == BOOLEAN_TYPE))
430     return t2;
431
432   gcc_assert (code1 == code2);
433
434   switch (code1)
435     {
436     case POINTER_TYPE:
437       /* For two pointers, do this recursively on the target type.  */
438       {
439         tree pointed_to_1 = TREE_TYPE (t1);
440         tree pointed_to_2 = TREE_TYPE (t2);
441         tree target = composite_type (pointed_to_1, pointed_to_2);
442         t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
443         t1 = build_type_attribute_variant (t1, attributes);
444         return qualify_type (t1, t2);
445       }
446
447     case ARRAY_TYPE:
448       {
449         tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
450         int quals;
451         tree unqual_elt;
452         tree d1 = TYPE_DOMAIN (t1);
453         tree d2 = TYPE_DOMAIN (t2);
454         bool d1_variable, d2_variable;
455         bool d1_zero, d2_zero;
456         bool t1_complete, t2_complete;
457
458         /* We should not have any type quals on arrays at all.  */
459         gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
460                     && !TYPE_QUALS_NO_ADDR_SPACE (t2));
461
462         t1_complete = COMPLETE_TYPE_P (t1);
463         t2_complete = COMPLETE_TYPE_P (t2);
464
465         d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
466         d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
467
468         d1_variable = (!d1_zero
469                        && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
470                            || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
471         d2_variable = (!d2_zero
472                        && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
473                            || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
474         d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
475         d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
476
477         /* Save space: see if the result is identical to one of the args.  */
478         if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
479             && (d2_variable || d2_zero || !d1_variable))
480           return build_type_attribute_variant (t1, attributes);
481         if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
482             && (d1_variable || d1_zero || !d2_variable))
483           return build_type_attribute_variant (t2, attributes);
484
485         if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
486           return build_type_attribute_variant (t1, attributes);
487         if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
488           return build_type_attribute_variant (t2, attributes);
489
490         /* Merge the element types, and have a size if either arg has
491            one.  We may have qualifiers on the element types.  To set
492            up TYPE_MAIN_VARIANT correctly, we need to form the
493            composite of the unqualified types and add the qualifiers
494            back at the end.  */
495         quals = TYPE_QUALS (strip_array_types (elt));
496         unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
497         t1 = build_array_type (unqual_elt,
498                                TYPE_DOMAIN ((TYPE_DOMAIN (t1)
499                                              && (d2_variable
500                                                  || d2_zero
501                                                  || !d1_variable))
502                                             ? t1
503                                             : t2));
504         /* Ensure a composite type involving a zero-length array type
505            is a zero-length type not an incomplete type.  */
506         if (d1_zero && d2_zero
507             && (t1_complete || t2_complete)
508             && !COMPLETE_TYPE_P (t1))
509           {
510             TYPE_SIZE (t1) = bitsize_zero_node;
511             TYPE_SIZE_UNIT (t1) = size_zero_node;
512           }
513         t1 = c_build_qualified_type (t1, quals);
514         return build_type_attribute_variant (t1, attributes);
515       }
516
517     case ENUMERAL_TYPE:
518     case RECORD_TYPE:
519     case UNION_TYPE:
520       if (attributes != NULL)
521         {
522           /* Try harder not to create a new aggregate type.  */
523           if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
524             return t1;
525           if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
526             return t2;
527         }
528       return build_type_attribute_variant (t1, attributes);
529
530     case FUNCTION_TYPE:
531       /* Function types: prefer the one that specified arg types.
532          If both do, merge the arg types.  Also merge the return types.  */
533       {
534         tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
535         tree p1 = TYPE_ARG_TYPES (t1);
536         tree p2 = TYPE_ARG_TYPES (t2);
537         int len;
538         tree newargs, n;
539         int i;
540
541         /* Save space: see if the result is identical to one of the args.  */
542         if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
543           return build_functype_attribute_variant (t1, t2, attributes);
544         if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
545           return build_functype_attribute_variant (t2, t1, attributes);
546
547         /* Simple way if one arg fails to specify argument types.  */
548         if (TYPE_ARG_TYPES (t1) == NULL_TREE)
549           {
550             t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2),
551                                       TYPE_NO_NAMED_ARGS_STDARG_P (t2));
552             t1 = build_type_attribute_variant (t1, attributes);
553             return qualify_type (t1, t2);
554          }
555         if (TYPE_ARG_TYPES (t2) == NULL_TREE)
556           {
557             t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1),
558                                       TYPE_NO_NAMED_ARGS_STDARG_P (t1));
559             t1 = build_type_attribute_variant (t1, attributes);
560             return qualify_type (t1, t2);
561           }
562
563         /* If both args specify argument types, we must merge the two
564            lists, argument by argument.  */
565
566         for (len = 0, newargs = p1;
567              newargs && newargs != void_list_node;
568              len++, newargs = TREE_CHAIN (newargs))
569           ;
570
571         for (i = 0; i < len; i++)
572           newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
573
574         n = newargs;
575
576         for (; p1 && p1 != void_list_node;
577              p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
578           {
579             /* A null type means arg type is not specified.
580                Take whatever the other function type has.  */
581             if (TREE_VALUE (p1) == NULL_TREE)
582               {
583                 TREE_VALUE (n) = TREE_VALUE (p2);
584                 goto parm_done;
585               }
586             if (TREE_VALUE (p2) == NULL_TREE)
587               {
588                 TREE_VALUE (n) = TREE_VALUE (p1);
589                 goto parm_done;
590               }
591
592             /* Given  wait (union {union wait *u; int *i} *)
593                and  wait (union wait *),
594                prefer  union wait *  as type of parm.  */
595             if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
596                 && TREE_VALUE (p1) != TREE_VALUE (p2))
597               {
598                 tree memb;
599                 tree mv2 = TREE_VALUE (p2);
600                 if (mv2 && mv2 != error_mark_node
601                     && TREE_CODE (mv2) != ARRAY_TYPE)
602                   mv2 = TYPE_MAIN_VARIANT (mv2);
603                 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
604                      memb; memb = DECL_CHAIN (memb))
605                   {
606                     tree mv3 = TREE_TYPE (memb);
607                     if (mv3 && mv3 != error_mark_node
608                         && TREE_CODE (mv3) != ARRAY_TYPE)
609                       mv3 = TYPE_MAIN_VARIANT (mv3);
610                     if (comptypes (mv3, mv2))
611                       {
612                         TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
613                                                          TREE_VALUE (p2));
614                         pedwarn (input_location, OPT_Wpedantic,
615                                  "function types not truly compatible in ISO C");
616                         goto parm_done;
617                       }
618                   }
619               }
620             if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
621                 && TREE_VALUE (p2) != TREE_VALUE (p1))
622               {
623                 tree memb;
624                 tree mv1 = TREE_VALUE (p1);
625                 if (mv1 && mv1 != error_mark_node
626                     && TREE_CODE (mv1) != ARRAY_TYPE)
627                   mv1 = TYPE_MAIN_VARIANT (mv1);
628                 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
629                      memb; memb = DECL_CHAIN (memb))
630                   {
631                     tree mv3 = TREE_TYPE (memb);
632                     if (mv3 && mv3 != error_mark_node
633                         && TREE_CODE (mv3) != ARRAY_TYPE)
634                       mv3 = TYPE_MAIN_VARIANT (mv3);
635                     if (comptypes (mv3, mv1))
636                       {
637                         TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
638                                                          TREE_VALUE (p1));
639                         pedwarn (input_location, OPT_Wpedantic,
640                                  "function types not truly compatible in ISO C");
641                         goto parm_done;
642                       }
643                   }
644               }
645             TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
646           parm_done: ;
647           }
648
649         t1 = build_function_type (valtype, newargs);
650         t1 = qualify_type (t1, t2);
651       }
652       /* FALLTHRU */
653
654     default:
655       return build_type_attribute_variant (t1, attributes);
656     }
657
658 }
659
660 /* Return the type of a conditional expression between pointers to
661    possibly differently qualified versions of compatible types.
662
663    We assume that comp_target_types has already been done and returned
664    nonzero; if that isn't so, this may crash.  */
665
666 static tree
667 common_pointer_type (tree t1, tree t2)
668 {
669   tree attributes;
670   tree pointed_to_1, mv1;
671   tree pointed_to_2, mv2;
672   tree target;
673   unsigned target_quals;
674   addr_space_t as1, as2, as_common;
675   int quals1, quals2;
676
677   /* Save time if the two types are the same.  */
678
679   if (t1 == t2) return t1;
680
681   /* If one type is nonsense, use the other.  */
682   if (t1 == error_mark_node)
683     return t2;
684   if (t2 == error_mark_node)
685     return t1;
686
687   gcc_assert (TREE_CODE (t1) == POINTER_TYPE
688               && TREE_CODE (t2) == POINTER_TYPE);
689
690   /* Merge the attributes.  */
691   attributes = targetm.merge_type_attributes (t1, t2);
692
693   /* Find the composite type of the target types, and combine the
694      qualifiers of the two types' targets.  Do not lose qualifiers on
695      array element types by taking the TYPE_MAIN_VARIANT.  */
696   mv1 = pointed_to_1 = TREE_TYPE (t1);
697   mv2 = pointed_to_2 = TREE_TYPE (t2);
698   if (TREE_CODE (mv1) != ARRAY_TYPE)
699     mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
700   if (TREE_CODE (mv2) != ARRAY_TYPE)
701     mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
702   target = composite_type (mv1, mv2);
703
704   /* Strip array types to get correct qualifier for pointers to arrays */
705   quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
706   quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
707
708   /* For function types do not merge const qualifiers, but drop them
709      if used inconsistently.  The middle-end uses these to mark const
710      and noreturn functions.  */
711   if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
712     target_quals = (quals1 & quals2);
713   else
714     target_quals = (quals1 | quals2);
715
716   /* If the two named address spaces are different, determine the common
717      superset address space.  This is guaranteed to exist due to the
718      assumption that comp_target_type returned non-zero.  */
719   as1 = TYPE_ADDR_SPACE (pointed_to_1);
720   as2 = TYPE_ADDR_SPACE (pointed_to_2);
721   if (!addr_space_superset (as1, as2, &as_common))
722     gcc_unreachable ();
723
724   target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
725
726   t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
727   return build_type_attribute_variant (t1, attributes);
728 }
729
730 /* Return the common type for two arithmetic types under the usual
731    arithmetic conversions.  The default conversions have already been
732    applied, and enumerated types converted to their compatible integer
733    types.  The resulting type is unqualified and has no attributes.
734
735    This is the type for the result of most arithmetic operations
736    if the operands have the given two types.  */
737
738 static tree
739 c_common_type (tree t1, tree t2)
740 {
741   enum tree_code code1;
742   enum tree_code code2;
743
744   /* If one type is nonsense, use the other.  */
745   if (t1 == error_mark_node)
746     return t2;
747   if (t2 == error_mark_node)
748     return t1;
749
750   if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
751     t1 = TYPE_MAIN_VARIANT (t1);
752
753   if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
754     t2 = TYPE_MAIN_VARIANT (t2);
755
756   if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
757     {
758       tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
759       t1 = build_type_attribute_variant (t1, attrs);
760     }
761
762   if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
763     {
764       tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
765       t2 = build_type_attribute_variant (t2, attrs);
766     }
767
768   /* Save time if the two types are the same.  */
769
770   if (t1 == t2) return t1;
771
772   code1 = TREE_CODE (t1);
773   code2 = TREE_CODE (t2);
774
775   gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
776               || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
777               || code1 == INTEGER_TYPE);
778   gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
779               || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
780               || code2 == INTEGER_TYPE);
781
782   /* When one operand is a decimal float type, the other operand cannot be
783      a generic float type or a complex type.  We also disallow vector types
784      here.  */
785   if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
786       && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
787     {
788       if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
789         {
790           error ("cannot mix operands of decimal floating and vector types");
791           return error_mark_node;
792         }
793       if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
794         {
795           error ("cannot mix operands of decimal floating and complex types");
796           return error_mark_node;
797         }
798       if (code1 == REAL_TYPE && code2 == REAL_TYPE)
799         {
800           error ("cannot mix operands of decimal floating "
801                  "and other floating types");
802           return error_mark_node;
803         }
804     }
805
806   /* If one type is a vector type, return that type.  (How the usual
807      arithmetic conversions apply to the vector types extension is not
808      precisely specified.)  */
809   if (code1 == VECTOR_TYPE)
810     return t1;
811
812   if (code2 == VECTOR_TYPE)
813     return t2;
814
815   /* If one type is complex, form the common type of the non-complex
816      components, then make that complex.  Use T1 or T2 if it is the
817      required type.  */
818   if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
819     {
820       tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
821       tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
822       tree subtype = c_common_type (subtype1, subtype2);
823
824       if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
825         return t1;
826       else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
827         return t2;
828       else
829         return build_complex_type (subtype);
830     }
831
832   /* If only one is real, use it as the result.  */
833
834   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
835     return t1;
836
837   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
838     return t2;
839
840   /* If both are real and either are decimal floating point types, use
841      the decimal floating point type with the greater precision. */
842
843   if (code1 == REAL_TYPE && code2 == REAL_TYPE)
844     {
845       if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
846           || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
847         return dfloat128_type_node;
848       else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
849                || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
850         return dfloat64_type_node;
851       else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
852                || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
853         return dfloat32_type_node;
854     }
855
856   /* Deal with fixed-point types.  */
857   if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
858     {
859       unsigned int unsignedp = 0, satp = 0;
860       scalar_mode m1, m2;
861       unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
862
863       m1 = SCALAR_TYPE_MODE (t1);
864       m2 = SCALAR_TYPE_MODE (t2);
865
866       /* If one input type is saturating, the result type is saturating.  */
867       if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
868         satp = 1;
869
870       /* If both fixed-point types are unsigned, the result type is unsigned.
871          When mixing fixed-point and integer types, follow the sign of the
872          fixed-point type.
873          Otherwise, the result type is signed.  */
874       if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
875            && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
876           || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
877               && TYPE_UNSIGNED (t1))
878           || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
879               && TYPE_UNSIGNED (t2)))
880         unsignedp = 1;
881
882       /* The result type is signed.  */
883       if (unsignedp == 0)
884         {
885           /* If the input type is unsigned, we need to convert to the
886              signed type.  */
887           if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
888             {
889               enum mode_class mclass = (enum mode_class) 0;
890               if (GET_MODE_CLASS (m1) == MODE_UFRACT)
891                 mclass = MODE_FRACT;
892               else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
893                 mclass = MODE_ACCUM;
894               else
895                 gcc_unreachable ();
896               m1 = as_a <scalar_mode>
897                 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
898             }
899           if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
900             {
901               enum mode_class mclass = (enum mode_class) 0;
902               if (GET_MODE_CLASS (m2) == MODE_UFRACT)
903                 mclass = MODE_FRACT;
904               else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
905                 mclass = MODE_ACCUM;
906               else
907                 gcc_unreachable ();
908               m2 = as_a <scalar_mode>
909                 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
910             }
911         }
912
913       if (code1 == FIXED_POINT_TYPE)
914         {
915           fbit1 = GET_MODE_FBIT (m1);
916           ibit1 = GET_MODE_IBIT (m1);
917         }
918       else
919         {
920           fbit1 = 0;
921           /* Signed integers need to subtract one sign bit.  */
922           ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
923         }
924
925       if (code2 == FIXED_POINT_TYPE)
926         {
927           fbit2 = GET_MODE_FBIT (m2);
928           ibit2 = GET_MODE_IBIT (m2);
929         }
930       else
931         {
932           fbit2 = 0;
933           /* Signed integers need to subtract one sign bit.  */
934           ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
935         }
936
937       max_ibit = ibit1 >= ibit2 ?  ibit1 : ibit2;
938       max_fbit = fbit1 >= fbit2 ?  fbit1 : fbit2;
939       return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
940                                                  satp);
941     }
942
943   /* Both real or both integers; use the one with greater precision.  */
944
945   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
946     return t1;
947   else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
948     return t2;
949
950   /* Same precision.  Prefer long longs to longs to ints when the
951      same precision, following the C99 rules on integer type rank
952      (which are equivalent to the C90 rules for C90 types).  */
953
954   if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
955       || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
956     return long_long_unsigned_type_node;
957
958   if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
959       || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
960     {
961       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
962         return long_long_unsigned_type_node;
963       else
964         return long_long_integer_type_node;
965     }
966
967   if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
968       || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
969     return long_unsigned_type_node;
970
971   if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
972       || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
973     {
974       /* But preserve unsignedness from the other type,
975          since long cannot hold all the values of an unsigned int.  */
976       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
977         return long_unsigned_type_node;
978       else
979         return long_integer_type_node;
980     }
981
982   /* For floating types of the same TYPE_PRECISION (which we here
983      assume means either the same set of values, or sets of values
984      neither a subset of the other, with behavior being undefined in
985      the latter case), follow the rules from TS 18661-3: prefer
986      interchange types _FloatN, then standard types long double,
987      double, float, then extended types _FloatNx.  For extended types,
988      check them starting with _Float128x as that seems most consistent
989      in spirit with preferring long double to double; for interchange
990      types, also check in that order for consistency although it's not
991      possible for more than one of them to have the same
992      precision.  */
993   tree mv1 = TYPE_MAIN_VARIANT (t1);
994   tree mv2 = TYPE_MAIN_VARIANT (t2);
995
996   for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
997     if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
998       return FLOATN_TYPE_NODE (i);
999
1000   /* Likewise, prefer long double to double even if same size.  */
1001   if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1002     return long_double_type_node;
1003
1004   /* Likewise, prefer double to float even if same size.
1005      We got a couple of embedded targets with 32 bit doubles, and the
1006      pdp11 might have 64 bit floats.  */
1007   if (mv1 == double_type_node || mv2 == double_type_node)
1008     return double_type_node;
1009
1010   if (mv1 == float_type_node || mv2 == float_type_node)
1011     return float_type_node;
1012
1013   for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1014     if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1015       return FLOATNX_TYPE_NODE (i);
1016
1017   /* Otherwise prefer the unsigned one.  */
1018
1019   if (TYPE_UNSIGNED (t1))
1020     return t1;
1021   else
1022     return t2;
1023 }
1024 \f
1025 /* Wrapper around c_common_type that is used by c-common.cc and other
1026    front end optimizations that remove promotions.  ENUMERAL_TYPEs
1027    are allowed here and are converted to their compatible integer types.
1028    BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1029    preferably a non-Boolean type as the common type.  */
1030 tree
1031 common_type (tree t1, tree t2)
1032 {
1033   if (TREE_CODE (t1) == ENUMERAL_TYPE)
1034     t1 = ENUM_UNDERLYING_TYPE (t1);
1035   if (TREE_CODE (t2) == ENUMERAL_TYPE)
1036     t2 = ENUM_UNDERLYING_TYPE (t2);
1037
1038   /* If both types are BOOLEAN_TYPE, then return boolean_type_node.  */
1039   if (TREE_CODE (t1) == BOOLEAN_TYPE
1040       && TREE_CODE (t2) == BOOLEAN_TYPE)
1041     return boolean_type_node;
1042
1043   /* If either type is BOOLEAN_TYPE, then return the other.  */
1044   if (TREE_CODE (t1) == BOOLEAN_TYPE)
1045     return t2;
1046   if (TREE_CODE (t2) == BOOLEAN_TYPE)
1047     return t1;
1048
1049   return c_common_type (t1, t2);
1050 }
1051
1052 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1053    or various other operations.  Return 2 if they are compatible
1054    but a warning may be needed if you use them together.  */
1055
1056 int
1057 comptypes (tree type1, tree type2)
1058 {
1059   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1060   int val;
1061
1062   val = comptypes_internal (type1, type2, NULL, NULL);
1063   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1064
1065   return val;
1066 }
1067
1068 /* Like comptypes, but if it returns non-zero because enum and int are
1069    compatible, it sets *ENUM_AND_INT_P to true.  */
1070
1071 int
1072 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1073 {
1074   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1075   int val;
1076
1077   val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1078   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1079
1080   return val;
1081 }
1082
1083 /* Like comptypes, but if it returns nonzero for different types, it
1084    sets *DIFFERENT_TYPES_P to true.  */
1085
1086 int
1087 comptypes_check_different_types (tree type1, tree type2,
1088                                  bool *different_types_p)
1089 {
1090   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1091   int val;
1092
1093   val = comptypes_internal (type1, type2, NULL, different_types_p);
1094   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1095
1096   return val;
1097 }
1098 \f
1099 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1100    or various other operations.  Return 2 if they are compatible
1101    but a warning may be needed if you use them together.  If
1102    ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1103    compatible integer type, then this sets *ENUM_AND_INT_P to true;
1104    *ENUM_AND_INT_P is never set to false.  If DIFFERENT_TYPES_P is not
1105    NULL, and the types are compatible but different enough not to be
1106    permitted in C11 typedef redeclarations, then this sets
1107    *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1108    false, but may or may not be set if the types are incompatible.
1109    This differs from comptypes, in that we don't free the seen
1110    types.  */
1111
1112 static int
1113 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1114                     bool *different_types_p)
1115 {
1116   const_tree t1 = type1;
1117   const_tree t2 = type2;
1118   int attrval, val;
1119
1120   /* Suppress errors caused by previously reported errors.  */
1121
1122   if (t1 == t2 || !t1 || !t2
1123       || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1124     return 1;
1125
1126   /* Enumerated types are compatible with integer types, but this is
1127      not transitive: two enumerated types in the same translation unit
1128      are compatible with each other only if they are the same type.  */
1129
1130   if (TREE_CODE (t1) == ENUMERAL_TYPE
1131       && COMPLETE_TYPE_P (t1)
1132       && TREE_CODE (t2) != ENUMERAL_TYPE)
1133     {
1134       t1 = ENUM_UNDERLYING_TYPE (t1);
1135       if (TREE_CODE (t2) != VOID_TYPE)
1136         {
1137           if (enum_and_int_p != NULL)
1138             *enum_and_int_p = true;
1139           if (different_types_p != NULL)
1140             *different_types_p = true;
1141         }
1142     }
1143   else if (TREE_CODE (t2) == ENUMERAL_TYPE
1144            && COMPLETE_TYPE_P (t2)
1145            && TREE_CODE (t1) != ENUMERAL_TYPE)
1146     {
1147       t2 = ENUM_UNDERLYING_TYPE (t2);
1148       if (TREE_CODE (t1) != VOID_TYPE)
1149         {
1150           if (enum_and_int_p != NULL)
1151             *enum_and_int_p = true;
1152           if (different_types_p != NULL)
1153             *different_types_p = true;
1154         }
1155     }
1156
1157   if (t1 == t2)
1158     return 1;
1159
1160   /* Different classes of types can't be compatible.  */
1161
1162   if (TREE_CODE (t1) != TREE_CODE (t2))
1163     return 0;
1164
1165   /* Qualifiers must match. C99 6.7.3p9 */
1166
1167   if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1168     return 0;
1169
1170   /* Allow for two different type nodes which have essentially the same
1171      definition.  Note that we already checked for equality of the type
1172      qualifiers (just above).  */
1173
1174   if (TREE_CODE (t1) != ARRAY_TYPE
1175       && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1176     return 1;
1177
1178   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1179   if (!(attrval = comp_type_attributes (t1, t2)))
1180      return 0;
1181
1182   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1183   val = 0;
1184
1185   switch (TREE_CODE (t1))
1186     {
1187     case INTEGER_TYPE:
1188     case FIXED_POINT_TYPE:
1189     case REAL_TYPE:
1190       /* With these nodes, we can't determine type equivalence by
1191          looking at what is stored in the nodes themselves, because
1192          two nodes might have different TYPE_MAIN_VARIANTs but still
1193          represent the same type.  For example, wchar_t and int could
1194          have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1195          TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1196          and are distinct types.  On the other hand, int and the
1197          following typedef
1198
1199            typedef int INT __attribute((may_alias));
1200
1201          have identical properties, different TYPE_MAIN_VARIANTs, but
1202          represent the same type.  The canonical type system keeps
1203          track of equivalence in this case, so we fall back on it.  */
1204       return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1205
1206     case POINTER_TYPE:
1207       /* Do not remove mode information.  */
1208       if (TYPE_MODE (t1) != TYPE_MODE (t2))
1209         break;
1210       val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1211              ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1212                                        enum_and_int_p, different_types_p));
1213       break;
1214
1215     case FUNCTION_TYPE:
1216       val = function_types_compatible_p (t1, t2, enum_and_int_p,
1217                                          different_types_p);
1218       break;
1219
1220     case ARRAY_TYPE:
1221       {
1222         tree d1 = TYPE_DOMAIN (t1);
1223         tree d2 = TYPE_DOMAIN (t2);
1224         bool d1_variable, d2_variable;
1225         bool d1_zero, d2_zero;
1226         val = 1;
1227
1228         /* Target types must match incl. qualifiers.  */
1229         if (TREE_TYPE (t1) != TREE_TYPE (t2)
1230             && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1231                                           enum_and_int_p,
1232                                           different_types_p)) == 0)
1233           return 0;
1234
1235         if (different_types_p != NULL
1236             && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1237           *different_types_p = true;
1238         /* Sizes must match unless one is missing or variable.  */
1239         if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1240           break;
1241
1242         d1_zero = !TYPE_MAX_VALUE (d1);
1243         d2_zero = !TYPE_MAX_VALUE (d2);
1244
1245         d1_variable = (!d1_zero
1246                        && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1247                            || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1248         d2_variable = (!d2_zero
1249                        && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1250                            || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1251         d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1252         d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1253
1254         if (different_types_p != NULL
1255             && d1_variable != d2_variable)
1256           *different_types_p = true;
1257         if (d1_variable || d2_variable)
1258           break;
1259         if (d1_zero && d2_zero)
1260           break;
1261         if (d1_zero || d2_zero
1262             || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1263             || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1264           val = 0;
1265
1266         break;
1267       }
1268
1269     case ENUMERAL_TYPE:
1270     case RECORD_TYPE:
1271     case UNION_TYPE:
1272       if (val != 1 && !same_translation_unit_p (t1, t2))
1273         {
1274           tree a1 = TYPE_ATTRIBUTES (t1);
1275           tree a2 = TYPE_ATTRIBUTES (t2);
1276
1277           if (! attribute_list_contained (a1, a2)
1278               && ! attribute_list_contained (a2, a1))
1279             break;
1280
1281           if (attrval != 2)
1282             return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1283                                                  different_types_p);
1284           val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1285                                               different_types_p);
1286         }
1287       break;
1288
1289     case VECTOR_TYPE:
1290       val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1291              && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1292                                     enum_and_int_p, different_types_p));
1293       break;
1294
1295     default:
1296       break;
1297     }
1298   return attrval == 2 && val == 1 ? 2 : val;
1299 }
1300
1301 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1302    their qualifiers, except for named address spaces.  If the pointers point to
1303    different named addresses, then we must determine if one address space is a
1304    subset of the other.  */
1305
1306 static int
1307 comp_target_types (location_t location, tree ttl, tree ttr)
1308 {
1309   int val;
1310   int val_ped;
1311   tree mvl = TREE_TYPE (ttl);
1312   tree mvr = TREE_TYPE (ttr);
1313   addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1314   addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1315   addr_space_t as_common;
1316   bool enum_and_int_p;
1317
1318   /* Fail if pointers point to incompatible address spaces.  */
1319   if (!addr_space_superset (asl, asr, &as_common))
1320     return 0;
1321
1322   /* For pedantic record result of comptypes on arrays before losing
1323      qualifiers on the element type below. */
1324   val_ped = 1;
1325
1326   if (TREE_CODE (mvl) == ARRAY_TYPE
1327       && TREE_CODE (mvr) == ARRAY_TYPE)
1328     val_ped = comptypes (mvl, mvr);
1329
1330   /* Qualifiers on element types of array types that are
1331      pointer targets are lost by taking their TYPE_MAIN_VARIANT.  */
1332
1333   mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1334          ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1335          : TYPE_MAIN_VARIANT (mvl));
1336
1337   mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1338          ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1339          : TYPE_MAIN_VARIANT (mvr));
1340
1341   enum_and_int_p = false;
1342   val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1343
1344   if (val == 1 && val_ped != 1)
1345     pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1346                                           "in ISO C before C2X");
1347
1348   if (val == 2)
1349     pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1350
1351   if (val == 1 && enum_and_int_p && warn_cxx_compat)
1352     warning_at (location, OPT_Wc___compat,
1353                 "pointer target types incompatible in C++");
1354
1355   return val;
1356 }
1357 \f
1358 /* Subroutines of `comptypes'.  */
1359
1360 /* Determine whether two trees derive from the same translation unit.
1361    If the CONTEXT chain ends in a null, that tree's context is still
1362    being parsed, so if two trees have context chains ending in null,
1363    they're in the same translation unit.  */
1364
1365 bool
1366 same_translation_unit_p (const_tree t1, const_tree t2)
1367 {
1368   while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1369     switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1370       {
1371       case tcc_declaration:
1372         t1 = DECL_CONTEXT (t1); break;
1373       case tcc_type:
1374         t1 = TYPE_CONTEXT (t1); break;
1375       case tcc_exceptional:
1376         t1 = BLOCK_SUPERCONTEXT (t1); break;  /* assume block */
1377       default: gcc_unreachable ();
1378       }
1379
1380   while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1381     switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1382       {
1383       case tcc_declaration:
1384         t2 = DECL_CONTEXT (t2); break;
1385       case tcc_type:
1386         t2 = TYPE_CONTEXT (t2); break;
1387       case tcc_exceptional:
1388         t2 = BLOCK_SUPERCONTEXT (t2); break;  /* assume block */
1389       default: gcc_unreachable ();
1390       }
1391
1392   return t1 == t2;
1393 }
1394
1395 /* Allocate the seen two types, assuming that they are compatible. */
1396
1397 static struct tagged_tu_seen_cache *
1398 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1399 {
1400   struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1401   tu->next = tagged_tu_seen_base;
1402   tu->t1 = t1;
1403   tu->t2 = t2;
1404
1405   tagged_tu_seen_base = tu;
1406
1407   /* The C standard says that two structures in different translation
1408      units are compatible with each other only if the types of their
1409      fields are compatible (among other things).  We assume that they
1410      are compatible until proven otherwise when building the cache.
1411      An example where this can occur is:
1412      struct a
1413      {
1414        struct a *next;
1415      };
1416      If we are comparing this against a similar struct in another TU,
1417      and did not assume they were compatible, we end up with an infinite
1418      loop.  */
1419   tu->val = 1;
1420   return tu;
1421 }
1422
1423 /* Free the seen types until we get to TU_TIL. */
1424
1425 static void
1426 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1427 {
1428   const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1429   while (tu != tu_til)
1430     {
1431       const struct tagged_tu_seen_cache *const tu1
1432         = (const struct tagged_tu_seen_cache *) tu;
1433       tu = tu1->next;
1434       XDELETE (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1435     }
1436   tagged_tu_seen_base = tu_til;
1437 }
1438
1439 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1440    compatible.  If the two types are not the same (which has been
1441    checked earlier), this can only happen when multiple translation
1442    units are being compiled.  See C99 6.2.7 paragraph 1 for the exact
1443    rules.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1444    comptypes_internal.  */
1445
1446 static int
1447 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1448                               bool *enum_and_int_p, bool *different_types_p)
1449 {
1450   tree s1, s2;
1451   bool needs_warning = false;
1452
1453   /* We have to verify that the tags of the types are the same.  This
1454      is harder than it looks because this may be a typedef, so we have
1455      to go look at the original type.  It may even be a typedef of a
1456      typedef...
1457      In the case of compiler-created builtin structs the TYPE_DECL
1458      may be a dummy, with no DECL_ORIGINAL_TYPE.  Don't fault.  */
1459   while (TYPE_NAME (t1)
1460          && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1461          && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1462     t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1463
1464   while (TYPE_NAME (t2)
1465          && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1466          && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1467     t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1468
1469   /* C90 didn't have the requirement that the two tags be the same.  */
1470   if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1471     return 0;
1472
1473   /* C90 didn't say what happened if one or both of the types were
1474      incomplete; we choose to follow C99 rules here, which is that they
1475      are compatible.  */
1476   if (TYPE_SIZE (t1) == NULL
1477       || TYPE_SIZE (t2) == NULL)
1478     return 1;
1479
1480   {
1481     const struct tagged_tu_seen_cache * tts_i;
1482     for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1483       if (tts_i->t1 == t1 && tts_i->t2 == t2)
1484         return tts_i->val;
1485   }
1486
1487   switch (TREE_CODE (t1))
1488     {
1489     case ENUMERAL_TYPE:
1490       {
1491         struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1492         /* Speed up the case where the type values are in the same order.  */
1493         tree tv1 = TYPE_VALUES (t1);
1494         tree tv2 = TYPE_VALUES (t2);
1495
1496         if (tv1 == tv2)
1497           {
1498             return 1;
1499           }
1500
1501         for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1502           {
1503             if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1504               break;
1505             if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1506               {
1507                 tu->val = 0;
1508                 return 0;
1509               }
1510           }
1511
1512         if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1513           {
1514             return 1;
1515           }
1516         if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1517           {
1518             tu->val = 0;
1519             return 0;
1520           }
1521
1522         if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1523           {
1524             tu->val = 0;
1525             return 0;
1526           }
1527
1528         for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1529           {
1530             s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1531             if (s2 == NULL
1532                 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1533               {
1534                 tu->val = 0;
1535                 return 0;
1536               }
1537           }
1538         return 1;
1539       }
1540
1541     case UNION_TYPE:
1542       {
1543         struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1544         if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1545           {
1546             tu->val = 0;
1547             return 0;
1548           }
1549
1550         /*  Speed up the common case where the fields are in the same order. */
1551         for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1552              s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1553           {
1554             int result;
1555
1556             if (DECL_NAME (s1) != DECL_NAME (s2))
1557               break;
1558             result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1559                                          enum_and_int_p, different_types_p);
1560
1561             if (result != 1 && !DECL_NAME (s1))
1562               break;
1563             if (result == 0)
1564               {
1565                 tu->val = 0;
1566                 return 0;
1567               }
1568             if (result == 2)
1569               needs_warning = true;
1570
1571             if (TREE_CODE (s1) == FIELD_DECL
1572                 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1573                                      DECL_FIELD_BIT_OFFSET (s2)) != 1)
1574               {
1575                 tu->val = 0;
1576                 return 0;
1577               }
1578           }
1579         if (!s1 && !s2)
1580           {
1581             tu->val = needs_warning ? 2 : 1;
1582             return tu->val;
1583           }
1584
1585         for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1586           {
1587             bool ok = false;
1588
1589             for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1590               if (DECL_NAME (s1) == DECL_NAME (s2))
1591                 {
1592                   int result;
1593
1594                   result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1595                                                enum_and_int_p,
1596                                                different_types_p);
1597
1598                   if (result != 1 && !DECL_NAME (s1))
1599                     continue;
1600                   if (result == 0)
1601                     {
1602                       tu->val = 0;
1603                       return 0;
1604                     }
1605                   if (result == 2)
1606                     needs_warning = true;
1607
1608                   if (TREE_CODE (s1) == FIELD_DECL
1609                       && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1610                                            DECL_FIELD_BIT_OFFSET (s2)) != 1)
1611                     break;
1612
1613                   ok = true;
1614                   break;
1615                 }
1616             if (!ok)
1617               {
1618                 tu->val = 0;
1619                 return 0;
1620               }
1621           }
1622         tu->val = needs_warning ? 2 : 10;
1623         return tu->val;
1624       }
1625
1626     case RECORD_TYPE:
1627       {
1628         struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1629
1630         for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1631              s1 && s2;
1632              s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1633           {
1634             int result;
1635             if (TREE_CODE (s1) != TREE_CODE (s2)
1636                 || DECL_NAME (s1) != DECL_NAME (s2))
1637               break;
1638             result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1639                                          enum_and_int_p, different_types_p);
1640             if (result == 0)
1641               break;
1642             if (result == 2)
1643               needs_warning = true;
1644
1645             if (TREE_CODE (s1) == FIELD_DECL
1646                 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1647                                      DECL_FIELD_BIT_OFFSET (s2)) != 1)
1648               break;
1649           }
1650         if (s1 && s2)
1651           tu->val = 0;
1652         else
1653           tu->val = needs_warning ? 2 : 1;
1654         return tu->val;
1655       }
1656
1657     default:
1658       gcc_unreachable ();
1659     }
1660 }
1661
1662 /* Return 1 if two function types F1 and F2 are compatible.
1663    If either type specifies no argument types,
1664    the other must specify a fixed number of self-promoting arg types.
1665    Otherwise, if one type specifies only the number of arguments,
1666    the other must specify that number of self-promoting arg types.
1667    Otherwise, the argument types must match.
1668    ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal.  */
1669
1670 static int
1671 function_types_compatible_p (const_tree f1, const_tree f2,
1672                              bool *enum_and_int_p, bool *different_types_p)
1673 {
1674   tree args1, args2;
1675   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1676   int val = 1;
1677   int val1;
1678   tree ret1, ret2;
1679
1680   ret1 = TREE_TYPE (f1);
1681   ret2 = TREE_TYPE (f2);
1682
1683   /* 'volatile' qualifiers on a function's return type used to mean
1684      the function is noreturn.  */
1685   if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1686     pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1687   if (TYPE_VOLATILE (ret1))
1688     ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1689                                  TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1690   if (TYPE_VOLATILE (ret2))
1691     ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1692                                  TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1693   val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1694   if (val == 0)
1695     return 0;
1696
1697   args1 = TYPE_ARG_TYPES (f1);
1698   args2 = TYPE_ARG_TYPES (f2);
1699
1700   if (different_types_p != NULL
1701       && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1702     *different_types_p = true;
1703
1704   /* An unspecified parmlist matches any specified parmlist
1705      whose argument types don't need default promotions.  */
1706
1707   if (args1 == NULL_TREE)
1708     {
1709       if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1710         return 0;
1711       if (!self_promoting_args_p (args2))
1712         return 0;
1713       /* If one of these types comes from a non-prototype fn definition,
1714          compare that with the other type's arglist.
1715          If they don't match, ask for a warning (but no error).  */
1716       if (TYPE_ACTUAL_ARG_TYPES (f1)
1717           && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1718                                       enum_and_int_p, different_types_p) != 1)
1719         val = 2;
1720       return val;
1721     }
1722   if (args2 == NULL_TREE)
1723     {
1724       if (TYPE_NO_NAMED_ARGS_STDARG_P (f1) != TYPE_NO_NAMED_ARGS_STDARG_P (f2))
1725         return 0;
1726       if (!self_promoting_args_p (args1))
1727         return 0;
1728       if (TYPE_ACTUAL_ARG_TYPES (f2)
1729           && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1730                                       enum_and_int_p, different_types_p) != 1)
1731         val = 2;
1732       return val;
1733     }
1734
1735   /* Both types have argument lists: compare them and propagate results.  */
1736   val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1737                                   different_types_p);
1738   return val1 != 1 ? val1 : val;
1739 }
1740
1741 /* Check two lists of types for compatibility, returning 0 for
1742    incompatible, 1 for compatible, or 2 for compatible with
1743    warning.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1744    comptypes_internal.  */
1745
1746 static int
1747 type_lists_compatible_p (const_tree args1, const_tree args2,
1748                          bool *enum_and_int_p, bool *different_types_p)
1749 {
1750   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1751   int val = 1;
1752   int newval = 0;
1753
1754   while (1)
1755     {
1756       tree a1, mv1, a2, mv2;
1757       if (args1 == NULL_TREE && args2 == NULL_TREE)
1758         return val;
1759       /* If one list is shorter than the other,
1760          they fail to match.  */
1761       if (args1 == NULL_TREE || args2 == NULL_TREE)
1762         return 0;
1763       mv1 = a1 = TREE_VALUE (args1);
1764       mv2 = a2 = TREE_VALUE (args2);
1765       if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1766         mv1 = (TYPE_ATOMIC (mv1)
1767                ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1768                                          TYPE_QUAL_ATOMIC)
1769                : TYPE_MAIN_VARIANT (mv1));
1770       if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1771         mv2 = (TYPE_ATOMIC (mv2)
1772                ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1773                                          TYPE_QUAL_ATOMIC)
1774                : TYPE_MAIN_VARIANT (mv2));
1775       /* A null pointer instead of a type
1776          means there is supposed to be an argument
1777          but nothing is specified about what type it has.
1778          So match anything that self-promotes.  */
1779       if (different_types_p != NULL
1780           && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1781         *different_types_p = true;
1782       if (a1 == NULL_TREE)
1783         {
1784           if (c_type_promotes_to (a2) != a2)
1785             return 0;
1786         }
1787       else if (a2 == NULL_TREE)
1788         {
1789           if (c_type_promotes_to (a1) != a1)
1790             return 0;
1791         }
1792       /* If one of the lists has an error marker, ignore this arg.  */
1793       else if (TREE_CODE (a1) == ERROR_MARK
1794                || TREE_CODE (a2) == ERROR_MARK)
1795         ;
1796       else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1797                                               different_types_p)))
1798         {
1799           if (different_types_p != NULL)
1800             *different_types_p = true;
1801           /* Allow  wait (union {union wait *u; int *i} *)
1802              and  wait (union wait *)  to be compatible.  */
1803           if (TREE_CODE (a1) == UNION_TYPE
1804               && (TYPE_NAME (a1) == NULL_TREE
1805                   || TYPE_TRANSPARENT_AGGR (a1))
1806               && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1807               && tree_int_cst_equal (TYPE_SIZE (a1),
1808                                      TYPE_SIZE (a2)))
1809             {
1810               tree memb;
1811               for (memb = TYPE_FIELDS (a1);
1812                    memb; memb = DECL_CHAIN (memb))
1813                 {
1814                   tree mv3 = TREE_TYPE (memb);
1815                   if (mv3 && mv3 != error_mark_node
1816                       && TREE_CODE (mv3) != ARRAY_TYPE)
1817                     mv3 = (TYPE_ATOMIC (mv3)
1818                            ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1819                                                      TYPE_QUAL_ATOMIC)
1820                            : TYPE_MAIN_VARIANT (mv3));
1821                   if (comptypes_internal (mv3, mv2, enum_and_int_p,
1822                                           different_types_p))
1823                     break;
1824                 }
1825               if (memb == NULL_TREE)
1826                 return 0;
1827             }
1828           else if (TREE_CODE (a2) == UNION_TYPE
1829                    && (TYPE_NAME (a2) == NULL_TREE
1830                        || TYPE_TRANSPARENT_AGGR (a2))
1831                    && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1832                    && tree_int_cst_equal (TYPE_SIZE (a2),
1833                                           TYPE_SIZE (a1)))
1834             {
1835               tree memb;
1836               for (memb = TYPE_FIELDS (a2);
1837                    memb; memb = DECL_CHAIN (memb))
1838                 {
1839                   tree mv3 = TREE_TYPE (memb);
1840                   if (mv3 && mv3 != error_mark_node
1841                       && TREE_CODE (mv3) != ARRAY_TYPE)
1842                     mv3 = (TYPE_ATOMIC (mv3)
1843                            ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1844                                                      TYPE_QUAL_ATOMIC)
1845                            : TYPE_MAIN_VARIANT (mv3));
1846                   if (comptypes_internal (mv3, mv1, enum_and_int_p,
1847                                           different_types_p))
1848                     break;
1849                 }
1850               if (memb == NULL_TREE)
1851                 return 0;
1852             }
1853           else
1854             return 0;
1855         }
1856
1857       /* comptypes said ok, but record if it said to warn.  */
1858       if (newval > val)
1859         val = newval;
1860
1861       args1 = TREE_CHAIN (args1);
1862       args2 = TREE_CHAIN (args2);
1863     }
1864 }
1865 \f
1866 /* Compute the size to increment a pointer by.  When a function type or void
1867    type or incomplete type is passed, size_one_node is returned.
1868    This function does not emit any diagnostics; the caller is responsible
1869    for that.  */
1870
1871 static tree
1872 c_size_in_bytes (const_tree type)
1873 {
1874   enum tree_code code = TREE_CODE (type);
1875
1876   if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1877       || !COMPLETE_TYPE_P (type))
1878     return size_one_node;
1879
1880   /* Convert in case a char is more than one unit.  */
1881   return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1882                          size_int (TYPE_PRECISION (char_type_node)
1883                                    / BITS_PER_UNIT));
1884 }
1885 \f
1886 /* Return either DECL or its known constant value (if it has one).  */
1887
1888 tree
1889 decl_constant_value_1 (tree decl, bool in_init)
1890 {
1891   if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL.  */
1892       TREE_CODE (decl) != PARM_DECL
1893       && !TREE_THIS_VOLATILE (decl)
1894       && TREE_READONLY (decl)
1895       && DECL_INITIAL (decl) != NULL_TREE
1896       && !error_operand_p (DECL_INITIAL (decl))
1897       /* This is invalid if initial value is not constant.
1898          If it has either a function call, a memory reference,
1899          or a variable, then re-evaluating it could give different results.  */
1900       && TREE_CONSTANT (DECL_INITIAL (decl))
1901       /* Check for cases where this is sub-optimal, even though valid.  */
1902       && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1903     return DECL_INITIAL (decl);
1904   return decl;
1905 }
1906
1907 /* Return either DECL or its known constant value (if it has one).
1908    Like the above, but always return decl outside of functions.  */
1909
1910 tree
1911 decl_constant_value (tree decl)
1912 {
1913   /* Don't change a variable array bound or initial value to a constant
1914      in a place where a variable is invalid.  */
1915   return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1916 }
1917
1918 /* Convert the array expression EXP to a pointer.  */
1919 static tree
1920 array_to_pointer_conversion (location_t loc, tree exp)
1921 {
1922   tree orig_exp = exp;
1923   tree type = TREE_TYPE (exp);
1924   tree adr;
1925   tree restype = TREE_TYPE (type);
1926   tree ptrtype;
1927
1928   gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1929
1930   STRIP_TYPE_NOPS (exp);
1931
1932   copy_warning (exp, orig_exp);
1933
1934   ptrtype = build_pointer_type (restype);
1935
1936   if (INDIRECT_REF_P (exp))
1937     return convert (ptrtype, TREE_OPERAND (exp, 0));
1938
1939   /* In C++ array compound literals are temporary objects unless they are
1940      const or appear in namespace scope, so they are destroyed too soon
1941      to use them for much of anything  (c++/53220).  */
1942   if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1943     {
1944       tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1945       if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1946         warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1947                     "converting an array compound literal to a pointer "
1948                     "is ill-formed in C++");
1949     }
1950
1951   adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1952   return convert (ptrtype, adr);
1953 }
1954
1955 /* Convert the function expression EXP to a pointer.  */
1956 static tree
1957 function_to_pointer_conversion (location_t loc, tree exp)
1958 {
1959   tree orig_exp = exp;
1960
1961   gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1962
1963   STRIP_TYPE_NOPS (exp);
1964
1965   copy_warning (exp, orig_exp);
1966
1967   return build_unary_op (loc, ADDR_EXPR, exp, false);
1968 }
1969
1970 /* Mark EXP as read, not just set, for set but not used -Wunused
1971    warning purposes.  */
1972
1973 void
1974 mark_exp_read (tree exp)
1975 {
1976   switch (TREE_CODE (exp))
1977     {
1978     case VAR_DECL:
1979     case PARM_DECL:
1980       DECL_READ_P (exp) = 1;
1981       break;
1982     case ARRAY_REF:
1983     case COMPONENT_REF:
1984     case MODIFY_EXPR:
1985     case REALPART_EXPR:
1986     case IMAGPART_EXPR:
1987     CASE_CONVERT:
1988     case ADDR_EXPR:
1989     case VIEW_CONVERT_EXPR:
1990       mark_exp_read (TREE_OPERAND (exp, 0));
1991       break;
1992     case COMPOUND_EXPR:
1993       /* Pattern match what build_atomic_assign produces with modifycode
1994          NOP_EXPR.  */
1995       if (VAR_P (TREE_OPERAND (exp, 1))
1996           && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1997           && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1998         {
1999           tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2000           tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
2001           if (TREE_CODE (t1) == TARGET_EXPR
2002               && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
2003               && TREE_CODE (t2) == CALL_EXPR)
2004             {
2005               tree fndecl = get_callee_fndecl (t2);
2006               tree arg = NULL_TREE;
2007               if (fndecl
2008                   && TREE_CODE (fndecl) == FUNCTION_DECL
2009                   && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
2010                   && call_expr_nargs (t2) >= 2)
2011                 switch (DECL_FUNCTION_CODE (fndecl))
2012                   {
2013                   case BUILT_IN_ATOMIC_STORE:
2014                     arg = CALL_EXPR_ARG (t2, 1);
2015                     break;
2016                   case BUILT_IN_ATOMIC_STORE_1:
2017                   case BUILT_IN_ATOMIC_STORE_2:
2018                   case BUILT_IN_ATOMIC_STORE_4:
2019                   case BUILT_IN_ATOMIC_STORE_8:
2020                   case BUILT_IN_ATOMIC_STORE_16:
2021                     arg = CALL_EXPR_ARG (t2, 0);
2022                     break;
2023                   default:
2024                     break;
2025                   }
2026               if (arg)
2027                 {
2028                   STRIP_NOPS (arg);
2029                   if (TREE_CODE (arg) == ADDR_EXPR
2030                       && DECL_P (TREE_OPERAND (arg, 0))
2031                       && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2032                     mark_exp_read (TREE_OPERAND (arg, 0));
2033                 }
2034             }
2035         }
2036       /* FALLTHRU */
2037     case C_MAYBE_CONST_EXPR:
2038       mark_exp_read (TREE_OPERAND (exp, 1));
2039       break;
2040     default:
2041       break;
2042     }
2043 }
2044
2045 /* Perform the default conversion of arrays and functions to pointers.
2046    Return the result of converting EXP.  For any other expression, just
2047    return EXP.
2048
2049    LOC is the location of the expression.  */
2050
2051 struct c_expr
2052 default_function_array_conversion (location_t loc, struct c_expr exp)
2053 {
2054   tree orig_exp = exp.value;
2055   tree type = TREE_TYPE (exp.value);
2056   enum tree_code code = TREE_CODE (type);
2057
2058   switch (code)
2059     {
2060     case ARRAY_TYPE:
2061       {
2062         bool not_lvalue = false;
2063         bool lvalue_array_p;
2064
2065         while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2066                 || CONVERT_EXPR_P (exp.value))
2067                && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2068           {
2069             if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2070               not_lvalue = true;
2071             exp.value = TREE_OPERAND (exp.value, 0);
2072           }
2073
2074         copy_warning (exp.value, orig_exp);
2075
2076         lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2077         if (!flag_isoc99 && !lvalue_array_p)
2078           {
2079             /* Before C99, non-lvalue arrays do not decay to pointers.
2080                Normally, using such an array would be invalid; but it can
2081                be used correctly inside sizeof or as a statement expression.
2082                Thus, do not give an error here; an error will result later.  */
2083             return exp;
2084           }
2085
2086         exp.value = array_to_pointer_conversion (loc, exp.value);
2087       }
2088       break;
2089     case FUNCTION_TYPE:
2090       exp.value = function_to_pointer_conversion (loc, exp.value);
2091       break;
2092     default:
2093       break;
2094     }
2095
2096   return exp;
2097 }
2098
2099 struct c_expr
2100 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2101 {
2102   mark_exp_read (exp.value);
2103   return default_function_array_conversion (loc, exp);
2104 }
2105
2106 /* Return whether EXPR should be treated as an atomic lvalue for the
2107    purposes of load and store handling.  */
2108
2109 static bool
2110 really_atomic_lvalue (tree expr)
2111 {
2112   if (error_operand_p (expr))
2113     return false;
2114   if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2115     return false;
2116   if (!lvalue_p (expr))
2117     return false;
2118
2119   /* Ignore _Atomic on register variables, since their addresses can't
2120      be taken so (a) atomicity is irrelevant and (b) the normal atomic
2121      sequences wouldn't work.  Ignore _Atomic on structures containing
2122      bit-fields, since accessing elements of atomic structures or
2123      unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2124      it's undefined at translation time or execution time, and the
2125      normal atomic sequences again wouldn't work.  */
2126   while (handled_component_p (expr))
2127     {
2128       if (TREE_CODE (expr) == COMPONENT_REF
2129           && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2130         return false;
2131       expr = TREE_OPERAND (expr, 0);
2132     }
2133   if (DECL_P (expr) && C_DECL_REGISTER (expr))
2134     return false;
2135   return true;
2136 }
2137
2138 /* If EXPR is a named constant (C2x) derived from a constexpr variable
2139    - that is, a reference to such a variable, or a member extracted by
2140    a sequence of structure and union (but not array) member accesses
2141    (where union member accesses must access the same member as
2142    initialized) - then return the corresponding initializer;
2143    otherwise, return NULL_TREE.  */
2144
2145 static tree
2146 maybe_get_constexpr_init (tree expr)
2147 {
2148   tree decl = NULL_TREE;
2149   if (TREE_CODE (expr) == VAR_DECL)
2150     decl = expr;
2151   else if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
2152     decl = COMPOUND_LITERAL_EXPR_DECL (expr);
2153   if (decl
2154       && C_DECL_DECLARED_CONSTEXPR (decl)
2155       && DECL_INITIAL (decl) != NULL_TREE
2156       && !error_operand_p (DECL_INITIAL (decl)))
2157     return DECL_INITIAL (decl);
2158   if (TREE_CODE (expr) != COMPONENT_REF)
2159     return NULL_TREE;
2160   tree inner = maybe_get_constexpr_init (TREE_OPERAND (expr, 0));
2161   if (inner == NULL_TREE)
2162     return NULL_TREE;
2163   while ((CONVERT_EXPR_P (inner) || TREE_CODE (inner) == NON_LVALUE_EXPR)
2164          && !error_operand_p (inner)
2165          && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
2166              == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (inner, 0)))))
2167     inner = TREE_OPERAND (inner, 0);
2168   if (TREE_CODE (inner) != CONSTRUCTOR)
2169     return NULL_TREE;
2170   tree field = TREE_OPERAND (expr, 1);
2171   unsigned HOST_WIDE_INT cidx;
2172   tree cfield, cvalue;
2173   bool have_other_init = false;
2174   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (inner), cidx, cfield, cvalue)
2175     {
2176       if (cfield == field)
2177         return cvalue;
2178       have_other_init = true;
2179     }
2180   if (TREE_CODE (TREE_TYPE (inner)) == UNION_TYPE
2181       && (have_other_init || field != TYPE_FIELDS (TREE_TYPE (inner))))
2182     return NULL_TREE;
2183   /* Return a default initializer.  */
2184   if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (expr)))
2185     return build_constructor (TREE_TYPE (expr), NULL);
2186   return build_zero_cst (TREE_TYPE (expr));
2187 }
2188
2189 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2190    including converting functions and arrays to pointers if CONVERT_P.
2191    If READ_P, also mark the expression as having been read.  If
2192    FOR_INIT, constexpr expressions of structure and union type should
2193    be replaced by the corresponding CONSTRUCTOR; otherwise, only
2194    constexpr scalars (including elements of structures and unions) are
2195    replaced by their initializers.  */
2196
2197 struct c_expr
2198 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2199                           bool convert_p, bool read_p, bool for_init)
2200 {
2201   bool force_non_npc = false;
2202   if (read_p)
2203     mark_exp_read (exp.value);
2204   if (convert_p)
2205     exp = default_function_array_conversion (loc, exp);
2206   if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2207     exp.value = require_complete_type (loc, exp.value);
2208   if (for_init || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (exp.value)))
2209     {
2210       tree init = maybe_get_constexpr_init (exp.value);
2211       if (init != NULL_TREE)
2212         {
2213           /* A named constant of pointer type or type nullptr_t is not
2214              a null pointer constant even if the initializer is
2215              one.  */
2216           if (TREE_CODE (init) == INTEGER_CST
2217               && !INTEGRAL_TYPE_P (TREE_TYPE (init))
2218               && integer_zerop (init))
2219             force_non_npc = true;
2220           exp.value = init;
2221         }
2222     }
2223   if (really_atomic_lvalue (exp.value))
2224     {
2225       vec<tree, va_gc> *params;
2226       tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2227       tree expr_type = TREE_TYPE (exp.value);
2228       tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2229       tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2230
2231       gcc_assert (TYPE_ATOMIC (expr_type));
2232
2233       /* Expansion of a generic atomic load may require an addition
2234          element, so allocate enough to prevent a resize.  */
2235       vec_alloc (params, 4);
2236
2237       /* Remove the qualifiers for the rest of the expressions and
2238          create the VAL temp variable to hold the RHS.  */
2239       nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2240       tmp = create_tmp_var_raw (nonatomic_type);
2241       tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2242       TREE_ADDRESSABLE (tmp) = 1;
2243       /* Do not disable warnings for TMP even though it's artificial.
2244          -Winvalid-memory-model depends on it.  */
2245
2246       /* Issue __atomic_load (&expr, &tmp, SEQ_CST);  */
2247       fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2248       params->quick_push (expr_addr);
2249       params->quick_push (tmp_addr);
2250       params->quick_push (seq_cst);
2251       func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2252
2253       /* EXPR is always read.  */
2254       mark_exp_read (exp.value);
2255
2256       /* Return tmp which contains the value loaded.  */
2257       exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2258                           NULL_TREE, NULL_TREE);
2259     }
2260   if (convert_p && !error_operand_p (exp.value)
2261       && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2262     exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2263   if (force_non_npc)
2264     exp.value = build1 (NOP_EXPR, TREE_TYPE (exp.value), exp.value);
2265   return exp;
2266 }
2267
2268 /* EXP is an expression of integer type.  Apply the integer promotions
2269    to it and return the promoted value.  */
2270
2271 tree
2272 perform_integral_promotions (tree exp)
2273 {
2274   tree type = TREE_TYPE (exp);
2275   enum tree_code code = TREE_CODE (type);
2276
2277   gcc_assert (INTEGRAL_TYPE_P (type));
2278
2279   /* Convert enums to the result of applying the integer promotions to
2280      their underlying type.  */
2281   if (code == ENUMERAL_TYPE)
2282     {
2283       type = ENUM_UNDERLYING_TYPE (type);
2284       if (c_promoting_integer_type_p (type))
2285         {
2286           if (TYPE_UNSIGNED (type)
2287               && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2288             type = unsigned_type_node;
2289           else
2290             type = integer_type_node;
2291         }
2292
2293       return convert (type, exp);
2294     }
2295
2296   /* ??? This should no longer be needed now bit-fields have their
2297      proper types.  */
2298   if (TREE_CODE (exp) == COMPONENT_REF
2299       && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2300       /* If it's thinner than an int, promote it like a
2301          c_promoting_integer_type_p, otherwise leave it alone.  */
2302       && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2303                            TYPE_PRECISION (integer_type_node)) < 0)
2304     return convert (integer_type_node, exp);
2305
2306   if (c_promoting_integer_type_p (type))
2307     {
2308       /* Preserve unsignedness if not really getting any wider.  */
2309       if (TYPE_UNSIGNED (type)
2310           && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2311         return convert (unsigned_type_node, exp);
2312
2313       return convert (integer_type_node, exp);
2314     }
2315
2316   return exp;
2317 }
2318
2319
2320 /* Perform default promotions for C data used in expressions.
2321    Enumeral types or short or char are converted to int.
2322    In addition, manifest constants symbols are replaced by their values.  */
2323
2324 tree
2325 default_conversion (tree exp)
2326 {
2327   tree orig_exp;
2328   tree type = TREE_TYPE (exp);
2329   enum tree_code code = TREE_CODE (type);
2330   tree promoted_type;
2331
2332   mark_exp_read (exp);
2333
2334   /* Functions and arrays have been converted during parsing.  */
2335   gcc_assert (code != FUNCTION_TYPE);
2336   if (code == ARRAY_TYPE)
2337     return exp;
2338
2339   /* Constants can be used directly unless they're not loadable.  */
2340   if (TREE_CODE (exp) == CONST_DECL)
2341     exp = DECL_INITIAL (exp);
2342
2343   /* Strip no-op conversions.  */
2344   orig_exp = exp;
2345   STRIP_TYPE_NOPS (exp);
2346
2347   copy_warning (exp, orig_exp);
2348
2349   if (code == VOID_TYPE)
2350     {
2351       error_at (EXPR_LOC_OR_LOC (exp, input_location),
2352                 "void value not ignored as it ought to be");
2353       return error_mark_node;
2354     }
2355
2356   exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2357   if (exp == error_mark_node)
2358     return error_mark_node;
2359
2360   promoted_type = targetm.promoted_type (type);
2361   if (promoted_type)
2362     return convert (promoted_type, exp);
2363
2364   if (INTEGRAL_TYPE_P (type))
2365     return perform_integral_promotions (exp);
2366
2367   return exp;
2368 }
2369 \f
2370 /* Look up COMPONENT in a structure or union TYPE.
2371
2372    If the component name is not found, returns NULL_TREE.  Otherwise,
2373    the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2374    stepping down the chain to the component, which is in the last
2375    TREE_VALUE of the list.  Normally the list is of length one, but if
2376    the component is embedded within (nested) anonymous structures or
2377    unions, the list steps down the chain to the component.  */
2378
2379 static tree
2380 lookup_field (tree type, tree component)
2381 {
2382   tree field;
2383
2384   /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2385      to the field elements.  Use a binary search on this array to quickly
2386      find the element.  Otherwise, do a linear search.  TYPE_LANG_SPECIFIC
2387      will always be set for structures which have many elements.
2388
2389      Duplicate field checking replaces duplicates with NULL_TREE so
2390      TYPE_LANG_SPECIFIC arrays are potentially no longer sorted.  In that
2391      case just iterate using DECL_CHAIN.  */
2392
2393   if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2394       && !seen_error ())
2395     {
2396       int bot, top, half;
2397       tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2398
2399       field = TYPE_FIELDS (type);
2400       bot = 0;
2401       top = TYPE_LANG_SPECIFIC (type)->s->len;
2402       while (top - bot > 1)
2403         {
2404           half = (top - bot + 1) >> 1;
2405           field = field_array[bot+half];
2406
2407           if (DECL_NAME (field) == NULL_TREE)
2408             {
2409               /* Step through all anon unions in linear fashion.  */
2410               while (DECL_NAME (field_array[bot]) == NULL_TREE)
2411                 {
2412                   field = field_array[bot++];
2413                   if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2414                     {
2415                       tree anon = lookup_field (TREE_TYPE (field), component);
2416
2417                       if (anon)
2418                         return tree_cons (NULL_TREE, field, anon);
2419
2420                       /* The Plan 9 compiler permits referring
2421                          directly to an anonymous struct/union field
2422                          using a typedef name.  */
2423                       if (flag_plan9_extensions
2424                           && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2425                           && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2426                               == TYPE_DECL)
2427                           && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2428                               == component))
2429                         break;
2430                     }
2431                 }
2432
2433               /* Entire record is only anon unions.  */
2434               if (bot > top)
2435                 return NULL_TREE;
2436
2437               /* Restart the binary search, with new lower bound.  */
2438               continue;
2439             }
2440
2441           if (DECL_NAME (field) == component)
2442             break;
2443           if (DECL_NAME (field) < component)
2444             bot += half;
2445           else
2446             top = bot + half;
2447         }
2448
2449       if (DECL_NAME (field_array[bot]) == component)
2450         field = field_array[bot];
2451       else if (DECL_NAME (field) != component)
2452         return NULL_TREE;
2453     }
2454   else
2455     {
2456       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2457         {
2458           if (DECL_NAME (field) == NULL_TREE
2459               && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2460             {
2461               tree anon = lookup_field (TREE_TYPE (field), component);
2462
2463               if (anon)
2464                 return tree_cons (NULL_TREE, field, anon);
2465
2466               /* The Plan 9 compiler permits referring directly to an
2467                  anonymous struct/union field using a typedef
2468                  name.  */
2469               if (flag_plan9_extensions
2470                   && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2471                   && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2472                   && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2473                       == component))
2474                 break;
2475             }
2476
2477           if (DECL_NAME (field) == component)
2478             break;
2479         }
2480
2481       if (field == NULL_TREE)
2482         return NULL_TREE;
2483     }
2484
2485   return tree_cons (NULL_TREE, field, NULL_TREE);
2486 }
2487
2488 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES.  */
2489
2490 static void
2491 lookup_field_fuzzy_find_candidates (tree type, tree component,
2492                                     vec<tree> *candidates)
2493 {
2494   tree field;
2495   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2496     {
2497       if (DECL_NAME (field) == NULL_TREE
2498           && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2499         lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2500                                             candidates);
2501
2502       if (DECL_NAME (field))
2503         candidates->safe_push (DECL_NAME (field));
2504     }
2505 }
2506
2507 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2508    rather than returning a TREE_LIST for an exact match.  */
2509
2510 static tree
2511 lookup_field_fuzzy (tree type, tree component)
2512 {
2513   gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2514
2515   /* First, gather a list of candidates.  */
2516   auto_vec <tree> candidates;
2517
2518   lookup_field_fuzzy_find_candidates (type, component,
2519                                       &candidates);
2520
2521   return find_closest_identifier (component, &candidates);
2522 }
2523
2524 /* Support function for build_component_ref's error-handling.
2525
2526    Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2527    struct or union, should we suggest "DATUM->COMPONENT" as a hint?  */
2528
2529 static bool
2530 should_suggest_deref_p (tree datum_type)
2531 {
2532   /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2533      allows "." for ptrs; we could be handling a failed attempt
2534      to access a property.  */
2535   if (c_dialect_objc ())
2536     return false;
2537
2538   /* Only suggest it for pointers...  */
2539   if (TREE_CODE (datum_type) != POINTER_TYPE)
2540     return false;
2541
2542   /* ...to structs/unions.  */
2543   tree underlying_type = TREE_TYPE (datum_type);
2544   enum tree_code code = TREE_CODE (underlying_type);
2545   if (code == RECORD_TYPE || code == UNION_TYPE)
2546     return true;
2547   else
2548     return false;
2549 }
2550
2551 /* Make an expression to refer to the COMPONENT field of structure or
2552    union value DATUM.  COMPONENT is an IDENTIFIER_NODE.  LOC is the
2553    location of the COMPONENT_REF.  COMPONENT_LOC is the location
2554    of COMPONENT.  ARROW_LOC is the location of the first -> operand if
2555    it is from -> operator.  */
2556
2557 tree
2558 build_component_ref (location_t loc, tree datum, tree component,
2559                      location_t component_loc, location_t arrow_loc)
2560 {
2561   tree type = TREE_TYPE (datum);
2562   enum tree_code code = TREE_CODE (type);
2563   tree field = NULL;
2564   tree ref;
2565   bool datum_lvalue = lvalue_p (datum);
2566
2567   if (!objc_is_public (datum, component))
2568     return error_mark_node;
2569
2570   /* Detect Objective-C property syntax object.property.  */
2571   if (c_dialect_objc ()
2572       && (ref = objc_maybe_build_component_ref (datum, component)))
2573     return ref;
2574
2575   /* See if there is a field or component with name COMPONENT.  */
2576
2577   if (code == RECORD_TYPE || code == UNION_TYPE)
2578     {
2579       if (!COMPLETE_TYPE_P (type))
2580         {
2581           c_incomplete_type_error (loc, NULL_TREE, type);
2582           return error_mark_node;
2583         }
2584
2585       field = lookup_field (type, component);
2586
2587       if (!field)
2588         {
2589           tree guessed_id = lookup_field_fuzzy (type, component);
2590           if (guessed_id)
2591             {
2592               /* Attempt to provide a fixit replacement hint, if
2593                  we have a valid range for the component.  */
2594               location_t reported_loc
2595                 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2596               gcc_rich_location rich_loc (reported_loc);
2597               if (component_loc != UNKNOWN_LOCATION)
2598                 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2599               error_at (&rich_loc,
2600                         "%qT has no member named %qE; did you mean %qE?",
2601                         type, component, guessed_id);
2602             }
2603           else
2604             error_at (loc, "%qT has no member named %qE", type, component);
2605           return error_mark_node;
2606         }
2607
2608       /* Accessing elements of atomic structures or unions is undefined
2609          behavior (C11 6.5.2.3#5).  */
2610       if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2611         {
2612           if (code == RECORD_TYPE)
2613             warning_at (loc, 0, "accessing a member %qE of an atomic "
2614                         "structure %qE", component, datum);
2615           else
2616             warning_at (loc, 0, "accessing a member %qE of an atomic "
2617                         "union %qE", component, datum);
2618         }
2619
2620       /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2621          This might be better solved in future the way the C++ front
2622          end does it - by giving the anonymous entities each a
2623          separate name and type, and then have build_component_ref
2624          recursively call itself.  We can't do that here.  */
2625       do
2626         {
2627           tree subdatum = TREE_VALUE (field);
2628           int quals;
2629           tree subtype;
2630           bool use_datum_quals;
2631
2632           if (TREE_TYPE (subdatum) == error_mark_node)
2633             return error_mark_node;
2634
2635           /* If this is an rvalue, it does not have qualifiers in C
2636              standard terms and we must avoid propagating such
2637              qualifiers down to a non-lvalue array that is then
2638              converted to a pointer.  */
2639           use_datum_quals = (datum_lvalue
2640                              || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2641
2642           quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2643           if (use_datum_quals)
2644             quals |= TYPE_QUALS (TREE_TYPE (datum));
2645           subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2646
2647           ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2648                         NULL_TREE);
2649           SET_EXPR_LOCATION (ref, loc);
2650           if (TREE_READONLY (subdatum)
2651               || (use_datum_quals && TREE_READONLY (datum)))
2652             TREE_READONLY (ref) = 1;
2653           if (TREE_THIS_VOLATILE (subdatum)
2654               || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2655             TREE_THIS_VOLATILE (ref) = 1;
2656
2657           if (TREE_UNAVAILABLE (subdatum))
2658             error_unavailable_use (subdatum, NULL_TREE);
2659           else if (TREE_DEPRECATED (subdatum))
2660             warn_deprecated_use (subdatum, NULL_TREE);
2661
2662           datum = ref;
2663
2664           field = TREE_CHAIN (field);
2665         }
2666       while (field);
2667
2668       return ref;
2669     }
2670   else if (should_suggest_deref_p (type))
2671     {
2672       /* Special-case the error message for "ptr.field" for the case
2673          where the user has confused "." vs "->".  */
2674       rich_location richloc (line_table, loc);
2675       if (TREE_CODE (datum) == INDIRECT_REF && arrow_loc != UNKNOWN_LOCATION)
2676         {
2677           richloc.add_fixit_insert_before (arrow_loc, "(*");
2678           richloc.add_fixit_insert_after (arrow_loc, ")");
2679           error_at (&richloc,
2680                     "%qE is a pointer to pointer; did you mean to dereference "
2681                     "it before applying %<->%> to it?",
2682                     TREE_OPERAND (datum, 0));
2683         }
2684       else
2685         {
2686           /* "loc" should be the "." token.  */
2687           richloc.add_fixit_replace ("->");
2688           error_at (&richloc,
2689                     "%qE is a pointer; did you mean to use %<->%>?",
2690                     datum);
2691         }
2692       return error_mark_node;
2693     }
2694   else if (code != ERROR_MARK)
2695     error_at (loc,
2696               "request for member %qE in something not a structure or union",
2697               component);
2698
2699   return error_mark_node;
2700 }
2701 \f
2702 /* Given an expression PTR for a pointer, return an expression
2703    for the value pointed to.
2704    ERRORSTRING is the name of the operator to appear in error messages.
2705
2706    LOC is the location to use for the generated tree.  */
2707
2708 tree
2709 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2710 {
2711   tree pointer = default_conversion (ptr);
2712   tree type = TREE_TYPE (pointer);
2713   tree ref;
2714
2715   if (TREE_CODE (type) == POINTER_TYPE)
2716     {
2717       if (CONVERT_EXPR_P (pointer)
2718           || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2719         {
2720           /* If a warning is issued, mark it to avoid duplicates from
2721              the backend.  This only needs to be done at
2722              warn_strict_aliasing > 2.  */
2723           if (warn_strict_aliasing > 2)
2724             if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2725                                          type, TREE_OPERAND (pointer, 0)))
2726               suppress_warning (pointer, OPT_Wstrict_aliasing_);
2727         }
2728
2729       if (TREE_CODE (pointer) == ADDR_EXPR
2730           && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2731               == TREE_TYPE (type)))
2732         {
2733           ref = TREE_OPERAND (pointer, 0);
2734           protected_set_expr_location (ref, loc);
2735           return ref;
2736         }
2737       else
2738         {
2739           tree t = TREE_TYPE (type);
2740
2741           ref = build1 (INDIRECT_REF, t, pointer);
2742
2743           if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2744             warning_at (loc, 0, "dereferencing %<void *%> pointer");
2745
2746           /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2747              so that we get the proper error message if the result is used
2748              to assign to.  Also, &* is supposed to be a no-op.
2749              And ANSI C seems to specify that the type of the result
2750              should be the const type.  */
2751           /* A de-reference of a pointer to const is not a const.  It is valid
2752              to change it via some other pointer.  */
2753           TREE_READONLY (ref) = TYPE_READONLY (t);
2754           TREE_SIDE_EFFECTS (ref)
2755             = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2756           TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2757           protected_set_expr_location (ref, loc);
2758           return ref;
2759         }
2760     }
2761   else if (TREE_CODE (pointer) != ERROR_MARK)
2762     invalid_indirection_error (loc, type, errstring);
2763
2764   return error_mark_node;
2765 }
2766
2767 /* This handles expressions of the form "a[i]", which denotes
2768    an array reference.
2769
2770    This is logically equivalent in C to *(a+i), but we may do it differently.
2771    If A is a variable or a member, we generate a primitive ARRAY_REF.
2772    This avoids forcing the array out of registers, and can work on
2773    arrays that are not lvalues (for example, members of structures returned
2774    by functions).
2775
2776    For vector types, allow vector[i] but not i[vector], and create
2777    *(((type*)&vectortype) + i) for the expression.
2778
2779    LOC is the location to use for the returned expression.  */
2780
2781 tree
2782 build_array_ref (location_t loc, tree array, tree index)
2783 {
2784   tree ret;
2785   bool swapped = false;
2786   if (TREE_TYPE (array) == error_mark_node
2787       || TREE_TYPE (index) == error_mark_node)
2788     return error_mark_node;
2789
2790   if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2791       && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2792       /* Allow vector[index] but not index[vector].  */
2793       && !gnu_vector_type_p (TREE_TYPE (array)))
2794     {
2795       if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2796           && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2797         {
2798           error_at (loc,
2799             "subscripted value is neither array nor pointer nor vector");
2800
2801           return error_mark_node;
2802         }
2803       std::swap (array, index);
2804       swapped = true;
2805     }
2806
2807   if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2808     {
2809       error_at (loc, "array subscript is not an integer");
2810       return error_mark_node;
2811     }
2812
2813   if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2814     {
2815       error_at (loc, "subscripted value is pointer to function");
2816       return error_mark_node;
2817     }
2818
2819   /* ??? Existing practice has been to warn only when the char
2820      index is syntactically the index, not for char[array].  */
2821   if (!swapped)
2822      warn_array_subscript_with_type_char (loc, index);
2823
2824   /* Apply default promotions *after* noticing character types.  */
2825   index = default_conversion (index);
2826   if (index == error_mark_node)
2827     return error_mark_node;
2828
2829   gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2830
2831   bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2832   bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2833
2834   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2835     {
2836       tree rval, type;
2837
2838       /* An array that is indexed by a non-constant
2839          cannot be stored in a register; we must be able to do
2840          address arithmetic on its address.
2841          Likewise an array of elements of variable size.  */
2842       if (TREE_CODE (index) != INTEGER_CST
2843           || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2844               && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2845         {
2846           if (!c_mark_addressable (array, true))
2847             return error_mark_node;
2848         }
2849       /* An array that is indexed by a constant value which is not within
2850          the array bounds cannot be stored in a register either; because we
2851          would get a crash in store_bit_field/extract_bit_field when trying
2852          to access a non-existent part of the register.  */
2853       if (TREE_CODE (index) == INTEGER_CST
2854           && TYPE_DOMAIN (TREE_TYPE (array))
2855           && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2856         {
2857           if (!c_mark_addressable (array))
2858             return error_mark_node;
2859         }
2860
2861       if ((pedantic || warn_c90_c99_compat)
2862           && ! was_vector)
2863         {
2864           tree foo = array;
2865           while (TREE_CODE (foo) == COMPONENT_REF)
2866             foo = TREE_OPERAND (foo, 0);
2867           if (VAR_P (foo) && C_DECL_REGISTER (foo))
2868             pedwarn (loc, OPT_Wpedantic,
2869                      "ISO C forbids subscripting %<register%> array");
2870           else if (!lvalue_p (foo))
2871             pedwarn_c90 (loc, OPT_Wpedantic,
2872                          "ISO C90 forbids subscripting non-lvalue "
2873                          "array");
2874         }
2875
2876       type = TREE_TYPE (TREE_TYPE (array));
2877       rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2878       /* Array ref is const/volatile if the array elements are
2879          or if the array is.  */
2880       TREE_READONLY (rval)
2881         |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2882             | TREE_READONLY (array));
2883       TREE_SIDE_EFFECTS (rval)
2884         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2885             | TREE_SIDE_EFFECTS (array));
2886       TREE_THIS_VOLATILE (rval)
2887         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2888             /* This was added by rms on 16 Nov 91.
2889                It fixes  vol struct foo *a;  a->elts[1]
2890                in an inline function.
2891                Hope it doesn't break something else.  */
2892             | TREE_THIS_VOLATILE (array));
2893       ret = require_complete_type (loc, rval);
2894       protected_set_expr_location (ret, loc);
2895       if (non_lvalue)
2896         ret = non_lvalue_loc (loc, ret);
2897       return ret;
2898     }
2899   else
2900     {
2901       tree ar = default_conversion (array);
2902
2903       if (ar == error_mark_node)
2904         return ar;
2905
2906       gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2907       gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2908
2909       ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2910                                                       index, false),
2911                                 RO_ARRAY_INDEXING);
2912       if (non_lvalue)
2913         ret = non_lvalue_loc (loc, ret);
2914       return ret;
2915     }
2916 }
2917 \f
2918 /* Build an external reference to identifier ID.  FUN indicates
2919    whether this will be used for a function call.  LOC is the source
2920    location of the identifier.  This sets *TYPE to the type of the
2921    identifier, which is not the same as the type of the returned value
2922    for CONST_DECLs defined as enum constants.  If the type of the
2923    identifier is not available, *TYPE is set to NULL.  */
2924 tree
2925 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2926 {
2927   tree ref;
2928   tree decl = lookup_name (id);
2929
2930   /* In Objective-C, an instance variable (ivar) may be preferred to
2931      whatever lookup_name() found.  */
2932   decl = objc_lookup_ivar (decl, id);
2933
2934   *type = NULL;
2935   if (decl && decl != error_mark_node)
2936     {
2937       ref = decl;
2938       *type = TREE_TYPE (ref);
2939       if (DECL_P (decl) && C_DECL_UNDERSPECIFIED (decl))
2940         error_at (loc, "underspecified %qD referenced in its initializer",
2941                   decl);
2942     }
2943   else if (fun)
2944     /* Implicit function declaration.  */
2945     ref = implicitly_declare (loc, id);
2946   else if (decl == error_mark_node)
2947     /* Don't complain about something that's already been
2948        complained about.  */
2949     return error_mark_node;
2950   else
2951     {
2952       undeclared_variable (loc, id);
2953       return error_mark_node;
2954     }
2955
2956   if (TREE_TYPE (ref) == error_mark_node)
2957     return error_mark_node;
2958
2959   if (TREE_UNAVAILABLE (ref))
2960     error_unavailable_use (ref, NULL_TREE);
2961   else if (TREE_DEPRECATED (ref))
2962     warn_deprecated_use (ref, NULL_TREE);
2963
2964   /* Recursive call does not count as usage.  */
2965   if (ref != current_function_decl)
2966     {
2967       TREE_USED (ref) = 1;
2968     }
2969
2970   if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2971     {
2972       if (!in_sizeof && !in_typeof)
2973         C_DECL_USED (ref) = 1;
2974       else if (DECL_INITIAL (ref) == NULL_TREE
2975                && DECL_EXTERNAL (ref)
2976                && !TREE_PUBLIC (ref))
2977         record_maybe_used_decl (ref);
2978     }
2979
2980   if (TREE_CODE (ref) == CONST_DECL)
2981     {
2982       used_types_insert (TREE_TYPE (ref));
2983
2984       if (warn_cxx_compat
2985           && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2986           && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2987         {
2988           warning_at (loc, OPT_Wc___compat,
2989                       ("enum constant defined in struct or union "
2990                        "is not visible in C++"));
2991           inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2992         }
2993
2994       ref = DECL_INITIAL (ref);
2995       TREE_CONSTANT (ref) = 1;
2996     }
2997   else if (current_function_decl != NULL_TREE
2998            && !DECL_FILE_SCOPE_P (current_function_decl)
2999            && (VAR_OR_FUNCTION_DECL_P (ref)
3000                || TREE_CODE (ref) == PARM_DECL))
3001     {
3002       tree context = decl_function_context (ref);
3003
3004       if (context != NULL_TREE && context != current_function_decl)
3005         DECL_NONLOCAL (ref) = 1;
3006     }
3007   /* C99 6.7.4p3: An inline definition of a function with external
3008      linkage ... shall not contain a reference to an identifier with
3009      internal linkage.  */
3010   else if (current_function_decl != NULL_TREE
3011            && DECL_DECLARED_INLINE_P (current_function_decl)
3012            && DECL_EXTERNAL (current_function_decl)
3013            && VAR_OR_FUNCTION_DECL_P (ref)
3014            && (!VAR_P (ref) || TREE_STATIC (ref))
3015            && ! TREE_PUBLIC (ref)
3016            && DECL_CONTEXT (ref) != current_function_decl)
3017     record_inline_static (loc, current_function_decl, ref,
3018                           csi_internal);
3019
3020   return ref;
3021 }
3022
3023 /* Record details of decls possibly used inside sizeof or typeof.  */
3024 struct maybe_used_decl
3025 {
3026   /* The decl.  */
3027   tree decl;
3028   /* The level seen at (in_sizeof + in_typeof).  */
3029   int level;
3030   /* The next one at this level or above, or NULL.  */
3031   struct maybe_used_decl *next;
3032 };
3033
3034 static struct maybe_used_decl *maybe_used_decls;
3035
3036 /* Record that DECL, an undefined static function reference seen
3037    inside sizeof or typeof, might be used if the operand of sizeof is
3038    a VLA type or the operand of typeof is a variably modified
3039    type.  */
3040
3041 static void
3042 record_maybe_used_decl (tree decl)
3043 {
3044   struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
3045   t->decl = decl;
3046   t->level = in_sizeof + in_typeof;
3047   t->next = maybe_used_decls;
3048   maybe_used_decls = t;
3049 }
3050
3051 /* Pop the stack of decls possibly used inside sizeof or typeof.  If
3052    USED is false, just discard them.  If it is true, mark them used
3053    (if no longer inside sizeof or typeof) or move them to the next
3054    level up (if still inside sizeof or typeof).  */
3055
3056 void
3057 pop_maybe_used (bool used)
3058 {
3059   struct maybe_used_decl *p = maybe_used_decls;
3060   int cur_level = in_sizeof + in_typeof;
3061   while (p && p->level > cur_level)
3062     {
3063       if (used)
3064         {
3065           if (cur_level == 0)
3066             C_DECL_USED (p->decl) = 1;
3067           else
3068             p->level = cur_level;
3069         }
3070       p = p->next;
3071     }
3072   if (!used || cur_level == 0)
3073     maybe_used_decls = p;
3074 }
3075
3076 /* Return the result of sizeof applied to EXPR.  */
3077
3078 struct c_expr
3079 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
3080 {
3081   struct c_expr ret;
3082   if (expr.value == error_mark_node)
3083     {
3084       ret.value = error_mark_node;
3085       ret.original_code = ERROR_MARK;
3086       ret.original_type = NULL;
3087       ret.m_decimal = 0;
3088       pop_maybe_used (false);
3089     }
3090   else
3091     {
3092       bool expr_const_operands = true;
3093
3094       if (TREE_CODE (expr.value) == PARM_DECL
3095           && C_ARRAY_PARAMETER (expr.value))
3096         {
3097           auto_diagnostic_group d;
3098           if (warning_at (loc, OPT_Wsizeof_array_argument,
3099                           "%<sizeof%> on array function parameter %qE will "
3100                           "return size of %qT", expr.value,
3101                           TREE_TYPE (expr.value)))
3102             inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3103         }
3104       tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3105                                        &expr_const_operands);
3106       ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3107       c_last_sizeof_arg = expr.value;
3108       c_last_sizeof_loc = loc;
3109       ret.original_code = SIZEOF_EXPR;
3110       ret.original_type = NULL;
3111       ret.m_decimal = 0;
3112       if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3113         {
3114           /* sizeof is evaluated when given a vla (C99 6.5.3.4p2).  */
3115           ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3116                               folded_expr, ret.value);
3117           C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3118           SET_EXPR_LOCATION (ret.value, loc);
3119         }
3120       pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3121     }
3122   return ret;
3123 }
3124
3125 /* Return the result of sizeof applied to T, a structure for the type
3126    name passed to sizeof (rather than the type itself).  LOC is the
3127    location of the original expression.  */
3128
3129 struct c_expr
3130 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3131 {
3132   tree type;
3133   struct c_expr ret;
3134   tree type_expr = NULL_TREE;
3135   bool type_expr_const = true;
3136   type = groktypename (t, &type_expr, &type_expr_const);
3137   ret.value = c_sizeof (loc, type);
3138   c_last_sizeof_arg = type;
3139   c_last_sizeof_loc = loc;
3140   ret.original_code = SIZEOF_EXPR;
3141   ret.original_type = NULL;
3142   ret.m_decimal = 0;
3143   if (type == error_mark_node)
3144     {
3145       ret.value = error_mark_node;
3146       ret.original_code = ERROR_MARK;
3147     }
3148   else
3149   if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3150       && C_TYPE_VARIABLE_SIZE (type))
3151     {
3152       /* If the type is a [*] array, it is a VLA but is represented as
3153          having a size of zero.  In such a case we must ensure that
3154          the result of sizeof does not get folded to a constant by
3155          c_fully_fold, because if the size is evaluated the result is
3156          not constant and so constraints on zero or negative size
3157          arrays must not be applied when this sizeof call is inside
3158          another array declarator.  */
3159       if (!type_expr)
3160         type_expr = integer_zero_node;
3161       ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3162                           type_expr, ret.value);
3163       C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3164     }
3165   pop_maybe_used (type != error_mark_node
3166                   ? C_TYPE_VARIABLE_SIZE (type) : false);
3167   return ret;
3168 }
3169
3170 /* Build a function call to function FUNCTION with parameters PARAMS.
3171    The function call is at LOC.
3172    PARAMS is a list--a chain of TREE_LIST nodes--in which the
3173    TREE_VALUE of each node is a parameter-expression.
3174    FUNCTION's data type may be a function type or a pointer-to-function.  */
3175
3176 tree
3177 build_function_call (location_t loc, tree function, tree params)
3178 {
3179   vec<tree, va_gc> *v;
3180   tree ret;
3181
3182   vec_alloc (v, list_length (params));
3183   for (; params; params = TREE_CHAIN (params))
3184     v->quick_push (TREE_VALUE (params));
3185   ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3186   vec_free (v);
3187   return ret;
3188 }
3189
3190 /* Give a note about the location of the declaration of DECL.  */
3191
3192 static void
3193 inform_declaration (tree decl)
3194 {
3195   if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3196                || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3197     inform (DECL_SOURCE_LOCATION (decl), "declared here");
3198 }
3199
3200 /* Build a function call to function FUNCTION with parameters PARAMS.
3201    If FUNCTION is the result of resolving an overloaded target built-in,
3202    ORIG_FUNDECL is the original function decl, otherwise it is null.
3203    ORIGTYPES, if not NULL, is a vector of types; each element is
3204    either NULL or the original type of the corresponding element in
3205    PARAMS.  The original type may differ from TREE_TYPE of the
3206    parameter for enums.  FUNCTION's data type may be a function type
3207    or pointer-to-function.  This function changes the elements of
3208    PARAMS.  */
3209
3210 tree
3211 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3212                          tree function, vec<tree, va_gc> *params,
3213                          vec<tree, va_gc> *origtypes, tree orig_fundecl)
3214 {
3215   tree fntype, fundecl = NULL_TREE;
3216   tree name = NULL_TREE, result;
3217   tree tem;
3218   int nargs;
3219   tree *argarray;
3220
3221
3222   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
3223   STRIP_TYPE_NOPS (function);
3224
3225   /* Convert anything with function type to a pointer-to-function.  */
3226   if (TREE_CODE (function) == FUNCTION_DECL)
3227     {
3228       name = DECL_NAME (function);
3229
3230       if (flag_tm)
3231         tm_malloc_replacement (function);
3232       fundecl = function;
3233       if (!orig_fundecl)
3234         orig_fundecl = fundecl;
3235       /* Atomic functions have type checking/casting already done.  They are 
3236          often rewritten and don't match the original parameter list.  */
3237       if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3238         origtypes = NULL;
3239     }
3240   if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3241     function = function_to_pointer_conversion (loc, function);
3242
3243   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3244      expressions, like those used for ObjC messenger dispatches.  */
3245   if (params && !params->is_empty ())
3246     function = objc_rewrite_function_call (function, (*params)[0]);
3247
3248   function = c_fully_fold (function, false, NULL);
3249
3250   fntype = TREE_TYPE (function);
3251
3252   if (TREE_CODE (fntype) == ERROR_MARK)
3253     return error_mark_node;
3254
3255   if (!(TREE_CODE (fntype) == POINTER_TYPE
3256         && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3257     {
3258       if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3259         error_at (loc,
3260                   "called object %qE is not a function or function pointer",
3261                   function);
3262       else if (DECL_P (function))
3263         {
3264           error_at (loc,
3265                     "called object %qD is not a function or function pointer",
3266                     function);
3267           inform_declaration (function);
3268         }
3269       else
3270         error_at (loc,
3271                   "called object is not a function or function pointer");
3272       return error_mark_node;
3273     }
3274
3275   if (fundecl && TREE_THIS_VOLATILE (fundecl))
3276     current_function_returns_abnormally = 1;
3277
3278   /* fntype now gets the type of function pointed to.  */
3279   fntype = TREE_TYPE (fntype);
3280   tree return_type = TREE_TYPE (fntype);
3281
3282   /* Convert the parameters to the types declared in the
3283      function prototype, or apply default promotions.  */
3284
3285   nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3286                              origtypes, function, fundecl);
3287   if (nargs < 0)
3288     return error_mark_node;
3289
3290   /* Check that the function is called through a compatible prototype.
3291      If it is not, warn.  */
3292   if (CONVERT_EXPR_P (function)
3293       && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3294       && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3295       && !comptypes (fntype, TREE_TYPE (tem)))
3296     {
3297       /* This situation leads to run-time undefined behavior.  We can't,
3298          therefore, simply error unless we can prove that all possible
3299          executions of the program must execute the code.  */
3300       warning_at (loc, 0, "function called through a non-compatible type");
3301
3302       if (VOID_TYPE_P (return_type)
3303           && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3304         pedwarn (loc, 0,
3305                  "function with qualified void return type called");
3306      }
3307
3308   argarray = vec_safe_address (params);
3309
3310   /* Check that arguments to builtin functions match the expectations.  */
3311   if (fundecl
3312       && fndecl_built_in_p (fundecl)
3313       && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3314                                             orig_fundecl, nargs, argarray))
3315     return error_mark_node;
3316
3317   /* Check that the arguments to the function are valid.  */
3318   bool warned_p = check_function_arguments (loc, fundecl, fntype,
3319                                             nargs, argarray, &arg_loc);
3320
3321   if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED
3322       && !VOID_TYPE_P (return_type))
3323     return_type = c_build_qualified_type (return_type, TYPE_UNQUALIFIED);
3324   if (name != NULL_TREE
3325       && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3326     {
3327       if (require_constant_value)
3328         result
3329           = fold_build_call_array_initializer_loc (loc, return_type,
3330                                                    function, nargs, argarray);
3331       else
3332         result = fold_build_call_array_loc (loc, return_type,
3333                                             function, nargs, argarray);
3334       if (TREE_CODE (result) == NOP_EXPR
3335           && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3336         STRIP_TYPE_NOPS (result);
3337     }
3338   else
3339     result = build_call_array_loc (loc, return_type,
3340                                    function, nargs, argarray);
3341   /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3342      later.  */
3343   if (warned_p && TREE_CODE (result) == CALL_EXPR)
3344     suppress_warning (result, OPT_Wnonnull);
3345
3346   /* In this improbable scenario, a nested function returns a VM type.
3347      Create a TARGET_EXPR so that the call always has a LHS, much as
3348      what the C++ FE does for functions returning non-PODs.  */
3349   if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3350     {
3351       tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3352       result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3353                        NULL_TREE, NULL_TREE);
3354     }
3355
3356   if (VOID_TYPE_P (TREE_TYPE (result)))
3357     {
3358       if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3359         pedwarn (loc, 0,
3360                  "function with qualified void return type called");
3361       return result;
3362     }
3363   return require_complete_type (loc, result);
3364 }
3365
3366 /* Like build_function_call_vec, but call also resolve_overloaded_builtin.  */
3367
3368 tree
3369 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3370                            tree function, vec<tree, va_gc> *params,
3371                            vec<tree, va_gc> *origtypes)
3372 {
3373   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
3374   STRIP_TYPE_NOPS (function);
3375
3376   /* Convert anything with function type to a pointer-to-function.  */
3377   if (TREE_CODE (function) == FUNCTION_DECL)
3378     {
3379       /* Implement type-directed function overloading for builtins.
3380          resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3381          handle all the type checking.  The result is a complete expression
3382          that implements this function call.  */
3383       tree tem = resolve_overloaded_builtin (loc, function, params);
3384       if (tem)
3385         return tem;
3386     }
3387   return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3388 }
3389 \f
3390 /* Helper for convert_arguments called to convert the VALue of argument
3391    number ARGNUM from ORIGTYPE to the corresponding parameter number
3392    PARMNUM and TYPE.
3393    PLOC is the location where the conversion is being performed.
3394    FUNCTION and FUNDECL are the same as in convert_arguments.
3395    VALTYPE is the original type of VAL before the conversion and,
3396    for EXCESS_PRECISION_EXPR, the operand of the expression.
3397    NPC is true if VAL represents the null pointer constant (VAL itself
3398    will have been folded to an integer constant).
3399    RNAME is the same as FUNCTION except in Objective C when it's
3400    the function selector.
3401    EXCESS_PRECISION is true when VAL was originally represented
3402    as EXCESS_PRECISION_EXPR.
3403    WARNOPT is the same as in convert_for_assignment.  */
3404
3405 static tree
3406 convert_argument (location_t ploc, tree function, tree fundecl,
3407                   tree type, tree origtype, tree val, tree valtype,
3408                   bool npc, tree rname, int parmnum, int argnum,
3409                   bool excess_precision, int warnopt)
3410 {
3411   /* Formal parm type is specified by a function prototype.  */
3412
3413   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3414     {
3415       error_at (ploc, "type of formal parameter %d is incomplete",
3416                 parmnum + 1);
3417       return val;
3418     }
3419
3420   /* Optionally warn about conversions that differ from the default
3421      conversions.  */
3422   if (warn_traditional_conversion || warn_traditional)
3423     {
3424       unsigned int formal_prec = TYPE_PRECISION (type);
3425
3426       if (INTEGRAL_TYPE_P (type)
3427           && TREE_CODE (valtype) == REAL_TYPE)
3428         warning_at (ploc, OPT_Wtraditional_conversion,
3429                     "passing argument %d of %qE as integer rather "
3430                     "than floating due to prototype",
3431                     argnum, rname);
3432       if (INTEGRAL_TYPE_P (type)
3433           && TREE_CODE (valtype) == COMPLEX_TYPE)
3434         warning_at (ploc, OPT_Wtraditional_conversion,
3435                     "passing argument %d of %qE as integer rather "
3436                     "than complex due to prototype",
3437                     argnum, rname);
3438       else if (TREE_CODE (type) == COMPLEX_TYPE
3439                && TREE_CODE (valtype) == REAL_TYPE)
3440         warning_at (ploc, OPT_Wtraditional_conversion,
3441                     "passing argument %d of %qE as complex rather "
3442                     "than floating due to prototype",
3443                     argnum, rname);
3444       else if (TREE_CODE (type) == REAL_TYPE
3445                && INTEGRAL_TYPE_P (valtype))
3446         warning_at (ploc, OPT_Wtraditional_conversion,
3447                     "passing argument %d of %qE as floating rather "
3448                     "than integer due to prototype",
3449                     argnum, rname);
3450       else if (TREE_CODE (type) == COMPLEX_TYPE
3451                && INTEGRAL_TYPE_P (valtype))
3452         warning_at (ploc, OPT_Wtraditional_conversion,
3453                     "passing argument %d of %qE as complex rather "
3454                     "than integer due to prototype",
3455                     argnum, rname);
3456       else if (TREE_CODE (type) == REAL_TYPE
3457                && TREE_CODE (valtype) == COMPLEX_TYPE)
3458         warning_at (ploc, OPT_Wtraditional_conversion,
3459                     "passing argument %d of %qE as floating rather "
3460                     "than complex due to prototype",
3461                     argnum, rname);
3462       /* ??? At some point, messages should be written about
3463          conversions between complex types, but that's too messy
3464          to do now.  */
3465       else if (TREE_CODE (type) == REAL_TYPE
3466                && TREE_CODE (valtype) == REAL_TYPE)
3467         {
3468           /* Warn if any argument is passed as `float',
3469              since without a prototype it would be `double'.  */
3470           if (formal_prec == TYPE_PRECISION (float_type_node)
3471               && type != dfloat32_type_node)
3472             warning_at (ploc, 0,
3473                         "passing argument %d of %qE as %<float%> "
3474                         "rather than %<double%> due to prototype",
3475                         argnum, rname);
3476
3477           /* Warn if mismatch between argument and prototype
3478              for decimal float types.  Warn of conversions with
3479              binary float types and of precision narrowing due to
3480              prototype.  */
3481           else if (type != valtype
3482                    && (type == dfloat32_type_node
3483                        || type == dfloat64_type_node
3484                        || type == dfloat128_type_node
3485                        || valtype == dfloat32_type_node
3486                        || valtype == dfloat64_type_node
3487                        || valtype == dfloat128_type_node)
3488                    && (formal_prec
3489                        <= TYPE_PRECISION (valtype)
3490                        || (type == dfloat128_type_node
3491                            && (valtype
3492                                != dfloat64_type_node
3493                                && (valtype
3494                                    != dfloat32_type_node)))
3495                        || (type == dfloat64_type_node
3496                            && (valtype
3497                                != dfloat32_type_node))))
3498             warning_at (ploc, 0,
3499                         "passing argument %d of %qE as %qT "
3500                         "rather than %qT due to prototype",
3501                         argnum, rname, type, valtype);
3502
3503         }
3504       /* Detect integer changing in width or signedness.
3505          These warnings are only activated with
3506          -Wtraditional-conversion, not with -Wtraditional.  */
3507       else if (warn_traditional_conversion
3508                && INTEGRAL_TYPE_P (type)
3509                && INTEGRAL_TYPE_P (valtype))
3510         {
3511           tree would_have_been = default_conversion (val);
3512           tree type1 = TREE_TYPE (would_have_been);
3513
3514           if (val == error_mark_node)
3515             /* VAL could have been of incomplete type.  */;
3516           else if (TREE_CODE (type) == ENUMERAL_TYPE
3517                    && (TYPE_MAIN_VARIANT (type)
3518                        == TYPE_MAIN_VARIANT (valtype)))
3519             /* No warning if function asks for enum
3520                and the actual arg is that enum type.  */
3521             ;
3522           else if (formal_prec != TYPE_PRECISION (type1))
3523             warning_at (ploc, OPT_Wtraditional_conversion,
3524                         "passing argument %d of %qE "
3525                         "with different width due to prototype",
3526                         argnum, rname);
3527           else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3528             ;
3529           /* Don't complain if the formal parameter type
3530              is an enum, because we can't tell now whether
3531              the value was an enum--even the same enum.  */
3532           else if (TREE_CODE (type) == ENUMERAL_TYPE)
3533             ;
3534           else if (TREE_CODE (val) == INTEGER_CST
3535                    && int_fits_type_p (val, type))
3536             /* Change in signedness doesn't matter
3537                if a constant value is unaffected.  */
3538             ;
3539           /* If the value is extended from a narrower
3540              unsigned type, it doesn't matter whether we
3541              pass it as signed or unsigned; the value
3542              certainly is the same either way.  */
3543           else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3544                    && TYPE_UNSIGNED (valtype))
3545             ;
3546           else if (TYPE_UNSIGNED (type))
3547             warning_at (ploc, OPT_Wtraditional_conversion,
3548                         "passing argument %d of %qE "
3549                         "as unsigned due to prototype",
3550                         argnum, rname);
3551           else
3552             warning_at (ploc, OPT_Wtraditional_conversion,
3553                         "passing argument %d of %qE "
3554                         "as signed due to prototype",
3555                         argnum, rname);
3556         }
3557     }
3558
3559   /* Possibly restore an EXCESS_PRECISION_EXPR for the
3560      sake of better warnings from convert_and_check.  */
3561   if (excess_precision)
3562     val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3563
3564   tree parmval = convert_for_assignment (ploc, ploc, type,
3565                                          val, origtype, ic_argpass,
3566                                          npc, fundecl, function,
3567                                          parmnum + 1, warnopt);
3568
3569   if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3570       && INTEGRAL_TYPE_P (type)
3571       && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3572     parmval = default_conversion (parmval);
3573
3574   return parmval;
3575 }
3576
3577 /* Convert the argument expressions in the vector VALUES
3578    to the types in the list TYPELIST.
3579
3580    If TYPELIST is exhausted, or when an element has NULL as its type,
3581    perform the default conversions.
3582
3583    ORIGTYPES is the original types of the expressions in VALUES.  This
3584    holds the type of enum values which have been converted to integral
3585    types.  It may be NULL.
3586
3587    FUNCTION is a tree for the called function.  It is used only for
3588    error messages, where it is formatted with %qE.
3589
3590    This is also where warnings about wrong number of args are generated.
3591
3592    ARG_LOC are locations of function arguments (if any).
3593
3594    Returns the actual number of arguments processed (which may be less
3595    than the length of VALUES in some error situations), or -1 on
3596    failure.  */
3597
3598 static int
3599 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3600                    vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3601                    tree function, tree fundecl)
3602 {
3603   unsigned int parmnum;
3604   bool error_args = false;
3605   const bool type_generic = fundecl
3606     && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3607   bool type_generic_remove_excess_precision = false;
3608   bool type_generic_overflow_p = false;
3609   tree selector;
3610
3611   /* Change pointer to function to the function itself for
3612      diagnostics.  */
3613   if (TREE_CODE (function) == ADDR_EXPR
3614       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3615     function = TREE_OPERAND (function, 0);
3616
3617   /* Handle an ObjC selector specially for diagnostics.  */
3618   selector = objc_message_selector ();
3619
3620   /* For a call to a built-in function declared without a prototype,
3621      set to the built-in function's argument list.  */
3622   tree builtin_typelist = NULL_TREE;
3623
3624   /* For type-generic built-in functions, determine whether excess
3625      precision should be removed (classification) or not
3626      (comparison).  */
3627   if (fundecl
3628       && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3629     {
3630       built_in_function code = DECL_FUNCTION_CODE (fundecl);
3631       if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3632         {
3633           /* For a call to a built-in function declared without a prototype
3634              use the types of the parameters of the internal built-in to
3635              match those of the arguments to.  */
3636           if (tree bdecl = builtin_decl_explicit (code))
3637             builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3638         }
3639
3640       /* For type-generic built-in functions, determine whether excess
3641          precision should be removed (classification) or not
3642          (comparison).  */
3643       if (type_generic)
3644         switch (code)
3645           {
3646           case BUILT_IN_ISFINITE:
3647           case BUILT_IN_ISINF:
3648           case BUILT_IN_ISINF_SIGN:
3649           case BUILT_IN_ISNAN:
3650           case BUILT_IN_ISNORMAL:
3651           case BUILT_IN_ISSIGNALING:
3652           case BUILT_IN_FPCLASSIFY:
3653             type_generic_remove_excess_precision = true;
3654             break;
3655
3656           case BUILT_IN_ADD_OVERFLOW_P:
3657           case BUILT_IN_SUB_OVERFLOW_P:
3658           case BUILT_IN_MUL_OVERFLOW_P:
3659             /* The last argument of these type-generic builtins
3660                should not be promoted.  */
3661             type_generic_overflow_p = true;
3662             break;
3663
3664           default:
3665             break;
3666           }
3667     }
3668
3669   /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3670      individual converted arguments.  */
3671
3672   tree typetail, builtin_typetail, val;
3673   for (typetail = typelist,
3674          builtin_typetail = builtin_typelist,
3675          parmnum = 0;
3676        values && values->iterate (parmnum, &val);
3677        ++parmnum)
3678     {
3679       /* The type of the function parameter (if it was declared with one).  */
3680       tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3681       /* The type of the built-in function parameter (if the function
3682          is a built-in).  Used to detect type incompatibilities in
3683          calls to built-ins declared without a prototype.  */
3684       tree builtin_type = (builtin_typetail
3685                            ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3686       /* The original type of the argument being passed to the function.  */
3687       tree valtype = TREE_TYPE (val);
3688       /* The called function (or function selector in Objective C).  */
3689       tree rname = function;
3690       int argnum = parmnum + 1;
3691       const char *invalid_func_diag;
3692       /* Set for EXCESS_PRECISION_EXPR arguments.  */
3693       bool excess_precision = false;
3694       /* The value of the argument after conversion to the type
3695          of the function parameter it is passed to.  */
3696       tree parmval;
3697       /* Some __atomic_* builtins have additional hidden argument at
3698          position 0.  */
3699       location_t ploc
3700         = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3701           ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3702           : input_location;
3703
3704       if (type == void_type_node)
3705         {
3706           if (selector)
3707             error_at (loc, "too many arguments to method %qE", selector);
3708           else
3709             error_at (loc, "too many arguments to function %qE", function);
3710           inform_declaration (fundecl);
3711           return error_args ? -1 : (int) parmnum;
3712         }
3713
3714       if (builtin_type == void_type_node)
3715         {
3716           if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3717                           "too many arguments to built-in function %qE "
3718                           "expecting %d", function, parmnum))
3719             inform_declaration (fundecl);
3720           builtin_typetail = NULL_TREE;
3721         }
3722
3723       if (selector && argnum > 2)
3724         {
3725           rname = selector;
3726           argnum -= 2;
3727         }
3728
3729       /* Determine if VAL is a null pointer constant before folding it.  */
3730       bool npc = null_pointer_constant_p (val);
3731
3732       /* If there is excess precision and a prototype, convert once to
3733          the required type rather than converting via the semantic
3734          type.  Likewise without a prototype a float value represented
3735          as long double should be converted once to double.  But for
3736          type-generic classification functions excess precision must
3737          be removed here.  */
3738       if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3739           && (type || !type_generic || !type_generic_remove_excess_precision))
3740         {
3741           val = TREE_OPERAND (val, 0);
3742           excess_precision = true;
3743         }
3744       val = c_fully_fold (val, false, NULL);
3745       STRIP_TYPE_NOPS (val);
3746
3747       val = require_complete_type (ploc, val);
3748
3749       /* Some floating-point arguments must be promoted to double when
3750          no type is specified by a prototype.  This applies to
3751          arguments of type float, and to architecture-specific types
3752          (ARM __fp16), but not to _FloatN or _FloatNx types.  */
3753       bool promote_float_arg = false;
3754       if (type == NULL_TREE
3755           && TREE_CODE (valtype) == REAL_TYPE
3756           && (TYPE_PRECISION (valtype)
3757               <= TYPE_PRECISION (double_type_node))
3758           && TYPE_MAIN_VARIANT (valtype) != double_type_node
3759           && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3760           && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3761         {
3762           /* Promote this argument, unless it has a _FloatN or
3763              _FloatNx type.  */
3764           promote_float_arg = true;
3765           for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3766             if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3767               {
3768                 promote_float_arg = false;
3769                 break;
3770               }
3771           /* Don't promote __bf16 either.  */
3772           if (TYPE_MAIN_VARIANT (valtype) == bfloat16_type_node)
3773             promote_float_arg = false;
3774         }
3775
3776       if (type != NULL_TREE)
3777         {
3778           tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3779           parmval = convert_argument (ploc, function, fundecl, type, origtype,
3780                                       val, valtype, npc, rname, parmnum, argnum,
3781                                       excess_precision, 0);
3782         }
3783       else if (promote_float_arg)
3784         {
3785           if (type_generic)
3786             parmval = val;
3787           else
3788             {
3789               /* Convert `float' to `double'.  */
3790               if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3791                 warning_at (ploc, OPT_Wdouble_promotion,
3792                             "implicit conversion from %qT to %qT when passing "
3793                             "argument to function",
3794                             valtype, double_type_node);
3795               parmval = convert (double_type_node, val);
3796             }
3797         }
3798       else if ((excess_precision && !type_generic)
3799                || (type_generic_overflow_p && parmnum == 2))
3800         /* A "double" argument with excess precision being passed
3801            without a prototype or in variable arguments.
3802            The last argument of __builtin_*_overflow_p should not be
3803            promoted.  */
3804         parmval = convert (valtype, val);
3805       else if ((invalid_func_diag =
3806                 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3807         {
3808           error (invalid_func_diag);
3809           return -1;
3810         }
3811       else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3812         {
3813           return -1;
3814         }
3815       else
3816         /* Convert `short' and `char' to full-size `int'.  */
3817         parmval = default_conversion (val);
3818
3819       (*values)[parmnum] = parmval;
3820       if (parmval == error_mark_node)
3821         error_args = true;
3822
3823       if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3824         {
3825           /* For a call to a built-in function declared without a prototype,
3826              perform the conversions from the argument to the expected type
3827              but issue warnings rather than errors for any mismatches.
3828              Ignore the converted argument and use the PARMVAL obtained
3829              above by applying default conversions instead.  */
3830           tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3831           convert_argument (ploc, function, fundecl, builtin_type, origtype,
3832                             val, valtype, npc, rname, parmnum, argnum,
3833                             excess_precision,
3834                             OPT_Wbuiltin_declaration_mismatch);
3835         }
3836
3837       if (typetail)
3838         typetail = TREE_CHAIN (typetail);
3839
3840       if (builtin_typetail)
3841         builtin_typetail = TREE_CHAIN (builtin_typetail);
3842     }
3843
3844   gcc_assert (parmnum == vec_safe_length (values));
3845
3846   if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3847     {
3848       error_at (loc, "too few arguments to function %qE", function);
3849       inform_declaration (fundecl);
3850       return -1;
3851     }
3852
3853   if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3854     {
3855       unsigned nargs = parmnum;
3856       for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3857         ++nargs;
3858
3859       if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3860                       "too few arguments to built-in function %qE "
3861                       "expecting %u", function, nargs - 1))
3862         inform_declaration (fundecl);
3863     }
3864
3865   return error_args ? -1 : (int) parmnum;
3866 }
3867 \f
3868 /* This is the entry point used by the parser to build unary operators
3869    in the input.  CODE, a tree_code, specifies the unary operator, and
3870    ARG is the operand.  For unary plus, the C parser currently uses
3871    CONVERT_EXPR for code.
3872
3873    LOC is the location to use for the tree generated.
3874 */
3875
3876 struct c_expr
3877 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3878 {
3879   struct c_expr result;
3880
3881   result.original_code = code;
3882   result.original_type = NULL;
3883   result.m_decimal = 0;
3884
3885   if (reject_gcc_builtin (arg.value))
3886     {
3887       result.value = error_mark_node;
3888     }
3889   else
3890     {
3891       result.value = build_unary_op (loc, code, arg.value, false);
3892
3893       if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3894         overflow_warning (loc, result.value, arg.value);
3895     }
3896
3897   /* We are typically called when parsing a prefix token at LOC acting on
3898      ARG.  Reflect this by updating the source range of the result to
3899      start at LOC and end at the end of ARG.  */
3900   set_c_expr_source_range (&result,
3901                            loc, arg.get_finish ());
3902
3903   return result;
3904 }
3905
3906 /* Returns true if TYPE is a character type, *not* including wchar_t.  */
3907
3908 bool
3909 char_type_p (tree type)
3910 {
3911   return (type == char_type_node
3912           || type == unsigned_char_type_node
3913           || type == signed_char_type_node
3914           || type == char16_type_node
3915           || type == char32_type_node);
3916 }
3917
3918 /* This is the entry point used by the parser to build binary operators
3919    in the input.  CODE, a tree_code, specifies the binary operator, and
3920    ARG1 and ARG2 are the operands.  In addition to constructing the
3921    expression, we check for operands that were written with other binary
3922    operators in a way that is likely to confuse the user.
3923
3924    LOCATION is the location of the binary operator.  */
3925
3926 struct c_expr
3927 parser_build_binary_op (location_t location, enum tree_code code,
3928                         struct c_expr arg1, struct c_expr arg2)
3929 {
3930   struct c_expr result;
3931   result.m_decimal = 0;
3932
3933   enum tree_code code1 = arg1.original_code;
3934   enum tree_code code2 = arg2.original_code;
3935   tree type1 = (arg1.original_type
3936                 ? arg1.original_type
3937                 : TREE_TYPE (arg1.value));
3938   tree type2 = (arg2.original_type
3939                 ? arg2.original_type
3940                 : TREE_TYPE (arg2.value));
3941
3942   result.value = build_binary_op (location, code,
3943                                   arg1.value, arg2.value, true);
3944   result.original_code = code;
3945   result.original_type = NULL;
3946   result.m_decimal = 0;
3947
3948   if (TREE_CODE (result.value) == ERROR_MARK)
3949     {
3950       set_c_expr_source_range (&result,
3951                                arg1.get_start (),
3952                                arg2.get_finish ());
3953       return result;
3954     }
3955
3956   if (location != UNKNOWN_LOCATION)
3957     protected_set_expr_location (result.value, location);
3958
3959   set_c_expr_source_range (&result,
3960                            arg1.get_start (),
3961                            arg2.get_finish ());
3962
3963   /* Check for cases such as x+y<<z which users are likely
3964      to misinterpret.  */
3965   if (warn_parentheses)
3966     warn_about_parentheses (location, code, code1, arg1.value, code2,
3967                             arg2.value);
3968
3969   if (warn_logical_op)
3970     warn_logical_operator (location, code, TREE_TYPE (result.value),
3971                            code1, arg1.value, code2, arg2.value);
3972
3973   if (warn_tautological_compare)
3974     {
3975       tree lhs = arg1.value;
3976       tree rhs = arg2.value;
3977       if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3978         {
3979           if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3980               && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3981             lhs = NULL_TREE;
3982           else
3983             lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3984         }
3985       if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3986         {
3987           if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3988               && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3989             rhs = NULL_TREE;
3990           else
3991             rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3992         }
3993       if (lhs != NULL_TREE && rhs != NULL_TREE)
3994         warn_tautological_cmp (location, code, lhs, rhs);
3995     }
3996
3997   if (warn_logical_not_paren
3998       && TREE_CODE_CLASS (code) == tcc_comparison
3999       && code1 == TRUTH_NOT_EXPR
4000       && code2 != TRUTH_NOT_EXPR
4001       /* Avoid warning for !!x == y.  */
4002       && (TREE_CODE (arg1.value) != NE_EXPR
4003           || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
4004     {
4005       /* Avoid warning for !b == y where b has _Bool type.  */
4006       tree t = integer_zero_node;
4007       if (TREE_CODE (arg1.value) == EQ_EXPR
4008           && integer_zerop (TREE_OPERAND (arg1.value, 1))
4009           && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
4010         {
4011           t = TREE_OPERAND (arg1.value, 0);
4012           do
4013             {
4014               if (TREE_TYPE (t) != integer_type_node)
4015                 break;
4016               if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
4017                 t = C_MAYBE_CONST_EXPR_EXPR (t);
4018               else if (CONVERT_EXPR_P (t))
4019                 t = TREE_OPERAND (t, 0);
4020               else
4021                 break;
4022             }
4023           while (1);
4024         }
4025       if (!C_BOOLEAN_TYPE_P (TREE_TYPE (t)))
4026         warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
4027     }
4028
4029   /* Warn about comparisons against string literals, with the exception
4030      of testing for equality or inequality of a string literal with NULL.  */
4031   if (code == EQ_EXPR || code == NE_EXPR)
4032     {
4033       if ((code1 == STRING_CST
4034            && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
4035           || (code2 == STRING_CST
4036               && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
4037         warning_at (location, OPT_Waddress,
4038                     "comparison with string literal results in unspecified behavior");
4039       /* Warn for ptr == '\0', it's likely that it should've been ptr[0].  */
4040       if (POINTER_TYPE_P (type1)
4041           && null_pointer_constant_p (arg2.value)
4042           && char_type_p (type2))
4043         {
4044           auto_diagnostic_group d;
4045           if (warning_at (location, OPT_Wpointer_compare,
4046                             "comparison between pointer and zero character "
4047                             "constant"))
4048             inform (arg1.get_start (),
4049                       "did you mean to dereference the pointer?");
4050         }
4051       else if (POINTER_TYPE_P (type2)
4052                && null_pointer_constant_p (arg1.value)
4053                && char_type_p (type1))
4054         {
4055           auto_diagnostic_group d;
4056           if (warning_at (location, OPT_Wpointer_compare,
4057                             "comparison between pointer and zero character "
4058                             "constant"))
4059             inform (arg2.get_start (),
4060                       "did you mean to dereference the pointer?");
4061         }
4062     }
4063   else if (TREE_CODE_CLASS (code) == tcc_comparison
4064            && (code1 == STRING_CST || code2 == STRING_CST))
4065     warning_at (location, OPT_Waddress,
4066                 "comparison with string literal results in unspecified "
4067                 "behavior");
4068
4069   if (warn_array_compare
4070       && TREE_CODE_CLASS (code) == tcc_comparison
4071       && TREE_CODE (type1) == ARRAY_TYPE
4072       && TREE_CODE (type2) == ARRAY_TYPE)
4073     do_warn_array_compare (location, code, arg1.value, arg2.value);
4074
4075   if (TREE_OVERFLOW_P (result.value)
4076       && !TREE_OVERFLOW_P (arg1.value)
4077       && !TREE_OVERFLOW_P (arg2.value))
4078     overflow_warning (location, result.value);
4079
4080   /* Warn about comparisons of different enum types.  */
4081   if (warn_enum_compare
4082       && TREE_CODE_CLASS (code) == tcc_comparison
4083       && TREE_CODE (type1) == ENUMERAL_TYPE
4084       && TREE_CODE (type2) == ENUMERAL_TYPE
4085       && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
4086     warning_at (location, OPT_Wenum_compare,
4087                 "comparison between %qT and %qT",
4088                 type1, type2);
4089
4090   if (warn_xor_used_as_pow
4091       && code == BIT_XOR_EXPR
4092       && arg1.m_decimal
4093       && arg2.m_decimal)
4094     check_for_xor_used_as_pow (arg1.get_location (), arg1.value,
4095                                location,
4096                                arg2.value);
4097
4098   return result;
4099 }
4100 \f
4101 /* Return a tree for the difference of pointers OP0 and OP1.
4102    The resulting tree has type ptrdiff_t.  If POINTER_SUBTRACT sanitization is
4103    enabled, assign to INSTRUMENT_EXPR call to libsanitizer.  */
4104
4105 static tree
4106 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
4107 {
4108   tree restype = ptrdiff_type_node;
4109   tree result, inttype;
4110
4111   addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
4112   addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
4113   tree target_type = TREE_TYPE (TREE_TYPE (op0));
4114   tree orig_op0 = op0;
4115   tree orig_op1 = op1;
4116
4117   /* If the operands point into different address spaces, we need to
4118      explicitly convert them to pointers into the common address space
4119      before we can subtract the numerical address values.  */
4120   if (as0 != as1)
4121     {
4122       addr_space_t as_common;
4123       tree common_type;
4124
4125       /* Determine the common superset address space.  This is guaranteed
4126          to exist because the caller verified that comp_target_types
4127          returned non-zero.  */
4128       if (!addr_space_superset (as0, as1, &as_common))
4129         gcc_unreachable ();
4130
4131       common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4132       op0 = convert (common_type, op0);
4133       op1 = convert (common_type, op1);
4134     }
4135
4136   /* Determine integer type result of the subtraction.  This will usually
4137      be the same as the result type (ptrdiff_t), but may need to be a wider
4138      type if pointers for the address space are wider than ptrdiff_t.  */
4139   if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4140     inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4141   else
4142     inttype = restype;
4143
4144   if (TREE_CODE (target_type) == VOID_TYPE)
4145     pedwarn (loc, OPT_Wpointer_arith,
4146              "pointer of type %<void *%> used in subtraction");
4147   if (TREE_CODE (target_type) == FUNCTION_TYPE)
4148     pedwarn (loc, OPT_Wpointer_arith,
4149              "pointer to a function used in subtraction");
4150
4151   if (current_function_decl != NULL_TREE
4152       && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4153     {
4154       op0 = save_expr (op0);
4155       op1 = save_expr (op1);
4156
4157       tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4158       *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4159     }
4160
4161   /* First do the subtraction, then build the divide operator
4162      and only convert at the very end.
4163      Do not do default conversions in case restype is a short type.  */
4164
4165   /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4166      pointers.  If some platform cannot provide that, or has a larger
4167      ptrdiff_type to support differences larger than half the address
4168      space, cast the pointers to some larger integer type and do the
4169      computations in that type.  */
4170   if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4171     op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4172                            convert (inttype, op1), false);
4173   else
4174     {
4175       /* Cast away qualifiers.  */
4176       op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4177       op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4178       op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4179     }
4180
4181   /* This generates an error if op1 is pointer to incomplete type.  */
4182   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4183     error_at (loc, "arithmetic on pointer to an incomplete type");
4184   else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4185                                 TREE_TYPE (TREE_TYPE (orig_op0))))
4186     verify_type_context (loc, TCTX_POINTER_ARITH,
4187                          TREE_TYPE (TREE_TYPE (orig_op1)));
4188
4189   op1 = c_size_in_bytes (target_type);
4190
4191   if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4192     error_at (loc, "arithmetic on pointer to an empty aggregate");
4193
4194   /* Divide by the size, in easiest possible way.  */
4195   result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4196                             op0, convert (inttype, op1));
4197
4198   /* Convert to final result type if necessary.  */
4199   return convert (restype, result);
4200 }
4201 \f
4202 /* Expand atomic compound assignments into an appropriate sequence as
4203    specified by the C11 standard section 6.5.16.2.
4204
4205        _Atomic T1 E1
4206        T2 E2
4207        E1 op= E2
4208
4209   This sequence is used for all types for which these operations are
4210   supported.
4211
4212   In addition, built-in versions of the 'fe' prefixed routines may
4213   need to be invoked for floating point (real, complex or vector) when
4214   floating-point exceptions are supported.  See 6.5.16.2 footnote 113.
4215
4216   T1 newval;
4217   T1 old;
4218   T1 *addr
4219   T2 val
4220   fenv_t fenv
4221
4222   addr = &E1;
4223   val = (E2);
4224   __atomic_load (addr, &old, SEQ_CST);
4225   feholdexcept (&fenv);
4226 loop:
4227     newval = old op val;
4228     if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4229                                           SEQ_CST))
4230       goto done;
4231     feclearexcept (FE_ALL_EXCEPT);
4232     goto loop:
4233 done:
4234   feupdateenv (&fenv);
4235
4236   The compiler will issue the __atomic_fetch_* built-in when possible,
4237   otherwise it will generate the generic form of the atomic operations.
4238   This requires temp(s) and has their address taken.  The atomic processing
4239   is smart enough to figure out when the size of an object can utilize
4240   a lock-free version, and convert the built-in call to the appropriate
4241   lock-free routine.  The optimizers will then dispose of any temps that
4242   are no longer required, and lock-free implementations are utilized as
4243   long as there is target support for the required size.
4244
4245   If the operator is NOP_EXPR, then this is a simple assignment, and
4246   an __atomic_store is issued to perform the assignment rather than
4247   the above loop.  */
4248
4249 /* Build an atomic assignment at LOC, expanding into the proper
4250    sequence to store LHS MODIFYCODE= RHS.  Return a value representing
4251    the result of the operation, unless RETURN_OLD_P, in which case
4252    return the old value of LHS (this is only for postincrement and
4253    postdecrement).  */
4254
4255 static tree
4256 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4257                      tree rhs, bool return_old_p)
4258 {
4259   tree fndecl, func_call;
4260   vec<tree, va_gc> *params;
4261   tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4262   tree old, old_addr;
4263   tree compound_stmt = NULL_TREE;
4264   tree stmt, goto_stmt;
4265   tree loop_label, loop_decl, done_label, done_decl;
4266
4267   tree lhs_type = TREE_TYPE (lhs);
4268   tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4269   tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4270   tree rhs_semantic_type = TREE_TYPE (rhs);
4271   tree nonatomic_rhs_semantic_type;
4272   tree rhs_type;
4273
4274   gcc_assert (TYPE_ATOMIC (lhs_type));
4275
4276   if (return_old_p)
4277     gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4278
4279   /* Allocate enough vector items for a compare_exchange.  */
4280   vec_alloc (params, 6);
4281
4282   /* Create a compound statement to hold the sequence of statements
4283      with a loop.  */
4284   if (modifycode != NOP_EXPR)
4285     {
4286       compound_stmt = c_begin_compound_stmt (false);
4287
4288       /* For consistency with build_modify_expr on non-_Atomic,
4289          mark the lhs as read.  Also, it would be very hard to match
4290          such expressions in mark_exp_read.  */
4291       mark_exp_read (lhs);
4292     }
4293
4294   /* Remove any excess precision (which is only present here in the
4295      case of compound assignments).  */
4296   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4297     {
4298       gcc_assert (modifycode != NOP_EXPR);
4299       rhs = TREE_OPERAND (rhs, 0);
4300     }
4301   rhs_type = TREE_TYPE (rhs);
4302
4303   /* Fold the RHS if it hasn't already been folded.  */
4304   if (modifycode != NOP_EXPR)
4305     rhs = c_fully_fold (rhs, false, NULL);
4306
4307   /* Remove the qualifiers for the rest of the expressions and create
4308      the VAL temp variable to hold the RHS.  */
4309   nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4310   nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4311   nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4312                                                       TYPE_UNQUALIFIED);
4313   val = create_tmp_var_raw (nonatomic_rhs_type);
4314   TREE_ADDRESSABLE (val) = 1;
4315   suppress_warning (val);
4316   rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4317                 NULL_TREE);
4318   TREE_SIDE_EFFECTS (rhs) = 1;
4319   SET_EXPR_LOCATION (rhs, loc);
4320   if (modifycode != NOP_EXPR)
4321     add_stmt (rhs);
4322
4323   /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4324      an atomic_store.  */
4325   if (modifycode == NOP_EXPR)
4326     {
4327       compound_stmt = rhs;
4328       /* Build __atomic_store (&lhs, &val, SEQ_CST)  */
4329       rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4330       fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4331       params->quick_push (lhs_addr);
4332       params->quick_push (rhs);
4333       params->quick_push (seq_cst);
4334       func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4335
4336       compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4337                               compound_stmt, func_call);
4338
4339       /* VAL is the value which was stored, return a COMPOUND_STMT of
4340          the statement and that value.  */
4341       return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4342     }
4343
4344   /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4345      __atomic_*_fetch built-in rather than a CAS loop.  atomic_bool type
4346      isn't applicable for such builtins.  ??? Do we want to handle enums?  */
4347   if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4348       && TREE_CODE (rhs_type) == INTEGER_TYPE)
4349     {
4350       built_in_function fncode;
4351       switch (modifycode)
4352         {
4353         case PLUS_EXPR:
4354         case POINTER_PLUS_EXPR:
4355           fncode = (return_old_p
4356                     ? BUILT_IN_ATOMIC_FETCH_ADD_N
4357                     : BUILT_IN_ATOMIC_ADD_FETCH_N);
4358           break;
4359         case MINUS_EXPR:
4360           fncode = (return_old_p
4361                     ? BUILT_IN_ATOMIC_FETCH_SUB_N
4362                     : BUILT_IN_ATOMIC_SUB_FETCH_N);
4363           break;
4364         case BIT_AND_EXPR:
4365           fncode = (return_old_p
4366                     ? BUILT_IN_ATOMIC_FETCH_AND_N
4367                     : BUILT_IN_ATOMIC_AND_FETCH_N);
4368           break;
4369         case BIT_IOR_EXPR:
4370           fncode = (return_old_p
4371                     ? BUILT_IN_ATOMIC_FETCH_OR_N
4372                     : BUILT_IN_ATOMIC_OR_FETCH_N);
4373           break;
4374         case BIT_XOR_EXPR:
4375           fncode = (return_old_p
4376                     ? BUILT_IN_ATOMIC_FETCH_XOR_N
4377                     : BUILT_IN_ATOMIC_XOR_FETCH_N);
4378           break;
4379         default:
4380           goto cas_loop;
4381         }
4382
4383       /* We can only use "_1" through "_16" variants of the atomic fetch
4384          built-ins.  */
4385       unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4386       if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4387         goto cas_loop;
4388
4389       /* If this is a pointer type, we need to multiply by the size of
4390          the pointer target type.  */
4391       if (POINTER_TYPE_P (lhs_type))
4392         {
4393           if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4394               /* ??? This would introduce -Wdiscarded-qualifiers
4395                  warning: __atomic_fetch_* expect volatile void *
4396                  type as the first argument.  (Assignments between
4397                  atomic and non-atomic objects are OK.) */
4398               || TYPE_RESTRICT (lhs_type))
4399             goto cas_loop;
4400           tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4401           rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4402                                  convert (ptrdiff_type_node, rhs),
4403                                  convert (ptrdiff_type_node, sz));
4404         }
4405
4406       /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4407          __atomic_*_fetch (&lhs, &val, SEQ_CST).  */
4408       fndecl = builtin_decl_explicit (fncode);
4409       params->quick_push (lhs_addr);
4410       params->quick_push (rhs);
4411       params->quick_push (seq_cst);
4412       func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4413
4414       newval = create_tmp_var_raw (nonatomic_lhs_type);
4415       TREE_ADDRESSABLE (newval) = 1;
4416       suppress_warning (newval);
4417       rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4418                     NULL_TREE, NULL_TREE);
4419       SET_EXPR_LOCATION (rhs, loc);
4420       add_stmt (rhs);
4421
4422       /* Finish the compound statement.  */
4423       compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4424
4425       /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4426          the statement and that value.  */
4427       return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4428     }
4429
4430 cas_loop:
4431   /* Create the variables and labels required for the op= form.  */
4432   old = create_tmp_var_raw (nonatomic_lhs_type);
4433   old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4434   TREE_ADDRESSABLE (old) = 1;
4435   suppress_warning (old);
4436
4437   newval = create_tmp_var_raw (nonatomic_lhs_type);
4438   newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4439   TREE_ADDRESSABLE (newval) = 1;
4440   suppress_warning (newval);
4441
4442   loop_decl = create_artificial_label (loc);
4443   loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4444
4445   done_decl = create_artificial_label (loc);
4446   done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4447
4448   /* __atomic_load (addr, &old, SEQ_CST).  */
4449   fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4450   params->quick_push (lhs_addr);
4451   params->quick_push (old_addr);
4452   params->quick_push (seq_cst);
4453   func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4454   old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4455                 NULL_TREE);
4456   add_stmt (old);
4457   params->truncate (0);
4458
4459   /* Create the expressions for floating-point environment
4460      manipulation, if required.  */
4461   bool need_fenv = (flag_trapping_math
4462                     && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4463   tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4464   if (need_fenv)
4465     targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4466
4467   if (hold_call)
4468     add_stmt (hold_call);
4469
4470   /* loop:  */
4471   add_stmt (loop_label);
4472
4473   /* newval = old + val;  */
4474   if (rhs_type != rhs_semantic_type)
4475     val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4476   rhs = build_binary_op (loc, modifycode, old, val, true);
4477   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4478     {
4479       tree eptype = TREE_TYPE (rhs);
4480       rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4481       rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4482     }
4483   else
4484     rhs = c_fully_fold (rhs, false, NULL);
4485   rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4486                                 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4487                                 NULL_TREE, 0);
4488   if (rhs != error_mark_node)
4489     {
4490       rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4491                     NULL_TREE);
4492       SET_EXPR_LOCATION (rhs, loc);
4493       add_stmt (rhs);
4494     }
4495
4496   /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4497        goto done;  */
4498   fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4499   params->quick_push (lhs_addr);
4500   params->quick_push (old_addr);
4501   params->quick_push (newval_addr);
4502   params->quick_push (integer_zero_node);
4503   params->quick_push (seq_cst);
4504   params->quick_push (seq_cst);
4505   func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4506
4507   goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4508   SET_EXPR_LOCATION (goto_stmt, loc);
4509
4510   stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4511   SET_EXPR_LOCATION (stmt, loc);
4512   add_stmt (stmt);
4513
4514   if (clear_call)
4515     add_stmt (clear_call);
4516
4517   /* goto loop;  */
4518   goto_stmt  = build1 (GOTO_EXPR, void_type_node, loop_decl);
4519   SET_EXPR_LOCATION (goto_stmt, loc);
4520   add_stmt (goto_stmt);
4521
4522   /* done:  */
4523   add_stmt (done_label);
4524
4525   if (update_call)
4526     add_stmt (update_call);
4527
4528   /* Finish the compound statement.  */
4529   compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4530
4531   /* NEWVAL is the value that was successfully stored, return a
4532      COMPOUND_EXPR of the statement and the appropriate value.  */
4533   return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4534                  return_old_p ? old : newval);
4535 }
4536
4537 /* Construct and perhaps optimize a tree representation
4538    for a unary operation.  CODE, a tree_code, specifies the operation
4539    and XARG is the operand.
4540    For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4541    promotions (such as from short to int).
4542    For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4543    non-lvalues; this is only used to handle conversion of non-lvalue arrays
4544    to pointers in C99.
4545
4546    LOCATION is the location of the operator.  */
4547
4548 tree
4549 build_unary_op (location_t location, enum tree_code code, tree xarg,
4550                 bool noconvert)
4551 {
4552   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
4553   tree arg = xarg;
4554   tree argtype = NULL_TREE;
4555   enum tree_code typecode;
4556   tree val;
4557   tree ret = error_mark_node;
4558   tree eptype = NULL_TREE;
4559   const char *invalid_op_diag;
4560   bool int_operands;
4561
4562   int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4563   if (int_operands)
4564     arg = remove_c_maybe_const_expr (arg);
4565
4566   if (code != ADDR_EXPR)
4567     arg = require_complete_type (location, arg);
4568
4569   typecode = TREE_CODE (TREE_TYPE (arg));
4570   if (typecode == ERROR_MARK)
4571     return error_mark_node;
4572   if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4573     typecode = INTEGER_TYPE;
4574
4575   if ((invalid_op_diag
4576        = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4577     {
4578       error_at (location, invalid_op_diag);
4579       return error_mark_node;
4580     }
4581
4582   if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4583     {
4584       eptype = TREE_TYPE (arg);
4585       arg = TREE_OPERAND (arg, 0);
4586     }
4587
4588   switch (code)
4589     {
4590     case CONVERT_EXPR:
4591       /* This is used for unary plus, because a CONVERT_EXPR
4592          is enough to prevent anybody from looking inside for
4593          associativity, but won't generate any code.  */
4594       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4595             || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4596             || gnu_vector_type_p (TREE_TYPE (arg))))
4597         {
4598           error_at (location, "wrong type argument to unary plus");
4599           return error_mark_node;
4600         }
4601       else if (!noconvert)
4602         arg = default_conversion (arg);
4603       arg = non_lvalue_loc (location, arg);
4604       break;
4605
4606     case NEGATE_EXPR:
4607       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4608             || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4609             || gnu_vector_type_p (TREE_TYPE (arg))))
4610         {
4611           error_at (location, "wrong type argument to unary minus");
4612           return error_mark_node;
4613         }
4614       else if (!noconvert)
4615         arg = default_conversion (arg);
4616       break;
4617
4618     case BIT_NOT_EXPR:
4619       /* ~ works on integer types and non float vectors. */
4620       if (typecode == INTEGER_TYPE
4621           || (gnu_vector_type_p (TREE_TYPE (arg))
4622               && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4623         {
4624           tree e = arg;
4625
4626           /* Warn if the expression has boolean value.  */
4627           while (TREE_CODE (e) == COMPOUND_EXPR)
4628             e = TREE_OPERAND (e, 1);
4629
4630           if ((C_BOOLEAN_TYPE_P (TREE_TYPE (arg))
4631                || truth_value_p (TREE_CODE (e))))
4632             {
4633               auto_diagnostic_group d;
4634               if (warning_at (location, OPT_Wbool_operation,
4635                                 "%<~%> on a boolean expression"))
4636                 {
4637                   gcc_rich_location richloc (location);
4638                   richloc.add_fixit_insert_before (location, "!");
4639                   inform (&richloc, "did you mean to use logical not?");
4640                 }
4641             }
4642           if (!noconvert)
4643             arg = default_conversion (arg);
4644         }
4645       else if (typecode == COMPLEX_TYPE)
4646         {
4647           code = CONJ_EXPR;
4648           pedwarn (location, OPT_Wpedantic,
4649                    "ISO C does not support %<~%> for complex conjugation");
4650           if (!noconvert)
4651             arg = default_conversion (arg);
4652         }
4653       else
4654         {
4655           error_at (location, "wrong type argument to bit-complement");
4656           return error_mark_node;
4657         }
4658       break;
4659
4660     case ABS_EXPR:
4661       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4662         {
4663           error_at (location, "wrong type argument to abs");
4664           return error_mark_node;
4665         }
4666       else if (!noconvert)
4667         arg = default_conversion (arg);
4668       break;
4669
4670     case ABSU_EXPR:
4671       if (!(typecode == INTEGER_TYPE))
4672         {
4673           error_at (location, "wrong type argument to absu");
4674           return error_mark_node;
4675         }
4676       else if (!noconvert)
4677         arg = default_conversion (arg);
4678       break;
4679
4680     case CONJ_EXPR:
4681       /* Conjugating a real value is a no-op, but allow it anyway.  */
4682       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4683             || typecode == COMPLEX_TYPE))
4684         {
4685           error_at (location, "wrong type argument to conjugation");
4686           return error_mark_node;
4687         }
4688       else if (!noconvert)
4689         arg = default_conversion (arg);
4690       break;
4691
4692     case TRUTH_NOT_EXPR:
4693       if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4694           && typecode != REAL_TYPE && typecode != POINTER_TYPE
4695           && typecode != COMPLEX_TYPE && typecode != NULLPTR_TYPE)
4696         {
4697           error_at (location,
4698                     "wrong type argument to unary exclamation mark");
4699           return error_mark_node;
4700         }
4701       if (int_operands)
4702         {
4703           arg = c_objc_common_truthvalue_conversion (location, xarg);
4704           arg = remove_c_maybe_const_expr (arg);
4705         }
4706       else
4707         arg = c_objc_common_truthvalue_conversion (location, arg);
4708       ret = invert_truthvalue_loc (location, arg);
4709       /* If the TRUTH_NOT_EXPR has been folded, reset the location.  */
4710       if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4711         location = EXPR_LOCATION (ret);
4712       goto return_build_unary_op;
4713
4714     case REALPART_EXPR:
4715     case IMAGPART_EXPR:
4716       ret = build_real_imag_expr (location, code, arg);
4717       if (ret == error_mark_node)
4718         return error_mark_node;
4719       if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4720         eptype = TREE_TYPE (eptype);
4721       goto return_build_unary_op;
4722
4723     case PREINCREMENT_EXPR:
4724     case POSTINCREMENT_EXPR:
4725     case PREDECREMENT_EXPR:
4726     case POSTDECREMENT_EXPR:
4727
4728       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4729         {
4730           tree inner = build_unary_op (location, code,
4731                                        C_MAYBE_CONST_EXPR_EXPR (arg),
4732                                        noconvert);
4733           if (inner == error_mark_node)
4734             return error_mark_node;
4735           ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4736                         C_MAYBE_CONST_EXPR_PRE (arg), inner);
4737           gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4738           C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4739           goto return_build_unary_op;
4740         }
4741
4742       /* Complain about anything that is not a true lvalue.  In
4743          Objective-C, skip this check for property_refs.  */
4744       if (!objc_is_property_ref (arg)
4745           && !lvalue_or_else (location,
4746                               arg, ((code == PREINCREMENT_EXPR
4747                                      || code == POSTINCREMENT_EXPR)
4748                                     ? lv_increment
4749                                     : lv_decrement)))
4750         return error_mark_node;
4751
4752       if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4753         {
4754           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4755             warning_at (location, OPT_Wc___compat,
4756                         "increment of enumeration value is invalid in C++");
4757           else
4758             warning_at (location, OPT_Wc___compat,
4759                         "decrement of enumeration value is invalid in C++");
4760         }
4761
4762       if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4763         {
4764           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4765             warning_at (location, OPT_Wbool_operation,
4766                         "increment of a boolean expression");
4767           else
4768             warning_at (location, OPT_Wbool_operation,
4769                         "decrement of a boolean expression");
4770         }
4771
4772       /* Ensure the argument is fully folded inside any SAVE_EXPR.  */
4773       arg = c_fully_fold (arg, false, NULL, true);
4774
4775       bool atomic_op;
4776       atomic_op = really_atomic_lvalue (arg);
4777
4778       /* Increment or decrement the real part of the value,
4779          and don't change the imaginary part.  */
4780       if (typecode == COMPLEX_TYPE)
4781         {
4782           tree real, imag;
4783
4784           pedwarn (location, OPT_Wpedantic,
4785                    "ISO C does not support %<++%> and %<--%> on complex types");
4786
4787           if (!atomic_op)
4788             {
4789               arg = stabilize_reference (arg);
4790               real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4791                                      true);
4792               imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4793                                      true);
4794               real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4795               if (real == error_mark_node || imag == error_mark_node)
4796                 return error_mark_node;
4797               ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4798                             real, imag);
4799               goto return_build_unary_op;
4800             }
4801         }
4802
4803       /* Report invalid types.  */
4804
4805       if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4806           && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4807           && typecode != COMPLEX_TYPE
4808           && !gnu_vector_type_p (TREE_TYPE (arg)))
4809         {
4810           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4811             error_at (location, "wrong type argument to increment");
4812           else
4813             error_at (location, "wrong type argument to decrement");
4814
4815           return error_mark_node;
4816         }
4817
4818       {
4819         tree inc;
4820
4821         argtype = TREE_TYPE (arg);
4822
4823         /* Compute the increment.  */
4824
4825         if (typecode == POINTER_TYPE)
4826           {
4827             /* If pointer target is an incomplete type,
4828                we just cannot know how to do the arithmetic.  */
4829             if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4830               {
4831                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4832                   error_at (location,
4833                             "increment of pointer to an incomplete type %qT",
4834                             TREE_TYPE (argtype));
4835                 else
4836                   error_at (location,
4837                             "decrement of pointer to an incomplete type %qT",
4838                             TREE_TYPE (argtype));
4839               }
4840             else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4841                      || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4842               {
4843                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4844                   pedwarn (location, OPT_Wpointer_arith,
4845                            "wrong type argument to increment");
4846                 else
4847                   pedwarn (location, OPT_Wpointer_arith,
4848                            "wrong type argument to decrement");
4849               }
4850             else
4851               verify_type_context (location, TCTX_POINTER_ARITH,
4852                                    TREE_TYPE (argtype));
4853
4854             inc = c_size_in_bytes (TREE_TYPE (argtype));
4855             inc = convert_to_ptrofftype_loc (location, inc);
4856           }
4857         else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4858           {
4859             /* For signed fract types, we invert ++ to -- or
4860                -- to ++, and change inc from 1 to -1, because
4861                it is not possible to represent 1 in signed fract constants.
4862                For unsigned fract types, the result always overflows and
4863                we get an undefined (original) or the maximum value.  */
4864             if (code == PREINCREMENT_EXPR)
4865               code = PREDECREMENT_EXPR;
4866             else if (code == PREDECREMENT_EXPR)
4867               code = PREINCREMENT_EXPR;
4868             else if (code == POSTINCREMENT_EXPR)
4869               code = POSTDECREMENT_EXPR;
4870             else /* code == POSTDECREMENT_EXPR  */
4871               code = POSTINCREMENT_EXPR;
4872
4873             inc = integer_minus_one_node;
4874             inc = convert (argtype, inc);
4875           }
4876         else
4877           {
4878             inc = VECTOR_TYPE_P (argtype)
4879               ? build_one_cst (argtype)
4880               : integer_one_node;
4881             inc = convert (argtype, inc);
4882           }
4883
4884         /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4885            need to ask Objective-C to build the increment or decrement
4886            expression for it.  */
4887         if (objc_is_property_ref (arg))
4888           return objc_build_incr_expr_for_property_ref (location, code,
4889                                                         arg, inc);
4890
4891         /* Report a read-only lvalue.  */
4892         if (TYPE_READONLY (argtype))
4893           {
4894             readonly_error (location, arg,
4895                             ((code == PREINCREMENT_EXPR
4896                               || code == POSTINCREMENT_EXPR)
4897                              ? lv_increment : lv_decrement));
4898             return error_mark_node;
4899           }
4900         else if (TREE_READONLY (arg))
4901           readonly_warning (arg,
4902                             ((code == PREINCREMENT_EXPR
4903                               || code == POSTINCREMENT_EXPR)
4904                              ? lv_increment : lv_decrement));
4905
4906         /* If the argument is atomic, use the special code sequences for
4907            atomic compound assignment.  */
4908         if (atomic_op)
4909           {
4910             arg = stabilize_reference (arg);
4911             ret = build_atomic_assign (location, arg,
4912                                        ((code == PREINCREMENT_EXPR
4913                                          || code == POSTINCREMENT_EXPR)
4914                                         ? PLUS_EXPR
4915                                         : MINUS_EXPR),
4916                                        (FRACT_MODE_P (TYPE_MODE (argtype))
4917                                         ? inc
4918                                         : integer_one_node),
4919                                        (code == POSTINCREMENT_EXPR
4920                                         || code == POSTDECREMENT_EXPR));
4921             goto return_build_unary_op;
4922           }
4923
4924         if (C_BOOLEAN_TYPE_P (TREE_TYPE (arg)))
4925           val = boolean_increment (code, arg);
4926         else
4927           val = build2 (code, TREE_TYPE (arg), arg, inc);
4928         TREE_SIDE_EFFECTS (val) = 1;
4929         if (TYPE_QUALS (TREE_TYPE (val)) != TYPE_UNQUALIFIED)
4930           TREE_TYPE (val) = c_build_qualified_type (TREE_TYPE (val),
4931                                                     TYPE_UNQUALIFIED);
4932         ret = val;
4933         goto return_build_unary_op;
4934       }
4935
4936     case ADDR_EXPR:
4937       /* Note that this operation never does default_conversion.  */
4938
4939       /* The operand of unary '&' must be an lvalue (which excludes
4940          expressions of type void), or, in C99, the result of a [] or
4941          unary '*' operator.  */
4942       if (VOID_TYPE_P (TREE_TYPE (arg))
4943           && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4944           && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4945         pedwarn (location, 0, "taking address of expression of type %<void%>");
4946
4947       /* Let &* cancel out to simplify resulting code.  */
4948       if (INDIRECT_REF_P (arg))
4949         {
4950           /* Don't let this be an lvalue.  */
4951           if (lvalue_p (TREE_OPERAND (arg, 0)))
4952             return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4953           ret = TREE_OPERAND (arg, 0);
4954           goto return_build_unary_op;
4955         }
4956
4957       /* Anything not already handled and not a true memory reference
4958          or a non-lvalue array is an error.  */
4959       if (typecode != FUNCTION_TYPE && !noconvert
4960           && !lvalue_or_else (location, arg, lv_addressof))
4961         return error_mark_node;
4962
4963       /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4964          folding later.  */
4965       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4966         {
4967           tree inner = build_unary_op (location, code,
4968                                        C_MAYBE_CONST_EXPR_EXPR (arg),
4969                                        noconvert);
4970           ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4971                         C_MAYBE_CONST_EXPR_PRE (arg), inner);
4972           gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4973           C_MAYBE_CONST_EXPR_NON_CONST (ret)
4974             = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4975           goto return_build_unary_op;
4976         }
4977
4978       /* Ordinary case; arg is a COMPONENT_REF or a decl.  */
4979       argtype = TREE_TYPE (arg);
4980
4981       /* If the lvalue is const or volatile, merge that into the type
4982          to which the address will point.  This is only needed
4983          for function types.  */
4984       if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4985           && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4986           && TREE_CODE (argtype) == FUNCTION_TYPE)
4987         {
4988           int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4989           int quals = orig_quals;
4990
4991           if (TREE_READONLY (arg))
4992             quals |= TYPE_QUAL_CONST;
4993           if (TREE_THIS_VOLATILE (arg))
4994             quals |= TYPE_QUAL_VOLATILE;
4995
4996           argtype = c_build_qualified_type (argtype, quals);
4997         }
4998
4999       switch (TREE_CODE (arg))
5000         {
5001         case COMPONENT_REF:
5002           if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5003             {
5004               error_at (location, "cannot take address of bit-field %qD",
5005                         TREE_OPERAND (arg, 1));
5006               return error_mark_node;
5007             }
5008
5009           /* fall through */
5010
5011         case ARRAY_REF:
5012           if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
5013             {
5014               if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
5015                   && !POINTER_TYPE_P (TREE_TYPE (arg))
5016                   && !VECTOR_TYPE_P (TREE_TYPE (arg)))
5017                 {
5018                   error_at (location, "cannot take address of scalar with "
5019                             "reverse storage order");
5020                   return error_mark_node;
5021                 }
5022
5023               if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
5024                   && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
5025                 warning_at (location, OPT_Wscalar_storage_order,
5026                             "address of array with reverse scalar storage "
5027                             "order requested");
5028             }
5029
5030         default:
5031           break;
5032         }
5033
5034       if (!c_mark_addressable (arg))
5035         return error_mark_node;
5036
5037       gcc_assert (TREE_CODE (arg) != COMPONENT_REF
5038                   || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
5039
5040       argtype = build_pointer_type (argtype);
5041
5042       /* ??? Cope with user tricks that amount to offsetof.  Delete this
5043          when we have proper support for integer constant expressions.  */
5044       val = get_base_address (arg);
5045       if (val && INDIRECT_REF_P (val)
5046           && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5047         {
5048           ret = fold_offsetof (arg, argtype);
5049           goto return_build_unary_op;
5050         }
5051
5052       val = build1 (ADDR_EXPR, argtype, arg);
5053
5054       ret = val;
5055       goto return_build_unary_op;
5056
5057     case PAREN_EXPR:
5058       ret = build1 (code, TREE_TYPE (arg), arg);
5059       goto return_build_unary_op;
5060
5061     default:
5062       gcc_unreachable ();
5063     }
5064
5065   if (argtype == NULL_TREE)
5066     argtype = TREE_TYPE (arg);
5067   if (TREE_CODE (arg) == INTEGER_CST)
5068     ret = (require_constant_value
5069            ? fold_build1_initializer_loc (location, code, argtype, arg)
5070            : fold_build1_loc (location, code, argtype, arg));
5071   else
5072     ret = build1 (code, argtype, arg);
5073  return_build_unary_op:
5074   gcc_assert (ret != error_mark_node);
5075   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
5076       && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
5077     ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
5078   else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
5079     ret = note_integer_operands (ret);
5080   if (eptype)
5081     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5082   protected_set_expr_location (ret, location);
5083   return ret;
5084 }
5085
5086 /* Return nonzero if REF is an lvalue valid for this language.
5087    Lvalues can be assigned, unless their type has TYPE_READONLY.
5088    Lvalues can have their address taken, unless they have C_DECL_REGISTER.  */
5089
5090 bool
5091 lvalue_p (const_tree ref)
5092 {
5093   const enum tree_code code = TREE_CODE (ref);
5094
5095   switch (code)
5096     {
5097     case REALPART_EXPR:
5098     case IMAGPART_EXPR:
5099     case COMPONENT_REF:
5100       return lvalue_p (TREE_OPERAND (ref, 0));
5101
5102     case C_MAYBE_CONST_EXPR:
5103       return lvalue_p (TREE_OPERAND (ref, 1));
5104
5105     case COMPOUND_LITERAL_EXPR:
5106     case STRING_CST:
5107       return true;
5108
5109     case MEM_REF:
5110     case TARGET_MEM_REF:
5111       /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
5112          here as well.  */
5113     case INDIRECT_REF:
5114     case ARRAY_REF:
5115     case VAR_DECL:
5116     case PARM_DECL:
5117     case RESULT_DECL:
5118     case ERROR_MARK:
5119       return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
5120               && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5121
5122     case BIND_EXPR:
5123       return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5124
5125     default:
5126       return false;
5127     }
5128 }
5129 \f
5130 /* Give a warning for storing in something that is read-only in GCC
5131    terms but not const in ISO C terms.  */
5132
5133 static void
5134 readonly_warning (tree arg, enum lvalue_use use)
5135 {
5136   switch (use)
5137     {
5138     case lv_assign:
5139       warning (0, "assignment of read-only location %qE", arg);
5140       break;
5141     case lv_increment:
5142       warning (0, "increment of read-only location %qE", arg);
5143       break;
5144     case lv_decrement:
5145       warning (0, "decrement of read-only location %qE", arg);
5146       break;
5147     default:
5148       gcc_unreachable ();
5149     }
5150   return;
5151 }
5152
5153
5154 /* Return nonzero if REF is an lvalue valid for this language;
5155    otherwise, print an error message and return zero.  USE says
5156    how the lvalue is being used and so selects the error message.
5157    LOCATION is the location at which any error should be reported.  */
5158
5159 static int
5160 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5161 {
5162   int win = lvalue_p (ref);
5163
5164   if (!win)
5165     lvalue_error (loc, use);
5166
5167   return win;
5168 }
5169 \f
5170 /* Mark EXP saying that we need to be able to take the
5171    address of it; it should not be allocated in a register.
5172    Returns true if successful.  ARRAY_REF_P is true if this
5173    is for ARRAY_REF construction - in that case we don't want
5174    to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5175    it is fine to use ARRAY_REFs for vector subscripts on vector
5176    register variables.  */
5177
5178 bool
5179 c_mark_addressable (tree exp, bool array_ref_p)
5180 {
5181   tree x = exp;
5182
5183   while (1)
5184     switch (TREE_CODE (x))
5185       {
5186       case VIEW_CONVERT_EXPR:
5187         if (array_ref_p
5188             && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5189             && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5190           return true;
5191         x = TREE_OPERAND (x, 0);
5192         break;
5193
5194       case COMPONENT_REF:
5195         if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5196           {
5197             error ("cannot take address of bit-field %qD",
5198                    TREE_OPERAND (x, 1));
5199             return false;
5200           }
5201         /* FALLTHRU */
5202       case ADDR_EXPR:
5203       case ARRAY_REF:
5204       case REALPART_EXPR:
5205       case IMAGPART_EXPR:
5206         x = TREE_OPERAND (x, 0);
5207         break;
5208
5209       case COMPOUND_LITERAL_EXPR:
5210         if (C_DECL_REGISTER (COMPOUND_LITERAL_EXPR_DECL (x)))
5211           {
5212             error ("address of register compound literal requested");
5213             return false;
5214           }
5215         TREE_ADDRESSABLE (x) = 1;
5216         TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5217         return true;
5218
5219       case CONSTRUCTOR:
5220         TREE_ADDRESSABLE (x) = 1;
5221         return true;
5222
5223       case VAR_DECL:
5224       case CONST_DECL:
5225       case PARM_DECL:
5226       case RESULT_DECL:
5227         if (C_DECL_REGISTER (x)
5228             && DECL_NONLOCAL (x))
5229           {
5230             if (TREE_PUBLIC (x) || is_global_var (x))
5231               {
5232                 error
5233                   ("global register variable %qD used in nested function", x);
5234                 return false;
5235               }
5236             pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5237           }
5238         else if (C_DECL_REGISTER (x))
5239           {
5240             if (TREE_PUBLIC (x) || is_global_var (x))
5241               error ("address of global register variable %qD requested", x);
5242             else
5243               error ("address of register variable %qD requested", x);
5244             return false;
5245           }
5246
5247         /* FALLTHRU */
5248       case FUNCTION_DECL:
5249         TREE_ADDRESSABLE (x) = 1;
5250         /* FALLTHRU */
5251       default:
5252         return true;
5253     }
5254 }
5255 \f
5256 /* Convert EXPR to TYPE, warning about conversion problems with
5257    constants.  SEMANTIC_TYPE is the type this conversion would use
5258    without excess precision. If SEMANTIC_TYPE is NULL, this function
5259    is equivalent to convert_and_check. This function is a wrapper that
5260    handles conversions that may be different than
5261    the usual ones because of excess precision.  */
5262
5263 static tree
5264 ep_convert_and_check (location_t loc, tree type, tree expr,
5265                       tree semantic_type)
5266 {
5267   if (TREE_TYPE (expr) == type)
5268     return expr;
5269
5270   /* For C11, integer conversions may have results with excess
5271      precision.  */
5272   if (flag_isoc11 || !semantic_type)
5273     return convert_and_check (loc, type, expr);
5274
5275   if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5276       && TREE_TYPE (expr) != semantic_type)
5277     {
5278       /* For integers, we need to check the real conversion, not
5279          the conversion to the excess precision type.  */
5280       expr = convert_and_check (loc, semantic_type, expr);
5281     }
5282   /* Result type is the excess precision type, which should be
5283      large enough, so do not check.  */
5284   return convert (type, expr);
5285 }
5286
5287 /* If EXPR refers to a built-in declared without a prototype returns
5288    the actual type of the built-in and, if non-null, set *BLTIN to
5289    a pointer to the built-in.  Otherwise return the type of EXPR
5290    and clear *BLTIN if non-null.  */
5291
5292 static tree
5293 type_or_builtin_type (tree expr, tree *bltin = NULL)
5294 {
5295   tree dummy;
5296   if (!bltin)
5297     bltin = &dummy;
5298
5299   *bltin = NULL_TREE;
5300
5301   tree type = TREE_TYPE (expr);
5302   if (TREE_CODE (expr) != ADDR_EXPR)
5303     return type;
5304
5305   tree oper = TREE_OPERAND (expr, 0);
5306   if (!DECL_P (oper)
5307       || TREE_CODE (oper) != FUNCTION_DECL
5308       || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5309     return type;
5310
5311   built_in_function code = DECL_FUNCTION_CODE (oper);
5312   if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5313     return type;
5314
5315   if ((*bltin = builtin_decl_implicit (code)))
5316     type = build_pointer_type (TREE_TYPE (*bltin));
5317
5318   return type;
5319 }
5320
5321 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  If
5322    IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5323    if folded to an integer constant then the unselected half may
5324    contain arbitrary operations not normally permitted in constant
5325    expressions.  Set the location of the expression to LOC.  */
5326
5327 tree
5328 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5329                         tree op1, tree op1_original_type, location_t op1_loc,
5330                         tree op2, tree op2_original_type, location_t op2_loc)
5331 {
5332   tree type1;
5333   tree type2;
5334   enum tree_code code1;
5335   enum tree_code code2;
5336   tree result_type = NULL;
5337   tree semantic_result_type = NULL;
5338   tree orig_op1 = op1, orig_op2 = op2;
5339   bool int_const, op1_int_operands, op2_int_operands, int_operands;
5340   bool ifexp_int_operands;
5341   tree ret;
5342
5343   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5344   if (op1_int_operands)
5345     op1 = remove_c_maybe_const_expr (op1);
5346   op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5347   if (op2_int_operands)
5348     op2 = remove_c_maybe_const_expr (op2);
5349   ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5350   if (ifexp_int_operands)
5351     ifexp = remove_c_maybe_const_expr (ifexp);
5352
5353   /* Promote both alternatives.  */
5354
5355   if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5356     op1 = default_conversion (op1);
5357   if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5358     op2 = default_conversion (op2);
5359
5360   if (TREE_CODE (ifexp) == ERROR_MARK
5361       || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5362       || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5363     return error_mark_node;
5364
5365   tree bltin1 = NULL_TREE;
5366   tree bltin2 = NULL_TREE;
5367   type1 = type_or_builtin_type (op1, &bltin1);
5368   code1 = TREE_CODE (type1);
5369   type2 = type_or_builtin_type (op2, &bltin2);
5370   code2 = TREE_CODE (type2);
5371
5372   if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5373     return error_mark_node;
5374
5375   if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5376     return error_mark_node;
5377
5378   /* C90 does not permit non-lvalue arrays in conditional expressions.
5379      In C99 they will be pointers by now.  */
5380   if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5381     {
5382       error_at (colon_loc, "non-lvalue array in conditional expression");
5383       return error_mark_node;
5384     }
5385
5386   if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5387        || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5388       && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5389           || code1 == COMPLEX_TYPE)
5390       && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5391           || code2 == COMPLEX_TYPE))
5392     {
5393       semantic_result_type = c_common_type (type1, type2);
5394       if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5395         {
5396           op1 = TREE_OPERAND (op1, 0);
5397           type1 = TREE_TYPE (op1);
5398           gcc_assert (TREE_CODE (type1) == code1);
5399         }
5400       if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5401         {
5402           op2 = TREE_OPERAND (op2, 0);
5403           type2 = TREE_TYPE (op2);
5404           gcc_assert (TREE_CODE (type2) == code2);
5405         }
5406     }
5407
5408   if (warn_cxx_compat)
5409     {
5410       tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5411       tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5412
5413       if (TREE_CODE (t1) == ENUMERAL_TYPE
5414           && TREE_CODE (t2) == ENUMERAL_TYPE
5415           && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5416         warning_at (colon_loc, OPT_Wc___compat,
5417                     ("different enum types in conditional is "
5418                      "invalid in C++: %qT vs %qT"),
5419                     t1, t2);
5420     }
5421
5422   /* Quickly detect the usual case where op1 and op2 have the same type
5423      after promotion.  */
5424   if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5425     {
5426       if (type1 == type2)
5427         result_type = type1;
5428       else
5429         result_type = TYPE_MAIN_VARIANT (type1);
5430     }
5431   else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5432             || code1 == COMPLEX_TYPE)
5433            && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5434                || code2 == COMPLEX_TYPE))
5435     {
5436       /* In C11, a conditional expression between a floating-point
5437          type and an integer type should convert the integer type to
5438          the evaluation format of the floating-point type, with
5439          possible excess precision.  */
5440       tree eptype1 = type1;
5441       tree eptype2 = type2;
5442       if (flag_isoc11)
5443         {
5444           tree eptype;
5445           if (ANY_INTEGRAL_TYPE_P (type1)
5446               && (eptype = excess_precision_type (type2)) != NULL_TREE)
5447             {
5448               eptype2 = eptype;
5449               if (!semantic_result_type)
5450                 semantic_result_type = c_common_type (type1, type2);
5451             }
5452           else if (ANY_INTEGRAL_TYPE_P (type2)
5453                    && (eptype = excess_precision_type (type1)) != NULL_TREE)
5454             {
5455               eptype1 = eptype;
5456               if (!semantic_result_type)
5457                 semantic_result_type = c_common_type (type1, type2);
5458             }
5459         }
5460       result_type = c_common_type (eptype1, eptype2);
5461       if (result_type == error_mark_node)
5462         return error_mark_node;
5463       do_warn_double_promotion (result_type, type1, type2,
5464                                 "implicit conversion from %qT to %qT to "
5465                                 "match other result of conditional",
5466                                 colon_loc);
5467
5468       /* If -Wsign-compare, warn here if type1 and type2 have
5469          different signedness.  We'll promote the signed to unsigned
5470          and later code won't know it used to be different.
5471          Do this check on the original types, so that explicit casts
5472          will be considered, but default promotions won't.  */
5473       if (c_inhibit_evaluation_warnings == 0)
5474         {
5475           int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5476           int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5477
5478           if (unsigned_op1 ^ unsigned_op2)
5479             {
5480               bool ovf;
5481
5482               /* Do not warn if the result type is signed, since the
5483                  signed type will only be chosen if it can represent
5484                  all the values of the unsigned type.  */
5485               if (!TYPE_UNSIGNED (result_type))
5486                 /* OK */;
5487               else
5488                 {
5489                   bool op1_maybe_const = true;
5490                   bool op2_maybe_const = true;
5491
5492                   /* Do not warn if the signed quantity is an
5493                      unsuffixed integer literal (or some static
5494                      constant expression involving such literals) and
5495                      it is non-negative.  This warning requires the
5496                      operands to be folded for best results, so do
5497                      that folding in this case even without
5498                      warn_sign_compare to avoid warning options
5499                      possibly affecting code generation.  */
5500                   c_inhibit_evaluation_warnings
5501                     += (ifexp == truthvalue_false_node);
5502                   op1 = c_fully_fold (op1, require_constant_value,
5503                                       &op1_maybe_const);
5504                   c_inhibit_evaluation_warnings
5505                     -= (ifexp == truthvalue_false_node);
5506
5507                   c_inhibit_evaluation_warnings
5508                     += (ifexp == truthvalue_true_node);
5509                   op2 = c_fully_fold (op2, require_constant_value,
5510                                       &op2_maybe_const);
5511                   c_inhibit_evaluation_warnings
5512                     -= (ifexp == truthvalue_true_node);
5513
5514                   if (warn_sign_compare)
5515                     {
5516                       if ((unsigned_op2
5517                            && tree_expr_nonnegative_warnv_p (op1, &ovf))
5518                           || (unsigned_op1
5519                               && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5520                         /* OK */;
5521                       else if (unsigned_op2)
5522                         warning_at (op1_loc, OPT_Wsign_compare,
5523                                     "operand of %<?:%> changes signedness from "
5524                                     "%qT to %qT due to unsignedness of other "
5525                                     "operand", TREE_TYPE (orig_op1),
5526                                     TREE_TYPE (orig_op2));
5527                       else
5528                         warning_at (op2_loc, OPT_Wsign_compare,
5529                                     "operand of %<?:%> changes signedness from "
5530                                     "%qT to %qT due to unsignedness of other "
5531                                     "operand", TREE_TYPE (orig_op2),
5532                                     TREE_TYPE (orig_op1));
5533                     }
5534                   if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5535                     op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5536                   if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5537                     op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5538                 }
5539             }
5540         }
5541     }
5542   else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5543     {
5544       if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5545         pedwarn (colon_loc, OPT_Wpedantic,
5546                  "ISO C forbids conditional expr with only one void side");
5547       result_type = void_type_node;
5548     }
5549   else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5550     {
5551       addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5552       addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5553       addr_space_t as_common;
5554
5555       if (comp_target_types (colon_loc, type1, type2))
5556         result_type = common_pointer_type (type1, type2);
5557       else if (null_pointer_constant_p (orig_op1))
5558         result_type = type2;
5559       else if (null_pointer_constant_p (orig_op2))
5560         result_type = type1;
5561       else if (!addr_space_superset (as1, as2, &as_common))
5562         {
5563           error_at (colon_loc, "pointers to disjoint address spaces "
5564                     "used in conditional expression");
5565           return error_mark_node;
5566         }
5567       else if ((VOID_TYPE_P (TREE_TYPE (type1))
5568                 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5569                || (VOID_TYPE_P (TREE_TYPE (type2))
5570                    && !TYPE_ATOMIC (TREE_TYPE (type2))))
5571         {
5572           tree t1 = TREE_TYPE (type1);
5573           tree t2 = TREE_TYPE (type2);
5574           if (!(VOID_TYPE_P (t1)
5575                 && !TYPE_ATOMIC (t1)))
5576            {
5577              /* roles are swapped */
5578              t1 = t2;
5579              t2 = TREE_TYPE (type1);
5580            }
5581           tree t2_stripped = strip_array_types (t2);
5582           if ((TREE_CODE (t2) == ARRAY_TYPE)
5583               && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5584             {
5585               if (!flag_isoc2x)
5586                 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5587                             "pointer to array loses qualifier "
5588                             "in conditional expression");
5589               else if (warn_c11_c2x_compat > 0)
5590                 warning_at (colon_loc, OPT_Wc11_c2x_compat,
5591                             "pointer to array loses qualifier "
5592                             "in conditional expression in ISO C before C2X");
5593             }
5594           if (TREE_CODE (t2) == FUNCTION_TYPE)
5595             pedwarn (colon_loc, OPT_Wpedantic,
5596                      "ISO C forbids conditional expr between "
5597                      "%<void *%> and function pointer");
5598           /* for array, use qualifiers of element type */
5599           if (flag_isoc2x)
5600             t2 = t2_stripped;
5601           result_type = build_pointer_type (qualify_type (t1, t2));
5602         }
5603       /* Objective-C pointer comparisons are a bit more lenient.  */
5604       else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5605         result_type = objc_common_type (type1, type2);
5606       else
5607         {
5608           int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5609           if (bltin1 && bltin2)
5610             warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5611                         "pointer type mismatch between %qT and %qT "
5612                         "of %qD and %qD in conditional expression",
5613                         type1, type2, bltin1, bltin2);
5614           else
5615             pedwarn (colon_loc, 0,
5616                      "pointer type mismatch in conditional expression");
5617           result_type = build_pointer_type
5618                           (build_qualified_type (void_type_node, qual));
5619         }
5620     }
5621   else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5622     {
5623       if (!null_pointer_constant_p (orig_op2))
5624         pedwarn (colon_loc, 0,
5625                  "pointer/integer type mismatch in conditional expression");
5626       else
5627         {
5628           op2 = null_pointer_node;
5629         }
5630       result_type = type1;
5631     }
5632   else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5633     {
5634       if (!null_pointer_constant_p (orig_op1))
5635         pedwarn (colon_loc, 0,
5636                  "pointer/integer type mismatch in conditional expression");
5637       else
5638         {
5639           op1 = null_pointer_node;
5640         }
5641       result_type = type2;
5642     }
5643   /* 6.5.15: "if one is a null pointer constant (other than a pointer) or has
5644      type nullptr_t and the other is a pointer, the result type is the pointer
5645      type."  */
5646   else if (code1 == NULLPTR_TYPE && code2 == POINTER_TYPE)
5647     result_type = type2;
5648   else if (code1 == POINTER_TYPE && code2 == NULLPTR_TYPE)
5649     result_type = type1;
5650
5651   if (!result_type)
5652     {
5653       if (flag_cond_mismatch)
5654         result_type = void_type_node;
5655       else
5656         {
5657           error_at (colon_loc, "type mismatch in conditional expression");
5658           return error_mark_node;
5659         }
5660     }
5661
5662   /* Merge const and volatile flags of the incoming types.  */
5663   result_type
5664     = build_type_variant (result_type,
5665                           TYPE_READONLY (type1) || TYPE_READONLY (type2),
5666                           TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5667
5668   op1 = ep_convert_and_check (colon_loc, result_type, op1,
5669                               semantic_result_type);
5670   op2 = ep_convert_and_check (colon_loc, result_type, op2,
5671                               semantic_result_type);
5672
5673   if (ifexp_bcp && ifexp == truthvalue_true_node)
5674     {
5675       op2_int_operands = true;
5676       op1 = c_fully_fold (op1, require_constant_value, NULL);
5677     }
5678   if (ifexp_bcp && ifexp == truthvalue_false_node)
5679     {
5680       op1_int_operands = true;
5681       op2 = c_fully_fold (op2, require_constant_value, NULL);
5682     }
5683   int_const = int_operands = (ifexp_int_operands
5684                               && op1_int_operands
5685                               && op2_int_operands);
5686   if (int_operands)
5687     {
5688       int_const = ((ifexp == truthvalue_true_node
5689                     && TREE_CODE (orig_op1) == INTEGER_CST
5690                     && !TREE_OVERFLOW (orig_op1))
5691                    || (ifexp == truthvalue_false_node
5692                        && TREE_CODE (orig_op2) == INTEGER_CST
5693                        && !TREE_OVERFLOW (orig_op2)));
5694     }
5695
5696   /* Need to convert condition operand into a vector mask.  */
5697   if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5698     {
5699       tree vectype = TREE_TYPE (ifexp);
5700       tree elem_type = TREE_TYPE (vectype);
5701       tree zero = build_int_cst (elem_type, 0);
5702       tree zero_vec = build_vector_from_val (vectype, zero);
5703       tree cmp_type = truth_type_for (vectype);
5704       ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5705     }
5706
5707   if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5708     ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5709   else
5710     {
5711       if (int_operands)
5712         {
5713           /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5714              nested inside of the expression.  */
5715           op1 = c_fully_fold (op1, false, NULL);
5716           op2 = c_fully_fold (op2, false, NULL);
5717         }
5718       ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5719       if (int_operands)
5720         ret = note_integer_operands (ret);
5721     }
5722   if (semantic_result_type)
5723     ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5724
5725   protected_set_expr_location (ret, colon_loc);
5726
5727   /* If the OP1 and OP2 are the same and don't have side-effects,
5728      warn here, because the COND_EXPR will be turned into OP1.  */
5729   if (warn_duplicated_branches
5730       && TREE_CODE (ret) == COND_EXPR
5731       && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5732     warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5733                 "this condition has identical branches");
5734
5735   return ret;
5736 }
5737 \f
5738 /* EXPR is an expression, location LOC, whose result is discarded.
5739    Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5740    whose right-hand operand is such a call, possibly recursively).  */
5741
5742 static void
5743 maybe_warn_nodiscard (location_t loc, tree expr)
5744 {
5745   if (VOID_TYPE_P (TREE_TYPE (expr)))
5746     return;
5747   while (TREE_CODE (expr) == COMPOUND_EXPR)
5748     {
5749       expr = TREE_OPERAND (expr, 1);
5750       if (EXPR_HAS_LOCATION (expr))
5751         loc = EXPR_LOCATION (expr);
5752     }
5753   if (TREE_CODE (expr) != CALL_EXPR)
5754     return;
5755   tree fn = CALL_EXPR_FN (expr);
5756   if (!fn)
5757     return;
5758   tree attr;
5759   if (TREE_CODE (fn) == ADDR_EXPR
5760       && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5761       && (attr = lookup_attribute ("nodiscard",
5762                                    DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5763     {
5764       fn = TREE_OPERAND (fn, 0);
5765       tree args = TREE_VALUE (attr);
5766       if (args)
5767         args = TREE_VALUE (args);
5768       auto_diagnostic_group d;
5769       int warned;
5770       if (args)
5771         warned = warning_at (loc, OPT_Wunused_result,
5772                              "ignoring return value of %qD, declared with "
5773                              "attribute %<nodiscard%>: %E", fn, args);
5774       else
5775         warned = warning_at (loc, OPT_Wunused_result,
5776                              "ignoring return value of %qD, declared with "
5777                              "attribute %<nodiscard%>", fn);
5778       if (warned)
5779         inform (DECL_SOURCE_LOCATION (fn), "declared here");
5780     }
5781   else
5782     {
5783       tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5784       attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5785       if (!attr)
5786         return;
5787       tree args = TREE_VALUE (attr);
5788       if (args)
5789         args = TREE_VALUE (args);
5790       auto_diagnostic_group d;
5791       int warned;
5792       if (args)
5793         warned = warning_at (loc, OPT_Wunused_result,
5794                              "ignoring return value of type %qT, declared "
5795                              "with attribute %<nodiscard%>: %E",
5796                              rettype, args);
5797       else
5798         warned = warning_at (loc, OPT_Wunused_result,
5799                              "ignoring return value of type %qT, declared "
5800                              "with attribute %<nodiscard%>", rettype);
5801       if (warned)
5802         {
5803           if (TREE_CODE (fn) == ADDR_EXPR)
5804             {
5805               fn = TREE_OPERAND (fn, 0);
5806               if (TREE_CODE (fn) == FUNCTION_DECL)
5807                 inform (DECL_SOURCE_LOCATION (fn),
5808                         "in call to %qD, declared here", fn);
5809             }
5810         }
5811     }
5812 }
5813
5814 /* Return a compound expression that performs two expressions and
5815    returns the value of the second of them.
5816
5817    LOC is the location of the COMPOUND_EXPR.  */
5818
5819 tree
5820 build_compound_expr (location_t loc, tree expr1, tree expr2)
5821 {
5822   bool expr1_int_operands, expr2_int_operands;
5823   tree eptype = NULL_TREE;
5824   tree ret;
5825
5826   expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5827   if (expr1_int_operands)
5828     expr1 = remove_c_maybe_const_expr (expr1);
5829   expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5830   if (expr2_int_operands)
5831     expr2 = remove_c_maybe_const_expr (expr2);
5832
5833   if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5834     expr1 = TREE_OPERAND (expr1, 0);
5835   if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5836     {
5837       eptype = TREE_TYPE (expr2);
5838       expr2 = TREE_OPERAND (expr2, 0);
5839     }
5840
5841   if (!TREE_SIDE_EFFECTS (expr1))
5842     {
5843       /* The left-hand operand of a comma expression is like an expression
5844          statement: with -Wunused, we should warn if it doesn't have
5845          any side-effects, unless it was explicitly cast to (void).  */
5846       if (warn_unused_value)
5847         {
5848           if (VOID_TYPE_P (TREE_TYPE (expr1))
5849               && CONVERT_EXPR_P (expr1))
5850             ; /* (void) a, b */
5851           else if (VOID_TYPE_P (TREE_TYPE (expr1))
5852                    && TREE_CODE (expr1) == COMPOUND_EXPR
5853                    && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5854             ; /* (void) a, (void) b, c */
5855           else
5856             warning_at (loc, OPT_Wunused_value,
5857                         "left-hand operand of comma expression has no effect");
5858         }
5859     }
5860   else if (TREE_CODE (expr1) == COMPOUND_EXPR
5861            && warn_unused_value)
5862     {
5863       tree r = expr1;
5864       location_t cloc = loc;
5865       while (TREE_CODE (r) == COMPOUND_EXPR)
5866         {
5867           if (EXPR_HAS_LOCATION (r))
5868             cloc = EXPR_LOCATION (r);
5869           r = TREE_OPERAND (r, 1);
5870         }
5871       if (!TREE_SIDE_EFFECTS (r)
5872           && !VOID_TYPE_P (TREE_TYPE (r))
5873           && !CONVERT_EXPR_P (r))
5874         warning_at (cloc, OPT_Wunused_value,
5875                     "right-hand operand of comma expression has no effect");
5876     }
5877
5878   /* With -Wunused, we should also warn if the left-hand operand does have
5879      side-effects, but computes a value which is not used.  For example, in
5880      `foo() + bar(), baz()' the result of the `+' operator is not used,
5881      so we should issue a warning.  */
5882   else if (warn_unused_value)
5883     warn_if_unused_value (expr1, loc);
5884
5885   maybe_warn_nodiscard (loc, expr1);
5886
5887   if (expr2 == error_mark_node)
5888     return error_mark_node;
5889
5890   ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5891
5892   if (flag_isoc99
5893       && expr1_int_operands
5894       && expr2_int_operands)
5895     ret = note_integer_operands (ret);
5896
5897   if (eptype)
5898     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5899
5900   protected_set_expr_location (ret, loc);
5901   return ret;
5902 }
5903
5904 /* Issue -Wcast-qual warnings when appropriate.  TYPE is the type to
5905    which we are casting.  OTYPE is the type of the expression being
5906    cast.  Both TYPE and OTYPE are pointer types.  LOC is the location
5907    of the cast.  -Wcast-qual appeared on the command line.  Named
5908    address space qualifiers are not handled here, because they result
5909    in different warnings.  */
5910
5911 static void
5912 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5913 {
5914   tree in_type = type;
5915   tree in_otype = otype;
5916   int added = 0;
5917   int discarded = 0;
5918   bool is_const;
5919
5920   /* Check that the qualifiers on IN_TYPE are a superset of the
5921      qualifiers of IN_OTYPE.  The outermost level of POINTER_TYPE
5922      nodes is uninteresting and we stop as soon as we hit a
5923      non-POINTER_TYPE node on either type.  */
5924   do
5925     {
5926       in_otype = TREE_TYPE (in_otype);
5927       in_type = TREE_TYPE (in_type);
5928
5929       /* GNU C allows cv-qualified function types.  'const' means the
5930          function is very pure, 'volatile' means it can't return.  We
5931          need to warn when such qualifiers are added, not when they're
5932          taken away.  */
5933       if (TREE_CODE (in_otype) == FUNCTION_TYPE
5934           && TREE_CODE (in_type) == FUNCTION_TYPE)
5935         added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5936                   & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5937       else
5938         discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5939                       & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5940     }
5941   while (TREE_CODE (in_type) == POINTER_TYPE
5942          && TREE_CODE (in_otype) == POINTER_TYPE);
5943
5944   if (added)
5945     warning_at (loc, OPT_Wcast_qual,
5946                 "cast adds %q#v qualifier to function type", added);
5947
5948   if (discarded)
5949     /* There are qualifiers present in IN_OTYPE that are not present
5950        in IN_TYPE.  */
5951     warning_at (loc, OPT_Wcast_qual,
5952                 "cast discards %qv qualifier from pointer target type",
5953                 discarded);
5954
5955   if (added || discarded)
5956     return;
5957
5958   /* A cast from **T to const **T is unsafe, because it can cause a
5959      const value to be changed with no additional warning.  We only
5960      issue this warning if T is the same on both sides, and we only
5961      issue the warning if there are the same number of pointers on
5962      both sides, as otherwise the cast is clearly unsafe anyhow.  A
5963      cast is unsafe when a qualifier is added at one level and const
5964      is not present at all outer levels.
5965
5966      To issue this warning, we check at each level whether the cast
5967      adds new qualifiers not already seen.  We don't need to special
5968      case function types, as they won't have the same
5969      TYPE_MAIN_VARIANT.  */
5970
5971   if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5972     return;
5973   if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5974     return;
5975
5976   in_type = type;
5977   in_otype = otype;
5978   is_const = TYPE_READONLY (TREE_TYPE (in_type));
5979   do
5980     {
5981       in_type = TREE_TYPE (in_type);
5982       in_otype = TREE_TYPE (in_otype);
5983       if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5984           && !is_const)
5985         {
5986           warning_at (loc, OPT_Wcast_qual,
5987                       "to be safe all intermediate pointers in cast from "
5988                       "%qT to %qT must be %<const%> qualified",
5989                       otype, type);
5990           break;
5991         }
5992       if (is_const)
5993         is_const = TYPE_READONLY (in_type);
5994     }
5995   while (TREE_CODE (in_type) == POINTER_TYPE);
5996 }
5997
5998 /* Heuristic check if two parameter types can be considered ABI-equivalent.  */
5999
6000 static bool
6001 c_safe_arg_type_equiv_p (tree t1, tree t2)
6002 {
6003   t1 = TYPE_MAIN_VARIANT (t1);
6004   t2 = TYPE_MAIN_VARIANT (t2);
6005
6006   if (TREE_CODE (t1) == POINTER_TYPE
6007       && TREE_CODE (t2) == POINTER_TYPE)
6008     return true;
6009
6010   /* The signedness of the parameter matters only when an integral
6011      type smaller than int is promoted to int, otherwise only the
6012      precision of the parameter matters.
6013      This check should make sure that the callee does not see
6014      undefined values in argument registers.  */
6015   if (INTEGRAL_TYPE_P (t1)
6016       && INTEGRAL_TYPE_P (t2)
6017       && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
6018       && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
6019           || !targetm.calls.promote_prototypes (NULL_TREE)
6020           || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
6021     return true;
6022
6023   return comptypes (t1, t2);
6024 }
6025
6026 /* Check if a type cast between two function types can be considered safe.  */
6027
6028 static bool
6029 c_safe_function_type_cast_p (tree t1, tree t2)
6030 {
6031   if (TREE_TYPE (t1) == void_type_node &&
6032       TYPE_ARG_TYPES (t1) == void_list_node)
6033     return true;
6034
6035   if (TREE_TYPE (t2) == void_type_node &&
6036       TYPE_ARG_TYPES (t2) == void_list_node)
6037     return true;
6038
6039   if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
6040     return false;
6041
6042   for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
6043        t1 && t2;
6044        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
6045     if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
6046       return false;
6047
6048   return true;
6049 }
6050
6051 /* Build an expression representing a cast to type TYPE of expression EXPR.
6052    LOC is the location of the cast-- typically the open paren of the cast.  */
6053
6054 tree
6055 build_c_cast (location_t loc, tree type, tree expr)
6056 {
6057   tree value;
6058
6059   bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
6060
6061   if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
6062     expr = TREE_OPERAND (expr, 0);
6063
6064   value = expr;
6065   if (int_operands)
6066     value = remove_c_maybe_const_expr (value);
6067
6068   if (type == error_mark_node || expr == error_mark_node)
6069     return error_mark_node;
6070
6071   /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
6072      only in <protocol> qualifications.  But when constructing cast expressions,
6073      the protocols do matter and must be kept around.  */
6074   if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
6075     return build1 (NOP_EXPR, type, expr);
6076
6077   type = TYPE_MAIN_VARIANT (type);
6078
6079   if (TREE_CODE (type) == ARRAY_TYPE)
6080     {
6081       error_at (loc, "cast specifies array type");
6082       return error_mark_node;
6083     }
6084
6085   if (TREE_CODE (type) == FUNCTION_TYPE)
6086     {
6087       error_at (loc, "cast specifies function type");
6088       return error_mark_node;
6089     }
6090
6091   if (!VOID_TYPE_P (type))
6092     {
6093       value = require_complete_type (loc, value);
6094       if (value == error_mark_node)
6095         return error_mark_node;
6096     }
6097
6098   if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
6099     {
6100       if (RECORD_OR_UNION_TYPE_P (type))
6101         pedwarn (loc, OPT_Wpedantic,
6102                  "ISO C forbids casting nonscalar to the same type");
6103
6104       /* Convert to remove any qualifiers from VALUE's type.  */
6105       value = convert (type, value);
6106     }
6107   else if (TREE_CODE (type) == UNION_TYPE)
6108     {
6109       tree field;
6110
6111       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6112         if (TREE_TYPE (field) != error_mark_node
6113             && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
6114                           TYPE_MAIN_VARIANT (TREE_TYPE (value))))
6115           break;
6116
6117       if (field)
6118         {
6119           tree t;
6120           bool maybe_const = true;
6121
6122           pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
6123           t = c_fully_fold (value, false, &maybe_const);
6124           t = build_constructor_single (type, field, t);
6125           if (!maybe_const)
6126             t = c_wrap_maybe_const (t, true);
6127           t = digest_init (loc, type, t,
6128                            NULL_TREE, false, false, false, true, false, false);
6129           TREE_CONSTANT (t) = TREE_CONSTANT (value);
6130           return t;
6131         }
6132       error_at (loc, "cast to union type from type not present in union");
6133       return error_mark_node;
6134     }
6135   else
6136     {
6137       tree otype, ovalue;
6138
6139       if (type == void_type_node)
6140         {
6141           tree t = build1 (CONVERT_EXPR, type, value);
6142           SET_EXPR_LOCATION (t, loc);
6143           return t;
6144         }
6145
6146       otype = TREE_TYPE (value);
6147
6148       /* Optionally warn about potentially worrisome casts.  */
6149       if (warn_cast_qual
6150           && TREE_CODE (type) == POINTER_TYPE
6151           && TREE_CODE (otype) == POINTER_TYPE)
6152         handle_warn_cast_qual (loc, type, otype);
6153
6154       /* Warn about conversions between pointers to disjoint
6155          address spaces.  */
6156       if (TREE_CODE (type) == POINTER_TYPE
6157           && TREE_CODE (otype) == POINTER_TYPE
6158           && !null_pointer_constant_p (value))
6159         {
6160           addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6161           addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6162           addr_space_t as_common;
6163
6164           if (!addr_space_superset (as_to, as_from, &as_common))
6165             {
6166               if (ADDR_SPACE_GENERIC_P (as_from))
6167                 warning_at (loc, 0, "cast to %qs address space pointer "
6168                             "from disjoint generic address space pointer",
6169                             c_addr_space_name (as_to));
6170
6171               else if (ADDR_SPACE_GENERIC_P (as_to))
6172                 warning_at (loc, 0, "cast to generic address space pointer "
6173                             "from disjoint %qs address space pointer",
6174                             c_addr_space_name (as_from));
6175
6176               else
6177                 warning_at (loc, 0, "cast to %qs address space pointer "
6178                             "from disjoint %qs address space pointer",
6179                             c_addr_space_name (as_to),
6180                             c_addr_space_name (as_from));
6181             }
6182         }
6183
6184       /* Warn about possible alignment problems.  */
6185       if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6186           && TREE_CODE (type) == POINTER_TYPE
6187           && TREE_CODE (otype) == POINTER_TYPE
6188           && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6189           && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6190           /* Don't warn about opaque types, where the actual alignment
6191              restriction is unknown.  */
6192           && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6193                && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6194           && min_align_of_type (TREE_TYPE (type))
6195              > min_align_of_type (TREE_TYPE (otype)))
6196         warning_at (loc, OPT_Wcast_align,
6197                     "cast increases required alignment of target type");
6198
6199       if (TREE_CODE (type) == INTEGER_TYPE
6200           && TREE_CODE (otype) == POINTER_TYPE
6201           && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6202       /* Unlike conversion of integers to pointers, where the
6203          warning is disabled for converting constants because
6204          of cases such as SIG_*, warn about converting constant
6205          pointers to integers. In some cases it may cause unwanted
6206          sign extension, and a warning is appropriate.  */
6207         warning_at (loc, OPT_Wpointer_to_int_cast,
6208                     "cast from pointer to integer of different size");
6209
6210       if (TREE_CODE (value) == CALL_EXPR
6211           && TREE_CODE (type) != TREE_CODE (otype))
6212         warning_at (loc, OPT_Wbad_function_cast,
6213                     "cast from function call of type %qT "
6214                     "to non-matching type %qT", otype, type);
6215
6216       if (TREE_CODE (type) == POINTER_TYPE
6217           && TREE_CODE (otype) == INTEGER_TYPE
6218           && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6219           /* Don't warn about converting any constant.  */
6220           && !TREE_CONSTANT (value))
6221         warning_at (loc,
6222                     OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6223                     "of different size");
6224
6225       if (warn_strict_aliasing <= 2)
6226         strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6227
6228       /* If pedantic, warn for conversions between function and object
6229          pointer types, except for converting a null pointer constant
6230          to function pointer type.  */
6231       if (pedantic
6232           && TREE_CODE (type) == POINTER_TYPE
6233           && TREE_CODE (otype) == POINTER_TYPE
6234           && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6235           && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6236         pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6237                  "conversion of function pointer to object pointer type");
6238
6239       if (pedantic
6240           && TREE_CODE (type) == POINTER_TYPE
6241           && TREE_CODE (otype) == POINTER_TYPE
6242           && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6243           && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6244           && !null_pointer_constant_p (value))
6245         pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6246                  "conversion of object pointer to function pointer type");
6247
6248       if (TREE_CODE (type) == POINTER_TYPE
6249           && TREE_CODE (otype) == POINTER_TYPE
6250           && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6251           && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6252           && !c_safe_function_type_cast_p (TREE_TYPE (type),
6253                                            TREE_TYPE (otype)))
6254         warning_at (loc, OPT_Wcast_function_type,
6255                     "cast between incompatible function types"
6256                     " from %qT to %qT", otype, type);
6257
6258       ovalue = value;
6259       value = convert (type, value);
6260
6261       /* Ignore any integer overflow caused by the cast.  */
6262       if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6263         {
6264           if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6265             {
6266               if (!TREE_OVERFLOW (value))
6267                 {
6268                   /* Avoid clobbering a shared constant.  */
6269                   value = copy_node (value);
6270                   TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6271                 }
6272             }
6273           else if (TREE_OVERFLOW (value))
6274             /* Reset VALUE's overflow flags, ensuring constant sharing.  */
6275             value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6276         }
6277     }
6278
6279   /* Don't let a cast be an lvalue.  */
6280   if (lvalue_p (value))
6281     value = non_lvalue_loc (loc, value);
6282
6283   /* Don't allow the results of casting to floating-point or complex
6284      types be confused with actual constants, or casts involving
6285      integer and pointer types other than direct integer-to-integer
6286      and integer-to-pointer be confused with integer constant
6287      expressions and null pointer constants.  */
6288   if (TREE_CODE (value) == REAL_CST
6289       || TREE_CODE (value) == COMPLEX_CST
6290       || (TREE_CODE (value) == INTEGER_CST
6291           && !((TREE_CODE (expr) == INTEGER_CST
6292                 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6293                || TREE_CODE (expr) == REAL_CST
6294                || TREE_CODE (expr) == COMPLEX_CST)))
6295       value = build1 (NOP_EXPR, type, value);
6296
6297   /* If the expression has integer operands and so can occur in an
6298      unevaluated part of an integer constant expression, ensure the
6299      return value reflects this.  */
6300   if (int_operands
6301       && INTEGRAL_TYPE_P (type)
6302       && value != error_mark_node
6303       && !EXPR_INT_CONST_OPERANDS (value))
6304     value = note_integer_operands (value);
6305
6306   protected_set_expr_location (value, loc);
6307   return value;
6308 }
6309
6310 /* Interpret a cast of expression EXPR to type TYPE.  LOC is the
6311    location of the open paren of the cast, or the position of the cast
6312    expr.  */
6313 tree
6314 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6315 {
6316   tree type;
6317   tree type_expr = NULL_TREE;
6318   bool type_expr_const = true;
6319   tree ret;
6320   int saved_wsp = warn_strict_prototypes;
6321
6322   /* This avoids warnings about unprototyped casts on
6323      integers.  E.g. "#define SIG_DFL (void(*)())0".  */
6324   if (TREE_CODE (expr) == INTEGER_CST)
6325     warn_strict_prototypes = 0;
6326   type = groktypename (type_name, &type_expr, &type_expr_const);
6327   warn_strict_prototypes = saved_wsp;
6328
6329   if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6330       && reject_gcc_builtin (expr))
6331     return error_mark_node;
6332
6333   ret = build_c_cast (loc, type, expr);
6334   if (type_expr)
6335     {
6336       bool inner_expr_const = true;
6337       ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6338       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6339       C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6340                                              && inner_expr_const);
6341       SET_EXPR_LOCATION (ret, loc);
6342     }
6343
6344   if (!EXPR_HAS_LOCATION (ret))
6345     protected_set_expr_location (ret, loc);
6346
6347   /* C++ does not permits types to be defined in a cast, but it
6348      allows references to incomplete types.  */
6349   if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6350     warning_at (loc, OPT_Wc___compat,
6351                 "defining a type in a cast is invalid in C++");
6352
6353   return ret;
6354 }
6355 \f
6356 /* Build an assignment expression of lvalue LHS from value RHS.
6357    If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6358    may differ from TREE_TYPE (LHS) for an enum bitfield.
6359    MODIFYCODE is the code for a binary operator that we use
6360    to combine the old value of LHS with RHS to get the new value.
6361    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6362    If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6363    which may differ from TREE_TYPE (RHS) for an enum value.
6364
6365    LOCATION is the location of the MODIFYCODE operator.
6366    RHS_LOC is the location of the RHS.  */
6367
6368 tree
6369 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6370                    enum tree_code modifycode,
6371                    location_t rhs_loc, tree rhs, tree rhs_origtype)
6372 {
6373   tree result;
6374   tree newrhs;
6375   tree rhseval = NULL_TREE;
6376   tree lhstype = TREE_TYPE (lhs);
6377   tree olhstype = lhstype;
6378   bool npc;
6379   bool is_atomic_op;
6380
6381   /* Types that aren't fully specified cannot be used in assignments.  */
6382   lhs = require_complete_type (location, lhs);
6383
6384   /* Avoid duplicate error messages from operands that had errors.  */
6385   if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6386     return error_mark_node;
6387
6388   /* Ensure an error for assigning a non-lvalue array to an array in
6389      C90.  */
6390   if (TREE_CODE (lhstype) == ARRAY_TYPE)
6391     {
6392       error_at (location, "assignment to expression with array type");
6393       return error_mark_node;
6394     }
6395
6396   /* For ObjC properties, defer this check.  */
6397   if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6398     return error_mark_node;
6399
6400   is_atomic_op = really_atomic_lvalue (lhs);
6401
6402   newrhs = rhs;
6403
6404   if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6405     {
6406       tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6407                                       lhs_origtype, modifycode, rhs_loc, rhs,
6408                                       rhs_origtype);
6409       if (inner == error_mark_node)
6410         return error_mark_node;
6411       result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6412                        C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6413       gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6414       C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6415       protected_set_expr_location (result, location);
6416       return result;
6417     }
6418
6419   /* If a binary op has been requested, combine the old LHS value with the RHS
6420      producing the value we should actually store into the LHS.  */
6421
6422   if (modifycode != NOP_EXPR)
6423     {
6424       lhs = c_fully_fold (lhs, false, NULL, true);
6425       lhs = stabilize_reference (lhs);
6426
6427       /* Construct the RHS for any non-atomic compound assignemnt. */
6428       if (!is_atomic_op)
6429         {
6430           /* If in LHS op= RHS the RHS has side-effects, ensure they
6431              are preevaluated before the rest of the assignment expression's
6432              side-effects, because RHS could contain e.g. function calls
6433              that modify LHS.  */
6434           if (TREE_SIDE_EFFECTS (rhs))
6435             {
6436               if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6437                 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6438               else
6439                 newrhs = save_expr (rhs);
6440               rhseval = newrhs;
6441               if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6442                 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6443                                  newrhs);
6444             }
6445           newrhs = build_binary_op (location,
6446                                     modifycode, lhs, newrhs, true);
6447
6448           /* The original type of the right hand side is no longer
6449              meaningful.  */
6450           rhs_origtype = NULL_TREE;
6451         }
6452     }
6453
6454   if (c_dialect_objc ())
6455     {
6456       /* Check if we are modifying an Objective-C property reference;
6457          if so, we need to generate setter calls.  */
6458       if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6459         result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6460       else
6461         result = objc_maybe_build_modify_expr (lhs, newrhs);
6462       if (result)
6463         goto return_result;
6464
6465       /* Else, do the check that we postponed for Objective-C.  */
6466       if (!lvalue_or_else (location, lhs, lv_assign))
6467         return error_mark_node;
6468     }
6469
6470   /* Give an error for storing in something that is 'const'.  */
6471
6472   if (TYPE_READONLY (lhstype)
6473       || (RECORD_OR_UNION_TYPE_P (lhstype)
6474           && C_TYPE_FIELDS_READONLY (lhstype)))
6475     {
6476       readonly_error (location, lhs, lv_assign);
6477       return error_mark_node;
6478     }
6479   else if (TREE_READONLY (lhs))
6480     readonly_warning (lhs, lv_assign);
6481
6482   /* If storing into a structure or union member,
6483      it has probably been given type `int'.
6484      Compute the type that would go with
6485      the actual amount of storage the member occupies.  */
6486
6487   if (TREE_CODE (lhs) == COMPONENT_REF
6488       && (TREE_CODE (lhstype) == INTEGER_TYPE
6489           || TREE_CODE (lhstype) == BOOLEAN_TYPE
6490           || TREE_CODE (lhstype) == REAL_TYPE
6491           || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6492     lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6493
6494   /* If storing in a field that is in actuality a short or narrower than one,
6495      we must store in the field in its actual type.  */
6496
6497   if (lhstype != TREE_TYPE (lhs))
6498     {
6499       lhs = copy_node (lhs);
6500       TREE_TYPE (lhs) = lhstype;
6501     }
6502
6503   /* Issue -Wc++-compat warnings about an assignment to an enum type
6504      when LHS does not have its original type.  This happens for,
6505      e.g., an enum bitfield in a struct.  */
6506   if (warn_cxx_compat
6507       && lhs_origtype != NULL_TREE
6508       && lhs_origtype != lhstype
6509       && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6510     {
6511       tree checktype = (rhs_origtype != NULL_TREE
6512                         ? rhs_origtype
6513                         : TREE_TYPE (rhs));
6514       if (checktype != error_mark_node
6515           && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6516               || (is_atomic_op && modifycode != NOP_EXPR)))
6517         warning_at (location, OPT_Wc___compat,
6518                     "enum conversion in assignment is invalid in C++");
6519     }
6520
6521   /* Remove qualifiers.  */
6522   lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6523   olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6524
6525   /* Convert new value to destination type.  Fold it first, then
6526      restore any excess precision information, for the sake of
6527      conversion warnings.  */
6528
6529   if (!(is_atomic_op && modifycode != NOP_EXPR))
6530     {
6531       tree rhs_semantic_type = NULL_TREE;
6532       if (!c_in_omp_for)
6533         {
6534           if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6535             {
6536               rhs_semantic_type = TREE_TYPE (newrhs);
6537               newrhs = TREE_OPERAND (newrhs, 0);
6538             }
6539           npc = null_pointer_constant_p (newrhs);
6540           newrhs = c_fully_fold (newrhs, false, NULL);
6541           if (rhs_semantic_type)
6542             newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6543         }
6544       else
6545         npc = null_pointer_constant_p (newrhs);
6546       newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6547                                        rhs_origtype, ic_assign, npc,
6548                                        NULL_TREE, NULL_TREE, 0);
6549       if (TREE_CODE (newrhs) == ERROR_MARK)
6550         return error_mark_node;
6551     }
6552
6553   /* Emit ObjC write barrier, if necessary.  */
6554   if (c_dialect_objc () && flag_objc_gc)
6555     {
6556       result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6557       if (result)
6558         {
6559           protected_set_expr_location (result, location);
6560           goto return_result;
6561         }
6562     }
6563
6564   /* Scan operands.  */
6565
6566   if (is_atomic_op)
6567     result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6568   else
6569     {
6570       result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6571       TREE_SIDE_EFFECTS (result) = 1;
6572       protected_set_expr_location (result, location);
6573     }
6574
6575   /* If we got the LHS in a different type for storing in,
6576      convert the result back to the nominal type of LHS
6577      so that the value we return always has the same type
6578      as the LHS argument.  */
6579
6580   if (olhstype == TREE_TYPE (result))
6581     goto return_result;
6582
6583   result = convert_for_assignment (location, rhs_loc, olhstype, result,
6584                                    rhs_origtype, ic_assign, false, NULL_TREE,
6585                                    NULL_TREE, 0);
6586   protected_set_expr_location (result, location);
6587
6588 return_result:
6589   if (rhseval)
6590     result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6591   return result;
6592 }
6593 \f
6594 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6595    This is used to implement -fplan9-extensions.  */
6596
6597 static bool
6598 find_anonymous_field_with_type (tree struct_type, tree type)
6599 {
6600   tree field;
6601   bool found;
6602
6603   gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6604   found = false;
6605   for (field = TYPE_FIELDS (struct_type);
6606        field != NULL_TREE;
6607        field = TREE_CHAIN (field))
6608     {
6609       tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6610                         ? c_build_qualified_type (TREE_TYPE (field),
6611                                                   TYPE_QUAL_ATOMIC)
6612                         : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6613       if (DECL_NAME (field) == NULL
6614           && comptypes (type, fieldtype))
6615         {
6616           if (found)
6617             return false;
6618           found = true;
6619         }
6620       else if (DECL_NAME (field) == NULL
6621                && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6622                && find_anonymous_field_with_type (TREE_TYPE (field), type))
6623         {
6624           if (found)
6625             return false;
6626           found = true;
6627         }
6628     }
6629   return found;
6630 }
6631
6632 /* RHS is an expression whose type is pointer to struct.  If there is
6633    an anonymous field in RHS with type TYPE, then return a pointer to
6634    that field in RHS.  This is used with -fplan9-extensions.  This
6635    returns NULL if no conversion could be found.  */
6636
6637 static tree
6638 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6639 {
6640   tree rhs_struct_type, lhs_main_type;
6641   tree field, found_field;
6642   bool found_sub_field;
6643   tree ret;
6644
6645   gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6646   rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6647   gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6648
6649   gcc_assert (POINTER_TYPE_P (type));
6650   lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6651                    ? c_build_qualified_type (TREE_TYPE (type),
6652                                              TYPE_QUAL_ATOMIC)
6653                    : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6654
6655   found_field = NULL_TREE;
6656   found_sub_field = false;
6657   for (field = TYPE_FIELDS (rhs_struct_type);
6658        field != NULL_TREE;
6659        field = TREE_CHAIN (field))
6660     {
6661       if (DECL_NAME (field) != NULL_TREE
6662           || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6663         continue;
6664       tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6665                         ? c_build_qualified_type (TREE_TYPE (field),
6666                                                   TYPE_QUAL_ATOMIC)
6667                         : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6668       if (comptypes (lhs_main_type, fieldtype))
6669         {
6670           if (found_field != NULL_TREE)
6671             return NULL_TREE;
6672           found_field = field;
6673         }
6674       else if (find_anonymous_field_with_type (TREE_TYPE (field),
6675                                                lhs_main_type))
6676         {
6677           if (found_field != NULL_TREE)
6678             return NULL_TREE;
6679           found_field = field;
6680           found_sub_field = true;
6681         }
6682     }
6683
6684   if (found_field == NULL_TREE)
6685     return NULL_TREE;
6686
6687   ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6688                          build_fold_indirect_ref (rhs), found_field,
6689                          NULL_TREE);
6690   ret = build_fold_addr_expr_loc (location, ret);
6691
6692   if (found_sub_field)
6693     {
6694       ret = convert_to_anonymous_field (location, type, ret);
6695       gcc_assert (ret != NULL_TREE);
6696     }
6697
6698   return ret;
6699 }
6700
6701 /* Issue an error message for a bad initializer component.
6702    GMSGID identifies the message.
6703    The component name is taken from the spelling stack.  */
6704
6705 static void ATTRIBUTE_GCC_DIAG (2,0)
6706 error_init (location_t loc, const char *gmsgid, ...)
6707 {
6708   char *ofwhat;
6709
6710   auto_diagnostic_group d;
6711
6712   /* The gmsgid may be a format string with %< and %>. */
6713   va_list ap;
6714   va_start (ap, gmsgid);
6715   bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6716   va_end (ap);
6717
6718   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6719   if (*ofwhat && warned)
6720     inform (loc, "(near initialization for %qs)", ofwhat);
6721 }
6722
6723 /* Issue a pedantic warning for a bad initializer component.  OPT is
6724    the option OPT_* (from options.h) controlling this warning or 0 if
6725    it is unconditionally given.  GMSGID identifies the message.  The
6726    component name is taken from the spelling stack.  */
6727
6728 static void ATTRIBUTE_GCC_DIAG (3,0)
6729 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6730 {
6731   /* Use the location where a macro was expanded rather than where
6732      it was defined to make sure macros defined in system headers
6733      but used incorrectly elsewhere are diagnosed.  */
6734   location_t exploc = expansion_point_location_if_in_system_header (loc);
6735   auto_diagnostic_group d;
6736   va_list ap;
6737   va_start (ap, gmsgid);
6738   bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6739   va_end (ap);
6740   char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6741   if (*ofwhat && warned)
6742     inform (exploc, "(near initialization for %qs)", ofwhat);
6743 }
6744
6745 /* Issue a warning for a bad initializer component.
6746
6747    OPT is the OPT_W* value corresponding to the warning option that
6748    controls this warning.  GMSGID identifies the message.  The
6749    component name is taken from the spelling stack.  */
6750
6751 static void
6752 warning_init (location_t loc, int opt, const char *gmsgid)
6753 {
6754   char *ofwhat;
6755   bool warned;
6756
6757   auto_diagnostic_group d;
6758
6759   /* Use the location where a macro was expanded rather than where
6760      it was defined to make sure macros defined in system headers
6761      but used incorrectly elsewhere are diagnosed.  */
6762   location_t exploc = expansion_point_location_if_in_system_header (loc);
6763
6764   /* The gmsgid may be a format string with %< and %>. */
6765   warned = warning_at (exploc, opt, gmsgid);
6766   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6767   if (*ofwhat && warned)
6768     inform (exploc, "(near initialization for %qs)", ofwhat);
6769 }
6770 \f
6771 /* If TYPE is an array type and EXPR is a parenthesized string
6772    constant, warn if pedantic that EXPR is being used to initialize an
6773    object of type TYPE.  */
6774
6775 void
6776 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6777 {
6778   if (pedantic
6779       && TREE_CODE (type) == ARRAY_TYPE
6780       && TREE_CODE (expr.value) == STRING_CST
6781       && expr.original_code != STRING_CST)
6782     pedwarn_init (loc, OPT_Wpedantic,
6783                   "array initialized from parenthesized string constant");
6784 }
6785
6786 /* Attempt to locate the parameter with the given index within FNDECL,
6787    returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found.  */
6788
6789 static location_t
6790 get_fndecl_argument_location (tree fndecl, int argnum)
6791 {
6792   int i;
6793   tree param;
6794
6795   /* Locate param by index within DECL_ARGUMENTS (fndecl).  */
6796   for (i = 0, param = DECL_ARGUMENTS (fndecl);
6797        i < argnum && param;
6798        i++, param = TREE_CHAIN (param))
6799     ;
6800
6801   /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6802      return DECL_SOURCE_LOCATION (FNDECL).  */
6803   if (param == NULL)
6804     return DECL_SOURCE_LOCATION (fndecl);
6805
6806   return DECL_SOURCE_LOCATION (param);
6807 }
6808
6809 /* Issue a note about a mismatching argument for parameter PARMNUM
6810    to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6811    Attempt to issue the note at the pertinent parameter of the decl;
6812    failing that issue it at the location of FUNDECL; failing that
6813    issue it at PLOC.  */
6814
6815 static void
6816 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6817                 tree expected_type, tree actual_type)
6818 {
6819   location_t loc;
6820   if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6821     loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6822   else
6823     loc = ploc;
6824
6825   inform (loc,
6826           "expected %qT but argument is of type %qT",
6827           expected_type, actual_type);
6828 }
6829
6830 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6831    function FUNDECL declared without prototype to parameter PARMNUM of
6832    PARMTYPE when ARGTYPE does not promote to PARMTYPE.  */
6833
6834 static void
6835 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6836                                  tree parmtype, tree argtype)
6837 {
6838   tree_code parmcode = TREE_CODE (parmtype);
6839   tree_code argcode = TREE_CODE (argtype);
6840   tree promoted = c_type_promotes_to (argtype);
6841
6842   /* Avoid warning for enum arguments that promote to an integer type
6843      of the same size/mode.  */
6844   if (parmcode == INTEGER_TYPE
6845       && argcode == ENUMERAL_TYPE
6846       && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6847     return;
6848
6849   if ((parmcode == argcode
6850        || (parmcode == INTEGER_TYPE
6851            && argcode == ENUMERAL_TYPE))
6852       && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6853     return;
6854
6855   /* This diagnoses even signed/unsigned mismatches.  Those might be
6856      safe in many cases but GCC may emit suboptimal code for them so
6857      warning on those cases drives efficiency improvements.  */
6858   if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6859                   TYPE_MAIN_VARIANT (promoted) == argtype
6860                   ? G_("%qD argument %d type is %qT where %qT is expected "
6861                        "in a call to built-in function declared without "
6862                        "prototype")
6863                   : G_("%qD argument %d promotes to %qT where %qT is expected "
6864                        "in a call to built-in function declared without "
6865                        "prototype"),
6866                   fundecl, parmnum, promoted, parmtype))
6867     inform (DECL_SOURCE_LOCATION (fundecl),
6868             "built-in %qD declared here",
6869             fundecl);
6870 }
6871
6872 /* Convert value RHS to type TYPE as preparation for an assignment to
6873    an lvalue of type TYPE.  If ORIGTYPE is not NULL_TREE, it is the
6874    original type of RHS; this differs from TREE_TYPE (RHS) for enum
6875    types.  NULL_POINTER_CONSTANT says whether RHS was a null pointer
6876    constant before any folding.
6877    The real work of conversion is done by `convert'.
6878    The purpose of this function is to generate error messages
6879    for assignments that are not allowed in C.
6880    ERRTYPE says whether it is argument passing, assignment,
6881    initialization or return.
6882
6883    In the following example, '~' denotes where EXPR_LOC and '^' where
6884    LOCATION point to:
6885
6886      f (var);      [ic_argpass]
6887      ^  ~~~
6888      x = var;      [ic_assign]
6889        ^ ~~~;
6890      int x = var;  [ic_init]
6891              ^^^
6892      return x;     [ic_return]
6893             ^
6894
6895    FUNCTION is a tree for the function being called.
6896    PARMNUM is the number of the argument, for printing in error messages.
6897    WARNOPT may be set to a warning option to issue the corresponding warning
6898    rather than an error for invalid conversions.  Used for calls to built-in
6899    functions declared without a prototype.  */
6900
6901 static tree
6902 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6903                         tree rhs, tree origtype, enum impl_conv errtype,
6904                         bool null_pointer_constant, tree fundecl,
6905                         tree function, int parmnum, int warnopt /* = 0 */)
6906 {
6907   enum tree_code codel = TREE_CODE (type);
6908   tree orig_rhs = rhs;
6909   tree rhstype;
6910   enum tree_code coder;
6911   tree rname = NULL_TREE;
6912   bool objc_ok = false;
6913
6914   /* Use the expansion point location to handle cases such as user's
6915      function returning a wrong-type macro defined in a system header.  */
6916   location = expansion_point_location_if_in_system_header (location);
6917
6918   if (errtype == ic_argpass)
6919     {
6920       tree selector;
6921       /* Change pointer to function to the function itself for
6922          diagnostics.  */
6923       if (TREE_CODE (function) == ADDR_EXPR
6924           && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6925         function = TREE_OPERAND (function, 0);
6926
6927       /* Handle an ObjC selector specially for diagnostics.  */
6928       selector = objc_message_selector ();
6929       rname = function;
6930       if (selector && parmnum > 2)
6931         {
6932           rname = selector;
6933           parmnum -= 2;
6934         }
6935     }
6936
6937   /* This macro is used to emit diagnostics to ensure that all format
6938      strings are complete sentences, visible to gettext and checked at
6939      compile time.  */
6940 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE)      \
6941   do {                                                                   \
6942     switch (errtype)                                                     \
6943       {                                                                  \
6944       case ic_argpass:                                                   \
6945         {                                                               \
6946           auto_diagnostic_group d;                                              \
6947           if (pedwarn (PLOC, OPT, AR, parmnum, rname))          \
6948             inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6949         }                                                               \
6950         break;                                                           \
6951       case ic_assign:                                                    \
6952         pedwarn (LOCATION, OPT, AS);                                     \
6953         break;                                                           \
6954       case ic_init:                                                      \
6955       case ic_init_const:                                                \
6956         pedwarn_init (LOCATION, OPT, IN);                                \
6957         break;                                                           \
6958       case ic_return:                                                    \
6959         pedwarn (LOCATION, OPT, RE);                                     \
6960         break;                                                           \
6961       default:                                                           \
6962         gcc_unreachable ();                                              \
6963       }                                                                  \
6964   } while (0)
6965
6966   /* This macro is used to emit diagnostics to ensure that all format
6967      strings are complete sentences, visible to gettext and checked at
6968      compile time.  It can be called with 'pedwarn' or 'warning_at'.  */
6969 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6970   do {                                                                   \
6971     switch (errtype)                                                     \
6972       {                                                                  \
6973       case ic_argpass:                                                   \
6974         {                                                                \
6975           auto_diagnostic_group d;                                       \
6976           if (PEDWARN) {                                                 \
6977             if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS))          \
6978               inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype);  \
6979           } else {                                                       \
6980             if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS))       \
6981               inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype);  \
6982           }                                                              \
6983         }                                                                \
6984         break;                                                           \
6985       case ic_assign:                                                    \
6986         if (PEDWARN)                                                     \
6987           pedwarn (LOCATION, OPT, AS, QUALS);                            \
6988         else                                                             \
6989           warning_at (LOCATION, OPT, AS, QUALS);                         \
6990         break;                                                           \
6991       case ic_init:                                                      \
6992       case ic_init_const:                                                \
6993         if (PEDWARN)                                                     \
6994           pedwarn (LOCATION, OPT, IN, QUALS);                            \
6995         else                                                             \
6996           warning_at (LOCATION, OPT, IN, QUALS);                         \
6997         break;                                                           \
6998       case ic_return:                                                    \
6999         if (PEDWARN)                                                     \
7000           pedwarn (LOCATION, OPT, RE, QUALS);                            \
7001         else                                                             \
7002           warning_at (LOCATION, OPT, RE, QUALS);                         \
7003         break;                                                           \
7004       default:                                                           \
7005         gcc_unreachable ();                                              \
7006       }                                                                  \
7007   } while (0)
7008
7009   /* This macro is used to emit diagnostics to ensure that all format
7010      strings are complete sentences, visible to gettext and checked at
7011      compile time.  It is the same as PEDWARN_FOR_ASSIGNMENT but with an
7012      extra parameter to enumerate qualifiers.  */
7013 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
7014    WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
7015
7016
7017   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
7018     rhs = TREE_OPERAND (rhs, 0);
7019
7020   rhstype = TREE_TYPE (rhs);
7021   coder = TREE_CODE (rhstype);
7022
7023   if (coder == ERROR_MARK)
7024     return error_mark_node;
7025
7026   if (c_dialect_objc ())
7027     {
7028       int parmno;
7029
7030       switch (errtype)
7031         {
7032         case ic_return:
7033           parmno = 0;
7034           break;
7035
7036         case ic_assign:
7037           parmno = -1;
7038           break;
7039
7040         case ic_init:
7041         case ic_init_const:
7042           parmno = -2;
7043           break;
7044
7045         default:
7046           parmno = parmnum;
7047           break;
7048         }
7049
7050       objc_ok = objc_compare_types (type, rhstype, parmno, rname);
7051     }
7052
7053   if (warn_cxx_compat)
7054     {
7055       tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7056       if (checktype != error_mark_node
7057           && TREE_CODE (type) == ENUMERAL_TYPE
7058           && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
7059         switch (errtype)
7060           {
7061           case ic_argpass:
7062             if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
7063                          "passing argument %d of %qE is invalid in C++",
7064                          parmnum, rname))
7065               inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
7066                       ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
7067                       "expected %qT but argument is of type %qT",
7068                       type, rhstype);
7069             break;
7070           case ic_assign:
7071             pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7072                      "%qT in assignment is invalid in C++", rhstype, type);
7073             break;
7074           case ic_init:
7075           case ic_init_const:
7076             pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
7077                           "%qT to %qT in initialization is invalid in C++",
7078                           rhstype, type);
7079             break;
7080           case ic_return:
7081             pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
7082                      "%qT in return is invalid in C++", rhstype, type);
7083             break;
7084           default:
7085             gcc_unreachable ();
7086           }
7087     }
7088
7089   if (warn_enum_conversion)
7090     {
7091       tree checktype = origtype != NULL_TREE ? origtype : rhstype;
7092       if (checktype != error_mark_node
7093           && TREE_CODE (checktype) == ENUMERAL_TYPE
7094           && TREE_CODE (type) == ENUMERAL_TYPE
7095           && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
7096        {
7097           gcc_rich_location loc (location);
7098           warning_at (&loc, OPT_Wenum_conversion,
7099                       "implicit conversion from %qT to %qT",
7100                       checktype, type);
7101        }
7102     }
7103
7104   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
7105     {
7106       warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7107       return rhs;
7108     }
7109
7110   if (coder == VOID_TYPE)
7111     {
7112       /* Except for passing an argument to an unprototyped function,
7113          this is a constraint violation.  When passing an argument to
7114          an unprototyped function, it is compile-time undefined;
7115          making it a constraint in that case was rejected in
7116          DR#252.  */
7117       const char msg[] = "void value not ignored as it ought to be";
7118       if (warnopt)
7119         warning_at (location, warnopt, msg);
7120       else
7121         error_at (location, msg);
7122       return error_mark_node;
7123     }
7124   rhs = require_complete_type (location, rhs);
7125   if (rhs == error_mark_node)
7126     return error_mark_node;
7127
7128   if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
7129     return error_mark_node;
7130
7131   /* A non-reference type can convert to a reference.  This handles
7132      va_start, va_copy and possibly port built-ins.  */
7133   if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
7134     {
7135       if (!lvalue_p (rhs))
7136         {
7137           const char msg[] = "cannot pass rvalue to reference parameter";
7138           if (warnopt)
7139             warning_at (location, warnopt, msg);
7140           else
7141             error_at (location, msg);
7142           return error_mark_node;
7143         }
7144       if (!c_mark_addressable (rhs))
7145         return error_mark_node;
7146       rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
7147       SET_EXPR_LOCATION (rhs, location);
7148
7149       rhs = convert_for_assignment (location, expr_loc,
7150                                     build_pointer_type (TREE_TYPE (type)),
7151                                     rhs, origtype, errtype,
7152                                     null_pointer_constant, fundecl, function,
7153                                     parmnum, warnopt);
7154       if (rhs == error_mark_node)
7155         return error_mark_node;
7156
7157       rhs = build1 (NOP_EXPR, type, rhs);
7158       SET_EXPR_LOCATION (rhs, location);
7159       return rhs;
7160     }
7161   /* Some types can interconvert without explicit casts.  */
7162   else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7163            && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7164     return convert (type, rhs);
7165   /* Arithmetic types all interconvert, and enum is treated like int.  */
7166   else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7167             || codel == FIXED_POINT_TYPE
7168             || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7169             || codel == BOOLEAN_TYPE)
7170            && (coder == INTEGER_TYPE || coder == REAL_TYPE
7171                || coder == FIXED_POINT_TYPE
7172                || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7173                || coder == BOOLEAN_TYPE))
7174     {
7175       if (warnopt && errtype == ic_argpass)
7176         maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7177                                          rhstype);
7178
7179       bool save = in_late_binary_op;
7180       if (C_BOOLEAN_TYPE_P (type) || codel == COMPLEX_TYPE
7181           || (coder == REAL_TYPE
7182               && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7183               && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7184         in_late_binary_op = true;
7185       tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7186                                     ? expr_loc : location, type, orig_rhs,
7187                                     errtype == ic_init_const);
7188       in_late_binary_op = save;
7189       return ret;
7190     }
7191
7192   /* Aggregates in different TUs might need conversion.  */
7193   if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7194       && codel == coder
7195       && comptypes (type, rhstype))
7196     return convert_and_check (expr_loc != UNKNOWN_LOCATION
7197                               ? expr_loc : location, type, rhs);
7198
7199   /* Conversion to a transparent union or record from its member types.
7200      This applies only to function arguments.  */
7201   if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7202       && TYPE_TRANSPARENT_AGGR (type))
7203       && errtype == ic_argpass)
7204     {
7205       tree memb, marginal_memb = NULL_TREE;
7206
7207       for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7208         {
7209           tree memb_type = TREE_TYPE (memb);
7210
7211           if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7212                          TYPE_MAIN_VARIANT (rhstype)))
7213             break;
7214
7215           if (TREE_CODE (memb_type) != POINTER_TYPE)
7216             continue;
7217
7218           if (coder == POINTER_TYPE)
7219             {
7220               tree ttl = TREE_TYPE (memb_type);
7221               tree ttr = TREE_TYPE (rhstype);
7222
7223               /* Any non-function converts to a [const][volatile] void *
7224                  and vice versa; otherwise, targets must be the same.
7225                  Meanwhile, the lhs target must have all the qualifiers of
7226                  the rhs.  */
7227               if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7228                   || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7229                   || comp_target_types (location, memb_type, rhstype))
7230                 {
7231                   int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7232                   int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7233                   /* If this type won't generate any warnings, use it.  */
7234                   if (lquals == rquals
7235                       || ((TREE_CODE (ttr) == FUNCTION_TYPE
7236                            && TREE_CODE (ttl) == FUNCTION_TYPE)
7237                           ? ((lquals | rquals) == rquals)
7238                           : ((lquals | rquals) == lquals)))
7239                     break;
7240
7241                   /* Keep looking for a better type, but remember this one.  */
7242                   if (!marginal_memb)
7243                     marginal_memb = memb;
7244                 }
7245             }
7246
7247           /* Can convert integer zero to any pointer type.  */
7248           if (null_pointer_constant)
7249             {
7250               rhs = null_pointer_node;
7251               break;
7252             }
7253         }
7254
7255       if (memb || marginal_memb)
7256         {
7257           if (!memb)
7258             {
7259               /* We have only a marginally acceptable member type;
7260                  it needs a warning.  */
7261               tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7262               tree ttr = TREE_TYPE (rhstype);
7263
7264               /* Const and volatile mean something different for function
7265                  types, so the usual warnings are not appropriate.  */
7266               if (TREE_CODE (ttr) == FUNCTION_TYPE
7267                   && TREE_CODE (ttl) == FUNCTION_TYPE)
7268                 {
7269                   /* Because const and volatile on functions are
7270                      restrictions that say the function will not do
7271                      certain things, it is okay to use a const or volatile
7272                      function where an ordinary one is wanted, but not
7273                      vice-versa.  */
7274                   if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7275                       & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7276                     PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7277                                             OPT_Wdiscarded_qualifiers,
7278                                             G_("passing argument %d of %qE "
7279                                                "makes %q#v qualified function "
7280                                                "pointer from unqualified"),
7281                                             G_("assignment makes %q#v qualified "
7282                                                "function pointer from "
7283                                                "unqualified"),
7284                                             G_("initialization makes %q#v qualified "
7285                                                "function pointer from "
7286                                                "unqualified"),
7287                                             G_("return makes %q#v qualified function "
7288                                                "pointer from unqualified"),
7289                                             TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7290                 }
7291               else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7292                        & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7293                 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7294                                         OPT_Wdiscarded_qualifiers,
7295                                         G_("passing argument %d of %qE discards "
7296                                            "%qv qualifier from pointer target type"),
7297                                         G_("assignment discards %qv qualifier "
7298                                            "from pointer target type"),
7299                                         G_("initialization discards %qv qualifier "
7300                                            "from pointer target type"),
7301                                         G_("return discards %qv qualifier from "
7302                                            "pointer target type"),
7303                                         TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7304
7305               memb = marginal_memb;
7306             }
7307
7308           if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7309             pedwarn (location, OPT_Wpedantic,
7310                      "ISO C prohibits argument conversion to union type");
7311
7312           rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7313           return build_constructor_single (type, memb, rhs);
7314         }
7315     }
7316
7317   /* Conversions among pointers */
7318   else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7319            && (coder == codel))
7320     {
7321       /* If RHS refers to a built-in declared without a prototype
7322          BLTIN is the declaration of the built-in with a prototype
7323          and RHSTYPE is set to the actual type of the built-in.  */
7324       tree bltin;
7325       rhstype = type_or_builtin_type (rhs, &bltin);
7326
7327       tree ttl = TREE_TYPE (type);
7328       tree ttr = TREE_TYPE (rhstype);
7329       tree mvl = ttl;
7330       tree mvr = ttr;
7331       bool is_opaque_pointer;
7332       int target_cmp = 0;   /* Cache comp_target_types () result.  */
7333       addr_space_t asl;
7334       addr_space_t asr;
7335
7336       if (TREE_CODE (mvl) != ARRAY_TYPE)
7337         mvl = (TYPE_ATOMIC (mvl)
7338                ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7339                                          TYPE_QUAL_ATOMIC)
7340                : TYPE_MAIN_VARIANT (mvl));
7341       if (TREE_CODE (mvr) != ARRAY_TYPE)
7342         mvr = (TYPE_ATOMIC (mvr)
7343                ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7344                                          TYPE_QUAL_ATOMIC)
7345                : TYPE_MAIN_VARIANT (mvr));
7346       /* Opaque pointers are treated like void pointers.  */
7347       is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7348
7349       /* The Plan 9 compiler permits a pointer to a struct to be
7350          automatically converted into a pointer to an anonymous field
7351          within the struct.  */
7352       if (flag_plan9_extensions
7353           && RECORD_OR_UNION_TYPE_P (mvl)
7354           && RECORD_OR_UNION_TYPE_P (mvr)
7355           && mvl != mvr)
7356         {
7357           tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7358           if (new_rhs != NULL_TREE)
7359             {
7360               rhs = new_rhs;
7361               rhstype = TREE_TYPE (rhs);
7362               coder = TREE_CODE (rhstype);
7363               ttr = TREE_TYPE (rhstype);
7364               mvr = TYPE_MAIN_VARIANT (ttr);
7365             }
7366         }
7367
7368       /* C++ does not allow the implicit conversion void* -> T*.  However,
7369          for the purpose of reducing the number of false positives, we
7370          tolerate the special case of
7371
7372                 int *p = NULL;
7373
7374          where NULL is typically defined in C to be '(void *) 0'.  */
7375       if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7376         warning_at (errtype == ic_argpass ? expr_loc : location,
7377                     OPT_Wc___compat,
7378                     "request for implicit conversion "
7379                     "from %qT to %qT not permitted in C++", rhstype, type);
7380
7381       /* See if the pointers point to incompatible address spaces.  */
7382       asl = TYPE_ADDR_SPACE (ttl);
7383       asr = TYPE_ADDR_SPACE (ttr);
7384       if (!null_pointer_constant_p (rhs)
7385           && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7386         {
7387           auto_diagnostic_group d;
7388           bool diagnosed = true;
7389           switch (errtype)
7390             {
7391             case ic_argpass:
7392               {
7393                 const char msg[] = G_("passing argument %d of %qE from "
7394                                       "pointer to non-enclosed address space");
7395                 if (warnopt)
7396                   diagnosed
7397                     = warning_at (expr_loc, warnopt, msg, parmnum, rname);
7398                 else
7399                   error_at (expr_loc, msg, parmnum, rname);
7400               break;
7401               }
7402             case ic_assign:
7403               {
7404                 const char msg[] = G_("assignment from pointer to "
7405                                       "non-enclosed address space");
7406                 if (warnopt)
7407                   diagnosed = warning_at (location, warnopt, msg);
7408                 else
7409                   error_at (location, msg);
7410                 break;
7411               }
7412             case ic_init:
7413             case ic_init_const:
7414               {
7415                 const char msg[] = G_("initialization from pointer to "
7416                                       "non-enclosed address space");
7417                 if (warnopt)
7418                   diagnosed = warning_at (location, warnopt, msg);
7419                 else
7420                   error_at (location, msg);
7421                 break;
7422               }
7423             case ic_return:
7424               {
7425                 const char msg[] = G_("return from pointer to "
7426                                       "non-enclosed address space");
7427                 if (warnopt)
7428                   diagnosed = warning_at (location, warnopt, msg);
7429                 else
7430                   error_at (location, msg);
7431                 break;
7432               }
7433             default:
7434               gcc_unreachable ();
7435             }
7436           if (diagnosed)
7437             {
7438               if (errtype == ic_argpass)
7439                 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7440               else
7441                 inform (location, "expected %qT but pointer is of type %qT",
7442                         type, rhstype);
7443             }
7444           return error_mark_node;
7445         }
7446
7447       /* Check if the right-hand side has a format attribute but the
7448          left-hand side doesn't.  */
7449       if (warn_suggest_attribute_format
7450           && check_missing_format_attribute (type, rhstype))
7451         {
7452           switch (errtype)
7453           {
7454           case ic_argpass:
7455             warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7456                         "argument %d of %qE might be "
7457                         "a candidate for a format attribute",
7458                         parmnum, rname);
7459             break;
7460           case ic_assign:
7461             warning_at (location, OPT_Wsuggest_attribute_format,
7462                         "assignment left-hand side might be "
7463                         "a candidate for a format attribute");
7464             break;
7465           case ic_init:
7466           case ic_init_const:
7467             warning_at (location, OPT_Wsuggest_attribute_format,
7468                         "initialization left-hand side might be "
7469                         "a candidate for a format attribute");
7470             break;
7471           case ic_return:
7472             warning_at (location, OPT_Wsuggest_attribute_format,
7473                         "return type might be "
7474                         "a candidate for a format attribute");
7475             break;
7476           default:
7477             gcc_unreachable ();
7478           }
7479         }
7480
7481       /* See if the pointers point to incompatible scalar storage orders.  */
7482       if (warn_scalar_storage_order
7483           && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7484              != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7485         {
7486           tree t;
7487
7488           switch (errtype)
7489           {
7490           case ic_argpass:
7491             /* Do not warn for built-in functions, for example memcpy, since we
7492                control how they behave and they can be useful in this area.  */
7493             if (TREE_CODE (rname) != FUNCTION_DECL
7494                 || !fndecl_built_in_p (rname))
7495               warning_at (location, OPT_Wscalar_storage_order,
7496                           "passing argument %d of %qE from incompatible "
7497                           "scalar storage order", parmnum, rname);
7498             break;
7499           case ic_assign:
7500             /* Do not warn if the RHS is a call to a function that returns a
7501                pointer that is not an alias.  */
7502             if (TREE_CODE (rhs) != CALL_EXPR
7503                 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7504                 || !DECL_IS_MALLOC (t))
7505               warning_at (location, OPT_Wscalar_storage_order,
7506                           "assignment to %qT from pointer type %qT with "
7507                           "incompatible scalar storage order", type, rhstype);
7508             break;
7509           case ic_init:
7510           case ic_init_const:
7511             /* Likewise.  */
7512             if (TREE_CODE (rhs) != CALL_EXPR
7513                 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7514                 || !DECL_IS_MALLOC (t))
7515               warning_at (location, OPT_Wscalar_storage_order,
7516                           "initialization of %qT from pointer type %qT with "
7517                           "incompatible scalar storage order", type, rhstype);
7518             break;
7519           case ic_return:
7520             warning_at (location, OPT_Wscalar_storage_order,
7521                         "returning %qT from pointer type with incompatible "
7522                         "scalar storage order %qT", rhstype, type);
7523             break;
7524           default:
7525             gcc_unreachable ();
7526           }
7527         }
7528
7529       /* Any non-function converts to a [const][volatile] void *
7530          and vice versa; otherwise, targets must be the same.
7531          Meanwhile, the lhs target must have all the qualifiers of the rhs.  */
7532       if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7533           || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7534           || (target_cmp = comp_target_types (location, type, rhstype))
7535           || is_opaque_pointer
7536           || ((c_common_unsigned_type (mvl)
7537                == c_common_unsigned_type (mvr))
7538               && (c_common_signed_type (mvl)
7539                   == c_common_signed_type (mvr))
7540               && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7541         {
7542           /* Warn about loss of qualifers from pointers to arrays with
7543              qualifiers on the element type. */
7544           if (TREE_CODE (ttr) == ARRAY_TYPE)
7545             {
7546               ttr = strip_array_types (ttr);
7547               ttl = strip_array_types (ttl);
7548
7549               if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7550                   & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7551                 WARNING_FOR_QUALIFIERS (flag_isoc2x,
7552                                         location, expr_loc,
7553                                         OPT_Wdiscarded_array_qualifiers,
7554                                         G_("passing argument %d of %qE discards "
7555                                            "%qv qualifier from pointer target type"),
7556                                         G_("assignment discards %qv qualifier "
7557                                            "from pointer target type"),
7558                                         G_("initialization discards %qv qualifier "
7559                                            "from pointer target type"),
7560                                         G_("return discards %qv qualifier from "
7561                                            "pointer target type"),
7562                                         TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7563             }
7564           else if (pedantic
7565               && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7566                   ||
7567                   (VOID_TYPE_P (ttr)
7568                    && !null_pointer_constant
7569                    && TREE_CODE (ttl) == FUNCTION_TYPE)))
7570             PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7571                                     G_("ISO C forbids passing argument %d of "
7572                                        "%qE between function pointer "
7573                                        "and %<void *%>"),
7574                                     G_("ISO C forbids assignment between "
7575                                        "function pointer and %<void *%>"),
7576                                     G_("ISO C forbids initialization between "
7577                                        "function pointer and %<void *%>"),
7578                                     G_("ISO C forbids return between function "
7579                                        "pointer and %<void *%>"));
7580           /* Const and volatile mean something different for function types,
7581              so the usual warnings are not appropriate.  */
7582           else if (TREE_CODE (ttr) != FUNCTION_TYPE
7583                    && TREE_CODE (ttl) != FUNCTION_TYPE)
7584             {
7585                /* Assignments between atomic and non-atomic objects are OK.  */
7586                bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7587                                      & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7588                bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7589                                  & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7590
7591               /* Don't warn about loss of qualifier for conversions from
7592                  qualified void* to pointers to arrays with corresponding
7593                  qualifier on the element type (except for pedantic before C23). */
7594               if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc2x))
7595                 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7596                                         OPT_Wdiscarded_qualifiers,
7597                                         G_("passing argument %d of %qE discards "
7598                                            "%qv qualifier from pointer target type"),
7599                                         G_("assignment discards %qv qualifier "
7600                                            "from pointer target type"),
7601                                         G_("initialization discards %qv qualifier "
7602                                            "from pointer target type"),
7603                                         G_("return discards %qv qualifier from "
7604                                            "pointer target type"),
7605                                         TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7606               else if (warn_quals_ped)
7607                 pedwarn_c11 (location, OPT_Wc11_c2x_compat,
7608                              "array with qualifier on the element is not qualified before C2X");
7609
7610               /* If this is not a case of ignoring a mismatch in signedness,
7611                  no warning.  */
7612               else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7613                        || target_cmp)
7614                 ;
7615               /* If there is a mismatch, do warn.  */
7616               else if (warn_pointer_sign)
7617                 switch (errtype)
7618                   {
7619                   case ic_argpass:
7620                     {
7621                       auto_diagnostic_group d;
7622                       range_label_for_type_mismatch rhs_label (rhstype, type);
7623                       gcc_rich_location richloc (expr_loc, &rhs_label);
7624                       if (pedwarn (&richloc, OPT_Wpointer_sign,
7625                                    "pointer targets in passing argument %d of "
7626                                    "%qE differ in signedness", parmnum, rname))
7627                         inform_for_arg (fundecl, expr_loc, parmnum, type,
7628                                         rhstype);
7629                     }
7630                     break;
7631                   case ic_assign:
7632                     pedwarn (location, OPT_Wpointer_sign,
7633                              "pointer targets in assignment from %qT to %qT "
7634                              "differ in signedness", rhstype, type);
7635                     break;
7636                   case ic_init:
7637                   case ic_init_const:
7638                     pedwarn_init (location, OPT_Wpointer_sign,
7639                                   "pointer targets in initialization of %qT "
7640                                   "from %qT differ in signedness", type,
7641                                   rhstype);
7642                     break;
7643                   case ic_return:
7644                     pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7645                              "returning %qT from a function with return type "
7646                              "%qT differ in signedness", rhstype, type);
7647                     break;
7648                   default:
7649                     gcc_unreachable ();
7650                   }
7651             }
7652           else if (TREE_CODE (ttl) == FUNCTION_TYPE
7653                    && TREE_CODE (ttr) == FUNCTION_TYPE)
7654             {
7655               /* Because const and volatile on functions are restrictions
7656                  that say the function will not do certain things,
7657                  it is okay to use a const or volatile function
7658                  where an ordinary one is wanted, but not vice-versa.  */
7659               if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7660                   & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7661                 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7662                                         OPT_Wdiscarded_qualifiers,
7663                                         G_("passing argument %d of %qE makes "
7664                                            "%q#v qualified function pointer "
7665                                            "from unqualified"),
7666                                         G_("assignment makes %q#v qualified function "
7667                                            "pointer from unqualified"),
7668                                         G_("initialization makes %q#v qualified "
7669                                            "function pointer from unqualified"),
7670                                         G_("return makes %q#v qualified function "
7671                                            "pointer from unqualified"),
7672                                         TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7673             }
7674         }
7675       /* Avoid warning about the volatile ObjC EH puts on decls.  */
7676       else if (!objc_ok)
7677         {
7678           switch (errtype)
7679             {
7680             case ic_argpass:
7681               {
7682                 auto_diagnostic_group d;
7683                 range_label_for_type_mismatch rhs_label (rhstype, type);
7684                 gcc_rich_location richloc (expr_loc, &rhs_label);
7685                 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7686                              "passing argument %d of %qE from incompatible "
7687                              "pointer type", parmnum, rname))
7688                   inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7689               }
7690               break;
7691             case ic_assign:
7692               if (bltin)
7693                 pedwarn (location, OPT_Wincompatible_pointer_types,
7694                          "assignment to %qT from pointer to "
7695                          "%qD with incompatible type %qT",
7696                          type, bltin, rhstype);
7697               else
7698                 pedwarn (location, OPT_Wincompatible_pointer_types,
7699                          "assignment to %qT from incompatible pointer type %qT",
7700                          type, rhstype);
7701               break;
7702             case ic_init:
7703             case ic_init_const:
7704               if (bltin)
7705                 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7706                               "initialization of %qT from pointer to "
7707                               "%qD with incompatible type %qT",
7708                               type, bltin, rhstype);
7709               else
7710                 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7711                               "initialization of %qT from incompatible "
7712                               "pointer type %qT",
7713                               type, rhstype);
7714               break;
7715             case ic_return:
7716               if (bltin)
7717                 pedwarn (location, OPT_Wincompatible_pointer_types,
7718                          "returning pointer to %qD of type %qT from "
7719                          "a function with incompatible type %qT",
7720                          bltin, rhstype, type);
7721               else
7722                 pedwarn (location, OPT_Wincompatible_pointer_types,
7723                          "returning %qT from a function with incompatible "
7724                          "return type %qT", rhstype, type);
7725               break;
7726             default:
7727               gcc_unreachable ();
7728             }
7729         }
7730
7731       /* If RHS isn't an address, check pointer or array of packed
7732          struct or union.  */
7733       warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7734
7735       return convert (type, rhs);
7736     }
7737   else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7738     {
7739       /* ??? This should not be an error when inlining calls to
7740          unprototyped functions.  */
7741       const char msg[] = "invalid use of non-lvalue array";
7742       if (warnopt)
7743         warning_at (location, warnopt, msg);
7744       else
7745         error_at (location, msg);
7746       return error_mark_node;
7747     }
7748   else if (codel == POINTER_TYPE
7749            && (coder == INTEGER_TYPE || coder == NULLPTR_TYPE))
7750     {
7751       /* An explicit constant 0 or type nullptr_t can convert to a pointer,
7752          or one that results from arithmetic, even including a cast to
7753          integer type.  */
7754       if (!null_pointer_constant && coder != NULLPTR_TYPE)
7755         switch (errtype)
7756           {
7757           case ic_argpass:
7758             {
7759               auto_diagnostic_group d;
7760               range_label_for_type_mismatch rhs_label (rhstype, type);
7761               gcc_rich_location richloc (expr_loc, &rhs_label);
7762               if (pedwarn (&richloc, OPT_Wint_conversion,
7763                            "passing argument %d of %qE makes pointer from "
7764                            "integer without a cast", parmnum, rname))
7765                 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7766             }
7767             break;
7768           case ic_assign:
7769             pedwarn (location, OPT_Wint_conversion,
7770                      "assignment to %qT from %qT makes pointer from integer "
7771                      "without a cast", type, rhstype);
7772             break;
7773           case ic_init:
7774           case ic_init_const:
7775             pedwarn_init (location, OPT_Wint_conversion,
7776                           "initialization of %qT from %qT makes pointer from "
7777                           "integer without a cast", type, rhstype);
7778             break;
7779           case ic_return:
7780             pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7781                      "function with return type %qT makes pointer from "
7782                      "integer without a cast", rhstype, type);
7783             break;
7784           default:
7785             gcc_unreachable ();
7786           }
7787
7788       return convert (type, rhs);
7789     }
7790   else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7791     {
7792       switch (errtype)
7793         {
7794         case ic_argpass:
7795           {
7796             auto_diagnostic_group d;
7797             range_label_for_type_mismatch rhs_label (rhstype, type);
7798             gcc_rich_location richloc (expr_loc, &rhs_label);
7799             if (pedwarn (&richloc, OPT_Wint_conversion,
7800                          "passing argument %d of %qE makes integer from "
7801                          "pointer without a cast", parmnum, rname))
7802               inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7803           }
7804           break;
7805         case ic_assign:
7806           pedwarn (location, OPT_Wint_conversion,
7807                    "assignment to %qT from %qT makes integer from pointer "
7808                    "without a cast", type, rhstype);
7809           break;
7810         case ic_init:
7811         case ic_init_const:
7812           pedwarn_init (location, OPT_Wint_conversion,
7813                         "initialization of %qT from %qT makes integer from "
7814                         "pointer without a cast", type, rhstype);
7815           break;
7816         case ic_return:
7817           pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7818                    "function with return type %qT makes integer from "
7819                    "pointer without a cast", rhstype, type);
7820           break;
7821         default:
7822           gcc_unreachable ();
7823         }
7824
7825       return convert (type, rhs);
7826     }
7827   else if (C_BOOLEAN_TYPE_P (type)
7828            /* The type nullptr_t may be converted to bool.  The
7829               result is false.  */
7830            && (coder == POINTER_TYPE || coder == NULLPTR_TYPE))
7831     {
7832       tree ret;
7833       bool save = in_late_binary_op;
7834       in_late_binary_op = true;
7835       ret = convert (type, rhs);
7836       in_late_binary_op = save;
7837       return ret;
7838     }
7839
7840   switch (errtype)
7841     {
7842     case ic_argpass:
7843       {
7844         auto_diagnostic_group d;
7845         range_label_for_type_mismatch rhs_label (rhstype, type);
7846         gcc_rich_location richloc (expr_loc, &rhs_label);
7847         const char msg[] = G_("incompatible type for argument %d of %qE");
7848         if (warnopt)
7849           warning_at (expr_loc, warnopt, msg, parmnum, rname);
7850         else
7851           error_at (&richloc, msg, parmnum, rname);
7852         inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7853       }
7854       break;
7855     case ic_assign:
7856       {
7857         const char msg[]
7858           = G_("incompatible types when assigning to type %qT from type %qT");
7859         if (warnopt)
7860           warning_at (expr_loc, 0, msg, type, rhstype);
7861         else
7862           error_at (expr_loc, msg, type, rhstype);
7863         break;
7864       }
7865     case ic_init:
7866     case ic_init_const:
7867       {
7868         const char msg[]
7869           = G_("incompatible types when initializing type %qT using type %qT");
7870         if (warnopt)
7871           warning_at (location, 0, msg, type, rhstype);
7872         else
7873           error_at (location, msg, type, rhstype);
7874         break;
7875       }
7876     case ic_return:
7877       {
7878         const char msg[]
7879           = G_("incompatible types when returning type %qT but %qT was expected");
7880         if (warnopt)
7881           warning_at (location, 0, msg, rhstype, type);
7882         else
7883           error_at (location, msg, rhstype, type);
7884         break;
7885       }
7886     default:
7887       gcc_unreachable ();
7888     }
7889
7890   return error_mark_node;
7891 }
7892 \f
7893 /* If VALUE is a compound expr all of whose expressions are constant, then
7894    return its value.  Otherwise, return error_mark_node.
7895
7896    This is for handling COMPOUND_EXPRs as initializer elements
7897    which is allowed with a warning when -pedantic is specified.  */
7898
7899 static tree
7900 valid_compound_expr_initializer (tree value, tree endtype)
7901 {
7902   if (TREE_CODE (value) == COMPOUND_EXPR)
7903     {
7904       if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7905           == error_mark_node)
7906         return error_mark_node;
7907       return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7908                                               endtype);
7909     }
7910   else if (!initializer_constant_valid_p (value, endtype))
7911     return error_mark_node;
7912   else
7913     return value;
7914 }
7915 \f
7916 /* Perform appropriate conversions on the initial value of a variable,
7917    store it in the declaration DECL,
7918    and print any error messages that are appropriate.
7919    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7920    If the init is invalid, store an ERROR_MARK.
7921
7922    INIT_LOC is the location of the initial value.  */
7923
7924 void
7925 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7926 {
7927   tree value, type;
7928   bool npc = false;
7929   bool int_const_expr = false;
7930   bool arith_const_expr = false;
7931
7932   /* If variable's type was invalidly declared, just ignore it.  */
7933
7934   type = TREE_TYPE (decl);
7935   if (TREE_CODE (type) == ERROR_MARK)
7936     return;
7937
7938   /* Digest the specified initializer into an expression.  */
7939
7940   if (init)
7941     {
7942       npc = null_pointer_constant_p (init);
7943       int_const_expr = (TREE_CODE (init) == INTEGER_CST
7944                         && !TREE_OVERFLOW (init)
7945                         && INTEGRAL_TYPE_P (TREE_TYPE (init)));
7946       /* Not fully determined before folding.  */
7947       arith_const_expr = true;
7948     }
7949   bool constexpr_p = (TREE_CODE (decl) == VAR_DECL
7950                       && C_DECL_DECLARED_CONSTEXPR (decl));
7951   value = digest_init (init_loc, type, init, origtype, npc, int_const_expr,
7952                        arith_const_expr, true,
7953                        TREE_STATIC (decl) || constexpr_p, constexpr_p);
7954
7955   /* Store the expression if valid; else report error.  */
7956
7957   if (!in_system_header_at (input_location)
7958       && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7959     warning (OPT_Wtraditional, "traditional C rejects automatic "
7960              "aggregate initialization");
7961
7962   if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7963     DECL_INITIAL (decl) = value;
7964
7965   /* ANSI wants warnings about out-of-range constant initializers.  */
7966   STRIP_TYPE_NOPS (value);
7967   if (TREE_STATIC (decl))
7968     constant_expression_warning (value);
7969
7970   /* Check if we need to set array size from compound literal size.  */
7971   if (TREE_CODE (type) == ARRAY_TYPE
7972       && TYPE_DOMAIN (type) == NULL_TREE
7973       && value != error_mark_node)
7974     {
7975       tree inside_init = init;
7976
7977       STRIP_TYPE_NOPS (inside_init);
7978       inside_init = fold (inside_init);
7979
7980       if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7981         {
7982           tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7983
7984           if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7985             {
7986               /* For int foo[] = (int [3]){1}; we need to set array size
7987                  now since later on array initializer will be just the
7988                  brace enclosed list of the compound literal.  */
7989               tree etype = strip_array_types (TREE_TYPE (decl));
7990               type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7991               TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7992               layout_type (type);
7993               layout_decl (cldecl, 0);
7994               TREE_TYPE (decl)
7995                 = c_build_qualified_type (type, TYPE_QUALS (etype));
7996             }
7997         }
7998     }
7999 }
8000 \f
8001 /* Methods for storing and printing names for error messages.  */
8002
8003 /* Implement a spelling stack that allows components of a name to be pushed
8004    and popped.  Each element on the stack is this structure.  */
8005
8006 struct spelling
8007 {
8008   int kind;
8009   union
8010     {
8011       unsigned HOST_WIDE_INT i;
8012       const char *s;
8013     } u;
8014 };
8015
8016 #define SPELLING_STRING 1
8017 #define SPELLING_MEMBER 2
8018 #define SPELLING_BOUNDS 3
8019
8020 static struct spelling *spelling;       /* Next stack element (unused).  */
8021 static struct spelling *spelling_base;  /* Spelling stack base.  */
8022 static int spelling_size;               /* Size of the spelling stack.  */
8023
8024 /* Macros to save and restore the spelling stack around push_... functions.
8025    Alternative to SAVE_SPELLING_STACK.  */
8026
8027 #define SPELLING_DEPTH() (spelling - spelling_base)
8028 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
8029
8030 /* Push an element on the spelling stack with type KIND and assign VALUE
8031    to MEMBER.  */
8032
8033 #define PUSH_SPELLING(KIND, VALUE, MEMBER)                              \
8034 {                                                                       \
8035   int depth = SPELLING_DEPTH ();                                        \
8036                                                                         \
8037   if (depth >= spelling_size)                                           \
8038     {                                                                   \
8039       spelling_size += 10;                                              \
8040       spelling_base = XRESIZEVEC (struct spelling, spelling_base,       \
8041                                   spelling_size);                       \
8042       RESTORE_SPELLING_DEPTH (depth);                                   \
8043     }                                                                   \
8044                                                                         \
8045   spelling->kind = (KIND);                                              \
8046   spelling->MEMBER = (VALUE);                                           \
8047   spelling++;                                                           \
8048 }
8049
8050 /* Push STRING on the stack.  Printed literally.  */
8051
8052 static void
8053 push_string (const char *string)
8054 {
8055   PUSH_SPELLING (SPELLING_STRING, string, u.s);
8056 }
8057
8058 /* Push a member name on the stack.  Printed as '.' STRING.  */
8059
8060 static void
8061 push_member_name (tree decl)
8062 {
8063   const char *const string
8064     = (DECL_NAME (decl)
8065        ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
8066        : _("<anonymous>"));
8067   PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
8068 }
8069
8070 /* Push an array bounds on the stack.  Printed as [BOUNDS].  */
8071
8072 static void
8073 push_array_bounds (unsigned HOST_WIDE_INT bounds)
8074 {
8075   PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
8076 }
8077
8078 /* Compute the maximum size in bytes of the printed spelling.  */
8079
8080 static int
8081 spelling_length (void)
8082 {
8083   int size = 0;
8084   struct spelling *p;
8085
8086   for (p = spelling_base; p < spelling; p++)
8087     {
8088       if (p->kind == SPELLING_BOUNDS)
8089         size += 25;
8090       else
8091         size += strlen (p->u.s) + 1;
8092     }
8093
8094   return size;
8095 }
8096
8097 /* Print the spelling to BUFFER and return it.  */
8098
8099 static char *
8100 print_spelling (char *buffer)
8101 {
8102   char *d = buffer;
8103   struct spelling *p;
8104
8105   for (p = spelling_base; p < spelling; p++)
8106     if (p->kind == SPELLING_BOUNDS)
8107       {
8108         sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
8109         d += strlen (d);
8110       }
8111     else
8112       {
8113         const char *s;
8114         if (p->kind == SPELLING_MEMBER)
8115           *d++ = '.';
8116         for (s = p->u.s; (*d = *s++); d++)
8117           ;
8118       }
8119   *d++ = '\0';
8120   return buffer;
8121 }
8122
8123 /* Check whether INIT, a floating or integer constant, is
8124    representable in TYPE, a real floating type with the same radix.
8125    Return true if OK, false if not.  */
8126 static bool
8127 constexpr_init_fits_real_type (tree type, tree init)
8128 {
8129   gcc_assert (TREE_CODE (type) == REAL_TYPE);
8130   gcc_assert (TREE_CODE (init) == INTEGER_CST || TREE_CODE (init) == REAL_CST);
8131   if (TREE_CODE (init) == REAL_CST
8132       && TYPE_MODE (TREE_TYPE (init)) == TYPE_MODE (type))
8133     /* Same mode, no conversion required.  */
8134     return true;
8135   if (TREE_CODE (init) == INTEGER_CST)
8136     {
8137       tree converted = build_real_from_int_cst (type, init);
8138       bool fail = false;
8139       wide_int w = real_to_integer (&TREE_REAL_CST (converted), &fail,
8140                                     TYPE_PRECISION (TREE_TYPE (init)));
8141       return !fail && wi::eq_p (w, wi::to_wide (init));
8142     }
8143   /* exact_real_truncate is not quite right here, since it doesn't
8144      allow even an exact conversion to subnormal values.  */
8145   REAL_VALUE_TYPE t;
8146   real_convert (&t, TYPE_MODE (type), &TREE_REAL_CST (init));
8147   return real_identical (&t, &TREE_REAL_CST (init));
8148 }
8149
8150 /* Check whether INIT (location LOC) is valid as a 'constexpr'
8151    initializer for type TYPE, and give an error if not.  INIT has
8152    already been folded and verified to be constant.
8153    NULL_POINTER_CONSTANT, INT_CONST_EXPR and ARITH_CONST_EXPR say
8154    whether it is a null pointer constant, integer constant expression
8155    or arithmetic constant expression, respectively.  If TYPE is not a
8156    scalar type, this function does nothing.  */
8157
8158 static void
8159 check_constexpr_init (location_t loc, tree type, tree init,
8160                       bool null_pointer_constant, bool int_const_expr,
8161                       bool arith_const_expr)
8162 {
8163   if (POINTER_TYPE_P (type))
8164     {
8165       /* The initializer must be a null pointer constant.  */
8166       if (!null_pointer_constant)
8167         error_at (loc, "%<constexpr%> pointer initializer is not a "
8168                   "null pointer constant");
8169       return;
8170     }
8171   if (INTEGRAL_TYPE_P (type))
8172     {
8173       /* The initializer must be an integer constant expression,
8174          representable in the target type.  */
8175       if (!int_const_expr)
8176         error_at (loc, "%<constexpr%> integer initializer is not an "
8177                   "integer constant expression");
8178       if (!int_fits_type_p (init, type))
8179         error_at (loc, "%<constexpr%> initializer not representable in "
8180                   "type of object");
8181       return;
8182     }
8183   /* We don't apply any extra checks to extension types such as vector
8184      or fixed-point types.  */
8185   if (TREE_CODE (type) != REAL_TYPE && TREE_CODE (type) != COMPLEX_TYPE)
8186     return;
8187   if (!arith_const_expr)
8188     {
8189       error_at (loc, "%<constexpr%> initializer is not an arithmetic "
8190                 "constant expression");
8191       return;
8192     }
8193   /* We don't apply any extra checks to complex integers.  */
8194   if (TREE_CODE (type) == COMPLEX_TYPE
8195       && TREE_CODE (TREE_TYPE (type)) != REAL_TYPE)
8196     return;
8197   /* Both the normative text and the relevant footnote are unclear, as
8198      of the C2x CD, about what exactly counts as a change of value in
8199      floating-point cases.  Here, we consider all conversions between
8200      binary and decimal types (even of infinities and NaNs, where
8201      quantum exponents are not involved) as involving a change of
8202      value, and likewise for conversions between real and complex
8203      types (even when the complex constant has imaginary part positive
8204      zero), and conversions of signaling NaN to a different machine
8205      mode.  But we allow exact conversions of integers to binary or
8206      decimal floating types, and exact conversions between different
8207      binary types or different decimal types, where "exact" in the
8208      decimal case requires the quantum exponent to be preserved.  */
8209   if (TREE_CODE (TREE_TYPE (init)) == COMPLEX_TYPE
8210       && TREE_CODE (type) == REAL_TYPE)
8211     {
8212       error_at (loc, "%<constexpr%> initializer for a real type is of "
8213                 "complex type");
8214       return;
8215     }
8216   if (TREE_CODE (type) == COMPLEX_TYPE
8217       && TREE_CODE (TREE_TYPE (init)) != COMPLEX_TYPE)
8218     {
8219       error_at (loc, "%<constexpr%> initializer for a complex type is of "
8220                 "real type");
8221       return;
8222     }
8223   if (TREE_CODE (type) == REAL_TYPE
8224       && TREE_CODE (TREE_TYPE (init)) == REAL_TYPE)
8225     {
8226       if (DECIMAL_FLOAT_TYPE_P (type)
8227           && !DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init)))
8228         {
8229           error_at (loc, "%<constexpr%> initializer for a decimal "
8230                     "floating-point type is of binary type");
8231           return;
8232         }
8233       else if (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (init))
8234                && !DECIMAL_FLOAT_TYPE_P (type))
8235         {
8236           error_at (loc, "%<constexpr%> initializer for a binary "
8237                     "floating-point type is of decimal type");
8238           return;
8239         }
8240     }
8241   bool fits;
8242   if (TREE_CODE (type) == COMPLEX_TYPE)
8243     {
8244       gcc_assert (TREE_CODE (init) == COMPLEX_CST);
8245       fits = (constexpr_init_fits_real_type (TREE_TYPE (type),
8246                                              TREE_REALPART (init))
8247               && constexpr_init_fits_real_type (TREE_TYPE (type),
8248                                                 TREE_IMAGPART (init)));
8249     }
8250   else
8251     fits = constexpr_init_fits_real_type (type, init);
8252   if (!fits)
8253     error_at (loc, "%<constexpr%> initializer not representable in "
8254               "type of object");
8255 }
8256
8257 /* Digest the parser output INIT as an initializer for type TYPE.
8258    Return a C expression of type TYPE to represent the initial value.
8259
8260    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
8261
8262    NULL_POINTER_CONSTANT is true if INIT is a null pointer constant,
8263    INT_CONST_EXPR is true if INIT is an integer constant expression,
8264    and ARITH_CONST_EXPR is true if INIT is, or might be, an arithmetic
8265    constant expression, false if it has already been determined in the
8266    caller that it is not (but folding may have made the value passed here
8267    indistinguishable from an arithmetic constant expression).
8268
8269    If INIT is a string constant, STRICT_STRING is true if it is
8270    unparenthesized or we should not warn here for it being parenthesized.
8271    For other types of INIT, STRICT_STRING is not used.
8272
8273    INIT_LOC is the location of the INIT.
8274
8275    REQUIRE_CONSTANT requests an error if non-constant initializers or
8276    elements are seen.  REQUIRE_CONSTEXPR means the stricter requirements
8277    on initializers for 'constexpr' objects apply.  */
8278
8279 static tree
8280 digest_init (location_t init_loc, tree type, tree init, tree origtype,
8281              bool null_pointer_constant, bool int_const_expr,
8282              bool arith_const_expr, bool strict_string,
8283              bool require_constant, bool require_constexpr)
8284 {
8285   enum tree_code code = TREE_CODE (type);
8286   tree inside_init = init;
8287   tree semantic_type = NULL_TREE;
8288   bool maybe_const = true;
8289
8290   if (type == error_mark_node
8291       || !init
8292       || error_operand_p (init))
8293     return error_mark_node;
8294
8295   STRIP_TYPE_NOPS (inside_init);
8296
8297   if (!c_in_omp_for)
8298     {
8299       if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
8300         {
8301           semantic_type = TREE_TYPE (inside_init);
8302           inside_init = TREE_OPERAND (inside_init, 0);
8303         }
8304       inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
8305     }
8306   /* TODO: this may not detect all cases of expressions folding to
8307      constants that are not arithmetic constant expressions.  */
8308   if (!maybe_const)
8309     arith_const_expr = false;
8310   else if (!INTEGRAL_TYPE_P (TREE_TYPE (inside_init))
8311       && TREE_CODE (TREE_TYPE (inside_init)) != REAL_TYPE
8312       && TREE_CODE (TREE_TYPE (inside_init)) != COMPLEX_TYPE)
8313     arith_const_expr = false;
8314   else if (TREE_CODE (inside_init) != INTEGER_CST
8315       && TREE_CODE (inside_init) != REAL_CST
8316       && TREE_CODE (inside_init) != COMPLEX_CST)
8317     arith_const_expr = false;
8318   else if (TREE_OVERFLOW (inside_init))
8319     arith_const_expr = false;
8320
8321   /* Initialization of an array of chars from a string constant
8322      optionally enclosed in braces.  */
8323
8324   if (code == ARRAY_TYPE && inside_init
8325       && TREE_CODE (inside_init) == STRING_CST)
8326     {
8327       tree typ1
8328         = (TYPE_ATOMIC (TREE_TYPE (type))
8329            ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
8330                                      TYPE_QUAL_ATOMIC)
8331            : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
8332       /* Note that an array could be both an array of character type
8333          and an array of wchar_t if wchar_t is signed char or unsigned
8334          char.  */
8335       bool char_array = (typ1 == char_type_node
8336                          || typ1 == signed_char_type_node
8337                          || typ1 == unsigned_char_type_node);
8338       bool wchar_array = !!comptypes (typ1, wchar_type_node);
8339       bool char16_array = !!comptypes (typ1, char16_type_node);
8340       bool char32_array = !!comptypes (typ1, char32_type_node);
8341
8342       if (char_array || wchar_array || char16_array || char32_array)
8343         {
8344           struct c_expr expr;
8345           tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
8346           bool incompat_string_cst = false;
8347           expr.value = inside_init;
8348           expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
8349           expr.original_type = NULL;
8350           expr.m_decimal = 0;
8351           maybe_warn_string_init (init_loc, type, expr);
8352
8353           if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8354             pedwarn_init (init_loc, OPT_Wpedantic,
8355                           "initialization of a flexible array member");
8356
8357           if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8358                          TYPE_MAIN_VARIANT (type)))
8359             return inside_init;
8360
8361           if (char_array)
8362             {
8363               if (typ2 != char_type_node && typ2 != char8_type_node)
8364                 incompat_string_cst = true;
8365             }
8366           else if (!comptypes (typ1, typ2))
8367             incompat_string_cst = true;
8368
8369           if (incompat_string_cst)
8370             {
8371               error_init (init_loc, "cannot initialize array of %qT from "
8372                           "a string literal with type array of %qT",
8373                           typ1, typ2);
8374               return error_mark_node;
8375             }
8376
8377           if (require_constexpr
8378               && TYPE_UNSIGNED (typ1) != TYPE_UNSIGNED (typ2))
8379             {
8380               /* Check if all characters of the string can be
8381                  represented in the type of the constexpr object being
8382                  initialized.  */
8383               unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8384               const unsigned char *p =
8385                 (const unsigned char *) TREE_STRING_POINTER (inside_init);
8386               gcc_assert (CHAR_TYPE_SIZE == 8 && CHAR_BIT == 8);
8387               for (unsigned i = 0; i < len; i++)
8388                 if (p[i] > 127)
8389                   {
8390                     error_init (init_loc, "%<constexpr%> initializer not "
8391                                 "representable in type of object");
8392                     break;
8393                   }
8394             }
8395
8396           if (TYPE_DOMAIN (type) != NULL_TREE
8397               && TYPE_SIZE (type) != NULL_TREE
8398               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8399             {
8400               unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8401               unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8402
8403               /* Subtract the size of a single (possibly wide) character
8404                  because it's ok to ignore the terminating null char
8405                  that is counted in the length of the constant.  */
8406               if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8407                 pedwarn_init (init_loc, 0,
8408                               ("initializer-string for array of %qT "
8409                                "is too long"), typ1);
8410               else if (warn_cxx_compat
8411                        && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8412                 warning_at (init_loc, OPT_Wc___compat,
8413                             ("initializer-string for array of %qT "
8414                              "is too long for C++"), typ1);
8415               if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8416                 {
8417                   unsigned HOST_WIDE_INT size
8418                     = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8419                   const char *p = TREE_STRING_POINTER (inside_init);
8420
8421                   inside_init = build_string (size, p);
8422                 }
8423             }
8424
8425           TREE_TYPE (inside_init) = type;
8426           return inside_init;
8427         }
8428       else if (INTEGRAL_TYPE_P (typ1))
8429         {
8430           error_init (init_loc, "array of inappropriate type initialized "
8431                       "from string constant");
8432           return error_mark_node;
8433         }
8434     }
8435
8436   /* Build a VECTOR_CST from a *constant* vector constructor.  If the
8437      vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8438      below and handle as a constructor.  */
8439   if (code == VECTOR_TYPE
8440       && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8441       && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8442       && TREE_CONSTANT (inside_init))
8443     {
8444       if (TREE_CODE (inside_init) == VECTOR_CST
8445           && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8446                         TYPE_MAIN_VARIANT (type)))
8447         return inside_init;
8448
8449       if (TREE_CODE (inside_init) == CONSTRUCTOR)
8450         {
8451           unsigned HOST_WIDE_INT ix;
8452           tree value;
8453           bool constant_p = true;
8454
8455           /* Iterate through elements and check if all constructor
8456              elements are *_CSTs.  */
8457           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8458             if (!CONSTANT_CLASS_P (value))
8459               {
8460                 constant_p = false;
8461                 break;
8462               }
8463
8464           if (constant_p)
8465             return build_vector_from_ctor (type,
8466                                            CONSTRUCTOR_ELTS (inside_init));
8467         }
8468     }
8469
8470   if (warn_sequence_point)
8471     verify_sequence_points (inside_init);
8472
8473   /* Any type can be initialized
8474      from an expression of the same type, optionally with braces.  */
8475
8476   if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8477       && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8478                      TYPE_MAIN_VARIANT (type))
8479           || (code == ARRAY_TYPE
8480               && comptypes (TREE_TYPE (inside_init), type))
8481           || (gnu_vector_type_p (type)
8482               && comptypes (TREE_TYPE (inside_init), type))
8483           || (code == POINTER_TYPE
8484               && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8485               && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8486                             TREE_TYPE (type)))))
8487     {
8488       if (code == POINTER_TYPE)
8489         {
8490           if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8491             {
8492               if (TREE_CODE (inside_init) == STRING_CST
8493                   || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8494                 inside_init = array_to_pointer_conversion
8495                   (init_loc, inside_init);
8496               else
8497                 {
8498                   error_init (init_loc, "invalid use of non-lvalue array");
8499                   return error_mark_node;
8500                 }
8501             }
8502         }
8503
8504       if (code == VECTOR_TYPE)
8505         /* Although the types are compatible, we may require a
8506            conversion.  */
8507         inside_init = convert (type, inside_init);
8508
8509       if (require_constant
8510           && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8511         {
8512           /* As an extension, allow initializing objects with static storage
8513              duration with compound literals (which are then treated just as
8514              the brace enclosed list they contain).  Also allow this for
8515              vectors, as we can only assign them with compound literals.  */
8516           if (flag_isoc99 && code != VECTOR_TYPE)
8517             pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8518                           "is not constant");
8519           tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8520           inside_init = DECL_INITIAL (decl);
8521         }
8522
8523       if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8524           && TREE_CODE (inside_init) != CONSTRUCTOR)
8525         {
8526           error_init (init_loc, "array initialized from non-constant array "
8527                       "expression");
8528           return error_mark_node;
8529         }
8530
8531       /* Compound expressions can only occur here if -Wpedantic or
8532          -pedantic-errors is specified.  In the later case, we always want
8533          an error.  In the former case, we simply want a warning.  */
8534       if (require_constant && pedantic
8535           && TREE_CODE (inside_init) == COMPOUND_EXPR)
8536         {
8537           inside_init
8538             = valid_compound_expr_initializer (inside_init,
8539                                                TREE_TYPE (inside_init));
8540           if (inside_init == error_mark_node)
8541             error_init (init_loc, "initializer element is not constant");
8542           else
8543             pedwarn_init (init_loc, OPT_Wpedantic,
8544                           "initializer element is not constant");
8545           if (flag_pedantic_errors)
8546             inside_init = error_mark_node;
8547         }
8548       else if (require_constant
8549                && !initializer_constant_valid_p (inside_init,
8550                                                  TREE_TYPE (inside_init)))
8551         {
8552           error_init (init_loc, "initializer element is not constant");
8553           inside_init = error_mark_node;
8554         }
8555       else if (require_constant && !maybe_const)
8556         pedwarn_init (init_loc, OPT_Wpedantic,
8557                       "initializer element is not a constant expression");
8558       else if (require_constexpr)
8559         check_constexpr_init (init_loc, type, inside_init,
8560                               null_pointer_constant, int_const_expr,
8561                               arith_const_expr);
8562
8563       /* Added to enable additional -Wsuggest-attribute=format warnings.  */
8564       if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8565         inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8566                                               type, inside_init, origtype,
8567                                               (require_constant
8568                                                ? ic_init_const
8569                                                : ic_init), null_pointer_constant,
8570                                               NULL_TREE, NULL_TREE, 0);
8571       return inside_init;
8572     }
8573
8574   /* Handle scalar types, including conversions.  */
8575
8576   if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8577       || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8578       || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8579     {
8580       tree unconverted_init = inside_init;
8581       if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8582           && (TREE_CODE (init) == STRING_CST
8583               || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8584         inside_init = init = array_to_pointer_conversion (init_loc, init);
8585       if (semantic_type)
8586         inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8587                               inside_init);
8588       inside_init
8589         = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8590                                   inside_init, origtype,
8591                                   require_constant ? ic_init_const : ic_init,
8592                                   null_pointer_constant, NULL_TREE, NULL_TREE,
8593                                   0);
8594
8595       /* Check to see if we have already given an error message.  */
8596       if (inside_init == error_mark_node)
8597         ;
8598       else if (require_constant && !TREE_CONSTANT (inside_init))
8599         {
8600           error_init (init_loc, "initializer element is not constant");
8601           inside_init = error_mark_node;
8602         }
8603       else if (require_constant
8604                && !initializer_constant_valid_p (inside_init,
8605                                                  TREE_TYPE (inside_init)))
8606         {
8607           error_init (init_loc, "initializer element is not computable at "
8608                       "load time");
8609           inside_init = error_mark_node;
8610         }
8611       else if (require_constant && !maybe_const)
8612         pedwarn_init (init_loc, OPT_Wpedantic,
8613                       "initializer element is not a constant expression");
8614       else if (require_constexpr)
8615         check_constexpr_init (init_loc, type, unconverted_init,
8616                               null_pointer_constant, int_const_expr,
8617                               arith_const_expr);
8618
8619       return inside_init;
8620     }
8621
8622   /* Come here only for records and arrays.  */
8623
8624   if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8625     {
8626       error_init (init_loc,
8627                   "variable-sized object may not be initialized except "
8628                   "with an empty initializer");
8629       return error_mark_node;
8630     }
8631
8632   error_init (init_loc, "invalid initializer");
8633   return error_mark_node;
8634 }
8635 \f
8636 /* Handle initializers that use braces.  */
8637
8638 /* Type of object we are accumulating a constructor for.
8639    This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE.  */
8640 static tree constructor_type;
8641
8642 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8643    left to fill.  */
8644 static tree constructor_fields;
8645
8646 /* For an ARRAY_TYPE, this is the specified index
8647    at which to store the next element we get.  */
8648 static tree constructor_index;
8649
8650 /* For an ARRAY_TYPE, this is the maximum index.  */
8651 static tree constructor_max_index;
8652
8653 /* For a RECORD_TYPE, this is the first field not yet written out.  */
8654 static tree constructor_unfilled_fields;
8655
8656 /* For an ARRAY_TYPE, this is the index of the first element
8657    not yet written out.  */
8658 static tree constructor_unfilled_index;
8659
8660 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8661    This is so we can generate gaps between fields, when appropriate.  */
8662 static tree constructor_bit_index;
8663
8664 /* If we are saving up the elements rather than allocating them,
8665    this is the list of elements so far (in reverse order,
8666    most recent first).  */
8667 static vec<constructor_elt, va_gc> *constructor_elements;
8668
8669 /* 1 if constructor should be incrementally stored into a constructor chain,
8670    0 if all the elements should be kept in AVL tree.  */
8671 static int constructor_incremental;
8672
8673 /* 1 if so far this constructor's elements are all compile-time constants.  */
8674 static int constructor_constant;
8675
8676 /* 1 if so far this constructor's elements are all valid address constants.  */
8677 static int constructor_simple;
8678
8679 /* 1 if this constructor has an element that cannot be part of a
8680    constant expression.  */
8681 static int constructor_nonconst;
8682
8683 /* 1 if this constructor is erroneous so far.  */
8684 static int constructor_erroneous;
8685
8686 /* 1 if this constructor is the universal zero initializer { 0 }.  */
8687 static int constructor_zeroinit;
8688
8689 /* Structure for managing pending initializer elements, organized as an
8690    AVL tree.  */
8691
8692 struct init_node
8693 {
8694   struct init_node *left, *right;
8695   struct init_node *parent;
8696   int balance;
8697   tree purpose;
8698   tree value;
8699   tree origtype;
8700 };
8701
8702 /* Tree of pending elements at this constructor level.
8703    These are elements encountered out of order
8704    which belong at places we haven't reached yet in actually
8705    writing the output.
8706    Will never hold tree nodes across GC runs.  */
8707 static struct init_node *constructor_pending_elts;
8708
8709 /* The SPELLING_DEPTH of this constructor.  */
8710 static int constructor_depth;
8711
8712 /* DECL node for which an initializer is being read.
8713    0 means we are reading a constructor expression
8714    such as (struct foo) {...}.  */
8715 static tree constructor_decl;
8716
8717 /* Nonzero if there were any member designators in this initializer.  */
8718 static int constructor_designated;
8719
8720 /* Nesting depth of designator list.  */
8721 static int designator_depth;
8722
8723 /* Nonzero if there were diagnosed errors in this designator list.  */
8724 static int designator_erroneous;
8725
8726 \f
8727 /* This stack has a level for each implicit or explicit level of
8728    structuring in the initializer, including the outermost one.  It
8729    saves the values of most of the variables above.  */
8730
8731 struct constructor_range_stack;
8732
8733 struct constructor_stack
8734 {
8735   struct constructor_stack *next;
8736   tree type;
8737   tree fields;
8738   tree index;
8739   tree max_index;
8740   tree unfilled_index;
8741   tree unfilled_fields;
8742   tree bit_index;
8743   vec<constructor_elt, va_gc> *elements;
8744   struct init_node *pending_elts;
8745   int offset;
8746   int depth;
8747   /* If value nonzero, this value should replace the entire
8748      constructor at this level.  */
8749   struct c_expr replacement_value;
8750   struct constructor_range_stack *range_stack;
8751   char constant;
8752   char simple;
8753   char nonconst;
8754   char implicit;
8755   char erroneous;
8756   char outer;
8757   char incremental;
8758   char designated;
8759   int designator_depth;
8760 };
8761
8762 static struct constructor_stack *constructor_stack;
8763
8764 /* This stack represents designators from some range designator up to
8765    the last designator in the list.  */
8766
8767 struct constructor_range_stack
8768 {
8769   struct constructor_range_stack *next, *prev;
8770   struct constructor_stack *stack;
8771   tree range_start;
8772   tree index;
8773   tree range_end;
8774   tree fields;
8775 };
8776
8777 static struct constructor_range_stack *constructor_range_stack;
8778
8779 /* This stack records separate initializers that are nested.
8780    Nested initializers can't happen in ANSI C, but GNU C allows them
8781    in cases like { ... (struct foo) { ... } ... }.  */
8782
8783 struct initializer_stack
8784 {
8785   struct initializer_stack *next;
8786   tree decl;
8787   struct constructor_stack *constructor_stack;
8788   struct constructor_range_stack *constructor_range_stack;
8789   vec<constructor_elt, va_gc> *elements;
8790   struct spelling *spelling;
8791   struct spelling *spelling_base;
8792   int spelling_size;
8793   char require_constant_value;
8794   char require_constant_elements;
8795   char require_constexpr_value;
8796   char designated;
8797   rich_location *missing_brace_richloc;
8798 };
8799
8800 static struct initializer_stack *initializer_stack;
8801 \f
8802 /* Prepare to parse and output the initializer for variable DECL.  */
8803
8804 void
8805 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED,
8806             bool init_require_constant, bool init_require_constexpr,
8807             rich_location *richloc)
8808 {
8809   const char *locus;
8810   struct initializer_stack *p = XNEW (struct initializer_stack);
8811
8812   p->decl = constructor_decl;
8813   p->require_constant_value = require_constant_value;
8814   p->require_constant_elements = require_constant_elements;
8815   p->require_constexpr_value = require_constexpr_value;
8816   p->constructor_stack = constructor_stack;
8817   p->constructor_range_stack = constructor_range_stack;
8818   p->elements = constructor_elements;
8819   p->spelling = spelling;
8820   p->spelling_base = spelling_base;
8821   p->spelling_size = spelling_size;
8822   p->next = initializer_stack;
8823   p->missing_brace_richloc = richloc;
8824   p->designated = constructor_designated;
8825   initializer_stack = p;
8826
8827   constructor_decl = decl;
8828   constructor_designated = 0;
8829
8830   require_constant_value = init_require_constant;
8831   require_constexpr_value = init_require_constexpr;
8832   if (decl != NULL_TREE && decl != error_mark_node)
8833     {
8834       require_constant_elements
8835         = ((init_require_constant || (pedantic && !flag_isoc99))
8836            /* For a scalar, you can always use any value to initialize,
8837               even within braces.  */
8838            && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8839       locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8840     }
8841   else
8842     {
8843       require_constant_elements = false;
8844       locus = _("(anonymous)");
8845     }
8846
8847   constructor_stack = 0;
8848   constructor_range_stack = 0;
8849
8850   found_missing_braces = 0;
8851
8852   spelling_base = 0;
8853   spelling_size = 0;
8854   RESTORE_SPELLING_DEPTH (0);
8855
8856   if (locus)
8857     push_string (locus);
8858 }
8859
8860 void
8861 finish_init (void)
8862 {
8863   struct initializer_stack *p = initializer_stack;
8864
8865   /* Free the whole constructor stack of this initializer.  */
8866   while (constructor_stack)
8867     {
8868       struct constructor_stack *q = constructor_stack;
8869       constructor_stack = q->next;
8870       XDELETE (q);
8871     }
8872
8873   gcc_assert (!constructor_range_stack);
8874
8875   /* Pop back to the data of the outer initializer (if any).  */
8876   XDELETE (spelling_base);
8877
8878   constructor_decl = p->decl;
8879   require_constant_value = p->require_constant_value;
8880   require_constant_elements = p->require_constant_elements;
8881   require_constexpr_value = p->require_constexpr_value;
8882   constructor_stack = p->constructor_stack;
8883   constructor_designated = p->designated;
8884   constructor_range_stack = p->constructor_range_stack;
8885   constructor_elements = p->elements;
8886   spelling = p->spelling;
8887   spelling_base = p->spelling_base;
8888   spelling_size = p->spelling_size;
8889   initializer_stack = p->next;
8890   XDELETE (p);
8891 }
8892 \f
8893 /* Call here when we see the initializer is surrounded by braces.
8894    This is instead of a call to push_init_level;
8895    it is matched by a call to pop_init_level.
8896
8897    TYPE is the type to initialize, for a constructor expression.
8898    For an initializer for a decl, TYPE is zero.  */
8899
8900 void
8901 really_start_incremental_init (tree type)
8902 {
8903   struct constructor_stack *p = XNEW (struct constructor_stack);
8904
8905   if (type == NULL_TREE)
8906     type = TREE_TYPE (constructor_decl);
8907
8908   if (VECTOR_TYPE_P (type)
8909       && TYPE_VECTOR_OPAQUE (type))
8910     error ("opaque vector types cannot be initialized");
8911
8912   p->type = constructor_type;
8913   p->fields = constructor_fields;
8914   p->index = constructor_index;
8915   p->max_index = constructor_max_index;
8916   p->unfilled_index = constructor_unfilled_index;
8917   p->unfilled_fields = constructor_unfilled_fields;
8918   p->bit_index = constructor_bit_index;
8919   p->elements = constructor_elements;
8920   p->constant = constructor_constant;
8921   p->simple = constructor_simple;
8922   p->nonconst = constructor_nonconst;
8923   p->erroneous = constructor_erroneous;
8924   p->pending_elts = constructor_pending_elts;
8925   p->depth = constructor_depth;
8926   p->replacement_value.value = 0;
8927   p->replacement_value.original_code = ERROR_MARK;
8928   p->replacement_value.original_type = NULL;
8929   p->implicit = 0;
8930   p->range_stack = 0;
8931   p->outer = 0;
8932   p->incremental = constructor_incremental;
8933   p->designated = constructor_designated;
8934   p->designator_depth = designator_depth;
8935   p->next = 0;
8936   constructor_stack = p;
8937
8938   constructor_constant = 1;
8939   constructor_simple = 1;
8940   constructor_nonconst = 0;
8941   constructor_depth = SPELLING_DEPTH ();
8942   constructor_elements = NULL;
8943   constructor_pending_elts = 0;
8944   constructor_type = type;
8945   constructor_incremental = 1;
8946   constructor_designated = 0;
8947   constructor_zeroinit = 1;
8948   designator_depth = 0;
8949   designator_erroneous = 0;
8950
8951   if (RECORD_OR_UNION_TYPE_P (constructor_type))
8952     {
8953       constructor_fields = TYPE_FIELDS (constructor_type);
8954       /* Skip any nameless bit fields at the beginning.  */
8955       while (constructor_fields != NULL_TREE
8956              && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8957         constructor_fields = DECL_CHAIN (constructor_fields);
8958
8959       constructor_unfilled_fields = constructor_fields;
8960       constructor_bit_index = bitsize_zero_node;
8961     }
8962   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8963     {
8964       if (TYPE_DOMAIN (constructor_type))
8965         {
8966           constructor_max_index
8967             = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8968
8969           /* Detect non-empty initializations of zero-length arrays.  */
8970           if (constructor_max_index == NULL_TREE
8971               && TYPE_SIZE (constructor_type))
8972             constructor_max_index = integer_minus_one_node;
8973
8974           /* constructor_max_index needs to be an INTEGER_CST.  Attempts
8975              to initialize VLAs with a nonempty initializer will cause a
8976              proper error; avoid tree checking errors as well by setting a
8977              safe value.  */
8978           if (constructor_max_index
8979               && TREE_CODE (constructor_max_index) != INTEGER_CST)
8980             constructor_max_index = integer_minus_one_node;
8981
8982           constructor_index
8983             = convert (bitsizetype,
8984                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8985         }
8986       else
8987         {
8988           constructor_index = bitsize_zero_node;
8989           constructor_max_index = NULL_TREE;
8990         }
8991
8992       constructor_unfilled_index = constructor_index;
8993     }
8994   else if (gnu_vector_type_p (constructor_type))
8995     {
8996       /* Vectors are like simple fixed-size arrays.  */
8997       constructor_max_index =
8998         bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8999       constructor_index = bitsize_zero_node;
9000       constructor_unfilled_index = constructor_index;
9001     }
9002   else
9003     {
9004       /* Handle the case of int x = {5}; */
9005       constructor_fields = constructor_type;
9006       constructor_unfilled_fields = constructor_type;
9007     }
9008 }
9009 \f
9010 extern location_t last_init_list_comma;
9011
9012 /* Called when we see an open brace for a nested initializer.  Finish
9013    off any pending levels with implicit braces.  */
9014 void
9015 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
9016 {
9017   while (constructor_stack->implicit)
9018     {
9019       if (RECORD_OR_UNION_TYPE_P (constructor_type)
9020           && constructor_fields == NULL_TREE)
9021         process_init_element (input_location,
9022                               pop_init_level (loc, 1, braced_init_obstack,
9023                                               last_init_list_comma),
9024                               true, braced_init_obstack);
9025       else if (TREE_CODE (constructor_type) == ARRAY_TYPE
9026                && constructor_max_index
9027                && tree_int_cst_lt (constructor_max_index,
9028                                    constructor_index))
9029         process_init_element (input_location,
9030                               pop_init_level (loc, 1, braced_init_obstack,
9031                                               last_init_list_comma),
9032                               true, braced_init_obstack);
9033       else
9034         break;
9035     }
9036 }
9037
9038 /* Push down into a subobject, for initialization.
9039    If this is for an explicit set of braces, IMPLICIT is 0.
9040    If it is because the next element belongs at a lower level,
9041    IMPLICIT is 1 (or 2 if the push is because of designator list).  */
9042
9043 void
9044 push_init_level (location_t loc, int implicit,
9045                  struct obstack *braced_init_obstack)
9046 {
9047   struct constructor_stack *p;
9048   tree value = NULL_TREE;
9049
9050   /* Unless this is an explicit brace, we need to preserve previous
9051      content if any.  */
9052   if (implicit)
9053     {
9054       if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
9055         value = find_init_member (constructor_fields, braced_init_obstack);
9056       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9057         value = find_init_member (constructor_index, braced_init_obstack);
9058     }
9059
9060   p = XNEW (struct constructor_stack);
9061   p->type = constructor_type;
9062   p->fields = constructor_fields;
9063   p->index = constructor_index;
9064   p->max_index = constructor_max_index;
9065   p->unfilled_index = constructor_unfilled_index;
9066   p->unfilled_fields = constructor_unfilled_fields;
9067   p->bit_index = constructor_bit_index;
9068   p->elements = constructor_elements;
9069   p->constant = constructor_constant;
9070   p->simple = constructor_simple;
9071   p->nonconst = constructor_nonconst;
9072   p->erroneous = constructor_erroneous;
9073   p->pending_elts = constructor_pending_elts;
9074   p->depth = constructor_depth;
9075   p->replacement_value.value = NULL_TREE;
9076   p->replacement_value.original_code = ERROR_MARK;
9077   p->replacement_value.original_type = NULL;
9078   p->implicit = implicit;
9079   p->outer = 0;
9080   p->incremental = constructor_incremental;
9081   p->designated = constructor_designated;
9082   p->designator_depth = designator_depth;
9083   p->next = constructor_stack;
9084   p->range_stack = 0;
9085   constructor_stack = p;
9086
9087   constructor_constant = 1;
9088   constructor_simple = 1;
9089   constructor_nonconst = 0;
9090   constructor_depth = SPELLING_DEPTH ();
9091   constructor_elements = NULL;
9092   constructor_incremental = 1;
9093   /* If the upper initializer is designated, then mark this as
9094      designated too to prevent bogus warnings.  */
9095   constructor_designated = p->designated;
9096   constructor_pending_elts = 0;
9097   if (!implicit)
9098     {
9099       p->range_stack = constructor_range_stack;
9100       constructor_range_stack = 0;
9101       designator_depth = 0;
9102       designator_erroneous = 0;
9103     }
9104
9105   /* Don't die if an entire brace-pair level is superfluous
9106      in the containing level.  */
9107   if (constructor_type == NULL_TREE)
9108     ;
9109   else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9110     {
9111       /* Don't die if there are extra init elts at the end.  */
9112       if (constructor_fields == NULL_TREE)
9113         constructor_type = NULL_TREE;
9114       else
9115         {
9116           constructor_type = TREE_TYPE (constructor_fields);
9117           push_member_name (constructor_fields);
9118           constructor_depth++;
9119         }
9120     }
9121   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9122     {
9123       constructor_type = TREE_TYPE (constructor_type);
9124       push_array_bounds (tree_to_uhwi (constructor_index));
9125       constructor_depth++;
9126     }
9127
9128   if (constructor_type == NULL_TREE)
9129     {
9130       error_init (loc, "extra brace group at end of initializer");
9131       constructor_fields = NULL_TREE;
9132       constructor_unfilled_fields = NULL_TREE;
9133       return;
9134     }
9135
9136   if (value && TREE_CODE (value) == CONSTRUCTOR)
9137     {
9138       constructor_constant = TREE_CONSTANT (value);
9139       constructor_simple = TREE_STATIC (value);
9140       constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
9141       constructor_elements = CONSTRUCTOR_ELTS (value);
9142       if (!vec_safe_is_empty (constructor_elements)
9143           && (TREE_CODE (constructor_type) == RECORD_TYPE
9144               || TREE_CODE (constructor_type) == ARRAY_TYPE))
9145         set_nonincremental_init (braced_init_obstack);
9146     }
9147
9148   if (implicit == 1)
9149     {
9150       found_missing_braces = 1;
9151       if (initializer_stack->missing_brace_richloc)
9152         initializer_stack->missing_brace_richloc->add_fixit_insert_before
9153           (loc, "{");
9154     }
9155
9156   if (RECORD_OR_UNION_TYPE_P (constructor_type))
9157     {
9158       constructor_fields = TYPE_FIELDS (constructor_type);
9159       /* Skip any nameless bit fields at the beginning.  */
9160       while (constructor_fields != NULL_TREE
9161              && DECL_UNNAMED_BIT_FIELD (constructor_fields))
9162         constructor_fields = DECL_CHAIN (constructor_fields);
9163
9164       constructor_unfilled_fields = constructor_fields;
9165       constructor_bit_index = bitsize_zero_node;
9166     }
9167   else if (gnu_vector_type_p (constructor_type))
9168     {
9169       /* Vectors are like simple fixed-size arrays.  */
9170       constructor_max_index =
9171         bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
9172       constructor_index = bitsize_int (0);
9173       constructor_unfilled_index = constructor_index;
9174     }
9175   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9176     {
9177       if (TYPE_DOMAIN (constructor_type))
9178         {
9179           constructor_max_index
9180             = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
9181
9182           /* Detect non-empty initializations of zero-length arrays.  */
9183           if (constructor_max_index == NULL_TREE
9184               && TYPE_SIZE (constructor_type))
9185             constructor_max_index = integer_minus_one_node;
9186
9187           /* constructor_max_index needs to be an INTEGER_CST.  Attempts
9188              to initialize VLAs will cause a proper error; avoid tree
9189              checking errors as well by setting a safe value.  */
9190           if (constructor_max_index
9191               && TREE_CODE (constructor_max_index) != INTEGER_CST)
9192             constructor_max_index = integer_minus_one_node;
9193
9194           constructor_index
9195             = convert (bitsizetype,
9196                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9197         }
9198       else
9199         constructor_index = bitsize_zero_node;
9200
9201       constructor_unfilled_index = constructor_index;
9202       if (value && TREE_CODE (value) == STRING_CST)
9203         {
9204           /* We need to split the char/wchar array into individual
9205              characters, so that we don't have to special case it
9206              everywhere.  */
9207           set_nonincremental_init_from_string (value, braced_init_obstack);
9208         }
9209     }
9210   else
9211     {
9212       if (constructor_type != error_mark_node)
9213         warning_init (input_location, 0, "braces around scalar initializer");
9214       constructor_fields = constructor_type;
9215       constructor_unfilled_fields = constructor_type;
9216     }
9217 }
9218
9219 /* At the end of an implicit or explicit brace level,
9220    finish up that level of constructor.  If a single expression
9221    with redundant braces initialized that level, return the
9222    c_expr structure for that expression.  Otherwise, the original_code
9223    element is set to ERROR_MARK.
9224    If we were outputting the elements as they are read, return 0 as the value
9225    from inner levels (process_init_element ignores that),
9226    but return error_mark_node as the value from the outermost level
9227    (that's what we want to put in DECL_INITIAL).
9228    Otherwise, return a CONSTRUCTOR expression as the value.  */
9229
9230 struct c_expr
9231 pop_init_level (location_t loc, int implicit,
9232                 struct obstack *braced_init_obstack,
9233                 location_t insert_before)
9234 {
9235   struct constructor_stack *p;
9236   struct c_expr ret;
9237   ret.value = NULL_TREE;
9238   ret.original_code = ERROR_MARK;
9239   ret.original_type = NULL;
9240   ret.m_decimal = 0;
9241
9242   if (implicit == 0)
9243     {
9244       /* When we come to an explicit close brace,
9245          pop any inner levels that didn't have explicit braces.  */
9246       while (constructor_stack->implicit)
9247         process_init_element (input_location,
9248                               pop_init_level (loc, 1, braced_init_obstack,
9249                                               insert_before),
9250                               true, braced_init_obstack);
9251       gcc_assert (!constructor_range_stack);
9252     }
9253   else
9254     if (initializer_stack->missing_brace_richloc)
9255       initializer_stack->missing_brace_richloc->add_fixit_insert_before
9256         (insert_before, "}");
9257
9258   /* Now output all pending elements.  */
9259   constructor_incremental = 1;
9260   output_pending_init_elements (1, braced_init_obstack);
9261
9262   p = constructor_stack;
9263
9264   /* Error for initializing a flexible array member, or a zero-length
9265      array member in an inappropriate context.  */
9266   if (constructor_type && constructor_fields
9267       && TREE_CODE (constructor_type) == ARRAY_TYPE
9268       && TYPE_DOMAIN (constructor_type)
9269       && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
9270     {
9271       /* Silently discard empty initializations.  The parser will
9272          already have pedwarned for empty brackets.  */
9273       if (integer_zerop (constructor_unfilled_index))
9274         constructor_type = NULL_TREE;
9275       else
9276         {
9277           gcc_assert (!TYPE_SIZE (constructor_type));
9278
9279           if (constructor_depth > 2)
9280             error_init (loc, "initialization of flexible array member in a nested context");
9281           else
9282             pedwarn_init (loc, OPT_Wpedantic,
9283                           "initialization of a flexible array member");
9284
9285           /* We have already issued an error message for the existence
9286              of a flexible array member not at the end of the structure.
9287              Discard the initializer so that we do not die later.  */
9288           if (DECL_CHAIN (constructor_fields) != NULL_TREE)
9289             constructor_type = NULL_TREE;
9290         }
9291     }
9292
9293   switch (vec_safe_length (constructor_elements))
9294     {
9295     case 0:
9296       /* Initialization with { } counts as zeroinit.  */
9297       constructor_zeroinit = 1;
9298       break;
9299     case 1:
9300       /* This might be zeroinit as well.  */
9301       if (integer_zerop ((*constructor_elements)[0].value))
9302         constructor_zeroinit = 1;
9303       break;
9304     default:
9305       /* If the constructor has more than one element, it can't be { 0 }.  */
9306       constructor_zeroinit = 0;
9307       break;
9308     }
9309
9310   /* Warn when some structs are initialized with direct aggregation.  */
9311   if (!implicit && found_missing_braces && warn_missing_braces
9312       && !constructor_zeroinit)
9313     {
9314       gcc_assert (initializer_stack->missing_brace_richloc);
9315       warning_at (initializer_stack->missing_brace_richloc,
9316                   OPT_Wmissing_braces,
9317                   "missing braces around initializer");
9318     }
9319
9320   /* Warn when some struct elements are implicitly initialized to zero.  */
9321   if (warn_missing_field_initializers
9322       && constructor_type
9323       && TREE_CODE (constructor_type) == RECORD_TYPE
9324       && constructor_unfilled_fields)
9325     {
9326         /* Do not warn for flexible array members or zero-length arrays.  */
9327         while (constructor_unfilled_fields
9328                && (!DECL_SIZE (constructor_unfilled_fields)
9329                    || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
9330           constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
9331
9332         if (constructor_unfilled_fields
9333             /* Do not warn if this level of the initializer uses member
9334                designators; it is likely to be deliberate.  */
9335             && !constructor_designated
9336             /* Do not warn about initializing with { 0 } or with { }.  */
9337             && !constructor_zeroinit)
9338           {
9339             if (warning_at (input_location, OPT_Wmissing_field_initializers,
9340                             "missing initializer for field %qD of %qT",
9341                             constructor_unfilled_fields,
9342                             constructor_type))
9343               inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
9344                       "%qD declared here", constructor_unfilled_fields);
9345           }
9346     }
9347
9348   /* Pad out the end of the structure.  */
9349   if (p->replacement_value.value)
9350     /* If this closes a superfluous brace pair,
9351        just pass out the element between them.  */
9352     ret = p->replacement_value;
9353   else if (constructor_type == NULL_TREE)
9354     ;
9355   else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
9356            && TREE_CODE (constructor_type) != ARRAY_TYPE
9357            && !gnu_vector_type_p (constructor_type))
9358     {
9359       /* A nonincremental scalar initializer--just return
9360          the element, after verifying there is just one.
9361          Empty scalar initializers are supported in C2X.  */
9362       if (vec_safe_is_empty (constructor_elements))
9363         {
9364           if (constructor_erroneous || constructor_type == error_mark_node)
9365             ret.value = error_mark_node;
9366           else if (TREE_CODE (constructor_type) == POINTER_TYPE)
9367             /* Ensure this is a null pointer constant in the case of a
9368                'constexpr' object initialized with {}.  */
9369             ret.value = build_zero_cst (ptr_type_node);
9370           else
9371             ret.value = build_zero_cst (constructor_type);
9372         }
9373       else if (vec_safe_length (constructor_elements) != 1)
9374         {
9375           error_init (loc, "extra elements in scalar initializer");
9376           ret.value = (*constructor_elements)[0].value;
9377         }
9378       else
9379         ret.value = (*constructor_elements)[0].value;
9380     }
9381   else
9382     {
9383       if (constructor_erroneous)
9384         ret.value = error_mark_node;
9385       else
9386         {
9387           ret.value = build_constructor (constructor_type,
9388                                          constructor_elements);
9389           if (constructor_constant)
9390             TREE_CONSTANT (ret.value) = 1;
9391           if (constructor_constant && constructor_simple)
9392             TREE_STATIC (ret.value) = 1;
9393           if (constructor_nonconst)
9394             CONSTRUCTOR_NON_CONST (ret.value) = 1;
9395         }
9396     }
9397
9398   if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9399     {
9400       if (constructor_nonconst)
9401         ret.original_code = C_MAYBE_CONST_EXPR;
9402       else if (ret.original_code == C_MAYBE_CONST_EXPR)
9403         ret.original_code = ERROR_MARK;
9404     }
9405
9406   constructor_type = p->type;
9407   constructor_fields = p->fields;
9408   constructor_index = p->index;
9409   constructor_max_index = p->max_index;
9410   constructor_unfilled_index = p->unfilled_index;
9411   constructor_unfilled_fields = p->unfilled_fields;
9412   constructor_bit_index = p->bit_index;
9413   constructor_elements = p->elements;
9414   constructor_constant = p->constant;
9415   constructor_simple = p->simple;
9416   constructor_nonconst = p->nonconst;
9417   constructor_erroneous = p->erroneous;
9418   constructor_incremental = p->incremental;
9419   constructor_designated = p->designated;
9420   designator_depth = p->designator_depth;
9421   constructor_pending_elts = p->pending_elts;
9422   constructor_depth = p->depth;
9423   if (!p->implicit)
9424     constructor_range_stack = p->range_stack;
9425   RESTORE_SPELLING_DEPTH (constructor_depth);
9426
9427   constructor_stack = p->next;
9428   XDELETE (p);
9429
9430   if (ret.value == NULL_TREE && constructor_stack == 0)
9431     ret.value = error_mark_node;
9432   return ret;
9433 }
9434
9435 /* Common handling for both array range and field name designators.
9436    ARRAY argument is nonzero for array ranges.  Returns false for success.  */
9437
9438 static bool
9439 set_designator (location_t loc, bool array,
9440                 struct obstack *braced_init_obstack)
9441 {
9442   tree subtype;
9443   enum tree_code subcode;
9444
9445   /* Don't die if an entire brace-pair level is superfluous
9446      in the containing level, or for an erroneous type.  */
9447   if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9448     return true;
9449
9450   /* If there were errors in this designator list already, bail out
9451      silently.  */
9452   if (designator_erroneous)
9453     return true;
9454
9455   /* Likewise for an initializer for a variable-size type.  Those are
9456      diagnosed in the parser, except for empty initializer braces.  */
9457   if (COMPLETE_TYPE_P (constructor_type)
9458       && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9459     return true;
9460
9461   if (!designator_depth)
9462     {
9463       gcc_assert (!constructor_range_stack);
9464
9465       /* Designator list starts at the level of closest explicit
9466          braces.  */
9467       while (constructor_stack->implicit)
9468         process_init_element (input_location,
9469                               pop_init_level (loc, 1, braced_init_obstack,
9470                                               last_init_list_comma),
9471                               true, braced_init_obstack);
9472       constructor_designated = 1;
9473       return false;
9474     }
9475
9476   switch (TREE_CODE (constructor_type))
9477     {
9478     case  RECORD_TYPE:
9479     case  UNION_TYPE:
9480       subtype = TREE_TYPE (constructor_fields);
9481       if (subtype != error_mark_node)
9482         subtype = TYPE_MAIN_VARIANT (subtype);
9483       break;
9484     case ARRAY_TYPE:
9485       subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9486       break;
9487     default:
9488       gcc_unreachable ();
9489     }
9490
9491   subcode = TREE_CODE (subtype);
9492   if (array && subcode != ARRAY_TYPE)
9493     {
9494       error_init (loc, "array index in non-array initializer");
9495       return true;
9496     }
9497   else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9498     {
9499       error_init (loc, "field name not in record or union initializer");
9500       return true;
9501     }
9502
9503   constructor_designated = 1;
9504   finish_implicit_inits (loc, braced_init_obstack);
9505   push_init_level (loc, 2, braced_init_obstack);
9506   return false;
9507 }
9508
9509 /* If there are range designators in designator list, push a new designator
9510    to constructor_range_stack.  RANGE_END is end of such stack range or
9511    NULL_TREE if there is no range designator at this level.  */
9512
9513 static void
9514 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9515 {
9516   struct constructor_range_stack *p;
9517
9518   p = (struct constructor_range_stack *)
9519     obstack_alloc (braced_init_obstack,
9520                    sizeof (struct constructor_range_stack));
9521   p->prev = constructor_range_stack;
9522   p->next = 0;
9523   p->fields = constructor_fields;
9524   p->range_start = constructor_index;
9525   p->index = constructor_index;
9526   p->stack = constructor_stack;
9527   p->range_end = range_end;
9528   if (constructor_range_stack)
9529     constructor_range_stack->next = p;
9530   constructor_range_stack = p;
9531 }
9532
9533 /* Within an array initializer, specify the next index to be initialized.
9534    FIRST is that index.  If LAST is nonzero, then initialize a range
9535    of indices, running from FIRST through LAST.  */
9536
9537 void
9538 set_init_index (location_t loc, tree first, tree last,
9539                 struct obstack *braced_init_obstack)
9540 {
9541   if (set_designator (loc, true, braced_init_obstack))
9542     return;
9543
9544   designator_erroneous = 1;
9545
9546   if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9547       || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9548     {
9549       error_init (loc, "array index in initializer not of integer type");
9550       return;
9551     }
9552
9553   if (TREE_CODE (first) != INTEGER_CST)
9554     {
9555       first = c_fully_fold (first, false, NULL);
9556       if (TREE_CODE (first) == INTEGER_CST)
9557         pedwarn_init (loc, OPT_Wpedantic,
9558                       "array index in initializer is not "
9559                       "an integer constant expression");
9560     }
9561
9562   if (last && TREE_CODE (last) != INTEGER_CST)
9563     {
9564       last = c_fully_fold (last, false, NULL);
9565       if (TREE_CODE (last) == INTEGER_CST)
9566         pedwarn_init (loc, OPT_Wpedantic,
9567                       "array index in initializer is not "
9568                       "an integer constant expression");
9569     }
9570
9571   if (TREE_CODE (first) != INTEGER_CST)
9572     error_init (loc, "nonconstant array index in initializer");
9573   else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9574     error_init (loc, "nonconstant array index in initializer");
9575   else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9576     error_init (loc, "array index in non-array initializer");
9577   else if (tree_int_cst_sgn (first) == -1)
9578     error_init (loc, "array index in initializer exceeds array bounds");
9579   else if (constructor_max_index
9580            && tree_int_cst_lt (constructor_max_index, first))
9581     error_init (loc, "array index in initializer exceeds array bounds");
9582   else
9583     {
9584       constant_expression_warning (first);
9585       if (last)
9586         constant_expression_warning (last);
9587       constructor_index = convert (bitsizetype, first);
9588       if (tree_int_cst_lt (constructor_index, first))
9589         {
9590           constructor_index = copy_node (constructor_index);
9591           TREE_OVERFLOW (constructor_index) = 1;
9592         }
9593
9594       if (last)
9595         {
9596           if (tree_int_cst_equal (first, last))
9597             last = NULL_TREE;
9598           else if (tree_int_cst_lt (last, first))
9599             {
9600               error_init (loc, "empty index range in initializer");
9601               last = NULL_TREE;
9602             }
9603           else
9604             {
9605               last = convert (bitsizetype, last);
9606               if (constructor_max_index != NULL_TREE
9607                   && tree_int_cst_lt (constructor_max_index, last))
9608                 {
9609                   error_init (loc, "array index range in initializer exceeds "
9610                               "array bounds");
9611                   last = NULL_TREE;
9612                 }
9613             }
9614         }
9615
9616       designator_depth++;
9617       designator_erroneous = 0;
9618       if (constructor_range_stack || last)
9619         push_range_stack (last, braced_init_obstack);
9620     }
9621 }
9622
9623 /* Within a struct initializer, specify the next field to be initialized.  */
9624
9625 void
9626 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9627                 struct obstack *braced_init_obstack)
9628 {
9629   tree field;
9630
9631   if (set_designator (loc, false, braced_init_obstack))
9632     return;
9633
9634   designator_erroneous = 1;
9635
9636   if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9637     {
9638       error_init (loc, "field name not in record or union initializer");
9639       return;
9640     }
9641
9642   field = lookup_field (constructor_type, fieldname);
9643
9644   if (field == NULL_TREE)
9645     {
9646       tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9647       if (guessed_id)
9648         {
9649           gcc_rich_location rich_loc (fieldname_loc);
9650           rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9651           error_at (&rich_loc,
9652                     "%qT has no member named %qE; did you mean %qE?",
9653                     constructor_type, fieldname, guessed_id);
9654         }
9655       else
9656         error_at (fieldname_loc, "%qT has no member named %qE",
9657                   constructor_type, fieldname);
9658     }
9659   else
9660     do
9661       {
9662         constructor_fields = TREE_VALUE (field);
9663         designator_depth++;
9664         designator_erroneous = 0;
9665         if (constructor_range_stack)
9666           push_range_stack (NULL_TREE, braced_init_obstack);
9667         field = TREE_CHAIN (field);
9668         if (field)
9669           {
9670             if (set_designator (loc, false, braced_init_obstack))
9671               return;
9672           }
9673       }
9674     while (field != NULL_TREE);
9675 }
9676 \f
9677 /* Add a new initializer to the tree of pending initializers.  PURPOSE
9678    identifies the initializer, either array index or field in a structure.
9679    VALUE is the value of that index or field.  If ORIGTYPE is not
9680    NULL_TREE, it is the original type of VALUE.
9681
9682    IMPLICIT is true if value comes from pop_init_level (1),
9683    the new initializer has been merged with the existing one
9684    and thus no warnings should be emitted about overriding an
9685    existing initializer.  */
9686
9687 static void
9688 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9689                   bool implicit, struct obstack *braced_init_obstack)
9690 {
9691   struct init_node *p, **q, *r;
9692
9693   q = &constructor_pending_elts;
9694   p = 0;
9695
9696   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9697     {
9698       while (*q != 0)
9699         {
9700           p = *q;
9701           if (tree_int_cst_lt (purpose, p->purpose))
9702             q = &p->left;
9703           else if (tree_int_cst_lt (p->purpose, purpose))
9704             q = &p->right;
9705           else
9706             {
9707               if (!implicit)
9708                 {
9709                   if (TREE_SIDE_EFFECTS (p->value))
9710                     warning_init (loc, OPT_Woverride_init_side_effects,
9711                                   "initialized field with side-effects "
9712                                   "overwritten");
9713                   else if (warn_override_init)
9714                     warning_init (loc, OPT_Woverride_init,
9715                                   "initialized field overwritten");
9716                 }
9717               p->value = value;
9718               p->origtype = origtype;
9719               return;
9720             }
9721         }
9722     }
9723   else
9724     {
9725       tree bitpos;
9726
9727       bitpos = bit_position (purpose);
9728       while (*q != NULL)
9729         {
9730           p = *q;
9731           if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9732             q = &p->left;
9733           else if (p->purpose != purpose)
9734             q = &p->right;
9735           else
9736             {
9737               if (!implicit)
9738                 {
9739                   if (TREE_SIDE_EFFECTS (p->value))
9740                     warning_init (loc, OPT_Woverride_init_side_effects,
9741                                   "initialized field with side-effects "
9742                                   "overwritten");
9743                   else if (warn_override_init)
9744                     warning_init (loc, OPT_Woverride_init,
9745                                   "initialized field overwritten");
9746                 }
9747               p->value = value;
9748               p->origtype = origtype;
9749               return;
9750             }
9751         }
9752     }
9753
9754   r = (struct init_node *) obstack_alloc (braced_init_obstack,
9755                                           sizeof (struct init_node));
9756   r->purpose = purpose;
9757   r->value = value;
9758   r->origtype = origtype;
9759
9760   *q = r;
9761   r->parent = p;
9762   r->left = 0;
9763   r->right = 0;
9764   r->balance = 0;
9765
9766   while (p)
9767     {
9768       struct init_node *s;
9769
9770       if (r == p->left)
9771         {
9772           if (p->balance == 0)
9773             p->balance = -1;
9774           else if (p->balance < 0)
9775             {
9776               if (r->balance < 0)
9777                 {
9778                   /* L rotation.  */
9779                   p->left = r->right;
9780                   if (p->left)
9781                     p->left->parent = p;
9782                   r->right = p;
9783
9784                   p->balance = 0;
9785                   r->balance = 0;
9786
9787                   s = p->parent;
9788                   p->parent = r;
9789                   r->parent = s;
9790                   if (s)
9791                     {
9792                       if (s->left == p)
9793                         s->left = r;
9794                       else
9795                         s->right = r;
9796                     }
9797                   else
9798                     constructor_pending_elts = r;
9799                 }
9800               else
9801                 {
9802                   /* LR rotation.  */
9803                   struct init_node *t = r->right;
9804
9805                   r->right = t->left;
9806                   if (r->right)
9807                     r->right->parent = r;
9808                   t->left = r;
9809
9810                   p->left = t->right;
9811                   if (p->left)
9812                     p->left->parent = p;
9813                   t->right = p;
9814
9815                   p->balance = t->balance < 0;
9816                   r->balance = -(t->balance > 0);
9817                   t->balance = 0;
9818
9819                   s = p->parent;
9820                   p->parent = t;
9821                   r->parent = t;
9822                   t->parent = s;
9823                   if (s)
9824                     {
9825                       if (s->left == p)
9826                         s->left = t;
9827                       else
9828                         s->right = t;
9829                     }
9830                   else
9831                     constructor_pending_elts = t;
9832                 }
9833               break;
9834             }
9835           else
9836             {
9837               /* p->balance == +1; growth of left side balances the node.  */
9838               p->balance = 0;
9839               break;
9840             }
9841         }
9842       else /* r == p->right */
9843         {
9844           if (p->balance == 0)
9845             /* Growth propagation from right side.  */
9846             p->balance++;
9847           else if (p->balance > 0)
9848             {
9849               if (r->balance > 0)
9850                 {
9851                   /* R rotation.  */
9852                   p->right = r->left;
9853                   if (p->right)
9854                     p->right->parent = p;
9855                   r->left = p;
9856
9857                   p->balance = 0;
9858                   r->balance = 0;
9859
9860                   s = p->parent;
9861                   p->parent = r;
9862                   r->parent = s;
9863                   if (s)
9864                     {
9865                       if (s->left == p)
9866                         s->left = r;
9867                       else
9868                         s->right = r;
9869                     }
9870                   else
9871                     constructor_pending_elts = r;
9872                 }
9873               else /* r->balance == -1 */
9874                 {
9875                   /* RL rotation */
9876                   struct init_node *t = r->left;
9877
9878                   r->left = t->right;
9879                   if (r->left)
9880                     r->left->parent = r;
9881                   t->right = r;
9882
9883                   p->right = t->left;
9884                   if (p->right)
9885                     p->right->parent = p;
9886                   t->left = p;
9887
9888                   r->balance = (t->balance < 0);
9889                   p->balance = -(t->balance > 0);
9890                   t->balance = 0;
9891
9892                   s = p->parent;
9893                   p->parent = t;
9894                   r->parent = t;
9895                   t->parent = s;
9896                   if (s)
9897                     {
9898                       if (s->left == p)
9899                         s->left = t;
9900                       else
9901                         s->right = t;
9902                     }
9903                   else
9904                     constructor_pending_elts = t;
9905                 }
9906               break;
9907             }
9908           else
9909             {
9910               /* p->balance == -1; growth of right side balances the node.  */
9911               p->balance = 0;
9912               break;
9913             }
9914         }
9915
9916       r = p;
9917       p = p->parent;
9918     }
9919 }
9920
9921 /* Build AVL tree from a sorted chain.  */
9922
9923 static void
9924 set_nonincremental_init (struct obstack * braced_init_obstack)
9925 {
9926   unsigned HOST_WIDE_INT ix;
9927   tree index, value;
9928
9929   if (TREE_CODE (constructor_type) != RECORD_TYPE
9930       && TREE_CODE (constructor_type) != ARRAY_TYPE)
9931     return;
9932
9933   FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9934     add_pending_init (input_location, index, value, NULL_TREE, true,
9935                       braced_init_obstack);
9936   constructor_elements = NULL;
9937   if (TREE_CODE (constructor_type) == RECORD_TYPE)
9938     {
9939       constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9940       /* Skip any nameless bit fields at the beginning.  */
9941       while (constructor_unfilled_fields != NULL_TREE
9942              && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9943         constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9944
9945     }
9946   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9947     {
9948       if (TYPE_DOMAIN (constructor_type))
9949         constructor_unfilled_index
9950             = convert (bitsizetype,
9951                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9952       else
9953         constructor_unfilled_index = bitsize_zero_node;
9954     }
9955   constructor_incremental = 0;
9956 }
9957
9958 /* Build AVL tree from a string constant.  */
9959
9960 static void
9961 set_nonincremental_init_from_string (tree str,
9962                                      struct obstack * braced_init_obstack)
9963 {
9964   tree value, purpose, type;
9965   HOST_WIDE_INT val[2];
9966   const char *p, *end;
9967   int byte, wchar_bytes, charwidth, bitpos;
9968
9969   gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9970
9971   wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9972   charwidth = TYPE_PRECISION (char_type_node);
9973   gcc_assert ((size_t) wchar_bytes * charwidth
9974               <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9975   type = TREE_TYPE (constructor_type);
9976   p = TREE_STRING_POINTER (str);
9977   end = p + TREE_STRING_LENGTH (str);
9978
9979   for (purpose = bitsize_zero_node;
9980        p < end
9981        && !(constructor_max_index
9982             && tree_int_cst_lt (constructor_max_index, purpose));
9983        purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9984     {
9985       if (wchar_bytes == 1)
9986         {
9987           val[0] = (unsigned char) *p++;
9988           val[1] = 0;
9989         }
9990       else
9991         {
9992           val[1] = 0;
9993           val[0] = 0;
9994           for (byte = 0; byte < wchar_bytes; byte++)
9995             {
9996               if (BYTES_BIG_ENDIAN)
9997                 bitpos = (wchar_bytes - byte - 1) * charwidth;
9998               else
9999                 bitpos = byte * charwidth;
10000               val[bitpos / HOST_BITS_PER_WIDE_INT]
10001                 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
10002                    << (bitpos % HOST_BITS_PER_WIDE_INT);
10003             }
10004         }
10005
10006       if (!TYPE_UNSIGNED (type))
10007         {
10008           bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
10009           if (bitpos < HOST_BITS_PER_WIDE_INT)
10010             {
10011               if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
10012                 {
10013                   val[0] |= HOST_WIDE_INT_M1U << bitpos;
10014                   val[1] = -1;
10015                 }
10016             }
10017           else if (bitpos == HOST_BITS_PER_WIDE_INT)
10018             {
10019               if (val[0] < 0)
10020                 val[1] = -1;
10021             }
10022           else if (val[1] & (HOST_WIDE_INT_1
10023                              << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
10024             val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
10025         }
10026
10027       value = wide_int_to_tree (type,
10028                                 wide_int::from_array (val, 2,
10029                                                       HOST_BITS_PER_WIDE_INT * 2));
10030       add_pending_init (input_location, purpose, value, NULL_TREE, true,
10031                         braced_init_obstack);
10032     }
10033
10034   constructor_incremental = 0;
10035 }
10036
10037 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
10038    not initialized yet.  */
10039
10040 static tree
10041 find_init_member (tree field, struct obstack * braced_init_obstack)
10042 {
10043   struct init_node *p;
10044
10045   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10046     {
10047       if (constructor_incremental
10048           && tree_int_cst_lt (field, constructor_unfilled_index))
10049         set_nonincremental_init (braced_init_obstack);
10050
10051       p = constructor_pending_elts;
10052       while (p)
10053         {
10054           if (tree_int_cst_lt (field, p->purpose))
10055             p = p->left;
10056           else if (tree_int_cst_lt (p->purpose, field))
10057             p = p->right;
10058           else
10059             return p->value;
10060         }
10061     }
10062   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10063     {
10064       tree bitpos = bit_position (field);
10065
10066       if (constructor_incremental
10067           && (!constructor_unfilled_fields
10068               || tree_int_cst_lt (bitpos,
10069                                   bit_position (constructor_unfilled_fields))))
10070         set_nonincremental_init (braced_init_obstack);
10071
10072       p = constructor_pending_elts;
10073       while (p)
10074         {
10075           if (field == p->purpose)
10076             return p->value;
10077           else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
10078             p = p->left;
10079           else
10080             p = p->right;
10081         }
10082     }
10083   else if (TREE_CODE (constructor_type) == UNION_TYPE)
10084     {
10085       if (!vec_safe_is_empty (constructor_elements)
10086           && (constructor_elements->last ().index == field))
10087         return constructor_elements->last ().value;
10088     }
10089   return NULL_TREE;
10090 }
10091
10092 /* "Output" the next constructor element.
10093    At top level, really output it to assembler code now.
10094    Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
10095    If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
10096    TYPE is the data type that the containing data type wants here.
10097    FIELD is the field (a FIELD_DECL) or the index that this element fills.
10098    If VALUE is a string constant, STRICT_STRING is true if it is
10099    unparenthesized or we should not warn here for it being parenthesized.
10100    For other types of VALUE, STRICT_STRING is not used.
10101
10102    PENDING if true means output pending elements that belong
10103    right after this element.  (PENDING is normally true;
10104    it is false while outputting pending elements, to avoid recursion.)
10105
10106    IMPLICIT is true if value comes from pop_init_level (1),
10107    the new initializer has been merged with the existing one
10108    and thus no warnings should be emitted about overriding an
10109    existing initializer.  */
10110
10111 static void
10112 output_init_element (location_t loc, tree value, tree origtype,
10113                      bool strict_string, tree type, tree field, bool pending,
10114                      bool implicit, struct obstack * braced_init_obstack)
10115 {
10116   tree semantic_type = NULL_TREE;
10117   bool maybe_const = true;
10118   bool npc, int_const_expr, arith_const_expr;
10119
10120   if (type == error_mark_node || value == error_mark_node)
10121     {
10122       constructor_erroneous = 1;
10123       return;
10124     }
10125   if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
10126       && (TREE_CODE (value) == STRING_CST
10127           || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
10128       && !(TREE_CODE (value) == STRING_CST
10129            && TREE_CODE (type) == ARRAY_TYPE
10130            && INTEGRAL_TYPE_P (TREE_TYPE (type)))
10131       && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
10132                      TYPE_MAIN_VARIANT (type)))
10133     value = array_to_pointer_conversion (input_location, value);
10134
10135   if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
10136       && require_constant_value && pending)
10137     {
10138       /* As an extension, allow initializing objects with static storage
10139          duration with compound literals (which are then treated just as
10140          the brace enclosed list they contain).  */
10141       if (flag_isoc99)
10142         pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
10143                       "constant");
10144       tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
10145       value = DECL_INITIAL (decl);
10146     }
10147
10148   npc = null_pointer_constant_p (value);
10149   int_const_expr = (TREE_CODE (value) == INTEGER_CST
10150                     && !TREE_OVERFLOW (value)
10151                     && INTEGRAL_TYPE_P (TREE_TYPE (value)));
10152   /* Not fully determined before folding.  */
10153   arith_const_expr = true;
10154   if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
10155     {
10156       semantic_type = TREE_TYPE (value);
10157       value = TREE_OPERAND (value, 0);
10158     }
10159   value = c_fully_fold (value, require_constant_value, &maybe_const);
10160   /* TODO: this may not detect all cases of expressions folding to
10161      constants that are not arithmetic constant expressions.  */
10162   if (!maybe_const)
10163     arith_const_expr = false;
10164   else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
10165       && TREE_CODE (TREE_TYPE (value)) != REAL_TYPE
10166       && TREE_CODE (TREE_TYPE (value)) != COMPLEX_TYPE)
10167     arith_const_expr = false;
10168   else if (TREE_CODE (value) != INTEGER_CST
10169       && TREE_CODE (value) != REAL_CST
10170       && TREE_CODE (value) != COMPLEX_CST)
10171     arith_const_expr = false;
10172   else if (TREE_OVERFLOW (value))
10173     arith_const_expr = false;
10174
10175   if (value == error_mark_node)
10176     constructor_erroneous = 1;
10177   else if (!TREE_CONSTANT (value))
10178     constructor_constant = 0;
10179   else if (!initializer_constant_valid_p (value,
10180                                           TREE_TYPE (value),
10181                                           AGGREGATE_TYPE_P (constructor_type)
10182                                           && TYPE_REVERSE_STORAGE_ORDER
10183                                              (constructor_type))
10184            || (RECORD_OR_UNION_TYPE_P (constructor_type)
10185                && DECL_C_BIT_FIELD (field)
10186                && TREE_CODE (value) != INTEGER_CST))
10187     constructor_simple = 0;
10188   if (!maybe_const)
10189     constructor_nonconst = 1;
10190
10191   /* Digest the initializer and issue any errors about incompatible
10192      types before issuing errors about non-constant initializers.  */
10193   tree new_value = value;
10194   if (semantic_type)
10195     new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
10196   /* In the case of braces around a scalar initializer, the result of
10197      this initializer processing goes through digest_init again at the
10198      outer level.  In the case of a constexpr initializer for a
10199      pointer, avoid converting a null pointer constant to something
10200      that is not a null pointer constant to avoid a spurious error
10201      from that second processing.  */
10202   if (!require_constexpr_value
10203       || !npc
10204       || TREE_CODE (constructor_type) != POINTER_TYPE)
10205     new_value = digest_init (loc, type, new_value, origtype, npc,
10206                              int_const_expr, arith_const_expr, strict_string,
10207                              require_constant_value, require_constexpr_value);
10208   if (new_value == error_mark_node)
10209     {
10210       constructor_erroneous = 1;
10211       return;
10212     }
10213   if (require_constant_value || require_constant_elements)
10214     constant_expression_warning (new_value);
10215
10216   /* Proceed to check the constness of the original initializer.  */
10217   if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
10218     {
10219       if (require_constant_value)
10220         {
10221           error_init (loc, "initializer element is not constant");
10222           value = error_mark_node;
10223         }
10224       else if (require_constant_elements)
10225         pedwarn (loc, OPT_Wpedantic,
10226                  "initializer element is not computable at load time");
10227     }
10228   else if (!maybe_const
10229            && (require_constant_value || require_constant_elements))
10230     pedwarn_init (loc, OPT_Wpedantic,
10231                   "initializer element is not a constant expression");
10232   /* digest_init has already carried out the additional checks
10233      required for 'constexpr' initializers (using the information
10234      passed to it about whether the original initializer was certain
10235      kinds of constant expression), so that check does not need to be
10236      repeated here.  */
10237
10238   /* Issue -Wc++-compat warnings about initializing a bitfield with
10239      enum type.  */
10240   if (warn_cxx_compat
10241       && field != NULL_TREE
10242       && TREE_CODE (field) == FIELD_DECL
10243       && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
10244       && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
10245           != TYPE_MAIN_VARIANT (type))
10246       && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
10247     {
10248       tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
10249       if (checktype != error_mark_node
10250           && (TYPE_MAIN_VARIANT (checktype)
10251               != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
10252         warning_init (loc, OPT_Wc___compat,
10253                       "enum conversion in initialization is invalid in C++");
10254     }
10255
10256   /* If this field is empty and does not have side effects (and is not at
10257      the end of structure), don't do anything other than checking the
10258      initializer.  */
10259   if (field
10260       && (TREE_TYPE (field) == error_mark_node
10261           || (COMPLETE_TYPE_P (TREE_TYPE (field))
10262               && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
10263               && !TREE_SIDE_EFFECTS (new_value)
10264               && (TREE_CODE (constructor_type) == ARRAY_TYPE
10265                   || DECL_CHAIN (field)))))
10266     return;
10267
10268   /* Finally, set VALUE to the initializer value digested above.  */
10269   value = new_value;
10270
10271   /* If this element doesn't come next in sequence,
10272      put it on constructor_pending_elts.  */
10273   if (TREE_CODE (constructor_type) == ARRAY_TYPE
10274       && (!constructor_incremental
10275           || !tree_int_cst_equal (field, constructor_unfilled_index)))
10276     {
10277       if (constructor_incremental
10278           && tree_int_cst_lt (field, constructor_unfilled_index))
10279         set_nonincremental_init (braced_init_obstack);
10280
10281       add_pending_init (loc, field, value, origtype, implicit,
10282                         braced_init_obstack);
10283       return;
10284     }
10285   else if (TREE_CODE (constructor_type) == RECORD_TYPE
10286            && (!constructor_incremental
10287                || field != constructor_unfilled_fields))
10288     {
10289       /* We do this for records but not for unions.  In a union,
10290          no matter which field is specified, it can be initialized
10291          right away since it starts at the beginning of the union.  */
10292       if (constructor_incremental)
10293         {
10294           if (!constructor_unfilled_fields)
10295             set_nonincremental_init (braced_init_obstack);
10296           else
10297             {
10298               tree bitpos, unfillpos;
10299
10300               bitpos = bit_position (field);
10301               unfillpos = bit_position (constructor_unfilled_fields);
10302
10303               if (tree_int_cst_lt (bitpos, unfillpos))
10304                 set_nonincremental_init (braced_init_obstack);
10305             }
10306         }
10307
10308       add_pending_init (loc, field, value, origtype, implicit,
10309                         braced_init_obstack);
10310       return;
10311     }
10312   else if (TREE_CODE (constructor_type) == UNION_TYPE
10313            && !vec_safe_is_empty (constructor_elements))
10314     {
10315       if (!implicit)
10316         {
10317           if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
10318             warning_init (loc, OPT_Woverride_init_side_effects,
10319                           "initialized field with side-effects overwritten");
10320           else if (warn_override_init)
10321             warning_init (loc, OPT_Woverride_init,
10322                           "initialized field overwritten");
10323         }
10324
10325       /* We can have just one union field set.  */
10326       constructor_elements = NULL;
10327     }
10328
10329   /* Otherwise, output this element either to
10330      constructor_elements or to the assembler file.  */
10331
10332   constructor_elt celt = {field, value};
10333   vec_safe_push (constructor_elements, celt);
10334
10335   /* Advance the variable that indicates sequential elements output.  */
10336   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10337     constructor_unfilled_index
10338       = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
10339                         bitsize_one_node);
10340   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
10341     {
10342       constructor_unfilled_fields
10343         = DECL_CHAIN (constructor_unfilled_fields);
10344
10345       /* Skip any nameless bit fields.  */
10346       while (constructor_unfilled_fields != NULL_TREE
10347              && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
10348         constructor_unfilled_fields =
10349           DECL_CHAIN (constructor_unfilled_fields);
10350     }
10351   else if (TREE_CODE (constructor_type) == UNION_TYPE)
10352     constructor_unfilled_fields = NULL_TREE;
10353
10354   /* Now output any pending elements which have become next.  */
10355   if (pending)
10356     output_pending_init_elements (0, braced_init_obstack);
10357 }
10358
10359 /* For two FIELD_DECLs in the same chain, return -1 if field1
10360    comes before field2, 1 if field1 comes after field2 and
10361    0 if field1 == field2.  */
10362
10363 static int
10364 init_field_decl_cmp (tree field1, tree field2)
10365 {
10366   if (field1 == field2)
10367     return 0;
10368
10369   tree bitpos1 = bit_position (field1);
10370   tree bitpos2 = bit_position (field2);
10371   if (tree_int_cst_equal (bitpos1, bitpos2))
10372     {
10373       /* If one of the fields has non-zero bitsize, then that
10374          field must be the last one in a sequence of zero
10375          sized fields, fields after it will have bigger
10376          bit_position.  */
10377       if (TREE_TYPE (field1) != error_mark_node
10378           && COMPLETE_TYPE_P (TREE_TYPE (field1))
10379           && integer_nonzerop (TREE_TYPE (field1)))
10380         return 1;
10381       if (TREE_TYPE (field2) != error_mark_node
10382           && COMPLETE_TYPE_P (TREE_TYPE (field2))
10383           && integer_nonzerop (TREE_TYPE (field2)))
10384         return -1;
10385       /* Otherwise, fallback to DECL_CHAIN walk to find out
10386          which field comes earlier.  Walk chains of both
10387          fields, so that if field1 and field2 are close to each
10388          other in either order, it is found soon even for large
10389          sequences of zero sized fields.  */
10390       tree f1 = field1, f2 = field2;
10391       while (1)
10392         {
10393           f1 = DECL_CHAIN (f1);
10394           f2 = DECL_CHAIN (f2);
10395           if (f1 == NULL_TREE)
10396             {
10397               gcc_assert (f2);
10398               return 1;
10399             }
10400           if (f2 == NULL_TREE)
10401             return -1;
10402           if (f1 == field2)
10403             return -1;
10404           if (f2 == field1)
10405             return 1;
10406           if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10407             return 1;
10408           if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10409             return -1;
10410         }
10411     }
10412   else if (tree_int_cst_lt (bitpos1, bitpos2))
10413     return -1;
10414   else
10415     return 1;
10416 }
10417
10418 /* Output any pending elements which have become next.
10419    As we output elements, constructor_unfilled_{fields,index}
10420    advances, which may cause other elements to become next;
10421    if so, they too are output.
10422
10423    If ALL is 0, we return when there are
10424    no more pending elements to output now.
10425
10426    If ALL is 1, we output space as necessary so that
10427    we can output all the pending elements.  */
10428 static void
10429 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10430 {
10431   struct init_node *elt = constructor_pending_elts;
10432   tree next;
10433
10434  retry:
10435
10436   /* Look through the whole pending tree.
10437      If we find an element that should be output now,
10438      output it.  Otherwise, set NEXT to the element
10439      that comes first among those still pending.  */
10440
10441   next = NULL_TREE;
10442   while (elt)
10443     {
10444       if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10445         {
10446           if (tree_int_cst_equal (elt->purpose,
10447                                   constructor_unfilled_index))
10448             output_init_element (input_location, elt->value, elt->origtype,
10449                                  true, TREE_TYPE (constructor_type),
10450                                  constructor_unfilled_index, false, false,
10451                                  braced_init_obstack);
10452           else if (tree_int_cst_lt (constructor_unfilled_index,
10453                                     elt->purpose))
10454             {
10455               /* Advance to the next smaller node.  */
10456               if (elt->left)
10457                 elt = elt->left;
10458               else
10459                 {
10460                   /* We have reached the smallest node bigger than the
10461                      current unfilled index.  Fill the space first.  */
10462                   next = elt->purpose;
10463                   break;
10464                 }
10465             }
10466           else
10467             {
10468               /* Advance to the next bigger node.  */
10469               if (elt->right)
10470                 elt = elt->right;
10471               else
10472                 {
10473                   /* We have reached the biggest node in a subtree.  Find
10474                      the parent of it, which is the next bigger node.  */
10475                   while (elt->parent && elt->parent->right == elt)
10476                     elt = elt->parent;
10477                   elt = elt->parent;
10478                   if (elt && tree_int_cst_lt (constructor_unfilled_index,
10479                                               elt->purpose))
10480                     {
10481                       next = elt->purpose;
10482                       break;
10483                     }
10484                 }
10485             }
10486         }
10487       else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10488         {
10489           /* If the current record is complete we are done.  */
10490           if (constructor_unfilled_fields == NULL_TREE)
10491             break;
10492
10493           int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10494                                          elt->purpose);
10495           if (cmp == 0)
10496             output_init_element (input_location, elt->value, elt->origtype,
10497                                  true, TREE_TYPE (elt->purpose),
10498                                  elt->purpose, false, false,
10499                                  braced_init_obstack);
10500           else if (cmp < 0)
10501             {
10502               /* Advance to the next smaller node.  */
10503               if (elt->left)
10504                 elt = elt->left;
10505               else
10506                 {
10507                   /* We have reached the smallest node bigger than the
10508                      current unfilled field.  Fill the space first.  */
10509                   next = elt->purpose;
10510                   break;
10511                 }
10512             }
10513           else
10514             {
10515               /* Advance to the next bigger node.  */
10516               if (elt->right)
10517                 elt = elt->right;
10518               else
10519                 {
10520                   /* We have reached the biggest node in a subtree.  Find
10521                      the parent of it, which is the next bigger node.  */
10522                   while (elt->parent && elt->parent->right == elt)
10523                     elt = elt->parent;
10524                   elt = elt->parent;
10525                   if (elt
10526                       && init_field_decl_cmp (constructor_unfilled_fields,
10527                                               elt->purpose) < 0)
10528                     {
10529                       next = elt->purpose;
10530                       break;
10531                     }
10532                 }
10533             }
10534         }
10535     }
10536
10537   /* Ordinarily return, but not if we want to output all
10538      and there are elements left.  */
10539   if (!(all && next != NULL_TREE))
10540     return;
10541
10542   /* If it's not incremental, just skip over the gap, so that after
10543      jumping to retry we will output the next successive element.  */
10544   if (RECORD_OR_UNION_TYPE_P (constructor_type))
10545     constructor_unfilled_fields = next;
10546   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10547     constructor_unfilled_index = next;
10548
10549   /* ELT now points to the node in the pending tree with the next
10550      initializer to output.  */
10551   goto retry;
10552 }
10553 \f
10554 /* Expression VALUE coincides with the start of type TYPE in a braced
10555    initializer.  Return true if we should treat VALUE as initializing
10556    the first element of TYPE, false if we should treat it as initializing
10557    TYPE as a whole.
10558
10559    If the initializer is clearly invalid, the question becomes:
10560    which choice gives the best error message?  */
10561
10562 static bool
10563 initialize_elementwise_p (tree type, tree value)
10564 {
10565   if (type == error_mark_node || value == error_mark_node)
10566     return false;
10567
10568   gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10569
10570   tree value_type = TREE_TYPE (value);
10571   if (value_type == error_mark_node)
10572     return false;
10573
10574   /* GNU vectors can be initialized elementwise.  However, treat any
10575      kind of vector value as initializing the vector type as a whole,
10576      regardless of whether the value is a GNU vector.  Such initializers
10577      are valid if and only if they would have been valid in a non-braced
10578      initializer like:
10579
10580         TYPE foo = VALUE;
10581
10582      so recursing into the vector type would be at best confusing or at
10583      worst wrong.  For example, when -flax-vector-conversions is in effect,
10584      it's possible to initialize a V8HI from a V4SI, even though the vectors
10585      have different element types and different numbers of elements.  */
10586   if (gnu_vector_type_p (type))
10587     return !VECTOR_TYPE_P (value_type);
10588
10589   if (AGGREGATE_TYPE_P (type))
10590     return type != TYPE_MAIN_VARIANT (value_type);
10591
10592   return false;
10593 }
10594
10595 /* Add one non-braced element to the current constructor level.
10596    This adjusts the current position within the constructor's type.
10597    This may also start or terminate implicit levels
10598    to handle a partly-braced initializer.
10599
10600    Once this has found the correct level for the new element,
10601    it calls output_init_element.
10602
10603    IMPLICIT is true if value comes from pop_init_level (1),
10604    the new initializer has been merged with the existing one
10605    and thus no warnings should be emitted about overriding an
10606    existing initializer.  */
10607
10608 void
10609 process_init_element (location_t loc, struct c_expr value, bool implicit,
10610                       struct obstack * braced_init_obstack)
10611 {
10612   tree orig_value = value.value;
10613   int string_flag
10614     = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10615   bool strict_string = value.original_code == STRING_CST;
10616   bool was_designated = designator_depth != 0;
10617
10618   designator_depth = 0;
10619   designator_erroneous = 0;
10620
10621   if (!implicit && value.value && !integer_zerop (value.value))
10622     constructor_zeroinit = 0;
10623
10624   /* Handle superfluous braces around string cst as in
10625      char x[] = {"foo"}; */
10626   if (string_flag
10627       && constructor_type
10628       && !was_designated
10629       && TREE_CODE (constructor_type) == ARRAY_TYPE
10630       && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10631       && integer_zerop (constructor_unfilled_index))
10632     {
10633       if (constructor_stack->replacement_value.value)
10634         error_init (loc, "excess elements in %<char%> array initializer");
10635       constructor_stack->replacement_value = value;
10636       return;
10637     }
10638
10639   if (constructor_stack->replacement_value.value != NULL_TREE)
10640     {
10641       error_init (loc, "excess elements in struct initializer");
10642       return;
10643     }
10644
10645   /* Ignore elements of a brace group if it is entirely superfluous
10646      and has already been diagnosed, or if the type is erroneous.  */
10647   if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10648     return;
10649
10650   /* Ignore elements of an initializer for a variable-size type.
10651      Those are diagnosed in the parser (empty initializer braces are OK).  */
10652   if (COMPLETE_TYPE_P (constructor_type)
10653       && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10654     return;
10655
10656   if (!implicit && warn_designated_init && !was_designated
10657       && TREE_CODE (constructor_type) == RECORD_TYPE
10658       && lookup_attribute ("designated_init",
10659                            TYPE_ATTRIBUTES (constructor_type)))
10660     warning_init (loc,
10661                   OPT_Wdesignated_init,
10662                   "positional initialization of field "
10663                   "in %<struct%> declared with %<designated_init%> attribute");
10664
10665   /* If we've exhausted any levels that didn't have braces,
10666      pop them now.  */
10667   while (constructor_stack->implicit)
10668     {
10669       if (RECORD_OR_UNION_TYPE_P (constructor_type)
10670           && constructor_fields == NULL_TREE)
10671         process_init_element (loc,
10672                               pop_init_level (loc, 1, braced_init_obstack,
10673                                               last_init_list_comma),
10674                               true, braced_init_obstack);
10675       else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10676                 || gnu_vector_type_p (constructor_type))
10677                && constructor_max_index
10678                && tree_int_cst_lt (constructor_max_index,
10679                                    constructor_index))
10680         process_init_element (loc,
10681                               pop_init_level (loc, 1, braced_init_obstack,
10682                                               last_init_list_comma),
10683                               true, braced_init_obstack);
10684       else
10685         break;
10686     }
10687
10688   /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once.  */
10689   if (constructor_range_stack)
10690     {
10691       /* If value is a compound literal and we'll be just using its
10692          content, don't put it into a SAVE_EXPR.  */
10693       if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10694           || !require_constant_value)
10695         {
10696           tree semantic_type = NULL_TREE;
10697           if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10698             {
10699               semantic_type = TREE_TYPE (value.value);
10700               value.value = TREE_OPERAND (value.value, 0);
10701             }
10702           value.value = save_expr (value.value);
10703           if (semantic_type)
10704             value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10705                                   value.value);
10706         }
10707     }
10708
10709   while (1)
10710     {
10711       if (TREE_CODE (constructor_type) == RECORD_TYPE)
10712         {
10713           tree fieldtype;
10714           enum tree_code fieldcode;
10715
10716           if (constructor_fields == NULL_TREE)
10717             {
10718               pedwarn_init (loc, 0, "excess elements in struct initializer");
10719               break;
10720             }
10721
10722           fieldtype = TREE_TYPE (constructor_fields);
10723           if (fieldtype != error_mark_node)
10724             fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10725           fieldcode = TREE_CODE (fieldtype);
10726
10727           /* Error for non-static initialization of a flexible array member.  */
10728           if (fieldcode == ARRAY_TYPE
10729               && !require_constant_value
10730               && TYPE_SIZE (fieldtype) == NULL_TREE
10731               && DECL_CHAIN (constructor_fields) == NULL_TREE)
10732             {
10733               error_init (loc, "non-static initialization of a flexible "
10734                           "array member");
10735               break;
10736             }
10737
10738           /* Error for initialization of a flexible array member with
10739              a string constant if the structure is in an array.  E.g.:
10740              struct S { int x; char y[]; };
10741              struct S s[] = { { 1, "foo" } };
10742              is invalid.  */
10743           if (string_flag
10744               && fieldcode == ARRAY_TYPE
10745               && constructor_depth > 1
10746               && TYPE_SIZE (fieldtype) == NULL_TREE
10747               && DECL_CHAIN (constructor_fields) == NULL_TREE)
10748             {
10749               bool in_array_p = false;
10750               for (struct constructor_stack *p = constructor_stack;
10751                    p && p->type; p = p->next)
10752                 if (TREE_CODE (p->type) == ARRAY_TYPE)
10753                   {
10754                     in_array_p = true;
10755                     break;
10756                   }
10757               if (in_array_p)
10758                 {
10759                   error_init (loc, "initialization of flexible array "
10760                               "member in a nested context");
10761                   break;
10762                 }
10763             }
10764
10765           /* Accept a string constant to initialize a subarray.  */
10766           if (value.value != NULL_TREE
10767               && fieldcode == ARRAY_TYPE
10768               && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10769               && string_flag)
10770             value.value = orig_value;
10771           /* Otherwise, if we have come to a subaggregate,
10772              and we don't have an element of its type, push into it.  */
10773           else if (value.value != NULL_TREE
10774                    && initialize_elementwise_p (fieldtype, value.value))
10775             {
10776               push_init_level (loc, 1, braced_init_obstack);
10777               continue;
10778             }
10779
10780           if (value.value)
10781             {
10782               push_member_name (constructor_fields);
10783               output_init_element (loc, value.value, value.original_type,
10784                                    strict_string, fieldtype,
10785                                    constructor_fields, true, implicit,
10786                                    braced_init_obstack);
10787               RESTORE_SPELLING_DEPTH (constructor_depth);
10788             }
10789           else
10790             /* Do the bookkeeping for an element that was
10791                directly output as a constructor.  */
10792             {
10793               /* For a record, keep track of end position of last field.  */
10794               if (DECL_SIZE (constructor_fields))
10795                 constructor_bit_index
10796                   = size_binop_loc (input_location, PLUS_EXPR,
10797                                     bit_position (constructor_fields),
10798                                     DECL_SIZE (constructor_fields));
10799
10800               /* If the current field was the first one not yet written out,
10801                  it isn't now, so update.  */
10802               if (constructor_unfilled_fields == constructor_fields)
10803                 {
10804                   constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10805                   /* Skip any nameless bit fields.  */
10806                   while (constructor_unfilled_fields != 0
10807                          && (DECL_UNNAMED_BIT_FIELD
10808                              (constructor_unfilled_fields)))
10809                     constructor_unfilled_fields =
10810                       DECL_CHAIN (constructor_unfilled_fields);
10811                 }
10812             }
10813
10814           constructor_fields = DECL_CHAIN (constructor_fields);
10815           /* Skip any nameless bit fields at the beginning.  */
10816           while (constructor_fields != NULL_TREE
10817                  && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10818             constructor_fields = DECL_CHAIN (constructor_fields);
10819         }
10820       else if (TREE_CODE (constructor_type) == UNION_TYPE)
10821         {
10822           tree fieldtype;
10823           enum tree_code fieldcode;
10824
10825           if (constructor_fields == NULL_TREE)
10826             {
10827               pedwarn_init (loc, 0,
10828                             "excess elements in union initializer");
10829               break;
10830             }
10831
10832           fieldtype = TREE_TYPE (constructor_fields);
10833           if (fieldtype != error_mark_node)
10834             fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10835           fieldcode = TREE_CODE (fieldtype);
10836
10837           /* Warn that traditional C rejects initialization of unions.
10838              We skip the warning if the value is zero.  This is done
10839              under the assumption that the zero initializer in user
10840              code appears conditioned on e.g. __STDC__ to avoid
10841              "missing initializer" warnings and relies on default
10842              initialization to zero in the traditional C case.
10843              We also skip the warning if the initializer is designated,
10844              again on the assumption that this must be conditional on
10845              __STDC__ anyway (and we've already complained about the
10846              member-designator already).  */
10847           if (!in_system_header_at (input_location) && !constructor_designated
10848               && !(value.value && (integer_zerop (value.value)
10849                                    || real_zerop (value.value))))
10850             warning (OPT_Wtraditional, "traditional C rejects initialization "
10851                      "of unions");
10852
10853           /* Accept a string constant to initialize a subarray.  */
10854           if (value.value != NULL_TREE
10855               && fieldcode == ARRAY_TYPE
10856               && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10857               && string_flag)
10858             value.value = orig_value;
10859           /* Otherwise, if we have come to a subaggregate,
10860              and we don't have an element of its type, push into it.  */
10861           else if (value.value != NULL_TREE
10862                    && initialize_elementwise_p (fieldtype, value.value))
10863             {
10864               push_init_level (loc, 1, braced_init_obstack);
10865               continue;
10866             }
10867
10868           if (value.value)
10869             {
10870               push_member_name (constructor_fields);
10871               output_init_element (loc, value.value, value.original_type,
10872                                    strict_string, fieldtype,
10873                                    constructor_fields, true, implicit,
10874                                    braced_init_obstack);
10875               RESTORE_SPELLING_DEPTH (constructor_depth);
10876             }
10877           else
10878             /* Do the bookkeeping for an element that was
10879                directly output as a constructor.  */
10880             {
10881               constructor_bit_index = DECL_SIZE (constructor_fields);
10882               constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10883             }
10884
10885           constructor_fields = NULL_TREE;
10886         }
10887       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10888         {
10889           tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10890           enum tree_code eltcode = TREE_CODE (elttype);
10891
10892           /* Accept a string constant to initialize a subarray.  */
10893           if (value.value != NULL_TREE
10894               && eltcode == ARRAY_TYPE
10895               && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10896               && string_flag)
10897             value.value = orig_value;
10898           /* Otherwise, if we have come to a subaggregate,
10899              and we don't have an element of its type, push into it.  */
10900           else if (value.value != NULL_TREE
10901                    && initialize_elementwise_p (elttype, value.value))
10902             {
10903               push_init_level (loc, 1, braced_init_obstack);
10904               continue;
10905             }
10906
10907           if (constructor_max_index != NULL_TREE
10908               && (tree_int_cst_lt (constructor_max_index, constructor_index)
10909                   || integer_all_onesp (constructor_max_index)))
10910             {
10911               pedwarn_init (loc, 0,
10912                             "excess elements in array initializer");
10913               break;
10914             }
10915
10916           /* Now output the actual element.  */
10917           if (value.value)
10918             {
10919               push_array_bounds (tree_to_uhwi (constructor_index));
10920               output_init_element (loc, value.value, value.original_type,
10921                                    strict_string, elttype,
10922                                    constructor_index, true, implicit,
10923                                    braced_init_obstack);
10924               RESTORE_SPELLING_DEPTH (constructor_depth);
10925             }
10926
10927           constructor_index
10928             = size_binop_loc (input_location, PLUS_EXPR,
10929                               constructor_index, bitsize_one_node);
10930
10931           if (!value.value)
10932             /* If we are doing the bookkeeping for an element that was
10933                directly output as a constructor, we must update
10934                constructor_unfilled_index.  */
10935             constructor_unfilled_index = constructor_index;
10936         }
10937       else if (gnu_vector_type_p (constructor_type))
10938         {
10939           tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10940
10941          /* Do a basic check of initializer size.  Note that vectors
10942             always have a fixed size derived from their type.  */
10943           if (tree_int_cst_lt (constructor_max_index, constructor_index))
10944             {
10945               pedwarn_init (loc, 0,
10946                             "excess elements in vector initializer");
10947               break;
10948             }
10949
10950           /* Now output the actual element.  */
10951           if (value.value)
10952             {
10953               if (TREE_CODE (value.value) == VECTOR_CST)
10954                 elttype = TYPE_MAIN_VARIANT (constructor_type);
10955               output_init_element (loc, value.value, value.original_type,
10956                                    strict_string, elttype,
10957                                    constructor_index, true, implicit,
10958                                    braced_init_obstack);
10959             }
10960
10961           constructor_index
10962             = size_binop_loc (input_location,
10963                               PLUS_EXPR, constructor_index, bitsize_one_node);
10964
10965           if (!value.value)
10966             /* If we are doing the bookkeeping for an element that was
10967                directly output as a constructor, we must update
10968                constructor_unfilled_index.  */
10969             constructor_unfilled_index = constructor_index;
10970         }
10971
10972       /* Handle the sole element allowed in a braced initializer
10973          for a scalar variable.  */
10974       else if (constructor_type != error_mark_node
10975                && constructor_fields == NULL_TREE)
10976         {
10977           pedwarn_init (loc, 0,
10978                         "excess elements in scalar initializer");
10979           break;
10980         }
10981       else
10982         {
10983           if (value.value)
10984             output_init_element (loc, value.value, value.original_type,
10985                                  strict_string, constructor_type,
10986                                  NULL_TREE, true, implicit,
10987                                  braced_init_obstack);
10988           constructor_fields = NULL_TREE;
10989         }
10990
10991       /* Handle range initializers either at this level or anywhere higher
10992          in the designator stack.  */
10993       if (constructor_range_stack)
10994         {
10995           struct constructor_range_stack *p, *range_stack;
10996           int finish = 0;
10997
10998           range_stack = constructor_range_stack;
10999           constructor_range_stack = 0;
11000           while (constructor_stack != range_stack->stack)
11001             {
11002               gcc_assert (constructor_stack->implicit);
11003               process_init_element (loc,
11004                                     pop_init_level (loc, 1,
11005                                                     braced_init_obstack,
11006                                                     last_init_list_comma),
11007                                     true, braced_init_obstack);
11008             }
11009           for (p = range_stack;
11010                !p->range_end || tree_int_cst_equal (p->index, p->range_end);
11011                p = p->prev)
11012             {
11013               gcc_assert (constructor_stack->implicit);
11014               process_init_element (loc,
11015                                     pop_init_level (loc, 1,
11016                                                     braced_init_obstack,
11017                                                     last_init_list_comma),
11018                                     true, braced_init_obstack);
11019             }
11020
11021           p->index = size_binop_loc (input_location,
11022                                      PLUS_EXPR, p->index, bitsize_one_node);
11023           if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
11024             finish = 1;
11025
11026           while (1)
11027             {
11028               constructor_index = p->index;
11029               constructor_fields = p->fields;
11030               if (finish && p->range_end && p->index == p->range_start)
11031                 {
11032                   finish = 0;
11033                   p->prev = 0;
11034                 }
11035               p = p->next;
11036               if (!p)
11037                 break;
11038               finish_implicit_inits (loc, braced_init_obstack);
11039               push_init_level (loc, 2, braced_init_obstack);
11040               p->stack = constructor_stack;
11041               if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
11042                 p->index = p->range_start;
11043             }
11044
11045           if (!finish)
11046             constructor_range_stack = range_stack;
11047           continue;
11048         }
11049
11050       break;
11051     }
11052
11053   constructor_range_stack = 0;
11054 }
11055 \f
11056 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
11057    (guaranteed to be 'volatile' or null) and ARGS (represented using
11058    an ASM_EXPR node).  */
11059 tree
11060 build_asm_stmt (bool is_volatile, tree args)
11061 {
11062   if (is_volatile)
11063     ASM_VOLATILE_P (args) = 1;
11064   return add_stmt (args);
11065 }
11066
11067 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
11068    some INPUTS, and some CLOBBERS.  The latter three may be NULL.
11069    SIMPLE indicates whether there was anything at all after the
11070    string in the asm expression -- asm("blah") and asm("blah" : )
11071    are subtly different.  We use a ASM_EXPR node to represent this.
11072    LOC is the location of the asm, and IS_INLINE says whether this
11073    is asm inline.  */
11074 tree
11075 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
11076                 tree clobbers, tree labels, bool simple, bool is_inline)
11077 {
11078   tree tail;
11079   tree args;
11080   int i;
11081   const char *constraint;
11082   const char **oconstraints;
11083   bool allows_mem, allows_reg, is_inout;
11084   int ninputs, noutputs;
11085
11086   ninputs = list_length (inputs);
11087   noutputs = list_length (outputs);
11088   oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
11089
11090   string = resolve_asm_operand_names (string, outputs, inputs, labels);
11091
11092   /* Remove output conversions that change the type but not the mode.  */
11093   for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
11094     {
11095       tree output = TREE_VALUE (tail);
11096
11097       output = c_fully_fold (output, false, NULL, true);
11098
11099       /* ??? Really, this should not be here.  Users should be using a
11100          proper lvalue, dammit.  But there's a long history of using casts
11101          in the output operands.  In cases like longlong.h, this becomes a
11102          primitive form of typechecking -- if the cast can be removed, then
11103          the output operand had a type of the proper width; otherwise we'll
11104          get an error.  Gross, but ...  */
11105       STRIP_NOPS (output);
11106
11107       if (!lvalue_or_else (loc, output, lv_asm))
11108         output = error_mark_node;
11109
11110       if (output != error_mark_node
11111           && (TREE_READONLY (output)
11112               || TYPE_READONLY (TREE_TYPE (output))
11113               || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
11114                   && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
11115         readonly_error (loc, output, lv_asm);
11116
11117       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11118       oconstraints[i] = constraint;
11119
11120       if (parse_output_constraint (&constraint, i, ninputs, noutputs,
11121                                    &allows_mem, &allows_reg, &is_inout))
11122         {
11123           /* If the operand is going to end up in memory,
11124              mark it addressable.  */
11125           if (!allows_reg && !c_mark_addressable (output))
11126             output = error_mark_node;
11127           if (!(!allows_reg && allows_mem)
11128               && output != error_mark_node
11129               && VOID_TYPE_P (TREE_TYPE (output)))
11130             {
11131               error_at (loc, "invalid use of void expression");
11132               output = error_mark_node;
11133             }
11134         }
11135       else
11136         output = error_mark_node;
11137
11138       TREE_VALUE (tail) = output;
11139     }
11140
11141   for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
11142     {
11143       tree input;
11144
11145       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
11146       input = TREE_VALUE (tail);
11147
11148       if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
11149                                   oconstraints, &allows_mem, &allows_reg))
11150         {
11151           /* If the operand is going to end up in memory,
11152              mark it addressable.  */
11153           if (!allows_reg && allows_mem)
11154             {
11155               input = c_fully_fold (input, false, NULL, true);
11156
11157               /* Strip the nops as we allow this case.  FIXME, this really
11158                  should be rejected or made deprecated.  */
11159               STRIP_NOPS (input);
11160               if (!c_mark_addressable (input))
11161                 input = error_mark_node;
11162             }
11163           else
11164             {
11165               struct c_expr expr;
11166               memset (&expr, 0, sizeof (expr));
11167               expr.value = input;
11168               expr = convert_lvalue_to_rvalue (loc, expr, true, false);
11169               input = c_fully_fold (expr.value, false, NULL);
11170
11171               if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
11172                 {
11173                   error_at (loc, "invalid use of void expression");
11174                   input = error_mark_node;
11175                 }
11176             }
11177         }
11178       else
11179         input = error_mark_node;
11180
11181       TREE_VALUE (tail) = input;
11182     }
11183
11184   args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
11185
11186   /* asm statements without outputs, including simple ones, are treated
11187      as volatile.  */
11188   ASM_INPUT_P (args) = simple;
11189   ASM_VOLATILE_P (args) = (noutputs == 0);
11190   ASM_INLINE_P (args) = is_inline;
11191
11192   return args;
11193 }
11194 \f
11195 /* Generate a goto statement to LABEL.  LOC is the location of the
11196    GOTO.  */
11197
11198 tree
11199 c_finish_goto_label (location_t loc, tree label)
11200 {
11201   tree decl = lookup_label_for_goto (loc, label);
11202   if (!decl)
11203     return NULL_TREE;
11204   TREE_USED (decl) = 1;
11205   {
11206     add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
11207     tree t = build1 (GOTO_EXPR, void_type_node, decl);
11208     SET_EXPR_LOCATION (t, loc);
11209     return add_stmt (t);
11210   }
11211 }
11212
11213 /* Generate a computed goto statement to EXPR.  LOC is the location of
11214    the GOTO.  */
11215
11216 tree
11217 c_finish_goto_ptr (location_t loc, c_expr val)
11218 {
11219   tree expr = val.value;
11220   tree t;
11221   pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
11222   if (expr != error_mark_node
11223       && !POINTER_TYPE_P (TREE_TYPE (expr))
11224       && !null_pointer_constant_p (expr))
11225     {
11226       error_at (val.get_location (),
11227                 "computed goto must be pointer type");
11228       expr = build_zero_cst (ptr_type_node);
11229     }
11230   expr = c_fully_fold (expr, false, NULL);
11231   expr = convert (ptr_type_node, expr);
11232   t = build1 (GOTO_EXPR, void_type_node, expr);
11233   SET_EXPR_LOCATION (t, loc);
11234   return add_stmt (t);
11235 }
11236
11237 /* Generate a C `return' statement.  RETVAL is the expression for what
11238    to return, or a null pointer for `return;' with no value.  LOC is
11239    the location of the return statement, or the location of the expression,
11240    if the statement has any.  If ORIGTYPE is not NULL_TREE, it
11241    is the original type of RETVAL.  */
11242
11243 tree
11244 c_finish_return (location_t loc, tree retval, tree origtype)
11245 {
11246   tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
11247   bool no_warning = false;
11248   bool npc = false;
11249
11250   /* Use the expansion point to handle cases such as returning NULL
11251      in a function returning void.  */
11252   location_t xloc = expansion_point_location_if_in_system_header (loc);
11253
11254   if (TREE_THIS_VOLATILE (current_function_decl))
11255     warning_at (xloc, 0,
11256                 "function declared %<noreturn%> has a %<return%> statement");
11257
11258   if (retval)
11259     {
11260       tree semantic_type = NULL_TREE;
11261       npc = null_pointer_constant_p (retval);
11262       if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
11263         {
11264           semantic_type = TREE_TYPE (retval);
11265           retval = TREE_OPERAND (retval, 0);
11266         }
11267       retval = c_fully_fold (retval, false, NULL);
11268       if (semantic_type
11269           && valtype != NULL_TREE
11270           && TREE_CODE (valtype) != VOID_TYPE)
11271         retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
11272     }
11273
11274   if (!retval)
11275     {
11276       current_function_returns_null = 1;
11277       if ((warn_return_type >= 0 || flag_isoc99)
11278           && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
11279         {
11280           bool warned_here;
11281           if (flag_isoc99)
11282             warned_here = pedwarn
11283               (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
11284                "%<return%> with no value, in function returning non-void");
11285           else
11286             warned_here = warning_at
11287               (loc, OPT_Wreturn_type,
11288                "%<return%> with no value, in function returning non-void");
11289           no_warning = true;
11290           if (warned_here)
11291             inform (DECL_SOURCE_LOCATION (current_function_decl),
11292                     "declared here");
11293         }
11294     }
11295   else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
11296     {
11297       current_function_returns_null = 1;
11298       bool warned_here;
11299       if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
11300         warned_here = pedwarn
11301           (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
11302            "%<return%> with a value, in function returning void");
11303       else
11304         warned_here = pedwarn
11305           (xloc, OPT_Wpedantic, "ISO C forbids "
11306            "%<return%> with expression, in function returning void");
11307       if (warned_here)
11308         inform (DECL_SOURCE_LOCATION (current_function_decl),
11309                 "declared here");
11310     }
11311   else
11312     {
11313       tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
11314                                        retval, origtype, ic_return,
11315                                        npc, NULL_TREE, NULL_TREE, 0);
11316       tree res = DECL_RESULT (current_function_decl);
11317       tree inner;
11318       bool save;
11319
11320       current_function_returns_value = 1;
11321       if (t == error_mark_node)
11322         return NULL_TREE;
11323
11324       save = in_late_binary_op;
11325       if (C_BOOLEAN_TYPE_P (TREE_TYPE (res))
11326           || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
11327           || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
11328               && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
11329                   || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
11330               && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
11331         in_late_binary_op = true;
11332       inner = t = convert (TREE_TYPE (res), t);
11333       in_late_binary_op = save;
11334
11335       /* Strip any conversions, additions, and subtractions, and see if
11336          we are returning the address of a local variable.  Warn if so.  */
11337       while (1)
11338         {
11339           switch (TREE_CODE (inner))
11340             {
11341             CASE_CONVERT:
11342             case NON_LVALUE_EXPR:
11343             case PLUS_EXPR:
11344             case POINTER_PLUS_EXPR:
11345               inner = TREE_OPERAND (inner, 0);
11346               continue;
11347
11348             case MINUS_EXPR:
11349               /* If the second operand of the MINUS_EXPR has a pointer
11350                  type (or is converted from it), this may be valid, so
11351                  don't give a warning.  */
11352               {
11353                 tree op1 = TREE_OPERAND (inner, 1);
11354
11355                 while (!POINTER_TYPE_P (TREE_TYPE (op1))
11356                        && (CONVERT_EXPR_P (op1)
11357                            || TREE_CODE (op1) == NON_LVALUE_EXPR))
11358                   op1 = TREE_OPERAND (op1, 0);
11359
11360                 if (POINTER_TYPE_P (TREE_TYPE (op1)))
11361                   break;
11362
11363                 inner = TREE_OPERAND (inner, 0);
11364                 continue;
11365               }
11366
11367             case ADDR_EXPR:
11368               inner = TREE_OPERAND (inner, 0);
11369
11370               while (REFERENCE_CLASS_P (inner)
11371                      && !INDIRECT_REF_P (inner))
11372                 inner = TREE_OPERAND (inner, 0);
11373
11374               if (DECL_P (inner)
11375                   && !DECL_EXTERNAL (inner)
11376                   && !TREE_STATIC (inner)
11377                   && DECL_CONTEXT (inner) == current_function_decl
11378                   && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
11379                 {
11380                   if (TREE_CODE (inner) == LABEL_DECL)
11381                     warning_at (loc, OPT_Wreturn_local_addr,
11382                                 "function returns address of label");
11383                   else
11384                     {
11385                       warning_at (loc, OPT_Wreturn_local_addr,
11386                                   "function returns address of local variable");
11387                       tree zero = build_zero_cst (TREE_TYPE (res));
11388                       t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
11389                     }
11390                 }
11391               break;
11392
11393             default:
11394               break;
11395             }
11396
11397           break;
11398         }
11399
11400       retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11401       SET_EXPR_LOCATION (retval, loc);
11402
11403       if (warn_sequence_point)
11404         verify_sequence_points (retval);
11405     }
11406
11407   ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11408   if (no_warning)
11409     suppress_warning (ret_stmt, OPT_Wreturn_type);
11410   return add_stmt (ret_stmt);
11411 }
11412 \f
11413 struct c_switch {
11414   /* The SWITCH_STMT being built.  */
11415   tree switch_stmt;
11416
11417   /* The original type of the testing expression, i.e. before the
11418      default conversion is applied.  */
11419   tree orig_type;
11420
11421   /* A splay-tree mapping the low element of a case range to the high
11422      element, or NULL_TREE if there is no high element.  Used to
11423      determine whether or not a new case label duplicates an old case
11424      label.  We need a tree, rather than simply a hash table, because
11425      of the GNU case range extension.  */
11426   splay_tree cases;
11427
11428   /* The bindings at the point of the switch.  This is used for
11429      warnings crossing decls when branching to a case label.  */
11430   struct c_spot_bindings *bindings;
11431
11432   /* Whether the switch includes any break statements.  */
11433   bool break_stmt_seen_p;
11434
11435   /* The next node on the stack.  */
11436   struct c_switch *next;
11437
11438   /* Remember whether the controlling expression had boolean type
11439      before integer promotions for the sake of -Wswitch-bool.  */
11440   bool bool_cond_p;
11441 };
11442
11443 /* A stack of the currently active switch statements.  The innermost
11444    switch statement is on the top of the stack.  There is no need to
11445    mark the stack for garbage collection because it is only active
11446    during the processing of the body of a function, and we never
11447    collect at that point.  */
11448
11449 struct c_switch *c_switch_stack;
11450
11451 /* Start a C switch statement, testing expression EXP.  Return the new
11452    SWITCH_STMT.  SWITCH_LOC is the location of the `switch'.
11453    SWITCH_COND_LOC is the location of the switch's condition.
11454    EXPLICIT_CAST_P is true if the expression EXP has an explicit cast.  */
11455
11456 tree
11457 c_start_switch (location_t switch_loc,
11458                 location_t switch_cond_loc,
11459                 tree exp, bool explicit_cast_p)
11460 {
11461   tree orig_type = error_mark_node;
11462   bool bool_cond_p = false;
11463   struct c_switch *cs;
11464
11465   if (exp != error_mark_node)
11466     {
11467       orig_type = TREE_TYPE (exp);
11468
11469       if (!INTEGRAL_TYPE_P (orig_type))
11470         {
11471           if (orig_type != error_mark_node)
11472             {
11473               error_at (switch_cond_loc, "switch quantity not an integer");
11474               orig_type = error_mark_node;
11475             }
11476           exp = integer_zero_node;
11477         }
11478       else
11479         {
11480           tree type = TYPE_MAIN_VARIANT (orig_type);
11481           tree e = exp;
11482
11483           /* Warn if the condition has boolean value.  */
11484           while (TREE_CODE (e) == COMPOUND_EXPR)
11485             e = TREE_OPERAND (e, 1);
11486
11487           if ((C_BOOLEAN_TYPE_P (type)
11488                || truth_value_p (TREE_CODE (e)))
11489               /* Explicit cast to int suppresses this warning.  */
11490               && !(TREE_CODE (type) == INTEGER_TYPE
11491                    && explicit_cast_p))
11492             bool_cond_p = true;
11493
11494           if (!in_system_header_at (input_location)
11495               && (type == long_integer_type_node
11496                   || type == long_unsigned_type_node))
11497             warning_at (switch_cond_loc,
11498                         OPT_Wtraditional, "%<long%> switch expression not "
11499                         "converted to %<int%> in ISO C");
11500
11501           exp = c_fully_fold (exp, false, NULL);
11502           exp = default_conversion (exp);
11503
11504           if (warn_sequence_point)
11505             verify_sequence_points (exp);
11506         }
11507     }
11508
11509   /* Add this new SWITCH_STMT to the stack.  */
11510   cs = XNEW (struct c_switch);
11511   cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11512                                 NULL_TREE, orig_type, NULL_TREE);
11513   cs->orig_type = orig_type;
11514   cs->cases = splay_tree_new (case_compare, NULL, NULL);
11515   cs->bindings = c_get_switch_bindings ();
11516   cs->break_stmt_seen_p = false;
11517   cs->bool_cond_p = bool_cond_p;
11518   cs->next = c_switch_stack;
11519   c_switch_stack = cs;
11520
11521   return add_stmt (cs->switch_stmt);
11522 }
11523
11524 /* Process a case label at location LOC, with attributes ATTRS.  */
11525
11526 tree
11527 do_case (location_t loc, tree low_value, tree high_value, tree attrs)
11528 {
11529   tree label = NULL_TREE;
11530
11531   if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11532     {
11533       low_value = c_fully_fold (low_value, false, NULL);
11534       if (TREE_CODE (low_value) == INTEGER_CST)
11535         pedwarn (loc, OPT_Wpedantic,
11536                  "case label is not an integer constant expression");
11537     }
11538
11539   if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11540     {
11541       high_value = c_fully_fold (high_value, false, NULL);
11542       if (TREE_CODE (high_value) == INTEGER_CST)
11543         pedwarn (input_location, OPT_Wpedantic,
11544                  "case label is not an integer constant expression");
11545     }
11546
11547   if (c_switch_stack == NULL)
11548     {
11549       if (low_value)
11550         error_at (loc, "case label not within a switch statement");
11551       else
11552         error_at (loc, "%<default%> label not within a switch statement");
11553       return NULL_TREE;
11554     }
11555
11556   if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11557                                     EXPR_LOCATION (c_switch_stack->switch_stmt),
11558                                     loc))
11559     return NULL_TREE;
11560
11561   label = c_add_case_label (loc, c_switch_stack->cases,
11562                             SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11563                             low_value, high_value, attrs);
11564   if (label == error_mark_node)
11565     label = NULL_TREE;
11566   return label;
11567 }
11568
11569 /* Finish the switch statement.  TYPE is the original type of the
11570    controlling expression of the switch, or NULL_TREE.  */
11571
11572 void
11573 c_finish_switch (tree body, tree type)
11574 {
11575   struct c_switch *cs = c_switch_stack;
11576   location_t switch_location;
11577
11578   SWITCH_STMT_BODY (cs->switch_stmt) = body;
11579
11580   /* Emit warnings as needed.  */
11581   switch_location = EXPR_LOCATION (cs->switch_stmt);
11582   c_do_switch_warnings (cs->cases, switch_location,
11583                         type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11584                         SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11585   if (c_switch_covers_all_cases_p (cs->cases,
11586                                    SWITCH_STMT_TYPE (cs->switch_stmt)))
11587     SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11588   SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11589
11590   /* Pop the stack.  */
11591   c_switch_stack = cs->next;
11592   splay_tree_delete (cs->cases);
11593   c_release_switch_bindings (cs->bindings);
11594   XDELETE (cs);
11595 }
11596 \f
11597 /* Emit an if statement.  IF_LOCUS is the location of the 'if'.  COND,
11598    THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11599    may be null.  */
11600
11601 void
11602 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11603                   tree else_block)
11604 {
11605   tree stmt;
11606
11607   stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11608   SET_EXPR_LOCATION (stmt, if_locus);
11609   add_stmt (stmt);
11610 }
11611
11612 tree
11613 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11614 {
11615   /* In switch statements break is sometimes stylistically used after
11616      a return statement.  This can lead to spurious warnings about
11617      control reaching the end of a non-void function when it is
11618      inlined.  Note that we are calling block_may_fallthru with
11619      language specific tree nodes; this works because
11620      block_may_fallthru returns true when given something it does not
11621      understand.  */
11622   bool skip = !block_may_fallthru (cur_stmt_list);
11623
11624   if (is_break)
11625     switch (in_statement)
11626       {
11627       case 0:
11628         error_at (loc, "break statement not within loop or switch");
11629         return NULL_TREE;
11630       case IN_OMP_BLOCK:
11631         error_at (loc, "invalid exit from OpenMP structured block");
11632         return NULL_TREE;
11633       case IN_OMP_FOR:
11634         error_at (loc, "break statement used with OpenMP for loop");
11635         return NULL_TREE;
11636       case IN_ITERATION_STMT:
11637       case IN_OBJC_FOREACH:
11638         break;
11639       default:
11640         gcc_assert (in_statement & IN_SWITCH_STMT);
11641         c_switch_stack->break_stmt_seen_p = true;
11642         break;
11643       }
11644   else
11645     switch (in_statement & ~IN_SWITCH_STMT)
11646       {
11647       case 0:
11648         error_at (loc, "continue statement not within a loop");
11649         return NULL_TREE;
11650       case IN_OMP_BLOCK:
11651         error_at (loc, "invalid exit from OpenMP structured block");
11652         return NULL_TREE;
11653       case IN_ITERATION_STMT:
11654       case IN_OMP_FOR:
11655       case IN_OBJC_FOREACH:
11656         break;
11657       default:
11658         gcc_unreachable ();
11659       }
11660
11661   if (skip)
11662     return NULL_TREE;
11663   else if ((in_statement & IN_OBJC_FOREACH)
11664            && !(is_break && (in_statement & IN_SWITCH_STMT)))
11665     {
11666       /* The foreach expander produces low-level code using gotos instead
11667          of a structured loop construct.  */
11668       gcc_assert (label);
11669       return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11670     }
11671   return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11672 }
11673
11674 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr.  */
11675
11676 static void
11677 emit_side_effect_warnings (location_t loc, tree expr)
11678 {
11679   maybe_warn_nodiscard (loc, expr);
11680   if (!warn_unused_value)
11681     return;
11682   if (expr == error_mark_node)
11683     ;
11684   else if (!TREE_SIDE_EFFECTS (expr))
11685     {
11686       if (!VOID_TYPE_P (TREE_TYPE (expr))
11687           && !warning_suppressed_p (expr, OPT_Wunused_value))
11688         warning_at (loc, OPT_Wunused_value, "statement with no effect");
11689     }
11690   else if (TREE_CODE (expr) == COMPOUND_EXPR)
11691     {
11692       tree r = expr;
11693       location_t cloc = loc;
11694       while (TREE_CODE (r) == COMPOUND_EXPR)
11695         {
11696           if (EXPR_HAS_LOCATION (r))
11697             cloc = EXPR_LOCATION (r);
11698           r = TREE_OPERAND (r, 1);
11699         }
11700       if (!TREE_SIDE_EFFECTS (r)
11701           && !VOID_TYPE_P (TREE_TYPE (r))
11702           && !CONVERT_EXPR_P (r)
11703           && !warning_suppressed_p (r, OPT_Wunused_value)
11704           && !warning_suppressed_p (expr, OPT_Wunused_value))
11705         warning_at (cloc, OPT_Wunused_value,
11706                     "right-hand operand of comma expression has no effect");
11707     }
11708   else
11709     warn_if_unused_value (expr, loc);
11710 }
11711
11712 /* Process an expression as if it were a complete statement.  Emit
11713    diagnostics, but do not call ADD_STMT.  LOC is the location of the
11714    statement.  */
11715
11716 tree
11717 c_process_expr_stmt (location_t loc, tree expr)
11718 {
11719   tree exprv;
11720
11721   if (!expr)
11722     return NULL_TREE;
11723
11724   expr = c_fully_fold (expr, false, NULL);
11725
11726   if (warn_sequence_point)
11727     verify_sequence_points (expr);
11728
11729   if (TREE_TYPE (expr) != error_mark_node
11730       && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11731       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11732     error_at (loc, "expression statement has incomplete type");
11733
11734   /* If we're not processing a statement expression, warn about unused values.
11735      Warnings for statement expressions will be emitted later, once we figure
11736      out which is the result.  */
11737   if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11738       && (warn_unused_value || warn_unused_result))
11739     emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11740
11741   exprv = expr;
11742   while (TREE_CODE (exprv) == COMPOUND_EXPR)
11743     exprv = TREE_OPERAND (exprv, 1);
11744   while (CONVERT_EXPR_P (exprv))
11745     exprv = TREE_OPERAND (exprv, 0);
11746   if (DECL_P (exprv)
11747       || handled_component_p (exprv)
11748       || TREE_CODE (exprv) == ADDR_EXPR)
11749     mark_exp_read (exprv);
11750
11751   /* If the expression is not of a type to which we cannot assign a line
11752      number, wrap the thing in a no-op NOP_EXPR.  */
11753   if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11754     {
11755       expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11756       SET_EXPR_LOCATION (expr, loc);
11757     }
11758
11759   return expr;
11760 }
11761
11762 /* Emit an expression as a statement.  LOC is the location of the
11763    expression.  */
11764
11765 tree
11766 c_finish_expr_stmt (location_t loc, tree expr)
11767 {
11768   if (expr)
11769     return add_stmt (c_process_expr_stmt (loc, expr));
11770   else
11771     return NULL;
11772 }
11773
11774 /* Do the opposite and emit a statement as an expression.  To begin,
11775    create a new binding level and return it.  */
11776
11777 tree
11778 c_begin_stmt_expr (void)
11779 {
11780   tree ret;
11781
11782   /* We must force a BLOCK for this level so that, if it is not expanded
11783      later, there is a way to turn off the entire subtree of blocks that
11784      are contained in it.  */
11785   keep_next_level ();
11786   ret = c_begin_compound_stmt (true);
11787
11788   c_bindings_start_stmt_expr (c_switch_stack == NULL
11789                               ? NULL
11790                               : c_switch_stack->bindings);
11791
11792   /* Mark the current statement list as belonging to a statement list.  */
11793   STATEMENT_LIST_STMT_EXPR (ret) = 1;
11794
11795   return ret;
11796 }
11797
11798 /* LOC is the location of the compound statement to which this body
11799    belongs.  */
11800
11801 tree
11802 c_finish_stmt_expr (location_t loc, tree body)
11803 {
11804   tree last, type, tmp, val;
11805   tree *last_p;
11806
11807   body = c_end_compound_stmt (loc, body, true);
11808
11809   c_bindings_end_stmt_expr (c_switch_stack == NULL
11810                             ? NULL
11811                             : c_switch_stack->bindings);
11812
11813   /* Locate the last statement in BODY.  See c_end_compound_stmt
11814      about always returning a BIND_EXPR.  */
11815   last_p = &BIND_EXPR_BODY (body);
11816   last = BIND_EXPR_BODY (body);
11817
11818  continue_searching:
11819   if (TREE_CODE (last) == STATEMENT_LIST)
11820     {
11821       tree_stmt_iterator l = tsi_last (last);
11822
11823       while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11824         tsi_prev (&l);
11825
11826       /* This can happen with degenerate cases like ({ }).  No value.  */
11827       if (tsi_end_p (l))
11828         return body;
11829
11830       /* If we're supposed to generate side effects warnings, process
11831          all of the statements except the last.  */
11832       if (warn_unused_value || warn_unused_result)
11833         {
11834           for (tree_stmt_iterator i = tsi_start (last);
11835                tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11836             {
11837               location_t tloc;
11838               tree t = tsi_stmt (i);
11839
11840               tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11841               emit_side_effect_warnings (tloc, t);
11842             }
11843         }
11844       last_p = tsi_stmt_ptr (l);
11845       last = *last_p;
11846     }
11847
11848   /* If the end of the list is exception related, then the list was split
11849      by a call to push_cleanup.  Continue searching.  */
11850   if (TREE_CODE (last) == TRY_FINALLY_EXPR
11851       || TREE_CODE (last) == TRY_CATCH_EXPR)
11852     {
11853       last_p = &TREE_OPERAND (last, 0);
11854       last = *last_p;
11855       goto continue_searching;
11856     }
11857
11858   if (last == error_mark_node)
11859     return last;
11860
11861   /* In the case that the BIND_EXPR is not necessary, return the
11862      expression out from inside it.  */
11863   if ((last == BIND_EXPR_BODY (body)
11864        /* Skip nested debug stmts.  */
11865        || last == expr_first (BIND_EXPR_BODY (body)))
11866       && BIND_EXPR_VARS (body) == NULL)
11867     {
11868       /* Even if this looks constant, do not allow it in a constant
11869          expression.  */
11870       last = c_wrap_maybe_const (last, true);
11871       /* Do not warn if the return value of a statement expression is
11872          unused.  */
11873       suppress_warning (last, OPT_Wunused);
11874       return last;
11875     }
11876
11877   /* Extract the type of said expression.  */
11878   type = TREE_TYPE (last);
11879
11880   /* If we're not returning a value at all, then the BIND_EXPR that
11881      we already have is a fine expression to return.  */
11882   if (!type || VOID_TYPE_P (type))
11883     return body;
11884
11885   /* Now that we've located the expression containing the value, it seems
11886      silly to make voidify_wrapper_expr repeat the process.  Create a
11887      temporary of the appropriate type and stick it in a TARGET_EXPR.  */
11888   tmp = create_tmp_var_raw (type);
11889
11890   /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt.  This avoids
11891      tree_expr_nonnegative_p giving up immediately.  */
11892   val = last;
11893   if (TREE_CODE (val) == NOP_EXPR
11894       && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11895     val = TREE_OPERAND (val, 0);
11896
11897   *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11898   SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11899
11900   {
11901     tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11902     SET_EXPR_LOCATION (t, loc);
11903     return t;
11904   }
11905 }
11906 \f
11907 /* Begin and end compound statements.  This is as simple as pushing
11908    and popping new statement lists from the tree.  */
11909
11910 tree
11911 c_begin_compound_stmt (bool do_scope)
11912 {
11913   tree stmt = push_stmt_list ();
11914   if (do_scope)
11915     push_scope ();
11916   return stmt;
11917 }
11918
11919 /* End a compound statement.  STMT is the statement.  LOC is the
11920    location of the compound statement-- this is usually the location
11921    of the opening brace.  */
11922
11923 tree
11924 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11925 {
11926   tree block = NULL;
11927
11928   if (do_scope)
11929     {
11930       if (c_dialect_objc ())
11931         objc_clear_super_receiver ();
11932       block = pop_scope ();
11933     }
11934
11935   stmt = pop_stmt_list (stmt);
11936   stmt = c_build_bind_expr (loc, block, stmt);
11937
11938   /* If this compound statement is nested immediately inside a statement
11939      expression, then force a BIND_EXPR to be created.  Otherwise we'll
11940      do the wrong thing for ({ { 1; } }) or ({ 1; { } }).  In particular,
11941      STATEMENT_LISTs merge, and thus we can lose track of what statement
11942      was really last.  */
11943   if (building_stmt_list_p ()
11944       && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11945       && TREE_CODE (stmt) != BIND_EXPR)
11946     {
11947       stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11948       TREE_SIDE_EFFECTS (stmt) = 1;
11949       SET_EXPR_LOCATION (stmt, loc);
11950     }
11951
11952   return stmt;
11953 }
11954
11955 /* Queue a cleanup.  CLEANUP is an expression/statement to be executed
11956    when the current scope is exited.  EH_ONLY is true when this is not
11957    meant to apply to normal control flow transfer.  */
11958
11959 void
11960 push_cleanup (tree decl, tree cleanup, bool eh_only)
11961 {
11962   enum tree_code code;
11963   tree stmt, list;
11964   bool stmt_expr;
11965
11966   code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11967   stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11968   add_stmt (stmt);
11969   stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11970   list = push_stmt_list ();
11971   TREE_OPERAND (stmt, 0) = list;
11972   STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11973 }
11974 \f
11975 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11976    into a value of TYPE type.  Comparison is done via VEC_COND_EXPR.  */
11977
11978 static tree
11979 build_vec_cmp (tree_code code, tree type,
11980                tree arg0, tree arg1)
11981 {
11982   tree zero_vec = build_zero_cst (type);
11983   tree minus_one_vec = build_minus_one_cst (type);
11984   tree cmp_type = truth_type_for (type);
11985   tree cmp = build2 (code, cmp_type, arg0, arg1);
11986   return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11987 }
11988
11989 /* Possibly warn about an address of OP never being NULL in a comparison
11990    operation CODE involving null.  */
11991
11992 static void
11993 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
11994 {
11995   /* Prevent warnings issued for macro expansion.  */
11996   if (!warn_address
11997       || warning_suppressed_p (op, OPT_Waddress)
11998       || from_macro_expansion_at (loc))
11999     return;
12000
12001   if (TREE_CODE (op) == NOP_EXPR)
12002     {
12003       /* Allow casts to intptr_t to suppress the warning.  */
12004       tree type = TREE_TYPE (op);
12005       if (TREE_CODE (type) == INTEGER_TYPE)
12006         return;
12007       op = TREE_OPERAND (op, 0);
12008     }
12009
12010   if (TREE_CODE (op) == POINTER_PLUS_EXPR)
12011     {
12012       /* Allow a cast to void* to suppress the warning.  */
12013       tree type = TREE_TYPE (TREE_TYPE (op));
12014       if (VOID_TYPE_P (type))
12015         return;
12016
12017       /* Adding any value to a null pointer, including zero, is undefined
12018          in C.  This includes the expression &p[0] where p is the null
12019          pointer, although &p[0] will have been folded to p by this point
12020          and so not diagnosed.  */
12021       if (code == EQ_EXPR)
12022         warning_at (loc, OPT_Waddress,
12023                     "the comparison will always evaluate as %<false%> "
12024                     "for the pointer operand in %qE must not be NULL",
12025                     op);
12026       else
12027         warning_at (loc, OPT_Waddress,
12028                     "the comparison will always evaluate as %<true%> "
12029                     "for the pointer operand in %qE must not be NULL",
12030                     op);
12031
12032       return;
12033     }
12034
12035   if (TREE_CODE (op) != ADDR_EXPR)
12036     return;
12037
12038   op = TREE_OPERAND (op, 0);
12039
12040   if (TREE_CODE (op) == IMAGPART_EXPR
12041       || TREE_CODE (op) == REALPART_EXPR)
12042     {
12043       /* The address of either complex part may not be null.  */
12044       if (code == EQ_EXPR)
12045         warning_at (loc, OPT_Waddress,
12046                     "the comparison will always evaluate as %<false%> "
12047                     "for the address of %qE will never be NULL",
12048                     op);
12049       else
12050         warning_at (loc, OPT_Waddress,
12051                     "the comparison will always evaluate as %<true%> "
12052                     "for the address of %qE will never be NULL",
12053                     op);
12054       return;
12055     }
12056
12057   /* Set to true in the loop below if OP dereferences is operand.
12058      In such a case the ultimate target need not be a decl for
12059      the null [in]equality test to be constant.  */
12060   bool deref = false;
12061
12062   /* Get the outermost array or object, or member.  */
12063   while (handled_component_p (op))
12064     {
12065       if (TREE_CODE (op) == COMPONENT_REF)
12066         {
12067           /* Get the member (its address is never null).  */
12068           op = TREE_OPERAND (op, 1);
12069           break;
12070         }
12071
12072       /* Get the outer array/object to refer to in the warning.  */
12073       op = TREE_OPERAND (op, 0);
12074       deref = true;
12075     }
12076
12077   if ((!deref && !decl_with_nonnull_addr_p (op))
12078       || from_macro_expansion_at (loc))
12079     return;
12080
12081   bool w;
12082   if (code == EQ_EXPR)
12083     w = warning_at (loc, OPT_Waddress,
12084                     "the comparison will always evaluate as %<false%> "
12085                     "for the address of %qE will never be NULL",
12086                     op);
12087   else
12088     w = warning_at (loc, OPT_Waddress,
12089                     "the comparison will always evaluate as %<true%> "
12090                     "for the address of %qE will never be NULL",
12091                     op);
12092
12093   if (w && DECL_P (op))
12094     inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
12095 }
12096
12097 /* Build a binary-operation expression without default conversions.
12098    CODE is the kind of expression to build.
12099    LOCATION is the operator's location.
12100    This function differs from `build' in several ways:
12101    the data type of the result is computed and recorded in it,
12102    warnings are generated if arg data types are invalid,
12103    special handling for addition and subtraction of pointers is known,
12104    and some optimization is done (operations on narrow ints
12105    are done in the narrower type when that gives the same result).
12106    Constant folding is also done before the result is returned.
12107
12108    Note that the operands will never have enumeral types, or function
12109    or array types, because either they will have the default conversions
12110    performed or they have both just been converted to some other type in which
12111    the arithmetic is to be done.  */
12112
12113 tree
12114 build_binary_op (location_t location, enum tree_code code,
12115                  tree orig_op0, tree orig_op1, bool convert_p)
12116 {
12117   tree type0, type1, orig_type0, orig_type1;
12118   tree eptype;
12119   enum tree_code code0, code1;
12120   tree op0, op1;
12121   tree ret = error_mark_node;
12122   const char *invalid_op_diag;
12123   bool op0_int_operands, op1_int_operands;
12124   bool int_const, int_const_or_overflow, int_operands;
12125
12126   /* Expression code to give to the expression when it is built.
12127      Normally this is CODE, which is what the caller asked for,
12128      but in some special cases we change it.  */
12129   enum tree_code resultcode = code;
12130
12131   /* Data type in which the computation is to be performed.
12132      In the simplest cases this is the common type of the arguments.  */
12133   tree result_type = NULL;
12134
12135   /* When the computation is in excess precision, the type of the
12136      final EXCESS_PRECISION_EXPR.  */
12137   tree semantic_result_type = NULL;
12138
12139   /* Nonzero means operands have already been type-converted
12140      in whatever way is necessary.
12141      Zero means they need to be converted to RESULT_TYPE.  */
12142   int converted = 0;
12143
12144   /* Nonzero means create the expression with this type, rather than
12145      RESULT_TYPE.  */
12146   tree build_type = NULL_TREE;
12147
12148   /* Nonzero means after finally constructing the expression
12149      convert it to this type.  */
12150   tree final_type = NULL_TREE;
12151
12152   /* Nonzero if this is an operation like MIN or MAX which can
12153      safely be computed in short if both args are promoted shorts.
12154      Also implies COMMON.
12155      -1 indicates a bitwise operation; this makes a difference
12156      in the exact conditions for when it is safe to do the operation
12157      in a narrower mode.  */
12158   int shorten = 0;
12159
12160   /* Nonzero if this is a comparison operation;
12161      if both args are promoted shorts, compare the original shorts.
12162      Also implies COMMON.  */
12163   int short_compare = 0;
12164
12165   /* Nonzero if this is a right-shift operation, which can be computed on the
12166      original short and then promoted if the operand is a promoted short.  */
12167   int short_shift = 0;
12168
12169   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
12170   int common = 0;
12171
12172   /* True means types are compatible as far as ObjC is concerned.  */
12173   bool objc_ok;
12174
12175   /* True means this is an arithmetic operation that may need excess
12176      precision.  */
12177   bool may_need_excess_precision;
12178
12179   /* True means this is a boolean operation that converts both its
12180      operands to truth-values.  */
12181   bool boolean_op = false;
12182
12183   /* Remember whether we're doing / or %.  */
12184   bool doing_div_or_mod = false;
12185
12186   /* Remember whether we're doing << or >>.  */
12187   bool doing_shift = false;
12188
12189   /* Tree holding instrumentation expression.  */
12190   tree instrument_expr = NULL;
12191
12192   if (location == UNKNOWN_LOCATION)
12193     location = input_location;
12194
12195   op0 = orig_op0;
12196   op1 = orig_op1;
12197
12198   op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
12199   if (op0_int_operands)
12200     op0 = remove_c_maybe_const_expr (op0);
12201   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
12202   if (op1_int_operands)
12203     op1 = remove_c_maybe_const_expr (op1);
12204   int_operands = (op0_int_operands && op1_int_operands);
12205   if (int_operands)
12206     {
12207       int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
12208                                && TREE_CODE (orig_op1) == INTEGER_CST);
12209       int_const = (int_const_or_overflow
12210                    && !TREE_OVERFLOW (orig_op0)
12211                    && !TREE_OVERFLOW (orig_op1));
12212     }
12213   else
12214     int_const = int_const_or_overflow = false;
12215
12216   /* Do not apply default conversion in mixed vector/scalar expression.  */
12217   if (convert_p
12218       && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
12219     {
12220       op0 = default_conversion (op0);
12221       op1 = default_conversion (op1);
12222     }
12223
12224   orig_type0 = type0 = TREE_TYPE (op0);
12225
12226   orig_type1 = type1 = TREE_TYPE (op1);
12227
12228   /* The expression codes of the data types of the arguments tell us
12229      whether the arguments are integers, floating, pointers, etc.  */
12230   code0 = TREE_CODE (type0);
12231   code1 = TREE_CODE (type1);
12232
12233   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
12234   STRIP_TYPE_NOPS (op0);
12235   STRIP_TYPE_NOPS (op1);
12236
12237   /* If an error was already reported for one of the arguments,
12238      avoid reporting another error.  */
12239
12240   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12241     return error_mark_node;
12242
12243   if (code0 == POINTER_TYPE
12244       && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
12245     return error_mark_node;
12246
12247   if (code1 == POINTER_TYPE
12248       && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
12249     return error_mark_node;
12250
12251   if ((invalid_op_diag
12252        = targetm.invalid_binary_op (code, type0, type1)))
12253     {
12254       error_at (location, invalid_op_diag);
12255       return error_mark_node;
12256     }
12257
12258   switch (code)
12259     {
12260     case PLUS_EXPR:
12261     case MINUS_EXPR:
12262     case MULT_EXPR:
12263     case TRUNC_DIV_EXPR:
12264     case CEIL_DIV_EXPR:
12265     case FLOOR_DIV_EXPR:
12266     case ROUND_DIV_EXPR:
12267     case EXACT_DIV_EXPR:
12268       may_need_excess_precision = true;
12269       break;
12270
12271     case EQ_EXPR:
12272     case NE_EXPR:
12273     case LE_EXPR:
12274     case GE_EXPR:
12275     case LT_EXPR:
12276     case GT_EXPR:
12277       /* Excess precision for implicit conversions of integers to
12278          floating point in C11 and later.  */
12279       may_need_excess_precision = (flag_isoc11
12280                                    && (ANY_INTEGRAL_TYPE_P (type0)
12281                                        || ANY_INTEGRAL_TYPE_P (type1)));
12282       break;
12283
12284     default:
12285       may_need_excess_precision = false;
12286       break;
12287     }
12288   if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
12289     {
12290       op0 = TREE_OPERAND (op0, 0);
12291       type0 = TREE_TYPE (op0);
12292     }
12293   else if (may_need_excess_precision
12294            && (eptype = excess_precision_type (type0)) != NULL_TREE)
12295     {
12296       type0 = eptype;
12297       op0 = convert (eptype, op0);
12298     }
12299   if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
12300     {
12301       op1 = TREE_OPERAND (op1, 0);
12302       type1 = TREE_TYPE (op1);
12303     }
12304   else if (may_need_excess_precision
12305            && (eptype = excess_precision_type (type1)) != NULL_TREE)
12306     {
12307       type1 = eptype;
12308       op1 = convert (eptype, op1);
12309     }
12310
12311   objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
12312
12313   /* In case when one of the operands of the binary operation is
12314      a vector and another is a scalar -- convert scalar to vector.  */
12315   if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
12316       || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
12317     {
12318       enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
12319                                                      orig_op1, true);
12320
12321       switch (convert_flag)
12322         {
12323           case stv_error:
12324             return error_mark_node;
12325           case stv_firstarg:
12326             {
12327               bool maybe_const = true;
12328               tree sc;
12329               sc = c_fully_fold (op0, false, &maybe_const);
12330               sc = save_expr (sc);
12331               sc = convert (TREE_TYPE (type1), sc);
12332               op0 = build_vector_from_val (type1, sc);
12333               if (!maybe_const)
12334                 op0 = c_wrap_maybe_const (op0, true);
12335               orig_type0 = type0 = TREE_TYPE (op0);
12336               code0 = TREE_CODE (type0);
12337               converted = 1;
12338               break;
12339             }
12340           case stv_secondarg:
12341             {
12342               bool maybe_const = true;
12343               tree sc;
12344               sc = c_fully_fold (op1, false, &maybe_const);
12345               sc = save_expr (sc);
12346               sc = convert (TREE_TYPE (type0), sc);
12347               op1 = build_vector_from_val (type0, sc);
12348               if (!maybe_const)
12349                 op1 = c_wrap_maybe_const (op1, true);
12350               orig_type1 = type1 = TREE_TYPE (op1);
12351               code1 = TREE_CODE (type1);
12352               converted = 1;
12353               break;
12354             }
12355           default:
12356             break;
12357         }
12358     }
12359
12360   switch (code)
12361     {
12362     case PLUS_EXPR:
12363       /* Handle the pointer + int case.  */
12364       if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12365         {
12366           ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
12367           goto return_build_binary_op;
12368         }
12369       else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
12370         {
12371           ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
12372           goto return_build_binary_op;
12373         }
12374       else
12375         common = 1;
12376       break;
12377
12378     case MINUS_EXPR:
12379       /* Subtraction of two similar pointers.
12380          We must subtract them as integers, then divide by object size.  */
12381       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
12382           && comp_target_types (location, type0, type1))
12383         {
12384           ret = pointer_diff (location, op0, op1, &instrument_expr);
12385           goto return_build_binary_op;
12386         }
12387       /* Handle pointer minus int.  Just like pointer plus int.  */
12388       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12389         {
12390           ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
12391           goto return_build_binary_op;
12392         }
12393       else
12394         common = 1;
12395       break;
12396
12397     case MULT_EXPR:
12398       common = 1;
12399       break;
12400
12401     case TRUNC_DIV_EXPR:
12402     case CEIL_DIV_EXPR:
12403     case FLOOR_DIV_EXPR:
12404     case ROUND_DIV_EXPR:
12405     case EXACT_DIV_EXPR:
12406       doing_div_or_mod = true;
12407       warn_for_div_by_zero (location, op1);
12408
12409       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12410            || code0 == FIXED_POINT_TYPE
12411            || code0 == COMPLEX_TYPE
12412            || gnu_vector_type_p (type0))
12413           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12414               || code1 == FIXED_POINT_TYPE
12415               || code1 == COMPLEX_TYPE
12416               || gnu_vector_type_p (type1)))
12417         {
12418           enum tree_code tcode0 = code0, tcode1 = code1;
12419
12420           if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12421             tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12422           if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12423             tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12424
12425           if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
12426               || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12427             resultcode = RDIV_EXPR;
12428           else
12429             /* Although it would be tempting to shorten always here, that
12430                loses on some targets, since the modulo instruction is
12431                undefined if the quotient can't be represented in the
12432                computation mode.  We shorten only if unsigned or if
12433                dividing by something we know != -1.  */
12434             shorten = may_shorten_divmod (op0, op1);
12435           common = 1;
12436         }
12437       break;
12438
12439     case BIT_AND_EXPR:
12440     case BIT_IOR_EXPR:
12441     case BIT_XOR_EXPR:
12442       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12443         shorten = -1;
12444       /* Allow vector types which are not floating point types.   */
12445       else if (gnu_vector_type_p (type0)
12446                && gnu_vector_type_p (type1)
12447                && !VECTOR_FLOAT_TYPE_P (type0)
12448                && !VECTOR_FLOAT_TYPE_P (type1))
12449         common = 1;
12450       break;
12451
12452     case TRUNC_MOD_EXPR:
12453     case FLOOR_MOD_EXPR:
12454       doing_div_or_mod = true;
12455       warn_for_div_by_zero (location, op1);
12456
12457       if (gnu_vector_type_p (type0)
12458           && gnu_vector_type_p (type1)
12459           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12460           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12461         common = 1;
12462       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12463         {
12464           /* Although it would be tempting to shorten always here, that loses
12465              on some targets, since the modulo instruction is undefined if the
12466              quotient can't be represented in the computation mode.  We shorten
12467              only if unsigned or if dividing by something we know != -1.  */
12468           shorten = may_shorten_divmod (op0, op1);
12469           common = 1;
12470         }
12471       break;
12472
12473     case TRUTH_ANDIF_EXPR:
12474     case TRUTH_ORIF_EXPR:
12475     case TRUTH_AND_EXPR:
12476     case TRUTH_OR_EXPR:
12477     case TRUTH_XOR_EXPR:
12478       if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12479            || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12480            || code0 == FIXED_POINT_TYPE || code0 == NULLPTR_TYPE)
12481           && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12482               || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12483               || code1 == FIXED_POINT_TYPE || code1 ==  NULLPTR_TYPE))
12484         {
12485           /* Result of these operations is always an int,
12486              but that does not mean the operands should be
12487              converted to ints!  */
12488           result_type = integer_type_node;
12489           if (op0_int_operands)
12490             {
12491               op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12492               op0 = remove_c_maybe_const_expr (op0);
12493             }
12494           else
12495             op0 = c_objc_common_truthvalue_conversion (location, op0);
12496           if (op1_int_operands)
12497             {
12498               op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12499               op1 = remove_c_maybe_const_expr (op1);
12500             }
12501           else
12502             op1 = c_objc_common_truthvalue_conversion (location, op1);
12503           converted = 1;
12504           boolean_op = true;
12505         }
12506       if (code == TRUTH_ANDIF_EXPR)
12507         {
12508           int_const_or_overflow = (int_operands
12509                                    && TREE_CODE (orig_op0) == INTEGER_CST
12510                                    && (op0 == truthvalue_false_node
12511                                        || TREE_CODE (orig_op1) == INTEGER_CST));
12512           int_const = (int_const_or_overflow
12513                        && !TREE_OVERFLOW (orig_op0)
12514                        && (op0 == truthvalue_false_node
12515                            || !TREE_OVERFLOW (orig_op1)));
12516         }
12517       else if (code == TRUTH_ORIF_EXPR)
12518         {
12519           int_const_or_overflow = (int_operands
12520                                    && TREE_CODE (orig_op0) == INTEGER_CST
12521                                    && (op0 == truthvalue_true_node
12522                                        || TREE_CODE (orig_op1) == INTEGER_CST));
12523           int_const = (int_const_or_overflow
12524                        && !TREE_OVERFLOW (orig_op0)
12525                        && (op0 == truthvalue_true_node
12526                            || !TREE_OVERFLOW (orig_op1)));
12527         }
12528       break;
12529
12530       /* Shift operations: result has same type as first operand;
12531          always convert second operand to int.
12532          Also set SHORT_SHIFT if shifting rightward.  */
12533
12534     case RSHIFT_EXPR:
12535       if (gnu_vector_type_p (type0)
12536           && gnu_vector_type_p (type1)
12537           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12538           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12539           && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12540                        TYPE_VECTOR_SUBPARTS (type1)))
12541         {
12542           result_type = type0;
12543           converted = 1;
12544         }
12545       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12546                 || (gnu_vector_type_p (type0)
12547                     && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12548                && code1 == INTEGER_TYPE)
12549         {
12550           doing_shift = true;
12551           if (TREE_CODE (op1) == INTEGER_CST)
12552             {
12553               if (tree_int_cst_sgn (op1) < 0)
12554                 {
12555                   int_const = false;
12556                   if (c_inhibit_evaluation_warnings == 0)
12557                     warning_at (location, OPT_Wshift_count_negative,
12558                                 "right shift count is negative");
12559                 }
12560               else if (code0 == VECTOR_TYPE)
12561                 {
12562                   if (compare_tree_int (op1,
12563                                         TYPE_PRECISION (TREE_TYPE (type0)))
12564                       >= 0)
12565                     {
12566                       int_const = false;
12567                       if (c_inhibit_evaluation_warnings == 0)
12568                         warning_at (location, OPT_Wshift_count_overflow,
12569                                     "right shift count >= width of vector element");
12570                     }
12571                 }
12572               else
12573                 {
12574                   if (!integer_zerop (op1))
12575                     short_shift = 1;
12576
12577                   if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12578                     {
12579                       int_const = false;
12580                       if (c_inhibit_evaluation_warnings == 0)
12581                         warning_at (location, OPT_Wshift_count_overflow,
12582                                     "right shift count >= width of type");
12583                     }
12584                 }
12585             }
12586
12587           /* Use the type of the value to be shifted.  */
12588           result_type = type0;
12589           /* Avoid converting op1 to result_type later.  */
12590           converted = 1;
12591         }
12592       break;
12593
12594     case LSHIFT_EXPR:
12595       if (gnu_vector_type_p (type0)
12596           && gnu_vector_type_p (type1)
12597           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12598           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12599           && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12600                        TYPE_VECTOR_SUBPARTS (type1)))
12601         {
12602           result_type = type0;
12603           converted = 1;
12604         }
12605       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12606                 || (gnu_vector_type_p (type0)
12607                     && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12608                && code1 == INTEGER_TYPE)
12609         {
12610           doing_shift = true;
12611           if (TREE_CODE (op0) == INTEGER_CST
12612               && tree_int_cst_sgn (op0) < 0
12613               && !TYPE_OVERFLOW_WRAPS (type0))
12614             {
12615               /* Don't reject a left shift of a negative value in a context
12616                  where a constant expression is needed in C90.  */
12617               if (flag_isoc99)
12618                 int_const = false;
12619               if (c_inhibit_evaluation_warnings == 0)
12620                 warning_at (location, OPT_Wshift_negative_value,
12621                             "left shift of negative value");
12622             }
12623           if (TREE_CODE (op1) == INTEGER_CST)
12624             {
12625               if (tree_int_cst_sgn (op1) < 0)
12626                 {
12627                   int_const = false;
12628                   if (c_inhibit_evaluation_warnings == 0)
12629                     warning_at (location, OPT_Wshift_count_negative,
12630                                 "left shift count is negative");
12631                 }
12632               else if (code0 == VECTOR_TYPE)
12633                 {
12634                   if (compare_tree_int (op1,
12635                                         TYPE_PRECISION (TREE_TYPE (type0)))
12636                       >= 0)
12637                     {
12638                       int_const = false;
12639                       if (c_inhibit_evaluation_warnings == 0)
12640                         warning_at (location, OPT_Wshift_count_overflow,
12641                                     "left shift count >= width of vector element");
12642                     }
12643                 }
12644               else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12645                 {
12646                   int_const = false;
12647                   if (c_inhibit_evaluation_warnings == 0)
12648                     warning_at (location, OPT_Wshift_count_overflow,
12649                                 "left shift count >= width of type");
12650                 }
12651               else if (TREE_CODE (op0) == INTEGER_CST
12652                        && maybe_warn_shift_overflow (location, op0, op1)
12653                        && flag_isoc99)
12654                 int_const = false;
12655             }
12656
12657           /* Use the type of the value to be shifted.  */
12658           result_type = type0;
12659           /* Avoid converting op1 to result_type later.  */
12660           converted = 1;
12661         }
12662       break;
12663
12664     case EQ_EXPR:
12665     case NE_EXPR:
12666       if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12667         {
12668           tree intt;
12669           if (!vector_types_compatible_elements_p (type0, type1))
12670             {
12671               error_at (location, "comparing vectors with different "
12672                                   "element types");
12673               return error_mark_node;
12674             }
12675
12676           if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12677                         TYPE_VECTOR_SUBPARTS (type1)))
12678             {
12679               error_at (location, "comparing vectors with different "
12680                                   "number of elements");
12681               return error_mark_node;
12682             }
12683
12684           /* It's not precisely specified how the usual arithmetic
12685              conversions apply to the vector types.  Here, we use
12686              the unsigned type if one of the operands is signed and
12687              the other one is unsigned.  */
12688           if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12689             {
12690               if (!TYPE_UNSIGNED (type0))
12691                 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12692               else
12693                 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12694               warning_at (location, OPT_Wsign_compare, "comparison between "
12695                           "types %qT and %qT", type0, type1);
12696             }
12697
12698           /* Always construct signed integer vector type.  */
12699           intt = c_common_type_for_size (GET_MODE_BITSIZE
12700                                          (SCALAR_TYPE_MODE
12701                                           (TREE_TYPE (type0))), 0);
12702           if (!intt)
12703             {
12704               error_at (location, "could not find an integer type "
12705                                   "of the same size as %qT",
12706                         TREE_TYPE (type0));
12707               return error_mark_node;
12708             }
12709           result_type = build_opaque_vector_type (intt,
12710                                                   TYPE_VECTOR_SUBPARTS (type0));
12711           converted = 1;
12712           ret = build_vec_cmp (resultcode, result_type, op0, op1);
12713           goto return_build_binary_op;
12714         }
12715       if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12716         warning_at (location,
12717                     OPT_Wfloat_equal,
12718                     "comparing floating-point with %<==%> or %<!=%> is unsafe");
12719       /* Result of comparison is always int,
12720          but don't convert the args to int!  */
12721       build_type = integer_type_node;
12722       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12723            || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12724           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12725               || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12726         short_compare = 1;
12727       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12728         {
12729           maybe_warn_for_null_address (location, op0, code);
12730           result_type = type0;
12731         }
12732       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12733         {
12734           maybe_warn_for_null_address (location, op1, code);
12735           result_type = type1;
12736         }
12737       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12738         {
12739           tree tt0 = TREE_TYPE (type0);
12740           tree tt1 = TREE_TYPE (type1);
12741           addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12742           addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12743           addr_space_t as_common = ADDR_SPACE_GENERIC;
12744
12745           /* Anything compares with void *.  void * compares with anything.
12746              Otherwise, the targets must be compatible
12747              and both must be object or both incomplete.  */
12748           if (comp_target_types (location, type0, type1))
12749             result_type = common_pointer_type (type0, type1);
12750           else if (!addr_space_superset (as0, as1, &as_common))
12751             {
12752               error_at (location, "comparison of pointers to "
12753                         "disjoint address spaces");
12754               return error_mark_node;
12755             }
12756           else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12757             {
12758               if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12759                 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12760                          "comparison of %<void *%> with function pointer");
12761             }
12762           else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12763             {
12764               if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12765                 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12766                          "comparison of %<void *%> with function pointer");
12767             }
12768           else
12769             /* Avoid warning about the volatile ObjC EH puts on decls.  */
12770             if (!objc_ok)
12771               pedwarn (location, 0,
12772                        "comparison of distinct pointer types lacks a cast");
12773
12774           if (result_type == NULL_TREE)
12775             {
12776               int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12777               result_type = build_pointer_type
12778                               (build_qualified_type (void_type_node, qual));
12779             }
12780         }
12781       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12782         {
12783           result_type = type0;
12784           pedwarn (location, 0, "comparison between pointer and integer");
12785         }
12786       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12787         {
12788           result_type = type1;
12789           pedwarn (location, 0, "comparison between pointer and integer");
12790         }
12791       /* 6.5.9: One of the following shall hold:
12792          -- both operands have type nullptr_t;  */
12793       else if (code0 == NULLPTR_TYPE && code1 == NULLPTR_TYPE)
12794         {
12795           result_type = nullptr_type_node;
12796           /* No need to convert the operands to result_type later.  */
12797           converted = 1;
12798         }
12799     /* -- one operand has type nullptr_t and the other is a null pointer
12800        constant.  We will have to convert the former to the type of the
12801        latter, because during gimplification we can't have mismatching
12802        comparison operand type.  We convert from nullptr_t to the other
12803        type, since only nullptr_t can be converted to nullptr_t.  Also,
12804        even a constant 0 is a null pointer constant, so we may have to
12805        create a pointer type from its type.  */
12806       else if (code0 == NULLPTR_TYPE && null_pointer_constant_p (orig_op1))
12807         result_type = (INTEGRAL_TYPE_P (type1)
12808                        ? build_pointer_type (type1) : type1);
12809       else if (code1 == NULLPTR_TYPE && null_pointer_constant_p (orig_op0))
12810         result_type = (INTEGRAL_TYPE_P (type0)
12811                        ? build_pointer_type (type0) : type0);
12812       if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
12813            || truth_value_p (TREE_CODE (orig_op0)))
12814           ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
12815              || truth_value_p (TREE_CODE (orig_op1))))
12816         maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12817       break;
12818
12819     case LE_EXPR:
12820     case GE_EXPR:
12821     case LT_EXPR:
12822     case GT_EXPR:
12823       if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12824         {
12825           tree intt;
12826           if (!vector_types_compatible_elements_p (type0, type1))
12827             {
12828               error_at (location, "comparing vectors with different "
12829                                   "element types");
12830               return error_mark_node;
12831             }
12832
12833           if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12834                         TYPE_VECTOR_SUBPARTS (type1)))
12835             {
12836               error_at (location, "comparing vectors with different "
12837                                   "number of elements");
12838               return error_mark_node;
12839             }
12840
12841           /* It's not precisely specified how the usual arithmetic
12842              conversions apply to the vector types.  Here, we use
12843              the unsigned type if one of the operands is signed and
12844              the other one is unsigned.  */
12845           if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12846             {
12847               if (!TYPE_UNSIGNED (type0))
12848                 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12849               else
12850                 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12851               warning_at (location, OPT_Wsign_compare, "comparison between "
12852                           "types %qT and %qT", type0, type1);
12853             }
12854
12855           /* Always construct signed integer vector type.  */
12856           intt = c_common_type_for_size (GET_MODE_BITSIZE
12857                                          (SCALAR_TYPE_MODE
12858                                           (TREE_TYPE (type0))), 0);
12859           if (!intt)
12860             {
12861               error_at (location, "could not find an integer type "
12862                                   "of the same size as %qT",
12863                         TREE_TYPE (type0));
12864               return error_mark_node;
12865             }
12866           result_type = build_opaque_vector_type (intt,
12867                                                   TYPE_VECTOR_SUBPARTS (type0));
12868           converted = 1;
12869           ret = build_vec_cmp (resultcode, result_type, op0, op1);
12870           goto return_build_binary_op;
12871         }
12872       build_type = integer_type_node;
12873       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12874            || code0 == FIXED_POINT_TYPE)
12875           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12876               || code1 == FIXED_POINT_TYPE))
12877         short_compare = 1;
12878       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12879         {
12880           addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12881           addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12882           addr_space_t as_common;
12883
12884           if (comp_target_types (location, type0, type1))
12885             {
12886               result_type = common_pointer_type (type0, type1);
12887               if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12888                   != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12889                 pedwarn_c99 (location, OPT_Wpedantic,
12890                              "comparison of complete and incomplete pointers");
12891               else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12892                 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12893                          "ordered comparisons of pointers to functions");
12894               else if (null_pointer_constant_p (orig_op0)
12895                        || null_pointer_constant_p (orig_op1))
12896                 warning_at (location, OPT_Wextra,
12897                             "ordered comparison of pointer with null pointer");
12898
12899             }
12900           else if (!addr_space_superset (as0, as1, &as_common))
12901             {
12902               error_at (location, "comparison of pointers to "
12903                         "disjoint address spaces");
12904               return error_mark_node;
12905             }
12906           else
12907             {
12908               int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12909               result_type = build_pointer_type
12910                               (build_qualified_type (void_type_node, qual));
12911               pedwarn (location, 0,
12912                        "comparison of distinct pointer types lacks a cast");
12913             }
12914         }
12915       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12916         {
12917           result_type = type0;
12918           if (pedantic)
12919             pedwarn (location, OPT_Wpedantic,
12920                      "ordered comparison of pointer with integer zero");
12921           else if (extra_warnings)
12922             warning_at (location, OPT_Wextra,
12923                         "ordered comparison of pointer with integer zero");
12924         }
12925       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12926         {
12927           result_type = type1;
12928           if (pedantic)
12929             pedwarn (location, OPT_Wpedantic,
12930                      "ordered comparison of pointer with integer zero");
12931           else if (extra_warnings)
12932             warning_at (location, OPT_Wextra,
12933                         "ordered comparison of pointer with integer zero");
12934         }
12935       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12936         {
12937           result_type = type0;
12938           pedwarn (location, 0, "comparison between pointer and integer");
12939         }
12940       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12941         {
12942           result_type = type1;
12943           pedwarn (location, 0, "comparison between pointer and integer");
12944         }
12945
12946       if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12947           && current_function_decl != NULL_TREE
12948           && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12949         {
12950           op0 = save_expr (op0);
12951           op1 = save_expr (op1);
12952
12953           tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12954           instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12955         }
12956
12957       if ((C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op0))
12958            || truth_value_p (TREE_CODE (orig_op0)))
12959           ^ (C_BOOLEAN_TYPE_P (TREE_TYPE (orig_op1))
12960              || truth_value_p (TREE_CODE (orig_op1))))
12961         maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12962       break;
12963
12964     case MIN_EXPR:
12965     case MAX_EXPR:
12966       /* Used for OpenMP atomics.  */
12967       gcc_assert (flag_openmp);
12968       common = 1;
12969       break;
12970
12971     default:
12972       gcc_unreachable ();
12973     }
12974
12975   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12976     return error_mark_node;
12977
12978   if (gnu_vector_type_p (type0)
12979       && gnu_vector_type_p (type1)
12980       && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12981           || !vector_types_compatible_elements_p (type0, type1)))
12982     {
12983       gcc_rich_location richloc (location);
12984       maybe_range_label_for_tree_type_mismatch
12985         label_for_op0 (orig_op0, orig_op1),
12986         label_for_op1 (orig_op1, orig_op0);
12987       richloc.maybe_add_expr (orig_op0, &label_for_op0);
12988       richloc.maybe_add_expr (orig_op1, &label_for_op1);
12989       binary_op_error (&richloc, code, type0, type1);
12990       return error_mark_node;
12991     }
12992
12993   if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12994        || code0 == FIXED_POINT_TYPE
12995        || gnu_vector_type_p (type0))
12996       &&
12997       (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12998        || code1 == FIXED_POINT_TYPE
12999        || gnu_vector_type_p (type1)))
13000     {
13001       bool first_complex = (code0 == COMPLEX_TYPE);
13002       bool second_complex = (code1 == COMPLEX_TYPE);
13003       int none_complex = (!first_complex && !second_complex);
13004
13005       if (shorten || common || short_compare)
13006         {
13007           result_type = c_common_type (type0, type1);
13008           do_warn_double_promotion (result_type, type0, type1,
13009                                     "implicit conversion from %qT to %qT "
13010                                     "to match other operand of binary "
13011                                     "expression",
13012                                     location);
13013           if (result_type == error_mark_node)
13014             return error_mark_node;
13015         }
13016
13017       if (first_complex != second_complex
13018           && (code == PLUS_EXPR
13019               || code == MINUS_EXPR
13020               || code == MULT_EXPR
13021               || (code == TRUNC_DIV_EXPR && first_complex))
13022           && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
13023           && flag_signed_zeros)
13024         {
13025           /* An operation on mixed real/complex operands must be
13026              handled specially, but the language-independent code can
13027              more easily optimize the plain complex arithmetic if
13028              -fno-signed-zeros.  */
13029           tree real_type = TREE_TYPE (result_type);
13030           tree real, imag;
13031           if (type0 != orig_type0 || type1 != orig_type1)
13032             {
13033               gcc_assert (may_need_excess_precision && common);
13034               semantic_result_type = c_common_type (orig_type0, orig_type1);
13035             }
13036           if (first_complex)
13037             {
13038               if (TREE_TYPE (op0) != result_type)
13039                 op0 = convert_and_check (location, result_type, op0);
13040               if (TREE_TYPE (op1) != real_type)
13041                 op1 = convert_and_check (location, real_type, op1);
13042             }
13043           else
13044             {
13045               if (TREE_TYPE (op0) != real_type)
13046                 op0 = convert_and_check (location, real_type, op0);
13047               if (TREE_TYPE (op1) != result_type)
13048                 op1 = convert_and_check (location, result_type, op1);
13049             }
13050           if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13051             return error_mark_node;
13052           if (first_complex)
13053             {
13054               op0 = save_expr (op0);
13055               real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
13056                                      op0, true);
13057               imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
13058                                      op0, true);
13059               switch (code)
13060                 {
13061                 case MULT_EXPR:
13062                 case TRUNC_DIV_EXPR:
13063                   op1 = save_expr (op1);
13064                   imag = build2 (resultcode, real_type, imag, op1);
13065                   /* Fall through.  */
13066                 case PLUS_EXPR:
13067                 case MINUS_EXPR:
13068                   real = build2 (resultcode, real_type, real, op1);
13069                   break;
13070                 default:
13071                   gcc_unreachable();
13072                 }
13073             }
13074           else
13075             {
13076               op1 = save_expr (op1);
13077               real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
13078                                      op1, true);
13079               imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
13080                                      op1, true);
13081               switch (code)
13082                 {
13083                 case MULT_EXPR:
13084                   op0 = save_expr (op0);
13085                   imag = build2 (resultcode, real_type, op0, imag);
13086                   /* Fall through.  */
13087                 case PLUS_EXPR:
13088                   real = build2 (resultcode, real_type, op0, real);
13089                   break;
13090                 case MINUS_EXPR:
13091                   real = build2 (resultcode, real_type, op0, real);
13092                   imag = build1 (NEGATE_EXPR, real_type, imag);
13093                   break;
13094                 default:
13095                   gcc_unreachable();
13096                 }
13097             }
13098           ret = build2 (COMPLEX_EXPR, result_type, real, imag);
13099           goto return_build_binary_op;
13100         }
13101
13102       /* For certain operations (which identify themselves by shorten != 0)
13103          if both args were extended from the same smaller type,
13104          do the arithmetic in that type and then extend.
13105
13106          shorten !=0 and !=1 indicates a bitwise operation.
13107          For them, this optimization is safe only if
13108          both args are zero-extended or both are sign-extended.
13109          Otherwise, we might change the result.
13110          Eg, (short)-1 | (unsigned short)-1 is (int)-1
13111          but calculated in (unsigned short) it would be (unsigned short)-1.  */
13112
13113       if (shorten && none_complex)
13114         {
13115           final_type = result_type;
13116           result_type = shorten_binary_op (result_type, op0, op1,
13117                                            shorten == -1);
13118         }
13119
13120       /* Shifts can be shortened if shifting right.  */
13121
13122       if (short_shift)
13123         {
13124           int unsigned_arg;
13125           tree arg0 = get_narrower (op0, &unsigned_arg);
13126
13127           final_type = result_type;
13128
13129           if (arg0 == op0 && final_type == TREE_TYPE (op0))
13130             unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
13131
13132           if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
13133               && tree_int_cst_sgn (op1) > 0
13134               /* We can shorten only if the shift count is less than the
13135                  number of bits in the smaller type size.  */
13136               && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
13137               /* We cannot drop an unsigned shift after sign-extension.  */
13138               && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
13139             {
13140               /* Do an unsigned shift if the operand was zero-extended.  */
13141               result_type
13142                 = c_common_signed_or_unsigned_type (unsigned_arg,
13143                                                     TREE_TYPE (arg0));
13144               /* Convert value-to-be-shifted to that type.  */
13145               if (TREE_TYPE (op0) != result_type)
13146                 op0 = convert (result_type, op0);
13147               converted = 1;
13148             }
13149         }
13150
13151       /* Comparison operations are shortened too but differently.
13152          They identify themselves by setting short_compare = 1.  */
13153
13154       if (short_compare)
13155         {
13156           /* Don't write &op0, etc., because that would prevent op0
13157              from being kept in a register.
13158              Instead, make copies of the our local variables and
13159              pass the copies by reference, then copy them back afterward.  */
13160           tree xop0 = op0, xop1 = op1, xresult_type = result_type;
13161           enum tree_code xresultcode = resultcode;
13162           tree val
13163             = shorten_compare (location, &xop0, &xop1, &xresult_type,
13164                                &xresultcode);
13165
13166           if (val != NULL_TREE)
13167             {
13168               ret = val;
13169               goto return_build_binary_op;
13170             }
13171
13172           op0 = xop0, op1 = xop1;
13173           converted = 1;
13174           resultcode = xresultcode;
13175
13176           if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
13177             {
13178               bool op0_maybe_const = true;
13179               bool op1_maybe_const = true;
13180               tree orig_op0_folded, orig_op1_folded;
13181
13182               if (in_late_binary_op)
13183                 {
13184                   orig_op0_folded = orig_op0;
13185                   orig_op1_folded = orig_op1;
13186                 }
13187               else
13188                 {
13189                   /* Fold for the sake of possible warnings, as in
13190                      build_conditional_expr.  This requires the
13191                      "original" values to be folded, not just op0 and
13192                      op1.  */
13193                   c_inhibit_evaluation_warnings++;
13194                   op0 = c_fully_fold (op0, require_constant_value,
13195                                       &op0_maybe_const);
13196                   op1 = c_fully_fold (op1, require_constant_value,
13197                                       &op1_maybe_const);
13198                   c_inhibit_evaluation_warnings--;
13199                   orig_op0_folded = c_fully_fold (orig_op0,
13200                                                   require_constant_value,
13201                                                   NULL);
13202                   orig_op1_folded = c_fully_fold (orig_op1,
13203                                                   require_constant_value,
13204                                                   NULL);
13205                 }
13206
13207               if (warn_sign_compare)
13208                 warn_for_sign_compare (location, orig_op0_folded,
13209                                        orig_op1_folded, op0, op1,
13210                                        result_type, resultcode);
13211               if (!in_late_binary_op && !int_operands)
13212                 {
13213                   if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
13214                     op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
13215                   if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
13216                     op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
13217                 }
13218             }
13219         }
13220     }
13221
13222   /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
13223      If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
13224      Then the expression will be built.
13225      It will be given type FINAL_TYPE if that is nonzero;
13226      otherwise, it will be given type RESULT_TYPE.  */
13227
13228   if (!result_type)
13229     {
13230       /* Favor showing any expression locations that are available. */
13231       op_location_t oploc (location, UNKNOWN_LOCATION);
13232       binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
13233       binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
13234       return error_mark_node;
13235     }
13236
13237   if (build_type == NULL_TREE)
13238     {
13239       build_type = result_type;
13240       if ((type0 != orig_type0 || type1 != orig_type1)
13241           && !boolean_op)
13242         {
13243           gcc_assert (may_need_excess_precision && common);
13244           semantic_result_type = c_common_type (orig_type0, orig_type1);
13245         }
13246     }
13247
13248   if (!converted)
13249     {
13250       op0 = ep_convert_and_check (location, result_type, op0,
13251                                   semantic_result_type);
13252       op1 = ep_convert_and_check (location, result_type, op1,
13253                                   semantic_result_type);
13254
13255       /* This can happen if one operand has a vector type, and the other
13256          has a different type.  */
13257       if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
13258         return error_mark_node;
13259     }
13260
13261   if (sanitize_flags_p ((SANITIZE_SHIFT
13262                          | SANITIZE_DIVIDE
13263                          | SANITIZE_FLOAT_DIVIDE
13264                          | SANITIZE_SI_OVERFLOW))
13265       && current_function_decl != NULL_TREE
13266       && (doing_div_or_mod || doing_shift)
13267       && !require_constant_value)
13268     {
13269       /* OP0 and/or OP1 might have side-effects.  */
13270       op0 = save_expr (op0);
13271       op1 = save_expr (op1);
13272       op0 = c_fully_fold (op0, false, NULL);
13273       op1 = c_fully_fold (op1, false, NULL);
13274       if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
13275                                                   | SANITIZE_FLOAT_DIVIDE
13276                                                   | SANITIZE_SI_OVERFLOW))))
13277         instrument_expr = ubsan_instrument_division (location, op0, op1);
13278       else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
13279         instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
13280     }
13281
13282   /* Treat expressions in initializers specially as they can't trap.  */
13283   if (int_const_or_overflow)
13284     ret = (require_constant_value
13285            ? fold_build2_initializer_loc (location, resultcode, build_type,
13286                                           op0, op1)
13287            : fold_build2_loc (location, resultcode, build_type, op0, op1));
13288   else
13289     ret = build2 (resultcode, build_type, op0, op1);
13290   if (final_type != NULL_TREE)
13291     ret = convert (final_type, ret);
13292
13293  return_build_binary_op:
13294   gcc_assert (ret != error_mark_node);
13295   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
13296     ret = (int_operands
13297            ? note_integer_operands (ret)
13298            : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
13299   else if (TREE_CODE (ret) != INTEGER_CST && int_operands
13300            && !in_late_binary_op)
13301     ret = note_integer_operands (ret);
13302   protected_set_expr_location (ret, location);
13303
13304   if (instrument_expr != NULL)
13305     ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
13306                        instrument_expr, ret);
13307
13308   if (semantic_result_type)
13309     ret = build1_loc (location, EXCESS_PRECISION_EXPR,
13310                       semantic_result_type, ret);
13311
13312   return ret;
13313 }
13314
13315
13316 /* Convert EXPR to be a truth-value, validating its type for this
13317    purpose.  LOCATION is the source location for the expression.  */
13318
13319 tree
13320 c_objc_common_truthvalue_conversion (location_t location, tree expr)
13321 {
13322   bool int_const, int_operands;
13323
13324   switch (TREE_CODE (TREE_TYPE (expr)))
13325     {
13326     case ARRAY_TYPE:
13327       error_at (location, "used array that cannot be converted to pointer where scalar is required");
13328       return error_mark_node;
13329
13330     case RECORD_TYPE:
13331       error_at (location, "used struct type value where scalar is required");
13332       return error_mark_node;
13333
13334     case UNION_TYPE:
13335       error_at (location, "used union type value where scalar is required");
13336       return error_mark_node;
13337
13338     case VOID_TYPE:
13339       error_at (location, "void value not ignored as it ought to be");
13340       return error_mark_node;
13341
13342     case POINTER_TYPE:
13343       if (reject_gcc_builtin (expr))
13344         return error_mark_node;
13345       break;
13346
13347     case FUNCTION_TYPE:
13348       gcc_unreachable ();
13349
13350     case VECTOR_TYPE:
13351       error_at (location, "used vector type where scalar is required");
13352       return error_mark_node;
13353
13354     default:
13355       break;
13356     }
13357
13358   int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
13359   int_operands = EXPR_INT_CONST_OPERANDS (expr);
13360   if (int_operands && TREE_CODE (expr) != INTEGER_CST)
13361     {
13362       expr = remove_c_maybe_const_expr (expr);
13363       expr = build2 (NE_EXPR, integer_type_node, expr,
13364                      convert (TREE_TYPE (expr), integer_zero_node));
13365       expr = note_integer_operands (expr);
13366     }
13367   else
13368     /* ??? Should we also give an error for vectors rather than leaving
13369        those to give errors later?  */
13370     expr = c_common_truthvalue_conversion (location, expr);
13371
13372   if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
13373     {
13374       if (TREE_OVERFLOW (expr))
13375         return expr;
13376       else
13377         return note_integer_operands (expr);
13378     }
13379   if (TREE_CODE (expr) == INTEGER_CST && !int_const)
13380     return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
13381   return expr;
13382 }
13383 \f
13384
13385 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
13386    required.  */
13387
13388 tree
13389 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
13390 {
13391   if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
13392     {
13393       tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
13394       /* Executing a compound literal inside a function reinitializes
13395          it.  */
13396       if (!TREE_STATIC (decl))
13397         *se = true;
13398       return decl;
13399     }
13400   else
13401     return expr;
13402 }
13403 \f
13404 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13405    statement.  LOC is the location of the construct.  */
13406
13407 tree
13408 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
13409                         tree clauses)
13410 {
13411   body = c_end_compound_stmt (loc, body, true);
13412
13413   tree stmt = make_node (code);
13414   TREE_TYPE (stmt) = void_type_node;
13415   OMP_BODY (stmt) = body;
13416   OMP_CLAUSES (stmt) = clauses;
13417   SET_EXPR_LOCATION (stmt, loc);
13418
13419   return add_stmt (stmt);
13420 }
13421
13422 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13423    statement.  LOC is the location of the OACC_DATA.  */
13424
13425 tree
13426 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13427 {
13428   tree stmt;
13429
13430   block = c_end_compound_stmt (loc, block, true);
13431
13432   stmt = make_node (OACC_DATA);
13433   TREE_TYPE (stmt) = void_type_node;
13434   OACC_DATA_CLAUSES (stmt) = clauses;
13435   OACC_DATA_BODY (stmt) = block;
13436   SET_EXPR_LOCATION (stmt, loc);
13437
13438   return add_stmt (stmt);
13439 }
13440
13441 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13442    statement.  LOC is the location of the OACC_HOST_DATA.  */
13443
13444 tree
13445 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13446 {
13447   tree stmt;
13448
13449   block = c_end_compound_stmt (loc, block, true);
13450
13451   stmt = make_node (OACC_HOST_DATA);
13452   TREE_TYPE (stmt) = void_type_node;
13453   OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13454   OACC_HOST_DATA_BODY (stmt) = block;
13455   SET_EXPR_LOCATION (stmt, loc);
13456
13457   return add_stmt (stmt);
13458 }
13459
13460 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
13461
13462 tree
13463 c_begin_omp_parallel (void)
13464 {
13465   tree block;
13466
13467   keep_next_level ();
13468   block = c_begin_compound_stmt (true);
13469
13470   return block;
13471 }
13472
13473 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13474    statement.  LOC is the location of the OMP_PARALLEL.  */
13475
13476 tree
13477 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13478 {
13479   tree stmt;
13480
13481   block = c_end_compound_stmt (loc, block, true);
13482
13483   stmt = make_node (OMP_PARALLEL);
13484   TREE_TYPE (stmt) = void_type_node;
13485   OMP_PARALLEL_CLAUSES (stmt) = clauses;
13486   OMP_PARALLEL_BODY (stmt) = block;
13487   SET_EXPR_LOCATION (stmt, loc);
13488
13489   return add_stmt (stmt);
13490 }
13491
13492 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
13493
13494 tree
13495 c_begin_omp_task (void)
13496 {
13497   tree block;
13498
13499   keep_next_level ();
13500   block = c_begin_compound_stmt (true);
13501
13502   return block;
13503 }
13504
13505 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13506    statement.  LOC is the location of the #pragma.  */
13507
13508 tree
13509 c_finish_omp_task (location_t loc, tree clauses, tree block)
13510 {
13511   tree stmt;
13512
13513   block = c_end_compound_stmt (loc, block, true);
13514
13515   stmt = make_node (OMP_TASK);
13516   TREE_TYPE (stmt) = void_type_node;
13517   OMP_TASK_CLAUSES (stmt) = clauses;
13518   OMP_TASK_BODY (stmt) = block;
13519   SET_EXPR_LOCATION (stmt, loc);
13520
13521   return add_stmt (stmt);
13522 }
13523
13524 /* Generate GOMP_cancel call for #pragma omp cancel.  */
13525
13526 void
13527 c_finish_omp_cancel (location_t loc, tree clauses)
13528 {
13529   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13530   int mask = 0;
13531   if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13532     mask = 1;
13533   else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13534     mask = 2;
13535   else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13536     mask = 4;
13537   else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13538     mask = 8;
13539   else
13540     {
13541       error_at (loc, "%<#pragma omp cancel%> must specify one of "
13542                      "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13543                      "clauses");
13544       return;
13545     }
13546   tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13547   if (ifc != NULL_TREE)
13548     {
13549       if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13550           && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13551         error_at (OMP_CLAUSE_LOCATION (ifc),
13552                   "expected %<cancel%> %<if%> clause modifier");
13553       else
13554         {
13555           tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13556           if (ifc2 != NULL_TREE)
13557             {
13558               gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13559                           && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13560                           && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13561               error_at (OMP_CLAUSE_LOCATION (ifc2),
13562                         "expected %<cancel%> %<if%> clause modifier");
13563             }
13564         }
13565
13566       tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13567       ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13568                              boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13569                              build_zero_cst (type));
13570     }
13571   else
13572     ifc = boolean_true_node;
13573   tree stmt = build_call_expr_loc (loc, fn, 2,
13574                                    build_int_cst (integer_type_node, mask),
13575                                    ifc);
13576   add_stmt (stmt);
13577 }
13578
13579 /* Generate GOMP_cancellation_point call for
13580    #pragma omp cancellation point.  */
13581
13582 void
13583 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13584 {
13585   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13586   int mask = 0;
13587   if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13588     mask = 1;
13589   else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13590     mask = 2;
13591   else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13592     mask = 4;
13593   else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13594     mask = 8;
13595   else
13596     {
13597       error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13598                      "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13599                      "clauses");
13600       return;
13601     }
13602   tree stmt = build_call_expr_loc (loc, fn, 1,
13603                                    build_int_cst (integer_type_node, mask));
13604   add_stmt (stmt);
13605 }
13606
13607 /* Helper function for handle_omp_array_sections.  Called recursively
13608    to handle multiple array-section-subscripts.  C is the clause,
13609    T current expression (initially OMP_CLAUSE_DECL), which is either
13610    a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13611    expression if specified, TREE_VALUE length expression if specified,
13612    TREE_CHAIN is what it has been specified after, or some decl.
13613    TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13614    set to true if any of the array-section-subscript could have length
13615    of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13616    first array-section-subscript which is known not to have length
13617    of one.  Given say:
13618    map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13619    FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13620    all are or may have length of 1, array-section-subscript [:2] is the
13621    first one known not to have length 1.  For array-section-subscript
13622    <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13623    0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13624    can if MAYBE_ZERO_LEN is false.  MAYBE_ZERO_LEN will be true in the above
13625    case though, as some lengths could be zero.  */
13626
13627 static tree
13628 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13629                              bool &maybe_zero_len, unsigned int &first_non_one,
13630                              enum c_omp_region_type ort)
13631 {
13632   tree ret, low_bound, length, type;
13633   if (TREE_CODE (t) != TREE_LIST)
13634     {
13635       if (error_operand_p (t))
13636         return error_mark_node;
13637       ret = t;
13638       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13639           && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13640           && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13641         {
13642           error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13643                     t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13644           return error_mark_node;
13645         }
13646       while (TREE_CODE (t) == INDIRECT_REF)
13647         {
13648           t = TREE_OPERAND (t, 0);
13649           STRIP_NOPS (t);
13650           if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13651             t = TREE_OPERAND (t, 0);
13652         }
13653       while (TREE_CODE (t) == COMPOUND_EXPR)
13654         {
13655           t = TREE_OPERAND (t, 1);
13656           STRIP_NOPS (t);
13657         }
13658       if (TREE_CODE (t) == COMPONENT_REF
13659           && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13660               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13661               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13662         {
13663           if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13664             {
13665               error_at (OMP_CLAUSE_LOCATION (c),
13666                         "bit-field %qE in %qs clause",
13667                         t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13668               return error_mark_node;
13669             }
13670           while (TREE_CODE (t) == COMPONENT_REF)
13671             {
13672               if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13673                 {
13674                   error_at (OMP_CLAUSE_LOCATION (c),
13675                             "%qE is a member of a union", t);
13676                   return error_mark_node;
13677                 }
13678               t = TREE_OPERAND (t, 0);
13679               while (TREE_CODE (t) == MEM_REF
13680                      || TREE_CODE (t) == INDIRECT_REF
13681                      || TREE_CODE (t) == ARRAY_REF)
13682                 {
13683                   t = TREE_OPERAND (t, 0);
13684                   STRIP_NOPS (t);
13685                   if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13686                     t = TREE_OPERAND (t, 0);
13687                 }
13688               if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13689                 {
13690                   if (maybe_ne (mem_ref_offset (t), 0))
13691                     error_at (OMP_CLAUSE_LOCATION (c),
13692                               "cannot dereference %qE in %qs clause", t,
13693                               omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13694                   else
13695                     t = TREE_OPERAND (t, 0);
13696                 }
13697             }
13698         }
13699       if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13700         {
13701           if (DECL_P (t))
13702             error_at (OMP_CLAUSE_LOCATION (c),
13703                       "%qD is not a variable in %qs clause", t,
13704                       omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13705           else
13706             error_at (OMP_CLAUSE_LOCATION (c),
13707                       "%qE is not a variable in %qs clause", t,
13708                       omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13709           return error_mark_node;
13710         }
13711       else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13712                && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13713                && TYPE_ATOMIC (TREE_TYPE (t)))
13714         {
13715           error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13716                     t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13717           return error_mark_node;
13718         }
13719       else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13720                && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13721                && VAR_P (t)
13722                && DECL_THREAD_LOCAL_P (t))
13723         {
13724           error_at (OMP_CLAUSE_LOCATION (c),
13725                     "%qD is threadprivate variable in %qs clause", t,
13726                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13727           return error_mark_node;
13728         }
13729       if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13730            || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13731           && TYPE_ATOMIC (TREE_TYPE (t))
13732           && POINTER_TYPE_P (TREE_TYPE (t)))
13733         {
13734           /* If the array section is pointer based and the pointer
13735              itself is _Atomic qualified, we need to atomically load
13736              the pointer.  */
13737           c_expr expr;
13738           memset (&expr, 0, sizeof (expr));
13739           expr.value = ret;
13740           expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13741                                            expr, false, false);
13742           ret = expr.value;
13743         }
13744       return ret;
13745     }
13746
13747   ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13748                                      maybe_zero_len, first_non_one, ort);
13749   if (ret == error_mark_node || ret == NULL_TREE)
13750     return ret;
13751
13752   type = TREE_TYPE (ret);
13753   low_bound = TREE_PURPOSE (t);
13754   length = TREE_VALUE (t);
13755
13756   if (low_bound == error_mark_node || length == error_mark_node)
13757     return error_mark_node;
13758
13759   if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13760     {
13761       error_at (OMP_CLAUSE_LOCATION (c),
13762                 "low bound %qE of array section does not have integral type",
13763                 low_bound);
13764       return error_mark_node;
13765     }
13766   if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13767     {
13768       error_at (OMP_CLAUSE_LOCATION (c),
13769                 "length %qE of array section does not have integral type",
13770                 length);
13771       return error_mark_node;
13772     }
13773   if (low_bound
13774       && TREE_CODE (low_bound) == INTEGER_CST
13775       && TYPE_PRECISION (TREE_TYPE (low_bound))
13776          > TYPE_PRECISION (sizetype))
13777     low_bound = fold_convert (sizetype, low_bound);
13778   if (length
13779       && TREE_CODE (length) == INTEGER_CST
13780       && TYPE_PRECISION (TREE_TYPE (length))
13781          > TYPE_PRECISION (sizetype))
13782     length = fold_convert (sizetype, length);
13783   if (low_bound == NULL_TREE)
13784     low_bound = integer_zero_node;
13785   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13786       && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13787           || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13788     {
13789       if (length != integer_one_node)
13790         {
13791           error_at (OMP_CLAUSE_LOCATION (c),
13792                     "expected single pointer in %qs clause",
13793                     user_omp_clause_code_name (c, ort == C_ORT_ACC));
13794           return error_mark_node;
13795         }
13796     }
13797   if (length != NULL_TREE)
13798     {
13799       if (!integer_nonzerop (length))
13800         {
13801           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13802               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13803               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13804               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13805               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13806             {
13807               if (integer_zerop (length))
13808                 {
13809                   error_at (OMP_CLAUSE_LOCATION (c),
13810                             "zero length array section in %qs clause",
13811                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13812                   return error_mark_node;
13813                 }
13814             }
13815           else
13816             maybe_zero_len = true;
13817         }
13818       if (first_non_one == types.length ()
13819           && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13820         first_non_one++;
13821     }
13822   if (TREE_CODE (type) == ARRAY_TYPE)
13823     {
13824       if (length == NULL_TREE
13825           && (TYPE_DOMAIN (type) == NULL_TREE
13826               || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13827         {
13828           error_at (OMP_CLAUSE_LOCATION (c),
13829                     "for unknown bound array type length expression must "
13830                     "be specified");
13831           return error_mark_node;
13832         }
13833       if (TREE_CODE (low_bound) == INTEGER_CST
13834           && tree_int_cst_sgn (low_bound) == -1)
13835         {
13836           error_at (OMP_CLAUSE_LOCATION (c),
13837                     "negative low bound in array section in %qs clause",
13838                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13839           return error_mark_node;
13840         }
13841       if (length != NULL_TREE
13842           && TREE_CODE (length) == INTEGER_CST
13843           && tree_int_cst_sgn (length) == -1)
13844         {
13845           error_at (OMP_CLAUSE_LOCATION (c),
13846                     "negative length in array section in %qs clause",
13847                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13848           return error_mark_node;
13849         }
13850       if (TYPE_DOMAIN (type)
13851           && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13852           && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13853                         == INTEGER_CST)
13854         {
13855           tree size
13856             = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13857           size = size_binop (PLUS_EXPR, size, size_one_node);
13858           if (TREE_CODE (low_bound) == INTEGER_CST)
13859             {
13860               if (tree_int_cst_lt (size, low_bound))
13861                 {
13862                   error_at (OMP_CLAUSE_LOCATION (c),
13863                             "low bound %qE above array section size "
13864                             "in %qs clause", low_bound,
13865                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13866                   return error_mark_node;
13867                 }
13868               if (tree_int_cst_equal (size, low_bound))
13869                 {
13870                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13871                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13872                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13873                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13874                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13875                     {
13876                       error_at (OMP_CLAUSE_LOCATION (c),
13877                                 "zero length array section in %qs clause",
13878                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13879                       return error_mark_node;
13880                     }
13881                   maybe_zero_len = true;
13882                 }
13883               else if (length == NULL_TREE
13884                        && first_non_one == types.length ()
13885                        && tree_int_cst_equal
13886                             (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13887                              low_bound))
13888                 first_non_one++;
13889             }
13890           else if (length == NULL_TREE)
13891             {
13892               if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13893                   && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13894                   && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13895                   && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13896                   && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13897                 maybe_zero_len = true;
13898               if (first_non_one == types.length ())
13899                 first_non_one++;
13900             }
13901           if (length && TREE_CODE (length) == INTEGER_CST)
13902             {
13903               if (tree_int_cst_lt (size, length))
13904                 {
13905                   error_at (OMP_CLAUSE_LOCATION (c),
13906                             "length %qE above array section size "
13907                             "in %qs clause", length,
13908                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13909                   return error_mark_node;
13910                 }
13911               if (TREE_CODE (low_bound) == INTEGER_CST)
13912                 {
13913                   tree lbpluslen
13914                     = size_binop (PLUS_EXPR,
13915                                   fold_convert (sizetype, low_bound),
13916                                   fold_convert (sizetype, length));
13917                   if (TREE_CODE (lbpluslen) == INTEGER_CST
13918                       && tree_int_cst_lt (size, lbpluslen))
13919                     {
13920                       error_at (OMP_CLAUSE_LOCATION (c),
13921                                 "high bound %qE above array section size "
13922                                 "in %qs clause", lbpluslen,
13923                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13924                       return error_mark_node;
13925                     }
13926                 }
13927             }
13928         }
13929       else if (length == NULL_TREE)
13930         {
13931           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13932               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13933               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13934               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13935               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13936             maybe_zero_len = true;
13937           if (first_non_one == types.length ())
13938             first_non_one++;
13939         }
13940
13941       /* For [lb:] we will need to evaluate lb more than once.  */
13942       if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13943         {
13944           tree lb = save_expr (low_bound);
13945           if (lb != low_bound)
13946             {
13947               TREE_PURPOSE (t) = lb;
13948               low_bound = lb;
13949             }
13950         }
13951     }
13952   else if (TREE_CODE (type) == POINTER_TYPE)
13953     {
13954       if (length == NULL_TREE)
13955         {
13956           if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13957             error_at (OMP_CLAUSE_LOCATION (c),
13958                       "for array function parameter length expression "
13959                       "must be specified");
13960           else
13961             error_at (OMP_CLAUSE_LOCATION (c),
13962                       "for pointer type length expression must be specified");
13963           return error_mark_node;
13964         }
13965       if (length != NULL_TREE
13966           && TREE_CODE (length) == INTEGER_CST
13967           && tree_int_cst_sgn (length) == -1)
13968         {
13969           error_at (OMP_CLAUSE_LOCATION (c),
13970                     "negative length in array section in %qs clause",
13971                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13972           return error_mark_node;
13973         }
13974       /* If there is a pointer type anywhere but in the very first
13975          array-section-subscript, the array section could be non-contiguous.  */
13976       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13977           && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13978           && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13979         {
13980           /* If any prior dimension has a non-one length, then deem this
13981              array section as non-contiguous.  */
13982           for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
13983                d = TREE_CHAIN (d))
13984             {
13985               tree d_length = TREE_VALUE (d);
13986               if (d_length == NULL_TREE || !integer_onep (d_length))
13987                 {
13988                   error_at (OMP_CLAUSE_LOCATION (c),
13989                             "array section is not contiguous in %qs clause",
13990                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13991                   return error_mark_node;
13992                 }
13993             }
13994         }
13995     }
13996   else
13997     {
13998       error_at (OMP_CLAUSE_LOCATION (c),
13999                 "%qE does not have pointer or array type", ret);
14000       return error_mark_node;
14001     }
14002   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
14003     types.safe_push (TREE_TYPE (ret));
14004   /* We will need to evaluate lb more than once.  */
14005   tree lb = save_expr (low_bound);
14006   if (lb != low_bound)
14007     {
14008       TREE_PURPOSE (t) = lb;
14009       low_bound = lb;
14010     }
14011   ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
14012   return ret;
14013 }
14014
14015 /* Handle array sections for clause C.  */
14016
14017 static bool
14018 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
14019 {
14020   bool maybe_zero_len = false;
14021   unsigned int first_non_one = 0;
14022   auto_vec<tree, 10> types;
14023   tree *tp = &OMP_CLAUSE_DECL (c);
14024   if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14025        || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14026       && TREE_CODE (*tp) == TREE_LIST
14027       && TREE_PURPOSE (*tp)
14028       && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
14029     tp = &TREE_VALUE (*tp);
14030   tree first = handle_omp_array_sections_1 (c, *tp, types,
14031                                             maybe_zero_len, first_non_one,
14032                                             ort);
14033   if (first == error_mark_node)
14034     return true;
14035   if (first == NULL_TREE)
14036     return false;
14037   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14038       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
14039     {
14040       tree t = *tp;
14041       tree tem = NULL_TREE;
14042       /* Need to evaluate side effects in the length expressions
14043          if any.  */
14044       while (TREE_CODE (t) == TREE_LIST)
14045         {
14046           if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
14047             {
14048               if (tem == NULL_TREE)
14049                 tem = TREE_VALUE (t);
14050               else
14051                 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
14052                               TREE_VALUE (t), tem);
14053             }
14054           t = TREE_CHAIN (t);
14055         }
14056       if (tem)
14057         first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
14058       first = c_fully_fold (first, false, NULL, true);
14059       *tp = first;
14060     }
14061   else
14062     {
14063       unsigned int num = types.length (), i;
14064       tree t, side_effects = NULL_TREE, size = NULL_TREE;
14065       tree condition = NULL_TREE;
14066
14067       if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
14068         maybe_zero_len = true;
14069
14070       for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
14071            t = TREE_CHAIN (t))
14072         {
14073           tree low_bound = TREE_PURPOSE (t);
14074           tree length = TREE_VALUE (t);
14075
14076           i--;
14077           if (low_bound
14078               && TREE_CODE (low_bound) == INTEGER_CST
14079               && TYPE_PRECISION (TREE_TYPE (low_bound))
14080                  > TYPE_PRECISION (sizetype))
14081             low_bound = fold_convert (sizetype, low_bound);
14082           if (length
14083               && TREE_CODE (length) == INTEGER_CST
14084               && TYPE_PRECISION (TREE_TYPE (length))
14085                  > TYPE_PRECISION (sizetype))
14086             length = fold_convert (sizetype, length);
14087           if (low_bound == NULL_TREE)
14088             low_bound = integer_zero_node;
14089           if (!maybe_zero_len && i > first_non_one)
14090             {
14091               if (integer_nonzerop (low_bound))
14092                 goto do_warn_noncontiguous;
14093               if (length != NULL_TREE
14094                   && TREE_CODE (length) == INTEGER_CST
14095                   && TYPE_DOMAIN (types[i])
14096                   && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
14097                   && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
14098                      == INTEGER_CST)
14099                 {
14100                   tree size;
14101                   size = size_binop (PLUS_EXPR,
14102                                      TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14103                                      size_one_node);
14104                   if (!tree_int_cst_equal (length, size))
14105                     {
14106                      do_warn_noncontiguous:
14107                       error_at (OMP_CLAUSE_LOCATION (c),
14108                                 "array section is not contiguous in %qs "
14109                                 "clause",
14110                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14111                       return true;
14112                     }
14113                 }
14114               if (length != NULL_TREE
14115                   && TREE_SIDE_EFFECTS (length))
14116                 {
14117                   if (side_effects == NULL_TREE)
14118                     side_effects = length;
14119                   else
14120                     side_effects = build2 (COMPOUND_EXPR,
14121                                            TREE_TYPE (side_effects),
14122                                            length, side_effects);
14123                 }
14124             }
14125           else
14126             {
14127               tree l;
14128
14129               if (i > first_non_one
14130                   && ((length && integer_nonzerop (length))
14131                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14132                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14133                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
14134                 continue;
14135               if (length)
14136                 l = fold_convert (sizetype, length);
14137               else
14138                 {
14139                   l = size_binop (PLUS_EXPR,
14140                                   TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
14141                                   size_one_node);
14142                   l = size_binop (MINUS_EXPR, l,
14143                                   fold_convert (sizetype, low_bound));
14144                 }
14145               if (i > first_non_one)
14146                 {
14147                   l = fold_build2 (NE_EXPR, boolean_type_node, l,
14148                                    size_zero_node);
14149                   if (condition == NULL_TREE)
14150                     condition = l;
14151                   else
14152                     condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
14153                                              l, condition);
14154                 }
14155               else if (size == NULL_TREE)
14156                 {
14157                   size = size_in_bytes (TREE_TYPE (types[i]));
14158                   tree eltype = TREE_TYPE (types[num - 1]);
14159                   while (TREE_CODE (eltype) == ARRAY_TYPE)
14160                     eltype = TREE_TYPE (eltype);
14161                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14162                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14163                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14164                     {
14165                       if (integer_zerop (size)
14166                           || integer_zerop (size_in_bytes (eltype)))
14167                         {
14168                           error_at (OMP_CLAUSE_LOCATION (c),
14169                                     "zero length array section in %qs clause",
14170                                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14171                           return error_mark_node;
14172                         }
14173                       size = size_binop (EXACT_DIV_EXPR, size,
14174                                          size_in_bytes (eltype));
14175                     }
14176                   size = size_binop (MULT_EXPR, size, l);
14177                   if (condition)
14178                     size = fold_build3 (COND_EXPR, sizetype, condition,
14179                                         size, size_zero_node);
14180                 }
14181               else
14182                 size = size_binop (MULT_EXPR, size, l);
14183             }
14184         }
14185       if (side_effects)
14186         size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
14187       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14188           || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14189           || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
14190         {
14191           size = size_binop (MINUS_EXPR, size, size_one_node);
14192           size = c_fully_fold (size, false, NULL);
14193           size = save_expr (size);
14194           tree index_type = build_index_type (size);
14195           tree eltype = TREE_TYPE (first);
14196           while (TREE_CODE (eltype) == ARRAY_TYPE)
14197             eltype = TREE_TYPE (eltype);
14198           tree type = build_array_type (eltype, index_type);
14199           tree ptype = build_pointer_type (eltype);
14200           if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14201             t = build_fold_addr_expr (t);
14202           tree t2 = build_fold_addr_expr (first);
14203           t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14204                                  ptrdiff_type_node, t2);
14205           t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14206                                 ptrdiff_type_node, t2,
14207                                 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14208                                                   ptrdiff_type_node, t));
14209           t2 = c_fully_fold (t2, false, NULL);
14210           if (tree_fits_shwi_p (t2))
14211             t = build2 (MEM_REF, type, t,
14212                         build_int_cst (ptype, tree_to_shwi (t2)));
14213           else
14214             {
14215               t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
14216               t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
14217                               TREE_TYPE (t), t, t2);
14218               t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
14219             }
14220           OMP_CLAUSE_DECL (c) = t;
14221           return false;
14222         }
14223       first = c_fully_fold (first, false, NULL);
14224       OMP_CLAUSE_DECL (c) = first;
14225       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
14226         return false;
14227       if (size)
14228         size = c_fully_fold (size, false, NULL);
14229       OMP_CLAUSE_SIZE (c) = size;
14230       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14231           || (TREE_CODE (t) == COMPONENT_REF
14232               && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
14233         return false;
14234       gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
14235       switch (OMP_CLAUSE_MAP_KIND (c))
14236         {
14237         case GOMP_MAP_ALLOC:
14238         case GOMP_MAP_IF_PRESENT:
14239         case GOMP_MAP_TO:
14240         case GOMP_MAP_FROM:
14241         case GOMP_MAP_TOFROM:
14242         case GOMP_MAP_ALWAYS_TO:
14243         case GOMP_MAP_ALWAYS_FROM:
14244         case GOMP_MAP_ALWAYS_TOFROM:
14245         case GOMP_MAP_RELEASE:
14246         case GOMP_MAP_DELETE:
14247         case GOMP_MAP_FORCE_TO:
14248         case GOMP_MAP_FORCE_FROM:
14249         case GOMP_MAP_FORCE_TOFROM:
14250         case GOMP_MAP_FORCE_PRESENT:
14251           OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
14252           break;
14253         default:
14254           break;
14255         }
14256       tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
14257       if (TREE_CODE (t) == COMPONENT_REF)
14258         OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
14259       else
14260         OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
14261       OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
14262       if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
14263           && !c_mark_addressable (t))
14264         return false;
14265       OMP_CLAUSE_DECL (c2) = t;
14266       t = build_fold_addr_expr (first);
14267       t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
14268       tree ptr = OMP_CLAUSE_DECL (c2);
14269       if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
14270         ptr = build_fold_addr_expr (ptr);
14271       t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14272                            ptrdiff_type_node, t,
14273                            fold_convert_loc (OMP_CLAUSE_LOCATION (c),
14274                                              ptrdiff_type_node, ptr));
14275       t = c_fully_fold (t, false, NULL);
14276       OMP_CLAUSE_SIZE (c2) = t;
14277       OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
14278       OMP_CLAUSE_CHAIN (c) = c2;
14279     }
14280   return false;
14281 }
14282
14283 /* Helper function of finish_omp_clauses.  Clone STMT as if we were making
14284    an inline call.  But, remap
14285    the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
14286    and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL.  */
14287
14288 static tree
14289 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
14290                  tree decl, tree placeholder)
14291 {
14292   copy_body_data id;
14293   hash_map<tree, tree> decl_map;
14294
14295   decl_map.put (omp_decl1, placeholder);
14296   decl_map.put (omp_decl2, decl);
14297   memset (&id, 0, sizeof (id));
14298   id.src_fn = DECL_CONTEXT (omp_decl1);
14299   id.dst_fn = current_function_decl;
14300   id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
14301   id.decl_map = &decl_map;
14302
14303   id.copy_decl = copy_decl_no_change;
14304   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
14305   id.transform_new_cfg = true;
14306   id.transform_return_to_modify = false;
14307   id.eh_lp_nr = 0;
14308   walk_tree (&stmt, copy_tree_body_r, &id, NULL);
14309   return stmt;
14310 }
14311
14312 /* Helper function of c_finish_omp_clauses, called via walk_tree.
14313    Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP.  */
14314
14315 static tree
14316 c_find_omp_placeholder_r (tree *tp, int *, void *data)
14317 {
14318   if (*tp == (tree) data)
14319     return *tp;
14320   return NULL_TREE;
14321 }
14322
14323 /* Similarly, but also walk aggregate fields.  */
14324
14325 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
14326
14327 static tree
14328 c_find_omp_var_r (tree *tp, int *, void *data)
14329 {
14330   if (*tp == ((struct c_find_omp_var_s *) data)->var)
14331     return *tp;
14332   if (RECORD_OR_UNION_TYPE_P (*tp))
14333     {
14334       tree field;
14335       hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
14336
14337       for (field = TYPE_FIELDS (*tp); field;
14338            field = DECL_CHAIN (field))
14339         if (TREE_CODE (field) == FIELD_DECL)
14340           {
14341             tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
14342                                   c_find_omp_var_r, data, pset);
14343             if (ret)
14344               return ret;
14345             ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
14346             if (ret)
14347               return ret;
14348             ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
14349                              pset);
14350             if (ret)
14351               return ret;
14352             ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
14353             if (ret)
14354               return ret;
14355           }
14356     }
14357   else if (INTEGRAL_TYPE_P (*tp))
14358     return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
14359                       ((struct c_find_omp_var_s *) data)->pset);
14360   return NULL_TREE;
14361 }
14362
14363 /* Finish OpenMP iterators ITER.  Return true if they are errorneous
14364    and clauses containing them should be removed.  */
14365
14366 static bool
14367 c_omp_finish_iterators (tree iter)
14368 {
14369   bool ret = false;
14370   for (tree it = iter; it; it = TREE_CHAIN (it))
14371     {
14372       tree var = TREE_VEC_ELT (it, 0);
14373       tree begin = TREE_VEC_ELT (it, 1);
14374       tree end = TREE_VEC_ELT (it, 2);
14375       tree step = TREE_VEC_ELT (it, 3);
14376       tree orig_step;
14377       tree type = TREE_TYPE (var);
14378       location_t loc = DECL_SOURCE_LOCATION (var);
14379       if (type == error_mark_node)
14380         {
14381           ret = true;
14382           continue;
14383         }
14384       if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14385         {
14386           error_at (loc, "iterator %qD has neither integral nor pointer type",
14387                     var);
14388           ret = true;
14389           continue;
14390         }
14391       else if (TYPE_ATOMIC (type))
14392         {
14393           error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
14394           ret = true;
14395           continue;
14396         }
14397       else if (TYPE_READONLY (type))
14398         {
14399           error_at (loc, "iterator %qD has const qualified type", var);
14400           ret = true;
14401           continue;
14402         }
14403       else if (step == error_mark_node
14404                || TREE_TYPE (step) == error_mark_node)
14405         {
14406           ret = true;
14407           continue;
14408         }
14409       else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14410         {
14411           error_at (EXPR_LOC_OR_LOC (step, loc),
14412                     "iterator step with non-integral type");
14413           ret = true;
14414           continue;
14415         }
14416       begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14417       end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14418       orig_step = save_expr (c_fully_fold (step, false, NULL));
14419       tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14420       step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14421       if (POINTER_TYPE_P (type))
14422         {
14423           begin = save_expr (begin);
14424           step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14425           step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14426                                   fold_convert (sizetype, step),
14427                                   fold_convert (sizetype, begin));
14428           step = fold_convert (ssizetype, step);
14429         }
14430       if (integer_zerop (step))
14431         {
14432           error_at (loc, "iterator %qD has zero step", var);
14433           ret = true;
14434           continue;
14435         }
14436
14437       if (begin == error_mark_node
14438           || end == error_mark_node
14439           || step == error_mark_node
14440           || orig_step == error_mark_node)
14441         {
14442           ret = true;
14443           continue;
14444         }
14445       hash_set<tree> pset;
14446       tree it2;
14447       for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14448         {
14449           tree var2 = TREE_VEC_ELT (it2, 0);
14450           tree begin2 = TREE_VEC_ELT (it2, 1);
14451           tree end2 = TREE_VEC_ELT (it2, 2);
14452           tree step2 = TREE_VEC_ELT (it2, 3);
14453           tree type2 = TREE_TYPE (var2);
14454           location_t loc2 = DECL_SOURCE_LOCATION (var2);
14455           struct c_find_omp_var_s data = { var, &pset };
14456           if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14457             {
14458               error_at (loc2,
14459                         "type of iterator %qD refers to outer iterator %qD",
14460                         var2, var);
14461               break;
14462             }
14463           else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14464             {
14465               error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14466                         "begin expression refers to outer iterator %qD", var);
14467               break;
14468             }
14469           else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14470             {
14471               error_at (EXPR_LOC_OR_LOC (end2, loc2),
14472                         "end expression refers to outer iterator %qD", var);
14473               break;
14474             }
14475           else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14476             {
14477               error_at (EXPR_LOC_OR_LOC (step2, loc2),
14478                         "step expression refers to outer iterator %qD", var);
14479               break;
14480             }
14481         }
14482       if (it2)
14483         {
14484           ret = true;
14485           continue;
14486         }
14487       TREE_VEC_ELT (it, 1) = begin;
14488       TREE_VEC_ELT (it, 2) = end;
14489       TREE_VEC_ELT (it, 3) = step;
14490       TREE_VEC_ELT (it, 4) = orig_step;
14491     }
14492   return ret;
14493 }
14494
14495 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14496    Return true if an error has been detected.  */
14497
14498 static bool
14499 c_oacc_check_attachments (tree c)
14500 {
14501   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14502     return false;
14503
14504   /* OpenACC attach / detach clauses must be pointers.  */
14505   if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14506       || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14507     {
14508       tree t = OMP_CLAUSE_DECL (c);
14509
14510       while (TREE_CODE (t) == TREE_LIST)
14511         t = TREE_CHAIN (t);
14512
14513       if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14514         {
14515           error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14516                     user_omp_clause_code_name (c, true));
14517           return true;
14518         }
14519     }
14520
14521   return false;
14522 }
14523
14524 /* For all elements of CLAUSES, validate them against their constraints.
14525    Remove any elements from the list that are invalid.  */
14526
14527 tree
14528 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14529 {
14530   bitmap_head generic_head, firstprivate_head, lastprivate_head;
14531   bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14532   bitmap_head oacc_reduction_head, is_on_device_head;
14533   tree c, t, type, *pc;
14534   tree simdlen = NULL_TREE, safelen = NULL_TREE;
14535   bool branch_seen = false;
14536   bool copyprivate_seen = false;
14537   bool mergeable_seen = false;
14538   tree *detach_seen = NULL;
14539   bool linear_variable_step_check = false;
14540   tree *nowait_clause = NULL;
14541   tree ordered_clause = NULL_TREE;
14542   tree schedule_clause = NULL_TREE;
14543   bool oacc_async = false;
14544   bool indir_component_ref_p = false;
14545   tree last_iterators = NULL_TREE;
14546   bool last_iterators_remove = false;
14547   tree *nogroup_seen = NULL;
14548   tree *order_clause = NULL;
14549   /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14550      has been seen, -2 if mixed inscan/normal reduction diagnosed.  */
14551   int reduction_seen = 0;
14552   bool allocate_seen = false;
14553   bool implicit_moved = false;
14554   bool target_in_reduction_seen = false;
14555
14556   bitmap_obstack_initialize (NULL);
14557   bitmap_initialize (&generic_head, &bitmap_default_obstack);
14558   bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14559   bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14560   bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14561   /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead.  */
14562   bitmap_initialize (&map_head, &bitmap_default_obstack);
14563   bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14564   bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14565   /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14566      instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head.  */
14567   bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14568   bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14569
14570   if (ort & C_ORT_ACC)
14571     for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14572       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14573         {
14574           oacc_async = true;
14575           break;
14576         }
14577
14578   tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
14579
14580   for (pc = &clauses, c = clauses; c ; c = *pc)
14581     {
14582       bool remove = false;
14583       bool need_complete = false;
14584       bool need_implicitly_determined = false;
14585
14586       /* We've reached the end of a list of expanded nodes.  Reset the group
14587          start pointer.  */
14588       if (c == grp_sentinel)
14589         grp_start_p = NULL;
14590
14591       switch (OMP_CLAUSE_CODE (c))
14592         {
14593         case OMP_CLAUSE_SHARED:
14594           need_implicitly_determined = true;
14595           goto check_dup_generic;
14596
14597         case OMP_CLAUSE_PRIVATE:
14598           need_complete = true;
14599           need_implicitly_determined = true;
14600           goto check_dup_generic;
14601
14602         case OMP_CLAUSE_REDUCTION:
14603           if (reduction_seen == 0)
14604             reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14605           else if (reduction_seen != -2
14606                    && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14607                                          ? -1 : 1))
14608             {
14609               error_at (OMP_CLAUSE_LOCATION (c),
14610                         "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14611                         "on the same construct");
14612               reduction_seen = -2;
14613             }
14614           /* FALLTHRU */
14615         case OMP_CLAUSE_IN_REDUCTION:
14616         case OMP_CLAUSE_TASK_REDUCTION:
14617           need_implicitly_determined = true;
14618           t = OMP_CLAUSE_DECL (c);
14619           if (TREE_CODE (t) == TREE_LIST)
14620             {
14621               if (handle_omp_array_sections (c, ort))
14622                 {
14623                   remove = true;
14624                   break;
14625                 }
14626
14627               t = OMP_CLAUSE_DECL (c);
14628               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14629                   && OMP_CLAUSE_REDUCTION_INSCAN (c))
14630                 {
14631                   error_at (OMP_CLAUSE_LOCATION (c),
14632                             "%<inscan%> %<reduction%> clause with array "
14633                             "section");
14634                   remove = true;
14635                   break;
14636                 }
14637             }
14638           t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14639           if (t == error_mark_node)
14640             {
14641               remove = true;
14642               break;
14643             }
14644           if (oacc_async)
14645             c_mark_addressable (t);
14646           type = TREE_TYPE (t);
14647           if (TREE_CODE (t) == MEM_REF)
14648             type = TREE_TYPE (type);
14649           if (TREE_CODE (type) == ARRAY_TYPE)
14650             {
14651               tree oatype = type;
14652               gcc_assert (TREE_CODE (t) != MEM_REF);
14653               while (TREE_CODE (type) == ARRAY_TYPE)
14654                 type = TREE_TYPE (type);
14655               if (integer_zerop (TYPE_SIZE_UNIT (type)))
14656                 {
14657                   error_at (OMP_CLAUSE_LOCATION (c),
14658                             "%qD in %<reduction%> clause is a zero size array",
14659                             t);
14660                   remove = true;
14661                   break;
14662                 }
14663               tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14664                                       TYPE_SIZE_UNIT (type));
14665               if (integer_zerop (size))
14666                 {
14667                   error_at (OMP_CLAUSE_LOCATION (c),
14668                             "%qD in %<reduction%> clause is a zero size array",
14669                             t);
14670                   remove = true;
14671                   break;
14672                 }
14673               size = size_binop (MINUS_EXPR, size, size_one_node);
14674               size = save_expr (size);
14675               tree index_type = build_index_type (size);
14676               tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14677                                              index_type);
14678               atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14679               tree ptype = build_pointer_type (type);
14680               if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14681                 t = build_fold_addr_expr (t);
14682               t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14683               OMP_CLAUSE_DECL (c) = t;
14684             }
14685           if (TYPE_ATOMIC (type))
14686             {
14687               error_at (OMP_CLAUSE_LOCATION (c),
14688                         "%<_Atomic%> %qE in %<reduction%> clause", t);
14689               remove = true;
14690               break;
14691             }
14692           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14693               || OMP_CLAUSE_REDUCTION_TASK (c))
14694             {
14695               /* Disallow zero sized or potentially zero sized task
14696                  reductions.  */
14697               if (integer_zerop (TYPE_SIZE_UNIT (type)))
14698                 {
14699                   error_at (OMP_CLAUSE_LOCATION (c),
14700                             "zero sized type %qT in %qs clause", type,
14701                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14702                   remove = true;
14703                   break;
14704                 }
14705               else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14706                 {
14707                   error_at (OMP_CLAUSE_LOCATION (c),
14708                             "variable sized type %qT in %qs clause", type,
14709                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14710                   remove = true;
14711                   break;
14712                 }
14713             }
14714           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14715               && (FLOAT_TYPE_P (type)
14716                   || TREE_CODE (type) == COMPLEX_TYPE))
14717             {
14718               enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14719               const char *r_name = NULL;
14720
14721               switch (r_code)
14722                 {
14723                 case PLUS_EXPR:
14724                 case MULT_EXPR:
14725                 case MINUS_EXPR:
14726                 case TRUTH_ANDIF_EXPR:
14727                 case TRUTH_ORIF_EXPR:
14728                   break;
14729                 case MIN_EXPR:
14730                   if (TREE_CODE (type) == COMPLEX_TYPE)
14731                     r_name = "min";
14732                   break;
14733                 case MAX_EXPR:
14734                   if (TREE_CODE (type) == COMPLEX_TYPE)
14735                     r_name = "max";
14736                   break;
14737                 case BIT_AND_EXPR:
14738                   r_name = "&";
14739                   break;
14740                 case BIT_XOR_EXPR:
14741                   r_name = "^";
14742                   break;
14743                 case BIT_IOR_EXPR:
14744                   r_name = "|";
14745                   break;
14746                 default:
14747                   gcc_unreachable ();
14748                 }
14749               if (r_name)
14750                 {
14751                   error_at (OMP_CLAUSE_LOCATION (c),
14752                             "%qE has invalid type for %<reduction(%s)%>",
14753                             t, r_name);
14754                   remove = true;
14755                   break;
14756                 }
14757             }
14758           else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14759             {
14760               error_at (OMP_CLAUSE_LOCATION (c),
14761                         "user defined reduction not found for %qE", t);
14762               remove = true;
14763               break;
14764             }
14765           else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14766             {
14767               tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14768               type = TYPE_MAIN_VARIANT (type);
14769               tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14770                                              VAR_DECL, NULL_TREE, type);
14771               tree decl_placeholder = NULL_TREE;
14772               OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14773               DECL_ARTIFICIAL (placeholder) = 1;
14774               DECL_IGNORED_P (placeholder) = 1;
14775               if (TREE_CODE (t) == MEM_REF)
14776                 {
14777                   decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14778                                                  VAR_DECL, NULL_TREE, type);
14779                   OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14780                   DECL_ARTIFICIAL (decl_placeholder) = 1;
14781                   DECL_IGNORED_P (decl_placeholder) = 1;
14782                 }
14783               if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14784                 c_mark_addressable (placeholder);
14785               if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14786                 c_mark_addressable (decl_placeholder ? decl_placeholder
14787                                     : OMP_CLAUSE_DECL (c));
14788               OMP_CLAUSE_REDUCTION_MERGE (c)
14789                 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14790                                    TREE_VEC_ELT (list, 0),
14791                                    TREE_VEC_ELT (list, 1),
14792                                    decl_placeholder ? decl_placeholder
14793                                    : OMP_CLAUSE_DECL (c), placeholder);
14794               OMP_CLAUSE_REDUCTION_MERGE (c)
14795                 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14796                               void_type_node, NULL_TREE,
14797                               OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14798               TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14799               if (TREE_VEC_LENGTH (list) == 6)
14800                 {
14801                   if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14802                     c_mark_addressable (decl_placeholder ? decl_placeholder
14803                                         : OMP_CLAUSE_DECL (c));
14804                   if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14805                     c_mark_addressable (placeholder);
14806                   tree init = TREE_VEC_ELT (list, 5);
14807                   if (init == error_mark_node)
14808                     init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14809                   OMP_CLAUSE_REDUCTION_INIT (c)
14810                     = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14811                                        TREE_VEC_ELT (list, 3),
14812                                        decl_placeholder ? decl_placeholder
14813                                        : OMP_CLAUSE_DECL (c), placeholder);
14814                   if (TREE_VEC_ELT (list, 5) == error_mark_node)
14815                     {
14816                       tree v = decl_placeholder ? decl_placeholder : t;
14817                       OMP_CLAUSE_REDUCTION_INIT (c)
14818                         = build2 (INIT_EXPR, TREE_TYPE (v), v,
14819                                   OMP_CLAUSE_REDUCTION_INIT (c));
14820                     }
14821                   if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14822                                  c_find_omp_placeholder_r,
14823                                  placeholder, NULL))
14824                     OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14825                 }
14826               else
14827                 {
14828                   tree init;
14829                   tree v = decl_placeholder ? decl_placeholder : t;
14830                   if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14831                     init = build_constructor (TREE_TYPE (v), NULL);
14832                   else
14833                     init = fold_convert (TREE_TYPE (v), integer_zero_node);
14834                   OMP_CLAUSE_REDUCTION_INIT (c)
14835                     = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14836                 }
14837               OMP_CLAUSE_REDUCTION_INIT (c)
14838                 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14839                               void_type_node, NULL_TREE,
14840                                OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14841               TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14842             }
14843           if (TREE_CODE (t) == MEM_REF)
14844             {
14845               if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14846                   || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14847                      != INTEGER_CST)
14848                 {
14849                   sorry ("variable length element type in array "
14850                          "%<reduction%> clause");
14851                   remove = true;
14852                   break;
14853                 }
14854               t = TREE_OPERAND (t, 0);
14855               if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14856                 t = TREE_OPERAND (t, 0);
14857               if (TREE_CODE (t) == ADDR_EXPR)
14858                 t = TREE_OPERAND (t, 0);
14859             }
14860           goto check_dup_generic_t;
14861
14862         case OMP_CLAUSE_COPYPRIVATE:
14863           copyprivate_seen = true;
14864           if (nowait_clause)
14865             {
14866               error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14867                         "%<nowait%> clause must not be used together "
14868                         "with %<copyprivate%>");
14869               *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14870               nowait_clause = NULL;
14871             }
14872           goto check_dup_generic;
14873
14874         case OMP_CLAUSE_COPYIN:
14875           t = OMP_CLAUSE_DECL (c);
14876           if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14877             {
14878               error_at (OMP_CLAUSE_LOCATION (c),
14879                         "%qE must be %<threadprivate%> for %<copyin%>", t);
14880               remove = true;
14881               break;
14882             }
14883           goto check_dup_generic;
14884
14885         case OMP_CLAUSE_LINEAR:
14886           if (ort != C_ORT_OMP_DECLARE_SIMD)
14887             need_implicitly_determined = true;
14888           t = OMP_CLAUSE_DECL (c);
14889           if (ort != C_ORT_OMP_DECLARE_SIMD
14890               && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
14891               && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
14892             {
14893               error_at (OMP_CLAUSE_LOCATION (c),
14894                         "modifier should not be specified in %<linear%> "
14895                         "clause on %<simd%> or %<for%> constructs when not "
14896                         "using OpenMP 5.2 modifiers");
14897               OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14898             }
14899           if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14900               && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14901             {
14902               error_at (OMP_CLAUSE_LOCATION (c),
14903                         "linear clause applied to non-integral non-pointer "
14904                         "variable with type %qT", TREE_TYPE (t));
14905               remove = true;
14906               break;
14907             }
14908           if (TYPE_ATOMIC (TREE_TYPE (t)))
14909             {
14910               error_at (OMP_CLAUSE_LOCATION (c),
14911                         "%<_Atomic%> %qD in %<linear%> clause", t);
14912               remove = true;
14913               break;
14914             }
14915           if (ort == C_ORT_OMP_DECLARE_SIMD)
14916             {
14917               tree s = OMP_CLAUSE_LINEAR_STEP (c);
14918               if (TREE_CODE (s) == PARM_DECL)
14919                 {
14920                   OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14921                   /* map_head bitmap is used as uniform_head if
14922                      declare_simd.  */
14923                   if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14924                     linear_variable_step_check = true;
14925                   goto check_dup_generic;
14926                 }
14927               if (TREE_CODE (s) != INTEGER_CST)
14928                 {
14929                   error_at (OMP_CLAUSE_LOCATION (c),
14930                             "%<linear%> clause step %qE is neither constant "
14931                             "nor a parameter", s);
14932                   remove = true;
14933                   break;
14934                 }
14935             }
14936           if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14937             {
14938               tree s = OMP_CLAUSE_LINEAR_STEP (c);
14939               s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14940                                    OMP_CLAUSE_DECL (c), s);
14941               s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14942                                    sizetype, fold_convert (sizetype, s),
14943                                    fold_convert
14944                                      (sizetype, OMP_CLAUSE_DECL (c)));
14945               if (s == error_mark_node)
14946                 s = size_one_node;
14947               OMP_CLAUSE_LINEAR_STEP (c) = s;
14948             }
14949           else
14950             OMP_CLAUSE_LINEAR_STEP (c)
14951               = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14952           goto check_dup_generic;
14953
14954         check_dup_generic:
14955           t = OMP_CLAUSE_DECL (c);
14956         check_dup_generic_t:
14957           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14958             {
14959               error_at (OMP_CLAUSE_LOCATION (c),
14960                         "%qE is not a variable in clause %qs", t,
14961                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14962               remove = true;
14963             }
14964           else if ((ort == C_ORT_ACC
14965                     && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14966                    || (ort == C_ORT_OMP
14967                        && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14968                            || (OMP_CLAUSE_CODE (c)
14969                                == OMP_CLAUSE_USE_DEVICE_ADDR)))
14970                    || (ort == C_ORT_OMP_TARGET
14971                        && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14972             {
14973               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14974                   && (bitmap_bit_p (&generic_head, DECL_UID (t))
14975                       || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14976                 {
14977                   error_at (OMP_CLAUSE_LOCATION (c),
14978                             "%qD appears more than once in data-sharing "
14979                             "clauses", t);
14980                   remove = true;
14981                   break;
14982                 }
14983               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14984                 target_in_reduction_seen = true;
14985               if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14986                 {
14987                   error_at (OMP_CLAUSE_LOCATION (c),
14988                             ort == C_ORT_ACC
14989                             ? "%qD appears more than once in reduction clauses"
14990                             : "%qD appears more than once in data clauses",
14991                             t);
14992                   remove = true;
14993                 }
14994               else
14995                 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14996             }
14997           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14998                    || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14999                    || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
15000                    || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15001             {
15002               error_at (OMP_CLAUSE_LOCATION (c),
15003                         "%qE appears more than once in data clauses", t);
15004               remove = true;
15005             }
15006           else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15007                     || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
15008                     || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15009                    && bitmap_bit_p (&map_head, DECL_UID (t)))
15010             {
15011               if (ort == C_ORT_ACC)
15012                 error_at (OMP_CLAUSE_LOCATION (c),
15013                           "%qD appears more than once in data clauses", t);
15014               else
15015                 error_at (OMP_CLAUSE_LOCATION (c),
15016                           "%qD appears both in data and map clauses", t);
15017               remove = true;
15018             }
15019           else
15020             bitmap_set_bit (&generic_head, DECL_UID (t));
15021           break;
15022
15023         case OMP_CLAUSE_FIRSTPRIVATE:
15024           if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
15025             {
15026             move_implicit:
15027               implicit_moved = true;
15028               /* Move firstprivate and map clauses with
15029                  OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
15030                  clauses chain.  */
15031               tree cl1 = NULL_TREE, cl2 = NULL_TREE;
15032               tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
15033               while (*pc1)
15034                 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
15035                     && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
15036                   {
15037                     *pc3 = *pc1;
15038                     pc3 = &OMP_CLAUSE_CHAIN (*pc3);
15039                     *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15040                   }
15041                 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
15042                          && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
15043                   {
15044                     *pc2 = *pc1;
15045                     pc2 = &OMP_CLAUSE_CHAIN (*pc2);
15046                     *pc1 = OMP_CLAUSE_CHAIN (*pc1);
15047                   }
15048                 else
15049                   pc1 = &OMP_CLAUSE_CHAIN (*pc1);
15050               *pc3 = NULL;
15051               *pc2 = cl2;
15052               *pc1 = cl1;
15053               continue;
15054             }
15055           t = OMP_CLAUSE_DECL (c);
15056           need_complete = true;
15057           need_implicitly_determined = true;
15058           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15059             {
15060               error_at (OMP_CLAUSE_LOCATION (c),
15061                         "%qE is not a variable in clause %<firstprivate%>", t);
15062               remove = true;
15063             }
15064           else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15065                    && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
15066                    && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15067             remove = true;
15068           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15069                    || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15070                    || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15071             {
15072               error_at (OMP_CLAUSE_LOCATION (c),
15073                         "%qE appears more than once in data clauses", t);
15074               remove = true;
15075             }
15076           else if (bitmap_bit_p (&map_head, DECL_UID (t)))
15077             {
15078               if (ort == C_ORT_ACC)
15079                 error_at (OMP_CLAUSE_LOCATION (c),
15080                           "%qD appears more than once in data clauses", t);
15081               else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
15082                        && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
15083                 /* Silently drop the clause.  */;
15084               else
15085                 error_at (OMP_CLAUSE_LOCATION (c),
15086                           "%qD appears both in data and map clauses", t);
15087               remove = true;
15088             }
15089           else
15090             bitmap_set_bit (&firstprivate_head, DECL_UID (t));
15091           break;
15092
15093         case OMP_CLAUSE_LASTPRIVATE:
15094           t = OMP_CLAUSE_DECL (c);
15095           need_complete = true;
15096           need_implicitly_determined = true;
15097           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15098             {
15099               error_at (OMP_CLAUSE_LOCATION (c),
15100                         "%qE is not a variable in clause %<lastprivate%>", t);
15101               remove = true;
15102             }
15103           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
15104                    || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
15105             {
15106               error_at (OMP_CLAUSE_LOCATION (c),
15107                      "%qE appears more than once in data clauses", t);
15108               remove = true;
15109             }
15110           else
15111             bitmap_set_bit (&lastprivate_head, DECL_UID (t));
15112           break;
15113
15114         case OMP_CLAUSE_ALIGNED:
15115           t = OMP_CLAUSE_DECL (c);
15116           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15117             {
15118               error_at (OMP_CLAUSE_LOCATION (c),
15119                         "%qE is not a variable in %<aligned%> clause", t);
15120               remove = true;
15121             }
15122           else if (!POINTER_TYPE_P (TREE_TYPE (t))
15123                    && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15124             {
15125               error_at (OMP_CLAUSE_LOCATION (c),
15126                         "%qE in %<aligned%> clause is neither a pointer nor "
15127                         "an array", t);
15128               remove = true;
15129             }
15130           else if (TYPE_ATOMIC (TREE_TYPE (t)))
15131             {
15132               error_at (OMP_CLAUSE_LOCATION (c),
15133                         "%<_Atomic%> %qD in %<aligned%> clause", t);
15134               remove = true;
15135               break;
15136             }
15137           else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15138             {
15139               error_at (OMP_CLAUSE_LOCATION (c),
15140                         "%qE appears more than once in %<aligned%> clauses",
15141                         t);
15142               remove = true;
15143             }
15144           else
15145             bitmap_set_bit (&aligned_head, DECL_UID (t));
15146           break;
15147
15148         case OMP_CLAUSE_NONTEMPORAL:
15149           t = OMP_CLAUSE_DECL (c);
15150           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15151             {
15152               error_at (OMP_CLAUSE_LOCATION (c),
15153                         "%qE is not a variable in %<nontemporal%> clause", t);
15154               remove = true;
15155             }
15156           else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15157             {
15158               error_at (OMP_CLAUSE_LOCATION (c),
15159                         "%qE appears more than once in %<nontemporal%> "
15160                         "clauses", t);
15161               remove = true;
15162             }
15163           else
15164             bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
15165           break;
15166
15167         case OMP_CLAUSE_ALLOCATE:
15168           t = OMP_CLAUSE_DECL (c);
15169           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15170             {
15171               error_at (OMP_CLAUSE_LOCATION (c),
15172                         "%qE is not a variable in %<allocate%> clause", t);
15173               remove = true;
15174             }
15175           else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
15176             {
15177               warning_at (OMP_CLAUSE_LOCATION (c), 0,
15178                           "%qE appears more than once in %<allocate%> clauses",
15179                           t);
15180               remove = true;
15181             }
15182           else
15183             {
15184               bitmap_set_bit (&aligned_head, DECL_UID (t));
15185               if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
15186                 allocate_seen = true;
15187             }
15188           break;
15189
15190         case OMP_CLAUSE_DOACROSS:
15191           t = OMP_CLAUSE_DECL (c);
15192           if (t == NULL_TREE)
15193             break;
15194           if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
15195             {
15196               gcc_assert (TREE_CODE (t) == TREE_LIST);
15197               for (; t; t = TREE_CHAIN (t))
15198                 {
15199                   tree decl = TREE_VALUE (t);
15200                   if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
15201                     {
15202                       tree offset = TREE_PURPOSE (t);
15203                       bool neg = wi::neg_p (wi::to_wide (offset));
15204                       offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
15205                       tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
15206                                                  neg ? MINUS_EXPR : PLUS_EXPR,
15207                                                  decl, offset);
15208                       t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
15209                                             sizetype,
15210                                             fold_convert (sizetype, t2),
15211                                             fold_convert (sizetype, decl));
15212                       if (t2 == error_mark_node)
15213                         {
15214                           remove = true;
15215                           break;
15216                         }
15217                       TREE_PURPOSE (t) = t2;
15218                     }
15219                 }
15220               break;
15221             }
15222           gcc_unreachable ();
15223         case OMP_CLAUSE_DEPEND:
15224         case OMP_CLAUSE_AFFINITY:
15225           t = OMP_CLAUSE_DECL (c);
15226           if (TREE_CODE (t) == TREE_LIST
15227               && TREE_PURPOSE (t)
15228               && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
15229             {
15230               if (TREE_PURPOSE (t) != last_iterators)
15231                 last_iterators_remove
15232                   = c_omp_finish_iterators (TREE_PURPOSE (t));
15233               last_iterators = TREE_PURPOSE (t);
15234               t = TREE_VALUE (t);
15235               if (last_iterators_remove)
15236                 t = error_mark_node;
15237             }
15238           else
15239             last_iterators = NULL_TREE;
15240           if (TREE_CODE (t) == TREE_LIST)
15241             {
15242               if (handle_omp_array_sections (c, ort))
15243                 remove = true;
15244               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15245                        && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15246                 {
15247                   error_at (OMP_CLAUSE_LOCATION (c),
15248                             "%<depend%> clause with %<depobj%> dependence "
15249                             "type on array section");
15250                   remove = true;
15251                 }
15252               break;
15253             }
15254           if (t == error_mark_node)
15255             remove = true;
15256           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15257                    && t == ridpointers[RID_OMP_ALL_MEMORY])
15258             {
15259               if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
15260                   && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
15261                 {
15262                   error_at (OMP_CLAUSE_LOCATION (c),
15263                             "%<omp_all_memory%> used with %<depend%> kind "
15264                             "other than %<out%> or %<inout%>");
15265                   remove = true;
15266                 }
15267             }
15268           else if (!lvalue_p (t))
15269             {
15270               error_at (OMP_CLAUSE_LOCATION (c),
15271                         "%qE is not lvalue expression nor array section in "
15272                         "%qs clause", t,
15273                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15274               remove = true;
15275             }
15276           else if (TREE_CODE (t) == COMPONENT_REF
15277                    && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
15278             {
15279               gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15280                           || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
15281               error_at (OMP_CLAUSE_LOCATION (c),
15282                         "bit-field %qE in %qs clause", t,
15283                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15284               remove = true;
15285             }
15286           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15287                    && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
15288             {
15289               if (!c_omp_depend_t_p (TREE_TYPE (t)))
15290                 {
15291                   error_at (OMP_CLAUSE_LOCATION (c),
15292                             "%qE does not have %<omp_depend_t%> type in "
15293                             "%<depend%> clause with %<depobj%> dependence "
15294                             "type", t);
15295                   remove = true;
15296                 }
15297             }
15298           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
15299                    && c_omp_depend_t_p (TREE_TYPE (t)))
15300             {
15301               error_at (OMP_CLAUSE_LOCATION (c),
15302                         "%qE should not have %<omp_depend_t%> type in "
15303                         "%<depend%> clause with dependence type other than "
15304                         "%<depobj%>", t);
15305               remove = true;
15306             }
15307           if (!remove)
15308             {
15309               if (t == ridpointers[RID_OMP_ALL_MEMORY])
15310                 t = null_pointer_node;
15311               else
15312                 {
15313                   tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
15314                                               ADDR_EXPR, t, false);
15315                   if (addr == error_mark_node)
15316                     {
15317                       remove = true;
15318                       break;
15319                     }
15320                   t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
15321                                           RO_UNARY_STAR);
15322                   if (t == error_mark_node)
15323                     {
15324                       remove = true;
15325                       break;
15326                     }
15327                 }
15328               if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
15329                   && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
15330                   && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
15331                       == TREE_VEC))
15332                 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
15333               else
15334                 OMP_CLAUSE_DECL (c) = t;
15335             }
15336           break;
15337
15338         case OMP_CLAUSE_MAP:
15339           if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
15340             goto move_implicit;
15341           /* FALLTHRU */
15342         case OMP_CLAUSE_TO:
15343         case OMP_CLAUSE_FROM:
15344         case OMP_CLAUSE__CACHE_:
15345           t = OMP_CLAUSE_DECL (c);
15346           if (TREE_CODE (t) == TREE_LIST)
15347             {
15348               grp_start_p = pc;
15349               grp_sentinel = OMP_CLAUSE_CHAIN (c);
15350
15351               if (handle_omp_array_sections (c, ort))
15352                 remove = true;
15353               else
15354                 {
15355                   t = OMP_CLAUSE_DECL (c);
15356                   if (!omp_mappable_type (TREE_TYPE (t)))
15357                     {
15358                       error_at (OMP_CLAUSE_LOCATION (c),
15359                                 "array section does not have mappable type "
15360                                 "in %qs clause",
15361                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15362                       remove = true;
15363                     }
15364                   else if (TYPE_ATOMIC (TREE_TYPE (t)))
15365                     {
15366                       error_at (OMP_CLAUSE_LOCATION (c),
15367                                 "%<_Atomic%> %qE in %qs clause", t,
15368                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15369                       remove = true;
15370                     }
15371                   while (TREE_CODE (t) == ARRAY_REF)
15372                     t = TREE_OPERAND (t, 0);
15373                   if (TREE_CODE (t) == COMPONENT_REF
15374                       && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
15375                     {
15376                       do
15377                         {
15378                           t = TREE_OPERAND (t, 0);
15379                           if (TREE_CODE (t) == MEM_REF
15380                               || TREE_CODE (t) == INDIRECT_REF)
15381                             {
15382                               t = TREE_OPERAND (t, 0);
15383                               STRIP_NOPS (t);
15384                               if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15385                                 t = TREE_OPERAND (t, 0);
15386                             }
15387                         }
15388                       while (TREE_CODE (t) == COMPONENT_REF
15389                              || TREE_CODE (t) == ARRAY_REF);
15390
15391                       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15392                           && OMP_CLAUSE_MAP_IMPLICIT (c)
15393                           && (bitmap_bit_p (&map_head, DECL_UID (t))
15394                               || bitmap_bit_p (&map_field_head, DECL_UID (t))
15395                               || bitmap_bit_p (&map_firstprivate_head,
15396                                                DECL_UID (t))))
15397                         {
15398                           remove = true;
15399                           break;
15400                         }
15401                       if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
15402                         break;
15403                       if (bitmap_bit_p (&map_head, DECL_UID (t)))
15404                         {
15405                           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15406                             error_at (OMP_CLAUSE_LOCATION (c),
15407                                       "%qD appears more than once in motion "
15408                                       "clauses", t);
15409                           else if (ort == C_ORT_ACC)
15410                             error_at (OMP_CLAUSE_LOCATION (c),
15411                                       "%qD appears more than once in data "
15412                                       "clauses", t);
15413                           else
15414                             error_at (OMP_CLAUSE_LOCATION (c),
15415                                       "%qD appears more than once in map "
15416                                       "clauses", t);
15417                           remove = true;
15418                         }
15419                       else
15420                         {
15421                           bitmap_set_bit (&map_head, DECL_UID (t));
15422                           bitmap_set_bit (&map_field_head, DECL_UID (t));
15423                         }
15424                     }
15425                 }
15426               if (c_oacc_check_attachments (c))
15427                 remove = true;
15428               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15429                   && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15430                       || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15431                 /* In this case, we have a single array element which is a
15432                    pointer, and we already set OMP_CLAUSE_SIZE in
15433                    handle_omp_array_sections above.  For attach/detach clauses,
15434                    reset the OMP_CLAUSE_SIZE (representing a bias) to zero
15435                    here.  */
15436                 OMP_CLAUSE_SIZE (c) = size_zero_node;
15437               break;
15438             }
15439           if (t == error_mark_node)
15440             {
15441               remove = true;
15442               break;
15443             }
15444           /* OpenACC attach / detach clauses must be pointers.  */
15445           if (c_oacc_check_attachments (c))
15446             {
15447               remove = true;
15448               break;
15449             }
15450           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15451               && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15452                   || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15453             /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15454                bias) to zero here, so it is not set erroneously to the pointer
15455                size later on in gimplify.cc.  */
15456             OMP_CLAUSE_SIZE (c) = size_zero_node;
15457           while (TREE_CODE (t) == INDIRECT_REF
15458                  || TREE_CODE (t) == ARRAY_REF)
15459             {
15460               t = TREE_OPERAND (t, 0);
15461               STRIP_NOPS (t);
15462               if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15463                 t = TREE_OPERAND (t, 0);
15464             }
15465           while (TREE_CODE (t) == COMPOUND_EXPR)
15466             {
15467               t = TREE_OPERAND (t, 1);
15468               STRIP_NOPS (t);
15469             }
15470           indir_component_ref_p = false;
15471           if (TREE_CODE (t) == COMPONENT_REF
15472               && (TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
15473                   || TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF
15474                   || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
15475             {
15476               t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
15477               indir_component_ref_p = true;
15478               STRIP_NOPS (t);
15479               if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15480                 t = TREE_OPERAND (t, 0);
15481             }
15482
15483           if (TREE_CODE (t) == COMPONENT_REF
15484               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
15485             {
15486               if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
15487                 {
15488                   error_at (OMP_CLAUSE_LOCATION (c),
15489                             "bit-field %qE in %qs clause",
15490                             t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15491                   remove = true;
15492                 }
15493               else if (!omp_mappable_type (TREE_TYPE (t)))
15494                 {
15495                   error_at (OMP_CLAUSE_LOCATION (c),
15496                             "%qE does not have a mappable type in %qs clause",
15497                             t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15498                   remove = true;
15499                 }
15500               else if (TYPE_ATOMIC (TREE_TYPE (t)))
15501                 {
15502                   error_at (OMP_CLAUSE_LOCATION (c),
15503                             "%<_Atomic%> %qE in %qs clause", t,
15504                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15505                   remove = true;
15506                 }
15507               while (TREE_CODE (t) == COMPONENT_REF)
15508                 {
15509                   if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
15510                       == UNION_TYPE)
15511                     {
15512                       error_at (OMP_CLAUSE_LOCATION (c),
15513                                 "%qE is a member of a union", t);
15514                       remove = true;
15515                       break;
15516                     }
15517                   t = TREE_OPERAND (t, 0);
15518                   if (TREE_CODE (t) == MEM_REF)
15519                     {
15520                       if (maybe_ne (mem_ref_offset (t), 0))
15521                         error_at (OMP_CLAUSE_LOCATION (c),
15522                                   "cannot dereference %qE in %qs clause", t,
15523                                   omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15524                       else
15525                         t = TREE_OPERAND (t, 0);
15526                     }
15527                   while (TREE_CODE (t) == MEM_REF
15528                          || TREE_CODE (t) == INDIRECT_REF
15529                          || TREE_CODE (t) == ARRAY_REF)
15530                     {
15531                       t = TREE_OPERAND (t, 0);
15532                       STRIP_NOPS (t);
15533                       if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15534                         t = TREE_OPERAND (t, 0);
15535                     }
15536                 }
15537               if (remove)
15538                 break;
15539               if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15540                 {
15541                   if (bitmap_bit_p (&map_field_head, DECL_UID (t))
15542                       || (ort != C_ORT_ACC
15543                           && bitmap_bit_p (&map_head, DECL_UID (t))))
15544                     break;
15545                 }
15546             }
15547           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15548             {
15549               error_at (OMP_CLAUSE_LOCATION (c),
15550                         "%qE is not a variable in %qs clause", t,
15551                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15552               remove = true;
15553             }
15554           else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15555             {
15556               error_at (OMP_CLAUSE_LOCATION (c),
15557                         "%qD is threadprivate variable in %qs clause", t,
15558                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15559               remove = true;
15560             }
15561           else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15562                     || (OMP_CLAUSE_MAP_KIND (c)
15563                         != GOMP_MAP_FIRSTPRIVATE_POINTER))
15564                    && !indir_component_ref_p
15565                    && !c_mark_addressable (t))
15566             remove = true;
15567           else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15568                      && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15569                          || (OMP_CLAUSE_MAP_KIND (c)
15570                              == GOMP_MAP_FIRSTPRIVATE_POINTER)
15571                          || (OMP_CLAUSE_MAP_KIND (c)
15572                              == GOMP_MAP_FORCE_DEVICEPTR)))
15573                    && t == OMP_CLAUSE_DECL (c)
15574                    && !omp_mappable_type (TREE_TYPE (t)))
15575             {
15576               error_at (OMP_CLAUSE_LOCATION (c),
15577                         "%qD does not have a mappable type in %qs clause", t,
15578                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15579               remove = true;
15580             }
15581           else if (TREE_TYPE (t) == error_mark_node)
15582             remove = true;
15583           else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15584             {
15585               error_at (OMP_CLAUSE_LOCATION (c),
15586                         "%<_Atomic%> %qE in %qs clause", t,
15587                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15588               remove = true;
15589             }
15590           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15591                    && OMP_CLAUSE_MAP_IMPLICIT (c)
15592                    && (bitmap_bit_p (&map_head, DECL_UID (t))
15593                        || bitmap_bit_p (&map_field_head, DECL_UID (t))
15594                        || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
15595             remove = true;
15596           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15597                    && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15598             {
15599               if (bitmap_bit_p (&generic_head, DECL_UID (t))
15600                   || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15601                   || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15602                 {
15603                   error_at (OMP_CLAUSE_LOCATION (c),
15604                             "%qD appears more than once in data clauses", t);
15605                   remove = true;
15606                 }
15607               else if (bitmap_bit_p (&map_head, DECL_UID (t))
15608                        && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15609                 {
15610                   if (ort == C_ORT_ACC)
15611                     error_at (OMP_CLAUSE_LOCATION (c),
15612                               "%qD appears more than once in data clauses", t);
15613                   else
15614                     error_at (OMP_CLAUSE_LOCATION (c),
15615                               "%qD appears both in data and map clauses", t);
15616                   remove = true;
15617                 }
15618               else
15619                 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15620             }
15621           else if (bitmap_bit_p (&map_head, DECL_UID (t))
15622                    && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15623             {
15624               if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15625                 error_at (OMP_CLAUSE_LOCATION (c),
15626                           "%qD appears more than once in motion clauses", t);
15627               else if (ort == C_ORT_ACC)
15628                 error_at (OMP_CLAUSE_LOCATION (c),
15629                           "%qD appears more than once in data clauses", t);
15630               else
15631                 error_at (OMP_CLAUSE_LOCATION (c),
15632                           "%qD appears more than once in map clauses", t);
15633               remove = true;
15634             }
15635           else if (ort == C_ORT_ACC
15636                    && bitmap_bit_p (&generic_head, DECL_UID (t)))
15637             {
15638               error_at (OMP_CLAUSE_LOCATION (c),
15639                         "%qD appears more than once in data clauses", t);
15640               remove = true;
15641             }
15642           else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15643                    || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15644             {
15645               if (ort == C_ORT_ACC)
15646                 error_at (OMP_CLAUSE_LOCATION (c),
15647                           "%qD appears more than once in data clauses", t);
15648               else
15649                 error_at (OMP_CLAUSE_LOCATION (c),
15650                           "%qD appears both in data and map clauses", t);
15651               remove = true;
15652             }
15653           else
15654             {
15655               bitmap_set_bit (&map_head, DECL_UID (t));
15656               if (t != OMP_CLAUSE_DECL (c)
15657                   && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15658                 bitmap_set_bit (&map_field_head, DECL_UID (t));
15659             }
15660           break;
15661
15662         case OMP_CLAUSE_ENTER:
15663         case OMP_CLAUSE_LINK:
15664           t = OMP_CLAUSE_DECL (c);
15665           const char *cname;
15666           cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
15667           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
15668               && OMP_CLAUSE_ENTER_TO (c))
15669             cname = "to";
15670           if (TREE_CODE (t) == FUNCTION_DECL
15671               && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15672             ;
15673           else if (!VAR_P (t))
15674             {
15675               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15676                 error_at (OMP_CLAUSE_LOCATION (c),
15677                           "%qE is neither a variable nor a function name in "
15678                           "clause %qs", t, cname);
15679               else
15680                 error_at (OMP_CLAUSE_LOCATION (c),
15681                           "%qE is not a variable in clause %qs", t, cname);
15682               remove = true;
15683             }
15684           else if (DECL_THREAD_LOCAL_P (t))
15685             {
15686               error_at (OMP_CLAUSE_LOCATION (c),
15687                         "%qD is threadprivate variable in %qs clause", t,
15688                         cname);
15689               remove = true;
15690             }
15691           else if (!omp_mappable_type (TREE_TYPE (t)))
15692             {
15693               error_at (OMP_CLAUSE_LOCATION (c),
15694                         "%qD does not have a mappable type in %qs clause", t,
15695                         cname);
15696               remove = true;
15697             }
15698           if (remove)
15699             break;
15700           if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15701             {
15702               error_at (OMP_CLAUSE_LOCATION (c),
15703                         "%qE appears more than once on the same "
15704                         "%<declare target%> directive", t);
15705               remove = true;
15706             }
15707           else
15708             bitmap_set_bit (&generic_head, DECL_UID (t));
15709           break;
15710
15711         case OMP_CLAUSE_UNIFORM:
15712           t = OMP_CLAUSE_DECL (c);
15713           if (TREE_CODE (t) != PARM_DECL)
15714             {
15715               if (DECL_P (t))
15716                 error_at (OMP_CLAUSE_LOCATION (c),
15717                           "%qD is not an argument in %<uniform%> clause", t);
15718               else
15719                 error_at (OMP_CLAUSE_LOCATION (c),
15720                           "%qE is not an argument in %<uniform%> clause", t);
15721               remove = true;
15722               break;
15723             }
15724           /* map_head bitmap is used as uniform_head if declare_simd.  */
15725           bitmap_set_bit (&map_head, DECL_UID (t));
15726           goto check_dup_generic;
15727
15728         case OMP_CLAUSE_IS_DEVICE_PTR:
15729         case OMP_CLAUSE_USE_DEVICE_PTR:
15730           t = OMP_CLAUSE_DECL (c);
15731           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15732             bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15733           if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15734             {
15735               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15736                   && ort != C_ORT_ACC)
15737                 {
15738                   error_at (OMP_CLAUSE_LOCATION (c),
15739                             "%qs variable is not a pointer",
15740                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15741                   remove = true;
15742                 }
15743               else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15744                 {
15745                   error_at (OMP_CLAUSE_LOCATION (c),
15746                             "%qs variable is neither a pointer nor an array",
15747                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15748                   remove = true;
15749                 }
15750             }
15751           goto check_dup_generic;
15752
15753         case OMP_CLAUSE_HAS_DEVICE_ADDR:
15754           t = OMP_CLAUSE_DECL (c);
15755           if (TREE_CODE (t) == TREE_LIST)
15756             {
15757               if (handle_omp_array_sections (c, ort))
15758                 remove = true;
15759               else
15760                 {
15761                   t = OMP_CLAUSE_DECL (c);
15762                   while (TREE_CODE (t) == ARRAY_REF)
15763                     t = TREE_OPERAND (t, 0);
15764                 }
15765             }
15766           bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15767           if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15768             c_mark_addressable (t);
15769           goto check_dup_generic_t;
15770
15771         case OMP_CLAUSE_USE_DEVICE_ADDR:
15772           t = OMP_CLAUSE_DECL (c);
15773           if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15774             c_mark_addressable (t);
15775           goto check_dup_generic;
15776
15777         case OMP_CLAUSE_NOWAIT:
15778           if (copyprivate_seen)
15779             {
15780               error_at (OMP_CLAUSE_LOCATION (c),
15781                         "%<nowait%> clause must not be used together "
15782                         "with %<copyprivate%>");
15783               remove = true;
15784               break;
15785             }
15786           nowait_clause = pc;
15787           pc = &OMP_CLAUSE_CHAIN (c);
15788           continue;
15789
15790         case OMP_CLAUSE_ORDER:
15791           if (ordered_clause)
15792             {
15793               error_at (OMP_CLAUSE_LOCATION (c),
15794                         "%<order%> clause must not be used together "
15795                         "with %<ordered%>");
15796               remove = true;
15797               break;
15798             }
15799           else if (order_clause)
15800             {
15801               /* Silently remove duplicates.  */
15802               remove = true;
15803               break;
15804             }
15805           order_clause = pc;
15806           pc = &OMP_CLAUSE_CHAIN (c);
15807           continue;
15808
15809         case OMP_CLAUSE_DETACH:
15810           t = OMP_CLAUSE_DECL (c);
15811           if (detach_seen)
15812             {
15813               error_at (OMP_CLAUSE_LOCATION (c),
15814                         "too many %qs clauses on a task construct",
15815                         "detach");
15816               remove = true;
15817               break;
15818             }
15819           detach_seen = pc;
15820           pc = &OMP_CLAUSE_CHAIN (c);
15821           c_mark_addressable (t);
15822           continue;
15823
15824         case OMP_CLAUSE_IF:
15825         case OMP_CLAUSE_NUM_THREADS:
15826         case OMP_CLAUSE_NUM_TEAMS:
15827         case OMP_CLAUSE_THREAD_LIMIT:
15828         case OMP_CLAUSE_DEFAULT:
15829         case OMP_CLAUSE_UNTIED:
15830         case OMP_CLAUSE_COLLAPSE:
15831         case OMP_CLAUSE_FINAL:
15832         case OMP_CLAUSE_DEVICE:
15833         case OMP_CLAUSE_DIST_SCHEDULE:
15834         case OMP_CLAUSE_PARALLEL:
15835         case OMP_CLAUSE_FOR:
15836         case OMP_CLAUSE_SECTIONS:
15837         case OMP_CLAUSE_TASKGROUP:
15838         case OMP_CLAUSE_PROC_BIND:
15839         case OMP_CLAUSE_DEVICE_TYPE:
15840         case OMP_CLAUSE_PRIORITY:
15841         case OMP_CLAUSE_GRAINSIZE:
15842         case OMP_CLAUSE_NUM_TASKS:
15843         case OMP_CLAUSE_THREADS:
15844         case OMP_CLAUSE_SIMD:
15845         case OMP_CLAUSE_HINT:
15846         case OMP_CLAUSE_FILTER:
15847         case OMP_CLAUSE_DEFAULTMAP:
15848         case OMP_CLAUSE_BIND:
15849         case OMP_CLAUSE_NUM_GANGS:
15850         case OMP_CLAUSE_NUM_WORKERS:
15851         case OMP_CLAUSE_VECTOR_LENGTH:
15852         case OMP_CLAUSE_ASYNC:
15853         case OMP_CLAUSE_WAIT:
15854         case OMP_CLAUSE_AUTO:
15855         case OMP_CLAUSE_INDEPENDENT:
15856         case OMP_CLAUSE_SEQ:
15857         case OMP_CLAUSE_GANG:
15858         case OMP_CLAUSE_WORKER:
15859         case OMP_CLAUSE_VECTOR:
15860         case OMP_CLAUSE_TILE:
15861         case OMP_CLAUSE_IF_PRESENT:
15862         case OMP_CLAUSE_FINALIZE:
15863         case OMP_CLAUSE_NOHOST:
15864           pc = &OMP_CLAUSE_CHAIN (c);
15865           continue;
15866
15867         case OMP_CLAUSE_MERGEABLE:
15868           mergeable_seen = true;
15869           pc = &OMP_CLAUSE_CHAIN (c);
15870           continue;
15871
15872         case OMP_CLAUSE_NOGROUP:
15873           nogroup_seen = pc;
15874           pc = &OMP_CLAUSE_CHAIN (c);
15875           continue;
15876
15877         case OMP_CLAUSE_SCHEDULE:
15878           schedule_clause = c;
15879           pc = &OMP_CLAUSE_CHAIN (c);
15880           continue;
15881
15882         case OMP_CLAUSE_ORDERED:
15883           ordered_clause = c;
15884           if (order_clause)
15885             {
15886               error_at (OMP_CLAUSE_LOCATION (*order_clause),
15887                         "%<order%> clause must not be used together "
15888                         "with %<ordered%>");
15889               *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15890               order_clause = NULL;
15891             }
15892           pc = &OMP_CLAUSE_CHAIN (c);
15893           continue;
15894
15895         case OMP_CLAUSE_SAFELEN:
15896           safelen = c;
15897           pc = &OMP_CLAUSE_CHAIN (c);
15898           continue;
15899         case OMP_CLAUSE_SIMDLEN:
15900           simdlen = c;
15901           pc = &OMP_CLAUSE_CHAIN (c);
15902           continue;
15903
15904         case OMP_CLAUSE_INBRANCH:
15905         case OMP_CLAUSE_NOTINBRANCH:
15906           if (branch_seen)
15907             {
15908               error_at (OMP_CLAUSE_LOCATION (c),
15909                         "%<inbranch%> clause is incompatible with "
15910                         "%<notinbranch%>");
15911               remove = true;
15912               break;
15913             }
15914           branch_seen = true;
15915           pc = &OMP_CLAUSE_CHAIN (c);
15916           continue;
15917
15918         case OMP_CLAUSE_INCLUSIVE:
15919         case OMP_CLAUSE_EXCLUSIVE:
15920           need_complete = true;
15921           need_implicitly_determined = true;
15922           t = OMP_CLAUSE_DECL (c);
15923           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15924             {
15925               error_at (OMP_CLAUSE_LOCATION (c),
15926                         "%qE is not a variable in clause %qs", t,
15927                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15928               remove = true;
15929             }
15930           break;
15931
15932         default:
15933           gcc_unreachable ();
15934         }
15935
15936       if (!remove)
15937         {
15938           t = OMP_CLAUSE_DECL (c);
15939
15940           if (need_complete)
15941             {
15942               t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15943               if (t == error_mark_node)
15944                 remove = true;
15945             }
15946
15947           if (need_implicitly_determined)
15948             {
15949               const char *share_name = NULL;
15950
15951               if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15952                 share_name = "threadprivate";
15953               else switch (c_omp_predetermined_sharing (t))
15954                 {
15955                 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15956                   break;
15957                 case OMP_CLAUSE_DEFAULT_SHARED:
15958                   if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15959                        || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15960                       && c_omp_predefined_variable (t))
15961                     /* The __func__ variable and similar function-local
15962                        predefined variables may be listed in a shared or
15963                        firstprivate clause.  */
15964                     break;
15965                   share_name = "shared";
15966                   break;
15967                 case OMP_CLAUSE_DEFAULT_PRIVATE:
15968                   share_name = "private";
15969                   break;
15970                 default:
15971                   gcc_unreachable ();
15972                 }
15973               if (share_name)
15974                 {
15975                   error_at (OMP_CLAUSE_LOCATION (c),
15976                             "%qE is predetermined %qs for %qs",
15977                             t, share_name,
15978                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15979                   remove = true;
15980                 }
15981               else if (TREE_READONLY (t)
15982                        && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15983                        && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15984                 {
15985                   error_at (OMP_CLAUSE_LOCATION (c),
15986                             "%<const%> qualified %qE may appear only in "
15987                             "%<shared%> or %<firstprivate%> clauses", t);
15988                   remove = true;
15989                 }
15990             }
15991         }
15992
15993       if (remove)
15994         {
15995           if (grp_start_p)
15996             {
15997               /* If we found a clause to remove, we want to remove the whole
15998                  expanded group, otherwise gimplify
15999                  (omp_resolve_clause_dependencies) can get confused.  */
16000               *grp_start_p = grp_sentinel;
16001               pc = grp_start_p;
16002               grp_start_p = NULL;
16003             }
16004           else
16005             *pc = OMP_CLAUSE_CHAIN (c);
16006         }
16007       else
16008         pc = &OMP_CLAUSE_CHAIN (c);
16009     }
16010
16011   if (simdlen
16012       && safelen
16013       && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
16014                           OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
16015     {
16016       error_at (OMP_CLAUSE_LOCATION (simdlen),
16017                 "%<simdlen%> clause value is bigger than "
16018                 "%<safelen%> clause value");
16019       OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
16020         = OMP_CLAUSE_SAFELEN_EXPR (safelen);
16021     }
16022
16023   if (ordered_clause
16024       && schedule_clause
16025       && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16026           & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
16027     {
16028       error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16029                 "%<nonmonotonic%> schedule modifier specified together "
16030                 "with %<ordered%> clause");
16031       OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16032         = (enum omp_clause_schedule_kind)
16033           (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
16034            & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
16035     }
16036
16037   if (reduction_seen < 0 && ordered_clause)
16038     {
16039       error_at (OMP_CLAUSE_LOCATION (ordered_clause),
16040                 "%qs clause specified together with %<inscan%> "
16041                 "%<reduction%> clause", "ordered");
16042       reduction_seen = -2;
16043     }
16044
16045   if (reduction_seen < 0 && schedule_clause)
16046     {
16047       error_at (OMP_CLAUSE_LOCATION (schedule_clause),
16048                 "%qs clause specified together with %<inscan%> "
16049                 "%<reduction%> clause", "schedule");
16050       reduction_seen = -2;
16051     }
16052
16053   if (linear_variable_step_check
16054       || reduction_seen == -2
16055       || allocate_seen
16056       || target_in_reduction_seen)
16057     for (pc = &clauses, c = clauses; c ; c = *pc)
16058       {
16059         bool remove = false;
16060         if (allocate_seen)
16061           switch (OMP_CLAUSE_CODE (c))
16062             {
16063             case OMP_CLAUSE_REDUCTION:
16064             case OMP_CLAUSE_IN_REDUCTION:
16065             case OMP_CLAUSE_TASK_REDUCTION:
16066               if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
16067                 {
16068                   t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
16069                   if (TREE_CODE (t) == POINTER_PLUS_EXPR)
16070                     t = TREE_OPERAND (t, 0);
16071                   if (TREE_CODE (t) == ADDR_EXPR
16072                       || TREE_CODE (t) == INDIRECT_REF)
16073                     t = TREE_OPERAND (t, 0);
16074                   if (DECL_P (t))
16075                     bitmap_clear_bit (&aligned_head, DECL_UID (t));
16076                   break;
16077                 }
16078               /* FALLTHRU */
16079             case OMP_CLAUSE_PRIVATE:
16080             case OMP_CLAUSE_FIRSTPRIVATE:
16081             case OMP_CLAUSE_LASTPRIVATE:
16082             case OMP_CLAUSE_LINEAR:
16083               if (DECL_P (OMP_CLAUSE_DECL (c)))
16084                 bitmap_clear_bit (&aligned_head,
16085                                   DECL_UID (OMP_CLAUSE_DECL (c)));
16086               break;
16087             default:
16088               break;
16089             }
16090         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
16091             && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
16092             && !bitmap_bit_p (&map_head,
16093                               DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
16094           {
16095             error_at (OMP_CLAUSE_LOCATION (c),
16096                       "%<linear%> clause step is a parameter %qD not "
16097                       "specified in %<uniform%> clause",
16098                       OMP_CLAUSE_LINEAR_STEP (c));
16099             remove = true;
16100           }
16101         else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
16102                  && reduction_seen == -2)
16103           OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
16104         if (target_in_reduction_seen
16105             && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
16106           {
16107             tree t = OMP_CLAUSE_DECL (c);
16108             while (handled_component_p (t)
16109                    || TREE_CODE (t) == INDIRECT_REF
16110                    || TREE_CODE (t) == ADDR_EXPR
16111                    || TREE_CODE (t) == MEM_REF
16112                    || TREE_CODE (t) == NON_LVALUE_EXPR)
16113               t = TREE_OPERAND (t, 0);
16114             if (DECL_P (t)
16115                 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
16116               OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
16117           }
16118
16119         if (remove)
16120           *pc = OMP_CLAUSE_CHAIN (c);
16121         else
16122           pc = &OMP_CLAUSE_CHAIN (c);
16123       }
16124
16125   if (allocate_seen)
16126     for (pc = &clauses, c = clauses; c ; c = *pc)
16127       {
16128         bool remove = false;
16129         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
16130             && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
16131             && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
16132           {
16133             error_at (OMP_CLAUSE_LOCATION (c),
16134                       "%qD specified in %<allocate%> clause but not in "
16135                       "an explicit privatization clause", OMP_CLAUSE_DECL (c));
16136             remove = true;
16137           }
16138         if (remove)
16139           *pc = OMP_CLAUSE_CHAIN (c);
16140         else
16141           pc = &OMP_CLAUSE_CHAIN (c);
16142       }
16143
16144   if (nogroup_seen && reduction_seen)
16145     {
16146       error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
16147                 "%<nogroup%> clause must not be used together with "
16148                 "%<reduction%> clause");
16149       *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
16150     }
16151
16152   if (detach_seen)
16153     {
16154       if (mergeable_seen)
16155         {
16156           error_at (OMP_CLAUSE_LOCATION (*detach_seen),
16157                     "%<detach%> clause must not be used together with "
16158                     "%<mergeable%> clause");
16159           *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
16160         }
16161       else
16162         {
16163           tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
16164
16165           for (pc = &clauses, c = clauses; c ; c = *pc)
16166             {
16167               bool remove = false;
16168               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
16169                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
16170                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
16171                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
16172                   && OMP_CLAUSE_DECL (c) == detach_decl)
16173                 {
16174                   error_at (OMP_CLAUSE_LOCATION (c),
16175                             "the event handle of a %<detach%> clause "
16176                             "should not be in a data-sharing clause");
16177                   remove = true;
16178                 }
16179               if (remove)
16180                 *pc = OMP_CLAUSE_CHAIN (c);
16181               else
16182                 pc = &OMP_CLAUSE_CHAIN (c);
16183             }
16184         }
16185     }
16186
16187   bitmap_obstack_release (NULL);
16188   return clauses;
16189 }
16190
16191 /* Return code to initialize DST with a copy constructor from SRC.
16192    C doesn't have copy constructors nor assignment operators, only for
16193    _Atomic vars we need to perform __atomic_load from src into a temporary
16194    followed by __atomic_store of the temporary to dst.  */
16195
16196 tree
16197 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
16198 {
16199   if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
16200     return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
16201
16202   location_t loc = OMP_CLAUSE_LOCATION (clause);
16203   tree type = TREE_TYPE (dst);
16204   tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
16205   tree tmp = create_tmp_var (nonatomic_type);
16206   tree tmp_addr = build_fold_addr_expr (tmp);
16207   TREE_ADDRESSABLE (tmp) = 1;
16208   suppress_warning (tmp);
16209   tree src_addr = build_fold_addr_expr (src);
16210   tree dst_addr = build_fold_addr_expr (dst);
16211   tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
16212   vec<tree, va_gc> *params;
16213   /* Expansion of a generic atomic load may require an addition
16214      element, so allocate enough to prevent a resize.  */
16215   vec_alloc (params, 4);
16216
16217   /* Build __atomic_load (&src, &tmp, SEQ_CST);  */
16218   tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
16219   params->quick_push (src_addr);
16220   params->quick_push (tmp_addr);
16221   params->quick_push (seq_cst);
16222   tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16223
16224   vec_alloc (params, 4);
16225
16226   /* Build __atomic_store (&dst, &tmp, SEQ_CST);  */
16227   fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
16228   params->quick_push (dst_addr);
16229   params->quick_push (tmp_addr);
16230   params->quick_push (seq_cst);
16231   tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
16232   return build2 (COMPOUND_EXPR, void_type_node, load, store);
16233 }
16234
16235 /* Create a transaction node.  */
16236
16237 tree
16238 c_finish_transaction (location_t loc, tree block, int flags)
16239 {
16240   tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
16241   if (flags & TM_STMT_ATTR_OUTER)
16242     TRANSACTION_EXPR_OUTER (stmt) = 1;
16243   if (flags & TM_STMT_ATTR_RELAXED)
16244     TRANSACTION_EXPR_RELAXED (stmt) = 1;
16245   return add_stmt (stmt);
16246 }
16247
16248 /* Make a variant type in the proper way for C/C++, propagating qualifiers
16249    down to the element type of an array.  If ORIG_QUAL_TYPE is not
16250    NULL, then it should be used as the qualified type
16251    ORIG_QUAL_INDIRECT levels down in array type derivation (to
16252    preserve information about the typedef name from which an array
16253    type was derived).  */
16254
16255 tree
16256 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
16257                         size_t orig_qual_indirect)
16258 {
16259   if (type == error_mark_node)
16260     return type;
16261
16262   if (TREE_CODE (type) == ARRAY_TYPE)
16263     {
16264       tree t;
16265       tree element_type = c_build_qualified_type (TREE_TYPE (type),
16266                                                   type_quals, orig_qual_type,
16267                                                   orig_qual_indirect - 1);
16268
16269       /* See if we already have an identically qualified type.  */
16270       if (orig_qual_type && orig_qual_indirect == 0)
16271         t = orig_qual_type;
16272       else
16273         for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
16274           {
16275             if (TYPE_QUALS (strip_array_types (t)) == type_quals
16276                 && TYPE_NAME (t) == TYPE_NAME (type)
16277                 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
16278                 && attribute_list_equal (TYPE_ATTRIBUTES (t),
16279                                          TYPE_ATTRIBUTES (type)))
16280               break;
16281           }
16282       if (!t)
16283         {
16284           tree domain = TYPE_DOMAIN (type);
16285
16286           t = build_variant_type_copy (type);
16287           TREE_TYPE (t) = element_type;
16288
16289           if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
16290               || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
16291             SET_TYPE_STRUCTURAL_EQUALITY (t);
16292           else if (TYPE_CANONICAL (element_type) != element_type
16293                    || (domain && TYPE_CANONICAL (domain) != domain))
16294             {
16295               tree unqualified_canon
16296                 = build_array_type (TYPE_CANONICAL (element_type),
16297                                     domain? TYPE_CANONICAL (domain)
16298                                           : NULL_TREE);
16299               if (TYPE_REVERSE_STORAGE_ORDER (type))
16300                 {
16301                   unqualified_canon
16302                     = build_distinct_type_copy (unqualified_canon);
16303                   TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
16304                 }
16305               TYPE_CANONICAL (t)
16306                 = c_build_qualified_type (unqualified_canon, type_quals);
16307             }
16308           else
16309             TYPE_CANONICAL (t) = t;
16310         }
16311       return t;
16312     }
16313
16314   /* A restrict-qualified pointer type must be a pointer to object or
16315      incomplete type.  Note that the use of POINTER_TYPE_P also allows
16316      REFERENCE_TYPEs, which is appropriate for C++.  */
16317   if ((type_quals & TYPE_QUAL_RESTRICT)
16318       && (!POINTER_TYPE_P (type)
16319           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
16320     {
16321       error ("invalid use of %<restrict%>");
16322       type_quals &= ~TYPE_QUAL_RESTRICT;
16323     }
16324
16325   tree var_type = (orig_qual_type && orig_qual_indirect == 0
16326                    ? orig_qual_type
16327                    : build_qualified_type (type, type_quals));
16328   /* A variant type does not inherit the list of incomplete vars from the
16329      type main variant.  */
16330   if ((RECORD_OR_UNION_TYPE_P (var_type)
16331        || TREE_CODE (var_type) == ENUMERAL_TYPE)
16332       && TYPE_MAIN_VARIANT (var_type) != var_type)
16333     C_TYPE_INCOMPLETE_VARS (var_type) = 0;
16334   return var_type;
16335 }
16336
16337 /* Build a VA_ARG_EXPR for the C parser.  */
16338
16339 tree
16340 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
16341 {
16342   if (error_operand_p (type))
16343     return error_mark_node;
16344   /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
16345      order because it takes the address of the expression.  */
16346   else if (handled_component_p (expr)
16347            && reverse_storage_order_for_component_p (expr))
16348     {
16349       error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
16350       return error_mark_node;
16351     }
16352   else if (!COMPLETE_TYPE_P (type))
16353     {
16354       error_at (loc2, "second argument to %<va_arg%> is of incomplete "
16355                 "type %qT", type);
16356       return error_mark_node;
16357     }
16358   else if (TREE_CODE (type) == FUNCTION_TYPE)
16359     {
16360       error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
16361                 type);
16362       return error_mark_node;
16363     }
16364   else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
16365     warning_at (loc2, OPT_Wc___compat,
16366                 "C++ requires promoted type, not enum type, in %<va_arg%>");
16367   return build_va_arg (loc2, expr, type);
16368 }
16369
16370 /* Return truthvalue of whether T1 is the same tree structure as T2.
16371    Return 1 if they are the same. Return false if they are different.  */
16372
16373 bool
16374 c_tree_equal (tree t1, tree t2)
16375 {
16376   enum tree_code code1, code2;
16377
16378   if (t1 == t2)
16379     return true;
16380   if (!t1 || !t2)
16381     return false;
16382
16383   for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
16384        code1 = TREE_CODE (t1))
16385     t1 = TREE_OPERAND (t1, 0);
16386   for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
16387        code2 = TREE_CODE (t2))
16388     t2 = TREE_OPERAND (t2, 0);
16389
16390   /* They might have become equal now.  */
16391   if (t1 == t2)
16392     return true;
16393
16394   if (code1 != code2)
16395     return false;
16396
16397   if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16398     return false;
16399
16400   switch (code1)
16401     {
16402     case INTEGER_CST:
16403       return wi::to_wide (t1) == wi::to_wide (t2);
16404
16405     case REAL_CST:
16406       return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
16407
16408     case STRING_CST:
16409       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
16410         && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
16411                     TREE_STRING_LENGTH (t1));
16412
16413     case FIXED_CST:
16414       return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
16415                                      TREE_FIXED_CST (t2));
16416
16417     case COMPLEX_CST:
16418       return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
16419              && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
16420
16421     case VECTOR_CST:
16422       return operand_equal_p (t1, t2, OEP_ONLY_CONST);
16423
16424     case CONSTRUCTOR:
16425       /* We need to do this when determining whether or not two
16426          non-type pointer to member function template arguments
16427          are the same.  */
16428       if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
16429           || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
16430         return false;
16431       {
16432         tree field, value;
16433         unsigned int i;
16434         FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
16435           {
16436             constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
16437             if (!c_tree_equal (field, elt2->index)
16438                 || !c_tree_equal (value, elt2->value))
16439               return false;
16440           }
16441       }
16442       return true;
16443
16444     case TREE_LIST:
16445       if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
16446         return false;
16447       if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
16448         return false;
16449       return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
16450
16451     case SAVE_EXPR:
16452       return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16453
16454     case CALL_EXPR:
16455       {
16456         tree arg1, arg2;
16457         call_expr_arg_iterator iter1, iter2;
16458         if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16459           return false;
16460         for (arg1 = first_call_expr_arg (t1, &iter1),
16461                arg2 = first_call_expr_arg (t2, &iter2);
16462              arg1 && arg2;
16463              arg1 = next_call_expr_arg (&iter1),
16464                arg2 = next_call_expr_arg (&iter2))
16465           if (!c_tree_equal (arg1, arg2))
16466             return false;
16467         if (arg1 || arg2)
16468           return false;
16469         return true;
16470       }
16471
16472     case TARGET_EXPR:
16473       {
16474         tree o1 = TREE_OPERAND (t1, 0);
16475         tree o2 = TREE_OPERAND (t2, 0);
16476
16477         /* Special case: if either target is an unallocated VAR_DECL,
16478            it means that it's going to be unified with whatever the
16479            TARGET_EXPR is really supposed to initialize, so treat it
16480            as being equivalent to anything.  */
16481         if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16482             && !DECL_RTL_SET_P (o1))
16483           /*Nop*/;
16484         else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16485                  && !DECL_RTL_SET_P (o2))
16486           /*Nop*/;
16487         else if (!c_tree_equal (o1, o2))
16488           return false;
16489
16490         return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16491       }
16492
16493     case COMPONENT_REF:
16494       if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16495         return false;
16496       return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16497
16498     case PARM_DECL:
16499     case VAR_DECL:
16500     case CONST_DECL:
16501     case FIELD_DECL:
16502     case FUNCTION_DECL:
16503     case IDENTIFIER_NODE:
16504     case SSA_NAME:
16505       return false;
16506
16507     case TREE_VEC:
16508       {
16509         unsigned ix;
16510         if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16511           return false;
16512         for (ix = TREE_VEC_LENGTH (t1); ix--;)
16513           if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16514                              TREE_VEC_ELT (t2, ix)))
16515             return false;
16516         return true;
16517       }
16518
16519     CASE_CONVERT:
16520       if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
16521         return false;
16522       break;
16523
16524     default:
16525       break;
16526     }
16527
16528   switch (TREE_CODE_CLASS (code1))
16529     {
16530     case tcc_unary:
16531     case tcc_binary:
16532     case tcc_comparison:
16533     case tcc_expression:
16534     case tcc_vl_exp:
16535     case tcc_reference:
16536     case tcc_statement:
16537       {
16538         int i, n = TREE_OPERAND_LENGTH (t1);
16539
16540         switch (code1)
16541           {
16542           case PREINCREMENT_EXPR:
16543           case PREDECREMENT_EXPR:
16544           case POSTINCREMENT_EXPR:
16545           case POSTDECREMENT_EXPR:
16546             n = 1;
16547             break;
16548           case ARRAY_REF:
16549             n = 2;
16550             break;
16551           default:
16552             break;
16553           }
16554
16555         if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16556             && n != TREE_OPERAND_LENGTH (t2))
16557           return false;
16558
16559         for (i = 0; i < n; ++i)
16560           if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16561             return false;
16562
16563         return true;
16564       }
16565
16566     case tcc_type:
16567       return comptypes (t1, t2);
16568     default:
16569       gcc_unreachable ();
16570     }
16571 }
16572
16573 /* Returns true when the function declaration FNDECL is implicit,
16574    introduced as a result of a call to an otherwise undeclared
16575    function, and false otherwise.  */
16576
16577 bool
16578 c_decl_implicit (const_tree fndecl)
16579 {
16580   return C_DECL_IMPLICIT (fndecl);
16581 }