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