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