1 /* Build expressions with type checking for C compiler.
2    Copyright (C) 1987-2019 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 bad conversions.  Used to select
57    diagnostic messages in convert_for_assignment.  */
58 enum impl_conv {
59   ic_argpass,
60   ic_assign,
61   ic_init,
62   ic_return
63 };
64 
65 /* The level of nesting inside "__alignof__".  */
66 int in_alignof;
67 
68 /* The level of nesting inside "sizeof".  */
69 int in_sizeof;
70 
71 /* The level of nesting inside "typeof".  */
72 int in_typeof;
73 
74 /* The argument of last parsed sizeof expression, only to be tested
75    if expr.original_code == SIZEOF_EXPR.  */
76 tree c_last_sizeof_arg;
77 location_t c_last_sizeof_loc;
78 
79 /* Nonzero if we might need to print a "missing braces around
80    initializer" message within this initializer.  */
81 static int found_missing_braces;
82 
83 static int require_constant_value;
84 static int require_constant_elements;
85 
86 static bool null_pointer_constant_p (const_tree);
87 static tree qualify_type (tree, tree);
88 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
89 					 bool *);
90 static int comp_target_types (location_t, tree, tree);
91 static int function_types_compatible_p (const_tree, const_tree, bool *,
92 					bool *);
93 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
94 static tree lookup_field (tree, tree);
95 static int convert_arguments (location_t, vec<location_t>, tree,
96 			      vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
97 			      tree);
98 static tree pointer_diff (location_t, tree, tree, tree *);
99 static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
100 				    enum impl_conv, bool, tree, tree, int,
101 				    int = 0);
102 static tree valid_compound_expr_initializer (tree, tree);
103 static void push_string (const char *);
104 static void push_member_name (tree);
105 static int spelling_length (void);
106 static char *print_spelling (char *);
107 static void warning_init (location_t, int, const char *);
108 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
109 static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
110 				 bool, struct obstack *);
111 static void output_pending_init_elements (int, struct obstack *);
112 static bool set_designator (location_t, bool, struct obstack *);
113 static void push_range_stack (tree, struct obstack *);
114 static void add_pending_init (location_t, tree, tree, tree, bool,
115 			      struct obstack *);
116 static void set_nonincremental_init (struct obstack *);
117 static void set_nonincremental_init_from_string (tree, struct obstack *);
118 static tree find_init_member (tree, struct obstack *);
119 static void readonly_warning (tree, enum lvalue_use);
120 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
121 static void record_maybe_used_decl (tree);
122 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
123 
124 /* Return true if EXP is a null pointer constant, false otherwise.  */
125 
126 static bool
null_pointer_constant_p(const_tree expr)127 null_pointer_constant_p (const_tree expr)
128 {
129   /* This should really operate on c_expr structures, but they aren't
130      yet available everywhere required.  */
131   tree type = TREE_TYPE (expr);
132   return (TREE_CODE (expr) == INTEGER_CST
133 	  && !TREE_OVERFLOW (expr)
134 	  && integer_zerop (expr)
135 	  && (INTEGRAL_TYPE_P (type)
136 	      || (TREE_CODE (type) == POINTER_TYPE
137 		  && VOID_TYPE_P (TREE_TYPE (type))
138 		  && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
139 }
140 
141 /* EXPR may appear in an unevaluated part of an integer constant
142    expression, but not in an evaluated part.  Wrap it in a
143    C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
144    INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR.  */
145 
146 static tree
note_integer_operands(tree expr)147 note_integer_operands (tree expr)
148 {
149   tree ret;
150   if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
151     {
152       ret = copy_node (expr);
153       TREE_OVERFLOW (ret) = 1;
154     }
155   else
156     {
157       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
158       C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
159     }
160   return ret;
161 }
162 
163 /* Having checked whether EXPR may appear in an unevaluated part of an
164    integer constant expression and found that it may, remove any
165    C_MAYBE_CONST_EXPR noting this fact and return the resulting
166    expression.  */
167 
168 static inline tree
remove_c_maybe_const_expr(tree expr)169 remove_c_maybe_const_expr (tree expr)
170 {
171   if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
172     return C_MAYBE_CONST_EXPR_EXPR (expr);
173   else
174     return expr;
175 }
176 
177 /* This is a cache to hold if two types are compatible or not.  */
178 
179 struct tagged_tu_seen_cache {
180   const struct tagged_tu_seen_cache * next;
181   const_tree t1;
182   const_tree t2;
183   /* The return value of tagged_types_tu_compatible_p if we had seen
184      these two types already.  */
185   int val;
186 };
187 
188 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
189 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
190 
191 /* Do `exp = require_complete_type (loc, exp);' to make sure exp
192    does not have an incomplete type.  (That includes void types.)
193    LOC is the location of the use.  */
194 
195 tree
require_complete_type(location_t loc,tree value)196 require_complete_type (location_t loc, tree value)
197 {
198   tree type = TREE_TYPE (value);
199 
200   if (error_operand_p (value))
201     return error_mark_node;
202 
203   /* First, detect a valid value with a complete type.  */
204   if (COMPLETE_TYPE_P (type))
205     return value;
206 
207   c_incomplete_type_error (loc, value, type);
208   return error_mark_node;
209 }
210 
211 /* Print an error message for invalid use of an incomplete type.
212    VALUE is the expression that was used (or 0 if that isn't known)
213    and TYPE is the type that was invalid.  LOC is the location for
214    the error.  */
215 
216 void
c_incomplete_type_error(location_t loc,const_tree value,const_tree type)217 c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
218 {
219   /* Avoid duplicate error message.  */
220   if (TREE_CODE (type) == ERROR_MARK)
221     return;
222 
223   if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
224     error_at (loc, "%qD has an incomplete type %qT", value, type);
225   else
226     {
227     retry:
228       /* We must print an error message.  Be clever about what it says.  */
229 
230       switch (TREE_CODE (type))
231 	{
232 	case RECORD_TYPE:
233 	case UNION_TYPE:
234 	case ENUMERAL_TYPE:
235 	  break;
236 
237 	case VOID_TYPE:
238 	  error_at (loc, "invalid use of void expression");
239 	  return;
240 
241 	case ARRAY_TYPE:
242 	  if (TYPE_DOMAIN (type))
243 	    {
244 	      if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
245 		{
246 		  error_at (loc, "invalid use of flexible array member");
247 		  return;
248 		}
249 	      type = TREE_TYPE (type);
250 	      goto retry;
251 	    }
252 	  error_at (loc, "invalid use of array with unspecified bounds");
253 	  return;
254 
255 	default:
256 	  gcc_unreachable ();
257 	}
258 
259       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
260 	error_at (loc, "invalid use of undefined type %qT", type);
261       else
262 	/* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL.  */
263 	error_at (loc, "invalid use of incomplete typedef %qT", type);
264     }
265 }
266 
267 /* Given a type, apply default promotions wrt unnamed function
268    arguments and return the new type.  */
269 
270 tree
c_type_promotes_to(tree type)271 c_type_promotes_to (tree type)
272 {
273   tree ret = NULL_TREE;
274 
275   if (TYPE_MAIN_VARIANT (type) == float_type_node)
276     ret = double_type_node;
277   else if (c_promoting_integer_type_p (type))
278     {
279       /* Preserve unsignedness if not really getting any wider.  */
280       if (TYPE_UNSIGNED (type)
281 	  && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
282 	ret = unsigned_type_node;
283       else
284 	ret = integer_type_node;
285     }
286 
287   if (ret != NULL_TREE)
288     return (TYPE_ATOMIC (type)
289 	    ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
290 	    : ret);
291 
292   return type;
293 }
294 
295 /* Return true if between two named address spaces, whether there is a superset
296    named address space that encompasses both address spaces.  If there is a
297    superset, return which address space is the superset.  */
298 
299 static bool
addr_space_superset(addr_space_t as1,addr_space_t as2,addr_space_t * common)300 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
301 {
302   if (as1 == as2)
303     {
304       *common = as1;
305       return true;
306     }
307   else if (targetm.addr_space.subset_p (as1, as2))
308     {
309       *common = as2;
310       return true;
311     }
312   else if (targetm.addr_space.subset_p (as2, as1))
313     {
314       *common = as1;
315       return true;
316     }
317   else
318     return false;
319 }
320 
321 /* Return a variant of TYPE which has all the type qualifiers of LIKE
322    as well as those of TYPE.  */
323 
324 static tree
qualify_type(tree type,tree like)325 qualify_type (tree type, tree like)
326 {
327   addr_space_t as_type = TYPE_ADDR_SPACE (type);
328   addr_space_t as_like = TYPE_ADDR_SPACE (like);
329   addr_space_t as_common;
330 
331   /* If the two named address spaces are different, determine the common
332      superset address space.  If there isn't one, raise an error.  */
333   if (!addr_space_superset (as_type, as_like, &as_common))
334     {
335       as_common = as_type;
336       error ("%qT and %qT are in disjoint named address spaces",
337 	     type, like);
338     }
339 
340   return c_build_qualified_type (type,
341 				 TYPE_QUALS_NO_ADDR_SPACE (type)
342 				 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
343 				 | ENCODE_QUAL_ADDR_SPACE (as_common));
344 }
345 
346 /* Return true iff the given tree T is a variable length array.  */
347 
348 bool
c_vla_type_p(const_tree t)349 c_vla_type_p (const_tree t)
350 {
351   if (TREE_CODE (t) == ARRAY_TYPE
352       && C_TYPE_VARIABLE_SIZE (t))
353     return true;
354   return false;
355 }
356 
357 /* Return the composite type of two compatible types.
358 
359    We assume that comptypes has already been done and returned
360    nonzero; if that isn't so, this may crash.  In particular, we
361    assume that qualifiers match.  */
362 
363 tree
composite_type(tree t1,tree t2)364 composite_type (tree t1, tree t2)
365 {
366   enum tree_code code1;
367   enum tree_code code2;
368   tree attributes;
369 
370   /* Save time if the two types are the same.  */
371 
372   if (t1 == t2) return t1;
373 
374   /* If one type is nonsense, use the other.  */
375   if (t1 == error_mark_node)
376     return t2;
377   if (t2 == error_mark_node)
378     return t1;
379 
380   code1 = TREE_CODE (t1);
381   code2 = TREE_CODE (t2);
382 
383   /* Merge the attributes.  */
384   attributes = targetm.merge_type_attributes (t1, t2);
385 
386   /* If one is an enumerated type and the other is the compatible
387      integer type, the composite type might be either of the two
388      (DR#013 question 3).  For consistency, use the enumerated type as
389      the composite type.  */
390 
391   if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
392     return t1;
393   if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
394     return t2;
395 
396   gcc_assert (code1 == code2);
397 
398   switch (code1)
399     {
400     case POINTER_TYPE:
401       /* For two pointers, do this recursively on the target type.  */
402       {
403 	tree pointed_to_1 = TREE_TYPE (t1);
404 	tree pointed_to_2 = TREE_TYPE (t2);
405 	tree target = composite_type (pointed_to_1, pointed_to_2);
406         t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
407 	t1 = build_type_attribute_variant (t1, attributes);
408 	return qualify_type (t1, t2);
409       }
410 
411     case ARRAY_TYPE:
412       {
413 	tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
414 	int quals;
415 	tree unqual_elt;
416 	tree d1 = TYPE_DOMAIN (t1);
417 	tree d2 = TYPE_DOMAIN (t2);
418 	bool d1_variable, d2_variable;
419 	bool d1_zero, d2_zero;
420 	bool t1_complete, t2_complete;
421 
422 	/* We should not have any type quals on arrays at all.  */
423 	gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
424 		    && !TYPE_QUALS_NO_ADDR_SPACE (t2));
425 
426 	t1_complete = COMPLETE_TYPE_P (t1);
427 	t2_complete = COMPLETE_TYPE_P (t2);
428 
429 	d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
430 	d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
431 
432 	d1_variable = (!d1_zero
433 		       && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
434 			   || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
435 	d2_variable = (!d2_zero
436 		       && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
437 			   || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
438 	d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
439 	d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
440 
441 	/* Save space: see if the result is identical to one of the args.  */
442 	if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
443 	    && (d2_variable || d2_zero || !d1_variable))
444 	  return build_type_attribute_variant (t1, attributes);
445 	if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
446 	    && (d1_variable || d1_zero || !d2_variable))
447 	  return build_type_attribute_variant (t2, attributes);
448 
449 	if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
450 	  return build_type_attribute_variant (t1, attributes);
451 	if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
452 	  return build_type_attribute_variant (t2, attributes);
453 
454 	/* Merge the element types, and have a size if either arg has
455 	   one.  We may have qualifiers on the element types.  To set
456 	   up TYPE_MAIN_VARIANT correctly, we need to form the
457 	   composite of the unqualified types and add the qualifiers
458 	   back at the end.  */
459 	quals = TYPE_QUALS (strip_array_types (elt));
460 	unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
461 	t1 = build_array_type (unqual_elt,
462 			       TYPE_DOMAIN ((TYPE_DOMAIN (t1)
463 					     && (d2_variable
464 						 || d2_zero
465 						 || !d1_variable))
466 					    ? t1
467 					    : t2));
468 	/* Ensure a composite type involving a zero-length array type
469 	   is a zero-length type not an incomplete type.  */
470 	if (d1_zero && d2_zero
471 	    && (t1_complete || t2_complete)
472 	    && !COMPLETE_TYPE_P (t1))
473 	  {
474 	    TYPE_SIZE (t1) = bitsize_zero_node;
475 	    TYPE_SIZE_UNIT (t1) = size_zero_node;
476 	  }
477 	t1 = c_build_qualified_type (t1, quals);
478 	return build_type_attribute_variant (t1, attributes);
479       }
480 
481     case ENUMERAL_TYPE:
482     case RECORD_TYPE:
483     case UNION_TYPE:
484       if (attributes != NULL)
485 	{
486 	  /* Try harder not to create a new aggregate type.  */
487 	  if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
488 	    return t1;
489 	  if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
490 	    return t2;
491 	}
492       return build_type_attribute_variant (t1, attributes);
493 
494     case FUNCTION_TYPE:
495       /* Function types: prefer the one that specified arg types.
496 	 If both do, merge the arg types.  Also merge the return types.  */
497       {
498 	tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
499 	tree p1 = TYPE_ARG_TYPES (t1);
500 	tree p2 = TYPE_ARG_TYPES (t2);
501 	int len;
502 	tree newargs, n;
503 	int i;
504 
505 	/* Save space: see if the result is identical to one of the args.  */
506 	if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
507 	  return build_type_attribute_variant (t1, attributes);
508 	if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
509 	  return build_type_attribute_variant (t2, attributes);
510 
511 	/* Simple way if one arg fails to specify argument types.  */
512 	if (TYPE_ARG_TYPES (t1) == NULL_TREE)
513 	 {
514 	    t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
515 	    t1 = build_type_attribute_variant (t1, attributes);
516 	    return qualify_type (t1, t2);
517 	 }
518 	if (TYPE_ARG_TYPES (t2) == NULL_TREE)
519 	 {
520 	   t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
521 	   t1 = build_type_attribute_variant (t1, attributes);
522 	   return qualify_type (t1, t2);
523 	 }
524 
525 	/* If both args specify argument types, we must merge the two
526 	   lists, argument by argument.  */
527 
528 	for (len = 0, newargs = p1;
529 	     newargs && newargs != void_list_node;
530 	     len++, newargs = TREE_CHAIN (newargs))
531 	  ;
532 
533 	for (i = 0; i < len; i++)
534 	  newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
535 
536 	n = newargs;
537 
538 	for (; p1 && p1 != void_list_node;
539 	     p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
540 	  {
541 	    /* A null type means arg type is not specified.
542 	       Take whatever the other function type has.  */
543 	    if (TREE_VALUE (p1) == NULL_TREE)
544 	      {
545 		TREE_VALUE (n) = TREE_VALUE (p2);
546 		goto parm_done;
547 	      }
548 	    if (TREE_VALUE (p2) == NULL_TREE)
549 	      {
550 		TREE_VALUE (n) = TREE_VALUE (p1);
551 		goto parm_done;
552 	      }
553 
554 	    /* Given  wait (union {union wait *u; int *i} *)
555 	       and  wait (union wait *),
556 	       prefer  union wait *  as type of parm.  */
557 	    if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
558 		&& TREE_VALUE (p1) != TREE_VALUE (p2))
559 	      {
560 		tree memb;
561 		tree mv2 = TREE_VALUE (p2);
562 		if (mv2 && mv2 != error_mark_node
563 		    && TREE_CODE (mv2) != ARRAY_TYPE)
564 		  mv2 = TYPE_MAIN_VARIANT (mv2);
565 		for (memb = TYPE_FIELDS (TREE_VALUE (p1));
566 		     memb; memb = DECL_CHAIN (memb))
567 		  {
568 		    tree mv3 = TREE_TYPE (memb);
569 		    if (mv3 && mv3 != error_mark_node
570 			&& TREE_CODE (mv3) != ARRAY_TYPE)
571 		      mv3 = TYPE_MAIN_VARIANT (mv3);
572 		    if (comptypes (mv3, mv2))
573 		      {
574 			TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
575 							 TREE_VALUE (p2));
576 			pedwarn (input_location, OPT_Wpedantic,
577 				 "function types not truly compatible in ISO C");
578 			goto parm_done;
579 		      }
580 		  }
581 	      }
582 	    if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
583 		&& TREE_VALUE (p2) != TREE_VALUE (p1))
584 	      {
585 		tree memb;
586 		tree mv1 = TREE_VALUE (p1);
587 		if (mv1 && mv1 != error_mark_node
588 		    && TREE_CODE (mv1) != ARRAY_TYPE)
589 		  mv1 = TYPE_MAIN_VARIANT (mv1);
590 		for (memb = TYPE_FIELDS (TREE_VALUE (p2));
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, mv1))
598 		      {
599 			TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
600 							 TREE_VALUE (p1));
601 			pedwarn (input_location, OPT_Wpedantic,
602 				 "function types not truly compatible in ISO C");
603 			goto parm_done;
604 		      }
605 		  }
606 	      }
607 	    TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
608 	  parm_done: ;
609 	  }
610 
611 	t1 = build_function_type (valtype, newargs);
612 	t1 = qualify_type (t1, t2);
613       }
614       /* FALLTHRU */
615 
616     default:
617       return build_type_attribute_variant (t1, attributes);
618     }
619 
620 }
621 
622 /* Return the type of a conditional expression between pointers to
623    possibly differently qualified versions of compatible types.
624 
625    We assume that comp_target_types has already been done and returned
626    nonzero; if that isn't so, this may crash.  */
627 
628 static tree
common_pointer_type(tree t1,tree t2)629 common_pointer_type (tree t1, tree t2)
630 {
631   tree attributes;
632   tree pointed_to_1, mv1;
633   tree pointed_to_2, mv2;
634   tree target;
635   unsigned target_quals;
636   addr_space_t as1, as2, as_common;
637   int quals1, quals2;
638 
639   /* Save time if the two types are the same.  */
640 
641   if (t1 == t2) return t1;
642 
643   /* If one type is nonsense, use the other.  */
644   if (t1 == error_mark_node)
645     return t2;
646   if (t2 == error_mark_node)
647     return t1;
648 
649   gcc_assert (TREE_CODE (t1) == POINTER_TYPE
650 	      && TREE_CODE (t2) == POINTER_TYPE);
651 
652   /* Merge the attributes.  */
653   attributes = targetm.merge_type_attributes (t1, t2);
654 
655   /* Find the composite type of the target types, and combine the
656      qualifiers of the two types' targets.  Do not lose qualifiers on
657      array element types by taking the TYPE_MAIN_VARIANT.  */
658   mv1 = pointed_to_1 = TREE_TYPE (t1);
659   mv2 = pointed_to_2 = TREE_TYPE (t2);
660   if (TREE_CODE (mv1) != ARRAY_TYPE)
661     mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
662   if (TREE_CODE (mv2) != ARRAY_TYPE)
663     mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
664   target = composite_type (mv1, mv2);
665 
666   /* Strip array types to get correct qualifier for pointers to arrays */
667   quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
668   quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
669 
670   /* For function types do not merge const qualifiers, but drop them
671      if used inconsistently.  The middle-end uses these to mark const
672      and noreturn functions.  */
673   if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
674     target_quals = (quals1 & quals2);
675   else
676     target_quals = (quals1 | quals2);
677 
678   /* If the two named address spaces are different, determine the common
679      superset address space.  This is guaranteed to exist due to the
680      assumption that comp_target_type returned non-zero.  */
681   as1 = TYPE_ADDR_SPACE (pointed_to_1);
682   as2 = TYPE_ADDR_SPACE (pointed_to_2);
683   if (!addr_space_superset (as1, as2, &as_common))
684     gcc_unreachable ();
685 
686   target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
687 
688   t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
689   return build_type_attribute_variant (t1, attributes);
690 }
691 
692 /* Return the common type for two arithmetic types under the usual
693    arithmetic conversions.  The default conversions have already been
694    applied, and enumerated types converted to their compatible integer
695    types.  The resulting type is unqualified and has no attributes.
696 
697    This is the type for the result of most arithmetic operations
698    if the operands have the given two types.  */
699 
700 static tree
c_common_type(tree t1,tree t2)701 c_common_type (tree t1, tree t2)
702 {
703   enum tree_code code1;
704   enum tree_code code2;
705 
706   /* If one type is nonsense, use the other.  */
707   if (t1 == error_mark_node)
708     return t2;
709   if (t2 == error_mark_node)
710     return t1;
711 
712   if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
713     t1 = TYPE_MAIN_VARIANT (t1);
714 
715   if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
716     t2 = TYPE_MAIN_VARIANT (t2);
717 
718   if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
719     t1 = build_type_attribute_variant (t1, NULL_TREE);
720 
721   if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
722     t2 = build_type_attribute_variant (t2, NULL_TREE);
723 
724   /* Save time if the two types are the same.  */
725 
726   if (t1 == t2) return t1;
727 
728   code1 = TREE_CODE (t1);
729   code2 = TREE_CODE (t2);
730 
731   gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
732 	      || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
733 	      || code1 == INTEGER_TYPE);
734   gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
735 	      || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
736 	      || code2 == INTEGER_TYPE);
737 
738   /* When one operand is a decimal float type, the other operand cannot be
739      a generic float type or a complex type.  We also disallow vector types
740      here.  */
741   if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
742       && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
743     {
744       if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
745 	{
746 	  error ("can%'t mix operands of decimal float and vector types");
747 	  return error_mark_node;
748 	}
749       if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
750 	{
751 	  error ("can%'t mix operands of decimal float and complex types");
752 	  return error_mark_node;
753 	}
754       if (code1 == REAL_TYPE && code2 == REAL_TYPE)
755 	{
756 	  error ("can%'t mix operands of decimal float and other float types");
757 	  return error_mark_node;
758 	}
759     }
760 
761   /* If one type is a vector type, return that type.  (How the usual
762      arithmetic conversions apply to the vector types extension is not
763      precisely specified.)  */
764   if (code1 == VECTOR_TYPE)
765     return t1;
766 
767   if (code2 == VECTOR_TYPE)
768     return t2;
769 
770   /* If one type is complex, form the common type of the non-complex
771      components, then make that complex.  Use T1 or T2 if it is the
772      required type.  */
773   if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
774     {
775       tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
776       tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
777       tree subtype = c_common_type (subtype1, subtype2);
778 
779       if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
780 	return t1;
781       else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
782 	return t2;
783       else
784 	return build_complex_type (subtype);
785     }
786 
787   /* If only one is real, use it as the result.  */
788 
789   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
790     return t1;
791 
792   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
793     return t2;
794 
795   /* If both are real and either are decimal floating point types, use
796      the decimal floating point type with the greater precision. */
797 
798   if (code1 == REAL_TYPE && code2 == REAL_TYPE)
799     {
800       if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
801 	  || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
802 	return dfloat128_type_node;
803       else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
804 	       || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
805 	return dfloat64_type_node;
806       else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
807 	       || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
808 	return dfloat32_type_node;
809     }
810 
811   /* Deal with fixed-point types.  */
812   if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
813     {
814       unsigned int unsignedp = 0, satp = 0;
815       scalar_mode m1, m2;
816       unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
817 
818       m1 = SCALAR_TYPE_MODE (t1);
819       m2 = SCALAR_TYPE_MODE (t2);
820 
821       /* If one input type is saturating, the result type is saturating.  */
822       if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
823 	satp = 1;
824 
825       /* If both fixed-point types are unsigned, the result type is unsigned.
826 	 When mixing fixed-point and integer types, follow the sign of the
827 	 fixed-point type.
828 	 Otherwise, the result type is signed.  */
829       if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
830 	   && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
831 	  || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
832 	      && TYPE_UNSIGNED (t1))
833 	  || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
834 	      && TYPE_UNSIGNED (t2)))
835 	unsignedp = 1;
836 
837       /* The result type is signed.  */
838       if (unsignedp == 0)
839 	{
840 	  /* If the input type is unsigned, we need to convert to the
841 	     signed type.  */
842 	  if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
843 	    {
844 	      enum mode_class mclass = (enum mode_class) 0;
845 	      if (GET_MODE_CLASS (m1) == MODE_UFRACT)
846 		mclass = MODE_FRACT;
847 	      else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
848 		mclass = MODE_ACCUM;
849 	      else
850 		gcc_unreachable ();
851 	      m1 = as_a <scalar_mode>
852 		(mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
853 	    }
854 	  if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
855 	    {
856 	      enum mode_class mclass = (enum mode_class) 0;
857 	      if (GET_MODE_CLASS (m2) == MODE_UFRACT)
858 		mclass = MODE_FRACT;
859 	      else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
860 		mclass = MODE_ACCUM;
861 	      else
862 		gcc_unreachable ();
863 	      m2 = as_a <scalar_mode>
864 		(mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
865 	    }
866 	}
867 
868       if (code1 == FIXED_POINT_TYPE)
869 	{
870 	  fbit1 = GET_MODE_FBIT (m1);
871 	  ibit1 = GET_MODE_IBIT (m1);
872 	}
873       else
874 	{
875 	  fbit1 = 0;
876 	  /* Signed integers need to subtract one sign bit.  */
877 	  ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
878 	}
879 
880       if (code2 == FIXED_POINT_TYPE)
881 	{
882 	  fbit2 = GET_MODE_FBIT (m2);
883 	  ibit2 = GET_MODE_IBIT (m2);
884 	}
885       else
886 	{
887 	  fbit2 = 0;
888 	  /* Signed integers need to subtract one sign bit.  */
889 	  ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
890 	}
891 
892       max_ibit = ibit1 >= ibit2 ?  ibit1 : ibit2;
893       max_fbit = fbit1 >= fbit2 ?  fbit1 : fbit2;
894       return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
895 						 satp);
896     }
897 
898   /* Both real or both integers; use the one with greater precision.  */
899 
900   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
901     return t1;
902   else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
903     return t2;
904 
905   /* Same precision.  Prefer long longs to longs to ints when the
906      same precision, following the C99 rules on integer type rank
907      (which are equivalent to the C90 rules for C90 types).  */
908 
909   if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
910       || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
911     return long_long_unsigned_type_node;
912 
913   if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
914       || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
915     {
916       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
917 	return long_long_unsigned_type_node;
918       else
919 	return long_long_integer_type_node;
920     }
921 
922   if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
923       || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
924     return long_unsigned_type_node;
925 
926   if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
927       || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
928     {
929       /* But preserve unsignedness from the other type,
930 	 since long cannot hold all the values of an unsigned int.  */
931       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
932 	return long_unsigned_type_node;
933       else
934 	return long_integer_type_node;
935     }
936 
937   /* For floating types of the same TYPE_PRECISION (which we here
938      assume means either the same set of values, or sets of values
939      neither a subset of the other, with behavior being undefined in
940      the latter case), follow the rules from TS 18661-3: prefer
941      interchange types _FloatN, then standard types long double,
942      double, float, then extended types _FloatNx.  For extended types,
943      check them starting with _Float128x as that seems most consistent
944      in spirit with preferring long double to double; for interchange
945      types, also check in that order for consistency although it's not
946      possible for more than one of them to have the same
947      precision.  */
948   tree mv1 = TYPE_MAIN_VARIANT (t1);
949   tree mv2 = TYPE_MAIN_VARIANT (t2);
950 
951   for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
952     if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
953       return FLOATN_TYPE_NODE (i);
954 
955   /* Likewise, prefer long double to double even if same size.  */
956   if (mv1 == long_double_type_node || mv2 == long_double_type_node)
957     return long_double_type_node;
958 
959   /* Likewise, prefer double to float even if same size.
960      We got a couple of embedded targets with 32 bit doubles, and the
961      pdp11 might have 64 bit floats.  */
962   if (mv1 == double_type_node || mv2 == double_type_node)
963     return double_type_node;
964 
965   if (mv1 == float_type_node || mv2 == float_type_node)
966     return float_type_node;
967 
968   for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
969     if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
970       return FLOATNX_TYPE_NODE (i);
971 
972   /* Otherwise prefer the unsigned one.  */
973 
974   if (TYPE_UNSIGNED (t1))
975     return t1;
976   else
977     return t2;
978 }
979 
980 /* Wrapper around c_common_type that is used by c-common.c and other
981    front end optimizations that remove promotions.  ENUMERAL_TYPEs
982    are allowed here and are converted to their compatible integer types.
983    BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
984    preferably a non-Boolean type as the common type.  */
985 tree
common_type(tree t1,tree t2)986 common_type (tree t1, tree t2)
987 {
988   if (TREE_CODE (t1) == ENUMERAL_TYPE)
989     t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
990   if (TREE_CODE (t2) == ENUMERAL_TYPE)
991     t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
992 
993   /* If both types are BOOLEAN_TYPE, then return boolean_type_node.  */
994   if (TREE_CODE (t1) == BOOLEAN_TYPE
995       && TREE_CODE (t2) == BOOLEAN_TYPE)
996     return boolean_type_node;
997 
998   /* If either type is BOOLEAN_TYPE, then return the other.  */
999   if (TREE_CODE (t1) == BOOLEAN_TYPE)
1000     return t2;
1001   if (TREE_CODE (t2) == BOOLEAN_TYPE)
1002     return t1;
1003 
1004   return c_common_type (t1, t2);
1005 }
1006 
1007 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1008    or various other operations.  Return 2 if they are compatible
1009    but a warning may be needed if you use them together.  */
1010 
1011 int
comptypes(tree type1,tree type2)1012 comptypes (tree type1, tree type2)
1013 {
1014   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1015   int val;
1016 
1017   val = comptypes_internal (type1, type2, NULL, NULL);
1018   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1019 
1020   return val;
1021 }
1022 
1023 /* Like comptypes, but if it returns non-zero because enum and int are
1024    compatible, it sets *ENUM_AND_INT_P to true.  */
1025 
1026 static int
comptypes_check_enum_int(tree type1,tree type2,bool * enum_and_int_p)1027 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1028 {
1029   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1030   int val;
1031 
1032   val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1033   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1034 
1035   return val;
1036 }
1037 
1038 /* Like comptypes, but if it returns nonzero for different types, it
1039    sets *DIFFERENT_TYPES_P to true.  */
1040 
1041 int
comptypes_check_different_types(tree type1,tree type2,bool * different_types_p)1042 comptypes_check_different_types (tree type1, tree type2,
1043 				 bool *different_types_p)
1044 {
1045   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1046   int val;
1047 
1048   val = comptypes_internal (type1, type2, NULL, different_types_p);
1049   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1050 
1051   return val;
1052 }
1053 
1054 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1055    or various other operations.  Return 2 if they are compatible
1056    but a warning may be needed if you use them together.  If
1057    ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1058    compatible integer type, then this sets *ENUM_AND_INT_P to true;
1059    *ENUM_AND_INT_P is never set to false.  If DIFFERENT_TYPES_P is not
1060    NULL, and the types are compatible but different enough not to be
1061    permitted in C11 typedef redeclarations, then this sets
1062    *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1063    false, but may or may not be set if the types are incompatible.
1064    This differs from comptypes, in that we don't free the seen
1065    types.  */
1066 
1067 static int
comptypes_internal(const_tree type1,const_tree type2,bool * enum_and_int_p,bool * different_types_p)1068 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1069 		    bool *different_types_p)
1070 {
1071   const_tree t1 = type1;
1072   const_tree t2 = type2;
1073   int attrval, val;
1074 
1075   /* Suppress errors caused by previously reported errors.  */
1076 
1077   if (t1 == t2 || !t1 || !t2
1078       || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1079     return 1;
1080 
1081   /* Enumerated types are compatible with integer types, but this is
1082      not transitive: two enumerated types in the same translation unit
1083      are compatible with each other only if they are the same type.  */
1084 
1085   if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1086     {
1087       t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1088       if (TREE_CODE (t2) != VOID_TYPE)
1089 	{
1090 	  if (enum_and_int_p != NULL)
1091 	    *enum_and_int_p = true;
1092 	  if (different_types_p != NULL)
1093 	    *different_types_p = true;
1094 	}
1095     }
1096   else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1097     {
1098       t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1099       if (TREE_CODE (t1) != VOID_TYPE)
1100 	{
1101 	  if (enum_and_int_p != NULL)
1102 	    *enum_and_int_p = true;
1103 	  if (different_types_p != NULL)
1104 	    *different_types_p = true;
1105 	}
1106     }
1107 
1108   if (t1 == t2)
1109     return 1;
1110 
1111   /* Different classes of types can't be compatible.  */
1112 
1113   if (TREE_CODE (t1) != TREE_CODE (t2))
1114     return 0;
1115 
1116   /* Qualifiers must match. C99 6.7.3p9 */
1117 
1118   if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1119     return 0;
1120 
1121   /* Allow for two different type nodes which have essentially the same
1122      definition.  Note that we already checked for equality of the type
1123      qualifiers (just above).  */
1124 
1125   if (TREE_CODE (t1) != ARRAY_TYPE
1126       && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1127     return 1;
1128 
1129   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1130   if (!(attrval = comp_type_attributes (t1, t2)))
1131      return 0;
1132 
1133   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1134   val = 0;
1135 
1136   switch (TREE_CODE (t1))
1137     {
1138     case INTEGER_TYPE:
1139     case FIXED_POINT_TYPE:
1140     case REAL_TYPE:
1141       /* With these nodes, we can't determine type equivalence by
1142 	 looking at what is stored in the nodes themselves, because
1143 	 two nodes might have different TYPE_MAIN_VARIANTs but still
1144 	 represent the same type.  For example, wchar_t and int could
1145 	 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1146 	 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1147 	 and are distinct types.  On the other hand, int and the
1148 	 following typedef
1149 
1150 	   typedef int INT __attribute((may_alias));
1151 
1152 	 have identical properties, different TYPE_MAIN_VARIANTs, but
1153 	 represent the same type.  The canonical type system keeps
1154 	 track of equivalence in this case, so we fall back on it.  */
1155       return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1156 
1157     case POINTER_TYPE:
1158       /* Do not remove mode information.  */
1159       if (TYPE_MODE (t1) != TYPE_MODE (t2))
1160 	break;
1161       val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1162 	     ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1163 				       enum_and_int_p, different_types_p));
1164       break;
1165 
1166     case FUNCTION_TYPE:
1167       val = function_types_compatible_p (t1, t2, enum_and_int_p,
1168 					 different_types_p);
1169       break;
1170 
1171     case ARRAY_TYPE:
1172       {
1173 	tree d1 = TYPE_DOMAIN (t1);
1174 	tree d2 = TYPE_DOMAIN (t2);
1175 	bool d1_variable, d2_variable;
1176 	bool d1_zero, d2_zero;
1177 	val = 1;
1178 
1179 	/* Target types must match incl. qualifiers.  */
1180 	if (TREE_TYPE (t1) != TREE_TYPE (t2)
1181 	    && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1182 					  enum_and_int_p,
1183 					  different_types_p)) == 0)
1184 	  return 0;
1185 
1186 	if (different_types_p != NULL
1187 	    && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1188 	  *different_types_p = true;
1189 	/* Sizes must match unless one is missing or variable.  */
1190 	if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1191 	  break;
1192 
1193 	d1_zero = !TYPE_MAX_VALUE (d1);
1194 	d2_zero = !TYPE_MAX_VALUE (d2);
1195 
1196 	d1_variable = (!d1_zero
1197 		       && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1198 			   || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1199 	d2_variable = (!d2_zero
1200 		       && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1201 			   || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1202 	d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1203 	d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1204 
1205 	if (different_types_p != NULL
1206 	    && d1_variable != d2_variable)
1207 	  *different_types_p = true;
1208 	if (d1_variable || d2_variable)
1209 	  break;
1210 	if (d1_zero && d2_zero)
1211 	  break;
1212 	if (d1_zero || d2_zero
1213 	    || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1214 	    || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1215 	  val = 0;
1216 
1217 	break;
1218       }
1219 
1220     case ENUMERAL_TYPE:
1221     case RECORD_TYPE:
1222     case UNION_TYPE:
1223       if (val != 1 && !same_translation_unit_p (t1, t2))
1224 	{
1225 	  tree a1 = TYPE_ATTRIBUTES (t1);
1226 	  tree a2 = TYPE_ATTRIBUTES (t2);
1227 
1228 	  if (! attribute_list_contained (a1, a2)
1229 	      && ! attribute_list_contained (a2, a1))
1230 	    break;
1231 
1232 	  if (attrval != 2)
1233 	    return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1234 						 different_types_p);
1235 	  val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1236 					      different_types_p);
1237 	}
1238       break;
1239 
1240     case VECTOR_TYPE:
1241       val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1242 	     && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1243 				    enum_and_int_p, different_types_p));
1244       break;
1245 
1246     default:
1247       break;
1248     }
1249   return attrval == 2 && val == 1 ? 2 : val;
1250 }
1251 
1252 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1253    their qualifiers, except for named address spaces.  If the pointers point to
1254    different named addresses, then we must determine if one address space is a
1255    subset of the other.  */
1256 
1257 static int
comp_target_types(location_t location,tree ttl,tree ttr)1258 comp_target_types (location_t location, tree ttl, tree ttr)
1259 {
1260   int val;
1261   int val_ped;
1262   tree mvl = TREE_TYPE (ttl);
1263   tree mvr = TREE_TYPE (ttr);
1264   addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1265   addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1266   addr_space_t as_common;
1267   bool enum_and_int_p;
1268 
1269   /* Fail if pointers point to incompatible address spaces.  */
1270   if (!addr_space_superset (asl, asr, &as_common))
1271     return 0;
1272 
1273   /* For pedantic record result of comptypes on arrays before losing
1274      qualifiers on the element type below. */
1275   val_ped = 1;
1276 
1277   if (TREE_CODE (mvl) == ARRAY_TYPE
1278       && TREE_CODE (mvr) == ARRAY_TYPE)
1279     val_ped = comptypes (mvl, mvr);
1280 
1281   /* Qualifiers on element types of array types that are
1282      pointer targets are lost by taking their TYPE_MAIN_VARIANT.  */
1283 
1284   mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1285 	 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1286 	 : TYPE_MAIN_VARIANT (mvl));
1287 
1288   mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1289 	 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1290 	 : TYPE_MAIN_VARIANT (mvr));
1291 
1292   enum_and_int_p = false;
1293   val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1294 
1295   if (val == 1 && val_ped != 1)
1296     pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1297                                       "are incompatible in ISO C");
1298 
1299   if (val == 2)
1300     pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1301 
1302   if (val == 1 && enum_and_int_p && warn_cxx_compat)
1303     warning_at (location, OPT_Wc___compat,
1304 		"pointer target types incompatible in C++");
1305 
1306   return val;
1307 }
1308 
1309 /* Subroutines of `comptypes'.  */
1310 
1311 /* Determine whether two trees derive from the same translation unit.
1312    If the CONTEXT chain ends in a null, that tree's context is still
1313    being parsed, so if two trees have context chains ending in null,
1314    they're in the same translation unit.  */
1315 
1316 bool
same_translation_unit_p(const_tree t1,const_tree t2)1317 same_translation_unit_p (const_tree t1, const_tree t2)
1318 {
1319   while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1320     switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1321       {
1322       case tcc_declaration:
1323 	t1 = DECL_CONTEXT (t1); break;
1324       case tcc_type:
1325 	t1 = TYPE_CONTEXT (t1); break;
1326       case tcc_exceptional:
1327 	t1 = BLOCK_SUPERCONTEXT (t1); break;  /* assume block */
1328       default: gcc_unreachable ();
1329       }
1330 
1331   while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1332     switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1333       {
1334       case tcc_declaration:
1335 	t2 = DECL_CONTEXT (t2); break;
1336       case tcc_type:
1337 	t2 = TYPE_CONTEXT (t2); break;
1338       case tcc_exceptional:
1339 	t2 = BLOCK_SUPERCONTEXT (t2); break;  /* assume block */
1340       default: gcc_unreachable ();
1341       }
1342 
1343   return t1 == t2;
1344 }
1345 
1346 /* Allocate the seen two types, assuming that they are compatible. */
1347 
1348 static struct tagged_tu_seen_cache *
alloc_tagged_tu_seen_cache(const_tree t1,const_tree t2)1349 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1350 {
1351   struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1352   tu->next = tagged_tu_seen_base;
1353   tu->t1 = t1;
1354   tu->t2 = t2;
1355 
1356   tagged_tu_seen_base = tu;
1357 
1358   /* The C standard says that two structures in different translation
1359      units are compatible with each other only if the types of their
1360      fields are compatible (among other things).  We assume that they
1361      are compatible until proven otherwise when building the cache.
1362      An example where this can occur is:
1363      struct a
1364      {
1365        struct a *next;
1366      };
1367      If we are comparing this against a similar struct in another TU,
1368      and did not assume they were compatible, we end up with an infinite
1369      loop.  */
1370   tu->val = 1;
1371   return tu;
1372 }
1373 
1374 /* Free the seen types until we get to TU_TIL. */
1375 
1376 static void
free_all_tagged_tu_seen_up_to(const struct tagged_tu_seen_cache * tu_til)1377 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1378 {
1379   const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1380   while (tu != tu_til)
1381     {
1382       const struct tagged_tu_seen_cache *const tu1
1383 	= (const struct tagged_tu_seen_cache *) tu;
1384       tu = tu1->next;
1385       free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1386     }
1387   tagged_tu_seen_base = tu_til;
1388 }
1389 
1390 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1391    compatible.  If the two types are not the same (which has been
1392    checked earlier), this can only happen when multiple translation
1393    units are being compiled.  See C99 6.2.7 paragraph 1 for the exact
1394    rules.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1395    comptypes_internal.  */
1396 
1397 static int
tagged_types_tu_compatible_p(const_tree t1,const_tree t2,bool * enum_and_int_p,bool * different_types_p)1398 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1399 			      bool *enum_and_int_p, bool *different_types_p)
1400 {
1401   tree s1, s2;
1402   bool needs_warning = false;
1403 
1404   /* We have to verify that the tags of the types are the same.  This
1405      is harder than it looks because this may be a typedef, so we have
1406      to go look at the original type.  It may even be a typedef of a
1407      typedef...
1408      In the case of compiler-created builtin structs the TYPE_DECL
1409      may be a dummy, with no DECL_ORIGINAL_TYPE.  Don't fault.  */
1410   while (TYPE_NAME (t1)
1411 	 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1412 	 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1413     t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1414 
1415   while (TYPE_NAME (t2)
1416 	 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1417 	 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1418     t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1419 
1420   /* C90 didn't have the requirement that the two tags be the same.  */
1421   if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1422     return 0;
1423 
1424   /* C90 didn't say what happened if one or both of the types were
1425      incomplete; we choose to follow C99 rules here, which is that they
1426      are compatible.  */
1427   if (TYPE_SIZE (t1) == NULL
1428       || TYPE_SIZE (t2) == NULL)
1429     return 1;
1430 
1431   {
1432     const struct tagged_tu_seen_cache * tts_i;
1433     for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1434       if (tts_i->t1 == t1 && tts_i->t2 == t2)
1435 	return tts_i->val;
1436   }
1437 
1438   switch (TREE_CODE (t1))
1439     {
1440     case ENUMERAL_TYPE:
1441       {
1442 	struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1443 	/* Speed up the case where the type values are in the same order.  */
1444 	tree tv1 = TYPE_VALUES (t1);
1445 	tree tv2 = TYPE_VALUES (t2);
1446 
1447 	if (tv1 == tv2)
1448 	  {
1449 	    return 1;
1450 	  }
1451 
1452 	for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1453 	  {
1454 	    if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1455 	      break;
1456 	    if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1457 	      {
1458 		tu->val = 0;
1459 		return 0;
1460 	      }
1461 	  }
1462 
1463 	if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1464 	  {
1465 	    return 1;
1466 	  }
1467 	if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1468 	  {
1469 	    tu->val = 0;
1470 	    return 0;
1471 	  }
1472 
1473 	if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1474 	  {
1475 	    tu->val = 0;
1476 	    return 0;
1477 	  }
1478 
1479 	for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1480 	  {
1481 	    s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1482 	    if (s2 == NULL
1483 		|| simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1484 	      {
1485 		tu->val = 0;
1486 		return 0;
1487 	      }
1488 	  }
1489 	return 1;
1490       }
1491 
1492     case UNION_TYPE:
1493       {
1494 	struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1495 	if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1496 	  {
1497 	    tu->val = 0;
1498 	    return 0;
1499 	  }
1500 
1501 	/*  Speed up the common case where the fields are in the same order. */
1502 	for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1503 	     s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1504 	  {
1505 	    int result;
1506 
1507 	    if (DECL_NAME (s1) != DECL_NAME (s2))
1508 	      break;
1509 	    result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1510 					 enum_and_int_p, different_types_p);
1511 
1512 	    if (result != 1 && !DECL_NAME (s1))
1513 	      break;
1514 	    if (result == 0)
1515 	      {
1516 		tu->val = 0;
1517 		return 0;
1518 	      }
1519 	    if (result == 2)
1520 	      needs_warning = true;
1521 
1522 	    if (TREE_CODE (s1) == FIELD_DECL
1523 		&& simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1524 				     DECL_FIELD_BIT_OFFSET (s2)) != 1)
1525 	      {
1526 		tu->val = 0;
1527 		return 0;
1528 	      }
1529 	  }
1530 	if (!s1 && !s2)
1531 	  {
1532 	    tu->val = needs_warning ? 2 : 1;
1533 	    return tu->val;
1534 	  }
1535 
1536 	for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1537 	  {
1538 	    bool ok = false;
1539 
1540 	    for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1541 	      if (DECL_NAME (s1) == DECL_NAME (s2))
1542 		{
1543 		  int result;
1544 
1545 		  result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1546 					       enum_and_int_p,
1547 					       different_types_p);
1548 
1549 		  if (result != 1 && !DECL_NAME (s1))
1550 		    continue;
1551 		  if (result == 0)
1552 		    {
1553 		      tu->val = 0;
1554 		      return 0;
1555 		    }
1556 		  if (result == 2)
1557 		    needs_warning = true;
1558 
1559 		  if (TREE_CODE (s1) == FIELD_DECL
1560 		      && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1561 					   DECL_FIELD_BIT_OFFSET (s2)) != 1)
1562 		    break;
1563 
1564 		  ok = true;
1565 		  break;
1566 		}
1567 	    if (!ok)
1568 	      {
1569 		tu->val = 0;
1570 		return 0;
1571 	      }
1572 	  }
1573 	tu->val = needs_warning ? 2 : 10;
1574 	return tu->val;
1575       }
1576 
1577     case RECORD_TYPE:
1578       {
1579 	struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1580 
1581 	for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1582 	     s1 && s2;
1583 	     s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1584 	  {
1585 	    int result;
1586 	    if (TREE_CODE (s1) != TREE_CODE (s2)
1587 		|| DECL_NAME (s1) != DECL_NAME (s2))
1588 	      break;
1589 	    result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1590 					 enum_and_int_p, different_types_p);
1591 	    if (result == 0)
1592 	      break;
1593 	    if (result == 2)
1594 	      needs_warning = true;
1595 
1596 	    if (TREE_CODE (s1) == FIELD_DECL
1597 		&& simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1598 				     DECL_FIELD_BIT_OFFSET (s2)) != 1)
1599 	      break;
1600 	  }
1601 	if (s1 && s2)
1602 	  tu->val = 0;
1603 	else
1604 	  tu->val = needs_warning ? 2 : 1;
1605 	return tu->val;
1606       }
1607 
1608     default:
1609       gcc_unreachable ();
1610     }
1611 }
1612 
1613 /* Return 1 if two function types F1 and F2 are compatible.
1614    If either type specifies no argument types,
1615    the other must specify a fixed number of self-promoting arg types.
1616    Otherwise, if one type specifies only the number of arguments,
1617    the other must specify that number of self-promoting arg types.
1618    Otherwise, the argument types must match.
1619    ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal.  */
1620 
1621 static int
function_types_compatible_p(const_tree f1,const_tree f2,bool * enum_and_int_p,bool * different_types_p)1622 function_types_compatible_p (const_tree f1, const_tree f2,
1623 			     bool *enum_and_int_p, bool *different_types_p)
1624 {
1625   tree args1, args2;
1626   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1627   int val = 1;
1628   int val1;
1629   tree ret1, ret2;
1630 
1631   ret1 = TREE_TYPE (f1);
1632   ret2 = TREE_TYPE (f2);
1633 
1634   /* 'volatile' qualifiers on a function's return type used to mean
1635      the function is noreturn.  */
1636   if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1637     pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1638   if (TYPE_VOLATILE (ret1))
1639     ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1640 				 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1641   if (TYPE_VOLATILE (ret2))
1642     ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1643 				 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1644   val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1645   if (val == 0)
1646     return 0;
1647 
1648   args1 = TYPE_ARG_TYPES (f1);
1649   args2 = TYPE_ARG_TYPES (f2);
1650 
1651   if (different_types_p != NULL
1652       && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1653     *different_types_p = true;
1654 
1655   /* An unspecified parmlist matches any specified parmlist
1656      whose argument types don't need default promotions.  */
1657 
1658   if (args1 == NULL_TREE)
1659     {
1660       if (!self_promoting_args_p (args2))
1661 	return 0;
1662       /* If one of these types comes from a non-prototype fn definition,
1663 	 compare that with the other type's arglist.
1664 	 If they don't match, ask for a warning (but no error).  */
1665       if (TYPE_ACTUAL_ARG_TYPES (f1)
1666 	  && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1667 				      enum_and_int_p, different_types_p) != 1)
1668 	val = 2;
1669       return val;
1670     }
1671   if (args2 == NULL_TREE)
1672     {
1673       if (!self_promoting_args_p (args1))
1674 	return 0;
1675       if (TYPE_ACTUAL_ARG_TYPES (f2)
1676 	  && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1677 				      enum_and_int_p, different_types_p) != 1)
1678 	val = 2;
1679       return val;
1680     }
1681 
1682   /* Both types have argument lists: compare them and propagate results.  */
1683   val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1684 				  different_types_p);
1685   return val1 != 1 ? val1 : val;
1686 }
1687 
1688 /* Check two lists of types for compatibility, returning 0 for
1689    incompatible, 1 for compatible, or 2 for compatible with
1690    warning.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1691    comptypes_internal.  */
1692 
1693 static int
type_lists_compatible_p(const_tree args1,const_tree args2,bool * enum_and_int_p,bool * different_types_p)1694 type_lists_compatible_p (const_tree args1, const_tree args2,
1695 			 bool *enum_and_int_p, bool *different_types_p)
1696 {
1697   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1698   int val = 1;
1699   int newval = 0;
1700 
1701   while (1)
1702     {
1703       tree a1, mv1, a2, mv2;
1704       if (args1 == NULL_TREE && args2 == NULL_TREE)
1705 	return val;
1706       /* If one list is shorter than the other,
1707 	 they fail to match.  */
1708       if (args1 == NULL_TREE || args2 == NULL_TREE)
1709 	return 0;
1710       mv1 = a1 = TREE_VALUE (args1);
1711       mv2 = a2 = TREE_VALUE (args2);
1712       if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1713 	mv1 = (TYPE_ATOMIC (mv1)
1714 	       ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1715 					 TYPE_QUAL_ATOMIC)
1716 	       : TYPE_MAIN_VARIANT (mv1));
1717       if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1718 	mv2 = (TYPE_ATOMIC (mv2)
1719 	       ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1720 					 TYPE_QUAL_ATOMIC)
1721 	       : TYPE_MAIN_VARIANT (mv2));
1722       /* A null pointer instead of a type
1723 	 means there is supposed to be an argument
1724 	 but nothing is specified about what type it has.
1725 	 So match anything that self-promotes.  */
1726       if (different_types_p != NULL
1727 	  && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1728 	*different_types_p = true;
1729       if (a1 == NULL_TREE)
1730 	{
1731 	  if (c_type_promotes_to (a2) != a2)
1732 	    return 0;
1733 	}
1734       else if (a2 == NULL_TREE)
1735 	{
1736 	  if (c_type_promotes_to (a1) != a1)
1737 	    return 0;
1738 	}
1739       /* If one of the lists has an error marker, ignore this arg.  */
1740       else if (TREE_CODE (a1) == ERROR_MARK
1741 	       || TREE_CODE (a2) == ERROR_MARK)
1742 	;
1743       else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1744 					      different_types_p)))
1745 	{
1746 	  if (different_types_p != NULL)
1747 	    *different_types_p = true;
1748 	  /* Allow  wait (union {union wait *u; int *i} *)
1749 	     and  wait (union wait *)  to be compatible.  */
1750 	  if (TREE_CODE (a1) == UNION_TYPE
1751 	      && (TYPE_NAME (a1) == NULL_TREE
1752 		  || TYPE_TRANSPARENT_AGGR (a1))
1753 	      && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1754 	      && tree_int_cst_equal (TYPE_SIZE (a1),
1755 				     TYPE_SIZE (a2)))
1756 	    {
1757 	      tree memb;
1758 	      for (memb = TYPE_FIELDS (a1);
1759 		   memb; memb = DECL_CHAIN (memb))
1760 		{
1761 		  tree mv3 = TREE_TYPE (memb);
1762 		  if (mv3 && mv3 != error_mark_node
1763 		      && TREE_CODE (mv3) != ARRAY_TYPE)
1764 		    mv3 = (TYPE_ATOMIC (mv3)
1765 			   ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1766 						     TYPE_QUAL_ATOMIC)
1767 			   : TYPE_MAIN_VARIANT (mv3));
1768 		  if (comptypes_internal (mv3, mv2, enum_and_int_p,
1769 					  different_types_p))
1770 		    break;
1771 		}
1772 	      if (memb == NULL_TREE)
1773 		return 0;
1774 	    }
1775 	  else if (TREE_CODE (a2) == UNION_TYPE
1776 		   && (TYPE_NAME (a2) == NULL_TREE
1777 		       || TYPE_TRANSPARENT_AGGR (a2))
1778 		   && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1779 		   && tree_int_cst_equal (TYPE_SIZE (a2),
1780 					  TYPE_SIZE (a1)))
1781 	    {
1782 	      tree memb;
1783 	      for (memb = TYPE_FIELDS (a2);
1784 		   memb; memb = DECL_CHAIN (memb))
1785 		{
1786 		  tree mv3 = TREE_TYPE (memb);
1787 		  if (mv3 && mv3 != error_mark_node
1788 		      && TREE_CODE (mv3) != ARRAY_TYPE)
1789 		    mv3 = (TYPE_ATOMIC (mv3)
1790 			   ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1791 						     TYPE_QUAL_ATOMIC)
1792 			   : TYPE_MAIN_VARIANT (mv3));
1793 		  if (comptypes_internal (mv3, mv1, enum_and_int_p,
1794 					  different_types_p))
1795 		    break;
1796 		}
1797 	      if (memb == NULL_TREE)
1798 		return 0;
1799 	    }
1800 	  else
1801 	    return 0;
1802 	}
1803 
1804       /* comptypes said ok, but record if it said to warn.  */
1805       if (newval > val)
1806 	val = newval;
1807 
1808       args1 = TREE_CHAIN (args1);
1809       args2 = TREE_CHAIN (args2);
1810     }
1811 }
1812 
1813 /* Compute the size to increment a pointer by.  When a function type or void
1814    type or incomplete type is passed, size_one_node is returned.
1815    This function does not emit any diagnostics; the caller is responsible
1816    for that.  */
1817 
1818 static tree
c_size_in_bytes(const_tree type)1819 c_size_in_bytes (const_tree type)
1820 {
1821   enum tree_code code = TREE_CODE (type);
1822 
1823   if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1824       || !COMPLETE_TYPE_P (type))
1825     return size_one_node;
1826 
1827   /* Convert in case a char is more than one unit.  */
1828   return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1829 			 size_int (TYPE_PRECISION (char_type_node)
1830 				   / BITS_PER_UNIT));
1831 }
1832 
1833 /* Return either DECL or its known constant value (if it has one).  */
1834 
1835 tree
decl_constant_value_1(tree decl,bool in_init)1836 decl_constant_value_1 (tree decl, bool in_init)
1837 {
1838   if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL.  */
1839       TREE_CODE (decl) != PARM_DECL
1840       && !TREE_THIS_VOLATILE (decl)
1841       && TREE_READONLY (decl)
1842       && DECL_INITIAL (decl) != NULL_TREE
1843       && !error_operand_p (DECL_INITIAL (decl))
1844       /* This is invalid if initial value is not constant.
1845 	 If it has either a function call, a memory reference,
1846 	 or a variable, then re-evaluating it could give different results.  */
1847       && TREE_CONSTANT (DECL_INITIAL (decl))
1848       /* Check for cases where this is sub-optimal, even though valid.  */
1849       && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1850     return DECL_INITIAL (decl);
1851   return decl;
1852 }
1853 
1854 /* Return either DECL or its known constant value (if it has one).
1855    Like the above, but always return decl outside of functions.  */
1856 
1857 tree
decl_constant_value(tree decl)1858 decl_constant_value (tree decl)
1859 {
1860   /* Don't change a variable array bound or initial value to a constant
1861      in a place where a variable is invalid.  */
1862   return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1863 }
1864 
1865 /* Convert the array expression EXP to a pointer.  */
1866 static tree
array_to_pointer_conversion(location_t loc,tree exp)1867 array_to_pointer_conversion (location_t loc, tree exp)
1868 {
1869   tree orig_exp = exp;
1870   tree type = TREE_TYPE (exp);
1871   tree adr;
1872   tree restype = TREE_TYPE (type);
1873   tree ptrtype;
1874 
1875   gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1876 
1877   STRIP_TYPE_NOPS (exp);
1878 
1879   if (TREE_NO_WARNING (orig_exp))
1880     TREE_NO_WARNING (exp) = 1;
1881 
1882   ptrtype = build_pointer_type (restype);
1883 
1884   if (INDIRECT_REF_P (exp))
1885     return convert (ptrtype, TREE_OPERAND (exp, 0));
1886 
1887   /* In C++ array compound literals are temporary objects unless they are
1888      const or appear in namespace scope, so they are destroyed too soon
1889      to use them for much of anything  (c++/53220).  */
1890   if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1891     {
1892       tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1893       if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1894 	warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1895 		    "converting an array compound literal to a pointer "
1896 		    "is ill-formed in C++");
1897     }
1898 
1899   adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1900   return convert (ptrtype, adr);
1901 }
1902 
1903 /* Convert the function expression EXP to a pointer.  */
1904 static tree
function_to_pointer_conversion(location_t loc,tree exp)1905 function_to_pointer_conversion (location_t loc, tree exp)
1906 {
1907   tree orig_exp = exp;
1908 
1909   gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1910 
1911   STRIP_TYPE_NOPS (exp);
1912 
1913   if (TREE_NO_WARNING (orig_exp))
1914     TREE_NO_WARNING (exp) = 1;
1915 
1916   return build_unary_op (loc, ADDR_EXPR, exp, false);
1917 }
1918 
1919 /* Mark EXP as read, not just set, for set but not used -Wunused
1920    warning purposes.  */
1921 
1922 void
mark_exp_read(tree exp)1923 mark_exp_read (tree exp)
1924 {
1925   switch (TREE_CODE (exp))
1926     {
1927     case VAR_DECL:
1928     case PARM_DECL:
1929       DECL_READ_P (exp) = 1;
1930       break;
1931     case ARRAY_REF:
1932     case COMPONENT_REF:
1933     case MODIFY_EXPR:
1934     case REALPART_EXPR:
1935     case IMAGPART_EXPR:
1936     CASE_CONVERT:
1937     case ADDR_EXPR:
1938     case VIEW_CONVERT_EXPR:
1939       mark_exp_read (TREE_OPERAND (exp, 0));
1940       break;
1941     case COMPOUND_EXPR:
1942     case C_MAYBE_CONST_EXPR:
1943       mark_exp_read (TREE_OPERAND (exp, 1));
1944       break;
1945     default:
1946       break;
1947     }
1948 }
1949 
1950 /* Perform the default conversion of arrays and functions to pointers.
1951    Return the result of converting EXP.  For any other expression, just
1952    return EXP.
1953 
1954    LOC is the location of the expression.  */
1955 
1956 struct c_expr
default_function_array_conversion(location_t loc,struct c_expr exp)1957 default_function_array_conversion (location_t loc, struct c_expr exp)
1958 {
1959   tree orig_exp = exp.value;
1960   tree type = TREE_TYPE (exp.value);
1961   enum tree_code code = TREE_CODE (type);
1962 
1963   switch (code)
1964     {
1965     case ARRAY_TYPE:
1966       {
1967 	bool not_lvalue = false;
1968 	bool lvalue_array_p;
1969 
1970 	while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1971 		|| CONVERT_EXPR_P (exp.value))
1972 	       && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1973 	  {
1974 	    if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1975 	      not_lvalue = true;
1976 	    exp.value = TREE_OPERAND (exp.value, 0);
1977 	  }
1978 
1979 	if (TREE_NO_WARNING (orig_exp))
1980 	  TREE_NO_WARNING (exp.value) = 1;
1981 
1982 	lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1983 	if (!flag_isoc99 && !lvalue_array_p)
1984 	  {
1985 	    /* Before C99, non-lvalue arrays do not decay to pointers.
1986 	       Normally, using such an array would be invalid; but it can
1987 	       be used correctly inside sizeof or as a statement expression.
1988 	       Thus, do not give an error here; an error will result later.  */
1989 	    return exp;
1990 	  }
1991 
1992 	exp.value = array_to_pointer_conversion (loc, exp.value);
1993       }
1994       break;
1995     case FUNCTION_TYPE:
1996       exp.value = function_to_pointer_conversion (loc, exp.value);
1997       break;
1998     default:
1999       break;
2000     }
2001 
2002   return exp;
2003 }
2004 
2005 struct c_expr
default_function_array_read_conversion(location_t loc,struct c_expr exp)2006 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2007 {
2008   mark_exp_read (exp.value);
2009   return default_function_array_conversion (loc, exp);
2010 }
2011 
2012 /* Return whether EXPR should be treated as an atomic lvalue for the
2013    purposes of load and store handling.  */
2014 
2015 static bool
really_atomic_lvalue(tree expr)2016 really_atomic_lvalue (tree expr)
2017 {
2018   if (error_operand_p (expr))
2019     return false;
2020   if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2021     return false;
2022   if (!lvalue_p (expr))
2023     return false;
2024 
2025   /* Ignore _Atomic on register variables, since their addresses can't
2026      be taken so (a) atomicity is irrelevant and (b) the normal atomic
2027      sequences wouldn't work.  Ignore _Atomic on structures containing
2028      bit-fields, since accessing elements of atomic structures or
2029      unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2030      it's undefined at translation time or execution time, and the
2031      normal atomic sequences again wouldn't work.  */
2032   while (handled_component_p (expr))
2033     {
2034       if (TREE_CODE (expr) == COMPONENT_REF
2035 	  && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2036 	return false;
2037       expr = TREE_OPERAND (expr, 0);
2038     }
2039   if (DECL_P (expr) && C_DECL_REGISTER (expr))
2040     return false;
2041   return true;
2042 }
2043 
2044 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2045    including converting functions and arrays to pointers if CONVERT_P.
2046    If READ_P, also mark the expression as having been read.  */
2047 
2048 struct c_expr
convert_lvalue_to_rvalue(location_t loc,struct c_expr exp,bool convert_p,bool read_p)2049 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2050 			  bool convert_p, bool read_p)
2051 {
2052   if (read_p)
2053     mark_exp_read (exp.value);
2054   if (convert_p)
2055     exp = default_function_array_conversion (loc, exp);
2056   if (really_atomic_lvalue (exp.value))
2057     {
2058       vec<tree, va_gc> *params;
2059       tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2060       tree expr_type = TREE_TYPE (exp.value);
2061       tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2062       tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2063 
2064       gcc_assert (TYPE_ATOMIC (expr_type));
2065 
2066       /* Expansion of a generic atomic load may require an addition
2067 	 element, so allocate enough to prevent a resize.  */
2068       vec_alloc (params, 4);
2069 
2070       /* Remove the qualifiers for the rest of the expressions and
2071 	 create the VAL temp variable to hold the RHS.  */
2072       nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2073       tmp = create_tmp_var_raw (nonatomic_type);
2074       tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2075       TREE_ADDRESSABLE (tmp) = 1;
2076       TREE_NO_WARNING (tmp) = 1;
2077 
2078       /* Issue __atomic_load (&expr, &tmp, SEQ_CST);  */
2079       fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2080       params->quick_push (expr_addr);
2081       params->quick_push (tmp_addr);
2082       params->quick_push (seq_cst);
2083       func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2084 
2085       /* EXPR is always read.  */
2086       mark_exp_read (exp.value);
2087 
2088       /* Return tmp which contains the value loaded.  */
2089       exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2090 			  NULL_TREE, NULL_TREE);
2091     }
2092   return exp;
2093 }
2094 
2095 /* EXP is an expression of integer type.  Apply the integer promotions
2096    to it and return the promoted value.  */
2097 
2098 tree
perform_integral_promotions(tree exp)2099 perform_integral_promotions (tree exp)
2100 {
2101   tree type = TREE_TYPE (exp);
2102   enum tree_code code = TREE_CODE (type);
2103 
2104   gcc_assert (INTEGRAL_TYPE_P (type));
2105 
2106   /* Normally convert enums to int,
2107      but convert wide enums to something wider.  */
2108   if (code == ENUMERAL_TYPE)
2109     {
2110       type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2111 					  TYPE_PRECISION (integer_type_node)),
2112 				     ((TYPE_PRECISION (type)
2113 				       >= TYPE_PRECISION (integer_type_node))
2114 				      && TYPE_UNSIGNED (type)));
2115 
2116       return convert (type, exp);
2117     }
2118 
2119   /* ??? This should no longer be needed now bit-fields have their
2120      proper types.  */
2121   if (TREE_CODE (exp) == COMPONENT_REF
2122       && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2123       /* If it's thinner than an int, promote it like a
2124 	 c_promoting_integer_type_p, otherwise leave it alone.  */
2125       && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2126 			   TYPE_PRECISION (integer_type_node)) < 0)
2127     return convert (integer_type_node, exp);
2128 
2129   if (c_promoting_integer_type_p (type))
2130     {
2131       /* Preserve unsignedness if not really getting any wider.  */
2132       if (TYPE_UNSIGNED (type)
2133 	  && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2134 	return convert (unsigned_type_node, exp);
2135 
2136       return convert (integer_type_node, exp);
2137     }
2138 
2139   return exp;
2140 }
2141 
2142 
2143 /* Perform default promotions for C data used in expressions.
2144    Enumeral types or short or char are converted to int.
2145    In addition, manifest constants symbols are replaced by their values.  */
2146 
2147 tree
default_conversion(tree exp)2148 default_conversion (tree exp)
2149 {
2150   tree orig_exp;
2151   tree type = TREE_TYPE (exp);
2152   enum tree_code code = TREE_CODE (type);
2153   tree promoted_type;
2154 
2155   mark_exp_read (exp);
2156 
2157   /* Functions and arrays have been converted during parsing.  */
2158   gcc_assert (code != FUNCTION_TYPE);
2159   if (code == ARRAY_TYPE)
2160     return exp;
2161 
2162   /* Constants can be used directly unless they're not loadable.  */
2163   if (TREE_CODE (exp) == CONST_DECL)
2164     exp = DECL_INITIAL (exp);
2165 
2166   /* Strip no-op conversions.  */
2167   orig_exp = exp;
2168   STRIP_TYPE_NOPS (exp);
2169 
2170   if (TREE_NO_WARNING (orig_exp))
2171     TREE_NO_WARNING (exp) = 1;
2172 
2173   if (code == VOID_TYPE)
2174     {
2175       error_at (EXPR_LOC_OR_LOC (exp, input_location),
2176 		"void value not ignored as it ought to be");
2177       return error_mark_node;
2178     }
2179 
2180   exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2181   if (exp == error_mark_node)
2182     return error_mark_node;
2183 
2184   promoted_type = targetm.promoted_type (type);
2185   if (promoted_type)
2186     return convert (promoted_type, exp);
2187 
2188   if (INTEGRAL_TYPE_P (type))
2189     return perform_integral_promotions (exp);
2190 
2191   return exp;
2192 }
2193 
2194 /* Look up COMPONENT in a structure or union TYPE.
2195 
2196    If the component name is not found, returns NULL_TREE.  Otherwise,
2197    the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2198    stepping down the chain to the component, which is in the last
2199    TREE_VALUE of the list.  Normally the list is of length one, but if
2200    the component is embedded within (nested) anonymous structures or
2201    unions, the list steps down the chain to the component.  */
2202 
2203 static tree
lookup_field(tree type,tree component)2204 lookup_field (tree type, tree component)
2205 {
2206   tree field;
2207 
2208   /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2209      to the field elements.  Use a binary search on this array to quickly
2210      find the element.  Otherwise, do a linear search.  TYPE_LANG_SPECIFIC
2211      will always be set for structures which have many elements.
2212 
2213      Duplicate field checking replaces duplicates with NULL_TREE so
2214      TYPE_LANG_SPECIFIC arrays are potentially no longer sorted.  In that
2215      case just iterate using DECL_CHAIN.  */
2216 
2217   if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2218       && !seen_error ())
2219     {
2220       int bot, top, half;
2221       tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2222 
2223       field = TYPE_FIELDS (type);
2224       bot = 0;
2225       top = TYPE_LANG_SPECIFIC (type)->s->len;
2226       while (top - bot > 1)
2227 	{
2228 	  half = (top - bot + 1) >> 1;
2229 	  field = field_array[bot+half];
2230 
2231 	  if (DECL_NAME (field) == NULL_TREE)
2232 	    {
2233 	      /* Step through all anon unions in linear fashion.  */
2234 	      while (DECL_NAME (field_array[bot]) == NULL_TREE)
2235 		{
2236 		  field = field_array[bot++];
2237 		  if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2238 		    {
2239 		      tree anon = lookup_field (TREE_TYPE (field), component);
2240 
2241 		      if (anon)
2242 			return tree_cons (NULL_TREE, field, anon);
2243 
2244 		      /* The Plan 9 compiler permits referring
2245 			 directly to an anonymous struct/union field
2246 			 using a typedef name.  */
2247 		      if (flag_plan9_extensions
2248 			  && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2249 			  && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2250 			      == TYPE_DECL)
2251 			  && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2252 			      == component))
2253 			break;
2254 		    }
2255 		}
2256 
2257 	      /* Entire record is only anon unions.  */
2258 	      if (bot > top)
2259 		return NULL_TREE;
2260 
2261 	      /* Restart the binary search, with new lower bound.  */
2262 	      continue;
2263 	    }
2264 
2265 	  if (DECL_NAME (field) == component)
2266 	    break;
2267 	  if (DECL_NAME (field) < component)
2268 	    bot += half;
2269 	  else
2270 	    top = bot + half;
2271 	}
2272 
2273       if (DECL_NAME (field_array[bot]) == component)
2274 	field = field_array[bot];
2275       else if (DECL_NAME (field) != component)
2276 	return NULL_TREE;
2277     }
2278   else
2279     {
2280       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2281 	{
2282 	  if (DECL_NAME (field) == NULL_TREE
2283 	      && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2284 	    {
2285 	      tree anon = lookup_field (TREE_TYPE (field), component);
2286 
2287 	      if (anon)
2288 		return tree_cons (NULL_TREE, field, anon);
2289 
2290 	      /* The Plan 9 compiler permits referring directly to an
2291 		 anonymous struct/union field using a typedef
2292 		 name.  */
2293 	      if (flag_plan9_extensions
2294 		  && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2295 		  && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2296 		  && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2297 		      == component))
2298 		break;
2299 	    }
2300 
2301 	  if (DECL_NAME (field) == component)
2302 	    break;
2303 	}
2304 
2305       if (field == NULL_TREE)
2306 	return NULL_TREE;
2307     }
2308 
2309   return tree_cons (NULL_TREE, field, NULL_TREE);
2310 }
2311 
2312 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES.  */
2313 
2314 static void
lookup_field_fuzzy_find_candidates(tree type,tree component,vec<tree> * candidates)2315 lookup_field_fuzzy_find_candidates (tree type, tree component,
2316 				    vec<tree> *candidates)
2317 {
2318   tree field;
2319   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2320     {
2321       if (DECL_NAME (field) == NULL_TREE
2322 	  && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2323 	lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2324 					    candidates);
2325 
2326       if (DECL_NAME (field))
2327 	candidates->safe_push (DECL_NAME (field));
2328     }
2329 }
2330 
2331 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2332    rather than returning a TREE_LIST for an exact match.  */
2333 
2334 static tree
lookup_field_fuzzy(tree type,tree component)2335 lookup_field_fuzzy (tree type, tree component)
2336 {
2337   gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2338 
2339   /* First, gather a list of candidates.  */
2340   auto_vec <tree> candidates;
2341 
2342   lookup_field_fuzzy_find_candidates (type, component,
2343 				      &candidates);
2344 
2345   return find_closest_identifier (component, &candidates);
2346 }
2347 
2348 /* Support function for build_component_ref's error-handling.
2349 
2350    Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2351    struct or union, should we suggest "DATUM->COMPONENT" as a hint?  */
2352 
2353 static bool
should_suggest_deref_p(tree datum_type)2354 should_suggest_deref_p (tree datum_type)
2355 {
2356   /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2357      allows "." for ptrs; we could be handling a failed attempt
2358      to access a property.  */
2359   if (c_dialect_objc ())
2360     return false;
2361 
2362   /* Only suggest it for pointers...  */
2363   if (TREE_CODE (datum_type) != POINTER_TYPE)
2364     return false;
2365 
2366   /* ...to structs/unions.  */
2367   tree underlying_type = TREE_TYPE (datum_type);
2368   enum tree_code code = TREE_CODE (underlying_type);
2369   if (code == RECORD_TYPE || code == UNION_TYPE)
2370     return true;
2371   else
2372     return false;
2373 }
2374 
2375 /* Make an expression to refer to the COMPONENT field of structure or
2376    union value DATUM.  COMPONENT is an IDENTIFIER_NODE.  LOC is the
2377    location of the COMPONENT_REF.  COMPONENT_LOC is the location
2378    of COMPONENT.  */
2379 
2380 tree
build_component_ref(location_t loc,tree datum,tree component,location_t component_loc)2381 build_component_ref (location_t loc, tree datum, tree component,
2382 		     location_t component_loc)
2383 {
2384   tree type = TREE_TYPE (datum);
2385   enum tree_code code = TREE_CODE (type);
2386   tree field = NULL;
2387   tree ref;
2388   bool datum_lvalue = lvalue_p (datum);
2389 
2390   if (!objc_is_public (datum, component))
2391     return error_mark_node;
2392 
2393   /* Detect Objective-C property syntax object.property.  */
2394   if (c_dialect_objc ()
2395       && (ref = objc_maybe_build_component_ref (datum, component)))
2396     return ref;
2397 
2398   /* See if there is a field or component with name COMPONENT.  */
2399 
2400   if (code == RECORD_TYPE || code == UNION_TYPE)
2401     {
2402       if (!COMPLETE_TYPE_P (type))
2403 	{
2404 	  c_incomplete_type_error (loc, NULL_TREE, type);
2405 	  return error_mark_node;
2406 	}
2407 
2408       field = lookup_field (type, component);
2409 
2410       if (!field)
2411 	{
2412 	  tree guessed_id = lookup_field_fuzzy (type, component);
2413 	  if (guessed_id)
2414 	    {
2415 	      /* Attempt to provide a fixit replacement hint, if
2416 		 we have a valid range for the component.  */
2417 	      location_t reported_loc
2418 		= (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2419 	      gcc_rich_location rich_loc (reported_loc);
2420 	      if (component_loc != UNKNOWN_LOCATION)
2421 		rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2422 	      error_at (&rich_loc,
2423 			"%qT has no member named %qE; did you mean %qE?",
2424 			type, component, guessed_id);
2425 	    }
2426 	  else
2427 	    error_at (loc, "%qT has no member named %qE", type, component);
2428 	  return error_mark_node;
2429 	}
2430 
2431       /* Accessing elements of atomic structures or unions is undefined
2432 	 behavior (C11 6.5.2.3#5).  */
2433       if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2434 	{
2435 	  if (code == RECORD_TYPE)
2436 	    warning_at (loc, 0, "accessing a member %qE of an atomic "
2437 			"structure %qE", component, datum);
2438 	  else
2439 	    warning_at (loc, 0, "accessing a member %qE of an atomic "
2440 			"union %qE", component, datum);
2441 	}
2442 
2443       /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2444 	 This might be better solved in future the way the C++ front
2445 	 end does it - by giving the anonymous entities each a
2446 	 separate name and type, and then have build_component_ref
2447 	 recursively call itself.  We can't do that here.  */
2448       do
2449 	{
2450 	  tree subdatum = TREE_VALUE (field);
2451 	  int quals;
2452 	  tree subtype;
2453 	  bool use_datum_quals;
2454 
2455 	  if (TREE_TYPE (subdatum) == error_mark_node)
2456 	    return error_mark_node;
2457 
2458 	  /* If this is an rvalue, it does not have qualifiers in C
2459 	     standard terms and we must avoid propagating such
2460 	     qualifiers down to a non-lvalue array that is then
2461 	     converted to a pointer.  */
2462 	  use_datum_quals = (datum_lvalue
2463 			     || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2464 
2465 	  quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2466 	  if (use_datum_quals)
2467 	    quals |= TYPE_QUALS (TREE_TYPE (datum));
2468 	  subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2469 
2470 	  ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2471 			NULL_TREE);
2472 	  SET_EXPR_LOCATION (ref, loc);
2473 	  if (TREE_READONLY (subdatum)
2474 	      || (use_datum_quals && TREE_READONLY (datum)))
2475 	    TREE_READONLY (ref) = 1;
2476 	  if (TREE_THIS_VOLATILE (subdatum)
2477 	      || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2478 	    TREE_THIS_VOLATILE (ref) = 1;
2479 
2480 	  if (TREE_DEPRECATED (subdatum))
2481 	    warn_deprecated_use (subdatum, NULL_TREE);
2482 
2483 	  datum = ref;
2484 
2485 	  field = TREE_CHAIN (field);
2486 	}
2487       while (field);
2488 
2489       return ref;
2490     }
2491   else if (should_suggest_deref_p (type))
2492     {
2493       /* Special-case the error message for "ptr.field" for the case
2494 	 where the user has confused "." vs "->".  */
2495       rich_location richloc (line_table, loc);
2496       /* "loc" should be the "." token.  */
2497       richloc.add_fixit_replace ("->");
2498       error_at (&richloc,
2499 		"%qE is a pointer; did you mean to use %<->%>?",
2500 		datum);
2501       return error_mark_node;
2502     }
2503   else if (code != ERROR_MARK)
2504     error_at (loc,
2505 	      "request for member %qE in something not a structure or union",
2506 	      component);
2507 
2508   return error_mark_node;
2509 }
2510 
2511 /* Given an expression PTR for a pointer, return an expression
2512    for the value pointed to.
2513    ERRORSTRING is the name of the operator to appear in error messages.
2514 
2515    LOC is the location to use for the generated tree.  */
2516 
2517 tree
build_indirect_ref(location_t loc,tree ptr,ref_operator errstring)2518 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2519 {
2520   tree pointer = default_conversion (ptr);
2521   tree type = TREE_TYPE (pointer);
2522   tree ref;
2523 
2524   if (TREE_CODE (type) == POINTER_TYPE)
2525     {
2526       if (CONVERT_EXPR_P (pointer)
2527           || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2528 	{
2529 	  /* If a warning is issued, mark it to avoid duplicates from
2530 	     the backend.  This only needs to be done at
2531 	     warn_strict_aliasing > 2.  */
2532 	  if (warn_strict_aliasing > 2)
2533 	    if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2534 					 type, TREE_OPERAND (pointer, 0)))
2535 	      TREE_NO_WARNING (pointer) = 1;
2536 	}
2537 
2538       if (TREE_CODE (pointer) == ADDR_EXPR
2539 	  && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2540 	      == TREE_TYPE (type)))
2541 	{
2542 	  ref = TREE_OPERAND (pointer, 0);
2543 	  protected_set_expr_location (ref, loc);
2544 	  return ref;
2545 	}
2546       else
2547 	{
2548 	  tree t = TREE_TYPE (type);
2549 
2550 	  ref = build1 (INDIRECT_REF, t, pointer);
2551 
2552 	  if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2553 	    {
2554 	      if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2555 		{
2556 		  error_at (loc, "dereferencing pointer to incomplete type "
2557 			    "%qT", t);
2558 		  C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2559 		}
2560 	      return error_mark_node;
2561 	    }
2562 	  if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2563 	    warning_at (loc, 0, "dereferencing %<void *%> pointer");
2564 
2565 	  /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2566 	     so that we get the proper error message if the result is used
2567 	     to assign to.  Also, &* is supposed to be a no-op.
2568 	     And ANSI C seems to specify that the type of the result
2569 	     should be the const type.  */
2570 	  /* A de-reference of a pointer to const is not a const.  It is valid
2571 	     to change it via some other pointer.  */
2572 	  TREE_READONLY (ref) = TYPE_READONLY (t);
2573 	  TREE_SIDE_EFFECTS (ref)
2574 	    = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2575 	  TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2576 	  protected_set_expr_location (ref, loc);
2577 	  return ref;
2578 	}
2579     }
2580   else if (TREE_CODE (pointer) != ERROR_MARK)
2581     invalid_indirection_error (loc, type, errstring);
2582 
2583   return error_mark_node;
2584 }
2585 
2586 /* This handles expressions of the form "a[i]", which denotes
2587    an array reference.
2588 
2589    This is logically equivalent in C to *(a+i), but we may do it differently.
2590    If A is a variable or a member, we generate a primitive ARRAY_REF.
2591    This avoids forcing the array out of registers, and can work on
2592    arrays that are not lvalues (for example, members of structures returned
2593    by functions).
2594 
2595    For vector types, allow vector[i] but not i[vector], and create
2596    *(((type*)&vectortype) + i) for the expression.
2597 
2598    LOC is the location to use for the returned expression.  */
2599 
2600 tree
build_array_ref(location_t loc,tree array,tree index)2601 build_array_ref (location_t loc, tree array, tree index)
2602 {
2603   tree ret;
2604   bool swapped = false;
2605   if (TREE_TYPE (array) == error_mark_node
2606       || TREE_TYPE (index) == error_mark_node)
2607     return error_mark_node;
2608 
2609   if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2610       && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2611       /* Allow vector[index] but not index[vector].  */
2612       && !VECTOR_TYPE_P (TREE_TYPE (array)))
2613     {
2614       if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2615 	  && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2616 	{
2617           error_at (loc,
2618             "subscripted value is neither array nor pointer nor vector");
2619 
2620 	  return error_mark_node;
2621 	}
2622       std::swap (array, index);
2623       swapped = true;
2624     }
2625 
2626   if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2627     {
2628       error_at (loc, "array subscript is not an integer");
2629       return error_mark_node;
2630     }
2631 
2632   if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2633     {
2634       error_at (loc, "subscripted value is pointer to function");
2635       return error_mark_node;
2636     }
2637 
2638   /* ??? Existing practice has been to warn only when the char
2639      index is syntactically the index, not for char[array].  */
2640   if (!swapped)
2641      warn_array_subscript_with_type_char (loc, index);
2642 
2643   /* Apply default promotions *after* noticing character types.  */
2644   index = default_conversion (index);
2645   if (index == error_mark_node)
2646     return error_mark_node;
2647 
2648   gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2649 
2650   bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2651   bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2652 
2653   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2654     {
2655       tree rval, type;
2656 
2657       /* An array that is indexed by a non-constant
2658 	 cannot be stored in a register; we must be able to do
2659 	 address arithmetic on its address.
2660 	 Likewise an array of elements of variable size.  */
2661       if (TREE_CODE (index) != INTEGER_CST
2662 	  || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2663 	      && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2664 	{
2665 	  if (!c_mark_addressable (array, true))
2666 	    return error_mark_node;
2667 	}
2668       /* An array that is indexed by a constant value which is not within
2669 	 the array bounds cannot be stored in a register either; because we
2670 	 would get a crash in store_bit_field/extract_bit_field when trying
2671 	 to access a non-existent part of the register.  */
2672       if (TREE_CODE (index) == INTEGER_CST
2673 	  && TYPE_DOMAIN (TREE_TYPE (array))
2674 	  && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2675 	{
2676 	  if (!c_mark_addressable (array))
2677 	    return error_mark_node;
2678 	}
2679 
2680       if ((pedantic || warn_c90_c99_compat)
2681 	  && ! was_vector)
2682 	{
2683 	  tree foo = array;
2684 	  while (TREE_CODE (foo) == COMPONENT_REF)
2685 	    foo = TREE_OPERAND (foo, 0);
2686 	  if (VAR_P (foo) && C_DECL_REGISTER (foo))
2687 	    pedwarn (loc, OPT_Wpedantic,
2688 		     "ISO C forbids subscripting %<register%> array");
2689 	  else if (!lvalue_p (foo))
2690 	    pedwarn_c90 (loc, OPT_Wpedantic,
2691 			 "ISO C90 forbids subscripting non-lvalue "
2692 			 "array");
2693 	}
2694 
2695       type = TREE_TYPE (TREE_TYPE (array));
2696       rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2697       /* Array ref is const/volatile if the array elements are
2698 	 or if the array is.  */
2699       TREE_READONLY (rval)
2700 	|= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2701 	    | TREE_READONLY (array));
2702       TREE_SIDE_EFFECTS (rval)
2703 	|= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2704 	    | TREE_SIDE_EFFECTS (array));
2705       TREE_THIS_VOLATILE (rval)
2706 	|= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2707 	    /* This was added by rms on 16 Nov 91.
2708 	       It fixes  vol struct foo *a;  a->elts[1]
2709 	       in an inline function.
2710 	       Hope it doesn't break something else.  */
2711 	    | TREE_THIS_VOLATILE (array));
2712       ret = require_complete_type (loc, rval);
2713       protected_set_expr_location (ret, loc);
2714       if (non_lvalue)
2715 	ret = non_lvalue_loc (loc, ret);
2716       return ret;
2717     }
2718   else
2719     {
2720       tree ar = default_conversion (array);
2721 
2722       if (ar == error_mark_node)
2723 	return ar;
2724 
2725       gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2726       gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2727 
2728       ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2729 						      index, false),
2730 				RO_ARRAY_INDEXING);
2731       if (non_lvalue)
2732 	ret = non_lvalue_loc (loc, ret);
2733       return ret;
2734     }
2735 }
2736 
2737 /* Build an external reference to identifier ID.  FUN indicates
2738    whether this will be used for a function call.  LOC is the source
2739    location of the identifier.  This sets *TYPE to the type of the
2740    identifier, which is not the same as the type of the returned value
2741    for CONST_DECLs defined as enum constants.  If the type of the
2742    identifier is not available, *TYPE is set to NULL.  */
2743 tree
build_external_ref(location_t loc,tree id,bool fun,tree * type)2744 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2745 {
2746   tree ref;
2747   tree decl = lookup_name (id);
2748 
2749   /* In Objective-C, an instance variable (ivar) may be preferred to
2750      whatever lookup_name() found.  */
2751   decl = objc_lookup_ivar (decl, id);
2752 
2753   *type = NULL;
2754   if (decl && decl != error_mark_node)
2755     {
2756       ref = decl;
2757       *type = TREE_TYPE (ref);
2758     }
2759   else if (fun)
2760     /* Implicit function declaration.  */
2761     ref = implicitly_declare (loc, id);
2762   else if (decl == error_mark_node)
2763     /* Don't complain about something that's already been
2764        complained about.  */
2765     return error_mark_node;
2766   else
2767     {
2768       undeclared_variable (loc, id);
2769       return error_mark_node;
2770     }
2771 
2772   if (TREE_TYPE (ref) == error_mark_node)
2773     return error_mark_node;
2774 
2775   if (TREE_DEPRECATED (ref))
2776     warn_deprecated_use (ref, NULL_TREE);
2777 
2778   /* Recursive call does not count as usage.  */
2779   if (ref != current_function_decl)
2780     {
2781       TREE_USED (ref) = 1;
2782     }
2783 
2784   if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2785     {
2786       if (!in_sizeof && !in_typeof)
2787 	C_DECL_USED (ref) = 1;
2788       else if (DECL_INITIAL (ref) == NULL_TREE
2789 	       && DECL_EXTERNAL (ref)
2790 	       && !TREE_PUBLIC (ref))
2791 	record_maybe_used_decl (ref);
2792     }
2793 
2794   if (TREE_CODE (ref) == CONST_DECL)
2795     {
2796       used_types_insert (TREE_TYPE (ref));
2797 
2798       if (warn_cxx_compat
2799 	  && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2800 	  && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2801 	{
2802 	  warning_at (loc, OPT_Wc___compat,
2803 		      ("enum constant defined in struct or union "
2804 		       "is not visible in C++"));
2805 	  inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2806 	}
2807 
2808       ref = DECL_INITIAL (ref);
2809       TREE_CONSTANT (ref) = 1;
2810     }
2811   else if (current_function_decl != NULL_TREE
2812 	   && !DECL_FILE_SCOPE_P (current_function_decl)
2813 	   && (VAR_OR_FUNCTION_DECL_P (ref)
2814 	       || TREE_CODE (ref) == PARM_DECL))
2815     {
2816       tree context = decl_function_context (ref);
2817 
2818       if (context != NULL_TREE && context != current_function_decl)
2819 	DECL_NONLOCAL (ref) = 1;
2820     }
2821   /* C99 6.7.4p3: An inline definition of a function with external
2822      linkage ... shall not contain a reference to an identifier with
2823      internal linkage.  */
2824   else if (current_function_decl != NULL_TREE
2825 	   && DECL_DECLARED_INLINE_P (current_function_decl)
2826 	   && DECL_EXTERNAL (current_function_decl)
2827 	   && VAR_OR_FUNCTION_DECL_P (ref)
2828 	   && (!VAR_P (ref) || TREE_STATIC (ref))
2829 	   && ! TREE_PUBLIC (ref)
2830 	   && DECL_CONTEXT (ref) != current_function_decl)
2831     record_inline_static (loc, current_function_decl, ref,
2832 			  csi_internal);
2833 
2834   return ref;
2835 }
2836 
2837 /* Record details of decls possibly used inside sizeof or typeof.  */
2838 struct maybe_used_decl
2839 {
2840   /* The decl.  */
2841   tree decl;
2842   /* The level seen at (in_sizeof + in_typeof).  */
2843   int level;
2844   /* The next one at this level or above, or NULL.  */
2845   struct maybe_used_decl *next;
2846 };
2847 
2848 static struct maybe_used_decl *maybe_used_decls;
2849 
2850 /* Record that DECL, an undefined static function reference seen
2851    inside sizeof or typeof, might be used if the operand of sizeof is
2852    a VLA type or the operand of typeof is a variably modified
2853    type.  */
2854 
2855 static void
record_maybe_used_decl(tree decl)2856 record_maybe_used_decl (tree decl)
2857 {
2858   struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2859   t->decl = decl;
2860   t->level = in_sizeof + in_typeof;
2861   t->next = maybe_used_decls;
2862   maybe_used_decls = t;
2863 }
2864 
2865 /* Pop the stack of decls possibly used inside sizeof or typeof.  If
2866    USED is false, just discard them.  If it is true, mark them used
2867    (if no longer inside sizeof or typeof) or move them to the next
2868    level up (if still inside sizeof or typeof).  */
2869 
2870 void
pop_maybe_used(bool used)2871 pop_maybe_used (bool used)
2872 {
2873   struct maybe_used_decl *p = maybe_used_decls;
2874   int cur_level = in_sizeof + in_typeof;
2875   while (p && p->level > cur_level)
2876     {
2877       if (used)
2878 	{
2879 	  if (cur_level == 0)
2880 	    C_DECL_USED (p->decl) = 1;
2881 	  else
2882 	    p->level = cur_level;
2883 	}
2884       p = p->next;
2885     }
2886   if (!used || cur_level == 0)
2887     maybe_used_decls = p;
2888 }
2889 
2890 /* Return the result of sizeof applied to EXPR.  */
2891 
2892 struct c_expr
c_expr_sizeof_expr(location_t loc,struct c_expr expr)2893 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2894 {
2895   struct c_expr ret;
2896   if (expr.value == error_mark_node)
2897     {
2898       ret.value = error_mark_node;
2899       ret.original_code = ERROR_MARK;
2900       ret.original_type = NULL;
2901       pop_maybe_used (false);
2902     }
2903   else
2904     {
2905       bool expr_const_operands = true;
2906 
2907       if (TREE_CODE (expr.value) == PARM_DECL
2908 	  && C_ARRAY_PARAMETER (expr.value))
2909 	{
2910 	  auto_diagnostic_group d;
2911 	  if (warning_at (loc, OPT_Wsizeof_array_argument,
2912 			  "%<sizeof%> on array function parameter %qE will "
2913 			  "return size of %qT", expr.value,
2914 			  TREE_TYPE (expr.value)))
2915 	    inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2916 	}
2917       tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2918 				       &expr_const_operands);
2919       ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2920       c_last_sizeof_arg = expr.value;
2921       c_last_sizeof_loc = loc;
2922       ret.original_code = SIZEOF_EXPR;
2923       ret.original_type = NULL;
2924       if (c_vla_type_p (TREE_TYPE (folded_expr)))
2925 	{
2926 	  /* sizeof is evaluated when given a vla (C99 6.5.3.4p2).  */
2927 	  ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2928 			      folded_expr, ret.value);
2929 	  C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2930 	  SET_EXPR_LOCATION (ret.value, loc);
2931 	}
2932       pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2933     }
2934   return ret;
2935 }
2936 
2937 /* Return the result of sizeof applied to T, a structure for the type
2938    name passed to sizeof (rather than the type itself).  LOC is the
2939    location of the original expression.  */
2940 
2941 struct c_expr
c_expr_sizeof_type(location_t loc,struct c_type_name * t)2942 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2943 {
2944   tree type;
2945   struct c_expr ret;
2946   tree type_expr = NULL_TREE;
2947   bool type_expr_const = true;
2948   type = groktypename (t, &type_expr, &type_expr_const);
2949   ret.value = c_sizeof (loc, type);
2950   c_last_sizeof_arg = type;
2951   c_last_sizeof_loc = loc;
2952   ret.original_code = SIZEOF_EXPR;
2953   ret.original_type = NULL;
2954   if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2955       && c_vla_type_p (type))
2956     {
2957       /* If the type is a [*] array, it is a VLA but is represented as
2958 	 having a size of zero.  In such a case we must ensure that
2959 	 the result of sizeof does not get folded to a constant by
2960 	 c_fully_fold, because if the size is evaluated the result is
2961 	 not constant and so constraints on zero or negative size
2962 	 arrays must not be applied when this sizeof call is inside
2963 	 another array declarator.  */
2964       if (!type_expr)
2965 	type_expr = integer_zero_node;
2966       ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2967 			  type_expr, ret.value);
2968       C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2969     }
2970   pop_maybe_used (type != error_mark_node
2971 		  ? C_TYPE_VARIABLE_SIZE (type) : false);
2972   return ret;
2973 }
2974 
2975 /* Build a function call to function FUNCTION with parameters PARAMS.
2976    The function call is at LOC.
2977    PARAMS is a list--a chain of TREE_LIST nodes--in which the
2978    TREE_VALUE of each node is a parameter-expression.
2979    FUNCTION's data type may be a function type or a pointer-to-function.  */
2980 
2981 tree
build_function_call(location_t loc,tree function,tree params)2982 build_function_call (location_t loc, tree function, tree params)
2983 {
2984   vec<tree, va_gc> *v;
2985   tree ret;
2986 
2987   vec_alloc (v, list_length (params));
2988   for (; params; params = TREE_CHAIN (params))
2989     v->quick_push (TREE_VALUE (params));
2990   ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
2991   vec_free (v);
2992   return ret;
2993 }
2994 
2995 /* Give a note about the location of the declaration of DECL.  */
2996 
2997 static void
inform_declaration(tree decl)2998 inform_declaration (tree decl)
2999 {
3000   if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
3001     inform (DECL_SOURCE_LOCATION (decl), "declared here");
3002 }
3003 
3004 /* Build a function call to function FUNCTION with parameters PARAMS.
3005    ORIGTYPES, if not NULL, is a vector of types; each element is
3006    either NULL or the original type of the corresponding element in
3007    PARAMS.  The original type may differ from TREE_TYPE of the
3008    parameter for enums.  FUNCTION's data type may be a function type
3009    or pointer-to-function.  This function changes the elements of
3010    PARAMS.  */
3011 
3012 tree
build_function_call_vec(location_t loc,vec<location_t> arg_loc,tree function,vec<tree,va_gc> * params,vec<tree,va_gc> * origtypes)3013 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3014 			 tree function, vec<tree, va_gc> *params,
3015 			 vec<tree, va_gc> *origtypes)
3016 {
3017   tree fntype, fundecl = NULL_TREE;
3018   tree name = NULL_TREE, result;
3019   tree tem;
3020   int nargs;
3021   tree *argarray;
3022 
3023 
3024   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
3025   STRIP_TYPE_NOPS (function);
3026 
3027   /* Convert anything with function type to a pointer-to-function.  */
3028   if (TREE_CODE (function) == FUNCTION_DECL)
3029     {
3030       name = DECL_NAME (function);
3031 
3032       if (flag_tm)
3033 	tm_malloc_replacement (function);
3034       fundecl = function;
3035       /* Atomic functions have type checking/casting already done.  They are
3036 	 often rewritten and don't match the original parameter list.  */
3037       if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3038         origtypes = NULL;
3039     }
3040   if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3041     function = function_to_pointer_conversion (loc, function);
3042 
3043   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3044      expressions, like those used for ObjC messenger dispatches.  */
3045   if (params && !params->is_empty ())
3046     function = objc_rewrite_function_call (function, (*params)[0]);
3047 
3048   function = c_fully_fold (function, false, NULL);
3049 
3050   fntype = TREE_TYPE (function);
3051 
3052   if (TREE_CODE (fntype) == ERROR_MARK)
3053     return error_mark_node;
3054 
3055   if (!(TREE_CODE (fntype) == POINTER_TYPE
3056 	&& TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3057     {
3058       if (!flag_diagnostics_show_caret)
3059 	error_at (loc,
3060 		  "called object %qE is not a function or function pointer",
3061 		  function);
3062       else if (DECL_P (function))
3063 	{
3064 	  error_at (loc,
3065 		    "called object %qD is not a function or function pointer",
3066 		    function);
3067 	  inform_declaration (function);
3068 	}
3069       else
3070 	error_at (loc,
3071 		  "called object is not a function or function pointer");
3072       return error_mark_node;
3073     }
3074 
3075   if (fundecl && TREE_THIS_VOLATILE (fundecl))
3076     current_function_returns_abnormally = 1;
3077 
3078   /* fntype now gets the type of function pointed to.  */
3079   fntype = TREE_TYPE (fntype);
3080 
3081   /* Convert the parameters to the types declared in the
3082      function prototype, or apply default promotions.  */
3083 
3084   nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3085 			     origtypes, function, fundecl);
3086   if (nargs < 0)
3087     return error_mark_node;
3088 
3089   /* Check that the function is called through a compatible prototype.
3090      If it is not, warn.  */
3091   if (CONVERT_EXPR_P (function)
3092       && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3093       && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3094       && !comptypes (fntype, TREE_TYPE (tem)))
3095     {
3096       tree return_type = TREE_TYPE (fntype);
3097 
3098       /* This situation leads to run-time undefined behavior.  We can't,
3099 	 therefore, simply error unless we can prove that all possible
3100 	 executions of the program must execute the code.  */
3101       warning_at (loc, 0, "function called through a non-compatible type");
3102 
3103       if (VOID_TYPE_P (return_type)
3104 	  && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3105 	pedwarn (loc, 0,
3106 		 "function with qualified void return type called");
3107      }
3108 
3109   argarray = vec_safe_address (params);
3110 
3111   /* Check that arguments to builtin functions match the expectations.  */
3112   if (fundecl && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL)
3113       && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3114 					    argarray))
3115     return error_mark_node;
3116 
3117   /* Check that the arguments to the function are valid.  */
3118   bool warned_p = check_function_arguments (loc, fundecl, fntype,
3119 					    nargs, argarray, &arg_loc);
3120 
3121   if (name != NULL_TREE
3122       && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3123     {
3124       if (require_constant_value)
3125 	result
3126 	  = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3127 						   function, nargs, argarray);
3128       else
3129 	result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3130 					    function, nargs, argarray);
3131       if (TREE_CODE (result) == NOP_EXPR
3132 	  && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3133 	STRIP_TYPE_NOPS (result);
3134     }
3135   else
3136     result = build_call_array_loc (loc, TREE_TYPE (fntype),
3137 				   function, nargs, argarray);
3138   /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3139      later.  */
3140   if (warned_p && TREE_CODE (result) == CALL_EXPR)
3141     TREE_NO_WARNING (result) = 1;
3142 
3143   /* In this improbable scenario, a nested function returns a VM type.
3144      Create a TARGET_EXPR so that the call always has a LHS, much as
3145      what the C++ FE does for functions returning non-PODs.  */
3146   if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3147     {
3148       tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3149       result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3150 		       NULL_TREE, NULL_TREE);
3151     }
3152 
3153   if (VOID_TYPE_P (TREE_TYPE (result)))
3154     {
3155       if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3156 	pedwarn (loc, 0,
3157 		 "function with qualified void return type called");
3158       return result;
3159     }
3160   return require_complete_type (loc, result);
3161 }
3162 
3163 /* Like build_function_call_vec, but call also resolve_overloaded_builtin.  */
3164 
3165 tree
c_build_function_call_vec(location_t loc,vec<location_t> arg_loc,tree function,vec<tree,va_gc> * params,vec<tree,va_gc> * origtypes)3166 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3167 			   tree function, vec<tree, va_gc> *params,
3168 			   vec<tree, va_gc> *origtypes)
3169 {
3170   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
3171   STRIP_TYPE_NOPS (function);
3172 
3173   /* Convert anything with function type to a pointer-to-function.  */
3174   if (TREE_CODE (function) == FUNCTION_DECL)
3175     {
3176       /* Implement type-directed function overloading for builtins.
3177 	 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3178 	 handle all the type checking.  The result is a complete expression
3179 	 that implements this function call.  */
3180       tree tem = resolve_overloaded_builtin (loc, function, params);
3181       if (tem)
3182 	return tem;
3183     }
3184   return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3185 }
3186 
3187 /* Helper for convert_arguments called to convert the VALue of argument
3188    number ARGNUM from ORIGTYPE to the corresponding parameter number
3189    PARMNUM and TYPE.
3190    PLOC is the location where the conversion is being performed.
3191    FUNCTION and FUNDECL are the same as in convert_arguments.
3192    VALTYPE is the original type of VAL before the conversion and,
3193    for EXCESS_PRECISION_EXPR, the operand of the expression.
3194    NPC is true if VAL represents the null pointer constant (VAL itself
3195    will have been folded to an integer constant).
3196    RNAME is the same as FUNCTION except in Objective C when it's
3197    the function selector.
3198    EXCESS_PRECISION is true when VAL was originally represented
3199    as EXCESS_PRECISION_EXPR.
3200    WARNOPT is the same as in convert_for_assignment.  */
3201 
3202 static tree
convert_argument(location_t ploc,tree function,tree fundecl,tree type,tree origtype,tree val,tree valtype,bool npc,tree rname,int parmnum,int argnum,bool excess_precision,int warnopt)3203 convert_argument (location_t ploc, tree function, tree fundecl,
3204 		  tree type, tree origtype, tree val, tree valtype,
3205 		  bool npc, tree rname, int parmnum, int argnum,
3206 		  bool excess_precision, int warnopt)
3207 {
3208   /* Formal parm type is specified by a function prototype.  */
3209 
3210   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3211     {
3212       error_at (ploc, "type of formal parameter %d is incomplete",
3213 		parmnum + 1);
3214       return val;
3215     }
3216 
3217   /* Optionally warn about conversions that differ from the default
3218      conversions.  */
3219   if (warn_traditional_conversion || warn_traditional)
3220     {
3221       unsigned int formal_prec = TYPE_PRECISION (type);
3222 
3223       if (INTEGRAL_TYPE_P (type)
3224 	  && TREE_CODE (valtype) == REAL_TYPE)
3225 	warning_at (ploc, OPT_Wtraditional_conversion,
3226 		    "passing argument %d of %qE as integer rather "
3227 		    "than floating due to prototype",
3228 		    argnum, rname);
3229       if (INTEGRAL_TYPE_P (type)
3230 	  && TREE_CODE (valtype) == COMPLEX_TYPE)
3231 	warning_at (ploc, OPT_Wtraditional_conversion,
3232 		    "passing argument %d of %qE as integer rather "
3233 		    "than complex due to prototype",
3234 		    argnum, rname);
3235       else if (TREE_CODE (type) == COMPLEX_TYPE
3236 	       && TREE_CODE (valtype) == REAL_TYPE)
3237 	warning_at (ploc, OPT_Wtraditional_conversion,
3238 		    "passing argument %d of %qE as complex rather "
3239 		    "than floating due to prototype",
3240 		    argnum, rname);
3241       else if (TREE_CODE (type) == REAL_TYPE
3242 	       && INTEGRAL_TYPE_P (valtype))
3243 	warning_at (ploc, OPT_Wtraditional_conversion,
3244 		    "passing argument %d of %qE as floating rather "
3245 		    "than integer due to prototype",
3246 		    argnum, rname);
3247       else if (TREE_CODE (type) == COMPLEX_TYPE
3248 	       && INTEGRAL_TYPE_P (valtype))
3249 	warning_at (ploc, OPT_Wtraditional_conversion,
3250 		    "passing argument %d of %qE as complex rather "
3251 		    "than integer due to prototype",
3252 		    argnum, rname);
3253       else if (TREE_CODE (type) == REAL_TYPE
3254 	       && TREE_CODE (valtype) == COMPLEX_TYPE)
3255 	warning_at (ploc, OPT_Wtraditional_conversion,
3256 		    "passing argument %d of %qE as floating rather "
3257 		    "than complex due to prototype",
3258 		    argnum, rname);
3259       /* ??? At some point, messages should be written about
3260 	 conversions between complex types, but that's too messy
3261 	 to do now.  */
3262       else if (TREE_CODE (type) == REAL_TYPE
3263 	       && TREE_CODE (valtype) == REAL_TYPE)
3264 	{
3265 	  /* Warn if any argument is passed as `float',
3266 	     since without a prototype it would be `double'.  */
3267 	  if (formal_prec == TYPE_PRECISION (float_type_node)
3268 	      && type != dfloat32_type_node)
3269 	    warning_at (ploc, 0,
3270 			"passing argument %d of %qE as %<float%> "
3271 			"rather than %<double%> due to prototype",
3272 			argnum, rname);
3273 
3274 	  /* Warn if mismatch between argument and prototype
3275 	     for decimal float types.  Warn of conversions with
3276 	     binary float types and of precision narrowing due to
3277 	     prototype.  */
3278 	  else if (type != valtype
3279 		   && (type == dfloat32_type_node
3280 		       || type == dfloat64_type_node
3281 		       || type == dfloat128_type_node
3282 		       || valtype == dfloat32_type_node
3283 		       || valtype == dfloat64_type_node
3284 		       || valtype == dfloat128_type_node)
3285 		   && (formal_prec
3286 		       <= TYPE_PRECISION (valtype)
3287 		       || (type == dfloat128_type_node
3288 			   && (valtype
3289 			       != dfloat64_type_node
3290 			       && (valtype
3291 				   != dfloat32_type_node)))
3292 		       || (type == dfloat64_type_node
3293 			   && (valtype
3294 			       != dfloat32_type_node))))
3295 	    warning_at (ploc, 0,
3296 			"passing argument %d of %qE as %qT "
3297 			"rather than %qT due to prototype",
3298 			argnum, rname, type, valtype);
3299 
3300 	}
3301       /* Detect integer changing in width or signedness.
3302 	 These warnings are only activated with
3303 	 -Wtraditional-conversion, not with -Wtraditional.  */
3304       else if (warn_traditional_conversion
3305 	       && INTEGRAL_TYPE_P (type)
3306 	       && INTEGRAL_TYPE_P (valtype))
3307 	{
3308 	  tree would_have_been = default_conversion (val);
3309 	  tree type1 = TREE_TYPE (would_have_been);
3310 
3311 	  if (val == error_mark_node)
3312 	    /* VAL could have been of incomplete type.  */;
3313 	  else if (TREE_CODE (type) == ENUMERAL_TYPE
3314 		   && (TYPE_MAIN_VARIANT (type)
3315 		       == TYPE_MAIN_VARIANT (valtype)))
3316 	    /* No warning if function asks for enum
3317 	       and the actual arg is that enum type.  */
3318 	    ;
3319 	  else if (formal_prec != TYPE_PRECISION (type1))
3320 	    warning_at (ploc, OPT_Wtraditional_conversion,
3321 			"passing argument %d of %qE "
3322 			"with different width due to prototype",
3323 			argnum, rname);
3324 	  else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3325 	    ;
3326 	  /* Don't complain if the formal parameter type
3327 	     is an enum, because we can't tell now whether
3328 	     the value was an enum--even the same enum.  */
3329 	  else if (TREE_CODE (type) == ENUMERAL_TYPE)
3330 	    ;
3331 	  else if (TREE_CODE (val) == INTEGER_CST
3332 		   && int_fits_type_p (val, type))
3333 	    /* Change in signedness doesn't matter
3334 	       if a constant value is unaffected.  */
3335 	    ;
3336 	  /* If the value is extended from a narrower
3337 	     unsigned type, it doesn't matter whether we
3338 	     pass it as signed or unsigned; the value
3339 	     certainly is the same either way.  */
3340 	  else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3341 		   && TYPE_UNSIGNED (valtype))
3342 	    ;
3343 	  else if (TYPE_UNSIGNED (type))
3344 	    warning_at (ploc, OPT_Wtraditional_conversion,
3345 			"passing argument %d of %qE "
3346 			"as unsigned due to prototype",
3347 			argnum, rname);
3348 	  else
3349 	    warning_at (ploc, OPT_Wtraditional_conversion,
3350 			"passing argument %d of %qE "
3351 			"as signed due to prototype",
3352 			argnum, rname);
3353 	}
3354     }
3355 
3356   /* Possibly restore an EXCESS_PRECISION_EXPR for the
3357      sake of better warnings from convert_and_check.  */
3358   if (excess_precision)
3359     val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3360 
3361   tree parmval = convert_for_assignment (ploc, ploc, type,
3362 					 val, origtype, ic_argpass,
3363 					 npc, fundecl, function,
3364 					 parmnum + 1, warnopt);
3365 
3366   if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3367       && INTEGRAL_TYPE_P (type)
3368       && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3369     parmval = default_conversion (parmval);
3370 
3371   return parmval;
3372 }
3373 
3374 /* Convert the argument expressions in the vector VALUES
3375    to the types in the list TYPELIST.
3376 
3377    If TYPELIST is exhausted, or when an element has NULL as its type,
3378    perform the default conversions.
3379 
3380    ORIGTYPES is the original types of the expressions in VALUES.  This
3381    holds the type of enum values which have been converted to integral
3382    types.  It may be NULL.
3383 
3384    FUNCTION is a tree for the called function.  It is used only for
3385    error messages, where it is formatted with %qE.
3386 
3387    This is also where warnings about wrong number of args are generated.
3388 
3389    ARG_LOC are locations of function arguments (if any).
3390 
3391    Returns the actual number of arguments processed (which may be less
3392    than the length of VALUES in some error situations), or -1 on
3393    failure.  */
3394 
3395 static int
convert_arguments(location_t loc,vec<location_t> arg_loc,tree typelist,vec<tree,va_gc> * values,vec<tree,va_gc> * origtypes,tree function,tree fundecl)3396 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3397 		   vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3398 		   tree function, tree fundecl)
3399 {
3400   unsigned int parmnum;
3401   bool error_args = false;
3402   const bool type_generic = fundecl
3403     && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3404   bool type_generic_remove_excess_precision = false;
3405   bool type_generic_overflow_p = false;
3406   tree selector;
3407 
3408   /* Change pointer to function to the function itself for
3409      diagnostics.  */
3410   if (TREE_CODE (function) == ADDR_EXPR
3411       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3412     function = TREE_OPERAND (function, 0);
3413 
3414   /* Handle an ObjC selector specially for diagnostics.  */
3415   selector = objc_message_selector ();
3416 
3417   /* For a call to a built-in function declared without a prototype,
3418      set to the built-in function's argument list.  */
3419   tree builtin_typelist = NULL_TREE;
3420 
3421   /* For type-generic built-in functions, determine whether excess
3422      precision should be removed (classification) or not
3423      (comparison).  */
3424   if (fundecl
3425       && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3426     {
3427       built_in_function code = DECL_FUNCTION_CODE (fundecl);
3428       if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3429 	{
3430 	  /* For a call to a built-in function declared without a prototype
3431 	     use the types of the parameters of the internal built-in to
3432 	     match those of the arguments to.  */
3433 	  if (tree bdecl = builtin_decl_explicit (code))
3434 	    builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3435 	}
3436 
3437       /* For type-generic built-in functions, determine whether excess
3438 	 precision should be removed (classification) or not
3439 	 (comparison).  */
3440       if (type_generic)
3441 	switch (code)
3442 	  {
3443 	  case BUILT_IN_ISFINITE:
3444 	  case BUILT_IN_ISINF:
3445 	  case BUILT_IN_ISINF_SIGN:
3446 	  case BUILT_IN_ISNAN:
3447 	  case BUILT_IN_ISNORMAL:
3448 	  case BUILT_IN_FPCLASSIFY:
3449 	    type_generic_remove_excess_precision = true;
3450 	    break;
3451 
3452 	  case BUILT_IN_ADD_OVERFLOW_P:
3453 	  case BUILT_IN_SUB_OVERFLOW_P:
3454 	  case BUILT_IN_MUL_OVERFLOW_P:
3455 	    /* The last argument of these type-generic builtins
3456 	       should not be promoted.  */
3457 	    type_generic_overflow_p = true;
3458 	    break;
3459 
3460 	  default:
3461 	    break;
3462 	  }
3463     }
3464 
3465   /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3466      individual converted arguments.  */
3467 
3468   tree typetail, builtin_typetail, val;
3469   for (typetail = typelist,
3470 	 builtin_typetail = builtin_typelist,
3471 	 parmnum = 0;
3472        values && values->iterate (parmnum, &val);
3473        ++parmnum)
3474     {
3475       /* The type of the function parameter (if it was declared with one).  */
3476       tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3477       /* The type of the built-in function parameter (if the function
3478 	 is a built-in).  Used to detect type incompatibilities in
3479 	 calls to built-ins declared without a prototype.  */
3480       tree builtin_type = (builtin_typetail
3481 			   ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3482       /* The original type of the argument being passed to the function.  */
3483       tree valtype = TREE_TYPE (val);
3484       /* The called function (or function selector in Objective C).  */
3485       tree rname = function;
3486       int argnum = parmnum + 1;
3487       const char *invalid_func_diag;
3488       /* Set for EXCESS_PRECISION_EXPR arguments.  */
3489       bool excess_precision = false;
3490       /* The value of the argument after conversion to the type
3491 	 of the function parameter it is passed to.  */
3492       tree parmval;
3493       /* Some __atomic_* builtins have additional hidden argument at
3494 	 position 0.  */
3495       location_t ploc
3496 	= !arg_loc.is_empty () && values->length () == arg_loc.length ()
3497 	  ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3498 	  : input_location;
3499 
3500       if (type == void_type_node)
3501 	{
3502 	  if (selector)
3503 	    error_at (loc, "too many arguments to method %qE", selector);
3504 	  else
3505 	    error_at (loc, "too many arguments to function %qE", function);
3506 	  inform_declaration (fundecl);
3507 	  return error_args ? -1 : (int) parmnum;
3508 	}
3509 
3510       if (builtin_type == void_type_node)
3511 	{
3512 	  if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3513 			  "too many arguments to built-in function %qE "
3514 			  "expecting %d", function, parmnum))
3515 	    inform_declaration (fundecl);
3516 	  builtin_typetail = NULL_TREE;
3517 	}
3518 
3519       if (selector && argnum > 2)
3520 	{
3521 	  rname = selector;
3522 	  argnum -= 2;
3523 	}
3524 
3525       /* Determine if VAL is a null pointer constant before folding it.  */
3526       bool npc = null_pointer_constant_p (val);
3527 
3528       /* If there is excess precision and a prototype, convert once to
3529 	 the required type rather than converting via the semantic
3530 	 type.  Likewise without a prototype a float value represented
3531 	 as long double should be converted once to double.  But for
3532 	 type-generic classification functions excess precision must
3533 	 be removed here.  */
3534       if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3535 	  && (type || !type_generic || !type_generic_remove_excess_precision))
3536 	{
3537 	  val = TREE_OPERAND (val, 0);
3538 	  excess_precision = true;
3539 	}
3540       val = c_fully_fold (val, false, NULL);
3541       STRIP_TYPE_NOPS (val);
3542 
3543       val = require_complete_type (ploc, val);
3544 
3545       /* Some floating-point arguments must be promoted to double when
3546 	 no type is specified by a prototype.  This applies to
3547 	 arguments of type float, and to architecture-specific types
3548 	 (ARM __fp16), but not to _FloatN or _FloatNx types.  */
3549       bool promote_float_arg = false;
3550       if (type == NULL_TREE
3551 	  && TREE_CODE (valtype) == REAL_TYPE
3552 	  && (TYPE_PRECISION (valtype)
3553 	      <= TYPE_PRECISION (double_type_node))
3554 	  && TYPE_MAIN_VARIANT (valtype) != double_type_node
3555 	  && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3556 	  && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3557 	{
3558 	  /* Promote this argument, unless it has a _FloatN or
3559 	     _FloatNx type.  */
3560 	  promote_float_arg = true;
3561 	  for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3562 	    if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3563 	      {
3564 		promote_float_arg = false;
3565 		break;
3566 	      }
3567 	}
3568 
3569       if (type != NULL_TREE)
3570 	{
3571 	  tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3572 	  parmval = convert_argument (ploc, function, fundecl, type, origtype,
3573 				      val, valtype, npc, rname, parmnum, argnum,
3574 				      excess_precision, 0);
3575 	}
3576       else if (promote_float_arg)
3577         {
3578 	  if (type_generic)
3579 	    parmval = val;
3580 	  else
3581 	    {
3582 	      /* Convert `float' to `double'.  */
3583 	      if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3584 		warning_at (ploc, OPT_Wdouble_promotion,
3585 			    "implicit conversion from %qT to %qT when passing "
3586 			    "argument to function",
3587 			    valtype, double_type_node);
3588 	      parmval = convert (double_type_node, val);
3589 	    }
3590 	}
3591       else if ((excess_precision && !type_generic)
3592 	       || (type_generic_overflow_p && parmnum == 2))
3593 	/* A "double" argument with excess precision being passed
3594 	   without a prototype or in variable arguments.
3595 	   The last argument of __builtin_*_overflow_p should not be
3596 	   promoted.  */
3597 	parmval = convert (valtype, val);
3598       else if ((invalid_func_diag =
3599 		targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3600 	{
3601 	  error (invalid_func_diag);
3602 	  return -1;
3603 	}
3604       else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3605 	{
3606 	  return -1;
3607 	}
3608       else
3609 	/* Convert `short' and `char' to full-size `int'.  */
3610 	parmval = default_conversion (val);
3611 
3612       (*values)[parmnum] = parmval;
3613       if (parmval == error_mark_node)
3614 	error_args = true;
3615 
3616       if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3617 	{
3618 	  /* For a call to a built-in function declared without a prototype,
3619 	     perform the conversions from the argument to the expected type
3620 	     but issue warnings rather than errors for any mismatches.
3621 	     Ignore the converted argument and use the PARMVAL obtained
3622 	     above by applying default conversions instead.  */
3623 	  tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3624 	  convert_argument (ploc, function, fundecl, builtin_type, origtype,
3625 			    val, valtype, npc, rname, parmnum, argnum,
3626 			    excess_precision,
3627 			    OPT_Wbuiltin_declaration_mismatch);
3628 	}
3629 
3630       if (typetail)
3631 	typetail = TREE_CHAIN (typetail);
3632 
3633       if (builtin_typetail)
3634 	builtin_typetail = TREE_CHAIN (builtin_typetail);
3635     }
3636 
3637   gcc_assert (parmnum == vec_safe_length (values));
3638 
3639   if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3640     {
3641       error_at (loc, "too few arguments to function %qE", function);
3642       inform_declaration (fundecl);
3643       return -1;
3644     }
3645 
3646   if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3647     {
3648       unsigned nargs = parmnum;
3649       for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3650 	++nargs;
3651 
3652       if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3653 		      "too few arguments to built-in function %qE "
3654 		      "expecting %u", function, nargs - 1))
3655 	inform_declaration (fundecl);
3656     }
3657 
3658   return error_args ? -1 : (int) parmnum;
3659 }
3660 
3661 /* This is the entry point used by the parser to build unary operators
3662    in the input.  CODE, a tree_code, specifies the unary operator, and
3663    ARG is the operand.  For unary plus, the C parser currently uses
3664    CONVERT_EXPR for code.
3665 
3666    LOC is the location to use for the tree generated.
3667 */
3668 
3669 struct c_expr
parser_build_unary_op(location_t loc,enum tree_code code,struct c_expr arg)3670 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3671 {
3672   struct c_expr result;
3673 
3674   result.original_code = code;
3675   result.original_type = NULL;
3676 
3677   if (reject_gcc_builtin (arg.value))
3678     {
3679       result.value = error_mark_node;
3680     }
3681   else
3682     {
3683       result.value = build_unary_op (loc, code, arg.value, false);
3684 
3685       if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3686 	overflow_warning (loc, result.value, arg.value);
3687     }
3688 
3689   /* We are typically called when parsing a prefix token at LOC acting on
3690      ARG.  Reflect this by updating the source range of the result to
3691      start at LOC and end at the end of ARG.  */
3692   set_c_expr_source_range (&result,
3693 			   loc, arg.get_finish ());
3694 
3695   return result;
3696 }
3697 
3698 /* Returns true if TYPE is a character type, *not* including wchar_t.  */
3699 
3700 static bool
char_type_p(tree type)3701 char_type_p (tree type)
3702 {
3703   return (type == char_type_node
3704 	  || type == unsigned_char_type_node
3705 	  || type == signed_char_type_node
3706 	  || type == char16_type_node
3707 	  || type == char32_type_node);
3708 }
3709 
3710 /* This is the entry point used by the parser to build binary operators
3711    in the input.  CODE, a tree_code, specifies the binary operator, and
3712    ARG1 and ARG2 are the operands.  In addition to constructing the
3713    expression, we check for operands that were written with other binary
3714    operators in a way that is likely to confuse the user.
3715 
3716    LOCATION is the location of the binary operator.  */
3717 
3718 struct c_expr
parser_build_binary_op(location_t location,enum tree_code code,struct c_expr arg1,struct c_expr arg2)3719 parser_build_binary_op (location_t location, enum tree_code code,
3720 			struct c_expr arg1, struct c_expr arg2)
3721 {
3722   struct c_expr result;
3723 
3724   enum tree_code code1 = arg1.original_code;
3725   enum tree_code code2 = arg2.original_code;
3726   tree type1 = (arg1.original_type
3727                 ? arg1.original_type
3728                 : TREE_TYPE (arg1.value));
3729   tree type2 = (arg2.original_type
3730                 ? arg2.original_type
3731                 : TREE_TYPE (arg2.value));
3732 
3733   result.value = build_binary_op (location, code,
3734 				  arg1.value, arg2.value, true);
3735   result.original_code = code;
3736   result.original_type = NULL;
3737 
3738   if (TREE_CODE (result.value) == ERROR_MARK)
3739     {
3740       set_c_expr_source_range (&result,
3741 			       arg1.get_start (),
3742 			       arg2.get_finish ());
3743       return result;
3744     }
3745 
3746   if (location != UNKNOWN_LOCATION)
3747     protected_set_expr_location (result.value, location);
3748 
3749   set_c_expr_source_range (&result,
3750 			   arg1.get_start (),
3751 			   arg2.get_finish ());
3752 
3753   /* Check for cases such as x+y<<z which users are likely
3754      to misinterpret.  */
3755   if (warn_parentheses)
3756     warn_about_parentheses (location, code, code1, arg1.value, code2,
3757 			    arg2.value);
3758 
3759   if (warn_logical_op)
3760     warn_logical_operator (location, code, TREE_TYPE (result.value),
3761 			   code1, arg1.value, code2, arg2.value);
3762 
3763   if (warn_tautological_compare)
3764     {
3765       tree lhs = arg1.value;
3766       tree rhs = arg2.value;
3767       if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3768 	{
3769 	  if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3770 	      && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3771 	    lhs = NULL_TREE;
3772 	  else
3773 	    lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3774 	}
3775       if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3776 	{
3777 	  if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3778 	      && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3779 	    rhs = NULL_TREE;
3780 	  else
3781 	    rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3782 	}
3783       if (lhs != NULL_TREE && rhs != NULL_TREE)
3784 	warn_tautological_cmp (location, code, lhs, rhs);
3785     }
3786 
3787   if (warn_logical_not_paren
3788       && TREE_CODE_CLASS (code) == tcc_comparison
3789       && code1 == TRUTH_NOT_EXPR
3790       && code2 != TRUTH_NOT_EXPR
3791       /* Avoid warning for !!x == y.  */
3792       && (TREE_CODE (arg1.value) != NE_EXPR
3793 	  || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3794     {
3795       /* Avoid warning for !b == y where b has _Bool type.  */
3796       tree t = integer_zero_node;
3797       if (TREE_CODE (arg1.value) == EQ_EXPR
3798 	  && integer_zerop (TREE_OPERAND (arg1.value, 1))
3799 	  && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3800 	{
3801 	  t = TREE_OPERAND (arg1.value, 0);
3802 	  do
3803 	    {
3804 	      if (TREE_TYPE (t) != integer_type_node)
3805 		break;
3806 	      if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3807 		t = C_MAYBE_CONST_EXPR_EXPR (t);
3808 	      else if (CONVERT_EXPR_P (t))
3809 		t = TREE_OPERAND (t, 0);
3810 	      else
3811 		break;
3812 	    }
3813 	  while (1);
3814 	}
3815       if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3816 	warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3817     }
3818 
3819   /* Warn about comparisons against string literals, with the exception
3820      of testing for equality or inequality of a string literal with NULL.  */
3821   if (code == EQ_EXPR || code == NE_EXPR)
3822     {
3823       if ((code1 == STRING_CST
3824 	   && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3825 	  || (code2 == STRING_CST
3826 	      && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3827 	warning_at (location, OPT_Waddress,
3828 		    "comparison with string literal results in unspecified behavior");
3829       /* Warn for ptr == '\0', it's likely that it should've been ptr[0].  */
3830       if (POINTER_TYPE_P (type1)
3831 	  && null_pointer_constant_p (arg2.value)
3832 	  && char_type_p (type2))
3833 	{
3834 	  auto_diagnostic_group d;
3835 	  if (warning_at (location, OPT_Wpointer_compare,
3836 			    "comparison between pointer and zero character "
3837 			    "constant"))
3838 	    inform (arg1.get_start (),
3839 		      "did you mean to dereference the pointer?");
3840 	}
3841       else if (POINTER_TYPE_P (type2)
3842 	       && null_pointer_constant_p (arg1.value)
3843 	       && char_type_p (type1))
3844 	{
3845 	  auto_diagnostic_group d;
3846 	  if (warning_at (location, OPT_Wpointer_compare,
3847 			    "comparison between pointer and zero character "
3848 			    "constant"))
3849 	    inform (arg2.get_start (),
3850 		      "did you mean to dereference the pointer?");
3851 	}
3852     }
3853   else if (TREE_CODE_CLASS (code) == tcc_comparison
3854 	   && (code1 == STRING_CST || code2 == STRING_CST))
3855     warning_at (location, OPT_Waddress,
3856 		"comparison with string literal results in unspecified behavior");
3857 
3858   if (TREE_OVERFLOW_P (result.value)
3859       && !TREE_OVERFLOW_P (arg1.value)
3860       && !TREE_OVERFLOW_P (arg2.value))
3861     overflow_warning (location, result.value);
3862 
3863   /* Warn about comparisons of different enum types.  */
3864   if (warn_enum_compare
3865       && TREE_CODE_CLASS (code) == tcc_comparison
3866       && TREE_CODE (type1) == ENUMERAL_TYPE
3867       && TREE_CODE (type2) == ENUMERAL_TYPE
3868       && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3869     warning_at (location, OPT_Wenum_compare,
3870 		"comparison between %qT and %qT",
3871 		type1, type2);
3872 
3873   return result;
3874 }
3875 
3876 /* Return a tree for the difference of pointers OP0 and OP1.
3877    The resulting tree has type ptrdiff_t.  If POINTER_SUBTRACT sanitization is
3878    enabled, assign to INSTRUMENT_EXPR call to libsanitizer.  */
3879 
3880 static tree
pointer_diff(location_t loc,tree op0,tree op1,tree * instrument_expr)3881 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3882 {
3883   tree restype = ptrdiff_type_node;
3884   tree result, inttype;
3885 
3886   addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3887   addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3888   tree target_type = TREE_TYPE (TREE_TYPE (op0));
3889   tree orig_op1 = op1;
3890 
3891   /* If the operands point into different address spaces, we need to
3892      explicitly convert them to pointers into the common address space
3893      before we can subtract the numerical address values.  */
3894   if (as0 != as1)
3895     {
3896       addr_space_t as_common;
3897       tree common_type;
3898 
3899       /* Determine the common superset address space.  This is guaranteed
3900 	 to exist because the caller verified that comp_target_types
3901 	 returned non-zero.  */
3902       if (!addr_space_superset (as0, as1, &as_common))
3903 	gcc_unreachable ();
3904 
3905       common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3906       op0 = convert (common_type, op0);
3907       op1 = convert (common_type, op1);
3908     }
3909 
3910   /* Determine integer type result of the subtraction.  This will usually
3911      be the same as the result type (ptrdiff_t), but may need to be a wider
3912      type if pointers for the address space are wider than ptrdiff_t.  */
3913   if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3914     inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3915   else
3916     inttype = restype;
3917 
3918   if (TREE_CODE (target_type) == VOID_TYPE)
3919     pedwarn (loc, OPT_Wpointer_arith,
3920 	     "pointer of type %<void *%> used in subtraction");
3921   if (TREE_CODE (target_type) == FUNCTION_TYPE)
3922     pedwarn (loc, OPT_Wpointer_arith,
3923 	     "pointer to a function used in subtraction");
3924 
3925   if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3926     {
3927       gcc_assert (current_function_decl != NULL_TREE);
3928 
3929       op0 = save_expr (op0);
3930       op1 = save_expr (op1);
3931 
3932       tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3933       *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3934     }
3935 
3936   /* First do the subtraction, then build the divide operator
3937      and only convert at the very end.
3938      Do not do default conversions in case restype is a short type.  */
3939 
3940   /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3941      pointers.  If some platform cannot provide that, or has a larger
3942      ptrdiff_type to support differences larger than half the address
3943      space, cast the pointers to some larger integer type and do the
3944      computations in that type.  */
3945   if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
3946     op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3947 			   convert (inttype, op1), false);
3948   else
3949     {
3950       /* Cast away qualifiers.  */
3951       op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
3952       op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
3953       op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3954     }
3955 
3956   /* This generates an error if op1 is pointer to incomplete type.  */
3957   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3958     error_at (loc, "arithmetic on pointer to an incomplete type");
3959 
3960   op1 = c_size_in_bytes (target_type);
3961 
3962   if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3963     error_at (loc, "arithmetic on pointer to an empty aggregate");
3964 
3965   /* Divide by the size, in easiest possible way.  */
3966   result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3967 			    op0, convert (inttype, op1));
3968 
3969   /* Convert to final result type if necessary.  */
3970   return convert (restype, result);
3971 }
3972 
3973 /* Expand atomic compound assignments into an appropriate sequence as
3974    specified by the C11 standard section 6.5.16.2.
3975 
3976        _Atomic T1 E1
3977        T2 E2
3978        E1 op= E2
3979 
3980   This sequence is used for all types for which these operations are
3981   supported.
3982 
3983   In addition, built-in versions of the 'fe' prefixed routines may
3984   need to be invoked for floating point (real, complex or vector) when
3985   floating-point exceptions are supported.  See 6.5.16.2 footnote 113.
3986 
3987   T1 newval;
3988   T1 old;
3989   T1 *addr
3990   T2 val
3991   fenv_t fenv
3992 
3993   addr = &E1;
3994   val = (E2);
3995   __atomic_load (addr, &old, SEQ_CST);
3996   feholdexcept (&fenv);
3997 loop:
3998     newval = old op val;
3999     if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4000 					  SEQ_CST))
4001       goto done;
4002     feclearexcept (FE_ALL_EXCEPT);
4003     goto loop:
4004 done:
4005   feupdateenv (&fenv);
4006 
4007   The compiler will issue the __atomic_fetch_* built-in when possible,
4008   otherwise it will generate the generic form of the atomic operations.
4009   This requires temp(s) and has their address taken.  The atomic processing
4010   is smart enough to figure out when the size of an object can utilize
4011   a lock-free version, and convert the built-in call to the appropriate
4012   lock-free routine.  The optimizers will then dispose of any temps that
4013   are no longer required, and lock-free implementations are utilized as
4014   long as there is target support for the required size.
4015 
4016   If the operator is NOP_EXPR, then this is a simple assignment, and
4017   an __atomic_store is issued to perform the assignment rather than
4018   the above loop.  */
4019 
4020 /* Build an atomic assignment at LOC, expanding into the proper
4021    sequence to store LHS MODIFYCODE= RHS.  Return a value representing
4022    the result of the operation, unless RETURN_OLD_P, in which case
4023    return the old value of LHS (this is only for postincrement and
4024    postdecrement).  */
4025 
4026 static tree
build_atomic_assign(location_t loc,tree lhs,enum tree_code modifycode,tree rhs,bool return_old_p)4027 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4028 		     tree rhs, bool return_old_p)
4029 {
4030   tree fndecl, func_call;
4031   vec<tree, va_gc> *params;
4032   tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4033   tree old, old_addr;
4034   tree compound_stmt;
4035   tree stmt, goto_stmt;
4036   tree loop_label, loop_decl, done_label, done_decl;
4037 
4038   tree lhs_type = TREE_TYPE (lhs);
4039   tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4040   tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4041   tree rhs_semantic_type = TREE_TYPE (rhs);
4042   tree nonatomic_rhs_semantic_type;
4043   tree rhs_type;
4044 
4045   gcc_assert (TYPE_ATOMIC (lhs_type));
4046 
4047   if (return_old_p)
4048     gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4049 
4050   /* Allocate enough vector items for a compare_exchange.  */
4051   vec_alloc (params, 6);
4052 
4053   /* Create a compound statement to hold the sequence of statements
4054      with a loop.  */
4055   compound_stmt = c_begin_compound_stmt (false);
4056 
4057   /* Remove any excess precision (which is only present here in the
4058      case of compound assignments).  */
4059   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4060     {
4061       gcc_assert (modifycode != NOP_EXPR);
4062       rhs = TREE_OPERAND (rhs, 0);
4063     }
4064   rhs_type = TREE_TYPE (rhs);
4065 
4066   /* Fold the RHS if it hasn't already been folded.  */
4067   if (modifycode != NOP_EXPR)
4068     rhs = c_fully_fold (rhs, false, NULL);
4069 
4070   /* Remove the qualifiers for the rest of the expressions and create
4071      the VAL temp variable to hold the RHS.  */
4072   nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4073   nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4074   nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4075 						      TYPE_UNQUALIFIED);
4076   val = create_tmp_var_raw (nonatomic_rhs_type);
4077   TREE_ADDRESSABLE (val) = 1;
4078   TREE_NO_WARNING (val) = 1;
4079   rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4080 		NULL_TREE);
4081   SET_EXPR_LOCATION (rhs, loc);
4082   add_stmt (rhs);
4083 
4084   /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4085      an atomic_store.  */
4086   if (modifycode == NOP_EXPR)
4087     {
4088       /* Build __atomic_store (&lhs, &val, SEQ_CST)  */
4089       rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4090       fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
4091       params->quick_push (lhs_addr);
4092       params->quick_push (rhs);
4093       params->quick_push (seq_cst);
4094       func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4095       add_stmt (func_call);
4096 
4097       /* Finish the compound statement.  */
4098       compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4099 
4100       /* VAL is the value which was stored, return a COMPOUND_STMT of
4101 	 the statement and that value.  */
4102       return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4103     }
4104 
4105   /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4106      __atomic_*_fetch built-in rather than a CAS loop.  atomic_bool type
4107      isn't applicable for such builtins.  ??? Do we want to handle enums?  */
4108   if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4109       && TREE_CODE (rhs_type) == INTEGER_TYPE)
4110     {
4111       built_in_function fncode;
4112       switch (modifycode)
4113 	{
4114 	case PLUS_EXPR:
4115 	case POINTER_PLUS_EXPR:
4116 	  fncode = (return_old_p
4117 		    ? BUILT_IN_ATOMIC_FETCH_ADD_N
4118 		    : BUILT_IN_ATOMIC_ADD_FETCH_N);
4119 	  break;
4120 	case MINUS_EXPR:
4121 	  fncode = (return_old_p
4122 		    ? BUILT_IN_ATOMIC_FETCH_SUB_N
4123 		    : BUILT_IN_ATOMIC_SUB_FETCH_N);
4124 	  break;
4125 	case BIT_AND_EXPR:
4126 	  fncode = (return_old_p
4127 		    ? BUILT_IN_ATOMIC_FETCH_AND_N
4128 		    : BUILT_IN_ATOMIC_AND_FETCH_N);
4129 	  break;
4130 	case BIT_IOR_EXPR:
4131 	  fncode = (return_old_p
4132 		    ? BUILT_IN_ATOMIC_FETCH_OR_N
4133 		    : BUILT_IN_ATOMIC_OR_FETCH_N);
4134 	  break;
4135 	case BIT_XOR_EXPR:
4136 	  fncode = (return_old_p
4137 		    ? BUILT_IN_ATOMIC_FETCH_XOR_N
4138 		    : BUILT_IN_ATOMIC_XOR_FETCH_N);
4139 	  break;
4140 	default:
4141 	  goto cas_loop;
4142 	}
4143 
4144       /* We can only use "_1" through "_16" variants of the atomic fetch
4145 	 built-ins.  */
4146       unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4147       if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4148 	goto cas_loop;
4149 
4150       /* If this is a pointer type, we need to multiply by the size of
4151 	 the pointer target type.  */
4152       if (POINTER_TYPE_P (lhs_type))
4153 	{
4154 	  if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4155 	      /* ??? This would introduce -Wdiscarded-qualifiers
4156 		 warning: __atomic_fetch_* expect volatile void *
4157 		 type as the first argument.  (Assignments between
4158 		 atomic and non-atomic objects are OK.) */
4159 	      || TYPE_RESTRICT (lhs_type))
4160 	    goto cas_loop;
4161 	  tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4162 	  rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4163 				 convert (ptrdiff_type_node, rhs),
4164 				 convert (ptrdiff_type_node, sz));
4165 	}
4166 
4167       /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4168 	 __atomic_*_fetch (&lhs, &val, SEQ_CST).  */
4169       fndecl = builtin_decl_explicit (fncode);
4170       params->quick_push (lhs_addr);
4171       params->quick_push (rhs);
4172       params->quick_push (seq_cst);
4173       func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4174 
4175       newval = create_tmp_var_raw (nonatomic_lhs_type);
4176       TREE_ADDRESSABLE (newval) = 1;
4177       TREE_NO_WARNING (newval) = 1;
4178       rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4179 		    NULL_TREE, NULL_TREE);
4180       SET_EXPR_LOCATION (rhs, loc);
4181       add_stmt (rhs);
4182 
4183       /* Finish the compound statement.  */
4184       compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4185 
4186       /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4187 	 the statement and that value.  */
4188       return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4189     }
4190 
4191 cas_loop:
4192   /* Create the variables and labels required for the op= form.  */
4193   old = create_tmp_var_raw (nonatomic_lhs_type);
4194   old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4195   TREE_ADDRESSABLE (old) = 1;
4196   TREE_NO_WARNING (old) = 1;
4197 
4198   newval = create_tmp_var_raw (nonatomic_lhs_type);
4199   newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4200   TREE_ADDRESSABLE (newval) = 1;
4201   TREE_NO_WARNING (newval) = 1;
4202 
4203   loop_decl = create_artificial_label (loc);
4204   loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4205 
4206   done_decl = create_artificial_label (loc);
4207   done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4208 
4209   /* __atomic_load (addr, &old, SEQ_CST).  */
4210   fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4211   params->quick_push (lhs_addr);
4212   params->quick_push (old_addr);
4213   params->quick_push (seq_cst);
4214   func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4215   old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4216 		NULL_TREE);
4217   add_stmt (old);
4218   params->truncate (0);
4219 
4220   /* Create the expressions for floating-point environment
4221      manipulation, if required.  */
4222   bool need_fenv = (flag_trapping_math
4223 		    && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4224   tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4225   if (need_fenv)
4226     targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4227 
4228   if (hold_call)
4229     add_stmt (hold_call);
4230 
4231   /* loop:  */
4232   add_stmt (loop_label);
4233 
4234   /* newval = old + val;  */
4235   if (rhs_type != rhs_semantic_type)
4236     val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4237   rhs = build_binary_op (loc, modifycode, old, val, true);
4238   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4239     {
4240       tree eptype = TREE_TYPE (rhs);
4241       rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4242       rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4243     }
4244   else
4245     rhs = c_fully_fold (rhs, false, NULL);
4246   rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4247 				rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4248 				NULL_TREE, 0);
4249   if (rhs != error_mark_node)
4250     {
4251       rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4252 		    NULL_TREE);
4253       SET_EXPR_LOCATION (rhs, loc);
4254       add_stmt (rhs);
4255     }
4256 
4257   /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4258        goto done;  */
4259   fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4260   params->quick_push (lhs_addr);
4261   params->quick_push (old_addr);
4262   params->quick_push (newval_addr);
4263   params->quick_push (integer_zero_node);
4264   params->quick_push (seq_cst);
4265   params->quick_push (seq_cst);
4266   func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4267 
4268   goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4269   SET_EXPR_LOCATION (goto_stmt, loc);
4270 
4271   stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4272   SET_EXPR_LOCATION (stmt, loc);
4273   add_stmt (stmt);
4274 
4275   if (clear_call)
4276     add_stmt (clear_call);
4277 
4278   /* goto loop;  */
4279   goto_stmt  = build1 (GOTO_EXPR, void_type_node, loop_decl);
4280   SET_EXPR_LOCATION (goto_stmt, loc);
4281   add_stmt (goto_stmt);
4282 
4283   /* done:  */
4284   add_stmt (done_label);
4285 
4286   if (update_call)
4287     add_stmt (update_call);
4288 
4289   /* Finish the compound statement.  */
4290   compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4291 
4292   /* NEWVAL is the value that was successfully stored, return a
4293      COMPOUND_EXPR of the statement and the appropriate value.  */
4294   return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4295 		 return_old_p ? old : newval);
4296 }
4297 
4298 /* Construct and perhaps optimize a tree representation
4299    for a unary operation.  CODE, a tree_code, specifies the operation
4300    and XARG is the operand.
4301    For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4302    promotions (such as from short to int).
4303    For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4304    non-lvalues; this is only used to handle conversion of non-lvalue arrays
4305    to pointers in C99.
4306 
4307    LOCATION is the location of the operator.  */
4308 
4309 tree
build_unary_op(location_t location,enum tree_code code,tree xarg,bool noconvert)4310 build_unary_op (location_t location, enum tree_code code, tree xarg,
4311 		bool noconvert)
4312 {
4313   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
4314   tree arg = xarg;
4315   tree argtype = NULL_TREE;
4316   enum tree_code typecode;
4317   tree val;
4318   tree ret = error_mark_node;
4319   tree eptype = NULL_TREE;
4320   const char *invalid_op_diag;
4321   bool int_operands;
4322 
4323   int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4324   if (int_operands)
4325     arg = remove_c_maybe_const_expr (arg);
4326 
4327   if (code != ADDR_EXPR)
4328     arg = require_complete_type (location, arg);
4329 
4330   typecode = TREE_CODE (TREE_TYPE (arg));
4331   if (typecode == ERROR_MARK)
4332     return error_mark_node;
4333   if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4334     typecode = INTEGER_TYPE;
4335 
4336   if ((invalid_op_diag
4337        = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4338     {
4339       error_at (location, invalid_op_diag);
4340       return error_mark_node;
4341     }
4342 
4343   if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4344     {
4345       eptype = TREE_TYPE (arg);
4346       arg = TREE_OPERAND (arg, 0);
4347     }
4348 
4349   switch (code)
4350     {
4351     case CONVERT_EXPR:
4352       /* This is used for unary plus, because a CONVERT_EXPR
4353 	 is enough to prevent anybody from looking inside for
4354 	 associativity, but won't generate any code.  */
4355       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4356 	    || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4357 	    || typecode == VECTOR_TYPE))
4358 	{
4359 	  error_at (location, "wrong type argument to unary plus");
4360 	  return error_mark_node;
4361 	}
4362       else if (!noconvert)
4363 	arg = default_conversion (arg);
4364       arg = non_lvalue_loc (location, arg);
4365       break;
4366 
4367     case NEGATE_EXPR:
4368       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4369 	    || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4370 	    || typecode == VECTOR_TYPE))
4371 	{
4372 	  error_at (location, "wrong type argument to unary minus");
4373 	  return error_mark_node;
4374 	}
4375       else if (!noconvert)
4376 	arg = default_conversion (arg);
4377       break;
4378 
4379     case BIT_NOT_EXPR:
4380       /* ~ works on integer types and non float vectors. */
4381       if (typecode == INTEGER_TYPE
4382 	  || (typecode == VECTOR_TYPE
4383 	      && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4384 	{
4385 	  tree e = arg;
4386 
4387 	  /* Warn if the expression has boolean value.  */
4388 	  while (TREE_CODE (e) == COMPOUND_EXPR)
4389 	    e = TREE_OPERAND (e, 1);
4390 
4391 	  if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4392 	       || truth_value_p (TREE_CODE (e))))
4393 	    {
4394 	      auto_diagnostic_group d;
4395 	      if (warning_at (location, OPT_Wbool_operation,
4396 				"%<~%> on a boolean expression"))
4397 		{
4398 		  gcc_rich_location richloc (location);
4399 		  richloc.add_fixit_insert_before (location, "!");
4400 		  inform (&richloc, "did you mean to use logical not?");
4401 		}
4402 	    }
4403 	  if (!noconvert)
4404 	    arg = default_conversion (arg);
4405 	}
4406       else if (typecode == COMPLEX_TYPE)
4407 	{
4408 	  code = CONJ_EXPR;
4409 	  pedwarn (location, OPT_Wpedantic,
4410 		   "ISO C does not support %<~%> for complex conjugation");
4411 	  if (!noconvert)
4412 	    arg = default_conversion (arg);
4413 	}
4414       else
4415 	{
4416 	  error_at (location, "wrong type argument to bit-complement");
4417 	  return error_mark_node;
4418 	}
4419       break;
4420 
4421     case ABS_EXPR:
4422       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4423 	{
4424 	  error_at (location, "wrong type argument to abs");
4425 	  return error_mark_node;
4426 	}
4427       else if (!noconvert)
4428 	arg = default_conversion (arg);
4429       break;
4430 
4431     case ABSU_EXPR:
4432       if (!(typecode == INTEGER_TYPE))
4433 	{
4434 	  error_at (location, "wrong type argument to absu");
4435 	  return error_mark_node;
4436 	}
4437       else if (!noconvert)
4438 	arg = default_conversion (arg);
4439       break;
4440 
4441     case CONJ_EXPR:
4442       /* Conjugating a real value is a no-op, but allow it anyway.  */
4443       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4444 	    || typecode == COMPLEX_TYPE))
4445 	{
4446 	  error_at (location, "wrong type argument to conjugation");
4447 	  return error_mark_node;
4448 	}
4449       else if (!noconvert)
4450 	arg = default_conversion (arg);
4451       break;
4452 
4453     case TRUTH_NOT_EXPR:
4454       if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4455 	  && typecode != REAL_TYPE && typecode != POINTER_TYPE
4456 	  && typecode != COMPLEX_TYPE)
4457 	{
4458 	  error_at (location,
4459 		    "wrong type argument to unary exclamation mark");
4460 	  return error_mark_node;
4461 	}
4462       if (int_operands)
4463 	{
4464 	  arg = c_objc_common_truthvalue_conversion (location, xarg);
4465 	  arg = remove_c_maybe_const_expr (arg);
4466 	}
4467       else
4468 	arg = c_objc_common_truthvalue_conversion (location, arg);
4469       ret = invert_truthvalue_loc (location, arg);
4470       /* If the TRUTH_NOT_EXPR has been folded, reset the location.  */
4471       if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4472 	location = EXPR_LOCATION (ret);
4473       goto return_build_unary_op;
4474 
4475     case REALPART_EXPR:
4476     case IMAGPART_EXPR:
4477       ret = build_real_imag_expr (location, code, arg);
4478       if (ret == error_mark_node)
4479 	return error_mark_node;
4480       if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4481 	eptype = TREE_TYPE (eptype);
4482       goto return_build_unary_op;
4483 
4484     case PREINCREMENT_EXPR:
4485     case POSTINCREMENT_EXPR:
4486     case PREDECREMENT_EXPR:
4487     case POSTDECREMENT_EXPR:
4488 
4489       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4490 	{
4491 	  tree inner = build_unary_op (location, code,
4492 				       C_MAYBE_CONST_EXPR_EXPR (arg),
4493 				       noconvert);
4494 	  if (inner == error_mark_node)
4495 	    return error_mark_node;
4496 	  ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4497 			C_MAYBE_CONST_EXPR_PRE (arg), inner);
4498 	  gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4499 	  C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4500 	  goto return_build_unary_op;
4501 	}
4502 
4503       /* Complain about anything that is not a true lvalue.  In
4504 	 Objective-C, skip this check for property_refs.  */
4505       if (!objc_is_property_ref (arg)
4506 	  && !lvalue_or_else (location,
4507 			      arg, ((code == PREINCREMENT_EXPR
4508 				     || code == POSTINCREMENT_EXPR)
4509 				    ? lv_increment
4510 				    : lv_decrement)))
4511 	return error_mark_node;
4512 
4513       if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4514 	{
4515 	  if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4516 	    warning_at (location, OPT_Wc___compat,
4517 			"increment of enumeration value is invalid in C++");
4518 	  else
4519 	    warning_at (location, OPT_Wc___compat,
4520 			"decrement of enumeration value is invalid in C++");
4521 	}
4522 
4523       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4524 	{
4525 	  if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4526 	    warning_at (location, OPT_Wbool_operation,
4527 			"increment of a boolean expression");
4528 	  else
4529 	    warning_at (location, OPT_Wbool_operation,
4530 			"decrement of a boolean expression");
4531 	}
4532 
4533       /* Ensure the argument is fully folded inside any SAVE_EXPR.  */
4534       arg = c_fully_fold (arg, false, NULL, true);
4535 
4536       bool atomic_op;
4537       atomic_op = really_atomic_lvalue (arg);
4538 
4539       /* Increment or decrement the real part of the value,
4540 	 and don't change the imaginary part.  */
4541       if (typecode == COMPLEX_TYPE)
4542 	{
4543 	  tree real, imag;
4544 
4545 	  pedwarn (location, OPT_Wpedantic,
4546 		   "ISO C does not support %<++%> and %<--%> on complex types");
4547 
4548 	  if (!atomic_op)
4549 	    {
4550 	      arg = stabilize_reference (arg);
4551 	      real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4552 				     true);
4553 	      imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4554 				     true);
4555 	      real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4556 	      if (real == error_mark_node || imag == error_mark_node)
4557 		return error_mark_node;
4558 	      ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4559 			    real, imag);
4560 	      goto return_build_unary_op;
4561 	    }
4562 	}
4563 
4564       /* Report invalid types.  */
4565 
4566       if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4567 	  && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4568 	  && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
4569 	{
4570 	  if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4571 	    error_at (location, "wrong type argument to increment");
4572 	  else
4573 	    error_at (location, "wrong type argument to decrement");
4574 
4575 	  return error_mark_node;
4576 	}
4577 
4578       {
4579 	tree inc;
4580 
4581 	argtype = TREE_TYPE (arg);
4582 
4583 	/* Compute the increment.  */
4584 
4585 	if (typecode == POINTER_TYPE)
4586 	  {
4587 	    /* If pointer target is an incomplete type,
4588 	       we just cannot know how to do the arithmetic.  */
4589 	    if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4590 	      {
4591 		if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4592 		  error_at (location,
4593 			    "increment of pointer to an incomplete type %qT",
4594 			    TREE_TYPE (argtype));
4595 		else
4596 		  error_at (location,
4597 			    "decrement of pointer to an incomplete type %qT",
4598 			    TREE_TYPE (argtype));
4599 	      }
4600 	    else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4601 		     || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4602 	      {
4603 		if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4604 		  pedwarn (location, OPT_Wpointer_arith,
4605 			   "wrong type argument to increment");
4606 		else
4607 		  pedwarn (location, OPT_Wpointer_arith,
4608 			   "wrong type argument to decrement");
4609 	      }
4610 
4611 	    inc = c_size_in_bytes (TREE_TYPE (argtype));
4612 	    inc = convert_to_ptrofftype_loc (location, inc);
4613 	  }
4614 	else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4615 	  {
4616 	    /* For signed fract types, we invert ++ to -- or
4617 	       -- to ++, and change inc from 1 to -1, because
4618 	       it is not possible to represent 1 in signed fract constants.
4619 	       For unsigned fract types, the result always overflows and
4620 	       we get an undefined (original) or the maximum value.  */
4621 	    if (code == PREINCREMENT_EXPR)
4622 	      code = PREDECREMENT_EXPR;
4623 	    else if (code == PREDECREMENT_EXPR)
4624 	      code = PREINCREMENT_EXPR;
4625 	    else if (code == POSTINCREMENT_EXPR)
4626 	      code = POSTDECREMENT_EXPR;
4627 	    else /* code == POSTDECREMENT_EXPR  */
4628 	      code = POSTINCREMENT_EXPR;
4629 
4630 	    inc = integer_minus_one_node;
4631 	    inc = convert (argtype, inc);
4632 	  }
4633 	else
4634 	  {
4635 	    inc = VECTOR_TYPE_P (argtype)
4636 	      ? build_one_cst (argtype)
4637 	      : integer_one_node;
4638 	    inc = convert (argtype, inc);
4639 	  }
4640 
4641 	/* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4642 	   need to ask Objective-C to build the increment or decrement
4643 	   expression for it.  */
4644 	if (objc_is_property_ref (arg))
4645 	  return objc_build_incr_expr_for_property_ref (location, code,
4646 							arg, inc);
4647 
4648 	/* Report a read-only lvalue.  */
4649 	if (TYPE_READONLY (argtype))
4650 	  {
4651 	    readonly_error (location, arg,
4652 			    ((code == PREINCREMENT_EXPR
4653 			      || code == POSTINCREMENT_EXPR)
4654 			     ? lv_increment : lv_decrement));
4655 	    return error_mark_node;
4656 	  }
4657 	else if (TREE_READONLY (arg))
4658 	  readonly_warning (arg,
4659 			    ((code == PREINCREMENT_EXPR
4660 			      || code == POSTINCREMENT_EXPR)
4661 			     ? lv_increment : lv_decrement));
4662 
4663 	/* If the argument is atomic, use the special code sequences for
4664 	   atomic compound assignment.  */
4665 	if (atomic_op)
4666 	  {
4667 	    arg = stabilize_reference (arg);
4668 	    ret = build_atomic_assign (location, arg,
4669 				       ((code == PREINCREMENT_EXPR
4670 					 || code == POSTINCREMENT_EXPR)
4671 					? PLUS_EXPR
4672 					: MINUS_EXPR),
4673 				       (FRACT_MODE_P (TYPE_MODE (argtype))
4674 					? inc
4675 					: integer_one_node),
4676 				       (code == POSTINCREMENT_EXPR
4677 					|| code == POSTDECREMENT_EXPR));
4678 	    goto return_build_unary_op;
4679 	  }
4680 
4681 	if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4682 	  val = boolean_increment (code, arg);
4683 	else
4684 	  val = build2 (code, TREE_TYPE (arg), arg, inc);
4685 	TREE_SIDE_EFFECTS (val) = 1;
4686 	if (TREE_CODE (val) != code)
4687 	  TREE_NO_WARNING (val) = 1;
4688 	ret = val;
4689 	goto return_build_unary_op;
4690       }
4691 
4692     case ADDR_EXPR:
4693       /* Note that this operation never does default_conversion.  */
4694 
4695       /* The operand of unary '&' must be an lvalue (which excludes
4696 	 expressions of type void), or, in C99, the result of a [] or
4697 	 unary '*' operator.  */
4698       if (VOID_TYPE_P (TREE_TYPE (arg))
4699 	  && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4700 	  && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4701 	pedwarn (location, 0, "taking address of expression of type %<void%>");
4702 
4703       /* Let &* cancel out to simplify resulting code.  */
4704       if (INDIRECT_REF_P (arg))
4705 	{
4706 	  /* Don't let this be an lvalue.  */
4707 	  if (lvalue_p (TREE_OPERAND (arg, 0)))
4708 	    return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4709 	  ret = TREE_OPERAND (arg, 0);
4710 	  goto return_build_unary_op;
4711 	}
4712 
4713       /* Anything not already handled and not a true memory reference
4714 	 or a non-lvalue array is an error.  */
4715       if (typecode != FUNCTION_TYPE && !noconvert
4716 	  && !lvalue_or_else (location, arg, lv_addressof))
4717 	return error_mark_node;
4718 
4719       /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4720 	 folding later.  */
4721       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4722 	{
4723 	  tree inner = build_unary_op (location, code,
4724 				       C_MAYBE_CONST_EXPR_EXPR (arg),
4725 				       noconvert);
4726 	  ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4727 			C_MAYBE_CONST_EXPR_PRE (arg), inner);
4728 	  gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4729 	  C_MAYBE_CONST_EXPR_NON_CONST (ret)
4730 	    = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4731 	  goto return_build_unary_op;
4732 	}
4733 
4734       /* Ordinary case; arg is a COMPONENT_REF or a decl.  */
4735       argtype = TREE_TYPE (arg);
4736 
4737       /* If the lvalue is const or volatile, merge that into the type
4738 	 to which the address will point.  This is only needed
4739 	 for function types.  */
4740       if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4741 	  && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4742 	  && TREE_CODE (argtype) == FUNCTION_TYPE)
4743 	{
4744 	  int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4745 	  int quals = orig_quals;
4746 
4747 	  if (TREE_READONLY (arg))
4748 	    quals |= TYPE_QUAL_CONST;
4749 	  if (TREE_THIS_VOLATILE (arg))
4750 	    quals |= TYPE_QUAL_VOLATILE;
4751 
4752 	  argtype = c_build_qualified_type (argtype, quals);
4753 	}
4754 
4755       switch (TREE_CODE (arg))
4756 	{
4757 	case COMPONENT_REF:
4758 	  if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4759 	    {
4760 	      error_at (location, "cannot take address of bit-field %qD",
4761 			TREE_OPERAND (arg, 1));
4762 	      return error_mark_node;
4763 	    }
4764 
4765 	  /* fall through */
4766 
4767 	case ARRAY_REF:
4768 	  if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4769 	    {
4770 	      if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4771 		  && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4772 		{
4773 		  error_at (location, "cannot take address of scalar with "
4774 			    "reverse storage order");
4775 		  return error_mark_node;
4776 		}
4777 
4778 	      if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4779 		  && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4780 		warning_at (location, OPT_Wscalar_storage_order,
4781 			    "address of array with reverse scalar storage "
4782 			    "order requested");
4783 	    }
4784 
4785 	default:
4786 	  break;
4787 	}
4788 
4789       if (!c_mark_addressable (arg))
4790 	return error_mark_node;
4791 
4792       gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4793 		  || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4794 
4795       argtype = build_pointer_type (argtype);
4796 
4797       /* ??? Cope with user tricks that amount to offsetof.  Delete this
4798 	 when we have proper support for integer constant expressions.  */
4799       val = get_base_address (arg);
4800       if (val && INDIRECT_REF_P (val)
4801           && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4802 	{
4803 	  ret = fold_offsetof (arg, argtype);
4804 	  goto return_build_unary_op;
4805 	}
4806 
4807       val = build1 (ADDR_EXPR, argtype, arg);
4808 
4809       ret = val;
4810       goto return_build_unary_op;
4811 
4812     default:
4813       gcc_unreachable ();
4814     }
4815 
4816   if (argtype == NULL_TREE)
4817     argtype = TREE_TYPE (arg);
4818   if (TREE_CODE (arg) == INTEGER_CST)
4819     ret = (require_constant_value
4820 	   ? fold_build1_initializer_loc (location, code, argtype, arg)
4821 	   : fold_build1_loc (location, code, argtype, arg));
4822   else
4823     ret = build1 (code, argtype, arg);
4824  return_build_unary_op:
4825   gcc_assert (ret != error_mark_node);
4826   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4827       && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4828     ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4829   else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4830     ret = note_integer_operands (ret);
4831   if (eptype)
4832     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4833   protected_set_expr_location (ret, location);
4834   return ret;
4835 }
4836 
4837 /* Return nonzero if REF is an lvalue valid for this language.
4838    Lvalues can be assigned, unless their type has TYPE_READONLY.
4839    Lvalues can have their address taken, unless they have C_DECL_REGISTER.  */
4840 
4841 bool
lvalue_p(const_tree ref)4842 lvalue_p (const_tree ref)
4843 {
4844   const enum tree_code code = TREE_CODE (ref);
4845 
4846   switch (code)
4847     {
4848     case REALPART_EXPR:
4849     case IMAGPART_EXPR:
4850     case COMPONENT_REF:
4851       return lvalue_p (TREE_OPERAND (ref, 0));
4852 
4853     case C_MAYBE_CONST_EXPR:
4854       return lvalue_p (TREE_OPERAND (ref, 1));
4855 
4856     case COMPOUND_LITERAL_EXPR:
4857     case STRING_CST:
4858       return true;
4859 
4860     case INDIRECT_REF:
4861     case ARRAY_REF:
4862     case VAR_DECL:
4863     case PARM_DECL:
4864     case RESULT_DECL:
4865     case ERROR_MARK:
4866       return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4867 	      && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4868 
4869     case BIND_EXPR:
4870       return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4871 
4872     default:
4873       return false;
4874     }
4875 }
4876 
4877 /* Give a warning for storing in something that is read-only in GCC
4878    terms but not const in ISO C terms.  */
4879 
4880 static void
readonly_warning(tree arg,enum lvalue_use use)4881 readonly_warning (tree arg, enum lvalue_use use)
4882 {
4883   switch (use)
4884     {
4885     case lv_assign:
4886       warning (0, "assignment of read-only location %qE", arg);
4887       break;
4888     case lv_increment:
4889       warning (0, "increment of read-only location %qE", arg);
4890       break;
4891     case lv_decrement:
4892       warning (0, "decrement of read-only location %qE", arg);
4893       break;
4894     default:
4895       gcc_unreachable ();
4896     }
4897   return;
4898 }
4899 
4900 
4901 /* Return nonzero if REF is an lvalue valid for this language;
4902    otherwise, print an error message and return zero.  USE says
4903    how the lvalue is being used and so selects the error message.
4904    LOCATION is the location at which any error should be reported.  */
4905 
4906 static int
lvalue_or_else(location_t loc,const_tree ref,enum lvalue_use use)4907 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4908 {
4909   int win = lvalue_p (ref);
4910 
4911   if (!win)
4912     lvalue_error (loc, use);
4913 
4914   return win;
4915 }
4916 
4917 /* Mark EXP saying that we need to be able to take the
4918    address of it; it should not be allocated in a register.
4919    Returns true if successful.  ARRAY_REF_P is true if this
4920    is for ARRAY_REF construction - in that case we don't want
4921    to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4922    it is fine to use ARRAY_REFs for vector subscripts on vector
4923    register variables.  */
4924 
4925 bool
c_mark_addressable(tree exp,bool array_ref_p)4926 c_mark_addressable (tree exp, bool array_ref_p)
4927 {
4928   tree x = exp;
4929 
4930   while (1)
4931     switch (TREE_CODE (x))
4932       {
4933       case VIEW_CONVERT_EXPR:
4934 	if (array_ref_p
4935 	    && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4936 	    && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4937 	  return true;
4938 	/* FALLTHRU */
4939       case COMPONENT_REF:
4940       case ADDR_EXPR:
4941       case ARRAY_REF:
4942       case REALPART_EXPR:
4943       case IMAGPART_EXPR:
4944 	x = TREE_OPERAND (x, 0);
4945 	break;
4946 
4947       case COMPOUND_LITERAL_EXPR:
4948 	TREE_ADDRESSABLE (x) = 1;
4949 	TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
4950 	return true;
4951 
4952       case CONSTRUCTOR:
4953 	TREE_ADDRESSABLE (x) = 1;
4954 	return true;
4955 
4956       case VAR_DECL:
4957       case CONST_DECL:
4958       case PARM_DECL:
4959       case RESULT_DECL:
4960 	if (C_DECL_REGISTER (x)
4961 	    && DECL_NONLOCAL (x))
4962 	  {
4963 	    if (TREE_PUBLIC (x) || is_global_var (x))
4964 	      {
4965 		error
4966 		  ("global register variable %qD used in nested function", x);
4967 		return false;
4968 	      }
4969 	    pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4970 	  }
4971 	else if (C_DECL_REGISTER (x))
4972 	  {
4973 	    if (TREE_PUBLIC (x) || is_global_var (x))
4974 	      error ("address of global register variable %qD requested", x);
4975 	    else
4976 	      error ("address of register variable %qD requested", x);
4977 	    return false;
4978 	  }
4979 
4980 	/* FALLTHRU */
4981       case FUNCTION_DECL:
4982 	TREE_ADDRESSABLE (x) = 1;
4983 	/* FALLTHRU */
4984       default:
4985 	return true;
4986     }
4987 }
4988 
4989 /* Convert EXPR to TYPE, warning about conversion problems with
4990    constants.  SEMANTIC_TYPE is the type this conversion would use
4991    without excess precision. If SEMANTIC_TYPE is NULL, this function
4992    is equivalent to convert_and_check. This function is a wrapper that
4993    handles conversions that may be different than
4994    the usual ones because of excess precision.  */
4995 
4996 static tree
ep_convert_and_check(location_t loc,tree type,tree expr,tree semantic_type)4997 ep_convert_and_check (location_t loc, tree type, tree expr,
4998 		      tree semantic_type)
4999 {
5000   if (TREE_TYPE (expr) == type)
5001     return expr;
5002 
5003   /* For C11, integer conversions may have results with excess
5004      precision.  */
5005   if (flag_isoc11 || !semantic_type)
5006     return convert_and_check (loc, type, expr);
5007 
5008   if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5009       && TREE_TYPE (expr) != semantic_type)
5010     {
5011       /* For integers, we need to check the real conversion, not
5012 	 the conversion to the excess precision type.  */
5013       expr = convert_and_check (loc, semantic_type, expr);
5014     }
5015   /* Result type is the excess precision type, which should be
5016      large enough, so do not check.  */
5017   return convert (type, expr);
5018 }
5019 
5020 /* If EXPR refers to a built-in declared without a prototype returns
5021    the actual type of the built-in and, if non-null, set *BLTIN to
5022    a pointer to the built-in.  Otherwise return the type of EXPR
5023    and clear *BLTIN if non-null.  */
5024 
5025 static tree
5026 type_or_builtin_type (tree expr, tree *bltin = NULL)
5027 {
5028   tree dummy;
5029   if (!bltin)
5030     bltin = &dummy;
5031 
5032   *bltin = NULL_TREE;
5033 
5034   tree type = TREE_TYPE (expr);
5035   if (TREE_CODE (expr) != ADDR_EXPR)
5036     return type;
5037 
5038   tree oper = TREE_OPERAND (expr, 0);
5039   if (!DECL_P (oper)
5040       || TREE_CODE (oper) != FUNCTION_DECL
5041       || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5042     return type;
5043 
5044   built_in_function code = DECL_FUNCTION_CODE (oper);
5045   if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5046     return type;
5047 
5048   if ((*bltin = builtin_decl_implicit (code)))
5049     type = build_pointer_type (TREE_TYPE (*bltin));
5050 
5051   return type;
5052 }
5053 
5054 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  If
5055    IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5056    if folded to an integer constant then the unselected half may
5057    contain arbitrary operations not normally permitted in constant
5058    expressions.  Set the location of the expression to LOC.  */
5059 
5060 tree
build_conditional_expr(location_t colon_loc,tree ifexp,bool ifexp_bcp,tree op1,tree op1_original_type,location_t op1_loc,tree op2,tree op2_original_type,location_t op2_loc)5061 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5062 			tree op1, tree op1_original_type, location_t op1_loc,
5063 			tree op2, tree op2_original_type, location_t op2_loc)
5064 {
5065   tree type1;
5066   tree type2;
5067   enum tree_code code1;
5068   enum tree_code code2;
5069   tree result_type = NULL;
5070   tree semantic_result_type = NULL;
5071   tree orig_op1 = op1, orig_op2 = op2;
5072   bool int_const, op1_int_operands, op2_int_operands, int_operands;
5073   bool ifexp_int_operands;
5074   tree ret;
5075 
5076   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5077   if (op1_int_operands)
5078     op1 = remove_c_maybe_const_expr (op1);
5079   op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5080   if (op2_int_operands)
5081     op2 = remove_c_maybe_const_expr (op2);
5082   ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5083   if (ifexp_int_operands)
5084     ifexp = remove_c_maybe_const_expr (ifexp);
5085 
5086   /* Promote both alternatives.  */
5087 
5088   if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5089     op1 = default_conversion (op1);
5090   if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5091     op2 = default_conversion (op2);
5092 
5093   if (TREE_CODE (ifexp) == ERROR_MARK
5094       || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5095       || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5096     return error_mark_node;
5097 
5098   tree bltin1 = NULL_TREE;
5099   tree bltin2 = NULL_TREE;
5100   type1 = type_or_builtin_type (op1, &bltin1);
5101   code1 = TREE_CODE (type1);
5102   type2 = type_or_builtin_type (op2, &bltin2);
5103   code2 = TREE_CODE (type2);
5104 
5105   if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5106     return error_mark_node;
5107 
5108   if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5109     return error_mark_node;
5110 
5111   /* C90 does not permit non-lvalue arrays in conditional expressions.
5112      In C99 they will be pointers by now.  */
5113   if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5114     {
5115       error_at (colon_loc, "non-lvalue array in conditional expression");
5116       return error_mark_node;
5117     }
5118 
5119   if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5120        || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5121       && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5122 	  || code1 == COMPLEX_TYPE)
5123       && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5124 	  || code2 == COMPLEX_TYPE))
5125     {
5126       semantic_result_type = c_common_type (type1, type2);
5127       if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5128 	{
5129 	  op1 = TREE_OPERAND (op1, 0);
5130 	  type1 = TREE_TYPE (op1);
5131 	  gcc_assert (TREE_CODE (type1) == code1);
5132 	}
5133       if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5134 	{
5135 	  op2 = TREE_OPERAND (op2, 0);
5136 	  type2 = TREE_TYPE (op2);
5137 	  gcc_assert (TREE_CODE (type2) == code2);
5138 	}
5139     }
5140 
5141   if (warn_cxx_compat)
5142     {
5143       tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5144       tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5145 
5146       if (TREE_CODE (t1) == ENUMERAL_TYPE
5147 	  && TREE_CODE (t2) == ENUMERAL_TYPE
5148 	  && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5149 	warning_at (colon_loc, OPT_Wc___compat,
5150 		    ("different enum types in conditional is "
5151 		     "invalid in C++: %qT vs %qT"),
5152 		    t1, t2);
5153     }
5154 
5155   /* Quickly detect the usual case where op1 and op2 have the same type
5156      after promotion.  */
5157   if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5158     {
5159       if (type1 == type2)
5160 	result_type = type1;
5161       else
5162 	result_type = TYPE_MAIN_VARIANT (type1);
5163     }
5164   else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5165 	    || code1 == COMPLEX_TYPE)
5166 	   && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5167 	       || code2 == COMPLEX_TYPE))
5168     {
5169       /* In C11, a conditional expression between a floating-point
5170 	 type and an integer type should convert the integer type to
5171 	 the evaluation format of the floating-point type, with
5172 	 possible excess precision.  */
5173       tree eptype1 = type1;
5174       tree eptype2 = type2;
5175       if (flag_isoc11)
5176 	{
5177 	  tree eptype;
5178 	  if (ANY_INTEGRAL_TYPE_P (type1)
5179 	      && (eptype = excess_precision_type (type2)) != NULL_TREE)
5180 	    {
5181 	      eptype2 = eptype;
5182 	      if (!semantic_result_type)
5183 		semantic_result_type = c_common_type (type1, type2);
5184 	    }
5185 	  else if (ANY_INTEGRAL_TYPE_P (type2)
5186 		   && (eptype = excess_precision_type (type1)) != NULL_TREE)
5187 	    {
5188 	      eptype1 = eptype;
5189 	      if (!semantic_result_type)
5190 		semantic_result_type = c_common_type (type1, type2);
5191 	    }
5192 	}
5193       result_type = c_common_type (eptype1, eptype2);
5194       if (result_type == error_mark_node)
5195 	return error_mark_node;
5196       do_warn_double_promotion (result_type, type1, type2,
5197 				"implicit conversion from %qT to %qT to "
5198 				"match other result of conditional",
5199 				colon_loc);
5200 
5201       /* If -Wsign-compare, warn here if type1 and type2 have
5202 	 different signedness.  We'll promote the signed to unsigned
5203 	 and later code won't know it used to be different.
5204 	 Do this check on the original types, so that explicit casts
5205 	 will be considered, but default promotions won't.  */
5206       if (c_inhibit_evaluation_warnings == 0)
5207 	{
5208 	  int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5209 	  int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5210 
5211 	  if (unsigned_op1 ^ unsigned_op2)
5212 	    {
5213 	      bool ovf;
5214 
5215 	      /* Do not warn if the result type is signed, since the
5216 		 signed type will only be chosen if it can represent
5217 		 all the values of the unsigned type.  */
5218 	      if (!TYPE_UNSIGNED (result_type))
5219 		/* OK */;
5220 	      else
5221 		{
5222 		  bool op1_maybe_const = true;
5223 		  bool op2_maybe_const = true;
5224 
5225 		  /* Do not warn if the signed quantity is an
5226 		     unsuffixed integer literal (or some static
5227 		     constant expression involving such literals) and
5228 		     it is non-negative.  This warning requires the
5229 		     operands to be folded for best results, so do
5230 		     that folding in this case even without
5231 		     warn_sign_compare to avoid warning options
5232 		     possibly affecting code generation.  */
5233 		  c_inhibit_evaluation_warnings
5234 		    += (ifexp == truthvalue_false_node);
5235 		  op1 = c_fully_fold (op1, require_constant_value,
5236 				      &op1_maybe_const);
5237 		  c_inhibit_evaluation_warnings
5238 		    -= (ifexp == truthvalue_false_node);
5239 
5240 		  c_inhibit_evaluation_warnings
5241 		    += (ifexp == truthvalue_true_node);
5242 		  op2 = c_fully_fold (op2, require_constant_value,
5243 				      &op2_maybe_const);
5244 		  c_inhibit_evaluation_warnings
5245 		    -= (ifexp == truthvalue_true_node);
5246 
5247 		  if (warn_sign_compare)
5248 		    {
5249 		      if ((unsigned_op2
5250 			   && tree_expr_nonnegative_warnv_p (op1, &ovf))
5251 			  || (unsigned_op1
5252 			      && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5253 			/* OK */;
5254 		      else if (unsigned_op2)
5255 			warning_at (op1_loc, OPT_Wsign_compare,
5256 				    "operand of ?: changes signedness from "
5257 				    "%qT to %qT due to unsignedness of other "
5258 				    "operand", TREE_TYPE (orig_op1),
5259 				    TREE_TYPE (orig_op2));
5260 		      else
5261 			warning_at (op2_loc, OPT_Wsign_compare,
5262 				    "operand of ?: changes signedness from "
5263 				    "%qT to %qT due to unsignedness of other "
5264 				    "operand", TREE_TYPE (orig_op2),
5265 				    TREE_TYPE (orig_op1));
5266 		    }
5267 		  if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5268 		    op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5269 		  if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5270 		    op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5271 		}
5272 	    }
5273 	}
5274     }
5275   else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5276     {
5277       if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5278 	pedwarn (colon_loc, OPT_Wpedantic,
5279 		 "ISO C forbids conditional expr with only one void side");
5280       result_type = void_type_node;
5281     }
5282   else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5283     {
5284       addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5285       addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5286       addr_space_t as_common;
5287 
5288       if (comp_target_types (colon_loc, type1, type2))
5289 	result_type = common_pointer_type (type1, type2);
5290       else if (null_pointer_constant_p (orig_op1))
5291 	result_type = type2;
5292       else if (null_pointer_constant_p (orig_op2))
5293 	result_type = type1;
5294       else if (!addr_space_superset (as1, as2, &as_common))
5295 	{
5296 	  error_at (colon_loc, "pointers to disjoint address spaces "
5297 		    "used in conditional expression");
5298 	  return error_mark_node;
5299 	}
5300       else if (VOID_TYPE_P (TREE_TYPE (type1))
5301 	       && !TYPE_ATOMIC (TREE_TYPE (type1)))
5302 	{
5303 	  if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5304 	      && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5305 		  & ~TYPE_QUALS (TREE_TYPE (type1))))
5306 	    warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5307 			"pointer to array loses qualifier "
5308 			"in conditional expression");
5309 
5310 	  if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5311 	    pedwarn (colon_loc, OPT_Wpedantic,
5312 		     "ISO C forbids conditional expr between "
5313 		     "%<void *%> and function pointer");
5314 	  result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5315 							  TREE_TYPE (type2)));
5316 	}
5317       else if (VOID_TYPE_P (TREE_TYPE (type2))
5318 	       && !TYPE_ATOMIC (TREE_TYPE (type2)))
5319 	{
5320 	  if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5321 	      && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5322 		  & ~TYPE_QUALS (TREE_TYPE (type2))))
5323 	    warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5324 			"pointer to array loses qualifier "
5325 			"in conditional expression");
5326 
5327 	  if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5328 	    pedwarn (colon_loc, OPT_Wpedantic,
5329 		     "ISO C forbids conditional expr between "
5330 		     "%<void *%> and function pointer");
5331 	  result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5332 							  TREE_TYPE (type1)));
5333 	}
5334       /* Objective-C pointer comparisons are a bit more lenient.  */
5335       else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5336 	result_type = objc_common_type (type1, type2);
5337       else
5338 	{
5339 	  int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5340 	  if (bltin1 && bltin2)
5341 	    warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5342 			"pointer type mismatch between %qT and %qT "
5343 			"of %qD and %qD in conditional expression",
5344 			type1, type2, bltin1, bltin2);
5345 	  else
5346 	    pedwarn (colon_loc, 0,
5347 		     "pointer type mismatch in conditional expression");
5348 	  result_type = build_pointer_type
5349 			  (build_qualified_type (void_type_node, qual));
5350 	}
5351     }
5352   else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5353     {
5354       if (!null_pointer_constant_p (orig_op2))
5355 	pedwarn (colon_loc, 0,
5356 		 "pointer/integer type mismatch in conditional expression");
5357       else
5358 	{
5359 	  op2 = null_pointer_node;
5360 	}
5361       result_type = type1;
5362     }
5363   else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5364     {
5365       if (!null_pointer_constant_p (orig_op1))
5366 	pedwarn (colon_loc, 0,
5367 		 "pointer/integer type mismatch in conditional expression");
5368       else
5369 	{
5370 	  op1 = null_pointer_node;
5371 	}
5372       result_type = type2;
5373     }
5374 
5375   if (!result_type)
5376     {
5377       if (flag_cond_mismatch)
5378 	result_type = void_type_node;
5379       else
5380 	{
5381 	  error_at (colon_loc, "type mismatch in conditional expression");
5382 	  return error_mark_node;
5383 	}
5384     }
5385 
5386   /* Merge const and volatile flags of the incoming types.  */
5387   result_type
5388     = build_type_variant (result_type,
5389 			  TYPE_READONLY (type1) || TYPE_READONLY (type2),
5390 			  TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5391 
5392   op1 = ep_convert_and_check (colon_loc, result_type, op1,
5393 			      semantic_result_type);
5394   op2 = ep_convert_and_check (colon_loc, result_type, op2,
5395 			      semantic_result_type);
5396 
5397   if (ifexp_bcp && ifexp == truthvalue_true_node)
5398     {
5399       op2_int_operands = true;
5400       op1 = c_fully_fold (op1, require_constant_value, NULL);
5401     }
5402   if (ifexp_bcp && ifexp == truthvalue_false_node)
5403     {
5404       op1_int_operands = true;
5405       op2 = c_fully_fold (op2, require_constant_value, NULL);
5406     }
5407   int_const = int_operands = (ifexp_int_operands
5408 			      && op1_int_operands
5409 			      && op2_int_operands);
5410   if (int_operands)
5411     {
5412       int_const = ((ifexp == truthvalue_true_node
5413 		    && TREE_CODE (orig_op1) == INTEGER_CST
5414 		    && !TREE_OVERFLOW (orig_op1))
5415 		   || (ifexp == truthvalue_false_node
5416 		       && TREE_CODE (orig_op2) == INTEGER_CST
5417 		       && !TREE_OVERFLOW (orig_op2)));
5418     }
5419 
5420   /* Need to convert condition operand into a vector mask.  */
5421   if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5422     {
5423       tree vectype = TREE_TYPE (ifexp);
5424       tree elem_type = TREE_TYPE (vectype);
5425       tree zero = build_int_cst (elem_type, 0);
5426       tree zero_vec = build_vector_from_val (vectype, zero);
5427       tree cmp_type = build_same_sized_truth_vector_type (vectype);
5428       ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5429     }
5430 
5431   if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5432     ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5433   else
5434     {
5435       if (int_operands)
5436 	{
5437 	  /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5438 	     nested inside of the expression.  */
5439 	  op1 = c_fully_fold (op1, false, NULL);
5440 	  op2 = c_fully_fold (op2, false, NULL);
5441 	}
5442       ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5443       if (int_operands)
5444 	ret = note_integer_operands (ret);
5445     }
5446   if (semantic_result_type)
5447     ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5448 
5449   protected_set_expr_location (ret, colon_loc);
5450 
5451   /* If the OP1 and OP2 are the same and don't have side-effects,
5452      warn here, because the COND_EXPR will be turned into OP1.  */
5453   if (warn_duplicated_branches
5454       && TREE_CODE (ret) == COND_EXPR
5455       && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5456     warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5457 		"this condition has identical branches");
5458 
5459   return ret;
5460 }
5461 
5462 /* Return a compound expression that performs two expressions and
5463    returns the value of the second of them.
5464 
5465    LOC is the location of the COMPOUND_EXPR.  */
5466 
5467 tree
build_compound_expr(location_t loc,tree expr1,tree expr2)5468 build_compound_expr (location_t loc, tree expr1, tree expr2)
5469 {
5470   bool expr1_int_operands, expr2_int_operands;
5471   tree eptype = NULL_TREE;
5472   tree ret;
5473 
5474   expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5475   if (expr1_int_operands)
5476     expr1 = remove_c_maybe_const_expr (expr1);
5477   expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5478   if (expr2_int_operands)
5479     expr2 = remove_c_maybe_const_expr (expr2);
5480 
5481   if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5482     expr1 = TREE_OPERAND (expr1, 0);
5483   if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5484     {
5485       eptype = TREE_TYPE (expr2);
5486       expr2 = TREE_OPERAND (expr2, 0);
5487     }
5488 
5489   if (!TREE_SIDE_EFFECTS (expr1))
5490     {
5491       /* The left-hand operand of a comma expression is like an expression
5492 	 statement: with -Wunused, we should warn if it doesn't have
5493 	 any side-effects, unless it was explicitly cast to (void).  */
5494       if (warn_unused_value)
5495 	{
5496 	  if (VOID_TYPE_P (TREE_TYPE (expr1))
5497 	      && CONVERT_EXPR_P (expr1))
5498 	    ; /* (void) a, b */
5499 	  else if (VOID_TYPE_P (TREE_TYPE (expr1))
5500 		   && TREE_CODE (expr1) == COMPOUND_EXPR
5501 		   && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5502 	    ; /* (void) a, (void) b, c */
5503 	  else
5504 	    warning_at (loc, OPT_Wunused_value,
5505 			"left-hand operand of comma expression has no effect");
5506 	}
5507     }
5508   else if (TREE_CODE (expr1) == COMPOUND_EXPR
5509 	   && warn_unused_value)
5510     {
5511       tree r = expr1;
5512       location_t cloc = loc;
5513       while (TREE_CODE (r) == COMPOUND_EXPR)
5514         {
5515 	  if (EXPR_HAS_LOCATION (r))
5516 	    cloc = EXPR_LOCATION (r);
5517 	  r = TREE_OPERAND (r, 1);
5518 	}
5519       if (!TREE_SIDE_EFFECTS (r)
5520 	  && !VOID_TYPE_P (TREE_TYPE (r))
5521 	  && !CONVERT_EXPR_P (r))
5522 	warning_at (cloc, OPT_Wunused_value,
5523 	            "right-hand operand of comma expression has no effect");
5524     }
5525 
5526   /* With -Wunused, we should also warn if the left-hand operand does have
5527      side-effects, but computes a value which is not used.  For example, in
5528      `foo() + bar(), baz()' the result of the `+' operator is not used,
5529      so we should issue a warning.  */
5530   else if (warn_unused_value)
5531     warn_if_unused_value (expr1, loc);
5532 
5533   if (expr2 == error_mark_node)
5534     return error_mark_node;
5535 
5536   ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5537 
5538   if (flag_isoc99
5539       && expr1_int_operands
5540       && expr2_int_operands)
5541     ret = note_integer_operands (ret);
5542 
5543   if (eptype)
5544     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5545 
5546   protected_set_expr_location (ret, loc);
5547   return ret;
5548 }
5549 
5550 /* Issue -Wcast-qual warnings when appropriate.  TYPE is the type to
5551    which we are casting.  OTYPE is the type of the expression being
5552    cast.  Both TYPE and OTYPE are pointer types.  LOC is the location
5553    of the cast.  -Wcast-qual appeared on the command line.  Named
5554    address space qualifiers are not handled here, because they result
5555    in different warnings.  */
5556 
5557 static void
handle_warn_cast_qual(location_t loc,tree type,tree otype)5558 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5559 {
5560   tree in_type = type;
5561   tree in_otype = otype;
5562   int added = 0;
5563   int discarded = 0;
5564   bool is_const;
5565 
5566   /* Check that the qualifiers on IN_TYPE are a superset of the
5567      qualifiers of IN_OTYPE.  The outermost level of POINTER_TYPE
5568      nodes is uninteresting and we stop as soon as we hit a
5569      non-POINTER_TYPE node on either type.  */
5570   do
5571     {
5572       in_otype = TREE_TYPE (in_otype);
5573       in_type = TREE_TYPE (in_type);
5574 
5575       /* GNU C allows cv-qualified function types.  'const' means the
5576 	 function is very pure, 'volatile' means it can't return.  We
5577 	 need to warn when such qualifiers are added, not when they're
5578 	 taken away.  */
5579       if (TREE_CODE (in_otype) == FUNCTION_TYPE
5580 	  && TREE_CODE (in_type) == FUNCTION_TYPE)
5581 	added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5582 		  & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5583       else
5584 	discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5585 		      & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5586     }
5587   while (TREE_CODE (in_type) == POINTER_TYPE
5588 	 && TREE_CODE (in_otype) == POINTER_TYPE);
5589 
5590   if (added)
5591     warning_at (loc, OPT_Wcast_qual,
5592 		"cast adds %q#v qualifier to function type", added);
5593 
5594   if (discarded)
5595     /* There are qualifiers present in IN_OTYPE that are not present
5596        in IN_TYPE.  */
5597     warning_at (loc, OPT_Wcast_qual,
5598 		"cast discards %qv qualifier from pointer target type",
5599 		discarded);
5600 
5601   if (added || discarded)
5602     return;
5603 
5604   /* A cast from **T to const **T is unsafe, because it can cause a
5605      const value to be changed with no additional warning.  We only
5606      issue this warning if T is the same on both sides, and we only
5607      issue the warning if there are the same number of pointers on
5608      both sides, as otherwise the cast is clearly unsafe anyhow.  A
5609      cast is unsafe when a qualifier is added at one level and const
5610      is not present at all outer levels.
5611 
5612      To issue this warning, we check at each level whether the cast
5613      adds new qualifiers not already seen.  We don't need to special
5614      case function types, as they won't have the same
5615      TYPE_MAIN_VARIANT.  */
5616 
5617   if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5618     return;
5619   if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5620     return;
5621 
5622   in_type = type;
5623   in_otype = otype;
5624   is_const = TYPE_READONLY (TREE_TYPE (in_type));
5625   do
5626     {
5627       in_type = TREE_TYPE (in_type);
5628       in_otype = TREE_TYPE (in_otype);
5629       if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5630 	  && !is_const)
5631 	{
5632 	  warning_at (loc, OPT_Wcast_qual,
5633 		      "to be safe all intermediate pointers in cast from "
5634                       "%qT to %qT must be %<const%> qualified",
5635 		      otype, type);
5636 	  break;
5637 	}
5638       if (is_const)
5639 	is_const = TYPE_READONLY (in_type);
5640     }
5641   while (TREE_CODE (in_type) == POINTER_TYPE);
5642 }
5643 
5644 /* Heuristic check if two parameter types can be considered ABI-equivalent.  */
5645 
5646 static bool
c_safe_arg_type_equiv_p(tree t1,tree t2)5647 c_safe_arg_type_equiv_p (tree t1, tree t2)
5648 {
5649   t1 = TYPE_MAIN_VARIANT (t1);
5650   t2 = TYPE_MAIN_VARIANT (t2);
5651 
5652   if (TREE_CODE (t1) == POINTER_TYPE
5653       && TREE_CODE (t2) == POINTER_TYPE)
5654     return true;
5655 
5656   /* The signedness of the parameter matters only when an integral
5657      type smaller than int is promoted to int, otherwise only the
5658      precision of the parameter matters.
5659      This check should make sure that the callee does not see
5660      undefined values in argument registers.  */
5661   if (INTEGRAL_TYPE_P (t1)
5662       && INTEGRAL_TYPE_P (t2)
5663       && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5664       && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5665 	  || !targetm.calls.promote_prototypes (NULL_TREE)
5666 	  || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5667     return true;
5668 
5669   return comptypes (t1, t2);
5670 }
5671 
5672 /* Check if a type cast between two function types can be considered safe.  */
5673 
5674 static bool
c_safe_function_type_cast_p(tree t1,tree t2)5675 c_safe_function_type_cast_p (tree t1, tree t2)
5676 {
5677   if (TREE_TYPE (t1) == void_type_node &&
5678       TYPE_ARG_TYPES (t1) == void_list_node)
5679     return true;
5680 
5681   if (TREE_TYPE (t2) == void_type_node &&
5682       TYPE_ARG_TYPES (t2) == void_list_node)
5683     return true;
5684 
5685   if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5686     return false;
5687 
5688   for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5689        t1 && t2;
5690        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5691     if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5692       return false;
5693 
5694   return true;
5695 }
5696 
5697 /* Build an expression representing a cast to type TYPE of expression EXPR.
5698    LOC is the location of the cast-- typically the open paren of the cast.  */
5699 
5700 tree
build_c_cast(location_t loc,tree type,tree expr)5701 build_c_cast (location_t loc, tree type, tree expr)
5702 {
5703   tree value;
5704 
5705   if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5706     expr = TREE_OPERAND (expr, 0);
5707 
5708   value = expr;
5709 
5710   if (type == error_mark_node || expr == error_mark_node)
5711     return error_mark_node;
5712 
5713   /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5714      only in <protocol> qualifications.  But when constructing cast expressions,
5715      the protocols do matter and must be kept around.  */
5716   if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5717     return build1 (NOP_EXPR, type, expr);
5718 
5719   type = TYPE_MAIN_VARIANT (type);
5720 
5721   if (TREE_CODE (type) == ARRAY_TYPE)
5722     {
5723       error_at (loc, "cast specifies array type");
5724       return error_mark_node;
5725     }
5726 
5727   if (TREE_CODE (type) == FUNCTION_TYPE)
5728     {
5729       error_at (loc, "cast specifies function type");
5730       return error_mark_node;
5731     }
5732 
5733   if (!VOID_TYPE_P (type))
5734     {
5735       value = require_complete_type (loc, value);
5736       if (value == error_mark_node)
5737 	return error_mark_node;
5738     }
5739 
5740   if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5741     {
5742       if (RECORD_OR_UNION_TYPE_P (type))
5743 	pedwarn (loc, OPT_Wpedantic,
5744 		 "ISO C forbids casting nonscalar to the same type");
5745 
5746       /* Convert to remove any qualifiers from VALUE's type.  */
5747       value = convert (type, value);
5748     }
5749   else if (TREE_CODE (type) == UNION_TYPE)
5750     {
5751       tree field;
5752 
5753       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5754 	if (TREE_TYPE (field) != error_mark_node
5755 	    && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5756 			  TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5757 	  break;
5758 
5759       if (field)
5760 	{
5761 	  tree t;
5762 	  bool maybe_const = true;
5763 
5764 	  pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5765 	  t = c_fully_fold (value, false, &maybe_const);
5766 	  t = build_constructor_single (type, field, t);
5767 	  if (!maybe_const)
5768 	    t = c_wrap_maybe_const (t, true);
5769 	  t = digest_init (loc, type, t,
5770 			   NULL_TREE, false, true, 0);
5771 	  TREE_CONSTANT (t) = TREE_CONSTANT (value);
5772 	  return t;
5773 	}
5774       error_at (loc, "cast to union type from type not present in union");
5775       return error_mark_node;
5776     }
5777   else
5778     {
5779       tree otype, ovalue;
5780 
5781       if (type == void_type_node)
5782 	{
5783 	  tree t = build1 (CONVERT_EXPR, type, value);
5784 	  SET_EXPR_LOCATION (t, loc);
5785 	  return t;
5786 	}
5787 
5788       otype = TREE_TYPE (value);
5789 
5790       /* Optionally warn about potentially worrisome casts.  */
5791       if (warn_cast_qual
5792 	  && TREE_CODE (type) == POINTER_TYPE
5793 	  && TREE_CODE (otype) == POINTER_TYPE)
5794 	handle_warn_cast_qual (loc, type, otype);
5795 
5796       /* Warn about conversions between pointers to disjoint
5797 	 address spaces.  */
5798       if (TREE_CODE (type) == POINTER_TYPE
5799 	  && TREE_CODE (otype) == POINTER_TYPE
5800 	  && !null_pointer_constant_p (value))
5801 	{
5802 	  addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5803 	  addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5804 	  addr_space_t as_common;
5805 
5806 	  if (!addr_space_superset (as_to, as_from, &as_common))
5807 	    {
5808 	      if (ADDR_SPACE_GENERIC_P (as_from))
5809 		warning_at (loc, 0, "cast to %s address space pointer "
5810 			    "from disjoint generic address space pointer",
5811 			    c_addr_space_name (as_to));
5812 
5813 	      else if (ADDR_SPACE_GENERIC_P (as_to))
5814 		warning_at (loc, 0, "cast to generic address space pointer "
5815 			    "from disjoint %s address space pointer",
5816 			    c_addr_space_name (as_from));
5817 
5818 	      else
5819 		warning_at (loc, 0, "cast to %s address space pointer "
5820 			    "from disjoint %s address space pointer",
5821 			    c_addr_space_name (as_to),
5822 			    c_addr_space_name (as_from));
5823 	    }
5824 	}
5825 
5826       /* Warn about possible alignment problems.  */
5827       if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5828 	  && TREE_CODE (type) == POINTER_TYPE
5829 	  && TREE_CODE (otype) == POINTER_TYPE
5830 	  && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5831 	  && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5832 	  /* Don't warn about opaque types, where the actual alignment
5833 	     restriction is unknown.  */
5834 	  && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5835 	       && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5836 	  && min_align_of_type (TREE_TYPE (type))
5837 	     > min_align_of_type (TREE_TYPE (otype)))
5838 	warning_at (loc, OPT_Wcast_align,
5839 		    "cast increases required alignment of target type");
5840 
5841       if (TREE_CODE (type) == INTEGER_TYPE
5842 	  && TREE_CODE (otype) == POINTER_TYPE
5843 	  && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5844       /* Unlike conversion of integers to pointers, where the
5845          warning is disabled for converting constants because
5846          of cases such as SIG_*, warn about converting constant
5847          pointers to integers. In some cases it may cause unwanted
5848          sign extension, and a warning is appropriate.  */
5849 	warning_at (loc, OPT_Wpointer_to_int_cast,
5850 		    "cast from pointer to integer of different size");
5851 
5852       if (TREE_CODE (value) == CALL_EXPR
5853 	  && TREE_CODE (type) != TREE_CODE (otype))
5854 	warning_at (loc, OPT_Wbad_function_cast,
5855 		    "cast from function call of type %qT "
5856 		    "to non-matching type %qT", otype, type);
5857 
5858       if (TREE_CODE (type) == POINTER_TYPE
5859 	  && TREE_CODE (otype) == INTEGER_TYPE
5860 	  && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5861 	  /* Don't warn about converting any constant.  */
5862 	  && !TREE_CONSTANT (value))
5863 	warning_at (loc,
5864 		    OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5865 		    "of different size");
5866 
5867       if (warn_strict_aliasing <= 2)
5868         strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
5869 
5870       /* If pedantic, warn for conversions between function and object
5871 	 pointer types, except for converting a null pointer constant
5872 	 to function pointer type.  */
5873       if (pedantic
5874 	  && TREE_CODE (type) == POINTER_TYPE
5875 	  && TREE_CODE (otype) == POINTER_TYPE
5876 	  && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5877 	  && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5878 	pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5879 		 "conversion of function pointer to object pointer type");
5880 
5881       if (pedantic
5882 	  && TREE_CODE (type) == POINTER_TYPE
5883 	  && TREE_CODE (otype) == POINTER_TYPE
5884 	  && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5885 	  && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5886 	  && !null_pointer_constant_p (value))
5887 	pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5888 		 "conversion of object pointer to function pointer type");
5889 
5890       if (TREE_CODE (type) == POINTER_TYPE
5891 	  && TREE_CODE (otype) == POINTER_TYPE
5892 	  && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5893 	  && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5894 	  && !c_safe_function_type_cast_p (TREE_TYPE (type),
5895 					   TREE_TYPE (otype)))
5896 	warning_at (loc, OPT_Wcast_function_type,
5897 		    "cast between incompatible function types"
5898 		    " from %qT to %qT", otype, type);
5899 
5900       ovalue = value;
5901       value = convert (type, value);
5902 
5903       /* Ignore any integer overflow caused by the cast.  */
5904       if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
5905 	{
5906 	  if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
5907 	    {
5908 	      if (!TREE_OVERFLOW (value))
5909 		{
5910 		  /* Avoid clobbering a shared constant.  */
5911 		  value = copy_node (value);
5912 		  TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5913 		}
5914 	    }
5915 	  else if (TREE_OVERFLOW (value))
5916 	    /* Reset VALUE's overflow flags, ensuring constant sharing.  */
5917 	    value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
5918 	}
5919     }
5920 
5921   /* Don't let a cast be an lvalue.  */
5922   if (lvalue_p (value))
5923     value = non_lvalue_loc (loc, value);
5924 
5925   /* Don't allow the results of casting to floating-point or complex
5926      types be confused with actual constants, or casts involving
5927      integer and pointer types other than direct integer-to-integer
5928      and integer-to-pointer be confused with integer constant
5929      expressions and null pointer constants.  */
5930   if (TREE_CODE (value) == REAL_CST
5931       || TREE_CODE (value) == COMPLEX_CST
5932       || (TREE_CODE (value) == INTEGER_CST
5933 	  && !((TREE_CODE (expr) == INTEGER_CST
5934 		&& INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5935 	       || TREE_CODE (expr) == REAL_CST
5936 	       || TREE_CODE (expr) == COMPLEX_CST)))
5937       value = build1 (NOP_EXPR, type, value);
5938 
5939   protected_set_expr_location (value, loc);
5940   return value;
5941 }
5942 
5943 /* Interpret a cast of expression EXPR to type TYPE.  LOC is the
5944    location of the open paren of the cast, or the position of the cast
5945    expr.  */
5946 tree
c_cast_expr(location_t loc,struct c_type_name * type_name,tree expr)5947 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
5948 {
5949   tree type;
5950   tree type_expr = NULL_TREE;
5951   bool type_expr_const = true;
5952   tree ret;
5953   int saved_wsp = warn_strict_prototypes;
5954 
5955   /* This avoids warnings about unprototyped casts on
5956      integers.  E.g. "#define SIG_DFL (void(*)())0".  */
5957   if (TREE_CODE (expr) == INTEGER_CST)
5958     warn_strict_prototypes = 0;
5959   type = groktypename (type_name, &type_expr, &type_expr_const);
5960   warn_strict_prototypes = saved_wsp;
5961 
5962   if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5963       && reject_gcc_builtin (expr))
5964     return error_mark_node;
5965 
5966   ret = build_c_cast (loc, type, expr);
5967   if (type_expr)
5968     {
5969       bool inner_expr_const = true;
5970       ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
5971       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
5972       C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5973 					     && inner_expr_const);
5974       SET_EXPR_LOCATION (ret, loc);
5975     }
5976 
5977   if (!EXPR_HAS_LOCATION (ret))
5978     protected_set_expr_location (ret, loc);
5979 
5980   /* C++ does not permits types to be defined in a cast, but it
5981      allows references to incomplete types.  */
5982   if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
5983     warning_at (loc, OPT_Wc___compat,
5984 		"defining a type in a cast is invalid in C++");
5985 
5986   return ret;
5987 }
5988 
5989 /* Build an assignment expression of lvalue LHS from value RHS.
5990    If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5991    may differ from TREE_TYPE (LHS) for an enum bitfield.
5992    MODIFYCODE is the code for a binary operator that we use
5993    to combine the old value of LHS with RHS to get the new value.
5994    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5995    If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5996    which may differ from TREE_TYPE (RHS) for an enum value.
5997 
5998    LOCATION is the location of the MODIFYCODE operator.
5999    RHS_LOC is the location of the RHS.  */
6000 
6001 tree
build_modify_expr(location_t location,tree lhs,tree lhs_origtype,enum tree_code modifycode,location_t rhs_loc,tree rhs,tree rhs_origtype)6002 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6003 		   enum tree_code modifycode,
6004 		   location_t rhs_loc, tree rhs, tree rhs_origtype)
6005 {
6006   tree result;
6007   tree newrhs;
6008   tree rhseval = NULL_TREE;
6009   tree lhstype = TREE_TYPE (lhs);
6010   tree olhstype = lhstype;
6011   bool npc;
6012   bool is_atomic_op;
6013 
6014   /* Types that aren't fully specified cannot be used in assignments.  */
6015   lhs = require_complete_type (location, lhs);
6016 
6017   /* Avoid duplicate error messages from operands that had errors.  */
6018   if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6019     return error_mark_node;
6020 
6021   /* Ensure an error for assigning a non-lvalue array to an array in
6022      C90.  */
6023   if (TREE_CODE (lhstype) == ARRAY_TYPE)
6024     {
6025       error_at (location, "assignment to expression with array type");
6026       return error_mark_node;
6027     }
6028 
6029   /* For ObjC properties, defer this check.  */
6030   if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6031     return error_mark_node;
6032 
6033   is_atomic_op = really_atomic_lvalue (lhs);
6034 
6035   newrhs = rhs;
6036 
6037   if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6038     {
6039       tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6040 				      lhs_origtype, modifycode, rhs_loc, rhs,
6041 				      rhs_origtype);
6042       if (inner == error_mark_node)
6043 	return error_mark_node;
6044       result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6045 		       C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6046       gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6047       C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6048       protected_set_expr_location (result, location);
6049       return result;
6050     }
6051 
6052   /* If a binary op has been requested, combine the old LHS value with the RHS
6053      producing the value we should actually store into the LHS.  */
6054 
6055   if (modifycode != NOP_EXPR)
6056     {
6057       lhs = c_fully_fold (lhs, false, NULL, true);
6058       lhs = stabilize_reference (lhs);
6059 
6060       /* Construct the RHS for any non-atomic compound assignemnt. */
6061       if (!is_atomic_op)
6062         {
6063 	  /* If in LHS op= RHS the RHS has side-effects, ensure they
6064 	     are preevaluated before the rest of the assignment expression's
6065 	     side-effects, because RHS could contain e.g. function calls
6066 	     that modify LHS.  */
6067 	  if (TREE_SIDE_EFFECTS (rhs))
6068 	    {
6069 	      if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6070 		newrhs = save_expr (TREE_OPERAND (rhs, 0));
6071 	      else
6072 		newrhs = save_expr (rhs);
6073 	      rhseval = newrhs;
6074 	      if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6075 		newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6076 				 newrhs);
6077 	    }
6078 	  newrhs = build_binary_op (location,
6079 				    modifycode, lhs, newrhs, true);
6080 
6081 	  /* The original type of the right hand side is no longer
6082 	     meaningful.  */
6083 	  rhs_origtype = NULL_TREE;
6084 	}
6085     }
6086 
6087   if (c_dialect_objc ())
6088     {
6089       /* Check if we are modifying an Objective-C property reference;
6090 	 if so, we need to generate setter calls.  */
6091       if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6092 	result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6093       else
6094 	result = objc_maybe_build_modify_expr (lhs, newrhs);
6095       if (result)
6096 	goto return_result;
6097 
6098       /* Else, do the check that we postponed for Objective-C.  */
6099       if (!lvalue_or_else (location, lhs, lv_assign))
6100 	return error_mark_node;
6101     }
6102 
6103   /* Give an error for storing in something that is 'const'.  */
6104 
6105   if (TYPE_READONLY (lhstype)
6106       || (RECORD_OR_UNION_TYPE_P (lhstype)
6107 	  && C_TYPE_FIELDS_READONLY (lhstype)))
6108     {
6109       readonly_error (location, lhs, lv_assign);
6110       return error_mark_node;
6111     }
6112   else if (TREE_READONLY (lhs))
6113     readonly_warning (lhs, lv_assign);
6114 
6115   /* If storing into a structure or union member,
6116      it has probably been given type `int'.
6117      Compute the type that would go with
6118      the actual amount of storage the member occupies.  */
6119 
6120   if (TREE_CODE (lhs) == COMPONENT_REF
6121       && (TREE_CODE (lhstype) == INTEGER_TYPE
6122 	  || TREE_CODE (lhstype) == BOOLEAN_TYPE
6123 	  || TREE_CODE (lhstype) == REAL_TYPE
6124 	  || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6125     lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6126 
6127   /* If storing in a field that is in actuality a short or narrower than one,
6128      we must store in the field in its actual type.  */
6129 
6130   if (lhstype != TREE_TYPE (lhs))
6131     {
6132       lhs = copy_node (lhs);
6133       TREE_TYPE (lhs) = lhstype;
6134     }
6135 
6136   /* Issue -Wc++-compat warnings about an assignment to an enum type
6137      when LHS does not have its original type.  This happens for,
6138      e.g., an enum bitfield in a struct.  */
6139   if (warn_cxx_compat
6140       && lhs_origtype != NULL_TREE
6141       && lhs_origtype != lhstype
6142       && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6143     {
6144       tree checktype = (rhs_origtype != NULL_TREE
6145 			? rhs_origtype
6146 			: TREE_TYPE (rhs));
6147       if (checktype != error_mark_node
6148 	  && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6149 	      || (is_atomic_op && modifycode != NOP_EXPR)))
6150 	warning_at (location, OPT_Wc___compat,
6151 		    "enum conversion in assignment is invalid in C++");
6152     }
6153 
6154   /* If the lhs is atomic, remove that qualifier.  */
6155   if (is_atomic_op)
6156     {
6157       lhstype = build_qualified_type (lhstype,
6158 				      (TYPE_QUALS (lhstype)
6159 				       & ~TYPE_QUAL_ATOMIC));
6160       olhstype = build_qualified_type (olhstype,
6161 				       (TYPE_QUALS (lhstype)
6162 					& ~TYPE_QUAL_ATOMIC));
6163     }
6164 
6165   /* Convert new value to destination type.  Fold it first, then
6166      restore any excess precision information, for the sake of
6167      conversion warnings.  */
6168 
6169   if (!(is_atomic_op && modifycode != NOP_EXPR))
6170     {
6171       tree rhs_semantic_type = NULL_TREE;
6172       if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6173 	{
6174 	  rhs_semantic_type = TREE_TYPE (newrhs);
6175 	  newrhs = TREE_OPERAND (newrhs, 0);
6176 	}
6177       npc = null_pointer_constant_p (newrhs);
6178       newrhs = c_fully_fold (newrhs, false, NULL);
6179       if (rhs_semantic_type)
6180 	newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6181       newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6182 				       rhs_origtype, ic_assign, npc,
6183 				       NULL_TREE, NULL_TREE, 0);
6184       if (TREE_CODE (newrhs) == ERROR_MARK)
6185 	return error_mark_node;
6186     }
6187 
6188   /* Emit ObjC write barrier, if necessary.  */
6189   if (c_dialect_objc () && flag_objc_gc)
6190     {
6191       result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6192       if (result)
6193 	{
6194 	  protected_set_expr_location (result, location);
6195 	  goto return_result;
6196 	}
6197     }
6198 
6199   /* Scan operands.  */
6200 
6201   if (is_atomic_op)
6202     result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6203   else
6204     {
6205       result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6206       TREE_SIDE_EFFECTS (result) = 1;
6207       protected_set_expr_location (result, location);
6208     }
6209 
6210   /* If we got the LHS in a different type for storing in,
6211      convert the result back to the nominal type of LHS
6212      so that the value we return always has the same type
6213      as the LHS argument.  */
6214 
6215   if (olhstype == TREE_TYPE (result))
6216     goto return_result;
6217 
6218   result = convert_for_assignment (location, rhs_loc, olhstype, result,
6219 				   rhs_origtype, ic_assign, false, NULL_TREE,
6220 				   NULL_TREE, 0);
6221   protected_set_expr_location (result, location);
6222 
6223 return_result:
6224   if (rhseval)
6225     result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6226   return result;
6227 }
6228 
6229 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6230    This is used to implement -fplan9-extensions.  */
6231 
6232 static bool
find_anonymous_field_with_type(tree struct_type,tree type)6233 find_anonymous_field_with_type (tree struct_type, tree type)
6234 {
6235   tree field;
6236   bool found;
6237 
6238   gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6239   found = false;
6240   for (field = TYPE_FIELDS (struct_type);
6241        field != NULL_TREE;
6242        field = TREE_CHAIN (field))
6243     {
6244       tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6245 			? c_build_qualified_type (TREE_TYPE (field),
6246 						  TYPE_QUAL_ATOMIC)
6247 			: TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6248       if (DECL_NAME (field) == NULL
6249 	  && comptypes (type, fieldtype))
6250 	{
6251 	  if (found)
6252 	    return false;
6253 	  found = true;
6254 	}
6255       else if (DECL_NAME (field) == NULL
6256 	       && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6257 	       && find_anonymous_field_with_type (TREE_TYPE (field), type))
6258 	{
6259 	  if (found)
6260 	    return false;
6261 	  found = true;
6262 	}
6263     }
6264   return found;
6265 }
6266 
6267 /* RHS is an expression whose type is pointer to struct.  If there is
6268    an anonymous field in RHS with type TYPE, then return a pointer to
6269    that field in RHS.  This is used with -fplan9-extensions.  This
6270    returns NULL if no conversion could be found.  */
6271 
6272 static tree
convert_to_anonymous_field(location_t location,tree type,tree rhs)6273 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6274 {
6275   tree rhs_struct_type, lhs_main_type;
6276   tree field, found_field;
6277   bool found_sub_field;
6278   tree ret;
6279 
6280   gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6281   rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6282   gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6283 
6284   gcc_assert (POINTER_TYPE_P (type));
6285   lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6286 		   ? c_build_qualified_type (TREE_TYPE (type),
6287 					     TYPE_QUAL_ATOMIC)
6288 		   : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6289 
6290   found_field = NULL_TREE;
6291   found_sub_field = false;
6292   for (field = TYPE_FIELDS (rhs_struct_type);
6293        field != NULL_TREE;
6294        field = TREE_CHAIN (field))
6295     {
6296       if (DECL_NAME (field) != NULL_TREE
6297 	  || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6298 	continue;
6299       tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6300 			? c_build_qualified_type (TREE_TYPE (field),
6301 						  TYPE_QUAL_ATOMIC)
6302 			: TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6303       if (comptypes (lhs_main_type, fieldtype))
6304 	{
6305 	  if (found_field != NULL_TREE)
6306 	    return NULL_TREE;
6307 	  found_field = field;
6308 	}
6309       else if (find_anonymous_field_with_type (TREE_TYPE (field),
6310 					       lhs_main_type))
6311 	{
6312 	  if (found_field != NULL_TREE)
6313 	    return NULL_TREE;
6314 	  found_field = field;
6315 	  found_sub_field = true;
6316 	}
6317     }
6318 
6319   if (found_field == NULL_TREE)
6320     return NULL_TREE;
6321 
6322   ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6323 			 build_fold_indirect_ref (rhs), found_field,
6324 			 NULL_TREE);
6325   ret = build_fold_addr_expr_loc (location, ret);
6326 
6327   if (found_sub_field)
6328     {
6329       ret = convert_to_anonymous_field (location, type, ret);
6330       gcc_assert (ret != NULL_TREE);
6331     }
6332 
6333   return ret;
6334 }
6335 
6336 /* Issue an error message for a bad initializer component.
6337    GMSGID identifies the message.
6338    The component name is taken from the spelling stack.  */
6339 
6340 static void ATTRIBUTE_GCC_DIAG (2,0)
error_init(location_t loc,const char * gmsgid,...)6341 error_init (location_t loc, const char *gmsgid, ...)
6342 {
6343   char *ofwhat;
6344 
6345   auto_diagnostic_group d;
6346 
6347   /* The gmsgid may be a format string with %< and %>. */
6348   va_list ap;
6349   va_start (ap, gmsgid);
6350   bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6351   va_end (ap);
6352 
6353   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6354   if (*ofwhat && warned)
6355     inform (loc, "(near initialization for %qs)", ofwhat);
6356 }
6357 
6358 /* Issue a pedantic warning for a bad initializer component.  OPT is
6359    the option OPT_* (from options.h) controlling this warning or 0 if
6360    it is unconditionally given.  GMSGID identifies the message.  The
6361    component name is taken from the spelling stack.  */
6362 
6363 static void ATTRIBUTE_GCC_DIAG (3,0)
pedwarn_init(location_t loc,int opt,const char * gmsgid,...)6364 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6365 {
6366   /* Use the location where a macro was expanded rather than where
6367      it was defined to make sure macros defined in system headers
6368      but used incorrectly elsewhere are diagnosed.  */
6369   location_t exploc = expansion_point_location_if_in_system_header (loc);
6370   auto_diagnostic_group d;
6371   va_list ap;
6372   va_start (ap, gmsgid);
6373   bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6374   va_end (ap);
6375   char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6376   if (*ofwhat && warned)
6377     inform (exploc, "(near initialization for %qs)", ofwhat);
6378 }
6379 
6380 /* Issue a warning for a bad initializer component.
6381 
6382    OPT is the OPT_W* value corresponding to the warning option that
6383    controls this warning.  GMSGID identifies the message.  The
6384    component name is taken from the spelling stack.  */
6385 
6386 static void
warning_init(location_t loc,int opt,const char * gmsgid)6387 warning_init (location_t loc, int opt, const char *gmsgid)
6388 {
6389   char *ofwhat;
6390   bool warned;
6391 
6392   auto_diagnostic_group d;
6393 
6394   /* Use the location where a macro was expanded rather than where
6395      it was defined to make sure macros defined in system headers
6396      but used incorrectly elsewhere are diagnosed.  */
6397   location_t exploc = expansion_point_location_if_in_system_header (loc);
6398 
6399   /* The gmsgid may be a format string with %< and %>. */
6400   warned = warning_at (exploc, opt, gmsgid);
6401   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6402   if (*ofwhat && warned)
6403     inform (exploc, "(near initialization for %qs)", ofwhat);
6404 }
6405 
6406 /* If TYPE is an array type and EXPR is a parenthesized string
6407    constant, warn if pedantic that EXPR is being used to initialize an
6408    object of type TYPE.  */
6409 
6410 void
maybe_warn_string_init(location_t loc,tree type,struct c_expr expr)6411 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6412 {
6413   if (pedantic
6414       && TREE_CODE (type) == ARRAY_TYPE
6415       && TREE_CODE (expr.value) == STRING_CST
6416       && expr.original_code != STRING_CST)
6417     pedwarn_init (loc, OPT_Wpedantic,
6418 		  "array initialized from parenthesized string constant");
6419 }
6420 
6421 /* Attempt to locate the parameter with the given index within FNDECL,
6422    returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found.  */
6423 
6424 static location_t
get_fndecl_argument_location(tree fndecl,int argnum)6425 get_fndecl_argument_location (tree fndecl, int argnum)
6426 {
6427   int i;
6428   tree param;
6429 
6430   /* Locate param by index within DECL_ARGUMENTS (fndecl).  */
6431   for (i = 0, param = DECL_ARGUMENTS (fndecl);
6432        i < argnum && param;
6433        i++, param = TREE_CHAIN (param))
6434     ;
6435 
6436   /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6437      return DECL_SOURCE_LOCATION (FNDECL).  */
6438   if (param == NULL)
6439     return DECL_SOURCE_LOCATION (fndecl);
6440 
6441   return DECL_SOURCE_LOCATION (param);
6442 }
6443 
6444 /* Issue a note about a mismatching argument for parameter PARMNUM
6445    to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6446    Attempt to issue the note at the pertinent parameter of the decl;
6447    failing that issue it at the location of FUNDECL; failing that
6448    issue it at PLOC.  */
6449 
6450 static void
inform_for_arg(tree fundecl,location_t ploc,int parmnum,tree expected_type,tree actual_type)6451 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6452 		tree expected_type, tree actual_type)
6453 {
6454   location_t loc;
6455   if (fundecl && !DECL_IS_BUILTIN (fundecl))
6456     loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6457   else
6458     loc = ploc;
6459 
6460   inform (loc,
6461 	  "expected %qT but argument is of type %qT",
6462 	  expected_type, actual_type);
6463 }
6464 
6465 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6466    function FUNDECL declared without prototype to parameter PARMNUM of
6467    PARMTYPE when ARGTYPE does not promote to PARMTYPE.  */
6468 
6469 static void
maybe_warn_builtin_no_proto_arg(location_t loc,tree fundecl,int parmnum,tree parmtype,tree argtype)6470 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6471 				 tree parmtype, tree argtype)
6472 {
6473   tree_code parmcode = TREE_CODE (parmtype);
6474   tree_code argcode = TREE_CODE (argtype);
6475   tree promoted = c_type_promotes_to (argtype);
6476 
6477   /* Avoid warning for enum arguments that promote to an integer type
6478      of the same size/mode.  */
6479   if (parmcode == INTEGER_TYPE
6480       && argcode == ENUMERAL_TYPE
6481       && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6482     return;
6483 
6484   if ((parmcode == argcode
6485        || (parmcode == INTEGER_TYPE
6486 	   && argcode == ENUMERAL_TYPE))
6487       && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6488     return;
6489 
6490   /* This diagnoses even signed/unsigned mismatches.  Those might be
6491      safe in many cases but GCC may emit suboptimal code for them so
6492      warning on those cases drives efficiency improvements.  */
6493   if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6494 		  TYPE_MAIN_VARIANT (promoted) == argtype
6495 		  ? G_("%qD argument %d type is %qT where %qT is expected "
6496 		       "in a call to built-in function declared without "
6497 		       "prototype")
6498 		  : G_("%qD argument %d promotes to %qT where %qT is expected "
6499 		       "in a call to built-in function declared without "
6500 		       "prototype"),
6501 		  fundecl, parmnum, promoted, parmtype))
6502     inform (DECL_SOURCE_LOCATION (fundecl),
6503 	    "built-in %qD declared here",
6504 	    fundecl);
6505 }
6506 
6507 /* Convert value RHS to type TYPE as preparation for an assignment to
6508    an lvalue of type TYPE.  If ORIGTYPE is not NULL_TREE, it is the
6509    original type of RHS; this differs from TREE_TYPE (RHS) for enum
6510    types.  NULL_POINTER_CONSTANT says whether RHS was a null pointer
6511    constant before any folding.
6512    The real work of conversion is done by `convert'.
6513    The purpose of this function is to generate error messages
6514    for assignments that are not allowed in C.
6515    ERRTYPE says whether it is argument passing, assignment,
6516    initialization or return.
6517 
6518    In the following example, '~' denotes where EXPR_LOC and '^' where
6519    LOCATION point to:
6520 
6521      f (var);      [ic_argpass]
6522      ^  ~~~
6523      x = var;      [ic_assign]
6524        ^ ~~~;
6525      int x = var;  [ic_init]
6526 	     ^^^
6527      return x;     [ic_return]
6528 	    ^
6529 
6530    FUNCTION is a tree for the function being called.
6531    PARMNUM is the number of the argument, for printing in error messages.
6532    WARNOPT may be set to a warning option to issue the corresponding warning
6533    rather than an error for invalid conversions.  Used for calls to built-in
6534    functions declared without a prototype.  */
6535 
6536 static tree
convert_for_assignment(location_t location,location_t expr_loc,tree type,tree rhs,tree origtype,enum impl_conv errtype,bool null_pointer_constant,tree fundecl,tree function,int parmnum,int warnopt)6537 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6538 			tree rhs, tree origtype, enum impl_conv errtype,
6539 			bool null_pointer_constant, tree fundecl,
6540 			tree function, int parmnum, int warnopt /* = 0 */)
6541 {
6542   enum tree_code codel = TREE_CODE (type);
6543   tree orig_rhs = rhs;
6544   tree rhstype;
6545   enum tree_code coder;
6546   tree rname = NULL_TREE;
6547   bool objc_ok = false;
6548 
6549   /* Use the expansion point location to handle cases such as user's
6550      function returning a wrong-type macro defined in a system header.  */
6551   location = expansion_point_location_if_in_system_header (location);
6552 
6553   if (errtype == ic_argpass)
6554     {
6555       tree selector;
6556       /* Change pointer to function to the function itself for
6557 	 diagnostics.  */
6558       if (TREE_CODE (function) == ADDR_EXPR
6559 	  && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6560 	function = TREE_OPERAND (function, 0);
6561 
6562       /* Handle an ObjC selector specially for diagnostics.  */
6563       selector = objc_message_selector ();
6564       rname = function;
6565       if (selector && parmnum > 2)
6566 	{
6567 	  rname = selector;
6568 	  parmnum -= 2;
6569 	}
6570     }
6571 
6572   /* This macro is used to emit diagnostics to ensure that all format
6573      strings are complete sentences, visible to gettext and checked at
6574      compile time.  */
6575 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE)	 \
6576   do {                                                                   \
6577     switch (errtype)                                                     \
6578       {                                                                  \
6579       case ic_argpass:                                                   \
6580 	{								\
6581 	  auto_diagnostic_group d;						\
6582 	  if (pedwarn (PLOC, OPT, AR, parmnum, rname))		\
6583 	    inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6584 	}								\
6585         break;                                                           \
6586       case ic_assign:                                                    \
6587         pedwarn (LOCATION, OPT, AS);                                     \
6588         break;                                                           \
6589       case ic_init:                                                      \
6590         pedwarn_init (LOCATION, OPT, IN);                                \
6591         break;                                                           \
6592       case ic_return:                                                    \
6593         pedwarn (LOCATION, OPT, RE);					 \
6594         break;                                                           \
6595       default:                                                           \
6596         gcc_unreachable ();                                              \
6597       }                                                                  \
6598   } while (0)
6599 
6600   /* This macro is used to emit diagnostics to ensure that all format
6601      strings are complete sentences, visible to gettext and checked at
6602      compile time.  It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6603      extra parameter to enumerate qualifiers.  */
6604 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6605   do {                                                                   \
6606     switch (errtype)                                                     \
6607       {                                                                  \
6608       case ic_argpass:                                                   \
6609 	{								\
6610 	auto_diagnostic_group d;						\
6611 	if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS))		\
6612 	  inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype);	\
6613 	}								\
6614         break;                                                           \
6615       case ic_assign:                                                    \
6616         pedwarn (LOCATION, OPT, AS, QUALS);				 \
6617         break;                                                           \
6618       case ic_init:                                                      \
6619         pedwarn (LOCATION, OPT, IN, QUALS);				 \
6620         break;                                                           \
6621       case ic_return:                                                    \
6622         pedwarn (LOCATION, OPT, RE, QUALS);				 \
6623         break;                                                           \
6624       default:                                                           \
6625         gcc_unreachable ();                                              \
6626       }                                                                  \
6627   } while (0)
6628 
6629   /* This macro is used to emit diagnostics to ensure that all format
6630      strings are complete sentences, visible to gettext and checked at
6631      compile time.  It is the same as PEDWARN_FOR_QUALIFIERS but uses
6632      warning_at instead of pedwarn.  */
6633 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6634   do {                                                                   \
6635     switch (errtype)                                                     \
6636       {                                                                  \
6637       case ic_argpass:                                                   \
6638 	{								\
6639 	  auto_diagnostic_group d;						\
6640 	  if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS))	\
6641 	    inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6642 	}								\
6643         break;                                                           \
6644       case ic_assign:                                                    \
6645         warning_at (LOCATION, OPT, AS, QUALS);                           \
6646         break;                                                           \
6647       case ic_init:                                                      \
6648         warning_at (LOCATION, OPT, IN, QUALS);                           \
6649         break;                                                           \
6650       case ic_return:                                                    \
6651         warning_at (LOCATION, OPT, RE, QUALS);                           \
6652         break;                                                           \
6653       default:                                                           \
6654         gcc_unreachable ();                                              \
6655       }                                                                  \
6656   } while (0)
6657 
6658   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6659     rhs = TREE_OPERAND (rhs, 0);
6660 
6661   rhstype = TREE_TYPE (rhs);
6662   coder = TREE_CODE (rhstype);
6663 
6664   if (coder == ERROR_MARK)
6665     return error_mark_node;
6666 
6667   if (c_dialect_objc ())
6668     {
6669       int parmno;
6670 
6671       switch (errtype)
6672 	{
6673 	case ic_return:
6674 	  parmno = 0;
6675 	  break;
6676 
6677 	case ic_assign:
6678 	  parmno = -1;
6679 	  break;
6680 
6681 	case ic_init:
6682 	  parmno = -2;
6683 	  break;
6684 
6685 	default:
6686 	  parmno = parmnum;
6687 	  break;
6688 	}
6689 
6690       objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6691     }
6692 
6693   if (warn_cxx_compat)
6694     {
6695       tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6696       if (checktype != error_mark_node
6697 	  && TREE_CODE (type) == ENUMERAL_TYPE
6698 	  && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6699 	switch (errtype)
6700 	  {
6701 	  case ic_argpass:
6702 	    if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6703 			 "passing argument %d of %qE is invalid in C++",
6704 			 parmnum, rname))
6705 	      inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6706 		      ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6707 		      "expected %qT but argument is of type %qT",
6708 		      type, rhstype);
6709 	    break;
6710 	  case ic_assign:
6711 	    pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6712 		     "%qT in assignment is invalid in C++", rhstype, type);
6713 	    break;
6714 	  case ic_init:
6715 	    pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6716 			  "%qT to %qT in initialization is invalid in C++",
6717 			  rhstype, type);
6718 	    break;
6719 	  case ic_return:
6720 	    pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6721 		     "%qT in return is invalid in C++", rhstype, type);
6722 	    break;
6723 	  default:
6724 	    gcc_unreachable ();
6725 	  }
6726     }
6727 
6728   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6729     {
6730       warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6731       return rhs;
6732     }
6733 
6734   if (coder == VOID_TYPE)
6735     {
6736       /* Except for passing an argument to an unprototyped function,
6737 	 this is a constraint violation.  When passing an argument to
6738 	 an unprototyped function, it is compile-time undefined;
6739 	 making it a constraint in that case was rejected in
6740 	 DR#252.  */
6741       const char msg[] = "void value not ignored as it ought to be";
6742       if (warnopt)
6743 	warning_at (location, warnopt, msg);
6744       else
6745 	error_at (location, msg);
6746       return error_mark_node;
6747     }
6748   rhs = require_complete_type (location, rhs);
6749   if (rhs == error_mark_node)
6750     return error_mark_node;
6751 
6752   if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6753     return error_mark_node;
6754 
6755   /* A non-reference type can convert to a reference.  This handles
6756      va_start, va_copy and possibly port built-ins.  */
6757   if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6758     {
6759       if (!lvalue_p (rhs))
6760 	{
6761 	  const char msg[] = "cannot pass rvalue to reference parameter";
6762 	  if (warnopt)
6763 	    warning_at (location, warnopt, msg);
6764 	  else
6765 	    error_at (location, msg);
6766 	  return error_mark_node;
6767 	}
6768       if (!c_mark_addressable (rhs))
6769 	return error_mark_node;
6770       rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6771       SET_EXPR_LOCATION (rhs, location);
6772 
6773       rhs = convert_for_assignment (location, expr_loc,
6774 				    build_pointer_type (TREE_TYPE (type)),
6775 				    rhs, origtype, errtype,
6776 				    null_pointer_constant, fundecl, function,
6777 				    parmnum, warnopt);
6778       if (rhs == error_mark_node)
6779 	return error_mark_node;
6780 
6781       rhs = build1 (NOP_EXPR, type, rhs);
6782       SET_EXPR_LOCATION (rhs, location);
6783       return rhs;
6784     }
6785   /* Some types can interconvert without explicit casts.  */
6786   else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6787 	   && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6788     return convert (type, rhs);
6789   /* Arithmetic types all interconvert, and enum is treated like int.  */
6790   else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6791 	    || codel == FIXED_POINT_TYPE
6792 	    || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6793 	    || codel == BOOLEAN_TYPE)
6794 	   && (coder == INTEGER_TYPE || coder == REAL_TYPE
6795 	       || coder == FIXED_POINT_TYPE
6796 	       || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6797 	       || coder == BOOLEAN_TYPE))
6798     {
6799       if (warnopt && errtype == ic_argpass)
6800 	maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
6801 					 rhstype);
6802 
6803       bool save = in_late_binary_op;
6804       if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6805 	  || (coder == REAL_TYPE
6806 	      && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6807 	      && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6808 	in_late_binary_op = true;
6809       tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6810 				    ? expr_loc : location, type, orig_rhs);
6811       in_late_binary_op = save;
6812       return ret;
6813     }
6814 
6815   /* Aggregates in different TUs might need conversion.  */
6816   if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6817       && codel == coder
6818       && comptypes (type, rhstype))
6819     return convert_and_check (expr_loc != UNKNOWN_LOCATION
6820 			      ? expr_loc : location, type, rhs);
6821 
6822   /* Conversion to a transparent union or record from its member types.
6823      This applies only to function arguments.  */
6824   if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6825       && TYPE_TRANSPARENT_AGGR (type))
6826       && errtype == ic_argpass)
6827     {
6828       tree memb, marginal_memb = NULL_TREE;
6829 
6830       for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6831 	{
6832 	  tree memb_type = TREE_TYPE (memb);
6833 
6834 	  if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6835 			 TYPE_MAIN_VARIANT (rhstype)))
6836 	    break;
6837 
6838 	  if (TREE_CODE (memb_type) != POINTER_TYPE)
6839 	    continue;
6840 
6841 	  if (coder == POINTER_TYPE)
6842 	    {
6843 	      tree ttl = TREE_TYPE (memb_type);
6844 	      tree ttr = TREE_TYPE (rhstype);
6845 
6846 	      /* Any non-function converts to a [const][volatile] void *
6847 		 and vice versa; otherwise, targets must be the same.
6848 		 Meanwhile, the lhs target must have all the qualifiers of
6849 		 the rhs.  */
6850 	      if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6851 		  || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6852 		  || comp_target_types (location, memb_type, rhstype))
6853 		{
6854 		  int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6855 		  int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6856 		  /* If this type won't generate any warnings, use it.  */
6857 		  if (lquals == rquals
6858 		      || ((TREE_CODE (ttr) == FUNCTION_TYPE
6859 			   && TREE_CODE (ttl) == FUNCTION_TYPE)
6860 			  ? ((lquals | rquals) == rquals)
6861 			  : ((lquals | rquals) == lquals)))
6862 		    break;
6863 
6864 		  /* Keep looking for a better type, but remember this one.  */
6865 		  if (!marginal_memb)
6866 		    marginal_memb = memb;
6867 		}
6868 	    }
6869 
6870 	  /* Can convert integer zero to any pointer type.  */
6871 	  if (null_pointer_constant)
6872 	    {
6873 	      rhs = null_pointer_node;
6874 	      break;
6875 	    }
6876 	}
6877 
6878       if (memb || marginal_memb)
6879 	{
6880 	  if (!memb)
6881 	    {
6882 	      /* We have only a marginally acceptable member type;
6883 		 it needs a warning.  */
6884 	      tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
6885 	      tree ttr = TREE_TYPE (rhstype);
6886 
6887 	      /* Const and volatile mean something different for function
6888 		 types, so the usual warnings are not appropriate.  */
6889 	      if (TREE_CODE (ttr) == FUNCTION_TYPE
6890 		  && TREE_CODE (ttl) == FUNCTION_TYPE)
6891 		{
6892 		  /* Because const and volatile on functions are
6893 		     restrictions that say the function will not do
6894 		     certain things, it is okay to use a const or volatile
6895 		     function where an ordinary one is wanted, but not
6896 		     vice-versa.  */
6897 		  if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6898 		      & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
6899 		    PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6900 					    OPT_Wdiscarded_qualifiers,
6901 					    G_("passing argument %d of %qE "
6902 					       "makes %q#v qualified function "
6903 					       "pointer from unqualified"),
6904 					    G_("assignment makes %q#v qualified "
6905 					       "function pointer from "
6906 					       "unqualified"),
6907 					    G_("initialization makes %q#v qualified "
6908 					       "function pointer from "
6909 					       "unqualified"),
6910 					    G_("return makes %q#v qualified function "
6911 					       "pointer from unqualified"),
6912 					    TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
6913 		}
6914 	      else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6915 		       & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
6916 		PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6917 				        OPT_Wdiscarded_qualifiers,
6918 				        G_("passing argument %d of %qE discards "
6919 					   "%qv qualifier from pointer target type"),
6920 				        G_("assignment discards %qv qualifier "
6921 					   "from pointer target type"),
6922 				        G_("initialization discards %qv qualifier "
6923 					   "from pointer target type"),
6924 				        G_("return discards %qv qualifier from "
6925 					   "pointer target type"),
6926 				        TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6927 
6928 	      memb = marginal_memb;
6929 	    }
6930 
6931 	  if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
6932 	    pedwarn (location, OPT_Wpedantic,
6933 		     "ISO C prohibits argument conversion to union type");
6934 
6935 	  rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
6936 	  return build_constructor_single (type, memb, rhs);
6937 	}
6938     }
6939 
6940   /* Conversions among pointers */
6941   else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6942 	   && (coder == codel))
6943     {
6944       /* If RHS refers to a built-in declared without a prototype
6945 	 BLTIN is the declaration of the built-in with a prototype
6946 	 and RHSTYPE is set to the actual type of the built-in.  */
6947       tree bltin;
6948       rhstype = type_or_builtin_type (rhs, &bltin);
6949 
6950       tree ttl = TREE_TYPE (type);
6951       tree ttr = TREE_TYPE (rhstype);
6952       tree mvl = ttl;
6953       tree mvr = ttr;
6954       bool is_opaque_pointer;
6955       int target_cmp = 0;   /* Cache comp_target_types () result.  */
6956       addr_space_t asl;
6957       addr_space_t asr;
6958 
6959       if (TREE_CODE (mvl) != ARRAY_TYPE)
6960 	mvl = (TYPE_ATOMIC (mvl)
6961 	       ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6962 					 TYPE_QUAL_ATOMIC)
6963 	       : TYPE_MAIN_VARIANT (mvl));
6964       if (TREE_CODE (mvr) != ARRAY_TYPE)
6965 	mvr = (TYPE_ATOMIC (mvr)
6966 	       ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6967 					 TYPE_QUAL_ATOMIC)
6968 	       : TYPE_MAIN_VARIANT (mvr));
6969       /* Opaque pointers are treated like void pointers.  */
6970       is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
6971 
6972       /* The Plan 9 compiler permits a pointer to a struct to be
6973 	 automatically converted into a pointer to an anonymous field
6974 	 within the struct.  */
6975       if (flag_plan9_extensions
6976 	  && RECORD_OR_UNION_TYPE_P (mvl)
6977 	  && RECORD_OR_UNION_TYPE_P (mvr)
6978 	  && mvl != mvr)
6979 	{
6980 	  tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6981 	  if (new_rhs != NULL_TREE)
6982 	    {
6983 	      rhs = new_rhs;
6984 	      rhstype = TREE_TYPE (rhs);
6985 	      coder = TREE_CODE (rhstype);
6986 	      ttr = TREE_TYPE (rhstype);
6987 	      mvr = TYPE_MAIN_VARIANT (ttr);
6988 	    }
6989 	}
6990 
6991       /* C++ does not allow the implicit conversion void* -> T*.  However,
6992 	 for the purpose of reducing the number of false positives, we
6993 	 tolerate the special case of
6994 
6995 		int *p = NULL;
6996 
6997 	 where NULL is typically defined in C to be '(void *) 0'.  */
6998       if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
6999 	warning_at (errtype == ic_argpass ? expr_loc : location,
7000 		    OPT_Wc___compat,
7001 		    "request for implicit conversion "
7002 		    "from %qT to %qT not permitted in C++", rhstype, type);
7003 
7004       /* See if the pointers point to incompatible address spaces.  */
7005       asl = TYPE_ADDR_SPACE (ttl);
7006       asr = TYPE_ADDR_SPACE (ttr);
7007       if (!null_pointer_constant_p (rhs)
7008 	  && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7009 	{
7010 	  switch (errtype)
7011 	    {
7012 	    case ic_argpass:
7013 	      {
7014 		const char msg[] = G_("passing argument %d of %qE from "
7015 				      "pointer to non-enclosed address space");
7016 		if (warnopt)
7017 		  warning_at (expr_loc, warnopt, msg, parmnum, rname);
7018 		else
7019 		  error_at (expr_loc, msg, parmnum, rname);
7020 	      break;
7021 	      }
7022 	    case ic_assign:
7023 	      {
7024 		const char msg[] = G_("assignment from pointer to "
7025 				      "non-enclosed address space");
7026 		if (warnopt)
7027 		  warning_at (location, warnopt, msg);
7028 		else
7029 		  error_at (location, msg);
7030 		break;
7031 	      }
7032 	    case ic_init:
7033 	      {
7034 		const char msg[] = G_("initialization from pointer to "
7035 				      "non-enclosed address space");
7036 		if (warnopt)
7037 		  warning_at (location, warnopt, msg);
7038 		else
7039 		  error_at (location, msg);
7040 		break;
7041 	      }
7042 	    case ic_return:
7043 	      {
7044 		const char msg[] = G_("return from pointer to "
7045 				      "non-enclosed address space");
7046 		if (warnopt)
7047 		  warning_at (location, warnopt, msg);
7048 		else
7049 		  error_at (location, msg);
7050 		break;
7051 	      }
7052 	    default:
7053 	      gcc_unreachable ();
7054 	    }
7055 	  return error_mark_node;
7056 	}
7057 
7058       /* Check if the right-hand side has a format attribute but the
7059 	 left-hand side doesn't.  */
7060       if (warn_suggest_attribute_format
7061 	  && check_missing_format_attribute (type, rhstype))
7062 	{
7063 	  switch (errtype)
7064 	  {
7065 	  case ic_argpass:
7066 	    warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7067 			"argument %d of %qE might be "
7068 			"a candidate for a format attribute",
7069 			parmnum, rname);
7070 	    break;
7071 	  case ic_assign:
7072 	    warning_at (location, OPT_Wsuggest_attribute_format,
7073 			"assignment left-hand side might be "
7074 			"a candidate for a format attribute");
7075 	    break;
7076 	  case ic_init:
7077 	    warning_at (location, OPT_Wsuggest_attribute_format,
7078 			"initialization left-hand side might be "
7079 			"a candidate for a format attribute");
7080 	    break;
7081 	  case ic_return:
7082 	    warning_at (location, OPT_Wsuggest_attribute_format,
7083 			"return type might be "
7084 			"a candidate for a format attribute");
7085 	    break;
7086 	  default:
7087 	    gcc_unreachable ();
7088 	  }
7089 	}
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 the rhs.  */
7094       if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7095 	  || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7096 	  || (target_cmp = comp_target_types (location, type, rhstype))
7097 	  || is_opaque_pointer
7098 	  || ((c_common_unsigned_type (mvl)
7099 	       == c_common_unsigned_type (mvr))
7100 	      && (c_common_signed_type (mvl)
7101 		  == c_common_signed_type (mvr))
7102 	      && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7103 	{
7104 	  /* Warn about loss of qualifers from pointers to arrays with
7105 	     qualifiers on the element type. */
7106 	  if (TREE_CODE (ttr) == ARRAY_TYPE)
7107 	    {
7108 	      ttr = strip_array_types (ttr);
7109 	      ttl = strip_array_types (ttl);
7110 
7111 	      if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7112 		  & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7113 		WARNING_FOR_QUALIFIERS (location, expr_loc,
7114 				        OPT_Wdiscarded_array_qualifiers,
7115 				        G_("passing argument %d of %qE discards "
7116 					   "%qv qualifier from pointer target type"),
7117 				        G_("assignment discards %qv qualifier "
7118 					   "from pointer target type"),
7119 				        G_("initialization discards %qv qualifier "
7120 					   "from pointer target type"),
7121 				        G_("return discards %qv qualifier from "
7122 					   "pointer target type"),
7123                                         TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7124             }
7125           else if (pedantic
7126 	      && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7127 		  ||
7128 		  (VOID_TYPE_P (ttr)
7129 		   && !null_pointer_constant
7130 		   && TREE_CODE (ttl) == FUNCTION_TYPE)))
7131 	    PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7132 				    G_("ISO C forbids passing argument %d of "
7133 				       "%qE between function pointer "
7134 				       "and %<void *%>"),
7135 				    G_("ISO C forbids assignment between "
7136 				       "function pointer and %<void *%>"),
7137 				    G_("ISO C forbids initialization between "
7138 				       "function pointer and %<void *%>"),
7139 				    G_("ISO C forbids return between function "
7140 				       "pointer and %<void *%>"));
7141 	  /* Const and volatile mean something different for function types,
7142 	     so the usual warnings are not appropriate.  */
7143 	  else if (TREE_CODE (ttr) != FUNCTION_TYPE
7144 		   && TREE_CODE (ttl) != FUNCTION_TYPE)
7145 	    {
7146 	      /* Don't warn about loss of qualifier for conversions from
7147 		 qualified void* to pointers to arrays with corresponding
7148 		 qualifier on the element type. */
7149 	      if (!pedantic)
7150 	        ttl = strip_array_types (ttl);
7151 
7152 	      /* Assignments between atomic and non-atomic objects are OK.  */
7153 	      if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7154 		  & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7155 		{
7156 		  PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7157 				          OPT_Wdiscarded_qualifiers,
7158 				          G_("passing argument %d of %qE discards "
7159 					     "%qv qualifier from pointer target type"),
7160 				          G_("assignment discards %qv qualifier "
7161 					     "from pointer target type"),
7162 				          G_("initialization discards %qv qualifier "
7163 					     "from pointer target type"),
7164 				          G_("return discards %qv qualifier from "
7165 					     "pointer target type"),
7166 				          TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7167 		}
7168 	      /* If this is not a case of ignoring a mismatch in signedness,
7169 		 no warning.  */
7170 	      else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7171 		       || target_cmp)
7172 		;
7173 	      /* If there is a mismatch, do warn.  */
7174 	      else if (warn_pointer_sign)
7175 		switch (errtype)
7176 		  {
7177 		  case ic_argpass:
7178 		    {
7179 		      auto_diagnostic_group d;
7180 		      range_label_for_type_mismatch rhs_label (rhstype, type);
7181 		      gcc_rich_location richloc (expr_loc, &rhs_label);
7182 		      if (pedwarn (&richloc, OPT_Wpointer_sign,
7183 				   "pointer targets in passing argument %d of "
7184 				   "%qE differ in signedness", parmnum, rname))
7185 			inform_for_arg (fundecl, expr_loc, parmnum, type,
7186 					rhstype);
7187 		    }
7188 		    break;
7189 		  case ic_assign:
7190 		    pedwarn (location, OPT_Wpointer_sign,
7191 			     "pointer targets in assignment from %qT to %qT "
7192 			     "differ in signedness", rhstype, type);
7193 		    break;
7194 		  case ic_init:
7195 		    pedwarn_init (location, OPT_Wpointer_sign,
7196 				  "pointer targets in initialization of %qT "
7197 				  "from %qT differ in signedness", type,
7198 				  rhstype);
7199 		    break;
7200 		  case ic_return:
7201 		    pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7202 			     "returning %qT from a function with return type "
7203 			     "%qT differ in signedness", rhstype, type);
7204 		    break;
7205 		  default:
7206 		    gcc_unreachable ();
7207 		  }
7208 	    }
7209 	  else if (TREE_CODE (ttl) == FUNCTION_TYPE
7210 		   && TREE_CODE (ttr) == FUNCTION_TYPE)
7211 	    {
7212 	      /* Because const and volatile on functions are restrictions
7213 		 that say the function will not do certain things,
7214 		 it is okay to use a const or volatile function
7215 		 where an ordinary one is wanted, but not vice-versa.  */
7216 	      if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7217 		  & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7218 		PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7219 				        OPT_Wdiscarded_qualifiers,
7220 				        G_("passing argument %d of %qE makes "
7221 					   "%q#v qualified function pointer "
7222 					   "from unqualified"),
7223 				        G_("assignment makes %q#v qualified function "
7224 					   "pointer from unqualified"),
7225 				        G_("initialization makes %q#v qualified "
7226 					   "function pointer from unqualified"),
7227 				        G_("return makes %q#v qualified function "
7228 					   "pointer from unqualified"),
7229 				        TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7230 	    }
7231 	}
7232       /* Avoid warning about the volatile ObjC EH puts on decls.  */
7233       else if (!objc_ok)
7234 	{
7235 	  switch (errtype)
7236 	    {
7237 	    case ic_argpass:
7238 	      {
7239 		auto_diagnostic_group d;
7240 		range_label_for_type_mismatch rhs_label (rhstype, type);
7241 		gcc_rich_location richloc (expr_loc, &rhs_label);
7242 		if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7243 			     "passing argument %d of %qE from incompatible "
7244 			     "pointer type", parmnum, rname))
7245 		  inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7246 	      }
7247 	      break;
7248 	    case ic_assign:
7249 	      if (bltin)
7250 		pedwarn (location, OPT_Wincompatible_pointer_types,
7251 			 "assignment to %qT from pointer to "
7252 			 "%qD with incompatible type %qT",
7253 			 type, bltin, rhstype);
7254 	      else
7255 		pedwarn (location, OPT_Wincompatible_pointer_types,
7256 			 "assignment to %qT from incompatible pointer type %qT",
7257 			 type, rhstype);
7258 	      break;
7259 	    case ic_init:
7260 	      if (bltin)
7261 		pedwarn_init (location, OPT_Wincompatible_pointer_types,
7262 			      "initialization of %qT from pointer to "
7263 			      "%qD with incompatible type %qT",
7264 			      type, bltin, rhstype);
7265 	      else
7266 		pedwarn_init (location, OPT_Wincompatible_pointer_types,
7267 			      "initialization of %qT from incompatible "
7268 			      "pointer type %qT",
7269 			      type, rhstype);
7270 	      break;
7271 	    case ic_return:
7272 	      if (bltin)
7273 		pedwarn (location, OPT_Wincompatible_pointer_types,
7274 			 "returning pointer to %qD of type %qT from "
7275 			 "a function with incompatible type %qT",
7276 			 bltin, rhstype, type);
7277 	      else
7278 		pedwarn (location, OPT_Wincompatible_pointer_types,
7279 			 "returning %qT from a function with incompatible "
7280 			 "return type %qT", rhstype, type);
7281 	      break;
7282 	    default:
7283 	      gcc_unreachable ();
7284 	    }
7285 	}
7286 
7287       /* If RHS isn't an address, check pointer or array of packed
7288 	 struct or union.  */
7289       warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7290 
7291       return convert (type, rhs);
7292     }
7293   else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7294     {
7295       /* ??? This should not be an error when inlining calls to
7296 	 unprototyped functions.  */
7297       const char msg[] = "invalid use of non-lvalue array";
7298       if (warnopt)
7299 	warning_at (location, warnopt, msg);
7300       else
7301 	error_at (location, msg);
7302       return error_mark_node;
7303     }
7304   else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7305     {
7306       /* An explicit constant 0 can convert to a pointer,
7307 	 or one that results from arithmetic, even including
7308 	 a cast to integer type.  */
7309       if (!null_pointer_constant)
7310 	switch (errtype)
7311 	  {
7312 	  case ic_argpass:
7313 	    {
7314 	      auto_diagnostic_group d;
7315 	      range_label_for_type_mismatch rhs_label (rhstype, type);
7316 	      gcc_rich_location richloc (expr_loc, &rhs_label);
7317 	      if (pedwarn (&richloc, OPT_Wint_conversion,
7318 			   "passing argument %d of %qE makes pointer from "
7319 			   "integer without a cast", parmnum, rname))
7320 		inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7321 	    }
7322 	    break;
7323 	  case ic_assign:
7324 	    pedwarn (location, OPT_Wint_conversion,
7325 		     "assignment to %qT from %qT makes pointer from integer "
7326 		     "without a cast", type, rhstype);
7327 	    break;
7328 	  case ic_init:
7329 	    pedwarn_init (location, OPT_Wint_conversion,
7330 			  "initialization of %qT from %qT makes pointer from "
7331 			  "integer without a cast", type, rhstype);
7332 	    break;
7333 	  case ic_return:
7334 	    pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7335 		     "function with return type %qT makes pointer from "
7336 		     "integer without a cast", rhstype, type);
7337 	    break;
7338 	  default:
7339 	    gcc_unreachable ();
7340 	  }
7341 
7342       return convert (type, rhs);
7343     }
7344   else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7345     {
7346       switch (errtype)
7347 	{
7348 	case ic_argpass:
7349 	  {
7350 	    auto_diagnostic_group d;
7351 	    range_label_for_type_mismatch rhs_label (rhstype, type);
7352 	    gcc_rich_location richloc (expr_loc, &rhs_label);
7353 	    if (pedwarn (&richloc, OPT_Wint_conversion,
7354 			 "passing argument %d of %qE makes integer from "
7355 			 "pointer without a cast", parmnum, rname))
7356 	      inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7357 	  }
7358 	  break;
7359 	case ic_assign:
7360 	  pedwarn (location, OPT_Wint_conversion,
7361 		   "assignment to %qT from %qT makes integer from pointer "
7362 		   "without a cast", type, rhstype);
7363 	  break;
7364 	case ic_init:
7365 	  pedwarn_init (location, OPT_Wint_conversion,
7366 			"initialization of %qT from %qT makes integer from "
7367 			"pointer without a cast", type, rhstype);
7368 	  break;
7369 	case ic_return:
7370 	  pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7371 		   "function with return type %qT makes integer from "
7372 		   "pointer without a cast", rhstype, type);
7373 	  break;
7374 	default:
7375 	  gcc_unreachable ();
7376 	}
7377 
7378       return convert (type, rhs);
7379     }
7380   else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7381     {
7382       tree ret;
7383       bool save = in_late_binary_op;
7384       in_late_binary_op = true;
7385       ret = convert (type, rhs);
7386       in_late_binary_op = save;
7387       return ret;
7388     }
7389 
7390   switch (errtype)
7391     {
7392     case ic_argpass:
7393       {
7394 	auto_diagnostic_group d;
7395 	range_label_for_type_mismatch rhs_label (rhstype, type);
7396 	gcc_rich_location richloc (expr_loc, &rhs_label);
7397 	const char msg[] = G_("incompatible type for argument %d of %qE");
7398 	if (warnopt)
7399 	  warning_at (expr_loc, warnopt, msg, parmnum, rname);
7400 	else
7401 	  error_at (&richloc, msg, parmnum, rname);
7402 	inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7403       }
7404       break;
7405     case ic_assign:
7406       {
7407 	const char msg[]
7408 	  = G_("incompatible types when assigning to type %qT from type %qT");
7409 	if (warnopt)
7410 	  warning_at (expr_loc, 0, msg, type, rhstype);
7411 	else
7412 	  error_at (expr_loc, msg, type, rhstype);
7413 	break;
7414       }
7415     case ic_init:
7416       {
7417 	const char msg[]
7418 	  = G_("incompatible types when initializing type %qT using type %qT");
7419 	if (warnopt)
7420 	  warning_at (location, 0, msg, type, rhstype);
7421 	else
7422 	  error_at (location, msg, type, rhstype);
7423 	break;
7424       }
7425     case ic_return:
7426       {
7427 	const char msg[]
7428 	  = G_("incompatible types when returning type %qT but %qT was expected");
7429 	if (warnopt)
7430 	  warning_at (location, 0, msg, rhstype, type);
7431 	else
7432 	  error_at (location, msg, rhstype, type);
7433 	break;
7434       }
7435     default:
7436       gcc_unreachable ();
7437     }
7438 
7439   return error_mark_node;
7440 }
7441 
7442 /* If VALUE is a compound expr all of whose expressions are constant, then
7443    return its value.  Otherwise, return error_mark_node.
7444 
7445    This is for handling COMPOUND_EXPRs as initializer elements
7446    which is allowed with a warning when -pedantic is specified.  */
7447 
7448 static tree
valid_compound_expr_initializer(tree value,tree endtype)7449 valid_compound_expr_initializer (tree value, tree endtype)
7450 {
7451   if (TREE_CODE (value) == COMPOUND_EXPR)
7452     {
7453       if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7454 	  == error_mark_node)
7455 	return error_mark_node;
7456       return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7457 					      endtype);
7458     }
7459   else if (!initializer_constant_valid_p (value, endtype))
7460     return error_mark_node;
7461   else
7462     return value;
7463 }
7464 
7465 /* Perform appropriate conversions on the initial value of a variable,
7466    store it in the declaration DECL,
7467    and print any error messages that are appropriate.
7468    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7469    If the init is invalid, store an ERROR_MARK.
7470 
7471    INIT_LOC is the location of the initial value.  */
7472 
7473 void
store_init_value(location_t init_loc,tree decl,tree init,tree origtype)7474 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7475 {
7476   tree value, type;
7477   bool npc = false;
7478 
7479   /* If variable's type was invalidly declared, just ignore it.  */
7480 
7481   type = TREE_TYPE (decl);
7482   if (TREE_CODE (type) == ERROR_MARK)
7483     return;
7484 
7485   /* Digest the specified initializer into an expression.  */
7486 
7487   if (init)
7488     npc = null_pointer_constant_p (init);
7489   value = digest_init (init_loc, type, init, origtype, npc,
7490       		       true, TREE_STATIC (decl));
7491 
7492   /* Store the expression if valid; else report error.  */
7493 
7494   if (!in_system_header_at (input_location)
7495       && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7496     warning (OPT_Wtraditional, "traditional C rejects automatic "
7497 	     "aggregate initialization");
7498 
7499   if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7500     DECL_INITIAL (decl) = value;
7501 
7502   /* ANSI wants warnings about out-of-range constant initializers.  */
7503   STRIP_TYPE_NOPS (value);
7504   if (TREE_STATIC (decl))
7505     constant_expression_warning (value);
7506 
7507   /* Check if we need to set array size from compound literal size.  */
7508   if (TREE_CODE (type) == ARRAY_TYPE
7509       && TYPE_DOMAIN (type) == NULL_TREE
7510       && value != error_mark_node)
7511     {
7512       tree inside_init = init;
7513 
7514       STRIP_TYPE_NOPS (inside_init);
7515       inside_init = fold (inside_init);
7516 
7517       if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7518 	{
7519 	  tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7520 
7521 	  if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7522 	    {
7523 	      /* For int foo[] = (int [3]){1}; we need to set array size
7524 		 now since later on array initializer will be just the
7525 		 brace enclosed list of the compound literal.  */
7526 	      tree etype = strip_array_types (TREE_TYPE (decl));
7527 	      type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7528 	      TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7529 	      layout_type (type);
7530 	      layout_decl (cldecl, 0);
7531 	      TREE_TYPE (decl)
7532 		= c_build_qualified_type (type, TYPE_QUALS (etype));
7533 	    }
7534 	}
7535     }
7536 }
7537 
7538 /* Methods for storing and printing names for error messages.  */
7539 
7540 /* Implement a spelling stack that allows components of a name to be pushed
7541    and popped.  Each element on the stack is this structure.  */
7542 
7543 struct spelling
7544 {
7545   int kind;
7546   union
7547     {
7548       unsigned HOST_WIDE_INT i;
7549       const char *s;
7550     } u;
7551 };
7552 
7553 #define SPELLING_STRING 1
7554 #define SPELLING_MEMBER 2
7555 #define SPELLING_BOUNDS 3
7556 
7557 static struct spelling *spelling;	/* Next stack element (unused).  */
7558 static struct spelling *spelling_base;	/* Spelling stack base.  */
7559 static int spelling_size;		/* Size of the spelling stack.  */
7560 
7561 /* Macros to save and restore the spelling stack around push_... functions.
7562    Alternative to SAVE_SPELLING_STACK.  */
7563 
7564 #define SPELLING_DEPTH() (spelling - spelling_base)
7565 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7566 
7567 /* Push an element on the spelling stack with type KIND and assign VALUE
7568    to MEMBER.  */
7569 
7570 #define PUSH_SPELLING(KIND, VALUE, MEMBER)				\
7571 {									\
7572   int depth = SPELLING_DEPTH ();					\
7573 									\
7574   if (depth >= spelling_size)						\
7575     {									\
7576       spelling_size += 10;						\
7577       spelling_base = XRESIZEVEC (struct spelling, spelling_base,	\
7578 				  spelling_size);			\
7579       RESTORE_SPELLING_DEPTH (depth);					\
7580     }									\
7581 									\
7582   spelling->kind = (KIND);						\
7583   spelling->MEMBER = (VALUE);						\
7584   spelling++;								\
7585 }
7586 
7587 /* Push STRING on the stack.  Printed literally.  */
7588 
7589 static void
push_string(const char * string)7590 push_string (const char *string)
7591 {
7592   PUSH_SPELLING (SPELLING_STRING, string, u.s);
7593 }
7594 
7595 /* Push a member name on the stack.  Printed as '.' STRING.  */
7596 
7597 static void
push_member_name(tree decl)7598 push_member_name (tree decl)
7599 {
7600   const char *const string
7601     = (DECL_NAME (decl)
7602        ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7603        : _("<anonymous>"));
7604   PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7605 }
7606 
7607 /* Push an array bounds on the stack.  Printed as [BOUNDS].  */
7608 
7609 static void
push_array_bounds(unsigned HOST_WIDE_INT bounds)7610 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7611 {
7612   PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7613 }
7614 
7615 /* Compute the maximum size in bytes of the printed spelling.  */
7616 
7617 static int
spelling_length(void)7618 spelling_length (void)
7619 {
7620   int size = 0;
7621   struct spelling *p;
7622 
7623   for (p = spelling_base; p < spelling; p++)
7624     {
7625       if (p->kind == SPELLING_BOUNDS)
7626 	size += 25;
7627       else
7628 	size += strlen (p->u.s) + 1;
7629     }
7630 
7631   return size;
7632 }
7633 
7634 /* Print the spelling to BUFFER and return it.  */
7635 
7636 static char *
print_spelling(char * buffer)7637 print_spelling (char *buffer)
7638 {
7639   char *d = buffer;
7640   struct spelling *p;
7641 
7642   for (p = spelling_base; p < spelling; p++)
7643     if (p->kind == SPELLING_BOUNDS)
7644       {
7645 	sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7646 	d += strlen (d);
7647       }
7648     else
7649       {
7650 	const char *s;
7651 	if (p->kind == SPELLING_MEMBER)
7652 	  *d++ = '.';
7653 	for (s = p->u.s; (*d = *s++); d++)
7654 	  ;
7655       }
7656   *d++ = '\0';
7657   return buffer;
7658 }
7659 
7660 /* Digest the parser output INIT as an initializer for type TYPE.
7661    Return a C expression of type TYPE to represent the initial value.
7662 
7663    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7664 
7665    NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7666 
7667    If INIT is a string constant, STRICT_STRING is true if it is
7668    unparenthesized or we should not warn here for it being parenthesized.
7669    For other types of INIT, STRICT_STRING is not used.
7670 
7671    INIT_LOC is the location of the INIT.
7672 
7673    REQUIRE_CONSTANT requests an error if non-constant initializers or
7674    elements are seen.  */
7675 
7676 static tree
digest_init(location_t init_loc,tree type,tree init,tree origtype,bool null_pointer_constant,bool strict_string,int require_constant)7677 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7678     	     bool null_pointer_constant, bool strict_string,
7679 	     int require_constant)
7680 {
7681   enum tree_code code = TREE_CODE (type);
7682   tree inside_init = init;
7683   tree semantic_type = NULL_TREE;
7684   bool maybe_const = true;
7685 
7686   if (type == error_mark_node
7687       || !init
7688       || error_operand_p (init))
7689     return error_mark_node;
7690 
7691   STRIP_TYPE_NOPS (inside_init);
7692 
7693   if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7694     {
7695       semantic_type = TREE_TYPE (inside_init);
7696       inside_init = TREE_OPERAND (inside_init, 0);
7697     }
7698   inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7699 
7700   /* Initialization of an array of chars from a string constant
7701      optionally enclosed in braces.  */
7702 
7703   if (code == ARRAY_TYPE && inside_init
7704       && TREE_CODE (inside_init) == STRING_CST)
7705     {
7706       tree typ1
7707 	= (TYPE_ATOMIC (TREE_TYPE (type))
7708 	   ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7709 				     TYPE_QUAL_ATOMIC)
7710 	   : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7711       /* Note that an array could be both an array of character type
7712 	 and an array of wchar_t if wchar_t is signed char or unsigned
7713 	 char.  */
7714       bool char_array = (typ1 == char_type_node
7715 			 || typ1 == signed_char_type_node
7716 			 || typ1 == unsigned_char_type_node);
7717       bool wchar_array = !!comptypes (typ1, wchar_type_node);
7718       bool char16_array = !!comptypes (typ1, char16_type_node);
7719       bool char32_array = !!comptypes (typ1, char32_type_node);
7720 
7721       if (char_array || wchar_array || char16_array || char32_array)
7722 	{
7723 	  struct c_expr expr;
7724 	  tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7725 	  bool incompat_string_cst = false;
7726 	  expr.value = inside_init;
7727 	  expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7728 	  expr.original_type = NULL;
7729 	  maybe_warn_string_init (init_loc, type, expr);
7730 
7731 	  if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7732 	    pedwarn_init (init_loc, OPT_Wpedantic,
7733 			  "initialization of a flexible array member");
7734 
7735 	  if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7736 			 TYPE_MAIN_VARIANT (type)))
7737 	    return inside_init;
7738 
7739 	  if (char_array)
7740 	    {
7741 	      if (typ2 != char_type_node)
7742 		incompat_string_cst = true;
7743 	    }
7744 	  else if (!comptypes (typ1, typ2))
7745 	    incompat_string_cst = true;
7746 
7747           if (incompat_string_cst)
7748             {
7749 	      error_init (init_loc, "cannot initialize array of %qT from "
7750 			  "a string literal with type array of %qT",
7751 			  typ1, typ2);
7752 	      return error_mark_node;
7753             }
7754 
7755 	  if (TYPE_DOMAIN (type) != NULL_TREE
7756 	      && TYPE_SIZE (type) != NULL_TREE
7757 	      && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7758 	    {
7759 	      unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7760 	      unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
7761 
7762 	      /* Subtract the size of a single (possibly wide) character
7763 		 because it's ok to ignore the terminating null char
7764 		 that is counted in the length of the constant.  */
7765 	      if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
7766 		pedwarn_init (init_loc, 0,
7767 			      ("initializer-string for array of chars "
7768 			       "is too long"));
7769 	      else if (warn_cxx_compat
7770 		       && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7771 		warning_at (init_loc, OPT_Wc___compat,
7772 			    ("initializer-string for array chars "
7773 			     "is too long for C++"));
7774 	      if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7775 		{
7776 		  unsigned HOST_WIDE_INT size
7777 		    = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7778 		  const char *p = TREE_STRING_POINTER (inside_init);
7779 
7780 		  inside_init = build_string (size, p);
7781 		}
7782 	    }
7783 
7784 	  TREE_TYPE (inside_init) = type;
7785 	  return inside_init;
7786 	}
7787       else if (INTEGRAL_TYPE_P (typ1))
7788 	{
7789 	  error_init (init_loc, "array of inappropriate type initialized "
7790 		      "from string constant");
7791 	  return error_mark_node;
7792 	}
7793     }
7794 
7795   /* Build a VECTOR_CST from a *constant* vector constructor.  If the
7796      vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7797      below and handle as a constructor.  */
7798   if (code == VECTOR_TYPE
7799       && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7800       && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7801       && TREE_CONSTANT (inside_init))
7802     {
7803       if (TREE_CODE (inside_init) == VECTOR_CST
7804 	  && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7805 			TYPE_MAIN_VARIANT (type)))
7806 	return inside_init;
7807 
7808       if (TREE_CODE (inside_init) == CONSTRUCTOR)
7809 	{
7810 	  unsigned HOST_WIDE_INT ix;
7811 	  tree value;
7812 	  bool constant_p = true;
7813 
7814 	  /* Iterate through elements and check if all constructor
7815 	     elements are *_CSTs.  */
7816 	  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7817 	    if (!CONSTANT_CLASS_P (value))
7818 	      {
7819 		constant_p = false;
7820 		break;
7821 	      }
7822 
7823 	  if (constant_p)
7824 	    return build_vector_from_ctor (type,
7825 					   CONSTRUCTOR_ELTS (inside_init));
7826 	}
7827     }
7828 
7829   if (warn_sequence_point)
7830     verify_sequence_points (inside_init);
7831 
7832   /* Any type can be initialized
7833      from an expression of the same type, optionally with braces.  */
7834 
7835   if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7836       && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7837 		     TYPE_MAIN_VARIANT (type))
7838 	  || (code == ARRAY_TYPE
7839 	      && comptypes (TREE_TYPE (inside_init), type))
7840 	  || (code == VECTOR_TYPE
7841 	      && comptypes (TREE_TYPE (inside_init), type))
7842 	  || (code == POINTER_TYPE
7843 	      && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7844 	      && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7845 			    TREE_TYPE (type)))))
7846     {
7847       if (code == POINTER_TYPE)
7848 	{
7849 	  if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7850 	    {
7851 	      if (TREE_CODE (inside_init) == STRING_CST
7852 		  || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7853 		inside_init = array_to_pointer_conversion
7854 		  (init_loc, inside_init);
7855 	      else
7856 		{
7857 		  error_init (init_loc, "invalid use of non-lvalue array");
7858 		  return error_mark_node;
7859 		}
7860 	    }
7861 	}
7862 
7863       if (code == VECTOR_TYPE)
7864 	/* Although the types are compatible, we may require a
7865 	   conversion.  */
7866 	inside_init = convert (type, inside_init);
7867 
7868       if (require_constant
7869 	  && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7870 	{
7871 	  /* As an extension, allow initializing objects with static storage
7872 	     duration with compound literals (which are then treated just as
7873 	     the brace enclosed list they contain).  Also allow this for
7874 	     vectors, as we can only assign them with compound literals.  */
7875 	  if (flag_isoc99 && code != VECTOR_TYPE)
7876 	    pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7877 			  "is not constant");
7878 	  tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7879 	  inside_init = DECL_INITIAL (decl);
7880 	}
7881 
7882       if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7883 	  && TREE_CODE (inside_init) != CONSTRUCTOR)
7884 	{
7885 	  error_init (init_loc, "array initialized from non-constant array "
7886 		      "expression");
7887 	  return error_mark_node;
7888 	}
7889 
7890       /* Compound expressions can only occur here if -Wpedantic or
7891 	 -pedantic-errors is specified.  In the later case, we always want
7892 	 an error.  In the former case, we simply want a warning.  */
7893       if (require_constant && pedantic
7894 	  && TREE_CODE (inside_init) == COMPOUND_EXPR)
7895 	{
7896 	  inside_init
7897 	    = valid_compound_expr_initializer (inside_init,
7898 					       TREE_TYPE (inside_init));
7899 	  if (inside_init == error_mark_node)
7900 	    error_init (init_loc, "initializer element is not constant");
7901 	  else
7902 	    pedwarn_init (init_loc, OPT_Wpedantic,
7903 			  "initializer element is not constant");
7904 	  if (flag_pedantic_errors)
7905 	    inside_init = error_mark_node;
7906 	}
7907       else if (require_constant
7908 	       && !initializer_constant_valid_p (inside_init,
7909 						 TREE_TYPE (inside_init)))
7910 	{
7911 	  error_init (init_loc, "initializer element is not constant");
7912 	  inside_init = error_mark_node;
7913 	}
7914       else if (require_constant && !maybe_const)
7915 	pedwarn_init (init_loc, OPT_Wpedantic,
7916 		      "initializer element is not a constant expression");
7917 
7918       /* Added to enable additional -Wsuggest-attribute=format warnings.  */
7919       if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
7920 	inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7921 					      type, inside_init, origtype,
7922 					      ic_init, null_pointer_constant,
7923 					      NULL_TREE, NULL_TREE, 0);
7924       return inside_init;
7925     }
7926 
7927   /* Handle scalar types, including conversions.  */
7928 
7929   if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7930       || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7931       || code == COMPLEX_TYPE || code == VECTOR_TYPE)
7932     {
7933       if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7934 	  && (TREE_CODE (init) == STRING_CST
7935 	      || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
7936 	inside_init = init = array_to_pointer_conversion (init_loc, init);
7937       if (semantic_type)
7938 	inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7939 			      inside_init);
7940       inside_init
7941 	= convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7942 				  inside_init, origtype, ic_init,
7943 				  null_pointer_constant, NULL_TREE, NULL_TREE,
7944 				  0);
7945 
7946       /* Check to see if we have already given an error message.  */
7947       if (inside_init == error_mark_node)
7948 	;
7949       else if (require_constant && !TREE_CONSTANT (inside_init))
7950 	{
7951 	  error_init (init_loc, "initializer element is not constant");
7952 	  inside_init = error_mark_node;
7953 	}
7954       else if (require_constant
7955 	       && !initializer_constant_valid_p (inside_init,
7956 						 TREE_TYPE (inside_init)))
7957 	{
7958 	  error_init (init_loc, "initializer element is not computable at "
7959 		      "load time");
7960 	  inside_init = error_mark_node;
7961 	}
7962       else if (require_constant && !maybe_const)
7963 	pedwarn_init (init_loc, OPT_Wpedantic,
7964 		      "initializer element is not a constant expression");
7965 
7966       return inside_init;
7967     }
7968 
7969   /* Come here only for records and arrays.  */
7970 
7971   if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
7972     {
7973       error_init (init_loc, "variable-sized object may not be initialized");
7974       return error_mark_node;
7975     }
7976 
7977   error_init (init_loc, "invalid initializer");
7978   return error_mark_node;
7979 }
7980 
7981 /* Handle initializers that use braces.  */
7982 
7983 /* Type of object we are accumulating a constructor for.
7984    This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE.  */
7985 static tree constructor_type;
7986 
7987 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7988    left to fill.  */
7989 static tree constructor_fields;
7990 
7991 /* For an ARRAY_TYPE, this is the specified index
7992    at which to store the next element we get.  */
7993 static tree constructor_index;
7994 
7995 /* For an ARRAY_TYPE, this is the maximum index.  */
7996 static tree constructor_max_index;
7997 
7998 /* For a RECORD_TYPE, this is the first field not yet written out.  */
7999 static tree constructor_unfilled_fields;
8000 
8001 /* For an ARRAY_TYPE, this is the index of the first element
8002    not yet written out.  */
8003 static tree constructor_unfilled_index;
8004 
8005 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8006    This is so we can generate gaps between fields, when appropriate.  */
8007 static tree constructor_bit_index;
8008 
8009 /* If we are saving up the elements rather than allocating them,
8010    this is the list of elements so far (in reverse order,
8011    most recent first).  */
8012 static vec<constructor_elt, va_gc> *constructor_elements;
8013 
8014 /* 1 if constructor should be incrementally stored into a constructor chain,
8015    0 if all the elements should be kept in AVL tree.  */
8016 static int constructor_incremental;
8017 
8018 /* 1 if so far this constructor's elements are all compile-time constants.  */
8019 static int constructor_constant;
8020 
8021 /* 1 if so far this constructor's elements are all valid address constants.  */
8022 static int constructor_simple;
8023 
8024 /* 1 if this constructor has an element that cannot be part of a
8025    constant expression.  */
8026 static int constructor_nonconst;
8027 
8028 /* 1 if this constructor is erroneous so far.  */
8029 static int constructor_erroneous;
8030 
8031 /* 1 if this constructor is the universal zero initializer { 0 }.  */
8032 static int constructor_zeroinit;
8033 
8034 /* Structure for managing pending initializer elements, organized as an
8035    AVL tree.  */
8036 
8037 struct init_node
8038 {
8039   struct init_node *left, *right;
8040   struct init_node *parent;
8041   int balance;
8042   tree purpose;
8043   tree value;
8044   tree origtype;
8045 };
8046 
8047 /* Tree of pending elements at this constructor level.
8048    These are elements encountered out of order
8049    which belong at places we haven't reached yet in actually
8050    writing the output.
8051    Will never hold tree nodes across GC runs.  */
8052 static struct init_node *constructor_pending_elts;
8053 
8054 /* The SPELLING_DEPTH of this constructor.  */
8055 static int constructor_depth;
8056 
8057 /* DECL node for which an initializer is being read.
8058    0 means we are reading a constructor expression
8059    such as (struct foo) {...}.  */
8060 static tree constructor_decl;
8061 
8062 /* Nonzero if this is an initializer for a top-level decl.  */
8063 static int constructor_top_level;
8064 
8065 /* Nonzero if there were any member designators in this initializer.  */
8066 static int constructor_designated;
8067 
8068 /* Nesting depth of designator list.  */
8069 static int designator_depth;
8070 
8071 /* Nonzero if there were diagnosed errors in this designator list.  */
8072 static int designator_erroneous;
8073 
8074 
8075 /* This stack has a level for each implicit or explicit level of
8076    structuring in the initializer, including the outermost one.  It
8077    saves the values of most of the variables above.  */
8078 
8079 struct constructor_range_stack;
8080 
8081 struct constructor_stack
8082 {
8083   struct constructor_stack *next;
8084   tree type;
8085   tree fields;
8086   tree index;
8087   tree max_index;
8088   tree unfilled_index;
8089   tree unfilled_fields;
8090   tree bit_index;
8091   vec<constructor_elt, va_gc> *elements;
8092   struct init_node *pending_elts;
8093   int offset;
8094   int depth;
8095   /* If value nonzero, this value should replace the entire
8096      constructor at this level.  */
8097   struct c_expr replacement_value;
8098   struct constructor_range_stack *range_stack;
8099   char constant;
8100   char simple;
8101   char nonconst;
8102   char implicit;
8103   char erroneous;
8104   char outer;
8105   char incremental;
8106   char designated;
8107   int designator_depth;
8108 };
8109 
8110 static struct constructor_stack *constructor_stack;
8111 
8112 /* This stack represents designators from some range designator up to
8113    the last designator in the list.  */
8114 
8115 struct constructor_range_stack
8116 {
8117   struct constructor_range_stack *next, *prev;
8118   struct constructor_stack *stack;
8119   tree range_start;
8120   tree index;
8121   tree range_end;
8122   tree fields;
8123 };
8124 
8125 static struct constructor_range_stack *constructor_range_stack;
8126 
8127 /* This stack records separate initializers that are nested.
8128    Nested initializers can't happen in ANSI C, but GNU C allows them
8129    in cases like { ... (struct foo) { ... } ... }.  */
8130 
8131 struct initializer_stack
8132 {
8133   struct initializer_stack *next;
8134   tree decl;
8135   struct constructor_stack *constructor_stack;
8136   struct constructor_range_stack *constructor_range_stack;
8137   vec<constructor_elt, va_gc> *elements;
8138   struct spelling *spelling;
8139   struct spelling *spelling_base;
8140   int spelling_size;
8141   char top_level;
8142   char require_constant_value;
8143   char require_constant_elements;
8144   rich_location *missing_brace_richloc;
8145 };
8146 
8147 static struct initializer_stack *initializer_stack;
8148 
8149 /* Prepare to parse and output the initializer for variable DECL.  */
8150 
8151 void
start_init(tree decl,tree asmspec_tree ATTRIBUTE_UNUSED,int top_level,rich_location * richloc)8152 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8153 	    rich_location *richloc)
8154 {
8155   const char *locus;
8156   struct initializer_stack *p = XNEW (struct initializer_stack);
8157 
8158   p->decl = constructor_decl;
8159   p->require_constant_value = require_constant_value;
8160   p->require_constant_elements = require_constant_elements;
8161   p->constructor_stack = constructor_stack;
8162   p->constructor_range_stack = constructor_range_stack;
8163   p->elements = constructor_elements;
8164   p->spelling = spelling;
8165   p->spelling_base = spelling_base;
8166   p->spelling_size = spelling_size;
8167   p->top_level = constructor_top_level;
8168   p->next = initializer_stack;
8169   p->missing_brace_richloc = richloc;
8170   initializer_stack = p;
8171 
8172   constructor_decl = decl;
8173   constructor_designated = 0;
8174   constructor_top_level = top_level;
8175 
8176   if (decl != NULL_TREE && decl != error_mark_node)
8177     {
8178       require_constant_value = TREE_STATIC (decl);
8179       require_constant_elements
8180 	= ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8181 	   /* For a scalar, you can always use any value to initialize,
8182 	      even within braces.  */
8183 	   && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8184       locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8185     }
8186   else
8187     {
8188       require_constant_value = 0;
8189       require_constant_elements = 0;
8190       locus = _("(anonymous)");
8191     }
8192 
8193   constructor_stack = 0;
8194   constructor_range_stack = 0;
8195 
8196   found_missing_braces = 0;
8197 
8198   spelling_base = 0;
8199   spelling_size = 0;
8200   RESTORE_SPELLING_DEPTH (0);
8201 
8202   if (locus)
8203     push_string (locus);
8204 }
8205 
8206 void
finish_init(void)8207 finish_init (void)
8208 {
8209   struct initializer_stack *p = initializer_stack;
8210 
8211   /* Free the whole constructor stack of this initializer.  */
8212   while (constructor_stack)
8213     {
8214       struct constructor_stack *q = constructor_stack;
8215       constructor_stack = q->next;
8216       free (q);
8217     }
8218 
8219   gcc_assert (!constructor_range_stack);
8220 
8221   /* Pop back to the data of the outer initializer (if any).  */
8222   free (spelling_base);
8223 
8224   constructor_decl = p->decl;
8225   require_constant_value = p->require_constant_value;
8226   require_constant_elements = p->require_constant_elements;
8227   constructor_stack = p->constructor_stack;
8228   constructor_range_stack = p->constructor_range_stack;
8229   constructor_elements = p->elements;
8230   spelling = p->spelling;
8231   spelling_base = p->spelling_base;
8232   spelling_size = p->spelling_size;
8233   constructor_top_level = p->top_level;
8234   initializer_stack = p->next;
8235   free (p);
8236 }
8237 
8238 /* Call here when we see the initializer is surrounded by braces.
8239    This is instead of a call to push_init_level;
8240    it is matched by a call to pop_init_level.
8241 
8242    TYPE is the type to initialize, for a constructor expression.
8243    For an initializer for a decl, TYPE is zero.  */
8244 
8245 void
really_start_incremental_init(tree type)8246 really_start_incremental_init (tree type)
8247 {
8248   struct constructor_stack *p = XNEW (struct constructor_stack);
8249 
8250   if (type == NULL_TREE)
8251     type = TREE_TYPE (constructor_decl);
8252 
8253   if (VECTOR_TYPE_P (type)
8254       && TYPE_VECTOR_OPAQUE (type))
8255     error ("opaque vector types cannot be initialized");
8256 
8257   p->type = constructor_type;
8258   p->fields = constructor_fields;
8259   p->index = constructor_index;
8260   p->max_index = constructor_max_index;
8261   p->unfilled_index = constructor_unfilled_index;
8262   p->unfilled_fields = constructor_unfilled_fields;
8263   p->bit_index = constructor_bit_index;
8264   p->elements = constructor_elements;
8265   p->constant = constructor_constant;
8266   p->simple = constructor_simple;
8267   p->nonconst = constructor_nonconst;
8268   p->erroneous = constructor_erroneous;
8269   p->pending_elts = constructor_pending_elts;
8270   p->depth = constructor_depth;
8271   p->replacement_value.value = 0;
8272   p->replacement_value.original_code = ERROR_MARK;
8273   p->replacement_value.original_type = NULL;
8274   p->implicit = 0;
8275   p->range_stack = 0;
8276   p->outer = 0;
8277   p->incremental = constructor_incremental;
8278   p->designated = constructor_designated;
8279   p->designator_depth = designator_depth;
8280   p->next = 0;
8281   constructor_stack = p;
8282 
8283   constructor_constant = 1;
8284   constructor_simple = 1;
8285   constructor_nonconst = 0;
8286   constructor_depth = SPELLING_DEPTH ();
8287   constructor_elements = NULL;
8288   constructor_pending_elts = 0;
8289   constructor_type = type;
8290   constructor_incremental = 1;
8291   constructor_designated = 0;
8292   constructor_zeroinit = 1;
8293   designator_depth = 0;
8294   designator_erroneous = 0;
8295 
8296   if (RECORD_OR_UNION_TYPE_P (constructor_type))
8297     {
8298       constructor_fields = TYPE_FIELDS (constructor_type);
8299       /* Skip any nameless bit fields at the beginning.  */
8300       while (constructor_fields != NULL_TREE
8301 	     && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8302 	constructor_fields = DECL_CHAIN (constructor_fields);
8303 
8304       constructor_unfilled_fields = constructor_fields;
8305       constructor_bit_index = bitsize_zero_node;
8306     }
8307   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8308     {
8309       if (TYPE_DOMAIN (constructor_type))
8310 	{
8311 	  constructor_max_index
8312 	    = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8313 
8314 	  /* Detect non-empty initializations of zero-length arrays.  */
8315 	  if (constructor_max_index == NULL_TREE
8316 	      && TYPE_SIZE (constructor_type))
8317 	    constructor_max_index = integer_minus_one_node;
8318 
8319 	  /* constructor_max_index needs to be an INTEGER_CST.  Attempts
8320 	     to initialize VLAs will cause a proper error; avoid tree
8321 	     checking errors as well by setting a safe value.  */
8322 	  if (constructor_max_index
8323 	      && TREE_CODE (constructor_max_index) != INTEGER_CST)
8324 	    constructor_max_index = integer_minus_one_node;
8325 
8326 	  constructor_index
8327 	    = convert (bitsizetype,
8328 		       TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8329 	}
8330       else
8331 	{
8332 	  constructor_index = bitsize_zero_node;
8333 	  constructor_max_index = NULL_TREE;
8334 	}
8335 
8336       constructor_unfilled_index = constructor_index;
8337     }
8338   else if (VECTOR_TYPE_P (constructor_type))
8339     {
8340       /* Vectors are like simple fixed-size arrays.  */
8341       constructor_max_index =
8342 	bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8343       constructor_index = bitsize_zero_node;
8344       constructor_unfilled_index = constructor_index;
8345     }
8346   else
8347     {
8348       /* Handle the case of int x = {5}; */
8349       constructor_fields = constructor_type;
8350       constructor_unfilled_fields = constructor_type;
8351     }
8352 }
8353 
8354 extern location_t last_init_list_comma;
8355 
8356 /* Called when we see an open brace for a nested initializer.  Finish
8357    off any pending levels with implicit braces.  */
8358 void
finish_implicit_inits(location_t loc,struct obstack * braced_init_obstack)8359 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8360 {
8361   while (constructor_stack->implicit)
8362     {
8363       if (RECORD_OR_UNION_TYPE_P (constructor_type)
8364 	  && constructor_fields == NULL_TREE)
8365 	process_init_element (input_location,
8366 			      pop_init_level (loc, 1, braced_init_obstack,
8367 					      last_init_list_comma),
8368 			      true, braced_init_obstack);
8369       else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8370 	       && constructor_max_index
8371 	       && tree_int_cst_lt (constructor_max_index,
8372 				   constructor_index))
8373 	process_init_element (input_location,
8374 			      pop_init_level (loc, 1, braced_init_obstack,
8375 					      last_init_list_comma),
8376 			      true, braced_init_obstack);
8377       else
8378 	break;
8379     }
8380 }
8381 
8382 /* Push down into a subobject, for initialization.
8383    If this is for an explicit set of braces, IMPLICIT is 0.
8384    If it is because the next element belongs at a lower level,
8385    IMPLICIT is 1 (or 2 if the push is because of designator list).  */
8386 
8387 void
push_init_level(location_t loc,int implicit,struct obstack * braced_init_obstack)8388 push_init_level (location_t loc, int implicit,
8389 		 struct obstack *braced_init_obstack)
8390 {
8391   struct constructor_stack *p;
8392   tree value = NULL_TREE;
8393 
8394   /* Unless this is an explicit brace, we need to preserve previous
8395      content if any.  */
8396   if (implicit)
8397     {
8398       if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8399 	value = find_init_member (constructor_fields, braced_init_obstack);
8400       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8401 	value = find_init_member (constructor_index, braced_init_obstack);
8402     }
8403 
8404   p = XNEW (struct constructor_stack);
8405   p->type = constructor_type;
8406   p->fields = constructor_fields;
8407   p->index = constructor_index;
8408   p->max_index = constructor_max_index;
8409   p->unfilled_index = constructor_unfilled_index;
8410   p->unfilled_fields = constructor_unfilled_fields;
8411   p->bit_index = constructor_bit_index;
8412   p->elements = constructor_elements;
8413   p->constant = constructor_constant;
8414   p->simple = constructor_simple;
8415   p->nonconst = constructor_nonconst;
8416   p->erroneous = constructor_erroneous;
8417   p->pending_elts = constructor_pending_elts;
8418   p->depth = constructor_depth;
8419   p->replacement_value.value = NULL_TREE;
8420   p->replacement_value.original_code = ERROR_MARK;
8421   p->replacement_value.original_type = NULL;
8422   p->implicit = implicit;
8423   p->outer = 0;
8424   p->incremental = constructor_incremental;
8425   p->designated = constructor_designated;
8426   p->designator_depth = designator_depth;
8427   p->next = constructor_stack;
8428   p->range_stack = 0;
8429   constructor_stack = p;
8430 
8431   constructor_constant = 1;
8432   constructor_simple = 1;
8433   constructor_nonconst = 0;
8434   constructor_depth = SPELLING_DEPTH ();
8435   constructor_elements = NULL;
8436   constructor_incremental = 1;
8437   constructor_designated = 0;
8438   constructor_pending_elts = 0;
8439   if (!implicit)
8440     {
8441       p->range_stack = constructor_range_stack;
8442       constructor_range_stack = 0;
8443       designator_depth = 0;
8444       designator_erroneous = 0;
8445     }
8446 
8447   /* Don't die if an entire brace-pair level is superfluous
8448      in the containing level.  */
8449   if (constructor_type == NULL_TREE)
8450     ;
8451   else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8452     {
8453       /* Don't die if there are extra init elts at the end.  */
8454       if (constructor_fields == NULL_TREE)
8455 	constructor_type = NULL_TREE;
8456       else
8457 	{
8458 	  constructor_type = TREE_TYPE (constructor_fields);
8459 	  push_member_name (constructor_fields);
8460 	  constructor_depth++;
8461 	}
8462       /* If upper initializer is designated, then mark this as
8463 	 designated too to prevent bogus warnings.  */
8464       constructor_designated = p->designated;
8465     }
8466   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8467     {
8468       constructor_type = TREE_TYPE (constructor_type);
8469       push_array_bounds (tree_to_uhwi (constructor_index));
8470       constructor_depth++;
8471     }
8472 
8473   if (constructor_type == NULL_TREE)
8474     {
8475       error_init (loc, "extra brace group at end of initializer");
8476       constructor_fields = NULL_TREE;
8477       constructor_unfilled_fields = NULL_TREE;
8478       return;
8479     }
8480 
8481   if (value && TREE_CODE (value) == CONSTRUCTOR)
8482     {
8483       constructor_constant = TREE_CONSTANT (value);
8484       constructor_simple = TREE_STATIC (value);
8485       constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8486       constructor_elements = CONSTRUCTOR_ELTS (value);
8487       if (!vec_safe_is_empty (constructor_elements)
8488 	  && (TREE_CODE (constructor_type) == RECORD_TYPE
8489 	      || TREE_CODE (constructor_type) == ARRAY_TYPE))
8490 	set_nonincremental_init (braced_init_obstack);
8491     }
8492 
8493   if (implicit == 1)
8494     {
8495       found_missing_braces = 1;
8496       if (initializer_stack->missing_brace_richloc)
8497 	initializer_stack->missing_brace_richloc->add_fixit_insert_before
8498 	  (loc, "{");
8499     }
8500 
8501   if (RECORD_OR_UNION_TYPE_P (constructor_type))
8502     {
8503       constructor_fields = TYPE_FIELDS (constructor_type);
8504       /* Skip any nameless bit fields at the beginning.  */
8505       while (constructor_fields != NULL_TREE
8506 	     && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8507 	constructor_fields = DECL_CHAIN (constructor_fields);
8508 
8509       constructor_unfilled_fields = constructor_fields;
8510       constructor_bit_index = bitsize_zero_node;
8511     }
8512   else if (VECTOR_TYPE_P (constructor_type))
8513     {
8514       /* Vectors are like simple fixed-size arrays.  */
8515       constructor_max_index =
8516 	bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8517       constructor_index = bitsize_int (0);
8518       constructor_unfilled_index = constructor_index;
8519     }
8520   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8521     {
8522       if (TYPE_DOMAIN (constructor_type))
8523 	{
8524 	  constructor_max_index
8525 	    = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8526 
8527 	  /* Detect non-empty initializations of zero-length arrays.  */
8528 	  if (constructor_max_index == NULL_TREE
8529 	      && TYPE_SIZE (constructor_type))
8530 	    constructor_max_index = integer_minus_one_node;
8531 
8532 	  /* constructor_max_index needs to be an INTEGER_CST.  Attempts
8533 	     to initialize VLAs will cause a proper error; avoid tree
8534 	     checking errors as well by setting a safe value.  */
8535 	  if (constructor_max_index
8536 	      && TREE_CODE (constructor_max_index) != INTEGER_CST)
8537 	    constructor_max_index = integer_minus_one_node;
8538 
8539 	  constructor_index
8540 	    = convert (bitsizetype,
8541 		       TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8542 	}
8543       else
8544 	constructor_index = bitsize_zero_node;
8545 
8546       constructor_unfilled_index = constructor_index;
8547       if (value && TREE_CODE (value) == STRING_CST)
8548 	{
8549 	  /* We need to split the char/wchar array into individual
8550 	     characters, so that we don't have to special case it
8551 	     everywhere.  */
8552 	  set_nonincremental_init_from_string (value, braced_init_obstack);
8553 	}
8554     }
8555   else
8556     {
8557       if (constructor_type != error_mark_node)
8558 	warning_init (input_location, 0, "braces around scalar initializer");
8559       constructor_fields = constructor_type;
8560       constructor_unfilled_fields = constructor_type;
8561     }
8562 }
8563 
8564 /* At the end of an implicit or explicit brace level,
8565    finish up that level of constructor.  If a single expression
8566    with redundant braces initialized that level, return the
8567    c_expr structure for that expression.  Otherwise, the original_code
8568    element is set to ERROR_MARK.
8569    If we were outputting the elements as they are read, return 0 as the value
8570    from inner levels (process_init_element ignores that),
8571    but return error_mark_node as the value from the outermost level
8572    (that's what we want to put in DECL_INITIAL).
8573    Otherwise, return a CONSTRUCTOR expression as the value.  */
8574 
8575 struct c_expr
pop_init_level(location_t loc,int implicit,struct obstack * braced_init_obstack,location_t insert_before)8576 pop_init_level (location_t loc, int implicit,
8577 		struct obstack *braced_init_obstack,
8578 		location_t insert_before)
8579 {
8580   struct constructor_stack *p;
8581   struct c_expr ret;
8582   ret.value = NULL_TREE;
8583   ret.original_code = ERROR_MARK;
8584   ret.original_type = NULL;
8585 
8586   if (implicit == 0)
8587     {
8588       /* When we come to an explicit close brace,
8589 	 pop any inner levels that didn't have explicit braces.  */
8590       while (constructor_stack->implicit)
8591 	process_init_element (input_location,
8592 			      pop_init_level (loc, 1, braced_init_obstack,
8593 					      insert_before),
8594 			      true, braced_init_obstack);
8595       gcc_assert (!constructor_range_stack);
8596     }
8597   else
8598     if (initializer_stack->missing_brace_richloc)
8599       initializer_stack->missing_brace_richloc->add_fixit_insert_before
8600 	(insert_before, "}");
8601 
8602   /* Now output all pending elements.  */
8603   constructor_incremental = 1;
8604   output_pending_init_elements (1, braced_init_obstack);
8605 
8606   p = constructor_stack;
8607 
8608   /* Error for initializing a flexible array member, or a zero-length
8609      array member in an inappropriate context.  */
8610   if (constructor_type && constructor_fields
8611       && TREE_CODE (constructor_type) == ARRAY_TYPE
8612       && TYPE_DOMAIN (constructor_type)
8613       && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8614     {
8615       /* Silently discard empty initializations.  The parser will
8616 	 already have pedwarned for empty brackets.  */
8617       if (integer_zerop (constructor_unfilled_index))
8618 	constructor_type = NULL_TREE;
8619       else
8620 	{
8621 	  gcc_assert (!TYPE_SIZE (constructor_type));
8622 
8623 	  if (constructor_depth > 2)
8624 	    error_init (loc, "initialization of flexible array member in a nested context");
8625 	  else
8626 	    pedwarn_init (loc, OPT_Wpedantic,
8627 			  "initialization of a flexible array member");
8628 
8629 	  /* We have already issued an error message for the existence
8630 	     of a flexible array member not at the end of the structure.
8631 	     Discard the initializer so that we do not die later.  */
8632 	  if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8633 	    constructor_type = NULL_TREE;
8634 	}
8635     }
8636 
8637   switch (vec_safe_length (constructor_elements))
8638     {
8639     case 0:
8640       /* Initialization with { } counts as zeroinit.  */
8641       constructor_zeroinit = 1;
8642       break;
8643     case 1:
8644       /* This might be zeroinit as well.  */
8645       if (integer_zerop ((*constructor_elements)[0].value))
8646 	constructor_zeroinit = 1;
8647       break;
8648     default:
8649       /* If the constructor has more than one element, it can't be { 0 }.  */
8650       constructor_zeroinit = 0;
8651       break;
8652     }
8653 
8654   /* Warn when some structs are initialized with direct aggregation.  */
8655   if (!implicit && found_missing_braces && warn_missing_braces
8656       && !constructor_zeroinit)
8657     {
8658       gcc_assert (initializer_stack->missing_brace_richloc);
8659       warning_at (initializer_stack->missing_brace_richloc,
8660 		  OPT_Wmissing_braces,
8661 		  "missing braces around initializer");
8662     }
8663 
8664   /* Warn when some struct elements are implicitly initialized to zero.  */
8665   if (warn_missing_field_initializers
8666       && constructor_type
8667       && TREE_CODE (constructor_type) == RECORD_TYPE
8668       && constructor_unfilled_fields)
8669     {
8670 	/* Do not warn for flexible array members or zero-length arrays.  */
8671 	while (constructor_unfilled_fields
8672 	       && (!DECL_SIZE (constructor_unfilled_fields)
8673 		   || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8674 	  constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8675 
8676 	if (constructor_unfilled_fields
8677 	    /* Do not warn if this level of the initializer uses member
8678 	       designators; it is likely to be deliberate.  */
8679 	    && !constructor_designated
8680 	    /* Do not warn about initializing with { 0 } or with { }.  */
8681 	    && !constructor_zeroinit)
8682 	  {
8683 	    if (warning_at (input_location, OPT_Wmissing_field_initializers,
8684 			    "missing initializer for field %qD of %qT",
8685 			    constructor_unfilled_fields,
8686 			    constructor_type))
8687 	      inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8688 		      "%qD declared here", constructor_unfilled_fields);
8689 	  }
8690     }
8691 
8692   /* Pad out the end of the structure.  */
8693   if (p->replacement_value.value)
8694     /* If this closes a superfluous brace pair,
8695        just pass out the element between them.  */
8696     ret = p->replacement_value;
8697   else if (constructor_type == NULL_TREE)
8698     ;
8699   else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8700 	   && TREE_CODE (constructor_type) != ARRAY_TYPE
8701 	   && !VECTOR_TYPE_P (constructor_type))
8702     {
8703       /* A nonincremental scalar initializer--just return
8704 	 the element, after verifying there is just one.  */
8705       if (vec_safe_is_empty (constructor_elements))
8706 	{
8707 	  if (!constructor_erroneous)
8708 	    error_init (loc, "empty scalar initializer");
8709 	  ret.value = error_mark_node;
8710 	}
8711       else if (vec_safe_length (constructor_elements) != 1)
8712 	{
8713 	  error_init (loc, "extra elements in scalar initializer");
8714 	  ret.value = (*constructor_elements)[0].value;
8715 	}
8716       else
8717 	ret.value = (*constructor_elements)[0].value;
8718     }
8719   else
8720     {
8721       if (constructor_erroneous)
8722 	ret.value = error_mark_node;
8723       else
8724 	{
8725 	  ret.value = build_constructor (constructor_type,
8726 					 constructor_elements);
8727 	  if (constructor_constant)
8728 	    TREE_CONSTANT (ret.value) = 1;
8729 	  if (constructor_constant && constructor_simple)
8730 	    TREE_STATIC (ret.value) = 1;
8731 	  if (constructor_nonconst)
8732 	    CONSTRUCTOR_NON_CONST (ret.value) = 1;
8733 	}
8734     }
8735 
8736   if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8737     {
8738       if (constructor_nonconst)
8739 	ret.original_code = C_MAYBE_CONST_EXPR;
8740       else if (ret.original_code == C_MAYBE_CONST_EXPR)
8741 	ret.original_code = ERROR_MARK;
8742     }
8743 
8744   constructor_type = p->type;
8745   constructor_fields = p->fields;
8746   constructor_index = p->index;
8747   constructor_max_index = p->max_index;
8748   constructor_unfilled_index = p->unfilled_index;
8749   constructor_unfilled_fields = p->unfilled_fields;
8750   constructor_bit_index = p->bit_index;
8751   constructor_elements = p->elements;
8752   constructor_constant = p->constant;
8753   constructor_simple = p->simple;
8754   constructor_nonconst = p->nonconst;
8755   constructor_erroneous = p->erroneous;
8756   constructor_incremental = p->incremental;
8757   constructor_designated = p->designated;
8758   designator_depth = p->designator_depth;
8759   constructor_pending_elts = p->pending_elts;
8760   constructor_depth = p->depth;
8761   if (!p->implicit)
8762     constructor_range_stack = p->range_stack;
8763   RESTORE_SPELLING_DEPTH (constructor_depth);
8764 
8765   constructor_stack = p->next;
8766   free (p);
8767 
8768   if (ret.value == NULL_TREE && constructor_stack == 0)
8769     ret.value = error_mark_node;
8770   return ret;
8771 }
8772 
8773 /* Common handling for both array range and field name designators.
8774    ARRAY argument is nonzero for array ranges.  Returns false for success.  */
8775 
8776 static bool
set_designator(location_t loc,bool array,struct obstack * braced_init_obstack)8777 set_designator (location_t loc, bool array,
8778 		struct obstack *braced_init_obstack)
8779 {
8780   tree subtype;
8781   enum tree_code subcode;
8782 
8783   /* Don't die if an entire brace-pair level is superfluous
8784      in the containing level.  */
8785   if (constructor_type == NULL_TREE)
8786     return true;
8787 
8788   /* If there were errors in this designator list already, bail out
8789      silently.  */
8790   if (designator_erroneous)
8791     return true;
8792 
8793   if (!designator_depth)
8794     {
8795       gcc_assert (!constructor_range_stack);
8796 
8797       /* Designator list starts at the level of closest explicit
8798 	 braces.  */
8799       while (constructor_stack->implicit)
8800 	process_init_element (input_location,
8801 			      pop_init_level (loc, 1, braced_init_obstack,
8802 					      last_init_list_comma),
8803 			      true, braced_init_obstack);
8804       constructor_designated = 1;
8805       return false;
8806     }
8807 
8808   switch (TREE_CODE (constructor_type))
8809     {
8810     case  RECORD_TYPE:
8811     case  UNION_TYPE:
8812       subtype = TREE_TYPE (constructor_fields);
8813       if (subtype != error_mark_node)
8814 	subtype = TYPE_MAIN_VARIANT (subtype);
8815       break;
8816     case ARRAY_TYPE:
8817       subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8818       break;
8819     default:
8820       gcc_unreachable ();
8821     }
8822 
8823   subcode = TREE_CODE (subtype);
8824   if (array && subcode != ARRAY_TYPE)
8825     {
8826       error_init (loc, "array index in non-array initializer");
8827       return true;
8828     }
8829   else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8830     {
8831       error_init (loc, "field name not in record or union initializer");
8832       return true;
8833     }
8834 
8835   constructor_designated = 1;
8836   finish_implicit_inits (loc, braced_init_obstack);
8837   push_init_level (loc, 2, braced_init_obstack);
8838   return false;
8839 }
8840 
8841 /* If there are range designators in designator list, push a new designator
8842    to constructor_range_stack.  RANGE_END is end of such stack range or
8843    NULL_TREE if there is no range designator at this level.  */
8844 
8845 static void
push_range_stack(tree range_end,struct obstack * braced_init_obstack)8846 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8847 {
8848   struct constructor_range_stack *p;
8849 
8850   p = (struct constructor_range_stack *)
8851     obstack_alloc (braced_init_obstack,
8852 		   sizeof (struct constructor_range_stack));
8853   p->prev = constructor_range_stack;
8854   p->next = 0;
8855   p->fields = constructor_fields;
8856   p->range_start = constructor_index;
8857   p->index = constructor_index;
8858   p->stack = constructor_stack;
8859   p->range_end = range_end;
8860   if (constructor_range_stack)
8861     constructor_range_stack->next = p;
8862   constructor_range_stack = p;
8863 }
8864 
8865 /* Within an array initializer, specify the next index to be initialized.
8866    FIRST is that index.  If LAST is nonzero, then initialize a range
8867    of indices, running from FIRST through LAST.  */
8868 
8869 void
set_init_index(location_t loc,tree first,tree last,struct obstack * braced_init_obstack)8870 set_init_index (location_t loc, tree first, tree last,
8871 		struct obstack *braced_init_obstack)
8872 {
8873   if (set_designator (loc, true, braced_init_obstack))
8874     return;
8875 
8876   designator_erroneous = 1;
8877 
8878   if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8879       || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8880     {
8881       error_init (loc, "array index in initializer not of integer type");
8882       return;
8883     }
8884 
8885   if (TREE_CODE (first) != INTEGER_CST)
8886     {
8887       first = c_fully_fold (first, false, NULL);
8888       if (TREE_CODE (first) == INTEGER_CST)
8889 	pedwarn_init (loc, OPT_Wpedantic,
8890 		      "array index in initializer is not "
8891 		      "an integer constant expression");
8892     }
8893 
8894   if (last && TREE_CODE (last) != INTEGER_CST)
8895     {
8896       last = c_fully_fold (last, false, NULL);
8897       if (TREE_CODE (last) == INTEGER_CST)
8898 	pedwarn_init (loc, OPT_Wpedantic,
8899 		      "array index in initializer is not "
8900 		      "an integer constant expression");
8901     }
8902 
8903   if (TREE_CODE (first) != INTEGER_CST)
8904     error_init (loc, "nonconstant array index in initializer");
8905   else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
8906     error_init (loc, "nonconstant array index in initializer");
8907   else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
8908     error_init (loc, "array index in non-array initializer");
8909   else if (tree_int_cst_sgn (first) == -1)
8910     error_init (loc, "array index in initializer exceeds array bounds");
8911   else if (constructor_max_index
8912 	   && tree_int_cst_lt (constructor_max_index, first))
8913     error_init (loc, "array index in initializer exceeds array bounds");
8914   else
8915     {
8916       constant_expression_warning (first);
8917       if (last)
8918 	constant_expression_warning (last);
8919       constructor_index = convert (bitsizetype, first);
8920       if (tree_int_cst_lt (constructor_index, first))
8921 	{
8922 	  constructor_index = copy_node (constructor_index);
8923 	  TREE_OVERFLOW (constructor_index) = 1;
8924 	}
8925 
8926       if (last)
8927 	{
8928 	  if (tree_int_cst_equal (first, last))
8929 	    last = NULL_TREE;
8930 	  else if (tree_int_cst_lt (last, first))
8931 	    {
8932 	      error_init (loc, "empty index range in initializer");
8933 	      last = NULL_TREE;
8934 	    }
8935 	  else
8936 	    {
8937 	      last = convert (bitsizetype, last);
8938 	      if (constructor_max_index != NULL_TREE
8939 		  && tree_int_cst_lt (constructor_max_index, last))
8940 		{
8941 		  error_init (loc, "array index range in initializer exceeds "
8942 			      "array bounds");
8943 		  last = NULL_TREE;
8944 		}
8945 	    }
8946 	}
8947 
8948       designator_depth++;
8949       designator_erroneous = 0;
8950       if (constructor_range_stack || last)
8951 	push_range_stack (last, braced_init_obstack);
8952     }
8953 }
8954 
8955 /* Within a struct initializer, specify the next field to be initialized.  */
8956 
8957 void
set_init_label(location_t loc,tree fieldname,location_t fieldname_loc,struct obstack * braced_init_obstack)8958 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
8959 		struct obstack *braced_init_obstack)
8960 {
8961   tree field;
8962 
8963   if (set_designator (loc, false, braced_init_obstack))
8964     return;
8965 
8966   designator_erroneous = 1;
8967 
8968   if (!RECORD_OR_UNION_TYPE_P (constructor_type))
8969     {
8970       error_init (loc, "field name not in record or union initializer");
8971       return;
8972     }
8973 
8974   field = lookup_field (constructor_type, fieldname);
8975 
8976   if (field == NULL_TREE)
8977     {
8978       tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8979       if (guessed_id)
8980 	{
8981 	  gcc_rich_location rich_loc (fieldname_loc);
8982 	  rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
8983 	  error_at (&rich_loc,
8984 		    "%qT has no member named %qE; did you mean %qE?",
8985 		    constructor_type, fieldname, guessed_id);
8986 	}
8987       else
8988 	error_at (fieldname_loc, "%qT has no member named %qE",
8989 		  constructor_type, fieldname);
8990     }
8991   else
8992     do
8993       {
8994 	constructor_fields = TREE_VALUE (field);
8995 	designator_depth++;
8996 	designator_erroneous = 0;
8997 	if (constructor_range_stack)
8998 	  push_range_stack (NULL_TREE, braced_init_obstack);
8999 	field = TREE_CHAIN (field);
9000 	if (field)
9001 	  {
9002 	    if (set_designator (loc, false, braced_init_obstack))
9003 	      return;
9004 	  }
9005       }
9006     while (field != NULL_TREE);
9007 }
9008 
9009 /* Add a new initializer to the tree of pending initializers.  PURPOSE
9010    identifies the initializer, either array index or field in a structure.
9011    VALUE is the value of that index or field.  If ORIGTYPE is not
9012    NULL_TREE, it is the original type of VALUE.
9013 
9014    IMPLICIT is true if value comes from pop_init_level (1),
9015    the new initializer has been merged with the existing one
9016    and thus no warnings should be emitted about overriding an
9017    existing initializer.  */
9018 
9019 static void
add_pending_init(location_t loc,tree purpose,tree value,tree origtype,bool implicit,struct obstack * braced_init_obstack)9020 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9021 		  bool implicit, struct obstack *braced_init_obstack)
9022 {
9023   struct init_node *p, **q, *r;
9024 
9025   q = &constructor_pending_elts;
9026   p = 0;
9027 
9028   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9029     {
9030       while (*q != 0)
9031 	{
9032 	  p = *q;
9033 	  if (tree_int_cst_lt (purpose, p->purpose))
9034 	    q = &p->left;
9035 	  else if (tree_int_cst_lt (p->purpose, purpose))
9036 	    q = &p->right;
9037 	  else
9038 	    {
9039 	      if (!implicit)
9040 		{
9041 		  if (TREE_SIDE_EFFECTS (p->value))
9042 		    warning_init (loc, OPT_Woverride_init_side_effects,
9043 				  "initialized field with side-effects "
9044 				  "overwritten");
9045 		  else if (warn_override_init)
9046 		    warning_init (loc, OPT_Woverride_init,
9047 				  "initialized field overwritten");
9048 		}
9049 	      p->value = value;
9050 	      p->origtype = origtype;
9051 	      return;
9052 	    }
9053 	}
9054     }
9055   else
9056     {
9057       tree bitpos;
9058 
9059       bitpos = bit_position (purpose);
9060       while (*q != NULL)
9061 	{
9062 	  p = *q;
9063 	  if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9064 	    q = &p->left;
9065 	  else if (p->purpose != purpose)
9066 	    q = &p->right;
9067 	  else
9068 	    {
9069 	      if (!implicit)
9070 		{
9071 		  if (TREE_SIDE_EFFECTS (p->value))
9072 		    warning_init (loc, OPT_Woverride_init_side_effects,
9073 				  "initialized field with side-effects "
9074 				  "overwritten");
9075 		  else if (warn_override_init)
9076 		    warning_init (loc, OPT_Woverride_init,
9077 				  "initialized field overwritten");
9078 		}
9079 	      p->value = value;
9080 	      p->origtype = origtype;
9081 	      return;
9082 	    }
9083 	}
9084     }
9085 
9086   r = (struct init_node *) obstack_alloc (braced_init_obstack,
9087 					  sizeof (struct init_node));
9088   r->purpose = purpose;
9089   r->value = value;
9090   r->origtype = origtype;
9091 
9092   *q = r;
9093   r->parent = p;
9094   r->left = 0;
9095   r->right = 0;
9096   r->balance = 0;
9097 
9098   while (p)
9099     {
9100       struct init_node *s;
9101 
9102       if (r == p->left)
9103 	{
9104 	  if (p->balance == 0)
9105 	    p->balance = -1;
9106 	  else if (p->balance < 0)
9107 	    {
9108 	      if (r->balance < 0)
9109 		{
9110 		  /* L rotation.  */
9111 		  p->left = r->right;
9112 		  if (p->left)
9113 		    p->left->parent = p;
9114 		  r->right = p;
9115 
9116 		  p->balance = 0;
9117 		  r->balance = 0;
9118 
9119 		  s = p->parent;
9120 		  p->parent = r;
9121 		  r->parent = s;
9122 		  if (s)
9123 		    {
9124 		      if (s->left == p)
9125 			s->left = r;
9126 		      else
9127 			s->right = r;
9128 		    }
9129 		  else
9130 		    constructor_pending_elts = r;
9131 		}
9132 	      else
9133 		{
9134 		  /* LR rotation.  */
9135 		  struct init_node *t = r->right;
9136 
9137 		  r->right = t->left;
9138 		  if (r->right)
9139 		    r->right->parent = r;
9140 		  t->left = r;
9141 
9142 		  p->left = t->right;
9143 		  if (p->left)
9144 		    p->left->parent = p;
9145 		  t->right = p;
9146 
9147 		  p->balance = t->balance < 0;
9148 		  r->balance = -(t->balance > 0);
9149 		  t->balance = 0;
9150 
9151 		  s = p->parent;
9152 		  p->parent = t;
9153 		  r->parent = t;
9154 		  t->parent = s;
9155 		  if (s)
9156 		    {
9157 		      if (s->left == p)
9158 			s->left = t;
9159 		      else
9160 			s->right = t;
9161 		    }
9162 		  else
9163 		    constructor_pending_elts = t;
9164 		}
9165 	      break;
9166 	    }
9167 	  else
9168 	    {
9169 	      /* p->balance == +1; growth of left side balances the node.  */
9170 	      p->balance = 0;
9171 	      break;
9172 	    }
9173 	}
9174       else /* r == p->right */
9175 	{
9176 	  if (p->balance == 0)
9177 	    /* Growth propagation from right side.  */
9178 	    p->balance++;
9179 	  else if (p->balance > 0)
9180 	    {
9181 	      if (r->balance > 0)
9182 		{
9183 		  /* R rotation.  */
9184 		  p->right = r->left;
9185 		  if (p->right)
9186 		    p->right->parent = p;
9187 		  r->left = p;
9188 
9189 		  p->balance = 0;
9190 		  r->balance = 0;
9191 
9192 		  s = p->parent;
9193 		  p->parent = r;
9194 		  r->parent = s;
9195 		  if (s)
9196 		    {
9197 		      if (s->left == p)
9198 			s->left = r;
9199 		      else
9200 			s->right = r;
9201 		    }
9202 		  else
9203 		    constructor_pending_elts = r;
9204 		}
9205 	      else /* r->balance == -1 */
9206 		{
9207 		  /* RL rotation */
9208 		  struct init_node *t = r->left;
9209 
9210 		  r->left = t->right;
9211 		  if (r->left)
9212 		    r->left->parent = r;
9213 		  t->right = r;
9214 
9215 		  p->right = t->left;
9216 		  if (p->right)
9217 		    p->right->parent = p;
9218 		  t->left = p;
9219 
9220 		  r->balance = (t->balance < 0);
9221 		  p->balance = -(t->balance > 0);
9222 		  t->balance = 0;
9223 
9224 		  s = p->parent;
9225 		  p->parent = t;
9226 		  r->parent = t;
9227 		  t->parent = s;
9228 		  if (s)
9229 		    {
9230 		      if (s->left == p)
9231 			s->left = t;
9232 		      else
9233 			s->right = t;
9234 		    }
9235 		  else
9236 		    constructor_pending_elts = t;
9237 		}
9238 	      break;
9239 	    }
9240 	  else
9241 	    {
9242 	      /* p->balance == -1; growth of right side balances the node.  */
9243 	      p->balance = 0;
9244 	      break;
9245 	    }
9246 	}
9247 
9248       r = p;
9249       p = p->parent;
9250     }
9251 }
9252 
9253 /* Build AVL tree from a sorted chain.  */
9254 
9255 static void
set_nonincremental_init(struct obstack * braced_init_obstack)9256 set_nonincremental_init (struct obstack * braced_init_obstack)
9257 {
9258   unsigned HOST_WIDE_INT ix;
9259   tree index, value;
9260 
9261   if (TREE_CODE (constructor_type) != RECORD_TYPE
9262       && TREE_CODE (constructor_type) != ARRAY_TYPE)
9263     return;
9264 
9265   FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9266     add_pending_init (input_location, index, value, NULL_TREE, true,
9267 		      braced_init_obstack);
9268   constructor_elements = NULL;
9269   if (TREE_CODE (constructor_type) == RECORD_TYPE)
9270     {
9271       constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9272       /* Skip any nameless bit fields at the beginning.  */
9273       while (constructor_unfilled_fields != NULL_TREE
9274 	     && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9275 	constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9276 
9277     }
9278   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9279     {
9280       if (TYPE_DOMAIN (constructor_type))
9281 	constructor_unfilled_index
9282 	    = convert (bitsizetype,
9283 		       TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9284       else
9285 	constructor_unfilled_index = bitsize_zero_node;
9286     }
9287   constructor_incremental = 0;
9288 }
9289 
9290 /* Build AVL tree from a string constant.  */
9291 
9292 static void
set_nonincremental_init_from_string(tree str,struct obstack * braced_init_obstack)9293 set_nonincremental_init_from_string (tree str,
9294 				     struct obstack * braced_init_obstack)
9295 {
9296   tree value, purpose, type;
9297   HOST_WIDE_INT val[2];
9298   const char *p, *end;
9299   int byte, wchar_bytes, charwidth, bitpos;
9300 
9301   gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9302 
9303   wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9304   charwidth = TYPE_PRECISION (char_type_node);
9305   gcc_assert ((size_t) wchar_bytes * charwidth
9306 	      <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9307   type = TREE_TYPE (constructor_type);
9308   p = TREE_STRING_POINTER (str);
9309   end = p + TREE_STRING_LENGTH (str);
9310 
9311   for (purpose = bitsize_zero_node;
9312        p < end
9313        && !(constructor_max_index
9314 	    && tree_int_cst_lt (constructor_max_index, purpose));
9315        purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9316     {
9317       if (wchar_bytes == 1)
9318 	{
9319 	  val[0] = (unsigned char) *p++;
9320 	  val[1] = 0;
9321 	}
9322       else
9323 	{
9324 	  val[1] = 0;
9325 	  val[0] = 0;
9326 	  for (byte = 0; byte < wchar_bytes; byte++)
9327 	    {
9328 	      if (BYTES_BIG_ENDIAN)
9329 		bitpos = (wchar_bytes - byte - 1) * charwidth;
9330 	      else
9331 		bitpos = byte * charwidth;
9332 	      val[bitpos / HOST_BITS_PER_WIDE_INT]
9333 		|= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9334 		   << (bitpos % HOST_BITS_PER_WIDE_INT);
9335 	    }
9336 	}
9337 
9338       if (!TYPE_UNSIGNED (type))
9339 	{
9340 	  bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9341 	  if (bitpos < HOST_BITS_PER_WIDE_INT)
9342 	    {
9343 	      if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9344 		{
9345 		  val[0] |= HOST_WIDE_INT_M1U << bitpos;
9346 		  val[1] = -1;
9347 		}
9348 	    }
9349 	  else if (bitpos == HOST_BITS_PER_WIDE_INT)
9350 	    {
9351 	      if (val[0] < 0)
9352 		val[1] = -1;
9353 	    }
9354 	  else if (val[1] & (HOST_WIDE_INT_1
9355 			     << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9356 	    val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9357 	}
9358 
9359       value = wide_int_to_tree (type,
9360 				wide_int::from_array (val, 2,
9361 						      HOST_BITS_PER_WIDE_INT * 2));
9362       add_pending_init (input_location, purpose, value, NULL_TREE, true,
9363                         braced_init_obstack);
9364     }
9365 
9366   constructor_incremental = 0;
9367 }
9368 
9369 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9370    not initialized yet.  */
9371 
9372 static tree
find_init_member(tree field,struct obstack * braced_init_obstack)9373 find_init_member (tree field, struct obstack * braced_init_obstack)
9374 {
9375   struct init_node *p;
9376 
9377   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9378     {
9379       if (constructor_incremental
9380 	  && tree_int_cst_lt (field, constructor_unfilled_index))
9381 	set_nonincremental_init (braced_init_obstack);
9382 
9383       p = constructor_pending_elts;
9384       while (p)
9385 	{
9386 	  if (tree_int_cst_lt (field, p->purpose))
9387 	    p = p->left;
9388 	  else if (tree_int_cst_lt (p->purpose, field))
9389 	    p = p->right;
9390 	  else
9391 	    return p->value;
9392 	}
9393     }
9394   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9395     {
9396       tree bitpos = bit_position (field);
9397 
9398       if (constructor_incremental
9399 	  && (!constructor_unfilled_fields
9400 	      || tree_int_cst_lt (bitpos,
9401 				  bit_position (constructor_unfilled_fields))))
9402 	set_nonincremental_init (braced_init_obstack);
9403 
9404       p = constructor_pending_elts;
9405       while (p)
9406 	{
9407 	  if (field == p->purpose)
9408 	    return p->value;
9409 	  else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9410 	    p = p->left;
9411 	  else
9412 	    p = p->right;
9413 	}
9414     }
9415   else if (TREE_CODE (constructor_type) == UNION_TYPE)
9416     {
9417       if (!vec_safe_is_empty (constructor_elements)
9418 	  && (constructor_elements->last ().index == field))
9419 	return constructor_elements->last ().value;
9420     }
9421   return NULL_TREE;
9422 }
9423 
9424 /* "Output" the next constructor element.
9425    At top level, really output it to assembler code now.
9426    Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9427    If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9428    TYPE is the data type that the containing data type wants here.
9429    FIELD is the field (a FIELD_DECL) or the index that this element fills.
9430    If VALUE is a string constant, STRICT_STRING is true if it is
9431    unparenthesized or we should not warn here for it being parenthesized.
9432    For other types of VALUE, STRICT_STRING is not used.
9433 
9434    PENDING if true means output pending elements that belong
9435    right after this element.  (PENDING is normally true;
9436    it is false while outputting pending elements, to avoid recursion.)
9437 
9438    IMPLICIT is true if value comes from pop_init_level (1),
9439    the new initializer has been merged with the existing one
9440    and thus no warnings should be emitted about overriding an
9441    existing initializer.  */
9442 
9443 static void
output_init_element(location_t loc,tree value,tree origtype,bool strict_string,tree type,tree field,bool pending,bool implicit,struct obstack * braced_init_obstack)9444 output_init_element (location_t loc, tree value, tree origtype,
9445 		     bool strict_string, tree type, tree field, bool pending,
9446 		     bool implicit, struct obstack * braced_init_obstack)
9447 {
9448   tree semantic_type = NULL_TREE;
9449   bool maybe_const = true;
9450   bool npc;
9451 
9452   if (type == error_mark_node || value == error_mark_node)
9453     {
9454       constructor_erroneous = 1;
9455       return;
9456     }
9457   if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9458       && (TREE_CODE (value) == STRING_CST
9459 	  || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9460       && !(TREE_CODE (value) == STRING_CST
9461 	   && TREE_CODE (type) == ARRAY_TYPE
9462 	   && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9463       && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9464 		     TYPE_MAIN_VARIANT (type)))
9465     value = array_to_pointer_conversion (input_location, value);
9466 
9467   if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9468       && require_constant_value && pending)
9469     {
9470       /* As an extension, allow initializing objects with static storage
9471 	 duration with compound literals (which are then treated just as
9472 	 the brace enclosed list they contain).  */
9473       if (flag_isoc99)
9474 	pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9475 		      "constant");
9476       tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9477       value = DECL_INITIAL (decl);
9478     }
9479 
9480   npc = null_pointer_constant_p (value);
9481   if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9482     {
9483       semantic_type = TREE_TYPE (value);
9484       value = TREE_OPERAND (value, 0);
9485     }
9486   value = c_fully_fold (value, require_constant_value, &maybe_const);
9487 
9488   if (value == error_mark_node)
9489     constructor_erroneous = 1;
9490   else if (!TREE_CONSTANT (value))
9491     constructor_constant = 0;
9492   else if (!initializer_constant_valid_p (value,
9493 					  TREE_TYPE (value),
9494 					  AGGREGATE_TYPE_P (constructor_type)
9495 					  && TYPE_REVERSE_STORAGE_ORDER
9496 					     (constructor_type))
9497 	   || (RECORD_OR_UNION_TYPE_P (constructor_type)
9498 	       && DECL_C_BIT_FIELD (field)
9499 	       && TREE_CODE (value) != INTEGER_CST))
9500     constructor_simple = 0;
9501   if (!maybe_const)
9502     constructor_nonconst = 1;
9503 
9504   /* Digest the initializer and issue any errors about incompatible
9505      types before issuing errors about non-constant initializers.  */
9506   tree new_value = value;
9507   if (semantic_type)
9508     new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9509   new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9510 			   require_constant_value);
9511   if (new_value == error_mark_node)
9512     {
9513       constructor_erroneous = 1;
9514       return;
9515     }
9516   if (require_constant_value || require_constant_elements)
9517     constant_expression_warning (new_value);
9518 
9519   /* Proceed to check the constness of the original initializer.  */
9520   if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9521     {
9522       if (require_constant_value)
9523 	{
9524 	  error_init (loc, "initializer element is not constant");
9525 	  value = error_mark_node;
9526 	}
9527       else if (require_constant_elements)
9528 	pedwarn (loc, OPT_Wpedantic,
9529 		 "initializer element is not computable at load time");
9530     }
9531   else if (!maybe_const
9532 	   && (require_constant_value || require_constant_elements))
9533     pedwarn_init (loc, OPT_Wpedantic,
9534 		  "initializer element is not a constant expression");
9535 
9536   /* Issue -Wc++-compat warnings about initializing a bitfield with
9537      enum type.  */
9538   if (warn_cxx_compat
9539       && field != NULL_TREE
9540       && TREE_CODE (field) == FIELD_DECL
9541       && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9542       && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9543 	  != TYPE_MAIN_VARIANT (type))
9544       && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9545     {
9546       tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9547       if (checktype != error_mark_node
9548 	  && (TYPE_MAIN_VARIANT (checktype)
9549 	      != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9550 	warning_init (loc, OPT_Wc___compat,
9551 		      "enum conversion in initialization is invalid in C++");
9552     }
9553 
9554   /* If this field is empty and does not have side effects (and is not at
9555      the end of structure), don't do anything other than checking the
9556      initializer.  */
9557   if (field
9558       && (TREE_TYPE (field) == error_mark_node
9559 	  || (COMPLETE_TYPE_P (TREE_TYPE (field))
9560 	      && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9561 	      && !TREE_SIDE_EFFECTS (new_value)
9562 	      && (TREE_CODE (constructor_type) == ARRAY_TYPE
9563 		  || DECL_CHAIN (field)))))
9564     return;
9565 
9566   /* Finally, set VALUE to the initializer value digested above.  */
9567   value = new_value;
9568 
9569   /* If this element doesn't come next in sequence,
9570      put it on constructor_pending_elts.  */
9571   if (TREE_CODE (constructor_type) == ARRAY_TYPE
9572       && (!constructor_incremental
9573 	  || !tree_int_cst_equal (field, constructor_unfilled_index)))
9574     {
9575       if (constructor_incremental
9576 	  && tree_int_cst_lt (field, constructor_unfilled_index))
9577 	set_nonincremental_init (braced_init_obstack);
9578 
9579       add_pending_init (loc, field, value, origtype, implicit,
9580 			braced_init_obstack);
9581       return;
9582     }
9583   else if (TREE_CODE (constructor_type) == RECORD_TYPE
9584 	   && (!constructor_incremental
9585 	       || field != constructor_unfilled_fields))
9586     {
9587       /* We do this for records but not for unions.  In a union,
9588 	 no matter which field is specified, it can be initialized
9589 	 right away since it starts at the beginning of the union.  */
9590       if (constructor_incremental)
9591 	{
9592 	  if (!constructor_unfilled_fields)
9593 	    set_nonincremental_init (braced_init_obstack);
9594 	  else
9595 	    {
9596 	      tree bitpos, unfillpos;
9597 
9598 	      bitpos = bit_position (field);
9599 	      unfillpos = bit_position (constructor_unfilled_fields);
9600 
9601 	      if (tree_int_cst_lt (bitpos, unfillpos))
9602 		set_nonincremental_init (braced_init_obstack);
9603 	    }
9604 	}
9605 
9606       add_pending_init (loc, field, value, origtype, implicit,
9607 			braced_init_obstack);
9608       return;
9609     }
9610   else if (TREE_CODE (constructor_type) == UNION_TYPE
9611 	   && !vec_safe_is_empty (constructor_elements))
9612     {
9613       if (!implicit)
9614 	{
9615 	  if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9616 	    warning_init (loc, OPT_Woverride_init_side_effects,
9617 			  "initialized field with side-effects overwritten");
9618 	  else if (warn_override_init)
9619 	    warning_init (loc, OPT_Woverride_init,
9620 			  "initialized field overwritten");
9621 	}
9622 
9623       /* We can have just one union field set.  */
9624       constructor_elements = NULL;
9625     }
9626 
9627   /* Otherwise, output this element either to
9628      constructor_elements or to the assembler file.  */
9629 
9630   constructor_elt celt = {field, value};
9631   vec_safe_push (constructor_elements, celt);
9632 
9633   /* Advance the variable that indicates sequential elements output.  */
9634   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9635     constructor_unfilled_index
9636       = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9637 			bitsize_one_node);
9638   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9639     {
9640       constructor_unfilled_fields
9641 	= DECL_CHAIN (constructor_unfilled_fields);
9642 
9643       /* Skip any nameless bit fields.  */
9644       while (constructor_unfilled_fields != NULL_TREE
9645 	     && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9646 	constructor_unfilled_fields =
9647 	  DECL_CHAIN (constructor_unfilled_fields);
9648     }
9649   else if (TREE_CODE (constructor_type) == UNION_TYPE)
9650     constructor_unfilled_fields = NULL_TREE;
9651 
9652   /* Now output any pending elements which have become next.  */
9653   if (pending)
9654     output_pending_init_elements (0, braced_init_obstack);
9655 }
9656 
9657 /* For two FIELD_DECLs in the same chain, return -1 if field1
9658    comes before field2, 1 if field1 comes after field2 and
9659    0 if field1 == field2.  */
9660 
9661 static int
init_field_decl_cmp(tree field1,tree field2)9662 init_field_decl_cmp (tree field1, tree field2)
9663 {
9664   if (field1 == field2)
9665     return 0;
9666 
9667   tree bitpos1 = bit_position (field1);
9668   tree bitpos2 = bit_position (field2);
9669   if (tree_int_cst_equal (bitpos1, bitpos2))
9670     {
9671       /* If one of the fields has non-zero bitsize, then that
9672 	 field must be the last one in a sequence of zero
9673 	 sized fields, fields after it will have bigger
9674 	 bit_position.  */
9675       if (TREE_TYPE (field1) != error_mark_node
9676 	  && COMPLETE_TYPE_P (TREE_TYPE (field1))
9677 	  && integer_nonzerop (TREE_TYPE (field1)))
9678 	return 1;
9679       if (TREE_TYPE (field2) != error_mark_node
9680 	  && COMPLETE_TYPE_P (TREE_TYPE (field2))
9681 	  && integer_nonzerop (TREE_TYPE (field2)))
9682 	return -1;
9683       /* Otherwise, fallback to DECL_CHAIN walk to find out
9684 	 which field comes earlier.  Walk chains of both
9685 	 fields, so that if field1 and field2 are close to each
9686 	 other in either order, it is found soon even for large
9687 	 sequences of zero sized fields.  */
9688       tree f1 = field1, f2 = field2;
9689       while (1)
9690 	{
9691 	  f1 = DECL_CHAIN (f1);
9692 	  f2 = DECL_CHAIN (f2);
9693 	  if (f1 == NULL_TREE)
9694 	    {
9695 	      gcc_assert (f2);
9696 	      return 1;
9697 	    }
9698 	  if (f2 == NULL_TREE)
9699 	    return -1;
9700 	  if (f1 == field2)
9701 	    return -1;
9702 	  if (f2 == field1)
9703 	    return 1;
9704 	  if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9705 	    return 1;
9706 	  if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9707 	    return -1;
9708 	}
9709     }
9710   else if (tree_int_cst_lt (bitpos1, bitpos2))
9711     return -1;
9712   else
9713     return 1;
9714 }
9715 
9716 /* Output any pending elements which have become next.
9717    As we output elements, constructor_unfilled_{fields,index}
9718    advances, which may cause other elements to become next;
9719    if so, they too are output.
9720 
9721    If ALL is 0, we return when there are
9722    no more pending elements to output now.
9723 
9724    If ALL is 1, we output space as necessary so that
9725    we can output all the pending elements.  */
9726 static void
output_pending_init_elements(int all,struct obstack * braced_init_obstack)9727 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9728 {
9729   struct init_node *elt = constructor_pending_elts;
9730   tree next;
9731 
9732  retry:
9733 
9734   /* Look through the whole pending tree.
9735      If we find an element that should be output now,
9736      output it.  Otherwise, set NEXT to the element
9737      that comes first among those still pending.  */
9738 
9739   next = NULL_TREE;
9740   while (elt)
9741     {
9742       if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9743 	{
9744 	  if (tree_int_cst_equal (elt->purpose,
9745 				  constructor_unfilled_index))
9746 	    output_init_element (input_location, elt->value, elt->origtype,
9747 				 true, TREE_TYPE (constructor_type),
9748 				 constructor_unfilled_index, false, false,
9749 				 braced_init_obstack);
9750 	  else if (tree_int_cst_lt (constructor_unfilled_index,
9751 				    elt->purpose))
9752 	    {
9753 	      /* Advance to the next smaller node.  */
9754 	      if (elt->left)
9755 		elt = elt->left;
9756 	      else
9757 		{
9758 		  /* We have reached the smallest node bigger than the
9759 		     current unfilled index.  Fill the space first.  */
9760 		  next = elt->purpose;
9761 		  break;
9762 		}
9763 	    }
9764 	  else
9765 	    {
9766 	      /* Advance to the next bigger node.  */
9767 	      if (elt->right)
9768 		elt = elt->right;
9769 	      else
9770 		{
9771 		  /* We have reached the biggest node in a subtree.  Find
9772 		     the parent of it, which is the next bigger node.  */
9773 		  while (elt->parent && elt->parent->right == elt)
9774 		    elt = elt->parent;
9775 		  elt = elt->parent;
9776 		  if (elt && tree_int_cst_lt (constructor_unfilled_index,
9777 					      elt->purpose))
9778 		    {
9779 		      next = elt->purpose;
9780 		      break;
9781 		    }
9782 		}
9783 	    }
9784 	}
9785       else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9786 	{
9787 	  /* If the current record is complete we are done.  */
9788 	  if (constructor_unfilled_fields == NULL_TREE)
9789 	    break;
9790 
9791 	  int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9792 					 elt->purpose);
9793 	  if (cmp == 0)
9794 	    output_init_element (input_location, elt->value, elt->origtype,
9795 				 true, TREE_TYPE (elt->purpose),
9796 				 elt->purpose, false, false,
9797 				 braced_init_obstack);
9798 	  else if (cmp < 0)
9799 	    {
9800 	      /* Advance to the next smaller node.  */
9801 	      if (elt->left)
9802 		elt = elt->left;
9803 	      else
9804 		{
9805 		  /* We have reached the smallest node bigger than the
9806 		     current unfilled field.  Fill the space first.  */
9807 		  next = elt->purpose;
9808 		  break;
9809 		}
9810 	    }
9811 	  else
9812 	    {
9813 	      /* Advance to the next bigger node.  */
9814 	      if (elt->right)
9815 		elt = elt->right;
9816 	      else
9817 		{
9818 		  /* We have reached the biggest node in a subtree.  Find
9819 		     the parent of it, which is the next bigger node.  */
9820 		  while (elt->parent && elt->parent->right == elt)
9821 		    elt = elt->parent;
9822 		  elt = elt->parent;
9823 		  if (elt
9824 		      && init_field_decl_cmp (constructor_unfilled_fields,
9825 					      elt->purpose) < 0)
9826 		    {
9827 		      next = elt->purpose;
9828 		      break;
9829 		    }
9830 		}
9831 	    }
9832 	}
9833     }
9834 
9835   /* Ordinarily return, but not if we want to output all
9836      and there are elements left.  */
9837   if (!(all && next != NULL_TREE))
9838     return;
9839 
9840   /* If it's not incremental, just skip over the gap, so that after
9841      jumping to retry we will output the next successive element.  */
9842   if (RECORD_OR_UNION_TYPE_P (constructor_type))
9843     constructor_unfilled_fields = next;
9844   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9845     constructor_unfilled_index = next;
9846 
9847   /* ELT now points to the node in the pending tree with the next
9848      initializer to output.  */
9849   goto retry;
9850 }
9851 
9852 /* Add one non-braced element to the current constructor level.
9853    This adjusts the current position within the constructor's type.
9854    This may also start or terminate implicit levels
9855    to handle a partly-braced initializer.
9856 
9857    Once this has found the correct level for the new element,
9858    it calls output_init_element.
9859 
9860    IMPLICIT is true if value comes from pop_init_level (1),
9861    the new initializer has been merged with the existing one
9862    and thus no warnings should be emitted about overriding an
9863    existing initializer.  */
9864 
9865 void
process_init_element(location_t loc,struct c_expr value,bool implicit,struct obstack * braced_init_obstack)9866 process_init_element (location_t loc, struct c_expr value, bool implicit,
9867 		      struct obstack * braced_init_obstack)
9868 {
9869   tree orig_value = value.value;
9870   int string_flag
9871     = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
9872   bool strict_string = value.original_code == STRING_CST;
9873   bool was_designated = designator_depth != 0;
9874 
9875   designator_depth = 0;
9876   designator_erroneous = 0;
9877 
9878   if (!implicit && value.value && !integer_zerop (value.value))
9879     constructor_zeroinit = 0;
9880 
9881   /* Handle superfluous braces around string cst as in
9882      char x[] = {"foo"}; */
9883   if (string_flag
9884       && constructor_type
9885       && !was_designated
9886       && TREE_CODE (constructor_type) == ARRAY_TYPE
9887       && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
9888       && integer_zerop (constructor_unfilled_index))
9889     {
9890       if (constructor_stack->replacement_value.value)
9891 	error_init (loc, "excess elements in char array initializer");
9892       constructor_stack->replacement_value = value;
9893       return;
9894     }
9895 
9896   if (constructor_stack->replacement_value.value != NULL_TREE)
9897     {
9898       error_init (loc, "excess elements in struct initializer");
9899       return;
9900     }
9901 
9902   /* Ignore elements of a brace group if it is entirely superfluous
9903      and has already been diagnosed.  */
9904   if (constructor_type == NULL_TREE)
9905     return;
9906 
9907   if (!implicit && warn_designated_init && !was_designated
9908       && TREE_CODE (constructor_type) == RECORD_TYPE
9909       && lookup_attribute ("designated_init",
9910 			   TYPE_ATTRIBUTES (constructor_type)))
9911     warning_init (loc,
9912 		  OPT_Wdesignated_init,
9913 		  "positional initialization of field "
9914 		  "in %<struct%> declared with %<designated_init%> attribute");
9915 
9916   /* If we've exhausted any levels that didn't have braces,
9917      pop them now.  */
9918   while (constructor_stack->implicit)
9919     {
9920       if (RECORD_OR_UNION_TYPE_P (constructor_type)
9921 	  && constructor_fields == NULL_TREE)
9922 	process_init_element (loc,
9923 			      pop_init_level (loc, 1, braced_init_obstack,
9924 					      last_init_list_comma),
9925 			      true, braced_init_obstack);
9926       else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
9927 		|| VECTOR_TYPE_P (constructor_type))
9928 	       && constructor_max_index
9929 	       && tree_int_cst_lt (constructor_max_index,
9930 				   constructor_index))
9931 	process_init_element (loc,
9932 			      pop_init_level (loc, 1, braced_init_obstack,
9933 					      last_init_list_comma),
9934 			      true, braced_init_obstack);
9935       else
9936 	break;
9937     }
9938 
9939   /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once.  */
9940   if (constructor_range_stack)
9941     {
9942       /* If value is a compound literal and we'll be just using its
9943 	 content, don't put it into a SAVE_EXPR.  */
9944       if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
9945 	  || !require_constant_value)
9946 	{
9947 	  tree semantic_type = NULL_TREE;
9948 	  if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9949 	    {
9950 	      semantic_type = TREE_TYPE (value.value);
9951 	      value.value = TREE_OPERAND (value.value, 0);
9952 	    }
9953 	  value.value = save_expr (value.value);
9954 	  if (semantic_type)
9955 	    value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9956 				  value.value);
9957 	}
9958     }
9959 
9960   while (1)
9961     {
9962       if (TREE_CODE (constructor_type) == RECORD_TYPE)
9963 	{
9964 	  tree fieldtype;
9965 	  enum tree_code fieldcode;
9966 
9967 	  if (constructor_fields == NULL_TREE)
9968 	    {
9969 	      pedwarn_init (loc, 0, "excess elements in struct initializer");
9970 	      break;
9971 	    }
9972 
9973 	  fieldtype = TREE_TYPE (constructor_fields);
9974 	  if (fieldtype != error_mark_node)
9975 	    fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9976 	  fieldcode = TREE_CODE (fieldtype);
9977 
9978 	  /* Error for non-static initialization of a flexible array member.  */
9979 	  if (fieldcode == ARRAY_TYPE
9980 	      && !require_constant_value
9981 	      && TYPE_SIZE (fieldtype) == NULL_TREE
9982 	      && DECL_CHAIN (constructor_fields) == NULL_TREE)
9983 	    {
9984 	      error_init (loc, "non-static initialization of a flexible "
9985 			  "array member");
9986 	      break;
9987 	    }
9988 
9989 	  /* Error for initialization of a flexible array member with
9990 	     a string constant if the structure is in an array.  E.g.:
9991 	     struct S { int x; char y[]; };
9992 	     struct S s[] = { { 1, "foo" } };
9993 	     is invalid.  */
9994 	  if (string_flag
9995 	      && fieldcode == ARRAY_TYPE
9996 	      && constructor_depth > 1
9997 	      && TYPE_SIZE (fieldtype) == NULL_TREE
9998 	      && DECL_CHAIN (constructor_fields) == NULL_TREE)
9999 	    {
10000 	      bool in_array_p = false;
10001 	      for (struct constructor_stack *p = constructor_stack;
10002 		   p && p->type; p = p->next)
10003 		if (TREE_CODE (p->type) == ARRAY_TYPE)
10004 		  {
10005 		    in_array_p = true;
10006 		    break;
10007 		  }
10008 	      if (in_array_p)
10009 		{
10010 		  error_init (loc, "initialization of flexible array "
10011 			      "member in a nested context");
10012 		  break;
10013 		}
10014 	    }
10015 
10016 	  /* Accept a string constant to initialize a subarray.  */
10017 	  if (value.value != NULL_TREE
10018 	      && fieldcode == ARRAY_TYPE
10019 	      && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10020 	      && string_flag)
10021 	    value.value = orig_value;
10022 	  /* Otherwise, if we have come to a subaggregate,
10023 	     and we don't have an element of its type, push into it.  */
10024 	  else if (value.value != NULL_TREE
10025 		   && value.value != error_mark_node
10026 		   && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
10027 		   && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
10028 		       || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
10029 	    {
10030 	      push_init_level (loc, 1, braced_init_obstack);
10031 	      continue;
10032 	    }
10033 
10034 	  if (value.value)
10035 	    {
10036 	      push_member_name (constructor_fields);
10037 	      output_init_element (loc, value.value, value.original_type,
10038 				   strict_string, fieldtype,
10039 				   constructor_fields, true, implicit,
10040 				   braced_init_obstack);
10041 	      RESTORE_SPELLING_DEPTH (constructor_depth);
10042 	    }
10043 	  else
10044 	    /* Do the bookkeeping for an element that was
10045 	       directly output as a constructor.  */
10046 	    {
10047 	      /* For a record, keep track of end position of last field.  */
10048 	      if (DECL_SIZE (constructor_fields))
10049 		constructor_bit_index
10050 		  = size_binop_loc (input_location, PLUS_EXPR,
10051 				    bit_position (constructor_fields),
10052 				    DECL_SIZE (constructor_fields));
10053 
10054 	      /* If the current field was the first one not yet written out,
10055 		 it isn't now, so update.  */
10056 	      if (constructor_unfilled_fields == constructor_fields)
10057 		{
10058 		  constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10059 		  /* Skip any nameless bit fields.  */
10060 		  while (constructor_unfilled_fields != 0
10061 			 && (DECL_UNNAMED_BIT_FIELD
10062 			     (constructor_unfilled_fields)))
10063 		    constructor_unfilled_fields =
10064 		      DECL_CHAIN (constructor_unfilled_fields);
10065 		}
10066 	    }
10067 
10068 	  constructor_fields = DECL_CHAIN (constructor_fields);
10069 	  /* Skip any nameless bit fields at the beginning.  */
10070 	  while (constructor_fields != NULL_TREE
10071 		 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10072 	    constructor_fields = DECL_CHAIN (constructor_fields);
10073 	}
10074       else if (TREE_CODE (constructor_type) == UNION_TYPE)
10075 	{
10076 	  tree fieldtype;
10077 	  enum tree_code fieldcode;
10078 
10079 	  if (constructor_fields == NULL_TREE)
10080 	    {
10081 	      pedwarn_init (loc, 0,
10082 			    "excess elements in union initializer");
10083 	      break;
10084 	    }
10085 
10086 	  fieldtype = TREE_TYPE (constructor_fields);
10087 	  if (fieldtype != error_mark_node)
10088 	    fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10089 	  fieldcode = TREE_CODE (fieldtype);
10090 
10091 	  /* Warn that traditional C rejects initialization of unions.
10092 	     We skip the warning if the value is zero.  This is done
10093 	     under the assumption that the zero initializer in user
10094 	     code appears conditioned on e.g. __STDC__ to avoid
10095 	     "missing initializer" warnings and relies on default
10096 	     initialization to zero in the traditional C case.
10097 	     We also skip the warning if the initializer is designated,
10098 	     again on the assumption that this must be conditional on
10099 	     __STDC__ anyway (and we've already complained about the
10100 	     member-designator already).  */
10101 	  if (!in_system_header_at (input_location) && !constructor_designated
10102 	      && !(value.value && (integer_zerop (value.value)
10103 				   || real_zerop (value.value))))
10104 	    warning (OPT_Wtraditional, "traditional C rejects initialization "
10105 		     "of unions");
10106 
10107 	  /* Accept a string constant to initialize a subarray.  */
10108 	  if (value.value != NULL_TREE
10109 	      && fieldcode == ARRAY_TYPE
10110 	      && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10111 	      && string_flag)
10112 	    value.value = orig_value;
10113 	  /* Otherwise, if we have come to a subaggregate,
10114 	     and we don't have an element of its type, push into it.  */
10115 	  else if (value.value != NULL_TREE
10116 		   && value.value != error_mark_node
10117 		   && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
10118 		   && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
10119 		       || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
10120 	    {
10121 	      push_init_level (loc, 1, braced_init_obstack);
10122 	      continue;
10123 	    }
10124 
10125 	  if (value.value)
10126 	    {
10127 	      push_member_name (constructor_fields);
10128 	      output_init_element (loc, value.value, value.original_type,
10129 				   strict_string, fieldtype,
10130 				   constructor_fields, true, implicit,
10131 				   braced_init_obstack);
10132 	      RESTORE_SPELLING_DEPTH (constructor_depth);
10133 	    }
10134 	  else
10135 	    /* Do the bookkeeping for an element that was
10136 	       directly output as a constructor.  */
10137 	    {
10138 	      constructor_bit_index = DECL_SIZE (constructor_fields);
10139 	      constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10140 	    }
10141 
10142 	  constructor_fields = NULL_TREE;
10143 	}
10144       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10145 	{
10146 	  tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10147 	  enum tree_code eltcode = TREE_CODE (elttype);
10148 
10149 	  /* Accept a string constant to initialize a subarray.  */
10150 	  if (value.value != NULL_TREE
10151 	      && eltcode == ARRAY_TYPE
10152 	      && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10153 	      && string_flag)
10154 	    value.value = orig_value;
10155 	  /* Otherwise, if we have come to a subaggregate,
10156 	     and we don't have an element of its type, push into it.  */
10157 	  else if (value.value != NULL_TREE
10158 		   && value.value != error_mark_node
10159 		   && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
10160 		   && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
10161 		       || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
10162 	    {
10163 	      push_init_level (loc, 1, braced_init_obstack);
10164 	      continue;
10165 	    }
10166 
10167 	  if (constructor_max_index != NULL_TREE
10168 	      && (tree_int_cst_lt (constructor_max_index, constructor_index)
10169 		  || integer_all_onesp (constructor_max_index)))
10170 	    {
10171 	      pedwarn_init (loc, 0,
10172 			    "excess elements in array initializer");
10173 	      break;
10174 	    }
10175 
10176 	  /* Now output the actual element.  */
10177 	  if (value.value)
10178 	    {
10179 	      push_array_bounds (tree_to_uhwi (constructor_index));
10180 	      output_init_element (loc, value.value, value.original_type,
10181 				   strict_string, elttype,
10182 				   constructor_index, true, implicit,
10183 				   braced_init_obstack);
10184 	      RESTORE_SPELLING_DEPTH (constructor_depth);
10185 	    }
10186 
10187 	  constructor_index
10188 	    = size_binop_loc (input_location, PLUS_EXPR,
10189 			      constructor_index, bitsize_one_node);
10190 
10191 	  if (!value.value)
10192 	    /* If we are doing the bookkeeping for an element that was
10193 	       directly output as a constructor, we must update
10194 	       constructor_unfilled_index.  */
10195 	    constructor_unfilled_index = constructor_index;
10196 	}
10197       else if (VECTOR_TYPE_P (constructor_type))
10198 	{
10199 	  tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10200 
10201 	 /* Do a basic check of initializer size.  Note that vectors
10202 	    always have a fixed size derived from their type.  */
10203 	  if (tree_int_cst_lt (constructor_max_index, constructor_index))
10204 	    {
10205 	      pedwarn_init (loc, 0,
10206 			    "excess elements in vector initializer");
10207 	      break;
10208 	    }
10209 
10210 	  /* Now output the actual element.  */
10211 	  if (value.value)
10212 	    {
10213 	      if (TREE_CODE (value.value) == VECTOR_CST)
10214 		elttype = TYPE_MAIN_VARIANT (constructor_type);
10215 	      output_init_element (loc, value.value, value.original_type,
10216 				   strict_string, elttype,
10217 				   constructor_index, true, implicit,
10218 				   braced_init_obstack);
10219 	    }
10220 
10221 	  constructor_index
10222 	    = size_binop_loc (input_location,
10223 			      PLUS_EXPR, constructor_index, bitsize_one_node);
10224 
10225 	  if (!value.value)
10226 	    /* If we are doing the bookkeeping for an element that was
10227 	       directly output as a constructor, we must update
10228 	       constructor_unfilled_index.  */
10229 	    constructor_unfilled_index = constructor_index;
10230 	}
10231 
10232       /* Handle the sole element allowed in a braced initializer
10233 	 for a scalar variable.  */
10234       else if (constructor_type != error_mark_node
10235 	       && constructor_fields == NULL_TREE)
10236 	{
10237 	  pedwarn_init (loc, 0,
10238 			"excess elements in scalar initializer");
10239 	  break;
10240 	}
10241       else
10242 	{
10243 	  if (value.value)
10244 	    output_init_element (loc, value.value, value.original_type,
10245 				 strict_string, constructor_type,
10246 				 NULL_TREE, true, implicit,
10247 				 braced_init_obstack);
10248 	  constructor_fields = NULL_TREE;
10249 	}
10250 
10251       /* Handle range initializers either at this level or anywhere higher
10252 	 in the designator stack.  */
10253       if (constructor_range_stack)
10254 	{
10255 	  struct constructor_range_stack *p, *range_stack;
10256 	  int finish = 0;
10257 
10258 	  range_stack = constructor_range_stack;
10259 	  constructor_range_stack = 0;
10260 	  while (constructor_stack != range_stack->stack)
10261 	    {
10262 	      gcc_assert (constructor_stack->implicit);
10263 	      process_init_element (loc,
10264 				    pop_init_level (loc, 1,
10265 						    braced_init_obstack,
10266 						    last_init_list_comma),
10267 				    true, braced_init_obstack);
10268 	    }
10269 	  for (p = range_stack;
10270 	       !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10271 	       p = p->prev)
10272 	    {
10273 	      gcc_assert (constructor_stack->implicit);
10274 	      process_init_element (loc,
10275 				    pop_init_level (loc, 1,
10276 						    braced_init_obstack,
10277 						    last_init_list_comma),
10278 				    true, braced_init_obstack);
10279 	    }
10280 
10281 	  p->index = size_binop_loc (input_location,
10282 				     PLUS_EXPR, p->index, bitsize_one_node);
10283 	  if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10284 	    finish = 1;
10285 
10286 	  while (1)
10287 	    {
10288 	      constructor_index = p->index;
10289 	      constructor_fields = p->fields;
10290 	      if (finish && p->range_end && p->index == p->range_start)
10291 		{
10292 		  finish = 0;
10293 		  p->prev = 0;
10294 		}
10295 	      p = p->next;
10296 	      if (!p)
10297 		break;
10298 	      finish_implicit_inits (loc, braced_init_obstack);
10299 	      push_init_level (loc, 2, braced_init_obstack);
10300 	      p->stack = constructor_stack;
10301 	      if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10302 		p->index = p->range_start;
10303 	    }
10304 
10305 	  if (!finish)
10306 	    constructor_range_stack = range_stack;
10307 	  continue;
10308 	}
10309 
10310       break;
10311     }
10312 
10313   constructor_range_stack = 0;
10314 }
10315 
10316 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10317    (guaranteed to be 'volatile' or null) and ARGS (represented using
10318    an ASM_EXPR node).  */
10319 tree
build_asm_stmt(bool is_volatile,tree args)10320 build_asm_stmt (bool is_volatile, tree args)
10321 {
10322   if (is_volatile)
10323     ASM_VOLATILE_P (args) = 1;
10324   return add_stmt (args);
10325 }
10326 
10327 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10328    some INPUTS, and some CLOBBERS.  The latter three may be NULL.
10329    SIMPLE indicates whether there was anything at all after the
10330    string in the asm expression -- asm("blah") and asm("blah" : )
10331    are subtly different.  We use a ASM_EXPR node to represent this.
10332    LOC is the location of the asm, and IS_INLINE says whether this
10333    is asm inline.  */
10334 tree
build_asm_expr(location_t loc,tree string,tree outputs,tree inputs,tree clobbers,tree labels,bool simple,bool is_inline)10335 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10336 		tree clobbers, tree labels, bool simple, bool is_inline)
10337 {
10338   tree tail;
10339   tree args;
10340   int i;
10341   const char *constraint;
10342   const char **oconstraints;
10343   bool allows_mem, allows_reg, is_inout;
10344   int ninputs, noutputs;
10345 
10346   ninputs = list_length (inputs);
10347   noutputs = list_length (outputs);
10348   oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10349 
10350   string = resolve_asm_operand_names (string, outputs, inputs, labels);
10351 
10352   /* Remove output conversions that change the type but not the mode.  */
10353   for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10354     {
10355       tree output = TREE_VALUE (tail);
10356 
10357       output = c_fully_fold (output, false, NULL, true);
10358 
10359       /* ??? Really, this should not be here.  Users should be using a
10360 	 proper lvalue, dammit.  But there's a long history of using casts
10361 	 in the output operands.  In cases like longlong.h, this becomes a
10362 	 primitive form of typechecking -- if the cast can be removed, then
10363 	 the output operand had a type of the proper width; otherwise we'll
10364 	 get an error.  Gross, but ...  */
10365       STRIP_NOPS (output);
10366 
10367       if (!lvalue_or_else (loc, output, lv_asm))
10368 	output = error_mark_node;
10369 
10370       if (output != error_mark_node
10371 	  && (TREE_READONLY (output)
10372 	      || TYPE_READONLY (TREE_TYPE (output))
10373 	      || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10374 		  && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10375 	readonly_error (loc, output, lv_asm);
10376 
10377       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10378       oconstraints[i] = constraint;
10379 
10380       if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10381 				   &allows_mem, &allows_reg, &is_inout))
10382 	{
10383 	  /* If the operand is going to end up in memory,
10384 	     mark it addressable.  */
10385 	  if (!allows_reg && !c_mark_addressable (output))
10386 	    output = error_mark_node;
10387 	  if (!(!allows_reg && allows_mem)
10388 	      && output != error_mark_node
10389 	      && VOID_TYPE_P (TREE_TYPE (output)))
10390 	    {
10391 	      error_at (loc, "invalid use of void expression");
10392 	      output = error_mark_node;
10393 	    }
10394 	}
10395       else
10396 	output = error_mark_node;
10397 
10398       TREE_VALUE (tail) = output;
10399     }
10400 
10401   for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10402     {
10403       tree input;
10404 
10405       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10406       input = TREE_VALUE (tail);
10407 
10408       if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10409 				  oconstraints, &allows_mem, &allows_reg))
10410 	{
10411 	  /* If the operand is going to end up in memory,
10412 	     mark it addressable.  */
10413 	  if (!allows_reg && allows_mem)
10414 	    {
10415 	      input = c_fully_fold (input, false, NULL, true);
10416 
10417 	      /* Strip the nops as we allow this case.  FIXME, this really
10418 		 should be rejected or made deprecated.  */
10419 	      STRIP_NOPS (input);
10420 	      if (!c_mark_addressable (input))
10421 		input = error_mark_node;
10422 	    }
10423 	  else
10424 	    {
10425 	      struct c_expr expr;
10426 	      memset (&expr, 0, sizeof (expr));
10427 	      expr.value = input;
10428 	      expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10429 	      input = c_fully_fold (expr.value, false, NULL);
10430 
10431 	      if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10432 		{
10433 		  error_at (loc, "invalid use of void expression");
10434 		  input = error_mark_node;
10435 		}
10436 	    }
10437 	}
10438       else
10439 	input = error_mark_node;
10440 
10441       TREE_VALUE (tail) = input;
10442     }
10443 
10444   /* ASMs with labels cannot have outputs.  This should have been
10445      enforced by the parser.  */
10446   gcc_assert (outputs == NULL || labels == NULL);
10447 
10448   args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10449 
10450   /* asm statements without outputs, including simple ones, are treated
10451      as volatile.  */
10452   ASM_INPUT_P (args) = simple;
10453   ASM_VOLATILE_P (args) = (noutputs == 0);
10454   ASM_INLINE_P (args) = is_inline;
10455 
10456   return args;
10457 }
10458 
10459 /* Generate a goto statement to LABEL.  LOC is the location of the
10460    GOTO.  */
10461 
10462 tree
c_finish_goto_label(location_t loc,tree label)10463 c_finish_goto_label (location_t loc, tree label)
10464 {
10465   tree decl = lookup_label_for_goto (loc, label);
10466   if (!decl)
10467     return NULL_TREE;
10468   TREE_USED (decl) = 1;
10469   {
10470     add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10471     tree t = build1 (GOTO_EXPR, void_type_node, decl);
10472     SET_EXPR_LOCATION (t, loc);
10473     return add_stmt (t);
10474   }
10475 }
10476 
10477 /* Generate a computed goto statement to EXPR.  LOC is the location of
10478    the GOTO.  */
10479 
10480 tree
c_finish_goto_ptr(location_t loc,tree expr)10481 c_finish_goto_ptr (location_t loc, tree expr)
10482 {
10483   tree t;
10484   pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10485   expr = c_fully_fold (expr, false, NULL);
10486   expr = convert (ptr_type_node, expr);
10487   t = build1 (GOTO_EXPR, void_type_node, expr);
10488   SET_EXPR_LOCATION (t, loc);
10489   return add_stmt (t);
10490 }
10491 
10492 /* Generate a C `return' statement.  RETVAL is the expression for what
10493    to return, or a null pointer for `return;' with no value.  LOC is
10494    the location of the return statement, or the location of the expression,
10495    if the statement has any.  If ORIGTYPE is not NULL_TREE, it
10496    is the original type of RETVAL.  */
10497 
10498 tree
c_finish_return(location_t loc,tree retval,tree origtype)10499 c_finish_return (location_t loc, tree retval, tree origtype)
10500 {
10501   tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10502   bool no_warning = false;
10503   bool npc = false;
10504 
10505   /* Use the expansion point to handle cases such as returning NULL
10506      in a function returning void.  */
10507   location_t xloc = expansion_point_location_if_in_system_header (loc);
10508 
10509   if (TREE_THIS_VOLATILE (current_function_decl))
10510     warning_at (xloc, 0,
10511 		"function declared %<noreturn%> has a %<return%> statement");
10512 
10513   if (retval)
10514     {
10515       tree semantic_type = NULL_TREE;
10516       npc = null_pointer_constant_p (retval);
10517       if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10518 	{
10519 	  semantic_type = TREE_TYPE (retval);
10520 	  retval = TREE_OPERAND (retval, 0);
10521 	}
10522       retval = c_fully_fold (retval, false, NULL);
10523       if (semantic_type)
10524 	retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10525     }
10526 
10527   if (!retval)
10528     {
10529       current_function_returns_null = 1;
10530       if ((warn_return_type >= 0 || flag_isoc99)
10531 	  && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10532 	{
10533 	  bool warned_here;
10534 	  if (flag_isoc99)
10535 	    warned_here = pedwarn
10536 	      (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10537 	       "%<return%> with no value, in function returning non-void");
10538 	  else
10539 	    warned_here = warning_at
10540 	      (loc, OPT_Wreturn_type,
10541 	       "%<return%> with no value, in function returning non-void");
10542 	  no_warning = true;
10543 	  if (warned_here)
10544 	    inform (DECL_SOURCE_LOCATION (current_function_decl),
10545 		    "declared here");
10546 	}
10547     }
10548   else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10549     {
10550       current_function_returns_null = 1;
10551       bool warned_here;
10552       if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10553 	warned_here = pedwarn
10554 	  (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10555 	   "%<return%> with a value, in function returning void");
10556       else
10557 	warned_here = pedwarn
10558 	  (xloc, OPT_Wpedantic, "ISO C forbids "
10559 	   "%<return%> with expression, in function returning void");
10560       if (warned_here)
10561 	inform (DECL_SOURCE_LOCATION (current_function_decl),
10562 		"declared here");
10563     }
10564   else
10565     {
10566       tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10567 				       retval, origtype, ic_return,
10568 				       npc, NULL_TREE, NULL_TREE, 0);
10569       tree res = DECL_RESULT (current_function_decl);
10570       tree inner;
10571       bool save;
10572 
10573       current_function_returns_value = 1;
10574       if (t == error_mark_node)
10575 	return NULL_TREE;
10576 
10577       save = in_late_binary_op;
10578       if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10579 	  || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10580 	  || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10581 	      && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10582 		  || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10583 	      && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10584         in_late_binary_op = true;
10585       inner = t = convert (TREE_TYPE (res), t);
10586       in_late_binary_op = save;
10587 
10588       /* Strip any conversions, additions, and subtractions, and see if
10589 	 we are returning the address of a local variable.  Warn if so.  */
10590       while (1)
10591 	{
10592 	  switch (TREE_CODE (inner))
10593 	    {
10594 	    CASE_CONVERT:
10595 	    case NON_LVALUE_EXPR:
10596 	    case PLUS_EXPR:
10597 	    case POINTER_PLUS_EXPR:
10598 	      inner = TREE_OPERAND (inner, 0);
10599 	      continue;
10600 
10601 	    case MINUS_EXPR:
10602 	      /* If the second operand of the MINUS_EXPR has a pointer
10603 		 type (or is converted from it), this may be valid, so
10604 		 don't give a warning.  */
10605 	      {
10606 		tree op1 = TREE_OPERAND (inner, 1);
10607 
10608 		while (!POINTER_TYPE_P (TREE_TYPE (op1))
10609 		       && (CONVERT_EXPR_P (op1)
10610 			   || TREE_CODE (op1) == NON_LVALUE_EXPR))
10611 		  op1 = TREE_OPERAND (op1, 0);
10612 
10613 		if (POINTER_TYPE_P (TREE_TYPE (op1)))
10614 		  break;
10615 
10616 		inner = TREE_OPERAND (inner, 0);
10617 		continue;
10618 	      }
10619 
10620 	    case ADDR_EXPR:
10621 	      inner = TREE_OPERAND (inner, 0);
10622 
10623 	      while (REFERENCE_CLASS_P (inner)
10624 		     && !INDIRECT_REF_P (inner))
10625 		inner = TREE_OPERAND (inner, 0);
10626 
10627 	      if (DECL_P (inner)
10628 		  && !DECL_EXTERNAL (inner)
10629 		  && !TREE_STATIC (inner)
10630 		  && DECL_CONTEXT (inner) == current_function_decl)
10631 		{
10632 		  if (TREE_CODE (inner) == LABEL_DECL)
10633 		    warning_at (loc, OPT_Wreturn_local_addr,
10634 				"function returns address of label");
10635 		  else
10636 		    {
10637 		      warning_at (loc, OPT_Wreturn_local_addr,
10638 				  "function returns address of local variable");
10639 		      tree zero = build_zero_cst (TREE_TYPE (res));
10640 		      t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10641 		    }
10642 		}
10643 	      break;
10644 
10645 	    default:
10646 	      break;
10647 	    }
10648 
10649 	  break;
10650 	}
10651 
10652       retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10653       SET_EXPR_LOCATION (retval, loc);
10654 
10655       if (warn_sequence_point)
10656 	verify_sequence_points (retval);
10657     }
10658 
10659   ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10660   TREE_NO_WARNING (ret_stmt) |= no_warning;
10661   return add_stmt (ret_stmt);
10662 }
10663 
10664 struct c_switch {
10665   /* The SWITCH_EXPR being built.  */
10666   tree switch_expr;
10667 
10668   /* The original type of the testing expression, i.e. before the
10669      default conversion is applied.  */
10670   tree orig_type;
10671 
10672   /* A splay-tree mapping the low element of a case range to the high
10673      element, or NULL_TREE if there is no high element.  Used to
10674      determine whether or not a new case label duplicates an old case
10675      label.  We need a tree, rather than simply a hash table, because
10676      of the GNU case range extension.  */
10677   splay_tree cases;
10678 
10679   /* The bindings at the point of the switch.  This is used for
10680      warnings crossing decls when branching to a case label.  */
10681   struct c_spot_bindings *bindings;
10682 
10683   /* The next node on the stack.  */
10684   struct c_switch *next;
10685 
10686   /* Remember whether the controlling expression had boolean type
10687      before integer promotions for the sake of -Wswitch-bool.  */
10688   bool bool_cond_p;
10689 };
10690 
10691 /* A stack of the currently active switch statements.  The innermost
10692    switch statement is on the top of the stack.  There is no need to
10693    mark the stack for garbage collection because it is only active
10694    during the processing of the body of a function, and we never
10695    collect at that point.  */
10696 
10697 struct c_switch *c_switch_stack;
10698 
10699 /* Start a C switch statement, testing expression EXP.  Return the new
10700    SWITCH_EXPR.  SWITCH_LOC is the location of the `switch'.
10701    SWITCH_COND_LOC is the location of the switch's condition.
10702    EXPLICIT_CAST_P is true if the expression EXP has an explicit cast.  */
10703 
10704 tree
c_start_case(location_t switch_loc,location_t switch_cond_loc,tree exp,bool explicit_cast_p)10705 c_start_case (location_t switch_loc,
10706 	      location_t switch_cond_loc,
10707 	      tree exp, bool explicit_cast_p)
10708 {
10709   tree orig_type = error_mark_node;
10710   bool bool_cond_p = false;
10711   struct c_switch *cs;
10712 
10713   if (exp != error_mark_node)
10714     {
10715       orig_type = TREE_TYPE (exp);
10716 
10717       if (!INTEGRAL_TYPE_P (orig_type))
10718 	{
10719 	  if (orig_type != error_mark_node)
10720 	    {
10721 	      error_at (switch_cond_loc, "switch quantity not an integer");
10722 	      orig_type = error_mark_node;
10723 	    }
10724 	  exp = integer_zero_node;
10725 	}
10726       else
10727 	{
10728 	  tree type = TYPE_MAIN_VARIANT (orig_type);
10729 	  tree e = exp;
10730 
10731 	  /* Warn if the condition has boolean value.  */
10732 	  while (TREE_CODE (e) == COMPOUND_EXPR)
10733 	    e = TREE_OPERAND (e, 1);
10734 
10735 	  if ((TREE_CODE (type) == BOOLEAN_TYPE
10736 	       || truth_value_p (TREE_CODE (e)))
10737 	      /* Explicit cast to int suppresses this warning.  */
10738 	      && !(TREE_CODE (type) == INTEGER_TYPE
10739 		   && explicit_cast_p))
10740 	    bool_cond_p = true;
10741 
10742 	  if (!in_system_header_at (input_location)
10743 	      && (type == long_integer_type_node
10744 		  || type == long_unsigned_type_node))
10745 	    warning_at (switch_cond_loc,
10746 			OPT_Wtraditional, "%<long%> switch expression not "
10747 			"converted to %<int%> in ISO C");
10748 
10749 	  exp = c_fully_fold (exp, false, NULL);
10750 	  exp = default_conversion (exp);
10751 
10752 	  if (warn_sequence_point)
10753 	    verify_sequence_points (exp);
10754 	}
10755     }
10756 
10757   /* Add this new SWITCH_EXPR to the stack.  */
10758   cs = XNEW (struct c_switch);
10759   cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10760   SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10761   cs->orig_type = orig_type;
10762   cs->cases = splay_tree_new (case_compare, NULL, NULL);
10763   cs->bindings = c_get_switch_bindings ();
10764   cs->bool_cond_p = bool_cond_p;
10765   cs->next = c_switch_stack;
10766   c_switch_stack = cs;
10767 
10768   return add_stmt (cs->switch_expr);
10769 }
10770 
10771 /* Process a case label at location LOC.  */
10772 
10773 tree
do_case(location_t loc,tree low_value,tree high_value)10774 do_case (location_t loc, tree low_value, tree high_value)
10775 {
10776   tree label = NULL_TREE;
10777 
10778   if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10779     {
10780       low_value = c_fully_fold (low_value, false, NULL);
10781       if (TREE_CODE (low_value) == INTEGER_CST)
10782 	pedwarn (loc, OPT_Wpedantic,
10783 		 "case label is not an integer constant expression");
10784     }
10785 
10786   if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10787     {
10788       high_value = c_fully_fold (high_value, false, NULL);
10789       if (TREE_CODE (high_value) == INTEGER_CST)
10790 	pedwarn (input_location, OPT_Wpedantic,
10791 		 "case label is not an integer constant expression");
10792     }
10793 
10794   if (c_switch_stack == NULL)
10795     {
10796       if (low_value)
10797 	error_at (loc, "case label not within a switch statement");
10798       else
10799 	error_at (loc, "%<default%> label not within a switch statement");
10800       return NULL_TREE;
10801     }
10802 
10803   if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10804 				    EXPR_LOCATION (c_switch_stack->switch_expr),
10805 				    loc))
10806     return NULL_TREE;
10807 
10808   label = c_add_case_label (loc, c_switch_stack->cases,
10809 			    SWITCH_COND (c_switch_stack->switch_expr),
10810 			    low_value, high_value);
10811   if (label == error_mark_node)
10812     label = NULL_TREE;
10813   return label;
10814 }
10815 
10816 /* Finish the switch statement.  TYPE is the original type of the
10817    controlling expression of the switch, or NULL_TREE.  */
10818 
10819 void
c_finish_case(tree body,tree type)10820 c_finish_case (tree body, tree type)
10821 {
10822   struct c_switch *cs = c_switch_stack;
10823   location_t switch_location;
10824 
10825   SWITCH_BODY (cs->switch_expr) = body;
10826 
10827   /* Emit warnings as needed.  */
10828   switch_location = EXPR_LOCATION (cs->switch_expr);
10829   c_do_switch_warnings (cs->cases, switch_location,
10830 			type ? type : TREE_TYPE (cs->switch_expr),
10831 			SWITCH_COND (cs->switch_expr), cs->bool_cond_p);
10832   if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10833     SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
10834 
10835   /* Pop the stack.  */
10836   c_switch_stack = cs->next;
10837   splay_tree_delete (cs->cases);
10838   c_release_switch_bindings (cs->bindings);
10839   XDELETE (cs);
10840 }
10841 
10842 /* Emit an if statement.  IF_LOCUS is the location of the 'if'.  COND,
10843    THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
10844    may be null.  */
10845 
10846 void
c_finish_if_stmt(location_t if_locus,tree cond,tree then_block,tree else_block)10847 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
10848 		  tree else_block)
10849 {
10850   tree stmt;
10851 
10852   stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
10853   SET_EXPR_LOCATION (stmt, if_locus);
10854   add_stmt (stmt);
10855 }
10856 
10857 /* Emit a general-purpose loop construct.  START_LOCUS is the location of
10858    the beginning of the loop.  COND is the loop condition.  COND_IS_FIRST
10859    is false for DO loops.  INCR is the FOR increment expression.  BODY is
10860    the statement controlled by the loop.  BLAB is the break label.  CLAB is
10861    the continue label.  Everything is allowed to be NULL.  */
10862 
10863 void
c_finish_loop(location_t start_locus,tree cond,tree incr,tree body,tree blab,tree clab,bool cond_is_first)10864 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10865 	       tree blab, tree clab, bool cond_is_first)
10866 {
10867   tree entry = NULL, exit = NULL, t;
10868 
10869   /* If the condition is zero don't generate a loop construct.  */
10870   if (cond && integer_zerop (cond))
10871     {
10872       if (cond_is_first)
10873 	{
10874 	  t = build_and_jump (&blab);
10875 	  SET_EXPR_LOCATION (t, start_locus);
10876 	  add_stmt (t);
10877 	}
10878     }
10879   else
10880     {
10881       tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10882 
10883       /* If we have an exit condition, then we build an IF with gotos either
10884 	 out of the loop, or to the top of it.  If there's no exit condition,
10885 	 then we just build a jump back to the top.  */
10886       exit = build_and_jump (&LABEL_EXPR_LABEL (top));
10887 
10888       if (cond && !integer_nonzerop (cond))
10889 	{
10890 	  /* Canonicalize the loop condition to the end.  This means
10891 	     generating a branch to the loop condition.  Reuse the
10892 	     continue label, if possible.  */
10893 	  if (cond_is_first)
10894 	    {
10895 	      if (incr || !clab)
10896 		{
10897 		  entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10898 		  t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10899 		}
10900 	      else
10901 		t = build1 (GOTO_EXPR, void_type_node, clab);
10902 	      SET_EXPR_LOCATION (t, start_locus);
10903 	      add_stmt (t);
10904 	    }
10905 
10906 	  t = build_and_jump (&blab);
10907 	  if (cond_is_first)
10908 	    exit = fold_build3_loc (start_locus,
10909 				COND_EXPR, void_type_node, cond, exit, t);
10910 	  else
10911 	    exit = fold_build3_loc (input_location,
10912 				COND_EXPR, void_type_node, cond, exit, t);
10913 	}
10914       else
10915 	{
10916 	  /* For the backward-goto's location of an unconditional loop
10917 	     use the beginning of the body, or, if there is none, the
10918 	     top of the loop.  */
10919 	  location_t loc = EXPR_LOCATION (expr_first (body));
10920 	  if (loc == UNKNOWN_LOCATION)
10921 	    loc = start_locus;
10922 	  SET_EXPR_LOCATION (exit, loc);
10923 	}
10924 
10925       add_stmt (top);
10926     }
10927 
10928   if (body)
10929     add_stmt (body);
10930   if (clab)
10931     add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10932   if (incr)
10933     add_stmt (incr);
10934   if (entry)
10935     add_stmt (entry);
10936   if (exit)
10937     add_stmt (exit);
10938   if (blab)
10939     add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
10940 }
10941 
10942 tree
c_finish_bc_stmt(location_t loc,tree * label_p,bool is_break)10943 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
10944 {
10945   bool skip;
10946   tree label = *label_p;
10947 
10948   /* In switch statements break is sometimes stylistically used after
10949      a return statement.  This can lead to spurious warnings about
10950      control reaching the end of a non-void function when it is
10951      inlined.  Note that we are calling block_may_fallthru with
10952      language specific tree nodes; this works because
10953      block_may_fallthru returns true when given something it does not
10954      understand.  */
10955   skip = !block_may_fallthru (cur_stmt_list);
10956 
10957   if (!label)
10958     {
10959       if (!skip)
10960 	*label_p = label = create_artificial_label (loc);
10961     }
10962   else if (TREE_CODE (label) == LABEL_DECL)
10963     ;
10964   else switch (TREE_INT_CST_LOW (label))
10965     {
10966     case 0:
10967       if (is_break)
10968 	error_at (loc, "break statement not within loop or switch");
10969       else
10970 	error_at (loc, "continue statement not within a loop");
10971       return NULL_TREE;
10972 
10973     case 1:
10974       gcc_assert (is_break);
10975       error_at (loc, "break statement used with OpenMP for loop");
10976       return NULL_TREE;
10977 
10978     case 2:
10979       if (is_break)
10980 	error ("break statement within %<#pragma simd%> loop body");
10981       else
10982 	error ("continue statement within %<#pragma simd%> loop body");
10983       return NULL_TREE;
10984 
10985     default:
10986       gcc_unreachable ();
10987     }
10988 
10989   if (skip)
10990     return NULL_TREE;
10991 
10992   if (!is_break)
10993     add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10994 
10995   return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
10996 }
10997 
10998 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr.  */
10999 
11000 static void
emit_side_effect_warnings(location_t loc,tree expr)11001 emit_side_effect_warnings (location_t loc, tree expr)
11002 {
11003   if (expr == error_mark_node)
11004     ;
11005   else if (!TREE_SIDE_EFFECTS (expr))
11006     {
11007       if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
11008 	warning_at (loc, OPT_Wunused_value, "statement with no effect");
11009     }
11010   else if (TREE_CODE (expr) == COMPOUND_EXPR)
11011     {
11012       tree r = expr;
11013       location_t cloc = loc;
11014       while (TREE_CODE (r) == COMPOUND_EXPR)
11015 	{
11016 	  if (EXPR_HAS_LOCATION (r))
11017 	    cloc = EXPR_LOCATION (r);
11018 	  r = TREE_OPERAND (r, 1);
11019 	}
11020       if (!TREE_SIDE_EFFECTS (r)
11021 	  && !VOID_TYPE_P (TREE_TYPE (r))
11022 	  && !CONVERT_EXPR_P (r)
11023 	  && !TREE_NO_WARNING (r)
11024 	  && !TREE_NO_WARNING (expr))
11025 	warning_at (cloc, OPT_Wunused_value,
11026 		    "right-hand operand of comma expression has no effect");
11027     }
11028   else
11029     warn_if_unused_value (expr, loc);
11030 }
11031 
11032 /* Process an expression as if it were a complete statement.  Emit
11033    diagnostics, but do not call ADD_STMT.  LOC is the location of the
11034    statement.  */
11035 
11036 tree
c_process_expr_stmt(location_t loc,tree expr)11037 c_process_expr_stmt (location_t loc, tree expr)
11038 {
11039   tree exprv;
11040 
11041   if (!expr)
11042     return NULL_TREE;
11043 
11044   expr = c_fully_fold (expr, false, NULL);
11045 
11046   if (warn_sequence_point)
11047     verify_sequence_points (expr);
11048 
11049   if (TREE_TYPE (expr) != error_mark_node
11050       && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11051       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11052     error_at (loc, "expression statement has incomplete type");
11053 
11054   /* If we're not processing a statement expression, warn about unused values.
11055      Warnings for statement expressions will be emitted later, once we figure
11056      out which is the result.  */
11057   if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11058       && warn_unused_value)
11059     emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11060 
11061   exprv = expr;
11062   while (TREE_CODE (exprv) == COMPOUND_EXPR)
11063     exprv = TREE_OPERAND (exprv, 1);
11064   while (CONVERT_EXPR_P (exprv))
11065     exprv = TREE_OPERAND (exprv, 0);
11066   if (DECL_P (exprv)
11067       || handled_component_p (exprv)
11068       || TREE_CODE (exprv) == ADDR_EXPR)
11069     mark_exp_read (exprv);
11070 
11071   /* If the expression is not of a type to which we cannot assign a line
11072      number, wrap the thing in a no-op NOP_EXPR.  */
11073   if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11074     {
11075       expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11076       SET_EXPR_LOCATION (expr, loc);
11077     }
11078 
11079   return expr;
11080 }
11081 
11082 /* Emit an expression as a statement.  LOC is the location of the
11083    expression.  */
11084 
11085 tree
c_finish_expr_stmt(location_t loc,tree expr)11086 c_finish_expr_stmt (location_t loc, tree expr)
11087 {
11088   if (expr)
11089     return add_stmt (c_process_expr_stmt (loc, expr));
11090   else
11091     return NULL;
11092 }
11093 
11094 /* Do the opposite and emit a statement as an expression.  To begin,
11095    create a new binding level and return it.  */
11096 
11097 tree
c_begin_stmt_expr(void)11098 c_begin_stmt_expr (void)
11099 {
11100   tree ret;
11101 
11102   /* We must force a BLOCK for this level so that, if it is not expanded
11103      later, there is a way to turn off the entire subtree of blocks that
11104      are contained in it.  */
11105   keep_next_level ();
11106   ret = c_begin_compound_stmt (true);
11107 
11108   c_bindings_start_stmt_expr (c_switch_stack == NULL
11109 			      ? NULL
11110 			      : c_switch_stack->bindings);
11111 
11112   /* Mark the current statement list as belonging to a statement list.  */
11113   STATEMENT_LIST_STMT_EXPR (ret) = 1;
11114 
11115   return ret;
11116 }
11117 
11118 /* LOC is the location of the compound statement to which this body
11119    belongs.  */
11120 
11121 tree
c_finish_stmt_expr(location_t loc,tree body)11122 c_finish_stmt_expr (location_t loc, tree body)
11123 {
11124   tree last, type, tmp, val;
11125   tree *last_p;
11126 
11127   body = c_end_compound_stmt (loc, body, true);
11128 
11129   c_bindings_end_stmt_expr (c_switch_stack == NULL
11130 			    ? NULL
11131 			    : c_switch_stack->bindings);
11132 
11133   /* Locate the last statement in BODY.  See c_end_compound_stmt
11134      about always returning a BIND_EXPR.  */
11135   last_p = &BIND_EXPR_BODY (body);
11136   last = BIND_EXPR_BODY (body);
11137 
11138  continue_searching:
11139   if (TREE_CODE (last) == STATEMENT_LIST)
11140     {
11141       tree_stmt_iterator l = tsi_last (last);
11142 
11143       while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11144 	tsi_prev (&l);
11145 
11146       /* This can happen with degenerate cases like ({ }).  No value.  */
11147       if (tsi_end_p (l))
11148 	return body;
11149 
11150       /* If we're supposed to generate side effects warnings, process
11151 	 all of the statements except the last.  */
11152       if (warn_unused_value)
11153 	{
11154 	  for (tree_stmt_iterator i = tsi_start (last);
11155 	       tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11156 	    {
11157 	      location_t tloc;
11158 	      tree t = tsi_stmt (i);
11159 
11160 	      tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11161 	      emit_side_effect_warnings (tloc, t);
11162 	    }
11163 	}
11164       last_p = tsi_stmt_ptr (l);
11165       last = *last_p;
11166     }
11167 
11168   /* If the end of the list is exception related, then the list was split
11169      by a call to push_cleanup.  Continue searching.  */
11170   if (TREE_CODE (last) == TRY_FINALLY_EXPR
11171       || TREE_CODE (last) == TRY_CATCH_EXPR)
11172     {
11173       last_p = &TREE_OPERAND (last, 0);
11174       last = *last_p;
11175       goto continue_searching;
11176     }
11177 
11178   if (last == error_mark_node)
11179     return last;
11180 
11181   /* In the case that the BIND_EXPR is not necessary, return the
11182      expression out from inside it.  */
11183   if ((last == BIND_EXPR_BODY (body)
11184        /* Skip nested debug stmts.  */
11185        || last == expr_first (BIND_EXPR_BODY (body)))
11186       && BIND_EXPR_VARS (body) == NULL)
11187     {
11188       /* Even if this looks constant, do not allow it in a constant
11189 	 expression.  */
11190       last = c_wrap_maybe_const (last, true);
11191       /* Do not warn if the return value of a statement expression is
11192 	 unused.  */
11193       TREE_NO_WARNING (last) = 1;
11194       return last;
11195     }
11196 
11197   /* Extract the type of said expression.  */
11198   type = TREE_TYPE (last);
11199 
11200   /* If we're not returning a value at all, then the BIND_EXPR that
11201      we already have is a fine expression to return.  */
11202   if (!type || VOID_TYPE_P (type))
11203     return body;
11204 
11205   /* Now that we've located the expression containing the value, it seems
11206      silly to make voidify_wrapper_expr repeat the process.  Create a
11207      temporary of the appropriate type and stick it in a TARGET_EXPR.  */
11208   tmp = create_tmp_var_raw (type);
11209 
11210   /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt.  This avoids
11211      tree_expr_nonnegative_p giving up immediately.  */
11212   val = last;
11213   if (TREE_CODE (val) == NOP_EXPR
11214       && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11215     val = TREE_OPERAND (val, 0);
11216 
11217   *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11218   SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11219 
11220   {
11221     tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11222     SET_EXPR_LOCATION (t, loc);
11223     return t;
11224   }
11225 }
11226 
11227 /* Begin and end compound statements.  This is as simple as pushing
11228    and popping new statement lists from the tree.  */
11229 
11230 tree
c_begin_compound_stmt(bool do_scope)11231 c_begin_compound_stmt (bool do_scope)
11232 {
11233   tree stmt = push_stmt_list ();
11234   if (do_scope)
11235     push_scope ();
11236   return stmt;
11237 }
11238 
11239 /* End a compound statement.  STMT is the statement.  LOC is the
11240    location of the compound statement-- this is usually the location
11241    of the opening brace.  */
11242 
11243 tree
c_end_compound_stmt(location_t loc,tree stmt,bool do_scope)11244 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11245 {
11246   tree block = NULL;
11247 
11248   if (do_scope)
11249     {
11250       if (c_dialect_objc ())
11251 	objc_clear_super_receiver ();
11252       block = pop_scope ();
11253     }
11254 
11255   stmt = pop_stmt_list (stmt);
11256   stmt = c_build_bind_expr (loc, block, stmt);
11257 
11258   /* If this compound statement is nested immediately inside a statement
11259      expression, then force a BIND_EXPR to be created.  Otherwise we'll
11260      do the wrong thing for ({ { 1; } }) or ({ 1; { } }).  In particular,
11261      STATEMENT_LISTs merge, and thus we can lose track of what statement
11262      was really last.  */
11263   if (building_stmt_list_p ()
11264       && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11265       && TREE_CODE (stmt) != BIND_EXPR)
11266     {
11267       stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11268       TREE_SIDE_EFFECTS (stmt) = 1;
11269       SET_EXPR_LOCATION (stmt, loc);
11270     }
11271 
11272   return stmt;
11273 }
11274 
11275 /* Queue a cleanup.  CLEANUP is an expression/statement to be executed
11276    when the current scope is exited.  EH_ONLY is true when this is not
11277    meant to apply to normal control flow transfer.  */
11278 
11279 void
push_cleanup(tree decl,tree cleanup,bool eh_only)11280 push_cleanup (tree decl, tree cleanup, bool eh_only)
11281 {
11282   enum tree_code code;
11283   tree stmt, list;
11284   bool stmt_expr;
11285 
11286   code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11287   stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11288   add_stmt (stmt);
11289   stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11290   list = push_stmt_list ();
11291   TREE_OPERAND (stmt, 0) = list;
11292   STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11293 }
11294 
11295 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11296    into a value of TYPE type.  Comparison is done via VEC_COND_EXPR.  */
11297 
11298 static tree
build_vec_cmp(tree_code code,tree type,tree arg0,tree arg1)11299 build_vec_cmp (tree_code code, tree type,
11300 	       tree arg0, tree arg1)
11301 {
11302   tree zero_vec = build_zero_cst (type);
11303   tree minus_one_vec = build_minus_one_cst (type);
11304   tree cmp_type = build_same_sized_truth_vector_type (type);
11305   tree cmp = build2 (code, cmp_type, arg0, arg1);
11306   return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11307 }
11308 
11309 /* Build a binary-operation expression without default conversions.
11310    CODE is the kind of expression to build.
11311    LOCATION is the operator's location.
11312    This function differs from `build' in several ways:
11313    the data type of the result is computed and recorded in it,
11314    warnings are generated if arg data types are invalid,
11315    special handling for addition and subtraction of pointers is known,
11316    and some optimization is done (operations on narrow ints
11317    are done in the narrower type when that gives the same result).
11318    Constant folding is also done before the result is returned.
11319 
11320    Note that the operands will never have enumeral types, or function
11321    or array types, because either they will have the default conversions
11322    performed or they have both just been converted to some other type in which
11323    the arithmetic is to be done.  */
11324 
11325 tree
build_binary_op(location_t location,enum tree_code code,tree orig_op0,tree orig_op1,bool convert_p)11326 build_binary_op (location_t location, enum tree_code code,
11327 		 tree orig_op0, tree orig_op1, bool convert_p)
11328 {
11329   tree type0, type1, orig_type0, orig_type1;
11330   tree eptype;
11331   enum tree_code code0, code1;
11332   tree op0, op1;
11333   tree ret = error_mark_node;
11334   const char *invalid_op_diag;
11335   bool op0_int_operands, op1_int_operands;
11336   bool int_const, int_const_or_overflow, int_operands;
11337 
11338   /* Expression code to give to the expression when it is built.
11339      Normally this is CODE, which is what the caller asked for,
11340      but in some special cases we change it.  */
11341   enum tree_code resultcode = code;
11342 
11343   /* Data type in which the computation is to be performed.
11344      In the simplest cases this is the common type of the arguments.  */
11345   tree result_type = NULL;
11346 
11347   /* When the computation is in excess precision, the type of the
11348      final EXCESS_PRECISION_EXPR.  */
11349   tree semantic_result_type = NULL;
11350 
11351   /* Nonzero means operands have already been type-converted
11352      in whatever way is necessary.
11353      Zero means they need to be converted to RESULT_TYPE.  */
11354   int converted = 0;
11355 
11356   /* Nonzero means create the expression with this type, rather than
11357      RESULT_TYPE.  */
11358   tree build_type = NULL_TREE;
11359 
11360   /* Nonzero means after finally constructing the expression
11361      convert it to this type.  */
11362   tree final_type = NULL_TREE;
11363 
11364   /* Nonzero if this is an operation like MIN or MAX which can
11365      safely be computed in short if both args are promoted shorts.
11366      Also implies COMMON.
11367      -1 indicates a bitwise operation; this makes a difference
11368      in the exact conditions for when it is safe to do the operation
11369      in a narrower mode.  */
11370   int shorten = 0;
11371 
11372   /* Nonzero if this is a comparison operation;
11373      if both args are promoted shorts, compare the original shorts.
11374      Also implies COMMON.  */
11375   int short_compare = 0;
11376 
11377   /* Nonzero if this is a right-shift operation, which can be computed on the
11378      original short and then promoted if the operand is a promoted short.  */
11379   int short_shift = 0;
11380 
11381   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
11382   int common = 0;
11383 
11384   /* True means types are compatible as far as ObjC is concerned.  */
11385   bool objc_ok;
11386 
11387   /* True means this is an arithmetic operation that may need excess
11388      precision.  */
11389   bool may_need_excess_precision;
11390 
11391   /* True means this is a boolean operation that converts both its
11392      operands to truth-values.  */
11393   bool boolean_op = false;
11394 
11395   /* Remember whether we're doing / or %.  */
11396   bool doing_div_or_mod = false;
11397 
11398   /* Remember whether we're doing << or >>.  */
11399   bool doing_shift = false;
11400 
11401   /* Tree holding instrumentation expression.  */
11402   tree instrument_expr = NULL;
11403 
11404   if (location == UNKNOWN_LOCATION)
11405     location = input_location;
11406 
11407   op0 = orig_op0;
11408   op1 = orig_op1;
11409 
11410   op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11411   if (op0_int_operands)
11412     op0 = remove_c_maybe_const_expr (op0);
11413   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11414   if (op1_int_operands)
11415     op1 = remove_c_maybe_const_expr (op1);
11416   int_operands = (op0_int_operands && op1_int_operands);
11417   if (int_operands)
11418     {
11419       int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11420 			       && TREE_CODE (orig_op1) == INTEGER_CST);
11421       int_const = (int_const_or_overflow
11422 		   && !TREE_OVERFLOW (orig_op0)
11423 		   && !TREE_OVERFLOW (orig_op1));
11424     }
11425   else
11426     int_const = int_const_or_overflow = false;
11427 
11428   /* Do not apply default conversion in mixed vector/scalar expression.  */
11429   if (convert_p
11430       && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11431     {
11432       op0 = default_conversion (op0);
11433       op1 = default_conversion (op1);
11434     }
11435 
11436   orig_type0 = type0 = TREE_TYPE (op0);
11437 
11438   orig_type1 = type1 = TREE_TYPE (op1);
11439 
11440   /* The expression codes of the data types of the arguments tell us
11441      whether the arguments are integers, floating, pointers, etc.  */
11442   code0 = TREE_CODE (type0);
11443   code1 = TREE_CODE (type1);
11444 
11445   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
11446   STRIP_TYPE_NOPS (op0);
11447   STRIP_TYPE_NOPS (op1);
11448 
11449   /* If an error was already reported for one of the arguments,
11450      avoid reporting another error.  */
11451 
11452   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11453     return error_mark_node;
11454 
11455   if (code0 == POINTER_TYPE
11456       && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11457     return error_mark_node;
11458 
11459   if (code1 == POINTER_TYPE
11460       && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11461     return error_mark_node;
11462 
11463   if ((invalid_op_diag
11464        = targetm.invalid_binary_op (code, type0, type1)))
11465     {
11466       error_at (location, invalid_op_diag);
11467       return error_mark_node;
11468     }
11469 
11470   switch (code)
11471     {
11472     case PLUS_EXPR:
11473     case MINUS_EXPR:
11474     case MULT_EXPR:
11475     case TRUNC_DIV_EXPR:
11476     case CEIL_DIV_EXPR:
11477     case FLOOR_DIV_EXPR:
11478     case ROUND_DIV_EXPR:
11479     case EXACT_DIV_EXPR:
11480       may_need_excess_precision = true;
11481       break;
11482 
11483     case EQ_EXPR:
11484     case NE_EXPR:
11485     case LE_EXPR:
11486     case GE_EXPR:
11487     case LT_EXPR:
11488     case GT_EXPR:
11489       /* Excess precision for implicit conversions of integers to
11490 	 floating point in C11 and later.  */
11491       may_need_excess_precision = (flag_isoc11
11492 				   && (ANY_INTEGRAL_TYPE_P (type0)
11493 				       || ANY_INTEGRAL_TYPE_P (type1)));
11494       break;
11495 
11496     default:
11497       may_need_excess_precision = false;
11498       break;
11499     }
11500   if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11501     {
11502       op0 = TREE_OPERAND (op0, 0);
11503       type0 = TREE_TYPE (op0);
11504     }
11505   else if (may_need_excess_precision
11506 	   && (eptype = excess_precision_type (type0)) != NULL_TREE)
11507     {
11508       type0 = eptype;
11509       op0 = convert (eptype, op0);
11510     }
11511   if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11512     {
11513       op1 = TREE_OPERAND (op1, 0);
11514       type1 = TREE_TYPE (op1);
11515     }
11516   else if (may_need_excess_precision
11517 	   && (eptype = excess_precision_type (type1)) != NULL_TREE)
11518     {
11519       type1 = eptype;
11520       op1 = convert (eptype, op1);
11521     }
11522 
11523   objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11524 
11525   /* In case when one of the operands of the binary operation is
11526      a vector and another is a scalar -- convert scalar to vector.  */
11527   if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11528     {
11529       enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11530 						     true);
11531 
11532       switch (convert_flag)
11533 	{
11534 	  case stv_error:
11535 	    return error_mark_node;
11536 	  case stv_firstarg:
11537 	    {
11538               bool maybe_const = true;
11539               tree sc;
11540               sc = c_fully_fold (op0, false, &maybe_const);
11541               sc = save_expr (sc);
11542               sc = convert (TREE_TYPE (type1), sc);
11543               op0 = build_vector_from_val (type1, sc);
11544               if (!maybe_const)
11545                 op0 = c_wrap_maybe_const (op0, true);
11546               orig_type0 = type0 = TREE_TYPE (op0);
11547               code0 = TREE_CODE (type0);
11548               converted = 1;
11549               break;
11550 	    }
11551 	  case stv_secondarg:
11552 	    {
11553 	      bool maybe_const = true;
11554 	      tree sc;
11555 	      sc = c_fully_fold (op1, false, &maybe_const);
11556 	      sc = save_expr (sc);
11557 	      sc = convert (TREE_TYPE (type0), sc);
11558 	      op1 = build_vector_from_val (type0, sc);
11559 	      if (!maybe_const)
11560 		op1 = c_wrap_maybe_const (op1, true);
11561 	      orig_type1 = type1 = TREE_TYPE (op1);
11562 	      code1 = TREE_CODE (type1);
11563 	      converted = 1;
11564 	      break;
11565 	    }
11566 	  default:
11567 	    break;
11568 	}
11569     }
11570 
11571   switch (code)
11572     {
11573     case PLUS_EXPR:
11574       /* Handle the pointer + int case.  */
11575       if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11576 	{
11577 	  ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11578 	  goto return_build_binary_op;
11579 	}
11580       else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11581 	{
11582 	  ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11583 	  goto return_build_binary_op;
11584 	}
11585       else
11586 	common = 1;
11587       break;
11588 
11589     case MINUS_EXPR:
11590       /* Subtraction of two similar pointers.
11591 	 We must subtract them as integers, then divide by object size.  */
11592       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11593 	  && comp_target_types (location, type0, type1))
11594 	{
11595 	  ret = pointer_diff (location, op0, op1, &instrument_expr);
11596 	  goto return_build_binary_op;
11597 	}
11598       /* Handle pointer minus int.  Just like pointer plus int.  */
11599       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11600 	{
11601 	  ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11602 	  goto return_build_binary_op;
11603 	}
11604       else
11605 	common = 1;
11606       break;
11607 
11608     case MULT_EXPR:
11609       common = 1;
11610       break;
11611 
11612     case TRUNC_DIV_EXPR:
11613     case CEIL_DIV_EXPR:
11614     case FLOOR_DIV_EXPR:
11615     case ROUND_DIV_EXPR:
11616     case EXACT_DIV_EXPR:
11617       doing_div_or_mod = true;
11618       warn_for_div_by_zero (location, op1);
11619 
11620       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11621 	   || code0 == FIXED_POINT_TYPE
11622 	   || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11623 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11624 	      || code1 == FIXED_POINT_TYPE
11625 	      || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
11626 	{
11627 	  enum tree_code tcode0 = code0, tcode1 = code1;
11628 
11629 	  if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11630 	    tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11631 	  if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11632 	    tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11633 
11634 	  if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11635 	      || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11636 	    resultcode = RDIV_EXPR;
11637 	  else
11638 	    /* Although it would be tempting to shorten always here, that
11639 	       loses on some targets, since the modulo instruction is
11640 	       undefined if the quotient can't be represented in the
11641 	       computation mode.  We shorten only if unsigned or if
11642 	       dividing by something we know != -1.  */
11643 	    shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11644 		       || (TREE_CODE (op1) == INTEGER_CST
11645 			   && !integer_all_onesp (op1)));
11646 	  common = 1;
11647 	}
11648       break;
11649 
11650     case BIT_AND_EXPR:
11651     case BIT_IOR_EXPR:
11652     case BIT_XOR_EXPR:
11653       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11654 	shorten = -1;
11655       /* Allow vector types which are not floating point types.   */
11656       else if (code0 == VECTOR_TYPE
11657 	       && code1 == VECTOR_TYPE
11658 	       && !VECTOR_FLOAT_TYPE_P (type0)
11659 	       && !VECTOR_FLOAT_TYPE_P (type1))
11660 	common = 1;
11661       break;
11662 
11663     case TRUNC_MOD_EXPR:
11664     case FLOOR_MOD_EXPR:
11665       doing_div_or_mod = true;
11666       warn_for_div_by_zero (location, op1);
11667 
11668       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11669 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11670 	  && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11671 	common = 1;
11672       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11673 	{
11674 	  /* Although it would be tempting to shorten always here, that loses
11675 	     on some targets, since the modulo instruction is undefined if the
11676 	     quotient can't be represented in the computation mode.  We shorten
11677 	     only if unsigned or if dividing by something we know != -1.  */
11678 	  shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11679 		     || (TREE_CODE (op1) == INTEGER_CST
11680 			 && !integer_all_onesp (op1)));
11681 	  common = 1;
11682 	}
11683       break;
11684 
11685     case TRUTH_ANDIF_EXPR:
11686     case TRUTH_ORIF_EXPR:
11687     case TRUTH_AND_EXPR:
11688     case TRUTH_OR_EXPR:
11689     case TRUTH_XOR_EXPR:
11690       if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11691 	   || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11692 	   || code0 == FIXED_POINT_TYPE)
11693 	  && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11694 	      || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11695 	      || code1 == FIXED_POINT_TYPE))
11696 	{
11697 	  /* Result of these operations is always an int,
11698 	     but that does not mean the operands should be
11699 	     converted to ints!  */
11700 	  result_type = integer_type_node;
11701 	  if (op0_int_operands)
11702 	    {
11703 	      op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11704 	      op0 = remove_c_maybe_const_expr (op0);
11705 	    }
11706 	  else
11707 	    op0 = c_objc_common_truthvalue_conversion (location, op0);
11708 	  if (op1_int_operands)
11709 	    {
11710 	      op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11711 	      op1 = remove_c_maybe_const_expr (op1);
11712 	    }
11713 	  else
11714 	    op1 = c_objc_common_truthvalue_conversion (location, op1);
11715 	  converted = 1;
11716 	  boolean_op = true;
11717 	}
11718       if (code == TRUTH_ANDIF_EXPR)
11719 	{
11720 	  int_const_or_overflow = (int_operands
11721 				   && TREE_CODE (orig_op0) == INTEGER_CST
11722 				   && (op0 == truthvalue_false_node
11723 				       || TREE_CODE (orig_op1) == INTEGER_CST));
11724 	  int_const = (int_const_or_overflow
11725 		       && !TREE_OVERFLOW (orig_op0)
11726 		       && (op0 == truthvalue_false_node
11727 			   || !TREE_OVERFLOW (orig_op1)));
11728 	}
11729       else if (code == TRUTH_ORIF_EXPR)
11730 	{
11731 	  int_const_or_overflow = (int_operands
11732 				   && TREE_CODE (orig_op0) == INTEGER_CST
11733 				   && (op0 == truthvalue_true_node
11734 				       || TREE_CODE (orig_op1) == INTEGER_CST));
11735 	  int_const = (int_const_or_overflow
11736 		       && !TREE_OVERFLOW (orig_op0)
11737 		       && (op0 == truthvalue_true_node
11738 			   || !TREE_OVERFLOW (orig_op1)));
11739 	}
11740       break;
11741 
11742       /* Shift operations: result has same type as first operand;
11743 	 always convert second operand to int.
11744 	 Also set SHORT_SHIFT if shifting rightward.  */
11745 
11746     case RSHIFT_EXPR:
11747       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11748 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11749 	  && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11750 	  && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11751 		       TYPE_VECTOR_SUBPARTS (type1)))
11752 	{
11753 	  result_type = type0;
11754 	  converted = 1;
11755 	}
11756       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11757 		|| (code0 == VECTOR_TYPE
11758 		    && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11759 	       && code1 == INTEGER_TYPE)
11760 	{
11761 	  doing_shift = true;
11762 	  if (TREE_CODE (op1) == INTEGER_CST)
11763 	    {
11764 	      if (tree_int_cst_sgn (op1) < 0)
11765 		{
11766 		  int_const = false;
11767 		  if (c_inhibit_evaluation_warnings == 0)
11768 		    warning_at (location, OPT_Wshift_count_negative,
11769 				"right shift count is negative");
11770 		}
11771 	      else if (code0 == VECTOR_TYPE)
11772 		{
11773 		  if (compare_tree_int (op1,
11774 					TYPE_PRECISION (TREE_TYPE (type0)))
11775 		      >= 0)
11776 		    {
11777 		      int_const = false;
11778 		      if (c_inhibit_evaluation_warnings == 0)
11779 			warning_at (location, OPT_Wshift_count_overflow,
11780 				    "right shift count >= width of vector element");
11781 		    }
11782 		}
11783 	      else
11784 		{
11785 		  if (!integer_zerop (op1))
11786 		    short_shift = 1;
11787 
11788 		  if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11789 		    {
11790 		      int_const = false;
11791 		      if (c_inhibit_evaluation_warnings == 0)
11792 			warning_at (location, OPT_Wshift_count_overflow,
11793 				    "right shift count >= width of type");
11794 		    }
11795 		}
11796 	    }
11797 
11798 	  /* Use the type of the value to be shifted.  */
11799 	  result_type = type0;
11800 	  /* Avoid converting op1 to result_type later.  */
11801 	  converted = 1;
11802 	}
11803       break;
11804 
11805     case LSHIFT_EXPR:
11806       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11807 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11808 	  && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11809 	  && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11810 		       TYPE_VECTOR_SUBPARTS (type1)))
11811 	{
11812 	  result_type = type0;
11813 	  converted = 1;
11814 	}
11815       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11816 		|| (code0 == VECTOR_TYPE
11817 		    && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11818 	       && code1 == INTEGER_TYPE)
11819 	{
11820 	  doing_shift = true;
11821 	  if (TREE_CODE (op0) == INTEGER_CST
11822 	      && tree_int_cst_sgn (op0) < 0)
11823 	    {
11824 	      /* Don't reject a left shift of a negative value in a context
11825 		 where a constant expression is needed in C90.  */
11826 	      if (flag_isoc99)
11827 		int_const = false;
11828 	      if (c_inhibit_evaluation_warnings == 0)
11829 		warning_at (location, OPT_Wshift_negative_value,
11830 			    "left shift of negative value");
11831 	    }
11832 	  if (TREE_CODE (op1) == INTEGER_CST)
11833 	    {
11834 	      if (tree_int_cst_sgn (op1) < 0)
11835 		{
11836 		  int_const = false;
11837 		  if (c_inhibit_evaluation_warnings == 0)
11838 		    warning_at (location, OPT_Wshift_count_negative,
11839 				"left shift count is negative");
11840 		}
11841 	      else if (code0 == VECTOR_TYPE)
11842 		{
11843 		  if (compare_tree_int (op1,
11844 					TYPE_PRECISION (TREE_TYPE (type0)))
11845 		      >= 0)
11846 		    {
11847 		      int_const = false;
11848 		      if (c_inhibit_evaluation_warnings == 0)
11849 			warning_at (location, OPT_Wshift_count_overflow,
11850 				    "left shift count >= width of vector element");
11851 		    }
11852 		}
11853 	      else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11854 		{
11855 		  int_const = false;
11856 		  if (c_inhibit_evaluation_warnings == 0)
11857 		    warning_at (location, OPT_Wshift_count_overflow,
11858 				"left shift count >= width of type");
11859 		}
11860 	      else if (TREE_CODE (op0) == INTEGER_CST
11861 		       && maybe_warn_shift_overflow (location, op0, op1)
11862 		       && flag_isoc99)
11863 		int_const = false;
11864 	    }
11865 
11866 	  /* Use the type of the value to be shifted.  */
11867 	  result_type = type0;
11868 	  /* Avoid converting op1 to result_type later.  */
11869 	  converted = 1;
11870 	}
11871       break;
11872 
11873     case EQ_EXPR:
11874     case NE_EXPR:
11875       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11876         {
11877           tree intt;
11878 	  if (!vector_types_compatible_elements_p (type0, type1))
11879             {
11880               error_at (location, "comparing vectors with different "
11881                                   "element types");
11882               return error_mark_node;
11883             }
11884 
11885 	  if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11886 			TYPE_VECTOR_SUBPARTS (type1)))
11887             {
11888               error_at (location, "comparing vectors with different "
11889                                   "number of elements");
11890               return error_mark_node;
11891             }
11892 
11893 	  /* It's not precisely specified how the usual arithmetic
11894 	     conversions apply to the vector types.  Here, we use
11895 	     the unsigned type if one of the operands is signed and
11896 	     the other one is unsigned.  */
11897 	  if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11898 	    {
11899 	      if (!TYPE_UNSIGNED (type0))
11900 		op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11901 	      else
11902 		op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11903 	      warning_at (location, OPT_Wsign_compare, "comparison between "
11904 			  "types %qT and %qT", type0, type1);
11905 	    }
11906 
11907           /* Always construct signed integer vector type.  */
11908           intt = c_common_type_for_size (GET_MODE_BITSIZE
11909 					 (SCALAR_TYPE_MODE
11910 					  (TREE_TYPE (type0))), 0);
11911 	  if (!intt)
11912 	    {
11913 	      error_at (location, "could not find an integer type "
11914 				  "of the same size as %qT",
11915 			TREE_TYPE (type0));
11916 	      return error_mark_node;
11917 	    }
11918           result_type = build_opaque_vector_type (intt,
11919 						  TYPE_VECTOR_SUBPARTS (type0));
11920           converted = 1;
11921 	  ret = build_vec_cmp (resultcode, result_type, op0, op1);
11922 	  goto return_build_binary_op;
11923         }
11924       if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
11925 	warning_at (location,
11926 		    OPT_Wfloat_equal,
11927 		    "comparing floating point with == or != is unsafe");
11928       /* Result of comparison is always int,
11929 	 but don't convert the args to int!  */
11930       build_type = integer_type_node;
11931       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11932 	   || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
11933 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11934 	      || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
11935 	short_compare = 1;
11936       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11937 	{
11938 	  if (TREE_CODE (op0) == ADDR_EXPR
11939 	      && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11940 	      && !from_macro_expansion_at (location))
11941 	    {
11942 	      if (code == EQ_EXPR)
11943 		warning_at (location,
11944 			    OPT_Waddress,
11945 			    "the comparison will always evaluate as %<false%> "
11946 			    "for the address of %qD will never be NULL",
11947 			    TREE_OPERAND (op0, 0));
11948 	      else
11949 		warning_at (location,
11950 			    OPT_Waddress,
11951 			    "the comparison will always evaluate as %<true%> "
11952 			    "for the address of %qD will never be NULL",
11953 			    TREE_OPERAND (op0, 0));
11954 	    }
11955 	  result_type = type0;
11956 	}
11957       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11958 	{
11959 	  if (TREE_CODE (op1) == ADDR_EXPR
11960 	      && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11961 	      && !from_macro_expansion_at (location))
11962 	    {
11963 	      if (code == EQ_EXPR)
11964 		warning_at (location,
11965 			    OPT_Waddress,
11966 			    "the comparison will always evaluate as %<false%> "
11967 			    "for the address of %qD will never be NULL",
11968 			    TREE_OPERAND (op1, 0));
11969 	      else
11970 		warning_at (location,
11971 			    OPT_Waddress,
11972 			    "the comparison will always evaluate as %<true%> "
11973 			    "for the address of %qD will never be NULL",
11974 			    TREE_OPERAND (op1, 0));
11975 	    }
11976 	  result_type = type1;
11977 	}
11978       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11979 	{
11980 	  tree tt0 = TREE_TYPE (type0);
11981 	  tree tt1 = TREE_TYPE (type1);
11982 	  addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11983 	  addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11984 	  addr_space_t as_common = ADDR_SPACE_GENERIC;
11985 
11986 	  /* Anything compares with void *.  void * compares with anything.
11987 	     Otherwise, the targets must be compatible
11988 	     and both must be object or both incomplete.  */
11989 	  if (comp_target_types (location, type0, type1))
11990 	    result_type = common_pointer_type (type0, type1);
11991 	  else if (!addr_space_superset (as0, as1, &as_common))
11992 	    {
11993 	      error_at (location, "comparison of pointers to "
11994 			"disjoint address spaces");
11995 	      return error_mark_node;
11996 	    }
11997 	  else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
11998 	    {
11999 	      if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12000 		pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12001 			 "comparison of %<void *%> with function pointer");
12002 	    }
12003 	  else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12004 	    {
12005 	      if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12006 		pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12007 			 "comparison of %<void *%> with function pointer");
12008 	    }
12009 	  else
12010 	    /* Avoid warning about the volatile ObjC EH puts on decls.  */
12011 	    if (!objc_ok)
12012 	      pedwarn (location, 0,
12013 		       "comparison of distinct pointer types lacks a cast");
12014 
12015 	  if (result_type == NULL_TREE)
12016 	    {
12017 	      int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12018 	      result_type = build_pointer_type
12019 			      (build_qualified_type (void_type_node, qual));
12020 	    }
12021 	}
12022       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12023 	{
12024 	  result_type = type0;
12025 	  pedwarn (location, 0, "comparison between pointer and integer");
12026 	}
12027       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12028 	{
12029 	  result_type = type1;
12030 	  pedwarn (location, 0, "comparison between pointer and integer");
12031 	}
12032       if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12033 	   || truth_value_p (TREE_CODE (orig_op0)))
12034 	  ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12035 	     || truth_value_p (TREE_CODE (orig_op1))))
12036 	maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12037       break;
12038 
12039     case LE_EXPR:
12040     case GE_EXPR:
12041     case LT_EXPR:
12042     case GT_EXPR:
12043       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
12044         {
12045           tree intt;
12046 	  if (!vector_types_compatible_elements_p (type0, type1))
12047             {
12048               error_at (location, "comparing vectors with different "
12049                                   "element types");
12050               return error_mark_node;
12051             }
12052 
12053 	  if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12054 			TYPE_VECTOR_SUBPARTS (type1)))
12055             {
12056               error_at (location, "comparing vectors with different "
12057                                   "number of elements");
12058               return error_mark_node;
12059             }
12060 
12061 	  /* It's not precisely specified how the usual arithmetic
12062 	     conversions apply to the vector types.  Here, we use
12063 	     the unsigned type if one of the operands is signed and
12064 	     the other one is unsigned.  */
12065 	  if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12066 	    {
12067 	      if (!TYPE_UNSIGNED (type0))
12068 		op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12069 	      else
12070 		op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12071 	      warning_at (location, OPT_Wsign_compare, "comparison between "
12072 			  "types %qT and %qT", type0, type1);
12073 	    }
12074 
12075           /* Always construct signed integer vector type.  */
12076           intt = c_common_type_for_size (GET_MODE_BITSIZE
12077 					 (SCALAR_TYPE_MODE
12078 					  (TREE_TYPE (type0))), 0);
12079 	  if (!intt)
12080 	    {
12081 	      error_at (location, "could not find an integer type "
12082 				  "of the same size as %qT",
12083 			TREE_TYPE (type0));
12084 	      return error_mark_node;
12085 	    }
12086           result_type = build_opaque_vector_type (intt,
12087 						  TYPE_VECTOR_SUBPARTS (type0));
12088           converted = 1;
12089 	  ret = build_vec_cmp (resultcode, result_type, op0, op1);
12090 	  goto return_build_binary_op;
12091         }
12092       build_type = integer_type_node;
12093       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12094 	   || code0 == FIXED_POINT_TYPE)
12095 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12096 	      || code1 == FIXED_POINT_TYPE))
12097 	short_compare = 1;
12098       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12099 	{
12100 	  addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12101 	  addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12102 	  addr_space_t as_common;
12103 
12104 	  if (comp_target_types (location, type0, type1))
12105 	    {
12106 	      result_type = common_pointer_type (type0, type1);
12107 	      if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12108 		  != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12109 		pedwarn (location, 0,
12110 			 "comparison of complete and incomplete pointers");
12111 	      else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12112 		pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12113 			 "ordered comparisons of pointers to functions");
12114 	      else if (null_pointer_constant_p (orig_op0)
12115 		       || null_pointer_constant_p (orig_op1))
12116 		warning_at (location, OPT_Wextra,
12117 			    "ordered comparison of pointer with null pointer");
12118 
12119 	    }
12120 	  else if (!addr_space_superset (as0, as1, &as_common))
12121 	    {
12122 	      error_at (location, "comparison of pointers to "
12123 			"disjoint address spaces");
12124 	      return error_mark_node;
12125 	    }
12126 	  else
12127 	    {
12128 	      int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12129 	      result_type = build_pointer_type
12130 			      (build_qualified_type (void_type_node, qual));
12131 	      pedwarn (location, 0,
12132 		       "comparison of distinct pointer types lacks a cast");
12133 	    }
12134 	}
12135       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12136 	{
12137 	  result_type = type0;
12138 	  if (pedantic)
12139 	    pedwarn (location, OPT_Wpedantic,
12140 		     "ordered comparison of pointer with integer zero");
12141 	  else if (extra_warnings)
12142 	    warning_at (location, OPT_Wextra,
12143 			"ordered comparison of pointer with integer zero");
12144 	}
12145       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12146 	{
12147 	  result_type = type1;
12148 	  if (pedantic)
12149 	    pedwarn (location, OPT_Wpedantic,
12150 		     "ordered comparison of pointer with integer zero");
12151 	  else if (extra_warnings)
12152 	    warning_at (location, OPT_Wextra,
12153 			"ordered comparison of pointer with integer zero");
12154 	}
12155       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12156 	{
12157 	  result_type = type0;
12158 	  pedwarn (location, 0, "comparison between pointer and integer");
12159 	}
12160       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12161 	{
12162 	  result_type = type1;
12163 	  pedwarn (location, 0, "comparison between pointer and integer");
12164 	}
12165 
12166       if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12167 	  && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12168 	{
12169 	  op0 = save_expr (op0);
12170 	  op1 = save_expr (op1);
12171 
12172 	  tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12173 	  instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12174 	}
12175 
12176       if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12177 	   || truth_value_p (TREE_CODE (orig_op0)))
12178 	  ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12179 	     || truth_value_p (TREE_CODE (orig_op1))))
12180 	maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12181       break;
12182 
12183     default:
12184       gcc_unreachable ();
12185     }
12186 
12187   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12188     return error_mark_node;
12189 
12190   if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
12191       && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12192 	  || !vector_types_compatible_elements_p (type0, type1)))
12193     {
12194       gcc_rich_location richloc (location);
12195       maybe_range_label_for_tree_type_mismatch
12196 	label_for_op0 (orig_op0, orig_op1),
12197 	label_for_op1 (orig_op1, orig_op0);
12198       richloc.maybe_add_expr (orig_op0, &label_for_op0);
12199       richloc.maybe_add_expr (orig_op1, &label_for_op1);
12200       binary_op_error (&richloc, code, type0, type1);
12201       return error_mark_node;
12202     }
12203 
12204   if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12205        || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
12206       &&
12207       (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12208        || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
12209     {
12210       bool first_complex = (code0 == COMPLEX_TYPE);
12211       bool second_complex = (code1 == COMPLEX_TYPE);
12212       int none_complex = (!first_complex && !second_complex);
12213 
12214       if (shorten || common || short_compare)
12215 	{
12216 	  result_type = c_common_type (type0, type1);
12217 	  do_warn_double_promotion (result_type, type0, type1,
12218 				    "implicit conversion from %qT to %qT "
12219 				    "to match other operand of binary "
12220 				    "expression",
12221 				    location);
12222 	  if (result_type == error_mark_node)
12223 	    return error_mark_node;
12224 	}
12225 
12226       if (first_complex != second_complex
12227 	  && (code == PLUS_EXPR
12228 	      || code == MINUS_EXPR
12229 	      || code == MULT_EXPR
12230 	      || (code == TRUNC_DIV_EXPR && first_complex))
12231 	  && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12232 	  && flag_signed_zeros)
12233 	{
12234 	  /* An operation on mixed real/complex operands must be
12235 	     handled specially, but the language-independent code can
12236 	     more easily optimize the plain complex arithmetic if
12237 	     -fno-signed-zeros.  */
12238 	  tree real_type = TREE_TYPE (result_type);
12239 	  tree real, imag;
12240 	  if (type0 != orig_type0 || type1 != orig_type1)
12241 	    {
12242 	      gcc_assert (may_need_excess_precision && common);
12243 	      semantic_result_type = c_common_type (orig_type0, orig_type1);
12244 	    }
12245 	  if (first_complex)
12246 	    {
12247 	      if (TREE_TYPE (op0) != result_type)
12248 		op0 = convert_and_check (location, result_type, op0);
12249 	      if (TREE_TYPE (op1) != real_type)
12250 		op1 = convert_and_check (location, real_type, op1);
12251 	    }
12252 	  else
12253 	    {
12254 	      if (TREE_TYPE (op0) != real_type)
12255 		op0 = convert_and_check (location, real_type, op0);
12256 	      if (TREE_TYPE (op1) != result_type)
12257 		op1 = convert_and_check (location, result_type, op1);
12258 	    }
12259 	  if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12260 	    return error_mark_node;
12261 	  if (first_complex)
12262 	    {
12263 	      op0 = save_expr (op0);
12264 	      real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12265 				     op0, true);
12266 	      imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12267 				     op0, true);
12268 	      switch (code)
12269 		{
12270 		case MULT_EXPR:
12271 		case TRUNC_DIV_EXPR:
12272 		  op1 = save_expr (op1);
12273 		  imag = build2 (resultcode, real_type, imag, op1);
12274 		  /* Fall through.  */
12275 		case PLUS_EXPR:
12276 		case MINUS_EXPR:
12277 		  real = build2 (resultcode, real_type, real, op1);
12278 		  break;
12279 		default:
12280 		  gcc_unreachable();
12281 		}
12282 	    }
12283 	  else
12284 	    {
12285 	      op1 = save_expr (op1);
12286 	      real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12287 				     op1, true);
12288 	      imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12289 				     op1, true);
12290 	      switch (code)
12291 		{
12292 		case MULT_EXPR:
12293 		  op0 = save_expr (op0);
12294 		  imag = build2 (resultcode, real_type, op0, imag);
12295 		  /* Fall through.  */
12296 		case PLUS_EXPR:
12297 		  real = build2 (resultcode, real_type, op0, real);
12298 		  break;
12299 		case MINUS_EXPR:
12300 		  real = build2 (resultcode, real_type, op0, real);
12301 		  imag = build1 (NEGATE_EXPR, real_type, imag);
12302 		  break;
12303 		default:
12304 		  gcc_unreachable();
12305 		}
12306 	    }
12307 	  ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12308 	  goto return_build_binary_op;
12309 	}
12310 
12311       /* For certain operations (which identify themselves by shorten != 0)
12312 	 if both args were extended from the same smaller type,
12313 	 do the arithmetic in that type and then extend.
12314 
12315 	 shorten !=0 and !=1 indicates a bitwise operation.
12316 	 For them, this optimization is safe only if
12317 	 both args are zero-extended or both are sign-extended.
12318 	 Otherwise, we might change the result.
12319 	 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12320 	 but calculated in (unsigned short) it would be (unsigned short)-1.  */
12321 
12322       if (shorten && none_complex)
12323 	{
12324 	  final_type = result_type;
12325 	  result_type = shorten_binary_op (result_type, op0, op1,
12326 					   shorten == -1);
12327 	}
12328 
12329       /* Shifts can be shortened if shifting right.  */
12330 
12331       if (short_shift)
12332 	{
12333 	  int unsigned_arg;
12334 	  tree arg0 = get_narrower (op0, &unsigned_arg);
12335 
12336 	  final_type = result_type;
12337 
12338 	  if (arg0 == op0 && final_type == TREE_TYPE (op0))
12339 	    unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12340 
12341 	  if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12342 	      && tree_int_cst_sgn (op1) > 0
12343 	      /* We can shorten only if the shift count is less than the
12344 		 number of bits in the smaller type size.  */
12345 	      && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12346 	      /* We cannot drop an unsigned shift after sign-extension.  */
12347 	      && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12348 	    {
12349 	      /* Do an unsigned shift if the operand was zero-extended.  */
12350 	      result_type
12351 		= c_common_signed_or_unsigned_type (unsigned_arg,
12352 						    TREE_TYPE (arg0));
12353 	      /* Convert value-to-be-shifted to that type.  */
12354 	      if (TREE_TYPE (op0) != result_type)
12355 		op0 = convert (result_type, op0);
12356 	      converted = 1;
12357 	    }
12358 	}
12359 
12360       /* Comparison operations are shortened too but differently.
12361 	 They identify themselves by setting short_compare = 1.  */
12362 
12363       if (short_compare)
12364 	{
12365 	  /* Don't write &op0, etc., because that would prevent op0
12366 	     from being kept in a register.
12367 	     Instead, make copies of the our local variables and
12368 	     pass the copies by reference, then copy them back afterward.  */
12369 	  tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12370 	  enum tree_code xresultcode = resultcode;
12371 	  tree val
12372 	    = shorten_compare (location, &xop0, &xop1, &xresult_type,
12373 			       &xresultcode);
12374 
12375 	  if (val != NULL_TREE)
12376 	    {
12377 	      ret = val;
12378 	      goto return_build_binary_op;
12379 	    }
12380 
12381 	  op0 = xop0, op1 = xop1;
12382 	  converted = 1;
12383 	  resultcode = xresultcode;
12384 
12385 	  if (c_inhibit_evaluation_warnings == 0)
12386 	    {
12387 	      bool op0_maybe_const = true;
12388 	      bool op1_maybe_const = true;
12389 	      tree orig_op0_folded, orig_op1_folded;
12390 
12391 	      if (in_late_binary_op)
12392 		{
12393 		  orig_op0_folded = orig_op0;
12394 		  orig_op1_folded = orig_op1;
12395 		}
12396 	      else
12397 		{
12398 		  /* Fold for the sake of possible warnings, as in
12399 		     build_conditional_expr.  This requires the
12400 		     "original" values to be folded, not just op0 and
12401 		     op1.  */
12402 		  c_inhibit_evaluation_warnings++;
12403 		  op0 = c_fully_fold (op0, require_constant_value,
12404 				      &op0_maybe_const);
12405 		  op1 = c_fully_fold (op1, require_constant_value,
12406 				      &op1_maybe_const);
12407 		  c_inhibit_evaluation_warnings--;
12408 		  orig_op0_folded = c_fully_fold (orig_op0,
12409 						  require_constant_value,
12410 						  NULL);
12411 		  orig_op1_folded = c_fully_fold (orig_op1,
12412 						  require_constant_value,
12413 						  NULL);
12414 		}
12415 
12416 	      if (warn_sign_compare)
12417 		warn_for_sign_compare (location, orig_op0_folded,
12418 				       orig_op1_folded, op0, op1,
12419 				       result_type, resultcode);
12420 	      if (!in_late_binary_op && !int_operands)
12421 		{
12422 		  if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12423 		    op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12424 		  if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12425 		    op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12426 		}
12427 	    }
12428 	}
12429     }
12430 
12431   /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12432      If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12433      Then the expression will be built.
12434      It will be given type FINAL_TYPE if that is nonzero;
12435      otherwise, it will be given type RESULT_TYPE.  */
12436 
12437   if (!result_type)
12438     {
12439       /* Favor showing any expression locations that are available. */
12440       op_location_t oploc (location, UNKNOWN_LOCATION);
12441       binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12442       binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12443       return error_mark_node;
12444     }
12445 
12446   if (build_type == NULL_TREE)
12447     {
12448       build_type = result_type;
12449       if ((type0 != orig_type0 || type1 != orig_type1)
12450 	  && !boolean_op)
12451 	{
12452 	  gcc_assert (may_need_excess_precision && common);
12453 	  semantic_result_type = c_common_type (orig_type0, orig_type1);
12454 	}
12455     }
12456 
12457   if (!converted)
12458     {
12459       op0 = ep_convert_and_check (location, result_type, op0,
12460 				  semantic_result_type);
12461       op1 = ep_convert_and_check (location, result_type, op1,
12462 				  semantic_result_type);
12463 
12464       /* This can happen if one operand has a vector type, and the other
12465 	 has a different type.  */
12466       if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12467 	return error_mark_node;
12468     }
12469 
12470   if (sanitize_flags_p ((SANITIZE_SHIFT
12471 			 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12472       && current_function_decl != NULL_TREE
12473       && (doing_div_or_mod || doing_shift)
12474       && !require_constant_value)
12475     {
12476       /* OP0 and/or OP1 might have side-effects.  */
12477       op0 = save_expr (op0);
12478       op1 = save_expr (op1);
12479       op0 = c_fully_fold (op0, false, NULL);
12480       op1 = c_fully_fold (op1, false, NULL);
12481       if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12482 						  | SANITIZE_FLOAT_DIVIDE))))
12483 	instrument_expr = ubsan_instrument_division (location, op0, op1);
12484       else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12485 	instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12486     }
12487 
12488   /* Treat expressions in initializers specially as they can't trap.  */
12489   if (int_const_or_overflow)
12490     ret = (require_constant_value
12491 	   ? fold_build2_initializer_loc (location, resultcode, build_type,
12492 					  op0, op1)
12493 	   : fold_build2_loc (location, resultcode, build_type, op0, op1));
12494   else
12495     ret = build2 (resultcode, build_type, op0, op1);
12496   if (final_type != NULL_TREE)
12497     ret = convert (final_type, ret);
12498 
12499  return_build_binary_op:
12500   gcc_assert (ret != error_mark_node);
12501   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12502     ret = (int_operands
12503 	   ? note_integer_operands (ret)
12504 	   : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12505   else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12506 	   && !in_late_binary_op)
12507     ret = note_integer_operands (ret);
12508   protected_set_expr_location (ret, location);
12509 
12510   if (instrument_expr != NULL)
12511     ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12512 		       instrument_expr, ret);
12513 
12514   if (semantic_result_type)
12515     ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12516 		      semantic_result_type, ret);
12517 
12518   return ret;
12519 }
12520 
12521 
12522 /* Convert EXPR to be a truth-value, validating its type for this
12523    purpose.  LOCATION is the source location for the expression.  */
12524 
12525 tree
c_objc_common_truthvalue_conversion(location_t location,tree expr)12526 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12527 {
12528   bool int_const, int_operands;
12529 
12530   switch (TREE_CODE (TREE_TYPE (expr)))
12531     {
12532     case ARRAY_TYPE:
12533       error_at (location, "used array that cannot be converted to pointer where scalar is required");
12534       return error_mark_node;
12535 
12536     case RECORD_TYPE:
12537       error_at (location, "used struct type value where scalar is required");
12538       return error_mark_node;
12539 
12540     case UNION_TYPE:
12541       error_at (location, "used union type value where scalar is required");
12542       return error_mark_node;
12543 
12544     case VOID_TYPE:
12545       error_at (location, "void value not ignored as it ought to be");
12546       return error_mark_node;
12547 
12548     case POINTER_TYPE:
12549       if (reject_gcc_builtin (expr))
12550 	return error_mark_node;
12551       break;
12552 
12553     case FUNCTION_TYPE:
12554       gcc_unreachable ();
12555 
12556     case VECTOR_TYPE:
12557       error_at (location, "used vector type where scalar is required");
12558       return error_mark_node;
12559 
12560     default:
12561       break;
12562     }
12563 
12564   int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12565   int_operands = EXPR_INT_CONST_OPERANDS (expr);
12566   if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12567     {
12568       expr = remove_c_maybe_const_expr (expr);
12569       expr = build2 (NE_EXPR, integer_type_node, expr,
12570 		     convert (TREE_TYPE (expr), integer_zero_node));
12571       expr = note_integer_operands (expr);
12572     }
12573   else
12574     /* ??? Should we also give an error for vectors rather than leaving
12575        those to give errors later?  */
12576     expr = c_common_truthvalue_conversion (location, expr);
12577 
12578   if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12579     {
12580       if (TREE_OVERFLOW (expr))
12581 	return expr;
12582       else
12583 	return note_integer_operands (expr);
12584     }
12585   if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12586     return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12587   return expr;
12588 }
12589 
12590 
12591 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12592    required.  */
12593 
12594 tree
c_expr_to_decl(tree expr,bool * tc ATTRIBUTE_UNUSED,bool * se)12595 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12596 {
12597   if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12598     {
12599       tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12600       /* Executing a compound literal inside a function reinitializes
12601 	 it.  */
12602       if (!TREE_STATIC (decl))
12603 	*se = true;
12604       return decl;
12605     }
12606   else
12607     return expr;
12608 }
12609 
12610 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12611    statement.  LOC is the location of the construct.  */
12612 
12613 tree
c_finish_omp_construct(location_t loc,enum tree_code code,tree body,tree clauses)12614 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12615 			tree clauses)
12616 {
12617   body = c_end_compound_stmt (loc, body, true);
12618 
12619   tree stmt = make_node (code);
12620   TREE_TYPE (stmt) = void_type_node;
12621   OMP_BODY (stmt) = body;
12622   OMP_CLAUSES (stmt) = clauses;
12623   SET_EXPR_LOCATION (stmt, loc);
12624 
12625   return add_stmt (stmt);
12626 }
12627 
12628 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12629    statement.  LOC is the location of the OACC_DATA.  */
12630 
12631 tree
c_finish_oacc_data(location_t loc,tree clauses,tree block)12632 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12633 {
12634   tree stmt;
12635 
12636   block = c_end_compound_stmt (loc, block, true);
12637 
12638   stmt = make_node (OACC_DATA);
12639   TREE_TYPE (stmt) = void_type_node;
12640   OACC_DATA_CLAUSES (stmt) = clauses;
12641   OACC_DATA_BODY (stmt) = block;
12642   SET_EXPR_LOCATION (stmt, loc);
12643 
12644   return add_stmt (stmt);
12645 }
12646 
12647 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12648    statement.  LOC is the location of the OACC_HOST_DATA.  */
12649 
12650 tree
c_finish_oacc_host_data(location_t loc,tree clauses,tree block)12651 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12652 {
12653   tree stmt;
12654 
12655   block = c_end_compound_stmt (loc, block, true);
12656 
12657   stmt = make_node (OACC_HOST_DATA);
12658   TREE_TYPE (stmt) = void_type_node;
12659   OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12660   OACC_HOST_DATA_BODY (stmt) = block;
12661   SET_EXPR_LOCATION (stmt, loc);
12662 
12663   return add_stmt (stmt);
12664 }
12665 
12666 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
12667 
12668 tree
c_begin_omp_parallel(void)12669 c_begin_omp_parallel (void)
12670 {
12671   tree block;
12672 
12673   keep_next_level ();
12674   block = c_begin_compound_stmt (true);
12675 
12676   return block;
12677 }
12678 
12679 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12680    statement.  LOC is the location of the OMP_PARALLEL.  */
12681 
12682 tree
c_finish_omp_parallel(location_t loc,tree clauses,tree block)12683 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12684 {
12685   tree stmt;
12686 
12687   block = c_end_compound_stmt (loc, block, true);
12688 
12689   stmt = make_node (OMP_PARALLEL);
12690   TREE_TYPE (stmt) = void_type_node;
12691   OMP_PARALLEL_CLAUSES (stmt) = clauses;
12692   OMP_PARALLEL_BODY (stmt) = block;
12693   SET_EXPR_LOCATION (stmt, loc);
12694 
12695   return add_stmt (stmt);
12696 }
12697 
12698 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
12699 
12700 tree
c_begin_omp_task(void)12701 c_begin_omp_task (void)
12702 {
12703   tree block;
12704 
12705   keep_next_level ();
12706   block = c_begin_compound_stmt (true);
12707 
12708   return block;
12709 }
12710 
12711 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12712    statement.  LOC is the location of the #pragma.  */
12713 
12714 tree
c_finish_omp_task(location_t loc,tree clauses,tree block)12715 c_finish_omp_task (location_t loc, tree clauses, tree block)
12716 {
12717   tree stmt;
12718 
12719   block = c_end_compound_stmt (loc, block, true);
12720 
12721   stmt = make_node (OMP_TASK);
12722   TREE_TYPE (stmt) = void_type_node;
12723   OMP_TASK_CLAUSES (stmt) = clauses;
12724   OMP_TASK_BODY (stmt) = block;
12725   SET_EXPR_LOCATION (stmt, loc);
12726 
12727   return add_stmt (stmt);
12728 }
12729 
12730 /* Generate GOMP_cancel call for #pragma omp cancel.  */
12731 
12732 void
c_finish_omp_cancel(location_t loc,tree clauses)12733 c_finish_omp_cancel (location_t loc, tree clauses)
12734 {
12735   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12736   int mask = 0;
12737   if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12738     mask = 1;
12739   else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12740     mask = 2;
12741   else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12742     mask = 4;
12743   else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12744     mask = 8;
12745   else
12746     {
12747       error_at (loc, "%<#pragma omp cancel%> must specify one of "
12748 		     "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12749 		     "clauses");
12750       return;
12751     }
12752   tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12753   if (ifc != NULL_TREE)
12754     {
12755       if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
12756 	  && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
12757 	error_at (OMP_CLAUSE_LOCATION (ifc),
12758 		  "expected %<cancel%> %<if%> clause modifier");
12759       else
12760 	{
12761 	  tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
12762 	  if (ifc2 != NULL_TREE)
12763 	    {
12764 	      gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
12765 			  && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
12766 			  && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
12767 	      error_at (OMP_CLAUSE_LOCATION (ifc2),
12768 			"expected %<cancel%> %<if%> clause modifier");
12769 	    }
12770 	}
12771 
12772       tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12773       ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12774 			     boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12775 			     build_zero_cst (type));
12776     }
12777   else
12778     ifc = boolean_true_node;
12779   tree stmt = build_call_expr_loc (loc, fn, 2,
12780 				   build_int_cst (integer_type_node, mask),
12781 				   ifc);
12782   add_stmt (stmt);
12783 }
12784 
12785 /* Generate GOMP_cancellation_point call for
12786    #pragma omp cancellation point.  */
12787 
12788 void
c_finish_omp_cancellation_point(location_t loc,tree clauses)12789 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12790 {
12791   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12792   int mask = 0;
12793   if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12794     mask = 1;
12795   else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12796     mask = 2;
12797   else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12798     mask = 4;
12799   else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12800     mask = 8;
12801   else
12802     {
12803       error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12804 		     "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12805 		     "clauses");
12806       return;
12807     }
12808   tree stmt = build_call_expr_loc (loc, fn, 1,
12809 				   build_int_cst (integer_type_node, mask));
12810   add_stmt (stmt);
12811 }
12812 
12813 /* Helper function for handle_omp_array_sections.  Called recursively
12814    to handle multiple array-section-subscripts.  C is the clause,
12815    T current expression (initially OMP_CLAUSE_DECL), which is either
12816    a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12817    expression if specified, TREE_VALUE length expression if specified,
12818    TREE_CHAIN is what it has been specified after, or some decl.
12819    TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12820    set to true if any of the array-section-subscript could have length
12821    of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12822    first array-section-subscript which is known not to have length
12823    of one.  Given say:
12824    map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12825    FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12826    all are or may have length of 1, array-section-subscript [:2] is the
12827    first one known not to have length 1.  For array-section-subscript
12828    <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12829    0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12830    can if MAYBE_ZERO_LEN is false.  MAYBE_ZERO_LEN will be true in the above
12831    case though, as some lengths could be zero.  */
12832 
12833 static tree
handle_omp_array_sections_1(tree c,tree t,vec<tree> & types,bool & maybe_zero_len,unsigned int & first_non_one,enum c_omp_region_type ort)12834 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
12835 			     bool &maybe_zero_len, unsigned int &first_non_one,
12836 			     enum c_omp_region_type ort)
12837 {
12838   tree ret, low_bound, length, type;
12839   if (TREE_CODE (t) != TREE_LIST)
12840     {
12841       if (error_operand_p (t))
12842 	return error_mark_node;
12843       ret = t;
12844       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12845 	  && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12846 	{
12847 	  error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12848 		    t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12849 	  return error_mark_node;
12850 	}
12851       if (TREE_CODE (t) == COMPONENT_REF
12852 	  && ort == C_ORT_OMP
12853 	  && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12854 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12855 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12856 	{
12857 	  if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12858 	    {
12859 	      error_at (OMP_CLAUSE_LOCATION (c),
12860 			"bit-field %qE in %qs clause",
12861 			t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12862 	      return error_mark_node;
12863 	    }
12864 	  while (TREE_CODE (t) == COMPONENT_REF)
12865 	    {
12866 	      if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12867 		{
12868 		  error_at (OMP_CLAUSE_LOCATION (c),
12869 			    "%qE is a member of a union", t);
12870 		  return error_mark_node;
12871 		}
12872 	      t = TREE_OPERAND (t, 0);
12873 	    }
12874 	}
12875       if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
12876 	{
12877 	  if (DECL_P (t))
12878 	    error_at (OMP_CLAUSE_LOCATION (c),
12879 		      "%qD is not a variable in %qs clause", t,
12880 		      omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12881 	  else
12882 	    error_at (OMP_CLAUSE_LOCATION (c),
12883 		      "%qE is not a variable in %qs clause", t,
12884 		      omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12885 	  return error_mark_node;
12886 	}
12887       else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12888 	       && TYPE_ATOMIC (TREE_TYPE (t)))
12889 	{
12890 	  error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12891 		    t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12892 	  return error_mark_node;
12893 	}
12894       else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12895 	       && VAR_P (t)
12896 	       && DECL_THREAD_LOCAL_P (t))
12897 	{
12898 	  error_at (OMP_CLAUSE_LOCATION (c),
12899 		    "%qD is threadprivate variable in %qs clause", t,
12900 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12901 	  return error_mark_node;
12902 	}
12903       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12904 	  && TYPE_ATOMIC (TREE_TYPE (t))
12905 	  && POINTER_TYPE_P (TREE_TYPE (t)))
12906 	{
12907 	  /* If the array section is pointer based and the pointer
12908 	     itself is _Atomic qualified, we need to atomically load
12909 	     the pointer.  */
12910 	  c_expr expr;
12911 	  memset (&expr, 0, sizeof (expr));
12912 	  expr.value = ret;
12913 	  expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12914 					   expr, false, false);
12915 	  ret = expr.value;
12916 	}
12917       return ret;
12918     }
12919 
12920   ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
12921 				     maybe_zero_len, first_non_one, ort);
12922   if (ret == error_mark_node || ret == NULL_TREE)
12923     return ret;
12924 
12925   type = TREE_TYPE (ret);
12926   low_bound = TREE_PURPOSE (t);
12927   length = TREE_VALUE (t);
12928 
12929   if (low_bound == error_mark_node || length == error_mark_node)
12930     return error_mark_node;
12931 
12932   if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12933     {
12934       error_at (OMP_CLAUSE_LOCATION (c),
12935 		"low bound %qE of array section does not have integral type",
12936 		low_bound);
12937       return error_mark_node;
12938     }
12939   if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12940     {
12941       error_at (OMP_CLAUSE_LOCATION (c),
12942 		"length %qE of array section does not have integral type",
12943 		length);
12944       return error_mark_node;
12945     }
12946   if (low_bound
12947       && TREE_CODE (low_bound) == INTEGER_CST
12948       && TYPE_PRECISION (TREE_TYPE (low_bound))
12949 	 > TYPE_PRECISION (sizetype))
12950     low_bound = fold_convert (sizetype, low_bound);
12951   if (length
12952       && TREE_CODE (length) == INTEGER_CST
12953       && TYPE_PRECISION (TREE_TYPE (length))
12954 	 > TYPE_PRECISION (sizetype))
12955     length = fold_convert (sizetype, length);
12956   if (low_bound == NULL_TREE)
12957     low_bound = integer_zero_node;
12958 
12959   if (length != NULL_TREE)
12960     {
12961       if (!integer_nonzerop (length))
12962 	{
12963 	  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12964 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
12965 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
12966 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
12967 	    {
12968 	      if (integer_zerop (length))
12969 		{
12970 		  error_at (OMP_CLAUSE_LOCATION (c),
12971 			    "zero length array section in %qs clause",
12972 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12973 		  return error_mark_node;
12974 		}
12975 	    }
12976 	  else
12977 	    maybe_zero_len = true;
12978 	}
12979       if (first_non_one == types.length ()
12980 	  && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12981 	first_non_one++;
12982     }
12983   if (TREE_CODE (type) == ARRAY_TYPE)
12984     {
12985       if (length == NULL_TREE
12986 	  && (TYPE_DOMAIN (type) == NULL_TREE
12987 	      || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12988 	{
12989 	  error_at (OMP_CLAUSE_LOCATION (c),
12990 		    "for unknown bound array type length expression must "
12991 		    "be specified");
12992 	  return error_mark_node;
12993 	}
12994       if (TREE_CODE (low_bound) == INTEGER_CST
12995 	  && tree_int_cst_sgn (low_bound) == -1)
12996 	{
12997 	  error_at (OMP_CLAUSE_LOCATION (c),
12998 		    "negative low bound in array section in %qs clause",
12999 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13000 	  return error_mark_node;
13001 	}
13002       if (length != NULL_TREE
13003 	  && TREE_CODE (length) == INTEGER_CST
13004 	  && tree_int_cst_sgn (length) == -1)
13005 	{
13006 	  error_at (OMP_CLAUSE_LOCATION (c),
13007 		    "negative length in array section in %qs clause",
13008 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13009 	  return error_mark_node;
13010 	}
13011       if (TYPE_DOMAIN (type)
13012 	  && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13013 	  && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13014 			== INTEGER_CST)
13015 	{
13016 	  tree size
13017 	    = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13018 	  size = size_binop (PLUS_EXPR, size, size_one_node);
13019 	  if (TREE_CODE (low_bound) == INTEGER_CST)
13020 	    {
13021 	      if (tree_int_cst_lt (size, low_bound))
13022 		{
13023 		  error_at (OMP_CLAUSE_LOCATION (c),
13024 			    "low bound %qE above array section size "
13025 			    "in %qs clause", low_bound,
13026 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13027 		  return error_mark_node;
13028 		}
13029 	      if (tree_int_cst_equal (size, low_bound))
13030 		{
13031 		  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13032 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13033 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13034 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13035 		    {
13036 		      error_at (OMP_CLAUSE_LOCATION (c),
13037 				"zero length array section in %qs clause",
13038 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13039 		      return error_mark_node;
13040 		    }
13041 		  maybe_zero_len = true;
13042 		}
13043 	      else if (length == NULL_TREE
13044 		       && first_non_one == types.length ()
13045 		       && tree_int_cst_equal
13046 			    (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13047 			     low_bound))
13048 		first_non_one++;
13049 	    }
13050 	  else if (length == NULL_TREE)
13051 	    {
13052 	      if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13053 		  && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13054 		  && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13055 		  && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13056 		maybe_zero_len = true;
13057 	      if (first_non_one == types.length ())
13058 		first_non_one++;
13059 	    }
13060 	  if (length && TREE_CODE (length) == INTEGER_CST)
13061 	    {
13062 	      if (tree_int_cst_lt (size, length))
13063 		{
13064 		  error_at (OMP_CLAUSE_LOCATION (c),
13065 			    "length %qE above array section size "
13066 			    "in %qs clause", length,
13067 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13068 		  return error_mark_node;
13069 		}
13070 	      if (TREE_CODE (low_bound) == INTEGER_CST)
13071 		{
13072 		  tree lbpluslen
13073 		    = size_binop (PLUS_EXPR,
13074 				  fold_convert (sizetype, low_bound),
13075 				  fold_convert (sizetype, length));
13076 		  if (TREE_CODE (lbpluslen) == INTEGER_CST
13077 		      && tree_int_cst_lt (size, lbpluslen))
13078 		    {
13079 		      error_at (OMP_CLAUSE_LOCATION (c),
13080 				"high bound %qE above array section size "
13081 				"in %qs clause", lbpluslen,
13082 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13083 		      return error_mark_node;
13084 		    }
13085 		}
13086 	    }
13087 	}
13088       else if (length == NULL_TREE)
13089 	{
13090 	  if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13091 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13092 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13093 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13094 	    maybe_zero_len = true;
13095 	  if (first_non_one == types.length ())
13096 	    first_non_one++;
13097 	}
13098 
13099       /* For [lb:] we will need to evaluate lb more than once.  */
13100       if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13101 	{
13102 	  tree lb = save_expr (low_bound);
13103 	  if (lb != low_bound)
13104 	    {
13105 	      TREE_PURPOSE (t) = lb;
13106 	      low_bound = lb;
13107 	    }
13108 	}
13109     }
13110   else if (TREE_CODE (type) == POINTER_TYPE)
13111     {
13112       if (length == NULL_TREE)
13113 	{
13114 	  error_at (OMP_CLAUSE_LOCATION (c),
13115 		    "for pointer type length expression must be specified");
13116 	  return error_mark_node;
13117 	}
13118       if (length != NULL_TREE
13119 	  && TREE_CODE (length) == INTEGER_CST
13120 	  && tree_int_cst_sgn (length) == -1)
13121 	{
13122 	  error_at (OMP_CLAUSE_LOCATION (c),
13123 		    "negative length in array section in %qs clause",
13124 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13125 	  return error_mark_node;
13126 	}
13127       /* If there is a pointer type anywhere but in the very first
13128 	 array-section-subscript, the array section can't be contiguous.  */
13129       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13130 	  && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13131 	{
13132 	  error_at (OMP_CLAUSE_LOCATION (c),
13133 		    "array section is not contiguous in %qs clause",
13134 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13135 	  return error_mark_node;
13136 	}
13137     }
13138   else
13139     {
13140       error_at (OMP_CLAUSE_LOCATION (c),
13141 		"%qE does not have pointer or array type", ret);
13142       return error_mark_node;
13143     }
13144   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13145     types.safe_push (TREE_TYPE (ret));
13146   /* We will need to evaluate lb more than once.  */
13147   tree lb = save_expr (low_bound);
13148   if (lb != low_bound)
13149     {
13150       TREE_PURPOSE (t) = lb;
13151       low_bound = lb;
13152     }
13153   ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13154   return ret;
13155 }
13156 
13157 /* Handle array sections for clause C.  */
13158 
13159 static bool
handle_omp_array_sections(tree c,enum c_omp_region_type ort)13160 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13161 {
13162   bool maybe_zero_len = false;
13163   unsigned int first_non_one = 0;
13164   auto_vec<tree, 10> types;
13165   tree *tp = &OMP_CLAUSE_DECL (c);
13166   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13167       && TREE_CODE (*tp) == TREE_LIST
13168       && TREE_PURPOSE (*tp)
13169       && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13170     tp = &TREE_VALUE (*tp);
13171   tree first = handle_omp_array_sections_1 (c, *tp, types,
13172 					    maybe_zero_len, first_non_one,
13173 					    ort);
13174   if (first == error_mark_node)
13175     return true;
13176   if (first == NULL_TREE)
13177     return false;
13178   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13179     {
13180       tree t = *tp;
13181       tree tem = NULL_TREE;
13182       /* Need to evaluate side effects in the length expressions
13183 	 if any.  */
13184       while (TREE_CODE (t) == TREE_LIST)
13185 	{
13186 	  if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13187 	    {
13188 	      if (tem == NULL_TREE)
13189 		tem = TREE_VALUE (t);
13190 	      else
13191 		tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13192 			      TREE_VALUE (t), tem);
13193 	    }
13194 	  t = TREE_CHAIN (t);
13195 	}
13196       if (tem)
13197 	first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13198       first = c_fully_fold (first, false, NULL, true);
13199       *tp = first;
13200     }
13201   else
13202     {
13203       unsigned int num = types.length (), i;
13204       tree t, side_effects = NULL_TREE, size = NULL_TREE;
13205       tree condition = NULL_TREE;
13206 
13207       if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13208 	maybe_zero_len = true;
13209 
13210       for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13211 	   t = TREE_CHAIN (t))
13212 	{
13213 	  tree low_bound = TREE_PURPOSE (t);
13214 	  tree length = TREE_VALUE (t);
13215 
13216 	  i--;
13217 	  if (low_bound
13218 	      && TREE_CODE (low_bound) == INTEGER_CST
13219 	      && TYPE_PRECISION (TREE_TYPE (low_bound))
13220 		 > TYPE_PRECISION (sizetype))
13221 	    low_bound = fold_convert (sizetype, low_bound);
13222 	  if (length
13223 	      && TREE_CODE (length) == INTEGER_CST
13224 	      && TYPE_PRECISION (TREE_TYPE (length))
13225 		 > TYPE_PRECISION (sizetype))
13226 	    length = fold_convert (sizetype, length);
13227 	  if (low_bound == NULL_TREE)
13228 	    low_bound = integer_zero_node;
13229 	  if (!maybe_zero_len && i > first_non_one)
13230 	    {
13231 	      if (integer_nonzerop (low_bound))
13232 		goto do_warn_noncontiguous;
13233 	      if (length != NULL_TREE
13234 		  && TREE_CODE (length) == INTEGER_CST
13235 		  && TYPE_DOMAIN (types[i])
13236 		  && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13237 		  && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13238 		     == INTEGER_CST)
13239 		{
13240 		  tree size;
13241 		  size = size_binop (PLUS_EXPR,
13242 				     TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13243 				     size_one_node);
13244 		  if (!tree_int_cst_equal (length, size))
13245 		    {
13246 		     do_warn_noncontiguous:
13247 		      error_at (OMP_CLAUSE_LOCATION (c),
13248 				"array section is not contiguous in %qs "
13249 				"clause",
13250 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13251 		      return true;
13252 		    }
13253 		}
13254 	      if (length != NULL_TREE
13255 		  && TREE_SIDE_EFFECTS (length))
13256 		{
13257 		  if (side_effects == NULL_TREE)
13258 		    side_effects = length;
13259 		  else
13260 		    side_effects = build2 (COMPOUND_EXPR,
13261 					   TREE_TYPE (side_effects),
13262 					   length, side_effects);
13263 		}
13264 	    }
13265 	  else
13266 	    {
13267 	      tree l;
13268 
13269 	      if (i > first_non_one
13270 		  && ((length && integer_nonzerop (length))
13271 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13272 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13273 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13274 		continue;
13275 	      if (length)
13276 		l = fold_convert (sizetype, length);
13277 	      else
13278 		{
13279 		  l = size_binop (PLUS_EXPR,
13280 				  TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13281 				  size_one_node);
13282 		  l = size_binop (MINUS_EXPR, l,
13283 				  fold_convert (sizetype, low_bound));
13284 		}
13285 	      if (i > first_non_one)
13286 		{
13287 		  l = fold_build2 (NE_EXPR, boolean_type_node, l,
13288 				   size_zero_node);
13289 		  if (condition == NULL_TREE)
13290 		    condition = l;
13291 		  else
13292 		    condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13293 					     l, condition);
13294 		}
13295 	      else if (size == NULL_TREE)
13296 		{
13297 		  size = size_in_bytes (TREE_TYPE (types[i]));
13298 		  tree eltype = TREE_TYPE (types[num - 1]);
13299 		  while (TREE_CODE (eltype) == ARRAY_TYPE)
13300 		    eltype = TREE_TYPE (eltype);
13301 		  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13302 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13303 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13304 		    {
13305 		      if (integer_zerop (size)
13306 			  || integer_zerop (size_in_bytes (eltype)))
13307 			{
13308 			  error_at (OMP_CLAUSE_LOCATION (c),
13309 				    "zero length array section in %qs clause",
13310 				    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13311 			  return error_mark_node;
13312 			}
13313 		      size = size_binop (EXACT_DIV_EXPR, size,
13314 					 size_in_bytes (eltype));
13315 		    }
13316 		  size = size_binop (MULT_EXPR, size, l);
13317 		  if (condition)
13318 		    size = fold_build3 (COND_EXPR, sizetype, condition,
13319 					size, size_zero_node);
13320 		}
13321 	      else
13322 		size = size_binop (MULT_EXPR, size, l);
13323 	    }
13324 	}
13325       if (side_effects)
13326 	size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13327       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13328 	  || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13329 	  || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13330 	{
13331 	  size = size_binop (MINUS_EXPR, size, size_one_node);
13332 	  size = c_fully_fold (size, false, NULL);
13333 	  size = save_expr (size);
13334 	  tree index_type = build_index_type (size);
13335 	  tree eltype = TREE_TYPE (first);
13336 	  while (TREE_CODE (eltype) == ARRAY_TYPE)
13337 	    eltype = TREE_TYPE (eltype);
13338 	  tree type = build_array_type (eltype, index_type);
13339 	  tree ptype = build_pointer_type (eltype);
13340 	  if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13341 	    t = build_fold_addr_expr (t);
13342 	  tree t2 = build_fold_addr_expr (first);
13343 	  t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13344 				 ptrdiff_type_node, t2);
13345 	  t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13346 				ptrdiff_type_node, t2,
13347 				fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13348 						  ptrdiff_type_node, t));
13349 	  t2 = c_fully_fold (t2, false, NULL);
13350 	  if (tree_fits_shwi_p (t2))
13351 	    t = build2 (MEM_REF, type, t,
13352 			build_int_cst (ptype, tree_to_shwi (t2)));
13353 	  else
13354 	    {
13355 	      t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13356 	      t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13357 			      TREE_TYPE (t), t, t2);
13358 	      t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13359 	    }
13360 	  OMP_CLAUSE_DECL (c) = t;
13361 	  return false;
13362 	}
13363       first = c_fully_fold (first, false, NULL);
13364       OMP_CLAUSE_DECL (c) = first;
13365       if (size)
13366 	size = c_fully_fold (size, false, NULL);
13367       OMP_CLAUSE_SIZE (c) = size;
13368       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13369 	  || (TREE_CODE (t) == COMPONENT_REF
13370 	      && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13371 	return false;
13372       gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13373       if (ort == C_ORT_OMP || ort == C_ORT_ACC)
13374 	switch (OMP_CLAUSE_MAP_KIND (c))
13375 	  {
13376 	  case GOMP_MAP_ALLOC:
13377 	  case GOMP_MAP_TO:
13378 	  case GOMP_MAP_FROM:
13379 	  case GOMP_MAP_TOFROM:
13380 	  case GOMP_MAP_ALWAYS_TO:
13381 	  case GOMP_MAP_ALWAYS_FROM:
13382 	  case GOMP_MAP_ALWAYS_TOFROM:
13383 	  case GOMP_MAP_RELEASE:
13384 	  case GOMP_MAP_DELETE:
13385 	  case GOMP_MAP_FORCE_TO:
13386 	  case GOMP_MAP_FORCE_FROM:
13387 	  case GOMP_MAP_FORCE_TOFROM:
13388 	  case GOMP_MAP_FORCE_PRESENT:
13389 	    OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13390 	    break;
13391 	  default:
13392 	    break;
13393 	  }
13394       tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13395       if (ort != C_ORT_OMP && ort != C_ORT_ACC)
13396 	OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13397       else if (TREE_CODE (t) == COMPONENT_REF)
13398 	OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
13399       else
13400 	OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13401       if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13402 	  && !c_mark_addressable (t))
13403 	return false;
13404       OMP_CLAUSE_DECL (c2) = t;
13405       t = build_fold_addr_expr (first);
13406       t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13407       tree ptr = OMP_CLAUSE_DECL (c2);
13408       if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13409 	ptr = build_fold_addr_expr (ptr);
13410       t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13411 			   ptrdiff_type_node, t,
13412 			   fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13413 					     ptrdiff_type_node, ptr));
13414       t = c_fully_fold (t, false, NULL);
13415       OMP_CLAUSE_SIZE (c2) = t;
13416       OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13417       OMP_CLAUSE_CHAIN (c) = c2;
13418     }
13419   return false;
13420 }
13421 
13422 /* Helper function of finish_omp_clauses.  Clone STMT as if we were making
13423    an inline call.  But, remap
13424    the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13425    and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL.  */
13426 
13427 static tree
c_clone_omp_udr(tree stmt,tree omp_decl1,tree omp_decl2,tree decl,tree placeholder)13428 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13429 		 tree decl, tree placeholder)
13430 {
13431   copy_body_data id;
13432   hash_map<tree, tree> decl_map;
13433 
13434   decl_map.put (omp_decl1, placeholder);
13435   decl_map.put (omp_decl2, decl);
13436   memset (&id, 0, sizeof (id));
13437   id.src_fn = DECL_CONTEXT (omp_decl1);
13438   id.dst_fn = current_function_decl;
13439   id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13440   id.decl_map = &decl_map;
13441 
13442   id.copy_decl = copy_decl_no_change;
13443   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13444   id.transform_new_cfg = true;
13445   id.transform_return_to_modify = false;
13446   id.transform_lang_insert_block = NULL;
13447   id.eh_lp_nr = 0;
13448   walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13449   return stmt;
13450 }
13451 
13452 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13453    Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP.  */
13454 
13455 static tree
c_find_omp_placeholder_r(tree * tp,int *,void * data)13456 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13457 {
13458   if (*tp == (tree) data)
13459     return *tp;
13460   return NULL_TREE;
13461 }
13462 
13463 /* Similarly, but also walk aggregate fields.  */
13464 
13465 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13466 
13467 static tree
c_find_omp_var_r(tree * tp,int *,void * data)13468 c_find_omp_var_r (tree *tp, int *, void *data)
13469 {
13470   if (*tp == ((struct c_find_omp_var_s *) data)->var)
13471     return *tp;
13472   if (RECORD_OR_UNION_TYPE_P (*tp))
13473     {
13474       tree field;
13475       hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13476 
13477       for (field = TYPE_FIELDS (*tp); field;
13478 	   field = DECL_CHAIN (field))
13479 	if (TREE_CODE (field) == FIELD_DECL)
13480 	  {
13481 	    tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13482 				  c_find_omp_var_r, data, pset);
13483 	    if (ret)
13484 	      return ret;
13485 	    ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13486 	    if (ret)
13487 	      return ret;
13488 	    ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13489 			     pset);
13490 	    if (ret)
13491 	      return ret;
13492 	    ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13493 	    if (ret)
13494 	      return ret;
13495 	  }
13496     }
13497   else if (INTEGRAL_TYPE_P (*tp))
13498     return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13499 		      ((struct c_find_omp_var_s *) data)->pset);
13500   return NULL_TREE;
13501 }
13502 
13503 /* Finish OpenMP iterators ITER.  Return true if they are errorneous
13504    and clauses containing them should be removed.  */
13505 
13506 static bool
c_omp_finish_iterators(tree iter)13507 c_omp_finish_iterators (tree iter)
13508 {
13509   bool ret = false;
13510   for (tree it = iter; it; it = TREE_CHAIN (it))
13511     {
13512       tree var = TREE_VEC_ELT (it, 0);
13513       tree begin = TREE_VEC_ELT (it, 1);
13514       tree end = TREE_VEC_ELT (it, 2);
13515       tree step = TREE_VEC_ELT (it, 3);
13516       tree orig_step;
13517       tree type = TREE_TYPE (var);
13518       location_t loc = DECL_SOURCE_LOCATION (var);
13519       if (type == error_mark_node)
13520 	{
13521 	  ret = true;
13522 	  continue;
13523 	}
13524       if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13525 	{
13526 	  error_at (loc, "iterator %qD has neither integral nor pointer type",
13527 		    var);
13528 	  ret = true;
13529 	  continue;
13530 	}
13531       else if (TYPE_ATOMIC (type))
13532 	{
13533 	  error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13534 	  ret = true;
13535 	  continue;
13536 	}
13537       else if (TYPE_READONLY (type))
13538 	{
13539 	  error_at (loc, "iterator %qD has const qualified type", var);
13540 	  ret = true;
13541 	  continue;
13542 	}
13543       else if (step == error_mark_node
13544 	       || TREE_TYPE (step) == error_mark_node)
13545 	{
13546 	  ret = true;
13547 	  continue;
13548 	}
13549       else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13550 	{
13551 	  error_at (EXPR_LOC_OR_LOC (step, loc),
13552 		    "iterator step with non-integral type");
13553 	  ret = true;
13554 	  continue;
13555 	}
13556       begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13557       end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13558       orig_step = save_expr (c_fully_fold (step, false, NULL));
13559       tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13560       step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13561       if (POINTER_TYPE_P (type))
13562 	{
13563 	  begin = save_expr (begin);
13564 	  step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13565 	  step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13566 				  fold_convert (sizetype, step),
13567 				  fold_convert (sizetype, begin));
13568 	  step = fold_convert (ssizetype, step);
13569 	}
13570       if (integer_zerop (step))
13571 	{
13572 	  error_at (loc, "iterator %qD has zero step", var);
13573 	  ret = true;
13574 	  continue;
13575 	}
13576 
13577       if (begin == error_mark_node
13578 	  || end == error_mark_node
13579 	  || step == error_mark_node
13580 	  || orig_step == error_mark_node)
13581 	{
13582 	  ret = true;
13583 	  continue;
13584 	}
13585       hash_set<tree> pset;
13586       tree it2;
13587       for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13588 	{
13589 	  tree var2 = TREE_VEC_ELT (it2, 0);
13590 	  tree begin2 = TREE_VEC_ELT (it2, 1);
13591 	  tree end2 = TREE_VEC_ELT (it2, 2);
13592 	  tree step2 = TREE_VEC_ELT (it2, 3);
13593 	  tree type2 = TREE_TYPE (var2);
13594 	  location_t loc2 = DECL_SOURCE_LOCATION (var2);
13595 	  struct c_find_omp_var_s data = { var, &pset };
13596 	  if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13597 	    {
13598 	      error_at (loc2,
13599 			"type of iterator %qD refers to outer iterator %qD",
13600 			var2, var);
13601 	      break;
13602 	    }
13603 	  else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13604 	    {
13605 	      error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13606 			"begin expression refers to outer iterator %qD", var);
13607 	      break;
13608 	    }
13609 	  else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13610 	    {
13611 	      error_at (EXPR_LOC_OR_LOC (end2, loc2),
13612 			"end expression refers to outer iterator %qD", var);
13613 	      break;
13614 	    }
13615 	  else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
13616 	    {
13617 	      error_at (EXPR_LOC_OR_LOC (step2, loc2),
13618 			"step expression refers to outer iterator %qD", var);
13619 	      break;
13620 	    }
13621 	}
13622       if (it2)
13623 	{
13624 	  ret = true;
13625 	  continue;
13626 	}
13627       TREE_VEC_ELT (it, 1) = begin;
13628       TREE_VEC_ELT (it, 2) = end;
13629       TREE_VEC_ELT (it, 3) = step;
13630       TREE_VEC_ELT (it, 4) = orig_step;
13631     }
13632   return ret;
13633 }
13634 
13635 /* For all elements of CLAUSES, validate them against their constraints.
13636    Remove any elements from the list that are invalid.  */
13637 
13638 tree
c_finish_omp_clauses(tree clauses,enum c_omp_region_type ort)13639 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13640 {
13641   bitmap_head generic_head, firstprivate_head, lastprivate_head;
13642   bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
13643   tree c, t, type, *pc;
13644   tree simdlen = NULL_TREE, safelen = NULL_TREE;
13645   bool branch_seen = false;
13646   bool copyprivate_seen = false;
13647   bool linear_variable_step_check = false;
13648   tree *nowait_clause = NULL;
13649   bool ordered_seen = false;
13650   tree schedule_clause = NULL_TREE;
13651   bool oacc_async = false;
13652   tree last_iterators = NULL_TREE;
13653   bool last_iterators_remove = false;
13654   tree *nogroup_seen = NULL;
13655   bool reduction_seen = false;
13656 
13657   bitmap_obstack_initialize (NULL);
13658   bitmap_initialize (&generic_head, &bitmap_default_obstack);
13659   bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13660   bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13661   bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13662   /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead.  */
13663   bitmap_initialize (&map_head, &bitmap_default_obstack);
13664   bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13665   /* If ort == C_ORT_OMP used as nontemporal_head instead.  */
13666   bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13667 
13668   if (ort & C_ORT_ACC)
13669     for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13670       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13671 	{
13672 	  oacc_async = true;
13673 	  break;
13674 	}
13675 
13676   for (pc = &clauses, c = clauses; c ; c = *pc)
13677     {
13678       bool remove = false;
13679       bool need_complete = false;
13680       bool need_implicitly_determined = false;
13681 
13682       switch (OMP_CLAUSE_CODE (c))
13683 	{
13684 	case OMP_CLAUSE_SHARED:
13685 	  need_implicitly_determined = true;
13686 	  goto check_dup_generic;
13687 
13688 	case OMP_CLAUSE_PRIVATE:
13689 	  need_complete = true;
13690 	  need_implicitly_determined = true;
13691 	  goto check_dup_generic;
13692 
13693 	case OMP_CLAUSE_REDUCTION:
13694 	  reduction_seen = true;
13695 	  /* FALLTHRU */
13696 	case OMP_CLAUSE_IN_REDUCTION:
13697 	case OMP_CLAUSE_TASK_REDUCTION:
13698 	  need_implicitly_determined = true;
13699 	  t = OMP_CLAUSE_DECL (c);
13700 	  if (TREE_CODE (t) == TREE_LIST)
13701 	    {
13702 	      if (handle_omp_array_sections (c, ort))
13703 		{
13704 		  remove = true;
13705 		  break;
13706 		}
13707 
13708 	      t = OMP_CLAUSE_DECL (c);
13709 	    }
13710 	  t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13711 	  if (t == error_mark_node)
13712 	    {
13713 	      remove = true;
13714 	      break;
13715 	    }
13716 	  if (oacc_async)
13717 	    c_mark_addressable (t);
13718 	  type = TREE_TYPE (t);
13719 	  if (TREE_CODE (t) == MEM_REF)
13720 	    type = TREE_TYPE (type);
13721 	  if (TREE_CODE (type) == ARRAY_TYPE)
13722 	    {
13723 	      tree oatype = type;
13724 	      gcc_assert (TREE_CODE (t) != MEM_REF);
13725 	      while (TREE_CODE (type) == ARRAY_TYPE)
13726 		type = TREE_TYPE (type);
13727 	      if (integer_zerop (TYPE_SIZE_UNIT (type)))
13728 		{
13729 		  error_at (OMP_CLAUSE_LOCATION (c),
13730 			    "%qD in %<reduction%> clause is a zero size array",
13731 			    t);
13732 		  remove = true;
13733 		  break;
13734 		}
13735 	      tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13736 				      TYPE_SIZE_UNIT (type));
13737 	      if (integer_zerop (size))
13738 		{
13739 		  error_at (OMP_CLAUSE_LOCATION (c),
13740 			    "%qD in %<reduction%> clause is a zero size array",
13741 			    t);
13742 		  remove = true;
13743 		  break;
13744 		}
13745 	      size = size_binop (MINUS_EXPR, size, size_one_node);
13746 	      size = save_expr (size);
13747 	      tree index_type = build_index_type (size);
13748 	      tree atype = build_array_type (type, index_type);
13749 	      tree ptype = build_pointer_type (type);
13750 	      if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13751 		t = build_fold_addr_expr (t);
13752 	      t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13753 	      OMP_CLAUSE_DECL (c) = t;
13754 	    }
13755 	  if (TYPE_ATOMIC (type))
13756 	    {
13757 	      error_at (OMP_CLAUSE_LOCATION (c),
13758 			"%<_Atomic%> %qE in %<reduction%> clause", t);
13759 	      remove = true;
13760 	      break;
13761 	    }
13762 	  if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13763 	      || OMP_CLAUSE_REDUCTION_TASK (c))
13764 	    {
13765 	      /* Disallow zero sized or potentially zero sized task
13766 		 reductions.  */
13767 	      if (integer_zerop (TYPE_SIZE_UNIT (type)))
13768 		{
13769 		  error_at (OMP_CLAUSE_LOCATION (c),
13770 			    "zero sized type %qT in %qs clause", type,
13771 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13772 		  remove = true;
13773 		  break;
13774 		}
13775 	      else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
13776 		{
13777 		  error_at (OMP_CLAUSE_LOCATION (c),
13778 			    "variable sized type %qT in %qs clause", type,
13779 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13780 		  remove = true;
13781 		  break;
13782 		}
13783 	    }
13784 	  if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
13785 	      && (FLOAT_TYPE_P (type)
13786 		  || TREE_CODE (type) == COMPLEX_TYPE))
13787 	    {
13788 	      enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13789 	      const char *r_name = NULL;
13790 
13791 	      switch (r_code)
13792 		{
13793 		case PLUS_EXPR:
13794 		case MULT_EXPR:
13795 		case MINUS_EXPR:
13796 		  break;
13797 		case MIN_EXPR:
13798 		  if (TREE_CODE (type) == COMPLEX_TYPE)
13799 		    r_name = "min";
13800 		  break;
13801 		case MAX_EXPR:
13802 		  if (TREE_CODE (type) == COMPLEX_TYPE)
13803 		    r_name = "max";
13804 		  break;
13805 		case BIT_AND_EXPR:
13806 		  r_name = "&";
13807 		  break;
13808 		case BIT_XOR_EXPR:
13809 		  r_name = "^";
13810 		  break;
13811 		case BIT_IOR_EXPR:
13812 		  r_name = "|";
13813 		  break;
13814 		case TRUTH_ANDIF_EXPR:
13815 		  if (FLOAT_TYPE_P (type))
13816 		    r_name = "&&";
13817 		  break;
13818 		case TRUTH_ORIF_EXPR:
13819 		  if (FLOAT_TYPE_P (type))
13820 		    r_name = "||";
13821 		  break;
13822 		default:
13823 		  gcc_unreachable ();
13824 		}
13825 	      if (r_name)
13826 		{
13827 		  error_at (OMP_CLAUSE_LOCATION (c),
13828 			    "%qE has invalid type for %<reduction(%s)%>",
13829 			    t, r_name);
13830 		  remove = true;
13831 		  break;
13832 		}
13833 	    }
13834 	  else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13835 	    {
13836 	      error_at (OMP_CLAUSE_LOCATION (c),
13837 			"user defined reduction not found for %qE", t);
13838 	      remove = true;
13839 	      break;
13840 	    }
13841 	  else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13842 	    {
13843 	      tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
13844 	      type = TYPE_MAIN_VARIANT (type);
13845 	      tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13846 					     VAR_DECL, NULL_TREE, type);
13847 	      tree decl_placeholder = NULL_TREE;
13848 	      OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13849 	      DECL_ARTIFICIAL (placeholder) = 1;
13850 	      DECL_IGNORED_P (placeholder) = 1;
13851 	      if (TREE_CODE (t) == MEM_REF)
13852 		{
13853 		  decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13854 						 VAR_DECL, NULL_TREE, type);
13855 		  OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13856 		  DECL_ARTIFICIAL (decl_placeholder) = 1;
13857 		  DECL_IGNORED_P (decl_placeholder) = 1;
13858 		}
13859 	      if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13860 		c_mark_addressable (placeholder);
13861 	      if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
13862 		c_mark_addressable (decl_placeholder ? decl_placeholder
13863 				    : OMP_CLAUSE_DECL (c));
13864 	      OMP_CLAUSE_REDUCTION_MERGE (c)
13865 		= c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13866 				   TREE_VEC_ELT (list, 0),
13867 				   TREE_VEC_ELT (list, 1),
13868 				   decl_placeholder ? decl_placeholder
13869 				   : OMP_CLAUSE_DECL (c), placeholder);
13870 	      OMP_CLAUSE_REDUCTION_MERGE (c)
13871 		= build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13872 			      void_type_node, NULL_TREE,
13873 			      OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
13874 	      TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13875 	      if (TREE_VEC_LENGTH (list) == 6)
13876 		{
13877 		  if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
13878 		    c_mark_addressable (decl_placeholder ? decl_placeholder
13879 					: OMP_CLAUSE_DECL (c));
13880 		  if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13881 		    c_mark_addressable (placeholder);
13882 		  tree init = TREE_VEC_ELT (list, 5);
13883 		  if (init == error_mark_node)
13884 		    init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13885 		  OMP_CLAUSE_REDUCTION_INIT (c)
13886 		    = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13887 				       TREE_VEC_ELT (list, 3),
13888 				       decl_placeholder ? decl_placeholder
13889 				       : OMP_CLAUSE_DECL (c), placeholder);
13890 		  if (TREE_VEC_ELT (list, 5) == error_mark_node)
13891 		    {
13892 		      tree v = decl_placeholder ? decl_placeholder : t;
13893 		      OMP_CLAUSE_REDUCTION_INIT (c)
13894 			= build2 (INIT_EXPR, TREE_TYPE (v), v,
13895 				  OMP_CLAUSE_REDUCTION_INIT (c));
13896 		    }
13897 		  if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13898 				 c_find_omp_placeholder_r,
13899 				 placeholder, NULL))
13900 		    OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13901 		}
13902 	      else
13903 		{
13904 		  tree init;
13905 		  tree v = decl_placeholder ? decl_placeholder : t;
13906 		  if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13907 		    init = build_constructor (TREE_TYPE (v), NULL);
13908 		  else
13909 		    init = fold_convert (TREE_TYPE (v), integer_zero_node);
13910 		  OMP_CLAUSE_REDUCTION_INIT (c)
13911 		    = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
13912 		}
13913 	      OMP_CLAUSE_REDUCTION_INIT (c)
13914 		= build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13915 			      void_type_node, NULL_TREE,
13916 			       OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13917 	      TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13918 	    }
13919 	  if (TREE_CODE (t) == MEM_REF)
13920 	    {
13921 	      if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13922 		  || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13923 		     != INTEGER_CST)
13924 		{
13925 		  sorry ("variable length element type in array "
13926 			 "%<reduction%> clause");
13927 		  remove = true;
13928 		  break;
13929 		}
13930 	      t = TREE_OPERAND (t, 0);
13931 	      if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13932 		t = TREE_OPERAND (t, 0);
13933 	      if (TREE_CODE (t) == ADDR_EXPR)
13934 		t = TREE_OPERAND (t, 0);
13935 	    }
13936 	  goto check_dup_generic_t;
13937 
13938 	case OMP_CLAUSE_COPYPRIVATE:
13939 	  copyprivate_seen = true;
13940 	  if (nowait_clause)
13941 	    {
13942 	      error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13943 			"%<nowait%> clause must not be used together "
13944 			"with %<copyprivate%>");
13945 	      *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13946 	      nowait_clause = NULL;
13947 	    }
13948 	  goto check_dup_generic;
13949 
13950 	case OMP_CLAUSE_COPYIN:
13951 	  t = OMP_CLAUSE_DECL (c);
13952 	  if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
13953 	    {
13954 	      error_at (OMP_CLAUSE_LOCATION (c),
13955 			"%qE must be %<threadprivate%> for %<copyin%>", t);
13956 	      remove = true;
13957 	      break;
13958 	    }
13959 	  goto check_dup_generic;
13960 
13961 	case OMP_CLAUSE_LINEAR:
13962 	  if (ort != C_ORT_OMP_DECLARE_SIMD)
13963 	    need_implicitly_determined = true;
13964 	  t = OMP_CLAUSE_DECL (c);
13965 	  if (ort != C_ORT_OMP_DECLARE_SIMD
13966 	      && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13967 	    {
13968 	      error_at (OMP_CLAUSE_LOCATION (c),
13969 			"modifier should not be specified in %<linear%> "
13970 			"clause on %<simd%> or %<for%> constructs");
13971 	      OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13972 	    }
13973 	  if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13974 	      && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13975 	    {
13976 	      error_at (OMP_CLAUSE_LOCATION (c),
13977 			"linear clause applied to non-integral non-pointer "
13978 			"variable with type %qT", TREE_TYPE (t));
13979 	      remove = true;
13980 	      break;
13981 	    }
13982 	  if (TYPE_ATOMIC (TREE_TYPE (t)))
13983 	    {
13984 	      error_at (OMP_CLAUSE_LOCATION (c),
13985 			"%<_Atomic%> %qD in %<linear%> clause", t);
13986 	      remove = true;
13987 	      break;
13988 	    }
13989 	  if (ort == C_ORT_OMP_DECLARE_SIMD)
13990 	    {
13991 	      tree s = OMP_CLAUSE_LINEAR_STEP (c);
13992 	      if (TREE_CODE (s) == PARM_DECL)
13993 		{
13994 		  OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13995 		  /* map_head bitmap is used as uniform_head if
13996 		     declare_simd.  */
13997 		  if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13998 		    linear_variable_step_check = true;
13999 		  goto check_dup_generic;
14000 		}
14001 	      if (TREE_CODE (s) != INTEGER_CST)
14002 		{
14003 		  error_at (OMP_CLAUSE_LOCATION (c),
14004 			    "%<linear%> clause step %qE is neither constant "
14005 			    "nor a parameter", s);
14006 		  remove = true;
14007 		  break;
14008 		}
14009 	    }
14010 	  if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14011 	    {
14012 	      tree s = OMP_CLAUSE_LINEAR_STEP (c);
14013 	      s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14014 				   OMP_CLAUSE_DECL (c), s);
14015 	      s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14016 				   sizetype, fold_convert (sizetype, s),
14017 				   fold_convert
14018 				     (sizetype, OMP_CLAUSE_DECL (c)));
14019 	      if (s == error_mark_node)
14020 		s = size_one_node;
14021 	      OMP_CLAUSE_LINEAR_STEP (c) = s;
14022 	    }
14023 	  else
14024 	    OMP_CLAUSE_LINEAR_STEP (c)
14025 	      = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14026 	  goto check_dup_generic;
14027 
14028 	check_dup_generic:
14029 	  t = OMP_CLAUSE_DECL (c);
14030 	check_dup_generic_t:
14031 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14032 	    {
14033 	      error_at (OMP_CLAUSE_LOCATION (c),
14034 			"%qE is not a variable in clause %qs", t,
14035 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14036 	      remove = true;
14037 	    }
14038 	  else if (ort == C_ORT_ACC
14039 		   && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14040 	    {
14041 	      if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14042 		{
14043 		  error_at (OMP_CLAUSE_LOCATION (c),
14044 			    "%qD appears more than once in reduction clauses",
14045 			    t);
14046 		  remove = true;
14047 		}
14048 	      else
14049 		bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14050 	    }
14051 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14052 		   || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14053 		   || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14054 	    {
14055 	      error_at (OMP_CLAUSE_LOCATION (c),
14056 			"%qE appears more than once in data clauses", t);
14057 	      remove = true;
14058 	    }
14059 	  else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14060 		   && bitmap_bit_p (&map_head, DECL_UID (t)))
14061 	    {
14062 	      if (ort == C_ORT_ACC)
14063 		error_at (OMP_CLAUSE_LOCATION (c),
14064 			  "%qD appears more than once in data clauses", t);
14065 	      else
14066 		error_at (OMP_CLAUSE_LOCATION (c),
14067 			  "%qD appears both in data and map clauses", t);
14068 	      remove = true;
14069 	    }
14070 	  else
14071 	    bitmap_set_bit (&generic_head, DECL_UID (t));
14072 	  break;
14073 
14074 	case OMP_CLAUSE_FIRSTPRIVATE:
14075 	  t = OMP_CLAUSE_DECL (c);
14076 	  need_complete = true;
14077 	  need_implicitly_determined = true;
14078 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14079 	    {
14080 	      error_at (OMP_CLAUSE_LOCATION (c),
14081 			"%qE is not a variable in clause %<firstprivate%>", t);
14082 	      remove = true;
14083 	    }
14084 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14085 		   || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14086 	    {
14087 	      error_at (OMP_CLAUSE_LOCATION (c),
14088 			"%qE appears more than once in data clauses", t);
14089 	      remove = true;
14090 	    }
14091 	  else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14092 	    {
14093 	      if (ort == C_ORT_ACC)
14094 		error_at (OMP_CLAUSE_LOCATION (c),
14095 			  "%qD appears more than once in data clauses", t);
14096 	      else
14097 		error_at (OMP_CLAUSE_LOCATION (c),
14098 			  "%qD appears both in data and map clauses", t);
14099 	      remove = true;
14100 	    }
14101 	  else
14102 	    bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14103 	  break;
14104 
14105 	case OMP_CLAUSE_LASTPRIVATE:
14106 	  t = OMP_CLAUSE_DECL (c);
14107 	  need_complete = true;
14108 	  need_implicitly_determined = true;
14109 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14110 	    {
14111 	      error_at (OMP_CLAUSE_LOCATION (c),
14112 			"%qE is not a variable in clause %<lastprivate%>", t);
14113 	      remove = true;
14114 	    }
14115 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14116 		   || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14117 	    {
14118 	      error_at (OMP_CLAUSE_LOCATION (c),
14119 		     "%qE appears more than once in data clauses", t);
14120 	      remove = true;
14121 	    }
14122 	  else
14123 	    bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14124 	  break;
14125 
14126 	case OMP_CLAUSE_ALIGNED:
14127 	  t = OMP_CLAUSE_DECL (c);
14128 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14129 	    {
14130 	      error_at (OMP_CLAUSE_LOCATION (c),
14131 			"%qE is not a variable in %<aligned%> clause", t);
14132 	      remove = true;
14133 	    }
14134 	  else if (!POINTER_TYPE_P (TREE_TYPE (t))
14135 		   && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14136 	    {
14137 	      error_at (OMP_CLAUSE_LOCATION (c),
14138 			"%qE in %<aligned%> clause is neither a pointer nor "
14139 			"an array", t);
14140 	      remove = true;
14141 	    }
14142 	  else if (TYPE_ATOMIC (TREE_TYPE (t)))
14143 	    {
14144 	      error_at (OMP_CLAUSE_LOCATION (c),
14145 			"%<_Atomic%> %qD in %<aligned%> clause", t);
14146 	      remove = true;
14147 	      break;
14148 	    }
14149 	  else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14150 	    {
14151 	      error_at (OMP_CLAUSE_LOCATION (c),
14152 			"%qE appears more than once in %<aligned%> clauses",
14153 			t);
14154 	      remove = true;
14155 	    }
14156 	  else
14157 	    bitmap_set_bit (&aligned_head, DECL_UID (t));
14158 	  break;
14159 
14160 	case OMP_CLAUSE_NONTEMPORAL:
14161 	  t = OMP_CLAUSE_DECL (c);
14162 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14163 	    {
14164 	      error_at (OMP_CLAUSE_LOCATION (c),
14165 			"%qE is not a variable in %<nontemporal%> clause", t);
14166 	      remove = true;
14167 	    }
14168 	  else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14169 	    {
14170 	      error_at (OMP_CLAUSE_LOCATION (c),
14171 			"%qE appears more than once in %<nontemporal%> "
14172 			"clauses", t);
14173 	      remove = true;
14174 	    }
14175 	  else
14176 	    bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14177 	  break;
14178 
14179 	case OMP_CLAUSE_DEPEND:
14180 	  t = OMP_CLAUSE_DECL (c);
14181 	  if (t == NULL_TREE)
14182 	    {
14183 	      gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14184 			  == OMP_CLAUSE_DEPEND_SOURCE);
14185 	      break;
14186 	    }
14187 	  if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14188 	    {
14189 	      gcc_assert (TREE_CODE (t) == TREE_LIST);
14190 	      for (; t; t = TREE_CHAIN (t))
14191 		{
14192 		  tree decl = TREE_VALUE (t);
14193 		  if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14194 		    {
14195 		      tree offset = TREE_PURPOSE (t);
14196 		      bool neg = wi::neg_p (wi::to_wide (offset));
14197 		      offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14198 		      tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14199 						 neg ? MINUS_EXPR : PLUS_EXPR,
14200 						 decl, offset);
14201 		      t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14202 					    sizetype,
14203 					    fold_convert (sizetype, t2),
14204 					    fold_convert (sizetype, decl));
14205 		      if (t2 == error_mark_node)
14206 			{
14207 			  remove = true;
14208 			  break;
14209 			}
14210 		      TREE_PURPOSE (t) = t2;
14211 		    }
14212 		}
14213 	      break;
14214 	    }
14215 	  if (TREE_CODE (t) == TREE_LIST
14216 	      && TREE_PURPOSE (t)
14217 	      && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14218 	    {
14219 	      if (TREE_PURPOSE (t) != last_iterators)
14220 		last_iterators_remove
14221 		  = c_omp_finish_iterators (TREE_PURPOSE (t));
14222 	      last_iterators = TREE_PURPOSE (t);
14223 	      t = TREE_VALUE (t);
14224 	      if (last_iterators_remove)
14225 		t = error_mark_node;
14226 	    }
14227 	  else
14228 	    last_iterators = NULL_TREE;
14229 	  if (TREE_CODE (t) == TREE_LIST)
14230 	    {
14231 	      if (handle_omp_array_sections (c, ort))
14232 		remove = true;
14233 	      else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14234 		{
14235 		  error_at (OMP_CLAUSE_LOCATION (c),
14236 			    "%<depend%> clause with %<depobj%> dependence "
14237 			    "type on array section");
14238 		  remove = true;
14239 		}
14240 	      break;
14241 	    }
14242 	  if (t == error_mark_node)
14243 	    remove = true;
14244 	  else if (!lvalue_p (t))
14245 	    {
14246 	      error_at (OMP_CLAUSE_LOCATION (c),
14247 			"%qE is not lvalue expression nor array section in "
14248 			"%<depend%> clause", t);
14249 	      remove = true;
14250 	    }
14251 	  else if (TREE_CODE (t) == COMPONENT_REF
14252 		   && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14253 	    {
14254 	      error_at (OMP_CLAUSE_LOCATION (c),
14255 			"bit-field %qE in %qs clause", t, "depend");
14256 	      remove = true;
14257 	    }
14258 	  else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14259 	    {
14260 	      if (!c_omp_depend_t_p (TREE_TYPE (t)))
14261 		{
14262 		  error_at (OMP_CLAUSE_LOCATION (c),
14263 			    "%qE does not have %<omp_depend_t%> type in "
14264 			    "%<depend%> clause with %<depobj%> dependence "
14265 			    "type", t);
14266 		  remove = true;
14267 		}
14268 	    }
14269 	  else if (c_omp_depend_t_p (TREE_TYPE (t)))
14270 	    {
14271 	      error_at (OMP_CLAUSE_LOCATION (c),
14272 			"%qE should not have %<omp_depend_t%> type in "
14273 			"%<depend%> clause with dependence type other than "
14274 			"%<depobj%>", t);
14275 	      remove = true;
14276 	    }
14277 	  if (!remove)
14278 	    {
14279 	      tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14280 					  t, false);
14281 	      if (addr == error_mark_node)
14282 		remove = true;
14283 	      else
14284 		{
14285 		  t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14286 					  RO_UNARY_STAR);
14287 		  if (t == error_mark_node)
14288 		    remove = true;
14289 		  else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14290 			   && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14291 			   && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14292 			       == TREE_VEC))
14293 		    TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14294 		  else
14295 		    OMP_CLAUSE_DECL (c) = t;
14296 		}
14297 	    }
14298 	  break;
14299 
14300 	case OMP_CLAUSE_MAP:
14301 	case OMP_CLAUSE_TO:
14302 	case OMP_CLAUSE_FROM:
14303 	case OMP_CLAUSE__CACHE_:
14304 	  t = OMP_CLAUSE_DECL (c);
14305 	  if (TREE_CODE (t) == TREE_LIST)
14306 	    {
14307 	      if (handle_omp_array_sections (c, ort))
14308 		remove = true;
14309 	      else
14310 		{
14311 		  t = OMP_CLAUSE_DECL (c);
14312 		  if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14313 		    {
14314 		      error_at (OMP_CLAUSE_LOCATION (c),
14315 				"array section does not have mappable type "
14316 				"in %qs clause",
14317 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14318 		      remove = true;
14319 		    }
14320 		  else if (TYPE_ATOMIC (TREE_TYPE (t)))
14321 		    {
14322 		      error_at (OMP_CLAUSE_LOCATION (c),
14323 				"%<_Atomic%> %qE in %qs clause", t,
14324 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14325 		      remove = true;
14326 		    }
14327 		  while (TREE_CODE (t) == ARRAY_REF)
14328 		    t = TREE_OPERAND (t, 0);
14329 		  if (TREE_CODE (t) == COMPONENT_REF
14330 		      && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14331 		    {
14332 		      while (TREE_CODE (t) == COMPONENT_REF)
14333 			t = TREE_OPERAND (t, 0);
14334 		      if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14335 			break;
14336 		      if (bitmap_bit_p (&map_head, DECL_UID (t)))
14337 			{
14338 			  if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14339 			    error_at (OMP_CLAUSE_LOCATION (c),
14340 				      "%qD appears more than once in motion "
14341 				      "clauses", t);
14342 			  else if (ort == C_ORT_ACC)
14343 			    error_at (OMP_CLAUSE_LOCATION (c),
14344 				      "%qD appears more than once in data "
14345 				      "clauses", t);
14346 			  else
14347 			    error_at (OMP_CLAUSE_LOCATION (c),
14348 				      "%qD appears more than once in map "
14349 				      "clauses", t);
14350 			  remove = true;
14351 			}
14352 		      else
14353 			{
14354 			  bitmap_set_bit (&map_head, DECL_UID (t));
14355 			  bitmap_set_bit (&map_field_head, DECL_UID (t));
14356 			}
14357 		    }
14358 		}
14359 	      break;
14360 	    }
14361 	  if (t == error_mark_node)
14362 	    {
14363 	      remove = true;
14364 	      break;
14365 	    }
14366 	  if (TREE_CODE (t) == COMPONENT_REF
14367 	      && (ort & C_ORT_OMP)
14368 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14369 	    {
14370 	      if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14371 		{
14372 		  error_at (OMP_CLAUSE_LOCATION (c),
14373 			    "bit-field %qE in %qs clause",
14374 			    t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14375 		  remove = true;
14376 		}
14377 	      else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14378 		{
14379 		  error_at (OMP_CLAUSE_LOCATION (c),
14380 			    "%qE does not have a mappable type in %qs clause",
14381 			    t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14382 		  remove = true;
14383 		}
14384 	      else if (TYPE_ATOMIC (TREE_TYPE (t)))
14385 		{
14386 		  error_at (OMP_CLAUSE_LOCATION (c),
14387 			    "%<_Atomic%> %qE in %qs clause", t,
14388 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14389 		  remove = true;
14390 		}
14391 	      while (TREE_CODE (t) == COMPONENT_REF)
14392 		{
14393 		  if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14394 		      == UNION_TYPE)
14395 		    {
14396 		      error_at (OMP_CLAUSE_LOCATION (c),
14397 				"%qE is a member of a union", t);
14398 		      remove = true;
14399 		      break;
14400 		    }
14401 		  t = TREE_OPERAND (t, 0);
14402 		}
14403 	      if (remove)
14404 		break;
14405 	      if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14406 		{
14407 		  if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14408 		    break;
14409 		}
14410 	    }
14411 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14412 	    {
14413 	      error_at (OMP_CLAUSE_LOCATION (c),
14414 			"%qE is not a variable in %qs clause", t,
14415 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14416 	      remove = true;
14417 	    }
14418 	  else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14419 	    {
14420 	      error_at (OMP_CLAUSE_LOCATION (c),
14421 			"%qD is threadprivate variable in %qs clause", t,
14422 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14423 	      remove = true;
14424 	    }
14425 	  else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14426 		    || (OMP_CLAUSE_MAP_KIND (c)
14427 			!= GOMP_MAP_FIRSTPRIVATE_POINTER))
14428 		   && !c_mark_addressable (t))
14429 	    remove = true;
14430 	  else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14431 		     && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
14432 			 || (OMP_CLAUSE_MAP_KIND (c)
14433 			     == GOMP_MAP_FIRSTPRIVATE_POINTER)
14434 			 || (OMP_CLAUSE_MAP_KIND (c)
14435 			     == GOMP_MAP_FORCE_DEVICEPTR)))
14436 		   && t == OMP_CLAUSE_DECL (c)
14437 		   && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14438 	    {
14439 	      error_at (OMP_CLAUSE_LOCATION (c),
14440 			"%qD does not have a mappable type in %qs clause", t,
14441 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14442 	      remove = true;
14443 	    }
14444 	  else if (TREE_TYPE (t) == error_mark_node)
14445 	    remove = true;
14446 	  else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
14447 	    {
14448 	      error_at (OMP_CLAUSE_LOCATION (c),
14449 			"%<_Atomic%> %qE in %qs clause", t,
14450 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14451 	      remove = true;
14452 	    }
14453 	  else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14454 		   && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14455 	    {
14456 	      if (bitmap_bit_p (&generic_head, DECL_UID (t))
14457 		  || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14458 		{
14459 		  error_at (OMP_CLAUSE_LOCATION (c),
14460 			    "%qD appears more than once in data clauses", t);
14461 		  remove = true;
14462 		}
14463 	      else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14464 		{
14465 		  if (ort == C_ORT_ACC)
14466 		    error_at (OMP_CLAUSE_LOCATION (c),
14467 			      "%qD appears more than once in data clauses", t);
14468 		  else
14469 		    error_at (OMP_CLAUSE_LOCATION (c),
14470 			      "%qD appears both in data and map clauses", t);
14471 		  remove = true;
14472 		}
14473 	      else
14474 		bitmap_set_bit (&generic_head, DECL_UID (t));
14475 	    }
14476 	  else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14477 	    {
14478 	      if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14479 		error_at (OMP_CLAUSE_LOCATION (c),
14480 			  "%qD appears more than once in motion clauses", t);
14481 	      else if (ort == C_ORT_ACC)
14482 		error_at (OMP_CLAUSE_LOCATION (c),
14483 			  "%qD appears more than once in data clauses", t);
14484 	      else
14485 		error_at (OMP_CLAUSE_LOCATION (c),
14486 			  "%qD appears more than once in map clauses", t);
14487 	      remove = true;
14488 	    }
14489 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14490 		   || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14491 	    {
14492 	      if (ort == C_ORT_ACC)
14493 		error_at (OMP_CLAUSE_LOCATION (c),
14494 			  "%qD appears more than once in data clauses", t);
14495 	      else
14496 		error_at (OMP_CLAUSE_LOCATION (c),
14497 			  "%qD appears both in data and map clauses", t);
14498 	      remove = true;
14499 	    }
14500 	  else
14501 	    {
14502 	      bitmap_set_bit (&map_head, DECL_UID (t));
14503 	      if (t != OMP_CLAUSE_DECL (c)
14504 		  && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14505 		bitmap_set_bit (&map_field_head, DECL_UID (t));
14506 	    }
14507 	  break;
14508 
14509 	case OMP_CLAUSE_TO_DECLARE:
14510 	case OMP_CLAUSE_LINK:
14511 	  t = OMP_CLAUSE_DECL (c);
14512 	  if (TREE_CODE (t) == FUNCTION_DECL
14513 	      && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14514 	    ;
14515 	  else if (!VAR_P (t))
14516 	    {
14517 	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14518 		error_at (OMP_CLAUSE_LOCATION (c),
14519 			  "%qE is neither a variable nor a function name in "
14520 			  "clause %qs", t,
14521 			  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14522 	      else
14523 		error_at (OMP_CLAUSE_LOCATION (c),
14524 			  "%qE is not a variable in clause %qs", t,
14525 			  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14526 	      remove = true;
14527 	    }
14528 	  else if (DECL_THREAD_LOCAL_P (t))
14529 	    {
14530 	      error_at (OMP_CLAUSE_LOCATION (c),
14531 			"%qD is threadprivate variable in %qs clause", t,
14532 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14533 	      remove = true;
14534 	    }
14535 	  else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14536 	    {
14537 	      error_at (OMP_CLAUSE_LOCATION (c),
14538 			"%qD does not have a mappable type in %qs clause", t,
14539 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14540 	      remove = true;
14541 	    }
14542 	  if (remove)
14543 	    break;
14544 	  if (bitmap_bit_p (&generic_head, DECL_UID (t)))
14545 	    {
14546 	      error_at (OMP_CLAUSE_LOCATION (c),
14547 			"%qE appears more than once on the same "
14548 			"%<declare target%> directive", t);
14549 	      remove = true;
14550 	    }
14551 	  else
14552 	    bitmap_set_bit (&generic_head, DECL_UID (t));
14553 	  break;
14554 
14555 	case OMP_CLAUSE_UNIFORM:
14556 	  t = OMP_CLAUSE_DECL (c);
14557 	  if (TREE_CODE (t) != PARM_DECL)
14558 	    {
14559 	      if (DECL_P (t))
14560 		error_at (OMP_CLAUSE_LOCATION (c),
14561 			  "%qD is not an argument in %<uniform%> clause", t);
14562 	      else
14563 		error_at (OMP_CLAUSE_LOCATION (c),
14564 			  "%qE is not an argument in %<uniform%> clause", t);
14565 	      remove = true;
14566 	      break;
14567 	    }
14568 	  /* map_head bitmap is used as uniform_head if declare_simd.  */
14569 	  bitmap_set_bit (&map_head, DECL_UID (t));
14570 	  goto check_dup_generic;
14571 
14572 	case OMP_CLAUSE_IS_DEVICE_PTR:
14573 	case OMP_CLAUSE_USE_DEVICE_PTR:
14574 	  t = OMP_CLAUSE_DECL (c);
14575 	  if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
14576 	      && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14577 	    {
14578 	      error_at (OMP_CLAUSE_LOCATION (c),
14579 			"%qs variable is neither a pointer nor an array",
14580 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14581 	      remove = true;
14582 	    }
14583 	  goto check_dup_generic;
14584 
14585 	case OMP_CLAUSE_NOWAIT:
14586 	  if (copyprivate_seen)
14587 	    {
14588 	      error_at (OMP_CLAUSE_LOCATION (c),
14589 			"%<nowait%> clause must not be used together "
14590 			"with %<copyprivate%>");
14591 	      remove = true;
14592 	      break;
14593 	    }
14594 	  nowait_clause = pc;
14595 	  pc = &OMP_CLAUSE_CHAIN (c);
14596 	  continue;
14597 
14598 	case OMP_CLAUSE_IF:
14599 	case OMP_CLAUSE_NUM_THREADS:
14600 	case OMP_CLAUSE_NUM_TEAMS:
14601 	case OMP_CLAUSE_THREAD_LIMIT:
14602 	case OMP_CLAUSE_DEFAULT:
14603 	case OMP_CLAUSE_UNTIED:
14604 	case OMP_CLAUSE_COLLAPSE:
14605 	case OMP_CLAUSE_FINAL:
14606 	case OMP_CLAUSE_MERGEABLE:
14607 	case OMP_CLAUSE_DEVICE:
14608 	case OMP_CLAUSE_DIST_SCHEDULE:
14609 	case OMP_CLAUSE_PARALLEL:
14610 	case OMP_CLAUSE_FOR:
14611 	case OMP_CLAUSE_SECTIONS:
14612 	case OMP_CLAUSE_TASKGROUP:
14613 	case OMP_CLAUSE_PROC_BIND:
14614 	case OMP_CLAUSE_PRIORITY:
14615 	case OMP_CLAUSE_GRAINSIZE:
14616 	case OMP_CLAUSE_NUM_TASKS:
14617 	case OMP_CLAUSE_THREADS:
14618 	case OMP_CLAUSE_SIMD:
14619 	case OMP_CLAUSE_HINT:
14620 	case OMP_CLAUSE_DEFAULTMAP:
14621 	case OMP_CLAUSE_NUM_GANGS:
14622 	case OMP_CLAUSE_NUM_WORKERS:
14623 	case OMP_CLAUSE_VECTOR_LENGTH:
14624 	case OMP_CLAUSE_ASYNC:
14625 	case OMP_CLAUSE_WAIT:
14626 	case OMP_CLAUSE_AUTO:
14627 	case OMP_CLAUSE_INDEPENDENT:
14628 	case OMP_CLAUSE_SEQ:
14629 	case OMP_CLAUSE_GANG:
14630 	case OMP_CLAUSE_WORKER:
14631 	case OMP_CLAUSE_VECTOR:
14632 	case OMP_CLAUSE_TILE:
14633 	case OMP_CLAUSE_IF_PRESENT:
14634 	case OMP_CLAUSE_FINALIZE:
14635 	  pc = &OMP_CLAUSE_CHAIN (c);
14636 	  continue;
14637 
14638 	case OMP_CLAUSE_NOGROUP:
14639 	  nogroup_seen = pc;
14640 	  pc = &OMP_CLAUSE_CHAIN (c);
14641 	  continue;
14642 
14643 	case OMP_CLAUSE_SCHEDULE:
14644 	  schedule_clause = c;
14645 	  pc = &OMP_CLAUSE_CHAIN (c);
14646 	  continue;
14647 
14648 	case OMP_CLAUSE_ORDERED:
14649 	  ordered_seen = true;
14650 	  pc = &OMP_CLAUSE_CHAIN (c);
14651 	  continue;
14652 
14653 	case OMP_CLAUSE_SAFELEN:
14654 	  safelen = c;
14655 	  pc = &OMP_CLAUSE_CHAIN (c);
14656 	  continue;
14657 	case OMP_CLAUSE_SIMDLEN:
14658 	  simdlen = c;
14659 	  pc = &OMP_CLAUSE_CHAIN (c);
14660 	  continue;
14661 
14662 	case OMP_CLAUSE_INBRANCH:
14663 	case OMP_CLAUSE_NOTINBRANCH:
14664 	  if (branch_seen)
14665 	    {
14666 	      error_at (OMP_CLAUSE_LOCATION (c),
14667 			"%<inbranch%> clause is incompatible with "
14668 			"%<notinbranch%>");
14669 	      remove = true;
14670 	      break;
14671 	    }
14672 	  branch_seen = true;
14673 	  pc = &OMP_CLAUSE_CHAIN (c);
14674 	  continue;
14675 
14676 	default:
14677 	  gcc_unreachable ();
14678 	}
14679 
14680       if (!remove)
14681 	{
14682 	  t = OMP_CLAUSE_DECL (c);
14683 
14684 	  if (need_complete)
14685 	    {
14686 	      t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
14687 	      if (t == error_mark_node)
14688 		remove = true;
14689 	    }
14690 
14691 	  if (need_implicitly_determined)
14692 	    {
14693 	      const char *share_name = NULL;
14694 
14695 	      if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14696 		share_name = "threadprivate";
14697 	      else switch (c_omp_predetermined_sharing (t))
14698 		{
14699 		case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
14700 		  break;
14701 		case OMP_CLAUSE_DEFAULT_SHARED:
14702 		  share_name = "shared";
14703 		  break;
14704 		case OMP_CLAUSE_DEFAULT_PRIVATE:
14705 		  share_name = "private";
14706 		  break;
14707 		default:
14708 		  gcc_unreachable ();
14709 		}
14710 	      if (share_name)
14711 		{
14712 		  error_at (OMP_CLAUSE_LOCATION (c),
14713 			    "%qE is predetermined %qs for %qs",
14714 			    t, share_name,
14715 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14716 		  remove = true;
14717 		}
14718 	      else if (TREE_READONLY (t)
14719 		       && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
14720 		       && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
14721 		{
14722 		  error_at (OMP_CLAUSE_LOCATION (c),
14723 			    "%<const%> qualified %qE may appear only in "
14724 			    "%<shared%> or %<firstprivate%> clauses", t);
14725 		  remove = true;
14726 		}
14727 	    }
14728 	}
14729 
14730       if (remove)
14731 	*pc = OMP_CLAUSE_CHAIN (c);
14732       else
14733 	pc = &OMP_CLAUSE_CHAIN (c);
14734     }
14735 
14736   if (simdlen
14737       && safelen
14738       && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
14739 			  OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
14740     {
14741       error_at (OMP_CLAUSE_LOCATION (simdlen),
14742 		"%<simdlen%> clause value is bigger than "
14743 		"%<safelen%> clause value");
14744       OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
14745 	= OMP_CLAUSE_SAFELEN_EXPR (safelen);
14746     }
14747 
14748   if (ordered_seen
14749       && schedule_clause
14750       && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14751 	  & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14752     {
14753       error_at (OMP_CLAUSE_LOCATION (schedule_clause),
14754 		"%<nonmonotonic%> schedule modifier specified together "
14755 		"with %<ordered%> clause");
14756       OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14757 	= (enum omp_clause_schedule_kind)
14758 	  (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14759 	   & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
14760     }
14761 
14762   if (linear_variable_step_check)
14763     for (pc = &clauses, c = clauses; c ; c = *pc)
14764       {
14765 	bool remove = false;
14766 	if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14767 	    && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
14768 	    && !bitmap_bit_p (&map_head,
14769 			      DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
14770 	  {
14771 	    error_at (OMP_CLAUSE_LOCATION (c),
14772 		      "%<linear%> clause step is a parameter %qD not "
14773 		      "specified in %<uniform%> clause",
14774 		      OMP_CLAUSE_LINEAR_STEP (c));
14775 	    remove = true;
14776 	  }
14777 
14778 	if (remove)
14779 	  *pc = OMP_CLAUSE_CHAIN (c);
14780 	else
14781 	  pc = &OMP_CLAUSE_CHAIN (c);
14782       }
14783 
14784   if (nogroup_seen && reduction_seen)
14785     {
14786       error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
14787 		"%<nogroup%> clause must not be used together with "
14788 		"%<reduction%> clause");
14789       *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
14790     }
14791 
14792   bitmap_obstack_release (NULL);
14793   return clauses;
14794 }
14795 
14796 /* Return code to initialize DST with a copy constructor from SRC.
14797    C doesn't have copy constructors nor assignment operators, only for
14798    _Atomic vars we need to perform __atomic_load from src into a temporary
14799    followed by __atomic_store of the temporary to dst.  */
14800 
14801 tree
c_omp_clause_copy_ctor(tree clause,tree dst,tree src)14802 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
14803 {
14804   if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
14805     return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
14806 
14807   location_t loc = OMP_CLAUSE_LOCATION (clause);
14808   tree type = TREE_TYPE (dst);
14809   tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
14810   tree tmp = create_tmp_var (nonatomic_type);
14811   tree tmp_addr = build_fold_addr_expr (tmp);
14812   TREE_ADDRESSABLE (tmp) = 1;
14813   TREE_NO_WARNING (tmp) = 1;
14814   tree src_addr = build_fold_addr_expr (src);
14815   tree dst_addr = build_fold_addr_expr (dst);
14816   tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14817   vec<tree, va_gc> *params;
14818   /* Expansion of a generic atomic load may require an addition
14819      element, so allocate enough to prevent a resize.  */
14820   vec_alloc (params, 4);
14821 
14822   /* Build __atomic_load (&src, &tmp, SEQ_CST);  */
14823   tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14824   params->quick_push (src_addr);
14825   params->quick_push (tmp_addr);
14826   params->quick_push (seq_cst);
14827   tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14828 
14829   vec_alloc (params, 4);
14830 
14831   /* Build __atomic_store (&dst, &tmp, SEQ_CST);  */
14832   fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14833   params->quick_push (dst_addr);
14834   params->quick_push (tmp_addr);
14835   params->quick_push (seq_cst);
14836   tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14837   return build2 (COMPOUND_EXPR, void_type_node, load, store);
14838 }
14839 
14840 /* Create a transaction node.  */
14841 
14842 tree
c_finish_transaction(location_t loc,tree block,int flags)14843 c_finish_transaction (location_t loc, tree block, int flags)
14844 {
14845   tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14846   if (flags & TM_STMT_ATTR_OUTER)
14847     TRANSACTION_EXPR_OUTER (stmt) = 1;
14848   if (flags & TM_STMT_ATTR_RELAXED)
14849     TRANSACTION_EXPR_RELAXED (stmt) = 1;
14850   return add_stmt (stmt);
14851 }
14852 
14853 /* Make a variant type in the proper way for C/C++, propagating qualifiers
14854    down to the element type of an array.  If ORIG_QUAL_TYPE is not
14855    NULL, then it should be used as the qualified type
14856    ORIG_QUAL_INDIRECT levels down in array type derivation (to
14857    preserve information about the typedef name from which an array
14858    type was derived).  */
14859 
14860 tree
c_build_qualified_type(tree type,int type_quals,tree orig_qual_type,size_t orig_qual_indirect)14861 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14862 			size_t orig_qual_indirect)
14863 {
14864   if (type == error_mark_node)
14865     return type;
14866 
14867   if (TREE_CODE (type) == ARRAY_TYPE)
14868     {
14869       tree t;
14870       tree element_type = c_build_qualified_type (TREE_TYPE (type),
14871 						  type_quals, orig_qual_type,
14872 						  orig_qual_indirect - 1);
14873 
14874       /* See if we already have an identically qualified type.  */
14875       if (orig_qual_type && orig_qual_indirect == 0)
14876 	t = orig_qual_type;
14877       else
14878 	for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14879 	  {
14880 	    if (TYPE_QUALS (strip_array_types (t)) == type_quals
14881 		&& TYPE_NAME (t) == TYPE_NAME (type)
14882 		&& TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14883 		&& attribute_list_equal (TYPE_ATTRIBUTES (t),
14884 					 TYPE_ATTRIBUTES (type)))
14885 	      break;
14886 	  }
14887       if (!t)
14888 	{
14889           tree domain = TYPE_DOMAIN (type);
14890 
14891 	  t = build_variant_type_copy (type);
14892 	  TREE_TYPE (t) = element_type;
14893 
14894           if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14895               || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14896             SET_TYPE_STRUCTURAL_EQUALITY (t);
14897           else if (TYPE_CANONICAL (element_type) != element_type
14898                    || (domain && TYPE_CANONICAL (domain) != domain))
14899             {
14900               tree unqualified_canon
14901                 = build_array_type (TYPE_CANONICAL (element_type),
14902                                     domain? TYPE_CANONICAL (domain)
14903                                           : NULL_TREE);
14904               if (TYPE_REVERSE_STORAGE_ORDER (type))
14905                 {
14906                   unqualified_canon
14907                     = build_distinct_type_copy (unqualified_canon);
14908                   TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14909                 }
14910               TYPE_CANONICAL (t)
14911                 = c_build_qualified_type (unqualified_canon, type_quals);
14912             }
14913           else
14914             TYPE_CANONICAL (t) = t;
14915 	}
14916       return t;
14917     }
14918 
14919   /* A restrict-qualified pointer type must be a pointer to object or
14920      incomplete type.  Note that the use of POINTER_TYPE_P also allows
14921      REFERENCE_TYPEs, which is appropriate for C++.  */
14922   if ((type_quals & TYPE_QUAL_RESTRICT)
14923       && (!POINTER_TYPE_P (type)
14924 	  || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14925     {
14926       error ("invalid use of %<restrict%>");
14927       type_quals &= ~TYPE_QUAL_RESTRICT;
14928     }
14929 
14930   tree var_type = (orig_qual_type && orig_qual_indirect == 0
14931 		   ? orig_qual_type
14932 		   : build_qualified_type (type, type_quals));
14933   /* A variant type does not inherit the list of incomplete vars from the
14934      type main variant.  */
14935   if (RECORD_OR_UNION_TYPE_P (var_type)
14936       && TYPE_MAIN_VARIANT (var_type) != var_type)
14937     C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14938   return var_type;
14939 }
14940 
14941 /* Build a VA_ARG_EXPR for the C parser.  */
14942 
14943 tree
c_build_va_arg(location_t loc1,tree expr,location_t loc2,tree type)14944 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
14945 {
14946   if (error_operand_p (type))
14947     return error_mark_node;
14948   /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14949      order because it takes the address of the expression.  */
14950   else if (handled_component_p (expr)
14951 	   && reverse_storage_order_for_component_p (expr))
14952     {
14953       error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14954       return error_mark_node;
14955     }
14956   else if (!COMPLETE_TYPE_P (type))
14957     {
14958       error_at (loc2, "second argument to %<va_arg%> is of incomplete "
14959 		"type %qT", type);
14960       return error_mark_node;
14961     }
14962   else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
14963     warning_at (loc2, OPT_Wc___compat,
14964 		"C++ requires promoted type, not enum type, in %<va_arg%>");
14965   return build_va_arg (loc2, expr, type);
14966 }
14967 
14968 /* Return truthvalue of whether T1 is the same tree structure as T2.
14969    Return 1 if they are the same. Return false if they are different.  */
14970 
14971 bool
c_tree_equal(tree t1,tree t2)14972 c_tree_equal (tree t1, tree t2)
14973 {
14974   enum tree_code code1, code2;
14975 
14976   if (t1 == t2)
14977     return true;
14978   if (!t1 || !t2)
14979     return false;
14980 
14981   for (code1 = TREE_CODE (t1);
14982        CONVERT_EXPR_CODE_P (code1)
14983 	 || code1 == NON_LVALUE_EXPR;
14984        code1 = TREE_CODE (t1))
14985     t1 = TREE_OPERAND (t1, 0);
14986   for (code2 = TREE_CODE (t2);
14987        CONVERT_EXPR_CODE_P (code2)
14988 	 || code2 == NON_LVALUE_EXPR;
14989        code2 = TREE_CODE (t2))
14990     t2 = TREE_OPERAND (t2, 0);
14991 
14992   /* They might have become equal now.  */
14993   if (t1 == t2)
14994     return true;
14995 
14996   if (code1 != code2)
14997     return false;
14998 
14999   switch (code1)
15000     {
15001     case INTEGER_CST:
15002       return wi::to_wide (t1) == wi::to_wide (t2);
15003 
15004     case REAL_CST:
15005       return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15006 
15007     case STRING_CST:
15008       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15009 	&& !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15010 		    TREE_STRING_LENGTH (t1));
15011 
15012     case FIXED_CST:
15013       return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15014 				     TREE_FIXED_CST (t2));
15015 
15016     case COMPLEX_CST:
15017       return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15018 	     && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15019 
15020     case VECTOR_CST:
15021       return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15022 
15023     case CONSTRUCTOR:
15024       /* We need to do this when determining whether or not two
15025 	 non-type pointer to member function template arguments
15026 	 are the same.  */
15027       if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15028 	  || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15029 	return false;
15030       {
15031 	tree field, value;
15032 	unsigned int i;
15033 	FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15034 	  {
15035 	    constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15036 	    if (!c_tree_equal (field, elt2->index)
15037 		|| !c_tree_equal (value, elt2->value))
15038 	      return false;
15039 	  }
15040       }
15041       return true;
15042 
15043     case TREE_LIST:
15044       if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15045 	return false;
15046       if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15047 	return false;
15048       return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15049 
15050     case SAVE_EXPR:
15051       return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15052 
15053     case CALL_EXPR:
15054       {
15055 	tree arg1, arg2;
15056 	call_expr_arg_iterator iter1, iter2;
15057 	if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
15058 	  return false;
15059 	for (arg1 = first_call_expr_arg (t1, &iter1),
15060 	       arg2 = first_call_expr_arg (t2, &iter2);
15061 	     arg1 && arg2;
15062 	     arg1 = next_call_expr_arg (&iter1),
15063 	       arg2 = next_call_expr_arg (&iter2))
15064 	  if (!c_tree_equal (arg1, arg2))
15065 	    return false;
15066 	if (arg1 || arg2)
15067 	  return false;
15068 	return true;
15069       }
15070 
15071     case TARGET_EXPR:
15072       {
15073 	tree o1 = TREE_OPERAND (t1, 0);
15074 	tree o2 = TREE_OPERAND (t2, 0);
15075 
15076 	/* Special case: if either target is an unallocated VAR_DECL,
15077 	   it means that it's going to be unified with whatever the
15078 	   TARGET_EXPR is really supposed to initialize, so treat it
15079 	   as being equivalent to anything.  */
15080 	if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
15081 	    && !DECL_RTL_SET_P (o1))
15082 	  /*Nop*/;
15083 	else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
15084 		 && !DECL_RTL_SET_P (o2))
15085 	  /*Nop*/;
15086 	else if (!c_tree_equal (o1, o2))
15087 	  return false;
15088 
15089 	return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
15090       }
15091 
15092     case COMPONENT_REF:
15093       if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
15094 	return false;
15095       return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15096 
15097     case PARM_DECL:
15098     case VAR_DECL:
15099     case CONST_DECL:
15100     case FIELD_DECL:
15101     case FUNCTION_DECL:
15102     case IDENTIFIER_NODE:
15103     case SSA_NAME:
15104       return false;
15105 
15106     case TREE_VEC:
15107       {
15108 	unsigned ix;
15109 	if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
15110 	  return false;
15111 	for (ix = TREE_VEC_LENGTH (t1); ix--;)
15112 	  if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
15113 			     TREE_VEC_ELT (t2, ix)))
15114 	    return false;
15115 	return true;
15116       }
15117 
15118     default:
15119       break;
15120     }
15121 
15122   switch (TREE_CODE_CLASS (code1))
15123     {
15124     case tcc_unary:
15125     case tcc_binary:
15126     case tcc_comparison:
15127     case tcc_expression:
15128     case tcc_vl_exp:
15129     case tcc_reference:
15130     case tcc_statement:
15131       {
15132 	int i, n = TREE_OPERAND_LENGTH (t1);
15133 
15134 	switch (code1)
15135 	  {
15136 	  case PREINCREMENT_EXPR:
15137 	  case PREDECREMENT_EXPR:
15138 	  case POSTINCREMENT_EXPR:
15139 	  case POSTDECREMENT_EXPR:
15140 	    n = 1;
15141 	    break;
15142 	  case ARRAY_REF:
15143 	    n = 2;
15144 	    break;
15145 	  default:
15146 	    break;
15147 	  }
15148 
15149 	if (TREE_CODE_CLASS (code1) == tcc_vl_exp
15150 	    && n != TREE_OPERAND_LENGTH (t2))
15151 	  return false;
15152 
15153 	for (i = 0; i < n; ++i)
15154 	  if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
15155 	    return false;
15156 
15157 	return true;
15158       }
15159 
15160     case tcc_type:
15161       return comptypes (t1, t2);
15162     default:
15163       gcc_unreachable ();
15164     }
15165   /* We can get here with --disable-checking.  */
15166   return false;
15167 }
15168 
15169 /* Returns true when the function declaration FNDECL is implicit,
15170    introduced as a result of a call to an otherwise undeclared
15171    function, and false otherwise.  */
15172 
15173 bool
c_decl_implicit(const_tree fndecl)15174 c_decl_implicit (const_tree fndecl)
15175 {
15176   return C_DECL_IMPLICIT (fndecl);
15177 }
15178