typeck.c revision 50397
1/* Build expressions with type checking for C++ compiler.
2   Copyright (C) 1987, 88, 89, 92-97, 1998 Free Software Foundation, Inc.
3   Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING.  If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA.  */
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 and C++ specific error
26   checks, and some optimization.
27
28   There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29   and to process initializations in declarations (since they work
30   like a strange sort of assignment).  */
31
32#include "config.h"
33#include "system.h"
34#include "tree.h"
35#include "rtl.h"
36#include "cp-tree.h"
37#include "flags.h"
38#include "output.h"
39#include "expr.h"
40#include "toplev.h"
41
42extern void compiler_error ();
43
44static tree convert_for_assignment PROTO((tree, tree, char*, tree,
45					  int));
46static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
47static tree rationalize_conditional_expr PROTO((enum tree_code, tree));
48static int comp_target_parms PROTO((tree, tree, int));
49static int comp_ptr_ttypes_real PROTO((tree, tree, int));
50static int comp_ptr_ttypes_const PROTO((tree, tree));
51static int comp_ptr_ttypes_reinterpret PROTO((tree, tree));
52static int comp_array_types PROTO((int (*) (tree, tree, int), tree,
53				   tree, int));
54static tree build_ptrmemfunc1 PROTO((tree, tree, tree, tree, tree));
55static tree common_base_type PROTO((tree, tree));
56#if 0
57static tree convert_sequence PROTO((tree, tree));
58#endif
59static tree lookup_anon_field PROTO((tree, tree));
60static tree pointer_diff PROTO((tree, tree, tree));
61static tree qualify_type PROTO((tree, tree));
62static tree get_delta_difference PROTO((tree, tree, int));
63
64/* Return the target type of TYPE, which meas return T for:
65   T*, T&, T[], T (...), and otherwise, just T.  */
66
67tree
68target_type (type)
69     tree type;
70{
71  if (TREE_CODE (type) == REFERENCE_TYPE)
72    type = TREE_TYPE (type);
73  while (TREE_CODE (type) == POINTER_TYPE
74	 || TREE_CODE (type) == ARRAY_TYPE
75	 || TREE_CODE (type) == FUNCTION_TYPE
76	 || TREE_CODE (type) == METHOD_TYPE
77	 || TREE_CODE (type) == OFFSET_TYPE)
78    type = TREE_TYPE (type);
79  return type;
80}
81
82/* Do `exp = require_complete_type (exp);' to make sure exp
83   does not have an incomplete type.  (That includes void types.)  */
84
85tree
86require_complete_type (value)
87     tree value;
88{
89  tree type;
90
91  if (processing_template_decl)
92    return value;
93
94  if (TREE_CODE (value) == OVERLOAD)
95    type = unknown_type_node;
96  else
97    type = TREE_TYPE (value);
98
99  /* First, detect a valid value with a complete type.  */
100  if (TYPE_SIZE (type) != 0
101      && type != void_type_node
102      && ! (TYPE_LANG_SPECIFIC (type)
103	    && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
104	    && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
105    return value;
106
107  /* If we see X::Y, we build an OFFSET_TYPE which has
108     not been laid out.  Try to avoid an error by interpreting
109     it as this->X::Y, if reasonable.  */
110  if (TREE_CODE (value) == OFFSET_REF
111      && current_class_ref != 0
112      && TREE_OPERAND (value, 0) == current_class_ref)
113    {
114      tree base, member = TREE_OPERAND (value, 1);
115      tree basetype = TYPE_OFFSET_BASETYPE (type);
116      my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
117      base = convert_pointer_to (basetype, current_class_ptr);
118      value = build (COMPONENT_REF, TREE_TYPE (member),
119		     build_indirect_ref (base, NULL_PTR), member);
120      return require_complete_type (value);
121    }
122
123  if (TYPE_SIZE (complete_type (type)))
124    return value;
125  else
126    {
127      incomplete_type_error (value, type);
128      return error_mark_node;
129    }
130}
131
132/* Try to complete TYPE, if it is incomplete.  For example, if TYPE is
133   a template instantiation, do the instantiation.  Returns TYPE,
134   whether or not it could be completed, unless something goes
135   horribly wrong, in which case the error_mark_node is returned.  */
136
137tree
138complete_type (type)
139     tree type;
140{
141  if (type == NULL_TREE)
142    /* Rather than crash, we return something sure to cause an error
143       at some point.  */
144    return error_mark_node;
145
146  if (type == error_mark_node || TYPE_SIZE (type) != NULL_TREE)
147    ;
148  else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
149    {
150      tree t = complete_type (TREE_TYPE (type));
151      if (TYPE_SIZE (t) != NULL_TREE && ! processing_template_decl)
152	layout_type (type);
153      TYPE_NEEDS_CONSTRUCTING (type)
154	= TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
155      TYPE_NEEDS_DESTRUCTOR (type)
156	= TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
157    }
158  else if (IS_AGGR_TYPE (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
159    instantiate_class_template (TYPE_MAIN_VARIANT (type));
160
161  return type;
162}
163
164/* Like complete_type, but issue an error if the TYPE cannot be
165   completed.  Returns NULL_TREE if the type cannot be made
166   complete.  */
167
168tree
169complete_type_or_else (type)
170     tree type;
171{
172  type = complete_type (type);
173  if (type != error_mark_node && !TYPE_SIZE (type))
174    {
175      incomplete_type_error (NULL_TREE, type);
176      return NULL_TREE;
177    }
178  else
179    return type;
180}
181
182/* Return truthvalue of whether type of EXP is instantiated.  */
183
184int
185type_unknown_p (exp)
186     tree exp;
187{
188  return (TREE_CODE (exp) == OVERLOAD
189          || TREE_CODE (exp) == TREE_LIST
190	  || TREE_TYPE (exp) == unknown_type_node
191	  || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
192	      && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
193}
194
195/* Return truthvalue of whether T is function (or pfn) type.  */
196
197int
198fntype_p (t)
199     tree t;
200{
201  return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
202	  || (TREE_CODE (t) == POINTER_TYPE
203	      && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
204		  || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
205}
206
207/* Do `exp = require_instantiated_type (type, exp);' to make sure EXP
208   does not have an uninstantiated type.
209   TYPE is type to instantiate with, if uninstantiated.  */
210
211tree
212require_instantiated_type (type, exp, errval)
213     tree type, exp, errval;
214{
215  if (TREE_TYPE (exp) == NULL_TREE)
216    {
217      error ("argument list may not have an initializer list");
218      return errval;
219    }
220
221  if (TREE_CODE (exp) == OVERLOAD
222      || TREE_TYPE (exp) == unknown_type_node
223      || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
224	  && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node))
225    {
226      exp = instantiate_type (type, exp, 1);
227      if (TREE_TYPE (exp) == error_mark_node)
228	return errval;
229    }
230  return exp;
231}
232
233/* Return a variant of TYPE which has all the type qualifiers of LIKE
234   as well as those of TYPE.  */
235
236static tree
237qualify_type (type, like)
238     tree type, like;
239{
240  int constflag = TYPE_READONLY (type) || TYPE_READONLY (like);
241  int volflag = TYPE_VOLATILE (type) || TYPE_VOLATILE (like);
242  /* @@ Must do member pointers here.  */
243  return cp_build_type_variant (type, constflag, volflag);
244}
245
246/* Return the common type of two parameter lists.
247   We assume that comptypes has already been done and returned 1;
248   if that isn't so, this may crash.
249
250   As an optimization, free the space we allocate if the parameter
251   lists are already common.  */
252
253tree
254commonparms (p1, p2)
255     tree p1, p2;
256{
257  tree oldargs = p1, newargs, n;
258  int i, len;
259  int any_change = 0;
260  char *first_obj = (char *) oballoc (0);
261
262  len = list_length (p1);
263  newargs = tree_last (p1);
264
265  if (newargs == void_list_node)
266    i = 1;
267  else
268    {
269      i = 0;
270      newargs = 0;
271    }
272
273  for (; i < len; i++)
274    newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
275
276  n = newargs;
277
278  for (i = 0; p1;
279       p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
280    {
281      if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
282	{
283	  TREE_PURPOSE (n) = TREE_PURPOSE (p1);
284	  any_change = 1;
285	}
286      else if (! TREE_PURPOSE (p1))
287	{
288	  if (TREE_PURPOSE (p2))
289	    {
290	      TREE_PURPOSE (n) = TREE_PURPOSE (p2);
291	      any_change = 1;
292	    }
293	}
294      else
295	{
296	  if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
297	    any_change = 1;
298	  TREE_PURPOSE (n) = TREE_PURPOSE (p2);
299	}
300      if (TREE_VALUE (p1) != TREE_VALUE (p2))
301	{
302	  any_change = 1;
303	  TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
304	}
305      else
306	TREE_VALUE (n) = TREE_VALUE (p1);
307    }
308  if (! any_change)
309    {
310      obfree (first_obj);
311      return oldargs;
312    }
313
314  return newargs;
315}
316
317/* Given a type, perhaps copied for a typedef,
318   find the "original" version of it.  */
319tree
320original_type (t)
321     tree t;
322{
323  while (TYPE_NAME (t) != NULL_TREE)
324    {
325      tree x = TYPE_NAME (t);
326      if (TREE_CODE (x) != TYPE_DECL)
327	break;
328      x = DECL_ORIGINAL_TYPE (x);
329      if (x == NULL_TREE)
330	break;
331      t = x;
332    }
333  return t;
334}
335
336/* Return the common type of two types.
337   We assume that comptypes has already been done and returned 1;
338   if that isn't so, this may crash.
339
340   This is the type for the result of most arithmetic operations
341   if the operands have the given two types.
342
343   We do not deal with enumeral types here because they have already been
344   converted to integer types.  */
345
346tree
347common_type (t1, t2)
348     tree t1, t2;
349{
350  register enum tree_code code1;
351  register enum tree_code code2;
352  tree attributes;
353
354  /* Save time if the two types are the same.  */
355  if (t1 == t2)
356    return t1;
357  t1 = original_type (t1);
358  t2 = original_type (t2);
359  if (t1 == t2)
360    return t1;
361
362  /* If one type is nonsense, use the other.  */
363  if (t1 == error_mark_node)
364    return t2;
365  if (t2 == error_mark_node)
366    return t1;
367
368  /* Merge the attributes.  */
369  attributes = merge_machine_type_attributes (t1, t2);
370
371  { register tree a1, a2;
372    a1 = TYPE_ATTRIBUTES (t1);
373    a2 = TYPE_ATTRIBUTES (t2);
374
375    /* Either one unset?  Take the set one.  */
376
377    if (!(attributes = a1))
378       attributes = a2;
379
380    /* One that completely contains the other?  Take it.  */
381
382    else if (a2 && !attribute_list_contained (a1, a2))
383      {
384	if (attribute_list_contained (a2, a1))
385	  attributes = a2;
386	else
387	  {
388	    /* Pick the longest list, and hang on the other list.  */
389	    /* ??? For the moment we punt on the issue of attrs with args.  */
390
391	    if (list_length (a1) < list_length (a2))
392	      attributes = a2, a2 = a1;
393
394	    for (; a2; a2 = TREE_CHAIN (a2))
395	      if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
396				    attributes) == NULL_TREE)
397		{
398		  a1 = copy_node (a2);
399		  TREE_CHAIN (a1) = attributes;
400		  attributes = a1;
401		}
402	  }
403      }
404  }
405
406  /* Treat an enum type as the unsigned integer type of the same width.  */
407
408  if (TREE_CODE (t1) == ENUMERAL_TYPE)
409    t1 = type_for_size (TYPE_PRECISION (t1), 1);
410  if (TREE_CODE (t2) == ENUMERAL_TYPE)
411    t2 = type_for_size (TYPE_PRECISION (t2), 1);
412
413  if (TYPE_PTRMEMFUNC_P (t1))
414    t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
415  if (TYPE_PTRMEMFUNC_P (t2))
416    t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
417
418  code1 = TREE_CODE (t1);
419  code2 = TREE_CODE (t2);
420
421  /* If one type is complex, form the common type of the non-complex
422     components, then make that complex.  Use T1 or T2 if it is the
423     required type.  */
424  if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
425    {
426      tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
427      tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
428      tree subtype = common_type (subtype1, subtype2);
429
430      if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
431	return build_type_attribute_variant (t1, attributes);
432      else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
433	return build_type_attribute_variant (t2, attributes);
434      else
435	return build_type_attribute_variant (build_complex_type (subtype),
436					     attributes);
437    }
438
439  switch (code1)
440    {
441    case INTEGER_TYPE:
442    case REAL_TYPE:
443      /* If only one is real, use it as the result.  */
444
445      if (code1 == REAL_TYPE && code2 != REAL_TYPE)
446	return build_type_attribute_variant (t1, attributes);
447
448      if (code2 == REAL_TYPE && code1 != REAL_TYPE)
449        return build_type_attribute_variant (t2, attributes);
450
451      /* Both real or both integers; use the one with greater precision.  */
452
453      if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
454	return build_type_attribute_variant (t1, attributes);
455      else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
456        return build_type_attribute_variant (t2, attributes);
457
458      /* Same precision.  Prefer longs to ints even when same size.  */
459
460      if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
461	  || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
462        return build_type_attribute_variant (long_unsigned_type_node,
463					     attributes);
464
465      if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
466	  || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
467	{
468	  /* But preserve unsignedness from the other type,
469	     since long cannot hold all the values of an unsigned int.  */
470	  if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
471	     t1 = long_unsigned_type_node;
472	  else
473	     t1 = long_integer_type_node;
474	  return build_type_attribute_variant (t1, attributes);
475	}
476
477      if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
478	  || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
479	return build_type_attribute_variant (long_double_type_node,
480					     attributes);
481
482      /* Otherwise prefer the unsigned one.  */
483
484      if (TREE_UNSIGNED (t1))
485	return build_type_attribute_variant (t1, attributes);
486      else
487	return build_type_attribute_variant (t2, attributes);
488
489    case POINTER_TYPE:
490    case REFERENCE_TYPE:
491      /* For two pointers, do this recursively on the target type,
492	 and combine the qualifiers of the two types' targets.  */
493      /* This code was turned off; I don't know why.
494 	 But ANSI C++ specifies doing this with the qualifiers.
495 	 So I turned it on again.  */
496      {
497	tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (t1));
498	tree tt2 = TYPE_MAIN_VARIANT (TREE_TYPE (t2));
499	int constp
500	  = TYPE_READONLY (TREE_TYPE (t1)) || TYPE_READONLY (TREE_TYPE (t2));
501	int volatilep
502	  = TYPE_VOLATILE (TREE_TYPE (t1)) || TYPE_VOLATILE (TREE_TYPE (t2));
503	tree target;
504
505	if (tt1 == tt2)
506	  target = tt1;
507	else if (tt1 == void_type_node || tt2 == void_type_node)
508	  target = void_type_node;
509	else if (tt1 == unknown_type_node)
510	  target = tt2;
511	else if (tt2 == unknown_type_node)
512	  target = tt1;
513	else
514	  target = common_type (tt1, tt2);
515
516	target = cp_build_type_variant (target, constp, volatilep);
517	if (code1 == POINTER_TYPE)
518	  t1 = build_pointer_type (target);
519	else
520	  t1 = build_reference_type (target);
521	t1 = build_type_attribute_variant (t1, attributes);
522
523	if (TREE_CODE (target) == METHOD_TYPE)
524	  t1 = build_ptrmemfunc_type (t1);
525
526	return t1;
527      }
528
529    case ARRAY_TYPE:
530      {
531	tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
532	/* Save space: see if the result is identical to one of the args.  */
533	if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
534	  return build_type_attribute_variant (t1, attributes);
535	if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
536	  return build_type_attribute_variant (t2, attributes);
537	/* Merge the element types, and have a size if either arg has one.  */
538	t1 = build_cplus_array_type
539	  (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
540	return build_type_attribute_variant (t1, attributes);
541      }
542
543    case FUNCTION_TYPE:
544      /* Function types: prefer the one that specified arg types.
545	 If both do, merge the arg types.  Also merge the return types.  */
546      {
547	tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
548	tree p1 = TYPE_ARG_TYPES (t1);
549	tree p2 = TYPE_ARG_TYPES (t2);
550	tree rval, raises;
551
552	/* Save space: see if the result is identical to one of the args.  */
553	if (valtype == TREE_TYPE (t1) && ! p2)
554	  return build_type_attribute_variant (t1, attributes);
555	if (valtype == TREE_TYPE (t2) && ! p1)
556	  return build_type_attribute_variant (t2, attributes);
557
558	/* Simple way if one arg fails to specify argument types.  */
559	if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
560	  {
561	    rval = build_function_type (valtype, p2);
562	    if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
563	      rval = build_exception_variant (rval, raises);
564	    return build_type_attribute_variant (rval, attributes);
565	  }
566	raises = TYPE_RAISES_EXCEPTIONS (t1);
567	if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
568	  {
569	    rval = build_function_type (valtype, p1);
570	    if (raises)
571	      rval = build_exception_variant (rval, raises);
572	    return build_type_attribute_variant (rval, attributes);
573	  }
574
575	rval = build_function_type (valtype, commonparms (p1, p2));
576	rval = build_exception_variant (rval, raises);
577	return build_type_attribute_variant (rval, attributes);
578      }
579
580    case RECORD_TYPE:
581    case UNION_TYPE:
582      t1 = TYPE_MAIN_VARIANT (t1);
583      t2 = TYPE_MAIN_VARIANT (t2);
584
585      if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
586	return build_type_attribute_variant (t1, attributes);
587      else if (binfo_or_else (t2, t1))
588	return build_type_attribute_variant (t2, attributes);
589      else
590	compiler_error ("common_type called with uncommon aggregate types");
591
592    case METHOD_TYPE:
593      if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
594	{
595	  /* Get this value the long way, since TYPE_METHOD_BASETYPE
596	     is just the main variant of this.  */
597	  tree basetype;
598	  tree raises, t3;
599
600	  tree b1 = TYPE_OFFSET_BASETYPE (t1);
601	  tree b2 = TYPE_OFFSET_BASETYPE (t2);
602
603	  if (comptypes (b1, b2, 1)
604	      || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
605	    basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
606	  else
607	    {
608	      if (binfo_or_else (b2, b1) == NULL_TREE)
609		compiler_error ("common_type called with uncommon method types");
610	      basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
611	    }
612
613	  raises = TYPE_RAISES_EXCEPTIONS (t1);
614
615	  /* If this was a member function type, get back to the
616	     original type of type member function (i.e., without
617	     the class instance variable up front.  */
618	  t1 = build_function_type (TREE_TYPE (t1),
619				    TREE_CHAIN (TYPE_ARG_TYPES (t1)));
620	  t2 = build_function_type (TREE_TYPE (t2),
621				    TREE_CHAIN (TYPE_ARG_TYPES (t2)));
622	  t3 = common_type (t1, t2);
623	  t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
624					TYPE_ARG_TYPES (t3));
625	  t1 = build_exception_variant (t3, raises);
626	}
627      else
628        compiler_error ("common_type called with uncommon method types");
629
630      return build_type_attribute_variant (t1, attributes);
631
632    case OFFSET_TYPE:
633      if (TREE_TYPE (t1) == TREE_TYPE (t2))
634	{
635	  tree b1 = TYPE_OFFSET_BASETYPE (t1);
636	  tree b2 = TYPE_OFFSET_BASETYPE (t2);
637
638	  if (comptypes (b1, b2, 1)
639	      || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
640	    return build_type_attribute_variant (t2, attributes);
641	  else if (binfo_or_else (b2, b1))
642	    return build_type_attribute_variant (t1, attributes);
643	}
644      compiler_error ("common_type called with uncommon member types");
645
646    default:
647      return build_type_attribute_variant (t1, attributes);
648    }
649}
650
651/* Return 1 if TYPE1 and TYPE2 raise the same exceptions.  */
652
653int
654compexcepttypes (t1, t2)
655     tree t1, t2;
656{
657  return TYPE_RAISES_EXCEPTIONS (t1) == TYPE_RAISES_EXCEPTIONS (t2);
658}
659
660static int
661comp_array_types (cmp, t1, t2, strict)
662     register int (*cmp) PROTO((tree, tree, int));
663     tree t1, t2;
664     int strict;
665{
666  tree d1 = TYPE_DOMAIN (t1);
667  tree d2 = TYPE_DOMAIN (t2);
668
669  /* Target types must match incl. qualifiers.  */
670  if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
671	|| (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2), strict)))
672    return 0;
673
674  /* Sizes must match unless one is missing or variable.  */
675  if (d1 == 0 || d2 == 0 || d1 == d2
676      || TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
677      || TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
678      || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST
679      || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST)
680    return 1;
681
682  return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (d1))
683	   == TREE_INT_CST_LOW (TYPE_MIN_VALUE (d2)))
684	  && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d1))
685	      == TREE_INT_CST_HIGH (TYPE_MIN_VALUE (d2)))
686	  && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (d1))
687	      == TREE_INT_CST_LOW (TYPE_MAX_VALUE (d2)))
688	  && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d1))
689	      == TREE_INT_CST_HIGH (TYPE_MAX_VALUE (d2))));
690}
691
692/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
693   or various other operations.  This is what ANSI C++ speaks of as
694   "being the same".
695
696   For C++: argument STRICT says we should be strict about this
697   comparison:
698
699	2 : strict, except that if one type is a reference and
700	    the other is not, compare the target type of the
701	    reference to the type that's not a reference (ARM, p308).
702	    This is used for checking for invalid overloading.
703	1 : strict (compared according to ANSI C)
704	    This is used for checking whether two function decls match.
705	0 : <= (compared according to C++)
706	-1: <= or >= (relaxed)
707
708   Otherwise, pointers involving base classes and derived classes can
709   be mixed as valid: i.e. a pointer to a derived class may be converted
710   to a pointer to one of its base classes, as per C++. A pointer to
711   a derived class may be passed as a parameter to a function expecting a
712   pointer to a base classes. These allowances do not commute. In this
713   case, TYPE1 is assumed to be the base class, and TYPE2 is assumed to
714   be the derived class.  */
715
716int
717comptypes (type1, type2, strict)
718     tree type1, type2;
719     int strict;
720{
721  register tree t1 = type1;
722  register tree t2 = type2;
723  int attrval, val;
724
725  /* Suppress errors caused by previously reported errors */
726
727  if (t1 == t2)
728    return 1;
729
730  /* This should never happen.  */
731  my_friendly_assert (t1 != error_mark_node, 307);
732
733  if (t2 == error_mark_node)
734    return 0;
735
736  if (strict < 0)
737    {
738      /* Treat an enum type as the unsigned integer type of the same width.  */
739
740      if (TREE_CODE (t1) == ENUMERAL_TYPE)
741	t1 = type_for_size (TYPE_PRECISION (t1), 1);
742      if (TREE_CODE (t2) == ENUMERAL_TYPE)
743	t2 = type_for_size (TYPE_PRECISION (t2), 1);
744
745      if (t1 == t2)
746	return 1;
747    }
748
749  if (TYPE_PTRMEMFUNC_P (t1))
750    t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
751  if (TYPE_PTRMEMFUNC_P (t2))
752    t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
753
754  /* Different classes of types can't be compatible.  */
755
756  if (TREE_CODE (t1) != TREE_CODE (t2))
757    {
758      if (strict == 2
759	  && ((TREE_CODE (t1) == REFERENCE_TYPE)
760	      ^ (TREE_CODE (t2) == REFERENCE_TYPE)))
761	{
762	  if (TREE_CODE (t1) == REFERENCE_TYPE)
763	    return comptypes (TREE_TYPE (t1), t2, 1);
764	  return comptypes (t1, TREE_TYPE (t2), 1);
765	}
766
767      return 0;
768    }
769  if (strict > 1)
770    strict = 1;
771
772  /* Qualifiers must match.  */
773
774  if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
775    return 0;
776  if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
777    return 0;
778  if (strict > 0 && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
779    return 0;
780
781  /* Allow for two different type nodes which have essentially the same
782     definition.  Note that we already checked for equality of the type
783     qualifiers (just above).  */
784
785  if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
786    return 1;
787
788  /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
789     attribute is its own main variant (`val' will remain 0).  */
790#ifndef COMP_TYPE_ATTRIBUTES
791#define COMP_TYPE_ATTRIBUTES(t1,t2)	1
792#endif
793
794  /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
795  if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
796     return 0;
797
798  /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
799  val = 0;
800
801  switch (TREE_CODE (t1))
802    {
803    case TEMPLATE_TEMPLATE_PARM:
804      if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
805	  || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
806	return 0;
807      if (! comp_template_parms (DECL_TEMPLATE_PARMS (TYPE_NAME (t1)),
808				 DECL_TEMPLATE_PARMS (TYPE_NAME (t2))))
809	return 0;
810      if (! CLASSTYPE_TEMPLATE_INFO (t1) && ! CLASSTYPE_TEMPLATE_INFO (t2))
811	return 1;
812      /* Don't check inheritance.  */
813      strict = 1;
814      /* fall through */
815
816    case RECORD_TYPE:
817    case UNION_TYPE:
818      if (CLASSTYPE_TEMPLATE_INFO (t1) && CLASSTYPE_TEMPLATE_INFO (t2)
819	  && (CLASSTYPE_TI_TEMPLATE (t1) == CLASSTYPE_TI_TEMPLATE (t2)
820	      || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM))
821	return comp_template_args (CLASSTYPE_TI_ARGS (t1),
822				   CLASSTYPE_TI_ARGS (t2));
823      if (strict <= 0)
824	goto look_hard;
825      return 0;
826
827    case OFFSET_TYPE:
828      val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
829			build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
830	     && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
831      break;
832
833    case METHOD_TYPE:
834      if (! compexcepttypes (t1, t2))
835	return 0;
836
837      /* This case is anti-symmetrical!
838	 One can pass a base member (or member function)
839	 to something expecting a derived member (or member function),
840	 but not vice-versa!  */
841
842      val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
843	     && compparms (TYPE_ARG_TYPES (t1),
844			   TYPE_ARG_TYPES (t2), strict));
845      break;
846
847    case POINTER_TYPE:
848    case REFERENCE_TYPE:
849      t1 = TREE_TYPE (t1);
850      t2 = TREE_TYPE (t2);
851      if (t1 == t2)
852	{
853	  val = 1;
854	  break;
855	}
856      if (strict <= 0)
857	{
858	  if (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE)
859	    {
860	      int rval;
861	    look_hard:
862	      rval = t1 == t2 || DERIVED_FROM_P (t1, t2);
863
864	      if (rval)
865		{
866		  val = 1;
867		  break;
868		}
869	      if (strict < 0)
870		{
871		  val = DERIVED_FROM_P (t2, t1);
872		  break;
873		}
874	    }
875	  return 0;
876	}
877      else
878	val = comptypes (t1, t2, strict);
879      break;
880
881    case FUNCTION_TYPE:
882      if (! compexcepttypes (t1, t2))
883	return 0;
884
885      val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
886	      || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
887	     && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2), strict));
888      break;
889
890    case ARRAY_TYPE:
891      /* Target types must match incl. qualifiers.  */
892      val = comp_array_types (comptypes, t1, t2, strict);
893      break;
894
895    case TEMPLATE_TYPE_PARM:
896      return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
897	&& TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
898
899    case TYPENAME_TYPE:
900      if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
901	return 0;
902      return comptypes (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2), 1);
903
904    default:
905      break;
906    }
907  return attrval == 2 && val == 1 ? 2 : val;
908}
909
910/* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
911   ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
912   converted to TTL. Return -1 means that TTL can be converted to TTR but
913   not vice versa.
914
915   NPTRS is the number of pointers we can strip off and keep cool.
916   This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
917   but to not permit B** to convert to A**.
918
919   This should go away.  Callers should use can_convert or something
920   similar instead.  (jason 17 Apr 1997)  */
921
922int
923comp_target_types (ttl, ttr, nptrs)
924     tree ttl, ttr;
925     int nptrs;
926{
927  ttl = TYPE_MAIN_VARIANT (ttl);
928  ttr = TYPE_MAIN_VARIANT (ttr);
929  if (ttl == ttr)
930    return 1;
931
932  if (TREE_CODE (ttr) != TREE_CODE (ttl))
933    return 0;
934
935  if (TREE_CODE (ttr) == POINTER_TYPE
936      || (TREE_CODE (ttr) == REFERENCE_TYPE))
937    {
938      int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
939
940      ttl = TREE_TYPE (ttl);
941      ttr = TREE_TYPE (ttr);
942
943      if (nptrs > 0 && is_ptr)
944	{
945	  if (TREE_CODE (ttl) == UNKNOWN_TYPE
946	      || TREE_CODE (ttr) == UNKNOWN_TYPE)
947	    return 1;
948	  else if (TREE_CODE (ttl) == VOID_TYPE
949		   && TREE_CODE (ttr) != FUNCTION_TYPE
950		   && TREE_CODE (ttr) != METHOD_TYPE
951		   && TREE_CODE (ttr) != OFFSET_TYPE)
952	    return 1;
953	  else if (TREE_CODE (ttr) == VOID_TYPE
954		   && TREE_CODE (ttl) != FUNCTION_TYPE
955		   && TREE_CODE (ttl) != METHOD_TYPE
956		   && TREE_CODE (ttl) != OFFSET_TYPE)
957	    return -1;
958	  else if (TREE_CODE (ttl) == POINTER_TYPE
959		   || TREE_CODE (ttl) == ARRAY_TYPE)
960	    {
961	      if (comp_ptr_ttypes (ttl, ttr))
962		return 1;
963	      else if (comp_ptr_ttypes (ttr, ttl))
964		return -1;
965	      return 0;
966	    }
967	}
968
969      /* Const and volatile mean something different for function types,
970	 so the usual checks are not appropriate.  */
971      if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
972	return comp_target_types (ttl, ttr, nptrs - 1);
973
974      /* Make sure that the cv-quals change only in the same direction as
975	 the target type.  */
976      {
977	int t;
978	int c = TYPE_READONLY (ttl) - TYPE_READONLY (ttr);
979	int v = TYPE_VOLATILE (ttl) - TYPE_VOLATILE (ttr);
980
981	if ((c > 0 && v < 0) || (c < 0 && v > 0))
982	  return 0;
983
984	if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
985	  return (c + v < 0) ? -1 : 1;
986
987	t = comp_target_types (ttl, ttr, nptrs - 1);
988	if ((t == 1 && c + v >= 0) || (t == -1 && c + v <= 0))
989	  return t;
990
991	return 0;
992      }
993    }
994
995  if (TREE_CODE (ttr) == ARRAY_TYPE)
996    return comp_array_types (comp_target_types, ttl, ttr, 0);
997  else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
998    {
999      tree argsl, argsr;
1000      int saw_contra = 0;
1001
1002      if (pedantic)
1003	{
1004	  if (comptypes (TREE_TYPE (ttl), TREE_TYPE (ttr), 1) == 0)
1005	    return 0;
1006	}
1007      else
1008	{
1009	  switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1010	    {
1011	    case 0:
1012	      return 0;
1013	    case -1:
1014	      saw_contra = 1;
1015	    }
1016	}
1017
1018      argsl = TYPE_ARG_TYPES (ttl);
1019      argsr = TYPE_ARG_TYPES (ttr);
1020
1021      /* Compare 'this' here, not in comp_target_parms.  */
1022      if (TREE_CODE (ttr) == METHOD_TYPE)
1023	{
1024	  tree tl = TYPE_METHOD_BASETYPE (ttl);
1025	  tree tr = TYPE_METHOD_BASETYPE (ttr);
1026
1027	  if (comptypes (tr, tl, 0) == 0)
1028	    {
1029	      if (comptypes (tl, tr, 0))
1030		saw_contra = 1;
1031	      else
1032		return 0;
1033	    }
1034
1035	  argsl = TREE_CHAIN (argsl);
1036	  argsr = TREE_CHAIN (argsr);
1037	}
1038
1039	switch (comp_target_parms (argsl, argsr, 1))
1040	  {
1041	  case 0:
1042	    return 0;
1043	  case -1:
1044	    saw_contra = 1;
1045	  }
1046
1047	return saw_contra ? -1 : 1;
1048    }
1049  /* for C++ */
1050  else if (TREE_CODE (ttr) == OFFSET_TYPE)
1051    {
1052      /* Contravariance: we can assign a pointer to base member to a pointer
1053	 to derived member.  Note difference from simple pointer case, where
1054	 we can pass a pointer to derived to a pointer to base.  */
1055      if (comptypes (TYPE_OFFSET_BASETYPE (ttr),
1056		     TYPE_OFFSET_BASETYPE (ttl), 0))
1057	return comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs);
1058      else if (comptypes (TYPE_OFFSET_BASETYPE (ttl),
1059			  TYPE_OFFSET_BASETYPE (ttr), 0)
1060	       && comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), nptrs))
1061	return -1;
1062    }
1063  else if (IS_AGGR_TYPE (ttl))
1064    {
1065      if (nptrs < 0)
1066	return 0;
1067      if (comptypes (build_pointer_type (ttl), build_pointer_type (ttr), 0))
1068	return 1;
1069      if (comptypes (build_pointer_type (ttr), build_pointer_type (ttl), 0))
1070	return -1;
1071      return 0;
1072    }
1073
1074  return 0;
1075}
1076
1077/* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1078   more cv-qualified that TYPE1, and 0 otherwise.  */
1079
1080int
1081comp_cv_qualification (type1, type2)
1082     tree type1;
1083     tree type2;
1084{
1085  if (TYPE_READONLY (type1) == TYPE_READONLY (type2)
1086      && TYPE_VOLATILE (type1) == TYPE_VOLATILE (type2))
1087    return 0;
1088
1089  if (TYPE_READONLY (type1) >= TYPE_READONLY (type2)
1090      && TYPE_VOLATILE (type1) >= TYPE_VOLATILE (type2))
1091    return 1;
1092
1093  if (TYPE_READONLY (type2) >= TYPE_READONLY (type1)
1094      && TYPE_VOLATILE (type2) >= TYPE_VOLATILE (type1))
1095    return -1;
1096
1097  return 0;
1098}
1099
1100/* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1101   subset of the cv-qualification signature of TYPE2, and the types
1102   are similar.  Returns -1 if the other way 'round, and 0 otherwise.  */
1103
1104int
1105comp_cv_qual_signature (type1, type2)
1106     tree type1;
1107     tree type2;
1108{
1109  if (comp_ptr_ttypes_real (type2, type1, -1))
1110    return 1;
1111  else if (comp_ptr_ttypes_real (type1, type2, -1))
1112    return -1;
1113  else
1114    return 0;
1115}
1116
1117/* If two types share a common base type, return that basetype.
1118   If there is not a unique most-derived base type, this function
1119   returns ERROR_MARK_NODE.  */
1120
1121static tree
1122common_base_type (tt1, tt2)
1123     tree tt1, tt2;
1124{
1125  tree best = NULL_TREE;
1126  int i;
1127
1128  /* If one is a baseclass of another, that's good enough.  */
1129  if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1130    return tt1;
1131  if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1132    return tt2;
1133
1134  /* Otherwise, try to find a unique baseclass of TT1
1135     that is shared by TT2, and follow that down.  */
1136  for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1137    {
1138      tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1139      tree trial = common_base_type (basetype, tt2);
1140      if (trial)
1141	{
1142	  if (trial == error_mark_node)
1143	    return trial;
1144	  if (best == NULL_TREE)
1145	    best = trial;
1146	  else if (best != trial)
1147	    return error_mark_node;
1148	}
1149    }
1150
1151  /* Same for TT2.  */
1152  for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1153    {
1154      tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1155      tree trial = common_base_type (tt1, basetype);
1156      if (trial)
1157	{
1158	  if (trial == error_mark_node)
1159	    return trial;
1160	  if (best == NULL_TREE)
1161	    best = trial;
1162	  else if (best != trial)
1163	    return error_mark_node;
1164	}
1165    }
1166  return best;
1167}
1168
1169/* Subroutines of `comptypes'.  */
1170
1171/* Return 1 if two parameter type lists PARMS1 and PARMS2
1172   are equivalent in the sense that functions with those parameter types
1173   can have equivalent types.
1174   If either list is empty, we win.
1175   Otherwise, the two lists must be equivalent, element by element.
1176
1177   C++: See comment above about TYPE1, TYPE2.
1178
1179   STRICT is no longer used.  */
1180
1181int
1182compparms (parms1, parms2, strict)
1183     tree parms1, parms2;
1184     int strict;
1185{
1186  register tree t1 = parms1, t2 = parms2;
1187
1188  /* An unspecified parmlist matches any specified parmlist
1189     whose argument types don't need default promotions.  */
1190
1191  while (1)
1192    {
1193      if (t1 == 0 && t2 == 0)
1194	return 1;
1195      /* If one parmlist is shorter than the other,
1196	 they fail to match.  */
1197      if (t1 == 0 || t2 == 0)
1198	return 0;
1199      if (! comptypes (TREE_VALUE (t2), TREE_VALUE (t1), 1))
1200	return 0;
1201
1202      t1 = TREE_CHAIN (t1);
1203      t2 = TREE_CHAIN (t2);
1204    }
1205}
1206
1207/* This really wants return whether or not parameter type lists
1208   would make their owning functions assignment compatible or not.
1209
1210   The return value is like for comp_target_types.
1211
1212   This should go away, possibly with the exception of the empty parmlist
1213   conversion; there are no conversions between function types in C++.
1214   (jason 17 Apr 1997)  */
1215
1216static int
1217comp_target_parms (parms1, parms2, strict)
1218     tree parms1, parms2;
1219     int strict;
1220{
1221  register tree t1 = parms1, t2 = parms2;
1222  int warn_contravariance = 0;
1223
1224  /* In C, an unspecified parmlist matches any specified parmlist
1225     whose argument types don't need default promotions.  This is not
1226     true for C++, but let's do it anyway for unfixed headers.  */
1227
1228  if (t1 == 0 && t2 != 0)
1229    {
1230      cp_pedwarn ("ANSI C++ prohibits conversion from `(%#T)' to `(...)'",
1231		  parms2);
1232      return self_promoting_args_p (t2);
1233    }
1234  if (t2 == 0)
1235    return self_promoting_args_p (t1);
1236
1237  for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1238    {
1239      tree p1, p2;
1240
1241      /* If one parmlist is shorter than the other,
1242	 they fail to match, unless STRICT is <= 0.  */
1243      if (t1 == 0 || t2 == 0)
1244	{
1245	  if (strict > 0)
1246	    return 0;
1247	  if (strict < 0)
1248	    return 1 + warn_contravariance;
1249	  return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1250	}
1251      p1 = TREE_VALUE (t1);
1252      p2 = TREE_VALUE (t2);
1253      if (comptypes (p1, p2, 1))
1254	continue;
1255
1256      if (pedantic)
1257	return 0;
1258
1259      if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1260	  || (TREE_CODE (p1) == REFERENCE_TYPE
1261	      && TREE_CODE (p2) == REFERENCE_TYPE))
1262	{
1263	  if (strict <= 0
1264	      && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1265		  == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1266	    continue;
1267
1268	  /* The following is wrong for contravariance,
1269	     but many programs depend on it.  */
1270	  if (TREE_TYPE (p1) == void_type_node)
1271	    continue;
1272	  if (TREE_TYPE (p2) == void_type_node)
1273	    {
1274	      warn_contravariance = 1;
1275	      continue;
1276	    }
1277	  if (IS_AGGR_TYPE (TREE_TYPE (p1)))
1278	    {
1279	      if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (p1)),
1280			     TYPE_MAIN_VARIANT (TREE_TYPE (p2)), 1) == 0)
1281		return 0;
1282	    }
1283	}
1284      /* Note backwards order due to contravariance.  */
1285      if (comp_target_types (p2, p1, 1) <= 0)
1286	{
1287	  if (comp_target_types (p1, p2, 1) > 0)
1288	    {
1289	      warn_contravariance = 1;
1290	      continue;
1291	    }
1292	  if (strict != 0)
1293	    return 0;
1294	}
1295    }
1296  return warn_contravariance ? -1 : 1;
1297}
1298
1299/* Return 1 if PARMS specifies a fixed number of parameters
1300   and none of their types is affected by default promotions.  */
1301
1302int
1303self_promoting_args_p (parms)
1304     tree parms;
1305{
1306  register tree t;
1307  for (t = parms; t; t = TREE_CHAIN (t))
1308    {
1309      register tree type = TREE_VALUE (t);
1310
1311      if (TREE_CHAIN (t) == 0 && type != void_type_node)
1312	return 0;
1313
1314      if (type == 0)
1315	return 0;
1316
1317      if (TYPE_MAIN_VARIANT (type) == float_type_node)
1318	return 0;
1319
1320      if (C_PROMOTING_INTEGER_TYPE_P (type))
1321	return 0;
1322    }
1323  return 1;
1324}
1325
1326/* Return an unsigned type the same as TYPE in other respects.
1327
1328   C++: must make these work for type variants as well.  */
1329
1330tree
1331unsigned_type (type)
1332     tree type;
1333{
1334  tree type1 = TYPE_MAIN_VARIANT (type);
1335  if (type1 == signed_char_type_node || type1 == char_type_node)
1336    return unsigned_char_type_node;
1337  if (type1 == integer_type_node)
1338    return unsigned_type_node;
1339  if (type1 == short_integer_type_node)
1340    return short_unsigned_type_node;
1341  if (type1 == long_integer_type_node)
1342    return long_unsigned_type_node;
1343  if (type1 == long_long_integer_type_node)
1344    return long_long_unsigned_type_node;
1345  if (type1 == intTI_type_node)
1346    return unsigned_intTI_type_node;
1347  if (type1 == intDI_type_node)
1348    return unsigned_intDI_type_node;
1349  if (type1 == intSI_type_node)
1350    return unsigned_intSI_type_node;
1351  if (type1 == intHI_type_node)
1352    return unsigned_intHI_type_node;
1353  if (type1 == intQI_type_node)
1354    return unsigned_intQI_type_node;
1355
1356  return signed_or_unsigned_type (1, type);
1357}
1358
1359/* Return a signed type the same as TYPE in other respects.  */
1360
1361tree
1362signed_type (type)
1363     tree type;
1364{
1365  tree type1 = TYPE_MAIN_VARIANT (type);
1366  if (type1 == unsigned_char_type_node || type1 == char_type_node)
1367    return signed_char_type_node;
1368  if (type1 == unsigned_type_node)
1369    return integer_type_node;
1370  if (type1 == short_unsigned_type_node)
1371    return short_integer_type_node;
1372  if (type1 == long_unsigned_type_node)
1373    return long_integer_type_node;
1374  if (type1 == long_long_unsigned_type_node)
1375    return long_long_integer_type_node;
1376  if (type1 == unsigned_intTI_type_node)
1377    return intTI_type_node;
1378  if (type1 == unsigned_intDI_type_node)
1379    return intDI_type_node;
1380  if (type1 == unsigned_intSI_type_node)
1381    return intSI_type_node;
1382  if (type1 == unsigned_intHI_type_node)
1383    return intHI_type_node;
1384  if (type1 == unsigned_intQI_type_node)
1385    return intQI_type_node;
1386
1387  return signed_or_unsigned_type (0, type);
1388}
1389
1390/* Return a type the same as TYPE except unsigned or
1391   signed according to UNSIGNEDP.  */
1392
1393tree
1394signed_or_unsigned_type (unsignedp, type)
1395     int unsignedp;
1396     tree type;
1397{
1398  if (! INTEGRAL_TYPE_P (type)
1399      || TREE_UNSIGNED (type) == unsignedp)
1400    return type;
1401
1402  if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1403    return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1404  if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1405    return unsignedp ? unsigned_type_node : integer_type_node;
1406  if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1407    return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1408  if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1409    return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1410  if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1411    return (unsignedp ? long_long_unsigned_type_node
1412	    : long_long_integer_type_node);
1413  return type;
1414}
1415
1416/* Compute the value of the `sizeof' operator.  */
1417
1418tree
1419c_sizeof (type)
1420     tree type;
1421{
1422  enum tree_code code = TREE_CODE (type);
1423  tree t;
1424
1425  if (processing_template_decl)
1426    return build_min (SIZEOF_EXPR, sizetype, type);
1427
1428  if (code == FUNCTION_TYPE)
1429    {
1430      if (pedantic || warn_pointer_arith)
1431	pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1432      return size_int (1);
1433    }
1434  if (code == METHOD_TYPE)
1435    {
1436      if (pedantic || warn_pointer_arith)
1437	pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1438      return size_int (1);
1439    }
1440  if (code == VOID_TYPE)
1441    {
1442      if (pedantic || warn_pointer_arith)
1443	pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1444      return size_int (1);
1445    }
1446  if (code == ERROR_MARK)
1447    return size_int (1);
1448
1449  /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1450     referenced object.'' */
1451  if (code == REFERENCE_TYPE)
1452    type = TREE_TYPE (type);
1453
1454  /* We couldn't find anything in the ARM or the draft standard that says,
1455     one way or the other, if doing sizeof on something that doesn't have
1456     an object associated with it is correct or incorrect.  For example, if
1457     you declare `struct S { char str[16]; };', and in your program do
1458     a `sizeof (S::str)', should we flag that as an error or should we give
1459     the size of it?  Since it seems like a reasonable thing to do, we'll go
1460     with giving the value.  */
1461  if (code == OFFSET_TYPE)
1462    type = TREE_TYPE (type);
1463
1464  /* @@ This also produces an error for a signature ref.
1465        In that case we should be able to do better.  */
1466  if (IS_SIGNATURE (type))
1467    {
1468      error ("`sizeof' applied to a signature type");
1469      return size_int (0);
1470    }
1471
1472  if (TYPE_SIZE (complete_type (type)) == 0)
1473    {
1474      cp_error ("`sizeof' applied to incomplete type `%T'", type);
1475      return size_int (0);
1476    }
1477
1478  /* Convert in case a char is more than one unit.  */
1479  t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1480		  size_int (TYPE_PRECISION (char_type_node)));
1481  t = convert (sizetype, t);
1482  /* size_binop does not put the constant in range, so do it now.  */
1483  if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1484    TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1485  return t;
1486}
1487
1488tree
1489expr_sizeof (e)
1490     tree e;
1491{
1492  if (processing_template_decl)
1493    return build_min (SIZEOF_EXPR, sizetype, e);
1494
1495  if (TREE_CODE (e) == COMPONENT_REF
1496      && DECL_BIT_FIELD (TREE_OPERAND (e, 1)))
1497    error ("sizeof applied to a bit-field");
1498  /* ANSI says arrays and functions are converted inside comma.
1499     But we can't really convert them in build_compound_expr
1500     because that would break commas in lvalues.
1501     So do the conversion here if operand was a comma.  */
1502  if (TREE_CODE (e) == COMPOUND_EXPR
1503      && (TREE_CODE (TREE_TYPE (e)) == ARRAY_TYPE
1504	  || TREE_CODE (TREE_TYPE (e)) == FUNCTION_TYPE))
1505    e = default_conversion (e);
1506  else if (TREE_CODE (e) == TREE_LIST)
1507    {
1508      tree t = TREE_VALUE (e);
1509      if (t != NULL_TREE
1510	  && ((TREE_TYPE (t)
1511	       && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1512	      || is_overloaded_fn (t)))
1513	pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1514    }
1515  return c_sizeof (TREE_TYPE (e));
1516}
1517
1518tree
1519c_sizeof_nowarn (type)
1520     tree type;
1521{
1522  enum tree_code code = TREE_CODE (type);
1523  tree t;
1524
1525  if (code == FUNCTION_TYPE
1526      || code == METHOD_TYPE
1527      || code == VOID_TYPE
1528      || code == ERROR_MARK)
1529    return size_int (1);
1530  if (code == REFERENCE_TYPE)
1531    type = TREE_TYPE (type);
1532
1533  if (TYPE_SIZE (type) == 0)
1534    return size_int (0);
1535
1536  /* Convert in case a char is more than one unit.  */
1537  t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1538		  size_int (TYPE_PRECISION (char_type_node)));
1539  t = convert (sizetype, t);
1540  force_fit_type (t, 0);
1541  return t;
1542}
1543
1544/* Implement the __alignof keyword: Return the minimum required
1545   alignment of TYPE, measured in bytes.  */
1546
1547tree
1548c_alignof (type)
1549     tree type;
1550{
1551  enum tree_code code = TREE_CODE (type);
1552  tree t;
1553
1554  if (processing_template_decl)
1555    return build_min (ALIGNOF_EXPR, sizetype, type);
1556
1557  if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1558    return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1559
1560  if (code == VOID_TYPE || code == ERROR_MARK)
1561    return size_int (1);
1562
1563  /* C++: this is really correct!  */
1564  if (code == REFERENCE_TYPE)
1565    type = TREE_TYPE (type);
1566
1567  /* @@ This also produces an error for a signature ref.
1568        In that case we should be able to do better.  */
1569  if (IS_SIGNATURE (type))
1570    {
1571      error ("`__alignof' applied to a signature type");
1572      return size_int (1);
1573    }
1574
1575  t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1576  force_fit_type (t, 0);
1577  return t;
1578}
1579
1580/* Perform default promotions for C data used in expressions.
1581   Arrays and functions are converted to pointers;
1582   enumeral types or short or char, to int.
1583   In addition, manifest constants symbols are replaced by their values.
1584
1585   C++: this will automatically bash references to their target type.  */
1586
1587tree
1588decay_conversion (exp)
1589     tree exp;
1590{
1591  register tree type = TREE_TYPE (exp);
1592  register enum tree_code code = TREE_CODE (type);
1593
1594  if (code == OFFSET_TYPE)
1595    {
1596      if (TREE_CODE (exp) == OFFSET_REF)
1597	return decay_conversion (resolve_offset_ref (exp));
1598
1599      type = TREE_TYPE (type);
1600      code = TREE_CODE (type);
1601
1602      if (type == unknown_type_node)
1603	{
1604	  cp_pedwarn ("assuming & on overloaded member function");
1605	  return build_unary_op (ADDR_EXPR, exp, 0);
1606	}
1607    }
1608
1609  if (code == REFERENCE_TYPE)
1610    {
1611      exp = convert_from_reference (exp);
1612      type = TREE_TYPE (exp);
1613      code = TREE_CODE (type);
1614    }
1615
1616  /* Constants can be used directly unless they're not loadable.  */
1617  if (TREE_CODE (exp) == CONST_DECL)
1618    exp = DECL_INITIAL (exp);
1619  /* Replace a nonvolatile const static variable with its value.  */
1620  else if (TREE_READONLY_DECL_P (exp))
1621    {
1622      exp = decl_constant_value (exp);
1623      type = TREE_TYPE (exp);
1624    }
1625
1626  /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1627     Leave such NOP_EXPRs, since RHS is being used in non-lvalue context.  */
1628
1629  if (code == VOID_TYPE)
1630    {
1631      error ("void value not ignored as it ought to be");
1632      return error_mark_node;
1633    }
1634  if (code == FUNCTION_TYPE)
1635    {
1636      return build_unary_op (ADDR_EXPR, exp, 0);
1637    }
1638  if (code == METHOD_TYPE)
1639    {
1640      cp_pedwarn ("assuming & on `%E'", exp);
1641      return build_unary_op (ADDR_EXPR, exp, 0);
1642    }
1643  if (code == ARRAY_TYPE)
1644    {
1645      register tree adr;
1646      tree restype;
1647      tree ptrtype;
1648      int constp, volatilep;
1649
1650      if (TREE_CODE (exp) == INDIRECT_REF)
1651	{
1652	  /* Stripping away the INDIRECT_REF is not the right
1653	     thing to do for references...  */
1654	  tree inner = TREE_OPERAND (exp, 0);
1655	  if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1656	    {
1657	      inner = build1 (CONVERT_EXPR,
1658			      build_pointer_type (TREE_TYPE
1659						  (TREE_TYPE (inner))),
1660			      inner);
1661	      TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1662	    }
1663	  return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1664	}
1665
1666      if (TREE_CODE (exp) == COMPOUND_EXPR)
1667	{
1668	  tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1669	  return build (COMPOUND_EXPR, TREE_TYPE (op1),
1670			TREE_OPERAND (exp, 0), op1);
1671	}
1672
1673      if (!lvalue_p (exp)
1674	  && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1675	{
1676	  error ("invalid use of non-lvalue array");
1677	  return error_mark_node;
1678	}
1679
1680      constp = volatilep = 0;
1681      if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r'
1682	  || TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
1683	{
1684	  constp = TREE_READONLY (exp);
1685	  volatilep = TREE_THIS_VOLATILE (exp);
1686	}
1687
1688      restype = TREE_TYPE (type);
1689      if (TYPE_READONLY (type) || TYPE_VOLATILE (type)
1690	  || constp || volatilep)
1691	restype = cp_build_type_variant (restype,
1692					 TYPE_READONLY (type) || constp,
1693					 TYPE_VOLATILE (type) || volatilep);
1694      ptrtype = build_pointer_type (restype);
1695
1696      if (TREE_CODE (exp) == VAR_DECL)
1697	{
1698	  /* ??? This is not really quite correct
1699	     in that the type of the operand of ADDR_EXPR
1700	     is not the target type of the type of the ADDR_EXPR itself.
1701	     Question is, can this lossage be avoided?  */
1702	  adr = build1 (ADDR_EXPR, ptrtype, exp);
1703	  if (mark_addressable (exp) == 0)
1704	    return error_mark_node;
1705	  TREE_CONSTANT (adr) = staticp (exp);
1706	  TREE_SIDE_EFFECTS (adr) = 0;   /* Default would be, same as EXP.  */
1707	  return adr;
1708	}
1709      /* This way is better for a COMPONENT_REF since it can
1710	 simplify the offset for a component.  */
1711      adr = build_unary_op (ADDR_EXPR, exp, 1);
1712      return cp_convert (ptrtype, adr);
1713    }
1714
1715  return exp;
1716}
1717
1718tree
1719default_conversion (exp)
1720     tree exp;
1721{
1722  tree type;
1723  enum tree_code code;
1724
1725  exp = decay_conversion (exp);
1726
1727  type = TREE_TYPE (exp);
1728  code = TREE_CODE (type);
1729
1730  if (INTEGRAL_CODE_P (code))
1731    {
1732      tree t = type_promotes_to (type);
1733      if (t != type)
1734	return cp_convert (t, exp);
1735    }
1736
1737  return exp;
1738}
1739
1740/* Take the address of an inline function without setting TREE_ADDRESSABLE
1741   or TREE_USED.  */
1742
1743tree
1744inline_conversion (exp)
1745     tree exp;
1746{
1747  if (TREE_CODE (exp) == FUNCTION_DECL)
1748    {
1749      tree type = build_type_variant
1750	(TREE_TYPE (exp), TREE_READONLY (exp), TREE_THIS_VOLATILE (exp));
1751      exp = build1 (ADDR_EXPR, build_pointer_type (type), exp);
1752    }
1753  return exp;
1754}
1755
1756tree
1757build_object_ref (datum, basetype, field)
1758     tree datum, basetype, field;
1759{
1760  tree dtype;
1761  if (datum == error_mark_node)
1762    return error_mark_node;
1763
1764  dtype = TREE_TYPE (datum);
1765  if (TREE_CODE (dtype) == REFERENCE_TYPE)
1766    dtype = TREE_TYPE (dtype);
1767  if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1768    {
1769      cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1770		basetype, field, dtype);
1771      return error_mark_node;
1772    }
1773  else if (IS_SIGNATURE (basetype))
1774    {
1775      warning ("signature name in scope resolution ignored");
1776      return build_component_ref (datum, field, NULL_TREE, 1);
1777    }
1778  else if (is_aggr_type (basetype, 1))
1779    {
1780      tree binfo = binfo_or_else (basetype, dtype);
1781      if (binfo)
1782	return build_x_component_ref (build_scoped_ref (datum, basetype),
1783				      field, binfo, 1);
1784    }
1785  return error_mark_node;
1786}
1787
1788/* Like `build_component_ref, but uses an already found field, and converts
1789   from a reference.  Must compute access for current_class_ref.
1790   Otherwise, ok.  */
1791
1792tree
1793build_component_ref_1 (datum, field, protect)
1794     tree datum, field;
1795     int protect;
1796{
1797  return convert_from_reference
1798    (build_component_ref (datum, field, NULL_TREE, protect));
1799}
1800
1801/* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1802   can, for example, use as an lvalue.  This code used to be in
1803   unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1804   expressions, where we're dealing with aggregates.  But now it's again only
1805   called from unary_complex_lvalue.  The case (in particular) that led to
1806   this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1807   get it there.  */
1808
1809static tree
1810rationalize_conditional_expr (code, t)
1811     enum tree_code code;
1812     tree t;
1813{
1814  /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1815     the first operand is always the one to be used if both operands
1816     are equal, so we know what conditional expression this used to be.  */
1817  if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1818    {
1819      return
1820	build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1821						    ? LE_EXPR : GE_EXPR),
1822						   TREE_OPERAND (t, 0),
1823						   TREE_OPERAND (t, 1)),
1824			    build_unary_op (code, TREE_OPERAND (t, 0), 0),
1825			    build_unary_op (code, TREE_OPERAND (t, 1), 0));
1826    }
1827
1828  return
1829    build_conditional_expr (TREE_OPERAND (t, 0),
1830			    build_unary_op (code, TREE_OPERAND (t, 1), 0),
1831			    build_unary_op (code, TREE_OPERAND (t, 2), 0));
1832}
1833
1834/* Given the TYPE of an anonymous union field inside T, return the
1835   FIELD_DECL for the field.  If not found return NULL_TREE.  Because
1836   anonymous unions can nest, we must also search all anonymous unions
1837   that are directly reachable.  */
1838
1839static tree
1840lookup_anon_field (t, type)
1841     tree t, type;
1842{
1843  tree field;
1844
1845  for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1846    {
1847      if (TREE_STATIC (field))
1848	continue;
1849      if (TREE_CODE (field) != FIELD_DECL)
1850	continue;
1851
1852      /* If we find it directly, return the field.  */
1853      if (DECL_NAME (field) == NULL_TREE
1854	  && type == TREE_TYPE (field))
1855	{
1856	  return field;
1857	}
1858
1859      /* Otherwise, it could be nested, search harder.  */
1860      if (DECL_NAME (field) == NULL_TREE
1861	  && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
1862	{
1863	  tree subfield = lookup_anon_field (TREE_TYPE (field), type);
1864	  if (subfield)
1865	    return subfield;
1866	}
1867    }
1868  return NULL_TREE;
1869}
1870
1871/* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
1872   COMPONENT can be an IDENTIFIER_NODE that is the name of the member
1873   that we are interested in, or it can be a FIELD_DECL.  */
1874
1875tree
1876build_component_ref (datum, component, basetype_path, protect)
1877     tree datum, component, basetype_path;
1878     int protect;
1879{
1880  register tree basetype = TREE_TYPE (datum);
1881  register enum tree_code code;
1882  register tree field = NULL;
1883  register tree ref;
1884
1885  if (processing_template_decl)
1886    return build_min_nt (COMPONENT_REF, datum, component);
1887
1888  /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
1889     inside it.  */
1890  switch (TREE_CODE (datum))
1891    {
1892    case COMPOUND_EXPR:
1893      {
1894	tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
1895					  basetype_path, protect);
1896	return build (COMPOUND_EXPR, TREE_TYPE (value),
1897		      TREE_OPERAND (datum, 0), value);
1898      }
1899    case COND_EXPR:
1900      return build_conditional_expr
1901	(TREE_OPERAND (datum, 0),
1902	 build_component_ref (TREE_OPERAND (datum, 1), component,
1903			      basetype_path, protect),
1904	 build_component_ref (TREE_OPERAND (datum, 2), component,
1905			      basetype_path, protect));
1906
1907    case TEMPLATE_DECL:
1908      cp_error ("invalid use of %D", datum);
1909      datum = error_mark_node;
1910      break;
1911
1912    default:
1913      break;
1914    }
1915
1916  code = TREE_CODE (basetype);
1917
1918  if (code == REFERENCE_TYPE)
1919    {
1920      datum = convert_from_reference (datum);
1921      basetype = TREE_TYPE (datum);
1922      code = TREE_CODE (basetype);
1923    }
1924  if (TREE_CODE (datum) == OFFSET_REF)
1925    {
1926      datum = resolve_offset_ref (datum);
1927      basetype = TREE_TYPE (datum);
1928      code = TREE_CODE (basetype);
1929    }
1930
1931  /* First, see if there is a field or component with name COMPONENT.  */
1932  if (TREE_CODE (component) == TREE_LIST)
1933    {
1934      /* I could not trigger this code. MvL */
1935      my_friendly_abort (980326);
1936#ifdef DEAD
1937      my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
1938		&& DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
1939#endif
1940      return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
1941    }
1942
1943  if (! IS_AGGR_TYPE_CODE (code))
1944    {
1945      if (code != ERROR_MARK)
1946	cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1947		  component, datum, basetype);
1948      return error_mark_node;
1949    }
1950
1951  if (!complete_type_or_else (basetype))
1952    return error_mark_node;
1953
1954  if (TREE_CODE (component) == BIT_NOT_EXPR)
1955    {
1956      if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
1957	{
1958	  cp_error ("destructor specifier `%T::~%T' must have matching names",
1959		    basetype, TREE_OPERAND (component, 0));
1960	  return error_mark_node;
1961	}
1962      if (! TYPE_HAS_DESTRUCTOR (basetype))
1963	{
1964	  cp_error ("type `%T' has no destructor", basetype);
1965	  return error_mark_node;
1966	}
1967      return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
1968    }
1969
1970  /* Look up component name in the structure type definition.  */
1971  if (CLASSTYPE_VFIELD (basetype)
1972      && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
1973    /* Special-case this because if we use normal lookups in an ambiguous
1974       hierarchy, the compiler will abort (because vptr lookups are
1975       not supposed to be ambiguous.  */
1976    field = CLASSTYPE_VFIELD (basetype);
1977  else if (TREE_CODE (component) == FIELD_DECL)
1978    field = component;
1979  else if (TREE_CODE (component) == TYPE_DECL)
1980    {
1981      cp_pedwarn ("invalid use of type decl `%#D' as expression", component);
1982      return component;
1983    }
1984  else
1985    {
1986      tree name = component;
1987      if (TREE_CODE (component) == VAR_DECL)
1988	name = DECL_NAME (component);
1989      if (basetype_path == NULL_TREE)
1990	basetype_path = TYPE_BINFO (basetype);
1991      field = lookup_field (basetype_path, name,
1992			    protect && !VFIELD_NAME_P (name), 0);
1993      if (field == error_mark_node)
1994	return error_mark_node;
1995
1996      if (field == NULL_TREE)
1997	{
1998	  /* Not found as a data field, look for it as a method.  If found,
1999	     then if this is the only possible one, return it, else
2000	     report ambiguity error.  */
2001	  tree fndecls = lookup_fnfields (basetype_path, name, 1);
2002	  if (fndecls == error_mark_node)
2003	    return error_mark_node;
2004	  if (fndecls)
2005	    {
2006	      if (TREE_CHAIN (fndecls) == NULL_TREE
2007		  && TREE_CODE (TREE_VALUE (fndecls)) != OVERLOAD)
2008		{
2009		  tree access, fndecl;
2010
2011		  /* Unique, so use this one now.  */
2012		  basetype = TREE_PURPOSE (fndecls);
2013		  fndecl = TREE_VALUE (fndecls);
2014		  access = compute_access (TREE_PURPOSE (fndecls), fndecl);
2015		  if (access == access_public_node)
2016		    {
2017		      if (DECL_VINDEX (fndecl)
2018			  && ! resolves_to_fixed_type_p (datum, 0))
2019			{
2020			  tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2021			  tree fntype = TREE_TYPE (fndecl);
2022
2023			  addr = convert_pointer_to (DECL_CONTEXT (fndecl),
2024						     addr);
2025			  datum = build_indirect_ref (addr, NULL_PTR);
2026			  my_friendly_assert (datum != error_mark_node, 310);
2027			  fndecl = build_vfn_ref (&addr, datum,
2028						  DECL_VINDEX (fndecl));
2029			  /* The type of fndecl is a function type,
2030			     not a pointer-to-function type, since
2031			     build_vfn_ref returns not the correct
2032			     vtable slot, but the indirection of the
2033			     correct vtable slot.  */
2034			  TREE_TYPE (fndecl) = fntype;
2035			}
2036		      else
2037			mark_used (fndecl);
2038		      return build (OFFSET_REF, TREE_TYPE (fndecl),
2039				    datum, fndecl);
2040		    }
2041		  if (access == access_protected_node)
2042		    cp_error ("member function `%D' is protected", fndecl);
2043		  else
2044		    cp_error ("member function `%D' is private", fndecl);
2045		  return error_mark_node;
2046		}
2047	      else
2048		{
2049		  /* Just act like build_offset_ref, since the object does
2050                     not matter unless we're actually calling the function.  */
2051		  tree t;
2052
2053		  t = build_tree_list (error_mark_node, fndecls);
2054		  TREE_TYPE (t) = build_offset_type (basetype,
2055						     unknown_type_node);
2056		  return t;
2057		}
2058	    }
2059
2060	  cp_error ("`%#T' has no member named `%D'", basetype, name);
2061	  return error_mark_node;
2062	}
2063      else if (TREE_TYPE (field) == error_mark_node)
2064	return error_mark_node;
2065
2066      if (TREE_CODE (field) != FIELD_DECL)
2067	{
2068	  if (TREE_CODE (field) == TYPE_DECL)
2069	    cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2070	  else if (DECL_RTL (field) != 0)
2071	    mark_used (field);
2072	  else
2073	    TREE_USED (field) = 1;
2074	  return field;
2075	}
2076    }
2077
2078  /* See if we have to do any conversions so that we pick up the field from the
2079     right context.  */
2080  if (DECL_FIELD_CONTEXT (field) != basetype)
2081    {
2082      tree context = DECL_FIELD_CONTEXT (field);
2083      tree base = context;
2084      while (!comptypes (base, basetype,1) && TYPE_NAME (base)
2085	     && ANON_UNION_TYPE_P (base))
2086	{
2087	  base = TYPE_CONTEXT (base);
2088	}
2089
2090      /* Handle base classes here...  */
2091      if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
2092	{
2093	  tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2094	  if (integer_zerop (addr))
2095	    {
2096	      error ("invalid reference to NULL ptr, use ptr-to-member instead");
2097	      return error_mark_node;
2098	    }
2099	  if (VBASE_NAME_P (DECL_NAME (field)))
2100	    {
2101	      /* It doesn't matter which vbase pointer we grab, just
2102		 find one of them.  */
2103	      tree binfo = get_binfo (base,
2104				      TREE_TYPE (TREE_TYPE (addr)), 0);
2105	      addr = convert_pointer_to_real (binfo, addr);
2106	    }
2107	  else
2108	    addr = convert_pointer_to (base, addr);
2109	  datum = build_indirect_ref (addr, NULL_PTR);
2110	  my_friendly_assert (datum != error_mark_node, 311);
2111	}
2112      basetype = base;
2113
2114      /* Handle things from anon unions here...  */
2115      if (TYPE_NAME (context) && ANON_UNION_TYPE_P (context))
2116	{
2117	  tree subfield = lookup_anon_field (basetype, context);
2118	  tree subdatum = build_component_ref (datum, subfield,
2119					       basetype_path, protect);
2120	  return build_component_ref (subdatum, field, basetype_path, protect);
2121	}
2122    }
2123
2124  ref = fold (build (COMPONENT_REF, TREE_TYPE (field),
2125		     break_out_cleanups (datum), field));
2126
2127  if (TREE_READONLY (datum) || TREE_READONLY (field))
2128    TREE_READONLY (ref) = 1;
2129  if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (field))
2130    TREE_THIS_VOLATILE (ref) = 1;
2131  if (DECL_LANG_SPECIFIC (field) && DECL_MUTABLE_P (field))
2132    TREE_READONLY (ref) = 0;
2133
2134  return ref;
2135}
2136
2137/* Variant of build_component_ref for use in expressions, which should
2138   never have REFERENCE_TYPE.  */
2139
2140tree
2141build_x_component_ref (datum, component, basetype_path, protect)
2142     tree datum, component, basetype_path;
2143     int protect;
2144{
2145  tree t = build_component_ref (datum, component, basetype_path, protect);
2146
2147  if (! processing_template_decl)
2148    t = convert_from_reference (t);
2149
2150  return t;
2151}
2152
2153/* Given an expression PTR for a pointer, return an expression
2154   for the value pointed to.
2155   ERRORSTRING is the name of the operator to appear in error messages.
2156
2157   This function may need to overload OPERATOR_FNNAME.
2158   Must also handle REFERENCE_TYPEs for C++.  */
2159
2160tree
2161build_x_indirect_ref (ptr, errorstring)
2162     tree ptr;
2163     char *errorstring;
2164{
2165  tree rval;
2166
2167  if (processing_template_decl)
2168    return build_min_nt (INDIRECT_REF, ptr);
2169
2170  rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2171			 NULL_TREE);
2172  if (rval)
2173    return rval;
2174  return build_indirect_ref (ptr, errorstring);
2175}
2176
2177tree
2178build_indirect_ref (ptr, errorstring)
2179     tree ptr;
2180     char *errorstring;
2181{
2182  register tree pointer, type;
2183
2184  if (ptr == error_mark_node)
2185    return error_mark_node;
2186
2187  pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2188	     ? ptr : default_conversion (ptr));
2189  type = TREE_TYPE (pointer);
2190
2191  if (ptr == current_class_ptr)
2192    return current_class_ref;
2193
2194  if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
2195    {
2196      if (TREE_CODE (pointer) == ADDR_EXPR
2197	  && !flag_volatile
2198	  && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (pointer, 0)))
2199	      == TYPE_MAIN_VARIANT (TREE_TYPE (type)))
2200	  && (TREE_READONLY (TREE_OPERAND (pointer, 0))
2201	      == TYPE_READONLY (TREE_TYPE (type)))
2202	  && (TREE_THIS_VOLATILE (TREE_OPERAND (pointer, 0))
2203	      == TYPE_VOLATILE (TREE_TYPE (type))))
2204	return TREE_OPERAND (pointer, 0);
2205      else
2206	{
2207	  tree t = TREE_TYPE (type);
2208	  register tree ref = build1 (INDIRECT_REF,
2209				      TYPE_MAIN_VARIANT (t), pointer);
2210
2211	  /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2212	     so that we get the proper error message if the result is used
2213	     to assign to.  Also, &* is supposed to be a no-op.  */
2214	  TREE_READONLY (ref) = TYPE_READONLY (t);
2215	  TREE_SIDE_EFFECTS (ref)
2216	    = (TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer)
2217	       || flag_volatile);
2218	  TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
2219	  return ref;
2220	}
2221    }
2222  /* `pointer' won't be an error_mark_node if we were given a
2223     pointer to member, so it's cool to check for this here.  */
2224  else if (TYPE_PTRMEMFUNC_P (type))
2225    error ("invalid use of `%s' on pointer to member function", errorstring);
2226  else if (TREE_CODE (type) == RECORD_TYPE
2227	   && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
2228    error ("cannot dereference signature pointer/reference");
2229  else if (pointer != error_mark_node)
2230    {
2231      if (errorstring)
2232	error ("invalid type argument of `%s'", errorstring);
2233      else
2234	error ("invalid type argument");
2235    }
2236  return error_mark_node;
2237}
2238
2239/* This handles expressions of the form "a[i]", which denotes
2240   an array reference.
2241
2242   This is logically equivalent in C to *(a+i), but we may do it differently.
2243   If A is a variable or a member, we generate a primitive ARRAY_REF.
2244   This avoids forcing the array out of registers, and can work on
2245   arrays that are not lvalues (for example, members of structures returned
2246   by functions).
2247
2248   If INDEX is of some user-defined type, it must be converted to
2249   integer type.  Otherwise, to make a compatible PLUS_EXPR, it
2250   will inherit the type of the array, which will be some pointer type.  */
2251
2252tree
2253build_array_ref (array, idx)
2254     tree array, idx;
2255{
2256  if (idx == 0)
2257    {
2258      error ("subscript missing in array reference");
2259      return error_mark_node;
2260    }
2261
2262  if (TREE_TYPE (array) == error_mark_node
2263      || TREE_TYPE (idx) == error_mark_node)
2264    return error_mark_node;
2265
2266  if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2267      && TREE_CODE (array) != INDIRECT_REF)
2268    {
2269      tree rval, type;
2270
2271      /* Subscripting with type char is likely to lose
2272	 on a machine where chars are signed.
2273	 So warn on any machine, but optionally.
2274	 Don't warn for unsigned char since that type is safe.
2275	 Don't warn for signed char because anyone who uses that
2276	 must have done so deliberately.  */
2277      if (warn_char_subscripts
2278	  && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2279	warning ("array subscript has type `char'");
2280
2281      /* Apply default promotions *after* noticing character types.  */
2282      idx = default_conversion (idx);
2283
2284      if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2285	{
2286	  error ("array subscript is not an integer");
2287	  return error_mark_node;
2288	}
2289
2290      /* An array that is indexed by a non-constant
2291	 cannot be stored in a register; we must be able to do
2292	 address arithmetic on its address.
2293	 Likewise an array of elements of variable size.  */
2294      if (TREE_CODE (idx) != INTEGER_CST
2295	  || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2296	      && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2297		  != INTEGER_CST)))
2298	{
2299	  if (mark_addressable (array) == 0)
2300	    return error_mark_node;
2301	}
2302      /* An array that is indexed by a constant value which is not within
2303	 the array bounds cannot be stored in a register either; because we
2304	 would get a crash in store_bit_field/extract_bit_field when trying
2305	 to access a non-existent part of the register.  */
2306      if (TREE_CODE (idx) == INTEGER_CST
2307	  && TYPE_VALUES (TREE_TYPE (array))
2308	  && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2309	{
2310	  if (mark_addressable (array) == 0)
2311	    return error_mark_node;
2312	}
2313
2314      if (pedantic && !lvalue_p (array))
2315	pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2316
2317      /* Note in C++ it is valid to subscript a `register' array, since
2318	 it is valid to take the address of something with that
2319	 storage specification.  */
2320      if (extra_warnings)
2321	{
2322	  tree foo = array;
2323	  while (TREE_CODE (foo) == COMPONENT_REF)
2324	    foo = TREE_OPERAND (foo, 0);
2325	  if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2326	    warning ("subscripting array declared `register'");
2327	}
2328
2329      type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
2330      rval = build (ARRAY_REF, type, array, idx);
2331      /* Array ref is const/volatile if the array elements are
2332	 or if the array is..  */
2333      TREE_READONLY (rval)
2334	|= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2335	    | TREE_READONLY (array));
2336      TREE_SIDE_EFFECTS (rval)
2337	|= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2338	    | TREE_SIDE_EFFECTS (array));
2339      TREE_THIS_VOLATILE (rval)
2340	|= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2341	    /* This was added by rms on 16 Nov 91.
2342	       It fixes  vol struct foo *a;  a->elts[1]
2343	       in an inline function.
2344	       Hope it doesn't break something else.  */
2345	    | TREE_THIS_VOLATILE (array));
2346      return require_complete_type (fold (rval));
2347    }
2348
2349  {
2350    tree ar = default_conversion (array);
2351    tree ind = default_conversion (idx);
2352
2353    /* Put the integer in IND to simplify error checking.  */
2354    if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2355      {
2356	tree temp = ar;
2357	ar = ind;
2358	ind = temp;
2359      }
2360
2361    if (ar == error_mark_node)
2362      return ar;
2363
2364    if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2365      {
2366	error ("subscripted value is neither array nor pointer");
2367	return error_mark_node;
2368      }
2369    if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2370      {
2371	error ("array subscript is not an integer");
2372	return error_mark_node;
2373      }
2374
2375    return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar,
2376							  ind, PLUS_EXPR),
2377			       "array indexing");
2378  }
2379}
2380
2381/* Build a function call to function FUNCTION with parameters PARAMS.
2382   PARAMS is a list--a chain of TREE_LIST nodes--in which the
2383   TREE_VALUE of each node is a parameter-expression.  The PARAMS do
2384   not include any object pointer that may be required.  FUNCTION's
2385   data type may be a function type or a pointer-to-function.
2386
2387   For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2388   is the list of possible methods that FUNCTION could conceivably
2389   be.  If the list of methods comes from a class, then it will be
2390   a list of lists (where each element is associated with the class
2391   that produced it), otherwise it will be a simple list (for
2392   functions overloaded in global scope).
2393
2394   In the first case, TREE_VALUE (function) is the head of one of those
2395   lists, and TREE_PURPOSE is the name of the function.
2396
2397   In the second case, TREE_PURPOSE (function) is the function's
2398   name directly.
2399
2400   DECL is the class instance variable, usually CURRENT_CLASS_REF.
2401
2402   When calling a TEMPLATE_DECL, we don't require a complete return
2403   type.  */
2404
2405tree
2406build_x_function_call (function, params, decl)
2407     tree function, params, decl;
2408{
2409  tree type;
2410  tree template_id = NULL_TREE;
2411  int is_method;
2412
2413  if (function == error_mark_node)
2414    return error_mark_node;
2415
2416  if (processing_template_decl)
2417    return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2418
2419  /* Save explicit template arguments if found */
2420  if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2421    {
2422      template_id = function;
2423      function = TREE_OPERAND (function, 0);
2424    }
2425
2426  type = TREE_TYPE (function);
2427
2428  if (TREE_CODE (type) == OFFSET_TYPE
2429      && TREE_TYPE (type) == unknown_type_node
2430      && TREE_CODE (function) == TREE_LIST
2431      && TREE_CHAIN (function) == NULL_TREE)
2432    {
2433      /* Undo (Foo:bar)()...  */
2434      type = TYPE_OFFSET_BASETYPE (type);
2435      function = TREE_VALUE (function);
2436      my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2437      my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2438      function = TREE_VALUE (function);
2439      if (TREE_CODE (function) == OVERLOAD)
2440	function = OVL_FUNCTION (function);
2441      my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2442      function = DECL_NAME (function);
2443      return build_method_call (decl, function, params,
2444				TYPE_BINFO (type), LOOKUP_NORMAL);
2445    }
2446
2447  is_method = ((TREE_CODE (function) == TREE_LIST
2448		&& current_class_type != NULL_TREE
2449		&& (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2450		    == function))
2451	       || TREE_CODE (function) == IDENTIFIER_NODE
2452	       || TREE_CODE (type) == METHOD_TYPE
2453	       || TYPE_PTRMEMFUNC_P (type));
2454
2455  if ((TREE_CODE (function) == FUNCTION_DECL
2456       && DECL_STATIC_FUNCTION_P (function))
2457      || (TREE_CODE (function) == TEMPLATE_DECL
2458	  && DECL_STATIC_FUNCTION_P (DECL_RESULT (function))))
2459    return build_member_call
2460      (DECL_CONTEXT (function), DECL_NAME (function), params);
2461
2462  /* A friend template.  Make it look like a toplevel declaration.  */
2463  if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2464    function = scratch_ovl_cons (function, NULL_TREE);
2465
2466  /* Handle methods, friends, and overloaded functions, respectively.  */
2467  if (is_method)
2468    {
2469      tree basetype = NULL_TREE;
2470
2471      if (TREE_CODE (function) == FUNCTION_DECL
2472	  || DECL_FUNCTION_TEMPLATE_P (function))
2473	{
2474	  basetype = DECL_CLASS_CONTEXT (function);
2475
2476	  if (DECL_NAME (function))
2477	    function = DECL_NAME (function);
2478	  else
2479	    function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2480	}
2481      else if (TREE_CODE (function) == TREE_LIST)
2482	{
2483	  my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2484			      == FUNCTION_DECL, 312);
2485	  basetype = DECL_CLASS_CONTEXT (TREE_VALUE (function));
2486	  function = TREE_PURPOSE (function);
2487	}
2488      else if (TREE_CODE (function) != IDENTIFIER_NODE)
2489	{
2490	  if (TREE_CODE (function) == OFFSET_REF)
2491	    {
2492	      if (TREE_OPERAND (function, 0))
2493		decl = TREE_OPERAND (function, 0);
2494	    }
2495	  /* Call via a pointer to member function.  */
2496	  if (decl == NULL_TREE)
2497	    {
2498	      error ("pointer to member function called, but not in class scope");
2499	      return error_mark_node;
2500	    }
2501	  /* What other type of POINTER_TYPE could this be? */
2502	  if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2503	      && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2504	      && TREE_CODE (function) != OFFSET_REF)
2505	    function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2506			      function);
2507	  goto do_x_function;
2508	}
2509
2510      /* this is an abbreviated method call.
2511         must go through here in case it is a virtual function.
2512	 @@ Perhaps this could be optimized.  */
2513
2514      if (basetype && (! current_class_type
2515		       || ! DERIVED_FROM_P (basetype, current_class_type)))
2516	return build_member_call (basetype, function, params);
2517
2518      if (decl == NULL_TREE)
2519	{
2520	  if (current_class_type == NULL_TREE)
2521	    {
2522	      error ("object missing in call to method `%s'",
2523		     IDENTIFIER_POINTER (function));
2524	      return error_mark_node;
2525	    }
2526	  /* Yow: call from a static member function.  */
2527	  decl = build1 (NOP_EXPR, build_pointer_type (current_class_type),
2528			 error_mark_node);
2529	  decl = build_indirect_ref (decl, NULL_PTR);
2530	}
2531
2532      /* Put back explicit template arguments, if any.  */
2533      if (template_id)
2534        function = template_id;
2535      return build_method_call (decl, function, params,
2536				NULL_TREE, LOOKUP_NORMAL);
2537    }
2538  else if (TREE_CODE (function) == COMPONENT_REF
2539	   && type == unknown_type_node)
2540    {
2541      /* Should we undo what was done in build_component_ref? */
2542      if (TREE_CODE (TREE_PURPOSE (TREE_OPERAND (function, 1))) == TREE_VEC)
2543	/* Get the name that build_component_ref hid.  */
2544	function = DECL_NAME (TREE_VALUE (TREE_OPERAND (function, 1)));
2545      else
2546	function = TREE_PURPOSE (TREE_OPERAND (function, 1));
2547      return build_method_call (decl, function, params,
2548				NULL_TREE, LOOKUP_NORMAL);
2549    }
2550  else if (really_overloaded_fn (function))
2551    {
2552      if (OVL_FUNCTION (function) == NULL_TREE)
2553	{
2554	  cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2555		    TREE_PURPOSE (function));
2556	  return error_mark_node;
2557	}
2558      else
2559	{
2560	  /* Put back explicit template arguments, if any.  */
2561	  if (template_id)
2562	    function = template_id;
2563	  return build_new_function_call (function, params);
2564	}
2565    }
2566  else
2567    /* Remove a potential OVERLOAD around it */
2568    function = OVL_CURRENT (function);
2569
2570 do_x_function:
2571  if (TREE_CODE (function) == OFFSET_REF)
2572    {
2573      /* If the component is a data element (or a virtual function), we play
2574	 games here to make things work.  */
2575      tree decl_addr;
2576
2577      if (TREE_OPERAND (function, 0))
2578	decl = TREE_OPERAND (function, 0);
2579      else
2580	decl = current_class_ref;
2581
2582      decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2583
2584      /* Sigh.  OFFSET_REFs are being used for too many things.
2585	 They're being used both for -> and ->*, and we want to resolve
2586	 the -> cases here, but leave the ->*.  We could use
2587	 resolve_offset_ref for those, too, but it would call
2588         get_member_function_from_ptrfunc and decl_addr wouldn't get
2589         updated properly.  Nasty.  */
2590      if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2591	function = resolve_offset_ref (function);
2592      else
2593	function = TREE_OPERAND (function, 1);
2594
2595      function = get_member_function_from_ptrfunc (&decl_addr, function);
2596      params = expr_tree_cons (NULL_TREE, decl_addr, params);
2597      return build_function_call (function, params);
2598    }
2599
2600  type = TREE_TYPE (function);
2601  if (type != error_mark_node)
2602    {
2603      if (TREE_CODE (type) == REFERENCE_TYPE)
2604	type = TREE_TYPE (type);
2605
2606      if (IS_AGGR_TYPE (type))
2607	return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2608    }
2609
2610  if (is_method)
2611    {
2612      tree fntype = TREE_TYPE (function);
2613      tree ctypeptr = NULL_TREE;
2614
2615      /* Explicitly named method?  */
2616      if (TREE_CODE (function) == FUNCTION_DECL)
2617	ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2618      /* Expression with ptr-to-method type?  It could either be a plain
2619	 usage, or it might be a case where the ptr-to-method is being
2620	 passed in as an argument.  */
2621      else if (TYPE_PTRMEMFUNC_P (fntype))
2622	{
2623	  tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2624					   (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2625	  ctypeptr = build_pointer_type (rec);
2626	}
2627      /* Unexpected node type?  */
2628      else
2629	my_friendly_abort (116);
2630      if (decl == NULL_TREE)
2631	{
2632	  if (current_function_decl
2633	      && DECL_STATIC_FUNCTION_P (current_function_decl))
2634	    error ("invalid call to member function needing `this' in static member function scope");
2635	  else
2636	    error ("pointer to member function called, but not in class scope");
2637	  return error_mark_node;
2638	}
2639      if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2640	  && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2641	{
2642	  decl = build_unary_op (ADDR_EXPR, decl, 0);
2643	  decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2644	}
2645      else
2646	decl = build_c_cast (ctypeptr, decl);
2647      params = expr_tree_cons (NULL_TREE, decl, params);
2648    }
2649
2650  return build_function_call (function, params);
2651}
2652
2653/* Resolve a pointer to member function.  INSTANCE is the object
2654   instance to use, if the member points to a virtual member.  */
2655
2656tree
2657get_member_function_from_ptrfunc (instance_ptrptr, function)
2658     tree *instance_ptrptr;
2659     tree function;
2660{
2661  if (TREE_CODE (function) == OFFSET_REF)
2662    {
2663      function = TREE_OPERAND (function, 1);
2664    }
2665
2666  if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2667    {
2668      tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2669      tree instance;
2670
2671      tree instance_ptr = *instance_ptrptr;
2672
2673      if (TREE_SIDE_EFFECTS (instance_ptr))
2674	instance_ptr = save_expr (instance_ptr);
2675
2676      if (TREE_SIDE_EFFECTS (function))
2677	function = save_expr (function);
2678
2679      fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2680
2681      /* Promoting idx before saving it improves performance on RISC
2682	 targets.  Without promoting, the first compare used
2683	 load-with-sign-extend, while the second used normal load then
2684	 shift to sign-extend.  An optimizer flaw, perhaps, but it's easier
2685	 to make this change.  */
2686      idx = save_expr (default_conversion
2687		       (build_component_ref (function,
2688					     index_identifier,
2689					     NULL_TREE, 0)));
2690      e1 = build_binary_op (GT_EXPR, idx, integer_zero_node, 1);
2691      delta = cp_convert (ptrdiff_type_node,
2692			  build_component_ref (function, delta_identifier,
2693					       NULL_TREE, 0));
2694      delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2695
2696      /* Convert down to the right base, before using the instance.  */
2697      instance
2698	= convert_pointer_to_real (TYPE_METHOD_BASETYPE (TREE_TYPE (fntype)),
2699				   instance_ptr);
2700      if (instance == error_mark_node && instance_ptr != error_mark_node)
2701	return instance;
2702
2703      vtbl = convert_pointer_to (ptr_type_node, instance);
2704      vtbl
2705	= build (PLUS_EXPR,
2706		 build_pointer_type (build_pointer_type (vtable_entry_type)),
2707		 vtbl, cp_convert (ptrdiff_type_node, delta2));
2708      vtbl = build_indirect_ref (vtbl, NULL_PTR);
2709      aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2710						     idx,
2711						     integer_one_node, 1));
2712      if (! flag_vtable_thunks)
2713	{
2714	  aref = save_expr (aref);
2715
2716	  delta = build_binary_op
2717	    (PLUS_EXPR,
2718	     build_conditional_expr (e1, build_component_ref (aref,
2719							      delta_identifier,
2720							      NULL_TREE, 0),
2721				     integer_zero_node),
2722	     delta, 1);
2723	}
2724
2725      *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2726				instance_ptr, delta);
2727      if (flag_vtable_thunks)
2728	e2 = aref;
2729      else
2730	e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2731
2732      e3 = PFN_FROM_PTRMEMFUNC (function);
2733      TREE_TYPE (e2) = TREE_TYPE (e3);
2734      e1 = build_conditional_expr (e1, e2, e3);
2735
2736      if (instance_ptr == error_mark_node
2737	  && TREE_CODE (e1) != ADDR_EXPR
2738	  && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2739	cp_error ("object missing in `%E'", function);
2740
2741      function = e1;
2742
2743      /* Make sure this doesn't get evaluated first inside one of the
2744         branches of the COND_EXPR.  */
2745      if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2746	function = build (COMPOUND_EXPR, TREE_TYPE (function),
2747			  instance_ptr, function);
2748    }
2749  return function;
2750}
2751
2752tree
2753build_function_call_real (function, params, require_complete, flags)
2754     tree function, params;
2755     int require_complete, flags;
2756{
2757  register tree fntype, fndecl;
2758  register tree value_type;
2759  register tree coerced_params;
2760  tree name = NULL_TREE, assembler_name = NULL_TREE;
2761  int is_method;
2762
2763  /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2764     Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context.  */
2765  if (TREE_CODE (function) == NOP_EXPR
2766      && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2767    function = TREE_OPERAND (function, 0);
2768
2769  if (TREE_CODE (function) == FUNCTION_DECL)
2770    {
2771      name = DECL_NAME (function);
2772      assembler_name = DECL_ASSEMBLER_NAME (function);
2773
2774      GNU_xref_call (current_function_decl,
2775		     IDENTIFIER_POINTER (name ? name
2776					 : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2777							    (function))));
2778      mark_used (function);
2779      fndecl = function;
2780
2781      /* Convert anything with function type to a pointer-to-function.  */
2782      if (pedantic && DECL_MAIN_P (function))
2783	pedwarn ("ANSI C++ forbids calling `main' from within program");
2784
2785      /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2786	 (because calling an inline function does not mean the function
2787	 needs to be separately compiled).  */
2788
2789      if (DECL_INLINE (function))
2790	function = inline_conversion (function);
2791      else
2792	function = build_addr_func (function);
2793    }
2794  else
2795    {
2796      fndecl = NULL_TREE;
2797
2798      function = build_addr_func (function);
2799    }
2800
2801  if (function == error_mark_node)
2802    return error_mark_node;
2803
2804  fntype = TREE_TYPE (function);
2805
2806  if (TYPE_PTRMEMFUNC_P (fntype))
2807    {
2808      cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
2809		function);
2810      return error_mark_node;
2811    }
2812
2813  is_method = (TREE_CODE (fntype) == POINTER_TYPE
2814	       && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
2815
2816  if (!((TREE_CODE (fntype) == POINTER_TYPE
2817	 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
2818	|| is_method
2819	|| TREE_CODE (function) == TEMPLATE_ID_EXPR))
2820    {
2821      cp_error ("`%E' cannot be used as a function", function);
2822      return error_mark_node;
2823    }
2824
2825  /* fntype now gets the type of function pointed to.  */
2826  fntype = TREE_TYPE (fntype);
2827
2828  /* Convert the parameters to the types declared in the
2829     function prototype, or apply default promotions.  */
2830
2831  if (flags & LOOKUP_COMPLAIN)
2832    coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2833					params, fndecl, LOOKUP_NORMAL);
2834  else
2835    coerced_params = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2836					params, fndecl, 0);
2837
2838  if (coerced_params == error_mark_node)
2839    {
2840      if (flags & LOOKUP_SPECULATIVELY)
2841	return NULL_TREE;
2842      else
2843	return error_mark_node;
2844    }
2845
2846  /* Check for errors in format strings.  */
2847
2848  if (warn_format && (name || assembler_name))
2849    check_function_format (name, assembler_name, coerced_params);
2850
2851  /* Recognize certain built-in functions so we can make tree-codes
2852     other than CALL_EXPR.  We do this when it enables fold-const.c
2853     to do something useful.  */
2854
2855  if (TREE_CODE (function) == ADDR_EXPR
2856      && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
2857      && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
2858    switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
2859      {
2860      case BUILT_IN_ABS:
2861      case BUILT_IN_LABS:
2862      case BUILT_IN_FABS:
2863	if (coerced_params == 0)
2864	  return integer_zero_node;
2865	return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
2866
2867      default:
2868	break;
2869      }
2870
2871  /* C++ */
2872  value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2873  {
2874    register tree result
2875      = build_call (function, value_type, coerced_params);
2876
2877    if (require_complete)
2878      {
2879	if (value_type == void_type_node)
2880	  return result;
2881	result = require_complete_type (result);
2882      }
2883    if (IS_AGGR_TYPE (value_type))
2884      result = build_cplus_new (value_type, result);
2885    return convert_from_reference (result);
2886  }
2887}
2888
2889tree
2890build_function_call (function, params)
2891     tree function, params;
2892{
2893  return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
2894}
2895
2896/* Convert the actual parameter expressions in the list VALUES
2897   to the types in the list TYPELIST.
2898   If parmdecls is exhausted, or when an element has NULL as its type,
2899   perform the default conversions.
2900
2901   RETURN_LOC is the location of the return value, if known, NULL_TREE
2902   otherwise.  This is useful in the case where we can avoid creating
2903   a temporary variable in the case where we can initialize the return
2904   value directly.  If we are not eliding constructors, then we set this
2905   to NULL_TREE to avoid this avoidance.
2906
2907   NAME is an IDENTIFIER_NODE or 0.  It is used only for error messages.
2908
2909   This is also where warnings about wrong number of args are generated.
2910
2911   Return a list of expressions for the parameters as converted.
2912
2913   Both VALUES and the returned value are chains of TREE_LIST nodes
2914   with the elements of the list in the TREE_VALUE slots of those nodes.
2915
2916   In C++, unspecified trailing parameters can be filled in with their
2917   default arguments, if such were specified.  Do so here.  */
2918
2919tree
2920convert_arguments (return_loc, typelist, values, fndecl, flags)
2921     tree return_loc, typelist, values, fndecl;
2922     int flags;
2923{
2924  register tree typetail, valtail;
2925  register tree result = NULL_TREE;
2926  char *called_thing = 0;
2927  int i = 0;
2928
2929  if (! flag_elide_constructors)
2930    return_loc = 0;
2931
2932  /* Argument passing is always copy-initialization.  */
2933  flags |= LOOKUP_ONLYCONVERTING;
2934
2935  if (fndecl)
2936    {
2937      if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
2938	{
2939	  if (DECL_NAME (fndecl) == NULL_TREE
2940	      || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
2941	    called_thing = "constructor";
2942	  else
2943	    called_thing = "member function";
2944	}
2945      else
2946	called_thing = "function";
2947    }
2948
2949  for (valtail = values, typetail = typelist;
2950       valtail;
2951       valtail = TREE_CHAIN (valtail), i++)
2952    {
2953      register tree type = typetail ? TREE_VALUE (typetail) : 0;
2954      register tree val = TREE_VALUE (valtail);
2955
2956      if (val == error_mark_node)
2957	return error_mark_node;
2958
2959      if (type == void_type_node)
2960	{
2961	  if (fndecl)
2962	    {
2963	      cp_error_at ("too many arguments to %s `%+D'", called_thing,
2964			   fndecl);
2965	      error ("at this point in file");
2966	    }
2967	  else
2968	    error ("too many arguments to function");
2969	  /* In case anybody wants to know if this argument
2970	     list is valid.  */
2971	  if (result)
2972	    TREE_TYPE (tree_last (result)) = error_mark_node;
2973	  break;
2974	}
2975
2976      /* The tree type of the parameter being passed may not yet be
2977	 known.  In this case, its type is TYPE_UNKNOWN, and will
2978	 be instantiated by the type given by TYPE.  If TYPE
2979	 is also NULL, the tree type of VAL is ERROR_MARK_NODE.  */
2980      if (type && type_unknown_p (val))
2981	val = require_instantiated_type (type, val, integer_zero_node);
2982      else if (type_unknown_p (val))
2983	{
2984	  /* Strip the `&' from an overloaded FUNCTION_DECL.  */
2985	  if (TREE_CODE (val) == ADDR_EXPR)
2986	    val = TREE_OPERAND (val, 0);
2987	  if (really_overloaded_fn (val))
2988	    cp_error ("insufficient type information to resolve address of overloaded function `%D'",
2989		      DECL_NAME (get_first_fn (val)));
2990	  else
2991	    error ("insufficient type information in parameter list");
2992	  val = integer_zero_node;
2993	}
2994      else if (TREE_CODE (val) == OFFSET_REF
2995	    && TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
2996	{
2997	  /* This is unclean.  Should be handled elsewhere.  */
2998	  val = build_unary_op (ADDR_EXPR, val, 0);
2999	}
3000      else if (TREE_CODE (val) == OFFSET_REF)
3001	val = resolve_offset_ref (val);
3002
3003      /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3004	 Strip such NOP_EXPRs, since VAL is used in non-lvalue context.  */
3005      if (TREE_CODE (val) == NOP_EXPR
3006	  && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3007	  && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3008	val = TREE_OPERAND (val, 0);
3009
3010      if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3011	{
3012	  if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3013	      || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3014	      || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3015	    val = default_conversion (val);
3016
3017	  val = require_complete_type (val);
3018	}
3019
3020      if (val == error_mark_node)
3021	return error_mark_node;
3022
3023      if (type != 0)
3024	{
3025	  /* Formal parm type is specified by a function prototype.  */
3026	  tree parmval;
3027
3028	  if (TYPE_SIZE (complete_type (type)) == 0)
3029	    {
3030	      error ("parameter type of called function is incomplete");
3031	      parmval = val;
3032	    }
3033	  else
3034	    {
3035	      parmval = convert_for_initialization
3036		(return_loc, type, val, flags,
3037		 "argument passing", fndecl, i);
3038#ifdef PROMOTE_PROTOTYPES
3039	      if ((TREE_CODE (type) == INTEGER_TYPE
3040		   || TREE_CODE (type) == ENUMERAL_TYPE)
3041		  && (TYPE_PRECISION (type)
3042		      < TYPE_PRECISION (integer_type_node)))
3043		parmval = default_conversion (parmval);
3044#endif
3045	    }
3046
3047	  if (parmval == error_mark_node)
3048	    return error_mark_node;
3049
3050	  result = expr_tree_cons (NULL_TREE, parmval, result);
3051	}
3052      else
3053	{
3054	  if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3055	    val = convert_from_reference (val);
3056
3057	  result = expr_tree_cons (NULL_TREE,
3058				   convert_arg_to_ellipsis (val),
3059				   result);
3060	}
3061
3062      if (typetail)
3063	typetail = TREE_CHAIN (typetail);
3064    }
3065
3066  if (typetail != 0 && typetail != void_list_node)
3067    {
3068      /* See if there are default arguments that can be used */
3069      if (TREE_PURPOSE (typetail))
3070	{
3071	  for (; typetail != void_list_node; ++i)
3072	    {
3073	      tree type = TREE_VALUE (typetail);
3074	      tree val = TREE_PURPOSE (typetail);
3075	      tree parmval = convert_default_arg (type, val);
3076
3077	      if (parmval == error_mark_node)
3078		return error_mark_node;
3079
3080	      result = expr_tree_cons (0, parmval, result);
3081	      typetail = TREE_CHAIN (typetail);
3082	      /* ends with `...'.  */
3083	      if (typetail == NULL_TREE)
3084		break;
3085	    }
3086	}
3087      else
3088	{
3089	  if (fndecl)
3090	    {
3091	      char *buf = (char *)alloca (32 + strlen (called_thing));
3092	      sprintf (buf, "too few arguments to %s `%%#D'", called_thing);
3093	      cp_error_at (buf, fndecl);
3094	      error ("at this point in file");
3095	    }
3096	  else
3097	    error ("too few arguments to function");
3098	  return error_mark_list;
3099	}
3100    }
3101
3102  return nreverse (result);
3103}
3104
3105/* Build a binary-operation expression, after performing default
3106   conversions on the operands.  CODE is the kind of expression to build.  */
3107
3108tree
3109build_x_binary_op (code, arg1, arg2)
3110     enum tree_code code;
3111     tree arg1, arg2;
3112{
3113  if (processing_template_decl)
3114    return build_min_nt (code, arg1, arg2);
3115
3116  return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3117}
3118
3119tree
3120build_binary_op (code, arg1, arg2, convert_p)
3121     enum tree_code code;
3122     tree arg1, arg2;
3123     int convert_p;
3124{
3125  tree args[2];
3126
3127  args[0] = arg1;
3128  args[1] = arg2;
3129
3130  if (convert_p)
3131    {
3132      tree type0, type1;
3133      args[0] = decay_conversion (args[0]);
3134      args[1] = decay_conversion (args[1]);
3135
3136      if (args[0] == error_mark_node || args[1] == error_mark_node)
3137	return error_mark_node;
3138
3139      type0 = TREE_TYPE (args[0]);
3140      type1 = TREE_TYPE (args[1]);
3141
3142      if (type_unknown_p (args[0]))
3143	{
3144	  args[0] = instantiate_type (type1, args[0], 1);
3145	  args[0] = decay_conversion (args[0]);
3146	}
3147      else if (type_unknown_p (args[1]))
3148	{
3149	  args[1] = require_instantiated_type (type0, args[1],
3150					       error_mark_node);
3151	  args[1] = decay_conversion (args[1]);
3152	}
3153
3154      if (IS_AGGR_TYPE (type0) || IS_AGGR_TYPE (type1))
3155	my_friendly_abort (754867);
3156    }
3157  return build_binary_op_nodefault (code, args[0], args[1], code);
3158}
3159
3160/* Build a binary-operation expression without default conversions.
3161   CODE is the kind of expression to build.
3162   This function differs from `build' in several ways:
3163   the data type of the result is computed and recorded in it,
3164   warnings are generated if arg data types are invalid,
3165   special handling for addition and subtraction of pointers is known,
3166   and some optimization is done (operations on narrow ints
3167   are done in the narrower type when that gives the same result).
3168   Constant folding is also done before the result is returned.
3169
3170   ERROR_CODE is the code that determines what to say in error messages.
3171   It is usually, but not always, the same as CODE.
3172
3173   Note that the operands will never have enumeral types
3174   because either they have just had the default conversions performed
3175   or they have both just been converted to some other type in which
3176   the arithmetic is to be done.
3177
3178   C++: must do special pointer arithmetic when implementing
3179   multiple inheritance, and deal with pointer to member functions.  */
3180
3181tree
3182build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3183     enum tree_code code;
3184     tree orig_op0, orig_op1;
3185     enum tree_code error_code;
3186{
3187  tree op0, op1;
3188  register enum tree_code code0, code1;
3189  tree type0, type1;
3190
3191  /* Expression code to give to the expression when it is built.
3192     Normally this is CODE, which is what the caller asked for,
3193     but in some special cases we change it.  */
3194  register enum tree_code resultcode = code;
3195
3196  /* Data type in which the computation is to be performed.
3197     In the simplest cases this is the common type of the arguments.  */
3198  register tree result_type = NULL;
3199
3200  /* Nonzero means operands have already been type-converted
3201     in whatever way is necessary.
3202     Zero means they need to be converted to RESULT_TYPE.  */
3203  int converted = 0;
3204
3205  /* Nonzero means create the expression with this type, rather than
3206     RESULT_TYPE.  */
3207  tree build_type = 0;
3208
3209  /* Nonzero means after finally constructing the expression
3210     convert it to this type.  */
3211  tree final_type = 0;
3212
3213  /* Nonzero if this is an operation like MIN or MAX which can
3214     safely be computed in short if both args are promoted shorts.
3215     Also implies COMMON.
3216     -1 indicates a bitwise operation; this makes a difference
3217     in the exact conditions for when it is safe to do the operation
3218     in a narrower mode.  */
3219  int shorten = 0;
3220
3221  /* Nonzero if this is a comparison operation;
3222     if both args are promoted shorts, compare the original shorts.
3223     Also implies COMMON.  */
3224  int short_compare = 0;
3225
3226  /* Nonzero if this is a right-shift operation, which can be computed on the
3227     original short and then promoted if the operand is a promoted short.  */
3228  int short_shift = 0;
3229
3230  /* Nonzero means set RESULT_TYPE to the common type of the args.  */
3231  int common = 0;
3232
3233  /* Apply default conversions.  */
3234  if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3235      || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3236      || code == TRUTH_XOR_EXPR)
3237    {
3238      op0 = decay_conversion (orig_op0);
3239      op1 = decay_conversion (orig_op1);
3240    }
3241  else
3242    {
3243      op0 = default_conversion (orig_op0);
3244      op1 = default_conversion (orig_op1);
3245    }
3246
3247  type0 = TREE_TYPE (op0);
3248  type1 = TREE_TYPE (op1);
3249
3250  /* The expression codes of the data types of the arguments tell us
3251     whether the arguments are integers, floating, pointers, etc.  */
3252  code0 = TREE_CODE (type0);
3253  code1 = TREE_CODE (type1);
3254
3255  /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
3256  STRIP_TYPE_NOPS (op0);
3257  STRIP_TYPE_NOPS (op1);
3258
3259  /* If an error was already reported for one of the arguments,
3260     avoid reporting another error.  */
3261
3262  if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3263    return error_mark_node;
3264
3265  switch (code)
3266    {
3267    case PLUS_EXPR:
3268      /* Handle the pointer + int case.  */
3269      if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3270	return pointer_int_sum (PLUS_EXPR, op0, op1);
3271      else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3272	return pointer_int_sum (PLUS_EXPR, op1, op0);
3273      else
3274	common = 1;
3275      break;
3276
3277    case MINUS_EXPR:
3278      /* Subtraction of two similar pointers.
3279	 We must subtract them as integers, then divide by object size.  */
3280      if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3281	  && comp_target_types (type0, type1, 1))
3282	return pointer_diff (op0, op1, common_type (type0, type1));
3283      /* Handle pointer minus int.  Just like pointer plus int.  */
3284      else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3285	return pointer_int_sum (MINUS_EXPR, op0, op1);
3286      else
3287	common = 1;
3288      break;
3289
3290    case MULT_EXPR:
3291      common = 1;
3292      break;
3293
3294    case TRUNC_DIV_EXPR:
3295    case CEIL_DIV_EXPR:
3296    case FLOOR_DIV_EXPR:
3297    case ROUND_DIV_EXPR:
3298    case EXACT_DIV_EXPR:
3299      if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3300	   || code0 == COMPLEX_TYPE)
3301	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3302	      || code1 == COMPLEX_TYPE))
3303	{
3304	  if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3305	    cp_warning ("division by zero in `%E / 0'", op0);
3306	  else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3307	    cp_warning ("division by zero in `%E / 0.'", op0);
3308
3309	  if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3310	    resultcode = RDIV_EXPR;
3311	  else
3312	    /* When dividing two signed integers, we have to promote to int.
3313	       unless we divide by a constant != -1.  Note that default
3314	       conversion will have been performed on the operands at this
3315	       point, so we have to dig out the original type to find out if
3316	       it was unsigned.  */
3317	    shorten = ((TREE_CODE (op0) == NOP_EXPR
3318			&& TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3319		       || (TREE_CODE (op1) == INTEGER_CST
3320			   && (TREE_INT_CST_LOW (op1) != -1
3321			       || TREE_INT_CST_HIGH (op1) != -1)));
3322	  common = 1;
3323	}
3324      break;
3325
3326    case BIT_AND_EXPR:
3327    case BIT_ANDTC_EXPR:
3328    case BIT_IOR_EXPR:
3329    case BIT_XOR_EXPR:
3330      if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3331	shorten = -1;
3332      /* If one operand is a constant, and the other is a short type
3333	 that has been converted to an int,
3334	 really do the work in the short type and then convert the
3335	 result to int.  If we are lucky, the constant will be 0 or 1
3336	 in the short type, making the entire operation go away.  */
3337      if (TREE_CODE (op0) == INTEGER_CST
3338	  && TREE_CODE (op1) == NOP_EXPR
3339	  && (TYPE_PRECISION (type1)
3340	      > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3341	  && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3342	{
3343	  final_type = result_type;
3344	  op1 = TREE_OPERAND (op1, 0);
3345	  result_type = TREE_TYPE (op1);
3346	}
3347      if (TREE_CODE (op1) == INTEGER_CST
3348	  && TREE_CODE (op0) == NOP_EXPR
3349	  && (TYPE_PRECISION (type0)
3350	      > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3351	  && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3352	{
3353	  final_type = result_type;
3354	  op0 = TREE_OPERAND (op0, 0);
3355	  result_type = TREE_TYPE (op0);
3356	}
3357      break;
3358
3359    case TRUNC_MOD_EXPR:
3360    case FLOOR_MOD_EXPR:
3361      if (code1 == INTEGER_TYPE && integer_zerop (op1))
3362	cp_warning ("division by zero in `%E %% 0'", op0);
3363      else if (code1 == REAL_TYPE && real_zerop (op1))
3364	cp_warning ("division by zero in `%E %% 0.'", op0);
3365
3366      if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3367	{
3368	  /* Although it would be tempting to shorten always here, that loses
3369	     on some targets, since the modulo instruction is undefined if the
3370	     quotient can't be represented in the computation mode.  We shorten
3371	     only if unsigned or if dividing by something we know != -1.  */
3372	  shorten = ((TREE_CODE (op0) == NOP_EXPR
3373		      && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3374		     || (TREE_CODE (op1) == INTEGER_CST
3375			 && (TREE_INT_CST_LOW (op1) != -1
3376			     || TREE_INT_CST_HIGH (op1) != -1)));
3377	  common = 1;
3378	}
3379      break;
3380
3381    case TRUTH_ANDIF_EXPR:
3382    case TRUTH_ORIF_EXPR:
3383    case TRUTH_AND_EXPR:
3384    case TRUTH_OR_EXPR:
3385      result_type = boolean_type_node;
3386      break;
3387
3388      /* Shift operations: result has same type as first operand;
3389	 always convert second operand to int.
3390	 Also set SHORT_SHIFT if shifting rightward.  */
3391
3392    case RSHIFT_EXPR:
3393      if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3394	{
3395	  result_type = type0;
3396	  if (TREE_CODE (op1) == INTEGER_CST)
3397	    {
3398	      if (tree_int_cst_lt (op1, integer_zero_node))
3399		warning ("right shift count is negative");
3400	      else
3401		{
3402		  if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3403		    short_shift = 1;
3404		  if (TREE_INT_CST_HIGH (op1) != 0
3405		      || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3406			  >= TYPE_PRECISION (type0)))
3407		    warning ("right shift count >= width of type");
3408		}
3409	    }
3410	  /* Convert the shift-count to an integer, regardless of
3411	     size of value being shifted.  */
3412	  if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3413	    op1 = cp_convert (integer_type_node, op1);
3414	  /* Avoid converting op1 to result_type later.  */
3415	  converted = 1;
3416	}
3417      break;
3418
3419    case LSHIFT_EXPR:
3420      if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3421	{
3422	  result_type = type0;
3423	  if (TREE_CODE (op1) == INTEGER_CST)
3424	    {
3425	      if (tree_int_cst_lt (op1, integer_zero_node))
3426		warning ("left shift count is negative");
3427	      else if (TREE_INT_CST_HIGH (op1) != 0
3428		       || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3429			   >= TYPE_PRECISION (type0)))
3430		warning ("left shift count >= width of type");
3431	    }
3432	  /* Convert the shift-count to an integer, regardless of
3433	     size of value being shifted.  */
3434	  if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3435	    op1 = cp_convert (integer_type_node, op1);
3436	  /* Avoid converting op1 to result_type later.  */
3437	  converted = 1;
3438	}
3439      break;
3440
3441    case RROTATE_EXPR:
3442    case LROTATE_EXPR:
3443      if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3444	{
3445	  result_type = type0;
3446	  if (TREE_CODE (op1) == INTEGER_CST)
3447	    {
3448	      if (tree_int_cst_lt (op1, integer_zero_node))
3449		warning ("%s rotate count is negative",
3450			 (code == LROTATE_EXPR) ? "left" : "right");
3451	      else if (TREE_INT_CST_HIGH (op1) != 0
3452		       || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3453			   >= TYPE_PRECISION (type0)))
3454		warning ("%s rotate count >= width of type",
3455			 (code == LROTATE_EXPR) ? "left" : "right");
3456	    }
3457	  /* Convert the shift-count to an integer, regardless of
3458	     size of value being shifted.  */
3459	  if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3460	    op1 = cp_convert (integer_type_node, op1);
3461	}
3462      break;
3463
3464    case EQ_EXPR:
3465    case NE_EXPR:
3466      build_type = boolean_type_node;
3467      if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3468	   || code0 == COMPLEX_TYPE)
3469	  && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3470	      || code1 == COMPLEX_TYPE))
3471	short_compare = 1;
3472      else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3473	{
3474	  register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3475	  register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3476
3477	  if (comp_target_types (type0, type1, 1))
3478	    result_type = common_type (type0, type1);
3479	  else if (tt0 == void_type_node)
3480	    {
3481	      if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3482		  && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3483		pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3484	      else if (TREE_CODE (tt1) == OFFSET_TYPE)
3485		pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3486	    }
3487	  else if (tt1 == void_type_node)
3488	    {
3489	      if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3490		  && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3491		pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3492	    }
3493	  else
3494	    cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3495			type0, type1);
3496
3497	  if (result_type == NULL_TREE)
3498	    result_type = ptr_type_node;
3499	}
3500      else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3501	       && integer_zerop (op1))
3502	result_type = type0;
3503      else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3504	       && integer_zerop (op0))
3505	result_type = type1;
3506      else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3507	{
3508	  result_type = type0;
3509	  error ("ANSI C++ forbids comparison between pointer and integer");
3510	}
3511      else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3512	{
3513	  result_type = type1;
3514	  error ("ANSI C++ forbids comparison between pointer and integer");
3515	}
3516      else if (TYPE_PTRMEMFUNC_P (type0) && TREE_CODE (op1) == INTEGER_CST
3517	       && integer_zerop (op1))
3518	{
3519	  op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3520	  op1 = integer_zero_node;
3521	  result_type = TREE_TYPE (op0);
3522	}
3523      else if (TYPE_PTRMEMFUNC_P (type1) && TREE_CODE (op0) == INTEGER_CST
3524	       && integer_zerop (op0))
3525	{
3526	  op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3527	  op1 = integer_zero_node;
3528	  result_type = TREE_TYPE (op0);
3529	}
3530      else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3531	       && (TYPE_PTRMEMFUNC_FN_TYPE (type0)
3532		   == TYPE_PTRMEMFUNC_FN_TYPE (type1)))
3533	{
3534	  /* The code we generate for the test is:
3535
3536	  (op0.index == op1.index
3537	   && ((op1.index != -1 && op0.delta2 == op1.delta2)
3538	       || op0.pfn == op1.pfn)) */
3539
3540	  tree index0 = build_component_ref (op0, index_identifier,
3541					     NULL_TREE, 0);
3542	  tree index1 = save_expr (build_component_ref (op1, index_identifier,
3543							NULL_TREE, 0));
3544	  tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3545	  tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3546	  tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3547	  tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3548	  tree e1, e2, e3;
3549	  tree integer_neg_one_node
3550	    = build_binary_op (MINUS_EXPR, integer_zero_node,
3551			       integer_one_node, 1);
3552	  e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3553	  e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3554	  e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3555				build_binary_op (EQ_EXPR, delta20, delta21, 1),
3556				1);
3557	  e3 = build_binary_op (EQ_EXPR, pfn0, pfn1, 1);
3558	  e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3559	  e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3560	  if (code == EQ_EXPR)
3561	    return e2;
3562	  return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3563	}
3564      else if (TYPE_PTRMEMFUNC_P (type0)
3565	       && TYPE_PTRMEMFUNC_FN_TYPE (type0) == type1)
3566	{
3567	  tree index0 = build_component_ref (op0, index_identifier,
3568					     NULL_TREE, 0);
3569	  tree index1;
3570	  tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3571	  tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3572	  tree delta21 = integer_zero_node;
3573	  tree e1, e2, e3;
3574	  tree integer_neg_one_node
3575	    = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node, 1);
3576	  if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3577	      && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3578	    {
3579	      /* Map everything down one to make room for
3580		 the null pointer to member.  */
3581	      index1 = size_binop (PLUS_EXPR,
3582				   DECL_VINDEX (TREE_OPERAND (op1, 0)),
3583				   integer_one_node);
3584	      op1 = integer_zero_node;
3585	      delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE
3586					  (TREE_TYPE (type1)));
3587	      delta21 = DECL_FIELD_BITPOS (delta21);
3588	      delta21 = size_binop (FLOOR_DIV_EXPR, delta21,
3589				    size_int (BITS_PER_UNIT));
3590	      delta21 = convert (sizetype, delta21);
3591	    }
3592	  else
3593	    index1 = integer_neg_one_node;
3594	  {
3595	    tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0),
3596				op1);
3597	    TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3598	    op1 = nop1;
3599	  }
3600	  e1 = build_binary_op (EQ_EXPR, index0, index1, 1);
3601	  e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node, 1);
3602	  e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3603				build_binary_op (EQ_EXPR, delta20, delta21, 1),
3604				1);
3605	  e3 = build_binary_op (EQ_EXPR, pfn0, op1, 1);
3606	  e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3, 1);
3607	  e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2, 1);
3608	  if (code == EQ_EXPR)
3609	    return e2;
3610	  return build_binary_op (EQ_EXPR, e2, integer_zero_node, 1);
3611	}
3612      else if (TYPE_PTRMEMFUNC_P (type1)
3613	       && TYPE_PTRMEMFUNC_FN_TYPE (type1) == type0)
3614	{
3615	  return build_binary_op (code, op1, op0, 1);
3616	}
3617      break;
3618
3619    case MAX_EXPR:
3620    case MIN_EXPR:
3621      if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3622	   && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3623	shorten = 1;
3624      else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3625	{
3626	  if (comp_target_types (type0, type1, 1))
3627	    result_type = common_type (type0, type1);
3628	  else
3629	    {
3630	      cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3631			  type0, type1);
3632	      result_type = ptr_type_node;
3633	    }
3634	}
3635      break;
3636
3637    case LE_EXPR:
3638    case GE_EXPR:
3639    case LT_EXPR:
3640    case GT_EXPR:
3641      build_type = boolean_type_node;
3642      if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3643	   && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3644	short_compare = 1;
3645      else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3646	{
3647	  if (comp_target_types (type0, type1, 1))
3648	    result_type = common_type (type0, type1);
3649	  else
3650	    {
3651	      cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3652			  type0, type1);
3653	      result_type = ptr_type_node;
3654	    }
3655	}
3656      else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3657	       && integer_zerop (op1))
3658	result_type = type0;
3659      else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3660	       && integer_zerop (op0))
3661	result_type = type1;
3662      else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3663	{
3664	  result_type = type0;
3665	  pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3666	}
3667      else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3668	{
3669	  result_type = type1;
3670	  pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3671	}
3672      break;
3673
3674    default:
3675      break;
3676    }
3677
3678  if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3679      &&
3680      (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3681    {
3682      int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3683
3684      if (shorten || common || short_compare)
3685	result_type = common_type (type0, type1);
3686
3687      /* For certain operations (which identify themselves by shorten != 0)
3688	 if both args were extended from the same smaller type,
3689	 do the arithmetic in that type and then extend.
3690
3691	 shorten !=0 and !=1 indicates a bitwise operation.
3692	 For them, this optimization is safe only if
3693	 both args are zero-extended or both are sign-extended.
3694	 Otherwise, we might change the result.
3695	 Eg, (short)-1 | (unsigned short)-1 is (int)-1
3696	 but calculated in (unsigned short) it would be (unsigned short)-1.  */
3697
3698      if (shorten && none_complex)
3699	{
3700	  int unsigned0, unsigned1;
3701	  tree arg0 = get_narrower (op0, &unsigned0);
3702	  tree arg1 = get_narrower (op1, &unsigned1);
3703	  /* UNS is 1 if the operation to be done is an unsigned one.  */
3704	  int uns = TREE_UNSIGNED (result_type);
3705	  tree type;
3706
3707	  final_type = result_type;
3708
3709	  /* Handle the case that OP0 does not *contain* a conversion
3710	     but it *requires* conversion to FINAL_TYPE.  */
3711
3712	  if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3713	    unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3714	  if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3715	    unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3716
3717	  /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE.  */
3718
3719	  /* For bitwise operations, signedness of nominal type
3720	     does not matter.  Consider only how operands were extended.  */
3721	  if (shorten == -1)
3722	    uns = unsigned0;
3723
3724	  /* Note that in all three cases below we refrain from optimizing
3725	     an unsigned operation on sign-extended args.
3726	     That would not be valid.  */
3727
3728	  /* Both args variable: if both extended in same way
3729	     from same width, do it in that width.
3730	     Do it unsigned if args were zero-extended.  */
3731	  if ((TYPE_PRECISION (TREE_TYPE (arg0))
3732	       < TYPE_PRECISION (result_type))
3733	      && (TYPE_PRECISION (TREE_TYPE (arg1))
3734		  == TYPE_PRECISION (TREE_TYPE (arg0)))
3735	      && unsigned0 == unsigned1
3736	      && (unsigned0 || !uns))
3737	    result_type
3738	      = signed_or_unsigned_type (unsigned0,
3739					 common_type (TREE_TYPE (arg0),
3740						      TREE_TYPE (arg1)));
3741	  else if (TREE_CODE (arg0) == INTEGER_CST
3742		   && (unsigned1 || !uns)
3743		   && (TYPE_PRECISION (TREE_TYPE (arg1))
3744		       < TYPE_PRECISION (result_type))
3745		   && (type = signed_or_unsigned_type (unsigned1,
3746						       TREE_TYPE (arg1)),
3747		       int_fits_type_p (arg0, type)))
3748	    result_type = type;
3749	  else if (TREE_CODE (arg1) == INTEGER_CST
3750		   && (unsigned0 || !uns)
3751		   && (TYPE_PRECISION (TREE_TYPE (arg0))
3752		       < TYPE_PRECISION (result_type))
3753		   && (type = signed_or_unsigned_type (unsigned0,
3754						       TREE_TYPE (arg0)),
3755		       int_fits_type_p (arg1, type)))
3756	    result_type = type;
3757	}
3758
3759      /* Shifts can be shortened if shifting right.  */
3760
3761      if (short_shift)
3762	{
3763	  int unsigned_arg;
3764	  tree arg0 = get_narrower (op0, &unsigned_arg);
3765
3766	  final_type = result_type;
3767
3768	  if (arg0 == op0 && final_type == TREE_TYPE (op0))
3769	    unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3770
3771	  if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3772	      /* We can shorten only if the shift count is less than the
3773		 number of bits in the smaller type size.  */
3774	      && TREE_INT_CST_HIGH (op1) == 0
3775	      && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3776	      /* If arg is sign-extended and then unsigned-shifted,
3777		 we can simulate this with a signed shift in arg's type
3778		 only if the extended result is at least twice as wide
3779		 as the arg.  Otherwise, the shift could use up all the
3780		 ones made by sign-extension and bring in zeros.
3781		 We can't optimize that case at all, but in most machines
3782		 it never happens because available widths are 2**N.  */
3783	      && (!TREE_UNSIGNED (final_type)
3784		  || unsigned_arg
3785		  || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3786		      <= TYPE_PRECISION (result_type))))
3787	    {
3788	      /* Do an unsigned shift if the operand was zero-extended.  */
3789	      result_type
3790		= signed_or_unsigned_type (unsigned_arg,
3791					   TREE_TYPE (arg0));
3792	      /* Convert value-to-be-shifted to that type.  */
3793	      if (TREE_TYPE (op0) != result_type)
3794		op0 = cp_convert (result_type, op0);
3795	      converted = 1;
3796	    }
3797	}
3798
3799      /* Comparison operations are shortened too but differently.
3800	 They identify themselves by setting short_compare = 1.  */
3801
3802      if (short_compare)
3803	{
3804	  /* Don't write &op0, etc., because that would prevent op0
3805	     from being kept in a register.
3806	     Instead, make copies of the our local variables and
3807	     pass the copies by reference, then copy them back afterward.  */
3808	  tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3809	  enum tree_code xresultcode = resultcode;
3810	  tree val
3811	    = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3812	  if (val != 0)
3813	    return cp_convert (boolean_type_node, val);
3814	  op0 = xop0, op1 = xop1;
3815	  converted = 1;
3816	  resultcode = xresultcode;
3817	}
3818
3819      if (short_compare && warn_sign_compare)
3820	{
3821	  int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3822	  int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3823
3824	  int unsignedp0, unsignedp1;
3825	  tree primop0 = get_narrower (op0, &unsignedp0);
3826	  tree primop1 = get_narrower (op1, &unsignedp1);
3827
3828	  /* Check for comparison of different enum types.  */
3829	  if (flag_int_enum_equivalence == 0
3830	      && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
3831	      && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
3832	      && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3833	         != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3834	    {
3835	      cp_warning ("comparison between `%#T' and `%#T'",
3836			  TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3837	    }
3838
3839	  /* Give warnings for comparisons between signed and unsigned
3840	     quantities that may fail.  */
3841	  /* Do the checking based on the original operand trees, so that
3842	     casts will be considered, but default promotions won't be.  */
3843
3844	  /* Do not warn if the comparison is being done in a signed type,
3845	     since the signed type will only be chosen if it can represent
3846	     all the values of the unsigned type.  */
3847	  if (! TREE_UNSIGNED (result_type))
3848	    /* OK */;
3849	  /* Do not warn if both operands are unsigned.  */
3850	  else if (op0_signed == op1_signed)
3851	    /* OK */;
3852	  /* Do not warn if the signed quantity is an unsuffixed
3853	     integer literal (or some static constant expression
3854	     involving such literals) and it is non-negative.  */
3855	  else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
3856		    && tree_int_cst_sgn (orig_op0) >= 0)
3857		   || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
3858		       && tree_int_cst_sgn (orig_op1) >= 0))
3859	    /* OK */;
3860	  /* Do not warn if the comparison is an equality operation,
3861	     the unsigned quantity is an integral constant and it does
3862	     not use the most significant bit of result_type.  */
3863	  else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
3864		   && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
3865			&& int_fits_type_p (orig_op1,
3866					    signed_type (result_type)))
3867			|| (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
3868			    && int_fits_type_p (orig_op0,
3869						signed_type (result_type)))))
3870	    /* OK */;
3871	  else
3872	    warning ("comparison between signed and unsigned");
3873
3874	  /* Warn if two unsigned values are being compared in a size
3875	     larger than their original size, and one (and only one) is the
3876	     result of a `~' operator.  This comparison will always fail.
3877
3878	     Also warn if one operand is a constant, and the constant does not
3879	     have all bits set that are set in the ~ operand when it is
3880	     extended.  */
3881
3882	  if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
3883	      ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
3884	    {
3885	      if (TREE_CODE (primop0) == BIT_NOT_EXPR)
3886		primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
3887	      if (TREE_CODE (primop1) == BIT_NOT_EXPR)
3888		primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
3889
3890	      if (TREE_CODE (primop0) == INTEGER_CST
3891		  || TREE_CODE (primop1) == INTEGER_CST)
3892		{
3893		  tree primop;
3894		  HOST_WIDE_INT constant, mask;
3895		  int unsignedp;
3896		  unsigned bits;
3897
3898		  if (TREE_CODE (primop0) == INTEGER_CST)
3899		    {
3900		      primop = primop1;
3901		      unsignedp = unsignedp1;
3902		      constant = TREE_INT_CST_LOW (primop0);
3903		    }
3904		  else
3905		    {
3906		      primop = primop0;
3907		      unsignedp = unsignedp0;
3908		      constant = TREE_INT_CST_LOW (primop1);
3909		    }
3910
3911		  bits = TYPE_PRECISION (TREE_TYPE (primop));
3912		  if (bits < TYPE_PRECISION (result_type)
3913		      && bits < HOST_BITS_PER_LONG && unsignedp)
3914		    {
3915		      mask = (~ (HOST_WIDE_INT) 0) << bits;
3916		      if ((mask & constant) != mask)
3917			warning ("comparison of promoted ~unsigned with constant");
3918		    }
3919		}
3920	      else if (unsignedp0 && unsignedp1
3921		       && (TYPE_PRECISION (TREE_TYPE (primop0))
3922			   < TYPE_PRECISION (result_type))
3923		       && (TYPE_PRECISION (TREE_TYPE (primop1))
3924			   < TYPE_PRECISION (result_type)))
3925		warning ("comparison of promoted ~unsigned with unsigned");
3926	    }
3927	}
3928    }
3929
3930  /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
3931     If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
3932     Then the expression will be built.
3933     It will be given type FINAL_TYPE if that is nonzero;
3934     otherwise, it will be given type RESULT_TYPE.  */
3935
3936  if (!result_type)
3937    {
3938      cp_error ("invalid operands `%T' and `%T' to binary `%O'",
3939		TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
3940      return error_mark_node;
3941    }
3942
3943  if (! converted)
3944    {
3945      if (TREE_TYPE (op0) != result_type)
3946	op0 = cp_convert (result_type, op0);
3947      if (TREE_TYPE (op1) != result_type)
3948	op1 = cp_convert (result_type, op1);
3949    }
3950
3951  if (build_type == NULL_TREE)
3952    build_type = result_type;
3953
3954  {
3955    register tree result = build (resultcode, build_type, op0, op1);
3956    register tree folded;
3957
3958    folded = fold (result);
3959    if (folded == result)
3960      TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
3961    if (final_type != 0)
3962      return cp_convert (final_type, folded);
3963    return folded;
3964  }
3965}
3966
3967/* Return a tree for the sum or difference (RESULTCODE says which)
3968   of pointer PTROP and integer INTOP.  */
3969
3970static tree
3971pointer_int_sum (resultcode, ptrop, intop)
3972     enum tree_code resultcode;
3973     register tree ptrop, intop;
3974{
3975  tree size_exp;
3976
3977  register tree result;
3978  register tree folded = fold (intop);
3979
3980  /* The result is a pointer of the same type that is being added.  */
3981
3982  register tree result_type = TREE_TYPE (ptrop);
3983
3984  if (!complete_type_or_else (result_type))
3985    return error_mark_node;
3986
3987  if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3988    {
3989      if (pedantic || warn_pointer_arith)
3990	pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
3991      size_exp = integer_one_node;
3992    }
3993  else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3994    {
3995      if (pedantic || warn_pointer_arith)
3996	pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
3997      size_exp = integer_one_node;
3998    }
3999  else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4000    {
4001      if (pedantic || warn_pointer_arith)
4002	pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
4003      size_exp = integer_one_node;
4004    }
4005  else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4006    {
4007      if (pedantic || warn_pointer_arith)
4008	pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
4009      size_exp = integer_one_node;
4010    }
4011  else
4012    size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4013
4014  /* Needed to make OOPS V2R3 work.  */
4015  intop = folded;
4016  if (TREE_CODE (intop) == INTEGER_CST
4017      && TREE_INT_CST_LOW (intop) == 0
4018      && TREE_INT_CST_HIGH (intop) == 0)
4019    return ptrop;
4020
4021  /* If what we are about to multiply by the size of the elements
4022     contains a constant term, apply distributive law
4023     and multiply that constant term separately.
4024     This helps produce common subexpressions.  */
4025
4026  if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4027      && ! TREE_CONSTANT (intop)
4028      && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4029      && TREE_CONSTANT (size_exp))
4030    {
4031      enum tree_code subcode = resultcode;
4032      if (TREE_CODE (intop) == MINUS_EXPR)
4033	subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4034      ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1), 1);
4035      intop = TREE_OPERAND (intop, 0);
4036    }
4037
4038  /* Convert the integer argument to a type the same size as sizetype
4039     so the multiply won't overflow spuriously.  */
4040
4041  if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4042    intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4043
4044  /* Replace the integer argument with a suitable product by the object size.
4045     Do this multiplication as signed, then convert to the appropriate
4046     pointer type (actually unsigned integral).  */
4047
4048  intop = cp_convert (result_type,
4049		      build_binary_op (MULT_EXPR, intop,
4050				       cp_convert (TREE_TYPE (intop),
4051						   size_exp),
4052				       1));
4053
4054  /* Create the sum or difference.  */
4055
4056  result = build (resultcode, result_type, ptrop, intop);
4057
4058  folded = fold (result);
4059  if (folded == result)
4060    TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4061  return folded;
4062}
4063
4064/* Return a tree for the difference of pointers OP0 and OP1.
4065   The resulting tree has type int.  */
4066
4067static tree
4068pointer_diff (op0, op1, ptrtype)
4069     register tree op0, op1;
4070     register tree ptrtype;
4071{
4072  register tree result, folded;
4073  tree restype = ptrdiff_type_node;
4074  tree target_type = TREE_TYPE (ptrtype);
4075
4076  if (!complete_type_or_else (target_type))
4077    return error_mark_node;
4078
4079  if (pedantic || warn_pointer_arith)
4080    {
4081      if (TREE_CODE (target_type) == VOID_TYPE)
4082	pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
4083      if (TREE_CODE (target_type) == FUNCTION_TYPE)
4084	pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
4085      if (TREE_CODE (target_type) == METHOD_TYPE)
4086	pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
4087      if (TREE_CODE (target_type) == OFFSET_TYPE)
4088	pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
4089    }
4090
4091  /* First do the subtraction as integers;
4092     then drop through to build the divide operator.  */
4093
4094  op0 = build_binary_op (MINUS_EXPR, cp_convert (restype, op0),
4095			 cp_convert (restype, op1), 1);
4096
4097  /* This generates an error if op1 is a pointer to an incomplete type.  */
4098  if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
4099    error ("arithmetic on pointer to an incomplete type");
4100
4101  op1 = ((TREE_CODE (target_type) == VOID_TYPE
4102	  || TREE_CODE (target_type) == FUNCTION_TYPE
4103	  || TREE_CODE (target_type) == METHOD_TYPE
4104	  || TREE_CODE (target_type) == OFFSET_TYPE)
4105	 ? integer_one_node
4106	 : size_in_bytes (target_type));
4107
4108  /* Do the division.  */
4109
4110  result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4111
4112  folded = fold (result);
4113  if (folded == result)
4114    TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4115  return folded;
4116}
4117
4118/* Handle the case of taking the address of a COMPONENT_REF.
4119   Called by `build_unary_op' and `build_up_reference'.
4120
4121   ARG is the COMPONENT_REF whose address we want.
4122   ARGTYPE is the pointer type that this address should have.
4123   MSG is an error message to print if this COMPONENT_REF is not
4124   addressable (such as a bitfield).  */
4125
4126tree
4127build_component_addr (arg, argtype, msg)
4128     tree arg, argtype;
4129     char *msg;
4130{
4131  tree field = TREE_OPERAND (arg, 1);
4132  tree basetype = decl_type_context (field);
4133  tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4134
4135  if (DECL_BIT_FIELD (field))
4136    {
4137      error (msg, IDENTIFIER_POINTER (DECL_NAME (field)));
4138      return error_mark_node;
4139    }
4140
4141  if (TREE_CODE (field) == FIELD_DECL
4142      && TYPE_USES_COMPLEX_INHERITANCE (basetype))
4143    {
4144      /* Can't convert directly to ARGTYPE, since that
4145	 may have the same pointer type as one of our
4146	 baseclasses.  */
4147      rval = build1 (NOP_EXPR, argtype,
4148		     convert_pointer_to (basetype, rval));
4149      TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4150    }
4151  else
4152    /* This conversion is harmless.  */
4153    rval = convert_force (argtype, rval, 0);
4154
4155  if (! integer_zerop (DECL_FIELD_BITPOS (field)))
4156    {
4157      tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
4158				size_int (BITS_PER_UNIT));
4159      int flag = TREE_CONSTANT (rval);
4160      offset = convert (sizetype, offset);
4161      rval = fold (build (PLUS_EXPR, argtype,
4162			  rval, cp_convert (argtype, offset)));
4163      TREE_CONSTANT (rval) = flag;
4164    }
4165  return rval;
4166}
4167
4168/* Construct and perhaps optimize a tree representation
4169   for a unary operation.  CODE, a tree_code, specifies the operation
4170   and XARG is the operand.  */
4171
4172tree
4173build_x_unary_op (code, xarg)
4174     enum tree_code code;
4175     tree xarg;
4176{
4177  if (processing_template_decl)
4178    return build_min_nt (code, xarg, NULL_TREE);
4179
4180  /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4181     error message.  */
4182  if (code == ADDR_EXPR
4183      && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4184      && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4185	   && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4186	  || (TREE_CODE (xarg) == OFFSET_REF)))
4187    /* don't look for a function */;
4188  else
4189    {
4190      tree rval;
4191
4192      rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4193			   NULL_TREE, NULL_TREE);
4194      if (rval || code != ADDR_EXPR)
4195	return rval;
4196    }
4197
4198  if (code == ADDR_EXPR)
4199    {
4200      if (TREE_CODE (xarg) == TARGET_EXPR)
4201	warning ("taking address of temporary");
4202    }
4203
4204  return build_unary_op (code, xarg, 0);
4205}
4206
4207/* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR.  */
4208
4209tree
4210condition_conversion (expr)
4211     tree expr;
4212{
4213  tree t;
4214  if (processing_template_decl)
4215    return expr;
4216  t = cp_convert (boolean_type_node, expr);
4217  t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4218  return t;
4219}
4220
4221/* C++: Must handle pointers to members.
4222
4223   Perhaps type instantiation should be extended to handle conversion
4224   from aggregates to types we don't yet know we want?  (Or are those
4225   cases typically errors which should be reported?)
4226
4227   NOCONVERT nonzero suppresses the default promotions
4228   (such as from short to int).  */
4229
4230tree
4231build_unary_op (code, xarg, noconvert)
4232     enum tree_code code;
4233     tree xarg;
4234     int noconvert;
4235{
4236  /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
4237  register tree arg = xarg;
4238  register tree argtype = 0;
4239  char *errstring = NULL;
4240  tree val;
4241
4242  if (arg == error_mark_node)
4243    return error_mark_node;
4244
4245  switch (code)
4246    {
4247    case CONVERT_EXPR:
4248      /* This is used for unary plus, because a CONVERT_EXPR
4249	 is enough to prevent anybody from looking inside for
4250	 associativity, but won't generate any code.  */
4251      if (!(arg = build_expr_type_conversion
4252	    (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4253	errstring = "wrong type argument to unary plus";
4254      else
4255	{
4256	  if (!noconvert)
4257	   arg = default_conversion (arg);
4258	  arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4259	  TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4260	}
4261      break;
4262
4263    case NEGATE_EXPR:
4264      if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4265	errstring = "wrong type argument to unary minus";
4266      else if (!noconvert)
4267	arg = default_conversion (arg);
4268      break;
4269
4270    case BIT_NOT_EXPR:
4271      if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4272	{
4273	  code = CONJ_EXPR;
4274	  if (!noconvert)
4275	    arg = default_conversion (arg);
4276	}
4277      else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4278						   arg, 1)))
4279	errstring = "wrong type argument to bit-complement";
4280      else if (!noconvert)
4281	arg = default_conversion (arg);
4282      break;
4283
4284    case ABS_EXPR:
4285      if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4286	errstring = "wrong type argument to abs";
4287      else if (!noconvert)
4288	arg = default_conversion (arg);
4289      break;
4290
4291    case CONJ_EXPR:
4292      /* Conjugating a real value is a no-op, but allow it anyway.  */
4293      if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4294	errstring = "wrong type argument to conjugation";
4295      else if (!noconvert)
4296	arg = default_conversion (arg);
4297      break;
4298
4299    case TRUTH_NOT_EXPR:
4300      arg = cp_convert (boolean_type_node, arg);
4301      val = invert_truthvalue (arg);
4302      if (arg != error_mark_node)
4303	return val;
4304      errstring = "in argument to unary !";
4305      break;
4306
4307    case NOP_EXPR:
4308      break;
4309
4310    case REALPART_EXPR:
4311      if (TREE_CODE (arg) == COMPLEX_CST)
4312	return TREE_REALPART (arg);
4313      else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4314	return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4315      else
4316	return arg;
4317
4318    case IMAGPART_EXPR:
4319      if (TREE_CODE (arg) == COMPLEX_CST)
4320	return TREE_IMAGPART (arg);
4321      else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4322	return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4323      else
4324	return cp_convert (TREE_TYPE (arg), integer_zero_node);
4325
4326    case PREINCREMENT_EXPR:
4327    case POSTINCREMENT_EXPR:
4328    case PREDECREMENT_EXPR:
4329    case POSTDECREMENT_EXPR:
4330      /* Handle complex lvalues (when permitted)
4331	 by reduction to simpler cases.  */
4332
4333      val = unary_complex_lvalue (code, arg);
4334      if (val != 0)
4335	return val;
4336
4337      /* Increment or decrement the real part of the value,
4338	 and don't change the imaginary part.  */
4339      if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4340	{
4341	  tree real, imag;
4342
4343	  arg = stabilize_reference (arg);
4344	  real = build_unary_op (REALPART_EXPR, arg, 1);
4345	  imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4346	  return build (COMPLEX_EXPR, TREE_TYPE (arg),
4347			build_unary_op (code, real, 1), imag);
4348	}
4349
4350      /* Report invalid types.  */
4351
4352      if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4353					      arg, 1)))
4354	{
4355	  if (code == PREINCREMENT_EXPR)
4356	    errstring ="no pre-increment operator for type";
4357	  else if (code == POSTINCREMENT_EXPR)
4358	    errstring ="no post-increment operator for type";
4359	  else if (code == PREDECREMENT_EXPR)
4360	    errstring ="no pre-decrement operator for type";
4361	  else
4362	    errstring ="no post-decrement operator for type";
4363	  break;
4364	}
4365
4366      /* Report something read-only.  */
4367
4368      if (TYPE_READONLY (TREE_TYPE (arg))
4369	  || TREE_READONLY (arg))
4370	readonly_error (arg, ((code == PREINCREMENT_EXPR
4371			       || code == POSTINCREMENT_EXPR)
4372			      ? "increment" : "decrement"),
4373			0);
4374
4375      {
4376	register tree inc;
4377	tree result_type = TREE_TYPE (arg);
4378
4379	arg = get_unwidened (arg, 0);
4380	argtype = TREE_TYPE (arg);
4381
4382	/* ARM $5.2.5 last annotation says this should be forbidden.  */
4383	if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4384	  pedwarn ("ANSI C++ forbids %sing an enum",
4385		   (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4386		   ? "increment" : "decrement");
4387
4388	/* Compute the increment.  */
4389
4390	if (TREE_CODE (argtype) == POINTER_TYPE)
4391	  {
4392	    enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4393	    if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4394	      cp_error ("cannot %s a pointer to incomplete type `%T'",
4395			((code == PREINCREMENT_EXPR
4396			  || code == POSTINCREMENT_EXPR)
4397			 ? "increment" : "decrement"), TREE_TYPE (argtype));
4398	    else if ((pedantic || warn_pointer_arith)
4399		     && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4400			 || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4401	      cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4402			  ((code == PREINCREMENT_EXPR
4403			    || code == POSTINCREMENT_EXPR)
4404			   ? "increment" : "decrement"), argtype);
4405	    inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4406	  }
4407	else
4408	  inc = integer_one_node;
4409
4410	inc = cp_convert (argtype, inc);
4411
4412	/* Handle incrementing a cast-expression.  */
4413
4414	switch (TREE_CODE (arg))
4415	  {
4416	  case NOP_EXPR:
4417	  case CONVERT_EXPR:
4418	  case FLOAT_EXPR:
4419	  case FIX_TRUNC_EXPR:
4420	  case FIX_FLOOR_EXPR:
4421	  case FIX_ROUND_EXPR:
4422	  case FIX_CEIL_EXPR:
4423	    {
4424	      tree incremented, modify, value, compound;
4425	      if (! lvalue_p (arg) && pedantic)
4426		pedwarn ("cast to non-reference type used as lvalue");
4427	      arg = stabilize_reference (arg);
4428	      if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4429		value = arg;
4430	      else
4431		value = save_expr (arg);
4432	      incremented = build (((code == PREINCREMENT_EXPR
4433				     || code == POSTINCREMENT_EXPR)
4434				    ? PLUS_EXPR : MINUS_EXPR),
4435				   argtype, value, inc);
4436	      TREE_SIDE_EFFECTS (incremented) = 1;
4437
4438	      modify = build_modify_expr (arg, NOP_EXPR, incremented);
4439	      compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4440
4441	      /* Eliminate warning about unused result of + or -.  */
4442	      TREE_NO_UNUSED_WARNING (compound) = 1;
4443	      return compound;
4444	    }
4445
4446	  default:
4447	    break;
4448	  }
4449
4450	/* Complain about anything else that is not a true lvalue.  */
4451	if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4452				    || code == POSTINCREMENT_EXPR)
4453				   ? "increment" : "decrement")))
4454	  return error_mark_node;
4455
4456	/* Forbid using -- on `bool'.  */
4457	if (TREE_TYPE (arg) == boolean_type_node)
4458	  {
4459	    if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4460	      {
4461		cp_error ("invalid use of `--' on bool variable `%D'", arg);
4462		return error_mark_node;
4463	      }
4464#if 0
4465	    /* This will only work if someone can convince Kenner to accept
4466	       my patch to expand_increment. (jason)  */
4467	    val = build (code, TREE_TYPE (arg), arg, inc);
4468#else
4469	    if (code == POSTINCREMENT_EXPR)
4470	      {
4471		arg = stabilize_reference (arg);
4472		val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4473			     boolean_true_node);
4474		TREE_SIDE_EFFECTS (val) = 1;
4475		arg = save_expr (arg);
4476		val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4477		val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4478	      }
4479	    else
4480	      val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4481			   boolean_true_node);
4482#endif
4483	  }
4484	else
4485	  val = build (code, TREE_TYPE (arg), arg, inc);
4486
4487	TREE_SIDE_EFFECTS (val) = 1;
4488	return cp_convert (result_type, val);
4489      }
4490
4491    case ADDR_EXPR:
4492      /* Note that this operation never does default_conversion
4493	 regardless of NOCONVERT.  */
4494
4495      argtype = TREE_TYPE (arg);
4496      if (TREE_CODE (argtype) == REFERENCE_TYPE)
4497	{
4498	  arg = build1
4499	    (CONVERT_EXPR,
4500	     build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4501	  TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4502	  return arg;
4503	}
4504      else if (pedantic && DECL_MAIN_P (arg))
4505	/* ARM $3.4 */
4506	pedwarn ("taking address of function `main'");
4507
4508      /* Let &* cancel out to simplify resulting code.  */
4509      if (TREE_CODE (arg) == INDIRECT_REF)
4510	{
4511	  /* We don't need to have `current_class_ptr' wrapped in a
4512	     NON_LVALUE_EXPR node.  */
4513	  if (arg == current_class_ref)
4514	    return current_class_ptr;
4515
4516	  arg = TREE_OPERAND (arg, 0);
4517	  if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4518	    {
4519	      arg = build1
4520		(CONVERT_EXPR,
4521		 build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4522	      TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4523	    }
4524	  else if (lvalue_p (arg))
4525	    /* Don't let this be an lvalue.  */
4526	    return non_lvalue (arg);
4527	  return arg;
4528	}
4529
4530      /* For &x[y], return x+y */
4531      if (TREE_CODE (arg) == ARRAY_REF)
4532	{
4533	  if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4534	    return error_mark_node;
4535	  return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4536				  TREE_OPERAND (arg, 1), 1);
4537	}
4538
4539      /* Uninstantiated types are all functions.  Taking the
4540	 address of a function is a no-op, so just return the
4541	 argument.  */
4542
4543      if (TREE_CODE (arg) == IDENTIFIER_NODE
4544	  && IDENTIFIER_OPNAME_P (arg))
4545	{
4546	  my_friendly_abort (117);
4547	  /* We don't know the type yet, so just work around the problem.
4548	     We know that this will resolve to an lvalue.  */
4549	  return build1 (ADDR_EXPR, unknown_type_node, arg);
4550	}
4551
4552      if (TREE_CODE (arg) == OVERLOAD
4553	  || (TREE_CODE (arg) == OFFSET_REF
4554	      && TREE_CODE (TREE_OPERAND (arg, 1)) == TEMPLATE_ID_EXPR))
4555	return build1 (ADDR_EXPR, unknown_type_node, arg);
4556      else if (TREE_CODE (arg) == TREE_LIST)
4557	{
4558	  if (TREE_CODE (TREE_VALUE (arg)) == FUNCTION_DECL)
4559	    /* Unique overloaded non-member function.  */
4560	    return build_unary_op (ADDR_EXPR, TREE_VALUE (arg), 0);
4561	  if (TREE_CHAIN (arg) == NULL_TREE
4562	      && TREE_CODE (TREE_VALUE (arg)) == TREE_LIST
4563	      && TREE_CODE (TREE_VALUE (TREE_VALUE (arg))) != OVERLOAD)
4564	    /* Unique overloaded member function.  */
4565	    return build_unary_op (ADDR_EXPR, TREE_VALUE (TREE_VALUE (arg)),
4566				   0);
4567	  return build1 (ADDR_EXPR, unknown_type_node, arg);
4568	}
4569      else if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
4570	{
4571	  tree targs;
4572	  tree fn;
4573
4574	  /* We don't require a match here; it's possible that the
4575	     context (like a cast to a particular type) will resolve
4576	     the particular choice of template.  */
4577	  fn = determine_specialization (arg,
4578					 NULL_TREE,
4579					 &targs,
4580					 0,
4581					 0);
4582
4583	  if (fn)
4584	    {
4585	      fn = instantiate_template (fn, targs);
4586	      mark_addressable (fn);
4587	      return build_unary_op (ADDR_EXPR, fn, 0);
4588	    }
4589
4590	  return build1 (ADDR_EXPR, unknown_type_node, arg);
4591	}
4592
4593      /* Handle complex lvalues (when permitted)
4594	 by reduction to simpler cases.  */
4595      val = unary_complex_lvalue (code, arg);
4596      if (val != 0)
4597	return val;
4598
4599      switch (TREE_CODE (arg))
4600	{
4601	case NOP_EXPR:
4602	case CONVERT_EXPR:
4603	case FLOAT_EXPR:
4604	case FIX_TRUNC_EXPR:
4605	case FIX_FLOOR_EXPR:
4606	case FIX_ROUND_EXPR:
4607	case FIX_CEIL_EXPR:
4608	  if (! lvalue_p (arg) && pedantic)
4609	    pedwarn ("taking the address of a cast to non-reference type");
4610	  break;
4611
4612	default:
4613	  break;
4614	}
4615
4616      /* Allow the address of a constructor if all the elements
4617	 are constant.  */
4618      if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4619	  && TREE_CONSTANT (arg))
4620	;
4621      /* Anything not already handled and not a true memory reference
4622	 is an error.  */
4623      else if (TREE_CODE (argtype) != FUNCTION_TYPE
4624	       && TREE_CODE (argtype) != METHOD_TYPE
4625	       && !lvalue_or_else (arg, "unary `&'"))
4626	return error_mark_node;
4627
4628      /* Ordinary case; arg is a COMPONENT_REF or a decl.  */
4629      /* If the lvalue is const or volatile,
4630	 merge that into the type that the address will point to.  */
4631      if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'd'
4632	  || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
4633	{
4634	  if (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4635	    argtype = cp_build_type_variant (argtype,
4636					    TREE_READONLY (arg),
4637					    TREE_THIS_VOLATILE (arg));
4638	}
4639
4640      argtype = build_pointer_type (argtype);
4641
4642      if (mark_addressable (arg) == 0)
4643	return error_mark_node;
4644
4645      {
4646	tree addr;
4647
4648	if (TREE_CODE (arg) == COMPONENT_REF)
4649	  addr = build_component_addr
4650	    (arg, argtype,
4651	     "attempt to take address of bit-field structure member `%s'");
4652	else
4653	  addr = build1 (code, argtype, arg);
4654
4655	/* Address of a static or external variable or
4656	   function counts as a constant */
4657	if (staticp (arg))
4658	  TREE_CONSTANT (addr) = 1;
4659
4660	if (TREE_CODE (argtype) == POINTER_TYPE
4661	    && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4662	  {
4663	    build_ptrmemfunc_type (argtype);
4664	    addr = build_ptrmemfunc (argtype, addr, 0);
4665	  }
4666
4667	return addr;
4668      }
4669
4670    default:
4671      break;
4672    }
4673
4674  if (!errstring)
4675    {
4676      if (argtype == 0)
4677	argtype = TREE_TYPE (arg);
4678      return fold (build1 (code, argtype, arg));
4679    }
4680
4681  error (errstring);
4682  return error_mark_node;
4683}
4684
4685#if 0
4686/* If CONVERSIONS is a conversion expression or a nested sequence of such,
4687   convert ARG with the same conversions in the same order
4688   and return the result.  */
4689
4690static tree
4691convert_sequence (conversions, arg)
4692     tree conversions;
4693     tree arg;
4694{
4695  switch (TREE_CODE (conversions))
4696    {
4697    case NOP_EXPR:
4698    case CONVERT_EXPR:
4699    case FLOAT_EXPR:
4700    case FIX_TRUNC_EXPR:
4701    case FIX_FLOOR_EXPR:
4702    case FIX_ROUND_EXPR:
4703    case FIX_CEIL_EXPR:
4704      return cp_convert (TREE_TYPE (conversions),
4705			 convert_sequence (TREE_OPERAND (conversions, 0),
4706					   arg));
4707
4708    default:
4709      return arg;
4710    }
4711}
4712#endif
4713
4714/* Apply unary lvalue-demanding operator CODE to the expression ARG
4715   for certain kinds of expressions which are not really lvalues
4716   but which we can accept as lvalues.
4717
4718   If ARG is not a kind of expression we can handle, return zero.  */
4719
4720tree
4721unary_complex_lvalue (code, arg)
4722     enum tree_code code;
4723     tree arg;
4724{
4725  /* Handle (a, b) used as an "lvalue".  */
4726  if (TREE_CODE (arg) == COMPOUND_EXPR)
4727    {
4728      tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4729      return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4730		    TREE_OPERAND (arg, 0), real_result);
4731    }
4732
4733  /* Handle (a ? b : c) used as an "lvalue".  */
4734  if (TREE_CODE (arg) == COND_EXPR
4735      || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4736    return rationalize_conditional_expr (code, arg);
4737
4738  if (TREE_CODE (arg) == MODIFY_EXPR
4739      || TREE_CODE (arg) == PREINCREMENT_EXPR
4740      || TREE_CODE (arg) == PREDECREMENT_EXPR)
4741    return unary_complex_lvalue
4742      (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4743		    arg, TREE_OPERAND (arg, 0)));
4744
4745  if (code != ADDR_EXPR)
4746    return 0;
4747
4748  /* Handle (a = b) used as an "lvalue" for `&'.  */
4749  if (TREE_CODE (arg) == MODIFY_EXPR
4750      || TREE_CODE (arg) == INIT_EXPR)
4751    {
4752      tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4753      arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4754      TREE_NO_UNUSED_WARNING (arg) = 1;
4755      return arg;
4756    }
4757
4758  if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4759      || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4760      || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4761    {
4762      /* The representation of something of type OFFSET_TYPE
4763	 is really the representation of a pointer to it.
4764	 Here give the representation its true type.  */
4765      tree t;
4766
4767      my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4768
4769      if (TREE_CODE (arg) != OFFSET_REF)
4770	return 0;
4771
4772      t = TREE_OPERAND (arg, 1);
4773
4774      /* Check all this code for right semantics.  */
4775      if (TREE_CODE (t) == FUNCTION_DECL)
4776	{
4777	  if (DECL_DESTRUCTOR_P (t))
4778	    cp_error ("taking address of destructor");
4779	  return build_unary_op (ADDR_EXPR, t, 0);
4780	}
4781      if (TREE_CODE (t) == VAR_DECL)
4782	return build_unary_op (ADDR_EXPR, t, 0);
4783      else
4784	{
4785	  tree type;
4786	  tree offset;
4787
4788	  if (TREE_OPERAND (arg, 0)
4789	      && (TREE_CODE (TREE_OPERAND (arg, 0)) != NOP_EXPR
4790		  || (TREE_OPERAND (TREE_OPERAND (arg, 0), 0)
4791		      != error_mark_node)))
4792	    if (TREE_CODE (t) != FIELD_DECL)
4793	      {
4794		/* Don't know if this should return address to just
4795		   _DECL, or actual address resolved in this expression.  */
4796		sorry ("address of bound pointer-to-member expression");
4797		return error_mark_node;
4798	      }
4799
4800	  /* Add in the offset to the field.  */
4801	  offset = convert (sizetype,
4802			    size_binop (EASY_DIV_EXPR,
4803					DECL_FIELD_BITPOS (t),
4804					size_int (BITS_PER_UNIT)));
4805
4806	  /* We offset all pointer to data members by 1 so that we can
4807	     distinguish between a null pointer to data member and the first
4808	     data member of a structure.  */
4809	  offset = size_binop (PLUS_EXPR, offset, size_int (1));
4810
4811	  type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4812	  type = build_pointer_type (type);
4813
4814	  return cp_convert (type, offset);
4815	}
4816    }
4817
4818
4819  /* We permit compiler to make function calls returning
4820     objects of aggregate type look like lvalues.  */
4821  {
4822    tree targ = arg;
4823
4824    if (TREE_CODE (targ) == SAVE_EXPR)
4825      targ = TREE_OPERAND (targ, 0);
4826
4827    if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4828      {
4829	if (TREE_CODE (arg) == SAVE_EXPR)
4830	  targ = arg;
4831	else
4832	  targ = build_cplus_new (TREE_TYPE (arg), arg);
4833	return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4834      }
4835
4836    if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4837      return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4838		     TREE_OPERAND (targ, 0), current_function_decl, NULL);
4839  }
4840
4841  /* Don't let anything else be handled specially.  */
4842  return 0;
4843}
4844
4845/* Mark EXP saying that we need to be able to take the
4846   address of it; it should not be allocated in a register.
4847   Value is 1 if successful.
4848
4849   C++: we do not allow `current_class_ptr' to be addressable.  */
4850
4851int
4852mark_addressable (exp)
4853     tree exp;
4854{
4855  register tree x = exp;
4856
4857  if (TREE_ADDRESSABLE (x) == 1)
4858    return 1;
4859
4860  while (1)
4861    switch (TREE_CODE (x))
4862      {
4863      case ADDR_EXPR:
4864      case COMPONENT_REF:
4865      case ARRAY_REF:
4866      case REALPART_EXPR:
4867      case IMAGPART_EXPR:
4868	x = TREE_OPERAND (x, 0);
4869	break;
4870
4871      case PARM_DECL:
4872	if (x == current_class_ptr)
4873	  {
4874	    if (! flag_this_is_variable)
4875	      error ("address of `this' not available");
4876	    TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
4877	    put_var_into_stack (x);
4878	    return 1;
4879	  }
4880      case VAR_DECL:
4881	if (TREE_STATIC (x) && TREE_READONLY (x)
4882	    && DECL_RTL (x) != 0
4883	    && ! DECL_IN_MEMORY_P (x))
4884	  {
4885	    /* We thought this would make a good constant variable,
4886	       but we were wrong.  */
4887	    push_obstacks_nochange ();
4888	    end_temporary_allocation ();
4889
4890	    TREE_ASM_WRITTEN (x) = 0;
4891	    DECL_RTL (x) = 0;
4892	    rest_of_decl_compilation (x, 0, IDENTIFIER_LOCAL_VALUE (x) == 0,
4893				      0);
4894	    TREE_ADDRESSABLE (x) = 1;
4895
4896	    pop_obstacks ();
4897
4898	    return 1;
4899	  }
4900	/* Caller should not be trying to mark initialized
4901	   constant fields addressable.  */
4902	my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
4903			    || DECL_IN_AGGR_P (x) == 0
4904			    || TREE_STATIC (x)
4905			    || DECL_EXTERNAL (x), 314);
4906
4907      case CONST_DECL:
4908      case RESULT_DECL:
4909	if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
4910	    && !DECL_ARTIFICIAL (x) && extra_warnings)
4911	  cp_warning ("address requested for `%D', which is declared `register'",
4912		      x);
4913	put_var_into_stack (x);
4914	TREE_ADDRESSABLE (x) = 1;
4915	return 1;
4916
4917      case FUNCTION_DECL:
4918	if (DECL_LANG_SPECIFIC (x) != 0)
4919	  {
4920	    x = DECL_MAIN_VARIANT (x);
4921	    /* We have to test both conditions here.  The first may be
4922	       non-zero in the case of processing a default function.  The
4923	       second may be non-zero in the case of a template function.  */
4924	    if (DECL_TEMPLATE_INFO (x) && !DECL_TEMPLATE_SPECIALIZATION (x))
4925	      mark_used (x);
4926	  }
4927	TREE_ADDRESSABLE (x) = 1;
4928	TREE_USED (x) = 1;
4929	TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
4930	return 1;
4931
4932      case CONSTRUCTOR:
4933	TREE_ADDRESSABLE (x) = 1;
4934	return 1;
4935
4936      case TARGET_EXPR:
4937	TREE_ADDRESSABLE (x) = 1;
4938	mark_addressable (TREE_OPERAND (x, 0));
4939	return 1;
4940
4941      default:
4942	return 1;
4943    }
4944}
4945
4946/* Build and return a conditional expression IFEXP ? OP1 : OP2.  */
4947
4948tree
4949build_x_conditional_expr (ifexp, op1, op2)
4950     tree ifexp, op1, op2;
4951{
4952  if (processing_template_decl)
4953    return build_min_nt (COND_EXPR, ifexp, op1, op2);
4954
4955  return build_new_op (COND_EXPR, LOOKUP_NORMAL, ifexp, op1, op2);
4956}
4957
4958tree
4959build_conditional_expr (ifexp, op1, op2)
4960     tree ifexp, op1, op2;
4961{
4962  register tree type1;
4963  register tree type2;
4964  register enum tree_code code1;
4965  register enum tree_code code2;
4966  register tree result_type = NULL_TREE;
4967
4968  /* If second operand is omitted, it is the same as the first one;
4969     make sure it is calculated only once.  */
4970  if (op1 == 0)
4971    {
4972      if (pedantic)
4973	pedwarn ("ANSI C++ forbids omitting the middle term of a ?: expression");
4974      ifexp = op1 = save_expr (ifexp);
4975    }
4976
4977  ifexp = cp_convert (boolean_type_node, ifexp);
4978
4979  if (TREE_CODE (ifexp) == ERROR_MARK)
4980    return error_mark_node;
4981
4982  op1 = require_instantiated_type (TREE_TYPE (op2), op1, error_mark_node);
4983  if (op1 == error_mark_node)
4984    return error_mark_node;
4985  op2 = require_instantiated_type (TREE_TYPE (op1), op2, error_mark_node);
4986  if (op2 == error_mark_node)
4987    return error_mark_node;
4988
4989  /* C++: REFERENCE_TYPES must be dereferenced.  */
4990  type1 = TREE_TYPE (op1);
4991  code1 = TREE_CODE (type1);
4992  type2 = TREE_TYPE (op2);
4993  code2 = TREE_CODE (type2);
4994
4995  if (code1 == REFERENCE_TYPE)
4996    {
4997      op1 = convert_from_reference (op1);
4998      type1 = TREE_TYPE (op1);
4999      code1 = TREE_CODE (type1);
5000    }
5001  if (code2 == REFERENCE_TYPE)
5002    {
5003      op2 = convert_from_reference (op2);
5004      type2 = TREE_TYPE (op2);
5005      code2 = TREE_CODE (type2);
5006    }
5007
5008  /* Don't promote the operands separately if they promote
5009     the same way.  Return the unpromoted type and let the combined
5010     value get promoted if necessary.  */
5011
5012  if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2)
5013      && code2 != ARRAY_TYPE
5014      && code2 != FUNCTION_TYPE
5015      && code2 != METHOD_TYPE)
5016    {
5017      tree result;
5018
5019      if (TREE_CONSTANT (ifexp)
5020	  && (TREE_CODE (ifexp) == INTEGER_CST
5021	      || TREE_CODE (ifexp) == ADDR_EXPR))
5022	return (integer_zerop (ifexp) ? op2 : op1);
5023
5024      if (TREE_CODE (op1) == CONST_DECL)
5025	op1 = DECL_INITIAL (op1);
5026      else if (TREE_READONLY_DECL_P (op1))
5027	op1 = decl_constant_value (op1);
5028      if (TREE_CODE (op2) == CONST_DECL)
5029	op2 = DECL_INITIAL (op2);
5030      else if (TREE_READONLY_DECL_P (op2))
5031	op2 = decl_constant_value (op2);
5032      if (type1 != type2)
5033	type1 = cp_build_type_variant
5034			(type1,
5035			 TREE_READONLY (op1) || TREE_READONLY (op2),
5036			 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
5037      /* ??? This is a kludge to deal with the fact that
5038	 we don't sort out integers and enums properly, yet.  */
5039      result = fold (build (COND_EXPR, type1, ifexp, op1, op2));
5040      if (TREE_TYPE (result) != type1)
5041	result = build1 (NOP_EXPR, type1, result);
5042      /* Expand both sides into the same slot,
5043	 hopefully the target of the ?: expression.  */
5044      if (TREE_CODE (op1) == TARGET_EXPR && TREE_CODE (op2) == TARGET_EXPR)
5045	{
5046	  tree slot = build (VAR_DECL, TREE_TYPE (result));
5047	  layout_decl (slot, 0);
5048	  result = build (TARGET_EXPR, TREE_TYPE (result),
5049			  slot, result, NULL_TREE, NULL_TREE);
5050	}
5051      return result;
5052    }
5053
5054  /* They don't match; promote them both and then try to reconcile them.
5055     But don't permit mismatching enum types.  */
5056  if (code1 == ENUMERAL_TYPE)
5057    {
5058      if (code2 == ENUMERAL_TYPE)
5059	{
5060	  cp_error ("enumeral mismatch in conditional expression: `%T' vs `%T'",
5061		    type1, type2);
5062	  return error_mark_node;
5063	}
5064      else if (extra_warnings && ! IS_AGGR_TYPE_CODE (code2)
5065	       && type2 != type_promotes_to (type1))
5066	warning ("enumeral and non-enumeral type in conditional expression");
5067    }
5068  else if (extra_warnings
5069	   && code2 == ENUMERAL_TYPE && ! IS_AGGR_TYPE_CODE (code1)
5070	   && type1 != type_promotes_to (type2))
5071    warning ("enumeral and non-enumeral type in conditional expression");
5072
5073  if (code1 != VOID_TYPE)
5074    {
5075      op1 = default_conversion (op1);
5076      type1 = TREE_TYPE (op1);
5077      if (TYPE_PTRMEMFUNC_P (type1))
5078	type1 = TYPE_PTRMEMFUNC_FN_TYPE (type1);
5079      code1 = TREE_CODE (type1);
5080    }
5081  if (code2 != VOID_TYPE)
5082    {
5083      op2 = default_conversion (op2);
5084      type2 = TREE_TYPE (op2);
5085      if (TYPE_PTRMEMFUNC_P (type2))
5086	type2 = TYPE_PTRMEMFUNC_FN_TYPE (type2);
5087      code2 = TREE_CODE (type2);
5088    }
5089
5090  if (code1 == RECORD_TYPE && code2 == RECORD_TYPE
5091      && real_lvalue_p (op1) && real_lvalue_p (op2)
5092      && comptypes (type1, type2, -1))
5093    {
5094      type1 = build_reference_type (type1);
5095      type2 = build_reference_type (type2);
5096      result_type = common_type (type1, type2);
5097      op1 = convert_to_reference (result_type, op1, CONV_IMPLICIT,
5098				  LOOKUP_NORMAL, NULL_TREE);
5099      op2 = convert_to_reference (result_type, op2, CONV_IMPLICIT,
5100				  LOOKUP_NORMAL, NULL_TREE);
5101    }
5102  /* Quickly detect the usual case where op1 and op2 have the same type
5103     after promotion.  */
5104  else if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
5105    {
5106      if (type1 == type2)
5107	result_type = type1;
5108      else
5109	result_type = cp_build_type_variant
5110			(type1,
5111			 TREE_READONLY (op1) || TREE_READONLY (op2),
5112			 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
5113    }
5114  else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE)
5115           && (code2 == INTEGER_TYPE || code2 == REAL_TYPE))
5116    {
5117      result_type = common_type (type1, type2);
5118    }
5119  else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5120    {
5121      if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
5122	pedwarn ("ANSI C++ forbids conditional expr with only one void side");
5123      result_type = void_type_node;
5124    }
5125  else if (code1 == POINTER_TYPE && null_ptr_cst_p (op2))
5126    result_type = qualify_type (type1, type2);
5127  else if (code2 == POINTER_TYPE && null_ptr_cst_p (op1))
5128    result_type = qualify_type (type2, type1);
5129  else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5130    {
5131      if (comp_target_types (type1, type2, 1))
5132	result_type = common_type (type1, type2);
5133      else if (TYPE_MAIN_VARIANT (TREE_TYPE (type1)) == void_type_node)
5134	{
5135	  if (pedantic && TREE_CODE (type2) == FUNCTION_TYPE)
5136	    pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5137	  result_type = qualify_type (type1, type2);
5138	}
5139      else if (TYPE_MAIN_VARIANT (TREE_TYPE (type2)) == void_type_node)
5140	{
5141	  if (pedantic && TREE_CODE (type1) == FUNCTION_TYPE)
5142	    pedwarn ("ANSI C++ forbids conditional expr between `void *' and function pointer");
5143	  result_type = qualify_type (type2, type1);
5144	}
5145      /* C++ */
5146      else if (comptypes (type2, type1, 0))
5147	result_type = type2;
5148      else if (IS_AGGR_TYPE (TREE_TYPE (type1))
5149	       && IS_AGGR_TYPE (TREE_TYPE (type2))
5150	       && (result_type = common_base_type (TREE_TYPE (type1),
5151						   TREE_TYPE (type2))))
5152	{
5153	  if (result_type == error_mark_node)
5154	    {
5155	      cp_error ("common base type of types `%T' and `%T' is ambiguous",
5156			TREE_TYPE (type1), TREE_TYPE (type2));
5157	      result_type = ptr_type_node;
5158	    }
5159	  else
5160	    {
5161	      if (pedantic
5162		  && result_type != TREE_TYPE (type1)
5163		  && result_type != TREE_TYPE (type2))
5164		cp_pedwarn ("`%T' and `%T' converted to `%T *' in conditional expression",
5165			    type1, type2, result_type);
5166
5167	      result_type = build_pointer_type (result_type);
5168	    }
5169	}
5170      else
5171	{
5172	  pedwarn ("pointer type mismatch in conditional expression");
5173	  result_type = ptr_type_node;
5174	}
5175    }
5176  else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5177    {
5178      pedwarn ("pointer/integer type mismatch in conditional expression");
5179      result_type = type1;
5180    }
5181  else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5182    {
5183      pedwarn ("pointer/integer type mismatch in conditional expression");
5184      result_type = type2;
5185    }
5186
5187  if (!result_type)
5188    {
5189      /* The match does not look good.  If either is
5190	 an aggregate value, try converting to a scalar type.  */
5191      if (code1 == RECORD_TYPE && code2 == RECORD_TYPE)
5192	{
5193	  cp_error ("aggregate mismatch in conditional expression: `%T' vs `%T'",
5194		    type1, type2);
5195	  return error_mark_node;
5196	}
5197      /* Warning: this code assumes that conversion between cv-variants of
5198         a type is done using NOP_EXPRs.  */
5199      if (code1 == RECORD_TYPE && TYPE_HAS_CONVERSION (type1))
5200	{
5201	  /* There are other types besides pointers and records.  */
5202	  tree tmp;
5203	  if (code2 == POINTER_TYPE)
5204	      tmp = build_pointer_type
5205		(build_type_variant (TREE_TYPE (type2), 1, 1));
5206	  else
5207	    tmp = type2;
5208	  tmp = build_type_conversion (CONVERT_EXPR, tmp, op1, 0);
5209	  if (tmp == NULL_TREE)
5210	    {
5211	      cp_error ("incompatible types `%T' and `%T' in `?:'",
5212			type1, type2);
5213	      return error_mark_node;
5214	    }
5215	  if (tmp == error_mark_node)
5216	    error ("ambiguous pointer conversion");
5217	  else
5218	    STRIP_NOPS (tmp);
5219	  result_type = common_type (type2, TREE_TYPE (tmp));
5220	  op1 = tmp;
5221	}
5222      else if (code2 == RECORD_TYPE && TYPE_HAS_CONVERSION (type2))
5223	{
5224	  tree tmp;
5225	  if (code1 == POINTER_TYPE)
5226	    tmp = build_pointer_type
5227	      (build_type_variant (TREE_TYPE (type1), 1, 1));
5228	  else
5229	    tmp = type1;
5230
5231	  tmp = build_type_conversion (CONVERT_EXPR, tmp, op2, 0);
5232	  if (tmp == NULL_TREE)
5233	    {
5234	      cp_error ("incompatible types `%T' and `%T' in `?:'",
5235			type1, type2);
5236	      return error_mark_node;
5237	    }
5238	  if (tmp == error_mark_node)
5239	    error ("ambiguous pointer conversion");
5240	  else
5241	    STRIP_NOPS (tmp);
5242	  result_type = common_type (type1, TREE_TYPE (tmp));
5243	  op2 = tmp;
5244	}
5245      else if (flag_cond_mismatch)
5246	result_type = void_type_node;
5247      else
5248	{
5249	  error ("type mismatch in conditional expression");
5250	  return error_mark_node;
5251	}
5252    }
5253
5254  if (TREE_CODE (result_type) == POINTER_TYPE
5255      && TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
5256    result_type = build_ptrmemfunc_type (result_type);
5257
5258  if (result_type != TREE_TYPE (op1))
5259    op1 = convert_for_initialization
5260      (NULL_TREE, result_type, op1, LOOKUP_NORMAL, "converting", NULL_TREE, 0);
5261  if (result_type != TREE_TYPE (op2))
5262    op2 = convert_for_initialization
5263      (NULL_TREE, result_type, op2, LOOKUP_NORMAL, "converting", NULL_TREE, 0);
5264
5265  if (TREE_CODE (ifexp) == INTEGER_CST)
5266    return integer_zerop (ifexp) ? op2 : op1;
5267
5268  return convert_from_reference
5269    (fold (build (COND_EXPR, result_type, ifexp, op1, op2)));
5270}
5271
5272/* Handle overloading of the ',' operator when needed.  Otherwise,
5273   this function just builds an expression list.  */
5274
5275tree
5276build_x_compound_expr (list)
5277     tree list;
5278{
5279  tree rest = TREE_CHAIN (list);
5280  tree result;
5281
5282  if (processing_template_decl)
5283    return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5284
5285  if (rest == NULL_TREE)
5286    return build_compound_expr (list);
5287
5288  result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5289			   TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5290  if (result)
5291    return build_x_compound_expr (expr_tree_cons (NULL_TREE, result,
5292						  TREE_CHAIN (rest)));
5293
5294  if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5295    {
5296      /* the left-hand operand of a comma expression is like an expression
5297         statement: we should warn if it doesn't have any side-effects,
5298         unless it was explicitly cast to (void).  */
5299      if ((extra_warnings || warn_unused)
5300           && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5301                && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5302        warning("left-hand operand of comma expression has no effect");
5303    }
5304#if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5305  else if (warn_unused)
5306    warn_if_unused_value (TREE_VALUE(list));
5307#endif
5308
5309  return build_compound_expr
5310    (expr_tree_cons (NULL_TREE, TREE_VALUE (list),
5311		     build_expr_list (NULL_TREE,
5312				      build_x_compound_expr (rest))));
5313}
5314
5315/* Given a list of expressions, return a compound expression
5316   that performs them all and returns the value of the last of them.  */
5317
5318tree
5319build_compound_expr (list)
5320     tree list;
5321{
5322  register tree rest;
5323
5324  if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5325    TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5326
5327  if (TREE_CHAIN (list) == 0)
5328    {
5329      /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5330	 Strip such NOP_EXPRs, since LIST is used in non-lvalue context.  */
5331      if (TREE_CODE (list) == NOP_EXPR
5332	  && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5333	list = TREE_OPERAND (list, 0);
5334
5335      /* Convert arrays to pointers.  */
5336      if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
5337	return default_conversion (TREE_VALUE (list));
5338      else
5339	return TREE_VALUE (list);
5340    }
5341
5342  rest = build_compound_expr (TREE_CHAIN (list));
5343
5344  /* When pedantic, a compound expression cannot be a constant expression.  */
5345  if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)) && ! pedantic)
5346    return rest;
5347
5348  return build (COMPOUND_EXPR, TREE_TYPE (rest),
5349		break_out_cleanups (TREE_VALUE (list)), rest);
5350}
5351
5352tree
5353build_static_cast (type, expr)
5354   tree type, expr;
5355{
5356  tree intype, binfo;
5357  int ok;
5358
5359  if (type == error_mark_node || expr == error_mark_node)
5360    return error_mark_node;
5361
5362  if (TREE_CODE (expr) == OFFSET_REF)
5363    expr = resolve_offset_ref (expr);
5364
5365  if (processing_template_decl)
5366    {
5367      tree t = build_min (STATIC_CAST_EXPR, copy_to_permanent (type),
5368			  expr);
5369      return t;
5370    }
5371
5372  /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5373     Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
5374  if (TREE_CODE (type) != REFERENCE_TYPE
5375      && TREE_CODE (expr) == NOP_EXPR
5376      && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5377    expr = TREE_OPERAND (expr, 0);
5378
5379  if (TREE_CODE (type) == VOID_TYPE)
5380    return build1 (CONVERT_EXPR, type, expr);
5381
5382  if (type_unknown_p (expr))
5383    {
5384      expr = instantiate_type (type, expr, 1);
5385      if (expr == error_mark_node)
5386	return error_mark_node;
5387    }
5388
5389  if (TREE_CODE (type) == REFERENCE_TYPE)
5390    return (convert_from_reference
5391	    (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5392				   LOOKUP_COMPLAIN, NULL_TREE)));
5393
5394  if (IS_AGGR_TYPE (type))
5395    return build_cplus_new
5396      (type, (build_method_call
5397	      (NULL_TREE, ctor_identifier, build_expr_list (NULL_TREE, expr),
5398	       TYPE_BINFO (type), LOOKUP_NORMAL)));
5399
5400  expr = decay_conversion (expr);
5401  intype = TREE_TYPE (expr);
5402
5403  /* FIXME handle casting to array type.  */
5404
5405  ok = 0;
5406  if (can_convert_arg (type, intype, expr))
5407    ok = 1;
5408  else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5409    {
5410      tree binfo;
5411      if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5412	  && (TYPE_READONLY (TREE_TYPE (type))
5413	      >= TYPE_READONLY (TREE_TYPE (intype)))
5414	  && (TYPE_VOLATILE (TREE_TYPE (type))
5415	      >= TYPE_VOLATILE (TREE_TYPE (intype)))
5416	  && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5417	  && ! TREE_VIA_VIRTUAL (binfo))
5418	ok = 1;
5419    }
5420  else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5421    {
5422      if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5423		     TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))), 1)
5424	  && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (type)))
5425	      >= TYPE_READONLY (TREE_TYPE (TREE_TYPE (intype))))
5426	  && (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (type)))
5427	      >= TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (intype))))
5428	  && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
5429				 TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)), 0))
5430	  && ! TREE_VIA_VIRTUAL (binfo))
5431	ok = 1;
5432    }
5433  else if (TREE_CODE (intype) != BOOLEAN_TYPE
5434	   && TREE_CODE (type) != ARRAY_TYPE
5435	   && TREE_CODE (type) != FUNCTION_TYPE
5436	   && can_convert (intype, type))
5437    ok = 1;
5438
5439  if (ok)
5440    return build_c_cast (type, expr);
5441
5442  cp_error ("static_cast from `%T' to `%T'", intype, type);
5443  return error_mark_node;
5444}
5445
5446tree
5447build_reinterpret_cast (type, expr)
5448   tree type, expr;
5449{
5450  tree intype;
5451
5452  if (type == error_mark_node || expr == error_mark_node)
5453    return error_mark_node;
5454
5455  if (TREE_CODE (expr) == OFFSET_REF)
5456    expr = resolve_offset_ref (expr);
5457
5458  if (processing_template_decl)
5459    {
5460      tree t = build_min (REINTERPRET_CAST_EXPR,
5461			  copy_to_permanent (type), expr);
5462      return t;
5463    }
5464
5465  if (TREE_CODE (type) != REFERENCE_TYPE)
5466    {
5467      expr = decay_conversion (expr);
5468
5469      /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5470	 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
5471      if (TREE_CODE (expr) == NOP_EXPR
5472	  && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5473	expr = TREE_OPERAND (expr, 0);
5474    }
5475
5476  if (type_unknown_p (expr))
5477    {
5478      expr = instantiate_type (type, expr, 1);
5479      if (expr == error_mark_node)
5480	return error_mark_node;
5481    }
5482
5483  intype = TREE_TYPE (expr);
5484
5485  if (TREE_CODE (type) == REFERENCE_TYPE)
5486    {
5487      if (! real_lvalue_p (expr))
5488	{
5489	  cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5490	  return error_mark_node;
5491	}
5492      expr = build_unary_op (ADDR_EXPR, expr, 0);
5493      if (expr != error_mark_node)
5494	expr = build_reinterpret_cast
5495	  (build_pointer_type (TREE_TYPE (type)), expr);
5496      if (expr != error_mark_node)
5497	expr = build_indirect_ref (expr, 0);
5498      return expr;
5499    }
5500  else if (comptypes (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type), 1))
5501    return build_static_cast (type, expr);
5502
5503  if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5504			    || TREE_CODE (intype) == ENUMERAL_TYPE))
5505    /* OK */;
5506  else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5507    {
5508      if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5509	cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5510		    intype, type);
5511    }
5512  else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5513	   || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5514    {
5515      if (TREE_READONLY_DECL_P (expr))
5516	expr = decl_constant_value (expr);
5517      return fold (build1 (NOP_EXPR, type, expr));
5518    }
5519  else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5520	   || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5521    {
5522      if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5523	cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5524		    intype, type);
5525
5526      if (TREE_READONLY_DECL_P (expr))
5527	expr = decl_constant_value (expr);
5528      return fold (build1 (NOP_EXPR, type, expr));
5529    }
5530  else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5531	   || (TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype)))
5532    {
5533      pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5534      if (TREE_READONLY_DECL_P (expr))
5535	expr = decl_constant_value (expr);
5536      return fold (build1 (NOP_EXPR, type, expr));
5537    }
5538  else
5539    {
5540      cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5541      return error_mark_node;
5542    }
5543
5544  return cp_convert (type, expr);
5545}
5546
5547tree
5548build_const_cast (type, expr)
5549   tree type, expr;
5550{
5551  tree intype;
5552
5553  if (type == error_mark_node || expr == error_mark_node)
5554    return error_mark_node;
5555
5556  if (TREE_CODE (expr) == OFFSET_REF)
5557    expr = resolve_offset_ref (expr);
5558
5559  if (processing_template_decl)
5560    {
5561      tree t = build_min (CONST_CAST_EXPR, copy_to_permanent (type),
5562			  expr);
5563      return t;
5564    }
5565
5566  if (TREE_CODE (type) != REFERENCE_TYPE)
5567    {
5568      expr = decay_conversion (expr);
5569
5570      /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5571	 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
5572      if (TREE_CODE (expr) == NOP_EXPR
5573	  && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5574	expr = TREE_OPERAND (expr, 0);
5575    }
5576
5577  if (type_unknown_p (expr))
5578    {
5579      expr = instantiate_type (type, expr, 1);
5580      if (expr == error_mark_node)
5581	return error_mark_node;
5582    }
5583
5584  intype = TREE_TYPE (expr);
5585
5586  if (comptypes (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type), 1))
5587    return build_static_cast (type, expr);
5588  else if (TREE_CODE (type) == REFERENCE_TYPE)
5589    {
5590      if (! real_lvalue_p (expr))
5591	{
5592	  cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5593	  return error_mark_node;
5594	}
5595
5596      if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5597	{
5598	  expr = build_unary_op (ADDR_EXPR, expr, 0);
5599	  expr = build1 (NOP_EXPR, type, expr);
5600	  return convert_from_reference (expr);
5601	}
5602    }
5603  else if (TREE_CODE (type) == POINTER_TYPE
5604	   && TREE_CODE (intype) == POINTER_TYPE
5605	   && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5606    return cp_convert (type, expr);
5607
5608  cp_error ("const_cast from `%T' to `%T'", intype, type);
5609  return error_mark_node;
5610}
5611
5612/* Build an expression representing a cast to type TYPE of expression EXPR.
5613
5614   ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5615   when doing the cast.  */
5616
5617tree
5618build_c_cast (type, expr)
5619     tree type, expr;
5620{
5621  register tree value = expr;
5622
5623  if (type == error_mark_node || expr == error_mark_node)
5624    return error_mark_node;
5625
5626  /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5627     Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
5628  if (TREE_CODE (type) != REFERENCE_TYPE
5629      && TREE_CODE (value) == NOP_EXPR
5630      && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5631    value = TREE_OPERAND (value, 0);
5632
5633  if (TREE_TYPE (expr)
5634      && TREE_CODE (TREE_TYPE (expr)) == OFFSET_TYPE
5635      && TREE_CODE (type) != OFFSET_TYPE)
5636    value = resolve_offset_ref (value);
5637
5638  if (TREE_CODE (type) == ARRAY_TYPE)
5639    {
5640      /* Allow casting from T1* to T2[] because Cfront allows it.
5641	 NIHCL uses it. It is not valid ANSI C however, and hence, not
5642	 valid ANSI C++.  */
5643      if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5644	{
5645	  if (pedantic)
5646	    pedwarn ("ANSI C++ forbids casting to an array type");
5647	  type = build_pointer_type (TREE_TYPE (type));
5648	}
5649      else
5650	{
5651	  error ("ANSI C++ forbids casting to an array type");
5652	  return error_mark_node;
5653	}
5654    }
5655
5656  if (TREE_CODE (type) == FUNCTION_TYPE
5657      || TREE_CODE (type) == METHOD_TYPE)
5658    {
5659      cp_error ("casting to function type `%T'", type);
5660      return error_mark_node;
5661    }
5662
5663  if (IS_SIGNATURE (type))
5664    {
5665      error ("cast specifies signature type");
5666      return error_mark_node;
5667    }
5668
5669  if (processing_template_decl)
5670    {
5671      tree t = build_min (CAST_EXPR, type,
5672			  min_tree_cons (NULL_TREE, value, NULL_TREE));
5673      return t;
5674    }
5675
5676  if (TREE_CODE (type) == VOID_TYPE)
5677    value = build1 (CONVERT_EXPR, type, value);
5678  else if (TREE_TYPE (value) == NULL_TREE
5679      || type_unknown_p (value))
5680    {
5681      value = instantiate_type (type, value, 1);
5682      /* Did we lose?  */
5683      if (value == error_mark_node)
5684	return error_mark_node;
5685    }
5686  else
5687    {
5688      tree otype;
5689
5690      /* Convert functions and arrays to pointers and
5691	 convert references to their expanded types,
5692	 but don't convert any other types.  If, however, we are
5693	 casting to a class type, there's no reason to do this: the
5694	 cast will only succeed if there is a converting constructor,
5695	 and the default conversions will be done at that point.  In
5696	 fact, doing the default conversion here is actually harmful
5697	 in cases like this:
5698
5699	     typedef int A[2];
5700             struct S { S(const A&); };
5701
5702         since we don't want the array-to-pointer conversion done.  */
5703      if (!IS_AGGR_TYPE (type))
5704	{
5705	  if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5706	      || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5707		  /* Don't do the default conversion if we want a
5708		     pointer to a function.  */
5709		  && ! (TREE_CODE (type) == POINTER_TYPE
5710			&& TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE))
5711	      || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5712	      || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5713	  value = default_conversion (value);
5714	}
5715      else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5716	/* However, even for class types, we still need to strip away
5717	   the reference type, since the call to convert_force below
5718	   does not expect the input expression to be of reference
5719	   type.  */
5720	value = convert_from_reference (value);
5721
5722      otype = TREE_TYPE (value);
5723
5724      /* Optionally warn about potentially worrisome casts.  */
5725
5726      if (warn_cast_qual
5727	  && TREE_CODE (type) == POINTER_TYPE
5728	  && TREE_CODE (otype) == POINTER_TYPE)
5729	{
5730	  /* For C++ we make these regular warnings, rather than
5731	     softening them into pedwarns.  */
5732	  if (TYPE_VOLATILE (TREE_TYPE (otype))
5733	      && ! TYPE_VOLATILE (TREE_TYPE (type)))
5734	    warning ("cast discards `volatile' from pointer target type");
5735	  if (TYPE_READONLY (TREE_TYPE (otype))
5736	      && ! TYPE_READONLY (TREE_TYPE (type)))
5737	    warning ("cast discards `const' from pointer target type");
5738	}
5739
5740      /* Warn about possible alignment problems.  */
5741      if (STRICT_ALIGNMENT && warn_cast_align
5742	  && TREE_CODE (type) == POINTER_TYPE
5743	  && TREE_CODE (otype) == POINTER_TYPE
5744	  && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5745	  && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5746	  && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5747	warning ("cast increases required alignment of target type");
5748
5749#if 0
5750      /* We should see about re-enabling these, they seem useful to
5751         me.  */
5752      if (TREE_CODE (type) == INTEGER_TYPE
5753	  && TREE_CODE (otype) == POINTER_TYPE
5754	  && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5755	warning ("cast from pointer to integer of different size");
5756
5757      if (TREE_CODE (type) == POINTER_TYPE
5758	  && TREE_CODE (otype) == INTEGER_TYPE
5759	  && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5760	  /* Don't warn about converting 0 to pointer,
5761	     provided the 0 was explicit--not cast or made by folding.  */
5762	  && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5763	warning ("cast to pointer from integer of different size");
5764#endif
5765
5766      if (TREE_CODE (type) == REFERENCE_TYPE)
5767	value = (convert_from_reference
5768		 (convert_to_reference (type, value, CONV_C_CAST,
5769					LOOKUP_COMPLAIN, NULL_TREE)));
5770      else
5771	{
5772	  tree ovalue;
5773
5774	  if (TREE_READONLY_DECL_P (value))
5775	    value = decl_constant_value (value);
5776
5777	  ovalue = value;
5778	  value = convert_force (type, value, CONV_C_CAST);
5779
5780	  /* Ignore any integer overflow caused by the cast.  */
5781	  if (TREE_CODE (value) == INTEGER_CST)
5782	    {
5783	      TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5784	      TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5785	    }
5786	}
5787    }
5788
5789    /* Always produce some operator for an explicit cast,
5790       so we can tell (for -pedantic) that the cast is no lvalue.  */
5791  if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5792      && real_lvalue_p (value))
5793    value = non_lvalue (value);
5794
5795  return value;
5796}
5797
5798/* Build an assignment expression of lvalue LHS from value RHS.
5799   MODIFYCODE is the code for a binary operator that we use
5800   to combine the old value of LHS with RHS to get the new value.
5801   Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5802
5803   C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.  */
5804
5805tree
5806build_modify_expr (lhs, modifycode, rhs)
5807     tree lhs;
5808     enum tree_code modifycode;
5809     tree rhs;
5810{
5811  register tree result;
5812  tree newrhs = rhs;
5813  tree lhstype = TREE_TYPE (lhs);
5814  tree olhstype = lhstype;
5815  tree olhs = lhs;
5816
5817  /* Avoid duplicate error messages from operands that had errors.  */
5818  if (lhs == error_mark_node || rhs == error_mark_node)
5819    return error_mark_node;
5820
5821  /* Types that aren't fully specified cannot be used in assignments.  */
5822  lhs = require_complete_type (lhs);
5823
5824  newrhs = rhs;
5825
5826  /* Handle assignment to signature pointers/refs.  */
5827
5828  if (TYPE_LANG_SPECIFIC (lhstype)
5829      && (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5830    {
5831      return build_signature_pointer_constructor (lhs, rhs);
5832    }
5833
5834  /* Handle control structure constructs used as "lvalues".  */
5835
5836  switch (TREE_CODE (lhs))
5837    {
5838      /* Handle --foo = 5; as these are valid constructs in C++ */
5839    case PREDECREMENT_EXPR:
5840    case PREINCREMENT_EXPR:
5841      if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5842	lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5843		     stabilize_reference (TREE_OPERAND (lhs, 0)),
5844		     TREE_OPERAND (lhs, 1));
5845      return build (COMPOUND_EXPR, lhstype,
5846		    lhs,
5847		    build_modify_expr (TREE_OPERAND (lhs, 0),
5848				       modifycode, rhs));
5849
5850      /* Handle (a, b) used as an "lvalue".  */
5851    case COMPOUND_EXPR:
5852      newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5853				  modifycode, rhs);
5854      if (newrhs == error_mark_node)
5855	return error_mark_node;
5856      return build (COMPOUND_EXPR, lhstype,
5857		    TREE_OPERAND (lhs, 0), newrhs);
5858
5859    case MODIFY_EXPR:
5860      newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5861      if (newrhs == error_mark_node)
5862	return error_mark_node;
5863      return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5864
5865      /* Handle (a ? b : c) used as an "lvalue".  */
5866    case COND_EXPR:
5867      rhs = save_expr (rhs);
5868      {
5869	/* Produce (a ? (b = rhs) : (c = rhs))
5870	   except that the RHS goes through a save-expr
5871	   so the code to compute it is only emitted once.  */
5872	tree cond
5873	  = build_conditional_expr (TREE_OPERAND (lhs, 0),
5874				    build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5875						       modifycode, rhs),
5876				    build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5877						       modifycode, rhs));
5878	if (cond == error_mark_node)
5879	  return cond;
5880	/* Make sure the code to compute the rhs comes out
5881	   before the split.  */
5882	return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5883		      /* Case to void to suppress warning
5884			 from warn_if_unused_value.  */
5885		      cp_convert (void_type_node, rhs), cond);
5886      }
5887
5888    default:
5889      break;
5890    }
5891
5892  if (TREE_CODE (lhs) == OFFSET_REF)
5893    {
5894      if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5895	{
5896	  /* Static class member?  */
5897	  tree member = TREE_OPERAND (lhs, 1);
5898	  if (TREE_CODE (member) == VAR_DECL)
5899	    lhs = member;
5900	  else
5901	    {
5902	      compiler_error ("invalid static class member");
5903	      return error_mark_node;
5904	    }
5905	}
5906      else
5907	lhs = resolve_offset_ref (lhs);
5908
5909      olhstype = lhstype = TREE_TYPE (lhs);
5910    }
5911
5912  if (TREE_CODE (lhstype) == REFERENCE_TYPE
5913      && modifycode != INIT_EXPR)
5914    {
5915      lhs = convert_from_reference (lhs);
5916      olhstype = lhstype = TREE_TYPE (lhs);
5917    }
5918
5919  /* If a binary op has been requested, combine the old LHS value with the RHS
5920     producing the value we should actually store into the LHS.  */
5921
5922  if (modifycode == INIT_EXPR)
5923    {
5924      if (! IS_AGGR_TYPE (lhstype))
5925	/* Do the default thing */;
5926      else
5927	{
5928	  result = build_method_call (lhs, ctor_identifier,
5929				      build_expr_list (NULL_TREE, rhs),
5930				      TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5931	  if (result == NULL_TREE)
5932	    return error_mark_node;
5933	  return result;
5934	}
5935    }
5936  else if (modifycode == NOP_EXPR)
5937    {
5938      /* `operator=' is not an inheritable operator.  */
5939      if (! IS_AGGR_TYPE (lhstype))
5940	/* Do the default thing */;
5941      else
5942	{
5943	  result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5944				   lhs, rhs, make_node (NOP_EXPR));
5945	  if (result == NULL_TREE)
5946	    return error_mark_node;
5947	  return result;
5948	}
5949      lhstype = olhstype;
5950    }
5951  else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5952    {
5953      my_friendly_abort (978652);
5954    }
5955  else
5956    {
5957      lhs = stabilize_reference (lhs);
5958      newrhs = build_binary_op (modifycode, lhs, rhs, 1);
5959      if (newrhs == error_mark_node)
5960	{
5961	  cp_error ("  in evaluation of `%Q(%#T, %#T)'", modifycode,
5962		    TREE_TYPE (lhs), TREE_TYPE (rhs));
5963	  return error_mark_node;
5964	}
5965    }
5966
5967  /* Handle a cast used as an "lvalue".
5968     We have already performed any binary operator using the value as cast.
5969     Now convert the result to the cast type of the lhs,
5970     and then true type of the lhs and store it there;
5971     then convert result back to the cast type to be the value
5972     of the assignment.  */
5973
5974  switch (TREE_CODE (lhs))
5975    {
5976    case NOP_EXPR:
5977    case CONVERT_EXPR:
5978    case FLOAT_EXPR:
5979    case FIX_TRUNC_EXPR:
5980    case FIX_FLOOR_EXPR:
5981    case FIX_ROUND_EXPR:
5982    case FIX_CEIL_EXPR:
5983      if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5984	  || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5985	  || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5986	  || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5987	newrhs = default_conversion (newrhs);
5988      {
5989	tree inner_lhs = TREE_OPERAND (lhs, 0);
5990	tree result;
5991
5992	/* WP 5.4.1:  The result is an lvalue if T is a reference type,
5993	   otherwise the result is an rvalue.   */
5994	if (! lvalue_p (lhs))
5995	  pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5996
5997	result = build_modify_expr (inner_lhs, NOP_EXPR,
5998				    cp_convert (TREE_TYPE (inner_lhs),
5999						cp_convert (lhstype, newrhs)));
6000	if (result == error_mark_node)
6001	  return result;
6002	return cp_convert (TREE_TYPE (lhs), result);
6003      }
6004
6005    default:
6006      break;
6007    }
6008
6009  /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
6010     Reject anything strange now.  */
6011
6012  if (!lvalue_or_else (lhs, "assignment"))
6013    return error_mark_node;
6014
6015  GNU_xref_assign (lhs);
6016
6017  /* Warn about storing in something that is `const'.  */
6018  /* For C++, don't warn if this is initialization.  */
6019  if (modifycode != INIT_EXPR
6020      /* For assignment to `const' signature pointer/reference fields,
6021	 don't warn either, we already printed a better message before.  */
6022      && ! (TREE_CODE (lhs) == COMPONENT_REF
6023	    && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
6024		|| IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
6025      && (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
6026	  || ((TREE_CODE (lhstype) == RECORD_TYPE
6027	       || TREE_CODE (lhstype) == UNION_TYPE)
6028	      && C_TYPE_FIELDS_READONLY (lhstype))
6029	  || (TREE_CODE (lhstype) == REFERENCE_TYPE
6030	      && TYPE_READONLY (TREE_TYPE (lhstype)))))
6031    readonly_error (lhs, "assignment", 0);
6032
6033  /* If storing into a structure or union member,
6034     it has probably been given type `int'.
6035     Compute the type that would go with
6036     the actual amount of storage the member occupies.  */
6037
6038  if (TREE_CODE (lhs) == COMPONENT_REF
6039      && (TREE_CODE (lhstype) == INTEGER_TYPE
6040	  || TREE_CODE (lhstype) == REAL_TYPE
6041	  || TREE_CODE (lhstype) == ENUMERAL_TYPE))
6042    {
6043      lhstype = TREE_TYPE (get_unwidened (lhs, 0));
6044
6045      /* If storing in a field that is in actuality a short or narrower
6046	 than one, we must store in the field in its actual type.  */
6047
6048      if (lhstype != TREE_TYPE (lhs))
6049	{
6050	  lhs = copy_node (lhs);
6051	  TREE_TYPE (lhs) = lhstype;
6052	}
6053    }
6054
6055  /* check to see if there is an assignment to `this' */
6056  if (lhs == current_class_ptr)
6057    {
6058      if (flag_this_is_variable > 0
6059	  && DECL_NAME (current_function_decl) != NULL_TREE
6060	  && (DECL_NAME (current_function_decl)
6061	      != constructor_name (current_class_type)))
6062	warning ("assignment to `this' not in constructor or destructor");
6063      current_function_just_assigned_this = 1;
6064    }
6065
6066  /* The TREE_TYPE of RHS may be TYPE_UNKNOWN.  This can happen
6067     when the type of RHS is not yet known, i.e. its type
6068     is inherited from LHS.  */
6069  rhs = require_instantiated_type (lhstype, newrhs, error_mark_node);
6070  if (rhs == error_mark_node)
6071    return error_mark_node;
6072  newrhs = rhs;
6073
6074  if (modifycode != INIT_EXPR)
6075    {
6076      /* Make modifycode now either a NOP_EXPR or an INIT_EXPR.  */
6077      modifycode = NOP_EXPR;
6078      /* Reference-bashing */
6079      if (TREE_CODE (lhstype) == REFERENCE_TYPE)
6080	{
6081	  tree tmp = convert_from_reference (lhs);
6082	  lhstype = TREE_TYPE (tmp);
6083	  if (TYPE_SIZE (lhstype) == 0)
6084	    {
6085	      incomplete_type_error (lhs, lhstype);
6086	      return error_mark_node;
6087	    }
6088	  lhs = tmp;
6089	  olhstype = lhstype;
6090	}
6091      if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
6092	{
6093	  tree tmp = convert_from_reference (newrhs);
6094	  if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
6095	    {
6096	      incomplete_type_error (newrhs, TREE_TYPE (tmp));
6097	      return error_mark_node;
6098	    }
6099	  newrhs = tmp;
6100	}
6101    }
6102
6103  if (TREE_SIDE_EFFECTS (lhs))
6104    lhs = stabilize_reference (lhs);
6105  if (TREE_SIDE_EFFECTS (newrhs))
6106    newrhs = stabilize_reference (newrhs);
6107
6108  /* Convert new value to destination type.  */
6109
6110  if (TREE_CODE (lhstype) == ARRAY_TYPE)
6111    {
6112      int from_array;
6113
6114      if (! comptypes (lhstype, TREE_TYPE (rhs), 0))
6115	{
6116	  cp_error ("incompatible types in assignment of `%T' to `%T'",
6117		    TREE_TYPE (rhs), lhstype);
6118	  return error_mark_node;
6119	}
6120
6121      /* Allow array assignment in compiler-generated code.  */
6122      if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
6123	pedwarn ("ANSI C++ forbids assignment of arrays");
6124
6125      /* Have to wrap this in RTL_EXPR for two cases:
6126	 in base or member initialization and if we
6127	 are a branch of a ?: operator.  Since we
6128	 can't easily know the latter, just do it always.  */
6129
6130      result = make_node (RTL_EXPR);
6131
6132      TREE_TYPE (result) = void_type_node;
6133      do_pending_stack_adjust ();
6134      start_sequence_for_rtl_expr (result);
6135
6136      /* As a matter of principle, `start_sequence' should do this.  */
6137      emit_note (0, -1);
6138
6139      from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
6140	           ? 1 + (modifycode != INIT_EXPR): 0;
6141      expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
6142		       from_array);
6143
6144      do_pending_stack_adjust ();
6145
6146      TREE_SIDE_EFFECTS (result) = 1;
6147      RTL_EXPR_SEQUENCE (result) = get_insns ();
6148      RTL_EXPR_RTL (result) = const0_rtx;
6149      end_sequence ();
6150      return result;
6151    }
6152
6153  if (modifycode == INIT_EXPR)
6154    {
6155      newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
6156					   "assignment", NULL_TREE, 0);
6157      if (lhs == DECL_RESULT (current_function_decl))
6158	{
6159	  if (DECL_INITIAL (lhs))
6160	    warning ("return value from function receives multiple initializations");
6161	  DECL_INITIAL (lhs) = newrhs;
6162	}
6163    }
6164  else
6165    {
6166      /* Avoid warnings on enum bit fields.  */
6167      if (TREE_CODE (olhstype) == ENUMERAL_TYPE
6168	  && TREE_CODE (lhstype) == INTEGER_TYPE)
6169	{
6170	  newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
6171					   NULL_TREE, 0);
6172	  newrhs = convert_force (lhstype, newrhs, 0);
6173	}
6174      else
6175	newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
6176					 NULL_TREE, 0);
6177      if (TREE_CODE (newrhs) == CALL_EXPR
6178	  && TYPE_NEEDS_CONSTRUCTING (lhstype))
6179	newrhs = build_cplus_new (lhstype, newrhs);
6180
6181      /* Can't initialize directly from a TARGET_EXPR, since that would
6182	 cause the lhs to be constructed twice, and possibly result in
6183	 accidental self-initialization.  So we force the TARGET_EXPR to be
6184	 expanded without a target.  */
6185      if (TREE_CODE (newrhs) == TARGET_EXPR)
6186	newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
6187			TREE_OPERAND (newrhs, 0));
6188    }
6189
6190  if (newrhs == error_mark_node)
6191    return error_mark_node;
6192
6193  if (TREE_CODE (newrhs) == COND_EXPR)
6194    {
6195      tree lhs1;
6196      tree cond = TREE_OPERAND (newrhs, 0);
6197
6198      if (TREE_SIDE_EFFECTS (lhs))
6199	cond = build_compound_expr (tree_cons
6200				    (NULL_TREE, lhs,
6201				     build_expr_list (NULL_TREE, cond)));
6202
6203      /* Cannot have two identical lhs on this one tree (result) as preexpand
6204	 calls will rip them out and fill in RTL for them, but when the
6205	 rtl is generated, the calls will only be in the first side of the
6206	 condition, not on both, or before the conditional jump! (mrs) */
6207      lhs1 = break_out_calls (lhs);
6208
6209      if (lhs == lhs1)
6210	/* If there's no change, the COND_EXPR behaves like any other rhs.  */
6211	result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6212			lhstype, lhs, newrhs);
6213      else
6214	{
6215	  tree result_type = TREE_TYPE (newrhs);
6216	  /* We have to convert each arm to the proper type because the
6217	     types may have been munged by constant folding.  */
6218	  result
6219	    = build (COND_EXPR, result_type, cond,
6220		     build_modify_expr (lhs, modifycode,
6221					cp_convert (result_type,
6222						    TREE_OPERAND (newrhs, 1))),
6223		     build_modify_expr (lhs1, modifycode,
6224					cp_convert (result_type,
6225						    TREE_OPERAND (newrhs, 2))));
6226	}
6227    }
6228  else
6229    result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6230		    lhstype, lhs, newrhs);
6231
6232  TREE_SIDE_EFFECTS (result) = 1;
6233
6234  /* If we got the LHS in a different type for storing in,
6235     convert the result back to the nominal type of LHS
6236     so that the value we return always has the same type
6237     as the LHS argument.  */
6238
6239  if (olhstype == TREE_TYPE (result))
6240    return result;
6241  /* Avoid warnings converting integral types back into enums
6242     for enum bit fields.  */
6243  if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
6244      && TREE_CODE (olhstype) == ENUMERAL_TYPE)
6245    {
6246      result = build (COMPOUND_EXPR, olhstype, result, olhs);
6247      TREE_NO_UNUSED_WARNING (result) = 1;
6248      return result;
6249    }
6250  return convert_for_assignment (olhstype, result, "assignment",
6251				 NULL_TREE, 0);
6252}
6253
6254tree
6255build_x_modify_expr (lhs, modifycode, rhs)
6256     tree lhs;
6257     enum tree_code modifycode;
6258     tree rhs;
6259{
6260  if (processing_template_decl)
6261    return build_min_nt (MODOP_EXPR, lhs,
6262			 build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6263
6264  if (modifycode != NOP_EXPR)
6265    {
6266      tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6267				  make_node (modifycode));
6268      if (rval)
6269	return rval;
6270    }
6271  return build_modify_expr (lhs, modifycode, rhs);
6272}
6273
6274/* Return 0 if EXP is not a valid lvalue in this language
6275   even though `lvalue_or_else' would accept it.  */
6276
6277int
6278language_lvalue_valid (exp)
6279     tree exp ATTRIBUTE_UNUSED;
6280{
6281  return 1;
6282}
6283
6284/* Get difference in deltas for different pointer to member function
6285   types.  Return integer_zero_node, if FROM cannot be converted to a
6286   TO type.  If FORCE is true, then allow reverse conversions as well.  */
6287
6288static tree
6289get_delta_difference (from, to, force)
6290     tree from, to;
6291     int force;
6292{
6293  tree delta = integer_zero_node;
6294  tree binfo;
6295
6296  if (to == from)
6297    return delta;
6298
6299  /* Should get_base_distance here, so we can check if any thing along the
6300     path is virtual, and we need to make sure we stay
6301     inside the real binfos when going through virtual bases.
6302     Maybe we should replace virtual bases with
6303     binfo_member (...CLASSTYPE_VBASECLASSES...)...  (mrs) */
6304  binfo = get_binfo (from, to, 1);
6305  if (binfo == error_mark_node)
6306    {
6307      error ("   in pointer to member function conversion");
6308      return delta;
6309    }
6310  if (binfo == 0)
6311    {
6312      if (!force)
6313	{
6314	  error_not_base_type (from, to);
6315	  error ("   in pointer to member conversion");
6316	  return delta;
6317	}
6318      binfo = get_binfo (to, from, 1);
6319      if (binfo == 0 || binfo == error_mark_node)
6320	return delta;
6321      if (TREE_VIA_VIRTUAL (binfo))
6322	{
6323	  binfo = binfo_member (BINFO_TYPE (binfo),
6324				CLASSTYPE_VBASECLASSES (from));
6325	  cp_warning ("pointer to member cast to virtual base `%T'",
6326		      BINFO_TYPE (binfo));
6327	  warning ("  will only work if you are very careful");
6328	}
6329      delta = BINFO_OFFSET (binfo);
6330      delta = cp_convert (ptrdiff_type_node, delta);
6331
6332      return build_binary_op (MINUS_EXPR,
6333			      integer_zero_node,
6334			      delta, 1);
6335    }
6336
6337  if (TREE_VIA_VIRTUAL (binfo))
6338    {
6339      if (force)
6340	{
6341	  cp_warning ("pointer to member cast from virtual base `%T'",
6342		      BINFO_TYPE (binfo));
6343	  warning ("  will only work if you are very careful");
6344	}
6345      else
6346	cp_error ("pointer to member conversion from virtual base `%T'",
6347		  BINFO_TYPE (binfo));
6348    }
6349
6350  return BINFO_OFFSET (binfo);
6351}
6352
6353static tree
6354build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6355     tree type, delta, idx, pfn, delta2;
6356{
6357  tree u;
6358
6359#if 0
6360  /* This is the old way we did it.  We want to avoid calling
6361     digest_init, so that it can give an error if we use { } when
6362     initializing a pointer to member function.  */
6363
6364  if (pfn)
6365    {
6366      u = build_nt (CONSTRUCTOR, NULL_TREE,
6367		    expr_tree_cons (pfn_identifier, pfn, NULL_TREE));
6368    }
6369  else
6370    {
6371      u = build_nt (CONSTRUCTOR, NULL_TREE,
6372		    expr_tree_cons (delta2_identifier, delta2, NULL_TREE));
6373    }
6374
6375  u = build_nt (CONSTRUCTOR, NULL_TREE,
6376		expr_tree_cons (NULL_TREE, delta,
6377			   expr_tree_cons (NULL_TREE, idx,
6378				      expr_tree_cons (NULL_TREE, u, NULL_TREE))));
6379
6380  return digest_init (type, u, (tree*)0);
6381#else
6382  tree delta_field, idx_field, pfn_or_delta2_field, pfn_field, delta2_field;
6383  tree subtype;
6384  int allconstant, allsimple;
6385
6386  delta_field = TYPE_FIELDS (type);
6387  idx_field = TREE_CHAIN (delta_field);
6388  pfn_or_delta2_field = TREE_CHAIN (idx_field);
6389  subtype = TREE_TYPE (pfn_or_delta2_field);
6390  pfn_field = TYPE_FIELDS (subtype);
6391  delta2_field = TREE_CHAIN (pfn_field);
6392
6393  if (pfn)
6394    {
6395      allconstant = TREE_CONSTANT (pfn);
6396      allsimple = !! initializer_constant_valid_p (pfn, TREE_TYPE (pfn));
6397      u = expr_tree_cons (pfn_field, pfn, NULL_TREE);
6398    }
6399  else
6400    {
6401      delta2 = convert_and_check (delta_type_node, delta2);
6402      allconstant = TREE_CONSTANT (delta2);
6403      allsimple = !! initializer_constant_valid_p (delta2, TREE_TYPE (delta2));
6404      u = expr_tree_cons (delta2_field, delta2, NULL_TREE);
6405    }
6406
6407  delta = convert_and_check (delta_type_node, delta);
6408  idx = convert_and_check (delta_type_node, idx);
6409
6410  allconstant = allconstant && TREE_CONSTANT (delta) && TREE_CONSTANT (idx);
6411  allsimple = allsimple
6412    && initializer_constant_valid_p (delta, TREE_TYPE (delta))
6413      && initializer_constant_valid_p (idx, TREE_TYPE (idx));
6414
6415  u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6416  u = expr_tree_cons (delta_field, delta,
6417		 expr_tree_cons (idx_field, idx,
6418			    expr_tree_cons (pfn_or_delta2_field, u, NULL_TREE)));
6419  u = build (CONSTRUCTOR, type, NULL_TREE, u);
6420  TREE_CONSTANT (u) = allconstant;
6421  TREE_STATIC (u) = allconstant && allsimple;
6422  return u;
6423#endif
6424}
6425
6426/* Build a constructor for a pointer to member function.  It can be
6427   used to initialize global variables, local variable, or used
6428   as a value in expressions.  TYPE is the POINTER to METHOD_TYPE we
6429   want to be.
6430
6431   If FORCE is non-zero, then force this conversion, even if
6432   we would rather not do it.  Usually set when using an explicit
6433   cast.
6434
6435   Return error_mark_node, if something goes wrong.  */
6436
6437tree
6438build_ptrmemfunc (type, pfn, force)
6439     tree type, pfn;
6440     int force;
6441{
6442  tree idx = integer_zero_node;
6443  tree delta = integer_zero_node;
6444  tree delta2 = integer_zero_node;
6445  tree vfield_offset;
6446  tree npfn = NULL_TREE;
6447
6448  /* Handle multiple conversions of pointer to member functions.  */
6449  if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6450    {
6451      tree ndelta, ndelta2;
6452      tree e1, e2, e3, n;
6453      tree pfn_type;
6454
6455      /* Is is already the right type? */
6456      if (type == TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn)))
6457	return pfn;
6458
6459      pfn_type = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (pfn));
6460      if (!force
6461	  && comp_target_types (type, pfn_type, 0) != 1)
6462	cp_error ("conversion to `%T' from `%T'", type, pfn_type);
6463
6464      ndelta = cp_convert (ptrdiff_type_node, build_component_ref (pfn, delta_identifier, NULL_TREE, 0));
6465      ndelta2 = cp_convert (ptrdiff_type_node, DELTA2_FROM_PTRMEMFUNC (pfn));
6466      idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6467
6468      n = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (pfn_type)),
6469				TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6470				force);
6471
6472      delta = build_binary_op (PLUS_EXPR, ndelta, n, 1);
6473      delta2 = build_binary_op (PLUS_EXPR, ndelta2, n, 1);
6474      e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6475
6476      e2 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx,
6477			      NULL_TREE, delta2);
6478
6479      pfn = PFN_FROM_PTRMEMFUNC (pfn);
6480      npfn = build1 (NOP_EXPR, type, pfn);
6481      TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6482
6483      e3 = build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx, npfn,
6484			      NULL_TREE);
6485      return build_conditional_expr (e1, e2, e3);
6486    }
6487
6488  /* Handle null pointer to member function conversions.  */
6489  if (integer_zerop (pfn))
6490    {
6491      pfn = build_c_cast (type, integer_zero_node);
6492      return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type),
6493				integer_zero_node, integer_zero_node,
6494				pfn, NULL_TREE);
6495    }
6496
6497  if (TREE_CODE (pfn) == TREE_LIST
6498      || (TREE_CODE (pfn) == ADDR_EXPR
6499	  && TREE_CODE (TREE_OPERAND (pfn, 0)) == TREE_LIST))
6500    return instantiate_type (type, pfn, 1);
6501
6502  if (!force
6503      && comp_target_types (type, TREE_TYPE (pfn), 0) != 1)
6504    cp_error ("conversion to `%T' from `%T'", type, TREE_TYPE (pfn));
6505
6506  /* Allow pointer to member conversions here.  */
6507  delta = get_delta_difference (TYPE_METHOD_BASETYPE (TREE_TYPE (TREE_TYPE (pfn))),
6508				TYPE_METHOD_BASETYPE (TREE_TYPE (type)),
6509				force);
6510  delta2 = build_binary_op (PLUS_EXPR, delta2, delta, 1);
6511
6512  if (TREE_CODE (TREE_OPERAND (pfn, 0)) != FUNCTION_DECL)
6513    warning ("assuming pointer to member function is non-virtual");
6514
6515  if (TREE_CODE (TREE_OPERAND (pfn, 0)) == FUNCTION_DECL
6516      && DECL_VINDEX (TREE_OPERAND (pfn, 0)))
6517    {
6518      /* Find the offset to the vfield pointer in the object.  */
6519      vfield_offset = get_binfo (DECL_CONTEXT (TREE_OPERAND (pfn, 0)),
6520				 DECL_CLASS_CONTEXT (TREE_OPERAND (pfn, 0)),
6521				 0);
6522      vfield_offset = get_vfield_offset (vfield_offset);
6523      delta2 = size_binop (PLUS_EXPR, vfield_offset, delta2);
6524
6525      /* Map everything down one to make room for the null pointer to member.  */
6526      idx = size_binop (PLUS_EXPR,
6527			DECL_VINDEX (TREE_OPERAND (pfn, 0)),
6528			integer_one_node);
6529    }
6530  else
6531    {
6532      idx = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6533
6534      if (type == TREE_TYPE (pfn))
6535	{
6536	  npfn = pfn;
6537	}
6538      else
6539	{
6540	  npfn = build1 (NOP_EXPR, type, pfn);
6541	  TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6542	}
6543    }
6544
6545  return build_ptrmemfunc1 (TYPE_GET_PTRMEMFUNC_TYPE (type), delta, idx, npfn, delta2);
6546}
6547
6548/* Convert value RHS to type TYPE as preparation for an assignment
6549   to an lvalue of type TYPE.
6550   The real work of conversion is done by `convert'.
6551   The purpose of this function is to generate error messages
6552   for assignments that are not allowed in C.
6553   ERRTYPE is a string to use in error messages:
6554   "assignment", "return", etc.
6555
6556   C++: attempts to allow `convert' to find conversions involving
6557   implicit type conversion between aggregate and scalar types
6558   as per 8.5.6 of C++ manual.  Does not randomly dereference
6559   pointers to aggregates!  */
6560
6561static tree
6562convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6563     tree type, rhs;
6564     char *errtype;
6565     tree fndecl;
6566     int parmnum;
6567{
6568  register enum tree_code codel = TREE_CODE (type);
6569  register tree rhstype;
6570  register enum tree_code coder = TREE_CODE (TREE_TYPE (rhs));
6571
6572  if (coder == UNKNOWN_TYPE)
6573    rhs = instantiate_type (type, rhs, 1);
6574
6575  if (coder == ERROR_MARK)
6576    return error_mark_node;
6577
6578  if (codel == OFFSET_TYPE)
6579    {
6580      type = TREE_TYPE (type);
6581      codel = TREE_CODE (type);
6582    }
6583
6584  /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
6585  if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6586    rhs = TREE_OPERAND (rhs, 0);
6587
6588  if (rhs == error_mark_node)
6589    return error_mark_node;
6590
6591  if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6592    return error_mark_node;
6593
6594  if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
6595    {
6596      rhs = resolve_offset_ref (rhs);
6597      if (rhs == error_mark_node)
6598	return error_mark_node;
6599      rhstype = TREE_TYPE (rhs);
6600      coder = TREE_CODE (rhstype);
6601    }
6602
6603  if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6604      || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6605      || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6606    rhs = default_conversion (rhs);
6607  else if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6608    rhs = convert_from_reference (rhs);
6609
6610  rhstype = TREE_TYPE (rhs);
6611  coder = TREE_CODE (rhstype);
6612
6613  /* This should no longer change types on us.  */
6614  if (TREE_CODE (rhs) == CONST_DECL)
6615    rhs = DECL_INITIAL (rhs);
6616  else if (TREE_READONLY_DECL_P (rhs))
6617    rhs = decl_constant_value (rhs);
6618
6619  if (comptypes (type, rhstype, 1))
6620    {
6621      overflow_warning (rhs);
6622      return rhs;
6623    }
6624
6625  if (coder == VOID_TYPE)
6626    {
6627      error ("void value not ignored as it ought to be");
6628      return error_mark_node;
6629    }
6630  /* Arithmetic types all interconvert.  */
6631  if ((codel == INTEGER_TYPE || codel == REAL_TYPE || codel == BOOLEAN_TYPE
6632       || codel == COMPLEX_TYPE)
6633       && (coder == INTEGER_TYPE || coder == REAL_TYPE || coder == BOOLEAN_TYPE
6634	   || coder == COMPLEX_TYPE))
6635    {
6636      /* But we should warn if assigning REAL_TYPE to INTEGER_TYPE.  */
6637      if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6638	{
6639	  if (fndecl)
6640	    cp_warning ("`%T' used for argument %P of `%D'",
6641			rhstype, parmnum, fndecl);
6642	  else
6643	    cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6644	}
6645      /* And we should warn if assigning a negative value to
6646	 an unsigned variable.  */
6647      else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6648	{
6649	  if (TREE_CODE (rhs) == INTEGER_CST
6650	      && TREE_NEGATED_INT (rhs))
6651	    {
6652	      if (fndecl)
6653		cp_warning ("negative value `%E' passed as argument %P of `%D'",
6654			    rhs, parmnum, fndecl);
6655	      else
6656		cp_warning ("%s of negative value `%E' to `%T'",
6657			    errtype, rhs, type);
6658	    }
6659	  overflow_warning (rhs);
6660	  if (TREE_CONSTANT (rhs))
6661	    rhs = fold (rhs);
6662	}
6663
6664      return convert_and_check (type, rhs);
6665    }
6666  /* Conversions involving enums.  */
6667  else if ((codel == ENUMERAL_TYPE
6668	    && (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE))
6669	   || (coder == ENUMERAL_TYPE
6670	       && (INTEGRAL_CODE_P (codel) || codel == REAL_TYPE)))
6671    {
6672      return ocp_convert (type, rhs, CONV_IMPLICIT, LOOKUP_NORMAL);
6673    }
6674  /* Conversions among pointers */
6675  else if (codel == POINTER_TYPE
6676	   && (coder == POINTER_TYPE
6677	       || (coder == RECORD_TYPE
6678		   && (IS_SIGNATURE_POINTER (rhstype)
6679		       || IS_SIGNATURE_REFERENCE (rhstype)))))
6680    {
6681      register tree ttl = TREE_TYPE (type);
6682      register tree ttr;
6683      int ctt = 0;
6684
6685      if (coder == RECORD_TYPE)
6686	{
6687	  rhs = build_optr_ref (rhs);
6688	  rhstype = TREE_TYPE (rhs);
6689	}
6690      ttr = TREE_TYPE (rhstype);
6691
6692      /* If both pointers are of aggregate type, then we
6693	 can give better error messages, and save some work
6694	 as well.  */
6695      if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
6696	{
6697	  tree binfo;
6698
6699	  if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr)
6700	      || type == class_star_type_node
6701	      || rhstype == class_star_type_node)
6702	    binfo = TYPE_BINFO (ttl);
6703	  else
6704	    binfo = get_binfo (ttl, ttr, 1);
6705
6706	  if (binfo == error_mark_node)
6707	    return error_mark_node;
6708	  if (binfo == 0)
6709	    return error_not_base_type (ttl, ttr);
6710
6711	  if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6712	    {
6713	      if (fndecl)
6714		cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6715			    rhstype, parmnum, fndecl);
6716	      else
6717		cp_pedwarn ("%s to `%T' from `%T' discards const",
6718			    errtype, type, rhstype);
6719	    }
6720	  if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6721	    {
6722	      if (fndecl)
6723		cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6724			    rhstype, parmnum, fndecl);
6725	      else
6726		cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6727			    errtype, type, rhstype);
6728	    }
6729	}
6730
6731      /* Any non-function converts to a [const][volatile] void *
6732	 and vice versa; otherwise, targets must be the same.
6733	 Meanwhile, the lhs target must have all the qualifiers of the rhs.  */
6734      else if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6735	       || TYPE_MAIN_VARIANT (ttr) == void_type_node
6736	       || (ctt = comp_target_types (type, rhstype, 1))
6737	       || (unsigned_type (TYPE_MAIN_VARIANT (ttl))
6738		   == unsigned_type (TYPE_MAIN_VARIANT (ttr))))
6739	{
6740	  /* ARM $4.8, commentary on p39.  */
6741	  if (TYPE_MAIN_VARIANT (ttl) == void_type_node
6742	      && TREE_CODE (ttr) == OFFSET_TYPE)
6743	    {
6744	      cp_error ("no standard conversion from `%T' to `void *'", ttr);
6745	      return error_mark_node;
6746	    }
6747
6748	  if (ctt < 0 && TYPE_MAIN_VARIANT (ttl) != TYPE_MAIN_VARIANT (ttr))
6749	    cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6750			rhstype, type);
6751
6752	  if (TYPE_MAIN_VARIANT (ttl) != void_type_node
6753	      && TYPE_MAIN_VARIANT (ttr) == void_type_node
6754	      && ! null_ptr_cst_p (rhs))
6755	    {
6756	      if (coder == RECORD_TYPE)
6757		cp_pedwarn ("implicit conversion of signature pointer to type `%T'",
6758			    type);
6759	      else
6760		pedwarn ("ANSI C++ forbids implicit conversion from `void *' in %s",
6761			 errtype);
6762	    }
6763	  /* Const and volatile mean something different for function types,
6764	     so the usual warnings are not appropriate.  */
6765	  else if ((TREE_CODE (ttr) != FUNCTION_TYPE && TREE_CODE (ttr) != METHOD_TYPE)
6766		   || (TREE_CODE (ttl) != FUNCTION_TYPE && TREE_CODE (ttl) != METHOD_TYPE))
6767	    {
6768	      if (TREE_CODE (ttl) == OFFSET_TYPE
6769		  && binfo_member (TYPE_OFFSET_BASETYPE (ttr),
6770				   CLASSTYPE_VBASECLASSES (TYPE_OFFSET_BASETYPE (ttl))))
6771		{
6772		  sorry ("%s between pointer to members converting across virtual baseclasses", errtype);
6773		  return error_mark_node;
6774		}
6775	      else if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
6776		{
6777		  if (fndecl)
6778		    cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6779				rhstype, parmnum, fndecl);
6780		  else
6781		    cp_pedwarn ("%s to `%T' from `%T' discards const",
6782				errtype, type, rhstype);
6783		}
6784	      else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
6785		{
6786		  if (fndecl)
6787		    cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6788				rhstype, parmnum, fndecl);
6789		  else
6790		    cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6791				errtype, type, rhstype);
6792		}
6793	      else if (TREE_CODE (ttl) == TREE_CODE (ttr)
6794		       && ! comp_target_types (type, rhstype, 1))
6795		{
6796		  if (fndecl)
6797		    cp_pedwarn ("passing `%T' as argument %P of `%D' changes signedness",
6798				rhstype, parmnum, fndecl);
6799		  else
6800		    cp_pedwarn ("%s to `%T' from `%T' changes signedness",
6801				errtype, type, rhstype);
6802		}
6803	    }
6804	}
6805      else
6806	{
6807	  int add_quals = 0, const_parity = 0, volatile_parity = 0;
6808	  int left_const = 1;
6809	  int unsigned_parity;
6810	  int nptrs = 0;
6811
6812	  /* This code is basically a duplicate of comp_ptr_ttypes_real.  */
6813	  for (; ; ttl = TREE_TYPE (ttl), ttr = TREE_TYPE (ttr))
6814	    {
6815	      nptrs -= 1;
6816	      const_parity |= (TYPE_READONLY (ttl) < TYPE_READONLY (ttr));
6817	      volatile_parity |= (TYPE_VOLATILE (ttl) < TYPE_VOLATILE (ttr));
6818
6819	      if (! left_const
6820		  && (TYPE_READONLY (ttl) > TYPE_READONLY (ttr)
6821		      || TYPE_VOLATILE (ttl) > TYPE_VOLATILE (ttr)))
6822		add_quals = 1;
6823	      left_const &= TYPE_READONLY (ttl);
6824
6825	      if (TREE_CODE (ttl) != POINTER_TYPE
6826		  || TREE_CODE (ttr) != POINTER_TYPE)
6827		break;
6828	    }
6829	  unsigned_parity = TREE_UNSIGNED (ttl) - TREE_UNSIGNED (ttr);
6830	  if (unsigned_parity)
6831	    {
6832	      if (TREE_UNSIGNED (ttl))
6833		ttr = unsigned_type (ttr);
6834	      else
6835		ttl = unsigned_type (ttl);
6836	    }
6837
6838	  if (comp_target_types (ttl, ttr, nptrs) > 0)
6839	    {
6840	      if (add_quals)
6841		{
6842		  if (fndecl)
6843		    cp_pedwarn ("passing `%T' as argument %P of `%D' adds cv-quals without intervening `const'",
6844				rhstype, parmnum, fndecl);
6845		  else
6846		    cp_pedwarn ("%s to `%T' from `%T' adds cv-quals without intervening `const'",
6847				errtype, type, rhstype);
6848		}
6849	      if (const_parity)
6850		{
6851		  if (fndecl)
6852		    cp_pedwarn ("passing `%T' as argument %P of `%D' discards const",
6853				rhstype, parmnum, fndecl);
6854		  else
6855		    cp_pedwarn ("%s to `%T' from `%T' discards const",
6856				errtype, type, rhstype);
6857		}
6858	      if (volatile_parity)
6859		{
6860		  if (fndecl)
6861		    cp_pedwarn ("passing `%T' as argument %P of `%D' discards volatile",
6862				rhstype, parmnum, fndecl);
6863		  else
6864		    cp_pedwarn ("%s to `%T' from `%T' discards volatile",
6865				errtype, type, rhstype);
6866		}
6867	      if (unsigned_parity > 0)
6868		{
6869		  if (fndecl)
6870		    cp_pedwarn ("passing `%T' as argument %P of `%D' changes signed to unsigned",
6871				rhstype, parmnum, fndecl);
6872		  else
6873		    cp_pedwarn ("%s to `%T' from `%T' changes signed to unsigned",
6874				errtype, type, rhstype);
6875		}
6876	      else if (unsigned_parity < 0)
6877		{
6878		  if (fndecl)
6879		    cp_pedwarn ("passing `%T' as argument %P of `%D' changes unsigned to signed",
6880				rhstype, parmnum, fndecl);
6881		  else
6882		    cp_pedwarn ("%s to `%T' from `%T' changes unsigned to signed",
6883				errtype, type, rhstype);
6884		}
6885
6886	      /* C++ is not so friendly about converting function and
6887		 member function pointers as C.  Emit warnings here.  */
6888	      if (TREE_CODE (ttl) == FUNCTION_TYPE
6889		  || TREE_CODE (ttl) == METHOD_TYPE)
6890		if (! comptypes (ttl, ttr, 0))
6891		  {
6892		    warning ("conflicting function types in %s:", errtype);
6893		    cp_warning ("\t`%T' != `%T'", type, rhstype);
6894		  }
6895	    }
6896	  else
6897	    {
6898	      if (fndecl)
6899		cp_error ("passing `%T' as argument %P of `%D'",
6900			  rhstype, parmnum, fndecl);
6901	      else
6902		cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6903	      return error_mark_node;
6904	    }
6905	}
6906      return cp_convert (type, rhs);
6907    }
6908  else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
6909    {
6910      /* An explicit constant 0 can convert to a pointer,
6911         but not a 0 that results from casting or folding.  */
6912      if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs)))
6913	{
6914	  if (fndecl)
6915	    cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6916			rhstype, parmnum, fndecl);
6917	  else
6918	    cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6919			errtype, type, rhstype);
6920	}
6921      return cp_convert (type, rhs);
6922    }
6923  else if (codel == INTEGER_TYPE
6924	   && (coder == POINTER_TYPE
6925	       || (coder == RECORD_TYPE
6926		   && (IS_SIGNATURE_POINTER (rhstype)
6927		       || TYPE_PTRMEMFUNC_FLAG (rhstype)
6928		       || IS_SIGNATURE_REFERENCE (rhstype)))))
6929    {
6930      if (fndecl)
6931	cp_pedwarn ("passing `%T' to argument %P of `%D' lacks a cast",
6932		    rhstype, parmnum, fndecl);
6933      else
6934	cp_pedwarn ("%s to `%T' from `%T' lacks a cast",
6935		    errtype, type, rhstype);
6936      return cp_convert (type, rhs);
6937    }
6938  else if (codel == BOOLEAN_TYPE
6939	   && (coder == POINTER_TYPE
6940	       || (coder == RECORD_TYPE
6941		   && (IS_SIGNATURE_POINTER (rhstype)
6942		       || TYPE_PTRMEMFUNC_FLAG (rhstype)
6943		       || IS_SIGNATURE_REFERENCE (rhstype)))))
6944    return cp_convert (type, rhs);
6945
6946  /* C++ */
6947  else if (((coder == POINTER_TYPE
6948	     && TREE_CODE (TREE_TYPE (rhstype)) == METHOD_TYPE)
6949	    || integer_zerop (rhs)
6950	    || TYPE_PTRMEMFUNC_P (rhstype))
6951	   && TYPE_PTRMEMFUNC_P (type))
6952    {
6953      tree ttl = TYPE_PTRMEMFUNC_FN_TYPE (type);
6954      tree ttr = (TYPE_PTRMEMFUNC_P (rhstype)
6955		  ? TYPE_PTRMEMFUNC_FN_TYPE (rhstype)
6956		  : rhstype);
6957      int ctt = (TREE_CODE (rhstype) == INTEGER_TYPE ? 1
6958		 : comp_target_types (ttl, ttr, 1));
6959
6960      if (ctt < 0)
6961	cp_pedwarn ("converting `%T' to `%T' is a contravariance violation",
6962		    ttr, ttl);
6963      else if (ctt == 0)
6964	cp_error ("%s to `%T' from `%T'", errtype, ttl, ttr);
6965
6966      /* compatible pointer to member functions.  */
6967      return build_ptrmemfunc (ttl, rhs, 0);
6968    }
6969  else if (codel == ERROR_MARK || coder == ERROR_MARK)
6970    return error_mark_node;
6971
6972  /* This should no longer happen.  References are initialized via
6973     `convert_for_initialization'.  They should otherwise be
6974     bashed before coming here.  */
6975  else if (codel == REFERENCE_TYPE)
6976    my_friendly_abort (317);
6977  else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (rhs)))
6978    {
6979      tree nrhs = build1 (NOP_EXPR, type, rhs);
6980      TREE_CONSTANT (nrhs) = TREE_CONSTANT (rhs);
6981      return nrhs;
6982    }
6983  else if (TYPE_HAS_CONSTRUCTOR (type) || IS_AGGR_TYPE (TREE_TYPE (rhs)))
6984    return cp_convert (type, rhs);
6985  /* Handle anachronistic conversions from (::*)() to cv void* or (*)().  */
6986  else if (TREE_CODE (type) == POINTER_TYPE
6987	   && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6988	       || TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
6989	   && TREE_TYPE (rhs)
6990	   && TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
6991    return cp_convert (type, rhs);
6992
6993  cp_error ("%s to `%T' from `%T'", errtype, type, rhstype);
6994  return error_mark_node;
6995}
6996
6997/* Convert RHS to be of type TYPE.  If EXP is non-zero,
6998   it is the target of the initialization.
6999   ERRTYPE is a string to use in error messages.
7000
7001   Two major differences between the behavior of
7002   `convert_for_assignment' and `convert_for_initialization'
7003   are that references are bashed in the former, while
7004   copied in the latter, and aggregates are assigned in
7005   the former (operator=) while initialized in the
7006   latter (X(X&)).
7007
7008   If using constructor make sure no conversion operator exists, if one does
7009   exist, an ambiguity exists.
7010
7011   If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything.  */
7012
7013tree
7014convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
7015     tree exp, type, rhs;
7016     int flags;
7017     char *errtype;
7018     tree fndecl;
7019     int parmnum;
7020{
7021  register enum tree_code codel = TREE_CODE (type);
7022  register tree rhstype;
7023  register enum tree_code coder;
7024
7025  /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7026     Strip such NOP_EXPRs, since RHS is used in non-lvalue context.  */
7027  if (TREE_CODE (rhs) == NOP_EXPR
7028      && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
7029      && codel != REFERENCE_TYPE)
7030    rhs = TREE_OPERAND (rhs, 0);
7031
7032  if (rhs == error_mark_node
7033      || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
7034    return error_mark_node;
7035
7036  if (TREE_CODE (TREE_TYPE (rhs)) == OFFSET_TYPE)
7037    {
7038      rhs = resolve_offset_ref (rhs);
7039      if (rhs == error_mark_node)
7040	return error_mark_node;
7041    }
7042
7043  if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
7044    rhs = convert_from_reference (rhs);
7045
7046  if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
7047       && TREE_CODE (type) != ARRAY_TYPE
7048       && (TREE_CODE (type) != REFERENCE_TYPE
7049	   || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
7050      || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
7051      || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
7052    rhs = default_conversion (rhs);
7053
7054  rhstype = TREE_TYPE (rhs);
7055  coder = TREE_CODE (rhstype);
7056
7057  if (coder == UNKNOWN_TYPE)
7058    {
7059      rhs = instantiate_type (type, rhs, 1);
7060      rhstype = TREE_TYPE (rhs);
7061      coder = TREE_CODE (rhstype);
7062    }
7063
7064  if (coder == ERROR_MARK)
7065    return error_mark_node;
7066
7067  /* We accept references to incomplete types, so we can
7068     return here before checking if RHS is of complete type.  */
7069
7070  if (codel == REFERENCE_TYPE)
7071    {
7072      /* This should eventually happen in convert_arguments.  */
7073      extern int warningcount, errorcount;
7074      int savew = 0, savee = 0;
7075
7076      if (fndecl)
7077	savew = warningcount, savee = errorcount;
7078      rhs = convert_to_reference (type, rhs, CONV_IMPLICIT, flags,
7079				  exp ? exp : error_mark_node);
7080      if (fndecl)
7081	{
7082	  if (warningcount > savew)
7083	    cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
7084	  else if (errorcount > savee)
7085	    cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
7086	}
7087      return rhs;
7088    }
7089
7090  rhs = require_complete_type (rhs);
7091  if (rhs == error_mark_node)
7092    return error_mark_node;
7093
7094  if (exp != 0) exp = require_complete_type (exp);
7095  if (exp == error_mark_node)
7096    return error_mark_node;
7097
7098  if (TREE_CODE (rhstype) == REFERENCE_TYPE)
7099    rhstype = TREE_TYPE (rhstype);
7100
7101  type = complete_type (type);
7102
7103  if (TYPE_LANG_SPECIFIC (type)
7104      && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
7105    return build_signature_pointer_constructor (type, rhs);
7106
7107  if (IS_AGGR_TYPE (type))
7108    return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
7109
7110  if (type == TREE_TYPE (rhs))
7111    {
7112      if (TREE_READONLY_DECL_P (rhs))
7113	rhs = decl_constant_value (rhs);
7114      return rhs;
7115    }
7116
7117  return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
7118}
7119
7120/* Expand an ASM statement with operands, handling output operands
7121   that are not variables or INDIRECT_REFS by transforming such
7122   cases into cases that expand_asm_operands can handle.
7123
7124   Arguments are same as for expand_asm_operands.
7125
7126   We don't do default conversions on all inputs, because it can screw
7127   up operands that are expected to be in memory.  */
7128
7129void
7130c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
7131     tree string, outputs, inputs, clobbers;
7132     int vol;
7133     char *filename;
7134     int line;
7135{
7136  int noutputs = list_length (outputs);
7137  register int i;
7138  /* o[I] is the place that output number I should be written.  */
7139  register tree *o = (tree *) alloca (noutputs * sizeof (tree));
7140  register tree tail;
7141
7142  /* Record the contents of OUTPUTS before it is modified.  */
7143  for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7144    o[i] = TREE_VALUE (tail);
7145
7146  /* Generate the ASM_OPERANDS insn;
7147     store into the TREE_VALUEs of OUTPUTS some trees for
7148     where the values were actually stored.  */
7149  expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
7150
7151  /* Copy all the intermediate outputs into the specified outputs.  */
7152  for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
7153    {
7154      if (o[i] != TREE_VALUE (tail))
7155	{
7156	  expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
7157		       const0_rtx, VOIDmode, EXPAND_NORMAL);
7158	  free_temp_slots ();
7159	}
7160      /* Detect modification of read-only values.
7161	 (Otherwise done by build_modify_expr.)  */
7162      else
7163	{
7164	  tree type = TREE_TYPE (o[i]);
7165	  if (TYPE_READONLY (type)
7166	      || ((TREE_CODE (type) == RECORD_TYPE
7167		   || TREE_CODE (type) == UNION_TYPE)
7168		  && C_TYPE_FIELDS_READONLY (type)))
7169	    readonly_error (o[i], "modification by `asm'", 1);
7170	}
7171    }
7172
7173  /* Those MODIFY_EXPRs could do autoincrements.  */
7174  emit_queue ();
7175}
7176
7177/* Expand a C `return' statement.
7178   RETVAL is the expression for what to return,
7179   or a null pointer for `return;' with no value.
7180
7181   C++: upon seeing a `return', we must call destructors on all
7182   variables in scope which had constructors called on them.
7183   This means that if in a destructor, the base class destructors
7184   must be called before returning.
7185
7186   The RETURN statement in C++ has initialization semantics.  */
7187
7188void
7189c_expand_return (retval)
7190     tree retval;
7191{
7192  extern struct nesting *cond_stack, *loop_stack, *case_stack;
7193  extern tree dtor_label, ctor_label;
7194  tree result = DECL_RESULT (current_function_decl);
7195  tree valtype = TREE_TYPE (result);
7196
7197  if (TREE_THIS_VOLATILE (current_function_decl))
7198    warning ("function declared `noreturn' has a `return' statement");
7199
7200  if (retval == error_mark_node)
7201    {
7202      current_function_returns_null = 1;
7203      return;
7204    }
7205
7206  if (processing_template_decl)
7207    {
7208      add_tree (build_min_nt (RETURN_STMT, retval));
7209      return;
7210    }
7211
7212  if (dtor_label)
7213    {
7214      if (retval)
7215	error ("returning a value from a destructor");
7216
7217      /* Can't just return from a destructor.  */
7218      expand_goto (dtor_label);
7219      return;
7220    }
7221
7222  if (retval == NULL_TREE)
7223    {
7224      /* A non-named return value does not count.  */
7225
7226      if (DECL_CONSTRUCTOR_P (current_function_decl))
7227	retval = current_class_ptr;
7228      else if (DECL_NAME (result) != NULL_TREE
7229	       && TREE_CODE (valtype) != VOID_TYPE)
7230	retval = result;
7231      else
7232	{
7233	  current_function_returns_null = 1;
7234
7235	  if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
7236	    {
7237	      if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
7238		{
7239		  pedwarn ("`return' with no value, in function returning non-void");
7240		  /* Clear this, so finish_function won't say that we
7241		     reach the end of a non-void function (which we don't,
7242		     we gave a return!).  */
7243		  current_function_returns_null = 0;
7244		}
7245	    }
7246
7247	  expand_null_return ();
7248	  return;
7249	}
7250    }
7251  else if (DECL_CONSTRUCTOR_P (current_function_decl))
7252    {
7253      if (flag_this_is_variable)
7254	error ("return from a constructor: use `this = ...' instead");
7255      else
7256	error ("returning a value from a constructor");
7257      retval = current_class_ptr;
7258    }
7259
7260  /* Effective C++ rule 15.  See also start_function.  */
7261  if (warn_ecpp
7262      && DECL_NAME (current_function_decl) == ansi_opname[(int) MODIFY_EXPR]
7263      && retval != current_class_ref)
7264    cp_warning ("`operator=' should return a reference to `*this'");
7265
7266  if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
7267    {
7268      current_function_returns_null = 1;
7269      if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7270	pedwarn ("`return' with a value, in function returning void");
7271      expand_return (retval);
7272      return;
7273    }
7274
7275  /* Now deal with possible C++ hair:
7276     (1) Compute the return value.
7277     (2) If there are aggregate values with destructors which
7278     must be cleaned up, clean them (taking care
7279     not to clobber the return value).
7280     (3) If an X(X&) constructor is defined, the return
7281     value must be returned via that.  */
7282
7283  if (retval == result
7284      || DECL_CONSTRUCTOR_P (current_function_decl))
7285    /* It's already done for us.  */;
7286  else if (TREE_TYPE (retval) == void_type_node)
7287    {
7288      pedwarn ("return of void value in function returning non-void");
7289      expand_expr_stmt (retval);
7290      retval = 0;
7291    }
7292  else
7293    {
7294      tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
7295
7296      /* First convert the value to the function's return type, then
7297	 to the type of return value's location to handle the
7298         case that functype is thiner than the valtype. */
7299
7300      retval = convert_for_initialization
7301	(NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
7302	 "return", NULL_TREE, 0);
7303
7304      retval = convert (valtype, retval);
7305
7306      if (retval == error_mark_node)
7307	{
7308	  /* Avoid warning about control reaching end of function.  */
7309	  expand_null_return ();
7310	  return;
7311	}
7312
7313      /* We can't initialize a register from a AGGR_INIT_EXPR.  */
7314      else if (! current_function_returns_struct
7315	       && TREE_CODE (retval) == TARGET_EXPR
7316	       && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
7317	retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
7318			TREE_OPERAND (retval, 0));
7319
7320      /* Add some useful error checking for C++.  */
7321      else if (TREE_CODE (valtype) == REFERENCE_TYPE)
7322	{
7323	  tree whats_returned;
7324
7325	  /* Sort through common things to see what it is
7326	     we are returning.  */
7327	  whats_returned = retval;
7328	  if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
7329	    {
7330	      whats_returned = TREE_OPERAND (whats_returned, 1);
7331	      if (TREE_CODE (whats_returned) == ADDR_EXPR)
7332		whats_returned = TREE_OPERAND (whats_returned, 0);
7333	    }
7334	  while (TREE_CODE (whats_returned) == CONVERT_EXPR
7335		 || TREE_CODE (whats_returned) == NOP_EXPR)
7336	    whats_returned = TREE_OPERAND (whats_returned, 0);
7337	  if (TREE_CODE (whats_returned) == ADDR_EXPR)
7338	    {
7339	      whats_returned = TREE_OPERAND (whats_returned, 0);
7340	      while (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
7341		     || TREE_CODE (whats_returned) == TARGET_EXPR)
7342		{
7343		  /* Get the target.  */
7344		  whats_returned = TREE_OPERAND (whats_returned, 0);
7345		  warning ("returning reference to temporary");
7346		}
7347	    }
7348
7349	  if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
7350	    {
7351	      if (TEMP_NAME_P (DECL_NAME (whats_returned)))
7352		warning ("reference to non-lvalue returned");
7353	      else if (TREE_CODE (TREE_TYPE (whats_returned)) != REFERENCE_TYPE
7354		       && ! TREE_STATIC (whats_returned)
7355		       && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
7356		       && !TREE_PUBLIC (whats_returned))
7357		cp_warning_at ("reference to local variable `%D' returned", whats_returned);
7358	    }
7359	}
7360      else if (TREE_CODE (retval) == ADDR_EXPR)
7361	{
7362	  tree whats_returned = TREE_OPERAND (retval, 0);
7363
7364	  if (TREE_CODE (whats_returned) == VAR_DECL
7365	      && DECL_NAME (whats_returned)
7366	      && IDENTIFIER_LOCAL_VALUE (DECL_NAME (whats_returned))
7367	      && !TREE_STATIC (whats_returned)
7368	      && !TREE_PUBLIC (whats_returned))
7369	    cp_warning_at ("address of local variable `%D' returned", whats_returned);
7370	}
7371    }
7372
7373  if (retval != NULL_TREE
7374      && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
7375      && cond_stack == 0 && loop_stack == 0 && case_stack == 0)
7376    current_function_return_value = retval;
7377
7378  if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
7379    {
7380      /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do.  */
7381      expand_goto (ctor_label);
7382    }
7383
7384  if (retval && retval != result)
7385    {
7386      result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
7387      TREE_SIDE_EFFECTS (result) = 1;
7388    }
7389
7390  expand_start_target_temps ();
7391
7392  expand_return (result);
7393
7394  expand_end_target_temps ();
7395
7396  current_function_returns_value = 1;
7397}
7398
7399/* Start a C switch statement, testing expression EXP.
7400   Return EXP if it is valid, an error node otherwise.  */
7401
7402tree
7403c_expand_start_case (exp)
7404     tree exp;
7405{
7406  tree type;
7407  register enum tree_code code;
7408
7409  /* Convert from references, etc.  */
7410  exp = default_conversion (exp);
7411  type = TREE_TYPE (exp);
7412  code = TREE_CODE (type);
7413
7414  if (IS_AGGR_TYPE_CODE (code))
7415    exp = build_type_conversion (CONVERT_EXPR, integer_type_node, exp, 1);
7416
7417  if (exp == NULL_TREE)
7418    {
7419      error ("switch quantity not an integer");
7420      exp = error_mark_node;
7421    }
7422  type = TREE_TYPE (exp);
7423  code = TREE_CODE (type);
7424
7425  if (code != INTEGER_TYPE && code != ENUMERAL_TYPE && code != ERROR_MARK)
7426    {
7427      error ("switch quantity not an integer");
7428      exp = error_mark_node;
7429    }
7430  else
7431    {
7432      tree idx;
7433
7434      exp = default_conversion (exp);
7435      type = TREE_TYPE (exp);
7436      idx = get_unwidened (exp, 0);
7437      /* We can't strip a conversion from a signed type to an unsigned,
7438	 because if we did, int_fits_type_p would do the wrong thing
7439	 when checking case values for being in range,
7440	 and it's too hard to do the right thing.  */
7441      if (TREE_UNSIGNED (TREE_TYPE (exp))
7442	  == TREE_UNSIGNED (TREE_TYPE (idx)))
7443	exp = idx;
7444    }
7445
7446  expand_start_case
7447    (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
7448     type, "switch statement");
7449
7450  return exp;
7451}
7452
7453/* Returns non-zero if the pointer-type FROM can be converted to the
7454   pointer-type TO via a qualification conversion.  If CONSTP is -1,
7455   then we return non-zero if the pointers are similar, and the
7456   cv-qualification signature of FROM is a proper subset of that of TO.
7457
7458   If CONSTP is positive, then all outer pointers have been
7459   const-qualified.  */
7460
7461static int
7462comp_ptr_ttypes_real (to, from, constp)
7463     tree to, from;
7464     int constp;
7465{
7466  int to_more_cv_qualified = 0;
7467
7468  for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7469    {
7470      if (TREE_CODE (to) != TREE_CODE (from))
7471	return 0;
7472
7473      if (TREE_CODE (from) == OFFSET_TYPE
7474	  && comptypes (TYPE_OFFSET_BASETYPE (from),
7475			TYPE_OFFSET_BASETYPE (to), 1))
7476	  continue;
7477
7478      /* Const and volatile mean something different for function types,
7479	 so the usual checks are not appropriate.  */
7480      if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7481	{
7482	  if (TYPE_READONLY (from) > TYPE_READONLY (to)
7483	      || TYPE_VOLATILE (from) > TYPE_VOLATILE (to))
7484	    return 0;
7485
7486	  if (TYPE_READONLY (to) > TYPE_READONLY (from)
7487	      || TYPE_VOLATILE (to) > TYPE_VOLATILE (from))
7488	    {
7489	      if (constp == 0)
7490		return 0;
7491	      else
7492		++to_more_cv_qualified;
7493	    }
7494
7495	  if (constp > 0)
7496	    constp &= TYPE_READONLY (to);
7497	}
7498
7499      if (TREE_CODE (to) != POINTER_TYPE)
7500	return
7501	  comptypes (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 1)
7502	  && (constp >= 0 || to_more_cv_qualified);
7503    }
7504}
7505
7506/* When comparing, say, char ** to char const **, this function takes the
7507   'char *' and 'char const *'.  Do not pass non-pointer types to this
7508   function.  */
7509
7510int
7511comp_ptr_ttypes (to, from)
7512     tree to, from;
7513{
7514  return comp_ptr_ttypes_real (to, from, 1);
7515}
7516
7517/* Returns 1 if to and from are (possibly multi-level) pointers to the same
7518   type or inheritance-related types, regardless of cv-quals.  */
7519
7520int
7521ptr_reasonably_similar (to, from)
7522     tree to, from;
7523{
7524  for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7525    {
7526      if (TREE_CODE (to) != TREE_CODE (from))
7527	return 0;
7528
7529      if (TREE_CODE (from) == OFFSET_TYPE
7530	  && comptypes (TYPE_OFFSET_BASETYPE (to),
7531			TYPE_OFFSET_BASETYPE (from), -1))
7532	continue;
7533
7534      if (TREE_CODE (to) != POINTER_TYPE)
7535	return comptypes
7536	  (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), -1);
7537    }
7538}
7539
7540/* Like comp_ptr_ttypes, for const_cast.  */
7541
7542static int
7543comp_ptr_ttypes_const (to, from)
7544     tree to, from;
7545{
7546  for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7547    {
7548      if (TREE_CODE (to) != TREE_CODE (from))
7549	return 0;
7550
7551      if (TREE_CODE (from) == OFFSET_TYPE
7552	  && comptypes (TYPE_OFFSET_BASETYPE (from),
7553			TYPE_OFFSET_BASETYPE (to), 1))
7554	  continue;
7555
7556      if (TREE_CODE (to) != POINTER_TYPE)
7557	return comptypes (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 1);
7558    }
7559}
7560
7561/* Like comp_ptr_ttypes, for reinterpret_cast.  */
7562
7563static int
7564comp_ptr_ttypes_reinterpret (to, from)
7565     tree to, from;
7566{
7567  int constp = 1;
7568
7569  for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7570    {
7571      if (TREE_CODE (from) == OFFSET_TYPE)
7572	from = TREE_TYPE (from);
7573      if (TREE_CODE (to) == OFFSET_TYPE)
7574	to = TREE_TYPE (to);
7575
7576      if (TREE_CODE (to) != TREE_CODE (from))
7577	return 1;
7578
7579      /* Const and volatile mean something different for function types,
7580	 so the usual checks are not appropriate.  */
7581      if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7582	{
7583	  if (TYPE_READONLY (from) > TYPE_READONLY (to)
7584	      || TYPE_VOLATILE (from) > TYPE_VOLATILE (to))
7585	    return 0;
7586
7587	  if (! constp
7588	      && (TYPE_READONLY (to) > TYPE_READONLY (from)
7589		  || TYPE_VOLATILE (to) > TYPE_READONLY (from)))
7590	    return 0;
7591	  constp &= TYPE_READONLY (to);
7592	}
7593
7594      if (TREE_CODE (to) != POINTER_TYPE)
7595	return 1;
7596    }
7597}
7598