1 /* Build expressions with type checking for C compiler.
2    Copyright (C) 1987-2013 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 "tm.h"
30 #include "tree.h"
31 #include "langhooks.h"
32 #include "c-tree.h"
33 #include "c-lang.h"
34 #include "flags.h"
35 #include "intl.h"
36 #include "target.h"
37 #include "tree-iterator.h"
38 #include "bitmap.h"
39 #include "gimple.h"
40 #include "c-family/c-objc.h"
41 #include "c-family/c-common.h"
42 
43 /* Possible cases of implicit bad conversions.  Used to select
44    diagnostic messages in convert_for_assignment.  */
45 enum impl_conv {
46   ic_argpass,
47   ic_assign,
48   ic_init,
49   ic_return
50 };
51 
52 /* The level of nesting inside "__alignof__".  */
53 int in_alignof;
54 
55 /* The level of nesting inside "sizeof".  */
56 int in_sizeof;
57 
58 /* The level of nesting inside "typeof".  */
59 int in_typeof;
60 
61 /* The argument of last parsed sizeof expression, only to be tested
62    if expr.original_code == SIZEOF_EXPR.  */
63 tree c_last_sizeof_arg;
64 
65 /* Nonzero if we've already printed a "missing braces around initializer"
66    message within this initializer.  */
67 static int missing_braces_mentioned;
68 
69 static int require_constant_value;
70 static int require_constant_elements;
71 
72 static bool null_pointer_constant_p (const_tree);
73 static tree qualify_type (tree, tree);
74 static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
75 					 bool *);
76 static int comp_target_types (location_t, tree, tree);
77 static int function_types_compatible_p (const_tree, const_tree, bool *,
78 					bool *);
79 static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
80 static tree lookup_field (tree, tree);
81 static int convert_arguments (tree, vec<tree, va_gc> *, vec<tree, va_gc> *,
82 			      tree, tree);
83 static tree pointer_diff (location_t, tree, tree);
84 static tree convert_for_assignment (location_t, tree, tree, tree,
85 				    enum impl_conv, bool, tree, tree, int);
86 static tree valid_compound_expr_initializer (tree, tree);
87 static void push_string (const char *);
88 static void push_member_name (tree);
89 static int spelling_length (void);
90 static char *print_spelling (char *);
91 static void warning_init (int, const char *);
92 static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
93 static void output_init_element (tree, tree, bool, tree, tree, int, bool,
94 				 struct obstack *);
95 static void output_pending_init_elements (int, struct obstack *);
96 static int set_designator (int, struct obstack *);
97 static void push_range_stack (tree, struct obstack *);
98 static void add_pending_init (tree, tree, tree, bool, struct obstack *);
99 static void set_nonincremental_init (struct obstack *);
100 static void set_nonincremental_init_from_string (tree, struct obstack *);
101 static tree find_init_member (tree, struct obstack *);
102 static void readonly_warning (tree, enum lvalue_use);
103 static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
104 static void record_maybe_used_decl (tree);
105 static int comptypes_internal (const_tree, const_tree, bool *, bool *);
106 
107 /* Return true if EXP is a null pointer constant, false otherwise.  */
108 
109 static bool
null_pointer_constant_p(const_tree expr)110 null_pointer_constant_p (const_tree expr)
111 {
112   /* This should really operate on c_expr structures, but they aren't
113      yet available everywhere required.  */
114   tree type = TREE_TYPE (expr);
115   return (TREE_CODE (expr) == INTEGER_CST
116 	  && !TREE_OVERFLOW (expr)
117 	  && integer_zerop (expr)
118 	  && (INTEGRAL_TYPE_P (type)
119 	      || (TREE_CODE (type) == POINTER_TYPE
120 		  && VOID_TYPE_P (TREE_TYPE (type))
121 		  && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
122 }
123 
124 /* EXPR may appear in an unevaluated part of an integer constant
125    expression, but not in an evaluated part.  Wrap it in a
126    C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
127    INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR.  */
128 
129 static tree
note_integer_operands(tree expr)130 note_integer_operands (tree expr)
131 {
132   tree ret;
133   if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
134     {
135       ret = copy_node (expr);
136       TREE_OVERFLOW (ret) = 1;
137     }
138   else
139     {
140       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
141       C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
142     }
143   return ret;
144 }
145 
146 /* Having checked whether EXPR may appear in an unevaluated part of an
147    integer constant expression and found that it may, remove any
148    C_MAYBE_CONST_EXPR noting this fact and return the resulting
149    expression.  */
150 
151 static inline tree
remove_c_maybe_const_expr(tree expr)152 remove_c_maybe_const_expr (tree expr)
153 {
154   if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
155     return C_MAYBE_CONST_EXPR_EXPR (expr);
156   else
157     return expr;
158 }
159 
160 /* This is a cache to hold if two types are compatible or not.  */
161 
162 struct tagged_tu_seen_cache {
163   const struct tagged_tu_seen_cache * next;
164   const_tree t1;
165   const_tree t2;
166   /* The return value of tagged_types_tu_compatible_p if we had seen
167      these two types already.  */
168   int val;
169 };
170 
171 static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
172 static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
173 
174 /* Do `exp = require_complete_type (exp);' to make sure exp
175    does not have an incomplete type.  (That includes void types.)  */
176 
177 tree
require_complete_type(tree value)178 require_complete_type (tree value)
179 {
180   tree type = TREE_TYPE (value);
181 
182   if (value == error_mark_node || type == error_mark_node)
183     return error_mark_node;
184 
185   /* First, detect a valid value with a complete type.  */
186   if (COMPLETE_TYPE_P (type))
187     return value;
188 
189   c_incomplete_type_error (value, type);
190   return error_mark_node;
191 }
192 
193 /* Print an error message for invalid use of an incomplete type.
194    VALUE is the expression that was used (or 0 if that isn't known)
195    and TYPE is the type that was invalid.  */
196 
197 void
c_incomplete_type_error(const_tree value,const_tree type)198 c_incomplete_type_error (const_tree value, const_tree type)
199 {
200   const char *type_code_string;
201 
202   /* Avoid duplicate error message.  */
203   if (TREE_CODE (type) == ERROR_MARK)
204     return;
205 
206   if (value != 0 && (TREE_CODE (value) == VAR_DECL
207 		     || TREE_CODE (value) == PARM_DECL))
208     error ("%qD has an incomplete type", value);
209   else
210     {
211     retry:
212       /* We must print an error message.  Be clever about what it says.  */
213 
214       switch (TREE_CODE (type))
215 	{
216 	case RECORD_TYPE:
217 	  type_code_string = "struct";
218 	  break;
219 
220 	case UNION_TYPE:
221 	  type_code_string = "union";
222 	  break;
223 
224 	case ENUMERAL_TYPE:
225 	  type_code_string = "enum";
226 	  break;
227 
228 	case VOID_TYPE:
229 	  error ("invalid use of void expression");
230 	  return;
231 
232 	case ARRAY_TYPE:
233 	  if (TYPE_DOMAIN (type))
234 	    {
235 	      if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
236 		{
237 		  error ("invalid use of flexible array member");
238 		  return;
239 		}
240 	      type = TREE_TYPE (type);
241 	      goto retry;
242 	    }
243 	  error ("invalid use of array with unspecified bounds");
244 	  return;
245 
246 	default:
247 	  gcc_unreachable ();
248 	}
249 
250       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
251 	error ("invalid use of undefined type %<%s %E%>",
252 	       type_code_string, TYPE_NAME (type));
253       else
254 	/* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL.  */
255 	error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
256     }
257 }
258 
259 /* Given a type, apply default promotions wrt unnamed function
260    arguments and return the new type.  */
261 
262 tree
c_type_promotes_to(tree type)263 c_type_promotes_to (tree type)
264 {
265   if (TYPE_MAIN_VARIANT (type) == float_type_node)
266     return double_type_node;
267 
268   if (c_promoting_integer_type_p (type))
269     {
270       /* Preserve unsignedness if not really getting any wider.  */
271       if (TYPE_UNSIGNED (type)
272 	  && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
273 	return unsigned_type_node;
274       return integer_type_node;
275     }
276 
277   return type;
278 }
279 
280 /* Return true if between two named address spaces, whether there is a superset
281    named address space that encompasses both address spaces.  If there is a
282    superset, return which address space is the superset.  */
283 
284 static bool
addr_space_superset(addr_space_t as1,addr_space_t as2,addr_space_t * common)285 addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
286 {
287   if (as1 == as2)
288     {
289       *common = as1;
290       return true;
291     }
292   else if (targetm.addr_space.subset_p (as1, as2))
293     {
294       *common = as2;
295       return true;
296     }
297   else if (targetm.addr_space.subset_p (as2, as1))
298     {
299       *common = as1;
300       return true;
301     }
302   else
303     return false;
304 }
305 
306 /* Return a variant of TYPE which has all the type qualifiers of LIKE
307    as well as those of TYPE.  */
308 
309 static tree
qualify_type(tree type,tree like)310 qualify_type (tree type, tree like)
311 {
312   addr_space_t as_type = TYPE_ADDR_SPACE (type);
313   addr_space_t as_like = TYPE_ADDR_SPACE (like);
314   addr_space_t as_common;
315 
316   /* If the two named address spaces are different, determine the common
317      superset address space.  If there isn't one, raise an error.  */
318   if (!addr_space_superset (as_type, as_like, &as_common))
319     {
320       as_common = as_type;
321       error ("%qT and %qT are in disjoint named address spaces",
322 	     type, like);
323     }
324 
325   return c_build_qualified_type (type,
326 				 TYPE_QUALS_NO_ADDR_SPACE (type)
327 				 | TYPE_QUALS_NO_ADDR_SPACE (like)
328 				 | ENCODE_QUAL_ADDR_SPACE (as_common));
329 }
330 
331 /* Return true iff the given tree T is a variable length array.  */
332 
333 bool
c_vla_type_p(const_tree t)334 c_vla_type_p (const_tree t)
335 {
336   if (TREE_CODE (t) == ARRAY_TYPE
337       && C_TYPE_VARIABLE_SIZE (t))
338     return true;
339   return false;
340 }
341 
342 /* Return the composite type of two compatible types.
343 
344    We assume that comptypes has already been done and returned
345    nonzero; if that isn't so, this may crash.  In particular, we
346    assume that qualifiers match.  */
347 
348 tree
composite_type(tree t1,tree t2)349 composite_type (tree t1, tree t2)
350 {
351   enum tree_code code1;
352   enum tree_code code2;
353   tree attributes;
354 
355   /* Save time if the two types are the same.  */
356 
357   if (t1 == t2) return t1;
358 
359   /* If one type is nonsense, use the other.  */
360   if (t1 == error_mark_node)
361     return t2;
362   if (t2 == error_mark_node)
363     return t1;
364 
365   code1 = TREE_CODE (t1);
366   code2 = TREE_CODE (t2);
367 
368   /* Merge the attributes.  */
369   attributes = targetm.merge_type_attributes (t1, t2);
370 
371   /* If one is an enumerated type and the other is the compatible
372      integer type, the composite type might be either of the two
373      (DR#013 question 3).  For consistency, use the enumerated type as
374      the composite type.  */
375 
376   if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
377     return t1;
378   if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
379     return t2;
380 
381   gcc_assert (code1 == code2);
382 
383   switch (code1)
384     {
385     case POINTER_TYPE:
386       /* For two pointers, do this recursively on the target type.  */
387       {
388 	tree pointed_to_1 = TREE_TYPE (t1);
389 	tree pointed_to_2 = TREE_TYPE (t2);
390 	tree target = composite_type (pointed_to_1, pointed_to_2);
391         t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
392 	t1 = build_type_attribute_variant (t1, attributes);
393 	return qualify_type (t1, t2);
394       }
395 
396     case ARRAY_TYPE:
397       {
398 	tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
399 	int quals;
400 	tree unqual_elt;
401 	tree d1 = TYPE_DOMAIN (t1);
402 	tree d2 = TYPE_DOMAIN (t2);
403 	bool d1_variable, d2_variable;
404 	bool d1_zero, d2_zero;
405 	bool t1_complete, t2_complete;
406 
407 	/* We should not have any type quals on arrays at all.  */
408 	gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
409 		    && !TYPE_QUALS_NO_ADDR_SPACE (t2));
410 
411 	t1_complete = COMPLETE_TYPE_P (t1);
412 	t2_complete = COMPLETE_TYPE_P (t2);
413 
414 	d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
415 	d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
416 
417 	d1_variable = (!d1_zero
418 		       && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
419 			   || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
420 	d2_variable = (!d2_zero
421 		       && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
422 			   || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
423 	d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
424 	d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
425 
426 	/* Save space: see if the result is identical to one of the args.  */
427 	if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
428 	    && (d2_variable || d2_zero || !d1_variable))
429 	  return build_type_attribute_variant (t1, attributes);
430 	if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
431 	    && (d1_variable || d1_zero || !d2_variable))
432 	  return build_type_attribute_variant (t2, attributes);
433 
434 	if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
435 	  return build_type_attribute_variant (t1, attributes);
436 	if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
437 	  return build_type_attribute_variant (t2, attributes);
438 
439 	/* Merge the element types, and have a size if either arg has
440 	   one.  We may have qualifiers on the element types.  To set
441 	   up TYPE_MAIN_VARIANT correctly, we need to form the
442 	   composite of the unqualified types and add the qualifiers
443 	   back at the end.  */
444 	quals = TYPE_QUALS (strip_array_types (elt));
445 	unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
446 	t1 = build_array_type (unqual_elt,
447 			       TYPE_DOMAIN ((TYPE_DOMAIN (t1)
448 					     && (d2_variable
449 						 || d2_zero
450 						 || !d1_variable))
451 					    ? t1
452 					    : t2));
453 	/* Ensure a composite type involving a zero-length array type
454 	   is a zero-length type not an incomplete type.  */
455 	if (d1_zero && d2_zero
456 	    && (t1_complete || t2_complete)
457 	    && !COMPLETE_TYPE_P (t1))
458 	  {
459 	    TYPE_SIZE (t1) = bitsize_zero_node;
460 	    TYPE_SIZE_UNIT (t1) = size_zero_node;
461 	  }
462 	t1 = c_build_qualified_type (t1, quals);
463 	return build_type_attribute_variant (t1, attributes);
464       }
465 
466     case ENUMERAL_TYPE:
467     case RECORD_TYPE:
468     case UNION_TYPE:
469       if (attributes != NULL)
470 	{
471 	  /* Try harder not to create a new aggregate type.  */
472 	  if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
473 	    return t1;
474 	  if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
475 	    return t2;
476 	}
477       return build_type_attribute_variant (t1, attributes);
478 
479     case FUNCTION_TYPE:
480       /* Function types: prefer the one that specified arg types.
481 	 If both do, merge the arg types.  Also merge the return types.  */
482       {
483 	tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
484 	tree p1 = TYPE_ARG_TYPES (t1);
485 	tree p2 = TYPE_ARG_TYPES (t2);
486 	int len;
487 	tree newargs, n;
488 	int i;
489 
490 	/* Save space: see if the result is identical to one of the args.  */
491 	if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
492 	  return build_type_attribute_variant (t1, attributes);
493 	if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
494 	  return build_type_attribute_variant (t2, attributes);
495 
496 	/* Simple way if one arg fails to specify argument types.  */
497 	if (TYPE_ARG_TYPES (t1) == 0)
498 	 {
499 	    t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
500 	    t1 = build_type_attribute_variant (t1, attributes);
501 	    return qualify_type (t1, t2);
502 	 }
503 	if (TYPE_ARG_TYPES (t2) == 0)
504 	 {
505 	   t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
506 	   t1 = build_type_attribute_variant (t1, attributes);
507 	   return qualify_type (t1, t2);
508 	 }
509 
510 	/* If both args specify argument types, we must merge the two
511 	   lists, argument by argument.  */
512 
513 	len = list_length (p1);
514 	newargs = 0;
515 
516 	for (i = 0; i < len; i++)
517 	  newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
518 
519 	n = newargs;
520 
521 	for (; p1;
522 	     p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
523 	  {
524 	    /* A null type means arg type is not specified.
525 	       Take whatever the other function type has.  */
526 	    if (TREE_VALUE (p1) == 0)
527 	      {
528 		TREE_VALUE (n) = TREE_VALUE (p2);
529 		goto parm_done;
530 	      }
531 	    if (TREE_VALUE (p2) == 0)
532 	      {
533 		TREE_VALUE (n) = TREE_VALUE (p1);
534 		goto parm_done;
535 	      }
536 
537 	    /* Given  wait (union {union wait *u; int *i} *)
538 	       and  wait (union wait *),
539 	       prefer  union wait *  as type of parm.  */
540 	    if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
541 		&& TREE_VALUE (p1) != TREE_VALUE (p2))
542 	      {
543 		tree memb;
544 		tree mv2 = TREE_VALUE (p2);
545 		if (mv2 && mv2 != error_mark_node
546 		    && TREE_CODE (mv2) != ARRAY_TYPE)
547 		  mv2 = TYPE_MAIN_VARIANT (mv2);
548 		for (memb = TYPE_FIELDS (TREE_VALUE (p1));
549 		     memb; memb = DECL_CHAIN (memb))
550 		  {
551 		    tree mv3 = TREE_TYPE (memb);
552 		    if (mv3 && mv3 != error_mark_node
553 			&& TREE_CODE (mv3) != ARRAY_TYPE)
554 		      mv3 = TYPE_MAIN_VARIANT (mv3);
555 		    if (comptypes (mv3, mv2))
556 		      {
557 			TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
558 							 TREE_VALUE (p2));
559 			pedwarn (input_location, OPT_Wpedantic,
560 				 "function types not truly compatible in ISO C");
561 			goto parm_done;
562 		      }
563 		  }
564 	      }
565 	    if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
566 		&& TREE_VALUE (p2) != TREE_VALUE (p1))
567 	      {
568 		tree memb;
569 		tree mv1 = TREE_VALUE (p1);
570 		if (mv1 && mv1 != error_mark_node
571 		    && TREE_CODE (mv1) != ARRAY_TYPE)
572 		  mv1 = TYPE_MAIN_VARIANT (mv1);
573 		for (memb = TYPE_FIELDS (TREE_VALUE (p2));
574 		     memb; memb = DECL_CHAIN (memb))
575 		  {
576 		    tree mv3 = TREE_TYPE (memb);
577 		    if (mv3 && mv3 != error_mark_node
578 			&& TREE_CODE (mv3) != ARRAY_TYPE)
579 		      mv3 = TYPE_MAIN_VARIANT (mv3);
580 		    if (comptypes (mv3, mv1))
581 		      {
582 			TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
583 							 TREE_VALUE (p1));
584 			pedwarn (input_location, OPT_Wpedantic,
585 				 "function types not truly compatible in ISO C");
586 			goto parm_done;
587 		      }
588 		  }
589 	      }
590 	    TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
591 	  parm_done: ;
592 	  }
593 
594 	t1 = build_function_type (valtype, newargs);
595 	t1 = qualify_type (t1, t2);
596 	/* ... falls through ...  */
597       }
598 
599     default:
600       return build_type_attribute_variant (t1, attributes);
601     }
602 
603 }
604 
605 /* Return the type of a conditional expression between pointers to
606    possibly differently qualified versions of compatible types.
607 
608    We assume that comp_target_types has already been done and returned
609    nonzero; if that isn't so, this may crash.  */
610 
611 static tree
common_pointer_type(tree t1,tree t2)612 common_pointer_type (tree t1, tree t2)
613 {
614   tree attributes;
615   tree pointed_to_1, mv1;
616   tree pointed_to_2, mv2;
617   tree target;
618   unsigned target_quals;
619   addr_space_t as1, as2, as_common;
620   int quals1, quals2;
621 
622   /* Save time if the two types are the same.  */
623 
624   if (t1 == t2) return t1;
625 
626   /* If one type is nonsense, use the other.  */
627   if (t1 == error_mark_node)
628     return t2;
629   if (t2 == error_mark_node)
630     return t1;
631 
632   gcc_assert (TREE_CODE (t1) == POINTER_TYPE
633 	      && TREE_CODE (t2) == POINTER_TYPE);
634 
635   /* Merge the attributes.  */
636   attributes = targetm.merge_type_attributes (t1, t2);
637 
638   /* Find the composite type of the target types, and combine the
639      qualifiers of the two types' targets.  Do not lose qualifiers on
640      array element types by taking the TYPE_MAIN_VARIANT.  */
641   mv1 = pointed_to_1 = TREE_TYPE (t1);
642   mv2 = pointed_to_2 = TREE_TYPE (t2);
643   if (TREE_CODE (mv1) != ARRAY_TYPE)
644     mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
645   if (TREE_CODE (mv2) != ARRAY_TYPE)
646     mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
647   target = composite_type (mv1, mv2);
648 
649   /* For function types do not merge const qualifiers, but drop them
650      if used inconsistently.  The middle-end uses these to mark const
651      and noreturn functions.  */
652   quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
653   quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
654 
655   if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
656     target_quals = (quals1 & quals2);
657   else
658     target_quals = (quals1 | quals2);
659 
660   /* If the two named address spaces are different, determine the common
661      superset address space.  This is guaranteed to exist due to the
662      assumption that comp_target_type returned non-zero.  */
663   as1 = TYPE_ADDR_SPACE (pointed_to_1);
664   as2 = TYPE_ADDR_SPACE (pointed_to_2);
665   if (!addr_space_superset (as1, as2, &as_common))
666     gcc_unreachable ();
667 
668   target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
669 
670   t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
671   return build_type_attribute_variant (t1, attributes);
672 }
673 
674 /* Return the common type for two arithmetic types under the usual
675    arithmetic conversions.  The default conversions have already been
676    applied, and enumerated types converted to their compatible integer
677    types.  The resulting type is unqualified and has no attributes.
678 
679    This is the type for the result of most arithmetic operations
680    if the operands have the given two types.  */
681 
682 static tree
c_common_type(tree t1,tree t2)683 c_common_type (tree t1, tree t2)
684 {
685   enum tree_code code1;
686   enum tree_code code2;
687 
688   /* If one type is nonsense, use the other.  */
689   if (t1 == error_mark_node)
690     return t2;
691   if (t2 == error_mark_node)
692     return t1;
693 
694   if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
695     t1 = TYPE_MAIN_VARIANT (t1);
696 
697   if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
698     t2 = TYPE_MAIN_VARIANT (t2);
699 
700   if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
701     t1 = build_type_attribute_variant (t1, NULL_TREE);
702 
703   if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
704     t2 = build_type_attribute_variant (t2, NULL_TREE);
705 
706   /* Save time if the two types are the same.  */
707 
708   if (t1 == t2) return t1;
709 
710   code1 = TREE_CODE (t1);
711   code2 = TREE_CODE (t2);
712 
713   gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
714 	      || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
715 	      || code1 == INTEGER_TYPE);
716   gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
717 	      || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
718 	      || code2 == INTEGER_TYPE);
719 
720   /* When one operand is a decimal float type, the other operand cannot be
721      a generic float type or a complex type.  We also disallow vector types
722      here.  */
723   if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
724       && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
725     {
726       if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
727 	{
728 	  error ("can%'t mix operands of decimal float and vector types");
729 	  return error_mark_node;
730 	}
731       if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
732 	{
733 	  error ("can%'t mix operands of decimal float and complex types");
734 	  return error_mark_node;
735 	}
736       if (code1 == REAL_TYPE && code2 == REAL_TYPE)
737 	{
738 	  error ("can%'t mix operands of decimal float and other float types");
739 	  return error_mark_node;
740 	}
741     }
742 
743   /* If one type is a vector type, return that type.  (How the usual
744      arithmetic conversions apply to the vector types extension is not
745      precisely specified.)  */
746   if (code1 == VECTOR_TYPE)
747     return t1;
748 
749   if (code2 == VECTOR_TYPE)
750     return t2;
751 
752   /* If one type is complex, form the common type of the non-complex
753      components, then make that complex.  Use T1 or T2 if it is the
754      required type.  */
755   if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
756     {
757       tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
758       tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
759       tree subtype = c_common_type (subtype1, subtype2);
760 
761       if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
762 	return t1;
763       else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
764 	return t2;
765       else
766 	return build_complex_type (subtype);
767     }
768 
769   /* If only one is real, use it as the result.  */
770 
771   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
772     return t1;
773 
774   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
775     return t2;
776 
777   /* If both are real and either are decimal floating point types, use
778      the decimal floating point type with the greater precision. */
779 
780   if (code1 == REAL_TYPE && code2 == REAL_TYPE)
781     {
782       if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
783 	  || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
784 	return dfloat128_type_node;
785       else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
786 	       || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
787 	return dfloat64_type_node;
788       else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
789 	       || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
790 	return dfloat32_type_node;
791     }
792 
793   /* Deal with fixed-point types.  */
794   if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
795     {
796       unsigned int unsignedp = 0, satp = 0;
797       enum machine_mode m1, m2;
798       unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
799 
800       m1 = TYPE_MODE (t1);
801       m2 = TYPE_MODE (t2);
802 
803       /* If one input type is saturating, the result type is saturating.  */
804       if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
805 	satp = 1;
806 
807       /* If both fixed-point types are unsigned, the result type is unsigned.
808 	 When mixing fixed-point and integer types, follow the sign of the
809 	 fixed-point type.
810 	 Otherwise, the result type is signed.  */
811       if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
812 	   && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
813 	  || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
814 	      && TYPE_UNSIGNED (t1))
815 	  || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
816 	      && TYPE_UNSIGNED (t2)))
817 	unsignedp = 1;
818 
819       /* The result type is signed.  */
820       if (unsignedp == 0)
821 	{
822 	  /* If the input type is unsigned, we need to convert to the
823 	     signed type.  */
824 	  if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
825 	    {
826 	      enum mode_class mclass = (enum mode_class) 0;
827 	      if (GET_MODE_CLASS (m1) == MODE_UFRACT)
828 		mclass = MODE_FRACT;
829 	      else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
830 		mclass = MODE_ACCUM;
831 	      else
832 		gcc_unreachable ();
833 	      m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
834 	    }
835 	  if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
836 	    {
837 	      enum mode_class mclass = (enum mode_class) 0;
838 	      if (GET_MODE_CLASS (m2) == MODE_UFRACT)
839 		mclass = MODE_FRACT;
840 	      else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
841 		mclass = MODE_ACCUM;
842 	      else
843 		gcc_unreachable ();
844 	      m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
845 	    }
846 	}
847 
848       if (code1 == FIXED_POINT_TYPE)
849 	{
850 	  fbit1 = GET_MODE_FBIT (m1);
851 	  ibit1 = GET_MODE_IBIT (m1);
852 	}
853       else
854 	{
855 	  fbit1 = 0;
856 	  /* Signed integers need to subtract one sign bit.  */
857 	  ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
858 	}
859 
860       if (code2 == FIXED_POINT_TYPE)
861 	{
862 	  fbit2 = GET_MODE_FBIT (m2);
863 	  ibit2 = GET_MODE_IBIT (m2);
864 	}
865       else
866 	{
867 	  fbit2 = 0;
868 	  /* Signed integers need to subtract one sign bit.  */
869 	  ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
870 	}
871 
872       max_ibit = ibit1 >= ibit2 ?  ibit1 : ibit2;
873       max_fbit = fbit1 >= fbit2 ?  fbit1 : fbit2;
874       return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
875 						 satp);
876     }
877 
878   /* Both real or both integers; use the one with greater precision.  */
879 
880   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
881     return t1;
882   else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
883     return t2;
884 
885   /* Same precision.  Prefer long longs to longs to ints when the
886      same precision, following the C99 rules on integer type rank
887      (which are equivalent to the C90 rules for C90 types).  */
888 
889   if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
890       || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
891     return long_long_unsigned_type_node;
892 
893   if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
894       || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
895     {
896       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
897 	return long_long_unsigned_type_node;
898       else
899 	return long_long_integer_type_node;
900     }
901 
902   if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
903       || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
904     return long_unsigned_type_node;
905 
906   if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
907       || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
908     {
909       /* But preserve unsignedness from the other type,
910 	 since long cannot hold all the values of an unsigned int.  */
911       if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
912 	return long_unsigned_type_node;
913       else
914 	return long_integer_type_node;
915     }
916 
917   /* Likewise, prefer long double to double even if same size.  */
918   if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
919       || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
920     return long_double_type_node;
921 
922   /* Otherwise prefer the unsigned one.  */
923 
924   if (TYPE_UNSIGNED (t1))
925     return t1;
926   else
927     return t2;
928 }
929 
930 /* Wrapper around c_common_type that is used by c-common.c and other
931    front end optimizations that remove promotions.  ENUMERAL_TYPEs
932    are allowed here and are converted to their compatible integer types.
933    BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
934    preferably a non-Boolean type as the common type.  */
935 tree
common_type(tree t1,tree t2)936 common_type (tree t1, tree t2)
937 {
938   if (TREE_CODE (t1) == ENUMERAL_TYPE)
939     t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
940   if (TREE_CODE (t2) == ENUMERAL_TYPE)
941     t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
942 
943   /* If both types are BOOLEAN_TYPE, then return boolean_type_node.  */
944   if (TREE_CODE (t1) == BOOLEAN_TYPE
945       && TREE_CODE (t2) == BOOLEAN_TYPE)
946     return boolean_type_node;
947 
948   /* If either type is BOOLEAN_TYPE, then return the other.  */
949   if (TREE_CODE (t1) == BOOLEAN_TYPE)
950     return t2;
951   if (TREE_CODE (t2) == BOOLEAN_TYPE)
952     return t1;
953 
954   return c_common_type (t1, t2);
955 }
956 
957 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
958    or various other operations.  Return 2 if they are compatible
959    but a warning may be needed if you use them together.  */
960 
961 int
comptypes(tree type1,tree type2)962 comptypes (tree type1, tree type2)
963 {
964   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
965   int val;
966 
967   val = comptypes_internal (type1, type2, NULL, NULL);
968   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
969 
970   return val;
971 }
972 
973 /* Like comptypes, but if it returns non-zero because enum and int are
974    compatible, it sets *ENUM_AND_INT_P to true.  */
975 
976 static int
comptypes_check_enum_int(tree type1,tree type2,bool * enum_and_int_p)977 comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
978 {
979   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
980   int val;
981 
982   val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
983   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
984 
985   return val;
986 }
987 
988 /* Like comptypes, but if it returns nonzero for different types, it
989    sets *DIFFERENT_TYPES_P to true.  */
990 
991 int
comptypes_check_different_types(tree type1,tree type2,bool * different_types_p)992 comptypes_check_different_types (tree type1, tree type2,
993 				 bool *different_types_p)
994 {
995   const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
996   int val;
997 
998   val = comptypes_internal (type1, type2, NULL, different_types_p);
999   free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1000 
1001   return val;
1002 }
1003 
1004 /* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1005    or various other operations.  Return 2 if they are compatible
1006    but a warning may be needed if you use them together.  If
1007    ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1008    compatible integer type, then this sets *ENUM_AND_INT_P to true;
1009    *ENUM_AND_INT_P is never set to false.  If DIFFERENT_TYPES_P is not
1010    NULL, and the types are compatible but different enough not to be
1011    permitted in C11 typedef redeclarations, then this sets
1012    *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1013    false, but may or may not be set if the types are incompatible.
1014    This differs from comptypes, in that we don't free the seen
1015    types.  */
1016 
1017 static int
comptypes_internal(const_tree type1,const_tree type2,bool * enum_and_int_p,bool * different_types_p)1018 comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1019 		    bool *different_types_p)
1020 {
1021   const_tree t1 = type1;
1022   const_tree t2 = type2;
1023   int attrval, val;
1024 
1025   /* Suppress errors caused by previously reported errors.  */
1026 
1027   if (t1 == t2 || !t1 || !t2
1028       || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
1029     return 1;
1030 
1031   /* Enumerated types are compatible with integer types, but this is
1032      not transitive: two enumerated types in the same translation unit
1033      are compatible with each other only if they are the same type.  */
1034 
1035   if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
1036     {
1037       t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
1038       if (TREE_CODE (t2) != VOID_TYPE)
1039 	{
1040 	  if (enum_and_int_p != NULL)
1041 	    *enum_and_int_p = true;
1042 	  if (different_types_p != NULL)
1043 	    *different_types_p = true;
1044 	}
1045     }
1046   else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
1047     {
1048       t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
1049       if (TREE_CODE (t1) != VOID_TYPE)
1050 	{
1051 	  if (enum_and_int_p != NULL)
1052 	    *enum_and_int_p = true;
1053 	  if (different_types_p != NULL)
1054 	    *different_types_p = true;
1055 	}
1056     }
1057 
1058   if (t1 == t2)
1059     return 1;
1060 
1061   /* Different classes of types can't be compatible.  */
1062 
1063   if (TREE_CODE (t1) != TREE_CODE (t2))
1064     return 0;
1065 
1066   /* Qualifiers must match. C99 6.7.3p9 */
1067 
1068   if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
1069     return 0;
1070 
1071   /* Allow for two different type nodes which have essentially the same
1072      definition.  Note that we already checked for equality of the type
1073      qualifiers (just above).  */
1074 
1075   if (TREE_CODE (t1) != ARRAY_TYPE
1076       && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1077     return 1;
1078 
1079   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1080   if (!(attrval = comp_type_attributes (t1, t2)))
1081      return 0;
1082 
1083   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1084   val = 0;
1085 
1086   switch (TREE_CODE (t1))
1087     {
1088     case POINTER_TYPE:
1089       /* Do not remove mode or aliasing information.  */
1090       if (TYPE_MODE (t1) != TYPE_MODE (t2)
1091 	  || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1092 	break;
1093       val = (TREE_TYPE (t1) == TREE_TYPE (t2)
1094 	     ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1095 				       enum_and_int_p, different_types_p));
1096       break;
1097 
1098     case FUNCTION_TYPE:
1099       val = function_types_compatible_p (t1, t2, enum_and_int_p,
1100 					 different_types_p);
1101       break;
1102 
1103     case ARRAY_TYPE:
1104       {
1105 	tree d1 = TYPE_DOMAIN (t1);
1106 	tree d2 = TYPE_DOMAIN (t2);
1107 	bool d1_variable, d2_variable;
1108 	bool d1_zero, d2_zero;
1109 	val = 1;
1110 
1111 	/* Target types must match incl. qualifiers.  */
1112 	if (TREE_TYPE (t1) != TREE_TYPE (t2)
1113 	    && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1114 					       enum_and_int_p,
1115 					       different_types_p)))
1116 	  return 0;
1117 
1118 	if (different_types_p != NULL
1119 	    && (d1 == 0) != (d2 == 0))
1120 	  *different_types_p = true;
1121 	/* Sizes must match unless one is missing or variable.  */
1122 	if (d1 == 0 || d2 == 0 || d1 == d2)
1123 	  break;
1124 
1125 	d1_zero = !TYPE_MAX_VALUE (d1);
1126 	d2_zero = !TYPE_MAX_VALUE (d2);
1127 
1128 	d1_variable = (!d1_zero
1129 		       && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1130 			   || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
1131 	d2_variable = (!d2_zero
1132 		       && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1133 			   || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
1134 	d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1135 	d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
1136 
1137 	if (different_types_p != NULL
1138 	    && d1_variable != d2_variable)
1139 	  *different_types_p = true;
1140 	if (d1_variable || d2_variable)
1141 	  break;
1142 	if (d1_zero && d2_zero)
1143 	  break;
1144 	if (d1_zero || d2_zero
1145 	    || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1146 	    || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
1147 	  val = 0;
1148 
1149 	break;
1150       }
1151 
1152     case ENUMERAL_TYPE:
1153     case RECORD_TYPE:
1154     case UNION_TYPE:
1155       if (val != 1 && !same_translation_unit_p (t1, t2))
1156 	{
1157 	  tree a1 = TYPE_ATTRIBUTES (t1);
1158 	  tree a2 = TYPE_ATTRIBUTES (t2);
1159 
1160 	  if (! attribute_list_contained (a1, a2)
1161 	      && ! attribute_list_contained (a2, a1))
1162 	    break;
1163 
1164 	  if (attrval != 2)
1165 	    return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1166 						 different_types_p);
1167 	  val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1168 					      different_types_p);
1169 	}
1170       break;
1171 
1172     case VECTOR_TYPE:
1173       val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1174 	     && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1175 				    enum_and_int_p, different_types_p));
1176       break;
1177 
1178     default:
1179       break;
1180     }
1181   return attrval == 2 && val == 1 ? 2 : val;
1182 }
1183 
1184 /* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1185    their qualifiers, except for named address spaces.  If the pointers point to
1186    different named addresses, then we must determine if one address space is a
1187    subset of the other.  */
1188 
1189 static int
comp_target_types(location_t location,tree ttl,tree ttr)1190 comp_target_types (location_t location, tree ttl, tree ttr)
1191 {
1192   int val;
1193   tree mvl = TREE_TYPE (ttl);
1194   tree mvr = TREE_TYPE (ttr);
1195   addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1196   addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1197   addr_space_t as_common;
1198   bool enum_and_int_p;
1199 
1200   /* Fail if pointers point to incompatible address spaces.  */
1201   if (!addr_space_superset (asl, asr, &as_common))
1202     return 0;
1203 
1204   /* Do not lose qualifiers on element types of array types that are
1205      pointer targets by taking their TYPE_MAIN_VARIANT.  */
1206   if (TREE_CODE (mvl) != ARRAY_TYPE)
1207     mvl = TYPE_MAIN_VARIANT (mvl);
1208   if (TREE_CODE (mvr) != ARRAY_TYPE)
1209     mvr = TYPE_MAIN_VARIANT (mvr);
1210   enum_and_int_p = false;
1211   val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
1212 
1213   if (val == 2)
1214     pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
1215 
1216   if (val == 1 && enum_and_int_p && warn_cxx_compat)
1217     warning_at (location, OPT_Wc___compat,
1218 		"pointer target types incompatible in C++");
1219 
1220   return val;
1221 }
1222 
1223 /* Subroutines of `comptypes'.  */
1224 
1225 /* Determine whether two trees derive from the same translation unit.
1226    If the CONTEXT chain ends in a null, that tree's context is still
1227    being parsed, so if two trees have context chains ending in null,
1228    they're in the same translation unit.  */
1229 int
same_translation_unit_p(const_tree t1,const_tree t2)1230 same_translation_unit_p (const_tree t1, const_tree t2)
1231 {
1232   while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1233     switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1234       {
1235       case tcc_declaration:
1236 	t1 = DECL_CONTEXT (t1); break;
1237       case tcc_type:
1238 	t1 = TYPE_CONTEXT (t1); break;
1239       case tcc_exceptional:
1240 	t1 = BLOCK_SUPERCONTEXT (t1); break;  /* assume block */
1241       default: gcc_unreachable ();
1242       }
1243 
1244   while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1245     switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1246       {
1247       case tcc_declaration:
1248 	t2 = DECL_CONTEXT (t2); break;
1249       case tcc_type:
1250 	t2 = TYPE_CONTEXT (t2); break;
1251       case tcc_exceptional:
1252 	t2 = BLOCK_SUPERCONTEXT (t2); break;  /* assume block */
1253       default: gcc_unreachable ();
1254       }
1255 
1256   return t1 == t2;
1257 }
1258 
1259 /* Allocate the seen two types, assuming that they are compatible. */
1260 
1261 static struct tagged_tu_seen_cache *
alloc_tagged_tu_seen_cache(const_tree t1,const_tree t2)1262 alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
1263 {
1264   struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
1265   tu->next = tagged_tu_seen_base;
1266   tu->t1 = t1;
1267   tu->t2 = t2;
1268 
1269   tagged_tu_seen_base = tu;
1270 
1271   /* The C standard says that two structures in different translation
1272      units are compatible with each other only if the types of their
1273      fields are compatible (among other things).  We assume that they
1274      are compatible until proven otherwise when building the cache.
1275      An example where this can occur is:
1276      struct a
1277      {
1278        struct a *next;
1279      };
1280      If we are comparing this against a similar struct in another TU,
1281      and did not assume they were compatible, we end up with an infinite
1282      loop.  */
1283   tu->val = 1;
1284   return tu;
1285 }
1286 
1287 /* Free the seen types until we get to TU_TIL. */
1288 
1289 static void
free_all_tagged_tu_seen_up_to(const struct tagged_tu_seen_cache * tu_til)1290 free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1291 {
1292   const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1293   while (tu != tu_til)
1294     {
1295       const struct tagged_tu_seen_cache *const tu1
1296 	= (const struct tagged_tu_seen_cache *) tu;
1297       tu = tu1->next;
1298       free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
1299     }
1300   tagged_tu_seen_base = tu_til;
1301 }
1302 
1303 /* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1304    compatible.  If the two types are not the same (which has been
1305    checked earlier), this can only happen when multiple translation
1306    units are being compiled.  See C99 6.2.7 paragraph 1 for the exact
1307    rules.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1308    comptypes_internal.  */
1309 
1310 static int
tagged_types_tu_compatible_p(const_tree t1,const_tree t2,bool * enum_and_int_p,bool * different_types_p)1311 tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
1312 			      bool *enum_and_int_p, bool *different_types_p)
1313 {
1314   tree s1, s2;
1315   bool needs_warning = false;
1316 
1317   /* We have to verify that the tags of the types are the same.  This
1318      is harder than it looks because this may be a typedef, so we have
1319      to go look at the original type.  It may even be a typedef of a
1320      typedef...
1321      In the case of compiler-created builtin structs the TYPE_DECL
1322      may be a dummy, with no DECL_ORIGINAL_TYPE.  Don't fault.  */
1323   while (TYPE_NAME (t1)
1324 	 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1325 	 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
1326     t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1327 
1328   while (TYPE_NAME (t2)
1329 	 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1330 	 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
1331     t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1332 
1333   /* C90 didn't have the requirement that the two tags be the same.  */
1334   if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1335     return 0;
1336 
1337   /* C90 didn't say what happened if one or both of the types were
1338      incomplete; we choose to follow C99 rules here, which is that they
1339      are compatible.  */
1340   if (TYPE_SIZE (t1) == NULL
1341       || TYPE_SIZE (t2) == NULL)
1342     return 1;
1343 
1344   {
1345     const struct tagged_tu_seen_cache * tts_i;
1346     for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1347       if (tts_i->t1 == t1 && tts_i->t2 == t2)
1348 	return tts_i->val;
1349   }
1350 
1351   switch (TREE_CODE (t1))
1352     {
1353     case ENUMERAL_TYPE:
1354       {
1355 	struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1356 	/* Speed up the case where the type values are in the same order.  */
1357 	tree tv1 = TYPE_VALUES (t1);
1358 	tree tv2 = TYPE_VALUES (t2);
1359 
1360 	if (tv1 == tv2)
1361 	  {
1362 	    return 1;
1363 	  }
1364 
1365 	for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1366 	  {
1367 	    if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1368 	      break;
1369 	    if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
1370 	      {
1371 		tu->val = 0;
1372 		return 0;
1373 	      }
1374 	  }
1375 
1376 	if (tv1 == NULL_TREE && tv2 == NULL_TREE)
1377 	  {
1378 	    return 1;
1379 	  }
1380 	if (tv1 == NULL_TREE || tv2 == NULL_TREE)
1381 	  {
1382 	    tu->val = 0;
1383 	    return 0;
1384 	  }
1385 
1386 	if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
1387 	  {
1388 	    tu->val = 0;
1389 	    return 0;
1390 	  }
1391 
1392 	for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1393 	  {
1394 	    s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1395 	    if (s2 == NULL
1396 		|| simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
1397 	      {
1398 		tu->val = 0;
1399 		return 0;
1400 	      }
1401 	  }
1402 	return 1;
1403       }
1404 
1405     case UNION_TYPE:
1406       {
1407 	struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1408 	if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
1409 	  {
1410 	    tu->val = 0;
1411 	    return 0;
1412 	  }
1413 
1414 	/*  Speed up the common case where the fields are in the same order. */
1415 	for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1416 	     s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1417 	  {
1418 	    int result;
1419 
1420 	    if (DECL_NAME (s1) != DECL_NAME (s2))
1421 	      break;
1422 	    result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1423 					 enum_and_int_p, different_types_p);
1424 
1425 	    if (result != 1 && !DECL_NAME (s1))
1426 	      break;
1427 	    if (result == 0)
1428 	      {
1429 		tu->val = 0;
1430 		return 0;
1431 	      }
1432 	    if (result == 2)
1433 	      needs_warning = true;
1434 
1435 	    if (TREE_CODE (s1) == FIELD_DECL
1436 		&& simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1437 				     DECL_FIELD_BIT_OFFSET (s2)) != 1)
1438 	      {
1439 		tu->val = 0;
1440 		return 0;
1441 	      }
1442 	  }
1443 	if (!s1 && !s2)
1444 	  {
1445 	    tu->val = needs_warning ? 2 : 1;
1446 	    return tu->val;
1447 	  }
1448 
1449 	for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
1450 	  {
1451 	    bool ok = false;
1452 
1453 	    for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
1454 	      if (DECL_NAME (s1) == DECL_NAME (s2))
1455 		{
1456 		  int result;
1457 
1458 		  result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1459 					       enum_and_int_p,
1460 					       different_types_p);
1461 
1462 		  if (result != 1 && !DECL_NAME (s1))
1463 		    continue;
1464 		  if (result == 0)
1465 		    {
1466 		      tu->val = 0;
1467 		      return 0;
1468 		    }
1469 		  if (result == 2)
1470 		    needs_warning = true;
1471 
1472 		  if (TREE_CODE (s1) == FIELD_DECL
1473 		      && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1474 					   DECL_FIELD_BIT_OFFSET (s2)) != 1)
1475 		    break;
1476 
1477 		  ok = true;
1478 		  break;
1479 		}
1480 	    if (!ok)
1481 	      {
1482 		tu->val = 0;
1483 		return 0;
1484 	      }
1485 	  }
1486 	tu->val = needs_warning ? 2 : 10;
1487 	return tu->val;
1488       }
1489 
1490     case RECORD_TYPE:
1491       {
1492 	struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
1493 
1494 	for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
1495 	     s1 && s2;
1496 	     s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
1497 	  {
1498 	    int result;
1499 	    if (TREE_CODE (s1) != TREE_CODE (s2)
1500 		|| DECL_NAME (s1) != DECL_NAME (s2))
1501 	      break;
1502 	    result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
1503 					 enum_and_int_p, different_types_p);
1504 	    if (result == 0)
1505 	      break;
1506 	    if (result == 2)
1507 	      needs_warning = true;
1508 
1509 	    if (TREE_CODE (s1) == FIELD_DECL
1510 		&& simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1511 				     DECL_FIELD_BIT_OFFSET (s2)) != 1)
1512 	      break;
1513 	  }
1514 	if (s1 && s2)
1515 	  tu->val = 0;
1516 	else
1517 	  tu->val = needs_warning ? 2 : 1;
1518 	return tu->val;
1519       }
1520 
1521     default:
1522       gcc_unreachable ();
1523     }
1524 }
1525 
1526 /* Return 1 if two function types F1 and F2 are compatible.
1527    If either type specifies no argument types,
1528    the other must specify a fixed number of self-promoting arg types.
1529    Otherwise, if one type specifies only the number of arguments,
1530    the other must specify that number of self-promoting arg types.
1531    Otherwise, the argument types must match.
1532    ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal.  */
1533 
1534 static int
function_types_compatible_p(const_tree f1,const_tree f2,bool * enum_and_int_p,bool * different_types_p)1535 function_types_compatible_p (const_tree f1, const_tree f2,
1536 			     bool *enum_and_int_p, bool *different_types_p)
1537 {
1538   tree args1, args2;
1539   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1540   int val = 1;
1541   int val1;
1542   tree ret1, ret2;
1543 
1544   ret1 = TREE_TYPE (f1);
1545   ret2 = TREE_TYPE (f2);
1546 
1547   /* 'volatile' qualifiers on a function's return type used to mean
1548      the function is noreturn.  */
1549   if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
1550     pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
1551   if (TYPE_VOLATILE (ret1))
1552     ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1553 				 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1554   if (TYPE_VOLATILE (ret2))
1555     ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1556 				 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
1557   val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
1558   if (val == 0)
1559     return 0;
1560 
1561   args1 = TYPE_ARG_TYPES (f1);
1562   args2 = TYPE_ARG_TYPES (f2);
1563 
1564   if (different_types_p != NULL
1565       && (args1 == 0) != (args2 == 0))
1566     *different_types_p = true;
1567 
1568   /* An unspecified parmlist matches any specified parmlist
1569      whose argument types don't need default promotions.  */
1570 
1571   if (args1 == 0)
1572     {
1573       if (!self_promoting_args_p (args2))
1574 	return 0;
1575       /* If one of these types comes from a non-prototype fn definition,
1576 	 compare that with the other type's arglist.
1577 	 If they don't match, ask for a warning (but no error).  */
1578       if (TYPE_ACTUAL_ARG_TYPES (f1)
1579 	  && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1580 					   enum_and_int_p, different_types_p))
1581 	val = 2;
1582       return val;
1583     }
1584   if (args2 == 0)
1585     {
1586       if (!self_promoting_args_p (args1))
1587 	return 0;
1588       if (TYPE_ACTUAL_ARG_TYPES (f2)
1589 	  && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1590 					   enum_and_int_p, different_types_p))
1591 	val = 2;
1592       return val;
1593     }
1594 
1595   /* Both types have argument lists: compare them and propagate results.  */
1596   val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1597 				  different_types_p);
1598   return val1 != 1 ? val1 : val;
1599 }
1600 
1601 /* Check two lists of types for compatibility, returning 0 for
1602    incompatible, 1 for compatible, or 2 for compatible with
1603    warning.  ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1604    comptypes_internal.  */
1605 
1606 static int
type_lists_compatible_p(const_tree args1,const_tree args2,bool * enum_and_int_p,bool * different_types_p)1607 type_lists_compatible_p (const_tree args1, const_tree args2,
1608 			 bool *enum_and_int_p, bool *different_types_p)
1609 {
1610   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
1611   int val = 1;
1612   int newval = 0;
1613 
1614   while (1)
1615     {
1616       tree a1, mv1, a2, mv2;
1617       if (args1 == 0 && args2 == 0)
1618 	return val;
1619       /* If one list is shorter than the other,
1620 	 they fail to match.  */
1621       if (args1 == 0 || args2 == 0)
1622 	return 0;
1623       mv1 = a1 = TREE_VALUE (args1);
1624       mv2 = a2 = TREE_VALUE (args2);
1625       if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1626 	mv1 = TYPE_MAIN_VARIANT (mv1);
1627       if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1628 	mv2 = TYPE_MAIN_VARIANT (mv2);
1629       /* A null pointer instead of a type
1630 	 means there is supposed to be an argument
1631 	 but nothing is specified about what type it has.
1632 	 So match anything that self-promotes.  */
1633       if (different_types_p != NULL
1634 	  && (a1 == 0) != (a2 == 0))
1635 	*different_types_p = true;
1636       if (a1 == 0)
1637 	{
1638 	  if (c_type_promotes_to (a2) != a2)
1639 	    return 0;
1640 	}
1641       else if (a2 == 0)
1642 	{
1643 	  if (c_type_promotes_to (a1) != a1)
1644 	    return 0;
1645 	}
1646       /* If one of the lists has an error marker, ignore this arg.  */
1647       else if (TREE_CODE (a1) == ERROR_MARK
1648 	       || TREE_CODE (a2) == ERROR_MARK)
1649 	;
1650       else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1651 					      different_types_p)))
1652 	{
1653 	  if (different_types_p != NULL)
1654 	    *different_types_p = true;
1655 	  /* Allow  wait (union {union wait *u; int *i} *)
1656 	     and  wait (union wait *)  to be compatible.  */
1657 	  if (TREE_CODE (a1) == UNION_TYPE
1658 	      && (TYPE_NAME (a1) == 0
1659 		  || TYPE_TRANSPARENT_AGGR (a1))
1660 	      && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1661 	      && tree_int_cst_equal (TYPE_SIZE (a1),
1662 				     TYPE_SIZE (a2)))
1663 	    {
1664 	      tree memb;
1665 	      for (memb = TYPE_FIELDS (a1);
1666 		   memb; memb = DECL_CHAIN (memb))
1667 		{
1668 		  tree mv3 = TREE_TYPE (memb);
1669 		  if (mv3 && mv3 != error_mark_node
1670 		      && TREE_CODE (mv3) != ARRAY_TYPE)
1671 		    mv3 = TYPE_MAIN_VARIANT (mv3);
1672 		  if (comptypes_internal (mv3, mv2, enum_and_int_p,
1673 					  different_types_p))
1674 		    break;
1675 		}
1676 	      if (memb == 0)
1677 		return 0;
1678 	    }
1679 	  else if (TREE_CODE (a2) == UNION_TYPE
1680 		   && (TYPE_NAME (a2) == 0
1681 		       || TYPE_TRANSPARENT_AGGR (a2))
1682 		   && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1683 		   && tree_int_cst_equal (TYPE_SIZE (a2),
1684 					  TYPE_SIZE (a1)))
1685 	    {
1686 	      tree memb;
1687 	      for (memb = TYPE_FIELDS (a2);
1688 		   memb; memb = DECL_CHAIN (memb))
1689 		{
1690 		  tree mv3 = TREE_TYPE (memb);
1691 		  if (mv3 && mv3 != error_mark_node
1692 		      && TREE_CODE (mv3) != ARRAY_TYPE)
1693 		    mv3 = TYPE_MAIN_VARIANT (mv3);
1694 		  if (comptypes_internal (mv3, mv1, enum_and_int_p,
1695 					  different_types_p))
1696 		    break;
1697 		}
1698 	      if (memb == 0)
1699 		return 0;
1700 	    }
1701 	  else
1702 	    return 0;
1703 	}
1704 
1705       /* comptypes said ok, but record if it said to warn.  */
1706       if (newval > val)
1707 	val = newval;
1708 
1709       args1 = TREE_CHAIN (args1);
1710       args2 = TREE_CHAIN (args2);
1711     }
1712 }
1713 
1714 /* Compute the size to increment a pointer by.  */
1715 
1716 static tree
c_size_in_bytes(const_tree type)1717 c_size_in_bytes (const_tree type)
1718 {
1719   enum tree_code code = TREE_CODE (type);
1720 
1721   if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1722     return size_one_node;
1723 
1724   if (!COMPLETE_OR_VOID_TYPE_P (type))
1725     {
1726       error ("arithmetic on pointer to an incomplete type");
1727       return size_one_node;
1728     }
1729 
1730   /* Convert in case a char is more than one unit.  */
1731   return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1732 			 size_int (TYPE_PRECISION (char_type_node)
1733 				   / BITS_PER_UNIT));
1734 }
1735 
1736 /* Return either DECL or its known constant value (if it has one).  */
1737 
1738 tree
decl_constant_value(tree decl)1739 decl_constant_value (tree decl)
1740 {
1741   if (/* Don't change a variable array bound or initial value to a constant
1742 	 in a place where a variable is invalid.  Note that DECL_INITIAL
1743 	 isn't valid for a PARM_DECL.  */
1744       current_function_decl != 0
1745       && TREE_CODE (decl) != PARM_DECL
1746       && !TREE_THIS_VOLATILE (decl)
1747       && TREE_READONLY (decl)
1748       && DECL_INITIAL (decl) != 0
1749       && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1750       /* This is invalid if initial value is not constant.
1751 	 If it has either a function call, a memory reference,
1752 	 or a variable, then re-evaluating it could give different results.  */
1753       && TREE_CONSTANT (DECL_INITIAL (decl))
1754       /* Check for cases where this is sub-optimal, even though valid.  */
1755       && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1756     return DECL_INITIAL (decl);
1757   return decl;
1758 }
1759 
1760 /* Convert the array expression EXP to a pointer.  */
1761 static tree
array_to_pointer_conversion(location_t loc,tree exp)1762 array_to_pointer_conversion (location_t loc, tree exp)
1763 {
1764   tree orig_exp = exp;
1765   tree type = TREE_TYPE (exp);
1766   tree adr;
1767   tree restype = TREE_TYPE (type);
1768   tree ptrtype;
1769 
1770   gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
1771 
1772   STRIP_TYPE_NOPS (exp);
1773 
1774   if (TREE_NO_WARNING (orig_exp))
1775     TREE_NO_WARNING (exp) = 1;
1776 
1777   ptrtype = build_pointer_type (restype);
1778 
1779   if (TREE_CODE (exp) == INDIRECT_REF)
1780     return convert (ptrtype, TREE_OPERAND (exp, 0));
1781 
1782   /* In C++ array compound literals are temporary objects unless they are
1783      const or appear in namespace scope, so they are destroyed too soon
1784      to use them for much of anything  (c++/53220).  */
1785   if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1786     {
1787       tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1788       if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1789 	warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1790 		    "converting an array compound literal to a pointer "
1791 		    "is ill-formed in C++");
1792     }
1793 
1794   adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
1795   return convert (ptrtype, adr);
1796 }
1797 
1798 /* Convert the function expression EXP to a pointer.  */
1799 static tree
function_to_pointer_conversion(location_t loc,tree exp)1800 function_to_pointer_conversion (location_t loc, tree exp)
1801 {
1802   tree orig_exp = exp;
1803 
1804   gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
1805 
1806   STRIP_TYPE_NOPS (exp);
1807 
1808   if (TREE_NO_WARNING (orig_exp))
1809     TREE_NO_WARNING (exp) = 1;
1810 
1811   return build_unary_op (loc, ADDR_EXPR, exp, 0);
1812 }
1813 
1814 /* Mark EXP as read, not just set, for set but not used -Wunused
1815    warning purposes.  */
1816 
1817 void
mark_exp_read(tree exp)1818 mark_exp_read (tree exp)
1819 {
1820   switch (TREE_CODE (exp))
1821     {
1822     case VAR_DECL:
1823     case PARM_DECL:
1824       DECL_READ_P (exp) = 1;
1825       break;
1826     case ARRAY_REF:
1827     case COMPONENT_REF:
1828     case MODIFY_EXPR:
1829     case REALPART_EXPR:
1830     case IMAGPART_EXPR:
1831     CASE_CONVERT:
1832     case ADDR_EXPR:
1833       mark_exp_read (TREE_OPERAND (exp, 0));
1834       break;
1835     case COMPOUND_EXPR:
1836     case C_MAYBE_CONST_EXPR:
1837       mark_exp_read (TREE_OPERAND (exp, 1));
1838       break;
1839     default:
1840       break;
1841     }
1842 }
1843 
1844 /* Perform the default conversion of arrays and functions to pointers.
1845    Return the result of converting EXP.  For any other expression, just
1846    return EXP.
1847 
1848    LOC is the location of the expression.  */
1849 
1850 struct c_expr
default_function_array_conversion(location_t loc,struct c_expr exp)1851 default_function_array_conversion (location_t loc, struct c_expr exp)
1852 {
1853   tree orig_exp = exp.value;
1854   tree type = TREE_TYPE (exp.value);
1855   enum tree_code code = TREE_CODE (type);
1856 
1857   switch (code)
1858     {
1859     case ARRAY_TYPE:
1860       {
1861 	bool not_lvalue = false;
1862 	bool lvalue_array_p;
1863 
1864 	while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1865 		|| CONVERT_EXPR_P (exp.value))
1866 	       && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1867 	  {
1868 	    if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1869 	      not_lvalue = true;
1870 	    exp.value = TREE_OPERAND (exp.value, 0);
1871 	  }
1872 
1873 	if (TREE_NO_WARNING (orig_exp))
1874 	  TREE_NO_WARNING (exp.value) = 1;
1875 
1876 	lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1877 	if (!flag_isoc99 && !lvalue_array_p)
1878 	  {
1879 	    /* Before C99, non-lvalue arrays do not decay to pointers.
1880 	       Normally, using such an array would be invalid; but it can
1881 	       be used correctly inside sizeof or as a statement expression.
1882 	       Thus, do not give an error here; an error will result later.  */
1883 	    return exp;
1884 	  }
1885 
1886 	exp.value = array_to_pointer_conversion (loc, exp.value);
1887       }
1888       break;
1889     case FUNCTION_TYPE:
1890       exp.value = function_to_pointer_conversion (loc, exp.value);
1891       break;
1892     default:
1893       break;
1894     }
1895 
1896   return exp;
1897 }
1898 
1899 struct c_expr
default_function_array_read_conversion(location_t loc,struct c_expr exp)1900 default_function_array_read_conversion (location_t loc, struct c_expr exp)
1901 {
1902   mark_exp_read (exp.value);
1903   return default_function_array_conversion (loc, exp);
1904 }
1905 
1906 /* EXP is an expression of integer type.  Apply the integer promotions
1907    to it and return the promoted value.  */
1908 
1909 tree
perform_integral_promotions(tree exp)1910 perform_integral_promotions (tree exp)
1911 {
1912   tree type = TREE_TYPE (exp);
1913   enum tree_code code = TREE_CODE (type);
1914 
1915   gcc_assert (INTEGRAL_TYPE_P (type));
1916 
1917   /* Normally convert enums to int,
1918      but convert wide enums to something wider.  */
1919   if (code == ENUMERAL_TYPE)
1920     {
1921       type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1922 					  TYPE_PRECISION (integer_type_node)),
1923 				     ((TYPE_PRECISION (type)
1924 				       >= TYPE_PRECISION (integer_type_node))
1925 				      && TYPE_UNSIGNED (type)));
1926 
1927       return convert (type, exp);
1928     }
1929 
1930   /* ??? This should no longer be needed now bit-fields have their
1931      proper types.  */
1932   if (TREE_CODE (exp) == COMPONENT_REF
1933       && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
1934       /* If it's thinner than an int, promote it like a
1935 	 c_promoting_integer_type_p, otherwise leave it alone.  */
1936       && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1937 			       TYPE_PRECISION (integer_type_node)))
1938     return convert (integer_type_node, exp);
1939 
1940   if (c_promoting_integer_type_p (type))
1941     {
1942       /* Preserve unsignedness if not really getting any wider.  */
1943       if (TYPE_UNSIGNED (type)
1944 	  && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1945 	return convert (unsigned_type_node, exp);
1946 
1947       return convert (integer_type_node, exp);
1948     }
1949 
1950   return exp;
1951 }
1952 
1953 
1954 /* Perform default promotions for C data used in expressions.
1955    Enumeral types or short or char are converted to int.
1956    In addition, manifest constants symbols are replaced by their values.  */
1957 
1958 tree
default_conversion(tree exp)1959 default_conversion (tree exp)
1960 {
1961   tree orig_exp;
1962   tree type = TREE_TYPE (exp);
1963   enum tree_code code = TREE_CODE (type);
1964   tree promoted_type;
1965 
1966   mark_exp_read (exp);
1967 
1968   /* Functions and arrays have been converted during parsing.  */
1969   gcc_assert (code != FUNCTION_TYPE);
1970   if (code == ARRAY_TYPE)
1971     return exp;
1972 
1973   /* Constants can be used directly unless they're not loadable.  */
1974   if (TREE_CODE (exp) == CONST_DECL)
1975     exp = DECL_INITIAL (exp);
1976 
1977   /* Strip no-op conversions.  */
1978   orig_exp = exp;
1979   STRIP_TYPE_NOPS (exp);
1980 
1981   if (TREE_NO_WARNING (orig_exp))
1982     TREE_NO_WARNING (exp) = 1;
1983 
1984   if (code == VOID_TYPE)
1985     {
1986       error ("void value not ignored as it ought to be");
1987       return error_mark_node;
1988     }
1989 
1990   exp = require_complete_type (exp);
1991   if (exp == error_mark_node)
1992     return error_mark_node;
1993 
1994   promoted_type = targetm.promoted_type (type);
1995   if (promoted_type)
1996     return convert (promoted_type, exp);
1997 
1998   if (INTEGRAL_TYPE_P (type))
1999     return perform_integral_promotions (exp);
2000 
2001   return exp;
2002 }
2003 
2004 /* Look up COMPONENT in a structure or union TYPE.
2005 
2006    If the component name is not found, returns NULL_TREE.  Otherwise,
2007    the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2008    stepping down the chain to the component, which is in the last
2009    TREE_VALUE of the list.  Normally the list is of length one, but if
2010    the component is embedded within (nested) anonymous structures or
2011    unions, the list steps down the chain to the component.  */
2012 
2013 static tree
lookup_field(tree type,tree component)2014 lookup_field (tree type, tree component)
2015 {
2016   tree field;
2017 
2018   /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2019      to the field elements.  Use a binary search on this array to quickly
2020      find the element.  Otherwise, do a linear search.  TYPE_LANG_SPECIFIC
2021      will always be set for structures which have many elements.  */
2022 
2023   if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2024     {
2025       int bot, top, half;
2026       tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2027 
2028       field = TYPE_FIELDS (type);
2029       bot = 0;
2030       top = TYPE_LANG_SPECIFIC (type)->s->len;
2031       while (top - bot > 1)
2032 	{
2033 	  half = (top - bot + 1) >> 1;
2034 	  field = field_array[bot+half];
2035 
2036 	  if (DECL_NAME (field) == NULL_TREE)
2037 	    {
2038 	      /* Step through all anon unions in linear fashion.  */
2039 	      while (DECL_NAME (field_array[bot]) == NULL_TREE)
2040 		{
2041 		  field = field_array[bot++];
2042 		  if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2043 		      || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2044 		    {
2045 		      tree anon = lookup_field (TREE_TYPE (field), component);
2046 
2047 		      if (anon)
2048 			return tree_cons (NULL_TREE, field, anon);
2049 
2050 		      /* The Plan 9 compiler permits referring
2051 			 directly to an anonymous struct/union field
2052 			 using a typedef name.  */
2053 		      if (flag_plan9_extensions
2054 			  && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2055 			  && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2056 			      == TYPE_DECL)
2057 			  && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2058 			      == component))
2059 			break;
2060 		    }
2061 		}
2062 
2063 	      /* Entire record is only anon unions.  */
2064 	      if (bot > top)
2065 		return NULL_TREE;
2066 
2067 	      /* Restart the binary search, with new lower bound.  */
2068 	      continue;
2069 	    }
2070 
2071 	  if (DECL_NAME (field) == component)
2072 	    break;
2073 	  if (DECL_NAME (field) < component)
2074 	    bot += half;
2075 	  else
2076 	    top = bot + half;
2077 	}
2078 
2079       if (DECL_NAME (field_array[bot]) == component)
2080 	field = field_array[bot];
2081       else if (DECL_NAME (field) != component)
2082 	return NULL_TREE;
2083     }
2084   else
2085     {
2086       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2087 	{
2088 	  if (DECL_NAME (field) == NULL_TREE
2089 	      && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2090 		  || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2091 	    {
2092 	      tree anon = lookup_field (TREE_TYPE (field), component);
2093 
2094 	      if (anon)
2095 		return tree_cons (NULL_TREE, field, anon);
2096 
2097 	      /* The Plan 9 compiler permits referring directly to an
2098 		 anonymous struct/union field using a typedef
2099 		 name.  */
2100 	      if (flag_plan9_extensions
2101 		  && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2102 		  && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2103 		  && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2104 		      == component))
2105 		break;
2106 	    }
2107 
2108 	  if (DECL_NAME (field) == component)
2109 	    break;
2110 	}
2111 
2112       if (field == NULL_TREE)
2113 	return NULL_TREE;
2114     }
2115 
2116   return tree_cons (NULL_TREE, field, NULL_TREE);
2117 }
2118 
2119 /* Make an expression to refer to the COMPONENT field of structure or
2120    union value DATUM.  COMPONENT is an IDENTIFIER_NODE.  LOC is the
2121    location of the COMPONENT_REF.  */
2122 
2123 tree
build_component_ref(location_t loc,tree datum,tree component)2124 build_component_ref (location_t loc, tree datum, tree component)
2125 {
2126   tree type = TREE_TYPE (datum);
2127   enum tree_code code = TREE_CODE (type);
2128   tree field = NULL;
2129   tree ref;
2130   bool datum_lvalue = lvalue_p (datum);
2131 
2132   if (!objc_is_public (datum, component))
2133     return error_mark_node;
2134 
2135   /* Detect Objective-C property syntax object.property.  */
2136   if (c_dialect_objc ()
2137       && (ref = objc_maybe_build_component_ref (datum, component)))
2138     return ref;
2139 
2140   /* See if there is a field or component with name COMPONENT.  */
2141 
2142   if (code == RECORD_TYPE || code == UNION_TYPE)
2143     {
2144       if (!COMPLETE_TYPE_P (type))
2145 	{
2146 	  c_incomplete_type_error (NULL_TREE, type);
2147 	  return error_mark_node;
2148 	}
2149 
2150       field = lookup_field (type, component);
2151 
2152       if (!field)
2153 	{
2154 	  error_at (loc, "%qT has no member named %qE", type, component);
2155 	  return error_mark_node;
2156 	}
2157 
2158       /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2159 	 This might be better solved in future the way the C++ front
2160 	 end does it - by giving the anonymous entities each a
2161 	 separate name and type, and then have build_component_ref
2162 	 recursively call itself.  We can't do that here.  */
2163       do
2164 	{
2165 	  tree subdatum = TREE_VALUE (field);
2166 	  int quals;
2167 	  tree subtype;
2168 	  bool use_datum_quals;
2169 
2170 	  if (TREE_TYPE (subdatum) == error_mark_node)
2171 	    return error_mark_node;
2172 
2173 	  /* If this is an rvalue, it does not have qualifiers in C
2174 	     standard terms and we must avoid propagating such
2175 	     qualifiers down to a non-lvalue array that is then
2176 	     converted to a pointer.  */
2177 	  use_datum_quals = (datum_lvalue
2178 			     || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2179 
2180 	  quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
2181 	  if (use_datum_quals)
2182 	    quals |= TYPE_QUALS (TREE_TYPE (datum));
2183 	  subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2184 
2185 	  ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
2186 			NULL_TREE);
2187 	  SET_EXPR_LOCATION (ref, loc);
2188 	  if (TREE_READONLY (subdatum)
2189 	      || (use_datum_quals && TREE_READONLY (datum)))
2190 	    TREE_READONLY (ref) = 1;
2191 	  if (TREE_THIS_VOLATILE (subdatum)
2192 	      || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
2193 	    TREE_THIS_VOLATILE (ref) = 1;
2194 
2195 	  if (TREE_DEPRECATED (subdatum))
2196 	    warn_deprecated_use (subdatum, NULL_TREE);
2197 
2198 	  datum = ref;
2199 
2200 	  field = TREE_CHAIN (field);
2201 	}
2202       while (field);
2203 
2204       return ref;
2205     }
2206   else if (code != ERROR_MARK)
2207     error_at (loc,
2208 	      "request for member %qE in something not a structure or union",
2209 	      component);
2210 
2211   return error_mark_node;
2212 }
2213 
2214 /* Given an expression PTR for a pointer, return an expression
2215    for the value pointed to.
2216    ERRORSTRING is the name of the operator to appear in error messages.
2217 
2218    LOC is the location to use for the generated tree.  */
2219 
2220 tree
build_indirect_ref(location_t loc,tree ptr,ref_operator errstring)2221 build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
2222 {
2223   tree pointer = default_conversion (ptr);
2224   tree type = TREE_TYPE (pointer);
2225   tree ref;
2226 
2227   if (TREE_CODE (type) == POINTER_TYPE)
2228     {
2229       if (CONVERT_EXPR_P (pointer)
2230           || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2231 	{
2232 	  /* If a warning is issued, mark it to avoid duplicates from
2233 	     the backend.  This only needs to be done at
2234 	     warn_strict_aliasing > 2.  */
2235 	  if (warn_strict_aliasing > 2)
2236 	    if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2237 					 type, TREE_OPERAND (pointer, 0)))
2238 	      TREE_NO_WARNING (pointer) = 1;
2239 	}
2240 
2241       if (TREE_CODE (pointer) == ADDR_EXPR
2242 	  && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2243 	      == TREE_TYPE (type)))
2244 	{
2245 	  ref = TREE_OPERAND (pointer, 0);
2246 	  protected_set_expr_location (ref, loc);
2247 	  return ref;
2248 	}
2249       else
2250 	{
2251 	  tree t = TREE_TYPE (type);
2252 
2253 	  ref = build1 (INDIRECT_REF, t, pointer);
2254 
2255 	  if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
2256 	    {
2257 	      error_at (loc, "dereferencing pointer to incomplete type");
2258 	      return error_mark_node;
2259 	    }
2260 	  if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
2261 	    warning_at (loc, 0, "dereferencing %<void *%> pointer");
2262 
2263 	  /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2264 	     so that we get the proper error message if the result is used
2265 	     to assign to.  Also, &* is supposed to be a no-op.
2266 	     And ANSI C seems to specify that the type of the result
2267 	     should be the const type.  */
2268 	  /* A de-reference of a pointer to const is not a const.  It is valid
2269 	     to change it via some other pointer.  */
2270 	  TREE_READONLY (ref) = TYPE_READONLY (t);
2271 	  TREE_SIDE_EFFECTS (ref)
2272 	    = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
2273 	  TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2274 	  protected_set_expr_location (ref, loc);
2275 	  return ref;
2276 	}
2277     }
2278   else if (TREE_CODE (pointer) != ERROR_MARK)
2279     invalid_indirection_error (loc, type, errstring);
2280 
2281   return error_mark_node;
2282 }
2283 
2284 /* This handles expressions of the form "a[i]", which denotes
2285    an array reference.
2286 
2287    This is logically equivalent in C to *(a+i), but we may do it differently.
2288    If A is a variable or a member, we generate a primitive ARRAY_REF.
2289    This avoids forcing the array out of registers, and can work on
2290    arrays that are not lvalues (for example, members of structures returned
2291    by functions).
2292 
2293    For vector types, allow vector[i] but not i[vector], and create
2294    *(((type*)&vectortype) + i) for the expression.
2295 
2296    LOC is the location to use for the returned expression.  */
2297 
2298 tree
build_array_ref(location_t loc,tree array,tree index)2299 build_array_ref (location_t loc, tree array, tree index)
2300 {
2301   tree ret;
2302   bool swapped = false;
2303   if (TREE_TYPE (array) == error_mark_node
2304       || TREE_TYPE (index) == error_mark_node)
2305     return error_mark_node;
2306 
2307   if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
2308       && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2309       /* Allow vector[index] but not index[vector].  */
2310       && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
2311     {
2312       tree temp;
2313       if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2314 	  && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
2315 	{
2316           error_at (loc,
2317             "subscripted value is neither array nor pointer nor vector");
2318 
2319 	  return error_mark_node;
2320 	}
2321       temp = array;
2322       array = index;
2323       index = temp;
2324       swapped = true;
2325     }
2326 
2327   if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2328     {
2329       error_at (loc, "array subscript is not an integer");
2330       return error_mark_node;
2331     }
2332 
2333   if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2334     {
2335       error_at (loc, "subscripted value is pointer to function");
2336       return error_mark_node;
2337     }
2338 
2339   /* ??? Existing practice has been to warn only when the char
2340      index is syntactically the index, not for char[array].  */
2341   if (!swapped)
2342      warn_array_subscript_with_type_char (index);
2343 
2344   /* Apply default promotions *after* noticing character types.  */
2345   index = default_conversion (index);
2346 
2347   gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
2348 
2349   convert_vector_to_pointer_for_subscript (loc, &array, index);
2350 
2351   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2352     {
2353       tree rval, type;
2354 
2355       /* An array that is indexed by a non-constant
2356 	 cannot be stored in a register; we must be able to do
2357 	 address arithmetic on its address.
2358 	 Likewise an array of elements of variable size.  */
2359       if (TREE_CODE (index) != INTEGER_CST
2360 	  || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
2361 	      && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2362 	{
2363 	  if (!c_mark_addressable (array))
2364 	    return error_mark_node;
2365 	}
2366       /* An array that is indexed by a constant value which is not within
2367 	 the array bounds cannot be stored in a register either; because we
2368 	 would get a crash in store_bit_field/extract_bit_field when trying
2369 	 to access a non-existent part of the register.  */
2370       if (TREE_CODE (index) == INTEGER_CST
2371 	  && TYPE_DOMAIN (TREE_TYPE (array))
2372 	  && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
2373 	{
2374 	  if (!c_mark_addressable (array))
2375 	    return error_mark_node;
2376 	}
2377 
2378       if (pedantic)
2379 	{
2380 	  tree foo = array;
2381 	  while (TREE_CODE (foo) == COMPONENT_REF)
2382 	    foo = TREE_OPERAND (foo, 0);
2383 	  if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
2384 	    pedwarn (loc, OPT_Wpedantic,
2385 		     "ISO C forbids subscripting %<register%> array");
2386 	  else if (!flag_isoc99 && !lvalue_p (foo))
2387 	    pedwarn (loc, OPT_Wpedantic,
2388 		     "ISO C90 forbids subscripting non-lvalue array");
2389 	}
2390 
2391       type = TREE_TYPE (TREE_TYPE (array));
2392       rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
2393       /* Array ref is const/volatile if the array elements are
2394 	 or if the array is.  */
2395       TREE_READONLY (rval)
2396 	|= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2397 	    | TREE_READONLY (array));
2398       TREE_SIDE_EFFECTS (rval)
2399 	|= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2400 	    | TREE_SIDE_EFFECTS (array));
2401       TREE_THIS_VOLATILE (rval)
2402 	|= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2403 	    /* This was added by rms on 16 Nov 91.
2404 	       It fixes  vol struct foo *a;  a->elts[1]
2405 	       in an inline function.
2406 	       Hope it doesn't break something else.  */
2407 	    | TREE_THIS_VOLATILE (array));
2408       ret = require_complete_type (rval);
2409       protected_set_expr_location (ret, loc);
2410       return ret;
2411     }
2412   else
2413     {
2414       tree ar = default_conversion (array);
2415 
2416       if (ar == error_mark_node)
2417 	return ar;
2418 
2419       gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2420       gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
2421 
2422       return build_indirect_ref
2423 	(loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
2424 	 RO_ARRAY_INDEXING);
2425     }
2426 }
2427 
2428 /* Build an external reference to identifier ID.  FUN indicates
2429    whether this will be used for a function call.  LOC is the source
2430    location of the identifier.  This sets *TYPE to the type of the
2431    identifier, which is not the same as the type of the returned value
2432    for CONST_DECLs defined as enum constants.  If the type of the
2433    identifier is not available, *TYPE is set to NULL.  */
2434 tree
build_external_ref(location_t loc,tree id,int fun,tree * type)2435 build_external_ref (location_t loc, tree id, int fun, tree *type)
2436 {
2437   tree ref;
2438   tree decl = lookup_name (id);
2439 
2440   /* In Objective-C, an instance variable (ivar) may be preferred to
2441      whatever lookup_name() found.  */
2442   decl = objc_lookup_ivar (decl, id);
2443 
2444   *type = NULL;
2445   if (decl && decl != error_mark_node)
2446     {
2447       ref = decl;
2448       *type = TREE_TYPE (ref);
2449     }
2450   else if (fun)
2451     /* Implicit function declaration.  */
2452     ref = implicitly_declare (loc, id);
2453   else if (decl == error_mark_node)
2454     /* Don't complain about something that's already been
2455        complained about.  */
2456     return error_mark_node;
2457   else
2458     {
2459       undeclared_variable (loc, id);
2460       return error_mark_node;
2461     }
2462 
2463   if (TREE_TYPE (ref) == error_mark_node)
2464     return error_mark_node;
2465 
2466   if (TREE_DEPRECATED (ref))
2467     warn_deprecated_use (ref, NULL_TREE);
2468 
2469   /* Recursive call does not count as usage.  */
2470   if (ref != current_function_decl)
2471     {
2472       TREE_USED (ref) = 1;
2473     }
2474 
2475   if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2476     {
2477       if (!in_sizeof && !in_typeof)
2478 	C_DECL_USED (ref) = 1;
2479       else if (DECL_INITIAL (ref) == 0
2480 	       && DECL_EXTERNAL (ref)
2481 	       && !TREE_PUBLIC (ref))
2482 	record_maybe_used_decl (ref);
2483     }
2484 
2485   if (TREE_CODE (ref) == CONST_DECL)
2486     {
2487       used_types_insert (TREE_TYPE (ref));
2488 
2489       if (warn_cxx_compat
2490 	  && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2491 	  && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2492 	{
2493 	  warning_at (loc, OPT_Wc___compat,
2494 		      ("enum constant defined in struct or union "
2495 		       "is not visible in C++"));
2496 	  inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2497 	}
2498 
2499       ref = DECL_INITIAL (ref);
2500       TREE_CONSTANT (ref) = 1;
2501     }
2502   else if (current_function_decl != 0
2503 	   && !DECL_FILE_SCOPE_P (current_function_decl)
2504 	   && (TREE_CODE (ref) == VAR_DECL
2505 	       || TREE_CODE (ref) == PARM_DECL
2506 	       || TREE_CODE (ref) == FUNCTION_DECL))
2507     {
2508       tree context = decl_function_context (ref);
2509 
2510       if (context != 0 && context != current_function_decl)
2511 	DECL_NONLOCAL (ref) = 1;
2512     }
2513   /* C99 6.7.4p3: An inline definition of a function with external
2514      linkage ... shall not contain a reference to an identifier with
2515      internal linkage.  */
2516   else if (current_function_decl != 0
2517 	   && DECL_DECLARED_INLINE_P (current_function_decl)
2518 	   && DECL_EXTERNAL (current_function_decl)
2519 	   && VAR_OR_FUNCTION_DECL_P (ref)
2520 	   && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
2521 	   && ! TREE_PUBLIC (ref)
2522 	   && DECL_CONTEXT (ref) != current_function_decl)
2523     record_inline_static (loc, current_function_decl, ref,
2524 			  csi_internal);
2525 
2526   return ref;
2527 }
2528 
2529 /* Record details of decls possibly used inside sizeof or typeof.  */
2530 struct maybe_used_decl
2531 {
2532   /* The decl.  */
2533   tree decl;
2534   /* The level seen at (in_sizeof + in_typeof).  */
2535   int level;
2536   /* The next one at this level or above, or NULL.  */
2537   struct maybe_used_decl *next;
2538 };
2539 
2540 static struct maybe_used_decl *maybe_used_decls;
2541 
2542 /* Record that DECL, an undefined static function reference seen
2543    inside sizeof or typeof, might be used if the operand of sizeof is
2544    a VLA type or the operand of typeof is a variably modified
2545    type.  */
2546 
2547 static void
record_maybe_used_decl(tree decl)2548 record_maybe_used_decl (tree decl)
2549 {
2550   struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2551   t->decl = decl;
2552   t->level = in_sizeof + in_typeof;
2553   t->next = maybe_used_decls;
2554   maybe_used_decls = t;
2555 }
2556 
2557 /* Pop the stack of decls possibly used inside sizeof or typeof.  If
2558    USED is false, just discard them.  If it is true, mark them used
2559    (if no longer inside sizeof or typeof) or move them to the next
2560    level up (if still inside sizeof or typeof).  */
2561 
2562 void
pop_maybe_used(bool used)2563 pop_maybe_used (bool used)
2564 {
2565   struct maybe_used_decl *p = maybe_used_decls;
2566   int cur_level = in_sizeof + in_typeof;
2567   while (p && p->level > cur_level)
2568     {
2569       if (used)
2570 	{
2571 	  if (cur_level == 0)
2572 	    C_DECL_USED (p->decl) = 1;
2573 	  else
2574 	    p->level = cur_level;
2575 	}
2576       p = p->next;
2577     }
2578   if (!used || cur_level == 0)
2579     maybe_used_decls = p;
2580 }
2581 
2582 /* Return the result of sizeof applied to EXPR.  */
2583 
2584 struct c_expr
c_expr_sizeof_expr(location_t loc,struct c_expr expr)2585 c_expr_sizeof_expr (location_t loc, struct c_expr expr)
2586 {
2587   struct c_expr ret;
2588   if (expr.value == error_mark_node)
2589     {
2590       ret.value = error_mark_node;
2591       ret.original_code = ERROR_MARK;
2592       ret.original_type = NULL;
2593       pop_maybe_used (false);
2594     }
2595   else
2596     {
2597       bool expr_const_operands = true;
2598       tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2599 				       &expr_const_operands);
2600       ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
2601       c_last_sizeof_arg = expr.value;
2602       ret.original_code = SIZEOF_EXPR;
2603       ret.original_type = NULL;
2604       if (c_vla_type_p (TREE_TYPE (folded_expr)))
2605 	{
2606 	  /* sizeof is evaluated when given a vla (C99 6.5.3.4p2).  */
2607 	  ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2608 			      folded_expr, ret.value);
2609 	  C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
2610 	  SET_EXPR_LOCATION (ret.value, loc);
2611 	}
2612       pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
2613     }
2614   return ret;
2615 }
2616 
2617 /* Return the result of sizeof applied to T, a structure for the type
2618    name passed to sizeof (rather than the type itself).  LOC is the
2619    location of the original expression.  */
2620 
2621 struct c_expr
c_expr_sizeof_type(location_t loc,struct c_type_name * t)2622 c_expr_sizeof_type (location_t loc, struct c_type_name *t)
2623 {
2624   tree type;
2625   struct c_expr ret;
2626   tree type_expr = NULL_TREE;
2627   bool type_expr_const = true;
2628   type = groktypename (t, &type_expr, &type_expr_const);
2629   ret.value = c_sizeof (loc, type);
2630   c_last_sizeof_arg = type;
2631   ret.original_code = SIZEOF_EXPR;
2632   ret.original_type = NULL;
2633   if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2634       && c_vla_type_p (type))
2635     {
2636       /* If the type is a [*] array, it is a VLA but is represented as
2637 	 having a size of zero.  In such a case we must ensure that
2638 	 the result of sizeof does not get folded to a constant by
2639 	 c_fully_fold, because if the size is evaluated the result is
2640 	 not constant and so constraints on zero or negative size
2641 	 arrays must not be applied when this sizeof call is inside
2642 	 another array declarator.  */
2643       if (!type_expr)
2644 	type_expr = integer_zero_node;
2645       ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2646 			  type_expr, ret.value);
2647       C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2648     }
2649   pop_maybe_used (type != error_mark_node
2650 		  ? C_TYPE_VARIABLE_SIZE (type) : false);
2651   return ret;
2652 }
2653 
2654 /* Build a function call to function FUNCTION with parameters PARAMS.
2655    The function call is at LOC.
2656    PARAMS is a list--a chain of TREE_LIST nodes--in which the
2657    TREE_VALUE of each node is a parameter-expression.
2658    FUNCTION's data type may be a function type or a pointer-to-function.  */
2659 
2660 tree
build_function_call(location_t loc,tree function,tree params)2661 build_function_call (location_t loc, tree function, tree params)
2662 {
2663   vec<tree, va_gc> *v;
2664   tree ret;
2665 
2666   vec_alloc (v, list_length (params));
2667   for (; params; params = TREE_CHAIN (params))
2668     v->quick_push (TREE_VALUE (params));
2669   ret = c_build_function_call_vec (loc, function, v, NULL);
2670   vec_free (v);
2671   return ret;
2672 }
2673 
2674 /* Give a note about the location of the declaration of DECL.  */
2675 
inform_declaration(tree decl)2676 static void inform_declaration (tree decl)
2677 {
2678   if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2679     inform (DECL_SOURCE_LOCATION (decl), "declared here");
2680 }
2681 
2682 /* Build a function call to function FUNCTION with parameters PARAMS.
2683    ORIGTYPES, if not NULL, is a vector of types; each element is
2684    either NULL or the original type of the corresponding element in
2685    PARAMS.  The original type may differ from TREE_TYPE of the
2686    parameter for enums.  FUNCTION's data type may be a function type
2687    or pointer-to-function.  This function changes the elements of
2688    PARAMS.  */
2689 
2690 tree
build_function_call_vec(location_t loc,tree function,vec<tree,va_gc> * params,vec<tree,va_gc> * origtypes)2691 build_function_call_vec (location_t loc, tree function,
2692 			 vec<tree, va_gc> *params,
2693 			 vec<tree, va_gc> *origtypes)
2694 {
2695   tree fntype, fundecl = 0;
2696   tree name = NULL_TREE, result;
2697   tree tem;
2698   int nargs;
2699   tree *argarray;
2700 
2701 
2702   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
2703   STRIP_TYPE_NOPS (function);
2704 
2705   /* Convert anything with function type to a pointer-to-function.  */
2706   if (TREE_CODE (function) == FUNCTION_DECL)
2707     {
2708       name = DECL_NAME (function);
2709 
2710       if (flag_tm)
2711 	tm_malloc_replacement (function);
2712       fundecl = function;
2713       /* Atomic functions have type checking/casting already done.  They are
2714 	 often rewritten and don't match the original parameter list.  */
2715       if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2716         origtypes = NULL;
2717     }
2718   if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
2719     function = function_to_pointer_conversion (loc, function);
2720 
2721   /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2722      expressions, like those used for ObjC messenger dispatches.  */
2723   if (params && !params->is_empty ())
2724     function = objc_rewrite_function_call (function, (*params)[0]);
2725 
2726   function = c_fully_fold (function, false, NULL);
2727 
2728   fntype = TREE_TYPE (function);
2729 
2730   if (TREE_CODE (fntype) == ERROR_MARK)
2731     return error_mark_node;
2732 
2733   if (!(TREE_CODE (fntype) == POINTER_TYPE
2734 	&& TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2735     {
2736       if (!flag_diagnostics_show_caret)
2737 	error_at (loc,
2738 		  "called object %qE is not a function or function pointer",
2739 		  function);
2740       else if (DECL_P (function))
2741 	{
2742 	  error_at (loc,
2743 		    "called object %qD is not a function or function pointer",
2744 		    function);
2745 	  inform_declaration (function);
2746 	}
2747       else
2748 	error_at (loc,
2749 		  "called object is not a function or function pointer");
2750       return error_mark_node;
2751     }
2752 
2753   if (fundecl && TREE_THIS_VOLATILE (fundecl))
2754     current_function_returns_abnormally = 1;
2755 
2756   /* fntype now gets the type of function pointed to.  */
2757   fntype = TREE_TYPE (fntype);
2758 
2759   /* Convert the parameters to the types declared in the
2760      function prototype, or apply default promotions.  */
2761 
2762   nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2763 			     function, fundecl);
2764   if (nargs < 0)
2765     return error_mark_node;
2766 
2767   /* Check that the function is called through a compatible prototype.
2768      If it is not, replace the call by a trap, wrapped up in a compound
2769      expression if necessary.  This has the nice side-effect to prevent
2770      the tree-inliner from generating invalid assignment trees which may
2771      blow up in the RTL expander later.  */
2772   if (CONVERT_EXPR_P (function)
2773       && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2774       && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
2775       && !comptypes (fntype, TREE_TYPE (tem)))
2776     {
2777       tree return_type = TREE_TYPE (fntype);
2778       tree trap = build_function_call (loc,
2779 				       builtin_decl_explicit (BUILT_IN_TRAP),
2780 				       NULL_TREE);
2781       int i;
2782 
2783       /* This situation leads to run-time undefined behavior.  We can't,
2784 	 therefore, simply error unless we can prove that all possible
2785 	 executions of the program must execute the code.  */
2786       if (warning_at (loc, 0, "function called through a non-compatible type"))
2787 	/* We can, however, treat "undefined" any way we please.
2788 	   Call abort to encourage the user to fix the program.  */
2789 	inform (loc, "if this code is reached, the program will abort");
2790       /* Before the abort, allow the function arguments to exit or
2791 	 call longjmp.  */
2792       for (i = 0; i < nargs; i++)
2793 	trap = build2 (COMPOUND_EXPR, void_type_node, (*params)[i], trap);
2794 
2795       if (VOID_TYPE_P (return_type))
2796 	{
2797 	  if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2798 	    pedwarn (loc, 0,
2799 		     "function with qualified void return type called");
2800 	  return trap;
2801 	}
2802       else
2803 	{
2804 	  tree rhs;
2805 
2806 	  if (AGGREGATE_TYPE_P (return_type))
2807 	    rhs = build_compound_literal (loc, return_type,
2808 					  build_constructor (return_type,
2809 					    NULL),
2810 					  false);
2811 	  else
2812 	    rhs = build_zero_cst (return_type);
2813 
2814 	  return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2815 						trap, rhs));
2816 	}
2817     }
2818 
2819   argarray = vec_safe_address (params);
2820 
2821   /* Check that arguments to builtin functions match the expectations.  */
2822   if (fundecl
2823       && DECL_BUILT_IN (fundecl)
2824       && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2825       && !check_builtin_function_arguments (fundecl, nargs, argarray))
2826     return error_mark_node;
2827 
2828   /* Check that the arguments to the function are valid.  */
2829   check_function_arguments (fntype, nargs, argarray);
2830 
2831   if (name != NULL_TREE
2832       && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
2833     {
2834       if (require_constant_value)
2835 	result =
2836 	  fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2837 						 function, nargs, argarray);
2838       else
2839 	result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2840 					    function, nargs, argarray);
2841       if (TREE_CODE (result) == NOP_EXPR
2842 	  && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2843 	STRIP_TYPE_NOPS (result);
2844     }
2845   else
2846     result = build_call_array_loc (loc, TREE_TYPE (fntype),
2847 				   function, nargs, argarray);
2848 
2849   if (VOID_TYPE_P (TREE_TYPE (result)))
2850     {
2851       if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
2852 	pedwarn (loc, 0,
2853 		 "function with qualified void return type called");
2854       return result;
2855     }
2856   return require_complete_type (result);
2857 }
2858 
2859 /* Like build_function_call_vec, but call also resolve_overloaded_builtin.  */
2860 
2861 tree
c_build_function_call_vec(location_t loc,tree function,vec<tree,va_gc> * params,vec<tree,va_gc> * origtypes)2862 c_build_function_call_vec (location_t loc, tree function,
2863 			   vec<tree, va_gc> *params,
2864 			   vec<tree, va_gc> *origtypes)
2865 {
2866   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
2867   STRIP_TYPE_NOPS (function);
2868 
2869   /* Convert anything with function type to a pointer-to-function.  */
2870   if (TREE_CODE (function) == FUNCTION_DECL)
2871     {
2872       /* Implement type-directed function overloading for builtins.
2873 	 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2874 	 handle all the type checking.  The result is a complete expression
2875 	 that implements this function call.  */
2876       tree tem = resolve_overloaded_builtin (loc, function, params);
2877       if (tem)
2878 	return tem;
2879     }
2880   return build_function_call_vec (loc, function, params, origtypes);
2881 }
2882 
2883 /* Convert the argument expressions in the vector VALUES
2884    to the types in the list TYPELIST.
2885 
2886    If TYPELIST is exhausted, or when an element has NULL as its type,
2887    perform the default conversions.
2888 
2889    ORIGTYPES is the original types of the expressions in VALUES.  This
2890    holds the type of enum values which have been converted to integral
2891    types.  It may be NULL.
2892 
2893    FUNCTION is a tree for the called function.  It is used only for
2894    error messages, where it is formatted with %qE.
2895 
2896    This is also where warnings about wrong number of args are generated.
2897 
2898    Returns the actual number of arguments processed (which may be less
2899    than the length of VALUES in some error situations), or -1 on
2900    failure.  */
2901 
2902 static int
convert_arguments(tree typelist,vec<tree,va_gc> * values,vec<tree,va_gc> * origtypes,tree function,tree fundecl)2903 convert_arguments (tree typelist, vec<tree, va_gc> *values,
2904 		   vec<tree, va_gc> *origtypes, tree function, tree fundecl)
2905 {
2906   tree typetail, val;
2907   unsigned int parmnum;
2908   bool error_args = false;
2909   const bool type_generic = fundecl
2910     && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
2911   bool type_generic_remove_excess_precision = false;
2912   tree selector;
2913 
2914   /* Change pointer to function to the function itself for
2915      diagnostics.  */
2916   if (TREE_CODE (function) == ADDR_EXPR
2917       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2918     function = TREE_OPERAND (function, 0);
2919 
2920   /* Handle an ObjC selector specially for diagnostics.  */
2921   selector = objc_message_selector ();
2922 
2923   /* For type-generic built-in functions, determine whether excess
2924      precision should be removed (classification) or not
2925      (comparison).  */
2926   if (type_generic
2927       && DECL_BUILT_IN (fundecl)
2928       && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
2929     {
2930       switch (DECL_FUNCTION_CODE (fundecl))
2931 	{
2932 	case BUILT_IN_ISFINITE:
2933 	case BUILT_IN_ISINF:
2934 	case BUILT_IN_ISINF_SIGN:
2935 	case BUILT_IN_ISNAN:
2936 	case BUILT_IN_ISNORMAL:
2937 	case BUILT_IN_FPCLASSIFY:
2938 	  type_generic_remove_excess_precision = true;
2939 	  break;
2940 
2941 	default:
2942 	  type_generic_remove_excess_precision = false;
2943 	  break;
2944 	}
2945     }
2946 
2947   /* Scan the given expressions and types, producing individual
2948      converted arguments.  */
2949 
2950   for (typetail = typelist, parmnum = 0;
2951        values && values->iterate (parmnum, &val);
2952        ++parmnum)
2953     {
2954       tree type = typetail ? TREE_VALUE (typetail) : 0;
2955       tree valtype = TREE_TYPE (val);
2956       tree rname = function;
2957       int argnum = parmnum + 1;
2958       const char *invalid_func_diag;
2959       bool excess_precision = false;
2960       bool npc;
2961       tree parmval;
2962 
2963       if (type == void_type_node)
2964 	{
2965 	  if (selector)
2966 	    error_at (input_location,
2967 		      "too many arguments to method %qE", selector);
2968 	  else
2969 	    error_at (input_location,
2970 		      "too many arguments to function %qE", function);
2971 	  inform_declaration (fundecl);
2972 	  return parmnum;
2973 	}
2974 
2975       if (selector && argnum > 2)
2976 	{
2977 	  rname = selector;
2978 	  argnum -= 2;
2979 	}
2980 
2981       npc = null_pointer_constant_p (val);
2982 
2983       /* If there is excess precision and a prototype, convert once to
2984 	 the required type rather than converting via the semantic
2985 	 type.  Likewise without a prototype a float value represented
2986 	 as long double should be converted once to double.  But for
2987 	 type-generic classification functions excess precision must
2988 	 be removed here.  */
2989       if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
2990 	  && (type || !type_generic || !type_generic_remove_excess_precision))
2991 	{
2992 	  val = TREE_OPERAND (val, 0);
2993 	  excess_precision = true;
2994 	}
2995       val = c_fully_fold (val, false, NULL);
2996       STRIP_TYPE_NOPS (val);
2997 
2998       val = require_complete_type (val);
2999 
3000       if (type != 0)
3001 	{
3002 	  /* Formal parm type is specified by a function prototype.  */
3003 
3004 	  if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3005 	    {
3006 	      error ("type of formal parameter %d is incomplete", parmnum + 1);
3007 	      parmval = val;
3008 	    }
3009 	  else
3010 	    {
3011 	      tree origtype;
3012 
3013 	      /* Optionally warn about conversions that
3014 		 differ from the default conversions.  */
3015 	      if (warn_traditional_conversion || warn_traditional)
3016 		{
3017 		  unsigned int formal_prec = TYPE_PRECISION (type);
3018 
3019 		  if (INTEGRAL_TYPE_P (type)
3020 		      && TREE_CODE (valtype) == REAL_TYPE)
3021 		    warning (0, "passing argument %d of %qE as integer "
3022 			     "rather than floating due to prototype",
3023 			     argnum, rname);
3024 		  if (INTEGRAL_TYPE_P (type)
3025 		      && TREE_CODE (valtype) == COMPLEX_TYPE)
3026 		    warning (0, "passing argument %d of %qE as integer "
3027 			     "rather than complex due to prototype",
3028 			     argnum, rname);
3029 		  else if (TREE_CODE (type) == COMPLEX_TYPE
3030 			   && TREE_CODE (valtype) == REAL_TYPE)
3031 		    warning (0, "passing argument %d of %qE as complex "
3032 			     "rather than floating due to prototype",
3033 			     argnum, rname);
3034 		  else if (TREE_CODE (type) == REAL_TYPE
3035 			   && INTEGRAL_TYPE_P (valtype))
3036 		    warning (0, "passing argument %d of %qE as floating "
3037 			     "rather than integer due to prototype",
3038 			     argnum, rname);
3039 		  else if (TREE_CODE (type) == COMPLEX_TYPE
3040 			   && INTEGRAL_TYPE_P (valtype))
3041 		    warning (0, "passing argument %d of %qE as complex "
3042 			     "rather than integer due to prototype",
3043 			     argnum, rname);
3044 		  else if (TREE_CODE (type) == REAL_TYPE
3045 			   && TREE_CODE (valtype) == COMPLEX_TYPE)
3046 		    warning (0, "passing argument %d of %qE as floating "
3047 			     "rather than complex due to prototype",
3048 			     argnum, rname);
3049 		  /* ??? At some point, messages should be written about
3050 		     conversions between complex types, but that's too messy
3051 		     to do now.  */
3052 		  else if (TREE_CODE (type) == REAL_TYPE
3053 			   && TREE_CODE (valtype) == REAL_TYPE)
3054 		    {
3055 		      /* Warn if any argument is passed as `float',
3056 			 since without a prototype it would be `double'.  */
3057 		      if (formal_prec == TYPE_PRECISION (float_type_node)
3058 			  && type != dfloat32_type_node)
3059 			warning (0, "passing argument %d of %qE as %<float%> "
3060 				 "rather than %<double%> due to prototype",
3061 				 argnum, rname);
3062 
3063 		      /* Warn if mismatch between argument and prototype
3064 			 for decimal float types.  Warn of conversions with
3065 			 binary float types and of precision narrowing due to
3066 			 prototype. */
3067  		      else if (type != valtype
3068 			       && (type == dfloat32_type_node
3069 				   || type == dfloat64_type_node
3070 				   || type == dfloat128_type_node
3071 				   || valtype == dfloat32_type_node
3072 				   || valtype == dfloat64_type_node
3073 				   || valtype == dfloat128_type_node)
3074 			       && (formal_prec
3075 				   <= TYPE_PRECISION (valtype)
3076 				   || (type == dfloat128_type_node
3077 				       && (valtype
3078 					   != dfloat64_type_node
3079 					   && (valtype
3080 					       != dfloat32_type_node)))
3081 				   || (type == dfloat64_type_node
3082 				       && (valtype
3083 					   != dfloat32_type_node))))
3084 			warning (0, "passing argument %d of %qE as %qT "
3085 				 "rather than %qT due to prototype",
3086 				 argnum, rname, type, valtype);
3087 
3088 		    }
3089 		  /* Detect integer changing in width or signedness.
3090 		     These warnings are only activated with
3091 		     -Wtraditional-conversion, not with -Wtraditional.  */
3092 		  else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
3093 			   && INTEGRAL_TYPE_P (valtype))
3094 		    {
3095 		      tree would_have_been = default_conversion (val);
3096 		      tree type1 = TREE_TYPE (would_have_been);
3097 
3098 		      if (TREE_CODE (type) == ENUMERAL_TYPE
3099 			  && (TYPE_MAIN_VARIANT (type)
3100 			      == TYPE_MAIN_VARIANT (valtype)))
3101 			/* No warning if function asks for enum
3102 			   and the actual arg is that enum type.  */
3103 			;
3104 		      else if (formal_prec != TYPE_PRECISION (type1))
3105 			warning (OPT_Wtraditional_conversion,
3106 				 "passing argument %d of %qE "
3107 				 "with different width due to prototype",
3108 				 argnum, rname);
3109 		      else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
3110 			;
3111 		      /* Don't complain if the formal parameter type
3112 			 is an enum, because we can't tell now whether
3113 			 the value was an enum--even the same enum.  */
3114 		      else if (TREE_CODE (type) == ENUMERAL_TYPE)
3115 			;
3116 		      else if (TREE_CODE (val) == INTEGER_CST
3117 			       && int_fits_type_p (val, type))
3118 			/* Change in signedness doesn't matter
3119 			   if a constant value is unaffected.  */
3120 			;
3121 		      /* If the value is extended from a narrower
3122 			 unsigned type, it doesn't matter whether we
3123 			 pass it as signed or unsigned; the value
3124 			 certainly is the same either way.  */
3125 		      else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3126 			       && TYPE_UNSIGNED (valtype))
3127 			;
3128 		      else if (TYPE_UNSIGNED (type))
3129 			warning (OPT_Wtraditional_conversion,
3130 				 "passing argument %d of %qE "
3131 				 "as unsigned due to prototype",
3132 				 argnum, rname);
3133 		      else
3134 			warning (OPT_Wtraditional_conversion,
3135 				 "passing argument %d of %qE "
3136 				 "as signed due to prototype", argnum, rname);
3137 		    }
3138 		}
3139 
3140 	      /* Possibly restore an EXCESS_PRECISION_EXPR for the
3141 		 sake of better warnings from convert_and_check.  */
3142 	      if (excess_precision)
3143 		val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
3144 	      origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
3145 	      parmval = convert_for_assignment (input_location, type, val,
3146 						origtype, ic_argpass, npc,
3147 						fundecl, function,
3148 						parmnum + 1);
3149 
3150 	      if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
3151 		  && INTEGRAL_TYPE_P (type)
3152 		  && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3153 		parmval = default_conversion (parmval);
3154 	    }
3155 	}
3156       else if (TREE_CODE (valtype) == REAL_TYPE
3157 	       && (TYPE_PRECISION (valtype)
3158 		   < TYPE_PRECISION (double_type_node))
3159 	       && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3160         {
3161 	  if (type_generic)
3162 	    parmval = val;
3163 	  else
3164 	    {
3165 	      /* Convert `float' to `double'.  */
3166 	      if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3167 		warning (OPT_Wdouble_promotion,
3168 			 "implicit conversion from %qT to %qT when passing "
3169 			 "argument to function",
3170 			 valtype, double_type_node);
3171 	      parmval = convert (double_type_node, val);
3172 	    }
3173 	}
3174       else if (excess_precision && !type_generic)
3175 	/* A "double" argument with excess precision being passed
3176 	   without a prototype or in variable arguments.  */
3177 	parmval = convert (valtype, val);
3178       else if ((invalid_func_diag =
3179 		targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
3180 	{
3181 	  error (invalid_func_diag);
3182 	  return -1;
3183 	}
3184       else
3185 	/* Convert `short' and `char' to full-size `int'.  */
3186 	parmval = default_conversion (val);
3187 
3188       (*values)[parmnum] = parmval;
3189       if (parmval == error_mark_node)
3190 	error_args = true;
3191 
3192       if (typetail)
3193 	typetail = TREE_CHAIN (typetail);
3194     }
3195 
3196   gcc_assert (parmnum == vec_safe_length (values));
3197 
3198   if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3199     {
3200       error_at (input_location,
3201 		"too few arguments to function %qE", function);
3202       inform_declaration (fundecl);
3203       return -1;
3204     }
3205 
3206   return error_args ? -1 : (int) parmnum;
3207 }
3208 
3209 /* This is the entry point used by the parser to build unary operators
3210    in the input.  CODE, a tree_code, specifies the unary operator, and
3211    ARG is the operand.  For unary plus, the C parser currently uses
3212    CONVERT_EXPR for code.
3213 
3214    LOC is the location to use for the tree generated.
3215 */
3216 
3217 struct c_expr
parser_build_unary_op(location_t loc,enum tree_code code,struct c_expr arg)3218 parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
3219 {
3220   struct c_expr result;
3221 
3222   result.value = build_unary_op (loc, code, arg.value, 0);
3223   result.original_code = code;
3224   result.original_type = NULL;
3225 
3226   if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3227     overflow_warning (loc, result.value);
3228 
3229   return result;
3230 }
3231 
3232 /* This is the entry point used by the parser to build binary operators
3233    in the input.  CODE, a tree_code, specifies the binary operator, and
3234    ARG1 and ARG2 are the operands.  In addition to constructing the
3235    expression, we check for operands that were written with other binary
3236    operators in a way that is likely to confuse the user.
3237 
3238    LOCATION is the location of the binary operator.  */
3239 
3240 struct c_expr
parser_build_binary_op(location_t location,enum tree_code code,struct c_expr arg1,struct c_expr arg2)3241 parser_build_binary_op (location_t location, enum tree_code code,
3242 			struct c_expr arg1, struct c_expr arg2)
3243 {
3244   struct c_expr result;
3245 
3246   enum tree_code code1 = arg1.original_code;
3247   enum tree_code code2 = arg2.original_code;
3248   tree type1 = (arg1.original_type
3249                 ? arg1.original_type
3250                 : TREE_TYPE (arg1.value));
3251   tree type2 = (arg2.original_type
3252                 ? arg2.original_type
3253                 : TREE_TYPE (arg2.value));
3254 
3255   result.value = build_binary_op (location, code,
3256 				  arg1.value, arg2.value, 1);
3257   result.original_code = code;
3258   result.original_type = NULL;
3259 
3260   if (TREE_CODE (result.value) == ERROR_MARK)
3261     return result;
3262 
3263   if (location != UNKNOWN_LOCATION)
3264     protected_set_expr_location (result.value, location);
3265 
3266   /* Check for cases such as x+y<<z which users are likely
3267      to misinterpret.  */
3268   if (warn_parentheses)
3269     warn_about_parentheses (input_location, code,
3270 			    code1, arg1.value, code2, arg2.value);
3271 
3272   if (warn_logical_op)
3273     warn_logical_operator (input_location, code, TREE_TYPE (result.value),
3274 			   code1, arg1.value, code2, arg2.value);
3275 
3276   /* Warn about comparisons against string literals, with the exception
3277      of testing for equality or inequality of a string literal with NULL.  */
3278   if (code == EQ_EXPR || code == NE_EXPR)
3279     {
3280       if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3281 	  || (code2 == STRING_CST && !integer_zerop (arg1.value)))
3282 	warning_at (location, OPT_Waddress,
3283 		    "comparison with string literal results in unspecified behavior");
3284     }
3285   else if (TREE_CODE_CLASS (code) == tcc_comparison
3286 	   && (code1 == STRING_CST || code2 == STRING_CST))
3287     warning_at (location, OPT_Waddress,
3288 		"comparison with string literal results in unspecified behavior");
3289 
3290   if (TREE_OVERFLOW_P (result.value)
3291       && !TREE_OVERFLOW_P (arg1.value)
3292       && !TREE_OVERFLOW_P (arg2.value))
3293     overflow_warning (location, result.value);
3294 
3295   /* Warn about comparisons of different enum types.  */
3296   if (warn_enum_compare
3297       && TREE_CODE_CLASS (code) == tcc_comparison
3298       && TREE_CODE (type1) == ENUMERAL_TYPE
3299       && TREE_CODE (type2) == ENUMERAL_TYPE
3300       && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3301     warning_at (location, OPT_Wenum_compare,
3302 		"comparison between %qT and %qT",
3303 		type1, type2);
3304 
3305   return result;
3306 }
3307 
3308 /* Return a tree for the difference of pointers OP0 and OP1.
3309    The resulting tree has type int.  */
3310 
3311 static tree
pointer_diff(location_t loc,tree op0,tree op1)3312 pointer_diff (location_t loc, tree op0, tree op1)
3313 {
3314   tree restype = ptrdiff_type_node;
3315   tree result, inttype;
3316 
3317   addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3318   addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3319   tree target_type = TREE_TYPE (TREE_TYPE (op0));
3320   tree con0, con1, lit0, lit1;
3321   tree orig_op1 = op1;
3322 
3323   /* If the operands point into different address spaces, we need to
3324      explicitly convert them to pointers into the common address space
3325      before we can subtract the numerical address values.  */
3326   if (as0 != as1)
3327     {
3328       addr_space_t as_common;
3329       tree common_type;
3330 
3331       /* Determine the common superset address space.  This is guaranteed
3332 	 to exist because the caller verified that comp_target_types
3333 	 returned non-zero.  */
3334       if (!addr_space_superset (as0, as1, &as_common))
3335 	gcc_unreachable ();
3336 
3337       common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3338       op0 = convert (common_type, op0);
3339       op1 = convert (common_type, op1);
3340     }
3341 
3342   /* Determine integer type to perform computations in.  This will usually
3343      be the same as the result type (ptrdiff_t), but may need to be a wider
3344      type if pointers for the address space are wider than ptrdiff_t.  */
3345   if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3346     inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
3347   else
3348     inttype = restype;
3349 
3350 
3351   if (TREE_CODE (target_type) == VOID_TYPE)
3352     pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3353 	     "pointer of type %<void *%> used in subtraction");
3354   if (TREE_CODE (target_type) == FUNCTION_TYPE)
3355     pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3356 	     "pointer to a function used in subtraction");
3357 
3358   /* If the conversion to ptrdiff_type does anything like widening or
3359      converting a partial to an integral mode, we get a convert_expression
3360      that is in the way to do any simplifications.
3361      (fold-const.c doesn't know that the extra bits won't be needed.
3362      split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3363      different mode in place.)
3364      So first try to find a common term here 'by hand'; we want to cover
3365      at least the cases that occur in legal static initializers.  */
3366   if (CONVERT_EXPR_P (op0)
3367       && (TYPE_PRECISION (TREE_TYPE (op0))
3368 	  == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3369     con0 = TREE_OPERAND (op0, 0);
3370   else
3371     con0 = op0;
3372   if (CONVERT_EXPR_P (op1)
3373       && (TYPE_PRECISION (TREE_TYPE (op1))
3374 	  == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3375     con1 = TREE_OPERAND (op1, 0);
3376   else
3377     con1 = op1;
3378 
3379   if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3380     {
3381       lit0 = TREE_OPERAND (con0, 1);
3382       con0 = TREE_OPERAND (con0, 0);
3383     }
3384   else
3385     lit0 = integer_zero_node;
3386 
3387   if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
3388     {
3389       lit1 = TREE_OPERAND (con1, 1);
3390       con1 = TREE_OPERAND (con1, 0);
3391     }
3392   else
3393     lit1 = integer_zero_node;
3394 
3395   if (operand_equal_p (con0, con1, 0))
3396     {
3397       op0 = lit0;
3398       op1 = lit1;
3399     }
3400 
3401 
3402   /* First do the subtraction as integers;
3403      then drop through to build the divide operator.
3404      Do not do default conversions on the minus operator
3405      in case restype is a short type.  */
3406 
3407   op0 = build_binary_op (loc,
3408 			 MINUS_EXPR, convert (inttype, op0),
3409 			 convert (inttype, op1), 0);
3410   /* This generates an error if op1 is pointer to incomplete type.  */
3411   if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
3412     error_at (loc, "arithmetic on pointer to an incomplete type");
3413 
3414   /* This generates an error if op0 is pointer to incomplete type.  */
3415   op1 = c_size_in_bytes (target_type);
3416 
3417   /* Divide by the size, in easiest possible way.  */
3418   result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3419 			    op0, convert (inttype, op1));
3420 
3421   /* Convert to final result type if necessary.  */
3422   return convert (restype, result);
3423 }
3424 
3425 /* Construct and perhaps optimize a tree representation
3426    for a unary operation.  CODE, a tree_code, specifies the operation
3427    and XARG is the operand.
3428    For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3429    the default promotions (such as from short to int).
3430    For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3431    allows non-lvalues; this is only used to handle conversion of non-lvalue
3432    arrays to pointers in C99.
3433 
3434    LOCATION is the location of the operator.  */
3435 
3436 tree
build_unary_op(location_t location,enum tree_code code,tree xarg,int flag)3437 build_unary_op (location_t location,
3438 		enum tree_code code, tree xarg, int flag)
3439 {
3440   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
3441   tree arg = xarg;
3442   tree argtype = 0;
3443   enum tree_code typecode;
3444   tree val;
3445   tree ret = error_mark_node;
3446   tree eptype = NULL_TREE;
3447   int noconvert = flag;
3448   const char *invalid_op_diag;
3449   bool int_operands;
3450 
3451   int_operands = EXPR_INT_CONST_OPERANDS (xarg);
3452   if (int_operands)
3453     arg = remove_c_maybe_const_expr (arg);
3454 
3455   if (code != ADDR_EXPR)
3456     arg = require_complete_type (arg);
3457 
3458   typecode = TREE_CODE (TREE_TYPE (arg));
3459   if (typecode == ERROR_MARK)
3460     return error_mark_node;
3461   if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3462     typecode = INTEGER_TYPE;
3463 
3464   if ((invalid_op_diag
3465        = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3466     {
3467       error_at (location, invalid_op_diag);
3468       return error_mark_node;
3469     }
3470 
3471   if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3472     {
3473       eptype = TREE_TYPE (arg);
3474       arg = TREE_OPERAND (arg, 0);
3475     }
3476 
3477   switch (code)
3478     {
3479     case CONVERT_EXPR:
3480       /* This is used for unary plus, because a CONVERT_EXPR
3481 	 is enough to prevent anybody from looking inside for
3482 	 associativity, but won't generate any code.  */
3483       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3484 	    || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3485 	    || typecode == VECTOR_TYPE))
3486 	{
3487 	  error_at (location, "wrong type argument to unary plus");
3488 	  return error_mark_node;
3489 	}
3490       else if (!noconvert)
3491 	arg = default_conversion (arg);
3492       arg = non_lvalue_loc (location, arg);
3493       break;
3494 
3495     case NEGATE_EXPR:
3496       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3497 	    || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3498 	    || typecode == VECTOR_TYPE))
3499 	{
3500 	  error_at (location, "wrong type argument to unary minus");
3501 	  return error_mark_node;
3502 	}
3503       else if (!noconvert)
3504 	arg = default_conversion (arg);
3505       break;
3506 
3507     case BIT_NOT_EXPR:
3508       /* ~ works on integer types and non float vectors. */
3509       if (typecode == INTEGER_TYPE
3510 	  || (typecode == VECTOR_TYPE
3511 	      && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
3512 	{
3513 	  if (!noconvert)
3514 	    arg = default_conversion (arg);
3515 	}
3516       else if (typecode == COMPLEX_TYPE)
3517 	{
3518 	  code = CONJ_EXPR;
3519 	  pedwarn (location, OPT_Wpedantic,
3520 		   "ISO C does not support %<~%> for complex conjugation");
3521 	  if (!noconvert)
3522 	    arg = default_conversion (arg);
3523 	}
3524       else
3525 	{
3526 	  error_at (location, "wrong type argument to bit-complement");
3527 	  return error_mark_node;
3528 	}
3529       break;
3530 
3531     case ABS_EXPR:
3532       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
3533 	{
3534 	  error_at (location, "wrong type argument to abs");
3535 	  return error_mark_node;
3536 	}
3537       else if (!noconvert)
3538 	arg = default_conversion (arg);
3539       break;
3540 
3541     case CONJ_EXPR:
3542       /* Conjugating a real value is a no-op, but allow it anyway.  */
3543       if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3544 	    || typecode == COMPLEX_TYPE))
3545 	{
3546 	  error_at (location, "wrong type argument to conjugation");
3547 	  return error_mark_node;
3548 	}
3549       else if (!noconvert)
3550 	arg = default_conversion (arg);
3551       break;
3552 
3553     case TRUTH_NOT_EXPR:
3554       if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3555 	  && typecode != REAL_TYPE && typecode != POINTER_TYPE
3556 	  && typecode != COMPLEX_TYPE)
3557 	{
3558 	  error_at (location,
3559 		    "wrong type argument to unary exclamation mark");
3560 	  return error_mark_node;
3561 	}
3562       if (int_operands)
3563 	{
3564 	  arg = c_objc_common_truthvalue_conversion (location, xarg);
3565 	  arg = remove_c_maybe_const_expr (arg);
3566 	}
3567       else
3568 	arg = c_objc_common_truthvalue_conversion (location, arg);
3569       ret = invert_truthvalue_loc (location, arg);
3570       /* If the TRUTH_NOT_EXPR has been folded, reset the location.  */
3571       if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3572 	location = EXPR_LOCATION (ret);
3573       goto return_build_unary_op;
3574 
3575     case REALPART_EXPR:
3576     case IMAGPART_EXPR:
3577       ret = build_real_imag_expr (location, code, arg);
3578       if (ret == error_mark_node)
3579 	return error_mark_node;
3580       if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3581 	eptype = TREE_TYPE (eptype);
3582       goto return_build_unary_op;
3583 
3584     case PREINCREMENT_EXPR:
3585     case POSTINCREMENT_EXPR:
3586     case PREDECREMENT_EXPR:
3587     case POSTDECREMENT_EXPR:
3588 
3589       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3590 	{
3591 	  tree inner = build_unary_op (location, code,
3592 				       C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3593 	  if (inner == error_mark_node)
3594 	    return error_mark_node;
3595 	  ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3596 			C_MAYBE_CONST_EXPR_PRE (arg), inner);
3597 	  gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3598 	  C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3599 	  goto return_build_unary_op;
3600 	}
3601 
3602       /* Complain about anything that is not a true lvalue.  In
3603 	 Objective-C, skip this check for property_refs.  */
3604       if (!objc_is_property_ref (arg)
3605 	  && !lvalue_or_else (location,
3606 			      arg, ((code == PREINCREMENT_EXPR
3607 				     || code == POSTINCREMENT_EXPR)
3608 				    ? lv_increment
3609 				    : lv_decrement)))
3610 	return error_mark_node;
3611 
3612       if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3613 	{
3614 	  if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3615 	    warning_at (location, OPT_Wc___compat,
3616 			"increment of enumeration value is invalid in C++");
3617 	  else
3618 	    warning_at (location, OPT_Wc___compat,
3619 			"decrement of enumeration value is invalid in C++");
3620 	}
3621 
3622       /* Ensure the argument is fully folded inside any SAVE_EXPR.  */
3623       arg = c_fully_fold (arg, false, NULL);
3624 
3625       /* Increment or decrement the real part of the value,
3626 	 and don't change the imaginary part.  */
3627       if (typecode == COMPLEX_TYPE)
3628 	{
3629 	  tree real, imag;
3630 
3631 	  pedwarn (location, OPT_Wpedantic,
3632 		   "ISO C does not support %<++%> and %<--%> on complex types");
3633 
3634 	  arg = stabilize_reference (arg);
3635 	  real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3636 	  imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3637 	  real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3638 	  if (real == error_mark_node || imag == error_mark_node)
3639 	    return error_mark_node;
3640 	  ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3641 			real, imag);
3642 	  goto return_build_unary_op;
3643 	}
3644 
3645       /* Report invalid types.  */
3646 
3647       if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3648 	  && typecode != INTEGER_TYPE && typecode != REAL_TYPE
3649 	  && typecode != VECTOR_TYPE)
3650 	{
3651 	  if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3652 	    error_at (location, "wrong type argument to increment");
3653 	  else
3654 	    error_at (location, "wrong type argument to decrement");
3655 
3656 	  return error_mark_node;
3657 	}
3658 
3659       {
3660 	tree inc;
3661 
3662 	argtype = TREE_TYPE (arg);
3663 
3664 	/* Compute the increment.  */
3665 
3666 	if (typecode == POINTER_TYPE)
3667 	  {
3668 	    /* If pointer target is an undefined struct,
3669 	       we just cannot know how to do the arithmetic.  */
3670 	    if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3671 	      {
3672 		if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3673 		  error_at (location,
3674 			    "increment of pointer to unknown structure");
3675 		else
3676 		  error_at (location,
3677 			    "decrement of pointer to unknown structure");
3678 	      }
3679 	    else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3680 		     || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
3681 	      {
3682 		if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3683 		  pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3684 			   "wrong type argument to increment");
3685 		else
3686 		  pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
3687 			   "wrong type argument to decrement");
3688 	      }
3689 
3690 	    inc = c_size_in_bytes (TREE_TYPE (argtype));
3691 	    inc = convert_to_ptrofftype_loc (location, inc);
3692 	  }
3693 	else if (FRACT_MODE_P (TYPE_MODE (argtype)))
3694 	  {
3695 	    /* For signed fract types, we invert ++ to -- or
3696 	       -- to ++, and change inc from 1 to -1, because
3697 	       it is not possible to represent 1 in signed fract constants.
3698 	       For unsigned fract types, the result always overflows and
3699 	       we get an undefined (original) or the maximum value.  */
3700 	    if (code == PREINCREMENT_EXPR)
3701 	      code = PREDECREMENT_EXPR;
3702 	    else if (code == PREDECREMENT_EXPR)
3703 	      code = PREINCREMENT_EXPR;
3704 	    else if (code == POSTINCREMENT_EXPR)
3705 	      code = POSTDECREMENT_EXPR;
3706 	    else /* code == POSTDECREMENT_EXPR  */
3707 	      code = POSTINCREMENT_EXPR;
3708 
3709 	    inc = integer_minus_one_node;
3710 	    inc = convert (argtype, inc);
3711 	  }
3712 	else
3713 	  {
3714 	    inc = (TREE_CODE (argtype) == VECTOR_TYPE
3715 		   ? build_one_cst (argtype)
3716 		   : integer_one_node);
3717 	    inc = convert (argtype, inc);
3718 	  }
3719 
3720 	/* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3721 	   need to ask Objective-C to build the increment or decrement
3722 	   expression for it.  */
3723 	if (objc_is_property_ref (arg))
3724 	  return objc_build_incr_expr_for_property_ref (location, code,
3725 							arg, inc);
3726 
3727 	/* Report a read-only lvalue.  */
3728 	if (TYPE_READONLY (argtype))
3729 	  {
3730 	    readonly_error (arg,
3731 			    ((code == PREINCREMENT_EXPR
3732 			      || code == POSTINCREMENT_EXPR)
3733 			     ? lv_increment : lv_decrement));
3734 	    return error_mark_node;
3735 	  }
3736 	else if (TREE_READONLY (arg))
3737 	  readonly_warning (arg,
3738 			    ((code == PREINCREMENT_EXPR
3739 			      || code == POSTINCREMENT_EXPR)
3740 			     ? lv_increment : lv_decrement));
3741 
3742 	if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3743 	  val = boolean_increment (code, arg);
3744 	else
3745 	  val = build2 (code, TREE_TYPE (arg), arg, inc);
3746 	TREE_SIDE_EFFECTS (val) = 1;
3747 	if (TREE_CODE (val) != code)
3748 	  TREE_NO_WARNING (val) = 1;
3749 	ret = val;
3750 	goto return_build_unary_op;
3751       }
3752 
3753     case ADDR_EXPR:
3754       /* Note that this operation never does default_conversion.  */
3755 
3756       /* The operand of unary '&' must be an lvalue (which excludes
3757 	 expressions of type void), or, in C99, the result of a [] or
3758 	 unary '*' operator.  */
3759       if (VOID_TYPE_P (TREE_TYPE (arg))
3760 	  && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3761 	  && (TREE_CODE (arg) != INDIRECT_REF
3762 	      || !flag_isoc99))
3763 	pedwarn (location, 0, "taking address of expression of type %<void%>");
3764 
3765       /* Let &* cancel out to simplify resulting code.  */
3766       if (TREE_CODE (arg) == INDIRECT_REF)
3767 	{
3768 	  /* Don't let this be an lvalue.  */
3769 	  if (lvalue_p (TREE_OPERAND (arg, 0)))
3770 	    return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
3771 	  ret = TREE_OPERAND (arg, 0);
3772 	  goto return_build_unary_op;
3773 	}
3774 
3775       /* For &x[y], return x+y */
3776       if (TREE_CODE (arg) == ARRAY_REF)
3777 	{
3778 	  tree op0 = TREE_OPERAND (arg, 0);
3779 	  if (!c_mark_addressable (op0))
3780 	    return error_mark_node;
3781 	}
3782 
3783       /* Anything not already handled and not a true memory reference
3784 	 or a non-lvalue array is an error.  */
3785       else if (typecode != FUNCTION_TYPE && !flag
3786 	       && !lvalue_or_else (location, arg, lv_addressof))
3787 	return error_mark_node;
3788 
3789       /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3790 	 folding later.  */
3791       if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3792 	{
3793 	  tree inner = build_unary_op (location, code,
3794 				       C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3795 	  ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3796 			C_MAYBE_CONST_EXPR_PRE (arg), inner);
3797 	  gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3798 	  C_MAYBE_CONST_EXPR_NON_CONST (ret)
3799 	    = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3800 	  goto return_build_unary_op;
3801 	}
3802 
3803       /* Ordinary case; arg is a COMPONENT_REF or a decl.  */
3804       argtype = TREE_TYPE (arg);
3805 
3806       /* If the lvalue is const or volatile, merge that into the type
3807 	 to which the address will point.  This is only needed
3808 	 for function types.  */
3809       if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
3810 	  && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3811 	  && TREE_CODE (argtype) == FUNCTION_TYPE)
3812 	{
3813 	  int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3814 	  int quals = orig_quals;
3815 
3816 	  if (TREE_READONLY (arg))
3817 	    quals |= TYPE_QUAL_CONST;
3818 	  if (TREE_THIS_VOLATILE (arg))
3819 	    quals |= TYPE_QUAL_VOLATILE;
3820 
3821 	  argtype = c_build_qualified_type (argtype, quals);
3822 	}
3823 
3824       if (!c_mark_addressable (arg))
3825 	return error_mark_node;
3826 
3827       gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3828 		  || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
3829 
3830       argtype = build_pointer_type (argtype);
3831 
3832       /* ??? Cope with user tricks that amount to offsetof.  Delete this
3833 	 when we have proper support for integer constant expressions.  */
3834       val = get_base_address (arg);
3835       if (val && TREE_CODE (val) == INDIRECT_REF
3836           && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3837 	{
3838 	  ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
3839 	  goto return_build_unary_op;
3840 	}
3841 
3842       val = build1 (ADDR_EXPR, argtype, arg);
3843 
3844       ret = val;
3845       goto return_build_unary_op;
3846 
3847     default:
3848       gcc_unreachable ();
3849     }
3850 
3851   if (argtype == 0)
3852     argtype = TREE_TYPE (arg);
3853   if (TREE_CODE (arg) == INTEGER_CST)
3854     ret = (require_constant_value
3855 	   ? fold_build1_initializer_loc (location, code, argtype, arg)
3856 	   : fold_build1_loc (location, code, argtype, arg));
3857   else
3858     ret = build1 (code, argtype, arg);
3859  return_build_unary_op:
3860   gcc_assert (ret != error_mark_node);
3861   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3862       && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3863     ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3864   else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3865     ret = note_integer_operands (ret);
3866   if (eptype)
3867     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
3868   protected_set_expr_location (ret, location);
3869   return ret;
3870 }
3871 
3872 /* Return nonzero if REF is an lvalue valid for this language.
3873    Lvalues can be assigned, unless their type has TYPE_READONLY.
3874    Lvalues can have their address taken, unless they have C_DECL_REGISTER.  */
3875 
3876 bool
lvalue_p(const_tree ref)3877 lvalue_p (const_tree ref)
3878 {
3879   const enum tree_code code = TREE_CODE (ref);
3880 
3881   switch (code)
3882     {
3883     case REALPART_EXPR:
3884     case IMAGPART_EXPR:
3885     case COMPONENT_REF:
3886       return lvalue_p (TREE_OPERAND (ref, 0));
3887 
3888     case C_MAYBE_CONST_EXPR:
3889       return lvalue_p (TREE_OPERAND (ref, 1));
3890 
3891     case COMPOUND_LITERAL_EXPR:
3892     case STRING_CST:
3893       return 1;
3894 
3895     case INDIRECT_REF:
3896     case ARRAY_REF:
3897     case VAR_DECL:
3898     case PARM_DECL:
3899     case RESULT_DECL:
3900     case ERROR_MARK:
3901       return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3902 	      && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
3903 
3904     case BIND_EXPR:
3905       return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3906 
3907     default:
3908       return 0;
3909     }
3910 }
3911 
3912 /* Give a warning for storing in something that is read-only in GCC
3913    terms but not const in ISO C terms.  */
3914 
3915 static void
readonly_warning(tree arg,enum lvalue_use use)3916 readonly_warning (tree arg, enum lvalue_use use)
3917 {
3918   switch (use)
3919     {
3920     case lv_assign:
3921       warning (0, "assignment of read-only location %qE", arg);
3922       break;
3923     case lv_increment:
3924       warning (0, "increment of read-only location %qE", arg);
3925       break;
3926     case lv_decrement:
3927       warning (0, "decrement of read-only location %qE", arg);
3928       break;
3929     default:
3930       gcc_unreachable ();
3931     }
3932   return;
3933 }
3934 
3935 
3936 /* Return nonzero if REF is an lvalue valid for this language;
3937    otherwise, print an error message and return zero.  USE says
3938    how the lvalue is being used and so selects the error message.
3939    LOCATION is the location at which any error should be reported.  */
3940 
3941 static int
lvalue_or_else(location_t loc,const_tree ref,enum lvalue_use use)3942 lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
3943 {
3944   int win = lvalue_p (ref);
3945 
3946   if (!win)
3947     lvalue_error (loc, use);
3948 
3949   return win;
3950 }
3951 
3952 /* Mark EXP saying that we need to be able to take the
3953    address of it; it should not be allocated in a register.
3954    Returns true if successful.  */
3955 
3956 bool
c_mark_addressable(tree exp)3957 c_mark_addressable (tree exp)
3958 {
3959   tree x = exp;
3960 
3961   while (1)
3962     switch (TREE_CODE (x))
3963       {
3964       case COMPONENT_REF:
3965 	if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
3966 	  {
3967 	    error
3968 	      ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3969 	    return false;
3970 	  }
3971 
3972 	/* ... fall through ...  */
3973 
3974       case ADDR_EXPR:
3975       case ARRAY_REF:
3976       case REALPART_EXPR:
3977       case IMAGPART_EXPR:
3978 	x = TREE_OPERAND (x, 0);
3979 	break;
3980 
3981       case COMPOUND_LITERAL_EXPR:
3982       case CONSTRUCTOR:
3983 	TREE_ADDRESSABLE (x) = 1;
3984 	return true;
3985 
3986       case VAR_DECL:
3987       case CONST_DECL:
3988       case PARM_DECL:
3989       case RESULT_DECL:
3990 	if (C_DECL_REGISTER (x)
3991 	    && DECL_NONLOCAL (x))
3992 	  {
3993 	    if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3994 	      {
3995 		error
3996 		  ("global register variable %qD used in nested function", x);
3997 		return false;
3998 	      }
3999 	    pedwarn (input_location, 0, "register variable %qD used in nested function", x);
4000 	  }
4001 	else if (C_DECL_REGISTER (x))
4002 	  {
4003 	    if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
4004 	      error ("address of global register variable %qD requested", x);
4005 	    else
4006 	      error ("address of register variable %qD requested", x);
4007 	    return false;
4008 	  }
4009 
4010 	/* drops in */
4011       case FUNCTION_DECL:
4012 	TREE_ADDRESSABLE (x) = 1;
4013 	/* drops out */
4014       default:
4015 	return true;
4016     }
4017 }
4018 
4019 /* Convert EXPR to TYPE, warning about conversion problems with
4020    constants.  SEMANTIC_TYPE is the type this conversion would use
4021    without excess precision. If SEMANTIC_TYPE is NULL, this function
4022    is equivalent to convert_and_check. This function is a wrapper that
4023    handles conversions that may be different than
4024    the usual ones because of excess precision.  */
4025 
4026 static tree
ep_convert_and_check(tree type,tree expr,tree semantic_type)4027 ep_convert_and_check (tree type, tree expr, tree semantic_type)
4028 {
4029   if (TREE_TYPE (expr) == type)
4030     return expr;
4031 
4032   if (!semantic_type)
4033     return convert_and_check (type, expr);
4034 
4035   if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4036       && TREE_TYPE (expr) != semantic_type)
4037     {
4038       /* For integers, we need to check the real conversion, not
4039 	 the conversion to the excess precision type.  */
4040       expr = convert_and_check (semantic_type, expr);
4041     }
4042   /* Result type is the excess precision type, which should be
4043      large enough, so do not check.  */
4044   return convert (type, expr);
4045 }
4046 
4047 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  If
4048    IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4049    if folded to an integer constant then the unselected half may
4050    contain arbitrary operations not normally permitted in constant
4051    expressions.  Set the location of the expression to LOC.  */
4052 
4053 tree
build_conditional_expr(location_t colon_loc,tree ifexp,bool ifexp_bcp,tree op1,tree op1_original_type,tree op2,tree op2_original_type)4054 build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
4055 			tree op1, tree op1_original_type, tree op2,
4056 			tree op2_original_type)
4057 {
4058   tree type1;
4059   tree type2;
4060   enum tree_code code1;
4061   enum tree_code code2;
4062   tree result_type = NULL;
4063   tree semantic_result_type = NULL;
4064   tree orig_op1 = op1, orig_op2 = op2;
4065   bool int_const, op1_int_operands, op2_int_operands, int_operands;
4066   bool ifexp_int_operands;
4067   tree ret;
4068 
4069   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4070   if (op1_int_operands)
4071     op1 = remove_c_maybe_const_expr (op1);
4072   op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4073   if (op2_int_operands)
4074     op2 = remove_c_maybe_const_expr (op2);
4075   ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4076   if (ifexp_int_operands)
4077     ifexp = remove_c_maybe_const_expr (ifexp);
4078 
4079   /* Promote both alternatives.  */
4080 
4081   if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4082     op1 = default_conversion (op1);
4083   if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4084     op2 = default_conversion (op2);
4085 
4086   if (TREE_CODE (ifexp) == ERROR_MARK
4087       || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4088       || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
4089     return error_mark_node;
4090 
4091   type1 = TREE_TYPE (op1);
4092   code1 = TREE_CODE (type1);
4093   type2 = TREE_TYPE (op2);
4094   code2 = TREE_CODE (type2);
4095 
4096   /* C90 does not permit non-lvalue arrays in conditional expressions.
4097      In C99 they will be pointers by now.  */
4098   if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4099     {
4100       error_at (colon_loc, "non-lvalue array in conditional expression");
4101       return error_mark_node;
4102     }
4103 
4104   if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4105        || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4106       && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4107 	  || code1 == COMPLEX_TYPE)
4108       && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4109 	  || code2 == COMPLEX_TYPE))
4110     {
4111       semantic_result_type = c_common_type (type1, type2);
4112       if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4113 	{
4114 	  op1 = TREE_OPERAND (op1, 0);
4115 	  type1 = TREE_TYPE (op1);
4116 	  gcc_assert (TREE_CODE (type1) == code1);
4117 	}
4118       if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4119 	{
4120 	  op2 = TREE_OPERAND (op2, 0);
4121 	  type2 = TREE_TYPE (op2);
4122 	  gcc_assert (TREE_CODE (type2) == code2);
4123 	}
4124     }
4125 
4126   if (warn_cxx_compat)
4127     {
4128       tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4129       tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4130 
4131       if (TREE_CODE (t1) == ENUMERAL_TYPE
4132 	  && TREE_CODE (t2) == ENUMERAL_TYPE
4133 	  && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4134 	warning_at (colon_loc, OPT_Wc___compat,
4135 		    ("different enum types in conditional is "
4136 		     "invalid in C++: %qT vs %qT"),
4137 		    t1, t2);
4138     }
4139 
4140   /* Quickly detect the usual case where op1 and op2 have the same type
4141      after promotion.  */
4142   if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
4143     {
4144       if (type1 == type2)
4145 	result_type = type1;
4146       else
4147 	result_type = TYPE_MAIN_VARIANT (type1);
4148     }
4149   else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
4150 	    || code1 == COMPLEX_TYPE)
4151 	   && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4152 	       || code2 == COMPLEX_TYPE))
4153     {
4154       result_type = c_common_type (type1, type2);
4155       do_warn_double_promotion (result_type, type1, type2,
4156 				"implicit conversion from %qT to %qT to "
4157 				"match other result of conditional",
4158 				colon_loc);
4159 
4160       /* If -Wsign-compare, warn here if type1 and type2 have
4161 	 different signedness.  We'll promote the signed to unsigned
4162 	 and later code won't know it used to be different.
4163 	 Do this check on the original types, so that explicit casts
4164 	 will be considered, but default promotions won't.  */
4165       if (c_inhibit_evaluation_warnings == 0)
4166 	{
4167 	  int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4168 	  int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
4169 
4170 	  if (unsigned_op1 ^ unsigned_op2)
4171 	    {
4172 	      bool ovf;
4173 
4174 	      /* Do not warn if the result type is signed, since the
4175 		 signed type will only be chosen if it can represent
4176 		 all the values of the unsigned type.  */
4177 	      if (!TYPE_UNSIGNED (result_type))
4178 		/* OK */;
4179 	      else
4180 		{
4181 		  bool op1_maybe_const = true;
4182 		  bool op2_maybe_const = true;
4183 
4184 		  /* Do not warn if the signed quantity is an
4185 		     unsuffixed integer literal (or some static
4186 		     constant expression involving such literals) and
4187 		     it is non-negative.  This warning requires the
4188 		     operands to be folded for best results, so do
4189 		     that folding in this case even without
4190 		     warn_sign_compare to avoid warning options
4191 		     possibly affecting code generation.  */
4192 		  c_inhibit_evaluation_warnings
4193 		    += (ifexp == truthvalue_false_node);
4194 		  op1 = c_fully_fold (op1, require_constant_value,
4195 				      &op1_maybe_const);
4196 		  c_inhibit_evaluation_warnings
4197 		    -= (ifexp == truthvalue_false_node);
4198 
4199 		  c_inhibit_evaluation_warnings
4200 		    += (ifexp == truthvalue_true_node);
4201 		  op2 = c_fully_fold (op2, require_constant_value,
4202 				      &op2_maybe_const);
4203 		  c_inhibit_evaluation_warnings
4204 		    -= (ifexp == truthvalue_true_node);
4205 
4206 		  if (warn_sign_compare)
4207 		    {
4208 		      if ((unsigned_op2
4209 			   && tree_expr_nonnegative_warnv_p (op1, &ovf))
4210 			  || (unsigned_op1
4211 			      && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4212 			/* OK */;
4213 		      else
4214 			warning_at (colon_loc, OPT_Wsign_compare,
4215 				    ("signed and unsigned type in "
4216 				     "conditional expression"));
4217 		    }
4218 		  if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
4219 		    op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
4220 		  if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
4221 		    op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
4222 		}
4223 	    }
4224 	}
4225     }
4226   else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4227     {
4228       if (code1 != VOID_TYPE || code2 != VOID_TYPE)
4229 	pedwarn (colon_loc, OPT_Wpedantic,
4230 		 "ISO C forbids conditional expr with only one void side");
4231       result_type = void_type_node;
4232     }
4233   else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4234     {
4235       addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4236       addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4237       addr_space_t as_common;
4238 
4239       if (comp_target_types (colon_loc, type1, type2))
4240 	result_type = common_pointer_type (type1, type2);
4241       else if (null_pointer_constant_p (orig_op1))
4242 	result_type = type2;
4243       else if (null_pointer_constant_p (orig_op2))
4244 	result_type = type1;
4245       else if (!addr_space_superset (as1, as2, &as_common))
4246 	{
4247 	  error_at (colon_loc, "pointers to disjoint address spaces "
4248 		    "used in conditional expression");
4249 	  return error_mark_node;
4250 	}
4251       else if (VOID_TYPE_P (TREE_TYPE (type1)))
4252 	{
4253 	  if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
4254 	    pedwarn (colon_loc, OPT_Wpedantic,
4255 		     "ISO C forbids conditional expr between "
4256 		     "%<void *%> and function pointer");
4257 	  result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4258 							  TREE_TYPE (type2)));
4259 	}
4260       else if (VOID_TYPE_P (TREE_TYPE (type2)))
4261 	{
4262 	  if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
4263 	    pedwarn (colon_loc, OPT_Wpedantic,
4264 		     "ISO C forbids conditional expr between "
4265 		     "%<void *%> and function pointer");
4266 	  result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4267 							  TREE_TYPE (type1)));
4268 	}
4269       /* Objective-C pointer comparisons are a bit more lenient.  */
4270       else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4271 	result_type = objc_common_type (type1, type2);
4272       else
4273 	{
4274 	  int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4275 
4276 	  pedwarn (colon_loc, 0,
4277 		   "pointer type mismatch in conditional expression");
4278 	  result_type = build_pointer_type
4279 			  (build_qualified_type (void_type_node, qual));
4280 	}
4281     }
4282   else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4283     {
4284       if (!null_pointer_constant_p (orig_op2))
4285 	pedwarn (colon_loc, 0,
4286 		 "pointer/integer type mismatch in conditional expression");
4287       else
4288 	{
4289 	  op2 = null_pointer_node;
4290 	}
4291       result_type = type1;
4292     }
4293   else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4294     {
4295       if (!null_pointer_constant_p (orig_op1))
4296 	pedwarn (colon_loc, 0,
4297 		 "pointer/integer type mismatch in conditional expression");
4298       else
4299 	{
4300 	  op1 = null_pointer_node;
4301 	}
4302       result_type = type2;
4303     }
4304 
4305   if (!result_type)
4306     {
4307       if (flag_cond_mismatch)
4308 	result_type = void_type_node;
4309       else
4310 	{
4311 	  error_at (colon_loc, "type mismatch in conditional expression");
4312 	  return error_mark_node;
4313 	}
4314     }
4315 
4316   /* Merge const and volatile flags of the incoming types.  */
4317   result_type
4318     = build_type_variant (result_type,
4319 			  TYPE_READONLY (type1) || TYPE_READONLY (type2),
4320 			  TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
4321 
4322   op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4323   op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
4324 
4325   if (ifexp_bcp && ifexp == truthvalue_true_node)
4326     {
4327       op2_int_operands = true;
4328       op1 = c_fully_fold (op1, require_constant_value, NULL);
4329     }
4330   if (ifexp_bcp && ifexp == truthvalue_false_node)
4331     {
4332       op1_int_operands = true;
4333       op2 = c_fully_fold (op2, require_constant_value, NULL);
4334     }
4335   int_const = int_operands = (ifexp_int_operands
4336 			      && op1_int_operands
4337 			      && op2_int_operands);
4338   if (int_operands)
4339     {
4340       int_const = ((ifexp == truthvalue_true_node
4341 		    && TREE_CODE (orig_op1) == INTEGER_CST
4342 		    && !TREE_OVERFLOW (orig_op1))
4343 		   || (ifexp == truthvalue_false_node
4344 		       && TREE_CODE (orig_op2) == INTEGER_CST
4345 		       && !TREE_OVERFLOW (orig_op2)));
4346     }
4347   if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
4348     ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
4349   else
4350     {
4351       if (int_operands)
4352 	{
4353 	  /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4354 	     nested inside of the expression.  */
4355 	  op1 = c_fully_fold (op1, false, NULL);
4356 	  op2 = c_fully_fold (op2, false, NULL);
4357 	}
4358       ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4359       if (int_operands)
4360 	ret = note_integer_operands (ret);
4361     }
4362   if (semantic_result_type)
4363     ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
4364 
4365   protected_set_expr_location (ret, colon_loc);
4366   return ret;
4367 }
4368 
4369 /* Return a compound expression that performs two expressions and
4370    returns the value of the second of them.
4371 
4372    LOC is the location of the COMPOUND_EXPR.  */
4373 
4374 tree
build_compound_expr(location_t loc,tree expr1,tree expr2)4375 build_compound_expr (location_t loc, tree expr1, tree expr2)
4376 {
4377   bool expr1_int_operands, expr2_int_operands;
4378   tree eptype = NULL_TREE;
4379   tree ret;
4380 
4381   expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4382   if (expr1_int_operands)
4383     expr1 = remove_c_maybe_const_expr (expr1);
4384   expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4385   if (expr2_int_operands)
4386     expr2 = remove_c_maybe_const_expr (expr2);
4387 
4388   if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4389     expr1 = TREE_OPERAND (expr1, 0);
4390   if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4391     {
4392       eptype = TREE_TYPE (expr2);
4393       expr2 = TREE_OPERAND (expr2, 0);
4394     }
4395 
4396   if (!TREE_SIDE_EFFECTS (expr1))
4397     {
4398       /* The left-hand operand of a comma expression is like an expression
4399 	 statement: with -Wunused, we should warn if it doesn't have
4400 	 any side-effects, unless it was explicitly cast to (void).  */
4401       if (warn_unused_value)
4402 	{
4403 	  if (VOID_TYPE_P (TREE_TYPE (expr1))
4404 	      && CONVERT_EXPR_P (expr1))
4405 	    ; /* (void) a, b */
4406 	  else if (VOID_TYPE_P (TREE_TYPE (expr1))
4407 		   && TREE_CODE (expr1) == COMPOUND_EXPR
4408 		   && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
4409 	    ; /* (void) a, (void) b, c */
4410 	  else
4411 	    warning_at (loc, OPT_Wunused_value,
4412 			"left-hand operand of comma expression has no effect");
4413 	}
4414     }
4415 
4416   /* With -Wunused, we should also warn if the left-hand operand does have
4417      side-effects, but computes a value which is not used.  For example, in
4418      `foo() + bar(), baz()' the result of the `+' operator is not used,
4419      so we should issue a warning.  */
4420   else if (warn_unused_value)
4421     warn_if_unused_value (expr1, loc);
4422 
4423   if (expr2 == error_mark_node)
4424     return error_mark_node;
4425 
4426   ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4427 
4428   if (flag_isoc99
4429       && expr1_int_operands
4430       && expr2_int_operands)
4431     ret = note_integer_operands (ret);
4432 
4433   if (eptype)
4434     ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4435 
4436   protected_set_expr_location (ret, loc);
4437   return ret;
4438 }
4439 
4440 /* Issue -Wcast-qual warnings when appropriate.  TYPE is the type to
4441    which we are casting.  OTYPE is the type of the expression being
4442    cast.  Both TYPE and OTYPE are pointer types.  LOC is the location
4443    of the cast.  -Wcast-qual appeared on the command line.  Named
4444    address space qualifiers are not handled here, because they result
4445    in different warnings.  */
4446 
4447 static void
handle_warn_cast_qual(location_t loc,tree type,tree otype)4448 handle_warn_cast_qual (location_t loc, tree type, tree otype)
4449 {
4450   tree in_type = type;
4451   tree in_otype = otype;
4452   int added = 0;
4453   int discarded = 0;
4454   bool is_const;
4455 
4456   /* Check that the qualifiers on IN_TYPE are a superset of the
4457      qualifiers of IN_OTYPE.  The outermost level of POINTER_TYPE
4458      nodes is uninteresting and we stop as soon as we hit a
4459      non-POINTER_TYPE node on either type.  */
4460   do
4461     {
4462       in_otype = TREE_TYPE (in_otype);
4463       in_type = TREE_TYPE (in_type);
4464 
4465       /* GNU C allows cv-qualified function types.  'const' means the
4466 	 function is very pure, 'volatile' means it can't return.  We
4467 	 need to warn when such qualifiers are added, not when they're
4468 	 taken away.  */
4469       if (TREE_CODE (in_otype) == FUNCTION_TYPE
4470 	  && TREE_CODE (in_type) == FUNCTION_TYPE)
4471 	added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4472 		  & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
4473       else
4474 	discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4475 		      & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
4476     }
4477   while (TREE_CODE (in_type) == POINTER_TYPE
4478 	 && TREE_CODE (in_otype) == POINTER_TYPE);
4479 
4480   if (added)
4481     warning_at (loc, OPT_Wcast_qual,
4482 		"cast adds %q#v qualifier to function type", added);
4483 
4484   if (discarded)
4485     /* There are qualifiers present in IN_OTYPE that are not present
4486        in IN_TYPE.  */
4487     warning_at (loc, OPT_Wcast_qual,
4488 		"cast discards %q#v qualifier from pointer target type",
4489 		discarded);
4490 
4491   if (added || discarded)
4492     return;
4493 
4494   /* A cast from **T to const **T is unsafe, because it can cause a
4495      const value to be changed with no additional warning.  We only
4496      issue this warning if T is the same on both sides, and we only
4497      issue the warning if there are the same number of pointers on
4498      both sides, as otherwise the cast is clearly unsafe anyhow.  A
4499      cast is unsafe when a qualifier is added at one level and const
4500      is not present at all outer levels.
4501 
4502      To issue this warning, we check at each level whether the cast
4503      adds new qualifiers not already seen.  We don't need to special
4504      case function types, as they won't have the same
4505      TYPE_MAIN_VARIANT.  */
4506 
4507   if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4508     return;
4509   if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4510     return;
4511 
4512   in_type = type;
4513   in_otype = otype;
4514   is_const = TYPE_READONLY (TREE_TYPE (in_type));
4515   do
4516     {
4517       in_type = TREE_TYPE (in_type);
4518       in_otype = TREE_TYPE (in_otype);
4519       if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4520 	  && !is_const)
4521 	{
4522 	  warning_at (loc, OPT_Wcast_qual,
4523 		      "to be safe all intermediate pointers in cast from "
4524                       "%qT to %qT must be %<const%> qualified",
4525 		      otype, type);
4526 	  break;
4527 	}
4528       if (is_const)
4529 	is_const = TYPE_READONLY (in_type);
4530     }
4531   while (TREE_CODE (in_type) == POINTER_TYPE);
4532 }
4533 
4534 /* Build an expression representing a cast to type TYPE of expression EXPR.
4535    LOC is the location of the cast-- typically the open paren of the cast.  */
4536 
4537 tree
build_c_cast(location_t loc,tree type,tree expr)4538 build_c_cast (location_t loc, tree type, tree expr)
4539 {
4540   tree value;
4541 
4542   if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4543     expr = TREE_OPERAND (expr, 0);
4544 
4545   value = expr;
4546 
4547   if (type == error_mark_node || expr == error_mark_node)
4548     return error_mark_node;
4549 
4550   /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4551      only in <protocol> qualifications.  But when constructing cast expressions,
4552      the protocols do matter and must be kept around.  */
4553   if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4554     return build1 (NOP_EXPR, type, expr);
4555 
4556   type = TYPE_MAIN_VARIANT (type);
4557 
4558   if (TREE_CODE (type) == ARRAY_TYPE)
4559     {
4560       error_at (loc, "cast specifies array type");
4561       return error_mark_node;
4562     }
4563 
4564   if (TREE_CODE (type) == FUNCTION_TYPE)
4565     {
4566       error_at (loc, "cast specifies function type");
4567       return error_mark_node;
4568     }
4569 
4570   if (!VOID_TYPE_P (type))
4571     {
4572       value = require_complete_type (value);
4573       if (value == error_mark_node)
4574 	return error_mark_node;
4575     }
4576 
4577   if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4578     {
4579       if (TREE_CODE (type) == RECORD_TYPE
4580 	  || TREE_CODE (type) == UNION_TYPE)
4581 	pedwarn (loc, OPT_Wpedantic,
4582 		 "ISO C forbids casting nonscalar to the same type");
4583     }
4584   else if (TREE_CODE (type) == UNION_TYPE)
4585     {
4586       tree field;
4587 
4588       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4589 	if (TREE_TYPE (field) != error_mark_node
4590 	    && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4591 			  TYPE_MAIN_VARIANT (TREE_TYPE (value))))
4592 	  break;
4593 
4594       if (field)
4595 	{
4596 	  tree t;
4597 	  bool maybe_const = true;
4598 
4599 	  pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
4600 	  t = c_fully_fold (value, false, &maybe_const);
4601 	  t = build_constructor_single (type, field, t);
4602 	  if (!maybe_const)
4603 	    t = c_wrap_maybe_const (t, true);
4604 	  t = digest_init (loc, type, t,
4605 			   NULL_TREE, false, true, 0);
4606 	  TREE_CONSTANT (t) = TREE_CONSTANT (value);
4607 	  return t;
4608 	}
4609       error_at (loc, "cast to union type from type not present in union");
4610       return error_mark_node;
4611     }
4612   else
4613     {
4614       tree otype, ovalue;
4615 
4616       if (type == void_type_node)
4617 	{
4618 	  tree t = build1 (CONVERT_EXPR, type, value);
4619 	  SET_EXPR_LOCATION (t, loc);
4620 	  return t;
4621 	}
4622 
4623       otype = TREE_TYPE (value);
4624 
4625       /* Optionally warn about potentially worrisome casts.  */
4626       if (warn_cast_qual
4627 	  && TREE_CODE (type) == POINTER_TYPE
4628 	  && TREE_CODE (otype) == POINTER_TYPE)
4629 	handle_warn_cast_qual (loc, type, otype);
4630 
4631       /* Warn about conversions between pointers to disjoint
4632 	 address spaces.  */
4633       if (TREE_CODE (type) == POINTER_TYPE
4634 	  && TREE_CODE (otype) == POINTER_TYPE
4635 	  && !null_pointer_constant_p (value))
4636 	{
4637 	  addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4638 	  addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4639 	  addr_space_t as_common;
4640 
4641 	  if (!addr_space_superset (as_to, as_from, &as_common))
4642 	    {
4643 	      if (ADDR_SPACE_GENERIC_P (as_from))
4644 		warning_at (loc, 0, "cast to %s address space pointer "
4645 			    "from disjoint generic address space pointer",
4646 			    c_addr_space_name (as_to));
4647 
4648 	      else if (ADDR_SPACE_GENERIC_P (as_to))
4649 		warning_at (loc, 0, "cast to generic address space pointer "
4650 			    "from disjoint %s address space pointer",
4651 			    c_addr_space_name (as_from));
4652 
4653 	      else
4654 		warning_at (loc, 0, "cast to %s address space pointer "
4655 			    "from disjoint %s address space pointer",
4656 			    c_addr_space_name (as_to),
4657 			    c_addr_space_name (as_from));
4658 	    }
4659 	}
4660 
4661       /* Warn about possible alignment problems.  */
4662       if (STRICT_ALIGNMENT
4663 	  && TREE_CODE (type) == POINTER_TYPE
4664 	  && TREE_CODE (otype) == POINTER_TYPE
4665 	  && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4666 	  && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4667 	  /* Don't warn about opaque types, where the actual alignment
4668 	     restriction is unknown.  */
4669 	  && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4670 		|| TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4671 	       && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4672 	  && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
4673 	warning_at (loc, OPT_Wcast_align,
4674 		    "cast increases required alignment of target type");
4675 
4676       if (TREE_CODE (type) == INTEGER_TYPE
4677 	  && TREE_CODE (otype) == POINTER_TYPE
4678 	  && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
4679       /* Unlike conversion of integers to pointers, where the
4680          warning is disabled for converting constants because
4681          of cases such as SIG_*, warn about converting constant
4682          pointers to integers. In some cases it may cause unwanted
4683          sign extension, and a warning is appropriate.  */
4684 	warning_at (loc, OPT_Wpointer_to_int_cast,
4685 		    "cast from pointer to integer of different size");
4686 
4687       if (TREE_CODE (value) == CALL_EXPR
4688 	  && TREE_CODE (type) != TREE_CODE (otype))
4689 	warning_at (loc, OPT_Wbad_function_cast,
4690 		    "cast from function call of type %qT "
4691 		    "to non-matching type %qT", otype, type);
4692 
4693       if (TREE_CODE (type) == POINTER_TYPE
4694 	  && TREE_CODE (otype) == INTEGER_TYPE
4695 	  && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4696 	  /* Don't warn about converting any constant.  */
4697 	  && !TREE_CONSTANT (value))
4698 	warning_at (loc,
4699 		    OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4700 		    "of different size");
4701 
4702       if (warn_strict_aliasing <= 2)
4703         strict_aliasing_warning (otype, type, expr);
4704 
4705       /* If pedantic, warn for conversions between function and object
4706 	 pointer types, except for converting a null pointer constant
4707 	 to function pointer type.  */
4708       if (pedantic
4709 	  && TREE_CODE (type) == POINTER_TYPE
4710 	  && TREE_CODE (otype) == POINTER_TYPE
4711 	  && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4712 	  && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
4713 	pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
4714 		 "conversion of function pointer to object pointer type");
4715 
4716       if (pedantic
4717 	  && TREE_CODE (type) == POINTER_TYPE
4718 	  && TREE_CODE (otype) == POINTER_TYPE
4719 	  && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4720 	  && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4721 	  && !null_pointer_constant_p (value))
4722 	pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
4723 		 "conversion of object pointer to function pointer type");
4724 
4725       ovalue = value;
4726       value = convert (type, value);
4727 
4728       /* Ignore any integer overflow caused by the cast.  */
4729       if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
4730 	{
4731 	  if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
4732 	    {
4733 	      if (!TREE_OVERFLOW (value))
4734 		{
4735 		  /* Avoid clobbering a shared constant.  */
4736 		  value = copy_node (value);
4737 		  TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4738 		}
4739 	    }
4740 	  else if (TREE_OVERFLOW (value))
4741 	    /* Reset VALUE's overflow flags, ensuring constant sharing.  */
4742 	    value = build_int_cst_wide (TREE_TYPE (value),
4743 					TREE_INT_CST_LOW (value),
4744 					TREE_INT_CST_HIGH (value));
4745 	}
4746     }
4747 
4748   /* Don't let a cast be an lvalue.  */
4749   if (value == expr)
4750     value = non_lvalue_loc (loc, value);
4751 
4752   /* Don't allow the results of casting to floating-point or complex
4753      types be confused with actual constants, or casts involving
4754      integer and pointer types other than direct integer-to-integer
4755      and integer-to-pointer be confused with integer constant
4756      expressions and null pointer constants.  */
4757   if (TREE_CODE (value) == REAL_CST
4758       || TREE_CODE (value) == COMPLEX_CST
4759       || (TREE_CODE (value) == INTEGER_CST
4760 	  && !((TREE_CODE (expr) == INTEGER_CST
4761 		&& INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4762 	       || TREE_CODE (expr) == REAL_CST
4763 	       || TREE_CODE (expr) == COMPLEX_CST)))
4764       value = build1 (NOP_EXPR, type, value);
4765 
4766   if (CAN_HAVE_LOCATION_P (value))
4767     SET_EXPR_LOCATION (value, loc);
4768   return value;
4769 }
4770 
4771 /* Interpret a cast of expression EXPR to type TYPE.  LOC is the
4772    location of the open paren of the cast, or the position of the cast
4773    expr.  */
4774 tree
c_cast_expr(location_t loc,struct c_type_name * type_name,tree expr)4775 c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
4776 {
4777   tree type;
4778   tree type_expr = NULL_TREE;
4779   bool type_expr_const = true;
4780   tree ret;
4781   int saved_wsp = warn_strict_prototypes;
4782 
4783   /* This avoids warnings about unprototyped casts on
4784      integers.  E.g. "#define SIG_DFL (void(*)())0".  */
4785   if (TREE_CODE (expr) == INTEGER_CST)
4786     warn_strict_prototypes = 0;
4787   type = groktypename (type_name, &type_expr, &type_expr_const);
4788   warn_strict_prototypes = saved_wsp;
4789 
4790   ret = build_c_cast (loc, type, expr);
4791   if (type_expr)
4792     {
4793       bool inner_expr_const = true;
4794       ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
4795       ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4796       C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
4797 					     && inner_expr_const);
4798       SET_EXPR_LOCATION (ret, loc);
4799     }
4800 
4801   if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4802     SET_EXPR_LOCATION (ret, loc);
4803 
4804   /* C++ does not permits types to be defined in a cast, but it
4805      allows references to incomplete types.  */
4806   if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
4807     warning_at (loc, OPT_Wc___compat,
4808 		"defining a type in a cast is invalid in C++");
4809 
4810   return ret;
4811 }
4812 
4813 /* Build an assignment expression of lvalue LHS from value RHS.
4814    If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4815    may differ from TREE_TYPE (LHS) for an enum bitfield.
4816    MODIFYCODE is the code for a binary operator that we use
4817    to combine the old value of LHS with RHS to get the new value.
4818    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
4819    If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4820    which may differ from TREE_TYPE (RHS) for an enum value.
4821 
4822    LOCATION is the location of the MODIFYCODE operator.
4823    RHS_LOC is the location of the RHS.  */
4824 
4825 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)4826 build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
4827 		   enum tree_code modifycode,
4828 		   location_t rhs_loc, tree rhs, tree rhs_origtype)
4829 {
4830   tree result;
4831   tree newrhs;
4832   tree rhs_semantic_type = NULL_TREE;
4833   tree lhstype = TREE_TYPE (lhs);
4834   tree olhstype = lhstype;
4835   bool npc;
4836 
4837   /* Types that aren't fully specified cannot be used in assignments.  */
4838   lhs = require_complete_type (lhs);
4839 
4840   /* Avoid duplicate error messages from operands that had errors.  */
4841   if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4842     return error_mark_node;
4843 
4844   /* For ObjC properties, defer this check.  */
4845   if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
4846     return error_mark_node;
4847 
4848   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4849     {
4850       rhs_semantic_type = TREE_TYPE (rhs);
4851       rhs = TREE_OPERAND (rhs, 0);
4852     }
4853 
4854   newrhs = rhs;
4855 
4856   if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4857     {
4858       tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
4859 				      lhs_origtype, modifycode, rhs_loc, rhs,
4860 				      rhs_origtype);
4861       if (inner == error_mark_node)
4862 	return error_mark_node;
4863       result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4864 		       C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4865       gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4866       C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4867       protected_set_expr_location (result, location);
4868       return result;
4869     }
4870 
4871   /* If a binary op has been requested, combine the old LHS value with the RHS
4872      producing the value we should actually store into the LHS.  */
4873 
4874   if (modifycode != NOP_EXPR)
4875     {
4876       lhs = c_fully_fold (lhs, false, NULL);
4877       lhs = stabilize_reference (lhs);
4878       newrhs = build_binary_op (location,
4879 				modifycode, lhs, rhs, 1);
4880 
4881       /* The original type of the right hand side is no longer
4882 	 meaningful.  */
4883       rhs_origtype = NULL_TREE;
4884     }
4885 
4886   if (c_dialect_objc ())
4887     {
4888       /* Check if we are modifying an Objective-C property reference;
4889 	 if so, we need to generate setter calls.  */
4890       result = objc_maybe_build_modify_expr (lhs, newrhs);
4891       if (result)
4892 	return result;
4893 
4894       /* Else, do the check that we postponed for Objective-C.  */
4895       if (!lvalue_or_else (location, lhs, lv_assign))
4896 	return error_mark_node;
4897     }
4898 
4899   /* Give an error for storing in something that is 'const'.  */
4900 
4901   if (TYPE_READONLY (lhstype)
4902       || ((TREE_CODE (lhstype) == RECORD_TYPE
4903 	   || TREE_CODE (lhstype) == UNION_TYPE)
4904 	  && C_TYPE_FIELDS_READONLY (lhstype)))
4905     {
4906       readonly_error (lhs, lv_assign);
4907       return error_mark_node;
4908     }
4909   else if (TREE_READONLY (lhs))
4910     readonly_warning (lhs, lv_assign);
4911 
4912   /* If storing into a structure or union member,
4913      it has probably been given type `int'.
4914      Compute the type that would go with
4915      the actual amount of storage the member occupies.  */
4916 
4917   if (TREE_CODE (lhs) == COMPONENT_REF
4918       && (TREE_CODE (lhstype) == INTEGER_TYPE
4919 	  || TREE_CODE (lhstype) == BOOLEAN_TYPE
4920 	  || TREE_CODE (lhstype) == REAL_TYPE
4921 	  || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4922     lhstype = TREE_TYPE (get_unwidened (lhs, 0));
4923 
4924   /* If storing in a field that is in actuality a short or narrower than one,
4925      we must store in the field in its actual type.  */
4926 
4927   if (lhstype != TREE_TYPE (lhs))
4928     {
4929       lhs = copy_node (lhs);
4930       TREE_TYPE (lhs) = lhstype;
4931     }
4932 
4933   /* Issue -Wc++-compat warnings about an assignment to an enum type
4934      when LHS does not have its original type.  This happens for,
4935      e.g., an enum bitfield in a struct.  */
4936   if (warn_cxx_compat
4937       && lhs_origtype != NULL_TREE
4938       && lhs_origtype != lhstype
4939       && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
4940     {
4941       tree checktype = (rhs_origtype != NULL_TREE
4942 			? rhs_origtype
4943 			: TREE_TYPE (rhs));
4944       if (checktype != error_mark_node
4945 	  && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
4946 	warning_at (location, OPT_Wc___compat,
4947 		    "enum conversion in assignment is invalid in C++");
4948     }
4949 
4950   /* Convert new value to destination type.  Fold it first, then
4951      restore any excess precision information, for the sake of
4952      conversion warnings.  */
4953 
4954   npc = null_pointer_constant_p (newrhs);
4955   newrhs = c_fully_fold (newrhs, false, NULL);
4956   if (rhs_semantic_type)
4957     newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
4958   newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
4959 				   ic_assign, npc, NULL_TREE, NULL_TREE, 0);
4960   if (TREE_CODE (newrhs) == ERROR_MARK)
4961     return error_mark_node;
4962 
4963   /* Emit ObjC write barrier, if necessary.  */
4964   if (c_dialect_objc () && flag_objc_gc)
4965     {
4966       result = objc_generate_write_barrier (lhs, modifycode, newrhs);
4967       if (result)
4968 	{
4969 	  protected_set_expr_location (result, location);
4970 	  return result;
4971 	}
4972     }
4973 
4974   /* Scan operands.  */
4975 
4976   result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
4977   TREE_SIDE_EFFECTS (result) = 1;
4978   protected_set_expr_location (result, location);
4979 
4980   /* If we got the LHS in a different type for storing in,
4981      convert the result back to the nominal type of LHS
4982      so that the value we return always has the same type
4983      as the LHS argument.  */
4984 
4985   if (olhstype == TREE_TYPE (result))
4986     return result;
4987 
4988   result = convert_for_assignment (location, olhstype, result, rhs_origtype,
4989 				   ic_assign, false, NULL_TREE, NULL_TREE, 0);
4990   protected_set_expr_location (result, location);
4991   return result;
4992 }
4993 
4994 /* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
4995    This is used to implement -fplan9-extensions.  */
4996 
4997 static bool
find_anonymous_field_with_type(tree struct_type,tree type)4998 find_anonymous_field_with_type (tree struct_type, tree type)
4999 {
5000   tree field;
5001   bool found;
5002 
5003   gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5004 	      || TREE_CODE (struct_type) == UNION_TYPE);
5005   found = false;
5006   for (field = TYPE_FIELDS (struct_type);
5007        field != NULL_TREE;
5008        field = TREE_CHAIN (field))
5009     {
5010       if (DECL_NAME (field) == NULL
5011 	  && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5012 	{
5013 	  if (found)
5014 	    return false;
5015 	  found = true;
5016 	}
5017       else if (DECL_NAME (field) == NULL
5018 	       && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5019 		   || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5020 	       && find_anonymous_field_with_type (TREE_TYPE (field), type))
5021 	{
5022 	  if (found)
5023 	    return false;
5024 	  found = true;
5025 	}
5026     }
5027   return found;
5028 }
5029 
5030 /* RHS is an expression whose type is pointer to struct.  If there is
5031    an anonymous field in RHS with type TYPE, then return a pointer to
5032    that field in RHS.  This is used with -fplan9-extensions.  This
5033    returns NULL if no conversion could be found.  */
5034 
5035 static tree
convert_to_anonymous_field(location_t location,tree type,tree rhs)5036 convert_to_anonymous_field (location_t location, tree type, tree rhs)
5037 {
5038   tree rhs_struct_type, lhs_main_type;
5039   tree field, found_field;
5040   bool found_sub_field;
5041   tree ret;
5042 
5043   gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5044   rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5045   gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5046 	      || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5047 
5048   gcc_assert (POINTER_TYPE_P (type));
5049   lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5050 
5051   found_field = NULL_TREE;
5052   found_sub_field = false;
5053   for (field = TYPE_FIELDS (rhs_struct_type);
5054        field != NULL_TREE;
5055        field = TREE_CHAIN (field))
5056     {
5057       if (DECL_NAME (field) != NULL_TREE
5058 	  || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5059 	      && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5060 	continue;
5061       if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5062 	{
5063 	  if (found_field != NULL_TREE)
5064 	    return NULL_TREE;
5065 	  found_field = field;
5066 	}
5067       else if (find_anonymous_field_with_type (TREE_TYPE (field),
5068 					       lhs_main_type))
5069 	{
5070 	  if (found_field != NULL_TREE)
5071 	    return NULL_TREE;
5072 	  found_field = field;
5073 	  found_sub_field = true;
5074 	}
5075     }
5076 
5077   if (found_field == NULL_TREE)
5078     return NULL_TREE;
5079 
5080   ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5081 			 build_fold_indirect_ref (rhs), found_field,
5082 			 NULL_TREE);
5083   ret = build_fold_addr_expr_loc (location, ret);
5084 
5085   if (found_sub_field)
5086     {
5087       ret = convert_to_anonymous_field (location, type, ret);
5088       gcc_assert (ret != NULL_TREE);
5089     }
5090 
5091   return ret;
5092 }
5093 
5094 /* Convert value RHS to type TYPE as preparation for an assignment to
5095    an lvalue of type TYPE.  If ORIGTYPE is not NULL_TREE, it is the
5096    original type of RHS; this differs from TREE_TYPE (RHS) for enum
5097    types.  NULL_POINTER_CONSTANT says whether RHS was a null pointer
5098    constant before any folding.
5099    The real work of conversion is done by `convert'.
5100    The purpose of this function is to generate error messages
5101    for assignments that are not allowed in C.
5102    ERRTYPE says whether it is argument passing, assignment,
5103    initialization or return.
5104 
5105    LOCATION is the location of the RHS.
5106    FUNCTION is a tree for the function being called.
5107    PARMNUM is the number of the argument, for printing in error messages.  */
5108 
5109 static tree
convert_for_assignment(location_t location,tree type,tree rhs,tree origtype,enum impl_conv errtype,bool null_pointer_constant,tree fundecl,tree function,int parmnum)5110 convert_for_assignment (location_t location, tree type, tree rhs,
5111 			tree origtype, enum impl_conv errtype,
5112 			bool null_pointer_constant, tree fundecl,
5113 			tree function, int parmnum)
5114 {
5115   enum tree_code codel = TREE_CODE (type);
5116   tree orig_rhs = rhs;
5117   tree rhstype;
5118   enum tree_code coder;
5119   tree rname = NULL_TREE;
5120   bool objc_ok = false;
5121 
5122   if (errtype == ic_argpass)
5123     {
5124       tree selector;
5125       /* Change pointer to function to the function itself for
5126 	 diagnostics.  */
5127       if (TREE_CODE (function) == ADDR_EXPR
5128 	  && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5129 	function = TREE_OPERAND (function, 0);
5130 
5131       /* Handle an ObjC selector specially for diagnostics.  */
5132       selector = objc_message_selector ();
5133       rname = function;
5134       if (selector && parmnum > 2)
5135 	{
5136 	  rname = selector;
5137 	  parmnum -= 2;
5138 	}
5139     }
5140 
5141   /* This macro is used to emit diagnostics to ensure that all format
5142      strings are complete sentences, visible to gettext and checked at
5143      compile time.  */
5144 #define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE)             	 \
5145   do {                                                                   \
5146     switch (errtype)                                                     \
5147       {                                                                  \
5148       case ic_argpass:                                                   \
5149         if (pedwarn (LOCATION, OPT, AR, parmnum, rname))                 \
5150           inform ((fundecl && !DECL_IS_BUILTIN (fundecl))	         \
5151 	      	  ? DECL_SOURCE_LOCATION (fundecl) : LOCATION,		 \
5152                   "expected %qT but argument is of type %qT",            \
5153                   type, rhstype);                                        \
5154         break;                                                           \
5155       case ic_assign:                                                    \
5156         pedwarn (LOCATION, OPT, AS);                                     \
5157         break;                                                           \
5158       case ic_init:                                                      \
5159         pedwarn_init (LOCATION, OPT, IN);                                \
5160         break;                                                           \
5161       case ic_return:                                                    \
5162         pedwarn (LOCATION, OPT, RE);                                 	 \
5163         break;                                                           \
5164       default:                                                           \
5165         gcc_unreachable ();                                              \
5166       }                                                                  \
5167   } while (0)
5168 
5169   /* This macro is used to emit diagnostics to ensure that all format
5170      strings are complete sentences, visible to gettext and checked at
5171      compile time.  It is the same as WARN_FOR_ASSIGNMENT but with an
5172      extra parameter to enumerate qualifiers.  */
5173 
5174 #define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS)        \
5175   do {                                                                   \
5176     switch (errtype)                                                     \
5177       {                                                                  \
5178       case ic_argpass:                                                   \
5179         if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS))          \
5180           inform ((fundecl && !DECL_IS_BUILTIN (fundecl))	         \
5181 	      	  ? DECL_SOURCE_LOCATION (fundecl) : LOCATION,		 \
5182                   "expected %qT but argument is of type %qT",            \
5183                   type, rhstype);                                        \
5184         break;                                                           \
5185       case ic_assign:                                                    \
5186         pedwarn (LOCATION, OPT, AS, QUALS);                          \
5187         break;                                                           \
5188       case ic_init:                                                      \
5189         pedwarn (LOCATION, OPT, IN, QUALS);                          \
5190         break;                                                           \
5191       case ic_return:                                                    \
5192         pedwarn (LOCATION, OPT, RE, QUALS);                        	 \
5193         break;                                                           \
5194       default:                                                           \
5195         gcc_unreachable ();                                              \
5196       }                                                                  \
5197   } while (0)
5198 
5199   if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5200     rhs = TREE_OPERAND (rhs, 0);
5201 
5202   rhstype = TREE_TYPE (rhs);
5203   coder = TREE_CODE (rhstype);
5204 
5205   if (coder == ERROR_MARK)
5206     return error_mark_node;
5207 
5208   if (c_dialect_objc ())
5209     {
5210       int parmno;
5211 
5212       switch (errtype)
5213 	{
5214 	case ic_return:
5215 	  parmno = 0;
5216 	  break;
5217 
5218 	case ic_assign:
5219 	  parmno = -1;
5220 	  break;
5221 
5222 	case ic_init:
5223 	  parmno = -2;
5224 	  break;
5225 
5226 	default:
5227 	  parmno = parmnum;
5228 	  break;
5229 	}
5230 
5231       objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5232     }
5233 
5234   if (warn_cxx_compat)
5235     {
5236       tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5237       if (checktype != error_mark_node
5238 	  && TREE_CODE (type) == ENUMERAL_TYPE
5239 	  && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5240 	{
5241 	  WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5242 			       G_("enum conversion when passing argument "
5243 				  "%d of %qE is invalid in C++"),
5244 			       G_("enum conversion in assignment is "
5245 				  "invalid in C++"),
5246 			       G_("enum conversion in initialization is "
5247 				  "invalid in C++"),
5248 			       G_("enum conversion in return is "
5249 				  "invalid in C++"));
5250 	}
5251     }
5252 
5253   if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
5254     return rhs;
5255 
5256   if (coder == VOID_TYPE)
5257     {
5258       /* Except for passing an argument to an unprototyped function,
5259 	 this is a constraint violation.  When passing an argument to
5260 	 an unprototyped function, it is compile-time undefined;
5261 	 making it a constraint in that case was rejected in
5262 	 DR#252.  */
5263       error_at (location, "void value not ignored as it ought to be");
5264       return error_mark_node;
5265     }
5266   rhs = require_complete_type (rhs);
5267   if (rhs == error_mark_node)
5268     return error_mark_node;
5269   /* A type converts to a reference to it.
5270      This code doesn't fully support references, it's just for the
5271      special case of va_start and va_copy.  */
5272   if (codel == REFERENCE_TYPE
5273       && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
5274     {
5275       if (!lvalue_p (rhs))
5276 	{
5277 	  error_at (location, "cannot pass rvalue to reference parameter");
5278 	  return error_mark_node;
5279 	}
5280       if (!c_mark_addressable (rhs))
5281 	return error_mark_node;
5282       rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
5283       SET_EXPR_LOCATION (rhs, location);
5284 
5285       /* We already know that these two types are compatible, but they
5286 	 may not be exactly identical.  In fact, `TREE_TYPE (type)' is
5287 	 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5288 	 likely to be va_list, a typedef to __builtin_va_list, which
5289 	 is different enough that it will cause problems later.  */
5290       if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
5291 	{
5292 	  rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5293 	  SET_EXPR_LOCATION (rhs, location);
5294 	}
5295 
5296       rhs = build1 (NOP_EXPR, type, rhs);
5297       SET_EXPR_LOCATION (rhs, location);
5298       return rhs;
5299     }
5300   /* Some types can interconvert without explicit casts.  */
5301   else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
5302 	   && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
5303     return convert (type, rhs);
5304   /* Arithmetic types all interconvert, and enum is treated like int.  */
5305   else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
5306 	    || codel == FIXED_POINT_TYPE
5307 	    || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5308 	    || codel == BOOLEAN_TYPE)
5309 	   && (coder == INTEGER_TYPE || coder == REAL_TYPE
5310 	       || coder == FIXED_POINT_TYPE
5311 	       || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5312 	       || coder == BOOLEAN_TYPE))
5313     {
5314       tree ret;
5315       bool save = in_late_binary_op;
5316       if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5317 	in_late_binary_op = true;
5318       ret = convert_and_check (type, orig_rhs);
5319       if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
5320 	in_late_binary_op = save;
5321       return ret;
5322     }
5323 
5324   /* Aggregates in different TUs might need conversion.  */
5325   if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5326       && codel == coder
5327       && comptypes (type, rhstype))
5328     return convert_and_check (type, rhs);
5329 
5330   /* Conversion to a transparent union or record from its member types.
5331      This applies only to function arguments.  */
5332   if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5333       && TYPE_TRANSPARENT_AGGR (type))
5334       && errtype == ic_argpass)
5335     {
5336       tree memb, marginal_memb = NULL_TREE;
5337 
5338       for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
5339 	{
5340 	  tree memb_type = TREE_TYPE (memb);
5341 
5342 	  if (comptypes (TYPE_MAIN_VARIANT (memb_type),
5343 			 TYPE_MAIN_VARIANT (rhstype)))
5344 	    break;
5345 
5346 	  if (TREE_CODE (memb_type) != POINTER_TYPE)
5347 	    continue;
5348 
5349 	  if (coder == POINTER_TYPE)
5350 	    {
5351 	      tree ttl = TREE_TYPE (memb_type);
5352 	      tree ttr = TREE_TYPE (rhstype);
5353 
5354 	      /* Any non-function converts to a [const][volatile] void *
5355 		 and vice versa; otherwise, targets must be the same.
5356 		 Meanwhile, the lhs target must have all the qualifiers of
5357 		 the rhs.  */
5358 	      if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5359 		  || comp_target_types (location, memb_type, rhstype))
5360 		{
5361 		  /* If this type won't generate any warnings, use it.  */
5362 		  if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5363 		      || ((TREE_CODE (ttr) == FUNCTION_TYPE
5364 			   && TREE_CODE (ttl) == FUNCTION_TYPE)
5365 			  ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5366 			     == TYPE_QUALS (ttr))
5367 			  : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5368 			     == TYPE_QUALS (ttl))))
5369 		    break;
5370 
5371 		  /* Keep looking for a better type, but remember this one.  */
5372 		  if (!marginal_memb)
5373 		    marginal_memb = memb;
5374 		}
5375 	    }
5376 
5377 	  /* Can convert integer zero to any pointer type.  */
5378 	  if (null_pointer_constant)
5379 	    {
5380 	      rhs = null_pointer_node;
5381 	      break;
5382 	    }
5383 	}
5384 
5385       if (memb || marginal_memb)
5386 	{
5387 	  if (!memb)
5388 	    {
5389 	      /* We have only a marginally acceptable member type;
5390 		 it needs a warning.  */
5391 	      tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
5392 	      tree ttr = TREE_TYPE (rhstype);
5393 
5394 	      /* Const and volatile mean something different for function
5395 		 types, so the usual warnings are not appropriate.  */
5396 	      if (TREE_CODE (ttr) == FUNCTION_TYPE
5397 		  && TREE_CODE (ttl) == FUNCTION_TYPE)
5398 		{
5399 		  /* Because const and volatile on functions are
5400 		     restrictions that say the function will not do
5401 		     certain things, it is okay to use a const or volatile
5402 		     function where an ordinary one is wanted, but not
5403 		     vice-versa.  */
5404 		  if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5405 		      & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5406 		    WARN_FOR_QUALIFIERS (location, 0,
5407 					 G_("passing argument %d of %qE "
5408 					    "makes %q#v qualified function "
5409 					    "pointer from unqualified"),
5410 					 G_("assignment makes %q#v qualified "
5411 					    "function pointer from "
5412 					    "unqualified"),
5413 					 G_("initialization makes %q#v qualified "
5414 					    "function pointer from "
5415 					    "unqualified"),
5416 					 G_("return makes %q#v qualified function "
5417 					    "pointer from unqualified"),
5418 					 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5419 		}
5420 	      else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5421 		       & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5422 		WARN_FOR_QUALIFIERS (location, 0,
5423 				     G_("passing argument %d of %qE discards "
5424 					"%qv qualifier from pointer target type"),
5425 				     G_("assignment discards %qv qualifier "
5426 					"from pointer target type"),
5427 				     G_("initialization discards %qv qualifier "
5428 					"from pointer target type"),
5429 				     G_("return discards %qv qualifier from "
5430 					"pointer target type"),
5431 				     TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5432 
5433 	      memb = marginal_memb;
5434 	    }
5435 
5436 	  if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
5437 	    pedwarn (location, OPT_Wpedantic,
5438 		     "ISO C prohibits argument conversion to union type");
5439 
5440 	  rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
5441 	  return build_constructor_single (type, memb, rhs);
5442 	}
5443     }
5444 
5445   /* Conversions among pointers */
5446   else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5447 	   && (coder == codel))
5448     {
5449       tree ttl = TREE_TYPE (type);
5450       tree ttr = TREE_TYPE (rhstype);
5451       tree mvl = ttl;
5452       tree mvr = ttr;
5453       bool is_opaque_pointer;
5454       int target_cmp = 0;   /* Cache comp_target_types () result.  */
5455       addr_space_t asl;
5456       addr_space_t asr;
5457 
5458       if (TREE_CODE (mvl) != ARRAY_TYPE)
5459 	mvl = TYPE_MAIN_VARIANT (mvl);
5460       if (TREE_CODE (mvr) != ARRAY_TYPE)
5461 	mvr = TYPE_MAIN_VARIANT (mvr);
5462       /* Opaque pointers are treated like void pointers.  */
5463       is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
5464 
5465       /* The Plan 9 compiler permits a pointer to a struct to be
5466 	 automatically converted into a pointer to an anonymous field
5467 	 within the struct.  */
5468       if (flag_plan9_extensions
5469 	  && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5470 	  && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5471 	  && mvl != mvr)
5472 	{
5473 	  tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5474 	  if (new_rhs != NULL_TREE)
5475 	    {
5476 	      rhs = new_rhs;
5477 	      rhstype = TREE_TYPE (rhs);
5478 	      coder = TREE_CODE (rhstype);
5479 	      ttr = TREE_TYPE (rhstype);
5480 	      mvr = TYPE_MAIN_VARIANT (ttr);
5481 	    }
5482 	}
5483 
5484       /* C++ does not allow the implicit conversion void* -> T*.  However,
5485 	 for the purpose of reducing the number of false positives, we
5486 	 tolerate the special case of
5487 
5488 		int *p = NULL;
5489 
5490 	 where NULL is typically defined in C to be '(void *) 0'.  */
5491       if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
5492 	warning_at (location, OPT_Wc___compat,
5493 	    	    "request for implicit conversion "
5494 		    "from %qT to %qT not permitted in C++", rhstype, type);
5495 
5496       /* See if the pointers point to incompatible address spaces.  */
5497       asl = TYPE_ADDR_SPACE (ttl);
5498       asr = TYPE_ADDR_SPACE (ttr);
5499       if (!null_pointer_constant_p (rhs)
5500 	  && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5501 	{
5502 	  switch (errtype)
5503 	    {
5504 	    case ic_argpass:
5505 	      error_at (location, "passing argument %d of %qE from pointer to "
5506 			"non-enclosed address space", parmnum, rname);
5507 	      break;
5508 	    case ic_assign:
5509 	      error_at (location, "assignment from pointer to "
5510 			"non-enclosed address space");
5511 	      break;
5512 	    case ic_init:
5513 	      error_at (location, "initialization from pointer to "
5514 			"non-enclosed address space");
5515 	      break;
5516 	    case ic_return:
5517 	      error_at (location, "return from pointer to "
5518 			"non-enclosed address space");
5519 	      break;
5520 	    default:
5521 	      gcc_unreachable ();
5522 	    }
5523 	  return error_mark_node;
5524 	}
5525 
5526       /* Check if the right-hand side has a format attribute but the
5527 	 left-hand side doesn't.  */
5528       if (warn_suggest_attribute_format
5529 	  && check_missing_format_attribute (type, rhstype))
5530 	{
5531 	  switch (errtype)
5532 	  {
5533 	  case ic_argpass:
5534 	    warning_at (location, OPT_Wsuggest_attribute_format,
5535 			"argument %d of %qE might be "
5536 			"a candidate for a format attribute",
5537 			parmnum, rname);
5538 	    break;
5539 	  case ic_assign:
5540 	    warning_at (location, OPT_Wsuggest_attribute_format,
5541 			"assignment left-hand side might be "
5542 			"a candidate for a format attribute");
5543 	    break;
5544 	  case ic_init:
5545 	    warning_at (location, OPT_Wsuggest_attribute_format,
5546 			"initialization left-hand side might be "
5547 			"a candidate for a format attribute");
5548 	    break;
5549 	  case ic_return:
5550 	    warning_at (location, OPT_Wsuggest_attribute_format,
5551 			"return type might be "
5552 			"a candidate for a format attribute");
5553 	    break;
5554 	  default:
5555 	    gcc_unreachable ();
5556 	  }
5557 	}
5558 
5559       /* Any non-function converts to a [const][volatile] void *
5560 	 and vice versa; otherwise, targets must be the same.
5561 	 Meanwhile, the lhs target must have all the qualifiers of the rhs.  */
5562       if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5563 	  || (target_cmp = comp_target_types (location, type, rhstype))
5564 	  || is_opaque_pointer
5565 	  || ((c_common_unsigned_type (mvl)
5566 	       == c_common_unsigned_type (mvr))
5567 	      && c_common_signed_type (mvl)
5568 		 == c_common_signed_type (mvr)))
5569 	{
5570 	  if (pedantic
5571 	      && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5572 		  ||
5573 		  (VOID_TYPE_P (ttr)
5574 		   && !null_pointer_constant
5575 		   && TREE_CODE (ttl) == FUNCTION_TYPE)))
5576 	    WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
5577 				 G_("ISO C forbids passing argument %d of "
5578 				    "%qE between function pointer "
5579 				    "and %<void *%>"),
5580 				 G_("ISO C forbids assignment between "
5581 				    "function pointer and %<void *%>"),
5582 				 G_("ISO C forbids initialization between "
5583 				    "function pointer and %<void *%>"),
5584 				 G_("ISO C forbids return between function "
5585 				    "pointer and %<void *%>"));
5586 	  /* Const and volatile mean something different for function types,
5587 	     so the usual warnings are not appropriate.  */
5588 	  else if (TREE_CODE (ttr) != FUNCTION_TYPE
5589 		   && TREE_CODE (ttl) != FUNCTION_TYPE)
5590 	    {
5591 	      if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5592 		  & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5593 		{
5594 		  WARN_FOR_QUALIFIERS (location, 0,
5595 				       G_("passing argument %d of %qE discards "
5596 					  "%qv qualifier from pointer target type"),
5597 				       G_("assignment discards %qv qualifier "
5598 					  "from pointer target type"),
5599 				       G_("initialization discards %qv qualifier "
5600 					  "from pointer target type"),
5601 				       G_("return discards %qv qualifier from "
5602 					  "pointer target type"),
5603 				       TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
5604 		}
5605 	      /* If this is not a case of ignoring a mismatch in signedness,
5606 		 no warning.  */
5607 	      else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
5608 		       || target_cmp)
5609 		;
5610 	      /* If there is a mismatch, do warn.  */
5611 	      else if (warn_pointer_sign)
5612 		WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
5613 				     G_("pointer targets in passing argument "
5614 					"%d of %qE differ in signedness"),
5615 				     G_("pointer targets in assignment "
5616 					"differ in signedness"),
5617 				     G_("pointer targets in initialization "
5618 					"differ in signedness"),
5619 				     G_("pointer targets in return differ "
5620 					"in signedness"));
5621 	    }
5622 	  else if (TREE_CODE (ttl) == FUNCTION_TYPE
5623 		   && TREE_CODE (ttr) == FUNCTION_TYPE)
5624 	    {
5625 	      /* Because const and volatile on functions are restrictions
5626 		 that say the function will not do certain things,
5627 		 it is okay to use a const or volatile function
5628 		 where an ordinary one is wanted, but not vice-versa.  */
5629 	      if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5630 		  & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5631 		WARN_FOR_QUALIFIERS (location, 0,
5632 				     G_("passing argument %d of %qE makes "
5633 					"%q#v qualified function pointer "
5634 					"from unqualified"),
5635 				     G_("assignment makes %q#v qualified function "
5636 					"pointer from unqualified"),
5637 				     G_("initialization makes %q#v qualified "
5638 					"function pointer from unqualified"),
5639 				     G_("return makes %q#v qualified function "
5640 					"pointer from unqualified"),
5641 				     TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
5642 	    }
5643 	}
5644       else
5645 	/* Avoid warning about the volatile ObjC EH puts on decls.  */
5646 	if (!objc_ok)
5647 	  WARN_FOR_ASSIGNMENT (location, 0,
5648 			       G_("passing argument %d of %qE from "
5649 				  "incompatible pointer type"),
5650 			       G_("assignment from incompatible pointer type"),
5651 			       G_("initialization from incompatible "
5652 				  "pointer type"),
5653 			       G_("return from incompatible pointer type"));
5654 
5655       return convert (type, rhs);
5656     }
5657   else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5658     {
5659       /* ??? This should not be an error when inlining calls to
5660 	 unprototyped functions.  */
5661       error_at (location, "invalid use of non-lvalue array");
5662       return error_mark_node;
5663     }
5664   else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
5665     {
5666       /* An explicit constant 0 can convert to a pointer,
5667 	 or one that results from arithmetic, even including
5668 	 a cast to integer type.  */
5669       if (!null_pointer_constant)
5670 	WARN_FOR_ASSIGNMENT (location, 0,
5671 			     G_("passing argument %d of %qE makes "
5672 				"pointer from integer without a cast"),
5673 			     G_("assignment makes pointer from integer "
5674 				"without a cast"),
5675 			     G_("initialization makes pointer from "
5676 				"integer without a cast"),
5677 			     G_("return makes pointer from integer "
5678 				"without a cast"));
5679 
5680       return convert (type, rhs);
5681     }
5682   else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
5683     {
5684       WARN_FOR_ASSIGNMENT (location, 0,
5685 			   G_("passing argument %d of %qE makes integer "
5686 			      "from pointer without a cast"),
5687 			   G_("assignment makes integer from pointer "
5688 			      "without a cast"),
5689 			   G_("initialization makes integer from pointer "
5690 			      "without a cast"),
5691 			   G_("return makes integer from pointer "
5692 			      "without a cast"));
5693       return convert (type, rhs);
5694     }
5695   else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
5696     {
5697       tree ret;
5698       bool save = in_late_binary_op;
5699       in_late_binary_op = true;
5700       ret = convert (type, rhs);
5701       in_late_binary_op = save;
5702       return ret;
5703     }
5704 
5705   switch (errtype)
5706     {
5707     case ic_argpass:
5708       error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
5709       inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5710 	      ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5711 	      "expected %qT but argument is of type %qT", type, rhstype);
5712       break;
5713     case ic_assign:
5714       error_at (location, "incompatible types when assigning to type %qT from "
5715 		"type %qT", type, rhstype);
5716       break;
5717     case ic_init:
5718       error_at (location,
5719 	  	"incompatible types when initializing type %qT using type %qT",
5720 		type, rhstype);
5721       break;
5722     case ic_return:
5723       error_at (location,
5724 	  	"incompatible types when returning type %qT but %qT was "
5725 		"expected", rhstype, type);
5726       break;
5727     default:
5728       gcc_unreachable ();
5729     }
5730 
5731   return error_mark_node;
5732 }
5733 
5734 /* If VALUE is a compound expr all of whose expressions are constant, then
5735    return its value.  Otherwise, return error_mark_node.
5736 
5737    This is for handling COMPOUND_EXPRs as initializer elements
5738    which is allowed with a warning when -pedantic is specified.  */
5739 
5740 static tree
valid_compound_expr_initializer(tree value,tree endtype)5741 valid_compound_expr_initializer (tree value, tree endtype)
5742 {
5743   if (TREE_CODE (value) == COMPOUND_EXPR)
5744     {
5745       if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5746 	  == error_mark_node)
5747 	return error_mark_node;
5748       return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5749 					      endtype);
5750     }
5751   else if (!initializer_constant_valid_p (value, endtype))
5752     return error_mark_node;
5753   else
5754     return value;
5755 }
5756 
5757 /* Perform appropriate conversions on the initial value of a variable,
5758    store it in the declaration DECL,
5759    and print any error messages that are appropriate.
5760    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5761    If the init is invalid, store an ERROR_MARK.
5762 
5763    INIT_LOC is the location of the initial value.  */
5764 
5765 void
store_init_value(location_t init_loc,tree decl,tree init,tree origtype)5766 store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
5767 {
5768   tree value, type;
5769   bool npc = false;
5770 
5771   /* If variable's type was invalidly declared, just ignore it.  */
5772 
5773   type = TREE_TYPE (decl);
5774   if (TREE_CODE (type) == ERROR_MARK)
5775     return;
5776 
5777   /* Digest the specified initializer into an expression.  */
5778 
5779   if (init)
5780     npc = null_pointer_constant_p (init);
5781   value = digest_init (init_loc, type, init, origtype, npc,
5782       		       true, TREE_STATIC (decl));
5783 
5784   /* Store the expression if valid; else report error.  */
5785 
5786   if (!in_system_header
5787       && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
5788     warning (OPT_Wtraditional, "traditional C rejects automatic "
5789 	     "aggregate initialization");
5790 
5791   if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
5792     DECL_INITIAL (decl) = value;
5793 
5794   /* ANSI wants warnings about out-of-range constant initializers.  */
5795   STRIP_TYPE_NOPS (value);
5796   if (TREE_STATIC (decl))
5797     constant_expression_warning (value);
5798 
5799   /* Check if we need to set array size from compound literal size.  */
5800   if (TREE_CODE (type) == ARRAY_TYPE
5801       && TYPE_DOMAIN (type) == 0
5802       && value != error_mark_node)
5803     {
5804       tree inside_init = init;
5805 
5806       STRIP_TYPE_NOPS (inside_init);
5807       inside_init = fold (inside_init);
5808 
5809       if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5810 	{
5811 	  tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
5812 
5813 	  if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
5814 	    {
5815 	      /* For int foo[] = (int [3]){1}; we need to set array size
5816 		 now since later on array initializer will be just the
5817 		 brace enclosed list of the compound literal.  */
5818 	      tree etype = strip_array_types (TREE_TYPE (decl));
5819 	      type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5820 	      TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
5821 	      layout_type (type);
5822 	      layout_decl (cldecl, 0);
5823 	      TREE_TYPE (decl)
5824 		= c_build_qualified_type (type, TYPE_QUALS (etype));
5825 	    }
5826 	}
5827     }
5828 }
5829 
5830 /* Methods for storing and printing names for error messages.  */
5831 
5832 /* Implement a spelling stack that allows components of a name to be pushed
5833    and popped.  Each element on the stack is this structure.  */
5834 
5835 struct spelling
5836 {
5837   int kind;
5838   union
5839     {
5840       unsigned HOST_WIDE_INT i;
5841       const char *s;
5842     } u;
5843 };
5844 
5845 #define SPELLING_STRING 1
5846 #define SPELLING_MEMBER 2
5847 #define SPELLING_BOUNDS 3
5848 
5849 static struct spelling *spelling;	/* Next stack element (unused).  */
5850 static struct spelling *spelling_base;	/* Spelling stack base.  */
5851 static int spelling_size;		/* Size of the spelling stack.  */
5852 
5853 /* Macros to save and restore the spelling stack around push_... functions.
5854    Alternative to SAVE_SPELLING_STACK.  */
5855 
5856 #define SPELLING_DEPTH() (spelling - spelling_base)
5857 #define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
5858 
5859 /* Push an element on the spelling stack with type KIND and assign VALUE
5860    to MEMBER.  */
5861 
5862 #define PUSH_SPELLING(KIND, VALUE, MEMBER)				\
5863 {									\
5864   int depth = SPELLING_DEPTH ();					\
5865 									\
5866   if (depth >= spelling_size)						\
5867     {									\
5868       spelling_size += 10;						\
5869       spelling_base = XRESIZEVEC (struct spelling, spelling_base,	\
5870 				  spelling_size);			\
5871       RESTORE_SPELLING_DEPTH (depth);					\
5872     }									\
5873 									\
5874   spelling->kind = (KIND);						\
5875   spelling->MEMBER = (VALUE);						\
5876   spelling++;								\
5877 }
5878 
5879 /* Push STRING on the stack.  Printed literally.  */
5880 
5881 static void
push_string(const char * string)5882 push_string (const char *string)
5883 {
5884   PUSH_SPELLING (SPELLING_STRING, string, u.s);
5885 }
5886 
5887 /* Push a member name on the stack.  Printed as '.' STRING.  */
5888 
5889 static void
push_member_name(tree decl)5890 push_member_name (tree decl)
5891 {
5892   const char *const string
5893     = (DECL_NAME (decl)
5894        ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5895        : _("<anonymous>"));
5896   PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5897 }
5898 
5899 /* Push an array bounds on the stack.  Printed as [BOUNDS].  */
5900 
5901 static void
push_array_bounds(unsigned HOST_WIDE_INT bounds)5902 push_array_bounds (unsigned HOST_WIDE_INT bounds)
5903 {
5904   PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5905 }
5906 
5907 /* Compute the maximum size in bytes of the printed spelling.  */
5908 
5909 static int
spelling_length(void)5910 spelling_length (void)
5911 {
5912   int size = 0;
5913   struct spelling *p;
5914 
5915   for (p = spelling_base; p < spelling; p++)
5916     {
5917       if (p->kind == SPELLING_BOUNDS)
5918 	size += 25;
5919       else
5920 	size += strlen (p->u.s) + 1;
5921     }
5922 
5923   return size;
5924 }
5925 
5926 /* Print the spelling to BUFFER and return it.  */
5927 
5928 static char *
print_spelling(char * buffer)5929 print_spelling (char *buffer)
5930 {
5931   char *d = buffer;
5932   struct spelling *p;
5933 
5934   for (p = spelling_base; p < spelling; p++)
5935     if (p->kind == SPELLING_BOUNDS)
5936       {
5937 	sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
5938 	d += strlen (d);
5939       }
5940     else
5941       {
5942 	const char *s;
5943 	if (p->kind == SPELLING_MEMBER)
5944 	  *d++ = '.';
5945 	for (s = p->u.s; (*d = *s++); d++)
5946 	  ;
5947       }
5948   *d++ = '\0';
5949   return buffer;
5950 }
5951 
5952 /* Issue an error message for a bad initializer component.
5953    GMSGID identifies the message.
5954    The component name is taken from the spelling stack.  */
5955 
5956 void
error_init(const char * gmsgid)5957 error_init (const char *gmsgid)
5958 {
5959   char *ofwhat;
5960 
5961   /* The gmsgid may be a format string with %< and %>. */
5962   error (gmsgid);
5963   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5964   if (*ofwhat)
5965     error ("(near initialization for %qs)", ofwhat);
5966 }
5967 
5968 /* Issue a pedantic warning for a bad initializer component.  OPT is
5969    the option OPT_* (from options.h) controlling this warning or 0 if
5970    it is unconditionally given.  GMSGID identifies the message.  The
5971    component name is taken from the spelling stack.  */
5972 
5973 void
pedwarn_init(location_t location,int opt,const char * gmsgid)5974 pedwarn_init (location_t location, int opt, const char *gmsgid)
5975 {
5976   char *ofwhat;
5977 
5978   /* The gmsgid may be a format string with %< and %>. */
5979   pedwarn (location, opt, gmsgid);
5980   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5981   if (*ofwhat)
5982     pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5983 }
5984 
5985 /* Issue a warning for a bad initializer component.
5986 
5987    OPT is the OPT_W* value corresponding to the warning option that
5988    controls this warning.  GMSGID identifies the message.  The
5989    component name is taken from the spelling stack.  */
5990 
5991 static void
warning_init(int opt,const char * gmsgid)5992 warning_init (int opt, const char *gmsgid)
5993 {
5994   char *ofwhat;
5995 
5996   /* The gmsgid may be a format string with %< and %>. */
5997   warning (opt, gmsgid);
5998   ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5999   if (*ofwhat)
6000     warning (opt, "(near initialization for %qs)", ofwhat);
6001 }
6002 
6003 /* If TYPE is an array type and EXPR is a parenthesized string
6004    constant, warn if pedantic that EXPR is being used to initialize an
6005    object of type TYPE.  */
6006 
6007 void
maybe_warn_string_init(tree type,struct c_expr expr)6008 maybe_warn_string_init (tree type, struct c_expr expr)
6009 {
6010   if (pedantic
6011       && TREE_CODE (type) == ARRAY_TYPE
6012       && TREE_CODE (expr.value) == STRING_CST
6013       && expr.original_code != STRING_CST)
6014     pedwarn_init (input_location, OPT_Wpedantic,
6015 		  "array initialized from parenthesized string constant");
6016 }
6017 
6018 /* Digest the parser output INIT as an initializer for type TYPE.
6019    Return a C expression of type TYPE to represent the initial value.
6020 
6021    If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6022 
6023    NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6024 
6025    If INIT is a string constant, STRICT_STRING is true if it is
6026    unparenthesized or we should not warn here for it being parenthesized.
6027    For other types of INIT, STRICT_STRING is not used.
6028 
6029    INIT_LOC is the location of the INIT.
6030 
6031    REQUIRE_CONSTANT requests an error if non-constant initializers or
6032    elements are seen.  */
6033 
6034 static tree
digest_init(location_t init_loc,tree type,tree init,tree origtype,bool null_pointer_constant,bool strict_string,int require_constant)6035 digest_init (location_t init_loc, tree type, tree init, tree origtype,
6036     	     bool null_pointer_constant, bool strict_string,
6037 	     int require_constant)
6038 {
6039   enum tree_code code = TREE_CODE (type);
6040   tree inside_init = init;
6041   tree semantic_type = NULL_TREE;
6042   bool maybe_const = true;
6043 
6044   if (type == error_mark_node
6045       || !init
6046       || init == error_mark_node
6047       || TREE_TYPE (init) == error_mark_node)
6048     return error_mark_node;
6049 
6050   STRIP_TYPE_NOPS (inside_init);
6051 
6052   if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6053     {
6054       semantic_type = TREE_TYPE (inside_init);
6055       inside_init = TREE_OPERAND (inside_init, 0);
6056     }
6057   inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6058   inside_init = decl_constant_value_for_optimization (inside_init);
6059 
6060   /* Initialization of an array of chars from a string constant
6061      optionally enclosed in braces.  */
6062 
6063   if (code == ARRAY_TYPE && inside_init
6064       && TREE_CODE (inside_init) == STRING_CST)
6065     {
6066       tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
6067       /* Note that an array could be both an array of character type
6068 	 and an array of wchar_t if wchar_t is signed char or unsigned
6069 	 char.  */
6070       bool char_array = (typ1 == char_type_node
6071 			 || typ1 == signed_char_type_node
6072 			 || typ1 == unsigned_char_type_node);
6073       bool wchar_array = !!comptypes (typ1, wchar_type_node);
6074       bool char16_array = !!comptypes (typ1, char16_type_node);
6075       bool char32_array = !!comptypes (typ1, char32_type_node);
6076 
6077       if (char_array || wchar_array || char16_array || char32_array)
6078 	{
6079 	  struct c_expr expr;
6080 	  tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
6081 	  expr.value = inside_init;
6082 	  expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6083 	  expr.original_type = NULL;
6084 	  maybe_warn_string_init (type, expr);
6085 
6086 	  if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
6087 	    pedwarn_init (init_loc, OPT_Wpedantic,
6088 			  "initialization of a flexible array member");
6089 
6090 	  if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6091 			 TYPE_MAIN_VARIANT (type)))
6092 	    return inside_init;
6093 
6094 	  if (char_array)
6095 	    {
6096 	      if (typ2 != char_type_node)
6097 		{
6098 		  error_init ("char-array initialized from wide string");
6099 		  return error_mark_node;
6100 		}
6101 	    }
6102 	  else
6103 	    {
6104 	      if (typ2 == char_type_node)
6105 		{
6106 		  error_init ("wide character array initialized from non-wide "
6107 			      "string");
6108 		  return error_mark_node;
6109 		}
6110 	      else if (!comptypes(typ1, typ2))
6111 		{
6112 		  error_init ("wide character array initialized from "
6113 			      "incompatible wide string");
6114 		  return error_mark_node;
6115 		}
6116 	    }
6117 
6118 	  TREE_TYPE (inside_init) = type;
6119 	  if (TYPE_DOMAIN (type) != 0
6120 	      && TYPE_SIZE (type) != 0
6121 	      && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6122 	    {
6123 	      unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6124 
6125 	      /* Subtract the size of a single (possibly wide) character
6126 		 because it's ok to ignore the terminating null char
6127 		 that is counted in the length of the constant.  */
6128 	      if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6129 					(len
6130 					 - (TYPE_PRECISION (typ1)
6131 					    / BITS_PER_UNIT))))
6132 		pedwarn_init (init_loc, 0,
6133 			      ("initializer-string for array of chars "
6134 			       "is too long"));
6135 	      else if (warn_cxx_compat
6136 		       && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6137 		warning_at (init_loc, OPT_Wc___compat,
6138 			    ("initializer-string for array chars "
6139 			     "is too long for C++"));
6140 	    }
6141 
6142 	  return inside_init;
6143 	}
6144       else if (INTEGRAL_TYPE_P (typ1))
6145 	{
6146 	  error_init ("array of inappropriate type initialized "
6147 		      "from string constant");
6148 	  return error_mark_node;
6149 	}
6150     }
6151 
6152   /* Build a VECTOR_CST from a *constant* vector constructor.  If the
6153      vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6154      below and handle as a constructor.  */
6155   if (code == VECTOR_TYPE
6156       && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
6157       && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
6158       && TREE_CONSTANT (inside_init))
6159     {
6160       if (TREE_CODE (inside_init) == VECTOR_CST
6161 	  && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6162 			TYPE_MAIN_VARIANT (type)))
6163 	return inside_init;
6164 
6165       if (TREE_CODE (inside_init) == CONSTRUCTOR)
6166 	{
6167 	  unsigned HOST_WIDE_INT ix;
6168 	  tree value;
6169 	  bool constant_p = true;
6170 
6171 	  /* Iterate through elements and check if all constructor
6172 	     elements are *_CSTs.  */
6173 	  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6174 	    if (!CONSTANT_CLASS_P (value))
6175 	      {
6176 		constant_p = false;
6177 		break;
6178 	      }
6179 
6180 	  if (constant_p)
6181 	    return build_vector_from_ctor (type,
6182 					   CONSTRUCTOR_ELTS (inside_init));
6183 	}
6184     }
6185 
6186   if (warn_sequence_point)
6187     verify_sequence_points (inside_init);
6188 
6189   /* Any type can be initialized
6190      from an expression of the same type, optionally with braces.  */
6191 
6192   if (inside_init && TREE_TYPE (inside_init) != 0
6193       && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6194 		     TYPE_MAIN_VARIANT (type))
6195 	  || (code == ARRAY_TYPE
6196 	      && comptypes (TREE_TYPE (inside_init), type))
6197 	  || (code == VECTOR_TYPE
6198 	      && comptypes (TREE_TYPE (inside_init), type))
6199 	  || (code == POINTER_TYPE
6200 	      && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
6201 	      && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
6202 			    TREE_TYPE (type)))))
6203     {
6204       if (code == POINTER_TYPE)
6205 	{
6206 	  if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6207 	    {
6208 	      if (TREE_CODE (inside_init) == STRING_CST
6209 		  || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6210 		inside_init = array_to_pointer_conversion
6211 		  (init_loc, inside_init);
6212 	      else
6213 		{
6214 		  error_init ("invalid use of non-lvalue array");
6215 		  return error_mark_node;
6216 		}
6217 	    }
6218 	}
6219 
6220       if (code == VECTOR_TYPE)
6221 	/* Although the types are compatible, we may require a
6222 	   conversion.  */
6223 	inside_init = convert (type, inside_init);
6224 
6225       if (require_constant
6226 	  && (code == VECTOR_TYPE || !flag_isoc99)
6227 	  && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6228 	{
6229 	  /* As an extension, allow initializing objects with static storage
6230 	     duration with compound literals (which are then treated just as
6231 	     the brace enclosed list they contain).  Also allow this for
6232 	     vectors, as we can only assign them with compound literals.  */
6233 	  tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6234 	  inside_init = DECL_INITIAL (decl);
6235 	}
6236 
6237       if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6238 	  && TREE_CODE (inside_init) != CONSTRUCTOR)
6239 	{
6240 	  error_init ("array initialized from non-constant array expression");
6241 	  return error_mark_node;
6242 	}
6243 
6244       /* Compound expressions can only occur here if -Wpedantic or
6245 	 -pedantic-errors is specified.  In the later case, we always want
6246 	 an error.  In the former case, we simply want a warning.  */
6247       if (require_constant && pedantic
6248 	  && TREE_CODE (inside_init) == COMPOUND_EXPR)
6249 	{
6250 	  inside_init
6251 	    = valid_compound_expr_initializer (inside_init,
6252 					       TREE_TYPE (inside_init));
6253 	  if (inside_init == error_mark_node)
6254 	    error_init ("initializer element is not constant");
6255 	  else
6256 	    pedwarn_init (init_loc, OPT_Wpedantic,
6257 			  "initializer element is not constant");
6258 	  if (flag_pedantic_errors)
6259 	    inside_init = error_mark_node;
6260 	}
6261       else if (require_constant
6262 	       && !initializer_constant_valid_p (inside_init,
6263 						 TREE_TYPE (inside_init)))
6264 	{
6265 	  error_init ("initializer element is not constant");
6266 	  inside_init = error_mark_node;
6267 	}
6268       else if (require_constant && !maybe_const)
6269 	pedwarn_init (init_loc, 0,
6270 		      "initializer element is not a constant expression");
6271 
6272       /* Added to enable additional -Wsuggest-attribute=format warnings.  */
6273       if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
6274 	inside_init = convert_for_assignment (init_loc, type, inside_init,
6275 	    				      origtype,
6276 					      ic_init, null_pointer_constant,
6277 					      NULL_TREE, NULL_TREE, 0);
6278       return inside_init;
6279     }
6280 
6281   /* Handle scalar types, including conversions.  */
6282 
6283   if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6284       || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6285       || code == COMPLEX_TYPE || code == VECTOR_TYPE)
6286     {
6287       if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6288 	  && (TREE_CODE (init) == STRING_CST
6289 	      || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
6290 	inside_init = init = array_to_pointer_conversion (init_loc, init);
6291       if (semantic_type)
6292 	inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6293 			      inside_init);
6294       inside_init
6295 	= convert_for_assignment (init_loc, type, inside_init, origtype,
6296 	    			  ic_init, null_pointer_constant,
6297 				  NULL_TREE, NULL_TREE, 0);
6298 
6299       /* Check to see if we have already given an error message.  */
6300       if (inside_init == error_mark_node)
6301 	;
6302       else if (require_constant && !TREE_CONSTANT (inside_init))
6303 	{
6304 	  error_init ("initializer element is not constant");
6305 	  inside_init = error_mark_node;
6306 	}
6307       else if (require_constant
6308 	       && !initializer_constant_valid_p (inside_init,
6309 						 TREE_TYPE (inside_init)))
6310 	{
6311 	  error_init ("initializer element is not computable at load time");
6312 	  inside_init = error_mark_node;
6313 	}
6314       else if (require_constant && !maybe_const)
6315 	pedwarn_init (init_loc, 0,
6316 		      "initializer element is not a constant expression");
6317 
6318       return inside_init;
6319     }
6320 
6321   /* Come here only for records and arrays.  */
6322 
6323   if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
6324     {
6325       error_init ("variable-sized object may not be initialized");
6326       return error_mark_node;
6327     }
6328 
6329   error_init ("invalid initializer");
6330   return error_mark_node;
6331 }
6332 
6333 /* Handle initializers that use braces.  */
6334 
6335 /* Type of object we are accumulating a constructor for.
6336    This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE.  */
6337 static tree constructor_type;
6338 
6339 /* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6340    left to fill.  */
6341 static tree constructor_fields;
6342 
6343 /* For an ARRAY_TYPE, this is the specified index
6344    at which to store the next element we get.  */
6345 static tree constructor_index;
6346 
6347 /* For an ARRAY_TYPE, this is the maximum index.  */
6348 static tree constructor_max_index;
6349 
6350 /* For a RECORD_TYPE, this is the first field not yet written out.  */
6351 static tree constructor_unfilled_fields;
6352 
6353 /* For an ARRAY_TYPE, this is the index of the first element
6354    not yet written out.  */
6355 static tree constructor_unfilled_index;
6356 
6357 /* In a RECORD_TYPE, the byte index of the next consecutive field.
6358    This is so we can generate gaps between fields, when appropriate.  */
6359 static tree constructor_bit_index;
6360 
6361 /* If we are saving up the elements rather than allocating them,
6362    this is the list of elements so far (in reverse order,
6363    most recent first).  */
6364 static vec<constructor_elt, va_gc> *constructor_elements;
6365 
6366 /* 1 if constructor should be incrementally stored into a constructor chain,
6367    0 if all the elements should be kept in AVL tree.  */
6368 static int constructor_incremental;
6369 
6370 /* 1 if so far this constructor's elements are all compile-time constants.  */
6371 static int constructor_constant;
6372 
6373 /* 1 if so far this constructor's elements are all valid address constants.  */
6374 static int constructor_simple;
6375 
6376 /* 1 if this constructor has an element that cannot be part of a
6377    constant expression.  */
6378 static int constructor_nonconst;
6379 
6380 /* 1 if this constructor is erroneous so far.  */
6381 static int constructor_erroneous;
6382 
6383 /* Structure for managing pending initializer elements, organized as an
6384    AVL tree.  */
6385 
6386 struct init_node
6387 {
6388   struct init_node *left, *right;
6389   struct init_node *parent;
6390   int balance;
6391   tree purpose;
6392   tree value;
6393   tree origtype;
6394 };
6395 
6396 /* Tree of pending elements at this constructor level.
6397    These are elements encountered out of order
6398    which belong at places we haven't reached yet in actually
6399    writing the output.
6400    Will never hold tree nodes across GC runs.  */
6401 static struct init_node *constructor_pending_elts;
6402 
6403 /* The SPELLING_DEPTH of this constructor.  */
6404 static int constructor_depth;
6405 
6406 /* DECL node for which an initializer is being read.
6407    0 means we are reading a constructor expression
6408    such as (struct foo) {...}.  */
6409 static tree constructor_decl;
6410 
6411 /* Nonzero if this is an initializer for a top-level decl.  */
6412 static int constructor_top_level;
6413 
6414 /* Nonzero if there were any member designators in this initializer.  */
6415 static int constructor_designated;
6416 
6417 /* Nesting depth of designator list.  */
6418 static int designator_depth;
6419 
6420 /* Nonzero if there were diagnosed errors in this designator list.  */
6421 static int designator_erroneous;
6422 
6423 
6424 /* This stack has a level for each implicit or explicit level of
6425    structuring in the initializer, including the outermost one.  It
6426    saves the values of most of the variables above.  */
6427 
6428 struct constructor_range_stack;
6429 
6430 struct constructor_stack
6431 {
6432   struct constructor_stack *next;
6433   tree type;
6434   tree fields;
6435   tree index;
6436   tree max_index;
6437   tree unfilled_index;
6438   tree unfilled_fields;
6439   tree bit_index;
6440   vec<constructor_elt, va_gc> *elements;
6441   struct init_node *pending_elts;
6442   int offset;
6443   int depth;
6444   /* If value nonzero, this value should replace the entire
6445      constructor at this level.  */
6446   struct c_expr replacement_value;
6447   struct constructor_range_stack *range_stack;
6448   char constant;
6449   char simple;
6450   char nonconst;
6451   char implicit;
6452   char erroneous;
6453   char outer;
6454   char incremental;
6455   char designated;
6456 };
6457 
6458 static struct constructor_stack *constructor_stack;
6459 
6460 /* This stack represents designators from some range designator up to
6461    the last designator in the list.  */
6462 
6463 struct constructor_range_stack
6464 {
6465   struct constructor_range_stack *next, *prev;
6466   struct constructor_stack *stack;
6467   tree range_start;
6468   tree index;
6469   tree range_end;
6470   tree fields;
6471 };
6472 
6473 static struct constructor_range_stack *constructor_range_stack;
6474 
6475 /* This stack records separate initializers that are nested.
6476    Nested initializers can't happen in ANSI C, but GNU C allows them
6477    in cases like { ... (struct foo) { ... } ... }.  */
6478 
6479 struct initializer_stack
6480 {
6481   struct initializer_stack *next;
6482   tree decl;
6483   struct constructor_stack *constructor_stack;
6484   struct constructor_range_stack *constructor_range_stack;
6485   vec<constructor_elt, va_gc> *elements;
6486   struct spelling *spelling;
6487   struct spelling *spelling_base;
6488   int spelling_size;
6489   char top_level;
6490   char require_constant_value;
6491   char require_constant_elements;
6492 };
6493 
6494 static struct initializer_stack *initializer_stack;
6495 
6496 /* Prepare to parse and output the initializer for variable DECL.  */
6497 
6498 void
start_init(tree decl,tree asmspec_tree ATTRIBUTE_UNUSED,int top_level)6499 start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
6500 {
6501   const char *locus;
6502   struct initializer_stack *p = XNEW (struct initializer_stack);
6503 
6504   p->decl = constructor_decl;
6505   p->require_constant_value = require_constant_value;
6506   p->require_constant_elements = require_constant_elements;
6507   p->constructor_stack = constructor_stack;
6508   p->constructor_range_stack = constructor_range_stack;
6509   p->elements = constructor_elements;
6510   p->spelling = spelling;
6511   p->spelling_base = spelling_base;
6512   p->spelling_size = spelling_size;
6513   p->top_level = constructor_top_level;
6514   p->next = initializer_stack;
6515   initializer_stack = p;
6516 
6517   constructor_decl = decl;
6518   constructor_designated = 0;
6519   constructor_top_level = top_level;
6520 
6521   if (decl != 0 && decl != error_mark_node)
6522     {
6523       require_constant_value = TREE_STATIC (decl);
6524       require_constant_elements
6525 	= ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6526 	   /* For a scalar, you can always use any value to initialize,
6527 	      even within braces.  */
6528 	   && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6529 	       || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6530 	       || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6531 	       || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
6532       locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
6533     }
6534   else
6535     {
6536       require_constant_value = 0;
6537       require_constant_elements = 0;
6538       locus = _("(anonymous)");
6539     }
6540 
6541   constructor_stack = 0;
6542   constructor_range_stack = 0;
6543 
6544   missing_braces_mentioned = 0;
6545 
6546   spelling_base = 0;
6547   spelling_size = 0;
6548   RESTORE_SPELLING_DEPTH (0);
6549 
6550   if (locus)
6551     push_string (locus);
6552 }
6553 
6554 void
finish_init(void)6555 finish_init (void)
6556 {
6557   struct initializer_stack *p = initializer_stack;
6558 
6559   /* Free the whole constructor stack of this initializer.  */
6560   while (constructor_stack)
6561     {
6562       struct constructor_stack *q = constructor_stack;
6563       constructor_stack = q->next;
6564       free (q);
6565     }
6566 
6567   gcc_assert (!constructor_range_stack);
6568 
6569   /* Pop back to the data of the outer initializer (if any).  */
6570   free (spelling_base);
6571 
6572   constructor_decl = p->decl;
6573   require_constant_value = p->require_constant_value;
6574   require_constant_elements = p->require_constant_elements;
6575   constructor_stack = p->constructor_stack;
6576   constructor_range_stack = p->constructor_range_stack;
6577   constructor_elements = p->elements;
6578   spelling = p->spelling;
6579   spelling_base = p->spelling_base;
6580   spelling_size = p->spelling_size;
6581   constructor_top_level = p->top_level;
6582   initializer_stack = p->next;
6583   free (p);
6584 }
6585 
6586 /* Call here when we see the initializer is surrounded by braces.
6587    This is instead of a call to push_init_level;
6588    it is matched by a call to pop_init_level.
6589 
6590    TYPE is the type to initialize, for a constructor expression.
6591    For an initializer for a decl, TYPE is zero.  */
6592 
6593 void
really_start_incremental_init(tree type)6594 really_start_incremental_init (tree type)
6595 {
6596   struct constructor_stack *p = XNEW (struct constructor_stack);
6597 
6598   if (type == 0)
6599     type = TREE_TYPE (constructor_decl);
6600 
6601   if (TREE_CODE (type) == VECTOR_TYPE
6602       && TYPE_VECTOR_OPAQUE (type))
6603     error ("opaque vector types cannot be initialized");
6604 
6605   p->type = constructor_type;
6606   p->fields = constructor_fields;
6607   p->index = constructor_index;
6608   p->max_index = constructor_max_index;
6609   p->unfilled_index = constructor_unfilled_index;
6610   p->unfilled_fields = constructor_unfilled_fields;
6611   p->bit_index = constructor_bit_index;
6612   p->elements = constructor_elements;
6613   p->constant = constructor_constant;
6614   p->simple = constructor_simple;
6615   p->nonconst = constructor_nonconst;
6616   p->erroneous = constructor_erroneous;
6617   p->pending_elts = constructor_pending_elts;
6618   p->depth = constructor_depth;
6619   p->replacement_value.value = 0;
6620   p->replacement_value.original_code = ERROR_MARK;
6621   p->replacement_value.original_type = NULL;
6622   p->implicit = 0;
6623   p->range_stack = 0;
6624   p->outer = 0;
6625   p->incremental = constructor_incremental;
6626   p->designated = constructor_designated;
6627   p->next = 0;
6628   constructor_stack = p;
6629 
6630   constructor_constant = 1;
6631   constructor_simple = 1;
6632   constructor_nonconst = 0;
6633   constructor_depth = SPELLING_DEPTH ();
6634   constructor_elements = NULL;
6635   constructor_pending_elts = 0;
6636   constructor_type = type;
6637   constructor_incremental = 1;
6638   constructor_designated = 0;
6639   designator_depth = 0;
6640   designator_erroneous = 0;
6641 
6642   if (TREE_CODE (constructor_type) == RECORD_TYPE
6643       || TREE_CODE (constructor_type) == UNION_TYPE)
6644     {
6645       constructor_fields = TYPE_FIELDS (constructor_type);
6646       /* Skip any nameless bit fields at the beginning.  */
6647       while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6648 	     && DECL_NAME (constructor_fields) == 0)
6649 	constructor_fields = DECL_CHAIN (constructor_fields);
6650 
6651       constructor_unfilled_fields = constructor_fields;
6652       constructor_bit_index = bitsize_zero_node;
6653     }
6654   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6655     {
6656       if (TYPE_DOMAIN (constructor_type))
6657 	{
6658 	  constructor_max_index
6659 	    = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6660 
6661 	  /* Detect non-empty initializations of zero-length arrays.  */
6662 	  if (constructor_max_index == NULL_TREE
6663 	      && TYPE_SIZE (constructor_type))
6664 	    constructor_max_index = integer_minus_one_node;
6665 
6666 	  /* constructor_max_index needs to be an INTEGER_CST.  Attempts
6667 	     to initialize VLAs will cause a proper error; avoid tree
6668 	     checking errors as well by setting a safe value.  */
6669 	  if (constructor_max_index
6670 	      && TREE_CODE (constructor_max_index) != INTEGER_CST)
6671 	    constructor_max_index = integer_minus_one_node;
6672 
6673 	  constructor_index
6674 	    = convert (bitsizetype,
6675 		       TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6676 	}
6677       else
6678 	{
6679 	  constructor_index = bitsize_zero_node;
6680 	  constructor_max_index = NULL_TREE;
6681 	}
6682 
6683       constructor_unfilled_index = constructor_index;
6684     }
6685   else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6686     {
6687       /* Vectors are like simple fixed-size arrays.  */
6688       constructor_max_index =
6689 	bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6690       constructor_index = bitsize_zero_node;
6691       constructor_unfilled_index = constructor_index;
6692     }
6693   else
6694     {
6695       /* Handle the case of int x = {5}; */
6696       constructor_fields = constructor_type;
6697       constructor_unfilled_fields = constructor_type;
6698     }
6699 }
6700 
6701 /* Push down into a subobject, for initialization.
6702    If this is for an explicit set of braces, IMPLICIT is 0.
6703    If it is because the next element belongs at a lower level,
6704    IMPLICIT is 1 (or 2 if the push is because of designator list).  */
6705 
6706 void
push_init_level(int implicit,struct obstack * braced_init_obstack)6707 push_init_level (int implicit, struct obstack * braced_init_obstack)
6708 {
6709   struct constructor_stack *p;
6710   tree value = NULL_TREE;
6711 
6712   /* If we've exhausted any levels that didn't have braces,
6713      pop them now.  If implicit == 1, this will have been done in
6714      process_init_element; do not repeat it here because in the case
6715      of excess initializers for an empty aggregate this leads to an
6716      infinite cycle of popping a level and immediately recreating
6717      it.  */
6718   if (implicit != 1)
6719     {
6720       while (constructor_stack->implicit)
6721 	{
6722 	  if ((TREE_CODE (constructor_type) == RECORD_TYPE
6723 	       || TREE_CODE (constructor_type) == UNION_TYPE)
6724 	      && constructor_fields == 0)
6725 	    process_init_element (pop_init_level (1, braced_init_obstack),
6726 				  true, braced_init_obstack);
6727 	  else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6728 		   && constructor_max_index
6729 		   && tree_int_cst_lt (constructor_max_index,
6730 				       constructor_index))
6731 	    process_init_element (pop_init_level (1, braced_init_obstack),
6732 				  true, braced_init_obstack);
6733 	  else
6734 	    break;
6735 	}
6736     }
6737 
6738   /* Unless this is an explicit brace, we need to preserve previous
6739      content if any.  */
6740   if (implicit)
6741     {
6742       if ((TREE_CODE (constructor_type) == RECORD_TYPE
6743 	   || TREE_CODE (constructor_type) == UNION_TYPE)
6744 	  && constructor_fields)
6745 	value = find_init_member (constructor_fields, braced_init_obstack);
6746       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6747 	value = find_init_member (constructor_index, braced_init_obstack);
6748     }
6749 
6750   p = XNEW (struct constructor_stack);
6751   p->type = constructor_type;
6752   p->fields = constructor_fields;
6753   p->index = constructor_index;
6754   p->max_index = constructor_max_index;
6755   p->unfilled_index = constructor_unfilled_index;
6756   p->unfilled_fields = constructor_unfilled_fields;
6757   p->bit_index = constructor_bit_index;
6758   p->elements = constructor_elements;
6759   p->constant = constructor_constant;
6760   p->simple = constructor_simple;
6761   p->nonconst = constructor_nonconst;
6762   p->erroneous = constructor_erroneous;
6763   p->pending_elts = constructor_pending_elts;
6764   p->depth = constructor_depth;
6765   p->replacement_value.value = 0;
6766   p->replacement_value.original_code = ERROR_MARK;
6767   p->replacement_value.original_type = NULL;
6768   p->implicit = implicit;
6769   p->outer = 0;
6770   p->incremental = constructor_incremental;
6771   p->designated = constructor_designated;
6772   p->next = constructor_stack;
6773   p->range_stack = 0;
6774   constructor_stack = p;
6775 
6776   constructor_constant = 1;
6777   constructor_simple = 1;
6778   constructor_nonconst = 0;
6779   constructor_depth = SPELLING_DEPTH ();
6780   constructor_elements = NULL;
6781   constructor_incremental = 1;
6782   constructor_designated = 0;
6783   constructor_pending_elts = 0;
6784   if (!implicit)
6785     {
6786       p->range_stack = constructor_range_stack;
6787       constructor_range_stack = 0;
6788       designator_depth = 0;
6789       designator_erroneous = 0;
6790     }
6791 
6792   /* Don't die if an entire brace-pair level is superfluous
6793      in the containing level.  */
6794   if (constructor_type == 0)
6795     ;
6796   else if (TREE_CODE (constructor_type) == RECORD_TYPE
6797 	   || TREE_CODE (constructor_type) == UNION_TYPE)
6798     {
6799       /* Don't die if there are extra init elts at the end.  */
6800       if (constructor_fields == 0)
6801 	constructor_type = 0;
6802       else
6803 	{
6804 	  constructor_type = TREE_TYPE (constructor_fields);
6805 	  push_member_name (constructor_fields);
6806 	  constructor_depth++;
6807 	}
6808     }
6809   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6810     {
6811       constructor_type = TREE_TYPE (constructor_type);
6812       push_array_bounds (tree_low_cst (constructor_index, 1));
6813       constructor_depth++;
6814     }
6815 
6816   if (constructor_type == 0)
6817     {
6818       error_init ("extra brace group at end of initializer");
6819       constructor_fields = 0;
6820       constructor_unfilled_fields = 0;
6821       return;
6822     }
6823 
6824   if (value && TREE_CODE (value) == CONSTRUCTOR)
6825     {
6826       constructor_constant = TREE_CONSTANT (value);
6827       constructor_simple = TREE_STATIC (value);
6828       constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
6829       constructor_elements = CONSTRUCTOR_ELTS (value);
6830       if (!vec_safe_is_empty (constructor_elements)
6831 	  && (TREE_CODE (constructor_type) == RECORD_TYPE
6832 	      || TREE_CODE (constructor_type) == ARRAY_TYPE))
6833 	set_nonincremental_init (braced_init_obstack);
6834     }
6835 
6836   if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6837     {
6838       missing_braces_mentioned = 1;
6839       warning_init (OPT_Wmissing_braces, "missing braces around initializer");
6840     }
6841 
6842   if (TREE_CODE (constructor_type) == RECORD_TYPE
6843 	   || TREE_CODE (constructor_type) == UNION_TYPE)
6844     {
6845       constructor_fields = TYPE_FIELDS (constructor_type);
6846       /* Skip any nameless bit fields at the beginning.  */
6847       while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6848 	     && DECL_NAME (constructor_fields) == 0)
6849 	constructor_fields = DECL_CHAIN (constructor_fields);
6850 
6851       constructor_unfilled_fields = constructor_fields;
6852       constructor_bit_index = bitsize_zero_node;
6853     }
6854   else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6855     {
6856       /* Vectors are like simple fixed-size arrays.  */
6857       constructor_max_index =
6858 	bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6859       constructor_index = bitsize_int (0);
6860       constructor_unfilled_index = constructor_index;
6861     }
6862   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6863     {
6864       if (TYPE_DOMAIN (constructor_type))
6865 	{
6866 	  constructor_max_index
6867 	    = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
6868 
6869 	  /* Detect non-empty initializations of zero-length arrays.  */
6870 	  if (constructor_max_index == NULL_TREE
6871 	      && TYPE_SIZE (constructor_type))
6872 	    constructor_max_index = integer_minus_one_node;
6873 
6874 	  /* constructor_max_index needs to be an INTEGER_CST.  Attempts
6875 	     to initialize VLAs will cause a proper error; avoid tree
6876 	     checking errors as well by setting a safe value.  */
6877 	  if (constructor_max_index
6878 	      && TREE_CODE (constructor_max_index) != INTEGER_CST)
6879 	    constructor_max_index = integer_minus_one_node;
6880 
6881 	  constructor_index
6882 	    = convert (bitsizetype,
6883 		       TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6884 	}
6885       else
6886 	constructor_index = bitsize_zero_node;
6887 
6888       constructor_unfilled_index = constructor_index;
6889       if (value && TREE_CODE (value) == STRING_CST)
6890 	{
6891 	  /* We need to split the char/wchar array into individual
6892 	     characters, so that we don't have to special case it
6893 	     everywhere.  */
6894 	  set_nonincremental_init_from_string (value, braced_init_obstack);
6895 	}
6896     }
6897   else
6898     {
6899       if (constructor_type != error_mark_node)
6900 	warning_init (0, "braces around scalar initializer");
6901       constructor_fields = constructor_type;
6902       constructor_unfilled_fields = constructor_type;
6903     }
6904 }
6905 
6906 /* At the end of an implicit or explicit brace level,
6907    finish up that level of constructor.  If a single expression
6908    with redundant braces initialized that level, return the
6909    c_expr structure for that expression.  Otherwise, the original_code
6910    element is set to ERROR_MARK.
6911    If we were outputting the elements as they are read, return 0 as the value
6912    from inner levels (process_init_element ignores that),
6913    but return error_mark_node as the value from the outermost level
6914    (that's what we want to put in DECL_INITIAL).
6915    Otherwise, return a CONSTRUCTOR expression as the value.  */
6916 
6917 struct c_expr
pop_init_level(int implicit,struct obstack * braced_init_obstack)6918 pop_init_level (int implicit, struct obstack * braced_init_obstack)
6919 {
6920   struct constructor_stack *p;
6921   struct c_expr ret;
6922   ret.value = 0;
6923   ret.original_code = ERROR_MARK;
6924   ret.original_type = NULL;
6925 
6926   if (implicit == 0)
6927     {
6928       /* When we come to an explicit close brace,
6929 	 pop any inner levels that didn't have explicit braces.  */
6930       while (constructor_stack->implicit)
6931 	{
6932 	  process_init_element (pop_init_level (1, braced_init_obstack),
6933 				true, braced_init_obstack);
6934 	}
6935       gcc_assert (!constructor_range_stack);
6936     }
6937 
6938   /* Now output all pending elements.  */
6939   constructor_incremental = 1;
6940   output_pending_init_elements (1, braced_init_obstack);
6941 
6942   p = constructor_stack;
6943 
6944   /* Error for initializing a flexible array member, or a zero-length
6945      array member in an inappropriate context.  */
6946   if (constructor_type && constructor_fields
6947       && TREE_CODE (constructor_type) == ARRAY_TYPE
6948       && TYPE_DOMAIN (constructor_type)
6949       && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
6950     {
6951       /* Silently discard empty initializations.  The parser will
6952 	 already have pedwarned for empty brackets.  */
6953       if (integer_zerop (constructor_unfilled_index))
6954 	constructor_type = NULL_TREE;
6955       else
6956 	{
6957 	  gcc_assert (!TYPE_SIZE (constructor_type));
6958 
6959 	  if (constructor_depth > 2)
6960 	    error_init ("initialization of flexible array member in a nested context");
6961 	  else
6962 	    pedwarn_init (input_location, OPT_Wpedantic,
6963 			  "initialization of a flexible array member");
6964 
6965 	  /* We have already issued an error message for the existence
6966 	     of a flexible array member not at the end of the structure.
6967 	     Discard the initializer so that we do not die later.  */
6968 	  if (DECL_CHAIN (constructor_fields) != NULL_TREE)
6969 	    constructor_type = NULL_TREE;
6970 	}
6971     }
6972 
6973   /* Warn when some struct elements are implicitly initialized to zero.  */
6974   if (warn_missing_field_initializers
6975       && constructor_type
6976       && TREE_CODE (constructor_type) == RECORD_TYPE
6977       && constructor_unfilled_fields)
6978     {
6979 	bool constructor_zeroinit =
6980 	 (vec_safe_length (constructor_elements) == 1
6981 	  && integer_zerop ((*constructor_elements)[0].value));
6982 
6983 	/* Do not warn for flexible array members or zero-length arrays.  */
6984 	while (constructor_unfilled_fields
6985 	       && (!DECL_SIZE (constructor_unfilled_fields)
6986 		   || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
6987 	  constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
6988 
6989 	if (constructor_unfilled_fields
6990 	    /* Do not warn if this level of the initializer uses member
6991 	       designators; it is likely to be deliberate.  */
6992 	    && !constructor_designated
6993 	    /* Do not warn about initializing with ` = {0}'.  */
6994 	    && !constructor_zeroinit)
6995 	  {
6996 	    if (warning_at (input_location, OPT_Wmissing_field_initializers,
6997 			    "missing initializer for field %qD of %qT",
6998 			    constructor_unfilled_fields,
6999 			    constructor_type))
7000 	      inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
7001 		      "%qD declared here", constructor_unfilled_fields);
7002 	  }
7003     }
7004 
7005   /* Pad out the end of the structure.  */
7006   if (p->replacement_value.value)
7007     /* If this closes a superfluous brace pair,
7008        just pass out the element between them.  */
7009     ret = p->replacement_value;
7010   else if (constructor_type == 0)
7011     ;
7012   else if (TREE_CODE (constructor_type) != RECORD_TYPE
7013 	   && TREE_CODE (constructor_type) != UNION_TYPE
7014 	   && TREE_CODE (constructor_type) != ARRAY_TYPE
7015 	   && TREE_CODE (constructor_type) != VECTOR_TYPE)
7016     {
7017       /* A nonincremental scalar initializer--just return
7018 	 the element, after verifying there is just one.  */
7019       if (vec_safe_is_empty (constructor_elements))
7020 	{
7021 	  if (!constructor_erroneous)
7022 	    error_init ("empty scalar initializer");
7023 	  ret.value = error_mark_node;
7024 	}
7025       else if (vec_safe_length (constructor_elements) != 1)
7026 	{
7027 	  error_init ("extra elements in scalar initializer");
7028 	  ret.value = (*constructor_elements)[0].value;
7029 	}
7030       else
7031 	ret.value = (*constructor_elements)[0].value;
7032     }
7033   else
7034     {
7035       if (constructor_erroneous)
7036 	ret.value = error_mark_node;
7037       else
7038 	{
7039 	  ret.value = build_constructor (constructor_type,
7040 					 constructor_elements);
7041 	  if (constructor_constant)
7042 	    TREE_CONSTANT (ret.value) = 1;
7043 	  if (constructor_constant && constructor_simple)
7044 	    TREE_STATIC (ret.value) = 1;
7045 	  if (constructor_nonconst)
7046 	    CONSTRUCTOR_NON_CONST (ret.value) = 1;
7047 	}
7048     }
7049 
7050   if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7051     {
7052       if (constructor_nonconst)
7053 	ret.original_code = C_MAYBE_CONST_EXPR;
7054       else if (ret.original_code == C_MAYBE_CONST_EXPR)
7055 	ret.original_code = ERROR_MARK;
7056     }
7057 
7058   constructor_type = p->type;
7059   constructor_fields = p->fields;
7060   constructor_index = p->index;
7061   constructor_max_index = p->max_index;
7062   constructor_unfilled_index = p->unfilled_index;
7063   constructor_unfilled_fields = p->unfilled_fields;
7064   constructor_bit_index = p->bit_index;
7065   constructor_elements = p->elements;
7066   constructor_constant = p->constant;
7067   constructor_simple = p->simple;
7068   constructor_nonconst = p->nonconst;
7069   constructor_erroneous = p->erroneous;
7070   constructor_incremental = p->incremental;
7071   constructor_designated = p->designated;
7072   constructor_pending_elts = p->pending_elts;
7073   constructor_depth = p->depth;
7074   if (!p->implicit)
7075     constructor_range_stack = p->range_stack;
7076   RESTORE_SPELLING_DEPTH (constructor_depth);
7077 
7078   constructor_stack = p->next;
7079   free (p);
7080 
7081   if (ret.value == 0 && constructor_stack == 0)
7082     ret.value = error_mark_node;
7083   return ret;
7084 }
7085 
7086 /* Common handling for both array range and field name designators.
7087    ARRAY argument is nonzero for array ranges.  Returns zero for success.  */
7088 
7089 static int
set_designator(int array,struct obstack * braced_init_obstack)7090 set_designator (int array, struct obstack * braced_init_obstack)
7091 {
7092   tree subtype;
7093   enum tree_code subcode;
7094 
7095   /* Don't die if an entire brace-pair level is superfluous
7096      in the containing level.  */
7097   if (constructor_type == 0)
7098     return 1;
7099 
7100   /* If there were errors in this designator list already, bail out
7101      silently.  */
7102   if (designator_erroneous)
7103     return 1;
7104 
7105   if (!designator_depth)
7106     {
7107       gcc_assert (!constructor_range_stack);
7108 
7109       /* Designator list starts at the level of closest explicit
7110 	 braces.  */
7111       while (constructor_stack->implicit)
7112 	{
7113 	  process_init_element (pop_init_level (1, braced_init_obstack),
7114 				true, braced_init_obstack);
7115 	}
7116       constructor_designated = 1;
7117       return 0;
7118     }
7119 
7120   switch (TREE_CODE (constructor_type))
7121     {
7122     case  RECORD_TYPE:
7123     case  UNION_TYPE:
7124       subtype = TREE_TYPE (constructor_fields);
7125       if (subtype != error_mark_node)
7126 	subtype = TYPE_MAIN_VARIANT (subtype);
7127       break;
7128     case ARRAY_TYPE:
7129       subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
7130       break;
7131     default:
7132       gcc_unreachable ();
7133     }
7134 
7135   subcode = TREE_CODE (subtype);
7136   if (array && subcode != ARRAY_TYPE)
7137     {
7138       error_init ("array index in non-array initializer");
7139       return 1;
7140     }
7141   else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7142     {
7143       error_init ("field name not in record or union initializer");
7144       return 1;
7145     }
7146 
7147   constructor_designated = 1;
7148   push_init_level (2, braced_init_obstack);
7149   return 0;
7150 }
7151 
7152 /* If there are range designators in designator list, push a new designator
7153    to constructor_range_stack.  RANGE_END is end of such stack range or
7154    NULL_TREE if there is no range designator at this level.  */
7155 
7156 static void
push_range_stack(tree range_end,struct obstack * braced_init_obstack)7157 push_range_stack (tree range_end, struct obstack * braced_init_obstack)
7158 {
7159   struct constructor_range_stack *p;
7160 
7161   p = (struct constructor_range_stack *)
7162     obstack_alloc (braced_init_obstack,
7163 		   sizeof (struct constructor_range_stack));
7164   p->prev = constructor_range_stack;
7165   p->next = 0;
7166   p->fields = constructor_fields;
7167   p->range_start = constructor_index;
7168   p->index = constructor_index;
7169   p->stack = constructor_stack;
7170   p->range_end = range_end;
7171   if (constructor_range_stack)
7172     constructor_range_stack->next = p;
7173   constructor_range_stack = p;
7174 }
7175 
7176 /* Within an array initializer, specify the next index to be initialized.
7177    FIRST is that index.  If LAST is nonzero, then initialize a range
7178    of indices, running from FIRST through LAST.  */
7179 
7180 void
set_init_index(tree first,tree last,struct obstack * braced_init_obstack)7181 set_init_index (tree first, tree last,
7182 		struct obstack * braced_init_obstack)
7183 {
7184   if (set_designator (1, braced_init_obstack))
7185     return;
7186 
7187   designator_erroneous = 1;
7188 
7189   if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7190       || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7191     {
7192       error_init ("array index in initializer not of integer type");
7193       return;
7194     }
7195 
7196   if (TREE_CODE (first) != INTEGER_CST)
7197     {
7198       first = c_fully_fold (first, false, NULL);
7199       if (TREE_CODE (first) == INTEGER_CST)
7200 	pedwarn_init (input_location, OPT_Wpedantic,
7201 		      "array index in initializer is not "
7202 		      "an integer constant expression");
7203     }
7204 
7205   if (last && TREE_CODE (last) != INTEGER_CST)
7206     {
7207       last = c_fully_fold (last, false, NULL);
7208       if (TREE_CODE (last) == INTEGER_CST)
7209 	pedwarn_init (input_location, OPT_Wpedantic,
7210 		      "array index in initializer is not "
7211 		      "an integer constant expression");
7212     }
7213 
7214   if (TREE_CODE (first) != INTEGER_CST)
7215     error_init ("nonconstant array index in initializer");
7216   else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7217     error_init ("nonconstant array index in initializer");
7218   else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7219     error_init ("array index in non-array initializer");
7220   else if (tree_int_cst_sgn (first) == -1)
7221     error_init ("array index in initializer exceeds array bounds");
7222   else if (constructor_max_index
7223 	   && tree_int_cst_lt (constructor_max_index, first))
7224     error_init ("array index in initializer exceeds array bounds");
7225   else
7226     {
7227       constant_expression_warning (first);
7228       if (last)
7229 	constant_expression_warning (last);
7230       constructor_index = convert (bitsizetype, first);
7231 
7232       if (last)
7233 	{
7234 	  if (tree_int_cst_equal (first, last))
7235 	    last = 0;
7236 	  else if (tree_int_cst_lt (last, first))
7237 	    {
7238 	      error_init ("empty index range in initializer");
7239 	      last = 0;
7240 	    }
7241 	  else
7242 	    {
7243 	      last = convert (bitsizetype, last);
7244 	      if (constructor_max_index != 0
7245 		  && tree_int_cst_lt (constructor_max_index, last))
7246 		{
7247 		  error_init ("array index range in initializer exceeds array bounds");
7248 		  last = 0;
7249 		}
7250 	    }
7251 	}
7252 
7253       designator_depth++;
7254       designator_erroneous = 0;
7255       if (constructor_range_stack || last)
7256 	push_range_stack (last, braced_init_obstack);
7257     }
7258 }
7259 
7260 /* Within a struct initializer, specify the next field to be initialized.  */
7261 
7262 void
set_init_label(tree fieldname,struct obstack * braced_init_obstack)7263 set_init_label (tree fieldname, struct obstack * braced_init_obstack)
7264 {
7265   tree field;
7266 
7267   if (set_designator (0, braced_init_obstack))
7268     return;
7269 
7270   designator_erroneous = 1;
7271 
7272   if (TREE_CODE (constructor_type) != RECORD_TYPE
7273       && TREE_CODE (constructor_type) != UNION_TYPE)
7274     {
7275       error_init ("field name not in record or union initializer");
7276       return;
7277     }
7278 
7279   field = lookup_field (constructor_type, fieldname);
7280 
7281   if (field == 0)
7282     error ("unknown field %qE specified in initializer", fieldname);
7283   else
7284     do
7285       {
7286 	constructor_fields = TREE_VALUE (field);
7287 	designator_depth++;
7288 	designator_erroneous = 0;
7289 	if (constructor_range_stack)
7290 	  push_range_stack (NULL_TREE, braced_init_obstack);
7291 	field = TREE_CHAIN (field);
7292 	if (field)
7293 	  {
7294 	    if (set_designator (0, braced_init_obstack))
7295 	      return;
7296 	  }
7297       }
7298     while (field != NULL_TREE);
7299 }
7300 
7301 /* Add a new initializer to the tree of pending initializers.  PURPOSE
7302    identifies the initializer, either array index or field in a structure.
7303    VALUE is the value of that index or field.  If ORIGTYPE is not
7304    NULL_TREE, it is the original type of VALUE.
7305 
7306    IMPLICIT is true if value comes from pop_init_level (1),
7307    the new initializer has been merged with the existing one
7308    and thus no warnings should be emitted about overriding an
7309    existing initializer.  */
7310 
7311 static void
add_pending_init(tree purpose,tree value,tree origtype,bool implicit,struct obstack * braced_init_obstack)7312 add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7313 		  struct obstack * braced_init_obstack)
7314 {
7315   struct init_node *p, **q, *r;
7316 
7317   q = &constructor_pending_elts;
7318   p = 0;
7319 
7320   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7321     {
7322       while (*q != 0)
7323 	{
7324 	  p = *q;
7325 	  if (tree_int_cst_lt (purpose, p->purpose))
7326 	    q = &p->left;
7327 	  else if (tree_int_cst_lt (p->purpose, purpose))
7328 	    q = &p->right;
7329 	  else
7330 	    {
7331 	      if (!implicit)
7332 		{
7333 		  if (TREE_SIDE_EFFECTS (p->value))
7334 		    warning_init (0, "initialized field with side-effects overwritten");
7335 		  else if (warn_override_init)
7336 		    warning_init (OPT_Woverride_init, "initialized field overwritten");
7337 		}
7338 	      p->value = value;
7339 	      p->origtype = origtype;
7340 	      return;
7341 	    }
7342 	}
7343     }
7344   else
7345     {
7346       tree bitpos;
7347 
7348       bitpos = bit_position (purpose);
7349       while (*q != NULL)
7350 	{
7351 	  p = *q;
7352 	  if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7353 	    q = &p->left;
7354 	  else if (p->purpose != purpose)
7355 	    q = &p->right;
7356 	  else
7357 	    {
7358 	      if (!implicit)
7359 		{
7360 		  if (TREE_SIDE_EFFECTS (p->value))
7361 		    warning_init (0, "initialized field with side-effects overwritten");
7362 		  else if (warn_override_init)
7363 		    warning_init (OPT_Woverride_init, "initialized field overwritten");
7364 		}
7365 	      p->value = value;
7366 	      p->origtype = origtype;
7367 	      return;
7368 	    }
7369 	}
7370     }
7371 
7372   r = (struct init_node *) obstack_alloc (braced_init_obstack,
7373 					  sizeof (struct init_node));
7374   r->purpose = purpose;
7375   r->value = value;
7376   r->origtype = origtype;
7377 
7378   *q = r;
7379   r->parent = p;
7380   r->left = 0;
7381   r->right = 0;
7382   r->balance = 0;
7383 
7384   while (p)
7385     {
7386       struct init_node *s;
7387 
7388       if (r == p->left)
7389 	{
7390 	  if (p->balance == 0)
7391 	    p->balance = -1;
7392 	  else if (p->balance < 0)
7393 	    {
7394 	      if (r->balance < 0)
7395 		{
7396 		  /* L rotation.  */
7397 		  p->left = r->right;
7398 		  if (p->left)
7399 		    p->left->parent = p;
7400 		  r->right = p;
7401 
7402 		  p->balance = 0;
7403 		  r->balance = 0;
7404 
7405 		  s = p->parent;
7406 		  p->parent = r;
7407 		  r->parent = s;
7408 		  if (s)
7409 		    {
7410 		      if (s->left == p)
7411 			s->left = r;
7412 		      else
7413 			s->right = r;
7414 		    }
7415 		  else
7416 		    constructor_pending_elts = r;
7417 		}
7418 	      else
7419 		{
7420 		  /* LR rotation.  */
7421 		  struct init_node *t = r->right;
7422 
7423 		  r->right = t->left;
7424 		  if (r->right)
7425 		    r->right->parent = r;
7426 		  t->left = r;
7427 
7428 		  p->left = t->right;
7429 		  if (p->left)
7430 		    p->left->parent = p;
7431 		  t->right = p;
7432 
7433 		  p->balance = t->balance < 0;
7434 		  r->balance = -(t->balance > 0);
7435 		  t->balance = 0;
7436 
7437 		  s = p->parent;
7438 		  p->parent = t;
7439 		  r->parent = t;
7440 		  t->parent = s;
7441 		  if (s)
7442 		    {
7443 		      if (s->left == p)
7444 			s->left = t;
7445 		      else
7446 			s->right = t;
7447 		    }
7448 		  else
7449 		    constructor_pending_elts = t;
7450 		}
7451 	      break;
7452 	    }
7453 	  else
7454 	    {
7455 	      /* p->balance == +1; growth of left side balances the node.  */
7456 	      p->balance = 0;
7457 	      break;
7458 	    }
7459 	}
7460       else /* r == p->right */
7461 	{
7462 	  if (p->balance == 0)
7463 	    /* Growth propagation from right side.  */
7464 	    p->balance++;
7465 	  else if (p->balance > 0)
7466 	    {
7467 	      if (r->balance > 0)
7468 		{
7469 		  /* R rotation.  */
7470 		  p->right = r->left;
7471 		  if (p->right)
7472 		    p->right->parent = p;
7473 		  r->left = p;
7474 
7475 		  p->balance = 0;
7476 		  r->balance = 0;
7477 
7478 		  s = p->parent;
7479 		  p->parent = r;
7480 		  r->parent = s;
7481 		  if (s)
7482 		    {
7483 		      if (s->left == p)
7484 			s->left = r;
7485 		      else
7486 			s->right = r;
7487 		    }
7488 		  else
7489 		    constructor_pending_elts = r;
7490 		}
7491 	      else /* r->balance == -1 */
7492 		{
7493 		  /* RL rotation */
7494 		  struct init_node *t = r->left;
7495 
7496 		  r->left = t->right;
7497 		  if (r->left)
7498 		    r->left->parent = r;
7499 		  t->right = r;
7500 
7501 		  p->right = t->left;
7502 		  if (p->right)
7503 		    p->right->parent = p;
7504 		  t->left = p;
7505 
7506 		  r->balance = (t->balance < 0);
7507 		  p->balance = -(t->balance > 0);
7508 		  t->balance = 0;
7509 
7510 		  s = p->parent;
7511 		  p->parent = t;
7512 		  r->parent = t;
7513 		  t->parent = s;
7514 		  if (s)
7515 		    {
7516 		      if (s->left == p)
7517 			s->left = t;
7518 		      else
7519 			s->right = t;
7520 		    }
7521 		  else
7522 		    constructor_pending_elts = t;
7523 		}
7524 	      break;
7525 	    }
7526 	  else
7527 	    {
7528 	      /* p->balance == -1; growth of right side balances the node.  */
7529 	      p->balance = 0;
7530 	      break;
7531 	    }
7532 	}
7533 
7534       r = p;
7535       p = p->parent;
7536     }
7537 }
7538 
7539 /* Build AVL tree from a sorted chain.  */
7540 
7541 static void
set_nonincremental_init(struct obstack * braced_init_obstack)7542 set_nonincremental_init (struct obstack * braced_init_obstack)
7543 {
7544   unsigned HOST_WIDE_INT ix;
7545   tree index, value;
7546 
7547   if (TREE_CODE (constructor_type) != RECORD_TYPE
7548       && TREE_CODE (constructor_type) != ARRAY_TYPE)
7549     return;
7550 
7551   FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
7552     {
7553       add_pending_init (index, value, NULL_TREE, true,
7554 			braced_init_obstack);
7555     }
7556   constructor_elements = NULL;
7557   if (TREE_CODE (constructor_type) == RECORD_TYPE)
7558     {
7559       constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7560       /* Skip any nameless bit fields at the beginning.  */
7561       while (constructor_unfilled_fields != 0
7562 	     && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7563 	     && DECL_NAME (constructor_unfilled_fields) == 0)
7564 	constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
7565 
7566     }
7567   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7568     {
7569       if (TYPE_DOMAIN (constructor_type))
7570 	constructor_unfilled_index
7571 	    = convert (bitsizetype,
7572 		       TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7573       else
7574 	constructor_unfilled_index = bitsize_zero_node;
7575     }
7576   constructor_incremental = 0;
7577 }
7578 
7579 /* Build AVL tree from a string constant.  */
7580 
7581 static void
set_nonincremental_init_from_string(tree str,struct obstack * braced_init_obstack)7582 set_nonincremental_init_from_string (tree str,
7583 				     struct obstack * braced_init_obstack)
7584 {
7585   tree value, purpose, type;
7586   HOST_WIDE_INT val[2];
7587   const char *p, *end;
7588   int byte, wchar_bytes, charwidth, bitpos;
7589 
7590   gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
7591 
7592   wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
7593   charwidth = TYPE_PRECISION (char_type_node);
7594   type = TREE_TYPE (constructor_type);
7595   p = TREE_STRING_POINTER (str);
7596   end = p + TREE_STRING_LENGTH (str);
7597 
7598   for (purpose = bitsize_zero_node;
7599        p < end
7600        && !(constructor_max_index
7601 	    && tree_int_cst_lt (constructor_max_index, purpose));
7602        purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
7603     {
7604       if (wchar_bytes == 1)
7605 	{
7606 	  val[1] = (unsigned char) *p++;
7607 	  val[0] = 0;
7608 	}
7609       else
7610 	{
7611 	  val[0] = 0;
7612 	  val[1] = 0;
7613 	  for (byte = 0; byte < wchar_bytes; byte++)
7614 	    {
7615 	      if (BYTES_BIG_ENDIAN)
7616 		bitpos = (wchar_bytes - byte - 1) * charwidth;
7617 	      else
7618 		bitpos = byte * charwidth;
7619 	      val[bitpos < HOST_BITS_PER_WIDE_INT]
7620 		|= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7621 		   << (bitpos % HOST_BITS_PER_WIDE_INT);
7622 	    }
7623 	}
7624 
7625       if (!TYPE_UNSIGNED (type))
7626 	{
7627 	  bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7628 	  if (bitpos < HOST_BITS_PER_WIDE_INT)
7629 	    {
7630 	      if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7631 		{
7632 		  val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7633 		  val[0] = -1;
7634 		}
7635 	    }
7636 	  else if (bitpos == HOST_BITS_PER_WIDE_INT)
7637 	    {
7638 	      if (val[1] < 0)
7639 		val[0] = -1;
7640 	    }
7641 	  else if (val[0] & (((HOST_WIDE_INT) 1)
7642 			     << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7643 	    val[0] |= ((HOST_WIDE_INT) -1)
7644 		      << (bitpos - HOST_BITS_PER_WIDE_INT);
7645 	}
7646 
7647       value = build_int_cst_wide (type, val[1], val[0]);
7648       add_pending_init (purpose, value, NULL_TREE, true,
7649                         braced_init_obstack);
7650     }
7651 
7652   constructor_incremental = 0;
7653 }
7654 
7655 /* Return value of FIELD in pending initializer or zero if the field was
7656    not initialized yet.  */
7657 
7658 static tree
find_init_member(tree field,struct obstack * braced_init_obstack)7659 find_init_member (tree field, struct obstack * braced_init_obstack)
7660 {
7661   struct init_node *p;
7662 
7663   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7664     {
7665       if (constructor_incremental
7666 	  && tree_int_cst_lt (field, constructor_unfilled_index))
7667 	set_nonincremental_init (braced_init_obstack);
7668 
7669       p = constructor_pending_elts;
7670       while (p)
7671 	{
7672 	  if (tree_int_cst_lt (field, p->purpose))
7673 	    p = p->left;
7674 	  else if (tree_int_cst_lt (p->purpose, field))
7675 	    p = p->right;
7676 	  else
7677 	    return p->value;
7678 	}
7679     }
7680   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7681     {
7682       tree bitpos = bit_position (field);
7683 
7684       if (constructor_incremental
7685 	  && (!constructor_unfilled_fields
7686 	      || tree_int_cst_lt (bitpos,
7687 				  bit_position (constructor_unfilled_fields))))
7688 	set_nonincremental_init (braced_init_obstack);
7689 
7690       p = constructor_pending_elts;
7691       while (p)
7692 	{
7693 	  if (field == p->purpose)
7694 	    return p->value;
7695 	  else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7696 	    p = p->left;
7697 	  else
7698 	    p = p->right;
7699 	}
7700     }
7701   else if (TREE_CODE (constructor_type) == UNION_TYPE)
7702     {
7703       if (!vec_safe_is_empty (constructor_elements)
7704 	  && (constructor_elements->last ().index == field))
7705 	return constructor_elements->last ().value;
7706     }
7707   return 0;
7708 }
7709 
7710 /* "Output" the next constructor element.
7711    At top level, really output it to assembler code now.
7712    Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
7713    If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
7714    TYPE is the data type that the containing data type wants here.
7715    FIELD is the field (a FIELD_DECL) or the index that this element fills.
7716    If VALUE is a string constant, STRICT_STRING is true if it is
7717    unparenthesized or we should not warn here for it being parenthesized.
7718    For other types of VALUE, STRICT_STRING is not used.
7719 
7720    PENDING if non-nil means output pending elements that belong
7721    right after this element.  (PENDING is normally 1;
7722    it is 0 while outputting pending elements, to avoid recursion.)
7723 
7724    IMPLICIT is true if value comes from pop_init_level (1),
7725    the new initializer has been merged with the existing one
7726    and thus no warnings should be emitted about overriding an
7727    existing initializer.  */
7728 
7729 static void
output_init_element(tree value,tree origtype,bool strict_string,tree type,tree field,int pending,bool implicit,struct obstack * braced_init_obstack)7730 output_init_element (tree value, tree origtype, bool strict_string, tree type,
7731 		     tree field, int pending, bool implicit,
7732 		     struct obstack * braced_init_obstack)
7733 {
7734   tree semantic_type = NULL_TREE;
7735   bool maybe_const = true;
7736   bool npc;
7737 
7738   if (type == error_mark_node || value == error_mark_node)
7739     {
7740       constructor_erroneous = 1;
7741       return;
7742     }
7743   if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7744       && (TREE_CODE (value) == STRING_CST
7745 	  || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7746       && !(TREE_CODE (value) == STRING_CST
7747 	   && TREE_CODE (type) == ARRAY_TYPE
7748 	   && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7749       && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7750 		     TYPE_MAIN_VARIANT (type)))
7751     value = array_to_pointer_conversion (input_location, value);
7752 
7753   if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7754       && require_constant_value && !flag_isoc99 && pending)
7755     {
7756       /* As an extension, allow initializing objects with static storage
7757 	 duration with compound literals (which are then treated just as
7758 	 the brace enclosed list they contain).  */
7759       tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7760       value = DECL_INITIAL (decl);
7761     }
7762 
7763   npc = null_pointer_constant_p (value);
7764   if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7765     {
7766       semantic_type = TREE_TYPE (value);
7767       value = TREE_OPERAND (value, 0);
7768     }
7769   value = c_fully_fold (value, require_constant_value, &maybe_const);
7770 
7771   if (value == error_mark_node)
7772     constructor_erroneous = 1;
7773   else if (!TREE_CONSTANT (value))
7774     constructor_constant = 0;
7775   else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
7776 	   || ((TREE_CODE (constructor_type) == RECORD_TYPE
7777 		|| TREE_CODE (constructor_type) == UNION_TYPE)
7778 	       && DECL_C_BIT_FIELD (field)
7779 	       && TREE_CODE (value) != INTEGER_CST))
7780     constructor_simple = 0;
7781   if (!maybe_const)
7782     constructor_nonconst = 1;
7783 
7784   if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
7785     {
7786       if (require_constant_value)
7787 	{
7788 	  error_init ("initializer element is not constant");
7789 	  value = error_mark_node;
7790 	}
7791       else if (require_constant_elements)
7792 	pedwarn (input_location, 0,
7793 		 "initializer element is not computable at load time");
7794     }
7795   else if (!maybe_const
7796 	   && (require_constant_value || require_constant_elements))
7797     pedwarn_init (input_location, 0,
7798 		  "initializer element is not a constant expression");
7799 
7800   /* Issue -Wc++-compat warnings about initializing a bitfield with
7801      enum type.  */
7802   if (warn_cxx_compat
7803       && field != NULL_TREE
7804       && TREE_CODE (field) == FIELD_DECL
7805       && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7806       && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7807 	  != TYPE_MAIN_VARIANT (type))
7808       && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7809     {
7810       tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7811       if (checktype != error_mark_node
7812 	  && (TYPE_MAIN_VARIANT (checktype)
7813 	      != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7814 	warning_init (OPT_Wc___compat,
7815 		      "enum conversion in initialization is invalid in C++");
7816     }
7817 
7818   /* If this field is empty (and not at the end of structure),
7819      don't do anything other than checking the initializer.  */
7820   if (field
7821       && (TREE_TYPE (field) == error_mark_node
7822 	  || (COMPLETE_TYPE_P (TREE_TYPE (field))
7823 	      && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7824 	      && (TREE_CODE (constructor_type) == ARRAY_TYPE
7825 		  || DECL_CHAIN (field)))))
7826     return;
7827 
7828   if (semantic_type)
7829     value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
7830   value = digest_init (input_location, type, value, origtype, npc,
7831       		       strict_string, require_constant_value);
7832   if (value == error_mark_node)
7833     {
7834       constructor_erroneous = 1;
7835       return;
7836     }
7837   if (require_constant_value || require_constant_elements)
7838     constant_expression_warning (value);
7839 
7840   /* If this element doesn't come next in sequence,
7841      put it on constructor_pending_elts.  */
7842   if (TREE_CODE (constructor_type) == ARRAY_TYPE
7843       && (!constructor_incremental
7844 	  || !tree_int_cst_equal (field, constructor_unfilled_index)))
7845     {
7846       if (constructor_incremental
7847 	  && tree_int_cst_lt (field, constructor_unfilled_index))
7848 	set_nonincremental_init (braced_init_obstack);
7849 
7850       add_pending_init (field, value, origtype, implicit,
7851 			braced_init_obstack);
7852       return;
7853     }
7854   else if (TREE_CODE (constructor_type) == RECORD_TYPE
7855 	   && (!constructor_incremental
7856 	       || field != constructor_unfilled_fields))
7857     {
7858       /* We do this for records but not for unions.  In a union,
7859 	 no matter which field is specified, it can be initialized
7860 	 right away since it starts at the beginning of the union.  */
7861       if (constructor_incremental)
7862 	{
7863 	  if (!constructor_unfilled_fields)
7864 	    set_nonincremental_init (braced_init_obstack);
7865 	  else
7866 	    {
7867 	      tree bitpos, unfillpos;
7868 
7869 	      bitpos = bit_position (field);
7870 	      unfillpos = bit_position (constructor_unfilled_fields);
7871 
7872 	      if (tree_int_cst_lt (bitpos, unfillpos))
7873 		set_nonincremental_init (braced_init_obstack);
7874 	    }
7875 	}
7876 
7877       add_pending_init (field, value, origtype, implicit,
7878 			braced_init_obstack);
7879       return;
7880     }
7881   else if (TREE_CODE (constructor_type) == UNION_TYPE
7882 	   && !vec_safe_is_empty (constructor_elements))
7883     {
7884       if (!implicit)
7885 	{
7886 	  if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
7887 	    warning_init (0,
7888 			  "initialized field with side-effects overwritten");
7889 	  else if (warn_override_init)
7890 	    warning_init (OPT_Woverride_init, "initialized field overwritten");
7891 	}
7892 
7893       /* We can have just one union field set.  */
7894       constructor_elements = NULL;
7895     }
7896 
7897   /* Otherwise, output this element either to
7898      constructor_elements or to the assembler file.  */
7899 
7900   constructor_elt celt = {field, value};
7901   vec_safe_push (constructor_elements, celt);
7902 
7903   /* Advance the variable that indicates sequential elements output.  */
7904   if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7905     constructor_unfilled_index
7906       = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7907 			bitsize_one_node);
7908   else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7909     {
7910       constructor_unfilled_fields
7911 	= DECL_CHAIN (constructor_unfilled_fields);
7912 
7913       /* Skip any nameless bit fields.  */
7914       while (constructor_unfilled_fields != 0
7915 	     && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7916 	     && DECL_NAME (constructor_unfilled_fields) == 0)
7917 	constructor_unfilled_fields =
7918 	  DECL_CHAIN (constructor_unfilled_fields);
7919     }
7920   else if (TREE_CODE (constructor_type) == UNION_TYPE)
7921     constructor_unfilled_fields = 0;
7922 
7923   /* Now output any pending elements which have become next.  */
7924   if (pending)
7925     output_pending_init_elements (0, braced_init_obstack);
7926 }
7927 
7928 /* Output any pending elements which have become next.
7929    As we output elements, constructor_unfilled_{fields,index}
7930    advances, which may cause other elements to become next;
7931    if so, they too are output.
7932 
7933    If ALL is 0, we return when there are
7934    no more pending elements to output now.
7935 
7936    If ALL is 1, we output space as necessary so that
7937    we can output all the pending elements.  */
7938 static void
output_pending_init_elements(int all,struct obstack * braced_init_obstack)7939 output_pending_init_elements (int all, struct obstack * braced_init_obstack)
7940 {
7941   struct init_node *elt = constructor_pending_elts;
7942   tree next;
7943 
7944  retry:
7945 
7946   /* Look through the whole pending tree.
7947      If we find an element that should be output now,
7948      output it.  Otherwise, set NEXT to the element
7949      that comes first among those still pending.  */
7950 
7951   next = 0;
7952   while (elt)
7953     {
7954       if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7955 	{
7956 	  if (tree_int_cst_equal (elt->purpose,
7957 				  constructor_unfilled_index))
7958 	    output_init_element (elt->value, elt->origtype, true,
7959 				 TREE_TYPE (constructor_type),
7960 				 constructor_unfilled_index, 0, false,
7961 				 braced_init_obstack);
7962 	  else if (tree_int_cst_lt (constructor_unfilled_index,
7963 				    elt->purpose))
7964 	    {
7965 	      /* Advance to the next smaller node.  */
7966 	      if (elt->left)
7967 		elt = elt->left;
7968 	      else
7969 		{
7970 		  /* We have reached the smallest node bigger than the
7971 		     current unfilled index.  Fill the space first.  */
7972 		  next = elt->purpose;
7973 		  break;
7974 		}
7975 	    }
7976 	  else
7977 	    {
7978 	      /* Advance to the next bigger node.  */
7979 	      if (elt->right)
7980 		elt = elt->right;
7981 	      else
7982 		{
7983 		  /* We have reached the biggest node in a subtree.  Find
7984 		     the parent of it, which is the next bigger node.  */
7985 		  while (elt->parent && elt->parent->right == elt)
7986 		    elt = elt->parent;
7987 		  elt = elt->parent;
7988 		  if (elt && tree_int_cst_lt (constructor_unfilled_index,
7989 					      elt->purpose))
7990 		    {
7991 		      next = elt->purpose;
7992 		      break;
7993 		    }
7994 		}
7995 	    }
7996 	}
7997       else if (TREE_CODE (constructor_type) == RECORD_TYPE
7998 	       || TREE_CODE (constructor_type) == UNION_TYPE)
7999 	{
8000 	  tree ctor_unfilled_bitpos, elt_bitpos;
8001 
8002 	  /* If the current record is complete we are done.  */
8003 	  if (constructor_unfilled_fields == 0)
8004 	    break;
8005 
8006 	  ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8007 	  elt_bitpos = bit_position (elt->purpose);
8008 	  /* We can't compare fields here because there might be empty
8009 	     fields in between.  */
8010 	  if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8011 	    {
8012 	      constructor_unfilled_fields = elt->purpose;
8013 	      output_init_element (elt->value, elt->origtype, true,
8014 				   TREE_TYPE (elt->purpose),
8015 				   elt->purpose, 0, false,
8016 				   braced_init_obstack);
8017 	    }
8018 	  else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8019 	    {
8020 	      /* Advance to the next smaller node.  */
8021 	      if (elt->left)
8022 		elt = elt->left;
8023 	      else
8024 		{
8025 		  /* We have reached the smallest node bigger than the
8026 		     current unfilled field.  Fill the space first.  */
8027 		  next = elt->purpose;
8028 		  break;
8029 		}
8030 	    }
8031 	  else
8032 	    {
8033 	      /* Advance to the next bigger node.  */
8034 	      if (elt->right)
8035 		elt = elt->right;
8036 	      else
8037 		{
8038 		  /* We have reached the biggest node in a subtree.  Find
8039 		     the parent of it, which is the next bigger node.  */
8040 		  while (elt->parent && elt->parent->right == elt)
8041 		    elt = elt->parent;
8042 		  elt = elt->parent;
8043 		  if (elt
8044 		      && (tree_int_cst_lt (ctor_unfilled_bitpos,
8045 					   bit_position (elt->purpose))))
8046 		    {
8047 		      next = elt->purpose;
8048 		      break;
8049 		    }
8050 		}
8051 	    }
8052 	}
8053     }
8054 
8055   /* Ordinarily return, but not if we want to output all
8056      and there are elements left.  */
8057   if (!(all && next != 0))
8058     return;
8059 
8060   /* If it's not incremental, just skip over the gap, so that after
8061      jumping to retry we will output the next successive element.  */
8062   if (TREE_CODE (constructor_type) == RECORD_TYPE
8063       || TREE_CODE (constructor_type) == UNION_TYPE)
8064     constructor_unfilled_fields = next;
8065   else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8066     constructor_unfilled_index = next;
8067 
8068   /* ELT now points to the node in the pending tree with the next
8069      initializer to output.  */
8070   goto retry;
8071 }
8072 
8073 /* Add one non-braced element to the current constructor level.
8074    This adjusts the current position within the constructor's type.
8075    This may also start or terminate implicit levels
8076    to handle a partly-braced initializer.
8077 
8078    Once this has found the correct level for the new element,
8079    it calls output_init_element.
8080 
8081    IMPLICIT is true if value comes from pop_init_level (1),
8082    the new initializer has been merged with the existing one
8083    and thus no warnings should be emitted about overriding an
8084    existing initializer.  */
8085 
8086 void
process_init_element(struct c_expr value,bool implicit,struct obstack * braced_init_obstack)8087 process_init_element (struct c_expr value, bool implicit,
8088 		      struct obstack * braced_init_obstack)
8089 {
8090   tree orig_value = value.value;
8091   int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8092   bool strict_string = value.original_code == STRING_CST;
8093 
8094   designator_depth = 0;
8095   designator_erroneous = 0;
8096 
8097   /* Handle superfluous braces around string cst as in
8098      char x[] = {"foo"}; */
8099   if (string_flag
8100       && constructor_type
8101       && TREE_CODE (constructor_type) == ARRAY_TYPE
8102       && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
8103       && integer_zerop (constructor_unfilled_index))
8104     {
8105       if (constructor_stack->replacement_value.value)
8106 	error_init ("excess elements in char array initializer");
8107       constructor_stack->replacement_value = value;
8108       return;
8109     }
8110 
8111   if (constructor_stack->replacement_value.value != 0)
8112     {
8113       error_init ("excess elements in struct initializer");
8114       return;
8115     }
8116 
8117   /* Ignore elements of a brace group if it is entirely superfluous
8118      and has already been diagnosed.  */
8119   if (constructor_type == 0)
8120     return;
8121 
8122   /* If we've exhausted any levels that didn't have braces,
8123      pop them now.  */
8124   while (constructor_stack->implicit)
8125     {
8126       if ((TREE_CODE (constructor_type) == RECORD_TYPE
8127 	   || TREE_CODE (constructor_type) == UNION_TYPE)
8128 	  && constructor_fields == 0)
8129 	process_init_element (pop_init_level (1, braced_init_obstack),
8130 			      true, braced_init_obstack);
8131       else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8132 	        || TREE_CODE (constructor_type) == VECTOR_TYPE)
8133 	       && constructor_max_index
8134 	       && tree_int_cst_lt (constructor_max_index,
8135 				   constructor_index))
8136 	process_init_element (pop_init_level (1, braced_init_obstack),
8137 			      true, braced_init_obstack);
8138       else
8139 	break;
8140     }
8141 
8142   /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once.  */
8143   if (constructor_range_stack)
8144     {
8145       /* If value is a compound literal and we'll be just using its
8146 	 content, don't put it into a SAVE_EXPR.  */
8147       if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
8148 	  || !require_constant_value
8149 	  || flag_isoc99)
8150 	{
8151 	  tree semantic_type = NULL_TREE;
8152 	  if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8153 	    {
8154 	      semantic_type = TREE_TYPE (value.value);
8155 	      value.value = TREE_OPERAND (value.value, 0);
8156 	    }
8157 	  value.value = c_save_expr (value.value);
8158 	  if (semantic_type)
8159 	    value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8160 				  value.value);
8161 	}
8162     }
8163 
8164   while (1)
8165     {
8166       if (TREE_CODE (constructor_type) == RECORD_TYPE)
8167 	{
8168 	  tree fieldtype;
8169 	  enum tree_code fieldcode;
8170 
8171 	  if (constructor_fields == 0)
8172 	    {
8173 	      pedwarn_init (input_location, 0,
8174 			    "excess elements in struct initializer");
8175 	      break;
8176 	    }
8177 
8178 	  fieldtype = TREE_TYPE (constructor_fields);
8179 	  if (fieldtype != error_mark_node)
8180 	    fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8181 	  fieldcode = TREE_CODE (fieldtype);
8182 
8183 	  /* Error for non-static initialization of a flexible array member.  */
8184 	  if (fieldcode == ARRAY_TYPE
8185 	      && !require_constant_value
8186 	      && TYPE_SIZE (fieldtype) == NULL_TREE
8187 	      && DECL_CHAIN (constructor_fields) == NULL_TREE)
8188 	    {
8189 	      error_init ("non-static initialization of a flexible array member");
8190 	      break;
8191 	    }
8192 
8193 	  /* Accept a string constant to initialize a subarray.  */
8194 	  if (value.value != 0
8195 	      && fieldcode == ARRAY_TYPE
8196 	      && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8197 	      && string_flag)
8198 	    value.value = orig_value;
8199 	  /* Otherwise, if we have come to a subaggregate,
8200 	     and we don't have an element of its type, push into it.  */
8201 	  else if (value.value != 0
8202 		   && value.value != error_mark_node
8203 		   && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8204 		   && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8205 		       || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8206 	    {
8207 	      push_init_level (1, braced_init_obstack);
8208 	      continue;
8209 	    }
8210 
8211 	  if (value.value)
8212 	    {
8213 	      push_member_name (constructor_fields);
8214 	      output_init_element (value.value, value.original_type,
8215 				   strict_string, fieldtype,
8216 				   constructor_fields, 1, implicit,
8217 				   braced_init_obstack);
8218 	      RESTORE_SPELLING_DEPTH (constructor_depth);
8219 	    }
8220 	  else
8221 	    /* Do the bookkeeping for an element that was
8222 	       directly output as a constructor.  */
8223 	    {
8224 	      /* For a record, keep track of end position of last field.  */
8225 	      if (DECL_SIZE (constructor_fields))
8226 		constructor_bit_index
8227 		  = size_binop_loc (input_location, PLUS_EXPR,
8228 				    bit_position (constructor_fields),
8229 				    DECL_SIZE (constructor_fields));
8230 
8231 	      /* If the current field was the first one not yet written out,
8232 		 it isn't now, so update.  */
8233 	      if (constructor_unfilled_fields == constructor_fields)
8234 		{
8235 		  constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8236 		  /* Skip any nameless bit fields.  */
8237 		  while (constructor_unfilled_fields != 0
8238 			 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8239 			 && DECL_NAME (constructor_unfilled_fields) == 0)
8240 		    constructor_unfilled_fields =
8241 		      DECL_CHAIN (constructor_unfilled_fields);
8242 		}
8243 	    }
8244 
8245 	  constructor_fields = DECL_CHAIN (constructor_fields);
8246 	  /* Skip any nameless bit fields at the beginning.  */
8247 	  while (constructor_fields != 0
8248 		 && DECL_C_BIT_FIELD (constructor_fields)
8249 		 && DECL_NAME (constructor_fields) == 0)
8250 	    constructor_fields = DECL_CHAIN (constructor_fields);
8251 	}
8252       else if (TREE_CODE (constructor_type) == UNION_TYPE)
8253 	{
8254 	  tree fieldtype;
8255 	  enum tree_code fieldcode;
8256 
8257 	  if (constructor_fields == 0)
8258 	    {
8259 	      pedwarn_init (input_location, 0,
8260 			    "excess elements in union initializer");
8261 	      break;
8262 	    }
8263 
8264 	  fieldtype = TREE_TYPE (constructor_fields);
8265 	  if (fieldtype != error_mark_node)
8266 	    fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8267 	  fieldcode = TREE_CODE (fieldtype);
8268 
8269 	  /* Warn that traditional C rejects initialization of unions.
8270 	     We skip the warning if the value is zero.  This is done
8271 	     under the assumption that the zero initializer in user
8272 	     code appears conditioned on e.g. __STDC__ to avoid
8273 	     "missing initializer" warnings and relies on default
8274 	     initialization to zero in the traditional C case.
8275 	     We also skip the warning if the initializer is designated,
8276 	     again on the assumption that this must be conditional on
8277 	     __STDC__ anyway (and we've already complained about the
8278 	     member-designator already).  */
8279 	  if (!in_system_header && !constructor_designated
8280 	      && !(value.value && (integer_zerop (value.value)
8281 				   || real_zerop (value.value))))
8282 	    warning (OPT_Wtraditional, "traditional C rejects initialization "
8283 		     "of unions");
8284 
8285 	  /* Accept a string constant to initialize a subarray.  */
8286 	  if (value.value != 0
8287 	      && fieldcode == ARRAY_TYPE
8288 	      && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
8289 	      && string_flag)
8290 	    value.value = orig_value;
8291 	  /* Otherwise, if we have come to a subaggregate,
8292 	     and we don't have an element of its type, push into it.  */
8293 	  else if (value.value != 0
8294 		   && value.value != error_mark_node
8295 		   && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
8296 		   && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
8297 		       || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
8298 	    {
8299 	      push_init_level (1, braced_init_obstack);
8300 	      continue;
8301 	    }
8302 
8303 	  if (value.value)
8304 	    {
8305 	      push_member_name (constructor_fields);
8306 	      output_init_element (value.value, value.original_type,
8307 				   strict_string, fieldtype,
8308 				   constructor_fields, 1, implicit,
8309 				   braced_init_obstack);
8310 	      RESTORE_SPELLING_DEPTH (constructor_depth);
8311 	    }
8312 	  else
8313 	    /* Do the bookkeeping for an element that was
8314 	       directly output as a constructor.  */
8315 	    {
8316 	      constructor_bit_index = DECL_SIZE (constructor_fields);
8317 	      constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
8318 	    }
8319 
8320 	  constructor_fields = 0;
8321 	}
8322       else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8323 	{
8324 	  tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8325 	  enum tree_code eltcode = TREE_CODE (elttype);
8326 
8327 	  /* Accept a string constant to initialize a subarray.  */
8328 	  if (value.value != 0
8329 	      && eltcode == ARRAY_TYPE
8330 	      && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
8331 	      && string_flag)
8332 	    value.value = orig_value;
8333 	  /* Otherwise, if we have come to a subaggregate,
8334 	     and we don't have an element of its type, push into it.  */
8335 	  else if (value.value != 0
8336 		   && value.value != error_mark_node
8337 		   && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
8338 		   && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
8339 		       || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
8340 	    {
8341 	      push_init_level (1, braced_init_obstack);
8342 	      continue;
8343 	    }
8344 
8345 	  if (constructor_max_index != 0
8346 	      && (tree_int_cst_lt (constructor_max_index, constructor_index)
8347 		  || integer_all_onesp (constructor_max_index)))
8348 	    {
8349 	      pedwarn_init (input_location, 0,
8350 			    "excess elements in array initializer");
8351 	      break;
8352 	    }
8353 
8354 	  /* Now output the actual element.  */
8355 	  if (value.value)
8356 	    {
8357 	      push_array_bounds (tree_low_cst (constructor_index, 1));
8358 	      output_init_element (value.value, value.original_type,
8359 				   strict_string, elttype,
8360 				   constructor_index, 1, implicit,
8361 				   braced_init_obstack);
8362 	      RESTORE_SPELLING_DEPTH (constructor_depth);
8363 	    }
8364 
8365 	  constructor_index
8366 	    = size_binop_loc (input_location, PLUS_EXPR,
8367 			      constructor_index, bitsize_one_node);
8368 
8369 	  if (!value.value)
8370 	    /* If we are doing the bookkeeping for an element that was
8371 	       directly output as a constructor, we must update
8372 	       constructor_unfilled_index.  */
8373 	    constructor_unfilled_index = constructor_index;
8374 	}
8375       else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8376 	{
8377 	  tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8378 
8379 	 /* Do a basic check of initializer size.  Note that vectors
8380 	    always have a fixed size derived from their type.  */
8381 	  if (tree_int_cst_lt (constructor_max_index, constructor_index))
8382 	    {
8383 	      pedwarn_init (input_location, 0,
8384 			    "excess elements in vector initializer");
8385 	      break;
8386 	    }
8387 
8388 	  /* Now output the actual element.  */
8389 	  if (value.value)
8390 	    {
8391 	      if (TREE_CODE (value.value) == VECTOR_CST)
8392 		elttype = TYPE_MAIN_VARIANT (constructor_type);
8393 	      output_init_element (value.value, value.original_type,
8394 				   strict_string, elttype,
8395 				   constructor_index, 1, implicit,
8396 				   braced_init_obstack);
8397 	    }
8398 
8399 	  constructor_index
8400 	    = size_binop_loc (input_location,
8401 			      PLUS_EXPR, constructor_index, bitsize_one_node);
8402 
8403 	  if (!value.value)
8404 	    /* If we are doing the bookkeeping for an element that was
8405 	       directly output as a constructor, we must update
8406 	       constructor_unfilled_index.  */
8407 	    constructor_unfilled_index = constructor_index;
8408 	}
8409 
8410       /* Handle the sole element allowed in a braced initializer
8411 	 for a scalar variable.  */
8412       else if (constructor_type != error_mark_node
8413 	       && constructor_fields == 0)
8414 	{
8415 	  pedwarn_init (input_location, 0,
8416 			"excess elements in scalar initializer");
8417 	  break;
8418 	}
8419       else
8420 	{
8421 	  if (value.value)
8422 	    output_init_element (value.value, value.original_type,
8423 				 strict_string, constructor_type,
8424 				 NULL_TREE, 1, implicit,
8425 				 braced_init_obstack);
8426 	  constructor_fields = 0;
8427 	}
8428 
8429       /* Handle range initializers either at this level or anywhere higher
8430 	 in the designator stack.  */
8431       if (constructor_range_stack)
8432 	{
8433 	  struct constructor_range_stack *p, *range_stack;
8434 	  int finish = 0;
8435 
8436 	  range_stack = constructor_range_stack;
8437 	  constructor_range_stack = 0;
8438 	  while (constructor_stack != range_stack->stack)
8439 	    {
8440 	      gcc_assert (constructor_stack->implicit);
8441 	      process_init_element (pop_init_level (1,
8442 						    braced_init_obstack),
8443 				    true, braced_init_obstack);
8444 	    }
8445 	  for (p = range_stack;
8446 	       !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8447 	       p = p->prev)
8448 	    {
8449 	      gcc_assert (constructor_stack->implicit);
8450 	      process_init_element (pop_init_level (1, braced_init_obstack),
8451 				    true, braced_init_obstack);
8452 	    }
8453 
8454 	  p->index = size_binop_loc (input_location,
8455 				     PLUS_EXPR, p->index, bitsize_one_node);
8456 	  if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8457 	    finish = 1;
8458 
8459 	  while (1)
8460 	    {
8461 	      constructor_index = p->index;
8462 	      constructor_fields = p->fields;
8463 	      if (finish && p->range_end && p->index == p->range_start)
8464 		{
8465 		  finish = 0;
8466 		  p->prev = 0;
8467 		}
8468 	      p = p->next;
8469 	      if (!p)
8470 		break;
8471 	      push_init_level (2, braced_init_obstack);
8472 	      p->stack = constructor_stack;
8473 	      if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8474 		p->index = p->range_start;
8475 	    }
8476 
8477 	  if (!finish)
8478 	    constructor_range_stack = range_stack;
8479 	  continue;
8480 	}
8481 
8482       break;
8483     }
8484 
8485   constructor_range_stack = 0;
8486 }
8487 
8488 /* Build a complete asm-statement, whose components are a CV_QUALIFIER
8489    (guaranteed to be 'volatile' or null) and ARGS (represented using
8490    an ASM_EXPR node).  */
8491 tree
build_asm_stmt(tree cv_qualifier,tree args)8492 build_asm_stmt (tree cv_qualifier, tree args)
8493 {
8494   if (!ASM_VOLATILE_P (args) && cv_qualifier)
8495     ASM_VOLATILE_P (args) = 1;
8496   return add_stmt (args);
8497 }
8498 
8499 /* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8500    some INPUTS, and some CLOBBERS.  The latter three may be NULL.
8501    SIMPLE indicates whether there was anything at all after the
8502    string in the asm expression -- asm("blah") and asm("blah" : )
8503    are subtly different.  We use a ASM_EXPR node to represent this.  */
8504 tree
build_asm_expr(location_t loc,tree string,tree outputs,tree inputs,tree clobbers,tree labels,bool simple)8505 build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
8506 		tree clobbers, tree labels, bool simple)
8507 {
8508   tree tail;
8509   tree args;
8510   int i;
8511   const char *constraint;
8512   const char **oconstraints;
8513   bool allows_mem, allows_reg, is_inout;
8514   int ninputs, noutputs;
8515 
8516   ninputs = list_length (inputs);
8517   noutputs = list_length (outputs);
8518   oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8519 
8520   string = resolve_asm_operand_names (string, outputs, inputs, labels);
8521 
8522   /* Remove output conversions that change the type but not the mode.  */
8523   for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
8524     {
8525       tree output = TREE_VALUE (tail);
8526 
8527       output = c_fully_fold (output, false, NULL);
8528 
8529       /* ??? Really, this should not be here.  Users should be using a
8530 	 proper lvalue, dammit.  But there's a long history of using casts
8531 	 in the output operands.  In cases like longlong.h, this becomes a
8532 	 primitive form of typechecking -- if the cast can be removed, then
8533 	 the output operand had a type of the proper width; otherwise we'll
8534 	 get an error.  Gross, but ...  */
8535       STRIP_NOPS (output);
8536 
8537       if (!lvalue_or_else (loc, output, lv_asm))
8538 	output = error_mark_node;
8539 
8540       if (output != error_mark_node
8541 	  && (TREE_READONLY (output)
8542 	      || TYPE_READONLY (TREE_TYPE (output))
8543 	      || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8544 		   || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8545 		  && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8546 	readonly_error (output, lv_asm);
8547 
8548       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8549       oconstraints[i] = constraint;
8550 
8551       if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8552 				   &allows_mem, &allows_reg, &is_inout))
8553 	{
8554 	  /* If the operand is going to end up in memory,
8555 	     mark it addressable.  */
8556 	  if (!allows_reg && !c_mark_addressable (output))
8557 	    output = error_mark_node;
8558 	  if (!(!allows_reg && allows_mem)
8559 	      && output != error_mark_node
8560 	      && VOID_TYPE_P (TREE_TYPE (output)))
8561 	    {
8562 	      error_at (loc, "invalid use of void expression");
8563 	      output = error_mark_node;
8564 	    }
8565 	}
8566       else
8567 	output = error_mark_node;
8568 
8569       TREE_VALUE (tail) = output;
8570     }
8571 
8572   for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8573     {
8574       tree input;
8575 
8576       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8577       input = TREE_VALUE (tail);
8578 
8579       if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8580 				  oconstraints, &allows_mem, &allows_reg))
8581 	{
8582 	  /* If the operand is going to end up in memory,
8583 	     mark it addressable.  */
8584 	  if (!allows_reg && allows_mem)
8585 	    {
8586 	      input = c_fully_fold (input, false, NULL);
8587 
8588 	      /* Strip the nops as we allow this case.  FIXME, this really
8589 		 should be rejected or made deprecated.  */
8590 	      STRIP_NOPS (input);
8591 	      if (!c_mark_addressable (input))
8592 		input = error_mark_node;
8593 	    }
8594 	  else
8595 	    {
8596 	      struct c_expr expr;
8597 	      memset (&expr, 0, sizeof (expr));
8598 	      expr.value = input;
8599 	      expr = default_function_array_conversion (loc, expr);
8600 	      input = c_fully_fold (expr.value, false, NULL);
8601 
8602 	      if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8603 		{
8604 		  error_at (loc, "invalid use of void expression");
8605 		  input = error_mark_node;
8606 		}
8607 	    }
8608 	}
8609       else
8610 	input = error_mark_node;
8611 
8612       TREE_VALUE (tail) = input;
8613     }
8614 
8615   /* ASMs with labels cannot have outputs.  This should have been
8616      enforced by the parser.  */
8617   gcc_assert (outputs == NULL || labels == NULL);
8618 
8619   args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
8620 
8621   /* asm statements without outputs, including simple ones, are treated
8622      as volatile.  */
8623   ASM_INPUT_P (args) = simple;
8624   ASM_VOLATILE_P (args) = (noutputs == 0);
8625 
8626   return args;
8627 }
8628 
8629 /* Generate a goto statement to LABEL.  LOC is the location of the
8630    GOTO.  */
8631 
8632 tree
c_finish_goto_label(location_t loc,tree label)8633 c_finish_goto_label (location_t loc, tree label)
8634 {
8635   tree decl = lookup_label_for_goto (loc, label);
8636   if (!decl)
8637     return NULL_TREE;
8638   TREE_USED (decl) = 1;
8639   {
8640     tree t = build1 (GOTO_EXPR, void_type_node, decl);
8641     SET_EXPR_LOCATION (t, loc);
8642     return add_stmt (t);
8643   }
8644 }
8645 
8646 /* Generate a computed goto statement to EXPR.  LOC is the location of
8647    the GOTO.  */
8648 
8649 tree
c_finish_goto_ptr(location_t loc,tree expr)8650 c_finish_goto_ptr (location_t loc, tree expr)
8651 {
8652   tree t;
8653   pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
8654   expr = c_fully_fold (expr, false, NULL);
8655   expr = convert (ptr_type_node, expr);
8656   t = build1 (GOTO_EXPR, void_type_node, expr);
8657   SET_EXPR_LOCATION (t, loc);
8658   return add_stmt (t);
8659 }
8660 
8661 /* Generate a C `return' statement.  RETVAL is the expression for what
8662    to return, or a null pointer for `return;' with no value.  LOC is
8663    the location of the return statement.  If ORIGTYPE is not NULL_TREE, it
8664    is the original type of RETVAL.  */
8665 
8666 tree
c_finish_return(location_t loc,tree retval,tree origtype)8667 c_finish_return (location_t loc, tree retval, tree origtype)
8668 {
8669   tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8670   bool no_warning = false;
8671   bool npc = false;
8672 
8673   if (TREE_THIS_VOLATILE (current_function_decl))
8674     warning_at (loc, 0,
8675 		"function declared %<noreturn%> has a %<return%> statement");
8676 
8677   if (retval)
8678     {
8679       tree semantic_type = NULL_TREE;
8680       npc = null_pointer_constant_p (retval);
8681       if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8682 	{
8683 	  semantic_type = TREE_TYPE (retval);
8684 	  retval = TREE_OPERAND (retval, 0);
8685 	}
8686       retval = c_fully_fold (retval, false, NULL);
8687       if (semantic_type)
8688 	retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
8689     }
8690 
8691   if (!retval)
8692     {
8693       current_function_returns_null = 1;
8694       if ((warn_return_type || flag_isoc99)
8695 	  && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
8696 	{
8697 	  pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
8698 		       "%<return%> with no value, in "
8699 		       "function returning non-void");
8700 	  no_warning = true;
8701 	}
8702     }
8703   else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
8704     {
8705       current_function_returns_null = 1;
8706       if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
8707 	pedwarn (loc, 0,
8708 		 "%<return%> with a value, in function returning void");
8709       else
8710 	pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
8711 		 "%<return%> with expression, in function returning void");
8712     }
8713   else
8714     {
8715       tree t = convert_for_assignment (loc, valtype, retval, origtype,
8716 	  			       ic_return,
8717 				       npc, NULL_TREE, NULL_TREE, 0);
8718       tree res = DECL_RESULT (current_function_decl);
8719       tree inner;
8720       bool save;
8721 
8722       current_function_returns_value = 1;
8723       if (t == error_mark_node)
8724 	return NULL_TREE;
8725 
8726       save = in_late_binary_op;
8727       if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
8728           || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
8729         in_late_binary_op = true;
8730       inner = t = convert (TREE_TYPE (res), t);
8731       in_late_binary_op = save;
8732 
8733       /* Strip any conversions, additions, and subtractions, and see if
8734 	 we are returning the address of a local variable.  Warn if so.  */
8735       while (1)
8736 	{
8737 	  switch (TREE_CODE (inner))
8738 	    {
8739 	    CASE_CONVERT:
8740 	    case NON_LVALUE_EXPR:
8741 	    case PLUS_EXPR:
8742 	    case POINTER_PLUS_EXPR:
8743 	      inner = TREE_OPERAND (inner, 0);
8744 	      continue;
8745 
8746 	    case MINUS_EXPR:
8747 	      /* If the second operand of the MINUS_EXPR has a pointer
8748 		 type (or is converted from it), this may be valid, so
8749 		 don't give a warning.  */
8750 	      {
8751 		tree op1 = TREE_OPERAND (inner, 1);
8752 
8753 		while (!POINTER_TYPE_P (TREE_TYPE (op1))
8754 		       && (CONVERT_EXPR_P (op1)
8755 			   || TREE_CODE (op1) == NON_LVALUE_EXPR))
8756 		  op1 = TREE_OPERAND (op1, 0);
8757 
8758 		if (POINTER_TYPE_P (TREE_TYPE (op1)))
8759 		  break;
8760 
8761 		inner = TREE_OPERAND (inner, 0);
8762 		continue;
8763 	      }
8764 
8765 	    case ADDR_EXPR:
8766 	      inner = TREE_OPERAND (inner, 0);
8767 
8768 	      while (REFERENCE_CLASS_P (inner)
8769 		     && TREE_CODE (inner) != INDIRECT_REF)
8770 		inner = TREE_OPERAND (inner, 0);
8771 
8772 	      if (DECL_P (inner)
8773 		  && !DECL_EXTERNAL (inner)
8774 		  && !TREE_STATIC (inner)
8775 		  && DECL_CONTEXT (inner) == current_function_decl)
8776 		warning_at (loc,
8777 			    OPT_Wreturn_local_addr, "function returns address "
8778 			    "of local variable");
8779 	      break;
8780 
8781 	    default:
8782 	      break;
8783 	    }
8784 
8785 	  break;
8786 	}
8787 
8788       retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
8789       SET_EXPR_LOCATION (retval, loc);
8790 
8791       if (warn_sequence_point)
8792 	verify_sequence_points (retval);
8793     }
8794 
8795   ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
8796   TREE_NO_WARNING (ret_stmt) |= no_warning;
8797   return add_stmt (ret_stmt);
8798 }
8799 
8800 struct c_switch {
8801   /* The SWITCH_EXPR being built.  */
8802   tree switch_expr;
8803 
8804   /* The original type of the testing expression, i.e. before the
8805      default conversion is applied.  */
8806   tree orig_type;
8807 
8808   /* A splay-tree mapping the low element of a case range to the high
8809      element, or NULL_TREE if there is no high element.  Used to
8810      determine whether or not a new case label duplicates an old case
8811      label.  We need a tree, rather than simply a hash table, because
8812      of the GNU case range extension.  */
8813   splay_tree cases;
8814 
8815   /* The bindings at the point of the switch.  This is used for
8816      warnings crossing decls when branching to a case label.  */
8817   struct c_spot_bindings *bindings;
8818 
8819   /* The next node on the stack.  */
8820   struct c_switch *next;
8821 };
8822 
8823 /* A stack of the currently active switch statements.  The innermost
8824    switch statement is on the top of the stack.  There is no need to
8825    mark the stack for garbage collection because it is only active
8826    during the processing of the body of a function, and we never
8827    collect at that point.  */
8828 
8829 struct c_switch *c_switch_stack;
8830 
8831 /* Start a C switch statement, testing expression EXP.  Return the new
8832    SWITCH_EXPR.  SWITCH_LOC is the location of the `switch'.
8833    SWITCH_COND_LOC is the location of the switch's condition.  */
8834 
8835 tree
c_start_case(location_t switch_loc,location_t switch_cond_loc,tree exp)8836 c_start_case (location_t switch_loc,
8837 	      location_t switch_cond_loc,
8838 	      tree exp)
8839 {
8840   tree orig_type = error_mark_node;
8841   struct c_switch *cs;
8842 
8843   if (exp != error_mark_node)
8844     {
8845       orig_type = TREE_TYPE (exp);
8846 
8847       if (!INTEGRAL_TYPE_P (orig_type))
8848 	{
8849 	  if (orig_type != error_mark_node)
8850 	    {
8851 	      error_at (switch_cond_loc, "switch quantity not an integer");
8852 	      orig_type = error_mark_node;
8853 	    }
8854 	  exp = integer_zero_node;
8855 	}
8856       else
8857 	{
8858 	  tree type = TYPE_MAIN_VARIANT (orig_type);
8859 
8860 	  if (!in_system_header
8861 	      && (type == long_integer_type_node
8862 		  || type == long_unsigned_type_node))
8863 	    warning_at (switch_cond_loc,
8864 			OPT_Wtraditional, "%<long%> switch expression not "
8865 			"converted to %<int%> in ISO C");
8866 
8867 	  exp = c_fully_fold (exp, false, NULL);
8868 	  exp = default_conversion (exp);
8869 
8870 	  if (warn_sequence_point)
8871 	    verify_sequence_points (exp);
8872 	}
8873     }
8874 
8875   /* Add this new SWITCH_EXPR to the stack.  */
8876   cs = XNEW (struct c_switch);
8877   cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
8878   SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
8879   cs->orig_type = orig_type;
8880   cs->cases = splay_tree_new (case_compare, NULL, NULL);
8881   cs->bindings = c_get_switch_bindings ();
8882   cs->next = c_switch_stack;
8883   c_switch_stack = cs;
8884 
8885   return add_stmt (cs->switch_expr);
8886 }
8887 
8888 /* Process a case label at location LOC.  */
8889 
8890 tree
do_case(location_t loc,tree low_value,tree high_value)8891 do_case (location_t loc, tree low_value, tree high_value)
8892 {
8893   tree label = NULL_TREE;
8894 
8895   if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8896     {
8897       low_value = c_fully_fold (low_value, false, NULL);
8898       if (TREE_CODE (low_value) == INTEGER_CST)
8899 	pedwarn (input_location, OPT_Wpedantic,
8900 		 "case label is not an integer constant expression");
8901     }
8902 
8903   if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8904     {
8905       high_value = c_fully_fold (high_value, false, NULL);
8906       if (TREE_CODE (high_value) == INTEGER_CST)
8907 	pedwarn (input_location, OPT_Wpedantic,
8908 		 "case label is not an integer constant expression");
8909     }
8910 
8911   if (c_switch_stack == NULL)
8912     {
8913       if (low_value)
8914 	error_at (loc, "case label not within a switch statement");
8915       else
8916 	error_at (loc, "%<default%> label not within a switch statement");
8917       return NULL_TREE;
8918     }
8919 
8920   if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8921 				    EXPR_LOCATION (c_switch_stack->switch_expr),
8922 				    loc))
8923     return NULL_TREE;
8924 
8925   label = c_add_case_label (loc, c_switch_stack->cases,
8926 			    SWITCH_COND (c_switch_stack->switch_expr),
8927 			    c_switch_stack->orig_type,
8928 			    low_value, high_value);
8929   if (label == error_mark_node)
8930     label = NULL_TREE;
8931   return label;
8932 }
8933 
8934 /* Finish the switch statement.  */
8935 
8936 void
c_finish_case(tree body)8937 c_finish_case (tree body)
8938 {
8939   struct c_switch *cs = c_switch_stack;
8940   location_t switch_location;
8941 
8942   SWITCH_BODY (cs->switch_expr) = body;
8943 
8944   /* Emit warnings as needed.  */
8945   switch_location = EXPR_LOCATION (cs->switch_expr);
8946   c_do_switch_warnings (cs->cases, switch_location,
8947 			TREE_TYPE (cs->switch_expr),
8948 			SWITCH_COND (cs->switch_expr));
8949 
8950   /* Pop the stack.  */
8951   c_switch_stack = cs->next;
8952   splay_tree_delete (cs->cases);
8953   c_release_switch_bindings (cs->bindings);
8954   XDELETE (cs);
8955 }
8956 
8957 /* Emit an if statement.  IF_LOCUS is the location of the 'if'.  COND,
8958    THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
8959    may be null.  NESTED_IF is true if THEN_BLOCK contains another IF
8960    statement, and was not surrounded with parenthesis.  */
8961 
8962 void
c_finish_if_stmt(location_t if_locus,tree cond,tree then_block,tree else_block,bool nested_if)8963 c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
8964 		  tree else_block, bool nested_if)
8965 {
8966   tree stmt;
8967 
8968   /* Diagnose an ambiguous else if if-then-else is nested inside if-then.  */
8969   if (warn_parentheses && nested_if && else_block == NULL)
8970     {
8971       tree inner_if = then_block;
8972 
8973       /* We know from the grammar productions that there is an IF nested
8974 	 within THEN_BLOCK.  Due to labels and c99 conditional declarations,
8975 	 it might not be exactly THEN_BLOCK, but should be the last
8976 	 non-container statement within.  */
8977       while (1)
8978 	switch (TREE_CODE (inner_if))
8979 	  {
8980 	  case COND_EXPR:
8981 	    goto found;
8982 	  case BIND_EXPR:
8983 	    inner_if = BIND_EXPR_BODY (inner_if);
8984 	    break;
8985 	  case STATEMENT_LIST:
8986 	    inner_if = expr_last (then_block);
8987 	    break;
8988 	  case TRY_FINALLY_EXPR:
8989 	  case TRY_CATCH_EXPR:
8990 	    inner_if = TREE_OPERAND (inner_if, 0);
8991 	    break;
8992 	  default:
8993 	    gcc_unreachable ();
8994 	  }
8995     found:
8996 
8997       if (COND_EXPR_ELSE (inner_if))
8998 	 warning_at (if_locus, OPT_Wparentheses,
8999 		     "suggest explicit braces to avoid ambiguous %<else%>");
9000     }
9001 
9002   stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
9003   SET_EXPR_LOCATION (stmt, if_locus);
9004   add_stmt (stmt);
9005 }
9006 
9007 /* Emit a general-purpose loop construct.  START_LOCUS is the location of
9008    the beginning of the loop.  COND is the loop condition.  COND_IS_FIRST
9009    is false for DO loops.  INCR is the FOR increment expression.  BODY is
9010    the statement controlled by the loop.  BLAB is the break label.  CLAB is
9011    the continue label.  Everything is allowed to be NULL.  */
9012 
9013 void
c_finish_loop(location_t start_locus,tree cond,tree incr,tree body,tree blab,tree clab,bool cond_is_first)9014 c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9015 	       tree blab, tree clab, bool cond_is_first)
9016 {
9017   tree entry = NULL, exit = NULL, t;
9018 
9019   /* If the condition is zero don't generate a loop construct.  */
9020   if (cond && integer_zerop (cond))
9021     {
9022       if (cond_is_first)
9023 	{
9024 	  t = build_and_jump (&blab);
9025 	  SET_EXPR_LOCATION (t, start_locus);
9026 	  add_stmt (t);
9027 	}
9028     }
9029   else
9030     {
9031       tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9032 
9033       /* If we have an exit condition, then we build an IF with gotos either
9034 	 out of the loop, or to the top of it.  If there's no exit condition,
9035 	 then we just build a jump back to the top.  */
9036       exit = build_and_jump (&LABEL_EXPR_LABEL (top));
9037 
9038       if (cond && !integer_nonzerop (cond))
9039 	{
9040 	  /* Canonicalize the loop condition to the end.  This means
9041 	     generating a branch to the loop condition.  Reuse the
9042 	     continue label, if possible.  */
9043 	  if (cond_is_first)
9044 	    {
9045 	      if (incr || !clab)
9046 		{
9047 		  entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9048 		  t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9049 		}
9050 	      else
9051 		t = build1 (GOTO_EXPR, void_type_node, clab);
9052 	      SET_EXPR_LOCATION (t, start_locus);
9053 	      add_stmt (t);
9054 	    }
9055 
9056 	  t = build_and_jump (&blab);
9057 	  if (cond_is_first)
9058 	    exit = fold_build3_loc (start_locus,
9059 				COND_EXPR, void_type_node, cond, exit, t);
9060 	  else
9061 	    exit = fold_build3_loc (input_location,
9062 				COND_EXPR, void_type_node, cond, exit, t);
9063 	}
9064 
9065       add_stmt (top);
9066     }
9067 
9068   if (body)
9069     add_stmt (body);
9070   if (clab)
9071     add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9072   if (incr)
9073     add_stmt (incr);
9074   if (entry)
9075     add_stmt (entry);
9076   if (exit)
9077     add_stmt (exit);
9078   if (blab)
9079     add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
9080 }
9081 
9082 tree
c_finish_bc_stmt(location_t loc,tree * label_p,bool is_break)9083 c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
9084 {
9085   bool skip;
9086   tree label = *label_p;
9087 
9088   /* In switch statements break is sometimes stylistically used after
9089      a return statement.  This can lead to spurious warnings about
9090      control reaching the end of a non-void function when it is
9091      inlined.  Note that we are calling block_may_fallthru with
9092      language specific tree nodes; this works because
9093      block_may_fallthru returns true when given something it does not
9094      understand.  */
9095   skip = !block_may_fallthru (cur_stmt_list);
9096 
9097   if (!label)
9098     {
9099       if (!skip)
9100 	*label_p = label = create_artificial_label (loc);
9101     }
9102   else if (TREE_CODE (label) == LABEL_DECL)
9103     ;
9104   else switch (TREE_INT_CST_LOW (label))
9105     {
9106     case 0:
9107       if (is_break)
9108 	error_at (loc, "break statement not within loop or switch");
9109       else
9110 	error_at (loc, "continue statement not within a loop");
9111       return NULL_TREE;
9112 
9113     case 1:
9114       gcc_assert (is_break);
9115       error_at (loc, "break statement used with OpenMP for loop");
9116       return NULL_TREE;
9117 
9118     default:
9119       gcc_unreachable ();
9120     }
9121 
9122   if (skip)
9123     return NULL_TREE;
9124 
9125   if (!is_break)
9126     add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9127 
9128   return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
9129 }
9130 
9131 /* A helper routine for c_process_expr_stmt and c_finish_stmt_expr.  */
9132 
9133 static void
emit_side_effect_warnings(location_t loc,tree expr)9134 emit_side_effect_warnings (location_t loc, tree expr)
9135 {
9136   if (expr == error_mark_node)
9137     ;
9138   else if (!TREE_SIDE_EFFECTS (expr))
9139     {
9140       if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
9141 	warning_at (loc, OPT_Wunused_value, "statement with no effect");
9142     }
9143   else
9144     warn_if_unused_value (expr, loc);
9145 }
9146 
9147 /* Process an expression as if it were a complete statement.  Emit
9148    diagnostics, but do not call ADD_STMT.  LOC is the location of the
9149    statement.  */
9150 
9151 tree
c_process_expr_stmt(location_t loc,tree expr)9152 c_process_expr_stmt (location_t loc, tree expr)
9153 {
9154   tree exprv;
9155 
9156   if (!expr)
9157     return NULL_TREE;
9158 
9159   expr = c_fully_fold (expr, false, NULL);
9160 
9161   if (warn_sequence_point)
9162     verify_sequence_points (expr);
9163 
9164   if (TREE_TYPE (expr) != error_mark_node
9165       && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9166       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
9167     error_at (loc, "expression statement has incomplete type");
9168 
9169   /* If we're not processing a statement expression, warn about unused values.
9170      Warnings for statement expressions will be emitted later, once we figure
9171      out which is the result.  */
9172   if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9173       && warn_unused_value)
9174     emit_side_effect_warnings (loc, expr);
9175 
9176   exprv = expr;
9177   while (TREE_CODE (exprv) == COMPOUND_EXPR)
9178     exprv = TREE_OPERAND (exprv, 1);
9179   while (CONVERT_EXPR_P (exprv))
9180     exprv = TREE_OPERAND (exprv, 0);
9181   if (DECL_P (exprv)
9182       || handled_component_p (exprv)
9183       || TREE_CODE (exprv) == ADDR_EXPR)
9184     mark_exp_read (exprv);
9185 
9186   /* If the expression is not of a type to which we cannot assign a line
9187      number, wrap the thing in a no-op NOP_EXPR.  */
9188   if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
9189     {
9190       expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9191       SET_EXPR_LOCATION (expr, loc);
9192     }
9193 
9194   return expr;
9195 }
9196 
9197 /* Emit an expression as a statement.  LOC is the location of the
9198    expression.  */
9199 
9200 tree
c_finish_expr_stmt(location_t loc,tree expr)9201 c_finish_expr_stmt (location_t loc, tree expr)
9202 {
9203   if (expr)
9204     return add_stmt (c_process_expr_stmt (loc, expr));
9205   else
9206     return NULL;
9207 }
9208 
9209 /* Do the opposite and emit a statement as an expression.  To begin,
9210    create a new binding level and return it.  */
9211 
9212 tree
c_begin_stmt_expr(void)9213 c_begin_stmt_expr (void)
9214 {
9215   tree ret;
9216 
9217   /* We must force a BLOCK for this level so that, if it is not expanded
9218      later, there is a way to turn off the entire subtree of blocks that
9219      are contained in it.  */
9220   keep_next_level ();
9221   ret = c_begin_compound_stmt (true);
9222 
9223   c_bindings_start_stmt_expr (c_switch_stack == NULL
9224 			      ? NULL
9225 			      : c_switch_stack->bindings);
9226 
9227   /* Mark the current statement list as belonging to a statement list.  */
9228   STATEMENT_LIST_STMT_EXPR (ret) = 1;
9229 
9230   return ret;
9231 }
9232 
9233 /* LOC is the location of the compound statement to which this body
9234    belongs.  */
9235 
9236 tree
c_finish_stmt_expr(location_t loc,tree body)9237 c_finish_stmt_expr (location_t loc, tree body)
9238 {
9239   tree last, type, tmp, val;
9240   tree *last_p;
9241 
9242   body = c_end_compound_stmt (loc, body, true);
9243 
9244   c_bindings_end_stmt_expr (c_switch_stack == NULL
9245 			    ? NULL
9246 			    : c_switch_stack->bindings);
9247 
9248   /* Locate the last statement in BODY.  See c_end_compound_stmt
9249      about always returning a BIND_EXPR.  */
9250   last_p = &BIND_EXPR_BODY (body);
9251   last = BIND_EXPR_BODY (body);
9252 
9253  continue_searching:
9254   if (TREE_CODE (last) == STATEMENT_LIST)
9255     {
9256       tree_stmt_iterator i;
9257 
9258       /* This can happen with degenerate cases like ({ }).  No value.  */
9259       if (!TREE_SIDE_EFFECTS (last))
9260 	return body;
9261 
9262       /* If we're supposed to generate side effects warnings, process
9263 	 all of the statements except the last.  */
9264       if (warn_unused_value)
9265 	{
9266 	  for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
9267 	    {
9268 	      location_t tloc;
9269 	      tree t = tsi_stmt (i);
9270 
9271 	      tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9272 	      emit_side_effect_warnings (tloc, t);
9273 	    }
9274 	}
9275       else
9276 	i = tsi_last (last);
9277       last_p = tsi_stmt_ptr (i);
9278       last = *last_p;
9279     }
9280 
9281   /* If the end of the list is exception related, then the list was split
9282      by a call to push_cleanup.  Continue searching.  */
9283   if (TREE_CODE (last) == TRY_FINALLY_EXPR
9284       || TREE_CODE (last) == TRY_CATCH_EXPR)
9285     {
9286       last_p = &TREE_OPERAND (last, 0);
9287       last = *last_p;
9288       goto continue_searching;
9289     }
9290 
9291   if (last == error_mark_node)
9292     return last;
9293 
9294   /* In the case that the BIND_EXPR is not necessary, return the
9295      expression out from inside it.  */
9296   if (last == BIND_EXPR_BODY (body)
9297       && BIND_EXPR_VARS (body) == NULL)
9298     {
9299       /* Even if this looks constant, do not allow it in a constant
9300 	 expression.  */
9301       last = c_wrap_maybe_const (last, true);
9302       /* Do not warn if the return value of a statement expression is
9303 	 unused.  */
9304       TREE_NO_WARNING (last) = 1;
9305       return last;
9306     }
9307 
9308   /* Extract the type of said expression.  */
9309   type = TREE_TYPE (last);
9310 
9311   /* If we're not returning a value at all, then the BIND_EXPR that
9312      we already have is a fine expression to return.  */
9313   if (!type || VOID_TYPE_P (type))
9314     return body;
9315 
9316   /* Now that we've located the expression containing the value, it seems
9317      silly to make voidify_wrapper_expr repeat the process.  Create a
9318      temporary of the appropriate type and stick it in a TARGET_EXPR.  */
9319   tmp = create_tmp_var_raw (type, NULL);
9320 
9321   /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt.  This avoids
9322      tree_expr_nonnegative_p giving up immediately.  */
9323   val = last;
9324   if (TREE_CODE (val) == NOP_EXPR
9325       && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9326     val = TREE_OPERAND (val, 0);
9327 
9328   *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
9329   SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
9330 
9331   {
9332     tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9333     SET_EXPR_LOCATION (t, loc);
9334     return t;
9335   }
9336 }
9337 
9338 /* Begin and end compound statements.  This is as simple as pushing
9339    and popping new statement lists from the tree.  */
9340 
9341 tree
c_begin_compound_stmt(bool do_scope)9342 c_begin_compound_stmt (bool do_scope)
9343 {
9344   tree stmt = push_stmt_list ();
9345   if (do_scope)
9346     push_scope ();
9347   return stmt;
9348 }
9349 
9350 /* End a compound statement.  STMT is the statement.  LOC is the
9351    location of the compound statement-- this is usually the location
9352    of the opening brace.  */
9353 
9354 tree
c_end_compound_stmt(location_t loc,tree stmt,bool do_scope)9355 c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
9356 {
9357   tree block = NULL;
9358 
9359   if (do_scope)
9360     {
9361       if (c_dialect_objc ())
9362 	objc_clear_super_receiver ();
9363       block = pop_scope ();
9364     }
9365 
9366   stmt = pop_stmt_list (stmt);
9367   stmt = c_build_bind_expr (loc, block, stmt);
9368 
9369   /* If this compound statement is nested immediately inside a statement
9370      expression, then force a BIND_EXPR to be created.  Otherwise we'll
9371      do the wrong thing for ({ { 1; } }) or ({ 1; { } }).  In particular,
9372      STATEMENT_LISTs merge, and thus we can lose track of what statement
9373      was really last.  */
9374   if (building_stmt_list_p ()
9375       && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9376       && TREE_CODE (stmt) != BIND_EXPR)
9377     {
9378       stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
9379       TREE_SIDE_EFFECTS (stmt) = 1;
9380       SET_EXPR_LOCATION (stmt, loc);
9381     }
9382 
9383   return stmt;
9384 }
9385 
9386 /* Queue a cleanup.  CLEANUP is an expression/statement to be executed
9387    when the current scope is exited.  EH_ONLY is true when this is not
9388    meant to apply to normal control flow transfer.  */
9389 
9390 void
push_cleanup(tree decl,tree cleanup,bool eh_only)9391 push_cleanup (tree decl, tree cleanup, bool eh_only)
9392 {
9393   enum tree_code code;
9394   tree stmt, list;
9395   bool stmt_expr;
9396 
9397   code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
9398   stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
9399   add_stmt (stmt);
9400   stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9401   list = push_stmt_list ();
9402   TREE_OPERAND (stmt, 0) = list;
9403   STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
9404 }
9405 
9406 /* Build a binary-operation expression without default conversions.
9407    CODE is the kind of expression to build.
9408    LOCATION is the operator's location.
9409    This function differs from `build' in several ways:
9410    the data type of the result is computed and recorded in it,
9411    warnings are generated if arg data types are invalid,
9412    special handling for addition and subtraction of pointers is known,
9413    and some optimization is done (operations on narrow ints
9414    are done in the narrower type when that gives the same result).
9415    Constant folding is also done before the result is returned.
9416 
9417    Note that the operands will never have enumeral types, or function
9418    or array types, because either they will have the default conversions
9419    performed or they have both just been converted to some other type in which
9420    the arithmetic is to be done.  */
9421 
9422 tree
build_binary_op(location_t location,enum tree_code code,tree orig_op0,tree orig_op1,int convert_p)9423 build_binary_op (location_t location, enum tree_code code,
9424 		 tree orig_op0, tree orig_op1, int convert_p)
9425 {
9426   tree type0, type1, orig_type0, orig_type1;
9427   tree eptype;
9428   enum tree_code code0, code1;
9429   tree op0, op1;
9430   tree ret = error_mark_node;
9431   const char *invalid_op_diag;
9432   bool op0_int_operands, op1_int_operands;
9433   bool int_const, int_const_or_overflow, int_operands;
9434 
9435   /* Expression code to give to the expression when it is built.
9436      Normally this is CODE, which is what the caller asked for,
9437      but in some special cases we change it.  */
9438   enum tree_code resultcode = code;
9439 
9440   /* Data type in which the computation is to be performed.
9441      In the simplest cases this is the common type of the arguments.  */
9442   tree result_type = NULL;
9443 
9444   /* When the computation is in excess precision, the type of the
9445      final EXCESS_PRECISION_EXPR.  */
9446   tree semantic_result_type = NULL;
9447 
9448   /* Nonzero means operands have already been type-converted
9449      in whatever way is necessary.
9450      Zero means they need to be converted to RESULT_TYPE.  */
9451   int converted = 0;
9452 
9453   /* Nonzero means create the expression with this type, rather than
9454      RESULT_TYPE.  */
9455   tree build_type = 0;
9456 
9457   /* Nonzero means after finally constructing the expression
9458      convert it to this type.  */
9459   tree final_type = 0;
9460 
9461   /* Nonzero if this is an operation like MIN or MAX which can
9462      safely be computed in short if both args are promoted shorts.
9463      Also implies COMMON.
9464      -1 indicates a bitwise operation; this makes a difference
9465      in the exact conditions for when it is safe to do the operation
9466      in a narrower mode.  */
9467   int shorten = 0;
9468 
9469   /* Nonzero if this is a comparison operation;
9470      if both args are promoted shorts, compare the original shorts.
9471      Also implies COMMON.  */
9472   int short_compare = 0;
9473 
9474   /* Nonzero if this is a right-shift operation, which can be computed on the
9475      original short and then promoted if the operand is a promoted short.  */
9476   int short_shift = 0;
9477 
9478   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
9479   int common = 0;
9480 
9481   /* True means types are compatible as far as ObjC is concerned.  */
9482   bool objc_ok;
9483 
9484   /* True means this is an arithmetic operation that may need excess
9485      precision.  */
9486   bool may_need_excess_precision;
9487 
9488   /* True means this is a boolean operation that converts both its
9489      operands to truth-values.  */
9490   bool boolean_op = false;
9491 
9492   if (location == UNKNOWN_LOCATION)
9493     location = input_location;
9494 
9495   op0 = orig_op0;
9496   op1 = orig_op1;
9497 
9498   op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9499   if (op0_int_operands)
9500     op0 = remove_c_maybe_const_expr (op0);
9501   op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9502   if (op1_int_operands)
9503     op1 = remove_c_maybe_const_expr (op1);
9504   int_operands = (op0_int_operands && op1_int_operands);
9505   if (int_operands)
9506     {
9507       int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9508 			       && TREE_CODE (orig_op1) == INTEGER_CST);
9509       int_const = (int_const_or_overflow
9510 		   && !TREE_OVERFLOW (orig_op0)
9511 		   && !TREE_OVERFLOW (orig_op1));
9512     }
9513   else
9514     int_const = int_const_or_overflow = false;
9515 
9516   /* Do not apply default conversion in mixed vector/scalar expression.  */
9517   if (convert_p
9518       && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
9519 	   != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
9520     {
9521       op0 = default_conversion (op0);
9522       op1 = default_conversion (op1);
9523     }
9524 
9525   orig_type0 = type0 = TREE_TYPE (op0);
9526   orig_type1 = type1 = TREE_TYPE (op1);
9527 
9528   /* The expression codes of the data types of the arguments tell us
9529      whether the arguments are integers, floating, pointers, etc.  */
9530   code0 = TREE_CODE (type0);
9531   code1 = TREE_CODE (type1);
9532 
9533   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
9534   STRIP_TYPE_NOPS (op0);
9535   STRIP_TYPE_NOPS (op1);
9536 
9537   /* If an error was already reported for one of the arguments,
9538      avoid reporting another error.  */
9539 
9540   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9541     return error_mark_node;
9542 
9543   if ((invalid_op_diag
9544        = targetm.invalid_binary_op (code, type0, type1)))
9545     {
9546       error_at (location, invalid_op_diag);
9547       return error_mark_node;
9548     }
9549 
9550   switch (code)
9551     {
9552     case PLUS_EXPR:
9553     case MINUS_EXPR:
9554     case MULT_EXPR:
9555     case TRUNC_DIV_EXPR:
9556     case CEIL_DIV_EXPR:
9557     case FLOOR_DIV_EXPR:
9558     case ROUND_DIV_EXPR:
9559     case EXACT_DIV_EXPR:
9560       may_need_excess_precision = true;
9561       break;
9562     default:
9563       may_need_excess_precision = false;
9564       break;
9565     }
9566   if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9567     {
9568       op0 = TREE_OPERAND (op0, 0);
9569       type0 = TREE_TYPE (op0);
9570     }
9571   else if (may_need_excess_precision
9572 	   && (eptype = excess_precision_type (type0)) != NULL_TREE)
9573     {
9574       type0 = eptype;
9575       op0 = convert (eptype, op0);
9576     }
9577   if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9578     {
9579       op1 = TREE_OPERAND (op1, 0);
9580       type1 = TREE_TYPE (op1);
9581     }
9582   else if (may_need_excess_precision
9583 	   && (eptype = excess_precision_type (type1)) != NULL_TREE)
9584     {
9585       type1 = eptype;
9586       op1 = convert (eptype, op1);
9587     }
9588 
9589   objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9590 
9591   /* In case when one of the operands of the binary operation is
9592      a vector and another is a scalar -- convert scalar to vector.  */
9593   if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
9594     {
9595       enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
9596 						     true);
9597 
9598       switch (convert_flag)
9599 	{
9600 	  case stv_error:
9601 	    return error_mark_node;
9602 	  case stv_firstarg:
9603 	    {
9604               bool maybe_const = true;
9605               tree sc;
9606               sc = c_fully_fold (op0, false, &maybe_const);
9607               sc = save_expr (sc);
9608               sc = convert (TREE_TYPE (type1), sc);
9609               op0 = build_vector_from_val (type1, sc);
9610               if (!maybe_const)
9611                 op0 = c_wrap_maybe_const (op0, true);
9612               orig_type0 = type0 = TREE_TYPE (op0);
9613               code0 = TREE_CODE (type0);
9614               converted = 1;
9615               break;
9616 	    }
9617 	  case stv_secondarg:
9618 	    {
9619 	      bool maybe_const = true;
9620 	      tree sc;
9621 	      sc = c_fully_fold (op1, false, &maybe_const);
9622 	      sc = save_expr (sc);
9623 	      sc = convert (TREE_TYPE (type0), sc);
9624 	      op1 = build_vector_from_val (type0, sc);
9625 	      if (!maybe_const)
9626 		op1 = c_wrap_maybe_const (op1, true);
9627 	      orig_type1 = type1 = TREE_TYPE (op1);
9628 	      code1 = TREE_CODE (type1);
9629 	      converted = 1;
9630 	      break;
9631 	    }
9632 	  default:
9633 	    break;
9634 	}
9635     }
9636 
9637   switch (code)
9638     {
9639     case PLUS_EXPR:
9640       /* Handle the pointer + int case.  */
9641       if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9642 	{
9643 	  ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
9644 	  goto return_build_binary_op;
9645 	}
9646       else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
9647 	{
9648 	  ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
9649 	  goto return_build_binary_op;
9650 	}
9651       else
9652 	common = 1;
9653       break;
9654 
9655     case MINUS_EXPR:
9656       /* Subtraction of two similar pointers.
9657 	 We must subtract them as integers, then divide by object size.  */
9658       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
9659 	  && comp_target_types (location, type0, type1))
9660 	{
9661 	  ret = pointer_diff (location, op0, op1);
9662 	  goto return_build_binary_op;
9663 	}
9664       /* Handle pointer minus int.  Just like pointer plus int.  */
9665       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
9666 	{
9667 	  ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
9668 	  goto return_build_binary_op;
9669 	}
9670       else
9671 	common = 1;
9672       break;
9673 
9674     case MULT_EXPR:
9675       common = 1;
9676       break;
9677 
9678     case TRUNC_DIV_EXPR:
9679     case CEIL_DIV_EXPR:
9680     case FLOOR_DIV_EXPR:
9681     case ROUND_DIV_EXPR:
9682     case EXACT_DIV_EXPR:
9683       warn_for_div_by_zero (location, op1);
9684 
9685       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9686 	   || code0 == FIXED_POINT_TYPE
9687 	   || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9688 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9689 	      || code1 == FIXED_POINT_TYPE
9690 	      || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
9691 	{
9692 	  enum tree_code tcode0 = code0, tcode1 = code1;
9693 
9694 	  if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9695 	    tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
9696 	  if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
9697 	    tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
9698 
9699 	  if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9700 	      || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
9701 	    resultcode = RDIV_EXPR;
9702 	  else
9703 	    /* Although it would be tempting to shorten always here, that
9704 	       loses on some targets, since the modulo instruction is
9705 	       undefined if the quotient can't be represented in the
9706 	       computation mode.  We shorten only if unsigned or if
9707 	       dividing by something we know != -1.  */
9708 	    shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9709 		       || (TREE_CODE (op1) == INTEGER_CST
9710 			   && !integer_all_onesp (op1)));
9711 	  common = 1;
9712 	}
9713       break;
9714 
9715     case BIT_AND_EXPR:
9716     case BIT_IOR_EXPR:
9717     case BIT_XOR_EXPR:
9718       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9719 	shorten = -1;
9720       /* Allow vector types which are not floating point types.   */
9721       else if (code0 == VECTOR_TYPE
9722 	       && code1 == VECTOR_TYPE
9723 	       && !VECTOR_FLOAT_TYPE_P (type0)
9724 	       && !VECTOR_FLOAT_TYPE_P (type1))
9725 	common = 1;
9726       break;
9727 
9728     case TRUNC_MOD_EXPR:
9729     case FLOOR_MOD_EXPR:
9730       warn_for_div_by_zero (location, op1);
9731 
9732       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9733 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9734 	  && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9735 	common = 1;
9736       else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9737 	{
9738 	  /* Although it would be tempting to shorten always here, that loses
9739 	     on some targets, since the modulo instruction is undefined if the
9740 	     quotient can't be represented in the computation mode.  We shorten
9741 	     only if unsigned or if dividing by something we know != -1.  */
9742 	  shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
9743 		     || (TREE_CODE (op1) == INTEGER_CST
9744 			 && !integer_all_onesp (op1)));
9745 	  common = 1;
9746 	}
9747       break;
9748 
9749     case TRUTH_ANDIF_EXPR:
9750     case TRUTH_ORIF_EXPR:
9751     case TRUTH_AND_EXPR:
9752     case TRUTH_OR_EXPR:
9753     case TRUTH_XOR_EXPR:
9754       if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9755 	   || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9756 	   || code0 == FIXED_POINT_TYPE)
9757 	  && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9758 	      || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9759 	      || code1 == FIXED_POINT_TYPE))
9760 	{
9761 	  /* Result of these operations is always an int,
9762 	     but that does not mean the operands should be
9763 	     converted to ints!  */
9764 	  result_type = integer_type_node;
9765 	  if (op0_int_operands)
9766 	    {
9767 	      op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
9768 	      op0 = remove_c_maybe_const_expr (op0);
9769 	    }
9770 	  else
9771 	    op0 = c_objc_common_truthvalue_conversion (location, op0);
9772 	  if (op1_int_operands)
9773 	    {
9774 	      op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
9775 	      op1 = remove_c_maybe_const_expr (op1);
9776 	    }
9777 	  else
9778 	    op1 = c_objc_common_truthvalue_conversion (location, op1);
9779 	  converted = 1;
9780 	  boolean_op = true;
9781 	}
9782       if (code == TRUTH_ANDIF_EXPR)
9783 	{
9784 	  int_const_or_overflow = (int_operands
9785 				   && TREE_CODE (orig_op0) == INTEGER_CST
9786 				   && (op0 == truthvalue_false_node
9787 				       || TREE_CODE (orig_op1) == INTEGER_CST));
9788 	  int_const = (int_const_or_overflow
9789 		       && !TREE_OVERFLOW (orig_op0)
9790 		       && (op0 == truthvalue_false_node
9791 			   || !TREE_OVERFLOW (orig_op1)));
9792 	}
9793       else if (code == TRUTH_ORIF_EXPR)
9794 	{
9795 	  int_const_or_overflow = (int_operands
9796 				   && TREE_CODE (orig_op0) == INTEGER_CST
9797 				   && (op0 == truthvalue_true_node
9798 				       || TREE_CODE (orig_op1) == INTEGER_CST));
9799 	  int_const = (int_const_or_overflow
9800 		       && !TREE_OVERFLOW (orig_op0)
9801 		       && (op0 == truthvalue_true_node
9802 			   || !TREE_OVERFLOW (orig_op1)));
9803 	}
9804       break;
9805 
9806       /* Shift operations: result has same type as first operand;
9807 	 always convert second operand to int.
9808 	 Also set SHORT_SHIFT if shifting rightward.  */
9809 
9810     case RSHIFT_EXPR:
9811       if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9812           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9813         {
9814           result_type = type0;
9815           converted = 1;
9816         }
9817       else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9818 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9819           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9820           && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9821 	{
9822 	  result_type = type0;
9823 	  converted = 1;
9824 	}
9825       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9826 	  && code1 == INTEGER_TYPE)
9827 	{
9828 	  if (TREE_CODE (op1) == INTEGER_CST)
9829 	    {
9830 	      if (tree_int_cst_sgn (op1) < 0)
9831 		{
9832 		  int_const = false;
9833 		  if (c_inhibit_evaluation_warnings == 0)
9834 		    warning (0, "right shift count is negative");
9835 		}
9836 	      else
9837 		{
9838 		  if (!integer_zerop (op1))
9839 		    short_shift = 1;
9840 
9841 		  if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9842 		    {
9843 		      int_const = false;
9844 		      if (c_inhibit_evaluation_warnings == 0)
9845 			warning (0, "right shift count >= width of type");
9846 		    }
9847 		}
9848 	    }
9849 
9850 	  /* Use the type of the value to be shifted.  */
9851 	  result_type = type0;
9852 	  /* Convert the non vector shift-count to an integer, regardless
9853 	     of size of value being shifted.  */
9854 	  if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9855 	      && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9856 	    op1 = convert (integer_type_node, op1);
9857 	  /* Avoid converting op1 to result_type later.  */
9858 	  converted = 1;
9859 	}
9860       break;
9861 
9862     case LSHIFT_EXPR:
9863       if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9864           && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9865         {
9866           result_type = type0;
9867           converted = 1;
9868         }
9869       else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9870 	  && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9871           && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9872           && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9873 	{
9874 	  result_type = type0;
9875 	  converted = 1;
9876 	}
9877       else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
9878 	  && code1 == INTEGER_TYPE)
9879 	{
9880 	  if (TREE_CODE (op1) == INTEGER_CST)
9881 	    {
9882 	      if (tree_int_cst_sgn (op1) < 0)
9883 		{
9884 		  int_const = false;
9885 		  if (c_inhibit_evaluation_warnings == 0)
9886 		    warning (0, "left shift count is negative");
9887 		}
9888 
9889 	      else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
9890 		{
9891 		  int_const = false;
9892 		  if (c_inhibit_evaluation_warnings == 0)
9893 		    warning (0, "left shift count >= width of type");
9894 		}
9895 	    }
9896 
9897 	  /* Use the type of the value to be shifted.  */
9898 	  result_type = type0;
9899 	  /* Convert the non vector shift-count to an integer, regardless
9900 	     of size of value being shifted.  */
9901 	  if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9902 	      && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
9903 	    op1 = convert (integer_type_node, op1);
9904 	  /* Avoid converting op1 to result_type later.  */
9905 	  converted = 1;
9906 	}
9907       break;
9908 
9909     case EQ_EXPR:
9910     case NE_EXPR:
9911       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
9912         {
9913           tree intt;
9914           if (TREE_TYPE (type0) != TREE_TYPE (type1))
9915             {
9916               error_at (location, "comparing vectors with different "
9917                                   "element types");
9918               return error_mark_node;
9919             }
9920 
9921           if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
9922             {
9923               error_at (location, "comparing vectors with different "
9924                                   "number of elements");
9925               return error_mark_node;
9926             }
9927 
9928           /* Always construct signed integer vector type.  */
9929           intt = c_common_type_for_size (GET_MODE_BITSIZE
9930 					   (TYPE_MODE (TREE_TYPE (type0))), 0);
9931           result_type = build_opaque_vector_type (intt,
9932 						  TYPE_VECTOR_SUBPARTS (type0));
9933           converted = 1;
9934           break;
9935         }
9936       if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
9937 	warning_at (location,
9938 		    OPT_Wfloat_equal,
9939 		    "comparing floating point with == or != is unsafe");
9940       /* Result of comparison is always int,
9941 	 but don't convert the args to int!  */
9942       build_type = integer_type_node;
9943       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9944 	   || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
9945 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9946 	      || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
9947 	short_compare = 1;
9948       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
9949 	{
9950 	  if (TREE_CODE (op0) == ADDR_EXPR
9951 	      && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
9952 	    {
9953 	      if (code == EQ_EXPR)
9954 		warning_at (location,
9955 			    OPT_Waddress,
9956 			    "the comparison will always evaluate as %<false%> "
9957 			    "for the address of %qD will never be NULL",
9958 			    TREE_OPERAND (op0, 0));
9959 	      else
9960 		warning_at (location,
9961 			    OPT_Waddress,
9962 			    "the comparison will always evaluate as %<true%> "
9963 			    "for the address of %qD will never be NULL",
9964 			    TREE_OPERAND (op0, 0));
9965 	    }
9966 	  result_type = type0;
9967 	}
9968       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
9969 	{
9970 	  if (TREE_CODE (op1) == ADDR_EXPR
9971 	      && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
9972 	    {
9973 	      if (code == EQ_EXPR)
9974 		warning_at (location,
9975 			    OPT_Waddress,
9976 			    "the comparison will always evaluate as %<false%> "
9977 			    "for the address of %qD will never be NULL",
9978 			    TREE_OPERAND (op1, 0));
9979 	      else
9980 		warning_at (location,
9981 			    OPT_Waddress,
9982 			    "the comparison will always evaluate as %<true%> "
9983 			    "for the address of %qD will never be NULL",
9984 			    TREE_OPERAND (op1, 0));
9985 	    }
9986 	  result_type = type1;
9987 	}
9988       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
9989 	{
9990 	  tree tt0 = TREE_TYPE (type0);
9991 	  tree tt1 = TREE_TYPE (type1);
9992 	  addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
9993 	  addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
9994 	  addr_space_t as_common = ADDR_SPACE_GENERIC;
9995 
9996 	  /* Anything compares with void *.  void * compares with anything.
9997 	     Otherwise, the targets must be compatible
9998 	     and both must be object or both incomplete.  */
9999 	  if (comp_target_types (location, type0, type1))
10000 	    result_type = common_pointer_type (type0, type1);
10001 	  else if (!addr_space_superset (as0, as1, &as_common))
10002 	    {
10003 	      error_at (location, "comparison of pointers to "
10004 			"disjoint address spaces");
10005 	      return error_mark_node;
10006 	    }
10007 	  else if (VOID_TYPE_P (tt0))
10008 	    {
10009 	      if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
10010 		pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10011 			 "comparison of %<void *%> with function pointer");
10012 	    }
10013 	  else if (VOID_TYPE_P (tt1))
10014 	    {
10015 	      if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
10016 		pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10017 			 "comparison of %<void *%> with function pointer");
10018 	    }
10019 	  else
10020 	    /* Avoid warning about the volatile ObjC EH puts on decls.  */
10021 	    if (!objc_ok)
10022 	      pedwarn (location, 0,
10023 		       "comparison of distinct pointer types lacks a cast");
10024 
10025 	  if (result_type == NULL_TREE)
10026 	    {
10027 	      int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10028 	      result_type = build_pointer_type
10029 			      (build_qualified_type (void_type_node, qual));
10030 	    }
10031 	}
10032       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10033 	{
10034 	  result_type = type0;
10035 	  pedwarn (location, 0, "comparison between pointer and integer");
10036 	}
10037       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10038 	{
10039 	  result_type = type1;
10040 	  pedwarn (location, 0, "comparison between pointer and integer");
10041 	}
10042       break;
10043 
10044     case LE_EXPR:
10045     case GE_EXPR:
10046     case LT_EXPR:
10047     case GT_EXPR:
10048       if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10049         {
10050           tree intt;
10051           if (TREE_TYPE (type0) != TREE_TYPE (type1))
10052             {
10053               error_at (location, "comparing vectors with different "
10054                                   "element types");
10055               return error_mark_node;
10056             }
10057 
10058           if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10059             {
10060               error_at (location, "comparing vectors with different "
10061                                   "number of elements");
10062               return error_mark_node;
10063             }
10064 
10065           /* Always construct signed integer vector type.  */
10066           intt = c_common_type_for_size (GET_MODE_BITSIZE
10067 					   (TYPE_MODE (TREE_TYPE (type0))), 0);
10068           result_type = build_opaque_vector_type (intt,
10069 						  TYPE_VECTOR_SUBPARTS (type0));
10070           converted = 1;
10071           break;
10072         }
10073       build_type = integer_type_node;
10074       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10075 	   || code0 == FIXED_POINT_TYPE)
10076 	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10077 	      || code1 == FIXED_POINT_TYPE))
10078 	short_compare = 1;
10079       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10080 	{
10081 	  addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10082 	  addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10083 	  addr_space_t as_common;
10084 
10085 	  if (comp_target_types (location, type0, type1))
10086 	    {
10087 	      result_type = common_pointer_type (type0, type1);
10088 	      if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10089 		  != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
10090 		pedwarn (location, 0,
10091 			 "comparison of complete and incomplete pointers");
10092 	      else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
10093 		pedwarn (location, OPT_Wpedantic, "ISO C forbids "
10094 			 "ordered comparisons of pointers to functions");
10095 	      else if (null_pointer_constant_p (orig_op0)
10096 		       || null_pointer_constant_p (orig_op1))
10097 		warning_at (location, OPT_Wextra,
10098 			    "ordered comparison of pointer with null pointer");
10099 
10100 	    }
10101 	  else if (!addr_space_superset (as0, as1, &as_common))
10102 	    {
10103 	      error_at (location, "comparison of pointers to "
10104 			"disjoint address spaces");
10105 	      return error_mark_node;
10106 	    }
10107 	  else
10108 	    {
10109 	      int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10110 	      result_type = build_pointer_type
10111 			      (build_qualified_type (void_type_node, qual));
10112 	      pedwarn (location, 0,
10113 		       "comparison of distinct pointer types lacks a cast");
10114 	    }
10115 	}
10116       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10117 	{
10118 	  result_type = type0;
10119 	  if (pedantic)
10120 	    pedwarn (location, OPT_Wpedantic,
10121 		     "ordered comparison of pointer with integer zero");
10122 	  else if (extra_warnings)
10123 	    warning_at (location, OPT_Wextra,
10124 			"ordered comparison of pointer with integer zero");
10125 	}
10126       else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10127 	{
10128 	  result_type = type1;
10129 	  if (pedantic)
10130 	    pedwarn (location, OPT_Wpedantic,
10131 		     "ordered comparison of pointer with integer zero");
10132 	  else if (extra_warnings)
10133 	    warning_at (location, OPT_Wextra,
10134 			"ordered comparison of pointer with integer zero");
10135 	}
10136       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10137 	{
10138 	  result_type = type0;
10139 	  pedwarn (location, 0, "comparison between pointer and integer");
10140 	}
10141       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10142 	{
10143 	  result_type = type1;
10144 	  pedwarn (location, 0, "comparison between pointer and integer");
10145 	}
10146       break;
10147 
10148     default:
10149       gcc_unreachable ();
10150     }
10151 
10152   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10153     return error_mark_node;
10154 
10155   if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10156       && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10157 	  || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10158 						    TREE_TYPE (type1))))
10159     {
10160       binary_op_error (location, code, type0, type1);
10161       return error_mark_node;
10162     }
10163 
10164   if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10165        || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
10166       &&
10167       (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10168        || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
10169     {
10170       bool first_complex = (code0 == COMPLEX_TYPE);
10171       bool second_complex = (code1 == COMPLEX_TYPE);
10172       int none_complex = (!first_complex && !second_complex);
10173 
10174       if (shorten || common || short_compare)
10175 	{
10176 	  result_type = c_common_type (type0, type1);
10177 	  do_warn_double_promotion (result_type, type0, type1,
10178 				    "implicit conversion from %qT to %qT "
10179 				    "to match other operand of binary "
10180 				    "expression",
10181 				    location);
10182 	  if (result_type == error_mark_node)
10183 	    return error_mark_node;
10184 	}
10185 
10186       if (first_complex != second_complex
10187 	  && (code == PLUS_EXPR
10188 	      || code == MINUS_EXPR
10189 	      || code == MULT_EXPR
10190 	      || (code == TRUNC_DIV_EXPR && first_complex))
10191 	  && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10192 	  && flag_signed_zeros)
10193 	{
10194 	  /* An operation on mixed real/complex operands must be
10195 	     handled specially, but the language-independent code can
10196 	     more easily optimize the plain complex arithmetic if
10197 	     -fno-signed-zeros.  */
10198 	  tree real_type = TREE_TYPE (result_type);
10199 	  tree real, imag;
10200 	  if (type0 != orig_type0 || type1 != orig_type1)
10201 	    {
10202 	      gcc_assert (may_need_excess_precision && common);
10203 	      semantic_result_type = c_common_type (orig_type0, orig_type1);
10204 	    }
10205 	  if (first_complex)
10206 	    {
10207 	      if (TREE_TYPE (op0) != result_type)
10208 		op0 = convert_and_check (result_type, op0);
10209 	      if (TREE_TYPE (op1) != real_type)
10210 		op1 = convert_and_check (real_type, op1);
10211 	    }
10212 	  else
10213 	    {
10214 	      if (TREE_TYPE (op0) != real_type)
10215 		op0 = convert_and_check (real_type, op0);
10216 	      if (TREE_TYPE (op1) != result_type)
10217 		op1 = convert_and_check (result_type, op1);
10218 	    }
10219 	  if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10220 	    return error_mark_node;
10221 	  if (first_complex)
10222 	    {
10223 	      op0 = c_save_expr (op0);
10224 	      real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10225 				     op0, 1);
10226 	      imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10227 				     op0, 1);
10228 	      switch (code)
10229 		{
10230 		case MULT_EXPR:
10231 		case TRUNC_DIV_EXPR:
10232 		  op1 = c_save_expr (op1);
10233 		  imag = build2 (resultcode, real_type, imag, op1);
10234 		  /* Fall through.  */
10235 		case PLUS_EXPR:
10236 		case MINUS_EXPR:
10237 		  real = build2 (resultcode, real_type, real, op1);
10238 		  break;
10239 		default:
10240 		  gcc_unreachable();
10241 		}
10242 	    }
10243 	  else
10244 	    {
10245 	      op1 = c_save_expr (op1);
10246 	      real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10247 				     op1, 1);
10248 	      imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10249 				     op1, 1);
10250 	      switch (code)
10251 		{
10252 		case MULT_EXPR:
10253 		  op0 = c_save_expr (op0);
10254 		  imag = build2 (resultcode, real_type, op0, imag);
10255 		  /* Fall through.  */
10256 		case PLUS_EXPR:
10257 		  real = build2 (resultcode, real_type, op0, real);
10258 		  break;
10259 		case MINUS_EXPR:
10260 		  real = build2 (resultcode, real_type, op0, real);
10261 		  imag = build1 (NEGATE_EXPR, real_type, imag);
10262 		  break;
10263 		default:
10264 		  gcc_unreachable();
10265 		}
10266 	    }
10267 	  ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10268 	  goto return_build_binary_op;
10269 	}
10270 
10271       /* For certain operations (which identify themselves by shorten != 0)
10272 	 if both args were extended from the same smaller type,
10273 	 do the arithmetic in that type and then extend.
10274 
10275 	 shorten !=0 and !=1 indicates a bitwise operation.
10276 	 For them, this optimization is safe only if
10277 	 both args are zero-extended or both are sign-extended.
10278 	 Otherwise, we might change the result.
10279 	 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10280 	 but calculated in (unsigned short) it would be (unsigned short)-1.  */
10281 
10282       if (shorten && none_complex)
10283 	{
10284 	  final_type = result_type;
10285 	  result_type = shorten_binary_op (result_type, op0, op1,
10286 					   shorten == -1);
10287 	}
10288 
10289       /* Shifts can be shortened if shifting right.  */
10290 
10291       if (short_shift)
10292 	{
10293 	  int unsigned_arg;
10294 	  tree arg0 = get_narrower (op0, &unsigned_arg);
10295 
10296 	  final_type = result_type;
10297 
10298 	  if (arg0 == op0 && final_type == TREE_TYPE (op0))
10299 	    unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
10300 
10301 	  if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
10302 	      && tree_int_cst_sgn (op1) > 0
10303 	      /* We can shorten only if the shift count is less than the
10304 		 number of bits in the smaller type size.  */
10305 	      && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10306 	      /* We cannot drop an unsigned shift after sign-extension.  */
10307 	      && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
10308 	    {
10309 	      /* Do an unsigned shift if the operand was zero-extended.  */
10310 	      result_type
10311 		= c_common_signed_or_unsigned_type (unsigned_arg,
10312 						    TREE_TYPE (arg0));
10313 	      /* Convert value-to-be-shifted to that type.  */
10314 	      if (TREE_TYPE (op0) != result_type)
10315 		op0 = convert (result_type, op0);
10316 	      converted = 1;
10317 	    }
10318 	}
10319 
10320       /* Comparison operations are shortened too but differently.
10321 	 They identify themselves by setting short_compare = 1.  */
10322 
10323       if (short_compare)
10324 	{
10325 	  /* Don't write &op0, etc., because that would prevent op0
10326 	     from being kept in a register.
10327 	     Instead, make copies of the our local variables and
10328 	     pass the copies by reference, then copy them back afterward.  */
10329 	  tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10330 	  enum tree_code xresultcode = resultcode;
10331 	  tree val
10332 	    = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
10333 
10334 	  if (val != 0)
10335 	    {
10336 	      ret = val;
10337 	      goto return_build_binary_op;
10338 	    }
10339 
10340 	  op0 = xop0, op1 = xop1;
10341 	  converted = 1;
10342 	  resultcode = xresultcode;
10343 
10344 	  if (c_inhibit_evaluation_warnings == 0)
10345 	    {
10346 	      bool op0_maybe_const = true;
10347 	      bool op1_maybe_const = true;
10348 	      tree orig_op0_folded, orig_op1_folded;
10349 
10350 	      if (in_late_binary_op)
10351 		{
10352 		  orig_op0_folded = orig_op0;
10353 		  orig_op1_folded = orig_op1;
10354 		}
10355 	      else
10356 		{
10357 		  /* Fold for the sake of possible warnings, as in
10358 		     build_conditional_expr.  This requires the
10359 		     "original" values to be folded, not just op0 and
10360 		     op1.  */
10361 		  c_inhibit_evaluation_warnings++;
10362 		  op0 = c_fully_fold (op0, require_constant_value,
10363 				      &op0_maybe_const);
10364 		  op1 = c_fully_fold (op1, require_constant_value,
10365 				      &op1_maybe_const);
10366 		  c_inhibit_evaluation_warnings--;
10367 		  orig_op0_folded = c_fully_fold (orig_op0,
10368 						  require_constant_value,
10369 						  NULL);
10370 		  orig_op1_folded = c_fully_fold (orig_op1,
10371 						  require_constant_value,
10372 						  NULL);
10373 		}
10374 
10375 	      if (warn_sign_compare)
10376 		warn_for_sign_compare (location, orig_op0_folded,
10377 				       orig_op1_folded, op0, op1,
10378 				       result_type, resultcode);
10379 	      if (!in_late_binary_op && !int_operands)
10380 		{
10381 		  if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
10382 		    op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
10383 		  if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
10384 		    op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
10385 		}
10386 	    }
10387 	}
10388     }
10389 
10390   /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10391      If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10392      Then the expression will be built.
10393      It will be given type FINAL_TYPE if that is nonzero;
10394      otherwise, it will be given type RESULT_TYPE.  */
10395 
10396   if (!result_type)
10397     {
10398       binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
10399       return error_mark_node;
10400     }
10401 
10402   if (build_type == NULL_TREE)
10403     {
10404       build_type = result_type;
10405       if ((type0 != orig_type0 || type1 != orig_type1)
10406 	  && !boolean_op)
10407 	{
10408 	  gcc_assert (may_need_excess_precision && common);
10409 	  semantic_result_type = c_common_type (orig_type0, orig_type1);
10410 	}
10411     }
10412 
10413   if (!converted)
10414     {
10415       op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10416       op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10417 
10418       /* This can happen if one operand has a vector type, and the other
10419 	 has a different type.  */
10420       if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10421 	return error_mark_node;
10422     }
10423 
10424   /* Treat expressions in initializers specially as they can't trap.  */
10425   if (int_const_or_overflow)
10426     ret = (require_constant_value
10427 	   ? fold_build2_initializer_loc (location, resultcode, build_type,
10428 					  op0, op1)
10429 	   : fold_build2_loc (location, resultcode, build_type, op0, op1));
10430   else
10431     ret = build2 (resultcode, build_type, op0, op1);
10432   if (final_type != 0)
10433     ret = convert (final_type, ret);
10434 
10435  return_build_binary_op:
10436   gcc_assert (ret != error_mark_node);
10437   if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10438     ret = (int_operands
10439 	   ? note_integer_operands (ret)
10440 	   : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10441   else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10442 	   && !in_late_binary_op)
10443     ret = note_integer_operands (ret);
10444   if (semantic_result_type)
10445     ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
10446   protected_set_expr_location (ret, location);
10447   return ret;
10448 }
10449 
10450 
10451 /* Convert EXPR to be a truth-value, validating its type for this
10452    purpose.  LOCATION is the source location for the expression.  */
10453 
10454 tree
c_objc_common_truthvalue_conversion(location_t location,tree expr)10455 c_objc_common_truthvalue_conversion (location_t location, tree expr)
10456 {
10457   bool int_const, int_operands;
10458 
10459   switch (TREE_CODE (TREE_TYPE (expr)))
10460     {
10461     case ARRAY_TYPE:
10462       error_at (location, "used array that cannot be converted to pointer where scalar is required");
10463       return error_mark_node;
10464 
10465     case RECORD_TYPE:
10466       error_at (location, "used struct type value where scalar is required");
10467       return error_mark_node;
10468 
10469     case UNION_TYPE:
10470       error_at (location, "used union type value where scalar is required");
10471       return error_mark_node;
10472 
10473     case VOID_TYPE:
10474       error_at (location, "void value not ignored as it ought to be");
10475       return error_mark_node;
10476 
10477     case FUNCTION_TYPE:
10478       gcc_unreachable ();
10479 
10480     case VECTOR_TYPE:
10481       error_at (location, "used vector type where scalar is required");
10482       return error_mark_node;
10483 
10484     default:
10485       break;
10486     }
10487 
10488   int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10489   int_operands = EXPR_INT_CONST_OPERANDS (expr);
10490   if (int_operands && TREE_CODE (expr) != INTEGER_CST)
10491     {
10492       expr = remove_c_maybe_const_expr (expr);
10493       expr = build2 (NE_EXPR, integer_type_node, expr,
10494 		     convert (TREE_TYPE (expr), integer_zero_node));
10495       expr = note_integer_operands (expr);
10496     }
10497   else
10498     /* ??? Should we also give an error for vectors rather than leaving
10499        those to give errors later?  */
10500     expr = c_common_truthvalue_conversion (location, expr);
10501 
10502   if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10503     {
10504       if (TREE_OVERFLOW (expr))
10505 	return expr;
10506       else
10507 	return note_integer_operands (expr);
10508     }
10509   if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10510     return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10511   return expr;
10512 }
10513 
10514 
10515 /* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10516    required.  */
10517 
10518 tree
c_expr_to_decl(tree expr,bool * tc ATTRIBUTE_UNUSED,bool * se)10519 c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
10520 {
10521   if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10522     {
10523       tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10524       /* Executing a compound literal inside a function reinitializes
10525 	 it.  */
10526       if (!TREE_STATIC (decl))
10527 	*se = true;
10528       return decl;
10529     }
10530   else
10531     return expr;
10532 }
10533 
10534 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
10535 
10536 tree
c_begin_omp_parallel(void)10537 c_begin_omp_parallel (void)
10538 {
10539   tree block;
10540 
10541   keep_next_level ();
10542   block = c_begin_compound_stmt (true);
10543 
10544   return block;
10545 }
10546 
10547 /* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10548    statement.  LOC is the location of the OMP_PARALLEL.  */
10549 
10550 tree
c_finish_omp_parallel(location_t loc,tree clauses,tree block)10551 c_finish_omp_parallel (location_t loc, tree clauses, tree block)
10552 {
10553   tree stmt;
10554 
10555   block = c_end_compound_stmt (loc, block, true);
10556 
10557   stmt = make_node (OMP_PARALLEL);
10558   TREE_TYPE (stmt) = void_type_node;
10559   OMP_PARALLEL_CLAUSES (stmt) = clauses;
10560   OMP_PARALLEL_BODY (stmt) = block;
10561   SET_EXPR_LOCATION (stmt, loc);
10562 
10563   return add_stmt (stmt);
10564 }
10565 
10566 /* Like c_begin_compound_stmt, except force the retention of the BLOCK.  */
10567 
10568 tree
c_begin_omp_task(void)10569 c_begin_omp_task (void)
10570 {
10571   tree block;
10572 
10573   keep_next_level ();
10574   block = c_begin_compound_stmt (true);
10575 
10576   return block;
10577 }
10578 
10579 /* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10580    statement.  LOC is the location of the #pragma.  */
10581 
10582 tree
c_finish_omp_task(location_t loc,tree clauses,tree block)10583 c_finish_omp_task (location_t loc, tree clauses, tree block)
10584 {
10585   tree stmt;
10586 
10587   block = c_end_compound_stmt (loc, block, true);
10588 
10589   stmt = make_node (OMP_TASK);
10590   TREE_TYPE (stmt) = void_type_node;
10591   OMP_TASK_CLAUSES (stmt) = clauses;
10592   OMP_TASK_BODY (stmt) = block;
10593   SET_EXPR_LOCATION (stmt, loc);
10594 
10595   return add_stmt (stmt);
10596 }
10597 
10598 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
10599    Remove any elements from the list that are invalid.  */
10600 
10601 tree
c_finish_omp_clauses(tree clauses)10602 c_finish_omp_clauses (tree clauses)
10603 {
10604   bitmap_head generic_head, firstprivate_head, lastprivate_head;
10605   tree c, t, *pc = &clauses;
10606   const char *name;
10607 
10608   bitmap_obstack_initialize (NULL);
10609   bitmap_initialize (&generic_head, &bitmap_default_obstack);
10610   bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10611   bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10612 
10613   for (pc = &clauses, c = clauses; c ; c = *pc)
10614     {
10615       bool remove = false;
10616       bool need_complete = false;
10617       bool need_implicitly_determined = false;
10618 
10619       switch (OMP_CLAUSE_CODE (c))
10620 	{
10621 	case OMP_CLAUSE_SHARED:
10622 	  name = "shared";
10623 	  need_implicitly_determined = true;
10624 	  goto check_dup_generic;
10625 
10626 	case OMP_CLAUSE_PRIVATE:
10627 	  name = "private";
10628 	  need_complete = true;
10629 	  need_implicitly_determined = true;
10630 	  goto check_dup_generic;
10631 
10632 	case OMP_CLAUSE_REDUCTION:
10633 	  name = "reduction";
10634 	  need_implicitly_determined = true;
10635 	  t = OMP_CLAUSE_DECL (c);
10636 	  if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10637 	      || POINTER_TYPE_P (TREE_TYPE (t)))
10638 	    {
10639 	      error_at (OMP_CLAUSE_LOCATION (c),
10640 			"%qE has invalid type for %<reduction%>", t);
10641 	      remove = true;
10642 	    }
10643 	  else if (FLOAT_TYPE_P (TREE_TYPE (t))
10644 		   || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
10645 	    {
10646 	      enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10647 	      const char *r_name = NULL;
10648 
10649 	      switch (r_code)
10650 		{
10651 		case PLUS_EXPR:
10652 		case MULT_EXPR:
10653 		case MINUS_EXPR:
10654 		  break;
10655 		case MIN_EXPR:
10656 		  if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
10657 		    r_name = "min";
10658 		  break;
10659 		case MAX_EXPR:
10660 		  if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
10661 		    r_name = "max";
10662 		  break;
10663 		case BIT_AND_EXPR:
10664 		  r_name = "&";
10665 		  break;
10666 		case BIT_XOR_EXPR:
10667 		  r_name = "^";
10668 		  break;
10669 		case BIT_IOR_EXPR:
10670 		  r_name = "|";
10671 		  break;
10672 		case TRUTH_ANDIF_EXPR:
10673 		  if (FLOAT_TYPE_P (TREE_TYPE (t)))
10674 		    r_name = "&&";
10675 		  break;
10676 		case TRUTH_ORIF_EXPR:
10677 		  if (FLOAT_TYPE_P (TREE_TYPE (t)))
10678 		    r_name = "||";
10679 		  break;
10680 		default:
10681 		  gcc_unreachable ();
10682 		}
10683 	      if (r_name)
10684 		{
10685 		  error_at (OMP_CLAUSE_LOCATION (c),
10686 			    "%qE has invalid type for %<reduction(%s)%>",
10687 			    t, r_name);
10688 		  remove = true;
10689 		}
10690 	    }
10691 	  goto check_dup_generic;
10692 
10693 	case OMP_CLAUSE_COPYPRIVATE:
10694 	  name = "copyprivate";
10695 	  goto check_dup_generic;
10696 
10697 	case OMP_CLAUSE_COPYIN:
10698 	  name = "copyin";
10699 	  t = OMP_CLAUSE_DECL (c);
10700 	  if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10701 	    {
10702 	      error_at (OMP_CLAUSE_LOCATION (c),
10703 			"%qE must be %<threadprivate%> for %<copyin%>", t);
10704 	      remove = true;
10705 	    }
10706 	  goto check_dup_generic;
10707 
10708 	check_dup_generic:
10709 	  t = OMP_CLAUSE_DECL (c);
10710 	  if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10711 	    {
10712 	      error_at (OMP_CLAUSE_LOCATION (c),
10713 			"%qE is not a variable in clause %qs", t, name);
10714 	      remove = true;
10715 	    }
10716 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10717 		   || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10718 		   || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10719 	    {
10720 	      error_at (OMP_CLAUSE_LOCATION (c),
10721 			"%qE appears more than once in data clauses", t);
10722 	      remove = true;
10723 	    }
10724 	  else
10725 	    bitmap_set_bit (&generic_head, DECL_UID (t));
10726 	  break;
10727 
10728 	case OMP_CLAUSE_FIRSTPRIVATE:
10729 	  name = "firstprivate";
10730 	  t = OMP_CLAUSE_DECL (c);
10731 	  need_complete = true;
10732 	  need_implicitly_determined = true;
10733 	  if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10734 	    {
10735 	      error_at (OMP_CLAUSE_LOCATION (c),
10736 			"%qE is not a variable in clause %<firstprivate%>", t);
10737 	      remove = true;
10738 	    }
10739 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10740 		   || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10741 	    {
10742 	      error_at (OMP_CLAUSE_LOCATION (c),
10743 			"%qE appears more than once in data clauses", t);
10744 	      remove = true;
10745 	    }
10746 	  else
10747 	    bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10748 	  break;
10749 
10750 	case OMP_CLAUSE_LASTPRIVATE:
10751 	  name = "lastprivate";
10752 	  t = OMP_CLAUSE_DECL (c);
10753 	  need_complete = true;
10754 	  need_implicitly_determined = true;
10755 	  if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10756 	    {
10757 	      error_at (OMP_CLAUSE_LOCATION (c),
10758 			"%qE is not a variable in clause %<lastprivate%>", t);
10759 	      remove = true;
10760 	    }
10761 	  else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10762 		   || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10763 	    {
10764 	      error_at (OMP_CLAUSE_LOCATION (c),
10765 		     "%qE appears more than once in data clauses", t);
10766 	      remove = true;
10767 	    }
10768 	  else
10769 	    bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10770 	  break;
10771 
10772 	case OMP_CLAUSE_IF:
10773 	case OMP_CLAUSE_NUM_THREADS:
10774 	case OMP_CLAUSE_SCHEDULE:
10775 	case OMP_CLAUSE_NOWAIT:
10776 	case OMP_CLAUSE_ORDERED:
10777 	case OMP_CLAUSE_DEFAULT:
10778 	case OMP_CLAUSE_UNTIED:
10779 	case OMP_CLAUSE_COLLAPSE:
10780 	case OMP_CLAUSE_FINAL:
10781 	case OMP_CLAUSE_MERGEABLE:
10782 	  pc = &OMP_CLAUSE_CHAIN (c);
10783 	  continue;
10784 
10785 	default:
10786 	  gcc_unreachable ();
10787 	}
10788 
10789       if (!remove)
10790 	{
10791 	  t = OMP_CLAUSE_DECL (c);
10792 
10793 	  if (need_complete)
10794 	    {
10795 	      t = require_complete_type (t);
10796 	      if (t == error_mark_node)
10797 		remove = true;
10798 	    }
10799 
10800 	  if (need_implicitly_determined)
10801 	    {
10802 	      const char *share_name = NULL;
10803 
10804 	      if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10805 		share_name = "threadprivate";
10806 	      else switch (c_omp_predetermined_sharing (t))
10807 		{
10808 		case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10809 		  break;
10810 		case OMP_CLAUSE_DEFAULT_SHARED:
10811 		  /* const vars may be specified in firstprivate clause.  */
10812 		  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10813 		      && TREE_READONLY (t))
10814 		    break;
10815 		  share_name = "shared";
10816 		  break;
10817 		case OMP_CLAUSE_DEFAULT_PRIVATE:
10818 		  share_name = "private";
10819 		  break;
10820 		default:
10821 		  gcc_unreachable ();
10822 		}
10823 	      if (share_name)
10824 		{
10825 		  error_at (OMP_CLAUSE_LOCATION (c),
10826 			    "%qE is predetermined %qs for %qs",
10827 			    t, share_name, name);
10828 		  remove = true;
10829 		}
10830 	    }
10831 	}
10832 
10833       if (remove)
10834 	*pc = OMP_CLAUSE_CHAIN (c);
10835       else
10836 	pc = &OMP_CLAUSE_CHAIN (c);
10837     }
10838 
10839   bitmap_obstack_release (NULL);
10840   return clauses;
10841 }
10842 
10843 /* Create a transaction node.  */
10844 
10845 tree
c_finish_transaction(location_t loc,tree block,int flags)10846 c_finish_transaction (location_t loc, tree block, int flags)
10847 {
10848   tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
10849   if (flags & TM_STMT_ATTR_OUTER)
10850     TRANSACTION_EXPR_OUTER (stmt) = 1;
10851   if (flags & TM_STMT_ATTR_RELAXED)
10852     TRANSACTION_EXPR_RELAXED (stmt) = 1;
10853   return add_stmt (stmt);
10854 }
10855 
10856 /* Make a variant type in the proper way for C/C++, propagating qualifiers
10857    down to the element type of an array.  */
10858 
10859 tree
c_build_qualified_type(tree type,int type_quals)10860 c_build_qualified_type (tree type, int type_quals)
10861 {
10862   if (type == error_mark_node)
10863     return type;
10864 
10865   if (TREE_CODE (type) == ARRAY_TYPE)
10866     {
10867       tree t;
10868       tree element_type = c_build_qualified_type (TREE_TYPE (type),
10869 						  type_quals);
10870 
10871       /* See if we already have an identically qualified type.  */
10872       for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10873 	{
10874 	  if (TYPE_QUALS (strip_array_types (t)) == type_quals
10875 	      && TYPE_NAME (t) == TYPE_NAME (type)
10876 	      && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10877 	      && attribute_list_equal (TYPE_ATTRIBUTES (t),
10878 				       TYPE_ATTRIBUTES (type)))
10879 	    break;
10880 	}
10881       if (!t)
10882 	{
10883           tree domain = TYPE_DOMAIN (type);
10884 
10885 	  t = build_variant_type_copy (type);
10886 	  TREE_TYPE (t) = element_type;
10887 
10888           if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10889               || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10890             SET_TYPE_STRUCTURAL_EQUALITY (t);
10891           else if (TYPE_CANONICAL (element_type) != element_type
10892                    || (domain && TYPE_CANONICAL (domain) != domain))
10893             {
10894               tree unqualified_canon
10895                 = build_array_type (TYPE_CANONICAL (element_type),
10896                                     domain? TYPE_CANONICAL (domain)
10897                                           : NULL_TREE);
10898               TYPE_CANONICAL (t)
10899                 = c_build_qualified_type (unqualified_canon, type_quals);
10900             }
10901           else
10902             TYPE_CANONICAL (t) = t;
10903 	}
10904       return t;
10905     }
10906 
10907   /* A restrict-qualified pointer type must be a pointer to object or
10908      incomplete type.  Note that the use of POINTER_TYPE_P also allows
10909      REFERENCE_TYPEs, which is appropriate for C++.  */
10910   if ((type_quals & TYPE_QUAL_RESTRICT)
10911       && (!POINTER_TYPE_P (type)
10912 	  || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
10913     {
10914       error ("invalid use of %<restrict%>");
10915       type_quals &= ~TYPE_QUAL_RESTRICT;
10916     }
10917 
10918   return build_qualified_type (type, type_quals);
10919 }
10920 
10921 /* Build a VA_ARG_EXPR for the C parser.  */
10922 
10923 tree
c_build_va_arg(location_t loc,tree expr,tree type)10924 c_build_va_arg (location_t loc, tree expr, tree type)
10925 {
10926   if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
10927     warning_at (loc, OPT_Wc___compat,
10928 		"C++ requires promoted type, not enum type, in %<va_arg%>");
10929   return build_va_arg (loc, expr, type);
10930 }
10931