c: Support C2x empty initializer braces
[platform/upstream/gcc.git] / gcc / c / c-typeck.cc
1 /* Build expressions with type checking for C compiler.
2    Copyright (C) 1987-2022 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 int require_constant_value;
88 static int require_constant_elements;
89
90 static bool null_pointer_constant_p (const_tree);
91 static tree qualify_type (tree, tree);
92 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
93                                          bool *);
94 static int comp_target_types (location_t, tree, tree);
95 static int function_types_compatible_p (const_tree, const_tree, bool *,
96                                         bool *);
97 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
98 static tree lookup_field (tree, tree);
99 static int convert_arguments (location_t, vec<location_t>, tree,
100                               vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
101                               tree);
102 static tree pointer_diff (location_t, tree, tree, tree *);
103 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
104                                     enum impl_conv, bool, tree, tree, int,
105                                     int = 0);
106 static tree valid_compound_expr_initializer (tree, tree);
107 static void push_string (const char *);
108 static void push_member_name (tree);
109 static int spelling_length (void);
110 static char *print_spelling (char *);
111 static void warning_init (location_t, int, const char *);
112 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
113 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
114                                  bool, struct obstack *);
115 static void output_pending_init_elements (int, struct obstack *);
116 static bool set_designator (location_t, bool, struct obstack *);
117 static void push_range_stack (tree, struct obstack *);
118 static void add_pending_init (location_t, tree, tree, tree, bool,
119                               struct obstack *);
120 static void set_nonincremental_init (struct obstack *);
121 static void set_nonincremental_init_from_string (tree, struct obstack *);
122 static tree find_init_member (tree, struct obstack *);
123 static void readonly_warning (tree, enum lvalue_use);
124 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
125 static void record_maybe_used_decl (tree);
126 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
127 \f
128 /* Return true if EXP is a null pointer constant, false otherwise.  */
129
130 static bool
131 null_pointer_constant_p (const_tree expr)
132 {
133   /* This should really operate on c_expr structures, but they aren't
134      yet available everywhere required.  */
135   tree type = TREE_TYPE (expr);
136   return (TREE_CODE (expr) == INTEGER_CST
137           && !TREE_OVERFLOW (expr)
138           && integer_zerop (expr)
139           && (INTEGRAL_TYPE_P (type)
140               || (TREE_CODE (type) == POINTER_TYPE
141                   && VOID_TYPE_P (TREE_TYPE (type))
142                   && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
143 }
144
145 /* EXPR may appear in an unevaluated part of an integer constant
146    expression, but not in an evaluated part.  Wrap it in a
147    C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
148    INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR.  */
149
150 static tree
151 note_integer_operands (tree expr)
152 {
153   tree ret;
154   if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
155     {
156       ret = copy_node (expr);
157       TREE_OVERFLOW (ret) = 1;
158     }
159   else
160     {
161       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
162       C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
163     }
164   return ret;
165 }
166
167 /* Having checked whether EXPR may appear in an unevaluated part of an
168    integer constant expression and found that it may, remove any
169    C_MAYBE_CONST_EXPR noting this fact and return the resulting
170    expression.  */
171
172 static inline tree
173 remove_c_maybe_const_expr (tree expr)
174 {
175   if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
176     return C_MAYBE_CONST_EXPR_EXPR (expr);
177   else
178     return expr;
179 }
180
181 \f/* This is a cache to hold if two types are compatible or not.  */
182
183 struct tagged_tu_seen_cache {
184   const struct tagged_tu_seen_cache * next;
185   const_tree t1;
186   const_tree t2;
187   /* The return value of tagged_types_tu_compatible_p if we had seen
188      these two types already.  */
189   int val;
190 };
191
192 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
193 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
194
195 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
196    does not have an incomplete type.  (That includes void types.)
197    LOC is the location of the use.  */
198
199 tree
200 require_complete_type (location_t loc, tree value)
201 {
202   tree type = TREE_TYPE (value);
203
204   if (error_operand_p (value))
205     return error_mark_node;
206
207   /* First, detect a valid value with a complete type.  */
208   if (COMPLETE_TYPE_P (type))
209     return value;
210
211   c_incomplete_type_error (loc, value, type);
212   return error_mark_node;
213 }
214
215 /* Print an error message for invalid use of an incomplete type.
216    VALUE is the expression that was used (or 0 if that isn't known)
217    and TYPE is the type that was invalid.  LOC is the location for
218    the error.  */
219
220 void
221 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
222 {
223   /* Avoid duplicate error message.  */
224   if (TREE_CODE (type) == ERROR_MARK)
225     return;
226
227   if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
228     error_at (loc, "%qD has an incomplete type %qT", value, type);
229   else
230     {
231     retry:
232       /* We must print an error message.  Be clever about what it says.  */
233
234       switch (TREE_CODE (type))
235         {
236         case RECORD_TYPE:
237         case UNION_TYPE:
238         case ENUMERAL_TYPE:
239           break;
240
241         case VOID_TYPE:
242           error_at (loc, "invalid use of void expression");
243           return;
244
245         case ARRAY_TYPE:
246           if (TYPE_DOMAIN (type))
247             {
248               if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
249                 {
250                   error_at (loc, "invalid use of flexible array member");
251                   return;
252                 }
253               type = TREE_TYPE (type);
254               goto retry;
255             }
256           error_at (loc, "invalid use of array with unspecified bounds");
257           return;
258
259         default:
260           gcc_unreachable ();
261         }
262
263       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
264         error_at (loc, "invalid use of undefined type %qT", type);
265       else
266         /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL.  */
267         error_at (loc, "invalid use of incomplete typedef %qT", type);
268     }
269 }
270
271 /* Given a type, apply default promotions wrt unnamed function
272    arguments and return the new type.  */
273
274 tree
275 c_type_promotes_to (tree type)
276 {
277   tree ret = NULL_TREE;
278
279   if (TYPE_MAIN_VARIANT (type) == float_type_node)
280     ret = double_type_node;
281   else if (c_promoting_integer_type_p (type))
282     {
283       /* Preserve unsignedness if not really getting any wider.  */
284       if (TYPE_UNSIGNED (type)
285           && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
286         ret = unsigned_type_node;
287       else
288         ret = integer_type_node;
289     }
290
291   if (ret != NULL_TREE)
292     return (TYPE_ATOMIC (type)
293             ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
294             : ret);
295
296   return type;
297 }
298
299 /* Return true if between two named address spaces, whether there is a superset
300    named address space that encompasses both address spaces.  If there is a
301    superset, return which address space is the superset.  */
302
303 static bool
304 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
305 {
306   if (as1 == as2)
307     {
308       *common = as1;
309       return true;
310     }
311   else if (targetm.addr_space.subset_p (as1, as2))
312     {
313       *common = as2;
314       return true;
315     }
316   else if (targetm.addr_space.subset_p (as2, as1))
317     {
318       *common = as1;
319       return true;
320     }
321   else
322     return false;
323 }
324
325 /* Return a variant of TYPE which has all the type qualifiers of LIKE
326    as well as those of TYPE.  */
327
328 static tree
329 qualify_type (tree type, tree like)
330 {
331   addr_space_t as_type = TYPE_ADDR_SPACE (type);
332   addr_space_t as_like = TYPE_ADDR_SPACE (like);
333   addr_space_t as_common;
334
335   /* If the two named address spaces are different, determine the common
336      superset address space.  If there isn't one, raise an error.  */
337   if (!addr_space_superset (as_type, as_like, &as_common))
338     {
339       as_common = as_type;
340       error ("%qT and %qT are in disjoint named address spaces",
341              type, like);
342     }
343
344   return c_build_qualified_type (type,
345                                  TYPE_QUALS_NO_ADDR_SPACE (type)
346                                  | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
347                                  | ENCODE_QUAL_ADDR_SPACE (as_common));
348 }
349
350 /* Return true iff the given tree T is a variable length array.  */
351
352 bool
353 c_vla_type_p (const_tree t)
354 {
355   if (TREE_CODE (t) == ARRAY_TYPE
356       && C_TYPE_VARIABLE_SIZE (t))
357     return true;
358   return false;
359 }
360
361 /* If NTYPE is a type of a non-variadic function with a prototype
362    and OTYPE is a type of a function without a prototype and ATTRS
363    contains attribute format, diagnosess and removes it from ATTRS.
364    Returns the result of build_type_attribute_variant of NTYPE and
365    the (possibly) modified ATTRS.  */
366
367 static tree
368 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
369 {
370   if (!prototype_p (otype)
371       && prototype_p (ntype)
372       && lookup_attribute ("format", attrs))
373     {
374       warning_at (input_location, OPT_Wattributes,
375                   "%qs attribute cannot be applied to a function that "
376                   "does not take variable arguments", "format");
377       attrs = remove_attribute ("format", attrs);
378     }
379   return build_type_attribute_variant (ntype, attrs);
380
381 }
382 /* Return the composite type of two compatible types.
383
384    We assume that comptypes has already been done and returned
385    nonzero; if that isn't so, this may crash.  In particular, we
386    assume that qualifiers match.  */
387
388 tree
389 composite_type (tree t1, tree t2)
390 {
391   enum tree_code code1;
392   enum tree_code code2;
393   tree attributes;
394
395   /* Save time if the two types are the same.  */
396
397   if (t1 == t2) return t1;
398
399   /* If one type is nonsense, use the other.  */
400   if (t1 == error_mark_node)
401     return t2;
402   if (t2 == error_mark_node)
403     return t1;
404
405   code1 = TREE_CODE (t1);
406   code2 = TREE_CODE (t2);
407
408   /* Merge the attributes.  */
409   attributes = targetm.merge_type_attributes (t1, t2);
410
411   /* If one is an enumerated type and the other is the compatible
412      integer type, the composite type might be either of the two
413      (DR#013 question 3).  For consistency, use the enumerated type as
414      the composite type.  */
415
416   if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
417     return t1;
418   if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
419     return t2;
420
421   gcc_assert (code1 == code2);
422
423   switch (code1)
424     {
425     case POINTER_TYPE:
426       /* For two pointers, do this recursively on the target type.  */
427       {
428         tree pointed_to_1 = TREE_TYPE (t1);
429         tree pointed_to_2 = TREE_TYPE (t2);
430         tree target = composite_type (pointed_to_1, pointed_to_2);
431         t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
432         t1 = build_type_attribute_variant (t1, attributes);
433         return qualify_type (t1, t2);
434       }
435
436     case ARRAY_TYPE:
437       {
438         tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
439         int quals;
440         tree unqual_elt;
441         tree d1 = TYPE_DOMAIN (t1);
442         tree d2 = TYPE_DOMAIN (t2);
443         bool d1_variable, d2_variable;
444         bool d1_zero, d2_zero;
445         bool t1_complete, t2_complete;
446
447         /* We should not have any type quals on arrays at all.  */
448         gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
449                     && !TYPE_QUALS_NO_ADDR_SPACE (t2));
450
451         t1_complete = COMPLETE_TYPE_P (t1);
452         t2_complete = COMPLETE_TYPE_P (t2);
453
454         d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
455         d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
456
457         d1_variable = (!d1_zero
458                        && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
459                            || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
460         d2_variable = (!d2_zero
461                        && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
462                            || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
463         d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
464         d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
465
466         /* Save space: see if the result is identical to one of the args.  */
467         if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
468             && (d2_variable || d2_zero || !d1_variable))
469           return build_type_attribute_variant (t1, attributes);
470         if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
471             && (d1_variable || d1_zero || !d2_variable))
472           return build_type_attribute_variant (t2, attributes);
473
474         if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
475           return build_type_attribute_variant (t1, attributes);
476         if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
477           return build_type_attribute_variant (t2, attributes);
478
479         /* Merge the element types, and have a size if either arg has
480            one.  We may have qualifiers on the element types.  To set
481            up TYPE_MAIN_VARIANT correctly, we need to form the
482            composite of the unqualified types and add the qualifiers
483            back at the end.  */
484         quals = TYPE_QUALS (strip_array_types (elt));
485         unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
486         t1 = build_array_type (unqual_elt,
487                                TYPE_DOMAIN ((TYPE_DOMAIN (t1)
488                                              && (d2_variable
489                                                  || d2_zero
490                                                  || !d1_variable))
491                                             ? t1
492                                             : t2));
493         /* Ensure a composite type involving a zero-length array type
494            is a zero-length type not an incomplete type.  */
495         if (d1_zero && d2_zero
496             && (t1_complete || t2_complete)
497             && !COMPLETE_TYPE_P (t1))
498           {
499             TYPE_SIZE (t1) = bitsize_zero_node;
500             TYPE_SIZE_UNIT (t1) = size_zero_node;
501           }
502         t1 = c_build_qualified_type (t1, quals);
503         return build_type_attribute_variant (t1, attributes);
504       }
505
506     case ENUMERAL_TYPE:
507     case RECORD_TYPE:
508     case UNION_TYPE:
509       if (attributes != NULL)
510         {
511           /* Try harder not to create a new aggregate type.  */
512           if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
513             return t1;
514           if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
515             return t2;
516         }
517       return build_type_attribute_variant (t1, attributes);
518
519     case FUNCTION_TYPE:
520       /* Function types: prefer the one that specified arg types.
521          If both do, merge the arg types.  Also merge the return types.  */
522       {
523         tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
524         tree p1 = TYPE_ARG_TYPES (t1);
525         tree p2 = TYPE_ARG_TYPES (t2);
526         int len;
527         tree newargs, n;
528         int i;
529
530         /* Save space: see if the result is identical to one of the args.  */
531         if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
532           return build_functype_attribute_variant (t1, t2, attributes);
533         if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
534           return build_functype_attribute_variant (t2, t1, attributes);
535
536         /* Simple way if one arg fails to specify argument types.  */
537         if (TYPE_ARG_TYPES (t1) == NULL_TREE)
538          {
539             t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
540             t1 = build_type_attribute_variant (t1, attributes);
541             return qualify_type (t1, t2);
542          }
543         if (TYPE_ARG_TYPES (t2) == NULL_TREE)
544          {
545            t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
546            t1 = build_type_attribute_variant (t1, attributes);
547            return qualify_type (t1, t2);
548          }
549
550         /* If both args specify argument types, we must merge the two
551            lists, argument by argument.  */
552
553         for (len = 0, newargs = p1;
554              newargs && newargs != void_list_node;
555              len++, newargs = TREE_CHAIN (newargs))
556           ;
557
558         for (i = 0; i < len; i++)
559           newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
560
561         n = newargs;
562
563         for (; p1 && p1 != void_list_node;
564              p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
565           {
566             /* A null type means arg type is not specified.
567                Take whatever the other function type has.  */
568             if (TREE_VALUE (p1) == NULL_TREE)
569               {
570                 TREE_VALUE (n) = TREE_VALUE (p2);
571                 goto parm_done;
572               }
573             if (TREE_VALUE (p2) == NULL_TREE)
574               {
575                 TREE_VALUE (n) = TREE_VALUE (p1);
576                 goto parm_done;
577               }
578
579             /* Given  wait (union {union wait *u; int *i} *)
580                and  wait (union wait *),
581                prefer  union wait *  as type of parm.  */
582             if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
583                 && TREE_VALUE (p1) != TREE_VALUE (p2))
584               {
585                 tree memb;
586                 tree mv2 = TREE_VALUE (p2);
587                 if (mv2 && mv2 != error_mark_node
588                     && TREE_CODE (mv2) != ARRAY_TYPE)
589                   mv2 = TYPE_MAIN_VARIANT (mv2);
590                 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
591                      memb; memb = DECL_CHAIN (memb))
592                   {
593                     tree mv3 = TREE_TYPE (memb);
594                     if (mv3 && mv3 != error_mark_node
595                         && TREE_CODE (mv3) != ARRAY_TYPE)
596                       mv3 = TYPE_MAIN_VARIANT (mv3);
597                     if (comptypes (mv3, mv2))
598                       {
599                         TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
600                                                          TREE_VALUE (p2));
601                         pedwarn (input_location, OPT_Wpedantic,
602                                  "function types not truly compatible in ISO C");
603                         goto parm_done;
604                       }
605                   }
606               }
607             if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
608                 && TREE_VALUE (p2) != TREE_VALUE (p1))
609               {
610                 tree memb;
611                 tree mv1 = TREE_VALUE (p1);
612                 if (mv1 && mv1 != error_mark_node
613                     && TREE_CODE (mv1) != ARRAY_TYPE)
614                   mv1 = TYPE_MAIN_VARIANT (mv1);
615                 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
616                      memb; memb = DECL_CHAIN (memb))
617                   {
618                     tree mv3 = TREE_TYPE (memb);
619                     if (mv3 && mv3 != error_mark_node
620                         && TREE_CODE (mv3) != ARRAY_TYPE)
621                       mv3 = TYPE_MAIN_VARIANT (mv3);
622                     if (comptypes (mv3, mv1))
623                       {
624                         TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
625                                                          TREE_VALUE (p1));
626                         pedwarn (input_location, OPT_Wpedantic,
627                                  "function types not truly compatible in ISO C");
628                         goto parm_done;
629                       }
630                   }
631               }
632             TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
633           parm_done: ;
634           }
635
636         t1 = build_function_type (valtype, newargs);
637         t1 = qualify_type (t1, t2);
638       }
639       /* FALLTHRU */
640
641     default:
642       return build_type_attribute_variant (t1, attributes);
643     }
644
645 }
646
647 /* Return the type of a conditional expression between pointers to
648    possibly differently qualified versions of compatible types.
649
650    We assume that comp_target_types has already been done and returned
651    nonzero; if that isn't so, this may crash.  */
652
653 static tree
654 common_pointer_type (tree t1, tree t2)
655 {
656   tree attributes;
657   tree pointed_to_1, mv1;
658   tree pointed_to_2, mv2;
659   tree target;
660   unsigned target_quals;
661   addr_space_t as1, as2, as_common;
662   int quals1, quals2;
663
664   /* Save time if the two types are the same.  */
665
666   if (t1 == t2) return t1;
667
668   /* If one type is nonsense, use the other.  */
669   if (t1 == error_mark_node)
670     return t2;
671   if (t2 == error_mark_node)
672     return t1;
673
674   gcc_assert (TREE_CODE (t1) == POINTER_TYPE
675               && TREE_CODE (t2) == POINTER_TYPE);
676
677   /* Merge the attributes.  */
678   attributes = targetm.merge_type_attributes (t1, t2);
679
680   /* Find the composite type of the target types, and combine the
681      qualifiers of the two types' targets.  Do not lose qualifiers on
682      array element types by taking the TYPE_MAIN_VARIANT.  */
683   mv1 = pointed_to_1 = TREE_TYPE (t1);
684   mv2 = pointed_to_2 = TREE_TYPE (t2);
685   if (TREE_CODE (mv1) != ARRAY_TYPE)
686     mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
687   if (TREE_CODE (mv2) != ARRAY_TYPE)
688     mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
689   target = composite_type (mv1, mv2);
690
691   /* Strip array types to get correct qualifier for pointers to arrays */
692   quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
693   quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
694
695   /* For function types do not merge const qualifiers, but drop them
696      if used inconsistently.  The middle-end uses these to mark const
697      and noreturn functions.  */
698   if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
699     target_quals = (quals1 & quals2);
700   else
701     target_quals = (quals1 | quals2);
702
703   /* If the two named address spaces are different, determine the common
704      superset address space.  This is guaranteed to exist due to the
705      assumption that comp_target_type returned non-zero.  */
706   as1 = TYPE_ADDR_SPACE (pointed_to_1);
707   as2 = TYPE_ADDR_SPACE (pointed_to_2);
708   if (!addr_space_superset (as1, as2, &as_common))
709     gcc_unreachable ();
710
711   target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
712
713   t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
714   return build_type_attribute_variant (t1, attributes);
715 }
716
717 /* Return the common type for two arithmetic types under the usual
718    arithmetic conversions.  The default conversions have already been
719    applied, and enumerated types converted to their compatible integer
720    types.  The resulting type is unqualified and has no attributes.
721
722    This is the type for the result of most arithmetic operations
723    if the operands have the given two types.  */
724
725 static tree
726 c_common_type (tree t1, tree t2)
727 {
728   enum tree_code code1;
729   enum tree_code code2;
730
731   /* If one type is nonsense, use the other.  */
732   if (t1 == error_mark_node)
733     return t2;
734   if (t2 == error_mark_node)
735     return t1;
736
737   if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
738     t1 = TYPE_MAIN_VARIANT (t1);
739
740   if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
741     t2 = TYPE_MAIN_VARIANT (t2);
742
743   if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
744     {
745       tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t1));
746       t1 = build_type_attribute_variant (t1, attrs);
747     }
748
749   if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
750     {
751       tree attrs = affects_type_identity_attributes (TYPE_ATTRIBUTES (t2));
752       t2 = build_type_attribute_variant (t2, attrs);
753     }
754
755   /* Save time if the two types are the same.  */
756
757   if (t1 == t2) return t1;
758
759   code1 = TREE_CODE (t1);
760   code2 = TREE_CODE (t2);
761
762   gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
763               || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
764               || code1 == INTEGER_TYPE);
765   gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
766               || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
767               || code2 == INTEGER_TYPE);
768
769   /* When one operand is a decimal float type, the other operand cannot be
770      a generic float type or a complex type.  We also disallow vector types
771      here.  */
772   if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
773       && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
774     {
775       if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
776         {
777           error ("cannot mix operands of decimal floating and vector types");
778           return error_mark_node;
779         }
780       if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
781         {
782           error ("cannot mix operands of decimal floating and complex types");
783           return error_mark_node;
784         }
785       if (code1 == REAL_TYPE && code2 == REAL_TYPE)
786         {
787           error ("cannot mix operands of decimal floating "
788                  "and other floating types");
789           return error_mark_node;
790         }
791     }
792
793   /* If one type is a vector type, return that type.  (How the usual
794      arithmetic conversions apply to the vector types extension is not
795      precisely specified.)  */
796   if (code1 == VECTOR_TYPE)
797     return t1;
798
799   if (code2 == VECTOR_TYPE)
800     return t2;
801
802   /* If one type is complex, form the common type of the non-complex
803      components, then make that complex.  Use T1 or T2 if it is the
804      required type.  */
805   if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
806     {
807       tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
808       tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
809       tree subtype = c_common_type (subtype1, subtype2);
810
811       if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
812         return t1;
813       else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
814         return t2;
815       else
816         return build_complex_type (subtype);
817     }
818
819   /* If only one is real, use it as the result.  */
820
821   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
822     return t1;
823
824   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
825     return t2;
826
827   /* If both are real and either are decimal floating point types, use
828      the decimal floating point type with the greater precision. */
829
830   if (code1 == REAL_TYPE && code2 == REAL_TYPE)
831     {
832       if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
833           || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
834         return dfloat128_type_node;
835       else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
836                || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
837         return dfloat64_type_node;
838       else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
839                || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
840         return dfloat32_type_node;
841     }
842
843   /* Deal with fixed-point types.  */
844   if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
845     {
846       unsigned int unsignedp = 0, satp = 0;
847       scalar_mode m1, m2;
848       unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
849
850       m1 = SCALAR_TYPE_MODE (t1);
851       m2 = SCALAR_TYPE_MODE (t2);
852
853       /* If one input type is saturating, the result type is saturating.  */
854       if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
855         satp = 1;
856
857       /* If both fixed-point types are unsigned, the result type is unsigned.
858          When mixing fixed-point and integer types, follow the sign of the
859          fixed-point type.
860          Otherwise, the result type is signed.  */
861       if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
862            && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
863           || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
864               && TYPE_UNSIGNED (t1))
865           || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
866               && TYPE_UNSIGNED (t2)))
867         unsignedp = 1;
868
869       /* The result type is signed.  */
870       if (unsignedp == 0)
871         {
872           /* If the input type is unsigned, we need to convert to the
873              signed type.  */
874           if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
875             {
876               enum mode_class mclass = (enum mode_class) 0;
877               if (GET_MODE_CLASS (m1) == MODE_UFRACT)
878                 mclass = MODE_FRACT;
879               else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
880                 mclass = MODE_ACCUM;
881               else
882                 gcc_unreachable ();
883               m1 = as_a <scalar_mode>
884                 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
885             }
886           if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
887             {
888               enum mode_class mclass = (enum mode_class) 0;
889               if (GET_MODE_CLASS (m2) == MODE_UFRACT)
890                 mclass = MODE_FRACT;
891               else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
892                 mclass = MODE_ACCUM;
893               else
894                 gcc_unreachable ();
895               m2 = as_a <scalar_mode>
896                 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
897             }
898         }
899
900       if (code1 == FIXED_POINT_TYPE)
901         {
902           fbit1 = GET_MODE_FBIT (m1);
903           ibit1 = GET_MODE_IBIT (m1);
904         }
905       else
906         {
907           fbit1 = 0;
908           /* Signed integers need to subtract one sign bit.  */
909           ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
910         }
911
912       if (code2 == FIXED_POINT_TYPE)
913         {
914           fbit2 = GET_MODE_FBIT (m2);
915           ibit2 = GET_MODE_IBIT (m2);
916         }
917       else
918         {
919           fbit2 = 0;
920           /* Signed integers need to subtract one sign bit.  */
921           ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
922         }
923
924       max_ibit = ibit1 >= ibit2 ?  ibit1 : ibit2;
925       max_fbit = fbit1 >= fbit2 ?  fbit1 : fbit2;
926       return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
927                                                  satp);
928     }
929
930   /* Both real or both integers; use the one with greater precision.  */
931
932   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
933     return t1;
934   else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
935     return t2;
936
937   /* Same precision.  Prefer long longs to longs to ints when the
938      same precision, following the C99 rules on integer type rank
939      (which are equivalent to the C90 rules for C90 types).  */
940
941   if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
942       || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
943     return long_long_unsigned_type_node;
944
945   if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
946       || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
947     {
948       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
949         return long_long_unsigned_type_node;
950       else
951         return long_long_integer_type_node;
952     }
953
954   if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
955       || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
956     return long_unsigned_type_node;
957
958   if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
959       || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
960     {
961       /* But preserve unsignedness from the other type,
962          since long cannot hold all the values of an unsigned int.  */
963       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
964         return long_unsigned_type_node;
965       else
966         return long_integer_type_node;
967     }
968
969   /* For floating types of the same TYPE_PRECISION (which we here
970      assume means either the same set of values, or sets of values
971      neither a subset of the other, with behavior being undefined in
972      the latter case), follow the rules from TS 18661-3: prefer
973      interchange types _FloatN, then standard types long double,
974      double, float, then extended types _FloatNx.  For extended types,
975      check them starting with _Float128x as that seems most consistent
976      in spirit with preferring long double to double; for interchange
977      types, also check in that order for consistency although it's not
978      possible for more than one of them to have the same
979      precision.  */
980   tree mv1 = TYPE_MAIN_VARIANT (t1);
981   tree mv2 = TYPE_MAIN_VARIANT (t2);
982
983   for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
984     if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
985       return FLOATN_TYPE_NODE (i);
986
987   /* Likewise, prefer long double to double even if same size.  */
988   if (mv1 == long_double_type_node || mv2 == long_double_type_node)
989     return long_double_type_node;
990
991   /* Likewise, prefer double to float even if same size.
992      We got a couple of embedded targets with 32 bit doubles, and the
993      pdp11 might have 64 bit floats.  */
994   if (mv1 == double_type_node || mv2 == double_type_node)
995     return double_type_node;
996
997   if (mv1 == float_type_node || mv2 == float_type_node)
998     return float_type_node;
999
1000   for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
1001     if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
1002       return FLOATNX_TYPE_NODE (i);
1003
1004   /* Otherwise prefer the unsigned one.  */
1005
1006   if (TYPE_UNSIGNED (t1))
1007     return t1;
1008   else
1009     return t2;
1010 }
1011 \f
1012 /* Wrapper around c_common_type that is used by c-common.cc and other
1013    front end optimizations that remove promotions.  ENUMERAL_TYPEs
1014    are allowed here and are converted to their compatible integer types.
1015    BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1016    preferably a non-Boolean type as the common type.  */
1017 tree
1018 common_type (tree t1, tree t2)
1019 {
1020   if (TREE_CODE (t1) == ENUMERAL_TYPE)
1021     t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1022   if (TREE_CODE (t2) == ENUMERAL_TYPE)
1023     t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1024
1025   /* If both types are BOOLEAN_TYPE, then return boolean_type_node.  */
1026   if (TREE_CODE (t1) == BOOLEAN_TYPE
1027       && TREE_CODE (t2) == BOOLEAN_TYPE)
1028     return boolean_type_node;
1029
1030   /* If either type is BOOLEAN_TYPE, then return the other.  */
1031   if (TREE_CODE (t1) == BOOLEAN_TYPE)
1032     return t2;
1033   if (TREE_CODE (t2) == BOOLEAN_TYPE)
1034     return t1;
1035
1036   return c_common_type (t1, t2);
1037 }
1038
1039 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1040    or various other operations.  Return 2 if they are compatible
1041    but a warning may be needed if you use them together.  */
1042
1043 int
1044 comptypes (tree type1, tree type2)
1045 {
1046   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1047   int val;
1048
1049   val = comptypes_internal (type1, type2, NULL, NULL);
1050   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1051
1052   return val;
1053 }
1054
1055 /* Like comptypes, but if it returns non-zero because enum and int are
1056    compatible, it sets *ENUM_AND_INT_P to true.  */
1057
1058 int
1059 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1060 {
1061   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1062   int val;
1063
1064   val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1065   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1066
1067   return val;
1068 }
1069
1070 /* Like comptypes, but if it returns nonzero for different types, it
1071    sets *DIFFERENT_TYPES_P to true.  */
1072
1073 int
1074 comptypes_check_different_types (tree type1, tree type2,
1075                                  bool *different_types_p)
1076 {
1077   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1078   int val;
1079
1080   val = comptypes_internal (type1, type2, NULL, different_types_p);
1081   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1082
1083   return val;
1084 }
1085 \f
1086 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1087    or various other operations.  Return 2 if they are compatible
1088    but a warning may be needed if you use them together.  If
1089    ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1090    compatible integer type, then this sets *ENUM_AND_INT_P to true;
1091    *ENUM_AND_INT_P is never set to false.  If DIFFERENT_TYPES_P is not
1092    NULL, and the types are compatible but different enough not to be
1093    permitted in C11 typedef redeclarations, then this sets
1094    *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1095    false, but may or may not be set if the types are incompatible.
1096    This differs from comptypes, in that we don't free the seen
1097    types.  */
1098
1099 static int
1100 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1101                     bool *different_types_p)
1102 {
1103   const_tree t1 = type1;
1104   const_tree t2 = type2;
1105   int attrval, val;
1106
1107   /* Suppress errors caused by previously reported errors.  */
1108
1109   if (t1 == t2 || !t1 || !t2
1110       || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1111     return 1;
1112
1113   /* Enumerated types are compatible with integer types, but this is
1114      not transitive: two enumerated types in the same translation unit
1115      are compatible with each other only if they are the same type.  */
1116
1117   if (TREE_CODE (t1) == ENUMERAL_TYPE
1118       && COMPLETE_TYPE_P (t1)
1119       && TREE_CODE (t2) != ENUMERAL_TYPE)
1120     {
1121       t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1122       if (TREE_CODE (t2) != VOID_TYPE)
1123         {
1124           if (enum_and_int_p != NULL)
1125             *enum_and_int_p = true;
1126           if (different_types_p != NULL)
1127             *different_types_p = true;
1128         }
1129     }
1130   else if (TREE_CODE (t2) == ENUMERAL_TYPE
1131            && COMPLETE_TYPE_P (t2)
1132            && TREE_CODE (t1) != ENUMERAL_TYPE)
1133     {
1134       t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1135       if (TREE_CODE (t1) != 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
1144   if (t1 == t2)
1145     return 1;
1146
1147   /* Different classes of types can't be compatible.  */
1148
1149   if (TREE_CODE (t1) != TREE_CODE (t2))
1150     return 0;
1151
1152   /* Qualifiers must match. C99 6.7.3p9 */
1153
1154   if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1155     return 0;
1156
1157   /* Allow for two different type nodes which have essentially the same
1158      definition.  Note that we already checked for equality of the type
1159      qualifiers (just above).  */
1160
1161   if (TREE_CODE (t1) != ARRAY_TYPE
1162       && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1163     return 1;
1164
1165   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1166   if (!(attrval = comp_type_attributes (t1, t2)))
1167      return 0;
1168
1169   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1170   val = 0;
1171
1172   switch (TREE_CODE (t1))
1173     {
1174     case INTEGER_TYPE:
1175     case FIXED_POINT_TYPE:
1176     case REAL_TYPE:
1177       /* With these nodes, we can't determine type equivalence by
1178          looking at what is stored in the nodes themselves, because
1179          two nodes might have different TYPE_MAIN_VARIANTs but still
1180          represent the same type.  For example, wchar_t and int could
1181          have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1182          TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1183          and are distinct types.  On the other hand, int and the
1184          following typedef
1185
1186            typedef int INT __attribute((may_alias));
1187
1188          have identical properties, different TYPE_MAIN_VARIANTs, but
1189          represent the same type.  The canonical type system keeps
1190          track of equivalence in this case, so we fall back on it.  */
1191       return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1192
1193     case POINTER_TYPE:
1194       /* Do not remove mode information.  */
1195       if (TYPE_MODE (t1) != TYPE_MODE (t2))
1196         break;
1197       val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1198              ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1199                                        enum_and_int_p, different_types_p));
1200       break;
1201
1202     case FUNCTION_TYPE:
1203       val = function_types_compatible_p (t1, t2, enum_and_int_p,
1204                                          different_types_p);
1205       break;
1206
1207     case ARRAY_TYPE:
1208       {
1209         tree d1 = TYPE_DOMAIN (t1);
1210         tree d2 = TYPE_DOMAIN (t2);
1211         bool d1_variable, d2_variable;
1212         bool d1_zero, d2_zero;
1213         val = 1;
1214
1215         /* Target types must match incl. qualifiers.  */
1216         if (TREE_TYPE (t1) != TREE_TYPE (t2)
1217             && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1218                                           enum_and_int_p,
1219                                           different_types_p)) == 0)
1220           return 0;
1221
1222         if (different_types_p != NULL
1223             && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1224           *different_types_p = true;
1225         /* Sizes must match unless one is missing or variable.  */
1226         if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1227           break;
1228
1229         d1_zero = !TYPE_MAX_VALUE (d1);
1230         d2_zero = !TYPE_MAX_VALUE (d2);
1231
1232         d1_variable = (!d1_zero
1233                        && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1234                            || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1235         d2_variable = (!d2_zero
1236                        && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1237                            || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1238         d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1239         d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1240
1241         if (different_types_p != NULL
1242             && d1_variable != d2_variable)
1243           *different_types_p = true;
1244         if (d1_variable || d2_variable)
1245           break;
1246         if (d1_zero && d2_zero)
1247           break;
1248         if (d1_zero || d2_zero
1249             || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1250             || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1251           val = 0;
1252
1253         break;
1254       }
1255
1256     case ENUMERAL_TYPE:
1257     case RECORD_TYPE:
1258     case UNION_TYPE:
1259       if (val != 1 && !same_translation_unit_p (t1, t2))
1260         {
1261           tree a1 = TYPE_ATTRIBUTES (t1);
1262           tree a2 = TYPE_ATTRIBUTES (t2);
1263
1264           if (! attribute_list_contained (a1, a2)
1265               && ! attribute_list_contained (a2, a1))
1266             break;
1267
1268           if (attrval != 2)
1269             return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1270                                                  different_types_p);
1271           val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1272                                               different_types_p);
1273         }
1274       break;
1275
1276     case VECTOR_TYPE:
1277       val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1278              && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1279                                     enum_and_int_p, different_types_p));
1280       break;
1281
1282     default:
1283       break;
1284     }
1285   return attrval == 2 && val == 1 ? 2 : val;
1286 }
1287
1288 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1289    their qualifiers, except for named address spaces.  If the pointers point to
1290    different named addresses, then we must determine if one address space is a
1291    subset of the other.  */
1292
1293 static int
1294 comp_target_types (location_t location, tree ttl, tree ttr)
1295 {
1296   int val;
1297   int val_ped;
1298   tree mvl = TREE_TYPE (ttl);
1299   tree mvr = TREE_TYPE (ttr);
1300   addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1301   addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1302   addr_space_t as_common;
1303   bool enum_and_int_p;
1304
1305   /* Fail if pointers point to incompatible address spaces.  */
1306   if (!addr_space_superset (asl, asr, &as_common))
1307     return 0;
1308
1309   /* For pedantic record result of comptypes on arrays before losing
1310      qualifiers on the element type below. */
1311   val_ped = 1;
1312
1313   if (TREE_CODE (mvl) == ARRAY_TYPE
1314       && TREE_CODE (mvr) == ARRAY_TYPE)
1315     val_ped = comptypes (mvl, mvr);
1316
1317   /* Qualifiers on element types of array types that are
1318      pointer targets are lost by taking their TYPE_MAIN_VARIANT.  */
1319
1320   mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1321          ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1322          : TYPE_MAIN_VARIANT (mvl));
1323
1324   mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1325          ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1326          : TYPE_MAIN_VARIANT (mvr));
1327
1328   enum_and_int_p = false;
1329   val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1330
1331   if (val == 1 && val_ped != 1)
1332     pedwarn_c11 (location, OPT_Wpedantic, "invalid use of pointers to arrays with different qualifiers "
1333                                           "in ISO C before C2X");
1334
1335   if (val == 2)
1336     pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1337
1338   if (val == 1 && enum_and_int_p && warn_cxx_compat)
1339     warning_at (location, OPT_Wc___compat,
1340                 "pointer target types incompatible in C++");
1341
1342   return val;
1343 }
1344 \f
1345 /* Subroutines of `comptypes'.  */
1346
1347 /* Determine whether two trees derive from the same translation unit.
1348    If the CONTEXT chain ends in a null, that tree's context is still
1349    being parsed, so if two trees have context chains ending in null,
1350    they're in the same translation unit.  */
1351
1352 bool
1353 same_translation_unit_p (const_tree t1, const_tree t2)
1354 {
1355   while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1356     switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1357       {
1358       case tcc_declaration:
1359         t1 = DECL_CONTEXT (t1); break;
1360       case tcc_type:
1361         t1 = TYPE_CONTEXT (t1); break;
1362       case tcc_exceptional:
1363         t1 = BLOCK_SUPERCONTEXT (t1); break;  /* assume block */
1364       default: gcc_unreachable ();
1365       }
1366
1367   while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1368     switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1369       {
1370       case tcc_declaration:
1371         t2 = DECL_CONTEXT (t2); break;
1372       case tcc_type:
1373         t2 = TYPE_CONTEXT (t2); break;
1374       case tcc_exceptional:
1375         t2 = BLOCK_SUPERCONTEXT (t2); break;  /* assume block */
1376       default: gcc_unreachable ();
1377       }
1378
1379   return t1 == t2;
1380 }
1381
1382 /* Allocate the seen two types, assuming that they are compatible. */
1383
1384 static struct tagged_tu_seen_cache *
1385 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1386 {
1387   struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1388   tu->next = tagged_tu_seen_base;
1389   tu->t1 = t1;
1390   tu->t2 = t2;
1391
1392   tagged_tu_seen_base = tu;
1393
1394   /* The C standard says that two structures in different translation
1395      units are compatible with each other only if the types of their
1396      fields are compatible (among other things).  We assume that they
1397      are compatible until proven otherwise when building the cache.
1398      An example where this can occur is:
1399      struct a
1400      {
1401        struct a *next;
1402      };
1403      If we are comparing this against a similar struct in another TU,
1404      and did not assume they were compatible, we end up with an infinite
1405      loop.  */
1406   tu->val = 1;
1407   return tu;
1408 }
1409
1410 /* Free the seen types until we get to TU_TIL. */
1411
1412 static void
1413 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1414 {
1415   const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1416   while (tu != tu_til)
1417     {
1418       const struct tagged_tu_seen_cache *const tu1
1419         = (const struct tagged_tu_seen_cache *) tu;
1420       tu = tu1->next;
1421       XDELETE (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1422     }
1423   tagged_tu_seen_base = tu_til;
1424 }
1425
1426 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1427    compatible.  If the two types are not the same (which has been
1428    checked earlier), this can only happen when multiple translation
1429    units are being compiled.  See C99 6.2.7 paragraph 1 for the exact
1430    rules.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1431    comptypes_internal.  */
1432
1433 static int
1434 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1435                               bool *enum_and_int_p, bool *different_types_p)
1436 {
1437   tree s1, s2;
1438   bool needs_warning = false;
1439
1440   /* We have to verify that the tags of the types are the same.  This
1441      is harder than it looks because this may be a typedef, so we have
1442      to go look at the original type.  It may even be a typedef of a
1443      typedef...
1444      In the case of compiler-created builtin structs the TYPE_DECL
1445      may be a dummy, with no DECL_ORIGINAL_TYPE.  Don't fault.  */
1446   while (TYPE_NAME (t1)
1447          && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1448          && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1449     t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1450
1451   while (TYPE_NAME (t2)
1452          && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1453          && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1454     t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1455
1456   /* C90 didn't have the requirement that the two tags be the same.  */
1457   if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1458     return 0;
1459
1460   /* C90 didn't say what happened if one or both of the types were
1461      incomplete; we choose to follow C99 rules here, which is that they
1462      are compatible.  */
1463   if (TYPE_SIZE (t1) == NULL
1464       || TYPE_SIZE (t2) == NULL)
1465     return 1;
1466
1467   {
1468     const struct tagged_tu_seen_cache * tts_i;
1469     for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1470       if (tts_i->t1 == t1 && tts_i->t2 == t2)
1471         return tts_i->val;
1472   }
1473
1474   switch (TREE_CODE (t1))
1475     {
1476     case ENUMERAL_TYPE:
1477       {
1478         struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1479         /* Speed up the case where the type values are in the same order.  */
1480         tree tv1 = TYPE_VALUES (t1);
1481         tree tv2 = TYPE_VALUES (t2);
1482
1483         if (tv1 == tv2)
1484           {
1485             return 1;
1486           }
1487
1488         for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1489           {
1490             if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1491               break;
1492             if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1493               {
1494                 tu->val = 0;
1495                 return 0;
1496               }
1497           }
1498
1499         if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1500           {
1501             return 1;
1502           }
1503         if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1504           {
1505             tu->val = 0;
1506             return 0;
1507           }
1508
1509         if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1510           {
1511             tu->val = 0;
1512             return 0;
1513           }
1514
1515         for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1516           {
1517             s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1518             if (s2 == NULL
1519                 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1520               {
1521                 tu->val = 0;
1522                 return 0;
1523               }
1524           }
1525         return 1;
1526       }
1527
1528     case UNION_TYPE:
1529       {
1530         struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1531         if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1532           {
1533             tu->val = 0;
1534             return 0;
1535           }
1536
1537         /*  Speed up the common case where the fields are in the same order. */
1538         for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1539              s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1540           {
1541             int result;
1542
1543             if (DECL_NAME (s1) != DECL_NAME (s2))
1544               break;
1545             result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1546                                          enum_and_int_p, different_types_p);
1547
1548             if (result != 1 && !DECL_NAME (s1))
1549               break;
1550             if (result == 0)
1551               {
1552                 tu->val = 0;
1553                 return 0;
1554               }
1555             if (result == 2)
1556               needs_warning = true;
1557
1558             if (TREE_CODE (s1) == FIELD_DECL
1559                 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1560                                      DECL_FIELD_BIT_OFFSET (s2)) != 1)
1561               {
1562                 tu->val = 0;
1563                 return 0;
1564               }
1565           }
1566         if (!s1 && !s2)
1567           {
1568             tu->val = needs_warning ? 2 : 1;
1569             return tu->val;
1570           }
1571
1572         for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1573           {
1574             bool ok = false;
1575
1576             for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1577               if (DECL_NAME (s1) == DECL_NAME (s2))
1578                 {
1579                   int result;
1580
1581                   result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1582                                                enum_and_int_p,
1583                                                different_types_p);
1584
1585                   if (result != 1 && !DECL_NAME (s1))
1586                     continue;
1587                   if (result == 0)
1588                     {
1589                       tu->val = 0;
1590                       return 0;
1591                     }
1592                   if (result == 2)
1593                     needs_warning = true;
1594
1595                   if (TREE_CODE (s1) == FIELD_DECL
1596                       && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1597                                            DECL_FIELD_BIT_OFFSET (s2)) != 1)
1598                     break;
1599
1600                   ok = true;
1601                   break;
1602                 }
1603             if (!ok)
1604               {
1605                 tu->val = 0;
1606                 return 0;
1607               }
1608           }
1609         tu->val = needs_warning ? 2 : 10;
1610         return tu->val;
1611       }
1612
1613     case RECORD_TYPE:
1614       {
1615         struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1616
1617         for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1618              s1 && s2;
1619              s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1620           {
1621             int result;
1622             if (TREE_CODE (s1) != TREE_CODE (s2)
1623                 || DECL_NAME (s1) != DECL_NAME (s2))
1624               break;
1625             result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1626                                          enum_and_int_p, different_types_p);
1627             if (result == 0)
1628               break;
1629             if (result == 2)
1630               needs_warning = true;
1631
1632             if (TREE_CODE (s1) == FIELD_DECL
1633                 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1634                                      DECL_FIELD_BIT_OFFSET (s2)) != 1)
1635               break;
1636           }
1637         if (s1 && s2)
1638           tu->val = 0;
1639         else
1640           tu->val = needs_warning ? 2 : 1;
1641         return tu->val;
1642       }
1643
1644     default:
1645       gcc_unreachable ();
1646     }
1647 }
1648
1649 /* Return 1 if two function types F1 and F2 are compatible.
1650    If either type specifies no argument types,
1651    the other must specify a fixed number of self-promoting arg types.
1652    Otherwise, if one type specifies only the number of arguments,
1653    the other must specify that number of self-promoting arg types.
1654    Otherwise, the argument types must match.
1655    ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal.  */
1656
1657 static int
1658 function_types_compatible_p (const_tree f1, const_tree f2,
1659                              bool *enum_and_int_p, bool *different_types_p)
1660 {
1661   tree args1, args2;
1662   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1663   int val = 1;
1664   int val1;
1665   tree ret1, ret2;
1666
1667   ret1 = TREE_TYPE (f1);
1668   ret2 = TREE_TYPE (f2);
1669
1670   /* 'volatile' qualifiers on a function's return type used to mean
1671      the function is noreturn.  */
1672   if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1673     pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1674   if (TYPE_VOLATILE (ret1))
1675     ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1676                                  TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1677   if (TYPE_VOLATILE (ret2))
1678     ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1679                                  TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1680   val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1681   if (val == 0)
1682     return 0;
1683
1684   args1 = TYPE_ARG_TYPES (f1);
1685   args2 = TYPE_ARG_TYPES (f2);
1686
1687   if (different_types_p != NULL
1688       && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1689     *different_types_p = true;
1690
1691   /* An unspecified parmlist matches any specified parmlist
1692      whose argument types don't need default promotions.  */
1693
1694   if (args1 == NULL_TREE)
1695     {
1696       if (!self_promoting_args_p (args2))
1697         return 0;
1698       /* If one of these types comes from a non-prototype fn definition,
1699          compare that with the other type's arglist.
1700          If they don't match, ask for a warning (but no error).  */
1701       if (TYPE_ACTUAL_ARG_TYPES (f1)
1702           && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1703                                       enum_and_int_p, different_types_p) != 1)
1704         val = 2;
1705       return val;
1706     }
1707   if (args2 == NULL_TREE)
1708     {
1709       if (!self_promoting_args_p (args1))
1710         return 0;
1711       if (TYPE_ACTUAL_ARG_TYPES (f2)
1712           && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1713                                       enum_and_int_p, different_types_p) != 1)
1714         val = 2;
1715       return val;
1716     }
1717
1718   /* Both types have argument lists: compare them and propagate results.  */
1719   val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1720                                   different_types_p);
1721   return val1 != 1 ? val1 : val;
1722 }
1723
1724 /* Check two lists of types for compatibility, returning 0 for
1725    incompatible, 1 for compatible, or 2 for compatible with
1726    warning.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1727    comptypes_internal.  */
1728
1729 static int
1730 type_lists_compatible_p (const_tree args1, const_tree args2,
1731                          bool *enum_and_int_p, bool *different_types_p)
1732 {
1733   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1734   int val = 1;
1735   int newval = 0;
1736
1737   while (1)
1738     {
1739       tree a1, mv1, a2, mv2;
1740       if (args1 == NULL_TREE && args2 == NULL_TREE)
1741         return val;
1742       /* If one list is shorter than the other,
1743          they fail to match.  */
1744       if (args1 == NULL_TREE || args2 == NULL_TREE)
1745         return 0;
1746       mv1 = a1 = TREE_VALUE (args1);
1747       mv2 = a2 = TREE_VALUE (args2);
1748       if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1749         mv1 = (TYPE_ATOMIC (mv1)
1750                ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1751                                          TYPE_QUAL_ATOMIC)
1752                : TYPE_MAIN_VARIANT (mv1));
1753       if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1754         mv2 = (TYPE_ATOMIC (mv2)
1755                ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1756                                          TYPE_QUAL_ATOMIC)
1757                : TYPE_MAIN_VARIANT (mv2));
1758       /* A null pointer instead of a type
1759          means there is supposed to be an argument
1760          but nothing is specified about what type it has.
1761          So match anything that self-promotes.  */
1762       if (different_types_p != NULL
1763           && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1764         *different_types_p = true;
1765       if (a1 == NULL_TREE)
1766         {
1767           if (c_type_promotes_to (a2) != a2)
1768             return 0;
1769         }
1770       else if (a2 == NULL_TREE)
1771         {
1772           if (c_type_promotes_to (a1) != a1)
1773             return 0;
1774         }
1775       /* If one of the lists has an error marker, ignore this arg.  */
1776       else if (TREE_CODE (a1) == ERROR_MARK
1777                || TREE_CODE (a2) == ERROR_MARK)
1778         ;
1779       else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1780                                               different_types_p)))
1781         {
1782           if (different_types_p != NULL)
1783             *different_types_p = true;
1784           /* Allow  wait (union {union wait *u; int *i} *)
1785              and  wait (union wait *)  to be compatible.  */
1786           if (TREE_CODE (a1) == UNION_TYPE
1787               && (TYPE_NAME (a1) == NULL_TREE
1788                   || TYPE_TRANSPARENT_AGGR (a1))
1789               && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1790               && tree_int_cst_equal (TYPE_SIZE (a1),
1791                                      TYPE_SIZE (a2)))
1792             {
1793               tree memb;
1794               for (memb = TYPE_FIELDS (a1);
1795                    memb; memb = DECL_CHAIN (memb))
1796                 {
1797                   tree mv3 = TREE_TYPE (memb);
1798                   if (mv3 && mv3 != error_mark_node
1799                       && TREE_CODE (mv3) != ARRAY_TYPE)
1800                     mv3 = (TYPE_ATOMIC (mv3)
1801                            ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1802                                                      TYPE_QUAL_ATOMIC)
1803                            : TYPE_MAIN_VARIANT (mv3));
1804                   if (comptypes_internal (mv3, mv2, enum_and_int_p,
1805                                           different_types_p))
1806                     break;
1807                 }
1808               if (memb == NULL_TREE)
1809                 return 0;
1810             }
1811           else if (TREE_CODE (a2) == UNION_TYPE
1812                    && (TYPE_NAME (a2) == NULL_TREE
1813                        || TYPE_TRANSPARENT_AGGR (a2))
1814                    && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1815                    && tree_int_cst_equal (TYPE_SIZE (a2),
1816                                           TYPE_SIZE (a1)))
1817             {
1818               tree memb;
1819               for (memb = TYPE_FIELDS (a2);
1820                    memb; memb = DECL_CHAIN (memb))
1821                 {
1822                   tree mv3 = TREE_TYPE (memb);
1823                   if (mv3 && mv3 != error_mark_node
1824                       && TREE_CODE (mv3) != ARRAY_TYPE)
1825                     mv3 = (TYPE_ATOMIC (mv3)
1826                            ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1827                                                      TYPE_QUAL_ATOMIC)
1828                            : TYPE_MAIN_VARIANT (mv3));
1829                   if (comptypes_internal (mv3, mv1, enum_and_int_p,
1830                                           different_types_p))
1831                     break;
1832                 }
1833               if (memb == NULL_TREE)
1834                 return 0;
1835             }
1836           else
1837             return 0;
1838         }
1839
1840       /* comptypes said ok, but record if it said to warn.  */
1841       if (newval > val)
1842         val = newval;
1843
1844       args1 = TREE_CHAIN (args1);
1845       args2 = TREE_CHAIN (args2);
1846     }
1847 }
1848 \f
1849 /* Compute the size to increment a pointer by.  When a function type or void
1850    type or incomplete type is passed, size_one_node is returned.
1851    This function does not emit any diagnostics; the caller is responsible
1852    for that.  */
1853
1854 static tree
1855 c_size_in_bytes (const_tree type)
1856 {
1857   enum tree_code code = TREE_CODE (type);
1858
1859   if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1860       || !COMPLETE_TYPE_P (type))
1861     return size_one_node;
1862
1863   /* Convert in case a char is more than one unit.  */
1864   return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1865                          size_int (TYPE_PRECISION (char_type_node)
1866                                    / BITS_PER_UNIT));
1867 }
1868 \f
1869 /* Return either DECL or its known constant value (if it has one).  */
1870
1871 tree
1872 decl_constant_value_1 (tree decl, bool in_init)
1873 {
1874   if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL.  */
1875       TREE_CODE (decl) != PARM_DECL
1876       && !TREE_THIS_VOLATILE (decl)
1877       && TREE_READONLY (decl)
1878       && DECL_INITIAL (decl) != NULL_TREE
1879       && !error_operand_p (DECL_INITIAL (decl))
1880       /* This is invalid if initial value is not constant.
1881          If it has either a function call, a memory reference,
1882          or a variable, then re-evaluating it could give different results.  */
1883       && TREE_CONSTANT (DECL_INITIAL (decl))
1884       /* Check for cases where this is sub-optimal, even though valid.  */
1885       && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1886     return DECL_INITIAL (decl);
1887   return decl;
1888 }
1889
1890 /* Return either DECL or its known constant value (if it has one).
1891    Like the above, but always return decl outside of functions.  */
1892
1893 tree
1894 decl_constant_value (tree decl)
1895 {
1896   /* Don't change a variable array bound or initial value to a constant
1897      in a place where a variable is invalid.  */
1898   return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1899 }
1900
1901 /* Convert the array expression EXP to a pointer.  */
1902 static tree
1903 array_to_pointer_conversion (location_t loc, tree exp)
1904 {
1905   tree orig_exp = exp;
1906   tree type = TREE_TYPE (exp);
1907   tree adr;
1908   tree restype = TREE_TYPE (type);
1909   tree ptrtype;
1910
1911   gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1912
1913   STRIP_TYPE_NOPS (exp);
1914
1915   copy_warning (exp, orig_exp);
1916
1917   ptrtype = build_pointer_type (restype);
1918
1919   if (INDIRECT_REF_P (exp))
1920     return convert (ptrtype, TREE_OPERAND (exp, 0));
1921
1922   /* In C++ array compound literals are temporary objects unless they are
1923      const or appear in namespace scope, so they are destroyed too soon
1924      to use them for much of anything  (c++/53220).  */
1925   if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1926     {
1927       tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1928       if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1929         warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1930                     "converting an array compound literal to a pointer "
1931                     "is ill-formed in C++");
1932     }
1933
1934   adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1935   return convert (ptrtype, adr);
1936 }
1937
1938 /* Convert the function expression EXP to a pointer.  */
1939 static tree
1940 function_to_pointer_conversion (location_t loc, tree exp)
1941 {
1942   tree orig_exp = exp;
1943
1944   gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1945
1946   STRIP_TYPE_NOPS (exp);
1947
1948   copy_warning (exp, orig_exp);
1949
1950   return build_unary_op (loc, ADDR_EXPR, exp, false);
1951 }
1952
1953 /* Mark EXP as read, not just set, for set but not used -Wunused
1954    warning purposes.  */
1955
1956 void
1957 mark_exp_read (tree exp)
1958 {
1959   switch (TREE_CODE (exp))
1960     {
1961     case VAR_DECL:
1962     case PARM_DECL:
1963       DECL_READ_P (exp) = 1;
1964       break;
1965     case ARRAY_REF:
1966     case COMPONENT_REF:
1967     case MODIFY_EXPR:
1968     case REALPART_EXPR:
1969     case IMAGPART_EXPR:
1970     CASE_CONVERT:
1971     case ADDR_EXPR:
1972     case VIEW_CONVERT_EXPR:
1973       mark_exp_read (TREE_OPERAND (exp, 0));
1974       break;
1975     case COMPOUND_EXPR:
1976       /* Pattern match what build_atomic_assign produces with modifycode
1977          NOP_EXPR.  */
1978       if (VAR_P (TREE_OPERAND (exp, 1))
1979           && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1980           && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1981         {
1982           tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1983           tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1984           if (TREE_CODE (t1) == TARGET_EXPR
1985               && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1986               && TREE_CODE (t2) == CALL_EXPR)
1987             {
1988               tree fndecl = get_callee_fndecl (t2);
1989               tree arg = NULL_TREE;
1990               if (fndecl
1991                   && TREE_CODE (fndecl) == FUNCTION_DECL
1992                   && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1993                   && call_expr_nargs (t2) >= 2)
1994                 switch (DECL_FUNCTION_CODE (fndecl))
1995                   {
1996                   case BUILT_IN_ATOMIC_STORE:
1997                     arg = CALL_EXPR_ARG (t2, 1);
1998                     break;
1999                   case BUILT_IN_ATOMIC_STORE_1:
2000                   case BUILT_IN_ATOMIC_STORE_2:
2001                   case BUILT_IN_ATOMIC_STORE_4:
2002                   case BUILT_IN_ATOMIC_STORE_8:
2003                   case BUILT_IN_ATOMIC_STORE_16:
2004                     arg = CALL_EXPR_ARG (t2, 0);
2005                     break;
2006                   default:
2007                     break;
2008                   }
2009               if (arg)
2010                 {
2011                   STRIP_NOPS (arg);
2012                   if (TREE_CODE (arg) == ADDR_EXPR
2013                       && DECL_P (TREE_OPERAND (arg, 0))
2014                       && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2015                     mark_exp_read (TREE_OPERAND (arg, 0));
2016                 }
2017             }
2018         }
2019       /* FALLTHRU */
2020     case C_MAYBE_CONST_EXPR:
2021       mark_exp_read (TREE_OPERAND (exp, 1));
2022       break;
2023     default:
2024       break;
2025     }
2026 }
2027
2028 /* Perform the default conversion of arrays and functions to pointers.
2029    Return the result of converting EXP.  For any other expression, just
2030    return EXP.
2031
2032    LOC is the location of the expression.  */
2033
2034 struct c_expr
2035 default_function_array_conversion (location_t loc, struct c_expr exp)
2036 {
2037   tree orig_exp = exp.value;
2038   tree type = TREE_TYPE (exp.value);
2039   enum tree_code code = TREE_CODE (type);
2040
2041   switch (code)
2042     {
2043     case ARRAY_TYPE:
2044       {
2045         bool not_lvalue = false;
2046         bool lvalue_array_p;
2047
2048         while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2049                 || CONVERT_EXPR_P (exp.value))
2050                && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2051           {
2052             if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2053               not_lvalue = true;
2054             exp.value = TREE_OPERAND (exp.value, 0);
2055           }
2056
2057         copy_warning (exp.value, orig_exp);
2058
2059         lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2060         if (!flag_isoc99 && !lvalue_array_p)
2061           {
2062             /* Before C99, non-lvalue arrays do not decay to pointers.
2063                Normally, using such an array would be invalid; but it can
2064                be used correctly inside sizeof or as a statement expression.
2065                Thus, do not give an error here; an error will result later.  */
2066             return exp;
2067           }
2068
2069         exp.value = array_to_pointer_conversion (loc, exp.value);
2070       }
2071       break;
2072     case FUNCTION_TYPE:
2073       exp.value = function_to_pointer_conversion (loc, exp.value);
2074       break;
2075     default:
2076       break;
2077     }
2078
2079   return exp;
2080 }
2081
2082 struct c_expr
2083 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2084 {
2085   mark_exp_read (exp.value);
2086   return default_function_array_conversion (loc, exp);
2087 }
2088
2089 /* Return whether EXPR should be treated as an atomic lvalue for the
2090    purposes of load and store handling.  */
2091
2092 static bool
2093 really_atomic_lvalue (tree expr)
2094 {
2095   if (error_operand_p (expr))
2096     return false;
2097   if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2098     return false;
2099   if (!lvalue_p (expr))
2100     return false;
2101
2102   /* Ignore _Atomic on register variables, since their addresses can't
2103      be taken so (a) atomicity is irrelevant and (b) the normal atomic
2104      sequences wouldn't work.  Ignore _Atomic on structures containing
2105      bit-fields, since accessing elements of atomic structures or
2106      unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2107      it's undefined at translation time or execution time, and the
2108      normal atomic sequences again wouldn't work.  */
2109   while (handled_component_p (expr))
2110     {
2111       if (TREE_CODE (expr) == COMPONENT_REF
2112           && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2113         return false;
2114       expr = TREE_OPERAND (expr, 0);
2115     }
2116   if (DECL_P (expr) && C_DECL_REGISTER (expr))
2117     return false;
2118   return true;
2119 }
2120
2121 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2122    including converting functions and arrays to pointers if CONVERT_P.
2123    If READ_P, also mark the expression as having been read.  */
2124
2125 struct c_expr
2126 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2127                           bool convert_p, bool read_p)
2128 {
2129   if (read_p)
2130     mark_exp_read (exp.value);
2131   if (convert_p)
2132     exp = default_function_array_conversion (loc, exp);
2133   if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2134     exp.value = require_complete_type (loc, exp.value);
2135   if (really_atomic_lvalue (exp.value))
2136     {
2137       vec<tree, va_gc> *params;
2138       tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2139       tree expr_type = TREE_TYPE (exp.value);
2140       tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2141       tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2142
2143       gcc_assert (TYPE_ATOMIC (expr_type));
2144
2145       /* Expansion of a generic atomic load may require an addition
2146          element, so allocate enough to prevent a resize.  */
2147       vec_alloc (params, 4);
2148
2149       /* Remove the qualifiers for the rest of the expressions and
2150          create the VAL temp variable to hold the RHS.  */
2151       nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2152       tmp = create_tmp_var_raw (nonatomic_type);
2153       tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2154       TREE_ADDRESSABLE (tmp) = 1;
2155       /* Do not disable warnings for TMP even though it's artificial.
2156          -Winvalid-memory-model depends on it.  */
2157
2158       /* Issue __atomic_load (&expr, &tmp, SEQ_CST);  */
2159       fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2160       params->quick_push (expr_addr);
2161       params->quick_push (tmp_addr);
2162       params->quick_push (seq_cst);
2163       func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2164
2165       /* EXPR is always read.  */
2166       mark_exp_read (exp.value);
2167
2168       /* Return tmp which contains the value loaded.  */
2169       exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2170                           NULL_TREE, NULL_TREE);
2171     }
2172   if (convert_p && !error_operand_p (exp.value)
2173       && (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
2174     exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
2175   return exp;
2176 }
2177
2178 /* EXP is an expression of integer type.  Apply the integer promotions
2179    to it and return the promoted value.  */
2180
2181 tree
2182 perform_integral_promotions (tree exp)
2183 {
2184   tree type = TREE_TYPE (exp);
2185   enum tree_code code = TREE_CODE (type);
2186
2187   gcc_assert (INTEGRAL_TYPE_P (type));
2188
2189   /* Normally convert enums to int,
2190      but convert wide enums to something wider.  */
2191   if (code == ENUMERAL_TYPE)
2192     {
2193       type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2194                                           TYPE_PRECISION (integer_type_node)),
2195                                      ((TYPE_PRECISION (type)
2196                                        >= TYPE_PRECISION (integer_type_node))
2197                                       && TYPE_UNSIGNED (type)));
2198
2199       return convert (type, exp);
2200     }
2201
2202   /* ??? This should no longer be needed now bit-fields have their
2203      proper types.  */
2204   if (TREE_CODE (exp) == COMPONENT_REF
2205       && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2206       /* If it's thinner than an int, promote it like a
2207          c_promoting_integer_type_p, otherwise leave it alone.  */
2208       && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2209                            TYPE_PRECISION (integer_type_node)) < 0)
2210     return convert (integer_type_node, exp);
2211
2212   if (c_promoting_integer_type_p (type))
2213     {
2214       /* Preserve unsignedness if not really getting any wider.  */
2215       if (TYPE_UNSIGNED (type)
2216           && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2217         return convert (unsigned_type_node, exp);
2218
2219       return convert (integer_type_node, exp);
2220     }
2221
2222   return exp;
2223 }
2224
2225
2226 /* Perform default promotions for C data used in expressions.
2227    Enumeral types or short or char are converted to int.
2228    In addition, manifest constants symbols are replaced by their values.  */
2229
2230 tree
2231 default_conversion (tree exp)
2232 {
2233   tree orig_exp;
2234   tree type = TREE_TYPE (exp);
2235   enum tree_code code = TREE_CODE (type);
2236   tree promoted_type;
2237
2238   mark_exp_read (exp);
2239
2240   /* Functions and arrays have been converted during parsing.  */
2241   gcc_assert (code != FUNCTION_TYPE);
2242   if (code == ARRAY_TYPE)
2243     return exp;
2244
2245   /* Constants can be used directly unless they're not loadable.  */
2246   if (TREE_CODE (exp) == CONST_DECL)
2247     exp = DECL_INITIAL (exp);
2248
2249   /* Strip no-op conversions.  */
2250   orig_exp = exp;
2251   STRIP_TYPE_NOPS (exp);
2252
2253   copy_warning (exp, orig_exp);
2254
2255   if (code == VOID_TYPE)
2256     {
2257       error_at (EXPR_LOC_OR_LOC (exp, input_location),
2258                 "void value not ignored as it ought to be");
2259       return error_mark_node;
2260     }
2261
2262   exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2263   if (exp == error_mark_node)
2264     return error_mark_node;
2265
2266   promoted_type = targetm.promoted_type (type);
2267   if (promoted_type)
2268     return convert (promoted_type, exp);
2269
2270   if (INTEGRAL_TYPE_P (type))
2271     return perform_integral_promotions (exp);
2272
2273   return exp;
2274 }
2275 \f
2276 /* Look up COMPONENT in a structure or union TYPE.
2277
2278    If the component name is not found, returns NULL_TREE.  Otherwise,
2279    the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2280    stepping down the chain to the component, which is in the last
2281    TREE_VALUE of the list.  Normally the list is of length one, but if
2282    the component is embedded within (nested) anonymous structures or
2283    unions, the list steps down the chain to the component.  */
2284
2285 static tree
2286 lookup_field (tree type, tree component)
2287 {
2288   tree field;
2289
2290   /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2291      to the field elements.  Use a binary search on this array to quickly
2292      find the element.  Otherwise, do a linear search.  TYPE_LANG_SPECIFIC
2293      will always be set for structures which have many elements.
2294
2295      Duplicate field checking replaces duplicates with NULL_TREE so
2296      TYPE_LANG_SPECIFIC arrays are potentially no longer sorted.  In that
2297      case just iterate using DECL_CHAIN.  */
2298
2299   if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2300       && !seen_error ())
2301     {
2302       int bot, top, half;
2303       tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2304
2305       field = TYPE_FIELDS (type);
2306       bot = 0;
2307       top = TYPE_LANG_SPECIFIC (type)->s->len;
2308       while (top - bot > 1)
2309         {
2310           half = (top - bot + 1) >> 1;
2311           field = field_array[bot+half];
2312
2313           if (DECL_NAME (field) == NULL_TREE)
2314             {
2315               /* Step through all anon unions in linear fashion.  */
2316               while (DECL_NAME (field_array[bot]) == NULL_TREE)
2317                 {
2318                   field = field_array[bot++];
2319                   if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2320                     {
2321                       tree anon = lookup_field (TREE_TYPE (field), component);
2322
2323                       if (anon)
2324                         return tree_cons (NULL_TREE, field, anon);
2325
2326                       /* The Plan 9 compiler permits referring
2327                          directly to an anonymous struct/union field
2328                          using a typedef name.  */
2329                       if (flag_plan9_extensions
2330                           && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2331                           && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2332                               == TYPE_DECL)
2333                           && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2334                               == component))
2335                         break;
2336                     }
2337                 }
2338
2339               /* Entire record is only anon unions.  */
2340               if (bot > top)
2341                 return NULL_TREE;
2342
2343               /* Restart the binary search, with new lower bound.  */
2344               continue;
2345             }
2346
2347           if (DECL_NAME (field) == component)
2348             break;
2349           if (DECL_NAME (field) < component)
2350             bot += half;
2351           else
2352             top = bot + half;
2353         }
2354
2355       if (DECL_NAME (field_array[bot]) == component)
2356         field = field_array[bot];
2357       else if (DECL_NAME (field) != component)
2358         return NULL_TREE;
2359     }
2360   else
2361     {
2362       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2363         {
2364           if (DECL_NAME (field) == NULL_TREE
2365               && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2366             {
2367               tree anon = lookup_field (TREE_TYPE (field), component);
2368
2369               if (anon)
2370                 return tree_cons (NULL_TREE, field, anon);
2371
2372               /* The Plan 9 compiler permits referring directly to an
2373                  anonymous struct/union field using a typedef
2374                  name.  */
2375               if (flag_plan9_extensions
2376                   && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2377                   && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2378                   && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2379                       == component))
2380                 break;
2381             }
2382
2383           if (DECL_NAME (field) == component)
2384             break;
2385         }
2386
2387       if (field == NULL_TREE)
2388         return NULL_TREE;
2389     }
2390
2391   return tree_cons (NULL_TREE, field, NULL_TREE);
2392 }
2393
2394 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES.  */
2395
2396 static void
2397 lookup_field_fuzzy_find_candidates (tree type, tree component,
2398                                     vec<tree> *candidates)
2399 {
2400   tree field;
2401   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2402     {
2403       if (DECL_NAME (field) == NULL_TREE
2404           && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2405         lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2406                                             candidates);
2407
2408       if (DECL_NAME (field))
2409         candidates->safe_push (DECL_NAME (field));
2410     }
2411 }
2412
2413 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2414    rather than returning a TREE_LIST for an exact match.  */
2415
2416 static tree
2417 lookup_field_fuzzy (tree type, tree component)
2418 {
2419   gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2420
2421   /* First, gather a list of candidates.  */
2422   auto_vec <tree> candidates;
2423
2424   lookup_field_fuzzy_find_candidates (type, component,
2425                                       &candidates);
2426
2427   return find_closest_identifier (component, &candidates);
2428 }
2429
2430 /* Support function for build_component_ref's error-handling.
2431
2432    Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2433    struct or union, should we suggest "DATUM->COMPONENT" as a hint?  */
2434
2435 static bool
2436 should_suggest_deref_p (tree datum_type)
2437 {
2438   /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2439      allows "." for ptrs; we could be handling a failed attempt
2440      to access a property.  */
2441   if (c_dialect_objc ())
2442     return false;
2443
2444   /* Only suggest it for pointers...  */
2445   if (TREE_CODE (datum_type) != POINTER_TYPE)
2446     return false;
2447
2448   /* ...to structs/unions.  */
2449   tree underlying_type = TREE_TYPE (datum_type);
2450   enum tree_code code = TREE_CODE (underlying_type);
2451   if (code == RECORD_TYPE || code == UNION_TYPE)
2452     return true;
2453   else
2454     return false;
2455 }
2456
2457 /* Make an expression to refer to the COMPONENT field of structure or
2458    union value DATUM.  COMPONENT is an IDENTIFIER_NODE.  LOC is the
2459    location of the COMPONENT_REF.  COMPONENT_LOC is the location
2460    of COMPONENT.  ARROW_LOC is the location of the first -> operand if
2461    it is from -> operator.  */
2462
2463 tree
2464 build_component_ref (location_t loc, tree datum, tree component,
2465                      location_t component_loc, location_t arrow_loc)
2466 {
2467   tree type = TREE_TYPE (datum);
2468   enum tree_code code = TREE_CODE (type);
2469   tree field = NULL;
2470   tree ref;
2471   bool datum_lvalue = lvalue_p (datum);
2472
2473   if (!objc_is_public (datum, component))
2474     return error_mark_node;
2475
2476   /* Detect Objective-C property syntax object.property.  */
2477   if (c_dialect_objc ()
2478       && (ref = objc_maybe_build_component_ref (datum, component)))
2479     return ref;
2480
2481   /* See if there is a field or component with name COMPONENT.  */
2482
2483   if (code == RECORD_TYPE || code == UNION_TYPE)
2484     {
2485       if (!COMPLETE_TYPE_P (type))
2486         {
2487           c_incomplete_type_error (loc, NULL_TREE, type);
2488           return error_mark_node;
2489         }
2490
2491       field = lookup_field (type, component);
2492
2493       if (!field)
2494         {
2495           tree guessed_id = lookup_field_fuzzy (type, component);
2496           if (guessed_id)
2497             {
2498               /* Attempt to provide a fixit replacement hint, if
2499                  we have a valid range for the component.  */
2500               location_t reported_loc
2501                 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2502               gcc_rich_location rich_loc (reported_loc);
2503               if (component_loc != UNKNOWN_LOCATION)
2504                 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2505               error_at (&rich_loc,
2506                         "%qT has no member named %qE; did you mean %qE?",
2507                         type, component, guessed_id);
2508             }
2509           else
2510             error_at (loc, "%qT has no member named %qE", type, component);
2511           return error_mark_node;
2512         }
2513
2514       /* Accessing elements of atomic structures or unions is undefined
2515          behavior (C11 6.5.2.3#5).  */
2516       if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2517         {
2518           if (code == RECORD_TYPE)
2519             warning_at (loc, 0, "accessing a member %qE of an atomic "
2520                         "structure %qE", component, datum);
2521           else
2522             warning_at (loc, 0, "accessing a member %qE of an atomic "
2523                         "union %qE", component, datum);
2524         }
2525
2526       /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2527          This might be better solved in future the way the C++ front
2528          end does it - by giving the anonymous entities each a
2529          separate name and type, and then have build_component_ref
2530          recursively call itself.  We can't do that here.  */
2531       do
2532         {
2533           tree subdatum = TREE_VALUE (field);
2534           int quals;
2535           tree subtype;
2536           bool use_datum_quals;
2537
2538           if (TREE_TYPE (subdatum) == error_mark_node)
2539             return error_mark_node;
2540
2541           /* If this is an rvalue, it does not have qualifiers in C
2542              standard terms and we must avoid propagating such
2543              qualifiers down to a non-lvalue array that is then
2544              converted to a pointer.  */
2545           use_datum_quals = (datum_lvalue
2546                              || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2547
2548           quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2549           if (use_datum_quals)
2550             quals |= TYPE_QUALS (TREE_TYPE (datum));
2551           subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2552
2553           ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2554                         NULL_TREE);
2555           SET_EXPR_LOCATION (ref, loc);
2556           if (TREE_READONLY (subdatum)
2557               || (use_datum_quals && TREE_READONLY (datum)))
2558             TREE_READONLY (ref) = 1;
2559           if (TREE_THIS_VOLATILE (subdatum)
2560               || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2561             TREE_THIS_VOLATILE (ref) = 1;
2562
2563           if (TREE_UNAVAILABLE (subdatum))
2564             error_unavailable_use (subdatum, NULL_TREE);
2565           else if (TREE_DEPRECATED (subdatum))
2566             warn_deprecated_use (subdatum, NULL_TREE);
2567
2568           datum = ref;
2569
2570           field = TREE_CHAIN (field);
2571         }
2572       while (field);
2573
2574       return ref;
2575     }
2576   else if (should_suggest_deref_p (type))
2577     {
2578       /* Special-case the error message for "ptr.field" for the case
2579          where the user has confused "." vs "->".  */
2580       rich_location richloc (line_table, loc);
2581       if (TREE_CODE (datum) == INDIRECT_REF && arrow_loc != UNKNOWN_LOCATION)
2582         {
2583           richloc.add_fixit_insert_before (arrow_loc, "(*");
2584           richloc.add_fixit_insert_after (arrow_loc, ")");
2585           error_at (&richloc,
2586                     "%qE is a pointer to pointer; did you mean to dereference "
2587                     "it before applying %<->%> to it?",
2588                     TREE_OPERAND (datum, 0));
2589         }
2590       else
2591         {
2592           /* "loc" should be the "." token.  */
2593           richloc.add_fixit_replace ("->");
2594           error_at (&richloc,
2595                     "%qE is a pointer; did you mean to use %<->%>?",
2596                     datum);
2597         }
2598       return error_mark_node;
2599     }
2600   else if (code != ERROR_MARK)
2601     error_at (loc,
2602               "request for member %qE in something not a structure or union",
2603               component);
2604
2605   return error_mark_node;
2606 }
2607 \f
2608 /* Given an expression PTR for a pointer, return an expression
2609    for the value pointed to.
2610    ERRORSTRING is the name of the operator to appear in error messages.
2611
2612    LOC is the location to use for the generated tree.  */
2613
2614 tree
2615 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2616 {
2617   tree pointer = default_conversion (ptr);
2618   tree type = TREE_TYPE (pointer);
2619   tree ref;
2620
2621   if (TREE_CODE (type) == POINTER_TYPE)
2622     {
2623       if (CONVERT_EXPR_P (pointer)
2624           || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2625         {
2626           /* If a warning is issued, mark it to avoid duplicates from
2627              the backend.  This only needs to be done at
2628              warn_strict_aliasing > 2.  */
2629           if (warn_strict_aliasing > 2)
2630             if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2631                                          type, TREE_OPERAND (pointer, 0)))
2632               suppress_warning (pointer, OPT_Wstrict_aliasing_);
2633         }
2634
2635       if (TREE_CODE (pointer) == ADDR_EXPR
2636           && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2637               == TREE_TYPE (type)))
2638         {
2639           ref = TREE_OPERAND (pointer, 0);
2640           protected_set_expr_location (ref, loc);
2641           return ref;
2642         }
2643       else
2644         {
2645           tree t = TREE_TYPE (type);
2646
2647           ref = build1 (INDIRECT_REF, t, pointer);
2648
2649           if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2650             warning_at (loc, 0, "dereferencing %<void *%> pointer");
2651
2652           /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2653              so that we get the proper error message if the result is used
2654              to assign to.  Also, &* is supposed to be a no-op.
2655              And ANSI C seems to specify that the type of the result
2656              should be the const type.  */
2657           /* A de-reference of a pointer to const is not a const.  It is valid
2658              to change it via some other pointer.  */
2659           TREE_READONLY (ref) = TYPE_READONLY (t);
2660           TREE_SIDE_EFFECTS (ref)
2661             = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2662           TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2663           protected_set_expr_location (ref, loc);
2664           return ref;
2665         }
2666     }
2667   else if (TREE_CODE (pointer) != ERROR_MARK)
2668     invalid_indirection_error (loc, type, errstring);
2669
2670   return error_mark_node;
2671 }
2672
2673 /* This handles expressions of the form "a[i]", which denotes
2674    an array reference.
2675
2676    This is logically equivalent in C to *(a+i), but we may do it differently.
2677    If A is a variable or a member, we generate a primitive ARRAY_REF.
2678    This avoids forcing the array out of registers, and can work on
2679    arrays that are not lvalues (for example, members of structures returned
2680    by functions).
2681
2682    For vector types, allow vector[i] but not i[vector], and create
2683    *(((type*)&vectortype) + i) for the expression.
2684
2685    LOC is the location to use for the returned expression.  */
2686
2687 tree
2688 build_array_ref (location_t loc, tree array, tree index)
2689 {
2690   tree ret;
2691   bool swapped = false;
2692   if (TREE_TYPE (array) == error_mark_node
2693       || TREE_TYPE (index) == error_mark_node)
2694     return error_mark_node;
2695
2696   if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2697       && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2698       /* Allow vector[index] but not index[vector].  */
2699       && !gnu_vector_type_p (TREE_TYPE (array)))
2700     {
2701       if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2702           && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2703         {
2704           error_at (loc,
2705             "subscripted value is neither array nor pointer nor vector");
2706
2707           return error_mark_node;
2708         }
2709       std::swap (array, index);
2710       swapped = true;
2711     }
2712
2713   if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2714     {
2715       error_at (loc, "array subscript is not an integer");
2716       return error_mark_node;
2717     }
2718
2719   if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2720     {
2721       error_at (loc, "subscripted value is pointer to function");
2722       return error_mark_node;
2723     }
2724
2725   /* ??? Existing practice has been to warn only when the char
2726      index is syntactically the index, not for char[array].  */
2727   if (!swapped)
2728      warn_array_subscript_with_type_char (loc, index);
2729
2730   /* Apply default promotions *after* noticing character types.  */
2731   index = default_conversion (index);
2732   if (index == error_mark_node)
2733     return error_mark_node;
2734
2735   gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2736
2737   bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2738   bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2739
2740   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2741     {
2742       tree rval, type;
2743
2744       /* An array that is indexed by a non-constant
2745          cannot be stored in a register; we must be able to do
2746          address arithmetic on its address.
2747          Likewise an array of elements of variable size.  */
2748       if (TREE_CODE (index) != INTEGER_CST
2749           || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2750               && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2751         {
2752           if (!c_mark_addressable (array, true))
2753             return error_mark_node;
2754         }
2755       /* An array that is indexed by a constant value which is not within
2756          the array bounds cannot be stored in a register either; because we
2757          would get a crash in store_bit_field/extract_bit_field when trying
2758          to access a non-existent part of the register.  */
2759       if (TREE_CODE (index) == INTEGER_CST
2760           && TYPE_DOMAIN (TREE_TYPE (array))
2761           && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2762         {
2763           if (!c_mark_addressable (array))
2764             return error_mark_node;
2765         }
2766
2767       if ((pedantic || warn_c90_c99_compat)
2768           && ! was_vector)
2769         {
2770           tree foo = array;
2771           while (TREE_CODE (foo) == COMPONENT_REF)
2772             foo = TREE_OPERAND (foo, 0);
2773           if (VAR_P (foo) && C_DECL_REGISTER (foo))
2774             pedwarn (loc, OPT_Wpedantic,
2775                      "ISO C forbids subscripting %<register%> array");
2776           else if (!lvalue_p (foo))
2777             pedwarn_c90 (loc, OPT_Wpedantic,
2778                          "ISO C90 forbids subscripting non-lvalue "
2779                          "array");
2780         }
2781
2782       type = TREE_TYPE (TREE_TYPE (array));
2783       rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2784       /* Array ref is const/volatile if the array elements are
2785          or if the array is.  */
2786       TREE_READONLY (rval)
2787         |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2788             | TREE_READONLY (array));
2789       TREE_SIDE_EFFECTS (rval)
2790         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2791             | TREE_SIDE_EFFECTS (array));
2792       TREE_THIS_VOLATILE (rval)
2793         |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2794             /* This was added by rms on 16 Nov 91.
2795                It fixes  vol struct foo *a;  a->elts[1]
2796                in an inline function.
2797                Hope it doesn't break something else.  */
2798             | TREE_THIS_VOLATILE (array));
2799       ret = require_complete_type (loc, rval);
2800       protected_set_expr_location (ret, loc);
2801       if (non_lvalue)
2802         ret = non_lvalue_loc (loc, ret);
2803       return ret;
2804     }
2805   else
2806     {
2807       tree ar = default_conversion (array);
2808
2809       if (ar == error_mark_node)
2810         return ar;
2811
2812       gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2813       gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2814
2815       ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2816                                                       index, false),
2817                                 RO_ARRAY_INDEXING);
2818       if (non_lvalue)
2819         ret = non_lvalue_loc (loc, ret);
2820       return ret;
2821     }
2822 }
2823 \f
2824 /* Build an external reference to identifier ID.  FUN indicates
2825    whether this will be used for a function call.  LOC is the source
2826    location of the identifier.  This sets *TYPE to the type of the
2827    identifier, which is not the same as the type of the returned value
2828    for CONST_DECLs defined as enum constants.  If the type of the
2829    identifier is not available, *TYPE is set to NULL.  */
2830 tree
2831 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2832 {
2833   tree ref;
2834   tree decl = lookup_name (id);
2835
2836   /* In Objective-C, an instance variable (ivar) may be preferred to
2837      whatever lookup_name() found.  */
2838   decl = objc_lookup_ivar (decl, id);
2839
2840   *type = NULL;
2841   if (decl && decl != error_mark_node)
2842     {
2843       ref = decl;
2844       *type = TREE_TYPE (ref);
2845     }
2846   else if (fun)
2847     /* Implicit function declaration.  */
2848     ref = implicitly_declare (loc, id);
2849   else if (decl == error_mark_node)
2850     /* Don't complain about something that's already been
2851        complained about.  */
2852     return error_mark_node;
2853   else
2854     {
2855       undeclared_variable (loc, id);
2856       return error_mark_node;
2857     }
2858
2859   if (TREE_TYPE (ref) == error_mark_node)
2860     return error_mark_node;
2861
2862   if (TREE_UNAVAILABLE (ref))
2863     error_unavailable_use (ref, NULL_TREE);
2864   else if (TREE_DEPRECATED (ref))
2865     warn_deprecated_use (ref, NULL_TREE);
2866
2867   /* Recursive call does not count as usage.  */
2868   if (ref != current_function_decl)
2869     {
2870       TREE_USED (ref) = 1;
2871     }
2872
2873   if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2874     {
2875       if (!in_sizeof && !in_typeof)
2876         C_DECL_USED (ref) = 1;
2877       else if (DECL_INITIAL (ref) == NULL_TREE
2878                && DECL_EXTERNAL (ref)
2879                && !TREE_PUBLIC (ref))
2880         record_maybe_used_decl (ref);
2881     }
2882
2883   if (TREE_CODE (ref) == CONST_DECL)
2884     {
2885       used_types_insert (TREE_TYPE (ref));
2886
2887       if (warn_cxx_compat
2888           && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2889           && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2890         {
2891           warning_at (loc, OPT_Wc___compat,
2892                       ("enum constant defined in struct or union "
2893                        "is not visible in C++"));
2894           inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2895         }
2896
2897       ref = DECL_INITIAL (ref);
2898       TREE_CONSTANT (ref) = 1;
2899     }
2900   else if (current_function_decl != NULL_TREE
2901            && !DECL_FILE_SCOPE_P (current_function_decl)
2902            && (VAR_OR_FUNCTION_DECL_P (ref)
2903                || TREE_CODE (ref) == PARM_DECL))
2904     {
2905       tree context = decl_function_context (ref);
2906
2907       if (context != NULL_TREE && context != current_function_decl)
2908         DECL_NONLOCAL (ref) = 1;
2909     }
2910   /* C99 6.7.4p3: An inline definition of a function with external
2911      linkage ... shall not contain a reference to an identifier with
2912      internal linkage.  */
2913   else if (current_function_decl != NULL_TREE
2914            && DECL_DECLARED_INLINE_P (current_function_decl)
2915            && DECL_EXTERNAL (current_function_decl)
2916            && VAR_OR_FUNCTION_DECL_P (ref)
2917            && (!VAR_P (ref) || TREE_STATIC (ref))
2918            && ! TREE_PUBLIC (ref)
2919            && DECL_CONTEXT (ref) != current_function_decl)
2920     record_inline_static (loc, current_function_decl, ref,
2921                           csi_internal);
2922
2923   return ref;
2924 }
2925
2926 /* Record details of decls possibly used inside sizeof or typeof.  */
2927 struct maybe_used_decl
2928 {
2929   /* The decl.  */
2930   tree decl;
2931   /* The level seen at (in_sizeof + in_typeof).  */
2932   int level;
2933   /* The next one at this level or above, or NULL.  */
2934   struct maybe_used_decl *next;
2935 };
2936
2937 static struct maybe_used_decl *maybe_used_decls;
2938
2939 /* Record that DECL, an undefined static function reference seen
2940    inside sizeof or typeof, might be used if the operand of sizeof is
2941    a VLA type or the operand of typeof is a variably modified
2942    type.  */
2943
2944 static void
2945 record_maybe_used_decl (tree decl)
2946 {
2947   struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2948   t->decl = decl;
2949   t->level = in_sizeof + in_typeof;
2950   t->next = maybe_used_decls;
2951   maybe_used_decls = t;
2952 }
2953
2954 /* Pop the stack of decls possibly used inside sizeof or typeof.  If
2955    USED is false, just discard them.  If it is true, mark them used
2956    (if no longer inside sizeof or typeof) or move them to the next
2957    level up (if still inside sizeof or typeof).  */
2958
2959 void
2960 pop_maybe_used (bool used)
2961 {
2962   struct maybe_used_decl *p = maybe_used_decls;
2963   int cur_level = in_sizeof + in_typeof;
2964   while (p && p->level > cur_level)
2965     {
2966       if (used)
2967         {
2968           if (cur_level == 0)
2969             C_DECL_USED (p->decl) = 1;
2970           else
2971             p->level = cur_level;
2972         }
2973       p = p->next;
2974     }
2975   if (!used || cur_level == 0)
2976     maybe_used_decls = p;
2977 }
2978
2979 /* Return the result of sizeof applied to EXPR.  */
2980
2981 struct c_expr
2982 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2983 {
2984   struct c_expr ret;
2985   if (expr.value == error_mark_node)
2986     {
2987       ret.value = error_mark_node;
2988       ret.original_code = ERROR_MARK;
2989       ret.original_type = NULL;
2990       pop_maybe_used (false);
2991     }
2992   else
2993     {
2994       bool expr_const_operands = true;
2995
2996       if (TREE_CODE (expr.value) == PARM_DECL
2997           && C_ARRAY_PARAMETER (expr.value))
2998         {
2999           auto_diagnostic_group d;
3000           if (warning_at (loc, OPT_Wsizeof_array_argument,
3001                           "%<sizeof%> on array function parameter %qE will "
3002                           "return size of %qT", expr.value,
3003                           TREE_TYPE (expr.value)))
3004             inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
3005         }
3006       tree folded_expr = c_fully_fold (expr.value, require_constant_value,
3007                                        &expr_const_operands);
3008       ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
3009       c_last_sizeof_arg = expr.value;
3010       c_last_sizeof_loc = loc;
3011       ret.original_code = SIZEOF_EXPR;
3012       ret.original_type = NULL;
3013       if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
3014         {
3015           /* sizeof is evaluated when given a vla (C99 6.5.3.4p2).  */
3016           ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3017                               folded_expr, ret.value);
3018           C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
3019           SET_EXPR_LOCATION (ret.value, loc);
3020         }
3021       pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
3022     }
3023   return ret;
3024 }
3025
3026 /* Return the result of sizeof applied to T, a structure for the type
3027    name passed to sizeof (rather than the type itself).  LOC is the
3028    location of the original expression.  */
3029
3030 struct c_expr
3031 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3032 {
3033   tree type;
3034   struct c_expr ret;
3035   tree type_expr = NULL_TREE;
3036   bool type_expr_const = true;
3037   type = groktypename (t, &type_expr, &type_expr_const);
3038   ret.value = c_sizeof (loc, type);
3039   c_last_sizeof_arg = type;
3040   c_last_sizeof_loc = loc;
3041   ret.original_code = SIZEOF_EXPR;
3042   ret.original_type = NULL;
3043   if (type == error_mark_node)
3044     {
3045       ret.value = error_mark_node;
3046       ret.original_code = ERROR_MARK;
3047     }
3048   else
3049   if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3050       && C_TYPE_VARIABLE_SIZE (type))
3051     {
3052       /* If the type is a [*] array, it is a VLA but is represented as
3053          having a size of zero.  In such a case we must ensure that
3054          the result of sizeof does not get folded to a constant by
3055          c_fully_fold, because if the size is evaluated the result is
3056          not constant and so constraints on zero or negative size
3057          arrays must not be applied when this sizeof call is inside
3058          another array declarator.  */
3059       if (!type_expr)
3060         type_expr = integer_zero_node;
3061       ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3062                           type_expr, ret.value);
3063       C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3064     }
3065   pop_maybe_used (type != error_mark_node
3066                   ? C_TYPE_VARIABLE_SIZE (type) : false);
3067   return ret;
3068 }
3069
3070 /* Build a function call to function FUNCTION with parameters PARAMS.
3071    The function call is at LOC.
3072    PARAMS is a list--a chain of TREE_LIST nodes--in which the
3073    TREE_VALUE of each node is a parameter-expression.
3074    FUNCTION's data type may be a function type or a pointer-to-function.  */
3075
3076 tree
3077 build_function_call (location_t loc, tree function, tree params)
3078 {
3079   vec<tree, va_gc> *v;
3080   tree ret;
3081
3082   vec_alloc (v, list_length (params));
3083   for (; params; params = TREE_CHAIN (params))
3084     v->quick_push (TREE_VALUE (params));
3085   ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3086   vec_free (v);
3087   return ret;
3088 }
3089
3090 /* Give a note about the location of the declaration of DECL.  */
3091
3092 static void
3093 inform_declaration (tree decl)
3094 {
3095   if (decl && (TREE_CODE (decl) != FUNCTION_DECL
3096                || !DECL_IS_UNDECLARED_BUILTIN (decl)))
3097     inform (DECL_SOURCE_LOCATION (decl), "declared here");
3098 }
3099
3100 /* Build a function call to function FUNCTION with parameters PARAMS.
3101    If FUNCTION is the result of resolving an overloaded target built-in,
3102    ORIG_FUNDECL is the original function decl, otherwise it is null.
3103    ORIGTYPES, if not NULL, is a vector of types; each element is
3104    either NULL or the original type of the corresponding element in
3105    PARAMS.  The original type may differ from TREE_TYPE of the
3106    parameter for enums.  FUNCTION's data type may be a function type
3107    or pointer-to-function.  This function changes the elements of
3108    PARAMS.  */
3109
3110 tree
3111 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3112                          tree function, vec<tree, va_gc> *params,
3113                          vec<tree, va_gc> *origtypes, tree orig_fundecl)
3114 {
3115   tree fntype, fundecl = NULL_TREE;
3116   tree name = NULL_TREE, result;
3117   tree tem;
3118   int nargs;
3119   tree *argarray;
3120
3121
3122   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
3123   STRIP_TYPE_NOPS (function);
3124
3125   /* Convert anything with function type to a pointer-to-function.  */
3126   if (TREE_CODE (function) == FUNCTION_DECL)
3127     {
3128       name = DECL_NAME (function);
3129
3130       if (flag_tm)
3131         tm_malloc_replacement (function);
3132       fundecl = function;
3133       if (!orig_fundecl)
3134         orig_fundecl = fundecl;
3135       /* Atomic functions have type checking/casting already done.  They are 
3136          often rewritten and don't match the original parameter list.  */
3137       if (name && startswith (IDENTIFIER_POINTER (name), "__atomic_"))
3138         origtypes = NULL;
3139     }
3140   if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3141     function = function_to_pointer_conversion (loc, function);
3142
3143   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3144      expressions, like those used for ObjC messenger dispatches.  */
3145   if (params && !params->is_empty ())
3146     function = objc_rewrite_function_call (function, (*params)[0]);
3147
3148   function = c_fully_fold (function, false, NULL);
3149
3150   fntype = TREE_TYPE (function);
3151
3152   if (TREE_CODE (fntype) == ERROR_MARK)
3153     return error_mark_node;
3154
3155   if (!(TREE_CODE (fntype) == POINTER_TYPE
3156         && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3157     {
3158       if (!flag_diagnostics_show_caret && !STATEMENT_CLASS_P (function))
3159         error_at (loc,
3160                   "called object %qE is not a function or function pointer",
3161                   function);
3162       else if (DECL_P (function))
3163         {
3164           error_at (loc,
3165                     "called object %qD is not a function or function pointer",
3166                     function);
3167           inform_declaration (function);
3168         }
3169       else
3170         error_at (loc,
3171                   "called object is not a function or function pointer");
3172       return error_mark_node;
3173     }
3174
3175   if (fundecl && TREE_THIS_VOLATILE (fundecl))
3176     current_function_returns_abnormally = 1;
3177
3178   /* fntype now gets the type of function pointed to.  */
3179   fntype = TREE_TYPE (fntype);
3180
3181   /* Convert the parameters to the types declared in the
3182      function prototype, or apply default promotions.  */
3183
3184   nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3185                              origtypes, function, fundecl);
3186   if (nargs < 0)
3187     return error_mark_node;
3188
3189   /* Check that the function is called through a compatible prototype.
3190      If it is not, warn.  */
3191   if (CONVERT_EXPR_P (function)
3192       && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3193       && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3194       && !comptypes (fntype, TREE_TYPE (tem)))
3195     {
3196       tree return_type = TREE_TYPE (fntype);
3197
3198       /* This situation leads to run-time undefined behavior.  We can't,
3199          therefore, simply error unless we can prove that all possible
3200          executions of the program must execute the code.  */
3201       warning_at (loc, 0, "function called through a non-compatible type");
3202
3203       if (VOID_TYPE_P (return_type)
3204           && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3205         pedwarn (loc, 0,
3206                  "function with qualified void return type called");
3207      }
3208
3209   argarray = vec_safe_address (params);
3210
3211   /* Check that arguments to builtin functions match the expectations.  */
3212   if (fundecl
3213       && fndecl_built_in_p (fundecl)
3214       && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3215                                             orig_fundecl, nargs, argarray))
3216     return error_mark_node;
3217
3218   /* Check that the arguments to the function are valid.  */
3219   bool warned_p = check_function_arguments (loc, fundecl, fntype,
3220                                             nargs, argarray, &arg_loc);
3221
3222   if (name != NULL_TREE
3223       && startswith (IDENTIFIER_POINTER (name), "__builtin_"))
3224     {
3225       if (require_constant_value)
3226         result
3227           = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3228                                                    function, nargs, argarray);
3229       else
3230         result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3231                                             function, nargs, argarray);
3232       if (TREE_CODE (result) == NOP_EXPR
3233           && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3234         STRIP_TYPE_NOPS (result);
3235     }
3236   else
3237     result = build_call_array_loc (loc, TREE_TYPE (fntype),
3238                                    function, nargs, argarray);
3239   /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3240      later.  */
3241   if (warned_p && TREE_CODE (result) == CALL_EXPR)
3242     suppress_warning (result, OPT_Wnonnull);
3243
3244   /* In this improbable scenario, a nested function returns a VM type.
3245      Create a TARGET_EXPR so that the call always has a LHS, much as
3246      what the C++ FE does for functions returning non-PODs.  */
3247   if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3248     {
3249       tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3250       result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3251                        NULL_TREE, NULL_TREE);
3252     }
3253
3254   if (VOID_TYPE_P (TREE_TYPE (result)))
3255     {
3256       if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3257         pedwarn (loc, 0,
3258                  "function with qualified void return type called");
3259       return result;
3260     }
3261   return require_complete_type (loc, result);
3262 }
3263
3264 /* Like build_function_call_vec, but call also resolve_overloaded_builtin.  */
3265
3266 tree
3267 c_build_function_call_vec (location_t loc, const vec<location_t> &arg_loc,
3268                            tree function, vec<tree, va_gc> *params,
3269                            vec<tree, va_gc> *origtypes)
3270 {
3271   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
3272   STRIP_TYPE_NOPS (function);
3273
3274   /* Convert anything with function type to a pointer-to-function.  */
3275   if (TREE_CODE (function) == FUNCTION_DECL)
3276     {
3277       /* Implement type-directed function overloading for builtins.
3278          resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3279          handle all the type checking.  The result is a complete expression
3280          that implements this function call.  */
3281       tree tem = resolve_overloaded_builtin (loc, function, params);
3282       if (tem)
3283         return tem;
3284     }
3285   return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3286 }
3287 \f
3288 /* Helper for convert_arguments called to convert the VALue of argument
3289    number ARGNUM from ORIGTYPE to the corresponding parameter number
3290    PARMNUM and TYPE.
3291    PLOC is the location where the conversion is being performed.
3292    FUNCTION and FUNDECL are the same as in convert_arguments.
3293    VALTYPE is the original type of VAL before the conversion and,
3294    for EXCESS_PRECISION_EXPR, the operand of the expression.
3295    NPC is true if VAL represents the null pointer constant (VAL itself
3296    will have been folded to an integer constant).
3297    RNAME is the same as FUNCTION except in Objective C when it's
3298    the function selector.
3299    EXCESS_PRECISION is true when VAL was originally represented
3300    as EXCESS_PRECISION_EXPR.
3301    WARNOPT is the same as in convert_for_assignment.  */
3302
3303 static tree
3304 convert_argument (location_t ploc, tree function, tree fundecl,
3305                   tree type, tree origtype, tree val, tree valtype,
3306                   bool npc, tree rname, int parmnum, int argnum,
3307                   bool excess_precision, int warnopt)
3308 {
3309   /* Formal parm type is specified by a function prototype.  */
3310
3311   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3312     {
3313       error_at (ploc, "type of formal parameter %d is incomplete",
3314                 parmnum + 1);
3315       return val;
3316     }
3317
3318   /* Optionally warn about conversions that differ from the default
3319      conversions.  */
3320   if (warn_traditional_conversion || warn_traditional)
3321     {
3322       unsigned int formal_prec = TYPE_PRECISION (type);
3323
3324       if (INTEGRAL_TYPE_P (type)
3325           && TREE_CODE (valtype) == REAL_TYPE)
3326         warning_at (ploc, OPT_Wtraditional_conversion,
3327                     "passing argument %d of %qE as integer rather "
3328                     "than floating due to prototype",
3329                     argnum, rname);
3330       if (INTEGRAL_TYPE_P (type)
3331           && TREE_CODE (valtype) == COMPLEX_TYPE)
3332         warning_at (ploc, OPT_Wtraditional_conversion,
3333                     "passing argument %d of %qE as integer rather "
3334                     "than complex due to prototype",
3335                     argnum, rname);
3336       else if (TREE_CODE (type) == COMPLEX_TYPE
3337                && TREE_CODE (valtype) == REAL_TYPE)
3338         warning_at (ploc, OPT_Wtraditional_conversion,
3339                     "passing argument %d of %qE as complex rather "
3340                     "than floating due to prototype",
3341                     argnum, rname);
3342       else if (TREE_CODE (type) == REAL_TYPE
3343                && INTEGRAL_TYPE_P (valtype))
3344         warning_at (ploc, OPT_Wtraditional_conversion,
3345                     "passing argument %d of %qE as floating rather "
3346                     "than integer due to prototype",
3347                     argnum, rname);
3348       else if (TREE_CODE (type) == COMPLEX_TYPE
3349                && INTEGRAL_TYPE_P (valtype))
3350         warning_at (ploc, OPT_Wtraditional_conversion,
3351                     "passing argument %d of %qE as complex rather "
3352                     "than integer due to prototype",
3353                     argnum, rname);
3354       else if (TREE_CODE (type) == REAL_TYPE
3355                && TREE_CODE (valtype) == COMPLEX_TYPE)
3356         warning_at (ploc, OPT_Wtraditional_conversion,
3357                     "passing argument %d of %qE as floating rather "
3358                     "than complex due to prototype",
3359                     argnum, rname);
3360       /* ??? At some point, messages should be written about
3361          conversions between complex types, but that's too messy
3362          to do now.  */
3363       else if (TREE_CODE (type) == REAL_TYPE
3364                && TREE_CODE (valtype) == REAL_TYPE)
3365         {
3366           /* Warn if any argument is passed as `float',
3367              since without a prototype it would be `double'.  */
3368           if (formal_prec == TYPE_PRECISION (float_type_node)
3369               && type != dfloat32_type_node)
3370             warning_at (ploc, 0,
3371                         "passing argument %d of %qE as %<float%> "
3372                         "rather than %<double%> due to prototype",
3373                         argnum, rname);
3374
3375           /* Warn if mismatch between argument and prototype
3376              for decimal float types.  Warn of conversions with
3377              binary float types and of precision narrowing due to
3378              prototype.  */
3379           else if (type != valtype
3380                    && (type == dfloat32_type_node
3381                        || type == dfloat64_type_node
3382                        || type == dfloat128_type_node
3383                        || valtype == dfloat32_type_node
3384                        || valtype == dfloat64_type_node
3385                        || valtype == dfloat128_type_node)
3386                    && (formal_prec
3387                        <= TYPE_PRECISION (valtype)
3388                        || (type == dfloat128_type_node
3389                            && (valtype
3390                                != dfloat64_type_node
3391                                && (valtype
3392                                    != dfloat32_type_node)))
3393                        || (type == dfloat64_type_node
3394                            && (valtype
3395                                != dfloat32_type_node))))
3396             warning_at (ploc, 0,
3397                         "passing argument %d of %qE as %qT "
3398                         "rather than %qT due to prototype",
3399                         argnum, rname, type, valtype);
3400
3401         }
3402       /* Detect integer changing in width or signedness.
3403          These warnings are only activated with
3404          -Wtraditional-conversion, not with -Wtraditional.  */
3405       else if (warn_traditional_conversion
3406                && INTEGRAL_TYPE_P (type)
3407                && INTEGRAL_TYPE_P (valtype))
3408         {
3409           tree would_have_been = default_conversion (val);
3410           tree type1 = TREE_TYPE (would_have_been);
3411
3412           if (val == error_mark_node)
3413             /* VAL could have been of incomplete type.  */;
3414           else if (TREE_CODE (type) == ENUMERAL_TYPE
3415                    && (TYPE_MAIN_VARIANT (type)
3416                        == TYPE_MAIN_VARIANT (valtype)))
3417             /* No warning if function asks for enum
3418                and the actual arg is that enum type.  */
3419             ;
3420           else if (formal_prec != TYPE_PRECISION (type1))
3421             warning_at (ploc, OPT_Wtraditional_conversion,
3422                         "passing argument %d of %qE "
3423                         "with different width due to prototype",
3424                         argnum, rname);
3425           else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3426             ;
3427           /* Don't complain if the formal parameter type
3428              is an enum, because we can't tell now whether
3429              the value was an enum--even the same enum.  */
3430           else if (TREE_CODE (type) == ENUMERAL_TYPE)
3431             ;
3432           else if (TREE_CODE (val) == INTEGER_CST
3433                    && int_fits_type_p (val, type))
3434             /* Change in signedness doesn't matter
3435                if a constant value is unaffected.  */
3436             ;
3437           /* If the value is extended from a narrower
3438              unsigned type, it doesn't matter whether we
3439              pass it as signed or unsigned; the value
3440              certainly is the same either way.  */
3441           else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3442                    && TYPE_UNSIGNED (valtype))
3443             ;
3444           else if (TYPE_UNSIGNED (type))
3445             warning_at (ploc, OPT_Wtraditional_conversion,
3446                         "passing argument %d of %qE "
3447                         "as unsigned due to prototype",
3448                         argnum, rname);
3449           else
3450             warning_at (ploc, OPT_Wtraditional_conversion,
3451                         "passing argument %d of %qE "
3452                         "as signed due to prototype",
3453                         argnum, rname);
3454         }
3455     }
3456
3457   /* Possibly restore an EXCESS_PRECISION_EXPR for the
3458      sake of better warnings from convert_and_check.  */
3459   if (excess_precision)
3460     val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3461
3462   tree parmval = convert_for_assignment (ploc, ploc, type,
3463                                          val, origtype, ic_argpass,
3464                                          npc, fundecl, function,
3465                                          parmnum + 1, warnopt);
3466
3467   if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3468       && INTEGRAL_TYPE_P (type)
3469       && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3470     parmval = default_conversion (parmval);
3471
3472   return parmval;
3473 }
3474
3475 /* Convert the argument expressions in the vector VALUES
3476    to the types in the list TYPELIST.
3477
3478    If TYPELIST is exhausted, or when an element has NULL as its type,
3479    perform the default conversions.
3480
3481    ORIGTYPES is the original types of the expressions in VALUES.  This
3482    holds the type of enum values which have been converted to integral
3483    types.  It may be NULL.
3484
3485    FUNCTION is a tree for the called function.  It is used only for
3486    error messages, where it is formatted with %qE.
3487
3488    This is also where warnings about wrong number of args are generated.
3489
3490    ARG_LOC are locations of function arguments (if any).
3491
3492    Returns the actual number of arguments processed (which may be less
3493    than the length of VALUES in some error situations), or -1 on
3494    failure.  */
3495
3496 static int
3497 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3498                    vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3499                    tree function, tree fundecl)
3500 {
3501   unsigned int parmnum;
3502   bool error_args = false;
3503   const bool type_generic = fundecl
3504     && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3505   bool type_generic_remove_excess_precision = false;
3506   bool type_generic_overflow_p = false;
3507   tree selector;
3508
3509   /* Change pointer to function to the function itself for
3510      diagnostics.  */
3511   if (TREE_CODE (function) == ADDR_EXPR
3512       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3513     function = TREE_OPERAND (function, 0);
3514
3515   /* Handle an ObjC selector specially for diagnostics.  */
3516   selector = objc_message_selector ();
3517
3518   /* For a call to a built-in function declared without a prototype,
3519      set to the built-in function's argument list.  */
3520   tree builtin_typelist = NULL_TREE;
3521
3522   /* For type-generic built-in functions, determine whether excess
3523      precision should be removed (classification) or not
3524      (comparison).  */
3525   if (fundecl
3526       && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3527     {
3528       built_in_function code = DECL_FUNCTION_CODE (fundecl);
3529       if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3530         {
3531           /* For a call to a built-in function declared without a prototype
3532              use the types of the parameters of the internal built-in to
3533              match those of the arguments to.  */
3534           if (tree bdecl = builtin_decl_explicit (code))
3535             builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3536         }
3537
3538       /* For type-generic built-in functions, determine whether excess
3539          precision should be removed (classification) or not
3540          (comparison).  */
3541       if (type_generic)
3542         switch (code)
3543           {
3544           case BUILT_IN_ISFINITE:
3545           case BUILT_IN_ISINF:
3546           case BUILT_IN_ISINF_SIGN:
3547           case BUILT_IN_ISNAN:
3548           case BUILT_IN_ISNORMAL:
3549           case BUILT_IN_FPCLASSIFY:
3550             type_generic_remove_excess_precision = true;
3551             break;
3552
3553           case BUILT_IN_ADD_OVERFLOW_P:
3554           case BUILT_IN_SUB_OVERFLOW_P:
3555           case BUILT_IN_MUL_OVERFLOW_P:
3556             /* The last argument of these type-generic builtins
3557                should not be promoted.  */
3558             type_generic_overflow_p = true;
3559             break;
3560
3561           default:
3562             break;
3563           }
3564     }
3565
3566   /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3567      individual converted arguments.  */
3568
3569   tree typetail, builtin_typetail, val;
3570   for (typetail = typelist,
3571          builtin_typetail = builtin_typelist,
3572          parmnum = 0;
3573        values && values->iterate (parmnum, &val);
3574        ++parmnum)
3575     {
3576       /* The type of the function parameter (if it was declared with one).  */
3577       tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3578       /* The type of the built-in function parameter (if the function
3579          is a built-in).  Used to detect type incompatibilities in
3580          calls to built-ins declared without a prototype.  */
3581       tree builtin_type = (builtin_typetail
3582                            ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3583       /* The original type of the argument being passed to the function.  */
3584       tree valtype = TREE_TYPE (val);
3585       /* The called function (or function selector in Objective C).  */
3586       tree rname = function;
3587       int argnum = parmnum + 1;
3588       const char *invalid_func_diag;
3589       /* Set for EXCESS_PRECISION_EXPR arguments.  */
3590       bool excess_precision = false;
3591       /* The value of the argument after conversion to the type
3592          of the function parameter it is passed to.  */
3593       tree parmval;
3594       /* Some __atomic_* builtins have additional hidden argument at
3595          position 0.  */
3596       location_t ploc
3597         = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3598           ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3599           : input_location;
3600
3601       if (type == void_type_node)
3602         {
3603           if (selector)
3604             error_at (loc, "too many arguments to method %qE", selector);
3605           else
3606             error_at (loc, "too many arguments to function %qE", function);
3607           inform_declaration (fundecl);
3608           return error_args ? -1 : (int) parmnum;
3609         }
3610
3611       if (builtin_type == void_type_node)
3612         {
3613           if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3614                           "too many arguments to built-in function %qE "
3615                           "expecting %d", function, parmnum))
3616             inform_declaration (fundecl);
3617           builtin_typetail = NULL_TREE;
3618         }
3619
3620       if (selector && argnum > 2)
3621         {
3622           rname = selector;
3623           argnum -= 2;
3624         }
3625
3626       /* Determine if VAL is a null pointer constant before folding it.  */
3627       bool npc = null_pointer_constant_p (val);
3628
3629       /* If there is excess precision and a prototype, convert once to
3630          the required type rather than converting via the semantic
3631          type.  Likewise without a prototype a float value represented
3632          as long double should be converted once to double.  But for
3633          type-generic classification functions excess precision must
3634          be removed here.  */
3635       if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3636           && (type || !type_generic || !type_generic_remove_excess_precision))
3637         {
3638           val = TREE_OPERAND (val, 0);
3639           excess_precision = true;
3640         }
3641       val = c_fully_fold (val, false, NULL);
3642       STRIP_TYPE_NOPS (val);
3643
3644       val = require_complete_type (ploc, val);
3645
3646       /* Some floating-point arguments must be promoted to double when
3647          no type is specified by a prototype.  This applies to
3648          arguments of type float, and to architecture-specific types
3649          (ARM __fp16), but not to _FloatN or _FloatNx types.  */
3650       bool promote_float_arg = false;
3651       if (type == NULL_TREE
3652           && TREE_CODE (valtype) == REAL_TYPE
3653           && (TYPE_PRECISION (valtype)
3654               <= TYPE_PRECISION (double_type_node))
3655           && TYPE_MAIN_VARIANT (valtype) != double_type_node
3656           && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3657           && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3658         {
3659           /* Promote this argument, unless it has a _FloatN or
3660              _FloatNx type.  */
3661           promote_float_arg = true;
3662           for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3663             if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3664               {
3665                 promote_float_arg = false;
3666                 break;
3667               }
3668         }
3669
3670       if (type != NULL_TREE)
3671         {
3672           tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3673           parmval = convert_argument (ploc, function, fundecl, type, origtype,
3674                                       val, valtype, npc, rname, parmnum, argnum,
3675                                       excess_precision, 0);
3676         }
3677       else if (promote_float_arg)
3678         {
3679           if (type_generic)
3680             parmval = val;
3681           else
3682             {
3683               /* Convert `float' to `double'.  */
3684               if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3685                 warning_at (ploc, OPT_Wdouble_promotion,
3686                             "implicit conversion from %qT to %qT when passing "
3687                             "argument to function",
3688                             valtype, double_type_node);
3689               parmval = convert (double_type_node, val);
3690             }
3691         }
3692       else if ((excess_precision && !type_generic)
3693                || (type_generic_overflow_p && parmnum == 2))
3694         /* A "double" argument with excess precision being passed
3695            without a prototype or in variable arguments.
3696            The last argument of __builtin_*_overflow_p should not be
3697            promoted.  */
3698         parmval = convert (valtype, val);
3699       else if ((invalid_func_diag =
3700                 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3701         {
3702           error (invalid_func_diag);
3703           return -1;
3704         }
3705       else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3706         {
3707           return -1;
3708         }
3709       else
3710         /* Convert `short' and `char' to full-size `int'.  */
3711         parmval = default_conversion (val);
3712
3713       (*values)[parmnum] = parmval;
3714       if (parmval == error_mark_node)
3715         error_args = true;
3716
3717       if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3718         {
3719           /* For a call to a built-in function declared without a prototype,
3720              perform the conversions from the argument to the expected type
3721              but issue warnings rather than errors for any mismatches.
3722              Ignore the converted argument and use the PARMVAL obtained
3723              above by applying default conversions instead.  */
3724           tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3725           convert_argument (ploc, function, fundecl, builtin_type, origtype,
3726                             val, valtype, npc, rname, parmnum, argnum,
3727                             excess_precision,
3728                             OPT_Wbuiltin_declaration_mismatch);
3729         }
3730
3731       if (typetail)
3732         typetail = TREE_CHAIN (typetail);
3733
3734       if (builtin_typetail)
3735         builtin_typetail = TREE_CHAIN (builtin_typetail);
3736     }
3737
3738   gcc_assert (parmnum == vec_safe_length (values));
3739
3740   if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3741     {
3742       error_at (loc, "too few arguments to function %qE", function);
3743       inform_declaration (fundecl);
3744       return -1;
3745     }
3746
3747   if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3748     {
3749       unsigned nargs = parmnum;
3750       for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3751         ++nargs;
3752
3753       if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3754                       "too few arguments to built-in function %qE "
3755                       "expecting %u", function, nargs - 1))
3756         inform_declaration (fundecl);
3757     }
3758
3759   return error_args ? -1 : (int) parmnum;
3760 }
3761 \f
3762 /* This is the entry point used by the parser to build unary operators
3763    in the input.  CODE, a tree_code, specifies the unary operator, and
3764    ARG is the operand.  For unary plus, the C parser currently uses
3765    CONVERT_EXPR for code.
3766
3767    LOC is the location to use for the tree generated.
3768 */
3769
3770 struct c_expr
3771 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3772 {
3773   struct c_expr result;
3774
3775   result.original_code = code;
3776   result.original_type = NULL;
3777
3778   if (reject_gcc_builtin (arg.value))
3779     {
3780       result.value = error_mark_node;
3781     }
3782   else
3783     {
3784       result.value = build_unary_op (loc, code, arg.value, false);
3785
3786       if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3787         overflow_warning (loc, result.value, arg.value);
3788     }
3789
3790   /* We are typically called when parsing a prefix token at LOC acting on
3791      ARG.  Reflect this by updating the source range of the result to
3792      start at LOC and end at the end of ARG.  */
3793   set_c_expr_source_range (&result,
3794                            loc, arg.get_finish ());
3795
3796   return result;
3797 }
3798
3799 /* Returns true if TYPE is a character type, *not* including wchar_t.  */
3800
3801 bool
3802 char_type_p (tree type)
3803 {
3804   return (type == char_type_node
3805           || type == unsigned_char_type_node
3806           || type == signed_char_type_node
3807           || type == char16_type_node
3808           || type == char32_type_node);
3809 }
3810
3811 /* This is the entry point used by the parser to build binary operators
3812    in the input.  CODE, a tree_code, specifies the binary operator, and
3813    ARG1 and ARG2 are the operands.  In addition to constructing the
3814    expression, we check for operands that were written with other binary
3815    operators in a way that is likely to confuse the user.
3816
3817    LOCATION is the location of the binary operator.  */
3818
3819 struct c_expr
3820 parser_build_binary_op (location_t location, enum tree_code code,
3821                         struct c_expr arg1, struct c_expr arg2)
3822 {
3823   struct c_expr result;
3824
3825   enum tree_code code1 = arg1.original_code;
3826   enum tree_code code2 = arg2.original_code;
3827   tree type1 = (arg1.original_type
3828                 ? arg1.original_type
3829                 : TREE_TYPE (arg1.value));
3830   tree type2 = (arg2.original_type
3831                 ? arg2.original_type
3832                 : TREE_TYPE (arg2.value));
3833
3834   result.value = build_binary_op (location, code,
3835                                   arg1.value, arg2.value, true);
3836   result.original_code = code;
3837   result.original_type = NULL;
3838
3839   if (TREE_CODE (result.value) == ERROR_MARK)
3840     {
3841       set_c_expr_source_range (&result,
3842                                arg1.get_start (),
3843                                arg2.get_finish ());
3844       return result;
3845     }
3846
3847   if (location != UNKNOWN_LOCATION)
3848     protected_set_expr_location (result.value, location);
3849
3850   set_c_expr_source_range (&result,
3851                            arg1.get_start (),
3852                            arg2.get_finish ());
3853
3854   /* Check for cases such as x+y<<z which users are likely
3855      to misinterpret.  */
3856   if (warn_parentheses)
3857     warn_about_parentheses (location, code, code1, arg1.value, code2,
3858                             arg2.value);
3859
3860   if (warn_logical_op)
3861     warn_logical_operator (location, code, TREE_TYPE (result.value),
3862                            code1, arg1.value, code2, arg2.value);
3863
3864   if (warn_tautological_compare)
3865     {
3866       tree lhs = arg1.value;
3867       tree rhs = arg2.value;
3868       if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3869         {
3870           if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3871               && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3872             lhs = NULL_TREE;
3873           else
3874             lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3875         }
3876       if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3877         {
3878           if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3879               && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3880             rhs = NULL_TREE;
3881           else
3882             rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3883         }
3884       if (lhs != NULL_TREE && rhs != NULL_TREE)
3885         warn_tautological_cmp (location, code, lhs, rhs);
3886     }
3887
3888   if (warn_logical_not_paren
3889       && TREE_CODE_CLASS (code) == tcc_comparison
3890       && code1 == TRUTH_NOT_EXPR
3891       && code2 != TRUTH_NOT_EXPR
3892       /* Avoid warning for !!x == y.  */
3893       && (TREE_CODE (arg1.value) != NE_EXPR
3894           || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3895     {
3896       /* Avoid warning for !b == y where b has _Bool type.  */
3897       tree t = integer_zero_node;
3898       if (TREE_CODE (arg1.value) == EQ_EXPR
3899           && integer_zerop (TREE_OPERAND (arg1.value, 1))
3900           && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3901         {
3902           t = TREE_OPERAND (arg1.value, 0);
3903           do
3904             {
3905               if (TREE_TYPE (t) != integer_type_node)
3906                 break;
3907               if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3908                 t = C_MAYBE_CONST_EXPR_EXPR (t);
3909               else if (CONVERT_EXPR_P (t))
3910                 t = TREE_OPERAND (t, 0);
3911               else
3912                 break;
3913             }
3914           while (1);
3915         }
3916       if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3917         warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3918     }
3919
3920   /* Warn about comparisons against string literals, with the exception
3921      of testing for equality or inequality of a string literal with NULL.  */
3922   if (code == EQ_EXPR || code == NE_EXPR)
3923     {
3924       if ((code1 == STRING_CST
3925            && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3926           || (code2 == STRING_CST
3927               && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3928         warning_at (location, OPT_Waddress,
3929                     "comparison with string literal results in unspecified behavior");
3930       /* Warn for ptr == '\0', it's likely that it should've been ptr[0].  */
3931       if (POINTER_TYPE_P (type1)
3932           && null_pointer_constant_p (arg2.value)
3933           && char_type_p (type2))
3934         {
3935           auto_diagnostic_group d;
3936           if (warning_at (location, OPT_Wpointer_compare,
3937                             "comparison between pointer and zero character "
3938                             "constant"))
3939             inform (arg1.get_start (),
3940                       "did you mean to dereference the pointer?");
3941         }
3942       else if (POINTER_TYPE_P (type2)
3943                && null_pointer_constant_p (arg1.value)
3944                && char_type_p (type1))
3945         {
3946           auto_diagnostic_group d;
3947           if (warning_at (location, OPT_Wpointer_compare,
3948                             "comparison between pointer and zero character "
3949                             "constant"))
3950             inform (arg2.get_start (),
3951                       "did you mean to dereference the pointer?");
3952         }
3953     }
3954   else if (TREE_CODE_CLASS (code) == tcc_comparison
3955            && (code1 == STRING_CST || code2 == STRING_CST))
3956     warning_at (location, OPT_Waddress,
3957                 "comparison with string literal results in unspecified "
3958                 "behavior");
3959
3960   if (warn_array_compare
3961       && TREE_CODE_CLASS (code) == tcc_comparison
3962       && TREE_CODE (type1) == ARRAY_TYPE
3963       && TREE_CODE (type2) == ARRAY_TYPE)
3964     do_warn_array_compare (location, code, arg1.value, arg2.value);
3965
3966   if (TREE_OVERFLOW_P (result.value)
3967       && !TREE_OVERFLOW_P (arg1.value)
3968       && !TREE_OVERFLOW_P (arg2.value))
3969     overflow_warning (location, result.value);
3970
3971   /* Warn about comparisons of different enum types.  */
3972   if (warn_enum_compare
3973       && TREE_CODE_CLASS (code) == tcc_comparison
3974       && TREE_CODE (type1) == ENUMERAL_TYPE
3975       && TREE_CODE (type2) == ENUMERAL_TYPE
3976       && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3977     warning_at (location, OPT_Wenum_compare,
3978                 "comparison between %qT and %qT",
3979                 type1, type2);
3980
3981   return result;
3982 }
3983 \f
3984 /* Return a tree for the difference of pointers OP0 and OP1.
3985    The resulting tree has type ptrdiff_t.  If POINTER_SUBTRACT sanitization is
3986    enabled, assign to INSTRUMENT_EXPR call to libsanitizer.  */
3987
3988 static tree
3989 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3990 {
3991   tree restype = ptrdiff_type_node;
3992   tree result, inttype;
3993
3994   addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3995   addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3996   tree target_type = TREE_TYPE (TREE_TYPE (op0));
3997   tree orig_op0 = op0;
3998   tree orig_op1 = op1;
3999
4000   /* If the operands point into different address spaces, we need to
4001      explicitly convert them to pointers into the common address space
4002      before we can subtract the numerical address values.  */
4003   if (as0 != as1)
4004     {
4005       addr_space_t as_common;
4006       tree common_type;
4007
4008       /* Determine the common superset address space.  This is guaranteed
4009          to exist because the caller verified that comp_target_types
4010          returned non-zero.  */
4011       if (!addr_space_superset (as0, as1, &as_common))
4012         gcc_unreachable ();
4013
4014       common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
4015       op0 = convert (common_type, op0);
4016       op1 = convert (common_type, op1);
4017     }
4018
4019   /* Determine integer type result of the subtraction.  This will usually
4020      be the same as the result type (ptrdiff_t), but may need to be a wider
4021      type if pointers for the address space are wider than ptrdiff_t.  */
4022   if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
4023     inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
4024   else
4025     inttype = restype;
4026
4027   if (TREE_CODE (target_type) == VOID_TYPE)
4028     pedwarn (loc, OPT_Wpointer_arith,
4029              "pointer of type %<void *%> used in subtraction");
4030   if (TREE_CODE (target_type) == FUNCTION_TYPE)
4031     pedwarn (loc, OPT_Wpointer_arith,
4032              "pointer to a function used in subtraction");
4033
4034   if (current_function_decl != NULL_TREE
4035       && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
4036     {
4037       op0 = save_expr (op0);
4038       op1 = save_expr (op1);
4039
4040       tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
4041       *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
4042     }
4043
4044   /* First do the subtraction, then build the divide operator
4045      and only convert at the very end.
4046      Do not do default conversions in case restype is a short type.  */
4047
4048   /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4049      pointers.  If some platform cannot provide that, or has a larger
4050      ptrdiff_type to support differences larger than half the address
4051      space, cast the pointers to some larger integer type and do the
4052      computations in that type.  */
4053   if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4054     op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4055                            convert (inttype, op1), false);
4056   else
4057     {
4058       /* Cast away qualifiers.  */
4059       op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4060       op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4061       op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4062     }
4063
4064   /* This generates an error if op1 is pointer to incomplete type.  */
4065   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4066     error_at (loc, "arithmetic on pointer to an incomplete type");
4067   else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4068                                 TREE_TYPE (TREE_TYPE (orig_op0))))
4069     verify_type_context (loc, TCTX_POINTER_ARITH,
4070                          TREE_TYPE (TREE_TYPE (orig_op1)));
4071
4072   op1 = c_size_in_bytes (target_type);
4073
4074   if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4075     error_at (loc, "arithmetic on pointer to an empty aggregate");
4076
4077   /* Divide by the size, in easiest possible way.  */
4078   result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4079                             op0, convert (inttype, op1));
4080
4081   /* Convert to final result type if necessary.  */
4082   return convert (restype, result);
4083 }
4084 \f
4085 /* Expand atomic compound assignments into an appropriate sequence as
4086    specified by the C11 standard section 6.5.16.2.
4087
4088        _Atomic T1 E1
4089        T2 E2
4090        E1 op= E2
4091
4092   This sequence is used for all types for which these operations are
4093   supported.
4094
4095   In addition, built-in versions of the 'fe' prefixed routines may
4096   need to be invoked for floating point (real, complex or vector) when
4097   floating-point exceptions are supported.  See 6.5.16.2 footnote 113.
4098
4099   T1 newval;
4100   T1 old;
4101   T1 *addr
4102   T2 val
4103   fenv_t fenv
4104
4105   addr = &E1;
4106   val = (E2);
4107   __atomic_load (addr, &old, SEQ_CST);
4108   feholdexcept (&fenv);
4109 loop:
4110     newval = old op val;
4111     if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4112                                           SEQ_CST))
4113       goto done;
4114     feclearexcept (FE_ALL_EXCEPT);
4115     goto loop:
4116 done:
4117   feupdateenv (&fenv);
4118
4119   The compiler will issue the __atomic_fetch_* built-in when possible,
4120   otherwise it will generate the generic form of the atomic operations.
4121   This requires temp(s) and has their address taken.  The atomic processing
4122   is smart enough to figure out when the size of an object can utilize
4123   a lock-free version, and convert the built-in call to the appropriate
4124   lock-free routine.  The optimizers will then dispose of any temps that
4125   are no longer required, and lock-free implementations are utilized as
4126   long as there is target support for the required size.
4127
4128   If the operator is NOP_EXPR, then this is a simple assignment, and
4129   an __atomic_store is issued to perform the assignment rather than
4130   the above loop.  */
4131
4132 /* Build an atomic assignment at LOC, expanding into the proper
4133    sequence to store LHS MODIFYCODE= RHS.  Return a value representing
4134    the result of the operation, unless RETURN_OLD_P, in which case
4135    return the old value of LHS (this is only for postincrement and
4136    postdecrement).  */
4137
4138 static tree
4139 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4140                      tree rhs, bool return_old_p)
4141 {
4142   tree fndecl, func_call;
4143   vec<tree, va_gc> *params;
4144   tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4145   tree old, old_addr;
4146   tree compound_stmt = NULL_TREE;
4147   tree stmt, goto_stmt;
4148   tree loop_label, loop_decl, done_label, done_decl;
4149
4150   tree lhs_type = TREE_TYPE (lhs);
4151   tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4152   tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4153   tree rhs_semantic_type = TREE_TYPE (rhs);
4154   tree nonatomic_rhs_semantic_type;
4155   tree rhs_type;
4156
4157   gcc_assert (TYPE_ATOMIC (lhs_type));
4158
4159   if (return_old_p)
4160     gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4161
4162   /* Allocate enough vector items for a compare_exchange.  */
4163   vec_alloc (params, 6);
4164
4165   /* Create a compound statement to hold the sequence of statements
4166      with a loop.  */
4167   if (modifycode != NOP_EXPR)
4168     {
4169       compound_stmt = c_begin_compound_stmt (false);
4170
4171       /* For consistency with build_modify_expr on non-_Atomic,
4172          mark the lhs as read.  Also, it would be very hard to match
4173          such expressions in mark_exp_read.  */
4174       mark_exp_read (lhs);
4175     }
4176
4177   /* Remove any excess precision (which is only present here in the
4178      case of compound assignments).  */
4179   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4180     {
4181       gcc_assert (modifycode != NOP_EXPR);
4182       rhs = TREE_OPERAND (rhs, 0);
4183     }
4184   rhs_type = TREE_TYPE (rhs);
4185
4186   /* Fold the RHS if it hasn't already been folded.  */
4187   if (modifycode != NOP_EXPR)
4188     rhs = c_fully_fold (rhs, false, NULL);
4189
4190   /* Remove the qualifiers for the rest of the expressions and create
4191      the VAL temp variable to hold the RHS.  */
4192   nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4193   nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4194   nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4195                                                       TYPE_UNQUALIFIED);
4196   val = create_tmp_var_raw (nonatomic_rhs_type);
4197   TREE_ADDRESSABLE (val) = 1;
4198   suppress_warning (val);
4199   rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4200                 NULL_TREE);
4201   TREE_SIDE_EFFECTS (rhs) = 1;
4202   SET_EXPR_LOCATION (rhs, loc);
4203   if (modifycode != NOP_EXPR)
4204     add_stmt (rhs);
4205
4206   /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4207      an atomic_store.  */
4208   if (modifycode == NOP_EXPR)
4209     {
4210       compound_stmt = rhs;
4211       /* Build __atomic_store (&lhs, &val, SEQ_CST)  */
4212       rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4213       fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4214       params->quick_push (lhs_addr);
4215       params->quick_push (rhs);
4216       params->quick_push (seq_cst);
4217       func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4218
4219       compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4220                               compound_stmt, func_call);
4221
4222       /* VAL is the value which was stored, return a COMPOUND_STMT of
4223          the statement and that value.  */
4224       return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4225     }
4226
4227   /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4228      __atomic_*_fetch built-in rather than a CAS loop.  atomic_bool type
4229      isn't applicable for such builtins.  ??? Do we want to handle enums?  */
4230   if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4231       && TREE_CODE (rhs_type) == INTEGER_TYPE)
4232     {
4233       built_in_function fncode;
4234       switch (modifycode)
4235         {
4236         case PLUS_EXPR:
4237         case POINTER_PLUS_EXPR:
4238           fncode = (return_old_p
4239                     ? BUILT_IN_ATOMIC_FETCH_ADD_N
4240                     : BUILT_IN_ATOMIC_ADD_FETCH_N);
4241           break;
4242         case MINUS_EXPR:
4243           fncode = (return_old_p
4244                     ? BUILT_IN_ATOMIC_FETCH_SUB_N
4245                     : BUILT_IN_ATOMIC_SUB_FETCH_N);
4246           break;
4247         case BIT_AND_EXPR:
4248           fncode = (return_old_p
4249                     ? BUILT_IN_ATOMIC_FETCH_AND_N
4250                     : BUILT_IN_ATOMIC_AND_FETCH_N);
4251           break;
4252         case BIT_IOR_EXPR:
4253           fncode = (return_old_p
4254                     ? BUILT_IN_ATOMIC_FETCH_OR_N
4255                     : BUILT_IN_ATOMIC_OR_FETCH_N);
4256           break;
4257         case BIT_XOR_EXPR:
4258           fncode = (return_old_p
4259                     ? BUILT_IN_ATOMIC_FETCH_XOR_N
4260                     : BUILT_IN_ATOMIC_XOR_FETCH_N);
4261           break;
4262         default:
4263           goto cas_loop;
4264         }
4265
4266       /* We can only use "_1" through "_16" variants of the atomic fetch
4267          built-ins.  */
4268       unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4269       if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4270         goto cas_loop;
4271
4272       /* If this is a pointer type, we need to multiply by the size of
4273          the pointer target type.  */
4274       if (POINTER_TYPE_P (lhs_type))
4275         {
4276           if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4277               /* ??? This would introduce -Wdiscarded-qualifiers
4278                  warning: __atomic_fetch_* expect volatile void *
4279                  type as the first argument.  (Assignments between
4280                  atomic and non-atomic objects are OK.) */
4281               || TYPE_RESTRICT (lhs_type))
4282             goto cas_loop;
4283           tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4284           rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4285                                  convert (ptrdiff_type_node, rhs),
4286                                  convert (ptrdiff_type_node, sz));
4287         }
4288
4289       /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4290          __atomic_*_fetch (&lhs, &val, SEQ_CST).  */
4291       fndecl = builtin_decl_explicit (fncode);
4292       params->quick_push (lhs_addr);
4293       params->quick_push (rhs);
4294       params->quick_push (seq_cst);
4295       func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4296
4297       newval = create_tmp_var_raw (nonatomic_lhs_type);
4298       TREE_ADDRESSABLE (newval) = 1;
4299       suppress_warning (newval);
4300       rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4301                     NULL_TREE, NULL_TREE);
4302       SET_EXPR_LOCATION (rhs, loc);
4303       add_stmt (rhs);
4304
4305       /* Finish the compound statement.  */
4306       compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4307
4308       /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4309          the statement and that value.  */
4310       return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4311     }
4312
4313 cas_loop:
4314   /* Create the variables and labels required for the op= form.  */
4315   old = create_tmp_var_raw (nonatomic_lhs_type);
4316   old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4317   TREE_ADDRESSABLE (old) = 1;
4318   suppress_warning (old);
4319
4320   newval = create_tmp_var_raw (nonatomic_lhs_type);
4321   newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4322   TREE_ADDRESSABLE (newval) = 1;
4323   suppress_warning (newval);
4324
4325   loop_decl = create_artificial_label (loc);
4326   loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4327
4328   done_decl = create_artificial_label (loc);
4329   done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4330
4331   /* __atomic_load (addr, &old, SEQ_CST).  */
4332   fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4333   params->quick_push (lhs_addr);
4334   params->quick_push (old_addr);
4335   params->quick_push (seq_cst);
4336   func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4337   old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4338                 NULL_TREE);
4339   add_stmt (old);
4340   params->truncate (0);
4341
4342   /* Create the expressions for floating-point environment
4343      manipulation, if required.  */
4344   bool need_fenv = (flag_trapping_math
4345                     && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4346   tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4347   if (need_fenv)
4348     targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4349
4350   if (hold_call)
4351     add_stmt (hold_call);
4352
4353   /* loop:  */
4354   add_stmt (loop_label);
4355
4356   /* newval = old + val;  */
4357   if (rhs_type != rhs_semantic_type)
4358     val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4359   rhs = build_binary_op (loc, modifycode, old, val, true);
4360   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4361     {
4362       tree eptype = TREE_TYPE (rhs);
4363       rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4364       rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4365     }
4366   else
4367     rhs = c_fully_fold (rhs, false, NULL);
4368   rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4369                                 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4370                                 NULL_TREE, 0);
4371   if (rhs != error_mark_node)
4372     {
4373       rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4374                     NULL_TREE);
4375       SET_EXPR_LOCATION (rhs, loc);
4376       add_stmt (rhs);
4377     }
4378
4379   /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4380        goto done;  */
4381   fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4382   params->quick_push (lhs_addr);
4383   params->quick_push (old_addr);
4384   params->quick_push (newval_addr);
4385   params->quick_push (integer_zero_node);
4386   params->quick_push (seq_cst);
4387   params->quick_push (seq_cst);
4388   func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4389
4390   goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4391   SET_EXPR_LOCATION (goto_stmt, loc);
4392
4393   stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4394   SET_EXPR_LOCATION (stmt, loc);
4395   add_stmt (stmt);
4396
4397   if (clear_call)
4398     add_stmt (clear_call);
4399
4400   /* goto loop;  */
4401   goto_stmt  = build1 (GOTO_EXPR, void_type_node, loop_decl);
4402   SET_EXPR_LOCATION (goto_stmt, loc);
4403   add_stmt (goto_stmt);
4404
4405   /* done:  */
4406   add_stmt (done_label);
4407
4408   if (update_call)
4409     add_stmt (update_call);
4410
4411   /* Finish the compound statement.  */
4412   compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4413
4414   /* NEWVAL is the value that was successfully stored, return a
4415      COMPOUND_EXPR of the statement and the appropriate value.  */
4416   return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4417                  return_old_p ? old : newval);
4418 }
4419
4420 /* Construct and perhaps optimize a tree representation
4421    for a unary operation.  CODE, a tree_code, specifies the operation
4422    and XARG is the operand.
4423    For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4424    promotions (such as from short to int).
4425    For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4426    non-lvalues; this is only used to handle conversion of non-lvalue arrays
4427    to pointers in C99.
4428
4429    LOCATION is the location of the operator.  */
4430
4431 tree
4432 build_unary_op (location_t location, enum tree_code code, tree xarg,
4433                 bool noconvert)
4434 {
4435   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
4436   tree arg = xarg;
4437   tree argtype = NULL_TREE;
4438   enum tree_code typecode;
4439   tree val;
4440   tree ret = error_mark_node;
4441   tree eptype = NULL_TREE;
4442   const char *invalid_op_diag;
4443   bool int_operands;
4444
4445   int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4446   if (int_operands)
4447     arg = remove_c_maybe_const_expr (arg);
4448
4449   if (code != ADDR_EXPR)
4450     arg = require_complete_type (location, arg);
4451
4452   typecode = TREE_CODE (TREE_TYPE (arg));
4453   if (typecode == ERROR_MARK)
4454     return error_mark_node;
4455   if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4456     typecode = INTEGER_TYPE;
4457
4458   if ((invalid_op_diag
4459        = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4460     {
4461       error_at (location, invalid_op_diag);
4462       return error_mark_node;
4463     }
4464
4465   if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4466     {
4467       eptype = TREE_TYPE (arg);
4468       arg = TREE_OPERAND (arg, 0);
4469     }
4470
4471   switch (code)
4472     {
4473     case CONVERT_EXPR:
4474       /* This is used for unary plus, because a CONVERT_EXPR
4475          is enough to prevent anybody from looking inside for
4476          associativity, but won't generate any code.  */
4477       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4478             || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4479             || gnu_vector_type_p (TREE_TYPE (arg))))
4480         {
4481           error_at (location, "wrong type argument to unary plus");
4482           return error_mark_node;
4483         }
4484       else if (!noconvert)
4485         arg = default_conversion (arg);
4486       arg = non_lvalue_loc (location, arg);
4487       break;
4488
4489     case NEGATE_EXPR:
4490       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4491             || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4492             || gnu_vector_type_p (TREE_TYPE (arg))))
4493         {
4494           error_at (location, "wrong type argument to unary minus");
4495           return error_mark_node;
4496         }
4497       else if (!noconvert)
4498         arg = default_conversion (arg);
4499       break;
4500
4501     case BIT_NOT_EXPR:
4502       /* ~ works on integer types and non float vectors. */
4503       if (typecode == INTEGER_TYPE
4504           || (gnu_vector_type_p (TREE_TYPE (arg))
4505               && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4506         {
4507           tree e = arg;
4508
4509           /* Warn if the expression has boolean value.  */
4510           while (TREE_CODE (e) == COMPOUND_EXPR)
4511             e = TREE_OPERAND (e, 1);
4512
4513           if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4514                || truth_value_p (TREE_CODE (e))))
4515             {
4516               auto_diagnostic_group d;
4517               if (warning_at (location, OPT_Wbool_operation,
4518                                 "%<~%> on a boolean expression"))
4519                 {
4520                   gcc_rich_location richloc (location);
4521                   richloc.add_fixit_insert_before (location, "!");
4522                   inform (&richloc, "did you mean to use logical not?");
4523                 }
4524             }
4525           if (!noconvert)
4526             arg = default_conversion (arg);
4527         }
4528       else if (typecode == COMPLEX_TYPE)
4529         {
4530           code = CONJ_EXPR;
4531           pedwarn (location, OPT_Wpedantic,
4532                    "ISO C does not support %<~%> for complex conjugation");
4533           if (!noconvert)
4534             arg = default_conversion (arg);
4535         }
4536       else
4537         {
4538           error_at (location, "wrong type argument to bit-complement");
4539           return error_mark_node;
4540         }
4541       break;
4542
4543     case ABS_EXPR:
4544       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4545         {
4546           error_at (location, "wrong type argument to abs");
4547           return error_mark_node;
4548         }
4549       else if (!noconvert)
4550         arg = default_conversion (arg);
4551       break;
4552
4553     case ABSU_EXPR:
4554       if (!(typecode == INTEGER_TYPE))
4555         {
4556           error_at (location, "wrong type argument to absu");
4557           return error_mark_node;
4558         }
4559       else if (!noconvert)
4560         arg = default_conversion (arg);
4561       break;
4562
4563     case CONJ_EXPR:
4564       /* Conjugating a real value is a no-op, but allow it anyway.  */
4565       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4566             || typecode == COMPLEX_TYPE))
4567         {
4568           error_at (location, "wrong type argument to conjugation");
4569           return error_mark_node;
4570         }
4571       else if (!noconvert)
4572         arg = default_conversion (arg);
4573       break;
4574
4575     case TRUTH_NOT_EXPR:
4576       if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4577           && typecode != REAL_TYPE && typecode != POINTER_TYPE
4578           && typecode != COMPLEX_TYPE)
4579         {
4580           error_at (location,
4581                     "wrong type argument to unary exclamation mark");
4582           return error_mark_node;
4583         }
4584       if (int_operands)
4585         {
4586           arg = c_objc_common_truthvalue_conversion (location, xarg);
4587           arg = remove_c_maybe_const_expr (arg);
4588         }
4589       else
4590         arg = c_objc_common_truthvalue_conversion (location, arg);
4591       ret = invert_truthvalue_loc (location, arg);
4592       /* If the TRUTH_NOT_EXPR has been folded, reset the location.  */
4593       if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4594         location = EXPR_LOCATION (ret);
4595       goto return_build_unary_op;
4596
4597     case REALPART_EXPR:
4598     case IMAGPART_EXPR:
4599       ret = build_real_imag_expr (location, code, arg);
4600       if (ret == error_mark_node)
4601         return error_mark_node;
4602       if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4603         eptype = TREE_TYPE (eptype);
4604       goto return_build_unary_op;
4605
4606     case PREINCREMENT_EXPR:
4607     case POSTINCREMENT_EXPR:
4608     case PREDECREMENT_EXPR:
4609     case POSTDECREMENT_EXPR:
4610
4611       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4612         {
4613           tree inner = build_unary_op (location, code,
4614                                        C_MAYBE_CONST_EXPR_EXPR (arg),
4615                                        noconvert);
4616           if (inner == error_mark_node)
4617             return error_mark_node;
4618           ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4619                         C_MAYBE_CONST_EXPR_PRE (arg), inner);
4620           gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4621           C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4622           goto return_build_unary_op;
4623         }
4624
4625       /* Complain about anything that is not a true lvalue.  In
4626          Objective-C, skip this check for property_refs.  */
4627       if (!objc_is_property_ref (arg)
4628           && !lvalue_or_else (location,
4629                               arg, ((code == PREINCREMENT_EXPR
4630                                      || code == POSTINCREMENT_EXPR)
4631                                     ? lv_increment
4632                                     : lv_decrement)))
4633         return error_mark_node;
4634
4635       if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4636         {
4637           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4638             warning_at (location, OPT_Wc___compat,
4639                         "increment of enumeration value is invalid in C++");
4640           else
4641             warning_at (location, OPT_Wc___compat,
4642                         "decrement of enumeration value is invalid in C++");
4643         }
4644
4645       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4646         {
4647           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4648             warning_at (location, OPT_Wbool_operation,
4649                         "increment of a boolean expression");
4650           else
4651             warning_at (location, OPT_Wbool_operation,
4652                         "decrement of a boolean expression");
4653         }
4654
4655       /* Ensure the argument is fully folded inside any SAVE_EXPR.  */
4656       arg = c_fully_fold (arg, false, NULL, true);
4657
4658       bool atomic_op;
4659       atomic_op = really_atomic_lvalue (arg);
4660
4661       /* Increment or decrement the real part of the value,
4662          and don't change the imaginary part.  */
4663       if (typecode == COMPLEX_TYPE)
4664         {
4665           tree real, imag;
4666
4667           pedwarn (location, OPT_Wpedantic,
4668                    "ISO C does not support %<++%> and %<--%> on complex types");
4669
4670           if (!atomic_op)
4671             {
4672               arg = stabilize_reference (arg);
4673               real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4674                                      true);
4675               imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4676                                      true);
4677               real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4678               if (real == error_mark_node || imag == error_mark_node)
4679                 return error_mark_node;
4680               ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4681                             real, imag);
4682               goto return_build_unary_op;
4683             }
4684         }
4685
4686       /* Report invalid types.  */
4687
4688       if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4689           && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4690           && typecode != COMPLEX_TYPE
4691           && !gnu_vector_type_p (TREE_TYPE (arg)))
4692         {
4693           if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4694             error_at (location, "wrong type argument to increment");
4695           else
4696             error_at (location, "wrong type argument to decrement");
4697
4698           return error_mark_node;
4699         }
4700
4701       {
4702         tree inc;
4703
4704         argtype = TREE_TYPE (arg);
4705
4706         /* Compute the increment.  */
4707
4708         if (typecode == POINTER_TYPE)
4709           {
4710             /* If pointer target is an incomplete type,
4711                we just cannot know how to do the arithmetic.  */
4712             if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4713               {
4714                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4715                   error_at (location,
4716                             "increment of pointer to an incomplete type %qT",
4717                             TREE_TYPE (argtype));
4718                 else
4719                   error_at (location,
4720                             "decrement of pointer to an incomplete type %qT",
4721                             TREE_TYPE (argtype));
4722               }
4723             else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4724                      || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4725               {
4726                 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4727                   pedwarn (location, OPT_Wpointer_arith,
4728                            "wrong type argument to increment");
4729                 else
4730                   pedwarn (location, OPT_Wpointer_arith,
4731                            "wrong type argument to decrement");
4732               }
4733             else
4734               verify_type_context (location, TCTX_POINTER_ARITH,
4735                                    TREE_TYPE (argtype));
4736
4737             inc = c_size_in_bytes (TREE_TYPE (argtype));
4738             inc = convert_to_ptrofftype_loc (location, inc);
4739           }
4740         else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4741           {
4742             /* For signed fract types, we invert ++ to -- or
4743                -- to ++, and change inc from 1 to -1, because
4744                it is not possible to represent 1 in signed fract constants.
4745                For unsigned fract types, the result always overflows and
4746                we get an undefined (original) or the maximum value.  */
4747             if (code == PREINCREMENT_EXPR)
4748               code = PREDECREMENT_EXPR;
4749             else if (code == PREDECREMENT_EXPR)
4750               code = PREINCREMENT_EXPR;
4751             else if (code == POSTINCREMENT_EXPR)
4752               code = POSTDECREMENT_EXPR;
4753             else /* code == POSTDECREMENT_EXPR  */
4754               code = POSTINCREMENT_EXPR;
4755
4756             inc = integer_minus_one_node;
4757             inc = convert (argtype, inc);
4758           }
4759         else
4760           {
4761             inc = VECTOR_TYPE_P (argtype)
4762               ? build_one_cst (argtype)
4763               : integer_one_node;
4764             inc = convert (argtype, inc);
4765           }
4766
4767         /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4768            need to ask Objective-C to build the increment or decrement
4769            expression for it.  */
4770         if (objc_is_property_ref (arg))
4771           return objc_build_incr_expr_for_property_ref (location, code,
4772                                                         arg, inc);
4773
4774         /* Report a read-only lvalue.  */
4775         if (TYPE_READONLY (argtype))
4776           {
4777             readonly_error (location, arg,
4778                             ((code == PREINCREMENT_EXPR
4779                               || code == POSTINCREMENT_EXPR)
4780                              ? lv_increment : lv_decrement));
4781             return error_mark_node;
4782           }
4783         else if (TREE_READONLY (arg))
4784           readonly_warning (arg,
4785                             ((code == PREINCREMENT_EXPR
4786                               || code == POSTINCREMENT_EXPR)
4787                              ? lv_increment : lv_decrement));
4788
4789         /* If the argument is atomic, use the special code sequences for
4790            atomic compound assignment.  */
4791         if (atomic_op)
4792           {
4793             arg = stabilize_reference (arg);
4794             ret = build_atomic_assign (location, arg,
4795                                        ((code == PREINCREMENT_EXPR
4796                                          || code == POSTINCREMENT_EXPR)
4797                                         ? PLUS_EXPR
4798                                         : MINUS_EXPR),
4799                                        (FRACT_MODE_P (TYPE_MODE (argtype))
4800                                         ? inc
4801                                         : integer_one_node),
4802                                        (code == POSTINCREMENT_EXPR
4803                                         || code == POSTDECREMENT_EXPR));
4804             goto return_build_unary_op;
4805           }
4806
4807         if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4808           val = boolean_increment (code, arg);
4809         else
4810           val = build2 (code, TREE_TYPE (arg), arg, inc);
4811         TREE_SIDE_EFFECTS (val) = 1;
4812         ret = val;
4813         goto return_build_unary_op;
4814       }
4815
4816     case ADDR_EXPR:
4817       /* Note that this operation never does default_conversion.  */
4818
4819       /* The operand of unary '&' must be an lvalue (which excludes
4820          expressions of type void), or, in C99, the result of a [] or
4821          unary '*' operator.  */
4822       if (VOID_TYPE_P (TREE_TYPE (arg))
4823           && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4824           && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4825         pedwarn (location, 0, "taking address of expression of type %<void%>");
4826
4827       /* Let &* cancel out to simplify resulting code.  */
4828       if (INDIRECT_REF_P (arg))
4829         {
4830           /* Don't let this be an lvalue.  */
4831           if (lvalue_p (TREE_OPERAND (arg, 0)))
4832             return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4833           ret = TREE_OPERAND (arg, 0);
4834           goto return_build_unary_op;
4835         }
4836
4837       /* Anything not already handled and not a true memory reference
4838          or a non-lvalue array is an error.  */
4839       if (typecode != FUNCTION_TYPE && !noconvert
4840           && !lvalue_or_else (location, arg, lv_addressof))
4841         return error_mark_node;
4842
4843       /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4844          folding later.  */
4845       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4846         {
4847           tree inner = build_unary_op (location, code,
4848                                        C_MAYBE_CONST_EXPR_EXPR (arg),
4849                                        noconvert);
4850           ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4851                         C_MAYBE_CONST_EXPR_PRE (arg), inner);
4852           gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4853           C_MAYBE_CONST_EXPR_NON_CONST (ret)
4854             = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4855           goto return_build_unary_op;
4856         }
4857
4858       /* Ordinary case; arg is a COMPONENT_REF or a decl.  */
4859       argtype = TREE_TYPE (arg);
4860
4861       /* If the lvalue is const or volatile, merge that into the type
4862          to which the address will point.  This is only needed
4863          for function types.  */
4864       if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4865           && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4866           && TREE_CODE (argtype) == FUNCTION_TYPE)
4867         {
4868           int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4869           int quals = orig_quals;
4870
4871           if (TREE_READONLY (arg))
4872             quals |= TYPE_QUAL_CONST;
4873           if (TREE_THIS_VOLATILE (arg))
4874             quals |= TYPE_QUAL_VOLATILE;
4875
4876           argtype = c_build_qualified_type (argtype, quals);
4877         }
4878
4879       switch (TREE_CODE (arg))
4880         {
4881         case COMPONENT_REF:
4882           if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4883             {
4884               error_at (location, "cannot take address of bit-field %qD",
4885                         TREE_OPERAND (arg, 1));
4886               return error_mark_node;
4887             }
4888
4889           /* fall through */
4890
4891         case ARRAY_REF:
4892           if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4893             {
4894               if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4895                   && !POINTER_TYPE_P (TREE_TYPE (arg))
4896                   && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4897                 {
4898                   error_at (location, "cannot take address of scalar with "
4899                             "reverse storage order");
4900                   return error_mark_node;
4901                 }
4902
4903               if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4904                   && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4905                 warning_at (location, OPT_Wscalar_storage_order,
4906                             "address of array with reverse scalar storage "
4907                             "order requested");
4908             }
4909
4910         default:
4911           break;
4912         }
4913
4914       if (!c_mark_addressable (arg))
4915         return error_mark_node;
4916
4917       gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4918                   || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4919
4920       argtype = build_pointer_type (argtype);
4921
4922       /* ??? Cope with user tricks that amount to offsetof.  Delete this
4923          when we have proper support for integer constant expressions.  */
4924       val = get_base_address (arg);
4925       if (val && INDIRECT_REF_P (val)
4926           && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4927         {
4928           ret = fold_offsetof (arg, argtype);
4929           goto return_build_unary_op;
4930         }
4931
4932       val = build1 (ADDR_EXPR, argtype, arg);
4933
4934       ret = val;
4935       goto return_build_unary_op;
4936
4937     case PAREN_EXPR:
4938       ret = build1 (code, TREE_TYPE (arg), arg);
4939       goto return_build_unary_op;
4940
4941     default:
4942       gcc_unreachable ();
4943     }
4944
4945   if (argtype == NULL_TREE)
4946     argtype = TREE_TYPE (arg);
4947   if (TREE_CODE (arg) == INTEGER_CST)
4948     ret = (require_constant_value
4949            ? fold_build1_initializer_loc (location, code, argtype, arg)
4950            : fold_build1_loc (location, code, argtype, arg));
4951   else
4952     ret = build1 (code, argtype, arg);
4953  return_build_unary_op:
4954   gcc_assert (ret != error_mark_node);
4955   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4956       && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4957     ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4958   else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4959     ret = note_integer_operands (ret);
4960   if (eptype)
4961     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4962   protected_set_expr_location (ret, location);
4963   return ret;
4964 }
4965
4966 /* Return nonzero if REF is an lvalue valid for this language.
4967    Lvalues can be assigned, unless their type has TYPE_READONLY.
4968    Lvalues can have their address taken, unless they have C_DECL_REGISTER.  */
4969
4970 bool
4971 lvalue_p (const_tree ref)
4972 {
4973   const enum tree_code code = TREE_CODE (ref);
4974
4975   switch (code)
4976     {
4977     case REALPART_EXPR:
4978     case IMAGPART_EXPR:
4979     case COMPONENT_REF:
4980       return lvalue_p (TREE_OPERAND (ref, 0));
4981
4982     case C_MAYBE_CONST_EXPR:
4983       return lvalue_p (TREE_OPERAND (ref, 1));
4984
4985     case COMPOUND_LITERAL_EXPR:
4986     case STRING_CST:
4987       return true;
4988
4989     case MEM_REF:
4990     case TARGET_MEM_REF:
4991       /* MEM_REFs can appear from -fgimple parsing or folding, so allow them
4992          here as well.  */
4993     case INDIRECT_REF:
4994     case ARRAY_REF:
4995     case VAR_DECL:
4996     case PARM_DECL:
4997     case RESULT_DECL:
4998     case ERROR_MARK:
4999       return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
5000               && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5001
5002     case BIND_EXPR:
5003       return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5004
5005     default:
5006       return false;
5007     }
5008 }
5009 \f
5010 /* Give a warning for storing in something that is read-only in GCC
5011    terms but not const in ISO C terms.  */
5012
5013 static void
5014 readonly_warning (tree arg, enum lvalue_use use)
5015 {
5016   switch (use)
5017     {
5018     case lv_assign:
5019       warning (0, "assignment of read-only location %qE", arg);
5020       break;
5021     case lv_increment:
5022       warning (0, "increment of read-only location %qE", arg);
5023       break;
5024     case lv_decrement:
5025       warning (0, "decrement of read-only location %qE", arg);
5026       break;
5027     default:
5028       gcc_unreachable ();
5029     }
5030   return;
5031 }
5032
5033
5034 /* Return nonzero if REF is an lvalue valid for this language;
5035    otherwise, print an error message and return zero.  USE says
5036    how the lvalue is being used and so selects the error message.
5037    LOCATION is the location at which any error should be reported.  */
5038
5039 static int
5040 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
5041 {
5042   int win = lvalue_p (ref);
5043
5044   if (!win)
5045     lvalue_error (loc, use);
5046
5047   return win;
5048 }
5049 \f
5050 /* Mark EXP saying that we need to be able to take the
5051    address of it; it should not be allocated in a register.
5052    Returns true if successful.  ARRAY_REF_P is true if this
5053    is for ARRAY_REF construction - in that case we don't want
5054    to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5055    it is fine to use ARRAY_REFs for vector subscripts on vector
5056    register variables.  */
5057
5058 bool
5059 c_mark_addressable (tree exp, bool array_ref_p)
5060 {
5061   tree x = exp;
5062
5063   while (1)
5064     switch (TREE_CODE (x))
5065       {
5066       case VIEW_CONVERT_EXPR:
5067         if (array_ref_p
5068             && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5069             && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5070           return true;
5071         x = TREE_OPERAND (x, 0);
5072         break;
5073
5074       case COMPONENT_REF:
5075         if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5076           {
5077             error ("cannot take address of bit-field %qD",
5078                    TREE_OPERAND (x, 1));
5079             return false;
5080           }
5081         /* FALLTHRU */
5082       case ADDR_EXPR:
5083       case ARRAY_REF:
5084       case REALPART_EXPR:
5085       case IMAGPART_EXPR:
5086         x = TREE_OPERAND (x, 0);
5087         break;
5088
5089       case COMPOUND_LITERAL_EXPR:
5090         TREE_ADDRESSABLE (x) = 1;
5091         TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5092         return true;
5093
5094       case CONSTRUCTOR:
5095         TREE_ADDRESSABLE (x) = 1;
5096         return true;
5097
5098       case VAR_DECL:
5099       case CONST_DECL:
5100       case PARM_DECL:
5101       case RESULT_DECL:
5102         if (C_DECL_REGISTER (x)
5103             && DECL_NONLOCAL (x))
5104           {
5105             if (TREE_PUBLIC (x) || is_global_var (x))
5106               {
5107                 error
5108                   ("global register variable %qD used in nested function", x);
5109                 return false;
5110               }
5111             pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5112           }
5113         else if (C_DECL_REGISTER (x))
5114           {
5115             if (TREE_PUBLIC (x) || is_global_var (x))
5116               error ("address of global register variable %qD requested", x);
5117             else
5118               error ("address of register variable %qD requested", x);
5119             return false;
5120           }
5121
5122         /* FALLTHRU */
5123       case FUNCTION_DECL:
5124         TREE_ADDRESSABLE (x) = 1;
5125         /* FALLTHRU */
5126       default:
5127         return true;
5128     }
5129 }
5130 \f
5131 /* Convert EXPR to TYPE, warning about conversion problems with
5132    constants.  SEMANTIC_TYPE is the type this conversion would use
5133    without excess precision. If SEMANTIC_TYPE is NULL, this function
5134    is equivalent to convert_and_check. This function is a wrapper that
5135    handles conversions that may be different than
5136    the usual ones because of excess precision.  */
5137
5138 static tree
5139 ep_convert_and_check (location_t loc, tree type, tree expr,
5140                       tree semantic_type)
5141 {
5142   if (TREE_TYPE (expr) == type)
5143     return expr;
5144
5145   /* For C11, integer conversions may have results with excess
5146      precision.  */
5147   if (flag_isoc11 || !semantic_type)
5148     return convert_and_check (loc, type, expr);
5149
5150   if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5151       && TREE_TYPE (expr) != semantic_type)
5152     {
5153       /* For integers, we need to check the real conversion, not
5154          the conversion to the excess precision type.  */
5155       expr = convert_and_check (loc, semantic_type, expr);
5156     }
5157   /* Result type is the excess precision type, which should be
5158      large enough, so do not check.  */
5159   return convert (type, expr);
5160 }
5161
5162 /* If EXPR refers to a built-in declared without a prototype returns
5163    the actual type of the built-in and, if non-null, set *BLTIN to
5164    a pointer to the built-in.  Otherwise return the type of EXPR
5165    and clear *BLTIN if non-null.  */
5166
5167 static tree
5168 type_or_builtin_type (tree expr, tree *bltin = NULL)
5169 {
5170   tree dummy;
5171   if (!bltin)
5172     bltin = &dummy;
5173
5174   *bltin = NULL_TREE;
5175
5176   tree type = TREE_TYPE (expr);
5177   if (TREE_CODE (expr) != ADDR_EXPR)
5178     return type;
5179
5180   tree oper = TREE_OPERAND (expr, 0);
5181   if (!DECL_P (oper)
5182       || TREE_CODE (oper) != FUNCTION_DECL
5183       || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5184     return type;
5185
5186   built_in_function code = DECL_FUNCTION_CODE (oper);
5187   if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5188     return type;
5189
5190   if ((*bltin = builtin_decl_implicit (code)))
5191     type = build_pointer_type (TREE_TYPE (*bltin));
5192
5193   return type;
5194 }
5195
5196 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  If
5197    IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5198    if folded to an integer constant then the unselected half may
5199    contain arbitrary operations not normally permitted in constant
5200    expressions.  Set the location of the expression to LOC.  */
5201
5202 tree
5203 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5204                         tree op1, tree op1_original_type, location_t op1_loc,
5205                         tree op2, tree op2_original_type, location_t op2_loc)
5206 {
5207   tree type1;
5208   tree type2;
5209   enum tree_code code1;
5210   enum tree_code code2;
5211   tree result_type = NULL;
5212   tree semantic_result_type = NULL;
5213   tree orig_op1 = op1, orig_op2 = op2;
5214   bool int_const, op1_int_operands, op2_int_operands, int_operands;
5215   bool ifexp_int_operands;
5216   tree ret;
5217
5218   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5219   if (op1_int_operands)
5220     op1 = remove_c_maybe_const_expr (op1);
5221   op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5222   if (op2_int_operands)
5223     op2 = remove_c_maybe_const_expr (op2);
5224   ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5225   if (ifexp_int_operands)
5226     ifexp = remove_c_maybe_const_expr (ifexp);
5227
5228   /* Promote both alternatives.  */
5229
5230   if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5231     op1 = default_conversion (op1);
5232   if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5233     op2 = default_conversion (op2);
5234
5235   if (TREE_CODE (ifexp) == ERROR_MARK
5236       || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5237       || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5238     return error_mark_node;
5239
5240   tree bltin1 = NULL_TREE;
5241   tree bltin2 = NULL_TREE;
5242   type1 = type_or_builtin_type (op1, &bltin1);
5243   code1 = TREE_CODE (type1);
5244   type2 = type_or_builtin_type (op2, &bltin2);
5245   code2 = TREE_CODE (type2);
5246
5247   if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5248     return error_mark_node;
5249
5250   if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5251     return error_mark_node;
5252
5253   /* C90 does not permit non-lvalue arrays in conditional expressions.
5254      In C99 they will be pointers by now.  */
5255   if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5256     {
5257       error_at (colon_loc, "non-lvalue array in conditional expression");
5258       return error_mark_node;
5259     }
5260
5261   if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5262        || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5263       && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5264           || code1 == COMPLEX_TYPE)
5265       && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5266           || code2 == COMPLEX_TYPE))
5267     {
5268       semantic_result_type = c_common_type (type1, type2);
5269       if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5270         {
5271           op1 = TREE_OPERAND (op1, 0);
5272           type1 = TREE_TYPE (op1);
5273           gcc_assert (TREE_CODE (type1) == code1);
5274         }
5275       if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5276         {
5277           op2 = TREE_OPERAND (op2, 0);
5278           type2 = TREE_TYPE (op2);
5279           gcc_assert (TREE_CODE (type2) == code2);
5280         }
5281     }
5282
5283   if (warn_cxx_compat)
5284     {
5285       tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5286       tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5287
5288       if (TREE_CODE (t1) == ENUMERAL_TYPE
5289           && TREE_CODE (t2) == ENUMERAL_TYPE
5290           && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5291         warning_at (colon_loc, OPT_Wc___compat,
5292                     ("different enum types in conditional is "
5293                      "invalid in C++: %qT vs %qT"),
5294                     t1, t2);
5295     }
5296
5297   /* Quickly detect the usual case where op1 and op2 have the same type
5298      after promotion.  */
5299   if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5300     {
5301       if (type1 == type2)
5302         result_type = type1;
5303       else
5304         result_type = TYPE_MAIN_VARIANT (type1);
5305     }
5306   else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5307             || code1 == COMPLEX_TYPE)
5308            && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5309                || code2 == COMPLEX_TYPE))
5310     {
5311       /* In C11, a conditional expression between a floating-point
5312          type and an integer type should convert the integer type to
5313          the evaluation format of the floating-point type, with
5314          possible excess precision.  */
5315       tree eptype1 = type1;
5316       tree eptype2 = type2;
5317       if (flag_isoc11)
5318         {
5319           tree eptype;
5320           if (ANY_INTEGRAL_TYPE_P (type1)
5321               && (eptype = excess_precision_type (type2)) != NULL_TREE)
5322             {
5323               eptype2 = eptype;
5324               if (!semantic_result_type)
5325                 semantic_result_type = c_common_type (type1, type2);
5326             }
5327           else if (ANY_INTEGRAL_TYPE_P (type2)
5328                    && (eptype = excess_precision_type (type1)) != NULL_TREE)
5329             {
5330               eptype1 = eptype;
5331               if (!semantic_result_type)
5332                 semantic_result_type = c_common_type (type1, type2);
5333             }
5334         }
5335       result_type = c_common_type (eptype1, eptype2);
5336       if (result_type == error_mark_node)
5337         return error_mark_node;
5338       do_warn_double_promotion (result_type, type1, type2,
5339                                 "implicit conversion from %qT to %qT to "
5340                                 "match other result of conditional",
5341                                 colon_loc);
5342
5343       /* If -Wsign-compare, warn here if type1 and type2 have
5344          different signedness.  We'll promote the signed to unsigned
5345          and later code won't know it used to be different.
5346          Do this check on the original types, so that explicit casts
5347          will be considered, but default promotions won't.  */
5348       if (c_inhibit_evaluation_warnings == 0)
5349         {
5350           int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5351           int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5352
5353           if (unsigned_op1 ^ unsigned_op2)
5354             {
5355               bool ovf;
5356
5357               /* Do not warn if the result type is signed, since the
5358                  signed type will only be chosen if it can represent
5359                  all the values of the unsigned type.  */
5360               if (!TYPE_UNSIGNED (result_type))
5361                 /* OK */;
5362               else
5363                 {
5364                   bool op1_maybe_const = true;
5365                   bool op2_maybe_const = true;
5366
5367                   /* Do not warn if the signed quantity is an
5368                      unsuffixed integer literal (or some static
5369                      constant expression involving such literals) and
5370                      it is non-negative.  This warning requires the
5371                      operands to be folded for best results, so do
5372                      that folding in this case even without
5373                      warn_sign_compare to avoid warning options
5374                      possibly affecting code generation.  */
5375                   c_inhibit_evaluation_warnings
5376                     += (ifexp == truthvalue_false_node);
5377                   op1 = c_fully_fold (op1, require_constant_value,
5378                                       &op1_maybe_const);
5379                   c_inhibit_evaluation_warnings
5380                     -= (ifexp == truthvalue_false_node);
5381
5382                   c_inhibit_evaluation_warnings
5383                     += (ifexp == truthvalue_true_node);
5384                   op2 = c_fully_fold (op2, require_constant_value,
5385                                       &op2_maybe_const);
5386                   c_inhibit_evaluation_warnings
5387                     -= (ifexp == truthvalue_true_node);
5388
5389                   if (warn_sign_compare)
5390                     {
5391                       if ((unsigned_op2
5392                            && tree_expr_nonnegative_warnv_p (op1, &ovf))
5393                           || (unsigned_op1
5394                               && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5395                         /* OK */;
5396                       else if (unsigned_op2)
5397                         warning_at (op1_loc, OPT_Wsign_compare,
5398                                     "operand of %<?:%> changes signedness from "
5399                                     "%qT to %qT due to unsignedness of other "
5400                                     "operand", TREE_TYPE (orig_op1),
5401                                     TREE_TYPE (orig_op2));
5402                       else
5403                         warning_at (op2_loc, OPT_Wsign_compare,
5404                                     "operand of %<?:%> changes signedness from "
5405                                     "%qT to %qT due to unsignedness of other "
5406                                     "operand", TREE_TYPE (orig_op2),
5407                                     TREE_TYPE (orig_op1));
5408                     }
5409                   if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5410                     op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5411                   if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5412                     op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5413                 }
5414             }
5415         }
5416     }
5417   else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5418     {
5419       if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5420         pedwarn (colon_loc, OPT_Wpedantic,
5421                  "ISO C forbids conditional expr with only one void side");
5422       result_type = void_type_node;
5423     }
5424   else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5425     {
5426       addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5427       addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5428       addr_space_t as_common;
5429
5430       if (comp_target_types (colon_loc, type1, type2))
5431         result_type = common_pointer_type (type1, type2);
5432       else if (null_pointer_constant_p (orig_op1))
5433         result_type = type2;
5434       else if (null_pointer_constant_p (orig_op2))
5435         result_type = type1;
5436       else if (!addr_space_superset (as1, as2, &as_common))
5437         {
5438           error_at (colon_loc, "pointers to disjoint address spaces "
5439                     "used in conditional expression");
5440           return error_mark_node;
5441         }
5442       else if ((VOID_TYPE_P (TREE_TYPE (type1))
5443                 && !TYPE_ATOMIC (TREE_TYPE (type1)))
5444                || (VOID_TYPE_P (TREE_TYPE (type2))
5445                    && !TYPE_ATOMIC (TREE_TYPE (type2))))
5446         {
5447           tree t1 = TREE_TYPE (type1);
5448           tree t2 = TREE_TYPE (type2);
5449           if (!(VOID_TYPE_P (t1)
5450                 && !TYPE_ATOMIC (t1)))
5451            {
5452              /* roles are swapped */
5453              t1 = t2;
5454              t2 = TREE_TYPE (type1);
5455            }
5456           tree t2_stripped = strip_array_types (t2);
5457           if ((TREE_CODE (t2) == ARRAY_TYPE)
5458               && (TYPE_QUALS (t2_stripped) & ~TYPE_QUALS (t1)))
5459             {
5460               if (!flag_isoc2x)
5461                 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5462                             "pointer to array loses qualifier "
5463                             "in conditional expression");
5464               else if (warn_c11_c2x_compat > 0)
5465                 warning_at (colon_loc, OPT_Wc11_c2x_compat,
5466                             "pointer to array loses qualifier "
5467                             "in conditional expression in ISO C before C2X");
5468             }
5469           if (TREE_CODE (t2) == FUNCTION_TYPE)
5470             pedwarn (colon_loc, OPT_Wpedantic,
5471                      "ISO C forbids conditional expr between "
5472                      "%<void *%> and function pointer");
5473           /* for array, use qualifiers of element type */
5474           if (flag_isoc2x)
5475             t2 = t2_stripped;
5476           result_type = build_pointer_type (qualify_type (t1, t2));
5477         }
5478       /* Objective-C pointer comparisons are a bit more lenient.  */
5479       else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5480         result_type = objc_common_type (type1, type2);
5481       else
5482         {
5483           int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5484           if (bltin1 && bltin2)
5485             warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5486                         "pointer type mismatch between %qT and %qT "
5487                         "of %qD and %qD in conditional expression",
5488                         type1, type2, bltin1, bltin2);
5489           else
5490             pedwarn (colon_loc, 0,
5491                      "pointer type mismatch in conditional expression");
5492           result_type = build_pointer_type
5493                           (build_qualified_type (void_type_node, qual));
5494         }
5495     }
5496   else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5497     {
5498       if (!null_pointer_constant_p (orig_op2))
5499         pedwarn (colon_loc, 0,
5500                  "pointer/integer type mismatch in conditional expression");
5501       else
5502         {
5503           op2 = null_pointer_node;
5504         }
5505       result_type = type1;
5506     }
5507   else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5508     {
5509       if (!null_pointer_constant_p (orig_op1))
5510         pedwarn (colon_loc, 0,
5511                  "pointer/integer type mismatch in conditional expression");
5512       else
5513         {
5514           op1 = null_pointer_node;
5515         }
5516       result_type = type2;
5517     }
5518
5519   if (!result_type)
5520     {
5521       if (flag_cond_mismatch)
5522         result_type = void_type_node;
5523       else
5524         {
5525           error_at (colon_loc, "type mismatch in conditional expression");
5526           return error_mark_node;
5527         }
5528     }
5529
5530   /* Merge const and volatile flags of the incoming types.  */
5531   result_type
5532     = build_type_variant (result_type,
5533                           TYPE_READONLY (type1) || TYPE_READONLY (type2),
5534                           TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5535
5536   op1 = ep_convert_and_check (colon_loc, result_type, op1,
5537                               semantic_result_type);
5538   op2 = ep_convert_and_check (colon_loc, result_type, op2,
5539                               semantic_result_type);
5540
5541   if (ifexp_bcp && ifexp == truthvalue_true_node)
5542     {
5543       op2_int_operands = true;
5544       op1 = c_fully_fold (op1, require_constant_value, NULL);
5545     }
5546   if (ifexp_bcp && ifexp == truthvalue_false_node)
5547     {
5548       op1_int_operands = true;
5549       op2 = c_fully_fold (op2, require_constant_value, NULL);
5550     }
5551   int_const = int_operands = (ifexp_int_operands
5552                               && op1_int_operands
5553                               && op2_int_operands);
5554   if (int_operands)
5555     {
5556       int_const = ((ifexp == truthvalue_true_node
5557                     && TREE_CODE (orig_op1) == INTEGER_CST
5558                     && !TREE_OVERFLOW (orig_op1))
5559                    || (ifexp == truthvalue_false_node
5560                        && TREE_CODE (orig_op2) == INTEGER_CST
5561                        && !TREE_OVERFLOW (orig_op2)));
5562     }
5563
5564   /* Need to convert condition operand into a vector mask.  */
5565   if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5566     {
5567       tree vectype = TREE_TYPE (ifexp);
5568       tree elem_type = TREE_TYPE (vectype);
5569       tree zero = build_int_cst (elem_type, 0);
5570       tree zero_vec = build_vector_from_val (vectype, zero);
5571       tree cmp_type = truth_type_for (vectype);
5572       ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5573     }
5574
5575   if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5576     ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5577   else
5578     {
5579       if (int_operands)
5580         {
5581           /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5582              nested inside of the expression.  */
5583           op1 = c_fully_fold (op1, false, NULL);
5584           op2 = c_fully_fold (op2, false, NULL);
5585         }
5586       ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5587       if (int_operands)
5588         ret = note_integer_operands (ret);
5589     }
5590   if (semantic_result_type)
5591     ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5592
5593   protected_set_expr_location (ret, colon_loc);
5594
5595   /* If the OP1 and OP2 are the same and don't have side-effects,
5596      warn here, because the COND_EXPR will be turned into OP1.  */
5597   if (warn_duplicated_branches
5598       && TREE_CODE (ret) == COND_EXPR
5599       && (op1 == op2 || operand_equal_p (op1, op2, OEP_ADDRESS_OF_SAME_FIELD)))
5600     warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5601                 "this condition has identical branches");
5602
5603   return ret;
5604 }
5605 \f
5606 /* EXPR is an expression, location LOC, whose result is discarded.
5607    Warn if it is a call to a nodiscard function (or a COMPOUND_EXPR
5608    whose right-hand operand is such a call, possibly recursively).  */
5609
5610 static void
5611 maybe_warn_nodiscard (location_t loc, tree expr)
5612 {
5613   if (VOID_TYPE_P (TREE_TYPE (expr)))
5614     return;
5615   while (TREE_CODE (expr) == COMPOUND_EXPR)
5616     {
5617       expr = TREE_OPERAND (expr, 1);
5618       if (EXPR_HAS_LOCATION (expr))
5619         loc = EXPR_LOCATION (expr);
5620     }
5621   if (TREE_CODE (expr) != CALL_EXPR)
5622     return;
5623   tree fn = CALL_EXPR_FN (expr);
5624   if (!fn)
5625     return;
5626   tree attr;
5627   if (TREE_CODE (fn) == ADDR_EXPR
5628       && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5629       && (attr = lookup_attribute ("nodiscard",
5630                                    DECL_ATTRIBUTES (TREE_OPERAND (fn, 0)))))
5631     {
5632       fn = TREE_OPERAND (fn, 0);
5633       tree args = TREE_VALUE (attr);
5634       if (args)
5635         args = TREE_VALUE (args);
5636       auto_diagnostic_group d;
5637       int warned;
5638       if (args)
5639         warned = warning_at (loc, OPT_Wunused_result,
5640                              "ignoring return value of %qD, declared with "
5641                              "attribute %<nodiscard%>: %E", fn, args);
5642       else
5643         warned = warning_at (loc, OPT_Wunused_result,
5644                              "ignoring return value of %qD, declared with "
5645                              "attribute %<nodiscard%>", fn);
5646       if (warned)
5647         inform (DECL_SOURCE_LOCATION (fn), "declared here");
5648     }
5649   else
5650     {
5651       tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
5652       attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype));
5653       if (!attr)
5654         return;
5655       tree args = TREE_VALUE (attr);
5656       if (args)
5657         args = TREE_VALUE (args);
5658       auto_diagnostic_group d;
5659       int warned;
5660       if (args)
5661         warned = warning_at (loc, OPT_Wunused_result,
5662                              "ignoring return value of type %qT, declared "
5663                              "with attribute %<nodiscard%>: %E",
5664                              rettype, args);
5665       else
5666         warned = warning_at (loc, OPT_Wunused_result,
5667                              "ignoring return value of type %qT, declared "
5668                              "with attribute %<nodiscard%>", rettype);
5669       if (warned)
5670         {
5671           if (TREE_CODE (fn) == ADDR_EXPR)
5672             {
5673               fn = TREE_OPERAND (fn, 0);
5674               if (TREE_CODE (fn) == FUNCTION_DECL)
5675                 inform (DECL_SOURCE_LOCATION (fn),
5676                         "in call to %qD, declared here", fn);
5677             }
5678         }
5679     }
5680 }
5681
5682 /* Return a compound expression that performs two expressions and
5683    returns the value of the second of them.
5684
5685    LOC is the location of the COMPOUND_EXPR.  */
5686
5687 tree
5688 build_compound_expr (location_t loc, tree expr1, tree expr2)
5689 {
5690   bool expr1_int_operands, expr2_int_operands;
5691   tree eptype = NULL_TREE;
5692   tree ret;
5693
5694   expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5695   if (expr1_int_operands)
5696     expr1 = remove_c_maybe_const_expr (expr1);
5697   expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5698   if (expr2_int_operands)
5699     expr2 = remove_c_maybe_const_expr (expr2);
5700
5701   if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5702     expr1 = TREE_OPERAND (expr1, 0);
5703   if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5704     {
5705       eptype = TREE_TYPE (expr2);
5706       expr2 = TREE_OPERAND (expr2, 0);
5707     }
5708
5709   if (!TREE_SIDE_EFFECTS (expr1))
5710     {
5711       /* The left-hand operand of a comma expression is like an expression
5712          statement: with -Wunused, we should warn if it doesn't have
5713          any side-effects, unless it was explicitly cast to (void).  */
5714       if (warn_unused_value)
5715         {
5716           if (VOID_TYPE_P (TREE_TYPE (expr1))
5717               && CONVERT_EXPR_P (expr1))
5718             ; /* (void) a, b */
5719           else if (VOID_TYPE_P (TREE_TYPE (expr1))
5720                    && TREE_CODE (expr1) == COMPOUND_EXPR
5721                    && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5722             ; /* (void) a, (void) b, c */
5723           else
5724             warning_at (loc, OPT_Wunused_value,
5725                         "left-hand operand of comma expression has no effect");
5726         }
5727     }
5728   else if (TREE_CODE (expr1) == COMPOUND_EXPR
5729            && warn_unused_value)
5730     {
5731       tree r = expr1;
5732       location_t cloc = loc;
5733       while (TREE_CODE (r) == COMPOUND_EXPR)
5734         {
5735           if (EXPR_HAS_LOCATION (r))
5736             cloc = EXPR_LOCATION (r);
5737           r = TREE_OPERAND (r, 1);
5738         }
5739       if (!TREE_SIDE_EFFECTS (r)
5740           && !VOID_TYPE_P (TREE_TYPE (r))
5741           && !CONVERT_EXPR_P (r))
5742         warning_at (cloc, OPT_Wunused_value,
5743                     "right-hand operand of comma expression has no effect");
5744     }
5745
5746   /* With -Wunused, we should also warn if the left-hand operand does have
5747      side-effects, but computes a value which is not used.  For example, in
5748      `foo() + bar(), baz()' the result of the `+' operator is not used,
5749      so we should issue a warning.  */
5750   else if (warn_unused_value)
5751     warn_if_unused_value (expr1, loc);
5752
5753   maybe_warn_nodiscard (loc, expr1);
5754
5755   if (expr2 == error_mark_node)
5756     return error_mark_node;
5757
5758   ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5759
5760   if (flag_isoc99
5761       && expr1_int_operands
5762       && expr2_int_operands)
5763     ret = note_integer_operands (ret);
5764
5765   if (eptype)
5766     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5767
5768   protected_set_expr_location (ret, loc);
5769   return ret;
5770 }
5771
5772 /* Issue -Wcast-qual warnings when appropriate.  TYPE is the type to
5773    which we are casting.  OTYPE is the type of the expression being
5774    cast.  Both TYPE and OTYPE are pointer types.  LOC is the location
5775    of the cast.  -Wcast-qual appeared on the command line.  Named
5776    address space qualifiers are not handled here, because they result
5777    in different warnings.  */
5778
5779 static void
5780 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5781 {
5782   tree in_type = type;
5783   tree in_otype = otype;
5784   int added = 0;
5785   int discarded = 0;
5786   bool is_const;
5787
5788   /* Check that the qualifiers on IN_TYPE are a superset of the
5789      qualifiers of IN_OTYPE.  The outermost level of POINTER_TYPE
5790      nodes is uninteresting and we stop as soon as we hit a
5791      non-POINTER_TYPE node on either type.  */
5792   do
5793     {
5794       in_otype = TREE_TYPE (in_otype);
5795       in_type = TREE_TYPE (in_type);
5796
5797       /* GNU C allows cv-qualified function types.  'const' means the
5798          function is very pure, 'volatile' means it can't return.  We
5799          need to warn when such qualifiers are added, not when they're
5800          taken away.  */
5801       if (TREE_CODE (in_otype) == FUNCTION_TYPE
5802           && TREE_CODE (in_type) == FUNCTION_TYPE)
5803         added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5804                   & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5805       else
5806         discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5807                       & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5808     }
5809   while (TREE_CODE (in_type) == POINTER_TYPE
5810          && TREE_CODE (in_otype) == POINTER_TYPE);
5811
5812   if (added)
5813     warning_at (loc, OPT_Wcast_qual,
5814                 "cast adds %q#v qualifier to function type", added);
5815
5816   if (discarded)
5817     /* There are qualifiers present in IN_OTYPE that are not present
5818        in IN_TYPE.  */
5819     warning_at (loc, OPT_Wcast_qual,
5820                 "cast discards %qv qualifier from pointer target type",
5821                 discarded);
5822
5823   if (added || discarded)
5824     return;
5825
5826   /* A cast from **T to const **T is unsafe, because it can cause a
5827      const value to be changed with no additional warning.  We only
5828      issue this warning if T is the same on both sides, and we only
5829      issue the warning if there are the same number of pointers on
5830      both sides, as otherwise the cast is clearly unsafe anyhow.  A
5831      cast is unsafe when a qualifier is added at one level and const
5832      is not present at all outer levels.
5833
5834      To issue this warning, we check at each level whether the cast
5835      adds new qualifiers not already seen.  We don't need to special
5836      case function types, as they won't have the same
5837      TYPE_MAIN_VARIANT.  */
5838
5839   if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5840     return;
5841   if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5842     return;
5843
5844   in_type = type;
5845   in_otype = otype;
5846   is_const = TYPE_READONLY (TREE_TYPE (in_type));
5847   do
5848     {
5849       in_type = TREE_TYPE (in_type);
5850       in_otype = TREE_TYPE (in_otype);
5851       if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5852           && !is_const)
5853         {
5854           warning_at (loc, OPT_Wcast_qual,
5855                       "to be safe all intermediate pointers in cast from "
5856                       "%qT to %qT must be %<const%> qualified",
5857                       otype, type);
5858           break;
5859         }
5860       if (is_const)
5861         is_const = TYPE_READONLY (in_type);
5862     }
5863   while (TREE_CODE (in_type) == POINTER_TYPE);
5864 }
5865
5866 /* Heuristic check if two parameter types can be considered ABI-equivalent.  */
5867
5868 static bool
5869 c_safe_arg_type_equiv_p (tree t1, tree t2)
5870 {
5871   t1 = TYPE_MAIN_VARIANT (t1);
5872   t2 = TYPE_MAIN_VARIANT (t2);
5873
5874   if (TREE_CODE (t1) == POINTER_TYPE
5875       && TREE_CODE (t2) == POINTER_TYPE)
5876     return true;
5877
5878   /* The signedness of the parameter matters only when an integral
5879      type smaller than int is promoted to int, otherwise only the
5880      precision of the parameter matters.
5881      This check should make sure that the callee does not see
5882      undefined values in argument registers.  */
5883   if (INTEGRAL_TYPE_P (t1)
5884       && INTEGRAL_TYPE_P (t2)
5885       && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5886       && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5887           || !targetm.calls.promote_prototypes (NULL_TREE)
5888           || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5889     return true;
5890
5891   return comptypes (t1, t2);
5892 }
5893
5894 /* Check if a type cast between two function types can be considered safe.  */
5895
5896 static bool
5897 c_safe_function_type_cast_p (tree t1, tree t2)
5898 {
5899   if (TREE_TYPE (t1) == void_type_node &&
5900       TYPE_ARG_TYPES (t1) == void_list_node)
5901     return true;
5902
5903   if (TREE_TYPE (t2) == void_type_node &&
5904       TYPE_ARG_TYPES (t2) == void_list_node)
5905     return true;
5906
5907   if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5908     return false;
5909
5910   for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5911        t1 && t2;
5912        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5913     if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5914       return false;
5915
5916   return true;
5917 }
5918
5919 /* Build an expression representing a cast to type TYPE of expression EXPR.
5920    LOC is the location of the cast-- typically the open paren of the cast.  */
5921
5922 tree
5923 build_c_cast (location_t loc, tree type, tree expr)
5924 {
5925   tree value;
5926
5927   bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
5928
5929   if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5930     expr = TREE_OPERAND (expr, 0);
5931
5932   value = expr;
5933   if (int_operands)
5934     value = remove_c_maybe_const_expr (value);
5935
5936   if (type == error_mark_node || expr == error_mark_node)
5937     return error_mark_node;
5938
5939   /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5940      only in <protocol> qualifications.  But when constructing cast expressions,
5941      the protocols do matter and must be kept around.  */
5942   if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5943     return build1 (NOP_EXPR, type, expr);
5944
5945   type = TYPE_MAIN_VARIANT (type);
5946
5947   if (TREE_CODE (type) == ARRAY_TYPE)
5948     {
5949       error_at (loc, "cast specifies array type");
5950       return error_mark_node;
5951     }
5952
5953   if (TREE_CODE (type) == FUNCTION_TYPE)
5954     {
5955       error_at (loc, "cast specifies function type");
5956       return error_mark_node;
5957     }
5958
5959   if (!VOID_TYPE_P (type))
5960     {
5961       value = require_complete_type (loc, value);
5962       if (value == error_mark_node)
5963         return error_mark_node;
5964     }
5965
5966   if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5967     {
5968       if (RECORD_OR_UNION_TYPE_P (type))
5969         pedwarn (loc, OPT_Wpedantic,
5970                  "ISO C forbids casting nonscalar to the same type");
5971
5972       /* Convert to remove any qualifiers from VALUE's type.  */
5973       value = convert (type, value);
5974     }
5975   else if (TREE_CODE (type) == UNION_TYPE)
5976     {
5977       tree field;
5978
5979       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5980         if (TREE_TYPE (field) != error_mark_node
5981             && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5982                           TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5983           break;
5984
5985       if (field)
5986         {
5987           tree t;
5988           bool maybe_const = true;
5989
5990           pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5991           t = c_fully_fold (value, false, &maybe_const);
5992           t = build_constructor_single (type, field, t);
5993           if (!maybe_const)
5994             t = c_wrap_maybe_const (t, true);
5995           t = digest_init (loc, type, t,
5996                            NULL_TREE, false, true, 0);
5997           TREE_CONSTANT (t) = TREE_CONSTANT (value);
5998           return t;
5999         }
6000       error_at (loc, "cast to union type from type not present in union");
6001       return error_mark_node;
6002     }
6003   else
6004     {
6005       tree otype, ovalue;
6006
6007       if (type == void_type_node)
6008         {
6009           tree t = build1 (CONVERT_EXPR, type, value);
6010           SET_EXPR_LOCATION (t, loc);
6011           return t;
6012         }
6013
6014       otype = TREE_TYPE (value);
6015
6016       /* Optionally warn about potentially worrisome casts.  */
6017       if (warn_cast_qual
6018           && TREE_CODE (type) == POINTER_TYPE
6019           && TREE_CODE (otype) == POINTER_TYPE)
6020         handle_warn_cast_qual (loc, type, otype);
6021
6022       /* Warn about conversions between pointers to disjoint
6023          address spaces.  */
6024       if (TREE_CODE (type) == POINTER_TYPE
6025           && TREE_CODE (otype) == POINTER_TYPE
6026           && !null_pointer_constant_p (value))
6027         {
6028           addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
6029           addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
6030           addr_space_t as_common;
6031
6032           if (!addr_space_superset (as_to, as_from, &as_common))
6033             {
6034               if (ADDR_SPACE_GENERIC_P (as_from))
6035                 warning_at (loc, 0, "cast to %qs address space pointer "
6036                             "from disjoint generic address space pointer",
6037                             c_addr_space_name (as_to));
6038
6039               else if (ADDR_SPACE_GENERIC_P (as_to))
6040                 warning_at (loc, 0, "cast to generic address space pointer "
6041                             "from disjoint %qs address space pointer",
6042                             c_addr_space_name (as_from));
6043
6044               else
6045                 warning_at (loc, 0, "cast to %qs address space pointer "
6046                             "from disjoint %qs address space pointer",
6047                             c_addr_space_name (as_to),
6048                             c_addr_space_name (as_from));
6049             }
6050         }
6051
6052       /* Warn about possible alignment problems.  */
6053       if ((STRICT_ALIGNMENT || warn_cast_align == 2)
6054           && TREE_CODE (type) == POINTER_TYPE
6055           && TREE_CODE (otype) == POINTER_TYPE
6056           && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
6057           && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6058           /* Don't warn about opaque types, where the actual alignment
6059              restriction is unknown.  */
6060           && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
6061                && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
6062           && min_align_of_type (TREE_TYPE (type))
6063              > min_align_of_type (TREE_TYPE (otype)))
6064         warning_at (loc, OPT_Wcast_align,
6065                     "cast increases required alignment of target type");
6066
6067       if (TREE_CODE (type) == INTEGER_TYPE
6068           && TREE_CODE (otype) == POINTER_TYPE
6069           && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
6070       /* Unlike conversion of integers to pointers, where the
6071          warning is disabled for converting constants because
6072          of cases such as SIG_*, warn about converting constant
6073          pointers to integers. In some cases it may cause unwanted
6074          sign extension, and a warning is appropriate.  */
6075         warning_at (loc, OPT_Wpointer_to_int_cast,
6076                     "cast from pointer to integer of different size");
6077
6078       if (TREE_CODE (value) == CALL_EXPR
6079           && TREE_CODE (type) != TREE_CODE (otype))
6080         warning_at (loc, OPT_Wbad_function_cast,
6081                     "cast from function call of type %qT "
6082                     "to non-matching type %qT", otype, type);
6083
6084       if (TREE_CODE (type) == POINTER_TYPE
6085           && TREE_CODE (otype) == INTEGER_TYPE
6086           && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
6087           /* Don't warn about converting any constant.  */
6088           && !TREE_CONSTANT (value))
6089         warning_at (loc,
6090                     OPT_Wint_to_pointer_cast, "cast to pointer from integer "
6091                     "of different size");
6092
6093       if (warn_strict_aliasing <= 2)
6094         strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
6095
6096       /* If pedantic, warn for conversions between function and object
6097          pointer types, except for converting a null pointer constant
6098          to function pointer type.  */
6099       if (pedantic
6100           && TREE_CODE (type) == POINTER_TYPE
6101           && TREE_CODE (otype) == POINTER_TYPE
6102           && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6103           && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
6104         pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6105                  "conversion of function pointer to object pointer type");
6106
6107       if (pedantic
6108           && TREE_CODE (type) == POINTER_TYPE
6109           && TREE_CODE (otype) == POINTER_TYPE
6110           && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6111           && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6112           && !null_pointer_constant_p (value))
6113         pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
6114                  "conversion of object pointer to function pointer type");
6115
6116       if (TREE_CODE (type) == POINTER_TYPE
6117           && TREE_CODE (otype) == POINTER_TYPE
6118           && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6119           && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
6120           && !c_safe_function_type_cast_p (TREE_TYPE (type),
6121                                            TREE_TYPE (otype)))
6122         warning_at (loc, OPT_Wcast_function_type,
6123                     "cast between incompatible function types"
6124                     " from %qT to %qT", otype, type);
6125
6126       ovalue = value;
6127       value = convert (type, value);
6128
6129       /* Ignore any integer overflow caused by the cast.  */
6130       if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6131         {
6132           if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6133             {
6134               if (!TREE_OVERFLOW (value))
6135                 {
6136                   /* Avoid clobbering a shared constant.  */
6137                   value = copy_node (value);
6138                   TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6139                 }
6140             }
6141           else if (TREE_OVERFLOW (value))
6142             /* Reset VALUE's overflow flags, ensuring constant sharing.  */
6143             value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6144         }
6145     }
6146
6147   /* Don't let a cast be an lvalue.  */
6148   if (lvalue_p (value))
6149     value = non_lvalue_loc (loc, value);
6150
6151   /* Don't allow the results of casting to floating-point or complex
6152      types be confused with actual constants, or casts involving
6153      integer and pointer types other than direct integer-to-integer
6154      and integer-to-pointer be confused with integer constant
6155      expressions and null pointer constants.  */
6156   if (TREE_CODE (value) == REAL_CST
6157       || TREE_CODE (value) == COMPLEX_CST
6158       || (TREE_CODE (value) == INTEGER_CST
6159           && !((TREE_CODE (expr) == INTEGER_CST
6160                 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6161                || TREE_CODE (expr) == REAL_CST
6162                || TREE_CODE (expr) == COMPLEX_CST)))
6163       value = build1 (NOP_EXPR, type, value);
6164
6165   /* If the expression has integer operands and so can occur in an
6166      unevaluated part of an integer constant expression, ensure the
6167      return value reflects this.  */
6168   if (int_operands
6169       && INTEGRAL_TYPE_P (type)
6170       && value != error_mark_node
6171       && !EXPR_INT_CONST_OPERANDS (value))
6172     value = note_integer_operands (value);
6173
6174   protected_set_expr_location (value, loc);
6175   return value;
6176 }
6177
6178 /* Interpret a cast of expression EXPR to type TYPE.  LOC is the
6179    location of the open paren of the cast, or the position of the cast
6180    expr.  */
6181 tree
6182 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6183 {
6184   tree type;
6185   tree type_expr = NULL_TREE;
6186   bool type_expr_const = true;
6187   tree ret;
6188   int saved_wsp = warn_strict_prototypes;
6189
6190   /* This avoids warnings about unprototyped casts on
6191      integers.  E.g. "#define SIG_DFL (void(*)())0".  */
6192   if (TREE_CODE (expr) == INTEGER_CST)
6193     warn_strict_prototypes = 0;
6194   type = groktypename (type_name, &type_expr, &type_expr_const);
6195   warn_strict_prototypes = saved_wsp;
6196
6197   if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6198       && reject_gcc_builtin (expr))
6199     return error_mark_node;
6200
6201   ret = build_c_cast (loc, type, expr);
6202   if (type_expr)
6203     {
6204       bool inner_expr_const = true;
6205       ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6206       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6207       C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6208                                              && inner_expr_const);
6209       SET_EXPR_LOCATION (ret, loc);
6210     }
6211
6212   if (!EXPR_HAS_LOCATION (ret))
6213     protected_set_expr_location (ret, loc);
6214
6215   /* C++ does not permits types to be defined in a cast, but it
6216      allows references to incomplete types.  */
6217   if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6218     warning_at (loc, OPT_Wc___compat,
6219                 "defining a type in a cast is invalid in C++");
6220
6221   return ret;
6222 }
6223 \f
6224 /* Build an assignment expression of lvalue LHS from value RHS.
6225    If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6226    may differ from TREE_TYPE (LHS) for an enum bitfield.
6227    MODIFYCODE is the code for a binary operator that we use
6228    to combine the old value of LHS with RHS to get the new value.
6229    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6230    If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6231    which may differ from TREE_TYPE (RHS) for an enum value.
6232
6233    LOCATION is the location of the MODIFYCODE operator.
6234    RHS_LOC is the location of the RHS.  */
6235
6236 tree
6237 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6238                    enum tree_code modifycode,
6239                    location_t rhs_loc, tree rhs, tree rhs_origtype)
6240 {
6241   tree result;
6242   tree newrhs;
6243   tree rhseval = NULL_TREE;
6244   tree lhstype = TREE_TYPE (lhs);
6245   tree olhstype = lhstype;
6246   bool npc;
6247   bool is_atomic_op;
6248
6249   /* Types that aren't fully specified cannot be used in assignments.  */
6250   lhs = require_complete_type (location, lhs);
6251
6252   /* Avoid duplicate error messages from operands that had errors.  */
6253   if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6254     return error_mark_node;
6255
6256   /* Ensure an error for assigning a non-lvalue array to an array in
6257      C90.  */
6258   if (TREE_CODE (lhstype) == ARRAY_TYPE)
6259     {
6260       error_at (location, "assignment to expression with array type");
6261       return error_mark_node;
6262     }
6263
6264   /* For ObjC properties, defer this check.  */
6265   if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6266     return error_mark_node;
6267
6268   is_atomic_op = really_atomic_lvalue (lhs);
6269
6270   newrhs = rhs;
6271
6272   if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6273     {
6274       tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6275                                       lhs_origtype, modifycode, rhs_loc, rhs,
6276                                       rhs_origtype);
6277       if (inner == error_mark_node)
6278         return error_mark_node;
6279       result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6280                        C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6281       gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6282       C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6283       protected_set_expr_location (result, location);
6284       return result;
6285     }
6286
6287   /* If a binary op has been requested, combine the old LHS value with the RHS
6288      producing the value we should actually store into the LHS.  */
6289
6290   if (modifycode != NOP_EXPR)
6291     {
6292       lhs = c_fully_fold (lhs, false, NULL, true);
6293       lhs = stabilize_reference (lhs);
6294
6295       /* Construct the RHS for any non-atomic compound assignemnt. */
6296       if (!is_atomic_op)
6297         {
6298           /* If in LHS op= RHS the RHS has side-effects, ensure they
6299              are preevaluated before the rest of the assignment expression's
6300              side-effects, because RHS could contain e.g. function calls
6301              that modify LHS.  */
6302           if (TREE_SIDE_EFFECTS (rhs))
6303             {
6304               if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6305                 newrhs = save_expr (TREE_OPERAND (rhs, 0));
6306               else
6307                 newrhs = save_expr (rhs);
6308               rhseval = newrhs;
6309               if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6310                 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6311                                  newrhs);
6312             }
6313           newrhs = build_binary_op (location,
6314                                     modifycode, lhs, newrhs, true);
6315
6316           /* The original type of the right hand side is no longer
6317              meaningful.  */
6318           rhs_origtype = NULL_TREE;
6319         }
6320     }
6321
6322   if (c_dialect_objc ())
6323     {
6324       /* Check if we are modifying an Objective-C property reference;
6325          if so, we need to generate setter calls.  */
6326       if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6327         result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6328       else
6329         result = objc_maybe_build_modify_expr (lhs, newrhs);
6330       if (result)
6331         goto return_result;
6332
6333       /* Else, do the check that we postponed for Objective-C.  */
6334       if (!lvalue_or_else (location, lhs, lv_assign))
6335         return error_mark_node;
6336     }
6337
6338   /* Give an error for storing in something that is 'const'.  */
6339
6340   if (TYPE_READONLY (lhstype)
6341       || (RECORD_OR_UNION_TYPE_P (lhstype)
6342           && C_TYPE_FIELDS_READONLY (lhstype)))
6343     {
6344       readonly_error (location, lhs, lv_assign);
6345       return error_mark_node;
6346     }
6347   else if (TREE_READONLY (lhs))
6348     readonly_warning (lhs, lv_assign);
6349
6350   /* If storing into a structure or union member,
6351      it has probably been given type `int'.
6352      Compute the type that would go with
6353      the actual amount of storage the member occupies.  */
6354
6355   if (TREE_CODE (lhs) == COMPONENT_REF
6356       && (TREE_CODE (lhstype) == INTEGER_TYPE
6357           || TREE_CODE (lhstype) == BOOLEAN_TYPE
6358           || TREE_CODE (lhstype) == REAL_TYPE
6359           || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6360     lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6361
6362   /* If storing in a field that is in actuality a short or narrower than one,
6363      we must store in the field in its actual type.  */
6364
6365   if (lhstype != TREE_TYPE (lhs))
6366     {
6367       lhs = copy_node (lhs);
6368       TREE_TYPE (lhs) = lhstype;
6369     }
6370
6371   /* Issue -Wc++-compat warnings about an assignment to an enum type
6372      when LHS does not have its original type.  This happens for,
6373      e.g., an enum bitfield in a struct.  */
6374   if (warn_cxx_compat
6375       && lhs_origtype != NULL_TREE
6376       && lhs_origtype != lhstype
6377       && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6378     {
6379       tree checktype = (rhs_origtype != NULL_TREE
6380                         ? rhs_origtype
6381                         : TREE_TYPE (rhs));
6382       if (checktype != error_mark_node
6383           && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6384               || (is_atomic_op && modifycode != NOP_EXPR)))
6385         warning_at (location, OPT_Wc___compat,
6386                     "enum conversion in assignment is invalid in C++");
6387     }
6388
6389   /* Remove qualifiers.  */
6390   lhstype = build_qualified_type (lhstype, TYPE_UNQUALIFIED);
6391   olhstype = build_qualified_type (olhstype, TYPE_UNQUALIFIED);
6392
6393   /* Convert new value to destination type.  Fold it first, then
6394      restore any excess precision information, for the sake of
6395      conversion warnings.  */
6396
6397   if (!(is_atomic_op && modifycode != NOP_EXPR))
6398     {
6399       tree rhs_semantic_type = NULL_TREE;
6400       if (!c_in_omp_for)
6401         {
6402           if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6403             {
6404               rhs_semantic_type = TREE_TYPE (newrhs);
6405               newrhs = TREE_OPERAND (newrhs, 0);
6406             }
6407           npc = null_pointer_constant_p (newrhs);
6408           newrhs = c_fully_fold (newrhs, false, NULL);
6409           if (rhs_semantic_type)
6410             newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6411         }
6412       else
6413         npc = null_pointer_constant_p (newrhs);
6414       newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6415                                        rhs_origtype, ic_assign, npc,
6416                                        NULL_TREE, NULL_TREE, 0);
6417       if (TREE_CODE (newrhs) == ERROR_MARK)
6418         return error_mark_node;
6419     }
6420
6421   /* Emit ObjC write barrier, if necessary.  */
6422   if (c_dialect_objc () && flag_objc_gc)
6423     {
6424       result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6425       if (result)
6426         {
6427           protected_set_expr_location (result, location);
6428           goto return_result;
6429         }
6430     }
6431
6432   /* Scan operands.  */
6433
6434   if (is_atomic_op)
6435     result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6436   else
6437     {
6438       result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6439       TREE_SIDE_EFFECTS (result) = 1;
6440       protected_set_expr_location (result, location);
6441     }
6442
6443   /* If we got the LHS in a different type for storing in,
6444      convert the result back to the nominal type of LHS
6445      so that the value we return always has the same type
6446      as the LHS argument.  */
6447
6448   if (olhstype == TREE_TYPE (result))
6449     goto return_result;
6450
6451   result = convert_for_assignment (location, rhs_loc, olhstype, result,
6452                                    rhs_origtype, ic_assign, false, NULL_TREE,
6453                                    NULL_TREE, 0);
6454   protected_set_expr_location (result, location);
6455
6456 return_result:
6457   if (rhseval)
6458     result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6459   return result;
6460 }
6461 \f
6462 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6463    This is used to implement -fplan9-extensions.  */
6464
6465 static bool
6466 find_anonymous_field_with_type (tree struct_type, tree type)
6467 {
6468   tree field;
6469   bool found;
6470
6471   gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6472   found = false;
6473   for (field = TYPE_FIELDS (struct_type);
6474        field != NULL_TREE;
6475        field = TREE_CHAIN (field))
6476     {
6477       tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6478                         ? c_build_qualified_type (TREE_TYPE (field),
6479                                                   TYPE_QUAL_ATOMIC)
6480                         : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6481       if (DECL_NAME (field) == NULL
6482           && comptypes (type, fieldtype))
6483         {
6484           if (found)
6485             return false;
6486           found = true;
6487         }
6488       else if (DECL_NAME (field) == NULL
6489                && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6490                && find_anonymous_field_with_type (TREE_TYPE (field), type))
6491         {
6492           if (found)
6493             return false;
6494           found = true;
6495         }
6496     }
6497   return found;
6498 }
6499
6500 /* RHS is an expression whose type is pointer to struct.  If there is
6501    an anonymous field in RHS with type TYPE, then return a pointer to
6502    that field in RHS.  This is used with -fplan9-extensions.  This
6503    returns NULL if no conversion could be found.  */
6504
6505 static tree
6506 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6507 {
6508   tree rhs_struct_type, lhs_main_type;
6509   tree field, found_field;
6510   bool found_sub_field;
6511   tree ret;
6512
6513   gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6514   rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6515   gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6516
6517   gcc_assert (POINTER_TYPE_P (type));
6518   lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6519                    ? c_build_qualified_type (TREE_TYPE (type),
6520                                              TYPE_QUAL_ATOMIC)
6521                    : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6522
6523   found_field = NULL_TREE;
6524   found_sub_field = false;
6525   for (field = TYPE_FIELDS (rhs_struct_type);
6526        field != NULL_TREE;
6527        field = TREE_CHAIN (field))
6528     {
6529       if (DECL_NAME (field) != NULL_TREE
6530           || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6531         continue;
6532       tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6533                         ? c_build_qualified_type (TREE_TYPE (field),
6534                                                   TYPE_QUAL_ATOMIC)
6535                         : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6536       if (comptypes (lhs_main_type, fieldtype))
6537         {
6538           if (found_field != NULL_TREE)
6539             return NULL_TREE;
6540           found_field = field;
6541         }
6542       else if (find_anonymous_field_with_type (TREE_TYPE (field),
6543                                                lhs_main_type))
6544         {
6545           if (found_field != NULL_TREE)
6546             return NULL_TREE;
6547           found_field = field;
6548           found_sub_field = true;
6549         }
6550     }
6551
6552   if (found_field == NULL_TREE)
6553     return NULL_TREE;
6554
6555   ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6556                          build_fold_indirect_ref (rhs), found_field,
6557                          NULL_TREE);
6558   ret = build_fold_addr_expr_loc (location, ret);
6559
6560   if (found_sub_field)
6561     {
6562       ret = convert_to_anonymous_field (location, type, ret);
6563       gcc_assert (ret != NULL_TREE);
6564     }
6565
6566   return ret;
6567 }
6568
6569 /* Issue an error message for a bad initializer component.
6570    GMSGID identifies the message.
6571    The component name is taken from the spelling stack.  */
6572
6573 static void ATTRIBUTE_GCC_DIAG (2,0)
6574 error_init (location_t loc, const char *gmsgid, ...)
6575 {
6576   char *ofwhat;
6577
6578   auto_diagnostic_group d;
6579
6580   /* The gmsgid may be a format string with %< and %>. */
6581   va_list ap;
6582   va_start (ap, gmsgid);
6583   bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6584   va_end (ap);
6585
6586   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6587   if (*ofwhat && warned)
6588     inform (loc, "(near initialization for %qs)", ofwhat);
6589 }
6590
6591 /* Issue a pedantic warning for a bad initializer component.  OPT is
6592    the option OPT_* (from options.h) controlling this warning or 0 if
6593    it is unconditionally given.  GMSGID identifies the message.  The
6594    component name is taken from the spelling stack.  */
6595
6596 static void ATTRIBUTE_GCC_DIAG (3,0)
6597 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6598 {
6599   /* Use the location where a macro was expanded rather than where
6600      it was defined to make sure macros defined in system headers
6601      but used incorrectly elsewhere are diagnosed.  */
6602   location_t exploc = expansion_point_location_if_in_system_header (loc);
6603   auto_diagnostic_group d;
6604   va_list ap;
6605   va_start (ap, gmsgid);
6606   bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6607   va_end (ap);
6608   char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6609   if (*ofwhat && warned)
6610     inform (exploc, "(near initialization for %qs)", ofwhat);
6611 }
6612
6613 /* Issue a warning for a bad initializer component.
6614
6615    OPT is the OPT_W* value corresponding to the warning option that
6616    controls this warning.  GMSGID identifies the message.  The
6617    component name is taken from the spelling stack.  */
6618
6619 static void
6620 warning_init (location_t loc, int opt, const char *gmsgid)
6621 {
6622   char *ofwhat;
6623   bool warned;
6624
6625   auto_diagnostic_group d;
6626
6627   /* Use the location where a macro was expanded rather than where
6628      it was defined to make sure macros defined in system headers
6629      but used incorrectly elsewhere are diagnosed.  */
6630   location_t exploc = expansion_point_location_if_in_system_header (loc);
6631
6632   /* The gmsgid may be a format string with %< and %>. */
6633   warned = warning_at (exploc, opt, gmsgid);
6634   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6635   if (*ofwhat && warned)
6636     inform (exploc, "(near initialization for %qs)", ofwhat);
6637 }
6638 \f
6639 /* If TYPE is an array type and EXPR is a parenthesized string
6640    constant, warn if pedantic that EXPR is being used to initialize an
6641    object of type TYPE.  */
6642
6643 void
6644 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6645 {
6646   if (pedantic
6647       && TREE_CODE (type) == ARRAY_TYPE
6648       && TREE_CODE (expr.value) == STRING_CST
6649       && expr.original_code != STRING_CST)
6650     pedwarn_init (loc, OPT_Wpedantic,
6651                   "array initialized from parenthesized string constant");
6652 }
6653
6654 /* Attempt to locate the parameter with the given index within FNDECL,
6655    returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found.  */
6656
6657 static location_t
6658 get_fndecl_argument_location (tree fndecl, int argnum)
6659 {
6660   int i;
6661   tree param;
6662
6663   /* Locate param by index within DECL_ARGUMENTS (fndecl).  */
6664   for (i = 0, param = DECL_ARGUMENTS (fndecl);
6665        i < argnum && param;
6666        i++, param = TREE_CHAIN (param))
6667     ;
6668
6669   /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6670      return DECL_SOURCE_LOCATION (FNDECL).  */
6671   if (param == NULL)
6672     return DECL_SOURCE_LOCATION (fndecl);
6673
6674   return DECL_SOURCE_LOCATION (param);
6675 }
6676
6677 /* Issue a note about a mismatching argument for parameter PARMNUM
6678    to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6679    Attempt to issue the note at the pertinent parameter of the decl;
6680    failing that issue it at the location of FUNDECL; failing that
6681    issue it at PLOC.  */
6682
6683 static void
6684 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6685                 tree expected_type, tree actual_type)
6686 {
6687   location_t loc;
6688   if (fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6689     loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6690   else
6691     loc = ploc;
6692
6693   inform (loc,
6694           "expected %qT but argument is of type %qT",
6695           expected_type, actual_type);
6696 }
6697
6698 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6699    function FUNDECL declared without prototype to parameter PARMNUM of
6700    PARMTYPE when ARGTYPE does not promote to PARMTYPE.  */
6701
6702 static void
6703 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6704                                  tree parmtype, tree argtype)
6705 {
6706   tree_code parmcode = TREE_CODE (parmtype);
6707   tree_code argcode = TREE_CODE (argtype);
6708   tree promoted = c_type_promotes_to (argtype);
6709
6710   /* Avoid warning for enum arguments that promote to an integer type
6711      of the same size/mode.  */
6712   if (parmcode == INTEGER_TYPE
6713       && argcode == ENUMERAL_TYPE
6714       && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6715     return;
6716
6717   if ((parmcode == argcode
6718        || (parmcode == INTEGER_TYPE
6719            && argcode == ENUMERAL_TYPE))
6720       && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6721     return;
6722
6723   /* This diagnoses even signed/unsigned mismatches.  Those might be
6724      safe in many cases but GCC may emit suboptimal code for them so
6725      warning on those cases drives efficiency improvements.  */
6726   if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6727                   TYPE_MAIN_VARIANT (promoted) == argtype
6728                   ? G_("%qD argument %d type is %qT where %qT is expected "
6729                        "in a call to built-in function declared without "
6730                        "prototype")
6731                   : G_("%qD argument %d promotes to %qT where %qT is expected "
6732                        "in a call to built-in function declared without "
6733                        "prototype"),
6734                   fundecl, parmnum, promoted, parmtype))
6735     inform (DECL_SOURCE_LOCATION (fundecl),
6736             "built-in %qD declared here",
6737             fundecl);
6738 }
6739
6740 /* Convert value RHS to type TYPE as preparation for an assignment to
6741    an lvalue of type TYPE.  If ORIGTYPE is not NULL_TREE, it is the
6742    original type of RHS; this differs from TREE_TYPE (RHS) for enum
6743    types.  NULL_POINTER_CONSTANT says whether RHS was a null pointer
6744    constant before any folding.
6745    The real work of conversion is done by `convert'.
6746    The purpose of this function is to generate error messages
6747    for assignments that are not allowed in C.
6748    ERRTYPE says whether it is argument passing, assignment,
6749    initialization or return.
6750
6751    In the following example, '~' denotes where EXPR_LOC and '^' where
6752    LOCATION point to:
6753
6754      f (var);      [ic_argpass]
6755      ^  ~~~
6756      x = var;      [ic_assign]
6757        ^ ~~~;
6758      int x = var;  [ic_init]
6759              ^^^
6760      return x;     [ic_return]
6761             ^
6762
6763    FUNCTION is a tree for the function being called.
6764    PARMNUM is the number of the argument, for printing in error messages.
6765    WARNOPT may be set to a warning option to issue the corresponding warning
6766    rather than an error for invalid conversions.  Used for calls to built-in
6767    functions declared without a prototype.  */
6768
6769 static tree
6770 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6771                         tree rhs, tree origtype, enum impl_conv errtype,
6772                         bool null_pointer_constant, tree fundecl,
6773                         tree function, int parmnum, int warnopt /* = 0 */)
6774 {
6775   enum tree_code codel = TREE_CODE (type);
6776   tree orig_rhs = rhs;
6777   tree rhstype;
6778   enum tree_code coder;
6779   tree rname = NULL_TREE;
6780   bool objc_ok = false;
6781
6782   /* Use the expansion point location to handle cases such as user's
6783      function returning a wrong-type macro defined in a system header.  */
6784   location = expansion_point_location_if_in_system_header (location);
6785
6786   if (errtype == ic_argpass)
6787     {
6788       tree selector;
6789       /* Change pointer to function to the function itself for
6790          diagnostics.  */
6791       if (TREE_CODE (function) == ADDR_EXPR
6792           && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6793         function = TREE_OPERAND (function, 0);
6794
6795       /* Handle an ObjC selector specially for diagnostics.  */
6796       selector = objc_message_selector ();
6797       rname = function;
6798       if (selector && parmnum > 2)
6799         {
6800           rname = selector;
6801           parmnum -= 2;
6802         }
6803     }
6804
6805   /* This macro is used to emit diagnostics to ensure that all format
6806      strings are complete sentences, visible to gettext and checked at
6807      compile time.  */
6808 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE)      \
6809   do {                                                                   \
6810     switch (errtype)                                                     \
6811       {                                                                  \
6812       case ic_argpass:                                                   \
6813         {                                                               \
6814           auto_diagnostic_group d;                                              \
6815           if (pedwarn (PLOC, OPT, AR, parmnum, rname))          \
6816             inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6817         }                                                               \
6818         break;                                                           \
6819       case ic_assign:                                                    \
6820         pedwarn (LOCATION, OPT, AS);                                     \
6821         break;                                                           \
6822       case ic_init:                                                      \
6823       case ic_init_const:                                                \
6824         pedwarn_init (LOCATION, OPT, IN);                                \
6825         break;                                                           \
6826       case ic_return:                                                    \
6827         pedwarn (LOCATION, OPT, RE);                                     \
6828         break;                                                           \
6829       default:                                                           \
6830         gcc_unreachable ();                                              \
6831       }                                                                  \
6832   } while (0)
6833
6834   /* This macro is used to emit diagnostics to ensure that all format
6835      strings are complete sentences, visible to gettext and checked at
6836      compile time.  It can be called with 'pedwarn' or 'warning_at'.  */
6837 #define WARNING_FOR_QUALIFIERS(PEDWARN, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6838   do {                                                                   \
6839     switch (errtype)                                                     \
6840       {                                                                  \
6841       case ic_argpass:                                                   \
6842         {                                                                \
6843           auto_diagnostic_group d;                                       \
6844           if (PEDWARN) {                                                 \
6845             if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS))          \
6846               inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype);  \
6847           } else {                                                       \
6848             if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS))       \
6849               inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype);  \
6850           }                                                              \
6851         }                                                                \
6852         break;                                                           \
6853       case ic_assign:                                                    \
6854         if (PEDWARN)                                                     \
6855           pedwarn (LOCATION, OPT, AS, QUALS);                            \
6856         else                                                             \
6857           warning_at (LOCATION, OPT, AS, QUALS);                         \
6858         break;                                                           \
6859       case ic_init:                                                      \
6860       case ic_init_const:                                                \
6861         if (PEDWARN)                                                     \
6862           pedwarn (LOCATION, OPT, IN, QUALS);                            \
6863         else                                                             \
6864           warning_at (LOCATION, OPT, IN, QUALS);                         \
6865         break;                                                           \
6866       case ic_return:                                                    \
6867         if (PEDWARN)                                                     \
6868           pedwarn (LOCATION, OPT, RE, QUALS);                            \
6869         else                                                             \
6870           warning_at (LOCATION, OPT, RE, QUALS);                         \
6871         break;                                                           \
6872       default:                                                           \
6873         gcc_unreachable ();                                              \
6874       }                                                                  \
6875   } while (0)
6876
6877   /* This macro is used to emit diagnostics to ensure that all format
6878      strings are complete sentences, visible to gettext and checked at
6879      compile time.  It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6880      extra parameter to enumerate qualifiers.  */
6881 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6882    WARNING_FOR_QUALIFIERS (true, LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS)
6883
6884
6885   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6886     rhs = TREE_OPERAND (rhs, 0);
6887
6888   rhstype = TREE_TYPE (rhs);
6889   coder = TREE_CODE (rhstype);
6890
6891   if (coder == ERROR_MARK)
6892     return error_mark_node;
6893
6894   if (c_dialect_objc ())
6895     {
6896       int parmno;
6897
6898       switch (errtype)
6899         {
6900         case ic_return:
6901           parmno = 0;
6902           break;
6903
6904         case ic_assign:
6905           parmno = -1;
6906           break;
6907
6908         case ic_init:
6909         case ic_init_const:
6910           parmno = -2;
6911           break;
6912
6913         default:
6914           parmno = parmnum;
6915           break;
6916         }
6917
6918       objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6919     }
6920
6921   if (warn_cxx_compat)
6922     {
6923       tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6924       if (checktype != error_mark_node
6925           && TREE_CODE (type) == ENUMERAL_TYPE
6926           && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6927         switch (errtype)
6928           {
6929           case ic_argpass:
6930             if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6931                          "passing argument %d of %qE is invalid in C++",
6932                          parmnum, rname))
6933               inform ((fundecl && !DECL_IS_UNDECLARED_BUILTIN (fundecl))
6934                       ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6935                       "expected %qT but argument is of type %qT",
6936                       type, rhstype);
6937             break;
6938           case ic_assign:
6939             pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6940                      "%qT in assignment is invalid in C++", rhstype, type);
6941             break;
6942           case ic_init:
6943           case ic_init_const:
6944             pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6945                           "%qT to %qT in initialization is invalid in C++",
6946                           rhstype, type);
6947             break;
6948           case ic_return:
6949             pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6950                      "%qT in return is invalid in C++", rhstype, type);
6951             break;
6952           default:
6953             gcc_unreachable ();
6954           }
6955     }
6956
6957   if (warn_enum_conversion)
6958     {
6959       tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6960       if (checktype != error_mark_node
6961           && TREE_CODE (checktype) == ENUMERAL_TYPE
6962           && TREE_CODE (type) == ENUMERAL_TYPE
6963           && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6964        {
6965           gcc_rich_location loc (location);
6966           warning_at (&loc, OPT_Wenum_conversion,
6967                       "implicit conversion from %qT to %qT",
6968                       checktype, type);
6969        }
6970     }
6971
6972   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6973     {
6974       warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6975       return rhs;
6976     }
6977
6978   if (coder == VOID_TYPE)
6979     {
6980       /* Except for passing an argument to an unprototyped function,
6981          this is a constraint violation.  When passing an argument to
6982          an unprototyped function, it is compile-time undefined;
6983          making it a constraint in that case was rejected in
6984          DR#252.  */
6985       const char msg[] = "void value not ignored as it ought to be";
6986       if (warnopt)
6987         warning_at (location, warnopt, msg);
6988       else
6989         error_at (location, msg);
6990       return error_mark_node;
6991     }
6992   rhs = require_complete_type (location, rhs);
6993   if (rhs == error_mark_node)
6994     return error_mark_node;
6995
6996   if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6997     return error_mark_node;
6998
6999   /* A non-reference type can convert to a reference.  This handles
7000      va_start, va_copy and possibly port built-ins.  */
7001   if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
7002     {
7003       if (!lvalue_p (rhs))
7004         {
7005           const char msg[] = "cannot pass rvalue to reference parameter";
7006           if (warnopt)
7007             warning_at (location, warnopt, msg);
7008           else
7009             error_at (location, msg);
7010           return error_mark_node;
7011         }
7012       if (!c_mark_addressable (rhs))
7013         return error_mark_node;
7014       rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
7015       SET_EXPR_LOCATION (rhs, location);
7016
7017       rhs = convert_for_assignment (location, expr_loc,
7018                                     build_pointer_type (TREE_TYPE (type)),
7019                                     rhs, origtype, errtype,
7020                                     null_pointer_constant, fundecl, function,
7021                                     parmnum, warnopt);
7022       if (rhs == error_mark_node)
7023         return error_mark_node;
7024
7025       rhs = build1 (NOP_EXPR, type, rhs);
7026       SET_EXPR_LOCATION (rhs, location);
7027       return rhs;
7028     }
7029   /* Some types can interconvert without explicit casts.  */
7030   else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
7031            && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
7032     return convert (type, rhs);
7033   /* Arithmetic types all interconvert, and enum is treated like int.  */
7034   else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
7035             || codel == FIXED_POINT_TYPE
7036             || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
7037             || codel == BOOLEAN_TYPE)
7038            && (coder == INTEGER_TYPE || coder == REAL_TYPE
7039                || coder == FIXED_POINT_TYPE
7040                || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
7041                || coder == BOOLEAN_TYPE))
7042     {
7043       if (warnopt && errtype == ic_argpass)
7044         maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
7045                                          rhstype);
7046
7047       bool save = in_late_binary_op;
7048       if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
7049           || (coder == REAL_TYPE
7050               && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
7051               && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
7052         in_late_binary_op = true;
7053       tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
7054                                     ? expr_loc : location, type, orig_rhs,
7055                                     errtype == ic_init_const);
7056       in_late_binary_op = save;
7057       return ret;
7058     }
7059
7060   /* Aggregates in different TUs might need conversion.  */
7061   if ((codel == RECORD_TYPE || codel == UNION_TYPE)
7062       && codel == coder
7063       && comptypes (type, rhstype))
7064     return convert_and_check (expr_loc != UNKNOWN_LOCATION
7065                               ? expr_loc : location, type, rhs);
7066
7067   /* Conversion to a transparent union or record from its member types.
7068      This applies only to function arguments.  */
7069   if (((codel == UNION_TYPE || codel == RECORD_TYPE)
7070       && TYPE_TRANSPARENT_AGGR (type))
7071       && errtype == ic_argpass)
7072     {
7073       tree memb, marginal_memb = NULL_TREE;
7074
7075       for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
7076         {
7077           tree memb_type = TREE_TYPE (memb);
7078
7079           if (comptypes (TYPE_MAIN_VARIANT (memb_type),
7080                          TYPE_MAIN_VARIANT (rhstype)))
7081             break;
7082
7083           if (TREE_CODE (memb_type) != POINTER_TYPE)
7084             continue;
7085
7086           if (coder == POINTER_TYPE)
7087             {
7088               tree ttl = TREE_TYPE (memb_type);
7089               tree ttr = TREE_TYPE (rhstype);
7090
7091               /* Any non-function converts to a [const][volatile] void *
7092                  and vice versa; otherwise, targets must be the same.
7093                  Meanwhile, the lhs target must have all the qualifiers of
7094                  the rhs.  */
7095               if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7096                   || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7097                   || comp_target_types (location, memb_type, rhstype))
7098                 {
7099                   int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
7100                   int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
7101                   /* If this type won't generate any warnings, use it.  */
7102                   if (lquals == rquals
7103                       || ((TREE_CODE (ttr) == FUNCTION_TYPE
7104                            && TREE_CODE (ttl) == FUNCTION_TYPE)
7105                           ? ((lquals | rquals) == rquals)
7106                           : ((lquals | rquals) == lquals)))
7107                     break;
7108
7109                   /* Keep looking for a better type, but remember this one.  */
7110                   if (!marginal_memb)
7111                     marginal_memb = memb;
7112                 }
7113             }
7114
7115           /* Can convert integer zero to any pointer type.  */
7116           if (null_pointer_constant)
7117             {
7118               rhs = null_pointer_node;
7119               break;
7120             }
7121         }
7122
7123       if (memb || marginal_memb)
7124         {
7125           if (!memb)
7126             {
7127               /* We have only a marginally acceptable member type;
7128                  it needs a warning.  */
7129               tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7130               tree ttr = TREE_TYPE (rhstype);
7131
7132               /* Const and volatile mean something different for function
7133                  types, so the usual warnings are not appropriate.  */
7134               if (TREE_CODE (ttr) == FUNCTION_TYPE
7135                   && TREE_CODE (ttl) == FUNCTION_TYPE)
7136                 {
7137                   /* Because const and volatile on functions are
7138                      restrictions that say the function will not do
7139                      certain things, it is okay to use a const or volatile
7140                      function where an ordinary one is wanted, but not
7141                      vice-versa.  */
7142                   if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7143                       & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7144                     PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7145                                             OPT_Wdiscarded_qualifiers,
7146                                             G_("passing argument %d of %qE "
7147                                                "makes %q#v qualified function "
7148                                                "pointer from unqualified"),
7149                                             G_("assignment makes %q#v qualified "
7150                                                "function pointer from "
7151                                                "unqualified"),
7152                                             G_("initialization makes %q#v qualified "
7153                                                "function pointer from "
7154                                                "unqualified"),
7155                                             G_("return makes %q#v qualified function "
7156                                                "pointer from unqualified"),
7157                                             TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7158                 }
7159               else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7160                        & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7161                 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7162                                         OPT_Wdiscarded_qualifiers,
7163                                         G_("passing argument %d of %qE discards "
7164                                            "%qv qualifier from pointer target type"),
7165                                         G_("assignment discards %qv qualifier "
7166                                            "from pointer target type"),
7167                                         G_("initialization discards %qv qualifier "
7168                                            "from pointer target type"),
7169                                         G_("return discards %qv qualifier from "
7170                                            "pointer target type"),
7171                                         TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7172
7173               memb = marginal_memb;
7174             }
7175
7176           if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7177             pedwarn (location, OPT_Wpedantic,
7178                      "ISO C prohibits argument conversion to union type");
7179
7180           rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7181           return build_constructor_single (type, memb, rhs);
7182         }
7183     }
7184
7185   /* Conversions among pointers */
7186   else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7187            && (coder == codel))
7188     {
7189       /* If RHS refers to a built-in declared without a prototype
7190          BLTIN is the declaration of the built-in with a prototype
7191          and RHSTYPE is set to the actual type of the built-in.  */
7192       tree bltin;
7193       rhstype = type_or_builtin_type (rhs, &bltin);
7194
7195       tree ttl = TREE_TYPE (type);
7196       tree ttr = TREE_TYPE (rhstype);
7197       tree mvl = ttl;
7198       tree mvr = ttr;
7199       bool is_opaque_pointer;
7200       int target_cmp = 0;   /* Cache comp_target_types () result.  */
7201       addr_space_t asl;
7202       addr_space_t asr;
7203
7204       if (TREE_CODE (mvl) != ARRAY_TYPE)
7205         mvl = (TYPE_ATOMIC (mvl)
7206                ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7207                                          TYPE_QUAL_ATOMIC)
7208                : TYPE_MAIN_VARIANT (mvl));
7209       if (TREE_CODE (mvr) != ARRAY_TYPE)
7210         mvr = (TYPE_ATOMIC (mvr)
7211                ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7212                                          TYPE_QUAL_ATOMIC)
7213                : TYPE_MAIN_VARIANT (mvr));
7214       /* Opaque pointers are treated like void pointers.  */
7215       is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7216
7217       /* The Plan 9 compiler permits a pointer to a struct to be
7218          automatically converted into a pointer to an anonymous field
7219          within the struct.  */
7220       if (flag_plan9_extensions
7221           && RECORD_OR_UNION_TYPE_P (mvl)
7222           && RECORD_OR_UNION_TYPE_P (mvr)
7223           && mvl != mvr)
7224         {
7225           tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7226           if (new_rhs != NULL_TREE)
7227             {
7228               rhs = new_rhs;
7229               rhstype = TREE_TYPE (rhs);
7230               coder = TREE_CODE (rhstype);
7231               ttr = TREE_TYPE (rhstype);
7232               mvr = TYPE_MAIN_VARIANT (ttr);
7233             }
7234         }
7235
7236       /* C++ does not allow the implicit conversion void* -> T*.  However,
7237          for the purpose of reducing the number of false positives, we
7238          tolerate the special case of
7239
7240                 int *p = NULL;
7241
7242          where NULL is typically defined in C to be '(void *) 0'.  */
7243       if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7244         warning_at (errtype == ic_argpass ? expr_loc : location,
7245                     OPT_Wc___compat,
7246                     "request for implicit conversion "
7247                     "from %qT to %qT not permitted in C++", rhstype, type);
7248
7249       /* See if the pointers point to incompatible address spaces.  */
7250       asl = TYPE_ADDR_SPACE (ttl);
7251       asr = TYPE_ADDR_SPACE (ttr);
7252       if (!null_pointer_constant_p (rhs)
7253           && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7254         {
7255           auto_diagnostic_group d;
7256           bool diagnosed = true;
7257           switch (errtype)
7258             {
7259             case ic_argpass:
7260               {
7261                 const char msg[] = G_("passing argument %d of %qE from "
7262                                       "pointer to non-enclosed address space");
7263                 if (warnopt)
7264                   diagnosed
7265                     = warning_at (expr_loc, warnopt, msg, parmnum, rname);
7266                 else
7267                   error_at (expr_loc, msg, parmnum, rname);
7268               break;
7269               }
7270             case ic_assign:
7271               {
7272                 const char msg[] = G_("assignment from pointer to "
7273                                       "non-enclosed address space");
7274                 if (warnopt)
7275                   diagnosed = warning_at (location, warnopt, msg);
7276                 else
7277                   error_at (location, msg);
7278                 break;
7279               }
7280             case ic_init:
7281             case ic_init_const:
7282               {
7283                 const char msg[] = G_("initialization from pointer to "
7284                                       "non-enclosed address space");
7285                 if (warnopt)
7286                   diagnosed = warning_at (location, warnopt, msg);
7287                 else
7288                   error_at (location, msg);
7289                 break;
7290               }
7291             case ic_return:
7292               {
7293                 const char msg[] = G_("return from pointer to "
7294                                       "non-enclosed address space");
7295                 if (warnopt)
7296                   diagnosed = warning_at (location, warnopt, msg);
7297                 else
7298                   error_at (location, msg);
7299                 break;
7300               }
7301             default:
7302               gcc_unreachable ();
7303             }
7304           if (diagnosed)
7305             {
7306               if (errtype == ic_argpass)
7307                 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7308               else
7309                 inform (location, "expected %qT but pointer is of type %qT",
7310                         type, rhstype);
7311             }
7312           return error_mark_node;
7313         }
7314
7315       /* Check if the right-hand side has a format attribute but the
7316          left-hand side doesn't.  */
7317       if (warn_suggest_attribute_format
7318           && check_missing_format_attribute (type, rhstype))
7319         {
7320           switch (errtype)
7321           {
7322           case ic_argpass:
7323             warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7324                         "argument %d of %qE might be "
7325                         "a candidate for a format attribute",
7326                         parmnum, rname);
7327             break;
7328           case ic_assign:
7329             warning_at (location, OPT_Wsuggest_attribute_format,
7330                         "assignment left-hand side might be "
7331                         "a candidate for a format attribute");
7332             break;
7333           case ic_init:
7334           case ic_init_const:
7335             warning_at (location, OPT_Wsuggest_attribute_format,
7336                         "initialization left-hand side might be "
7337                         "a candidate for a format attribute");
7338             break;
7339           case ic_return:
7340             warning_at (location, OPT_Wsuggest_attribute_format,
7341                         "return type might be "
7342                         "a candidate for a format attribute");
7343             break;
7344           default:
7345             gcc_unreachable ();
7346           }
7347         }
7348
7349       /* See if the pointers point to incompatible scalar storage orders.  */
7350       if (warn_scalar_storage_order
7351           && (AGGREGATE_TYPE_P (ttl) && TYPE_REVERSE_STORAGE_ORDER (ttl))
7352              != (AGGREGATE_TYPE_P (ttr) && TYPE_REVERSE_STORAGE_ORDER (ttr)))
7353         {
7354           tree t;
7355
7356           switch (errtype)
7357           {
7358           case ic_argpass:
7359             /* Do not warn for built-in functions, for example memcpy, since we
7360                control how they behave and they can be useful in this area.  */
7361             if (TREE_CODE (rname) != FUNCTION_DECL
7362                 || !fndecl_built_in_p (rname))
7363               warning_at (location, OPT_Wscalar_storage_order,
7364                           "passing argument %d of %qE from incompatible "
7365                           "scalar storage order", parmnum, rname);
7366             break;
7367           case ic_assign:
7368             /* Do not warn if the RHS is a call to a function that returns a
7369                pointer that is not an alias.  */
7370             if (TREE_CODE (rhs) != CALL_EXPR
7371                 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7372                 || !DECL_IS_MALLOC (t))
7373               warning_at (location, OPT_Wscalar_storage_order,
7374                           "assignment to %qT from pointer type %qT with "
7375                           "incompatible scalar storage order", type, rhstype);
7376             break;
7377           case ic_init:
7378           case ic_init_const:
7379             /* Likewise.  */
7380             if (TREE_CODE (rhs) != CALL_EXPR
7381                 || (t = get_callee_fndecl (rhs)) == NULL_TREE
7382                 || !DECL_IS_MALLOC (t))
7383               warning_at (location, OPT_Wscalar_storage_order,
7384                           "initialization of %qT from pointer type %qT with "
7385                           "incompatible scalar storage order", type, rhstype);
7386             break;
7387           case ic_return:
7388             warning_at (location, OPT_Wscalar_storage_order,
7389                         "returning %qT from pointer type with incompatible "
7390                         "scalar storage order %qT", rhstype, type);
7391             break;
7392           default:
7393             gcc_unreachable ();
7394           }
7395         }
7396
7397       /* Any non-function converts to a [const][volatile] void *
7398          and vice versa; otherwise, targets must be the same.
7399          Meanwhile, the lhs target must have all the qualifiers of the rhs.  */
7400       if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7401           || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7402           || (target_cmp = comp_target_types (location, type, rhstype))
7403           || is_opaque_pointer
7404           || ((c_common_unsigned_type (mvl)
7405                == c_common_unsigned_type (mvr))
7406               && (c_common_signed_type (mvl)
7407                   == c_common_signed_type (mvr))
7408               && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7409         {
7410           /* Warn about loss of qualifers from pointers to arrays with
7411              qualifiers on the element type. */
7412           if (TREE_CODE (ttr) == ARRAY_TYPE)
7413             {
7414               ttr = strip_array_types (ttr);
7415               ttl = strip_array_types (ttl);
7416
7417               if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7418                   & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7419                 WARNING_FOR_QUALIFIERS (flag_isoc2x,
7420                                         location, expr_loc,
7421                                         OPT_Wdiscarded_array_qualifiers,
7422                                         G_("passing argument %d of %qE discards "
7423                                            "%qv qualifier from pointer target type"),
7424                                         G_("assignment discards %qv qualifier "
7425                                            "from pointer target type"),
7426                                         G_("initialization discards %qv qualifier "
7427                                            "from pointer target type"),
7428                                         G_("return discards %qv qualifier from "
7429                                            "pointer target type"),
7430                                         TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7431             }
7432           else if (pedantic
7433               && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7434                   ||
7435                   (VOID_TYPE_P (ttr)
7436                    && !null_pointer_constant
7437                    && TREE_CODE (ttl) == FUNCTION_TYPE)))
7438             PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7439                                     G_("ISO C forbids passing argument %d of "
7440                                        "%qE between function pointer "
7441                                        "and %<void *%>"),
7442                                     G_("ISO C forbids assignment between "
7443                                        "function pointer and %<void *%>"),
7444                                     G_("ISO C forbids initialization between "
7445                                        "function pointer and %<void *%>"),
7446                                     G_("ISO C forbids return between function "
7447                                        "pointer and %<void *%>"));
7448           /* Const and volatile mean something different for function types,
7449              so the usual warnings are not appropriate.  */
7450           else if (TREE_CODE (ttr) != FUNCTION_TYPE
7451                    && TREE_CODE (ttl) != FUNCTION_TYPE)
7452             {
7453                /* Assignments between atomic and non-atomic objects are OK.  */
7454                bool warn_quals_ped = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7455                                      & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl);
7456                bool warn_quals = TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7457                                  & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (strip_array_types (ttl));
7458
7459               /* Don't warn about loss of qualifier for conversions from
7460                  qualified void* to pointers to arrays with corresponding
7461                  qualifier on the element type (except for pedantic before C23). */
7462               if (warn_quals || (warn_quals_ped && pedantic && !flag_isoc2x))
7463                 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7464                                         OPT_Wdiscarded_qualifiers,
7465                                         G_("passing argument %d of %qE discards "
7466                                            "%qv qualifier from pointer target type"),
7467                                         G_("assignment discards %qv qualifier "
7468                                            "from pointer target type"),
7469                                         G_("initialization discards %qv qualifier "
7470                                            "from pointer target type"),
7471                                         G_("return discards %qv qualifier from "
7472                                            "pointer target type"),
7473                                         TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7474               else if (warn_quals_ped)
7475                 pedwarn_c11 (location, OPT_Wc11_c2x_compat,
7476                              "array with qualifier on the element is not qualified before C2X");
7477
7478               /* If this is not a case of ignoring a mismatch in signedness,
7479                  no warning.  */
7480               else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7481                        || target_cmp)
7482                 ;
7483               /* If there is a mismatch, do warn.  */
7484               else if (warn_pointer_sign)
7485                 switch (errtype)
7486                   {
7487                   case ic_argpass:
7488                     {
7489                       auto_diagnostic_group d;
7490                       range_label_for_type_mismatch rhs_label (rhstype, type);
7491                       gcc_rich_location richloc (expr_loc, &rhs_label);
7492                       if (pedwarn (&richloc, OPT_Wpointer_sign,
7493                                    "pointer targets in passing argument %d of "
7494                                    "%qE differ in signedness", parmnum, rname))
7495                         inform_for_arg (fundecl, expr_loc, parmnum, type,
7496                                         rhstype);
7497                     }
7498                     break;
7499                   case ic_assign:
7500                     pedwarn (location, OPT_Wpointer_sign,
7501                              "pointer targets in assignment from %qT to %qT "
7502                              "differ in signedness", rhstype, type);
7503                     break;
7504                   case ic_init:
7505                   case ic_init_const:
7506                     pedwarn_init (location, OPT_Wpointer_sign,
7507                                   "pointer targets in initialization of %qT "
7508                                   "from %qT differ in signedness", type,
7509                                   rhstype);
7510                     break;
7511                   case ic_return:
7512                     pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7513                              "returning %qT from a function with return type "
7514                              "%qT differ in signedness", rhstype, type);
7515                     break;
7516                   default:
7517                     gcc_unreachable ();
7518                   }
7519             }
7520           else if (TREE_CODE (ttl) == FUNCTION_TYPE
7521                    && TREE_CODE (ttr) == FUNCTION_TYPE)
7522             {
7523               /* Because const and volatile on functions are restrictions
7524                  that say the function will not do certain things,
7525                  it is okay to use a const or volatile function
7526                  where an ordinary one is wanted, but not vice-versa.  */
7527               if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7528                   & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7529                 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7530                                         OPT_Wdiscarded_qualifiers,
7531                                         G_("passing argument %d of %qE makes "
7532                                            "%q#v qualified function pointer "
7533                                            "from unqualified"),
7534                                         G_("assignment makes %q#v qualified function "
7535                                            "pointer from unqualified"),
7536                                         G_("initialization makes %q#v qualified "
7537                                            "function pointer from unqualified"),
7538                                         G_("return makes %q#v qualified function "
7539                                            "pointer from unqualified"),
7540                                         TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7541             }
7542         }
7543       /* Avoid warning about the volatile ObjC EH puts on decls.  */
7544       else if (!objc_ok)
7545         {
7546           switch (errtype)
7547             {
7548             case ic_argpass:
7549               {
7550                 auto_diagnostic_group d;
7551                 range_label_for_type_mismatch rhs_label (rhstype, type);
7552                 gcc_rich_location richloc (expr_loc, &rhs_label);
7553                 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7554                              "passing argument %d of %qE from incompatible "
7555                              "pointer type", parmnum, rname))
7556                   inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7557               }
7558               break;
7559             case ic_assign:
7560               if (bltin)
7561                 pedwarn (location, OPT_Wincompatible_pointer_types,
7562                          "assignment to %qT from pointer to "
7563                          "%qD with incompatible type %qT",
7564                          type, bltin, rhstype);
7565               else
7566                 pedwarn (location, OPT_Wincompatible_pointer_types,
7567                          "assignment to %qT from incompatible pointer type %qT",
7568                          type, rhstype);
7569               break;
7570             case ic_init:
7571             case ic_init_const:
7572               if (bltin)
7573                 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7574                               "initialization of %qT from pointer to "
7575                               "%qD with incompatible type %qT",
7576                               type, bltin, rhstype);
7577               else
7578                 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7579                               "initialization of %qT from incompatible "
7580                               "pointer type %qT",
7581                               type, rhstype);
7582               break;
7583             case ic_return:
7584               if (bltin)
7585                 pedwarn (location, OPT_Wincompatible_pointer_types,
7586                          "returning pointer to %qD of type %qT from "
7587                          "a function with incompatible type %qT",
7588                          bltin, rhstype, type);
7589               else
7590                 pedwarn (location, OPT_Wincompatible_pointer_types,
7591                          "returning %qT from a function with incompatible "
7592                          "return type %qT", rhstype, type);
7593               break;
7594             default:
7595               gcc_unreachable ();
7596             }
7597         }
7598
7599       /* If RHS isn't an address, check pointer or array of packed
7600          struct or union.  */
7601       warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7602
7603       return convert (type, rhs);
7604     }
7605   else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7606     {
7607       /* ??? This should not be an error when inlining calls to
7608          unprototyped functions.  */
7609       const char msg[] = "invalid use of non-lvalue array";
7610       if (warnopt)
7611         warning_at (location, warnopt, msg);
7612       else
7613         error_at (location, msg);
7614       return error_mark_node;
7615     }
7616   else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7617     {
7618       /* An explicit constant 0 can convert to a pointer,
7619          or one that results from arithmetic, even including
7620          a cast to integer type.  */
7621       if (!null_pointer_constant)
7622         switch (errtype)
7623           {
7624           case ic_argpass:
7625             {
7626               auto_diagnostic_group d;
7627               range_label_for_type_mismatch rhs_label (rhstype, type);
7628               gcc_rich_location richloc (expr_loc, &rhs_label);
7629               if (pedwarn (&richloc, OPT_Wint_conversion,
7630                            "passing argument %d of %qE makes pointer from "
7631                            "integer without a cast", parmnum, rname))
7632                 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7633             }
7634             break;
7635           case ic_assign:
7636             pedwarn (location, OPT_Wint_conversion,
7637                      "assignment to %qT from %qT makes pointer from integer "
7638                      "without a cast", type, rhstype);
7639             break;
7640           case ic_init:
7641           case ic_init_const:
7642             pedwarn_init (location, OPT_Wint_conversion,
7643                           "initialization of %qT from %qT makes pointer from "
7644                           "integer without a cast", type, rhstype);
7645             break;
7646           case ic_return:
7647             pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7648                      "function with return type %qT makes pointer from "
7649                      "integer without a cast", rhstype, type);
7650             break;
7651           default:
7652             gcc_unreachable ();
7653           }
7654
7655       return convert (type, rhs);
7656     }
7657   else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7658     {
7659       switch (errtype)
7660         {
7661         case ic_argpass:
7662           {
7663             auto_diagnostic_group d;
7664             range_label_for_type_mismatch rhs_label (rhstype, type);
7665             gcc_rich_location richloc (expr_loc, &rhs_label);
7666             if (pedwarn (&richloc, OPT_Wint_conversion,
7667                          "passing argument %d of %qE makes integer from "
7668                          "pointer without a cast", parmnum, rname))
7669               inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7670           }
7671           break;
7672         case ic_assign:
7673           pedwarn (location, OPT_Wint_conversion,
7674                    "assignment to %qT from %qT makes integer from pointer "
7675                    "without a cast", type, rhstype);
7676           break;
7677         case ic_init:
7678         case ic_init_const:
7679           pedwarn_init (location, OPT_Wint_conversion,
7680                         "initialization of %qT from %qT makes integer from "
7681                         "pointer without a cast", type, rhstype);
7682           break;
7683         case ic_return:
7684           pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7685                    "function with return type %qT makes integer from "
7686                    "pointer without a cast", rhstype, type);
7687           break;
7688         default:
7689           gcc_unreachable ();
7690         }
7691
7692       return convert (type, rhs);
7693     }
7694   else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7695     {
7696       tree ret;
7697       bool save = in_late_binary_op;
7698       in_late_binary_op = true;
7699       ret = convert (type, rhs);
7700       in_late_binary_op = save;
7701       return ret;
7702     }
7703
7704   switch (errtype)
7705     {
7706     case ic_argpass:
7707       {
7708         auto_diagnostic_group d;
7709         range_label_for_type_mismatch rhs_label (rhstype, type);
7710         gcc_rich_location richloc (expr_loc, &rhs_label);
7711         const char msg[] = G_("incompatible type for argument %d of %qE");
7712         if (warnopt)
7713           warning_at (expr_loc, warnopt, msg, parmnum, rname);
7714         else
7715           error_at (&richloc, msg, parmnum, rname);
7716         inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7717       }
7718       break;
7719     case ic_assign:
7720       {
7721         const char msg[]
7722           = G_("incompatible types when assigning to type %qT from type %qT");
7723         if (warnopt)
7724           warning_at (expr_loc, 0, msg, type, rhstype);
7725         else
7726           error_at (expr_loc, msg, type, rhstype);
7727         break;
7728       }
7729     case ic_init:
7730     case ic_init_const:
7731       {
7732         const char msg[]
7733           = G_("incompatible types when initializing type %qT using type %qT");
7734         if (warnopt)
7735           warning_at (location, 0, msg, type, rhstype);
7736         else
7737           error_at (location, msg, type, rhstype);
7738         break;
7739       }
7740     case ic_return:
7741       {
7742         const char msg[]
7743           = G_("incompatible types when returning type %qT but %qT was expected");
7744         if (warnopt)
7745           warning_at (location, 0, msg, rhstype, type);
7746         else
7747           error_at (location, msg, rhstype, type);
7748         break;
7749       }
7750     default:
7751       gcc_unreachable ();
7752     }
7753
7754   return error_mark_node;
7755 }
7756 \f
7757 /* If VALUE is a compound expr all of whose expressions are constant, then
7758    return its value.  Otherwise, return error_mark_node.
7759
7760    This is for handling COMPOUND_EXPRs as initializer elements
7761    which is allowed with a warning when -pedantic is specified.  */
7762
7763 static tree
7764 valid_compound_expr_initializer (tree value, tree endtype)
7765 {
7766   if (TREE_CODE (value) == COMPOUND_EXPR)
7767     {
7768       if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7769           == error_mark_node)
7770         return error_mark_node;
7771       return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7772                                               endtype);
7773     }
7774   else if (!initializer_constant_valid_p (value, endtype))
7775     return error_mark_node;
7776   else
7777     return value;
7778 }
7779 \f
7780 /* Perform appropriate conversions on the initial value of a variable,
7781    store it in the declaration DECL,
7782    and print any error messages that are appropriate.
7783    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7784    If the init is invalid, store an ERROR_MARK.
7785
7786    INIT_LOC is the location of the initial value.  */
7787
7788 void
7789 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7790 {
7791   tree value, type;
7792   bool npc = false;
7793
7794   /* If variable's type was invalidly declared, just ignore it.  */
7795
7796   type = TREE_TYPE (decl);
7797   if (TREE_CODE (type) == ERROR_MARK)
7798     return;
7799
7800   /* Digest the specified initializer into an expression.  */
7801
7802   if (init)
7803     npc = null_pointer_constant_p (init);
7804   value = digest_init (init_loc, type, init, origtype, npc,
7805                        true, TREE_STATIC (decl));
7806
7807   /* Store the expression if valid; else report error.  */
7808
7809   if (!in_system_header_at (input_location)
7810       && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7811     warning (OPT_Wtraditional, "traditional C rejects automatic "
7812              "aggregate initialization");
7813
7814   if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7815     DECL_INITIAL (decl) = value;
7816
7817   /* ANSI wants warnings about out-of-range constant initializers.  */
7818   STRIP_TYPE_NOPS (value);
7819   if (TREE_STATIC (decl))
7820     constant_expression_warning (value);
7821
7822   /* Check if we need to set array size from compound literal size.  */
7823   if (TREE_CODE (type) == ARRAY_TYPE
7824       && TYPE_DOMAIN (type) == NULL_TREE
7825       && value != error_mark_node)
7826     {
7827       tree inside_init = init;
7828
7829       STRIP_TYPE_NOPS (inside_init);
7830       inside_init = fold (inside_init);
7831
7832       if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7833         {
7834           tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7835
7836           if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7837             {
7838               /* For int foo[] = (int [3]){1}; we need to set array size
7839                  now since later on array initializer will be just the
7840                  brace enclosed list of the compound literal.  */
7841               tree etype = strip_array_types (TREE_TYPE (decl));
7842               type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7843               TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7844               layout_type (type);
7845               layout_decl (cldecl, 0);
7846               TREE_TYPE (decl)
7847                 = c_build_qualified_type (type, TYPE_QUALS (etype));
7848             }
7849         }
7850     }
7851 }
7852 \f
7853 /* Methods for storing and printing names for error messages.  */
7854
7855 /* Implement a spelling stack that allows components of a name to be pushed
7856    and popped.  Each element on the stack is this structure.  */
7857
7858 struct spelling
7859 {
7860   int kind;
7861   union
7862     {
7863       unsigned HOST_WIDE_INT i;
7864       const char *s;
7865     } u;
7866 };
7867
7868 #define SPELLING_STRING 1
7869 #define SPELLING_MEMBER 2
7870 #define SPELLING_BOUNDS 3
7871
7872 static struct spelling *spelling;       /* Next stack element (unused).  */
7873 static struct spelling *spelling_base;  /* Spelling stack base.  */
7874 static int spelling_size;               /* Size of the spelling stack.  */
7875
7876 /* Macros to save and restore the spelling stack around push_... functions.
7877    Alternative to SAVE_SPELLING_STACK.  */
7878
7879 #define SPELLING_DEPTH() (spelling - spelling_base)
7880 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7881
7882 /* Push an element on the spelling stack with type KIND and assign VALUE
7883    to MEMBER.  */
7884
7885 #define PUSH_SPELLING(KIND, VALUE, MEMBER)                              \
7886 {                                                                       \
7887   int depth = SPELLING_DEPTH ();                                        \
7888                                                                         \
7889   if (depth >= spelling_size)                                           \
7890     {                                                                   \
7891       spelling_size += 10;                                              \
7892       spelling_base = XRESIZEVEC (struct spelling, spelling_base,       \
7893                                   spelling_size);                       \
7894       RESTORE_SPELLING_DEPTH (depth);                                   \
7895     }                                                                   \
7896                                                                         \
7897   spelling->kind = (KIND);                                              \
7898   spelling->MEMBER = (VALUE);                                           \
7899   spelling++;                                                           \
7900 }
7901
7902 /* Push STRING on the stack.  Printed literally.  */
7903
7904 static void
7905 push_string (const char *string)
7906 {
7907   PUSH_SPELLING (SPELLING_STRING, string, u.s);
7908 }
7909
7910 /* Push a member name on the stack.  Printed as '.' STRING.  */
7911
7912 static void
7913 push_member_name (tree decl)
7914 {
7915   const char *const string
7916     = (DECL_NAME (decl)
7917        ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7918        : _("<anonymous>"));
7919   PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7920 }
7921
7922 /* Push an array bounds on the stack.  Printed as [BOUNDS].  */
7923
7924 static void
7925 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7926 {
7927   PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7928 }
7929
7930 /* Compute the maximum size in bytes of the printed spelling.  */
7931
7932 static int
7933 spelling_length (void)
7934 {
7935   int size = 0;
7936   struct spelling *p;
7937
7938   for (p = spelling_base; p < spelling; p++)
7939     {
7940       if (p->kind == SPELLING_BOUNDS)
7941         size += 25;
7942       else
7943         size += strlen (p->u.s) + 1;
7944     }
7945
7946   return size;
7947 }
7948
7949 /* Print the spelling to BUFFER and return it.  */
7950
7951 static char *
7952 print_spelling (char *buffer)
7953 {
7954   char *d = buffer;
7955   struct spelling *p;
7956
7957   for (p = spelling_base; p < spelling; p++)
7958     if (p->kind == SPELLING_BOUNDS)
7959       {
7960         sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7961         d += strlen (d);
7962       }
7963     else
7964       {
7965         const char *s;
7966         if (p->kind == SPELLING_MEMBER)
7967           *d++ = '.';
7968         for (s = p->u.s; (*d = *s++); d++)
7969           ;
7970       }
7971   *d++ = '\0';
7972   return buffer;
7973 }
7974
7975 /* Digest the parser output INIT as an initializer for type TYPE.
7976    Return a C expression of type TYPE to represent the initial value.
7977
7978    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7979
7980    NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7981
7982    If INIT is a string constant, STRICT_STRING is true if it is
7983    unparenthesized or we should not warn here for it being parenthesized.
7984    For other types of INIT, STRICT_STRING is not used.
7985
7986    INIT_LOC is the location of the INIT.
7987
7988    REQUIRE_CONSTANT requests an error if non-constant initializers or
7989    elements are seen.  */
7990
7991 static tree
7992 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7993              bool null_pointer_constant, bool strict_string,
7994              int require_constant)
7995 {
7996   enum tree_code code = TREE_CODE (type);
7997   tree inside_init = init;
7998   tree semantic_type = NULL_TREE;
7999   bool maybe_const = true;
8000
8001   if (type == error_mark_node
8002       || !init
8003       || error_operand_p (init))
8004     return error_mark_node;
8005
8006   STRIP_TYPE_NOPS (inside_init);
8007
8008   if (!c_in_omp_for)
8009     {
8010       if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
8011         {
8012           semantic_type = TREE_TYPE (inside_init);
8013           inside_init = TREE_OPERAND (inside_init, 0);
8014         }
8015       inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
8016     }
8017
8018   /* Initialization of an array of chars from a string constant
8019      optionally enclosed in braces.  */
8020
8021   if (code == ARRAY_TYPE && inside_init
8022       && TREE_CODE (inside_init) == STRING_CST)
8023     {
8024       tree typ1
8025         = (TYPE_ATOMIC (TREE_TYPE (type))
8026            ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
8027                                      TYPE_QUAL_ATOMIC)
8028            : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
8029       /* Note that an array could be both an array of character type
8030          and an array of wchar_t if wchar_t is signed char or unsigned
8031          char.  */
8032       bool char_array = (typ1 == char_type_node
8033                          || typ1 == signed_char_type_node
8034                          || typ1 == unsigned_char_type_node);
8035       bool wchar_array = !!comptypes (typ1, wchar_type_node);
8036       bool char16_array = !!comptypes (typ1, char16_type_node);
8037       bool char32_array = !!comptypes (typ1, char32_type_node);
8038
8039       if (char_array || wchar_array || char16_array || char32_array)
8040         {
8041           struct c_expr expr;
8042           tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
8043           bool incompat_string_cst = false;
8044           expr.value = inside_init;
8045           expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
8046           expr.original_type = NULL;
8047           maybe_warn_string_init (init_loc, type, expr);
8048
8049           if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8050             pedwarn_init (init_loc, OPT_Wpedantic,
8051                           "initialization of a flexible array member");
8052
8053           if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8054                          TYPE_MAIN_VARIANT (type)))
8055             return inside_init;
8056
8057           if (char_array)
8058             {
8059               if (typ2 != char_type_node && typ2 != char8_type_node)
8060                 incompat_string_cst = true;
8061             }
8062           else if (!comptypes (typ1, typ2))
8063             incompat_string_cst = true;
8064
8065           if (incompat_string_cst)
8066             {
8067               error_init (init_loc, "cannot initialize array of %qT from "
8068                           "a string literal with type array of %qT",
8069                           typ1, typ2);
8070               return error_mark_node;
8071             }
8072
8073           if (TYPE_DOMAIN (type) != NULL_TREE
8074               && TYPE_SIZE (type) != NULL_TREE
8075               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
8076             {
8077               unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
8078               unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
8079
8080               /* Subtract the size of a single (possibly wide) character
8081                  because it's ok to ignore the terminating null char
8082                  that is counted in the length of the constant.  */
8083               if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
8084                 pedwarn_init (init_loc, 0,
8085                               ("initializer-string for array of %qT "
8086                                "is too long"), typ1);
8087               else if (warn_cxx_compat
8088                        && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8089                 warning_at (init_loc, OPT_Wc___compat,
8090                             ("initializer-string for array of %qT "
8091                              "is too long for C++"), typ1);
8092               if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
8093                 {
8094                   unsigned HOST_WIDE_INT size
8095                     = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8096                   const char *p = TREE_STRING_POINTER (inside_init);
8097
8098                   inside_init = build_string (size, p);
8099                 }
8100             }
8101
8102           TREE_TYPE (inside_init) = type;
8103           return inside_init;
8104         }
8105       else if (INTEGRAL_TYPE_P (typ1))
8106         {
8107           error_init (init_loc, "array of inappropriate type initialized "
8108                       "from string constant");
8109           return error_mark_node;
8110         }
8111     }
8112
8113   /* Build a VECTOR_CST from a *constant* vector constructor.  If the
8114      vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
8115      below and handle as a constructor.  */
8116   if (code == VECTOR_TYPE
8117       && VECTOR_TYPE_P (TREE_TYPE (inside_init))
8118       && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
8119       && TREE_CONSTANT (inside_init))
8120     {
8121       if (TREE_CODE (inside_init) == VECTOR_CST
8122           && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8123                         TYPE_MAIN_VARIANT (type)))
8124         return inside_init;
8125
8126       if (TREE_CODE (inside_init) == CONSTRUCTOR)
8127         {
8128           unsigned HOST_WIDE_INT ix;
8129           tree value;
8130           bool constant_p = true;
8131
8132           /* Iterate through elements and check if all constructor
8133              elements are *_CSTs.  */
8134           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
8135             if (!CONSTANT_CLASS_P (value))
8136               {
8137                 constant_p = false;
8138                 break;
8139               }
8140
8141           if (constant_p)
8142             return build_vector_from_ctor (type,
8143                                            CONSTRUCTOR_ELTS (inside_init));
8144         }
8145     }
8146
8147   if (warn_sequence_point)
8148     verify_sequence_points (inside_init);
8149
8150   /* Any type can be initialized
8151      from an expression of the same type, optionally with braces.  */
8152
8153   if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
8154       && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
8155                      TYPE_MAIN_VARIANT (type))
8156           || (code == ARRAY_TYPE
8157               && comptypes (TREE_TYPE (inside_init), type))
8158           || (gnu_vector_type_p (type)
8159               && comptypes (TREE_TYPE (inside_init), type))
8160           || (code == POINTER_TYPE
8161               && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
8162               && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
8163                             TREE_TYPE (type)))))
8164     {
8165       if (code == POINTER_TYPE)
8166         {
8167           if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
8168             {
8169               if (TREE_CODE (inside_init) == STRING_CST
8170                   || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8171                 inside_init = array_to_pointer_conversion
8172                   (init_loc, inside_init);
8173               else
8174                 {
8175                   error_init (init_loc, "invalid use of non-lvalue array");
8176                   return error_mark_node;
8177                 }
8178             }
8179         }
8180
8181       if (code == VECTOR_TYPE)
8182         /* Although the types are compatible, we may require a
8183            conversion.  */
8184         inside_init = convert (type, inside_init);
8185
8186       if (require_constant
8187           && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
8188         {
8189           /* As an extension, allow initializing objects with static storage
8190              duration with compound literals (which are then treated just as
8191              the brace enclosed list they contain).  Also allow this for
8192              vectors, as we can only assign them with compound literals.  */
8193           if (flag_isoc99 && code != VECTOR_TYPE)
8194             pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
8195                           "is not constant");
8196           tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
8197           inside_init = DECL_INITIAL (decl);
8198         }
8199
8200       if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8201           && TREE_CODE (inside_init) != CONSTRUCTOR)
8202         {
8203           error_init (init_loc, "array initialized from non-constant array "
8204                       "expression");
8205           return error_mark_node;
8206         }
8207
8208       /* Compound expressions can only occur here if -Wpedantic or
8209          -pedantic-errors is specified.  In the later case, we always want
8210          an error.  In the former case, we simply want a warning.  */
8211       if (require_constant && pedantic
8212           && TREE_CODE (inside_init) == COMPOUND_EXPR)
8213         {
8214           inside_init
8215             = valid_compound_expr_initializer (inside_init,
8216                                                TREE_TYPE (inside_init));
8217           if (inside_init == error_mark_node)
8218             error_init (init_loc, "initializer element is not constant");
8219           else
8220             pedwarn_init (init_loc, OPT_Wpedantic,
8221                           "initializer element is not constant");
8222           if (flag_pedantic_errors)
8223             inside_init = error_mark_node;
8224         }
8225       else if (require_constant
8226                && !initializer_constant_valid_p (inside_init,
8227                                                  TREE_TYPE (inside_init)))
8228         {
8229           error_init (init_loc, "initializer element is not constant");
8230           inside_init = error_mark_node;
8231         }
8232       else if (require_constant && !maybe_const)
8233         pedwarn_init (init_loc, OPT_Wpedantic,
8234                       "initializer element is not a constant expression");
8235
8236       /* Added to enable additional -Wsuggest-attribute=format warnings.  */
8237       if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8238         inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8239                                               type, inside_init, origtype,
8240                                               (require_constant
8241                                                ? ic_init_const
8242                                                : ic_init), null_pointer_constant,
8243                                               NULL_TREE, NULL_TREE, 0);
8244       return inside_init;
8245     }
8246
8247   /* Handle scalar types, including conversions.  */
8248
8249   if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8250       || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8251       || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8252     {
8253       if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8254           && (TREE_CODE (init) == STRING_CST
8255               || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8256         inside_init = init = array_to_pointer_conversion (init_loc, init);
8257       if (semantic_type)
8258         inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8259                               inside_init);
8260       inside_init
8261         = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8262                                   inside_init, origtype,
8263                                   require_constant ? ic_init_const : ic_init,
8264                                   null_pointer_constant, NULL_TREE, NULL_TREE,
8265                                   0);
8266
8267       /* Check to see if we have already given an error message.  */
8268       if (inside_init == error_mark_node)
8269         ;
8270       else if (require_constant && !TREE_CONSTANT (inside_init))
8271         {
8272           error_init (init_loc, "initializer element is not constant");
8273           inside_init = error_mark_node;
8274         }
8275       else if (require_constant
8276                && !initializer_constant_valid_p (inside_init,
8277                                                  TREE_TYPE (inside_init)))
8278         {
8279           error_init (init_loc, "initializer element is not computable at "
8280                       "load time");
8281           inside_init = error_mark_node;
8282         }
8283       else if (require_constant && !maybe_const)
8284         pedwarn_init (init_loc, OPT_Wpedantic,
8285                       "initializer element is not a constant expression");
8286
8287       return inside_init;
8288     }
8289
8290   /* Come here only for records and arrays.  */
8291
8292   if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8293     {
8294       error_init (init_loc,
8295                   "variable-sized object may not be initialized except "
8296                   "with an empty initializer");
8297       return error_mark_node;
8298     }
8299
8300   error_init (init_loc, "invalid initializer");
8301   return error_mark_node;
8302 }
8303 \f
8304 /* Handle initializers that use braces.  */
8305
8306 /* Type of object we are accumulating a constructor for.
8307    This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE.  */
8308 static tree constructor_type;
8309
8310 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8311    left to fill.  */
8312 static tree constructor_fields;
8313
8314 /* For an ARRAY_TYPE, this is the specified index
8315    at which to store the next element we get.  */
8316 static tree constructor_index;
8317
8318 /* For an ARRAY_TYPE, this is the maximum index.  */
8319 static tree constructor_max_index;
8320
8321 /* For a RECORD_TYPE, this is the first field not yet written out.  */
8322 static tree constructor_unfilled_fields;
8323
8324 /* For an ARRAY_TYPE, this is the index of the first element
8325    not yet written out.  */
8326 static tree constructor_unfilled_index;
8327
8328 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8329    This is so we can generate gaps between fields, when appropriate.  */
8330 static tree constructor_bit_index;
8331
8332 /* If we are saving up the elements rather than allocating them,
8333    this is the list of elements so far (in reverse order,
8334    most recent first).  */
8335 static vec<constructor_elt, va_gc> *constructor_elements;
8336
8337 /* 1 if constructor should be incrementally stored into a constructor chain,
8338    0 if all the elements should be kept in AVL tree.  */
8339 static int constructor_incremental;
8340
8341 /* 1 if so far this constructor's elements are all compile-time constants.  */
8342 static int constructor_constant;
8343
8344 /* 1 if so far this constructor's elements are all valid address constants.  */
8345 static int constructor_simple;
8346
8347 /* 1 if this constructor has an element that cannot be part of a
8348    constant expression.  */
8349 static int constructor_nonconst;
8350
8351 /* 1 if this constructor is erroneous so far.  */
8352 static int constructor_erroneous;
8353
8354 /* 1 if this constructor is the universal zero initializer { 0 }.  */
8355 static int constructor_zeroinit;
8356
8357 /* Structure for managing pending initializer elements, organized as an
8358    AVL tree.  */
8359
8360 struct init_node
8361 {
8362   struct init_node *left, *right;
8363   struct init_node *parent;
8364   int balance;
8365   tree purpose;
8366   tree value;
8367   tree origtype;
8368 };
8369
8370 /* Tree of pending elements at this constructor level.
8371    These are elements encountered out of order
8372    which belong at places we haven't reached yet in actually
8373    writing the output.
8374    Will never hold tree nodes across GC runs.  */
8375 static struct init_node *constructor_pending_elts;
8376
8377 /* The SPELLING_DEPTH of this constructor.  */
8378 static int constructor_depth;
8379
8380 /* DECL node for which an initializer is being read.
8381    0 means we are reading a constructor expression
8382    such as (struct foo) {...}.  */
8383 static tree constructor_decl;
8384
8385 /* Nonzero if this is an initializer for a top-level decl.  */
8386 static int constructor_top_level;
8387
8388 /* Nonzero if there were any member designators in this initializer.  */
8389 static int constructor_designated;
8390
8391 /* Nesting depth of designator list.  */
8392 static int designator_depth;
8393
8394 /* Nonzero if there were diagnosed errors in this designator list.  */
8395 static int designator_erroneous;
8396
8397 \f
8398 /* This stack has a level for each implicit or explicit level of
8399    structuring in the initializer, including the outermost one.  It
8400    saves the values of most of the variables above.  */
8401
8402 struct constructor_range_stack;
8403
8404 struct constructor_stack
8405 {
8406   struct constructor_stack *next;
8407   tree type;
8408   tree fields;
8409   tree index;
8410   tree max_index;
8411   tree unfilled_index;
8412   tree unfilled_fields;
8413   tree bit_index;
8414   vec<constructor_elt, va_gc> *elements;
8415   struct init_node *pending_elts;
8416   int offset;
8417   int depth;
8418   /* If value nonzero, this value should replace the entire
8419      constructor at this level.  */
8420   struct c_expr replacement_value;
8421   struct constructor_range_stack *range_stack;
8422   char constant;
8423   char simple;
8424   char nonconst;
8425   char implicit;
8426   char erroneous;
8427   char outer;
8428   char incremental;
8429   char designated;
8430   int designator_depth;
8431 };
8432
8433 static struct constructor_stack *constructor_stack;
8434
8435 /* This stack represents designators from some range designator up to
8436    the last designator in the list.  */
8437
8438 struct constructor_range_stack
8439 {
8440   struct constructor_range_stack *next, *prev;
8441   struct constructor_stack *stack;
8442   tree range_start;
8443   tree index;
8444   tree range_end;
8445   tree fields;
8446 };
8447
8448 static struct constructor_range_stack *constructor_range_stack;
8449
8450 /* This stack records separate initializers that are nested.
8451    Nested initializers can't happen in ANSI C, but GNU C allows them
8452    in cases like { ... (struct foo) { ... } ... }.  */
8453
8454 struct initializer_stack
8455 {
8456   struct initializer_stack *next;
8457   tree decl;
8458   struct constructor_stack *constructor_stack;
8459   struct constructor_range_stack *constructor_range_stack;
8460   vec<constructor_elt, va_gc> *elements;
8461   struct spelling *spelling;
8462   struct spelling *spelling_base;
8463   int spelling_size;
8464   char top_level;
8465   char require_constant_value;
8466   char require_constant_elements;
8467   char designated;
8468   rich_location *missing_brace_richloc;
8469 };
8470
8471 static struct initializer_stack *initializer_stack;
8472 \f
8473 /* Prepare to parse and output the initializer for variable DECL.  */
8474
8475 void
8476 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8477             rich_location *richloc)
8478 {
8479   const char *locus;
8480   struct initializer_stack *p = XNEW (struct initializer_stack);
8481
8482   p->decl = constructor_decl;
8483   p->require_constant_value = require_constant_value;
8484   p->require_constant_elements = require_constant_elements;
8485   p->constructor_stack = constructor_stack;
8486   p->constructor_range_stack = constructor_range_stack;
8487   p->elements = constructor_elements;
8488   p->spelling = spelling;
8489   p->spelling_base = spelling_base;
8490   p->spelling_size = spelling_size;
8491   p->top_level = constructor_top_level;
8492   p->next = initializer_stack;
8493   p->missing_brace_richloc = richloc;
8494   p->designated = constructor_designated;
8495   initializer_stack = p;
8496
8497   constructor_decl = decl;
8498   constructor_designated = 0;
8499   constructor_top_level = top_level;
8500
8501   if (decl != NULL_TREE && decl != error_mark_node)
8502     {
8503       require_constant_value = TREE_STATIC (decl);
8504       require_constant_elements
8505         = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8506            /* For a scalar, you can always use any value to initialize,
8507               even within braces.  */
8508            && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8509       locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8510     }
8511   else
8512     {
8513       require_constant_value = 0;
8514       require_constant_elements = 0;
8515       locus = _("(anonymous)");
8516     }
8517
8518   constructor_stack = 0;
8519   constructor_range_stack = 0;
8520
8521   found_missing_braces = 0;
8522
8523   spelling_base = 0;
8524   spelling_size = 0;
8525   RESTORE_SPELLING_DEPTH (0);
8526
8527   if (locus)
8528     push_string (locus);
8529 }
8530
8531 void
8532 finish_init (void)
8533 {
8534   struct initializer_stack *p = initializer_stack;
8535
8536   /* Free the whole constructor stack of this initializer.  */
8537   while (constructor_stack)
8538     {
8539       struct constructor_stack *q = constructor_stack;
8540       constructor_stack = q->next;
8541       XDELETE (q);
8542     }
8543
8544   gcc_assert (!constructor_range_stack);
8545
8546   /* Pop back to the data of the outer initializer (if any).  */
8547   XDELETE (spelling_base);
8548
8549   constructor_decl = p->decl;
8550   require_constant_value = p->require_constant_value;
8551   require_constant_elements = p->require_constant_elements;
8552   constructor_stack = p->constructor_stack;
8553   constructor_designated = p->designated;
8554   constructor_range_stack = p->constructor_range_stack;
8555   constructor_elements = p->elements;
8556   spelling = p->spelling;
8557   spelling_base = p->spelling_base;
8558   spelling_size = p->spelling_size;
8559   constructor_top_level = p->top_level;
8560   initializer_stack = p->next;
8561   XDELETE (p);
8562 }
8563 \f
8564 /* Call here when we see the initializer is surrounded by braces.
8565    This is instead of a call to push_init_level;
8566    it is matched by a call to pop_init_level.
8567
8568    TYPE is the type to initialize, for a constructor expression.
8569    For an initializer for a decl, TYPE is zero.  */
8570
8571 void
8572 really_start_incremental_init (tree type)
8573 {
8574   struct constructor_stack *p = XNEW (struct constructor_stack);
8575
8576   if (type == NULL_TREE)
8577     type = TREE_TYPE (constructor_decl);
8578
8579   if (VECTOR_TYPE_P (type)
8580       && TYPE_VECTOR_OPAQUE (type))
8581     error ("opaque vector types cannot be initialized");
8582
8583   p->type = constructor_type;
8584   p->fields = constructor_fields;
8585   p->index = constructor_index;
8586   p->max_index = constructor_max_index;
8587   p->unfilled_index = constructor_unfilled_index;
8588   p->unfilled_fields = constructor_unfilled_fields;
8589   p->bit_index = constructor_bit_index;
8590   p->elements = constructor_elements;
8591   p->constant = constructor_constant;
8592   p->simple = constructor_simple;
8593   p->nonconst = constructor_nonconst;
8594   p->erroneous = constructor_erroneous;
8595   p->pending_elts = constructor_pending_elts;
8596   p->depth = constructor_depth;
8597   p->replacement_value.value = 0;
8598   p->replacement_value.original_code = ERROR_MARK;
8599   p->replacement_value.original_type = NULL;
8600   p->implicit = 0;
8601   p->range_stack = 0;
8602   p->outer = 0;
8603   p->incremental = constructor_incremental;
8604   p->designated = constructor_designated;
8605   p->designator_depth = designator_depth;
8606   p->next = 0;
8607   constructor_stack = p;
8608
8609   constructor_constant = 1;
8610   constructor_simple = 1;
8611   constructor_nonconst = 0;
8612   constructor_depth = SPELLING_DEPTH ();
8613   constructor_elements = NULL;
8614   constructor_pending_elts = 0;
8615   constructor_type = type;
8616   constructor_incremental = 1;
8617   constructor_designated = 0;
8618   constructor_zeroinit = 1;
8619   designator_depth = 0;
8620   designator_erroneous = 0;
8621
8622   if (RECORD_OR_UNION_TYPE_P (constructor_type))
8623     {
8624       constructor_fields = TYPE_FIELDS (constructor_type);
8625       /* Skip any nameless bit fields at the beginning.  */
8626       while (constructor_fields != NULL_TREE
8627              && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8628         constructor_fields = DECL_CHAIN (constructor_fields);
8629
8630       constructor_unfilled_fields = constructor_fields;
8631       constructor_bit_index = bitsize_zero_node;
8632     }
8633   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8634     {
8635       if (TYPE_DOMAIN (constructor_type))
8636         {
8637           constructor_max_index
8638             = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8639
8640           /* Detect non-empty initializations of zero-length arrays.  */
8641           if (constructor_max_index == NULL_TREE
8642               && TYPE_SIZE (constructor_type))
8643             constructor_max_index = integer_minus_one_node;
8644
8645           /* constructor_max_index needs to be an INTEGER_CST.  Attempts
8646              to initialize VLAs with a nonempty initializer will cause a
8647              proper error; avoid tree checking errors as well by setting a
8648              safe value.  */
8649           if (constructor_max_index
8650               && TREE_CODE (constructor_max_index) != INTEGER_CST)
8651             constructor_max_index = integer_minus_one_node;
8652
8653           constructor_index
8654             = convert (bitsizetype,
8655                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8656         }
8657       else
8658         {
8659           constructor_index = bitsize_zero_node;
8660           constructor_max_index = NULL_TREE;
8661         }
8662
8663       constructor_unfilled_index = constructor_index;
8664     }
8665   else if (gnu_vector_type_p (constructor_type))
8666     {
8667       /* Vectors are like simple fixed-size arrays.  */
8668       constructor_max_index =
8669         bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8670       constructor_index = bitsize_zero_node;
8671       constructor_unfilled_index = constructor_index;
8672     }
8673   else
8674     {
8675       /* Handle the case of int x = {5}; */
8676       constructor_fields = constructor_type;
8677       constructor_unfilled_fields = constructor_type;
8678     }
8679 }
8680 \f
8681 extern location_t last_init_list_comma;
8682
8683 /* Called when we see an open brace for a nested initializer.  Finish
8684    off any pending levels with implicit braces.  */
8685 void
8686 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8687 {
8688   while (constructor_stack->implicit)
8689     {
8690       if (RECORD_OR_UNION_TYPE_P (constructor_type)
8691           && constructor_fields == NULL_TREE)
8692         process_init_element (input_location,
8693                               pop_init_level (loc, 1, braced_init_obstack,
8694                                               last_init_list_comma),
8695                               true, braced_init_obstack);
8696       else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8697                && constructor_max_index
8698                && tree_int_cst_lt (constructor_max_index,
8699                                    constructor_index))
8700         process_init_element (input_location,
8701                               pop_init_level (loc, 1, braced_init_obstack,
8702                                               last_init_list_comma),
8703                               true, braced_init_obstack);
8704       else
8705         break;
8706     }
8707 }
8708
8709 /* Push down into a subobject, for initialization.
8710    If this is for an explicit set of braces, IMPLICIT is 0.
8711    If it is because the next element belongs at a lower level,
8712    IMPLICIT is 1 (or 2 if the push is because of designator list).  */
8713
8714 void
8715 push_init_level (location_t loc, int implicit,
8716                  struct obstack *braced_init_obstack)
8717 {
8718   struct constructor_stack *p;
8719   tree value = NULL_TREE;
8720
8721   /* Unless this is an explicit brace, we need to preserve previous
8722      content if any.  */
8723   if (implicit)
8724     {
8725       if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8726         value = find_init_member (constructor_fields, braced_init_obstack);
8727       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8728         value = find_init_member (constructor_index, braced_init_obstack);
8729     }
8730
8731   p = XNEW (struct constructor_stack);
8732   p->type = constructor_type;
8733   p->fields = constructor_fields;
8734   p->index = constructor_index;
8735   p->max_index = constructor_max_index;
8736   p->unfilled_index = constructor_unfilled_index;
8737   p->unfilled_fields = constructor_unfilled_fields;
8738   p->bit_index = constructor_bit_index;
8739   p->elements = constructor_elements;
8740   p->constant = constructor_constant;
8741   p->simple = constructor_simple;
8742   p->nonconst = constructor_nonconst;
8743   p->erroneous = constructor_erroneous;
8744   p->pending_elts = constructor_pending_elts;
8745   p->depth = constructor_depth;
8746   p->replacement_value.value = NULL_TREE;
8747   p->replacement_value.original_code = ERROR_MARK;
8748   p->replacement_value.original_type = NULL;
8749   p->implicit = implicit;
8750   p->outer = 0;
8751   p->incremental = constructor_incremental;
8752   p->designated = constructor_designated;
8753   p->designator_depth = designator_depth;
8754   p->next = constructor_stack;
8755   p->range_stack = 0;
8756   constructor_stack = p;
8757
8758   constructor_constant = 1;
8759   constructor_simple = 1;
8760   constructor_nonconst = 0;
8761   constructor_depth = SPELLING_DEPTH ();
8762   constructor_elements = NULL;
8763   constructor_incremental = 1;
8764   /* If the upper initializer is designated, then mark this as
8765      designated too to prevent bogus warnings.  */
8766   constructor_designated = p->designated;
8767   constructor_pending_elts = 0;
8768   if (!implicit)
8769     {
8770       p->range_stack = constructor_range_stack;
8771       constructor_range_stack = 0;
8772       designator_depth = 0;
8773       designator_erroneous = 0;
8774     }
8775
8776   /* Don't die if an entire brace-pair level is superfluous
8777      in the containing level.  */
8778   if (constructor_type == NULL_TREE)
8779     ;
8780   else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8781     {
8782       /* Don't die if there are extra init elts at the end.  */
8783       if (constructor_fields == NULL_TREE)
8784         constructor_type = NULL_TREE;
8785       else
8786         {
8787           constructor_type = TREE_TYPE (constructor_fields);
8788           push_member_name (constructor_fields);
8789           constructor_depth++;
8790         }
8791     }
8792   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8793     {
8794       constructor_type = TREE_TYPE (constructor_type);
8795       push_array_bounds (tree_to_uhwi (constructor_index));
8796       constructor_depth++;
8797     }
8798
8799   if (constructor_type == NULL_TREE)
8800     {
8801       error_init (loc, "extra brace group at end of initializer");
8802       constructor_fields = NULL_TREE;
8803       constructor_unfilled_fields = NULL_TREE;
8804       return;
8805     }
8806
8807   if (value && TREE_CODE (value) == CONSTRUCTOR)
8808     {
8809       constructor_constant = TREE_CONSTANT (value);
8810       constructor_simple = TREE_STATIC (value);
8811       constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8812       constructor_elements = CONSTRUCTOR_ELTS (value);
8813       if (!vec_safe_is_empty (constructor_elements)
8814           && (TREE_CODE (constructor_type) == RECORD_TYPE
8815               || TREE_CODE (constructor_type) == ARRAY_TYPE))
8816         set_nonincremental_init (braced_init_obstack);
8817     }
8818
8819   if (implicit == 1)
8820     {
8821       found_missing_braces = 1;
8822       if (initializer_stack->missing_brace_richloc)
8823         initializer_stack->missing_brace_richloc->add_fixit_insert_before
8824           (loc, "{");
8825     }
8826
8827   if (RECORD_OR_UNION_TYPE_P (constructor_type))
8828     {
8829       constructor_fields = TYPE_FIELDS (constructor_type);
8830       /* Skip any nameless bit fields at the beginning.  */
8831       while (constructor_fields != NULL_TREE
8832              && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8833         constructor_fields = DECL_CHAIN (constructor_fields);
8834
8835       constructor_unfilled_fields = constructor_fields;
8836       constructor_bit_index = bitsize_zero_node;
8837     }
8838   else if (gnu_vector_type_p (constructor_type))
8839     {
8840       /* Vectors are like simple fixed-size arrays.  */
8841       constructor_max_index =
8842         bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8843       constructor_index = bitsize_int (0);
8844       constructor_unfilled_index = constructor_index;
8845     }
8846   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8847     {
8848       if (TYPE_DOMAIN (constructor_type))
8849         {
8850           constructor_max_index
8851             = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8852
8853           /* Detect non-empty initializations of zero-length arrays.  */
8854           if (constructor_max_index == NULL_TREE
8855               && TYPE_SIZE (constructor_type))
8856             constructor_max_index = integer_minus_one_node;
8857
8858           /* constructor_max_index needs to be an INTEGER_CST.  Attempts
8859              to initialize VLAs will cause a proper error; avoid tree
8860              checking errors as well by setting a safe value.  */
8861           if (constructor_max_index
8862               && TREE_CODE (constructor_max_index) != INTEGER_CST)
8863             constructor_max_index = integer_minus_one_node;
8864
8865           constructor_index
8866             = convert (bitsizetype,
8867                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8868         }
8869       else
8870         constructor_index = bitsize_zero_node;
8871
8872       constructor_unfilled_index = constructor_index;
8873       if (value && TREE_CODE (value) == STRING_CST)
8874         {
8875           /* We need to split the char/wchar array into individual
8876              characters, so that we don't have to special case it
8877              everywhere.  */
8878           set_nonincremental_init_from_string (value, braced_init_obstack);
8879         }
8880     }
8881   else
8882     {
8883       if (constructor_type != error_mark_node)
8884         warning_init (input_location, 0, "braces around scalar initializer");
8885       constructor_fields = constructor_type;
8886       constructor_unfilled_fields = constructor_type;
8887     }
8888 }
8889
8890 /* At the end of an implicit or explicit brace level,
8891    finish up that level of constructor.  If a single expression
8892    with redundant braces initialized that level, return the
8893    c_expr structure for that expression.  Otherwise, the original_code
8894    element is set to ERROR_MARK.
8895    If we were outputting the elements as they are read, return 0 as the value
8896    from inner levels (process_init_element ignores that),
8897    but return error_mark_node as the value from the outermost level
8898    (that's what we want to put in DECL_INITIAL).
8899    Otherwise, return a CONSTRUCTOR expression as the value.  */
8900
8901 struct c_expr
8902 pop_init_level (location_t loc, int implicit,
8903                 struct obstack *braced_init_obstack,
8904                 location_t insert_before)
8905 {
8906   struct constructor_stack *p;
8907   struct c_expr ret;
8908   ret.value = NULL_TREE;
8909   ret.original_code = ERROR_MARK;
8910   ret.original_type = NULL;
8911
8912   if (implicit == 0)
8913     {
8914       /* When we come to an explicit close brace,
8915          pop any inner levels that didn't have explicit braces.  */
8916       while (constructor_stack->implicit)
8917         process_init_element (input_location,
8918                               pop_init_level (loc, 1, braced_init_obstack,
8919                                               insert_before),
8920                               true, braced_init_obstack);
8921       gcc_assert (!constructor_range_stack);
8922     }
8923   else
8924     if (initializer_stack->missing_brace_richloc)
8925       initializer_stack->missing_brace_richloc->add_fixit_insert_before
8926         (insert_before, "}");
8927
8928   /* Now output all pending elements.  */
8929   constructor_incremental = 1;
8930   output_pending_init_elements (1, braced_init_obstack);
8931
8932   p = constructor_stack;
8933
8934   /* Error for initializing a flexible array member, or a zero-length
8935      array member in an inappropriate context.  */
8936   if (constructor_type && constructor_fields
8937       && TREE_CODE (constructor_type) == ARRAY_TYPE
8938       && TYPE_DOMAIN (constructor_type)
8939       && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8940     {
8941       /* Silently discard empty initializations.  The parser will
8942          already have pedwarned for empty brackets.  */
8943       if (integer_zerop (constructor_unfilled_index))
8944         constructor_type = NULL_TREE;
8945       else
8946         {
8947           gcc_assert (!TYPE_SIZE (constructor_type));
8948
8949           if (constructor_depth > 2)
8950             error_init (loc, "initialization of flexible array member in a nested context");
8951           else
8952             pedwarn_init (loc, OPT_Wpedantic,
8953                           "initialization of a flexible array member");
8954
8955           /* We have already issued an error message for the existence
8956              of a flexible array member not at the end of the structure.
8957              Discard the initializer so that we do not die later.  */
8958           if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8959             constructor_type = NULL_TREE;
8960         }
8961     }
8962
8963   switch (vec_safe_length (constructor_elements))
8964     {
8965     case 0:
8966       /* Initialization with { } counts as zeroinit.  */
8967       constructor_zeroinit = 1;
8968       break;
8969     case 1:
8970       /* This might be zeroinit as well.  */
8971       if (integer_zerop ((*constructor_elements)[0].value))
8972         constructor_zeroinit = 1;
8973       break;
8974     default:
8975       /* If the constructor has more than one element, it can't be { 0 }.  */
8976       constructor_zeroinit = 0;
8977       break;
8978     }
8979
8980   /* Warn when some structs are initialized with direct aggregation.  */
8981   if (!implicit && found_missing_braces && warn_missing_braces
8982       && !constructor_zeroinit)
8983     {
8984       gcc_assert (initializer_stack->missing_brace_richloc);
8985       warning_at (initializer_stack->missing_brace_richloc,
8986                   OPT_Wmissing_braces,
8987                   "missing braces around initializer");
8988     }
8989
8990   /* Warn when some struct elements are implicitly initialized to zero.  */
8991   if (warn_missing_field_initializers
8992       && constructor_type
8993       && TREE_CODE (constructor_type) == RECORD_TYPE
8994       && constructor_unfilled_fields)
8995     {
8996         /* Do not warn for flexible array members or zero-length arrays.  */
8997         while (constructor_unfilled_fields
8998                && (!DECL_SIZE (constructor_unfilled_fields)
8999                    || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
9000           constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
9001
9002         if (constructor_unfilled_fields
9003             /* Do not warn if this level of the initializer uses member
9004                designators; it is likely to be deliberate.  */
9005             && !constructor_designated
9006             /* Do not warn about initializing with { 0 } or with { }.  */
9007             && !constructor_zeroinit)
9008           {
9009             if (warning_at (input_location, OPT_Wmissing_field_initializers,
9010                             "missing initializer for field %qD of %qT",
9011                             constructor_unfilled_fields,
9012                             constructor_type))
9013               inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
9014                       "%qD declared here", constructor_unfilled_fields);
9015           }
9016     }
9017
9018   /* Pad out the end of the structure.  */
9019   if (p->replacement_value.value)
9020     /* If this closes a superfluous brace pair,
9021        just pass out the element between them.  */
9022     ret = p->replacement_value;
9023   else if (constructor_type == NULL_TREE)
9024     ;
9025   else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
9026            && TREE_CODE (constructor_type) != ARRAY_TYPE
9027            && !gnu_vector_type_p (constructor_type))
9028     {
9029       /* A nonincremental scalar initializer--just return
9030          the element, after verifying there is just one.
9031          Empty scalar initializers are supported in C2X.  */
9032       if (vec_safe_is_empty (constructor_elements))
9033         {
9034           if (constructor_erroneous || constructor_type == error_mark_node)
9035             ret.value = error_mark_node;
9036           else
9037             ret.value = build_zero_cst (constructor_type);
9038         }
9039       else if (vec_safe_length (constructor_elements) != 1)
9040         {
9041           error_init (loc, "extra elements in scalar initializer");
9042           ret.value = (*constructor_elements)[0].value;
9043         }
9044       else
9045         ret.value = (*constructor_elements)[0].value;
9046     }
9047   else
9048     {
9049       if (constructor_erroneous)
9050         ret.value = error_mark_node;
9051       else
9052         {
9053           ret.value = build_constructor (constructor_type,
9054                                          constructor_elements);
9055           if (constructor_constant)
9056             TREE_CONSTANT (ret.value) = 1;
9057           if (constructor_constant && constructor_simple)
9058             TREE_STATIC (ret.value) = 1;
9059           if (constructor_nonconst)
9060             CONSTRUCTOR_NON_CONST (ret.value) = 1;
9061         }
9062     }
9063
9064   if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9065     {
9066       if (constructor_nonconst)
9067         ret.original_code = C_MAYBE_CONST_EXPR;
9068       else if (ret.original_code == C_MAYBE_CONST_EXPR)
9069         ret.original_code = ERROR_MARK;
9070     }
9071
9072   constructor_type = p->type;
9073   constructor_fields = p->fields;
9074   constructor_index = p->index;
9075   constructor_max_index = p->max_index;
9076   constructor_unfilled_index = p->unfilled_index;
9077   constructor_unfilled_fields = p->unfilled_fields;
9078   constructor_bit_index = p->bit_index;
9079   constructor_elements = p->elements;
9080   constructor_constant = p->constant;
9081   constructor_simple = p->simple;
9082   constructor_nonconst = p->nonconst;
9083   constructor_erroneous = p->erroneous;
9084   constructor_incremental = p->incremental;
9085   constructor_designated = p->designated;
9086   designator_depth = p->designator_depth;
9087   constructor_pending_elts = p->pending_elts;
9088   constructor_depth = p->depth;
9089   if (!p->implicit)
9090     constructor_range_stack = p->range_stack;
9091   RESTORE_SPELLING_DEPTH (constructor_depth);
9092
9093   constructor_stack = p->next;
9094   XDELETE (p);
9095
9096   if (ret.value == NULL_TREE && constructor_stack == 0)
9097     ret.value = error_mark_node;
9098   return ret;
9099 }
9100
9101 /* Common handling for both array range and field name designators.
9102    ARRAY argument is nonzero for array ranges.  Returns false for success.  */
9103
9104 static bool
9105 set_designator (location_t loc, bool array,
9106                 struct obstack *braced_init_obstack)
9107 {
9108   tree subtype;
9109   enum tree_code subcode;
9110
9111   /* Don't die if an entire brace-pair level is superfluous
9112      in the containing level, or for an erroneous type.  */
9113   if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9114     return true;
9115
9116   /* If there were errors in this designator list already, bail out
9117      silently.  */
9118   if (designator_erroneous)
9119     return true;
9120
9121   /* Likewise for an initializer for a variable-size type.  Those are
9122      diagnosed in the parser, except for empty initializer braces.  */
9123   if (COMPLETE_TYPE_P (constructor_type)
9124       && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9125     return true;
9126
9127   if (!designator_depth)
9128     {
9129       gcc_assert (!constructor_range_stack);
9130
9131       /* Designator list starts at the level of closest explicit
9132          braces.  */
9133       while (constructor_stack->implicit)
9134         process_init_element (input_location,
9135                               pop_init_level (loc, 1, braced_init_obstack,
9136                                               last_init_list_comma),
9137                               true, braced_init_obstack);
9138       constructor_designated = 1;
9139       return false;
9140     }
9141
9142   switch (TREE_CODE (constructor_type))
9143     {
9144     case  RECORD_TYPE:
9145     case  UNION_TYPE:
9146       subtype = TREE_TYPE (constructor_fields);
9147       if (subtype != error_mark_node)
9148         subtype = TYPE_MAIN_VARIANT (subtype);
9149       break;
9150     case ARRAY_TYPE:
9151       subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9152       break;
9153     default:
9154       gcc_unreachable ();
9155     }
9156
9157   subcode = TREE_CODE (subtype);
9158   if (array && subcode != ARRAY_TYPE)
9159     {
9160       error_init (loc, "array index in non-array initializer");
9161       return true;
9162     }
9163   else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9164     {
9165       error_init (loc, "field name not in record or union initializer");
9166       return true;
9167     }
9168
9169   constructor_designated = 1;
9170   finish_implicit_inits (loc, braced_init_obstack);
9171   push_init_level (loc, 2, braced_init_obstack);
9172   return false;
9173 }
9174
9175 /* If there are range designators in designator list, push a new designator
9176    to constructor_range_stack.  RANGE_END is end of such stack range or
9177    NULL_TREE if there is no range designator at this level.  */
9178
9179 static void
9180 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9181 {
9182   struct constructor_range_stack *p;
9183
9184   p = (struct constructor_range_stack *)
9185     obstack_alloc (braced_init_obstack,
9186                    sizeof (struct constructor_range_stack));
9187   p->prev = constructor_range_stack;
9188   p->next = 0;
9189   p->fields = constructor_fields;
9190   p->range_start = constructor_index;
9191   p->index = constructor_index;
9192   p->stack = constructor_stack;
9193   p->range_end = range_end;
9194   if (constructor_range_stack)
9195     constructor_range_stack->next = p;
9196   constructor_range_stack = p;
9197 }
9198
9199 /* Within an array initializer, specify the next index to be initialized.
9200    FIRST is that index.  If LAST is nonzero, then initialize a range
9201    of indices, running from FIRST through LAST.  */
9202
9203 void
9204 set_init_index (location_t loc, tree first, tree last,
9205                 struct obstack *braced_init_obstack)
9206 {
9207   if (set_designator (loc, true, braced_init_obstack))
9208     return;
9209
9210   designator_erroneous = 1;
9211
9212   if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9213       || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9214     {
9215       error_init (loc, "array index in initializer not of integer type");
9216       return;
9217     }
9218
9219   if (TREE_CODE (first) != INTEGER_CST)
9220     {
9221       first = c_fully_fold (first, false, NULL);
9222       if (TREE_CODE (first) == INTEGER_CST)
9223         pedwarn_init (loc, OPT_Wpedantic,
9224                       "array index in initializer is not "
9225                       "an integer constant expression");
9226     }
9227
9228   if (last && TREE_CODE (last) != INTEGER_CST)
9229     {
9230       last = c_fully_fold (last, false, NULL);
9231       if (TREE_CODE (last) == INTEGER_CST)
9232         pedwarn_init (loc, OPT_Wpedantic,
9233                       "array index in initializer is not "
9234                       "an integer constant expression");
9235     }
9236
9237   if (TREE_CODE (first) != INTEGER_CST)
9238     error_init (loc, "nonconstant array index in initializer");
9239   else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9240     error_init (loc, "nonconstant array index in initializer");
9241   else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9242     error_init (loc, "array index in non-array initializer");
9243   else if (tree_int_cst_sgn (first) == -1)
9244     error_init (loc, "array index in initializer exceeds array bounds");
9245   else if (constructor_max_index
9246            && tree_int_cst_lt (constructor_max_index, first))
9247     error_init (loc, "array index in initializer exceeds array bounds");
9248   else
9249     {
9250       constant_expression_warning (first);
9251       if (last)
9252         constant_expression_warning (last);
9253       constructor_index = convert (bitsizetype, first);
9254       if (tree_int_cst_lt (constructor_index, first))
9255         {
9256           constructor_index = copy_node (constructor_index);
9257           TREE_OVERFLOW (constructor_index) = 1;
9258         }
9259
9260       if (last)
9261         {
9262           if (tree_int_cst_equal (first, last))
9263             last = NULL_TREE;
9264           else if (tree_int_cst_lt (last, first))
9265             {
9266               error_init (loc, "empty index range in initializer");
9267               last = NULL_TREE;
9268             }
9269           else
9270             {
9271               last = convert (bitsizetype, last);
9272               if (constructor_max_index != NULL_TREE
9273                   && tree_int_cst_lt (constructor_max_index, last))
9274                 {
9275                   error_init (loc, "array index range in initializer exceeds "
9276                               "array bounds");
9277                   last = NULL_TREE;
9278                 }
9279             }
9280         }
9281
9282       designator_depth++;
9283       designator_erroneous = 0;
9284       if (constructor_range_stack || last)
9285         push_range_stack (last, braced_init_obstack);
9286     }
9287 }
9288
9289 /* Within a struct initializer, specify the next field to be initialized.  */
9290
9291 void
9292 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9293                 struct obstack *braced_init_obstack)
9294 {
9295   tree field;
9296
9297   if (set_designator (loc, false, braced_init_obstack))
9298     return;
9299
9300   designator_erroneous = 1;
9301
9302   if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9303     {
9304       error_init (loc, "field name not in record or union initializer");
9305       return;
9306     }
9307
9308   field = lookup_field (constructor_type, fieldname);
9309
9310   if (field == NULL_TREE)
9311     {
9312       tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9313       if (guessed_id)
9314         {
9315           gcc_rich_location rich_loc (fieldname_loc);
9316           rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9317           error_at (&rich_loc,
9318                     "%qT has no member named %qE; did you mean %qE?",
9319                     constructor_type, fieldname, guessed_id);
9320         }
9321       else
9322         error_at (fieldname_loc, "%qT has no member named %qE",
9323                   constructor_type, fieldname);
9324     }
9325   else
9326     do
9327       {
9328         constructor_fields = TREE_VALUE (field);
9329         designator_depth++;
9330         designator_erroneous = 0;
9331         if (constructor_range_stack)
9332           push_range_stack (NULL_TREE, braced_init_obstack);
9333         field = TREE_CHAIN (field);
9334         if (field)
9335           {
9336             if (set_designator (loc, false, braced_init_obstack))
9337               return;
9338           }
9339       }
9340     while (field != NULL_TREE);
9341 }
9342 \f
9343 /* Add a new initializer to the tree of pending initializers.  PURPOSE
9344    identifies the initializer, either array index or field in a structure.
9345    VALUE is the value of that index or field.  If ORIGTYPE is not
9346    NULL_TREE, it is the original type of VALUE.
9347
9348    IMPLICIT is true if value comes from pop_init_level (1),
9349    the new initializer has been merged with the existing one
9350    and thus no warnings should be emitted about overriding an
9351    existing initializer.  */
9352
9353 static void
9354 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9355                   bool implicit, struct obstack *braced_init_obstack)
9356 {
9357   struct init_node *p, **q, *r;
9358
9359   q = &constructor_pending_elts;
9360   p = 0;
9361
9362   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9363     {
9364       while (*q != 0)
9365         {
9366           p = *q;
9367           if (tree_int_cst_lt (purpose, p->purpose))
9368             q = &p->left;
9369           else if (tree_int_cst_lt (p->purpose, purpose))
9370             q = &p->right;
9371           else
9372             {
9373               if (!implicit)
9374                 {
9375                   if (TREE_SIDE_EFFECTS (p->value))
9376                     warning_init (loc, OPT_Woverride_init_side_effects,
9377                                   "initialized field with side-effects "
9378                                   "overwritten");
9379                   else if (warn_override_init)
9380                     warning_init (loc, OPT_Woverride_init,
9381                                   "initialized field overwritten");
9382                 }
9383               p->value = value;
9384               p->origtype = origtype;
9385               return;
9386             }
9387         }
9388     }
9389   else
9390     {
9391       tree bitpos;
9392
9393       bitpos = bit_position (purpose);
9394       while (*q != NULL)
9395         {
9396           p = *q;
9397           if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9398             q = &p->left;
9399           else if (p->purpose != purpose)
9400             q = &p->right;
9401           else
9402             {
9403               if (!implicit)
9404                 {
9405                   if (TREE_SIDE_EFFECTS (p->value))
9406                     warning_init (loc, OPT_Woverride_init_side_effects,
9407                                   "initialized field with side-effects "
9408                                   "overwritten");
9409                   else if (warn_override_init)
9410                     warning_init (loc, OPT_Woverride_init,
9411                                   "initialized field overwritten");
9412                 }
9413               p->value = value;
9414               p->origtype = origtype;
9415               return;
9416             }
9417         }
9418     }
9419
9420   r = (struct init_node *) obstack_alloc (braced_init_obstack,
9421                                           sizeof (struct init_node));
9422   r->purpose = purpose;
9423   r->value = value;
9424   r->origtype = origtype;
9425
9426   *q = r;
9427   r->parent = p;
9428   r->left = 0;
9429   r->right = 0;
9430   r->balance = 0;
9431
9432   while (p)
9433     {
9434       struct init_node *s;
9435
9436       if (r == p->left)
9437         {
9438           if (p->balance == 0)
9439             p->balance = -1;
9440           else if (p->balance < 0)
9441             {
9442               if (r->balance < 0)
9443                 {
9444                   /* L rotation.  */
9445                   p->left = r->right;
9446                   if (p->left)
9447                     p->left->parent = p;
9448                   r->right = p;
9449
9450                   p->balance = 0;
9451                   r->balance = 0;
9452
9453                   s = p->parent;
9454                   p->parent = r;
9455                   r->parent = s;
9456                   if (s)
9457                     {
9458                       if (s->left == p)
9459                         s->left = r;
9460                       else
9461                         s->right = r;
9462                     }
9463                   else
9464                     constructor_pending_elts = r;
9465                 }
9466               else
9467                 {
9468                   /* LR rotation.  */
9469                   struct init_node *t = r->right;
9470
9471                   r->right = t->left;
9472                   if (r->right)
9473                     r->right->parent = r;
9474                   t->left = r;
9475
9476                   p->left = t->right;
9477                   if (p->left)
9478                     p->left->parent = p;
9479                   t->right = p;
9480
9481                   p->balance = t->balance < 0;
9482                   r->balance = -(t->balance > 0);
9483                   t->balance = 0;
9484
9485                   s = p->parent;
9486                   p->parent = t;
9487                   r->parent = t;
9488                   t->parent = s;
9489                   if (s)
9490                     {
9491                       if (s->left == p)
9492                         s->left = t;
9493                       else
9494                         s->right = t;
9495                     }
9496                   else
9497                     constructor_pending_elts = t;
9498                 }
9499               break;
9500             }
9501           else
9502             {
9503               /* p->balance == +1; growth of left side balances the node.  */
9504               p->balance = 0;
9505               break;
9506             }
9507         }
9508       else /* r == p->right */
9509         {
9510           if (p->balance == 0)
9511             /* Growth propagation from right side.  */
9512             p->balance++;
9513           else if (p->balance > 0)
9514             {
9515               if (r->balance > 0)
9516                 {
9517                   /* R rotation.  */
9518                   p->right = r->left;
9519                   if (p->right)
9520                     p->right->parent = p;
9521                   r->left = p;
9522
9523                   p->balance = 0;
9524                   r->balance = 0;
9525
9526                   s = p->parent;
9527                   p->parent = r;
9528                   r->parent = s;
9529                   if (s)
9530                     {
9531                       if (s->left == p)
9532                         s->left = r;
9533                       else
9534                         s->right = r;
9535                     }
9536                   else
9537                     constructor_pending_elts = r;
9538                 }
9539               else /* r->balance == -1 */
9540                 {
9541                   /* RL rotation */
9542                   struct init_node *t = r->left;
9543
9544                   r->left = t->right;
9545                   if (r->left)
9546                     r->left->parent = r;
9547                   t->right = r;
9548
9549                   p->right = t->left;
9550                   if (p->right)
9551                     p->right->parent = p;
9552                   t->left = p;
9553
9554                   r->balance = (t->balance < 0);
9555                   p->balance = -(t->balance > 0);
9556                   t->balance = 0;
9557
9558                   s = p->parent;
9559                   p->parent = t;
9560                   r->parent = t;
9561                   t->parent = s;
9562                   if (s)
9563                     {
9564                       if (s->left == p)
9565                         s->left = t;
9566                       else
9567                         s->right = t;
9568                     }
9569                   else
9570                     constructor_pending_elts = t;
9571                 }
9572               break;
9573             }
9574           else
9575             {
9576               /* p->balance == -1; growth of right side balances the node.  */
9577               p->balance = 0;
9578               break;
9579             }
9580         }
9581
9582       r = p;
9583       p = p->parent;
9584     }
9585 }
9586
9587 /* Build AVL tree from a sorted chain.  */
9588
9589 static void
9590 set_nonincremental_init (struct obstack * braced_init_obstack)
9591 {
9592   unsigned HOST_WIDE_INT ix;
9593   tree index, value;
9594
9595   if (TREE_CODE (constructor_type) != RECORD_TYPE
9596       && TREE_CODE (constructor_type) != ARRAY_TYPE)
9597     return;
9598
9599   FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9600     add_pending_init (input_location, index, value, NULL_TREE, true,
9601                       braced_init_obstack);
9602   constructor_elements = NULL;
9603   if (TREE_CODE (constructor_type) == RECORD_TYPE)
9604     {
9605       constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9606       /* Skip any nameless bit fields at the beginning.  */
9607       while (constructor_unfilled_fields != NULL_TREE
9608              && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9609         constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9610
9611     }
9612   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9613     {
9614       if (TYPE_DOMAIN (constructor_type))
9615         constructor_unfilled_index
9616             = convert (bitsizetype,
9617                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9618       else
9619         constructor_unfilled_index = bitsize_zero_node;
9620     }
9621   constructor_incremental = 0;
9622 }
9623
9624 /* Build AVL tree from a string constant.  */
9625
9626 static void
9627 set_nonincremental_init_from_string (tree str,
9628                                      struct obstack * braced_init_obstack)
9629 {
9630   tree value, purpose, type;
9631   HOST_WIDE_INT val[2];
9632   const char *p, *end;
9633   int byte, wchar_bytes, charwidth, bitpos;
9634
9635   gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9636
9637   wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9638   charwidth = TYPE_PRECISION (char_type_node);
9639   gcc_assert ((size_t) wchar_bytes * charwidth
9640               <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9641   type = TREE_TYPE (constructor_type);
9642   p = TREE_STRING_POINTER (str);
9643   end = p + TREE_STRING_LENGTH (str);
9644
9645   for (purpose = bitsize_zero_node;
9646        p < end
9647        && !(constructor_max_index
9648             && tree_int_cst_lt (constructor_max_index, purpose));
9649        purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9650     {
9651       if (wchar_bytes == 1)
9652         {
9653           val[0] = (unsigned char) *p++;
9654           val[1] = 0;
9655         }
9656       else
9657         {
9658           val[1] = 0;
9659           val[0] = 0;
9660           for (byte = 0; byte < wchar_bytes; byte++)
9661             {
9662               if (BYTES_BIG_ENDIAN)
9663                 bitpos = (wchar_bytes - byte - 1) * charwidth;
9664               else
9665                 bitpos = byte * charwidth;
9666               val[bitpos / HOST_BITS_PER_WIDE_INT]
9667                 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9668                    << (bitpos % HOST_BITS_PER_WIDE_INT);
9669             }
9670         }
9671
9672       if (!TYPE_UNSIGNED (type))
9673         {
9674           bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9675           if (bitpos < HOST_BITS_PER_WIDE_INT)
9676             {
9677               if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9678                 {
9679                   val[0] |= HOST_WIDE_INT_M1U << bitpos;
9680                   val[1] = -1;
9681                 }
9682             }
9683           else if (bitpos == HOST_BITS_PER_WIDE_INT)
9684             {
9685               if (val[0] < 0)
9686                 val[1] = -1;
9687             }
9688           else if (val[1] & (HOST_WIDE_INT_1
9689                              << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9690             val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9691         }
9692
9693       value = wide_int_to_tree (type,
9694                                 wide_int::from_array (val, 2,
9695                                                       HOST_BITS_PER_WIDE_INT * 2));
9696       add_pending_init (input_location, purpose, value, NULL_TREE, true,
9697                         braced_init_obstack);
9698     }
9699
9700   constructor_incremental = 0;
9701 }
9702
9703 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9704    not initialized yet.  */
9705
9706 static tree
9707 find_init_member (tree field, struct obstack * braced_init_obstack)
9708 {
9709   struct init_node *p;
9710
9711   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9712     {
9713       if (constructor_incremental
9714           && tree_int_cst_lt (field, constructor_unfilled_index))
9715         set_nonincremental_init (braced_init_obstack);
9716
9717       p = constructor_pending_elts;
9718       while (p)
9719         {
9720           if (tree_int_cst_lt (field, p->purpose))
9721             p = p->left;
9722           else if (tree_int_cst_lt (p->purpose, field))
9723             p = p->right;
9724           else
9725             return p->value;
9726         }
9727     }
9728   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9729     {
9730       tree bitpos = bit_position (field);
9731
9732       if (constructor_incremental
9733           && (!constructor_unfilled_fields
9734               || tree_int_cst_lt (bitpos,
9735                                   bit_position (constructor_unfilled_fields))))
9736         set_nonincremental_init (braced_init_obstack);
9737
9738       p = constructor_pending_elts;
9739       while (p)
9740         {
9741           if (field == p->purpose)
9742             return p->value;
9743           else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9744             p = p->left;
9745           else
9746             p = p->right;
9747         }
9748     }
9749   else if (TREE_CODE (constructor_type) == UNION_TYPE)
9750     {
9751       if (!vec_safe_is_empty (constructor_elements)
9752           && (constructor_elements->last ().index == field))
9753         return constructor_elements->last ().value;
9754     }
9755   return NULL_TREE;
9756 }
9757
9758 /* "Output" the next constructor element.
9759    At top level, really output it to assembler code now.
9760    Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9761    If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9762    TYPE is the data type that the containing data type wants here.
9763    FIELD is the field (a FIELD_DECL) or the index that this element fills.
9764    If VALUE is a string constant, STRICT_STRING is true if it is
9765    unparenthesized or we should not warn here for it being parenthesized.
9766    For other types of VALUE, STRICT_STRING is not used.
9767
9768    PENDING if true means output pending elements that belong
9769    right after this element.  (PENDING is normally true;
9770    it is false while outputting pending elements, to avoid recursion.)
9771
9772    IMPLICIT is true if value comes from pop_init_level (1),
9773    the new initializer has been merged with the existing one
9774    and thus no warnings should be emitted about overriding an
9775    existing initializer.  */
9776
9777 static void
9778 output_init_element (location_t loc, tree value, tree origtype,
9779                      bool strict_string, tree type, tree field, bool pending,
9780                      bool implicit, struct obstack * braced_init_obstack)
9781 {
9782   tree semantic_type = NULL_TREE;
9783   bool maybe_const = true;
9784   bool npc;
9785
9786   if (type == error_mark_node || value == error_mark_node)
9787     {
9788       constructor_erroneous = 1;
9789       return;
9790     }
9791   if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9792       && (TREE_CODE (value) == STRING_CST
9793           || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9794       && !(TREE_CODE (value) == STRING_CST
9795            && TREE_CODE (type) == ARRAY_TYPE
9796            && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9797       && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9798                      TYPE_MAIN_VARIANT (type)))
9799     value = array_to_pointer_conversion (input_location, value);
9800
9801   if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9802       && require_constant_value && pending)
9803     {
9804       /* As an extension, allow initializing objects with static storage
9805          duration with compound literals (which are then treated just as
9806          the brace enclosed list they contain).  */
9807       if (flag_isoc99)
9808         pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9809                       "constant");
9810       tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9811       value = DECL_INITIAL (decl);
9812     }
9813
9814   npc = null_pointer_constant_p (value);
9815   if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9816     {
9817       semantic_type = TREE_TYPE (value);
9818       value = TREE_OPERAND (value, 0);
9819     }
9820   value = c_fully_fold (value, require_constant_value, &maybe_const);
9821
9822   if (value == error_mark_node)
9823     constructor_erroneous = 1;
9824   else if (!TREE_CONSTANT (value))
9825     constructor_constant = 0;
9826   else if (!initializer_constant_valid_p (value,
9827                                           TREE_TYPE (value),
9828                                           AGGREGATE_TYPE_P (constructor_type)
9829                                           && TYPE_REVERSE_STORAGE_ORDER
9830                                              (constructor_type))
9831            || (RECORD_OR_UNION_TYPE_P (constructor_type)
9832                && DECL_C_BIT_FIELD (field)
9833                && TREE_CODE (value) != INTEGER_CST))
9834     constructor_simple = 0;
9835   if (!maybe_const)
9836     constructor_nonconst = 1;
9837
9838   /* Digest the initializer and issue any errors about incompatible
9839      types before issuing errors about non-constant initializers.  */
9840   tree new_value = value;
9841   if (semantic_type)
9842     new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9843   new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9844                            require_constant_value);
9845   if (new_value == error_mark_node)
9846     {
9847       constructor_erroneous = 1;
9848       return;
9849     }
9850   if (require_constant_value || require_constant_elements)
9851     constant_expression_warning (new_value);
9852
9853   /* Proceed to check the constness of the original initializer.  */
9854   if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9855     {
9856       if (require_constant_value)
9857         {
9858           error_init (loc, "initializer element is not constant");
9859           value = error_mark_node;
9860         }
9861       else if (require_constant_elements)
9862         pedwarn (loc, OPT_Wpedantic,
9863                  "initializer element is not computable at load time");
9864     }
9865   else if (!maybe_const
9866            && (require_constant_value || require_constant_elements))
9867     pedwarn_init (loc, OPT_Wpedantic,
9868                   "initializer element is not a constant expression");
9869
9870   /* Issue -Wc++-compat warnings about initializing a bitfield with
9871      enum type.  */
9872   if (warn_cxx_compat
9873       && field != NULL_TREE
9874       && TREE_CODE (field) == FIELD_DECL
9875       && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9876       && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9877           != TYPE_MAIN_VARIANT (type))
9878       && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9879     {
9880       tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9881       if (checktype != error_mark_node
9882           && (TYPE_MAIN_VARIANT (checktype)
9883               != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9884         warning_init (loc, OPT_Wc___compat,
9885                       "enum conversion in initialization is invalid in C++");
9886     }
9887
9888   /* If this field is empty and does not have side effects (and is not at
9889      the end of structure), don't do anything other than checking the
9890      initializer.  */
9891   if (field
9892       && (TREE_TYPE (field) == error_mark_node
9893           || (COMPLETE_TYPE_P (TREE_TYPE (field))
9894               && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9895               && !TREE_SIDE_EFFECTS (new_value)
9896               && (TREE_CODE (constructor_type) == ARRAY_TYPE
9897                   || DECL_CHAIN (field)))))
9898     return;
9899
9900   /* Finally, set VALUE to the initializer value digested above.  */
9901   value = new_value;
9902
9903   /* If this element doesn't come next in sequence,
9904      put it on constructor_pending_elts.  */
9905   if (TREE_CODE (constructor_type) == ARRAY_TYPE
9906       && (!constructor_incremental
9907           || !tree_int_cst_equal (field, constructor_unfilled_index)))
9908     {
9909       if (constructor_incremental
9910           && tree_int_cst_lt (field, constructor_unfilled_index))
9911         set_nonincremental_init (braced_init_obstack);
9912
9913       add_pending_init (loc, field, value, origtype, implicit,
9914                         braced_init_obstack);
9915       return;
9916     }
9917   else if (TREE_CODE (constructor_type) == RECORD_TYPE
9918            && (!constructor_incremental
9919                || field != constructor_unfilled_fields))
9920     {
9921       /* We do this for records but not for unions.  In a union,
9922          no matter which field is specified, it can be initialized
9923          right away since it starts at the beginning of the union.  */
9924       if (constructor_incremental)
9925         {
9926           if (!constructor_unfilled_fields)
9927             set_nonincremental_init (braced_init_obstack);
9928           else
9929             {
9930               tree bitpos, unfillpos;
9931
9932               bitpos = bit_position (field);
9933               unfillpos = bit_position (constructor_unfilled_fields);
9934
9935               if (tree_int_cst_lt (bitpos, unfillpos))
9936                 set_nonincremental_init (braced_init_obstack);
9937             }
9938         }
9939
9940       add_pending_init (loc, field, value, origtype, implicit,
9941                         braced_init_obstack);
9942       return;
9943     }
9944   else if (TREE_CODE (constructor_type) == UNION_TYPE
9945            && !vec_safe_is_empty (constructor_elements))
9946     {
9947       if (!implicit)
9948         {
9949           if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9950             warning_init (loc, OPT_Woverride_init_side_effects,
9951                           "initialized field with side-effects overwritten");
9952           else if (warn_override_init)
9953             warning_init (loc, OPT_Woverride_init,
9954                           "initialized field overwritten");
9955         }
9956
9957       /* We can have just one union field set.  */
9958       constructor_elements = NULL;
9959     }
9960
9961   /* Otherwise, output this element either to
9962      constructor_elements or to the assembler file.  */
9963
9964   constructor_elt celt = {field, value};
9965   vec_safe_push (constructor_elements, celt);
9966
9967   /* Advance the variable that indicates sequential elements output.  */
9968   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9969     constructor_unfilled_index
9970       = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9971                         bitsize_one_node);
9972   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9973     {
9974       constructor_unfilled_fields
9975         = DECL_CHAIN (constructor_unfilled_fields);
9976
9977       /* Skip any nameless bit fields.  */
9978       while (constructor_unfilled_fields != NULL_TREE
9979              && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9980         constructor_unfilled_fields =
9981           DECL_CHAIN (constructor_unfilled_fields);
9982     }
9983   else if (TREE_CODE (constructor_type) == UNION_TYPE)
9984     constructor_unfilled_fields = NULL_TREE;
9985
9986   /* Now output any pending elements which have become next.  */
9987   if (pending)
9988     output_pending_init_elements (0, braced_init_obstack);
9989 }
9990
9991 /* For two FIELD_DECLs in the same chain, return -1 if field1
9992    comes before field2, 1 if field1 comes after field2 and
9993    0 if field1 == field2.  */
9994
9995 static int
9996 init_field_decl_cmp (tree field1, tree field2)
9997 {
9998   if (field1 == field2)
9999     return 0;
10000
10001   tree bitpos1 = bit_position (field1);
10002   tree bitpos2 = bit_position (field2);
10003   if (tree_int_cst_equal (bitpos1, bitpos2))
10004     {
10005       /* If one of the fields has non-zero bitsize, then that
10006          field must be the last one in a sequence of zero
10007          sized fields, fields after it will have bigger
10008          bit_position.  */
10009       if (TREE_TYPE (field1) != error_mark_node
10010           && COMPLETE_TYPE_P (TREE_TYPE (field1))
10011           && integer_nonzerop (TREE_TYPE (field1)))
10012         return 1;
10013       if (TREE_TYPE (field2) != error_mark_node
10014           && COMPLETE_TYPE_P (TREE_TYPE (field2))
10015           && integer_nonzerop (TREE_TYPE (field2)))
10016         return -1;
10017       /* Otherwise, fallback to DECL_CHAIN walk to find out
10018          which field comes earlier.  Walk chains of both
10019          fields, so that if field1 and field2 are close to each
10020          other in either order, it is found soon even for large
10021          sequences of zero sized fields.  */
10022       tree f1 = field1, f2 = field2;
10023       while (1)
10024         {
10025           f1 = DECL_CHAIN (f1);
10026           f2 = DECL_CHAIN (f2);
10027           if (f1 == NULL_TREE)
10028             {
10029               gcc_assert (f2);
10030               return 1;
10031             }
10032           if (f2 == NULL_TREE)
10033             return -1;
10034           if (f1 == field2)
10035             return -1;
10036           if (f2 == field1)
10037             return 1;
10038           if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10039             return 1;
10040           if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10041             return -1;
10042         }
10043     }
10044   else if (tree_int_cst_lt (bitpos1, bitpos2))
10045     return -1;
10046   else
10047     return 1;
10048 }
10049
10050 /* Output any pending elements which have become next.
10051    As we output elements, constructor_unfilled_{fields,index}
10052    advances, which may cause other elements to become next;
10053    if so, they too are output.
10054
10055    If ALL is 0, we return when there are
10056    no more pending elements to output now.
10057
10058    If ALL is 1, we output space as necessary so that
10059    we can output all the pending elements.  */
10060 static void
10061 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10062 {
10063   struct init_node *elt = constructor_pending_elts;
10064   tree next;
10065
10066  retry:
10067
10068   /* Look through the whole pending tree.
10069      If we find an element that should be output now,
10070      output it.  Otherwise, set NEXT to the element
10071      that comes first among those still pending.  */
10072
10073   next = NULL_TREE;
10074   while (elt)
10075     {
10076       if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10077         {
10078           if (tree_int_cst_equal (elt->purpose,
10079                                   constructor_unfilled_index))
10080             output_init_element (input_location, elt->value, elt->origtype,
10081                                  true, TREE_TYPE (constructor_type),
10082                                  constructor_unfilled_index, false, false,
10083                                  braced_init_obstack);
10084           else if (tree_int_cst_lt (constructor_unfilled_index,
10085                                     elt->purpose))
10086             {
10087               /* Advance to the next smaller node.  */
10088               if (elt->left)
10089                 elt = elt->left;
10090               else
10091                 {
10092                   /* We have reached the smallest node bigger than the
10093                      current unfilled index.  Fill the space first.  */
10094                   next = elt->purpose;
10095                   break;
10096                 }
10097             }
10098           else
10099             {
10100               /* Advance to the next bigger node.  */
10101               if (elt->right)
10102                 elt = elt->right;
10103               else
10104                 {
10105                   /* We have reached the biggest node in a subtree.  Find
10106                      the parent of it, which is the next bigger node.  */
10107                   while (elt->parent && elt->parent->right == elt)
10108                     elt = elt->parent;
10109                   elt = elt->parent;
10110                   if (elt && tree_int_cst_lt (constructor_unfilled_index,
10111                                               elt->purpose))
10112                     {
10113                       next = elt->purpose;
10114                       break;
10115                     }
10116                 }
10117             }
10118         }
10119       else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10120         {
10121           /* If the current record is complete we are done.  */
10122           if (constructor_unfilled_fields == NULL_TREE)
10123             break;
10124
10125           int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10126                                          elt->purpose);
10127           if (cmp == 0)
10128             output_init_element (input_location, elt->value, elt->origtype,
10129                                  true, TREE_TYPE (elt->purpose),
10130                                  elt->purpose, false, false,
10131                                  braced_init_obstack);
10132           else if (cmp < 0)
10133             {
10134               /* Advance to the next smaller node.  */
10135               if (elt->left)
10136                 elt = elt->left;
10137               else
10138                 {
10139                   /* We have reached the smallest node bigger than the
10140                      current unfilled field.  Fill the space first.  */
10141                   next = elt->purpose;
10142                   break;
10143                 }
10144             }
10145           else
10146             {
10147               /* Advance to the next bigger node.  */
10148               if (elt->right)
10149                 elt = elt->right;
10150               else
10151                 {
10152                   /* We have reached the biggest node in a subtree.  Find
10153                      the parent of it, which is the next bigger node.  */
10154                   while (elt->parent && elt->parent->right == elt)
10155                     elt = elt->parent;
10156                   elt = elt->parent;
10157                   if (elt
10158                       && init_field_decl_cmp (constructor_unfilled_fields,
10159                                               elt->purpose) < 0)
10160                     {
10161                       next = elt->purpose;
10162                       break;
10163                     }
10164                 }
10165             }
10166         }
10167     }
10168
10169   /* Ordinarily return, but not if we want to output all
10170      and there are elements left.  */
10171   if (!(all && next != NULL_TREE))
10172     return;
10173
10174   /* If it's not incremental, just skip over the gap, so that after
10175      jumping to retry we will output the next successive element.  */
10176   if (RECORD_OR_UNION_TYPE_P (constructor_type))
10177     constructor_unfilled_fields = next;
10178   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10179     constructor_unfilled_index = next;
10180
10181   /* ELT now points to the node in the pending tree with the next
10182      initializer to output.  */
10183   goto retry;
10184 }
10185 \f
10186 /* Expression VALUE coincides with the start of type TYPE in a braced
10187    initializer.  Return true if we should treat VALUE as initializing
10188    the first element of TYPE, false if we should treat it as initializing
10189    TYPE as a whole.
10190
10191    If the initializer is clearly invalid, the question becomes:
10192    which choice gives the best error message?  */
10193
10194 static bool
10195 initialize_elementwise_p (tree type, tree value)
10196 {
10197   if (type == error_mark_node || value == error_mark_node)
10198     return false;
10199
10200   gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10201
10202   tree value_type = TREE_TYPE (value);
10203   if (value_type == error_mark_node)
10204     return false;
10205
10206   /* GNU vectors can be initialized elementwise.  However, treat any
10207      kind of vector value as initializing the vector type as a whole,
10208      regardless of whether the value is a GNU vector.  Such initializers
10209      are valid if and only if they would have been valid in a non-braced
10210      initializer like:
10211
10212         TYPE foo = VALUE;
10213
10214      so recursing into the vector type would be at best confusing or at
10215      worst wrong.  For example, when -flax-vector-conversions is in effect,
10216      it's possible to initialize a V8HI from a V4SI, even though the vectors
10217      have different element types and different numbers of elements.  */
10218   if (gnu_vector_type_p (type))
10219     return !VECTOR_TYPE_P (value_type);
10220
10221   if (AGGREGATE_TYPE_P (type))
10222     return type != TYPE_MAIN_VARIANT (value_type);
10223
10224   return false;
10225 }
10226
10227 /* Add one non-braced element to the current constructor level.
10228    This adjusts the current position within the constructor's type.
10229    This may also start or terminate implicit levels
10230    to handle a partly-braced initializer.
10231
10232    Once this has found the correct level for the new element,
10233    it calls output_init_element.
10234
10235    IMPLICIT is true if value comes from pop_init_level (1),
10236    the new initializer has been merged with the existing one
10237    and thus no warnings should be emitted about overriding an
10238    existing initializer.  */
10239
10240 void
10241 process_init_element (location_t loc, struct c_expr value, bool implicit,
10242                       struct obstack * braced_init_obstack)
10243 {
10244   tree orig_value = value.value;
10245   int string_flag
10246     = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10247   bool strict_string = value.original_code == STRING_CST;
10248   bool was_designated = designator_depth != 0;
10249
10250   designator_depth = 0;
10251   designator_erroneous = 0;
10252
10253   if (!implicit && value.value && !integer_zerop (value.value))
10254     constructor_zeroinit = 0;
10255
10256   /* Handle superfluous braces around string cst as in
10257      char x[] = {"foo"}; */
10258   if (string_flag
10259       && constructor_type
10260       && !was_designated
10261       && TREE_CODE (constructor_type) == ARRAY_TYPE
10262       && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10263       && integer_zerop (constructor_unfilled_index))
10264     {
10265       if (constructor_stack->replacement_value.value)
10266         error_init (loc, "excess elements in %<char%> array initializer");
10267       constructor_stack->replacement_value = value;
10268       return;
10269     }
10270
10271   if (constructor_stack->replacement_value.value != NULL_TREE)
10272     {
10273       error_init (loc, "excess elements in struct initializer");
10274       return;
10275     }
10276
10277   /* Ignore elements of a brace group if it is entirely superfluous
10278      and has already been diagnosed, or if the type is erroneous.  */
10279   if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10280     return;
10281
10282   /* Ignore elements of an initializer for a variable-size type.
10283      Those are diagnosed in the parser (empty initializer braces are OK).  */
10284   if (COMPLETE_TYPE_P (constructor_type)
10285       && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10286     return;
10287
10288   if (!implicit && warn_designated_init && !was_designated
10289       && TREE_CODE (constructor_type) == RECORD_TYPE
10290       && lookup_attribute ("designated_init",
10291                            TYPE_ATTRIBUTES (constructor_type)))
10292     warning_init (loc,
10293                   OPT_Wdesignated_init,
10294                   "positional initialization of field "
10295                   "in %<struct%> declared with %<designated_init%> attribute");
10296
10297   /* If we've exhausted any levels that didn't have braces,
10298      pop them now.  */
10299   while (constructor_stack->implicit)
10300     {
10301       if (RECORD_OR_UNION_TYPE_P (constructor_type)
10302           && constructor_fields == NULL_TREE)
10303         process_init_element (loc,
10304                               pop_init_level (loc, 1, braced_init_obstack,
10305                                               last_init_list_comma),
10306                               true, braced_init_obstack);
10307       else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10308                 || gnu_vector_type_p (constructor_type))
10309                && constructor_max_index
10310                && tree_int_cst_lt (constructor_max_index,
10311                                    constructor_index))
10312         process_init_element (loc,
10313                               pop_init_level (loc, 1, braced_init_obstack,
10314                                               last_init_list_comma),
10315                               true, braced_init_obstack);
10316       else
10317         break;
10318     }
10319
10320   /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once.  */
10321   if (constructor_range_stack)
10322     {
10323       /* If value is a compound literal and we'll be just using its
10324          content, don't put it into a SAVE_EXPR.  */
10325       if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10326           || !require_constant_value)
10327         {
10328           tree semantic_type = NULL_TREE;
10329           if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10330             {
10331               semantic_type = TREE_TYPE (value.value);
10332               value.value = TREE_OPERAND (value.value, 0);
10333             }
10334           value.value = save_expr (value.value);
10335           if (semantic_type)
10336             value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10337                                   value.value);
10338         }
10339     }
10340
10341   while (1)
10342     {
10343       if (TREE_CODE (constructor_type) == RECORD_TYPE)
10344         {
10345           tree fieldtype;
10346           enum tree_code fieldcode;
10347
10348           if (constructor_fields == NULL_TREE)
10349             {
10350               pedwarn_init (loc, 0, "excess elements in struct initializer");
10351               break;
10352             }
10353
10354           fieldtype = TREE_TYPE (constructor_fields);
10355           if (fieldtype != error_mark_node)
10356             fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10357           fieldcode = TREE_CODE (fieldtype);
10358
10359           /* Error for non-static initialization of a flexible array member.  */
10360           if (fieldcode == ARRAY_TYPE
10361               && !require_constant_value
10362               && TYPE_SIZE (fieldtype) == NULL_TREE
10363               && DECL_CHAIN (constructor_fields) == NULL_TREE)
10364             {
10365               error_init (loc, "non-static initialization of a flexible "
10366                           "array member");
10367               break;
10368             }
10369
10370           /* Error for initialization of a flexible array member with
10371              a string constant if the structure is in an array.  E.g.:
10372              struct S { int x; char y[]; };
10373              struct S s[] = { { 1, "foo" } };
10374              is invalid.  */
10375           if (string_flag
10376               && fieldcode == ARRAY_TYPE
10377               && constructor_depth > 1
10378               && TYPE_SIZE (fieldtype) == NULL_TREE
10379               && DECL_CHAIN (constructor_fields) == NULL_TREE)
10380             {
10381               bool in_array_p = false;
10382               for (struct constructor_stack *p = constructor_stack;
10383                    p && p->type; p = p->next)
10384                 if (TREE_CODE (p->type) == ARRAY_TYPE)
10385                   {
10386                     in_array_p = true;
10387                     break;
10388                   }
10389               if (in_array_p)
10390                 {
10391                   error_init (loc, "initialization of flexible array "
10392                               "member in a nested context");
10393                   break;
10394                 }
10395             }
10396
10397           /* Accept a string constant to initialize a subarray.  */
10398           if (value.value != NULL_TREE
10399               && fieldcode == ARRAY_TYPE
10400               && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10401               && string_flag)
10402             value.value = orig_value;
10403           /* Otherwise, if we have come to a subaggregate,
10404              and we don't have an element of its type, push into it.  */
10405           else if (value.value != NULL_TREE
10406                    && initialize_elementwise_p (fieldtype, value.value))
10407             {
10408               push_init_level (loc, 1, braced_init_obstack);
10409               continue;
10410             }
10411
10412           if (value.value)
10413             {
10414               push_member_name (constructor_fields);
10415               output_init_element (loc, value.value, value.original_type,
10416                                    strict_string, fieldtype,
10417                                    constructor_fields, true, implicit,
10418                                    braced_init_obstack);
10419               RESTORE_SPELLING_DEPTH (constructor_depth);
10420             }
10421           else
10422             /* Do the bookkeeping for an element that was
10423                directly output as a constructor.  */
10424             {
10425               /* For a record, keep track of end position of last field.  */
10426               if (DECL_SIZE (constructor_fields))
10427                 constructor_bit_index
10428                   = size_binop_loc (input_location, PLUS_EXPR,
10429                                     bit_position (constructor_fields),
10430                                     DECL_SIZE (constructor_fields));
10431
10432               /* If the current field was the first one not yet written out,
10433                  it isn't now, so update.  */
10434               if (constructor_unfilled_fields == constructor_fields)
10435                 {
10436                   constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10437                   /* Skip any nameless bit fields.  */
10438                   while (constructor_unfilled_fields != 0
10439                          && (DECL_UNNAMED_BIT_FIELD
10440                              (constructor_unfilled_fields)))
10441                     constructor_unfilled_fields =
10442                       DECL_CHAIN (constructor_unfilled_fields);
10443                 }
10444             }
10445
10446           constructor_fields = DECL_CHAIN (constructor_fields);
10447           /* Skip any nameless bit fields at the beginning.  */
10448           while (constructor_fields != NULL_TREE
10449                  && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10450             constructor_fields = DECL_CHAIN (constructor_fields);
10451         }
10452       else if (TREE_CODE (constructor_type) == UNION_TYPE)
10453         {
10454           tree fieldtype;
10455           enum tree_code fieldcode;
10456
10457           if (constructor_fields == NULL_TREE)
10458             {
10459               pedwarn_init (loc, 0,
10460                             "excess elements in union initializer");
10461               break;
10462             }
10463
10464           fieldtype = TREE_TYPE (constructor_fields);
10465           if (fieldtype != error_mark_node)
10466             fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10467           fieldcode = TREE_CODE (fieldtype);
10468
10469           /* Warn that traditional C rejects initialization of unions.
10470              We skip the warning if the value is zero.  This is done
10471              under the assumption that the zero initializer in user
10472              code appears conditioned on e.g. __STDC__ to avoid
10473              "missing initializer" warnings and relies on default
10474              initialization to zero in the traditional C case.
10475              We also skip the warning if the initializer is designated,
10476              again on the assumption that this must be conditional on
10477              __STDC__ anyway (and we've already complained about the
10478              member-designator already).  */
10479           if (!in_system_header_at (input_location) && !constructor_designated
10480               && !(value.value && (integer_zerop (value.value)
10481                                    || real_zerop (value.value))))
10482             warning (OPT_Wtraditional, "traditional C rejects initialization "
10483                      "of unions");
10484
10485           /* Accept a string constant to initialize a subarray.  */
10486           if (value.value != NULL_TREE
10487               && fieldcode == ARRAY_TYPE
10488               && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10489               && string_flag)
10490             value.value = orig_value;
10491           /* Otherwise, if we have come to a subaggregate,
10492              and we don't have an element of its type, push into it.  */
10493           else if (value.value != NULL_TREE
10494                    && initialize_elementwise_p (fieldtype, value.value))
10495             {
10496               push_init_level (loc, 1, braced_init_obstack);
10497               continue;
10498             }
10499
10500           if (value.value)
10501             {
10502               push_member_name (constructor_fields);
10503               output_init_element (loc, value.value, value.original_type,
10504                                    strict_string, fieldtype,
10505                                    constructor_fields, true, implicit,
10506                                    braced_init_obstack);
10507               RESTORE_SPELLING_DEPTH (constructor_depth);
10508             }
10509           else
10510             /* Do the bookkeeping for an element that was
10511                directly output as a constructor.  */
10512             {
10513               constructor_bit_index = DECL_SIZE (constructor_fields);
10514               constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10515             }
10516
10517           constructor_fields = NULL_TREE;
10518         }
10519       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10520         {
10521           tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10522           enum tree_code eltcode = TREE_CODE (elttype);
10523
10524           /* Accept a string constant to initialize a subarray.  */
10525           if (value.value != NULL_TREE
10526               && eltcode == ARRAY_TYPE
10527               && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10528               && string_flag)
10529             value.value = orig_value;
10530           /* Otherwise, if we have come to a subaggregate,
10531              and we don't have an element of its type, push into it.  */
10532           else if (value.value != NULL_TREE
10533                    && initialize_elementwise_p (elttype, value.value))
10534             {
10535               push_init_level (loc, 1, braced_init_obstack);
10536               continue;
10537             }
10538
10539           if (constructor_max_index != NULL_TREE
10540               && (tree_int_cst_lt (constructor_max_index, constructor_index)
10541                   || integer_all_onesp (constructor_max_index)))
10542             {
10543               pedwarn_init (loc, 0,
10544                             "excess elements in array initializer");
10545               break;
10546             }
10547
10548           /* Now output the actual element.  */
10549           if (value.value)
10550             {
10551               push_array_bounds (tree_to_uhwi (constructor_index));
10552               output_init_element (loc, value.value, value.original_type,
10553                                    strict_string, elttype,
10554                                    constructor_index, true, implicit,
10555                                    braced_init_obstack);
10556               RESTORE_SPELLING_DEPTH (constructor_depth);
10557             }
10558
10559           constructor_index
10560             = size_binop_loc (input_location, PLUS_EXPR,
10561                               constructor_index, bitsize_one_node);
10562
10563           if (!value.value)
10564             /* If we are doing the bookkeeping for an element that was
10565                directly output as a constructor, we must update
10566                constructor_unfilled_index.  */
10567             constructor_unfilled_index = constructor_index;
10568         }
10569       else if (gnu_vector_type_p (constructor_type))
10570         {
10571           tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10572
10573          /* Do a basic check of initializer size.  Note that vectors
10574             always have a fixed size derived from their type.  */
10575           if (tree_int_cst_lt (constructor_max_index, constructor_index))
10576             {
10577               pedwarn_init (loc, 0,
10578                             "excess elements in vector initializer");
10579               break;
10580             }
10581
10582           /* Now output the actual element.  */
10583           if (value.value)
10584             {
10585               if (TREE_CODE (value.value) == VECTOR_CST)
10586                 elttype = TYPE_MAIN_VARIANT (constructor_type);
10587               output_init_element (loc, value.value, value.original_type,
10588                                    strict_string, elttype,
10589                                    constructor_index, true, implicit,
10590                                    braced_init_obstack);
10591             }
10592
10593           constructor_index
10594             = size_binop_loc (input_location,
10595                               PLUS_EXPR, constructor_index, bitsize_one_node);
10596
10597           if (!value.value)
10598             /* If we are doing the bookkeeping for an element that was
10599                directly output as a constructor, we must update
10600                constructor_unfilled_index.  */
10601             constructor_unfilled_index = constructor_index;
10602         }
10603
10604       /* Handle the sole element allowed in a braced initializer
10605          for a scalar variable.  */
10606       else if (constructor_type != error_mark_node
10607                && constructor_fields == NULL_TREE)
10608         {
10609           pedwarn_init (loc, 0,
10610                         "excess elements in scalar initializer");
10611           break;
10612         }
10613       else
10614         {
10615           if (value.value)
10616             output_init_element (loc, value.value, value.original_type,
10617                                  strict_string, constructor_type,
10618                                  NULL_TREE, true, implicit,
10619                                  braced_init_obstack);
10620           constructor_fields = NULL_TREE;
10621         }
10622
10623       /* Handle range initializers either at this level or anywhere higher
10624          in the designator stack.  */
10625       if (constructor_range_stack)
10626         {
10627           struct constructor_range_stack *p, *range_stack;
10628           int finish = 0;
10629
10630           range_stack = constructor_range_stack;
10631           constructor_range_stack = 0;
10632           while (constructor_stack != range_stack->stack)
10633             {
10634               gcc_assert (constructor_stack->implicit);
10635               process_init_element (loc,
10636                                     pop_init_level (loc, 1,
10637                                                     braced_init_obstack,
10638                                                     last_init_list_comma),
10639                                     true, braced_init_obstack);
10640             }
10641           for (p = range_stack;
10642                !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10643                p = p->prev)
10644             {
10645               gcc_assert (constructor_stack->implicit);
10646               process_init_element (loc,
10647                                     pop_init_level (loc, 1,
10648                                                     braced_init_obstack,
10649                                                     last_init_list_comma),
10650                                     true, braced_init_obstack);
10651             }
10652
10653           p->index = size_binop_loc (input_location,
10654                                      PLUS_EXPR, p->index, bitsize_one_node);
10655           if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10656             finish = 1;
10657
10658           while (1)
10659             {
10660               constructor_index = p->index;
10661               constructor_fields = p->fields;
10662               if (finish && p->range_end && p->index == p->range_start)
10663                 {
10664                   finish = 0;
10665                   p->prev = 0;
10666                 }
10667               p = p->next;
10668               if (!p)
10669                 break;
10670               finish_implicit_inits (loc, braced_init_obstack);
10671               push_init_level (loc, 2, braced_init_obstack);
10672               p->stack = constructor_stack;
10673               if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10674                 p->index = p->range_start;
10675             }
10676
10677           if (!finish)
10678             constructor_range_stack = range_stack;
10679           continue;
10680         }
10681
10682       break;
10683     }
10684
10685   constructor_range_stack = 0;
10686 }
10687 \f
10688 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10689    (guaranteed to be 'volatile' or null) and ARGS (represented using
10690    an ASM_EXPR node).  */
10691 tree
10692 build_asm_stmt (bool is_volatile, tree args)
10693 {
10694   if (is_volatile)
10695     ASM_VOLATILE_P (args) = 1;
10696   return add_stmt (args);
10697 }
10698
10699 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10700    some INPUTS, and some CLOBBERS.  The latter three may be NULL.
10701    SIMPLE indicates whether there was anything at all after the
10702    string in the asm expression -- asm("blah") and asm("blah" : )
10703    are subtly different.  We use a ASM_EXPR node to represent this.
10704    LOC is the location of the asm, and IS_INLINE says whether this
10705    is asm inline.  */
10706 tree
10707 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10708                 tree clobbers, tree labels, bool simple, bool is_inline)
10709 {
10710   tree tail;
10711   tree args;
10712   int i;
10713   const char *constraint;
10714   const char **oconstraints;
10715   bool allows_mem, allows_reg, is_inout;
10716   int ninputs, noutputs;
10717
10718   ninputs = list_length (inputs);
10719   noutputs = list_length (outputs);
10720   oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10721
10722   string = resolve_asm_operand_names (string, outputs, inputs, labels);
10723
10724   /* Remove output conversions that change the type but not the mode.  */
10725   for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10726     {
10727       tree output = TREE_VALUE (tail);
10728
10729       output = c_fully_fold (output, false, NULL, true);
10730
10731       /* ??? Really, this should not be here.  Users should be using a
10732          proper lvalue, dammit.  But there's a long history of using casts
10733          in the output operands.  In cases like longlong.h, this becomes a
10734          primitive form of typechecking -- if the cast can be removed, then
10735          the output operand had a type of the proper width; otherwise we'll
10736          get an error.  Gross, but ...  */
10737       STRIP_NOPS (output);
10738
10739       if (!lvalue_or_else (loc, output, lv_asm))
10740         output = error_mark_node;
10741
10742       if (output != error_mark_node
10743           && (TREE_READONLY (output)
10744               || TYPE_READONLY (TREE_TYPE (output))
10745               || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10746                   && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10747         readonly_error (loc, output, lv_asm);
10748
10749       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10750       oconstraints[i] = constraint;
10751
10752       if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10753                                    &allows_mem, &allows_reg, &is_inout))
10754         {
10755           /* If the operand is going to end up in memory,
10756              mark it addressable.  */
10757           if (!allows_reg && !c_mark_addressable (output))
10758             output = error_mark_node;
10759           if (!(!allows_reg && allows_mem)
10760               && output != error_mark_node
10761               && VOID_TYPE_P (TREE_TYPE (output)))
10762             {
10763               error_at (loc, "invalid use of void expression");
10764               output = error_mark_node;
10765             }
10766         }
10767       else
10768         output = error_mark_node;
10769
10770       TREE_VALUE (tail) = output;
10771     }
10772
10773   for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10774     {
10775       tree input;
10776
10777       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10778       input = TREE_VALUE (tail);
10779
10780       if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10781                                   oconstraints, &allows_mem, &allows_reg))
10782         {
10783           /* If the operand is going to end up in memory,
10784              mark it addressable.  */
10785           if (!allows_reg && allows_mem)
10786             {
10787               input = c_fully_fold (input, false, NULL, true);
10788
10789               /* Strip the nops as we allow this case.  FIXME, this really
10790                  should be rejected or made deprecated.  */
10791               STRIP_NOPS (input);
10792               if (!c_mark_addressable (input))
10793                 input = error_mark_node;
10794             }
10795           else
10796             {
10797               struct c_expr expr;
10798               memset (&expr, 0, sizeof (expr));
10799               expr.value = input;
10800               expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10801               input = c_fully_fold (expr.value, false, NULL);
10802
10803               if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10804                 {
10805                   error_at (loc, "invalid use of void expression");
10806                   input = error_mark_node;
10807                 }
10808             }
10809         }
10810       else
10811         input = error_mark_node;
10812
10813       TREE_VALUE (tail) = input;
10814     }
10815
10816   args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10817
10818   /* asm statements without outputs, including simple ones, are treated
10819      as volatile.  */
10820   ASM_INPUT_P (args) = simple;
10821   ASM_VOLATILE_P (args) = (noutputs == 0);
10822   ASM_INLINE_P (args) = is_inline;
10823
10824   return args;
10825 }
10826 \f
10827 /* Generate a goto statement to LABEL.  LOC is the location of the
10828    GOTO.  */
10829
10830 tree
10831 c_finish_goto_label (location_t loc, tree label)
10832 {
10833   tree decl = lookup_label_for_goto (loc, label);
10834   if (!decl)
10835     return NULL_TREE;
10836   TREE_USED (decl) = 1;
10837   {
10838     add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10839     tree t = build1 (GOTO_EXPR, void_type_node, decl);
10840     SET_EXPR_LOCATION (t, loc);
10841     return add_stmt (t);
10842   }
10843 }
10844
10845 /* Generate a computed goto statement to EXPR.  LOC is the location of
10846    the GOTO.  */
10847
10848 tree
10849 c_finish_goto_ptr (location_t loc, c_expr val)
10850 {
10851   tree expr = val.value;
10852   tree t;
10853   pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10854   if (expr != error_mark_node
10855       && !POINTER_TYPE_P (TREE_TYPE (expr))
10856       && !null_pointer_constant_p (expr))
10857     {
10858       error_at (val.get_location (),
10859                 "computed goto must be pointer type");
10860       expr = build_zero_cst (ptr_type_node);
10861     }
10862   expr = c_fully_fold (expr, false, NULL);
10863   expr = convert (ptr_type_node, expr);
10864   t = build1 (GOTO_EXPR, void_type_node, expr);
10865   SET_EXPR_LOCATION (t, loc);
10866   return add_stmt (t);
10867 }
10868
10869 /* Generate a C `return' statement.  RETVAL is the expression for what
10870    to return, or a null pointer for `return;' with no value.  LOC is
10871    the location of the return statement, or the location of the expression,
10872    if the statement has any.  If ORIGTYPE is not NULL_TREE, it
10873    is the original type of RETVAL.  */
10874
10875 tree
10876 c_finish_return (location_t loc, tree retval, tree origtype)
10877 {
10878   tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10879   bool no_warning = false;
10880   bool npc = false;
10881
10882   /* Use the expansion point to handle cases such as returning NULL
10883      in a function returning void.  */
10884   location_t xloc = expansion_point_location_if_in_system_header (loc);
10885
10886   if (TREE_THIS_VOLATILE (current_function_decl))
10887     warning_at (xloc, 0,
10888                 "function declared %<noreturn%> has a %<return%> statement");
10889
10890   if (retval)
10891     {
10892       tree semantic_type = NULL_TREE;
10893       npc = null_pointer_constant_p (retval);
10894       if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10895         {
10896           semantic_type = TREE_TYPE (retval);
10897           retval = TREE_OPERAND (retval, 0);
10898         }
10899       retval = c_fully_fold (retval, false, NULL);
10900       if (semantic_type
10901           && valtype != NULL_TREE
10902           && TREE_CODE (valtype) != VOID_TYPE)
10903         retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10904     }
10905
10906   if (!retval)
10907     {
10908       current_function_returns_null = 1;
10909       if ((warn_return_type >= 0 || flag_isoc99)
10910           && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10911         {
10912           bool warned_here;
10913           if (flag_isoc99)
10914             warned_here = pedwarn
10915               (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10916                "%<return%> with no value, in function returning non-void");
10917           else
10918             warned_here = warning_at
10919               (loc, OPT_Wreturn_type,
10920                "%<return%> with no value, in function returning non-void");
10921           no_warning = true;
10922           if (warned_here)
10923             inform (DECL_SOURCE_LOCATION (current_function_decl),
10924                     "declared here");
10925         }
10926     }
10927   else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10928     {
10929       current_function_returns_null = 1;
10930       bool warned_here;
10931       if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10932         warned_here = pedwarn
10933           (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10934            "%<return%> with a value, in function returning void");
10935       else
10936         warned_here = pedwarn
10937           (xloc, OPT_Wpedantic, "ISO C forbids "
10938            "%<return%> with expression, in function returning void");
10939       if (warned_here)
10940         inform (DECL_SOURCE_LOCATION (current_function_decl),
10941                 "declared here");
10942     }
10943   else
10944     {
10945       tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10946                                        retval, origtype, ic_return,
10947                                        npc, NULL_TREE, NULL_TREE, 0);
10948       tree res = DECL_RESULT (current_function_decl);
10949       tree inner;
10950       bool save;
10951
10952       current_function_returns_value = 1;
10953       if (t == error_mark_node)
10954         return NULL_TREE;
10955
10956       save = in_late_binary_op;
10957       if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10958           || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10959           || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10960               && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10961                   || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10962               && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10963         in_late_binary_op = true;
10964       inner = t = convert (TREE_TYPE (res), t);
10965       in_late_binary_op = save;
10966
10967       /* Strip any conversions, additions, and subtractions, and see if
10968          we are returning the address of a local variable.  Warn if so.  */
10969       while (1)
10970         {
10971           switch (TREE_CODE (inner))
10972             {
10973             CASE_CONVERT:
10974             case NON_LVALUE_EXPR:
10975             case PLUS_EXPR:
10976             case POINTER_PLUS_EXPR:
10977               inner = TREE_OPERAND (inner, 0);
10978               continue;
10979
10980             case MINUS_EXPR:
10981               /* If the second operand of the MINUS_EXPR has a pointer
10982                  type (or is converted from it), this may be valid, so
10983                  don't give a warning.  */
10984               {
10985                 tree op1 = TREE_OPERAND (inner, 1);
10986
10987                 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10988                        && (CONVERT_EXPR_P (op1)
10989                            || TREE_CODE (op1) == NON_LVALUE_EXPR))
10990                   op1 = TREE_OPERAND (op1, 0);
10991
10992                 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10993                   break;
10994
10995                 inner = TREE_OPERAND (inner, 0);
10996                 continue;
10997               }
10998
10999             case ADDR_EXPR:
11000               inner = TREE_OPERAND (inner, 0);
11001
11002               while (REFERENCE_CLASS_P (inner)
11003                      && !INDIRECT_REF_P (inner))
11004                 inner = TREE_OPERAND (inner, 0);
11005
11006               if (DECL_P (inner)
11007                   && !DECL_EXTERNAL (inner)
11008                   && !TREE_STATIC (inner)
11009                   && DECL_CONTEXT (inner) == current_function_decl
11010                   && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
11011                 {
11012                   if (TREE_CODE (inner) == LABEL_DECL)
11013                     warning_at (loc, OPT_Wreturn_local_addr,
11014                                 "function returns address of label");
11015                   else
11016                     {
11017                       warning_at (loc, OPT_Wreturn_local_addr,
11018                                   "function returns address of local variable");
11019                       tree zero = build_zero_cst (TREE_TYPE (res));
11020                       t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
11021                     }
11022                 }
11023               break;
11024
11025             default:
11026               break;
11027             }
11028
11029           break;
11030         }
11031
11032       retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11033       SET_EXPR_LOCATION (retval, loc);
11034
11035       if (warn_sequence_point)
11036         verify_sequence_points (retval);
11037     }
11038
11039   ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11040   if (no_warning)
11041     suppress_warning (ret_stmt, OPT_Wreturn_type);
11042   return add_stmt (ret_stmt);
11043 }
11044 \f
11045 struct c_switch {
11046   /* The SWITCH_STMT being built.  */
11047   tree switch_stmt;
11048
11049   /* The original type of the testing expression, i.e. before the
11050      default conversion is applied.  */
11051   tree orig_type;
11052
11053   /* A splay-tree mapping the low element of a case range to the high
11054      element, or NULL_TREE if there is no high element.  Used to
11055      determine whether or not a new case label duplicates an old case
11056      label.  We need a tree, rather than simply a hash table, because
11057      of the GNU case range extension.  */
11058   splay_tree cases;
11059
11060   /* The bindings at the point of the switch.  This is used for
11061      warnings crossing decls when branching to a case label.  */
11062   struct c_spot_bindings *bindings;
11063
11064   /* Whether the switch includes any break statements.  */
11065   bool break_stmt_seen_p;
11066
11067   /* The next node on the stack.  */
11068   struct c_switch *next;
11069
11070   /* Remember whether the controlling expression had boolean type
11071      before integer promotions for the sake of -Wswitch-bool.  */
11072   bool bool_cond_p;
11073 };
11074
11075 /* A stack of the currently active switch statements.  The innermost
11076    switch statement is on the top of the stack.  There is no need to
11077    mark the stack for garbage collection because it is only active
11078    during the processing of the body of a function, and we never
11079    collect at that point.  */
11080
11081 struct c_switch *c_switch_stack;
11082
11083 /* Start a C switch statement, testing expression EXP.  Return the new
11084    SWITCH_STMT.  SWITCH_LOC is the location of the `switch'.
11085    SWITCH_COND_LOC is the location of the switch's condition.
11086    EXPLICIT_CAST_P is true if the expression EXP has an explicit cast.  */
11087
11088 tree
11089 c_start_switch (location_t switch_loc,
11090                 location_t switch_cond_loc,
11091                 tree exp, bool explicit_cast_p)
11092 {
11093   tree orig_type = error_mark_node;
11094   bool bool_cond_p = false;
11095   struct c_switch *cs;
11096
11097   if (exp != error_mark_node)
11098     {
11099       orig_type = TREE_TYPE (exp);
11100
11101       if (!INTEGRAL_TYPE_P (orig_type))
11102         {
11103           if (orig_type != error_mark_node)
11104             {
11105               error_at (switch_cond_loc, "switch quantity not an integer");
11106               orig_type = error_mark_node;
11107             }
11108           exp = integer_zero_node;
11109         }
11110       else
11111         {
11112           tree type = TYPE_MAIN_VARIANT (orig_type);
11113           tree e = exp;
11114
11115           /* Warn if the condition has boolean value.  */
11116           while (TREE_CODE (e) == COMPOUND_EXPR)
11117             e = TREE_OPERAND (e, 1);
11118
11119           if ((TREE_CODE (type) == BOOLEAN_TYPE
11120                || truth_value_p (TREE_CODE (e)))
11121               /* Explicit cast to int suppresses this warning.  */
11122               && !(TREE_CODE (type) == INTEGER_TYPE
11123                    && explicit_cast_p))
11124             bool_cond_p = true;
11125
11126           if (!in_system_header_at (input_location)
11127               && (type == long_integer_type_node
11128                   || type == long_unsigned_type_node))
11129             warning_at (switch_cond_loc,
11130                         OPT_Wtraditional, "%<long%> switch expression not "
11131                         "converted to %<int%> in ISO C");
11132
11133           exp = c_fully_fold (exp, false, NULL);
11134           exp = default_conversion (exp);
11135
11136           if (warn_sequence_point)
11137             verify_sequence_points (exp);
11138         }
11139     }
11140
11141   /* Add this new SWITCH_STMT to the stack.  */
11142   cs = XNEW (struct c_switch);
11143   cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11144                                 NULL_TREE, orig_type, NULL_TREE);
11145   cs->orig_type = orig_type;
11146   cs->cases = splay_tree_new (case_compare, NULL, NULL);
11147   cs->bindings = c_get_switch_bindings ();
11148   cs->break_stmt_seen_p = false;
11149   cs->bool_cond_p = bool_cond_p;
11150   cs->next = c_switch_stack;
11151   c_switch_stack = cs;
11152
11153   return add_stmt (cs->switch_stmt);
11154 }
11155
11156 /* Process a case label at location LOC.  */
11157
11158 tree
11159 do_case (location_t loc, tree low_value, tree high_value)
11160 {
11161   tree label = NULL_TREE;
11162
11163   if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11164     {
11165       low_value = c_fully_fold (low_value, false, NULL);
11166       if (TREE_CODE (low_value) == INTEGER_CST)
11167         pedwarn (loc, OPT_Wpedantic,
11168                  "case label is not an integer constant expression");
11169     }
11170
11171   if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11172     {
11173       high_value = c_fully_fold (high_value, false, NULL);
11174       if (TREE_CODE (high_value) == INTEGER_CST)
11175         pedwarn (input_location, OPT_Wpedantic,
11176                  "case label is not an integer constant expression");
11177     }
11178
11179   if (c_switch_stack == NULL)
11180     {
11181       if (low_value)
11182         error_at (loc, "case label not within a switch statement");
11183       else
11184         error_at (loc, "%<default%> label not within a switch statement");
11185       return NULL_TREE;
11186     }
11187
11188   if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11189                                     EXPR_LOCATION (c_switch_stack->switch_stmt),
11190                                     loc))
11191     return NULL_TREE;
11192
11193   label = c_add_case_label (loc, c_switch_stack->cases,
11194                             SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11195                             low_value, high_value);
11196   if (label == error_mark_node)
11197     label = NULL_TREE;
11198   return label;
11199 }
11200
11201 /* Finish the switch statement.  TYPE is the original type of the
11202    controlling expression of the switch, or NULL_TREE.  */
11203
11204 void
11205 c_finish_switch (tree body, tree type)
11206 {
11207   struct c_switch *cs = c_switch_stack;
11208   location_t switch_location;
11209
11210   SWITCH_STMT_BODY (cs->switch_stmt) = body;
11211
11212   /* Emit warnings as needed.  */
11213   switch_location = EXPR_LOCATION (cs->switch_stmt);
11214   c_do_switch_warnings (cs->cases, switch_location,
11215                         type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11216                         SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11217   if (c_switch_covers_all_cases_p (cs->cases,
11218                                    SWITCH_STMT_TYPE (cs->switch_stmt)))
11219     SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11220   SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11221
11222   /* Pop the stack.  */
11223   c_switch_stack = cs->next;
11224   splay_tree_delete (cs->cases);
11225   c_release_switch_bindings (cs->bindings);
11226   XDELETE (cs);
11227 }
11228 \f
11229 /* Emit an if statement.  IF_LOCUS is the location of the 'if'.  COND,
11230    THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11231    may be null.  */
11232
11233 void
11234 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11235                   tree else_block)
11236 {
11237   tree stmt;
11238
11239   stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11240   SET_EXPR_LOCATION (stmt, if_locus);
11241   add_stmt (stmt);
11242 }
11243
11244 tree
11245 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11246 {
11247   /* In switch statements break is sometimes stylistically used after
11248      a return statement.  This can lead to spurious warnings about
11249      control reaching the end of a non-void function when it is
11250      inlined.  Note that we are calling block_may_fallthru with
11251      language specific tree nodes; this works because
11252      block_may_fallthru returns true when given something it does not
11253      understand.  */
11254   bool skip = !block_may_fallthru (cur_stmt_list);
11255
11256   if (is_break)
11257     switch (in_statement)
11258       {
11259       case 0:
11260         error_at (loc, "break statement not within loop or switch");
11261         return NULL_TREE;
11262       case IN_OMP_BLOCK:
11263         error_at (loc, "invalid exit from OpenMP structured block");
11264         return NULL_TREE;
11265       case IN_OMP_FOR:
11266         error_at (loc, "break statement used with OpenMP for loop");
11267         return NULL_TREE;
11268       case IN_ITERATION_STMT:
11269       case IN_OBJC_FOREACH:
11270         break;
11271       default:
11272         gcc_assert (in_statement & IN_SWITCH_STMT);
11273         c_switch_stack->break_stmt_seen_p = true;
11274         break;
11275       }
11276   else
11277     switch (in_statement & ~IN_SWITCH_STMT)
11278       {
11279       case 0:
11280         error_at (loc, "continue statement not within a loop");
11281         return NULL_TREE;
11282       case IN_OMP_BLOCK:
11283         error_at (loc, "invalid exit from OpenMP structured block");
11284         return NULL_TREE;
11285       case IN_ITERATION_STMT:
11286       case IN_OMP_FOR:
11287       case IN_OBJC_FOREACH:
11288         break;
11289       default:
11290         gcc_unreachable ();
11291       }
11292
11293   if (skip)
11294     return NULL_TREE;
11295   else if ((in_statement & IN_OBJC_FOREACH)
11296            && !(is_break && (in_statement & IN_SWITCH_STMT)))
11297     {
11298       /* The foreach expander produces low-level code using gotos instead
11299          of a structured loop construct.  */
11300       gcc_assert (label);
11301       return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11302     }
11303   return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11304 }
11305
11306 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr.  */
11307
11308 static void
11309 emit_side_effect_warnings (location_t loc, tree expr)
11310 {
11311   maybe_warn_nodiscard (loc, expr);
11312   if (!warn_unused_value)
11313     return;
11314   if (expr == error_mark_node)
11315     ;
11316   else if (!TREE_SIDE_EFFECTS (expr))
11317     {
11318       if (!VOID_TYPE_P (TREE_TYPE (expr))
11319           && !warning_suppressed_p (expr, OPT_Wunused_value))
11320         warning_at (loc, OPT_Wunused_value, "statement with no effect");
11321     }
11322   else if (TREE_CODE (expr) == COMPOUND_EXPR)
11323     {
11324       tree r = expr;
11325       location_t cloc = loc;
11326       while (TREE_CODE (r) == COMPOUND_EXPR)
11327         {
11328           if (EXPR_HAS_LOCATION (r))
11329             cloc = EXPR_LOCATION (r);
11330           r = TREE_OPERAND (r, 1);
11331         }
11332       if (!TREE_SIDE_EFFECTS (r)
11333           && !VOID_TYPE_P (TREE_TYPE (r))
11334           && !CONVERT_EXPR_P (r)
11335           && !warning_suppressed_p (r, OPT_Wunused_value)
11336           && !warning_suppressed_p (expr, OPT_Wunused_value))
11337         warning_at (cloc, OPT_Wunused_value,
11338                     "right-hand operand of comma expression has no effect");
11339     }
11340   else
11341     warn_if_unused_value (expr, loc);
11342 }
11343
11344 /* Process an expression as if it were a complete statement.  Emit
11345    diagnostics, but do not call ADD_STMT.  LOC is the location of the
11346    statement.  */
11347
11348 tree
11349 c_process_expr_stmt (location_t loc, tree expr)
11350 {
11351   tree exprv;
11352
11353   if (!expr)
11354     return NULL_TREE;
11355
11356   expr = c_fully_fold (expr, false, NULL);
11357
11358   if (warn_sequence_point)
11359     verify_sequence_points (expr);
11360
11361   if (TREE_TYPE (expr) != error_mark_node
11362       && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11363       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11364     error_at (loc, "expression statement has incomplete type");
11365
11366   /* If we're not processing a statement expression, warn about unused values.
11367      Warnings for statement expressions will be emitted later, once we figure
11368      out which is the result.  */
11369   if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11370       && (warn_unused_value || warn_unused_result))
11371     emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11372
11373   exprv = expr;
11374   while (TREE_CODE (exprv) == COMPOUND_EXPR)
11375     exprv = TREE_OPERAND (exprv, 1);
11376   while (CONVERT_EXPR_P (exprv))
11377     exprv = TREE_OPERAND (exprv, 0);
11378   if (DECL_P (exprv)
11379       || handled_component_p (exprv)
11380       || TREE_CODE (exprv) == ADDR_EXPR)
11381     mark_exp_read (exprv);
11382
11383   /* If the expression is not of a type to which we cannot assign a line
11384      number, wrap the thing in a no-op NOP_EXPR.  */
11385   if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11386     {
11387       expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11388       SET_EXPR_LOCATION (expr, loc);
11389     }
11390
11391   return expr;
11392 }
11393
11394 /* Emit an expression as a statement.  LOC is the location of the
11395    expression.  */
11396
11397 tree
11398 c_finish_expr_stmt (location_t loc, tree expr)
11399 {
11400   if (expr)
11401     return add_stmt (c_process_expr_stmt (loc, expr));
11402   else
11403     return NULL;
11404 }
11405
11406 /* Do the opposite and emit a statement as an expression.  To begin,
11407    create a new binding level and return it.  */
11408
11409 tree
11410 c_begin_stmt_expr (void)
11411 {
11412   tree ret;
11413
11414   /* We must force a BLOCK for this level so that, if it is not expanded
11415      later, there is a way to turn off the entire subtree of blocks that
11416      are contained in it.  */
11417   keep_next_level ();
11418   ret = c_begin_compound_stmt (true);
11419
11420   c_bindings_start_stmt_expr (c_switch_stack == NULL
11421                               ? NULL
11422                               : c_switch_stack->bindings);
11423
11424   /* Mark the current statement list as belonging to a statement list.  */
11425   STATEMENT_LIST_STMT_EXPR (ret) = 1;
11426
11427   return ret;
11428 }
11429
11430 /* LOC is the location of the compound statement to which this body
11431    belongs.  */
11432
11433 tree
11434 c_finish_stmt_expr (location_t loc, tree body)
11435 {
11436   tree last, type, tmp, val;
11437   tree *last_p;
11438
11439   body = c_end_compound_stmt (loc, body, true);
11440
11441   c_bindings_end_stmt_expr (c_switch_stack == NULL
11442                             ? NULL
11443                             : c_switch_stack->bindings);
11444
11445   /* Locate the last statement in BODY.  See c_end_compound_stmt
11446      about always returning a BIND_EXPR.  */
11447   last_p = &BIND_EXPR_BODY (body);
11448   last = BIND_EXPR_BODY (body);
11449
11450  continue_searching:
11451   if (TREE_CODE (last) == STATEMENT_LIST)
11452     {
11453       tree_stmt_iterator l = tsi_last (last);
11454
11455       while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11456         tsi_prev (&l);
11457
11458       /* This can happen with degenerate cases like ({ }).  No value.  */
11459       if (tsi_end_p (l))
11460         return body;
11461
11462       /* If we're supposed to generate side effects warnings, process
11463          all of the statements except the last.  */
11464       if (warn_unused_value || warn_unused_result)
11465         {
11466           for (tree_stmt_iterator i = tsi_start (last);
11467                tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11468             {
11469               location_t tloc;
11470               tree t = tsi_stmt (i);
11471
11472               tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11473               emit_side_effect_warnings (tloc, t);
11474             }
11475         }
11476       last_p = tsi_stmt_ptr (l);
11477       last = *last_p;
11478     }
11479
11480   /* If the end of the list is exception related, then the list was split
11481      by a call to push_cleanup.  Continue searching.  */
11482   if (TREE_CODE (last) == TRY_FINALLY_EXPR
11483       || TREE_CODE (last) == TRY_CATCH_EXPR)
11484     {
11485       last_p = &TREE_OPERAND (last, 0);
11486       last = *last_p;
11487       goto continue_searching;
11488     }
11489
11490   if (last == error_mark_node)
11491     return last;
11492
11493   /* In the case that the BIND_EXPR is not necessary, return the
11494      expression out from inside it.  */
11495   if ((last == BIND_EXPR_BODY (body)
11496        /* Skip nested debug stmts.  */
11497        || last == expr_first (BIND_EXPR_BODY (body)))
11498       && BIND_EXPR_VARS (body) == NULL)
11499     {
11500       /* Even if this looks constant, do not allow it in a constant
11501          expression.  */
11502       last = c_wrap_maybe_const (last, true);
11503       /* Do not warn if the return value of a statement expression is
11504          unused.  */
11505       suppress_warning (last, OPT_Wunused);
11506       return last;
11507     }
11508
11509   /* Extract the type of said expression.  */
11510   type = TREE_TYPE (last);
11511
11512   /* If we're not returning a value at all, then the BIND_EXPR that
11513      we already have is a fine expression to return.  */
11514   if (!type || VOID_TYPE_P (type))
11515     return body;
11516
11517   /* Now that we've located the expression containing the value, it seems
11518      silly to make voidify_wrapper_expr repeat the process.  Create a
11519      temporary of the appropriate type and stick it in a TARGET_EXPR.  */
11520   tmp = create_tmp_var_raw (type);
11521
11522   /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt.  This avoids
11523      tree_expr_nonnegative_p giving up immediately.  */
11524   val = last;
11525   if (TREE_CODE (val) == NOP_EXPR
11526       && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11527     val = TREE_OPERAND (val, 0);
11528
11529   *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11530   SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11531
11532   {
11533     tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11534     SET_EXPR_LOCATION (t, loc);
11535     return t;
11536   }
11537 }
11538 \f
11539 /* Begin and end compound statements.  This is as simple as pushing
11540    and popping new statement lists from the tree.  */
11541
11542 tree
11543 c_begin_compound_stmt (bool do_scope)
11544 {
11545   tree stmt = push_stmt_list ();
11546   if (do_scope)
11547     push_scope ();
11548   return stmt;
11549 }
11550
11551 /* End a compound statement.  STMT is the statement.  LOC is the
11552    location of the compound statement-- this is usually the location
11553    of the opening brace.  */
11554
11555 tree
11556 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11557 {
11558   tree block = NULL;
11559
11560   if (do_scope)
11561     {
11562       if (c_dialect_objc ())
11563         objc_clear_super_receiver ();
11564       block = pop_scope ();
11565     }
11566
11567   stmt = pop_stmt_list (stmt);
11568   stmt = c_build_bind_expr (loc, block, stmt);
11569
11570   /* If this compound statement is nested immediately inside a statement
11571      expression, then force a BIND_EXPR to be created.  Otherwise we'll
11572      do the wrong thing for ({ { 1; } }) or ({ 1; { } }).  In particular,
11573      STATEMENT_LISTs merge, and thus we can lose track of what statement
11574      was really last.  */
11575   if (building_stmt_list_p ()
11576       && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11577       && TREE_CODE (stmt) != BIND_EXPR)
11578     {
11579       stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11580       TREE_SIDE_EFFECTS (stmt) = 1;
11581       SET_EXPR_LOCATION (stmt, loc);
11582     }
11583
11584   return stmt;
11585 }
11586
11587 /* Queue a cleanup.  CLEANUP is an expression/statement to be executed
11588    when the current scope is exited.  EH_ONLY is true when this is not
11589    meant to apply to normal control flow transfer.  */
11590
11591 void
11592 push_cleanup (tree decl, tree cleanup, bool eh_only)
11593 {
11594   enum tree_code code;
11595   tree stmt, list;
11596   bool stmt_expr;
11597
11598   code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11599   stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11600   add_stmt (stmt);
11601   stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11602   list = push_stmt_list ();
11603   TREE_OPERAND (stmt, 0) = list;
11604   STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11605 }
11606 \f
11607 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11608    into a value of TYPE type.  Comparison is done via VEC_COND_EXPR.  */
11609
11610 static tree
11611 build_vec_cmp (tree_code code, tree type,
11612                tree arg0, tree arg1)
11613 {
11614   tree zero_vec = build_zero_cst (type);
11615   tree minus_one_vec = build_minus_one_cst (type);
11616   tree cmp_type = truth_type_for (type);
11617   tree cmp = build2 (code, cmp_type, arg0, arg1);
11618   return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11619 }
11620
11621 /* Possibly warn about an address of OP never being NULL in a comparison
11622    operation CODE involving null.  */
11623
11624 static void
11625 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
11626 {
11627   /* Prevent warnings issued for macro expansion.  */
11628   if (!warn_address
11629       || warning_suppressed_p (op, OPT_Waddress)
11630       || from_macro_expansion_at (loc))
11631     return;
11632
11633   if (TREE_CODE (op) == NOP_EXPR)
11634     {
11635       /* Allow casts to intptr_t to suppress the warning.  */
11636       tree type = TREE_TYPE (op);
11637       if (TREE_CODE (type) == INTEGER_TYPE)
11638         return;
11639       op = TREE_OPERAND (op, 0);
11640     }
11641
11642   if (TREE_CODE (op) == POINTER_PLUS_EXPR)
11643     {
11644       /* Allow a cast to void* to suppress the warning.  */
11645       tree type = TREE_TYPE (TREE_TYPE (op));
11646       if (VOID_TYPE_P (type))
11647         return;
11648
11649       /* Adding any value to a null pointer, including zero, is undefined
11650          in C.  This includes the expression &p[0] where p is the null
11651          pointer, although &p[0] will have been folded to p by this point
11652          and so not diagnosed.  */
11653       if (code == EQ_EXPR)
11654         warning_at (loc, OPT_Waddress,
11655                     "the comparison will always evaluate as %<false%> "
11656                     "for the pointer operand in %qE must not be NULL",
11657                     op);
11658       else
11659         warning_at (loc, OPT_Waddress,
11660                     "the comparison will always evaluate as %<true%> "
11661                     "for the pointer operand in %qE must not be NULL",
11662                     op);
11663
11664       return;
11665     }
11666
11667   if (TREE_CODE (op) != ADDR_EXPR)
11668     return;
11669
11670   op = TREE_OPERAND (op, 0);
11671
11672   if (TREE_CODE (op) == IMAGPART_EXPR
11673       || TREE_CODE (op) == REALPART_EXPR)
11674     {
11675       /* The address of either complex part may not be null.  */
11676       if (code == EQ_EXPR)
11677         warning_at (loc, OPT_Waddress,
11678                     "the comparison will always evaluate as %<false%> "
11679                     "for the address of %qE will never be NULL",
11680                     op);
11681       else
11682         warning_at (loc, OPT_Waddress,
11683                     "the comparison will always evaluate as %<true%> "
11684                     "for the address of %qE will never be NULL",
11685                     op);
11686       return;
11687     }
11688
11689   /* Set to true in the loop below if OP dereferences is operand.
11690      In such a case the ultimate target need not be a decl for
11691      the null [in]equality test to be constant.  */
11692   bool deref = false;
11693
11694   /* Get the outermost array or object, or member.  */
11695   while (handled_component_p (op))
11696     {
11697       if (TREE_CODE (op) == COMPONENT_REF)
11698         {
11699           /* Get the member (its address is never null).  */
11700           op = TREE_OPERAND (op, 1);
11701           break;
11702         }
11703
11704       /* Get the outer array/object to refer to in the warning.  */
11705       op = TREE_OPERAND (op, 0);
11706       deref = true;
11707     }
11708
11709   if ((!deref && !decl_with_nonnull_addr_p (op))
11710       || from_macro_expansion_at (loc))
11711     return;
11712
11713   if (code == EQ_EXPR)
11714     warning_at (loc, OPT_Waddress,
11715                 "the comparison will always evaluate as %<false%> "
11716                 "for the address of %qE will never be NULL",
11717                 op);
11718   else
11719     warning_at (loc, OPT_Waddress,
11720                 "the comparison will always evaluate as %<true%> "
11721                 "for the address of %qE will never be NULL",
11722                 op);
11723
11724   if (DECL_P (op))
11725     inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
11726 }
11727
11728 /* Build a binary-operation expression without default conversions.
11729    CODE is the kind of expression to build.
11730    LOCATION is the operator's location.
11731    This function differs from `build' in several ways:
11732    the data type of the result is computed and recorded in it,
11733    warnings are generated if arg data types are invalid,
11734    special handling for addition and subtraction of pointers is known,
11735    and some optimization is done (operations on narrow ints
11736    are done in the narrower type when that gives the same result).
11737    Constant folding is also done before the result is returned.
11738
11739    Note that the operands will never have enumeral types, or function
11740    or array types, because either they will have the default conversions
11741    performed or they have both just been converted to some other type in which
11742    the arithmetic is to be done.  */
11743
11744 tree
11745 build_binary_op (location_t location, enum tree_code code,
11746                  tree orig_op0, tree orig_op1, bool convert_p)
11747 {
11748   tree type0, type1, orig_type0, orig_type1;
11749   tree eptype;
11750   enum tree_code code0, code1;
11751   tree op0, op1;
11752   tree ret = error_mark_node;
11753   const char *invalid_op_diag;
11754   bool op0_int_operands, op1_int_operands;
11755   bool int_const, int_const_or_overflow, int_operands;
11756
11757   /* Expression code to give to the expression when it is built.
11758      Normally this is CODE, which is what the caller asked for,
11759      but in some special cases we change it.  */
11760   enum tree_code resultcode = code;
11761
11762   /* Data type in which the computation is to be performed.
11763      In the simplest cases this is the common type of the arguments.  */
11764   tree result_type = NULL;
11765
11766   /* When the computation is in excess precision, the type of the
11767      final EXCESS_PRECISION_EXPR.  */
11768   tree semantic_result_type = NULL;
11769
11770   /* Nonzero means operands have already been type-converted
11771      in whatever way is necessary.
11772      Zero means they need to be converted to RESULT_TYPE.  */
11773   int converted = 0;
11774
11775   /* Nonzero means create the expression with this type, rather than
11776      RESULT_TYPE.  */
11777   tree build_type = NULL_TREE;
11778
11779   /* Nonzero means after finally constructing the expression
11780      convert it to this type.  */
11781   tree final_type = NULL_TREE;
11782
11783   /* Nonzero if this is an operation like MIN or MAX which can
11784      safely be computed in short if both args are promoted shorts.
11785      Also implies COMMON.
11786      -1 indicates a bitwise operation; this makes a difference
11787      in the exact conditions for when it is safe to do the operation
11788      in a narrower mode.  */
11789   int shorten = 0;
11790
11791   /* Nonzero if this is a comparison operation;
11792      if both args are promoted shorts, compare the original shorts.
11793      Also implies COMMON.  */
11794   int short_compare = 0;
11795
11796   /* Nonzero if this is a right-shift operation, which can be computed on the
11797      original short and then promoted if the operand is a promoted short.  */
11798   int short_shift = 0;
11799
11800   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
11801   int common = 0;
11802
11803   /* True means types are compatible as far as ObjC is concerned.  */
11804   bool objc_ok;
11805
11806   /* True means this is an arithmetic operation that may need excess
11807      precision.  */
11808   bool may_need_excess_precision;
11809
11810   /* True means this is a boolean operation that converts both its
11811      operands to truth-values.  */
11812   bool boolean_op = false;
11813
11814   /* Remember whether we're doing / or %.  */
11815   bool doing_div_or_mod = false;
11816
11817   /* Remember whether we're doing << or >>.  */
11818   bool doing_shift = false;
11819
11820   /* Tree holding instrumentation expression.  */
11821   tree instrument_expr = NULL;
11822
11823   if (location == UNKNOWN_LOCATION)
11824     location = input_location;
11825
11826   op0 = orig_op0;
11827   op1 = orig_op1;
11828
11829   op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11830   if (op0_int_operands)
11831     op0 = remove_c_maybe_const_expr (op0);
11832   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11833   if (op1_int_operands)
11834     op1 = remove_c_maybe_const_expr (op1);
11835   int_operands = (op0_int_operands && op1_int_operands);
11836   if (int_operands)
11837     {
11838       int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11839                                && TREE_CODE (orig_op1) == INTEGER_CST);
11840       int_const = (int_const_or_overflow
11841                    && !TREE_OVERFLOW (orig_op0)
11842                    && !TREE_OVERFLOW (orig_op1));
11843     }
11844   else
11845     int_const = int_const_or_overflow = false;
11846
11847   /* Do not apply default conversion in mixed vector/scalar expression.  */
11848   if (convert_p
11849       && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11850     {
11851       op0 = default_conversion (op0);
11852       op1 = default_conversion (op1);
11853     }
11854
11855   orig_type0 = type0 = TREE_TYPE (op0);
11856
11857   orig_type1 = type1 = TREE_TYPE (op1);
11858
11859   /* The expression codes of the data types of the arguments tell us
11860      whether the arguments are integers, floating, pointers, etc.  */
11861   code0 = TREE_CODE (type0);
11862   code1 = TREE_CODE (type1);
11863
11864   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
11865   STRIP_TYPE_NOPS (op0);
11866   STRIP_TYPE_NOPS (op1);
11867
11868   /* If an error was already reported for one of the arguments,
11869      avoid reporting another error.  */
11870
11871   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11872     return error_mark_node;
11873
11874   if (code0 == POINTER_TYPE
11875       && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11876     return error_mark_node;
11877
11878   if (code1 == POINTER_TYPE
11879       && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11880     return error_mark_node;
11881
11882   if ((invalid_op_diag
11883        = targetm.invalid_binary_op (code, type0, type1)))
11884     {
11885       error_at (location, invalid_op_diag);
11886       return error_mark_node;
11887     }
11888
11889   switch (code)
11890     {
11891     case PLUS_EXPR:
11892     case MINUS_EXPR:
11893     case MULT_EXPR:
11894     case TRUNC_DIV_EXPR:
11895     case CEIL_DIV_EXPR:
11896     case FLOOR_DIV_EXPR:
11897     case ROUND_DIV_EXPR:
11898     case EXACT_DIV_EXPR:
11899       may_need_excess_precision = true;
11900       break;
11901
11902     case EQ_EXPR:
11903     case NE_EXPR:
11904     case LE_EXPR:
11905     case GE_EXPR:
11906     case LT_EXPR:
11907     case GT_EXPR:
11908       /* Excess precision for implicit conversions of integers to
11909          floating point in C11 and later.  */
11910       may_need_excess_precision = (flag_isoc11
11911                                    && (ANY_INTEGRAL_TYPE_P (type0)
11912                                        || ANY_INTEGRAL_TYPE_P (type1)));
11913       break;
11914
11915     default:
11916       may_need_excess_precision = false;
11917       break;
11918     }
11919   if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11920     {
11921       op0 = TREE_OPERAND (op0, 0);
11922       type0 = TREE_TYPE (op0);
11923     }
11924   else if (may_need_excess_precision
11925            && (eptype = excess_precision_type (type0)) != NULL_TREE)
11926     {
11927       type0 = eptype;
11928       op0 = convert (eptype, op0);
11929     }
11930   if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11931     {
11932       op1 = TREE_OPERAND (op1, 0);
11933       type1 = TREE_TYPE (op1);
11934     }
11935   else if (may_need_excess_precision
11936            && (eptype = excess_precision_type (type1)) != NULL_TREE)
11937     {
11938       type1 = eptype;
11939       op1 = convert (eptype, op1);
11940     }
11941
11942   objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11943
11944   /* In case when one of the operands of the binary operation is
11945      a vector and another is a scalar -- convert scalar to vector.  */
11946   if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11947       || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11948     {
11949       enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11950                                                      true);
11951
11952       switch (convert_flag)
11953         {
11954           case stv_error:
11955             return error_mark_node;
11956           case stv_firstarg:
11957             {
11958               bool maybe_const = true;
11959               tree sc;
11960               sc = c_fully_fold (op0, false, &maybe_const);
11961               sc = save_expr (sc);
11962               sc = convert (TREE_TYPE (type1), sc);
11963               op0 = build_vector_from_val (type1, sc);
11964               if (!maybe_const)
11965                 op0 = c_wrap_maybe_const (op0, true);
11966               orig_type0 = type0 = TREE_TYPE (op0);
11967               code0 = TREE_CODE (type0);
11968               converted = 1;
11969               break;
11970             }
11971           case stv_secondarg:
11972             {
11973               bool maybe_const = true;
11974               tree sc;
11975               sc = c_fully_fold (op1, false, &maybe_const);
11976               sc = save_expr (sc);
11977               sc = convert (TREE_TYPE (type0), sc);
11978               op1 = build_vector_from_val (type0, sc);
11979               if (!maybe_const)
11980                 op1 = c_wrap_maybe_const (op1, true);
11981               orig_type1 = type1 = TREE_TYPE (op1);
11982               code1 = TREE_CODE (type1);
11983               converted = 1;
11984               break;
11985             }
11986           default:
11987             break;
11988         }
11989     }
11990
11991   switch (code)
11992     {
11993     case PLUS_EXPR:
11994       /* Handle the pointer + int case.  */
11995       if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11996         {
11997           ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11998           goto return_build_binary_op;
11999         }
12000       else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
12001         {
12002           ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
12003           goto return_build_binary_op;
12004         }
12005       else
12006         common = 1;
12007       break;
12008
12009     case MINUS_EXPR:
12010       /* Subtraction of two similar pointers.
12011          We must subtract them as integers, then divide by object size.  */
12012       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
12013           && comp_target_types (location, type0, type1))
12014         {
12015           ret = pointer_diff (location, op0, op1, &instrument_expr);
12016           goto return_build_binary_op;
12017         }
12018       /* Handle pointer minus int.  Just like pointer plus int.  */
12019       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12020         {
12021           ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
12022           goto return_build_binary_op;
12023         }
12024       else
12025         common = 1;
12026       break;
12027
12028     case MULT_EXPR:
12029       common = 1;
12030       break;
12031
12032     case TRUNC_DIV_EXPR:
12033     case CEIL_DIV_EXPR:
12034     case FLOOR_DIV_EXPR:
12035     case ROUND_DIV_EXPR:
12036     case EXACT_DIV_EXPR:
12037       doing_div_or_mod = true;
12038       warn_for_div_by_zero (location, op1);
12039
12040       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12041            || code0 == FIXED_POINT_TYPE
12042            || code0 == COMPLEX_TYPE
12043            || gnu_vector_type_p (type0))
12044           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12045               || code1 == FIXED_POINT_TYPE
12046               || code1 == COMPLEX_TYPE
12047               || gnu_vector_type_p (type1)))
12048         {
12049           enum tree_code tcode0 = code0, tcode1 = code1;
12050
12051           if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12052             tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12053           if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12054             tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12055
12056           if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
12057               || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12058             resultcode = RDIV_EXPR;
12059           else
12060             /* Although it would be tempting to shorten always here, that
12061                loses on some targets, since the modulo instruction is
12062                undefined if the quotient can't be represented in the
12063                computation mode.  We shorten only if unsigned or if
12064                dividing by something we know != -1.  */
12065             shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12066                        || (TREE_CODE (op1) == INTEGER_CST
12067                            && !integer_all_onesp (op1)));
12068           common = 1;
12069         }
12070       break;
12071
12072     case BIT_AND_EXPR:
12073     case BIT_IOR_EXPR:
12074     case BIT_XOR_EXPR:
12075       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12076         shorten = -1;
12077       /* Allow vector types which are not floating point types.   */
12078       else if (gnu_vector_type_p (type0)
12079                && gnu_vector_type_p (type1)
12080                && !VECTOR_FLOAT_TYPE_P (type0)
12081                && !VECTOR_FLOAT_TYPE_P (type1))
12082         common = 1;
12083       break;
12084
12085     case TRUNC_MOD_EXPR:
12086     case FLOOR_MOD_EXPR:
12087       doing_div_or_mod = true;
12088       warn_for_div_by_zero (location, op1);
12089
12090       if (gnu_vector_type_p (type0)
12091           && gnu_vector_type_p (type1)
12092           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12093           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12094         common = 1;
12095       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12096         {
12097           /* Although it would be tempting to shorten always here, that loses
12098              on some targets, since the modulo instruction is undefined if the
12099              quotient can't be represented in the computation mode.  We shorten
12100              only if unsigned or if dividing by something we know != -1.  */
12101           shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12102                      || (TREE_CODE (op1) == INTEGER_CST
12103                          && !integer_all_onesp (op1)));
12104           common = 1;
12105         }
12106       break;
12107
12108     case TRUTH_ANDIF_EXPR:
12109     case TRUTH_ORIF_EXPR:
12110     case TRUTH_AND_EXPR:
12111     case TRUTH_OR_EXPR:
12112     case TRUTH_XOR_EXPR:
12113       if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12114            || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12115            || code0 == FIXED_POINT_TYPE)
12116           && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12117               || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12118               || code1 == FIXED_POINT_TYPE))
12119         {
12120           /* Result of these operations is always an int,
12121              but that does not mean the operands should be
12122              converted to ints!  */
12123           result_type = integer_type_node;
12124           if (op0_int_operands)
12125             {
12126               op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12127               op0 = remove_c_maybe_const_expr (op0);
12128             }
12129           else
12130             op0 = c_objc_common_truthvalue_conversion (location, op0);
12131           if (op1_int_operands)
12132             {
12133               op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12134               op1 = remove_c_maybe_const_expr (op1);
12135             }
12136           else
12137             op1 = c_objc_common_truthvalue_conversion (location, op1);
12138           converted = 1;
12139           boolean_op = true;
12140         }
12141       if (code == TRUTH_ANDIF_EXPR)
12142         {
12143           int_const_or_overflow = (int_operands
12144                                    && TREE_CODE (orig_op0) == INTEGER_CST
12145                                    && (op0 == truthvalue_false_node
12146                                        || TREE_CODE (orig_op1) == INTEGER_CST));
12147           int_const = (int_const_or_overflow
12148                        && !TREE_OVERFLOW (orig_op0)
12149                        && (op0 == truthvalue_false_node
12150                            || !TREE_OVERFLOW (orig_op1)));
12151         }
12152       else if (code == TRUTH_ORIF_EXPR)
12153         {
12154           int_const_or_overflow = (int_operands
12155                                    && TREE_CODE (orig_op0) == INTEGER_CST
12156                                    && (op0 == truthvalue_true_node
12157                                        || TREE_CODE (orig_op1) == INTEGER_CST));
12158           int_const = (int_const_or_overflow
12159                        && !TREE_OVERFLOW (orig_op0)
12160                        && (op0 == truthvalue_true_node
12161                            || !TREE_OVERFLOW (orig_op1)));
12162         }
12163       break;
12164
12165       /* Shift operations: result has same type as first operand;
12166          always convert second operand to int.
12167          Also set SHORT_SHIFT if shifting rightward.  */
12168
12169     case RSHIFT_EXPR:
12170       if (gnu_vector_type_p (type0)
12171           && gnu_vector_type_p (type1)
12172           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12173           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12174           && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12175                        TYPE_VECTOR_SUBPARTS (type1)))
12176         {
12177           result_type = type0;
12178           converted = 1;
12179         }
12180       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12181                 || (gnu_vector_type_p (type0)
12182                     && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12183                && code1 == INTEGER_TYPE)
12184         {
12185           doing_shift = true;
12186           if (TREE_CODE (op1) == INTEGER_CST)
12187             {
12188               if (tree_int_cst_sgn (op1) < 0)
12189                 {
12190                   int_const = false;
12191                   if (c_inhibit_evaluation_warnings == 0)
12192                     warning_at (location, OPT_Wshift_count_negative,
12193                                 "right shift count is negative");
12194                 }
12195               else if (code0 == VECTOR_TYPE)
12196                 {
12197                   if (compare_tree_int (op1,
12198                                         TYPE_PRECISION (TREE_TYPE (type0)))
12199                       >= 0)
12200                     {
12201                       int_const = false;
12202                       if (c_inhibit_evaluation_warnings == 0)
12203                         warning_at (location, OPT_Wshift_count_overflow,
12204                                     "right shift count >= width of vector element");
12205                     }
12206                 }
12207               else
12208                 {
12209                   if (!integer_zerop (op1))
12210                     short_shift = 1;
12211
12212                   if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12213                     {
12214                       int_const = false;
12215                       if (c_inhibit_evaluation_warnings == 0)
12216                         warning_at (location, OPT_Wshift_count_overflow,
12217                                     "right shift count >= width of type");
12218                     }
12219                 }
12220             }
12221
12222           /* Use the type of the value to be shifted.  */
12223           result_type = type0;
12224           /* Avoid converting op1 to result_type later.  */
12225           converted = 1;
12226         }
12227       break;
12228
12229     case LSHIFT_EXPR:
12230       if (gnu_vector_type_p (type0)
12231           && gnu_vector_type_p (type1)
12232           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12233           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12234           && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12235                        TYPE_VECTOR_SUBPARTS (type1)))
12236         {
12237           result_type = type0;
12238           converted = 1;
12239         }
12240       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12241                 || (gnu_vector_type_p (type0)
12242                     && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12243                && code1 == INTEGER_TYPE)
12244         {
12245           doing_shift = true;
12246           if (TREE_CODE (op0) == INTEGER_CST
12247               && tree_int_cst_sgn (op0) < 0
12248               && !TYPE_OVERFLOW_WRAPS (type0))
12249             {
12250               /* Don't reject a left shift of a negative value in a context
12251                  where a constant expression is needed in C90.  */
12252               if (flag_isoc99)
12253                 int_const = false;
12254               if (c_inhibit_evaluation_warnings == 0)
12255                 warning_at (location, OPT_Wshift_negative_value,
12256                             "left shift of negative value");
12257             }
12258           if (TREE_CODE (op1) == INTEGER_CST)
12259             {
12260               if (tree_int_cst_sgn (op1) < 0)
12261                 {
12262                   int_const = false;
12263                   if (c_inhibit_evaluation_warnings == 0)
12264                     warning_at (location, OPT_Wshift_count_negative,
12265                                 "left shift count is negative");
12266                 }
12267               else if (code0 == VECTOR_TYPE)
12268                 {
12269                   if (compare_tree_int (op1,
12270                                         TYPE_PRECISION (TREE_TYPE (type0)))
12271                       >= 0)
12272                     {
12273                       int_const = false;
12274                       if (c_inhibit_evaluation_warnings == 0)
12275                         warning_at (location, OPT_Wshift_count_overflow,
12276                                     "left shift count >= width of vector element");
12277                     }
12278                 }
12279               else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12280                 {
12281                   int_const = false;
12282                   if (c_inhibit_evaluation_warnings == 0)
12283                     warning_at (location, OPT_Wshift_count_overflow,
12284                                 "left shift count >= width of type");
12285                 }
12286               else if (TREE_CODE (op0) == INTEGER_CST
12287                        && maybe_warn_shift_overflow (location, op0, op1)
12288                        && flag_isoc99)
12289                 int_const = false;
12290             }
12291
12292           /* Use the type of the value to be shifted.  */
12293           result_type = type0;
12294           /* Avoid converting op1 to result_type later.  */
12295           converted = 1;
12296         }
12297       break;
12298
12299     case EQ_EXPR:
12300     case NE_EXPR:
12301       if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12302         {
12303           tree intt;
12304           if (!vector_types_compatible_elements_p (type0, type1))
12305             {
12306               error_at (location, "comparing vectors with different "
12307                                   "element types");
12308               return error_mark_node;
12309             }
12310
12311           if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12312                         TYPE_VECTOR_SUBPARTS (type1)))
12313             {
12314               error_at (location, "comparing vectors with different "
12315                                   "number of elements");
12316               return error_mark_node;
12317             }
12318
12319           /* It's not precisely specified how the usual arithmetic
12320              conversions apply to the vector types.  Here, we use
12321              the unsigned type if one of the operands is signed and
12322              the other one is unsigned.  */
12323           if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12324             {
12325               if (!TYPE_UNSIGNED (type0))
12326                 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12327               else
12328                 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12329               warning_at (location, OPT_Wsign_compare, "comparison between "
12330                           "types %qT and %qT", type0, type1);
12331             }
12332
12333           /* Always construct signed integer vector type.  */
12334           intt = c_common_type_for_size (GET_MODE_BITSIZE
12335                                          (SCALAR_TYPE_MODE
12336                                           (TREE_TYPE (type0))), 0);
12337           if (!intt)
12338             {
12339               error_at (location, "could not find an integer type "
12340                                   "of the same size as %qT",
12341                         TREE_TYPE (type0));
12342               return error_mark_node;
12343             }
12344           result_type = build_opaque_vector_type (intt,
12345                                                   TYPE_VECTOR_SUBPARTS (type0));
12346           converted = 1;
12347           ret = build_vec_cmp (resultcode, result_type, op0, op1);
12348           goto return_build_binary_op;
12349         }
12350       if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12351         warning_at (location,
12352                     OPT_Wfloat_equal,
12353                     "comparing floating-point with %<==%> or %<!=%> is unsafe");
12354       /* Result of comparison is always int,
12355          but don't convert the args to int!  */
12356       build_type = integer_type_node;
12357       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12358            || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12359           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12360               || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12361         short_compare = 1;
12362       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12363         {
12364           maybe_warn_for_null_address (location, op0, code);
12365           result_type = type0;
12366         }
12367       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12368         {
12369           maybe_warn_for_null_address (location, op1, code);
12370           result_type = type1;
12371         }
12372       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12373         {
12374           tree tt0 = TREE_TYPE (type0);
12375           tree tt1 = TREE_TYPE (type1);
12376           addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12377           addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12378           addr_space_t as_common = ADDR_SPACE_GENERIC;
12379
12380           /* Anything compares with void *.  void * compares with anything.
12381              Otherwise, the targets must be compatible
12382              and both must be object or both incomplete.  */
12383           if (comp_target_types (location, type0, type1))
12384             result_type = common_pointer_type (type0, type1);
12385           else if (!addr_space_superset (as0, as1, &as_common))
12386             {
12387               error_at (location, "comparison of pointers to "
12388                         "disjoint address spaces");
12389               return error_mark_node;
12390             }
12391           else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12392             {
12393               if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12394                 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12395                          "comparison of %<void *%> with function pointer");
12396             }
12397           else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12398             {
12399               if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12400                 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12401                          "comparison of %<void *%> with function pointer");
12402             }
12403           else
12404             /* Avoid warning about the volatile ObjC EH puts on decls.  */
12405             if (!objc_ok)
12406               pedwarn (location, 0,
12407                        "comparison of distinct pointer types lacks a cast");
12408
12409           if (result_type == NULL_TREE)
12410             {
12411               int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12412               result_type = build_pointer_type
12413                               (build_qualified_type (void_type_node, qual));
12414             }
12415         }
12416       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12417         {
12418           result_type = type0;
12419           pedwarn (location, 0, "comparison between pointer and integer");
12420         }
12421       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12422         {
12423           result_type = type1;
12424           pedwarn (location, 0, "comparison between pointer and integer");
12425         }
12426       if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12427            || truth_value_p (TREE_CODE (orig_op0)))
12428           ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12429              || truth_value_p (TREE_CODE (orig_op1))))
12430         maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12431       break;
12432
12433     case LE_EXPR:
12434     case GE_EXPR:
12435     case LT_EXPR:
12436     case GT_EXPR:
12437       if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12438         {
12439           tree intt;
12440           if (!vector_types_compatible_elements_p (type0, type1))
12441             {
12442               error_at (location, "comparing vectors with different "
12443                                   "element types");
12444               return error_mark_node;
12445             }
12446
12447           if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12448                         TYPE_VECTOR_SUBPARTS (type1)))
12449             {
12450               error_at (location, "comparing vectors with different "
12451                                   "number of elements");
12452               return error_mark_node;
12453             }
12454
12455           /* It's not precisely specified how the usual arithmetic
12456              conversions apply to the vector types.  Here, we use
12457              the unsigned type if one of the operands is signed and
12458              the other one is unsigned.  */
12459           if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12460             {
12461               if (!TYPE_UNSIGNED (type0))
12462                 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12463               else
12464                 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12465               warning_at (location, OPT_Wsign_compare, "comparison between "
12466                           "types %qT and %qT", type0, type1);
12467             }
12468
12469           /* Always construct signed integer vector type.  */
12470           intt = c_common_type_for_size (GET_MODE_BITSIZE
12471                                          (SCALAR_TYPE_MODE
12472                                           (TREE_TYPE (type0))), 0);
12473           if (!intt)
12474             {
12475               error_at (location, "could not find an integer type "
12476                                   "of the same size as %qT",
12477                         TREE_TYPE (type0));
12478               return error_mark_node;
12479             }
12480           result_type = build_opaque_vector_type (intt,
12481                                                   TYPE_VECTOR_SUBPARTS (type0));
12482           converted = 1;
12483           ret = build_vec_cmp (resultcode, result_type, op0, op1);
12484           goto return_build_binary_op;
12485         }
12486       build_type = integer_type_node;
12487       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12488            || code0 == FIXED_POINT_TYPE)
12489           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12490               || code1 == FIXED_POINT_TYPE))
12491         short_compare = 1;
12492       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12493         {
12494           addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12495           addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12496           addr_space_t as_common;
12497
12498           if (comp_target_types (location, type0, type1))
12499             {
12500               result_type = common_pointer_type (type0, type1);
12501               if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12502                   != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12503                 pedwarn_c99 (location, OPT_Wpedantic,
12504                              "comparison of complete and incomplete pointers");
12505               else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12506                 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12507                          "ordered comparisons of pointers to functions");
12508               else if (null_pointer_constant_p (orig_op0)
12509                        || null_pointer_constant_p (orig_op1))
12510                 warning_at (location, OPT_Wextra,
12511                             "ordered comparison of pointer with null pointer");
12512
12513             }
12514           else if (!addr_space_superset (as0, as1, &as_common))
12515             {
12516               error_at (location, "comparison of pointers to "
12517                         "disjoint address spaces");
12518               return error_mark_node;
12519             }
12520           else
12521             {
12522               int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12523               result_type = build_pointer_type
12524                               (build_qualified_type (void_type_node, qual));
12525               pedwarn (location, 0,
12526                        "comparison of distinct pointer types lacks a cast");
12527             }
12528         }
12529       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12530         {
12531           result_type = type0;
12532           if (pedantic)
12533             pedwarn (location, OPT_Wpedantic,
12534                      "ordered comparison of pointer with integer zero");
12535           else if (extra_warnings)
12536             warning_at (location, OPT_Wextra,
12537                         "ordered comparison of pointer with integer zero");
12538         }
12539       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12540         {
12541           result_type = type1;
12542           if (pedantic)
12543             pedwarn (location, OPT_Wpedantic,
12544                      "ordered comparison of pointer with integer zero");
12545           else if (extra_warnings)
12546             warning_at (location, OPT_Wextra,
12547                         "ordered comparison of pointer with integer zero");
12548         }
12549       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12550         {
12551           result_type = type0;
12552           pedwarn (location, 0, "comparison between pointer and integer");
12553         }
12554       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12555         {
12556           result_type = type1;
12557           pedwarn (location, 0, "comparison between pointer and integer");
12558         }
12559
12560       if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12561           && current_function_decl != NULL_TREE
12562           && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12563         {
12564           op0 = save_expr (op0);
12565           op1 = save_expr (op1);
12566
12567           tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12568           instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12569         }
12570
12571       if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12572            || truth_value_p (TREE_CODE (orig_op0)))
12573           ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12574              || truth_value_p (TREE_CODE (orig_op1))))
12575         maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12576       break;
12577
12578     case MIN_EXPR:
12579     case MAX_EXPR:
12580       /* Used for OpenMP atomics.  */
12581       gcc_assert (flag_openmp);
12582       common = 1;
12583       break;
12584
12585     default:
12586       gcc_unreachable ();
12587     }
12588
12589   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12590     return error_mark_node;
12591
12592   if (gnu_vector_type_p (type0)
12593       && gnu_vector_type_p (type1)
12594       && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12595           || !vector_types_compatible_elements_p (type0, type1)))
12596     {
12597       gcc_rich_location richloc (location);
12598       maybe_range_label_for_tree_type_mismatch
12599         label_for_op0 (orig_op0, orig_op1),
12600         label_for_op1 (orig_op1, orig_op0);
12601       richloc.maybe_add_expr (orig_op0, &label_for_op0);
12602       richloc.maybe_add_expr (orig_op1, &label_for_op1);
12603       binary_op_error (&richloc, code, type0, type1);
12604       return error_mark_node;
12605     }
12606
12607   if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12608        || code0 == FIXED_POINT_TYPE
12609        || gnu_vector_type_p (type0))
12610       &&
12611       (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12612        || code1 == FIXED_POINT_TYPE
12613        || gnu_vector_type_p (type1)))
12614     {
12615       bool first_complex = (code0 == COMPLEX_TYPE);
12616       bool second_complex = (code1 == COMPLEX_TYPE);
12617       int none_complex = (!first_complex && !second_complex);
12618
12619       if (shorten || common || short_compare)
12620         {
12621           result_type = c_common_type (type0, type1);
12622           do_warn_double_promotion (result_type, type0, type1,
12623                                     "implicit conversion from %qT to %qT "
12624                                     "to match other operand of binary "
12625                                     "expression",
12626                                     location);
12627           if (result_type == error_mark_node)
12628             return error_mark_node;
12629         }
12630
12631       if (first_complex != second_complex
12632           && (code == PLUS_EXPR
12633               || code == MINUS_EXPR
12634               || code == MULT_EXPR
12635               || (code == TRUNC_DIV_EXPR && first_complex))
12636           && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12637           && flag_signed_zeros)
12638         {
12639           /* An operation on mixed real/complex operands must be
12640              handled specially, but the language-independent code can
12641              more easily optimize the plain complex arithmetic if
12642              -fno-signed-zeros.  */
12643           tree real_type = TREE_TYPE (result_type);
12644           tree real, imag;
12645           if (type0 != orig_type0 || type1 != orig_type1)
12646             {
12647               gcc_assert (may_need_excess_precision && common);
12648               semantic_result_type = c_common_type (orig_type0, orig_type1);
12649             }
12650           if (first_complex)
12651             {
12652               if (TREE_TYPE (op0) != result_type)
12653                 op0 = convert_and_check (location, result_type, op0);
12654               if (TREE_TYPE (op1) != real_type)
12655                 op1 = convert_and_check (location, real_type, op1);
12656             }
12657           else
12658             {
12659               if (TREE_TYPE (op0) != real_type)
12660                 op0 = convert_and_check (location, real_type, op0);
12661               if (TREE_TYPE (op1) != result_type)
12662                 op1 = convert_and_check (location, result_type, op1);
12663             }
12664           if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12665             return error_mark_node;
12666           if (first_complex)
12667             {
12668               op0 = save_expr (op0);
12669               real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12670                                      op0, true);
12671               imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12672                                      op0, true);
12673               switch (code)
12674                 {
12675                 case MULT_EXPR:
12676                 case TRUNC_DIV_EXPR:
12677                   op1 = save_expr (op1);
12678                   imag = build2 (resultcode, real_type, imag, op1);
12679                   /* Fall through.  */
12680                 case PLUS_EXPR:
12681                 case MINUS_EXPR:
12682                   real = build2 (resultcode, real_type, real, op1);
12683                   break;
12684                 default:
12685                   gcc_unreachable();
12686                 }
12687             }
12688           else
12689             {
12690               op1 = save_expr (op1);
12691               real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12692                                      op1, true);
12693               imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12694                                      op1, true);
12695               switch (code)
12696                 {
12697                 case MULT_EXPR:
12698                   op0 = save_expr (op0);
12699                   imag = build2 (resultcode, real_type, op0, imag);
12700                   /* Fall through.  */
12701                 case PLUS_EXPR:
12702                   real = build2 (resultcode, real_type, op0, real);
12703                   break;
12704                 case MINUS_EXPR:
12705                   real = build2 (resultcode, real_type, op0, real);
12706                   imag = build1 (NEGATE_EXPR, real_type, imag);
12707                   break;
12708                 default:
12709                   gcc_unreachable();
12710                 }
12711             }
12712           ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12713           goto return_build_binary_op;
12714         }
12715
12716       /* For certain operations (which identify themselves by shorten != 0)
12717          if both args were extended from the same smaller type,
12718          do the arithmetic in that type and then extend.
12719
12720          shorten !=0 and !=1 indicates a bitwise operation.
12721          For them, this optimization is safe only if
12722          both args are zero-extended or both are sign-extended.
12723          Otherwise, we might change the result.
12724          Eg, (short)-1 | (unsigned short)-1 is (int)-1
12725          but calculated in (unsigned short) it would be (unsigned short)-1.  */
12726
12727       if (shorten && none_complex)
12728         {
12729           final_type = result_type;
12730           result_type = shorten_binary_op (result_type, op0, op1,
12731                                            shorten == -1);
12732         }
12733
12734       /* Shifts can be shortened if shifting right.  */
12735
12736       if (short_shift)
12737         {
12738           int unsigned_arg;
12739           tree arg0 = get_narrower (op0, &unsigned_arg);
12740
12741           final_type = result_type;
12742
12743           if (arg0 == op0 && final_type == TREE_TYPE (op0))
12744             unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12745
12746           if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12747               && tree_int_cst_sgn (op1) > 0
12748               /* We can shorten only if the shift count is less than the
12749                  number of bits in the smaller type size.  */
12750               && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12751               /* We cannot drop an unsigned shift after sign-extension.  */
12752               && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12753             {
12754               /* Do an unsigned shift if the operand was zero-extended.  */
12755               result_type
12756                 = c_common_signed_or_unsigned_type (unsigned_arg,
12757                                                     TREE_TYPE (arg0));
12758               /* Convert value-to-be-shifted to that type.  */
12759               if (TREE_TYPE (op0) != result_type)
12760                 op0 = convert (result_type, op0);
12761               converted = 1;
12762             }
12763         }
12764
12765       /* Comparison operations are shortened too but differently.
12766          They identify themselves by setting short_compare = 1.  */
12767
12768       if (short_compare)
12769         {
12770           /* Don't write &op0, etc., because that would prevent op0
12771              from being kept in a register.
12772              Instead, make copies of the our local variables and
12773              pass the copies by reference, then copy them back afterward.  */
12774           tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12775           enum tree_code xresultcode = resultcode;
12776           tree val
12777             = shorten_compare (location, &xop0, &xop1, &xresult_type,
12778                                &xresultcode);
12779
12780           if (val != NULL_TREE)
12781             {
12782               ret = val;
12783               goto return_build_binary_op;
12784             }
12785
12786           op0 = xop0, op1 = xop1;
12787           converted = 1;
12788           resultcode = xresultcode;
12789
12790           if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
12791             {
12792               bool op0_maybe_const = true;
12793               bool op1_maybe_const = true;
12794               tree orig_op0_folded, orig_op1_folded;
12795
12796               if (in_late_binary_op)
12797                 {
12798                   orig_op0_folded = orig_op0;
12799                   orig_op1_folded = orig_op1;
12800                 }
12801               else
12802                 {
12803                   /* Fold for the sake of possible warnings, as in
12804                      build_conditional_expr.  This requires the
12805                      "original" values to be folded, not just op0 and
12806                      op1.  */
12807                   c_inhibit_evaluation_warnings++;
12808                   op0 = c_fully_fold (op0, require_constant_value,
12809                                       &op0_maybe_const);
12810                   op1 = c_fully_fold (op1, require_constant_value,
12811                                       &op1_maybe_const);
12812                   c_inhibit_evaluation_warnings--;
12813                   orig_op0_folded = c_fully_fold (orig_op0,
12814                                                   require_constant_value,
12815                                                   NULL);
12816                   orig_op1_folded = c_fully_fold (orig_op1,
12817                                                   require_constant_value,
12818                                                   NULL);
12819                 }
12820
12821               if (warn_sign_compare)
12822                 warn_for_sign_compare (location, orig_op0_folded,
12823                                        orig_op1_folded, op0, op1,
12824                                        result_type, resultcode);
12825               if (!in_late_binary_op && !int_operands)
12826                 {
12827                   if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12828                     op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12829                   if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12830                     op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12831                 }
12832             }
12833         }
12834     }
12835
12836   /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12837      If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12838      Then the expression will be built.
12839      It will be given type FINAL_TYPE if that is nonzero;
12840      otherwise, it will be given type RESULT_TYPE.  */
12841
12842   if (!result_type)
12843     {
12844       /* Favor showing any expression locations that are available. */
12845       op_location_t oploc (location, UNKNOWN_LOCATION);
12846       binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12847       binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12848       return error_mark_node;
12849     }
12850
12851   if (build_type == NULL_TREE)
12852     {
12853       build_type = result_type;
12854       if ((type0 != orig_type0 || type1 != orig_type1)
12855           && !boolean_op)
12856         {
12857           gcc_assert (may_need_excess_precision && common);
12858           semantic_result_type = c_common_type (orig_type0, orig_type1);
12859         }
12860     }
12861
12862   if (!converted)
12863     {
12864       op0 = ep_convert_and_check (location, result_type, op0,
12865                                   semantic_result_type);
12866       op1 = ep_convert_and_check (location, result_type, op1,
12867                                   semantic_result_type);
12868
12869       /* This can happen if one operand has a vector type, and the other
12870          has a different type.  */
12871       if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12872         return error_mark_node;
12873     }
12874
12875   if (sanitize_flags_p ((SANITIZE_SHIFT
12876                          | SANITIZE_DIVIDE
12877                          | SANITIZE_FLOAT_DIVIDE
12878                          | SANITIZE_SI_OVERFLOW))
12879       && current_function_decl != NULL_TREE
12880       && (doing_div_or_mod || doing_shift)
12881       && !require_constant_value)
12882     {
12883       /* OP0 and/or OP1 might have side-effects.  */
12884       op0 = save_expr (op0);
12885       op1 = save_expr (op1);
12886       op0 = c_fully_fold (op0, false, NULL);
12887       op1 = c_fully_fold (op1, false, NULL);
12888       if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12889                                                   | SANITIZE_FLOAT_DIVIDE
12890                                                   | SANITIZE_SI_OVERFLOW))))
12891         instrument_expr = ubsan_instrument_division (location, op0, op1);
12892       else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12893         instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12894     }
12895
12896   /* Treat expressions in initializers specially as they can't trap.  */
12897   if (int_const_or_overflow)
12898     ret = (require_constant_value
12899            ? fold_build2_initializer_loc (location, resultcode, build_type,
12900                                           op0, op1)
12901            : fold_build2_loc (location, resultcode, build_type, op0, op1));
12902   else
12903     ret = build2 (resultcode, build_type, op0, op1);
12904   if (final_type != NULL_TREE)
12905     ret = convert (final_type, ret);
12906
12907  return_build_binary_op:
12908   gcc_assert (ret != error_mark_node);
12909   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12910     ret = (int_operands
12911            ? note_integer_operands (ret)
12912            : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12913   else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12914            && !in_late_binary_op)
12915     ret = note_integer_operands (ret);
12916   protected_set_expr_location (ret, location);
12917
12918   if (instrument_expr != NULL)
12919     ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12920                        instrument_expr, ret);
12921
12922   if (semantic_result_type)
12923     ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12924                       semantic_result_type, ret);
12925
12926   return ret;
12927 }
12928
12929
12930 /* Convert EXPR to be a truth-value, validating its type for this
12931    purpose.  LOCATION is the source location for the expression.  */
12932
12933 tree
12934 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12935 {
12936   bool int_const, int_operands;
12937
12938   switch (TREE_CODE (TREE_TYPE (expr)))
12939     {
12940     case ARRAY_TYPE:
12941       error_at (location, "used array that cannot be converted to pointer where scalar is required");
12942       return error_mark_node;
12943
12944     case RECORD_TYPE:
12945       error_at (location, "used struct type value where scalar is required");
12946       return error_mark_node;
12947
12948     case UNION_TYPE:
12949       error_at (location, "used union type value where scalar is required");
12950       return error_mark_node;
12951
12952     case VOID_TYPE:
12953       error_at (location, "void value not ignored as it ought to be");
12954       return error_mark_node;
12955
12956     case POINTER_TYPE:
12957       if (reject_gcc_builtin (expr))
12958         return error_mark_node;
12959       break;
12960
12961     case FUNCTION_TYPE:
12962       gcc_unreachable ();
12963
12964     case VECTOR_TYPE:
12965       error_at (location, "used vector type where scalar is required");
12966       return error_mark_node;
12967
12968     default:
12969       break;
12970     }
12971
12972   int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12973   int_operands = EXPR_INT_CONST_OPERANDS (expr);
12974   if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12975     {
12976       expr = remove_c_maybe_const_expr (expr);
12977       expr = build2 (NE_EXPR, integer_type_node, expr,
12978                      convert (TREE_TYPE (expr), integer_zero_node));
12979       expr = note_integer_operands (expr);
12980     }
12981   else
12982     /* ??? Should we also give an error for vectors rather than leaving
12983        those to give errors later?  */
12984     expr = c_common_truthvalue_conversion (location, expr);
12985
12986   if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12987     {
12988       if (TREE_OVERFLOW (expr))
12989         return expr;
12990       else
12991         return note_integer_operands (expr);
12992     }
12993   if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12994     return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12995   return expr;
12996 }
12997 \f
12998
12999 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
13000    required.  */
13001
13002 tree
13003 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
13004 {
13005   if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
13006     {
13007       tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
13008       /* Executing a compound literal inside a function reinitializes
13009          it.  */
13010       if (!TREE_STATIC (decl))
13011         *se = true;
13012       return decl;
13013     }
13014   else
13015     return expr;
13016 }
13017 \f
13018 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13019    statement.  LOC is the location of the construct.  */
13020
13021 tree
13022 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
13023                         tree clauses)
13024 {
13025   body = c_end_compound_stmt (loc, body, true);
13026
13027   tree stmt = make_node (code);
13028   TREE_TYPE (stmt) = void_type_node;
13029   OMP_BODY (stmt) = body;
13030   OMP_CLAUSES (stmt) = clauses;
13031   SET_EXPR_LOCATION (stmt, loc);
13032
13033   return add_stmt (stmt);
13034 }
13035
13036 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13037    statement.  LOC is the location of the OACC_DATA.  */
13038
13039 tree
13040 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13041 {
13042   tree stmt;
13043
13044   block = c_end_compound_stmt (loc, block, true);
13045
13046   stmt = make_node (OACC_DATA);
13047   TREE_TYPE (stmt) = void_type_node;
13048   OACC_DATA_CLAUSES (stmt) = clauses;
13049   OACC_DATA_BODY (stmt) = block;
13050   SET_EXPR_LOCATION (stmt, loc);
13051
13052   return add_stmt (stmt);
13053 }
13054
13055 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13056    statement.  LOC is the location of the OACC_HOST_DATA.  */
13057
13058 tree
13059 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13060 {
13061   tree stmt;
13062
13063   block = c_end_compound_stmt (loc, block, true);
13064
13065   stmt = make_node (OACC_HOST_DATA);
13066   TREE_TYPE (stmt) = void_type_node;
13067   OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13068   OACC_HOST_DATA_BODY (stmt) = block;
13069   SET_EXPR_LOCATION (stmt, loc);
13070
13071   return add_stmt (stmt);
13072 }
13073
13074 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
13075
13076 tree
13077 c_begin_omp_parallel (void)
13078 {
13079   tree block;
13080
13081   keep_next_level ();
13082   block = c_begin_compound_stmt (true);
13083
13084   return block;
13085 }
13086
13087 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13088    statement.  LOC is the location of the OMP_PARALLEL.  */
13089
13090 tree
13091 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13092 {
13093   tree stmt;
13094
13095   block = c_end_compound_stmt (loc, block, true);
13096
13097   stmt = make_node (OMP_PARALLEL);
13098   TREE_TYPE (stmt) = void_type_node;
13099   OMP_PARALLEL_CLAUSES (stmt) = clauses;
13100   OMP_PARALLEL_BODY (stmt) = block;
13101   SET_EXPR_LOCATION (stmt, loc);
13102
13103   return add_stmt (stmt);
13104 }
13105
13106 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
13107
13108 tree
13109 c_begin_omp_task (void)
13110 {
13111   tree block;
13112
13113   keep_next_level ();
13114   block = c_begin_compound_stmt (true);
13115
13116   return block;
13117 }
13118
13119 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13120    statement.  LOC is the location of the #pragma.  */
13121
13122 tree
13123 c_finish_omp_task (location_t loc, tree clauses, tree block)
13124 {
13125   tree stmt;
13126
13127   block = c_end_compound_stmt (loc, block, true);
13128
13129   stmt = make_node (OMP_TASK);
13130   TREE_TYPE (stmt) = void_type_node;
13131   OMP_TASK_CLAUSES (stmt) = clauses;
13132   OMP_TASK_BODY (stmt) = block;
13133   SET_EXPR_LOCATION (stmt, loc);
13134
13135   return add_stmt (stmt);
13136 }
13137
13138 /* Generate GOMP_cancel call for #pragma omp cancel.  */
13139
13140 void
13141 c_finish_omp_cancel (location_t loc, tree clauses)
13142 {
13143   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13144   int mask = 0;
13145   if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13146     mask = 1;
13147   else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13148     mask = 2;
13149   else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13150     mask = 4;
13151   else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13152     mask = 8;
13153   else
13154     {
13155       error_at (loc, "%<#pragma omp cancel%> must specify one of "
13156                      "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13157                      "clauses");
13158       return;
13159     }
13160   tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13161   if (ifc != NULL_TREE)
13162     {
13163       if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13164           && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13165         error_at (OMP_CLAUSE_LOCATION (ifc),
13166                   "expected %<cancel%> %<if%> clause modifier");
13167       else
13168         {
13169           tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13170           if (ifc2 != NULL_TREE)
13171             {
13172               gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13173                           && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13174                           && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13175               error_at (OMP_CLAUSE_LOCATION (ifc2),
13176                         "expected %<cancel%> %<if%> clause modifier");
13177             }
13178         }
13179
13180       tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13181       ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13182                              boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13183                              build_zero_cst (type));
13184     }
13185   else
13186     ifc = boolean_true_node;
13187   tree stmt = build_call_expr_loc (loc, fn, 2,
13188                                    build_int_cst (integer_type_node, mask),
13189                                    ifc);
13190   add_stmt (stmt);
13191 }
13192
13193 /* Generate GOMP_cancellation_point call for
13194    #pragma omp cancellation point.  */
13195
13196 void
13197 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13198 {
13199   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13200   int mask = 0;
13201   if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13202     mask = 1;
13203   else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13204     mask = 2;
13205   else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13206     mask = 4;
13207   else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13208     mask = 8;
13209   else
13210     {
13211       error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13212                      "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13213                      "clauses");
13214       return;
13215     }
13216   tree stmt = build_call_expr_loc (loc, fn, 1,
13217                                    build_int_cst (integer_type_node, mask));
13218   add_stmt (stmt);
13219 }
13220
13221 /* Helper function for handle_omp_array_sections.  Called recursively
13222    to handle multiple array-section-subscripts.  C is the clause,
13223    T current expression (initially OMP_CLAUSE_DECL), which is either
13224    a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13225    expression if specified, TREE_VALUE length expression if specified,
13226    TREE_CHAIN is what it has been specified after, or some decl.
13227    TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13228    set to true if any of the array-section-subscript could have length
13229    of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13230    first array-section-subscript which is known not to have length
13231    of one.  Given say:
13232    map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13233    FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13234    all are or may have length of 1, array-section-subscript [:2] is the
13235    first one known not to have length 1.  For array-section-subscript
13236    <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13237    0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13238    can if MAYBE_ZERO_LEN is false.  MAYBE_ZERO_LEN will be true in the above
13239    case though, as some lengths could be zero.  */
13240
13241 static tree
13242 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13243                              bool &maybe_zero_len, unsigned int &first_non_one,
13244                              enum c_omp_region_type ort)
13245 {
13246   tree ret, low_bound, length, type;
13247   if (TREE_CODE (t) != TREE_LIST)
13248     {
13249       if (error_operand_p (t))
13250         return error_mark_node;
13251       ret = t;
13252       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13253           && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13254           && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13255         {
13256           error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13257                     t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13258           return error_mark_node;
13259         }
13260       while (TREE_CODE (t) == INDIRECT_REF)
13261         {
13262           t = TREE_OPERAND (t, 0);
13263           STRIP_NOPS (t);
13264           if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13265             t = TREE_OPERAND (t, 0);
13266         }
13267       while (TREE_CODE (t) == COMPOUND_EXPR)
13268         {
13269           t = TREE_OPERAND (t, 1);
13270           STRIP_NOPS (t);
13271         }
13272       if (TREE_CODE (t) == COMPONENT_REF
13273           && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13274               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13275               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13276         {
13277           if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13278             {
13279               error_at (OMP_CLAUSE_LOCATION (c),
13280                         "bit-field %qE in %qs clause",
13281                         t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13282               return error_mark_node;
13283             }
13284           while (TREE_CODE (t) == COMPONENT_REF)
13285             {
13286               if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13287                 {
13288                   error_at (OMP_CLAUSE_LOCATION (c),
13289                             "%qE is a member of a union", t);
13290                   return error_mark_node;
13291                 }
13292               t = TREE_OPERAND (t, 0);
13293               while (TREE_CODE (t) == MEM_REF
13294                      || TREE_CODE (t) == INDIRECT_REF
13295                      || TREE_CODE (t) == ARRAY_REF)
13296                 {
13297                   t = TREE_OPERAND (t, 0);
13298                   STRIP_NOPS (t);
13299                   if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13300                     t = TREE_OPERAND (t, 0);
13301                 }
13302               if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13303                 {
13304                   if (maybe_ne (mem_ref_offset (t), 0))
13305                     error_at (OMP_CLAUSE_LOCATION (c),
13306                               "cannot dereference %qE in %qs clause", t,
13307                               omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13308                   else
13309                     t = TREE_OPERAND (t, 0);
13310                 }
13311             }
13312         }
13313       if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13314         {
13315           if (DECL_P (t))
13316             error_at (OMP_CLAUSE_LOCATION (c),
13317                       "%qD is not a variable in %qs clause", t,
13318                       omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13319           else
13320             error_at (OMP_CLAUSE_LOCATION (c),
13321                       "%qE is not a variable in %qs clause", t,
13322                       omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13323           return error_mark_node;
13324         }
13325       else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13326                && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13327                && TYPE_ATOMIC (TREE_TYPE (t)))
13328         {
13329           error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13330                     t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13331           return error_mark_node;
13332         }
13333       else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13334                && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13335                && VAR_P (t)
13336                && DECL_THREAD_LOCAL_P (t))
13337         {
13338           error_at (OMP_CLAUSE_LOCATION (c),
13339                     "%qD is threadprivate variable in %qs clause", t,
13340                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13341           return error_mark_node;
13342         }
13343       if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13344            || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13345           && TYPE_ATOMIC (TREE_TYPE (t))
13346           && POINTER_TYPE_P (TREE_TYPE (t)))
13347         {
13348           /* If the array section is pointer based and the pointer
13349              itself is _Atomic qualified, we need to atomically load
13350              the pointer.  */
13351           c_expr expr;
13352           memset (&expr, 0, sizeof (expr));
13353           expr.value = ret;
13354           expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13355                                            expr, false, false);
13356           ret = expr.value;
13357         }
13358       return ret;
13359     }
13360
13361   ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13362                                      maybe_zero_len, first_non_one, ort);
13363   if (ret == error_mark_node || ret == NULL_TREE)
13364     return ret;
13365
13366   type = TREE_TYPE (ret);
13367   low_bound = TREE_PURPOSE (t);
13368   length = TREE_VALUE (t);
13369
13370   if (low_bound == error_mark_node || length == error_mark_node)
13371     return error_mark_node;
13372
13373   if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13374     {
13375       error_at (OMP_CLAUSE_LOCATION (c),
13376                 "low bound %qE of array section does not have integral type",
13377                 low_bound);
13378       return error_mark_node;
13379     }
13380   if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13381     {
13382       error_at (OMP_CLAUSE_LOCATION (c),
13383                 "length %qE of array section does not have integral type",
13384                 length);
13385       return error_mark_node;
13386     }
13387   if (low_bound
13388       && TREE_CODE (low_bound) == INTEGER_CST
13389       && TYPE_PRECISION (TREE_TYPE (low_bound))
13390          > TYPE_PRECISION (sizetype))
13391     low_bound = fold_convert (sizetype, low_bound);
13392   if (length
13393       && TREE_CODE (length) == INTEGER_CST
13394       && TYPE_PRECISION (TREE_TYPE (length))
13395          > TYPE_PRECISION (sizetype))
13396     length = fold_convert (sizetype, length);
13397   if (low_bound == NULL_TREE)
13398     low_bound = integer_zero_node;
13399   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13400       && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13401           || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13402     {
13403       if (length != integer_one_node)
13404         {
13405           error_at (OMP_CLAUSE_LOCATION (c),
13406                     "expected single pointer in %qs clause",
13407                     user_omp_clause_code_name (c, ort == C_ORT_ACC));
13408           return error_mark_node;
13409         }
13410     }
13411   if (length != NULL_TREE)
13412     {
13413       if (!integer_nonzerop (length))
13414         {
13415           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13416               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13417               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13418               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13419               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13420             {
13421               if (integer_zerop (length))
13422                 {
13423                   error_at (OMP_CLAUSE_LOCATION (c),
13424                             "zero length array section in %qs clause",
13425                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13426                   return error_mark_node;
13427                 }
13428             }
13429           else
13430             maybe_zero_len = true;
13431         }
13432       if (first_non_one == types.length ()
13433           && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13434         first_non_one++;
13435     }
13436   if (TREE_CODE (type) == ARRAY_TYPE)
13437     {
13438       if (length == NULL_TREE
13439           && (TYPE_DOMAIN (type) == NULL_TREE
13440               || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13441         {
13442           error_at (OMP_CLAUSE_LOCATION (c),
13443                     "for unknown bound array type length expression must "
13444                     "be specified");
13445           return error_mark_node;
13446         }
13447       if (TREE_CODE (low_bound) == INTEGER_CST
13448           && tree_int_cst_sgn (low_bound) == -1)
13449         {
13450           error_at (OMP_CLAUSE_LOCATION (c),
13451                     "negative low bound in array section in %qs clause",
13452                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13453           return error_mark_node;
13454         }
13455       if (length != NULL_TREE
13456           && TREE_CODE (length) == INTEGER_CST
13457           && tree_int_cst_sgn (length) == -1)
13458         {
13459           error_at (OMP_CLAUSE_LOCATION (c),
13460                     "negative length in array section in %qs clause",
13461                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13462           return error_mark_node;
13463         }
13464       if (TYPE_DOMAIN (type)
13465           && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13466           && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13467                         == INTEGER_CST)
13468         {
13469           tree size
13470             = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13471           size = size_binop (PLUS_EXPR, size, size_one_node);
13472           if (TREE_CODE (low_bound) == INTEGER_CST)
13473             {
13474               if (tree_int_cst_lt (size, low_bound))
13475                 {
13476                   error_at (OMP_CLAUSE_LOCATION (c),
13477                             "low bound %qE above array section size "
13478                             "in %qs clause", low_bound,
13479                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13480                   return error_mark_node;
13481                 }
13482               if (tree_int_cst_equal (size, low_bound))
13483                 {
13484                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13485                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13486                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13487                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13488                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13489                     {
13490                       error_at (OMP_CLAUSE_LOCATION (c),
13491                                 "zero length array section in %qs clause",
13492                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13493                       return error_mark_node;
13494                     }
13495                   maybe_zero_len = true;
13496                 }
13497               else if (length == NULL_TREE
13498                        && first_non_one == types.length ()
13499                        && tree_int_cst_equal
13500                             (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13501                              low_bound))
13502                 first_non_one++;
13503             }
13504           else if (length == NULL_TREE)
13505             {
13506               if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13507                   && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13508                   && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13509                   && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13510                   && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13511                 maybe_zero_len = true;
13512               if (first_non_one == types.length ())
13513                 first_non_one++;
13514             }
13515           if (length && TREE_CODE (length) == INTEGER_CST)
13516             {
13517               if (tree_int_cst_lt (size, length))
13518                 {
13519                   error_at (OMP_CLAUSE_LOCATION (c),
13520                             "length %qE above array section size "
13521                             "in %qs clause", length,
13522                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13523                   return error_mark_node;
13524                 }
13525               if (TREE_CODE (low_bound) == INTEGER_CST)
13526                 {
13527                   tree lbpluslen
13528                     = size_binop (PLUS_EXPR,
13529                                   fold_convert (sizetype, low_bound),
13530                                   fold_convert (sizetype, length));
13531                   if (TREE_CODE (lbpluslen) == INTEGER_CST
13532                       && tree_int_cst_lt (size, lbpluslen))
13533                     {
13534                       error_at (OMP_CLAUSE_LOCATION (c),
13535                                 "high bound %qE above array section size "
13536                                 "in %qs clause", lbpluslen,
13537                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13538                       return error_mark_node;
13539                     }
13540                 }
13541             }
13542         }
13543       else if (length == NULL_TREE)
13544         {
13545           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13546               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13547               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13548               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13549               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13550             maybe_zero_len = true;
13551           if (first_non_one == types.length ())
13552             first_non_one++;
13553         }
13554
13555       /* For [lb:] we will need to evaluate lb more than once.  */
13556       if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13557         {
13558           tree lb = save_expr (low_bound);
13559           if (lb != low_bound)
13560             {
13561               TREE_PURPOSE (t) = lb;
13562               low_bound = lb;
13563             }
13564         }
13565     }
13566   else if (TREE_CODE (type) == POINTER_TYPE)
13567     {
13568       if (length == NULL_TREE)
13569         {
13570           if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13571             error_at (OMP_CLAUSE_LOCATION (c),
13572                       "for array function parameter length expression "
13573                       "must be specified");
13574           else
13575             error_at (OMP_CLAUSE_LOCATION (c),
13576                       "for pointer type length expression must be specified");
13577           return error_mark_node;
13578         }
13579       if (length != NULL_TREE
13580           && TREE_CODE (length) == INTEGER_CST
13581           && tree_int_cst_sgn (length) == -1)
13582         {
13583           error_at (OMP_CLAUSE_LOCATION (c),
13584                     "negative length in array section in %qs clause",
13585                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13586           return error_mark_node;
13587         }
13588       /* If there is a pointer type anywhere but in the very first
13589          array-section-subscript, the array section could be non-contiguous.  */
13590       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13591           && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13592           && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13593         {
13594           /* If any prior dimension has a non-one length, then deem this
13595              array section as non-contiguous.  */
13596           for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
13597                d = TREE_CHAIN (d))
13598             {
13599               tree d_length = TREE_VALUE (d);
13600               if (d_length == NULL_TREE || !integer_onep (d_length))
13601                 {
13602                   error_at (OMP_CLAUSE_LOCATION (c),
13603                             "array section is not contiguous in %qs clause",
13604                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13605                   return error_mark_node;
13606                 }
13607             }
13608         }
13609     }
13610   else
13611     {
13612       error_at (OMP_CLAUSE_LOCATION (c),
13613                 "%qE does not have pointer or array type", ret);
13614       return error_mark_node;
13615     }
13616   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13617     types.safe_push (TREE_TYPE (ret));
13618   /* We will need to evaluate lb more than once.  */
13619   tree lb = save_expr (low_bound);
13620   if (lb != low_bound)
13621     {
13622       TREE_PURPOSE (t) = lb;
13623       low_bound = lb;
13624     }
13625   ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13626   return ret;
13627 }
13628
13629 /* Handle array sections for clause C.  */
13630
13631 static bool
13632 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13633 {
13634   bool maybe_zero_len = false;
13635   unsigned int first_non_one = 0;
13636   auto_vec<tree, 10> types;
13637   tree *tp = &OMP_CLAUSE_DECL (c);
13638   if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13639        || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13640       && TREE_CODE (*tp) == TREE_LIST
13641       && TREE_PURPOSE (*tp)
13642       && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13643     tp = &TREE_VALUE (*tp);
13644   tree first = handle_omp_array_sections_1 (c, *tp, types,
13645                                             maybe_zero_len, first_non_one,
13646                                             ort);
13647   if (first == error_mark_node)
13648     return true;
13649   if (first == NULL_TREE)
13650     return false;
13651   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13652       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13653     {
13654       tree t = *tp;
13655       tree tem = NULL_TREE;
13656       /* Need to evaluate side effects in the length expressions
13657          if any.  */
13658       while (TREE_CODE (t) == TREE_LIST)
13659         {
13660           if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13661             {
13662               if (tem == NULL_TREE)
13663                 tem = TREE_VALUE (t);
13664               else
13665                 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13666                               TREE_VALUE (t), tem);
13667             }
13668           t = TREE_CHAIN (t);
13669         }
13670       if (tem)
13671         first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13672       first = c_fully_fold (first, false, NULL, true);
13673       *tp = first;
13674     }
13675   else
13676     {
13677       unsigned int num = types.length (), i;
13678       tree t, side_effects = NULL_TREE, size = NULL_TREE;
13679       tree condition = NULL_TREE;
13680
13681       if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13682         maybe_zero_len = true;
13683
13684       for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13685            t = TREE_CHAIN (t))
13686         {
13687           tree low_bound = TREE_PURPOSE (t);
13688           tree length = TREE_VALUE (t);
13689
13690           i--;
13691           if (low_bound
13692               && TREE_CODE (low_bound) == INTEGER_CST
13693               && TYPE_PRECISION (TREE_TYPE (low_bound))
13694                  > TYPE_PRECISION (sizetype))
13695             low_bound = fold_convert (sizetype, low_bound);
13696           if (length
13697               && TREE_CODE (length) == INTEGER_CST
13698               && TYPE_PRECISION (TREE_TYPE (length))
13699                  > TYPE_PRECISION (sizetype))
13700             length = fold_convert (sizetype, length);
13701           if (low_bound == NULL_TREE)
13702             low_bound = integer_zero_node;
13703           if (!maybe_zero_len && i > first_non_one)
13704             {
13705               if (integer_nonzerop (low_bound))
13706                 goto do_warn_noncontiguous;
13707               if (length != NULL_TREE
13708                   && TREE_CODE (length) == INTEGER_CST
13709                   && TYPE_DOMAIN (types[i])
13710                   && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13711                   && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13712                      == INTEGER_CST)
13713                 {
13714                   tree size;
13715                   size = size_binop (PLUS_EXPR,
13716                                      TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13717                                      size_one_node);
13718                   if (!tree_int_cst_equal (length, size))
13719                     {
13720                      do_warn_noncontiguous:
13721                       error_at (OMP_CLAUSE_LOCATION (c),
13722                                 "array section is not contiguous in %qs "
13723                                 "clause",
13724                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13725                       return true;
13726                     }
13727                 }
13728               if (length != NULL_TREE
13729                   && TREE_SIDE_EFFECTS (length))
13730                 {
13731                   if (side_effects == NULL_TREE)
13732                     side_effects = length;
13733                   else
13734                     side_effects = build2 (COMPOUND_EXPR,
13735                                            TREE_TYPE (side_effects),
13736                                            length, side_effects);
13737                 }
13738             }
13739           else
13740             {
13741               tree l;
13742
13743               if (i > first_non_one
13744                   && ((length && integer_nonzerop (length))
13745                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13746                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13747                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13748                 continue;
13749               if (length)
13750                 l = fold_convert (sizetype, length);
13751               else
13752                 {
13753                   l = size_binop (PLUS_EXPR,
13754                                   TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13755                                   size_one_node);
13756                   l = size_binop (MINUS_EXPR, l,
13757                                   fold_convert (sizetype, low_bound));
13758                 }
13759               if (i > first_non_one)
13760                 {
13761                   l = fold_build2 (NE_EXPR, boolean_type_node, l,
13762                                    size_zero_node);
13763                   if (condition == NULL_TREE)
13764                     condition = l;
13765                   else
13766                     condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13767                                              l, condition);
13768                 }
13769               else if (size == NULL_TREE)
13770                 {
13771                   size = size_in_bytes (TREE_TYPE (types[i]));
13772                   tree eltype = TREE_TYPE (types[num - 1]);
13773                   while (TREE_CODE (eltype) == ARRAY_TYPE)
13774                     eltype = TREE_TYPE (eltype);
13775                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13776                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13777                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13778                     {
13779                       if (integer_zerop (size)
13780                           || integer_zerop (size_in_bytes (eltype)))
13781                         {
13782                           error_at (OMP_CLAUSE_LOCATION (c),
13783                                     "zero length array section in %qs clause",
13784                                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13785                           return error_mark_node;
13786                         }
13787                       size = size_binop (EXACT_DIV_EXPR, size,
13788                                          size_in_bytes (eltype));
13789                     }
13790                   size = size_binop (MULT_EXPR, size, l);
13791                   if (condition)
13792                     size = fold_build3 (COND_EXPR, sizetype, condition,
13793                                         size, size_zero_node);
13794                 }
13795               else
13796                 size = size_binop (MULT_EXPR, size, l);
13797             }
13798         }
13799       if (side_effects)
13800         size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13801       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13802           || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13803           || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13804         {
13805           size = size_binop (MINUS_EXPR, size, size_one_node);
13806           size = c_fully_fold (size, false, NULL);
13807           size = save_expr (size);
13808           tree index_type = build_index_type (size);
13809           tree eltype = TREE_TYPE (first);
13810           while (TREE_CODE (eltype) == ARRAY_TYPE)
13811             eltype = TREE_TYPE (eltype);
13812           tree type = build_array_type (eltype, index_type);
13813           tree ptype = build_pointer_type (eltype);
13814           if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13815             t = build_fold_addr_expr (t);
13816           tree t2 = build_fold_addr_expr (first);
13817           t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13818                                  ptrdiff_type_node, t2);
13819           t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13820                                 ptrdiff_type_node, t2,
13821                                 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13822                                                   ptrdiff_type_node, t));
13823           t2 = c_fully_fold (t2, false, NULL);
13824           if (tree_fits_shwi_p (t2))
13825             t = build2 (MEM_REF, type, t,
13826                         build_int_cst (ptype, tree_to_shwi (t2)));
13827           else
13828             {
13829               t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13830               t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13831                               TREE_TYPE (t), t, t2);
13832               t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13833             }
13834           OMP_CLAUSE_DECL (c) = t;
13835           return false;
13836         }
13837       first = c_fully_fold (first, false, NULL);
13838       OMP_CLAUSE_DECL (c) = first;
13839       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13840         return false;
13841       if (size)
13842         size = c_fully_fold (size, false, NULL);
13843       OMP_CLAUSE_SIZE (c) = size;
13844       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13845           || (TREE_CODE (t) == COMPONENT_REF
13846               && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13847         return false;
13848       gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13849       switch (OMP_CLAUSE_MAP_KIND (c))
13850         {
13851         case GOMP_MAP_ALLOC:
13852         case GOMP_MAP_IF_PRESENT:
13853         case GOMP_MAP_TO:
13854         case GOMP_MAP_FROM:
13855         case GOMP_MAP_TOFROM:
13856         case GOMP_MAP_ALWAYS_TO:
13857         case GOMP_MAP_ALWAYS_FROM:
13858         case GOMP_MAP_ALWAYS_TOFROM:
13859         case GOMP_MAP_RELEASE:
13860         case GOMP_MAP_DELETE:
13861         case GOMP_MAP_FORCE_TO:
13862         case GOMP_MAP_FORCE_FROM:
13863         case GOMP_MAP_FORCE_TOFROM:
13864         case GOMP_MAP_FORCE_PRESENT:
13865           OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13866           break;
13867         default:
13868           break;
13869         }
13870       tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13871       if (TREE_CODE (t) == COMPONENT_REF)
13872         OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
13873       else
13874         OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13875       OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
13876       if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13877           && !c_mark_addressable (t))
13878         return false;
13879       OMP_CLAUSE_DECL (c2) = t;
13880       t = build_fold_addr_expr (first);
13881       t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13882       tree ptr = OMP_CLAUSE_DECL (c2);
13883       if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13884         ptr = build_fold_addr_expr (ptr);
13885       t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13886                            ptrdiff_type_node, t,
13887                            fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13888                                              ptrdiff_type_node, ptr));
13889       t = c_fully_fold (t, false, NULL);
13890       OMP_CLAUSE_SIZE (c2) = t;
13891       OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13892       OMP_CLAUSE_CHAIN (c) = c2;
13893     }
13894   return false;
13895 }
13896
13897 /* Helper function of finish_omp_clauses.  Clone STMT as if we were making
13898    an inline call.  But, remap
13899    the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13900    and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL.  */
13901
13902 static tree
13903 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13904                  tree decl, tree placeholder)
13905 {
13906   copy_body_data id;
13907   hash_map<tree, tree> decl_map;
13908
13909   decl_map.put (omp_decl1, placeholder);
13910   decl_map.put (omp_decl2, decl);
13911   memset (&id, 0, sizeof (id));
13912   id.src_fn = DECL_CONTEXT (omp_decl1);
13913   id.dst_fn = current_function_decl;
13914   id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13915   id.decl_map = &decl_map;
13916
13917   id.copy_decl = copy_decl_no_change;
13918   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13919   id.transform_new_cfg = true;
13920   id.transform_return_to_modify = false;
13921   id.eh_lp_nr = 0;
13922   walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13923   return stmt;
13924 }
13925
13926 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13927    Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP.  */
13928
13929 static tree
13930 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13931 {
13932   if (*tp == (tree) data)
13933     return *tp;
13934   return NULL_TREE;
13935 }
13936
13937 /* Similarly, but also walk aggregate fields.  */
13938
13939 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13940
13941 static tree
13942 c_find_omp_var_r (tree *tp, int *, void *data)
13943 {
13944   if (*tp == ((struct c_find_omp_var_s *) data)->var)
13945     return *tp;
13946   if (RECORD_OR_UNION_TYPE_P (*tp))
13947     {
13948       tree field;
13949       hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13950
13951       for (field = TYPE_FIELDS (*tp); field;
13952            field = DECL_CHAIN (field))
13953         if (TREE_CODE (field) == FIELD_DECL)
13954           {
13955             tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13956                                   c_find_omp_var_r, data, pset);
13957             if (ret)
13958               return ret;
13959             ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13960             if (ret)
13961               return ret;
13962             ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13963                              pset);
13964             if (ret)
13965               return ret;
13966             ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13967             if (ret)
13968               return ret;
13969           }
13970     }
13971   else if (INTEGRAL_TYPE_P (*tp))
13972     return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13973                       ((struct c_find_omp_var_s *) data)->pset);
13974   return NULL_TREE;
13975 }
13976
13977 /* Finish OpenMP iterators ITER.  Return true if they are errorneous
13978    and clauses containing them should be removed.  */
13979
13980 static bool
13981 c_omp_finish_iterators (tree iter)
13982 {
13983   bool ret = false;
13984   for (tree it = iter; it; it = TREE_CHAIN (it))
13985     {
13986       tree var = TREE_VEC_ELT (it, 0);
13987       tree begin = TREE_VEC_ELT (it, 1);
13988       tree end = TREE_VEC_ELT (it, 2);
13989       tree step = TREE_VEC_ELT (it, 3);
13990       tree orig_step;
13991       tree type = TREE_TYPE (var);
13992       location_t loc = DECL_SOURCE_LOCATION (var);
13993       if (type == error_mark_node)
13994         {
13995           ret = true;
13996           continue;
13997         }
13998       if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13999         {
14000           error_at (loc, "iterator %qD has neither integral nor pointer type",
14001                     var);
14002           ret = true;
14003           continue;
14004         }
14005       else if (TYPE_ATOMIC (type))
14006         {
14007           error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
14008           ret = true;
14009           continue;
14010         }
14011       else if (TYPE_READONLY (type))
14012         {
14013           error_at (loc, "iterator %qD has const qualified type", var);
14014           ret = true;
14015           continue;
14016         }
14017       else if (step == error_mark_node
14018                || TREE_TYPE (step) == error_mark_node)
14019         {
14020           ret = true;
14021           continue;
14022         }
14023       else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14024         {
14025           error_at (EXPR_LOC_OR_LOC (step, loc),
14026                     "iterator step with non-integral type");
14027           ret = true;
14028           continue;
14029         }
14030       begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14031       end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14032       orig_step = save_expr (c_fully_fold (step, false, NULL));
14033       tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14034       step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14035       if (POINTER_TYPE_P (type))
14036         {
14037           begin = save_expr (begin);
14038           step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14039           step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14040                                   fold_convert (sizetype, step),
14041                                   fold_convert (sizetype, begin));
14042           step = fold_convert (ssizetype, step);
14043         }
14044       if (integer_zerop (step))
14045         {
14046           error_at (loc, "iterator %qD has zero step", var);
14047           ret = true;
14048           continue;
14049         }
14050
14051       if (begin == error_mark_node
14052           || end == error_mark_node
14053           || step == error_mark_node
14054           || orig_step == error_mark_node)
14055         {
14056           ret = true;
14057           continue;
14058         }
14059       hash_set<tree> pset;
14060       tree it2;
14061       for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14062         {
14063           tree var2 = TREE_VEC_ELT (it2, 0);
14064           tree begin2 = TREE_VEC_ELT (it2, 1);
14065           tree end2 = TREE_VEC_ELT (it2, 2);
14066           tree step2 = TREE_VEC_ELT (it2, 3);
14067           tree type2 = TREE_TYPE (var2);
14068           location_t loc2 = DECL_SOURCE_LOCATION (var2);
14069           struct c_find_omp_var_s data = { var, &pset };
14070           if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14071             {
14072               error_at (loc2,
14073                         "type of iterator %qD refers to outer iterator %qD",
14074                         var2, var);
14075               break;
14076             }
14077           else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14078             {
14079               error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14080                         "begin expression refers to outer iterator %qD", var);
14081               break;
14082             }
14083           else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14084             {
14085               error_at (EXPR_LOC_OR_LOC (end2, loc2),
14086                         "end expression refers to outer iterator %qD", var);
14087               break;
14088             }
14089           else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14090             {
14091               error_at (EXPR_LOC_OR_LOC (step2, loc2),
14092                         "step expression refers to outer iterator %qD", var);
14093               break;
14094             }
14095         }
14096       if (it2)
14097         {
14098           ret = true;
14099           continue;
14100         }
14101       TREE_VEC_ELT (it, 1) = begin;
14102       TREE_VEC_ELT (it, 2) = end;
14103       TREE_VEC_ELT (it, 3) = step;
14104       TREE_VEC_ELT (it, 4) = orig_step;
14105     }
14106   return ret;
14107 }
14108
14109 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14110    Return true if an error has been detected.  */
14111
14112 static bool
14113 c_oacc_check_attachments (tree c)
14114 {
14115   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14116     return false;
14117
14118   /* OpenACC attach / detach clauses must be pointers.  */
14119   if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14120       || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14121     {
14122       tree t = OMP_CLAUSE_DECL (c);
14123
14124       while (TREE_CODE (t) == TREE_LIST)
14125         t = TREE_CHAIN (t);
14126
14127       if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14128         {
14129           error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14130                     user_omp_clause_code_name (c, true));
14131           return true;
14132         }
14133     }
14134
14135   return false;
14136 }
14137
14138 /* For all elements of CLAUSES, validate them against their constraints.
14139    Remove any elements from the list that are invalid.  */
14140
14141 tree
14142 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14143 {
14144   bitmap_head generic_head, firstprivate_head, lastprivate_head;
14145   bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14146   bitmap_head oacc_reduction_head, is_on_device_head;
14147   tree c, t, type, *pc;
14148   tree simdlen = NULL_TREE, safelen = NULL_TREE;
14149   bool branch_seen = false;
14150   bool copyprivate_seen = false;
14151   bool mergeable_seen = false;
14152   tree *detach_seen = NULL;
14153   bool linear_variable_step_check = false;
14154   tree *nowait_clause = NULL;
14155   tree ordered_clause = NULL_TREE;
14156   tree schedule_clause = NULL_TREE;
14157   bool oacc_async = false;
14158   bool indir_component_ref_p = false;
14159   tree last_iterators = NULL_TREE;
14160   bool last_iterators_remove = false;
14161   tree *nogroup_seen = NULL;
14162   tree *order_clause = NULL;
14163   /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14164      has been seen, -2 if mixed inscan/normal reduction diagnosed.  */
14165   int reduction_seen = 0;
14166   bool allocate_seen = false;
14167   bool implicit_moved = false;
14168   bool target_in_reduction_seen = false;
14169
14170   bitmap_obstack_initialize (NULL);
14171   bitmap_initialize (&generic_head, &bitmap_default_obstack);
14172   bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14173   bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14174   bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14175   /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead.  */
14176   bitmap_initialize (&map_head, &bitmap_default_obstack);
14177   bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14178   bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14179   /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14180      instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head.  */
14181   bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14182   bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14183
14184   if (ort & C_ORT_ACC)
14185     for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14186       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14187         {
14188           oacc_async = true;
14189           break;
14190         }
14191
14192   for (pc = &clauses, c = clauses; c ; c = *pc)
14193     {
14194       bool remove = false;
14195       bool need_complete = false;
14196       bool need_implicitly_determined = false;
14197
14198       switch (OMP_CLAUSE_CODE (c))
14199         {
14200         case OMP_CLAUSE_SHARED:
14201           need_implicitly_determined = true;
14202           goto check_dup_generic;
14203
14204         case OMP_CLAUSE_PRIVATE:
14205           need_complete = true;
14206           need_implicitly_determined = true;
14207           goto check_dup_generic;
14208
14209         case OMP_CLAUSE_REDUCTION:
14210           if (reduction_seen == 0)
14211             reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14212           else if (reduction_seen != -2
14213                    && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14214                                          ? -1 : 1))
14215             {
14216               error_at (OMP_CLAUSE_LOCATION (c),
14217                         "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14218                         "on the same construct");
14219               reduction_seen = -2;
14220             }
14221           /* FALLTHRU */
14222         case OMP_CLAUSE_IN_REDUCTION:
14223         case OMP_CLAUSE_TASK_REDUCTION:
14224           need_implicitly_determined = true;
14225           t = OMP_CLAUSE_DECL (c);
14226           if (TREE_CODE (t) == TREE_LIST)
14227             {
14228               if (handle_omp_array_sections (c, ort))
14229                 {
14230                   remove = true;
14231                   break;
14232                 }
14233
14234               t = OMP_CLAUSE_DECL (c);
14235               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14236                   && OMP_CLAUSE_REDUCTION_INSCAN (c))
14237                 {
14238                   error_at (OMP_CLAUSE_LOCATION (c),
14239                             "%<inscan%> %<reduction%> clause with array "
14240                             "section");
14241                   remove = true;
14242                   break;
14243                 }
14244             }
14245           t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14246           if (t == error_mark_node)
14247             {
14248               remove = true;
14249               break;
14250             }
14251           if (oacc_async)
14252             c_mark_addressable (t);
14253           type = TREE_TYPE (t);
14254           if (TREE_CODE (t) == MEM_REF)
14255             type = TREE_TYPE (type);
14256           if (TREE_CODE (type) == ARRAY_TYPE)
14257             {
14258               tree oatype = type;
14259               gcc_assert (TREE_CODE (t) != MEM_REF);
14260               while (TREE_CODE (type) == ARRAY_TYPE)
14261                 type = TREE_TYPE (type);
14262               if (integer_zerop (TYPE_SIZE_UNIT (type)))
14263                 {
14264                   error_at (OMP_CLAUSE_LOCATION (c),
14265                             "%qD in %<reduction%> clause is a zero size array",
14266                             t);
14267                   remove = true;
14268                   break;
14269                 }
14270               tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14271                                       TYPE_SIZE_UNIT (type));
14272               if (integer_zerop (size))
14273                 {
14274                   error_at (OMP_CLAUSE_LOCATION (c),
14275                             "%qD in %<reduction%> clause is a zero size array",
14276                             t);
14277                   remove = true;
14278                   break;
14279                 }
14280               size = size_binop (MINUS_EXPR, size, size_one_node);
14281               size = save_expr (size);
14282               tree index_type = build_index_type (size);
14283               tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14284                                              index_type);
14285               atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14286               tree ptype = build_pointer_type (type);
14287               if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14288                 t = build_fold_addr_expr (t);
14289               t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14290               OMP_CLAUSE_DECL (c) = t;
14291             }
14292           if (TYPE_ATOMIC (type))
14293             {
14294               error_at (OMP_CLAUSE_LOCATION (c),
14295                         "%<_Atomic%> %qE in %<reduction%> clause", t);
14296               remove = true;
14297               break;
14298             }
14299           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14300               || OMP_CLAUSE_REDUCTION_TASK (c))
14301             {
14302               /* Disallow zero sized or potentially zero sized task
14303                  reductions.  */
14304               if (integer_zerop (TYPE_SIZE_UNIT (type)))
14305                 {
14306                   error_at (OMP_CLAUSE_LOCATION (c),
14307                             "zero sized type %qT in %qs clause", type,
14308                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14309                   remove = true;
14310                   break;
14311                 }
14312               else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14313                 {
14314                   error_at (OMP_CLAUSE_LOCATION (c),
14315                             "variable sized type %qT in %qs clause", type,
14316                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14317                   remove = true;
14318                   break;
14319                 }
14320             }
14321           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14322               && (FLOAT_TYPE_P (type)
14323                   || TREE_CODE (type) == COMPLEX_TYPE))
14324             {
14325               enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14326               const char *r_name = NULL;
14327
14328               switch (r_code)
14329                 {
14330                 case PLUS_EXPR:
14331                 case MULT_EXPR:
14332                 case MINUS_EXPR:
14333                 case TRUTH_ANDIF_EXPR:
14334                 case TRUTH_ORIF_EXPR:
14335                   break;
14336                 case MIN_EXPR:
14337                   if (TREE_CODE (type) == COMPLEX_TYPE)
14338                     r_name = "min";
14339                   break;
14340                 case MAX_EXPR:
14341                   if (TREE_CODE (type) == COMPLEX_TYPE)
14342                     r_name = "max";
14343                   break;
14344                 case BIT_AND_EXPR:
14345                   r_name = "&";
14346                   break;
14347                 case BIT_XOR_EXPR:
14348                   r_name = "^";
14349                   break;
14350                 case BIT_IOR_EXPR:
14351                   r_name = "|";
14352                   break;
14353                 default:
14354                   gcc_unreachable ();
14355                 }
14356               if (r_name)
14357                 {
14358                   error_at (OMP_CLAUSE_LOCATION (c),
14359                             "%qE has invalid type for %<reduction(%s)%>",
14360                             t, r_name);
14361                   remove = true;
14362                   break;
14363                 }
14364             }
14365           else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14366             {
14367               error_at (OMP_CLAUSE_LOCATION (c),
14368                         "user defined reduction not found for %qE", t);
14369               remove = true;
14370               break;
14371             }
14372           else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14373             {
14374               tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14375               type = TYPE_MAIN_VARIANT (type);
14376               tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14377                                              VAR_DECL, NULL_TREE, type);
14378               tree decl_placeholder = NULL_TREE;
14379               OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14380               DECL_ARTIFICIAL (placeholder) = 1;
14381               DECL_IGNORED_P (placeholder) = 1;
14382               if (TREE_CODE (t) == MEM_REF)
14383                 {
14384                   decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14385                                                  VAR_DECL, NULL_TREE, type);
14386                   OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14387                   DECL_ARTIFICIAL (decl_placeholder) = 1;
14388                   DECL_IGNORED_P (decl_placeholder) = 1;
14389                 }
14390               if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14391                 c_mark_addressable (placeholder);
14392               if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14393                 c_mark_addressable (decl_placeholder ? decl_placeholder
14394                                     : OMP_CLAUSE_DECL (c));
14395               OMP_CLAUSE_REDUCTION_MERGE (c)
14396                 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14397                                    TREE_VEC_ELT (list, 0),
14398                                    TREE_VEC_ELT (list, 1),
14399                                    decl_placeholder ? decl_placeholder
14400                                    : OMP_CLAUSE_DECL (c), placeholder);
14401               OMP_CLAUSE_REDUCTION_MERGE (c)
14402                 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14403                               void_type_node, NULL_TREE,
14404                               OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14405               TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14406               if (TREE_VEC_LENGTH (list) == 6)
14407                 {
14408                   if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14409                     c_mark_addressable (decl_placeholder ? decl_placeholder
14410                                         : OMP_CLAUSE_DECL (c));
14411                   if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14412                     c_mark_addressable (placeholder);
14413                   tree init = TREE_VEC_ELT (list, 5);
14414                   if (init == error_mark_node)
14415                     init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14416                   OMP_CLAUSE_REDUCTION_INIT (c)
14417                     = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14418                                        TREE_VEC_ELT (list, 3),
14419                                        decl_placeholder ? decl_placeholder
14420                                        : OMP_CLAUSE_DECL (c), placeholder);
14421                   if (TREE_VEC_ELT (list, 5) == error_mark_node)
14422                     {
14423                       tree v = decl_placeholder ? decl_placeholder : t;
14424                       OMP_CLAUSE_REDUCTION_INIT (c)
14425                         = build2 (INIT_EXPR, TREE_TYPE (v), v,
14426                                   OMP_CLAUSE_REDUCTION_INIT (c));
14427                     }
14428                   if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14429                                  c_find_omp_placeholder_r,
14430                                  placeholder, NULL))
14431                     OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14432                 }
14433               else
14434                 {
14435                   tree init;
14436                   tree v = decl_placeholder ? decl_placeholder : t;
14437                   if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14438                     init = build_constructor (TREE_TYPE (v), NULL);
14439                   else
14440                     init = fold_convert (TREE_TYPE (v), integer_zero_node);
14441                   OMP_CLAUSE_REDUCTION_INIT (c)
14442                     = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14443                 }
14444               OMP_CLAUSE_REDUCTION_INIT (c)
14445                 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14446                               void_type_node, NULL_TREE,
14447                                OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14448               TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14449             }
14450           if (TREE_CODE (t) == MEM_REF)
14451             {
14452               if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14453                   || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14454                      != INTEGER_CST)
14455                 {
14456                   sorry ("variable length element type in array "
14457                          "%<reduction%> clause");
14458                   remove = true;
14459                   break;
14460                 }
14461               t = TREE_OPERAND (t, 0);
14462               if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14463                 t = TREE_OPERAND (t, 0);
14464               if (TREE_CODE (t) == ADDR_EXPR)
14465                 t = TREE_OPERAND (t, 0);
14466             }
14467           goto check_dup_generic_t;
14468
14469         case OMP_CLAUSE_COPYPRIVATE:
14470           copyprivate_seen = true;
14471           if (nowait_clause)
14472             {
14473               error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14474                         "%<nowait%> clause must not be used together "
14475                         "with %<copyprivate%>");
14476               *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14477               nowait_clause = NULL;
14478             }
14479           goto check_dup_generic;
14480
14481         case OMP_CLAUSE_COPYIN:
14482           t = OMP_CLAUSE_DECL (c);
14483           if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14484             {
14485               error_at (OMP_CLAUSE_LOCATION (c),
14486                         "%qE must be %<threadprivate%> for %<copyin%>", t);
14487               remove = true;
14488               break;
14489             }
14490           goto check_dup_generic;
14491
14492         case OMP_CLAUSE_LINEAR:
14493           if (ort != C_ORT_OMP_DECLARE_SIMD)
14494             need_implicitly_determined = true;
14495           t = OMP_CLAUSE_DECL (c);
14496           if (ort != C_ORT_OMP_DECLARE_SIMD
14497               && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
14498               && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
14499             {
14500               error_at (OMP_CLAUSE_LOCATION (c),
14501                         "modifier should not be specified in %<linear%> "
14502                         "clause on %<simd%> or %<for%> constructs when not "
14503                         "using OpenMP 5.2 modifiers");
14504               OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14505             }
14506           if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14507               && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14508             {
14509               error_at (OMP_CLAUSE_LOCATION (c),
14510                         "linear clause applied to non-integral non-pointer "
14511                         "variable with type %qT", TREE_TYPE (t));
14512               remove = true;
14513               break;
14514             }
14515           if (TYPE_ATOMIC (TREE_TYPE (t)))
14516             {
14517               error_at (OMP_CLAUSE_LOCATION (c),
14518                         "%<_Atomic%> %qD in %<linear%> clause", t);
14519               remove = true;
14520               break;
14521             }
14522           if (ort == C_ORT_OMP_DECLARE_SIMD)
14523             {
14524               tree s = OMP_CLAUSE_LINEAR_STEP (c);
14525               if (TREE_CODE (s) == PARM_DECL)
14526                 {
14527                   OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14528                   /* map_head bitmap is used as uniform_head if
14529                      declare_simd.  */
14530                   if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14531                     linear_variable_step_check = true;
14532                   goto check_dup_generic;
14533                 }
14534               if (TREE_CODE (s) != INTEGER_CST)
14535                 {
14536                   error_at (OMP_CLAUSE_LOCATION (c),
14537                             "%<linear%> clause step %qE is neither constant "
14538                             "nor a parameter", s);
14539                   remove = true;
14540                   break;
14541                 }
14542             }
14543           if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14544             {
14545               tree s = OMP_CLAUSE_LINEAR_STEP (c);
14546               s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14547                                    OMP_CLAUSE_DECL (c), s);
14548               s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14549                                    sizetype, fold_convert (sizetype, s),
14550                                    fold_convert
14551                                      (sizetype, OMP_CLAUSE_DECL (c)));
14552               if (s == error_mark_node)
14553                 s = size_one_node;
14554               OMP_CLAUSE_LINEAR_STEP (c) = s;
14555             }
14556           else
14557             OMP_CLAUSE_LINEAR_STEP (c)
14558               = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14559           goto check_dup_generic;
14560
14561         check_dup_generic:
14562           t = OMP_CLAUSE_DECL (c);
14563         check_dup_generic_t:
14564           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14565             {
14566               error_at (OMP_CLAUSE_LOCATION (c),
14567                         "%qE is not a variable in clause %qs", t,
14568                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14569               remove = true;
14570             }
14571           else if ((ort == C_ORT_ACC
14572                     && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14573                    || (ort == C_ORT_OMP
14574                        && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14575                            || (OMP_CLAUSE_CODE (c)
14576                                == OMP_CLAUSE_USE_DEVICE_ADDR)))
14577                    || (ort == C_ORT_OMP_TARGET
14578                        && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14579             {
14580               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14581                   && (bitmap_bit_p (&generic_head, DECL_UID (t))
14582                       || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14583                 {
14584                   error_at (OMP_CLAUSE_LOCATION (c),
14585                             "%qD appears more than once in data-sharing "
14586                             "clauses", t);
14587                   remove = true;
14588                   break;
14589                 }
14590               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14591                 target_in_reduction_seen = true;
14592               if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14593                 {
14594                   error_at (OMP_CLAUSE_LOCATION (c),
14595                             ort == C_ORT_ACC
14596                             ? "%qD appears more than once in reduction clauses"
14597                             : "%qD appears more than once in data clauses",
14598                             t);
14599                   remove = true;
14600                 }
14601               else
14602                 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14603             }
14604           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14605                    || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14606                    || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
14607                    || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14608             {
14609               error_at (OMP_CLAUSE_LOCATION (c),
14610                         "%qE appears more than once in data clauses", t);
14611               remove = true;
14612             }
14613           else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14614                     || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
14615                     || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
14616                    && bitmap_bit_p (&map_head, DECL_UID (t)))
14617             {
14618               if (ort == C_ORT_ACC)
14619                 error_at (OMP_CLAUSE_LOCATION (c),
14620                           "%qD appears more than once in data clauses", t);
14621               else
14622                 error_at (OMP_CLAUSE_LOCATION (c),
14623                           "%qD appears both in data and map clauses", t);
14624               remove = true;
14625             }
14626           else
14627             bitmap_set_bit (&generic_head, DECL_UID (t));
14628           break;
14629
14630         case OMP_CLAUSE_FIRSTPRIVATE:
14631           if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
14632             {
14633             move_implicit:
14634               implicit_moved = true;
14635               /* Move firstprivate and map clauses with
14636                  OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
14637                  clauses chain.  */
14638               tree cl1 = NULL_TREE, cl2 = NULL_TREE;
14639               tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
14640               while (*pc1)
14641                 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
14642                     && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
14643                   {
14644                     *pc3 = *pc1;
14645                     pc3 = &OMP_CLAUSE_CHAIN (*pc3);
14646                     *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14647                   }
14648                 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
14649                          && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
14650                   {
14651                     *pc2 = *pc1;
14652                     pc2 = &OMP_CLAUSE_CHAIN (*pc2);
14653                     *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14654                   }
14655                 else
14656                   pc1 = &OMP_CLAUSE_CHAIN (*pc1);
14657               *pc3 = NULL;
14658               *pc2 = cl2;
14659               *pc1 = cl1;
14660               continue;
14661             }
14662           t = OMP_CLAUSE_DECL (c);
14663           need_complete = true;
14664           need_implicitly_determined = true;
14665           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14666             {
14667               error_at (OMP_CLAUSE_LOCATION (c),
14668                         "%qE is not a variable in clause %<firstprivate%>", t);
14669               remove = true;
14670             }
14671           else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14672                    && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
14673                    && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14674             remove = true;
14675           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14676                    || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14677                    || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14678             {
14679               error_at (OMP_CLAUSE_LOCATION (c),
14680                         "%qE appears more than once in data clauses", t);
14681               remove = true;
14682             }
14683           else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14684             {
14685               if (ort == C_ORT_ACC)
14686                 error_at (OMP_CLAUSE_LOCATION (c),
14687                           "%qD appears more than once in data clauses", t);
14688               else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14689                        && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
14690                 /* Silently drop the clause.  */;
14691               else
14692                 error_at (OMP_CLAUSE_LOCATION (c),
14693                           "%qD appears both in data and map clauses", t);
14694               remove = true;
14695             }
14696           else
14697             bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14698           break;
14699
14700         case OMP_CLAUSE_LASTPRIVATE:
14701           t = OMP_CLAUSE_DECL (c);
14702           need_complete = true;
14703           need_implicitly_determined = true;
14704           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14705             {
14706               error_at (OMP_CLAUSE_LOCATION (c),
14707                         "%qE is not a variable in clause %<lastprivate%>", t);
14708               remove = true;
14709             }
14710           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14711                    || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14712             {
14713               error_at (OMP_CLAUSE_LOCATION (c),
14714                      "%qE appears more than once in data clauses", t);
14715               remove = true;
14716             }
14717           else
14718             bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14719           break;
14720
14721         case OMP_CLAUSE_ALIGNED:
14722           t = OMP_CLAUSE_DECL (c);
14723           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14724             {
14725               error_at (OMP_CLAUSE_LOCATION (c),
14726                         "%qE is not a variable in %<aligned%> clause", t);
14727               remove = true;
14728             }
14729           else if (!POINTER_TYPE_P (TREE_TYPE (t))
14730                    && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14731             {
14732               error_at (OMP_CLAUSE_LOCATION (c),
14733                         "%qE in %<aligned%> clause is neither a pointer nor "
14734                         "an array", t);
14735               remove = true;
14736             }
14737           else if (TYPE_ATOMIC (TREE_TYPE (t)))
14738             {
14739               error_at (OMP_CLAUSE_LOCATION (c),
14740                         "%<_Atomic%> %qD in %<aligned%> clause", t);
14741               remove = true;
14742               break;
14743             }
14744           else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14745             {
14746               error_at (OMP_CLAUSE_LOCATION (c),
14747                         "%qE appears more than once in %<aligned%> clauses",
14748                         t);
14749               remove = true;
14750             }
14751           else
14752             bitmap_set_bit (&aligned_head, DECL_UID (t));
14753           break;
14754
14755         case OMP_CLAUSE_NONTEMPORAL:
14756           t = OMP_CLAUSE_DECL (c);
14757           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14758             {
14759               error_at (OMP_CLAUSE_LOCATION (c),
14760                         "%qE is not a variable in %<nontemporal%> clause", t);
14761               remove = true;
14762             }
14763           else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14764             {
14765               error_at (OMP_CLAUSE_LOCATION (c),
14766                         "%qE appears more than once in %<nontemporal%> "
14767                         "clauses", t);
14768               remove = true;
14769             }
14770           else
14771             bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14772           break;
14773
14774         case OMP_CLAUSE_ALLOCATE:
14775           t = OMP_CLAUSE_DECL (c);
14776           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14777             {
14778               error_at (OMP_CLAUSE_LOCATION (c),
14779                         "%qE is not a variable in %<allocate%> clause", t);
14780               remove = true;
14781             }
14782           else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14783             {
14784               warning_at (OMP_CLAUSE_LOCATION (c), 0,
14785                           "%qE appears more than once in %<allocate%> clauses",
14786                           t);
14787               remove = true;
14788             }
14789           else
14790             {
14791               bitmap_set_bit (&aligned_head, DECL_UID (t));
14792               if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
14793                 allocate_seen = true;
14794             }
14795           break;
14796
14797         case OMP_CLAUSE_DEPEND:
14798           t = OMP_CLAUSE_DECL (c);
14799           if (t == NULL_TREE)
14800             {
14801               gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14802                           == OMP_CLAUSE_DEPEND_SOURCE);
14803               break;
14804             }
14805           if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14806             {
14807               gcc_assert (TREE_CODE (t) == TREE_LIST);
14808               for (; t; t = TREE_CHAIN (t))
14809                 {
14810                   tree decl = TREE_VALUE (t);
14811                   if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14812                     {
14813                       tree offset = TREE_PURPOSE (t);
14814                       bool neg = wi::neg_p (wi::to_wide (offset));
14815                       offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14816                       tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14817                                                  neg ? MINUS_EXPR : PLUS_EXPR,
14818                                                  decl, offset);
14819                       t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14820                                             sizetype,
14821                                             fold_convert (sizetype, t2),
14822                                             fold_convert (sizetype, decl));
14823                       if (t2 == error_mark_node)
14824                         {
14825                           remove = true;
14826                           break;
14827                         }
14828                       TREE_PURPOSE (t) = t2;
14829                     }
14830                 }
14831               break;
14832             }
14833           /* FALLTHRU */
14834         case OMP_CLAUSE_AFFINITY:
14835           t = OMP_CLAUSE_DECL (c);
14836           if (TREE_CODE (t) == TREE_LIST
14837               && TREE_PURPOSE (t)
14838               && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14839             {
14840               if (TREE_PURPOSE (t) != last_iterators)
14841                 last_iterators_remove
14842                   = c_omp_finish_iterators (TREE_PURPOSE (t));
14843               last_iterators = TREE_PURPOSE (t);
14844               t = TREE_VALUE (t);
14845               if (last_iterators_remove)
14846                 t = error_mark_node;
14847             }
14848           else
14849             last_iterators = NULL_TREE;
14850           if (TREE_CODE (t) == TREE_LIST)
14851             {
14852               if (handle_omp_array_sections (c, ort))
14853                 remove = true;
14854               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14855                        && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14856                 {
14857                   error_at (OMP_CLAUSE_LOCATION (c),
14858                             "%<depend%> clause with %<depobj%> dependence "
14859                             "type on array section");
14860                   remove = true;
14861                 }
14862               break;
14863             }
14864           if (t == error_mark_node)
14865             remove = true;
14866           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14867                    && t == ridpointers[RID_OMP_ALL_MEMORY])
14868             {
14869               if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
14870                   && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
14871                 {
14872                   error_at (OMP_CLAUSE_LOCATION (c),
14873                             "%<omp_all_memory%> used with %<depend%> kind "
14874                             "other than %<out%> or %<inout%>");
14875                   remove = true;
14876                 }
14877             }
14878           else if (!lvalue_p (t))
14879             {
14880               error_at (OMP_CLAUSE_LOCATION (c),
14881                         "%qE is not lvalue expression nor array section in "
14882                         "%qs clause", t,
14883                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14884               remove = true;
14885             }
14886           else if (TREE_CODE (t) == COMPONENT_REF
14887                    && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14888             {
14889               gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14890                           || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
14891               error_at (OMP_CLAUSE_LOCATION (c),
14892                         "bit-field %qE in %qs clause", t,
14893                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14894               remove = true;
14895             }
14896           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14897                    && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14898             {
14899               if (!c_omp_depend_t_p (TREE_TYPE (t)))
14900                 {
14901                   error_at (OMP_CLAUSE_LOCATION (c),
14902                             "%qE does not have %<omp_depend_t%> type in "
14903                             "%<depend%> clause with %<depobj%> dependence "
14904                             "type", t);
14905                   remove = true;
14906                 }
14907             }
14908           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14909                    && c_omp_depend_t_p (TREE_TYPE (t)))
14910             {
14911               error_at (OMP_CLAUSE_LOCATION (c),
14912                         "%qE should not have %<omp_depend_t%> type in "
14913                         "%<depend%> clause with dependence type other than "
14914                         "%<depobj%>", t);
14915               remove = true;
14916             }
14917           if (!remove)
14918             {
14919               if (t == ridpointers[RID_OMP_ALL_MEMORY])
14920                 t = null_pointer_node;
14921               else
14922                 {
14923                   tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
14924                                               ADDR_EXPR, t, false);
14925                   if (addr == error_mark_node)
14926                     {
14927                       remove = true;
14928                       break;
14929                     }
14930                   t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14931                                           RO_UNARY_STAR);
14932                   if (t == error_mark_node)
14933                     {
14934                       remove = true;
14935                       break;
14936                     }
14937                 }
14938               if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14939                   && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14940                   && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14941                       == TREE_VEC))
14942                 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14943               else
14944                 OMP_CLAUSE_DECL (c) = t;
14945             }
14946           break;
14947
14948         case OMP_CLAUSE_MAP:
14949           if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
14950             goto move_implicit;
14951           /* FALLTHRU */
14952         case OMP_CLAUSE_TO:
14953         case OMP_CLAUSE_FROM:
14954         case OMP_CLAUSE__CACHE_:
14955           t = OMP_CLAUSE_DECL (c);
14956           if (TREE_CODE (t) == TREE_LIST)
14957             {
14958               if (handle_omp_array_sections (c, ort))
14959                 remove = true;
14960               else
14961                 {
14962                   t = OMP_CLAUSE_DECL (c);
14963                   if (!omp_mappable_type (TREE_TYPE (t)))
14964                     {
14965                       error_at (OMP_CLAUSE_LOCATION (c),
14966                                 "array section does not have mappable type "
14967                                 "in %qs clause",
14968                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14969                       remove = true;
14970                     }
14971                   else if (TYPE_ATOMIC (TREE_TYPE (t)))
14972                     {
14973                       error_at (OMP_CLAUSE_LOCATION (c),
14974                                 "%<_Atomic%> %qE in %qs clause", t,
14975                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14976                       remove = true;
14977                     }
14978                   while (TREE_CODE (t) == ARRAY_REF)
14979                     t = TREE_OPERAND (t, 0);
14980                   if (TREE_CODE (t) == COMPONENT_REF
14981                       && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14982                     {
14983                       do
14984                         {
14985                           t = TREE_OPERAND (t, 0);
14986                           if (TREE_CODE (t) == MEM_REF
14987                               || TREE_CODE (t) == INDIRECT_REF)
14988                             {
14989                               t = TREE_OPERAND (t, 0);
14990                               STRIP_NOPS (t);
14991                               if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14992                                 t = TREE_OPERAND (t, 0);
14993                             }
14994                         }
14995                       while (TREE_CODE (t) == COMPONENT_REF
14996                              || TREE_CODE (t) == ARRAY_REF);
14997
14998                       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14999                           && OMP_CLAUSE_MAP_IMPLICIT (c)
15000                           && (bitmap_bit_p (&map_head, DECL_UID (t))
15001                               || bitmap_bit_p (&map_field_head, DECL_UID (t))
15002                               || bitmap_bit_p (&map_firstprivate_head,
15003                                                DECL_UID (t))))
15004                         {
15005                           remove = true;
15006                           break;
15007                         }
15008                       if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
15009                         break;
15010                       if (bitmap_bit_p (&map_head, DECL_UID (t)))
15011                         {
15012                           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15013                             error_at (OMP_CLAUSE_LOCATION (c),
15014                                       "%qD appears more than once in motion "
15015                                       "clauses", t);
15016                           else if (ort == C_ORT_ACC)
15017                             error_at (OMP_CLAUSE_LOCATION (c),
15018                                       "%qD appears more than once in data "
15019                                       "clauses", t);
15020                           else
15021                             error_at (OMP_CLAUSE_LOCATION (c),
15022                                       "%qD appears more than once in map "
15023                                       "clauses", t);
15024                           remove = true;
15025                         }
15026                       else
15027                         {
15028                           bitmap_set_bit (&map_head, DECL_UID (t));
15029                           bitmap_set_bit (&map_field_head, DECL_UID (t));
15030                         }
15031                     }
15032                 }
15033               if (c_oacc_check_attachments (c))
15034                 remove = true;
15035               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15036                   && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15037                       || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15038                 /* In this case, we have a single array element which is a
15039                    pointer, and we already set OMP_CLAUSE_SIZE in
15040                    handle_omp_array_sections above.  For attach/detach clauses,
15041                    reset the OMP_CLAUSE_SIZE (representing a bias) to zero
15042                    here.  */
15043                 OMP_CLAUSE_SIZE (c) = size_zero_node;
15044               break;
15045             }
15046           if (t == error_mark_node)
15047             {
15048               remove = true;
15049               break;
15050             }
15051           /* OpenACC attach / detach clauses must be pointers.  */
15052           if (c_oacc_check_attachments (c))
15053             {
15054               remove = true;
15055               break;
15056             }
15057           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15058               && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15059                   || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15060             /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15061                bias) to zero here, so it is not set erroneously to the pointer
15062                size later on in gimplify.cc.  */
15063             OMP_CLAUSE_SIZE (c) = size_zero_node;
15064           while (TREE_CODE (t) == INDIRECT_REF
15065                  || TREE_CODE (t) == ARRAY_REF)
15066             {
15067               t = TREE_OPERAND (t, 0);
15068               STRIP_NOPS (t);
15069               if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15070                 t = TREE_OPERAND (t, 0);
15071             }
15072           while (TREE_CODE (t) == COMPOUND_EXPR)
15073             {
15074               t = TREE_OPERAND (t, 1);
15075               STRIP_NOPS (t);
15076             }
15077           indir_component_ref_p = false;
15078           if (TREE_CODE (t) == COMPONENT_REF
15079               && (TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
15080                   || TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF
15081                   || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
15082             {
15083               t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
15084               indir_component_ref_p = true;
15085               STRIP_NOPS (t);
15086               if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15087                 t = TREE_OPERAND (t, 0);
15088             }
15089
15090           if (TREE_CODE (t) == COMPONENT_REF
15091               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
15092             {
15093               if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
15094                 {
15095                   error_at (OMP_CLAUSE_LOCATION (c),
15096                             "bit-field %qE in %qs clause",
15097                             t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15098                   remove = true;
15099                 }
15100               else if (!omp_mappable_type (TREE_TYPE (t)))
15101                 {
15102                   error_at (OMP_CLAUSE_LOCATION (c),
15103                             "%qE does not have a mappable type in %qs clause",
15104                             t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15105                   remove = true;
15106                 }
15107               else if (TYPE_ATOMIC (TREE_TYPE (t)))
15108                 {
15109                   error_at (OMP_CLAUSE_LOCATION (c),
15110                             "%<_Atomic%> %qE in %qs clause", t,
15111                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15112                   remove = true;
15113                 }
15114               while (TREE_CODE (t) == COMPONENT_REF)
15115                 {
15116                   if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
15117                       == UNION_TYPE)
15118                     {
15119                       error_at (OMP_CLAUSE_LOCATION (c),
15120                                 "%qE is a member of a union", t);
15121                       remove = true;
15122                       break;
15123                     }
15124                   t = TREE_OPERAND (t, 0);
15125                   if (TREE_CODE (t) == MEM_REF)
15126                     {
15127                       if (maybe_ne (mem_ref_offset (t), 0))
15128                         error_at (OMP_CLAUSE_LOCATION (c),
15129                                   "cannot dereference %qE in %qs clause", t,
15130                                   omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15131                       else
15132                         t = TREE_OPERAND (t, 0);
15133                     }
15134                   while (TREE_CODE (t) == MEM_REF
15135                          || TREE_CODE (t) == INDIRECT_REF
15136                          || TREE_CODE (t) == ARRAY_REF)
15137                     {
15138                       t = TREE_OPERAND (t, 0);
15139                       STRIP_NOPS (t);
15140                       if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15141                         t = TREE_OPERAND (t, 0);
15142                     }
15143                 }
15144               if (remove)
15145                 break;
15146               if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15147                 {
15148                   if (bitmap_bit_p (&map_field_head, DECL_UID (t))
15149                       || (ort != C_ORT_ACC
15150                           && bitmap_bit_p (&map_head, DECL_UID (t))))
15151                     break;
15152                 }
15153             }
15154           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15155             {
15156               error_at (OMP_CLAUSE_LOCATION (c),
15157                         "%qE is not a variable in %qs clause", t,
15158                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15159               remove = true;
15160             }
15161           else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15162             {
15163               error_at (OMP_CLAUSE_LOCATION (c),
15164                         "%qD is threadprivate variable in %qs clause", t,
15165                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15166               remove = true;
15167             }
15168           else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15169                     || (OMP_CLAUSE_MAP_KIND (c)
15170                         != GOMP_MAP_FIRSTPRIVATE_POINTER))
15171                    && !indir_component_ref_p
15172                    && !c_mark_addressable (t))
15173             remove = true;
15174           else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15175                      && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15176                          || (OMP_CLAUSE_MAP_KIND (c)
15177                              == GOMP_MAP_FIRSTPRIVATE_POINTER)
15178                          || (OMP_CLAUSE_MAP_KIND (c)
15179                              == GOMP_MAP_FORCE_DEVICEPTR)))
15180                    && t == OMP_CLAUSE_DECL (c)
15181                    && !omp_mappable_type (TREE_TYPE (t)))
15182             {
15183               error_at (OMP_CLAUSE_LOCATION (c),
15184                         "%qD does not have a mappable type in %qs clause", t,
15185                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15186               remove = true;
15187             }
15188           else if (TREE_TYPE (t) == error_mark_node)
15189             remove = true;
15190           else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15191             {
15192               error_at (OMP_CLAUSE_LOCATION (c),
15193                         "%<_Atomic%> %qE in %qs clause", t,
15194                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15195               remove = true;
15196             }
15197           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15198                    && OMP_CLAUSE_MAP_IMPLICIT (c)
15199                    && (bitmap_bit_p (&map_head, DECL_UID (t))
15200                        || bitmap_bit_p (&map_field_head, DECL_UID (t))
15201                        || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
15202             remove = true;
15203           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15204                    && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15205             {
15206               if (bitmap_bit_p (&generic_head, DECL_UID (t))
15207                   || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15208                   || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15209                 {
15210                   error_at (OMP_CLAUSE_LOCATION (c),
15211                             "%qD appears more than once in data clauses", t);
15212                   remove = true;
15213                 }
15214               else if (bitmap_bit_p (&map_head, DECL_UID (t))
15215                        && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15216                 {
15217                   if (ort == C_ORT_ACC)
15218                     error_at (OMP_CLAUSE_LOCATION (c),
15219                               "%qD appears more than once in data clauses", t);
15220                   else
15221                     error_at (OMP_CLAUSE_LOCATION (c),
15222                               "%qD appears both in data and map clauses", t);
15223                   remove = true;
15224                 }
15225               else
15226                 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15227             }
15228           else if (bitmap_bit_p (&map_head, DECL_UID (t))
15229                    && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15230             {
15231               if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15232                 error_at (OMP_CLAUSE_LOCATION (c),
15233                           "%qD appears more than once in motion clauses", t);
15234               else if (ort == C_ORT_ACC)
15235                 error_at (OMP_CLAUSE_LOCATION (c),
15236                           "%qD appears more than once in data clauses", t);
15237               else
15238                 error_at (OMP_CLAUSE_LOCATION (c),
15239                           "%qD appears more than once in map clauses", t);
15240               remove = true;
15241             }
15242           else if (ort == C_ORT_ACC
15243                    && bitmap_bit_p (&generic_head, DECL_UID (t)))
15244             {
15245               error_at (OMP_CLAUSE_LOCATION (c),
15246                         "%qD appears more than once in data clauses", t);
15247               remove = true;
15248             }
15249           else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15250                    || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15251             {
15252               if (ort == C_ORT_ACC)
15253                 error_at (OMP_CLAUSE_LOCATION (c),
15254                           "%qD appears more than once in data clauses", t);
15255               else
15256                 error_at (OMP_CLAUSE_LOCATION (c),
15257                           "%qD appears both in data and map clauses", t);
15258               remove = true;
15259             }
15260           else
15261             {
15262               bitmap_set_bit (&map_head, DECL_UID (t));
15263               if (t != OMP_CLAUSE_DECL (c)
15264                   && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15265                 bitmap_set_bit (&map_field_head, DECL_UID (t));
15266             }
15267           break;
15268
15269         case OMP_CLAUSE_ENTER:
15270         case OMP_CLAUSE_LINK:
15271           t = OMP_CLAUSE_DECL (c);
15272           const char *cname;
15273           cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
15274           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
15275               && OMP_CLAUSE_ENTER_TO (c))
15276             cname = "to";
15277           if (TREE_CODE (t) == FUNCTION_DECL
15278               && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15279             ;
15280           else if (!VAR_P (t))
15281             {
15282               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15283                 error_at (OMP_CLAUSE_LOCATION (c),
15284                           "%qE is neither a variable nor a function name in "
15285                           "clause %qs", t, cname);
15286               else
15287                 error_at (OMP_CLAUSE_LOCATION (c),
15288                           "%qE is not a variable in clause %qs", t, cname);
15289               remove = true;
15290             }
15291           else if (DECL_THREAD_LOCAL_P (t))
15292             {
15293               error_at (OMP_CLAUSE_LOCATION (c),
15294                         "%qD is threadprivate variable in %qs clause", t,
15295                         cname);
15296               remove = true;
15297             }
15298           else if (!omp_mappable_type (TREE_TYPE (t)))
15299             {
15300               error_at (OMP_CLAUSE_LOCATION (c),
15301                         "%qD does not have a mappable type in %qs clause", t,
15302                         cname);
15303               remove = true;
15304             }
15305           if (remove)
15306             break;
15307           if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15308             {
15309               error_at (OMP_CLAUSE_LOCATION (c),
15310                         "%qE appears more than once on the same "
15311                         "%<declare target%> directive", t);
15312               remove = true;
15313             }
15314           else
15315             bitmap_set_bit (&generic_head, DECL_UID (t));
15316           break;
15317
15318         case OMP_CLAUSE_UNIFORM:
15319           t = OMP_CLAUSE_DECL (c);
15320           if (TREE_CODE (t) != PARM_DECL)
15321             {
15322               if (DECL_P (t))
15323                 error_at (OMP_CLAUSE_LOCATION (c),
15324                           "%qD is not an argument in %<uniform%> clause", t);
15325               else
15326                 error_at (OMP_CLAUSE_LOCATION (c),
15327                           "%qE is not an argument in %<uniform%> clause", t);
15328               remove = true;
15329               break;
15330             }
15331           /* map_head bitmap is used as uniform_head if declare_simd.  */
15332           bitmap_set_bit (&map_head, DECL_UID (t));
15333           goto check_dup_generic;
15334
15335         case OMP_CLAUSE_IS_DEVICE_PTR:
15336         case OMP_CLAUSE_USE_DEVICE_PTR:
15337           t = OMP_CLAUSE_DECL (c);
15338           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15339             bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15340           if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15341             {
15342               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15343                   && ort != C_ORT_ACC)
15344                 {
15345                   error_at (OMP_CLAUSE_LOCATION (c),
15346                             "%qs variable is not a pointer",
15347                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15348                   remove = true;
15349                 }
15350               else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15351                 {
15352                   error_at (OMP_CLAUSE_LOCATION (c),
15353                             "%qs variable is neither a pointer nor an array",
15354                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15355                   remove = true;
15356                 }
15357             }
15358           goto check_dup_generic;
15359
15360         case OMP_CLAUSE_HAS_DEVICE_ADDR:
15361           t = OMP_CLAUSE_DECL (c);
15362           if (TREE_CODE (t) == TREE_LIST)
15363             {
15364               if (handle_omp_array_sections (c, ort))
15365                 remove = true;
15366               else
15367                 {
15368                   t = OMP_CLAUSE_DECL (c);
15369                   while (TREE_CODE (t) == ARRAY_REF)
15370                     t = TREE_OPERAND (t, 0);
15371                 }
15372             }
15373           bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15374           if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15375             c_mark_addressable (t);
15376           goto check_dup_generic_t;
15377
15378         case OMP_CLAUSE_USE_DEVICE_ADDR:
15379           t = OMP_CLAUSE_DECL (c);
15380           if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15381             c_mark_addressable (t);
15382           goto check_dup_generic;
15383
15384         case OMP_CLAUSE_NOWAIT:
15385           if (copyprivate_seen)
15386             {
15387               error_at (OMP_CLAUSE_LOCATION (c),
15388                         "%<nowait%> clause must not be used together "
15389                         "with %<copyprivate%>");
15390               remove = true;
15391               break;
15392             }
15393           nowait_clause = pc;
15394           pc = &OMP_CLAUSE_CHAIN (c);
15395           continue;
15396
15397         case OMP_CLAUSE_ORDER:
15398           if (ordered_clause)
15399             {
15400               error_at (OMP_CLAUSE_LOCATION (c),
15401                         "%<order%> clause must not be used together "
15402                         "with %<ordered%>");
15403               remove = true;
15404               break;
15405             }
15406           else if (order_clause)
15407             {
15408               /* Silently remove duplicates.  */
15409               remove = true;
15410               break;
15411             }
15412           order_clause = pc;
15413           pc = &OMP_CLAUSE_CHAIN (c);
15414           continue;
15415
15416         case OMP_CLAUSE_DETACH:
15417           t = OMP_CLAUSE_DECL (c);
15418           if (detach_seen)
15419             {
15420               error_at (OMP_CLAUSE_LOCATION (c),
15421                         "too many %qs clauses on a task construct",
15422                         "detach");
15423               remove = true;
15424               break;
15425             }
15426           detach_seen = pc;
15427           pc = &OMP_CLAUSE_CHAIN (c);
15428           c_mark_addressable (t);
15429           continue;
15430
15431         case OMP_CLAUSE_IF:
15432         case OMP_CLAUSE_NUM_THREADS:
15433         case OMP_CLAUSE_NUM_TEAMS:
15434         case OMP_CLAUSE_THREAD_LIMIT:
15435         case OMP_CLAUSE_DEFAULT:
15436         case OMP_CLAUSE_UNTIED:
15437         case OMP_CLAUSE_COLLAPSE:
15438         case OMP_CLAUSE_FINAL:
15439         case OMP_CLAUSE_DEVICE:
15440         case OMP_CLAUSE_DIST_SCHEDULE:
15441         case OMP_CLAUSE_PARALLEL:
15442         case OMP_CLAUSE_FOR:
15443         case OMP_CLAUSE_SECTIONS:
15444         case OMP_CLAUSE_TASKGROUP:
15445         case OMP_CLAUSE_PROC_BIND:
15446         case OMP_CLAUSE_DEVICE_TYPE:
15447         case OMP_CLAUSE_PRIORITY:
15448         case OMP_CLAUSE_GRAINSIZE:
15449         case OMP_CLAUSE_NUM_TASKS:
15450         case OMP_CLAUSE_THREADS:
15451         case OMP_CLAUSE_SIMD:
15452         case OMP_CLAUSE_HINT:
15453         case OMP_CLAUSE_FILTER:
15454         case OMP_CLAUSE_DEFAULTMAP:
15455         case OMP_CLAUSE_BIND:
15456         case OMP_CLAUSE_NUM_GANGS:
15457         case OMP_CLAUSE_NUM_WORKERS:
15458         case OMP_CLAUSE_VECTOR_LENGTH:
15459         case OMP_CLAUSE_ASYNC:
15460         case OMP_CLAUSE_WAIT:
15461         case OMP_CLAUSE_AUTO:
15462         case OMP_CLAUSE_INDEPENDENT:
15463         case OMP_CLAUSE_SEQ:
15464         case OMP_CLAUSE_GANG:
15465         case OMP_CLAUSE_WORKER:
15466         case OMP_CLAUSE_VECTOR:
15467         case OMP_CLAUSE_TILE:
15468         case OMP_CLAUSE_IF_PRESENT:
15469         case OMP_CLAUSE_FINALIZE:
15470         case OMP_CLAUSE_NOHOST:
15471           pc = &OMP_CLAUSE_CHAIN (c);
15472           continue;
15473
15474         case OMP_CLAUSE_MERGEABLE:
15475           mergeable_seen = true;
15476           pc = &OMP_CLAUSE_CHAIN (c);
15477           continue;
15478
15479         case OMP_CLAUSE_NOGROUP:
15480           nogroup_seen = pc;
15481           pc = &OMP_CLAUSE_CHAIN (c);
15482           continue;
15483
15484         case OMP_CLAUSE_SCHEDULE:
15485           schedule_clause = c;
15486           pc = &OMP_CLAUSE_CHAIN (c);
15487           continue;
15488
15489         case OMP_CLAUSE_ORDERED:
15490           ordered_clause = c;
15491           if (order_clause)
15492             {
15493               error_at (OMP_CLAUSE_LOCATION (*order_clause),
15494                         "%<order%> clause must not be used together "
15495                         "with %<ordered%>");
15496               *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15497               order_clause = NULL;
15498             }
15499           pc = &OMP_CLAUSE_CHAIN (c);
15500           continue;
15501
15502         case OMP_CLAUSE_SAFELEN:
15503           safelen = c;
15504           pc = &OMP_CLAUSE_CHAIN (c);
15505           continue;
15506         case OMP_CLAUSE_SIMDLEN:
15507           simdlen = c;
15508           pc = &OMP_CLAUSE_CHAIN (c);
15509           continue;
15510
15511         case OMP_CLAUSE_INBRANCH:
15512         case OMP_CLAUSE_NOTINBRANCH:
15513           if (branch_seen)
15514             {
15515               error_at (OMP_CLAUSE_LOCATION (c),
15516                         "%<inbranch%> clause is incompatible with "
15517                         "%<notinbranch%>");
15518               remove = true;
15519               break;
15520             }
15521           branch_seen = true;
15522           pc = &OMP_CLAUSE_CHAIN (c);
15523           continue;
15524
15525         case OMP_CLAUSE_INCLUSIVE:
15526         case OMP_CLAUSE_EXCLUSIVE:
15527           need_complete = true;
15528           need_implicitly_determined = true;
15529           t = OMP_CLAUSE_DECL (c);
15530           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15531             {
15532               error_at (OMP_CLAUSE_LOCATION (c),
15533                         "%qE is not a variable in clause %qs", t,
15534                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15535               remove = true;
15536             }
15537           break;
15538
15539         default:
15540           gcc_unreachable ();
15541         }
15542
15543       if (!remove)
15544         {
15545           t = OMP_CLAUSE_DECL (c);
15546
15547           if (need_complete)
15548             {
15549               t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15550               if (t == error_mark_node)
15551                 remove = true;
15552             }
15553
15554           if (need_implicitly_determined)
15555             {
15556               const char *share_name = NULL;
15557
15558               if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15559                 share_name = "threadprivate";
15560               else switch (c_omp_predetermined_sharing (t))
15561                 {
15562                 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15563                   break;
15564                 case OMP_CLAUSE_DEFAULT_SHARED:
15565                   if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15566                        || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15567                       && c_omp_predefined_variable (t))
15568                     /* The __func__ variable and similar function-local
15569                        predefined variables may be listed in a shared or
15570                        firstprivate clause.  */
15571                     break;
15572                   share_name = "shared";
15573                   break;
15574                 case OMP_CLAUSE_DEFAULT_PRIVATE:
15575                   share_name = "private";
15576                   break;
15577                 default:
15578                   gcc_unreachable ();
15579                 }
15580               if (share_name)
15581                 {
15582                   error_at (OMP_CLAUSE_LOCATION (c),
15583                             "%qE is predetermined %qs for %qs",
15584                             t, share_name,
15585                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15586                   remove = true;
15587                 }
15588               else if (TREE_READONLY (t)
15589                        && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15590                        && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15591                 {
15592                   error_at (OMP_CLAUSE_LOCATION (c),
15593                             "%<const%> qualified %qE may appear only in "
15594                             "%<shared%> or %<firstprivate%> clauses", t);
15595                   remove = true;
15596                 }
15597             }
15598         }
15599
15600       if (remove)
15601         *pc = OMP_CLAUSE_CHAIN (c);
15602       else
15603         pc = &OMP_CLAUSE_CHAIN (c);
15604     }
15605
15606   if (simdlen
15607       && safelen
15608       && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15609                           OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15610     {
15611       error_at (OMP_CLAUSE_LOCATION (simdlen),
15612                 "%<simdlen%> clause value is bigger than "
15613                 "%<safelen%> clause value");
15614       OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15615         = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15616     }
15617
15618   if (ordered_clause
15619       && schedule_clause
15620       && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15621           & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15622     {
15623       error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15624                 "%<nonmonotonic%> schedule modifier specified together "
15625                 "with %<ordered%> clause");
15626       OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15627         = (enum omp_clause_schedule_kind)
15628           (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15629            & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15630     }
15631
15632   if (reduction_seen < 0 && ordered_clause)
15633     {
15634       error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15635                 "%qs clause specified together with %<inscan%> "
15636                 "%<reduction%> clause", "ordered");
15637       reduction_seen = -2;
15638     }
15639
15640   if (reduction_seen < 0 && schedule_clause)
15641     {
15642       error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15643                 "%qs clause specified together with %<inscan%> "
15644                 "%<reduction%> clause", "schedule");
15645       reduction_seen = -2;
15646     }
15647
15648   if (linear_variable_step_check
15649       || reduction_seen == -2
15650       || allocate_seen
15651       || target_in_reduction_seen)
15652     for (pc = &clauses, c = clauses; c ; c = *pc)
15653       {
15654         bool remove = false;
15655         if (allocate_seen)
15656           switch (OMP_CLAUSE_CODE (c))
15657             {
15658             case OMP_CLAUSE_REDUCTION:
15659             case OMP_CLAUSE_IN_REDUCTION:
15660             case OMP_CLAUSE_TASK_REDUCTION:
15661               if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
15662                 {
15663                   t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
15664                   if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15665                     t = TREE_OPERAND (t, 0);
15666                   if (TREE_CODE (t) == ADDR_EXPR
15667                       || TREE_CODE (t) == INDIRECT_REF)
15668                     t = TREE_OPERAND (t, 0);
15669                   if (DECL_P (t))
15670                     bitmap_clear_bit (&aligned_head, DECL_UID (t));
15671                   break;
15672                 }
15673               /* FALLTHRU */
15674             case OMP_CLAUSE_PRIVATE:
15675             case OMP_CLAUSE_FIRSTPRIVATE:
15676             case OMP_CLAUSE_LASTPRIVATE:
15677             case OMP_CLAUSE_LINEAR:
15678               if (DECL_P (OMP_CLAUSE_DECL (c)))
15679                 bitmap_clear_bit (&aligned_head,
15680                                   DECL_UID (OMP_CLAUSE_DECL (c)));
15681               break;
15682             default:
15683               break;
15684             }
15685         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15686             && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15687             && !bitmap_bit_p (&map_head,
15688                               DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15689           {
15690             error_at (OMP_CLAUSE_LOCATION (c),
15691                       "%<linear%> clause step is a parameter %qD not "
15692                       "specified in %<uniform%> clause",
15693                       OMP_CLAUSE_LINEAR_STEP (c));
15694             remove = true;
15695           }
15696         else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15697                  && reduction_seen == -2)
15698           OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15699         if (target_in_reduction_seen
15700             && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
15701           {
15702             tree t = OMP_CLAUSE_DECL (c);
15703             while (handled_component_p (t)
15704                    || TREE_CODE (t) == INDIRECT_REF
15705                    || TREE_CODE (t) == ADDR_EXPR
15706                    || TREE_CODE (t) == MEM_REF
15707                    || TREE_CODE (t) == NON_LVALUE_EXPR)
15708               t = TREE_OPERAND (t, 0);
15709             if (DECL_P (t)
15710                 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15711               OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15712           }
15713
15714         if (remove)
15715           *pc = OMP_CLAUSE_CHAIN (c);
15716         else
15717           pc = &OMP_CLAUSE_CHAIN (c);
15718       }
15719
15720   if (allocate_seen)
15721     for (pc = &clauses, c = clauses; c ; c = *pc)
15722       {
15723         bool remove = false;
15724         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
15725             && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
15726             && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
15727           {
15728             error_at (OMP_CLAUSE_LOCATION (c),
15729                       "%qD specified in %<allocate%> clause but not in "
15730                       "an explicit privatization clause", OMP_CLAUSE_DECL (c));
15731             remove = true;
15732           }
15733         if (remove)
15734           *pc = OMP_CLAUSE_CHAIN (c);
15735         else
15736           pc = &OMP_CLAUSE_CHAIN (c);
15737       }
15738
15739   if (nogroup_seen && reduction_seen)
15740     {
15741       error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15742                 "%<nogroup%> clause must not be used together with "
15743                 "%<reduction%> clause");
15744       *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15745     }
15746
15747   if (detach_seen)
15748     {
15749       if (mergeable_seen)
15750         {
15751           error_at (OMP_CLAUSE_LOCATION (*detach_seen),
15752                     "%<detach%> clause must not be used together with "
15753                     "%<mergeable%> clause");
15754           *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
15755         }
15756       else
15757         {
15758           tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
15759
15760           for (pc = &clauses, c = clauses; c ; c = *pc)
15761             {
15762               bool remove = false;
15763               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15764                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15765                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
15766                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
15767                   && OMP_CLAUSE_DECL (c) == detach_decl)
15768                 {
15769                   error_at (OMP_CLAUSE_LOCATION (c),
15770                             "the event handle of a %<detach%> clause "
15771                             "should not be in a data-sharing clause");
15772                   remove = true;
15773                 }
15774               if (remove)
15775                 *pc = OMP_CLAUSE_CHAIN (c);
15776               else
15777                 pc = &OMP_CLAUSE_CHAIN (c);
15778             }
15779         }
15780     }
15781
15782   bitmap_obstack_release (NULL);
15783   return clauses;
15784 }
15785
15786 /* Return code to initialize DST with a copy constructor from SRC.
15787    C doesn't have copy constructors nor assignment operators, only for
15788    _Atomic vars we need to perform __atomic_load from src into a temporary
15789    followed by __atomic_store of the temporary to dst.  */
15790
15791 tree
15792 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15793 {
15794   if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15795     return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15796
15797   location_t loc = OMP_CLAUSE_LOCATION (clause);
15798   tree type = TREE_TYPE (dst);
15799   tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15800   tree tmp = create_tmp_var (nonatomic_type);
15801   tree tmp_addr = build_fold_addr_expr (tmp);
15802   TREE_ADDRESSABLE (tmp) = 1;
15803   suppress_warning (tmp);
15804   tree src_addr = build_fold_addr_expr (src);
15805   tree dst_addr = build_fold_addr_expr (dst);
15806   tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15807   vec<tree, va_gc> *params;
15808   /* Expansion of a generic atomic load may require an addition
15809      element, so allocate enough to prevent a resize.  */
15810   vec_alloc (params, 4);
15811
15812   /* Build __atomic_load (&src, &tmp, SEQ_CST);  */
15813   tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15814   params->quick_push (src_addr);
15815   params->quick_push (tmp_addr);
15816   params->quick_push (seq_cst);
15817   tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15818
15819   vec_alloc (params, 4);
15820
15821   /* Build __atomic_store (&dst, &tmp, SEQ_CST);  */
15822   fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15823   params->quick_push (dst_addr);
15824   params->quick_push (tmp_addr);
15825   params->quick_push (seq_cst);
15826   tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15827   return build2 (COMPOUND_EXPR, void_type_node, load, store);
15828 }
15829
15830 /* Create a transaction node.  */
15831
15832 tree
15833 c_finish_transaction (location_t loc, tree block, int flags)
15834 {
15835   tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15836   if (flags & TM_STMT_ATTR_OUTER)
15837     TRANSACTION_EXPR_OUTER (stmt) = 1;
15838   if (flags & TM_STMT_ATTR_RELAXED)
15839     TRANSACTION_EXPR_RELAXED (stmt) = 1;
15840   return add_stmt (stmt);
15841 }
15842
15843 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15844    down to the element type of an array.  If ORIG_QUAL_TYPE is not
15845    NULL, then it should be used as the qualified type
15846    ORIG_QUAL_INDIRECT levels down in array type derivation (to
15847    preserve information about the typedef name from which an array
15848    type was derived).  */
15849
15850 tree
15851 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15852                         size_t orig_qual_indirect)
15853 {
15854   if (type == error_mark_node)
15855     return type;
15856
15857   if (TREE_CODE (type) == ARRAY_TYPE)
15858     {
15859       tree t;
15860       tree element_type = c_build_qualified_type (TREE_TYPE (type),
15861                                                   type_quals, orig_qual_type,
15862                                                   orig_qual_indirect - 1);
15863
15864       /* See if we already have an identically qualified type.  */
15865       if (orig_qual_type && orig_qual_indirect == 0)
15866         t = orig_qual_type;
15867       else
15868         for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15869           {
15870             if (TYPE_QUALS (strip_array_types (t)) == type_quals
15871                 && TYPE_NAME (t) == TYPE_NAME (type)
15872                 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15873                 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15874                                          TYPE_ATTRIBUTES (type)))
15875               break;
15876           }
15877       if (!t)
15878         {
15879           tree domain = TYPE_DOMAIN (type);
15880
15881           t = build_variant_type_copy (type);
15882           TREE_TYPE (t) = element_type;
15883
15884           if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15885               || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15886             SET_TYPE_STRUCTURAL_EQUALITY (t);
15887           else if (TYPE_CANONICAL (element_type) != element_type
15888                    || (domain && TYPE_CANONICAL (domain) != domain))
15889             {
15890               tree unqualified_canon
15891                 = build_array_type (TYPE_CANONICAL (element_type),
15892                                     domain? TYPE_CANONICAL (domain)
15893                                           : NULL_TREE);
15894               if (TYPE_REVERSE_STORAGE_ORDER (type))
15895                 {
15896                   unqualified_canon
15897                     = build_distinct_type_copy (unqualified_canon);
15898                   TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15899                 }
15900               TYPE_CANONICAL (t)
15901                 = c_build_qualified_type (unqualified_canon, type_quals);
15902             }
15903           else
15904             TYPE_CANONICAL (t) = t;
15905         }
15906       return t;
15907     }
15908
15909   /* A restrict-qualified pointer type must be a pointer to object or
15910      incomplete type.  Note that the use of POINTER_TYPE_P also allows
15911      REFERENCE_TYPEs, which is appropriate for C++.  */
15912   if ((type_quals & TYPE_QUAL_RESTRICT)
15913       && (!POINTER_TYPE_P (type)
15914           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15915     {
15916       error ("invalid use of %<restrict%>");
15917       type_quals &= ~TYPE_QUAL_RESTRICT;
15918     }
15919
15920   tree var_type = (orig_qual_type && orig_qual_indirect == 0
15921                    ? orig_qual_type
15922                    : build_qualified_type (type, type_quals));
15923   /* A variant type does not inherit the list of incomplete vars from the
15924      type main variant.  */
15925   if ((RECORD_OR_UNION_TYPE_P (var_type)
15926        || TREE_CODE (var_type) == ENUMERAL_TYPE)
15927       && TYPE_MAIN_VARIANT (var_type) != var_type)
15928     C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15929   return var_type;
15930 }
15931
15932 /* Build a VA_ARG_EXPR for the C parser.  */
15933
15934 tree
15935 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15936 {
15937   if (error_operand_p (type))
15938     return error_mark_node;
15939   /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15940      order because it takes the address of the expression.  */
15941   else if (handled_component_p (expr)
15942            && reverse_storage_order_for_component_p (expr))
15943     {
15944       error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15945       return error_mark_node;
15946     }
15947   else if (!COMPLETE_TYPE_P (type))
15948     {
15949       error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15950                 "type %qT", type);
15951       return error_mark_node;
15952     }
15953   else if (TREE_CODE (type) == FUNCTION_TYPE)
15954     {
15955       error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
15956                 type);
15957       return error_mark_node;
15958     }
15959   else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15960     warning_at (loc2, OPT_Wc___compat,
15961                 "C++ requires promoted type, not enum type, in %<va_arg%>");
15962   return build_va_arg (loc2, expr, type);
15963 }
15964
15965 /* Return truthvalue of whether T1 is the same tree structure as T2.
15966    Return 1 if they are the same. Return false if they are different.  */
15967
15968 bool
15969 c_tree_equal (tree t1, tree t2)
15970 {
15971   enum tree_code code1, code2;
15972
15973   if (t1 == t2)
15974     return true;
15975   if (!t1 || !t2)
15976     return false;
15977
15978   for (code1 = TREE_CODE (t1);
15979        CONVERT_EXPR_CODE_P (code1)
15980          || code1 == NON_LVALUE_EXPR;
15981        code1 = TREE_CODE (t1))
15982     t1 = TREE_OPERAND (t1, 0);
15983   for (code2 = TREE_CODE (t2);
15984        CONVERT_EXPR_CODE_P (code2)
15985          || code2 == NON_LVALUE_EXPR;
15986        code2 = TREE_CODE (t2))
15987     t2 = TREE_OPERAND (t2, 0);
15988
15989   /* They might have become equal now.  */
15990   if (t1 == t2)
15991     return true;
15992
15993   if (code1 != code2)
15994     return false;
15995
15996   switch (code1)
15997     {
15998     case INTEGER_CST:
15999       return wi::to_wide (t1) == wi::to_wide (t2);
16000
16001     case REAL_CST:
16002       return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
16003
16004     case STRING_CST:
16005       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
16006         && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
16007                     TREE_STRING_LENGTH (t1));
16008
16009     case FIXED_CST:
16010       return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
16011                                      TREE_FIXED_CST (t2));
16012
16013     case COMPLEX_CST:
16014       return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
16015              && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
16016
16017     case VECTOR_CST:
16018       return operand_equal_p (t1, t2, OEP_ONLY_CONST);
16019
16020     case CONSTRUCTOR:
16021       /* We need to do this when determining whether or not two
16022          non-type pointer to member function template arguments
16023          are the same.  */
16024       if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
16025           || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
16026         return false;
16027       {
16028         tree field, value;
16029         unsigned int i;
16030         FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
16031           {
16032             constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
16033             if (!c_tree_equal (field, elt2->index)
16034                 || !c_tree_equal (value, elt2->value))
16035               return false;
16036           }
16037       }
16038       return true;
16039
16040     case TREE_LIST:
16041       if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
16042         return false;
16043       if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
16044         return false;
16045       return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
16046
16047     case SAVE_EXPR:
16048       return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16049
16050     case CALL_EXPR:
16051       {
16052         tree arg1, arg2;
16053         call_expr_arg_iterator iter1, iter2;
16054         if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16055           return false;
16056         for (arg1 = first_call_expr_arg (t1, &iter1),
16057                arg2 = first_call_expr_arg (t2, &iter2);
16058              arg1 && arg2;
16059              arg1 = next_call_expr_arg (&iter1),
16060                arg2 = next_call_expr_arg (&iter2))
16061           if (!c_tree_equal (arg1, arg2))
16062             return false;
16063         if (arg1 || arg2)
16064           return false;
16065         return true;
16066       }
16067
16068     case TARGET_EXPR:
16069       {
16070         tree o1 = TREE_OPERAND (t1, 0);
16071         tree o2 = TREE_OPERAND (t2, 0);
16072
16073         /* Special case: if either target is an unallocated VAR_DECL,
16074            it means that it's going to be unified with whatever the
16075            TARGET_EXPR is really supposed to initialize, so treat it
16076            as being equivalent to anything.  */
16077         if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16078             && !DECL_RTL_SET_P (o1))
16079           /*Nop*/;
16080         else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16081                  && !DECL_RTL_SET_P (o2))
16082           /*Nop*/;
16083         else if (!c_tree_equal (o1, o2))
16084           return false;
16085
16086         return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16087       }
16088
16089     case COMPONENT_REF:
16090       if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16091         return false;
16092       return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16093
16094     case PARM_DECL:
16095     case VAR_DECL:
16096     case CONST_DECL:
16097     case FIELD_DECL:
16098     case FUNCTION_DECL:
16099     case IDENTIFIER_NODE:
16100     case SSA_NAME:
16101       return false;
16102
16103     case TREE_VEC:
16104       {
16105         unsigned ix;
16106         if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16107           return false;
16108         for (ix = TREE_VEC_LENGTH (t1); ix--;)
16109           if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16110                              TREE_VEC_ELT (t2, ix)))
16111             return false;
16112         return true;
16113       }
16114
16115     default:
16116       break;
16117     }
16118
16119   switch (TREE_CODE_CLASS (code1))
16120     {
16121     case tcc_unary:
16122     case tcc_binary:
16123     case tcc_comparison:
16124     case tcc_expression:
16125     case tcc_vl_exp:
16126     case tcc_reference:
16127     case tcc_statement:
16128       {
16129         int i, n = TREE_OPERAND_LENGTH (t1);
16130
16131         switch (code1)
16132           {
16133           case PREINCREMENT_EXPR:
16134           case PREDECREMENT_EXPR:
16135           case POSTINCREMENT_EXPR:
16136           case POSTDECREMENT_EXPR:
16137             n = 1;
16138             break;
16139           case ARRAY_REF:
16140             n = 2;
16141             break;
16142           default:
16143             break;
16144           }
16145
16146         if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16147             && n != TREE_OPERAND_LENGTH (t2))
16148           return false;
16149
16150         for (i = 0; i < n; ++i)
16151           if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16152             return false;
16153
16154         return true;
16155       }
16156
16157     case tcc_type:
16158       return comptypes (t1, t2);
16159     default:
16160       gcc_unreachable ();
16161     }
16162 }
16163
16164 /* Returns true when the function declaration FNDECL is implicit,
16165    introduced as a result of a call to an otherwise undeclared
16166    function, and false otherwise.  */
16167
16168 bool
16169 c_decl_implicit (const_tree fndecl)
16170 {
16171   return C_DECL_IMPLICIT (fndecl);
16172 }