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