xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/c/c-typeck.c (revision 23f5f46327e37e7811da3520f4bb933f9489322f)
1 /* Build expressions with type checking for C compiler.
2    Copyright (C) 1987-2020 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 /* If NTYPE is a type of a non-variadic function with a prototype
358    and OTYPE is a type of a function without a prototype and ATTRS
359    contains attribute format, diagnosess and removes it from ATTRS.
360    Returns the result of build_type_attribute_variant of NTYPE and
361    the (possibly) modified ATTRS.  */
362 
363 static tree
build_functype_attribute_variant(tree ntype,tree otype,tree attrs)364 build_functype_attribute_variant (tree ntype, tree otype, tree attrs)
365 {
366   if (!prototype_p (otype)
367       && prototype_p (ntype)
368       && lookup_attribute ("format", attrs))
369     {
370       warning_at (input_location, OPT_Wattributes,
371 		  "%qs attribute cannot be applied to a function that "
372 		  "does not take variable arguments", "format");
373       attrs = remove_attribute ("format", attrs);
374     }
375   return build_type_attribute_variant (ntype, attrs);
376 
377 }
378 /* Return the composite type of two compatible types.
379 
380    We assume that comptypes has already been done and returned
381    nonzero; if that isn't so, this may crash.  In particular, we
382    assume that qualifiers match.  */
383 
384 tree
composite_type(tree t1,tree t2)385 composite_type (tree t1, tree t2)
386 {
387   enum tree_code code1;
388   enum tree_code code2;
389   tree attributes;
390 
391   /* Save time if the two types are the same.  */
392 
393   if (t1 == t2) return t1;
394 
395   /* If one type is nonsense, use the other.  */
396   if (t1 == error_mark_node)
397     return t2;
398   if (t2 == error_mark_node)
399     return t1;
400 
401   code1 = TREE_CODE (t1);
402   code2 = TREE_CODE (t2);
403 
404   /* Merge the attributes.  */
405   attributes = targetm.merge_type_attributes (t1, t2);
406 
407   /* If one is an enumerated type and the other is the compatible
408      integer type, the composite type might be either of the two
409      (DR#013 question 3).  For consistency, use the enumerated type as
410      the composite type.  */
411 
412   if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
413     return t1;
414   if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
415     return t2;
416 
417   gcc_assert (code1 == code2);
418 
419   switch (code1)
420     {
421     case POINTER_TYPE:
422       /* For two pointers, do this recursively on the target type.  */
423       {
424 	tree pointed_to_1 = TREE_TYPE (t1);
425 	tree pointed_to_2 = TREE_TYPE (t2);
426 	tree target = composite_type (pointed_to_1, pointed_to_2);
427         t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
428 	t1 = build_type_attribute_variant (t1, attributes);
429 	return qualify_type (t1, t2);
430       }
431 
432     case ARRAY_TYPE:
433       {
434 	tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
435 	int quals;
436 	tree unqual_elt;
437 	tree d1 = TYPE_DOMAIN (t1);
438 	tree d2 = TYPE_DOMAIN (t2);
439 	bool d1_variable, d2_variable;
440 	bool d1_zero, d2_zero;
441 	bool t1_complete, t2_complete;
442 
443 	/* We should not have any type quals on arrays at all.  */
444 	gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
445 		    && !TYPE_QUALS_NO_ADDR_SPACE (t2));
446 
447 	t1_complete = COMPLETE_TYPE_P (t1);
448 	t2_complete = COMPLETE_TYPE_P (t2);
449 
450 	d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
451 	d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
452 
453 	d1_variable = (!d1_zero
454 		       && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
455 			   || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
456 	d2_variable = (!d2_zero
457 		       && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
458 			   || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
459 	d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
460 	d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
461 
462 	/* Save space: see if the result is identical to one of the args.  */
463 	if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
464 	    && (d2_variable || d2_zero || !d1_variable))
465 	  return build_type_attribute_variant (t1, attributes);
466 	if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
467 	    && (d1_variable || d1_zero || !d2_variable))
468 	  return build_type_attribute_variant (t2, attributes);
469 
470 	if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
471 	  return build_type_attribute_variant (t1, attributes);
472 	if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
473 	  return build_type_attribute_variant (t2, attributes);
474 
475 	/* Merge the element types, and have a size if either arg has
476 	   one.  We may have qualifiers on the element types.  To set
477 	   up TYPE_MAIN_VARIANT correctly, we need to form the
478 	   composite of the unqualified types and add the qualifiers
479 	   back at the end.  */
480 	quals = TYPE_QUALS (strip_array_types (elt));
481 	unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
482 	t1 = build_array_type (unqual_elt,
483 			       TYPE_DOMAIN ((TYPE_DOMAIN (t1)
484 					     && (d2_variable
485 						 || d2_zero
486 						 || !d1_variable))
487 					    ? t1
488 					    : t2));
489 	/* Ensure a composite type involving a zero-length array type
490 	   is a zero-length type not an incomplete type.  */
491 	if (d1_zero && d2_zero
492 	    && (t1_complete || t2_complete)
493 	    && !COMPLETE_TYPE_P (t1))
494 	  {
495 	    TYPE_SIZE (t1) = bitsize_zero_node;
496 	    TYPE_SIZE_UNIT (t1) = size_zero_node;
497 	  }
498 	t1 = c_build_qualified_type (t1, quals);
499 	return build_type_attribute_variant (t1, attributes);
500       }
501 
502     case ENUMERAL_TYPE:
503     case RECORD_TYPE:
504     case UNION_TYPE:
505       if (attributes != NULL)
506 	{
507 	  /* Try harder not to create a new aggregate type.  */
508 	  if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
509 	    return t1;
510 	  if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
511 	    return t2;
512 	}
513       return build_type_attribute_variant (t1, attributes);
514 
515     case FUNCTION_TYPE:
516       /* Function types: prefer the one that specified arg types.
517 	 If both do, merge the arg types.  Also merge the return types.  */
518       {
519 	tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
520 	tree p1 = TYPE_ARG_TYPES (t1);
521 	tree p2 = TYPE_ARG_TYPES (t2);
522 	int len;
523 	tree newargs, n;
524 	int i;
525 
526 	/* Save space: see if the result is identical to one of the args.  */
527 	if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
528 	  return build_functype_attribute_variant (t1, t2, attributes);
529 	if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
530 	  return build_functype_attribute_variant (t2, t1, attributes);
531 
532 	/* Simple way if one arg fails to specify argument types.  */
533 	if (TYPE_ARG_TYPES (t1) == NULL_TREE)
534 	 {
535 	    t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
536 	    t1 = build_type_attribute_variant (t1, attributes);
537 	    return qualify_type (t1, t2);
538 	 }
539 	if (TYPE_ARG_TYPES (t2) == NULL_TREE)
540 	 {
541 	   t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
542 	   t1 = build_type_attribute_variant (t1, attributes);
543 	   return qualify_type (t1, t2);
544 	 }
545 
546 	/* If both args specify argument types, we must merge the two
547 	   lists, argument by argument.  */
548 
549 	for (len = 0, newargs = p1;
550 	     newargs && newargs != void_list_node;
551 	     len++, newargs = TREE_CHAIN (newargs))
552 	  ;
553 
554 	for (i = 0; i < len; i++)
555 	  newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
556 
557 	n = newargs;
558 
559 	for (; p1 && p1 != void_list_node;
560 	     p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
561 	  {
562 	    /* A null type means arg type is not specified.
563 	       Take whatever the other function type has.  */
564 	    if (TREE_VALUE (p1) == NULL_TREE)
565 	      {
566 		TREE_VALUE (n) = TREE_VALUE (p2);
567 		goto parm_done;
568 	      }
569 	    if (TREE_VALUE (p2) == NULL_TREE)
570 	      {
571 		TREE_VALUE (n) = TREE_VALUE (p1);
572 		goto parm_done;
573 	      }
574 
575 	    /* Given  wait (union {union wait *u; int *i} *)
576 	       and  wait (union wait *),
577 	       prefer  union wait *  as type of parm.  */
578 	    if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
579 		&& TREE_VALUE (p1) != TREE_VALUE (p2))
580 	      {
581 		tree memb;
582 		tree mv2 = TREE_VALUE (p2);
583 		if (mv2 && mv2 != error_mark_node
584 		    && TREE_CODE (mv2) != ARRAY_TYPE)
585 		  mv2 = TYPE_MAIN_VARIANT (mv2);
586 		for (memb = TYPE_FIELDS (TREE_VALUE (p1));
587 		     memb; memb = DECL_CHAIN (memb))
588 		  {
589 		    tree mv3 = TREE_TYPE (memb);
590 		    if (mv3 && mv3 != error_mark_node
591 			&& TREE_CODE (mv3) != ARRAY_TYPE)
592 		      mv3 = TYPE_MAIN_VARIANT (mv3);
593 		    if (comptypes (mv3, mv2))
594 		      {
595 			TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
596 							 TREE_VALUE (p2));
597 			pedwarn (input_location, OPT_Wpedantic,
598 				 "function types not truly compatible in ISO C");
599 			goto parm_done;
600 		      }
601 		  }
602 	      }
603 	    if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
604 		&& TREE_VALUE (p2) != TREE_VALUE (p1))
605 	      {
606 		tree memb;
607 		tree mv1 = TREE_VALUE (p1);
608 		if (mv1 && mv1 != error_mark_node
609 		    && TREE_CODE (mv1) != ARRAY_TYPE)
610 		  mv1 = TYPE_MAIN_VARIANT (mv1);
611 		for (memb = TYPE_FIELDS (TREE_VALUE (p2));
612 		     memb; memb = DECL_CHAIN (memb))
613 		  {
614 		    tree mv3 = TREE_TYPE (memb);
615 		    if (mv3 && mv3 != error_mark_node
616 			&& TREE_CODE (mv3) != ARRAY_TYPE)
617 		      mv3 = TYPE_MAIN_VARIANT (mv3);
618 		    if (comptypes (mv3, mv1))
619 		      {
620 			TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
621 							 TREE_VALUE (p1));
622 			pedwarn (input_location, OPT_Wpedantic,
623 				 "function types not truly compatible in ISO C");
624 			goto parm_done;
625 		      }
626 		  }
627 	      }
628 	    TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
629 	  parm_done: ;
630 	  }
631 
632 	t1 = build_function_type (valtype, newargs);
633 	t1 = qualify_type (t1, t2);
634       }
635       /* FALLTHRU */
636 
637     default:
638       return build_type_attribute_variant (t1, attributes);
639     }
640 
641 }
642 
643 /* Return the type of a conditional expression between pointers to
644    possibly differently qualified versions of compatible types.
645 
646    We assume that comp_target_types has already been done and returned
647    nonzero; if that isn't so, this may crash.  */
648 
649 static tree
common_pointer_type(tree t1,tree t2)650 common_pointer_type (tree t1, tree t2)
651 {
652   tree attributes;
653   tree pointed_to_1, mv1;
654   tree pointed_to_2, mv2;
655   tree target;
656   unsigned target_quals;
657   addr_space_t as1, as2, as_common;
658   int quals1, quals2;
659 
660   /* Save time if the two types are the same.  */
661 
662   if (t1 == t2) return t1;
663 
664   /* If one type is nonsense, use the other.  */
665   if (t1 == error_mark_node)
666     return t2;
667   if (t2 == error_mark_node)
668     return t1;
669 
670   gcc_assert (TREE_CODE (t1) == POINTER_TYPE
671 	      && TREE_CODE (t2) == POINTER_TYPE);
672 
673   /* Merge the attributes.  */
674   attributes = targetm.merge_type_attributes (t1, t2);
675 
676   /* Find the composite type of the target types, and combine the
677      qualifiers of the two types' targets.  Do not lose qualifiers on
678      array element types by taking the TYPE_MAIN_VARIANT.  */
679   mv1 = pointed_to_1 = TREE_TYPE (t1);
680   mv2 = pointed_to_2 = TREE_TYPE (t2);
681   if (TREE_CODE (mv1) != ARRAY_TYPE)
682     mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
683   if (TREE_CODE (mv2) != ARRAY_TYPE)
684     mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
685   target = composite_type (mv1, mv2);
686 
687   /* Strip array types to get correct qualifier for pointers to arrays */
688   quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
689   quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
690 
691   /* For function types do not merge const qualifiers, but drop them
692      if used inconsistently.  The middle-end uses these to mark const
693      and noreturn functions.  */
694   if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
695     target_quals = (quals1 & quals2);
696   else
697     target_quals = (quals1 | quals2);
698 
699   /* If the two named address spaces are different, determine the common
700      superset address space.  This is guaranteed to exist due to the
701      assumption that comp_target_type returned non-zero.  */
702   as1 = TYPE_ADDR_SPACE (pointed_to_1);
703   as2 = TYPE_ADDR_SPACE (pointed_to_2);
704   if (!addr_space_superset (as1, as2, &as_common))
705     gcc_unreachable ();
706 
707   target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
708 
709   t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
710   return build_type_attribute_variant (t1, attributes);
711 }
712 
713 /* Return the common type for two arithmetic types under the usual
714    arithmetic conversions.  The default conversions have already been
715    applied, and enumerated types converted to their compatible integer
716    types.  The resulting type is unqualified and has no attributes.
717 
718    This is the type for the result of most arithmetic operations
719    if the operands have the given two types.  */
720 
721 static tree
c_common_type(tree t1,tree t2)722 c_common_type (tree t1, tree t2)
723 {
724   enum tree_code code1;
725   enum tree_code code2;
726 
727   /* If one type is nonsense, use the other.  */
728   if (t1 == error_mark_node)
729     return t2;
730   if (t2 == error_mark_node)
731     return t1;
732 
733   if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
734     t1 = TYPE_MAIN_VARIANT (t1);
735 
736   if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
737     t2 = TYPE_MAIN_VARIANT (t2);
738 
739   if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
740     t1 = build_type_attribute_variant (t1, NULL_TREE);
741 
742   if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
743     t2 = build_type_attribute_variant (t2, NULL_TREE);
744 
745   /* Save time if the two types are the same.  */
746 
747   if (t1 == t2) return t1;
748 
749   code1 = TREE_CODE (t1);
750   code2 = TREE_CODE (t2);
751 
752   gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
753 	      || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
754 	      || code1 == INTEGER_TYPE);
755   gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
756 	      || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
757 	      || code2 == INTEGER_TYPE);
758 
759   /* When one operand is a decimal float type, the other operand cannot be
760      a generic float type or a complex type.  We also disallow vector types
761      here.  */
762   if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
763       && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
764     {
765       if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
766 	{
767 	  error ("cannot mix operands of decimal floating and vector types");
768 	  return error_mark_node;
769 	}
770       if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
771 	{
772 	  error ("cannot mix operands of decimal floating and complex types");
773 	  return error_mark_node;
774 	}
775       if (code1 == REAL_TYPE && code2 == REAL_TYPE)
776 	{
777 	  error ("cannot mix operands of decimal floating "
778 		 "and other floating types");
779 	  return error_mark_node;
780 	}
781     }
782 
783   /* If one type is a vector type, return that type.  (How the usual
784      arithmetic conversions apply to the vector types extension is not
785      precisely specified.)  */
786   if (code1 == VECTOR_TYPE)
787     return t1;
788 
789   if (code2 == VECTOR_TYPE)
790     return t2;
791 
792   /* If one type is complex, form the common type of the non-complex
793      components, then make that complex.  Use T1 or T2 if it is the
794      required type.  */
795   if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
796     {
797       tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
798       tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
799       tree subtype = c_common_type (subtype1, subtype2);
800 
801       if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
802 	return t1;
803       else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
804 	return t2;
805       else
806 	return build_complex_type (subtype);
807     }
808 
809   /* If only one is real, use it as the result.  */
810 
811   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
812     return t1;
813 
814   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
815     return t2;
816 
817   /* If both are real and either are decimal floating point types, use
818      the decimal floating point type with the greater precision. */
819 
820   if (code1 == REAL_TYPE && code2 == REAL_TYPE)
821     {
822       if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
823 	  || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
824 	return dfloat128_type_node;
825       else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
826 	       || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
827 	return dfloat64_type_node;
828       else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
829 	       || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
830 	return dfloat32_type_node;
831     }
832 
833   /* Deal with fixed-point types.  */
834   if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
835     {
836       unsigned int unsignedp = 0, satp = 0;
837       scalar_mode m1, m2;
838       unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
839 
840       m1 = SCALAR_TYPE_MODE (t1);
841       m2 = SCALAR_TYPE_MODE (t2);
842 
843       /* If one input type is saturating, the result type is saturating.  */
844       if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
845 	satp = 1;
846 
847       /* If both fixed-point types are unsigned, the result type is unsigned.
848 	 When mixing fixed-point and integer types, follow the sign of the
849 	 fixed-point type.
850 	 Otherwise, the result type is signed.  */
851       if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
852 	   && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
853 	  || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
854 	      && TYPE_UNSIGNED (t1))
855 	  || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
856 	      && TYPE_UNSIGNED (t2)))
857 	unsignedp = 1;
858 
859       /* The result type is signed.  */
860       if (unsignedp == 0)
861 	{
862 	  /* If the input type is unsigned, we need to convert to the
863 	     signed type.  */
864 	  if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
865 	    {
866 	      enum mode_class mclass = (enum mode_class) 0;
867 	      if (GET_MODE_CLASS (m1) == MODE_UFRACT)
868 		mclass = MODE_FRACT;
869 	      else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
870 		mclass = MODE_ACCUM;
871 	      else
872 		gcc_unreachable ();
873 	      m1 = as_a <scalar_mode>
874 		(mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
875 	    }
876 	  if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
877 	    {
878 	      enum mode_class mclass = (enum mode_class) 0;
879 	      if (GET_MODE_CLASS (m2) == MODE_UFRACT)
880 		mclass = MODE_FRACT;
881 	      else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
882 		mclass = MODE_ACCUM;
883 	      else
884 		gcc_unreachable ();
885 	      m2 = as_a <scalar_mode>
886 		(mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
887 	    }
888 	}
889 
890       if (code1 == FIXED_POINT_TYPE)
891 	{
892 	  fbit1 = GET_MODE_FBIT (m1);
893 	  ibit1 = GET_MODE_IBIT (m1);
894 	}
895       else
896 	{
897 	  fbit1 = 0;
898 	  /* Signed integers need to subtract one sign bit.  */
899 	  ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
900 	}
901 
902       if (code2 == FIXED_POINT_TYPE)
903 	{
904 	  fbit2 = GET_MODE_FBIT (m2);
905 	  ibit2 = GET_MODE_IBIT (m2);
906 	}
907       else
908 	{
909 	  fbit2 = 0;
910 	  /* Signed integers need to subtract one sign bit.  */
911 	  ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
912 	}
913 
914       max_ibit = ibit1 >= ibit2 ?  ibit1 : ibit2;
915       max_fbit = fbit1 >= fbit2 ?  fbit1 : fbit2;
916       return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
917 						 satp);
918     }
919 
920   /* Both real or both integers; use the one with greater precision.  */
921 
922   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
923     return t1;
924   else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
925     return t2;
926 
927   /* Same precision.  Prefer long longs to longs to ints when the
928      same precision, following the C99 rules on integer type rank
929      (which are equivalent to the C90 rules for C90 types).  */
930 
931   if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
932       || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
933     return long_long_unsigned_type_node;
934 
935   if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
936       || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
937     {
938       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
939 	return long_long_unsigned_type_node;
940       else
941 	return long_long_integer_type_node;
942     }
943 
944   if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
945       || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
946     return long_unsigned_type_node;
947 
948   if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
949       || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
950     {
951       /* But preserve unsignedness from the other type,
952 	 since long cannot hold all the values of an unsigned int.  */
953       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
954 	return long_unsigned_type_node;
955       else
956 	return long_integer_type_node;
957     }
958 
959   /* For floating types of the same TYPE_PRECISION (which we here
960      assume means either the same set of values, or sets of values
961      neither a subset of the other, with behavior being undefined in
962      the latter case), follow the rules from TS 18661-3: prefer
963      interchange types _FloatN, then standard types long double,
964      double, float, then extended types _FloatNx.  For extended types,
965      check them starting with _Float128x as that seems most consistent
966      in spirit with preferring long double to double; for interchange
967      types, also check in that order for consistency although it's not
968      possible for more than one of them to have the same
969      precision.  */
970   tree mv1 = TYPE_MAIN_VARIANT (t1);
971   tree mv2 = TYPE_MAIN_VARIANT (t2);
972 
973   for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
974     if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
975       return FLOATN_TYPE_NODE (i);
976 
977   /* Likewise, prefer long double to double even if same size.  */
978   if (mv1 == long_double_type_node || mv2 == long_double_type_node)
979     return long_double_type_node;
980 
981   /* Likewise, prefer double to float even if same size.
982      We got a couple of embedded targets with 32 bit doubles, and the
983      pdp11 might have 64 bit floats.  */
984   if (mv1 == double_type_node || mv2 == double_type_node)
985     return double_type_node;
986 
987   if (mv1 == float_type_node || mv2 == float_type_node)
988     return float_type_node;
989 
990   for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
991     if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
992       return FLOATNX_TYPE_NODE (i);
993 
994   /* Otherwise prefer the unsigned one.  */
995 
996   if (TYPE_UNSIGNED (t1))
997     return t1;
998   else
999     return t2;
1000 }
1001 
1002 /* Wrapper around c_common_type that is used by c-common.c and other
1003    front end optimizations that remove promotions.  ENUMERAL_TYPEs
1004    are allowed here and are converted to their compatible integer types.
1005    BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
1006    preferably a non-Boolean type as the common type.  */
1007 tree
common_type(tree t1,tree t2)1008 common_type (tree t1, tree t2)
1009 {
1010   if (TREE_CODE (t1) == ENUMERAL_TYPE)
1011     t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
1012   if (TREE_CODE (t2) == ENUMERAL_TYPE)
1013     t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
1014 
1015   /* If both types are BOOLEAN_TYPE, then return boolean_type_node.  */
1016   if (TREE_CODE (t1) == BOOLEAN_TYPE
1017       && TREE_CODE (t2) == BOOLEAN_TYPE)
1018     return boolean_type_node;
1019 
1020   /* If either type is BOOLEAN_TYPE, then return the other.  */
1021   if (TREE_CODE (t1) == BOOLEAN_TYPE)
1022     return t2;
1023   if (TREE_CODE (t2) == BOOLEAN_TYPE)
1024     return t1;
1025 
1026   return c_common_type (t1, t2);
1027 }
1028 
1029 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1030    or various other operations.  Return 2 if they are compatible
1031    but a warning may be needed if you use them together.  */
1032 
1033 int
comptypes(tree type1,tree type2)1034 comptypes (tree type1, tree type2)
1035 {
1036   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1037   int val;
1038 
1039   val = comptypes_internal (type1, type2, NULL, NULL);
1040   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1041 
1042   return val;
1043 }
1044 
1045 /* Like comptypes, but if it returns non-zero because enum and int are
1046    compatible, it sets *ENUM_AND_INT_P to true.  */
1047 
1048 static int
comptypes_check_enum_int(tree type1,tree type2,bool * enum_and_int_p)1049 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1050 {
1051   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1052   int val;
1053 
1054   val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1055   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1056 
1057   return val;
1058 }
1059 
1060 /* Like comptypes, but if it returns nonzero for different types, it
1061    sets *DIFFERENT_TYPES_P to true.  */
1062 
1063 int
comptypes_check_different_types(tree type1,tree type2,bool * different_types_p)1064 comptypes_check_different_types (tree type1, tree type2,
1065 				 bool *different_types_p)
1066 {
1067   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1068   int val;
1069 
1070   val = comptypes_internal (type1, type2, NULL, different_types_p);
1071   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1072 
1073   return val;
1074 }
1075 
1076 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1077    or various other operations.  Return 2 if they are compatible
1078    but a warning may be needed if you use them together.  If
1079    ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1080    compatible integer type, then this sets *ENUM_AND_INT_P to true;
1081    *ENUM_AND_INT_P is never set to false.  If DIFFERENT_TYPES_P is not
1082    NULL, and the types are compatible but different enough not to be
1083    permitted in C11 typedef redeclarations, then this sets
1084    *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1085    false, but may or may not be set if the types are incompatible.
1086    This differs from comptypes, in that we don't free the seen
1087    types.  */
1088 
1089 static int
comptypes_internal(const_tree type1,const_tree type2,bool * enum_and_int_p,bool * different_types_p)1090 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1091 		    bool *different_types_p)
1092 {
1093   const_tree t1 = type1;
1094   const_tree t2 = type2;
1095   int attrval, val;
1096 
1097   /* Suppress errors caused by previously reported errors.  */
1098 
1099   if (t1 == t2 || !t1 || !t2
1100       || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1101     return 1;
1102 
1103   /* Enumerated types are compatible with integer types, but this is
1104      not transitive: two enumerated types in the same translation unit
1105      are compatible with each other only if they are the same type.  */
1106 
1107   if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1108     {
1109       t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1110       if (TREE_CODE (t2) != VOID_TYPE)
1111 	{
1112 	  if (enum_and_int_p != NULL)
1113 	    *enum_and_int_p = true;
1114 	  if (different_types_p != NULL)
1115 	    *different_types_p = true;
1116 	}
1117     }
1118   else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1119     {
1120       t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1121       if (TREE_CODE (t1) != VOID_TYPE)
1122 	{
1123 	  if (enum_and_int_p != NULL)
1124 	    *enum_and_int_p = true;
1125 	  if (different_types_p != NULL)
1126 	    *different_types_p = true;
1127 	}
1128     }
1129 
1130   if (t1 == t2)
1131     return 1;
1132 
1133   /* Different classes of types can't be compatible.  */
1134 
1135   if (TREE_CODE (t1) != TREE_CODE (t2))
1136     return 0;
1137 
1138   /* Qualifiers must match. C99 6.7.3p9 */
1139 
1140   if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1141     return 0;
1142 
1143   /* Allow for two different type nodes which have essentially the same
1144      definition.  Note that we already checked for equality of the type
1145      qualifiers (just above).  */
1146 
1147   if (TREE_CODE (t1) != ARRAY_TYPE
1148       && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1149     return 1;
1150 
1151   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1152   if (!(attrval = comp_type_attributes (t1, t2)))
1153      return 0;
1154 
1155   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1156   val = 0;
1157 
1158   switch (TREE_CODE (t1))
1159     {
1160     case INTEGER_TYPE:
1161     case FIXED_POINT_TYPE:
1162     case REAL_TYPE:
1163       /* With these nodes, we can't determine type equivalence by
1164 	 looking at what is stored in the nodes themselves, because
1165 	 two nodes might have different TYPE_MAIN_VARIANTs but still
1166 	 represent the same type.  For example, wchar_t and int could
1167 	 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1168 	 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1169 	 and are distinct types.  On the other hand, int and the
1170 	 following typedef
1171 
1172 	   typedef int INT __attribute((may_alias));
1173 
1174 	 have identical properties, different TYPE_MAIN_VARIANTs, but
1175 	 represent the same type.  The canonical type system keeps
1176 	 track of equivalence in this case, so we fall back on it.  */
1177       return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1178 
1179     case POINTER_TYPE:
1180       /* Do not remove mode information.  */
1181       if (TYPE_MODE (t1) != TYPE_MODE (t2))
1182 	break;
1183       val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1184 	     ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1185 				       enum_and_int_p, different_types_p));
1186       break;
1187 
1188     case FUNCTION_TYPE:
1189       val = function_types_compatible_p (t1, t2, enum_and_int_p,
1190 					 different_types_p);
1191       break;
1192 
1193     case ARRAY_TYPE:
1194       {
1195 	tree d1 = TYPE_DOMAIN (t1);
1196 	tree d2 = TYPE_DOMAIN (t2);
1197 	bool d1_variable, d2_variable;
1198 	bool d1_zero, d2_zero;
1199 	val = 1;
1200 
1201 	/* Target types must match incl. qualifiers.  */
1202 	if (TREE_TYPE (t1) != TREE_TYPE (t2)
1203 	    && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1204 					  enum_and_int_p,
1205 					  different_types_p)) == 0)
1206 	  return 0;
1207 
1208 	if (different_types_p != NULL
1209 	    && (d1 == NULL_TREE) != (d2 == NULL_TREE))
1210 	  *different_types_p = true;
1211 	/* Sizes must match unless one is missing or variable.  */
1212 	if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
1213 	  break;
1214 
1215 	d1_zero = !TYPE_MAX_VALUE (d1);
1216 	d2_zero = !TYPE_MAX_VALUE (d2);
1217 
1218 	d1_variable = (!d1_zero
1219 		       && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1220 			   || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1221 	d2_variable = (!d2_zero
1222 		       && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1223 			   || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1224 	d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1225 	d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1226 
1227 	if (different_types_p != NULL
1228 	    && d1_variable != d2_variable)
1229 	  *different_types_p = true;
1230 	if (d1_variable || d2_variable)
1231 	  break;
1232 	if (d1_zero && d2_zero)
1233 	  break;
1234 	if (d1_zero || d2_zero
1235 	    || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1236 	    || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1237 	  val = 0;
1238 
1239 	break;
1240       }
1241 
1242     case ENUMERAL_TYPE:
1243     case RECORD_TYPE:
1244     case UNION_TYPE:
1245       if (val != 1 && !same_translation_unit_p (t1, t2))
1246 	{
1247 	  tree a1 = TYPE_ATTRIBUTES (t1);
1248 	  tree a2 = TYPE_ATTRIBUTES (t2);
1249 
1250 	  if (! attribute_list_contained (a1, a2)
1251 	      && ! attribute_list_contained (a2, a1))
1252 	    break;
1253 
1254 	  if (attrval != 2)
1255 	    return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1256 						 different_types_p);
1257 	  val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1258 					      different_types_p);
1259 	}
1260       break;
1261 
1262     case VECTOR_TYPE:
1263       val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1264 	     && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1265 				    enum_and_int_p, different_types_p));
1266       break;
1267 
1268     default:
1269       break;
1270     }
1271   return attrval == 2 && val == 1 ? 2 : val;
1272 }
1273 
1274 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1275    their qualifiers, except for named address spaces.  If the pointers point to
1276    different named addresses, then we must determine if one address space is a
1277    subset of the other.  */
1278 
1279 static int
comp_target_types(location_t location,tree ttl,tree ttr)1280 comp_target_types (location_t location, tree ttl, tree ttr)
1281 {
1282   int val;
1283   int val_ped;
1284   tree mvl = TREE_TYPE (ttl);
1285   tree mvr = TREE_TYPE (ttr);
1286   addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1287   addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1288   addr_space_t as_common;
1289   bool enum_and_int_p;
1290 
1291   /* Fail if pointers point to incompatible address spaces.  */
1292   if (!addr_space_superset (asl, asr, &as_common))
1293     return 0;
1294 
1295   /* For pedantic record result of comptypes on arrays before losing
1296      qualifiers on the element type below. */
1297   val_ped = 1;
1298 
1299   if (TREE_CODE (mvl) == ARRAY_TYPE
1300       && TREE_CODE (mvr) == ARRAY_TYPE)
1301     val_ped = comptypes (mvl, mvr);
1302 
1303   /* Qualifiers on element types of array types that are
1304      pointer targets are lost by taking their TYPE_MAIN_VARIANT.  */
1305 
1306   mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1307 	 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1308 	 : TYPE_MAIN_VARIANT (mvl));
1309 
1310   mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1311 	 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1312 	 : TYPE_MAIN_VARIANT (mvr));
1313 
1314   enum_and_int_p = false;
1315   val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1316 
1317   if (val == 1 && val_ped != 1)
1318     pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1319                                       "are incompatible in ISO C");
1320 
1321   if (val == 2)
1322     pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1323 
1324   if (val == 1 && enum_and_int_p && warn_cxx_compat)
1325     warning_at (location, OPT_Wc___compat,
1326 		"pointer target types incompatible in C++");
1327 
1328   return val;
1329 }
1330 
1331 /* Subroutines of `comptypes'.  */
1332 
1333 /* Determine whether two trees derive from the same translation unit.
1334    If the CONTEXT chain ends in a null, that tree's context is still
1335    being parsed, so if two trees have context chains ending in null,
1336    they're in the same translation unit.  */
1337 
1338 bool
same_translation_unit_p(const_tree t1,const_tree t2)1339 same_translation_unit_p (const_tree t1, const_tree t2)
1340 {
1341   while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1342     switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1343       {
1344       case tcc_declaration:
1345 	t1 = DECL_CONTEXT (t1); break;
1346       case tcc_type:
1347 	t1 = TYPE_CONTEXT (t1); break;
1348       case tcc_exceptional:
1349 	t1 = BLOCK_SUPERCONTEXT (t1); break;  /* assume block */
1350       default: gcc_unreachable ();
1351       }
1352 
1353   while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1354     switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1355       {
1356       case tcc_declaration:
1357 	t2 = DECL_CONTEXT (t2); break;
1358       case tcc_type:
1359 	t2 = TYPE_CONTEXT (t2); break;
1360       case tcc_exceptional:
1361 	t2 = BLOCK_SUPERCONTEXT (t2); break;  /* assume block */
1362       default: gcc_unreachable ();
1363       }
1364 
1365   return t1 == t2;
1366 }
1367 
1368 /* Allocate the seen two types, assuming that they are compatible. */
1369 
1370 static struct tagged_tu_seen_cache *
alloc_tagged_tu_seen_cache(const_tree t1,const_tree t2)1371 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1372 {
1373   struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1374   tu->next = tagged_tu_seen_base;
1375   tu->t1 = t1;
1376   tu->t2 = t2;
1377 
1378   tagged_tu_seen_base = tu;
1379 
1380   /* The C standard says that two structures in different translation
1381      units are compatible with each other only if the types of their
1382      fields are compatible (among other things).  We assume that they
1383      are compatible until proven otherwise when building the cache.
1384      An example where this can occur is:
1385      struct a
1386      {
1387        struct a *next;
1388      };
1389      If we are comparing this against a similar struct in another TU,
1390      and did not assume they were compatible, we end up with an infinite
1391      loop.  */
1392   tu->val = 1;
1393   return tu;
1394 }
1395 
1396 /* Free the seen types until we get to TU_TIL. */
1397 
1398 static void
free_all_tagged_tu_seen_up_to(const struct tagged_tu_seen_cache * tu_til)1399 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1400 {
1401   const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1402   while (tu != tu_til)
1403     {
1404       const struct tagged_tu_seen_cache *const tu1
1405 	= (const struct tagged_tu_seen_cache *) tu;
1406       tu = tu1->next;
1407       free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1408     }
1409   tagged_tu_seen_base = tu_til;
1410 }
1411 
1412 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1413    compatible.  If the two types are not the same (which has been
1414    checked earlier), this can only happen when multiple translation
1415    units are being compiled.  See C99 6.2.7 paragraph 1 for the exact
1416    rules.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1417    comptypes_internal.  */
1418 
1419 static int
tagged_types_tu_compatible_p(const_tree t1,const_tree t2,bool * enum_and_int_p,bool * different_types_p)1420 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1421 			      bool *enum_and_int_p, bool *different_types_p)
1422 {
1423   tree s1, s2;
1424   bool needs_warning = false;
1425 
1426   /* We have to verify that the tags of the types are the same.  This
1427      is harder than it looks because this may be a typedef, so we have
1428      to go look at the original type.  It may even be a typedef of a
1429      typedef...
1430      In the case of compiler-created builtin structs the TYPE_DECL
1431      may be a dummy, with no DECL_ORIGINAL_TYPE.  Don't fault.  */
1432   while (TYPE_NAME (t1)
1433 	 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1434 	 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1435     t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1436 
1437   while (TYPE_NAME (t2)
1438 	 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1439 	 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1440     t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1441 
1442   /* C90 didn't have the requirement that the two tags be the same.  */
1443   if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1444     return 0;
1445 
1446   /* C90 didn't say what happened if one or both of the types were
1447      incomplete; we choose to follow C99 rules here, which is that they
1448      are compatible.  */
1449   if (TYPE_SIZE (t1) == NULL
1450       || TYPE_SIZE (t2) == NULL)
1451     return 1;
1452 
1453   {
1454     const struct tagged_tu_seen_cache * tts_i;
1455     for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1456       if (tts_i->t1 == t1 && tts_i->t2 == t2)
1457 	return tts_i->val;
1458   }
1459 
1460   switch (TREE_CODE (t1))
1461     {
1462     case ENUMERAL_TYPE:
1463       {
1464 	struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1465 	/* Speed up the case where the type values are in the same order.  */
1466 	tree tv1 = TYPE_VALUES (t1);
1467 	tree tv2 = TYPE_VALUES (t2);
1468 
1469 	if (tv1 == tv2)
1470 	  {
1471 	    return 1;
1472 	  }
1473 
1474 	for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1475 	  {
1476 	    if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1477 	      break;
1478 	    if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1479 	      {
1480 		tu->val = 0;
1481 		return 0;
1482 	      }
1483 	  }
1484 
1485 	if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1486 	  {
1487 	    return 1;
1488 	  }
1489 	if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1490 	  {
1491 	    tu->val = 0;
1492 	    return 0;
1493 	  }
1494 
1495 	if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1496 	  {
1497 	    tu->val = 0;
1498 	    return 0;
1499 	  }
1500 
1501 	for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1502 	  {
1503 	    s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1504 	    if (s2 == NULL
1505 		|| simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1506 	      {
1507 		tu->val = 0;
1508 		return 0;
1509 	      }
1510 	  }
1511 	return 1;
1512       }
1513 
1514     case UNION_TYPE:
1515       {
1516 	struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1517 	if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1518 	  {
1519 	    tu->val = 0;
1520 	    return 0;
1521 	  }
1522 
1523 	/*  Speed up the common case where the fields are in the same order. */
1524 	for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1525 	     s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1526 	  {
1527 	    int result;
1528 
1529 	    if (DECL_NAME (s1) != DECL_NAME (s2))
1530 	      break;
1531 	    result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1532 					 enum_and_int_p, different_types_p);
1533 
1534 	    if (result != 1 && !DECL_NAME (s1))
1535 	      break;
1536 	    if (result == 0)
1537 	      {
1538 		tu->val = 0;
1539 		return 0;
1540 	      }
1541 	    if (result == 2)
1542 	      needs_warning = true;
1543 
1544 	    if (TREE_CODE (s1) == FIELD_DECL
1545 		&& simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1546 				     DECL_FIELD_BIT_OFFSET (s2)) != 1)
1547 	      {
1548 		tu->val = 0;
1549 		return 0;
1550 	      }
1551 	  }
1552 	if (!s1 && !s2)
1553 	  {
1554 	    tu->val = needs_warning ? 2 : 1;
1555 	    return tu->val;
1556 	  }
1557 
1558 	for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1559 	  {
1560 	    bool ok = false;
1561 
1562 	    for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1563 	      if (DECL_NAME (s1) == DECL_NAME (s2))
1564 		{
1565 		  int result;
1566 
1567 		  result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1568 					       enum_and_int_p,
1569 					       different_types_p);
1570 
1571 		  if (result != 1 && !DECL_NAME (s1))
1572 		    continue;
1573 		  if (result == 0)
1574 		    {
1575 		      tu->val = 0;
1576 		      return 0;
1577 		    }
1578 		  if (result == 2)
1579 		    needs_warning = true;
1580 
1581 		  if (TREE_CODE (s1) == FIELD_DECL
1582 		      && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1583 					   DECL_FIELD_BIT_OFFSET (s2)) != 1)
1584 		    break;
1585 
1586 		  ok = true;
1587 		  break;
1588 		}
1589 	    if (!ok)
1590 	      {
1591 		tu->val = 0;
1592 		return 0;
1593 	      }
1594 	  }
1595 	tu->val = needs_warning ? 2 : 10;
1596 	return tu->val;
1597       }
1598 
1599     case RECORD_TYPE:
1600       {
1601 	struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1602 
1603 	for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1604 	     s1 && s2;
1605 	     s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1606 	  {
1607 	    int result;
1608 	    if (TREE_CODE (s1) != TREE_CODE (s2)
1609 		|| DECL_NAME (s1) != DECL_NAME (s2))
1610 	      break;
1611 	    result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1612 					 enum_and_int_p, different_types_p);
1613 	    if (result == 0)
1614 	      break;
1615 	    if (result == 2)
1616 	      needs_warning = true;
1617 
1618 	    if (TREE_CODE (s1) == FIELD_DECL
1619 		&& simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1620 				     DECL_FIELD_BIT_OFFSET (s2)) != 1)
1621 	      break;
1622 	  }
1623 	if (s1 && s2)
1624 	  tu->val = 0;
1625 	else
1626 	  tu->val = needs_warning ? 2 : 1;
1627 	return tu->val;
1628       }
1629 
1630     default:
1631       gcc_unreachable ();
1632     }
1633 }
1634 
1635 /* Return 1 if two function types F1 and F2 are compatible.
1636    If either type specifies no argument types,
1637    the other must specify a fixed number of self-promoting arg types.
1638    Otherwise, if one type specifies only the number of arguments,
1639    the other must specify that number of self-promoting arg types.
1640    Otherwise, the argument types must match.
1641    ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal.  */
1642 
1643 static int
function_types_compatible_p(const_tree f1,const_tree f2,bool * enum_and_int_p,bool * different_types_p)1644 function_types_compatible_p (const_tree f1, const_tree f2,
1645 			     bool *enum_and_int_p, bool *different_types_p)
1646 {
1647   tree args1, args2;
1648   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1649   int val = 1;
1650   int val1;
1651   tree ret1, ret2;
1652 
1653   ret1 = TREE_TYPE (f1);
1654   ret2 = TREE_TYPE (f2);
1655 
1656   /* 'volatile' qualifiers on a function's return type used to mean
1657      the function is noreturn.  */
1658   if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1659     pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1660   if (TYPE_VOLATILE (ret1))
1661     ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1662 				 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1663   if (TYPE_VOLATILE (ret2))
1664     ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1665 				 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1666   val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1667   if (val == 0)
1668     return 0;
1669 
1670   args1 = TYPE_ARG_TYPES (f1);
1671   args2 = TYPE_ARG_TYPES (f2);
1672 
1673   if (different_types_p != NULL
1674       && (args1 == NULL_TREE) != (args2 == NULL_TREE))
1675     *different_types_p = true;
1676 
1677   /* An unspecified parmlist matches any specified parmlist
1678      whose argument types don't need default promotions.  */
1679 
1680   if (args1 == NULL_TREE)
1681     {
1682       if (!self_promoting_args_p (args2))
1683 	return 0;
1684       /* If one of these types comes from a non-prototype fn definition,
1685 	 compare that with the other type's arglist.
1686 	 If they don't match, ask for a warning (but no error).  */
1687       if (TYPE_ACTUAL_ARG_TYPES (f1)
1688 	  && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1689 				      enum_and_int_p, different_types_p) != 1)
1690 	val = 2;
1691       return val;
1692     }
1693   if (args2 == NULL_TREE)
1694     {
1695       if (!self_promoting_args_p (args1))
1696 	return 0;
1697       if (TYPE_ACTUAL_ARG_TYPES (f2)
1698 	  && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1699 				      enum_and_int_p, different_types_p) != 1)
1700 	val = 2;
1701       return val;
1702     }
1703 
1704   /* Both types have argument lists: compare them and propagate results.  */
1705   val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1706 				  different_types_p);
1707   return val1 != 1 ? val1 : val;
1708 }
1709 
1710 /* Check two lists of types for compatibility, returning 0 for
1711    incompatible, 1 for compatible, or 2 for compatible with
1712    warning.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1713    comptypes_internal.  */
1714 
1715 static int
type_lists_compatible_p(const_tree args1,const_tree args2,bool * enum_and_int_p,bool * different_types_p)1716 type_lists_compatible_p (const_tree args1, const_tree args2,
1717 			 bool *enum_and_int_p, bool *different_types_p)
1718 {
1719   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1720   int val = 1;
1721   int newval = 0;
1722 
1723   while (1)
1724     {
1725       tree a1, mv1, a2, mv2;
1726       if (args1 == NULL_TREE && args2 == NULL_TREE)
1727 	return val;
1728       /* If one list is shorter than the other,
1729 	 they fail to match.  */
1730       if (args1 == NULL_TREE || args2 == NULL_TREE)
1731 	return 0;
1732       mv1 = a1 = TREE_VALUE (args1);
1733       mv2 = a2 = TREE_VALUE (args2);
1734       if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1735 	mv1 = (TYPE_ATOMIC (mv1)
1736 	       ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1737 					 TYPE_QUAL_ATOMIC)
1738 	       : TYPE_MAIN_VARIANT (mv1));
1739       if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1740 	mv2 = (TYPE_ATOMIC (mv2)
1741 	       ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1742 					 TYPE_QUAL_ATOMIC)
1743 	       : TYPE_MAIN_VARIANT (mv2));
1744       /* A null pointer instead of a type
1745 	 means there is supposed to be an argument
1746 	 but nothing is specified about what type it has.
1747 	 So match anything that self-promotes.  */
1748       if (different_types_p != NULL
1749 	  && (a1 == NULL_TREE) != (a2 == NULL_TREE))
1750 	*different_types_p = true;
1751       if (a1 == NULL_TREE)
1752 	{
1753 	  if (c_type_promotes_to (a2) != a2)
1754 	    return 0;
1755 	}
1756       else if (a2 == NULL_TREE)
1757 	{
1758 	  if (c_type_promotes_to (a1) != a1)
1759 	    return 0;
1760 	}
1761       /* If one of the lists has an error marker, ignore this arg.  */
1762       else if (TREE_CODE (a1) == ERROR_MARK
1763 	       || TREE_CODE (a2) == ERROR_MARK)
1764 	;
1765       else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1766 					      different_types_p)))
1767 	{
1768 	  if (different_types_p != NULL)
1769 	    *different_types_p = true;
1770 	  /* Allow  wait (union {union wait *u; int *i} *)
1771 	     and  wait (union wait *)  to be compatible.  */
1772 	  if (TREE_CODE (a1) == UNION_TYPE
1773 	      && (TYPE_NAME (a1) == NULL_TREE
1774 		  || TYPE_TRANSPARENT_AGGR (a1))
1775 	      && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1776 	      && tree_int_cst_equal (TYPE_SIZE (a1),
1777 				     TYPE_SIZE (a2)))
1778 	    {
1779 	      tree memb;
1780 	      for (memb = TYPE_FIELDS (a1);
1781 		   memb; memb = DECL_CHAIN (memb))
1782 		{
1783 		  tree mv3 = TREE_TYPE (memb);
1784 		  if (mv3 && mv3 != error_mark_node
1785 		      && TREE_CODE (mv3) != ARRAY_TYPE)
1786 		    mv3 = (TYPE_ATOMIC (mv3)
1787 			   ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1788 						     TYPE_QUAL_ATOMIC)
1789 			   : TYPE_MAIN_VARIANT (mv3));
1790 		  if (comptypes_internal (mv3, mv2, enum_and_int_p,
1791 					  different_types_p))
1792 		    break;
1793 		}
1794 	      if (memb == NULL_TREE)
1795 		return 0;
1796 	    }
1797 	  else if (TREE_CODE (a2) == UNION_TYPE
1798 		   && (TYPE_NAME (a2) == NULL_TREE
1799 		       || TYPE_TRANSPARENT_AGGR (a2))
1800 		   && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1801 		   && tree_int_cst_equal (TYPE_SIZE (a2),
1802 					  TYPE_SIZE (a1)))
1803 	    {
1804 	      tree memb;
1805 	      for (memb = TYPE_FIELDS (a2);
1806 		   memb; memb = DECL_CHAIN (memb))
1807 		{
1808 		  tree mv3 = TREE_TYPE (memb);
1809 		  if (mv3 && mv3 != error_mark_node
1810 		      && TREE_CODE (mv3) != ARRAY_TYPE)
1811 		    mv3 = (TYPE_ATOMIC (mv3)
1812 			   ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1813 						     TYPE_QUAL_ATOMIC)
1814 			   : TYPE_MAIN_VARIANT (mv3));
1815 		  if (comptypes_internal (mv3, mv1, enum_and_int_p,
1816 					  different_types_p))
1817 		    break;
1818 		}
1819 	      if (memb == NULL_TREE)
1820 		return 0;
1821 	    }
1822 	  else
1823 	    return 0;
1824 	}
1825 
1826       /* comptypes said ok, but record if it said to warn.  */
1827       if (newval > val)
1828 	val = newval;
1829 
1830       args1 = TREE_CHAIN (args1);
1831       args2 = TREE_CHAIN (args2);
1832     }
1833 }
1834 
1835 /* Compute the size to increment a pointer by.  When a function type or void
1836    type or incomplete type is passed, size_one_node is returned.
1837    This function does not emit any diagnostics; the caller is responsible
1838    for that.  */
1839 
1840 static tree
c_size_in_bytes(const_tree type)1841 c_size_in_bytes (const_tree type)
1842 {
1843   enum tree_code code = TREE_CODE (type);
1844 
1845   if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1846       || !COMPLETE_TYPE_P (type))
1847     return size_one_node;
1848 
1849   /* Convert in case a char is more than one unit.  */
1850   return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1851 			 size_int (TYPE_PRECISION (char_type_node)
1852 				   / BITS_PER_UNIT));
1853 }
1854 
1855 /* Return either DECL or its known constant value (if it has one).  */
1856 
1857 tree
decl_constant_value_1(tree decl,bool in_init)1858 decl_constant_value_1 (tree decl, bool in_init)
1859 {
1860   if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL.  */
1861       TREE_CODE (decl) != PARM_DECL
1862       && !TREE_THIS_VOLATILE (decl)
1863       && TREE_READONLY (decl)
1864       && DECL_INITIAL (decl) != NULL_TREE
1865       && !error_operand_p (DECL_INITIAL (decl))
1866       /* This is invalid if initial value is not constant.
1867 	 If it has either a function call, a memory reference,
1868 	 or a variable, then re-evaluating it could give different results.  */
1869       && TREE_CONSTANT (DECL_INITIAL (decl))
1870       /* Check for cases where this is sub-optimal, even though valid.  */
1871       && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
1872     return DECL_INITIAL (decl);
1873   return decl;
1874 }
1875 
1876 /* Return either DECL or its known constant value (if it has one).
1877    Like the above, but always return decl outside of functions.  */
1878 
1879 tree
decl_constant_value(tree decl)1880 decl_constant_value (tree decl)
1881 {
1882   /* Don't change a variable array bound or initial value to a constant
1883      in a place where a variable is invalid.  */
1884   return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
1885 }
1886 
1887 /* Convert the array expression EXP to a pointer.  */
1888 static tree
array_to_pointer_conversion(location_t loc,tree exp)1889 array_to_pointer_conversion (location_t loc, tree exp)
1890 {
1891   tree orig_exp = exp;
1892   tree type = TREE_TYPE (exp);
1893   tree adr;
1894   tree restype = TREE_TYPE (type);
1895   tree ptrtype;
1896 
1897   gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1898 
1899   STRIP_TYPE_NOPS (exp);
1900 
1901   if (TREE_NO_WARNING (orig_exp))
1902     TREE_NO_WARNING (exp) = 1;
1903 
1904   ptrtype = build_pointer_type (restype);
1905 
1906   if (INDIRECT_REF_P (exp))
1907     return convert (ptrtype, TREE_OPERAND (exp, 0));
1908 
1909   /* In C++ array compound literals are temporary objects unless they are
1910      const or appear in namespace scope, so they are destroyed too soon
1911      to use them for much of anything  (c++/53220).  */
1912   if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1913     {
1914       tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1915       if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1916 	warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1917 		    "converting an array compound literal to a pointer "
1918 		    "is ill-formed in C++");
1919     }
1920 
1921   adr = build_unary_op (loc, ADDR_EXPR, exp, true);
1922   return convert (ptrtype, adr);
1923 }
1924 
1925 /* Convert the function expression EXP to a pointer.  */
1926 static tree
function_to_pointer_conversion(location_t loc,tree exp)1927 function_to_pointer_conversion (location_t loc, tree exp)
1928 {
1929   tree orig_exp = exp;
1930 
1931   gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1932 
1933   STRIP_TYPE_NOPS (exp);
1934 
1935   if (TREE_NO_WARNING (orig_exp))
1936     TREE_NO_WARNING (exp) = 1;
1937 
1938   return build_unary_op (loc, ADDR_EXPR, exp, false);
1939 }
1940 
1941 /* Mark EXP as read, not just set, for set but not used -Wunused
1942    warning purposes.  */
1943 
1944 void
mark_exp_read(tree exp)1945 mark_exp_read (tree exp)
1946 {
1947   switch (TREE_CODE (exp))
1948     {
1949     case VAR_DECL:
1950     case PARM_DECL:
1951       DECL_READ_P (exp) = 1;
1952       break;
1953     case ARRAY_REF:
1954     case COMPONENT_REF:
1955     case MODIFY_EXPR:
1956     case REALPART_EXPR:
1957     case IMAGPART_EXPR:
1958     CASE_CONVERT:
1959     case ADDR_EXPR:
1960     case VIEW_CONVERT_EXPR:
1961       mark_exp_read (TREE_OPERAND (exp, 0));
1962       break;
1963     case COMPOUND_EXPR:
1964       /* Pattern match what build_atomic_assign produces with modifycode
1965 	 NOP_EXPR.  */
1966       if (VAR_P (TREE_OPERAND (exp, 1))
1967 	  && DECL_ARTIFICIAL (TREE_OPERAND (exp, 1))
1968 	  && TREE_CODE (TREE_OPERAND (exp, 0)) == COMPOUND_EXPR)
1969 	{
1970 	  tree t1 = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1971 	  tree t2 = TREE_OPERAND (TREE_OPERAND (exp, 0), 1);
1972 	  if (TREE_CODE (t1) == TARGET_EXPR
1973 	      && TARGET_EXPR_SLOT (t1) == TREE_OPERAND (exp, 1)
1974 	      && TREE_CODE (t2) == CALL_EXPR)
1975 	    {
1976 	      tree fndecl = get_callee_fndecl (t2);
1977 	      tree arg = NULL_TREE;
1978 	      if (fndecl
1979 		  && TREE_CODE (fndecl) == FUNCTION_DECL
1980 		  && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1981 		  && call_expr_nargs (t2) >= 2)
1982 		switch (DECL_FUNCTION_CODE (fndecl))
1983 		  {
1984 		  case BUILT_IN_ATOMIC_STORE:
1985 		    arg = CALL_EXPR_ARG (t2, 1);
1986 		    break;
1987 		  case BUILT_IN_ATOMIC_STORE_1:
1988 		  case BUILT_IN_ATOMIC_STORE_2:
1989 		  case BUILT_IN_ATOMIC_STORE_4:
1990 		  case BUILT_IN_ATOMIC_STORE_8:
1991 		  case BUILT_IN_ATOMIC_STORE_16:
1992 		    arg = CALL_EXPR_ARG (t2, 0);
1993 		    break;
1994 		  default:
1995 		    break;
1996 		  }
1997 	      if (arg)
1998 		{
1999 		  STRIP_NOPS (arg);
2000 		  if (TREE_CODE (arg) == ADDR_EXPR
2001 		      && DECL_P (TREE_OPERAND (arg, 0))
2002 		      && TYPE_ATOMIC (TREE_TYPE (TREE_OPERAND (arg, 0))))
2003 		    mark_exp_read (TREE_OPERAND (arg, 0));
2004 		}
2005 	    }
2006 	}
2007       /* FALLTHRU */
2008     case C_MAYBE_CONST_EXPR:
2009       mark_exp_read (TREE_OPERAND (exp, 1));
2010       break;
2011     default:
2012       break;
2013     }
2014 }
2015 
2016 /* Perform the default conversion of arrays and functions to pointers.
2017    Return the result of converting EXP.  For any other expression, just
2018    return EXP.
2019 
2020    LOC is the location of the expression.  */
2021 
2022 struct c_expr
default_function_array_conversion(location_t loc,struct c_expr exp)2023 default_function_array_conversion (location_t loc, struct c_expr exp)
2024 {
2025   tree orig_exp = exp.value;
2026   tree type = TREE_TYPE (exp.value);
2027   enum tree_code code = TREE_CODE (type);
2028 
2029   switch (code)
2030     {
2031     case ARRAY_TYPE:
2032       {
2033 	bool not_lvalue = false;
2034 	bool lvalue_array_p;
2035 
2036 	while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
2037 		|| CONVERT_EXPR_P (exp.value))
2038 	       && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
2039 	  {
2040 	    if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
2041 	      not_lvalue = true;
2042 	    exp.value = TREE_OPERAND (exp.value, 0);
2043 	  }
2044 
2045 	if (TREE_NO_WARNING (orig_exp))
2046 	  TREE_NO_WARNING (exp.value) = 1;
2047 
2048 	lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
2049 	if (!flag_isoc99 && !lvalue_array_p)
2050 	  {
2051 	    /* Before C99, non-lvalue arrays do not decay to pointers.
2052 	       Normally, using such an array would be invalid; but it can
2053 	       be used correctly inside sizeof or as a statement expression.
2054 	       Thus, do not give an error here; an error will result later.  */
2055 	    return exp;
2056 	  }
2057 
2058 	exp.value = array_to_pointer_conversion (loc, exp.value);
2059       }
2060       break;
2061     case FUNCTION_TYPE:
2062       exp.value = function_to_pointer_conversion (loc, exp.value);
2063       break;
2064     default:
2065       break;
2066     }
2067 
2068   return exp;
2069 }
2070 
2071 struct c_expr
default_function_array_read_conversion(location_t loc,struct c_expr exp)2072 default_function_array_read_conversion (location_t loc, struct c_expr exp)
2073 {
2074   mark_exp_read (exp.value);
2075   return default_function_array_conversion (loc, exp);
2076 }
2077 
2078 /* Return whether EXPR should be treated as an atomic lvalue for the
2079    purposes of load and store handling.  */
2080 
2081 static bool
really_atomic_lvalue(tree expr)2082 really_atomic_lvalue (tree expr)
2083 {
2084   if (error_operand_p (expr))
2085     return false;
2086   if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2087     return false;
2088   if (!lvalue_p (expr))
2089     return false;
2090 
2091   /* Ignore _Atomic on register variables, since their addresses can't
2092      be taken so (a) atomicity is irrelevant and (b) the normal atomic
2093      sequences wouldn't work.  Ignore _Atomic on structures containing
2094      bit-fields, since accessing elements of atomic structures or
2095      unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2096      it's undefined at translation time or execution time, and the
2097      normal atomic sequences again wouldn't work.  */
2098   while (handled_component_p (expr))
2099     {
2100       if (TREE_CODE (expr) == COMPONENT_REF
2101 	  && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2102 	return false;
2103       expr = TREE_OPERAND (expr, 0);
2104     }
2105   if (DECL_P (expr) && C_DECL_REGISTER (expr))
2106     return false;
2107   return true;
2108 }
2109 
2110 /* Convert expression EXP (location LOC) from lvalue to rvalue,
2111    including converting functions and arrays to pointers if CONVERT_P.
2112    If READ_P, also mark the expression as having been read.  */
2113 
2114 struct c_expr
convert_lvalue_to_rvalue(location_t loc,struct c_expr exp,bool convert_p,bool read_p)2115 convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2116 			  bool convert_p, bool read_p)
2117 {
2118   if (read_p)
2119     mark_exp_read (exp.value);
2120   if (convert_p)
2121     exp = default_function_array_conversion (loc, exp);
2122   if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
2123     exp.value = require_complete_type (loc, exp.value);
2124   if (really_atomic_lvalue (exp.value))
2125     {
2126       vec<tree, va_gc> *params;
2127       tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2128       tree expr_type = TREE_TYPE (exp.value);
2129       tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
2130       tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2131 
2132       gcc_assert (TYPE_ATOMIC (expr_type));
2133 
2134       /* Expansion of a generic atomic load may require an addition
2135 	 element, so allocate enough to prevent a resize.  */
2136       vec_alloc (params, 4);
2137 
2138       /* Remove the qualifiers for the rest of the expressions and
2139 	 create the VAL temp variable to hold the RHS.  */
2140       nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2141       tmp = create_tmp_var_raw (nonatomic_type);
2142       tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
2143       TREE_ADDRESSABLE (tmp) = 1;
2144       TREE_NO_WARNING (tmp) = 1;
2145 
2146       /* Issue __atomic_load (&expr, &tmp, SEQ_CST);  */
2147       fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2148       params->quick_push (expr_addr);
2149       params->quick_push (tmp_addr);
2150       params->quick_push (seq_cst);
2151       func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
2152 
2153       /* EXPR is always read.  */
2154       mark_exp_read (exp.value);
2155 
2156       /* Return tmp which contains the value loaded.  */
2157       exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2158 			  NULL_TREE, NULL_TREE);
2159     }
2160   return exp;
2161 }
2162 
2163 /* EXP is an expression of integer type.  Apply the integer promotions
2164    to it and return the promoted value.  */
2165 
2166 tree
perform_integral_promotions(tree exp)2167 perform_integral_promotions (tree exp)
2168 {
2169   tree type = TREE_TYPE (exp);
2170   enum tree_code code = TREE_CODE (type);
2171 
2172   gcc_assert (INTEGRAL_TYPE_P (type));
2173 
2174   /* Normally convert enums to int,
2175      but convert wide enums to something wider.  */
2176   if (code == ENUMERAL_TYPE)
2177     {
2178       type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2179 					  TYPE_PRECISION (integer_type_node)),
2180 				     ((TYPE_PRECISION (type)
2181 				       >= TYPE_PRECISION (integer_type_node))
2182 				      && TYPE_UNSIGNED (type)));
2183 
2184       return convert (type, exp);
2185     }
2186 
2187   /* ??? This should no longer be needed now bit-fields have their
2188      proper types.  */
2189   if (TREE_CODE (exp) == COMPONENT_REF
2190       && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
2191       /* If it's thinner than an int, promote it like a
2192 	 c_promoting_integer_type_p, otherwise leave it alone.  */
2193       && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2194 			   TYPE_PRECISION (integer_type_node)) < 0)
2195     return convert (integer_type_node, exp);
2196 
2197   if (c_promoting_integer_type_p (type))
2198     {
2199       /* Preserve unsignedness if not really getting any wider.  */
2200       if (TYPE_UNSIGNED (type)
2201 	  && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2202 	return convert (unsigned_type_node, exp);
2203 
2204       return convert (integer_type_node, exp);
2205     }
2206 
2207   return exp;
2208 }
2209 
2210 
2211 /* Perform default promotions for C data used in expressions.
2212    Enumeral types or short or char are converted to int.
2213    In addition, manifest constants symbols are replaced by their values.  */
2214 
2215 tree
default_conversion(tree exp)2216 default_conversion (tree exp)
2217 {
2218   tree orig_exp;
2219   tree type = TREE_TYPE (exp);
2220   enum tree_code code = TREE_CODE (type);
2221   tree promoted_type;
2222 
2223   mark_exp_read (exp);
2224 
2225   /* Functions and arrays have been converted during parsing.  */
2226   gcc_assert (code != FUNCTION_TYPE);
2227   if (code == ARRAY_TYPE)
2228     return exp;
2229 
2230   /* Constants can be used directly unless they're not loadable.  */
2231   if (TREE_CODE (exp) == CONST_DECL)
2232     exp = DECL_INITIAL (exp);
2233 
2234   /* Strip no-op conversions.  */
2235   orig_exp = exp;
2236   STRIP_TYPE_NOPS (exp);
2237 
2238   if (TREE_NO_WARNING (orig_exp))
2239     TREE_NO_WARNING (exp) = 1;
2240 
2241   if (code == VOID_TYPE)
2242     {
2243       error_at (EXPR_LOC_OR_LOC (exp, input_location),
2244 		"void value not ignored as it ought to be");
2245       return error_mark_node;
2246     }
2247 
2248   exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
2249   if (exp == error_mark_node)
2250     return error_mark_node;
2251 
2252   promoted_type = targetm.promoted_type (type);
2253   if (promoted_type)
2254     return convert (promoted_type, exp);
2255 
2256   if (INTEGRAL_TYPE_P (type))
2257     return perform_integral_promotions (exp);
2258 
2259   return exp;
2260 }
2261 
2262 /* Look up COMPONENT in a structure or union TYPE.
2263 
2264    If the component name is not found, returns NULL_TREE.  Otherwise,
2265    the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2266    stepping down the chain to the component, which is in the last
2267    TREE_VALUE of the list.  Normally the list is of length one, but if
2268    the component is embedded within (nested) anonymous structures or
2269    unions, the list steps down the chain to the component.  */
2270 
2271 static tree
lookup_field(tree type,tree component)2272 lookup_field (tree type, tree component)
2273 {
2274   tree field;
2275 
2276   /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2277      to the field elements.  Use a binary search on this array to quickly
2278      find the element.  Otherwise, do a linear search.  TYPE_LANG_SPECIFIC
2279      will always be set for structures which have many elements.
2280 
2281      Duplicate field checking replaces duplicates with NULL_TREE so
2282      TYPE_LANG_SPECIFIC arrays are potentially no longer sorted.  In that
2283      case just iterate using DECL_CHAIN.  */
2284 
2285   if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2286       && !seen_error ())
2287     {
2288       int bot, top, half;
2289       tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2290 
2291       field = TYPE_FIELDS (type);
2292       bot = 0;
2293       top = TYPE_LANG_SPECIFIC (type)->s->len;
2294       while (top - bot > 1)
2295 	{
2296 	  half = (top - bot + 1) >> 1;
2297 	  field = field_array[bot+half];
2298 
2299 	  if (DECL_NAME (field) == NULL_TREE)
2300 	    {
2301 	      /* Step through all anon unions in linear fashion.  */
2302 	      while (DECL_NAME (field_array[bot]) == NULL_TREE)
2303 		{
2304 		  field = field_array[bot++];
2305 		  if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2306 		    {
2307 		      tree anon = lookup_field (TREE_TYPE (field), component);
2308 
2309 		      if (anon)
2310 			return tree_cons (NULL_TREE, field, anon);
2311 
2312 		      /* The Plan 9 compiler permits referring
2313 			 directly to an anonymous struct/union field
2314 			 using a typedef name.  */
2315 		      if (flag_plan9_extensions
2316 			  && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2317 			  && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2318 			      == TYPE_DECL)
2319 			  && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2320 			      == component))
2321 			break;
2322 		    }
2323 		}
2324 
2325 	      /* Entire record is only anon unions.  */
2326 	      if (bot > top)
2327 		return NULL_TREE;
2328 
2329 	      /* Restart the binary search, with new lower bound.  */
2330 	      continue;
2331 	    }
2332 
2333 	  if (DECL_NAME (field) == component)
2334 	    break;
2335 	  if (DECL_NAME (field) < component)
2336 	    bot += half;
2337 	  else
2338 	    top = bot + half;
2339 	}
2340 
2341       if (DECL_NAME (field_array[bot]) == component)
2342 	field = field_array[bot];
2343       else if (DECL_NAME (field) != component)
2344 	return NULL_TREE;
2345     }
2346   else
2347     {
2348       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2349 	{
2350 	  if (DECL_NAME (field) == NULL_TREE
2351 	      && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2352 	    {
2353 	      tree anon = lookup_field (TREE_TYPE (field), component);
2354 
2355 	      if (anon)
2356 		return tree_cons (NULL_TREE, field, anon);
2357 
2358 	      /* The Plan 9 compiler permits referring directly to an
2359 		 anonymous struct/union field using a typedef
2360 		 name.  */
2361 	      if (flag_plan9_extensions
2362 		  && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2363 		  && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2364 		  && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2365 		      == component))
2366 		break;
2367 	    }
2368 
2369 	  if (DECL_NAME (field) == component)
2370 	    break;
2371 	}
2372 
2373       if (field == NULL_TREE)
2374 	return NULL_TREE;
2375     }
2376 
2377   return tree_cons (NULL_TREE, field, NULL_TREE);
2378 }
2379 
2380 /* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES.  */
2381 
2382 static void
lookup_field_fuzzy_find_candidates(tree type,tree component,vec<tree> * candidates)2383 lookup_field_fuzzy_find_candidates (tree type, tree component,
2384 				    vec<tree> *candidates)
2385 {
2386   tree field;
2387   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2388     {
2389       if (DECL_NAME (field) == NULL_TREE
2390 	  && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2391 	lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2392 					    candidates);
2393 
2394       if (DECL_NAME (field))
2395 	candidates->safe_push (DECL_NAME (field));
2396     }
2397 }
2398 
2399 /* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2400    rather than returning a TREE_LIST for an exact match.  */
2401 
2402 static tree
lookup_field_fuzzy(tree type,tree component)2403 lookup_field_fuzzy (tree type, tree component)
2404 {
2405   gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2406 
2407   /* First, gather a list of candidates.  */
2408   auto_vec <tree> candidates;
2409 
2410   lookup_field_fuzzy_find_candidates (type, component,
2411 				      &candidates);
2412 
2413   return find_closest_identifier (component, &candidates);
2414 }
2415 
2416 /* Support function for build_component_ref's error-handling.
2417 
2418    Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2419    struct or union, should we suggest "DATUM->COMPONENT" as a hint?  */
2420 
2421 static bool
should_suggest_deref_p(tree datum_type)2422 should_suggest_deref_p (tree datum_type)
2423 {
2424   /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2425      allows "." for ptrs; we could be handling a failed attempt
2426      to access a property.  */
2427   if (c_dialect_objc ())
2428     return false;
2429 
2430   /* Only suggest it for pointers...  */
2431   if (TREE_CODE (datum_type) != POINTER_TYPE)
2432     return false;
2433 
2434   /* ...to structs/unions.  */
2435   tree underlying_type = TREE_TYPE (datum_type);
2436   enum tree_code code = TREE_CODE (underlying_type);
2437   if (code == RECORD_TYPE || code == UNION_TYPE)
2438     return true;
2439   else
2440     return false;
2441 }
2442 
2443 /* Make an expression to refer to the COMPONENT field of structure or
2444    union value DATUM.  COMPONENT is an IDENTIFIER_NODE.  LOC is the
2445    location of the COMPONENT_REF.  COMPONENT_LOC is the location
2446    of COMPONENT.  */
2447 
2448 tree
build_component_ref(location_t loc,tree datum,tree component,location_t component_loc)2449 build_component_ref (location_t loc, tree datum, tree component,
2450 		     location_t component_loc)
2451 {
2452   tree type = TREE_TYPE (datum);
2453   enum tree_code code = TREE_CODE (type);
2454   tree field = NULL;
2455   tree ref;
2456   bool datum_lvalue = lvalue_p (datum);
2457 
2458   if (!objc_is_public (datum, component))
2459     return error_mark_node;
2460 
2461   /* Detect Objective-C property syntax object.property.  */
2462   if (c_dialect_objc ()
2463       && (ref = objc_maybe_build_component_ref (datum, component)))
2464     return ref;
2465 
2466   /* See if there is a field or component with name COMPONENT.  */
2467 
2468   if (code == RECORD_TYPE || code == UNION_TYPE)
2469     {
2470       if (!COMPLETE_TYPE_P (type))
2471 	{
2472 	  c_incomplete_type_error (loc, NULL_TREE, type);
2473 	  return error_mark_node;
2474 	}
2475 
2476       field = lookup_field (type, component);
2477 
2478       if (!field)
2479 	{
2480 	  tree guessed_id = lookup_field_fuzzy (type, component);
2481 	  if (guessed_id)
2482 	    {
2483 	      /* Attempt to provide a fixit replacement hint, if
2484 		 we have a valid range for the component.  */
2485 	      location_t reported_loc
2486 		= (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2487 	      gcc_rich_location rich_loc (reported_loc);
2488 	      if (component_loc != UNKNOWN_LOCATION)
2489 		rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
2490 	      error_at (&rich_loc,
2491 			"%qT has no member named %qE; did you mean %qE?",
2492 			type, component, guessed_id);
2493 	    }
2494 	  else
2495 	    error_at (loc, "%qT has no member named %qE", type, component);
2496 	  return error_mark_node;
2497 	}
2498 
2499       /* Accessing elements of atomic structures or unions is undefined
2500 	 behavior (C11 6.5.2.3#5).  */
2501       if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2502 	{
2503 	  if (code == RECORD_TYPE)
2504 	    warning_at (loc, 0, "accessing a member %qE of an atomic "
2505 			"structure %qE", component, datum);
2506 	  else
2507 	    warning_at (loc, 0, "accessing a member %qE of an atomic "
2508 			"union %qE", component, datum);
2509 	}
2510 
2511       /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2512 	 This might be better solved in future the way the C++ front
2513 	 end does it - by giving the anonymous entities each a
2514 	 separate name and type, and then have build_component_ref
2515 	 recursively call itself.  We can't do that here.  */
2516       do
2517 	{
2518 	  tree subdatum = TREE_VALUE (field);
2519 	  int quals;
2520 	  tree subtype;
2521 	  bool use_datum_quals;
2522 
2523 	  if (TREE_TYPE (subdatum) == error_mark_node)
2524 	    return error_mark_node;
2525 
2526 	  /* If this is an rvalue, it does not have qualifiers in C
2527 	     standard terms and we must avoid propagating such
2528 	     qualifiers down to a non-lvalue array that is then
2529 	     converted to a pointer.  */
2530 	  use_datum_quals = (datum_lvalue
2531 			     || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2532 
2533 	  quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2534 	  if (use_datum_quals)
2535 	    quals |= TYPE_QUALS (TREE_TYPE (datum));
2536 	  subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2537 
2538 	  ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2539 			NULL_TREE);
2540 	  SET_EXPR_LOCATION (ref, loc);
2541 	  if (TREE_READONLY (subdatum)
2542 	      || (use_datum_quals && TREE_READONLY (datum)))
2543 	    TREE_READONLY (ref) = 1;
2544 	  if (TREE_THIS_VOLATILE (subdatum)
2545 	      || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2546 	    TREE_THIS_VOLATILE (ref) = 1;
2547 
2548 	  if (TREE_DEPRECATED (subdatum))
2549 	    warn_deprecated_use (subdatum, NULL_TREE);
2550 
2551 	  datum = ref;
2552 
2553 	  field = TREE_CHAIN (field);
2554 	}
2555       while (field);
2556 
2557       return ref;
2558     }
2559   else if (should_suggest_deref_p (type))
2560     {
2561       /* Special-case the error message for "ptr.field" for the case
2562 	 where the user has confused "." vs "->".  */
2563       rich_location richloc (line_table, loc);
2564       /* "loc" should be the "." token.  */
2565       richloc.add_fixit_replace ("->");
2566       error_at (&richloc,
2567 		"%qE is a pointer; did you mean to use %<->%>?",
2568 		datum);
2569       return error_mark_node;
2570     }
2571   else if (code != ERROR_MARK)
2572     error_at (loc,
2573 	      "request for member %qE in something not a structure or union",
2574 	      component);
2575 
2576   return error_mark_node;
2577 }
2578 
2579 /* Given an expression PTR for a pointer, return an expression
2580    for the value pointed to.
2581    ERRORSTRING is the name of the operator to appear in error messages.
2582 
2583    LOC is the location to use for the generated tree.  */
2584 
2585 tree
build_indirect_ref(location_t loc,tree ptr,ref_operator errstring)2586 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2587 {
2588   tree pointer = default_conversion (ptr);
2589   tree type = TREE_TYPE (pointer);
2590   tree ref;
2591 
2592   if (TREE_CODE (type) == POINTER_TYPE)
2593     {
2594       if (CONVERT_EXPR_P (pointer)
2595           || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2596 	{
2597 	  /* If a warning is issued, mark it to avoid duplicates from
2598 	     the backend.  This only needs to be done at
2599 	     warn_strict_aliasing > 2.  */
2600 	  if (warn_strict_aliasing > 2)
2601 	    if (strict_aliasing_warning (EXPR_LOCATION (pointer),
2602 					 type, TREE_OPERAND (pointer, 0)))
2603 	      TREE_NO_WARNING (pointer) = 1;
2604 	}
2605 
2606       if (TREE_CODE (pointer) == ADDR_EXPR
2607 	  && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2608 	      == TREE_TYPE (type)))
2609 	{
2610 	  ref = TREE_OPERAND (pointer, 0);
2611 	  protected_set_expr_location (ref, loc);
2612 	  return ref;
2613 	}
2614       else
2615 	{
2616 	  tree t = TREE_TYPE (type);
2617 
2618 	  ref = build1 (INDIRECT_REF, t, pointer);
2619 
2620 	  if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2621 	    warning_at (loc, 0, "dereferencing %<void *%> pointer");
2622 
2623 	  /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2624 	     so that we get the proper error message if the result is used
2625 	     to assign to.  Also, &* is supposed to be a no-op.
2626 	     And ANSI C seems to specify that the type of the result
2627 	     should be the const type.  */
2628 	  /* A de-reference of a pointer to const is not a const.  It is valid
2629 	     to change it via some other pointer.  */
2630 	  TREE_READONLY (ref) = TYPE_READONLY (t);
2631 	  TREE_SIDE_EFFECTS (ref)
2632 	    = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2633 	  TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2634 	  protected_set_expr_location (ref, loc);
2635 	  return ref;
2636 	}
2637     }
2638   else if (TREE_CODE (pointer) != ERROR_MARK)
2639     invalid_indirection_error (loc, type, errstring);
2640 
2641   return error_mark_node;
2642 }
2643 
2644 /* This handles expressions of the form "a[i]", which denotes
2645    an array reference.
2646 
2647    This is logically equivalent in C to *(a+i), but we may do it differently.
2648    If A is a variable or a member, we generate a primitive ARRAY_REF.
2649    This avoids forcing the array out of registers, and can work on
2650    arrays that are not lvalues (for example, members of structures returned
2651    by functions).
2652 
2653    For vector types, allow vector[i] but not i[vector], and create
2654    *(((type*)&vectortype) + i) for the expression.
2655 
2656    LOC is the location to use for the returned expression.  */
2657 
2658 tree
build_array_ref(location_t loc,tree array,tree index)2659 build_array_ref (location_t loc, tree array, tree index)
2660 {
2661   tree ret;
2662   bool swapped = false;
2663   if (TREE_TYPE (array) == error_mark_node
2664       || TREE_TYPE (index) == error_mark_node)
2665     return error_mark_node;
2666 
2667   if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2668       && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2669       /* Allow vector[index] but not index[vector].  */
2670       && !gnu_vector_type_p (TREE_TYPE (array)))
2671     {
2672       if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2673 	  && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2674 	{
2675           error_at (loc,
2676             "subscripted value is neither array nor pointer nor vector");
2677 
2678 	  return error_mark_node;
2679 	}
2680       std::swap (array, index);
2681       swapped = true;
2682     }
2683 
2684   if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2685     {
2686       error_at (loc, "array subscript is not an integer");
2687       return error_mark_node;
2688     }
2689 
2690   if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2691     {
2692       error_at (loc, "subscripted value is pointer to function");
2693       return error_mark_node;
2694     }
2695 
2696   /* ??? Existing practice has been to warn only when the char
2697      index is syntactically the index, not for char[array].  */
2698   if (!swapped)
2699      warn_array_subscript_with_type_char (loc, index);
2700 
2701   /* Apply default promotions *after* noticing character types.  */
2702   index = default_conversion (index);
2703   if (index == error_mark_node)
2704     return error_mark_node;
2705 
2706   gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2707 
2708   bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2709   bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
2710 
2711   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2712     {
2713       tree rval, type;
2714 
2715       /* An array that is indexed by a non-constant
2716 	 cannot be stored in a register; we must be able to do
2717 	 address arithmetic on its address.
2718 	 Likewise an array of elements of variable size.  */
2719       if (TREE_CODE (index) != INTEGER_CST
2720 	  || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2721 	      && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2722 	{
2723 	  if (!c_mark_addressable (array, true))
2724 	    return error_mark_node;
2725 	}
2726       /* An array that is indexed by a constant value which is not within
2727 	 the array bounds cannot be stored in a register either; because we
2728 	 would get a crash in store_bit_field/extract_bit_field when trying
2729 	 to access a non-existent part of the register.  */
2730       if (TREE_CODE (index) == INTEGER_CST
2731 	  && TYPE_DOMAIN (TREE_TYPE (array))
2732 	  && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2733 	{
2734 	  if (!c_mark_addressable (array))
2735 	    return error_mark_node;
2736 	}
2737 
2738       if ((pedantic || warn_c90_c99_compat)
2739 	  && ! was_vector)
2740 	{
2741 	  tree foo = array;
2742 	  while (TREE_CODE (foo) == COMPONENT_REF)
2743 	    foo = TREE_OPERAND (foo, 0);
2744 	  if (VAR_P (foo) && C_DECL_REGISTER (foo))
2745 	    pedwarn (loc, OPT_Wpedantic,
2746 		     "ISO C forbids subscripting %<register%> array");
2747 	  else if (!lvalue_p (foo))
2748 	    pedwarn_c90 (loc, OPT_Wpedantic,
2749 			 "ISO C90 forbids subscripting non-lvalue "
2750 			 "array");
2751 	}
2752 
2753       type = TREE_TYPE (TREE_TYPE (array));
2754       rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2755       /* Array ref is const/volatile if the array elements are
2756 	 or if the array is.  */
2757       TREE_READONLY (rval)
2758 	|= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2759 	    | TREE_READONLY (array));
2760       TREE_SIDE_EFFECTS (rval)
2761 	|= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2762 	    | TREE_SIDE_EFFECTS (array));
2763       TREE_THIS_VOLATILE (rval)
2764 	|= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2765 	    /* This was added by rms on 16 Nov 91.
2766 	       It fixes  vol struct foo *a;  a->elts[1]
2767 	       in an inline function.
2768 	       Hope it doesn't break something else.  */
2769 	    | TREE_THIS_VOLATILE (array));
2770       ret = require_complete_type (loc, rval);
2771       protected_set_expr_location (ret, loc);
2772       if (non_lvalue)
2773 	ret = non_lvalue_loc (loc, ret);
2774       return ret;
2775     }
2776   else
2777     {
2778       tree ar = default_conversion (array);
2779 
2780       if (ar == error_mark_node)
2781 	return ar;
2782 
2783       gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2784       gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2785 
2786       ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2787 						      index, false),
2788 				RO_ARRAY_INDEXING);
2789       if (non_lvalue)
2790 	ret = non_lvalue_loc (loc, ret);
2791       return ret;
2792     }
2793 }
2794 
2795 /* Build an external reference to identifier ID.  FUN indicates
2796    whether this will be used for a function call.  LOC is the source
2797    location of the identifier.  This sets *TYPE to the type of the
2798    identifier, which is not the same as the type of the returned value
2799    for CONST_DECLs defined as enum constants.  If the type of the
2800    identifier is not available, *TYPE is set to NULL.  */
2801 tree
build_external_ref(location_t loc,tree id,bool fun,tree * type)2802 build_external_ref (location_t loc, tree id, bool fun, tree *type)
2803 {
2804   tree ref;
2805   tree decl = lookup_name (id);
2806 
2807   /* In Objective-C, an instance variable (ivar) may be preferred to
2808      whatever lookup_name() found.  */
2809   decl = objc_lookup_ivar (decl, id);
2810 
2811   *type = NULL;
2812   if (decl && decl != error_mark_node)
2813     {
2814       ref = decl;
2815       *type = TREE_TYPE (ref);
2816     }
2817   else if (fun)
2818     /* Implicit function declaration.  */
2819     ref = implicitly_declare (loc, id);
2820   else if (decl == error_mark_node)
2821     /* Don't complain about something that's already been
2822        complained about.  */
2823     return error_mark_node;
2824   else
2825     {
2826       undeclared_variable (loc, id);
2827       return error_mark_node;
2828     }
2829 
2830   if (TREE_TYPE (ref) == error_mark_node)
2831     return error_mark_node;
2832 
2833   if (TREE_DEPRECATED (ref))
2834     warn_deprecated_use (ref, NULL_TREE);
2835 
2836   /* Recursive call does not count as usage.  */
2837   if (ref != current_function_decl)
2838     {
2839       TREE_USED (ref) = 1;
2840     }
2841 
2842   if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2843     {
2844       if (!in_sizeof && !in_typeof)
2845 	C_DECL_USED (ref) = 1;
2846       else if (DECL_INITIAL (ref) == NULL_TREE
2847 	       && DECL_EXTERNAL (ref)
2848 	       && !TREE_PUBLIC (ref))
2849 	record_maybe_used_decl (ref);
2850     }
2851 
2852   if (TREE_CODE (ref) == CONST_DECL)
2853     {
2854       used_types_insert (TREE_TYPE (ref));
2855 
2856       if (warn_cxx_compat
2857 	  && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2858 	  && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2859 	{
2860 	  warning_at (loc, OPT_Wc___compat,
2861 		      ("enum constant defined in struct or union "
2862 		       "is not visible in C++"));
2863 	  inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2864 	}
2865 
2866       ref = DECL_INITIAL (ref);
2867       TREE_CONSTANT (ref) = 1;
2868     }
2869   else if (current_function_decl != NULL_TREE
2870 	   && !DECL_FILE_SCOPE_P (current_function_decl)
2871 	   && (VAR_OR_FUNCTION_DECL_P (ref)
2872 	       || TREE_CODE (ref) == PARM_DECL))
2873     {
2874       tree context = decl_function_context (ref);
2875 
2876       if (context != NULL_TREE && context != current_function_decl)
2877 	DECL_NONLOCAL (ref) = 1;
2878     }
2879   /* C99 6.7.4p3: An inline definition of a function with external
2880      linkage ... shall not contain a reference to an identifier with
2881      internal linkage.  */
2882   else if (current_function_decl != NULL_TREE
2883 	   && DECL_DECLARED_INLINE_P (current_function_decl)
2884 	   && DECL_EXTERNAL (current_function_decl)
2885 	   && VAR_OR_FUNCTION_DECL_P (ref)
2886 	   && (!VAR_P (ref) || TREE_STATIC (ref))
2887 	   && ! TREE_PUBLIC (ref)
2888 	   && DECL_CONTEXT (ref) != current_function_decl)
2889     record_inline_static (loc, current_function_decl, ref,
2890 			  csi_internal);
2891 
2892   return ref;
2893 }
2894 
2895 /* Record details of decls possibly used inside sizeof or typeof.  */
2896 struct maybe_used_decl
2897 {
2898   /* The decl.  */
2899   tree decl;
2900   /* The level seen at (in_sizeof + in_typeof).  */
2901   int level;
2902   /* The next one at this level or above, or NULL.  */
2903   struct maybe_used_decl *next;
2904 };
2905 
2906 static struct maybe_used_decl *maybe_used_decls;
2907 
2908 /* Record that DECL, an undefined static function reference seen
2909    inside sizeof or typeof, might be used if the operand of sizeof is
2910    a VLA type or the operand of typeof is a variably modified
2911    type.  */
2912 
2913 static void
record_maybe_used_decl(tree decl)2914 record_maybe_used_decl (tree decl)
2915 {
2916   struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2917   t->decl = decl;
2918   t->level = in_sizeof + in_typeof;
2919   t->next = maybe_used_decls;
2920   maybe_used_decls = t;
2921 }
2922 
2923 /* Pop the stack of decls possibly used inside sizeof or typeof.  If
2924    USED is false, just discard them.  If it is true, mark them used
2925    (if no longer inside sizeof or typeof) or move them to the next
2926    level up (if still inside sizeof or typeof).  */
2927 
2928 void
pop_maybe_used(bool used)2929 pop_maybe_used (bool used)
2930 {
2931   struct maybe_used_decl *p = maybe_used_decls;
2932   int cur_level = in_sizeof + in_typeof;
2933   while (p && p->level > cur_level)
2934     {
2935       if (used)
2936 	{
2937 	  if (cur_level == 0)
2938 	    C_DECL_USED (p->decl) = 1;
2939 	  else
2940 	    p->level = cur_level;
2941 	}
2942       p = p->next;
2943     }
2944   if (!used || cur_level == 0)
2945     maybe_used_decls = p;
2946 }
2947 
2948 /* Return the result of sizeof applied to EXPR.  */
2949 
2950 struct c_expr
c_expr_sizeof_expr(location_t loc,struct c_expr expr)2951 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2952 {
2953   struct c_expr ret;
2954   if (expr.value == error_mark_node)
2955     {
2956       ret.value = error_mark_node;
2957       ret.original_code = ERROR_MARK;
2958       ret.original_type = NULL;
2959       pop_maybe_used (false);
2960     }
2961   else
2962     {
2963       bool expr_const_operands = true;
2964 
2965       if (TREE_CODE (expr.value) == PARM_DECL
2966 	  && C_ARRAY_PARAMETER (expr.value))
2967 	{
2968 	  auto_diagnostic_group d;
2969 	  if (warning_at (loc, OPT_Wsizeof_array_argument,
2970 			  "%<sizeof%> on array function parameter %qE will "
2971 			  "return size of %qT", expr.value,
2972 			  TREE_TYPE (expr.value)))
2973 	    inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2974 	}
2975       tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2976 				       &expr_const_operands);
2977       ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2978       c_last_sizeof_arg = expr.value;
2979       c_last_sizeof_loc = loc;
2980       ret.original_code = SIZEOF_EXPR;
2981       ret.original_type = NULL;
2982       if (c_vla_type_p (TREE_TYPE (folded_expr)))
2983 	{
2984 	  /* sizeof is evaluated when given a vla (C99 6.5.3.4p2).  */
2985 	  ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2986 			      folded_expr, ret.value);
2987 	  C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2988 	  SET_EXPR_LOCATION (ret.value, loc);
2989 	}
2990       pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2991     }
2992   return ret;
2993 }
2994 
2995 /* Return the result of sizeof applied to T, a structure for the type
2996    name passed to sizeof (rather than the type itself).  LOC is the
2997    location of the original expression.  */
2998 
2999 struct c_expr
c_expr_sizeof_type(location_t loc,struct c_type_name * t)3000 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
3001 {
3002   tree type;
3003   struct c_expr ret;
3004   tree type_expr = NULL_TREE;
3005   bool type_expr_const = true;
3006   type = groktypename (t, &type_expr, &type_expr_const);
3007   ret.value = c_sizeof (loc, type);
3008   c_last_sizeof_arg = type;
3009   c_last_sizeof_loc = loc;
3010   ret.original_code = SIZEOF_EXPR;
3011   ret.original_type = NULL;
3012   if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
3013       && c_vla_type_p (type))
3014     {
3015       /* If the type is a [*] array, it is a VLA but is represented as
3016 	 having a size of zero.  In such a case we must ensure that
3017 	 the result of sizeof does not get folded to a constant by
3018 	 c_fully_fold, because if the size is evaluated the result is
3019 	 not constant and so constraints on zero or negative size
3020 	 arrays must not be applied when this sizeof call is inside
3021 	 another array declarator.  */
3022       if (!type_expr)
3023 	type_expr = integer_zero_node;
3024       ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
3025 			  type_expr, ret.value);
3026       C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
3027     }
3028   pop_maybe_used (type != error_mark_node
3029 		  ? C_TYPE_VARIABLE_SIZE (type) : false);
3030   return ret;
3031 }
3032 
3033 /* Build a function call to function FUNCTION with parameters PARAMS.
3034    The function call is at LOC.
3035    PARAMS is a list--a chain of TREE_LIST nodes--in which the
3036    TREE_VALUE of each node is a parameter-expression.
3037    FUNCTION's data type may be a function type or a pointer-to-function.  */
3038 
3039 tree
build_function_call(location_t loc,tree function,tree params)3040 build_function_call (location_t loc, tree function, tree params)
3041 {
3042   vec<tree, va_gc> *v;
3043   tree ret;
3044 
3045   vec_alloc (v, list_length (params));
3046   for (; params; params = TREE_CHAIN (params))
3047     v->quick_push (TREE_VALUE (params));
3048   ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
3049   vec_free (v);
3050   return ret;
3051 }
3052 
3053 /* Give a note about the location of the declaration of DECL.  */
3054 
3055 static void
inform_declaration(tree decl)3056 inform_declaration (tree decl)
3057 {
3058   if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
3059     inform (DECL_SOURCE_LOCATION (decl), "declared here");
3060 }
3061 
3062 /* Build a function call to function FUNCTION with parameters PARAMS.
3063    If FUNCTION is the result of resolving an overloaded target built-in,
3064    ORIG_FUNDECL is the original function decl, otherwise it is null.
3065    ORIGTYPES, if not NULL, is a vector of types; each element is
3066    either NULL or the original type of the corresponding element in
3067    PARAMS.  The original type may differ from TREE_TYPE of the
3068    parameter for enums.  FUNCTION's data type may be a function type
3069    or pointer-to-function.  This function changes the elements of
3070    PARAMS.  */
3071 
3072 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,tree orig_fundecl)3073 build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3074 			 tree function, vec<tree, va_gc> *params,
3075 			 vec<tree, va_gc> *origtypes, tree orig_fundecl)
3076 {
3077   tree fntype, fundecl = NULL_TREE;
3078   tree name = NULL_TREE, result;
3079   tree tem;
3080   int nargs;
3081   tree *argarray;
3082 
3083 
3084   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
3085   STRIP_TYPE_NOPS (function);
3086 
3087   /* Convert anything with function type to a pointer-to-function.  */
3088   if (TREE_CODE (function) == FUNCTION_DECL)
3089     {
3090       name = DECL_NAME (function);
3091 
3092       if (flag_tm)
3093 	tm_malloc_replacement (function);
3094       fundecl = function;
3095       if (!orig_fundecl)
3096 	orig_fundecl = fundecl;
3097       /* Atomic functions have type checking/casting already done.  They are
3098 	 often rewritten and don't match the original parameter list.  */
3099       if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3100         origtypes = NULL;
3101     }
3102   if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
3103     function = function_to_pointer_conversion (loc, function);
3104 
3105   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3106      expressions, like those used for ObjC messenger dispatches.  */
3107   if (params && !params->is_empty ())
3108     function = objc_rewrite_function_call (function, (*params)[0]);
3109 
3110   function = c_fully_fold (function, false, NULL);
3111 
3112   fntype = TREE_TYPE (function);
3113 
3114   if (TREE_CODE (fntype) == ERROR_MARK)
3115     return error_mark_node;
3116 
3117   if (!(TREE_CODE (fntype) == POINTER_TYPE
3118 	&& TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3119     {
3120       if (!flag_diagnostics_show_caret)
3121 	error_at (loc,
3122 		  "called object %qE is not a function or function pointer",
3123 		  function);
3124       else if (DECL_P (function))
3125 	{
3126 	  error_at (loc,
3127 		    "called object %qD is not a function or function pointer",
3128 		    function);
3129 	  inform_declaration (function);
3130 	}
3131       else
3132 	error_at (loc,
3133 		  "called object is not a function or function pointer");
3134       return error_mark_node;
3135     }
3136 
3137   if (fundecl && TREE_THIS_VOLATILE (fundecl))
3138     current_function_returns_abnormally = 1;
3139 
3140   /* fntype now gets the type of function pointed to.  */
3141   fntype = TREE_TYPE (fntype);
3142 
3143   /* Convert the parameters to the types declared in the
3144      function prototype, or apply default promotions.  */
3145 
3146   nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3147 			     origtypes, function, fundecl);
3148   if (nargs < 0)
3149     return error_mark_node;
3150 
3151   /* Check that the function is called through a compatible prototype.
3152      If it is not, warn.  */
3153   if (CONVERT_EXPR_P (function)
3154       && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3155       && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3156       && !comptypes (fntype, TREE_TYPE (tem)))
3157     {
3158       tree return_type = TREE_TYPE (fntype);
3159 
3160       /* This situation leads to run-time undefined behavior.  We can't,
3161 	 therefore, simply error unless we can prove that all possible
3162 	 executions of the program must execute the code.  */
3163       warning_at (loc, 0, "function called through a non-compatible type");
3164 
3165       if (VOID_TYPE_P (return_type)
3166 	  && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3167 	pedwarn (loc, 0,
3168 		 "function with qualified void return type called");
3169      }
3170 
3171   argarray = vec_safe_address (params);
3172 
3173   /* Check that arguments to builtin functions match the expectations.  */
3174   if (fundecl
3175       && fndecl_built_in_p (fundecl)
3176       && !check_builtin_function_arguments (loc, arg_loc, fundecl,
3177 					    orig_fundecl, nargs, argarray))
3178     return error_mark_node;
3179 
3180   /* Check that the arguments to the function are valid.  */
3181   bool warned_p = check_function_arguments (loc, fundecl, fntype,
3182 					    nargs, argarray, &arg_loc);
3183 
3184   if (name != NULL_TREE
3185       && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
3186     {
3187       if (require_constant_value)
3188 	result
3189 	  = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3190 						   function, nargs, argarray);
3191       else
3192 	result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3193 					    function, nargs, argarray);
3194       if (TREE_CODE (result) == NOP_EXPR
3195 	  && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3196 	STRIP_TYPE_NOPS (result);
3197     }
3198   else
3199     result = build_call_array_loc (loc, TREE_TYPE (fntype),
3200 				   function, nargs, argarray);
3201   /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3202      later.  */
3203   if (warned_p && TREE_CODE (result) == CALL_EXPR)
3204     TREE_NO_WARNING (result) = 1;
3205 
3206   /* In this improbable scenario, a nested function returns a VM type.
3207      Create a TARGET_EXPR so that the call always has a LHS, much as
3208      what the C++ FE does for functions returning non-PODs.  */
3209   if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3210     {
3211       tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3212       result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3213 		       NULL_TREE, NULL_TREE);
3214     }
3215 
3216   if (VOID_TYPE_P (TREE_TYPE (result)))
3217     {
3218       if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
3219 	pedwarn (loc, 0,
3220 		 "function with qualified void return type called");
3221       return result;
3222     }
3223   return require_complete_type (loc, result);
3224 }
3225 
3226 /* Like build_function_call_vec, but call also resolve_overloaded_builtin.  */
3227 
3228 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)3229 c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3230 			   tree function, vec<tree, va_gc> *params,
3231 			   vec<tree, va_gc> *origtypes)
3232 {
3233   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
3234   STRIP_TYPE_NOPS (function);
3235 
3236   /* Convert anything with function type to a pointer-to-function.  */
3237   if (TREE_CODE (function) == FUNCTION_DECL)
3238     {
3239       /* Implement type-directed function overloading for builtins.
3240 	 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3241 	 handle all the type checking.  The result is a complete expression
3242 	 that implements this function call.  */
3243       tree tem = resolve_overloaded_builtin (loc, function, params);
3244       if (tem)
3245 	return tem;
3246     }
3247   return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3248 }
3249 
3250 /* Helper for convert_arguments called to convert the VALue of argument
3251    number ARGNUM from ORIGTYPE to the corresponding parameter number
3252    PARMNUM and TYPE.
3253    PLOC is the location where the conversion is being performed.
3254    FUNCTION and FUNDECL are the same as in convert_arguments.
3255    VALTYPE is the original type of VAL before the conversion and,
3256    for EXCESS_PRECISION_EXPR, the operand of the expression.
3257    NPC is true if VAL represents the null pointer constant (VAL itself
3258    will have been folded to an integer constant).
3259    RNAME is the same as FUNCTION except in Objective C when it's
3260    the function selector.
3261    EXCESS_PRECISION is true when VAL was originally represented
3262    as EXCESS_PRECISION_EXPR.
3263    WARNOPT is the same as in convert_for_assignment.  */
3264 
3265 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)3266 convert_argument (location_t ploc, tree function, tree fundecl,
3267 		  tree type, tree origtype, tree val, tree valtype,
3268 		  bool npc, tree rname, int parmnum, int argnum,
3269 		  bool excess_precision, int warnopt)
3270 {
3271   /* Formal parm type is specified by a function prototype.  */
3272 
3273   if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3274     {
3275       error_at (ploc, "type of formal parameter %d is incomplete",
3276 		parmnum + 1);
3277       return val;
3278     }
3279 
3280   /* Optionally warn about conversions that differ from the default
3281      conversions.  */
3282   if (warn_traditional_conversion || warn_traditional)
3283     {
3284       unsigned int formal_prec = TYPE_PRECISION (type);
3285 
3286       if (INTEGRAL_TYPE_P (type)
3287 	  && TREE_CODE (valtype) == REAL_TYPE)
3288 	warning_at (ploc, OPT_Wtraditional_conversion,
3289 		    "passing argument %d of %qE as integer rather "
3290 		    "than floating due to prototype",
3291 		    argnum, rname);
3292       if (INTEGRAL_TYPE_P (type)
3293 	  && TREE_CODE (valtype) == COMPLEX_TYPE)
3294 	warning_at (ploc, OPT_Wtraditional_conversion,
3295 		    "passing argument %d of %qE as integer rather "
3296 		    "than complex due to prototype",
3297 		    argnum, rname);
3298       else if (TREE_CODE (type) == COMPLEX_TYPE
3299 	       && TREE_CODE (valtype) == REAL_TYPE)
3300 	warning_at (ploc, OPT_Wtraditional_conversion,
3301 		    "passing argument %d of %qE as complex rather "
3302 		    "than floating due to prototype",
3303 		    argnum, rname);
3304       else if (TREE_CODE (type) == REAL_TYPE
3305 	       && INTEGRAL_TYPE_P (valtype))
3306 	warning_at (ploc, OPT_Wtraditional_conversion,
3307 		    "passing argument %d of %qE as floating rather "
3308 		    "than integer due to prototype",
3309 		    argnum, rname);
3310       else if (TREE_CODE (type) == COMPLEX_TYPE
3311 	       && INTEGRAL_TYPE_P (valtype))
3312 	warning_at (ploc, OPT_Wtraditional_conversion,
3313 		    "passing argument %d of %qE as complex rather "
3314 		    "than integer due to prototype",
3315 		    argnum, rname);
3316       else if (TREE_CODE (type) == REAL_TYPE
3317 	       && TREE_CODE (valtype) == COMPLEX_TYPE)
3318 	warning_at (ploc, OPT_Wtraditional_conversion,
3319 		    "passing argument %d of %qE as floating rather "
3320 		    "than complex due to prototype",
3321 		    argnum, rname);
3322       /* ??? At some point, messages should be written about
3323 	 conversions between complex types, but that's too messy
3324 	 to do now.  */
3325       else if (TREE_CODE (type) == REAL_TYPE
3326 	       && TREE_CODE (valtype) == REAL_TYPE)
3327 	{
3328 	  /* Warn if any argument is passed as `float',
3329 	     since without a prototype it would be `double'.  */
3330 	  if (formal_prec == TYPE_PRECISION (float_type_node)
3331 	      && type != dfloat32_type_node)
3332 	    warning_at (ploc, 0,
3333 			"passing argument %d of %qE as %<float%> "
3334 			"rather than %<double%> due to prototype",
3335 			argnum, rname);
3336 
3337 	  /* Warn if mismatch between argument and prototype
3338 	     for decimal float types.  Warn of conversions with
3339 	     binary float types and of precision narrowing due to
3340 	     prototype.  */
3341 	  else if (type != valtype
3342 		   && (type == dfloat32_type_node
3343 		       || type == dfloat64_type_node
3344 		       || type == dfloat128_type_node
3345 		       || valtype == dfloat32_type_node
3346 		       || valtype == dfloat64_type_node
3347 		       || valtype == dfloat128_type_node)
3348 		   && (formal_prec
3349 		       <= TYPE_PRECISION (valtype)
3350 		       || (type == dfloat128_type_node
3351 			   && (valtype
3352 			       != dfloat64_type_node
3353 			       && (valtype
3354 				   != dfloat32_type_node)))
3355 		       || (type == dfloat64_type_node
3356 			   && (valtype
3357 			       != dfloat32_type_node))))
3358 	    warning_at (ploc, 0,
3359 			"passing argument %d of %qE as %qT "
3360 			"rather than %qT due to prototype",
3361 			argnum, rname, type, valtype);
3362 
3363 	}
3364       /* Detect integer changing in width or signedness.
3365 	 These warnings are only activated with
3366 	 -Wtraditional-conversion, not with -Wtraditional.  */
3367       else if (warn_traditional_conversion
3368 	       && INTEGRAL_TYPE_P (type)
3369 	       && INTEGRAL_TYPE_P (valtype))
3370 	{
3371 	  tree would_have_been = default_conversion (val);
3372 	  tree type1 = TREE_TYPE (would_have_been);
3373 
3374 	  if (val == error_mark_node)
3375 	    /* VAL could have been of incomplete type.  */;
3376 	  else if (TREE_CODE (type) == ENUMERAL_TYPE
3377 		   && (TYPE_MAIN_VARIANT (type)
3378 		       == TYPE_MAIN_VARIANT (valtype)))
3379 	    /* No warning if function asks for enum
3380 	       and the actual arg is that enum type.  */
3381 	    ;
3382 	  else if (formal_prec != TYPE_PRECISION (type1))
3383 	    warning_at (ploc, OPT_Wtraditional_conversion,
3384 			"passing argument %d of %qE "
3385 			"with different width due to prototype",
3386 			argnum, rname);
3387 	  else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3388 	    ;
3389 	  /* Don't complain if the formal parameter type
3390 	     is an enum, because we can't tell now whether
3391 	     the value was an enum--even the same enum.  */
3392 	  else if (TREE_CODE (type) == ENUMERAL_TYPE)
3393 	    ;
3394 	  else if (TREE_CODE (val) == INTEGER_CST
3395 		   && int_fits_type_p (val, type))
3396 	    /* Change in signedness doesn't matter
3397 	       if a constant value is unaffected.  */
3398 	    ;
3399 	  /* If the value is extended from a narrower
3400 	     unsigned type, it doesn't matter whether we
3401 	     pass it as signed or unsigned; the value
3402 	     certainly is the same either way.  */
3403 	  else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3404 		   && TYPE_UNSIGNED (valtype))
3405 	    ;
3406 	  else if (TYPE_UNSIGNED (type))
3407 	    warning_at (ploc, OPT_Wtraditional_conversion,
3408 			"passing argument %d of %qE "
3409 			"as unsigned due to prototype",
3410 			argnum, rname);
3411 	  else
3412 	    warning_at (ploc, OPT_Wtraditional_conversion,
3413 			"passing argument %d of %qE "
3414 			"as signed due to prototype",
3415 			argnum, rname);
3416 	}
3417     }
3418 
3419   /* Possibly restore an EXCESS_PRECISION_EXPR for the
3420      sake of better warnings from convert_and_check.  */
3421   if (excess_precision)
3422     val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3423 
3424   tree parmval = convert_for_assignment (ploc, ploc, type,
3425 					 val, origtype, ic_argpass,
3426 					 npc, fundecl, function,
3427 					 parmnum + 1, warnopt);
3428 
3429   if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3430       && INTEGRAL_TYPE_P (type)
3431       && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3432     parmval = default_conversion (parmval);
3433 
3434   return parmval;
3435 }
3436 
3437 /* Convert the argument expressions in the vector VALUES
3438    to the types in the list TYPELIST.
3439 
3440    If TYPELIST is exhausted, or when an element has NULL as its type,
3441    perform the default conversions.
3442 
3443    ORIGTYPES is the original types of the expressions in VALUES.  This
3444    holds the type of enum values which have been converted to integral
3445    types.  It may be NULL.
3446 
3447    FUNCTION is a tree for the called function.  It is used only for
3448    error messages, where it is formatted with %qE.
3449 
3450    This is also where warnings about wrong number of args are generated.
3451 
3452    ARG_LOC are locations of function arguments (if any).
3453 
3454    Returns the actual number of arguments processed (which may be less
3455    than the length of VALUES in some error situations), or -1 on
3456    failure.  */
3457 
3458 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)3459 convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3460 		   vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3461 		   tree function, tree fundecl)
3462 {
3463   unsigned int parmnum;
3464   bool error_args = false;
3465   const bool type_generic = fundecl
3466     && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
3467   bool type_generic_remove_excess_precision = false;
3468   bool type_generic_overflow_p = false;
3469   tree selector;
3470 
3471   /* Change pointer to function to the function itself for
3472      diagnostics.  */
3473   if (TREE_CODE (function) == ADDR_EXPR
3474       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
3475     function = TREE_OPERAND (function, 0);
3476 
3477   /* Handle an ObjC selector specially for diagnostics.  */
3478   selector = objc_message_selector ();
3479 
3480   /* For a call to a built-in function declared without a prototype,
3481      set to the built-in function's argument list.  */
3482   tree builtin_typelist = NULL_TREE;
3483 
3484   /* For type-generic built-in functions, determine whether excess
3485      precision should be removed (classification) or not
3486      (comparison).  */
3487   if (fundecl
3488       && fndecl_built_in_p (fundecl, BUILT_IN_NORMAL))
3489     {
3490       built_in_function code = DECL_FUNCTION_CODE (fundecl);
3491       if (C_DECL_BUILTIN_PROTOTYPE (fundecl))
3492 	{
3493 	  /* For a call to a built-in function declared without a prototype
3494 	     use the types of the parameters of the internal built-in to
3495 	     match those of the arguments to.  */
3496 	  if (tree bdecl = builtin_decl_explicit (code))
3497 	    builtin_typelist = TYPE_ARG_TYPES (TREE_TYPE (bdecl));
3498 	}
3499 
3500       /* For type-generic built-in functions, determine whether excess
3501 	 precision should be removed (classification) or not
3502 	 (comparison).  */
3503       if (type_generic)
3504 	switch (code)
3505 	  {
3506 	  case BUILT_IN_ISFINITE:
3507 	  case BUILT_IN_ISINF:
3508 	  case BUILT_IN_ISINF_SIGN:
3509 	  case BUILT_IN_ISNAN:
3510 	  case BUILT_IN_ISNORMAL:
3511 	  case BUILT_IN_FPCLASSIFY:
3512 	    type_generic_remove_excess_precision = true;
3513 	    break;
3514 
3515 	  case BUILT_IN_ADD_OVERFLOW_P:
3516 	  case BUILT_IN_SUB_OVERFLOW_P:
3517 	  case BUILT_IN_MUL_OVERFLOW_P:
3518 	    /* The last argument of these type-generic builtins
3519 	       should not be promoted.  */
3520 	    type_generic_overflow_p = true;
3521 	    break;
3522 
3523 	  default:
3524 	    break;
3525 	  }
3526     }
3527 
3528   /* Scan the given expressions (VALUES) and types (TYPELIST), producing
3529      individual converted arguments.  */
3530 
3531   tree typetail, builtin_typetail, val;
3532   for (typetail = typelist,
3533 	 builtin_typetail = builtin_typelist,
3534 	 parmnum = 0;
3535        values && values->iterate (parmnum, &val);
3536        ++parmnum)
3537     {
3538       /* The type of the function parameter (if it was declared with one).  */
3539       tree type = typetail ? TREE_VALUE (typetail) : NULL_TREE;
3540       /* The type of the built-in function parameter (if the function
3541 	 is a built-in).  Used to detect type incompatibilities in
3542 	 calls to built-ins declared without a prototype.  */
3543       tree builtin_type = (builtin_typetail
3544 			   ? TREE_VALUE (builtin_typetail) : NULL_TREE);
3545       /* The original type of the argument being passed to the function.  */
3546       tree valtype = TREE_TYPE (val);
3547       /* The called function (or function selector in Objective C).  */
3548       tree rname = function;
3549       int argnum = parmnum + 1;
3550       const char *invalid_func_diag;
3551       /* Set for EXCESS_PRECISION_EXPR arguments.  */
3552       bool excess_precision = false;
3553       /* The value of the argument after conversion to the type
3554 	 of the function parameter it is passed to.  */
3555       tree parmval;
3556       /* Some __atomic_* builtins have additional hidden argument at
3557 	 position 0.  */
3558       location_t ploc
3559 	= !arg_loc.is_empty () && values->length () == arg_loc.length ()
3560 	  ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3561 	  : input_location;
3562 
3563       if (type == void_type_node)
3564 	{
3565 	  if (selector)
3566 	    error_at (loc, "too many arguments to method %qE", selector);
3567 	  else
3568 	    error_at (loc, "too many arguments to function %qE", function);
3569 	  inform_declaration (fundecl);
3570 	  return error_args ? -1 : (int) parmnum;
3571 	}
3572 
3573       if (builtin_type == void_type_node)
3574 	{
3575 	  if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3576 			  "too many arguments to built-in function %qE "
3577 			  "expecting %d", function, parmnum))
3578 	    inform_declaration (fundecl);
3579 	  builtin_typetail = NULL_TREE;
3580 	}
3581 
3582       if (selector && argnum > 2)
3583 	{
3584 	  rname = selector;
3585 	  argnum -= 2;
3586 	}
3587 
3588       /* Determine if VAL is a null pointer constant before folding it.  */
3589       bool npc = null_pointer_constant_p (val);
3590 
3591       /* If there is excess precision and a prototype, convert once to
3592 	 the required type rather than converting via the semantic
3593 	 type.  Likewise without a prototype a float value represented
3594 	 as long double should be converted once to double.  But for
3595 	 type-generic classification functions excess precision must
3596 	 be removed here.  */
3597       if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3598 	  && (type || !type_generic || !type_generic_remove_excess_precision))
3599 	{
3600 	  val = TREE_OPERAND (val, 0);
3601 	  excess_precision = true;
3602 	}
3603       val = c_fully_fold (val, false, NULL);
3604       STRIP_TYPE_NOPS (val);
3605 
3606       val = require_complete_type (ploc, val);
3607 
3608       /* Some floating-point arguments must be promoted to double when
3609 	 no type is specified by a prototype.  This applies to
3610 	 arguments of type float, and to architecture-specific types
3611 	 (ARM __fp16), but not to _FloatN or _FloatNx types.  */
3612       bool promote_float_arg = false;
3613       if (type == NULL_TREE
3614 	  && TREE_CODE (valtype) == REAL_TYPE
3615 	  && (TYPE_PRECISION (valtype)
3616 	      <= TYPE_PRECISION (double_type_node))
3617 	  && TYPE_MAIN_VARIANT (valtype) != double_type_node
3618 	  && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3619 	  && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3620 	{
3621 	  /* Promote this argument, unless it has a _FloatN or
3622 	     _FloatNx type.  */
3623 	  promote_float_arg = true;
3624 	  for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3625 	    if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3626 	      {
3627 		promote_float_arg = false;
3628 		break;
3629 	      }
3630 	}
3631 
3632       if (type != NULL_TREE)
3633 	{
3634 	  tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3635 	  parmval = convert_argument (ploc, function, fundecl, type, origtype,
3636 				      val, valtype, npc, rname, parmnum, argnum,
3637 				      excess_precision, 0);
3638 	}
3639       else if (promote_float_arg)
3640         {
3641 	  if (type_generic)
3642 	    parmval = val;
3643 	  else
3644 	    {
3645 	      /* Convert `float' to `double'.  */
3646 	      if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3647 		warning_at (ploc, OPT_Wdouble_promotion,
3648 			    "implicit conversion from %qT to %qT when passing "
3649 			    "argument to function",
3650 			    valtype, double_type_node);
3651 	      parmval = convert (double_type_node, val);
3652 	    }
3653 	}
3654       else if ((excess_precision && !type_generic)
3655 	       || (type_generic_overflow_p && parmnum == 2))
3656 	/* A "double" argument with excess precision being passed
3657 	   without a prototype or in variable arguments.
3658 	   The last argument of __builtin_*_overflow_p should not be
3659 	   promoted.  */
3660 	parmval = convert (valtype, val);
3661       else if ((invalid_func_diag =
3662 		targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3663 	{
3664 	  error (invalid_func_diag);
3665 	  return -1;
3666 	}
3667       else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3668 	{
3669 	  return -1;
3670 	}
3671       else
3672 	/* Convert `short' and `char' to full-size `int'.  */
3673 	parmval = default_conversion (val);
3674 
3675       (*values)[parmnum] = parmval;
3676       if (parmval == error_mark_node)
3677 	error_args = true;
3678 
3679       if (!type && builtin_type && TREE_CODE (builtin_type) != VOID_TYPE)
3680 	{
3681 	  /* For a call to a built-in function declared without a prototype,
3682 	     perform the conversions from the argument to the expected type
3683 	     but issue warnings rather than errors for any mismatches.
3684 	     Ignore the converted argument and use the PARMVAL obtained
3685 	     above by applying default conversions instead.  */
3686 	  tree origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3687 	  convert_argument (ploc, function, fundecl, builtin_type, origtype,
3688 			    val, valtype, npc, rname, parmnum, argnum,
3689 			    excess_precision,
3690 			    OPT_Wbuiltin_declaration_mismatch);
3691 	}
3692 
3693       if (typetail)
3694 	typetail = TREE_CHAIN (typetail);
3695 
3696       if (builtin_typetail)
3697 	builtin_typetail = TREE_CHAIN (builtin_typetail);
3698     }
3699 
3700   gcc_assert (parmnum == vec_safe_length (values));
3701 
3702   if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3703     {
3704       error_at (loc, "too few arguments to function %qE", function);
3705       inform_declaration (fundecl);
3706       return -1;
3707     }
3708 
3709   if (builtin_typetail && TREE_VALUE (builtin_typetail) != void_type_node)
3710     {
3711       unsigned nargs = parmnum;
3712       for (tree t = builtin_typetail; t; t = TREE_CHAIN (t))
3713 	++nargs;
3714 
3715       if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
3716 		      "too few arguments to built-in function %qE "
3717 		      "expecting %u", function, nargs - 1))
3718 	inform_declaration (fundecl);
3719     }
3720 
3721   return error_args ? -1 : (int) parmnum;
3722 }
3723 
3724 /* This is the entry point used by the parser to build unary operators
3725    in the input.  CODE, a tree_code, specifies the unary operator, and
3726    ARG is the operand.  For unary plus, the C parser currently uses
3727    CONVERT_EXPR for code.
3728 
3729    LOC is the location to use for the tree generated.
3730 */
3731 
3732 struct c_expr
parser_build_unary_op(location_t loc,enum tree_code code,struct c_expr arg)3733 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3734 {
3735   struct c_expr result;
3736 
3737   result.original_code = code;
3738   result.original_type = NULL;
3739 
3740   if (reject_gcc_builtin (arg.value))
3741     {
3742       result.value = error_mark_node;
3743     }
3744   else
3745     {
3746       result.value = build_unary_op (loc, code, arg.value, false);
3747 
3748       if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3749 	overflow_warning (loc, result.value, arg.value);
3750     }
3751 
3752   /* We are typically called when parsing a prefix token at LOC acting on
3753      ARG.  Reflect this by updating the source range of the result to
3754      start at LOC and end at the end of ARG.  */
3755   set_c_expr_source_range (&result,
3756 			   loc, arg.get_finish ());
3757 
3758   return result;
3759 }
3760 
3761 /* Returns true if TYPE is a character type, *not* including wchar_t.  */
3762 
3763 static bool
char_type_p(tree type)3764 char_type_p (tree type)
3765 {
3766   return (type == char_type_node
3767 	  || type == unsigned_char_type_node
3768 	  || type == signed_char_type_node
3769 	  || type == char16_type_node
3770 	  || type == char32_type_node);
3771 }
3772 
3773 /* This is the entry point used by the parser to build binary operators
3774    in the input.  CODE, a tree_code, specifies the binary operator, and
3775    ARG1 and ARG2 are the operands.  In addition to constructing the
3776    expression, we check for operands that were written with other binary
3777    operators in a way that is likely to confuse the user.
3778 
3779    LOCATION is the location of the binary operator.  */
3780 
3781 struct c_expr
parser_build_binary_op(location_t location,enum tree_code code,struct c_expr arg1,struct c_expr arg2)3782 parser_build_binary_op (location_t location, enum tree_code code,
3783 			struct c_expr arg1, struct c_expr arg2)
3784 {
3785   struct c_expr result;
3786 
3787   enum tree_code code1 = arg1.original_code;
3788   enum tree_code code2 = arg2.original_code;
3789   tree type1 = (arg1.original_type
3790                 ? arg1.original_type
3791                 : TREE_TYPE (arg1.value));
3792   tree type2 = (arg2.original_type
3793                 ? arg2.original_type
3794                 : TREE_TYPE (arg2.value));
3795 
3796   result.value = build_binary_op (location, code,
3797 				  arg1.value, arg2.value, true);
3798   result.original_code = code;
3799   result.original_type = NULL;
3800 
3801   if (TREE_CODE (result.value) == ERROR_MARK)
3802     {
3803       set_c_expr_source_range (&result,
3804 			       arg1.get_start (),
3805 			       arg2.get_finish ());
3806       return result;
3807     }
3808 
3809   if (location != UNKNOWN_LOCATION)
3810     protected_set_expr_location (result.value, location);
3811 
3812   set_c_expr_source_range (&result,
3813 			   arg1.get_start (),
3814 			   arg2.get_finish ());
3815 
3816   /* Check for cases such as x+y<<z which users are likely
3817      to misinterpret.  */
3818   if (warn_parentheses)
3819     warn_about_parentheses (location, code, code1, arg1.value, code2,
3820 			    arg2.value);
3821 
3822   if (warn_logical_op)
3823     warn_logical_operator (location, code, TREE_TYPE (result.value),
3824 			   code1, arg1.value, code2, arg2.value);
3825 
3826   if (warn_tautological_compare)
3827     {
3828       tree lhs = arg1.value;
3829       tree rhs = arg2.value;
3830       if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3831 	{
3832 	  if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3833 	      && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3834 	    lhs = NULL_TREE;
3835 	  else
3836 	    lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3837 	}
3838       if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3839 	{
3840 	  if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3841 	      && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3842 	    rhs = NULL_TREE;
3843 	  else
3844 	    rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3845 	}
3846       if (lhs != NULL_TREE && rhs != NULL_TREE)
3847 	warn_tautological_cmp (location, code, lhs, rhs);
3848     }
3849 
3850   if (warn_logical_not_paren
3851       && TREE_CODE_CLASS (code) == tcc_comparison
3852       && code1 == TRUTH_NOT_EXPR
3853       && code2 != TRUTH_NOT_EXPR
3854       /* Avoid warning for !!x == y.  */
3855       && (TREE_CODE (arg1.value) != NE_EXPR
3856 	  || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3857     {
3858       /* Avoid warning for !b == y where b has _Bool type.  */
3859       tree t = integer_zero_node;
3860       if (TREE_CODE (arg1.value) == EQ_EXPR
3861 	  && integer_zerop (TREE_OPERAND (arg1.value, 1))
3862 	  && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3863 	{
3864 	  t = TREE_OPERAND (arg1.value, 0);
3865 	  do
3866 	    {
3867 	      if (TREE_TYPE (t) != integer_type_node)
3868 		break;
3869 	      if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3870 		t = C_MAYBE_CONST_EXPR_EXPR (t);
3871 	      else if (CONVERT_EXPR_P (t))
3872 		t = TREE_OPERAND (t, 0);
3873 	      else
3874 		break;
3875 	    }
3876 	  while (1);
3877 	}
3878       if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3879 	warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3880     }
3881 
3882   /* Warn about comparisons against string literals, with the exception
3883      of testing for equality or inequality of a string literal with NULL.  */
3884   if (code == EQ_EXPR || code == NE_EXPR)
3885     {
3886       if ((code1 == STRING_CST
3887 	   && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3888 	  || (code2 == STRING_CST
3889 	      && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
3890 	warning_at (location, OPT_Waddress,
3891 		    "comparison with string literal results in unspecified behavior");
3892       /* Warn for ptr == '\0', it's likely that it should've been ptr[0].  */
3893       if (POINTER_TYPE_P (type1)
3894 	  && null_pointer_constant_p (arg2.value)
3895 	  && char_type_p (type2))
3896 	{
3897 	  auto_diagnostic_group d;
3898 	  if (warning_at (location, OPT_Wpointer_compare,
3899 			    "comparison between pointer and zero character "
3900 			    "constant"))
3901 	    inform (arg1.get_start (),
3902 		      "did you mean to dereference the pointer?");
3903 	}
3904       else if (POINTER_TYPE_P (type2)
3905 	       && null_pointer_constant_p (arg1.value)
3906 	       && char_type_p (type1))
3907 	{
3908 	  auto_diagnostic_group d;
3909 	  if (warning_at (location, OPT_Wpointer_compare,
3910 			    "comparison between pointer and zero character "
3911 			    "constant"))
3912 	    inform (arg2.get_start (),
3913 		      "did you mean to dereference the pointer?");
3914 	}
3915     }
3916   else if (TREE_CODE_CLASS (code) == tcc_comparison
3917 	   && (code1 == STRING_CST || code2 == STRING_CST))
3918     warning_at (location, OPT_Waddress,
3919 		"comparison with string literal results in unspecified behavior");
3920 
3921   if (TREE_OVERFLOW_P (result.value)
3922       && !TREE_OVERFLOW_P (arg1.value)
3923       && !TREE_OVERFLOW_P (arg2.value))
3924     overflow_warning (location, result.value);
3925 
3926   /* Warn about comparisons of different enum types.  */
3927   if (warn_enum_compare
3928       && TREE_CODE_CLASS (code) == tcc_comparison
3929       && TREE_CODE (type1) == ENUMERAL_TYPE
3930       && TREE_CODE (type2) == ENUMERAL_TYPE
3931       && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3932     warning_at (location, OPT_Wenum_compare,
3933 		"comparison between %qT and %qT",
3934 		type1, type2);
3935 
3936   return result;
3937 }
3938 
3939 /* Return a tree for the difference of pointers OP0 and OP1.
3940    The resulting tree has type ptrdiff_t.  If POINTER_SUBTRACT sanitization is
3941    enabled, assign to INSTRUMENT_EXPR call to libsanitizer.  */
3942 
3943 static tree
pointer_diff(location_t loc,tree op0,tree op1,tree * instrument_expr)3944 pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3945 {
3946   tree restype = ptrdiff_type_node;
3947   tree result, inttype;
3948 
3949   addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3950   addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3951   tree target_type = TREE_TYPE (TREE_TYPE (op0));
3952   tree orig_op0 = op0;
3953   tree orig_op1 = op1;
3954 
3955   /* If the operands point into different address spaces, we need to
3956      explicitly convert them to pointers into the common address space
3957      before we can subtract the numerical address values.  */
3958   if (as0 != as1)
3959     {
3960       addr_space_t as_common;
3961       tree common_type;
3962 
3963       /* Determine the common superset address space.  This is guaranteed
3964 	 to exist because the caller verified that comp_target_types
3965 	 returned non-zero.  */
3966       if (!addr_space_superset (as0, as1, &as_common))
3967 	gcc_unreachable ();
3968 
3969       common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3970       op0 = convert (common_type, op0);
3971       op1 = convert (common_type, op1);
3972     }
3973 
3974   /* Determine integer type result of the subtraction.  This will usually
3975      be the same as the result type (ptrdiff_t), but may need to be a wider
3976      type if pointers for the address space are wider than ptrdiff_t.  */
3977   if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3978     inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3979   else
3980     inttype = restype;
3981 
3982   if (TREE_CODE (target_type) == VOID_TYPE)
3983     pedwarn (loc, OPT_Wpointer_arith,
3984 	     "pointer of type %<void *%> used in subtraction");
3985   if (TREE_CODE (target_type) == FUNCTION_TYPE)
3986     pedwarn (loc, OPT_Wpointer_arith,
3987 	     "pointer to a function used in subtraction");
3988 
3989   if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3990     {
3991       gcc_assert (current_function_decl != NULL_TREE);
3992 
3993       op0 = save_expr (op0);
3994       op1 = save_expr (op1);
3995 
3996       tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3997       *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3998     }
3999 
4000   /* First do the subtraction, then build the divide operator
4001      and only convert at the very end.
4002      Do not do default conversions in case restype is a short type.  */
4003 
4004   /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
4005      pointers.  If some platform cannot provide that, or has a larger
4006      ptrdiff_type to support differences larger than half the address
4007      space, cast the pointers to some larger integer type and do the
4008      computations in that type.  */
4009   if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
4010     op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
4011 			   convert (inttype, op1), false);
4012   else
4013     {
4014       /* Cast away qualifiers.  */
4015       op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
4016       op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
4017       op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
4018     }
4019 
4020   /* This generates an error if op1 is pointer to incomplete type.  */
4021   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
4022     error_at (loc, "arithmetic on pointer to an incomplete type");
4023   else if (verify_type_context (loc, TCTX_POINTER_ARITH,
4024 				TREE_TYPE (TREE_TYPE (orig_op0))))
4025     verify_type_context (loc, TCTX_POINTER_ARITH,
4026 			 TREE_TYPE (TREE_TYPE (orig_op1)));
4027 
4028   op1 = c_size_in_bytes (target_type);
4029 
4030   if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
4031     error_at (loc, "arithmetic on pointer to an empty aggregate");
4032 
4033   /* Divide by the size, in easiest possible way.  */
4034   result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
4035 			    op0, convert (inttype, op1));
4036 
4037   /* Convert to final result type if necessary.  */
4038   return convert (restype, result);
4039 }
4040 
4041 /* Expand atomic compound assignments into an appropriate sequence as
4042    specified by the C11 standard section 6.5.16.2.
4043 
4044        _Atomic T1 E1
4045        T2 E2
4046        E1 op= E2
4047 
4048   This sequence is used for all types for which these operations are
4049   supported.
4050 
4051   In addition, built-in versions of the 'fe' prefixed routines may
4052   need to be invoked for floating point (real, complex or vector) when
4053   floating-point exceptions are supported.  See 6.5.16.2 footnote 113.
4054 
4055   T1 newval;
4056   T1 old;
4057   T1 *addr
4058   T2 val
4059   fenv_t fenv
4060 
4061   addr = &E1;
4062   val = (E2);
4063   __atomic_load (addr, &old, SEQ_CST);
4064   feholdexcept (&fenv);
4065 loop:
4066     newval = old op val;
4067     if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
4068 					  SEQ_CST))
4069       goto done;
4070     feclearexcept (FE_ALL_EXCEPT);
4071     goto loop:
4072 done:
4073   feupdateenv (&fenv);
4074 
4075   The compiler will issue the __atomic_fetch_* built-in when possible,
4076   otherwise it will generate the generic form of the atomic operations.
4077   This requires temp(s) and has their address taken.  The atomic processing
4078   is smart enough to figure out when the size of an object can utilize
4079   a lock-free version, and convert the built-in call to the appropriate
4080   lock-free routine.  The optimizers will then dispose of any temps that
4081   are no longer required, and lock-free implementations are utilized as
4082   long as there is target support for the required size.
4083 
4084   If the operator is NOP_EXPR, then this is a simple assignment, and
4085   an __atomic_store is issued to perform the assignment rather than
4086   the above loop.  */
4087 
4088 /* Build an atomic assignment at LOC, expanding into the proper
4089    sequence to store LHS MODIFYCODE= RHS.  Return a value representing
4090    the result of the operation, unless RETURN_OLD_P, in which case
4091    return the old value of LHS (this is only for postincrement and
4092    postdecrement).  */
4093 
4094 static tree
build_atomic_assign(location_t loc,tree lhs,enum tree_code modifycode,tree rhs,bool return_old_p)4095 build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
4096 		     tree rhs, bool return_old_p)
4097 {
4098   tree fndecl, func_call;
4099   vec<tree, va_gc> *params;
4100   tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
4101   tree old, old_addr;
4102   tree compound_stmt = NULL_TREE;
4103   tree stmt, goto_stmt;
4104   tree loop_label, loop_decl, done_label, done_decl;
4105 
4106   tree lhs_type = TREE_TYPE (lhs);
4107   tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
4108   tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
4109   tree rhs_semantic_type = TREE_TYPE (rhs);
4110   tree nonatomic_rhs_semantic_type;
4111   tree rhs_type;
4112 
4113   gcc_assert (TYPE_ATOMIC (lhs_type));
4114 
4115   if (return_old_p)
4116     gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
4117 
4118   /* Allocate enough vector items for a compare_exchange.  */
4119   vec_alloc (params, 6);
4120 
4121   /* Create a compound statement to hold the sequence of statements
4122      with a loop.  */
4123   if (modifycode != NOP_EXPR)
4124     {
4125       compound_stmt = c_begin_compound_stmt (false);
4126 
4127       /* For consistency with build_modify_expr on non-_Atomic,
4128 	 mark the lhs as read.  Also, it would be very hard to match
4129 	 such expressions in mark_exp_read.  */
4130       mark_exp_read (lhs);
4131     }
4132 
4133   /* Remove any excess precision (which is only present here in the
4134      case of compound assignments).  */
4135   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4136     {
4137       gcc_assert (modifycode != NOP_EXPR);
4138       rhs = TREE_OPERAND (rhs, 0);
4139     }
4140   rhs_type = TREE_TYPE (rhs);
4141 
4142   /* Fold the RHS if it hasn't already been folded.  */
4143   if (modifycode != NOP_EXPR)
4144     rhs = c_fully_fold (rhs, false, NULL);
4145 
4146   /* Remove the qualifiers for the rest of the expressions and create
4147      the VAL temp variable to hold the RHS.  */
4148   nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
4149   nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
4150   nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
4151 						      TYPE_UNQUALIFIED);
4152   val = create_tmp_var_raw (nonatomic_rhs_type);
4153   TREE_ADDRESSABLE (val) = 1;
4154   TREE_NO_WARNING (val) = 1;
4155   rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
4156 		NULL_TREE);
4157   TREE_SIDE_EFFECTS (rhs) = 1;
4158   SET_EXPR_LOCATION (rhs, loc);
4159   if (modifycode != NOP_EXPR)
4160     add_stmt (rhs);
4161 
4162   /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
4163      an atomic_store.  */
4164   if (modifycode == NOP_EXPR)
4165     {
4166       compound_stmt = rhs;
4167       /* Build __atomic_store (&lhs, &val, SEQ_CST)  */
4168       rhs = build_unary_op (loc, ADDR_EXPR, val, false);
4169       fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
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       compound_stmt = build2 (COMPOUND_EXPR, void_type_node,
4176 			      compound_stmt, func_call);
4177 
4178       /* VAL is the value which was stored, return a COMPOUND_STMT of
4179 	 the statement and that value.  */
4180       return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4181     }
4182 
4183   /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4184      __atomic_*_fetch built-in rather than a CAS loop.  atomic_bool type
4185      isn't applicable for such builtins.  ??? Do we want to handle enums?  */
4186   if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4187       && TREE_CODE (rhs_type) == INTEGER_TYPE)
4188     {
4189       built_in_function fncode;
4190       switch (modifycode)
4191 	{
4192 	case PLUS_EXPR:
4193 	case POINTER_PLUS_EXPR:
4194 	  fncode = (return_old_p
4195 		    ? BUILT_IN_ATOMIC_FETCH_ADD_N
4196 		    : BUILT_IN_ATOMIC_ADD_FETCH_N);
4197 	  break;
4198 	case MINUS_EXPR:
4199 	  fncode = (return_old_p
4200 		    ? BUILT_IN_ATOMIC_FETCH_SUB_N
4201 		    : BUILT_IN_ATOMIC_SUB_FETCH_N);
4202 	  break;
4203 	case BIT_AND_EXPR:
4204 	  fncode = (return_old_p
4205 		    ? BUILT_IN_ATOMIC_FETCH_AND_N
4206 		    : BUILT_IN_ATOMIC_AND_FETCH_N);
4207 	  break;
4208 	case BIT_IOR_EXPR:
4209 	  fncode = (return_old_p
4210 		    ? BUILT_IN_ATOMIC_FETCH_OR_N
4211 		    : BUILT_IN_ATOMIC_OR_FETCH_N);
4212 	  break;
4213 	case BIT_XOR_EXPR:
4214 	  fncode = (return_old_p
4215 		    ? BUILT_IN_ATOMIC_FETCH_XOR_N
4216 		    : BUILT_IN_ATOMIC_XOR_FETCH_N);
4217 	  break;
4218 	default:
4219 	  goto cas_loop;
4220 	}
4221 
4222       /* We can only use "_1" through "_16" variants of the atomic fetch
4223 	 built-ins.  */
4224       unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4225       if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4226 	goto cas_loop;
4227 
4228       /* If this is a pointer type, we need to multiply by the size of
4229 	 the pointer target type.  */
4230       if (POINTER_TYPE_P (lhs_type))
4231 	{
4232 	  if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4233 	      /* ??? This would introduce -Wdiscarded-qualifiers
4234 		 warning: __atomic_fetch_* expect volatile void *
4235 		 type as the first argument.  (Assignments between
4236 		 atomic and non-atomic objects are OK.) */
4237 	      || TYPE_RESTRICT (lhs_type))
4238 	    goto cas_loop;
4239 	  tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4240 	  rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4241 				 convert (ptrdiff_type_node, rhs),
4242 				 convert (ptrdiff_type_node, sz));
4243 	}
4244 
4245       /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4246 	 __atomic_*_fetch (&lhs, &val, SEQ_CST).  */
4247       fndecl = builtin_decl_explicit (fncode);
4248       params->quick_push (lhs_addr);
4249       params->quick_push (rhs);
4250       params->quick_push (seq_cst);
4251       func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4252 
4253       newval = create_tmp_var_raw (nonatomic_lhs_type);
4254       TREE_ADDRESSABLE (newval) = 1;
4255       TREE_NO_WARNING (newval) = 1;
4256       rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4257 		    NULL_TREE, NULL_TREE);
4258       SET_EXPR_LOCATION (rhs, loc);
4259       add_stmt (rhs);
4260 
4261       /* Finish the compound statement.  */
4262       compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4263 
4264       /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4265 	 the statement and that value.  */
4266       return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4267     }
4268 
4269 cas_loop:
4270   /* Create the variables and labels required for the op= form.  */
4271   old = create_tmp_var_raw (nonatomic_lhs_type);
4272   old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
4273   TREE_ADDRESSABLE (old) = 1;
4274   TREE_NO_WARNING (old) = 1;
4275 
4276   newval = create_tmp_var_raw (nonatomic_lhs_type);
4277   newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
4278   TREE_ADDRESSABLE (newval) = 1;
4279   TREE_NO_WARNING (newval) = 1;
4280 
4281   loop_decl = create_artificial_label (loc);
4282   loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4283 
4284   done_decl = create_artificial_label (loc);
4285   done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4286 
4287   /* __atomic_load (addr, &old, SEQ_CST).  */
4288   fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4289   params->quick_push (lhs_addr);
4290   params->quick_push (old_addr);
4291   params->quick_push (seq_cst);
4292   func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4293   old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4294 		NULL_TREE);
4295   add_stmt (old);
4296   params->truncate (0);
4297 
4298   /* Create the expressions for floating-point environment
4299      manipulation, if required.  */
4300   bool need_fenv = (flag_trapping_math
4301 		    && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4302   tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4303   if (need_fenv)
4304     targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4305 
4306   if (hold_call)
4307     add_stmt (hold_call);
4308 
4309   /* loop:  */
4310   add_stmt (loop_label);
4311 
4312   /* newval = old + val;  */
4313   if (rhs_type != rhs_semantic_type)
4314     val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
4315   rhs = build_binary_op (loc, modifycode, old, val, true);
4316   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4317     {
4318       tree eptype = TREE_TYPE (rhs);
4319       rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4320       rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4321     }
4322   else
4323     rhs = c_fully_fold (rhs, false, NULL);
4324   rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4325 				rhs, NULL_TREE, ic_assign, false, NULL_TREE,
4326 				NULL_TREE, 0);
4327   if (rhs != error_mark_node)
4328     {
4329       rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4330 		    NULL_TREE);
4331       SET_EXPR_LOCATION (rhs, loc);
4332       add_stmt (rhs);
4333     }
4334 
4335   /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4336        goto done;  */
4337   fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4338   params->quick_push (lhs_addr);
4339   params->quick_push (old_addr);
4340   params->quick_push (newval_addr);
4341   params->quick_push (integer_zero_node);
4342   params->quick_push (seq_cst);
4343   params->quick_push (seq_cst);
4344   func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4345 
4346   goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4347   SET_EXPR_LOCATION (goto_stmt, loc);
4348 
4349   stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4350   SET_EXPR_LOCATION (stmt, loc);
4351   add_stmt (stmt);
4352 
4353   if (clear_call)
4354     add_stmt (clear_call);
4355 
4356   /* goto loop;  */
4357   goto_stmt  = build1 (GOTO_EXPR, void_type_node, loop_decl);
4358   SET_EXPR_LOCATION (goto_stmt, loc);
4359   add_stmt (goto_stmt);
4360 
4361   /* done:  */
4362   add_stmt (done_label);
4363 
4364   if (update_call)
4365     add_stmt (update_call);
4366 
4367   /* Finish the compound statement.  */
4368   compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4369 
4370   /* NEWVAL is the value that was successfully stored, return a
4371      COMPOUND_EXPR of the statement and the appropriate value.  */
4372   return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4373 		 return_old_p ? old : newval);
4374 }
4375 
4376 /* Construct and perhaps optimize a tree representation
4377    for a unary operation.  CODE, a tree_code, specifies the operation
4378    and XARG is the operand.
4379    For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4380    promotions (such as from short to int).
4381    For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4382    non-lvalues; this is only used to handle conversion of non-lvalue arrays
4383    to pointers in C99.
4384 
4385    LOCATION is the location of the operator.  */
4386 
4387 tree
build_unary_op(location_t location,enum tree_code code,tree xarg,bool noconvert)4388 build_unary_op (location_t location, enum tree_code code, tree xarg,
4389 		bool noconvert)
4390 {
4391   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
4392   tree arg = xarg;
4393   tree argtype = NULL_TREE;
4394   enum tree_code typecode;
4395   tree val;
4396   tree ret = error_mark_node;
4397   tree eptype = NULL_TREE;
4398   const char *invalid_op_diag;
4399   bool int_operands;
4400 
4401   int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4402   if (int_operands)
4403     arg = remove_c_maybe_const_expr (arg);
4404 
4405   if (code != ADDR_EXPR)
4406     arg = require_complete_type (location, arg);
4407 
4408   typecode = TREE_CODE (TREE_TYPE (arg));
4409   if (typecode == ERROR_MARK)
4410     return error_mark_node;
4411   if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4412     typecode = INTEGER_TYPE;
4413 
4414   if ((invalid_op_diag
4415        = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4416     {
4417       error_at (location, invalid_op_diag);
4418       return error_mark_node;
4419     }
4420 
4421   if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4422     {
4423       eptype = TREE_TYPE (arg);
4424       arg = TREE_OPERAND (arg, 0);
4425     }
4426 
4427   switch (code)
4428     {
4429     case CONVERT_EXPR:
4430       /* This is used for unary plus, because a CONVERT_EXPR
4431 	 is enough to prevent anybody from looking inside for
4432 	 associativity, but won't generate any code.  */
4433       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4434 	    || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4435 	    || gnu_vector_type_p (TREE_TYPE (arg))))
4436 	{
4437 	  error_at (location, "wrong type argument to unary plus");
4438 	  return error_mark_node;
4439 	}
4440       else if (!noconvert)
4441 	arg = default_conversion (arg);
4442       arg = non_lvalue_loc (location, arg);
4443       break;
4444 
4445     case NEGATE_EXPR:
4446       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4447 	    || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
4448 	    || gnu_vector_type_p (TREE_TYPE (arg))))
4449 	{
4450 	  error_at (location, "wrong type argument to unary minus");
4451 	  return error_mark_node;
4452 	}
4453       else if (!noconvert)
4454 	arg = default_conversion (arg);
4455       break;
4456 
4457     case BIT_NOT_EXPR:
4458       /* ~ works on integer types and non float vectors. */
4459       if (typecode == INTEGER_TYPE
4460 	  || (gnu_vector_type_p (TREE_TYPE (arg))
4461 	      && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
4462 	{
4463 	  tree e = arg;
4464 
4465 	  /* Warn if the expression has boolean value.  */
4466 	  while (TREE_CODE (e) == COMPOUND_EXPR)
4467 	    e = TREE_OPERAND (e, 1);
4468 
4469 	  if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4470 	       || truth_value_p (TREE_CODE (e))))
4471 	    {
4472 	      auto_diagnostic_group d;
4473 	      if (warning_at (location, OPT_Wbool_operation,
4474 				"%<~%> on a boolean expression"))
4475 		{
4476 		  gcc_rich_location richloc (location);
4477 		  richloc.add_fixit_insert_before (location, "!");
4478 		  inform (&richloc, "did you mean to use logical not?");
4479 		}
4480 	    }
4481 	  if (!noconvert)
4482 	    arg = default_conversion (arg);
4483 	}
4484       else if (typecode == COMPLEX_TYPE)
4485 	{
4486 	  code = CONJ_EXPR;
4487 	  pedwarn (location, OPT_Wpedantic,
4488 		   "ISO C does not support %<~%> for complex conjugation");
4489 	  if (!noconvert)
4490 	    arg = default_conversion (arg);
4491 	}
4492       else
4493 	{
4494 	  error_at (location, "wrong type argument to bit-complement");
4495 	  return error_mark_node;
4496 	}
4497       break;
4498 
4499     case ABS_EXPR:
4500       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
4501 	{
4502 	  error_at (location, "wrong type argument to abs");
4503 	  return error_mark_node;
4504 	}
4505       else if (!noconvert)
4506 	arg = default_conversion (arg);
4507       break;
4508 
4509     case ABSU_EXPR:
4510       if (!(typecode == INTEGER_TYPE))
4511 	{
4512 	  error_at (location, "wrong type argument to absu");
4513 	  return error_mark_node;
4514 	}
4515       else if (!noconvert)
4516 	arg = default_conversion (arg);
4517       break;
4518 
4519     case CONJ_EXPR:
4520       /* Conjugating a real value is a no-op, but allow it anyway.  */
4521       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4522 	    || typecode == COMPLEX_TYPE))
4523 	{
4524 	  error_at (location, "wrong type argument to conjugation");
4525 	  return error_mark_node;
4526 	}
4527       else if (!noconvert)
4528 	arg = default_conversion (arg);
4529       break;
4530 
4531     case TRUTH_NOT_EXPR:
4532       if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
4533 	  && typecode != REAL_TYPE && typecode != POINTER_TYPE
4534 	  && typecode != COMPLEX_TYPE)
4535 	{
4536 	  error_at (location,
4537 		    "wrong type argument to unary exclamation mark");
4538 	  return error_mark_node;
4539 	}
4540       if (int_operands)
4541 	{
4542 	  arg = c_objc_common_truthvalue_conversion (location, xarg);
4543 	  arg = remove_c_maybe_const_expr (arg);
4544 	}
4545       else
4546 	arg = c_objc_common_truthvalue_conversion (location, arg);
4547       ret = invert_truthvalue_loc (location, arg);
4548       /* If the TRUTH_NOT_EXPR has been folded, reset the location.  */
4549       if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4550 	location = EXPR_LOCATION (ret);
4551       goto return_build_unary_op;
4552 
4553     case REALPART_EXPR:
4554     case IMAGPART_EXPR:
4555       ret = build_real_imag_expr (location, code, arg);
4556       if (ret == error_mark_node)
4557 	return error_mark_node;
4558       if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4559 	eptype = TREE_TYPE (eptype);
4560       goto return_build_unary_op;
4561 
4562     case PREINCREMENT_EXPR:
4563     case POSTINCREMENT_EXPR:
4564     case PREDECREMENT_EXPR:
4565     case POSTDECREMENT_EXPR:
4566 
4567       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4568 	{
4569 	  tree inner = build_unary_op (location, code,
4570 				       C_MAYBE_CONST_EXPR_EXPR (arg),
4571 				       noconvert);
4572 	  if (inner == error_mark_node)
4573 	    return error_mark_node;
4574 	  ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4575 			C_MAYBE_CONST_EXPR_PRE (arg), inner);
4576 	  gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4577 	  C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4578 	  goto return_build_unary_op;
4579 	}
4580 
4581       /* Complain about anything that is not a true lvalue.  In
4582 	 Objective-C, skip this check for property_refs.  */
4583       if (!objc_is_property_ref (arg)
4584 	  && !lvalue_or_else (location,
4585 			      arg, ((code == PREINCREMENT_EXPR
4586 				     || code == POSTINCREMENT_EXPR)
4587 				    ? lv_increment
4588 				    : lv_decrement)))
4589 	return error_mark_node;
4590 
4591       if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4592 	{
4593 	  if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4594 	    warning_at (location, OPT_Wc___compat,
4595 			"increment of enumeration value is invalid in C++");
4596 	  else
4597 	    warning_at (location, OPT_Wc___compat,
4598 			"decrement of enumeration value is invalid in C++");
4599 	}
4600 
4601       if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4602 	{
4603 	  if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4604 	    warning_at (location, OPT_Wbool_operation,
4605 			"increment of a boolean expression");
4606 	  else
4607 	    warning_at (location, OPT_Wbool_operation,
4608 			"decrement of a boolean expression");
4609 	}
4610 
4611       /* Ensure the argument is fully folded inside any SAVE_EXPR.  */
4612       arg = c_fully_fold (arg, false, NULL, true);
4613 
4614       bool atomic_op;
4615       atomic_op = really_atomic_lvalue (arg);
4616 
4617       /* Increment or decrement the real part of the value,
4618 	 and don't change the imaginary part.  */
4619       if (typecode == COMPLEX_TYPE)
4620 	{
4621 	  tree real, imag;
4622 
4623 	  pedwarn (location, OPT_Wpedantic,
4624 		   "ISO C does not support %<++%> and %<--%> on complex types");
4625 
4626 	  if (!atomic_op)
4627 	    {
4628 	      arg = stabilize_reference (arg);
4629 	      real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4630 				     true);
4631 	      imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4632 				     true);
4633 	      real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
4634 	      if (real == error_mark_node || imag == error_mark_node)
4635 		return error_mark_node;
4636 	      ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4637 			    real, imag);
4638 	      goto return_build_unary_op;
4639 	    }
4640 	}
4641 
4642       /* Report invalid types.  */
4643 
4644       if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
4645 	  && typecode != INTEGER_TYPE && typecode != REAL_TYPE
4646 	  && typecode != COMPLEX_TYPE
4647 	  && !gnu_vector_type_p (TREE_TYPE (arg)))
4648 	{
4649 	  if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4650 	    error_at (location, "wrong type argument to increment");
4651 	  else
4652 	    error_at (location, "wrong type argument to decrement");
4653 
4654 	  return error_mark_node;
4655 	}
4656 
4657       {
4658 	tree inc;
4659 
4660 	argtype = TREE_TYPE (arg);
4661 
4662 	/* Compute the increment.  */
4663 
4664 	if (typecode == POINTER_TYPE)
4665 	  {
4666 	    /* If pointer target is an incomplete type,
4667 	       we just cannot know how to do the arithmetic.  */
4668 	    if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
4669 	      {
4670 		if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4671 		  error_at (location,
4672 			    "increment of pointer to an incomplete type %qT",
4673 			    TREE_TYPE (argtype));
4674 		else
4675 		  error_at (location,
4676 			    "decrement of pointer to an incomplete type %qT",
4677 			    TREE_TYPE (argtype));
4678 	      }
4679 	    else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4680 		     || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
4681 	      {
4682 		if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4683 		  pedwarn (location, OPT_Wpointer_arith,
4684 			   "wrong type argument to increment");
4685 		else
4686 		  pedwarn (location, OPT_Wpointer_arith,
4687 			   "wrong type argument to decrement");
4688 	      }
4689 	    else
4690 	      verify_type_context (location, TCTX_POINTER_ARITH,
4691 				   TREE_TYPE (argtype));
4692 
4693 	    inc = c_size_in_bytes (TREE_TYPE (argtype));
4694 	    inc = convert_to_ptrofftype_loc (location, inc);
4695 	  }
4696 	else if (FRACT_MODE_P (TYPE_MODE (argtype)))
4697 	  {
4698 	    /* For signed fract types, we invert ++ to -- or
4699 	       -- to ++, and change inc from 1 to -1, because
4700 	       it is not possible to represent 1 in signed fract constants.
4701 	       For unsigned fract types, the result always overflows and
4702 	       we get an undefined (original) or the maximum value.  */
4703 	    if (code == PREINCREMENT_EXPR)
4704 	      code = PREDECREMENT_EXPR;
4705 	    else if (code == PREDECREMENT_EXPR)
4706 	      code = PREINCREMENT_EXPR;
4707 	    else if (code == POSTINCREMENT_EXPR)
4708 	      code = POSTDECREMENT_EXPR;
4709 	    else /* code == POSTDECREMENT_EXPR  */
4710 	      code = POSTINCREMENT_EXPR;
4711 
4712 	    inc = integer_minus_one_node;
4713 	    inc = convert (argtype, inc);
4714 	  }
4715 	else
4716 	  {
4717 	    inc = VECTOR_TYPE_P (argtype)
4718 	      ? build_one_cst (argtype)
4719 	      : integer_one_node;
4720 	    inc = convert (argtype, inc);
4721 	  }
4722 
4723 	/* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4724 	   need to ask Objective-C to build the increment or decrement
4725 	   expression for it.  */
4726 	if (objc_is_property_ref (arg))
4727 	  return objc_build_incr_expr_for_property_ref (location, code,
4728 							arg, inc);
4729 
4730 	/* Report a read-only lvalue.  */
4731 	if (TYPE_READONLY (argtype))
4732 	  {
4733 	    readonly_error (location, arg,
4734 			    ((code == PREINCREMENT_EXPR
4735 			      || code == POSTINCREMENT_EXPR)
4736 			     ? lv_increment : lv_decrement));
4737 	    return error_mark_node;
4738 	  }
4739 	else if (TREE_READONLY (arg))
4740 	  readonly_warning (arg,
4741 			    ((code == PREINCREMENT_EXPR
4742 			      || code == POSTINCREMENT_EXPR)
4743 			     ? lv_increment : lv_decrement));
4744 
4745 	/* If the argument is atomic, use the special code sequences for
4746 	   atomic compound assignment.  */
4747 	if (atomic_op)
4748 	  {
4749 	    arg = stabilize_reference (arg);
4750 	    ret = build_atomic_assign (location, arg,
4751 				       ((code == PREINCREMENT_EXPR
4752 					 || code == POSTINCREMENT_EXPR)
4753 					? PLUS_EXPR
4754 					: MINUS_EXPR),
4755 				       (FRACT_MODE_P (TYPE_MODE (argtype))
4756 					? inc
4757 					: integer_one_node),
4758 				       (code == POSTINCREMENT_EXPR
4759 					|| code == POSTDECREMENT_EXPR));
4760 	    goto return_build_unary_op;
4761 	  }
4762 
4763 	if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4764 	  val = boolean_increment (code, arg);
4765 	else
4766 	  val = build2 (code, TREE_TYPE (arg), arg, inc);
4767 	TREE_SIDE_EFFECTS (val) = 1;
4768 	if (TREE_CODE (val) != code)
4769 	  TREE_NO_WARNING (val) = 1;
4770 	ret = val;
4771 	goto return_build_unary_op;
4772       }
4773 
4774     case ADDR_EXPR:
4775       /* Note that this operation never does default_conversion.  */
4776 
4777       /* The operand of unary '&' must be an lvalue (which excludes
4778 	 expressions of type void), or, in C99, the result of a [] or
4779 	 unary '*' operator.  */
4780       if (VOID_TYPE_P (TREE_TYPE (arg))
4781 	  && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4782 	  && (!INDIRECT_REF_P (arg) || !flag_isoc99))
4783 	pedwarn (location, 0, "taking address of expression of type %<void%>");
4784 
4785       /* Let &* cancel out to simplify resulting code.  */
4786       if (INDIRECT_REF_P (arg))
4787 	{
4788 	  /* Don't let this be an lvalue.  */
4789 	  if (lvalue_p (TREE_OPERAND (arg, 0)))
4790 	    return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
4791 	  ret = TREE_OPERAND (arg, 0);
4792 	  goto return_build_unary_op;
4793 	}
4794 
4795       /* Anything not already handled and not a true memory reference
4796 	 or a non-lvalue array is an error.  */
4797       if (typecode != FUNCTION_TYPE && !noconvert
4798 	  && !lvalue_or_else (location, arg, lv_addressof))
4799 	return error_mark_node;
4800 
4801       /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4802 	 folding later.  */
4803       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4804 	{
4805 	  tree inner = build_unary_op (location, code,
4806 				       C_MAYBE_CONST_EXPR_EXPR (arg),
4807 				       noconvert);
4808 	  ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4809 			C_MAYBE_CONST_EXPR_PRE (arg), inner);
4810 	  gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4811 	  C_MAYBE_CONST_EXPR_NON_CONST (ret)
4812 	    = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4813 	  goto return_build_unary_op;
4814 	}
4815 
4816       /* Ordinary case; arg is a COMPONENT_REF or a decl.  */
4817       argtype = TREE_TYPE (arg);
4818 
4819       /* If the lvalue is const or volatile, merge that into the type
4820 	 to which the address will point.  This is only needed
4821 	 for function types.  */
4822       if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
4823 	  && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4824 	  && TREE_CODE (argtype) == FUNCTION_TYPE)
4825 	{
4826 	  int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4827 	  int quals = orig_quals;
4828 
4829 	  if (TREE_READONLY (arg))
4830 	    quals |= TYPE_QUAL_CONST;
4831 	  if (TREE_THIS_VOLATILE (arg))
4832 	    quals |= TYPE_QUAL_VOLATILE;
4833 
4834 	  argtype = c_build_qualified_type (argtype, quals);
4835 	}
4836 
4837       switch (TREE_CODE (arg))
4838 	{
4839 	case COMPONENT_REF:
4840 	  if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4841 	    {
4842 	      error_at (location, "cannot take address of bit-field %qD",
4843 			TREE_OPERAND (arg, 1));
4844 	      return error_mark_node;
4845 	    }
4846 
4847 	  /* fall through */
4848 
4849 	case ARRAY_REF:
4850 	  if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4851 	    {
4852 	      if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4853 		  && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4854 		{
4855 		  error_at (location, "cannot take address of scalar with "
4856 			    "reverse storage order");
4857 		  return error_mark_node;
4858 		}
4859 
4860 	      if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4861 		  && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4862 		warning_at (location, OPT_Wscalar_storage_order,
4863 			    "address of array with reverse scalar storage "
4864 			    "order requested");
4865 	    }
4866 
4867 	default:
4868 	  break;
4869 	}
4870 
4871       if (!c_mark_addressable (arg))
4872 	return error_mark_node;
4873 
4874       gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4875 		  || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
4876 
4877       argtype = build_pointer_type (argtype);
4878 
4879       /* ??? Cope with user tricks that amount to offsetof.  Delete this
4880 	 when we have proper support for integer constant expressions.  */
4881       val = get_base_address (arg);
4882       if (val && INDIRECT_REF_P (val)
4883           && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4884 	{
4885 	  ret = fold_offsetof (arg, argtype);
4886 	  goto return_build_unary_op;
4887 	}
4888 
4889       val = build1 (ADDR_EXPR, argtype, arg);
4890 
4891       ret = val;
4892       goto return_build_unary_op;
4893 
4894     default:
4895       gcc_unreachable ();
4896     }
4897 
4898   if (argtype == NULL_TREE)
4899     argtype = TREE_TYPE (arg);
4900   if (TREE_CODE (arg) == INTEGER_CST)
4901     ret = (require_constant_value
4902 	   ? fold_build1_initializer_loc (location, code, argtype, arg)
4903 	   : fold_build1_loc (location, code, argtype, arg));
4904   else
4905     ret = build1 (code, argtype, arg);
4906  return_build_unary_op:
4907   gcc_assert (ret != error_mark_node);
4908   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4909       && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4910     ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4911   else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4912     ret = note_integer_operands (ret);
4913   if (eptype)
4914     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4915   protected_set_expr_location (ret, location);
4916   return ret;
4917 }
4918 
4919 /* Return nonzero if REF is an lvalue valid for this language.
4920    Lvalues can be assigned, unless their type has TYPE_READONLY.
4921    Lvalues can have their address taken, unless they have C_DECL_REGISTER.  */
4922 
4923 bool
lvalue_p(const_tree ref)4924 lvalue_p (const_tree ref)
4925 {
4926   const enum tree_code code = TREE_CODE (ref);
4927 
4928   switch (code)
4929     {
4930     case REALPART_EXPR:
4931     case IMAGPART_EXPR:
4932     case COMPONENT_REF:
4933       return lvalue_p (TREE_OPERAND (ref, 0));
4934 
4935     case C_MAYBE_CONST_EXPR:
4936       return lvalue_p (TREE_OPERAND (ref, 1));
4937 
4938     case COMPOUND_LITERAL_EXPR:
4939     case STRING_CST:
4940       return true;
4941 
4942     case INDIRECT_REF:
4943     case ARRAY_REF:
4944     case VAR_DECL:
4945     case PARM_DECL:
4946     case RESULT_DECL:
4947     case ERROR_MARK:
4948       return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4949 	      && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
4950 
4951     case BIND_EXPR:
4952       return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
4953 
4954     default:
4955       return false;
4956     }
4957 }
4958 
4959 /* Give a warning for storing in something that is read-only in GCC
4960    terms but not const in ISO C terms.  */
4961 
4962 static void
readonly_warning(tree arg,enum lvalue_use use)4963 readonly_warning (tree arg, enum lvalue_use use)
4964 {
4965   switch (use)
4966     {
4967     case lv_assign:
4968       warning (0, "assignment of read-only location %qE", arg);
4969       break;
4970     case lv_increment:
4971       warning (0, "increment of read-only location %qE", arg);
4972       break;
4973     case lv_decrement:
4974       warning (0, "decrement of read-only location %qE", arg);
4975       break;
4976     default:
4977       gcc_unreachable ();
4978     }
4979   return;
4980 }
4981 
4982 
4983 /* Return nonzero if REF is an lvalue valid for this language;
4984    otherwise, print an error message and return zero.  USE says
4985    how the lvalue is being used and so selects the error message.
4986    LOCATION is the location at which any error should be reported.  */
4987 
4988 static int
lvalue_or_else(location_t loc,const_tree ref,enum lvalue_use use)4989 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
4990 {
4991   int win = lvalue_p (ref);
4992 
4993   if (!win)
4994     lvalue_error (loc, use);
4995 
4996   return win;
4997 }
4998 
4999 /* Mark EXP saying that we need to be able to take the
5000    address of it; it should not be allocated in a register.
5001    Returns true if successful.  ARRAY_REF_P is true if this
5002    is for ARRAY_REF construction - in that case we don't want
5003    to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
5004    it is fine to use ARRAY_REFs for vector subscripts on vector
5005    register variables.  */
5006 
5007 bool
c_mark_addressable(tree exp,bool array_ref_p)5008 c_mark_addressable (tree exp, bool array_ref_p)
5009 {
5010   tree x = exp;
5011 
5012   while (1)
5013     switch (TREE_CODE (x))
5014       {
5015       case VIEW_CONVERT_EXPR:
5016 	if (array_ref_p
5017 	    && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5018 	    && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
5019 	  return true;
5020 	x = TREE_OPERAND (x, 0);
5021 	break;
5022 
5023       case COMPONENT_REF:
5024 	if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
5025 	  {
5026 	    error ("cannot take address of bit-field %qD",
5027 		   TREE_OPERAND (x, 1));
5028 	    return false;
5029 	  }
5030 	/* FALLTHRU */
5031       case ADDR_EXPR:
5032       case ARRAY_REF:
5033       case REALPART_EXPR:
5034       case IMAGPART_EXPR:
5035 	x = TREE_OPERAND (x, 0);
5036 	break;
5037 
5038       case COMPOUND_LITERAL_EXPR:
5039 	TREE_ADDRESSABLE (x) = 1;
5040 	TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
5041 	return true;
5042 
5043       case CONSTRUCTOR:
5044 	TREE_ADDRESSABLE (x) = 1;
5045 	return true;
5046 
5047       case VAR_DECL:
5048       case CONST_DECL:
5049       case PARM_DECL:
5050       case RESULT_DECL:
5051 	if (C_DECL_REGISTER (x)
5052 	    && DECL_NONLOCAL (x))
5053 	  {
5054 	    if (TREE_PUBLIC (x) || is_global_var (x))
5055 	      {
5056 		error
5057 		  ("global register variable %qD used in nested function", x);
5058 		return false;
5059 	      }
5060 	    pedwarn (input_location, 0, "register variable %qD used in nested function", x);
5061 	  }
5062 	else if (C_DECL_REGISTER (x))
5063 	  {
5064 	    if (TREE_PUBLIC (x) || is_global_var (x))
5065 	      error ("address of global register variable %qD requested", x);
5066 	    else
5067 	      error ("address of register variable %qD requested", x);
5068 	    return false;
5069 	  }
5070 
5071 	/* FALLTHRU */
5072       case FUNCTION_DECL:
5073 	TREE_ADDRESSABLE (x) = 1;
5074 	/* FALLTHRU */
5075       default:
5076 	return true;
5077     }
5078 }
5079 
5080 /* Convert EXPR to TYPE, warning about conversion problems with
5081    constants.  SEMANTIC_TYPE is the type this conversion would use
5082    without excess precision. If SEMANTIC_TYPE is NULL, this function
5083    is equivalent to convert_and_check. This function is a wrapper that
5084    handles conversions that may be different than
5085    the usual ones because of excess precision.  */
5086 
5087 static tree
ep_convert_and_check(location_t loc,tree type,tree expr,tree semantic_type)5088 ep_convert_and_check (location_t loc, tree type, tree expr,
5089 		      tree semantic_type)
5090 {
5091   if (TREE_TYPE (expr) == type)
5092     return expr;
5093 
5094   /* For C11, integer conversions may have results with excess
5095      precision.  */
5096   if (flag_isoc11 || !semantic_type)
5097     return convert_and_check (loc, type, expr);
5098 
5099   if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
5100       && TREE_TYPE (expr) != semantic_type)
5101     {
5102       /* For integers, we need to check the real conversion, not
5103 	 the conversion to the excess precision type.  */
5104       expr = convert_and_check (loc, semantic_type, expr);
5105     }
5106   /* Result type is the excess precision type, which should be
5107      large enough, so do not check.  */
5108   return convert (type, expr);
5109 }
5110 
5111 /* If EXPR refers to a built-in declared without a prototype returns
5112    the actual type of the built-in and, if non-null, set *BLTIN to
5113    a pointer to the built-in.  Otherwise return the type of EXPR
5114    and clear *BLTIN if non-null.  */
5115 
5116 static tree
5117 type_or_builtin_type (tree expr, tree *bltin = NULL)
5118 {
5119   tree dummy;
5120   if (!bltin)
5121     bltin = &dummy;
5122 
5123   *bltin = NULL_TREE;
5124 
5125   tree type = TREE_TYPE (expr);
5126   if (TREE_CODE (expr) != ADDR_EXPR)
5127     return type;
5128 
5129   tree oper = TREE_OPERAND (expr, 0);
5130   if (!DECL_P (oper)
5131       || TREE_CODE (oper) != FUNCTION_DECL
5132       || !fndecl_built_in_p (oper, BUILT_IN_NORMAL))
5133     return type;
5134 
5135   built_in_function code = DECL_FUNCTION_CODE (oper);
5136   if (!C_DECL_BUILTIN_PROTOTYPE (oper))
5137     return type;
5138 
5139   if ((*bltin = builtin_decl_implicit (code)))
5140     type = build_pointer_type (TREE_TYPE (*bltin));
5141 
5142   return type;
5143 }
5144 
5145 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  If
5146    IFEXP_BCP then the condition is a call to __builtin_constant_p, and
5147    if folded to an integer constant then the unselected half may
5148    contain arbitrary operations not normally permitted in constant
5149    expressions.  Set the location of the expression to LOC.  */
5150 
5151 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)5152 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
5153 			tree op1, tree op1_original_type, location_t op1_loc,
5154 			tree op2, tree op2_original_type, location_t op2_loc)
5155 {
5156   tree type1;
5157   tree type2;
5158   enum tree_code code1;
5159   enum tree_code code2;
5160   tree result_type = NULL;
5161   tree semantic_result_type = NULL;
5162   tree orig_op1 = op1, orig_op2 = op2;
5163   bool int_const, op1_int_operands, op2_int_operands, int_operands;
5164   bool ifexp_int_operands;
5165   tree ret;
5166 
5167   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
5168   if (op1_int_operands)
5169     op1 = remove_c_maybe_const_expr (op1);
5170   op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
5171   if (op2_int_operands)
5172     op2 = remove_c_maybe_const_expr (op2);
5173   ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
5174   if (ifexp_int_operands)
5175     ifexp = remove_c_maybe_const_expr (ifexp);
5176 
5177   /* Promote both alternatives.  */
5178 
5179   if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
5180     op1 = default_conversion (op1);
5181   if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
5182     op2 = default_conversion (op2);
5183 
5184   if (TREE_CODE (ifexp) == ERROR_MARK
5185       || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
5186       || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
5187     return error_mark_node;
5188 
5189   tree bltin1 = NULL_TREE;
5190   tree bltin2 = NULL_TREE;
5191   type1 = type_or_builtin_type (op1, &bltin1);
5192   code1 = TREE_CODE (type1);
5193   type2 = type_or_builtin_type (op2, &bltin2);
5194   code2 = TREE_CODE (type2);
5195 
5196   if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
5197     return error_mark_node;
5198 
5199   if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
5200     return error_mark_node;
5201 
5202   /* C90 does not permit non-lvalue arrays in conditional expressions.
5203      In C99 they will be pointers by now.  */
5204   if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
5205     {
5206       error_at (colon_loc, "non-lvalue array in conditional expression");
5207       return error_mark_node;
5208     }
5209 
5210   if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
5211        || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5212       && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
5213 	  || code1 == COMPLEX_TYPE)
5214       && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5215 	  || code2 == COMPLEX_TYPE))
5216     {
5217       semantic_result_type = c_common_type (type1, type2);
5218       if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5219 	{
5220 	  op1 = TREE_OPERAND (op1, 0);
5221 	  type1 = TREE_TYPE (op1);
5222 	  gcc_assert (TREE_CODE (type1) == code1);
5223 	}
5224       if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5225 	{
5226 	  op2 = TREE_OPERAND (op2, 0);
5227 	  type2 = TREE_TYPE (op2);
5228 	  gcc_assert (TREE_CODE (type2) == code2);
5229 	}
5230     }
5231 
5232   if (warn_cxx_compat)
5233     {
5234       tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5235       tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5236 
5237       if (TREE_CODE (t1) == ENUMERAL_TYPE
5238 	  && TREE_CODE (t2) == ENUMERAL_TYPE
5239 	  && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5240 	warning_at (colon_loc, OPT_Wc___compat,
5241 		    ("different enum types in conditional is "
5242 		     "invalid in C++: %qT vs %qT"),
5243 		    t1, t2);
5244     }
5245 
5246   /* Quickly detect the usual case where op1 and op2 have the same type
5247      after promotion.  */
5248   if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5249     {
5250       if (type1 == type2)
5251 	result_type = type1;
5252       else
5253 	result_type = TYPE_MAIN_VARIANT (type1);
5254     }
5255   else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
5256 	    || code1 == COMPLEX_TYPE)
5257 	   && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5258 	       || code2 == COMPLEX_TYPE))
5259     {
5260       /* In C11, a conditional expression between a floating-point
5261 	 type and an integer type should convert the integer type to
5262 	 the evaluation format of the floating-point type, with
5263 	 possible excess precision.  */
5264       tree eptype1 = type1;
5265       tree eptype2 = type2;
5266       if (flag_isoc11)
5267 	{
5268 	  tree eptype;
5269 	  if (ANY_INTEGRAL_TYPE_P (type1)
5270 	      && (eptype = excess_precision_type (type2)) != NULL_TREE)
5271 	    {
5272 	      eptype2 = eptype;
5273 	      if (!semantic_result_type)
5274 		semantic_result_type = c_common_type (type1, type2);
5275 	    }
5276 	  else if (ANY_INTEGRAL_TYPE_P (type2)
5277 		   && (eptype = excess_precision_type (type1)) != NULL_TREE)
5278 	    {
5279 	      eptype1 = eptype;
5280 	      if (!semantic_result_type)
5281 		semantic_result_type = c_common_type (type1, type2);
5282 	    }
5283 	}
5284       result_type = c_common_type (eptype1, eptype2);
5285       if (result_type == error_mark_node)
5286 	return error_mark_node;
5287       do_warn_double_promotion (result_type, type1, type2,
5288 				"implicit conversion from %qT to %qT to "
5289 				"match other result of conditional",
5290 				colon_loc);
5291 
5292       /* If -Wsign-compare, warn here if type1 and type2 have
5293 	 different signedness.  We'll promote the signed to unsigned
5294 	 and later code won't know it used to be different.
5295 	 Do this check on the original types, so that explicit casts
5296 	 will be considered, but default promotions won't.  */
5297       if (c_inhibit_evaluation_warnings == 0)
5298 	{
5299 	  int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5300 	  int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
5301 
5302 	  if (unsigned_op1 ^ unsigned_op2)
5303 	    {
5304 	      bool ovf;
5305 
5306 	      /* Do not warn if the result type is signed, since the
5307 		 signed type will only be chosen if it can represent
5308 		 all the values of the unsigned type.  */
5309 	      if (!TYPE_UNSIGNED (result_type))
5310 		/* OK */;
5311 	      else
5312 		{
5313 		  bool op1_maybe_const = true;
5314 		  bool op2_maybe_const = true;
5315 
5316 		  /* Do not warn if the signed quantity is an
5317 		     unsuffixed integer literal (or some static
5318 		     constant expression involving such literals) and
5319 		     it is non-negative.  This warning requires the
5320 		     operands to be folded for best results, so do
5321 		     that folding in this case even without
5322 		     warn_sign_compare to avoid warning options
5323 		     possibly affecting code generation.  */
5324 		  c_inhibit_evaluation_warnings
5325 		    += (ifexp == truthvalue_false_node);
5326 		  op1 = c_fully_fold (op1, require_constant_value,
5327 				      &op1_maybe_const);
5328 		  c_inhibit_evaluation_warnings
5329 		    -= (ifexp == truthvalue_false_node);
5330 
5331 		  c_inhibit_evaluation_warnings
5332 		    += (ifexp == truthvalue_true_node);
5333 		  op2 = c_fully_fold (op2, require_constant_value,
5334 				      &op2_maybe_const);
5335 		  c_inhibit_evaluation_warnings
5336 		    -= (ifexp == truthvalue_true_node);
5337 
5338 		  if (warn_sign_compare)
5339 		    {
5340 		      if ((unsigned_op2
5341 			   && tree_expr_nonnegative_warnv_p (op1, &ovf))
5342 			  || (unsigned_op1
5343 			      && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5344 			/* OK */;
5345 		      else if (unsigned_op2)
5346 			warning_at (op1_loc, OPT_Wsign_compare,
5347 				    "operand of %<?:%> changes signedness from "
5348 				    "%qT to %qT due to unsignedness of other "
5349 				    "operand", TREE_TYPE (orig_op1),
5350 				    TREE_TYPE (orig_op2));
5351 		      else
5352 			warning_at (op2_loc, OPT_Wsign_compare,
5353 				    "operand of %<?:%> changes signedness from "
5354 				    "%qT to %qT due to unsignedness of other "
5355 				    "operand", TREE_TYPE (orig_op2),
5356 				    TREE_TYPE (orig_op1));
5357 		    }
5358 		  if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
5359 		    op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
5360 		  if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
5361 		    op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
5362 		}
5363 	    }
5364 	}
5365     }
5366   else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5367     {
5368       if (code1 != VOID_TYPE || code2 != VOID_TYPE)
5369 	pedwarn (colon_loc, OPT_Wpedantic,
5370 		 "ISO C forbids conditional expr with only one void side");
5371       result_type = void_type_node;
5372     }
5373   else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5374     {
5375       addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5376       addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5377       addr_space_t as_common;
5378 
5379       if (comp_target_types (colon_loc, type1, type2))
5380 	result_type = common_pointer_type (type1, type2);
5381       else if (null_pointer_constant_p (orig_op1))
5382 	result_type = type2;
5383       else if (null_pointer_constant_p (orig_op2))
5384 	result_type = type1;
5385       else if (!addr_space_superset (as1, as2, &as_common))
5386 	{
5387 	  error_at (colon_loc, "pointers to disjoint address spaces "
5388 		    "used in conditional expression");
5389 	  return error_mark_node;
5390 	}
5391       else if (VOID_TYPE_P (TREE_TYPE (type1))
5392 	       && !TYPE_ATOMIC (TREE_TYPE (type1)))
5393 	{
5394 	  if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5395 	      && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5396 		  & ~TYPE_QUALS (TREE_TYPE (type1))))
5397 	    warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5398 			"pointer to array loses qualifier "
5399 			"in conditional expression");
5400 
5401 	  if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
5402 	    pedwarn (colon_loc, OPT_Wpedantic,
5403 		     "ISO C forbids conditional expr between "
5404 		     "%<void *%> and function pointer");
5405 	  result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5406 							  TREE_TYPE (type2)));
5407 	}
5408       else if (VOID_TYPE_P (TREE_TYPE (type2))
5409 	       && !TYPE_ATOMIC (TREE_TYPE (type2)))
5410 	{
5411 	  if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5412 	      && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5413 		  & ~TYPE_QUALS (TREE_TYPE (type2))))
5414 	    warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5415 			"pointer to array loses qualifier "
5416 			"in conditional expression");
5417 
5418 	  if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
5419 	    pedwarn (colon_loc, OPT_Wpedantic,
5420 		     "ISO C forbids conditional expr between "
5421 		     "%<void *%> and function pointer");
5422 	  result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5423 							  TREE_TYPE (type1)));
5424 	}
5425       /* Objective-C pointer comparisons are a bit more lenient.  */
5426       else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5427 	result_type = objc_common_type (type1, type2);
5428       else
5429 	{
5430 	  int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5431 	  if (bltin1 && bltin2)
5432 	    warning_at (colon_loc, OPT_Wincompatible_pointer_types,
5433 			"pointer type mismatch between %qT and %qT "
5434 			"of %qD and %qD in conditional expression",
5435 			type1, type2, bltin1, bltin2);
5436 	  else
5437 	    pedwarn (colon_loc, 0,
5438 		     "pointer type mismatch in conditional expression");
5439 	  result_type = build_pointer_type
5440 			  (build_qualified_type (void_type_node, qual));
5441 	}
5442     }
5443   else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5444     {
5445       if (!null_pointer_constant_p (orig_op2))
5446 	pedwarn (colon_loc, 0,
5447 		 "pointer/integer type mismatch in conditional expression");
5448       else
5449 	{
5450 	  op2 = null_pointer_node;
5451 	}
5452       result_type = type1;
5453     }
5454   else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5455     {
5456       if (!null_pointer_constant_p (orig_op1))
5457 	pedwarn (colon_loc, 0,
5458 		 "pointer/integer type mismatch in conditional expression");
5459       else
5460 	{
5461 	  op1 = null_pointer_node;
5462 	}
5463       result_type = type2;
5464     }
5465 
5466   if (!result_type)
5467     {
5468       if (flag_cond_mismatch)
5469 	result_type = void_type_node;
5470       else
5471 	{
5472 	  error_at (colon_loc, "type mismatch in conditional expression");
5473 	  return error_mark_node;
5474 	}
5475     }
5476 
5477   /* Merge const and volatile flags of the incoming types.  */
5478   result_type
5479     = build_type_variant (result_type,
5480 			  TYPE_READONLY (type1) || TYPE_READONLY (type2),
5481 			  TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
5482 
5483   op1 = ep_convert_and_check (colon_loc, result_type, op1,
5484 			      semantic_result_type);
5485   op2 = ep_convert_and_check (colon_loc, result_type, op2,
5486 			      semantic_result_type);
5487 
5488   if (ifexp_bcp && ifexp == truthvalue_true_node)
5489     {
5490       op2_int_operands = true;
5491       op1 = c_fully_fold (op1, require_constant_value, NULL);
5492     }
5493   if (ifexp_bcp && ifexp == truthvalue_false_node)
5494     {
5495       op1_int_operands = true;
5496       op2 = c_fully_fold (op2, require_constant_value, NULL);
5497     }
5498   int_const = int_operands = (ifexp_int_operands
5499 			      && op1_int_operands
5500 			      && op2_int_operands);
5501   if (int_operands)
5502     {
5503       int_const = ((ifexp == truthvalue_true_node
5504 		    && TREE_CODE (orig_op1) == INTEGER_CST
5505 		    && !TREE_OVERFLOW (orig_op1))
5506 		   || (ifexp == truthvalue_false_node
5507 		       && TREE_CODE (orig_op2) == INTEGER_CST
5508 		       && !TREE_OVERFLOW (orig_op2)));
5509     }
5510 
5511   /* Need to convert condition operand into a vector mask.  */
5512   if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5513     {
5514       tree vectype = TREE_TYPE (ifexp);
5515       tree elem_type = TREE_TYPE (vectype);
5516       tree zero = build_int_cst (elem_type, 0);
5517       tree zero_vec = build_vector_from_val (vectype, zero);
5518       tree cmp_type = truth_type_for (vectype);
5519       ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5520     }
5521 
5522   if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
5523     ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
5524   else
5525     {
5526       if (int_operands)
5527 	{
5528 	  /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5529 	     nested inside of the expression.  */
5530 	  op1 = c_fully_fold (op1, false, NULL);
5531 	  op2 = c_fully_fold (op2, false, NULL);
5532 	}
5533       ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5534       if (int_operands)
5535 	ret = note_integer_operands (ret);
5536     }
5537   if (semantic_result_type)
5538     ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
5539 
5540   protected_set_expr_location (ret, colon_loc);
5541 
5542   /* If the OP1 and OP2 are the same and don't have side-effects,
5543      warn here, because the COND_EXPR will be turned into OP1.  */
5544   if (warn_duplicated_branches
5545       && TREE_CODE (ret) == COND_EXPR
5546       && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5547     warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5548 		"this condition has identical branches");
5549 
5550   return ret;
5551 }
5552 
5553 /* Return a compound expression that performs two expressions and
5554    returns the value of the second of them.
5555 
5556    LOC is the location of the COMPOUND_EXPR.  */
5557 
5558 tree
build_compound_expr(location_t loc,tree expr1,tree expr2)5559 build_compound_expr (location_t loc, tree expr1, tree expr2)
5560 {
5561   bool expr1_int_operands, expr2_int_operands;
5562   tree eptype = NULL_TREE;
5563   tree ret;
5564 
5565   expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5566   if (expr1_int_operands)
5567     expr1 = remove_c_maybe_const_expr (expr1);
5568   expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5569   if (expr2_int_operands)
5570     expr2 = remove_c_maybe_const_expr (expr2);
5571 
5572   if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5573     expr1 = TREE_OPERAND (expr1, 0);
5574   if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5575     {
5576       eptype = TREE_TYPE (expr2);
5577       expr2 = TREE_OPERAND (expr2, 0);
5578     }
5579 
5580   if (!TREE_SIDE_EFFECTS (expr1))
5581     {
5582       /* The left-hand operand of a comma expression is like an expression
5583 	 statement: with -Wunused, we should warn if it doesn't have
5584 	 any side-effects, unless it was explicitly cast to (void).  */
5585       if (warn_unused_value)
5586 	{
5587 	  if (VOID_TYPE_P (TREE_TYPE (expr1))
5588 	      && CONVERT_EXPR_P (expr1))
5589 	    ; /* (void) a, b */
5590 	  else if (VOID_TYPE_P (TREE_TYPE (expr1))
5591 		   && TREE_CODE (expr1) == COMPOUND_EXPR
5592 		   && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
5593 	    ; /* (void) a, (void) b, c */
5594 	  else
5595 	    warning_at (loc, OPT_Wunused_value,
5596 			"left-hand operand of comma expression has no effect");
5597 	}
5598     }
5599   else if (TREE_CODE (expr1) == COMPOUND_EXPR
5600 	   && warn_unused_value)
5601     {
5602       tree r = expr1;
5603       location_t cloc = loc;
5604       while (TREE_CODE (r) == COMPOUND_EXPR)
5605         {
5606 	  if (EXPR_HAS_LOCATION (r))
5607 	    cloc = EXPR_LOCATION (r);
5608 	  r = TREE_OPERAND (r, 1);
5609 	}
5610       if (!TREE_SIDE_EFFECTS (r)
5611 	  && !VOID_TYPE_P (TREE_TYPE (r))
5612 	  && !CONVERT_EXPR_P (r))
5613 	warning_at (cloc, OPT_Wunused_value,
5614 	            "right-hand operand of comma expression has no effect");
5615     }
5616 
5617   /* With -Wunused, we should also warn if the left-hand operand does have
5618      side-effects, but computes a value which is not used.  For example, in
5619      `foo() + bar(), baz()' the result of the `+' operator is not used,
5620      so we should issue a warning.  */
5621   else if (warn_unused_value)
5622     warn_if_unused_value (expr1, loc);
5623 
5624   if (expr2 == error_mark_node)
5625     return error_mark_node;
5626 
5627   ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5628 
5629   if (flag_isoc99
5630       && expr1_int_operands
5631       && expr2_int_operands)
5632     ret = note_integer_operands (ret);
5633 
5634   if (eptype)
5635     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5636 
5637   protected_set_expr_location (ret, loc);
5638   return ret;
5639 }
5640 
5641 /* Issue -Wcast-qual warnings when appropriate.  TYPE is the type to
5642    which we are casting.  OTYPE is the type of the expression being
5643    cast.  Both TYPE and OTYPE are pointer types.  LOC is the location
5644    of the cast.  -Wcast-qual appeared on the command line.  Named
5645    address space qualifiers are not handled here, because they result
5646    in different warnings.  */
5647 
5648 static void
handle_warn_cast_qual(location_t loc,tree type,tree otype)5649 handle_warn_cast_qual (location_t loc, tree type, tree otype)
5650 {
5651   tree in_type = type;
5652   tree in_otype = otype;
5653   int added = 0;
5654   int discarded = 0;
5655   bool is_const;
5656 
5657   /* Check that the qualifiers on IN_TYPE are a superset of the
5658      qualifiers of IN_OTYPE.  The outermost level of POINTER_TYPE
5659      nodes is uninteresting and we stop as soon as we hit a
5660      non-POINTER_TYPE node on either type.  */
5661   do
5662     {
5663       in_otype = TREE_TYPE (in_otype);
5664       in_type = TREE_TYPE (in_type);
5665 
5666       /* GNU C allows cv-qualified function types.  'const' means the
5667 	 function is very pure, 'volatile' means it can't return.  We
5668 	 need to warn when such qualifiers are added, not when they're
5669 	 taken away.  */
5670       if (TREE_CODE (in_otype) == FUNCTION_TYPE
5671 	  && TREE_CODE (in_type) == FUNCTION_TYPE)
5672 	added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5673 		  & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
5674       else
5675 	discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5676 		      & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
5677     }
5678   while (TREE_CODE (in_type) == POINTER_TYPE
5679 	 && TREE_CODE (in_otype) == POINTER_TYPE);
5680 
5681   if (added)
5682     warning_at (loc, OPT_Wcast_qual,
5683 		"cast adds %q#v qualifier to function type", added);
5684 
5685   if (discarded)
5686     /* There are qualifiers present in IN_OTYPE that are not present
5687        in IN_TYPE.  */
5688     warning_at (loc, OPT_Wcast_qual,
5689 		"cast discards %qv qualifier from pointer target type",
5690 		discarded);
5691 
5692   if (added || discarded)
5693     return;
5694 
5695   /* A cast from **T to const **T is unsafe, because it can cause a
5696      const value to be changed with no additional warning.  We only
5697      issue this warning if T is the same on both sides, and we only
5698      issue the warning if there are the same number of pointers on
5699      both sides, as otherwise the cast is clearly unsafe anyhow.  A
5700      cast is unsafe when a qualifier is added at one level and const
5701      is not present at all outer levels.
5702 
5703      To issue this warning, we check at each level whether the cast
5704      adds new qualifiers not already seen.  We don't need to special
5705      case function types, as they won't have the same
5706      TYPE_MAIN_VARIANT.  */
5707 
5708   if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5709     return;
5710   if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5711     return;
5712 
5713   in_type = type;
5714   in_otype = otype;
5715   is_const = TYPE_READONLY (TREE_TYPE (in_type));
5716   do
5717     {
5718       in_type = TREE_TYPE (in_type);
5719       in_otype = TREE_TYPE (in_otype);
5720       if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5721 	  && !is_const)
5722 	{
5723 	  warning_at (loc, OPT_Wcast_qual,
5724 		      "to be safe all intermediate pointers in cast from "
5725                       "%qT to %qT must be %<const%> qualified",
5726 		      otype, type);
5727 	  break;
5728 	}
5729       if (is_const)
5730 	is_const = TYPE_READONLY (in_type);
5731     }
5732   while (TREE_CODE (in_type) == POINTER_TYPE);
5733 }
5734 
5735 /* Heuristic check if two parameter types can be considered ABI-equivalent.  */
5736 
5737 static bool
c_safe_arg_type_equiv_p(tree t1,tree t2)5738 c_safe_arg_type_equiv_p (tree t1, tree t2)
5739 {
5740   t1 = TYPE_MAIN_VARIANT (t1);
5741   t2 = TYPE_MAIN_VARIANT (t2);
5742 
5743   if (TREE_CODE (t1) == POINTER_TYPE
5744       && TREE_CODE (t2) == POINTER_TYPE)
5745     return true;
5746 
5747   /* The signedness of the parameter matters only when an integral
5748      type smaller than int is promoted to int, otherwise only the
5749      precision of the parameter matters.
5750      This check should make sure that the callee does not see
5751      undefined values in argument registers.  */
5752   if (INTEGRAL_TYPE_P (t1)
5753       && INTEGRAL_TYPE_P (t2)
5754       && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5755       && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5756 	  || !targetm.calls.promote_prototypes (NULL_TREE)
5757 	  || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5758     return true;
5759 
5760   return comptypes (t1, t2);
5761 }
5762 
5763 /* Check if a type cast between two function types can be considered safe.  */
5764 
5765 static bool
c_safe_function_type_cast_p(tree t1,tree t2)5766 c_safe_function_type_cast_p (tree t1, tree t2)
5767 {
5768   if (TREE_TYPE (t1) == void_type_node &&
5769       TYPE_ARG_TYPES (t1) == void_list_node)
5770     return true;
5771 
5772   if (TREE_TYPE (t2) == void_type_node &&
5773       TYPE_ARG_TYPES (t2) == void_list_node)
5774     return true;
5775 
5776   if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5777     return false;
5778 
5779   for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5780        t1 && t2;
5781        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5782     if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5783       return false;
5784 
5785   return true;
5786 }
5787 
5788 /* Build an expression representing a cast to type TYPE of expression EXPR.
5789    LOC is the location of the cast-- typically the open paren of the cast.  */
5790 
5791 tree
build_c_cast(location_t loc,tree type,tree expr)5792 build_c_cast (location_t loc, tree type, tree expr)
5793 {
5794   tree value;
5795 
5796   bool int_operands = EXPR_INT_CONST_OPERANDS (expr);
5797 
5798   if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5799     expr = TREE_OPERAND (expr, 0);
5800 
5801   value = expr;
5802   if (int_operands)
5803     value = remove_c_maybe_const_expr (value);
5804 
5805   if (type == error_mark_node || expr == error_mark_node)
5806     return error_mark_node;
5807 
5808   /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5809      only in <protocol> qualifications.  But when constructing cast expressions,
5810      the protocols do matter and must be kept around.  */
5811   if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5812     return build1 (NOP_EXPR, type, expr);
5813 
5814   type = TYPE_MAIN_VARIANT (type);
5815 
5816   if (TREE_CODE (type) == ARRAY_TYPE)
5817     {
5818       error_at (loc, "cast specifies array type");
5819       return error_mark_node;
5820     }
5821 
5822   if (TREE_CODE (type) == FUNCTION_TYPE)
5823     {
5824       error_at (loc, "cast specifies function type");
5825       return error_mark_node;
5826     }
5827 
5828   if (!VOID_TYPE_P (type))
5829     {
5830       value = require_complete_type (loc, value);
5831       if (value == error_mark_node)
5832 	return error_mark_node;
5833     }
5834 
5835   if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5836     {
5837       if (RECORD_OR_UNION_TYPE_P (type))
5838 	pedwarn (loc, OPT_Wpedantic,
5839 		 "ISO C forbids casting nonscalar to the same type");
5840 
5841       /* Convert to remove any qualifiers from VALUE's type.  */
5842       value = convert (type, value);
5843     }
5844   else if (TREE_CODE (type) == UNION_TYPE)
5845     {
5846       tree field;
5847 
5848       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5849 	if (TREE_TYPE (field) != error_mark_node
5850 	    && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5851 			  TYPE_MAIN_VARIANT (TREE_TYPE (value))))
5852 	  break;
5853 
5854       if (field)
5855 	{
5856 	  tree t;
5857 	  bool maybe_const = true;
5858 
5859 	  pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
5860 	  t = c_fully_fold (value, false, &maybe_const);
5861 	  t = build_constructor_single (type, field, t);
5862 	  if (!maybe_const)
5863 	    t = c_wrap_maybe_const (t, true);
5864 	  t = digest_init (loc, type, t,
5865 			   NULL_TREE, false, true, 0);
5866 	  TREE_CONSTANT (t) = TREE_CONSTANT (value);
5867 	  return t;
5868 	}
5869       error_at (loc, "cast to union type from type not present in union");
5870       return error_mark_node;
5871     }
5872   else
5873     {
5874       tree otype, ovalue;
5875 
5876       if (type == void_type_node)
5877 	{
5878 	  tree t = build1 (CONVERT_EXPR, type, value);
5879 	  SET_EXPR_LOCATION (t, loc);
5880 	  return t;
5881 	}
5882 
5883       otype = TREE_TYPE (value);
5884 
5885       /* Optionally warn about potentially worrisome casts.  */
5886       if (warn_cast_qual
5887 	  && TREE_CODE (type) == POINTER_TYPE
5888 	  && TREE_CODE (otype) == POINTER_TYPE)
5889 	handle_warn_cast_qual (loc, type, otype);
5890 
5891       /* Warn about conversions between pointers to disjoint
5892 	 address spaces.  */
5893       if (TREE_CODE (type) == POINTER_TYPE
5894 	  && TREE_CODE (otype) == POINTER_TYPE
5895 	  && !null_pointer_constant_p (value))
5896 	{
5897 	  addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5898 	  addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5899 	  addr_space_t as_common;
5900 
5901 	  if (!addr_space_superset (as_to, as_from, &as_common))
5902 	    {
5903 	      if (ADDR_SPACE_GENERIC_P (as_from))
5904 		warning_at (loc, 0, "cast to %s address space pointer "
5905 			    "from disjoint generic address space pointer",
5906 			    c_addr_space_name (as_to));
5907 
5908 	      else if (ADDR_SPACE_GENERIC_P (as_to))
5909 		warning_at (loc, 0, "cast to generic address space pointer "
5910 			    "from disjoint %s address space pointer",
5911 			    c_addr_space_name (as_from));
5912 
5913 	      else
5914 		warning_at (loc, 0, "cast to %s address space pointer "
5915 			    "from disjoint %s address space pointer",
5916 			    c_addr_space_name (as_to),
5917 			    c_addr_space_name (as_from));
5918 	    }
5919 	}
5920 
5921       /* Warn about possible alignment problems.  */
5922       if ((STRICT_ALIGNMENT || warn_cast_align == 2)
5923 	  && TREE_CODE (type) == POINTER_TYPE
5924 	  && TREE_CODE (otype) == POINTER_TYPE
5925 	  && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5926 	  && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5927 	  /* Don't warn about opaque types, where the actual alignment
5928 	     restriction is unknown.  */
5929 	  && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
5930 	       && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5931 	  && min_align_of_type (TREE_TYPE (type))
5932 	     > min_align_of_type (TREE_TYPE (otype)))
5933 	warning_at (loc, OPT_Wcast_align,
5934 		    "cast increases required alignment of target type");
5935 
5936       if (TREE_CODE (type) == INTEGER_TYPE
5937 	  && TREE_CODE (otype) == POINTER_TYPE
5938 	  && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5939       /* Unlike conversion of integers to pointers, where the
5940          warning is disabled for converting constants because
5941          of cases such as SIG_*, warn about converting constant
5942          pointers to integers. In some cases it may cause unwanted
5943          sign extension, and a warning is appropriate.  */
5944 	warning_at (loc, OPT_Wpointer_to_int_cast,
5945 		    "cast from pointer to integer of different size");
5946 
5947       if (TREE_CODE (value) == CALL_EXPR
5948 	  && TREE_CODE (type) != TREE_CODE (otype))
5949 	warning_at (loc, OPT_Wbad_function_cast,
5950 		    "cast from function call of type %qT "
5951 		    "to non-matching type %qT", otype, type);
5952 
5953       if (TREE_CODE (type) == POINTER_TYPE
5954 	  && TREE_CODE (otype) == INTEGER_TYPE
5955 	  && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5956 	  /* Don't warn about converting any constant.  */
5957 	  && !TREE_CONSTANT (value))
5958 	warning_at (loc,
5959 		    OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5960 		    "of different size");
5961 
5962       if (warn_strict_aliasing <= 2)
5963         strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
5964 
5965       /* If pedantic, warn for conversions between function and object
5966 	 pointer types, except for converting a null pointer constant
5967 	 to function pointer type.  */
5968       if (pedantic
5969 	  && TREE_CODE (type) == POINTER_TYPE
5970 	  && TREE_CODE (otype) == POINTER_TYPE
5971 	  && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5972 	  && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
5973 	pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5974 		 "conversion of function pointer to object pointer type");
5975 
5976       if (pedantic
5977 	  && TREE_CODE (type) == POINTER_TYPE
5978 	  && TREE_CODE (otype) == POINTER_TYPE
5979 	  && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5980 	  && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5981 	  && !null_pointer_constant_p (value))
5982 	pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
5983 		 "conversion of object pointer to function pointer type");
5984 
5985       if (TREE_CODE (type) == POINTER_TYPE
5986 	  && TREE_CODE (otype) == POINTER_TYPE
5987 	  && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5988 	  && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5989 	  && !c_safe_function_type_cast_p (TREE_TYPE (type),
5990 					   TREE_TYPE (otype)))
5991 	warning_at (loc, OPT_Wcast_function_type,
5992 		    "cast between incompatible function types"
5993 		    " from %qT to %qT", otype, type);
5994 
5995       ovalue = value;
5996       value = convert (type, value);
5997 
5998       /* Ignore any integer overflow caused by the cast.  */
5999       if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
6000 	{
6001 	  if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6002 	    {
6003 	      if (!TREE_OVERFLOW (value))
6004 		{
6005 		  /* Avoid clobbering a shared constant.  */
6006 		  value = copy_node (value);
6007 		  TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
6008 		}
6009 	    }
6010 	  else if (TREE_OVERFLOW (value))
6011 	    /* Reset VALUE's overflow flags, ensuring constant sharing.  */
6012 	    value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
6013 	}
6014     }
6015 
6016   /* Don't let a cast be an lvalue.  */
6017   if (lvalue_p (value))
6018     value = non_lvalue_loc (loc, value);
6019 
6020   /* Don't allow the results of casting to floating-point or complex
6021      types be confused with actual constants, or casts involving
6022      integer and pointer types other than direct integer-to-integer
6023      and integer-to-pointer be confused with integer constant
6024      expressions and null pointer constants.  */
6025   if (TREE_CODE (value) == REAL_CST
6026       || TREE_CODE (value) == COMPLEX_CST
6027       || (TREE_CODE (value) == INTEGER_CST
6028 	  && !((TREE_CODE (expr) == INTEGER_CST
6029 		&& INTEGRAL_TYPE_P (TREE_TYPE (expr)))
6030 	       || TREE_CODE (expr) == REAL_CST
6031 	       || TREE_CODE (expr) == COMPLEX_CST)))
6032       value = build1 (NOP_EXPR, type, value);
6033 
6034   /* If the expression has integer operands and so can occur in an
6035      unevaluated part of an integer constant expression, ensure the
6036      return value reflects this.  */
6037   if (int_operands
6038       && INTEGRAL_TYPE_P (type)
6039       && value != error_mark_node
6040       && !EXPR_INT_CONST_OPERANDS (value))
6041     value = note_integer_operands (value);
6042 
6043   protected_set_expr_location (value, loc);
6044   return value;
6045 }
6046 
6047 /* Interpret a cast of expression EXPR to type TYPE.  LOC is the
6048    location of the open paren of the cast, or the position of the cast
6049    expr.  */
6050 tree
c_cast_expr(location_t loc,struct c_type_name * type_name,tree expr)6051 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
6052 {
6053   tree type;
6054   tree type_expr = NULL_TREE;
6055   bool type_expr_const = true;
6056   tree ret;
6057   int saved_wsp = warn_strict_prototypes;
6058 
6059   /* This avoids warnings about unprototyped casts on
6060      integers.  E.g. "#define SIG_DFL (void(*)())0".  */
6061   if (TREE_CODE (expr) == INTEGER_CST)
6062     warn_strict_prototypes = 0;
6063   type = groktypename (type_name, &type_expr, &type_expr_const);
6064   warn_strict_prototypes = saved_wsp;
6065 
6066   if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
6067       && reject_gcc_builtin (expr))
6068     return error_mark_node;
6069 
6070   ret = build_c_cast (loc, type, expr);
6071   if (type_expr)
6072     {
6073       bool inner_expr_const = true;
6074       ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
6075       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
6076       C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
6077 					     && inner_expr_const);
6078       SET_EXPR_LOCATION (ret, loc);
6079     }
6080 
6081   if (!EXPR_HAS_LOCATION (ret))
6082     protected_set_expr_location (ret, loc);
6083 
6084   /* C++ does not permits types to be defined in a cast, but it
6085      allows references to incomplete types.  */
6086   if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
6087     warning_at (loc, OPT_Wc___compat,
6088 		"defining a type in a cast is invalid in C++");
6089 
6090   return ret;
6091 }
6092 
6093 /* Build an assignment expression of lvalue LHS from value RHS.
6094    If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
6095    may differ from TREE_TYPE (LHS) for an enum bitfield.
6096    MODIFYCODE is the code for a binary operator that we use
6097    to combine the old value of LHS with RHS to get the new value.
6098    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
6099    If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
6100    which may differ from TREE_TYPE (RHS) for an enum value.
6101 
6102    LOCATION is the location of the MODIFYCODE operator.
6103    RHS_LOC is the location of the RHS.  */
6104 
6105 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)6106 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
6107 		   enum tree_code modifycode,
6108 		   location_t rhs_loc, tree rhs, tree rhs_origtype)
6109 {
6110   tree result;
6111   tree newrhs;
6112   tree rhseval = NULL_TREE;
6113   tree lhstype = TREE_TYPE (lhs);
6114   tree olhstype = lhstype;
6115   bool npc;
6116   bool is_atomic_op;
6117 
6118   /* Types that aren't fully specified cannot be used in assignments.  */
6119   lhs = require_complete_type (location, lhs);
6120 
6121   /* Avoid duplicate error messages from operands that had errors.  */
6122   if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
6123     return error_mark_node;
6124 
6125   /* Ensure an error for assigning a non-lvalue array to an array in
6126      C90.  */
6127   if (TREE_CODE (lhstype) == ARRAY_TYPE)
6128     {
6129       error_at (location, "assignment to expression with array type");
6130       return error_mark_node;
6131     }
6132 
6133   /* For ObjC properties, defer this check.  */
6134   if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
6135     return error_mark_node;
6136 
6137   is_atomic_op = really_atomic_lvalue (lhs);
6138 
6139   newrhs = rhs;
6140 
6141   if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
6142     {
6143       tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
6144 				      lhs_origtype, modifycode, rhs_loc, rhs,
6145 				      rhs_origtype);
6146       if (inner == error_mark_node)
6147 	return error_mark_node;
6148       result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
6149 		       C_MAYBE_CONST_EXPR_PRE (lhs), inner);
6150       gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
6151       C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
6152       protected_set_expr_location (result, location);
6153       return result;
6154     }
6155 
6156   /* If a binary op has been requested, combine the old LHS value with the RHS
6157      producing the value we should actually store into the LHS.  */
6158 
6159   if (modifycode != NOP_EXPR)
6160     {
6161       lhs = c_fully_fold (lhs, false, NULL, true);
6162       lhs = stabilize_reference (lhs);
6163 
6164       /* Construct the RHS for any non-atomic compound assignemnt. */
6165       if (!is_atomic_op)
6166         {
6167 	  /* If in LHS op= RHS the RHS has side-effects, ensure they
6168 	     are preevaluated before the rest of the assignment expression's
6169 	     side-effects, because RHS could contain e.g. function calls
6170 	     that modify LHS.  */
6171 	  if (TREE_SIDE_EFFECTS (rhs))
6172 	    {
6173 	      if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6174 		newrhs = save_expr (TREE_OPERAND (rhs, 0));
6175 	      else
6176 		newrhs = save_expr (rhs);
6177 	      rhseval = newrhs;
6178 	      if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6179 		newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
6180 				 newrhs);
6181 	    }
6182 	  newrhs = build_binary_op (location,
6183 				    modifycode, lhs, newrhs, true);
6184 
6185 	  /* The original type of the right hand side is no longer
6186 	     meaningful.  */
6187 	  rhs_origtype = NULL_TREE;
6188 	}
6189     }
6190 
6191   if (c_dialect_objc ())
6192     {
6193       /* Check if we are modifying an Objective-C property reference;
6194 	 if so, we need to generate setter calls.  */
6195       if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6196 	result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
6197       else
6198 	result = objc_maybe_build_modify_expr (lhs, newrhs);
6199       if (result)
6200 	goto return_result;
6201 
6202       /* Else, do the check that we postponed for Objective-C.  */
6203       if (!lvalue_or_else (location, lhs, lv_assign))
6204 	return error_mark_node;
6205     }
6206 
6207   /* Give an error for storing in something that is 'const'.  */
6208 
6209   if (TYPE_READONLY (lhstype)
6210       || (RECORD_OR_UNION_TYPE_P (lhstype)
6211 	  && C_TYPE_FIELDS_READONLY (lhstype)))
6212     {
6213       readonly_error (location, lhs, lv_assign);
6214       return error_mark_node;
6215     }
6216   else if (TREE_READONLY (lhs))
6217     readonly_warning (lhs, lv_assign);
6218 
6219   /* If storing into a structure or union member,
6220      it has probably been given type `int'.
6221      Compute the type that would go with
6222      the actual amount of storage the member occupies.  */
6223 
6224   if (TREE_CODE (lhs) == COMPONENT_REF
6225       && (TREE_CODE (lhstype) == INTEGER_TYPE
6226 	  || TREE_CODE (lhstype) == BOOLEAN_TYPE
6227 	  || TREE_CODE (lhstype) == REAL_TYPE
6228 	  || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6229     lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6230 
6231   /* If storing in a field that is in actuality a short or narrower than one,
6232      we must store in the field in its actual type.  */
6233 
6234   if (lhstype != TREE_TYPE (lhs))
6235     {
6236       lhs = copy_node (lhs);
6237       TREE_TYPE (lhs) = lhstype;
6238     }
6239 
6240   /* Issue -Wc++-compat warnings about an assignment to an enum type
6241      when LHS does not have its original type.  This happens for,
6242      e.g., an enum bitfield in a struct.  */
6243   if (warn_cxx_compat
6244       && lhs_origtype != NULL_TREE
6245       && lhs_origtype != lhstype
6246       && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6247     {
6248       tree checktype = (rhs_origtype != NULL_TREE
6249 			? rhs_origtype
6250 			: TREE_TYPE (rhs));
6251       if (checktype != error_mark_node
6252 	  && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6253 	      || (is_atomic_op && modifycode != NOP_EXPR)))
6254 	warning_at (location, OPT_Wc___compat,
6255 		    "enum conversion in assignment is invalid in C++");
6256     }
6257 
6258   /* If the lhs is atomic, remove that qualifier.  */
6259   if (is_atomic_op)
6260     {
6261       lhstype = build_qualified_type (lhstype,
6262 				      (TYPE_QUALS (lhstype)
6263 				       & ~TYPE_QUAL_ATOMIC));
6264       olhstype = build_qualified_type (olhstype,
6265 				       (TYPE_QUALS (lhstype)
6266 					& ~TYPE_QUAL_ATOMIC));
6267     }
6268 
6269   /* Convert new value to destination type.  Fold it first, then
6270      restore any excess precision information, for the sake of
6271      conversion warnings.  */
6272 
6273   if (!(is_atomic_op && modifycode != NOP_EXPR))
6274     {
6275       tree rhs_semantic_type = NULL_TREE;
6276       if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6277 	{
6278 	  rhs_semantic_type = TREE_TYPE (newrhs);
6279 	  newrhs = TREE_OPERAND (newrhs, 0);
6280 	}
6281       npc = null_pointer_constant_p (newrhs);
6282       newrhs = c_fully_fold (newrhs, false, NULL);
6283       if (rhs_semantic_type)
6284 	newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
6285       newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6286 				       rhs_origtype, ic_assign, npc,
6287 				       NULL_TREE, NULL_TREE, 0);
6288       if (TREE_CODE (newrhs) == ERROR_MARK)
6289 	return error_mark_node;
6290     }
6291 
6292   /* Emit ObjC write barrier, if necessary.  */
6293   if (c_dialect_objc () && flag_objc_gc)
6294     {
6295       result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6296       if (result)
6297 	{
6298 	  protected_set_expr_location (result, location);
6299 	  goto return_result;
6300 	}
6301     }
6302 
6303   /* Scan operands.  */
6304 
6305   if (is_atomic_op)
6306     result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6307   else
6308     {
6309       result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6310       TREE_SIDE_EFFECTS (result) = 1;
6311       protected_set_expr_location (result, location);
6312     }
6313 
6314   /* If we got the LHS in a different type for storing in,
6315      convert the result back to the nominal type of LHS
6316      so that the value we return always has the same type
6317      as the LHS argument.  */
6318 
6319   if (olhstype == TREE_TYPE (result))
6320     goto return_result;
6321 
6322   result = convert_for_assignment (location, rhs_loc, olhstype, result,
6323 				   rhs_origtype, ic_assign, false, NULL_TREE,
6324 				   NULL_TREE, 0);
6325   protected_set_expr_location (result, location);
6326 
6327 return_result:
6328   if (rhseval)
6329     result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
6330   return result;
6331 }
6332 
6333 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6334    This is used to implement -fplan9-extensions.  */
6335 
6336 static bool
find_anonymous_field_with_type(tree struct_type,tree type)6337 find_anonymous_field_with_type (tree struct_type, tree type)
6338 {
6339   tree field;
6340   bool found;
6341 
6342   gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
6343   found = false;
6344   for (field = TYPE_FIELDS (struct_type);
6345        field != NULL_TREE;
6346        field = TREE_CHAIN (field))
6347     {
6348       tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6349 			? c_build_qualified_type (TREE_TYPE (field),
6350 						  TYPE_QUAL_ATOMIC)
6351 			: TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6352       if (DECL_NAME (field) == NULL
6353 	  && comptypes (type, fieldtype))
6354 	{
6355 	  if (found)
6356 	    return false;
6357 	  found = true;
6358 	}
6359       else if (DECL_NAME (field) == NULL
6360 	       && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
6361 	       && find_anonymous_field_with_type (TREE_TYPE (field), type))
6362 	{
6363 	  if (found)
6364 	    return false;
6365 	  found = true;
6366 	}
6367     }
6368   return found;
6369 }
6370 
6371 /* RHS is an expression whose type is pointer to struct.  If there is
6372    an anonymous field in RHS with type TYPE, then return a pointer to
6373    that field in RHS.  This is used with -fplan9-extensions.  This
6374    returns NULL if no conversion could be found.  */
6375 
6376 static tree
convert_to_anonymous_field(location_t location,tree type,tree rhs)6377 convert_to_anonymous_field (location_t location, tree type, tree rhs)
6378 {
6379   tree rhs_struct_type, lhs_main_type;
6380   tree field, found_field;
6381   bool found_sub_field;
6382   tree ret;
6383 
6384   gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6385   rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
6386   gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
6387 
6388   gcc_assert (POINTER_TYPE_P (type));
6389   lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6390 		   ? c_build_qualified_type (TREE_TYPE (type),
6391 					     TYPE_QUAL_ATOMIC)
6392 		   : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
6393 
6394   found_field = NULL_TREE;
6395   found_sub_field = false;
6396   for (field = TYPE_FIELDS (rhs_struct_type);
6397        field != NULL_TREE;
6398        field = TREE_CHAIN (field))
6399     {
6400       if (DECL_NAME (field) != NULL_TREE
6401 	  || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
6402 	continue;
6403       tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6404 			? c_build_qualified_type (TREE_TYPE (field),
6405 						  TYPE_QUAL_ATOMIC)
6406 			: TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6407       if (comptypes (lhs_main_type, fieldtype))
6408 	{
6409 	  if (found_field != NULL_TREE)
6410 	    return NULL_TREE;
6411 	  found_field = field;
6412 	}
6413       else if (find_anonymous_field_with_type (TREE_TYPE (field),
6414 					       lhs_main_type))
6415 	{
6416 	  if (found_field != NULL_TREE)
6417 	    return NULL_TREE;
6418 	  found_field = field;
6419 	  found_sub_field = true;
6420 	}
6421     }
6422 
6423   if (found_field == NULL_TREE)
6424     return NULL_TREE;
6425 
6426   ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6427 			 build_fold_indirect_ref (rhs), found_field,
6428 			 NULL_TREE);
6429   ret = build_fold_addr_expr_loc (location, ret);
6430 
6431   if (found_sub_field)
6432     {
6433       ret = convert_to_anonymous_field (location, type, ret);
6434       gcc_assert (ret != NULL_TREE);
6435     }
6436 
6437   return ret;
6438 }
6439 
6440 /* Issue an error message for a bad initializer component.
6441    GMSGID identifies the message.
6442    The component name is taken from the spelling stack.  */
6443 
6444 static void ATTRIBUTE_GCC_DIAG (2,0)
error_init(location_t loc,const char * gmsgid,...)6445 error_init (location_t loc, const char *gmsgid, ...)
6446 {
6447   char *ofwhat;
6448 
6449   auto_diagnostic_group d;
6450 
6451   /* The gmsgid may be a format string with %< and %>. */
6452   va_list ap;
6453   va_start (ap, gmsgid);
6454   bool warned = emit_diagnostic_valist (DK_ERROR, loc, -1, gmsgid, &ap);
6455   va_end (ap);
6456 
6457   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6458   if (*ofwhat && warned)
6459     inform (loc, "(near initialization for %qs)", ofwhat);
6460 }
6461 
6462 /* Issue a pedantic warning for a bad initializer component.  OPT is
6463    the option OPT_* (from options.h) controlling this warning or 0 if
6464    it is unconditionally given.  GMSGID identifies the message.  The
6465    component name is taken from the spelling stack.  */
6466 
6467 static void ATTRIBUTE_GCC_DIAG (3,0)
pedwarn_init(location_t loc,int opt,const char * gmsgid,...)6468 pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
6469 {
6470   /* Use the location where a macro was expanded rather than where
6471      it was defined to make sure macros defined in system headers
6472      but used incorrectly elsewhere are diagnosed.  */
6473   location_t exploc = expansion_point_location_if_in_system_header (loc);
6474   auto_diagnostic_group d;
6475   va_list ap;
6476   va_start (ap, gmsgid);
6477   bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6478   va_end (ap);
6479   char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6480   if (*ofwhat && warned)
6481     inform (exploc, "(near initialization for %qs)", ofwhat);
6482 }
6483 
6484 /* Issue a warning for a bad initializer component.
6485 
6486    OPT is the OPT_W* value corresponding to the warning option that
6487    controls this warning.  GMSGID identifies the message.  The
6488    component name is taken from the spelling stack.  */
6489 
6490 static void
warning_init(location_t loc,int opt,const char * gmsgid)6491 warning_init (location_t loc, int opt, const char *gmsgid)
6492 {
6493   char *ofwhat;
6494   bool warned;
6495 
6496   auto_diagnostic_group d;
6497 
6498   /* Use the location where a macro was expanded rather than where
6499      it was defined to make sure macros defined in system headers
6500      but used incorrectly elsewhere are diagnosed.  */
6501   location_t exploc = expansion_point_location_if_in_system_header (loc);
6502 
6503   /* The gmsgid may be a format string with %< and %>. */
6504   warned = warning_at (exploc, opt, gmsgid);
6505   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6506   if (*ofwhat && warned)
6507     inform (exploc, "(near initialization for %qs)", ofwhat);
6508 }
6509 
6510 /* If TYPE is an array type and EXPR is a parenthesized string
6511    constant, warn if pedantic that EXPR is being used to initialize an
6512    object of type TYPE.  */
6513 
6514 void
maybe_warn_string_init(location_t loc,tree type,struct c_expr expr)6515 maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
6516 {
6517   if (pedantic
6518       && TREE_CODE (type) == ARRAY_TYPE
6519       && TREE_CODE (expr.value) == STRING_CST
6520       && expr.original_code != STRING_CST)
6521     pedwarn_init (loc, OPT_Wpedantic,
6522 		  "array initialized from parenthesized string constant");
6523 }
6524 
6525 /* Attempt to locate the parameter with the given index within FNDECL,
6526    returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found.  */
6527 
6528 static location_t
get_fndecl_argument_location(tree fndecl,int argnum)6529 get_fndecl_argument_location (tree fndecl, int argnum)
6530 {
6531   int i;
6532   tree param;
6533 
6534   /* Locate param by index within DECL_ARGUMENTS (fndecl).  */
6535   for (i = 0, param = DECL_ARGUMENTS (fndecl);
6536        i < argnum && param;
6537        i++, param = TREE_CHAIN (param))
6538     ;
6539 
6540   /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6541      return DECL_SOURCE_LOCATION (FNDECL).  */
6542   if (param == NULL)
6543     return DECL_SOURCE_LOCATION (fndecl);
6544 
6545   return DECL_SOURCE_LOCATION (param);
6546 }
6547 
6548 /* Issue a note about a mismatching argument for parameter PARMNUM
6549    to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6550    Attempt to issue the note at the pertinent parameter of the decl;
6551    failing that issue it at the location of FUNDECL; failing that
6552    issue it at PLOC.  */
6553 
6554 static void
inform_for_arg(tree fundecl,location_t ploc,int parmnum,tree expected_type,tree actual_type)6555 inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6556 		tree expected_type, tree actual_type)
6557 {
6558   location_t loc;
6559   if (fundecl && !DECL_IS_BUILTIN (fundecl))
6560     loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6561   else
6562     loc = ploc;
6563 
6564   inform (loc,
6565 	  "expected %qT but argument is of type %qT",
6566 	  expected_type, actual_type);
6567 }
6568 
6569 /* Issue a warning when an argument of ARGTYPE is passed to a built-in
6570    function FUNDECL declared without prototype to parameter PARMNUM of
6571    PARMTYPE when ARGTYPE does not promote to PARMTYPE.  */
6572 
6573 static void
maybe_warn_builtin_no_proto_arg(location_t loc,tree fundecl,int parmnum,tree parmtype,tree argtype)6574 maybe_warn_builtin_no_proto_arg (location_t loc, tree fundecl, int parmnum,
6575 				 tree parmtype, tree argtype)
6576 {
6577   tree_code parmcode = TREE_CODE (parmtype);
6578   tree_code argcode = TREE_CODE (argtype);
6579   tree promoted = c_type_promotes_to (argtype);
6580 
6581   /* Avoid warning for enum arguments that promote to an integer type
6582      of the same size/mode.  */
6583   if (parmcode == INTEGER_TYPE
6584       && argcode == ENUMERAL_TYPE
6585       && TYPE_MODE (parmtype) == TYPE_MODE (argtype))
6586     return;
6587 
6588   if ((parmcode == argcode
6589        || (parmcode == INTEGER_TYPE
6590 	   && argcode == ENUMERAL_TYPE))
6591       && TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (promoted))
6592     return;
6593 
6594   /* This diagnoses even signed/unsigned mismatches.  Those might be
6595      safe in many cases but GCC may emit suboptimal code for them so
6596      warning on those cases drives efficiency improvements.  */
6597   if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
6598 		  TYPE_MAIN_VARIANT (promoted) == argtype
6599 		  ? G_("%qD argument %d type is %qT where %qT is expected "
6600 		       "in a call to built-in function declared without "
6601 		       "prototype")
6602 		  : G_("%qD argument %d promotes to %qT where %qT is expected "
6603 		       "in a call to built-in function declared without "
6604 		       "prototype"),
6605 		  fundecl, parmnum, promoted, parmtype))
6606     inform (DECL_SOURCE_LOCATION (fundecl),
6607 	    "built-in %qD declared here",
6608 	    fundecl);
6609 }
6610 
6611 /* Convert value RHS to type TYPE as preparation for an assignment to
6612    an lvalue of type TYPE.  If ORIGTYPE is not NULL_TREE, it is the
6613    original type of RHS; this differs from TREE_TYPE (RHS) for enum
6614    types.  NULL_POINTER_CONSTANT says whether RHS was a null pointer
6615    constant before any folding.
6616    The real work of conversion is done by `convert'.
6617    The purpose of this function is to generate error messages
6618    for assignments that are not allowed in C.
6619    ERRTYPE says whether it is argument passing, assignment,
6620    initialization or return.
6621 
6622    In the following example, '~' denotes where EXPR_LOC and '^' where
6623    LOCATION point to:
6624 
6625      f (var);      [ic_argpass]
6626      ^  ~~~
6627      x = var;      [ic_assign]
6628        ^ ~~~;
6629      int x = var;  [ic_init]
6630 	     ^^^
6631      return x;     [ic_return]
6632 	    ^
6633 
6634    FUNCTION is a tree for the function being called.
6635    PARMNUM is the number of the argument, for printing in error messages.
6636    WARNOPT may be set to a warning option to issue the corresponding warning
6637    rather than an error for invalid conversions.  Used for calls to built-in
6638    functions declared without a prototype.  */
6639 
6640 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)6641 convert_for_assignment (location_t location, location_t expr_loc, tree type,
6642 			tree rhs, tree origtype, enum impl_conv errtype,
6643 			bool null_pointer_constant, tree fundecl,
6644 			tree function, int parmnum, int warnopt /* = 0 */)
6645 {
6646   enum tree_code codel = TREE_CODE (type);
6647   tree orig_rhs = rhs;
6648   tree rhstype;
6649   enum tree_code coder;
6650   tree rname = NULL_TREE;
6651   bool objc_ok = false;
6652 
6653   /* Use the expansion point location to handle cases such as user's
6654      function returning a wrong-type macro defined in a system header.  */
6655   location = expansion_point_location_if_in_system_header (location);
6656 
6657   if (errtype == ic_argpass)
6658     {
6659       tree selector;
6660       /* Change pointer to function to the function itself for
6661 	 diagnostics.  */
6662       if (TREE_CODE (function) == ADDR_EXPR
6663 	  && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6664 	function = TREE_OPERAND (function, 0);
6665 
6666       /* Handle an ObjC selector specially for diagnostics.  */
6667       selector = objc_message_selector ();
6668       rname = function;
6669       if (selector && parmnum > 2)
6670 	{
6671 	  rname = selector;
6672 	  parmnum -= 2;
6673 	}
6674     }
6675 
6676   /* This macro is used to emit diagnostics to ensure that all format
6677      strings are complete sentences, visible to gettext and checked at
6678      compile time.  */
6679 #define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE)	 \
6680   do {                                                                   \
6681     switch (errtype)                                                     \
6682       {                                                                  \
6683       case ic_argpass:                                                   \
6684 	{								\
6685 	  auto_diagnostic_group d;						\
6686 	  if (pedwarn (PLOC, OPT, AR, parmnum, rname))		\
6687 	    inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6688 	}								\
6689         break;                                                           \
6690       case ic_assign:                                                    \
6691         pedwarn (LOCATION, OPT, AS);                                     \
6692         break;                                                           \
6693       case ic_init:                                                      \
6694         pedwarn_init (LOCATION, OPT, IN);                                \
6695         break;                                                           \
6696       case ic_return:                                                    \
6697         pedwarn (LOCATION, OPT, RE);					 \
6698         break;                                                           \
6699       default:                                                           \
6700         gcc_unreachable ();                                              \
6701       }                                                                  \
6702   } while (0)
6703 
6704   /* This macro is used to emit diagnostics to ensure that all format
6705      strings are complete sentences, visible to gettext and checked at
6706      compile time.  It is the same as PEDWARN_FOR_ASSIGNMENT but with an
6707      extra parameter to enumerate qualifiers.  */
6708 #define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6709   do {                                                                   \
6710     switch (errtype)                                                     \
6711       {                                                                  \
6712       case ic_argpass:                                                   \
6713 	{								\
6714 	auto_diagnostic_group d;						\
6715 	if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS))		\
6716 	  inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype);	\
6717 	}								\
6718         break;                                                           \
6719       case ic_assign:                                                    \
6720         pedwarn (LOCATION, OPT, AS, QUALS);				 \
6721         break;                                                           \
6722       case ic_init:                                                      \
6723         pedwarn (LOCATION, OPT, IN, QUALS);				 \
6724         break;                                                           \
6725       case ic_return:                                                    \
6726         pedwarn (LOCATION, OPT, RE, QUALS);				 \
6727         break;                                                           \
6728       default:                                                           \
6729         gcc_unreachable ();                                              \
6730       }                                                                  \
6731   } while (0)
6732 
6733   /* This macro is used to emit diagnostics to ensure that all format
6734      strings are complete sentences, visible to gettext and checked at
6735      compile time.  It is the same as PEDWARN_FOR_QUALIFIERS but uses
6736      warning_at instead of pedwarn.  */
6737 #define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6738   do {                                                                   \
6739     switch (errtype)                                                     \
6740       {                                                                  \
6741       case ic_argpass:                                                   \
6742 	{								\
6743 	  auto_diagnostic_group d;						\
6744 	  if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS))	\
6745 	    inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6746 	}								\
6747         break;                                                           \
6748       case ic_assign:                                                    \
6749         warning_at (LOCATION, OPT, AS, QUALS);                           \
6750         break;                                                           \
6751       case ic_init:                                                      \
6752         warning_at (LOCATION, OPT, IN, QUALS);                           \
6753         break;                                                           \
6754       case ic_return:                                                    \
6755         warning_at (LOCATION, OPT, RE, QUALS);                           \
6756         break;                                                           \
6757       default:                                                           \
6758         gcc_unreachable ();                                              \
6759       }                                                                  \
6760   } while (0)
6761 
6762   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6763     rhs = TREE_OPERAND (rhs, 0);
6764 
6765   rhstype = TREE_TYPE (rhs);
6766   coder = TREE_CODE (rhstype);
6767 
6768   if (coder == ERROR_MARK)
6769     return error_mark_node;
6770 
6771   if (c_dialect_objc ())
6772     {
6773       int parmno;
6774 
6775       switch (errtype)
6776 	{
6777 	case ic_return:
6778 	  parmno = 0;
6779 	  break;
6780 
6781 	case ic_assign:
6782 	  parmno = -1;
6783 	  break;
6784 
6785 	case ic_init:
6786 	  parmno = -2;
6787 	  break;
6788 
6789 	default:
6790 	  parmno = parmnum;
6791 	  break;
6792 	}
6793 
6794       objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6795     }
6796 
6797   if (warn_cxx_compat)
6798     {
6799       tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6800       if (checktype != error_mark_node
6801 	  && TREE_CODE (type) == ENUMERAL_TYPE
6802 	  && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6803 	switch (errtype)
6804 	  {
6805 	  case ic_argpass:
6806 	    if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6807 			 "passing argument %d of %qE is invalid in C++",
6808 			 parmnum, rname))
6809 	      inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6810 		      ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6811 		      "expected %qT but argument is of type %qT",
6812 		      type, rhstype);
6813 	    break;
6814 	  case ic_assign:
6815 	    pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6816 		     "%qT in assignment is invalid in C++", rhstype, type);
6817 	    break;
6818 	  case ic_init:
6819 	    pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6820 			  "%qT to %qT in initialization is invalid in C++",
6821 			  rhstype, type);
6822 	    break;
6823 	  case ic_return:
6824 	    pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6825 		     "%qT in return is invalid in C++", rhstype, type);
6826 	    break;
6827 	  default:
6828 	    gcc_unreachable ();
6829 	  }
6830     }
6831 
6832   if (warn_enum_conversion)
6833     {
6834       tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6835       if (checktype != error_mark_node
6836 	  && TREE_CODE (checktype) == ENUMERAL_TYPE
6837 	  && TREE_CODE (type) == ENUMERAL_TYPE
6838 	  && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6839        {
6840 	  gcc_rich_location loc (location);
6841 	  warning_at (&loc, OPT_Wenum_conversion,
6842 		      "implicit conversion from %qT to %qT",
6843 		      checktype, type);
6844        }
6845     }
6846 
6847   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
6848     {
6849       warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
6850       return rhs;
6851     }
6852 
6853   if (coder == VOID_TYPE)
6854     {
6855       /* Except for passing an argument to an unprototyped function,
6856 	 this is a constraint violation.  When passing an argument to
6857 	 an unprototyped function, it is compile-time undefined;
6858 	 making it a constraint in that case was rejected in
6859 	 DR#252.  */
6860       const char msg[] = "void value not ignored as it ought to be";
6861       if (warnopt)
6862 	warning_at (location, warnopt, msg);
6863       else
6864 	error_at (location, msg);
6865       return error_mark_node;
6866     }
6867   rhs = require_complete_type (location, rhs);
6868   if (rhs == error_mark_node)
6869     return error_mark_node;
6870 
6871   if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6872     return error_mark_node;
6873 
6874   /* A non-reference type can convert to a reference.  This handles
6875      va_start, va_copy and possibly port built-ins.  */
6876   if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
6877     {
6878       if (!lvalue_p (rhs))
6879 	{
6880 	  const char msg[] = "cannot pass rvalue to reference parameter";
6881 	  if (warnopt)
6882 	    warning_at (location, warnopt, msg);
6883 	  else
6884 	    error_at (location, msg);
6885 	  return error_mark_node;
6886 	}
6887       if (!c_mark_addressable (rhs))
6888 	return error_mark_node;
6889       rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
6890       SET_EXPR_LOCATION (rhs, location);
6891 
6892       rhs = convert_for_assignment (location, expr_loc,
6893 				    build_pointer_type (TREE_TYPE (type)),
6894 				    rhs, origtype, errtype,
6895 				    null_pointer_constant, fundecl, function,
6896 				    parmnum, warnopt);
6897       if (rhs == error_mark_node)
6898 	return error_mark_node;
6899 
6900       rhs = build1 (NOP_EXPR, type, rhs);
6901       SET_EXPR_LOCATION (rhs, location);
6902       return rhs;
6903     }
6904   /* Some types can interconvert without explicit casts.  */
6905   else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
6906 	   && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
6907     return convert (type, rhs);
6908   /* Arithmetic types all interconvert, and enum is treated like int.  */
6909   else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
6910 	    || codel == FIXED_POINT_TYPE
6911 	    || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6912 	    || codel == BOOLEAN_TYPE)
6913 	   && (coder == INTEGER_TYPE || coder == REAL_TYPE
6914 	       || coder == FIXED_POINT_TYPE
6915 	       || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6916 	       || coder == BOOLEAN_TYPE))
6917     {
6918       if (warnopt && errtype == ic_argpass)
6919 	maybe_warn_builtin_no_proto_arg (expr_loc, fundecl, parmnum, type,
6920 					 rhstype);
6921 
6922       bool save = in_late_binary_op;
6923       if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6924 	  || (coder == REAL_TYPE
6925 	      && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6926 	      && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
6927 	in_late_binary_op = true;
6928       tree ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6929 				    ? expr_loc : location, type, orig_rhs);
6930       in_late_binary_op = save;
6931       return ret;
6932     }
6933 
6934   /* Aggregates in different TUs might need conversion.  */
6935   if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6936       && codel == coder
6937       && comptypes (type, rhstype))
6938     return convert_and_check (expr_loc != UNKNOWN_LOCATION
6939 			      ? expr_loc : location, type, rhs);
6940 
6941   /* Conversion to a transparent union or record from its member types.
6942      This applies only to function arguments.  */
6943   if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6944       && TYPE_TRANSPARENT_AGGR (type))
6945       && errtype == ic_argpass)
6946     {
6947       tree memb, marginal_memb = NULL_TREE;
6948 
6949       for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
6950 	{
6951 	  tree memb_type = TREE_TYPE (memb);
6952 
6953 	  if (comptypes (TYPE_MAIN_VARIANT (memb_type),
6954 			 TYPE_MAIN_VARIANT (rhstype)))
6955 	    break;
6956 
6957 	  if (TREE_CODE (memb_type) != POINTER_TYPE)
6958 	    continue;
6959 
6960 	  if (coder == POINTER_TYPE)
6961 	    {
6962 	      tree ttl = TREE_TYPE (memb_type);
6963 	      tree ttr = TREE_TYPE (rhstype);
6964 
6965 	      /* Any non-function converts to a [const][volatile] void *
6966 		 and vice versa; otherwise, targets must be the same.
6967 		 Meanwhile, the lhs target must have all the qualifiers of
6968 		 the rhs.  */
6969 	      if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6970 		  || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
6971 		  || comp_target_types (location, memb_type, rhstype))
6972 		{
6973 		  int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6974 		  int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
6975 		  /* If this type won't generate any warnings, use it.  */
6976 		  if (lquals == rquals
6977 		      || ((TREE_CODE (ttr) == FUNCTION_TYPE
6978 			   && TREE_CODE (ttl) == FUNCTION_TYPE)
6979 			  ? ((lquals | rquals) == rquals)
6980 			  : ((lquals | rquals) == lquals)))
6981 		    break;
6982 
6983 		  /* Keep looking for a better type, but remember this one.  */
6984 		  if (!marginal_memb)
6985 		    marginal_memb = memb;
6986 		}
6987 	    }
6988 
6989 	  /* Can convert integer zero to any pointer type.  */
6990 	  if (null_pointer_constant)
6991 	    {
6992 	      rhs = null_pointer_node;
6993 	      break;
6994 	    }
6995 	}
6996 
6997       if (memb || marginal_memb)
6998 	{
6999 	  if (!memb)
7000 	    {
7001 	      /* We have only a marginally acceptable member type;
7002 		 it needs a warning.  */
7003 	      tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
7004 	      tree ttr = TREE_TYPE (rhstype);
7005 
7006 	      /* Const and volatile mean something different for function
7007 		 types, so the usual warnings are not appropriate.  */
7008 	      if (TREE_CODE (ttr) == FUNCTION_TYPE
7009 		  && TREE_CODE (ttl) == FUNCTION_TYPE)
7010 		{
7011 		  /* Because const and volatile on functions are
7012 		     restrictions that say the function will not do
7013 		     certain things, it is okay to use a const or volatile
7014 		     function where an ordinary one is wanted, but not
7015 		     vice-versa.  */
7016 		  if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7017 		      & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7018 		    PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7019 					    OPT_Wdiscarded_qualifiers,
7020 					    G_("passing argument %d of %qE "
7021 					       "makes %q#v qualified function "
7022 					       "pointer from unqualified"),
7023 					    G_("assignment makes %q#v qualified "
7024 					       "function pointer from "
7025 					       "unqualified"),
7026 					    G_("initialization makes %q#v qualified "
7027 					       "function pointer from "
7028 					       "unqualified"),
7029 					    G_("return makes %q#v qualified function "
7030 					       "pointer from unqualified"),
7031 					    TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7032 		}
7033 	      else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
7034 		       & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
7035 		PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7036 				        OPT_Wdiscarded_qualifiers,
7037 				        G_("passing argument %d of %qE discards "
7038 					   "%qv qualifier from pointer target type"),
7039 				        G_("assignment discards %qv qualifier "
7040 					   "from pointer target type"),
7041 				        G_("initialization discards %qv qualifier "
7042 					   "from pointer target type"),
7043 				        G_("return discards %qv qualifier from "
7044 					   "pointer target type"),
7045 				        TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7046 
7047 	      memb = marginal_memb;
7048 	    }
7049 
7050 	  if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
7051 	    pedwarn (location, OPT_Wpedantic,
7052 		     "ISO C prohibits argument conversion to union type");
7053 
7054 	  rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
7055 	  return build_constructor_single (type, memb, rhs);
7056 	}
7057     }
7058 
7059   /* Conversions among pointers */
7060   else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
7061 	   && (coder == codel))
7062     {
7063       /* If RHS refers to a built-in declared without a prototype
7064 	 BLTIN is the declaration of the built-in with a prototype
7065 	 and RHSTYPE is set to the actual type of the built-in.  */
7066       tree bltin;
7067       rhstype = type_or_builtin_type (rhs, &bltin);
7068 
7069       tree ttl = TREE_TYPE (type);
7070       tree ttr = TREE_TYPE (rhstype);
7071       tree mvl = ttl;
7072       tree mvr = ttr;
7073       bool is_opaque_pointer;
7074       int target_cmp = 0;   /* Cache comp_target_types () result.  */
7075       addr_space_t asl;
7076       addr_space_t asr;
7077 
7078       if (TREE_CODE (mvl) != ARRAY_TYPE)
7079 	mvl = (TYPE_ATOMIC (mvl)
7080 	       ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
7081 					 TYPE_QUAL_ATOMIC)
7082 	       : TYPE_MAIN_VARIANT (mvl));
7083       if (TREE_CODE (mvr) != ARRAY_TYPE)
7084 	mvr = (TYPE_ATOMIC (mvr)
7085 	       ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
7086 					 TYPE_QUAL_ATOMIC)
7087 	       : TYPE_MAIN_VARIANT (mvr));
7088       /* Opaque pointers are treated like void pointers.  */
7089       is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
7090 
7091       /* The Plan 9 compiler permits a pointer to a struct to be
7092 	 automatically converted into a pointer to an anonymous field
7093 	 within the struct.  */
7094       if (flag_plan9_extensions
7095 	  && RECORD_OR_UNION_TYPE_P (mvl)
7096 	  && RECORD_OR_UNION_TYPE_P (mvr)
7097 	  && mvl != mvr)
7098 	{
7099 	  tree new_rhs = convert_to_anonymous_field (location, type, rhs);
7100 	  if (new_rhs != NULL_TREE)
7101 	    {
7102 	      rhs = new_rhs;
7103 	      rhstype = TREE_TYPE (rhs);
7104 	      coder = TREE_CODE (rhstype);
7105 	      ttr = TREE_TYPE (rhstype);
7106 	      mvr = TYPE_MAIN_VARIANT (ttr);
7107 	    }
7108 	}
7109 
7110       /* C++ does not allow the implicit conversion void* -> T*.  However,
7111 	 for the purpose of reducing the number of false positives, we
7112 	 tolerate the special case of
7113 
7114 		int *p = NULL;
7115 
7116 	 where NULL is typically defined in C to be '(void *) 0'.  */
7117       if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
7118 	warning_at (errtype == ic_argpass ? expr_loc : location,
7119 		    OPT_Wc___compat,
7120 		    "request for implicit conversion "
7121 		    "from %qT to %qT not permitted in C++", rhstype, type);
7122 
7123       /* See if the pointers point to incompatible address spaces.  */
7124       asl = TYPE_ADDR_SPACE (ttl);
7125       asr = TYPE_ADDR_SPACE (ttr);
7126       if (!null_pointer_constant_p (rhs)
7127 	  && asr != asl && !targetm.addr_space.subset_p (asr, asl))
7128 	{
7129 	  switch (errtype)
7130 	    {
7131 	    case ic_argpass:
7132 	      {
7133 		const char msg[] = G_("passing argument %d of %qE from "
7134 				      "pointer to non-enclosed address space");
7135 		if (warnopt)
7136 		  warning_at (expr_loc, warnopt, msg, parmnum, rname);
7137 		else
7138 		  error_at (expr_loc, msg, parmnum, rname);
7139 	      break;
7140 	      }
7141 	    case ic_assign:
7142 	      {
7143 		const char msg[] = G_("assignment from pointer to "
7144 				      "non-enclosed address space");
7145 		if (warnopt)
7146 		  warning_at (location, warnopt, msg);
7147 		else
7148 		  error_at (location, msg);
7149 		break;
7150 	      }
7151 	    case ic_init:
7152 	      {
7153 		const char msg[] = G_("initialization from pointer to "
7154 				      "non-enclosed address space");
7155 		if (warnopt)
7156 		  warning_at (location, warnopt, msg);
7157 		else
7158 		  error_at (location, msg);
7159 		break;
7160 	      }
7161 	    case ic_return:
7162 	      {
7163 		const char msg[] = G_("return from pointer to "
7164 				      "non-enclosed address space");
7165 		if (warnopt)
7166 		  warning_at (location, warnopt, msg);
7167 		else
7168 		  error_at (location, msg);
7169 		break;
7170 	      }
7171 	    default:
7172 	      gcc_unreachable ();
7173 	    }
7174 	  return error_mark_node;
7175 	}
7176 
7177       /* Check if the right-hand side has a format attribute but the
7178 	 left-hand side doesn't.  */
7179       if (warn_suggest_attribute_format
7180 	  && check_missing_format_attribute (type, rhstype))
7181 	{
7182 	  switch (errtype)
7183 	  {
7184 	  case ic_argpass:
7185 	    warning_at (expr_loc, OPT_Wsuggest_attribute_format,
7186 			"argument %d of %qE might be "
7187 			"a candidate for a format attribute",
7188 			parmnum, rname);
7189 	    break;
7190 	  case ic_assign:
7191 	    warning_at (location, OPT_Wsuggest_attribute_format,
7192 			"assignment left-hand side might be "
7193 			"a candidate for a format attribute");
7194 	    break;
7195 	  case ic_init:
7196 	    warning_at (location, OPT_Wsuggest_attribute_format,
7197 			"initialization left-hand side might be "
7198 			"a candidate for a format attribute");
7199 	    break;
7200 	  case ic_return:
7201 	    warning_at (location, OPT_Wsuggest_attribute_format,
7202 			"return type might be "
7203 			"a candidate for a format attribute");
7204 	    break;
7205 	  default:
7206 	    gcc_unreachable ();
7207 	  }
7208 	}
7209 
7210       /* Any non-function converts to a [const][volatile] void *
7211 	 and vice versa; otherwise, targets must be the same.
7212 	 Meanwhile, the lhs target must have all the qualifiers of the rhs.  */
7213       if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
7214 	  || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
7215 	  || (target_cmp = comp_target_types (location, type, rhstype))
7216 	  || is_opaque_pointer
7217 	  || ((c_common_unsigned_type (mvl)
7218 	       == c_common_unsigned_type (mvr))
7219 	      && (c_common_signed_type (mvl)
7220 		  == c_common_signed_type (mvr))
7221 	      && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
7222 	{
7223 	  /* Warn about loss of qualifers from pointers to arrays with
7224 	     qualifiers on the element type. */
7225 	  if (TREE_CODE (ttr) == ARRAY_TYPE)
7226 	    {
7227 	      ttr = strip_array_types (ttr);
7228 	      ttl = strip_array_types (ttl);
7229 
7230 	      if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7231 		  & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7232 		WARNING_FOR_QUALIFIERS (location, expr_loc,
7233 				        OPT_Wdiscarded_array_qualifiers,
7234 				        G_("passing argument %d of %qE discards "
7235 					   "%qv qualifier from pointer target type"),
7236 				        G_("assignment discards %qv qualifier "
7237 					   "from pointer target type"),
7238 				        G_("initialization discards %qv qualifier "
7239 					   "from pointer target type"),
7240 				        G_("return discards %qv qualifier from "
7241 					   "pointer target type"),
7242                                         TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7243             }
7244           else if (pedantic
7245 	      && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
7246 		  ||
7247 		  (VOID_TYPE_P (ttr)
7248 		   && !null_pointer_constant
7249 		   && TREE_CODE (ttl) == FUNCTION_TYPE)))
7250 	    PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
7251 				    G_("ISO C forbids passing argument %d of "
7252 				       "%qE between function pointer "
7253 				       "and %<void *%>"),
7254 				    G_("ISO C forbids assignment between "
7255 				       "function pointer and %<void *%>"),
7256 				    G_("ISO C forbids initialization between "
7257 				       "function pointer and %<void *%>"),
7258 				    G_("ISO C forbids return between function "
7259 				       "pointer and %<void *%>"));
7260 	  /* Const and volatile mean something different for function types,
7261 	     so the usual warnings are not appropriate.  */
7262 	  else if (TREE_CODE (ttr) != FUNCTION_TYPE
7263 		   && TREE_CODE (ttl) != FUNCTION_TYPE)
7264 	    {
7265 	      /* Don't warn about loss of qualifier for conversions from
7266 		 qualified void* to pointers to arrays with corresponding
7267 		 qualifier on the element type. */
7268 	      if (!pedantic)
7269 	        ttl = strip_array_types (ttl);
7270 
7271 	      /* Assignments between atomic and non-atomic objects are OK.  */
7272 	      if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
7273 		  & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
7274 		{
7275 		  PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7276 				          OPT_Wdiscarded_qualifiers,
7277 				          G_("passing argument %d of %qE discards "
7278 					     "%qv qualifier from pointer target type"),
7279 				          G_("assignment discards %qv qualifier "
7280 					     "from pointer target type"),
7281 				          G_("initialization discards %qv qualifier "
7282 					     "from pointer target type"),
7283 				          G_("return discards %qv qualifier from "
7284 					     "pointer target type"),
7285 				          TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
7286 		}
7287 	      /* If this is not a case of ignoring a mismatch in signedness,
7288 		 no warning.  */
7289 	      else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
7290 		       || target_cmp)
7291 		;
7292 	      /* If there is a mismatch, do warn.  */
7293 	      else if (warn_pointer_sign)
7294 		switch (errtype)
7295 		  {
7296 		  case ic_argpass:
7297 		    {
7298 		      auto_diagnostic_group d;
7299 		      range_label_for_type_mismatch rhs_label (rhstype, type);
7300 		      gcc_rich_location richloc (expr_loc, &rhs_label);
7301 		      if (pedwarn (&richloc, OPT_Wpointer_sign,
7302 				   "pointer targets in passing argument %d of "
7303 				   "%qE differ in signedness", parmnum, rname))
7304 			inform_for_arg (fundecl, expr_loc, parmnum, type,
7305 					rhstype);
7306 		    }
7307 		    break;
7308 		  case ic_assign:
7309 		    pedwarn (location, OPT_Wpointer_sign,
7310 			     "pointer targets in assignment from %qT to %qT "
7311 			     "differ in signedness", rhstype, type);
7312 		    break;
7313 		  case ic_init:
7314 		    pedwarn_init (location, OPT_Wpointer_sign,
7315 				  "pointer targets in initialization of %qT "
7316 				  "from %qT differ in signedness", type,
7317 				  rhstype);
7318 		    break;
7319 		  case ic_return:
7320 		    pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
7321 			     "returning %qT from a function with return type "
7322 			     "%qT differ in signedness", rhstype, type);
7323 		    break;
7324 		  default:
7325 		    gcc_unreachable ();
7326 		  }
7327 	    }
7328 	  else if (TREE_CODE (ttl) == FUNCTION_TYPE
7329 		   && TREE_CODE (ttr) == FUNCTION_TYPE)
7330 	    {
7331 	      /* Because const and volatile on functions are restrictions
7332 		 that say the function will not do certain things,
7333 		 it is okay to use a const or volatile function
7334 		 where an ordinary one is wanted, but not vice-versa.  */
7335 	      if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
7336 		  & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
7337 		PEDWARN_FOR_QUALIFIERS (location, expr_loc,
7338 				        OPT_Wdiscarded_qualifiers,
7339 				        G_("passing argument %d of %qE makes "
7340 					   "%q#v qualified function pointer "
7341 					   "from unqualified"),
7342 				        G_("assignment makes %q#v qualified function "
7343 					   "pointer from unqualified"),
7344 				        G_("initialization makes %q#v qualified "
7345 					   "function pointer from unqualified"),
7346 				        G_("return makes %q#v qualified function "
7347 					   "pointer from unqualified"),
7348 				        TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
7349 	    }
7350 	}
7351       /* Avoid warning about the volatile ObjC EH puts on decls.  */
7352       else if (!objc_ok)
7353 	{
7354 	  switch (errtype)
7355 	    {
7356 	    case ic_argpass:
7357 	      {
7358 		auto_diagnostic_group d;
7359 		range_label_for_type_mismatch rhs_label (rhstype, type);
7360 		gcc_rich_location richloc (expr_loc, &rhs_label);
7361 		if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7362 			     "passing argument %d of %qE from incompatible "
7363 			     "pointer type", parmnum, rname))
7364 		  inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7365 	      }
7366 	      break;
7367 	    case ic_assign:
7368 	      if (bltin)
7369 		pedwarn (location, OPT_Wincompatible_pointer_types,
7370 			 "assignment to %qT from pointer to "
7371 			 "%qD with incompatible type %qT",
7372 			 type, bltin, rhstype);
7373 	      else
7374 		pedwarn (location, OPT_Wincompatible_pointer_types,
7375 			 "assignment to %qT from incompatible pointer type %qT",
7376 			 type, rhstype);
7377 	      break;
7378 	    case ic_init:
7379 	      if (bltin)
7380 		pedwarn_init (location, OPT_Wincompatible_pointer_types,
7381 			      "initialization of %qT from pointer to "
7382 			      "%qD with incompatible type %qT",
7383 			      type, bltin, rhstype);
7384 	      else
7385 		pedwarn_init (location, OPT_Wincompatible_pointer_types,
7386 			      "initialization of %qT from incompatible "
7387 			      "pointer type %qT",
7388 			      type, rhstype);
7389 	      break;
7390 	    case ic_return:
7391 	      if (bltin)
7392 		pedwarn (location, OPT_Wincompatible_pointer_types,
7393 			 "returning pointer to %qD of type %qT from "
7394 			 "a function with incompatible type %qT",
7395 			 bltin, rhstype, type);
7396 	      else
7397 		pedwarn (location, OPT_Wincompatible_pointer_types,
7398 			 "returning %qT from a function with incompatible "
7399 			 "return type %qT", rhstype, type);
7400 	      break;
7401 	    default:
7402 	      gcc_unreachable ();
7403 	    }
7404 	}
7405 
7406       /* If RHS isn't an address, check pointer or array of packed
7407 	 struct or union.  */
7408       warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
7409 
7410       return convert (type, rhs);
7411     }
7412   else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7413     {
7414       /* ??? This should not be an error when inlining calls to
7415 	 unprototyped functions.  */
7416       const char msg[] = "invalid use of non-lvalue array";
7417       if (warnopt)
7418 	warning_at (location, warnopt, msg);
7419       else
7420 	error_at (location, msg);
7421       return error_mark_node;
7422     }
7423   else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
7424     {
7425       /* An explicit constant 0 can convert to a pointer,
7426 	 or one that results from arithmetic, even including
7427 	 a cast to integer type.  */
7428       if (!null_pointer_constant)
7429 	switch (errtype)
7430 	  {
7431 	  case ic_argpass:
7432 	    {
7433 	      auto_diagnostic_group d;
7434 	      range_label_for_type_mismatch rhs_label (rhstype, type);
7435 	      gcc_rich_location richloc (expr_loc, &rhs_label);
7436 	      if (pedwarn (&richloc, OPT_Wint_conversion,
7437 			   "passing argument %d of %qE makes pointer from "
7438 			   "integer without a cast", parmnum, rname))
7439 		inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7440 	    }
7441 	    break;
7442 	  case ic_assign:
7443 	    pedwarn (location, OPT_Wint_conversion,
7444 		     "assignment to %qT from %qT makes pointer from integer "
7445 		     "without a cast", type, rhstype);
7446 	    break;
7447 	  case ic_init:
7448 	    pedwarn_init (location, OPT_Wint_conversion,
7449 			  "initialization of %qT from %qT makes pointer from "
7450 			  "integer without a cast", type, rhstype);
7451 	    break;
7452 	  case ic_return:
7453 	    pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7454 		     "function with return type %qT makes pointer from "
7455 		     "integer without a cast", rhstype, type);
7456 	    break;
7457 	  default:
7458 	    gcc_unreachable ();
7459 	  }
7460 
7461       return convert (type, rhs);
7462     }
7463   else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
7464     {
7465       switch (errtype)
7466 	{
7467 	case ic_argpass:
7468 	  {
7469 	    auto_diagnostic_group d;
7470 	    range_label_for_type_mismatch rhs_label (rhstype, type);
7471 	    gcc_rich_location richloc (expr_loc, &rhs_label);
7472 	    if (pedwarn (&richloc, OPT_Wint_conversion,
7473 			 "passing argument %d of %qE makes integer from "
7474 			 "pointer without a cast", parmnum, rname))
7475 	      inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7476 	  }
7477 	  break;
7478 	case ic_assign:
7479 	  pedwarn (location, OPT_Wint_conversion,
7480 		   "assignment to %qT from %qT makes integer from pointer "
7481 		   "without a cast", type, rhstype);
7482 	  break;
7483 	case ic_init:
7484 	  pedwarn_init (location, OPT_Wint_conversion,
7485 			"initialization of %qT from %qT makes integer from "
7486 			"pointer without a cast", type, rhstype);
7487 	  break;
7488 	case ic_return:
7489 	  pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7490 		   "function with return type %qT makes integer from "
7491 		   "pointer without a cast", rhstype, type);
7492 	  break;
7493 	default:
7494 	  gcc_unreachable ();
7495 	}
7496 
7497       return convert (type, rhs);
7498     }
7499   else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
7500     {
7501       tree ret;
7502       bool save = in_late_binary_op;
7503       in_late_binary_op = true;
7504       ret = convert (type, rhs);
7505       in_late_binary_op = save;
7506       return ret;
7507     }
7508 
7509   switch (errtype)
7510     {
7511     case ic_argpass:
7512       {
7513 	auto_diagnostic_group d;
7514 	range_label_for_type_mismatch rhs_label (rhstype, type);
7515 	gcc_rich_location richloc (expr_loc, &rhs_label);
7516 	const char msg[] = G_("incompatible type for argument %d of %qE");
7517 	if (warnopt)
7518 	  warning_at (expr_loc, warnopt, msg, parmnum, rname);
7519 	else
7520 	  error_at (&richloc, msg, parmnum, rname);
7521 	inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7522       }
7523       break;
7524     case ic_assign:
7525       {
7526 	const char msg[]
7527 	  = G_("incompatible types when assigning to type %qT from type %qT");
7528 	if (warnopt)
7529 	  warning_at (expr_loc, 0, msg, type, rhstype);
7530 	else
7531 	  error_at (expr_loc, msg, type, rhstype);
7532 	break;
7533       }
7534     case ic_init:
7535       {
7536 	const char msg[]
7537 	  = G_("incompatible types when initializing type %qT using type %qT");
7538 	if (warnopt)
7539 	  warning_at (location, 0, msg, type, rhstype);
7540 	else
7541 	  error_at (location, msg, type, rhstype);
7542 	break;
7543       }
7544     case ic_return:
7545       {
7546 	const char msg[]
7547 	  = G_("incompatible types when returning type %qT but %qT was expected");
7548 	if (warnopt)
7549 	  warning_at (location, 0, msg, rhstype, type);
7550 	else
7551 	  error_at (location, msg, rhstype, type);
7552 	break;
7553       }
7554     default:
7555       gcc_unreachable ();
7556     }
7557 
7558   return error_mark_node;
7559 }
7560 
7561 /* If VALUE is a compound expr all of whose expressions are constant, then
7562    return its value.  Otherwise, return error_mark_node.
7563 
7564    This is for handling COMPOUND_EXPRs as initializer elements
7565    which is allowed with a warning when -pedantic is specified.  */
7566 
7567 static tree
valid_compound_expr_initializer(tree value,tree endtype)7568 valid_compound_expr_initializer (tree value, tree endtype)
7569 {
7570   if (TREE_CODE (value) == COMPOUND_EXPR)
7571     {
7572       if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7573 	  == error_mark_node)
7574 	return error_mark_node;
7575       return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7576 					      endtype);
7577     }
7578   else if (!initializer_constant_valid_p (value, endtype))
7579     return error_mark_node;
7580   else
7581     return value;
7582 }
7583 
7584 /* Perform appropriate conversions on the initial value of a variable,
7585    store it in the declaration DECL,
7586    and print any error messages that are appropriate.
7587    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7588    If the init is invalid, store an ERROR_MARK.
7589 
7590    INIT_LOC is the location of the initial value.  */
7591 
7592 void
store_init_value(location_t init_loc,tree decl,tree init,tree origtype)7593 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
7594 {
7595   tree value, type;
7596   bool npc = false;
7597 
7598   /* If variable's type was invalidly declared, just ignore it.  */
7599 
7600   type = TREE_TYPE (decl);
7601   if (TREE_CODE (type) == ERROR_MARK)
7602     return;
7603 
7604   /* Digest the specified initializer into an expression.  */
7605 
7606   if (init)
7607     npc = null_pointer_constant_p (init);
7608   value = digest_init (init_loc, type, init, origtype, npc,
7609       		       true, TREE_STATIC (decl));
7610 
7611   /* Store the expression if valid; else report error.  */
7612 
7613   if (!in_system_header_at (input_location)
7614       && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
7615     warning (OPT_Wtraditional, "traditional C rejects automatic "
7616 	     "aggregate initialization");
7617 
7618   if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7619     DECL_INITIAL (decl) = value;
7620 
7621   /* ANSI wants warnings about out-of-range constant initializers.  */
7622   STRIP_TYPE_NOPS (value);
7623   if (TREE_STATIC (decl))
7624     constant_expression_warning (value);
7625 
7626   /* Check if we need to set array size from compound literal size.  */
7627   if (TREE_CODE (type) == ARRAY_TYPE
7628       && TYPE_DOMAIN (type) == NULL_TREE
7629       && value != error_mark_node)
7630     {
7631       tree inside_init = init;
7632 
7633       STRIP_TYPE_NOPS (inside_init);
7634       inside_init = fold (inside_init);
7635 
7636       if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7637 	{
7638 	  tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7639 
7640 	  if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
7641 	    {
7642 	      /* For int foo[] = (int [3]){1}; we need to set array size
7643 		 now since later on array initializer will be just the
7644 		 brace enclosed list of the compound literal.  */
7645 	      tree etype = strip_array_types (TREE_TYPE (decl));
7646 	      type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7647 	      TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
7648 	      layout_type (type);
7649 	      layout_decl (cldecl, 0);
7650 	      TREE_TYPE (decl)
7651 		= c_build_qualified_type (type, TYPE_QUALS (etype));
7652 	    }
7653 	}
7654     }
7655 }
7656 
7657 /* Methods for storing and printing names for error messages.  */
7658 
7659 /* Implement a spelling stack that allows components of a name to be pushed
7660    and popped.  Each element on the stack is this structure.  */
7661 
7662 struct spelling
7663 {
7664   int kind;
7665   union
7666     {
7667       unsigned HOST_WIDE_INT i;
7668       const char *s;
7669     } u;
7670 };
7671 
7672 #define SPELLING_STRING 1
7673 #define SPELLING_MEMBER 2
7674 #define SPELLING_BOUNDS 3
7675 
7676 static struct spelling *spelling;	/* Next stack element (unused).  */
7677 static struct spelling *spelling_base;	/* Spelling stack base.  */
7678 static int spelling_size;		/* Size of the spelling stack.  */
7679 
7680 /* Macros to save and restore the spelling stack around push_... functions.
7681    Alternative to SAVE_SPELLING_STACK.  */
7682 
7683 #define SPELLING_DEPTH() (spelling - spelling_base)
7684 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
7685 
7686 /* Push an element on the spelling stack with type KIND and assign VALUE
7687    to MEMBER.  */
7688 
7689 #define PUSH_SPELLING(KIND, VALUE, MEMBER)				\
7690 {									\
7691   int depth = SPELLING_DEPTH ();					\
7692 									\
7693   if (depth >= spelling_size)						\
7694     {									\
7695       spelling_size += 10;						\
7696       spelling_base = XRESIZEVEC (struct spelling, spelling_base,	\
7697 				  spelling_size);			\
7698       RESTORE_SPELLING_DEPTH (depth);					\
7699     }									\
7700 									\
7701   spelling->kind = (KIND);						\
7702   spelling->MEMBER = (VALUE);						\
7703   spelling++;								\
7704 }
7705 
7706 /* Push STRING on the stack.  Printed literally.  */
7707 
7708 static void
push_string(const char * string)7709 push_string (const char *string)
7710 {
7711   PUSH_SPELLING (SPELLING_STRING, string, u.s);
7712 }
7713 
7714 /* Push a member name on the stack.  Printed as '.' STRING.  */
7715 
7716 static void
push_member_name(tree decl)7717 push_member_name (tree decl)
7718 {
7719   const char *const string
7720     = (DECL_NAME (decl)
7721        ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7722        : _("<anonymous>"));
7723   PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7724 }
7725 
7726 /* Push an array bounds on the stack.  Printed as [BOUNDS].  */
7727 
7728 static void
push_array_bounds(unsigned HOST_WIDE_INT bounds)7729 push_array_bounds (unsigned HOST_WIDE_INT bounds)
7730 {
7731   PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7732 }
7733 
7734 /* Compute the maximum size in bytes of the printed spelling.  */
7735 
7736 static int
spelling_length(void)7737 spelling_length (void)
7738 {
7739   int size = 0;
7740   struct spelling *p;
7741 
7742   for (p = spelling_base; p < spelling; p++)
7743     {
7744       if (p->kind == SPELLING_BOUNDS)
7745 	size += 25;
7746       else
7747 	size += strlen (p->u.s) + 1;
7748     }
7749 
7750   return size;
7751 }
7752 
7753 /* Print the spelling to BUFFER and return it.  */
7754 
7755 static char *
print_spelling(char * buffer)7756 print_spelling (char *buffer)
7757 {
7758   char *d = buffer;
7759   struct spelling *p;
7760 
7761   for (p = spelling_base; p < spelling; p++)
7762     if (p->kind == SPELLING_BOUNDS)
7763       {
7764 	sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
7765 	d += strlen (d);
7766       }
7767     else
7768       {
7769 	const char *s;
7770 	if (p->kind == SPELLING_MEMBER)
7771 	  *d++ = '.';
7772 	for (s = p->u.s; (*d = *s++); d++)
7773 	  ;
7774       }
7775   *d++ = '\0';
7776   return buffer;
7777 }
7778 
7779 /* Digest the parser output INIT as an initializer for type TYPE.
7780    Return a C expression of type TYPE to represent the initial value.
7781 
7782    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7783 
7784    NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7785 
7786    If INIT is a string constant, STRICT_STRING is true if it is
7787    unparenthesized or we should not warn here for it being parenthesized.
7788    For other types of INIT, STRICT_STRING is not used.
7789 
7790    INIT_LOC is the location of the INIT.
7791 
7792    REQUIRE_CONSTANT requests an error if non-constant initializers or
7793    elements are seen.  */
7794 
7795 static tree
digest_init(location_t init_loc,tree type,tree init,tree origtype,bool null_pointer_constant,bool strict_string,int require_constant)7796 digest_init (location_t init_loc, tree type, tree init, tree origtype,
7797     	     bool null_pointer_constant, bool strict_string,
7798 	     int require_constant)
7799 {
7800   enum tree_code code = TREE_CODE (type);
7801   tree inside_init = init;
7802   tree semantic_type = NULL_TREE;
7803   bool maybe_const = true;
7804 
7805   if (type == error_mark_node
7806       || !init
7807       || error_operand_p (init))
7808     return error_mark_node;
7809 
7810   STRIP_TYPE_NOPS (inside_init);
7811 
7812   if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7813     {
7814       semantic_type = TREE_TYPE (inside_init);
7815       inside_init = TREE_OPERAND (inside_init, 0);
7816     }
7817   inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7818 
7819   /* Initialization of an array of chars from a string constant
7820      optionally enclosed in braces.  */
7821 
7822   if (code == ARRAY_TYPE && inside_init
7823       && TREE_CODE (inside_init) == STRING_CST)
7824     {
7825       tree typ1
7826 	= (TYPE_ATOMIC (TREE_TYPE (type))
7827 	   ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7828 				     TYPE_QUAL_ATOMIC)
7829 	   : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
7830       /* Note that an array could be both an array of character type
7831 	 and an array of wchar_t if wchar_t is signed char or unsigned
7832 	 char.  */
7833       bool char_array = (typ1 == char_type_node
7834 			 || typ1 == signed_char_type_node
7835 			 || typ1 == unsigned_char_type_node);
7836       bool wchar_array = !!comptypes (typ1, wchar_type_node);
7837       bool char16_array = !!comptypes (typ1, char16_type_node);
7838       bool char32_array = !!comptypes (typ1, char32_type_node);
7839 
7840       if (char_array || wchar_array || char16_array || char32_array)
7841 	{
7842 	  struct c_expr expr;
7843 	  tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
7844 	  bool incompat_string_cst = false;
7845 	  expr.value = inside_init;
7846 	  expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
7847 	  expr.original_type = NULL;
7848 	  maybe_warn_string_init (init_loc, type, expr);
7849 
7850 	  if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
7851 	    pedwarn_init (init_loc, OPT_Wpedantic,
7852 			  "initialization of a flexible array member");
7853 
7854 	  if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7855 			 TYPE_MAIN_VARIANT (type)))
7856 	    return inside_init;
7857 
7858 	  if (char_array)
7859 	    {
7860 	      if (typ2 != char_type_node)
7861 		incompat_string_cst = true;
7862 	    }
7863 	  else if (!comptypes (typ1, typ2))
7864 	    incompat_string_cst = true;
7865 
7866           if (incompat_string_cst)
7867             {
7868 	      error_init (init_loc, "cannot initialize array of %qT from "
7869 			  "a string literal with type array of %qT",
7870 			  typ1, typ2);
7871 	      return error_mark_node;
7872             }
7873 
7874 	  if (TYPE_DOMAIN (type) != NULL_TREE
7875 	      && TYPE_SIZE (type) != NULL_TREE
7876 	      && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7877 	    {
7878 	      unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7879 	      unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT;
7880 
7881 	      /* Subtract the size of a single (possibly wide) character
7882 		 because it's ok to ignore the terminating null char
7883 		 that is counted in the length of the constant.  */
7884 	      if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0)
7885 		pedwarn_init (init_loc, 0,
7886 			      ("initializer-string for array of %qT "
7887 			       "is too long"), typ1);
7888 	      else if (warn_cxx_compat
7889 		       && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7890 		warning_at (init_loc, OPT_Wc___compat,
7891 			    ("initializer-string for array of %qT "
7892 			     "is too long for C++"), typ1);
7893 	      if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
7894 		{
7895 		  unsigned HOST_WIDE_INT size
7896 		    = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7897 		  const char *p = TREE_STRING_POINTER (inside_init);
7898 
7899 		  inside_init = build_string (size, p);
7900 		}
7901 	    }
7902 
7903 	  TREE_TYPE (inside_init) = type;
7904 	  return inside_init;
7905 	}
7906       else if (INTEGRAL_TYPE_P (typ1))
7907 	{
7908 	  error_init (init_loc, "array of inappropriate type initialized "
7909 		      "from string constant");
7910 	  return error_mark_node;
7911 	}
7912     }
7913 
7914   /* Build a VECTOR_CST from a *constant* vector constructor.  If the
7915      vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7916      below and handle as a constructor.  */
7917   if (code == VECTOR_TYPE
7918       && VECTOR_TYPE_P (TREE_TYPE (inside_init))
7919       && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
7920       && TREE_CONSTANT (inside_init))
7921     {
7922       if (TREE_CODE (inside_init) == VECTOR_CST
7923 	  && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7924 			TYPE_MAIN_VARIANT (type)))
7925 	return inside_init;
7926 
7927       if (TREE_CODE (inside_init) == CONSTRUCTOR)
7928 	{
7929 	  unsigned HOST_WIDE_INT ix;
7930 	  tree value;
7931 	  bool constant_p = true;
7932 
7933 	  /* Iterate through elements and check if all constructor
7934 	     elements are *_CSTs.  */
7935 	  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7936 	    if (!CONSTANT_CLASS_P (value))
7937 	      {
7938 		constant_p = false;
7939 		break;
7940 	      }
7941 
7942 	  if (constant_p)
7943 	    return build_vector_from_ctor (type,
7944 					   CONSTRUCTOR_ELTS (inside_init));
7945 	}
7946     }
7947 
7948   if (warn_sequence_point)
7949     verify_sequence_points (inside_init);
7950 
7951   /* Any type can be initialized
7952      from an expression of the same type, optionally with braces.  */
7953 
7954   if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
7955       && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7956 		     TYPE_MAIN_VARIANT (type))
7957 	  || (code == ARRAY_TYPE
7958 	      && comptypes (TREE_TYPE (inside_init), type))
7959 	  || (gnu_vector_type_p (type)
7960 	      && comptypes (TREE_TYPE (inside_init), type))
7961 	  || (code == POINTER_TYPE
7962 	      && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
7963 	      && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
7964 			    TREE_TYPE (type)))))
7965     {
7966       if (code == POINTER_TYPE)
7967 	{
7968 	  if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7969 	    {
7970 	      if (TREE_CODE (inside_init) == STRING_CST
7971 		  || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7972 		inside_init = array_to_pointer_conversion
7973 		  (init_loc, inside_init);
7974 	      else
7975 		{
7976 		  error_init (init_loc, "invalid use of non-lvalue array");
7977 		  return error_mark_node;
7978 		}
7979 	    }
7980 	}
7981 
7982       if (code == VECTOR_TYPE)
7983 	/* Although the types are compatible, we may require a
7984 	   conversion.  */
7985 	inside_init = convert (type, inside_init);
7986 
7987       if (require_constant
7988 	  && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7989 	{
7990 	  /* As an extension, allow initializing objects with static storage
7991 	     duration with compound literals (which are then treated just as
7992 	     the brace enclosed list they contain).  Also allow this for
7993 	     vectors, as we can only assign them with compound literals.  */
7994 	  if (flag_isoc99 && code != VECTOR_TYPE)
7995 	    pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7996 			  "is not constant");
7997 	  tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7998 	  inside_init = DECL_INITIAL (decl);
7999 	}
8000 
8001       if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
8002 	  && TREE_CODE (inside_init) != CONSTRUCTOR)
8003 	{
8004 	  error_init (init_loc, "array initialized from non-constant array "
8005 		      "expression");
8006 	  return error_mark_node;
8007 	}
8008 
8009       /* Compound expressions can only occur here if -Wpedantic or
8010 	 -pedantic-errors is specified.  In the later case, we always want
8011 	 an error.  In the former case, we simply want a warning.  */
8012       if (require_constant && pedantic
8013 	  && TREE_CODE (inside_init) == COMPOUND_EXPR)
8014 	{
8015 	  inside_init
8016 	    = valid_compound_expr_initializer (inside_init,
8017 					       TREE_TYPE (inside_init));
8018 	  if (inside_init == error_mark_node)
8019 	    error_init (init_loc, "initializer element is not constant");
8020 	  else
8021 	    pedwarn_init (init_loc, OPT_Wpedantic,
8022 			  "initializer element is not constant");
8023 	  if (flag_pedantic_errors)
8024 	    inside_init = error_mark_node;
8025 	}
8026       else if (require_constant
8027 	       && !initializer_constant_valid_p (inside_init,
8028 						 TREE_TYPE (inside_init)))
8029 	{
8030 	  error_init (init_loc, "initializer element is not constant");
8031 	  inside_init = error_mark_node;
8032 	}
8033       else if (require_constant && !maybe_const)
8034 	pedwarn_init (init_loc, OPT_Wpedantic,
8035 		      "initializer element is not a constant expression");
8036 
8037       /* Added to enable additional -Wsuggest-attribute=format warnings.  */
8038       if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
8039 	inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
8040 					      type, inside_init, origtype,
8041 					      ic_init, null_pointer_constant,
8042 					      NULL_TREE, NULL_TREE, 0);
8043       return inside_init;
8044     }
8045 
8046   /* Handle scalar types, including conversions.  */
8047 
8048   if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
8049       || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
8050       || code == COMPLEX_TYPE || code == VECTOR_TYPE)
8051     {
8052       if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
8053 	  && (TREE_CODE (init) == STRING_CST
8054 	      || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
8055 	inside_init = init = array_to_pointer_conversion (init_loc, init);
8056       if (semantic_type)
8057 	inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8058 			      inside_init);
8059       inside_init
8060 	= convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
8061 				  inside_init, origtype, ic_init,
8062 				  null_pointer_constant, NULL_TREE, NULL_TREE,
8063 				  0);
8064 
8065       /* Check to see if we have already given an error message.  */
8066       if (inside_init == error_mark_node)
8067 	;
8068       else if (require_constant && !TREE_CONSTANT (inside_init))
8069 	{
8070 	  error_init (init_loc, "initializer element is not constant");
8071 	  inside_init = error_mark_node;
8072 	}
8073       else if (require_constant
8074 	       && !initializer_constant_valid_p (inside_init,
8075 						 TREE_TYPE (inside_init)))
8076 	{
8077 	  error_init (init_loc, "initializer element is not computable at "
8078 		      "load time");
8079 	  inside_init = error_mark_node;
8080 	}
8081       else if (require_constant && !maybe_const)
8082 	pedwarn_init (init_loc, OPT_Wpedantic,
8083 		      "initializer element is not a constant expression");
8084 
8085       return inside_init;
8086     }
8087 
8088   /* Come here only for records and arrays.  */
8089 
8090   if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
8091     {
8092       error_init (init_loc, "variable-sized object may not be initialized");
8093       return error_mark_node;
8094     }
8095 
8096   error_init (init_loc, "invalid initializer");
8097   return error_mark_node;
8098 }
8099 
8100 /* Handle initializers that use braces.  */
8101 
8102 /* Type of object we are accumulating a constructor for.
8103    This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE.  */
8104 static tree constructor_type;
8105 
8106 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
8107    left to fill.  */
8108 static tree constructor_fields;
8109 
8110 /* For an ARRAY_TYPE, this is the specified index
8111    at which to store the next element we get.  */
8112 static tree constructor_index;
8113 
8114 /* For an ARRAY_TYPE, this is the maximum index.  */
8115 static tree constructor_max_index;
8116 
8117 /* For a RECORD_TYPE, this is the first field not yet written out.  */
8118 static tree constructor_unfilled_fields;
8119 
8120 /* For an ARRAY_TYPE, this is the index of the first element
8121    not yet written out.  */
8122 static tree constructor_unfilled_index;
8123 
8124 /* In a RECORD_TYPE, the byte index of the next consecutive field.
8125    This is so we can generate gaps between fields, when appropriate.  */
8126 static tree constructor_bit_index;
8127 
8128 /* If we are saving up the elements rather than allocating them,
8129    this is the list of elements so far (in reverse order,
8130    most recent first).  */
8131 static vec<constructor_elt, va_gc> *constructor_elements;
8132 
8133 /* 1 if constructor should be incrementally stored into a constructor chain,
8134    0 if all the elements should be kept in AVL tree.  */
8135 static int constructor_incremental;
8136 
8137 /* 1 if so far this constructor's elements are all compile-time constants.  */
8138 static int constructor_constant;
8139 
8140 /* 1 if so far this constructor's elements are all valid address constants.  */
8141 static int constructor_simple;
8142 
8143 /* 1 if this constructor has an element that cannot be part of a
8144    constant expression.  */
8145 static int constructor_nonconst;
8146 
8147 /* 1 if this constructor is erroneous so far.  */
8148 static int constructor_erroneous;
8149 
8150 /* 1 if this constructor is the universal zero initializer { 0 }.  */
8151 static int constructor_zeroinit;
8152 
8153 /* Structure for managing pending initializer elements, organized as an
8154    AVL tree.  */
8155 
8156 struct init_node
8157 {
8158   struct init_node *left, *right;
8159   struct init_node *parent;
8160   int balance;
8161   tree purpose;
8162   tree value;
8163   tree origtype;
8164 };
8165 
8166 /* Tree of pending elements at this constructor level.
8167    These are elements encountered out of order
8168    which belong at places we haven't reached yet in actually
8169    writing the output.
8170    Will never hold tree nodes across GC runs.  */
8171 static struct init_node *constructor_pending_elts;
8172 
8173 /* The SPELLING_DEPTH of this constructor.  */
8174 static int constructor_depth;
8175 
8176 /* DECL node for which an initializer is being read.
8177    0 means we are reading a constructor expression
8178    such as (struct foo) {...}.  */
8179 static tree constructor_decl;
8180 
8181 /* Nonzero if this is an initializer for a top-level decl.  */
8182 static int constructor_top_level;
8183 
8184 /* Nonzero if there were any member designators in this initializer.  */
8185 static int constructor_designated;
8186 
8187 /* Nesting depth of designator list.  */
8188 static int designator_depth;
8189 
8190 /* Nonzero if there were diagnosed errors in this designator list.  */
8191 static int designator_erroneous;
8192 
8193 
8194 /* This stack has a level for each implicit or explicit level of
8195    structuring in the initializer, including the outermost one.  It
8196    saves the values of most of the variables above.  */
8197 
8198 struct constructor_range_stack;
8199 
8200 struct constructor_stack
8201 {
8202   struct constructor_stack *next;
8203   tree type;
8204   tree fields;
8205   tree index;
8206   tree max_index;
8207   tree unfilled_index;
8208   tree unfilled_fields;
8209   tree bit_index;
8210   vec<constructor_elt, va_gc> *elements;
8211   struct init_node *pending_elts;
8212   int offset;
8213   int depth;
8214   /* If value nonzero, this value should replace the entire
8215      constructor at this level.  */
8216   struct c_expr replacement_value;
8217   struct constructor_range_stack *range_stack;
8218   char constant;
8219   char simple;
8220   char nonconst;
8221   char implicit;
8222   char erroneous;
8223   char outer;
8224   char incremental;
8225   char designated;
8226   int designator_depth;
8227 };
8228 
8229 static struct constructor_stack *constructor_stack;
8230 
8231 /* This stack represents designators from some range designator up to
8232    the last designator in the list.  */
8233 
8234 struct constructor_range_stack
8235 {
8236   struct constructor_range_stack *next, *prev;
8237   struct constructor_stack *stack;
8238   tree range_start;
8239   tree index;
8240   tree range_end;
8241   tree fields;
8242 };
8243 
8244 static struct constructor_range_stack *constructor_range_stack;
8245 
8246 /* This stack records separate initializers that are nested.
8247    Nested initializers can't happen in ANSI C, but GNU C allows them
8248    in cases like { ... (struct foo) { ... } ... }.  */
8249 
8250 struct initializer_stack
8251 {
8252   struct initializer_stack *next;
8253   tree decl;
8254   struct constructor_stack *constructor_stack;
8255   struct constructor_range_stack *constructor_range_stack;
8256   vec<constructor_elt, va_gc> *elements;
8257   struct spelling *spelling;
8258   struct spelling *spelling_base;
8259   int spelling_size;
8260   char top_level;
8261   char require_constant_value;
8262   char require_constant_elements;
8263   rich_location *missing_brace_richloc;
8264 };
8265 
8266 static struct initializer_stack *initializer_stack;
8267 
8268 /* Prepare to parse and output the initializer for variable DECL.  */
8269 
8270 void
start_init(tree decl,tree asmspec_tree ATTRIBUTE_UNUSED,int top_level,rich_location * richloc)8271 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
8272 	    rich_location *richloc)
8273 {
8274   const char *locus;
8275   struct initializer_stack *p = XNEW (struct initializer_stack);
8276 
8277   p->decl = constructor_decl;
8278   p->require_constant_value = require_constant_value;
8279   p->require_constant_elements = require_constant_elements;
8280   p->constructor_stack = constructor_stack;
8281   p->constructor_range_stack = constructor_range_stack;
8282   p->elements = constructor_elements;
8283   p->spelling = spelling;
8284   p->spelling_base = spelling_base;
8285   p->spelling_size = spelling_size;
8286   p->top_level = constructor_top_level;
8287   p->next = initializer_stack;
8288   p->missing_brace_richloc = richloc;
8289   initializer_stack = p;
8290 
8291   constructor_decl = decl;
8292   constructor_designated = 0;
8293   constructor_top_level = top_level;
8294 
8295   if (decl != NULL_TREE && decl != error_mark_node)
8296     {
8297       require_constant_value = TREE_STATIC (decl);
8298       require_constant_elements
8299 	= ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
8300 	   /* For a scalar, you can always use any value to initialize,
8301 	      even within braces.  */
8302 	   && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
8303       locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
8304     }
8305   else
8306     {
8307       require_constant_value = 0;
8308       require_constant_elements = 0;
8309       locus = _("(anonymous)");
8310     }
8311 
8312   constructor_stack = 0;
8313   constructor_range_stack = 0;
8314 
8315   found_missing_braces = 0;
8316 
8317   spelling_base = 0;
8318   spelling_size = 0;
8319   RESTORE_SPELLING_DEPTH (0);
8320 
8321   if (locus)
8322     push_string (locus);
8323 }
8324 
8325 void
finish_init(void)8326 finish_init (void)
8327 {
8328   struct initializer_stack *p = initializer_stack;
8329 
8330   /* Free the whole constructor stack of this initializer.  */
8331   while (constructor_stack)
8332     {
8333       struct constructor_stack *q = constructor_stack;
8334       constructor_stack = q->next;
8335       free (q);
8336     }
8337 
8338   gcc_assert (!constructor_range_stack);
8339 
8340   /* Pop back to the data of the outer initializer (if any).  */
8341   free (spelling_base);
8342 
8343   constructor_decl = p->decl;
8344   require_constant_value = p->require_constant_value;
8345   require_constant_elements = p->require_constant_elements;
8346   constructor_stack = p->constructor_stack;
8347   constructor_range_stack = p->constructor_range_stack;
8348   constructor_elements = p->elements;
8349   spelling = p->spelling;
8350   spelling_base = p->spelling_base;
8351   spelling_size = p->spelling_size;
8352   constructor_top_level = p->top_level;
8353   initializer_stack = p->next;
8354   free (p);
8355 }
8356 
8357 /* Call here when we see the initializer is surrounded by braces.
8358    This is instead of a call to push_init_level;
8359    it is matched by a call to pop_init_level.
8360 
8361    TYPE is the type to initialize, for a constructor expression.
8362    For an initializer for a decl, TYPE is zero.  */
8363 
8364 void
really_start_incremental_init(tree type)8365 really_start_incremental_init (tree type)
8366 {
8367   struct constructor_stack *p = XNEW (struct constructor_stack);
8368 
8369   if (type == NULL_TREE)
8370     type = TREE_TYPE (constructor_decl);
8371 
8372   if (VECTOR_TYPE_P (type)
8373       && TYPE_VECTOR_OPAQUE (type))
8374     error ("opaque vector types cannot be initialized");
8375 
8376   p->type = constructor_type;
8377   p->fields = constructor_fields;
8378   p->index = constructor_index;
8379   p->max_index = constructor_max_index;
8380   p->unfilled_index = constructor_unfilled_index;
8381   p->unfilled_fields = constructor_unfilled_fields;
8382   p->bit_index = constructor_bit_index;
8383   p->elements = constructor_elements;
8384   p->constant = constructor_constant;
8385   p->simple = constructor_simple;
8386   p->nonconst = constructor_nonconst;
8387   p->erroneous = constructor_erroneous;
8388   p->pending_elts = constructor_pending_elts;
8389   p->depth = constructor_depth;
8390   p->replacement_value.value = 0;
8391   p->replacement_value.original_code = ERROR_MARK;
8392   p->replacement_value.original_type = NULL;
8393   p->implicit = 0;
8394   p->range_stack = 0;
8395   p->outer = 0;
8396   p->incremental = constructor_incremental;
8397   p->designated = constructor_designated;
8398   p->designator_depth = designator_depth;
8399   p->next = 0;
8400   constructor_stack = p;
8401 
8402   constructor_constant = 1;
8403   constructor_simple = 1;
8404   constructor_nonconst = 0;
8405   constructor_depth = SPELLING_DEPTH ();
8406   constructor_elements = NULL;
8407   constructor_pending_elts = 0;
8408   constructor_type = type;
8409   constructor_incremental = 1;
8410   constructor_designated = 0;
8411   constructor_zeroinit = 1;
8412   designator_depth = 0;
8413   designator_erroneous = 0;
8414 
8415   if (RECORD_OR_UNION_TYPE_P (constructor_type))
8416     {
8417       constructor_fields = TYPE_FIELDS (constructor_type);
8418       /* Skip any nameless bit fields at the beginning.  */
8419       while (constructor_fields != NULL_TREE
8420 	     && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8421 	constructor_fields = DECL_CHAIN (constructor_fields);
8422 
8423       constructor_unfilled_fields = constructor_fields;
8424       constructor_bit_index = bitsize_zero_node;
8425     }
8426   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8427     {
8428       if (TYPE_DOMAIN (constructor_type))
8429 	{
8430 	  constructor_max_index
8431 	    = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8432 
8433 	  /* Detect non-empty initializations of zero-length arrays.  */
8434 	  if (constructor_max_index == NULL_TREE
8435 	      && TYPE_SIZE (constructor_type))
8436 	    constructor_max_index = integer_minus_one_node;
8437 
8438 	  /* constructor_max_index needs to be an INTEGER_CST.  Attempts
8439 	     to initialize VLAs will cause a proper error; avoid tree
8440 	     checking errors as well by setting a safe value.  */
8441 	  if (constructor_max_index
8442 	      && TREE_CODE (constructor_max_index) != INTEGER_CST)
8443 	    constructor_max_index = integer_minus_one_node;
8444 
8445 	  constructor_index
8446 	    = convert (bitsizetype,
8447 		       TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8448 	}
8449       else
8450 	{
8451 	  constructor_index = bitsize_zero_node;
8452 	  constructor_max_index = NULL_TREE;
8453 	}
8454 
8455       constructor_unfilled_index = constructor_index;
8456     }
8457   else if (gnu_vector_type_p (constructor_type))
8458     {
8459       /* Vectors are like simple fixed-size arrays.  */
8460       constructor_max_index =
8461 	bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8462       constructor_index = bitsize_zero_node;
8463       constructor_unfilled_index = constructor_index;
8464     }
8465   else
8466     {
8467       /* Handle the case of int x = {5}; */
8468       constructor_fields = constructor_type;
8469       constructor_unfilled_fields = constructor_type;
8470     }
8471 }
8472 
8473 extern location_t last_init_list_comma;
8474 
8475 /* Called when we see an open brace for a nested initializer.  Finish
8476    off any pending levels with implicit braces.  */
8477 void
finish_implicit_inits(location_t loc,struct obstack * braced_init_obstack)8478 finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8479 {
8480   while (constructor_stack->implicit)
8481     {
8482       if (RECORD_OR_UNION_TYPE_P (constructor_type)
8483 	  && constructor_fields == NULL_TREE)
8484 	process_init_element (input_location,
8485 			      pop_init_level (loc, 1, braced_init_obstack,
8486 					      last_init_list_comma),
8487 			      true, braced_init_obstack);
8488       else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8489 	       && constructor_max_index
8490 	       && tree_int_cst_lt (constructor_max_index,
8491 				   constructor_index))
8492 	process_init_element (input_location,
8493 			      pop_init_level (loc, 1, braced_init_obstack,
8494 					      last_init_list_comma),
8495 			      true, braced_init_obstack);
8496       else
8497 	break;
8498     }
8499 }
8500 
8501 /* Push down into a subobject, for initialization.
8502    If this is for an explicit set of braces, IMPLICIT is 0.
8503    If it is because the next element belongs at a lower level,
8504    IMPLICIT is 1 (or 2 if the push is because of designator list).  */
8505 
8506 void
push_init_level(location_t loc,int implicit,struct obstack * braced_init_obstack)8507 push_init_level (location_t loc, int implicit,
8508 		 struct obstack *braced_init_obstack)
8509 {
8510   struct constructor_stack *p;
8511   tree value = NULL_TREE;
8512 
8513   /* Unless this is an explicit brace, we need to preserve previous
8514      content if any.  */
8515   if (implicit)
8516     {
8517       if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
8518 	value = find_init_member (constructor_fields, braced_init_obstack);
8519       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8520 	value = find_init_member (constructor_index, braced_init_obstack);
8521     }
8522 
8523   p = XNEW (struct constructor_stack);
8524   p->type = constructor_type;
8525   p->fields = constructor_fields;
8526   p->index = constructor_index;
8527   p->max_index = constructor_max_index;
8528   p->unfilled_index = constructor_unfilled_index;
8529   p->unfilled_fields = constructor_unfilled_fields;
8530   p->bit_index = constructor_bit_index;
8531   p->elements = constructor_elements;
8532   p->constant = constructor_constant;
8533   p->simple = constructor_simple;
8534   p->nonconst = constructor_nonconst;
8535   p->erroneous = constructor_erroneous;
8536   p->pending_elts = constructor_pending_elts;
8537   p->depth = constructor_depth;
8538   p->replacement_value.value = NULL_TREE;
8539   p->replacement_value.original_code = ERROR_MARK;
8540   p->replacement_value.original_type = NULL;
8541   p->implicit = implicit;
8542   p->outer = 0;
8543   p->incremental = constructor_incremental;
8544   p->designated = constructor_designated;
8545   p->designator_depth = designator_depth;
8546   p->next = constructor_stack;
8547   p->range_stack = 0;
8548   constructor_stack = p;
8549 
8550   constructor_constant = 1;
8551   constructor_simple = 1;
8552   constructor_nonconst = 0;
8553   constructor_depth = SPELLING_DEPTH ();
8554   constructor_elements = NULL;
8555   constructor_incremental = 1;
8556   constructor_designated = 0;
8557   constructor_pending_elts = 0;
8558   if (!implicit)
8559     {
8560       p->range_stack = constructor_range_stack;
8561       constructor_range_stack = 0;
8562       designator_depth = 0;
8563       designator_erroneous = 0;
8564     }
8565 
8566   /* Don't die if an entire brace-pair level is superfluous
8567      in the containing level.  */
8568   if (constructor_type == NULL_TREE)
8569     ;
8570   else if (RECORD_OR_UNION_TYPE_P (constructor_type))
8571     {
8572       /* Don't die if there are extra init elts at the end.  */
8573       if (constructor_fields == NULL_TREE)
8574 	constructor_type = NULL_TREE;
8575       else
8576 	{
8577 	  constructor_type = TREE_TYPE (constructor_fields);
8578 	  push_member_name (constructor_fields);
8579 	  constructor_depth++;
8580 	}
8581       /* If upper initializer is designated, then mark this as
8582 	 designated too to prevent bogus warnings.  */
8583       constructor_designated = p->designated;
8584     }
8585   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8586     {
8587       constructor_type = TREE_TYPE (constructor_type);
8588       push_array_bounds (tree_to_uhwi (constructor_index));
8589       constructor_depth++;
8590     }
8591 
8592   if (constructor_type == NULL_TREE)
8593     {
8594       error_init (loc, "extra brace group at end of initializer");
8595       constructor_fields = NULL_TREE;
8596       constructor_unfilled_fields = NULL_TREE;
8597       return;
8598     }
8599 
8600   if (value && TREE_CODE (value) == CONSTRUCTOR)
8601     {
8602       constructor_constant = TREE_CONSTANT (value);
8603       constructor_simple = TREE_STATIC (value);
8604       constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
8605       constructor_elements = CONSTRUCTOR_ELTS (value);
8606       if (!vec_safe_is_empty (constructor_elements)
8607 	  && (TREE_CODE (constructor_type) == RECORD_TYPE
8608 	      || TREE_CODE (constructor_type) == ARRAY_TYPE))
8609 	set_nonincremental_init (braced_init_obstack);
8610     }
8611 
8612   if (implicit == 1)
8613     {
8614       found_missing_braces = 1;
8615       if (initializer_stack->missing_brace_richloc)
8616 	initializer_stack->missing_brace_richloc->add_fixit_insert_before
8617 	  (loc, "{");
8618     }
8619 
8620   if (RECORD_OR_UNION_TYPE_P (constructor_type))
8621     {
8622       constructor_fields = TYPE_FIELDS (constructor_type);
8623       /* Skip any nameless bit fields at the beginning.  */
8624       while (constructor_fields != NULL_TREE
8625 	     && DECL_UNNAMED_BIT_FIELD (constructor_fields))
8626 	constructor_fields = DECL_CHAIN (constructor_fields);
8627 
8628       constructor_unfilled_fields = constructor_fields;
8629       constructor_bit_index = bitsize_zero_node;
8630     }
8631   else if (gnu_vector_type_p (constructor_type))
8632     {
8633       /* Vectors are like simple fixed-size arrays.  */
8634       constructor_max_index =
8635 	bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8636       constructor_index = bitsize_int (0);
8637       constructor_unfilled_index = constructor_index;
8638     }
8639   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8640     {
8641       if (TYPE_DOMAIN (constructor_type))
8642 	{
8643 	  constructor_max_index
8644 	    = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
8645 
8646 	  /* Detect non-empty initializations of zero-length arrays.  */
8647 	  if (constructor_max_index == NULL_TREE
8648 	      && TYPE_SIZE (constructor_type))
8649 	    constructor_max_index = integer_minus_one_node;
8650 
8651 	  /* constructor_max_index needs to be an INTEGER_CST.  Attempts
8652 	     to initialize VLAs will cause a proper error; avoid tree
8653 	     checking errors as well by setting a safe value.  */
8654 	  if (constructor_max_index
8655 	      && TREE_CODE (constructor_max_index) != INTEGER_CST)
8656 	    constructor_max_index = integer_minus_one_node;
8657 
8658 	  constructor_index
8659 	    = convert (bitsizetype,
8660 		       TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8661 	}
8662       else
8663 	constructor_index = bitsize_zero_node;
8664 
8665       constructor_unfilled_index = constructor_index;
8666       if (value && TREE_CODE (value) == STRING_CST)
8667 	{
8668 	  /* We need to split the char/wchar array into individual
8669 	     characters, so that we don't have to special case it
8670 	     everywhere.  */
8671 	  set_nonincremental_init_from_string (value, braced_init_obstack);
8672 	}
8673     }
8674   else
8675     {
8676       if (constructor_type != error_mark_node)
8677 	warning_init (input_location, 0, "braces around scalar initializer");
8678       constructor_fields = constructor_type;
8679       constructor_unfilled_fields = constructor_type;
8680     }
8681 }
8682 
8683 /* At the end of an implicit or explicit brace level,
8684    finish up that level of constructor.  If a single expression
8685    with redundant braces initialized that level, return the
8686    c_expr structure for that expression.  Otherwise, the original_code
8687    element is set to ERROR_MARK.
8688    If we were outputting the elements as they are read, return 0 as the value
8689    from inner levels (process_init_element ignores that),
8690    but return error_mark_node as the value from the outermost level
8691    (that's what we want to put in DECL_INITIAL).
8692    Otherwise, return a CONSTRUCTOR expression as the value.  */
8693 
8694 struct c_expr
pop_init_level(location_t loc,int implicit,struct obstack * braced_init_obstack,location_t insert_before)8695 pop_init_level (location_t loc, int implicit,
8696 		struct obstack *braced_init_obstack,
8697 		location_t insert_before)
8698 {
8699   struct constructor_stack *p;
8700   struct c_expr ret;
8701   ret.value = NULL_TREE;
8702   ret.original_code = ERROR_MARK;
8703   ret.original_type = NULL;
8704 
8705   if (implicit == 0)
8706     {
8707       /* When we come to an explicit close brace,
8708 	 pop any inner levels that didn't have explicit braces.  */
8709       while (constructor_stack->implicit)
8710 	process_init_element (input_location,
8711 			      pop_init_level (loc, 1, braced_init_obstack,
8712 					      insert_before),
8713 			      true, braced_init_obstack);
8714       gcc_assert (!constructor_range_stack);
8715     }
8716   else
8717     if (initializer_stack->missing_brace_richloc)
8718       initializer_stack->missing_brace_richloc->add_fixit_insert_before
8719 	(insert_before, "}");
8720 
8721   /* Now output all pending elements.  */
8722   constructor_incremental = 1;
8723   output_pending_init_elements (1, braced_init_obstack);
8724 
8725   p = constructor_stack;
8726 
8727   /* Error for initializing a flexible array member, or a zero-length
8728      array member in an inappropriate context.  */
8729   if (constructor_type && constructor_fields
8730       && TREE_CODE (constructor_type) == ARRAY_TYPE
8731       && TYPE_DOMAIN (constructor_type)
8732       && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
8733     {
8734       /* Silently discard empty initializations.  The parser will
8735 	 already have pedwarned for empty brackets.  */
8736       if (integer_zerop (constructor_unfilled_index))
8737 	constructor_type = NULL_TREE;
8738       else
8739 	{
8740 	  gcc_assert (!TYPE_SIZE (constructor_type));
8741 
8742 	  if (constructor_depth > 2)
8743 	    error_init (loc, "initialization of flexible array member in a nested context");
8744 	  else
8745 	    pedwarn_init (loc, OPT_Wpedantic,
8746 			  "initialization of a flexible array member");
8747 
8748 	  /* We have already issued an error message for the existence
8749 	     of a flexible array member not at the end of the structure.
8750 	     Discard the initializer so that we do not die later.  */
8751 	  if (DECL_CHAIN (constructor_fields) != NULL_TREE)
8752 	    constructor_type = NULL_TREE;
8753 	}
8754     }
8755 
8756   switch (vec_safe_length (constructor_elements))
8757     {
8758     case 0:
8759       /* Initialization with { } counts as zeroinit.  */
8760       constructor_zeroinit = 1;
8761       break;
8762     case 1:
8763       /* This might be zeroinit as well.  */
8764       if (integer_zerop ((*constructor_elements)[0].value))
8765 	constructor_zeroinit = 1;
8766       break;
8767     default:
8768       /* If the constructor has more than one element, it can't be { 0 }.  */
8769       constructor_zeroinit = 0;
8770       break;
8771     }
8772 
8773   /* Warn when some structs are initialized with direct aggregation.  */
8774   if (!implicit && found_missing_braces && warn_missing_braces
8775       && !constructor_zeroinit)
8776     {
8777       gcc_assert (initializer_stack->missing_brace_richloc);
8778       warning_at (initializer_stack->missing_brace_richloc,
8779 		  OPT_Wmissing_braces,
8780 		  "missing braces around initializer");
8781     }
8782 
8783   /* Warn when some struct elements are implicitly initialized to zero.  */
8784   if (warn_missing_field_initializers
8785       && constructor_type
8786       && TREE_CODE (constructor_type) == RECORD_TYPE
8787       && constructor_unfilled_fields)
8788     {
8789 	/* Do not warn for flexible array members or zero-length arrays.  */
8790 	while (constructor_unfilled_fields
8791 	       && (!DECL_SIZE (constructor_unfilled_fields)
8792 		   || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
8793 	  constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
8794 
8795 	if (constructor_unfilled_fields
8796 	    /* Do not warn if this level of the initializer uses member
8797 	       designators; it is likely to be deliberate.  */
8798 	    && !constructor_designated
8799 	    /* Do not warn about initializing with { 0 } or with { }.  */
8800 	    && !constructor_zeroinit)
8801 	  {
8802 	    if (warning_at (input_location, OPT_Wmissing_field_initializers,
8803 			    "missing initializer for field %qD of %qT",
8804 			    constructor_unfilled_fields,
8805 			    constructor_type))
8806 	      inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
8807 		      "%qD declared here", constructor_unfilled_fields);
8808 	  }
8809     }
8810 
8811   /* Pad out the end of the structure.  */
8812   if (p->replacement_value.value)
8813     /* If this closes a superfluous brace pair,
8814        just pass out the element between them.  */
8815     ret = p->replacement_value;
8816   else if (constructor_type == NULL_TREE)
8817     ;
8818   else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
8819 	   && TREE_CODE (constructor_type) != ARRAY_TYPE
8820 	   && !gnu_vector_type_p (constructor_type))
8821     {
8822       /* A nonincremental scalar initializer--just return
8823 	 the element, after verifying there is just one.  */
8824       if (vec_safe_is_empty (constructor_elements))
8825 	{
8826 	  if (!constructor_erroneous && constructor_type != error_mark_node)
8827 	    error_init (loc, "empty scalar initializer");
8828 	  ret.value = error_mark_node;
8829 	}
8830       else if (vec_safe_length (constructor_elements) != 1)
8831 	{
8832 	  error_init (loc, "extra elements in scalar initializer");
8833 	  ret.value = (*constructor_elements)[0].value;
8834 	}
8835       else
8836 	ret.value = (*constructor_elements)[0].value;
8837     }
8838   else
8839     {
8840       if (constructor_erroneous)
8841 	ret.value = error_mark_node;
8842       else
8843 	{
8844 	  ret.value = build_constructor (constructor_type,
8845 					 constructor_elements);
8846 	  if (constructor_constant)
8847 	    TREE_CONSTANT (ret.value) = 1;
8848 	  if (constructor_constant && constructor_simple)
8849 	    TREE_STATIC (ret.value) = 1;
8850 	  if (constructor_nonconst)
8851 	    CONSTRUCTOR_NON_CONST (ret.value) = 1;
8852 	}
8853     }
8854 
8855   if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8856     {
8857       if (constructor_nonconst)
8858 	ret.original_code = C_MAYBE_CONST_EXPR;
8859       else if (ret.original_code == C_MAYBE_CONST_EXPR)
8860 	ret.original_code = ERROR_MARK;
8861     }
8862 
8863   constructor_type = p->type;
8864   constructor_fields = p->fields;
8865   constructor_index = p->index;
8866   constructor_max_index = p->max_index;
8867   constructor_unfilled_index = p->unfilled_index;
8868   constructor_unfilled_fields = p->unfilled_fields;
8869   constructor_bit_index = p->bit_index;
8870   constructor_elements = p->elements;
8871   constructor_constant = p->constant;
8872   constructor_simple = p->simple;
8873   constructor_nonconst = p->nonconst;
8874   constructor_erroneous = p->erroneous;
8875   constructor_incremental = p->incremental;
8876   constructor_designated = p->designated;
8877   designator_depth = p->designator_depth;
8878   constructor_pending_elts = p->pending_elts;
8879   constructor_depth = p->depth;
8880   if (!p->implicit)
8881     constructor_range_stack = p->range_stack;
8882   RESTORE_SPELLING_DEPTH (constructor_depth);
8883 
8884   constructor_stack = p->next;
8885   free (p);
8886 
8887   if (ret.value == NULL_TREE && constructor_stack == 0)
8888     ret.value = error_mark_node;
8889   return ret;
8890 }
8891 
8892 /* Common handling for both array range and field name designators.
8893    ARRAY argument is nonzero for array ranges.  Returns false for success.  */
8894 
8895 static bool
set_designator(location_t loc,bool array,struct obstack * braced_init_obstack)8896 set_designator (location_t loc, bool array,
8897 		struct obstack *braced_init_obstack)
8898 {
8899   tree subtype;
8900   enum tree_code subcode;
8901 
8902   /* Don't die if an entire brace-pair level is superfluous
8903      in the containing level, or for an erroneous type.  */
8904   if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
8905     return true;
8906 
8907   /* If there were errors in this designator list already, bail out
8908      silently.  */
8909   if (designator_erroneous)
8910     return true;
8911 
8912   /* Likewise for an initializer for a variable-size type.  Those are
8913      diagnosed in digest_init.  */
8914   if (COMPLETE_TYPE_P (constructor_type)
8915       && TREE_CODE (TYPE_SIZE (constructor_type)) != INTEGER_CST)
8916     return true;
8917 
8918   if (!designator_depth)
8919     {
8920       gcc_assert (!constructor_range_stack);
8921 
8922       /* Designator list starts at the level of closest explicit
8923 	 braces.  */
8924       while (constructor_stack->implicit)
8925 	process_init_element (input_location,
8926 			      pop_init_level (loc, 1, braced_init_obstack,
8927 					      last_init_list_comma),
8928 			      true, braced_init_obstack);
8929       constructor_designated = 1;
8930       return false;
8931     }
8932 
8933   switch (TREE_CODE (constructor_type))
8934     {
8935     case  RECORD_TYPE:
8936     case  UNION_TYPE:
8937       subtype = TREE_TYPE (constructor_fields);
8938       if (subtype != error_mark_node)
8939 	subtype = TYPE_MAIN_VARIANT (subtype);
8940       break;
8941     case ARRAY_TYPE:
8942       subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8943       break;
8944     default:
8945       gcc_unreachable ();
8946     }
8947 
8948   subcode = TREE_CODE (subtype);
8949   if (array && subcode != ARRAY_TYPE)
8950     {
8951       error_init (loc, "array index in non-array initializer");
8952       return true;
8953     }
8954   else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8955     {
8956       error_init (loc, "field name not in record or union initializer");
8957       return true;
8958     }
8959 
8960   constructor_designated = 1;
8961   finish_implicit_inits (loc, braced_init_obstack);
8962   push_init_level (loc, 2, braced_init_obstack);
8963   return false;
8964 }
8965 
8966 /* If there are range designators in designator list, push a new designator
8967    to constructor_range_stack.  RANGE_END is end of such stack range or
8968    NULL_TREE if there is no range designator at this level.  */
8969 
8970 static void
push_range_stack(tree range_end,struct obstack * braced_init_obstack)8971 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
8972 {
8973   struct constructor_range_stack *p;
8974 
8975   p = (struct constructor_range_stack *)
8976     obstack_alloc (braced_init_obstack,
8977 		   sizeof (struct constructor_range_stack));
8978   p->prev = constructor_range_stack;
8979   p->next = 0;
8980   p->fields = constructor_fields;
8981   p->range_start = constructor_index;
8982   p->index = constructor_index;
8983   p->stack = constructor_stack;
8984   p->range_end = range_end;
8985   if (constructor_range_stack)
8986     constructor_range_stack->next = p;
8987   constructor_range_stack = p;
8988 }
8989 
8990 /* Within an array initializer, specify the next index to be initialized.
8991    FIRST is that index.  If LAST is nonzero, then initialize a range
8992    of indices, running from FIRST through LAST.  */
8993 
8994 void
set_init_index(location_t loc,tree first,tree last,struct obstack * braced_init_obstack)8995 set_init_index (location_t loc, tree first, tree last,
8996 		struct obstack *braced_init_obstack)
8997 {
8998   if (set_designator (loc, true, braced_init_obstack))
8999     return;
9000 
9001   designator_erroneous = 1;
9002 
9003   if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
9004       || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
9005     {
9006       error_init (loc, "array index in initializer not of integer type");
9007       return;
9008     }
9009 
9010   if (TREE_CODE (first) != INTEGER_CST)
9011     {
9012       first = c_fully_fold (first, false, NULL);
9013       if (TREE_CODE (first) == INTEGER_CST)
9014 	pedwarn_init (loc, OPT_Wpedantic,
9015 		      "array index in initializer is not "
9016 		      "an integer constant expression");
9017     }
9018 
9019   if (last && TREE_CODE (last) != INTEGER_CST)
9020     {
9021       last = c_fully_fold (last, false, NULL);
9022       if (TREE_CODE (last) == INTEGER_CST)
9023 	pedwarn_init (loc, OPT_Wpedantic,
9024 		      "array index in initializer is not "
9025 		      "an integer constant expression");
9026     }
9027 
9028   if (TREE_CODE (first) != INTEGER_CST)
9029     error_init (loc, "nonconstant array index in initializer");
9030   else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
9031     error_init (loc, "nonconstant array index in initializer");
9032   else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
9033     error_init (loc, "array index in non-array initializer");
9034   else if (tree_int_cst_sgn (first) == -1)
9035     error_init (loc, "array index in initializer exceeds array bounds");
9036   else if (constructor_max_index
9037 	   && tree_int_cst_lt (constructor_max_index, first))
9038     error_init (loc, "array index in initializer exceeds array bounds");
9039   else
9040     {
9041       constant_expression_warning (first);
9042       if (last)
9043 	constant_expression_warning (last);
9044       constructor_index = convert (bitsizetype, first);
9045       if (tree_int_cst_lt (constructor_index, first))
9046 	{
9047 	  constructor_index = copy_node (constructor_index);
9048 	  TREE_OVERFLOW (constructor_index) = 1;
9049 	}
9050 
9051       if (last)
9052 	{
9053 	  if (tree_int_cst_equal (first, last))
9054 	    last = NULL_TREE;
9055 	  else if (tree_int_cst_lt (last, first))
9056 	    {
9057 	      error_init (loc, "empty index range in initializer");
9058 	      last = NULL_TREE;
9059 	    }
9060 	  else
9061 	    {
9062 	      last = convert (bitsizetype, last);
9063 	      if (constructor_max_index != NULL_TREE
9064 		  && tree_int_cst_lt (constructor_max_index, last))
9065 		{
9066 		  error_init (loc, "array index range in initializer exceeds "
9067 			      "array bounds");
9068 		  last = NULL_TREE;
9069 		}
9070 	    }
9071 	}
9072 
9073       designator_depth++;
9074       designator_erroneous = 0;
9075       if (constructor_range_stack || last)
9076 	push_range_stack (last, braced_init_obstack);
9077     }
9078 }
9079 
9080 /* Within a struct initializer, specify the next field to be initialized.  */
9081 
9082 void
set_init_label(location_t loc,tree fieldname,location_t fieldname_loc,struct obstack * braced_init_obstack)9083 set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
9084 		struct obstack *braced_init_obstack)
9085 {
9086   tree field;
9087 
9088   if (set_designator (loc, false, braced_init_obstack))
9089     return;
9090 
9091   designator_erroneous = 1;
9092 
9093   if (!RECORD_OR_UNION_TYPE_P (constructor_type))
9094     {
9095       error_init (loc, "field name not in record or union initializer");
9096       return;
9097     }
9098 
9099   field = lookup_field (constructor_type, fieldname);
9100 
9101   if (field == NULL_TREE)
9102     {
9103       tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
9104       if (guessed_id)
9105 	{
9106 	  gcc_rich_location rich_loc (fieldname_loc);
9107 	  rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
9108 	  error_at (&rich_loc,
9109 		    "%qT has no member named %qE; did you mean %qE?",
9110 		    constructor_type, fieldname, guessed_id);
9111 	}
9112       else
9113 	error_at (fieldname_loc, "%qT has no member named %qE",
9114 		  constructor_type, fieldname);
9115     }
9116   else
9117     do
9118       {
9119 	constructor_fields = TREE_VALUE (field);
9120 	designator_depth++;
9121 	designator_erroneous = 0;
9122 	if (constructor_range_stack)
9123 	  push_range_stack (NULL_TREE, braced_init_obstack);
9124 	field = TREE_CHAIN (field);
9125 	if (field)
9126 	  {
9127 	    if (set_designator (loc, false, braced_init_obstack))
9128 	      return;
9129 	  }
9130       }
9131     while (field != NULL_TREE);
9132 }
9133 
9134 /* Add a new initializer to the tree of pending initializers.  PURPOSE
9135    identifies the initializer, either array index or field in a structure.
9136    VALUE is the value of that index or field.  If ORIGTYPE is not
9137    NULL_TREE, it is the original type of VALUE.
9138 
9139    IMPLICIT is true if value comes from pop_init_level (1),
9140    the new initializer has been merged with the existing one
9141    and thus no warnings should be emitted about overriding an
9142    existing initializer.  */
9143 
9144 static void
add_pending_init(location_t loc,tree purpose,tree value,tree origtype,bool implicit,struct obstack * braced_init_obstack)9145 add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
9146 		  bool implicit, struct obstack *braced_init_obstack)
9147 {
9148   struct init_node *p, **q, *r;
9149 
9150   q = &constructor_pending_elts;
9151   p = 0;
9152 
9153   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9154     {
9155       while (*q != 0)
9156 	{
9157 	  p = *q;
9158 	  if (tree_int_cst_lt (purpose, p->purpose))
9159 	    q = &p->left;
9160 	  else if (tree_int_cst_lt (p->purpose, purpose))
9161 	    q = &p->right;
9162 	  else
9163 	    {
9164 	      if (!implicit)
9165 		{
9166 		  if (TREE_SIDE_EFFECTS (p->value))
9167 		    warning_init (loc, OPT_Woverride_init_side_effects,
9168 				  "initialized field with side-effects "
9169 				  "overwritten");
9170 		  else if (warn_override_init)
9171 		    warning_init (loc, OPT_Woverride_init,
9172 				  "initialized field overwritten");
9173 		}
9174 	      p->value = value;
9175 	      p->origtype = origtype;
9176 	      return;
9177 	    }
9178 	}
9179     }
9180   else
9181     {
9182       tree bitpos;
9183 
9184       bitpos = bit_position (purpose);
9185       while (*q != NULL)
9186 	{
9187 	  p = *q;
9188 	  if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9189 	    q = &p->left;
9190 	  else if (p->purpose != purpose)
9191 	    q = &p->right;
9192 	  else
9193 	    {
9194 	      if (!implicit)
9195 		{
9196 		  if (TREE_SIDE_EFFECTS (p->value))
9197 		    warning_init (loc, OPT_Woverride_init_side_effects,
9198 				  "initialized field with side-effects "
9199 				  "overwritten");
9200 		  else if (warn_override_init)
9201 		    warning_init (loc, OPT_Woverride_init,
9202 				  "initialized field overwritten");
9203 		}
9204 	      p->value = value;
9205 	      p->origtype = origtype;
9206 	      return;
9207 	    }
9208 	}
9209     }
9210 
9211   r = (struct init_node *) obstack_alloc (braced_init_obstack,
9212 					  sizeof (struct init_node));
9213   r->purpose = purpose;
9214   r->value = value;
9215   r->origtype = origtype;
9216 
9217   *q = r;
9218   r->parent = p;
9219   r->left = 0;
9220   r->right = 0;
9221   r->balance = 0;
9222 
9223   while (p)
9224     {
9225       struct init_node *s;
9226 
9227       if (r == p->left)
9228 	{
9229 	  if (p->balance == 0)
9230 	    p->balance = -1;
9231 	  else if (p->balance < 0)
9232 	    {
9233 	      if (r->balance < 0)
9234 		{
9235 		  /* L rotation.  */
9236 		  p->left = r->right;
9237 		  if (p->left)
9238 		    p->left->parent = p;
9239 		  r->right = p;
9240 
9241 		  p->balance = 0;
9242 		  r->balance = 0;
9243 
9244 		  s = p->parent;
9245 		  p->parent = r;
9246 		  r->parent = s;
9247 		  if (s)
9248 		    {
9249 		      if (s->left == p)
9250 			s->left = r;
9251 		      else
9252 			s->right = r;
9253 		    }
9254 		  else
9255 		    constructor_pending_elts = r;
9256 		}
9257 	      else
9258 		{
9259 		  /* LR rotation.  */
9260 		  struct init_node *t = r->right;
9261 
9262 		  r->right = t->left;
9263 		  if (r->right)
9264 		    r->right->parent = r;
9265 		  t->left = r;
9266 
9267 		  p->left = t->right;
9268 		  if (p->left)
9269 		    p->left->parent = p;
9270 		  t->right = p;
9271 
9272 		  p->balance = t->balance < 0;
9273 		  r->balance = -(t->balance > 0);
9274 		  t->balance = 0;
9275 
9276 		  s = p->parent;
9277 		  p->parent = t;
9278 		  r->parent = t;
9279 		  t->parent = s;
9280 		  if (s)
9281 		    {
9282 		      if (s->left == p)
9283 			s->left = t;
9284 		      else
9285 			s->right = t;
9286 		    }
9287 		  else
9288 		    constructor_pending_elts = t;
9289 		}
9290 	      break;
9291 	    }
9292 	  else
9293 	    {
9294 	      /* p->balance == +1; growth of left side balances the node.  */
9295 	      p->balance = 0;
9296 	      break;
9297 	    }
9298 	}
9299       else /* r == p->right */
9300 	{
9301 	  if (p->balance == 0)
9302 	    /* Growth propagation from right side.  */
9303 	    p->balance++;
9304 	  else if (p->balance > 0)
9305 	    {
9306 	      if (r->balance > 0)
9307 		{
9308 		  /* R rotation.  */
9309 		  p->right = r->left;
9310 		  if (p->right)
9311 		    p->right->parent = p;
9312 		  r->left = p;
9313 
9314 		  p->balance = 0;
9315 		  r->balance = 0;
9316 
9317 		  s = p->parent;
9318 		  p->parent = r;
9319 		  r->parent = s;
9320 		  if (s)
9321 		    {
9322 		      if (s->left == p)
9323 			s->left = r;
9324 		      else
9325 			s->right = r;
9326 		    }
9327 		  else
9328 		    constructor_pending_elts = r;
9329 		}
9330 	      else /* r->balance == -1 */
9331 		{
9332 		  /* RL rotation */
9333 		  struct init_node *t = r->left;
9334 
9335 		  r->left = t->right;
9336 		  if (r->left)
9337 		    r->left->parent = r;
9338 		  t->right = r;
9339 
9340 		  p->right = t->left;
9341 		  if (p->right)
9342 		    p->right->parent = p;
9343 		  t->left = p;
9344 
9345 		  r->balance = (t->balance < 0);
9346 		  p->balance = -(t->balance > 0);
9347 		  t->balance = 0;
9348 
9349 		  s = p->parent;
9350 		  p->parent = t;
9351 		  r->parent = t;
9352 		  t->parent = s;
9353 		  if (s)
9354 		    {
9355 		      if (s->left == p)
9356 			s->left = t;
9357 		      else
9358 			s->right = t;
9359 		    }
9360 		  else
9361 		    constructor_pending_elts = t;
9362 		}
9363 	      break;
9364 	    }
9365 	  else
9366 	    {
9367 	      /* p->balance == -1; growth of right side balances the node.  */
9368 	      p->balance = 0;
9369 	      break;
9370 	    }
9371 	}
9372 
9373       r = p;
9374       p = p->parent;
9375     }
9376 }
9377 
9378 /* Build AVL tree from a sorted chain.  */
9379 
9380 static void
set_nonincremental_init(struct obstack * braced_init_obstack)9381 set_nonincremental_init (struct obstack * braced_init_obstack)
9382 {
9383   unsigned HOST_WIDE_INT ix;
9384   tree index, value;
9385 
9386   if (TREE_CODE (constructor_type) != RECORD_TYPE
9387       && TREE_CODE (constructor_type) != ARRAY_TYPE)
9388     return;
9389 
9390   FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
9391     add_pending_init (input_location, index, value, NULL_TREE, true,
9392 		      braced_init_obstack);
9393   constructor_elements = NULL;
9394   if (TREE_CODE (constructor_type) == RECORD_TYPE)
9395     {
9396       constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9397       /* Skip any nameless bit fields at the beginning.  */
9398       while (constructor_unfilled_fields != NULL_TREE
9399 	     && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9400 	constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
9401 
9402     }
9403   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9404     {
9405       if (TYPE_DOMAIN (constructor_type))
9406 	constructor_unfilled_index
9407 	    = convert (bitsizetype,
9408 		       TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9409       else
9410 	constructor_unfilled_index = bitsize_zero_node;
9411     }
9412   constructor_incremental = 0;
9413 }
9414 
9415 /* Build AVL tree from a string constant.  */
9416 
9417 static void
set_nonincremental_init_from_string(tree str,struct obstack * braced_init_obstack)9418 set_nonincremental_init_from_string (tree str,
9419 				     struct obstack * braced_init_obstack)
9420 {
9421   tree value, purpose, type;
9422   HOST_WIDE_INT val[2];
9423   const char *p, *end;
9424   int byte, wchar_bytes, charwidth, bitpos;
9425 
9426   gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
9427 
9428   wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
9429   charwidth = TYPE_PRECISION (char_type_node);
9430   gcc_assert ((size_t) wchar_bytes * charwidth
9431 	      <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
9432   type = TREE_TYPE (constructor_type);
9433   p = TREE_STRING_POINTER (str);
9434   end = p + TREE_STRING_LENGTH (str);
9435 
9436   for (purpose = bitsize_zero_node;
9437        p < end
9438        && !(constructor_max_index
9439 	    && tree_int_cst_lt (constructor_max_index, purpose));
9440        purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
9441     {
9442       if (wchar_bytes == 1)
9443 	{
9444 	  val[0] = (unsigned char) *p++;
9445 	  val[1] = 0;
9446 	}
9447       else
9448 	{
9449 	  val[1] = 0;
9450 	  val[0] = 0;
9451 	  for (byte = 0; byte < wchar_bytes; byte++)
9452 	    {
9453 	      if (BYTES_BIG_ENDIAN)
9454 		bitpos = (wchar_bytes - byte - 1) * charwidth;
9455 	      else
9456 		bitpos = byte * charwidth;
9457 	      val[bitpos / HOST_BITS_PER_WIDE_INT]
9458 		|= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9459 		   << (bitpos % HOST_BITS_PER_WIDE_INT);
9460 	    }
9461 	}
9462 
9463       if (!TYPE_UNSIGNED (type))
9464 	{
9465 	  bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9466 	  if (bitpos < HOST_BITS_PER_WIDE_INT)
9467 	    {
9468 	      if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
9469 		{
9470 		  val[0] |= HOST_WIDE_INT_M1U << bitpos;
9471 		  val[1] = -1;
9472 		}
9473 	    }
9474 	  else if (bitpos == HOST_BITS_PER_WIDE_INT)
9475 	    {
9476 	      if (val[0] < 0)
9477 		val[1] = -1;
9478 	    }
9479 	  else if (val[1] & (HOST_WIDE_INT_1
9480 			     << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
9481 	    val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
9482 	}
9483 
9484       value = wide_int_to_tree (type,
9485 				wide_int::from_array (val, 2,
9486 						      HOST_BITS_PER_WIDE_INT * 2));
9487       add_pending_init (input_location, purpose, value, NULL_TREE, true,
9488                         braced_init_obstack);
9489     }
9490 
9491   constructor_incremental = 0;
9492 }
9493 
9494 /* Return value of FIELD in pending initializer or NULL_TREE if the field was
9495    not initialized yet.  */
9496 
9497 static tree
find_init_member(tree field,struct obstack * braced_init_obstack)9498 find_init_member (tree field, struct obstack * braced_init_obstack)
9499 {
9500   struct init_node *p;
9501 
9502   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9503     {
9504       if (constructor_incremental
9505 	  && tree_int_cst_lt (field, constructor_unfilled_index))
9506 	set_nonincremental_init (braced_init_obstack);
9507 
9508       p = constructor_pending_elts;
9509       while (p)
9510 	{
9511 	  if (tree_int_cst_lt (field, p->purpose))
9512 	    p = p->left;
9513 	  else if (tree_int_cst_lt (p->purpose, field))
9514 	    p = p->right;
9515 	  else
9516 	    return p->value;
9517 	}
9518     }
9519   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9520     {
9521       tree bitpos = bit_position (field);
9522 
9523       if (constructor_incremental
9524 	  && (!constructor_unfilled_fields
9525 	      || tree_int_cst_lt (bitpos,
9526 				  bit_position (constructor_unfilled_fields))))
9527 	set_nonincremental_init (braced_init_obstack);
9528 
9529       p = constructor_pending_elts;
9530       while (p)
9531 	{
9532 	  if (field == p->purpose)
9533 	    return p->value;
9534 	  else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9535 	    p = p->left;
9536 	  else
9537 	    p = p->right;
9538 	}
9539     }
9540   else if (TREE_CODE (constructor_type) == UNION_TYPE)
9541     {
9542       if (!vec_safe_is_empty (constructor_elements)
9543 	  && (constructor_elements->last ().index == field))
9544 	return constructor_elements->last ().value;
9545     }
9546   return NULL_TREE;
9547 }
9548 
9549 /* "Output" the next constructor element.
9550    At top level, really output it to assembler code now.
9551    Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
9552    If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
9553    TYPE is the data type that the containing data type wants here.
9554    FIELD is the field (a FIELD_DECL) or the index that this element fills.
9555    If VALUE is a string constant, STRICT_STRING is true if it is
9556    unparenthesized or we should not warn here for it being parenthesized.
9557    For other types of VALUE, STRICT_STRING is not used.
9558 
9559    PENDING if true means output pending elements that belong
9560    right after this element.  (PENDING is normally true;
9561    it is false while outputting pending elements, to avoid recursion.)
9562 
9563    IMPLICIT is true if value comes from pop_init_level (1),
9564    the new initializer has been merged with the existing one
9565    and thus no warnings should be emitted about overriding an
9566    existing initializer.  */
9567 
9568 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)9569 output_init_element (location_t loc, tree value, tree origtype,
9570 		     bool strict_string, tree type, tree field, bool pending,
9571 		     bool implicit, struct obstack * braced_init_obstack)
9572 {
9573   tree semantic_type = NULL_TREE;
9574   bool maybe_const = true;
9575   bool npc;
9576 
9577   if (type == error_mark_node || value == error_mark_node)
9578     {
9579       constructor_erroneous = 1;
9580       return;
9581     }
9582   if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9583       && (TREE_CODE (value) == STRING_CST
9584 	  || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9585       && !(TREE_CODE (value) == STRING_CST
9586 	   && TREE_CODE (type) == ARRAY_TYPE
9587 	   && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9588       && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9589 		     TYPE_MAIN_VARIANT (type)))
9590     value = array_to_pointer_conversion (input_location, value);
9591 
9592   if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
9593       && require_constant_value && pending)
9594     {
9595       /* As an extension, allow initializing objects with static storage
9596 	 duration with compound literals (which are then treated just as
9597 	 the brace enclosed list they contain).  */
9598       if (flag_isoc99)
9599 	pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9600 		      "constant");
9601       tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9602       value = DECL_INITIAL (decl);
9603     }
9604 
9605   npc = null_pointer_constant_p (value);
9606   if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9607     {
9608       semantic_type = TREE_TYPE (value);
9609       value = TREE_OPERAND (value, 0);
9610     }
9611   value = c_fully_fold (value, require_constant_value, &maybe_const);
9612 
9613   if (value == error_mark_node)
9614     constructor_erroneous = 1;
9615   else if (!TREE_CONSTANT (value))
9616     constructor_constant = 0;
9617   else if (!initializer_constant_valid_p (value,
9618 					  TREE_TYPE (value),
9619 					  AGGREGATE_TYPE_P (constructor_type)
9620 					  && TYPE_REVERSE_STORAGE_ORDER
9621 					     (constructor_type))
9622 	   || (RECORD_OR_UNION_TYPE_P (constructor_type)
9623 	       && DECL_C_BIT_FIELD (field)
9624 	       && TREE_CODE (value) != INTEGER_CST))
9625     constructor_simple = 0;
9626   if (!maybe_const)
9627     constructor_nonconst = 1;
9628 
9629   /* Digest the initializer and issue any errors about incompatible
9630      types before issuing errors about non-constant initializers.  */
9631   tree new_value = value;
9632   if (semantic_type)
9633     new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9634   new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9635 			   require_constant_value);
9636   if (new_value == error_mark_node)
9637     {
9638       constructor_erroneous = 1;
9639       return;
9640     }
9641   if (require_constant_value || require_constant_elements)
9642     constant_expression_warning (new_value);
9643 
9644   /* Proceed to check the constness of the original initializer.  */
9645   if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
9646     {
9647       if (require_constant_value)
9648 	{
9649 	  error_init (loc, "initializer element is not constant");
9650 	  value = error_mark_node;
9651 	}
9652       else if (require_constant_elements)
9653 	pedwarn (loc, OPT_Wpedantic,
9654 		 "initializer element is not computable at load time");
9655     }
9656   else if (!maybe_const
9657 	   && (require_constant_value || require_constant_elements))
9658     pedwarn_init (loc, OPT_Wpedantic,
9659 		  "initializer element is not a constant expression");
9660 
9661   /* Issue -Wc++-compat warnings about initializing a bitfield with
9662      enum type.  */
9663   if (warn_cxx_compat
9664       && field != NULL_TREE
9665       && TREE_CODE (field) == FIELD_DECL
9666       && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9667       && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9668 	  != TYPE_MAIN_VARIANT (type))
9669       && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9670     {
9671       tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9672       if (checktype != error_mark_node
9673 	  && (TYPE_MAIN_VARIANT (checktype)
9674 	      != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
9675 	warning_init (loc, OPT_Wc___compat,
9676 		      "enum conversion in initialization is invalid in C++");
9677     }
9678 
9679   /* If this field is empty and does not have side effects (and is not at
9680      the end of structure), don't do anything other than checking the
9681      initializer.  */
9682   if (field
9683       && (TREE_TYPE (field) == error_mark_node
9684 	  || (COMPLETE_TYPE_P (TREE_TYPE (field))
9685 	      && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
9686 	      && !TREE_SIDE_EFFECTS (new_value)
9687 	      && (TREE_CODE (constructor_type) == ARRAY_TYPE
9688 		  || DECL_CHAIN (field)))))
9689     return;
9690 
9691   /* Finally, set VALUE to the initializer value digested above.  */
9692   value = new_value;
9693 
9694   /* If this element doesn't come next in sequence,
9695      put it on constructor_pending_elts.  */
9696   if (TREE_CODE (constructor_type) == ARRAY_TYPE
9697       && (!constructor_incremental
9698 	  || !tree_int_cst_equal (field, constructor_unfilled_index)))
9699     {
9700       if (constructor_incremental
9701 	  && tree_int_cst_lt (field, constructor_unfilled_index))
9702 	set_nonincremental_init (braced_init_obstack);
9703 
9704       add_pending_init (loc, field, value, origtype, implicit,
9705 			braced_init_obstack);
9706       return;
9707     }
9708   else if (TREE_CODE (constructor_type) == RECORD_TYPE
9709 	   && (!constructor_incremental
9710 	       || field != constructor_unfilled_fields))
9711     {
9712       /* We do this for records but not for unions.  In a union,
9713 	 no matter which field is specified, it can be initialized
9714 	 right away since it starts at the beginning of the union.  */
9715       if (constructor_incremental)
9716 	{
9717 	  if (!constructor_unfilled_fields)
9718 	    set_nonincremental_init (braced_init_obstack);
9719 	  else
9720 	    {
9721 	      tree bitpos, unfillpos;
9722 
9723 	      bitpos = bit_position (field);
9724 	      unfillpos = bit_position (constructor_unfilled_fields);
9725 
9726 	      if (tree_int_cst_lt (bitpos, unfillpos))
9727 		set_nonincremental_init (braced_init_obstack);
9728 	    }
9729 	}
9730 
9731       add_pending_init (loc, field, value, origtype, implicit,
9732 			braced_init_obstack);
9733       return;
9734     }
9735   else if (TREE_CODE (constructor_type) == UNION_TYPE
9736 	   && !vec_safe_is_empty (constructor_elements))
9737     {
9738       if (!implicit)
9739 	{
9740 	  if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
9741 	    warning_init (loc, OPT_Woverride_init_side_effects,
9742 			  "initialized field with side-effects overwritten");
9743 	  else if (warn_override_init)
9744 	    warning_init (loc, OPT_Woverride_init,
9745 			  "initialized field overwritten");
9746 	}
9747 
9748       /* We can have just one union field set.  */
9749       constructor_elements = NULL;
9750     }
9751 
9752   /* Otherwise, output this element either to
9753      constructor_elements or to the assembler file.  */
9754 
9755   constructor_elt celt = {field, value};
9756   vec_safe_push (constructor_elements, celt);
9757 
9758   /* Advance the variable that indicates sequential elements output.  */
9759   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9760     constructor_unfilled_index
9761       = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9762 			bitsize_one_node);
9763   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9764     {
9765       constructor_unfilled_fields
9766 	= DECL_CHAIN (constructor_unfilled_fields);
9767 
9768       /* Skip any nameless bit fields.  */
9769       while (constructor_unfilled_fields != NULL_TREE
9770 	     && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
9771 	constructor_unfilled_fields =
9772 	  DECL_CHAIN (constructor_unfilled_fields);
9773     }
9774   else if (TREE_CODE (constructor_type) == UNION_TYPE)
9775     constructor_unfilled_fields = NULL_TREE;
9776 
9777   /* Now output any pending elements which have become next.  */
9778   if (pending)
9779     output_pending_init_elements (0, braced_init_obstack);
9780 }
9781 
9782 /* For two FIELD_DECLs in the same chain, return -1 if field1
9783    comes before field2, 1 if field1 comes after field2 and
9784    0 if field1 == field2.  */
9785 
9786 static int
init_field_decl_cmp(tree field1,tree field2)9787 init_field_decl_cmp (tree field1, tree field2)
9788 {
9789   if (field1 == field2)
9790     return 0;
9791 
9792   tree bitpos1 = bit_position (field1);
9793   tree bitpos2 = bit_position (field2);
9794   if (tree_int_cst_equal (bitpos1, bitpos2))
9795     {
9796       /* If one of the fields has non-zero bitsize, then that
9797 	 field must be the last one in a sequence of zero
9798 	 sized fields, fields after it will have bigger
9799 	 bit_position.  */
9800       if (TREE_TYPE (field1) != error_mark_node
9801 	  && COMPLETE_TYPE_P (TREE_TYPE (field1))
9802 	  && integer_nonzerop (TREE_TYPE (field1)))
9803 	return 1;
9804       if (TREE_TYPE (field2) != error_mark_node
9805 	  && COMPLETE_TYPE_P (TREE_TYPE (field2))
9806 	  && integer_nonzerop (TREE_TYPE (field2)))
9807 	return -1;
9808       /* Otherwise, fallback to DECL_CHAIN walk to find out
9809 	 which field comes earlier.  Walk chains of both
9810 	 fields, so that if field1 and field2 are close to each
9811 	 other in either order, it is found soon even for large
9812 	 sequences of zero sized fields.  */
9813       tree f1 = field1, f2 = field2;
9814       while (1)
9815 	{
9816 	  f1 = DECL_CHAIN (f1);
9817 	  f2 = DECL_CHAIN (f2);
9818 	  if (f1 == NULL_TREE)
9819 	    {
9820 	      gcc_assert (f2);
9821 	      return 1;
9822 	    }
9823 	  if (f2 == NULL_TREE)
9824 	    return -1;
9825 	  if (f1 == field2)
9826 	    return -1;
9827 	  if (f2 == field1)
9828 	    return 1;
9829 	  if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9830 	    return 1;
9831 	  if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9832 	    return -1;
9833 	}
9834     }
9835   else if (tree_int_cst_lt (bitpos1, bitpos2))
9836     return -1;
9837   else
9838     return 1;
9839 }
9840 
9841 /* Output any pending elements which have become next.
9842    As we output elements, constructor_unfilled_{fields,index}
9843    advances, which may cause other elements to become next;
9844    if so, they too are output.
9845 
9846    If ALL is 0, we return when there are
9847    no more pending elements to output now.
9848 
9849    If ALL is 1, we output space as necessary so that
9850    we can output all the pending elements.  */
9851 static void
output_pending_init_elements(int all,struct obstack * braced_init_obstack)9852 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
9853 {
9854   struct init_node *elt = constructor_pending_elts;
9855   tree next;
9856 
9857  retry:
9858 
9859   /* Look through the whole pending tree.
9860      If we find an element that should be output now,
9861      output it.  Otherwise, set NEXT to the element
9862      that comes first among those still pending.  */
9863 
9864   next = NULL_TREE;
9865   while (elt)
9866     {
9867       if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9868 	{
9869 	  if (tree_int_cst_equal (elt->purpose,
9870 				  constructor_unfilled_index))
9871 	    output_init_element (input_location, elt->value, elt->origtype,
9872 				 true, TREE_TYPE (constructor_type),
9873 				 constructor_unfilled_index, false, false,
9874 				 braced_init_obstack);
9875 	  else if (tree_int_cst_lt (constructor_unfilled_index,
9876 				    elt->purpose))
9877 	    {
9878 	      /* Advance to the next smaller node.  */
9879 	      if (elt->left)
9880 		elt = elt->left;
9881 	      else
9882 		{
9883 		  /* We have reached the smallest node bigger than the
9884 		     current unfilled index.  Fill the space first.  */
9885 		  next = elt->purpose;
9886 		  break;
9887 		}
9888 	    }
9889 	  else
9890 	    {
9891 	      /* Advance to the next bigger node.  */
9892 	      if (elt->right)
9893 		elt = elt->right;
9894 	      else
9895 		{
9896 		  /* We have reached the biggest node in a subtree.  Find
9897 		     the parent of it, which is the next bigger node.  */
9898 		  while (elt->parent && elt->parent->right == elt)
9899 		    elt = elt->parent;
9900 		  elt = elt->parent;
9901 		  if (elt && tree_int_cst_lt (constructor_unfilled_index,
9902 					      elt->purpose))
9903 		    {
9904 		      next = elt->purpose;
9905 		      break;
9906 		    }
9907 		}
9908 	    }
9909 	}
9910       else if (RECORD_OR_UNION_TYPE_P (constructor_type))
9911 	{
9912 	  /* If the current record is complete we are done.  */
9913 	  if (constructor_unfilled_fields == NULL_TREE)
9914 	    break;
9915 
9916 	  int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9917 					 elt->purpose);
9918 	  if (cmp == 0)
9919 	    output_init_element (input_location, elt->value, elt->origtype,
9920 				 true, TREE_TYPE (elt->purpose),
9921 				 elt->purpose, false, false,
9922 				 braced_init_obstack);
9923 	  else if (cmp < 0)
9924 	    {
9925 	      /* Advance to the next smaller node.  */
9926 	      if (elt->left)
9927 		elt = elt->left;
9928 	      else
9929 		{
9930 		  /* We have reached the smallest node bigger than the
9931 		     current unfilled field.  Fill the space first.  */
9932 		  next = elt->purpose;
9933 		  break;
9934 		}
9935 	    }
9936 	  else
9937 	    {
9938 	      /* Advance to the next bigger node.  */
9939 	      if (elt->right)
9940 		elt = elt->right;
9941 	      else
9942 		{
9943 		  /* We have reached the biggest node in a subtree.  Find
9944 		     the parent of it, which is the next bigger node.  */
9945 		  while (elt->parent && elt->parent->right == elt)
9946 		    elt = elt->parent;
9947 		  elt = elt->parent;
9948 		  if (elt
9949 		      && init_field_decl_cmp (constructor_unfilled_fields,
9950 					      elt->purpose) < 0)
9951 		    {
9952 		      next = elt->purpose;
9953 		      break;
9954 		    }
9955 		}
9956 	    }
9957 	}
9958     }
9959 
9960   /* Ordinarily return, but not if we want to output all
9961      and there are elements left.  */
9962   if (!(all && next != NULL_TREE))
9963     return;
9964 
9965   /* If it's not incremental, just skip over the gap, so that after
9966      jumping to retry we will output the next successive element.  */
9967   if (RECORD_OR_UNION_TYPE_P (constructor_type))
9968     constructor_unfilled_fields = next;
9969   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9970     constructor_unfilled_index = next;
9971 
9972   /* ELT now points to the node in the pending tree with the next
9973      initializer to output.  */
9974   goto retry;
9975 }
9976 
9977 /* Expression VALUE coincides with the start of type TYPE in a braced
9978    initializer.  Return true if we should treat VALUE as initializing
9979    the first element of TYPE, false if we should treat it as initializing
9980    TYPE as a whole.
9981 
9982    If the initializer is clearly invalid, the question becomes:
9983    which choice gives the best error message?  */
9984 
9985 static bool
initialize_elementwise_p(tree type,tree value)9986 initialize_elementwise_p (tree type, tree value)
9987 {
9988   if (type == error_mark_node || value == error_mark_node)
9989     return false;
9990 
9991   gcc_checking_assert (TYPE_MAIN_VARIANT (type) == type);
9992 
9993   tree value_type = TREE_TYPE (value);
9994   if (value_type == error_mark_node)
9995     return false;
9996 
9997   /* GNU vectors can be initialized elementwise.  However, treat any
9998      kind of vector value as initializing the vector type as a whole,
9999      regardless of whether the value is a GNU vector.  Such initializers
10000      are valid if and only if they would have been valid in a non-braced
10001      initializer like:
10002 
10003 	TYPE foo = VALUE;
10004 
10005      so recursing into the vector type would be at best confusing or at
10006      worst wrong.  For example, when -flax-vector-conversions is in effect,
10007      it's possible to initialize a V8HI from a V4SI, even though the vectors
10008      have different element types and different numbers of elements.  */
10009   if (gnu_vector_type_p (type))
10010     return !VECTOR_TYPE_P (value_type);
10011 
10012   if (AGGREGATE_TYPE_P (type))
10013     return type != TYPE_MAIN_VARIANT (value_type);
10014 
10015   return false;
10016 }
10017 
10018 /* Add one non-braced element to the current constructor level.
10019    This adjusts the current position within the constructor's type.
10020    This may also start or terminate implicit levels
10021    to handle a partly-braced initializer.
10022 
10023    Once this has found the correct level for the new element,
10024    it calls output_init_element.
10025 
10026    IMPLICIT is true if value comes from pop_init_level (1),
10027    the new initializer has been merged with the existing one
10028    and thus no warnings should be emitted about overriding an
10029    existing initializer.  */
10030 
10031 void
process_init_element(location_t loc,struct c_expr value,bool implicit,struct obstack * braced_init_obstack)10032 process_init_element (location_t loc, struct c_expr value, bool implicit,
10033 		      struct obstack * braced_init_obstack)
10034 {
10035   tree orig_value = value.value;
10036   int string_flag
10037     = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
10038   bool strict_string = value.original_code == STRING_CST;
10039   bool was_designated = designator_depth != 0;
10040 
10041   designator_depth = 0;
10042   designator_erroneous = 0;
10043 
10044   if (!implicit && value.value && !integer_zerop (value.value))
10045     constructor_zeroinit = 0;
10046 
10047   /* Handle superfluous braces around string cst as in
10048      char x[] = {"foo"}; */
10049   if (string_flag
10050       && constructor_type
10051       && !was_designated
10052       && TREE_CODE (constructor_type) == ARRAY_TYPE
10053       && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
10054       && integer_zerop (constructor_unfilled_index))
10055     {
10056       if (constructor_stack->replacement_value.value)
10057 	error_init (loc, "excess elements in %<char%> array initializer");
10058       constructor_stack->replacement_value = value;
10059       return;
10060     }
10061 
10062   if (constructor_stack->replacement_value.value != NULL_TREE)
10063     {
10064       error_init (loc, "excess elements in struct initializer");
10065       return;
10066     }
10067 
10068   /* Ignore elements of a brace group if it is entirely superfluous
10069      and has already been diagnosed, or if the type is erroneous.  */
10070   if (constructor_type == NULL_TREE || constructor_type == error_mark_node)
10071     return;
10072 
10073   /* Ignore elements of an initializer for a variable-size type.
10074      Those are diagnosed in digest_init.  */
10075   if (COMPLETE_TYPE_P (constructor_type)
10076       && !poly_int_tree_p (TYPE_SIZE (constructor_type)))
10077     return;
10078 
10079   if (!implicit && warn_designated_init && !was_designated
10080       && TREE_CODE (constructor_type) == RECORD_TYPE
10081       && lookup_attribute ("designated_init",
10082 			   TYPE_ATTRIBUTES (constructor_type)))
10083     warning_init (loc,
10084 		  OPT_Wdesignated_init,
10085 		  "positional initialization of field "
10086 		  "in %<struct%> declared with %<designated_init%> attribute");
10087 
10088   /* If we've exhausted any levels that didn't have braces,
10089      pop them now.  */
10090   while (constructor_stack->implicit)
10091     {
10092       if (RECORD_OR_UNION_TYPE_P (constructor_type)
10093 	  && constructor_fields == NULL_TREE)
10094 	process_init_element (loc,
10095 			      pop_init_level (loc, 1, braced_init_obstack,
10096 					      last_init_list_comma),
10097 			      true, braced_init_obstack);
10098       else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
10099 		|| gnu_vector_type_p (constructor_type))
10100 	       && constructor_max_index
10101 	       && tree_int_cst_lt (constructor_max_index,
10102 				   constructor_index))
10103 	process_init_element (loc,
10104 			      pop_init_level (loc, 1, braced_init_obstack,
10105 					      last_init_list_comma),
10106 			      true, braced_init_obstack);
10107       else
10108 	break;
10109     }
10110 
10111   /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once.  */
10112   if (constructor_range_stack)
10113     {
10114       /* If value is a compound literal and we'll be just using its
10115 	 content, don't put it into a SAVE_EXPR.  */
10116       if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
10117 	  || !require_constant_value)
10118 	{
10119 	  tree semantic_type = NULL_TREE;
10120 	  if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
10121 	    {
10122 	      semantic_type = TREE_TYPE (value.value);
10123 	      value.value = TREE_OPERAND (value.value, 0);
10124 	    }
10125 	  value.value = save_expr (value.value);
10126 	  if (semantic_type)
10127 	    value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
10128 				  value.value);
10129 	}
10130     }
10131 
10132   while (1)
10133     {
10134       if (TREE_CODE (constructor_type) == RECORD_TYPE)
10135 	{
10136 	  tree fieldtype;
10137 	  enum tree_code fieldcode;
10138 
10139 	  if (constructor_fields == NULL_TREE)
10140 	    {
10141 	      pedwarn_init (loc, 0, "excess elements in struct initializer");
10142 	      break;
10143 	    }
10144 
10145 	  fieldtype = TREE_TYPE (constructor_fields);
10146 	  if (fieldtype != error_mark_node)
10147 	    fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10148 	  fieldcode = TREE_CODE (fieldtype);
10149 
10150 	  /* Error for non-static initialization of a flexible array member.  */
10151 	  if (fieldcode == ARRAY_TYPE
10152 	      && !require_constant_value
10153 	      && TYPE_SIZE (fieldtype) == NULL_TREE
10154 	      && DECL_CHAIN (constructor_fields) == NULL_TREE)
10155 	    {
10156 	      error_init (loc, "non-static initialization of a flexible "
10157 			  "array member");
10158 	      break;
10159 	    }
10160 
10161 	  /* Error for initialization of a flexible array member with
10162 	     a string constant if the structure is in an array.  E.g.:
10163 	     struct S { int x; char y[]; };
10164 	     struct S s[] = { { 1, "foo" } };
10165 	     is invalid.  */
10166 	  if (string_flag
10167 	      && fieldcode == ARRAY_TYPE
10168 	      && constructor_depth > 1
10169 	      && TYPE_SIZE (fieldtype) == NULL_TREE
10170 	      && DECL_CHAIN (constructor_fields) == NULL_TREE)
10171 	    {
10172 	      bool in_array_p = false;
10173 	      for (struct constructor_stack *p = constructor_stack;
10174 		   p && p->type; p = p->next)
10175 		if (TREE_CODE (p->type) == ARRAY_TYPE)
10176 		  {
10177 		    in_array_p = true;
10178 		    break;
10179 		  }
10180 	      if (in_array_p)
10181 		{
10182 		  error_init (loc, "initialization of flexible array "
10183 			      "member in a nested context");
10184 		  break;
10185 		}
10186 	    }
10187 
10188 	  /* Accept a string constant to initialize a subarray.  */
10189 	  if (value.value != NULL_TREE
10190 	      && fieldcode == ARRAY_TYPE
10191 	      && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10192 	      && string_flag)
10193 	    value.value = orig_value;
10194 	  /* Otherwise, if we have come to a subaggregate,
10195 	     and we don't have an element of its type, push into it.  */
10196 	  else if (value.value != NULL_TREE
10197 		   && initialize_elementwise_p (fieldtype, value.value))
10198 	    {
10199 	      push_init_level (loc, 1, braced_init_obstack);
10200 	      continue;
10201 	    }
10202 
10203 	  if (value.value)
10204 	    {
10205 	      push_member_name (constructor_fields);
10206 	      output_init_element (loc, value.value, value.original_type,
10207 				   strict_string, fieldtype,
10208 				   constructor_fields, true, implicit,
10209 				   braced_init_obstack);
10210 	      RESTORE_SPELLING_DEPTH (constructor_depth);
10211 	    }
10212 	  else
10213 	    /* Do the bookkeeping for an element that was
10214 	       directly output as a constructor.  */
10215 	    {
10216 	      /* For a record, keep track of end position of last field.  */
10217 	      if (DECL_SIZE (constructor_fields))
10218 		constructor_bit_index
10219 		  = size_binop_loc (input_location, PLUS_EXPR,
10220 				    bit_position (constructor_fields),
10221 				    DECL_SIZE (constructor_fields));
10222 
10223 	      /* If the current field was the first one not yet written out,
10224 		 it isn't now, so update.  */
10225 	      if (constructor_unfilled_fields == constructor_fields)
10226 		{
10227 		  constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10228 		  /* Skip any nameless bit fields.  */
10229 		  while (constructor_unfilled_fields != 0
10230 			 && (DECL_UNNAMED_BIT_FIELD
10231 			     (constructor_unfilled_fields)))
10232 		    constructor_unfilled_fields =
10233 		      DECL_CHAIN (constructor_unfilled_fields);
10234 		}
10235 	    }
10236 
10237 	  constructor_fields = DECL_CHAIN (constructor_fields);
10238 	  /* Skip any nameless bit fields at the beginning.  */
10239 	  while (constructor_fields != NULL_TREE
10240 		 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
10241 	    constructor_fields = DECL_CHAIN (constructor_fields);
10242 	}
10243       else if (TREE_CODE (constructor_type) == UNION_TYPE)
10244 	{
10245 	  tree fieldtype;
10246 	  enum tree_code fieldcode;
10247 
10248 	  if (constructor_fields == NULL_TREE)
10249 	    {
10250 	      pedwarn_init (loc, 0,
10251 			    "excess elements in union initializer");
10252 	      break;
10253 	    }
10254 
10255 	  fieldtype = TREE_TYPE (constructor_fields);
10256 	  if (fieldtype != error_mark_node)
10257 	    fieldtype = TYPE_MAIN_VARIANT (fieldtype);
10258 	  fieldcode = TREE_CODE (fieldtype);
10259 
10260 	  /* Warn that traditional C rejects initialization of unions.
10261 	     We skip the warning if the value is zero.  This is done
10262 	     under the assumption that the zero initializer in user
10263 	     code appears conditioned on e.g. __STDC__ to avoid
10264 	     "missing initializer" warnings and relies on default
10265 	     initialization to zero in the traditional C case.
10266 	     We also skip the warning if the initializer is designated,
10267 	     again on the assumption that this must be conditional on
10268 	     __STDC__ anyway (and we've already complained about the
10269 	     member-designator already).  */
10270 	  if (!in_system_header_at (input_location) && !constructor_designated
10271 	      && !(value.value && (integer_zerop (value.value)
10272 				   || real_zerop (value.value))))
10273 	    warning (OPT_Wtraditional, "traditional C rejects initialization "
10274 		     "of unions");
10275 
10276 	  /* Accept a string constant to initialize a subarray.  */
10277 	  if (value.value != NULL_TREE
10278 	      && fieldcode == ARRAY_TYPE
10279 	      && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
10280 	      && string_flag)
10281 	    value.value = orig_value;
10282 	  /* Otherwise, if we have come to a subaggregate,
10283 	     and we don't have an element of its type, push into it.  */
10284 	  else if (value.value != NULL_TREE
10285 		   && initialize_elementwise_p (fieldtype, value.value))
10286 	    {
10287 	      push_init_level (loc, 1, braced_init_obstack);
10288 	      continue;
10289 	    }
10290 
10291 	  if (value.value)
10292 	    {
10293 	      push_member_name (constructor_fields);
10294 	      output_init_element (loc, value.value, value.original_type,
10295 				   strict_string, fieldtype,
10296 				   constructor_fields, true, implicit,
10297 				   braced_init_obstack);
10298 	      RESTORE_SPELLING_DEPTH (constructor_depth);
10299 	    }
10300 	  else
10301 	    /* Do the bookkeeping for an element that was
10302 	       directly output as a constructor.  */
10303 	    {
10304 	      constructor_bit_index = DECL_SIZE (constructor_fields);
10305 	      constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
10306 	    }
10307 
10308 	  constructor_fields = NULL_TREE;
10309 	}
10310       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
10311 	{
10312 	  tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10313 	  enum tree_code eltcode = TREE_CODE (elttype);
10314 
10315 	  /* Accept a string constant to initialize a subarray.  */
10316 	  if (value.value != NULL_TREE
10317 	      && eltcode == ARRAY_TYPE
10318 	      && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
10319 	      && string_flag)
10320 	    value.value = orig_value;
10321 	  /* Otherwise, if we have come to a subaggregate,
10322 	     and we don't have an element of its type, push into it.  */
10323 	  else if (value.value != NULL_TREE
10324 		   && initialize_elementwise_p (elttype, value.value))
10325 	    {
10326 	      push_init_level (loc, 1, braced_init_obstack);
10327 	      continue;
10328 	    }
10329 
10330 	  if (constructor_max_index != NULL_TREE
10331 	      && (tree_int_cst_lt (constructor_max_index, constructor_index)
10332 		  || integer_all_onesp (constructor_max_index)))
10333 	    {
10334 	      pedwarn_init (loc, 0,
10335 			    "excess elements in array initializer");
10336 	      break;
10337 	    }
10338 
10339 	  /* Now output the actual element.  */
10340 	  if (value.value)
10341 	    {
10342 	      push_array_bounds (tree_to_uhwi (constructor_index));
10343 	      output_init_element (loc, value.value, value.original_type,
10344 				   strict_string, elttype,
10345 				   constructor_index, true, implicit,
10346 				   braced_init_obstack);
10347 	      RESTORE_SPELLING_DEPTH (constructor_depth);
10348 	    }
10349 
10350 	  constructor_index
10351 	    = size_binop_loc (input_location, PLUS_EXPR,
10352 			      constructor_index, bitsize_one_node);
10353 
10354 	  if (!value.value)
10355 	    /* If we are doing the bookkeeping for an element that was
10356 	       directly output as a constructor, we must update
10357 	       constructor_unfilled_index.  */
10358 	    constructor_unfilled_index = constructor_index;
10359 	}
10360       else if (gnu_vector_type_p (constructor_type))
10361 	{
10362 	  tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
10363 
10364 	 /* Do a basic check of initializer size.  Note that vectors
10365 	    always have a fixed size derived from their type.  */
10366 	  if (tree_int_cst_lt (constructor_max_index, constructor_index))
10367 	    {
10368 	      pedwarn_init (loc, 0,
10369 			    "excess elements in vector initializer");
10370 	      break;
10371 	    }
10372 
10373 	  /* Now output the actual element.  */
10374 	  if (value.value)
10375 	    {
10376 	      if (TREE_CODE (value.value) == VECTOR_CST)
10377 		elttype = TYPE_MAIN_VARIANT (constructor_type);
10378 	      output_init_element (loc, value.value, value.original_type,
10379 				   strict_string, elttype,
10380 				   constructor_index, true, implicit,
10381 				   braced_init_obstack);
10382 	    }
10383 
10384 	  constructor_index
10385 	    = size_binop_loc (input_location,
10386 			      PLUS_EXPR, constructor_index, bitsize_one_node);
10387 
10388 	  if (!value.value)
10389 	    /* If we are doing the bookkeeping for an element that was
10390 	       directly output as a constructor, we must update
10391 	       constructor_unfilled_index.  */
10392 	    constructor_unfilled_index = constructor_index;
10393 	}
10394 
10395       /* Handle the sole element allowed in a braced initializer
10396 	 for a scalar variable.  */
10397       else if (constructor_type != error_mark_node
10398 	       && constructor_fields == NULL_TREE)
10399 	{
10400 	  pedwarn_init (loc, 0,
10401 			"excess elements in scalar initializer");
10402 	  break;
10403 	}
10404       else
10405 	{
10406 	  if (value.value)
10407 	    output_init_element (loc, value.value, value.original_type,
10408 				 strict_string, constructor_type,
10409 				 NULL_TREE, true, implicit,
10410 				 braced_init_obstack);
10411 	  constructor_fields = NULL_TREE;
10412 	}
10413 
10414       /* Handle range initializers either at this level or anywhere higher
10415 	 in the designator stack.  */
10416       if (constructor_range_stack)
10417 	{
10418 	  struct constructor_range_stack *p, *range_stack;
10419 	  int finish = 0;
10420 
10421 	  range_stack = constructor_range_stack;
10422 	  constructor_range_stack = 0;
10423 	  while (constructor_stack != range_stack->stack)
10424 	    {
10425 	      gcc_assert (constructor_stack->implicit);
10426 	      process_init_element (loc,
10427 				    pop_init_level (loc, 1,
10428 						    braced_init_obstack,
10429 						    last_init_list_comma),
10430 				    true, braced_init_obstack);
10431 	    }
10432 	  for (p = range_stack;
10433 	       !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10434 	       p = p->prev)
10435 	    {
10436 	      gcc_assert (constructor_stack->implicit);
10437 	      process_init_element (loc,
10438 				    pop_init_level (loc, 1,
10439 						    braced_init_obstack,
10440 						    last_init_list_comma),
10441 				    true, braced_init_obstack);
10442 	    }
10443 
10444 	  p->index = size_binop_loc (input_location,
10445 				     PLUS_EXPR, p->index, bitsize_one_node);
10446 	  if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10447 	    finish = 1;
10448 
10449 	  while (1)
10450 	    {
10451 	      constructor_index = p->index;
10452 	      constructor_fields = p->fields;
10453 	      if (finish && p->range_end && p->index == p->range_start)
10454 		{
10455 		  finish = 0;
10456 		  p->prev = 0;
10457 		}
10458 	      p = p->next;
10459 	      if (!p)
10460 		break;
10461 	      finish_implicit_inits (loc, braced_init_obstack);
10462 	      push_init_level (loc, 2, braced_init_obstack);
10463 	      p->stack = constructor_stack;
10464 	      if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10465 		p->index = p->range_start;
10466 	    }
10467 
10468 	  if (!finish)
10469 	    constructor_range_stack = range_stack;
10470 	  continue;
10471 	}
10472 
10473       break;
10474     }
10475 
10476   constructor_range_stack = 0;
10477 }
10478 
10479 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
10480    (guaranteed to be 'volatile' or null) and ARGS (represented using
10481    an ASM_EXPR node).  */
10482 tree
build_asm_stmt(bool is_volatile,tree args)10483 build_asm_stmt (bool is_volatile, tree args)
10484 {
10485   if (is_volatile)
10486     ASM_VOLATILE_P (args) = 1;
10487   return add_stmt (args);
10488 }
10489 
10490 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10491    some INPUTS, and some CLOBBERS.  The latter three may be NULL.
10492    SIMPLE indicates whether there was anything at all after the
10493    string in the asm expression -- asm("blah") and asm("blah" : )
10494    are subtly different.  We use a ASM_EXPR node to represent this.
10495    LOC is the location of the asm, and IS_INLINE says whether this
10496    is asm inline.  */
10497 tree
build_asm_expr(location_t loc,tree string,tree outputs,tree inputs,tree clobbers,tree labels,bool simple,bool is_inline)10498 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
10499 		tree clobbers, tree labels, bool simple, bool is_inline)
10500 {
10501   tree tail;
10502   tree args;
10503   int i;
10504   const char *constraint;
10505   const char **oconstraints;
10506   bool allows_mem, allows_reg, is_inout;
10507   int ninputs, noutputs;
10508 
10509   ninputs = list_length (inputs);
10510   noutputs = list_length (outputs);
10511   oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10512 
10513   string = resolve_asm_operand_names (string, outputs, inputs, labels);
10514 
10515   /* Remove output conversions that change the type but not the mode.  */
10516   for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
10517     {
10518       tree output = TREE_VALUE (tail);
10519 
10520       output = c_fully_fold (output, false, NULL, true);
10521 
10522       /* ??? Really, this should not be here.  Users should be using a
10523 	 proper lvalue, dammit.  But there's a long history of using casts
10524 	 in the output operands.  In cases like longlong.h, this becomes a
10525 	 primitive form of typechecking -- if the cast can be removed, then
10526 	 the output operand had a type of the proper width; otherwise we'll
10527 	 get an error.  Gross, but ...  */
10528       STRIP_NOPS (output);
10529 
10530       if (!lvalue_or_else (loc, output, lv_asm))
10531 	output = error_mark_node;
10532 
10533       if (output != error_mark_node
10534 	  && (TREE_READONLY (output)
10535 	      || TYPE_READONLY (TREE_TYPE (output))
10536 	      || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
10537 		  && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
10538 	readonly_error (loc, output, lv_asm);
10539 
10540       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10541       oconstraints[i] = constraint;
10542 
10543       if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10544 				   &allows_mem, &allows_reg, &is_inout))
10545 	{
10546 	  /* If the operand is going to end up in memory,
10547 	     mark it addressable.  */
10548 	  if (!allows_reg && !c_mark_addressable (output))
10549 	    output = error_mark_node;
10550 	  if (!(!allows_reg && allows_mem)
10551 	      && output != error_mark_node
10552 	      && VOID_TYPE_P (TREE_TYPE (output)))
10553 	    {
10554 	      error_at (loc, "invalid use of void expression");
10555 	      output = error_mark_node;
10556 	    }
10557 	}
10558       else
10559 	output = error_mark_node;
10560 
10561       TREE_VALUE (tail) = output;
10562     }
10563 
10564   for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10565     {
10566       tree input;
10567 
10568       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10569       input = TREE_VALUE (tail);
10570 
10571       if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10572 				  oconstraints, &allows_mem, &allows_reg))
10573 	{
10574 	  /* If the operand is going to end up in memory,
10575 	     mark it addressable.  */
10576 	  if (!allows_reg && allows_mem)
10577 	    {
10578 	      input = c_fully_fold (input, false, NULL, true);
10579 
10580 	      /* Strip the nops as we allow this case.  FIXME, this really
10581 		 should be rejected or made deprecated.  */
10582 	      STRIP_NOPS (input);
10583 	      if (!c_mark_addressable (input))
10584 		input = error_mark_node;
10585 	    }
10586 	  else
10587 	    {
10588 	      struct c_expr expr;
10589 	      memset (&expr, 0, sizeof (expr));
10590 	      expr.value = input;
10591 	      expr = convert_lvalue_to_rvalue (loc, expr, true, false);
10592 	      input = c_fully_fold (expr.value, false, NULL);
10593 
10594 	      if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10595 		{
10596 		  error_at (loc, "invalid use of void expression");
10597 		  input = error_mark_node;
10598 		}
10599 	    }
10600 	}
10601       else
10602 	input = error_mark_node;
10603 
10604       TREE_VALUE (tail) = input;
10605     }
10606 
10607   /* ASMs with labels cannot have outputs.  This should have been
10608      enforced by the parser.  */
10609   gcc_assert (outputs == NULL || labels == NULL);
10610 
10611   args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
10612 
10613   /* asm statements without outputs, including simple ones, are treated
10614      as volatile.  */
10615   ASM_INPUT_P (args) = simple;
10616   ASM_VOLATILE_P (args) = (noutputs == 0);
10617   ASM_INLINE_P (args) = is_inline;
10618 
10619   return args;
10620 }
10621 
10622 /* Generate a goto statement to LABEL.  LOC is the location of the
10623    GOTO.  */
10624 
10625 tree
c_finish_goto_label(location_t loc,tree label)10626 c_finish_goto_label (location_t loc, tree label)
10627 {
10628   tree decl = lookup_label_for_goto (loc, label);
10629   if (!decl)
10630     return NULL_TREE;
10631   TREE_USED (decl) = 1;
10632   {
10633     add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
10634     tree t = build1 (GOTO_EXPR, void_type_node, decl);
10635     SET_EXPR_LOCATION (t, loc);
10636     return add_stmt (t);
10637   }
10638 }
10639 
10640 /* Generate a computed goto statement to EXPR.  LOC is the location of
10641    the GOTO.  */
10642 
10643 tree
c_finish_goto_ptr(location_t loc,tree expr)10644 c_finish_goto_ptr (location_t loc, tree expr)
10645 {
10646   tree t;
10647   pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
10648   expr = c_fully_fold (expr, false, NULL);
10649   expr = convert (ptr_type_node, expr);
10650   t = build1 (GOTO_EXPR, void_type_node, expr);
10651   SET_EXPR_LOCATION (t, loc);
10652   return add_stmt (t);
10653 }
10654 
10655 /* Generate a C `return' statement.  RETVAL is the expression for what
10656    to return, or a null pointer for `return;' with no value.  LOC is
10657    the location of the return statement, or the location of the expression,
10658    if the statement has any.  If ORIGTYPE is not NULL_TREE, it
10659    is the original type of RETVAL.  */
10660 
10661 tree
c_finish_return(location_t loc,tree retval,tree origtype)10662 c_finish_return (location_t loc, tree retval, tree origtype)
10663 {
10664   tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10665   bool no_warning = false;
10666   bool npc = false;
10667 
10668   /* Use the expansion point to handle cases such as returning NULL
10669      in a function returning void.  */
10670   location_t xloc = expansion_point_location_if_in_system_header (loc);
10671 
10672   if (TREE_THIS_VOLATILE (current_function_decl))
10673     warning_at (xloc, 0,
10674 		"function declared %<noreturn%> has a %<return%> statement");
10675 
10676   if (retval)
10677     {
10678       tree semantic_type = NULL_TREE;
10679       npc = null_pointer_constant_p (retval);
10680       if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10681 	{
10682 	  semantic_type = TREE_TYPE (retval);
10683 	  retval = TREE_OPERAND (retval, 0);
10684 	}
10685       retval = c_fully_fold (retval, false, NULL);
10686       if (semantic_type
10687 	  && valtype != NULL_TREE
10688 	  && TREE_CODE (valtype) != VOID_TYPE)
10689 	retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
10690     }
10691 
10692   if (!retval)
10693     {
10694       current_function_returns_null = 1;
10695       if ((warn_return_type >= 0 || flag_isoc99)
10696 	  && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
10697 	{
10698 	  bool warned_here;
10699 	  if (flag_isoc99)
10700 	    warned_here = pedwarn
10701 	      (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10702 	       "%<return%> with no value, in function returning non-void");
10703 	  else
10704 	    warned_here = warning_at
10705 	      (loc, OPT_Wreturn_type,
10706 	       "%<return%> with no value, in function returning non-void");
10707 	  no_warning = true;
10708 	  if (warned_here)
10709 	    inform (DECL_SOURCE_LOCATION (current_function_decl),
10710 		    "declared here");
10711 	}
10712     }
10713   else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
10714     {
10715       current_function_returns_null = 1;
10716       bool warned_here;
10717       if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
10718 	warned_here = pedwarn
10719 	  (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
10720 	   "%<return%> with a value, in function returning void");
10721       else
10722 	warned_here = pedwarn
10723 	  (xloc, OPT_Wpedantic, "ISO C forbids "
10724 	   "%<return%> with expression, in function returning void");
10725       if (warned_here)
10726 	inform (DECL_SOURCE_LOCATION (current_function_decl),
10727 		"declared here");
10728     }
10729   else
10730     {
10731       tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10732 				       retval, origtype, ic_return,
10733 				       npc, NULL_TREE, NULL_TREE, 0);
10734       tree res = DECL_RESULT (current_function_decl);
10735       tree inner;
10736       bool save;
10737 
10738       current_function_returns_value = 1;
10739       if (t == error_mark_node)
10740 	return NULL_TREE;
10741 
10742       save = in_late_binary_op;
10743       if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
10744 	  || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10745 	  || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10746 	      && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10747 		  || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
10748 	      && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
10749         in_late_binary_op = true;
10750       inner = t = convert (TREE_TYPE (res), t);
10751       in_late_binary_op = save;
10752 
10753       /* Strip any conversions, additions, and subtractions, and see if
10754 	 we are returning the address of a local variable.  Warn if so.  */
10755       while (1)
10756 	{
10757 	  switch (TREE_CODE (inner))
10758 	    {
10759 	    CASE_CONVERT:
10760 	    case NON_LVALUE_EXPR:
10761 	    case PLUS_EXPR:
10762 	    case POINTER_PLUS_EXPR:
10763 	      inner = TREE_OPERAND (inner, 0);
10764 	      continue;
10765 
10766 	    case MINUS_EXPR:
10767 	      /* If the second operand of the MINUS_EXPR has a pointer
10768 		 type (or is converted from it), this may be valid, so
10769 		 don't give a warning.  */
10770 	      {
10771 		tree op1 = TREE_OPERAND (inner, 1);
10772 
10773 		while (!POINTER_TYPE_P (TREE_TYPE (op1))
10774 		       && (CONVERT_EXPR_P (op1)
10775 			   || TREE_CODE (op1) == NON_LVALUE_EXPR))
10776 		  op1 = TREE_OPERAND (op1, 0);
10777 
10778 		if (POINTER_TYPE_P (TREE_TYPE (op1)))
10779 		  break;
10780 
10781 		inner = TREE_OPERAND (inner, 0);
10782 		continue;
10783 	      }
10784 
10785 	    case ADDR_EXPR:
10786 	      inner = TREE_OPERAND (inner, 0);
10787 
10788 	      while (REFERENCE_CLASS_P (inner)
10789 		     && !INDIRECT_REF_P (inner))
10790 		inner = TREE_OPERAND (inner, 0);
10791 
10792 	      if (DECL_P (inner)
10793 		  && !DECL_EXTERNAL (inner)
10794 		  && !TREE_STATIC (inner)
10795 		  && DECL_CONTEXT (inner) == current_function_decl
10796 		  && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
10797 		{
10798 		  if (TREE_CODE (inner) == LABEL_DECL)
10799 		    warning_at (loc, OPT_Wreturn_local_addr,
10800 				"function returns address of label");
10801 		  else
10802 		    {
10803 		      warning_at (loc, OPT_Wreturn_local_addr,
10804 				  "function returns address of local variable");
10805 		      tree zero = build_zero_cst (TREE_TYPE (res));
10806 		      t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10807 		    }
10808 		}
10809 	      break;
10810 
10811 	    default:
10812 	      break;
10813 	    }
10814 
10815 	  break;
10816 	}
10817 
10818       retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
10819       SET_EXPR_LOCATION (retval, loc);
10820 
10821       if (warn_sequence_point)
10822 	verify_sequence_points (retval);
10823     }
10824 
10825   ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
10826   TREE_NO_WARNING (ret_stmt) |= no_warning;
10827   return add_stmt (ret_stmt);
10828 }
10829 
10830 struct c_switch {
10831   /* The SWITCH_EXPR being built.  */
10832   tree switch_expr;
10833 
10834   /* The original type of the testing expression, i.e. before the
10835      default conversion is applied.  */
10836   tree orig_type;
10837 
10838   /* A splay-tree mapping the low element of a case range to the high
10839      element, or NULL_TREE if there is no high element.  Used to
10840      determine whether or not a new case label duplicates an old case
10841      label.  We need a tree, rather than simply a hash table, because
10842      of the GNU case range extension.  */
10843   splay_tree cases;
10844 
10845   /* The bindings at the point of the switch.  This is used for
10846      warnings crossing decls when branching to a case label.  */
10847   struct c_spot_bindings *bindings;
10848 
10849   /* The next node on the stack.  */
10850   struct c_switch *next;
10851 
10852   /* Remember whether the controlling expression had boolean type
10853      before integer promotions for the sake of -Wswitch-bool.  */
10854   bool bool_cond_p;
10855 };
10856 
10857 /* A stack of the currently active switch statements.  The innermost
10858    switch statement is on the top of the stack.  There is no need to
10859    mark the stack for garbage collection because it is only active
10860    during the processing of the body of a function, and we never
10861    collect at that point.  */
10862 
10863 struct c_switch *c_switch_stack;
10864 
10865 /* Start a C switch statement, testing expression EXP.  Return the new
10866    SWITCH_EXPR.  SWITCH_LOC is the location of the `switch'.
10867    SWITCH_COND_LOC is the location of the switch's condition.
10868    EXPLICIT_CAST_P is true if the expression EXP has an explicit cast.  */
10869 
10870 tree
c_start_case(location_t switch_loc,location_t switch_cond_loc,tree exp,bool explicit_cast_p)10871 c_start_case (location_t switch_loc,
10872 	      location_t switch_cond_loc,
10873 	      tree exp, bool explicit_cast_p)
10874 {
10875   tree orig_type = error_mark_node;
10876   bool bool_cond_p = false;
10877   struct c_switch *cs;
10878 
10879   if (exp != error_mark_node)
10880     {
10881       orig_type = TREE_TYPE (exp);
10882 
10883       if (!INTEGRAL_TYPE_P (orig_type))
10884 	{
10885 	  if (orig_type != error_mark_node)
10886 	    {
10887 	      error_at (switch_cond_loc, "switch quantity not an integer");
10888 	      orig_type = error_mark_node;
10889 	    }
10890 	  exp = integer_zero_node;
10891 	}
10892       else
10893 	{
10894 	  tree type = TYPE_MAIN_VARIANT (orig_type);
10895 	  tree e = exp;
10896 
10897 	  /* Warn if the condition has boolean value.  */
10898 	  while (TREE_CODE (e) == COMPOUND_EXPR)
10899 	    e = TREE_OPERAND (e, 1);
10900 
10901 	  if ((TREE_CODE (type) == BOOLEAN_TYPE
10902 	       || truth_value_p (TREE_CODE (e)))
10903 	      /* Explicit cast to int suppresses this warning.  */
10904 	      && !(TREE_CODE (type) == INTEGER_TYPE
10905 		   && explicit_cast_p))
10906 	    bool_cond_p = true;
10907 
10908 	  if (!in_system_header_at (input_location)
10909 	      && (type == long_integer_type_node
10910 		  || type == long_unsigned_type_node))
10911 	    warning_at (switch_cond_loc,
10912 			OPT_Wtraditional, "%<long%> switch expression not "
10913 			"converted to %<int%> in ISO C");
10914 
10915 	  exp = c_fully_fold (exp, false, NULL);
10916 	  exp = default_conversion (exp);
10917 
10918 	  if (warn_sequence_point)
10919 	    verify_sequence_points (exp);
10920 	}
10921     }
10922 
10923   /* Add this new SWITCH_EXPR to the stack.  */
10924   cs = XNEW (struct c_switch);
10925   cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
10926   SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
10927   cs->orig_type = orig_type;
10928   cs->cases = splay_tree_new (case_compare, NULL, NULL);
10929   cs->bindings = c_get_switch_bindings ();
10930   cs->bool_cond_p = bool_cond_p;
10931   cs->next = c_switch_stack;
10932   c_switch_stack = cs;
10933 
10934   return add_stmt (cs->switch_expr);
10935 }
10936 
10937 /* Process a case label at location LOC.  */
10938 
10939 tree
do_case(location_t loc,tree low_value,tree high_value)10940 do_case (location_t loc, tree low_value, tree high_value)
10941 {
10942   tree label = NULL_TREE;
10943 
10944   if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10945     {
10946       low_value = c_fully_fold (low_value, false, NULL);
10947       if (TREE_CODE (low_value) == INTEGER_CST)
10948 	pedwarn (loc, OPT_Wpedantic,
10949 		 "case label is not an integer constant expression");
10950     }
10951 
10952   if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10953     {
10954       high_value = c_fully_fold (high_value, false, NULL);
10955       if (TREE_CODE (high_value) == INTEGER_CST)
10956 	pedwarn (input_location, OPT_Wpedantic,
10957 		 "case label is not an integer constant expression");
10958     }
10959 
10960   if (c_switch_stack == NULL)
10961     {
10962       if (low_value)
10963 	error_at (loc, "case label not within a switch statement");
10964       else
10965 	error_at (loc, "%<default%> label not within a switch statement");
10966       return NULL_TREE;
10967     }
10968 
10969   if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10970 				    EXPR_LOCATION (c_switch_stack->switch_expr),
10971 				    loc))
10972     return NULL_TREE;
10973 
10974   label = c_add_case_label (loc, c_switch_stack->cases,
10975 			    SWITCH_COND (c_switch_stack->switch_expr),
10976 			    low_value, high_value);
10977   if (label == error_mark_node)
10978     label = NULL_TREE;
10979   return label;
10980 }
10981 
10982 /* Finish the switch statement.  TYPE is the original type of the
10983    controlling expression of the switch, or NULL_TREE.  */
10984 
10985 void
c_finish_case(tree body,tree type)10986 c_finish_case (tree body, tree type)
10987 {
10988   struct c_switch *cs = c_switch_stack;
10989   location_t switch_location;
10990 
10991   SWITCH_BODY (cs->switch_expr) = body;
10992 
10993   /* Emit warnings as needed.  */
10994   switch_location = EXPR_LOCATION (cs->switch_expr);
10995   c_do_switch_warnings (cs->cases, switch_location,
10996 			type ? type : TREE_TYPE (cs->switch_expr),
10997 			SWITCH_COND (cs->switch_expr), cs->bool_cond_p);
10998   if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10999     SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
11000 
11001   /* Pop the stack.  */
11002   c_switch_stack = cs->next;
11003   splay_tree_delete (cs->cases);
11004   c_release_switch_bindings (cs->bindings);
11005   XDELETE (cs);
11006 }
11007 
11008 /* Emit an if statement.  IF_LOCUS is the location of the 'if'.  COND,
11009    THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
11010    may be null.  */
11011 
11012 void
c_finish_if_stmt(location_t if_locus,tree cond,tree then_block,tree else_block)11013 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
11014 		  tree else_block)
11015 {
11016   tree stmt;
11017 
11018   stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
11019   SET_EXPR_LOCATION (stmt, if_locus);
11020   add_stmt (stmt);
11021 }
11022 
11023 /* Emit a general-purpose loop construct.  START_LOCUS is the location of
11024    the beginning of the loop.  COND is the loop condition.  COND_IS_FIRST
11025    is false for DO loops.  INCR is the FOR increment expression.  BODY is
11026    the statement controlled by the loop.  BLAB is the break label.  CLAB is
11027    the continue label.  Everything is allowed to be NULL.
11028    COND_LOCUS is the location of the loop condition, INCR_LOCUS is the
11029    location of the FOR increment expression.  */
11030 
11031 void
c_finish_loop(location_t start_locus,location_t cond_locus,tree cond,location_t incr_locus,tree incr,tree body,tree blab,tree clab,bool cond_is_first)11032 c_finish_loop (location_t start_locus, location_t cond_locus, tree cond,
11033 	       location_t incr_locus, tree incr, tree body, tree blab,
11034 	       tree clab, bool cond_is_first)
11035 {
11036   tree entry = NULL, exit = NULL, t;
11037 
11038   /* If the condition is zero don't generate a loop construct.  */
11039   if (cond && integer_zerop (cond))
11040     {
11041       if (cond_is_first)
11042 	{
11043 	  t = build_and_jump (&blab);
11044 	  SET_EXPR_LOCATION (t, start_locus);
11045 	  add_stmt (t);
11046 	}
11047     }
11048   else
11049     {
11050       tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
11051 
11052       /* If we have an exit condition, then we build an IF with gotos either
11053 	 out of the loop, or to the top of it.  If there's no exit condition,
11054 	 then we just build a jump back to the top.  */
11055       exit = build_and_jump (&LABEL_EXPR_LABEL (top));
11056 
11057       if (cond && !integer_nonzerop (cond))
11058 	{
11059 	  /* Canonicalize the loop condition to the end.  This means
11060 	     generating a branch to the loop condition.  Reuse the
11061 	     continue label, if possible.  */
11062 	  if (cond_is_first)
11063 	    {
11064 	      if (incr || !clab)
11065 		{
11066 		  entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
11067 		  t = build_and_jump (&LABEL_EXPR_LABEL (entry));
11068 		}
11069 	      else
11070 		t = build1 (GOTO_EXPR, void_type_node, clab);
11071 	      SET_EXPR_LOCATION (t, start_locus);
11072 	      add_stmt (t);
11073 	    }
11074 
11075 	  t = build_and_jump (&blab);
11076 	  exit = fold_build3_loc (cond_is_first ? start_locus : input_location,
11077 				  COND_EXPR, void_type_node, cond, exit, t);
11078 	}
11079       else
11080 	{
11081 	  /* For the backward-goto's location of an unconditional loop
11082 	     use the beginning of the body, or, if there is none, the
11083 	     top of the loop.  */
11084 	  location_t loc = EXPR_LOCATION (expr_first (body));
11085 	  if (loc == UNKNOWN_LOCATION)
11086 	    loc = start_locus;
11087 	  SET_EXPR_LOCATION (exit, loc);
11088 	}
11089 
11090       add_stmt (top);
11091     }
11092 
11093   if (body)
11094     add_stmt (body);
11095   if (clab)
11096     add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
11097   if (incr)
11098     {
11099       if (MAY_HAVE_DEBUG_MARKER_STMTS && incr_locus != UNKNOWN_LOCATION)
11100 	{
11101 	  t = build0 (DEBUG_BEGIN_STMT, void_type_node);
11102 	  SET_EXPR_LOCATION (t, incr_locus);
11103 	  add_stmt (t);
11104 	}
11105       add_stmt (incr);
11106     }
11107   if (entry)
11108     add_stmt (entry);
11109   if (MAY_HAVE_DEBUG_MARKER_STMTS && cond_locus != UNKNOWN_LOCATION)
11110     {
11111       t = build0 (DEBUG_BEGIN_STMT, void_type_node);
11112       SET_EXPR_LOCATION (t, cond_locus);
11113       add_stmt (t);
11114     }
11115   if (exit)
11116     add_stmt (exit);
11117   if (blab)
11118     add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
11119 }
11120 
11121 tree
c_finish_bc_stmt(location_t loc,tree * label_p,bool is_break)11122 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
11123 {
11124   bool skip;
11125   tree label = *label_p;
11126 
11127   /* In switch statements break is sometimes stylistically used after
11128      a return statement.  This can lead to spurious warnings about
11129      control reaching the end of a non-void function when it is
11130      inlined.  Note that we are calling block_may_fallthru with
11131      language specific tree nodes; this works because
11132      block_may_fallthru returns true when given something it does not
11133      understand.  */
11134   skip = !block_may_fallthru (cur_stmt_list);
11135 
11136   if (!label)
11137     {
11138       if (!skip)
11139 	*label_p = label = create_artificial_label (loc);
11140     }
11141   else if (TREE_CODE (label) == LABEL_DECL)
11142     ;
11143   else switch (TREE_INT_CST_LOW (label))
11144     {
11145     case 0:
11146       if (is_break)
11147 	error_at (loc, "break statement not within loop or switch");
11148       else
11149 	error_at (loc, "continue statement not within a loop");
11150       return NULL_TREE;
11151 
11152     case 1:
11153       gcc_assert (is_break);
11154       error_at (loc, "break statement used with OpenMP for loop");
11155       return NULL_TREE;
11156 
11157     case 2:
11158       if (is_break)
11159 	error ("break statement within %<#pragma simd%> loop body");
11160       else
11161 	error ("continue statement within %<#pragma simd%> loop body");
11162       return NULL_TREE;
11163 
11164     default:
11165       gcc_unreachable ();
11166     }
11167 
11168   if (skip)
11169     return NULL_TREE;
11170 
11171   if (!is_break)
11172     add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
11173 
11174   return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
11175 }
11176 
11177 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr.  */
11178 
11179 static void
emit_side_effect_warnings(location_t loc,tree expr)11180 emit_side_effect_warnings (location_t loc, tree expr)
11181 {
11182   if (expr == error_mark_node)
11183     ;
11184   else if (!TREE_SIDE_EFFECTS (expr))
11185     {
11186       if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
11187 	warning_at (loc, OPT_Wunused_value, "statement with no effect");
11188     }
11189   else if (TREE_CODE (expr) == COMPOUND_EXPR)
11190     {
11191       tree r = expr;
11192       location_t cloc = loc;
11193       while (TREE_CODE (r) == COMPOUND_EXPR)
11194 	{
11195 	  if (EXPR_HAS_LOCATION (r))
11196 	    cloc = EXPR_LOCATION (r);
11197 	  r = TREE_OPERAND (r, 1);
11198 	}
11199       if (!TREE_SIDE_EFFECTS (r)
11200 	  && !VOID_TYPE_P (TREE_TYPE (r))
11201 	  && !CONVERT_EXPR_P (r)
11202 	  && !TREE_NO_WARNING (r)
11203 	  && !TREE_NO_WARNING (expr))
11204 	warning_at (cloc, OPT_Wunused_value,
11205 		    "right-hand operand of comma expression has no effect");
11206     }
11207   else
11208     warn_if_unused_value (expr, loc);
11209 }
11210 
11211 /* Process an expression as if it were a complete statement.  Emit
11212    diagnostics, but do not call ADD_STMT.  LOC is the location of the
11213    statement.  */
11214 
11215 tree
c_process_expr_stmt(location_t loc,tree expr)11216 c_process_expr_stmt (location_t loc, tree expr)
11217 {
11218   tree exprv;
11219 
11220   if (!expr)
11221     return NULL_TREE;
11222 
11223   expr = c_fully_fold (expr, false, NULL);
11224 
11225   if (warn_sequence_point)
11226     verify_sequence_points (expr);
11227 
11228   if (TREE_TYPE (expr) != error_mark_node
11229       && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
11230       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
11231     error_at (loc, "expression statement has incomplete type");
11232 
11233   /* If we're not processing a statement expression, warn about unused values.
11234      Warnings for statement expressions will be emitted later, once we figure
11235      out which is the result.  */
11236   if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11237       && warn_unused_value)
11238     emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
11239 
11240   exprv = expr;
11241   while (TREE_CODE (exprv) == COMPOUND_EXPR)
11242     exprv = TREE_OPERAND (exprv, 1);
11243   while (CONVERT_EXPR_P (exprv))
11244     exprv = TREE_OPERAND (exprv, 0);
11245   if (DECL_P (exprv)
11246       || handled_component_p (exprv)
11247       || TREE_CODE (exprv) == ADDR_EXPR)
11248     mark_exp_read (exprv);
11249 
11250   /* If the expression is not of a type to which we cannot assign a line
11251      number, wrap the thing in a no-op NOP_EXPR.  */
11252   if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
11253     {
11254       expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11255       SET_EXPR_LOCATION (expr, loc);
11256     }
11257 
11258   return expr;
11259 }
11260 
11261 /* Emit an expression as a statement.  LOC is the location of the
11262    expression.  */
11263 
11264 tree
c_finish_expr_stmt(location_t loc,tree expr)11265 c_finish_expr_stmt (location_t loc, tree expr)
11266 {
11267   if (expr)
11268     return add_stmt (c_process_expr_stmt (loc, expr));
11269   else
11270     return NULL;
11271 }
11272 
11273 /* Do the opposite and emit a statement as an expression.  To begin,
11274    create a new binding level and return it.  */
11275 
11276 tree
c_begin_stmt_expr(void)11277 c_begin_stmt_expr (void)
11278 {
11279   tree ret;
11280 
11281   /* We must force a BLOCK for this level so that, if it is not expanded
11282      later, there is a way to turn off the entire subtree of blocks that
11283      are contained in it.  */
11284   keep_next_level ();
11285   ret = c_begin_compound_stmt (true);
11286 
11287   c_bindings_start_stmt_expr (c_switch_stack == NULL
11288 			      ? NULL
11289 			      : c_switch_stack->bindings);
11290 
11291   /* Mark the current statement list as belonging to a statement list.  */
11292   STATEMENT_LIST_STMT_EXPR (ret) = 1;
11293 
11294   return ret;
11295 }
11296 
11297 /* LOC is the location of the compound statement to which this body
11298    belongs.  */
11299 
11300 tree
c_finish_stmt_expr(location_t loc,tree body)11301 c_finish_stmt_expr (location_t loc, tree body)
11302 {
11303   tree last, type, tmp, val;
11304   tree *last_p;
11305 
11306   body = c_end_compound_stmt (loc, body, true);
11307 
11308   c_bindings_end_stmt_expr (c_switch_stack == NULL
11309 			    ? NULL
11310 			    : c_switch_stack->bindings);
11311 
11312   /* Locate the last statement in BODY.  See c_end_compound_stmt
11313      about always returning a BIND_EXPR.  */
11314   last_p = &BIND_EXPR_BODY (body);
11315   last = BIND_EXPR_BODY (body);
11316 
11317  continue_searching:
11318   if (TREE_CODE (last) == STATEMENT_LIST)
11319     {
11320       tree_stmt_iterator l = tsi_last (last);
11321 
11322       while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
11323 	tsi_prev (&l);
11324 
11325       /* This can happen with degenerate cases like ({ }).  No value.  */
11326       if (tsi_end_p (l))
11327 	return body;
11328 
11329       /* If we're supposed to generate side effects warnings, process
11330 	 all of the statements except the last.  */
11331       if (warn_unused_value)
11332 	{
11333 	  for (tree_stmt_iterator i = tsi_start (last);
11334 	       tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
11335 	    {
11336 	      location_t tloc;
11337 	      tree t = tsi_stmt (i);
11338 
11339 	      tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
11340 	      emit_side_effect_warnings (tloc, t);
11341 	    }
11342 	}
11343       last_p = tsi_stmt_ptr (l);
11344       last = *last_p;
11345     }
11346 
11347   /* If the end of the list is exception related, then the list was split
11348      by a call to push_cleanup.  Continue searching.  */
11349   if (TREE_CODE (last) == TRY_FINALLY_EXPR
11350       || TREE_CODE (last) == TRY_CATCH_EXPR)
11351     {
11352       last_p = &TREE_OPERAND (last, 0);
11353       last = *last_p;
11354       goto continue_searching;
11355     }
11356 
11357   if (last == error_mark_node)
11358     return last;
11359 
11360   /* In the case that the BIND_EXPR is not necessary, return the
11361      expression out from inside it.  */
11362   if ((last == BIND_EXPR_BODY (body)
11363        /* Skip nested debug stmts.  */
11364        || last == expr_first (BIND_EXPR_BODY (body)))
11365       && BIND_EXPR_VARS (body) == NULL)
11366     {
11367       /* Even if this looks constant, do not allow it in a constant
11368 	 expression.  */
11369       last = c_wrap_maybe_const (last, true);
11370       /* Do not warn if the return value of a statement expression is
11371 	 unused.  */
11372       TREE_NO_WARNING (last) = 1;
11373       return last;
11374     }
11375 
11376   /* Extract the type of said expression.  */
11377   type = TREE_TYPE (last);
11378 
11379   /* If we're not returning a value at all, then the BIND_EXPR that
11380      we already have is a fine expression to return.  */
11381   if (!type || VOID_TYPE_P (type))
11382     return body;
11383 
11384   /* Now that we've located the expression containing the value, it seems
11385      silly to make voidify_wrapper_expr repeat the process.  Create a
11386      temporary of the appropriate type and stick it in a TARGET_EXPR.  */
11387   tmp = create_tmp_var_raw (type);
11388 
11389   /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt.  This avoids
11390      tree_expr_nonnegative_p giving up immediately.  */
11391   val = last;
11392   if (TREE_CODE (val) == NOP_EXPR
11393       && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
11394     val = TREE_OPERAND (val, 0);
11395 
11396   *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
11397   SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
11398 
11399   {
11400     tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
11401     SET_EXPR_LOCATION (t, loc);
11402     return t;
11403   }
11404 }
11405 
11406 /* Begin and end compound statements.  This is as simple as pushing
11407    and popping new statement lists from the tree.  */
11408 
11409 tree
c_begin_compound_stmt(bool do_scope)11410 c_begin_compound_stmt (bool do_scope)
11411 {
11412   tree stmt = push_stmt_list ();
11413   if (do_scope)
11414     push_scope ();
11415   return stmt;
11416 }
11417 
11418 /* End a compound statement.  STMT is the statement.  LOC is the
11419    location of the compound statement-- this is usually the location
11420    of the opening brace.  */
11421 
11422 tree
c_end_compound_stmt(location_t loc,tree stmt,bool do_scope)11423 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
11424 {
11425   tree block = NULL;
11426 
11427   if (do_scope)
11428     {
11429       if (c_dialect_objc ())
11430 	objc_clear_super_receiver ();
11431       block = pop_scope ();
11432     }
11433 
11434   stmt = pop_stmt_list (stmt);
11435   stmt = c_build_bind_expr (loc, block, stmt);
11436 
11437   /* If this compound statement is nested immediately inside a statement
11438      expression, then force a BIND_EXPR to be created.  Otherwise we'll
11439      do the wrong thing for ({ { 1; } }) or ({ 1; { } }).  In particular,
11440      STATEMENT_LISTs merge, and thus we can lose track of what statement
11441      was really last.  */
11442   if (building_stmt_list_p ()
11443       && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
11444       && TREE_CODE (stmt) != BIND_EXPR)
11445     {
11446       stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
11447       TREE_SIDE_EFFECTS (stmt) = 1;
11448       SET_EXPR_LOCATION (stmt, loc);
11449     }
11450 
11451   return stmt;
11452 }
11453 
11454 /* Queue a cleanup.  CLEANUP is an expression/statement to be executed
11455    when the current scope is exited.  EH_ONLY is true when this is not
11456    meant to apply to normal control flow transfer.  */
11457 
11458 void
push_cleanup(tree decl,tree cleanup,bool eh_only)11459 push_cleanup (tree decl, tree cleanup, bool eh_only)
11460 {
11461   enum tree_code code;
11462   tree stmt, list;
11463   bool stmt_expr;
11464 
11465   code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
11466   stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
11467   add_stmt (stmt);
11468   stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11469   list = push_stmt_list ();
11470   TREE_OPERAND (stmt, 0) = list;
11471   STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
11472 }
11473 
11474 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11475    into a value of TYPE type.  Comparison is done via VEC_COND_EXPR.  */
11476 
11477 static tree
build_vec_cmp(tree_code code,tree type,tree arg0,tree arg1)11478 build_vec_cmp (tree_code code, tree type,
11479 	       tree arg0, tree arg1)
11480 {
11481   tree zero_vec = build_zero_cst (type);
11482   tree minus_one_vec = build_minus_one_cst (type);
11483   tree cmp_type = truth_type_for (type);
11484   tree cmp = build2 (code, cmp_type, arg0, arg1);
11485   return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11486 }
11487 
11488 /* Build a binary-operation expression without default conversions.
11489    CODE is the kind of expression to build.
11490    LOCATION is the operator's location.
11491    This function differs from `build' in several ways:
11492    the data type of the result is computed and recorded in it,
11493    warnings are generated if arg data types are invalid,
11494    special handling for addition and subtraction of pointers is known,
11495    and some optimization is done (operations on narrow ints
11496    are done in the narrower type when that gives the same result).
11497    Constant folding is also done before the result is returned.
11498 
11499    Note that the operands will never have enumeral types, or function
11500    or array types, because either they will have the default conversions
11501    performed or they have both just been converted to some other type in which
11502    the arithmetic is to be done.  */
11503 
11504 tree
build_binary_op(location_t location,enum tree_code code,tree orig_op0,tree orig_op1,bool convert_p)11505 build_binary_op (location_t location, enum tree_code code,
11506 		 tree orig_op0, tree orig_op1, bool convert_p)
11507 {
11508   tree type0, type1, orig_type0, orig_type1;
11509   tree eptype;
11510   enum tree_code code0, code1;
11511   tree op0, op1;
11512   tree ret = error_mark_node;
11513   const char *invalid_op_diag;
11514   bool op0_int_operands, op1_int_operands;
11515   bool int_const, int_const_or_overflow, int_operands;
11516 
11517   /* Expression code to give to the expression when it is built.
11518      Normally this is CODE, which is what the caller asked for,
11519      but in some special cases we change it.  */
11520   enum tree_code resultcode = code;
11521 
11522   /* Data type in which the computation is to be performed.
11523      In the simplest cases this is the common type of the arguments.  */
11524   tree result_type = NULL;
11525 
11526   /* When the computation is in excess precision, the type of the
11527      final EXCESS_PRECISION_EXPR.  */
11528   tree semantic_result_type = NULL;
11529 
11530   /* Nonzero means operands have already been type-converted
11531      in whatever way is necessary.
11532      Zero means they need to be converted to RESULT_TYPE.  */
11533   int converted = 0;
11534 
11535   /* Nonzero means create the expression with this type, rather than
11536      RESULT_TYPE.  */
11537   tree build_type = NULL_TREE;
11538 
11539   /* Nonzero means after finally constructing the expression
11540      convert it to this type.  */
11541   tree final_type = NULL_TREE;
11542 
11543   /* Nonzero if this is an operation like MIN or MAX which can
11544      safely be computed in short if both args are promoted shorts.
11545      Also implies COMMON.
11546      -1 indicates a bitwise operation; this makes a difference
11547      in the exact conditions for when it is safe to do the operation
11548      in a narrower mode.  */
11549   int shorten = 0;
11550 
11551   /* Nonzero if this is a comparison operation;
11552      if both args are promoted shorts, compare the original shorts.
11553      Also implies COMMON.  */
11554   int short_compare = 0;
11555 
11556   /* Nonzero if this is a right-shift operation, which can be computed on the
11557      original short and then promoted if the operand is a promoted short.  */
11558   int short_shift = 0;
11559 
11560   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
11561   int common = 0;
11562 
11563   /* True means types are compatible as far as ObjC is concerned.  */
11564   bool objc_ok;
11565 
11566   /* True means this is an arithmetic operation that may need excess
11567      precision.  */
11568   bool may_need_excess_precision;
11569 
11570   /* True means this is a boolean operation that converts both its
11571      operands to truth-values.  */
11572   bool boolean_op = false;
11573 
11574   /* Remember whether we're doing / or %.  */
11575   bool doing_div_or_mod = false;
11576 
11577   /* Remember whether we're doing << or >>.  */
11578   bool doing_shift = false;
11579 
11580   /* Tree holding instrumentation expression.  */
11581   tree instrument_expr = NULL;
11582 
11583   if (location == UNKNOWN_LOCATION)
11584     location = input_location;
11585 
11586   op0 = orig_op0;
11587   op1 = orig_op1;
11588 
11589   op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11590   if (op0_int_operands)
11591     op0 = remove_c_maybe_const_expr (op0);
11592   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11593   if (op1_int_operands)
11594     op1 = remove_c_maybe_const_expr (op1);
11595   int_operands = (op0_int_operands && op1_int_operands);
11596   if (int_operands)
11597     {
11598       int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11599 			       && TREE_CODE (orig_op1) == INTEGER_CST);
11600       int_const = (int_const_or_overflow
11601 		   && !TREE_OVERFLOW (orig_op0)
11602 		   && !TREE_OVERFLOW (orig_op1));
11603     }
11604   else
11605     int_const = int_const_or_overflow = false;
11606 
11607   /* Do not apply default conversion in mixed vector/scalar expression.  */
11608   if (convert_p
11609       && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
11610     {
11611       op0 = default_conversion (op0);
11612       op1 = default_conversion (op1);
11613     }
11614 
11615   orig_type0 = type0 = TREE_TYPE (op0);
11616 
11617   orig_type1 = type1 = TREE_TYPE (op1);
11618 
11619   /* The expression codes of the data types of the arguments tell us
11620      whether the arguments are integers, floating, pointers, etc.  */
11621   code0 = TREE_CODE (type0);
11622   code1 = TREE_CODE (type1);
11623 
11624   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
11625   STRIP_TYPE_NOPS (op0);
11626   STRIP_TYPE_NOPS (op1);
11627 
11628   /* If an error was already reported for one of the arguments,
11629      avoid reporting another error.  */
11630 
11631   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11632     return error_mark_node;
11633 
11634   if (code0 == POINTER_TYPE
11635       && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11636     return error_mark_node;
11637 
11638   if (code1 == POINTER_TYPE
11639       && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11640     return error_mark_node;
11641 
11642   if ((invalid_op_diag
11643        = targetm.invalid_binary_op (code, type0, type1)))
11644     {
11645       error_at (location, invalid_op_diag);
11646       return error_mark_node;
11647     }
11648 
11649   switch (code)
11650     {
11651     case PLUS_EXPR:
11652     case MINUS_EXPR:
11653     case MULT_EXPR:
11654     case TRUNC_DIV_EXPR:
11655     case CEIL_DIV_EXPR:
11656     case FLOOR_DIV_EXPR:
11657     case ROUND_DIV_EXPR:
11658     case EXACT_DIV_EXPR:
11659       may_need_excess_precision = true;
11660       break;
11661 
11662     case EQ_EXPR:
11663     case NE_EXPR:
11664     case LE_EXPR:
11665     case GE_EXPR:
11666     case LT_EXPR:
11667     case GT_EXPR:
11668       /* Excess precision for implicit conversions of integers to
11669 	 floating point in C11 and later.  */
11670       may_need_excess_precision = (flag_isoc11
11671 				   && (ANY_INTEGRAL_TYPE_P (type0)
11672 				       || ANY_INTEGRAL_TYPE_P (type1)));
11673       break;
11674 
11675     default:
11676       may_need_excess_precision = false;
11677       break;
11678     }
11679   if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11680     {
11681       op0 = TREE_OPERAND (op0, 0);
11682       type0 = TREE_TYPE (op0);
11683     }
11684   else if (may_need_excess_precision
11685 	   && (eptype = excess_precision_type (type0)) != NULL_TREE)
11686     {
11687       type0 = eptype;
11688       op0 = convert (eptype, op0);
11689     }
11690   if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11691     {
11692       op1 = TREE_OPERAND (op1, 0);
11693       type1 = TREE_TYPE (op1);
11694     }
11695   else if (may_need_excess_precision
11696 	   && (eptype = excess_precision_type (type1)) != NULL_TREE)
11697     {
11698       type1 = eptype;
11699       op1 = convert (eptype, op1);
11700     }
11701 
11702   objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11703 
11704   /* In case when one of the operands of the binary operation is
11705      a vector and another is a scalar -- convert scalar to vector.  */
11706   if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
11707       || (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
11708     {
11709       enum stv_conv convert_flag = scalar_to_vector (location, code, orig_op0,
11710 						     orig_op1, true);
11711 
11712       switch (convert_flag)
11713 	{
11714 	  case stv_error:
11715 	    return error_mark_node;
11716 	  case stv_firstarg:
11717 	    {
11718               bool maybe_const = true;
11719               tree sc;
11720               sc = c_fully_fold (op0, false, &maybe_const);
11721               sc = save_expr (sc);
11722               sc = convert (TREE_TYPE (type1), sc);
11723               op0 = build_vector_from_val (type1, sc);
11724               if (!maybe_const)
11725                 op0 = c_wrap_maybe_const (op0, true);
11726               orig_type0 = type0 = TREE_TYPE (op0);
11727               code0 = TREE_CODE (type0);
11728               converted = 1;
11729               break;
11730 	    }
11731 	  case stv_secondarg:
11732 	    {
11733 	      bool maybe_const = true;
11734 	      tree sc;
11735 	      sc = c_fully_fold (op1, false, &maybe_const);
11736 	      sc = save_expr (sc);
11737 	      sc = convert (TREE_TYPE (type0), sc);
11738 	      op1 = build_vector_from_val (type0, sc);
11739 	      if (!maybe_const)
11740 		op1 = c_wrap_maybe_const (op1, true);
11741 	      orig_type1 = type1 = TREE_TYPE (op1);
11742 	      code1 = TREE_CODE (type1);
11743 	      converted = 1;
11744 	      break;
11745 	    }
11746 	  default:
11747 	    break;
11748 	}
11749     }
11750 
11751   switch (code)
11752     {
11753     case PLUS_EXPR:
11754       /* Handle the pointer + int case.  */
11755       if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11756 	{
11757 	  ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
11758 	  goto return_build_binary_op;
11759 	}
11760       else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
11761 	{
11762 	  ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
11763 	  goto return_build_binary_op;
11764 	}
11765       else
11766 	common = 1;
11767       break;
11768 
11769     case MINUS_EXPR:
11770       /* Subtraction of two similar pointers.
11771 	 We must subtract them as integers, then divide by object size.  */
11772       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
11773 	  && comp_target_types (location, type0, type1))
11774 	{
11775 	  ret = pointer_diff (location, op0, op1, &instrument_expr);
11776 	  goto return_build_binary_op;
11777 	}
11778       /* Handle pointer minus int.  Just like pointer plus int.  */
11779       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11780 	{
11781 	  ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
11782 	  goto return_build_binary_op;
11783 	}
11784       else
11785 	common = 1;
11786       break;
11787 
11788     case MULT_EXPR:
11789       common = 1;
11790       break;
11791 
11792     case TRUNC_DIV_EXPR:
11793     case CEIL_DIV_EXPR:
11794     case FLOOR_DIV_EXPR:
11795     case ROUND_DIV_EXPR:
11796     case EXACT_DIV_EXPR:
11797       doing_div_or_mod = true;
11798       warn_for_div_by_zero (location, op1);
11799 
11800       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11801 	   || code0 == FIXED_POINT_TYPE
11802 	   || code0 == COMPLEX_TYPE
11803 	   || gnu_vector_type_p (type0))
11804 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11805 	      || code1 == FIXED_POINT_TYPE
11806 	      || code1 == COMPLEX_TYPE
11807 	      || gnu_vector_type_p (type1)))
11808 	{
11809 	  enum tree_code tcode0 = code0, tcode1 = code1;
11810 
11811 	  if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11812 	    tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
11813 	  if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
11814 	    tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
11815 
11816 	  if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11817 	      || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
11818 	    resultcode = RDIV_EXPR;
11819 	  else
11820 	    /* Although it would be tempting to shorten always here, that
11821 	       loses on some targets, since the modulo instruction is
11822 	       undefined if the quotient can't be represented in the
11823 	       computation mode.  We shorten only if unsigned or if
11824 	       dividing by something we know != -1.  */
11825 	    shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11826 		       || (TREE_CODE (op1) == INTEGER_CST
11827 			   && !integer_all_onesp (op1)));
11828 	  common = 1;
11829 	}
11830       break;
11831 
11832     case BIT_AND_EXPR:
11833     case BIT_IOR_EXPR:
11834     case BIT_XOR_EXPR:
11835       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11836 	shorten = -1;
11837       /* Allow vector types which are not floating point types.   */
11838       else if (gnu_vector_type_p (type0)
11839 	       && gnu_vector_type_p (type1)
11840 	       && !VECTOR_FLOAT_TYPE_P (type0)
11841 	       && !VECTOR_FLOAT_TYPE_P (type1))
11842 	common = 1;
11843       break;
11844 
11845     case TRUNC_MOD_EXPR:
11846     case FLOOR_MOD_EXPR:
11847       doing_div_or_mod = true;
11848       warn_for_div_by_zero (location, op1);
11849 
11850       if (gnu_vector_type_p (type0)
11851 	  && gnu_vector_type_p (type1)
11852 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11853 	  && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11854 	common = 1;
11855       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11856 	{
11857 	  /* Although it would be tempting to shorten always here, that loses
11858 	     on some targets, since the modulo instruction is undefined if the
11859 	     quotient can't be represented in the computation mode.  We shorten
11860 	     only if unsigned or if dividing by something we know != -1.  */
11861 	  shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
11862 		     || (TREE_CODE (op1) == INTEGER_CST
11863 			 && !integer_all_onesp (op1)));
11864 	  common = 1;
11865 	}
11866       break;
11867 
11868     case TRUTH_ANDIF_EXPR:
11869     case TRUTH_ORIF_EXPR:
11870     case TRUTH_AND_EXPR:
11871     case TRUTH_OR_EXPR:
11872     case TRUTH_XOR_EXPR:
11873       if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
11874 	   || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11875 	   || code0 == FIXED_POINT_TYPE)
11876 	  && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
11877 	      || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11878 	      || code1 == FIXED_POINT_TYPE))
11879 	{
11880 	  /* Result of these operations is always an int,
11881 	     but that does not mean the operands should be
11882 	     converted to ints!  */
11883 	  result_type = integer_type_node;
11884 	  if (op0_int_operands)
11885 	    {
11886 	      op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11887 	      op0 = remove_c_maybe_const_expr (op0);
11888 	    }
11889 	  else
11890 	    op0 = c_objc_common_truthvalue_conversion (location, op0);
11891 	  if (op1_int_operands)
11892 	    {
11893 	      op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11894 	      op1 = remove_c_maybe_const_expr (op1);
11895 	    }
11896 	  else
11897 	    op1 = c_objc_common_truthvalue_conversion (location, op1);
11898 	  converted = 1;
11899 	  boolean_op = true;
11900 	}
11901       if (code == TRUTH_ANDIF_EXPR)
11902 	{
11903 	  int_const_or_overflow = (int_operands
11904 				   && TREE_CODE (orig_op0) == INTEGER_CST
11905 				   && (op0 == truthvalue_false_node
11906 				       || TREE_CODE (orig_op1) == INTEGER_CST));
11907 	  int_const = (int_const_or_overflow
11908 		       && !TREE_OVERFLOW (orig_op0)
11909 		       && (op0 == truthvalue_false_node
11910 			   || !TREE_OVERFLOW (orig_op1)));
11911 	}
11912       else if (code == TRUTH_ORIF_EXPR)
11913 	{
11914 	  int_const_or_overflow = (int_operands
11915 				   && TREE_CODE (orig_op0) == INTEGER_CST
11916 				   && (op0 == truthvalue_true_node
11917 				       || TREE_CODE (orig_op1) == INTEGER_CST));
11918 	  int_const = (int_const_or_overflow
11919 		       && !TREE_OVERFLOW (orig_op0)
11920 		       && (op0 == truthvalue_true_node
11921 			   || !TREE_OVERFLOW (orig_op1)));
11922 	}
11923       break;
11924 
11925       /* Shift operations: result has same type as first operand;
11926 	 always convert second operand to int.
11927 	 Also set SHORT_SHIFT if shifting rightward.  */
11928 
11929     case RSHIFT_EXPR:
11930       if (gnu_vector_type_p (type0)
11931 	  && gnu_vector_type_p (type1)
11932 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11933 	  && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11934 	  && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11935 		       TYPE_VECTOR_SUBPARTS (type1)))
11936 	{
11937 	  result_type = type0;
11938 	  converted = 1;
11939 	}
11940       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11941 		|| (gnu_vector_type_p (type0)
11942 		    && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
11943 	       && code1 == INTEGER_TYPE)
11944 	{
11945 	  doing_shift = true;
11946 	  if (TREE_CODE (op1) == INTEGER_CST)
11947 	    {
11948 	      if (tree_int_cst_sgn (op1) < 0)
11949 		{
11950 		  int_const = false;
11951 		  if (c_inhibit_evaluation_warnings == 0)
11952 		    warning_at (location, OPT_Wshift_count_negative,
11953 				"right shift count is negative");
11954 		}
11955 	      else if (code0 == VECTOR_TYPE)
11956 		{
11957 		  if (compare_tree_int (op1,
11958 					TYPE_PRECISION (TREE_TYPE (type0)))
11959 		      >= 0)
11960 		    {
11961 		      int_const = false;
11962 		      if (c_inhibit_evaluation_warnings == 0)
11963 			warning_at (location, OPT_Wshift_count_overflow,
11964 				    "right shift count >= width of vector element");
11965 		    }
11966 		}
11967 	      else
11968 		{
11969 		  if (!integer_zerop (op1))
11970 		    short_shift = 1;
11971 
11972 		  if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
11973 		    {
11974 		      int_const = false;
11975 		      if (c_inhibit_evaluation_warnings == 0)
11976 			warning_at (location, OPT_Wshift_count_overflow,
11977 				    "right shift count >= width of type");
11978 		    }
11979 		}
11980 	    }
11981 
11982 	  /* Use the type of the value to be shifted.  */
11983 	  result_type = type0;
11984 	  /* Avoid converting op1 to result_type later.  */
11985 	  converted = 1;
11986 	}
11987       break;
11988 
11989     case LSHIFT_EXPR:
11990       if (gnu_vector_type_p (type0)
11991 	  && gnu_vector_type_p (type1)
11992 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11993 	  && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11994 	  && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11995 		       TYPE_VECTOR_SUBPARTS (type1)))
11996 	{
11997 	  result_type = type0;
11998 	  converted = 1;
11999 	}
12000       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
12001 		|| (gnu_vector_type_p (type0)
12002 		    && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
12003 	       && code1 == INTEGER_TYPE)
12004 	{
12005 	  doing_shift = true;
12006 	  if (TREE_CODE (op0) == INTEGER_CST
12007 	      && tree_int_cst_sgn (op0) < 0
12008 	      && !TYPE_OVERFLOW_WRAPS (type0))
12009 	    {
12010 	      /* Don't reject a left shift of a negative value in a context
12011 		 where a constant expression is needed in C90.  */
12012 	      if (flag_isoc99)
12013 		int_const = false;
12014 	      if (c_inhibit_evaluation_warnings == 0)
12015 		warning_at (location, OPT_Wshift_negative_value,
12016 			    "left shift of negative value");
12017 	    }
12018 	  if (TREE_CODE (op1) == INTEGER_CST)
12019 	    {
12020 	      if (tree_int_cst_sgn (op1) < 0)
12021 		{
12022 		  int_const = false;
12023 		  if (c_inhibit_evaluation_warnings == 0)
12024 		    warning_at (location, OPT_Wshift_count_negative,
12025 				"left shift count is negative");
12026 		}
12027 	      else if (code0 == VECTOR_TYPE)
12028 		{
12029 		  if (compare_tree_int (op1,
12030 					TYPE_PRECISION (TREE_TYPE (type0)))
12031 		      >= 0)
12032 		    {
12033 		      int_const = false;
12034 		      if (c_inhibit_evaluation_warnings == 0)
12035 			warning_at (location, OPT_Wshift_count_overflow,
12036 				    "left shift count >= width of vector element");
12037 		    }
12038 		}
12039 	      else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
12040 		{
12041 		  int_const = false;
12042 		  if (c_inhibit_evaluation_warnings == 0)
12043 		    warning_at (location, OPT_Wshift_count_overflow,
12044 				"left shift count >= width of type");
12045 		}
12046 	      else if (TREE_CODE (op0) == INTEGER_CST
12047 		       && maybe_warn_shift_overflow (location, op0, op1)
12048 		       && flag_isoc99)
12049 		int_const = false;
12050 	    }
12051 
12052 	  /* Use the type of the value to be shifted.  */
12053 	  result_type = type0;
12054 	  /* Avoid converting op1 to result_type later.  */
12055 	  converted = 1;
12056 	}
12057       break;
12058 
12059     case EQ_EXPR:
12060     case NE_EXPR:
12061       if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12062         {
12063           tree intt;
12064 	  if (!vector_types_compatible_elements_p (type0, type1))
12065             {
12066               error_at (location, "comparing vectors with different "
12067                                   "element types");
12068               return error_mark_node;
12069             }
12070 
12071 	  if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12072 			TYPE_VECTOR_SUBPARTS (type1)))
12073             {
12074               error_at (location, "comparing vectors with different "
12075                                   "number of elements");
12076               return error_mark_node;
12077             }
12078 
12079 	  /* It's not precisely specified how the usual arithmetic
12080 	     conversions apply to the vector types.  Here, we use
12081 	     the unsigned type if one of the operands is signed and
12082 	     the other one is unsigned.  */
12083 	  if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12084 	    {
12085 	      if (!TYPE_UNSIGNED (type0))
12086 		op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12087 	      else
12088 		op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12089 	      warning_at (location, OPT_Wsign_compare, "comparison between "
12090 			  "types %qT and %qT", type0, type1);
12091 	    }
12092 
12093           /* Always construct signed integer vector type.  */
12094           intt = c_common_type_for_size (GET_MODE_BITSIZE
12095 					 (SCALAR_TYPE_MODE
12096 					  (TREE_TYPE (type0))), 0);
12097 	  if (!intt)
12098 	    {
12099 	      error_at (location, "could not find an integer type "
12100 				  "of the same size as %qT",
12101 			TREE_TYPE (type0));
12102 	      return error_mark_node;
12103 	    }
12104           result_type = build_opaque_vector_type (intt,
12105 						  TYPE_VECTOR_SUBPARTS (type0));
12106           converted = 1;
12107 	  ret = build_vec_cmp (resultcode, result_type, op0, op1);
12108 	  goto return_build_binary_op;
12109         }
12110       if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
12111 	warning_at (location,
12112 		    OPT_Wfloat_equal,
12113 		    "comparing floating-point with %<==%> or %<!=%> is unsafe");
12114       /* Result of comparison is always int,
12115 	 but don't convert the args to int!  */
12116       build_type = integer_type_node;
12117       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12118 	   || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
12119 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12120 	      || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
12121 	short_compare = 1;
12122       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12123 	{
12124 	  if (TREE_CODE (op0) == ADDR_EXPR
12125 	      && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
12126 	      && !from_macro_expansion_at (location))
12127 	    {
12128 	      if (code == EQ_EXPR)
12129 		warning_at (location,
12130 			    OPT_Waddress,
12131 			    "the comparison will always evaluate as %<false%> "
12132 			    "for the address of %qD will never be NULL",
12133 			    TREE_OPERAND (op0, 0));
12134 	      else
12135 		warning_at (location,
12136 			    OPT_Waddress,
12137 			    "the comparison will always evaluate as %<true%> "
12138 			    "for the address of %qD will never be NULL",
12139 			    TREE_OPERAND (op0, 0));
12140 	    }
12141 	  result_type = type0;
12142 	}
12143       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12144 	{
12145 	  if (TREE_CODE (op1) == ADDR_EXPR
12146 	      && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
12147 	      && !from_macro_expansion_at (location))
12148 	    {
12149 	      if (code == EQ_EXPR)
12150 		warning_at (location,
12151 			    OPT_Waddress,
12152 			    "the comparison will always evaluate as %<false%> "
12153 			    "for the address of %qD will never be NULL",
12154 			    TREE_OPERAND (op1, 0));
12155 	      else
12156 		warning_at (location,
12157 			    OPT_Waddress,
12158 			    "the comparison will always evaluate as %<true%> "
12159 			    "for the address of %qD will never be NULL",
12160 			    TREE_OPERAND (op1, 0));
12161 	    }
12162 	  result_type = type1;
12163 	}
12164       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12165 	{
12166 	  tree tt0 = TREE_TYPE (type0);
12167 	  tree tt1 = TREE_TYPE (type1);
12168 	  addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
12169 	  addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
12170 	  addr_space_t as_common = ADDR_SPACE_GENERIC;
12171 
12172 	  /* Anything compares with void *.  void * compares with anything.
12173 	     Otherwise, the targets must be compatible
12174 	     and both must be object or both incomplete.  */
12175 	  if (comp_target_types (location, type0, type1))
12176 	    result_type = common_pointer_type (type0, type1);
12177 	  else if (!addr_space_superset (as0, as1, &as_common))
12178 	    {
12179 	      error_at (location, "comparison of pointers to "
12180 			"disjoint address spaces");
12181 	      return error_mark_node;
12182 	    }
12183 	  else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
12184 	    {
12185 	      if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
12186 		pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12187 			 "comparison of %<void *%> with function pointer");
12188 	    }
12189 	  else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
12190 	    {
12191 	      if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
12192 		pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12193 			 "comparison of %<void *%> with function pointer");
12194 	    }
12195 	  else
12196 	    /* Avoid warning about the volatile ObjC EH puts on decls.  */
12197 	    if (!objc_ok)
12198 	      pedwarn (location, 0,
12199 		       "comparison of distinct pointer types lacks a cast");
12200 
12201 	  if (result_type == NULL_TREE)
12202 	    {
12203 	      int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12204 	      result_type = build_pointer_type
12205 			      (build_qualified_type (void_type_node, qual));
12206 	    }
12207 	}
12208       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12209 	{
12210 	  result_type = type0;
12211 	  pedwarn (location, 0, "comparison between pointer and integer");
12212 	}
12213       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12214 	{
12215 	  result_type = type1;
12216 	  pedwarn (location, 0, "comparison between pointer and integer");
12217 	}
12218       if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12219 	   || truth_value_p (TREE_CODE (orig_op0)))
12220 	  ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12221 	     || truth_value_p (TREE_CODE (orig_op1))))
12222 	maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12223       break;
12224 
12225     case LE_EXPR:
12226     case GE_EXPR:
12227     case LT_EXPR:
12228     case GT_EXPR:
12229       if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
12230         {
12231           tree intt;
12232 	  if (!vector_types_compatible_elements_p (type0, type1))
12233             {
12234               error_at (location, "comparing vectors with different "
12235                                   "element types");
12236               return error_mark_node;
12237             }
12238 
12239 	  if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
12240 			TYPE_VECTOR_SUBPARTS (type1)))
12241             {
12242               error_at (location, "comparing vectors with different "
12243                                   "number of elements");
12244               return error_mark_node;
12245             }
12246 
12247 	  /* It's not precisely specified how the usual arithmetic
12248 	     conversions apply to the vector types.  Here, we use
12249 	     the unsigned type if one of the operands is signed and
12250 	     the other one is unsigned.  */
12251 	  if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
12252 	    {
12253 	      if (!TYPE_UNSIGNED (type0))
12254 		op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
12255 	      else
12256 		op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
12257 	      warning_at (location, OPT_Wsign_compare, "comparison between "
12258 			  "types %qT and %qT", type0, type1);
12259 	    }
12260 
12261           /* Always construct signed integer vector type.  */
12262           intt = c_common_type_for_size (GET_MODE_BITSIZE
12263 					 (SCALAR_TYPE_MODE
12264 					  (TREE_TYPE (type0))), 0);
12265 	  if (!intt)
12266 	    {
12267 	      error_at (location, "could not find an integer type "
12268 				  "of the same size as %qT",
12269 			TREE_TYPE (type0));
12270 	      return error_mark_node;
12271 	    }
12272           result_type = build_opaque_vector_type (intt,
12273 						  TYPE_VECTOR_SUBPARTS (type0));
12274           converted = 1;
12275 	  ret = build_vec_cmp (resultcode, result_type, op0, op1);
12276 	  goto return_build_binary_op;
12277         }
12278       build_type = integer_type_node;
12279       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
12280 	   || code0 == FIXED_POINT_TYPE)
12281 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
12282 	      || code1 == FIXED_POINT_TYPE))
12283 	short_compare = 1;
12284       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
12285 	{
12286 	  addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
12287 	  addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
12288 	  addr_space_t as_common;
12289 
12290 	  if (comp_target_types (location, type0, type1))
12291 	    {
12292 	      result_type = common_pointer_type (type0, type1);
12293 	      if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
12294 		  != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
12295 		pedwarn (location, 0,
12296 			 "comparison of complete and incomplete pointers");
12297 	      else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
12298 		pedwarn (location, OPT_Wpedantic, "ISO C forbids "
12299 			 "ordered comparisons of pointers to functions");
12300 	      else if (null_pointer_constant_p (orig_op0)
12301 		       || null_pointer_constant_p (orig_op1))
12302 		warning_at (location, OPT_Wextra,
12303 			    "ordered comparison of pointer with null pointer");
12304 
12305 	    }
12306 	  else if (!addr_space_superset (as0, as1, &as_common))
12307 	    {
12308 	      error_at (location, "comparison of pointers to "
12309 			"disjoint address spaces");
12310 	      return error_mark_node;
12311 	    }
12312 	  else
12313 	    {
12314 	      int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
12315 	      result_type = build_pointer_type
12316 			      (build_qualified_type (void_type_node, qual));
12317 	      pedwarn (location, 0,
12318 		       "comparison of distinct pointer types lacks a cast");
12319 	    }
12320 	}
12321       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
12322 	{
12323 	  result_type = type0;
12324 	  if (pedantic)
12325 	    pedwarn (location, OPT_Wpedantic,
12326 		     "ordered comparison of pointer with integer zero");
12327 	  else if (extra_warnings)
12328 	    warning_at (location, OPT_Wextra,
12329 			"ordered comparison of pointer with integer zero");
12330 	}
12331       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
12332 	{
12333 	  result_type = type1;
12334 	  if (pedantic)
12335 	    pedwarn (location, OPT_Wpedantic,
12336 		     "ordered comparison of pointer with integer zero");
12337 	  else if (extra_warnings)
12338 	    warning_at (location, OPT_Wextra,
12339 			"ordered comparison of pointer with integer zero");
12340 	}
12341       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
12342 	{
12343 	  result_type = type0;
12344 	  pedwarn (location, 0, "comparison between pointer and integer");
12345 	}
12346       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
12347 	{
12348 	  result_type = type1;
12349 	  pedwarn (location, 0, "comparison between pointer and integer");
12350 	}
12351 
12352       if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
12353 	  && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
12354 	{
12355 	  op0 = save_expr (op0);
12356 	  op1 = save_expr (op1);
12357 
12358 	  tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
12359 	  instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
12360 	}
12361 
12362       if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
12363 	   || truth_value_p (TREE_CODE (orig_op0)))
12364 	  ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
12365 	     || truth_value_p (TREE_CODE (orig_op1))))
12366 	maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
12367       break;
12368 
12369     default:
12370       gcc_unreachable ();
12371     }
12372 
12373   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
12374     return error_mark_node;
12375 
12376   if (gnu_vector_type_p (type0)
12377       && gnu_vector_type_p (type1)
12378       && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
12379 	  || !vector_types_compatible_elements_p (type0, type1)))
12380     {
12381       gcc_rich_location richloc (location);
12382       maybe_range_label_for_tree_type_mismatch
12383 	label_for_op0 (orig_op0, orig_op1),
12384 	label_for_op1 (orig_op1, orig_op0);
12385       richloc.maybe_add_expr (orig_op0, &label_for_op0);
12386       richloc.maybe_add_expr (orig_op1, &label_for_op1);
12387       binary_op_error (&richloc, code, type0, type1);
12388       return error_mark_node;
12389     }
12390 
12391   if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
12392        || code0 == FIXED_POINT_TYPE
12393        || gnu_vector_type_p (type0))
12394       &&
12395       (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
12396        || code1 == FIXED_POINT_TYPE
12397        || gnu_vector_type_p (type1)))
12398     {
12399       bool first_complex = (code0 == COMPLEX_TYPE);
12400       bool second_complex = (code1 == COMPLEX_TYPE);
12401       int none_complex = (!first_complex && !second_complex);
12402 
12403       if (shorten || common || short_compare)
12404 	{
12405 	  result_type = c_common_type (type0, type1);
12406 	  do_warn_double_promotion (result_type, type0, type1,
12407 				    "implicit conversion from %qT to %qT "
12408 				    "to match other operand of binary "
12409 				    "expression",
12410 				    location);
12411 	  if (result_type == error_mark_node)
12412 	    return error_mark_node;
12413 	}
12414 
12415       if (first_complex != second_complex
12416 	  && (code == PLUS_EXPR
12417 	      || code == MINUS_EXPR
12418 	      || code == MULT_EXPR
12419 	      || (code == TRUNC_DIV_EXPR && first_complex))
12420 	  && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
12421 	  && flag_signed_zeros)
12422 	{
12423 	  /* An operation on mixed real/complex operands must be
12424 	     handled specially, but the language-independent code can
12425 	     more easily optimize the plain complex arithmetic if
12426 	     -fno-signed-zeros.  */
12427 	  tree real_type = TREE_TYPE (result_type);
12428 	  tree real, imag;
12429 	  if (type0 != orig_type0 || type1 != orig_type1)
12430 	    {
12431 	      gcc_assert (may_need_excess_precision && common);
12432 	      semantic_result_type = c_common_type (orig_type0, orig_type1);
12433 	    }
12434 	  if (first_complex)
12435 	    {
12436 	      if (TREE_TYPE (op0) != result_type)
12437 		op0 = convert_and_check (location, result_type, op0);
12438 	      if (TREE_TYPE (op1) != real_type)
12439 		op1 = convert_and_check (location, real_type, op1);
12440 	    }
12441 	  else
12442 	    {
12443 	      if (TREE_TYPE (op0) != real_type)
12444 		op0 = convert_and_check (location, real_type, op0);
12445 	      if (TREE_TYPE (op1) != result_type)
12446 		op1 = convert_and_check (location, result_type, op1);
12447 	    }
12448 	  if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12449 	    return error_mark_node;
12450 	  if (first_complex)
12451 	    {
12452 	      op0 = save_expr (op0);
12453 	      real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
12454 				     op0, true);
12455 	      imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
12456 				     op0, true);
12457 	      switch (code)
12458 		{
12459 		case MULT_EXPR:
12460 		case TRUNC_DIV_EXPR:
12461 		  op1 = save_expr (op1);
12462 		  imag = build2 (resultcode, real_type, imag, op1);
12463 		  /* Fall through.  */
12464 		case PLUS_EXPR:
12465 		case MINUS_EXPR:
12466 		  real = build2 (resultcode, real_type, real, op1);
12467 		  break;
12468 		default:
12469 		  gcc_unreachable();
12470 		}
12471 	    }
12472 	  else
12473 	    {
12474 	      op1 = save_expr (op1);
12475 	      real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
12476 				     op1, true);
12477 	      imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
12478 				     op1, true);
12479 	      switch (code)
12480 		{
12481 		case MULT_EXPR:
12482 		  op0 = save_expr (op0);
12483 		  imag = build2 (resultcode, real_type, op0, imag);
12484 		  /* Fall through.  */
12485 		case PLUS_EXPR:
12486 		  real = build2 (resultcode, real_type, op0, real);
12487 		  break;
12488 		case MINUS_EXPR:
12489 		  real = build2 (resultcode, real_type, op0, real);
12490 		  imag = build1 (NEGATE_EXPR, real_type, imag);
12491 		  break;
12492 		default:
12493 		  gcc_unreachable();
12494 		}
12495 	    }
12496 	  ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12497 	  goto return_build_binary_op;
12498 	}
12499 
12500       /* For certain operations (which identify themselves by shorten != 0)
12501 	 if both args were extended from the same smaller type,
12502 	 do the arithmetic in that type and then extend.
12503 
12504 	 shorten !=0 and !=1 indicates a bitwise operation.
12505 	 For them, this optimization is safe only if
12506 	 both args are zero-extended or both are sign-extended.
12507 	 Otherwise, we might change the result.
12508 	 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12509 	 but calculated in (unsigned short) it would be (unsigned short)-1.  */
12510 
12511       if (shorten && none_complex)
12512 	{
12513 	  final_type = result_type;
12514 	  result_type = shorten_binary_op (result_type, op0, op1,
12515 					   shorten == -1);
12516 	}
12517 
12518       /* Shifts can be shortened if shifting right.  */
12519 
12520       if (short_shift)
12521 	{
12522 	  int unsigned_arg;
12523 	  tree arg0 = get_narrower (op0, &unsigned_arg);
12524 
12525 	  final_type = result_type;
12526 
12527 	  if (arg0 == op0 && final_type == TREE_TYPE (op0))
12528 	    unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
12529 
12530 	  if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
12531 	      && tree_int_cst_sgn (op1) > 0
12532 	      /* We can shorten only if the shift count is less than the
12533 		 number of bits in the smaller type size.  */
12534 	      && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12535 	      /* We cannot drop an unsigned shift after sign-extension.  */
12536 	      && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
12537 	    {
12538 	      /* Do an unsigned shift if the operand was zero-extended.  */
12539 	      result_type
12540 		= c_common_signed_or_unsigned_type (unsigned_arg,
12541 						    TREE_TYPE (arg0));
12542 	      /* Convert value-to-be-shifted to that type.  */
12543 	      if (TREE_TYPE (op0) != result_type)
12544 		op0 = convert (result_type, op0);
12545 	      converted = 1;
12546 	    }
12547 	}
12548 
12549       /* Comparison operations are shortened too but differently.
12550 	 They identify themselves by setting short_compare = 1.  */
12551 
12552       if (short_compare)
12553 	{
12554 	  /* Don't write &op0, etc., because that would prevent op0
12555 	     from being kept in a register.
12556 	     Instead, make copies of the our local variables and
12557 	     pass the copies by reference, then copy them back afterward.  */
12558 	  tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12559 	  enum tree_code xresultcode = resultcode;
12560 	  tree val
12561 	    = shorten_compare (location, &xop0, &xop1, &xresult_type,
12562 			       &xresultcode);
12563 
12564 	  if (val != NULL_TREE)
12565 	    {
12566 	      ret = val;
12567 	      goto return_build_binary_op;
12568 	    }
12569 
12570 	  op0 = xop0, op1 = xop1;
12571 	  converted = 1;
12572 	  resultcode = xresultcode;
12573 
12574 	  if (c_inhibit_evaluation_warnings == 0)
12575 	    {
12576 	      bool op0_maybe_const = true;
12577 	      bool op1_maybe_const = true;
12578 	      tree orig_op0_folded, orig_op1_folded;
12579 
12580 	      if (in_late_binary_op)
12581 		{
12582 		  orig_op0_folded = orig_op0;
12583 		  orig_op1_folded = orig_op1;
12584 		}
12585 	      else
12586 		{
12587 		  /* Fold for the sake of possible warnings, as in
12588 		     build_conditional_expr.  This requires the
12589 		     "original" values to be folded, not just op0 and
12590 		     op1.  */
12591 		  c_inhibit_evaluation_warnings++;
12592 		  op0 = c_fully_fold (op0, require_constant_value,
12593 				      &op0_maybe_const);
12594 		  op1 = c_fully_fold (op1, require_constant_value,
12595 				      &op1_maybe_const);
12596 		  c_inhibit_evaluation_warnings--;
12597 		  orig_op0_folded = c_fully_fold (orig_op0,
12598 						  require_constant_value,
12599 						  NULL);
12600 		  orig_op1_folded = c_fully_fold (orig_op1,
12601 						  require_constant_value,
12602 						  NULL);
12603 		}
12604 
12605 	      if (warn_sign_compare)
12606 		warn_for_sign_compare (location, orig_op0_folded,
12607 				       orig_op1_folded, op0, op1,
12608 				       result_type, resultcode);
12609 	      if (!in_late_binary_op && !int_operands)
12610 		{
12611 		  if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
12612 		    op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
12613 		  if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
12614 		    op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
12615 		}
12616 	    }
12617 	}
12618     }
12619 
12620   /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12621      If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12622      Then the expression will be built.
12623      It will be given type FINAL_TYPE if that is nonzero;
12624      otherwise, it will be given type RESULT_TYPE.  */
12625 
12626   if (!result_type)
12627     {
12628       /* Favor showing any expression locations that are available. */
12629       op_location_t oploc (location, UNKNOWN_LOCATION);
12630       binary_op_rich_location richloc (oploc, orig_op0, orig_op1, true);
12631       binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
12632       return error_mark_node;
12633     }
12634 
12635   if (build_type == NULL_TREE)
12636     {
12637       build_type = result_type;
12638       if ((type0 != orig_type0 || type1 != orig_type1)
12639 	  && !boolean_op)
12640 	{
12641 	  gcc_assert (may_need_excess_precision && common);
12642 	  semantic_result_type = c_common_type (orig_type0, orig_type1);
12643 	}
12644     }
12645 
12646   if (!converted)
12647     {
12648       op0 = ep_convert_and_check (location, result_type, op0,
12649 				  semantic_result_type);
12650       op1 = ep_convert_and_check (location, result_type, op1,
12651 				  semantic_result_type);
12652 
12653       /* This can happen if one operand has a vector type, and the other
12654 	 has a different type.  */
12655       if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12656 	return error_mark_node;
12657     }
12658 
12659   if (sanitize_flags_p ((SANITIZE_SHIFT
12660 			 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
12661       && current_function_decl != NULL_TREE
12662       && (doing_div_or_mod || doing_shift)
12663       && !require_constant_value)
12664     {
12665       /* OP0 and/or OP1 might have side-effects.  */
12666       op0 = save_expr (op0);
12667       op1 = save_expr (op1);
12668       op0 = c_fully_fold (op0, false, NULL);
12669       op1 = c_fully_fold (op1, false, NULL);
12670       if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12671 						  | SANITIZE_FLOAT_DIVIDE))))
12672 	instrument_expr = ubsan_instrument_division (location, op0, op1);
12673       else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
12674 	instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12675     }
12676 
12677   /* Treat expressions in initializers specially as they can't trap.  */
12678   if (int_const_or_overflow)
12679     ret = (require_constant_value
12680 	   ? fold_build2_initializer_loc (location, resultcode, build_type,
12681 					  op0, op1)
12682 	   : fold_build2_loc (location, resultcode, build_type, op0, op1));
12683   else
12684     ret = build2 (resultcode, build_type, op0, op1);
12685   if (final_type != NULL_TREE)
12686     ret = convert (final_type, ret);
12687 
12688  return_build_binary_op:
12689   gcc_assert (ret != error_mark_node);
12690   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12691     ret = (int_operands
12692 	   ? note_integer_operands (ret)
12693 	   : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12694   else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12695 	   && !in_late_binary_op)
12696     ret = note_integer_operands (ret);
12697   protected_set_expr_location (ret, location);
12698 
12699   if (instrument_expr != NULL)
12700     ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12701 		       instrument_expr, ret);
12702 
12703   if (semantic_result_type)
12704     ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12705 		      semantic_result_type, ret);
12706 
12707   return ret;
12708 }
12709 
12710 
12711 /* Convert EXPR to be a truth-value, validating its type for this
12712    purpose.  LOCATION is the source location for the expression.  */
12713 
12714 tree
c_objc_common_truthvalue_conversion(location_t location,tree expr)12715 c_objc_common_truthvalue_conversion (location_t location, tree expr)
12716 {
12717   bool int_const, int_operands;
12718 
12719   switch (TREE_CODE (TREE_TYPE (expr)))
12720     {
12721     case ARRAY_TYPE:
12722       error_at (location, "used array that cannot be converted to pointer where scalar is required");
12723       return error_mark_node;
12724 
12725     case RECORD_TYPE:
12726       error_at (location, "used struct type value where scalar is required");
12727       return error_mark_node;
12728 
12729     case UNION_TYPE:
12730       error_at (location, "used union type value where scalar is required");
12731       return error_mark_node;
12732 
12733     case VOID_TYPE:
12734       error_at (location, "void value not ignored as it ought to be");
12735       return error_mark_node;
12736 
12737     case POINTER_TYPE:
12738       if (reject_gcc_builtin (expr))
12739 	return error_mark_node;
12740       break;
12741 
12742     case FUNCTION_TYPE:
12743       gcc_unreachable ();
12744 
12745     case VECTOR_TYPE:
12746       error_at (location, "used vector type where scalar is required");
12747       return error_mark_node;
12748 
12749     default:
12750       break;
12751     }
12752 
12753   int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12754   int_operands = EXPR_INT_CONST_OPERANDS (expr);
12755   if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12756     {
12757       expr = remove_c_maybe_const_expr (expr);
12758       expr = build2 (NE_EXPR, integer_type_node, expr,
12759 		     convert (TREE_TYPE (expr), integer_zero_node));
12760       expr = note_integer_operands (expr);
12761     }
12762   else
12763     /* ??? Should we also give an error for vectors rather than leaving
12764        those to give errors later?  */
12765     expr = c_common_truthvalue_conversion (location, expr);
12766 
12767   if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12768     {
12769       if (TREE_OVERFLOW (expr))
12770 	return expr;
12771       else
12772 	return note_integer_operands (expr);
12773     }
12774   if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12775     return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12776   return expr;
12777 }
12778 
12779 
12780 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12781    required.  */
12782 
12783 tree
c_expr_to_decl(tree expr,bool * tc ATTRIBUTE_UNUSED,bool * se)12784 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
12785 {
12786   if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12787     {
12788       tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12789       /* Executing a compound literal inside a function reinitializes
12790 	 it.  */
12791       if (!TREE_STATIC (decl))
12792 	*se = true;
12793       return decl;
12794     }
12795   else
12796     return expr;
12797 }
12798 
12799 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12800    statement.  LOC is the location of the construct.  */
12801 
12802 tree
c_finish_omp_construct(location_t loc,enum tree_code code,tree body,tree clauses)12803 c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12804 			tree clauses)
12805 {
12806   body = c_end_compound_stmt (loc, body, true);
12807 
12808   tree stmt = make_node (code);
12809   TREE_TYPE (stmt) = void_type_node;
12810   OMP_BODY (stmt) = body;
12811   OMP_CLAUSES (stmt) = clauses;
12812   SET_EXPR_LOCATION (stmt, loc);
12813 
12814   return add_stmt (stmt);
12815 }
12816 
12817 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12818    statement.  LOC is the location of the OACC_DATA.  */
12819 
12820 tree
c_finish_oacc_data(location_t loc,tree clauses,tree block)12821 c_finish_oacc_data (location_t loc, tree clauses, tree block)
12822 {
12823   tree stmt;
12824 
12825   block = c_end_compound_stmt (loc, block, true);
12826 
12827   stmt = make_node (OACC_DATA);
12828   TREE_TYPE (stmt) = void_type_node;
12829   OACC_DATA_CLAUSES (stmt) = clauses;
12830   OACC_DATA_BODY (stmt) = block;
12831   SET_EXPR_LOCATION (stmt, loc);
12832 
12833   return add_stmt (stmt);
12834 }
12835 
12836 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12837    statement.  LOC is the location of the OACC_HOST_DATA.  */
12838 
12839 tree
c_finish_oacc_host_data(location_t loc,tree clauses,tree block)12840 c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12841 {
12842   tree stmt;
12843 
12844   block = c_end_compound_stmt (loc, block, true);
12845 
12846   stmt = make_node (OACC_HOST_DATA);
12847   TREE_TYPE (stmt) = void_type_node;
12848   OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12849   OACC_HOST_DATA_BODY (stmt) = block;
12850   SET_EXPR_LOCATION (stmt, loc);
12851 
12852   return add_stmt (stmt);
12853 }
12854 
12855 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
12856 
12857 tree
c_begin_omp_parallel(void)12858 c_begin_omp_parallel (void)
12859 {
12860   tree block;
12861 
12862   keep_next_level ();
12863   block = c_begin_compound_stmt (true);
12864 
12865   return block;
12866 }
12867 
12868 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12869    statement.  LOC is the location of the OMP_PARALLEL.  */
12870 
12871 tree
c_finish_omp_parallel(location_t loc,tree clauses,tree block)12872 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
12873 {
12874   tree stmt;
12875 
12876   block = c_end_compound_stmt (loc, block, true);
12877 
12878   stmt = make_node (OMP_PARALLEL);
12879   TREE_TYPE (stmt) = void_type_node;
12880   OMP_PARALLEL_CLAUSES (stmt) = clauses;
12881   OMP_PARALLEL_BODY (stmt) = block;
12882   SET_EXPR_LOCATION (stmt, loc);
12883 
12884   return add_stmt (stmt);
12885 }
12886 
12887 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
12888 
12889 tree
c_begin_omp_task(void)12890 c_begin_omp_task (void)
12891 {
12892   tree block;
12893 
12894   keep_next_level ();
12895   block = c_begin_compound_stmt (true);
12896 
12897   return block;
12898 }
12899 
12900 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12901    statement.  LOC is the location of the #pragma.  */
12902 
12903 tree
c_finish_omp_task(location_t loc,tree clauses,tree block)12904 c_finish_omp_task (location_t loc, tree clauses, tree block)
12905 {
12906   tree stmt;
12907 
12908   block = c_end_compound_stmt (loc, block, true);
12909 
12910   stmt = make_node (OMP_TASK);
12911   TREE_TYPE (stmt) = void_type_node;
12912   OMP_TASK_CLAUSES (stmt) = clauses;
12913   OMP_TASK_BODY (stmt) = block;
12914   SET_EXPR_LOCATION (stmt, loc);
12915 
12916   return add_stmt (stmt);
12917 }
12918 
12919 /* Generate GOMP_cancel call for #pragma omp cancel.  */
12920 
12921 void
c_finish_omp_cancel(location_t loc,tree clauses)12922 c_finish_omp_cancel (location_t loc, tree clauses)
12923 {
12924   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12925   int mask = 0;
12926   if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12927     mask = 1;
12928   else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12929     mask = 2;
12930   else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12931     mask = 4;
12932   else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12933     mask = 8;
12934   else
12935     {
12936       error_at (loc, "%<#pragma omp cancel%> must specify one of "
12937 		     "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12938 		     "clauses");
12939       return;
12940     }
12941   tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
12942   if (ifc != NULL_TREE)
12943     {
12944       if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
12945 	  && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
12946 	error_at (OMP_CLAUSE_LOCATION (ifc),
12947 		  "expected %<cancel%> %<if%> clause modifier");
12948       else
12949 	{
12950 	  tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
12951 	  if (ifc2 != NULL_TREE)
12952 	    {
12953 	      gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
12954 			  && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
12955 			  && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
12956 	      error_at (OMP_CLAUSE_LOCATION (ifc2),
12957 			"expected %<cancel%> %<if%> clause modifier");
12958 	    }
12959 	}
12960 
12961       tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12962       ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12963 			     boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12964 			     build_zero_cst (type));
12965     }
12966   else
12967     ifc = boolean_true_node;
12968   tree stmt = build_call_expr_loc (loc, fn, 2,
12969 				   build_int_cst (integer_type_node, mask),
12970 				   ifc);
12971   add_stmt (stmt);
12972 }
12973 
12974 /* Generate GOMP_cancellation_point call for
12975    #pragma omp cancellation point.  */
12976 
12977 void
c_finish_omp_cancellation_point(location_t loc,tree clauses)12978 c_finish_omp_cancellation_point (location_t loc, tree clauses)
12979 {
12980   tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12981   int mask = 0;
12982   if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
12983     mask = 1;
12984   else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
12985     mask = 2;
12986   else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
12987     mask = 4;
12988   else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
12989     mask = 8;
12990   else
12991     {
12992       error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
12993 		     "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12994 		     "clauses");
12995       return;
12996     }
12997   tree stmt = build_call_expr_loc (loc, fn, 1,
12998 				   build_int_cst (integer_type_node, mask));
12999   add_stmt (stmt);
13000 }
13001 
13002 /* Helper function for handle_omp_array_sections.  Called recursively
13003    to handle multiple array-section-subscripts.  C is the clause,
13004    T current expression (initially OMP_CLAUSE_DECL), which is either
13005    a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
13006    expression if specified, TREE_VALUE length expression if specified,
13007    TREE_CHAIN is what it has been specified after, or some decl.
13008    TYPES vector is populated with array section types, MAYBE_ZERO_LEN
13009    set to true if any of the array-section-subscript could have length
13010    of zero (explicit or implicit), FIRST_NON_ONE is the index of the
13011    first array-section-subscript which is known not to have length
13012    of one.  Given say:
13013    map(a[:b][2:1][:c][:2][:d][e:f][2:5])
13014    FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
13015    all are or may have length of 1, array-section-subscript [:2] is the
13016    first one known not to have length 1.  For array-section-subscript
13017    <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
13018    0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
13019    can if MAYBE_ZERO_LEN is false.  MAYBE_ZERO_LEN will be true in the above
13020    case though, as some lengths could be zero.  */
13021 
13022 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)13023 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
13024 			     bool &maybe_zero_len, unsigned int &first_non_one,
13025 			     enum c_omp_region_type ort)
13026 {
13027   tree ret, low_bound, length, type;
13028   if (TREE_CODE (t) != TREE_LIST)
13029     {
13030       if (error_operand_p (t))
13031 	return error_mark_node;
13032       ret = t;
13033       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13034 	  && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13035 	{
13036 	  error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
13037 		    t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13038 	  return error_mark_node;
13039 	}
13040       if (TREE_CODE (t) == COMPONENT_REF
13041 	  && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13042 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
13043 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
13044 	{
13045 	  if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13046 	    {
13047 	      error_at (OMP_CLAUSE_LOCATION (c),
13048 			"bit-field %qE in %qs clause",
13049 			t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13050 	      return error_mark_node;
13051 	    }
13052 	  while (TREE_CODE (t) == COMPONENT_REF)
13053 	    {
13054 	      if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
13055 		{
13056 		  error_at (OMP_CLAUSE_LOCATION (c),
13057 			    "%qE is a member of a union", t);
13058 		  return error_mark_node;
13059 		}
13060 	      t = TREE_OPERAND (t, 0);
13061 	      if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
13062 		{
13063 		  if (maybe_ne (mem_ref_offset (t), 0))
13064 		    error_at (OMP_CLAUSE_LOCATION (c),
13065 			      "cannot dereference %qE in %qs clause", t,
13066 			      omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13067 		  else
13068 		    t = TREE_OPERAND (t, 0);
13069 		}
13070 	    }
13071 	}
13072       if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
13073 	{
13074 	  if (DECL_P (t))
13075 	    error_at (OMP_CLAUSE_LOCATION (c),
13076 		      "%qD is not a variable in %qs clause", t,
13077 		      omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13078 	  else
13079 	    error_at (OMP_CLAUSE_LOCATION (c),
13080 		      "%qE is not a variable in %qs clause", t,
13081 		      omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13082 	  return error_mark_node;
13083 	}
13084       else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13085 	       && TYPE_ATOMIC (TREE_TYPE (t)))
13086 	{
13087 	  error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
13088 		    t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13089 	  return error_mark_node;
13090 	}
13091       else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13092 	       && VAR_P (t)
13093 	       && DECL_THREAD_LOCAL_P (t))
13094 	{
13095 	  error_at (OMP_CLAUSE_LOCATION (c),
13096 		    "%qD is threadprivate variable in %qs clause", t,
13097 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13098 	  return error_mark_node;
13099 	}
13100       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13101 	  && TYPE_ATOMIC (TREE_TYPE (t))
13102 	  && POINTER_TYPE_P (TREE_TYPE (t)))
13103 	{
13104 	  /* If the array section is pointer based and the pointer
13105 	     itself is _Atomic qualified, we need to atomically load
13106 	     the pointer.  */
13107 	  c_expr expr;
13108 	  memset (&expr, 0, sizeof (expr));
13109 	  expr.value = ret;
13110 	  expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
13111 					   expr, false, false);
13112 	  ret = expr.value;
13113 	}
13114       return ret;
13115     }
13116 
13117   ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
13118 				     maybe_zero_len, first_non_one, ort);
13119   if (ret == error_mark_node || ret == NULL_TREE)
13120     return ret;
13121 
13122   type = TREE_TYPE (ret);
13123   low_bound = TREE_PURPOSE (t);
13124   length = TREE_VALUE (t);
13125 
13126   if (low_bound == error_mark_node || length == error_mark_node)
13127     return error_mark_node;
13128 
13129   if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
13130     {
13131       error_at (OMP_CLAUSE_LOCATION (c),
13132 		"low bound %qE of array section does not have integral type",
13133 		low_bound);
13134       return error_mark_node;
13135     }
13136   if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
13137     {
13138       error_at (OMP_CLAUSE_LOCATION (c),
13139 		"length %qE of array section does not have integral type",
13140 		length);
13141       return error_mark_node;
13142     }
13143   if (low_bound
13144       && TREE_CODE (low_bound) == INTEGER_CST
13145       && TYPE_PRECISION (TREE_TYPE (low_bound))
13146 	 > TYPE_PRECISION (sizetype))
13147     low_bound = fold_convert (sizetype, low_bound);
13148   if (length
13149       && TREE_CODE (length) == INTEGER_CST
13150       && TYPE_PRECISION (TREE_TYPE (length))
13151 	 > TYPE_PRECISION (sizetype))
13152     length = fold_convert (sizetype, length);
13153   if (low_bound == NULL_TREE)
13154     low_bound = integer_zero_node;
13155   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13156       && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13157 	  || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
13158     {
13159       if (length != integer_one_node)
13160 	{
13161 	  error_at (OMP_CLAUSE_LOCATION (c),
13162 		    "expected single pointer in %qs clause",
13163 		    c_omp_map_clause_name (c, ort == C_ORT_ACC));
13164 	  return error_mark_node;
13165 	}
13166     }
13167   if (length != NULL_TREE)
13168     {
13169       if (!integer_nonzerop (length))
13170 	{
13171 	  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13172 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13173 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13174 	      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13175 	    {
13176 	      if (integer_zerop (length))
13177 		{
13178 		  error_at (OMP_CLAUSE_LOCATION (c),
13179 			    "zero length array section in %qs clause",
13180 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13181 		  return error_mark_node;
13182 		}
13183 	    }
13184 	  else
13185 	    maybe_zero_len = true;
13186 	}
13187       if (first_non_one == types.length ()
13188 	  && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
13189 	first_non_one++;
13190     }
13191   if (TREE_CODE (type) == ARRAY_TYPE)
13192     {
13193       if (length == NULL_TREE
13194 	  && (TYPE_DOMAIN (type) == NULL_TREE
13195 	      || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
13196 	{
13197 	  error_at (OMP_CLAUSE_LOCATION (c),
13198 		    "for unknown bound array type length expression must "
13199 		    "be specified");
13200 	  return error_mark_node;
13201 	}
13202       if (TREE_CODE (low_bound) == INTEGER_CST
13203 	  && tree_int_cst_sgn (low_bound) == -1)
13204 	{
13205 	  error_at (OMP_CLAUSE_LOCATION (c),
13206 		    "negative low bound in array section in %qs clause",
13207 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13208 	  return error_mark_node;
13209 	}
13210       if (length != NULL_TREE
13211 	  && TREE_CODE (length) == INTEGER_CST
13212 	  && tree_int_cst_sgn (length) == -1)
13213 	{
13214 	  error_at (OMP_CLAUSE_LOCATION (c),
13215 		    "negative length in array section in %qs clause",
13216 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13217 	  return error_mark_node;
13218 	}
13219       if (TYPE_DOMAIN (type)
13220 	  && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
13221 	  && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
13222 			== INTEGER_CST)
13223 	{
13224 	  tree size
13225 	    = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
13226 	  size = size_binop (PLUS_EXPR, size, size_one_node);
13227 	  if (TREE_CODE (low_bound) == INTEGER_CST)
13228 	    {
13229 	      if (tree_int_cst_lt (size, low_bound))
13230 		{
13231 		  error_at (OMP_CLAUSE_LOCATION (c),
13232 			    "low bound %qE above array section size "
13233 			    "in %qs clause", low_bound,
13234 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13235 		  return error_mark_node;
13236 		}
13237 	      if (tree_int_cst_equal (size, low_bound))
13238 		{
13239 		  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13240 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13241 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13242 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13243 		    {
13244 		      error_at (OMP_CLAUSE_LOCATION (c),
13245 				"zero length array section in %qs clause",
13246 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13247 		      return error_mark_node;
13248 		    }
13249 		  maybe_zero_len = true;
13250 		}
13251 	      else if (length == NULL_TREE
13252 		       && first_non_one == types.length ()
13253 		       && tree_int_cst_equal
13254 			    (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
13255 			     low_bound))
13256 		first_non_one++;
13257 	    }
13258 	  else if (length == NULL_TREE)
13259 	    {
13260 	      if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13261 		  && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13262 		  && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13263 		  && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13264 		maybe_zero_len = true;
13265 	      if (first_non_one == types.length ())
13266 		first_non_one++;
13267 	    }
13268 	  if (length && TREE_CODE (length) == INTEGER_CST)
13269 	    {
13270 	      if (tree_int_cst_lt (size, length))
13271 		{
13272 		  error_at (OMP_CLAUSE_LOCATION (c),
13273 			    "length %qE above array section size "
13274 			    "in %qs clause", length,
13275 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13276 		  return error_mark_node;
13277 		}
13278 	      if (TREE_CODE (low_bound) == INTEGER_CST)
13279 		{
13280 		  tree lbpluslen
13281 		    = size_binop (PLUS_EXPR,
13282 				  fold_convert (sizetype, low_bound),
13283 				  fold_convert (sizetype, length));
13284 		  if (TREE_CODE (lbpluslen) == INTEGER_CST
13285 		      && tree_int_cst_lt (size, lbpluslen))
13286 		    {
13287 		      error_at (OMP_CLAUSE_LOCATION (c),
13288 				"high bound %qE above array section size "
13289 				"in %qs clause", lbpluslen,
13290 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13291 		      return error_mark_node;
13292 		    }
13293 		}
13294 	    }
13295 	}
13296       else if (length == NULL_TREE)
13297 	{
13298 	  if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13299 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
13300 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
13301 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
13302 	    maybe_zero_len = true;
13303 	  if (first_non_one == types.length ())
13304 	    first_non_one++;
13305 	}
13306 
13307       /* For [lb:] we will need to evaluate lb more than once.  */
13308       if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13309 	{
13310 	  tree lb = save_expr (low_bound);
13311 	  if (lb != low_bound)
13312 	    {
13313 	      TREE_PURPOSE (t) = lb;
13314 	      low_bound = lb;
13315 	    }
13316 	}
13317     }
13318   else if (TREE_CODE (type) == POINTER_TYPE)
13319     {
13320       if (length == NULL_TREE)
13321 	{
13322 	  error_at (OMP_CLAUSE_LOCATION (c),
13323 		    "for pointer type length expression must be specified");
13324 	  return error_mark_node;
13325 	}
13326       if (length != NULL_TREE
13327 	  && TREE_CODE (length) == INTEGER_CST
13328 	  && tree_int_cst_sgn (length) == -1)
13329 	{
13330 	  error_at (OMP_CLAUSE_LOCATION (c),
13331 		    "negative length in array section in %qs clause",
13332 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13333 	  return error_mark_node;
13334 	}
13335       /* If there is a pointer type anywhere but in the very first
13336 	 array-section-subscript, the array section can't be contiguous.  */
13337       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
13338 	  && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
13339 	{
13340 	  error_at (OMP_CLAUSE_LOCATION (c),
13341 		    "array section is not contiguous in %qs clause",
13342 		    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13343 	  return error_mark_node;
13344 	}
13345     }
13346   else
13347     {
13348       error_at (OMP_CLAUSE_LOCATION (c),
13349 		"%qE does not have pointer or array type", ret);
13350       return error_mark_node;
13351     }
13352   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
13353     types.safe_push (TREE_TYPE (ret));
13354   /* We will need to evaluate lb more than once.  */
13355   tree lb = save_expr (low_bound);
13356   if (lb != low_bound)
13357     {
13358       TREE_PURPOSE (t) = lb;
13359       low_bound = lb;
13360     }
13361   ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
13362   return ret;
13363 }
13364 
13365 /* Handle array sections for clause C.  */
13366 
13367 static bool
handle_omp_array_sections(tree c,enum c_omp_region_type ort)13368 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
13369 {
13370   bool maybe_zero_len = false;
13371   unsigned int first_non_one = 0;
13372   auto_vec<tree, 10> types;
13373   tree *tp = &OMP_CLAUSE_DECL (c);
13374   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
13375       && TREE_CODE (*tp) == TREE_LIST
13376       && TREE_PURPOSE (*tp)
13377       && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
13378     tp = &TREE_VALUE (*tp);
13379   tree first = handle_omp_array_sections_1 (c, *tp, types,
13380 					    maybe_zero_len, first_non_one,
13381 					    ort);
13382   if (first == error_mark_node)
13383     return true;
13384   if (first == NULL_TREE)
13385     return false;
13386   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
13387     {
13388       tree t = *tp;
13389       tree tem = NULL_TREE;
13390       /* Need to evaluate side effects in the length expressions
13391 	 if any.  */
13392       while (TREE_CODE (t) == TREE_LIST)
13393 	{
13394 	  if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
13395 	    {
13396 	      if (tem == NULL_TREE)
13397 		tem = TREE_VALUE (t);
13398 	      else
13399 		tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
13400 			      TREE_VALUE (t), tem);
13401 	    }
13402 	  t = TREE_CHAIN (t);
13403 	}
13404       if (tem)
13405 	first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
13406       first = c_fully_fold (first, false, NULL, true);
13407       *tp = first;
13408     }
13409   else
13410     {
13411       unsigned int num = types.length (), i;
13412       tree t, side_effects = NULL_TREE, size = NULL_TREE;
13413       tree condition = NULL_TREE;
13414 
13415       if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
13416 	maybe_zero_len = true;
13417 
13418       for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
13419 	   t = TREE_CHAIN (t))
13420 	{
13421 	  tree low_bound = TREE_PURPOSE (t);
13422 	  tree length = TREE_VALUE (t);
13423 
13424 	  i--;
13425 	  if (low_bound
13426 	      && TREE_CODE (low_bound) == INTEGER_CST
13427 	      && TYPE_PRECISION (TREE_TYPE (low_bound))
13428 		 > TYPE_PRECISION (sizetype))
13429 	    low_bound = fold_convert (sizetype, low_bound);
13430 	  if (length
13431 	      && TREE_CODE (length) == INTEGER_CST
13432 	      && TYPE_PRECISION (TREE_TYPE (length))
13433 		 > TYPE_PRECISION (sizetype))
13434 	    length = fold_convert (sizetype, length);
13435 	  if (low_bound == NULL_TREE)
13436 	    low_bound = integer_zero_node;
13437 	  if (!maybe_zero_len && i > first_non_one)
13438 	    {
13439 	      if (integer_nonzerop (low_bound))
13440 		goto do_warn_noncontiguous;
13441 	      if (length != NULL_TREE
13442 		  && TREE_CODE (length) == INTEGER_CST
13443 		  && TYPE_DOMAIN (types[i])
13444 		  && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
13445 		  && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
13446 		     == INTEGER_CST)
13447 		{
13448 		  tree size;
13449 		  size = size_binop (PLUS_EXPR,
13450 				     TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13451 				     size_one_node);
13452 		  if (!tree_int_cst_equal (length, size))
13453 		    {
13454 		     do_warn_noncontiguous:
13455 		      error_at (OMP_CLAUSE_LOCATION (c),
13456 				"array section is not contiguous in %qs "
13457 				"clause",
13458 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13459 		      return true;
13460 		    }
13461 		}
13462 	      if (length != NULL_TREE
13463 		  && TREE_SIDE_EFFECTS (length))
13464 		{
13465 		  if (side_effects == NULL_TREE)
13466 		    side_effects = length;
13467 		  else
13468 		    side_effects = build2 (COMPOUND_EXPR,
13469 					   TREE_TYPE (side_effects),
13470 					   length, side_effects);
13471 		}
13472 	    }
13473 	  else
13474 	    {
13475 	      tree l;
13476 
13477 	      if (i > first_non_one
13478 		  && ((length && integer_nonzerop (length))
13479 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13480 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13481 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
13482 		continue;
13483 	      if (length)
13484 		l = fold_convert (sizetype, length);
13485 	      else
13486 		{
13487 		  l = size_binop (PLUS_EXPR,
13488 				  TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13489 				  size_one_node);
13490 		  l = size_binop (MINUS_EXPR, l,
13491 				  fold_convert (sizetype, low_bound));
13492 		}
13493 	      if (i > first_non_one)
13494 		{
13495 		  l = fold_build2 (NE_EXPR, boolean_type_node, l,
13496 				   size_zero_node);
13497 		  if (condition == NULL_TREE)
13498 		    condition = l;
13499 		  else
13500 		    condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13501 					     l, condition);
13502 		}
13503 	      else if (size == NULL_TREE)
13504 		{
13505 		  size = size_in_bytes (TREE_TYPE (types[i]));
13506 		  tree eltype = TREE_TYPE (types[num - 1]);
13507 		  while (TREE_CODE (eltype) == ARRAY_TYPE)
13508 		    eltype = TREE_TYPE (eltype);
13509 		  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13510 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13511 		      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13512 		    {
13513 		      if (integer_zerop (size)
13514 			  || integer_zerop (size_in_bytes (eltype)))
13515 			{
13516 			  error_at (OMP_CLAUSE_LOCATION (c),
13517 				    "zero length array section in %qs clause",
13518 				    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13519 			  return error_mark_node;
13520 			}
13521 		      size = size_binop (EXACT_DIV_EXPR, size,
13522 					 size_in_bytes (eltype));
13523 		    }
13524 		  size = size_binop (MULT_EXPR, size, l);
13525 		  if (condition)
13526 		    size = fold_build3 (COND_EXPR, sizetype, condition,
13527 					size, size_zero_node);
13528 		}
13529 	      else
13530 		size = size_binop (MULT_EXPR, size, l);
13531 	    }
13532 	}
13533       if (side_effects)
13534 	size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
13535       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13536 	  || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
13537 	  || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
13538 	{
13539 	  size = size_binop (MINUS_EXPR, size, size_one_node);
13540 	  size = c_fully_fold (size, false, NULL);
13541 	  size = save_expr (size);
13542 	  tree index_type = build_index_type (size);
13543 	  tree eltype = TREE_TYPE (first);
13544 	  while (TREE_CODE (eltype) == ARRAY_TYPE)
13545 	    eltype = TREE_TYPE (eltype);
13546 	  tree type = build_array_type (eltype, index_type);
13547 	  tree ptype = build_pointer_type (eltype);
13548 	  if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13549 	    t = build_fold_addr_expr (t);
13550 	  tree t2 = build_fold_addr_expr (first);
13551 	  t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13552 				 ptrdiff_type_node, t2);
13553 	  t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13554 				ptrdiff_type_node, t2,
13555 				fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13556 						  ptrdiff_type_node, t));
13557 	  t2 = c_fully_fold (t2, false, NULL);
13558 	  if (tree_fits_shwi_p (t2))
13559 	    t = build2 (MEM_REF, type, t,
13560 			build_int_cst (ptype, tree_to_shwi (t2)));
13561 	  else
13562 	    {
13563 	      t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13564 	      t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13565 			      TREE_TYPE (t), t, t2);
13566 	      t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13567 	    }
13568 	  OMP_CLAUSE_DECL (c) = t;
13569 	  return false;
13570 	}
13571       first = c_fully_fold (first, false, NULL);
13572       OMP_CLAUSE_DECL (c) = first;
13573       if (size)
13574 	size = c_fully_fold (size, false, NULL);
13575       OMP_CLAUSE_SIZE (c) = size;
13576       if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13577 	  || (TREE_CODE (t) == COMPONENT_REF
13578 	      && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
13579 	return false;
13580       gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
13581       if (ort == C_ORT_OMP || ort == C_ORT_ACC)
13582 	switch (OMP_CLAUSE_MAP_KIND (c))
13583 	  {
13584 	  case GOMP_MAP_ALLOC:
13585 	  case GOMP_MAP_IF_PRESENT:
13586 	  case GOMP_MAP_TO:
13587 	  case GOMP_MAP_FROM:
13588 	  case GOMP_MAP_TOFROM:
13589 	  case GOMP_MAP_ALWAYS_TO:
13590 	  case GOMP_MAP_ALWAYS_FROM:
13591 	  case GOMP_MAP_ALWAYS_TOFROM:
13592 	  case GOMP_MAP_RELEASE:
13593 	  case GOMP_MAP_DELETE:
13594 	  case GOMP_MAP_FORCE_TO:
13595 	  case GOMP_MAP_FORCE_FROM:
13596 	  case GOMP_MAP_FORCE_TOFROM:
13597 	  case GOMP_MAP_FORCE_PRESENT:
13598 	    OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13599 	    break;
13600 	  default:
13601 	    break;
13602 	  }
13603       tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
13604       if (ort != C_ORT_OMP && ort != C_ORT_ACC)
13605 	OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13606       else if (TREE_CODE (t) == COMPONENT_REF)
13607 	{
13608 	  gomp_map_kind k = (ort == C_ORT_ACC) ? GOMP_MAP_ATTACH_DETACH
13609 					       : GOMP_MAP_ALWAYS_POINTER;
13610 	  OMP_CLAUSE_SET_MAP_KIND (c2, k);
13611 	}
13612       else
13613 	OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13614       if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13615 	  && !c_mark_addressable (t))
13616 	return false;
13617       OMP_CLAUSE_DECL (c2) = t;
13618       t = build_fold_addr_expr (first);
13619       t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13620       tree ptr = OMP_CLAUSE_DECL (c2);
13621       if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13622 	ptr = build_fold_addr_expr (ptr);
13623       t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13624 			   ptrdiff_type_node, t,
13625 			   fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13626 					     ptrdiff_type_node, ptr));
13627       t = c_fully_fold (t, false, NULL);
13628       OMP_CLAUSE_SIZE (c2) = t;
13629       OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13630       OMP_CLAUSE_CHAIN (c) = c2;
13631     }
13632   return false;
13633 }
13634 
13635 /* Helper function of finish_omp_clauses.  Clone STMT as if we were making
13636    an inline call.  But, remap
13637    the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13638    and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL.  */
13639 
13640 static tree
c_clone_omp_udr(tree stmt,tree omp_decl1,tree omp_decl2,tree decl,tree placeholder)13641 c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13642 		 tree decl, tree placeholder)
13643 {
13644   copy_body_data id;
13645   hash_map<tree, tree> decl_map;
13646 
13647   decl_map.put (omp_decl1, placeholder);
13648   decl_map.put (omp_decl2, decl);
13649   memset (&id, 0, sizeof (id));
13650   id.src_fn = DECL_CONTEXT (omp_decl1);
13651   id.dst_fn = current_function_decl;
13652   id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
13653   id.decl_map = &decl_map;
13654 
13655   id.copy_decl = copy_decl_no_change;
13656   id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13657   id.transform_new_cfg = true;
13658   id.transform_return_to_modify = false;
13659   id.transform_lang_insert_block = NULL;
13660   id.eh_lp_nr = 0;
13661   walk_tree (&stmt, copy_tree_body_r, &id, NULL);
13662   return stmt;
13663 }
13664 
13665 /* Helper function of c_finish_omp_clauses, called via walk_tree.
13666    Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP.  */
13667 
13668 static tree
c_find_omp_placeholder_r(tree * tp,int *,void * data)13669 c_find_omp_placeholder_r (tree *tp, int *, void *data)
13670 {
13671   if (*tp == (tree) data)
13672     return *tp;
13673   return NULL_TREE;
13674 }
13675 
13676 /* Similarly, but also walk aggregate fields.  */
13677 
13678 struct c_find_omp_var_s { tree var; hash_set<tree> *pset; };
13679 
13680 static tree
c_find_omp_var_r(tree * tp,int *,void * data)13681 c_find_omp_var_r (tree *tp, int *, void *data)
13682 {
13683   if (*tp == ((struct c_find_omp_var_s *) data)->var)
13684     return *tp;
13685   if (RECORD_OR_UNION_TYPE_P (*tp))
13686     {
13687       tree field;
13688       hash_set<tree> *pset = ((struct c_find_omp_var_s *) data)->pset;
13689 
13690       for (field = TYPE_FIELDS (*tp); field;
13691 	   field = DECL_CHAIN (field))
13692 	if (TREE_CODE (field) == FIELD_DECL)
13693 	  {
13694 	    tree ret = walk_tree (&DECL_FIELD_OFFSET (field),
13695 				  c_find_omp_var_r, data, pset);
13696 	    if (ret)
13697 	      return ret;
13698 	    ret = walk_tree (&DECL_SIZE (field), c_find_omp_var_r, data, pset);
13699 	    if (ret)
13700 	      return ret;
13701 	    ret = walk_tree (&DECL_SIZE_UNIT (field), c_find_omp_var_r, data,
13702 			     pset);
13703 	    if (ret)
13704 	      return ret;
13705 	    ret = walk_tree (&TREE_TYPE (field), c_find_omp_var_r, data, pset);
13706 	    if (ret)
13707 	      return ret;
13708 	  }
13709     }
13710   else if (INTEGRAL_TYPE_P (*tp))
13711     return walk_tree (&TYPE_MAX_VALUE (*tp), c_find_omp_var_r, data,
13712 		      ((struct c_find_omp_var_s *) data)->pset);
13713   return NULL_TREE;
13714 }
13715 
13716 /* Finish OpenMP iterators ITER.  Return true if they are errorneous
13717    and clauses containing them should be removed.  */
13718 
13719 static bool
c_omp_finish_iterators(tree iter)13720 c_omp_finish_iterators (tree iter)
13721 {
13722   bool ret = false;
13723   for (tree it = iter; it; it = TREE_CHAIN (it))
13724     {
13725       tree var = TREE_VEC_ELT (it, 0);
13726       tree begin = TREE_VEC_ELT (it, 1);
13727       tree end = TREE_VEC_ELT (it, 2);
13728       tree step = TREE_VEC_ELT (it, 3);
13729       tree orig_step;
13730       tree type = TREE_TYPE (var);
13731       location_t loc = DECL_SOURCE_LOCATION (var);
13732       if (type == error_mark_node)
13733 	{
13734 	  ret = true;
13735 	  continue;
13736 	}
13737       if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
13738 	{
13739 	  error_at (loc, "iterator %qD has neither integral nor pointer type",
13740 		    var);
13741 	  ret = true;
13742 	  continue;
13743 	}
13744       else if (TYPE_ATOMIC (type))
13745 	{
13746 	  error_at (loc, "iterator %qD has %<_Atomic%> qualified type", var);
13747 	  ret = true;
13748 	  continue;
13749 	}
13750       else if (TYPE_READONLY (type))
13751 	{
13752 	  error_at (loc, "iterator %qD has const qualified type", var);
13753 	  ret = true;
13754 	  continue;
13755 	}
13756       else if (step == error_mark_node
13757 	       || TREE_TYPE (step) == error_mark_node)
13758 	{
13759 	  ret = true;
13760 	  continue;
13761 	}
13762       else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13763 	{
13764 	  error_at (EXPR_LOC_OR_LOC (step, loc),
13765 		    "iterator step with non-integral type");
13766 	  ret = true;
13767 	  continue;
13768 	}
13769       begin = c_fully_fold (build_c_cast (loc, type, begin), false, NULL);
13770       end = c_fully_fold (build_c_cast (loc, type, end), false, NULL);
13771       orig_step = save_expr (c_fully_fold (step, false, NULL));
13772       tree stype = POINTER_TYPE_P (type) ? sizetype : type;
13773       step = c_fully_fold (build_c_cast (loc, stype, orig_step), false, NULL);
13774       if (POINTER_TYPE_P (type))
13775 	{
13776 	  begin = save_expr (begin);
13777 	  step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
13778 	  step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
13779 				  fold_convert (sizetype, step),
13780 				  fold_convert (sizetype, begin));
13781 	  step = fold_convert (ssizetype, step);
13782 	}
13783       if (integer_zerop (step))
13784 	{
13785 	  error_at (loc, "iterator %qD has zero step", var);
13786 	  ret = true;
13787 	  continue;
13788 	}
13789 
13790       if (begin == error_mark_node
13791 	  || end == error_mark_node
13792 	  || step == error_mark_node
13793 	  || orig_step == error_mark_node)
13794 	{
13795 	  ret = true;
13796 	  continue;
13797 	}
13798       hash_set<tree> pset;
13799       tree it2;
13800       for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
13801 	{
13802 	  tree var2 = TREE_VEC_ELT (it2, 0);
13803 	  tree begin2 = TREE_VEC_ELT (it2, 1);
13804 	  tree end2 = TREE_VEC_ELT (it2, 2);
13805 	  tree step2 = TREE_VEC_ELT (it2, 3);
13806 	  tree type2 = TREE_TYPE (var2);
13807 	  location_t loc2 = DECL_SOURCE_LOCATION (var2);
13808 	  struct c_find_omp_var_s data = { var, &pset };
13809 	  if (walk_tree (&type2, c_find_omp_var_r, &data, &pset))
13810 	    {
13811 	      error_at (loc2,
13812 			"type of iterator %qD refers to outer iterator %qD",
13813 			var2, var);
13814 	      break;
13815 	    }
13816 	  else if (walk_tree (&begin2, c_find_omp_var_r, &data, &pset))
13817 	    {
13818 	      error_at (EXPR_LOC_OR_LOC (begin2, loc2),
13819 			"begin expression refers to outer iterator %qD", var);
13820 	      break;
13821 	    }
13822 	  else if (walk_tree (&end2, c_find_omp_var_r, &data, &pset))
13823 	    {
13824 	      error_at (EXPR_LOC_OR_LOC (end2, loc2),
13825 			"end expression refers to outer iterator %qD", var);
13826 	      break;
13827 	    }
13828 	  else if (walk_tree (&step2, c_find_omp_var_r, &data, &pset))
13829 	    {
13830 	      error_at (EXPR_LOC_OR_LOC (step2, loc2),
13831 			"step expression refers to outer iterator %qD", var);
13832 	      break;
13833 	    }
13834 	}
13835       if (it2)
13836 	{
13837 	  ret = true;
13838 	  continue;
13839 	}
13840       TREE_VEC_ELT (it, 1) = begin;
13841       TREE_VEC_ELT (it, 2) = end;
13842       TREE_VEC_ELT (it, 3) = step;
13843       TREE_VEC_ELT (it, 4) = orig_step;
13844     }
13845   return ret;
13846 }
13847 
13848 /* Ensure that pointers are used in OpenACC attach and detach clauses.
13849    Return true if an error has been detected.  */
13850 
13851 static bool
c_oacc_check_attachments(tree c)13852 c_oacc_check_attachments (tree c)
13853 {
13854   if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13855     return false;
13856 
13857   /* OpenACC attach / detach clauses must be pointers.  */
13858   if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
13859       || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
13860     {
13861       tree t = OMP_CLAUSE_DECL (c);
13862 
13863       while (TREE_CODE (t) == TREE_LIST)
13864 	t = TREE_CHAIN (t);
13865 
13866       if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13867 	{
13868 	  error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
13869 		    c_omp_map_clause_name (c, true));
13870 	  return true;
13871 	}
13872     }
13873 
13874   return false;
13875 }
13876 
13877 /* For all elements of CLAUSES, validate them against their constraints.
13878    Remove any elements from the list that are invalid.  */
13879 
13880 tree
c_finish_omp_clauses(tree clauses,enum c_omp_region_type ort)13881 c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
13882 {
13883   bitmap_head generic_head, firstprivate_head, lastprivate_head;
13884   bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
13885   tree c, t, type, *pc;
13886   tree simdlen = NULL_TREE, safelen = NULL_TREE;
13887   bool branch_seen = false;
13888   bool copyprivate_seen = false;
13889   bool linear_variable_step_check = false;
13890   tree *nowait_clause = NULL;
13891   tree ordered_clause = NULL_TREE;
13892   tree schedule_clause = NULL_TREE;
13893   bool oacc_async = false;
13894   tree last_iterators = NULL_TREE;
13895   bool last_iterators_remove = false;
13896   tree *nogroup_seen = NULL;
13897   tree *order_clause = NULL;
13898   /* 1 if normal/task reduction has been seen, -1 if inscan reduction
13899      has been seen, -2 if mixed inscan/normal reduction diagnosed.  */
13900   int reduction_seen = 0;
13901 
13902   bitmap_obstack_initialize (NULL);
13903   bitmap_initialize (&generic_head, &bitmap_default_obstack);
13904   bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13905   bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
13906   bitmap_initialize (&aligned_head, &bitmap_default_obstack);
13907   /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead.  */
13908   bitmap_initialize (&map_head, &bitmap_default_obstack);
13909   bitmap_initialize (&map_field_head, &bitmap_default_obstack);
13910   /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
13911      instead.  */
13912   bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
13913 
13914   if (ort & C_ORT_ACC)
13915     for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13916       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13917 	{
13918 	  oacc_async = true;
13919 	  break;
13920 	}
13921 
13922   for (pc = &clauses, c = clauses; c ; c = *pc)
13923     {
13924       bool remove = false;
13925       bool need_complete = false;
13926       bool need_implicitly_determined = false;
13927 
13928       switch (OMP_CLAUSE_CODE (c))
13929 	{
13930 	case OMP_CLAUSE_SHARED:
13931 	  need_implicitly_determined = true;
13932 	  goto check_dup_generic;
13933 
13934 	case OMP_CLAUSE_PRIVATE:
13935 	  need_complete = true;
13936 	  need_implicitly_determined = true;
13937 	  goto check_dup_generic;
13938 
13939 	case OMP_CLAUSE_REDUCTION:
13940 	  if (reduction_seen == 0)
13941 	    reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
13942 	  else if (reduction_seen != -2
13943 		   && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
13944 					 ? -1 : 1))
13945 	    {
13946 	      error_at (OMP_CLAUSE_LOCATION (c),
13947 			"%<inscan%> and non-%<inscan%> %<reduction%> clauses "
13948 			"on the same construct");
13949 	      reduction_seen = -2;
13950 	    }
13951 	  /* FALLTHRU */
13952 	case OMP_CLAUSE_IN_REDUCTION:
13953 	case OMP_CLAUSE_TASK_REDUCTION:
13954 	  need_implicitly_determined = true;
13955 	  t = OMP_CLAUSE_DECL (c);
13956 	  if (TREE_CODE (t) == TREE_LIST)
13957 	    {
13958 	      if (handle_omp_array_sections (c, ort))
13959 		{
13960 		  remove = true;
13961 		  break;
13962 		}
13963 
13964 	      t = OMP_CLAUSE_DECL (c);
13965 	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
13966 		  && OMP_CLAUSE_REDUCTION_INSCAN (c))
13967 		{
13968 		  error_at (OMP_CLAUSE_LOCATION (c),
13969 			    "%<inscan%> %<reduction%> clause with array "
13970 			    "section");
13971 		  remove = true;
13972 		  break;
13973 		}
13974 	    }
13975 	  t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
13976 	  if (t == error_mark_node)
13977 	    {
13978 	      remove = true;
13979 	      break;
13980 	    }
13981 	  if (oacc_async)
13982 	    c_mark_addressable (t);
13983 	  type = TREE_TYPE (t);
13984 	  if (TREE_CODE (t) == MEM_REF)
13985 	    type = TREE_TYPE (type);
13986 	  if (TREE_CODE (type) == ARRAY_TYPE)
13987 	    {
13988 	      tree oatype = type;
13989 	      gcc_assert (TREE_CODE (t) != MEM_REF);
13990 	      while (TREE_CODE (type) == ARRAY_TYPE)
13991 		type = TREE_TYPE (type);
13992 	      if (integer_zerop (TYPE_SIZE_UNIT (type)))
13993 		{
13994 		  error_at (OMP_CLAUSE_LOCATION (c),
13995 			    "%qD in %<reduction%> clause is a zero size array",
13996 			    t);
13997 		  remove = true;
13998 		  break;
13999 		}
14000 	      tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
14001 				      TYPE_SIZE_UNIT (type));
14002 	      if (integer_zerop (size))
14003 		{
14004 		  error_at (OMP_CLAUSE_LOCATION (c),
14005 			    "%qD in %<reduction%> clause is a zero size array",
14006 			    t);
14007 		  remove = true;
14008 		  break;
14009 		}
14010 	      size = size_binop (MINUS_EXPR, size, size_one_node);
14011 	      size = save_expr (size);
14012 	      tree index_type = build_index_type (size);
14013 	      tree atype = build_array_type (type, index_type);
14014 	      tree ptype = build_pointer_type (type);
14015 	      if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14016 		t = build_fold_addr_expr (t);
14017 	      t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
14018 	      OMP_CLAUSE_DECL (c) = t;
14019 	    }
14020 	  if (TYPE_ATOMIC (type))
14021 	    {
14022 	      error_at (OMP_CLAUSE_LOCATION (c),
14023 			"%<_Atomic%> %qE in %<reduction%> clause", t);
14024 	      remove = true;
14025 	      break;
14026 	    }
14027 	  if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
14028 	      || OMP_CLAUSE_REDUCTION_TASK (c))
14029 	    {
14030 	      /* Disallow zero sized or potentially zero sized task
14031 		 reductions.  */
14032 	      if (integer_zerop (TYPE_SIZE_UNIT (type)))
14033 		{
14034 		  error_at (OMP_CLAUSE_LOCATION (c),
14035 			    "zero sized type %qT in %qs clause", type,
14036 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14037 		  remove = true;
14038 		  break;
14039 		}
14040 	      else if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
14041 		{
14042 		  error_at (OMP_CLAUSE_LOCATION (c),
14043 			    "variable sized type %qT in %qs clause", type,
14044 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14045 		  remove = true;
14046 		  break;
14047 		}
14048 	    }
14049 	  if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
14050 	      && (FLOAT_TYPE_P (type)
14051 		  || TREE_CODE (type) == COMPLEX_TYPE))
14052 	    {
14053 	      enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
14054 	      const char *r_name = NULL;
14055 
14056 	      switch (r_code)
14057 		{
14058 		case PLUS_EXPR:
14059 		case MULT_EXPR:
14060 		case MINUS_EXPR:
14061 		case TRUTH_ANDIF_EXPR:
14062 		case TRUTH_ORIF_EXPR:
14063 		  break;
14064 		case MIN_EXPR:
14065 		  if (TREE_CODE (type) == COMPLEX_TYPE)
14066 		    r_name = "min";
14067 		  break;
14068 		case MAX_EXPR:
14069 		  if (TREE_CODE (type) == COMPLEX_TYPE)
14070 		    r_name = "max";
14071 		  break;
14072 		case BIT_AND_EXPR:
14073 		  r_name = "&";
14074 		  break;
14075 		case BIT_XOR_EXPR:
14076 		  r_name = "^";
14077 		  break;
14078 		case BIT_IOR_EXPR:
14079 		  r_name = "|";
14080 		  break;
14081 		default:
14082 		  gcc_unreachable ();
14083 		}
14084 	      if (r_name)
14085 		{
14086 		  error_at (OMP_CLAUSE_LOCATION (c),
14087 			    "%qE has invalid type for %<reduction(%s)%>",
14088 			    t, r_name);
14089 		  remove = true;
14090 		  break;
14091 		}
14092 	    }
14093 	  else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
14094 	    {
14095 	      error_at (OMP_CLAUSE_LOCATION (c),
14096 			"user defined reduction not found for %qE", t);
14097 	      remove = true;
14098 	      break;
14099 	    }
14100 	  else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
14101 	    {
14102 	      tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
14103 	      type = TYPE_MAIN_VARIANT (type);
14104 	      tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14105 					     VAR_DECL, NULL_TREE, type);
14106 	      tree decl_placeholder = NULL_TREE;
14107 	      OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
14108 	      DECL_ARTIFICIAL (placeholder) = 1;
14109 	      DECL_IGNORED_P (placeholder) = 1;
14110 	      if (TREE_CODE (t) == MEM_REF)
14111 		{
14112 		  decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
14113 						 VAR_DECL, NULL_TREE, type);
14114 		  OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
14115 		  DECL_ARTIFICIAL (decl_placeholder) = 1;
14116 		  DECL_IGNORED_P (decl_placeholder) = 1;
14117 		}
14118 	      if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
14119 		c_mark_addressable (placeholder);
14120 	      if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
14121 		c_mark_addressable (decl_placeholder ? decl_placeholder
14122 				    : OMP_CLAUSE_DECL (c));
14123 	      OMP_CLAUSE_REDUCTION_MERGE (c)
14124 		= c_clone_omp_udr (TREE_VEC_ELT (list, 2),
14125 				   TREE_VEC_ELT (list, 0),
14126 				   TREE_VEC_ELT (list, 1),
14127 				   decl_placeholder ? decl_placeholder
14128 				   : OMP_CLAUSE_DECL (c), placeholder);
14129 	      OMP_CLAUSE_REDUCTION_MERGE (c)
14130 		= build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14131 			      void_type_node, NULL_TREE,
14132 			      OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
14133 	      TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
14134 	      if (TREE_VEC_LENGTH (list) == 6)
14135 		{
14136 		  if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
14137 		    c_mark_addressable (decl_placeholder ? decl_placeholder
14138 					: OMP_CLAUSE_DECL (c));
14139 		  if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
14140 		    c_mark_addressable (placeholder);
14141 		  tree init = TREE_VEC_ELT (list, 5);
14142 		  if (init == error_mark_node)
14143 		    init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
14144 		  OMP_CLAUSE_REDUCTION_INIT (c)
14145 		    = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
14146 				       TREE_VEC_ELT (list, 3),
14147 				       decl_placeholder ? decl_placeholder
14148 				       : OMP_CLAUSE_DECL (c), placeholder);
14149 		  if (TREE_VEC_ELT (list, 5) == error_mark_node)
14150 		    {
14151 		      tree v = decl_placeholder ? decl_placeholder : t;
14152 		      OMP_CLAUSE_REDUCTION_INIT (c)
14153 			= build2 (INIT_EXPR, TREE_TYPE (v), v,
14154 				  OMP_CLAUSE_REDUCTION_INIT (c));
14155 		    }
14156 		  if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
14157 				 c_find_omp_placeholder_r,
14158 				 placeholder, NULL))
14159 		    OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
14160 		}
14161 	      else
14162 		{
14163 		  tree init;
14164 		  tree v = decl_placeholder ? decl_placeholder : t;
14165 		  if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
14166 		    init = build_constructor (TREE_TYPE (v), NULL);
14167 		  else
14168 		    init = fold_convert (TREE_TYPE (v), integer_zero_node);
14169 		  OMP_CLAUSE_REDUCTION_INIT (c)
14170 		    = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
14171 		}
14172 	      OMP_CLAUSE_REDUCTION_INIT (c)
14173 		= build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
14174 			      void_type_node, NULL_TREE,
14175 			       OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
14176 	      TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
14177 	    }
14178 	  if (TREE_CODE (t) == MEM_REF)
14179 	    {
14180 	      if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
14181 		  || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
14182 		     != INTEGER_CST)
14183 		{
14184 		  sorry ("variable length element type in array "
14185 			 "%<reduction%> clause");
14186 		  remove = true;
14187 		  break;
14188 		}
14189 	      t = TREE_OPERAND (t, 0);
14190 	      if (TREE_CODE (t) == POINTER_PLUS_EXPR)
14191 		t = TREE_OPERAND (t, 0);
14192 	      if (TREE_CODE (t) == ADDR_EXPR)
14193 		t = TREE_OPERAND (t, 0);
14194 	    }
14195 	  goto check_dup_generic_t;
14196 
14197 	case OMP_CLAUSE_COPYPRIVATE:
14198 	  copyprivate_seen = true;
14199 	  if (nowait_clause)
14200 	    {
14201 	      error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
14202 			"%<nowait%> clause must not be used together "
14203 			"with %<copyprivate%>");
14204 	      *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
14205 	      nowait_clause = NULL;
14206 	    }
14207 	  goto check_dup_generic;
14208 
14209 	case OMP_CLAUSE_COPYIN:
14210 	  t = OMP_CLAUSE_DECL (c);
14211 	  if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
14212 	    {
14213 	      error_at (OMP_CLAUSE_LOCATION (c),
14214 			"%qE must be %<threadprivate%> for %<copyin%>", t);
14215 	      remove = true;
14216 	      break;
14217 	    }
14218 	  goto check_dup_generic;
14219 
14220 	case OMP_CLAUSE_LINEAR:
14221 	  if (ort != C_ORT_OMP_DECLARE_SIMD)
14222 	    need_implicitly_determined = true;
14223 	  t = OMP_CLAUSE_DECL (c);
14224 	  if (ort != C_ORT_OMP_DECLARE_SIMD
14225 	      && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
14226 	    {
14227 	      error_at (OMP_CLAUSE_LOCATION (c),
14228 			"modifier should not be specified in %<linear%> "
14229 			"clause on %<simd%> or %<for%> constructs");
14230 	      OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
14231 	    }
14232 	  if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
14233 	      && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14234 	    {
14235 	      error_at (OMP_CLAUSE_LOCATION (c),
14236 			"linear clause applied to non-integral non-pointer "
14237 			"variable with type %qT", TREE_TYPE (t));
14238 	      remove = true;
14239 	      break;
14240 	    }
14241 	  if (TYPE_ATOMIC (TREE_TYPE (t)))
14242 	    {
14243 	      error_at (OMP_CLAUSE_LOCATION (c),
14244 			"%<_Atomic%> %qD in %<linear%> clause", t);
14245 	      remove = true;
14246 	      break;
14247 	    }
14248 	  if (ort == C_ORT_OMP_DECLARE_SIMD)
14249 	    {
14250 	      tree s = OMP_CLAUSE_LINEAR_STEP (c);
14251 	      if (TREE_CODE (s) == PARM_DECL)
14252 		{
14253 		  OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
14254 		  /* map_head bitmap is used as uniform_head if
14255 		     declare_simd.  */
14256 		  if (!bitmap_bit_p (&map_head, DECL_UID (s)))
14257 		    linear_variable_step_check = true;
14258 		  goto check_dup_generic;
14259 		}
14260 	      if (TREE_CODE (s) != INTEGER_CST)
14261 		{
14262 		  error_at (OMP_CLAUSE_LOCATION (c),
14263 			    "%<linear%> clause step %qE is neither constant "
14264 			    "nor a parameter", s);
14265 		  remove = true;
14266 		  break;
14267 		}
14268 	    }
14269 	  if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
14270 	    {
14271 	      tree s = OMP_CLAUSE_LINEAR_STEP (c);
14272 	      s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
14273 				   OMP_CLAUSE_DECL (c), s);
14274 	      s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14275 				   sizetype, fold_convert (sizetype, s),
14276 				   fold_convert
14277 				     (sizetype, OMP_CLAUSE_DECL (c)));
14278 	      if (s == error_mark_node)
14279 		s = size_one_node;
14280 	      OMP_CLAUSE_LINEAR_STEP (c) = s;
14281 	    }
14282 	  else
14283 	    OMP_CLAUSE_LINEAR_STEP (c)
14284 	      = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
14285 	  goto check_dup_generic;
14286 
14287 	check_dup_generic:
14288 	  t = OMP_CLAUSE_DECL (c);
14289 	check_dup_generic_t:
14290 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14291 	    {
14292 	      error_at (OMP_CLAUSE_LOCATION (c),
14293 			"%qE is not a variable in clause %qs", t,
14294 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14295 	      remove = true;
14296 	    }
14297 	  else if ((ort == C_ORT_ACC
14298 		    && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
14299 		   || (ort == C_ORT_OMP
14300 		       && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14301 			   || (OMP_CLAUSE_CODE (c)
14302 			       == OMP_CLAUSE_USE_DEVICE_ADDR))))
14303 	    {
14304 	      if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14305 		{
14306 		  error_at (OMP_CLAUSE_LOCATION (c),
14307 			    ort == C_ORT_ACC
14308 			    ? "%qD appears more than once in reduction clauses"
14309 			    : "%qD appears more than once in data clauses",
14310 			    t);
14311 		  remove = true;
14312 		}
14313 	      else
14314 		bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14315 	    }
14316 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14317 		   || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
14318 		   || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14319 	    {
14320 	      error_at (OMP_CLAUSE_LOCATION (c),
14321 			"%qE appears more than once in data clauses", t);
14322 	      remove = true;
14323 	    }
14324 	  else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
14325 		   && bitmap_bit_p (&map_head, DECL_UID (t)))
14326 	    {
14327 	      if (ort == C_ORT_ACC)
14328 		error_at (OMP_CLAUSE_LOCATION (c),
14329 			  "%qD appears more than once in data clauses", t);
14330 	      else
14331 		error_at (OMP_CLAUSE_LOCATION (c),
14332 			  "%qD appears both in data and map clauses", t);
14333 	      remove = true;
14334 	    }
14335 	  else
14336 	    bitmap_set_bit (&generic_head, DECL_UID (t));
14337 	  break;
14338 
14339 	case OMP_CLAUSE_FIRSTPRIVATE:
14340 	  t = OMP_CLAUSE_DECL (c);
14341 	  need_complete = true;
14342 	  need_implicitly_determined = true;
14343 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14344 	    {
14345 	      error_at (OMP_CLAUSE_LOCATION (c),
14346 			"%qE is not a variable in clause %<firstprivate%>", t);
14347 	      remove = true;
14348 	    }
14349 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14350 		   || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14351 	    {
14352 	      error_at (OMP_CLAUSE_LOCATION (c),
14353 			"%qE appears more than once in data clauses", t);
14354 	      remove = true;
14355 	    }
14356 	  else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14357 	    {
14358 	      if (ort == C_ORT_ACC)
14359 		error_at (OMP_CLAUSE_LOCATION (c),
14360 			  "%qD appears more than once in data clauses", t);
14361 	      else
14362 		error_at (OMP_CLAUSE_LOCATION (c),
14363 			  "%qD appears both in data and map clauses", t);
14364 	      remove = true;
14365 	    }
14366 	  else
14367 	    bitmap_set_bit (&firstprivate_head, DECL_UID (t));
14368 	  break;
14369 
14370 	case OMP_CLAUSE_LASTPRIVATE:
14371 	  t = OMP_CLAUSE_DECL (c);
14372 	  need_complete = true;
14373 	  need_implicitly_determined = true;
14374 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14375 	    {
14376 	      error_at (OMP_CLAUSE_LOCATION (c),
14377 			"%qE is not a variable in clause %<lastprivate%>", t);
14378 	      remove = true;
14379 	    }
14380 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14381 		   || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
14382 	    {
14383 	      error_at (OMP_CLAUSE_LOCATION (c),
14384 		     "%qE appears more than once in data clauses", t);
14385 	      remove = true;
14386 	    }
14387 	  else
14388 	    bitmap_set_bit (&lastprivate_head, DECL_UID (t));
14389 	  break;
14390 
14391 	case OMP_CLAUSE_ALIGNED:
14392 	  t = OMP_CLAUSE_DECL (c);
14393 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14394 	    {
14395 	      error_at (OMP_CLAUSE_LOCATION (c),
14396 			"%qE is not a variable in %<aligned%> clause", t);
14397 	      remove = true;
14398 	    }
14399 	  else if (!POINTER_TYPE_P (TREE_TYPE (t))
14400 		   && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14401 	    {
14402 	      error_at (OMP_CLAUSE_LOCATION (c),
14403 			"%qE in %<aligned%> clause is neither a pointer nor "
14404 			"an array", t);
14405 	      remove = true;
14406 	    }
14407 	  else if (TYPE_ATOMIC (TREE_TYPE (t)))
14408 	    {
14409 	      error_at (OMP_CLAUSE_LOCATION (c),
14410 			"%<_Atomic%> %qD in %<aligned%> clause", t);
14411 	      remove = true;
14412 	      break;
14413 	    }
14414 	  else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
14415 	    {
14416 	      error_at (OMP_CLAUSE_LOCATION (c),
14417 			"%qE appears more than once in %<aligned%> clauses",
14418 			t);
14419 	      remove = true;
14420 	    }
14421 	  else
14422 	    bitmap_set_bit (&aligned_head, DECL_UID (t));
14423 	  break;
14424 
14425 	case OMP_CLAUSE_NONTEMPORAL:
14426 	  t = OMP_CLAUSE_DECL (c);
14427 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14428 	    {
14429 	      error_at (OMP_CLAUSE_LOCATION (c),
14430 			"%qE is not a variable in %<nontemporal%> clause", t);
14431 	      remove = true;
14432 	    }
14433 	  else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
14434 	    {
14435 	      error_at (OMP_CLAUSE_LOCATION (c),
14436 			"%qE appears more than once in %<nontemporal%> "
14437 			"clauses", t);
14438 	      remove = true;
14439 	    }
14440 	  else
14441 	    bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
14442 	  break;
14443 
14444 	case OMP_CLAUSE_DEPEND:
14445 	  t = OMP_CLAUSE_DECL (c);
14446 	  if (t == NULL_TREE)
14447 	    {
14448 	      gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
14449 			  == OMP_CLAUSE_DEPEND_SOURCE);
14450 	      break;
14451 	    }
14452 	  if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
14453 	    {
14454 	      gcc_assert (TREE_CODE (t) == TREE_LIST);
14455 	      for (; t; t = TREE_CHAIN (t))
14456 		{
14457 		  tree decl = TREE_VALUE (t);
14458 		  if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
14459 		    {
14460 		      tree offset = TREE_PURPOSE (t);
14461 		      bool neg = wi::neg_p (wi::to_wide (offset));
14462 		      offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
14463 		      tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
14464 						 neg ? MINUS_EXPR : PLUS_EXPR,
14465 						 decl, offset);
14466 		      t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
14467 					    sizetype,
14468 					    fold_convert (sizetype, t2),
14469 					    fold_convert (sizetype, decl));
14470 		      if (t2 == error_mark_node)
14471 			{
14472 			  remove = true;
14473 			  break;
14474 			}
14475 		      TREE_PURPOSE (t) = t2;
14476 		    }
14477 		}
14478 	      break;
14479 	    }
14480 	  if (TREE_CODE (t) == TREE_LIST
14481 	      && TREE_PURPOSE (t)
14482 	      && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
14483 	    {
14484 	      if (TREE_PURPOSE (t) != last_iterators)
14485 		last_iterators_remove
14486 		  = c_omp_finish_iterators (TREE_PURPOSE (t));
14487 	      last_iterators = TREE_PURPOSE (t);
14488 	      t = TREE_VALUE (t);
14489 	      if (last_iterators_remove)
14490 		t = error_mark_node;
14491 	    }
14492 	  else
14493 	    last_iterators = NULL_TREE;
14494 	  if (TREE_CODE (t) == TREE_LIST)
14495 	    {
14496 	      if (handle_omp_array_sections (c, ort))
14497 		remove = true;
14498 	      else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14499 		{
14500 		  error_at (OMP_CLAUSE_LOCATION (c),
14501 			    "%<depend%> clause with %<depobj%> dependence "
14502 			    "type on array section");
14503 		  remove = true;
14504 		}
14505 	      break;
14506 	    }
14507 	  if (t == error_mark_node)
14508 	    remove = true;
14509 	  else if (!lvalue_p (t))
14510 	    {
14511 	      error_at (OMP_CLAUSE_LOCATION (c),
14512 			"%qE is not lvalue expression nor array section in "
14513 			"%<depend%> clause", t);
14514 	      remove = true;
14515 	    }
14516 	  else if (TREE_CODE (t) == COMPONENT_REF
14517 		   && DECL_C_BIT_FIELD (TREE_OPERAND (t, 1)))
14518 	    {
14519 	      error_at (OMP_CLAUSE_LOCATION (c),
14520 			"bit-field %qE in %qs clause", t, "depend");
14521 	      remove = true;
14522 	    }
14523 	  else if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
14524 	    {
14525 	      if (!c_omp_depend_t_p (TREE_TYPE (t)))
14526 		{
14527 		  error_at (OMP_CLAUSE_LOCATION (c),
14528 			    "%qE does not have %<omp_depend_t%> type in "
14529 			    "%<depend%> clause with %<depobj%> dependence "
14530 			    "type", t);
14531 		  remove = true;
14532 		}
14533 	    }
14534 	  else if (c_omp_depend_t_p (TREE_TYPE (t)))
14535 	    {
14536 	      error_at (OMP_CLAUSE_LOCATION (c),
14537 			"%qE should not have %<omp_depend_t%> type in "
14538 			"%<depend%> clause with dependence type other than "
14539 			"%<depobj%>", t);
14540 	      remove = true;
14541 	    }
14542 	  if (!remove)
14543 	    {
14544 	      tree addr = build_unary_op (OMP_CLAUSE_LOCATION (c), ADDR_EXPR,
14545 					  t, false);
14546 	      if (addr == error_mark_node)
14547 		remove = true;
14548 	      else
14549 		{
14550 		  t = build_indirect_ref (OMP_CLAUSE_LOCATION (c), addr,
14551 					  RO_UNARY_STAR);
14552 		  if (t == error_mark_node)
14553 		    remove = true;
14554 		  else if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
14555 			   && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
14556 			   && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
14557 			       == TREE_VEC))
14558 		    TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
14559 		  else
14560 		    OMP_CLAUSE_DECL (c) = t;
14561 		}
14562 	    }
14563 	  break;
14564 
14565 	case OMP_CLAUSE_MAP:
14566 	case OMP_CLAUSE_TO:
14567 	case OMP_CLAUSE_FROM:
14568 	case OMP_CLAUSE__CACHE_:
14569 	  t = OMP_CLAUSE_DECL (c);
14570 	  if (TREE_CODE (t) == TREE_LIST)
14571 	    {
14572 	      if (handle_omp_array_sections (c, ort))
14573 		remove = true;
14574 	      else
14575 		{
14576 		  t = OMP_CLAUSE_DECL (c);
14577 		  if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14578 		    {
14579 		      error_at (OMP_CLAUSE_LOCATION (c),
14580 				"array section does not have mappable type "
14581 				"in %qs clause",
14582 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14583 		      remove = true;
14584 		    }
14585 		  else if (TYPE_ATOMIC (TREE_TYPE (t)))
14586 		    {
14587 		      error_at (OMP_CLAUSE_LOCATION (c),
14588 				"%<_Atomic%> %qE in %qs clause", t,
14589 				omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14590 		      remove = true;
14591 		    }
14592 		  while (TREE_CODE (t) == ARRAY_REF)
14593 		    t = TREE_OPERAND (t, 0);
14594 		  if (TREE_CODE (t) == COMPONENT_REF
14595 		      && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
14596 		    {
14597 		      while (TREE_CODE (t) == COMPONENT_REF)
14598 			t = TREE_OPERAND (t, 0);
14599 		      if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14600 			break;
14601 		      if (bitmap_bit_p (&map_head, DECL_UID (t)))
14602 			{
14603 			  if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14604 			    error_at (OMP_CLAUSE_LOCATION (c),
14605 				      "%qD appears more than once in motion "
14606 				      "clauses", t);
14607 			  else if (ort == C_ORT_ACC)
14608 			    error_at (OMP_CLAUSE_LOCATION (c),
14609 				      "%qD appears more than once in data "
14610 				      "clauses", t);
14611 			  else
14612 			    error_at (OMP_CLAUSE_LOCATION (c),
14613 				      "%qD appears more than once in map "
14614 				      "clauses", t);
14615 			  remove = true;
14616 			}
14617 		      else
14618 			{
14619 			  bitmap_set_bit (&map_head, DECL_UID (t));
14620 			  bitmap_set_bit (&map_field_head, DECL_UID (t));
14621 			}
14622 		    }
14623 		}
14624 	      if (c_oacc_check_attachments (c))
14625 		remove = true;
14626 	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14627 		  && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14628 		      || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14629 		/* In this case, we have a single array element which is a
14630 		   pointer, and we already set OMP_CLAUSE_SIZE in
14631 		   handle_omp_array_sections above.  For attach/detach clauses,
14632 		   reset the OMP_CLAUSE_SIZE (representing a bias) to zero
14633 		   here.  */
14634 		OMP_CLAUSE_SIZE (c) = size_zero_node;
14635 	      break;
14636 	    }
14637 	  if (t == error_mark_node)
14638 	    {
14639 	      remove = true;
14640 	      break;
14641 	    }
14642 	  /* OpenACC attach / detach clauses must be pointers.  */
14643 	  if (c_oacc_check_attachments (c))
14644 	    {
14645 	      remove = true;
14646 	      break;
14647 	    }
14648 	  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14649 	      && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
14650 		  || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
14651 	    /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
14652 	       bias) to zero here, so it is not set erroneously to the pointer
14653 	       size later on in gimplify.c.  */
14654 	    OMP_CLAUSE_SIZE (c) = size_zero_node;
14655 	  if (TREE_CODE (t) == COMPONENT_REF
14656 	      && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
14657 	    {
14658 	      if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
14659 		{
14660 		  error_at (OMP_CLAUSE_LOCATION (c),
14661 			    "bit-field %qE in %qs clause",
14662 			    t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14663 		  remove = true;
14664 		}
14665 	      else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14666 		{
14667 		  error_at (OMP_CLAUSE_LOCATION (c),
14668 			    "%qE does not have a mappable type in %qs clause",
14669 			    t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14670 		  remove = true;
14671 		}
14672 	      else if (TYPE_ATOMIC (TREE_TYPE (t)))
14673 		{
14674 		  error_at (OMP_CLAUSE_LOCATION (c),
14675 			    "%<_Atomic%> %qE in %qs clause", t,
14676 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14677 		  remove = true;
14678 		}
14679 	      while (TREE_CODE (t) == COMPONENT_REF)
14680 		{
14681 		  if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
14682 		      == UNION_TYPE)
14683 		    {
14684 		      error_at (OMP_CLAUSE_LOCATION (c),
14685 				"%qE is a member of a union", t);
14686 		      remove = true;
14687 		      break;
14688 		    }
14689 		  t = TREE_OPERAND (t, 0);
14690 		  if (ort == C_ORT_ACC && TREE_CODE (t) == MEM_REF)
14691 		    {
14692 		      if (maybe_ne (mem_ref_offset (t), 0))
14693 			error_at (OMP_CLAUSE_LOCATION (c),
14694 				  "cannot dereference %qE in %qs clause", t,
14695 				  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14696 		      else
14697 			t = TREE_OPERAND (t, 0);
14698 		    }
14699 		}
14700 	      if (remove)
14701 		break;
14702 	      if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14703 		{
14704 		  if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
14705 		    break;
14706 		}
14707 	    }
14708 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
14709 	    {
14710 	      error_at (OMP_CLAUSE_LOCATION (c),
14711 			"%qE is not a variable in %qs clause", t,
14712 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14713 	      remove = true;
14714 	    }
14715 	  else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
14716 	    {
14717 	      error_at (OMP_CLAUSE_LOCATION (c),
14718 			"%qD is threadprivate variable in %qs clause", t,
14719 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14720 	      remove = true;
14721 	    }
14722 	  else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
14723 		    || (OMP_CLAUSE_MAP_KIND (c)
14724 			!= GOMP_MAP_FIRSTPRIVATE_POINTER))
14725 		   && !c_mark_addressable (t))
14726 	    remove = true;
14727 	  else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14728 		     && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
14729 			 || (OMP_CLAUSE_MAP_KIND (c)
14730 			     == GOMP_MAP_FIRSTPRIVATE_POINTER)
14731 			 || (OMP_CLAUSE_MAP_KIND (c)
14732 			     == GOMP_MAP_FORCE_DEVICEPTR)))
14733 		   && t == OMP_CLAUSE_DECL (c)
14734 		   && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14735 	    {
14736 	      error_at (OMP_CLAUSE_LOCATION (c),
14737 			"%qD does not have a mappable type in %qs clause", t,
14738 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14739 	      remove = true;
14740 	    }
14741 	  else if (TREE_TYPE (t) == error_mark_node)
14742 	    remove = true;
14743 	  else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
14744 	    {
14745 	      error_at (OMP_CLAUSE_LOCATION (c),
14746 			"%<_Atomic%> %qE in %qs clause", t,
14747 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14748 	      remove = true;
14749 	    }
14750 	  else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
14751 		   && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
14752 	    {
14753 	      if (bitmap_bit_p (&generic_head, DECL_UID (t))
14754 		  || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14755 		{
14756 		  error_at (OMP_CLAUSE_LOCATION (c),
14757 			    "%qD appears more than once in data clauses", t);
14758 		  remove = true;
14759 		}
14760 	      else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14761 		{
14762 		  if (ort == C_ORT_ACC)
14763 		    error_at (OMP_CLAUSE_LOCATION (c),
14764 			      "%qD appears more than once in data clauses", t);
14765 		  else
14766 		    error_at (OMP_CLAUSE_LOCATION (c),
14767 			      "%qD appears both in data and map clauses", t);
14768 		  remove = true;
14769 		}
14770 	      else
14771 		bitmap_set_bit (&generic_head, DECL_UID (t));
14772 	    }
14773 	  else if (bitmap_bit_p (&map_head, DECL_UID (t)))
14774 	    {
14775 	      if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
14776 		error_at (OMP_CLAUSE_LOCATION (c),
14777 			  "%qD appears more than once in motion clauses", t);
14778 	      else if (ort == C_ORT_ACC)
14779 		error_at (OMP_CLAUSE_LOCATION (c),
14780 			  "%qD appears more than once in data clauses", t);
14781 	      else
14782 		error_at (OMP_CLAUSE_LOCATION (c),
14783 			  "%qD appears more than once in map clauses", t);
14784 	      remove = true;
14785 	    }
14786 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
14787 		   || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
14788 	    {
14789 	      if (ort == C_ORT_ACC)
14790 		error_at (OMP_CLAUSE_LOCATION (c),
14791 			  "%qD appears more than once in data clauses", t);
14792 	      else
14793 		error_at (OMP_CLAUSE_LOCATION (c),
14794 			  "%qD appears both in data and map clauses", t);
14795 	      remove = true;
14796 	    }
14797 	  else
14798 	    {
14799 	      bitmap_set_bit (&map_head, DECL_UID (t));
14800 	      if (t != OMP_CLAUSE_DECL (c)
14801 		  && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
14802 		bitmap_set_bit (&map_field_head, DECL_UID (t));
14803 	    }
14804 	  break;
14805 
14806 	case OMP_CLAUSE_TO_DECLARE:
14807 	case OMP_CLAUSE_LINK:
14808 	  t = OMP_CLAUSE_DECL (c);
14809 	  if (TREE_CODE (t) == FUNCTION_DECL
14810 	      && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14811 	    ;
14812 	  else if (!VAR_P (t))
14813 	    {
14814 	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
14815 		error_at (OMP_CLAUSE_LOCATION (c),
14816 			  "%qE is neither a variable nor a function name in "
14817 			  "clause %qs", t,
14818 			  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14819 	      else
14820 		error_at (OMP_CLAUSE_LOCATION (c),
14821 			  "%qE is not a variable in clause %qs", t,
14822 			  omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14823 	      remove = true;
14824 	    }
14825 	  else if (DECL_THREAD_LOCAL_P (t))
14826 	    {
14827 	      error_at (OMP_CLAUSE_LOCATION (c),
14828 			"%qD is threadprivate variable in %qs clause", t,
14829 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14830 	      remove = true;
14831 	    }
14832 	  else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
14833 	    {
14834 	      error_at (OMP_CLAUSE_LOCATION (c),
14835 			"%qD does not have a mappable type in %qs clause", t,
14836 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14837 	      remove = true;
14838 	    }
14839 	  if (remove)
14840 	    break;
14841 	  if (bitmap_bit_p (&generic_head, DECL_UID (t)))
14842 	    {
14843 	      error_at (OMP_CLAUSE_LOCATION (c),
14844 			"%qE appears more than once on the same "
14845 			"%<declare target%> directive", t);
14846 	      remove = true;
14847 	    }
14848 	  else
14849 	    bitmap_set_bit (&generic_head, DECL_UID (t));
14850 	  break;
14851 
14852 	case OMP_CLAUSE_UNIFORM:
14853 	  t = OMP_CLAUSE_DECL (c);
14854 	  if (TREE_CODE (t) != PARM_DECL)
14855 	    {
14856 	      if (DECL_P (t))
14857 		error_at (OMP_CLAUSE_LOCATION (c),
14858 			  "%qD is not an argument in %<uniform%> clause", t);
14859 	      else
14860 		error_at (OMP_CLAUSE_LOCATION (c),
14861 			  "%qE is not an argument in %<uniform%> clause", t);
14862 	      remove = true;
14863 	      break;
14864 	    }
14865 	  /* map_head bitmap is used as uniform_head if declare_simd.  */
14866 	  bitmap_set_bit (&map_head, DECL_UID (t));
14867 	  goto check_dup_generic;
14868 
14869 	case OMP_CLAUSE_IS_DEVICE_PTR:
14870 	case OMP_CLAUSE_USE_DEVICE_PTR:
14871 	  t = OMP_CLAUSE_DECL (c);
14872 	  if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
14873 	    {
14874 	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
14875 		  && ort == C_ORT_OMP)
14876 		{
14877 		  error_at (OMP_CLAUSE_LOCATION (c),
14878 			    "%qs variable is not a pointer",
14879 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14880 		  remove = true;
14881 		}
14882 	      else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
14883 		{
14884 		  error_at (OMP_CLAUSE_LOCATION (c),
14885 			    "%qs variable is neither a pointer nor an array",
14886 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
14887 		  remove = true;
14888 		}
14889 	    }
14890 	  goto check_dup_generic;
14891 
14892 	case OMP_CLAUSE_USE_DEVICE_ADDR:
14893 	  t = OMP_CLAUSE_DECL (c);
14894 	  if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
14895 	    c_mark_addressable (t);
14896 	  goto check_dup_generic;
14897 
14898 	case OMP_CLAUSE_NOWAIT:
14899 	  if (copyprivate_seen)
14900 	    {
14901 	      error_at (OMP_CLAUSE_LOCATION (c),
14902 			"%<nowait%> clause must not be used together "
14903 			"with %<copyprivate%>");
14904 	      remove = true;
14905 	      break;
14906 	    }
14907 	  nowait_clause = pc;
14908 	  pc = &OMP_CLAUSE_CHAIN (c);
14909 	  continue;
14910 
14911 	case OMP_CLAUSE_ORDER:
14912 	  if (ordered_clause)
14913 	    {
14914 	      error_at (OMP_CLAUSE_LOCATION (c),
14915 			"%<order%> clause must not be used together "
14916 			"with %<ordered%>");
14917 	      remove = true;
14918 	      break;
14919 	    }
14920 	  else if (order_clause)
14921 	    {
14922 	      /* Silently remove duplicates.  */
14923 	      remove = true;
14924 	      break;
14925 	    }
14926 	  order_clause = pc;
14927 	  pc = &OMP_CLAUSE_CHAIN (c);
14928 	  continue;
14929 
14930 	case OMP_CLAUSE_IF:
14931 	case OMP_CLAUSE_NUM_THREADS:
14932 	case OMP_CLAUSE_NUM_TEAMS:
14933 	case OMP_CLAUSE_THREAD_LIMIT:
14934 	case OMP_CLAUSE_DEFAULT:
14935 	case OMP_CLAUSE_UNTIED:
14936 	case OMP_CLAUSE_COLLAPSE:
14937 	case OMP_CLAUSE_FINAL:
14938 	case OMP_CLAUSE_MERGEABLE:
14939 	case OMP_CLAUSE_DEVICE:
14940 	case OMP_CLAUSE_DIST_SCHEDULE:
14941 	case OMP_CLAUSE_PARALLEL:
14942 	case OMP_CLAUSE_FOR:
14943 	case OMP_CLAUSE_SECTIONS:
14944 	case OMP_CLAUSE_TASKGROUP:
14945 	case OMP_CLAUSE_PROC_BIND:
14946 	case OMP_CLAUSE_DEVICE_TYPE:
14947 	case OMP_CLAUSE_PRIORITY:
14948 	case OMP_CLAUSE_GRAINSIZE:
14949 	case OMP_CLAUSE_NUM_TASKS:
14950 	case OMP_CLAUSE_THREADS:
14951 	case OMP_CLAUSE_SIMD:
14952 	case OMP_CLAUSE_HINT:
14953 	case OMP_CLAUSE_DEFAULTMAP:
14954 	case OMP_CLAUSE_BIND:
14955 	case OMP_CLAUSE_NUM_GANGS:
14956 	case OMP_CLAUSE_NUM_WORKERS:
14957 	case OMP_CLAUSE_VECTOR_LENGTH:
14958 	case OMP_CLAUSE_ASYNC:
14959 	case OMP_CLAUSE_WAIT:
14960 	case OMP_CLAUSE_AUTO:
14961 	case OMP_CLAUSE_INDEPENDENT:
14962 	case OMP_CLAUSE_SEQ:
14963 	case OMP_CLAUSE_GANG:
14964 	case OMP_CLAUSE_WORKER:
14965 	case OMP_CLAUSE_VECTOR:
14966 	case OMP_CLAUSE_TILE:
14967 	case OMP_CLAUSE_IF_PRESENT:
14968 	case OMP_CLAUSE_FINALIZE:
14969 	  pc = &OMP_CLAUSE_CHAIN (c);
14970 	  continue;
14971 
14972 	case OMP_CLAUSE_NOGROUP:
14973 	  nogroup_seen = pc;
14974 	  pc = &OMP_CLAUSE_CHAIN (c);
14975 	  continue;
14976 
14977 	case OMP_CLAUSE_SCHEDULE:
14978 	  schedule_clause = c;
14979 	  pc = &OMP_CLAUSE_CHAIN (c);
14980 	  continue;
14981 
14982 	case OMP_CLAUSE_ORDERED:
14983 	  ordered_clause = c;
14984 	  if (order_clause)
14985 	    {
14986 	      error_at (OMP_CLAUSE_LOCATION (*order_clause),
14987 			"%<order%> clause must not be used together "
14988 			"with %<ordered%>");
14989 	      *order_clause = OMP_CLAUSE_CHAIN (*order_clause);
14990 	      order_clause = NULL;
14991 	    }
14992 	  pc = &OMP_CLAUSE_CHAIN (c);
14993 	  continue;
14994 
14995 	case OMP_CLAUSE_SAFELEN:
14996 	  safelen = c;
14997 	  pc = &OMP_CLAUSE_CHAIN (c);
14998 	  continue;
14999 	case OMP_CLAUSE_SIMDLEN:
15000 	  simdlen = c;
15001 	  pc = &OMP_CLAUSE_CHAIN (c);
15002 	  continue;
15003 
15004 	case OMP_CLAUSE_INBRANCH:
15005 	case OMP_CLAUSE_NOTINBRANCH:
15006 	  if (branch_seen)
15007 	    {
15008 	      error_at (OMP_CLAUSE_LOCATION (c),
15009 			"%<inbranch%> clause is incompatible with "
15010 			"%<notinbranch%>");
15011 	      remove = true;
15012 	      break;
15013 	    }
15014 	  branch_seen = true;
15015 	  pc = &OMP_CLAUSE_CHAIN (c);
15016 	  continue;
15017 
15018 	case OMP_CLAUSE_INCLUSIVE:
15019 	case OMP_CLAUSE_EXCLUSIVE:
15020 	  need_complete = true;
15021 	  need_implicitly_determined = true;
15022 	  t = OMP_CLAUSE_DECL (c);
15023 	  if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
15024 	    {
15025 	      error_at (OMP_CLAUSE_LOCATION (c),
15026 			"%qE is not a variable in clause %qs", t,
15027 			omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15028 	      remove = true;
15029 	    }
15030 	  break;
15031 
15032 	default:
15033 	  gcc_unreachable ();
15034 	}
15035 
15036       if (!remove)
15037 	{
15038 	  t = OMP_CLAUSE_DECL (c);
15039 
15040 	  if (need_complete)
15041 	    {
15042 	      t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
15043 	      if (t == error_mark_node)
15044 		remove = true;
15045 	    }
15046 
15047 	  if (need_implicitly_determined)
15048 	    {
15049 	      const char *share_name = NULL;
15050 
15051 	      if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
15052 		share_name = "threadprivate";
15053 	      else switch (c_omp_predetermined_sharing (t))
15054 		{
15055 		case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
15056 		  break;
15057 		case OMP_CLAUSE_DEFAULT_SHARED:
15058 		  if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
15059 		       || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
15060 		      && c_omp_predefined_variable (t))
15061 		    /* The __func__ variable and similar function-local
15062 		       predefined variables may be listed in a shared or
15063 		       firstprivate clause.  */
15064 		    break;
15065 		  share_name = "shared";
15066 		  break;
15067 		case OMP_CLAUSE_DEFAULT_PRIVATE:
15068 		  share_name = "private";
15069 		  break;
15070 		default:
15071 		  gcc_unreachable ();
15072 		}
15073 	      if (share_name)
15074 		{
15075 		  error_at (OMP_CLAUSE_LOCATION (c),
15076 			    "%qE is predetermined %qs for %qs",
15077 			    t, share_name,
15078 			    omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
15079 		  remove = true;
15080 		}
15081 	      else if (TREE_READONLY (t)
15082 		       && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
15083 		       && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE)
15084 		{
15085 		  error_at (OMP_CLAUSE_LOCATION (c),
15086 			    "%<const%> qualified %qE may appear only in "
15087 			    "%<shared%> or %<firstprivate%> clauses", t);
15088 		  remove = true;
15089 		}
15090 	    }
15091 	}
15092 
15093       if (remove)
15094 	*pc = OMP_CLAUSE_CHAIN (c);
15095       else
15096 	pc = &OMP_CLAUSE_CHAIN (c);
15097     }
15098 
15099   if (simdlen
15100       && safelen
15101       && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
15102 			  OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
15103     {
15104       error_at (OMP_CLAUSE_LOCATION (simdlen),
15105 		"%<simdlen%> clause value is bigger than "
15106 		"%<safelen%> clause value");
15107       OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
15108 	= OMP_CLAUSE_SAFELEN_EXPR (safelen);
15109     }
15110 
15111   if (ordered_clause
15112       && schedule_clause
15113       && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15114 	  & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
15115     {
15116       error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15117 		"%<nonmonotonic%> schedule modifier specified together "
15118 		"with %<ordered%> clause");
15119       OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15120 	= (enum omp_clause_schedule_kind)
15121 	  (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
15122 	   & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
15123     }
15124 
15125   if (reduction_seen < 0 && ordered_clause)
15126     {
15127       error_at (OMP_CLAUSE_LOCATION (ordered_clause),
15128 		"%qs clause specified together with %<inscan%> "
15129 		"%<reduction%> clause", "ordered");
15130       reduction_seen = -2;
15131     }
15132 
15133   if (reduction_seen < 0 && schedule_clause)
15134     {
15135       error_at (OMP_CLAUSE_LOCATION (schedule_clause),
15136 		"%qs clause specified together with %<inscan%> "
15137 		"%<reduction%> clause", "schedule");
15138       reduction_seen = -2;
15139     }
15140 
15141   if (linear_variable_step_check || reduction_seen == -2)
15142     for (pc = &clauses, c = clauses; c ; c = *pc)
15143       {
15144 	bool remove = false;
15145 	if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
15146 	    && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
15147 	    && !bitmap_bit_p (&map_head,
15148 			      DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
15149 	  {
15150 	    error_at (OMP_CLAUSE_LOCATION (c),
15151 		      "%<linear%> clause step is a parameter %qD not "
15152 		      "specified in %<uniform%> clause",
15153 		      OMP_CLAUSE_LINEAR_STEP (c));
15154 	    remove = true;
15155 	  }
15156 	else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
15157 	  OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
15158 
15159 	if (remove)
15160 	  *pc = OMP_CLAUSE_CHAIN (c);
15161 	else
15162 	  pc = &OMP_CLAUSE_CHAIN (c);
15163       }
15164 
15165   if (nogroup_seen && reduction_seen)
15166     {
15167       error_at (OMP_CLAUSE_LOCATION (*nogroup_seen),
15168 		"%<nogroup%> clause must not be used together with "
15169 		"%<reduction%> clause");
15170       *nogroup_seen = OMP_CLAUSE_CHAIN (*nogroup_seen);
15171     }
15172 
15173   bitmap_obstack_release (NULL);
15174   return clauses;
15175 }
15176 
15177 /* Return code to initialize DST with a copy constructor from SRC.
15178    C doesn't have copy constructors nor assignment operators, only for
15179    _Atomic vars we need to perform __atomic_load from src into a temporary
15180    followed by __atomic_store of the temporary to dst.  */
15181 
15182 tree
c_omp_clause_copy_ctor(tree clause,tree dst,tree src)15183 c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
15184 {
15185   if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
15186     return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
15187 
15188   location_t loc = OMP_CLAUSE_LOCATION (clause);
15189   tree type = TREE_TYPE (dst);
15190   tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
15191   tree tmp = create_tmp_var (nonatomic_type);
15192   tree tmp_addr = build_fold_addr_expr (tmp);
15193   TREE_ADDRESSABLE (tmp) = 1;
15194   TREE_NO_WARNING (tmp) = 1;
15195   tree src_addr = build_fold_addr_expr (src);
15196   tree dst_addr = build_fold_addr_expr (dst);
15197   tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
15198   vec<tree, va_gc> *params;
15199   /* Expansion of a generic atomic load may require an addition
15200      element, so allocate enough to prevent a resize.  */
15201   vec_alloc (params, 4);
15202 
15203   /* Build __atomic_load (&src, &tmp, SEQ_CST);  */
15204   tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
15205   params->quick_push (src_addr);
15206   params->quick_push (tmp_addr);
15207   params->quick_push (seq_cst);
15208   tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15209 
15210   vec_alloc (params, 4);
15211 
15212   /* Build __atomic_store (&dst, &tmp, SEQ_CST);  */
15213   fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
15214   params->quick_push (dst_addr);
15215   params->quick_push (tmp_addr);
15216   params->quick_push (seq_cst);
15217   tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
15218   return build2 (COMPOUND_EXPR, void_type_node, load, store);
15219 }
15220 
15221 /* Create a transaction node.  */
15222 
15223 tree
c_finish_transaction(location_t loc,tree block,int flags)15224 c_finish_transaction (location_t loc, tree block, int flags)
15225 {
15226   tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
15227   if (flags & TM_STMT_ATTR_OUTER)
15228     TRANSACTION_EXPR_OUTER (stmt) = 1;
15229   if (flags & TM_STMT_ATTR_RELAXED)
15230     TRANSACTION_EXPR_RELAXED (stmt) = 1;
15231   return add_stmt (stmt);
15232 }
15233 
15234 /* Make a variant type in the proper way for C/C++, propagating qualifiers
15235    down to the element type of an array.  If ORIG_QUAL_TYPE is not
15236    NULL, then it should be used as the qualified type
15237    ORIG_QUAL_INDIRECT levels down in array type derivation (to
15238    preserve information about the typedef name from which an array
15239    type was derived).  */
15240 
15241 tree
c_build_qualified_type(tree type,int type_quals,tree orig_qual_type,size_t orig_qual_indirect)15242 c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
15243 			size_t orig_qual_indirect)
15244 {
15245   if (type == error_mark_node)
15246     return type;
15247 
15248   if (TREE_CODE (type) == ARRAY_TYPE)
15249     {
15250       tree t;
15251       tree element_type = c_build_qualified_type (TREE_TYPE (type),
15252 						  type_quals, orig_qual_type,
15253 						  orig_qual_indirect - 1);
15254 
15255       /* See if we already have an identically qualified type.  */
15256       if (orig_qual_type && orig_qual_indirect == 0)
15257 	t = orig_qual_type;
15258       else
15259 	for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
15260 	  {
15261 	    if (TYPE_QUALS (strip_array_types (t)) == type_quals
15262 		&& TYPE_NAME (t) == TYPE_NAME (type)
15263 		&& TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
15264 		&& attribute_list_equal (TYPE_ATTRIBUTES (t),
15265 					 TYPE_ATTRIBUTES (type)))
15266 	      break;
15267 	  }
15268       if (!t)
15269 	{
15270           tree domain = TYPE_DOMAIN (type);
15271 
15272 	  t = build_variant_type_copy (type);
15273 	  TREE_TYPE (t) = element_type;
15274 
15275           if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
15276               || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
15277             SET_TYPE_STRUCTURAL_EQUALITY (t);
15278           else if (TYPE_CANONICAL (element_type) != element_type
15279                    || (domain && TYPE_CANONICAL (domain) != domain))
15280             {
15281               tree unqualified_canon
15282                 = build_array_type (TYPE_CANONICAL (element_type),
15283                                     domain? TYPE_CANONICAL (domain)
15284                                           : NULL_TREE);
15285               if (TYPE_REVERSE_STORAGE_ORDER (type))
15286                 {
15287                   unqualified_canon
15288                     = build_distinct_type_copy (unqualified_canon);
15289                   TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
15290                 }
15291               TYPE_CANONICAL (t)
15292                 = c_build_qualified_type (unqualified_canon, type_quals);
15293             }
15294           else
15295             TYPE_CANONICAL (t) = t;
15296 	}
15297       return t;
15298     }
15299 
15300   /* A restrict-qualified pointer type must be a pointer to object or
15301      incomplete type.  Note that the use of POINTER_TYPE_P also allows
15302      REFERENCE_TYPEs, which is appropriate for C++.  */
15303   if ((type_quals & TYPE_QUAL_RESTRICT)
15304       && (!POINTER_TYPE_P (type)
15305 	  || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
15306     {
15307       error ("invalid use of %<restrict%>");
15308       type_quals &= ~TYPE_QUAL_RESTRICT;
15309     }
15310 
15311   tree var_type = (orig_qual_type && orig_qual_indirect == 0
15312 		   ? orig_qual_type
15313 		   : build_qualified_type (type, type_quals));
15314   /* A variant type does not inherit the list of incomplete vars from the
15315      type main variant.  */
15316   if ((RECORD_OR_UNION_TYPE_P (var_type)
15317        || TREE_CODE (var_type) == ENUMERAL_TYPE)
15318       && TYPE_MAIN_VARIANT (var_type) != var_type)
15319     C_TYPE_INCOMPLETE_VARS (var_type) = 0;
15320   return var_type;
15321 }
15322 
15323 /* Build a VA_ARG_EXPR for the C parser.  */
15324 
15325 tree
c_build_va_arg(location_t loc1,tree expr,location_t loc2,tree type)15326 c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
15327 {
15328   if (error_operand_p (type))
15329     return error_mark_node;
15330   /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
15331      order because it takes the address of the expression.  */
15332   else if (handled_component_p (expr)
15333 	   && reverse_storage_order_for_component_p (expr))
15334     {
15335       error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
15336       return error_mark_node;
15337     }
15338   else if (!COMPLETE_TYPE_P (type))
15339     {
15340       error_at (loc2, "second argument to %<va_arg%> is of incomplete "
15341 		"type %qT", type);
15342       return error_mark_node;
15343     }
15344   else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
15345     warning_at (loc2, OPT_Wc___compat,
15346 		"C++ requires promoted type, not enum type, in %<va_arg%>");
15347   return build_va_arg (loc2, expr, type);
15348 }
15349 
15350 /* Return truthvalue of whether T1 is the same tree structure as T2.
15351    Return 1 if they are the same. Return false if they are different.  */
15352 
15353 bool
c_tree_equal(tree t1,tree t2)15354 c_tree_equal (tree t1, tree t2)
15355 {
15356   enum tree_code code1, code2;
15357 
15358   if (t1 == t2)
15359     return true;
15360   if (!t1 || !t2)
15361     return false;
15362 
15363   for (code1 = TREE_CODE (t1); code1 == NON_LVALUE_EXPR;
15364        code1 = TREE_CODE (t1))
15365     t1 = TREE_OPERAND (t1, 0);
15366   for (code2 = TREE_CODE (t2); code2 == NON_LVALUE_EXPR;
15367        code2 = TREE_CODE (t2))
15368     t2 = TREE_OPERAND (t2, 0);
15369 
15370   /* They might have become equal now.  */
15371   if (t1 == t2)
15372     return true;
15373 
15374   if (code1 != code2)
15375     return false;
15376 
15377   if (CONSTANT_CLASS_P (t1) && !comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
15378     return false;
15379 
15380   switch (code1)
15381     {
15382     case INTEGER_CST:
15383       return wi::to_wide (t1) == wi::to_wide (t2);
15384 
15385     case REAL_CST:
15386       return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
15387 
15388     case STRING_CST:
15389       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
15390 	&& !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
15391 		    TREE_STRING_LENGTH (t1));
15392 
15393     case FIXED_CST:
15394       return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
15395 				     TREE_FIXED_CST (t2));
15396 
15397     case COMPLEX_CST:
15398       return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
15399 	     && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
15400 
15401     case VECTOR_CST:
15402       return operand_equal_p (t1, t2, OEP_ONLY_CONST);
15403 
15404     case CONSTRUCTOR:
15405       /* We need to do this when determining whether or not two
15406 	 non-type pointer to member function template arguments
15407 	 are the same.  */
15408       if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
15409 	  || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
15410 	return false;
15411       {
15412 	tree field, value;
15413 	unsigned int i;
15414 	FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
15415 	  {
15416 	    constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
15417 	    if (!c_tree_equal (field, elt2->index)
15418 		|| !c_tree_equal (value, elt2->value))
15419 	      return false;
15420 	  }
15421       }
15422       return true;
15423 
15424     case TREE_LIST:
15425       if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
15426 	return false;
15427       if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
15428 	return false;
15429       return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
15430 
15431     case SAVE_EXPR:
15432       return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15433 
15434     case CALL_EXPR:
15435       {
15436 	tree arg1, arg2;
15437 	call_expr_arg_iterator iter1, iter2;
15438 	if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
15439 	  return false;
15440 	for (arg1 = first_call_expr_arg (t1, &iter1),
15441 	       arg2 = first_call_expr_arg (t2, &iter2);
15442 	     arg1 && arg2;
15443 	     arg1 = next_call_expr_arg (&iter1),
15444 	       arg2 = next_call_expr_arg (&iter2))
15445 	  if (!c_tree_equal (arg1, arg2))
15446 	    return false;
15447 	if (arg1 || arg2)
15448 	  return false;
15449 	return true;
15450       }
15451 
15452     case TARGET_EXPR:
15453       {
15454 	tree o1 = TREE_OPERAND (t1, 0);
15455 	tree o2 = TREE_OPERAND (t2, 0);
15456 
15457 	/* Special case: if either target is an unallocated VAR_DECL,
15458 	   it means that it's going to be unified with whatever the
15459 	   TARGET_EXPR is really supposed to initialize, so treat it
15460 	   as being equivalent to anything.  */
15461 	if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
15462 	    && !DECL_RTL_SET_P (o1))
15463 	  /*Nop*/;
15464 	else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
15465 		 && !DECL_RTL_SET_P (o2))
15466 	  /*Nop*/;
15467 	else if (!c_tree_equal (o1, o2))
15468 	  return false;
15469 
15470 	return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
15471       }
15472 
15473     case COMPONENT_REF:
15474       if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
15475 	return false;
15476       return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
15477 
15478     case PARM_DECL:
15479     case VAR_DECL:
15480     case CONST_DECL:
15481     case FIELD_DECL:
15482     case FUNCTION_DECL:
15483     case IDENTIFIER_NODE:
15484     case SSA_NAME:
15485       return false;
15486 
15487     case TREE_VEC:
15488       {
15489 	unsigned ix;
15490 	if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
15491 	  return false;
15492 	for (ix = TREE_VEC_LENGTH (t1); ix--;)
15493 	  if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
15494 			     TREE_VEC_ELT (t2, ix)))
15495 	    return false;
15496 	return true;
15497       }
15498 
15499     CASE_CONVERT:
15500       if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2)))
15501 	return false;
15502       break;
15503 
15504     default:
15505       break;
15506     }
15507 
15508   switch (TREE_CODE_CLASS (code1))
15509     {
15510     case tcc_unary:
15511     case tcc_binary:
15512     case tcc_comparison:
15513     case tcc_expression:
15514     case tcc_vl_exp:
15515     case tcc_reference:
15516     case tcc_statement:
15517       {
15518 	int i, n = TREE_OPERAND_LENGTH (t1);
15519 
15520 	switch (code1)
15521 	  {
15522 	  case PREINCREMENT_EXPR:
15523 	  case PREDECREMENT_EXPR:
15524 	  case POSTINCREMENT_EXPR:
15525 	  case POSTDECREMENT_EXPR:
15526 	    n = 1;
15527 	    break;
15528 	  case ARRAY_REF:
15529 	    n = 2;
15530 	    break;
15531 	  default:
15532 	    break;
15533 	  }
15534 
15535 	if (TREE_CODE_CLASS (code1) == tcc_vl_exp
15536 	    && n != TREE_OPERAND_LENGTH (t2))
15537 	  return false;
15538 
15539 	for (i = 0; i < n; ++i)
15540 	  if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
15541 	    return false;
15542 
15543 	return true;
15544       }
15545 
15546     case tcc_type:
15547       return comptypes (t1, t2);
15548     default:
15549       gcc_unreachable ();
15550     }
15551   /* We can get here with --disable-checking.  */
15552   return false;
15553 }
15554 
15555 /* Returns true when the function declaration FNDECL is implicit,
15556    introduced as a result of a call to an otherwise undeclared
15557    function, and false otherwise.  */
15558 
15559 bool
c_decl_implicit(const_tree fndecl)15560 c_decl_implicit (const_tree fndecl)
15561 {
15562   return C_DECL_IMPLICIT (fndecl);
15563 }
15564