de8780a1502d3a7083af50a0dbdcfa7d51c997a9
[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, "variable-sized object may not be initialized");
8295       return error_mark_node;
8296     }
8297
8298   error_init (init_loc, "invalid initializer");
8299   return error_mark_node;
8300 }
8301 \f
8302 /* Handle initializers that use braces.  */
8303
8304 /* Type of object we are accumulating a constructor for.
8305    This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE.  */
8306 static tree constructor_type;
8307
8308 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8309    left to fill.  */
8310 static tree constructor_fields;
8311
8312 /* For an ARRAY_TYPE, this is the specified index
8313    at which to store the next element we get.  */
8314 static tree constructor_index;
8315
8316 /* For an ARRAY_TYPE, this is the maximum index.  */
8317 static tree constructor_max_index;
8318
8319 /* For a RECORD_TYPE, this is the first field not yet written out.  */
8320 static tree constructor_unfilled_fields;
8321
8322 /* For an ARRAY_TYPE, this is the index of the first element
8323    not yet written out.  */
8324 static tree constructor_unfilled_index;
8325
8326 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8327    This is so we can generate gaps between fields, when appropriate.  */
8328 static tree constructor_bit_index;
8329
8330 /* If we are saving up the elements rather than allocating them,
8331    this is the list of elements so far (in reverse order,
8332    most recent first).  */
8333 static vec<constructor_elt, va_gc> *constructor_elements;
8334
8335 /* 1 if constructor should be incrementally stored into a constructor chain,
8336    0 if all the elements should be kept in AVL tree.  */
8337 static int constructor_incremental;
8338
8339 /* 1 if so far this constructor's elements are all compile-time constants.  */
8340 static int constructor_constant;
8341
8342 /* 1 if so far this constructor's elements are all valid address constants.  */
8343 static int constructor_simple;
8344
8345 /* 1 if this constructor has an element that cannot be part of a
8346    constant expression.  */
8347 static int constructor_nonconst;
8348
8349 /* 1 if this constructor is erroneous so far.  */
8350 static int constructor_erroneous;
8351
8352 /* 1 if this constructor is the universal zero initializer { 0 }.  */
8353 static int constructor_zeroinit;
8354
8355 /* Structure for managing pending initializer elements, organized as an
8356    AVL tree.  */
8357
8358 struct init_node
8359 {
8360   struct init_node *left, *right;
8361   struct init_node *parent;
8362   int balance;
8363   tree purpose;
8364   tree value;
8365   tree origtype;
8366 };
8367
8368 /* Tree of pending elements at this constructor level.
8369    These are elements encountered out of order
8370    which belong at places we haven't reached yet in actually
8371    writing the output.
8372    Will never hold tree nodes across GC runs.  */
8373 static struct init_node *constructor_pending_elts;
8374
8375 /* The SPELLING_DEPTH of this constructor.  */
8376 static int constructor_depth;
8377
8378 /* DECL node for which an initializer is being read.
8379    0 means we are reading a constructor expression
8380    such as (struct foo) {...}.  */
8381 static tree constructor_decl;
8382
8383 /* Nonzero if this is an initializer for a top-level decl.  */
8384 static int constructor_top_level;
8385
8386 /* Nonzero if there were any member designators in this initializer.  */
8387 static int constructor_designated;
8388
8389 /* Nesting depth of designator list.  */
8390 static int designator_depth;
8391
8392 /* Nonzero if there were diagnosed errors in this designator list.  */
8393 static int designator_erroneous;
8394
8395 \f
8396 /* This stack has a level for each implicit or explicit level of
8397    structuring in the initializer, including the outermost one.  It
8398    saves the values of most of the variables above.  */
8399
8400 struct constructor_range_stack;
8401
8402 struct constructor_stack
8403 {
8404   struct constructor_stack *next;
8405   tree type;
8406   tree fields;
8407   tree index;
8408   tree max_index;
8409   tree unfilled_index;
8410   tree unfilled_fields;
8411   tree bit_index;
8412   vec<constructor_elt, va_gc> *elements;
8413   struct init_node *pending_elts;
8414   int offset;
8415   int depth;
8416   /* If value nonzero, this value should replace the entire
8417      constructor at this level.  */
8418   struct c_expr replacement_value;
8419   struct constructor_range_stack *range_stack;
8420   char constant;
8421   char simple;
8422   char nonconst;
8423   char implicit;
8424   char erroneous;
8425   char outer;
8426   char incremental;
8427   char designated;
8428   int designator_depth;
8429 };
8430
8431 static struct constructor_stack *constructor_stack;
8432
8433 /* This stack represents designators from some range designator up to
8434    the last designator in the list.  */
8435
8436 struct constructor_range_stack
8437 {
8438   struct constructor_range_stack *next, *prev;
8439   struct constructor_stack *stack;
8440   tree range_start;
8441   tree index;
8442   tree range_end;
8443   tree fields;
8444 };
8445
8446 static struct constructor_range_stack *constructor_range_stack;
8447
8448 /* This stack records separate initializers that are nested.
8449    Nested initializers can't happen in ANSI C, but GNU C allows them
8450    in cases like { ... (struct foo) { ... } ... }.  */
8451
8452 struct initializer_stack
8453 {
8454   struct initializer_stack *next;
8455   tree decl;
8456   struct constructor_stack *constructor_stack;
8457   struct constructor_range_stack *constructor_range_stack;
8458   vec<constructor_elt, va_gc> *elements;
8459   struct spelling *spelling;
8460   struct spelling *spelling_base;
8461   int spelling_size;
8462   char top_level;
8463   char require_constant_value;
8464   char require_constant_elements;
8465   char designated;
8466   rich_location *missing_brace_richloc;
8467 };
8468
8469 static struct initializer_stack *initializer_stack;
8470 \f
8471 /* Prepare to parse and output the initializer for variable DECL.  */
8472
8473 void
8474 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8475             rich_location *richloc)
8476 {
8477   const char *locus;
8478   struct initializer_stack *p = XNEW (struct initializer_stack);
8479
8480   p->decl = constructor_decl;
8481   p->require_constant_value = require_constant_value;
8482   p->require_constant_elements = require_constant_elements;
8483   p->constructor_stack = constructor_stack;
8484   p->constructor_range_stack = constructor_range_stack;
8485   p->elements = constructor_elements;
8486   p->spelling = spelling;
8487   p->spelling_base = spelling_base;
8488   p->spelling_size = spelling_size;
8489   p->top_level = constructor_top_level;
8490   p->next = initializer_stack;
8491   p->missing_brace_richloc = richloc;
8492   p->designated = constructor_designated;
8493   initializer_stack = p;
8494
8495   constructor_decl = decl;
8496   constructor_designated = 0;
8497   constructor_top_level = top_level;
8498
8499   if (decl != NULL_TREE && decl != error_mark_node)
8500     {
8501       require_constant_value = TREE_STATIC (decl);
8502       require_constant_elements
8503         = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8504            /* For a scalar, you can always use any value to initialize,
8505               even within braces.  */
8506            && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8507       locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8508     }
8509   else
8510     {
8511       require_constant_value = 0;
8512       require_constant_elements = 0;
8513       locus = _("(anonymous)");
8514     }
8515
8516   constructor_stack = 0;
8517   constructor_range_stack = 0;
8518
8519   found_missing_braces = 0;
8520
8521   spelling_base = 0;
8522   spelling_size = 0;
8523   RESTORE_SPELLING_DEPTH (0);
8524
8525   if (locus)
8526     push_string (locus);
8527 }
8528
8529 void
8530 finish_init (void)
8531 {
8532   struct initializer_stack *p = initializer_stack;
8533
8534   /* Free the whole constructor stack of this initializer.  */
8535   while (constructor_stack)
8536     {
8537       struct constructor_stack *q = constructor_stack;
8538       constructor_stack = q->next;
8539       XDELETE (q);
8540     }
8541
8542   gcc_assert (!constructor_range_stack);
8543
8544   /* Pop back to the data of the outer initializer (if any).  */
8545   XDELETE (spelling_base);
8546
8547   constructor_decl = p->decl;
8548   require_constant_value = p->require_constant_value;
8549   require_constant_elements = p->require_constant_elements;
8550   constructor_stack = p->constructor_stack;
8551   constructor_designated = p->designated;
8552   constructor_range_stack = p->constructor_range_stack;
8553   constructor_elements = p->elements;
8554   spelling = p->spelling;
8555   spelling_base = p->spelling_base;
8556   spelling_size = p->spelling_size;
8557   constructor_top_level = p->top_level;
8558   initializer_stack = p->next;
8559   XDELETE (p);
8560 }
8561 \f
8562 /* Call here when we see the initializer is surrounded by braces.
8563    This is instead of a call to push_init_level;
8564    it is matched by a call to pop_init_level.
8565
8566    TYPE is the type to initialize, for a constructor expression.
8567    For an initializer for a decl, TYPE is zero.  */
8568
8569 void
8570 really_start_incremental_init (tree type)
8571 {
8572   struct constructor_stack *p = XNEW (struct constructor_stack);
8573
8574   if (type == NULL_TREE)
8575     type = TREE_TYPE (constructor_decl);
8576
8577   if (VECTOR_TYPE_P (type)
8578       && TYPE_VECTOR_OPAQUE (type))
8579     error ("opaque vector types cannot be initialized");
8580
8581   p->type = constructor_type;
8582   p->fields = constructor_fields;
8583   p->index = constructor_index;
8584   p->max_index = constructor_max_index;
8585   p->unfilled_index = constructor_unfilled_index;
8586   p->unfilled_fields = constructor_unfilled_fields;
8587   p->bit_index = constructor_bit_index;
8588   p->elements = constructor_elements;
8589   p->constant = constructor_constant;
8590   p->simple = constructor_simple;
8591   p->nonconst = constructor_nonconst;
8592   p->erroneous = constructor_erroneous;
8593   p->pending_elts = constructor_pending_elts;
8594   p->depth = constructor_depth;
8595   p->replacement_value.value = 0;
8596   p->replacement_value.original_code = ERROR_MARK;
8597   p->replacement_value.original_type = NULL;
8598   p->implicit = 0;
8599   p->range_stack = 0;
8600   p->outer = 0;
8601   p->incremental = constructor_incremental;
8602   p->designated = constructor_designated;
8603   p->designator_depth = designator_depth;
8604   p->next = 0;
8605   constructor_stack = p;
8606
8607   constructor_constant = 1;
8608   constructor_simple = 1;
8609   constructor_nonconst = 0;
8610   constructor_depth = SPELLING_DEPTH ();
8611   constructor_elements = NULL;
8612   constructor_pending_elts = 0;
8613   constructor_type = type;
8614   constructor_incremental = 1;
8615   constructor_designated = 0;
8616   constructor_zeroinit = 1;
8617   designator_depth = 0;
8618   designator_erroneous = 0;
8619
8620   if (RECORD_OR_UNION_TYPE_P (constructor_type))
8621     {
8622       constructor_fields = TYPE_FIELDS (constructor_type);
8623       /* Skip any nameless bit fields at the beginning.  */
8624       while (constructor_fields != NULL_TREE
8625              && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8626         constructor_fields = DECL_CHAIN (constructor_fields);
8627
8628       constructor_unfilled_fields = constructor_fields;
8629       constructor_bit_index = bitsize_zero_node;
8630     }
8631   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8632     {
8633       if (TYPE_DOMAIN (constructor_type))
8634         {
8635           constructor_max_index
8636             = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8637
8638           /* Detect non-empty initializations of zero-length arrays.  */
8639           if (constructor_max_index == NULL_TREE
8640               && TYPE_SIZE (constructor_type))
8641             constructor_max_index = integer_minus_one_node;
8642
8643           /* constructor_max_index needs to be an INTEGER_CST.  Attempts
8644              to initialize VLAs will cause a proper error; avoid tree
8645              checking errors as well by setting a safe value.  */
8646           if (constructor_max_index
8647               && TREE_CODE (constructor_max_index) != INTEGER_CST)
8648             constructor_max_index = integer_minus_one_node;
8649
8650           constructor_index
8651             = convert (bitsizetype,
8652                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8653         }
8654       else
8655         {
8656           constructor_index = bitsize_zero_node;
8657           constructor_max_index = NULL_TREE;
8658         }
8659
8660       constructor_unfilled_index = constructor_index;
8661     }
8662   else if (gnu_vector_type_p (constructor_type))
8663     {
8664       /* Vectors are like simple fixed-size arrays.  */
8665       constructor_max_index =
8666         bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8667       constructor_index = bitsize_zero_node;
8668       constructor_unfilled_index = constructor_index;
8669     }
8670   else
8671     {
8672       /* Handle the case of int x = {5}; */
8673       constructor_fields = constructor_type;
8674       constructor_unfilled_fields = constructor_type;
8675     }
8676 }
8677 \f
8678 extern location_t last_init_list_comma;
8679
8680 /* Called when we see an open brace for a nested initializer.  Finish
8681    off any pending levels with implicit braces.  */
8682 void
8683 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8684 {
8685   while (constructor_stack->implicit)
8686     {
8687       if (RECORD_OR_UNION_TYPE_P (constructor_type)
8688           && constructor_fields == NULL_TREE)
8689         process_init_element (input_location,
8690                               pop_init_level (loc, 1, braced_init_obstack,
8691                                               last_init_list_comma),
8692                               true, braced_init_obstack);
8693       else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8694                && constructor_max_index
8695                && tree_int_cst_lt (constructor_max_index,
8696                                    constructor_index))
8697         process_init_element (input_location,
8698                               pop_init_level (loc, 1, braced_init_obstack,
8699                                               last_init_list_comma),
8700                               true, braced_init_obstack);
8701       else
8702         break;
8703     }
8704 }
8705
8706 /* Push down into a subobject, for initialization.
8707    If this is for an explicit set of braces, IMPLICIT is 0.
8708    If it is because the next element belongs at a lower level,
8709    IMPLICIT is 1 (or 2 if the push is because of designator list).  */
8710
8711 void
8712 push_init_level (location_t loc, int implicit,
8713                  struct obstack *braced_init_obstack)
8714 {
8715   struct constructor_stack *p;
8716   tree value = NULL_TREE;
8717
8718   /* Unless this is an explicit brace, we need to preserve previous
8719      content if any.  */
8720   if (implicit)
8721     {
8722       if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8723         value = find_init_member (constructor_fields, braced_init_obstack);
8724       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8725         value = find_init_member (constructor_index, braced_init_obstack);
8726     }
8727
8728   p = XNEW (struct constructor_stack);
8729   p->type = constructor_type;
8730   p->fields = constructor_fields;
8731   p->index = constructor_index;
8732   p->max_index = constructor_max_index;
8733   p->unfilled_index = constructor_unfilled_index;
8734   p->unfilled_fields = constructor_unfilled_fields;
8735   p->bit_index = constructor_bit_index;
8736   p->elements = constructor_elements;
8737   p->constant = constructor_constant;
8738   p->simple = constructor_simple;
8739   p->nonconst = constructor_nonconst;
8740   p->erroneous = constructor_erroneous;
8741   p->pending_elts = constructor_pending_elts;
8742   p->depth = constructor_depth;
8743   p->replacement_value.value = NULL_TREE;
8744   p->replacement_value.original_code = ERROR_MARK;
8745   p->replacement_value.original_type = NULL;
8746   p->implicit = implicit;
8747   p->outer = 0;
8748   p->incremental = constructor_incremental;
8749   p->designated = constructor_designated;
8750   p->designator_depth = designator_depth;
8751   p->next = constructor_stack;
8752   p->range_stack = 0;
8753   constructor_stack = p;
8754
8755   constructor_constant = 1;
8756   constructor_simple = 1;
8757   constructor_nonconst = 0;
8758   constructor_depth = SPELLING_DEPTH ();
8759   constructor_elements = NULL;
8760   constructor_incremental = 1;
8761   /* If the upper initializer is designated, then mark this as
8762      designated too to prevent bogus warnings.  */
8763   constructor_designated = p->designated;
8764   constructor_pending_elts = 0;
8765   if (!implicit)
8766     {
8767       p->range_stack = constructor_range_stack;
8768       constructor_range_stack = 0;
8769       designator_depth = 0;
8770       designator_erroneous = 0;
8771     }
8772
8773   /* Don't die if an entire brace-pair level is superfluous
8774      in the containing level.  */
8775   if (constructor_type == NULL_TREE)
8776     ;
8777   else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8778     {
8779       /* Don't die if there are extra init elts at the end.  */
8780       if (constructor_fields == NULL_TREE)
8781         constructor_type = NULL_TREE;
8782       else
8783         {
8784           constructor_type = TREE_TYPE (constructor_fields);
8785           push_member_name (constructor_fields);
8786           constructor_depth++;
8787         }
8788     }
8789   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8790     {
8791       constructor_type = TREE_TYPE (constructor_type);
8792       push_array_bounds (tree_to_uhwi (constructor_index));
8793       constructor_depth++;
8794     }
8795
8796   if (constructor_type == NULL_TREE)
8797     {
8798       error_init (loc, "extra brace group at end of initializer");
8799       constructor_fields = NULL_TREE;
8800       constructor_unfilled_fields = NULL_TREE;
8801       return;
8802     }
8803
8804   if (value && TREE_CODE (value) == CONSTRUCTOR)
8805     {
8806       constructor_constant = TREE_CONSTANT (value);
8807       constructor_simple = TREE_STATIC (value);
8808       constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8809       constructor_elements = CONSTRUCTOR_ELTS (value);
8810       if (!vec_safe_is_empty (constructor_elements)
8811           && (TREE_CODE (constructor_type) == RECORD_TYPE
8812               || TREE_CODE (constructor_type) == ARRAY_TYPE))
8813         set_nonincremental_init (braced_init_obstack);
8814     }
8815
8816   if (implicit == 1)
8817     {
8818       found_missing_braces = 1;
8819       if (initializer_stack->missing_brace_richloc)
8820         initializer_stack->missing_brace_richloc->add_fixit_insert_before
8821           (loc, "{");
8822     }
8823
8824   if (RECORD_OR_UNION_TYPE_P (constructor_type))
8825     {
8826       constructor_fields = TYPE_FIELDS (constructor_type);
8827       /* Skip any nameless bit fields at the beginning.  */
8828       while (constructor_fields != NULL_TREE
8829              && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8830         constructor_fields = DECL_CHAIN (constructor_fields);
8831
8832       constructor_unfilled_fields = constructor_fields;
8833       constructor_bit_index = bitsize_zero_node;
8834     }
8835   else if (gnu_vector_type_p (constructor_type))
8836     {
8837       /* Vectors are like simple fixed-size arrays.  */
8838       constructor_max_index =
8839         bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8840       constructor_index = bitsize_int (0);
8841       constructor_unfilled_index = constructor_index;
8842     }
8843   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8844     {
8845       if (TYPE_DOMAIN (constructor_type))
8846         {
8847           constructor_max_index
8848             = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8849
8850           /* Detect non-empty initializations of zero-length arrays.  */
8851           if (constructor_max_index == NULL_TREE
8852               && TYPE_SIZE (constructor_type))
8853             constructor_max_index = integer_minus_one_node;
8854
8855           /* constructor_max_index needs to be an INTEGER_CST.  Attempts
8856              to initialize VLAs will cause a proper error; avoid tree
8857              checking errors as well by setting a safe value.  */
8858           if (constructor_max_index
8859               && TREE_CODE (constructor_max_index) != INTEGER_CST)
8860             constructor_max_index = integer_minus_one_node;
8861
8862           constructor_index
8863             = convert (bitsizetype,
8864                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8865         }
8866       else
8867         constructor_index = bitsize_zero_node;
8868
8869       constructor_unfilled_index = constructor_index;
8870       if (value && TREE_CODE (value) == STRING_CST)
8871         {
8872           /* We need to split the char/wchar array into individual
8873              characters, so that we don't have to special case it
8874              everywhere.  */
8875           set_nonincremental_init_from_string (value, braced_init_obstack);
8876         }
8877     }
8878   else
8879     {
8880       if (constructor_type != error_mark_node)
8881         warning_init (input_location, 0, "braces around scalar initializer");
8882       constructor_fields = constructor_type;
8883       constructor_unfilled_fields = constructor_type;
8884     }
8885 }
8886
8887 /* At the end of an implicit or explicit brace level,
8888    finish up that level of constructor.  If a single expression
8889    with redundant braces initialized that level, return the
8890    c_expr structure for that expression.  Otherwise, the original_code
8891    element is set to ERROR_MARK.
8892    If we were outputting the elements as they are read, return 0 as the value
8893    from inner levels (process_init_element ignores that),
8894    but return error_mark_node as the value from the outermost level
8895    (that's what we want to put in DECL_INITIAL).
8896    Otherwise, return a CONSTRUCTOR expression as the value.  */
8897
8898 struct c_expr
8899 pop_init_level (location_t loc, int implicit,
8900                 struct obstack *braced_init_obstack,
8901                 location_t insert_before)
8902 {
8903   struct constructor_stack *p;
8904   struct c_expr ret;
8905   ret.value = NULL_TREE;
8906   ret.original_code = ERROR_MARK;
8907   ret.original_type = NULL;
8908
8909   if (implicit == 0)
8910     {
8911       /* When we come to an explicit close brace,
8912          pop any inner levels that didn't have explicit braces.  */
8913       while (constructor_stack->implicit)
8914         process_init_element (input_location,
8915                               pop_init_level (loc, 1, braced_init_obstack,
8916                                               insert_before),
8917                               true, braced_init_obstack);
8918       gcc_assert (!constructor_range_stack);
8919     }
8920   else
8921     if (initializer_stack->missing_brace_richloc)
8922       initializer_stack->missing_brace_richloc->add_fixit_insert_before
8923         (insert_before, "}");
8924
8925   /* Now output all pending elements.  */
8926   constructor_incremental = 1;
8927   output_pending_init_elements (1, braced_init_obstack);
8928
8929   p = constructor_stack;
8930
8931   /* Error for initializing a flexible array member, or a zero-length
8932      array member in an inappropriate context.  */
8933   if (constructor_type && constructor_fields
8934       && TREE_CODE (constructor_type) == ARRAY_TYPE
8935       && TYPE_DOMAIN (constructor_type)
8936       && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8937     {
8938       /* Silently discard empty initializations.  The parser will
8939          already have pedwarned for empty brackets.  */
8940       if (integer_zerop (constructor_unfilled_index))
8941         constructor_type = NULL_TREE;
8942       else
8943         {
8944           gcc_assert (!TYPE_SIZE (constructor_type));
8945
8946           if (constructor_depth > 2)
8947             error_init (loc, "initialization of flexible array member in a nested context");
8948           else
8949             pedwarn_init (loc, OPT_Wpedantic,
8950                           "initialization of a flexible array member");
8951
8952           /* We have already issued an error message for the existence
8953              of a flexible array member not at the end of the structure.
8954              Discard the initializer so that we do not die later.  */
8955           if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8956             constructor_type = NULL_TREE;
8957         }
8958     }
8959
8960   switch (vec_safe_length (constructor_elements))
8961     {
8962     case 0:
8963       /* Initialization with { } counts as zeroinit.  */
8964       constructor_zeroinit = 1;
8965       break;
8966     case 1:
8967       /* This might be zeroinit as well.  */
8968       if (integer_zerop ((*constructor_elements)[0].value))
8969         constructor_zeroinit = 1;
8970       break;
8971     default:
8972       /* If the constructor has more than one element, it can't be { 0 }.  */
8973       constructor_zeroinit = 0;
8974       break;
8975     }
8976
8977   /* Warn when some structs are initialized with direct aggregation.  */
8978   if (!implicit && found_missing_braces && warn_missing_braces
8979       && !constructor_zeroinit)
8980     {
8981       gcc_assert (initializer_stack->missing_brace_richloc);
8982       warning_at (initializer_stack->missing_brace_richloc,
8983                   OPT_Wmissing_braces,
8984                   "missing braces around initializer");
8985     }
8986
8987   /* Warn when some struct elements are implicitly initialized to zero.  */
8988   if (warn_missing_field_initializers
8989       && constructor_type
8990       && TREE_CODE (constructor_type) == RECORD_TYPE
8991       && constructor_unfilled_fields)
8992     {
8993         /* Do not warn for flexible array members or zero-length arrays.  */
8994         while (constructor_unfilled_fields
8995                && (!DECL_SIZE (constructor_unfilled_fields)
8996                    || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8997           constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8998
8999         if (constructor_unfilled_fields
9000             /* Do not warn if this level of the initializer uses member
9001                designators; it is likely to be deliberate.  */
9002             && !constructor_designated
9003             /* Do not warn about initializing with { 0 } or with { }.  */
9004             && !constructor_zeroinit)
9005           {
9006             if (warning_at (input_location, OPT_Wmissing_field_initializers,
9007                             "missing initializer for field %qD of %qT",
9008                             constructor_unfilled_fields,
9009                             constructor_type))
9010               inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
9011                       "%qD declared here", constructor_unfilled_fields);
9012           }
9013     }
9014
9015   /* Pad out the end of the structure.  */
9016   if (p->replacement_value.value)
9017     /* If this closes a superfluous brace pair,
9018        just pass out the element between them.  */
9019     ret = p->replacement_value;
9020   else if (constructor_type == NULL_TREE)
9021     ;
9022   else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
9023            && TREE_CODE (constructor_type) != ARRAY_TYPE
9024            && !gnu_vector_type_p (constructor_type))
9025     {
9026       /* A nonincremental scalar initializer--just return
9027          the element, after verifying there is just one.  */
9028       if (vec_safe_is_empty (constructor_elements))
9029         {
9030           if (!constructor_erroneous && constructor_type != error_mark_node)
9031             error_init (loc, "empty scalar initializer");
9032           ret.value = error_mark_node;
9033         }
9034       else if (vec_safe_length (constructor_elements) != 1)
9035         {
9036           error_init (loc, "extra elements in scalar initializer");
9037           ret.value = (*constructor_elements)[0].value;
9038         }
9039       else
9040         ret.value = (*constructor_elements)[0].value;
9041     }
9042   else
9043     {
9044       if (constructor_erroneous)
9045         ret.value = error_mark_node;
9046       else
9047         {
9048           ret.value = build_constructor (constructor_type,
9049                                          constructor_elements);
9050           if (constructor_constant)
9051             TREE_CONSTANT (ret.value) = 1;
9052           if (constructor_constant && constructor_simple)
9053             TREE_STATIC (ret.value) = 1;
9054           if (constructor_nonconst)
9055             CONSTRUCTOR_NON_CONST (ret.value) = 1;
9056         }
9057     }
9058
9059   if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
9060     {
9061       if (constructor_nonconst)
9062         ret.original_code = C_MAYBE_CONST_EXPR;
9063       else if (ret.original_code == C_MAYBE_CONST_EXPR)
9064         ret.original_code = ERROR_MARK;
9065     }
9066
9067   constructor_type = p->type;
9068   constructor_fields = p->fields;
9069   constructor_index = p->index;
9070   constructor_max_index = p->max_index;
9071   constructor_unfilled_index = p->unfilled_index;
9072   constructor_unfilled_fields = p->unfilled_fields;
9073   constructor_bit_index = p->bit_index;
9074   constructor_elements = p->elements;
9075   constructor_constant = p->constant;
9076   constructor_simple = p->simple;
9077   constructor_nonconst = p->nonconst;
9078   constructor_erroneous = p->erroneous;
9079   constructor_incremental = p->incremental;
9080   constructor_designated = p->designated;
9081   designator_depth = p->designator_depth;
9082   constructor_pending_elts = p->pending_elts;
9083   constructor_depth = p->depth;
9084   if (!p->implicit)
9085     constructor_range_stack = p->range_stack;
9086   RESTORE_SPELLING_DEPTH (constructor_depth);
9087
9088   constructor_stack = p->next;
9089   XDELETE (p);
9090
9091   if (ret.value == NULL_TREE && constructor_stack == 0)
9092     ret.value = error_mark_node;
9093   return ret;
9094 }
9095
9096 /* Common handling for both array range and field name designators.
9097    ARRAY argument is nonzero for array ranges.  Returns false for success.  */
9098
9099 static bool
9100 set_designator (location_t loc, bool array,
9101                 struct obstack *braced_init_obstack)
9102 {
9103   tree subtype;
9104   enum tree_code subcode;
9105
9106   /* Don't die if an entire brace-pair level is superfluous
9107      in the containing level, or for an erroneous type.  */
9108   if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
9109     return true;
9110
9111   /* If there were errors in this designator list already, bail out
9112      silently.  */
9113   if (designator_erroneous)
9114     return true;
9115
9116   /* Likewise for an initializer for a variable-size type.  Those are
9117      diagnosed in digest_init.  */
9118   if (COMPLETE_TYPE_P (constructor_type)
9119       && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
9120     return true;
9121
9122   if (!designator_depth)
9123     {
9124       gcc_assert (!constructor_range_stack);
9125
9126       /* Designator list starts at the level of closest explicit
9127          braces.  */
9128       while (constructor_stack->implicit)
9129         process_init_element (input_location,
9130                               pop_init_level (loc, 1, braced_init_obstack,
9131                                               last_init_list_comma),
9132                               true, braced_init_obstack);
9133       constructor_designated = 1;
9134       return false;
9135     }
9136
9137   switch (TREE_CODE (constructor_type))
9138     {
9139     case  RECORD_TYPE:
9140     case  UNION_TYPE:
9141       subtype = TREE_TYPE (constructor_fields);
9142       if (subtype != error_mark_node)
9143         subtype = TYPE_MAIN_VARIANT (subtype);
9144       break;
9145     case ARRAY_TYPE:
9146       subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9147       break;
9148     default:
9149       gcc_unreachable ();
9150     }
9151
9152   subcode = TREE_CODE (subtype);
9153   if (array && subcode != ARRAY_TYPE)
9154     {
9155       error_init (loc, "array index in non-array initializer");
9156       return true;
9157     }
9158   else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
9159     {
9160       error_init (loc, "field name not in record or union initializer");
9161       return true;
9162     }
9163
9164   constructor_designated = 1;
9165   finish_implicit_inits (loc, braced_init_obstack);
9166   push_init_level (loc, 2, braced_init_obstack);
9167   return false;
9168 }
9169
9170 /* If there are range designators in designator list, push a new designator
9171    to constructor_range_stack.  RANGE_END is end of such stack range or
9172    NULL_TREE if there is no range designator at this level.  */
9173
9174 static void
9175 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
9176 {
9177   struct constructor_range_stack *p;
9178
9179   p = (struct constructor_range_stack *)
9180     obstack_alloc (braced_init_obstack,
9181                    sizeof (struct constructor_range_stack));
9182   p->prev = constructor_range_stack;
9183   p->next = 0;
9184   p->fields = constructor_fields;
9185   p->range_start = constructor_index;
9186   p->index = constructor_index;
9187   p->stack = constructor_stack;
9188   p->range_end = range_end;
9189   if (constructor_range_stack)
9190     constructor_range_stack->next = p;
9191   constructor_range_stack = p;
9192 }
9193
9194 /* Within an array initializer, specify the next index to be initialized.
9195    FIRST is that index.  If LAST is nonzero, then initialize a range
9196    of indices, running from FIRST through LAST.  */
9197
9198 void
9199 set_init_index (location_t loc, tree first, tree last,
9200                 struct obstack *braced_init_obstack)
9201 {
9202   if (set_designator (loc, true, braced_init_obstack))
9203     return;
9204
9205   designator_erroneous = 1;
9206
9207   if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9208       || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9209     {
9210       error_init (loc, "array index in initializer not of integer type");
9211       return;
9212     }
9213
9214   if (TREE_CODE (first) != INTEGER_CST)
9215     {
9216       first = c_fully_fold (first, false, NULL);
9217       if (TREE_CODE (first) == INTEGER_CST)
9218         pedwarn_init (loc, OPT_Wpedantic,
9219                       "array index in initializer is not "
9220                       "an integer constant expression");
9221     }
9222
9223   if (last && TREE_CODE (last) != INTEGER_CST)
9224     {
9225       last = c_fully_fold (last, false, NULL);
9226       if (TREE_CODE (last) == INTEGER_CST)
9227         pedwarn_init (loc, OPT_Wpedantic,
9228                       "array index in initializer is not "
9229                       "an integer constant expression");
9230     }
9231
9232   if (TREE_CODE (first) != INTEGER_CST)
9233     error_init (loc, "nonconstant array index in initializer");
9234   else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9235     error_init (loc, "nonconstant array index in initializer");
9236   else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9237     error_init (loc, "array index in non-array initializer");
9238   else if (tree_int_cst_sgn (first) == -1)
9239     error_init (loc, "array index in initializer exceeds array bounds");
9240   else if (constructor_max_index
9241            && tree_int_cst_lt (constructor_max_index, first))
9242     error_init (loc, "array index in initializer exceeds array bounds");
9243   else
9244     {
9245       constant_expression_warning (first);
9246       if (last)
9247         constant_expression_warning (last);
9248       constructor_index = convert (bitsizetype, first);
9249       if (tree_int_cst_lt (constructor_index, first))
9250         {
9251           constructor_index = copy_node (constructor_index);
9252           TREE_OVERFLOW (constructor_index) = 1;
9253         }
9254
9255       if (last)
9256         {
9257           if (tree_int_cst_equal (first, last))
9258             last = NULL_TREE;
9259           else if (tree_int_cst_lt (last, first))
9260             {
9261               error_init (loc, "empty index range in initializer");
9262               last = NULL_TREE;
9263             }
9264           else
9265             {
9266               last = convert (bitsizetype, last);
9267               if (constructor_max_index != NULL_TREE
9268                   && tree_int_cst_lt (constructor_max_index, last))
9269                 {
9270                   error_init (loc, "array index range in initializer exceeds "
9271                               "array bounds");
9272                   last = NULL_TREE;
9273                 }
9274             }
9275         }
9276
9277       designator_depth++;
9278       designator_erroneous = 0;
9279       if (constructor_range_stack || last)
9280         push_range_stack (last, braced_init_obstack);
9281     }
9282 }
9283
9284 /* Within a struct initializer, specify the next field to be initialized.  */
9285
9286 void
9287 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9288                 struct obstack *braced_init_obstack)
9289 {
9290   tree field;
9291
9292   if (set_designator (loc, false, braced_init_obstack))
9293     return;
9294
9295   designator_erroneous = 1;
9296
9297   if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9298     {
9299       error_init (loc, "field name not in record or union initializer");
9300       return;
9301     }
9302
9303   field = lookup_field (constructor_type, fieldname);
9304
9305   if (field == NULL_TREE)
9306     {
9307       tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9308       if (guessed_id)
9309         {
9310           gcc_rich_location rich_loc (fieldname_loc);
9311           rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9312           error_at (&rich_loc,
9313                     "%qT has no member named %qE; did you mean %qE?",
9314                     constructor_type, fieldname, guessed_id);
9315         }
9316       else
9317         error_at (fieldname_loc, "%qT has no member named %qE",
9318                   constructor_type, fieldname);
9319     }
9320   else
9321     do
9322       {
9323         constructor_fields = TREE_VALUE (field);
9324         designator_depth++;
9325         designator_erroneous = 0;
9326         if (constructor_range_stack)
9327           push_range_stack (NULL_TREE, braced_init_obstack);
9328         field = TREE_CHAIN (field);
9329         if (field)
9330           {
9331             if (set_designator (loc, false, braced_init_obstack))
9332               return;
9333           }
9334       }
9335     while (field != NULL_TREE);
9336 }
9337 \f
9338 /* Add a new initializer to the tree of pending initializers.  PURPOSE
9339    identifies the initializer, either array index or field in a structure.
9340    VALUE is the value of that index or field.  If ORIGTYPE is not
9341    NULL_TREE, it is the original type of VALUE.
9342
9343    IMPLICIT is true if value comes from pop_init_level (1),
9344    the new initializer has been merged with the existing one
9345    and thus no warnings should be emitted about overriding an
9346    existing initializer.  */
9347
9348 static void
9349 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9350                   bool implicit, struct obstack *braced_init_obstack)
9351 {
9352   struct init_node *p, **q, *r;
9353
9354   q = &constructor_pending_elts;
9355   p = 0;
9356
9357   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9358     {
9359       while (*q != 0)
9360         {
9361           p = *q;
9362           if (tree_int_cst_lt (purpose, p->purpose))
9363             q = &p->left;
9364           else if (tree_int_cst_lt (p->purpose, purpose))
9365             q = &p->right;
9366           else
9367             {
9368               if (!implicit)
9369                 {
9370                   if (TREE_SIDE_EFFECTS (p->value))
9371                     warning_init (loc, OPT_Woverride_init_side_effects,
9372                                   "initialized field with side-effects "
9373                                   "overwritten");
9374                   else if (warn_override_init)
9375                     warning_init (loc, OPT_Woverride_init,
9376                                   "initialized field overwritten");
9377                 }
9378               p->value = value;
9379               p->origtype = origtype;
9380               return;
9381             }
9382         }
9383     }
9384   else
9385     {
9386       tree bitpos;
9387
9388       bitpos = bit_position (purpose);
9389       while (*q != NULL)
9390         {
9391           p = *q;
9392           if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9393             q = &p->left;
9394           else if (p->purpose != purpose)
9395             q = &p->right;
9396           else
9397             {
9398               if (!implicit)
9399                 {
9400                   if (TREE_SIDE_EFFECTS (p->value))
9401                     warning_init (loc, OPT_Woverride_init_side_effects,
9402                                   "initialized field with side-effects "
9403                                   "overwritten");
9404                   else if (warn_override_init)
9405                     warning_init (loc, OPT_Woverride_init,
9406                                   "initialized field overwritten");
9407                 }
9408               p->value = value;
9409               p->origtype = origtype;
9410               return;
9411             }
9412         }
9413     }
9414
9415   r = (struct init_node *) obstack_alloc (braced_init_obstack,
9416                                           sizeof (struct init_node));
9417   r->purpose = purpose;
9418   r->value = value;
9419   r->origtype = origtype;
9420
9421   *q = r;
9422   r->parent = p;
9423   r->left = 0;
9424   r->right = 0;
9425   r->balance = 0;
9426
9427   while (p)
9428     {
9429       struct init_node *s;
9430
9431       if (r == p->left)
9432         {
9433           if (p->balance == 0)
9434             p->balance = -1;
9435           else if (p->balance < 0)
9436             {
9437               if (r->balance < 0)
9438                 {
9439                   /* L rotation.  */
9440                   p->left = r->right;
9441                   if (p->left)
9442                     p->left->parent = p;
9443                   r->right = p;
9444
9445                   p->balance = 0;
9446                   r->balance = 0;
9447
9448                   s = p->parent;
9449                   p->parent = r;
9450                   r->parent = s;
9451                   if (s)
9452                     {
9453                       if (s->left == p)
9454                         s->left = r;
9455                       else
9456                         s->right = r;
9457                     }
9458                   else
9459                     constructor_pending_elts = r;
9460                 }
9461               else
9462                 {
9463                   /* LR rotation.  */
9464                   struct init_node *t = r->right;
9465
9466                   r->right = t->left;
9467                   if (r->right)
9468                     r->right->parent = r;
9469                   t->left = r;
9470
9471                   p->left = t->right;
9472                   if (p->left)
9473                     p->left->parent = p;
9474                   t->right = p;
9475
9476                   p->balance = t->balance < 0;
9477                   r->balance = -(t->balance > 0);
9478                   t->balance = 0;
9479
9480                   s = p->parent;
9481                   p->parent = t;
9482                   r->parent = t;
9483                   t->parent = s;
9484                   if (s)
9485                     {
9486                       if (s->left == p)
9487                         s->left = t;
9488                       else
9489                         s->right = t;
9490                     }
9491                   else
9492                     constructor_pending_elts = t;
9493                 }
9494               break;
9495             }
9496           else
9497             {
9498               /* p->balance == +1; growth of left side balances the node.  */
9499               p->balance = 0;
9500               break;
9501             }
9502         }
9503       else /* r == p->right */
9504         {
9505           if (p->balance == 0)
9506             /* Growth propagation from right side.  */
9507             p->balance++;
9508           else if (p->balance > 0)
9509             {
9510               if (r->balance > 0)
9511                 {
9512                   /* R rotation.  */
9513                   p->right = r->left;
9514                   if (p->right)
9515                     p->right->parent = p;
9516                   r->left = p;
9517
9518                   p->balance = 0;
9519                   r->balance = 0;
9520
9521                   s = p->parent;
9522                   p->parent = r;
9523                   r->parent = s;
9524                   if (s)
9525                     {
9526                       if (s->left == p)
9527                         s->left = r;
9528                       else
9529                         s->right = r;
9530                     }
9531                   else
9532                     constructor_pending_elts = r;
9533                 }
9534               else /* r->balance == -1 */
9535                 {
9536                   /* RL rotation */
9537                   struct init_node *t = r->left;
9538
9539                   r->left = t->right;
9540                   if (r->left)
9541                     r->left->parent = r;
9542                   t->right = r;
9543
9544                   p->right = t->left;
9545                   if (p->right)
9546                     p->right->parent = p;
9547                   t->left = p;
9548
9549                   r->balance = (t->balance < 0);
9550                   p->balance = -(t->balance > 0);
9551                   t->balance = 0;
9552
9553                   s = p->parent;
9554                   p->parent = t;
9555                   r->parent = t;
9556                   t->parent = s;
9557                   if (s)
9558                     {
9559                       if (s->left == p)
9560                         s->left = t;
9561                       else
9562                         s->right = t;
9563                     }
9564                   else
9565                     constructor_pending_elts = t;
9566                 }
9567               break;
9568             }
9569           else
9570             {
9571               /* p->balance == -1; growth of right side balances the node.  */
9572               p->balance = 0;
9573               break;
9574             }
9575         }
9576
9577       r = p;
9578       p = p->parent;
9579     }
9580 }
9581
9582 /* Build AVL tree from a sorted chain.  */
9583
9584 static void
9585 set_nonincremental_init (struct obstack * braced_init_obstack)
9586 {
9587   unsigned HOST_WIDE_INT ix;
9588   tree index, value;
9589
9590   if (TREE_CODE (constructor_type) != RECORD_TYPE
9591       && TREE_CODE (constructor_type) != ARRAY_TYPE)
9592     return;
9593
9594   FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9595     add_pending_init (input_location, index, value, NULL_TREE, true,
9596                       braced_init_obstack);
9597   constructor_elements = NULL;
9598   if (TREE_CODE (constructor_type) == RECORD_TYPE)
9599     {
9600       constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9601       /* Skip any nameless bit fields at the beginning.  */
9602       while (constructor_unfilled_fields != NULL_TREE
9603              && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9604         constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9605
9606     }
9607   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9608     {
9609       if (TYPE_DOMAIN (constructor_type))
9610         constructor_unfilled_index
9611             = convert (bitsizetype,
9612                        TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9613       else
9614         constructor_unfilled_index = bitsize_zero_node;
9615     }
9616   constructor_incremental = 0;
9617 }
9618
9619 /* Build AVL tree from a string constant.  */
9620
9621 static void
9622 set_nonincremental_init_from_string (tree str,
9623                                      struct obstack * braced_init_obstack)
9624 {
9625   tree value, purpose, type;
9626   HOST_WIDE_INT val[2];
9627   const char *p, *end;
9628   int byte, wchar_bytes, charwidth, bitpos;
9629
9630   gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9631
9632   wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9633   charwidth = TYPE_PRECISION (char_type_node);
9634   gcc_assert ((size_t) wchar_bytes * charwidth
9635               <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9636   type = TREE_TYPE (constructor_type);
9637   p = TREE_STRING_POINTER (str);
9638   end = p + TREE_STRING_LENGTH (str);
9639
9640   for (purpose = bitsize_zero_node;
9641        p < end
9642        && !(constructor_max_index
9643             && tree_int_cst_lt (constructor_max_index, purpose));
9644        purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9645     {
9646       if (wchar_bytes == 1)
9647         {
9648           val[0] = (unsigned char) *p++;
9649           val[1] = 0;
9650         }
9651       else
9652         {
9653           val[1] = 0;
9654           val[0] = 0;
9655           for (byte = 0; byte < wchar_bytes; byte++)
9656             {
9657               if (BYTES_BIG_ENDIAN)
9658                 bitpos = (wchar_bytes - byte - 1) * charwidth;
9659               else
9660                 bitpos = byte * charwidth;
9661               val[bitpos / HOST_BITS_PER_WIDE_INT]
9662                 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9663                    << (bitpos % HOST_BITS_PER_WIDE_INT);
9664             }
9665         }
9666
9667       if (!TYPE_UNSIGNED (type))
9668         {
9669           bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9670           if (bitpos < HOST_BITS_PER_WIDE_INT)
9671             {
9672               if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9673                 {
9674                   val[0] |= HOST_WIDE_INT_M1U << bitpos;
9675                   val[1] = -1;
9676                 }
9677             }
9678           else if (bitpos == HOST_BITS_PER_WIDE_INT)
9679             {
9680               if (val[0] < 0)
9681                 val[1] = -1;
9682             }
9683           else if (val[1] & (HOST_WIDE_INT_1
9684                              << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9685             val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9686         }
9687
9688       value = wide_int_to_tree (type,
9689                                 wide_int::from_array (val, 2,
9690                                                       HOST_BITS_PER_WIDE_INT * 2));
9691       add_pending_init (input_location, purpose, value, NULL_TREE, true,
9692                         braced_init_obstack);
9693     }
9694
9695   constructor_incremental = 0;
9696 }
9697
9698 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9699    not initialized yet.  */
9700
9701 static tree
9702 find_init_member (tree field, struct obstack * braced_init_obstack)
9703 {
9704   struct init_node *p;
9705
9706   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9707     {
9708       if (constructor_incremental
9709           && tree_int_cst_lt (field, constructor_unfilled_index))
9710         set_nonincremental_init (braced_init_obstack);
9711
9712       p = constructor_pending_elts;
9713       while (p)
9714         {
9715           if (tree_int_cst_lt (field, p->purpose))
9716             p = p->left;
9717           else if (tree_int_cst_lt (p->purpose, field))
9718             p = p->right;
9719           else
9720             return p->value;
9721         }
9722     }
9723   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9724     {
9725       tree bitpos = bit_position (field);
9726
9727       if (constructor_incremental
9728           && (!constructor_unfilled_fields
9729               || tree_int_cst_lt (bitpos,
9730                                   bit_position (constructor_unfilled_fields))))
9731         set_nonincremental_init (braced_init_obstack);
9732
9733       p = constructor_pending_elts;
9734       while (p)
9735         {
9736           if (field == p->purpose)
9737             return p->value;
9738           else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9739             p = p->left;
9740           else
9741             p = p->right;
9742         }
9743     }
9744   else if (TREE_CODE (constructor_type) == UNION_TYPE)
9745     {
9746       if (!vec_safe_is_empty (constructor_elements)
9747           && (constructor_elements->last ().index == field))
9748         return constructor_elements->last ().value;
9749     }
9750   return NULL_TREE;
9751 }
9752
9753 /* "Output" the next constructor element.
9754    At top level, really output it to assembler code now.
9755    Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9756    If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9757    TYPE is the data type that the containing data type wants here.
9758    FIELD is the field (a FIELD_DECL) or the index that this element fills.
9759    If VALUE is a string constant, STRICT_STRING is true if it is
9760    unparenthesized or we should not warn here for it being parenthesized.
9761    For other types of VALUE, STRICT_STRING is not used.
9762
9763    PENDING if true means output pending elements that belong
9764    right after this element.  (PENDING is normally true;
9765    it is false while outputting pending elements, to avoid recursion.)
9766
9767    IMPLICIT is true if value comes from pop_init_level (1),
9768    the new initializer has been merged with the existing one
9769    and thus no warnings should be emitted about overriding an
9770    existing initializer.  */
9771
9772 static void
9773 output_init_element (location_t loc, tree value, tree origtype,
9774                      bool strict_string, tree type, tree field, bool pending,
9775                      bool implicit, struct obstack * braced_init_obstack)
9776 {
9777   tree semantic_type = NULL_TREE;
9778   bool maybe_const = true;
9779   bool npc;
9780
9781   if (type == error_mark_node || value == error_mark_node)
9782     {
9783       constructor_erroneous = 1;
9784       return;
9785     }
9786   if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9787       && (TREE_CODE (value) == STRING_CST
9788           || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9789       && !(TREE_CODE (value) == STRING_CST
9790            && TREE_CODE (type) == ARRAY_TYPE
9791            && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9792       && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9793                      TYPE_MAIN_VARIANT (type)))
9794     value = array_to_pointer_conversion (input_location, value);
9795
9796   if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9797       && require_constant_value && pending)
9798     {
9799       /* As an extension, allow initializing objects with static storage
9800          duration with compound literals (which are then treated just as
9801          the brace enclosed list they contain).  */
9802       if (flag_isoc99)
9803         pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9804                       "constant");
9805       tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9806       value = DECL_INITIAL (decl);
9807     }
9808
9809   npc = null_pointer_constant_p (value);
9810   if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9811     {
9812       semantic_type = TREE_TYPE (value);
9813       value = TREE_OPERAND (value, 0);
9814     }
9815   value = c_fully_fold (value, require_constant_value, &maybe_const);
9816
9817   if (value == error_mark_node)
9818     constructor_erroneous = 1;
9819   else if (!TREE_CONSTANT (value))
9820     constructor_constant = 0;
9821   else if (!initializer_constant_valid_p (value,
9822                                           TREE_TYPE (value),
9823                                           AGGREGATE_TYPE_P (constructor_type)
9824                                           && TYPE_REVERSE_STORAGE_ORDER
9825                                              (constructor_type))
9826            || (RECORD_OR_UNION_TYPE_P (constructor_type)
9827                && DECL_C_BIT_FIELD (field)
9828                && TREE_CODE (value) != INTEGER_CST))
9829     constructor_simple = 0;
9830   if (!maybe_const)
9831     constructor_nonconst = 1;
9832
9833   /* Digest the initializer and issue any errors about incompatible
9834      types before issuing errors about non-constant initializers.  */
9835   tree new_value = value;
9836   if (semantic_type)
9837     new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9838   new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9839                            require_constant_value);
9840   if (new_value == error_mark_node)
9841     {
9842       constructor_erroneous = 1;
9843       return;
9844     }
9845   if (require_constant_value || require_constant_elements)
9846     constant_expression_warning (new_value);
9847
9848   /* Proceed to check the constness of the original initializer.  */
9849   if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9850     {
9851       if (require_constant_value)
9852         {
9853           error_init (loc, "initializer element is not constant");
9854           value = error_mark_node;
9855         }
9856       else if (require_constant_elements)
9857         pedwarn (loc, OPT_Wpedantic,
9858                  "initializer element is not computable at load time");
9859     }
9860   else if (!maybe_const
9861            && (require_constant_value || require_constant_elements))
9862     pedwarn_init (loc, OPT_Wpedantic,
9863                   "initializer element is not a constant expression");
9864
9865   /* Issue -Wc++-compat warnings about initializing a bitfield with
9866      enum type.  */
9867   if (warn_cxx_compat
9868       && field != NULL_TREE
9869       && TREE_CODE (field) == FIELD_DECL
9870       && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9871       && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9872           != TYPE_MAIN_VARIANT (type))
9873       && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9874     {
9875       tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9876       if (checktype != error_mark_node
9877           && (TYPE_MAIN_VARIANT (checktype)
9878               != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9879         warning_init (loc, OPT_Wc___compat,
9880                       "enum conversion in initialization is invalid in C++");
9881     }
9882
9883   /* If this field is empty and does not have side effects (and is not at
9884      the end of structure), don't do anything other than checking the
9885      initializer.  */
9886   if (field
9887       && (TREE_TYPE (field) == error_mark_node
9888           || (COMPLETE_TYPE_P (TREE_TYPE (field))
9889               && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9890               && !TREE_SIDE_EFFECTS (new_value)
9891               && (TREE_CODE (constructor_type) == ARRAY_TYPE
9892                   || DECL_CHAIN (field)))))
9893     return;
9894
9895   /* Finally, set VALUE to the initializer value digested above.  */
9896   value = new_value;
9897
9898   /* If this element doesn't come next in sequence,
9899      put it on constructor_pending_elts.  */
9900   if (TREE_CODE (constructor_type) == ARRAY_TYPE
9901       && (!constructor_incremental
9902           || !tree_int_cst_equal (field, constructor_unfilled_index)))
9903     {
9904       if (constructor_incremental
9905           && tree_int_cst_lt (field, constructor_unfilled_index))
9906         set_nonincremental_init (braced_init_obstack);
9907
9908       add_pending_init (loc, field, value, origtype, implicit,
9909                         braced_init_obstack);
9910       return;
9911     }
9912   else if (TREE_CODE (constructor_type) == RECORD_TYPE
9913            && (!constructor_incremental
9914                || field != constructor_unfilled_fields))
9915     {
9916       /* We do this for records but not for unions.  In a union,
9917          no matter which field is specified, it can be initialized
9918          right away since it starts at the beginning of the union.  */
9919       if (constructor_incremental)
9920         {
9921           if (!constructor_unfilled_fields)
9922             set_nonincremental_init (braced_init_obstack);
9923           else
9924             {
9925               tree bitpos, unfillpos;
9926
9927               bitpos = bit_position (field);
9928               unfillpos = bit_position (constructor_unfilled_fields);
9929
9930               if (tree_int_cst_lt (bitpos, unfillpos))
9931                 set_nonincremental_init (braced_init_obstack);
9932             }
9933         }
9934
9935       add_pending_init (loc, field, value, origtype, implicit,
9936                         braced_init_obstack);
9937       return;
9938     }
9939   else if (TREE_CODE (constructor_type) == UNION_TYPE
9940            && !vec_safe_is_empty (constructor_elements))
9941     {
9942       if (!implicit)
9943         {
9944           if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9945             warning_init (loc, OPT_Woverride_init_side_effects,
9946                           "initialized field with side-effects overwritten");
9947           else if (warn_override_init)
9948             warning_init (loc, OPT_Woverride_init,
9949                           "initialized field overwritten");
9950         }
9951
9952       /* We can have just one union field set.  */
9953       constructor_elements = NULL;
9954     }
9955
9956   /* Otherwise, output this element either to
9957      constructor_elements or to the assembler file.  */
9958
9959   constructor_elt celt = {field, value};
9960   vec_safe_push (constructor_elements, celt);
9961
9962   /* Advance the variable that indicates sequential elements output.  */
9963   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9964     constructor_unfilled_index
9965       = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9966                         bitsize_one_node);
9967   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9968     {
9969       constructor_unfilled_fields
9970         = DECL_CHAIN (constructor_unfilled_fields);
9971
9972       /* Skip any nameless bit fields.  */
9973       while (constructor_unfilled_fields != NULL_TREE
9974              && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9975         constructor_unfilled_fields =
9976           DECL_CHAIN (constructor_unfilled_fields);
9977     }
9978   else if (TREE_CODE (constructor_type) == UNION_TYPE)
9979     constructor_unfilled_fields = NULL_TREE;
9980
9981   /* Now output any pending elements which have become next.  */
9982   if (pending)
9983     output_pending_init_elements (0, braced_init_obstack);
9984 }
9985
9986 /* For two FIELD_DECLs in the same chain, return -1 if field1
9987    comes before field2, 1 if field1 comes after field2 and
9988    0 if field1 == field2.  */
9989
9990 static int
9991 init_field_decl_cmp (tree field1, tree field2)
9992 {
9993   if (field1 == field2)
9994     return 0;
9995
9996   tree bitpos1 = bit_position (field1);
9997   tree bitpos2 = bit_position (field2);
9998   if (tree_int_cst_equal (bitpos1, bitpos2))
9999     {
10000       /* If one of the fields has non-zero bitsize, then that
10001          field must be the last one in a sequence of zero
10002          sized fields, fields after it will have bigger
10003          bit_position.  */
10004       if (TREE_TYPE (field1) != error_mark_node
10005           && COMPLETE_TYPE_P (TREE_TYPE (field1))
10006           && integer_nonzerop (TREE_TYPE (field1)))
10007         return 1;
10008       if (TREE_TYPE (field2) != error_mark_node
10009           && COMPLETE_TYPE_P (TREE_TYPE (field2))
10010           && integer_nonzerop (TREE_TYPE (field2)))
10011         return -1;
10012       /* Otherwise, fallback to DECL_CHAIN walk to find out
10013          which field comes earlier.  Walk chains of both
10014          fields, so that if field1 and field2 are close to each
10015          other in either order, it is found soon even for large
10016          sequences of zero sized fields.  */
10017       tree f1 = field1, f2 = field2;
10018       while (1)
10019         {
10020           f1 = DECL_CHAIN (f1);
10021           f2 = DECL_CHAIN (f2);
10022           if (f1 == NULL_TREE)
10023             {
10024               gcc_assert (f2);
10025               return 1;
10026             }
10027           if (f2 == NULL_TREE)
10028             return -1;
10029           if (f1 == field2)
10030             return -1;
10031           if (f2 == field1)
10032             return 1;
10033           if (!tree_int_cst_equal (bit_position (f1), bitpos1))
10034             return 1;
10035           if (!tree_int_cst_equal (bit_position (f2), bitpos1))
10036             return -1;
10037         }
10038     }
10039   else if (tree_int_cst_lt (bitpos1, bitpos2))
10040     return -1;
10041   else
10042     return 1;
10043 }
10044
10045 /* Output any pending elements which have become next.
10046    As we output elements, constructor_unfilled_{fields,index}
10047    advances, which may cause other elements to become next;
10048    if so, they too are output.
10049
10050    If ALL is 0, we return when there are
10051    no more pending elements to output now.
10052
10053    If ALL is 1, we output space as necessary so that
10054    we can output all the pending elements.  */
10055 static void
10056 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
10057 {
10058   struct init_node *elt = constructor_pending_elts;
10059   tree next;
10060
10061  retry:
10062
10063   /* Look through the whole pending tree.
10064      If we find an element that should be output now,
10065      output it.  Otherwise, set NEXT to the element
10066      that comes first among those still pending.  */
10067
10068   next = NULL_TREE;
10069   while (elt)
10070     {
10071       if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10072         {
10073           if (tree_int_cst_equal (elt->purpose,
10074                                   constructor_unfilled_index))
10075             output_init_element (input_location, elt->value, elt->origtype,
10076                                  true, TREE_TYPE (constructor_type),
10077                                  constructor_unfilled_index, false, false,
10078                                  braced_init_obstack);
10079           else if (tree_int_cst_lt (constructor_unfilled_index,
10080                                     elt->purpose))
10081             {
10082               /* Advance to the next smaller node.  */
10083               if (elt->left)
10084                 elt = elt->left;
10085               else
10086                 {
10087                   /* We have reached the smallest node bigger than the
10088                      current unfilled index.  Fill the space first.  */
10089                   next = elt->purpose;
10090                   break;
10091                 }
10092             }
10093           else
10094             {
10095               /* Advance to the next bigger node.  */
10096               if (elt->right)
10097                 elt = elt->right;
10098               else
10099                 {
10100                   /* We have reached the biggest node in a subtree.  Find
10101                      the parent of it, which is the next bigger node.  */
10102                   while (elt->parent && elt->parent->right == elt)
10103                     elt = elt->parent;
10104                   elt = elt->parent;
10105                   if (elt && tree_int_cst_lt (constructor_unfilled_index,
10106                                               elt->purpose))
10107                     {
10108                       next = elt->purpose;
10109                       break;
10110                     }
10111                 }
10112             }
10113         }
10114       else if (RECORD_OR_UNION_TYPE_P (constructor_type))
10115         {
10116           /* If the current record is complete we are done.  */
10117           if (constructor_unfilled_fields == NULL_TREE)
10118             break;
10119
10120           int cmp = init_field_decl_cmp (constructor_unfilled_fields,
10121                                          elt->purpose);
10122           if (cmp == 0)
10123             output_init_element (input_location, elt->value, elt->origtype,
10124                                  true, TREE_TYPE (elt->purpose),
10125                                  elt->purpose, false, false,
10126                                  braced_init_obstack);
10127           else if (cmp < 0)
10128             {
10129               /* Advance to the next smaller node.  */
10130               if (elt->left)
10131                 elt = elt->left;
10132               else
10133                 {
10134                   /* We have reached the smallest node bigger than the
10135                      current unfilled field.  Fill the space first.  */
10136                   next = elt->purpose;
10137                   break;
10138                 }
10139             }
10140           else
10141             {
10142               /* Advance to the next bigger node.  */
10143               if (elt->right)
10144                 elt = elt->right;
10145               else
10146                 {
10147                   /* We have reached the biggest node in a subtree.  Find
10148                      the parent of it, which is the next bigger node.  */
10149                   while (elt->parent && elt->parent->right == elt)
10150                     elt = elt->parent;
10151                   elt = elt->parent;
10152                   if (elt
10153                       && init_field_decl_cmp (constructor_unfilled_fields,
10154                                               elt->purpose) < 0)
10155                     {
10156                       next = elt->purpose;
10157                       break;
10158                     }
10159                 }
10160             }
10161         }
10162     }
10163
10164   /* Ordinarily return, but not if we want to output all
10165      and there are elements left.  */
10166   if (!(all && next != NULL_TREE))
10167     return;
10168
10169   /* If it's not incremental, just skip over the gap, so that after
10170      jumping to retry we will output the next successive element.  */
10171   if (RECORD_OR_UNION_TYPE_P (constructor_type))
10172     constructor_unfilled_fields = next;
10173   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10174     constructor_unfilled_index = next;
10175
10176   /* ELT now points to the node in the pending tree with the next
10177      initializer to output.  */
10178   goto retry;
10179 }
10180 \f
10181 /* Expression VALUE coincides with the start of type TYPE in a braced
10182    initializer.  Return true if we should treat VALUE as initializing
10183    the first element of TYPE, false if we should treat it as initializing
10184    TYPE as a whole.
10185
10186    If the initializer is clearly invalid, the question becomes:
10187    which choice gives the best error message?  */
10188
10189 static bool
10190 initialize_elementwise_p (tree type, tree value)
10191 {
10192   if (type == error_mark_node || value == error_mark_node)
10193     return false;
10194
10195   gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
10196
10197   tree value_type = TREE_TYPE (value);
10198   if (value_type == error_mark_node)
10199     return false;
10200
10201   /* GNU vectors can be initialized elementwise.  However, treat any
10202      kind of vector value as initializing the vector type as a whole,
10203      regardless of whether the value is a GNU vector.  Such initializers
10204      are valid if and only if they would have been valid in a non-braced
10205      initializer like:
10206
10207         TYPE foo = VALUE;
10208
10209      so recursing into the vector type would be at best confusing or at
10210      worst wrong.  For example, when -flax-vector-conversions is in effect,
10211      it's possible to initialize a V8HI from a V4SI, even though the vectors
10212      have different element types and different numbers of elements.  */
10213   if (gnu_vector_type_p (type))
10214     return !VECTOR_TYPE_P (value_type);
10215
10216   if (AGGREGATE_TYPE_P (type))
10217     return type != TYPE_MAIN_VARIANT (value_type);
10218
10219   return false;
10220 }
10221
10222 /* Add one non-braced element to the current constructor level.
10223    This adjusts the current position within the constructor's type.
10224    This may also start or terminate implicit levels
10225    to handle a partly-braced initializer.
10226
10227    Once this has found the correct level for the new element,
10228    it calls output_init_element.
10229
10230    IMPLICIT is true if value comes from pop_init_level (1),
10231    the new initializer has been merged with the existing one
10232    and thus no warnings should be emitted about overriding an
10233    existing initializer.  */
10234
10235 void
10236 process_init_element (location_t loc, struct c_expr value, bool implicit,
10237                       struct obstack * braced_init_obstack)
10238 {
10239   tree orig_value = value.value;
10240   int string_flag
10241     = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10242   bool strict_string = value.original_code == STRING_CST;
10243   bool was_designated = designator_depth != 0;
10244
10245   designator_depth = 0;
10246   designator_erroneous = 0;
10247
10248   if (!implicit && value.value && !integer_zerop (value.value))
10249     constructor_zeroinit = 0;
10250
10251   /* Handle superfluous braces around string cst as in
10252      char x[] = {"foo"}; */
10253   if (string_flag
10254       && constructor_type
10255       && !was_designated
10256       && TREE_CODE (constructor_type) == ARRAY_TYPE
10257       && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10258       && integer_zerop (constructor_unfilled_index))
10259     {
10260       if (constructor_stack->replacement_value.value)
10261         error_init (loc, "excess elements in %<char%> array initializer");
10262       constructor_stack->replacement_value = value;
10263       return;
10264     }
10265
10266   if (constructor_stack->replacement_value.value != NULL_TREE)
10267     {
10268       error_init (loc, "excess elements in struct initializer");
10269       return;
10270     }
10271
10272   /* Ignore elements of a brace group if it is entirely superfluous
10273      and has already been diagnosed, or if the type is erroneous.  */
10274   if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10275     return;
10276
10277   /* Ignore elements of an initializer for a variable-size type.
10278      Those are diagnosed in digest_init.  */
10279   if (COMPLETE_TYPE_P (constructor_type)
10280       && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10281     return;
10282
10283   if (!implicit && warn_designated_init && !was_designated
10284       && TREE_CODE (constructor_type) == RECORD_TYPE
10285       && lookup_attribute ("designated_init",
10286                            TYPE_ATTRIBUTES (constructor_type)))
10287     warning_init (loc,
10288                   OPT_Wdesignated_init,
10289                   "positional initialization of field "
10290                   "in %<struct%> declared with %<designated_init%> attribute");
10291
10292   /* If we've exhausted any levels that didn't have braces,
10293      pop them now.  */
10294   while (constructor_stack->implicit)
10295     {
10296       if (RECORD_OR_UNION_TYPE_P (constructor_type)
10297           && constructor_fields == NULL_TREE)
10298         process_init_element (loc,
10299                               pop_init_level (loc, 1, braced_init_obstack,
10300                                               last_init_list_comma),
10301                               true, braced_init_obstack);
10302       else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10303                 || gnu_vector_type_p (constructor_type))
10304                && constructor_max_index
10305                && tree_int_cst_lt (constructor_max_index,
10306                                    constructor_index))
10307         process_init_element (loc,
10308                               pop_init_level (loc, 1, braced_init_obstack,
10309                                               last_init_list_comma),
10310                               true, braced_init_obstack);
10311       else
10312         break;
10313     }
10314
10315   /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once.  */
10316   if (constructor_range_stack)
10317     {
10318       /* If value is a compound literal and we'll be just using its
10319          content, don't put it into a SAVE_EXPR.  */
10320       if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10321           || !require_constant_value)
10322         {
10323           tree semantic_type = NULL_TREE;
10324           if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10325             {
10326               semantic_type = TREE_TYPE (value.value);
10327               value.value = TREE_OPERAND (value.value, 0);
10328             }
10329           value.value = save_expr (value.value);
10330           if (semantic_type)
10331             value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10332                                   value.value);
10333         }
10334     }
10335
10336   while (1)
10337     {
10338       if (TREE_CODE (constructor_type) == RECORD_TYPE)
10339         {
10340           tree fieldtype;
10341           enum tree_code fieldcode;
10342
10343           if (constructor_fields == NULL_TREE)
10344             {
10345               pedwarn_init (loc, 0, "excess elements in struct initializer");
10346               break;
10347             }
10348
10349           fieldtype = TREE_TYPE (constructor_fields);
10350           if (fieldtype != error_mark_node)
10351             fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10352           fieldcode = TREE_CODE (fieldtype);
10353
10354           /* Error for non-static initialization of a flexible array member.  */
10355           if (fieldcode == ARRAY_TYPE
10356               && !require_constant_value
10357               && TYPE_SIZE (fieldtype) == NULL_TREE
10358               && DECL_CHAIN (constructor_fields) == NULL_TREE)
10359             {
10360               error_init (loc, "non-static initialization of a flexible "
10361                           "array member");
10362               break;
10363             }
10364
10365           /* Error for initialization of a flexible array member with
10366              a string constant if the structure is in an array.  E.g.:
10367              struct S { int x; char y[]; };
10368              struct S s[] = { { 1, "foo" } };
10369              is invalid.  */
10370           if (string_flag
10371               && fieldcode == ARRAY_TYPE
10372               && constructor_depth > 1
10373               && TYPE_SIZE (fieldtype) == NULL_TREE
10374               && DECL_CHAIN (constructor_fields) == NULL_TREE)
10375             {
10376               bool in_array_p = false;
10377               for (struct constructor_stack *p = constructor_stack;
10378                    p && p->type; p = p->next)
10379                 if (TREE_CODE (p->type) == ARRAY_TYPE)
10380                   {
10381                     in_array_p = true;
10382                     break;
10383                   }
10384               if (in_array_p)
10385                 {
10386                   error_init (loc, "initialization of flexible array "
10387                               "member in a nested context");
10388                   break;
10389                 }
10390             }
10391
10392           /* Accept a string constant to initialize a subarray.  */
10393           if (value.value != NULL_TREE
10394               && fieldcode == ARRAY_TYPE
10395               && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10396               && string_flag)
10397             value.value = orig_value;
10398           /* Otherwise, if we have come to a subaggregate,
10399              and we don't have an element of its type, push into it.  */
10400           else if (value.value != NULL_TREE
10401                    && initialize_elementwise_p (fieldtype, value.value))
10402             {
10403               push_init_level (loc, 1, braced_init_obstack);
10404               continue;
10405             }
10406
10407           if (value.value)
10408             {
10409               push_member_name (constructor_fields);
10410               output_init_element (loc, value.value, value.original_type,
10411                                    strict_string, fieldtype,
10412                                    constructor_fields, true, implicit,
10413                                    braced_init_obstack);
10414               RESTORE_SPELLING_DEPTH (constructor_depth);
10415             }
10416           else
10417             /* Do the bookkeeping for an element that was
10418                directly output as a constructor.  */
10419             {
10420               /* For a record, keep track of end position of last field.  */
10421               if (DECL_SIZE (constructor_fields))
10422                 constructor_bit_index
10423                   = size_binop_loc (input_location, PLUS_EXPR,
10424                                     bit_position (constructor_fields),
10425                                     DECL_SIZE (constructor_fields));
10426
10427               /* If the current field was the first one not yet written out,
10428                  it isn't now, so update.  */
10429               if (constructor_unfilled_fields == constructor_fields)
10430                 {
10431                   constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10432                   /* Skip any nameless bit fields.  */
10433                   while (constructor_unfilled_fields != 0
10434                          && (DECL_UNNAMED_BIT_FIELD
10435                              (constructor_unfilled_fields)))
10436                     constructor_unfilled_fields =
10437                       DECL_CHAIN (constructor_unfilled_fields);
10438                 }
10439             }
10440
10441           constructor_fields = DECL_CHAIN (constructor_fields);
10442           /* Skip any nameless bit fields at the beginning.  */
10443           while (constructor_fields != NULL_TREE
10444                  && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10445             constructor_fields = DECL_CHAIN (constructor_fields);
10446         }
10447       else if (TREE_CODE (constructor_type) == UNION_TYPE)
10448         {
10449           tree fieldtype;
10450           enum tree_code fieldcode;
10451
10452           if (constructor_fields == NULL_TREE)
10453             {
10454               pedwarn_init (loc, 0,
10455                             "excess elements in union initializer");
10456               break;
10457             }
10458
10459           fieldtype = TREE_TYPE (constructor_fields);
10460           if (fieldtype != error_mark_node)
10461             fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10462           fieldcode = TREE_CODE (fieldtype);
10463
10464           /* Warn that traditional C rejects initialization of unions.
10465              We skip the warning if the value is zero.  This is done
10466              under the assumption that the zero initializer in user
10467              code appears conditioned on e.g. __STDC__ to avoid
10468              "missing initializer" warnings and relies on default
10469              initialization to zero in the traditional C case.
10470              We also skip the warning if the initializer is designated,
10471              again on the assumption that this must be conditional on
10472              __STDC__ anyway (and we've already complained about the
10473              member-designator already).  */
10474           if (!in_system_header_at (input_location) && !constructor_designated
10475               && !(value.value && (integer_zerop (value.value)
10476                                    || real_zerop (value.value))))
10477             warning (OPT_Wtraditional, "traditional C rejects initialization "
10478                      "of unions");
10479
10480           /* Accept a string constant to initialize a subarray.  */
10481           if (value.value != NULL_TREE
10482               && fieldcode == ARRAY_TYPE
10483               && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10484               && string_flag)
10485             value.value = orig_value;
10486           /* Otherwise, if we have come to a subaggregate,
10487              and we don't have an element of its type, push into it.  */
10488           else if (value.value != NULL_TREE
10489                    && initialize_elementwise_p (fieldtype, value.value))
10490             {
10491               push_init_level (loc, 1, braced_init_obstack);
10492               continue;
10493             }
10494
10495           if (value.value)
10496             {
10497               push_member_name (constructor_fields);
10498               output_init_element (loc, value.value, value.original_type,
10499                                    strict_string, fieldtype,
10500                                    constructor_fields, true, implicit,
10501                                    braced_init_obstack);
10502               RESTORE_SPELLING_DEPTH (constructor_depth);
10503             }
10504           else
10505             /* Do the bookkeeping for an element that was
10506                directly output as a constructor.  */
10507             {
10508               constructor_bit_index = DECL_SIZE (constructor_fields);
10509               constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10510             }
10511
10512           constructor_fields = NULL_TREE;
10513         }
10514       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10515         {
10516           tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10517           enum tree_code eltcode = TREE_CODE (elttype);
10518
10519           /* Accept a string constant to initialize a subarray.  */
10520           if (value.value != NULL_TREE
10521               && eltcode == ARRAY_TYPE
10522               && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10523               && string_flag)
10524             value.value = orig_value;
10525           /* Otherwise, if we have come to a subaggregate,
10526              and we don't have an element of its type, push into it.  */
10527           else if (value.value != NULL_TREE
10528                    && initialize_elementwise_p (elttype, value.value))
10529             {
10530               push_init_level (loc, 1, braced_init_obstack);
10531               continue;
10532             }
10533
10534           if (constructor_max_index != NULL_TREE
10535               && (tree_int_cst_lt (constructor_max_index, constructor_index)
10536                   || integer_all_onesp (constructor_max_index)))
10537             {
10538               pedwarn_init (loc, 0,
10539                             "excess elements in array initializer");
10540               break;
10541             }
10542
10543           /* Now output the actual element.  */
10544           if (value.value)
10545             {
10546               push_array_bounds (tree_to_uhwi (constructor_index));
10547               output_init_element (loc, value.value, value.original_type,
10548                                    strict_string, elttype,
10549                                    constructor_index, true, implicit,
10550                                    braced_init_obstack);
10551               RESTORE_SPELLING_DEPTH (constructor_depth);
10552             }
10553
10554           constructor_index
10555             = size_binop_loc (input_location, PLUS_EXPR,
10556                               constructor_index, bitsize_one_node);
10557
10558           if (!value.value)
10559             /* If we are doing the bookkeeping for an element that was
10560                directly output as a constructor, we must update
10561                constructor_unfilled_index.  */
10562             constructor_unfilled_index = constructor_index;
10563         }
10564       else if (gnu_vector_type_p (constructor_type))
10565         {
10566           tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10567
10568          /* Do a basic check of initializer size.  Note that vectors
10569             always have a fixed size derived from their type.  */
10570           if (tree_int_cst_lt (constructor_max_index, constructor_index))
10571             {
10572               pedwarn_init (loc, 0,
10573                             "excess elements in vector initializer");
10574               break;
10575             }
10576
10577           /* Now output the actual element.  */
10578           if (value.value)
10579             {
10580               if (TREE_CODE (value.value) == VECTOR_CST)
10581                 elttype = TYPE_MAIN_VARIANT (constructor_type);
10582               output_init_element (loc, value.value, value.original_type,
10583                                    strict_string, elttype,
10584                                    constructor_index, true, implicit,
10585                                    braced_init_obstack);
10586             }
10587
10588           constructor_index
10589             = size_binop_loc (input_location,
10590                               PLUS_EXPR, constructor_index, bitsize_one_node);
10591
10592           if (!value.value)
10593             /* If we are doing the bookkeeping for an element that was
10594                directly output as a constructor, we must update
10595                constructor_unfilled_index.  */
10596             constructor_unfilled_index = constructor_index;
10597         }
10598
10599       /* Handle the sole element allowed in a braced initializer
10600          for a scalar variable.  */
10601       else if (constructor_type != error_mark_node
10602                && constructor_fields == NULL_TREE)
10603         {
10604           pedwarn_init (loc, 0,
10605                         "excess elements in scalar initializer");
10606           break;
10607         }
10608       else
10609         {
10610           if (value.value)
10611             output_init_element (loc, value.value, value.original_type,
10612                                  strict_string, constructor_type,
10613                                  NULL_TREE, true, implicit,
10614                                  braced_init_obstack);
10615           constructor_fields = NULL_TREE;
10616         }
10617
10618       /* Handle range initializers either at this level or anywhere higher
10619          in the designator stack.  */
10620       if (constructor_range_stack)
10621         {
10622           struct constructor_range_stack *p, *range_stack;
10623           int finish = 0;
10624
10625           range_stack = constructor_range_stack;
10626           constructor_range_stack = 0;
10627           while (constructor_stack != range_stack->stack)
10628             {
10629               gcc_assert (constructor_stack->implicit);
10630               process_init_element (loc,
10631                                     pop_init_level (loc, 1,
10632                                                     braced_init_obstack,
10633                                                     last_init_list_comma),
10634                                     true, braced_init_obstack);
10635             }
10636           for (p = range_stack;
10637                !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10638                p = p->prev)
10639             {
10640               gcc_assert (constructor_stack->implicit);
10641               process_init_element (loc,
10642                                     pop_init_level (loc, 1,
10643                                                     braced_init_obstack,
10644                                                     last_init_list_comma),
10645                                     true, braced_init_obstack);
10646             }
10647
10648           p->index = size_binop_loc (input_location,
10649                                      PLUS_EXPR, p->index, bitsize_one_node);
10650           if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10651             finish = 1;
10652
10653           while (1)
10654             {
10655               constructor_index = p->index;
10656               constructor_fields = p->fields;
10657               if (finish && p->range_end && p->index == p->range_start)
10658                 {
10659                   finish = 0;
10660                   p->prev = 0;
10661                 }
10662               p = p->next;
10663               if (!p)
10664                 break;
10665               finish_implicit_inits (loc, braced_init_obstack);
10666               push_init_level (loc, 2, braced_init_obstack);
10667               p->stack = constructor_stack;
10668               if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10669                 p->index = p->range_start;
10670             }
10671
10672           if (!finish)
10673             constructor_range_stack = range_stack;
10674           continue;
10675         }
10676
10677       break;
10678     }
10679
10680   constructor_range_stack = 0;
10681 }
10682 \f
10683 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10684    (guaranteed to be 'volatile' or null) and ARGS (represented using
10685    an ASM_EXPR node).  */
10686 tree
10687 build_asm_stmt (bool is_volatile, tree args)
10688 {
10689   if (is_volatile)
10690     ASM_VOLATILE_P (args) = 1;
10691   return add_stmt (args);
10692 }
10693
10694 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10695    some INPUTS, and some CLOBBERS.  The latter three may be NULL.
10696    SIMPLE indicates whether there was anything at all after the
10697    string in the asm expression -- asm("blah") and asm("blah" : )
10698    are subtly different.  We use a ASM_EXPR node to represent this.
10699    LOC is the location of the asm, and IS_INLINE says whether this
10700    is asm inline.  */
10701 tree
10702 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10703                 tree clobbers, tree labels, bool simple, bool is_inline)
10704 {
10705   tree tail;
10706   tree args;
10707   int i;
10708   const char *constraint;
10709   const char **oconstraints;
10710   bool allows_mem, allows_reg, is_inout;
10711   int ninputs, noutputs;
10712
10713   ninputs = list_length (inputs);
10714   noutputs = list_length (outputs);
10715   oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10716
10717   string = resolve_asm_operand_names (string, outputs, inputs, labels);
10718
10719   /* Remove output conversions that change the type but not the mode.  */
10720   for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10721     {
10722       tree output = TREE_VALUE (tail);
10723
10724       output = c_fully_fold (output, false, NULL, true);
10725
10726       /* ??? Really, this should not be here.  Users should be using a
10727          proper lvalue, dammit.  But there's a long history of using casts
10728          in the output operands.  In cases like longlong.h, this becomes a
10729          primitive form of typechecking -- if the cast can be removed, then
10730          the output operand had a type of the proper width; otherwise we'll
10731          get an error.  Gross, but ...  */
10732       STRIP_NOPS (output);
10733
10734       if (!lvalue_or_else (loc, output, lv_asm))
10735         output = error_mark_node;
10736
10737       if (output != error_mark_node
10738           && (TREE_READONLY (output)
10739               || TYPE_READONLY (TREE_TYPE (output))
10740               || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10741                   && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10742         readonly_error (loc, output, lv_asm);
10743
10744       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10745       oconstraints[i] = constraint;
10746
10747       if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10748                                    &allows_mem, &allows_reg, &is_inout))
10749         {
10750           /* If the operand is going to end up in memory,
10751              mark it addressable.  */
10752           if (!allows_reg && !c_mark_addressable (output))
10753             output = error_mark_node;
10754           if (!(!allows_reg && allows_mem)
10755               && output != error_mark_node
10756               && VOID_TYPE_P (TREE_TYPE (output)))
10757             {
10758               error_at (loc, "invalid use of void expression");
10759               output = error_mark_node;
10760             }
10761         }
10762       else
10763         output = error_mark_node;
10764
10765       TREE_VALUE (tail) = output;
10766     }
10767
10768   for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10769     {
10770       tree input;
10771
10772       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10773       input = TREE_VALUE (tail);
10774
10775       if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10776                                   oconstraints, &allows_mem, &allows_reg))
10777         {
10778           /* If the operand is going to end up in memory,
10779              mark it addressable.  */
10780           if (!allows_reg && allows_mem)
10781             {
10782               input = c_fully_fold (input, false, NULL, true);
10783
10784               /* Strip the nops as we allow this case.  FIXME, this really
10785                  should be rejected or made deprecated.  */
10786               STRIP_NOPS (input);
10787               if (!c_mark_addressable (input))
10788                 input = error_mark_node;
10789             }
10790           else
10791             {
10792               struct c_expr expr;
10793               memset (&expr, 0, sizeof (expr));
10794               expr.value = input;
10795               expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10796               input = c_fully_fold (expr.value, false, NULL);
10797
10798               if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10799                 {
10800                   error_at (loc, "invalid use of void expression");
10801                   input = error_mark_node;
10802                 }
10803             }
10804         }
10805       else
10806         input = error_mark_node;
10807
10808       TREE_VALUE (tail) = input;
10809     }
10810
10811   args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10812
10813   /* asm statements without outputs, including simple ones, are treated
10814      as volatile.  */
10815   ASM_INPUT_P (args) = simple;
10816   ASM_VOLATILE_P (args) = (noutputs == 0);
10817   ASM_INLINE_P (args) = is_inline;
10818
10819   return args;
10820 }
10821 \f
10822 /* Generate a goto statement to LABEL.  LOC is the location of the
10823    GOTO.  */
10824
10825 tree
10826 c_finish_goto_label (location_t loc, tree label)
10827 {
10828   tree decl = lookup_label_for_goto (loc, label);
10829   if (!decl)
10830     return NULL_TREE;
10831   TREE_USED (decl) = 1;
10832   {
10833     add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10834     tree t = build1 (GOTO_EXPR, void_type_node, decl);
10835     SET_EXPR_LOCATION (t, loc);
10836     return add_stmt (t);
10837   }
10838 }
10839
10840 /* Generate a computed goto statement to EXPR.  LOC is the location of
10841    the GOTO.  */
10842
10843 tree
10844 c_finish_goto_ptr (location_t loc, c_expr val)
10845 {
10846   tree expr = val.value;
10847   tree t;
10848   pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10849   if (expr != error_mark_node
10850       && !POINTER_TYPE_P (TREE_TYPE (expr))
10851       && !null_pointer_constant_p (expr))
10852     {
10853       error_at (val.get_location (),
10854                 "computed goto must be pointer type");
10855       expr = build_zero_cst (ptr_type_node);
10856     }
10857   expr = c_fully_fold (expr, false, NULL);
10858   expr = convert (ptr_type_node, expr);
10859   t = build1 (GOTO_EXPR, void_type_node, expr);
10860   SET_EXPR_LOCATION (t, loc);
10861   return add_stmt (t);
10862 }
10863
10864 /* Generate a C `return' statement.  RETVAL is the expression for what
10865    to return, or a null pointer for `return;' with no value.  LOC is
10866    the location of the return statement, or the location of the expression,
10867    if the statement has any.  If ORIGTYPE is not NULL_TREE, it
10868    is the original type of RETVAL.  */
10869
10870 tree
10871 c_finish_return (location_t loc, tree retval, tree origtype)
10872 {
10873   tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10874   bool no_warning = false;
10875   bool npc = false;
10876
10877   /* Use the expansion point to handle cases such as returning NULL
10878      in a function returning void.  */
10879   location_t xloc = expansion_point_location_if_in_system_header (loc);
10880
10881   if (TREE_THIS_VOLATILE (current_function_decl))
10882     warning_at (xloc, 0,
10883                 "function declared %<noreturn%> has a %<return%> statement");
10884
10885   if (retval)
10886     {
10887       tree semantic_type = NULL_TREE;
10888       npc = null_pointer_constant_p (retval);
10889       if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10890         {
10891           semantic_type = TREE_TYPE (retval);
10892           retval = TREE_OPERAND (retval, 0);
10893         }
10894       retval = c_fully_fold (retval, false, NULL);
10895       if (semantic_type
10896           && valtype != NULL_TREE
10897           && TREE_CODE (valtype) != VOID_TYPE)
10898         retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10899     }
10900
10901   if (!retval)
10902     {
10903       current_function_returns_null = 1;
10904       if ((warn_return_type >= 0 || flag_isoc99)
10905           && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10906         {
10907           bool warned_here;
10908           if (flag_isoc99)
10909             warned_here = pedwarn
10910               (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10911                "%<return%> with no value, in function returning non-void");
10912           else
10913             warned_here = warning_at
10914               (loc, OPT_Wreturn_type,
10915                "%<return%> with no value, in function returning non-void");
10916           no_warning = true;
10917           if (warned_here)
10918             inform (DECL_SOURCE_LOCATION (current_function_decl),
10919                     "declared here");
10920         }
10921     }
10922   else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10923     {
10924       current_function_returns_null = 1;
10925       bool warned_here;
10926       if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10927         warned_here = pedwarn
10928           (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10929            "%<return%> with a value, in function returning void");
10930       else
10931         warned_here = pedwarn
10932           (xloc, OPT_Wpedantic, "ISO C forbids "
10933            "%<return%> with expression, in function returning void");
10934       if (warned_here)
10935         inform (DECL_SOURCE_LOCATION (current_function_decl),
10936                 "declared here");
10937     }
10938   else
10939     {
10940       tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10941                                        retval, origtype, ic_return,
10942                                        npc, NULL_TREE, NULL_TREE, 0);
10943       tree res = DECL_RESULT (current_function_decl);
10944       tree inner;
10945       bool save;
10946
10947       current_function_returns_value = 1;
10948       if (t == error_mark_node)
10949         return NULL_TREE;
10950
10951       save = in_late_binary_op;
10952       if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10953           || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10954           || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10955               && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10956                   || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10957               && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10958         in_late_binary_op = true;
10959       inner = t = convert (TREE_TYPE (res), t);
10960       in_late_binary_op = save;
10961
10962       /* Strip any conversions, additions, and subtractions, and see if
10963          we are returning the address of a local variable.  Warn if so.  */
10964       while (1)
10965         {
10966           switch (TREE_CODE (inner))
10967             {
10968             CASE_CONVERT:
10969             case NON_LVALUE_EXPR:
10970             case PLUS_EXPR:
10971             case POINTER_PLUS_EXPR:
10972               inner = TREE_OPERAND (inner, 0);
10973               continue;
10974
10975             case MINUS_EXPR:
10976               /* If the second operand of the MINUS_EXPR has a pointer
10977                  type (or is converted from it), this may be valid, so
10978                  don't give a warning.  */
10979               {
10980                 tree op1 = TREE_OPERAND (inner, 1);
10981
10982                 while (!POINTER_TYPE_P (TREE_TYPE (op1))
10983                        && (CONVERT_EXPR_P (op1)
10984                            || TREE_CODE (op1) == NON_LVALUE_EXPR))
10985                   op1 = TREE_OPERAND (op1, 0);
10986
10987                 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10988                   break;
10989
10990                 inner = TREE_OPERAND (inner, 0);
10991                 continue;
10992               }
10993
10994             case ADDR_EXPR:
10995               inner = TREE_OPERAND (inner, 0);
10996
10997               while (REFERENCE_CLASS_P (inner)
10998                      && !INDIRECT_REF_P (inner))
10999                 inner = TREE_OPERAND (inner, 0);
11000
11001               if (DECL_P (inner)
11002                   && !DECL_EXTERNAL (inner)
11003                   && !TREE_STATIC (inner)
11004                   && DECL_CONTEXT (inner) == current_function_decl
11005                   && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
11006                 {
11007                   if (TREE_CODE (inner) == LABEL_DECL)
11008                     warning_at (loc, OPT_Wreturn_local_addr,
11009                                 "function returns address of label");
11010                   else
11011                     {
11012                       warning_at (loc, OPT_Wreturn_local_addr,
11013                                   "function returns address of local variable");
11014                       tree zero = build_zero_cst (TREE_TYPE (res));
11015                       t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
11016                     }
11017                 }
11018               break;
11019
11020             default:
11021               break;
11022             }
11023
11024           break;
11025         }
11026
11027       retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
11028       SET_EXPR_LOCATION (retval, loc);
11029
11030       if (warn_sequence_point)
11031         verify_sequence_points (retval);
11032     }
11033
11034   ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
11035   if (no_warning)
11036     suppress_warning (ret_stmt, OPT_Wreturn_type);
11037   return add_stmt (ret_stmt);
11038 }
11039 \f
11040 struct c_switch {
11041   /* The SWITCH_STMT being built.  */
11042   tree switch_stmt;
11043
11044   /* The original type of the testing expression, i.e. before the
11045      default conversion is applied.  */
11046   tree orig_type;
11047
11048   /* A splay-tree mapping the low element of a case range to the high
11049      element, or NULL_TREE if there is no high element.  Used to
11050      determine whether or not a new case label duplicates an old case
11051      label.  We need a tree, rather than simply a hash table, because
11052      of the GNU case range extension.  */
11053   splay_tree cases;
11054
11055   /* The bindings at the point of the switch.  This is used for
11056      warnings crossing decls when branching to a case label.  */
11057   struct c_spot_bindings *bindings;
11058
11059   /* Whether the switch includes any break statements.  */
11060   bool break_stmt_seen_p;
11061
11062   /* The next node on the stack.  */
11063   struct c_switch *next;
11064
11065   /* Remember whether the controlling expression had boolean type
11066      before integer promotions for the sake of -Wswitch-bool.  */
11067   bool bool_cond_p;
11068 };
11069
11070 /* A stack of the currently active switch statements.  The innermost
11071    switch statement is on the top of the stack.  There is no need to
11072    mark the stack for garbage collection because it is only active
11073    during the processing of the body of a function, and we never
11074    collect at that point.  */
11075
11076 struct c_switch *c_switch_stack;
11077
11078 /* Start a C switch statement, testing expression EXP.  Return the new
11079    SWITCH_STMT.  SWITCH_LOC is the location of the `switch'.
11080    SWITCH_COND_LOC is the location of the switch's condition.
11081    EXPLICIT_CAST_P is true if the expression EXP has an explicit cast.  */
11082
11083 tree
11084 c_start_switch (location_t switch_loc,
11085                 location_t switch_cond_loc,
11086                 tree exp, bool explicit_cast_p)
11087 {
11088   tree orig_type = error_mark_node;
11089   bool bool_cond_p = false;
11090   struct c_switch *cs;
11091
11092   if (exp != error_mark_node)
11093     {
11094       orig_type = TREE_TYPE (exp);
11095
11096       if (!INTEGRAL_TYPE_P (orig_type))
11097         {
11098           if (orig_type != error_mark_node)
11099             {
11100               error_at (switch_cond_loc, "switch quantity not an integer");
11101               orig_type = error_mark_node;
11102             }
11103           exp = integer_zero_node;
11104         }
11105       else
11106         {
11107           tree type = TYPE_MAIN_VARIANT (orig_type);
11108           tree e = exp;
11109
11110           /* Warn if the condition has boolean value.  */
11111           while (TREE_CODE (e) == COMPOUND_EXPR)
11112             e = TREE_OPERAND (e, 1);
11113
11114           if ((TREE_CODE (type) == BOOLEAN_TYPE
11115                || truth_value_p (TREE_CODE (e)))
11116               /* Explicit cast to int suppresses this warning.  */
11117               && !(TREE_CODE (type) == INTEGER_TYPE
11118                    && explicit_cast_p))
11119             bool_cond_p = true;
11120
11121           if (!in_system_header_at (input_location)
11122               && (type == long_integer_type_node
11123                   || type == long_unsigned_type_node))
11124             warning_at (switch_cond_loc,
11125                         OPT_Wtraditional, "%<long%> switch expression not "
11126                         "converted to %<int%> in ISO C");
11127
11128           exp = c_fully_fold (exp, false, NULL);
11129           exp = default_conversion (exp);
11130
11131           if (warn_sequence_point)
11132             verify_sequence_points (exp);
11133         }
11134     }
11135
11136   /* Add this new SWITCH_STMT to the stack.  */
11137   cs = XNEW (struct c_switch);
11138   cs->switch_stmt = build_stmt (switch_loc, SWITCH_STMT, exp,
11139                                 NULL_TREE, orig_type, NULL_TREE);
11140   cs->orig_type = orig_type;
11141   cs->cases = splay_tree_new (case_compare, NULL, NULL);
11142   cs->bindings = c_get_switch_bindings ();
11143   cs->break_stmt_seen_p = false;
11144   cs->bool_cond_p = bool_cond_p;
11145   cs->next = c_switch_stack;
11146   c_switch_stack = cs;
11147
11148   return add_stmt (cs->switch_stmt);
11149 }
11150
11151 /* Process a case label at location LOC.  */
11152
11153 tree
11154 do_case (location_t loc, tree low_value, tree high_value)
11155 {
11156   tree label = NULL_TREE;
11157
11158   if (low_value && TREE_CODE (low_value) != INTEGER_CST)
11159     {
11160       low_value = c_fully_fold (low_value, false, NULL);
11161       if (TREE_CODE (low_value) == INTEGER_CST)
11162         pedwarn (loc, OPT_Wpedantic,
11163                  "case label is not an integer constant expression");
11164     }
11165
11166   if (high_value && TREE_CODE (high_value) != INTEGER_CST)
11167     {
11168       high_value = c_fully_fold (high_value, false, NULL);
11169       if (TREE_CODE (high_value) == INTEGER_CST)
11170         pedwarn (input_location, OPT_Wpedantic,
11171                  "case label is not an integer constant expression");
11172     }
11173
11174   if (c_switch_stack == NULL)
11175     {
11176       if (low_value)
11177         error_at (loc, "case label not within a switch statement");
11178       else
11179         error_at (loc, "%<default%> label not within a switch statement");
11180       return NULL_TREE;
11181     }
11182
11183   if (c_check_switch_jump_warnings (c_switch_stack->bindings,
11184                                     EXPR_LOCATION (c_switch_stack->switch_stmt),
11185                                     loc))
11186     return NULL_TREE;
11187
11188   label = c_add_case_label (loc, c_switch_stack->cases,
11189                             SWITCH_STMT_COND (c_switch_stack->switch_stmt),
11190                             low_value, high_value);
11191   if (label == error_mark_node)
11192     label = NULL_TREE;
11193   return label;
11194 }
11195
11196 /* Finish the switch statement.  TYPE is the original type of the
11197    controlling expression of the switch, or NULL_TREE.  */
11198
11199 void
11200 c_finish_switch (tree body, tree type)
11201 {
11202   struct c_switch *cs = c_switch_stack;
11203   location_t switch_location;
11204
11205   SWITCH_STMT_BODY (cs->switch_stmt) = body;
11206
11207   /* Emit warnings as needed.  */
11208   switch_location = EXPR_LOCATION (cs->switch_stmt);
11209   c_do_switch_warnings (cs->cases, switch_location,
11210                         type ? type : SWITCH_STMT_TYPE (cs->switch_stmt),
11211                         SWITCH_STMT_COND (cs->switch_stmt), cs->bool_cond_p);
11212   if (c_switch_covers_all_cases_p (cs->cases,
11213                                    SWITCH_STMT_TYPE (cs->switch_stmt)))
11214     SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
11215   SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
11216
11217   /* Pop the stack.  */
11218   c_switch_stack = cs->next;
11219   splay_tree_delete (cs->cases);
11220   c_release_switch_bindings (cs->bindings);
11221   XDELETE (cs);
11222 }
11223 \f
11224 /* Emit an if statement.  IF_LOCUS is the location of the 'if'.  COND,
11225    THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11226    may be null.  */
11227
11228 void
11229 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11230                   tree else_block)
11231 {
11232   tree stmt;
11233
11234   stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11235   SET_EXPR_LOCATION (stmt, if_locus);
11236   add_stmt (stmt);
11237 }
11238
11239 tree
11240 c_finish_bc_stmt (location_t loc, tree label, bool is_break)
11241 {
11242   /* In switch statements break is sometimes stylistically used after
11243      a return statement.  This can lead to spurious warnings about
11244      control reaching the end of a non-void function when it is
11245      inlined.  Note that we are calling block_may_fallthru with
11246      language specific tree nodes; this works because
11247      block_may_fallthru returns true when given something it does not
11248      understand.  */
11249   bool skip = !block_may_fallthru (cur_stmt_list);
11250
11251   if (is_break)
11252     switch (in_statement)
11253       {
11254       case 0:
11255         error_at (loc, "break statement not within loop or switch");
11256         return NULL_TREE;
11257       case IN_OMP_BLOCK:
11258         error_at (loc, "invalid exit from OpenMP structured block");
11259         return NULL_TREE;
11260       case IN_OMP_FOR:
11261         error_at (loc, "break statement used with OpenMP for loop");
11262         return NULL_TREE;
11263       case IN_ITERATION_STMT:
11264       case IN_OBJC_FOREACH:
11265         break;
11266       default:
11267         gcc_assert (in_statement & IN_SWITCH_STMT);
11268         c_switch_stack->break_stmt_seen_p = true;
11269         break;
11270       }
11271   else
11272     switch (in_statement & ~IN_SWITCH_STMT)
11273       {
11274       case 0:
11275         error_at (loc, "continue statement not within a loop");
11276         return NULL_TREE;
11277       case IN_OMP_BLOCK:
11278         error_at (loc, "invalid exit from OpenMP structured block");
11279         return NULL_TREE;
11280       case IN_ITERATION_STMT:
11281       case IN_OMP_FOR:
11282       case IN_OBJC_FOREACH:
11283         break;
11284       default:
11285         gcc_unreachable ();
11286       }
11287
11288   if (skip)
11289     return NULL_TREE;
11290   else if ((in_statement & IN_OBJC_FOREACH)
11291            && !(is_break && (in_statement & IN_SWITCH_STMT)))
11292     {
11293       /* The foreach expander produces low-level code using gotos instead
11294          of a structured loop construct.  */
11295       gcc_assert (label);
11296       return add_stmt (build_stmt (loc, GOTO_EXPR, label));
11297     }
11298   return add_stmt (build_stmt (loc, (is_break ? BREAK_STMT : CONTINUE_STMT)));
11299 }
11300
11301 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr.  */
11302
11303 static void
11304 emit_side_effect_warnings (location_t loc, tree expr)
11305 {
11306   maybe_warn_nodiscard (loc, expr);
11307   if (!warn_unused_value)
11308     return;
11309   if (expr == error_mark_node)
11310     ;
11311   else if (!TREE_SIDE_EFFECTS (expr))
11312     {
11313       if (!VOID_TYPE_P (TREE_TYPE (expr))
11314           && !warning_suppressed_p (expr, OPT_Wunused_value))
11315         warning_at (loc, OPT_Wunused_value, "statement with no effect");
11316     }
11317   else if (TREE_CODE (expr) == COMPOUND_EXPR)
11318     {
11319       tree r = expr;
11320       location_t cloc = loc;
11321       while (TREE_CODE (r) == COMPOUND_EXPR)
11322         {
11323           if (EXPR_HAS_LOCATION (r))
11324             cloc = EXPR_LOCATION (r);
11325           r = TREE_OPERAND (r, 1);
11326         }
11327       if (!TREE_SIDE_EFFECTS (r)
11328           && !VOID_TYPE_P (TREE_TYPE (r))
11329           && !CONVERT_EXPR_P (r)
11330           && !warning_suppressed_p (r, OPT_Wunused_value)
11331           && !warning_suppressed_p (expr, OPT_Wunused_value))
11332         warning_at (cloc, OPT_Wunused_value,
11333                     "right-hand operand of comma expression has no effect");
11334     }
11335   else
11336     warn_if_unused_value (expr, loc);
11337 }
11338
11339 /* Process an expression as if it were a complete statement.  Emit
11340    diagnostics, but do not call ADD_STMT.  LOC is the location of the
11341    statement.  */
11342
11343 tree
11344 c_process_expr_stmt (location_t loc, tree expr)
11345 {
11346   tree exprv;
11347
11348   if (!expr)
11349     return NULL_TREE;
11350
11351   expr = c_fully_fold (expr, false, NULL);
11352
11353   if (warn_sequence_point)
11354     verify_sequence_points (expr);
11355
11356   if (TREE_TYPE (expr) != error_mark_node
11357       && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11358       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11359     error_at (loc, "expression statement has incomplete type");
11360
11361   /* If we're not processing a statement expression, warn about unused values.
11362      Warnings for statement expressions will be emitted later, once we figure
11363      out which is the result.  */
11364   if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11365       && (warn_unused_value || warn_unused_result))
11366     emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11367
11368   exprv = expr;
11369   while (TREE_CODE (exprv) == COMPOUND_EXPR)
11370     exprv = TREE_OPERAND (exprv, 1);
11371   while (CONVERT_EXPR_P (exprv))
11372     exprv = TREE_OPERAND (exprv, 0);
11373   if (DECL_P (exprv)
11374       || handled_component_p (exprv)
11375       || TREE_CODE (exprv) == ADDR_EXPR)
11376     mark_exp_read (exprv);
11377
11378   /* If the expression is not of a type to which we cannot assign a line
11379      number, wrap the thing in a no-op NOP_EXPR.  */
11380   if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11381     {
11382       expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11383       SET_EXPR_LOCATION (expr, loc);
11384     }
11385
11386   return expr;
11387 }
11388
11389 /* Emit an expression as a statement.  LOC is the location of the
11390    expression.  */
11391
11392 tree
11393 c_finish_expr_stmt (location_t loc, tree expr)
11394 {
11395   if (expr)
11396     return add_stmt (c_process_expr_stmt (loc, expr));
11397   else
11398     return NULL;
11399 }
11400
11401 /* Do the opposite and emit a statement as an expression.  To begin,
11402    create a new binding level and return it.  */
11403
11404 tree
11405 c_begin_stmt_expr (void)
11406 {
11407   tree ret;
11408
11409   /* We must force a BLOCK for this level so that, if it is not expanded
11410      later, there is a way to turn off the entire subtree of blocks that
11411      are contained in it.  */
11412   keep_next_level ();
11413   ret = c_begin_compound_stmt (true);
11414
11415   c_bindings_start_stmt_expr (c_switch_stack == NULL
11416                               ? NULL
11417                               : c_switch_stack->bindings);
11418
11419   /* Mark the current statement list as belonging to a statement list.  */
11420   STATEMENT_LIST_STMT_EXPR (ret) = 1;
11421
11422   return ret;
11423 }
11424
11425 /* LOC is the location of the compound statement to which this body
11426    belongs.  */
11427
11428 tree
11429 c_finish_stmt_expr (location_t loc, tree body)
11430 {
11431   tree last, type, tmp, val;
11432   tree *last_p;
11433
11434   body = c_end_compound_stmt (loc, body, true);
11435
11436   c_bindings_end_stmt_expr (c_switch_stack == NULL
11437                             ? NULL
11438                             : c_switch_stack->bindings);
11439
11440   /* Locate the last statement in BODY.  See c_end_compound_stmt
11441      about always returning a BIND_EXPR.  */
11442   last_p = &BIND_EXPR_BODY (body);
11443   last = BIND_EXPR_BODY (body);
11444
11445  continue_searching:
11446   if (TREE_CODE (last) == STATEMENT_LIST)
11447     {
11448       tree_stmt_iterator l = tsi_last (last);
11449
11450       while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11451         tsi_prev (&l);
11452
11453       /* This can happen with degenerate cases like ({ }).  No value.  */
11454       if (tsi_end_p (l))
11455         return body;
11456
11457       /* If we're supposed to generate side effects warnings, process
11458          all of the statements except the last.  */
11459       if (warn_unused_value || warn_unused_result)
11460         {
11461           for (tree_stmt_iterator i = tsi_start (last);
11462                tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11463             {
11464               location_t tloc;
11465               tree t = tsi_stmt (i);
11466
11467               tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11468               emit_side_effect_warnings (tloc, t);
11469             }
11470         }
11471       last_p = tsi_stmt_ptr (l);
11472       last = *last_p;
11473     }
11474
11475   /* If the end of the list is exception related, then the list was split
11476      by a call to push_cleanup.  Continue searching.  */
11477   if (TREE_CODE (last) == TRY_FINALLY_EXPR
11478       || TREE_CODE (last) == TRY_CATCH_EXPR)
11479     {
11480       last_p = &TREE_OPERAND (last, 0);
11481       last = *last_p;
11482       goto continue_searching;
11483     }
11484
11485   if (last == error_mark_node)
11486     return last;
11487
11488   /* In the case that the BIND_EXPR is not necessary, return the
11489      expression out from inside it.  */
11490   if ((last == BIND_EXPR_BODY (body)
11491        /* Skip nested debug stmts.  */
11492        || last == expr_first (BIND_EXPR_BODY (body)))
11493       && BIND_EXPR_VARS (body) == NULL)
11494     {
11495       /* Even if this looks constant, do not allow it in a constant
11496          expression.  */
11497       last = c_wrap_maybe_const (last, true);
11498       /* Do not warn if the return value of a statement expression is
11499          unused.  */
11500       suppress_warning (last, OPT_Wunused);
11501       return last;
11502     }
11503
11504   /* Extract the type of said expression.  */
11505   type = TREE_TYPE (last);
11506
11507   /* If we're not returning a value at all, then the BIND_EXPR that
11508      we already have is a fine expression to return.  */
11509   if (!type || VOID_TYPE_P (type))
11510     return body;
11511
11512   /* Now that we've located the expression containing the value, it seems
11513      silly to make voidify_wrapper_expr repeat the process.  Create a
11514      temporary of the appropriate type and stick it in a TARGET_EXPR.  */
11515   tmp = create_tmp_var_raw (type);
11516
11517   /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt.  This avoids
11518      tree_expr_nonnegative_p giving up immediately.  */
11519   val = last;
11520   if (TREE_CODE (val) == NOP_EXPR
11521       && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11522     val = TREE_OPERAND (val, 0);
11523
11524   *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11525   SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11526
11527   {
11528     tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11529     SET_EXPR_LOCATION (t, loc);
11530     return t;
11531   }
11532 }
11533 \f
11534 /* Begin and end compound statements.  This is as simple as pushing
11535    and popping new statement lists from the tree.  */
11536
11537 tree
11538 c_begin_compound_stmt (bool do_scope)
11539 {
11540   tree stmt = push_stmt_list ();
11541   if (do_scope)
11542     push_scope ();
11543   return stmt;
11544 }
11545
11546 /* End a compound statement.  STMT is the statement.  LOC is the
11547    location of the compound statement-- this is usually the location
11548    of the opening brace.  */
11549
11550 tree
11551 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11552 {
11553   tree block = NULL;
11554
11555   if (do_scope)
11556     {
11557       if (c_dialect_objc ())
11558         objc_clear_super_receiver ();
11559       block = pop_scope ();
11560     }
11561
11562   stmt = pop_stmt_list (stmt);
11563   stmt = c_build_bind_expr (loc, block, stmt);
11564
11565   /* If this compound statement is nested immediately inside a statement
11566      expression, then force a BIND_EXPR to be created.  Otherwise we'll
11567      do the wrong thing for ({ { 1; } }) or ({ 1; { } }).  In particular,
11568      STATEMENT_LISTs merge, and thus we can lose track of what statement
11569      was really last.  */
11570   if (building_stmt_list_p ()
11571       && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11572       && TREE_CODE (stmt) != BIND_EXPR)
11573     {
11574       stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11575       TREE_SIDE_EFFECTS (stmt) = 1;
11576       SET_EXPR_LOCATION (stmt, loc);
11577     }
11578
11579   return stmt;
11580 }
11581
11582 /* Queue a cleanup.  CLEANUP is an expression/statement to be executed
11583    when the current scope is exited.  EH_ONLY is true when this is not
11584    meant to apply to normal control flow transfer.  */
11585
11586 void
11587 push_cleanup (tree decl, tree cleanup, bool eh_only)
11588 {
11589   enum tree_code code;
11590   tree stmt, list;
11591   bool stmt_expr;
11592
11593   code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11594   stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11595   add_stmt (stmt);
11596   stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11597   list = push_stmt_list ();
11598   TREE_OPERAND (stmt, 0) = list;
11599   STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11600 }
11601 \f
11602 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11603    into a value of TYPE type.  Comparison is done via VEC_COND_EXPR.  */
11604
11605 static tree
11606 build_vec_cmp (tree_code code, tree type,
11607                tree arg0, tree arg1)
11608 {
11609   tree zero_vec = build_zero_cst (type);
11610   tree minus_one_vec = build_minus_one_cst (type);
11611   tree cmp_type = truth_type_for (type);
11612   tree cmp = build2 (code, cmp_type, arg0, arg1);
11613   return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11614 }
11615
11616 /* Possibly warn about an address of OP never being NULL in a comparison
11617    operation CODE involving null.  */
11618
11619 static void
11620 maybe_warn_for_null_address (location_t loc, tree op, tree_code code)
11621 {
11622   /* Prevent warnings issued for macro expansion.  */
11623   if (!warn_address
11624       || warning_suppressed_p (op, OPT_Waddress)
11625       || from_macro_expansion_at (loc))
11626     return;
11627
11628   if (TREE_CODE (op) == NOP_EXPR)
11629     {
11630       /* Allow casts to intptr_t to suppress the warning.  */
11631       tree type = TREE_TYPE (op);
11632       if (TREE_CODE (type) == INTEGER_TYPE)
11633         return;
11634       op = TREE_OPERAND (op, 0);
11635     }
11636
11637   if (TREE_CODE (op) == POINTER_PLUS_EXPR)
11638     {
11639       /* Allow a cast to void* to suppress the warning.  */
11640       tree type = TREE_TYPE (TREE_TYPE (op));
11641       if (VOID_TYPE_P (type))
11642         return;
11643
11644       /* Adding any value to a null pointer, including zero, is undefined
11645          in C.  This includes the expression &p[0] where p is the null
11646          pointer, although &p[0] will have been folded to p by this point
11647          and so not diagnosed.  */
11648       if (code == EQ_EXPR)
11649         warning_at (loc, OPT_Waddress,
11650                     "the comparison will always evaluate as %<false%> "
11651                     "for the pointer operand in %qE must not be NULL",
11652                     op);
11653       else
11654         warning_at (loc, OPT_Waddress,
11655                     "the comparison will always evaluate as %<true%> "
11656                     "for the pointer operand in %qE must not be NULL",
11657                     op);
11658
11659       return;
11660     }
11661
11662   if (TREE_CODE (op) != ADDR_EXPR)
11663     return;
11664
11665   op = TREE_OPERAND (op, 0);
11666
11667   if (TREE_CODE (op) == IMAGPART_EXPR
11668       || TREE_CODE (op) == REALPART_EXPR)
11669     {
11670       /* The address of either complex part may not be null.  */
11671       if (code == EQ_EXPR)
11672         warning_at (loc, OPT_Waddress,
11673                     "the comparison will always evaluate as %<false%> "
11674                     "for the address of %qE will never be NULL",
11675                     op);
11676       else
11677         warning_at (loc, OPT_Waddress,
11678                     "the comparison will always evaluate as %<true%> "
11679                     "for the address of %qE will never be NULL",
11680                     op);
11681       return;
11682     }
11683
11684   /* Set to true in the loop below if OP dereferences is operand.
11685      In such a case the ultimate target need not be a decl for
11686      the null [in]equality test to be constant.  */
11687   bool deref = false;
11688
11689   /* Get the outermost array or object, or member.  */
11690   while (handled_component_p (op))
11691     {
11692       if (TREE_CODE (op) == COMPONENT_REF)
11693         {
11694           /* Get the member (its address is never null).  */
11695           op = TREE_OPERAND (op, 1);
11696           break;
11697         }
11698
11699       /* Get the outer array/object to refer to in the warning.  */
11700       op = TREE_OPERAND (op, 0);
11701       deref = true;
11702     }
11703
11704   if ((!deref && !decl_with_nonnull_addr_p (op))
11705       || from_macro_expansion_at (loc))
11706     return;
11707
11708   if (code == EQ_EXPR)
11709     warning_at (loc, OPT_Waddress,
11710                 "the comparison will always evaluate as %<false%> "
11711                 "for the address of %qE will never be NULL",
11712                 op);
11713   else
11714     warning_at (loc, OPT_Waddress,
11715                 "the comparison will always evaluate as %<true%> "
11716                 "for the address of %qE will never be NULL",
11717                 op);
11718
11719   if (DECL_P (op))
11720     inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
11721 }
11722
11723 /* Build a binary-operation expression without default conversions.
11724    CODE is the kind of expression to build.
11725    LOCATION is the operator's location.
11726    This function differs from `build' in several ways:
11727    the data type of the result is computed and recorded in it,
11728    warnings are generated if arg data types are invalid,
11729    special handling for addition and subtraction of pointers is known,
11730    and some optimization is done (operations on narrow ints
11731    are done in the narrower type when that gives the same result).
11732    Constant folding is also done before the result is returned.
11733
11734    Note that the operands will never have enumeral types, or function
11735    or array types, because either they will have the default conversions
11736    performed or they have both just been converted to some other type in which
11737    the arithmetic is to be done.  */
11738
11739 tree
11740 build_binary_op (location_t location, enum tree_code code,
11741                  tree orig_op0, tree orig_op1, bool convert_p)
11742 {
11743   tree type0, type1, orig_type0, orig_type1;
11744   tree eptype;
11745   enum tree_code code0, code1;
11746   tree op0, op1;
11747   tree ret = error_mark_node;
11748   const char *invalid_op_diag;
11749   bool op0_int_operands, op1_int_operands;
11750   bool int_const, int_const_or_overflow, int_operands;
11751
11752   /* Expression code to give to the expression when it is built.
11753      Normally this is CODE, which is what the caller asked for,
11754      but in some special cases we change it.  */
11755   enum tree_code resultcode = code;
11756
11757   /* Data type in which the computation is to be performed.
11758      In the simplest cases this is the common type of the arguments.  */
11759   tree result_type = NULL;
11760
11761   /* When the computation is in excess precision, the type of the
11762      final EXCESS_PRECISION_EXPR.  */
11763   tree semantic_result_type = NULL;
11764
11765   /* Nonzero means operands have already been type-converted
11766      in whatever way is necessary.
11767      Zero means they need to be converted to RESULT_TYPE.  */
11768   int converted = 0;
11769
11770   /* Nonzero means create the expression with this type, rather than
11771      RESULT_TYPE.  */
11772   tree build_type = NULL_TREE;
11773
11774   /* Nonzero means after finally constructing the expression
11775      convert it to this type.  */
11776   tree final_type = NULL_TREE;
11777
11778   /* Nonzero if this is an operation like MIN or MAX which can
11779      safely be computed in short if both args are promoted shorts.
11780      Also implies COMMON.
11781      -1 indicates a bitwise operation; this makes a difference
11782      in the exact conditions for when it is safe to do the operation
11783      in a narrower mode.  */
11784   int shorten = 0;
11785
11786   /* Nonzero if this is a comparison operation;
11787      if both args are promoted shorts, compare the original shorts.
11788      Also implies COMMON.  */
11789   int short_compare = 0;
11790
11791   /* Nonzero if this is a right-shift operation, which can be computed on the
11792      original short and then promoted if the operand is a promoted short.  */
11793   int short_shift = 0;
11794
11795   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
11796   int common = 0;
11797
11798   /* True means types are compatible as far as ObjC is concerned.  */
11799   bool objc_ok;
11800
11801   /* True means this is an arithmetic operation that may need excess
11802      precision.  */
11803   bool may_need_excess_precision;
11804
11805   /* True means this is a boolean operation that converts both its
11806      operands to truth-values.  */
11807   bool boolean_op = false;
11808
11809   /* Remember whether we're doing / or %.  */
11810   bool doing_div_or_mod = false;
11811
11812   /* Remember whether we're doing << or >>.  */
11813   bool doing_shift = false;
11814
11815   /* Tree holding instrumentation expression.  */
11816   tree instrument_expr = NULL;
11817
11818   if (location == UNKNOWN_LOCATION)
11819     location = input_location;
11820
11821   op0 = orig_op0;
11822   op1 = orig_op1;
11823
11824   op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11825   if (op0_int_operands)
11826     op0 = remove_c_maybe_const_expr (op0);
11827   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11828   if (op1_int_operands)
11829     op1 = remove_c_maybe_const_expr (op1);
11830   int_operands = (op0_int_operands && op1_int_operands);
11831   if (int_operands)
11832     {
11833       int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11834                                && TREE_CODE (orig_op1) == INTEGER_CST);
11835       int_const = (int_const_or_overflow
11836                    && !TREE_OVERFLOW (orig_op0)
11837                    && !TREE_OVERFLOW (orig_op1));
11838     }
11839   else
11840     int_const = int_const_or_overflow = false;
11841
11842   /* Do not apply default conversion in mixed vector/scalar expression.  */
11843   if (convert_p
11844       && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11845     {
11846       op0 = default_conversion (op0);
11847       op1 = default_conversion (op1);
11848     }
11849
11850   orig_type0 = type0 = TREE_TYPE (op0);
11851
11852   orig_type1 = type1 = TREE_TYPE (op1);
11853
11854   /* The expression codes of the data types of the arguments tell us
11855      whether the arguments are integers, floating, pointers, etc.  */
11856   code0 = TREE_CODE (type0);
11857   code1 = TREE_CODE (type1);
11858
11859   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
11860   STRIP_TYPE_NOPS (op0);
11861   STRIP_TYPE_NOPS (op1);
11862
11863   /* If an error was already reported for one of the arguments,
11864      avoid reporting another error.  */
11865
11866   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11867     return error_mark_node;
11868
11869   if (code0 == POINTER_TYPE
11870       && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11871     return error_mark_node;
11872
11873   if (code1 == POINTER_TYPE
11874       && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11875     return error_mark_node;
11876
11877   if ((invalid_op_diag
11878        = targetm.invalid_binary_op (code, type0, type1)))
11879     {
11880       error_at (location, invalid_op_diag);
11881       return error_mark_node;
11882     }
11883
11884   switch (code)
11885     {
11886     case PLUS_EXPR:
11887     case MINUS_EXPR:
11888     case MULT_EXPR:
11889     case TRUNC_DIV_EXPR:
11890     case CEIL_DIV_EXPR:
11891     case FLOOR_DIV_EXPR:
11892     case ROUND_DIV_EXPR:
11893     case EXACT_DIV_EXPR:
11894       may_need_excess_precision = true;
11895       break;
11896
11897     case EQ_EXPR:
11898     case NE_EXPR:
11899     case LE_EXPR:
11900     case GE_EXPR:
11901     case LT_EXPR:
11902     case GT_EXPR:
11903       /* Excess precision for implicit conversions of integers to
11904          floating point in C11 and later.  */
11905       may_need_excess_precision = (flag_isoc11
11906                                    && (ANY_INTEGRAL_TYPE_P (type0)
11907                                        || ANY_INTEGRAL_TYPE_P (type1)));
11908       break;
11909
11910     default:
11911       may_need_excess_precision = false;
11912       break;
11913     }
11914   if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11915     {
11916       op0 = TREE_OPERAND (op0, 0);
11917       type0 = TREE_TYPE (op0);
11918     }
11919   else if (may_need_excess_precision
11920            && (eptype = excess_precision_type (type0)) != NULL_TREE)
11921     {
11922       type0 = eptype;
11923       op0 = convert (eptype, op0);
11924     }
11925   if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11926     {
11927       op1 = TREE_OPERAND (op1, 0);
11928       type1 = TREE_TYPE (op1);
11929     }
11930   else if (may_need_excess_precision
11931            && (eptype = excess_precision_type (type1)) != NULL_TREE)
11932     {
11933       type1 = eptype;
11934       op1 = convert (eptype, op1);
11935     }
11936
11937   objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11938
11939   /* In case when one of the operands of the binary operation is
11940      a vector and another is a scalar -- convert scalar to vector.  */
11941   if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11942       || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11943     {
11944       enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11945                                                      true);
11946
11947       switch (convert_flag)
11948         {
11949           case stv_error:
11950             return error_mark_node;
11951           case stv_firstarg:
11952             {
11953               bool maybe_const = true;
11954               tree sc;
11955               sc = c_fully_fold (op0, false, &maybe_const);
11956               sc = save_expr (sc);
11957               sc = convert (TREE_TYPE (type1), sc);
11958               op0 = build_vector_from_val (type1, sc);
11959               if (!maybe_const)
11960                 op0 = c_wrap_maybe_const (op0, true);
11961               orig_type0 = type0 = TREE_TYPE (op0);
11962               code0 = TREE_CODE (type0);
11963               converted = 1;
11964               break;
11965             }
11966           case stv_secondarg:
11967             {
11968               bool maybe_const = true;
11969               tree sc;
11970               sc = c_fully_fold (op1, false, &maybe_const);
11971               sc = save_expr (sc);
11972               sc = convert (TREE_TYPE (type0), sc);
11973               op1 = build_vector_from_val (type0, sc);
11974               if (!maybe_const)
11975                 op1 = c_wrap_maybe_const (op1, true);
11976               orig_type1 = type1 = TREE_TYPE (op1);
11977               code1 = TREE_CODE (type1);
11978               converted = 1;
11979               break;
11980             }
11981           default:
11982             break;
11983         }
11984     }
11985
11986   switch (code)
11987     {
11988     case PLUS_EXPR:
11989       /* Handle the pointer + int case.  */
11990       if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11991         {
11992           ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11993           goto return_build_binary_op;
11994         }
11995       else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11996         {
11997           ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11998           goto return_build_binary_op;
11999         }
12000       else
12001         common = 1;
12002       break;
12003
12004     case MINUS_EXPR:
12005       /* Subtraction of two similar pointers.
12006          We must subtract them as integers, then divide by object size.  */
12007       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
12008           && comp_target_types (location, type0, type1))
12009         {
12010           ret = pointer_diff (location, op0, op1, &instrument_expr);
12011           goto return_build_binary_op;
12012         }
12013       /* Handle pointer minus int.  Just like pointer plus int.  */
12014       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12015         {
12016           ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
12017           goto return_build_binary_op;
12018         }
12019       else
12020         common = 1;
12021       break;
12022
12023     case MULT_EXPR:
12024       common = 1;
12025       break;
12026
12027     case TRUNC_DIV_EXPR:
12028     case CEIL_DIV_EXPR:
12029     case FLOOR_DIV_EXPR:
12030     case ROUND_DIV_EXPR:
12031     case EXACT_DIV_EXPR:
12032       doing_div_or_mod = true;
12033       warn_for_div_by_zero (location, op1);
12034
12035       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12036            || code0 == FIXED_POINT_TYPE
12037            || code0 == COMPLEX_TYPE
12038            || gnu_vector_type_p (type0))
12039           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12040               || code1 == FIXED_POINT_TYPE
12041               || code1 == COMPLEX_TYPE
12042               || gnu_vector_type_p (type1)))
12043         {
12044           enum tree_code tcode0 = code0, tcode1 = code1;
12045
12046           if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
12047             tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
12048           if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
12049             tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
12050
12051           if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
12052               || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
12053             resultcode = RDIV_EXPR;
12054           else
12055             /* Although it would be tempting to shorten always here, that
12056                loses on some targets, since the modulo instruction is
12057                undefined if the quotient can't be represented in the
12058                computation mode.  We shorten only if unsigned or if
12059                dividing by something we know != -1.  */
12060             shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12061                        || (TREE_CODE (op1) == INTEGER_CST
12062                            && !integer_all_onesp (op1)));
12063           common = 1;
12064         }
12065       break;
12066
12067     case BIT_AND_EXPR:
12068     case BIT_IOR_EXPR:
12069     case BIT_XOR_EXPR:
12070       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12071         shorten = -1;
12072       /* Allow vector types which are not floating point types.   */
12073       else if (gnu_vector_type_p (type0)
12074                && gnu_vector_type_p (type1)
12075                && !VECTOR_FLOAT_TYPE_P (type0)
12076                && !VECTOR_FLOAT_TYPE_P (type1))
12077         common = 1;
12078       break;
12079
12080     case TRUNC_MOD_EXPR:
12081     case FLOOR_MOD_EXPR:
12082       doing_div_or_mod = true;
12083       warn_for_div_by_zero (location, op1);
12084
12085       if (gnu_vector_type_p (type0)
12086           && gnu_vector_type_p (type1)
12087           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12088           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12089         common = 1;
12090       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
12091         {
12092           /* Although it would be tempting to shorten always here, that loses
12093              on some targets, since the modulo instruction is undefined if the
12094              quotient can't be represented in the computation mode.  We shorten
12095              only if unsigned or if dividing by something we know != -1.  */
12096           shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
12097                      || (TREE_CODE (op1) == INTEGER_CST
12098                          && !integer_all_onesp (op1)));
12099           common = 1;
12100         }
12101       break;
12102
12103     case TRUTH_ANDIF_EXPR:
12104     case TRUTH_ORIF_EXPR:
12105     case TRUTH_AND_EXPR:
12106     case TRUTH_OR_EXPR:
12107     case TRUTH_XOR_EXPR:
12108       if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
12109            || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12110            || code0 == FIXED_POINT_TYPE)
12111           && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
12112               || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12113               || code1 == FIXED_POINT_TYPE))
12114         {
12115           /* Result of these operations is always an int,
12116              but that does not mean the operands should be
12117              converted to ints!  */
12118           result_type = integer_type_node;
12119           if (op0_int_operands)
12120             {
12121               op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
12122               op0 = remove_c_maybe_const_expr (op0);
12123             }
12124           else
12125             op0 = c_objc_common_truthvalue_conversion (location, op0);
12126           if (op1_int_operands)
12127             {
12128               op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
12129               op1 = remove_c_maybe_const_expr (op1);
12130             }
12131           else
12132             op1 = c_objc_common_truthvalue_conversion (location, op1);
12133           converted = 1;
12134           boolean_op = true;
12135         }
12136       if (code == TRUTH_ANDIF_EXPR)
12137         {
12138           int_const_or_overflow = (int_operands
12139                                    && TREE_CODE (orig_op0) == INTEGER_CST
12140                                    && (op0 == truthvalue_false_node
12141                                        || TREE_CODE (orig_op1) == INTEGER_CST));
12142           int_const = (int_const_or_overflow
12143                        && !TREE_OVERFLOW (orig_op0)
12144                        && (op0 == truthvalue_false_node
12145                            || !TREE_OVERFLOW (orig_op1)));
12146         }
12147       else if (code == TRUTH_ORIF_EXPR)
12148         {
12149           int_const_or_overflow = (int_operands
12150                                    && TREE_CODE (orig_op0) == INTEGER_CST
12151                                    && (op0 == truthvalue_true_node
12152                                        || TREE_CODE (orig_op1) == INTEGER_CST));
12153           int_const = (int_const_or_overflow
12154                        && !TREE_OVERFLOW (orig_op0)
12155                        && (op0 == truthvalue_true_node
12156                            || !TREE_OVERFLOW (orig_op1)));
12157         }
12158       break;
12159
12160       /* Shift operations: result has same type as first operand;
12161          always convert second operand to int.
12162          Also set SHORT_SHIFT if shifting rightward.  */
12163
12164     case RSHIFT_EXPR:
12165       if (gnu_vector_type_p (type0)
12166           && gnu_vector_type_p (type1)
12167           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12168           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12169           && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12170                        TYPE_VECTOR_SUBPARTS (type1)))
12171         {
12172           result_type = type0;
12173           converted = 1;
12174         }
12175       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12176                 || (gnu_vector_type_p (type0)
12177                     && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12178                && code1 == INTEGER_TYPE)
12179         {
12180           doing_shift = true;
12181           if (TREE_CODE (op1) == INTEGER_CST)
12182             {
12183               if (tree_int_cst_sgn (op1) < 0)
12184                 {
12185                   int_const = false;
12186                   if (c_inhibit_evaluation_warnings == 0)
12187                     warning_at (location, OPT_Wshift_count_negative,
12188                                 "right shift count is negative");
12189                 }
12190               else if (code0 == VECTOR_TYPE)
12191                 {
12192                   if (compare_tree_int (op1,
12193                                         TYPE_PRECISION (TREE_TYPE (type0)))
12194                       >= 0)
12195                     {
12196                       int_const = false;
12197                       if (c_inhibit_evaluation_warnings == 0)
12198                         warning_at (location, OPT_Wshift_count_overflow,
12199                                     "right shift count >= width of vector element");
12200                     }
12201                 }
12202               else
12203                 {
12204                   if (!integer_zerop (op1))
12205                     short_shift = 1;
12206
12207                   if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12208                     {
12209                       int_const = false;
12210                       if (c_inhibit_evaluation_warnings == 0)
12211                         warning_at (location, OPT_Wshift_count_overflow,
12212                                     "right shift count >= width of type");
12213                     }
12214                 }
12215             }
12216
12217           /* Use the type of the value to be shifted.  */
12218           result_type = type0;
12219           /* Avoid converting op1 to result_type later.  */
12220           converted = 1;
12221         }
12222       break;
12223
12224     case LSHIFT_EXPR:
12225       if (gnu_vector_type_p (type0)
12226           && gnu_vector_type_p (type1)
12227           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
12228           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
12229           && known_eq (TYPE_VECTOR_SUBPARTS (type0),
12230                        TYPE_VECTOR_SUBPARTS (type1)))
12231         {
12232           result_type = type0;
12233           converted = 1;
12234         }
12235       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12236                 || (gnu_vector_type_p (type0)
12237                     && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12238                && code1 == INTEGER_TYPE)
12239         {
12240           doing_shift = true;
12241           if (TREE_CODE (op0) == INTEGER_CST
12242               && tree_int_cst_sgn (op0) < 0
12243               && !TYPE_OVERFLOW_WRAPS (type0))
12244             {
12245               /* Don't reject a left shift of a negative value in a context
12246                  where a constant expression is needed in C90.  */
12247               if (flag_isoc99)
12248                 int_const = false;
12249               if (c_inhibit_evaluation_warnings == 0)
12250                 warning_at (location, OPT_Wshift_negative_value,
12251                             "left shift of negative value");
12252             }
12253           if (TREE_CODE (op1) == INTEGER_CST)
12254             {
12255               if (tree_int_cst_sgn (op1) < 0)
12256                 {
12257                   int_const = false;
12258                   if (c_inhibit_evaluation_warnings == 0)
12259                     warning_at (location, OPT_Wshift_count_negative,
12260                                 "left shift count is negative");
12261                 }
12262               else if (code0 == VECTOR_TYPE)
12263                 {
12264                   if (compare_tree_int (op1,
12265                                         TYPE_PRECISION (TREE_TYPE (type0)))
12266                       >= 0)
12267                     {
12268                       int_const = false;
12269                       if (c_inhibit_evaluation_warnings == 0)
12270                         warning_at (location, OPT_Wshift_count_overflow,
12271                                     "left shift count >= width of vector element");
12272                     }
12273                 }
12274               else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12275                 {
12276                   int_const = false;
12277                   if (c_inhibit_evaluation_warnings == 0)
12278                     warning_at (location, OPT_Wshift_count_overflow,
12279                                 "left shift count >= width of type");
12280                 }
12281               else if (TREE_CODE (op0) == INTEGER_CST
12282                        && maybe_warn_shift_overflow (location, op0, op1)
12283                        && flag_isoc99)
12284                 int_const = false;
12285             }
12286
12287           /* Use the type of the value to be shifted.  */
12288           result_type = type0;
12289           /* Avoid converting op1 to result_type later.  */
12290           converted = 1;
12291         }
12292       break;
12293
12294     case EQ_EXPR:
12295     case NE_EXPR:
12296       if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12297         {
12298           tree intt;
12299           if (!vector_types_compatible_elements_p (type0, type1))
12300             {
12301               error_at (location, "comparing vectors with different "
12302                                   "element types");
12303               return error_mark_node;
12304             }
12305
12306           if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12307                         TYPE_VECTOR_SUBPARTS (type1)))
12308             {
12309               error_at (location, "comparing vectors with different "
12310                                   "number of elements");
12311               return error_mark_node;
12312             }
12313
12314           /* It's not precisely specified how the usual arithmetic
12315              conversions apply to the vector types.  Here, we use
12316              the unsigned type if one of the operands is signed and
12317              the other one is unsigned.  */
12318           if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12319             {
12320               if (!TYPE_UNSIGNED (type0))
12321                 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12322               else
12323                 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12324               warning_at (location, OPT_Wsign_compare, "comparison between "
12325                           "types %qT and %qT", type0, type1);
12326             }
12327
12328           /* Always construct signed integer vector type.  */
12329           intt = c_common_type_for_size (GET_MODE_BITSIZE
12330                                          (SCALAR_TYPE_MODE
12331                                           (TREE_TYPE (type0))), 0);
12332           if (!intt)
12333             {
12334               error_at (location, "could not find an integer type "
12335                                   "of the same size as %qT",
12336                         TREE_TYPE (type0));
12337               return error_mark_node;
12338             }
12339           result_type = build_opaque_vector_type (intt,
12340                                                   TYPE_VECTOR_SUBPARTS (type0));
12341           converted = 1;
12342           ret = build_vec_cmp (resultcode, result_type, op0, op1);
12343           goto return_build_binary_op;
12344         }
12345       if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12346         warning_at (location,
12347                     OPT_Wfloat_equal,
12348                     "comparing floating-point with %<==%> or %<!=%> is unsafe");
12349       /* Result of comparison is always int,
12350          but don't convert the args to int!  */
12351       build_type = integer_type_node;
12352       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12353            || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12354           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12355               || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12356         short_compare = 1;
12357       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12358         {
12359           maybe_warn_for_null_address (location, op0, code);
12360           result_type = type0;
12361         }
12362       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12363         {
12364           maybe_warn_for_null_address (location, op1, code);
12365           result_type = type1;
12366         }
12367       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12368         {
12369           tree tt0 = TREE_TYPE (type0);
12370           tree tt1 = TREE_TYPE (type1);
12371           addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12372           addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12373           addr_space_t as_common = ADDR_SPACE_GENERIC;
12374
12375           /* Anything compares with void *.  void * compares with anything.
12376              Otherwise, the targets must be compatible
12377              and both must be object or both incomplete.  */
12378           if (comp_target_types (location, type0, type1))
12379             result_type = common_pointer_type (type0, type1);
12380           else if (!addr_space_superset (as0, as1, &as_common))
12381             {
12382               error_at (location, "comparison of pointers to "
12383                         "disjoint address spaces");
12384               return error_mark_node;
12385             }
12386           else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12387             {
12388               if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12389                 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12390                          "comparison of %<void *%> with function pointer");
12391             }
12392           else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12393             {
12394               if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12395                 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12396                          "comparison of %<void *%> with function pointer");
12397             }
12398           else
12399             /* Avoid warning about the volatile ObjC EH puts on decls.  */
12400             if (!objc_ok)
12401               pedwarn (location, 0,
12402                        "comparison of distinct pointer types lacks a cast");
12403
12404           if (result_type == NULL_TREE)
12405             {
12406               int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12407               result_type = build_pointer_type
12408                               (build_qualified_type (void_type_node, qual));
12409             }
12410         }
12411       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12412         {
12413           result_type = type0;
12414           pedwarn (location, 0, "comparison between pointer and integer");
12415         }
12416       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12417         {
12418           result_type = type1;
12419           pedwarn (location, 0, "comparison between pointer and integer");
12420         }
12421       if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12422            || truth_value_p (TREE_CODE (orig_op0)))
12423           ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12424              || truth_value_p (TREE_CODE (orig_op1))))
12425         maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12426       break;
12427
12428     case LE_EXPR:
12429     case GE_EXPR:
12430     case LT_EXPR:
12431     case GT_EXPR:
12432       if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12433         {
12434           tree intt;
12435           if (!vector_types_compatible_elements_p (type0, type1))
12436             {
12437               error_at (location, "comparing vectors with different "
12438                                   "element types");
12439               return error_mark_node;
12440             }
12441
12442           if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12443                         TYPE_VECTOR_SUBPARTS (type1)))
12444             {
12445               error_at (location, "comparing vectors with different "
12446                                   "number of elements");
12447               return error_mark_node;
12448             }
12449
12450           /* It's not precisely specified how the usual arithmetic
12451              conversions apply to the vector types.  Here, we use
12452              the unsigned type if one of the operands is signed and
12453              the other one is unsigned.  */
12454           if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12455             {
12456               if (!TYPE_UNSIGNED (type0))
12457                 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12458               else
12459                 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12460               warning_at (location, OPT_Wsign_compare, "comparison between "
12461                           "types %qT and %qT", type0, type1);
12462             }
12463
12464           /* Always construct signed integer vector type.  */
12465           intt = c_common_type_for_size (GET_MODE_BITSIZE
12466                                          (SCALAR_TYPE_MODE
12467                                           (TREE_TYPE (type0))), 0);
12468           if (!intt)
12469             {
12470               error_at (location, "could not find an integer type "
12471                                   "of the same size as %qT",
12472                         TREE_TYPE (type0));
12473               return error_mark_node;
12474             }
12475           result_type = build_opaque_vector_type (intt,
12476                                                   TYPE_VECTOR_SUBPARTS (type0));
12477           converted = 1;
12478           ret = build_vec_cmp (resultcode, result_type, op0, op1);
12479           goto return_build_binary_op;
12480         }
12481       build_type = integer_type_node;
12482       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12483            || code0 == FIXED_POINT_TYPE)
12484           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12485               || code1 == FIXED_POINT_TYPE))
12486         short_compare = 1;
12487       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12488         {
12489           addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12490           addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12491           addr_space_t as_common;
12492
12493           if (comp_target_types (location, type0, type1))
12494             {
12495               result_type = common_pointer_type (type0, type1);
12496               if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12497                   != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12498                 pedwarn_c99 (location, OPT_Wpedantic,
12499                              "comparison of complete and incomplete pointers");
12500               else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12501                 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12502                          "ordered comparisons of pointers to functions");
12503               else if (null_pointer_constant_p (orig_op0)
12504                        || null_pointer_constant_p (orig_op1))
12505                 warning_at (location, OPT_Wextra,
12506                             "ordered comparison of pointer with null pointer");
12507
12508             }
12509           else if (!addr_space_superset (as0, as1, &as_common))
12510             {
12511               error_at (location, "comparison of pointers to "
12512                         "disjoint address spaces");
12513               return error_mark_node;
12514             }
12515           else
12516             {
12517               int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12518               result_type = build_pointer_type
12519                               (build_qualified_type (void_type_node, qual));
12520               pedwarn (location, 0,
12521                        "comparison of distinct pointer types lacks a cast");
12522             }
12523         }
12524       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12525         {
12526           result_type = type0;
12527           if (pedantic)
12528             pedwarn (location, OPT_Wpedantic,
12529                      "ordered comparison of pointer with integer zero");
12530           else if (extra_warnings)
12531             warning_at (location, OPT_Wextra,
12532                         "ordered comparison of pointer with integer zero");
12533         }
12534       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12535         {
12536           result_type = type1;
12537           if (pedantic)
12538             pedwarn (location, OPT_Wpedantic,
12539                      "ordered comparison of pointer with integer zero");
12540           else if (extra_warnings)
12541             warning_at (location, OPT_Wextra,
12542                         "ordered comparison of pointer with integer zero");
12543         }
12544       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12545         {
12546           result_type = type0;
12547           pedwarn (location, 0, "comparison between pointer and integer");
12548         }
12549       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12550         {
12551           result_type = type1;
12552           pedwarn (location, 0, "comparison between pointer and integer");
12553         }
12554
12555       if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12556           && current_function_decl != NULL_TREE
12557           && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12558         {
12559           op0 = save_expr (op0);
12560           op1 = save_expr (op1);
12561
12562           tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12563           instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12564         }
12565
12566       if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12567            || truth_value_p (TREE_CODE (orig_op0)))
12568           ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12569              || truth_value_p (TREE_CODE (orig_op1))))
12570         maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12571       break;
12572
12573     case MIN_EXPR:
12574     case MAX_EXPR:
12575       /* Used for OpenMP atomics.  */
12576       gcc_assert (flag_openmp);
12577       common = 1;
12578       break;
12579
12580     default:
12581       gcc_unreachable ();
12582     }
12583
12584   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12585     return error_mark_node;
12586
12587   if (gnu_vector_type_p (type0)
12588       && gnu_vector_type_p (type1)
12589       && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12590           || !vector_types_compatible_elements_p (type0, type1)))
12591     {
12592       gcc_rich_location richloc (location);
12593       maybe_range_label_for_tree_type_mismatch
12594         label_for_op0 (orig_op0, orig_op1),
12595         label_for_op1 (orig_op1, orig_op0);
12596       richloc.maybe_add_expr (orig_op0, &label_for_op0);
12597       richloc.maybe_add_expr (orig_op1, &label_for_op1);
12598       binary_op_error (&richloc, code, type0, type1);
12599       return error_mark_node;
12600     }
12601
12602   if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12603        || code0 == FIXED_POINT_TYPE
12604        || gnu_vector_type_p (type0))
12605       &&
12606       (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12607        || code1 == FIXED_POINT_TYPE
12608        || gnu_vector_type_p (type1)))
12609     {
12610       bool first_complex = (code0 == COMPLEX_TYPE);
12611       bool second_complex = (code1 == COMPLEX_TYPE);
12612       int none_complex = (!first_complex && !second_complex);
12613
12614       if (shorten || common || short_compare)
12615         {
12616           result_type = c_common_type (type0, type1);
12617           do_warn_double_promotion (result_type, type0, type1,
12618                                     "implicit conversion from %qT to %qT "
12619                                     "to match other operand of binary "
12620                                     "expression",
12621                                     location);
12622           if (result_type == error_mark_node)
12623             return error_mark_node;
12624         }
12625
12626       if (first_complex != second_complex
12627           && (code == PLUS_EXPR
12628               || code == MINUS_EXPR
12629               || code == MULT_EXPR
12630               || (code == TRUNC_DIV_EXPR && first_complex))
12631           && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12632           && flag_signed_zeros)
12633         {
12634           /* An operation on mixed real/complex operands must be
12635              handled specially, but the language-independent code can
12636              more easily optimize the plain complex arithmetic if
12637              -fno-signed-zeros.  */
12638           tree real_type = TREE_TYPE (result_type);
12639           tree real, imag;
12640           if (type0 != orig_type0 || type1 != orig_type1)
12641             {
12642               gcc_assert (may_need_excess_precision && common);
12643               semantic_result_type = c_common_type (orig_type0, orig_type1);
12644             }
12645           if (first_complex)
12646             {
12647               if (TREE_TYPE (op0) != result_type)
12648                 op0 = convert_and_check (location, result_type, op0);
12649               if (TREE_TYPE (op1) != real_type)
12650                 op1 = convert_and_check (location, real_type, op1);
12651             }
12652           else
12653             {
12654               if (TREE_TYPE (op0) != real_type)
12655                 op0 = convert_and_check (location, real_type, op0);
12656               if (TREE_TYPE (op1) != result_type)
12657                 op1 = convert_and_check (location, result_type, op1);
12658             }
12659           if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12660             return error_mark_node;
12661           if (first_complex)
12662             {
12663               op0 = save_expr (op0);
12664               real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12665                                      op0, true);
12666               imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12667                                      op0, true);
12668               switch (code)
12669                 {
12670                 case MULT_EXPR:
12671                 case TRUNC_DIV_EXPR:
12672                   op1 = save_expr (op1);
12673                   imag = build2 (resultcode, real_type, imag, op1);
12674                   /* Fall through.  */
12675                 case PLUS_EXPR:
12676                 case MINUS_EXPR:
12677                   real = build2 (resultcode, real_type, real, op1);
12678                   break;
12679                 default:
12680                   gcc_unreachable();
12681                 }
12682             }
12683           else
12684             {
12685               op1 = save_expr (op1);
12686               real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12687                                      op1, true);
12688               imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12689                                      op1, true);
12690               switch (code)
12691                 {
12692                 case MULT_EXPR:
12693                   op0 = save_expr (op0);
12694                   imag = build2 (resultcode, real_type, op0, imag);
12695                   /* Fall through.  */
12696                 case PLUS_EXPR:
12697                   real = build2 (resultcode, real_type, op0, real);
12698                   break;
12699                 case MINUS_EXPR:
12700                   real = build2 (resultcode, real_type, op0, real);
12701                   imag = build1 (NEGATE_EXPR, real_type, imag);
12702                   break;
12703                 default:
12704                   gcc_unreachable();
12705                 }
12706             }
12707           ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12708           goto return_build_binary_op;
12709         }
12710
12711       /* For certain operations (which identify themselves by shorten != 0)
12712          if both args were extended from the same smaller type,
12713          do the arithmetic in that type and then extend.
12714
12715          shorten !=0 and !=1 indicates a bitwise operation.
12716          For them, this optimization is safe only if
12717          both args are zero-extended or both are sign-extended.
12718          Otherwise, we might change the result.
12719          Eg, (short)-1 | (unsigned short)-1 is (int)-1
12720          but calculated in (unsigned short) it would be (unsigned short)-1.  */
12721
12722       if (shorten && none_complex)
12723         {
12724           final_type = result_type;
12725           result_type = shorten_binary_op (result_type, op0, op1,
12726                                            shorten == -1);
12727         }
12728
12729       /* Shifts can be shortened if shifting right.  */
12730
12731       if (short_shift)
12732         {
12733           int unsigned_arg;
12734           tree arg0 = get_narrower (op0, &unsigned_arg);
12735
12736           final_type = result_type;
12737
12738           if (arg0 == op0 && final_type == TREE_TYPE (op0))
12739             unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12740
12741           if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12742               && tree_int_cst_sgn (op1) > 0
12743               /* We can shorten only if the shift count is less than the
12744                  number of bits in the smaller type size.  */
12745               && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12746               /* We cannot drop an unsigned shift after sign-extension.  */
12747               && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12748             {
12749               /* Do an unsigned shift if the operand was zero-extended.  */
12750               result_type
12751                 = c_common_signed_or_unsigned_type (unsigned_arg,
12752                                                     TREE_TYPE (arg0));
12753               /* Convert value-to-be-shifted to that type.  */
12754               if (TREE_TYPE (op0) != result_type)
12755                 op0 = convert (result_type, op0);
12756               converted = 1;
12757             }
12758         }
12759
12760       /* Comparison operations are shortened too but differently.
12761          They identify themselves by setting short_compare = 1.  */
12762
12763       if (short_compare)
12764         {
12765           /* Don't write &op0, etc., because that would prevent op0
12766              from being kept in a register.
12767              Instead, make copies of the our local variables and
12768              pass the copies by reference, then copy them back afterward.  */
12769           tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12770           enum tree_code xresultcode = resultcode;
12771           tree val
12772             = shorten_compare (location, &xop0, &xop1, &xresult_type,
12773                                &xresultcode);
12774
12775           if (val != NULL_TREE)
12776             {
12777               ret = val;
12778               goto return_build_binary_op;
12779             }
12780
12781           op0 = xop0, op1 = xop1;
12782           converted = 1;
12783           resultcode = xresultcode;
12784
12785           if (c_inhibit_evaluation_warnings == 0 && !c_in_omp_for)
12786             {
12787               bool op0_maybe_const = true;
12788               bool op1_maybe_const = true;
12789               tree orig_op0_folded, orig_op1_folded;
12790
12791               if (in_late_binary_op)
12792                 {
12793                   orig_op0_folded = orig_op0;
12794                   orig_op1_folded = orig_op1;
12795                 }
12796               else
12797                 {
12798                   /* Fold for the sake of possible warnings, as in
12799                      build_conditional_expr.  This requires the
12800                      "original" values to be folded, not just op0 and
12801                      op1.  */
12802                   c_inhibit_evaluation_warnings++;
12803                   op0 = c_fully_fold (op0, require_constant_value,
12804                                       &op0_maybe_const);
12805                   op1 = c_fully_fold (op1, require_constant_value,
12806                                       &op1_maybe_const);
12807                   c_inhibit_evaluation_warnings--;
12808                   orig_op0_folded = c_fully_fold (orig_op0,
12809                                                   require_constant_value,
12810                                                   NULL);
12811                   orig_op1_folded = c_fully_fold (orig_op1,
12812                                                   require_constant_value,
12813                                                   NULL);
12814                 }
12815
12816               if (warn_sign_compare)
12817                 warn_for_sign_compare (location, orig_op0_folded,
12818                                        orig_op1_folded, op0, op1,
12819                                        result_type, resultcode);
12820               if (!in_late_binary_op && !int_operands)
12821                 {
12822                   if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12823                     op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12824                   if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12825                     op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12826                 }
12827             }
12828         }
12829     }
12830
12831   /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12832      If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12833      Then the expression will be built.
12834      It will be given type FINAL_TYPE if that is nonzero;
12835      otherwise, it will be given type RESULT_TYPE.  */
12836
12837   if (!result_type)
12838     {
12839       /* Favor showing any expression locations that are available. */
12840       op_location_t oploc (location, UNKNOWN_LOCATION);
12841       binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12842       binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12843       return error_mark_node;
12844     }
12845
12846   if (build_type == NULL_TREE)
12847     {
12848       build_type = result_type;
12849       if ((type0 != orig_type0 || type1 != orig_type1)
12850           && !boolean_op)
12851         {
12852           gcc_assert (may_need_excess_precision && common);
12853           semantic_result_type = c_common_type (orig_type0, orig_type1);
12854         }
12855     }
12856
12857   if (!converted)
12858     {
12859       op0 = ep_convert_and_check (location, result_type, op0,
12860                                   semantic_result_type);
12861       op1 = ep_convert_and_check (location, result_type, op1,
12862                                   semantic_result_type);
12863
12864       /* This can happen if one operand has a vector type, and the other
12865          has a different type.  */
12866       if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12867         return error_mark_node;
12868     }
12869
12870   if (sanitize_flags_p ((SANITIZE_SHIFT
12871                          | SANITIZE_DIVIDE
12872                          | SANITIZE_FLOAT_DIVIDE
12873                          | SANITIZE_SI_OVERFLOW))
12874       && current_function_decl != NULL_TREE
12875       && (doing_div_or_mod || doing_shift)
12876       && !require_constant_value)
12877     {
12878       /* OP0 and/or OP1 might have side-effects.  */
12879       op0 = save_expr (op0);
12880       op1 = save_expr (op1);
12881       op0 = c_fully_fold (op0, false, NULL);
12882       op1 = c_fully_fold (op1, false, NULL);
12883       if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12884                                                   | SANITIZE_FLOAT_DIVIDE
12885                                                   | SANITIZE_SI_OVERFLOW))))
12886         instrument_expr = ubsan_instrument_division (location, op0, op1);
12887       else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12888         instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12889     }
12890
12891   /* Treat expressions in initializers specially as they can't trap.  */
12892   if (int_const_or_overflow)
12893     ret = (require_constant_value
12894            ? fold_build2_initializer_loc (location, resultcode, build_type,
12895                                           op0, op1)
12896            : fold_build2_loc (location, resultcode, build_type, op0, op1));
12897   else
12898     ret = build2 (resultcode, build_type, op0, op1);
12899   if (final_type != NULL_TREE)
12900     ret = convert (final_type, ret);
12901
12902  return_build_binary_op:
12903   gcc_assert (ret != error_mark_node);
12904   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12905     ret = (int_operands
12906            ? note_integer_operands (ret)
12907            : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12908   else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12909            && !in_late_binary_op)
12910     ret = note_integer_operands (ret);
12911   protected_set_expr_location (ret, location);
12912
12913   if (instrument_expr != NULL)
12914     ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12915                        instrument_expr, ret);
12916
12917   if (semantic_result_type)
12918     ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12919                       semantic_result_type, ret);
12920
12921   return ret;
12922 }
12923
12924
12925 /* Convert EXPR to be a truth-value, validating its type for this
12926    purpose.  LOCATION is the source location for the expression.  */
12927
12928 tree
12929 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12930 {
12931   bool int_const, int_operands;
12932
12933   switch (TREE_CODE (TREE_TYPE (expr)))
12934     {
12935     case ARRAY_TYPE:
12936       error_at (location, "used array that cannot be converted to pointer where scalar is required");
12937       return error_mark_node;
12938
12939     case RECORD_TYPE:
12940       error_at (location, "used struct type value where scalar is required");
12941       return error_mark_node;
12942
12943     case UNION_TYPE:
12944       error_at (location, "used union type value where scalar is required");
12945       return error_mark_node;
12946
12947     case VOID_TYPE:
12948       error_at (location, "void value not ignored as it ought to be");
12949       return error_mark_node;
12950
12951     case POINTER_TYPE:
12952       if (reject_gcc_builtin (expr))
12953         return error_mark_node;
12954       break;
12955
12956     case FUNCTION_TYPE:
12957       gcc_unreachable ();
12958
12959     case VECTOR_TYPE:
12960       error_at (location, "used vector type where scalar is required");
12961       return error_mark_node;
12962
12963     default:
12964       break;
12965     }
12966
12967   int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12968   int_operands = EXPR_INT_CONST_OPERANDS (expr);
12969   if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12970     {
12971       expr = remove_c_maybe_const_expr (expr);
12972       expr = build2 (NE_EXPR, integer_type_node, expr,
12973                      convert (TREE_TYPE (expr), integer_zero_node));
12974       expr = note_integer_operands (expr);
12975     }
12976   else
12977     /* ??? Should we also give an error for vectors rather than leaving
12978        those to give errors later?  */
12979     expr = c_common_truthvalue_conversion (location, expr);
12980
12981   if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12982     {
12983       if (TREE_OVERFLOW (expr))
12984         return expr;
12985       else
12986         return note_integer_operands (expr);
12987     }
12988   if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12989     return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12990   return expr;
12991 }
12992 \f
12993
12994 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12995    required.  */
12996
12997 tree
12998 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12999 {
13000   if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
13001     {
13002       tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
13003       /* Executing a compound literal inside a function reinitializes
13004          it.  */
13005       if (!TREE_STATIC (decl))
13006         *se = true;
13007       return decl;
13008     }
13009   else
13010     return expr;
13011 }
13012 \f
13013 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
13014    statement.  LOC is the location of the construct.  */
13015
13016 tree
13017 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
13018                         tree clauses)
13019 {
13020   body = c_end_compound_stmt (loc, body, true);
13021
13022   tree stmt = make_node (code);
13023   TREE_TYPE (stmt) = void_type_node;
13024   OMP_BODY (stmt) = body;
13025   OMP_CLAUSES (stmt) = clauses;
13026   SET_EXPR_LOCATION (stmt, loc);
13027
13028   return add_stmt (stmt);
13029 }
13030
13031 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
13032    statement.  LOC is the location of the OACC_DATA.  */
13033
13034 tree
13035 c_finish_oacc_data (location_t loc, tree clauses, tree block)
13036 {
13037   tree stmt;
13038
13039   block = c_end_compound_stmt (loc, block, true);
13040
13041   stmt = make_node (OACC_DATA);
13042   TREE_TYPE (stmt) = void_type_node;
13043   OACC_DATA_CLAUSES (stmt) = clauses;
13044   OACC_DATA_BODY (stmt) = block;
13045   SET_EXPR_LOCATION (stmt, loc);
13046
13047   return add_stmt (stmt);
13048 }
13049
13050 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
13051    statement.  LOC is the location of the OACC_HOST_DATA.  */
13052
13053 tree
13054 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
13055 {
13056   tree stmt;
13057
13058   block = c_end_compound_stmt (loc, block, true);
13059
13060   stmt = make_node (OACC_HOST_DATA);
13061   TREE_TYPE (stmt) = void_type_node;
13062   OACC_HOST_DATA_CLAUSES (stmt) = clauses;
13063   OACC_HOST_DATA_BODY (stmt) = block;
13064   SET_EXPR_LOCATION (stmt, loc);
13065
13066   return add_stmt (stmt);
13067 }
13068
13069 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
13070
13071 tree
13072 c_begin_omp_parallel (void)
13073 {
13074   tree block;
13075
13076   keep_next_level ();
13077   block = c_begin_compound_stmt (true);
13078
13079   return block;
13080 }
13081
13082 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
13083    statement.  LOC is the location of the OMP_PARALLEL.  */
13084
13085 tree
13086 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
13087 {
13088   tree stmt;
13089
13090   block = c_end_compound_stmt (loc, block, true);
13091
13092   stmt = make_node (OMP_PARALLEL);
13093   TREE_TYPE (stmt) = void_type_node;
13094   OMP_PARALLEL_CLAUSES (stmt) = clauses;
13095   OMP_PARALLEL_BODY (stmt) = block;
13096   SET_EXPR_LOCATION (stmt, loc);
13097
13098   return add_stmt (stmt);
13099 }
13100
13101 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
13102
13103 tree
13104 c_begin_omp_task (void)
13105 {
13106   tree block;
13107
13108   keep_next_level ();
13109   block = c_begin_compound_stmt (true);
13110
13111   return block;
13112 }
13113
13114 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
13115    statement.  LOC is the location of the #pragma.  */
13116
13117 tree
13118 c_finish_omp_task (location_t loc, tree clauses, tree block)
13119 {
13120   tree stmt;
13121
13122   block = c_end_compound_stmt (loc, block, true);
13123
13124   stmt = make_node (OMP_TASK);
13125   TREE_TYPE (stmt) = void_type_node;
13126   OMP_TASK_CLAUSES (stmt) = clauses;
13127   OMP_TASK_BODY (stmt) = block;
13128   SET_EXPR_LOCATION (stmt, loc);
13129
13130   return add_stmt (stmt);
13131 }
13132
13133 /* Generate GOMP_cancel call for #pragma omp cancel.  */
13134
13135 void
13136 c_finish_omp_cancel (location_t loc, tree clauses)
13137 {
13138   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
13139   int mask = 0;
13140   if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13141     mask = 1;
13142   else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13143     mask = 2;
13144   else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13145     mask = 4;
13146   else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13147     mask = 8;
13148   else
13149     {
13150       error_at (loc, "%<#pragma omp cancel%> must specify one of "
13151                      "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13152                      "clauses");
13153       return;
13154     }
13155   tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
13156   if (ifc != NULL_TREE)
13157     {
13158       if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
13159           && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
13160         error_at (OMP_CLAUSE_LOCATION (ifc),
13161                   "expected %<cancel%> %<if%> clause modifier");
13162       else
13163         {
13164           tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
13165           if (ifc2 != NULL_TREE)
13166             {
13167               gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
13168                           && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
13169                           && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
13170               error_at (OMP_CLAUSE_LOCATION (ifc2),
13171                         "expected %<cancel%> %<if%> clause modifier");
13172             }
13173         }
13174
13175       tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
13176       ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
13177                              boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
13178                              build_zero_cst (type));
13179     }
13180   else
13181     ifc = boolean_true_node;
13182   tree stmt = build_call_expr_loc (loc, fn, 2,
13183                                    build_int_cst (integer_type_node, mask),
13184                                    ifc);
13185   add_stmt (stmt);
13186 }
13187
13188 /* Generate GOMP_cancellation_point call for
13189    #pragma omp cancellation point.  */
13190
13191 void
13192 c_finish_omp_cancellation_point (location_t loc, tree clauses)
13193 {
13194   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
13195   int mask = 0;
13196   if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
13197     mask = 1;
13198   else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
13199     mask = 2;
13200   else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
13201     mask = 4;
13202   else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
13203     mask = 8;
13204   else
13205     {
13206       error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
13207                      "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
13208                      "clauses");
13209       return;
13210     }
13211   tree stmt = build_call_expr_loc (loc, fn, 1,
13212                                    build_int_cst (integer_type_node, mask));
13213   add_stmt (stmt);
13214 }
13215
13216 /* Helper function for handle_omp_array_sections.  Called recursively
13217    to handle multiple array-section-subscripts.  C is the clause,
13218    T current expression (initially OMP_CLAUSE_DECL), which is either
13219    a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13220    expression if specified, TREE_VALUE length expression if specified,
13221    TREE_CHAIN is what it has been specified after, or some decl.
13222    TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13223    set to true if any of the array-section-subscript could have length
13224    of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13225    first array-section-subscript which is known not to have length
13226    of one.  Given say:
13227    map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13228    FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13229    all are or may have length of 1, array-section-subscript [:2] is the
13230    first one known not to have length 1.  For array-section-subscript
13231    <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13232    0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13233    can if MAYBE_ZERO_LEN is false.  MAYBE_ZERO_LEN will be true in the above
13234    case though, as some lengths could be zero.  */
13235
13236 static tree
13237 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13238                              bool &maybe_zero_len, unsigned int &first_non_one,
13239                              enum c_omp_region_type ort)
13240 {
13241   tree ret, low_bound, length, type;
13242   if (TREE_CODE (t) != TREE_LIST)
13243     {
13244       if (error_operand_p (t))
13245         return error_mark_node;
13246       ret = t;
13247       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13248           && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13249           && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13250         {
13251           error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13252                     t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13253           return error_mark_node;
13254         }
13255       while (TREE_CODE (t) == INDIRECT_REF)
13256         {
13257           t = TREE_OPERAND (t, 0);
13258           STRIP_NOPS (t);
13259           if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13260             t = TREE_OPERAND (t, 0);
13261         }
13262       while (TREE_CODE (t) == COMPOUND_EXPR)
13263         {
13264           t = TREE_OPERAND (t, 1);
13265           STRIP_NOPS (t);
13266         }
13267       if (TREE_CODE (t) == COMPONENT_REF
13268           && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13269               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13270               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13271         {
13272           if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13273             {
13274               error_at (OMP_CLAUSE_LOCATION (c),
13275                         "bit-field %qE in %qs clause",
13276                         t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13277               return error_mark_node;
13278             }
13279           while (TREE_CODE (t) == COMPONENT_REF)
13280             {
13281               if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13282                 {
13283                   error_at (OMP_CLAUSE_LOCATION (c),
13284                             "%qE is a member of a union", t);
13285                   return error_mark_node;
13286                 }
13287               t = TREE_OPERAND (t, 0);
13288               while (TREE_CODE (t) == MEM_REF
13289                      || TREE_CODE (t) == INDIRECT_REF
13290                      || TREE_CODE (t) == ARRAY_REF)
13291                 {
13292                   t = TREE_OPERAND (t, 0);
13293                   STRIP_NOPS (t);
13294                   if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13295                     t = TREE_OPERAND (t, 0);
13296                 }
13297               if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13298                 {
13299                   if (maybe_ne (mem_ref_offset (t), 0))
13300                     error_at (OMP_CLAUSE_LOCATION (c),
13301                               "cannot dereference %qE in %qs clause", t,
13302                               omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13303                   else
13304                     t = TREE_OPERAND (t, 0);
13305                 }
13306             }
13307         }
13308       if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13309         {
13310           if (DECL_P (t))
13311             error_at (OMP_CLAUSE_LOCATION (c),
13312                       "%qD is not a variable in %qs clause", t,
13313                       omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13314           else
13315             error_at (OMP_CLAUSE_LOCATION (c),
13316                       "%qE is not a variable in %qs clause", t,
13317                       omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13318           return error_mark_node;
13319         }
13320       else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13321                && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13322                && TYPE_ATOMIC (TREE_TYPE (t)))
13323         {
13324           error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13325                     t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13326           return error_mark_node;
13327         }
13328       else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13329                && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13330                && VAR_P (t)
13331                && DECL_THREAD_LOCAL_P (t))
13332         {
13333           error_at (OMP_CLAUSE_LOCATION (c),
13334                     "%qD is threadprivate variable in %qs clause", t,
13335                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13336           return error_mark_node;
13337         }
13338       if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13339            || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13340           && TYPE_ATOMIC (TREE_TYPE (t))
13341           && POINTER_TYPE_P (TREE_TYPE (t)))
13342         {
13343           /* If the array section is pointer based and the pointer
13344              itself is _Atomic qualified, we need to atomically load
13345              the pointer.  */
13346           c_expr expr;
13347           memset (&expr, 0, sizeof (expr));
13348           expr.value = ret;
13349           expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13350                                            expr, false, false);
13351           ret = expr.value;
13352         }
13353       return ret;
13354     }
13355
13356   ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13357                                      maybe_zero_len, first_non_one, ort);
13358   if (ret == error_mark_node || ret == NULL_TREE)
13359     return ret;
13360
13361   type = TREE_TYPE (ret);
13362   low_bound = TREE_PURPOSE (t);
13363   length = TREE_VALUE (t);
13364
13365   if (low_bound == error_mark_node || length == error_mark_node)
13366     return error_mark_node;
13367
13368   if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13369     {
13370       error_at (OMP_CLAUSE_LOCATION (c),
13371                 "low bound %qE of array section does not have integral type",
13372                 low_bound);
13373       return error_mark_node;
13374     }
13375   if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13376     {
13377       error_at (OMP_CLAUSE_LOCATION (c),
13378                 "length %qE of array section does not have integral type",
13379                 length);
13380       return error_mark_node;
13381     }
13382   if (low_bound
13383       && TREE_CODE (low_bound) == INTEGER_CST
13384       && TYPE_PRECISION (TREE_TYPE (low_bound))
13385          > TYPE_PRECISION (sizetype))
13386     low_bound = fold_convert (sizetype, low_bound);
13387   if (length
13388       && TREE_CODE (length) == INTEGER_CST
13389       && TYPE_PRECISION (TREE_TYPE (length))
13390          > TYPE_PRECISION (sizetype))
13391     length = fold_convert (sizetype, length);
13392   if (low_bound == NULL_TREE)
13393     low_bound = integer_zero_node;
13394   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13395       && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13396           || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13397     {
13398       if (length != integer_one_node)
13399         {
13400           error_at (OMP_CLAUSE_LOCATION (c),
13401                     "expected single pointer in %qs clause",
13402                     user_omp_clause_code_name (c, ort == C_ORT_ACC));
13403           return error_mark_node;
13404         }
13405     }
13406   if (length != NULL_TREE)
13407     {
13408       if (!integer_nonzerop (length))
13409         {
13410           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13411               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13412               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13413               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13414               || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13415             {
13416               if (integer_zerop (length))
13417                 {
13418                   error_at (OMP_CLAUSE_LOCATION (c),
13419                             "zero length array section in %qs clause",
13420                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13421                   return error_mark_node;
13422                 }
13423             }
13424           else
13425             maybe_zero_len = true;
13426         }
13427       if (first_non_one == types.length ()
13428           && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13429         first_non_one++;
13430     }
13431   if (TREE_CODE (type) == ARRAY_TYPE)
13432     {
13433       if (length == NULL_TREE
13434           && (TYPE_DOMAIN (type) == NULL_TREE
13435               || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13436         {
13437           error_at (OMP_CLAUSE_LOCATION (c),
13438                     "for unknown bound array type length expression must "
13439                     "be specified");
13440           return error_mark_node;
13441         }
13442       if (TREE_CODE (low_bound) == INTEGER_CST
13443           && tree_int_cst_sgn (low_bound) == -1)
13444         {
13445           error_at (OMP_CLAUSE_LOCATION (c),
13446                     "negative low bound in array section in %qs clause",
13447                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13448           return error_mark_node;
13449         }
13450       if (length != NULL_TREE
13451           && TREE_CODE (length) == INTEGER_CST
13452           && tree_int_cst_sgn (length) == -1)
13453         {
13454           error_at (OMP_CLAUSE_LOCATION (c),
13455                     "negative length in array section in %qs clause",
13456                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13457           return error_mark_node;
13458         }
13459       if (TYPE_DOMAIN (type)
13460           && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13461           && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13462                         == INTEGER_CST)
13463         {
13464           tree size
13465             = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13466           size = size_binop (PLUS_EXPR, size, size_one_node);
13467           if (TREE_CODE (low_bound) == INTEGER_CST)
13468             {
13469               if (tree_int_cst_lt (size, low_bound))
13470                 {
13471                   error_at (OMP_CLAUSE_LOCATION (c),
13472                             "low bound %qE above array section size "
13473                             "in %qs clause", low_bound,
13474                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13475                   return error_mark_node;
13476                 }
13477               if (tree_int_cst_equal (size, low_bound))
13478                 {
13479                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
13480                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13481                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13482                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13483                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13484                     {
13485                       error_at (OMP_CLAUSE_LOCATION (c),
13486                                 "zero length array section in %qs clause",
13487                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13488                       return error_mark_node;
13489                     }
13490                   maybe_zero_len = true;
13491                 }
13492               else if (length == NULL_TREE
13493                        && first_non_one == types.length ()
13494                        && tree_int_cst_equal
13495                             (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13496                              low_bound))
13497                 first_non_one++;
13498             }
13499           else if (length == NULL_TREE)
13500             {
13501               if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13502                   && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13503                   && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13504                   && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13505                   && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13506                 maybe_zero_len = true;
13507               if (first_non_one == types.length ())
13508                 first_non_one++;
13509             }
13510           if (length && TREE_CODE (length) == INTEGER_CST)
13511             {
13512               if (tree_int_cst_lt (size, length))
13513                 {
13514                   error_at (OMP_CLAUSE_LOCATION (c),
13515                             "length %qE above array section size "
13516                             "in %qs clause", length,
13517                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13518                   return error_mark_node;
13519                 }
13520               if (TREE_CODE (low_bound) == INTEGER_CST)
13521                 {
13522                   tree lbpluslen
13523                     = size_binop (PLUS_EXPR,
13524                                   fold_convert (sizetype, low_bound),
13525                                   fold_convert (sizetype, length));
13526                   if (TREE_CODE (lbpluslen) == INTEGER_CST
13527                       && tree_int_cst_lt (size, lbpluslen))
13528                     {
13529                       error_at (OMP_CLAUSE_LOCATION (c),
13530                                 "high bound %qE above array section size "
13531                                 "in %qs clause", lbpluslen,
13532                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13533                       return error_mark_node;
13534                     }
13535                 }
13536             }
13537         }
13538       else if (length == NULL_TREE)
13539         {
13540           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13541               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13542               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13543               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13544               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13545             maybe_zero_len = true;
13546           if (first_non_one == types.length ())
13547             first_non_one++;
13548         }
13549
13550       /* For [lb:] we will need to evaluate lb more than once.  */
13551       if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13552         {
13553           tree lb = save_expr (low_bound);
13554           if (lb != low_bound)
13555             {
13556               TREE_PURPOSE (t) = lb;
13557               low_bound = lb;
13558             }
13559         }
13560     }
13561   else if (TREE_CODE (type) == POINTER_TYPE)
13562     {
13563       if (length == NULL_TREE)
13564         {
13565           if (TREE_CODE (ret) == PARM_DECL && C_ARRAY_PARAMETER (ret))
13566             error_at (OMP_CLAUSE_LOCATION (c),
13567                       "for array function parameter length expression "
13568                       "must be specified");
13569           else
13570             error_at (OMP_CLAUSE_LOCATION (c),
13571                       "for pointer type length expression must be specified");
13572           return error_mark_node;
13573         }
13574       if (length != NULL_TREE
13575           && TREE_CODE (length) == INTEGER_CST
13576           && tree_int_cst_sgn (length) == -1)
13577         {
13578           error_at (OMP_CLAUSE_LOCATION (c),
13579                     "negative length in array section in %qs clause",
13580                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13581           return error_mark_node;
13582         }
13583       /* If there is a pointer type anywhere but in the very first
13584          array-section-subscript, the array section could be non-contiguous.  */
13585       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13586           && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
13587           && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13588         {
13589           /* If any prior dimension has a non-one length, then deem this
13590              array section as non-contiguous.  */
13591           for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
13592                d = TREE_CHAIN (d))
13593             {
13594               tree d_length = TREE_VALUE (d);
13595               if (d_length == NULL_TREE || !integer_onep (d_length))
13596                 {
13597                   error_at (OMP_CLAUSE_LOCATION (c),
13598                             "array section is not contiguous in %qs clause",
13599                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13600                   return error_mark_node;
13601                 }
13602             }
13603         }
13604     }
13605   else
13606     {
13607       error_at (OMP_CLAUSE_LOCATION (c),
13608                 "%qE does not have pointer or array type", ret);
13609       return error_mark_node;
13610     }
13611   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13612     types.safe_push (TREE_TYPE (ret));
13613   /* We will need to evaluate lb more than once.  */
13614   tree lb = save_expr (low_bound);
13615   if (lb != low_bound)
13616     {
13617       TREE_PURPOSE (t) = lb;
13618       low_bound = lb;
13619     }
13620   ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13621   return ret;
13622 }
13623
13624 /* Handle array sections for clause C.  */
13625
13626 static bool
13627 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13628 {
13629   bool maybe_zero_len = false;
13630   unsigned int first_non_one = 0;
13631   auto_vec<tree, 10> types;
13632   tree *tp = &OMP_CLAUSE_DECL (c);
13633   if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13634        || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13635       && TREE_CODE (*tp) == TREE_LIST
13636       && TREE_PURPOSE (*tp)
13637       && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13638     tp = &TREE_VALUE (*tp);
13639   tree first = handle_omp_array_sections_1 (c, *tp, types,
13640                                             maybe_zero_len, first_non_one,
13641                                             ort);
13642   if (first == error_mark_node)
13643     return true;
13644   if (first == NULL_TREE)
13645     return false;
13646   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13647       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
13648     {
13649       tree t = *tp;
13650       tree tem = NULL_TREE;
13651       /* Need to evaluate side effects in the length expressions
13652          if any.  */
13653       while (TREE_CODE (t) == TREE_LIST)
13654         {
13655           if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13656             {
13657               if (tem == NULL_TREE)
13658                 tem = TREE_VALUE (t);
13659               else
13660                 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13661                               TREE_VALUE (t), tem);
13662             }
13663           t = TREE_CHAIN (t);
13664         }
13665       if (tem)
13666         first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13667       first = c_fully_fold (first, false, NULL, true);
13668       *tp = first;
13669     }
13670   else
13671     {
13672       unsigned int num = types.length (), i;
13673       tree t, side_effects = NULL_TREE, size = NULL_TREE;
13674       tree condition = NULL_TREE;
13675
13676       if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13677         maybe_zero_len = true;
13678
13679       for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13680            t = TREE_CHAIN (t))
13681         {
13682           tree low_bound = TREE_PURPOSE (t);
13683           tree length = TREE_VALUE (t);
13684
13685           i--;
13686           if (low_bound
13687               && TREE_CODE (low_bound) == INTEGER_CST
13688               && TYPE_PRECISION (TREE_TYPE (low_bound))
13689                  > TYPE_PRECISION (sizetype))
13690             low_bound = fold_convert (sizetype, low_bound);
13691           if (length
13692               && TREE_CODE (length) == INTEGER_CST
13693               && TYPE_PRECISION (TREE_TYPE (length))
13694                  > TYPE_PRECISION (sizetype))
13695             length = fold_convert (sizetype, length);
13696           if (low_bound == NULL_TREE)
13697             low_bound = integer_zero_node;
13698           if (!maybe_zero_len && i > first_non_one)
13699             {
13700               if (integer_nonzerop (low_bound))
13701                 goto do_warn_noncontiguous;
13702               if (length != NULL_TREE
13703                   && TREE_CODE (length) == INTEGER_CST
13704                   && TYPE_DOMAIN (types[i])
13705                   && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13706                   && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13707                      == INTEGER_CST)
13708                 {
13709                   tree size;
13710                   size = size_binop (PLUS_EXPR,
13711                                      TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13712                                      size_one_node);
13713                   if (!tree_int_cst_equal (length, size))
13714                     {
13715                      do_warn_noncontiguous:
13716                       error_at (OMP_CLAUSE_LOCATION (c),
13717                                 "array section is not contiguous in %qs "
13718                                 "clause",
13719                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13720                       return true;
13721                     }
13722                 }
13723               if (length != NULL_TREE
13724                   && TREE_SIDE_EFFECTS (length))
13725                 {
13726                   if (side_effects == NULL_TREE)
13727                     side_effects = length;
13728                   else
13729                     side_effects = build2 (COMPOUND_EXPR,
13730                                            TREE_TYPE (side_effects),
13731                                            length, side_effects);
13732                 }
13733             }
13734           else
13735             {
13736               tree l;
13737
13738               if (i > first_non_one
13739                   && ((length && integer_nonzerop (length))
13740                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13741                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13742                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13743                 continue;
13744               if (length)
13745                 l = fold_convert (sizetype, length);
13746               else
13747                 {
13748                   l = size_binop (PLUS_EXPR,
13749                                   TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13750                                   size_one_node);
13751                   l = size_binop (MINUS_EXPR, l,
13752                                   fold_convert (sizetype, low_bound));
13753                 }
13754               if (i > first_non_one)
13755                 {
13756                   l = fold_build2 (NE_EXPR, boolean_type_node, l,
13757                                    size_zero_node);
13758                   if (condition == NULL_TREE)
13759                     condition = l;
13760                   else
13761                     condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13762                                              l, condition);
13763                 }
13764               else if (size == NULL_TREE)
13765                 {
13766                   size = size_in_bytes (TREE_TYPE (types[i]));
13767                   tree eltype = TREE_TYPE (types[num - 1]);
13768                   while (TREE_CODE (eltype) == ARRAY_TYPE)
13769                     eltype = TREE_TYPE (eltype);
13770                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13771                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13772                       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13773                     {
13774                       if (integer_zerop (size)
13775                           || integer_zerop (size_in_bytes (eltype)))
13776                         {
13777                           error_at (OMP_CLAUSE_LOCATION (c),
13778                                     "zero length array section in %qs clause",
13779                                     omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13780                           return error_mark_node;
13781                         }
13782                       size = size_binop (EXACT_DIV_EXPR, size,
13783                                          size_in_bytes (eltype));
13784                     }
13785                   size = size_binop (MULT_EXPR, size, l);
13786                   if (condition)
13787                     size = fold_build3 (COND_EXPR, sizetype, condition,
13788                                         size, size_zero_node);
13789                 }
13790               else
13791                 size = size_binop (MULT_EXPR, size, l);
13792             }
13793         }
13794       if (side_effects)
13795         size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13796       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13797           || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13798           || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13799         {
13800           size = size_binop (MINUS_EXPR, size, size_one_node);
13801           size = c_fully_fold (size, false, NULL);
13802           size = save_expr (size);
13803           tree index_type = build_index_type (size);
13804           tree eltype = TREE_TYPE (first);
13805           while (TREE_CODE (eltype) == ARRAY_TYPE)
13806             eltype = TREE_TYPE (eltype);
13807           tree type = build_array_type (eltype, index_type);
13808           tree ptype = build_pointer_type (eltype);
13809           if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13810             t = build_fold_addr_expr (t);
13811           tree t2 = build_fold_addr_expr (first);
13812           t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13813                                  ptrdiff_type_node, t2);
13814           t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13815                                 ptrdiff_type_node, t2,
13816                                 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13817                                                   ptrdiff_type_node, t));
13818           t2 = c_fully_fold (t2, false, NULL);
13819           if (tree_fits_shwi_p (t2))
13820             t = build2 (MEM_REF, type, t,
13821                         build_int_cst (ptype, tree_to_shwi (t2)));
13822           else
13823             {
13824               t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13825               t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13826                               TREE_TYPE (t), t, t2);
13827               t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13828             }
13829           OMP_CLAUSE_DECL (c) = t;
13830           return false;
13831         }
13832       first = c_fully_fold (first, false, NULL);
13833       OMP_CLAUSE_DECL (c) = first;
13834       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
13835         return false;
13836       if (size)
13837         size = c_fully_fold (size, false, NULL);
13838       OMP_CLAUSE_SIZE (c) = size;
13839       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13840           || (TREE_CODE (t) == COMPONENT_REF
13841               && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13842         return false;
13843       gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13844       switch (OMP_CLAUSE_MAP_KIND (c))
13845         {
13846         case GOMP_MAP_ALLOC:
13847         case GOMP_MAP_IF_PRESENT:
13848         case GOMP_MAP_TO:
13849         case GOMP_MAP_FROM:
13850         case GOMP_MAP_TOFROM:
13851         case GOMP_MAP_ALWAYS_TO:
13852         case GOMP_MAP_ALWAYS_FROM:
13853         case GOMP_MAP_ALWAYS_TOFROM:
13854         case GOMP_MAP_RELEASE:
13855         case GOMP_MAP_DELETE:
13856         case GOMP_MAP_FORCE_TO:
13857         case GOMP_MAP_FORCE_FROM:
13858         case GOMP_MAP_FORCE_TOFROM:
13859         case GOMP_MAP_FORCE_PRESENT:
13860           OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13861           break;
13862         default:
13863           break;
13864         }
13865       tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13866       if (TREE_CODE (t) == COMPONENT_REF)
13867         OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
13868       else
13869         OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13870       OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
13871       if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13872           && !c_mark_addressable (t))
13873         return false;
13874       OMP_CLAUSE_DECL (c2) = t;
13875       t = build_fold_addr_expr (first);
13876       t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13877       tree ptr = OMP_CLAUSE_DECL (c2);
13878       if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13879         ptr = build_fold_addr_expr (ptr);
13880       t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13881                            ptrdiff_type_node, t,
13882                            fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13883                                              ptrdiff_type_node, ptr));
13884       t = c_fully_fold (t, false, NULL);
13885       OMP_CLAUSE_SIZE (c2) = t;
13886       OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13887       OMP_CLAUSE_CHAIN (c) = c2;
13888     }
13889   return false;
13890 }
13891
13892 /* Helper function of finish_omp_clauses.  Clone STMT as if we were making
13893    an inline call.  But, remap
13894    the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13895    and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL.  */
13896
13897 static tree
13898 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13899                  tree decl, tree placeholder)
13900 {
13901   copy_body_data id;
13902   hash_map<tree, tree> decl_map;
13903
13904   decl_map.put (omp_decl1, placeholder);
13905   decl_map.put (omp_decl2, decl);
13906   memset (&id, 0, sizeof (id));
13907   id.src_fn = DECL_CONTEXT (omp_decl1);
13908   id.dst_fn = current_function_decl;
13909   id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13910   id.decl_map = &decl_map;
13911
13912   id.copy_decl = copy_decl_no_change;
13913   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13914   id.transform_new_cfg = true;
13915   id.transform_return_to_modify = false;
13916   id.eh_lp_nr = 0;
13917   walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13918   return stmt;
13919 }
13920
13921 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13922    Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP.  */
13923
13924 static tree
13925 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13926 {
13927   if (*tp == (tree) data)
13928     return *tp;
13929   return NULL_TREE;
13930 }
13931
13932 /* Similarly, but also walk aggregate fields.  */
13933
13934 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13935
13936 static tree
13937 c_find_omp_var_r (tree *tp, int *, void *data)
13938 {
13939   if (*tp == ((struct c_find_omp_var_s *) data)->var)
13940     return *tp;
13941   if (RECORD_OR_UNION_TYPE_P (*tp))
13942     {
13943       tree field;
13944       hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13945
13946       for (field = TYPE_FIELDS (*tp); field;
13947            field = DECL_CHAIN (field))
13948         if (TREE_CODE (field) == FIELD_DECL)
13949           {
13950             tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13951                                   c_find_omp_var_r, data, pset);
13952             if (ret)
13953               return ret;
13954             ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13955             if (ret)
13956               return ret;
13957             ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13958                              pset);
13959             if (ret)
13960               return ret;
13961             ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13962             if (ret)
13963               return ret;
13964           }
13965     }
13966   else if (INTEGRAL_TYPE_P (*tp))
13967     return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13968                       ((struct c_find_omp_var_s *) data)->pset);
13969   return NULL_TREE;
13970 }
13971
13972 /* Finish OpenMP iterators ITER.  Return true if they are errorneous
13973    and clauses containing them should be removed.  */
13974
13975 static bool
13976 c_omp_finish_iterators (tree iter)
13977 {
13978   bool ret = false;
13979   for (tree it = iter; it; it = TREE_CHAIN (it))
13980     {
13981       tree var = TREE_VEC_ELT (it, 0);
13982       tree begin = TREE_VEC_ELT (it, 1);
13983       tree end = TREE_VEC_ELT (it, 2);
13984       tree step = TREE_VEC_ELT (it, 3);
13985       tree orig_step;
13986       tree type = TREE_TYPE (var);
13987       location_t loc = DECL_SOURCE_LOCATION (var);
13988       if (type == error_mark_node)
13989         {
13990           ret = true;
13991           continue;
13992         }
13993       if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13994         {
13995           error_at (loc, "iterator %qD has neither integral nor pointer type",
13996                     var);
13997           ret = true;
13998           continue;
13999         }
14000       else if (TYPE_ATOMIC (type))
14001         {
14002           error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
14003           ret = true;
14004           continue;
14005         }
14006       else if (TYPE_READONLY (type))
14007         {
14008           error_at (loc, "iterator %qD has const qualified type", var);
14009           ret = true;
14010           continue;
14011         }
14012       else if (step == error_mark_node
14013                || TREE_TYPE (step) == error_mark_node)
14014         {
14015           ret = true;
14016           continue;
14017         }
14018       else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
14019         {
14020           error_at (EXPR_LOC_OR_LOC (step, loc),
14021                     "iterator step with non-integral type");
14022           ret = true;
14023           continue;
14024         }
14025       begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
14026       end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
14027       orig_step = save_expr (c_fully_fold (step, false, NULL));
14028       tree stype = POINTER_TYPE_P (type) ? sizetype : type;
14029       step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
14030       if (POINTER_TYPE_P (type))
14031         {
14032           begin = save_expr (begin);
14033           step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
14034           step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
14035                                   fold_convert (sizetype, step),
14036                                   fold_convert (sizetype, begin));
14037           step = fold_convert (ssizetype, step);
14038         }
14039       if (integer_zerop (step))
14040         {
14041           error_at (loc, "iterator %qD has zero step", var);
14042           ret = true;
14043           continue;
14044         }
14045
14046       if (begin == error_mark_node
14047           || end == error_mark_node
14048           || step == error_mark_node
14049           || orig_step == error_mark_node)
14050         {
14051           ret = true;
14052           continue;
14053         }
14054       hash_set<tree> pset;
14055       tree it2;
14056       for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
14057         {
14058           tree var2 = TREE_VEC_ELT (it2, 0);
14059           tree begin2 = TREE_VEC_ELT (it2, 1);
14060           tree end2 = TREE_VEC_ELT (it2, 2);
14061           tree step2 = TREE_VEC_ELT (it2, 3);
14062           tree type2 = TREE_TYPE (var2);
14063           location_t loc2 = DECL_SOURCE_LOCATION (var2);
14064           struct c_find_omp_var_s data = { var, &pset };
14065           if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
14066             {
14067               error_at (loc2,
14068                         "type of iterator %qD refers to outer iterator %qD",
14069                         var2, var);
14070               break;
14071             }
14072           else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
14073             {
14074               error_at (EXPR_LOC_OR_LOC (begin2, loc2),
14075                         "begin expression refers to outer iterator %qD", var);
14076               break;
14077             }
14078           else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
14079             {
14080               error_at (EXPR_LOC_OR_LOC (end2, loc2),
14081                         "end expression refers to outer iterator %qD", var);
14082               break;
14083             }
14084           else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
14085             {
14086               error_at (EXPR_LOC_OR_LOC (step2, loc2),
14087                         "step expression refers to outer iterator %qD", var);
14088               break;
14089             }
14090         }
14091       if (it2)
14092         {
14093           ret = true;
14094           continue;
14095         }
14096       TREE_VEC_ELT (it, 1) = begin;
14097       TREE_VEC_ELT (it, 2) = end;
14098       TREE_VEC_ELT (it, 3) = step;
14099       TREE_VEC_ELT (it, 4) = orig_step;
14100     }
14101   return ret;
14102 }
14103
14104 /* Ensure that pointers are used in OpenACC attach and detach clauses.
14105    Return true if an error has been detected.  */
14106
14107 static bool
14108 c_oacc_check_attachments (tree c)
14109 {
14110   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14111     return false;
14112
14113   /* OpenACC attach / detach clauses must be pointers.  */
14114   if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14115       || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
14116     {
14117       tree t = OMP_CLAUSE_DECL (c);
14118
14119       while (TREE_CODE (t) == TREE_LIST)
14120         t = TREE_CHAIN (t);
14121
14122       if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14123         {
14124           error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
14125                     user_omp_clause_code_name (c, true));
14126           return true;
14127         }
14128     }
14129
14130   return false;
14131 }
14132
14133 /* For all elements of CLAUSES, validate them against their constraints.
14134    Remove any elements from the list that are invalid.  */
14135
14136 tree
14137 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
14138 {
14139   bitmap_head generic_head, firstprivate_head, lastprivate_head;
14140   bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
14141   bitmap_head oacc_reduction_head, is_on_device_head;
14142   tree c, t, type, *pc;
14143   tree simdlen = NULL_TREE, safelen = NULL_TREE;
14144   bool branch_seen = false;
14145   bool copyprivate_seen = false;
14146   bool mergeable_seen = false;
14147   tree *detach_seen = NULL;
14148   bool linear_variable_step_check = false;
14149   tree *nowait_clause = NULL;
14150   tree ordered_clause = NULL_TREE;
14151   tree schedule_clause = NULL_TREE;
14152   bool oacc_async = false;
14153   bool indir_component_ref_p = false;
14154   tree last_iterators = NULL_TREE;
14155   bool last_iterators_remove = false;
14156   tree *nogroup_seen = NULL;
14157   tree *order_clause = NULL;
14158   /* 1 if normal/task reduction has been seen, -1 if inscan reduction
14159      has been seen, -2 if mixed inscan/normal reduction diagnosed.  */
14160   int reduction_seen = 0;
14161   bool allocate_seen = false;
14162   bool implicit_moved = false;
14163   bool target_in_reduction_seen = false;
14164
14165   bitmap_obstack_initialize (NULL);
14166   bitmap_initialize (&generic_head, &bitmap_default_obstack);
14167   bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
14168   bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
14169   bitmap_initialize (&aligned_head, &bitmap_default_obstack);
14170   /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead.  */
14171   bitmap_initialize (&map_head, &bitmap_default_obstack);
14172   bitmap_initialize (&map_field_head, &bitmap_default_obstack);
14173   bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
14174   /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
14175      instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head.  */
14176   bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
14177   bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
14178
14179   if (ort & C_ORT_ACC)
14180     for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
14181       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
14182         {
14183           oacc_async = true;
14184           break;
14185         }
14186
14187   for (pc = &clauses, c = clauses; c ; c = *pc)
14188     {
14189       bool remove = false;
14190       bool need_complete = false;
14191       bool need_implicitly_determined = false;
14192
14193       switch (OMP_CLAUSE_CODE (c))
14194         {
14195         case OMP_CLAUSE_SHARED:
14196           need_implicitly_determined = true;
14197           goto check_dup_generic;
14198
14199         case OMP_CLAUSE_PRIVATE:
14200           need_complete = true;
14201           need_implicitly_determined = true;
14202           goto check_dup_generic;
14203
14204         case OMP_CLAUSE_REDUCTION:
14205           if (reduction_seen == 0)
14206             reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
14207           else if (reduction_seen != -2
14208                    && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
14209                                          ? -1 : 1))
14210             {
14211               error_at (OMP_CLAUSE_LOCATION (c),
14212                         "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
14213                         "on the same construct");
14214               reduction_seen = -2;
14215             }
14216           /* FALLTHRU */
14217         case OMP_CLAUSE_IN_REDUCTION:
14218         case OMP_CLAUSE_TASK_REDUCTION:
14219           need_implicitly_determined = true;
14220           t = OMP_CLAUSE_DECL (c);
14221           if (TREE_CODE (t) == TREE_LIST)
14222             {
14223               if (handle_omp_array_sections (c, ort))
14224                 {
14225                   remove = true;
14226                   break;
14227                 }
14228
14229               t = OMP_CLAUSE_DECL (c);
14230               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
14231                   && OMP_CLAUSE_REDUCTION_INSCAN (c))
14232                 {
14233                   error_at (OMP_CLAUSE_LOCATION (c),
14234                             "%<inscan%> %<reduction%> clause with array "
14235                             "section");
14236                   remove = true;
14237                   break;
14238                 }
14239             }
14240           t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14241           if (t == error_mark_node)
14242             {
14243               remove = true;
14244               break;
14245             }
14246           if (oacc_async)
14247             c_mark_addressable (t);
14248           type = TREE_TYPE (t);
14249           if (TREE_CODE (t) == MEM_REF)
14250             type = TREE_TYPE (type);
14251           if (TREE_CODE (type) == ARRAY_TYPE)
14252             {
14253               tree oatype = type;
14254               gcc_assert (TREE_CODE (t) != MEM_REF);
14255               while (TREE_CODE (type) == ARRAY_TYPE)
14256                 type = TREE_TYPE (type);
14257               if (integer_zerop (TYPE_SIZE_UNIT (type)))
14258                 {
14259                   error_at (OMP_CLAUSE_LOCATION (c),
14260                             "%qD in %<reduction%> clause is a zero size array",
14261                             t);
14262                   remove = true;
14263                   break;
14264                 }
14265               tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14266                                       TYPE_SIZE_UNIT (type));
14267               if (integer_zerop (size))
14268                 {
14269                   error_at (OMP_CLAUSE_LOCATION (c),
14270                             "%qD in %<reduction%> clause is a zero size array",
14271                             t);
14272                   remove = true;
14273                   break;
14274                 }
14275               size = size_binop (MINUS_EXPR, size, size_one_node);
14276               size = save_expr (size);
14277               tree index_type = build_index_type (size);
14278               tree atype = build_array_type (TYPE_MAIN_VARIANT (type),
14279                                              index_type);
14280               atype = c_build_qualified_type (atype, TYPE_QUALS (type));
14281               tree ptype = build_pointer_type (type);
14282               if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14283                 t = build_fold_addr_expr (t);
14284               t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14285               OMP_CLAUSE_DECL (c) = t;
14286             }
14287           if (TYPE_ATOMIC (type))
14288             {
14289               error_at (OMP_CLAUSE_LOCATION (c),
14290                         "%<_Atomic%> %qE in %<reduction%> clause", t);
14291               remove = true;
14292               break;
14293             }
14294           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14295               || OMP_CLAUSE_REDUCTION_TASK (c))
14296             {
14297               /* Disallow zero sized or potentially zero sized task
14298                  reductions.  */
14299               if (integer_zerop (TYPE_SIZE_UNIT (type)))
14300                 {
14301                   error_at (OMP_CLAUSE_LOCATION (c),
14302                             "zero sized type %qT in %qs clause", type,
14303                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14304                   remove = true;
14305                   break;
14306                 }
14307               else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14308                 {
14309                   error_at (OMP_CLAUSE_LOCATION (c),
14310                             "variable sized type %qT in %qs clause", type,
14311                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14312                   remove = true;
14313                   break;
14314                 }
14315             }
14316           if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14317               && (FLOAT_TYPE_P (type)
14318                   || TREE_CODE (type) == COMPLEX_TYPE))
14319             {
14320               enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14321               const char *r_name = NULL;
14322
14323               switch (r_code)
14324                 {
14325                 case PLUS_EXPR:
14326                 case MULT_EXPR:
14327                 case MINUS_EXPR:
14328                 case TRUTH_ANDIF_EXPR:
14329                 case TRUTH_ORIF_EXPR:
14330                   break;
14331                 case MIN_EXPR:
14332                   if (TREE_CODE (type) == COMPLEX_TYPE)
14333                     r_name = "min";
14334                   break;
14335                 case MAX_EXPR:
14336                   if (TREE_CODE (type) == COMPLEX_TYPE)
14337                     r_name = "max";
14338                   break;
14339                 case BIT_AND_EXPR:
14340                   r_name = "&";
14341                   break;
14342                 case BIT_XOR_EXPR:
14343                   r_name = "^";
14344                   break;
14345                 case BIT_IOR_EXPR:
14346                   r_name = "|";
14347                   break;
14348                 default:
14349                   gcc_unreachable ();
14350                 }
14351               if (r_name)
14352                 {
14353                   error_at (OMP_CLAUSE_LOCATION (c),
14354                             "%qE has invalid type for %<reduction(%s)%>",
14355                             t, r_name);
14356                   remove = true;
14357                   break;
14358                 }
14359             }
14360           else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14361             {
14362               error_at (OMP_CLAUSE_LOCATION (c),
14363                         "user defined reduction not found for %qE", t);
14364               remove = true;
14365               break;
14366             }
14367           else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14368             {
14369               tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14370               type = TYPE_MAIN_VARIANT (type);
14371               tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14372                                              VAR_DECL, NULL_TREE, type);
14373               tree decl_placeholder = NULL_TREE;
14374               OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14375               DECL_ARTIFICIAL (placeholder) = 1;
14376               DECL_IGNORED_P (placeholder) = 1;
14377               if (TREE_CODE (t) == MEM_REF)
14378                 {
14379                   decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14380                                                  VAR_DECL, NULL_TREE, type);
14381                   OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14382                   DECL_ARTIFICIAL (decl_placeholder) = 1;
14383                   DECL_IGNORED_P (decl_placeholder) = 1;
14384                 }
14385               if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14386                 c_mark_addressable (placeholder);
14387               if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14388                 c_mark_addressable (decl_placeholder ? decl_placeholder
14389                                     : OMP_CLAUSE_DECL (c));
14390               OMP_CLAUSE_REDUCTION_MERGE (c)
14391                 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14392                                    TREE_VEC_ELT (list, 0),
14393                                    TREE_VEC_ELT (list, 1),
14394                                    decl_placeholder ? decl_placeholder
14395                                    : OMP_CLAUSE_DECL (c), placeholder);
14396               OMP_CLAUSE_REDUCTION_MERGE (c)
14397                 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14398                               void_type_node, NULL_TREE,
14399                               OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14400               TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14401               if (TREE_VEC_LENGTH (list) == 6)
14402                 {
14403                   if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14404                     c_mark_addressable (decl_placeholder ? decl_placeholder
14405                                         : OMP_CLAUSE_DECL (c));
14406                   if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14407                     c_mark_addressable (placeholder);
14408                   tree init = TREE_VEC_ELT (list, 5);
14409                   if (init == error_mark_node)
14410                     init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14411                   OMP_CLAUSE_REDUCTION_INIT (c)
14412                     = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14413                                        TREE_VEC_ELT (list, 3),
14414                                        decl_placeholder ? decl_placeholder
14415                                        : OMP_CLAUSE_DECL (c), placeholder);
14416                   if (TREE_VEC_ELT (list, 5) == error_mark_node)
14417                     {
14418                       tree v = decl_placeholder ? decl_placeholder : t;
14419                       OMP_CLAUSE_REDUCTION_INIT (c)
14420                         = build2 (INIT_EXPR, TREE_TYPE (v), v,
14421                                   OMP_CLAUSE_REDUCTION_INIT (c));
14422                     }
14423                   if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14424                                  c_find_omp_placeholder_r,
14425                                  placeholder, NULL))
14426                     OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14427                 }
14428               else
14429                 {
14430                   tree init;
14431                   tree v = decl_placeholder ? decl_placeholder : t;
14432                   if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14433                     init = build_constructor (TREE_TYPE (v), NULL);
14434                   else
14435                     init = fold_convert (TREE_TYPE (v), integer_zero_node);
14436                   OMP_CLAUSE_REDUCTION_INIT (c)
14437                     = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14438                 }
14439               OMP_CLAUSE_REDUCTION_INIT (c)
14440                 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14441                               void_type_node, NULL_TREE,
14442                                OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14443               TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14444             }
14445           if (TREE_CODE (t) == MEM_REF)
14446             {
14447               if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14448                   || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14449                      != INTEGER_CST)
14450                 {
14451                   sorry ("variable length element type in array "
14452                          "%<reduction%> clause");
14453                   remove = true;
14454                   break;
14455                 }
14456               t = TREE_OPERAND (t, 0);
14457               if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14458                 t = TREE_OPERAND (t, 0);
14459               if (TREE_CODE (t) == ADDR_EXPR)
14460                 t = TREE_OPERAND (t, 0);
14461             }
14462           goto check_dup_generic_t;
14463
14464         case OMP_CLAUSE_COPYPRIVATE:
14465           copyprivate_seen = true;
14466           if (nowait_clause)
14467             {
14468               error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14469                         "%<nowait%> clause must not be used together "
14470                         "with %<copyprivate%>");
14471               *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14472               nowait_clause = NULL;
14473             }
14474           goto check_dup_generic;
14475
14476         case OMP_CLAUSE_COPYIN:
14477           t = OMP_CLAUSE_DECL (c);
14478           if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14479             {
14480               error_at (OMP_CLAUSE_LOCATION (c),
14481                         "%qE must be %<threadprivate%> for %<copyin%>", t);
14482               remove = true;
14483               break;
14484             }
14485           goto check_dup_generic;
14486
14487         case OMP_CLAUSE_LINEAR:
14488           if (ort != C_ORT_OMP_DECLARE_SIMD)
14489             need_implicitly_determined = true;
14490           t = OMP_CLAUSE_DECL (c);
14491           if (ort != C_ORT_OMP_DECLARE_SIMD
14492               && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT
14493               && OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
14494             {
14495               error_at (OMP_CLAUSE_LOCATION (c),
14496                         "modifier should not be specified in %<linear%> "
14497                         "clause on %<simd%> or %<for%> constructs when not "
14498                         "using OpenMP 5.2 modifiers");
14499               OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14500             }
14501           if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14502               && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14503             {
14504               error_at (OMP_CLAUSE_LOCATION (c),
14505                         "linear clause applied to non-integral non-pointer "
14506                         "variable with type %qT", TREE_TYPE (t));
14507               remove = true;
14508               break;
14509             }
14510           if (TYPE_ATOMIC (TREE_TYPE (t)))
14511             {
14512               error_at (OMP_CLAUSE_LOCATION (c),
14513                         "%<_Atomic%> %qD in %<linear%> clause", t);
14514               remove = true;
14515               break;
14516             }
14517           if (ort == C_ORT_OMP_DECLARE_SIMD)
14518             {
14519               tree s = OMP_CLAUSE_LINEAR_STEP (c);
14520               if (TREE_CODE (s) == PARM_DECL)
14521                 {
14522                   OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14523                   /* map_head bitmap is used as uniform_head if
14524                      declare_simd.  */
14525                   if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14526                     linear_variable_step_check = true;
14527                   goto check_dup_generic;
14528                 }
14529               if (TREE_CODE (s) != INTEGER_CST)
14530                 {
14531                   error_at (OMP_CLAUSE_LOCATION (c),
14532                             "%<linear%> clause step %qE is neither constant "
14533                             "nor a parameter", s);
14534                   remove = true;
14535                   break;
14536                 }
14537             }
14538           if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14539             {
14540               tree s = OMP_CLAUSE_LINEAR_STEP (c);
14541               s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14542                                    OMP_CLAUSE_DECL (c), s);
14543               s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14544                                    sizetype, fold_convert (sizetype, s),
14545                                    fold_convert
14546                                      (sizetype, OMP_CLAUSE_DECL (c)));
14547               if (s == error_mark_node)
14548                 s = size_one_node;
14549               OMP_CLAUSE_LINEAR_STEP (c) = s;
14550             }
14551           else
14552             OMP_CLAUSE_LINEAR_STEP (c)
14553               = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14554           goto check_dup_generic;
14555
14556         check_dup_generic:
14557           t = OMP_CLAUSE_DECL (c);
14558         check_dup_generic_t:
14559           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14560             {
14561               error_at (OMP_CLAUSE_LOCATION (c),
14562                         "%qE is not a variable in clause %qs", t,
14563                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14564               remove = true;
14565             }
14566           else if ((ort == C_ORT_ACC
14567                     && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14568                    || (ort == C_ORT_OMP
14569                        && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14570                            || (OMP_CLAUSE_CODE (c)
14571                                == OMP_CLAUSE_USE_DEVICE_ADDR)))
14572                    || (ort == C_ORT_OMP_TARGET
14573                        && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
14574             {
14575               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
14576                   && (bitmap_bit_p (&generic_head, DECL_UID (t))
14577                       || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
14578                 {
14579                   error_at (OMP_CLAUSE_LOCATION (c),
14580                             "%qD appears more than once in data-sharing "
14581                             "clauses", t);
14582                   remove = true;
14583                   break;
14584                 }
14585               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
14586                 target_in_reduction_seen = true;
14587               if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14588                 {
14589                   error_at (OMP_CLAUSE_LOCATION (c),
14590                             ort == C_ORT_ACC
14591                             ? "%qD appears more than once in reduction clauses"
14592                             : "%qD appears more than once in data clauses",
14593                             t);
14594                   remove = true;
14595                 }
14596               else
14597                 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14598             }
14599           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14600                    || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14601                    || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
14602                    || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14603             {
14604               error_at (OMP_CLAUSE_LOCATION (c),
14605                         "%qE appears more than once in data clauses", t);
14606               remove = true;
14607             }
14608           else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14609                     || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
14610                     || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
14611                    && bitmap_bit_p (&map_head, DECL_UID (t)))
14612             {
14613               if (ort == C_ORT_ACC)
14614                 error_at (OMP_CLAUSE_LOCATION (c),
14615                           "%qD appears more than once in data clauses", t);
14616               else
14617                 error_at (OMP_CLAUSE_LOCATION (c),
14618                           "%qD appears both in data and map clauses", t);
14619               remove = true;
14620             }
14621           else
14622             bitmap_set_bit (&generic_head, DECL_UID (t));
14623           break;
14624
14625         case OMP_CLAUSE_FIRSTPRIVATE:
14626           if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
14627             {
14628             move_implicit:
14629               implicit_moved = true;
14630               /* Move firstprivate and map clauses with
14631                  OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
14632                  clauses chain.  */
14633               tree cl1 = NULL_TREE, cl2 = NULL_TREE;
14634               tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
14635               while (*pc1)
14636                 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
14637                     && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
14638                   {
14639                     *pc3 = *pc1;
14640                     pc3 = &OMP_CLAUSE_CHAIN (*pc3);
14641                     *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14642                   }
14643                 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
14644                          && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
14645                   {
14646                     *pc2 = *pc1;
14647                     pc2 = &OMP_CLAUSE_CHAIN (*pc2);
14648                     *pc1 = OMP_CLAUSE_CHAIN (*pc1);
14649                   }
14650                 else
14651                   pc1 = &OMP_CLAUSE_CHAIN (*pc1);
14652               *pc3 = NULL;
14653               *pc2 = cl2;
14654               *pc1 = cl1;
14655               continue;
14656             }
14657           t = OMP_CLAUSE_DECL (c);
14658           need_complete = true;
14659           need_implicitly_determined = true;
14660           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14661             {
14662               error_at (OMP_CLAUSE_LOCATION (c),
14663                         "%qE is not a variable in clause %<firstprivate%>", t);
14664               remove = true;
14665             }
14666           else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14667                    && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
14668                    && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14669             remove = true;
14670           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14671                    || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14672                    || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
14673             {
14674               error_at (OMP_CLAUSE_LOCATION (c),
14675                         "%qE appears more than once in data clauses", t);
14676               remove = true;
14677             }
14678           else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14679             {
14680               if (ort == C_ORT_ACC)
14681                 error_at (OMP_CLAUSE_LOCATION (c),
14682                           "%qD appears more than once in data clauses", t);
14683               else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
14684                        && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
14685                 /* Silently drop the clause.  */;
14686               else
14687                 error_at (OMP_CLAUSE_LOCATION (c),
14688                           "%qD appears both in data and map clauses", t);
14689               remove = true;
14690             }
14691           else
14692             bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14693           break;
14694
14695         case OMP_CLAUSE_LASTPRIVATE:
14696           t = OMP_CLAUSE_DECL (c);
14697           need_complete = true;
14698           need_implicitly_determined = true;
14699           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14700             {
14701               error_at (OMP_CLAUSE_LOCATION (c),
14702                         "%qE is not a variable in clause %<lastprivate%>", t);
14703               remove = true;
14704             }
14705           else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14706                    || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14707             {
14708               error_at (OMP_CLAUSE_LOCATION (c),
14709                      "%qE appears more than once in data clauses", t);
14710               remove = true;
14711             }
14712           else
14713             bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14714           break;
14715
14716         case OMP_CLAUSE_ALIGNED:
14717           t = OMP_CLAUSE_DECL (c);
14718           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14719             {
14720               error_at (OMP_CLAUSE_LOCATION (c),
14721                         "%qE is not a variable in %<aligned%> clause", t);
14722               remove = true;
14723             }
14724           else if (!POINTER_TYPE_P (TREE_TYPE (t))
14725                    && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14726             {
14727               error_at (OMP_CLAUSE_LOCATION (c),
14728                         "%qE in %<aligned%> clause is neither a pointer nor "
14729                         "an array", t);
14730               remove = true;
14731             }
14732           else if (TYPE_ATOMIC (TREE_TYPE (t)))
14733             {
14734               error_at (OMP_CLAUSE_LOCATION (c),
14735                         "%<_Atomic%> %qD in %<aligned%> clause", t);
14736               remove = true;
14737               break;
14738             }
14739           else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14740             {
14741               error_at (OMP_CLAUSE_LOCATION (c),
14742                         "%qE appears more than once in %<aligned%> clauses",
14743                         t);
14744               remove = true;
14745             }
14746           else
14747             bitmap_set_bit (&aligned_head, DECL_UID (t));
14748           break;
14749
14750         case OMP_CLAUSE_NONTEMPORAL:
14751           t = OMP_CLAUSE_DECL (c);
14752           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14753             {
14754               error_at (OMP_CLAUSE_LOCATION (c),
14755                         "%qE is not a variable in %<nontemporal%> clause", t);
14756               remove = true;
14757             }
14758           else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14759             {
14760               error_at (OMP_CLAUSE_LOCATION (c),
14761                         "%qE appears more than once in %<nontemporal%> "
14762                         "clauses", t);
14763               remove = true;
14764             }
14765           else
14766             bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14767           break;
14768
14769         case OMP_CLAUSE_ALLOCATE:
14770           t = OMP_CLAUSE_DECL (c);
14771           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14772             {
14773               error_at (OMP_CLAUSE_LOCATION (c),
14774                         "%qE is not a variable in %<allocate%> clause", t);
14775               remove = true;
14776             }
14777           else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14778             {
14779               warning_at (OMP_CLAUSE_LOCATION (c), 0,
14780                           "%qE appears more than once in %<allocate%> clauses",
14781                           t);
14782               remove = true;
14783             }
14784           else
14785             {
14786               bitmap_set_bit (&aligned_head, DECL_UID (t));
14787               if (!OMP_CLAUSE_ALLOCATE_COMBINED (c))
14788                 allocate_seen = true;
14789             }
14790           break;
14791
14792         case OMP_CLAUSE_DEPEND:
14793           t = OMP_CLAUSE_DECL (c);
14794           if (t == NULL_TREE)
14795             {
14796               gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14797                           == OMP_CLAUSE_DEPEND_SOURCE);
14798               break;
14799             }
14800           if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14801             {
14802               gcc_assert (TREE_CODE (t) == TREE_LIST);
14803               for (; t; t = TREE_CHAIN (t))
14804                 {
14805                   tree decl = TREE_VALUE (t);
14806                   if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14807                     {
14808                       tree offset = TREE_PURPOSE (t);
14809                       bool neg = wi::neg_p (wi::to_wide (offset));
14810                       offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14811                       tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14812                                                  neg ? MINUS_EXPR : PLUS_EXPR,
14813                                                  decl, offset);
14814                       t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14815                                             sizetype,
14816                                             fold_convert (sizetype, t2),
14817                                             fold_convert (sizetype, decl));
14818                       if (t2 == error_mark_node)
14819                         {
14820                           remove = true;
14821                           break;
14822                         }
14823                       TREE_PURPOSE (t) = t2;
14824                     }
14825                 }
14826               break;
14827             }
14828           /* FALLTHRU */
14829         case OMP_CLAUSE_AFFINITY:
14830           t = OMP_CLAUSE_DECL (c);
14831           if (TREE_CODE (t) == TREE_LIST
14832               && TREE_PURPOSE (t)
14833               && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14834             {
14835               if (TREE_PURPOSE (t) != last_iterators)
14836                 last_iterators_remove
14837                   = c_omp_finish_iterators (TREE_PURPOSE (t));
14838               last_iterators = TREE_PURPOSE (t);
14839               t = TREE_VALUE (t);
14840               if (last_iterators_remove)
14841                 t = error_mark_node;
14842             }
14843           else
14844             last_iterators = NULL_TREE;
14845           if (TREE_CODE (t) == TREE_LIST)
14846             {
14847               if (handle_omp_array_sections (c, ort))
14848                 remove = true;
14849               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14850                        && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14851                 {
14852                   error_at (OMP_CLAUSE_LOCATION (c),
14853                             "%<depend%> clause with %<depobj%> dependence "
14854                             "type on array section");
14855                   remove = true;
14856                 }
14857               break;
14858             }
14859           if (t == error_mark_node)
14860             remove = true;
14861           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14862                    && t == ridpointers[RID_OMP_ALL_MEMORY])
14863             {
14864               if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
14865                   && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
14866                 {
14867                   error_at (OMP_CLAUSE_LOCATION (c),
14868                             "%<omp_all_memory%> used with %<depend%> kind "
14869                             "other than %<out%> or %<inout%>");
14870                   remove = true;
14871                 }
14872             }
14873           else if (!lvalue_p (t))
14874             {
14875               error_at (OMP_CLAUSE_LOCATION (c),
14876                         "%qE is not lvalue expression nor array section in "
14877                         "%qs clause", t,
14878                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14879               remove = true;
14880             }
14881           else if (TREE_CODE (t) == COMPONENT_REF
14882                    && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14883             {
14884               gcc_assert (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14885                           || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY);
14886               error_at (OMP_CLAUSE_LOCATION (c),
14887                         "bit-field %qE in %qs clause", t,
14888                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14889               remove = true;
14890             }
14891           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14892                    && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14893             {
14894               if (!c_omp_depend_t_p (TREE_TYPE (t)))
14895                 {
14896                   error_at (OMP_CLAUSE_LOCATION (c),
14897                             "%qE does not have %<omp_depend_t%> type in "
14898                             "%<depend%> clause with %<depobj%> dependence "
14899                             "type", t);
14900                   remove = true;
14901                 }
14902             }
14903           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
14904                    && c_omp_depend_t_p (TREE_TYPE (t)))
14905             {
14906               error_at (OMP_CLAUSE_LOCATION (c),
14907                         "%qE should not have %<omp_depend_t%> type in "
14908                         "%<depend%> clause with dependence type other than "
14909                         "%<depobj%>", t);
14910               remove = true;
14911             }
14912           if (!remove)
14913             {
14914               if (t == ridpointers[RID_OMP_ALL_MEMORY])
14915                 t = null_pointer_node;
14916               else
14917                 {
14918                   tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c),
14919                                               ADDR_EXPR, t, false);
14920                   if (addr == error_mark_node)
14921                     {
14922                       remove = true;
14923                       break;
14924                     }
14925                   t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14926                                           RO_UNARY_STAR);
14927                   if (t == error_mark_node)
14928                     {
14929                       remove = true;
14930                       break;
14931                     }
14932                 }
14933               if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14934                   && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14935                   && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14936                       == TREE_VEC))
14937                 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14938               else
14939                 OMP_CLAUSE_DECL (c) = t;
14940             }
14941           break;
14942
14943         case OMP_CLAUSE_MAP:
14944           if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
14945             goto move_implicit;
14946           /* FALLTHRU */
14947         case OMP_CLAUSE_TO:
14948         case OMP_CLAUSE_FROM:
14949         case OMP_CLAUSE__CACHE_:
14950           t = OMP_CLAUSE_DECL (c);
14951           if (TREE_CODE (t) == TREE_LIST)
14952             {
14953               if (handle_omp_array_sections (c, ort))
14954                 remove = true;
14955               else
14956                 {
14957                   t = OMP_CLAUSE_DECL (c);
14958                   if (!omp_mappable_type (TREE_TYPE (t)))
14959                     {
14960                       error_at (OMP_CLAUSE_LOCATION (c),
14961                                 "array section does not have mappable type "
14962                                 "in %qs clause",
14963                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14964                       remove = true;
14965                     }
14966                   else if (TYPE_ATOMIC (TREE_TYPE (t)))
14967                     {
14968                       error_at (OMP_CLAUSE_LOCATION (c),
14969                                 "%<_Atomic%> %qE in %qs clause", t,
14970                                 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14971                       remove = true;
14972                     }
14973                   while (TREE_CODE (t) == ARRAY_REF)
14974                     t = TREE_OPERAND (t, 0);
14975                   if (TREE_CODE (t) == COMPONENT_REF
14976                       && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14977                     {
14978                       do
14979                         {
14980                           t = TREE_OPERAND (t, 0);
14981                           if (TREE_CODE (t) == MEM_REF
14982                               || TREE_CODE (t) == INDIRECT_REF)
14983                             {
14984                               t = TREE_OPERAND (t, 0);
14985                               STRIP_NOPS (t);
14986                               if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14987                                 t = TREE_OPERAND (t, 0);
14988                             }
14989                         }
14990                       while (TREE_CODE (t) == COMPONENT_REF
14991                              || TREE_CODE (t) == ARRAY_REF);
14992
14993                       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14994                           && OMP_CLAUSE_MAP_IMPLICIT (c)
14995                           && (bitmap_bit_p (&map_head, DECL_UID (t))
14996                               || bitmap_bit_p (&map_field_head, DECL_UID (t))
14997                               || bitmap_bit_p (&map_firstprivate_head,
14998                                                DECL_UID (t))))
14999                         {
15000                           remove = true;
15001                           break;
15002                         }
15003                       if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
15004                         break;
15005                       if (bitmap_bit_p (&map_head, DECL_UID (t)))
15006                         {
15007                           if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15008                             error_at (OMP_CLAUSE_LOCATION (c),
15009                                       "%qD appears more than once in motion "
15010                                       "clauses", t);
15011                           else if (ort == C_ORT_ACC)
15012                             error_at (OMP_CLAUSE_LOCATION (c),
15013                                       "%qD appears more than once in data "
15014                                       "clauses", t);
15015                           else
15016                             error_at (OMP_CLAUSE_LOCATION (c),
15017                                       "%qD appears more than once in map "
15018                                       "clauses", t);
15019                           remove = true;
15020                         }
15021                       else
15022                         {
15023                           bitmap_set_bit (&map_head, DECL_UID (t));
15024                           bitmap_set_bit (&map_field_head, DECL_UID (t));
15025                         }
15026                     }
15027                 }
15028               if (c_oacc_check_attachments (c))
15029                 remove = true;
15030               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15031                   && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15032                       || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15033                 /* In this case, we have a single array element which is a
15034                    pointer, and we already set OMP_CLAUSE_SIZE in
15035                    handle_omp_array_sections above.  For attach/detach clauses,
15036                    reset the OMP_CLAUSE_SIZE (representing a bias) to zero
15037                    here.  */
15038                 OMP_CLAUSE_SIZE (c) = size_zero_node;
15039               break;
15040             }
15041           if (t == error_mark_node)
15042             {
15043               remove = true;
15044               break;
15045             }
15046           /* OpenACC attach / detach clauses must be pointers.  */
15047           if (c_oacc_check_attachments (c))
15048             {
15049               remove = true;
15050               break;
15051             }
15052           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15053               && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
15054                   || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
15055             /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
15056                bias) to zero here, so it is not set erroneously to the pointer
15057                size later on in gimplify.cc.  */
15058             OMP_CLAUSE_SIZE (c) = size_zero_node;
15059           while (TREE_CODE (t) == INDIRECT_REF
15060                  || TREE_CODE (t) == ARRAY_REF)
15061             {
15062               t = TREE_OPERAND (t, 0);
15063               STRIP_NOPS (t);
15064               if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15065                 t = TREE_OPERAND (t, 0);
15066             }
15067           while (TREE_CODE (t) == COMPOUND_EXPR)
15068             {
15069               t = TREE_OPERAND (t, 1);
15070               STRIP_NOPS (t);
15071             }
15072           indir_component_ref_p = false;
15073           if (TREE_CODE (t) == COMPONENT_REF
15074               && (TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
15075                   || TREE_CODE (TREE_OPERAND (t, 0)) == INDIRECT_REF
15076                   || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
15077             {
15078               t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
15079               indir_component_ref_p = true;
15080               STRIP_NOPS (t);
15081               if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15082                 t = TREE_OPERAND (t, 0);
15083             }
15084
15085           if (TREE_CODE (t) == COMPONENT_REF
15086               && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
15087             {
15088               if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
15089                 {
15090                   error_at (OMP_CLAUSE_LOCATION (c),
15091                             "bit-field %qE in %qs clause",
15092                             t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15093                   remove = true;
15094                 }
15095               else if (!omp_mappable_type (TREE_TYPE (t)))
15096                 {
15097                   error_at (OMP_CLAUSE_LOCATION (c),
15098                             "%qE does not have a mappable type in %qs clause",
15099                             t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15100                   remove = true;
15101                 }
15102               else if (TYPE_ATOMIC (TREE_TYPE (t)))
15103                 {
15104                   error_at (OMP_CLAUSE_LOCATION (c),
15105                             "%<_Atomic%> %qE in %qs clause", t,
15106                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15107                   remove = true;
15108                 }
15109               while (TREE_CODE (t) == COMPONENT_REF)
15110                 {
15111                   if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
15112                       == UNION_TYPE)
15113                     {
15114                       error_at (OMP_CLAUSE_LOCATION (c),
15115                                 "%qE is a member of a union", t);
15116                       remove = true;
15117                       break;
15118                     }
15119                   t = TREE_OPERAND (t, 0);
15120                   if (TREE_CODE (t) == MEM_REF)
15121                     {
15122                       if (maybe_ne (mem_ref_offset (t), 0))
15123                         error_at (OMP_CLAUSE_LOCATION (c),
15124                                   "cannot dereference %qE in %qs clause", t,
15125                                   omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15126                       else
15127                         t = TREE_OPERAND (t, 0);
15128                     }
15129                   while (TREE_CODE (t) == MEM_REF
15130                          || TREE_CODE (t) == INDIRECT_REF
15131                          || TREE_CODE (t) == ARRAY_REF)
15132                     {
15133                       t = TREE_OPERAND (t, 0);
15134                       STRIP_NOPS (t);
15135                       if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15136                         t = TREE_OPERAND (t, 0);
15137                     }
15138                 }
15139               if (remove)
15140                 break;
15141               if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15142                 {
15143                   if (bitmap_bit_p (&map_field_head, DECL_UID (t))
15144                       || (ort != C_ORT_ACC
15145                           && bitmap_bit_p (&map_head, DECL_UID (t))))
15146                     break;
15147                 }
15148             }
15149           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15150             {
15151               error_at (OMP_CLAUSE_LOCATION (c),
15152                         "%qE is not a variable in %qs clause", t,
15153                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15154               remove = true;
15155             }
15156           else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15157             {
15158               error_at (OMP_CLAUSE_LOCATION (c),
15159                         "%qD is threadprivate variable in %qs clause", t,
15160                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15161               remove = true;
15162             }
15163           else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
15164                     || (OMP_CLAUSE_MAP_KIND (c)
15165                         != GOMP_MAP_FIRSTPRIVATE_POINTER))
15166                    && !indir_component_ref_p
15167                    && !c_mark_addressable (t))
15168             remove = true;
15169           else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15170                      && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
15171                          || (OMP_CLAUSE_MAP_KIND (c)
15172                              == GOMP_MAP_FIRSTPRIVATE_POINTER)
15173                          || (OMP_CLAUSE_MAP_KIND (c)
15174                              == GOMP_MAP_FORCE_DEVICEPTR)))
15175                    && t == OMP_CLAUSE_DECL (c)
15176                    && !omp_mappable_type (TREE_TYPE (t)))
15177             {
15178               error_at (OMP_CLAUSE_LOCATION (c),
15179                         "%qD does not have a mappable type in %qs clause", t,
15180                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15181               remove = true;
15182             }
15183           else if (TREE_TYPE (t) == error_mark_node)
15184             remove = true;
15185           else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
15186             {
15187               error_at (OMP_CLAUSE_LOCATION (c),
15188                         "%<_Atomic%> %qE in %qs clause", t,
15189                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15190               remove = true;
15191             }
15192           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15193                    && OMP_CLAUSE_MAP_IMPLICIT (c)
15194                    && (bitmap_bit_p (&map_head, DECL_UID (t))
15195                        || bitmap_bit_p (&map_field_head, DECL_UID (t))
15196                        || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t))))
15197             remove = true;
15198           else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
15199                    && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
15200             {
15201               if (bitmap_bit_p (&generic_head, DECL_UID (t))
15202                   || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15203                   || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
15204                 {
15205                   error_at (OMP_CLAUSE_LOCATION (c),
15206                             "%qD appears more than once in data clauses", t);
15207                   remove = true;
15208                 }
15209               else if (bitmap_bit_p (&map_head, DECL_UID (t))
15210                        && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15211                 {
15212                   if (ort == C_ORT_ACC)
15213                     error_at (OMP_CLAUSE_LOCATION (c),
15214                               "%qD appears more than once in data clauses", t);
15215                   else
15216                     error_at (OMP_CLAUSE_LOCATION (c),
15217                               "%qD appears both in data and map clauses", t);
15218                   remove = true;
15219                 }
15220               else
15221                 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
15222             }
15223           else if (bitmap_bit_p (&map_head, DECL_UID (t))
15224                    && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
15225             {
15226               if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
15227                 error_at (OMP_CLAUSE_LOCATION (c),
15228                           "%qD appears more than once in motion clauses", t);
15229               else if (ort == C_ORT_ACC)
15230                 error_at (OMP_CLAUSE_LOCATION (c),
15231                           "%qD appears more than once in data clauses", t);
15232               else
15233                 error_at (OMP_CLAUSE_LOCATION (c),
15234                           "%qD appears more than once in map clauses", t);
15235               remove = true;
15236             }
15237           else if (ort == C_ORT_ACC
15238                    && bitmap_bit_p (&generic_head, DECL_UID (t)))
15239             {
15240               error_at (OMP_CLAUSE_LOCATION (c),
15241                         "%qD appears more than once in data clauses", t);
15242               remove = true;
15243             }
15244           else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
15245                    || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
15246             {
15247               if (ort == C_ORT_ACC)
15248                 error_at (OMP_CLAUSE_LOCATION (c),
15249                           "%qD appears more than once in data clauses", t);
15250               else
15251                 error_at (OMP_CLAUSE_LOCATION (c),
15252                           "%qD appears both in data and map clauses", t);
15253               remove = true;
15254             }
15255           else
15256             {
15257               bitmap_set_bit (&map_head, DECL_UID (t));
15258               if (t != OMP_CLAUSE_DECL (c)
15259                   && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
15260                 bitmap_set_bit (&map_field_head, DECL_UID (t));
15261             }
15262           break;
15263
15264         case OMP_CLAUSE_ENTER:
15265         case OMP_CLAUSE_LINK:
15266           t = OMP_CLAUSE_DECL (c);
15267           const char *cname;
15268           cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
15269           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
15270               && OMP_CLAUSE_ENTER_TO (c))
15271             cname = "to";
15272           if (TREE_CODE (t) == FUNCTION_DECL
15273               && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15274             ;
15275           else if (!VAR_P (t))
15276             {
15277               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
15278                 error_at (OMP_CLAUSE_LOCATION (c),
15279                           "%qE is neither a variable nor a function name in "
15280                           "clause %qs", t, cname);
15281               else
15282                 error_at (OMP_CLAUSE_LOCATION (c),
15283                           "%qE is not a variable in clause %qs", t, cname);
15284               remove = true;
15285             }
15286           else if (DECL_THREAD_LOCAL_P (t))
15287             {
15288               error_at (OMP_CLAUSE_LOCATION (c),
15289                         "%qD is threadprivate variable in %qs clause", t,
15290                         cname);
15291               remove = true;
15292             }
15293           else if (!omp_mappable_type (TREE_TYPE (t)))
15294             {
15295               error_at (OMP_CLAUSE_LOCATION (c),
15296                         "%qD does not have a mappable type in %qs clause", t,
15297                         cname);
15298               remove = true;
15299             }
15300           if (remove)
15301             break;
15302           if (bitmap_bit_p (&generic_head, DECL_UID (t)))
15303             {
15304               error_at (OMP_CLAUSE_LOCATION (c),
15305                         "%qE appears more than once on the same "
15306                         "%<declare target%> directive", t);
15307               remove = true;
15308             }
15309           else
15310             bitmap_set_bit (&generic_head, DECL_UID (t));
15311           break;
15312
15313         case OMP_CLAUSE_UNIFORM:
15314           t = OMP_CLAUSE_DECL (c);
15315           if (TREE_CODE (t) != PARM_DECL)
15316             {
15317               if (DECL_P (t))
15318                 error_at (OMP_CLAUSE_LOCATION (c),
15319                           "%qD is not an argument in %<uniform%> clause", t);
15320               else
15321                 error_at (OMP_CLAUSE_LOCATION (c),
15322                           "%qE is not an argument in %<uniform%> clause", t);
15323               remove = true;
15324               break;
15325             }
15326           /* map_head bitmap is used as uniform_head if declare_simd.  */
15327           bitmap_set_bit (&map_head, DECL_UID (t));
15328           goto check_dup_generic;
15329
15330         case OMP_CLAUSE_IS_DEVICE_PTR:
15331         case OMP_CLAUSE_USE_DEVICE_PTR:
15332           t = OMP_CLAUSE_DECL (c);
15333           if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
15334             bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15335           if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
15336             {
15337               if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
15338                   && ort != C_ORT_ACC)
15339                 {
15340                   error_at (OMP_CLAUSE_LOCATION (c),
15341                             "%qs variable is not a pointer",
15342                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15343                   remove = true;
15344                 }
15345               else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
15346                 {
15347                   error_at (OMP_CLAUSE_LOCATION (c),
15348                             "%qs variable is neither a pointer nor an array",
15349                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15350                   remove = true;
15351                 }
15352             }
15353           goto check_dup_generic;
15354
15355         case OMP_CLAUSE_HAS_DEVICE_ADDR:
15356           t = OMP_CLAUSE_DECL (c);
15357           if (TREE_CODE (t) == TREE_LIST)
15358             {
15359               if (handle_omp_array_sections (c, ort))
15360                 remove = true;
15361               else
15362                 {
15363                   t = OMP_CLAUSE_DECL (c);
15364                   while (TREE_CODE (t) == ARRAY_REF)
15365                     t = TREE_OPERAND (t, 0);
15366                 }
15367             }
15368           bitmap_set_bit (&is_on_device_head, DECL_UID (t));
15369           if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15370             c_mark_addressable (t);
15371           goto check_dup_generic_t;
15372
15373         case OMP_CLAUSE_USE_DEVICE_ADDR:
15374           t = OMP_CLAUSE_DECL (c);
15375           if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
15376             c_mark_addressable (t);
15377           goto check_dup_generic;
15378
15379         case OMP_CLAUSE_NOWAIT:
15380           if (copyprivate_seen)
15381             {
15382               error_at (OMP_CLAUSE_LOCATION (c),
15383                         "%<nowait%> clause must not be used together "
15384                         "with %<copyprivate%>");
15385               remove = true;
15386               break;
15387             }
15388           nowait_clause = pc;
15389           pc = &OMP_CLAUSE_CHAIN (c);
15390           continue;
15391
15392         case OMP_CLAUSE_ORDER:
15393           if (ordered_clause)
15394             {
15395               error_at (OMP_CLAUSE_LOCATION (c),
15396                         "%<order%> clause must not be used together "
15397                         "with %<ordered%>");
15398               remove = true;
15399               break;
15400             }
15401           else if (order_clause)
15402             {
15403               /* Silently remove duplicates.  */
15404               remove = true;
15405               break;
15406             }
15407           order_clause = pc;
15408           pc = &OMP_CLAUSE_CHAIN (c);
15409           continue;
15410
15411         case OMP_CLAUSE_DETACH:
15412           t = OMP_CLAUSE_DECL (c);
15413           if (detach_seen)
15414             {
15415               error_at (OMP_CLAUSE_LOCATION (c),
15416                         "too many %qs clauses on a task construct",
15417                         "detach");
15418               remove = true;
15419               break;
15420             }
15421           detach_seen = pc;
15422           pc = &OMP_CLAUSE_CHAIN (c);
15423           c_mark_addressable (t);
15424           continue;
15425
15426         case OMP_CLAUSE_IF:
15427         case OMP_CLAUSE_NUM_THREADS:
15428         case OMP_CLAUSE_NUM_TEAMS:
15429         case OMP_CLAUSE_THREAD_LIMIT:
15430         case OMP_CLAUSE_DEFAULT:
15431         case OMP_CLAUSE_UNTIED:
15432         case OMP_CLAUSE_COLLAPSE:
15433         case OMP_CLAUSE_FINAL:
15434         case OMP_CLAUSE_DEVICE:
15435         case OMP_CLAUSE_DIST_SCHEDULE:
15436         case OMP_CLAUSE_PARALLEL:
15437         case OMP_CLAUSE_FOR:
15438         case OMP_CLAUSE_SECTIONS:
15439         case OMP_CLAUSE_TASKGROUP:
15440         case OMP_CLAUSE_PROC_BIND:
15441         case OMP_CLAUSE_DEVICE_TYPE:
15442         case OMP_CLAUSE_PRIORITY:
15443         case OMP_CLAUSE_GRAINSIZE:
15444         case OMP_CLAUSE_NUM_TASKS:
15445         case OMP_CLAUSE_THREADS:
15446         case OMP_CLAUSE_SIMD:
15447         case OMP_CLAUSE_HINT:
15448         case OMP_CLAUSE_FILTER:
15449         case OMP_CLAUSE_DEFAULTMAP:
15450         case OMP_CLAUSE_BIND:
15451         case OMP_CLAUSE_NUM_GANGS:
15452         case OMP_CLAUSE_NUM_WORKERS:
15453         case OMP_CLAUSE_VECTOR_LENGTH:
15454         case OMP_CLAUSE_ASYNC:
15455         case OMP_CLAUSE_WAIT:
15456         case OMP_CLAUSE_AUTO:
15457         case OMP_CLAUSE_INDEPENDENT:
15458         case OMP_CLAUSE_SEQ:
15459         case OMP_CLAUSE_GANG:
15460         case OMP_CLAUSE_WORKER:
15461         case OMP_CLAUSE_VECTOR:
15462         case OMP_CLAUSE_TILE:
15463         case OMP_CLAUSE_IF_PRESENT:
15464         case OMP_CLAUSE_FINALIZE:
15465         case OMP_CLAUSE_NOHOST:
15466           pc = &OMP_CLAUSE_CHAIN (c);
15467           continue;
15468
15469         case OMP_CLAUSE_MERGEABLE:
15470           mergeable_seen = true;
15471           pc = &OMP_CLAUSE_CHAIN (c);
15472           continue;
15473
15474         case OMP_CLAUSE_NOGROUP:
15475           nogroup_seen = pc;
15476           pc = &OMP_CLAUSE_CHAIN (c);
15477           continue;
15478
15479         case OMP_CLAUSE_SCHEDULE:
15480           schedule_clause = c;
15481           pc = &OMP_CLAUSE_CHAIN (c);
15482           continue;
15483
15484         case OMP_CLAUSE_ORDERED:
15485           ordered_clause = c;
15486           if (order_clause)
15487             {
15488               error_at (OMP_CLAUSE_LOCATION (*order_clause),
15489                         "%<order%> clause must not be used together "
15490                         "with %<ordered%>");
15491               *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
15492               order_clause = NULL;
15493             }
15494           pc = &OMP_CLAUSE_CHAIN (c);
15495           continue;
15496
15497         case OMP_CLAUSE_SAFELEN:
15498           safelen = c;
15499           pc = &OMP_CLAUSE_CHAIN (c);
15500           continue;
15501         case OMP_CLAUSE_SIMDLEN:
15502           simdlen = c;
15503           pc = &OMP_CLAUSE_CHAIN (c);
15504           continue;
15505
15506         case OMP_CLAUSE_INBRANCH:
15507         case OMP_CLAUSE_NOTINBRANCH:
15508           if (branch_seen)
15509             {
15510               error_at (OMP_CLAUSE_LOCATION (c),
15511                         "%<inbranch%> clause is incompatible with "
15512                         "%<notinbranch%>");
15513               remove = true;
15514               break;
15515             }
15516           branch_seen = true;
15517           pc = &OMP_CLAUSE_CHAIN (c);
15518           continue;
15519
15520         case OMP_CLAUSE_INCLUSIVE:
15521         case OMP_CLAUSE_EXCLUSIVE:
15522           need_complete = true;
15523           need_implicitly_determined = true;
15524           t = OMP_CLAUSE_DECL (c);
15525           if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15526             {
15527               error_at (OMP_CLAUSE_LOCATION (c),
15528                         "%qE is not a variable in clause %qs", t,
15529                         omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15530               remove = true;
15531             }
15532           break;
15533
15534         default:
15535           gcc_unreachable ();
15536         }
15537
15538       if (!remove)
15539         {
15540           t = OMP_CLAUSE_DECL (c);
15541
15542           if (need_complete)
15543             {
15544               t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15545               if (t == error_mark_node)
15546                 remove = true;
15547             }
15548
15549           if (need_implicitly_determined)
15550             {
15551               const char *share_name = NULL;
15552
15553               if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15554                 share_name = "threadprivate";
15555               else switch (c_omp_predetermined_sharing (t))
15556                 {
15557                 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15558                   break;
15559                 case OMP_CLAUSE_DEFAULT_SHARED:
15560                   if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15561                        || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15562                       && c_omp_predefined_variable (t))
15563                     /* The __func__ variable and similar function-local
15564                        predefined variables may be listed in a shared or
15565                        firstprivate clause.  */
15566                     break;
15567                   share_name = "shared";
15568                   break;
15569                 case OMP_CLAUSE_DEFAULT_PRIVATE:
15570                   share_name = "private";
15571                   break;
15572                 default:
15573                   gcc_unreachable ();
15574                 }
15575               if (share_name)
15576                 {
15577                   error_at (OMP_CLAUSE_LOCATION (c),
15578                             "%qE is predetermined %qs for %qs",
15579                             t, share_name,
15580                             omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15581                   remove = true;
15582                 }
15583               else if (TREE_READONLY (t)
15584                        && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15585                        && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15586                 {
15587                   error_at (OMP_CLAUSE_LOCATION (c),
15588                             "%<const%> qualified %qE may appear only in "
15589                             "%<shared%> or %<firstprivate%> clauses", t);
15590                   remove = true;
15591                 }
15592             }
15593         }
15594
15595       if (remove)
15596         *pc = OMP_CLAUSE_CHAIN (c);
15597       else
15598         pc = &OMP_CLAUSE_CHAIN (c);
15599     }
15600
15601   if (simdlen
15602       && safelen
15603       && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15604                           OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15605     {
15606       error_at (OMP_CLAUSE_LOCATION (simdlen),
15607                 "%<simdlen%> clause value is bigger than "
15608                 "%<safelen%> clause value");
15609       OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15610         = OMP_CLAUSE_SAFELEN_EXPR (safelen);
15611     }
15612
15613   if (ordered_clause
15614       && schedule_clause
15615       && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15616           & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15617     {
15618       error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15619                 "%<nonmonotonic%> schedule modifier specified together "
15620                 "with %<ordered%> clause");
15621       OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15622         = (enum omp_clause_schedule_kind)
15623           (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15624            & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15625     }
15626
15627   if (reduction_seen < 0 && ordered_clause)
15628     {
15629       error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15630                 "%qs clause specified together with %<inscan%> "
15631                 "%<reduction%> clause", "ordered");
15632       reduction_seen = -2;
15633     }
15634
15635   if (reduction_seen < 0 && schedule_clause)
15636     {
15637       error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15638                 "%qs clause specified together with %<inscan%> "
15639                 "%<reduction%> clause", "schedule");
15640       reduction_seen = -2;
15641     }
15642
15643   if (linear_variable_step_check
15644       || reduction_seen == -2
15645       || allocate_seen
15646       || target_in_reduction_seen)
15647     for (pc = &clauses, c = clauses; c ; c = *pc)
15648       {
15649         bool remove = false;
15650         if (allocate_seen)
15651           switch (OMP_CLAUSE_CODE (c))
15652             {
15653             case OMP_CLAUSE_REDUCTION:
15654             case OMP_CLAUSE_IN_REDUCTION:
15655             case OMP_CLAUSE_TASK_REDUCTION:
15656               if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
15657                 {
15658                   t = TREE_OPERAND (OMP_CLAUSE_DECL (c), 0);
15659                   if (TREE_CODE (t) == POINTER_PLUS_EXPR)
15660                     t = TREE_OPERAND (t, 0);
15661                   if (TREE_CODE (t) == ADDR_EXPR
15662                       || TREE_CODE (t) == INDIRECT_REF)
15663                     t = TREE_OPERAND (t, 0);
15664                   if (DECL_P (t))
15665                     bitmap_clear_bit (&aligned_head, DECL_UID (t));
15666                   break;
15667                 }
15668               /* FALLTHRU */
15669             case OMP_CLAUSE_PRIVATE:
15670             case OMP_CLAUSE_FIRSTPRIVATE:
15671             case OMP_CLAUSE_LASTPRIVATE:
15672             case OMP_CLAUSE_LINEAR:
15673               if (DECL_P (OMP_CLAUSE_DECL (c)))
15674                 bitmap_clear_bit (&aligned_head,
15675                                   DECL_UID (OMP_CLAUSE_DECL (c)));
15676               break;
15677             default:
15678               break;
15679             }
15680         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15681             && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15682             && !bitmap_bit_p (&map_head,
15683                               DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15684           {
15685             error_at (OMP_CLAUSE_LOCATION (c),
15686                       "%<linear%> clause step is a parameter %qD not "
15687                       "specified in %<uniform%> clause",
15688                       OMP_CLAUSE_LINEAR_STEP (c));
15689             remove = true;
15690           }
15691         else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
15692                  && reduction_seen == -2)
15693           OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15694         if (target_in_reduction_seen
15695             && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
15696           {
15697             tree t = OMP_CLAUSE_DECL (c);
15698             while (handled_component_p (t)
15699                    || TREE_CODE (t) == INDIRECT_REF
15700                    || TREE_CODE (t) == ADDR_EXPR
15701                    || TREE_CODE (t) == MEM_REF
15702                    || TREE_CODE (t) == NON_LVALUE_EXPR)
15703               t = TREE_OPERAND (t, 0);
15704             if (DECL_P (t)
15705                 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
15706               OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
15707           }
15708
15709         if (remove)
15710           *pc = OMP_CLAUSE_CHAIN (c);
15711         else
15712           pc = &OMP_CLAUSE_CHAIN (c);
15713       }
15714
15715   if (allocate_seen)
15716     for (pc = &clauses, c = clauses; c ; c = *pc)
15717       {
15718         bool remove = false;
15719         if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
15720             && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
15721             && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
15722           {
15723             error_at (OMP_CLAUSE_LOCATION (c),
15724                       "%qD specified in %<allocate%> clause but not in "
15725                       "an explicit privatization clause", OMP_CLAUSE_DECL (c));
15726             remove = true;
15727           }
15728         if (remove)
15729           *pc = OMP_CLAUSE_CHAIN (c);
15730         else
15731           pc = &OMP_CLAUSE_CHAIN (c);
15732       }
15733
15734   if (nogroup_seen && reduction_seen)
15735     {
15736       error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15737                 "%<nogroup%> clause must not be used together with "
15738                 "%<reduction%> clause");
15739       *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15740     }
15741
15742   if (detach_seen)
15743     {
15744       if (mergeable_seen)
15745         {
15746           error_at (OMP_CLAUSE_LOCATION (*detach_seen),
15747                     "%<detach%> clause must not be used together with "
15748                     "%<mergeable%> clause");
15749           *detach_seen = OMP_CLAUSE_CHAIN (*detach_seen);
15750         }
15751       else
15752         {
15753           tree detach_decl = OMP_CLAUSE_DECL (*detach_seen);
15754
15755           for (pc = &clauses, c = clauses; c ; c = *pc)
15756             {
15757               bool remove = false;
15758               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15759                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
15760                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
15761                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
15762                   && OMP_CLAUSE_DECL (c) == detach_decl)
15763                 {
15764                   error_at (OMP_CLAUSE_LOCATION (c),
15765                             "the event handle of a %<detach%> clause "
15766                             "should not be in a data-sharing clause");
15767                   remove = true;
15768                 }
15769               if (remove)
15770                 *pc = OMP_CLAUSE_CHAIN (c);
15771               else
15772                 pc = &OMP_CLAUSE_CHAIN (c);
15773             }
15774         }
15775     }
15776
15777   bitmap_obstack_release (NULL);
15778   return clauses;
15779 }
15780
15781 /* Return code to initialize DST with a copy constructor from SRC.
15782    C doesn't have copy constructors nor assignment operators, only for
15783    _Atomic vars we need to perform __atomic_load from src into a temporary
15784    followed by __atomic_store of the temporary to dst.  */
15785
15786 tree
15787 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15788 {
15789   if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15790     return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15791
15792   location_t loc = OMP_CLAUSE_LOCATION (clause);
15793   tree type = TREE_TYPE (dst);
15794   tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15795   tree tmp = create_tmp_var (nonatomic_type);
15796   tree tmp_addr = build_fold_addr_expr (tmp);
15797   TREE_ADDRESSABLE (tmp) = 1;
15798   suppress_warning (tmp);
15799   tree src_addr = build_fold_addr_expr (src);
15800   tree dst_addr = build_fold_addr_expr (dst);
15801   tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15802   vec<tree, va_gc> *params;
15803   /* Expansion of a generic atomic load may require an addition
15804      element, so allocate enough to prevent a resize.  */
15805   vec_alloc (params, 4);
15806
15807   /* Build __atomic_load (&src, &tmp, SEQ_CST);  */
15808   tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15809   params->quick_push (src_addr);
15810   params->quick_push (tmp_addr);
15811   params->quick_push (seq_cst);
15812   tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15813
15814   vec_alloc (params, 4);
15815
15816   /* Build __atomic_store (&dst, &tmp, SEQ_CST);  */
15817   fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15818   params->quick_push (dst_addr);
15819   params->quick_push (tmp_addr);
15820   params->quick_push (seq_cst);
15821   tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15822   return build2 (COMPOUND_EXPR, void_type_node, load, store);
15823 }
15824
15825 /* Create a transaction node.  */
15826
15827 tree
15828 c_finish_transaction (location_t loc, tree block, int flags)
15829 {
15830   tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15831   if (flags & TM_STMT_ATTR_OUTER)
15832     TRANSACTION_EXPR_OUTER (stmt) = 1;
15833   if (flags & TM_STMT_ATTR_RELAXED)
15834     TRANSACTION_EXPR_RELAXED (stmt) = 1;
15835   return add_stmt (stmt);
15836 }
15837
15838 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15839    down to the element type of an array.  If ORIG_QUAL_TYPE is not
15840    NULL, then it should be used as the qualified type
15841    ORIG_QUAL_INDIRECT levels down in array type derivation (to
15842    preserve information about the typedef name from which an array
15843    type was derived).  */
15844
15845 tree
15846 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15847                         size_t orig_qual_indirect)
15848 {
15849   if (type == error_mark_node)
15850     return type;
15851
15852   if (TREE_CODE (type) == ARRAY_TYPE)
15853     {
15854       tree t;
15855       tree element_type = c_build_qualified_type (TREE_TYPE (type),
15856                                                   type_quals, orig_qual_type,
15857                                                   orig_qual_indirect - 1);
15858
15859       /* See if we already have an identically qualified type.  */
15860       if (orig_qual_type && orig_qual_indirect == 0)
15861         t = orig_qual_type;
15862       else
15863         for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15864           {
15865             if (TYPE_QUALS (strip_array_types (t)) == type_quals
15866                 && TYPE_NAME (t) == TYPE_NAME (type)
15867                 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15868                 && attribute_list_equal (TYPE_ATTRIBUTES (t),
15869                                          TYPE_ATTRIBUTES (type)))
15870               break;
15871           }
15872       if (!t)
15873         {
15874           tree domain = TYPE_DOMAIN (type);
15875
15876           t = build_variant_type_copy (type);
15877           TREE_TYPE (t) = element_type;
15878
15879           if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15880               || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15881             SET_TYPE_STRUCTURAL_EQUALITY (t);
15882           else if (TYPE_CANONICAL (element_type) != element_type
15883                    || (domain && TYPE_CANONICAL (domain) != domain))
15884             {
15885               tree unqualified_canon
15886                 = build_array_type (TYPE_CANONICAL (element_type),
15887                                     domain? TYPE_CANONICAL (domain)
15888                                           : NULL_TREE);
15889               if (TYPE_REVERSE_STORAGE_ORDER (type))
15890                 {
15891                   unqualified_canon
15892                     = build_distinct_type_copy (unqualified_canon);
15893                   TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15894                 }
15895               TYPE_CANONICAL (t)
15896                 = c_build_qualified_type (unqualified_canon, type_quals);
15897             }
15898           else
15899             TYPE_CANONICAL (t) = t;
15900         }
15901       return t;
15902     }
15903
15904   /* A restrict-qualified pointer type must be a pointer to object or
15905      incomplete type.  Note that the use of POINTER_TYPE_P also allows
15906      REFERENCE_TYPEs, which is appropriate for C++.  */
15907   if ((type_quals & TYPE_QUAL_RESTRICT)
15908       && (!POINTER_TYPE_P (type)
15909           || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15910     {
15911       error ("invalid use of %<restrict%>");
15912       type_quals &= ~TYPE_QUAL_RESTRICT;
15913     }
15914
15915   tree var_type = (orig_qual_type && orig_qual_indirect == 0
15916                    ? orig_qual_type
15917                    : build_qualified_type (type, type_quals));
15918   /* A variant type does not inherit the list of incomplete vars from the
15919      type main variant.  */
15920   if ((RECORD_OR_UNION_TYPE_P (var_type)
15921        || TREE_CODE (var_type) == ENUMERAL_TYPE)
15922       && TYPE_MAIN_VARIANT (var_type) != var_type)
15923     C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15924   return var_type;
15925 }
15926
15927 /* Build a VA_ARG_EXPR for the C parser.  */
15928
15929 tree
15930 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15931 {
15932   if (error_operand_p (type))
15933     return error_mark_node;
15934   /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15935      order because it takes the address of the expression.  */
15936   else if (handled_component_p (expr)
15937            && reverse_storage_order_for_component_p (expr))
15938     {
15939       error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15940       return error_mark_node;
15941     }
15942   else if (!COMPLETE_TYPE_P (type))
15943     {
15944       error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15945                 "type %qT", type);
15946       return error_mark_node;
15947     }
15948   else if (TREE_CODE (type) == FUNCTION_TYPE)
15949     {
15950       error_at (loc2, "second argument to %<va_arg%> is a function type %qT",
15951                 type);
15952       return error_mark_node;
15953     }
15954   else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15955     warning_at (loc2, OPT_Wc___compat,
15956                 "C++ requires promoted type, not enum type, in %<va_arg%>");
15957   return build_va_arg (loc2, expr, type);
15958 }
15959
15960 /* Return truthvalue of whether T1 is the same tree structure as T2.
15961    Return 1 if they are the same. Return false if they are different.  */
15962
15963 bool
15964 c_tree_equal (tree t1, tree t2)
15965 {
15966   enum tree_code code1, code2;
15967
15968   if (t1 == t2)
15969     return true;
15970   if (!t1 || !t2)
15971     return false;
15972
15973   for (code1 = TREE_CODE (t1);
15974        CONVERT_EXPR_CODE_P (code1)
15975          || code1 == NON_LVALUE_EXPR;
15976        code1 = TREE_CODE (t1))
15977     t1 = TREE_OPERAND (t1, 0);
15978   for (code2 = TREE_CODE (t2);
15979        CONVERT_EXPR_CODE_P (code2)
15980          || code2 == NON_LVALUE_EXPR;
15981        code2 = TREE_CODE (t2))
15982     t2 = TREE_OPERAND (t2, 0);
15983
15984   /* They might have become equal now.  */
15985   if (t1 == t2)
15986     return true;
15987
15988   if (code1 != code2)
15989     return false;
15990
15991   switch (code1)
15992     {
15993     case INTEGER_CST:
15994       return wi::to_wide (t1) == wi::to_wide (t2);
15995
15996     case REAL_CST:
15997       return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15998
15999     case STRING_CST:
16000       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
16001         && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
16002                     TREE_STRING_LENGTH (t1));
16003
16004     case FIXED_CST:
16005       return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
16006                                      TREE_FIXED_CST (t2));
16007
16008     case COMPLEX_CST:
16009       return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
16010              && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
16011
16012     case VECTOR_CST:
16013       return operand_equal_p (t1, t2, OEP_ONLY_CONST);
16014
16015     case CONSTRUCTOR:
16016       /* We need to do this when determining whether or not two
16017          non-type pointer to member function template arguments
16018          are the same.  */
16019       if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
16020           || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
16021         return false;
16022       {
16023         tree field, value;
16024         unsigned int i;
16025         FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
16026           {
16027             constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
16028             if (!c_tree_equal (field, elt2->index)
16029                 || !c_tree_equal (value, elt2->value))
16030               return false;
16031           }
16032       }
16033       return true;
16034
16035     case TREE_LIST:
16036       if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
16037         return false;
16038       if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
16039         return false;
16040       return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
16041
16042     case SAVE_EXPR:
16043       return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16044
16045     case CALL_EXPR:
16046       {
16047         tree arg1, arg2;
16048         call_expr_arg_iterator iter1, iter2;
16049         if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
16050           return false;
16051         for (arg1 = first_call_expr_arg (t1, &iter1),
16052                arg2 = first_call_expr_arg (t2, &iter2);
16053              arg1 && arg2;
16054              arg1 = next_call_expr_arg (&iter1),
16055                arg2 = next_call_expr_arg (&iter2))
16056           if (!c_tree_equal (arg1, arg2))
16057             return false;
16058         if (arg1 || arg2)
16059           return false;
16060         return true;
16061       }
16062
16063     case TARGET_EXPR:
16064       {
16065         tree o1 = TREE_OPERAND (t1, 0);
16066         tree o2 = TREE_OPERAND (t2, 0);
16067
16068         /* Special case: if either target is an unallocated VAR_DECL,
16069            it means that it's going to be unified with whatever the
16070            TARGET_EXPR is really supposed to initialize, so treat it
16071            as being equivalent to anything.  */
16072         if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
16073             && !DECL_RTL_SET_P (o1))
16074           /*Nop*/;
16075         else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
16076                  && !DECL_RTL_SET_P (o2))
16077           /*Nop*/;
16078         else if (!c_tree_equal (o1, o2))
16079           return false;
16080
16081         return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
16082       }
16083
16084     case COMPONENT_REF:
16085       if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
16086         return false;
16087       return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
16088
16089     case PARM_DECL:
16090     case VAR_DECL:
16091     case CONST_DECL:
16092     case FIELD_DECL:
16093     case FUNCTION_DECL:
16094     case IDENTIFIER_NODE:
16095     case SSA_NAME:
16096       return false;
16097
16098     case TREE_VEC:
16099       {
16100         unsigned ix;
16101         if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
16102           return false;
16103         for (ix = TREE_VEC_LENGTH (t1); ix--;)
16104           if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
16105                              TREE_VEC_ELT (t2, ix)))
16106             return false;
16107         return true;
16108       }
16109
16110     default:
16111       break;
16112     }
16113
16114   switch (TREE_CODE_CLASS (code1))
16115     {
16116     case tcc_unary:
16117     case tcc_binary:
16118     case tcc_comparison:
16119     case tcc_expression:
16120     case tcc_vl_exp:
16121     case tcc_reference:
16122     case tcc_statement:
16123       {
16124         int i, n = TREE_OPERAND_LENGTH (t1);
16125
16126         switch (code1)
16127           {
16128           case PREINCREMENT_EXPR:
16129           case PREDECREMENT_EXPR:
16130           case POSTINCREMENT_EXPR:
16131           case POSTDECREMENT_EXPR:
16132             n = 1;
16133             break;
16134           case ARRAY_REF:
16135             n = 2;
16136             break;
16137           default:
16138             break;
16139           }
16140
16141         if (TREE_CODE_CLASS (code1) == tcc_vl_exp
16142             && n != TREE_OPERAND_LENGTH (t2))
16143           return false;
16144
16145         for (i = 0; i < n; ++i)
16146           if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
16147             return false;
16148
16149         return true;
16150       }
16151
16152     case tcc_type:
16153       return comptypes (t1, t2);
16154     default:
16155       gcc_unreachable ();
16156     }
16157 }
16158
16159 /* Returns true when the function declaration FNDECL is implicit,
16160    introduced as a result of a call to an otherwise undeclared
16161    function, and false otherwise.  */
16162
16163 bool
16164 c_decl_implicit (const_tree fndecl)
16165 {
16166   return C_DECL_IMPLICIT (fndecl);
16167 }