OSDN Git Service

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