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